session history orphaned when session ends inside EnterWorktree worktree
Bug
A session started in the main repo checkout that uses the EnterWorktree tool gets its history jsonl re-keyed to a project directory derived from the worktree path. If ExitWorktree is never called, the file stays there permanently. The session remains visible in the extension's conversation list only while the window is open (held in memory) — after a window reload the list is rebuilt from the workspace's project dir on disk, and the session silently disappears from both the VSCode extension list and the --resume picker.
Repro
- Start a session in
/path/to/repo(VSCode extension or CLI) - Ask for work in a separate worktree — agent calls EnterWorktree, cwd switches to
<repo>/.claude/worktrees/<branch> - Session jsonl moves from
~/.claude/projects/-path-to-repo/to~/.claude/projects/-path-to-repo--claude-worktrees-<branch>/ - Session ends without ExitWorktree
- Reload VSCode window → session gone from conversation list. No workspace maps to the worktree-slug dir, so it's unreachable from any list
Expected
Session started in the main checkout should remain resumable from there — either keep history keyed to the origin cwd, or re-key back on session end when ExitWorktree wasn't called.
Workaround
Copy the jsonl back into the origin project dir, or claude --resume <session-id>.
Version: 2.1.220, Linux
Related: #15776 (same class of problem for manually-created worktrees).