Skip to content

feat: let users choose JSON or SQLite when profile copies diverge - #23278

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/np-json-recover-cmd
Sep 27, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/np-json-recover-cmd

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​288 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​284
Prod 11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​298 $\color{#cf222e}{\Huge{\mathbf{−}}}$​41 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​257

ELI5

Sometimes a profile ends up with two saved copies that disagree — usually after opening it in an older version of Orca. Orca used to stop and show a wall of text plus a "copy recovery command" button, so the user had to go run a CLI command. Now it just asks: Use SQLite (Recommended) or Use JSON, and fixes it for you.

What Changed

Before: startup failed with "Orca cannot safely choose a profile-state authority…", listing several orca profile state … commands and a "Copy recovery command" button (which actually copied orca profile state exports, not the fix).

After: for this specific case (both copies readable, JSON changed without SQLite accepting it), Orca shows a plain dialog explaining the two copies, with each copy's last-saved time, and three buttons: Use SQLite (Recommended), Use JSON, Quit. Picking one restarts Orca, applies the choice, and opens normally. If applying fails, Orca shows the reason and quits without starting.

Mechanism:

  • The bootstrap error for this case is now tagged (divergence: 'diverged-json') so only it gets the choice dialog; every other ambiguous/unreadable-state error keeps the existing copy-command dialog.
  • A choice relaunches Orca with a new --profile-state-desktop-recovery flag. That runs the existing recovery preflight (which takes the profile maintenance lock and the single-instance lock), then app.relaunch()es with the original arguments.
  • Use JSON reuses the existing current-json recovery.
  • Use SQLite is a new current-sqlite recovery: validate SQLite is readable, archive the database and the diverged JSON into a quarantine folder, then rewrite orca-data.json from SQLite via the normal compatibility export (which also updates the acceptance marker). If the rewrite fails, the original JSON is put back.
  • CLI parity: orca profile state rollback --current-sqlite.

Why

The user has to make this decision anyway; making them copy a command, quit, find a terminal and run it is needless friction. Recovery runs in a fresh relaunched process rather than in the failed one because the failed startup may still hold profile locks and partially-initialized state; the preflight path is already the audited, lock-owning recovery route the CLI uses.

Linked Issue

N/A (maintainer change)

Visual Proof

N/A — native Electron message box; not yet captured. Dialog content is asserted in profile-state-startup-recovery-dialog.test.ts.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

New/updated tests: profile-state-current-sqlite-command.test.ts (keep SQLite, unparseable JSON, unreadable SQLite leaves both untouched, JSON restored on export failure, admission refusal, error tagging), desktop relaunch/failure/malformed cases in profile-state-recovery-preflight.test.ts, choice-dialog tests, CLI --current-sqlite handler/spec tests. pnpm tc:node, pnpm tc:cli, oxlint and oxfmt pass. Not yet manually exercised end to end on a real diverged profile — the relaunch happening pre-ready is the main thing to eyeball. Three profile-state-backup-rotation.test.ts tests time out locally on a clean main tree as well (pre-existing, unrelated).

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Cross-platform: uses app.relaunch / dialog / path-based helpers only. SSH/remote: local profile startup only; no wire changes (the recovery selector is exchanged only between CLI and app of the same install). Serve/background launches never show the dialog.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Replace the copy-a-command startup dialog for diverged JSON/SQLite profile
state with Use SQLite / Use JSON buttons. The choice relaunches Orca into the
locked recovery preflight, applies it, then starts normally. Adds a
current-sqlite recovery selector (and --current-sqlite CLI flag) that archives
the diverged JSON and republishes it from SQLite.
@OrcaWin
OrcaWin merged commit 47ddfbd into main Sep 27, 2026
9 checks passed

@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.

ℹ️ Minor suggestions only.

Reviewed changes

This is a fresh review of the full 16-file diff at a93066b1bb (the PR's only commit). I traced the adoption path, the relaunch/lock ordering, and the CLI/spec wiring; two independent deep-dives confirmed there is no data-loss or lock-ordering defect in the change.

  • Divergence tagging — bootstrapProfileStateAuthority tags the both-copies/no-marker error with divergence: 'diverged-json', and isDivergedProfileStateFailure routes only that case to the new choice dialog; every other ambiguous/unreadable state keeps the copy-command dialog.
  • Startup choice dialog + desktop relaunch — index.ts shows chooseProfileStateCopy (SQLite/JSON/Quit with save times), then app.relaunch with profileStateDesktopRecoveryArgs and exits. The new --profile-state-desktop-recovery flag applies the choice through the lock-owning preflight and relaunches ordinary startup, or shows an error dialog on failure.
  • current-sqlite recovery — adoptCurrentDatabase validates SQLite, quarantines (copies) the database family and the diverged JSON, removes the JSON, and republishes it from SQLite via the compatibility export (which updates the acceptance marker), restoring the original JSON if the rewrite fails. The SQLite database itself is never removed.
  • CLI/spec parity — --current-sqlite added to the shared selector schema, CLI flag set, handler parseSelector, command spec, and formatRollback; syncHttp1CompatibilityMarker extracted for reuse.

The adoption path is safe against partial failure: the acceptance marker's accepted/pending hashes are always the SQLite export, never the diverged JSON, so a failure can only re-expose the same resolvable divergence or fall back to an intact SQLite-only startup. The three-process desktop flow cannot deadlock on locks — Electron's relauncher starts the replacement only after the current process has exited, so the dead process's participant owner is reclaimable and the single-instance lock is released before the recovery process acquires both. stripRecoveryArgs removes flag and payload, so there is no relaunch loop.

ℹ️ Nitpicks

  • src/main/index.ts:142 uses app.getPath('userData') for the recovery request, while the rest of the persistence/startup stack deliberately uses getCanonicalUserDataPath() to dodge late-resolution differences after app.setName. It resolves to the same root today, but using the canonical accessor here would match the surrounding convention and remove the subtlety.
  • The two new app.relaunch calls bypass relaunchApp(), so these relaunches record no app_relaunch_requested breadcrumb, and the choice path exits with code 1 where the app:relaunch handler uses 0. Passing custom args rules out relaunchApp() as-is, but recording the breadcrumb (and exiting 0 when a relaunch is queued) would keep diagnostics consistent.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

try {
// Validate before archiving so an unreadable database leaves both copies untouched.
authority.readInitialState()
if (authority.revision === 0) {

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.

This guard correctly refuses an empty SQLite, but the dialog recommends exactly this action: when both copies exist and SQLite is at revision 0 (no acceptance marker, so readAcceptedState returns undefined), the new divergence dialog shows and its default button "Use SQLite (Recommended)" guarantees this throw, bouncing the user through a relaunch plus error dialog before they can pick JSON.

Technical details
# Empty SQLite makes the recommended dialog choice a dead end

## Affected sites
- `src/main/persistence/profile-state/profile-state-recovery-command.ts:136-141` — throws on `authority.revision === 0`.
- `src/main/persistence/profile-state/profile-state-documents.ts:170-185` — `readAcceptedSnapshot` returns `undefined` whenever the marker is missing, so an empty (revision-0) database alongside any JSON classifies as `both` and reaches the divergence dialog.
- `src/main/persistence/profile-state/profile-state-startup-recovery-dialog.ts:81-88` — `defaultId: 0` presents "Use SQLite (Recommended)".

## Reachability
A user who launches this build once (creating and leaving an empty `profile-state.db`) and then opens the profile in an older build (which writes `orca-data.json`) lands in exactly this state: classification `both`, no acceptance marker, SQLite empty. Clicking the recommended button cannot succeed; the user must reopen Orca and choose JSON.

## Required outcome
Choosing "Use SQLite" on an empty database should not be a guaranteed failure. Either surface (or preselect) JSON when SQLite has no revision, or have the choice path treat an empty SQLite as the JSON selection so the user is not sent through a failed relaunch first.

## Open questions for the human
- Is an empty database alongside JSON genuinely expected `diverged-json` input, or should the bootstrap classify it separately so the dialog never offers SQLite?

OmarHung pushed a commit to OmarHung/orca that referenced this pull request Sep 27, 2026
…ablyai#23278)

Replace the copy-a-command startup dialog for diverged JSON/SQLite profile
state with Use SQLite / Use JSON buttons. The choice relaunches Orca into the
locked recovery preflight, applies it, then starts normally. Adds a
current-sqlite recovery selector (and --current-sqlite CLI flag) that archives
the diverged JSON and republishes it from SQLite.

Co-authored-by: m4air <[email protected]>
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