@smoothbricks/nx-plugin 0.6.3-0 → 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 (109) hide show
  1. package/README.md +159 -121
  2. package/dist/.build-inputs.json +25 -20
  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/bounded-test-policy.d.ts +1 -1
  10. package/dist/bounded-test-policy.d.ts.map +1 -1
  11. package/dist/bounded-test-policy.js +9 -8
  12. package/dist/cargo-workspace.d.ts +10 -20
  13. package/dist/cargo-workspace.d.ts.map +1 -1
  14. package/dist/cargo-workspace.js +12 -22
  15. package/dist/cross-check-policy.d.ts +2 -2
  16. package/dist/cross-check-policy.js +2 -2
  17. package/dist/deploy-policy.d.ts +6 -0
  18. package/dist/deploy-policy.d.ts.map +1 -1
  19. package/dist/deploy-policy.js +11 -0
  20. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/executor.js +49 -37
  22. package/dist/executors/bounded-exec/mbx-pool.d.ts +39 -10
  23. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/mbx-pool.js +78 -24
  25. package/dist/executors/bounded-exec/progress.js +5 -5
  26. package/dist/executors/bounded-exec/runner-sizing.d.ts +40 -2
  27. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
  28. package/dist/executors/bounded-exec/runner-sizing.js +55 -4
  29. package/dist/executors/bounded-exec/runtime-pin.js +1 -1
  30. package/dist/executors/bounded-exec/verdict.d.ts +6 -0
  31. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  32. package/dist/executors/bounded-exec/verdict.js +6 -0
  33. package/dist/executors/wrangler-deploy/deploy-hash.d.ts +10 -0
  34. package/dist/executors/wrangler-deploy/deploy-hash.d.ts.map +1 -0
  35. package/dist/executors/wrangler-deploy/deploy-hash.js +14 -0
  36. package/dist/executors/wrangler-deploy/executor.d.ts +24 -0
  37. package/dist/executors/wrangler-deploy/executor.d.ts.map +1 -0
  38. package/dist/executors/wrangler-deploy/executor.js +43 -0
  39. package/dist/executors/wrangler-deploy/hasher.d.ts +27 -0
  40. package/dist/executors/wrangler-deploy/hasher.d.ts.map +1 -0
  41. package/dist/executors/wrangler-deploy/hasher.js +90 -0
  42. package/dist/executors/wrangler-deploy/schema.d.ts +5 -0
  43. package/dist/executors/wrangler-deploy/schema.d.ts.map +1 -0
  44. package/dist/go-tests.d.ts +30 -0
  45. package/dist/go-tests.d.ts.map +1 -0
  46. package/dist/go-tests.js +43 -0
  47. package/dist/index.d.ts +9 -2
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +271 -230
  50. package/dist/managed-files/files.d.ts.map +1 -1
  51. package/dist/managed-files/files.js +5 -0
  52. package/dist/process-group.js +2 -2
  53. package/dist/runtime-input-policy.d.ts +3 -3
  54. package/dist/runtime-input-policy.js +3 -3
  55. package/dist/test-timings.d.ts +23 -4
  56. package/dist/test-timings.d.ts.map +1 -1
  57. package/dist/test-timings.js +72 -12
  58. package/dist/version-actions.cjs +10 -11
  59. package/dist/version-actions.d.cts.map +1 -1
  60. package/dist/workspace-config-policy.d.ts.map +1 -1
  61. package/dist/workspace-config-policy.js +17 -22
  62. package/dist/workspace-manifest.js +3 -3
  63. package/executors.json +6 -5
  64. package/managed/raw/patches/nx@23.2.1.patch +118 -24
  65. package/managed/raw/patches/ttsc@0.34.1.patch +64 -0
  66. package/managed/raw/tooling/direnv/developer-links.ts +121 -51
  67. package/managed/raw/tooling/direnv/installed-bun-links.ts +7 -6
  68. package/managed/raw/tooling/direnv/setup-environment.ts +14 -4
  69. package/managed/raw/tooling/git-hooks/pinned-revisions-served.ts +252 -0
  70. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -32
  71. package/package.json +57 -7
  72. package/runtime/nx-publish/.build-inputs.json +11 -0
  73. package/runtime-input.sh +50 -23
  74. package/src/bin/smoo-nx-go-test-build.ts +83 -0
  75. package/src/bin/smoo-nx-go-test-run.ts +140 -0
  76. package/src/bounded-test-policy.ts +9 -8
  77. package/src/cargo-workspace.ts +12 -24
  78. package/src/cross-check-policy.ts +2 -2
  79. package/src/deploy-policy.ts +11 -0
  80. package/src/executors/bounded-exec/executor.ts +61 -42
  81. package/src/executors/bounded-exec/mbx-pool.ts +97 -33
  82. package/src/executors/bounded-exec/progress.ts +5 -5
  83. package/src/executors/bounded-exec/runner-sizing.ts +73 -7
  84. package/src/executors/bounded-exec/runtime-pin.ts +1 -1
  85. package/src/executors/bounded-exec/verdict.ts +7 -0
  86. package/src/executors/wrangler-deploy/deploy-hash.ts +15 -0
  87. package/src/executors/wrangler-deploy/executor.ts +63 -0
  88. package/src/executors/wrangler-deploy/hasher.ts +111 -0
  89. package/src/executors/wrangler-deploy/schema.json +16 -0
  90. package/src/executors/wrangler-deploy/schema.ts +4 -0
  91. package/src/go-tests.ts +67 -0
  92. package/src/index.ts +294 -253
  93. package/src/managed-files/files.ts +5 -0
  94. package/src/process-group.ts +2 -2
  95. package/src/runtime-input-policy.ts +3 -3
  96. package/src/test-timings.ts +72 -14
  97. package/src/version-actions.cts +10 -11
  98. package/src/workspace-config-policy.ts +20 -29
  99. package/src/workspace-manifest.ts +3 -3
  100. package/test-timings.json +131 -130
  101. package/dist/executors/clean-outputs/executor.d.ts +0 -26
  102. package/dist/executors/clean-outputs/executor.d.ts.map +0 -1
  103. package/dist/executors/clean-outputs/executor.js +0 -83
  104. package/dist/executors/clean-outputs/schema.d.ts +0 -4
  105. package/dist/executors/clean-outputs/schema.d.ts.map +0 -1
  106. package/src/executors/clean-outputs/executor.ts +0 -131
  107. package/src/executors/clean-outputs/schema.json +0 -15
  108. package/src/executors/clean-outputs/schema.ts +0 -3
  109. /package/dist/executors/{clean-outputs → wrangler-deploy}/schema.js +0 -0
