Skip to content

Commit c3718ea

Browse files
committed
Tweak context summary format
1 parent 6248218 commit c3718ea

3 files changed

Lines changed: 97 additions & 84 deletions

File tree

agents/__tests__/context-pruner.test.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@ describe('context-pruner handleSteps', () => {
294294

295295
// Should use a memory artifact format, not transcript role markers
296296
expect(content).toContain('<historical_memory>')
297-
expect(content).toContain('User request:')
297+
expect(content).toContain('[USER]')
298298
expect(content).toContain('Progress note:')
299-
expect(content).not.toContain('[USER]')
300299
expect(content).not.toContain('[ASSISTANT]')
301300
})
302301

@@ -321,8 +320,8 @@ describe('context-pruner handleSteps', () => {
321320
const content = results[0].input.messages[0].content[0].text
322321

323322
// Should contain tool summaries
324-
expect(content).toContain('Previously inspected files: file1.ts, file2.ts')
325-
expect(content).toContain('Previously edited file: file1.ts')
323+
expect(content).toContain('inspected files: file1.ts, file2.ts')
324+
expect(content).toContain('edited file: file1.ts')
326325
})
327326

328327
test('summarizes various tool types correctly', () => {
@@ -350,10 +349,10 @@ describe('context-pruner handleSteps', () => {
350349
const results = runHandleSteps(messages, 50000, 10000)
351350
const content = results[0].input.messages[0].content[0].text
352351

353-
expect(content).toContain('Previously wrote file: new-file.ts')
354-
expect(content).toContain('Previously ran command: npm test')
355-
expect(content).toContain('Previous code search for "function"')
356-
expect(content).toContain('Previously delegated agents:')
352+
expect(content).toContain('wrote file: new-file.ts')
353+
expect(content).toContain('ran command: npm test')
354+
expect(content).toContain('code search for "function"')
355+
expect(content).toContain('delegated agents:')
357356
expect(content).toContain('- file-picker')
358357
expect(content).toContain('- commander')
359358
})
@@ -387,7 +386,7 @@ describe('context-pruner handleSteps', () => {
387386
const results = runHandleSteps(messages, 50000, 10000)
388387
const content = results[0].input.messages[0].content[0].text
389388

390-
expect(content).toContain('User request [image(s) were attached]:')
389+
expect(content).toContain('[USER] [image(s) were attached]')
391390
})
392391

393392
test('removes only INSTRUCTIONS_PROMPT and SUBAGENT_SPAWN when under context limit', () => {
@@ -569,7 +568,7 @@ describe('context-pruner handleSteps', () => {
569568
.text
570569
expect(summaryContent).toContain('PLEASE FIX THE BUG')
571570
expect(summaryContent).toContain('I found the likely issue.')
572-
expect(summaryContent).toContain('Previously inspected files: src/bug.ts')
571+
expect(summaryContent).toContain('inspected files: src/bug.ts')
573572

574573
expect(resultMessages[1].role).toBe('user')
575574
expect(resultMessages[1].tags).toBeUndefined()
@@ -653,7 +652,7 @@ describe('context-pruner handleSteps', () => {
653652
const results = runHandleSteps(messages, 50000, 10000)
654653
const content = results[0].input.messages[0].content[0].text
655654

656-
expect(content).toContain('Previously delegated agent file-picker')
655+
expect(content).toContain('delegated agent file-picker')
657656
})
658657

659658
test('handles long terminal commands by truncating', () => {
@@ -672,7 +671,7 @@ describe('context-pruner handleSteps', () => {
672671

673672
// Should truncate to 50 chars + ...
674673
expect(content).toContain(
675-
'Previously ran command: npm run build -- --config=production --verbose --o...',
674+
'ran command: npm run build -- --config=production --verbose --o...',
676675
)
677676
})
678677

@@ -686,7 +685,7 @@ describe('context-pruner handleSteps', () => {
686685
const results = runHandleSteps(messages, 50000, 10000)
687686
const content = results[0].input.messages[0].content[0].text
688687

689-
expect(content).toContain('Previously used tool unknown_tool_name')
688+
expect(content).toContain('used tool unknown_tool_name')
690689
})
691690

692691
test('handles multiple tool calls in single assistant message', () => {
@@ -719,8 +718,8 @@ describe('context-pruner handleSteps', () => {
719718
const content = results[0].input.messages[0].content[0].text
720719

721720
// Both tool calls should be in the summary
722-
expect(content).toContain('Previously inspected files: a.ts')
723-
expect(content).toContain('Previously inspected files: b.ts')
721+
expect(content).toContain('inspected files: a.ts')
722+
expect(content).toContain('inspected files: b.ts')
724723
})
725724

726725
test('handles mixed text and tool calls in assistant message', () => {
@@ -748,7 +747,7 @@ describe('context-pruner handleSteps', () => {
748747

749748
// Should have both text and tool summary
750749
expect(content).toContain('Let me read that file for you')
751-
expect(content).toContain('Previously inspected files: test.ts')
750+
expect(content).toContain('inspected files: test.ts')
752751
})
753752
})
754753

@@ -893,7 +892,7 @@ describe('context-pruner code_search with flags', () => {
893892
const content = results[0].input.messages[0].content[0].text
894893

895894
expect(content).toContain(
896-
'Previous code search for "myFunction" (-g *.ts -i)',
895+
'code search for "myFunction" (-g *.ts -i)',
897896
)
898897
})
899898
})
@@ -1775,7 +1774,7 @@ describe('context-pruner str_replace and write_file tool results', () => {
17751774
const content = results[0].input.messages[0].content[0].text
17761775

17771776
// Should have both the tool call summary and the full result
1778-
expect(content).toContain('Previously edited file: src/file.ts')
1777+
expect(content).toContain('edited file: src/file.ts')
17791778
expect(content).toContain('Edit result from str_replace:')
17801779
expect(content).toContain('errorMessage')
17811780
expect(content).toContain('No match found for old string')
@@ -1826,7 +1825,7 @@ describe('context-pruner glob and list_directory tools', () => {
18261825
const results = runHandleSteps(messages)
18271826
const content = results[0].input.messages[0].content[0].text
18281827

1829-
expect(content).toContain('Previous glob search for **/*.ts')
1828+
expect(content).toContain('glob search for **/*.ts')
18301829
})
18311830

18321831
test('summarizes list_directory tool with path', () => {
@@ -1841,7 +1840,7 @@ describe('context-pruner glob and list_directory tools', () => {
18411840
const results = runHandleSteps(messages)
18421841
const content = results[0].input.messages[0].content[0].text
18431842

1844-
expect(content).toContain('Previously listed directory: src')
1843+
expect(content).toContain('listed directory: src')
18451844
})
18461845

18471846
test('summarizes read_subtree tool with paths', () => {
@@ -1857,7 +1856,7 @@ describe('context-pruner glob and list_directory tools', () => {
18571856
const content = results[0].input.messages[0].content[0].text
18581857

18591858
expect(content).toContain(
1860-
'Previously inspected subtrees: src/components, src/utils',
1859+
'inspected subtrees: src/components, src/utils',
18611860
)
18621861
})
18631862
})
@@ -2357,10 +2356,10 @@ describe('context-pruner dual-budget behavior', () => {
23572356

23582357
// === Tool call summaries present ===
23592358
expect(content).toContain(
2360-
'Previously inspected files: src/model.ts, src/service.ts',
2359+
'inspected files: src/model.ts, src/service.ts',
23612360
)
2362-
expect(content).toContain('Previously edited file: src/model.ts')
2363-
expect(content).toContain('Previously delegated agents:')
2361+
expect(content).toContain('edited file: src/model.ts')
2362+
expect(content).toContain('delegated agents:')
23642363

23652364
// === str_replace result: present but truncated at 2k chars ===
23662365
expect(content).toContain('Edit result from str_replace:')

agents/context-pruner.ts

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -137,77 +137,73 @@ const definition: AgentDefinition = {
137137
case 'read_files': {
138138
const paths = input.paths as string[] | undefined
139139
if (paths && paths.length > 0) {
140-
return `Previously inspected files: ${paths.join(', ')}`
140+
return `inspected files: ${paths.join(', ')}`
141141
}
142-
return 'Previously inspected files'
142+
return 'inspected files'
143143
}
144144
case 'write_file': {
145145
const path = input.path as string | undefined
146-
return path
147-
? `Previously wrote file: ${path}`
148-
: 'Previously wrote a file'
146+
return path ? `wrote file: ${path}` : 'wrote a file'
149147
}
150148
case 'str_replace': {
151149
const path = input.path as string | undefined
152-
return path
153-
? `Previously edited file: ${path}`
154-
: 'Previously edited a file'
150+
return path ? `edited file: ${path}` : 'edited a file'
155151
}
156152
case 'propose_write_file': {
157153
const path = input.path as string | undefined
158154
return path
159-
? `Previously proposed writing: ${path}`
160-
: 'Previously proposed a file write'
155+
? `proposed writing: ${path}`
156+
: 'proposed a file write'
161157
}
162158
case 'propose_str_replace': {
163159
const path = input.path as string | undefined
164160
return path
165-
? `Previously proposed editing: ${path}`
166-
: 'Previously proposed a file edit'
161+
? `proposed editing: ${path}`
162+
: 'proposed a file edit'
167163
}
168164
case 'read_subtree': {
169165
const paths = input.paths as string[] | undefined
170166
if (paths && paths.length > 0) {
171-
return `Previously inspected subtrees: ${paths.join(', ')}`
167+
return `inspected subtrees: ${paths.join(', ')}`
172168
}
173-
return 'Previously inspected a subtree'
169+
return 'inspected a subtree'
174170
}
175171
case 'code_search': {
176172
const pattern = input.pattern as string | undefined
177173
const flags = input.flags as string | undefined
178174
if (pattern && flags) {
179-
return `Previous code search for "${pattern}" (${flags})`
175+
return `code search for "${pattern}" (${flags})`
180176
}
181177
return pattern
182-
? `Previous code search for "${pattern}"`
183-
: 'Previous code search'
178+
? `code search for "${pattern}"`
179+
: 'code search'
184180
}
185181
case 'glob': {
186182
const pattern = input.pattern as string | undefined
187183
return pattern
188-
? `Previous glob search for ${pattern}`
189-
: 'Previous glob search'
184+
? `glob search for ${pattern}`
185+
: 'glob search'
190186
}
191187
case 'list_directory': {
192188
const path = input.path as string | undefined
193189
return path
194-
? `Previously listed directory: ${path}`
195-
: 'Previously listed a directory'
190+
? `listed directory: ${path}`
191+
: 'listed a directory'
196192
}
197193
case 'find_files': {
198194
const prompt = input.prompt as string | undefined
199195
return prompt
200-
? `Previous file-finding request: "${prompt}"`
201-
: 'Previous file-finding request'
196+
? `file-finding request: "${prompt}"`
197+
: 'file-finding request'
202198
}
203199
case 'run_terminal_command': {
204200
const command = input.command as string | undefined
205201
if (command) {
206202
const shortCmd =
207203
command.length > 50 ? command.slice(0, 50) + '...' : command
208-
return `Previously ran command: ${shortCmd}`
204+
return `ran command: ${shortCmd}`
209205
}
210-
return 'Previously ran a terminal command'
206+
return 'ran a terminal command'
211207
}
212208
case 'spawn_agents':
213209
case 'spawn_agent_inline': {
@@ -248,7 +244,7 @@ const definition: AgentDefinition = {
248244
}
249245
return detail
250246
})
251-
return `Previously delegated agents:\n${agentDetails.map((d) => `- ${d}`).join('\n')}`
247+
return `delegated agents:\n${agentDetails.map((d) => `- ${d}`).join('\n')}`
252248
}
253249
if (agentType) {
254250
const extras: string[] = []
@@ -266,11 +262,11 @@ const definition: AgentDefinition = {
266262
extras.push(`params: ${truncatedParams}`)
267263
}
268264
if (extras.length > 0) {
269-
return `Previously delegated agent ${agentType} (${extras.join(', ')})`
265+
return `delegated agent ${agentType} (${extras.join(', ')})`
270266
}
271-
return `Previously delegated agent ${agentType}`
267+
return `delegated agent ${agentType}`
272268
}
273-
return 'Previously delegated agent work'
269+
return 'delegated agent work'
274270
}
275271
case 'write_todos': {
276272
const todos = input.todos as
@@ -308,35 +304,35 @@ const definition: AgentDefinition = {
308304
case 'web_search': {
309305
const query = input.query as string | undefined
310306
return query
311-
? `Previous web search for "${query}"`
312-
: 'Previous web search'
307+
? `web search for "${query}"`
308+
: 'web search'
313309
}
314310
case 'gravity_index': {
315311
const query = input.query as string | undefined
316312
const action = input.action as string | undefined
317313
if (query) {
318-
return `Previous Gravity Index ${action ?? 'search'} for "${query}"`
314+
return `Gravity Index ${action ?? 'search'} for "${query}"`
319315
}
320316
return action
321-
? `Previous Gravity Index ${action}`
322-
: 'Previous Gravity Index use'
317+
? `Gravity Index ${action}`
318+
: 'Gravity Index use'
323319
}
324320
case 'read_docs': {
325321
const libraryTitle = input.libraryTitle as string | undefined
326322
const topic = input.topic as string | undefined
327323
if (libraryTitle && topic) {
328-
return `Previously consulted docs: ${libraryTitle} - ${topic}`
324+
return `consulted docs: ${libraryTitle} - ${topic}`
329325
}
330326
return libraryTitle
331-
? `Previously consulted docs: ${libraryTitle}`
332-
: 'Previously consulted docs'
327+
? `consulted docs: ${libraryTitle}`
328+
: 'consulted docs'
333329
}
334330
case 'set_output':
335-
return 'Previously set structured output'
331+
return 'set structured output'
336332
case 'set_messages':
337-
return 'Previously updated message history'
333+
return 'updated message history'
338334
default:
339-
return `Previously used tool ${toolName}`
335+
return `used tool ${toolName}`
340336
}
341337
}
342338

@@ -483,8 +479,7 @@ const definition: AgentDefinition = {
483479
return chunks.map((chunk) => {
484480
const trimmed = chunk.trim()
485481
const isUser =
486-
trimmed.startsWith('[USER]\n') ||
487-
trimmed.startsWith('[USER] [with image') ||
482+
trimmed.startsWith('[USER]') ||
488483
trimmed.startsWith('User request') ||
489484
trimmed.startsWith('User message') ||
490485
trimmed.startsWith('Current unresolved user request')
@@ -572,7 +567,7 @@ const definition: AgentDefinition = {
572567
const imageNote = hasImages ? ' [image(s) were attached]' : ''
573568
summarizedEntries.push({
574569
role: 'user',
575-
parts: [`User request${imageNote}:\n${text}`],
570+
parts: [`[USER]${imageNote}\n${text}`],
576571
})
577572
}
578573
} else if (message.role === 'assistant') {
@@ -606,7 +601,7 @@ const definition: AgentDefinition = {
606601
parts.push(`Progress note:\n${combinedText}`)
607602
}
608603
if (toolSummaries.length > 0) {
609-
parts.push(`Prior action record:\n${toolSummaries.join('\n')}`)
604+
parts.push(toolSummaries.join('\n'))
610605
}
611606

612607
if (parts.length > 0) {

0 commit comments

Comments
 (0)