Skip to content

fix: don't mint worktreeMeta from a sidebar sort-order snapshot - #9342

Merged
nwparker merged 1 commit into
stablyai:mainfrom
PannenetsF:fix/persist-sort-order-no-orphan-mint
Jul 24, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
PannenetsF:fix/persist-sort-order-no-orphan-mint

Conversation

@PannenetsF

@PannenetsF PannenetsF commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stops a deleted or orphaned workspace from silently reappearing as a duplicate/"unknown" project whenever the sidebar snapshots its sort order.

In plain terms: the app was accidentally re-creating projects you had already deleted, just because it was saving the order they appear in the sidebar. It now only reorders projects that still actually exist.

Root cause & fix: Both worktrees:persistSortOrder (src/main/ipc/worktrees.ts) and persistManagedWorktreeSortOrder (src/main/runtime/orca-runtime.ts) looped over the renderer's orderedIds and called store.setWorktreeMeta(...), which has no repo-existence check — it merges onto defaults and mints a fresh instanceId for any key. A stale id in the client's order (a repo removed elsewhere, or an already-orphaned id) therefore got re-created on every snapshot. The fix guards both call sites with an early continue on ids that have no existing worktreeMeta, so a snapshot can only reorder existing worktrees, never fabricate one.

Trade-offs / regression risk: None expected. Legitimate worktrees always have meta stamped before their order is snapshotted, so no real ordering is dropped. The guard only adds an early-continue on ids that lack existing meta — exactly the ids that previously would have been fabricated. Existing worktrees follow the identical setWorktreeMeta path as before.

Credit to @PannenetsF (Yunqian Fan) for the original fix. Validated and rebased onto latest main during a bug-bash triage on 2026-07-23; original fix design preserved.

Screenshots

No visual change. The fix is entirely in the main process (IPC handler and runtime service); no renderer UI is touched. The observable behavioral effect is indirect: deleted/orphaned projects no longer resurface as duplicate/"unknown" entries in the sidebar after a relaunch.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build
  • Added or updated high-quality tests for the change, or explained why not

Author-reported testing (I cannot run pnpm in this environment, so the unchecked boxes above reflect that the full gates were not confirmed as run):

  • Added a guard regression test: persistSortOrder only reorders existing worktrees and never mints meta for a stale id in src/main/ipc/worktrees.test.ts.
  • Author reports src/main/ipc/worktrees.test.ts passes on-branch (Test Files 1 passed, Tests 200 passed) and that reverting both fix files to origin/main fails the new guard assertion.
  • Author reports oxlint on both source files and the test exits 0 (note: pnpm lint full run not confirmed, so left unchecked).

AI Review Report

I reviewed this diff directly. The change is small and well-scoped: an identical early-continue guard added to two loops that call store.setWorktreeMeta(...), plus one added unit test.

  • Correctness: The guard if (!store.getWorktreeMeta(id)) continue correctly prevents minting new meta for ids without existing entries, which is the documented root cause. Both call sites (worktrees.ts and orca-runtime.ts) are guarded consistently.
  • Edge cases: In orca-runtime.ts, the updated counter now only increments for ids that already had meta — this is the intended behavior (it should reflect actual reorders, not attempted ones). One minor consideration: the guard adds a getWorktreeMeta read per id inside the loop; for large orderedIds this is O(n) extra store reads, but sidebar orderings are small and this is negligible. Sort-order semantics (descending timestamps, now - i * 1000) are unchanged.
  • Cross-platform (macOS/Linux/Windows): Confirmed checked. This is pure main-process JavaScript logic with no OS-specific code — no keyboard shortcuts, no shortcut labels, no filesystem path construction, no shell/command execution, and no Electron API surface that differs by platform. The worktree ids are opaque ${repoId}::${path} strings compared by identity, not parsed as paths. No platform-specific behavior was touched.

No correctness or portability issues found beyond the negligible per-loop read noted above.

Security Audit

