Skip to content

Commit 14d1fc4

Browse files
unknownclaude
andcommitted
Prevent agents from escaping worktree via prompt constraint
Root cause found via agent logs: Claude Code agents cd out of their worktree into the main repo when they encounter errors (e.g., npm test fails in worktree). This abandons the worktree, and subsequent git operations in the main repo context can destroy worktree metadata. Fix: append worktree constraint to agent prompt instructing it to stay in the current directory and not run git worktree/init/thinktank commands. Evidence from agent-1 logs: cd C:/Users/silon/Documents/repos/thinktank && npm run lint Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 975c7c6 commit 14d1fc4

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/runners/claude-code.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ export const claudeCodeRunner: Runner = {
3838
let error = "";
3939
let settled = false;
4040

41+
// Append worktree constraint to prompt to prevent agents from escaping
42+
const constrainedPrompt =
43+
`${opts.prompt}\n\nIMPORTANT: Work ONLY in the current directory (${opts.worktreePath}). ` +
44+
"Do NOT cd to other directories. Do NOT run git worktree, git init, or thinktank commands. " +
45+
"All your changes must be made in the current working directory.";
46+
4147
const args = [
4248
"-p",
43-
opts.prompt,
49+
constrainedPrompt,
4450
"--output-format",
4551
"text",
4652
"--model",

0 commit comments

Comments
 (0)