Skip to content

Commit 2fc41fb

Browse files
committed
Include file edit tool results in context-pruner summary
1 parent d6770e2 commit 2fc41fb

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

agents/__tests__/context-pruner.test.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,123 @@ describe('context-pruner threshold behavior', () => {
14431443
})
14441444
})
14451445

1446+
describe('context-pruner str_replace and write_file tool results', () => {
1447+
let mockAgentState: any
1448+
1449+
beforeEach(() => {
1450+
mockAgentState = {
1451+
messageHistory: [] as Message[],
1452+
contextTokenCount: 0,
1453+
}
1454+
})
1455+
1456+
const runHandleSteps = (messages: Message[]) => {
1457+
mockAgentState.messageHistory = messages
1458+
mockAgentState.contextTokenCount = 250000
1459+
const mockLogger = {
1460+
debug: () => {},
1461+
info: () => {},
1462+
warn: () => {},
1463+
error: () => {},
1464+
}
1465+
const generator = contextPruner.handleSteps!({
1466+
agentState: mockAgentState,
1467+
logger: mockLogger,
1468+
params: { maxContextLength: 200000 },
1469+
})
1470+
const results: any[] = []
1471+
let result = generator.next()
1472+
while (!result.done) {
1473+
if (typeof result.value === 'object') {
1474+
results.push(result.value)
1475+
}
1476+
result = generator.next()
1477+
}
1478+
return results
1479+
}
1480+
1481+
test('includes str_replace diff in summary', () => {
1482+
const messages = [
1483+
createMessage('user', 'Edit this file'),
1484+
createToolCallMessage('call-1', 'str_replace', {
1485+
path: 'src/utils.ts',
1486+
replacements: [{ old: 'foo', new: 'bar' }],
1487+
}),
1488+
createToolResultMessage('call-1', 'str_replace', {
1489+
diff: '--- a/src/utils.ts\n+++ b/src/utils.ts\n@@ -1,1 +1,1 @@\n-foo\n+bar',
1490+
}),
1491+
]
1492+
1493+
const results = runHandleSteps(messages)
1494+
const content = results[0].input.messages[0].content[0].text
1495+
1496+
expect(content).toContain('[EDIT RESULT]')
1497+
expect(content).toContain('-foo')
1498+
expect(content).toContain('+bar')
1499+
})
1500+
1501+
test('includes write_file diff in summary', () => {
1502+
const messages = [
1503+
createMessage('user', 'Create a new file'),
1504+
createToolCallMessage('call-1', 'write_file', {
1505+
path: 'src/new-file.ts',
1506+
content: 'export const hello = "world"',
1507+
}),
1508+
createToolResultMessage('call-1', 'write_file', {
1509+
diff: '--- /dev/null\n+++ b/src/new-file.ts\n@@ -0,0 +1 @@\n+export const hello = "world"',
1510+
}),
1511+
]
1512+
1513+
const results = runHandleSteps(messages)
1514+
const content = results[0].input.messages[0].content[0].text
1515+
1516+
expect(content).toContain('[WRITE RESULT]')
1517+
expect(content).toContain('+export const hello = "world"')
1518+
})
1519+
1520+
test('truncates very long str_replace diffs', () => {
1521+
const longDiff = 'X'.repeat(3000)
1522+
const messages = [
1523+
createMessage('user', 'Make big changes'),
1524+
createToolCallMessage('call-1', 'str_replace', {
1525+
path: 'src/big-file.ts',
1526+
replacements: [],
1527+
}),
1528+
createToolResultMessage('call-1', 'str_replace', {
1529+
diff: longDiff,
1530+
}),
1531+
]
1532+
1533+
const results = runHandleSteps(messages)
1534+
const content = results[0].input.messages[0].content[0].text
1535+
1536+
expect(content).toContain('[EDIT RESULT]')
1537+
expect(content).toContain('...')
1538+
// Should not contain the full diff
1539+
expect(content).not.toContain(longDiff)
1540+
})
1541+
1542+
test('does not include edit result when no diff is present', () => {
1543+
const messages = [
1544+
createMessage('user', 'Edit file'),
1545+
createToolCallMessage('call-1', 'str_replace', {
1546+
path: 'src/file.ts',
1547+
replacements: [],
1548+
}),
1549+
createToolResultMessage('call-1', 'str_replace', {
1550+
success: true,
1551+
}),
1552+
]
1553+
1554+
const results = runHandleSteps(messages)
1555+
const content = results[0].input.messages[0].content[0].text
1556+
1557+
// Should have the tool call summary but not the result
1558+
expect(content).toContain('Edited file: src/file.ts')
1559+
expect(content).not.toContain('[EDIT RESULT]')
1560+
})
1561+
})
1562+
14461563
describe('context-pruner glob and list_directory tools', () => {
14471564
let mockAgentState: any
14481565

agents/context-pruner.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,28 @@ const definition: AgentDefinition = {
803803
}
804804
}
805805
}
806+
807+
// Capture str_replace results (diff of changes made)
808+
if (toolMessage.toolName === 'str_replace') {
809+
const diff = value.diff as string | undefined
810+
if (diff) {
811+
// Truncate long diffs to 2000 chars
812+
const truncatedDiff =
813+
diff.length > 2000 ? diff.slice(0, 2000) + '...' : diff
814+
summaryParts.push(`[EDIT RESULT]\n${truncatedDiff}`)
815+
}
816+
}
817+
818+
// Capture write_file results (diff of changes made)
819+
if (toolMessage.toolName === 'write_file') {
820+
const diff = value.diff as string | undefined
821+
if (diff) {
822+
// Truncate long diffs to 2000 chars
823+
const truncatedDiff =
824+
diff.length > 2000 ? diff.slice(0, 2000) + '...' : diff
825+
summaryParts.push(`[WRITE RESULT]\n${truncatedDiff}`)
826+
}
827+
}
806828
}
807829
}
808830
}

0 commit comments

Comments
 (0)