Fix retained closed window contexts and PTY respawn

Merged
AU
1d ago
+7598-1045

Closes #8349

Summary

  • Centralize main-window closure in one idempotent, exact-NSWindow transaction shared by AppKit observation, controller delegation, socket close, tab close, and orphan cleanup.
  • Keep windowless recoverable owners in session persistence and quit-safety decisions while excluding them from visible/scriptable routing until their exact native window is live again.
  • Permanently retire closed TabManager, Workspace, Dock, terminal-registry, observer, remote-session, and background-work graphs so retained SwiftUI/AppKit models cannot respawn PTYs.
  • Guard every workspace and surface acquisition path after retirement; roll back an extension-created worktree safely if asynchronous workspace admission loses a window-close race.
  • Preserve exact route identity and O(1) single-route lookup, with route state explicitly owned by each AppDelegate.
  • Merge current origin/main and preserve the current last-terminal cancellation/recovery behavior.

The root cause was split authority: context removal, native-window visibility, terminal registration, and close callbacks could disagree about whether an ordered-out or retained window still owned runtime work. This change makes route ownership explicit and makes successful route removal the authority boundary before irreversible teardown.

Testing

  • git diff --check
  • ./scripts/check-pbxproj.sh
  • ./scripts/lint-pbxproj-test-wiring.sh — passed for 655 test files
  • python3 scripts/check-package-resolved-policy.py
  • python3 scripts/check-workspace-package-groups.py
  • python3 scripts/check-test-determinism.py — 0 findings
  • Behavior coverage includes exact-window close routing, windowless persistence, stale/duplicate native windows, retained-model retirement, terminal-registry cleanup, worktree rollback, and workspace-create idempotency recovery.
  • Regression provenance remains visible in the commit history, including test-first/fix-second pairs and the macOS 15/26 red proof linked in earlier PR history.
  • Localization audit: the final merge/cleanup introduces no new user-facing copy; the reused workspace-create error key already has English and Japanese translations.
  • Per the closeout instruction, no local build, local xcodebuild, or XCUITest was run. Required GitHub checks are the build/test authority for this pushed HEAD.

Demo Video

  • Video URL or attachment: Not recorded for the latest HEAD; build and dogfood are intentionally deferred until explicit user approval.

Review Trigger (Copy/Paste as PR comment)

@codex review
@coderabbitai review
@greptile-apps review
@cubic-dev-ai review

Checklist

  • I tested the change locally (non-build repository guards listed above)
  • I added or updated tests for behavior changes
  • I updated docs/changelog if needed (not needed for this bug fix)
  • I requested bot reviews after my latest commit
  • All code review bot comments are resolved
  • All human review comments are resolved

Summary by CodeRabbit

  • Bug Fixes
    • Improved main-window closing, recovery, and session persistence, including safer handling of closed or temporarily windowless workspaces.
    • Prevented focus, workspace, panel, Dock, and terminal actions after their associated window or workspace has been closed.
    • Improved cleanup of terminal surfaces, remote sessions, panels, notifications, and Git tracking state.
    • Added safer rollback when extension worktree creation fails or changes unexpectedly.
    • Prevented duplicate or invalid workspace creation during shutdown and startup recovery.
  • Tests
    • Expanded coverage for window routing, teardown, workspace recovery, Dock retirement, and worktree rollback.

[!NOTE]
High Risk
Touches core window routing, session persistence, terminal registry lifetime, and irreversible teardown on close—bugs could leak PTYs, drop session state, or close the wrong window.

Overview
Centralizes main-window close in one idempotent path keyed to the exact NSWindow, with committed-close tombstones so SwiftUI/AppKit cannot re-register or focus a window already tearing down.

Recoverable routes now keep sidebar snapshots and optional Dock transfer through transient context replacement; session autosave and persistence use registered plus recoverable owners, while visible routing still requires a live exact window. Route retirement no longer depends on “any terminal surface still registered”—inactive/finalized TabManagers are pruned instead.

Closed owners are permanently finalized: TabManager/Workspace graphs retire via finalizeAllWorkspacesForWindowClose, workspace creation goes through addWorkspaceIfActive (and related guards), and Dock stores honor isRetired on mutations. Terminal surface registry is reworked with weak registration ledgers, dead-entry sweeps, and coalesced retireInactiveRecoverableMainWindowRoutes callbacks; surfaces unregister on explicit teardown before deinit.

Smaller fixes: sidebar Git probe reset consolidation, workspace-group creation when host creation fails, Iroh host lifecycle test clock alignment, and extension worktree rollback when workspace admission fails after async creation.

Reviewed by Cursor Bugbot for commit b7ada499fdcbfca5ca884b2cb0ede72fb182a113. Bugbot is set up for automated code reviews on this repo. Configure here.