@smoothbricks/nx-plugin 0.7.17 → 0.7.19

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 (199) hide show
  1. package/README.md +315 -114
  2. package/dist/.build-inputs.json +47 -38
  3. package/dist/built-output-packages.js +1 -1
  4. package/dist/bun-lock-scope.d.ts +128 -20
  5. package/dist/bun-lock-scope.d.ts.map +1 -1
  6. package/dist/bun-lock-scope.js +267 -44
  7. package/dist/cargo-closure-input.d.ts +8 -6
  8. package/dist/cargo-closure-input.d.ts.map +1 -1
  9. package/dist/cargo-closure-input.js +15 -12
  10. package/dist/cargo-source-hash.d.ts +26 -25
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +84 -105
  13. package/dist/cargo-toolchain-paths.d.ts +2 -2
  14. package/dist/cargo-toolchain-paths.js +2 -2
  15. package/dist/cargo-workspace.d.ts +20 -2
  16. package/dist/cargo-workspace.d.ts.map +1 -1
  17. package/dist/cargo-workspace.js +43 -22
  18. package/dist/ensure-built-receipt.d.ts +3 -2
  19. package/dist/ensure-built-receipt.d.ts.map +1 -1
  20. package/dist/ensure-built-receipt.js +3 -2
  21. package/dist/eslint-config-closure.d.ts +8 -3
  22. package/dist/eslint-config-closure.d.ts.map +1 -1
  23. package/dist/eslint-config-closure.js +24 -5
  24. package/dist/executors/bounded-exec/admission-queue.d.ts +3 -1
  25. package/dist/executors/bounded-exec/admission-queue.d.ts.map +1 -1
  26. package/dist/executors/bounded-exec/admission-queue.js +9 -10
  27. package/dist/executors/bounded-exec/awake-clock.d.ts +5 -0
  28. package/dist/executors/bounded-exec/awake-clock.d.ts.map +1 -0
  29. package/dist/executors/bounded-exec/awake-clock.js +22 -0
  30. package/dist/executors/bounded-exec/bounds.d.ts +19 -6
  31. package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
  32. package/dist/executors/bounded-exec/bounds.js +22 -15
  33. package/dist/executors/bounded-exec/case-gate.d.ts +114 -0
  34. package/dist/executors/bounded-exec/case-gate.d.ts.map +1 -0
  35. package/dist/executors/bounded-exec/case-gate.js +342 -0
  36. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +1 -0
  37. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
  38. package/dist/executors/bounded-exec/cpu-pool-thread.js +2 -2
  39. package/dist/executors/bounded-exec/cpu-pool.d.ts +28 -1
  40. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  41. package/dist/executors/bounded-exec/cpu-pool.js +102 -16
  42. package/dist/executors/bounded-exec/cpu-rise.d.ts +73 -0
  43. package/dist/executors/bounded-exec/cpu-rise.d.ts.map +1 -0
  44. package/dist/executors/bounded-exec/cpu-rise.js +111 -0
  45. package/dist/executors/bounded-exec/endpoint-life.d.ts +146 -0
  46. package/dist/executors/bounded-exec/endpoint-life.d.ts.map +1 -0
  47. package/dist/executors/bounded-exec/endpoint-life.js +228 -0
  48. package/dist/executors/bounded-exec/executor.d.ts +1 -1
  49. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  50. package/dist/executors/bounded-exec/executor.js +256 -86
  51. package/dist/executors/bounded-exec/gateway-lease.d.ts +1 -1
  52. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -1
  53. package/dist/executors/bounded-exec/gateway-lease.js +8 -1
  54. package/dist/executors/bounded-exec/go-run-summary.d.ts +51 -0
  55. package/dist/executors/bounded-exec/go-run-summary.d.ts.map +1 -0
  56. package/dist/executors/bounded-exec/go-run-summary.js +98 -0
  57. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +11 -3
  58. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
  59. package/dist/executors/bounded-exec/hang-diagnostics.js +125 -47
  60. package/dist/executors/bounded-exec/lease-wait.d.ts +1 -0
  61. package/dist/executors/bounded-exec/lease-wait.d.ts.map +1 -1
  62. package/dist/executors/bounded-exec/lease-wait.js +1 -0
  63. package/dist/executors/bounded-exec/mbx-pool.d.ts +42 -7
  64. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  65. package/dist/executors/bounded-exec/mbx-pool.js +45 -8
  66. package/dist/executors/bounded-exec/mbx-reservations.d.ts +12 -6
  67. package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -1
  68. package/dist/executors/bounded-exec/mbx-reservations.js +190 -284
  69. package/dist/executors/bounded-exec/parent-grant.d.ts +2 -0
  70. package/dist/executors/bounded-exec/parent-grant.d.ts.map +1 -1
  71. package/dist/executors/bounded-exec/parent-grant.js +1 -0
  72. package/dist/executors/bounded-exec/pool-admission.d.ts +29 -3
  73. package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -1
  74. package/dist/executors/bounded-exec/pool-admission.js +47 -1
  75. package/dist/executors/bounded-exec/progress-channel.d.ts +38 -0
  76. package/dist/executors/bounded-exec/progress-channel.d.ts.map +1 -0
  77. package/dist/executors/bounded-exec/progress-channel.js +113 -0
  78. package/dist/executors/bounded-exec/progress.d.ts +5 -1
  79. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  80. package/dist/executors/bounded-exec/progress.js +31 -1
  81. package/dist/executors/bounded-exec/ram-temp.d.ts +24 -1
  82. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  83. package/dist/executors/bounded-exec/ram-temp.js +123 -16
  84. package/dist/executors/bounded-exec/reservation-connection.d.ts +90 -0
  85. package/dist/executors/bounded-exec/reservation-connection.d.ts.map +1 -0
  86. package/dist/executors/bounded-exec/reservation-connection.js +248 -0
  87. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  88. package/dist/executors/bounded-exec/run-cpu-pool.js +3 -0
  89. package/dist/executors/bounded-exec/runner-sizing.d.ts +9 -5
  90. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
  91. package/dist/executors/bounded-exec/runner-sizing.js +13 -9
  92. package/dist/executors/bounded-exec/schema.d.ts +7 -2
  93. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  94. package/dist/executors/bounded-exec/schema.js +17 -1
  95. package/dist/executors/bounded-exec/suite-minimum.d.ts +21 -0
  96. package/dist/executors/bounded-exec/suite-minimum.d.ts.map +1 -0
  97. package/dist/executors/bounded-exec/suite-minimum.js +85 -0
  98. package/dist/executors/bounded-exec/test-result-writer.d.ts +2 -0
  99. package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -1
  100. package/dist/executors/bounded-exec/test-result-writer.js +1 -0
  101. package/dist/executors/bounded-exec/test-results.d.ts +5 -0
  102. package/dist/executors/bounded-exec/test-results.d.ts.map +1 -1
  103. package/dist/executors/bounded-exec/test-results.js +2 -2
  104. package/dist/executors/bounded-exec/verdict.d.ts +39 -10
  105. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  106. package/dist/executors/bounded-exec/verdict.js +60 -14
  107. package/dist/executors/cargo-zigbuild/executor.d.ts +10 -8
  108. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -1
  109. package/dist/executors/cargo-zigbuild/executor.js +32 -19
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +294 -152
  112. package/dist/managed-files/ci-workflow.d.ts +4 -1
  113. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  114. package/dist/managed-files/ci-workflow.js +9 -14
  115. package/dist/managed-files/files.d.ts +3 -3
  116. package/dist/managed-files/files.js +3 -3
  117. package/dist/managed-files/generator.d.ts +5 -1
  118. package/dist/managed-files/generator.d.ts.map +1 -1
  119. package/dist/managed-files/generator.js +22 -7
  120. package/dist/managed-files/render-memo.d.ts +13 -0
  121. package/dist/managed-files/render-memo.d.ts.map +1 -0
  122. package/dist/managed-files/render-memo.js +94 -0
  123. package/dist/task-results.js +2 -1
  124. package/dist/test-program.d.ts +11 -6
  125. package/dist/test-program.d.ts.map +1 -1
  126. package/dist/test-program.js +39 -35
  127. package/dist/tool-bins.d.ts +12 -0
  128. package/dist/tool-bins.d.ts.map +1 -1
  129. package/dist/tool-bins.js +31 -6
  130. package/dist/typecheck-test-policy.d.ts +10 -1
  131. package/dist/typecheck-test-policy.d.ts.map +1 -1
  132. package/dist/typecheck-test-policy.js +40 -24
  133. package/dist/workspace-config-policy.d.ts.map +1 -1
  134. package/dist/workspace-config-policy.js +135 -42
  135. package/dist/workspace-index.d.ts +14 -1
  136. package/dist/workspace-index.d.ts.map +1 -1
  137. package/dist/workspace-index.js +36 -4
  138. package/executors.json +2 -0
  139. package/managed/raw/envrc +24 -0
  140. package/managed/raw/patches/nx@24.0.3.patch +1851 -221
  141. package/managed/raw/patches/ttsc@0.37.1.patch +1055 -0
  142. package/managed/raw/tooling/direnv/developer-links.ts +122 -14
  143. package/managed/raw/tooling/direnv/devenv.smoo.nix +20 -19
  144. package/managed/templates/github/actions/save-nix-devenv/action.yml +19 -1
  145. package/managed/templates/github/actions/setup-devenv/action.yml +2 -0
  146. package/package.json +68 -11
  147. package/runtime/nx-publish/.build-inputs.json +2 -2
  148. package/runtime/nx-publish/cache_adapter.js +293 -3
  149. package/runtime/nx-publish/nx_publish.js +36 -14
  150. package/src/built-output-packages.ts +1 -1
  151. package/src/bun-lock-scope.ts +293 -40
  152. package/src/cargo-closure-input.ts +20 -12
  153. package/src/cargo-source-hash.ts +119 -145
  154. package/src/cargo-toolchain-paths.ts +2 -2
  155. package/src/cargo-workspace.ts +59 -21
  156. package/src/ensure-built-receipt.ts +3 -2
  157. package/src/eslint-config-closure.ts +27 -6
  158. package/src/executors/bounded-exec/admission-queue.ts +11 -10
  159. package/src/executors/bounded-exec/awake-clock.ts +20 -0
  160. package/src/executors/bounded-exec/bounds.ts +41 -19
  161. package/src/executors/bounded-exec/case-gate.ts +365 -0
  162. package/src/executors/bounded-exec/cpu-pool-thread.ts +3 -3
  163. package/src/executors/bounded-exec/cpu-pool.ts +139 -12
  164. package/src/executors/bounded-exec/cpu-rise.ts +156 -0
  165. package/src/executors/bounded-exec/endpoint-life.ts +279 -0
  166. package/src/executors/bounded-exec/executor.ts +341 -123
  167. package/src/executors/bounded-exec/gateway-lease.ts +6 -0
  168. package/src/executors/bounded-exec/go-run-summary.ts +137 -0
  169. package/src/executors/bounded-exec/hang-diagnostics.ts +150 -50
  170. package/src/executors/bounded-exec/lease-wait.ts +1 -0
  171. package/src/executors/bounded-exec/mbx-pool.ts +57 -9
  172. package/src/executors/bounded-exec/mbx-reservations.ts +216 -183
  173. package/src/executors/bounded-exec/parent-grant.ts +3 -0
  174. package/src/executors/bounded-exec/pool-admission.ts +59 -2
  175. package/src/executors/bounded-exec/progress-channel.ts +129 -0
  176. package/src/executors/bounded-exec/progress.ts +39 -3
  177. package/src/executors/bounded-exec/ram-temp.ts +137 -17
  178. package/src/executors/bounded-exec/reservation-connection.ts +221 -0
  179. package/src/executors/bounded-exec/run-cpu-pool.ts +2 -0
  180. package/src/executors/bounded-exec/runner-sizing.ts +13 -8
  181. package/src/executors/bounded-exec/schema.json +1 -1
  182. package/src/executors/bounded-exec/schema.ts +21 -2
  183. package/src/executors/bounded-exec/suite-minimum.ts +81 -0
  184. package/src/executors/bounded-exec/test-result-writer.ts +3 -0
  185. package/src/executors/bounded-exec/test-results.ts +5 -0
  186. package/src/executors/bounded-exec/verdict.ts +83 -22
  187. package/src/executors/cargo-zigbuild/executor.ts +30 -19
  188. package/src/index.ts +357 -166
  189. package/src/managed-files/ci-workflow.ts +13 -17
  190. package/src/managed-files/files.ts +3 -3
  191. package/src/managed-files/generator.ts +21 -6
  192. package/src/managed-files/render-memo.ts +135 -0
  193. package/src/test-program.ts +36 -41
  194. package/src/tool-bins.ts +35 -6
  195. package/src/typecheck-test-policy.ts +49 -24
  196. package/src/workspace-config-policy.ts +161 -43
  197. package/src/workspace-index.ts +40 -5
  198. package/test-timings.json +157 -141
  199. package/managed/raw/patches/ttsc@0.36.3.patch +0 -1660
