Keep an exited terminal's tab edge consistent with its revision

Merged
TE
2mo ago
+128-1

Summary

cmux ssh <host> --command ... failed on every open with ssh_failed: cmux-tui could not be started: The Cloud connection did not complete after a terminal on a Linux SSH host exited. The workspace showed [ssh:connected], and a forced cmux vm tree <id> --refresh left the graph stale at the same cursor as the host daemon.

Root cause: a terminal exit is one revision. It carries the exited terminal row, then deletes for its tab, pane, and screen. The daemon built that terminal row (terminal_exit_snapshot_in_state) before the detach, so it still had tab_id/tab_ids pointing at the tab deleted later in the same batch. A full snapshot at that revision derives the edge from topology and reports tab_id: null, tab_ids: [].

The app applied the delta. Its typed graph detached the tab, but the raw terminal row kept the stale edge. hasSameRevisionedContent compares raw terminal rows strictly, so every later snapshot at that cursor was an equal-cursor-conflict. refreshCurrentGraph never became current and SSHTuiWorkspaceCoordinator.attach threw. Nothing could recover it until the host moved to a new revision, and opening a workspace needs that refresh first.

Seen on a live host: the only difference between the app's retained snapshot and the daemon's snapshot at the same cursor was the exited terminal's tab_id/tab_ids.

Changes

  • Daemon (persist_terminal_exit): when the same commit detaches the terminal's views, publish the exited row with tab_id: null, tab_ids: []. The delta then matches the snapshot at its revision.
  • App (CloudVMState.hasSameTerminalDocument): a terminal row's tab_id/tab_ids are the reverse edge of the tab rows. They are compared once, on the typed terminal (revisionedTerminals), where deltas already maintain the edge, and not a second time on the raw row. This lets hosts running an older daemon recover without an upgrade. The relation itself still has to match: a snapshot that puts the terminal in a different tab is still a conflict.

Tests

  • CmuxSurfaceCatalogModelTests: new terminalExitDeltaMatchesFullSnapshot applies the exit batch to a revision 22 graph and checks the result against the revision 23 full snapshot, in both directions. It also checks that a different tab relation or a different exit outcome is still rejected. It fails without the fix and passes with it (swift test for the package).
  • cmux-tui-core: hosted_terminal_exit_atomically_detaches_every_projected_view now asserts that the exit delta's terminal row and the public snapshot agree on tab_id/tab_ids.
  • cmux-tui focused run (hosted_terminal_exit) passed on Linux and macOS.
  • The branch also applies cargo fmt to ghostty-vt/src/terminal.rs. That formatting drift came from main (#15163) and made every cmux-tui run fail at the fmt step.

Verification

  • Checked against the running app on a live host: the retained snapshot and the host daemon's snapshot at the same cursor (same generation, revision 23) differed only in the exited terminal's tab_id/tab_ids. The app's typed terminal already had tab_ids: []. The terminal exited about a minute after the last open that succeeded.
  • Not dogfooded end to end on a fleet build. Reproducing it needs a second SSH host reachable from the test Mac, and that could not be set up for this run. The before and after here come from the live-host diff and the model and daemon tests above, not from a fleet app run.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes cmux ssh <host> --command ... failing with The Cloud connection did not complete after a terminal on a Linux SSH host exits. A terminal exit is one revision that deletes the exited terminal's tab, pane, and screen in the same batch; the exited row kept its old tab_id/tab_ids, so every later snapshot at that cursor was an equal-cursor conflict and the graph never became current.

  • Daemon: publish the exited row with tab_id: null, tab_ids: [] when the same commit detaches its views, so the delta matches the full snapshot at its revision.
  • App: compare a terminal row's tab edge once, on the typed graph, and not again on the raw row, so hosts running an older daemon recover without an upgrade.
  • Adds tests that the exit delta matches the full snapshot at the same revision and that different tab relations or exit outcomes are still rejected.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed snapshot comparisons so stale tab associations no longer cause conflicts after a terminal is moved or its tab is deleted.
    • Improved consistency between terminal-exit updates and saved session state: exited terminals no longer appear attached to tabs when they have been detached.
    • Confirmed that changes to a terminal’s tab placement or exit code are still treated as meaningful snapshot differences.