Skip to content

fix(#1228): pre-tool.sh reset --hard guard evaluates the directory the reset targets - #1237

Merged
admarble merged 3 commits into
mainfrom
feature/1228-fix-hooks-pre-tool-sh-reset-hard-guard-reads-git-s
Sep 30, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/1228-fix-hooks-pre-tool-sh-reset-hard-guard-reads-git-s

Conversation

@admarble

@admarble admarble commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Root cause: pre-tool.sh's git reset --hard guard ran git status, the unpushed-commit check and the MERGE_HEAD check in the hook process's own cwd. PreToolUse runs before the command's shell, so a cd <wt> && prefix had not taken effect yet. Clean worktrees were blocked whenever the main checkout was dirty, and dirty worktrees were allowed whenever it was clean. git -C <dir> reset --hard never matched the trigger at all.
  • Fix (all three hook copies, byte-identical):
    • New resolve_git_c_target next to resolve_cd_target. It reads the raw command, because $SEGMENTS blanks git -C "$WT". It fails open on $, backticks and backslashes, and resolves a relative -C path against the cd target.
    • The trigger is widened to git +(-C +[^ ]* +)?reset…. It still matches sanitized segments only, so quoted and heredoc text never triggers it (fix(hooks): ^gh (issue|pr) prefix skip disarms all 5 pre-tool guards for compound commands, and re-breaks on any cd prefix #763).
    • Target order follows the feat(worktree): configurable worktree root #1199 commit guard: the -C path, then the cd target, then the payload cwd. A target that is not a repo falls back to the payload cwd, then the process cwd, so an unresolvable target never allows silently.
    • The branch lookup, Check 1, Check 2, Check 3 (--absolute-git-dir, since a linked worktree keeps MERGE_HEAD in its per-worktree gitdir) and the remediation branch name all use git -C "$RESET_DIR". The local variable GIT_DIR is renamed to RESET_GIT_DIR so it cannot shadow an exported GIT_DIR.
  • Plugin reach: Claude Code runs both the repo hook and the plugin-cache hook. Plugin users only get this fix after the plugin version bump in the next release.

Pre-PR AC Verification

AC Description Status Evidence
AC-1 cd <clean-wt> && git reset --hard origin/x allowed while main is dirty ✅ __tests__/pre-tool-hook.integration.test.ts #1228 block, "AC-1: allows …", ×3 hook copies
AC-2 cd <dirty-wt> &&, git -C <dirty-wt>, git -C "<dirty-wt>" and cd <parent> && git -C <relative> all block with a clean hook cwd ✅ "AC-2: blocks …" (4 forms), which also assert git push origin feature/1228-dirty
AC-3 Bare git reset --hard in a dirty cwd still blocks; no existing corpus verdict changes ✅ npx tsx scripts/hook-corpus.ts --diff origin/main gives 0 changes on existing entries; __tests__/hook-corpus.integration.test.ts 1025/1025; "AC-3: still blocks …"
AC-4 Check 1, Check 3 and the remediation branch use the resolved target ✅ "AC-4: blocks … mid-merge" (worktree MERGE_HEAD, branch feature/1228-merge) and "AC-4: allows … only the main checkout is mid-merge"
AC-5 (derived) Quoted git -C <dir> reset --hard body text stays allowed ✅ "AC-5: quoted body text …"
AC-6 (derived) A dynamic git -C "$WT" falls back to the payload cwd ✅ "AC-6: a dynamic git -C path …" (clean allows, dirty blocks)

Hook verdicts changed:

  • git -C /Users/user/Projects/missing reset --hard origin/main (new corpus entry, state dirty): allow -> block. This is intended: git -C … reset now triggers the guard, and a missing target falls back to the cwd. Its clean twin stays allowed. No existing entry changed.

Mutation verification

Mutation-verified: AC-1 — reverted Check 2 to a cwd-relative git status --porcelain; __tests__/pre-tool-hook.integration.test.ts AC-1 test failed (along with the 4 AC-2 dirty-worktree cases, which also depend on Check 2); restored.

Two more mutations, for extra coverage:

  • Reverting the trigger to git reset.* failed the three git -C AC-2 cases and AC-6.
  • Running Check 3 in the process cwd failed both AC-4 cases.

Runner verification (tracker #1236)

QA verdict: NEEDS_VERIFICATION. The run log's QA summary was null, and the issue comment carries only the verdict line, so the runner verified the change independently.

  • Probe: the real hook (branch hooks/pre-tool.sh) was run with real payloads against a sandbox main checkout and two linked worktrees:

    Case Before (main) After
    bare git reset --hard, dirty main BLOCKED BLOCKED
    git -C <dirty-wt> reset --hard from a clean cwd allowed BLOCKED
    cd <clean-wt> && git reset --hard from a dirty main (the report) BLOCKED allowed
    cd <dirty-wt> && git reset --hard from a clean cwd allowed BLOCKED
    WT=…; cd "$WT" && git reset --hard from a dirty main BLOCKED BLOCKED (see below)
    quoted gh … --body "git -C /x reset --hard" allowed allowed
  • Added by the runner (b6162999): the issue's own repro used cd "$WT". A variable target can't be resolved before the command's shell runs, so the guard checks the command's cwd and still blocks there. That's the safe side, but the old message didn't say why. The block now prints Checked: <dir> and says that only a literal cd <dir> / git -C <dir> target is resolved. The AC-6 test asserts both lines.

  • Repeated the AC-1 mutation myself (Check 2 reverted to a cwd-relative git status, in all three copies). The AC-1 and AC-2 cases failed for every hook copy; restored.

  • __tests__/pre-tool-hook.integration.test.ts, __tests__/hook-corpus.integration.test.ts and scripts/hook-corpus.test.ts: 1461 passed. The three hook copies are byte-identical (cmp).

Test plan

  • npx vitest run __tests__/pre-tool-hook.integration.test.ts: 423/423
  • npx vitest run __tests__/hook-corpus.integration.test.ts: 1025/1025
  • npm run build, npm run lint, bash -n, and cmp across the three hook copies
  • Full npm test not run, per the spec's standard block (targeted suites only)

Follow-ups (not in scope)

  • resolve_cd_target takes the last cd, so cd <dirty> && git reset --hard && cd <clean> evaluates <clean>. It also tests a relative cd target against the process cwd. Both limits already affect the commit guard, and the reset guard now inherits them. They are noted in a code comment and are candidates for one shared follow-up.

Closes #1228
Refs #1236

🤖 Generated with Claude Code

WIP checkpoint before mutation testing

Entire-Checkpoint: e2b470885a29
…d target fix

Entire-Checkpoint: fe845e95beee
The issue's own repro used cd "$WT": a variable target can't be resolved before the shell runs, so the guard still checks the command's cwd and blocks. That's the safe side, but the message gave no hint why; it now prints the checked directory and says only literal targets resolve.

Entire-Checkpoint: 87fbf46730f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(hooks): pre-tool.sh reset --hard guard reads git status from the hook's cwd, not the command's target worktree

1 participant