@@ -4,7 +4,7 @@ import { generateCompactId } from '@codebuff/common/util/string'
44
55import { loopAgentSteps } from '../../../run-agent-step'
66import { getAgentTemplate } from '../../../templates/agent-registry'
7- import { filterUnfinishedToolCalls } from '../../../util/messages'
7+ import { filterUnfinishedToolCalls , withSystemTags } from '../../../util/messages'
88
99import type { AgentTemplate } from '@codebuff/common/types/agent-template'
1010import 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