Repository navigation
fix(hook): make commit-msg reject missing trailers when requireTrailers is on - #339
Conversation
…rs is on (closes #299) The generated commit-msg hook only normalized a Governed-By or Resolves-Request trailer the author had already written, so with bootstrap's default requireTrailers: true every commit passed locally and the first push failed charter validate --ci. The hook keeps the interpret-trailers normalization and then applies CI's rule: either trailer, case-insensitive, in the terminal trailer block of the message git will record (comments and any commit -v diff stripped). It blocks when CI would fail the policy (citationStrictness FAIL/STRICT, or ci.failOnWarn) with a one-line example, and only warns under advisory strictness. Missing or malformed config, requireTrailers not true, or no node on PATH disables the gate. Doctor now counts the new hook as a local gate and asks for a re-install of a pre-gate normalizer hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
stackbilt-admin
left a comment
There was a problem hiding this comment.
CHANGES_REQUESTED
I built the branch, installed the generated hook in temp repos, and ran 22 messages through it with git commit -m, -F --cleanup=strip and -F --cleanup=verbatim. For each one I compared the hook's exit code with parseTrailersFromMessage (packages/git/dist) run on the message that ended up committed. The trailer-block logic matches CI. The config handling does not.
Required changes
-
The config defaults don't match
loadConfig, so the hook accepts commits that CI rejects. This is the bug #299 reports. (packages/cli/src/commands/hook.ts:47-60, line 54 in particular)
loadConfig(packages/cli/src/config.ts:193-226) merges each section overDEFAULT_CONFIG, which hasrequireTrailers: trueandcitationStrictness: 'FAIL'. If the file can't be parsed, it falls back to those defaults.validatethen testsrequireTrailersfor truthiness. The hook instead needsc.git && c.git.requireTrailers === trueand otherwise turns itself off. In each case below I committedfeat: nothingwith the hook installed, then rancharter validate --ci --format json. The hook let every commit through; validate failed every one:{"git":{}}: hook exit 0, validate FAIL{"project":"x"}(nogitkey): hook exit 0, validate FAIL{"git":{"requireTrailers":"true"}}: hook exit 0, validate FAIL{bad(malformed JSON): hook exit 0, validate FAIL- no config file: hook exit 0, validate FAIL
Fix:
- Mirror the merge semantics. When the key is absent,
requireTrailersdefaults to true. Test it for truthiness, likevalidate. - For malformed JSON, follow CI and treat the policy as strict. At minimum, print a stderr warning; don't turn off silently.
- A missing config file can reasonably stay off, because a repo with no
.charter/may never runcharter validate. That would be a deliberate exception to parity, so name it as one in the hook comment and the docs. Today the docs present it as ordinary behaviour.
-
Two tests assert behaviour that CI contradicts. (
packages/cli/src/__tests__/hook.test.ts:139-140)
'requireTrailers is absent'({ git: {} }) and'the config is malformed JSON'both expect exit 0, but CI fails both configs. Change them to expect rejection (or a warning, for malformed JSON, if that's what item 1 settles on). Add a case for a config with nogitkey and one for a truthy non-boolean value. -
Doctor reports a gate that does nothing. (
packages/cli/src/commands/doctor.ts:178-181withdoctor-gate-enforcement.ts:69)
Doctor decides the policy is strict by reading the mergedloadConfig, so{"git":{}}counts as strict. It then reports PASS once the gate marker is present, but with that config the hook doesn't gate. Item 1 fixes this as a side effect. Add a doctor or hook test that pins the two to the same answer. -
Docs and CHANGELOG describe the wrong behaviour. (
CHANGELOG.md:11,packages/cli/README.md:296, hook commenthook.ts:47)
They say "requireTrailersnot literallytrue… or unparseable JSON disables the gate". After item 1 this text no longer matches what the hook does, and even now it describes the hook as matching CI when it doesn't. Update it to the final behaviour.
Verified correct (no change needed)
- Trailer-block detection matches
extractTerminalTrailerBlockLinesin every case I ran:- only the terminal block counts, separated by a blank line
- a key with no value (
Governed-By:followed by a body paragraph) doesn't count - a space before the colon doesn't count
- an indented key doesn't count
- an
X-Governed-Bykey doesn't count - a value with a tab separator counts
- a lowercase key with no space counts
- a message whose only paragraph is a trailer counts
- CRLF, either a trailing CRLF or CRLF throughout, is handled (
git stripspacestrips the\r) - continuation lines and
(cherry picked from …)give the same result in the hook and in CI, because normalization collapses them first
- Normalization runs before the check, and the check reads the file that
interpret-trailersrewrote. Wheninterpret-trailersmoves a line, the hook and CI end up looking at the same text, so this can't cause a false accept or a false reject. - The generated script is intact. The installed file has no
${,\or CRLF damage. The JS insidenode -e '…'contains no single quotes.set -eutogether with|| gate_mode=offand|| trailer_state=presentbehaves as intended. - cwd: git runs the hook from the top of the worktree. A commit made from
sub/deepwas still gated correctly by the relative.charter/config.json. - Injection: none possible. The output of
nodeis restricted tooff/warn/block. A message containing$(touch pwned)and backticks ran nothing. - Doctor marker logic: an old hook is still recognised as Charter-managed, so re-installing overwrites it without
--force. - Tests:
hook.test.tsanddoctor-gate-enforcement.test.tspass (30/30).
Non-blocking notes
- Comment stripping can cause false accepts under the
whitespaceandverbatimcleanup modes. The hook always runsgit stripspace --strip-comments. Git's default cleanup forgit commit -mwithout an editor iswhitespace, which keeps#lines in the commit. Example:-m "feat: x\n\nGoverned-By: ADR-1\n#123". The hook accepts it, but the committed message ends in#123, so CI fails it. A trailing# headingparagraph has the same effect. My harness found 6 such mismatches across-mand verbatim. Only false accepts are possible, never false rejects. The hook can't see the cleanup mode, so at least write this limitation down. --cleanup=verbatimwithGoverned-By:(whitespace-only value): the hook rejects it, but CI accepts it, because\s*.+intrailers.tsmatches a trailing space. This is arguably a bug intrailers.ts, not in the hook.- The hook hardcodes
.charter/config.json. A repo whose CI runsvalidate --config <dir>gets no local gate. [[:space:]]in awk needs mawk 1.3.4 or later. Older mawk (Debian/Ubuntu before roughly 2020) would misparse it. It's probably fine, but[ \t]would be more portable.- The hook still doesn't check HIGH-risk unlinked commits, the other way CI can fail. The PR description already acknowledges this.
doctor-gate-enforcement.ts:23labels the commit-msg gate "Local validation hook". It's accurate enough, but "Local trailer gate" would be clearer.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # CHANGELOG.md
Closes #299
The generated commit-msg hook was a normalizer: it rewrote a
Governed-ByorResolves-Requesttrailer the author had already typed, and exited 0 otherwise. Bootstrap defaults torequireTrailers: true, so every commit passed locally and the first push failedcharter validate --ci.What changed
hook.ts: the hook still runs thegit interpret-trailersnormalization. After that it applies the same rule asvalidate/parseTrailersFromMessage. Either trailer counts, the key is case-insensitive, and only the terminal trailer block is checked. The check runs on the message git will record: comment lines are stripped withgit stripspace --strip-comments, and acommit -vdiff below the scissors line is dropped.citationStrictnessisFAIL(the default) orSTRICT, orci.failOnWarnis true. It exits 1 and prints a one-line example,Governed-By: ADR-001.WARN/PERMISSIVE), matching CI's WARN.loadConfig. Each section is merged over the defaults (requireTrailers: true,citationStrictness: FAIL),requireTrailersis tested for truthiness, and malformed JSON falls back to the defaults and blocks, with a stderr notice naming the file.requireTrailersfalsy, or two deliberate exceptions that each print a stderr warning: no.charter/config.jsonat all (the hook can't tell the repo opted in), and nonodeon PATH.-mor--cleanup=verbatimwhere a#...line follows the trailer can pass locally and fail CI. This is documented.node -e. The generated pre-commit hook already needs node throughnpx.doctor-gate-enforcement.ts: doctor now counts a commit-msg hook carrying the new gate marker as a local gate. For an old hook that only normalizes, it suggests re-installing. The old marker line is kept, so re-installing overwrites an existing hook without--force.git commit, using only relative paths. The tests cover:failOnWarngitsection or key, a truthy non-boolean value, and malformed JSON (checking the stderr notice); no gate forrequireTrailers: false, or for no config file (with a warning){"git":{}}Review: round 1 found that the hook's config defaults diverged from CI. A differential harness, 22 messages × 3 cleanup modes run through both the hook and
parseTrailersFromMessage, now shows no config mismatches. The 7 remaining mismatches are the documented#-line gap and a CI-parser quirk with whitespace-only values.Not addressed: baselining the validation range at adoption (
validateSince, the issue's second item), and the HIGH-risk file check that CI runs separately.Implemented via constellation-loop.
🤖 Generated with Claude Code