Repository navigation
fix(native-chat): stop a settling handoff throwing an unhandled rejection at teardown - #18824
Conversation
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.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Detached-session publish guard —
structured-agent-session-host-handoff.tsresolves the handoff-status fence from the durable record first, falls back to a non-throwingfindSession, and no-ops when neither exists, so a settling flow's final status publish can no longer turnrequireSessioninto anagent_session_ownership_unknownunhandled rejection. - Settle-only flow tracking —
structured-agent-session-handoff-flow-runner.tsswapsvoid task.finally(...)forvoid task.then(forget, forget)so bookkeeping can't resurface a rejection thatdrain'sallSettledwas already absorbing. - Bounded handoff drain at teardown —
structured-agent-session-host.tsadds adrain-handoffsphase before the session map is dropped, bounded to 5s viawithTimeout, 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.waitForpoll 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.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
…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
…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

claude-structured-session-integration.test.tsfails in CI shardtest / 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 unhandledagent_session_ownership_unknown, andENOTEMPTYin teardown. Present on main — it reproduced on a control PR of main plus one no-op line.Cause
vi.waitForwas never polling.requestHandoffenqueues the flow on the session's serialized chain before it returns (schedule: host.serialize,structured-agent-session-host-handoff.ts:57), andhandoffStatusserializes on that same chain — so the first call blocks, andvi.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 clearedhost.sessions. The orphaned flow then failed, and its failure notification threw:publish→host.session()→requireSession→agent_session_ownership_unknown.FlowRunner.trackusedvoid 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 theENOTEMPTY.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
structured-agent-session-host-handoff.ts:99-108publishresolves the fence via the record, falling back to a non-throwingfindSession, 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-36trackis settle-only (then(forget, forget)) rather than.finally, so bookkeeping cannot resurface a rejection.drainalready settles viaallSettled.structured-agent-session-host.ts:270drain-handoffsteardown phase before the session map is dropped.drain()existed but was never wired into production — only tests called it. Bounded at 5s via the existingwithTimeouthelper, because this sits on the app-quit path and a flow wedged inlaunchTuiwould 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-723vi.waitForwrappers 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 withagent_session_ownership_unknown.structured-agent-session-handoff-flow-runner.test.ts— leaked-rejection test; fails without thetrackfix.structured-agent-session-teardown-handoff-drain.test.ts— new file.waits for an in-flight handoff…fails without the drain phase (and reproduces theENOTEMPTY);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/runtime644 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 reconciliationfailed 9/24 under 8x local concurrency both pre- and post-fix (claimStatusstayslive, expectedreleased). Same class — a bounded poll at:526-534racing the adapter-exit recovery chain — but the barrier is the runtime'swaitForRecovery(), which the test has no handle on. Out of scope here; deserves its own fix.