Skip to content

fix(delegate): pin async completion to spawning parent session (#57498) - #57535

Closed
nankingjing wants to merge 1 commit into
NousResearch:mainfrom
nankingjing:fix/57498-delegate-parent-session-routing
Closed

fix(delegate): pin async completion to spawning parent session (#57498)#57535
nankingjing wants to merge 1 commit into
NousResearch:mainfrom
nankingjing:fix/57498-delegate-parent-session-routing

Conversation

@nankingjing

Copy link
Copy Markdown
Contributor

Summary

delegate_task(background=true) completions could re-enter the wrong gateway session when multiple active rows shared the same routing peer. The async registry only carried session_key; on injection, get_or_create_session() could recover the latest ended_at IS NULL session instead of the session that spawned the subagent.

  • tools/delegate_tool.py: capture parent_agent.session_id at dispatch time
  • tools/async_delegation.py: carry parent_session_id on completion events (single + batch)
  • gateway/run.py: pass gateway_session_id on synthetic completion MessageEvent metadata; pin routing with switch_session() before handling the turn

Fixes #57498

Scope note

Short-term fix from the issue (carry parent session id). Session lifecycle / ended_at leaks and cross-channel session_key reuse are separate follow-ups.

Test plan

  • pytest tests/tools/test_async_delegation.py::test_completion_event_lands_on_shared_queue_with_session_key -q

…esearch#57498)

Background delegate_task completions only carried session_key. When multiple
active sessions shared a routing peer, get_or_create_session could recover the
latest ended_at IS NULL row and inject the subagent result into the wrong
session.

Capture parent_agent.session_id at dispatch time, include it on async-delegation
completion events, and pin gateway routing via switch_session when the
synthetic completion message is handled.

Fixes NousResearch#57498
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery tool/delegate Subagent delegation sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 3, 2026
@teknium1

teknium1 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Merged via PR #60871 — your commit was cherry-picked onto current main with your authorship preserved in git log, plus a follow-up commit completing the class: the pinning now fails closed when the spawning session has ended (switch_session re-opens ended sessions, so blind pinning could resurrect a /new-ended conversation), and /new now interrupts the old conversation's in-flight delegations via a parent_session_id selector. Your parent-session pin was the keystone that made both possible — thanks!

@teknium1 teknium1 closed this Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/delegate Subagent delegation type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

delegate_task background results routed to wrong session (cross-channel contamination)

3 participants