Spec-driven development, for real
Spec-driven development, for realPart 10
Guardrail hooks
On this page
New to the short codes like REQ, ADR or AC? See the vocabulary in Part 1, or hover over any code.
In short: A few moments in every change go wrong quietly: a merge from a stale branch, a force-push, a pull request with no record of what it did, a push that leaves the tracker stale, an edit that breaks a principle. Each gets one hook. The policy is deliberately asymmetric: only the irreversible things block; daily work warns, with the exact checks attached. Every hook file starts with why it exists.
Part 9 checked the change. This part covers block 9, which guards the moments around it. The team and product are invented.
Thursday, 9:15
Maya’s pull request is ready. When she asks Claude to merge it, a warning comes back: the branch is nine commits behind main. She would have merged without a second look. Instead she runs the two checks the warning names, and finds that resolving a conflict last night quietly undid a rounding fix Jonas merged on Monday.
Nobody did anything wrong on purpose. That is exactly why it needs a hook.
Think of it like this: A railway does not put a signal on every metre of track. It puts them at the junctions, where trains can collide. Each signal does one thing, and the driver knows why it is there.
Signals at the junctions
How work disappears without anyone deleting it
- Day 1 · Maya branches from main
Her branch has
fees.pyas it is today. - Day 5 · Jonas fixes rounding on main
His fix changes three lines of
fees.py. Merged, reviewed, green. - Day 12 · Maya resolves a conflict by keeping her whole file
Her editor makes it one click. Jonas’s three lines are gone from her branch.
- Day 12 · The pull request diff looks clean
GitHub shows the diff against the merge base: day 1. Against day 1, she changed only her lines.
- Day 13 · Merged. Jonas’s fix is gone
No conflict, no red test (his test was in the same file), no reviewer saw it.
The trap is in step 4. GitHub shows a pull request’s diff against the merge base, the commit the branch started from, not against today’s main. A stale branch that overwrites a whole file conflicts with nothing and looks clean.
Block or warn?
A force-push to a shared branch. It rewrites history others built on and erases the evidence. There is no good reason, and no undo from the other side.
A merge. Merges are daily work; blocking them trains people to switch the hook off. The warning carries the two checks: how far behind, and what it deletes.
MERGE SAFETY (warning): before merging, prove nothing is being deleted.
1. How stale is it? git rev-list --count <pr-head>..origin/main (this checkout: 9 behind)
2. What does it delete? git diff --diff-filter=D --name-status origin/main...<pr-head>
git diff origin/main...<pr-head> | grep '^-[^-]'
A PR that is 0 commits behind and deletes nothing cannot clobber anyone.
See the pr-merge-safety skill.
Rule: turn on “Require branches to be up to date before merging” in your branch protection. It removes this whole class of problem. Without it, GitHub calls a branch thirty commits behind “mergeable”, because it only checks for textual conflicts.
A line per pull request
## Unreleased
### Added
- ACME-41: a rental returned late shows its fee at the desk, per started
hour and never more than a day; staff also see how it was worked out.
---
$ gh pr create --fill (on a branch with code and no entry)
CHANGELOG: this pull request changes code but not CHANGELOG.md.
Add one line under "## Unreleased", at the end of Added, Changed or Fixed:
- TICKET-123: what changed, in plain words for a reader outside the team
The tracker gate works the same way on git push: code without a work.md update is refused. It asks for one file only. Earlier versions in the real project tried to work out which other docs each change affected; they were noisy and still missed things, and a gate that is both gets reached past. So it requires the one file that is always right, and prompts for the rest.
Trackers that never conflict
Every pull request appends to the same files. Without help, every merge would conflict on the others.
- Three pull requests are open
Each one appended a line to
work.mdandCHANGELOG.md, as the gates require. - One merges
The other two now conflict on lines nobody disagrees about.
- A workflow merges main into each open branch
.gitattributesmarks the trackersmerge=union, so git keeps both sides’ lines.work.md merge=union CHANGELOG.md merge=union docs/modules/STATUS.md merge=union
- A real conflict is left for a person
Anywhere else, the workflow aborts and comments on the pull request. Nothing dropped, nothing doubled.
Checking every edit
"editWarnings": [
{ "files": "src/**/*.py",
"pattern": "\\.objects\\.(all|filter|get)\\([^)]*company_id",
"message": "manual company filter: the tenant boundary is the scoped manager (P1)." },
{ "files": "src/features/**/*.tsx",
"pattern": "role\\s*===|hasRole\\(",
"message": "a role check in a feature (F1): render what the API returns." }
]
RULE CHECK after your edit:
src/features/returns/fee-panel.tsx:31: a role check in a feature (F1):
render what the API returns.
Under the hood: writing hooks people keep
- Start every hook with WHY. The kit’s hooks open with the incident or reasoning behind them, and what earlier versions got wrong. The next person deciding whether to keep a hook reads that first.
- Fail open. No git, no network, bad JSON: allow the action. A hook that wedges a session is switched off, and then it protects nothing.
- An off switch per session (
SPEC_KIT_MERGE_GATE=off,SPEC_KIT_CHANGELOG_GATE=offfor a revert or a dependency bump). - Exit codes: 0 allows, and stdout is shown; 2 blocks a PreToolUse call, and stderr goes to Claude. A PostToolUse hook uses exit 2 to show text to Claude, since the edit already happened. See hooks.
- Your own pain points get hooks too. The real project added a hook that stops several Claude sessions on one shared laptop each building their own copy of the same Docker image and database, after the machine ran out of memory. Not in the kit, because it is specific, but the pattern is the same: one moment, one reason, block or warn.
Which hook fits?
- Work vanished after a merge: merge-safety-gate + the skillRefuse force-pushes; warn on merges with staleness and deletions; detectors in pr-merge-safety.
- Nobody knows what shipped: changelog-gateOne plain line per pull request, required at create and merge.
- The tracker is always stale: push-tracker-gateEvery push with code updates work.md. One hard rule, plus a prompt for other docs.
- A rule keeps getting broken: edit-warningsA pattern and a reason in spec-kit.json, checked after every edit.
- Trackers conflict all the time: merge=union + the workflowAppend-only files merge by keeping both sides.
In the kit
merge-safety-gate.mjs,changelog-gate.mjs,push-tracker-gate.mjsandedit-warnings.mjs.- The
pr-merge-safetyskill: the four ways code disappears, and a detector for each. .gitattributesand theupdate-pr-branchesworkflow.
Three rules of thumb
Force-pushes and missing records block. Daily work warns, with the checks attached.
Every hook file starts with WHY. The next person reading it decides whether to keep it.
Earlier tracker gates tried to work out which docs a change affects. Noisy and incomplete. Require one file; prompt for the rest.
QuizA pull request is 12 commits behind main and its diff looks clean. Is it safe to merge?
Show the answer
Answer: Not yet: merge main into it first, then check what it deletes
The clean diff is against the merge base, not today’s main. Merging main in first makes the branch current, so any line it would remove from newer work shows up as a real change you can see.
Words in this post:
- Merge base: the commit a branch started from. GitHub diffs against it.
- Stale-branch clobber: a behind branch overwriting newer work without a conflict.
- Force-push: rewriting a branch’s history. Blocked on shared branches.
- merge=union: a git setting that keeps both sides’ lines in a file.
- Asymmetric policy: block the irreversible, warn on the routine.
Next, Part 11: Proof of done shows that green tests and “a person can do this” are two different claims, and how to prove both.
Setting up Claude Code or AI agents for your team?
I help teams build this: agents, RAG search and Claude Code setups that hold up in production.
Series
Spec-driven development, for real
- Spec-driven development, for realThe building blocks
- Spec-driven development, for realThe paper trail
- Spec-driven development, for realPrinciples and scoped rules
- Spec-driven development, for realDecision records
- Spec-driven development, for realDefinitions and open questions
- Spec-driven development, for realFeature specs and plans
- Spec-driven development, for realThe contract
- Spec-driven development, for realGates that make Claude show its work
- Spec-driven development, for realSkills and reviewers
- Spec-driven development, for realGuardrail hooksyou are here
- Spec-driven development, for realProof of done
- Spec-driven development, for realMemory and handoffs
- Spec-driven development, for realOne ticket, end to end