Skip to content

[hardening 5/N] refactor(projects): shrink twin-removal teardown to the attributed close actions - #30060

Merged
nwparker merged 1 commit into
mainfrom
nwparker/twin-removal-attribution-shrink
Oct 18, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/twin-removal-attribution-shrink

Conversation

@nwparker

@nwparker nwparker commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​124 $\color{#cf222e}{\Huge{\mathbf{−}}}$​19 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​105
Prod 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​181 $\color{#cf222e}{\Huge{\mathbf{−}}}$​355 $\color{#cf222e}{\Huge{\mathbf{−}}}$​174

Stack (unmerged, review in order): 1. #30030 (base main) → 2. #30035 → 3. #30059 (1a.4) → 4. #30047 → 5. this PR (base nwparker/host-worktree-target-kill-sleep)

ELI5

If the same project is open on two machines (say this computer and an SSH host) at the same folder path, Orca shows two copies that share one id. Removing one copy must close only that copy's tabs and leave the other copy alone. The code that did this had grown to about 575 lines with several special cases. This PR cuts it down and reuses the same "which tab belongs to which host" logic that purge, kill and sleep already use.

What Changed

  • Before: removing one copy ran its own teardown. It trimmed the reopen-closed-tab history, and dropped sleeping-agent records by guessing their host. That guess was wrong in one case: removing the local copy of a local+runtime pair also dropped the runtime copy's sleeping-agent record, because the record named no host.
  • After: removal closes only the terminals, files, browsers and chats that the shared attribution gives to the removed host. It uses the normal close actions, so structured chats, document previews and shells shut down the same way as on any close. In a split tab shared with the other host, the tab stays, but the removed host's shell in it is still stopped, so no process outlives the removal. Anything else the attribution can't place stays as it is: reopen history and sleeping records with no tab. Those are stale entries, and nothing is lost. The runtime copy's sleeping record now stays, so the last KNOWN_TWIN_LEAKS cell (remove project | local+runtime | local) is deleted and the cell now asserts isolation.
  • Mechanism:
    • Twin detection now uses collectHostTwinPurgeScopes, the same check the host-scoped purge uses. It replaces three removal-only helpers (getSurvivingTwinHostIds, retainSurvivingTwinHostIds, splitRemovedWorktreeIds).
    • The pre-call/post-call merge is reduced to one rule. A terminal tab that was attributed before the host call, and that a host frame dropped during the call, is still closed so its agent rows don't linger. This also applies when the other copy was removed at the same time and no twin is left.
    • twinSelectionPatch moves into the shared attribution module, its only other consumer being the purge.
    • The evidence helpers become module-private.
  • Production lines (against the restacked slice 4, which already moved the shared tab-evidence helpers into host-twin-terminal-evidence.ts): the attribution and teardown modules go from 540 (278 + 262) to 368. The shared attribution goes from 278 to 216; purge, kill and sleep depend on it. The removal-only teardown goes from 262 to 152. repo-removal.ts goes from 314 to 312.

Why

The follow-up from the #29954 review asked removal to close only what the attribution names, using the existing close actions, and to drop the special cases that only ever left stale entries. Since then, #30030 and #30047 have reused collectRemovedHostTwinState, removedHostCanonicalizer, twinSelectionPatch and collectHostTwinPurgeScopes. So the shared attribution stays as it is, mixed-tab detection included, because kill and sleep refuse on it and removal uses it to stop the removed host's pane in a mixed tab. Only the removal-specific code shrinks.

Alternative considered: route twin removal entirely through the host-scoped purge (purgeWorktreeTerminalState with host targets). That would save about 100 more lines. I rejected it because the bulk purge skips close-action side effects: it doesn't send agentSession.close to a published structured chat, doesn't revoke a document preview's file grant, and doesn't run the retirement planner's shared-PTY guard. Codex round 1 confirmed all three.

Tests changed on purpose (special cases dropped):

  • project-removal-host-twin-state.test.ts:
    • The reopen-history assertions in the first two tests and the local-removal test now check only that the survivor's entries are kept, not that the removed host's entries are pruned.
    • The SSH test no longer expects a tabless SSH sleeping record to be dropped.
    • The local-removal test still expects the mixed tab's local pane to be killed. It is renamed to say so.
    • "judges a tab that gained a surviving pane" is renamed "keeps a tab that gained a surviving pane during removal and stops its removed pane". It still expects the local pane to be killed.
    • New test: a removed-host tab that a host frame drops during the call is still retired, so its agent status goes.
    • New test: the same holds when the other copy is removed during the call and no twin is left.
  • repo-removal-host-twin-teardown.test.ts (new): launch cancellation stays scoped to each attributed host's ids.
  • host-twin-isolation.test.ts: removed the last KNOWN_TWIN_LEAKS entry.

Fixes from review:

  • The mixed-tab process stop is restored. An earlier revision dropped it, which would have left the removed host's live shell running.
  • Codex: tabs that were attributed before the call and dropped during it are retired even when the other copy was removed at the same time.
  • Codex: pending chat launches are cancelled only under each attributed host's own twin ids, so a host that is one twin's survivor never has its launches cancelled by another twin's iteration.
  • Codex: runtime terminal stops before removal read the current worktree ids again. Ids a listing refresh adds during the registration check are no longer skipped, which matters for older runtimes that rely on the client's stops.

Linked Issue

N/A. This is Stage 1 of the SSH hardening plan (follow-up from the #29954 review).

Visual Proof

N/A. No visual change. This is store teardown only, covered by unit tests.

Testing

  • pnpm tc
  • vitest single-file runs, plus the full src/renderer/src/store, src/renderer/src/lib, components/sidebar and hooks/ipc-events folders: 1549 files, all pass
  • the static-analysis gate script: PASS
  • Codex review: clean, against 53aaf9e7933f after the review fixes

Covered only by unit tests, not live: local, SSH, runtime and folder twins, all from the twin suite.

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

Notes

  • No wire or on-disk change.
  • SSH and runtime twins go through the same attribution as purge, kill and sleep.
  • Folder workspaces are unaffected: removeProject doesn't apply to them, and the folder twin-suite cells stay green.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • N/A visual with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered
  • pnpm typecheck and targeted tests pass locally

@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 73239d0 to 53aaf9e Compare October 17, 2026 11:40
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch from b1f230f to 41344c8 Compare October 17, 2026 11:40
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 436045b to ef28e88 Compare October 17, 2026 12:37
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch 2 times, most recently from a95f7bb to ac1e44f Compare October 17, 2026 14:13
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 651a0f8 to 135c6fa Compare October 17, 2026 14:26
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch from ac1e44f to 8bc289d Compare October 17, 2026 14:26
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 135c6fa to 516f433 Compare October 17, 2026 21:59
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 516f433 to 9cabc62 Compare October 17, 2026 22:01
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch from 8bc289d to 4a4c4d4 Compare October 17, 2026 22:01
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from 9cabc62 to acee149 Compare October 17, 2026 22:52
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch from 4a4c4d4 to 6a952e0 Compare October 17, 2026 22:52
@nwparker
nwparker force-pushed the nwparker/host-worktree-target-kill-sleep branch from acee149 to 75a8c16 Compare October 18, 2026 03:47
@nwparker
nwparker force-pushed the nwparker/twin-removal-attribution-shrink branch from 6a952e0 to 1ba9512 Compare October 18, 2026 04:00
@nwparker
nwparker changed the base branch from nwparker/host-worktree-target-kill-sleep to main October 18, 2026 04:00
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0678fea-605a-467d-879a-9a5f1bae72e1
📥 Commits

Reviewing files that changed from the base of the PR and between 98b4139 and 1ba9512.

📒 Files selected for processing: 7
  • src/renderer/src/store/host-twin-isolation.test.ts
  • src/renderer/src/store/repos/repo-removal-host-twin-attribution.ts
  • src/renderer/src/store/repos/repo-removal-host-twin-teardown.test.ts
  • src/renderer/src/store/repos/repo-removal-host-twin-teardown.ts
  • src/renderer/src/store/repos/repo-removal.ts
  • src/renderer/src/store/slices/project-removal-host-twin-state.test.ts
  • src/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

Project removal now captures host-scoped twin state before the host call and recalculates it after the call. Teardown processes current and pre-call state per host. Selection attribution moved to a separate module. Tests update expectations for retained history and add checks for agent-status and PTY cleanup during removal.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 1ba95

Removing a project that also exists on another host now closes only that host's terminals, files, browsers and chats through the normal close actions. Shared reopen history and some sleeping-agent records are intentionally left in place. No concrete defect was identified. The change is mergeable, though the twin scenarios have only been exercised in unit tests.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check Warning The description covers the main change, rationale, testing, visual proof, scope, and checklist. However, it does not provide the required linked issue, and it omits the Review, AI Disclosure, and Agen… Add a valid issue reference under Linked Issue instead of N/A. Complete the Review, AI Disclosure, and Agent skill upstream boundary sections, or state why each is not applicable. Confirm all required checklist items.
Passed Passed checks: 3 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly identifies the main change: reducing twin-removal teardown to actions attributed to the removed host. It is specific and related to the changeset.
Full details: Description check

Explanation

The description covers the main change, rationale, testing, visual proof, scope, and checklist. However, it does not provide the required linked issue, and it omits the Review, AI Disclosure, and Agent skill upstream boundary sections.

Resolution

Add a valid issue reference under Linked Issue instead of N/A. Complete the Review, AI Disclosure, and Agent skill upstream boundary sections, or state why each is not applicable. Confirm all required checklist items.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@nwparker
nwparker merged commit af056df into main Oct 18, 2026
39 of 59 checks passed
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