From 3759f657f7bdf1fdebc082493a7568f2bf6f1897 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sun, 22 Nov 2026 03:39:46 -0800 Subject: [PATCH] Changed-constant check stops flagging unrelated tests for one- and two-digit numbers (#4683) Changing a small constant such as `MAX_ITEMS = 10` no longer flags every test that mentions its module and holds a `10` somewhere; only lines that use something taken from the module, or name the constant, count. Why: one-line raises of two-digit constants were blocked by dozens of false matches (#4312); on gstack itself, changing `RECEIPT_RUNS_KEPT` (30) flagged 20 test files and now flags 4. Nothing to do. Three-digit and longer values keep the old rule, and a test that imports the module and asserts the old value is still caught. Co-authored-by: Mahmoud ElNeMR <83190847+ElNeMeR70@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p1/red-team#2 reader=red-team --- changelog.d/pregate-literals-short-numbers.md | 1 + docs/pregate.md | 14 +++- lib/pregate/checks/literals.ts | 47 ++++++++++- test/pregate-selection.test.ts | 82 ++++++++++++++----- 4 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 changelog.d/pregate-literals-short-numbers.md diff --git a/changelog.d/pregate-literals-short-numbers.md b/changelog.d/pregate-literals-short-numbers.md new file mode 100644 index 000000000..576f45bdd --- /dev/null +++ b/changelog.d/pregate-literals-short-numbers.md @@ -0,0 +1 @@ +- **The changed-constant check no longer floods on one- and two-digit numbers.** When a constant such as `MAX_ITEMS = 10` changes, a test that only mentions the constant's module counts as a consumer of the old `10` only when that line uses something the test imported from the module (or a variable it got from running it or from such a name), or names the constant. A comment that names the module beside an unrelated `10` no longer blocks the push; on gstack itself, changing `RECEIPT_RUNS_KEPT` (30) flagged 20 test files before and 4 now, and the most any two-digit constant flags fell from 29 files to 13 (#4312). Three-digit and longer values keep the old rule, and negative values such as `-10` follow the same rule as `10`. diff --git a/docs/pregate.md b/docs/pregate.md index 2f57cefe5..bd6c297b1 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -245,7 +245,8 @@ Two stages with separate clocks, both printed: literal (a named literal the branch only adds, such as `limit: 300` in a new object, is not a change), the whole test tree (nightly- and Heavy-only files included) is searched for the old value in files that name the constant or, when the old - value is longer than one character, its module (the module's file name not + value is longer than one character, its module (for a one- or two-digit + integer, only on a line that uses a name taken from the module, below; the module's file name not followed by another extension, or a path ending in its stem followed by its own extension, for a JS/TS module also any JS/TS extension or `.d.ts`, a sentence-ending `.`, or anything but a word @@ -266,7 +267,16 @@ Two stages with separate clocks, both printed: line before ends with `(`, `[`, `{`, `,`, `.`, `=` or another operator, or the line starts with `.`, `)`, `]`, `}` or an operator), never on a comment line, plus the history rows above. A `$2` in a shell stub, a comment, or `export default 2` in a fixture - elsewhere in the file is not a consumer. A call-site name such as `attempts` + elsewhere in the file is not a consumer. + A one- or two-digit integer (`12`, `-10`) found through the module counts only + in a statement that names the constant or a binding the file takes from the + module: a JS/TS import of it (type-only imports excluded) or a variable that a + statement naming the module or another such binding declares or assigns, its + continuation lines included (`const out = run('scripts/arms.ts')`, + `const items = listItems()`). So `expect(shards()).toBe(12)` after importing + `shards` still fails, while a comment naming the module beside an unrelated + `10` does not (#4312). A local helper function that wraps an imported name is + not followed, so its assertions need the constant's name to count. A call-site name such as `attempts` or `limit` is an ordinary word, so for it the file must also name the changed module, whatever the old value's length: a test that only uses the word is not a consumer, and a test that imports the module still is. diff --git a/lib/pregate/checks/literals.ts b/lib/pregate/checks/literals.ts index f4dd3bf22..5eab67786 100644 --- a/lib/pregate/checks/literals.ts +++ b/lib/pregate/checks/literals.ts @@ -97,7 +97,13 @@ function unquote(v: string): string { return /^['"`]/.test(v) ? v.slice(1, -1) : * in a statement that names it: on the name's line, or on a line that * continues it (the line before ends open, `(` `,` `.` `=`, or the line * starts with `.` `)` `]`), comments excluded (#3492: `$2`, "from revision 2", - * `export default 2`). A call-site name (`attempts`, `limit`) is an ordinary + * `export default 2`). A one- or two-digit integer (`12`, `-10`) is nearly as + * common, so through the module it counts only in a statement that names the + * constant or a binding the file takes from the module: an import of it + * (`import { shards } from '…/arms'`, type-only imports excluded) or a variable + * a line naming it declares (`const out = run('…/arms.ts')`). A comment that + * names the module beside an unrelated `10` is not a consumer (#4312), while + * `expect(shards()).toBe(12)` still is (GST-820). A call-site name (`attempts`, `limit`) is an ordinary * word, so for it the file must also name the module whatever the value's * length (gstack-evals#97, #4478). * @@ -128,6 +134,7 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan const moduleRe = new RegExp(`(?:^|[/\\\\])${escape(stem)}(?=[^\\w.-]|$|\\.${dts}(?:${exts})(?![\\w.-])|\\.(?!\\w))|(? /^\s*(?:\/\/|#|\*|\/\*|