Skip to content

Commit e9b44ab

Browse files
fix(pm): dispatch-gates derives the gates that load a changed scripts/pm module, transitively (#34080)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9MG4BPhsDcJCksmNwyRu6xu)_ Fixes #33848 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:派发工具 `dispatch-gates.mjs` 新增「import 图」匹配键:改动落在 `scripts/pm/**` 下的某个模块时,所有直接或间接 import(含动态 import、再导出)它的门禁脚本所属的门禁族都会被派生出来,输出里打印从门禁脚本到该模块的整条 import 链。 - 为什么改:PR #33804 第一版只改了 `check-governed-queue-guard.mjs` 和 `check-governed-merges.mjs`,`--commands` 派生 35 条、漏了 `check:pm-fleet-write-execute`(它的脚本 import 了被改的函数),开发者「35 条全绿」而 CI 红。现在同一组文件派生 62 条,含 fleet-write 的执行器、校验器和派发器。 - 风险与回滚:只改 `scripts/pm/**` 下一个工具脚本和它的自测,不发布任何包。新键排在所有旧键之后,旧匹配的键与来源标签一字不变,只会多派、不会少派;代价是改大多数 pm 模块会多派约 40 多个门禁族(pm 工具互相 import 成一个大环,这是实测的真实影响面)。回滚即还原本 PR。 - 先看:`scripts/pm/dispatch-gates.mjs` 的 `loadEdgeTargets`(读 import 边,含动态 import 的兜底)与 `coveringKey` 末尾的新分支;自测文件里 `The import-graph key (#33848)` 一节和改过的两条旧用例。 - 两分钟验证:`node scripts/pm/dispatch-gates.mjs --commands scripts/pm/check-governed-queue-guard.mjs scripts/pm/check-governed-merges.mjs | grep fleet-write`,预期三行 `pnpm check:pm-fleet-write-{dispatch,execute,validate}`(修复前零行)。 - 需要你做的:无,席位落地(非受管面)。 ## What changes `dispatch-gates --commands` named a gate when a changed path was the gate's own script, a CI trigger, a job filter, a literal the gate spells, or a file it reads. It never named a gate because its script **imports** the changed module: `coveringKey`'s docblock refused that key (#13126) on precision, because the shared heads under `scripts/` (`invoked-as.mjs` and four others) are imported by most families. The pm tools are the opposite shape, and the measured miss is the card's: on PR #33804's first head the derivation named 35 gates, not `check:pm-fleet-write-execute`, whose script imports a changed function and whose self-test then went red in CI. A new key, consulted **last** in `coveringKey`, answers a changed path under `IMPORT_GRAPH_KEY_ROOT` (`scripts/pm/`): a family matches when the path is in the transitive load closure of its gate files. Read from the module's side that is the reverse import graph over `scripts/pm/**`, at any depth. The row prints the chain, e.g. `import graph scripts/pm/fleet-write/execute.mjs -> scripts/pm/check-governed-queue-guard.mjs`. Outside `scripts/pm/` the #13126 refusal stands unchanged (a self-test row pins that). | Piece | What it does | |:--|:--| | `loadEdgeTargets(path, source)` | One file's first-party load edges: static `import … from`, `export … from` (a re-export loads its target), the side-effect form, a dynamic `import()` of a string literal or of `new URL(literal, import.meta.url)`. Comments are masked; a keyword inside a string or template literal is fixture text, not a load. Self-test bodies are **not** masked: a `--self-test` run loads them. | | Unreadable dynamic `import()` | The argument is a variable, a property or a computed URL. It is not dropped: every code-position module literal in that file becomes an edge, whether relative or spelled from the repo root under `scripts/` (toward deriving more). Both shapes are live: a loader seam (`load = (specifier) => import(specifier)`) with its relative specifiers in a constant (`ARM_READER_SOURCE`), and a root-spelled roster loaded in process (`fleet-write/dispatch.mjs`'s self-test imports each sender through `pathToFileURL(join(REPO_ROOT, sender))`; `check-dispatch-gates.mjs` loads its engine through `import(resolve(ROOT, TOOL))`). The root spelling also takes fixture rows and text reads written the same way (five modules `check-governed-merges.mjs` names as data); that over-derives, which is this key's direction. | | `importGraphReach(files, edgesOf)` | Breadth-first forward closure from a family's gate files, recording each module's first importer, so the printed chain is a shortest one. Walks self-test families and gate files too: the key claims what RUNNING the family loads, never what it watches, so neither population narrowing applies. | | `discoverFamilies` | Computes `entry.importReach` for every family, one scan per file per discovery. | | `derivationInputs` | Adds every reached module, since its edges decide what the key names. | Resolution is the existing first-party rule: relative specifiers only, inside `scripts/`, outside `node_modules`, the target must exist. A bare specifier is an installed package. Measured on this tree (b8685e5), and printed by a self-test row on every run rather than recorded in prose: the key reaches 28 pm modules over 1195 (family, module) pairs, and the most-reached module, `clause2-line.mjs`, names 53 families. The pm tools close into one large cycle (the governed register, the half-state patrol, the queue guard and the dispatch-gates engine load one another), so an edit to most pm modules names forty-odd families. That is the measured size of what such an edit can break. Time, measured on a quiet container, two cold runs each: a discovery took 16.6 s and 14.8 s before the key (base e71ecc1) and 17.2 s and 17.0 s with it (6f34ab1); a whole `--commands` run over one pm path took 36.0 s and 37.0 s before and 38.0 s and 35.7 s after. The review's 48.7 s was the same run on a loaded box. The stale "~14 s cold" in `derivationInputs`' docblock now carries these readings. ## Tests Full battery (`node scripts/pm/dispatch-gates.mjs --self-test`, both tiers) on b8685e5: **2530 cases pass** (fast 2215 + slow 315), lock `VERDICT command-exit 0`. Round 1: 2528 passed on 80bc14b and on e6452d3. New rows, all in the fast tier: - Fixture graph (in memory, so the shapes cannot rot with the tree): the reader takes the static import, the re-export, the side-effect form, a literal `import()` inside a self-test body and a `new URL` import; it refuses an import spelled in a template literal or a comment and a bare package; an unread `import()` widens to every module literal in the file, relative or root-spelled; CONTROL: the same two literals in a file with no unread `import()` are no edges. - ⭐ Transitive row: in the fixture, `b.mjs` reached only through `gate.mjs -> a.mjs -> b.mjs` names the family, and the row prints that chain. - Scope row: a module outside `scripts/pm/` reached by import is still answered by no key. - ⭐ Root-spelled roster row (live tree, review M1): `scripts/pm/issue-create.mjs`, which no file under `scripts/` imports statically and which the dispatcher's self-test loads by root path, derives `check:pm-fleet-write-dispatch` through the import graph. - Price row: prints the modules the key reaches, the (family, module) pairs and the most-reached module, asserting only that the key reaches anything. - ⭐ Replay row (live tree): PR #33804's first file list places `check:pm-fleet-write-execute` as matched via `import graph scripts/pm/fleet-write/execute.mjs -> …`, rendered `pnpm check:pm-fleet-write-execute`; and `check:pm-fleet-write-validate` and `check:pm-fleet-write-dispatch` match too. Forced edits to existing rows (both are about the key order this PR extends): 1. **The #13126 witness** (`the witness holds — bare-root-worklist --self-test imports THIS file, and no key names that family…`) asserted the refusal's one known missing lead. That module is under `scripts/pm/`, so the new key names it; the row now asserts the edge exists AND the key that names it is the import-graph key (`the witness is paid back — …`). The other five refusal pins (novel class, worst head, concentration, the deferred-pair pins) are unchanged and green. 2. **Read-key additivity** (`every read edge earns a key, and none is re-attributed …`) stripped `entry.reads` and compared against the keys left. The new key now sits after the read key, so with the reads stripped it answered those targets and the row reported 7 false re-attributions (`program text read by …` -> `import graph …`). The row now strips `entry.importReach` too, so it compares the read key against the keys consulted BEFORE it, which is its claim. <details><summary>Ablation: the new branch in coveringKey disabled on disk, exactly the four key rows red, restored (head e6452d3)</summary> Through `node scripts/ablation-replace.mjs` (anchor hits 1 -> 0 on disk, blob `7d65b7fe4962` -> `09197c9f6da0`), the fast tier under `os-verify-lock.sh`; restore: blob == HEAD (`7d65b7fe4962`) and `git diff HEAD` empty. ```text mutation: the import-graph branch's condition prefixed with `false &&` -> VERDICT command-exit 1, 4 of 2213 failed: the witness is paid back — bare-root-worklist --self-test imports THIS file, and the import-graph key names that family for a card editing it the transitive row: a module reached only through an importer of an importer names the family, and the row prints the chain from the gate file the replay row: PR #33804's first file list derives check:pm-fleet-write-execute, through the import graph …and the validator and the dispatcher, the other two fleet-write families the card names (the edge-reader rows and the scope row stay green, as they must: they do not read the key) ``` </details> <details><summary>Ablation, round 2 (review M1): the root-spelled half of the fallback disabled on disk, exactly two rows red, restored (head b8685e5)</summary> Through `node scripts/ablation-replace.mjs` (anchor hits 1 -> 0, blob `8e00e6752a69` -> `fcd7324b7a64`), the fast tier under `os-verify-lock.sh`; restore: blob == HEAD (`8e00e6752a69`) and `git diff HEAD` empty. ```text mutation: a root-spelled literal in a file with an unreadable import() is skipped instead of admitted -> VERDICT command-exit 1, 2 of 2215 failed: an import() whose argument is not a literal widens toward MORE: every module literal in the file is an edge, relative or spelled from the repo root a sender the dispatcher loads by a root-spelled path derives check:pm-fleet-write-dispatch, through the import graph ``` </details> ## Acceptance notes - Additive by construction: the key is the last one `coveringKey` consults, so every (family, path) pair an earlier key answered keeps its key and label; the 2,520 rows that existed before stay green except the two forced edits above. - Price, stated rather than hidden: a card editing most `scripts/pm/` modules now derives about 62 commands where it derived about 30 (this PR's own card: 62). Every added row prints its import chain, so a reader can check why it is there. - Not covered by this key: the import closure of a script a gate only SPAWNS as a child (`node scripts/pm/x.mjs`) is not walked; the existing run and read keys answer the spawned file itself where they apply. carrier: #34084. - The related evidence the delta names (`check:adr-anchors` reddened by a fixture string on PR #33975) is a different mechanism: that family declares five hints (`docs/adr`, `scripts/adr-anchors`, its own script and two `CHANGELOG.md` files) and no whole-tree marker, while its scan reads the whole tree's text. Its population is under-declared; not fixed here. carrier: #34086. - Serial: #34002 (dotAll in `triggerPatternRegex`, same file) is held and not folded in. ## Verification Local subset on b8685e5 (round 2; main e71ecc1 merged in): `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` derived 62 commands; 61 exit 0 and one exit 3 (each exit captured before any pipe). `--ran` with every line annotated `:: exit N`: **62 derived, 61 run, 1 NOT-MEASURED (derived from the recorded exit 3), 0 UNRUN**. `pnpm check:i18n-coverage` is derived only through the import graph (its self-test imports the engine): its self-test half passed, its scan half refused with exit 3 because the CLI build closure is not built here; declared to CI. No package touched, so no build closure and no package test is owed. <details><summary>The 62 commands and their exit codes</summary> ```text node scripts/check-closing-keyword-parity.mjs :: exit 0 node scripts/check-closing-keyword-parity.mjs --self-test :: exit 0 node scripts/check-comment-mask-corpus.mjs :: exit 0 node scripts/check-declaration-mirrors.mjs :: exit 0 node scripts/check-declaration-mirrors.mjs --self-test :: exit 0 node scripts/check-issue-citations.mjs :: exit 0 node scripts/check-self-test-wired.mjs :: exit 0 node scripts/check-self-test-wired.mjs --self-test :: exit 0 node scripts/check-self-test-workflow-commands.mjs :: exit 0 node scripts/check-self-test-workflow-commands.mjs --self-test :: exit 0 node scripts/check-skills-token-ratchet.mjs :: exit 0 node scripts/check-skills-token-ratchet.mjs --self-test :: exit 0 node scripts/check-whole-set-label-write.mjs :: exit 0 node scripts/check-whole-set-label-write.mjs --self-test :: exit 0 node scripts/ci/plan-test-core-shards.mjs --self-test :: exit 0 node scripts/ci/scheduled-full-run-card.mjs --self-test :: exit 0 node scripts/ci/scheduled-full-run.mjs --self-test :: exit 0 node scripts/ci/snapshot-next.mjs --self-test :: exit 0 node scripts/pm/bare-root-worklist.mjs --self-test :: exit 0 node scripts/pm/board-snapshot.mjs --self-test :: exit 0 node scripts/pm/check-governed-queue-guard.mjs --self-test :: exit 0 node scripts/pm/sweep-closed-cards.mjs --self-test :: exit 0 pnpm check:agent-test-spelling :: exit 0 pnpm check:bash32-floor :: exit 0 pnpm check:ci-filter-parity :: exit 0 pnpm check:cli-command-ids :: exit 0 pnpm check:closing-target-claim :: exit 0 pnpm check:commit-card-trailers :: exit 0 pnpm check:cross-package-test-inputs :: exit 0 pnpm check:declared-population-live :: exit 0 pnpm check:driver-memory-census :: exit 0 pnpm check:entry-guard :: exit 0 pnpm check:gitlink-declared :: exit 0 pnpm check:i18n-coverage :: exit 3 pnpm check:issue-citations :: exit 0 pnpm check:nul-bytes :: exit 0 pnpm check:parse-guard :: exit 0 pnpm check:partof-closing-keyword :: exit 0 pnpm check:pm-board-snapshot :: exit 0 pnpm check:pm-close-cards :: exit 0 pnpm check:pm-dispatch-gates :: exit 0 pnpm check:pm-expected-skips :: exit 0 pnpm check:pm-fleet-token :: exit 0 pnpm check:pm-fleet-write-dispatch :: exit 0 pnpm check:pm-fleet-write-execute :: exit 0 pnpm check:pm-fleet-write-validate :: exit 0 pnpm check:pm-governed-merges :: exit 0 pnpm check:pm-governed-prose :: exit 0 pnpm check:pm-half-states :: exit 0 pnpm check:pm-issue-create :: exit 0 pnpm check:pm-issue-transfer :: exit 0 pnpm check:pm-label-write :: exit 0 pnpm check:pm-post-stamped :: exit 0 pnpm check:pm-prior-rulings :: exit 0 pnpm check:pm-record-recognisers :: exit 0 pnpm check:pnpm-filter-targets :: exit 0 pnpm check:pnpm-flag-spellings :: exit 0 pnpm check:ratchet-remedy-authority :: exit 0 pnpm check:refd-timer-probe :: exit 0 pnpm check:scripts-symbol-anchors :: exit 0 pnpm check:single-claim-paths :: exit 0 pnpm check:watch-hint-literal :: exit 0 ``` </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmRtrFmEsp5pAjTBpMT)_ <!-- signed: domain:skills#1 · os-steve · session_01ADHkmPd7uRqHauEehGPr19 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8d0035d commit e9b44ab

2 files changed

Lines changed: 395 additions & 10 deletions

File tree

‎scripts/pm/dispatch-gates.mjs‎

Lines changed: 237 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6572,6 +6572,174 @@ export function firstPartyImportTargets(scriptPath, source, { root = ROOT } = {}
65726572
return [...firstPartyImportBindings(scriptPath, source, { root }).keys()];
65736573
}
65746574

6575+
/**
6576+
* ── The LOAD edge, read for the import-graph key (#33848) ──────────────────
6577+
*
6578+
* `firstPartyImportBindings` answers a POPULATION question — what a gate
6579+
* inherits from a module it imports — and every one of its narrowings is
6580+
* priced for that question: static clauses only, self-test bodies masked.
6581+
* This answers a different one: *which first-party modules does running this
6582+
* file LOAD?* — the question `importGraphReach` walks to name the family that
6583+
* runs a changed `scripts/pm/**` module. A missed edge here is a gate a card
6584+
* can break without being told, so every narrowing above is turned the other
6585+
* way, toward MORE edges:
6586+
*
6587+
* - self-test bodies are NOT masked: a `--self-test` invocation runs them, and
6588+
* a self-test that loads its battery lazily loads it there.
6589+
* - comments ARE masked, and a keyword inside a string or template literal is
6590+
* not a load: a fixture spelling an import is text this file carries.
6591+
* - static `import … from`, `export … from` (a re-export loads its target)
6592+
* and the side-effect form; a dynamic `import()` whose argument is a string
6593+
* literal or `new URL('<literal>', import.meta.url)`.
6594+
* - a dynamic `import()` whose argument this scan cannot read (a variable, a
6595+
* property, a computed URL) has a target nothing here can name. It is not
6596+
* dropped: the file is marked, and EVERY code-position string literal in it
6597+
* that names a first-party module — a relative specifier, or a path spelled
6598+
* from the repo ROOT under `scripts/` — is taken as an edge. Both live
6599+
* shapes: a loader seam (`load = (specifier) => import(specifier)`) whose
6600+
* relative specifiers sit in a constant elsewhere in the file
6601+
* (`ARM_READER_SOURCE`), and a root-spelled roster loaded in process —
6602+
* `fleet-write/dispatch.mjs`'s self-test imports every sender through
6603+
* `pathToFileURL(join(REPO_ROOT, sender))`, `check-dispatch-gates.mjs` its
6604+
* engine through `import(resolve(ROOT, TOOL))`. Taking the root spelling
6605+
* also takes the fixture rows and text reads such a file spells in the same
6606+
* form (measured: five modules `check-governed-merges.mjs` names as data
6607+
* and never loads), and that is the direction this key errs in: a lead too
6608+
* many costs one gate run, a lead too few costs a red CI round.
6609+
*
6610+
* Resolution is `firstPartyImportBindings`': relative specifiers only, inside
6611+
* `scripts/`, outside `node_modules`, and only a target that exists — a bare
6612+
* specifier is an installed package, not a source a card edits.
6613+
*
6614+
* Returns `{ targets, unreadDynamic }`: `targets` sorted, `unreadDynamic` the
6615+
* count of `import()` calls whose argument was not read, so a caller can name
6616+
* the files the literal fallback widened.
6617+
*/
6618+
const LOAD_DYNAMIC_IMPORT = /(?<![\w$.])import[ \t\n]*\(/g;
6619+
const LOAD_DYNAMIC_LITERAL = /^[ \t\n]*(['"`])([^'"`\n]+)\1[ \t\n]*[,)]/;
6620+
const LOAD_DYNAMIC_URL = /^[ \t\n]*new[ \t\n]+URL[ \t\n]*\([ \t\n]*(['"`])([^'"`\n]+)\1[ \t\n]*,[ \t\n]*import\.meta\.url[ \t\n]*\)/;
6621+
const LOAD_MODULE_LITERAL = /(['"`])((?:\.\.?\/|scripts\/)[^'"`\n]*?\.(?:mjs|cjs|js|mts|cts|ts))\1/g;
6622+
6623+
export function loadEdgeTargets(scriptPath, source, { root = ROOT, exists = null } = {}) {
6624+
const text = String(source);
6625+
const scan = scanSource(text);
6626+
const body = blank(text, scan.comment);
6627+
const isCode = (at) => !scan.comment[at] && !scan.literal[at];
6628+
const here = nodePath.dirname(nodePath.join(root, scriptPath));
6629+
const present = exists ?? ((rel) => {
6630+
const abs = nodePath.join(root, rel);
6631+
return existsSync(abs) && statSync(abs).isFile();
6632+
});
6633+
const targets = new Set();
6634+
const admit = (rel) => {
6635+
if (!rel.startsWith('scripts/')) return;
6636+
if (rel.split('/').includes('node_modules')) return;
6637+
if (rel === scriptPath) return;
6638+
if (present(rel)) targets.add(rel);
6639+
};
6640+
const resolveRelative = (specifier) => {
6641+
if (!specifier.startsWith('./') && !specifier.startsWith('../')) return;
6642+
admit(nodePath.relative(root, nodePath.resolve(here, specifier)));
6643+
};
6644+
// The keyword's own offset, not the match's: both regexes open on the
6645+
// statement separator before it.
6646+
for (const m of body.matchAll(IMPORT_FROM_SPECIFIER)) {
6647+
if (isCode(m.index + m[0].indexOf(m[1]))) resolveRelative(m[4]);
6648+
}
6649+
for (const m of body.matchAll(SIDE_EFFECT_IMPORT)) {
6650+
if (isCode(m.index + m[0].indexOf('import'))) resolveRelative(m[2]);
6651+
}
6652+
let unreadDynamic = 0;
6653+
for (const m of body.matchAll(LOAD_DYNAMIC_IMPORT)) {
6654+
if (!isCode(m.index)) continue;
6655+
const rest = body.slice(m.index + m[0].length, m.index + m[0].length + 400);
6656+
if (/^[ \t\n]*\)/.test(rest)) continue;
6657+
const literal = LOAD_DYNAMIC_LITERAL.exec(rest);
6658+
if (literal && !(literal[1] === '`' && literal[2].includes('${'))) {
6659+
resolveRelative(literal[2]);
6660+
continue;
6661+
}
6662+
const url = LOAD_DYNAMIC_URL.exec(rest);
6663+
if (url && !(url[1] === '`' && url[2].includes('${'))) {
6664+
resolveRelative(url[2]);
6665+
continue;
6666+
}
6667+
unreadDynamic += 1;
6668+
}
6669+
if (unreadDynamic > 0) {
6670+
for (const m of body.matchAll(LOAD_MODULE_LITERAL)) {
6671+
// A literal in CODE: its opening delimiter is code and its first byte is
6672+
// literal — the scan flags a literal's content, never its delimiters.
6673+
if (!isCode(m.index) || !scan.literal[m.index + 1]) continue;
6674+
if (m[1] === '`' && m[2].includes('${')) continue;
6675+
if (m[2].startsWith('scripts/')) admit(m[2]);
6676+
else resolveRelative(m[2]);
6677+
}
6678+
}
6679+
return { targets: [...targets].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)), unreadDynamic };
6680+
}
6681+
6682+
/**
6683+
* Every first-party module running these files LOADS, transitively, each with
6684+
* the importer that first reached it (#33848) — the forward closure of
6685+
* `loadEdgeTargets` from a family's gate files, which read from the module's
6686+
* side is the REVERSE import graph: module M is in family F's reach exactly
6687+
* when M is reachable backwards from F's gate files. Breadth-first, so each
6688+
* module's recorded importer is on a shortest chain, and the chain a row
6689+
* prints is the shortest one a reader can walk.
6690+
*
6691+
* `edgesOf(rel)` answers one file's targets; the discovery hands in a memoised
6692+
* one so a module shared by many families is scanned once. A file it cannot
6693+
* read contributes no edge and is still in the reach — the edge INTO it is a
6694+
* fact about its importer.
6695+
*/
6696+
export function importGraphReach(files, edgesOf) {
6697+
const own = new Set(files);
6698+
const reach = new Map();
6699+
const queue = [...files];
6700+
const seen = new Set(files);
6701+
while (queue.length > 0) {
6702+
const from = queue.shift();
6703+
let targets = [];
6704+
try {
6705+
targets = edgesOf(from) ?? [];
6706+
} catch {
6707+
targets = [];
6708+
}
6709+
for (const to of targets) {
6710+
if (seen.has(to)) continue;
6711+
seen.add(to);
6712+
if (!own.has(to)) reach.set(to, from);
6713+
queue.push(to);
6714+
}
6715+
}
6716+
return reach;
6717+
}
6718+
6719+
/**
6720+
* The chain a reach row prints, gate file first: walk the recorded importers
6721+
* back from the module until one is the family's own file.
6722+
*/
6723+
export function importGraphChain(reach, module) {
6724+
const chain = [module];
6725+
let at = module;
6726+
const guard = new Set([module]);
6727+
while (reach.has(at)) {
6728+
at = reach.get(at);
6729+
if (guard.has(at)) break;
6730+
guard.add(at);
6731+
chain.unshift(at);
6732+
}
6733+
return chain;
6734+
}
6735+
6736+
/**
6737+
* The subtree the import-graph key answers for (#33848). The refusal in
6738+
* `coveringKey`'s docblock (#13126) stands for every other module: its price is
6739+
* the fan-in of the shared heads under `scripts/`, none of which is in here.
6740+
*/
6741+
export const IMPORT_GRAPH_KEY_ROOT = 'scripts/pm/';
6742+
65756743
/**
65766744
* The exporter half of the binding rule: is NAME declared in this module as a
65776745
* VALUE — a `const` initialised to one primitive literal?
@@ -7889,12 +8057,43 @@ export function coveringJobFilter(entry, inputPath) {
78898057
*
78908058
* What the refusal COSTS is a live missing lead, and it is named rather than
78918059
* implied: `scripts/pm/bare-root-worklist.mjs --self-test` statically imports
7892-
* THIS file, and a card editing this file derives 14 families without naming
8060+
* THIS file, and a card editing this file derived 14 families without naming
78938061
* it. That gate gets run by hand, but lint.yml runs it on every PR too, so the
78948062
* miss costs one CI round, which is the side this file's header errs on
78958063
* everywhere, and it is a far smaller cost than the card the general key
78968064
* prints for the module nearly every family imports — the run's `would name F
7897-
* families` line is what prices that one.
8065+
* families` line is what prices that one. (That witness is now paid back by
8066+
* the admission below, because THIS file lives under `scripts/pm/`.)
8067+
*
8068+
* ## …and ADMITTED for `scripts/pm/**`, transitively (#33848)
8069+
*
8070+
* The refusal is priced on the shared heads, and none of them is a pm module.
8071+
* The pm tools are the other shape: they import one another in CHAINS — a
8072+
* register module read by an executor that a dispatcher loads — and each
8073+
* chain ends in a gate whose self-test fakes what it imports. Measured on PR
8074+
* #33804's first head (`check-governed-queue-guard.mjs` and
8075+
* `check-governed-merges.mjs`): the derivation named 35 gates and not
8076+
* `check:pm-fleet-write-execute`, whose script imports a changed function and
8077+
* whose self-test went red in CI on that head, behind a dev's "35 of 35 exit
8078+
* 0". So a changed path under `IMPORT_GRAPH_KEY_ROOT` is answered by the
8079+
* family of every gate whose files LOAD it, at any depth: `importGraphReach`
8080+
* walks `loadEdgeTargets` forward from each family's files, which read from
8081+
* the module's side is the reverse import graph. The key is the LAST one
8082+
* consulted, so it re-attributes nothing an earlier key answered, and it
8083+
* ignores both narrowings the population follow takes — it walks self-test
8084+
* families too, and gate files too — because it claims what RUNNING the
8085+
* family loads, never what the family watches.
8086+
*
8087+
* Its price is real and is the card's to pay: the pm tools close into one
8088+
* large cycle (the governed register, the half-state patrol, the queue guard
8089+
* and this file all load one another), so an edit to most pm modules names
8090+
* the forty-odd families that run any of them. That is the size of what such
8091+
* an edit can break, measured rather than assumed; the replay row and the
8092+
* transitive row in `--self-test` are its pins, and a row there PRINTS the
8093+
* current size (modules reached, pairs, the most-reached module) rather than
8094+
* this prose recording it. Its time price is small and measured: the load
8095+
* scan adds about one to two seconds to a cold discovery (`derivationInputs`
8096+
* carries the before/after readings).
78988097
*
78998098
* WHERE THAT PRICE DOES NOT HOLD (#13467). "One CI round" is the WITNESS's
79008099
* price and an AVERAGE over the class, and the next reader will quote it as the
@@ -8010,6 +8209,16 @@ export function coveringKey(entry, inputPath) {
80108209
entry.readEdge?.get(read) === 'declared-self-test' ? 'declared self-test read' : 'program text read';
80118210
return { key: read, via: by && by !== read ? `${kind} by ${by}` : kind };
80128211
}
8212+
// The import-graph key (#33848), LAST for the reason the read key above is
8213+
// last: additive by construction, so every path an earlier key already
8214+
// answered keeps its exact key and label. The FIFTH key this docblock refuses
8215+
// is this one, and it stays refused outside `IMPORT_GRAPH_KEY_ROOT`; inside it
8216+
// it is admitted transitively, because the pm tools import one another in
8217+
// chains (a changed register read by an executor a dispatcher loads) and a
8218+
// one-level edge would miss the importer of an importer.
8219+
if (String(inputPath).startsWith(IMPORT_GRAPH_KEY_ROOT) && entry.importReach?.has(inputPath)) {
8220+
return { key: inputPath, via: `import graph ${importGraphChain(entry.importReach, inputPath).join(' -> ')}` };
8221+
}
80138222
return null;
80148223
}
80158224

@@ -13527,7 +13736,22 @@ function discoverFamiliesPass(tree) {
1352713736
}
1352813737
return manifestHints.get(rel);
1352913738
};
13739+
// The LOAD graph the import-graph key walks (#33848), one scan per file
13740+
// however many families reach it — the discovery memo makes it one per tree.
13741+
const loadEdges = new Map();
13742+
const loadEdgesOf = (rel) => {
13743+
if (!loadEdges.has(rel)) {
13744+
const abs = nodePath.join(ROOT, rel);
13745+
loadEdges.set(rel, existsSync(abs) ? loadEdgeTargets(rel, readFileSync(abs, 'utf8')).targets : []);
13746+
}
13747+
return loadEdges.get(rel);
13748+
};
1353013749
for (const entry of byCheck.values()) {
13750+
// ⛔ NOT gated on `entry.selfTest`, and not narrowed to non-gate modules:
13751+
// this is a claim about what RUNNING the family loads, and a `--self-test`
13752+
// invocation loads its script's whole import graph as surely as the bare
13753+
// run does — the card's own specimen is a self-test family.
13754+
entry.importReach = importGraphReach(entry.files ?? [], loadEdgesOf);
1353113755
entry.imports = [];
1353213756
// The subset of `entry.imports` that CONTRIBUTES a population (#17991).
1353313757
// The edge list stays whole — the gate really does import every module in
@@ -16361,9 +16585,14 @@ const derivationInputsMemo = new WeakMap();
1636116585
*
1636216586
* Memoised per discovery, which is itself memoised per tree, so a derivation
1636316587
* that already discovered pays nothing more for it — and that is the ONLY
16364-
* price this may have. A discovery costs seconds (measured: ~14 s cold on
16365-
* this tree), so the modes that derive hand it to `baseDrift` and `--tier`,
16366-
* which reads no workflow by contract, does not: a battery of `--tier`
16588+
* price this may have. A discovery costs seconds — measured on a quiet
16589+
* container, two cold runs each: 16.6 s and 14.8 s before the import-graph
16590+
* key (#33848, base e71ecc1b), 17.2 s and 17.0 s with it (6f34ab17); a whole `--commands`
16591+
* run over one pm path, 36.0 s and 37.0 s before, 38.0 s and 35.7 s after.
16592+
* A shared box reads higher (48.7 s was measured for that run under load), so
16593+
* quote the ratio, never one absolute. Because it costs that much, the modes
16594+
* that derive hand it to `baseDrift` and `--tier`, which reads no workflow by
16595+
* contract, does not: a battery of `--tier`
1636716596
* children on a tree a few commits behind was measured to quadruple its wall
1636816597
* clock when the banner asked for a discovery the mode never needed.
1636916598
*/
@@ -16378,6 +16607,9 @@ export function derivationInputs({ tree = repoCorpus().tree } = {}) {
1637816607
for (const list of [entry.files, entry.imports, entry.runs, entry.reads, entry.manifests]) {
1637916608
for (const rel of list ?? []) inputs.add(rel);
1638016609
}
16610+
// The import-graph key's reach (#33848): a module's edges decide which
16611+
// families that key names, so every file the walk opened is an input.
16612+
for (const rel of entry.importReach?.keys() ?? []) inputs.add(rel);
1638116613
}
1638216614
derivationInputsMemo.set(discovered, inputs);
1638316615
return inputs;

0 commit comments

Comments
 (0)