@smoothbricks/nx-plugin 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/README.md +417 -171
  2. package/dist/.build-inputs.json +39 -28
  3. package/dist/bin/smoo-nx-go-test-build.d.ts +3 -0
  4. package/dist/bin/smoo-nx-go-test-build.d.ts.map +1 -0
  5. package/dist/bin/smoo-nx-go-test-build.js +79 -0
  6. package/dist/bin/smoo-nx-go-test-run.d.ts +3 -0
  7. package/dist/bin/smoo-nx-go-test-run.d.ts.map +1 -0
  8. package/dist/bin/smoo-nx-go-test-run.js +129 -0
  9. package/dist/bin/smoo-nx-publish.d.ts +3 -0
  10. package/dist/bin/smoo-nx-publish.d.ts.map +1 -0
  11. package/dist/bin/smoo-nx-publish.js +3 -0
  12. package/dist/bounded-test-policy.d.ts +1 -1
  13. package/dist/bounded-test-policy.d.ts.map +1 -1
  14. package/dist/bounded-test-policy.js +9 -8
  15. package/dist/cargo-retention-hook.d.ts +6 -2
  16. package/dist/cargo-retention-hook.d.ts.map +1 -1
  17. package/dist/cargo-retention-hook.js +13 -7
  18. package/dist/cargo-workspace.d.ts +10 -20
  19. package/dist/cargo-workspace.d.ts.map +1 -1
  20. package/dist/cargo-workspace.js +12 -22
  21. package/dist/cross-check-policy.d.ts +2 -2
  22. package/dist/cross-check-policy.js +2 -2
  23. package/dist/deploy-policy.d.ts +6 -0
  24. package/dist/deploy-policy.d.ts.map +1 -1
  25. package/dist/deploy-policy.js +11 -0
  26. package/dist/eslint-file-set.d.ts +4 -1
  27. package/dist/eslint-file-set.d.ts.map +1 -1
  28. package/dist/eslint-sources.d.ts +25 -0
  29. package/dist/eslint-sources.d.ts.map +1 -0
  30. package/dist/eslint-sources.js +99 -0
  31. package/dist/executors/all-verdicts/executor.d.ts +2 -1
  32. package/dist/executors/all-verdicts/executor.d.ts.map +1 -1
  33. package/dist/executors/all-verdicts/executor.js +48 -2
  34. package/dist/executors/all-verdicts/schema.d.ts +13 -0
  35. package/dist/executors/all-verdicts/schema.d.ts.map +1 -1
  36. package/dist/executors/all-verdicts/schema.js +7 -0
  37. package/dist/executors/bounded-exec/bounds.d.ts +57 -8
  38. package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
  39. package/dist/executors/bounded-exec/bounds.js +100 -11
  40. package/dist/executors/bounded-exec/executor.d.ts +2 -1
  41. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  42. package/dist/executors/bounded-exec/executor.js +289 -34
  43. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +2 -0
  44. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
  45. package/dist/executors/bounded-exec/hang-diagnostics.js +2 -2
  46. package/dist/executors/bounded-exec/mbx-pool.d.ts +56 -10
  47. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  48. package/dist/executors/bounded-exec/mbx-pool.js +145 -11
  49. package/dist/executors/bounded-exec/mbx-queue.d.ts +30 -0
  50. package/dist/executors/bounded-exec/mbx-queue.d.ts.map +1 -0
  51. package/dist/executors/bounded-exec/mbx-queue.js +229 -0
  52. package/dist/executors/bounded-exec/progress.d.ts +9 -2
  53. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  54. package/dist/executors/bounded-exec/progress.js +151 -25
  55. package/dist/executors/bounded-exec/runner-sizing.d.ts +40 -2
  56. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
  57. package/dist/executors/bounded-exec/runner-sizing.js +55 -4
  58. package/dist/executors/bounded-exec/runtime-pin.js +1 -1
  59. package/dist/executors/bounded-exec/test-result-writer.d.ts +30 -0
  60. package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/test-result-writer.js +126 -0
  62. package/dist/executors/bounded-exec/test-results.d.ts +33 -0
  63. package/dist/executors/bounded-exec/test-results.d.ts.map +1 -0
  64. package/dist/executors/bounded-exec/test-results.js +33 -0
  65. package/dist/executors/bounded-exec/verdict.d.ts +26 -2
  66. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  67. package/dist/executors/bounded-exec/verdict.js +108 -4
  68. package/dist/executors/wrangler-deploy/deploy-hash.d.ts +10 -0
  69. package/dist/executors/wrangler-deploy/deploy-hash.d.ts.map +1 -0
  70. package/dist/executors/wrangler-deploy/deploy-hash.js +14 -0
  71. package/dist/executors/wrangler-deploy/executor.d.ts +24 -0
  72. package/dist/executors/wrangler-deploy/executor.d.ts.map +1 -0
  73. package/dist/executors/wrangler-deploy/executor.js +43 -0
  74. package/dist/executors/wrangler-deploy/hasher.d.ts +27 -0
  75. package/dist/executors/wrangler-deploy/hasher.d.ts.map +1 -0
  76. package/dist/executors/wrangler-deploy/hasher.js +90 -0
  77. package/dist/executors/wrangler-deploy/schema.d.ts +5 -0
  78. package/dist/executors/wrangler-deploy/schema.d.ts.map +1 -0
  79. package/dist/go-tests.d.ts +30 -0
  80. package/dist/go-tests.d.ts.map +1 -0
  81. package/dist/go-tests.js +43 -0
  82. package/dist/index.d.ts +12 -4
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +338 -295
  85. package/dist/managed-files/files.d.ts.map +1 -1
  86. package/dist/managed-files/files.js +5 -0
  87. package/dist/nx-publish.d.ts +24 -0
  88. package/dist/nx-publish.d.ts.map +1 -0
  89. package/dist/nx-publish.js +150 -0
  90. package/dist/process-group.js +2 -2
  91. package/dist/runtime-input-policy.d.ts +17 -3
  92. package/dist/runtime-input-policy.d.ts.map +1 -1
  93. package/dist/runtime-input-policy.js +4 -4
  94. package/dist/test-timings.d.ts +23 -4
  95. package/dist/test-timings.d.ts.map +1 -1
  96. package/dist/test-timings.js +72 -12
  97. package/dist/tool-bins.d.ts +6 -8
  98. package/dist/tool-bins.d.ts.map +1 -1
  99. package/dist/tool-bins.js +7 -5
  100. package/dist/version-actions.cjs +10 -11
  101. package/dist/version-actions.d.cts.map +1 -1
  102. package/dist/workspace-config-policy.d.ts.map +1 -1
  103. package/dist/workspace-config-policy.js +17 -22
  104. package/dist/workspace-index.d.ts +7 -0
  105. package/dist/workspace-index.d.ts.map +1 -1
  106. package/dist/workspace-index.js +11 -0
  107. package/dist/workspace-manifest.js +3 -3
  108. package/executors.json +7 -6
  109. package/managed/raw/patches/nx@23.2.1.patch +118 -24
  110. package/managed/raw/patches/ttsc@0.34.1.patch +139 -0
  111. package/managed/raw/tooling/devenv +1 -2
  112. package/managed/raw/tooling/direnv/developer-links.ts +124 -51
  113. package/managed/raw/tooling/direnv/devenv.smoo.nix +34 -15
  114. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -10
  115. package/managed/raw/tooling/direnv/installed-bun-links.ts +7 -6
  116. package/managed/raw/tooling/direnv/setup-environment.ts +49 -24
  117. package/managed/raw/tooling/direnv/ttsc-env.sh +8 -6
  118. package/managed/raw/tooling/git-hooks/pinned-revisions-served.ts +252 -0
  119. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -32
  120. package/managed/templates/github/actions/cache-node-modules/action.yml +1 -1
  121. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +12 -11
  122. package/package.json +277 -11
  123. package/runtime/nx-publish/.build-inputs.json +11 -0
  124. package/runtime/nx-publish/cache_adapter.js +45 -0
  125. package/runtime/nx-publish/nx_publish.js +60 -0
  126. package/runtime-input.sh +62 -26
  127. package/src/bin/smoo-nx-go-test-build.ts +83 -0
  128. package/src/bin/smoo-nx-go-test-run.ts +140 -0
  129. package/src/bin/smoo-nx-publish.ts +4 -0
  130. package/src/bounded-test-policy.ts +9 -8
  131. package/src/cargo-retention-hook.ts +16 -10
  132. package/src/cargo-workspace.ts +12 -24
  133. package/src/cross-check-policy.ts +2 -2
  134. package/src/deploy-policy.ts +11 -0
  135. package/src/eslint-file-set.ts +4 -1
  136. package/src/eslint-sources.ts +134 -0
  137. package/src/executors/all-verdicts/executor.ts +58 -3
  138. package/src/executors/all-verdicts/schema.json +4 -0
  139. package/src/executors/all-verdicts/schema.ts +14 -0
  140. package/src/executors/bounded-exec/bounds.ts +132 -15
  141. package/src/executors/bounded-exec/executor.ts +329 -34
  142. package/src/executors/bounded-exec/hang-diagnostics.ts +3 -2
  143. package/src/executors/bounded-exec/mbx-pool.ts +158 -17
  144. package/src/executors/bounded-exec/mbx-queue.ts +170 -0
  145. package/src/executors/bounded-exec/progress.ts +171 -18
  146. package/src/executors/bounded-exec/runner-sizing.ts +73 -7
  147. package/src/executors/bounded-exec/runtime-pin.ts +1 -1
  148. package/src/executors/bounded-exec/test-result-writer.ts +152 -0
  149. package/src/executors/bounded-exec/test-results.ts +39 -0
  150. package/src/executors/bounded-exec/verdict.ts +115 -5
  151. package/src/executors/wrangler-deploy/deploy-hash.ts +15 -0
  152. package/src/executors/wrangler-deploy/executor.ts +63 -0
  153. package/src/executors/wrangler-deploy/hasher.ts +111 -0
  154. package/src/executors/wrangler-deploy/schema.json +16 -0
  155. package/src/executors/wrangler-deploy/schema.ts +4 -0
  156. package/src/go-tests.ts +67 -0
  157. package/src/index.ts +368 -323
  158. package/src/managed-files/files.ts +5 -0
  159. package/src/nx-publish.ts +157 -0
  160. package/src/process-group.ts +2 -2
  161. package/src/runtime-input-policy.ts +4 -4
  162. package/src/test-timings.ts +72 -14
  163. package/src/tool-bins.ts +13 -9
  164. package/src/version-actions.cts +10 -11
  165. package/src/workspace-config-policy.ts +20 -29
  166. package/src/workspace-index.ts +12 -0
  167. package/src/workspace-manifest.ts +3 -3
  168. package/test-timings.json +131 -125
  169. package/dist/executors/clean-outputs/executor.d.ts +0 -26
  170. package/dist/executors/clean-outputs/executor.d.ts.map +0 -1
  171. package/dist/executors/clean-outputs/executor.js +0 -83
  172. package/dist/executors/clean-outputs/schema.d.ts +0 -4
  173. package/dist/executors/clean-outputs/schema.d.ts.map +0 -1
  174. package/src/executors/clean-outputs/executor.ts +0 -131
  175. package/src/executors/clean-outputs/schema.json +0 -15
  176. package/src/executors/clean-outputs/schema.ts +0 -3
  177. /package/dist/executors/{clean-outputs → wrangler-deploy}/schema.js +0 -0
