Skip to content

fix(runtime): isolate parallel transfer_task sessions and preserve tr… - #5030

Open
nasagong wants to merge 1 commit into
docker:mainfrom
nasagong:fix/parallel-transfer-task-isolation
Open

nasagong wants to merge 1 commit into
docker:mainfrom
nasagong:fix/parallel-transfer-task-isolation

Conversation

@nasagong

Copy link
Copy Markdown
Contributor

Fixes #4156

Problem

When multiple transfer_task tool calls run concurrently in the same turn:

  • Foreground sub-sessions are not pinned (PinAgent is unset for root parents), so parallel transfers resolve their agent from the shared agentRouter.current and misroute each other.
  • swapCurrentAgent restores from a single entry-time shared snapshot, corrupting currentAgent when parallel transfers overlap, exit out of order, or run alongside a handoff.
  • loop.go logs and records telemetry/OTel attributes from r.currentAgentName() instead of the session's resolved agent.

Fix

  • pkg/runtime/agent_delegation.go: Always pin transfer_task children (PinAgent = true), distinguish foreground/nested transfers from background pinned sessions via r.agents.isActiveTransfer(parent.ID), and emit AgentInfo for the actual restoredAgent.
  • pkg/runtime/agent_router.go & pkg/runtime/routing.go: Track active foreground transfers as a token-indexed stack with transferBase under a single mu (lock-free atomic.Pointer reads unchanged, never held across event emission or hooks), defer Set to transferBase while transfers are active, and update active transfer entries on in-session handoff/force_handoff via updateTransferAgent.
  • pkg/runtime/loop.go: Resolve stream start logs, telemetry, and OTel span attributes via r.sessionAgentName(sess).
  • Tests: Add race-tested coverage for parallel transfer_task isolation, parallel transfer_task + handoff, parallel transfer_task + run_skill fork, and child handoff / force_handoff followed by nested transfer_task.

Verification

  • task format
  • go test -race ./pkg/runtime/...
  • task build

…ansfer stack

Pin foreground transfer_task sub-sessions so parallel delegations cannot misroute each other, track active foreground transfers as a token-indexed stack in agentRouter, update active transfer entries on in-session handoff, and resolve session-scoped agent names for stream logging and telemetry.

Signed-off-by: nasagong <83327025+nasagong@users.noreply.github.com>
@nasagong
nasagong requested a review from a team as a code owner November 24, 2026 14:36
@aheritier aheritier added area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transfer_task is not concurrency-safe: parallel calls race on current-agent pointer, later calls fail with "No agents are configured in this list"

2 participants