Skip to content

chore(agents): enforce worktree-first discipline with a PreToolUse guard - #1839

Merged
xuyushun441-sys merged 1 commit into
mainfrom
chore/agent-worktree-guard
Jun 14, 2026
Merged

xuyushun441-sys merged 1 commit into
mainfrom
chore/agent-worktree-guard

Conversation

@xuyushun441-sys

Copy link
Copy Markdown
Collaborator

Why

Multiple agents work this repo in parallel. The shared main checkout gets its HEAD switched and the tree reset under an agent mid-task, silently clobbering uncommitted edits — a full session's work was reverted twice during #1837. AGENTS.md already recommended one-worktree-per-task, but it was a soft "prefer" buried below the Prime Directives, and nothing enforced it. The gap was adherence, not clarity — so this adds a guardrail.

What

  • .claude/hooks/guard-main-checkout.sh (new) — a PreToolUse hook wired in .claude/settings.json that blocks Edit/Write/NotebookEdit whenever the session's checkout HEAD is on main, with an actionable message telling the agent to create a worktree first. Exits 0 (allow) on any feature branch / non-git dir. OS_ALLOW_MAIN_EDITS=1 overrides for a deliberate, non-task human fix.
  • AGENTS.md — promotes worktree-first to Prime Directive Migrate documentation site to Fumadocs with monorepo structure and shared content #11 and hardens the "Multi-agent working discipline" section ("prefer" → mandatory; the shared main checkout is explicitly not a supported fallback).

Verification

Hook tested locally across all three paths:

  • feature branch → exit 0 (allow)
  • main checkout → exit 2 (block) with guidance
  • main + OS_ALLOW_MAIN_EDITS=1 → exit 0 (allow)

Tooling/docs only — no package version impact (empty changeset).

🤖 Generated with Claude Code

Multiple agents work this repo in parallel. The shared `main` checkout has its
HEAD switched and the tree reset under an agent mid-task, silently clobbering
uncommitted edits (a full session's work was reverted twice). AGENTS.md already
recommended one-worktree-per-task, but it was a soft "prefer" buried below the
Prime Directives and nothing enforced it.

- Add `.claude/hooks/guard-main-checkout.sh` (PreToolUse, wired in
  `.claude/settings.json`): blocks Edit/Write/NotebookEdit whenever HEAD is on
  `main`, with an actionable message; `OS_ALLOW_MAIN_EDITS=1` overrides for a
  deliberate non-task fix.
- Promote worktree-first to AGENTS.md Prime Directive #11 and harden the
  Multi-agent working discipline section ("prefer" -> mandatory; main is not a
  supported fallback).

Tooling/docs only — no package version impact.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jun 14, 2026 6:37am

Request Review

@github-actions github-actions Bot added size/s documentation Improvements or additions to documentation tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Jun 14, 2026
@xuyushun441-sys
xuyushun441-sys merged commit 3ecb012 into main Jun 14, 2026
11 checks passed
@xuyushun441-sys
xuyushun441-sys deleted the chore/agent-worktree-guard branch June 14, 2026 06:39

This branch was successfully deployed

1 active deployment
Preview — e7cb79c9 Deployed Jun 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/s tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants