Description
Tier: judgment Graph: tracker #1236 (label graph-2026-09-prerelease)
Moved from admarble/varius#65 — pre-tool.sh is sequant-managed, so the fix has to land here. Still present at a12c441d (hooks/pre-tool.sh:834, same line in templates/hooks/ and .claude/hooks/).
What
.claude/hooks/pre-tool.sh's git reset --hard guard (~line 525) blocked this command:
cd "$WT" && git reset --hard origin/feature/42-... && git merge origin/masterwith:
HOOK_BLOCKED: git reset --hard would lose local work:
- 16 uncommitted file(s)
The target worktree was completely clean — git status --short in it printed nothing.
Root cause
The guard runs git status --porcelain with no -C, so it evaluates the hook's own cwd (the main checkout), not the directory the command targets. PreToolUse runs outside and before the command's shell, so the cd "$WT" prefix has not happened yet — a fact fullsolve/SKILL.md already documents in its Phase 0.3 note ("PreToolUse runs outside and before your command's shell, so nothing a skill bash block exports is visible to it"). The guard just doesn't account for it.
Proof: the main checkout has exactly 16 dirty files, matching the block message digit for digit:
$ git status --porcelain | wc -l
16
Why it matters here specifically
Those 16 files are sequant's own drift artifacts — modified .claude/skills/*/SKILL.md, .sequant-manifest.json, untracked .sequant/ and .claude/.local/. They are permanently present in this checkout. So the guard currently blocks every cd <worktree> && git reset --hard in this repo, unconditionally, regardless of the worktree's real state. The safety check is inverted from its intent: it fires hardest in exactly the isolated worktrees that are safest to reset.
Compounding cost (hit live during #42): with reset --hard blocked and force-push blocked, a routine post-rebase push had no in-session path at all — it had to be handed to the human to run manually.
Fix sketch
Resolve the target directory from the command's own cd/git -C prefix and run the status check there:
TARGET_DIR=$(extract_cd_or_dashC_target "$COMMAND") # fallback: $PWD
UNCOMMITTED=$(git -C "${TARGET_DIR:-.}" status --porcelain 2>/dev/null | wc -l | tr -d ' ')Check 1 (unpushed commits) and Check 3 (MERGE_HEAD) have the same defect — both resolve against the hook's cwd — so all three want the same treatment.
Non-goals
- Relaxing the guard. Blocking a genuinely destructive
reset --hardis correct; this is about evaluating the right repository.
Found during admarble/varius#42 (PR admarble/varius#63).
- Fix pattern already in the file.
resolve_cd_target(used by the commit guard since 2026-09-20,pre-tool.sh:575) resolvescd/segment-startcdtargets. Reuse it, plus agit -C <dir>extraction, for all three reset-hard checks, with fallback to the hook cwd.
Probe (2026-09-30, main @ 9da8aeb8, hooks/pre-tool.sh driven with a real payload)
| Command | Hook cwd | Today | Correct |
|---|---|---|---|
git reset --hard |
dirty repo | BLOCKED | BLOCKED |
git -C <dirty> reset --hard |
clean repo | allowed | BLOCKED |
cd <clean> && git reset --hard |
dirty repo | BLOCKED (this report) | allowed |
cd <dirty> && git reset --hard |
clean repo | allowed | BLOCKED |
The false allows are the more serious half. git -C <dir> reset --hard never matches the trigger (seg_match 'git reset.*(--hard|origin)' requires git reset to be adjacent), and a cd <dirty> prefix is evaluated against the clean hook cwd. So the fix must widen the trigger to accept git -C <dir> reset, as well as resolve the target directory. That's why the tier is judgment.
Acceptance Criteria
- AC-1:
cd <clean-worktree> && git reset --hard origin/xis allowed while the hook cwd (the main checkout) is dirty. Evidence:npx vitest run __tests__/pre-tool-hook.integration.test.ts, using a sandbox main checkout plus a linked worktree; mutation-verified by reverting Check 2 to a cwd-relativegit status - AC-2:
cd <dirty-worktree> && git reset --hardandgit -C <dirty-worktree> reset --hardare both blocked, including when the hook cwd is clean. Evidence:npx vitest run __tests__/pre-tool-hook.integration.test.ts - AC-3: A bare
git reset --hardin a dirty cwd is still blocked, and every existing golden-corpus verdict is unchanged. Evidence:npx vitest run __tests__/hook-corpus.integration.test.ts - AC-4: Check 1 (unpushed commits), Check 3 (
MERGE_HEAD) and the remediation text's branch name all evaluate the same resolved target directory. Evidence:npx vitest run __tests__/pre-tool-hook.integration.test.ts, covering a worktree mid-merge reached with acd <wt>prefix (BLOCKED) and a main checkout mid-merge with acd <clean-wt>prefix (allowed)
Non-Goals
- Relaxing the guard. Blocking a genuinely destructive reset is correct; this is about evaluating the right repository.
🤖 Generated with Claude Code
May touch
hooks/pre-tool.sh,.claude/hooks/pre-tool.sh,templates/hooks/pre-tool.sh(keep all three byte-identical)__tests__/pre-tool-hook.integration.test.ts__tests__/fixtures/hook-corpus.jsonl(add entries only; don't change existing verdicts)
File overlap: none with the other #1236 nodes.
Activity
admarble commentedon Sep 30, 2026
Context for the run (re-checked against
main@9da8aeb8, 2026-09-30; graph tracker #1236, wave 1)The body now carries a probe table and retargeted ACs. Read it first: the trigger regex has to widen, not just the git calls.
Current code (
hooks/pre-tool.sh, identical in.claude/hooks/andtemplates/hooks/)::820: triggerseg_match 'git reset.*(--hard|origin)'. It never matchesgit -C <dir> reset ….:821:CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD). This feeds Check 1 and the remediation text (git push origin $CURRENT_BRANCH).:826: Check 1,git log origin/$CURRENT_BRANCH..HEAD.:833: Check 2,git status --porcelain.:840: Check 3,GIT_DIR=$(git rev-parse --git-dir)then$GIT_DIR/MERGE_HEAD. In a linked worktreeMERGE_HEADlives in the per-worktree gitdir, so this must run with-C <target>.Target resolution: mirror the #1199 commit guard exactly (
:1212-1214):HOOK_CWDis the payload'scwd, which is the command's own directory. A bare$PWDfallback is wrong.git -C "$TARGET_DIR" ….TARGET_DIRisn't a git repo, keep today's behaviour for that case and don't silently allow.Trigger: widen it so
git -C <dir> reset --hard(andgit -C <dir> reset … origin…) match. Keep the #763 property that quoted or heredoc text never triggers.$SEGMENTSis already sanitized, so match against it just asseg_matchdoes today.Tests:
__tests__/pre-tool-hook.integration.test.ts. Reuse the feat(worktree): configurable worktree root #1199 sandbox pattern in that file (a main checkout plus a linked worktree atcustom-root/issue-1199). For AC-4's mid-merge state, writeMERGE_HEADundergit -C <wt> rev-parse --git-dir.__tests__/hook-corpus.integration.test.ts, which snapshots every verdict, so any changed existing entry is a regression. You may add corpus entries, but you may not change existing verdicts.git statusand confirm exactly the AC-1 test fails. Record the marker.cmp).PR body: say that the hook change reaches plugin users only after the plugin version bump in the next release.
Standard block:
npm testin exec or in qa. This applies to the qa phase as well as exec. Run targeted suites in the foreground with a timeout:timeout 600 npx vitest run __tests__/pre-tool-hook.integration.test.ts __tests__/hook-corpus.integration.test.ts. Global setup runsnpm run buildfirst.failedTeststarts with the test file path.Closes #1228. UseRefsonly for the tracker, tracker(graph): pre-release fixes before the next release after 2.18.0 (4 nodes) #1236..sequant/settings.jsonmust handle JSONC (not expected to apply here).### Fixedentry under[Unreleased].admarble commentedon Sep 30, 2026
Complexity: Standard (4 ACs, 2 directories)
AC Quality Check
All four ACs are single-line with a runnable
Evidence:clause, and none is vacuous onmain@9da8aeb8. AC-1 and AC-2 fail today per the probe table in the body (a false block and two false allows). AC-4's cd-into-a-mid-merge-worktree case is a false allow today. AC-3 passes today, which is expected because it is the regression guard.-Cpath. The literal casegit -C <dirty-worktree> reset --hardis unquoted, but$SEGMENTSblanks double-quoted regions.git -C "$WT" reset --hard origin/xsanitizes togit -C reset --hard origin/x(probed withemit_segments). So the-Cpath has to come from the raw command, the wayresolve_cd_targetreads it, not from$SEGMENTS. A dynamic path ($, backtick, backslash) fails open to the fallback directory, which is today's behaviour.Scope Assessment
Non-Goals: Relaxing the guard. Blocking a genuinely destructive reset stays correct.
hooks/×3 mirrors,__tests__/)Verdict: ✅ SCOPE_OK
Acceptance Criteria
AC-1:
cd <clean-wt> && git reset --hard origin/xis allowed while the main checkout is dirtyVerification: Integration Test (
__tests__/pre-tool-hook.integration.test.ts)Scenario: Given a sandbox main checkout with an untracked file and a clean linked worktree. When the hook runs from the main checkout (process cwd and payload
cwd= main) withcd <wt> && git reset --hard origin/x. Then exit 0 with noHOOK_BLOCKED.Assumptions:
origin/xdoesn't need to exist, because the guard never resolves the ref. Mutation: revert Check 2 to a baregit status --porcelain, and only this test fails. Record<!-- SEQUANT_MUTATION: {"ac":"AC-1",…,"failedTest":"__tests__/pre-tool-hook.integration.test.ts > … > …"} -->.AC-2:
cd <dirty-wt> && git reset --hardandgit -C <dirty-wt> reset --hardboth block, even with a clean hook cwdVerification: Integration Test
Scenario: Given a clean main checkout and a linked worktree with an untracked file. When either form runs from main. Then exit 2 with
HOOK_BLOCKED: git reset --hard would lose local workand1 uncommitted file(s).Assumptions: The trigger has to widen. Today
seg_match 'git reset.*(--hard|origin)'never seesgit -C <dir> reset. Add a quoted-path variant (git -C "<dirty-wt>" reset --hard) so the raw-command extraction is pinned too.AC-3: A bare
git reset --hardin a dirty cwd still blocks, and every golden-corpus verdict is unchangedVerification: Integration Test (
__tests__/hook-corpus.integration.test.ts)Scenario: Given the 339-entry corpus, 11 of which mention
reset. When it is replayed. Then the snapshot matches, and new entries are add-only.Assumptions: The corpus
cdtargets are/Users/user/…paths that don't exist, or dynamic (cd $W,cd $S). Soresolve_cd_targetreturns empty and falls back to the replay sandbox, which keeps existing verdicts. If one flips, that is a regression, not a snapshot update.AC-4: Check 1, Check 3 and the remediation branch name all use the resolved target
Verification: Integration Test
Scenario: Case 1: the worktree is mid-merge (
MERGE_HEADwritten undergit -C <wt> rev-parse --git-dir), andcd <wt> && git reset --hardruns from a clean main. The command is blocked withUnfinished merge in progress. Case 2: main is mid-merge but otherwise clean, andcd <clean-wt> && git reset --hardis allowed.Assumptions: In a linked worktree,
git rev-parse --git-dirreturns an absolute per-worktree path. In the main checkout it returns a relative.git, so it has to be anchored:git -C "$T" rev-parse --absolute-git-dir, or$T/$GIT_DIR. Add an assertion that the remediation line readsgit push origin feature/…, which pinsCURRENT_BRANCH.Implementation Plan
Phase 1: Hook (
hooks/pre-tool.sh, thencpto.claude/hooks/andtemplates/hooks/)resolve_git_c_target <tool_input>next toresolve_cd_target(:575). It prints the<dir>of a rawgit -C <dir> resetoccurrence and applies the same quote-strip, the same fail-open on$/backtick/backslash, and the same-dcheck. It runs on the raw command because$SEGMENTSblanks quoted paths. Resolve a relative<dir>against the cd target, or else againstHOOK_CWD.:820) toseg_match 'git( -C +[^ ]*)? +reset.*(--hard|origin)', or an equivalent ERE. It has to tolerate the blank that a sanitized quoted path leaves behind. The match stays on$SEGMENTS, so the fix(hooks):^gh (issue|pr)prefix skip disarms all 5 pre-tool guards for compound commands, and re-breaks on anycdprefix #763 guarantee still holds: quoted text and heredoc text never trigger it.:1212):RESET_DIR=-Cdir if found, elseresolve_cd_target, else${HOOK_CWD:-$PWD}. Ifgit -C "$RESET_DIR" rev-parse --git-dirfails, meaning the target is not a repo, fall back to${HOOK_CWD:-$PWD}. That is today's evaluation, and it never silently allows.:821,:826,:833,:840) togit -C "$RESET_DIR" …. Check 3 uses--absolute-git-dirsoMERGE_HEADresolves correctly from a hook process running in another directory.cmpall three copies.Phase 2: Tests
describe.each(HOOK_COPIES)block for fix(hooks): pre-tool.sh reset --hard guard reads git status from the hook's cwd, not the command's target worktree #1228 inpre-tool-hook.integration.test.ts. Reuse the feat(worktree): configurable worktree root #1199 sandbox shape: a main checkout plus a linked worktree at a custom root, and a runner that sets both the processcwdand the payloadcwd. Cover AC-1, AC-2 (unquoted, quoted, andcdforms), and AC-4 (both merge cases plus the branch in the remediation text). Toggle dirty/merge state per test (beforeEach), or use separate worktrees, so the cases don't bleed into each other.timeout 600 npx vitest run __tests__/pre-tool-hook.integration.test.ts __tests__/hook-corpus.integration.test.ts. No fullnpm test, in either exec or qa.failedTestthat starts with the file path.Phase 3: Release notes
[Unreleased]→### Fixedentry. The PR body saysCloses #1228andRefs #1236, and notes that plugin users only get the hook change after the next release's plugin version bump. Both hooks fire: the repo copy and the plugin-cache copy.Design Review
pre-tool.shreset guard, next to the existingresolve_cd_target/in_linked_worktreehelpers. The mirror copies stay byte-identical.-C. I rejected parsing a full shell (the "reimplementing the shell" argument from bug(hooks): pre-tool.sh No-Changes Guard only recognizes single-line 'cd path && git commit', silently blocking real multi-line commits #963) and moving the guard to TS (hooks are excluded from TS consolidation). Neither choice between designs is an ADR-level decision, so no ADR is needed.COMMIT_DIR=$(resolve_cd_target …); ${COMMIT_DIR:-${HOOK_CWD:-$PWD}}) and the bug(hooks): pre-tool.sh No-Changes Guard only recognizes single-line 'cd path && git commit', silently blocking real multi-line commits #963 fail-open-on-dynamic rule.$SEGMENTSfor-C?" It blanks quoted paths, as shown above.gh issue comment --body "git -C x reset --hard"allow case.Feature Quality Planning
All standard checks pass. Notable items:
resolve_cd_targethas two latent limits that the reset guard now inherits. (a) It takes the lastcdin the command, so incd <dirty> && git reset --hard && cd <clean>the guard evaluates<clean>, which is a false allow. (b) It tests a relative target with-dagainst the hook process cwd, notHOOK_CWD. Both already affect the commit guard. See Open Questions.Derived ACs
gh issue comment 1 --body "git -C /x reset --hard"stays allowed (the widened trigger ignores quoted text). Evidence:npx vitest run __tests__/pre-tool-hook.integration.test.tsgit -C "$WT" reset --hard(dynamic) evaluates the fallback directory, matching today. Evidence: same suiteOpen Questions
cdwins" inresolve_cd_target. Recommendation: leave it as it is for this issue, add a comment in the reset guard, and file a follow-up covering both guards. Impact if wrong: acd <dirty> && git reset --hard && cd <clean>chain gets through. That is rare, but it is a false allow.cdtarget resolved against the process cwd. Recommendation: fix it insideresolve_cd_targetonly if a relative case turns up in the new tests. Otherwise make it the same follow-up. Impact if wrong: a relativecd sub && git reset --hardfrom a payload cwd different from the process cwd evaluates the wrong directory, which exec can't reproduce in real Claude Code, where the two cwds match.Recommended Workflow
Phases: exec → qa
Quality Loop: enabled
Reasoning: This is a judgment-tier bug fix in a safety guard, and the owner's context comment already fixes the test design, so testgen would be ceremony. The quality loop is on because the trigger regex widens, and a regression there shows up as a corpus verdict flip.
Label Review
Current:
graph-2026-09-prereleaseRecommended: add
bug,plannedReason: It's a defect fix in
hooks/, and it's labeledplannedonce this spec posts.admarble commentedon Sep 30, 2026
QA Review for Issue #1228
AC Coverage
-C, quoted-C, relative-Cunder cd) block with branchfeature/1228-dirtyin remediationcd <clean-wt>allows; remediation branch pinned. Check 1 retarget verified by code review only (see gaps)-C "$WT"falls back to payload cwdCoverage: 6/6 met
CI Status
testCI Summary: 9 passed, 1 pending (
test), 0 failedLocal verification (run this pass)
timeout 600 npx vitest run __tests__/pre-tool-hook.integration.test.ts __tests__/hook-corpus.integration.test.ts→ 2 files, 1448/1448 passed, exit 0cmpacrosshooks/,.claude/hooks/,templates/hooks/→ byte-identicalnpm testnot run (per the issue's standard block)Code Review
Strengths: mirrors the #1199 commit guard's target order;
-Cpath is read from the raw command (sanitized$SEGMENTSblanks quoted paths) while the trigger still matches sanitized segments (#763 preserved); non-repo target falls back to payload cwd then process cwd (never a silent allow);--absolute-git-dirfixes the relative-.gittrap for Check 3;GIT_DIR→RESET_GIT_DIRavoids env shadowing.Issues / gaps (non-blocking):
cd "$WT" && git reset --hard origin/feature/42-… && git merge origin/masterwith a dirty main checkout → stillHOOK_BLOCKED(probed). This is the spec's intended bug(hooks): pre-tool.sh No-Changes Guard only recognizes single-line 'cd path && git commit', silently blocking real multi-line commits #963 fail-open for a dynamic path, and literal paths now work, but the reporter's exact shape isn't fixed.mainwith unpushed commits while the hook cwd differs; only code review covers it.git -C <dirty> -c x=y reset --hard,git -c … -C <dirty> reset --hard,git --git-dir=… reset --hardnever trigger. Same for last-cd/last--Cwins (noted in PR).Adversarial Re-Read
Findings: Ran 10 hand probes against a real main+worktree sandbox: literal/quoted cd → allowed when target clean;
echo '…git -C … reset --hard'allowed;git -C wt log && git reset --hardevaluates cwd (correct: the-Cisn't on the reset);--softnot triggered; relative-C ../wtresolved. Gaps are the three above. Non-goal (relaxing the guard) not violated.Status: Gaps Found
Trust-Boundary Check
Finding: Clean — diff implements only product requirements; no agent-directed external-text instruction was acted on.
Status: Clean
Behavior-Rule Survival Check
Status: N/A — no AC triggers the heuristic.
Declared-Evidence Execution
npx vitest run __tests__/pre-tool-hook.integration.test.tsnpx vitest run __tests__/hook-corpus.integration.test.tsStatus: Complete
Mutation Verification
__tests__/pre-tool-hook.integration.test.ts > … > AC-1: allows …Status: Verified
CHANGELOG Verification
Result:
[Unreleased]→### Fixedentry present with (#1228); notes the plugin-bump reach.Risk Assessment
cd "$WT") still evaluates the hook cwd — both the false block (dirty main) and false allow (clean main, dirty target) persist for that shape.mainwith unpushed commits.resolve_cd_target's last-cd-wins and process-cwd-dlimits.Verdict: NEEDS_VERIFICATION
All ACs are met locally, but the required
testCI check is still IN_PROGRESS on3cd15fa2. Once it is green, this isAC_MET_BUT_NOT_A_PLUS(non-blocking gaps above), notREADY_FOR_MERGE.Next Steps
/qa 1228oncetestreports.cd, other git global options, last-cdwins).admarble commentedon Sep 30, 2026
QA Verdict: NEEDS_VERIFICATION
fix(#1228): reset guard's block names the repository it checked
admarble commentedon Sep 30, 2026
Runner verification: PR #1237 (QA NEEDS_VERIFICATION; QA's review body wasn't recorded, only the verdict line). I drove the real hook with payloads against a sandbox main checkout and linked worktrees. Both false allows are closed (
git -C <dirty> reset --hard, andcd <dirty> && …from a clean cwd), and the reported false block is fixed for literalcd <wt>.Design point for the owner: the report's exact command,
cd "$WT" && git reset --hard …, is still blocked when the main checkout is dirty. A variable target can't be resolved before the command's shell runs, so the guard checks the command's cwd. That fails closed on purpose. The block message now printsChecked: <dir>and says only literal targets resolve, so the fix for the user is to write the path literally. I repeated the AC-1 mutation myself. Full table in the PR.