@@ -1,4 +1,5 @@
1
- import type { RunnerKind } from './runner-sizing.js';
1
+ import { shellTokens } from '../../runtime-input-policy.js';
2
+ import { CPU_TOKENS_ENV, type Demand, grantedSizing, invocationSpan, type ReservedDemand } from './runner-sizing.js';
2
3
  import { shellQuote } from './verdict.js';
3
4
 
4
5
  /**
@@ -11,38 +12,178 @@ export interface MbxPool {
11
12
  readonly priority: 'normal' | 'low';
12
13
  }
13
14
 
14
- /** The pool the environment names, or `null` where the project has no mbx and cowshed's own pools apply unchanged. */
15
+ /**
16
+ * The pool the environment names, or `null` where the project has no mbx and cowshed's own pools apply unchanged, or
17
+ * where this run is already inside an mbx reservation: `mbx reserve` hands its command `MBX_SCHED_DISABLE=1`, and nested
18
+ * non-Cargo work shares the enclosing grant instead of reserving twice (specs/cowshed/05_gateway.md, "Parallelism
19
+ * inside a job"). A second reservation would wait in the pool while its parent holds a grant, so under a full pool the
20
+ * nested leaf queued behind the very work it belongs to.
21
+ */
15
22
  export function mbxPool(environment: Readonly<NodeJS.ProcessEnv>): MbxPool | null {
16
- if (environment.MBX_SCHEDULER !== '1' || !environment.MBX_CACHE_DIR) {
23
+ if (environment.MBX_SCHEDULER !== '1' || !environment.MBX_CACHE_DIR || environment.MBX_SCHED_DISABLE === '1') {
17
24
  return null;
18
25
  }
19
26
  return { priority: environment.MBX_SCHEDULER_PRIORITY === 'normal' ? 'normal' : 'low' };
20
27
  }
21
28
 
22
29
  /**
23
- * How a leaf enters the pool once: a Cargo leaf runs through mbx, which admits its compiler and supported test work
24
- * itself, so wrapping it would charge it twice; any other leaf holds `cpus` of the pool for its whole run through
25
- * `mbx reserve`, `cpus` being the job's own count.
30
+ * How a leaf enters the pool once. A Cargo leaf that builds runs through mbx, which admits its compilers and supported
31
+ * test work itself, so wrapping it would charge it twice. Any other leaf reserves for its whole run through
32
+ * `mbx reserve --cpus <its runner's count>`, a maximum: mbx grants as many of those cores as are free once one is, and
33
+ * the runner runs on the count it is granted (`sizeRunnerToGrant`). That includes a nextest run of test binaries
34
+ * already built (the inferred extracted-archive run, or one from `--archive-file`): it compiles nothing, and beneath a
35
+ * reservation mbx schedules none of its test processes, so they run inside the one grant. Admitted one by one under
36
+ * `MBX_SCHEDULER_TESTS=1`, each queued for a ticket of its own: in a downstream gate on 2026-10-30, codec tests of at
37
+ * most 0.25 s took 73.5 s apiece, and their 121 tests run in 1.18 s unqueued (specs/cowshed/05_gateway.md,
38
+ * "Parallelism inside a job").
26
39
  */
27
40
  export type MbxLeaf =
28
41
  | { readonly kind: 'cargo' }
29
- | { readonly kind: 'reserve'; readonly cpus: number; readonly priority: 'normal' | 'low' };
42
+ | {
43
+ readonly kind: 'reserve';
44
+ readonly priority: 'normal' | 'low';
45
+ readonly demand: ReservedDemand;
46
+ };
47
+
48
+ /** A nextest invocation that runs test binaries already built, from extracted archive metadata or an archive file. */
49
+ const PREBUILT_NEXTEST = /(?<![\w-])--(?:binaries-metadata|archive-file)(?=[=\s]|$)/;
50
+
51
+ /** Whether `command` runs nextest over test binaries already built, which compiles nothing. */
52
+ export function runsPrebuiltNextest(command: string): boolean {
53
+ return PREBUILT_NEXTEST.test(command);
54
+ }
55
+
56
+ /**
57
+ * A Vitest or Go test leaf enters no pool, `null` (owner's ruling, 2026-10-31): it holds nothing and runs its own
58
+ * default workers, uncapped. Reserved like any other leaf, it asked for that default, which sizes to the host: Vitest one
59
+ * worker per core but one, Go one package per core. So each held 17 or 18 of an 18-core pool, and mbx's ordered admission
60
+ * kept every reservation queued behind it waiting while the pool drained. On 2026-10-31 a downstream gate ran 4.9 of 18
61
+ * cores busy while 25 of its tasks queued a minute or more, and a gate's Go test leaf waited 401 s for all 18 cores.
62
+ */
63
+ export function mbxLeaf(pool: MbxPool, demand: Demand, command: string): MbxLeaf | null {
64
+ const reserve = (reserved: ReservedDemand): MbxLeaf => ({
65
+ kind: 'reserve',
66
+ priority: pool.priority,
67
+ demand: reserved,
68
+ });
69
+ switch (demand.kind) {
70
+ case 'vitest':
71
+ case 'go-test':
72
+ return null;
73
+ case 'cargo':
74
+ case 'cargo-mutants':
75
+ return { kind: 'cargo' };
76
+ case 'nextest':
77
+ return runsPrebuiltNextest(command) ? reserve(demand) : { kind: 'cargo' };
78
+ case 'bun-parallel':
79
+ case 'stryker':
80
+ case 'process':
81
+ return reserve(demand);
82
+ }
83
+ }
84
+
85
+ /** Nx's own default count of tasks at once, when nothing names one. */
86
+ const NX_DEFAULT_PARALLEL = 3;
30
87
 
31
- const CARGO_LEAVES: ReadonlySet<RunnerKind> = new Set<RunnerKind>(['cargo', 'cargo-mutants', 'nextest']);
88
+ /**
89
+ * The cores one task of an Nx run counts as its own: the host's cores divided among the tasks Nx runs at once, at
90
+ * least one. Nx reads that count from `NX_PARALLEL`, a number or a share of the cores (`100%`), else runs three.
91
+ *
92
+ * A prebuilt nextest leaf asks for this share rather than its runner's own count, every core by default, because it
93
+ * does not know how many tests it runs: mbx grants a reservation as many of its cores as are free, so a leaf of a few
94
+ * long tests asking for every core would hold the idle rest for its whole run (specs/cowshed/05_gateway.md, "Open
95
+ * questions"). Before mbx granted reservations their free cores, every cargo-test leaf of a gate reserved the whole
96
+ * pool and they ran one at a time: on 2026-10-30 a gate's 18-core host ran 4.2 cores busy, its leaves each holding
97
+ * `mbx reserve --cpus 18`.
98
+ */
99
+ export function nxTaskCores(cores: number, environment: Readonly<NodeJS.ProcessEnv>): number {
100
+ const named = environment.NX_PARALLEL?.trim() ?? '';
101
+ const share = /^(\d+)%$/.exec(named);
102
+ const count = share?.[1] !== undefined ? (cores * Number(share[1])) / 100 : /^\d+$/.test(named) ? Number(named) : 0;
103
+ const parallel = count >= 1 ? Math.floor(count) : NX_DEFAULT_PARALLEL;
104
+ return Math.max(1, Math.floor(cores / parallel));
105
+ }
106
+ /**
107
+ * The value an in-place runner gives `--target-dir-remap`: smoo-nx-nextest-binaries' answer, quoted. The plugin infers
108
+ * the native helper; a repository's own runner may name its script through bun, which prints the same path.
109
+ */
110
+ const INFERRED_TARGET_DIR_REMAP =
111
+ /--target-dir-remap "\$\((?:bun )?[^\s"'$`;&|()<>\\]+\/smoo-nx-nextest-binaries(?:\.js)? target-dir\)"/g;
32
112
 
33
- export function mbxLeaf(pool: MbxPool, kind: RunnerKind, want: number): MbxLeaf {
34
- return CARGO_LEAVES.has(kind)
35
- ? { kind: 'cargo' }
36
- : { kind: 'reserve', cpus: Math.max(1, want), priority: pool.priority };
113
+ /**
114
+ * Only a known scheduled execution may stop its idle clock at zero holds. Plain Cargo tests can run doctests;
115
+ * archive-file targets, direct binaries and other runners are not proven scheduled at spawn (11_shell.md,
116
+ * "Cargo test-runner boundaries"). The inferred in-place nextest command is a supported Cargo invocation.
117
+ */
118
+ export function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment: Readonly<NodeJS.ProcessEnv>): boolean {
119
+ if (leaf === null) return false;
120
+ if (leaf.kind === 'reserve') return true;
121
+ if (environment.MBX_SCHEDULER_TESTS !== '1') return false;
122
+ if (environment.MBX_SCHEDULER === '0' || environment.MBX_SCHED_DISABLE === '1' || environment.MBX_DISABLE === '1')
123
+ return false;
124
+ // The inferred in-place runner names this checkout's target directory through the plugin's helper: one quoted
125
+ // substitution, the value of `--target-dir-remap`, that prints a path and runs nothing mbx schedules.
126
+ const known = command.replace(INFERRED_TARGET_DIR_REMAP, '--target-dir-remap target');
127
+ // invocationSpan shares the lifecycle mapper's conservative shell-syntax boundary.
128
+ if (/[<>\n\r`\\]/.test(known)) return false;
129
+ if (/[^\S \t]/.test(known)) return false;
130
+ const invocation = /cargo(?:\s+(?:--frozen|--locked|--offline))*\s+(nextest\s+run|test)(?=\s|$)/;
131
+ const found = invocation.exec(known);
132
+ if (found === null || known.slice(0, found.index).trim() !== '') return false;
133
+ let remaining = known.slice(found.index);
134
+ const unquoted = remaining.replace(/"[^"]*"|'[^']*'/g, '');
135
+ if (/[$*?{}~[\]]/.test(unquoted)) return false;
136
+ // Only the inferred command's path templates are known expansions; arbitrary words could supply mbx overrides.
137
+ if (remaining.replace(/\$PWD(?=\/)/g, '').includes('$')) return false;
138
+ for (;;) {
139
+ const leg = invocation.exec(remaining);
140
+ if (leg === null || leg.index !== 0) return false;
141
+ const { argsEnd } = invocationSpan(remaining, invocation);
142
+ const args: string[] = [];
143
+ for (const token of shellTokens(remaining.slice(0, argsEnd), [])) {
144
+ if (token.kind !== 'word') return false;
145
+ if (token.text === '--') break;
146
+ if (token.text.startsWith('$')) return false;
147
+ args.push(token.text);
148
+ }
149
+ if (
150
+ args.some((arg) =>
151
+ /^(?:--(?:config|directory|archive-file)(?:=|$)|--(?:doc|no-run|list|help)(?:=|$)|-C|-h$|\+)/.test(arg),
152
+ )
153
+ ) {
154
+ return false;
155
+ }
156
+ if (
157
+ leg[1] === 'test' &&
158
+ !args.some((arg) => /^--(?:lib|tests|bins|examples|benches|all-targets|test|bin|example|bench)(?:=|$)/.test(arg))
159
+ ) {
160
+ return false;
161
+ }
162
+ const suffix = remaining.slice(argsEnd).trim();
163
+ if (suffix === '') return true;
164
+ const separator = /^(?:&&|\|\||;)\s*/.exec(suffix);
165
+ if (separator === null) return false;
166
+ remaining = suffix.slice(separator[0].length);
167
+ }
37
168
  }
38
169
 
39
170
  /**
40
- * `command` as the shell runs it under its leaf's admission. `mbx` is the canonical environment's, whose realpath
41
- * cowshed's admission proved to be the host's pinned mbx.
171
+ * mbx writes one byte per admission (g) and permit drop (r). Cargo descendants inherit this descriptor through
172
+ * MBX_GRANT_FD; reserve names it with --grant-fd and closes it on exec of the external command.
173
+ */
174
+ export const MBX_GRANT_FD = 5;
175
+
176
+ /**
177
+ * `command` as the shell runs it under its leaf's admission, or as it is for a leaf in no pool (`null`). A reserved leaf
178
+ * asks for its runner's count, and its reservation first sizes the runner to what mbx granted (`grantedSizing`) and says
179
+ * so on stderr. `mbx` is the canonical environment's, whose realpath cowshed's admission proved to be the host's pinned
180
+ * mbx; it writes the grant on this descriptor itself.
42
181
  */
43
- export function withMbxLeaf(command: string, leaf: MbxLeaf): string {
44
- if (leaf.kind === 'cargo') {
182
+ export function withMbxLeaf(command: string, leaf: MbxLeaf | null): string {
183
+ if (leaf === null || leaf.kind === 'cargo') {
45
184
  return command;
46
185
  }
47
- return `mbx reserve --cpus ${leaf.cpus} --priority ${leaf.priority} -- /bin/sh -c ${shellQuote(command)}`;
186
+ const asked = leaf.demand.want;
187
+ const said = `printf 'cowshed: mbx reserve running cpus=%s asked=${asked}\\n' "$${CPU_TOKENS_ENV}" >&2; `;
188
+ return `mbx reserve --grant-fd ${MBX_GRANT_FD} --cpus ${asked} --priority ${leaf.priority} -- /bin/sh -c ${shellQuote(`${grantedSizing(leaf.demand)}${said}${command}`)}`;
48
189
  }
@@ -0,0 +1,170 @@
1
+ import type { ProcessLineage } from '@smoothbricks/cowshed/process-lineage';
2
+ import typia from 'typia';
3
+ import { type HangDiagnosticHost, runProbe } from './hang-diagnostics.js';
4
+ import { CONTROL_MS } from './lease-wait.js';
5
+
6
+ /** The additive projection of mbx stats --scheduler --json --activity that the admission observer consumes. */
7
+ interface SchedulerStats {
8
+ version: 2;
9
+ scheduler: {
10
+ activity: {
11
+ kind: string;
12
+ pid: number | null;
13
+ ticket: number | null;
14
+ queue_position: number | null;
15
+ live: boolean | null;
16
+ }[];
17
+ };
18
+ }
19
+ const validateStats = typia.createValidate<SchedulerStats>();
20
+
21
+ export interface MbxQueuedRequest {
22
+ readonly pid: number;
23
+ readonly ticket: number;
24
+ readonly queuePosition: number;
25
+ }
26
+
27
+ export type MbxQueueSnapshot =
28
+ | { readonly ok: true; readonly requests: readonly MbxQueuedRequest[]; readonly diagnostics?: readonly string[] }
29
+ | { readonly ok: false; readonly detail: string };
30
+
31
+ /** One observation, including all scoped process reads, has this monotonic control-plane budget. */
32
+ export const MBX_QUEUE_SNAPSHOT_MS = CONTROL_MS;
33
+ export const MBX_QUEUE_POLL_MS = 1_000;
34
+
35
+ interface MbxQueueHost extends Pick<HangDiagnosticHost, 'probe'> {
36
+ now(): number;
37
+ lineages(pids: readonly number[]): Promise<readonly ProcessLineage[]>;
38
+ }
39
+
40
+ /**
41
+ * Read only the public pool snapshot and the listed waiters' native per-PID lineage/ancestors. A live member of the
42
+ * task's process group, or its live descendant, corroborates a PID; a command name never does. No ps process or host
43
+ * process census is used. The leaf's environment selects the same machine-wide pool as its observation.
44
+ */
45
+ export async function snapshotMbxQueue(
46
+ rootPid: number,
47
+ environment: NodeJS.ProcessEnv,
48
+ signal: AbortSignal,
49
+ host: MbxQueueHost = {
50
+ now: () => performance.now(),
51
+ probe: (file, args, timeoutMs, abort) => runProbe(file, args, timeoutMs, abort, environment),
52
+ async lineages(pids) {
53
+ const { processLineages } = await import('@smoothbricks/cowshed/process-lineage');
54
+ return processLineages(pids);
55
+ },
56
+ },
57
+ ): Promise<MbxQueueSnapshot> {
58
+ const now = () => host.now();
59
+ const endsAt = now() + MBX_QUEUE_SNAPSHOT_MS;
60
+ const probe = async (file: string, args: readonly string[]) => {
61
+ const remaining = endsAt - now();
62
+ if (remaining <= 0 || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' } as const;
63
+ const result = await host.probe(file, args, remaining, signal);
64
+ if (now() >= endsAt || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' } as const;
65
+ const command = `${file} ${args.join(' ')}`;
66
+ if (result.status !== 'exited') {
67
+ return { ok: false, detail: `${command}: ${result.status}: ${result.detail}; stderr=${result.stderr}` } as const;
68
+ }
69
+ if (result.code !== 0) {
70
+ return { ok: false, detail: `${command}: exit=${result.code}; stderr=${result.stderr}` } as const;
71
+ }
72
+ if (result.stderr.trim() !== '') return { ok: false, detail: `${command}: stderr=${result.stderr}` } as const;
73
+ return { ok: true, stdout: result.stdout } as const;
74
+ };
75
+ const stats = await probe('mbx', ['stats', '--scheduler', '--json', '--activity']);
76
+ if (!stats.ok) return stats;
77
+ let wire: unknown;
78
+ try {
79
+ wire = JSON.parse(stats.stdout);
80
+ } catch (error) {
81
+ return { ok: false, detail: `mbx stats --scheduler --json --activity: invalid JSON: ${String(error)}` };
82
+ }
83
+ const validated = validateStats(wire);
84
+ if (!validated.success) {
85
+ return {
86
+ ok: false,
87
+ detail: `mbx stats --scheduler --json --activity: invalid scheduler.activity: ${JSON.stringify(validated.errors)}`,
88
+ };
89
+ }
90
+ const waiters = validated.data.scheduler.activity.filter((row) => row.kind === 'waiter' && row.live !== false);
91
+ const diagnostics: string[] = [];
92
+ const candidates = waiters.flatMap((row) => {
93
+ const pid = row.pid;
94
+ if (pid === null || !Number.isSafeInteger(pid) || pid < 1) {
95
+ diagnostics.push(
96
+ 'mbx stats: pending waiter has no valid PID; task membership unavailable, not queue or admission evidence',
97
+ );
98
+ return [];
99
+ }
100
+ return [{ ...row, pid }];
101
+ });
102
+ if (now() >= endsAt || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' };
103
+ if (candidates.length === 0) return { ok: true, requests: [], ...(diagnostics.length === 0 ? {} : { diagnostics }) };
104
+ const table = new Map<number, ProcessLineage>();
105
+ const inspected = new Set<number>();
106
+ let pending = [...new Set([rootPid, ...candidates.map((row) => row.pid)])];
107
+ while (pending.length > 0) {
108
+ if (now() >= endsAt || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' };
109
+ let entries: readonly ProcessLineage[];
110
+ try {
111
+ entries = await host.lineages(pending);
112
+ } catch (error) {
113
+ return {
114
+ ok: false,
115
+ detail: `admission snapshot: native process lineages: ${error instanceof Error ? error.message : String(error)}`,
116
+ };
117
+ }
118
+ if (now() >= endsAt || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' };
119
+ for (const pid of pending) inspected.add(pid);
120
+ for (const entry of entries) table.set(entry.pid, entry);
121
+ pending = [
122
+ ...new Set(
123
+ [...table.values()]
124
+ .filter(
125
+ (entry) =>
126
+ !entry.zombie &&
127
+ entry.pgid !== rootPid &&
128
+ entry.pid !== rootPid &&
129
+ entry.ppid > 1 &&
130
+ !inspected.has(entry.ppid),
131
+ )
132
+ .map((entry) => entry.ppid),
133
+ ),
134
+ ];
135
+ }
136
+ const belongs = (pid: number): boolean => {
137
+ const seen = new Set<number>();
138
+ for (let entry = table.get(pid); entry !== undefined && !seen.has(entry.pid); entry = table.get(entry.ppid)) {
139
+ if (entry.zombie) return false;
140
+ if (entry.pid === rootPid || entry.pgid === rootPid) return true;
141
+ seen.add(entry.pid);
142
+ }
143
+ return false;
144
+ };
145
+ const requests: MbxQueuedRequest[] = [];
146
+ for (const row of candidates) {
147
+ if (!belongs(row.pid)) continue;
148
+ if (row.live === null)
149
+ return {
150
+ ok: false,
151
+ detail: `mbx stats --scheduler --json --activity: waiter pid=${row.pid} live status unavailable`,
152
+ };
153
+ if (
154
+ row.ticket === null ||
155
+ !Number.isSafeInteger(row.ticket) ||
156
+ row.ticket < 0 ||
157
+ row.queue_position === null ||
158
+ !Number.isSafeInteger(row.queue_position) ||
159
+ row.queue_position < 1
160
+ ) {
161
+ return {
162
+ ok: false,
163
+ detail: `mbx stats --scheduler --json --activity: live waiter pid=${row.pid} lacks ticket/queue position`,
164
+ };
165
+ }
166
+ requests.push({ pid: row.pid, ticket: row.ticket, queuePosition: row.queue_position });
167
+ }
168
+ if (now() >= endsAt || signal.aborted) return { ok: false, detail: 'admission snapshot deadline reached' };
169
+ return { ok: true, requests, ...(diagnostics.length === 0 ? {} : { diagnostics }) };
170
+ }
@@ -1,6 +1,8 @@
1
- import typia from 'typia';
1
+ import { StringDecoder } from 'node:string_decoder';
2
+ import typia, { type tags } from 'typia';
2
3
  import { invocationSpan, withInvocationFlag } from './runner-sizing.js';
3
- import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
4
+ import type { NativeTestResult } from './test-result-writer.js';
5
+ import { BUN_TEST, GO_TEST, GO_TEST_RUNNER, NEXTEST_RUN } from './verdict.js';
4
6
 
5
7
  /**
6
8
  * What a run's output says about its forward motion: whether a test finished, and which tests the runner said
@@ -30,9 +32,10 @@ import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
30
32
  */
31
33
  export interface RunProgress {
32
34
  /** One chunk of one output stream; true when it reports a finished test, file or Go package. */
33
- scan(stream: OutputStream, chunk: string): boolean;
35
+ scan(stream: OutputStream, chunk: Buffer | string): boolean;
34
36
  /** The tests the runner reported running and has not reported finished, in the order it first named them. */
35
37
  inFlight(): string[];
38
+ error(): string | null;
36
39
  }
37
40
 
38
41
  export type OutputStream = 'stdout' | 'stderr';
@@ -42,7 +45,6 @@ const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*[A-Za-z]`, 'g');
42
45
  const FINISHED = [
43
46
  /^\s*(?:[A-Z0-9][A-Z0-9-]*\s+)+\[\s*[0-9.]+s\]\s+\(\s*\d+\/\d+\)\s+(\S.*)$/,
44
47
  /^test (\S+) \.\.\. (?:ok|FAILED|ignored)\b/,
45
- /^\((?:pass|fail|skip|todo)\) (.+)$/,
46
48
  /^\s*[✓×]\s+(\S.*?)\s+\d+ms\b/,
47
49
  /^\s*❯\s+(\S.*?)\s+\(\d+ tests?(?: \| \d+ (?:failed|skipped|todo))*\)\s+\d+ms\b/,
48
50
  /^\s*↓\s+(\S.*?)\s+\(\d+ tests?(?: \| \d+ (?:skipped|todo))+\)(?:\s+\d+ms\b|$)/,
@@ -53,21 +55,43 @@ const RUNNING = [
53
55
  /^test (\S+) has been running for over \d+ seconds$/,
54
56
  ];
55
57
  const DOTS = /^\.+$/;
58
+ const BUN_RESULT =
59
+ /^(?:\((?<status>pass|fail|skip|todo|pending)\)|(?<symbol>✓|✗|×|»|✎|…))\s+(?<name>.+?)(?: \[(?<duration>\d+(?:\.\d+)?)ms\])?$/;
60
+ const UNCAPTURED_NEXTEST = /(?<![\w-])--(?:no-?capture|debugger|tracer)(?=[=\s]|$)/;
56
61
 
57
62
  type GoTestEvent =
58
63
  | { Action: 'start'; Package: string }
59
64
  | { Action: 'run' | 'pause' | 'cont'; Package: string; Test: string }
60
- | { Action: 'pass' | 'fail' | 'skip' | 'bench'; Package: string; Test?: string }
65
+ | { Action: 'pass' | 'fail' | 'skip' | 'bench'; Package: string; Test?: string; Elapsed?: number }
61
66
  | { Action: 'output'; Package: string; Output: string; Test?: string };
62
- const isGoTestEvent = typia.createIs<GoTestEvent>();
67
+ type NextestTestEvent =
68
+ | { type: 'test'; event: 'started' | 'ignored'; name: string }
69
+ | {
70
+ type: 'test';
71
+ event: 'ok' | 'failed';
72
+ name: string;
73
+ exec_time: number & tags.Minimum<0>;
74
+ stdout?: string;
75
+ reason?: string;
76
+ };
77
+ type NextestSuiteEvent = { type: 'suite'; event: 'started' | 'ok' | 'failed' };
78
+ type NativeTestEvent = GoTestEvent | NextestTestEvent | NextestSuiteEvent;
79
+ const isNativeTestEvent = typia.createIs<NativeTestEvent>();
63
80
 
64
- function goTestEvent(text: string): GoTestEvent | null {
81
+ function nativeTestEvent(text: string, invalid?: (detail: string) => void): NativeTestEvent | null {
65
82
  if (!text.startsWith('{')) return null;
66
83
  try {
67
84
  const event: unknown = JSON.parse(text);
68
- return isGoTestEvent(event) ? event : null;
85
+ if (isNativeTestEvent(event)) return event;
86
+ if (typeof event === 'object' && event !== null && 'type' in event && event.type === 'test') {
87
+ invalid?.(`invalid native test record: ${text}`);
88
+ }
89
+ return null;
69
90
  } catch (error) {
70
- if (error instanceof SyntaxError) return null;
91
+ if (error instanceof SyntaxError) {
92
+ invalid?.(`invalid native test JSON: ${error.message}`);
93
+ return null;
94
+ }
71
95
  throw error;
72
96
  }
73
97
  }
@@ -75,8 +99,8 @@ function goTestEvent(text: string): GoTestEvent | null {
75
99
  /** A direct Go chain reports tests only through JSON; other commands may also carry text runner reports. */
76
100
  function onlyGoTests(command: string): boolean {
77
101
  let rest = command.trim();
78
- while (rest.search(GO_TEST) === 0) {
79
- rest = rest.slice(invocationSpan(rest, GO_TEST).argsEnd).trimStart();
102
+ while (rest.search(GO_TEST_RUNNER) === 0) {
103
+ rest = rest.slice(invocationSpan(rest, GO_TEST_RUNNER).argsEnd).trimStart();
80
104
  if (rest === '') return true;
81
105
  const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
82
106
  if (separator === null) return false;
@@ -91,19 +115,43 @@ interface LineState {
91
115
  countedDots: number;
92
116
  }
93
117
 
94
- export function createRunProgress(command = ''): RunProgress {
118
+ export function createRunProgress(command = '', closed?: (result: NativeTestResult) => void): RunProgress {
95
119
  const goLifecycle = onlyGoTests(command);
120
+ const goResults = command.search(GO_TEST_RUNNER) !== -1;
96
121
  const streams: Record<OutputStream, LineState> = {
97
122
  stdout: { partial: '', countedDots: 0 },
98
123
  stderr: { partial: '', countedDots: 0 },
99
124
  };
100
125
  const running = new Set<string>();
101
126
  const packages = new Map<string, Set<string>>();
127
+ const goOutput = new Map<string, string[]>();
128
+ const decoders: Record<OutputStream, StringDecoder> = {
129
+ stdout: new StringDecoder('utf8'),
130
+ stderr: new StringDecoder('utf8'),
131
+ };
132
+ const nextestStarted = new Set<string>();
133
+ const nextestJson =
134
+ /--message-format(?:=|\s+)libtest-json-plus(?=\s|$)/.test(command) && !UNCAPTURED_NEXTEST.test(command);
135
+ let failure: string | null = null;
136
+ const bunResults = command.search(BUN_TEST) !== -1 && command.search(NEXTEST_RUN) === -1;
137
+ let bunFile = '';
138
+ let bunSummary = false;
139
+ const bunDiagnostics: Record<OutputStream, string[]> = { stdout: [], stderr: [] };
102
140
 
103
141
  // One complete line, ANSI stripped, that is not a line of dots: true when it names a finished test.
104
- const finishedLine = (text: string): boolean => {
105
- const event = goTestEvent(text);
106
- if (event !== null) {
142
+ const finishedLine = (text: string, stream: OutputStream): boolean => {
143
+ const event =
144
+ stream === 'stdout'
145
+ ? nativeTestEvent(
146
+ text,
147
+ nextestJson
148
+ ? (detail) => {
149
+ failure ??= detail;
150
+ }
151
+ : undefined,
152
+ )
153
+ : null;
154
+ if (event !== null && 'Action' in event) {
107
155
  switch (event.Action) {
108
156
  case 'start':
109
157
  packages.set(event.Package, new Set());
@@ -116,15 +164,84 @@ export function createRunProgress(command = ''): RunProgress {
116
164
  case 'skip':
117
165
  case 'bench':
118
166
  if (event.Test === undefined) packages.delete(event.Package);
119
- else packages.get(event.Package)?.delete(event.Test);
167
+ else {
168
+ packages.get(event.Package)?.delete(event.Test);
169
+ const id = `${event.Package}::${event.Test}`;
170
+ if (event.Action !== 'bench' && goResults)
171
+ closed?.({
172
+ id,
173
+ status: event.Action,
174
+ source: 'native',
175
+ durationMs: event.Elapsed === undefined ? null : event.Elapsed * 1000,
176
+ sourceRef: null,
177
+ failureOutput: event.Action === 'fail' ? (goOutput.get(id)?.join('') ?? null) : null,
178
+ });
179
+ goOutput.delete(id);
180
+ }
120
181
  return true;
121
182
  case 'pause':
122
183
  case 'cont':
184
+ return false;
123
185
  case 'output':
186
+ if (goResults && closed !== undefined && event.Test !== undefined) {
187
+ const id = `${event.Package}::${event.Test}`;
188
+ const output = goOutput.get(id) ?? [];
189
+ output.push(event.Output);
190
+ goOutput.set(id, output);
191
+ }
124
192
  return false;
125
193
  }
126
194
  }
195
+ if (nextestJson && event !== null && 'type' in event && event.type === 'test') {
196
+ if (event.event === 'started') {
197
+ nextestStarted.add(event.name);
198
+ return false;
199
+ }
200
+ // nextest appends #N only after retries; a literal #N in the original name stays intact.
201
+ const name = nextestStarted.has(event.name) ? event.name : event.name.replace(/#\d+$/, '');
202
+ nextestStarted.delete(name);
203
+ const split = name.indexOf('$');
204
+ if (nextestJson && split !== -1)
205
+ closed?.({
206
+ id: `${name.slice(0, split)}::${name.slice(split + 1)}`,
207
+ status: event.event === 'ok' ? 'pass' : event.event === 'failed' ? 'fail' : 'skip',
208
+ source: 'native',
209
+ durationMs: 'exec_time' in event ? event.exec_time * 1000 : null,
210
+ sourceRef: null,
211
+ failureOutput: event.event === 'failed' ? (event.stdout ?? event.reason ?? null) : null,
212
+ });
213
+ return true;
214
+ }
127
215
  if (goLifecycle) return false;
216
+ const bun = BUN_RESULT.exec(text)?.groups;
217
+ if (
218
+ !bunSummary &&
219
+ bun !== undefined &&
220
+ bun.name !== undefined &&
221
+ ((bunResults && stream === 'stderr') || (bun.status !== undefined && bun.status !== 'pending'))
222
+ ) {
223
+ const status =
224
+ bun.status === 'fail' ||
225
+ bun.status === 'pending' ||
226
+ bun.symbol === '…' ||
227
+ bun.symbol === '✗' ||
228
+ bun.symbol === '×'
229
+ ? 'fail'
230
+ : bun.status === 'skip' || bun.status === 'todo' || bun.symbol === '»' || bun.symbol === '✎'
231
+ ? 'skip'
232
+ : 'pass';
233
+ if (bunResults && stream === 'stderr')
234
+ closed?.({
235
+ id: `${bunFile}::${bun.name}`,
236
+ status,
237
+ source: 'progress',
238
+ durationMs: bun.duration === undefined ? null : Number(bun.duration),
239
+ sourceRef: null,
240
+ failureOutput: status === 'fail' ? bunDiagnostics[stream].join('') : null,
241
+ });
242
+ bunDiagnostics[stream] = [];
243
+ return true;
244
+ }
128
245
  for (const pattern of FINISHED) {
129
246
  const name = pattern.exec(text)?.[1];
130
247
  if (name !== undefined) {
@@ -145,18 +262,28 @@ export function createRunProgress(command = ''): RunProgress {
145
262
  return {
146
263
  scan(stream, chunk) {
147
264
  const state = streams[stream];
148
- const lines = (state.partial + chunk).split('\n');
265
+ const decoded = typeof chunk === 'string' ? chunk : decoders[stream].write(chunk);
266
+ const lines = (state.partial + decoded).split('\n');
149
267
  const rest = lines.pop() ?? '';
150
268
  let progressed = false;
151
269
  // The first complete line continues the partial one, whose dots were counted as they came.
152
270
  let counted = state.countedDots;
153
271
  for (const raw of lines) {
154
272
  const text = raw.replace(ANSI, '').trimEnd();
273
+ if (bunResults && stream === 'stderr' && closed !== undefined) {
274
+ const header = /^(?:::group::)?(\S.*\.[cm]?[jt]sx?):(?: \(run #\d+\))?$/.exec(text)?.[1];
275
+ if (header !== undefined) {
276
+ bunFile = header.replace(/^\.\//, '');
277
+ bunSummary = false;
278
+ }
279
+ if (/^\s*\d+ tests (?:skipped|todo|failed):$/.test(text)) bunSummary = true;
280
+ if (!bunSummary) bunDiagnostics[stream].push(`${raw}\n`);
281
+ }
155
282
  if (DOTS.test(text)) {
156
283
  if (!goLifecycle && DOTS.test(text)) {
157
284
  progressed ||= text.length > counted;
158
285
  }
159
- } else if (finishedLine(text)) {
286
+ } else if (finishedLine(text, stream)) {
160
287
  progressed = true;
161
288
  }
162
289
  counted = 0;
@@ -168,6 +295,7 @@ export function createRunProgress(command = ''): RunProgress {
168
295
  state.countedDots = dots;
169
296
  return progressed;
170
297
  },
298
+ error: () => failure,
171
299
  inFlight: () => [
172
300
  ...running,
173
301
  ...[...packages].flatMap(([name, tests]) =>
@@ -177,6 +305,31 @@ export function createRunProgress(command = ''): RunProgress {
177
305
  };
178
306
  }
179
307
 
308
+ /** JSON-plus 0.1 flushes each closed result; plain libtest-json buffers a whole binary. */
309
+ export function withTestResultReport(command: string): { command: string; env: Record<string, string> } {
310
+ if (
311
+ (command.match(NEXTEST_RUN)?.length ?? 0) !== 1 ||
312
+ command.search(BUN_TEST) !== -1 ||
313
+ command.search(GO_TEST_RUNNER) !== -1 ||
314
+ UNCAPTURED_NEXTEST.test(command)
315
+ )
316
+ return { command, env: {} };
317
+ let reported = withInvocationFlag(
318
+ command,
319
+ NEXTEST_RUN,
320
+ /(?<![\w-])--message-format(?:=|\s+)(?:"[^"]*"|'[^']*'|[^\s]+)/g,
321
+ '--message-format=libtest-json-plus',
322
+ true,
323
+ );
324
+ reported = withInvocationFlag(
325
+ reported,
326
+ NEXTEST_RUN,
327
+ /(?<![\w-])--message-format-version(?:=|\s+)(?:"[^"]*"|'[^']*'|[^\s]+)/g,
328
+ '--message-format-version=0.1',
329
+ true,
330
+ );
331
+ return { command: reported, env: { NEXTEST_EXPERIMENTAL_LIBTEST_JSON: '1' } };
332
+ }
180
333
  /**
181
334
  * `command` with its one `bun test` asked for a line of dots, one per finished test: the only per-test report bun
182
335
  * prints whoever runs it. A command that runs no bun, more than one runner, or already chose bun's dots is returned