anthropics / anthropics/claude-agent-sdk-typescript
ExitWorktree claims "no active EnterWorktree session" if worktree created in earlier query()
- Lingua principale
- Shell
- Stelle
- 1.8k
- Fork
- 226
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
When the SDK creates a worktree with the EnterWorktree tool, and then a later query() invocation, even with the same session ID, tries to use ExitWorktree, the exit fails with an error saying "no active EnterWorktree session". I would expect the worktree to persist across query() calls if they have the same session ID.
## Repro steps
1. Call query() with a prompt to use EnterWorktree to create a worktree. Capture the session ID.
2. Call query() again using the session ID to continue the session, with a prompt to use ExitWorktree to exit and remove the tree.
Expected: agent exits and removes the worktree
Actual: ExitWorktree tool errors, saying "there is no active EnterWorktree session to exit."
## Example code
A minimal repo that reproduces the problem is here: https://github.com/jasoncrawford/claude-worktree-repro
When I run that, I get:
```
$ npm run repro
> claude-worktree-repro@1.0.0 repro
> tsx repro.ts
Claude Agent SDK version: 0.2.81
────────────────────────────────────────────────────────────
QUERY: Please invoke the EnterWorktree tool to create a worktree.
SESSION ID IN: (none — fresh session)
────────────────────────────────────────────────────────────
SESSION ID CAPTURED: ed45d9e5-92f7-49c0-ade7-1b8542f9a76a
Model: claude-sonnet-4-6
Tool call: ToolSearch({"query":"select:EnterWorktree"})
Tool result [OK]:
Tool call: EnterWorktree({})
Tool result [OK]: Created worktree at /Users/jason/projects/claude-worktree-repro/.claude/worktrees/glistening-crunching-donut on branch worktree-glistening-crunching-donut. The session is now working in the worktree. Use ExitWorktree to leave mid-session, or exit the session to be prompted.
Claude: Worktree created at `.claude/worktrees/glistening-crunching-donut` on branch `worktree-glistening-crunching-donut`.
Result: success
────────────────────────────────────────────────────────────
QUERY: Now please invoke ExitWorktree to exit, removing the worktree.
SESSION ID IN: ed45d9e5-92f7-49c0-ade7-1b8542f9a76a
────────────────────────────────────────────────────────────
Model: claude-sonnet-4-6
Tool call: ToolSearch({"query":"select:ExitWorktree"})
Tool result [OK]:
Tool call: ExitWorktree({"action":"remove"})
Tool result [ERROR]: No-op: there is no active EnterWorktree session to exit. This tool only operates on worktrees created by EnterWorktree in the current session — it will not touch worktrees created manually or in a previous session. No filesystem changes were made.
Claude: The tool returned a no-op error — it says there's no active EnterWorktree session, even though we just created one. This matches the bug described in the recent commit `5a75d29`: **"EnterWorktree state lost across query() calls"** — the worktree session state isn't persisted across separate `query()` calls in the SDK.
The worktree directory and branch (`worktree-glistening-crunching-donut`) likely still exist on disk at `.claude/worktrees/glistening-crunching-donut`. You may need to clean those up manually with `git worktree remove`.
Result: success
```
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.