Skip to content

ci: block closing keywords that would auto-close a pull request - #484

Merged
nockawa merged 2 commits into
mainfrom
ci/guard-closing-keywords
Jul 14, 2026
Merged

nockawa merged 2 commits into
mainfrom
ci/guard-closing-keywords

Conversation

@nockawa

@nockawa nockawa commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

The incident this prevents

While merging #483, GitHub silently closed the unrelated, in-flight PR #482. Nobody clicked anything — but the audit trail said actor=nockawa, because GitHub attributes keyword-closes to whoever merged.

The cause was one sentence in #483's description, in a Notes section explaining what the PR doesn't do:

It does not retroactively fix #482's red check — that run stays in the history.

Two GitHub behaviours combined:

  1. The closing-keyword parser has no notion of negation. It matches a bare KEYWORD #N adjacency and nothing else. does not retroactively fix #N links and closes identically to fixes #N.
  2. Pull requests and issues share one numbering space. The docs only ever discuss closing issues, but PRs are issues underneath — so an #N that happens to be a PR closes that PR.

There is no repository, org, or enterprise setting to disable closing keywords. A CI check is the only systemic defense — hence this PR.

What the check does

New closing-keywords job, wired into the aggregate gate verdict. It scans the same two surfaces GitHub's own parser reads and fails only when a keyword-linked number resolves to a pull request. The everyday intended use — a closing keyword naming a real issue — stays green.

Surface handling deliberately mirrors GitHub:

Surface Handling Why
PR description Markdown — fenced blocks + inline code spans stripped first GitHub doesn't parse refs inside code. Without this, any doc that quotes a keyword self-trips — including the backticked defusing form the error message recommends. (This very PR body is the proof: it's full of quoted keywords and stays green.)
Commit messages Plain text — scanned raw, no stripping Backticks don't defuse a keyword in a commit message. Matters because squash-merge concatenates every commit message into the squash body.

Word boundary is spelled (^|[^[:alnum:]_]) rather than \b so it doesn't depend on GNU-grep extensions — without it, postfix #1 trips on the trailing fix.

Verification

Replayed against the real incident:

Plus 15 positive/negative regex cases, including every defusing form the error message suggests (`#N` backticked, PR #N intervening word, rephrased) and near-miss words (postfix, hotfix, suffix, prefix-fixture).

And the markdown-stripping test: a synthetic PR body documenting closing keywords drops from 4 false positives → 0, keeping only the one genuinely live reference.

When it fires, contributors see

Closing keyword targets #N, which is a PULL REQUEST — merging would auto-close it. Defuse the reference: backtick it, write 'PR #N', or rephrase.

🤖 Generated with Claude Code

nockawa and others added 2 commits July 14, 2026 07:04
GitHub scans a PR's description and its commit messages for a bare KEYWORD-then-
reference adjacency (close/closes/closed, fix/fixes/fixed, resolve/resolves/
resolved, immediately followed by #N) and closes item N on merge into the default
branch. Two properties make this a trap:

  1. The parser has NO notion of negation. The phrase "does not retroactively
     fix", immediately followed by "#N", links and closes item N exactly as if it
     had read "fixes #N".
  2. Pull requests and issues share one numbering space, so an N that happens to
     be a PR closes that PR.

Both fired together: the body of PR #483 contained that negated phrasing followed
by a bare reference to the then-open PR #482, silently closing that unrelated
in-flight PR on merge. GitHub attributes such closes to whoever merged, so the
audit trail blamed a human who never clicked anything.

There is no repo/org/enterprise setting to disable closing keywords, so a check is
the only systemic defense. New `closing-keywords` job scans the same two surfaces
GitHub does and fails only when a keyword-linked number resolves to a pull request.
The everyday intended use -- a closing keyword naming a real issue -- stays green.

Surface handling mirrors GitHub's own parser:
  - PR description is Markdown: fenced blocks and inline code spans are stripped
    first, since GitHub does not parse references inside them. Without this the
    guard flags any doc that merely quotes a keyword -- including the backticked
    form the guard's own error message recommends.
  - Commit messages are plain text: backticks do NOT defuse a keyword there, so
    they are scanned raw. This matters because squash-merge concatenates every
    commit message into the squash body.

Regex notes: the leading (^|[^[:alnum:]_]) is a word boundary -- without it
"postfix #N" trips on the trailing "fix". Spelled out rather than \b so it does
not rely on GNU-grep extensions. An intervening word (as in "fix PR #N") breaks
GitHub's adjacency rule and is correctly not matched here either.

Wired into the aggregate `gate` verdict; skipped on push/workflow_dispatch, where
there is no PR body to scan.

Verified: replays the incident (PR #483 -> RED, PR #482 -> GREEN), plus 15
positive/negative regex cases including the defusing forms the error suggests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The step's shell is `bash -e` and the script adds pipefail, so the keyword grep
exiting 1 on NO match -- the healthy, common case -- propagated and killed the
script before it printed anything. Every clean PR went red; caught when this
branch's own PR failed its own check with zero output.

Guard the assignment with `|| true` and note why it must stay. Verified under
`bash -e` + pipefail: no-keyword -> exit 0, keyword-present -> detected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nockawa
nockawa merged commit e3a885b into main Jul 14, 2026
6 checks passed
@nockawa
nockawa deleted the ci/guard-closing-keywords branch July 18, 2026 12:11
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.

1 participant