Skip to content

Commit 50e4ee9

Browse files
committed
context-pruner: Include tool results of most spawned agents, except blacklisted agents
1 parent a94e4f2 commit 50e4ee9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

agents/context-pruner.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ const definition: AgentDefinition = {
3232
// Target: summarized messages should be at most 10% of max context
3333
const TARGET_SUMMARY_FACTOR = 0.1
3434

35+
// Blacklist of agent IDs whose output should be excluded from spawn_agents results
36+
const SPAWN_AGENTS_OUTPUT_BLACKLIST = [
37+
'file-picker',
38+
'code-searcher',
39+
'directory-lister',
40+
'glob-matcher',
41+
'researcher-web',
42+
'researcher-docs',
43+
'code-reviewer',
44+
'code-reviewer-multi-prompt',
45+
]
46+
3547
// Limits for truncating long messages (chars)
3648
const USER_MESSAGE_LIMIT = 15000
3749
const ASSISTANT_MESSAGE_LIMIT = 4000
@@ -514,6 +526,57 @@ const definition: AgentDefinition = {
514526
}
515527
}
516528
}
529+
530+
// Capture spawn_agents results (excluding blacklisted agents)
531+
// The tool result value is an array of agent results at the top level
532+
if (
533+
toolMessage.toolName === 'spawn_agents' &&
534+
Array.isArray(toolMessage.content)
535+
) {
536+
for (const part of toolMessage.content) {
537+
if (part.type === 'json' && Array.isArray(part.value)) {
538+
const agentResults = part.value as Array<{
539+
agentName?: string
540+
agentType?: string
541+
value?: {
542+
type?: string
543+
value?: unknown
544+
}
545+
}>
546+
const includedResults = agentResults.filter(
547+
(r) =>
548+
r.agentType &&
549+
!SPAWN_AGENTS_OUTPUT_BLACKLIST.includes(r.agentType),
550+
)
551+
if (includedResults.length > 0) {
552+
const resultSummaries = includedResults.map((r) => {
553+
let outputStr = ''
554+
// Extract the actual output from value.value (e.g., lastMessage content)
555+
if (r.value?.value !== undefined && r.value?.value !== null) {
556+
if (typeof r.value.value === 'string') {
557+
outputStr = r.value.value
558+
} else {
559+
outputStr = JSON.stringify(r.value.value)
560+
}
561+
// Remove <think> tags and their contents to save context tokens
562+
outputStr = outputStr
563+
.replace(/<think>[\s\S]*?<\/think>/g, '')
564+
.trim()
565+
// Truncate long outputs to ASSISTANT_MESSAGE_LIMIT chars
566+
if (outputStr.length > ASSISTANT_MESSAGE_LIMIT) {
567+
outputStr =
568+
outputStr.slice(0, ASSISTANT_MESSAGE_LIMIT) + '...'
569+
}
570+
}
571+
return `- ${r.agentType}: ${outputStr || '(no output)'}`
572+
})
573+
summaryParts.push(
574+
`[AGENT RESULTS]\n${resultSummaries.join('\n')}`,
575+
)
576+
}
577+
}
578+
}
579+
}
517580
}
518581
}
519582

0 commit comments

Comments
 (0)