@@ -1,5 +1,5 @@
1
1
  import { shellTokens } from '../../runtime-input-policy.js';
2
- import { invocationSpan, type RunnerKind } from './runner-sizing.js';
2
+ import { CPU_TOKENS_ENV, type Demand, grantedSizing, invocationSpan, type ReservedDemand } from './runner-sizing.js';
3
3
  import { shellQuote } from './verdict.js';
4
4
 
5
5
  /**
@@ -27,26 +27,93 @@ export function mbxPool(environment: Readonly<NodeJS.ProcessEnv>): MbxPool | nul
27
27
  }
28
28
 
29
29
  /**
30
- * How a leaf enters the pool once: a Cargo leaf runs through mbx, which admits its compiler and supported test work
31
- * itself, so wrapping it would charge it twice; any other leaf holds `cpus` of the pool for its whole run through
32
- * `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").
33
39
  */
34
40
  export type MbxLeaf =
35
41
  | { readonly kind: 'cargo' }
36
- | { 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
+ };
37
47
 
38
- const CARGO_LEAVES: ReadonlySet<RunnerKind> = new Set<RunnerKind>(['cargo', 'cargo-mutants', 'nextest']);
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]|$)/;
39
50
 
40
- export function mbxLeaf(pool: MbxPool, kind: RunnerKind, want: number): MbxLeaf {
41
- return CARGO_LEAVES.has(kind)
42
- ? { kind: 'cargo' }
43
- : { kind: 'reserve', cpus: Math.max(1, want), priority: pool.priority };
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);
44
54
  }
45
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;
87
+
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;
112
+
46
113
  /**
47
114
  * Only a known scheduled execution may stop its idle clock at zero holds. Plain Cargo tests can run doctests;
48
115
  * archive-file targets, direct binaries and other runners are not proven scheduled at spawn (11_shell.md,
49
- * "Cargo test-runner boundaries"). The inferred extracted native nextest command is a supported Cargo invocation.
116
+ * "Cargo test-runner boundaries"). The inferred in-place nextest command is a supported Cargo invocation.
50
117
  */
51
118
  export function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment: Readonly<NodeJS.ProcessEnv>): boolean {
52
119
  if (leaf === null) return false;
@@ -54,20 +121,20 @@ export function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment:
54
121
  if (environment.MBX_SCHEDULER_TESTS !== '1') return false;
55
122
  if (environment.MBX_SCHEDULER === '0' || environment.MBX_SCHED_DISABLE === '1' || environment.MBX_DISABLE === '1')
56
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');
57
127
  // invocationSpan shares the lifecycle mapper's conservative shell-syntax boundary.
58
- if (/[<>\n\r`\\]/.test(command)) return false;
59
- if (/[^\S \t]/.test(command)) return false;
128
+ if (/[<>\n\r`\\]/.test(known)) return false;
129
+ if (/[^\S \t]/.test(known)) return false;
60
130
  const invocation = /cargo(?:\s+(?:--frozen|--locked|--offline))*\s+(nextest\s+run|test)(?=\s|$)/;
61
- const found = invocation.exec(command);
62
- if (found === null) return false;
63
- const prefix = command.slice(0, found.index).trim();
64
- if (prefix !== '' && !/^extracted="\$\([^"\n]+\)"\s*&&$/.test(prefix)) return false;
65
- let remaining = command.slice(found.index);
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);
66
134
  const unquoted = remaining.replace(/"[^"]*"|'[^']*'/g, '');