@@ -0,0 +1,20 @@
1
+ /** The idle clock tracks awake time, not wall time or CPU consumed by this process. */
2
+ export async function openAwakeClock(): Promise<() => number> {
3
+ // Bun 1.4.4 uses Rust Instant, and Linux Node uses CLOCK_MONOTONIC. Darwin Node's libuv uses
4
+ // mach_continuous_time, which includes sleep; its awake reading comes from the cowshed addon.
5
+ if (process.platform !== 'darwin' || process.versions.bun !== undefined) return () => performance.now();
6
+ const { awakeTimeMs } = await import('@smoothbricks/cowshed/awake-clock');
7
+ awakeTimeMs(); // Refuse a missing or stale addon before the executor spawns anything.
8
+ return awakeTimeMs;
9
+ }
10
+
11
+ /** A timer is only a wake-up. An early callback arms the remaining time on the same clock. */
12
+ export function atClockDeadline(at: number, now: () => number, reached: () => void): () => void {
13
+ const wake = (): void => {
14
+ const remaining = at - now();
15
+ if (remaining > 0) timer = setTimeout(wake, Math.ceil(remaining));
16
+ else reached();
17
+ };
18
+ let timer = setTimeout(wake, Math.ceil(Math.max(0, at - now())));
19
+ return () => clearTimeout(timer);
20
+ }
@@ -7,7 +7,9 @@
7
7
  * ceiling starts at spawn. Later zero-hold intervals pause only the idle budget.
