Skip to content

Commit 99b5e40

Browse files
committed
refactor(logger): move logger into AgentStepContext\n\nCentralizes logging in AgentStepContext to simplify step signatures and improve context-aware tracing. This reduces parameter surface area and ensures logging is consistently tied to agent context.\n\n🤖 Generated with Codebuff\nCo-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 2cf6321 commit 99b5e40

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

.agents/__tests__/context-pruner.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ describe('context-pruner handleSteps', () => {
7272
warn: () => {},
7373
error: () => {},
7474
}
75-
const generator = contextPruner.handleSteps!({ agentState: mockAgentState }, mockLogger)
75+
const generator = contextPruner.handleSteps!({
76+
agentState: mockAgentState,
77+
logger: mockLogger,
78+
})
7679
const results: any[] = []
7780
let result = generator.next()
7881
while (!result.done) {
@@ -336,7 +339,10 @@ describe('context-pruner edge cases', () => {
336339
warn: () => {},
337340
error: () => {},
338341
}
339-
const generator = contextPruner.handleSteps!({ agentState: mockAgentState }, mockLogger)
342+
const generator = contextPruner.handleSteps!({
343+
agentState: mockAgentState,
344+
logger: mockLogger,
345+
})
340346
const results: ReturnType<typeof generator.next>['value'][] = []
341347
let result = generator.next()
342348
while (!result.done) {

.agents/types/agent-definition.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface AgentDefinition {
155155
* Or use 'return' to end the turn.
156156
*
157157
* Example 1:
158-
* function* handleSteps({ agentStep, prompt, params}, logger) {
158+
* function* handleSteps({ agentState, prompt, params, logger }) {
159159
* logger.info('Starting file read process')
160160
* const { toolResult } = yield {
161161
* toolName: 'read_files',
@@ -174,7 +174,7 @@ export interface AgentDefinition {
174174
* }
175175
*
176176
* Example 2:
177-
* handleSteps: function* ({ agentState, prompt, params }, logger) {
177+
* handleSteps: function* ({ agentState, prompt, params, logger }) {
178178
* while (true) {
179179
* logger.debug('Spawning thinker agent')
180180
* yield {
@@ -193,10 +193,7 @@ export interface AgentDefinition {
193193
* }
194194
* }
195195
*/
196-
handleSteps?: (
197-
context: AgentStepContext,
198-
logger: Logger,
199-
) => Generator<
196+
handleSteps?: (context: AgentStepContext) => Generator<
200197
ToolCall | 'STEP' | 'STEP_ALL',
201198
void,
202199
{
@@ -230,6 +227,7 @@ export interface AgentStepContext {
230227
agentState: AgentState
231228
prompt?: string
232229
params?: Record<string, any>
230+
logger: Logger
233231
}
234232

235233
/**

backend/src/run-programmatic-step.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,19 @@ export async function runProgrammaticStep(
118118
agentState,
119119
prompt,
120120
params,
121+
logger: streamingLogger,
121122
},
122123
undefined, // config
123-
streamingLogger, // pass the streaming logger instance
124+
streamingLogger, // pass the streaming logger instance for internal use
124125
)
125126
} else {
126127
// Initialize native generator
127-
generator = (template.handleSteps as any)(
128-
{
129-
agentState,
130-
prompt,
131-
params,
132-
},
133-
streamingLogger,
134-
)
128+
generator = (template.handleSteps as any)({
129+
agentState,
130+
prompt,
131+
params,
132+
logger: streamingLogger,
133+
})
135134
runIdToGenerator[agentState.runId] = generator
136135
}
137136
}

backend/src/util/quickjs-sandbox.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ export class QuickJSSandbox {
143143
// Agent function
144144
const handleSteps = ${generatorCode};
145145
146-
// Create generator instance
147-
let generator = handleSteps(${JSON.stringify(initialInput)}, logger);
146+
// Create generator instance with logger injected into context
147+
const context = ${JSON.stringify(initialInput)};
148+
context.logger = logger;
149+
let generator = handleSteps(context);
148150
149151
// Generator management
150152
globalThis._generator = generator;

common/src/templates/initial-agents-dir/types/agent-definition.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface AgentDefinition {
155155
* Or use 'return' to end the turn.
156156
*
157157
* Example 1:
158-
* function* handleSteps({ agentStep, prompt, params}, logger) {
158+
* function* handleSteps({ agentState, prompt, params, logger }) {
159159
* logger.info('Starting file read process')
160160
* const { toolResult } = yield {
161161
* toolName: 'read_files',
@@ -174,7 +174,7 @@ export interface AgentDefinition {
174174
* }
175175
*
176176
* Example 2:
177-
* handleSteps: function* ({ agentState, prompt, params }, logger) {
177+
* handleSteps: function* ({ agentState, prompt, params, logger }) {
178178
* while (true) {
179179
* logger.debug('Spawning thinker agent')
180180
* yield {
@@ -193,10 +193,7 @@ export interface AgentDefinition {
193193
* }
194194
* }
195195
*/
196-
handleSteps?: (
197-
context: AgentStepContext,
198-
logger: Logger,
199-
) => Generator<
196+
handleSteps?: (context: AgentStepContext) => Generator<
200197
ToolCall | 'STEP' | 'STEP_ALL',
201198
void,
202199
{
@@ -230,6 +227,7 @@ export interface AgentStepContext {
230227
agentState: AgentState
231228
prompt?: string
232229
params?: Record<string, any>
230+
logger: Logger
233231
}
234232

235233
/**

0 commit comments

Comments
 (0)