Edit-Time Design-Canon Guard — Recommendation
Date: 2026-08-04 Question: Must UserHappy catch design-canon drift at edit time with a Claude Code hook? Method: Read the three guards. Traced both documented failures through git history. Built and measured a prototype hook.
Verdict
Do not build the drift hook. Build the shared canon first. Then add one narrow hook that guards the token files, not the call sites.
The evidence is stronger than expected. The proposed hook would have caught neither of the two failures that motivate it. Not the typeface gap, and not the warm-gray gap either.
Finding 1 — The hook catches neither failure
Failure 1: warm gray as text ink (69 sites, 11 days)
The call sites never contained the banned value. At c3b79c0, raw 0xB0A49D appeared outside the token file zero times. All 72 ink-role uses read like this:
.foregroundStyle(UserHappyDesign.Colors.tertiaryInk)
That line is correct code. It uses a design token. No regex over that file can flag it, and no regex should.
The violation lived on one line, in one file. This diff is the entire fix:
-static let tertiaryInk = Color(hex: 0xB0A49D) // brand warm gray
+static let tertiaryInk = Color(hex: 0x565352)
Sixty-nine call sites changed colour. Not one call site was edited.
The dates make it worse. Aline banned the value on 2026-07-23. The last commit that added warm-gray ink to iOS was 9c2d03a5 on 2026-07-22 — one day earlier. Between the ban and the fix, three commits touched ios/, and none touched the token file.
A hook installed on the day of the ban fires zero times on this violation. Edit-time guards see edits. Pre-existing debt is invisible to them. A ratified rule that must reach existing code needs a sweep, and a sweep is a whole-repo operation.
Failure 2: SF Pro typeface (388 sites)
This one is worse for the hook, exactly as suspected.
Those sites arrived across 12 or more commits between 2026-07-03 and 2026-07-13. Nobody wrote down the rule "iOS text uses Mulish or Poppins" until 2026-08-03. Commit 2a1e9dea on 2026-07-13 bundled Mulish and Poppins and added .system( sites in the same commit.
A hook enforces rules. It cannot invent them. No hook, test, linter, or language server catches a rule that nobody has stated.
Stated plainly, as asked: the proposal does not catch the iOS typeface gap. A person who compared the two platforms on purpose caught it. The lesson is "schedule the parity audit", not "add a hook".
Finding 2 — A reimplemented rule cries wolf at once
I wrote the hook. Twenty lines, faithful to paletteCanon.test.ts as I read it. I ran it against src/pages/Dashboard.tsx.
It reported that the file grew its hex debt from 6 to 49.
All 43 extra hits were false. I had dropped one clause: the ALLOWED_HEXES filter. The file holds 43 hex literals. 37 are canon swatches. 6 are genuinely off-canon, which is exactly what the baseline records.
One clause. First file. All noise. I produced this by accident while I tried to be careful.
The lesson generalises. The rule set today holds the 11 canon swatches, 4 Google brand hexes, black, a 3-digit hex normaliser, the staff-surface polarity inversion, 4 hard bans, 4 scan exclusions, 3 iOS typography exemptions, and a token-file carve-out. Any second implementation drifts from that set. Drift becomes noise. Noise gets the hook turned off.
The concern in the brief is correct. The failure arrives faster than a week.
Finding 3 — Ratchet at edit time works, but only after the write
The tool contract decides this question.
PreToolUse runs before the write. The file on disk holds the old content. For Edit, the input gives old_string and new_string — a fragment, not the resulting file. To count whole-file occurrences, the hook must replay the edit itself. A replay must reimplement replace_all, uniqueness, and MultiEdit semantics. The ratchet is not available at PreToolUse. Hard bans against the inserted fragment do work.
PostToolUse runs after the write. The file on disk holds the new content. The hook reads it and counts exactly. The ratchet works, and it works per file, which is how palette-baseline.json is already keyed.
So the ratchet objection is not fatal. But it forces PostToolUse, and PostToolUse means the write already happened. The hook becomes a fast warning, not a block.
Is a hard-bans-only hook worth having? Barely. The four web hard bans cover patterns that were fully swept. They return rarely. CI catches them in one round trip. The hook saves one round trip on a rare event.
Finding 4 — Performance is a non-issue, and it does not matter
Measured on this repo:
| Operation | Time |
|---|---|
| node cold start | ~30 ms |
| whole-file regex scan (largest debt file) | 0.7 ms |
| end-to-end hook (spawn, read, scan, compare) | 30–50 ms |
full vitest palette guard |
2.74 s |
The budget is comfortable. A regex sweep of one file fits with three orders of magnitude to spare.
Performance was never the constraint. Correctness of the rule set is. These numbers close the question rather than support the hook.
One consequence matters. The real test runs in 2.74 s and is correct by definition. If edit-time feedback is the goal, a hook that calls the real test cannot drift. That beats any reimplementation.
Finding 5 — The hook covers one author
Hooks fire when Claude Code edits. They do not fire for a human in an IDE, for git apply, for a rebase, for another agent, or for a teammate who does not run Claude Code.
As enforcement, that is a colander. CI stays the gate regardless. The hook is therefore pure addition and never a replacement.
A pre-commit hook covers every author and every path into the repo. It can call the real test. It gets whole-repo state for free, so the ratchet works without any reimplementation. For coverage per unit of work, pre-commit dominates the Claude hook. It loses only on immediacy, and it costs 2.7 s per commit.
The NUL-byte blind spot is already closed
I scanned every .ts, .tsx, .swift, and .css file for real NUL bytes. Zero files.
A caution for whoever tests this next. My first scan reported more than 300 files. The command used grep -qa $'\0', and zsh turns $'\0' into an empty pattern, which matches every line. A byte-level check in Python gave the true answer.
PaletteCanonTests.swift already force-decodes bytes for this reason. New tooling must read bytes and force-decode. That is a two-line habit, not a reason to build anything.
Build this instead
1. One canon file, two consumers
design/canon.json holds:
- swatches, as hex to name
- roles (ink, fill, border, surface, danger, success, warning) and the swatches each role permits
- typefaces per role, per platform
- bans, each with the surfaces it covers and its polarity
- exemptions: SF Symbols, monospace, italic, Google SSO hexes, fixtures, token files
- the per-file ratchet baseline
paletteCanon.test.ts and PaletteCanonTests.swift both read it. One rule addition then covers both platforms.
This design catches failure 1, which no per-file regex can. The canon states that #b0a49d serves fill and border, never ink. The token files declare the role of each token:
--color-warm-gray: #b0a49dserves rolefillColors.tertiaryInkserves roleink, by name
A check that resolves token to role to permitted swatch flags tertiaryInk = 0xB0A49D on the day somebody writes it. One file. Deterministic. This is a token-graph check, not a text scan, and it stays small because token files are small.
It does not catch failure 2. Nothing catches failure 2. That gap needs a calendar entry for the next parity audit.
2. The propagation test — the guard that was actually missing
Add one CI test: every ban in canon.json must supply a matcher for every platform it claims. A ban that lists platforms: ["web", "ios"] with no Swift pattern fails the build.
This is the guard whose absence caused the 11-day gap. It is about 30 lines. It is the highest-value item in this document.
3. The narrow hook, if you still want one
Scope it to the two token files. Blast radius is high there, false positives are near zero, and the check is semantic rather than textual.
.claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write|MultiEdit",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/canon-token-guard.mjs",
"timeout": 10
}
]
}
]
}
}
The matcher field matches tool names, not paths. Path filtering therefore belongs in the script. canon-token-guard.mjs exits 0 at once unless file_path ends with src/index.css or ios/UserHappy/Core/Design/UserHappyDesign.swift. Otherwise it reads design/canon.json, resolves every token in the edited file to a role and a swatch, and exits 2 with a message on a role violation. Exit code 2 returns stderr to Claude, which then corrects the file in the same turn.
Choose PostToolUse over PreToolUse for two reasons. PostToolUse sees the written file, which is the thing that must be analysed. And a wrong PostToolUse warning costs a note, while a wrong PreToolUse denial stops the session. Given the false-positive risk measured above, prefer the failure mode that only annoys.
This hook fires perhaps once a month. That is the point. It stays quiet, and it guards the one line that moved 69 call sites.
Rollout
Scope: project, not user. The rule is a property of this repo. User scope would apply it to unrelated repos and would never reach a teammate.
Prerequisite that is easy to miss. .gitignore line 4 is .claude/*, with an un-ignore only for .claude/skills/. A committed project hook therefore needs two new lines:
!.claude/settings.json
!.claude/hooks/
Without them the hook stays local and the teammate problem remains unsolved.
How a teammate turns it off. They add the same block to .claude/settings.local.json with the hook removed, or they delete the entry there. That file is gitignored and takes precedence over the shared file. No negotiation with the repo owner is required.
Order of work.
design/canon.jsonand the propagation test.- Both existing guards refactored onto the canon file.
- The narrow token-file hook.
Steps 1 and 2 carry most of the value. If step 3 never lands, little is lost. If only step 3 lands, nothing is gained.