Reviewed for security-relevant surface:

  • Input handling: orderedIds from the renderer are treated as opaque keys and only used to look up / write store meta; the guard actually tightens input trust by rejecting ids that don't already exist. No new parsing or deserialization.
  • Command execution / shell: None. No child processes or Git commands added.
  • Path handling: None. Ids are compared by identity, not resolved as filesystem paths.
  • Auth / secrets / dependencies: No changes.
  • IPC: The existing worktrees:persistSortOrder IPC handler signature is unchanged; the change only makes it more conservative about what it persists.

No security concerns identified.

Notes

  • No platform-specific behavior; safe across macOS, Linux, and Windows, and unaffected by the SSH use case (main-process store logic only).
  • Follow-up (optional, out of scope): the underlying setWorktreeMeta still has no repo-existence check of its own — this PR guards the two known callers rather than hardening the store itself, so any future caller must apply the same guard.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Sort-order persistence now checks for existing worktree metadata before updating sort order in both the IPC handler and runtime snapshot loop. Missing or stale worktree IDs are skipped instead of creating metadata. A unit test verifies that stale IDs do not trigger metadata writes.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue #9025 requires pruning workspace session state on project removal, but this PR only guards sort-order persistence and doesn't implement that cleanup. Implement the removeProject/pruneWorktreeStateForRepo cleanup from #9025, or retarget the linked issue if this PR is intentionally a narrower follow-up.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The diff stays on-target: one behavior fix in two sort-order paths plus a regression test, with no unrelated feature or refactor work.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title is concise and accurately describes the main change: preventing worktreeMeta from being minted during sidebar sort-order snapshots.
Description check ✅ Passed The description matches the template and covers summary, screenshots, testing, AI review, security audit, and notes with the required cross-platform check.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

`worktrees:persistSortOrder` (ipc/worktrees.ts) and `persistManagedWorktreeSortOrder`
(orca-runtime.ts) call `store.setWorktreeMeta(id, { sortOrder })` for every id the
renderer sends. `setWorktreeMeta` has no repo-existence check, so a stale id the
client still lists — e.g. a removed repo's `${repoId}::${path}` that lingers in the
renderer's order — gets a brand-new `worktreeMeta` entry minted on every sidebar
snapshot, resurrecting an orphan/duplicate workspace on the next launch.

A sort-order snapshot must only reorder worktrees that already exist, never create
one. Guard both call sites with `getWorktreeMeta` so absent ids are skipped.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@nwparker
nwparker force-pushed the fix/persist-sort-order-no-orphan-mint branch from e7d50ce to cd64c4c Compare July 23, 2026 11:48
@nwparker
nwparker merged commit d8499fa into stablyai:main Jul 24, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Thanks so much for this, @PannenetsF — don't mint worktreeMeta from a sidebar sort-order snapshot. Verified (fail-on-main/pass-on-branch), rebased on latest main, reviewed, and merged. Really appreciate it! 🙏

bbingz added a commit to bbingz/orca that referenced this pull request Jul 24, 2026
persistWorktreeSortOrder skips unchanged ranks and never mints worktreeMeta
for unknown ids (stablyai#9342).
bbingz added a commit to bbingz/orca that referenced this pull request Aug 5, 2026
persistWorktreeSortOrder skips unchanged ranks and never mints worktreeMeta
for unknown ids (stablyai#9342).
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…lyai#9342)

`worktrees:persistSortOrder` (ipc/worktrees.ts) and `persistManagedWorktreeSortOrder`
(orca-runtime.ts) call `store.setWorktreeMeta(id, { sortOrder })` for every id the
renderer sends. `setWorktreeMeta` has no repo-existence check, so a stale id the
client still lists — e.g. a removed repo's `${repoId}::${path}` that lingers in the
renderer's order — gets a brand-new `worktreeMeta` entry minted on every sidebar
snapshot, resurrecting an orphan/duplicate workspace on the next launch.

A sort-order snapshot must only reorder worktrees that already exist, never create
one. Guard both call sites with `getWorktreeMeta` so absent ids are skipped.

Co-authored-by: fanyunqian.1 <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[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.

4 participants