@@ -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+
118166const DISARM_RE = / \b d i s a b l e C o n s o l e I n t e r c e p t \s * : \s * t r u e \b / ;
119167const REARM_RE = / \b d i s a b l e C o n s o l e I n t e r c e p t \s * : \s * f a l s e \b / ;
120168const PROJECTS_RE = / \b p r o j e c t s \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 */
209257export 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}
0 commit comments