Skip to content

Detect gate assertions whose literal also appears in prose, using go/ast #846

Closed as not planned

Description

@scttfrdmn

Split out of #845. The remaining Phase 4 item, scoped honestly rather than swept.

The defect class

A gate asserting strings.Contains(artifact, "LITERAL") cannot fail if LITERAL also
appears in a comment in the source that generates the artifact — the sentence
documenting the gate satisfies the gate. That is CLAUDE.md rule 3, and it has bitten
repeatedly:

Why this is not a sweep

There are 324 strings.Contains(artifact, …) assertions in the tree. The large
majority are correct — asserting that a specific generated line is present is the right
thing to do, and most literals are not words that appear in prose.

Why a regex scan will not do

I tried. It reported 38 candidates and is dominated by false positives, because stripping
// comments with a regex also strips inside string literals — so policy JSON, shell
templates and URLs in raw strings get truncated and their contents look comment-only.
Spot-check: it flagged iam:CreateServiceLinkedRole as appearing only in comments; it is
in code at pkg/aws/iam.go:1013.

The deliverable

A gate using go/ast to separate code from comments properly — the approach
cmd/user_facing_strings_test.go already takes, for exactly this reason. For each
strings.Contains(x, "LIT") in a _test.go file:

  1. collect every string literal and identifier text from the non-test sources of the
    package under test (and, where the artifact is spliced from elsewhere, the whole repo);
  2. collect the comment text separately;
  3. flag an assertion whose literal appears in the comment set and not in the code set.

Expect a handful of real findings and a short exception list — a literal may legitimately
appear in both. Each exception needs a reason, and if the list grows past a few entries the
rule is wrong (rule 6).

Related

The same shape is already gated in two narrower places, which is where the pattern was
learned: scripts/lambda-deploy-census.sh anchors on ^\s*aws logs put-retention-policy
rather than the bare call name, and sigVerifyOn matches (?m)^SPORED_SIG_VERIFY=1$
rather than the bare identifier, because the identifier appears in two shell comments
(#813).

Activity

  1. scttfrdmn commented on Oct 11, 2026

    @scttfrdmn
    ContributorAuthor

    I built this and am recommending against shipping it. Negative result, with the
    measurements, so nobody repeats the attempt.

    What I built

    go/ast-based, as the issue specified: parse every non-test Go file, strip comments by
    byte offset
    so a code pattern asserted as syntax still counts as code, collect comment
    text separately, then flag any strings.Contains(x, "LIT") in a test whose literal appears
    in the comment corpus and not the code corpus.

    What it found

    16 findings, then 6, then 0 true positives.

    First run, Go string literals and identifiers as the "code" corpus: 16 findings, all false.
    Three distinct corpus gaps:

    gap examples
    the artifact is not Go "./test/e2e" (asserts on the Makefile), "far-future" (asserts on template.yaml and a README)
    the literal is Go syntax, not a string "time.Sleep(", "return nil", "ec2.NewFromConfig(c.cfg)"
    the string is produced outside the repo "connection refused", "address already in use", "InvalidInstanceType"

    Widening the corpus to raw comment-stripped Go source plus every .yaml/.yml/.sh/.md/.json/.py
    and Makefile fixed the first two gaps: 16 → 6.

    All 6 remaining are the third gap — the asserted text is produced by code this repository
    does not contain:

    • "connection refused", "address already in use" — Go's net package error text
    • "InvalidInstanceType" — an AWS API error code
    • "(static fallback)" — rendered by SourceLabel() in libs/pricing, a separate module

    So: zero true positives, six false ones, and no way to distinguish "prose-only in this
    repo" from "produced by a dependency" without enumerating every dependency's error strings.

    Why not just add an exception list

    Six entries, all saying "this string comes from somewhere else", and no way to tell a
    seventh legitimate one from a real defect. That is precisely the smell the rule I added in
    #845 describes: if a gate needs an allowlist of things that are obviously fine, the rule is
    wrong — a list of what is normal means the rule is wrong, which is rule 4 inside out.

    Shipping it would add a gate with no true positives and a maintenance burden that trains
    people to extend the allowlist rather than read the failure. That is worse than no gate,
    for the same reason stated at the top of the "Writing a gate" section.

    What to do instead

    The defect class is real — four instances are on record (spawn:version,
    put-retention-policy, #818, #808) — but every one was caught by reading the failure
    message of a revert that should have failed and didn't
    . That is rule 1, and it already
    covers this: a gate verified against a compiling revert cannot be satisfied by its own
    prose, because the revert changes the code and the prose stays.

    Rule 3 remains the right guidance for the author, and rule 1 the enforcement. Automating
    the detection does not pay here.

    Closing unless you disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions