diff --git a/packages/agent-core-v2/src/agent/tools/agent/agentTool.ts b/packages/agent-core-v2/src/agent/tools/agent/agentTool.ts index d294993c70..189980f557 100644 --- a/packages/agent-core-v2/src/agent/tools/agent/agentTool.ts +++ b/packages/agent-core-v2/src/agent/tools/agent/agentTool.ts @@ -344,15 +344,6 @@ export class SubagentTool implements ISubagentTool { }); } - const runInBackground = args.run_in_background === true; - emitAgentRunSpawned(requester, agentId, { - profileName, - parentToolCallId: toolCallId, - description: args.description, - runInBackground, - model: displayModel, - }); - const target = this.lifecycle.findAgentHandle(agentId); if (target === undefined) throw new Error(`Agent "${agentId}" does not exist`); const run = await this.subagents.run( diff --git a/packages/agent-core-v2/test/tool/tool.test.ts b/packages/agent-core-v2/test/tool/tool.test.ts index 9ba5d047bc..babb257794 100644 --- a/packages/agent-core-v2/test/tool/tool.test.ts +++ b/packages/agent-core-v2/test/tool/tool.test.ts @@ -1335,6 +1335,29 @@ describe('Agent tool execution contract', () => { expect(result.output).toContain('child result'); }); + it('emits subagent.spawned exactly once, after task registration, carrying the task id', async () => { + const lifecycle = createAgentLifecycleStub({ + createAgentIds: ['agent-child'], + runCompletion: async () => ({ summary: 'child result' }), + }); + const context = createAgentToolContext(lifecycle); + + await executeAgentTool(context, { + prompt: 'Investigate', + description: 'Find cause', + subagent_type: 'explore', + }); + + const spawned = lifecycle.publishedEvents.filter( + (event) => event.type === 'subagent.spawned', + ); + expect(spawned).toHaveLength(1); + expect(spawned[0]).toMatchObject({ + subagentId: 'agent-child', + taskId: expect.any(String), + }); + }); + it('spawns the subagent on the pool default model when the tool call omits model', async () => { const lifecycle = createAgentLifecycleStub({ createAgentIds: ['agent-child'] }); const context = createAgentToolContext(lifecycle, secondaryModelFlags(), {