TODO: Add pre-agent fetch/pull and post-merge worktree cleanup
Problem
Dev agent spawned with
isolation: "worktree" created a worktree from stale local main. The worktree was missing ~260 lines of infrastructure (DORA, CNPG, Postgres) that had been merged to remote but not fetched locally. Result: agent's PR would have destroyed production resources. 40K+ tokens wasted. QA also failed to catch the scope mismatch.Root Cause
Two SOP gaps with no enforcement:
- No fetch/pull before worktree creation. The
worktree-workflowSOP says "ALWAYS fetch + pull main before creating a worktree" but Claude Code's built-inisolation: "worktree"bypasses this — it branches from whatever HEAD is, with no fetch. - No worktree cleanup after merge. SOP says clean up after merge, but no hook enforces it. 10 stale worktrees accumulated in
.claude/worktrees/, some weeks old.
Fix Options
Option A: PreToolUse hook on Agent tool
When
isolation: "worktree" is detected, run git fetch forgejo && git pull forgejo main before the worktree is created. Problem: the PreToolUse hook can block but can't modify behavior — it can only pass/fail.Option B: Betty Sue runs fetch/pull before every agent spawn
Add to
agent-spawn-conventions: "Before spawning a dev agent, run git fetch <remote> && git pull <remote> main." Relies on discipline, not automation.Option C: Post-merge cleanup hook
After a PR merge, auto-remove the associated worktree. Could be a PostToolUse hook on
mcp__forgejo__merge_approved_pr.Option D: Periodic cleanup
A session-start hook that removes worktrees whose branches have been merged to main.
Recommendation
Option B (immediate, low-effort) + Option D (automated safety net). Option A would be ideal but PreToolUse hooks can't inject commands before tool execution.
Incident
2026-03-06: PR #19 on pal-e-platform. Dev agent worktree branched from stale main missing DORA+CNPG+Postgres resources. Caught during
tofu plan review before merge. No production impact.