Skip to content

Commit a39185b

Browse files
committed
fix(dispatch-gates): a package-root vitest config or manifest edit derives check-console-intercept-disarm
The gate declares what its scan opens (a package-root package.json and vitest config under each workspace glob) as ROOT_DIR_WATCH_HINTS in its own source, and its scan reds when that declaration drifts from pnpm-workspace.yaml. Both invocations leave the root-walk residue, so their two ROOT_WALK_RESIDUE_LEDGER rows are retired. The dispatch-gates self-test pins the acceptance against the live tree: every package-root config and manifest places both invocations; no test file under a package does. Claude-Session: https://claude.ai/code/session_01JQwxbXV6v4rbiDvvaaabhb Co-authored-by: Claude <noreply@anthropic.com>
1 parent 399df5b commit a39185b

3 files changed

Lines changed: 249 additions & 34 deletions

File tree

‎scripts/check-console-intercept-disarm.mjs‎

Lines changed: 175 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,54 @@ const VITEST_CONFIG_NAMES = [
115115
'vitest.config.cjs',
116116
];
117117

118+
// The population this gate reads, declared for `scripts/pm/dispatch-gates.mjs`
119+
// (the ROOT_DIR_WATCH_HINTS idiom), so a card that edits a package-root
120+
// manifest or vitest config is told about this gate before CI is. `scan()`
121+
// below opens exactly two files per workspace package: its `package.json` and
122+
// its package-root vitest config. Nothing else under a package moves the
123+
// verdict, so a new test file under an existing package derives nothing.
124+
//
125+
// One pair per glob `workspaceGlobs()` reads, in that order. A `<dir>/*` glob
126+
// is spelled `<dir>/*/package.json` and `<dir>/*/vitest.config.*` — the glob in
127+
// a non-final segment makes the derivation judge it as a pattern, one path
128+
// segment per `*`. A literal member `<dir>` would be spelled `<dir>/package.json`
129+
// and `<dir>/vitest.config` (`watchHintsForGlob`): with no glob ahead of it a
130+
// trailing `.*` is collapsed to `<dir>/vitest.config.`, which names nothing,
131+
// while the bare stem reaches every one of VITEST_CONFIG_NAMES through the
132+
// derivation's module-extension equality. None is read today: the parse stops
133+
// at the first comment line in the `packages:` list, so the `ui` members below
134+
// it are not in this gate's population, and are not declared here either.
135+
//
136+
// ⛔ Hand-kept, and `scan()` holds it to the workspace file: a glob in
137+
// `pnpm-workspace.yaml` with no pair here, or a pair here for a glob that file
138+
// no longer lists, is a finding (`watchHintDrift`). Not readable from the
139+
// repo-root files themselves — `pnpm-workspace.yaml` and `vitest.preset.mts`
140+
// carry no separator, so no spelling of them is a hint the derivation admits.
141+
export const ROOT_DIR_WATCH_HINTS = Object.freeze([
142+
'packages/*/package.json',
143+
'packages/*/vitest.config.*',
144+
'packages/apps/*/package.json',
145+
'packages/apps/*/vitest.config.*',
146+
'packages/drivers/*/package.json',
147+
'packages/drivers/*/vitest.config.*',
148+
'packages/plugins/*/package.json',
149+
'packages/plugins/*/vitest.config.*',
150+
'packages/qa/*/package.json',
151+
'packages/qa/*/vitest.config.*',
152+
'packages/triggers/*/package.json',
153+
'packages/triggers/*/vitest.config.*',
154+
'packages/services/*/package.json',
155+
'packages/services/*/vitest.config.*',
156+
'packages/adapters/*/package.json',
157+
'packages/adapters/*/vitest.config.*',
158+
'packages/connectors/*/package.json',
159+
'packages/connectors/*/vitest.config.*',
160+
'apps/*/package.json',
161+
'apps/*/vitest.config.*',
162+
'examples/*/package.json',
163+
'examples/*/vitest.config.*',
164+
]);
165+
118166
const DISARM_RE = /\bdisableConsoleIntercept\s*:\s*true\b/;
119167
const REARM_RE = /\bdisableConsoleIntercept\s*:\s*false\b/;
120168
const PROJECTS_RE = /\bprojects\s*:\s*\[/;
@@ -207,6 +255,31 @@ const REMEDY = ` // Late console writes must not redden a green suite (#10374
207255
* instead of silently emptying its population.
208256
*/
209257
export function workspacePackageDirs(root) {
258+
const dirs = [];
259+
for (const glob of workspaceGlobs(root)) {
260+
if (glob.endsWith('/*')) {
261+
const base = join(root, glob.slice(0, -2));
262+
if (!existsSync(base)) continue;
263+
for (const entry of readdirSync(base, { withFileTypes: true })) {
264+
if (!entry.isDirectory()) continue;
265+
const dir = join(base, entry.name);
266+
if (existsSync(join(dir, 'package.json'))) dirs.push(dir);
267+
}
268+
} else if (!glob.includes('*')) {
269+
const dir = join(root, glob);
270+
if (existsSync(join(dir, 'package.json'))) dirs.push(dir);
271+
} else {
272+
throw new Error(
273+
`pnpm-workspace.yaml glob '${glob}' is wider than '<dir>/*' — teach ` +
274+
`workspacePackageDirs() the new shape before trusting this gate again`,
275+
);
276+
}
277+
}
278+
return dirs;
279+
}
280+
281+
/** `pnpm-workspace.yaml`'s `packages:` globs, in file order. Throws when none parse. */
282+
export function workspaceGlobs(root) {
210283
const raw = readFileSync(join(root, 'pnpm-workspace.yaml'), 'utf8');
211284
const globs = [];
212285
let inPackages = false;
@@ -227,27 +300,51 @@ export function workspacePackageDirs(root) {
227300
if (globs.length === 0) {
228301
throw new Error(`no package globs parsed from ${join(root, 'pnpm-workspace.yaml')}`);
229302
}
230-
const dirs = [];
231-
for (const glob of globs) {
232-
if (glob.endsWith('/*')) {
233-
const base = join(root, glob.slice(0, -2));
234-
if (!existsSync(base)) continue;
235-
for (const entry of readdirSync(base, { withFileTypes: true })) {
236-
if (!entry.isDirectory()) continue;
237-
const dir = join(base, entry.name);
238-
if (existsSync(join(dir, 'package.json'))) dirs.push(dir);
239-
}
240-
} else if (!glob.includes('*')) {
241-
const dir = join(root, glob);
242-
if (existsSync(join(dir, 'package.json'))) dirs.push(dir);
243-
} else {
244-
throw new Error(
245-
`pnpm-workspace.yaml glob '${glob}' is wider than '<dir>/*' — teach ` +
246-
`workspacePackageDirs() the new shape before trusting this gate again`,
247-
);
248-
}
303+
return globs;
304+
}
305+
306+
/**
307+
* The watch-hint pair one workspace glob owes `ROOT_DIR_WATCH_HINTS`, spelled
308+
* the way that declaration's comment explains. A glob wider than `<dir>/*` owes
309+
* nothing here: `workspacePackageDirs()` already refuses it, loudly.
310+
*/
311+
export function watchHintsForGlob(glob) {
312+
if (glob.endsWith('/*')) return [`${glob}/package.json`, `${glob}/vitest.config.*`];
313+
if (!glob.includes('*')) return [`${glob}/package.json`, `${glob}/vitest.config`];
314+
return [];
315+
}
316+
317+
/**
318+
* Where `ROOT_DIR_WATCH_HINTS` and the workspace globs disagree: `missing` are
319+
* pairs a listed glob owes and the declaration lacks (a package there edits its
320+
* config and no dispatch derivation names this gate); `stale` are declared
321+
* hints no listed glob owes (a lead for a population this gate no longer reads).
322+
*/
323+
export function watchHintDrift(globs, hints = ROOT_DIR_WATCH_HINTS) {
324+
const owed = globs.flatMap(watchHintsForGlob);
325+
return {
326+
missing: owed.filter((h) => !hints.includes(h)),
327+
stale: hints.filter((h) => !owed.includes(h)),
328+
};
329+
}
330+
331+
function watchHintDriftFindings(root) {
332+
const { missing, stale } = watchHintDrift(workspaceGlobs(root));
333+
const findings = [];
334+
if (missing.length > 0) {
335+
findings.push(
336+
`ROOT_DIR_WATCH_HINTS lacks ${missing.join(', ')} — pnpm-workspace.yaml lists the glob, so a ` +
337+
`card editing a package-root manifest or vitest config there is not told about this gate ` +
338+
`until CI. Add the pair to the declaration in scripts/check-console-intercept-disarm.mjs.`,
339+
);
249340
}
250-
return dirs;
341+
if (stale.length > 0) {
342+
findings.push(
343+
`ROOT_DIR_WATCH_HINTS declares ${stale.join(', ')}, which no pnpm-workspace.yaml glob owes — ` +
344+
`remove it from the declaration in scripts/check-console-intercept-disarm.mjs.`,
345+
);
346+
}
347+
return findings;
251348
}
252349

253350
/** @returns {{findings: string[], vitestPackages: number}} */
@@ -325,11 +422,12 @@ function main() {
325422
let result;
326423
try {
327424
result = scan(REPO_ROOT);
425+
result.driftFindings = watchHintDriftFindings(REPO_ROOT);
328426
} catch (error) {
329427
console.error(`check-console-intercept-disarm: MEASUREMENT FAILED — ${error.message}`);
330428
process.exit(2);
331429
}
332-
const { findings, vitestPackages } = result;
430+
const { findings, vitestPackages, driftFindings } = result;
333431
if (vitestPackages === 0) {
334432
console.error(
335433
'check-console-intercept-disarm: MEASUREMENT FAILED — the scan found ZERO ' +
@@ -343,8 +441,14 @@ function main() {
343441
`check-console-intercept-disarm: ${findings.length} package(s) exposed to the ` +
344442
`vitest late-console teardown race (#10374):\n\n${findings.join('\n\n')}\n`,
345443
);
346-
process.exit(1);
347444
}
445+
if (driftFindings.length > 0) {
446+
console.error(
447+
`check-console-intercept-disarm: the declared watch population has drifted from ` +
448+
`pnpm-workspace.yaml:\n\n${driftFindings.join('\n\n')}\n`,
449+
);
450+
}
451+
if (findings.length > 0 || driftFindings.length > 0) process.exit(1);
348452
console.log(
349453
`OK: ${vitestPackages} vitest-running package(s), every one disarms console ` +
350454
`interception at the package root.`,
@@ -644,6 +748,54 @@ check(() => {
644748
}
645749
});
646750

751+
// The declared watch population (ROOT_DIR_WATCH_HINTS) against the REAL
752+
// workspace file, and the drift reading in both directions. Fixture globs are
753+
// assembled from segments so no fixture string reads as a path literal of
754+
// this gate's own.
755+
const realDrift = watchHintDrift(workspaceGlobs(REPO_ROOT));
756+
check(() => {
757+
if (realDrift.missing.length > 0 || realDrift.stale.length > 0) {
758+
failures += 1;
759+
console.error(
760+
`self-test FAIL: ROOT_DIR_WATCH_HINTS has drifted from pnpm-workspace.yaml — ` +
761+
`missing: ${realDrift.missing.join(', ') || 'none'}; stale: ${realDrift.stale.join(', ') || 'none'}.`,
762+
);
763+
}
764+
});
765+
const extraGlob = ['fixture-root', '*'].join('/');
766+
const addedGlob = watchHintDrift([...workspaceGlobs(REPO_ROOT), extraGlob]);
767+
check(() => {
768+
const want = watchHintsForGlob(extraGlob);
769+
if (want.length !== 2 || addedGlob.missing.join('|') !== want.join('|') || addedGlob.stale.length !== 0) {
770+
failures += 1;
771+
console.error(`self-test FAIL: a workspace glob the declaration lacks is not read as missing: ${JSON.stringify(addedGlob)}`);
772+
}
773+
});
774+
const droppedGlob = watchHintDrift(workspaceGlobs(REPO_ROOT).filter((g) => g !== workspaceGlobs(REPO_ROOT)[0]));
775+
check(() => {
776+
const want = watchHintsForGlob(workspaceGlobs(REPO_ROOT)[0]);
777+
if (droppedGlob.stale.join('|') !== want.join('|') || droppedGlob.missing.length !== 0) {
778+
failures += 1;
779+
console.error(`self-test FAIL: a declared pair no workspace glob owes is not read as stale: ${JSON.stringify(droppedGlob)}`);
780+
}
781+
});
782+
check(() => {
783+
// A literal member is spelled with the bare config STEM, never a trailing
784+
// `.*` (which the derivation collapses to a name no file has); a `<dir>/*`
785+
// member keeps the `.*`, and a wider glob owes nothing.
786+
const literal = watchHintsForGlob('fixture-root');
787+
const starred = watchHintsForGlob(extraGlob);
788+
const wide = watchHintsForGlob(['fixture-root', '**'].join('/'));
789+
if (
790+
literal[1] !== ['fixture-root', 'vitest.config'].join('/') ||
791+
starred[1] !== [extraGlob, 'vitest.config.*'].join('/') ||
792+
wide.length !== 0
793+
) {
794+
failures += 1;
795+
console.error(`self-test FAIL: watchHintsForGlob spelling — ${JSON.stringify({ literal, starred, wide })}`);
796+
}
797+
});
798+
647799
// ── The floor: every declared battery RAN, and ran its cases (#13489) ────
648800
//
649801
// Evaluated after every battery has had its chance and BEFORE the verdict, so
@@ -692,7 +844,7 @@ check(() => {
692844
}
693845

694846
if (failures > 0) process.exit(1);
695-
console.log(`self-test OK: ${cases.length} cases + real-tree population floor.`);
847+
console.log(`self-test OK: ${cases.length} cases + real-tree population floor + watch-hint declaration.`);
696848

697849
return SELF_TEST_VERDICT;
698850
}

‎scripts/pm/dispatch-gates.data.mjs‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,22 @@ export const PATH_LIST_MARKER_KEYS = Object.freeze(['inherited-population', 'sel
278278
* reds too: a stale exclusion is an exclusion nobody is measuring any more.
279279
*/
280280
export const ROOT_WALK_RESIDUE_LEDGER = [
281-
[
282-
'scripts/check-console-intercept-disarm.mjs',
283-
'its `scan(REPO_ROOT)` walks `workspacePackageDirs(root)` — every workspace PACKAGE ROOT\'s package.json and '
284-
+ 'vitest.config.*, read off pnpm-workspace.yaml. That is workspace-wide but it is not every file: a new test '
285-
+ 'file under an existing package does not move it, a new PACKAGE does. Declaring the whole tree would put it '
286-
+ 'on every card on a population it does not read.',
287-
],
288-
[
289-
'scripts/check-console-intercept-disarm.mjs --self-test',
290-
'the same gate file, reached through its self-test invocation; the reading above is the whole of it.',
291-
],
292281
[
293282
'scripts/check-skill-frame-freshness.mjs --self-test',
294283
'lint.yml runs the SELF-TEST HALF and never the scan — its step is named that, and the step comment states why '
295284
+ '(the pnpm script would drag the scan in with it). The repo-root default parameter belongs to the scan half '
296285
+ 'CI does not schedule, so a whole-tree row for this family would advertise work no workflow performs.',
297286
],
287+
// ⚖️ `scripts/check-console-intercept-disarm.mjs` and its `--self-test` LEFT this
288+
// population under #29353 and their two rows are gone with them — a listed
289+
// family that stops being a member reds here. They left BY PATH: the gate now
290+
// declares what its scan opens, a package-root `package.json` and vitest
291+
// config under each workspace glob, as `ROOT_DIR_WATCH_HINTS` in its own
292+
// source, and its scan reds when that declaration drifts from
293+
// `pnpm-workspace.yaml`. The rows had stated the population right and drawn
294+
// the wrong conclusion from it: an EDITED package-root config moves the gate
295+
// as surely as a new package does, so a card editing one learned of the gate
296+
// only from CI (PR #29345 paid one fix round).
298297
// ⚖️ `check:pm-half-states` LEFT this population under #16904's D2 and its row
299298
// is gone with it — a listed family that stops being a member reds here, and
300299
// a stale exclusion is an exclusion nobody measures. It is now placeable BY

‎scripts/pm/dispatch-gates.self-test.mjs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7988,6 +7988,70 @@ export function selfTest({ tier = 'full' } = {}) {
79887988
);
79897989
}
79907990

7991+
// ── A package-root config edit derives the console-intercept gate (#29353) ──
7992+
//
7993+
// The card's acceptance, held against the LIVE tree. The gate's scan opens a
7994+
// package-root `package.json` and vitest config under each workspace glob and
7995+
// nothing else, and it declares exactly that population in its own source, so
7996+
// BOTH invocations CI runs (scan and `--self-test`, one lint.yml step) are
7997+
// placed by every such file, while a test file under an existing package —
7998+
// tracked, or one a card adds — places neither. The rows it left in
7999+
// ROOT_WALK_RESIDUE_LEDGER are held gone by the stale limb above.
8000+
{
8001+
const gate = 'scripts/check-console-intercept-disarm.mjs';
8002+
const invocations = [gate, `${gate} --self-test`];
8003+
const entries = invocations.map((c) => liveDiscovery.byCheck.get(c));
8004+
t('both console-intercept invocations are discovered families', entries.every(Boolean));
8005+
const tracked = [...liveTree.files];
8006+
const trackedSet = new Set(tracked);
8007+
// Package roots read off the tree, not off a copy of the workspace globs:
8008+
// a tracked vitest config whose own directory carries a tracked manifest,
8009+
// under one of the three workspace top-level roots.
8010+
const rootConfigs = tracked.filter((f) => {
8011+
const m = /^((?:packages|apps|examples)\/(?:[^/]+\/)?[^/]+)\/vitest\.config\.[cm]?[jt]s$/.exec(f);
8012+
return m && trackedSet.has(`${m[1]}/package.json`);
8013+
});
8014+
t(
8015+
`control: the live tree carries package-root vitest configs to probe with (${rootConfigs.length})`,
8016+
rootConfigs.length >= 60,
8017+
);
8018+
const unplacedBy = (entry, probe) => placeFamily(entry, [probe]).verdict !== 'matched';
8019+
const missedConfigs = rootConfigs.filter((f) => entries.some((e) => e && unplacedBy(e, f)));
8020+
t(
8021+
'every package-root vitest config places both invocations — the card PR #29345 paid a CI round for' +
8022+
(missedConfigs.length ? ` — not placed by: ${missedConfigs.slice(0, 5).join(', ')}` : ''),
8023+
entries.every(Boolean) && missedConfigs.length === 0,
8024+
);
8025+
const manifests = rootConfigs.map((f) => f.replace(/\/[^/]+$/, '/package.json'));
8026+
const missedManifests = manifests.filter((f) => entries.some((e) => e && unplacedBy(e, f)));
8027+
t(
8028+
'and so does the package-root package.json beside each — the other file the scan opens' +
8029+
(missedManifests.length ? ` — not placed by: ${missedManifests.slice(0, 5).join(', ')}` : ''),
8030+
entries.every(Boolean) && missedManifests.length === 0,
8031+
);
8032+
// A card's file surface is often paths that do not exist yet: a NEW package.
8033+
const newPackage = ['packages', 'plugins', 'plugin-not-yet-written'].join('/');
8034+
t(
8035+
'a NEW package\'s root config and manifest place both invocations too',
8036+
entries.every((e) => e && !unplacedBy(e, `${newPackage}/vitest.config.ts`) && !unplacedBy(e, `${newPackage}/package.json`)),
8037+
);
8038+
const testFiles = tracked.filter((f) => /^(?:packages|apps|examples)\/.+\.(?:test|spec)\.[cm]?[jt]sx?$/.test(f));
8039+
t(
8040+
`control: the live tree carries test files under packages to probe with (${testFiles.length})`,
8041+
testFiles.length >= 1000,
8042+
);
8043+
t(
8044+
'no test file under an existing package places either invocation, tracked or brand new',
8045+
entries.every((e) => e
8046+
&& placeFamily(e, testFiles).verdict !== 'matched'
8047+
&& unplacedBy(e, ['packages', 'services', 'service-sms', 'src', 'not-yet-written.test.ts'].join('/'))),
8048+
);
8049+
t(
8050+
'nor does a vitest config BELOW a package root — the scan deliberately reads the root one only',
8051+
entries.every((e) => e && unplacedBy(e, ['packages', 'services', 'service-sms', 'test', 'fixtures', 'vitest.config.ts'].join('/'))),
8052+
);
8053+
}
8054+
79918055
// ── The CI-MEASURED-ONLY shape (#14004) ───────────────────────────────────
79928056
//
79938057
// The two markers above are DECLARATIONS a gate carries. This one is the

0 commit comments

Comments
 (0)