-
-
Notifications
You must be signed in to change notification settings - Fork 80.7k
subagent_ended hook does not fire for runs created via api.runtime.subagent.run() #59164
Copy link
Copy link
Closed
Labels
P2Normal backlog priority with limited blast radius.Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.ClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.ClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.ClawSweeper found a high-confidence source-level issue reproduction.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.Session, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.Very strong issue quality with high-confidence source-level or clear reproduction.
Description
Metadata
Metadata
Assignees
Labels
P2Normal backlog priority with limited blast radius.Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.ClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.ClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.ClawSweeper found a high-confidence source-level issue reproduction.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.Session, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.Very strong issue quality with high-confidence source-level or clear reproduction.
Type
Fields
Priority
None yet
Summary
api.on("subagent_ended", handler)never fires for subagent sessions created viaapi.runtime.subagent.run(). The hook only fires for runs created through the internalsessions_spawntool path.Root Cause
Two execution paths diverge:
sessions_spawn(tool)api.runtime.subagent.run()(plugin SDK)registerSubagentRun()sessions-spawn-tool.ts:259)SubagentRunRecordsubagent_endedhooksubagent-registry-lifecycle.ts:148)When a plugin calls
api.runtime.subagent.run(params):server-plugins.ts:328dispatches to the gateway"agent"methodserver-methods/agent.ts:264callsagentCommandFromIngress()directlyregisterSubagentRun()— the run is never tracked in the subagent registrySubagentRunRecordexists, soemitSubagentEndedHookOnce()is never calledapi.on("subagent_ended", handler)never firesImpact
Any plugin that uses
api.runtime.subagent.run()to spawn workers andapi.on("subagent_ended")to handle completion will never receive the completion event. These two APIs are documented as companion features but are not wired together.Workaround
Workers can self-report completion by calling a plugin-registered tool (e.g.
tasks.handoff) before their run ends. The plugin handles completion in the tool'sexecutehandler instead of the hook.Suggested Fix
The gateway's
"agent"handler (server-methods/agent.ts) should callregisterSubagentRun()for runs dispatched via the plugin SDK, so they participate in the normal subagent completion lifecycle and emitsubagent_endedwhen done.Reproduction
subagent_endedhookapi.runtime.subagent.run({ sessionKey: "test:key", message: "hello" })subagent_endedhandler is never calledEnvironment
src/gateway/server-plugins.ts,src/gateway/server-methods/agent.ts,src/agents/subagent-registry.ts