Skip to content

fix(hook): make commit-msg reject missing trailers when requireTrailers is on - #339

Merged
stackbilt-admin merged 3 commits into
mainfrom
auto/bugfix/issue-299
Oct 15, 2026
Merged

stackbilt-admin merged 3 commits into
mainfrom
auto/bugfix/issue-299

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Closes #299

The generated commit-msg hook was a normalizer: it rewrote a Governed-By or Resolves-Request trailer the author had already typed, and exited 0 otherwise. Bootstrap defaults to requireTrailers: true, so every commit passed locally and the first push failed charter validate --ci.

What changed

  • hook.ts: the hook still runs the git interpret-trailers normalization. After that it applies the same rule as validate / 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 with git stripspace --strip-comments, and a commit -v diff below the scissors line is dropped.
    • When it blocks: only when CI would fail the commit for the policy, meaning citationStrictness is FAIL (the default) or STRICT, or ci.failOnWarn is true. It exits 1 and prints a one-line example, Governed-By: ADR-001.
    • When it only warns: under advisory strictness (WARN/PERMISSIVE), matching CI's WARN.
    • Config resolution: mirrors loadConfig. Each section is merged over the defaults (requireTrailers: true, citationStrictness: FAIL), requireTrailers is tested for truthiness, and malformed JSON falls back to the defaults and blocks, with a stderr notice naming the file.
    • When it is off: only requireTrailers falsy, or two deliberate exceptions that each print a stderr warning: no .charter/config.json at all (the hook can't tell the repo opted in), and no node on PATH.
    • Known gap: comment lines are always stripped, so a commit made with -m or --cleanup=verbatim where a #... line follows the trailer can pass locally and fail CI. This is documented.
    • Reading the config: done with node -e. The generated pre-commit hook already needs node through npx.
    • Exemptions: none. CI doesn't exempt merge, revert or fixup! commits, so the hook doesn't either.
  • 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.
  • Tests: the hook is installed into a temp repo and driven through a real git commit, using only relative paths. The tests cover:
    • rejection with the example message
    • default strictness
    • a trailer-like line outside the trailer block
    • acceptance of either trailer, case-insensitive, with normalization kept
    • a body-only trailer moved into the trailer block
    • a warning under advisory strictness
    • blocking under failOnWarn
    • rejection for an absent git section or key, a truthy non-boolean value, and malformed JSON (checking the stderr notice); no gate for requireTrailers: false, or for no config file (with a warning)
    • doctor's verdict agreeing with the hook for {"git":{}}
  • Docs (README, cli-reference, ci-integration and their snippets) and CHANGELOG.

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

…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 stackbilt-admin left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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 over DEFAULT_CONFIG, which has requireTrailers: true and citationStrictness: 'FAIL'. If the file can't be parsed, it falls back to those defaults. validate then tests requireTrailers for truthiness. The hook instead needs c.git && c.git.requireTrailers === true and otherwise turns itself off. In each case below I committed feat: nothing with the hook installed, then ran charter validate --ci --format json. The hook let every commit through; validate failed every one:

    • {"git":{}}: hook exit 0, validate FAIL
    • {"project":"x"} (no git key): 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, requireTrailers defaults to true. Test it for truthiness, like validate.
    • 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 run charter 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.
  2. 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 no git key and one for a truthy non-boolean value.

  3. Doctor reports a gate that does nothing. (packages/cli/src/commands/doctor.ts:178-181 with doctor-gate-enforcement.ts:69)
    Doctor decides the policy is strict by reading the merged loadConfig, 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.

  4. Docs and CHANGELOG describe the wrong behaviour. (CHANGELOG.md:11, packages/cli/README.md:296, hook comment hook.ts:47)
    They say "requireTrailers not literally true … 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 extractTerminalTrailerBlockLines in 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-By key 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 stripspace strips 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-trailers rewrote. When interpret-trailers moves 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 inside node -e '…' contains no single quotes. set -eu together with || gate_mode=off and || trailer_state=present behaves as intended.
  • cwd: git runs the hook from the top of the worktree. A commit made from sub/deep was still gated correctly by the relative .charter/config.json.
  • Injection: none possible. The output of node is restricted to off/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.ts and doctor-gate-enforcement.test.ts pass (30/30).

Non-blocking notes

  • Comment stripping can cause false accepts under the whitespace and verbatim cleanup modes. The hook always runs git stripspace --strip-comments. Git's default cleanup for git commit -m without an editor is whitespace, 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 # heading paragraph has the same effect. My harness found 6 such mismatches across -m and 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=verbatim with Governed-By: (whitespace-only value): the hook rejects it, but CI accepts it, because \s*.+ in trailers.ts matches a trailing space. This is arguably a bug in trailers.ts, not in the hook.
  • The hook hardcodes .charter/config.json. A repo whose CI runs validate --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:23 labels the commit-msg gate "Local validation hook". It's accurate enough, but "Local trailer gate" would be clearer.

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.

bug: hook install --commit-msg installs a trailer normalizer, not a gate, so requireTrailers fails only in CI

2 participants