File tree Expand file tree Collapse file tree
apps/sim/lib/copilot/request/go Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -396,10 +396,21 @@ export async function runStreamLoop(
396396 if ( isPendingPause ) {
397397 return
398398 }
399- if ( context . subAgentParentStack . length > 0 ) {
400- context . subAgentParentStack . pop ( )
399+ // Pop by id, not by position: parallel subagents can end out of
400+ // order, and a positional pop would otherwise remove the wrong
401+ // parent and leave subAgentParentToolCallId pointing at the
402+ // just-ended lane. Every subagent_end carries a toolCallId in
403+ // current protocol; if one ever doesn't, prefer leaving the stack
404+ // intact over guessing the wrong parent.
405+ if ( toolCallId ) {
406+ const idx = context . subAgentParentStack . lastIndexOf ( toolCallId )
407+ if ( idx >= 0 ) {
408+ context . subAgentParentStack . splice ( idx , 1 )
409+ } else {
410+ logger . warn ( 'subagent end without matching start' , { toolCallId } )
411+ }
401412 } else {
402- logger . warn ( 'subagent end without matching start ' )
413+ logger . warn ( 'subagent end missing toolCallId ' )
403414 }
404415 context . subAgentParentToolCallId =
405416 context . subAgentParentStack . length > 0
You can’t perform that action at this time.
0 commit comments