{"record":{"id":"10c6532f3f827cfa","repo":"gastownhall/beads","slug":"cannot-remove-the-worktree-containing-the-running","errorCode":null,"errorMessage":"cannot remove the worktree containing the running command","messagePattern":"cannot remove the worktree containing the running command","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/worktreeremove/policy.go","lineNumber":178,"sourceCode":"\t\treturn Cleanup{}, false\n\t}\n\treturn Cleanup{Entry: plan.managedIgnoreEntry}, true\n}\n\nfunc (plan Plan) valid() bool {\n\treturn plan.approved && (plan.mode == Normal || plan.mode == Force) && plan.targetPath != \"\"\n}\n\n// Prepare validates the initial observations and returns an approved plan.\nfunc Prepare(request Request, facts PrepareFacts) (Plan, error) {\n\tif request.Mode != Normal && request.Mode != Force {\n\t\treturn Plan{}, fmt.Errorf(\"worktree removal mode is absent or invalid\")\n\t}\n\tswitch facts.Target {\n\tcase PrimaryWorktree:\n\t\treturn Plan{}, fmt.Errorf(\"cannot remove the primary worktree\")\n\tcase CurrentWorktree:\n\t\treturn Plan{}, fmt.Errorf(\"cannot remove the worktree containing the running command\")\n\tcase RegisteredTarget:\n\tdefault:\n\t\treturn Plan{}, fmt.Errorf(\"registered worktree target is absent or invalid\")\n\t}\n\tif facts.Registration != Present || facts.RegisteredPath == \"\" ||\n\t\tfacts.TargetDir != Present || facts.GitAdminDir != Present ||\n\t\tfacts.GitMarker != Present || facts.Head != Present {\n\t\treturn Plan{}, fmt.Errorf(\"registered worktree target is absent or invalid\")\n\t}\n\tif facts.CommonDir != Matched {\n\t\treturn Plan{}, fmt.Errorf(\"target common git directory does not match repository\")\n\t}\n\tif facts.Status != Clean && facts.Status != Dirty {\n\t\treturn Plan{}, fmt.Errorf(\"worktree cleanliness observation is absent or invalid\")\n\t}\n\tif facts.ManagedIgnore != IgnoreAbsent && facts.ManagedIgnore != IgnoreManaged {\n\t\treturn Plan{}, fmt.Errorf(\"managed ignore observation is absent or invalid\")\n\t}","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/worktreeremove/policy.go#L160-L196","documentation":"Prepare refuses to remove the worktree that contains the currently running process (facts.Target == CurrentWorktree). Removing the checkout the command itself is executing from would pull the working directory out from under the process. Like the primary-worktree rule, this is an unconditional safety refusal evaluated before fact completeness.","triggerScenarios":"Calling Prepare with facts.Target == CurrentWorktree — typically when the caller detected that the resolved worktree path is an ancestor of os.Executable()/the cwd; running a remove command from inside the worktree being removed.","commonSituations":"Developer cds into a linked worktree and runs the remove command for that same path; an agent/script executes removals from inside one of the worktrees it iterates over; shells whose cwd silently resolves to the worktree being targeted.","solutions":["Run the removal from a different directory (e.g. the primary worktree or outside the repo).","Detect the current-worktree condition before calling Prepare and refuse with a clear message telling the user to cd elsewhere.","In automation, chdir the worker process to a neutral path before performing removals.","If targeting multiple worktrees, exclude the one containing the process from the batch."],"exampleFix":"// before\nplan, err := worktreeremove.Prepare(req, facts) // facts.Target == CurrentWorktree\n\n// after\nif facts.Target == worktreeremove.CurrentWorktree {\n    return fmt.Errorf(\"cd out of this worktree before removing it\")\n}\nplan, err := worktreeremove.Prepare(req, facts)","handlingStrategy":"validation","validationCode":"cwd, _ := os.Getwd()\nif isWithin(cwd, targetPath) {\n    return fmt.Errorf(\"cannot remove the worktree containing the running command; cd elsewhere first\")\n}","typeGuard":null,"tryCatchPattern":"plan, err := worktreeremove.Prepare(req, facts)\nif err != nil && strings.Contains(err.Error(), \"containing the running command\") {\n    return fmt.Errorf(\"run this command from outside %s\", targetPath)\n}","preventionTips":["Compare the process cwd (or executable path) with the target before removal.","Run batch removals from a neutral directory (e.g. the primary worktree or $HOME).","In scripts/agents, chdir to a safe path before issuing removals."],"tags":["validation","worktree","git","policy"],"backgroundTag":"operation-not-permitted","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}