Skip to content

Commit fda79bb

Browse files
committed
Include a system message that an agent was spawned (since the spawn tool call is no longer included)
1 parent ceb37a2 commit fda79bb

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

.agents/context-pruner.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ const definition: AgentDefinition = {
172172
if (lastInstructionsPromptIndex !== -1) {
173173
currentMessages.splice(lastInstructionsPromptIndex, 1)
174174
}
175+
const lastSubagentSpawnIndex = currentMessages.findLastIndex((message) =>
176+
message.tags?.includes('SUBAGENT_SPAWN'),
177+
)
178+
if (lastSubagentSpawnIndex !== -1) {
179+
currentMessages.splice(lastSubagentSpawnIndex, 1)
180+
}
175181

176182
// Initial check - if already under limit, return
177183
const initialTokens = countMessagesTokens(currentMessages)

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-utils.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { generateCompactId } from '@codebuff/common/util/string'
44

55
import { loopAgentSteps } from '../../../run-agent-step'
66
import { getAgentTemplate } from '../../../templates/agent-registry'
7-
import { filterUnfinishedToolCalls } from '../../../util/messages'
7+
import { filterUnfinishedToolCalls, withSystemTags } from '../../../util/messages'
88

99
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
1010
import type { Logger } from '@codebuff/common/types/contracts/logger'
@@ -19,6 +19,7 @@ import type {
1919
AgentTemplateType,
2020
Subgoal,
2121
} from '@codebuff/common/types/session-state'
22+
import { Message } from '@codebuff/common/types/messages/codebuff-message'
2223

2324
/**
2425
* Checks if a parent agent is allowed to spawn a child agent
@@ -166,9 +167,21 @@ export function createAgentState(
166167
// When including message history, filter out any tool calls that don't have
167168
// corresponding tool responses. This prevents the spawned agent from seeing
168169
// unfinished tool calls which throw errors in the Anthropic API.
169-
const messageHistory = agentTemplate.includeMessageHistory
170-
? filterUnfinishedToolCalls(parentAgentState.messageHistory)
171-
: []
170+
let messageHistory: Message[] = []
171+
172+
if (agentTemplate.includeMessageHistory) {
173+
messageHistory = filterUnfinishedToolCalls(parentAgentState.messageHistory)
174+
messageHistory.push({
175+
role: 'user',
176+
content: [
177+
{
178+
type: 'text',
179+
text: withSystemTags(`Subagent ${agentType} has been spawned.`),
180+
},
181+
],
182+
tags: ['SUBAGENT_SPAWN'],
183+
})
184+
}
172185

173186
return {
174187
agentId,

0 commit comments

Comments
 (0)