[Bug]: Terminal shows "cwd does not exist" after storage cleanup removes a thread's worktree #24154
Before submitting#
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area#
- apps/server
Steps to reproduce#
- Have a thread that runs in a managed worktree.
- Let storage cleanup remove that worktree (Settings > Storage,
storageCleanup.worktreeAfterDays), or delete the folder by hand. - Open the thread and open its terminal.
- Send a message. Turn start sees the folder is missing and recreates the worktree (
ProviderTurnStartService, "provider turn start recreating missing worktree"). - Look at the terminal again.
Expected behavior#
The terminal opens in the worktree. The server already knows how to put a cleaned-up managed worktree back for a turn, so the terminal could do the same instead of failing.
Actual behavior#
terminal.attach fails with [terminal] Terminal cwd does not exist: <worktree path>. assertValidCwd in apps/server/src/terminal/Manager.ts only checks the folder; nothing on the terminal path recreates it. After the turn recreates the worktree, the terminal still shows the error until it is closed and reopened.
From my server trace: terminal.attach failed at 17:14:39, turn start recreated the worktree 11 seconds later, and the terminal drawer kept showing the error.
Impact#
- Minor bug or occasional failure
Version or commit#
main @ 66ee628e08
Environment#
Linux, desktop app, storage cleanup set to 21 days
Workaround#
Send a message in the thread so turn start recreates the worktree, then close and reopen the terminal.
Related#
Confirmed on macOS arm64, desktop nightly
0.1.3-nightly.20261026.3726.After restoring the missing worktree with
git worktree add, closing and reopening the terminal panel still displayed the original “Terminal cwd does not exist” error.At that point, server logs showed successful
terminal.opencalls, and the shell was running in the restored directory. The UI continued displaying the earlier error. View → Reload (⌘R) resolved it.This suggests the failed terminal attachment needs to be retried or invalidated after the directory is restored. The workaround should mention reloading the app if reopening the panel doesn’t recover it.
Logs also showed the original failure on nightly
20261023.3592, before updating.Investigated with GPT-6.1-sol in Codex via T3 Code.
Additional occurrence reproduced on nightly
0.1.7-nightly.20261104.4151(Linux desktop, server trace evidence below).Setup
- Thread
b572e5a5("Review AGENT-PROMPT.md File"), project salamandco-com, brancht3/agent-prompt, recordedworktreePath = /home/imbios/.t3/worktrees/salamandco-com/t3-ce0d9f39, run ordinal 1 completed 2026-11-04T03:21Z withworkspacePreparation: {"type": "worktree", "baseRef": "main", "startFromOrigin": true}. storageCleanup:worktreeAfterDays: 8,worktreeOnMerge: true,worktreeOnDelete: true,worktreeUnchanged: true.- Local branch
t3/agent-promptstill exists; only the worktree folder is gone (git worktree listshows just main + an unrelatedt3-7a6eac81checkout).
Trace timeline (server.trace.ndjson, local time)
- 10:31–10:53Z: normal git ops run with
git.cwd = .../t3-ce0d9f39(worktree alive). - 11:09:15–11:09:18Z:
StorageCleanup.branchChanged,localChanges,resolveCommit,integratedBranch,submodulesspans probe the same path (cleanup eligibility check). - 11:13:38Z:
ws.rpc.terminal.openandws.rpc.terminal.attachboth fail interminal.assertValidCwdwithTerminalCwdNotFoundError: Terminal cwd does not exist: .../t3-ce0d9f39.
So the folder was alive in the morning and gone by the time the terminal was opened — matching this issue's storage-cleanup-removes-worktree root cause. Neither
terminal.opennorterminal.attachrecreates it; only a turn start would (not yet sent in this thread).Notes for #24156
- Recovery precondition holds here: the local branch tip survived, so recreate-from-branch-tip applies.
- The failing cwd is the thread's recorded
worktreePath(no subdir involved), i.e. the exact case that PR covers. - One gap the PR description doesn't mention: after manual
git worktree addrestore, Djis247's comment reports the drawer keeps showing the stale error until app reload — worth covering with an attach retry/invalidation alongside the recreation.
- Thread
🤖 This comment was AI-generated by Claude (Sonnet 5.5, in Claude Code via
t3 triage) after investigating my machine. I reviewed it before posting.Same bug on macOS arm64 (Darwin 27.0), desktop app, server
0.1.8-nightly.20261105.4218. The thread was reopened after an app restart and the terminal showedTerminal cwd does not exist: <worktree path>. The worktree folder had been recreated by the time I looked (git worktree metadata dated after the thread's original creation), and git calls on it succeeded in the server trace.The error also persists after the folder is back, which matches the comment above. Reading
main@337c5858:attachedTerminalSessionFamilyinapps/web/src/state/terminalSessions.tskeys the attach atom on[environmentId, TerminalAttachInput]and turns an attachFailureintostatus: "error".createEnvironmentSubscriptionAtomFamilyinpackages/client-runtime/src/state/runtime.tssetsAtom.setIdleTTL(5 * 60_000), so a failed attach stays cached after the drawer closes.
Observed: Cmd+J, close, Cmd+J again keeps the error. The
+button opens a second terminal fine, because its new terminal id gives a new atom key. I haven't reproduced the 5 minute expiry, so this is a hypothesis from the source.Suggested fix direction: don't cache a failed terminal attach, or invalidate it on retry, in addition to recreating the worktree on
terminal.attach.🤖 This comment was AI-generated by Claude (Opus 5.5, in Claude Code, following the
t3 triageplaybook) after investigating my machine. I reviewed it before posting.Same bug on Windows, on the stable build.
Environment: desktop app + local server
0.1.7, Windows 11 Pro.storageCleanup:worktreeSettledAfterDays: 8,worktreeOnMerge: true,worktreeOnDelete: true,worktreeUnchanged: true.What happened: thread created 2026-10-30 in a managed worktree (
~\.t3\worktrees\<project>\t3-395bbcfb). Its branch never got commits beyondmain. The folder was removed around 2026-11-03 and git no longer listed the worktree. That fits theworktreeUnchangedpath ("no commits beyond the default branch"), but traces from then have rotated. The thread still recorded the oldworktreePath, asstorageCleanup.tsintends. Opening the terminal on 2026-11-07 02:01 UTC failed:terminal.assertValidCwd → TerminalCwdNotFoundError: Terminal cwd does not exist: ~\.t3\worktrees\<project>\t3-395bbcfb at terminal.openLocked → ws.rpc.terminal.openDifference from the other reports: the trigger was
worktreeUnchanged, not age or a merged PR. That means a thread you open just to look around can hit this before you send any message.Workaround confirmed on Windows: sending a message in the thread made turn start log "recreating missing worktree". The worktree came back on the same branch, git lists it again, and the next
terminal.openLockedat 02:18 UTC succeeded.tanstack-source added labelsbug
TonybynMp4(GitHub profile, opens in a new tab) linked this to #24156
juliusmarminge closed this
Sign in to join this conversation.
🤖 This comment was AI-generated. Claude (Opus 5.5, running in Claude Code via
t3 triage) wrote it after investigating my machine, and I reviewed it before posting.Same bug here on a different setup, with two details the original report doesn't have.
Environment
v0.1.3-nightly.20261023.3579(server and desktop app); triage CLI 0.1.2storageCleanup.worktreeOnMerge: true,worktreeSettledAfterDays: 1What's different from the original report
worktreeOnMergeremoved the worktree. I came back to the thread days later, so the time-based setting isn't needed to hit this. Long-lived threads whose PR merged are the usual way in.Unknown terminal threaderrors. Opening the terminal fails once with[terminal] Terminal cwd does not exist: <worktrees>/caelo/t3-703133df. After that, every input or resize on each terminal tab logs[terminal] Unknown terminal thread: <threadId>, terminal: term-4(andterm-5, and so on), becauseopenWithWorkspaceLeasenever created a session. To the user this looks like the terminal is broken. Closing every terminal and opening new ones doesn't help, and nothing says the worktree was removed or that sending a message would bring it back.Trace evidence: four
terminal.assertValidCwdfailures between 17:41:01Z and 17:41:11Z on Oct 23, all for the same missing path.Workaround that doesn't need a turn:
git -C <project> worktree add <thread worktreePath> <thread branch>. Cleanup keeps the local branch, so this puts back the exact path the thread expects. After that, newly opened terminals should work.Suggestion: have
terminal.openeither reuse the turn-start recreation path fromProviderTurnStartServicefor managed worktrees, or show a clear message that the worktree was cleaned up and how to restore it, instead of the raw cwd error.AI-generated by Claude Opus 5.5 (Claude Code,
t3 triage).