8
8
  * - Any explicitly queued task process pauses idle, including alongside held grants; ceilings keep running.
9
9
  * - The fixture ceiling ends with its segment. The idle clock also stops outside the testing lifecycle.
10
- * - Finished tests reset the idle budget whether their release byte arrived before or after their output.
10
+ * - Finished tests reset the idle budget whether their release byte arrived before or after their output, and so does
11
+ * progress a running test reports on the run's progress channel (progress-channel.ts). The run's CPU is no event:
12
+ * it never resets the budget.
11
13
  */
12
14
 
13
15
  /** The limits a run was given, in milliseconds; an absent one never expires. */
@@ -18,8 +20,8 @@ export interface BoundLimits {
18
20
  }
19
21
 
20
22
  /**
21
- * When each armed bound expires, in the clock's milliseconds; `null` when it is not armed. `queued`: the run waits for
22
- * its pool's grant, so none is armed yet but a fixture ceiling started at spawn (`fixtureFirst`).
23
+ * When each armed bound expires: idle on the awake clock, fixture and total on the wall clock. A null bound is not
24
+ * armed. A queued run waits for its pool's grant, so only a fixture started at spawn can have a ceiling then.
23
25
  */
24
26
  export interface Bounds {
25
27
  readonly idle: number | null;
@@ -37,18 +39,25 @@ export type BoundEvent =
37
39
  /**
38
40
  * The command started; `testing` is the lifecycle's answer then, `fixture` whether a supervised fixture leads it,
39
41
  * `queued` whether mbx schedules this leaf, as known before spawn, and `fixtureFirst` whether that fixture runs
40
- * before the leaf's reservation (it counts the leaf's units), so its ceiling starts now, not at the grant.
42
+ * before the leaf's reservation (every reserved leaf's does), so its ceiling starts now, not at the grant.
41
43
  */
42
44
  | {
43
45
  readonly kind: 'spawn';
44
46
  readonly at: number;
47
+ readonly wallAt: number;
45
48
  readonly testing: boolean;
46
49
  readonly fixture: boolean;
47
50
  readonly queued: boolean;
48
51
  readonly fixtureFirst: boolean;
49
52
  }
50
53
  /** Each admission begins a hold; the first one starts the queued leaf's ceilings. */
51
- | { readonly kind: 'granted'; readonly at: number; readonly testing: boolean; readonly fixture: boolean }
54
+ | {
55
+ readonly kind: 'granted';
56
+ readonly at: number;
57
+ readonly wallAt: number;
58
+ readonly testing: boolean;
59
+ readonly fixture: boolean;
60
+ }
52
61
  | { readonly kind: 'released'; readonly at: number }
53
62
  /** A successful public snapshot proved whether a live task process is queued; `at` is completion, not poll start. */
54
63
  | { readonly kind: 'admission-queue'; readonly at: number; readonly waiting: boolean; readonly testing: boolean }
@@ -63,6 +72,11 @@ export type BoundEvent =
63
72
  readonly wasTesting: boolean;
64
73
  readonly finished: boolean;
65
74
  }
