Skip to content

fix(#910): new-feature.sh must not mutate the main checkout - #912

Merged
admarble merged 3 commits into
mainfrom
feature/910-fix-hooks-new-feature-sh-bypasses-the-checkout-gua
Aug 8, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/910-fix-hooks-new-feature-sh-bypasses-the-checkout-gua

Conversation

@admarble

@admarble admarble commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • new-feature.sh no longer mutates the main checkout, closing the last bypass of the feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901 checkout guard.
  • The guard in pre-tool.sh inspects only the top-level Bash command an agent runs; ./scripts/new-feature.sh <issue> carries no guarded git verb, so it passed the guard and then ran git checkout <base> + git pull internally, in the main checkout — letting a non-holder session switch the holder's branch out from under it. The refusal message even recommended new-feature.sh as the way to proceed, so the documented escape hatch was the bypass.
  • Fix per the issue's option 2: remove the mutation rather than teach the hook to see through the script. The worktree is now branched directly off the freshly fetched origin/<base> via git worktree add -b <branch> <path> origin/<base>, which resolves the start-point ref without checking out or pulling into the main tree. No hook change is needed — the operation that required the lock no longer exists.
  • Consequences: a dirty main checkout no longer blocks worktree creation, and --stash (whose only job was to clear the way for the old checkout) becomes a deprecated no-op, still accepted so existing callers do not break.

AC / Test-plan coverage

Issue test-plan item Status Evidence
Main checkout's HEAD unchanged when the script runs without holding the lock ✅ The script never touches the main tree; new-feature-checkout-guard.integration.test.ts asserts branch + HEAD unchanged from an unrelated branch
Guard still allows the script when the lock is free / held by the caller ✅ No hook change; the script runs no guarded verb, so the guard never engages regardless of lock state
(option 2) Worktree creation still works with the main checkout on an unrelated branch with a dirty tree ✅ Dedicated dirty-tree test; dirty changes left untouched

Test plan

  • npm run build — clean
  • npm run lint — clean
  • npx vitest run on the four new-feature/worktree integration files — 103 passed
  • Gate test mutation-verified: reintroducing git checkout <base> + git pull fails Test A (main branch flips to main) and Test C (local base ref mutated by pull); restored, green again

Closes #910


QA gap fill: cleanup-worktree.sh sibling site (commit 1c705c4)

QA's cross-file sibling scan flagged that cleanup-worktree.sh's post-cleanup "update main" step shared the exact root cause: an unconditional git checkout main (+ pull/rebase) run inside a script the pre-tool guard can't see into. Fixed in this PR with the same option-2 treatment:

  • On main already: update in place (fetch + ff-only pull; rebase on divergence) — unchanged behavior, no branch switch involved.
  • On any other branch (or detached HEAD): the checkout is left alone; the local main ref is fast-forwarded via git fetch origin main:main, which never touches the working tree. Not fast-forwardable → skip with a message.
  • release.sh's git pull is deliberately untouched: it refuses to run unless already on a clean main (so it can never switch a branch out from under a holder), and /release lock participation is fix(skills): /merger and /release mutate the main checkout without participating in the checkout lock #911's scope.

Gate test mutation record: reintroducing git checkout main after the fetch fails exactly the two tests that assert no branch switch in cleanup-worktree-checkout-guard.integration.test.ts ("leaves an unrelated branch checked out…" and "skips the main-ref update…"); the on-main test rightly still passes (checkout is a no-op there). Restored — all 7 tests across both checkout-guard suites green.

The pre-tool checkout guard (#901) inspects only the top-level Bash command
an agent runs. `./scripts/new-feature.sh <issue>` carries no guarded git verb,
so it passed the guard and then ran `git checkout <base>` + `git pull`
internally, in the main checkout — letting a non-holder session switch the
holder's branch out from under it. The guard's own refusal message even
recommended new-feature.sh as the way to proceed, so the documented escape
hatch was the bypass.

Fix per the issue's option 2: remove the mutation instead of teaching the hook
to see through the script. The worktree is now branched directly off the
freshly fetched `origin/<base>` via `git worktree add -b <branch> <path>
origin/<base>`, which resolves the start-point ref without checking out or
pulling into the main tree. Because the main working tree is no longer touched,
a dirty main checkout no longer blocks worktree creation, and `--stash` (whose
only job was to clear the way for the old checkout) becomes a deprecated no-op,
still accepted so existing callers do not break. No hook change is needed.

Gate test (new-feature-checkout-guard.integration.test.ts, mutation-verified
by reintroducing checkout+pull → Test A and Test C fail):
- main checkout branch/HEAD unchanged after running the script from an
  unrelated branch
- worktree created even with a dirty main working tree
- worktree branched off the fetched origin/<base>, local base ref untouched
- --stash accepted as a no-op without stashing

Entire-Checkpoint: 300525659938
…g branches

QA's cross-file sibling scan on the new-feature.sh fix flagged the identical
root cause in cleanup-worktree.sh: its post-cleanup "update main" step ran an
unconditional `git checkout main` (+ pull/rebase) inside the script, invisible
to the pre-tool checkout guard, so a non-holder session could switch the main
checkout's branch out from under the #901 lock holder.

Same option-2 treatment: never switch branches. On main, update in place as
before; on any other branch, ff-update the local main ref via
`git fetch origin main:main` (never touches the working tree), skipping with
a message when not fast-forwardable.

release.sh's `git pull` is deliberately left alone: it refuses to run unless
already on a clean main (no branch switch possible), and /release lock
participation is #911's scope.
…-new-feature-sh-bypasses-the-checkout-gua

# Conflicts:
#	CHANGELOG.md
@admarble
admarble merged commit 429e335 into main Aug 8, 2026
5 checks passed
@admarble
admarble deleted the feature/910-fix-hooks-new-feature-sh-bypasses-the-checkout-gua branch August 8, 2026 03:34
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): new-feature.sh bypasses the checkout guard — the script the block message recommends mutates the main checkout

1 participant