From 49fdfc52b4f1f30e9e25ca07fd46169de6cad540 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 23:56:34 -0800 Subject: [PATCH 1/4] ci: spec-trace accepts DOCS, TEST and CI kinds, each checked against its files A PR that changes no behaviour names one of three kinds instead of IMPL, FIX or CHANGE. TEST may touch only tests, testdata and test helpers; CI only .github/ and scripts/; DOCS docs, tests and Go whose hunks change only comments. For DOCS the workflow passes the diff of the non-test Go files, and the check compares removed and added lines with line comments (outside strings) and spacing dropped; every changed Go file must appear in that diff. Ten self-test cases added. Co-Authored-By: Claude Opus 5.5 --- .../.github/pull_request_template.md | 5 + .../.github/scripts/spec-trace-check-test.sh | 43 +++++++++ .../.github/scripts/spec-trace-check.sh | 92 +++++++++++++++++-- .../.github/workflows/spec-trace.yml | 3 +- 4 files changed, 135 insertions(+), 8 deletions(-) diff --git a/seaweed-block/.github/pull_request_template.md b/seaweed-block/.github/pull_request_template.md index bec2170b6d..7ee00d78f0 100644 --- a/seaweed-block/.github/pull_request_template.md +++ b/seaweed-block/.github/pull_request_template.md @@ -14,6 +14,11 @@ Machine/INV starts with exactly one kind: text and rows land with or before the code. A fix without an invariant, or a guard term without a transition, is a CHANGE. +A PR that changes no behaviour uses one of these instead, checked against its files: + DOCS docs, tests, and Go whose hunks change only comments. + TEST only tests, testdata and test helpers. + CI only .github/ and scripts/. + NONE needs a reason of at least 10 characters. Simplest is required when the PR changes non-test Go under core/ or cmd/. --> diff --git a/seaweed-block/.github/scripts/spec-trace-check-test.sh b/seaweed-block/.github/scripts/spec-trace-check-test.sh index 6b6128ff69..270aa690d0 100644 --- a/seaweed-block/.github/scripts/spec-trace-check-test.sh +++ b/seaweed-block/.github/scripts/spec-trace-check-test.sh @@ -80,5 +80,48 @@ expect_raw 2 "status cut off before its NUL" "M\0docs/a.md\0M" expect_raw 2 "unknown status" "R100\0core/a.go\0" expect_raw 0 "well-formed docs change" "M\0docs/a.md\0" +# Kinds for PRs that change no behaviour: each is checked against its paths, +# and DOCS also against the Go diff (only comments may change in non-test Go). +# expect_kind WANT_EXIT NAME KIND GO_DIFF_TEXT [STATUS PATH]... +expect_kind() { + local want=$1 name=$2 k=$3 d=$4 got=0 + shift 4 + printf '%s' "${ok/IMPL M5.Quiesce>Copy/$k} +$simplest" >"$tmp/body" + printf '%s' "$d" >"$tmp/go.diff" + if (($#)); then printf '%s\0' "$@"; fi | bash "$check" "$tmp/body" "$tmp/go.diff" >/dev/null || got=$? + if [[ $got != "$want" ]]; then + echo "FAIL $name: exit $got, want $want" + fail=1 + fi +} +comment_diff='--- a/core/x/s.go ++++ b/core/x/s.go +@@ -1,3 +1,4 @@ +- Name string `json:"name"` ++ Name string `json:"name"` // the volume name ++ // Count is how many. + Count int' +code_diff='--- a/core/x/s.go ++++ b/core/x/s.go +@@ -1,2 +1,2 @@ +- limit := 512 // chunks ++ limit := 256 // chunks' +url_diff='--- a/core/x/s.go ++++ b/core/x/s.go +@@ -1,1 +1,1 @@ +- u := "http://a" ++ u := "http://b"' +expect_kind 0 "DOCS: docs and comment-only Go" DOCS "$comment_diff" M docs/a.md M core/x/s.go A core/x/s_test.go +expect_kind 1 "DOCS: a code change in Go" DOCS "$code_diff" M core/x/s.go +expect_kind 1 "DOCS: a change inside a string that holds //" DOCS "$url_diff" M core/x/s.go +expect_kind 1 "DOCS: Go changed but no diff given" DOCS "" M core/x/s.go +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS "" M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST "" M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST "$comment_diff" M core/x/s.go +expect_kind 0 "CI: workflow and scripts" CI "" M .github/workflows/w.yml A scripts/x.sh +expect_kind 1 "CI: anything else" CI "" M docs/a.md +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" "" M docs/a.md + [[ $fail == 0 ]] && echo "spec-trace self-test: ok" exit $fail diff --git a/seaweed-block/.github/scripts/spec-trace-check.sh b/seaweed-block/.github/scripts/spec-trace-check.sh index dac909ef10..dadb3b145a 100644 --- a/seaweed-block/.github/scripts/spec-trace-check.sh +++ b/seaweed-block/.github/scripts/spec-trace-check.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Usage: spec-trace-check.sh BODY_FILE < changes +# Usage: spec-trace-check.sh BODY_FILE [GO_DIFF] < changes # # Checks the spec trace in a PR description. changes are NUL-separated # STATUS, PATH pairs (post-merge-review-changed.sh). @@ -8,7 +8,11 @@ # optional indentation, a list marker or bold) and carry a value that is not # a template placeholder; "NONE:" needs a reason of at least 10 characters. # - Machine/INV: starts with exactly one PR kind: "IMPL .", -# "FIX " or "CHANGE ". NONE is not a kind. +# "FIX ", "CHANGE ", or one of the kinds a PR that changes +# no behaviour may use, each checked against what it changes: +# DOCS (docs, tests, and non-test Go whose hunks change only comments; GO_DIFF +# is the git diff of the non-test Go files), TEST (only tests, testdata and +# test helpers), CI (only .github/ and scripts/). NONE is not a kind. # - Simplest: (20+ characters) is present when the PR adds, modifies or # deletes non-test Go under core/ or cmd/. # @@ -16,7 +20,8 @@ # usage error or a malformed change list. set -euo pipefail -body_file=${1:?usage: spec-trace-check.sh BODY_FILE < changes} +body_file=${1:?usage: spec-trace-check.sh BODY_FILE [GO_DIFF] < changes} +go_diff=${2-} [[ -f $body_file ]] || { echo "spec-trace: body file $body_file missing" >&2; exit 2; } body=$(tr -d '\r' <"$body_file") @@ -57,21 +62,31 @@ done # One kind token, ending at the line end, a space, a comma or a slash. end='($|[[:space:],/])' -kind_re="^(IMPL [A-Za-z0-9_]+[.][A-Za-z0-9_>.-]+|FIX INV-[A-Z0-9]+(-[A-Z0-9]+)*|CHANGE [A-Za-z0-9_]+)$end" +kind_re="^(IMPL [A-Za-z0-9_]+[.][A-Za-z0-9_>.-]+|FIX INV-[A-Z0-9]+(-[A-Z0-9]+)*|CHANGE [A-Za-z0-9_]+|DOCS|TEST|CI)$end" +kind="" mi=$(value Machine/INV:) if [[ -n $mi && $mi != '<'* ]]; then if [[ ! $mi =~ $kind_re ]]; then - problem "Machine/INV: must start with IMPL ., FIX or CHANGE " - elif [[ ${mi#"${BASH_REMATCH[0]}"} =~ (^|[^A-Za-z0-9_])(IMPL|FIX|CHANGE)([^A-Za-z0-9_]|$) ]]; then - problem "Machine/INV: names more than one kind; a PR is exactly one of IMPL, FIX or CHANGE" + problem "Machine/INV: must start with IMPL ., FIX , CHANGE , DOCS, TEST or CI" + else + token=${BASH_REMATCH[1]} + if [[ ${mi#"${BASH_REMATCH[0]}"} =~ (^|[^A-Za-z0-9_])(IMPL|FIX|CHANGE|DOCS|TEST|CI)([^A-Za-z0-9_]|$) ]]; then + problem "Machine/INV: names more than one kind; a PR is exactly one kind" + else + kind=${token%% *} + fi fi fi +is_test() { [[ $1 == *_test.go || $1 == testdata/* || $1 == */testdata/* || $1 == */testutil/* ]]; } +is_doc() { [[ $1 == *.md || $1 == docs/* ]]; } core=0 +paths=() while IFS= read -r -d '' status; do [[ $status == [ADMT] ]] || { echo "spec-trace: malformed change list: status ${status@Q}" >&2; exit 2; } IFS= read -r -d '' path || { echo "spec-trace: malformed change list: status $status without a path" >&2; exit 2; } [[ -n $path ]] || { echo "spec-trace: malformed change list: empty path" >&2; exit 2; } + paths+=("$path") if [[ $path == core/*.go || $path == cmd/*.go ]] && [[ $path != *_test.go ]]; then core=1 fi @@ -85,4 +100,67 @@ if ((core)); then fi fi +# comment_only: print each non-test Go file in GO_DIFF whose removed and added +# lines differ once line comments (outside strings) and spacing are dropped. +comment_only() { + awk ' + function norm(s, o, i, c, q, b) { + o = ""; q = 0; b = 0 + for (i = 1; i <= length(s); i++) { + c = substr(s, i, 1) + if (!q && !b && c == "/" && substr(s, i + 1, 1) == "/") break + if (q && c == "\\") { o = o c substr(s, i + 1, 1); i++; continue } + if (c == "\"" && !b) q = !q + if (c == "`" && !q) b = !b + o = o c + } + gsub(/[ ]+/, " ", o); sub(/^ /, "", o); sub(/ $/, "", o) + return o + } + /^\+\+\+ / { f = substr($0, 7); next } + /^--- / { next } + /^[-+]/ { + if (f !~ /\.go$/ || f ~ /_test\.go$/) next + n = norm(substr($0, 2)); if (n == "") next + k = f SUBSEP n + if (substr($0, 1, 1) == "+") d[k]++; else d[k]-- + } + END { for (k in d) if (d[k] != 0) { split(k, p, SUBSEP); bad[p[1]] = 1 } for (x in bad) print x } + ' "$1" +} + +case $kind in +CI) + for p in ${paths[@]+"${paths[@]}"}; do + [[ $p == .github/* || $p == scripts/* ]] || problem "kind CI: $p is outside .github/ and scripts/" + done + ;; +TEST) + for p in ${paths[@]+"${paths[@]}"}; do + is_test "$p" || problem "kind TEST: $p is not a test, testdata or test helper" + done + ;; +DOCS) + gofiles=() + for p in ${paths[@]+"${paths[@]}"}; do + if is_doc "$p" || is_test "$p"; then continue; fi + if [[ $p == *.go ]]; then gofiles+=("$p"); continue; fi + problem "kind DOCS: $p is neither docs, a test, nor Go" + done + if ((${#gofiles[@]})); then + if [[ -z $go_diff || ! -f $go_diff ]]; then + problem "kind DOCS: non-test Go changed but no Go diff was given to check it is comment-only" + else + # Every changed Go file must be in the diff, or its hunks went unchecked. + for p in "${gofiles[@]}"; do + grep -qxF -e "+++ b/$p" -e "--- a/$p" "$go_diff" || problem "kind DOCS: $p is not in the Go diff, so its hunks were not checked" + done + while IFS= read -r f; do + [[ -n $f ]] && problem "kind DOCS: $f changes more than comments" + done < <(comment_only "$go_diff") + fi + fi + ;; +esac + exit $bad diff --git a/seaweed-block/.github/workflows/spec-trace.yml b/seaweed-block/.github/workflows/spec-trace.yml index d16c1e6978..93515f75be 100644 --- a/seaweed-block/.github/workflows/spec-trace.yml +++ b/seaweed-block/.github/workflows/spec-trace.yml @@ -47,4 +47,5 @@ jobs: git fetch --no-tags origin "+refs/pull/${PR}/head:refs/remotes/pr/head" printf '%s' "$BODY" >body.txt bash .github/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" >changed.bin - bash .github/scripts/spec-trace-check.sh body.txt go.diff + bash .github/scripts/spec-trace-check.sh body.txt go.diff Date: Sat, 21 Nov 2026 00:16:40 -0800 Subject: [PATCH 2/4] ci: kind DOCS compares Go tokens, not lines; CI covers .github/ only Review fold. The line-multiset comparison let reordered statements, directive comments (//go:build, //go:linkname, the cgo preamble), a // inside a string after a rune quote, a line inside a multi-line raw string and spacing inside a string all pass as comment-only. A small Go program (sametokens.go, go/scanner) now requires the old and new token sequences of each changed non-test Go file to be identical, comments dropped except directives and the cgo preamble; gofmt realignment passes by construction. The workflow passes old/new blobs instead of a diff. DOCS no longer covers docs/spec (the models); CI no longer covers scripts/, which holds shipped install and deploy scripts. Co-Authored-By: Claude Opus 5.5 --- .../.github/pull_request_template.md | 5 +- seaweed-block/.github/scripts/sametokens.go | 120 ++++++++++++++++++ .../.github/scripts/spec-trace-check-test.sh | 65 +++++----- .../.github/scripts/spec-trace-check.sh | 64 +++------- .../.github/workflows/spec-trace.yml | 13 +- 5 files changed, 185 insertions(+), 82 deletions(-) create mode 100644 seaweed-block/.github/scripts/sametokens.go diff --git a/seaweed-block/.github/pull_request_template.md b/seaweed-block/.github/pull_request_template.md index 7ee00d78f0..3584ecacc9 100644 --- a/seaweed-block/.github/pull_request_template.md +++ b/seaweed-block/.github/pull_request_template.md @@ -15,9 +15,10 @@ Machine/INV starts with exactly one kind: A fix without an invariant, or a guard term without a transition, is a CHANGE. A PR that changes no behaviour uses one of these instead, checked against its files: - DOCS docs, tests, and Go whose hunks change only comments. + DOCS docs (not docs/spec), tests, and Go whose tokens are unchanged + (comments only; directive comments count as code). TEST only tests, testdata and test helpers. - CI only .github/ and scripts/. + CI only .github/ (scripts/ ships install and deploy scripts). NONE needs a reason of at least 10 characters. Simplest is required when the PR changes non-test Go under core/ or cmd/. diff --git a/seaweed-block/.github/scripts/sametokens.go b/seaweed-block/.github/scripts/sametokens.go new file mode 100644 index 0000000000..ea85d32406 --- /dev/null +++ b/seaweed-block/.github/scripts/sametokens.go @@ -0,0 +1,120 @@ +// Command sametokens reports whether two versions of a Go source file have the +// same tokens in the same order, ignoring comments that the compiler ignores. +// +// Usage: go run sametokens.go OLD NEW +// +// A missing file counts as empty. Directive comments are compared as tokens: +// //go:..., // +build, //line, /*line, //export, and any comment directly +// above import "C" (the cgo preamble). Exit 0: same tokens; 1: different +// (the first difference is printed); 2: usage error or a file that does not scan. +package main + +import ( + "fmt" + "go/scanner" + "go/token" + "os" + "strings" +) + +type tok struct { + t token.Token + lit string +} + +func main() { + if len(os.Args) != 3 { + fmt.Fprintln(os.Stderr, "usage: sametokens OLD NEW") + os.Exit(2) + } + a, err := tokens(os.Args[1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + b, err := tokens(os.Args[2]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + for i := 0; i < len(a) || i < len(b); i++ { + if i >= len(a) || i >= len(b) || a[i] != b[i] { + fmt.Printf("token %d differs: %v vs %v\n", i, at(a, i), at(b, i)) + os.Exit(1) + } + } +} + +func at(ts []tok, i int) string { + if i >= len(ts) { + return "end of file" + } + return fmt.Sprintf("%s %q", ts[i].t, ts[i].lit) +} + +// tokens scans a file and keeps every token except comments the compiler +// ignores. An automatic semicolon is compared by kind only. +func tokens(path string) ([]tok, error) { + src, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil, nil + } else if err != nil { + return nil, err + } + fset := token.NewFileSet() + file := fset.AddFile(path, -1, len(src)) + var s scanner.Scanner + var scanErr error + s.Init(file, src, func(pos token.Position, msg string) { + if scanErr == nil { + scanErr = fmt.Errorf("%s: %s", pos, msg) + } + }, scanner.ScanComments) + var all []tok + for { + _, t, lit := s.Scan() + if t == token.EOF { + break + } + if t == token.SEMICOLON { + lit = "" + } + all = append(all, tok{t, lit}) + } + if scanErr != nil { + return nil, scanErr + } + var out []tok + for i, x := range all { + if x.t == token.COMMENT && !directive(x.lit) && !abovePreamble(all, i) { + continue + } + out = append(out, x) + } + return out, nil +} + +func directive(c string) bool { + for _, p := range []string{"//go:", "// +build", "//+build", "//line ", "/*line ", "//export "} { + if strings.HasPrefix(c, p) { + return true + } + } + return false +} + +// abovePreamble reports whether the comment at i is followed, past other +// comments and automatic semicolons, by import "C". +func abovePreamble(all []tok, i int) bool { + for j := i + 1; j < len(all); j++ { + switch all[j].t { + case token.COMMENT, token.SEMICOLON: + continue + case token.IMPORT: + return j+1 < len(all) && all[j+1].t == token.STRING && all[j+1].lit == `"C"` + default: + return false + } + } + return false +} diff --git a/seaweed-block/.github/scripts/spec-trace-check-test.sh b/seaweed-block/.github/scripts/spec-trace-check-test.sh index 270aa690d0..76a1cb3c63 100644 --- a/seaweed-block/.github/scripts/spec-trace-check-test.sh +++ b/seaweed-block/.github/scripts/spec-trace-check-test.sh @@ -80,48 +80,45 @@ expect_raw 2 "status cut off before its NUL" "M\0docs/a.md\0M" expect_raw 2 "unknown status" "R100\0core/a.go\0" expect_raw 0 "well-formed docs change" "M\0docs/a.md\0" -# Kinds for PRs that change no behaviour: each is checked against its paths, -# and DOCS also against the Go diff (only comments may change in non-test Go). -# expect_kind WANT_EXIT NAME KIND GO_DIFF_TEXT [STATUS PATH]... + +# Kinds for PRs that change no behaviour: each is checked against its paths; +# DOCS also compares the old and new tokens of each changed non-test Go file. +go build -o "$tmp/sametokens" "$here/sametokens.go" +export SAMETOKENS="$tmp/sametokens" +# expect_kind WANT_EXIT NAME KIND OLD_GO NEW_GO [STATUS PATH]... +# OLD_GO/NEW_GO are core/x/s.go before and after ("-" = no blob for that side). expect_kind() { - local want=$1 name=$2 k=$3 d=$4 got=0 - shift 4 + local want=$1 name=$2 k=$3 old=$4 new=$5 got=0 + shift 5 printf '%s' "${ok/IMPL M5.Quiesce>Copy/$k} $simplest" >"$tmp/body" - printf '%s' "$d" >"$tmp/go.diff" - if (($#)); then printf '%s\0' "$@"; fi | bash "$check" "$tmp/body" "$tmp/go.diff" >/dev/null || got=$? + rm -rf "$tmp/blobs"; mkdir -p "$tmp/blobs/old/core/x" "$tmp/blobs/new/core/x" + [[ $old == - ]] || printf '%s' "$old" >"$tmp/blobs/old/core/x/s.go" + [[ $new == - ]] || printf '%s' "$new" >"$tmp/blobs/new/core/x/s.go" + if (($#)); then printf '%s\0' "$@"; fi | bash "$check" "$tmp/body" "$tmp/blobs" >/dev/null || got=$? if [[ $got != "$want" ]]; then echo "FAIL $name: exit $got, want $want" fail=1 fi } -comment_diff='--- a/core/x/s.go -+++ b/core/x/s.go -@@ -1,3 +1,4 @@ -- Name string `json:"name"` -+ Name string `json:"name"` // the volume name -+ // Count is how many. - Count int' -code_diff='--- a/core/x/s.go -+++ b/core/x/s.go -@@ -1,2 +1,2 @@ -- limit := 512 // chunks -+ limit := 256 // chunks' -url_diff='--- a/core/x/s.go -+++ b/core/x/s.go -@@ -1,1 +1,1 @@ -- u := "http://a" -+ u := "http://b"' -expect_kind 0 "DOCS: docs and comment-only Go" DOCS "$comment_diff" M docs/a.md M core/x/s.go A core/x/s_test.go -expect_kind 1 "DOCS: a code change in Go" DOCS "$code_diff" M core/x/s.go -expect_kind 1 "DOCS: a change inside a string that holds //" DOCS "$url_diff" M core/x/s.go -expect_kind 1 "DOCS: Go changed but no diff given" DOCS "" M core/x/s.go -expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS "" M deploy/x.env -expect_kind 0 "TEST: tests and testdata" TEST "" M core/x/s_test.go A core/x/testdata/in.bin -expect_kind 1 "TEST: production Go" TEST "$comment_diff" M core/x/s.go -expect_kind 0 "CI: workflow and scripts" CI "" M .github/workflows/w.yml A scripts/x.sh -expect_kind 1 "CI: anything else" CI "" M docs/a.md -expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" "" M docs/a.md +P=$'package x\n' +expect_kind 0 "DOCS: comment-only Go" DOCS "${P}var A = 1"$'\n' "${P}"$'// A is one.\nvar A = 1 // one\n' M docs/a.md M core/x/s.go A core/x/s_test.go +expect_kind 0 "DOCS: gofmt realignment" DOCS "${P}var A = 1"$'\n' "${P}var A = 1"$'\n' M core/x/s.go +expect_kind 0 "DOCS: block comment" DOCS "${P}var A = 1"$'\n' "${P}"$'/* doc */\nvar A = 1\n' M core/x/s.go +expect_kind 1 "DOCS: reorder" DOCS "${P}func f() { u(); p() }"$'\n' "${P}func f() { p(); u() }"$'\n' M core/x/s.go +expect_kind 1 "DOCS: build constraint comment" DOCS "$P" $'//go:build never\n\npackage x\n' M core/x/s.go +expect_kind 1 "DOCS: rune quote then // in a string" DOCS "${P}var _ = f('\"', \"http://a\")"$'\n' "${P}var _ = f('\"', \"http://b\")"$'\n' M core/x/s.go +expect_kind 1 "DOCS: line inside a raw string" DOCS "${P}var s = \`a"$'\n'"curl http://old | sh"$'\n'"\`"$'\n' "${P}var s = \`a"$'\n'"curl http://new | sh"$'\n'"\`"$'\n' M core/x/s.go +expect_kind 1 "DOCS: spaces inside a string" DOCS "${P}var s = \"a b\""$'\n' "${P}var s = \"a b\""$'\n' M core/x/s.go +expect_kind 1 "DOCS: cgo preamble" DOCS "${P}// #include "$'\nimport "C"\n' "${P}// #include "$'\nimport "C"\n' M core/x/s.go +expect_kind 1 "DOCS: Go changed, no blob" DOCS - - M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md [[ $fail == 0 ]] && echo "spec-trace self-test: ok" exit $fail diff --git a/seaweed-block/.github/scripts/spec-trace-check.sh b/seaweed-block/.github/scripts/spec-trace-check.sh index dadb3b145a..7c217792bc 100644 --- a/seaweed-block/.github/scripts/spec-trace-check.sh +++ b/seaweed-block/.github/scripts/spec-trace-check.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Usage: spec-trace-check.sh BODY_FILE [GO_DIFF] < changes +# Usage: spec-trace-check.sh BODY_FILE [BLOBS] < changes # # Checks the spec trace in a PR description. changes are NUL-separated # STATUS, PATH pairs (post-merge-review-changed.sh). @@ -10,9 +10,10 @@ # - Machine/INV: starts with exactly one PR kind: "IMPL .", # "FIX ", "CHANGE ", or one of the kinds a PR that changes # no behaviour may use, each checked against what it changes: -# DOCS (docs, tests, and non-test Go whose hunks change only comments; GO_DIFF -# is the git diff of the non-test Go files), TEST (only tests, testdata and -# test helpers), CI (only .github/ and scripts/). NONE is not a kind. +# DOCS (docs outside docs/spec/, tests, and non-test Go whose tokens are +# unchanged: BLOBS holds old/ and new/ for each changed non-test +# Go file, compared by sametokens.go, run as $SAMETOKENS if set), TEST (only +# tests, testdata and test helpers), CI (only .github/). NONE is not a kind. # - Simplest: (20+ characters) is present when the PR adds, modifies or # deletes non-test Go under core/ or cmd/. # @@ -20,8 +21,9 @@ # usage error or a malformed change list. set -euo pipefail -body_file=${1:?usage: spec-trace-check.sh BODY_FILE [GO_DIFF] < changes} -go_diff=${2-} +body_file=${1:?usage: spec-trace-check.sh BODY_FILE [BLOBS] < changes} +blobs=${2-} +here=$(cd "$(dirname "$0")" && pwd) [[ -f $body_file ]] || { echo "spec-trace: body file $body_file missing" >&2; exit 2; } body=$(tr -d '\r' <"$body_file") @@ -79,7 +81,8 @@ if [[ -n $mi && $mi != '<'* ]]; then fi is_test() { [[ $1 == *_test.go || $1 == testdata/* || $1 == */testdata/* || $1 == */testutil/* ]]; } -is_doc() { [[ $1 == *.md || $1 == docs/* ]]; } +# docs/spec holds the models: changing one is not a docs change. +is_doc() { [[ $1 != docs/spec/* ]] && [[ $1 == *.md || $1 == docs/* ]]; } core=0 paths=() while IFS= read -r -d '' status; do @@ -100,39 +103,10 @@ if ((core)); then fi fi -# comment_only: print each non-test Go file in GO_DIFF whose removed and added -# lines differ once line comments (outside strings) and spacing are dropped. -comment_only() { - awk ' - function norm(s, o, i, c, q, b) { - o = ""; q = 0; b = 0 - for (i = 1; i <= length(s); i++) { - c = substr(s, i, 1) - if (!q && !b && c == "/" && substr(s, i + 1, 1) == "/") break - if (q && c == "\\") { o = o c substr(s, i + 1, 1); i++; continue } - if (c == "\"" && !b) q = !q - if (c == "`" && !q) b = !b - o = o c - } - gsub(/[ ]+/, " ", o); sub(/^ /, "", o); sub(/ $/, "", o) - return o - } - /^\+\+\+ / { f = substr($0, 7); next } - /^--- / { next } - /^[-+]/ { - if (f !~ /\.go$/ || f ~ /_test\.go$/) next - n = norm(substr($0, 2)); if (n == "") next - k = f SUBSEP n - if (substr($0, 1, 1) == "+") d[k]++; else d[k]-- - } - END { for (k in d) if (d[k] != 0) { split(k, p, SUBSEP); bad[p[1]] = 1 } for (x in bad) print x } - ' "$1" -} - case $kind in CI) for p in ${paths[@]+"${paths[@]}"}; do - [[ $p == .github/* || $p == scripts/* ]] || problem "kind CI: $p is outside .github/ and scripts/" + [[ $p == .github/* ]] || problem "kind CI: $p is outside .github/ (scripts/ holds shipped install and deploy scripts)" done ;; TEST) @@ -148,16 +122,18 @@ DOCS) problem "kind DOCS: $p is neither docs, a test, nor Go" done if ((${#gofiles[@]})); then - if [[ -z $go_diff || ! -f $go_diff ]]; then - problem "kind DOCS: non-test Go changed but no Go diff was given to check it is comment-only" + if [[ -z $blobs || ! -d $blobs ]]; then + problem "kind DOCS: non-test Go changed but no old/new blobs were given to compare" else - # Every changed Go file must be in the diff, or its hunks went unchecked. + st=(go run "$here/sametokens.go") + [[ -z ${SAMETOKENS-} ]] || st=("$SAMETOKENS") for p in "${gofiles[@]}"; do - grep -qxF -e "+++ b/$p" -e "--- a/$p" "$go_diff" || problem "kind DOCS: $p is not in the Go diff, so its hunks were not checked" + if [[ ! -e $blobs/old/$p && ! -e $blobs/new/$p ]]; then + problem "kind DOCS: $p has no old or new blob, so it was not compared" + elif ! "${st[@]}" "$blobs/old/$p" "$blobs/new/$p" >/dev/null; then + problem "kind DOCS: $p changes more than comments (its Go tokens differ)" + fi done - while IFS= read -r f; do - [[ -n $f ]] && problem "kind DOCS: $f changes more than comments" - done < <(comment_only "$go_diff") fi fi ;; diff --git a/seaweed-block/.github/workflows/spec-trace.yml b/seaweed-block/.github/workflows/spec-trace.yml index 93515f75be..983aff63c2 100644 --- a/seaweed-block/.github/workflows/spec-trace.yml +++ b/seaweed-block/.github/workflows/spec-trace.yml @@ -47,5 +47,14 @@ jobs: git fetch --no-tags origin "+refs/pull/${PR}/head:refs/remotes/pr/head" printf '%s' "$BODY" >body.txt bash .github/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" >changed.bin - git diff --no-renames "$BASE...$HEAD" -- "*.go" ":(exclude)*_test.go" >go.diff - bash .github/scripts/spec-trace-check.sh body.txt go.diff "blobs/old/$p" 2>/dev/null || rm -f "blobs/old/$p" + git show "$HEAD:$p" >"blobs/new/$p" 2>/dev/null || rm -f "blobs/new/$p" + done + mkdir -p blobs + bash .github/scripts/spec-trace-check.sh body.txt blobs Date: Sat, 21 Nov 2026 00:25:13 -0800 Subject: [PATCH 3/4] ci: build sametokens once and tell a failed check from differing tokens Review fold. The check ran the tool with go run, which takes the .go arguments as more source files, so in CI every DOCS PR touching Go would have been refused; the self-test hid it by building the tool itself. The check now builds the tool into a temporary directory, the self-test runs that same path, and a build or tool failure exits 2 (the check did not run) instead of reporting changed tokens. In a file that uses cgo every comment counts (the grouped import ("C") form dropped the preamble). The template says DOCS may still change contract text, with the reviewer as the gate; the script header names the go:embed limit. Co-Authored-By: Claude Opus 5.5 --- .../.github/pull_request_template.md | 1 + seaweed-block/.github/scripts/sametokens.go | 31 +++++++---------- .../.github/scripts/spec-trace-check-test.sh | 33 +++++++++++++++++-- .../.github/scripts/spec-trace-check.sh | 28 ++++++++++++---- 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/seaweed-block/.github/pull_request_template.md b/seaweed-block/.github/pull_request_template.md index 3584ecacc9..25ad685875 100644 --- a/seaweed-block/.github/pull_request_template.md +++ b/seaweed-block/.github/pull_request_template.md @@ -19,6 +19,7 @@ A PR that changes no behaviour uses one of these instead, checked against its fi (comments only; directive comments count as code). TEST only tests, testdata and test helpers. CI only .github/ (scripts/ ships install and deploy scripts). +DOCS may still change contract text (docs/design/invariants.md): the reviewer is the gate for that. NONE needs a reason of at least 10 characters. Simplest is required when the PR changes non-test Go under core/ or cmd/. diff --git a/seaweed-block/.github/scripts/sametokens.go b/seaweed-block/.github/scripts/sametokens.go index ea85d32406..c167f2429d 100644 --- a/seaweed-block/.github/scripts/sametokens.go +++ b/seaweed-block/.github/scripts/sametokens.go @@ -4,8 +4,9 @@ // Usage: go run sametokens.go OLD NEW // // A missing file counts as empty. Directive comments are compared as tokens: -// //go:..., // +build, //line, /*line, //export, and any comment directly -// above import "C" (the cgo preamble). Exit 0: same tokens; 1: different +// //go:..., // +build, //line, /*line, //export; in a file that uses cgo +// (any "C" string literal, as in import "C"), every comment counts, since the +// preamble is code. Exit 0: same tokens; 1: different // (the first difference is printed); 2: usage error or a file that does not scan. package main @@ -84,9 +85,15 @@ func tokens(path string) ([]tok, error) { if scanErr != nil { return nil, scanErr } + cgo := false + for _, x := range all { + if x.t == token.STRING && x.lit == "\"C\"" { + cgo = true + } + } var out []tok - for i, x := range all { - if x.t == token.COMMENT && !directive(x.lit) && !abovePreamble(all, i) { + for _, x := range all { + if x.t == token.COMMENT && !cgo && !directive(x.lit) { continue } out = append(out, x) @@ -102,19 +109,3 @@ func directive(c string) bool { } return false } - -// abovePreamble reports whether the comment at i is followed, past other -// comments and automatic semicolons, by import "C". -func abovePreamble(all []tok, i int) bool { - for j := i + 1; j < len(all); j++ { - switch all[j].t { - case token.COMMENT, token.SEMICOLON: - continue - case token.IMPORT: - return j+1 < len(all) && all[j+1].t == token.STRING && all[j+1].lit == `"C"` - default: - return false - } - } - return false -} diff --git a/seaweed-block/.github/scripts/spec-trace-check-test.sh b/seaweed-block/.github/scripts/spec-trace-check-test.sh index 76a1cb3c63..c5379d0ff5 100644 --- a/seaweed-block/.github/scripts/spec-trace-check-test.sh +++ b/seaweed-block/.github/scripts/spec-trace-check-test.sh @@ -83,8 +83,6 @@ expect_raw 0 "well-formed docs change" "M\0docs/a.md\0" # Kinds for PRs that change no behaviour: each is checked against its paths; # DOCS also compares the old and new tokens of each changed non-test Go file. -go build -o "$tmp/sametokens" "$here/sametokens.go" -export SAMETOKENS="$tmp/sametokens" # expect_kind WANT_EXIT NAME KIND OLD_GO NEW_GO [STATUS PATH]... # OLD_GO/NEW_GO are core/x/s.go before and after ("-" = no blob for that side). expect_kind() { @@ -111,6 +109,7 @@ expect_kind 1 "DOCS: rune quote then // in a string" DOCS "${P}var _ = f('\"', \ expect_kind 1 "DOCS: line inside a raw string" DOCS "${P}var s = \`a"$'\n'"curl http://old | sh"$'\n'"\`"$'\n' "${P}var s = \`a"$'\n'"curl http://new | sh"$'\n'"\`"$'\n' M core/x/s.go expect_kind 1 "DOCS: spaces inside a string" DOCS "${P}var s = \"a b\""$'\n' "${P}var s = \"a b\""$'\n' M core/x/s.go expect_kind 1 "DOCS: cgo preamble" DOCS "${P}// #include "$'\nimport "C"\n' "${P}// #include "$'\nimport "C"\n' M core/x/s.go +expect_kind 1 "DOCS: cgo preamble, grouped import" DOCS "${P}// #include "$'\nimport (\n\t"C"\n)\n' "${P}// #include "$'\nimport (\n\t"C"\n)\n' M core/x/s.go expect_kind 1 "DOCS: Go changed, no blob" DOCS - - M core/x/s.go expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env @@ -122,3 +121,33 @@ expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md [[ $fail == 0 ]] && echo "spec-trace self-test: ok" exit $fail + +import ( + "C" +) +' "${P}// #include " DOCS - - M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md + +[[ $fail == 0 ]] && echo "spec-trace self-test: ok" +exit $fail + +import ( + "C" +) +' M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md + +[[ $fail == 0 ]] && echo "spec-trace self-test: ok" +exit $fail diff --git a/seaweed-block/.github/scripts/spec-trace-check.sh b/seaweed-block/.github/scripts/spec-trace-check.sh index 7c217792bc..0021fa1a37 100644 --- a/seaweed-block/.github/scripts/spec-trace-check.sh +++ b/seaweed-block/.github/scripts/spec-trace-check.sh @@ -12,8 +12,9 @@ # no behaviour may use, each checked against what it changes: # DOCS (docs outside docs/spec/, tests, and non-test Go whose tokens are # unchanged: BLOBS holds old/ and new/ for each changed non-test -# Go file, compared by sametokens.go, run as $SAMETOKENS if set), TEST (only -# tests, testdata and test helpers), CI (only .github/). NONE is not a kind. +# Go file, compared by sametokens.go), TEST (only tests, testdata and test +# helpers), CI (only .github/). NONE is not a kind. Known limit: a docs file +# that is go:embed-ed into a binary changes behaviour under DOCS (none today). # - Simplest: (20+ characters) is present when the PR adds, modifies or # deletes non-test Go under core/ or cmd/. # @@ -125,14 +126,29 @@ DOCS) if [[ -z $blobs || ! -d $blobs ]]; then problem "kind DOCS: non-test Go changed but no old/new blobs were given to compare" else - st=(go run "$here/sametokens.go") - [[ -z ${SAMETOKENS-} ]] || st=("$SAMETOKENS") + # Built here, not run with "go run": that would take the .go + # arguments as more source files of the program. + bin=$(mktemp -d) + trap 'rm -rf "$bin"' EXIT + if ! go build -o "$bin/sametokens" "$here/sametokens.go"; then + echo "spec-trace: could not build sametokens.go; the DOCS check did not run" >&2 + exit 2 + fi for p in "${gofiles[@]}"; do if [[ ! -e $blobs/old/$p && ! -e $blobs/new/$p ]]; then problem "kind DOCS: $p has no old or new blob, so it was not compared" - elif ! "${st[@]}" "$blobs/old/$p" "$blobs/new/$p" >/dev/null; then - problem "kind DOCS: $p changes more than comments (its Go tokens differ)" + continue fi + rc=0 + "$bin/sametokens" "$blobs/old/$p" "$blobs/new/$p" >/dev/null || rc=$? + case $rc in + 0) ;; + 1) problem "kind DOCS: $p changes more than comments (its Go tokens differ)" ;; + *) + echo "spec-trace: sametokens failed on $p (exit $rc); the DOCS check did not run" >&2 + exit 2 + ;; + esac done fi fi From c87f464e2983820c5a3fbab2311b0449d6aac366 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 00:46:22 -0800 Subject: [PATCH 4/4] ci: spec-trace checks out the base branch tip, not the PR's recorded base On pull_request_target, base.sha is the base recorded when the PR was last synchronised, so a PR opened before the spec-trace scripts landed checked out a tree without them and failed with 'No such file or directory'. github.sha is the base branch tip at the event; the diff still uses the merge base of the PR's base and head. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/.github/workflows/spec-trace.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/seaweed-block/.github/workflows/spec-trace.yml b/seaweed-block/.github/workflows/spec-trace.yml index 983aff63c2..2e09b1c3cb 100644 --- a/seaweed-block/.github/workflows/spec-trace.yml +++ b/seaweed-block/.github/workflows/spec-trace.yml @@ -31,7 +31,9 @@ jobs: - name: Checkout base uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: - ref: ${{ github.event.pull_request.base.sha }} + # github.sha is the tip of the base branch when the event fires; the + # PR's base.sha can predate this workflow's own scripts. + ref: ${{ github.sha }} fetch-depth: 0 persist-credentials: false