Skip to content

fix(ssh): stop remote workspace pulls from resurrecting closed tabs - #12339

Closed
dbachko wants to merge 3 commits into
stablyai:mainfrom
dbachko:dbachko/fix-quick-script-tabs
Closed

dbachko wants to merge 3 commits into
stablyai:mainfrom
dbachko:dbachko/fix-quick-script-tabs

Conversation

@dbachko

@dbachko dbachko commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #10342. Related: #2323 (item C2), #9352 (paired-viewer sibling), #11729 / #12264 (complementary main-process lease path — no file overlap).

Summary

On a direct-SSH workspace, closing terminal tabs doesn't stick: closed tabs re-materialize the next time a terminal is opened (e.g. launching a quick command), on reconnect, or on system wake.

Mechanism. Orca mirrors the tab session to the SSH host as a revisioned snapshot (workspace.get/workspace.patch), and every connect/reconnect/wake runs syncAfterConnect, which pulled the snapshot and replaced local tab state whenever revision > 0 — with no check for whether the remote had actually changed since this client last synced (src/renderer/src/hooks/remote-workspace-target-sync.ts). Any close that never reached the remote mirror (target offline, push conflict latch per #2323-C2, or the write-drop below) was resurrected by the next pull. Opening a quick-command terminal triggers the SSH connect, hence "open new terminal → all my closed tabs come back" (#10342).

Compounding it, the session-write subscriber dropped (not deferred) pending changes while a snapshot apply was in flight — a window up to ~30s after each reconnect (createSessionWriteSubscriber + the shouldSchedulePersist gate in App.tsx). A tab closed in that window never persisted to the local session file or the remote mirror at all.

Fixes

  • Reconnect arbitration: track lastSyncedRemoteWorkspaceRevisionByTargetId (recorded on every 'synced' push/pull status; cleared on target removal). In syncAfterConnect, if the remote revision did not advance past it, skip the pull and push local state instead — local offline edits win over an unchanged remote snapshot. A genuinely newer revision (another device wrote) still pulls and merges as before.
  • Echo/ordering guard: ignore unsolicited workspace.changed snapshots at or below the last synced revision, so late or out-of-order notifications can't stomp newer local state.
  • Defer, don't drop: session writes blocked by an in-flight snapshot apply are retried after the debounce interval instead of being discarded, so closes made during the apply window persist once it ends.

Test plan

  • New regression tests (each fails without the fix): reconnect skip-pull + push-local when revision unchanged; pull still happens when revision advanced; unsolicited snapshots at/below last synced revision ignored; blocked session flush deferred and persisted once the gate reopens (remote-workspace-target-sync.test.ts, session-write-subscriber.test.ts, ssh.test.ts)
  • Existing suites green: remote-workspace-target-sync, session-write-subscriber, ssh slice, ssh-target-cleanup, useIpcEvents (96), store-cascades/tabs/tabs-hydration/terminals-hydration (248), direct-ssh recovery/scope, remote-workspace-snapshot-duplicate-tab-repair
  • oxlint, typecheck:web, check:max-lines-ratchet clean
  • Manual: on an SSH workspace, close tabs while disconnected, reconnect via quick command — closed tabs stay closed

Generated with Devin

On direct-SSH targets, every connect/reconnect/wake pulled the remote
workspace snapshot and replaced local tab state whenever its revision
was > 0, even when the remote had not changed since this client last
synced. Tabs closed while the target was offline (or while a push was
suppressed) came back on the next reconnect — e.g. launching a quick
command spawned a terminal, triggered the connect, and re-opened every
previously closed tab.

Three coordinated fixes:
- Track the last fully synced remote revision per target (recorded on
  every 'synced' push/pull status) and, on reconnect, push local state
  instead of pulling when the remote revision did not advance. Local
  offline edits win over an unchanged remote snapshot.
- Ignore unsolicited workspace.changed snapshots at or below the last
  synced revision so late/out-of-order notifications cannot stomp newer
  local state.
- Defer session writes blocked by an in-flight snapshot apply instead of
  dropping them; a tab closed during the apply window now persists to
  the local session file and the remote mirror once the apply ends.

Fixes stablyai#10342

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The SSH slice tracks the last successfully synchronized remote workspace revision per target and removes that data during target cleanup. Remote workspace synchronization uploads local state when no newer remote revision exists and ignores stale unsolicited snapshots. Session persistence retains changes and pending debounce work while scheduling is blocked, then flushes them when scheduling reopens. Tests cover revision tracking, synchronization decisions, cleanup, deferred persistence, and terminal-tab closure during reconnection.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes a clear summary and test plan but omits the required Screenshots, AI Review Report, Security Audit, and Notes sections. Add all missing template sections, including visual-change status, AI review and cross-platform checks, security audit details, and platform-specific notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the SSH fix that prevents closed remote workspace tabs from reappearing.
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.

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.

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b9bf25f9-c68d-4b3e-9dbd-05e6fa0c76a3

📥 Commits

Reviewing files that changed from the base of the PR and between 866bcda and 767d7c2.

📒 Files selected for processing (7)
  • src/renderer/src/hooks/remote-workspace-target-sync.test.ts
  • src/renderer/src/hooks/remote-workspace-target-sync.ts
  • src/renderer/src/lib/session-write-subscriber.test.ts
  • src/renderer/src/lib/session-write-subscriber.ts
  • src/renderer/src/store/slices/ssh-target-cleanup.ts
  • src/renderer/src/store/slices/ssh.test.ts
  • src/renderer/src/store/slices/ssh.ts

Docker-SSH E2E for the stablyai#10342 scenario: commit three tabs to the relay
workspace, latch the push path in 'conflict' (the stablyai#2323 C2 stale-revision
state), close two tabs through the real tab-bar buttons while the target
is disconnected, then reconnect. Without the reconnect arbitration the
stale relay snapshot is pulled and both closed tabs re-materialize in the
tab bar (verified failing on the pre-fix sources); with it the client
uploads local state instead and the relay mirror converges to the
surviving tab.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@dbachko

dbachko commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

E2E verification (A/B against a real Electron app + dockerized SSH host)

Added tests/e2e/ssh-remote-workspace-close-resurrection.spec.ts (docker-SSH harness, ORCA_E2E_SSH_DOCKER=1), which drives the exact user scenario from #10342: commit 3 terminal tabs to the relay workspace snapshot, latch pushes in the conflict state (#2323 C2), close 2 tabs through the real tab-bar close buttons while the target is disconnected, verify the relay-side snapshot file still holds all 3 tabs (staleness precondition), then reconnect.

Without the fix (same spec run with the four fixed source files reverted to their parent-commit state):

post-reconnect sync status: {"phase":"synced","direction":"pull","revision":2, "message":"Workspace synced"}
post-reconnect store tabs: [3 tab ids]   ← both closed tabs resurrected in the tab bar
1 failed

With the fix:

post-reconnect sync status: {"phase":"synced","direction":"push","revision":3, "message":"Workspace uploaded"}
post-reconnect store tabs: [surviving tab id only]
1 passed

The spec pins the arbitration itself (direction: 'push' after reconnect when the remote revision did not advance), asserts the DOM tab bar shows only the surviving tab after a 5s misbehavior window, and confirms the relay mirror converges to the surviving tab so the next reconnect has nothing stale to restore.

Review follow-up (PR stablyai#12339): pushLocalSession only checked preparation-token
currency, so an older syncAfterConnect attempt resolving after a newer one
could report its stale upload result and regress the sync status and
last-synced revision. Gate the upload on arrival currency before and after
the request, matching the pull path's fencing.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

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

🧹 Nitpick comments (1)
src/renderer/src/hooks/remote-workspace-target-sync.test.ts (1)

274-278: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Assert that the stale upload does not regress lastSyncedRemoteWorkspaceRevisionByTargetId.

setRemoteWorkspaceSyncStatus(..., { phase: 'synced', revision: 6 }) also writes state.lastSyncedRemoteWorkspaceRevisionByTargetId[owner.targetId] = 6, so this test can pass while dropping the stored revision from 7 to 6. Add an assertion after await first that the stored revision remains 7.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0735abee-ee19-469c-bfea-5df83c16d0d3

📥 Commits

Reviewing files that changed from the base of the PR and between 2cf46a1 and ae240cf.

📒 Files selected for processing (2)
  • src/renderer/src/hooks/remote-workspace-target-sync.test.ts
  • src/renderer/src/hooks/remote-workspace-target-sync.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/hooks/remote-workspace-target-sync.ts

@nwparker

Copy link
Copy Markdown
Contributor

Closing as superseded by merged #16752 and #16969. #16752 makes an unacknowledged remote terminal close survive reconnect through persisted tombstones and includes Docker SSH coverage; #16969 ensures closes made during snapshot application are persisted instead of dropped. Those are the two failure paths documented when #10342 was closed, and they cover this PR’s resurrection scenario without the older revision-arbitration approach.

@nwparker nwparker closed this Sep 27, 2026
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.

Phantom terminal tabs survive close and are restored from persisted layout state (SSH host)

4 participants