Skip to content

fix: recover fresh local terminal spawns from a deleted saved cwd - #7847

Merged
nwparker merged 1 commit into
mainfrom
nwparker/bug-new-terminal-creation-persistently-fails-wit
Jul 8, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/bug-new-terminal-creation-persistently-fails-wit

Conversation

@nwparker

@nwparker nwparker commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces #7678 (that PR's head branch got detached on GitHub's side after a branch delete/re-create, so it can no longer track pushes; same branch, reworked fix). Two changes on main since #7678 was opened reshaped the fix:

This PR now fixes that residual path:

  • Fresh local renderer spawns opt in via cwdFallback: 'worktree'; when the resolved startup cwd is missing and the workspace root exists, main spawns at the workspace root and returns startupCwdFallback metadata.
  • The affected terminal prints a one-line generic notice (no path, since saved cwds can contain private repo/user names): [Orca opened this terminal at the workspace root because its saved start folder no longer exists.]
  • Existing directories are never remapped — including directories outside the worktree, preserving fix(terminal): allow terminals to spawn outside the worktree (#7685) #7750/[Bug]: Split terminal is not working #7685 behavior.
  • Reattach (sessionId), SSH (connectionId), remote-runtime, runtime/API, and mobile callers never engage the fallback: the renderer only sends the flag for fresh local IPC spawns and main independently re-checks.
  • WSL UNC paths never engage the probe-based fallback (Win32 statSync on \\wsl.localhost 9P shares can falsely report ENOENT); the provider's WSL-aware validation stays authoritative.

Closes #7239.
Supersedes #7297 (its outside-worktree fallback premise was mooted by #7750) and #7184 (same missing-dir bug, but via an unconditional resolver fallback that would also remap reattach/runtime/API cwds).

Repro evidence (red)

On current main, in a live dev build (daemon provider), spawning a fresh local terminal whose saved cwd was deleted:

Error invoking remote method 'pty:spawn': DaemonProtocolError: Working directory
"/private/tmp/orca-cwd-fallback-repo/deleted-folder" does not exist. It may have
been deleted or is on an unmounted volume.

→ red TerminalErrorToast with "file an issue", every retry, no recovery — the same UX class as #7239. The unit test keeps a missing cwd unchanged without the fallback flag pins this error shape.

Live Electron evidence (green)

Verified in a dev build at this branch (isolated profile, throwaway repo, daemon provider):

  1. pty:spawn with cwd=<repo>/deleted-folder + cwdFallback:'worktree' → spawned, response carried startupCwdFallback: { kind: 'worktree', cwd: '/private/tmp/orca-cwd-fallback-repo' }, and pty:getCwd confirmed the live shell at the repo root.
  2. Full UI surface: a real terminal tab created with the deleted startupCwd rendered the notice and an interactive prompt; typing pwd printed /private/tmp/orca-cwd-fallback-repo. No toast.
  3. Strictness: the same spawn without the flag still surfaced the provider error (above), and cwd=/private/tmp (existing, outside worktree) + flag spawned at /private/tmp with no fallback metadata ([Bug]: Split terminal is not working #7685 preserved).

ELI5

Orca sometimes remembers which folder a terminal should start in. If that folder gets deleted, clicking "new terminal" used to fail with a red error — every single time. Now Orca notices the folder is gone, starts the terminal at the workspace root instead, and prints a short note in the terminal saying what happened. Terminals that reconnect to existing sessions, remote/SSH terminals, and API-created terminals are unchanged.

Tradeoffs

  • A deleted saved folder now opens at the workspace root instead of failing. Less precise than the user's original intent, but that folder no longer exists — there is nothing more precise to offer, and the alternative is a permanently bricked tab.
  • The fallback is explicit and fresh-local-only. Reattach keeps a session's exact cwd (silently remapping would detach a restored terminal from its recorded state); SSH/remote hosts can't be probed from local statSync, so they keep exact cwd semantics.
  • The notice omits the missing path (privacy: repo/user names) — the terminal itself shows where it opened.
  • If the workspace root is missing too (unmounted volume, stopped WSL distro), no fallback happens and the provider's normal error surfaces — a fallback there would mislead.

Reliability proof

Gate: terminal-session.startup-cwd-missing-dir-recovery (experimental) — invariant: a fresh local renderer spawn may recover from a missing saved cwd only by spawning at the workspace root with a generic notice; existing directories always spawn as requested; reattach/SSH/remote/runtime/mobile callers keep exact cwd semantics.

Performance risk inventory: touches pty:spawn preflight (adds ≤2 statSync probes for fresh local spawns only — the provider already stats the same paths in validateWorkingDirectory), renderer transport option routing, and one bounded terminal write only when fallback occurs (visibility-gated foreground). No polling, fanout, provider listing, render-loop, startup-await, or subprocess work added.

Correctness evidence (all at this branch):

  • pnpm exec vitest run --config config/vitest.config.ts src/shared/terminal-startup-cwd.test.ts — 21 passed
  • pnpm exec vitest run --config config/vitest.config.ts src/main/ipc/pty.test.ts — 225 passed
  • pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-transport.test.ts — 58 passed
  • pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts — 341 passed
  • pnpm run typecheck — passed
  • pnpm run check:reliability-gates — passed (30 gates)
  • pnpm run check:max-lines-ratchet — passed
  • pnpm exec oxlint <changed files> — clean
  • Live Electron: daemon-provider recovery + strictness runs above

Residual gaps (recorded in the gate): no live daemon-provider run in CI (covered locally above), WSL/mobile/API provider-contract runs, Linux/Windows live behavior.

Note on the second report in #7239

Shoma-DS's follow-up (in-worktree cwd still rejected on 1.4.121) was hit on the pre-#7750 guard, which no longer exists. The orca status / stale_bootstrap runtime-connection instability in that comment is a separate issue and is not addressed here.

Made with Orca 🐋

After #7750 removed the containment guard, the residual #7239 failure mode
is a persisted/inherited startupCwd whose directory no longer exists: every
spawn dies with the provider's missing-directory error. Fresh local renderer
spawns now opt in (cwdFallback: 'worktree') to recover at the workspace root
with a generic in-terminal notice; reattach, SSH, remote-runtime, runtime/API
and mobile callers keep exact cwd semantics, and existing directories —
including outside the worktree (#7685) — spawn as requested.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/bug-new-terminal-creation-persistently-fails-wit branch from 923bede to b7aaffa Compare July 8, 2026 23:20
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 03fb3c79-c180-4450-8bbe-c226d3da6ba5

📥 Commits

Reviewing files that changed from the base of the PR and between 923bedeb1d64c6ec8d5bbcb2bd95572f91945304 and b7aaffa.

📒 Files selected for processing (12)
  • config/reliability-gates.jsonc
  • src/main/ipc/pty.test.ts
  • src/main/ipc/pty.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/pty-transport-types.ts
  • src/renderer/src/components/terminal-pane/pty-transport.test.ts
  • src/renderer/src/components/terminal-pane/pty-transport.ts
  • src/shared/terminal-startup-cwd.test.ts
  • src/shared/terminal-startup-cwd.ts
🚧 Files skipped from review as they are similar to previous changes (12)
  • src/preload/index.ts
  • src/renderer/src/components/terminal-pane/pty-transport-types.ts
  • src/renderer/src/components/terminal-pane/pty-transport.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/shared/terminal-startup-cwd.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/pty-transport.test.ts
  • src/shared/terminal-startup-cwd.test.ts
  • config/reliability-gates.jsonc
  • src/preload/api-types.ts
  • src/main/ipc/pty.test.ts
  • src/main/ipc/pty.ts

📝 Walkthrough

Walkthrough

Adds a fallback mechanism for terminal PTY startup working directories: when a persisted startupCwd directory no longer exists for a fresh local terminal spawn, the system detects the missing directory, falls back to the worktree root, and reports this via a startupCwdFallback field. This spans shared resolution logic, the main-process pty:spawn IPC handler, preload contract types, and renderer transport/connection code, which displays an in-terminal notice when the fallback occurs. A reliability gate configuration entry and tests were added for the shared resolver, main IPC handler, and renderer behavior.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: recovering fresh local terminal spawns when a saved cwd was deleted.
Description check ✅ Passed The description is mostly complete with summary, testing, risks, and notes, but it does not follow the screenshots/checklist template sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/src/components/terminal-pane/pty-transport.ts (1)

683-705: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Forward startupCwdFallback in the coldRestore return (src/renderer/src/components/terminal-pane/pty-transport.ts:683-695)
pty:spawn can include startupCwdFallback on the same response as coldRestore, but this early return drops it. That prevents pty-connection.ts from showing the saved-folder-missing notice and leaves the terminal opening at the workspace root without explanation.

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-transport.test.ts (1)

199-214: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Consider adding a case for coldRestore + startupCwdFallback together.

The current test only covers a plain fresh spawn returning startupCwdFallback. Given the coldRestore-branch concern raised on pty-transport.ts (Lines 683-705), a test mocking spawn to resolve { id, coldRestore: {...}, startupCwdFallback: {...} } would pin down whether the field is expected to survive that branch.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 614ab14b-1a29-47cb-9641-e10248ca5eb6

📥 Commits

Reviewing files that changed from the base of the PR and between 19f1ad4 and 923bedeb1d64c6ec8d5bbcb2bd95572f91945304.

📒 Files selected for processing (12)
  • config/reliability-gates.jsonc
  • src/main/ipc/pty.test.ts
  • src/main/ipc/pty.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/pty-transport-types.ts
  • src/renderer/src/components/terminal-pane/pty-transport.test.ts
  • src/renderer/src/components/terminal-pane/pty-transport.ts
  • src/shared/terminal-startup-cwd.test.ts
  • src/shared/terminal-startup-cwd.ts

@nwparker
nwparker merged commit 4a6d129 into main Jul 8, 2026
2 checks passed
@nwparker
nwparker deleted the nwparker/bug-new-terminal-creation-persistently-fails-wit branch July 8, 2026 23:34
Jinwoo-H added a commit that referenced this pull request Jul 9, 2026
Clean auto-merge, no conflicts. Brought in from main:
- daemon boot hardening: fail builds/CI when terminal daemon can't start;
  plain-node-entry-guard + verify-packaged-daemon-entry (electron require
  leaked into daemon-entry chunk, #7844/#7849)
- perf(windows): relax whole-process-table scans for terminals with no
  agent evidence (#7607) — foreground-scan-cadence
- fix(agents): route WSL OMP hooks to the Windows listener (#7565/#7641)
- fix: recover fresh local terminal spawns from a deleted saved cwd (#7847)
- native-chat: upload composer attachments over SSH (#7832)
- mobile: keyboard dismiss control; agent-tab launch fix
- new GitHub issue draft persistence (#7778)

Perf-branch structure preserved (clean auto-merge; no restructured terminal
files conflicted). Gates: typecheck 0 errors; terminal-pane + ipc/pty +
daemon + pane-manager suites 4497 passed / 14 skipped / 0 failed.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ablyai#7847)

After stablyai#7750 removed the containment guard, the residual stablyai#7239 failure mode
is a persisted/inherited startupCwd whose directory no longer exists: every
spawn dies with the provider's missing-directory error. Fresh local renderer
spawns now opt in (cwdFallback: 'worktree') to recover at the workspace root
with a generic in-terminal notice; reattach, SSH, remote-runtime, runtime/API
and mobile callers keep exact cwd semantics, and existing directories —
including outside the worktree (stablyai#7685) — spawn as requested.

Co-authored-by: Orca <[email protected]>
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.

[Bug]: New terminal creation persistently fails with "Terminal cwd must be inside the selected worktree." toast

1 participant