Fix Cmd+T opening in home after agent-resume session restore (#6617)

Merged
AU
4mo ago
+195-15

Summary

Fixes #6617 — after Cmd+Q and session restore, Cmd+T opened the new tab in home (~) instead of the directory the resumed terminal/agent is in. This regressed the #6047 fix (whose Cmd+T workspace-cwd fallback is correct, but was being fed a clobbered value).

Root cause

The snapshot does persist the workspace cwd, and restore does replay it onto currentDirectory / panelDirectories. The directory was being lost after restore:

  • A terminal whose startup command cds itself — agent resume, tmux attach, agent-hook — is intentionally spawned with no working directory so it tolerates a since-deleted saved directory (see the localWorkingDirectory == nil branch).
  • Its shell therefore starts in the default directory and shell integration emits a report_pwd (OSC-7) for that directory — typically home — before the resume command cds into the saved one.
  • That spurious live report flows report_pwd → updateSurfaceDirectory → Workspace.updatePanelDirectory(.liveReport) and overwrote the restored panelDirectories[focused] and the focused workspace currentDirectory.
  • Cmd+T's cwd-inheritance fallback (panelDirectories[source] → … → currentDirectory) then resolved to home.

A fresh (non-restored) terminal doesn't hit this: it's spawned in the project directory, so its first report already is the project directory.

The restore path already had a guard (restoredGuardedWorkingDirectoriesByPanelId) meant to ignore exactly these spurious post-resume reports — but it only armed when the saved directory was on an unmounted volume, so a normal (existing) project directory was left unprotected.

Fix

  • Arm the guard for every startup-handles-cwd restore with a non-empty saved directory (not just unmounted volumes).
  • shouldIgnoreRestoredGuardedDirectoryReport now:
    • clears + allows when the report matches the restored directory (shell confirmed it);
    • for an accessible directory, ignores the first mismatched report once and then stops guarding (so the spurious home report is dropped while the user's own later navigation is honored);
    • keeps the existing persistent ignore for unmounted volumes.

This is surgical: only a report that differs from the restored directory is suppressed, and only the first one for accessible directories. savedWorkingDirectory is the panel's last-known directory, so legitimate same-directory reports (e.g. tmux panes) match and clear the guard immediately.

Tests

Two-commit structure (red → green):

  1. test: adds cmdTAfterAgentResumeRestoreKeepsProjectCwdDespiteSpuriousHomePwdReport in AgentSessionAutoResumeSwiftTests (a .serialized session-restore suite). It saves a workspace whose focused terminal runs an auto-resumed Claude agent in a project dir, restores it, simulates the resumed shell's spurious home report_pwd, and asserts currentDirectory survives and a new Cmd+T surface inherits the project directory. Fails without the fix (home clobbers the cwd).
  2. fix: the guard change above.

Localization

No user-facing strings changed (guard logic + a #if DEBUG diagnostic only); no Localizable.xcstrings updates required.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Fixes #6617: after Cmd+Q and session restore, Cmd+T now opens in the project directory instead of home when resuming terminals whose startup command cds itself (agent resume, tmux attach, agent-hook). The restore path now guards the restored cwd for local terminals, ignoring the first spurious home PWD report and accepting it if the saved directory was deleted.

  • Bug Fixes
    • Arm cwd guard for all startup-handles-cwd restores on local terminals; ignore the first mismatched report once when the saved dir still exists, persist ignore for unmounted volumes, accept the report if the saved dir was deleted; remote terminals keep prior behavior.
    • Added regression tests for both cases; updated .github/swift-file-length-budget.tsv.

Summary by CodeRabbit

Release Notes

  • Tests

    • Added regression coverage for auto-resumed agent session restore, verifying the restored working directory stays correct for project vs. home directory cases, including when a saved directory was deleted.
  • Bug Fixes

    • Improved terminal session restore to better preserve the intended working directory. When startup commands can change cwd, the restore now guards against spurious shell-reported directory updates and correctly falls back if the saved directory is no longer available.