Skip to content

Recover a stalled profile writer without restarting Orca - #25319

Closed
AmethystLiang wants to merge 2 commits into
unable-to-save-profilefrom
unable-to-save-profile-recovery
Closed

AmethystLiang wants to merge 2 commits into
unable-to-save-profilefrom
unable-to-save-profile-recovery

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​346 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​346
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​748 $\color{#cf222e}{\Huge{\mathbf{−}}}$​98 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​650

Stacked on #25318. Review that first; this PR adds recovery after a worker timeout or exit.

ELI5

When Orca's save helper stops responding, saving can recover without restarting Orca. Before retrying a save, the replacement checks whether that exact save already reached the database. If it cannot establish a safe outcome, it stops and reports the failure once.

What Changed

  • Each committing write records a unique operation ID in the same SQLite transaction as its revision. No-op writes record nothing.
  • A supervisor waits for the old worker to exit before opening a replacement. If the interrupted operation committed, it adopts that result without replaying it. If the revision is unchanged, it retries the interrupted command before allowing later commands through.
  • Concurrent callers share one recovery. Idle worker exits also trigger recovery. Recovery is limited to three attempts per ten minutes.
  • Close, quit, profile switching, and maintenance prevent a replacement from taking ownership. Conflicts, protocol errors, failed replacements, and unresolved outcomes remain failures.
  • Diagnostics record recovery outcomes without profile payloads.

Why

A revision increase alone cannot prove whose save committed: another participant may have written to the profile. An operation ID recorded atomically with the revision supplies that proof. Rewriting a complete snapshot could overwrite someone else's changes, and content hashes cannot identify every supported operation.

The parent PR lets queued replies arrive before declaring a timeout, preventing healthy workers from unnecessarily consuming the recovery budget. Its close path also retains ownership until worker exit is confirmed.

Linked Issue

N/A — internal report; no existing linked issue.

Visual Proof

N/A — successful recovery has no UI surface. Refused recovery reuses the existing alert. Validation below exercises the runtime and database; a rendered user-flow demonstration has not been performed.

Testing

  • Real-worker recovery tests cover replay of an uncommitted write, adoption after a lost acknowledgment, foreign writes, shared recovery, idle exits, failed replacement, the retry limit, close during recovery, maintenance, queued edits, durability waiters, and saving a terminal binding afterward.

  • Operation-ID tests cover atomic commit recording, no-op behavior, matching adoption, and foreign-revision rejection.

  • 119 targeted tests across 13 files pass on the rebased branch. The first combined run exposed a timing assumption in the admission test; holding replacement initialization at a shared-memory gate fixes the test, and the recovery suite passes on rerun. The queued-reply test now also asserts one worker and no recovery.

  • pnpm tc:node, changed-code quality, lint/format checks, and git diff --check pass.

  • Inherited real Electron regression: four stalls beyond the save deadline followed by another save, with one worker and no failure alerts. The production 30-second version passed locally on macOS before restacking; the two-second version is included in the rebased validation and Linux/Windows CI.

  • Local execution: macOS. CI typecheck and static analysis pass. The persistence matrix has passed on macOS and Linux (x64 and ARM) and Windows x64; These results precede the final restack over the parent's headless-test-routing fix. Both recovery commits are unchanged by that restack (git range-diff matches); full CI is rerunning on the latest head.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Focus on replay versus adoption, refusing foreign revisions, and close during recovery. An unresolved write remains unresolved at quit or profile switch; closing does not show a failure alert.

Agent skill upstream boundary

  • Not applicable; no upstream skill source is copied.

Notes

One extra metadata upsert per committing write; no additional process on successful saves. Older builds ignore the new metadata key, and a foreign commit cannot match the new worker's operation ID. No schema-version or wire-protocol change. The execution host continues to own persistence for SSH/headless use. This does not establish the cause of the original main-process stall.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Visual-proof scope and limitations explained above
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and compatibility impact considered
  • Full lint, typecheck, tests, and build pass in CI

@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

Initial review of the recovery layer stacked on #25318 — one commit (493ac55e0c) over unable-to-save-profile, 16 files.

  • Durable operation identity: profile_state_meta.last_write_operation records ${connectionToken}:${requestId} in the same SQLite transaction as the revision bump; no-op writes record nothing. Centralized in writeProfileStateRevision / readProfileStateRevisionOperation.
  • Readmission fence: assertProfileStateRevisionOnDisk adopts revision + 1 only when disk carries the interrupted request's id, threaded through initializeFromRevision and the worker entry.
  • Supervisor: ProfileStateWriterSupervisor replaces a timed-out or exited worker after the old thread exits — adopting a proven commit, replaying at the acknowledged revision, and refusing (alerting once) on conflict, unconfirmed exit, start failure, closing, and more than 3 attempts per 10 minutes.
  • Wiring & diagnostics: the connection exposes admitted / faulted / waitForExit and captures the interrupted operation on fault; the worker authority delegates to the supervisor; recovery breadcrumbs are payload-free.
  • Tests: new profile-state-writer-recovery.test.ts (real bundled worker under a fault-injection wrapper) and profile-state-write-operation.test.ts.

I traced the adopt-vs-replay predicate under concurrent writers, no-op writes, stale keys, multiple recoveries per supervisor, reads vs writes, and the committed computation, and traced close / abort / stopAdmission / pauseForMaintenance against an in-flight replace. Both hold: committed === true requires the fence match, the revision is monotonic with a CAS inside BEGIN IMMEDIATE, the per-connection UUID token makes the id collision-free, closing is latch-once, the install is in the same synchronous continuation as the post-ready closing check, and every await is deadline-bounded.

ℹ️ Nitpicks

  • The committed-during-closing branch (profile-state-writer-supervisor.ts:223-228) — where close() lands after a replacement has already adopted the interrupted write — has no test. The fixture's startDelayMs + hangWrite plan covers only the non-committed closing refusal, so this success-without-install path is unpinned.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@AmethystLiang
AmethystLiang force-pushed the unable-to-save-profile-recovery branch from 493ac55 to 109a752 Compare October 5, 2026 04:27

@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

Incremental review of the follow-up commit 109a7520 (Make recovery admission regression deterministic) on top of the recovery layer. The branch was rebased, so the net delta since the prior review is this test-only commit — no production code changed.

  • Deterministic recovery-admission test: added a startGate (SharedArrayBuffer) to the fault-injection fixture so a replacement worker blocks before its entry runs, letting shares one replacement between the interrupted caller and the eager failure handler assert assertWritable() while recovery is provably in flight instead of racing the replacement's initialization.
  • Strengthened queued-reply test: renamed and tightened the deadline test to assert exactly one worker instance and no recovery breadcrumbs, pinning the parent PR's drain-queued-reply-after-deadline behavior.

I traced the gate's Atomics.wait / Atomics.notify pairing across the worker boundary and the try/finally release path: the buffer is shared rather than cloned through workerData, the release runs even when the wait throws, and a worker that starts after release reads a non-zero gate and returns immediately. Ran both modified suites — profile-state-writer-recovery.test.ts (13 tests) and profile-state-write-operation.test.ts (3 tests) — all 16 pass.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

When the profile writer times out or its worker exits, Orca previously
stopped saving until restart. Each committing write now records a unique
operation id in the same SQLite transaction as its revision, so a
replacement writer can prove whether an interrupted write committed.

A supervisor in the worker authority replaces a timed-out or exited worker
only after the old thread has exited, at the last acknowledged revision. It
adopts a commit proven to be ours, replays a command when disk is still at
the acknowledged revision, and refuses conflicts, unconfirmed exits,
replacement start failures, closing, and more than three attempts in ten
minutes, alerting once. Recovery start, success, and refusal leave
sanitized durable breadcrumbs.
@AmethystLiang
AmethystLiang force-pushed the unable-to-save-profile-recovery branch from 109a752 to 4660353 Compare October 5, 2026 04:36
@AmethystLiang

Copy link
Copy Markdown
Contributor Author

Closing in favor of the approach merged in #26447. Slow profile writes now produce a warning and remain pending until the worker replies; elapsed time alone no longer faults or replaces the writer. This removes the timeout failure path that motivated this recovery PR, so we are not proceeding with its timeout-based replacement and replay design.

This PR also adds recovery after a real worker exit, which #26447 does not implement. That remains a separate potential follow-up: if we pursue it, it should be scoped to actual exits and adapted to the current warning-only save path, preserving proof of worker exit and safe commit adoption/replay. Closing this PR does not mean automatic recovery after real exits has shipped.

@Arcanada

Copy link
Copy Markdown

Source-owner clarification for the remaining revision-conflict boundary, following your closure note and #26447.

We verified that #26447 is merged and addresses slow-save warnings, not adoption of an intervening foreign revision. At main 47b94ef64ae62ac62e6d7a93f51a8b1a857f6104, assertProfileStateRevisionOnDisk still refuses when actual revision differs from the admitted revision. We are preserving that refusal; warning-only changes are not being treated as CAS recovery.

Is there an existing successor PR or a supported public, non-destructive readmission/reconciliation entrypoint for a live stale writer? The required boundary is preservation of active terminals and dirty foreign domains, reconciliation of an uncertain prior write before any replay, and continued exact revision fencing. We will not reset an observed revision, rewrite the profile database, patch an installed package, restart active PTYs, or replay historical pending automation runs.

Our source-only automation preflush proposal prevents creation of another pending occurrence after a known persistence conflict, but deliberately does not claim readmission or global writer recovery. If live safe readmission is not supported, an explicit source-owner refusal/locator would let us keep this boundary held rather than infer an installation route from successful CI. No timeout-based worker replacement or replay is requested here.

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.

2 participants