Skip to content

fix(delegation): route async results to origin session - #59767

Closed
dschnurbusch wants to merge 1 commit into
NousResearch:mainfrom
dschnurbusch:fix/async-delegation-origin-routing
Closed

fix(delegation): route async results to origin session#59767
dschnurbusch wants to merge 1 commit into
NousResearch:mainfrom
dschnurbusch:fix/async-delegation-origin-routing

Conversation

@dschnurbusch

@dschnurbusch dschnurbusch commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix async delegate_task completions in desktop/TUI so results return to the session that commissioned the subagent, even when context compression rotates the durable session id while the child is running.

  • carry a live HERMES_UI_SESSION_ID / origin_ui_session_id return address with async delegation completion events
  • for TUI dispatch, capture the live parent_agent.session_id instead of only the approval/session-context key
  • make the desktop/TUI completion poller prefer the commissioning UI session and resolve compression continuation chains before treating an event as orphaned
  • add regression coverage for stale/orphaned keys, compression continuations, finalized origin tabs, and TUI delegate dispatch capture

Why

Desktop/TUI sessions share one process-wide completion queue. If an async delegation completion only carries a durable session_key, and that key goes stale after context compression/session rotation, another session poller can consume the event and inject the delegated result into the wrong conversation.

Related PRs

I found related open PRs while checking for duplicate work: #58690, #57535, and #57586. This PR is a focused variant for the desktop/TUI path that adds a live UI return address plus compression-chain resolution rather than relying only on the durable session key.

Verification

  • live smoke test after restarting the desktop app: dispatched deleg_586ecc5e; result returned to the commissioning session with DELEGATION_ROUTING_SMOKE_OK_20260706_1317
  • independent review subagent passed; follow-up suggestions applied
  • python -m py_compile gateway/session_context.py tools/async_delegation.py tools/delegate_tool.py tui_gateway/server.py
  • python -m pytest tests/tools/test_async_delegation.py::test_delegate_task_background_routes_async_and_does_not_block tests/tools/test_async_delegation.py::test_delegate_task_background_uses_live_tui_agent_session_id tests/tools/test_async_delegation.py::test_delegate_task_background_batch_runs_as_one_unit tests/test_tui_gateway_server.py::test_notification_event_routing_by_session_key tests/test_tui_gateway_server.py::test_async_delegation_event_prefers_origin_ui_session tests/test_tui_gateway_server.py::test_notification_event_follows_compression_continuation tests/test_tui_gateway_server.py::test_finalized_origin_ui_session_falls_back_to_live_continuation tests/gateway/test_session_env.py tests/gateway/test_session_context_inheritance.py -q -o 'addopts=' → 29 passed
  • python -m pytest tests/tools/test_async_delegation.py tests/gateway/test_session_env.py tests/gateway/test_session_context_inheritance.py -q -o 'addopts=' → 43 passed
  • git diff --check
  • GitHub CI: mergeable=MERGEABLE, mergeStateStatus=CLEAN

@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery comp/tui Terminal UI (ui-tui/ + tui_gateway/) 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 P2 Medium — degraded but workaround exists labels Jul 6, 2026
Carry the live TUI session id with async delegation completion events and prefer the commissioning UI session when desktop pollers share the completion queue. Resolve compressed session keys to their continuation before treating events as orphaned, and capture the live parent agent session id for TUI/ACP dispatch.
@teknium1

teknium1 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Merged via PR #60863 — your commit was cherry-picked onto current main with your authorship preserved in git log, plus a follow-up commit adding fail-closed orphan handling (unowned delegation payloads are never adopted by a foreign session) and session-scoped delegation lifecycle (a closing session interrupts its own in-flight subagents). The origin_ui_session_id return-address design and the compression-chain resolution were exactly the right shape — thanks for the thorough cross-referencing against the sibling PRs in your body text.

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

Labels

comp/gateway Gateway runner, session dispatch, delivery comp/tui Terminal UI (ui-tui/ + tui_gateway/) 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.

3 participants