75
+ /**
76
+ * A running test reported progress on the run's progress channel: it restarts the idle bound as a finished test
77
+ * does, and moves no phase, so `testing` is the answer before and after it.
78
+ */
79
+ | { readonly kind: 'reported'; readonly at: number; readonly testing: boolean }
66
80
  /** The fixture's segment ended: its ceiling no longer applies. */
67
81
  | { readonly kind: 'fixture-done' };
68
82
 
@@ -76,12 +90,12 @@ export const UNARMED: Bounds = {
76
90
  idleRemaining: null,
77
91
  };
78
92
 
79
- /** The bounds a command that starts running at `at` arms. */
80
- function armed(limits: BoundLimits, at: number, testing: boolean, fixture: boolean): Bounds {
93
+ /** The bounds a command arms when it starts running. */
94
+ function armed(limits: BoundLimits, event: Extract<BoundEvent, { kind: 'spawn' | 'granted' }>): Bounds {
81
95
  return {
82
- idle: testing && limits.idleMs !== undefined ? at + limits.idleMs : null,
83
- fixture: fixture && limits.fixtureMs !== undefined ? at + limits.fixtureMs : null,
84
- total: limits.totalMs !== undefined ? at + limits.totalMs : null,
96
+ idle: event.testing && limits.idleMs !== undefined ? event.at + limits.idleMs : null,
97
+ fixture: event.fixture && limits.fixtureMs !== undefined ? event.wallAt + limits.fixtureMs : null,
98
+ total: limits.totalMs !== undefined ? event.wallAt + limits.totalMs : null,
85
99
  queued: false,
86
100
  admissionWaiting: false,
87
101
  holds: null,
@@ -99,13 +113,13 @@ export function stepBounds(bounds: Bounds, limits: BoundLimits, event: BoundEven
99
113
  queued: true,
100
114
  holds: 0,
101
115
  idleRemaining: limits.idleMs ?? null,
102
- fixture: event.fixtureFirst && limits.fixtureMs !== undefined ? event.at + limits.fixtureMs : null,
116
+ fixture: event.fixtureFirst && limits.fixtureMs !== undefined ? event.wallAt + limits.fixtureMs : null,
103
117
  }
104
- : armed(limits, event.at, event.testing, event.fixture);
118
+ : armed(limits, event);
105
119
  case 'granted': {
106
120
  if (bounds.holds === null) return bounds;
107
121
  if (bounds.queued) {
108
- const admitted = armed(limits, event.at, event.testing, event.fixture);
122
+ const admitted = armed(limits, event);
109
123
  return {
110
124
  ...admitted,
111
125
  holds: 1,
@@ -151,10 +165,12 @@ export function stepBounds(bounds: Bounds, limits: BoundLimits, event: BoundEven
151
165
  idleRemaining: bounds.idle === null ? bounds.idleRemaining : Math.max(0, bounds.idle - event.at),
152
166
  };
153
167
  }
154
- case 'progress': {
155
- if (bounds.queued) return event.finished ? { ...bounds, idleRemaining: limits.idleMs ?? null } : bounds;
168
+ case 'progress':
169
+ case 'reported': {
170
+ const moved = event.kind === 'reported' || event.finished;
171
+ if (bounds.queued) return moved ? { ...bounds, idleRemaining: limits.idleMs ?? null } : bounds;
156
172
  if (!event.testing) return { ...bounds, idle: null, idleRemaining: limits.idleMs ?? null };
157
- const restart = event.finished || !event.wasTesting;
173
+ const restart = moved || (event.kind === 'progress' && !event.wasTesting);
158
174
  if (!restart || limits.idleMs === undefined) return bounds;
159
175
  return {
160
176
  ...bounds,
@@ -168,11 +184,17 @@ export function stepBounds(bounds: Bounds, limits: BoundLimits, event: BoundEven
168
184
  }
169
185
 
170
186
  /** The bound that expires first, or `null` when none is armed. A tie names the total, then the fixture, then idle. */
171
- export function earliestBound(bounds: Bounds): { readonly kind: BoundKind; readonly at: number } | null {
172
- let earliest: { kind: BoundKind; at: number } | null = null;
187
+ export function earliestBound(
188
+ bounds: Bounds,
189
+ awakeNow: number,
190
+ wallNow: number,
191
+ ): { readonly kind: BoundKind; readonly remainingMs: number } | null {
192
+ let earliest: { kind: BoundKind; remainingMs: number } | null = null;
173
193
  for (const kind of ['total', 'fixture', 'idle'] as const) {
174
194
  const at = bounds[kind];
175
- if (at !== null && (earliest === null || at < earliest.at)) earliest = { kind, at };
195
+ if (at === null) continue;
196
+ const remainingMs = at - (kind === 'idle' ? awakeNow : wallNow);
197
+ if (earliest === null || remainingMs < earliest.remainingMs) earliest = { kind, remainingMs };
176
198
  }
177
199
  return earliest;
178
200
  }
@@ -0,0 +1,365 @@
1
+ import { rm } from 'node:fs/promises';
2
+ import { createServer, type Server, type Socket } from 'node:net';
3
+ import { join } from 'node:path';
4
+ import typia from 'typia';
5
+ import { ownedSocketDirectory } from '../../testing.js';
6
+
7
+ /**
8
+ * A reserved nextest leaf's case gate (specs/cowshed/05_gateway.md, "A case starts on a core"). The leaf's runner
9
+ * starts every thread of its ask, and each case takes a core before it runs: one of the leaf's grant when one is free,
10
+ * else one reserved for that case alone, which the leaf's executor asks its run's endpoint for and gives back when the
11
+ * case ends. A grant never grows, so this is how the cores other work frees after the leaf's grant reach it.
12
+ *
13
+ * The gate is a Unix stream socket the executor serves for one run and names in the command's environment as
14
+ * {@link TEST_GATE_ENV}. mbx's test shim, through which nextest starts every case, connects before the case starts,
15
+ * writes one line ({@link CaseLine}), starts the case when the line {@link GO} answers, and keeps the connection until
16
+ * the case has exited: the connection's end is the case's end.
17
+ */
18
+ export const TEST_GATE_ENV = 'MBX_TEST_GATE';
19
+ /**
20
+ * Set to `1` in a run's environment once its endpoint answered that the installed mbx's shim gates its cases
21
+ * (endpoint-life.ts, `gatesTests`): without it a leaf's runner is sized to its grant.
22
+ */
23
+ export const TEST_GATE_SERVED_ENV = 'BOUNDED_EXEC_MBX_TEST_GATE';
24
+ /** The line that starts a case. */
25
+ export const GO = 'go';
26
+
27
+ /** The line a case's shim writes: nextest's binary id and test name, which together are the case's result id. */
28
+ interface CaseLine {
29
+ binary: string;
30
+ test: string;
31
+ }
32
+ const parseCaseLine = typia.json.createValidateParse<CaseLine>();
33
+
34
+ /** A case's id as its result names it (progress.ts): the binary id, `::`, the test. */
35
+ export function caseId(binary: string, test: string): string {
36
+ return `${binary}::${test}`;
37
+ }
38
+
39
+ /** What the gate does outside itself. */
40
+ export interface CaseGateEffects {
41
+ /** Start the case: answer its connection. */
42
+ readonly start: (id: string) => void;
43
+ /** Ask the pool for one core in the case's name. */
44
+ readonly reserve: (id: string) => void;
45
+ /** Give the case's core back to the pool, or withdraw its request while it waits. */
46
+ readonly release: (id: string) => void;
47
+ }
48
+
49
+ /** What a leaf's cases did at its gate. */
50
+ export interface CaseGateSummary {
51
+ /** Cases that ran on the leaf's grant, and on a core of their own. */
52
+ readonly own: number;
53
+ readonly lent: number;
54
+ /** How long the cases on a core of their own held it, summed. */
55
+ readonly lentCoreMs: number;
56
+ /** How long the cases waited at the gate, summed. */
57
+ readonly waitedCaseMs: number;
58
+ }
59
+
60
+ type CaseState =
61
+ | { readonly kind: 'waiting'; readonly since: number }
62
+ | { readonly kind: 'own' }
63
+ | { readonly kind: 'lent'; readonly since: number };
64
+
65
+ /**
66
+ * The gate's decisions, as a state machine over three events: a case arrived, the pool granted a case's request, a
67
+ * case ended. `own` is the leaf's granted cores; `wall` a case's last recorded wall, by which a freed core of the
68
+ * grant picks the longest waiting case, so the cores the pool gives go to the shorter ones and come back soonest.
69
+ */
70
+ export class CaseGate {
71
+ readonly #cases = new Map<string, CaseState>();
72
+ readonly #waited = new Map<string, number>();
73
+ #ownRunning = 0;
74
+ #own = 0;
75
+ #lent = 0;
76
+ #lentCoreMs = 0;
77
+ #waitedCaseMs = 0;
78
+ #ranWallMs = 0;
79
+
80
+ constructor(
81
+ private readonly own: number,
82
+ private readonly wall: (id: string) => number,
83
+ private readonly effects: CaseGateEffects,
84
+ private readonly now: () => number = Date.now,
85
+ ) {}
86
+
87
+ /** A case's shim connected. False for a case already at the gate, which its second connection may not join. */
88
+ arrived(id: string): boolean {
89
+ if (this.#cases.has(id)) return false;
90
+ if (this.#ownRunning < this.own) {
91
+ this.#startOwn(id, 0);
92
+ return true;
93
+ }
94
+ this.#cases.set(id, { kind: 'waiting', since: this.now() });
95
+ this.effects.reserve(id);
96
+ return true;
97
+ }
98
+
99
+ /** The pool granted the case's request. One whose case no longer waits gives the core straight back. */
100
+ granted(id: string): void {
101
+ const state = this.#cases.get(id);
102
+ if (state?.kind !== 'waiting') {
103
+ this.effects.release(id);
104
+ return;
105
+ }
106
+ const at = this.now();
107
+ this.#startedAfter(id, at - state.since);
108
+ this.#cases.set(id, { kind: 'lent', since: at });
109
+ this.#lent += 1;
110
+ this.effects.start(id);
111
+ }
112
+
113
+ /** A case's connection ended: its core goes back where it came from, a waiting case's request is withdrawn. */
114
+ ended(id: string): void {
115
+ const state = this.#cases.get(id);
116
+ if (state === undefined) return;
117
+ this.#cases.delete(id);
118
+ if (state.kind !== 'waiting') this.#ranWallMs += this.wall(id);
119
+ switch (state.kind) {
120
+ case 'waiting':
121
+ this.effects.release(id);
122
+ return;
123
+ case 'lent':
124
+ this.#lentCoreMs += this.now() - state.since;
125
+ this.effects.release(id);
126
+ return;
127
+ case 'own': {
128
+ this.#ownRunning -= 1;
129
+ let longest: { id: string; since: number } | null = null;
130
+ for (const [waiting, other] of this.#cases) {
131
+ if (other.kind !== 'waiting') continue;
132
+ if (longest === null || this.wall(waiting) > this.wall(longest.id)) {
133
+ longest = { id: waiting, since: other.since };
134
+ }
135
+ }
136
+ if (longest === null) return;
137
+ this.effects.release(longest.id);
138
+ this.#startOwn(longest.id, this.now() - longest.since);
139
+ return;
140
+ }
141
+ }
142
+ }
143
+
144
+ /** How long the case waited at the gate before it started, in ms; zero for one that never did. */
145
+ waitedMs(id: string): number {
146
+ return this.#waited.get(id) ?? 0;
147
+ }
148
+
149
+ /** The last recorded walls of the cases that ran and ended, summed. */
150
+ ranWallMs(): number {
151
+ return this.#ranWallMs;
152
+ }
153
+
154
+ summary(): CaseGateSummary {
155
+ return { own: this.#own, lent: this.#lent, lentCoreMs: this.#lentCoreMs, waitedCaseMs: this.#waitedCaseMs };
156
+ }
157
+
158
+ #startOwn(id: string, waitedMs: number): void {
159
+ this.#startedAfter(id, waitedMs);
160
+ this.#cases.set(id, { kind: 'own' });
161
+ this.#ownRunning += 1;
162
+ this.#own += 1;
163
+ this.effects.start(id);
164
+ }
165
+
166
+ #startedAfter(id: string, waitedMs: number): void {
167
+ this.#waited.set(id, waitedMs);
168
+ this.#waitedCaseMs += waitedMs;
169
+ }
170
+ }
171
+
172
+ /** One core asked of a pool for one case: whether the pool granted it, and how it is given back or withdrawn. */
173
+ export interface CaseCore {
174
+ /** Settles once the pool grants the core (true), or refuses, loses or is withdrawn the request (false). */
175
+ readonly granted: Promise<boolean>;
176
+ /** Gives the core back, or withdraws the request while it waits. Later calls do nothing. */
177
+ release(): void;
178
+ }
179
+
180
+ /** A leaf's gate decisions and the cores its waiting cases asked their pool for. */
181
+ export interface GatedCases {
182
+ readonly cases: CaseGate;
183
+ /** Gives back every core a case still holds and withdraws every request that waits. */
184
+ releaseAll(): void;
185
+ }
186
+
187
+ /**
188
+ * The gate of a leaf that holds `own` cores, over whichever pool `ask` asks: mbx's, through the run's reservation
189
+ * endpoint, or the run's own token pool (cpu-pool.ts, `lendParentToken`). `start` answers a case's connection.
190
+ * `walls` are the cases' last recorded walls; a request is told the work the leaf has left, their sum less the walls
191
+ * of the cases that ran, by which a token pool lends to the leaf that would otherwise end last.
192
+ */
193
+ export function gateCases(
194
+ own: number,
195
+ walls: ReadonlyMap<string, number>,
196
+ start: (id: string) => void,
197
+ ask: (id: string, leftMs: number) => CaseCore,
198
+ ): GatedCases {
199
+ const requests = new Map<string, CaseCore>();
200
+ let total = 0;
201
+ for (const wall of walls.values()) total += wall;
202
+ const cases: CaseGate = new CaseGate(own, (id) => walls.get(id) ?? 0, {
203
+ start,
204
+ reserve: (id) => {
205
+ const held = ask(id, Math.max(0, total - cases.ranWallMs()));
206
+ requests.set(id, held);
207
+ void held.granted.then((granted) => {
208
+ // Released since: the case started on a core of the grant, or ended while it waited.
209
+ if (requests.get(id) !== held) return;
210
+ if (granted) cases.granted(id);
211
+ else requests.delete(id);
212
+ });
213
+ },
214
+ release: (id) => {
215
+ requests.get(id)?.release();
216
+ requests.delete(id);
217
+ },
218
+ });
219
+ return {
220
+ cases,
221
+ releaseAll: () => {
222
+ for (const held of requests.values()) held.release();
223
+ requests.clear();
224
+ },
225
+ };
226
+ }
227
+
228
+ /**
229
+ * A case's wall as its result records it: nextest times a case from the shim's start, so its own duration includes the
230
+ * wait at the gate, which is taken off. Never below zero.
231
+ */
232
+ export function runWallMs(nextestMs: number, waitedMs: number): number {
233
+ return Math.max(0, nextestMs - waitedMs);
234
+ }
235
+
236
+ const GATE_DIRECTORY = 'bounded-gate-';
237
+ const GATE_SOCKET = 'g.sock';
238
+ /** No case line is longer: a binary id and a test name. */
239
+ const LINE_BYTES = 16 * 1024;
240
+
241
+ export interface CaseGateServer {
242
+ /** The socket's path, as the command's environment names it. */
243
+ readonly socket: string;
244
+ /** Answer the case's connection: it starts. */
245
+ start(id: string): void;
246
+ /** Ends every connection and removes the socket's directory; the error that left something behind, or null. */
247
+ close(): Promise<string | null>;
248
+ }
249
+
250
+ export interface CaseGateHandlers {
251
+ /** A case's shim connected and named it. False refuses the connection. */
252
+ readonly arrived: (id: string) => boolean;
253
+ /** A named case's connection ended. */
254
+ readonly ended: (id: string) => void;
255
+ /** A client wrote something that is no case line, or named a case already at the gate; its connection is ended. */
256
+ readonly invalid: (detail: string) => void;
257
+ }
258
+
259
+ function describe(error: unknown): string {
260
+ return error instanceof Error ? error.message : String(error);
261
+ }
262
+
263
+ /** Serve one run's case gate; a gate that cannot be served is the run's refusal. */
264
+ export async function serveCaseGate(
265
+ handlers: CaseGateHandlers,
266
+ ): Promise<{ ok: true; gate: CaseGateServer } | { ok: false; error: string }> {
267
+ let directory: string;
268
+ try {
269
+ directory = await ownedSocketDirectory(GATE_DIRECTORY, GATE_SOCKET);
270
+ } catch (error) {
271
+ return { ok: false, error: `its directory could not be made: ${describe(error)}` };
272
+ }
273
+ const socketPath = join(directory, GATE_SOCKET);
274
+ const clients = new Set<Socket>();
275
+ const named = new Map<string, Socket>();
276
+ const serve = (client: Socket): void => {
277
+ clients.add(client);
278
+ // A case that is killed loses its connection with its process: that end is the case's end, read below.
279
+ client.on('error', () => {});
280
+ client.setEncoding('utf8');
281
+ let partial = '';
282
+ let id: string | null = null;
283
+ const refuse = (detail: string): void => {
284
+ client.destroy();
285
+ handlers.invalid(detail);
286
+ };
287
+ client.on('data', (text: string) => {
288
+ if (id !== null) {
289
+ refuse(`case ${id} wrote a second line at its gate`);
290
+ return;
291
+ }
292
+ partial += text;
293
+ const end = partial.indexOf('\n');
294
+ if (end === -1) {
295
+ if (partial.length > LINE_BYTES) refuse(`a case line is at most ${LINE_BYTES} bytes`);
296
+ return;
297
+ }
298
+ let parsed: ReturnType<typeof parseCaseLine> | null;
299
+ try {
300
+ parsed = parseCaseLine(partial.slice(0, end));
301
+ } catch {
302
+ parsed = null;
303
+ }
304
+ if (parsed === null || !parsed.success || partial.length > end + 1) {
305
+ refuse(`a case line names a binary and a test, one line; got ${JSON.stringify(partial.slice(0, 200))}`);
306
+ return;
307
+ }
308
+ const arriving = caseId(parsed.data.binary, parsed.data.test);
309
+ named.set(arriving, client);
310
+ if (!handlers.arrived(arriving)) {
311
+ named.delete(arriving);
312
+ refuse(`case ${arriving} is already at its gate`);
313
+ return;
314
+ }
315
+ id = arriving;
316
+ });
317
+ client.on('close', () => {
318
+ clients.delete(client);
319
+ if (id === null) return;
320
+ if (named.get(id) === client) named.delete(id);
321
+ handlers.ended(id);
322
+ });
323
+ };
324
+ const server: Server = createServer(serve);
325
+ // eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: listen is this process's own bind of a path in its fresh directory; the kernel answers it with the listening callback or an error, and no peer takes part
326
+ const listened = await new Promise<Error | null>((settle) => {
327
+ server.once('error', settle);
328
+ server.listen(socketPath, () => {
329
+ server.off('error', settle);
330
+ settle(null);
331
+ });
332
+ });
333
+ const close = async (): Promise<string | null> => {
334
+ const failures: string[] = [];
335
+ for (const client of clients) client.destroy();
336
+ if (listened === null) {
337
+ // eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: every accepted connection was destroyed just above, so the server closes without waiting on a peer
338
+ const closed = await new Promise<Error | undefined>((settle) => server.close(settle));
339
+ if (closed !== undefined) failures.push(`its socket did not close: ${closed.message}`);
340
+ }
341
+ try {
342
+ await rm(directory, { recursive: true, force: true });
343
+ } catch (error) {
344
+ failures.push(`its directory was not removed: ${describe(error)}`);
345
+ }
346
+ return failures.length === 0 ? null : `the case gate at ${socketPath}: ${failures.join('; ')}`;
347
+ };
348
+ if (listened !== null) {
349
+ const left = await close();
350
+ return {
351
+ ok: false,
352
+ error: `its socket ${socketPath} could not be bound: ${listened.message}${left === null ? '' : `; ${left}`}`,
353
+ };
354
+ }
355
+ return {
356
+ ok: true,
357
+ gate: {
358
+ socket: socketPath,
359
+ start: (id) => {
360
+ named.get(id)?.write(`${GO}\n`);
361
+ },
362
+ close,
363
+ },
364
+ };
365
+ }
@@ -1,5 +1,5 @@
1
1
  import { parentPort, workerData } from 'node:worker_threads';
2
- import { type CloseOutcome, createCpuPool } from './cpu-pool.js';
2
+ import { type CloseOutcome, createCpuPool, describeIdle } from './cpu-pool.js';
3
3
  import type { RunMemory } from './host-memory.js';
4
4
 
5
5
  /**
@@ -24,7 +24,7 @@ export interface CpuPoolThreadData {
24
24
  export type CpuPoolThreadReport =
25
25
  | { readonly kind: 'serving'; readonly environment: Record<string, string> }
26
26
  | { readonly kind: 'failed'; readonly error: string }
27
- | { readonly kind: 'closed'; readonly outcome: CloseOutcome };
27
+ | { readonly kind: 'closed'; readonly outcome: CloseOutcome; readonly idle: string | null };
28
28
 
29
29
  const host = parentPort;
30
30
  if (host === null) {
@@ -37,7 +37,7 @@ const made = await createCpuPool(data.tokens, new Promise<void>(() => {}), data.
37
37
  if (made.ok) {
38
38
  const { pool } = made;
39
39
  host.once('message', () => {
40
- void pool.close().then((outcome) => report({ kind: 'closed', outcome }));
40
+ void pool.close().then((outcome) => report({ kind: 'closed', outcome, idle: describeIdle(made.summary()) }));
41
41
  });
42
42
  report({ kind: 'serving', environment: pool.environment });
43
43
  } else {