Skip to content

Commit aa15e68

Browse files
committed
context pruner: Include file editing results in summary. exclude some more subagents
1 parent fd575f2 commit aa15e68

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

agents/__tests__/context-pruner.test.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,46 +1552,51 @@ describe('context-pruner str_replace and write_file tool results', () => {
15521552
return results
15531553
}
15541554

1555-
test('includes str_replace diff in summary', () => {
1555+
test('includes str_replace result in summary', () => {
15561556
const messages = [
15571557
createMessage('user', 'Edit this file'),
15581558
createToolCallMessage('call-1', 'str_replace', {
15591559
path: 'src/utils.ts',
15601560
replacements: [{ old: 'foo', new: 'bar' }],
15611561
}),
15621562
createToolResultMessage('call-1', 'str_replace', {
1563-
diff: '--- a/src/utils.ts\n+++ b/src/utils.ts\n@@ -1,1 +1,1 @@\n-foo\n+bar',
1563+
file: 'src/utils.ts',
1564+
message: 'Updated file',
1565+
unifiedDiff: '--- a/src/utils.ts\n+++ b/src/utils.ts\n@@ -1,1 +1,1 @@\n-foo\n+bar',
15641566
}),
15651567
]
15661568

15671569
const results = runHandleSteps(messages)
15681570
const content = results[0].input.messages[0].content[0].text
15691571

1570-
expect(content).toContain('[EDIT RESULT]')
1572+
expect(content).toContain('[EDIT RESULT: str_replace]')
1573+
expect(content).toContain('unifiedDiff')
15711574
expect(content).toContain('-foo')
15721575
expect(content).toContain('+bar')
15731576
})
15741577

1575-
test('includes write_file diff in summary', () => {
1578+
test('includes write_file result in summary', () => {
15761579
const messages = [
15771580
createMessage('user', 'Create a new file'),
15781581
createToolCallMessage('call-1', 'write_file', {
15791582
path: 'src/new-file.ts',
15801583
content: 'export const hello = "world"',
15811584
}),
15821585
createToolResultMessage('call-1', 'write_file', {
1583-
diff: '--- /dev/null\n+++ b/src/new-file.ts\n@@ -0,0 +1 @@\n+export const hello = "world"',
1586+
file: 'src/new-file.ts',
1587+
message: 'Created file',
1588+
unifiedDiff: '--- /dev/null\n+++ b/src/new-file.ts\n@@ -0,0 +1 @@\n+export const hello = "world"',
15841589
}),
15851590
]
15861591

15871592
const results = runHandleSteps(messages)
15881593
const content = results[0].input.messages[0].content[0].text
15891594

1590-
expect(content).toContain('[WRITE RESULT]')
1591-
expect(content).toContain('+export const hello = "world"')
1595+
expect(content).toContain('[EDIT RESULT: write_file]')
1596+
expect(content).toContain('export const hello')
15921597
})
15931598

1594-
test('truncates very long str_replace diffs', () => {
1599+
test('truncates very long str_replace results', () => {
15951600
const longDiff = 'X'.repeat(3000)
15961601
const messages = [
15971602
createMessage('user', 'Make big changes'),
@@ -1600,14 +1605,16 @@ describe('context-pruner str_replace and write_file tool results', () => {
16001605
replacements: [],
16011606
}),
16021607
createToolResultMessage('call-1', 'str_replace', {
1603-
diff: longDiff,
1608+
file: 'src/big-file.ts',
1609+
message: 'Updated file',
1610+
unifiedDiff: longDiff,
16041611
}),
16051612
]
16061613

16071614
const results = runHandleSteps(messages)
16081615
const content = results[0].input.messages[0].content[0].text
16091616

1610-
expect(content).toContain('[EDIT RESULT]')
1617+
expect(content).toContain('[EDIT RESULT: str_replace]')
16111618
expect(content).toContain('...')
16121619
// Should not contain the full diff
16131620
expect(content).not.toContain(longDiff)
@@ -1656,24 +1663,27 @@ describe('context-pruner str_replace and write_file tool results', () => {
16561663
expect(content).toContain('AGENT_0_START_')
16571664
})
16581665

1659-
test('does not include edit result when no diff is present', () => {
1666+
test('includes all result properties even without unifiedDiff', () => {
16601667
const messages = [
16611668
createMessage('user', 'Edit file'),
16621669
createToolCallMessage('call-1', 'str_replace', {
16631670
path: 'src/file.ts',
16641671
replacements: [],
16651672
}),
16661673
createToolResultMessage('call-1', 'str_replace', {
1667-
success: true,
1674+
file: 'src/file.ts',
1675+
errorMessage: 'No match found for old string',
16681676
}),
16691677
]
16701678

16711679
const results = runHandleSteps(messages)
16721680
const content = results[0].input.messages[0].content[0].text
16731681

1674-
// Should have the tool call summary but not the result
1682+
// Should have both the tool call summary and the full result
16751683
expect(content).toContain('Edited file: src/file.ts')
1676-
expect(content).not.toContain('[EDIT RESULT]')
1684+
expect(content).toContain('[EDIT RESULT: str_replace]')
1685+
expect(content).toContain('errorMessage')
1686+
expect(content).toContain('No match found for old string')
16771687
})
16781688
})
16791689

@@ -1910,12 +1920,12 @@ describe('context-pruner dual-budget behavior', () => {
19101920
})
19111921

19121922
test('counts tool result summaries against assistant+tool budget', () => {
1913-
// Use str_replace with a large diff — this produces a summarized [EDIT RESULT] entry
1923+
// Use str_replace with a large result — this produces a summarized [EDIT RESULT] entry
19141924
const largeDiff = 'LARGE_DIFF_CONTENT_' + 'X'.repeat(900)
19151925
const messages = [
19161926
createMessage('user', 'Do something'),
19171927
createToolCallMessage('call-1', 'str_replace', { path: 'big.ts', replacements: [] }),
1918-
createToolResultMessage('call-1', 'str_replace', { diff: largeDiff }),
1928+
createToolResultMessage('call-1', 'str_replace', { file: 'big.ts', message: 'Updated', unifiedDiff: largeDiff }),
19191929
createMessage('user', 'Recent question'),
19201930
createMessage('assistant', 'Recent answer'),
19211931
]
@@ -2179,7 +2189,7 @@ describe('context-pruner dual-budget behavior', () => {
21792189
createMessage('user', longUserMessage),
21802190
assistantWithToolCalls,
21812191
createToolResultMessage('call-1', 'read_files', { content: 'file data' } as JSONValue),
2182-
createToolResultMessage('call-2', 'str_replace', { diff: largeDiff }),
2192+
createToolResultMessage('call-2', 'str_replace', { file: 'src/model.ts', message: 'Updated', unifiedDiff: largeDiff }),
21832193
{
21842194
role: 'tool',
21852195
toolCallId: 'call-3',
@@ -2223,10 +2233,10 @@ describe('context-pruner dual-budget behavior', () => {
22232233
expect(content).toContain('Edited file: src/model.ts')
22242234
expect(content).toContain('Spawned agents:')
22252235

2226-
// === str_replace diff: present but truncated at 2k chars ===
2227-
expect(content).toContain('[EDIT RESULT]')
2236+
// === str_replace result: present but truncated at 2k chars ===
2237+
expect(content).toContain('[EDIT RESULT: str_replace]')
22282238
expect(content).toContain('DIFF_START_MARKER_')
2229-
expect(content).not.toContain('_DIFF_END_MARKER') // Truncated by 2k diff limit
2239+
expect(content).not.toContain('_DIFF_END_MARKER') // Truncated by 2k result limit
22302240

22312241
// === spawn_agents tool entry: truncated by TOOL_ENTRY_LIMIT ===
22322242
expect(content).toContain('AGENT_0_OUTPUT_START_') // First agent's start in 80% prefix
@@ -2272,7 +2282,9 @@ describe('context-pruner dual-budget behavior', () => {
22722282

22732283
// Tool result with a diff
22742284
const toolResult = createToolResultMessage('call-1', 'str_replace', {
2275-
diff: '--- a/src/app.ts\n+++ b/src/app.ts\n@@ -1 +1 @@\n-old\n+SURVIVED_DIFF_CONTENT',
2285+
file: 'src/app.ts',
2286+
message: 'Updated file',
2287+
unifiedDiff: '--- a/src/app.ts\n+++ b/src/app.ts\n@@ -1 +1 @@\n-old\n+SURVIVED_DIFF_CONTENT',
22762288
})
22772289

22782290
const messages: Message[] = [
@@ -2285,10 +2297,10 @@ describe('context-pruner dual-budget behavior', () => {
22852297
]
22862298

22872299
// Tight budgets: enough for new entries but not old summary entries
2288-
// New assistant entries: ~15 + ~30 + ~30 = ~75 assistant tokens
2289-
// Old assistant entries: ~20+ each would push over budget of 80
2300+
// New assistant entries: ~25 (assistant text+tool) + ~56 (edit result JSON) + ~13 (final) = ~94 tokens
2301+
// Old assistant entries: ~20 for OLD_DROPPED_ASSISTANT_2 would push over budget of 100
22902302
const results = runHandleSteps(messages, 250000, 200000, {
2291-
assistantToolBudget: 80,
2303+
assistantToolBudget: 100,
22922304
userBudget: 4200,
22932305
})
22942306

agents/context-pruner.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const definition: AgentDefinition = {
5252
'basher',
5353
'code-reviewer',
5454
'code-reviewer-multi-prompt',
55+
'librarian',
56+
'tmux-cli',
57+
'browser-use',
5558
]
5659

5760
/** Limits for truncating long messages in the summary (estimated tokens) */
@@ -574,22 +577,20 @@ const definition: AgentDefinition = {
574577
}
575578
}
576579

577-
if (toolMessage.toolName === 'str_replace') {
578-
const diff = value.diff as string | undefined
579-
if (diff) {
580-
const truncatedDiff =
581-
diff.length > 2000 ? diff.slice(0, 2000) + '...' : diff
582-
entryParts.push(`[EDIT RESULT]\n${truncatedDiff}`)
583-
}
584-
}
585-
586-
if (toolMessage.toolName === 'write_file') {
587-
const diff = value.diff as string | undefined
588-
if (diff) {
589-
const truncatedDiff =
590-
diff.length > 2000 ? diff.slice(0, 2000) + '...' : diff
591-
entryParts.push(`[WRITE RESULT]\n${truncatedDiff}`)
592-
}
580+
if (
581+
toolMessage.toolName === 'str_replace' ||
582+
toolMessage.toolName === 'propose_str_replace' ||
583+
toolMessage.toolName === 'write_file' ||
584+
toolMessage.toolName === 'propose_write_file'
585+
) {
586+
const resultStr = JSON.stringify(value)
587+
const truncatedResult =
588+
resultStr.length > 2000
589+
? resultStr.slice(0, 2000) + '...'
590+
: resultStr
591+
entryParts.push(
592+
`[EDIT RESULT: ${toolMessage.toolName}]\n${truncatedResult}`,
593+
)
593594
}
594595
}
595596
}

0 commit comments

Comments
 (0)