67
135
  if (/[$*?{}~[\]]/.test(unquoted)) return false;
68
136
  // Only the inferred command's path templates are known expansions; arbitrary words could supply mbx overrides.
69
- const paths = remaining.replace(/\$PWD(?=\/)/g, '');
70
- if ((prefix === '' ? paths : paths.replace(/\$extracted(?=\/)/g, '')).includes('$')) return false;
137
+ if (remaining.replace(/\$PWD(?=\/)/g, '').includes('$')) return false;
71
138
  for (;;) {
72
139
  const leg = invocation.exec(remaining);
73
140
  if (leg === null || leg.index !== 0) return false;
@@ -76,14 +143,7 @@ export function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment:
76
143
  for (const token of shellTokens(remaining.slice(0, argsEnd), [])) {
77
144
  if (token.kind !== 'word') return false;
78
145
  if (token.text === '--') break;
79
- if (
80
- token.text.startsWith('$') &&
81
- !(
82
- token.text.startsWith('$extracted/') &&
83
- ['--binaries-metadata', '--cargo-metadata', '--target-dir-remap'].includes(args.at(-1) ?? '')
84
- )
85
- )
86
- return false;
146
+ if (token.text.startsWith('$')) return false;
87
147
  args.push(token.text);
88
148
  }
89
149
  if (
@@ -114,12 +174,16 @@ export function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment:
114
174
  export const MBX_GRANT_FD = 5;
115
175
 
116
176
  /**
117
- * `command` as the shell runs it under its leaf's admission. `mbx` is the canonical environment's, whose realpath
118
- * cowshed's admission proved to be the host's pinned mbx; it writes the grant on this descriptor itself.
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.
119
181
  */
120
- export function withMbxLeaf(command: string, leaf: MbxLeaf): string {
121
- if (leaf.kind === 'cargo') {
182
+ export function withMbxLeaf(command: string, leaf: MbxLeaf | null): string {
183
+ if (leaf === null || leaf.kind === 'cargo') {
122
184
  return command;
123
185
  }
124
- return `mbx reserve --grant-fd ${MBX_GRANT_FD} --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}`)}`;
125
189
  }
@@ -2,7 +2,7 @@ import { StringDecoder } from 'node:string_decoder';
2
2
  import typia, { type tags } from 'typia';
3
3
  import { invocationSpan, withInvocationFlag } from './runner-sizing.js';
4
4
  import type { NativeTestResult } from './test-result-writer.js';
5
- import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
5
+ import { BUN_TEST, GO_TEST, GO_TEST_RUNNER, NEXTEST_RUN } from './verdict.js';
6
6
 
7
7
  /**
8
8
  * What a run's output says about its forward motion: whether a test finished, and which tests the runner said
@@ -99,8 +99,8 @@ function nativeTestEvent(text: string, invalid?: (detail: string) => void): Nati
99
99
  /** A direct Go chain reports tests only through JSON; other commands may also carry text runner reports. */
100
100
  function onlyGoTests(command: string): boolean {
101
101
  let rest = command.trim();
102
- while (rest.search(GO_TEST) === 0) {
103
- 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();
104
104
  if (rest === '') return true;
105
105
  const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
106
106
  if (separator === null) return false;
@@ -117,7 +117,7 @@ interface LineState {
117
117
 
118
118
  export function createRunProgress(command = '', closed?: (result: NativeTestResult) => void): RunProgress {
119
119
  const goLifecycle = onlyGoTests(command);
120
- const goResults = command.search(GO_TEST) !== -1;
120
+ const goResults = command.search(GO_TEST_RUNNER) !== -1;
121
121
  const streams: Record<OutputStream, LineState> = {
122
122
  stdout: { partial: '', countedDots: 0 },
123
123
  stderr: { partial: '', countedDots: 0 },
@@ -310,7 +310,7 @@ export function withTestResultReport(command: string): { command: string; env: R
310
310
  if (
311
311
  (command.match(NEXTEST_RUN)?.length ?? 0) !== 1 ||
312
312
  command.search(BUN_TEST) !== -1 ||
313
- command.search(GO_TEST) !== -1 ||
313
+ command.search(GO_TEST_RUNNER) !== -1 ||
314
314
  UNCAPTURED_NEXTEST.test(command)
315
315
  )
316
316
  return { command, env: {} };
@@ -1,4 +1,4 @@
1
- import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
1
+ import { BUN_TEST, GO_TEST_RUNNER, NEXTEST_RUN } from './verdict.js';
2
2
 
3
3
  /**
4
4
  * How a runner's parallelism is read from its command and sized to the tokens its CPU pool lends
@@ -40,13 +40,27 @@ export interface TestThreadCeilings {
40
40
  * ceilings their sizing keeps; cargo-mutants carries how many cargo processes it runs at once, which share its grant.
41
41
  */
42
42
  export type Demand =
43
- | { kind: 'cargo' | 'process'; want: number; testThreads: TestThreadCeilings }
43
+ | { kind: 'cargo'; want: number; testThreads: TestThreadCeilings }
44
+ | { kind: 'process'; want: number; testThreads: TestThreadCeilings }
44
45
  | { kind: 'cargo-mutants'; want: number; jobs: number }
45
- | { kind: Exclude<RunnerKind, 'cargo' | 'cargo-mutants' | 'process'>; want: number };
46
+ | { kind: 'nextest'; want: number }
47
+ | { kind: 'bun-parallel'; want: number }
48
+ | { kind: 'stryker'; want: number }
49
+ | { kind: 'vitest'; want: number }
50
+ | { kind: 'go-test'; want: number };
51
+
52
+ /** A runner a leaf reserves for under mbx (mbx-pool.ts, `mbxLeaf`): one that Cargo does not admit through mbx itself. */
53
+ export type ReservedDemand = Extract<Demand, { kind: 'nextest' | 'bun-parallel' | 'stryker' | 'process' }>;
46
54
 
47
55
  /** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
48
56
  export const CPU_TOKENS_ENV = 'BOUNDED_EXEC_CPU_TOKENS';
49
57
 
58
+ /**
59
+ * The cores mbx granted the reservation a leaf runs under: `mbx reserve --cpus N` names at most N to its command, as many
60
+ * as were free once one was.
61
+ */
62
+ export const GRANTED_CPUS_ENV = 'MBX_RESERVE_CPUS';
63
+
50
64
  const CARGO_BUILD =
51
65
  /(?<=^|[\s;&|()])(?:cargo(?:\s+-\S+)*\s+(?:build|test|check|clippy|rustc|doc|run|nextest\s+archive)|napi\s+build)(?=\s|$)/;
52
66
  const TEST_THREADS = /--test-threads(?:=|\s+)(\d+)/g;
@@ -75,7 +89,7 @@ interface WorkerFlag {
75
89
  /** The flag with its value, one shell word, captured. */
76
90
  readonly flag: RegExp;
77
91
  /** The flag naming `count`. */
78
- readonly sized: (count: number) => string;
92
+ readonly sized: (count: string) => string;
79
93
  /** The runner's own count when the flag names none, from the cores and the environment the command will see. */
80
94
  readonly ownDefault: (cores: number, env: Readonly<NodeJS.ProcessEnv>) => number;
81
95
  /** A flag with which the runner runs one worker whatever count it names. */
@@ -101,9 +115,9 @@ const WORKER_FLAGS: Readonly<Record<WorkerFlagKind, WorkerFlag>> = {
101
115
  ownDefault: (cores) => Math.max(cores - 1, 1),
102
116
  serial: /(?<![\w-])(?:--no-file-parallelism|--file(?:P|-p)arallelism=false)(?![\w-])/,
103
117
  },
104
- /** `go test -p`: packages built and tested at once, `GOMAXPROCS` by default, normally one per core. */
118
+ /** `go test -p`: packages built and tested at once, `GOMAXPROCS` by default, normally one per core; prebuilt runs alike. */
105
119
  'go-test': {
106
- invocation: GO_TEST,
120
+ invocation: GO_TEST_RUNNER,
107
121
  flag: new RegExp(String.raw`(?<![\w-])-p(?:=|\s+)${WORD}`, 'g'),
108
122
  sized: (count) => `-p=${count}`,
109
123
  ownDefault: (cores, env) => parseInheritedTokens(env.GOMAXPROCS) ?? cores,
@@ -322,7 +336,7 @@ export function sizeRunner(
322
336
  case 'go-test': {
323
337
  const runner = WORKER_FLAGS[demand.kind];
324
338
  return {
325
- command: withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(runnerCount), true),
339
+ command: withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(count), true),
326
340
  env: { [CPU_TOKENS_ENV]: held },
327
341
  };
328
342
  }
@@ -346,6 +360,58 @@ export function sizeRunner(
346
360
  }
347
361
  }
348
362
 
363
+ /**
364
+ * The shell statements that run first in a reserved leaf's reservation and size its runner to the grant, which only
365
+ * the command learns as it starts: `CPU_TOKENS_ENV` becomes the cores mbx granted (`GRANTED_CPUS_ENV`), never more
366
+ * than the leaf asked for, its `--cpus` (the runner's own count), which also stands in under an mbx that grants a
367
+ * reservation whole and names none. The test-thread variables a sizing sets follow from it, under their inherited
368
+ * ceilings, outranking the target's own as a numeric grant's do (`sizeRunner`).
369
+ */
370
+ export function grantedSizing(demand: ReservedDemand): string {
371
+ const asked = demand.want;
372
+ const named = `\${${GRANTED_CPUS_ENV}:-${asked}}`;
373
+ const tokens = `$${CPU_TOKENS_ENV}`;
374
+ const atMost = (ceiling: number | null): string =>
375
+ ceiling === null ? `"${tokens}"` : `"$(( ${tokens} < ${ceiling} ? ${tokens} : ${ceiling} ))"`;
376
+ const statements = [`export ${CPU_TOKENS_ENV}="$(( ${named} < ${asked} ? ${named} : ${asked} ))"`];
377
+ switch (demand.kind) {
378
+ case 'nextest':
379
+ statements.push(`export NEXTEST_TEST_THREADS="${tokens}"`);
380
+ break;
381
+ case 'process':
382
+ // A process's script may start Cargo, whose test threads take no jobserver token: its whole grant.
383
+ statements.push(
384
+ `export RUST_TEST_THREADS=${atMost(demand.testThreads.rust)}`,
385
+ `export NEXTEST_TEST_THREADS=${atMost(demand.testThreads.nextest)}`,
386
+ );
387
+ break;
388
+ case 'bun-parallel':
389
+ case 'stryker':
390
+ break;
391
+ }
392
+ return statements.map((statement) => `${statement}; `).join('');
393
+ }
394
+
395
+ /**
396
+ * `command` with a reserved runner's named count read from its grant, `CPU_TOKENS_ENV` as `grantedSizing` sets it,
397
+ * where the runner's shell runs it; the runner then runs on all it holds.
398
+ */
399
+ export function sizeRunnerToGrant(demand: ReservedDemand, command: string): string {
400
+ const granted = `\${${CPU_TOKENS_ENV}}`;
401
+ switch (demand.kind) {
402
+ case 'nextest':
403
+ return command.replace(TEST_THREADS, `--test-threads=${granted}`);
404
+ case 'bun-parallel':
405
+ return command.replace(BUN_PARALLEL, `--parallel=${granted}`);
406
+ case 'stryker': {
407
+ const runner = WORKER_FLAGS.stryker;
408
+ return withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(granted), true);
409
+ }
410
+ case 'process':
411
+ return command;
412
+ }
413
+ }
414
+
349
415
  /** A configured value may lower an inherited ceiling, never raise it. */
350
416
  function inheritedCeiling(inherited: string | undefined, configured: string | undefined): number | null {
351
417
  const left = parseInheritedTokens(inherited);
@@ -7,7 +7,7 @@ import type { Result } from './ram-temp.js';
7
7
  /**
8
8
  * A leg that runs `bun` runs the Bun its workspace declares.
9
9
  *
10
- * The root `package.json` names that Bun in `packageManager` (`bun@1.4.3`); the Bun the command gets is the first `bun`
10
+ * The root `package.json` names that Bun in `packageManager` (`bun@x.y.z`); the Bun the command gets is the first `bun`
11
11
  * on the PATH it runs with. When the two disagree the leg is refused before it asks for anything, naming both versions
12
12
  * and the binary. A Bun other than the declared one does not fail at startup: on Bun 1.4.2 `console.log` drops what a
13
13
  * full pipe refuses, so a child's JSON document reaches the parent that reads it cut in the middle of a string, the
@@ -415,6 +415,13 @@ export const BUN_TEST = /(?<=^|[\s;&|()])((?:[^\s;&|()<>'"`\\=]*\/)?bun)\s+test(
415
415
  export const NEXTEST_RUN = /(?<=^|[\s;&|()-])nextest\s+run(?=\s|$)/g;
416
416
  export const GO_TEST = /(?<=^|[\s;&|()])go\s+test(?=\s|$)/;
417
417
 
418
+ /**
419
+ * `go test`, or `smoo-nx-go-test-run` running the test binaries `smoo-nx-go-test-build` made with `go test -c`, under
420
+ * any path: both write `go test -json` events on stdout and run `-p` packages at once. Only `go test` builds, so only
421
+ * it is asked for `-json` (withProgressReport) or given a build phase (withRunnerLifecycle).
422
+ */
423
+ export const GO_TEST_RUNNER = /(?<=^|[\s;&|()])(?:go\s+test|(?:[^\s;&|()<>'"`\\=]*\/)?smoo-nx-go-test-run)(?=\s|$)/;
424
+
418
425
  /**
419
426
  * `command` with its test runner asked to write a JUnit report into `directory`, the report
420
427
  * `judgeRun` reads. Both runners are recognised by their invocation, so every target that runs
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The hash a wrangler deploy runs under when its hasher could not confirm that the stage serves its artifact's
3
+ * version: the artifact hash, then a nonce no cache holds, so the run misses whatever an earlier run stored, rolled
4
+ * back to or not. A miss is the right answer there. The deploy still tags the artifact's version
5
+ * (`deployArtifactHash`), so once that version is live the next hash is the artifact's own.
6
+ */
7
+ export function unconfirmedDeployHash(artifactHash: string, nonce: string): string {
8
+ return `${artifactHash}-${nonce}`;
9
+ }
10
+
11
+ /** The artifact hash a wrangler deploy task's hash carries: all of a confirmed one, the part before an unconfirmed one's nonce. */
12
+ export function deployArtifactHash(taskHash: string): string {
13
+ const nonce = taskHash.indexOf('-');
14
+ return nonce === -1 ? taskHash : taskHash.slice(0, nonce);
15
+ }
@@ -0,0 +1,63 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { join } from 'node:path';
3
+ import { deployArtifactHash } from './deploy-hash.js';
4
+ import { workspaceBinPath } from './hasher.js';
5
+ import type { WranglerDeployOptions } from './schema.js';
6
+
7
+ /** What the executor reads of Nx's context: the workspace and the project's root. */
8
+ interface WranglerDeployContext {
9
+ readonly root: string;
10
+ readonly projectName?: string | undefined;
11
+ readonly projectsConfigurations: { readonly projects: Readonly<Record<string, { readonly root: string }>> };
12
+ }
13
+
14
+ /**
15
+ * `smoo wrangler deploy-stage --stage <stage>` in the project, as the workspace's own `smoo`, under the artifact hash
16
+ * of the hash the task ran under: `deploy-stage` tags the version it deploys with that hash, and the hasher compares
17
+ * the stage's live tag with it.
18
+ */
19
+ export function runWranglerDeploy(
20
+ options: WranglerDeployOptions,
21
+ context: WranglerDeployContext,
22
+ env: NodeJS.ProcessEnv,
23
+ ): Promise<{ success: boolean }> {
24
+ const project =
25
+ context.projectName === undefined ? undefined : context.projectsConfigurations.projects[context.projectName];
26
+ if (project === undefined) {
27
+ throw new Error('@smoothbricks/nx-plugin:wrangler-deploy requires a project context.');
28
+ }
29
+ const hash = env.NX_TASK_HASH;
30
+ const deployEnv: NodeJS.ProcessEnv = {
31
+ ...env,
32
+ PATH: workspaceBinPath(context.root, env),
33
+ ...(hash === undefined ? {} : { NX_TASK_HASH: deployArtifactHash(hash) }),
34
+ };
35
+ const command = `smoo wrangler deploy-stage --stage ${options.stage}`;
36
+ // eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a deploy lasts as long as wrangler's upload and the live-version wait deploy-stage bounds itself, as it did under nx:run-commands
37
+ return new Promise((resolve) => {
38
+ const child = spawn('smoo', ['wrangler', 'deploy-stage', '--stage', options.stage], {
39
+ cwd: join(context.root, project.root),
40
+ env: deployEnv,
41
+ stdio: 'inherit',
42
+ });
43
+ child.on('error', (error) => {
44
+ console.error(`wrangler-deploy: \`${command}\` could not start: ${error.message}`);
45
+ resolve({ success: false });
46
+ });
47
+ child.on('close', (code, signal) => {
48
+ if (code !== 0) {
49
+ console.error(
50
+ `wrangler-deploy: \`${command}\` ${signal === null ? `exited ${code}` : `was ended by ${signal}`}`,
51
+ );
52
+ }
53
+ resolve({ success: code === 0 });
54
+ });
55
+ });
56
+ }
57
+
58
+ export default function wranglerDeployExecutor(
59
+ options: WranglerDeployOptions,
60
+ context: WranglerDeployContext,
61
+ ): Promise<{ success: boolean }> {
62
+ return runWranglerDeploy(options, context, process.env);
63
+ }
@@ -0,0 +1,111 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { randomBytes } from 'node:crypto';
3
+ import { delimiter, join } from 'node:path';
4
+ import { promisify } from 'node:util';
5
+ import type { HasherContext } from 'nx/src/config/misc-interfaces';
6
+ import type { Task } from 'nx/src/config/task-graph';
7
+ import type { Hash } from 'nx/src/hasher/task-hasher';
8
+ import { workspaceRoot } from 'nx/src/utils/workspace-root';
9
+ import { deployVersionTag } from '../../deploy-policy.js';
10
+ import { isRecord } from '../../type-guards.js';
11
+ import { unconfirmedDeployHash } from './deploy-hash.js';
12
+
13
+ const execFileAsync = promisify(execFile);
14
+
15
+ /**
16
+ * `smoo wrangler deployed-version` asks Cloudflare twice through wrangler, in seconds. The bound is for one that never
17
+ * answers: the live version is then unknown, the hash misses, and the deploy reads Cloudflare itself.
18
+ */
19
+ const LIVE_TAG_DEADLINE_MS = 120_000;
20
+
21
+ /** What serves a stage: the tag of the version taking all its traffic (null for a version deployed untagged), or why that is not known. */
22
+ export type LiveTag =
23
+ | { readonly kind: 'tag'; readonly tag: string | null }
24
+ | { readonly kind: 'unknown'; readonly reason: string };
25
+
26
+ /** The hash detail that says what the stage served when it was not the artifact's version. */
27
+ export const LIVE_VERSION_NODE = 'wrangler:live-version';
28
+
29
+ /**
30
+ * A deploy's hash: `artifact`, Nx's hash of what the deploy uploads and the stage it names, while the stage serves the
31
+ * version tagged with it, so a cache hit replays a deploy that would change nothing. Any other live version, or none
32
+ * known, makes it `unconfirmedDeployHash`, which no cache holds, so the run deploys; a rollback is never answered
33
+ * from the cache. Hashing the live tag in instead would never converge: the deployed version is tagged with the hash.
34
+ */
35
+ export function deployHash(artifact: Hash, live: LiveTag, nonce: string): Hash {
36
+ const expected = deployVersionTag(artifact.value);
37
+ if (live.kind === 'tag' && expected !== null && live.tag === expected) return artifact;
38
+ const served =
39
+ expected === null
40
+ ? `the artifact hash ${artifact.value} names no version tag`
41
+ : live.kind === 'unknown'
42
+ ? live.reason
43
+ : `${live.tag ?? 'an untagged version'} is live, not ${expected}`;
44
+ return {
45
+ ...artifact,
46
+ value: unconfirmedDeployHash(artifact.value, nonce),
47
+ details: { ...artifact.details, nodes: { ...artifact.details.nodes, [LIVE_VERSION_NODE]: served } },
48
+ };
49
+ }
50
+
51
+ /** `env`'s PATH with the workspace's own bins first, as nx:run-commands runs a command. */
52
+ export function workspaceBinPath(root: string, env: NodeJS.ProcessEnv): string {
53
+ const bins = join(root, 'node_modules', '.bin');
54
+ return env.PATH === undefined || env.PATH === '' ? bins : `${bins}${delimiter}${env.PATH}`;
55
+ }
56
+
57
+ /** The tag serving `stage` for the project at `projectRoot`, read from Cloudflare now, never from a cached answer. */
58
+ export async function probeLiveTag(
59
+ root: string,
60
+ projectRoot: string,
61
+ stage: string,
62
+ env: NodeJS.ProcessEnv = process.env,
63
+ ): Promise<LiveTag> {
64
+ const args = ['wrangler', 'deployed-version', '--stage', stage, '--refresh'];
65
+ const command = `smoo ${args.join(' ')}`;
66
+ try {
67
+ const { stdout } = await execFileAsync('smoo', args, {
68
+ cwd: join(root, projectRoot),
69
+ env: { ...env, PATH: workspaceBinPath(root, env) },
70
+ encoding: 'utf8',
71
+ timeout: LIVE_TAG_DEADLINE_MS,
72
+ killSignal: 'SIGKILL',
73
+ maxBuffer: 64 * 1024,
74
+ });
75
+ const tag = stdout.trim();
76
+ if (tag === '' || /\s/.test(tag)) {
77
+ return { kind: 'unknown', reason: `\`${command}\` printed no single tag: ${JSON.stringify(stdout)}` };
78
+ }
79
+ // `deployed-version` says `untagged` for a version deployed outside Nx.
80
+ return { kind: 'tag', tag: tag === 'untagged' ? null : tag };
81
+ } catch (error) {
82
+ if (error instanceof Error && 'killed' in error && error.killed === true) {
83
+ return { kind: 'unknown', reason: `\`${command}\` gave no answer within ${LIVE_TAG_DEADLINE_MS} ms` };
84
+ }
85
+ const said =
86
+ error instanceof Error && 'stderr' in error && typeof error.stderr === 'string' ? error.stderr.trim() : '';
87
+ const reason = said !== '' ? said : error instanceof Error ? error.message : String(error);
88
+ return { kind: 'unknown', reason: `\`${command}\` failed: ${reason}` };
89
+ }
90
+ }
91
+
92
+ /** The stage the task deploys: its `--stage` override, else its target's `stage` option. */
93
+ function deployStage(task: Task, options: unknown): string | null {
94
+ const overrides: unknown = task.overrides;
95
+ if (isRecord(overrides) && typeof overrides.stage === 'string') return overrides.stage;
96
+ return isRecord(options) && typeof options.stage === 'string' ? options.stage : null;
97
+ }
98
+
99
+ /** Nx's custom hasher of `@smoothbricks/nx-plugin:wrangler-deploy`: `deployHash` of the task's own hash and its stage's live tag. */
100
+ export default async function wranglerDeployHasher(task: Task, context: HasherContext): Promise<Hash> {
101
+ const artifact = await context.hasher.hashTask(task, context.taskGraph);
102
+ const project = context.projectsConfigurations.projects[task.target.project];
103
+ const stage = deployStage(task, project?.targets?.[task.target.target]?.options);
104
+ const live: LiveTag =
105
+ project === undefined
106
+ ? { kind: 'unknown', reason: `Nx names no project ${task.target.project}` }
107
+ : stage === null
108
+ ? { kind: 'unknown', reason: 'the task names no --stage' }
109
+ : await probeLiveTag(workspaceRoot, project.root, stage);
110
+ return deployHash(artifact, live, randomBytes(8).toString('hex'));
111
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "version": 2,
4
+ "title": "Wrangler Deploy Executor",
5
+ "description": "Deploy a stage with smoo wrangler deploy-stage, cached on the artifact and the stage while the stage serves that artifact's version.",
6
+ "type": "object",
7
+ "cli": "nx",
8
+ "required": ["stage"],
9
+ "properties": {
10
+ "stage": {
11
+ "type": "string",
12
+ "description": "staging, production, or prN: the stage this run deploys, given with --stage."
13
+ }
14
+ },
15
+ "additionalProperties": false
16
+ }
@@ -0,0 +1,4 @@
1
+ /** Options of `@smoothbricks/nx-plugin:wrangler-deploy`: the stage a run deploys, given with `--stage`. */
2
+ export interface WranglerDeployOptions {
3
+ readonly stage: string;
4
+ }
@@ -0,0 +1,67 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, isAbsolute, resolve } from 'node:path';
3
+ import { isRecord } from './type-guards.js';
4
+
5
+ /**
6
+ * The manifest `smoo-nx-go-test-build` writes beside the test binaries it builds with `go test -c`, and
7
+ * `smoo-nx-go-test-run` runs. Every path in it is relative to the manifest's own directory, so the binaries and the
8
+ * packages they test are found wherever both are restored together, as an Nx cache restore puts them back.
9
+ */
10
+ export const GO_TESTS_MANIFEST = 'go-tests.json';
11
+
12
+ /** One package's test binary. */
13
+ export interface GoTestBinary {
14
+ /** The binary `go test -c` built for the package. */
15
+ readonly binary: string;
16
+ /** The package directory: `go test` runs a package's binary there, so its relative test data resolves alike. */
17
+ readonly dir: string;
18
+ /** The package's import path, every event's `Package`. */
19
+ readonly importPath: string;
20
+ }
21
+
22
+ export interface GoTestsManifest {
23
+ readonly version: 1;
24
+ /** One entry per package with test files, sorted by import path. */
25
+ readonly tests: readonly GoTestBinary[];
26
+ }
27
+
28
+ export type GoTestsRead =
29
+ | { readonly ok: true; readonly tests: readonly GoTestBinary[] }
30
+ | { readonly ok: false; readonly reason: string };
31
+
32
+ /** The binaries the manifest at `path` lists, their paths resolved against its directory, or why it lists none. */
33
+ export function readGoTestsManifest(path: string): GoTestsRead {
34
+ let parsed: unknown;
35
+ try {
36
+ parsed = JSON.parse(readFileSync(path, 'utf8'));
37
+ } catch (error) {
38
+ return { ok: false, reason: `${path} cannot be read: ${error instanceof Error ? error.message : String(error)}` };
39
+ }
40
+ if (!isRecord(parsed) || parsed.version !== 1 || !Array.isArray(parsed.tests)) {
41
+ return { ok: false, reason: `${path} is no version 1 ${GO_TESTS_MANIFEST}: it needs {"version":1,"tests":[…]}` };
42
+ }
43
+ const base = dirname(path);
44
+ const tests: GoTestBinary[] = [];
45
+ const listed = new Set<string>();
46
+ for (const [index, entry] of parsed.tests.entries()) {
47
+ if (
48
+ !isRecord(entry) ||
49
+ !isRelativePath(entry.binary) ||
50
+ !isRelativePath(entry.dir) ||
51
+ typeof entry.importPath !== 'string' ||
52
+ entry.importPath === ''
53
+ ) {
54
+ return { ok: false, reason: `${path}: tests[${index}] needs a relative binary and dir and an importPath` };
55
+ }
56
+ if (listed.has(entry.importPath)) {
57
+ return { ok: false, reason: `${path}: ${entry.importPath} is listed twice` };
58
+ }
59
+ listed.add(entry.importPath);
60
+ tests.push({ binary: resolve(base, entry.binary), dir: resolve(base, entry.dir), importPath: entry.importPath });
61
+ }
62
+ return { ok: true, tests };
63
+ }
64
+
65
+ function isRelativePath(value: unknown): value is string {
66
+ return typeof value === 'string' && value !== '' && !isAbsolute(value);
67
+ }