Skip to content

fix(native-chat): stop a settling handoff throwing an unhandled rejection at teardown - #18824

Merged
nwparker merged 2 commits into
mainfrom
fix/structured-session-handoff-race
Sep 5, 2026
Merged

nwparker merged 2 commits into
mainfrom
fix/structured-session-handoff-race

Conversation

@nwparker

@nwparker nwparker commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​354 $\color{#cf222e}{\Huge{\mathbf{−}}}$​67 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​287
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​29 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​26

claude-structured-session-integration.test.ts fails in CI shard test / tests node 24 3/8 ("completes a scripted native to TUI to native cycle with provider-history rehydration") with three linked symptoms: Timed out in waitFor!, an unhandled agent_session_ownership_unknown, and ENOTEMPTY in teardown. Present on main — it reproduced on a control PR of main plus one no-op line.

Cause

vi.waitFor was never polling. requestHandoff enqueues the flow on the session's serialized chain before it returns (schedule: host.serialize, structured-agent-session-host-handoff.ts:57), and handoffStatus serializes on that same chain — so the first call blocks, and vi.waitFor's 1 s default became a hard deadline on a multi-fsync durable flow that costs ~185 ms locally. A loaded runner exceeds it.

Expiring that deadline abandoned a live flow into afterEach, which cleared host.sessions. The orphaned flow then failed, and its failure notification threw: publish → host.session() → requireSession → agent_session_ownership_unknown. FlowRunner.track used void task.finally(...), which forwards that rejection onto a promise nobody awaits. The un-drained flow kept writing journal rows into the temp dir mid-rm, giving the ENOTEMPTY.

Three of the four defects are production bugs, each reachable in the app — host.close() on last-hold release and app-quit teardown both drop a session while a handoff is in flight.

Fix

Location Change
structured-agent-session-host-handoff.ts:99-108 publish resolves the fence via the record, falling back to a non-throwing findSession, and no-ops when neither exists. A status publish is a notification; a vanished session must not make it throw.
structured-agent-session-handoff-flow-runner.ts:29-36 track is settle-only (then(forget, forget)) rather than .finally, so bookkeeping cannot resurface a rejection. drain already settles via allSettled.
structured-agent-session-host.ts:270 New drain-handoffs teardown phase before the session map is dropped. drain() existed but was never wired into production — only tests called it. Bounded at 5s via the existing withTimeout helper, because this sits on the app-quit path and a flow wedged in launchTui would otherwise hold quit open forever; giving up merely restores the old orphaning, which the publish guard now makes survivable.
claude-structured-session-integration.test.ts:708-723 Both vi.waitFor wrappers removed — not widened, no retry or sleep. The assertions are unchanged; only the redundant poll and its deadline are gone.

Verification

Every test below was verified to fail with its source fix reverted, then restored:

  • structured-agent-session-host-handoff.test.ts — 2 new tests; without the publish fix both fail with agent_session_ownership_unknown.
  • structured-agent-session-handoff-flow-runner.test.ts — leaked-rejection test; fails without the track fix.
  • structured-agent-session-teardown-handoff-drain.test.ts — new file. waits for an in-flight handoff… fails without the drain phase (and reproduces the ENOTEMPTY); gives up on a wedged handoff… hangs to the 30s test timeout without the bound, which is exactly the quit-hang it guards.

Plus: src/main/native-chat/agent-session-wire + src/main/runtime 644 files / 6,732 tests pass; target file 5/5 consecutive clean runs; pnpm tc, oxlint and the changed-code quality gate clean.

Known, not fixed here

A second, unrelated flake in the same file, present on main before and after this change: routes a published Claude first-hand exit through fenced host reconciliation failed 9/24 under 8x local concurrency both pre- and post-fix (claimStatus stays live, expected released). Same class — a bounded poll at :526-534 racing the adapter-exit recovery chain — but the barrier is the runtime's waitForRecovery(), which the test has no handle on. Out of scope here; deserves its own fix.

A structured handoff runs on the session's serialized chain and nothing in
production awaited it. When the client-side deadline for the switch expired
first, teardown dropped the session map out from under a live flow, and the
flow's own failure notification then threw `agent_session_ownership_unknown`
out of a status publish — an unhandled rejection, plus journal rows written
into a directory that was already being removed.

Three fixes, each with a regression test that fails without it:

- The status publish is a notification, not a mutation: it now reads the fence
  without requiring an attached session, so an evicted or torn-down session
  makes it a no-op instead of a throw.
- `track` used `.finally`, which forwards a rejection onto a promise nobody
  awaits. `drain` settles flows through `allSettled`, so the bookkeeping chain
  is now settle-only and cannot resurface one.
- Host teardown drains in-flight handoffs before dropping the session map.
  `drain` existed for exactly this and was never wired up.

The integration test's `vi.waitFor` is dropped rather than widened: the request
enqueues the flow on the session's serialized chain before it returns, so the
status read is already ordered behind it. The poll only added a wall-clock
deadline that a loaded runner missed.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Detached-session publish guard — structured-agent-session-host-handoff.ts resolves the handoff-status fence from the durable record first, falls back to a non-throwing findSession, and no-ops when neither exists, so a settling flow's final status publish can no longer turn requireSession into an agent_session_ownership_unknown unhandled rejection.
  • Settle-only flow tracking — structured-agent-session-handoff-flow-runner.ts swaps void task.finally(...) for void task.then(forget, forget) so bookkeeping can't resurface a rejection that drain's allSettled was already absorbing.
  • Bounded handoff drain at teardown — structured-agent-session-host.ts adds a drain-handoffs phase before the session map is dropped, bounded to 5s via withTimeout, so an in-flight handoff settles (or is abandoned, survivably) before journals close.
  • Test coverage — two new unit tests pin the publish guard, one pins the leaked-rejection fix, a new teardown-drain spec pins the bounded drain (including the ENOTEMPTY repro), and the integration test drops its redundant vi.waitFor poll in favor of a serialization-ordered read.

I verified the load-bearing claims against the source rather than taking the PR narrative on faith: requestHandoff enqueues the flow on the session's serialized chain before returning, and handoffStatus serializes on that same StructuredAgentSessionTaskQueue chain, so the direct await host.handoffStatus(...) is deterministically ordered behind the flow — the removed poll was only a wall-clock deadline. The track change is the ultimate defense: even an abandoned wedged flow can never surface an unhandled rejection, because its .catch absorbs store errors and track swallows the forwarded rejection. The bounded drain's wedge path deliberately restores the old orphaning, which the publish guard makes survivable — a documented and defensible tradeoff.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit 265871c into main Sep 5, 2026
29 checks passed
@nwparker
nwparker deleted the fix/structured-session-handoff-race branch September 5, 2026 10:15
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Sep 5, 2026
…tion at teardown (stablyai#18824)

* fix: stop a handoff flow from outliving the host that owns its session

A structured handoff runs on the session's serialized chain and nothing in
production awaited it. When the client-side deadline for the switch expired
first, teardown dropped the session map out from under a live flow, and the
flow's own failure notification then threw `agent_session_ownership_unknown`
out of a status publish — an unhandled rejection, plus journal rows written
into a directory that was already being removed.

Three fixes, each with a regression test that fails without it:

- The status publish is a notification, not a mutation: it now reads the fence
  without requiring an attached session, so an evicted or torn-down session
  makes it a no-op instead of a throw.
- `track` used `.finally`, which forwards a rejection onto a promise nobody
  awaits. `drain` settles flows through `allSettled`, so the bookkeeping chain
  is now settle-only and cannot resurface one.
- Host teardown drains in-flight handoffs before dropping the session map.
  `drain` existed for exactly this and was never wired up.

The integration test's `vi.waitFor` is dropped rather than widened: the request
enqueues the flow on the session's serialized chain before it returns, so the
status read is already ordered behind it. The poll only added a wall-clock
deadline that a loaded runner missed.

* fix: bound the handoff drain so a wedged flow cannot hold the quit open

Mirror-Of: 265871c
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tion at teardown (stablyai#18824)

* fix: stop a handoff flow from outliving the host that owns its session

A structured handoff runs on the session's serialized chain and nothing in
production awaited it. When the client-side deadline for the switch expired
first, teardown dropped the session map out from under a live flow, and the
flow's own failure notification then threw `agent_session_ownership_unknown`
out of a status publish — an unhandled rejection, plus journal rows written
into a directory that was already being removed.

Three fixes, each with a regression test that fails without it:

- The status publish is a notification, not a mutation: it now reads the fence
  without requiring an attached session, so an evicted or torn-down session
  makes it a no-op instead of a throw.
- `track` used `.finally`, which forwards a rejection onto a promise nobody
  awaits. `drain` settles flows through `allSettled`, so the bookkeeping chain
  is now settle-only and cannot resurface one.
- Host teardown drains in-flight handoffs before dropping the session map.
  `drain` existed for exactly this and was never wired up.

The integration test's `vi.waitFor` is dropped rather than widened: the request
enqueues the flow on the session's serialized chain before it returns, so the
status read is already ordered behind it. The poll only added a wall-clock
deadline that a loaded runner missed.

* fix: bound the handoff drain so a wedged flow cannot hold the quit open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant