diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index c51f458..eae056e 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -19,6 +19,11 @@ jobs: - uses: actions/checkout@v4 with: persist-credentials: false + - name: comment-check + if: github.event_name == 'pull_request' + run: | + git fetch --no-tags --depth=2 origin "$GITHUB_SHA" + sh ci/comment-check.sh HEAD^1 - name: Rust ${{ env.RUST_VERSION }} run: | rustup toolchain install "$RUST_VERSION" --profile minimal --no-self-update diff --git a/catalog/stall-sweep/check.sh b/catalog/stall-sweep/check.sh index 830ff67..c173e28 100644 --- a/catalog/stall-sweep/check.sh +++ b/catalog/stall-sweep/check.sh @@ -1,4 +1,5 @@ #!/bin/sh +# Prints the stalled work. # stall-sweep's check: prints one line per open pull request in the # repository $WIZLOOP_PARAM_REPO that has had no progress for # $WIZLOOP_PARAM_IDLE_MINUTES minutes or more, with why it looks stalled. diff --git a/ci/check.sh b/ci/check.sh index 3f33a2b..04898b2 100644 --- a/ci/check.sh +++ b/ci/check.sh @@ -14,3 +14,4 @@ echo "check: no private identifiers" python3 -I ci/validate.py # The validator refuses broken copies. sh ci/test-validate.sh +sh ci/test-comment-check.sh diff --git a/ci/comment-check.sh b/ci/comment-check.sh new file mode 100644 index 0000000..12e6ba0 --- /dev/null +++ b/ci/comment-check.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +base=$1 +exec python3 -I "$(dirname "$0")/../tools/comment-check/check.py" diff --base "$base" --exclude 'format/fixtures/' diff --git a/ci/evidence/comment-check-red.txt b/ci/evidence/comment-check-red.txt new file mode 100644 index 0000000..d27bc39 --- /dev/null +++ b/ci/evidence/comment-check-red.txt @@ -0,0 +1,7 @@ +test-comment-check: catches: a pull request adding a disallowed comment passes CI: expected exit 1 and "catalog/x/check.sh:2: comment:", got exit 2: +sh: 0: cannot open /ci/comment-check.sh: No such file +test-comment-check: catches: a reasoned escape tag still fails CI: expected exit 0 and "", got exit 2: +sh: 0: cannot open /ci/comment-check.sh: No such file +test-comment-check: catches: a format fixture planting a comment the validator reads fails CI: expected exit 0 and "", got exit 2: +sh: 0: cannot open /ci/comment-check.sh: No such file +exit 1 diff --git a/ci/test-comment-check.sh b/ci/test-comment-check.sh new file mode 100644 index 0000000..dd0b8e1 --- /dev/null +++ b/ci/test-comment-check.sh @@ -0,0 +1,50 @@ +#!/bin/sh +set -eu +root=$(cd "$(dirname "$0")/.." && pwd) +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +failures=0 + +pull_request_ci() { + name=$1 path=$2 line=$3 + repo="$tmp/$name" + git init -q -b main "$repo" + cd "$repo" + git config user.email ci@example.com + git config user.name ci + mkdir -p "$(dirname "$path")" + printf '#!/bin/sh\necho base\n' > "$path" + git add -A && git commit -q -m base + git checkout -q -b pr + printf '#!/bin/sh\n%s\necho base\n' "$line" > "$path" + git commit -q -am pr + git checkout -q main + git merge -q --no-ff -m merge pr + set +e + out=$(sh "$root/ci/comment-check.sh" HEAD^1 2>&1) + code=$? + set -e + cd "$root" +} + +expect() { + case=$1 want_code=$2 want_text=$3 + if [ "$code" != "$want_code" ] || { [ -n "$want_text" ] && ! printf '%s' "$out" | grep -q -F -- "$want_text"; }; then + printf 'test-comment-check: %s: expected exit %s and "%s", got exit %s:\n%s\n' \ + "$case" "$want_code" "$want_text" "$code" "$out" >&2 + failures=$((failures + 1)) + else + echo "ok: $case" + fi +} + +pull_request_ci disallowed catalog/x/check.sh '# a plain comment' +expect 'catches: a pull request adding a disallowed comment passes CI' 1 'catalog/x/check.sh:2: comment:' + +pull_request_ci escaped catalog/x/check.sh '# comment-ok: the runner reads this line' +expect 'catches: a reasoned escape tag still fails CI' 0 '' + +pull_request_ci fixture format/fixtures/valid/x/check.sh '# Prints nothing: nothing is due.' +expect 'catches: a format fixture planting a comment the validator reads fails CI' 0 '' + +[ "$failures" -eq 0 ] || exit 1 diff --git a/contracts/comments.v1.md b/contracts/comments.v1.md new file mode 100644 index 0000000..d222f8a --- /dev/null +++ b/contracts/comments.v1.md @@ -0,0 +1,143 @@ +# OpenWiz comments contract, version 1 (`openwiz.comments.v1`) + +Which comments stay in our code, how CI keeps new ones out, and how a repository's comment lines are counted, BEFORE and AFTER, the same way everywhere. The checker is [`tools/comment-check/check.py`](../tools/comment-check/check.py) (standard-library Python, one file); the count report's schema is [comments.v1.schema.json](comments.v1.schema.json); the fixtures are in [`fixtures/comments/`](fixtures/comments/), and `tools/comment-check/test_check.py` proves the checker against them. + +**Contract and checker first** (#1816, CC1 of spec #1815). Each repository's own ticket (CC2, CC6, CC8, CC9, CC11, CC12) carries a byte-identical copy of this file and the checker and wires the diff mode into its CI; all but CC2 also cut that repository's comments, and CC3 to CC5 cut OpenWiz's by area; CC7 gives review-ticket its comment pass; CC10 draws the count report in the war room. + +Authority: spec #1815, from Jake's comment-cut decisions (J238, C1 to C3), 2026-11-19 and 2026-11-20 ([private record](https://github.com/jparker2006/ClosedWiz/issues/3065#issuecomment-7034782715)). + +## Words + +| Word | Meaning here | +| --- | --- | +| **Comment** | Text the language ignores: a line comment, a block comment, a doc comment, or a Python string that stands alone as a statement (a docstring included). Text inside a string literal is never a comment, even when it holds code with comments of its own. | +| **Comment line** | A line that holds any part of a comment, whole or trailing code. | +| **Block** | One trailing comment, or a run of whole-line comments on consecutive lines of one kind (doc or not). A multi-line block comment or docstring is one block. The checker judges blocks, never single lines. | +| **Added line** | A line the pull request adds or changes, from `git diff ...`. | +| **Allowed kind** | A block the checker lets in, below. Every other block is disallowed. | +| **Escape tag** | `comment-ok: `, the one way to keep a block that is none of the allowed kinds. | +| **Tool directive** | A comment a tool reads to run or build the code. It is code, not a comment. | + +## What stays + +Only these kinds. The checker reads each one from its form, so the form is the rule: a block that means one of these but lacks its form fails. + +| Kind | The form the checker reads | Example | +| --- | --- | --- | +| Licence or notice header | `SPDX-License-Identifier:`, `Copyright` or `Licensed under` in a block with no code before it in the file (a shebang or encoding line is not code) | `// SPDX-License-Identifier: MIT` | +| A constraint from outside our code | The block starts `External:` and holds a link to something that isn't ours (not `github.com/openwizdev/` or `github.com/jparker2006/`) | `# External: the vendor API rounds timeouts down, https://example.com/vendor/issues/7` | +| Public API docs a published documentation build renders | A doc comment of its own language (Rust `///`, `//!`, `/** */`, `/*! */`; JavaScript and TypeScript `/** */` only; Python's standalone strings, docstrings included; CSS has none) in a path the repository names with `--docs`. A repository with no published documentation build names none | `/// Returns the answer.` under `--docs crates/sdk/` | +| A security or data-loss warning | The block starts `SECURITY:`, `SAFETY:`, `PRIVACY:` or `DATA LOSS:` and says something after it | `// SAFETY: the buffer outlives every reader.` | +| A private-record pointer | The block holds a pointer in the [open development contract's form](open-development.v1.md#the-pointer) | `# Private record: https://github.com/jparker2006/ClosedWiz/issues/9001#issuecomment-9000000001` | + +The form proves only the shape. Whether the outside constraint is real, the warning true and the docs really published is the reviewer's to judge (CC7's comment pass); a block that wears a form it hasn't earned is a review finding. + +## Meaning first + +Adapted from pstack's `/no-comments` (Lauren Tan, MIT), in our own words: + +1. **When unsure, the comment goes.** Keeping a comment needs a reason from the list above; doubt is not one. +2. **A surprise in our own code is a refactor, not a comment.** Rename it, extract it, give it a type or write the test that pins it, so the code says it without prose. +3. **"Important" and "do not remove" are leads, not proof.** Read the code the comment guards and check the claim before keeping or deleting it. +4. **A constraint comment becomes a check.** If a comment guards a rule (keep this order, never call that first), encode the rule as a type, a test or a CI check, then delete the comment. +5. **A suppressed correctness lint is a bug to fix.** Remove the suppression and fix the cause; a switch survives only for a style rule or a rule that is wrong for this line, with its reason. +6. **Nothing that carried meaning is lost.** A comment that told the reader something true becomes a name, a test or a check before it is deleted, and the cut's pull request lists each one; the check or test is red first. + +Our addition: private-record pointers and privacy warnings stay, so the open development contract keeps working. + +## The escape tag + +For the rare real exception none of the kinds covers, a block starts with the tag and its reason: + +```text +# comment-ok: the synthetic runner reads this line as its job name +``` + +One form: `comment-ok:` at the start of the block's first line, then a reason of at least one non-space character. `comment-ok:` with nothing after it fails as `escape-tag-reason`. The reason is for the reviewer: a tag whose reason doesn't hold up is a review finding. + +## Tool directives are code + +These are neither counted nor checked: + +- A shebang on line 1 (`#!/usr/bin/env python3`), and a Python encoding line on line 1 or 2 (`# -*- coding: utf-8 -*-`). +- A lint or formatter switch **with its reason**: the comment starts with one of `noqa`, `type: ignore`, `pragma:`, `pylint:`, `fmt: off|on|skip`, `ruff:`, `mypy:`, `pyright:`, `isort:`, `eslint`, `eslint-disable`, `eslint-enable`, `prettier-ignore`, `@ts-ignore`, `@ts-expect-error`, `@ts-nocheck`, `biome-ignore`, `shellcheck `, `yamllint `, `stylelint-disable`, `stylelint-enable`, `istanbul ignore`, `c8 ignore` or `v8 ignore`, and gives a reason after ` -- ` or a second ` # `, each followed by a space and then the reason: `# shellcheck disable=SC2086 -- word splitting is wanted here`. The same switch without a reason (or with `#reason` and no space) is a comment and fails as `directive-reason`. Only real comments are switches: a Python string that reads like one is a standalone string, counted and judged as a comment. +- A pinned GitHub Action's version, which update tools read: a line whose only value is a `uses:` field, plain or quoted, `uses: owner/action@<40 hex digits> # v7.0.1`, in a workflow (under `.github/workflows/`) or an action manifest (`action.yml`, `action.yaml`). Elsewhere it is a comment. + +Rust's `#[allow(...)]` and `#![allow(...)]` are attributes, not comments: Clippy's `allow_attributes_without_reason` makes each state its reason (CC2). + +## What counts as a comment + +| Language | Files | Comments | Not comments | +| --- | --- | --- | --- | +| Rust | `.rs` | `//`, `///`, `//!`, `/* */` (nested), `/** */`, `/*! */` | strings, raw strings `r#"..."#`, byte and C strings, char literals, lifetimes, attributes | +| Python | `.py`, `.pyi`, extensionless with a `python` shebang | `#` comments (Python's own tokenizer) and every string that stands alone as a statement (Python's own parser), docstrings included | strings in expressions; a file that doesn't parse is read with the TOML rules | +| Shell | `.sh`, `.bash`, extensionless with a `sh`, `bash`, `dash` or `zsh` shebang | `#` starting a word (an escaped space doesn't end a word) | quoted text, `$#`, `${#x}`, here-documents (`<<`, never a `<<<` here-string), arithmetic `$(( ))`, `(( ))` and `$[ ]` | +| YAML | `.yml`, `.yaml` | `#` at a line's start or after a space, outside quotes; the shell rules inside a `run:` block scalar | other block scalars' content; quoted text, including a quoted value that runs over several lines (a quote opens only where a value starts, never inside a plain value) | +| TOML | `.toml` | `#` outside strings | basic and literal strings; multi-line strings, including a closing run of four or five quotes | +| JavaScript, TypeScript | `.js`, `.mjs`, `.cjs`, `.jsx`, extensionless with a `node` shebang; `.ts`, `.mts`, `.cts`, `.tsx` | `//`, `/* */`, `/** */` | strings, template literals (with `${}` inside), regular expression literals | +| CSS | `.css` | `/* */` | strings | +| HTML | `.html`, `.htm` | `` in text; the CSS rules inside `