Repository navigation
Conversation
📝 WalkthroughWalkthroughThe change tracks sink-failure recovery promises in Merge Risk: 🟡 Moderate · up to Teardown now waits for journal-recovery completion before releasing a session, preventing writes after journal closure. However, an adapter shutdown that never completes can indefinitely block session teardown, so this should be resolved before merge. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 5bfd2bbd-03b8-4dfd-9a00-0c69f9969869
📒 Files selected for processing (3)
src/main/native-chat/agent-session-wire/structured-agent-session-event-recovery.tssrc/main/native-chat/agent-session-wire/structured-agent-session-host.tssrc/main/native-chat/agent-session-wire/structured-agent-session-surface-lifetime.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 0 remain after this review.
| { name: 'drain-attaches', run: () => this.tasks.drainAttaches() }, | ||
| { name: 'flush-event-sinks', run: () => this.runtimeState.flushAllEventSinks() } | ||
| { name: 'flush-event-sinks', run: () => this.runtimeState.flushAllEventSinks() }, | ||
| { name: 'drain-sink-recoveries', run: () => this.eventRecovery.drainSinkRecoveries() } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Prevent an unbounded host teardown wait.
drainSinkRecoveries() waits for recoverAfterSinkFailure() to settle. That recovery awaits adapter.forceCloseSession or adapter.closeSession. This phase has no timeout. A non-settling adapter operation keeps flushAllStreamedEvents() pending indefinitely during teardown.
Use a bounded, cancellable recovery protocol. Do not only race the promise with a timeout, because a recovery that continues after teardown can again write after its journal closes.
There was a problem hiding this comment.
Important
The new drain-sink-recoveries phase is unbounded on the app-quit path, where the adjacent drain-handoffs phase is deliberately bounded to avoid holding quit open forever. Worth bounding (or explicitly documenting why unbounded is safe) before merge.
Reviewed changes
This run reviewed the sink-failure recovery drain: in-flight recovery tracking in StructuredAgentSessionEventRecovery, a new drain-sink-recoveries teardown phase, and a regression test in structured-agent-session-surface-lifetime.test.ts.
- Track pending recoveries —
recoverAfterSinkFailurerecords each recovery promise inpendingSinkRecoveriesand removes it on settlement. - Drain before closing journals — a new
drain-sink-recoveriesphase runs afterflush-event-sinksand before the teardown's trailing journalclose(), awaiting all pending recoveries. - Regression test — a gate-controlled
store.transitionHandoffspy proves teardown stays blocked while lease persistence is in flight.
⚠️ Unbounded recovery drain on the app-quit path
The new phase awaits Promise.allSettled(this.pendingSinkRecoveries) with no timeout, in direct tension with the pattern the adjacent drain-handoffs phase already rejects. drain-handoffs is bounded to HANDOFF_DRAIN_TIMEOUT_MS (5s) with a comment explaining that a wedged flow on the quit path would otherwise "hold the quit open forever." A sink recovery that hangs — forceCloseSession/closeSession never resolving, or resumeHeldStructuredAgentSession's acquire spawning a provider that never comes up — now blocks flushAllStreamedEvents() (and therefore app quit) indefinitely, and the trailing journal close() never runs.
Technical details
# Bound (or justify) the sink-recovery drain on the app-quit path
## Affected sites
- src/main/native-chat/agent-session-wire/structured-agent-session-event-recovery.ts:73 — `drainSinkRecoveries()` awaits `Promise.allSettled` with no timeout
- src/main/native-chat/agent-session-wire/structured-agent-session-host.ts:271 — new phase has no bound, unlike the adjacent `drain-handoffs` (`HANDOFF_DRAIN_TIMEOUT_MS`)
## Required outcome
- Decide whether a wedged provider recovery may hold app quit open indefinitely, and either bound the drain (e.g. `withTimeout` like `handoffs.drain()`) or document why unbounded is safe here.
## Open questions for the human
- Unlike handoffs, "giving up" on a sink recovery re-introduces the lease-persists-after-journal-close bug this PR fixes (the ENOTEMPTY path). If bounded, what should happen to the not-yet-persisted lease on timeout — retry via `agentSessionJournalCloseRetries`, or accept the orphan with the recovery still tracked for a later drain?ℹ️ Nitpicks
runtimeState.eventSinkFor(SESSION)in the new test (structured-agent-session-surface-lifetime.test.ts:302) is load-bearing — it swaps out the failed cached sink so theflush-event-sinksphase does not reject andawait teardownresolves — but, unlike the sibling test at line 353, it has no comment explaining why it is there. A one-line comment would save the next reader the same trace.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
The new drain-sink-recoveries teardown phase was unbounded, unlike the drain-handoffs phase beside it. Recovery awaits closeSession (Claude has no forceCloseSession, and its close path awaits an in-flight acquire handshake, a transcript read and a durable write) and can re-attach a provider child, none of it with a deadline — so a wedged recovery held the quit open forever. A test proves teardown hangs to timeout without the bound. Also: flushAllEventSinks used Promise.all, which short-circuited on the first failing sink, so with two or more sessions a still-draining sink could raise its recovery after the drain phase had already run — the leak this phase exists to close. It now settles every sink before throwing the first failure. Reuse rather than a second copy: the tracked-promise-drain idiom already existed as trackAttach/drainAttaches, so both now share one PendingOperationDrain, with unit coverage for re-enqueue-during-drain, a rejected entry, and one that never settles. The teardown phase list moved next to its runner because host.ts sat exactly at its 300-line max-lines budget and the bound did not fit inline.
…teardown-sink-recovery # Conflicts: # src/main/native-chat/agent-session-wire/structured-agent-session-host.ts
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
This run reviewed the delta against the prior review (commit a367048): bounding the sink-recovery drain, closing the multi-session flush gap, and the PendingOperationDrain extraction.
- Bounded the recovery drain —
drain-sink-recoveriesnow runs under the same 5swithTimeout(..., undefined)asdrain-handoffs, resolving the prior⚠️ about a wedged recovery holding quit open forever. - Closed the multi-session flush gap —
flushAllEventSinksnow settles every sink viaPromise.allSettledbefore throwing the first rejection, so a slow secondary sink can no longer raise its recovery after the drain phase has already run. - Extracted
PendingOperationDrain— the fire-and-forget tracking + drain idiom is now one class shared bytrackAttach/drainAttachesand the sink recoveries, replacing the duplicated attach set. - Moved the teardown phase list —
structuredAgentSessionTeardownPhases()lives next to the runner that consumes it, keepingstructured-agent-session-host.tsunder itsmax-linesbudget. - Tests — two regression tests (teardown blocks on recovery; teardown gives up on a wedged recovery) plus
PendingOperationDrainunit coverage (re-enqueue during drain, rejected entry, never-settling entry).
The prior eventSinkFor swap) are both addressed. The give-up-on-timeout tradeoff and the still-unbounded drain-attaches phase are documented in the PR body as accepted residual risk and are unchanged in behavior from today.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

ELI5
Orca keeps a written log of everything an agent session does. If a write to that log fails (a disk hiccup, a full disk), Orca quietly starts a background cleanup: it stops the agent's process and records that the session ended. The bug was that quitting Orca did not wait for that cleanup to finish — it closed the log files and forgot the session while the cleanup was still writing to them. The cleanup then wrote into files that were already closed, which surfaced as errors on quit and, in tests, as a session folder that refused to delete. Now quit waits for that cleanup to finish first. And if the cleanup itself gets stuck, quit gives up on it after five seconds instead of hanging forever.
What Changed
StructuredAgentSessionEventRecoverytracks its in-flight sink-failure recoveries and exposesdrainSinkRecoveries().drain-sink-recoveriesteardown phase runs afterflush-event-sinks(the flush is what surfaces the failure that starts recovery) and before journals close. It is bounded by the same 5s deadline asdrain-handoffs.structured-agent-session-host-teardown.tsasstructuredAgentSessionTeardownPhases(), next to the runner that consumes it.structured-agent-session-host.tswas at its 300-linemax-linesbudget, so the bound did not fit inline.PendingOperationDrainclass reused by bothtrackAttach/drainAttachesand the new sink recoveries, rather than a second copy (AGENTS.md "Reuse Before Reimplementing").flushAllEventSinksnow settles every sink before throwing.Promise.allshort-circuited on the first failure, so with two or more open sessions a still-draining sink could raise its recovery after the new drain phase had already run — the exact leak this PR closes.Why
The recovery started as fire-and-forget (
void this.context.serialize(...)) and nothing awaited it. This is structurally the same leak the neighbouringdrain-handoffsphase already documents: a flow left running writes rows into a journal teardown is about to close. The fix is the same shape, in the same place.Bounding it matters: the recovery awaits
adapter.forceCloseSession ?? closeSession— Claude has noforceCloseSession, and itscloseSessionpath awaits an in-flight acquire handshake, a transcript read, and a durable store write, none with a deadline. It can then re-attach and spawn a new provider child. Unbounded, a wedged recovery holds the quit open forever.Linked Issue
Fixes #
Visual Proof
N/A— main-process session-teardown ordering. There is no rendered surface; the observable difference is the absence of post-close journal writes on quit. Per AGENTS.md, local Electron launches are paused for this work, so validation is unit-level and CI.Tradeoffs
Genuine user-facing costs of this change:
drain-handoffsanddrain-sink-recoveriesare bounded independently, so a pathological quit that wedges both can now take ~10s rather than ~5s before journals close.drain-handoffsdocuments.holds.dispose()does not clear the holder set, so a recovery during teardown can still passhasResumeCapableHolderand re-attach. Quit waits for that (bounded). Suppressing the re-attach outright would be the better fix and is deliberately not attempted here.flushAllEventSinksno longer fails fast. With multiple sessions it now waits for every sink barrier before throwing, so a teardown with one failed and one slow sink takes as long as the slow one. It still throws the first failure, so callers see the same error.drainAttachesand the handoff drain now sharePendingOperationDrain, and the teardown phase list moved files. Both are behaviour-preserving, but they touch the quit path, which is hard to exercise outside tests.Not addressed (residual risk, unchanged by this PR):
drain-attachesremains the one unbounded drain on the quit path.Testing
pnpm tc:node— pass.pnpm test src/main/native-chat/agent-session-wire/— 52 files / 327 tests pass after merging currentmain.pnpm test src/main/runtime/— 597 files / 6487 tests pass.mainmerged into the branch to clear an import conflict instructured-agent-session-host.ts; re-verified after the merge.oxlintandoxfmt --checkclean on changed files.The wedged-recovery test was verified to fail (teardown hangs to timeout) when the 5s bound is removed, so it is a real regression test rather than a tautology.
Platforms: logic is platform-independent main-process code; no path, shell, or process-spawn behaviour changed. Structured sessions are local-only today (both adapters gate on
executionHostId === LOCAL_EXECUTION_HOST_ID), so there is no SSH/remote wire surface here.I manually tested these changes locally
Automated tests added/updated, or explained why not below
Three tests added: teardown waits for recovery; teardown gives up on a wedged recovery; and unit coverage for
PendingOperationDrain(re-enqueue during drain, rejected entry, never-settling entry).AI Disclosure
Review
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance
No persisted schema, wire format, or IPC payload changed — this is teardown ordering only, so there is no mixed-version or rollback surface.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)