@@ -380,26 +380,26 @@ interface ClaudeTaskAgentState {
380380}
381381
382382/**
383- * How many racing snapshot models to buffer per session. A snapshot whose
384- * task_started never arrives would otherwise pin its entry for the session's
383+ * How many entries each task_started lookup map buffers per session. An entry
384+ * whose task_started never arrives would otherwise pin it for the session's
385385 * lifetime; oldest entries evict first.
386386 */
387- const PENDING_TASK_MODEL_CAP = 64 ;
387+ const PENDING_TASK_ENTRY_CAP = 256 ;
388388/** How long Stop waits for Claude to abort a turn before killing the process. */
389389const CLAUDE_INTERRUPT_GRACE = "3 seconds" ;
390390
391391/**
392- * Buffers a subagent snapshot's authoritative model under its
393- * parent_tool_use_id, for snapshots that beat their task_started to the
394- * stream. task_started consumes the entry when it registers the task.
392+ * Buffers a value that a later task_started reads by tool_use_id (a racing
393+ * snapshot model, or a subagent's tool_use owner). task_started consumes the
394+ * entry when it registers the task.
395395 */
396- function rememberPendingTaskModel (
396+ function rememberPendingTaskEntry (
397397 pending : Map < string , string > ,
398- parentToolUseId : string ,
399- model : string ,
398+ toolUseId : string ,
399+ value : string ,
400400) : void {
401- pending . set ( parentToolUseId , model ) ;
402- if ( pending . size > PENDING_TASK_MODEL_CAP ) {
401+ pending . set ( toolUseId , value ) ;
402+ if ( pending . size > PENDING_TASK_ENTRY_CAP ) {
403403 const oldest = pending . keys ( ) . next ( ) ;
404404 if ( ! oldest . done ) {
405405 pending . delete ( oldest . value ) ;
@@ -434,9 +434,16 @@ interface ClaudeSessionContext {
434434 /**
435435 * Authoritative subagent models from assistant snapshots that arrived before
436436 * their task_started registered the task, keyed by parent_tool_use_id.
437- * Written through `rememberPendingTaskModel `, consumed by task_started.
437+ * Written through `rememberPendingTaskEntry `, consumed by task_started.
438438 */
439439 readonly pendingTaskModels : Map < string , string > ;
440+ /**
441+ * tool_use_id → parent_tool_use_id for tool calls made inside a subagent.
442+ * The SDK forwards those calls only as assistant snapshots, never as stream
443+ * events, so task_started reads this to find the owner of a task launched
444+ * from inside a subagent.
445+ */
446+ readonly subagentToolParents : Map < string , string > ;
440447 /**
441448 * Last emitted workflow-member fingerprint per member slot. A coordinator
442449 * task_progress repeats the FULL member array every tick; without a
@@ -3370,18 +3377,45 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
33703377 const owningTaskId = agentIdForParentToolUse ( context . taskAgents , assistantParentToolUseId ) ;
33713378 const snapshotModel = trimmedString ( message . message . model ) ;
33723379 const owningAgent = owningTaskId ? context . taskAgents . get ( owningTaskId ) : undefined ;
3373- if ( snapshotModel ) {
3374- if ( owningAgent ) {
3380+ const snapshotContent = Array . isArray ( message . message . content ) ? message . message . content : [ ] ;
3381+ for ( const block of snapshotContent ) {
3382+ if ( block . type === "tool_use" ) {
3383+ rememberPendingTaskEntry ( context . subagentToolParents , block . id , assistantParentToolUseId ) ;
3384+ }
3385+ }
3386+ if ( snapshotModel && owningAgent ) {
3387+ if ( owningAgent . model !== snapshotModel ) {
33753388 owningAgent . model = snapshotModel ;
3376- } else {
3377- // The snapshot beat its task_started (or its tool_use_id was never
3378- // recorded): hold the model until the task registers.
3379- rememberPendingTaskModel (
3380- context . pendingTaskModels ,
3381- assistantParentToolUseId ,
3382- snapshotModel ,
3383- ) ;
3389+ // Push the correction now. The next task row can be minutes away
3390+ // when the subagent blocks on a long foreground tool.
3391+ const stamp = yield * makeEventStamp ( ) ;
3392+ yield * offerRuntimeEvent ( {
3393+ type : "task.updated" ,
3394+ eventId : stamp . eventId ,
3395+ provider : PROVIDER ,
3396+ createdAt : stamp . createdAt ,
3397+ threadId : context . session . threadId ,
3398+ ...( context . turnState ? { turnId : asCanonicalTurnId ( context . turnState . turnId ) } : { } ) ,
3399+ payload : {
3400+ taskId : RuntimeTaskId . make ( owningAgent . taskId ) ,
3401+ ...taskLinkageFor ( context . taskAgents , owningAgent . taskId ) ,
3402+ } ,
3403+ providerRefs : nativeProviderRefs ( context ) ,
3404+ raw : {
3405+ source : "claude.sdk.message" ,
3406+ method : "claude/assistant" ,
3407+ payload : message ,
3408+ } ,
3409+ } ) ;
33843410 }
3411+ } else if ( snapshotModel ) {
3412+ // The snapshot beat its task_started (or its tool_use_id was never
3413+ // recorded): hold the model until the task registers.
3414+ rememberPendingTaskEntry (
3415+ context . pendingTaskModels ,
3416+ assistantParentToolUseId ,
3417+ snapshotModel ,
3418+ ) ;
33853419 }
33863420 context . lastAssistantUuid = message . uuid ;
33873421 yield * updateResumeCursor ( context ) ;
@@ -3735,15 +3769,23 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
37353769 } ) ;
37363770 return ;
37373771 case "task_started" : {
3738- // A task launched by a tool that itself ran inside a subagent (the
3739- // in-flight tool carries agentId from parent_tool_use_id) is
3740- // agent-internal: a subagent's background shell, not parent work.
3741- const launchingTool = message . tool_use_id
3742- ? Array . from ( context . inFlightTools . values ( ) ) . find (
3743- ( tool ) => tool . itemId === message . tool_use_id ,
3744- )
3772+ // A task launched by a tool that itself ran inside a subagent is
3773+ // agent-internal: a subagent's background shell, not parent work. A
3774+ // streamed tool carries agentId from parent_tool_use_id; a tool seen
3775+ // only in a subagent snapshot resolves through subagentToolParents.
3776+ const toolUseId = message . tool_use_id ;
3777+ const launchingTool = toolUseId
3778+ ? Array . from ( context . inFlightTools . values ( ) ) . find ( ( tool ) => tool . itemId === toolUseId )
37453779 : undefined ;
3746- const owningAgentId = launchingTool ?. agentId ;
3780+ const launchParentToolUseId = toolUseId
3781+ ? context . subagentToolParents . get ( toolUseId )
3782+ : undefined ;
3783+ if ( toolUseId ) {
3784+ context . subagentToolParents . delete ( toolUseId ) ;
3785+ }
3786+ const owningAgentId =
3787+ launchingTool ?. agentId ??
3788+ agentIdForParentToolUse ( context . taskAgents , launchParentToolUseId ) ;
37473789 if (
37483790 context . turnState &&
37493791 classifyTaskAgentKind ( {
@@ -3754,20 +3796,20 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
37543796 context . turnState . hasSubagents = true ;
37553797 }
37563798 // Model/effort: the Agent tool's input carries explicit overrides;
3757- // absent ones inherit the session 's selection (SDK behavior).
3758- // Subagent assistant snapshots refine model with the authoritative API
3759- // id: one that already arrived is buffered and outranks the seed here,
3760- // later ones refine the record in place. AgentInput.effort may be a
3761- // named level or an integer.
3799+ // absent ones inherit the owning subagent 's model, else the session's
3800+ // selection (SDK behavior). Subagent assistant snapshots refine model
3801+ // with the authoritative API id: one that already arrived is buffered
3802+ // and outranks the seed here, later ones refine the record in place.
3803+ // AgentInput.effort may be a named level or an integer.
37623804 const launchInput = launchingTool ?. input ;
3763- const toolUseId = message . tool_use_id ;
37643805 const bufferedModel = toolUseId ? context . pendingTaskModels . get ( toolUseId ) : undefined ;
37653806 if ( toolUseId ) {
37663807 context . pendingTaskModels . delete ( toolUseId ) ;
37673808 }
37683809 const model =
37693810 bufferedModel ??
37703811 trimmedString ( launchInput ?. model ) ??
3812+ ( owningAgentId ? context . taskAgents . get ( owningAgentId ) ?. model : undefined ) ??
37713813 trimmedString ( context . session . model ?? undefined ) ;
37723814 const rawLaunchEffort = launchInput ?. effort ;
37733815 const effort =
@@ -4471,6 +4513,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
44714513 const claudeTasks = new Map < string , ClaudeTaskState > ( ) ;
44724514 const taskAgents = new Map < string , ClaudeTaskAgentState > ( ) ;
44734515 const pendingTaskModels = new Map < string , string > ( ) ;
4516+ const subagentToolParents = new Map < string , string > ( ) ;
44744517 const workflowMemberFingerprints = new Map < string , string > ( ) ;
44754518 const liveTaskIds = new Set < string > ( ) ;
44764519
@@ -5078,6 +5121,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
50785121 claudeTasks,
50795122 taskAgents,
50805123 pendingTaskModels,
5124+ subagentToolParents,
50815125 workflowMemberFingerprints,
50825126 liveTaskIds,
50835127 turnState : undefined ,
0 commit comments