@smoothbricks/nx-plugin 0.6.2 → 0.6.3-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 (97) hide show
  1. package/README.md +281 -73
  2. package/dist/.build-inputs.json +20 -14
  3. package/dist/bin/smoo-nx-publish.d.ts +3 -0
  4. package/dist/bin/smoo-nx-publish.d.ts.map +1 -0
  5. package/dist/bin/smoo-nx-publish.js +3 -0
  6. package/dist/cargo-retention-hook.d.ts +6 -2
  7. package/dist/cargo-retention-hook.d.ts.map +1 -1
  8. package/dist/cargo-retention-hook.js +13 -7
  9. package/dist/eslint-file-set.d.ts +4 -1
  10. package/dist/eslint-file-set.d.ts.map +1 -1
  11. package/dist/eslint-sources.d.ts +25 -0
  12. package/dist/eslint-sources.d.ts.map +1 -0
  13. package/dist/eslint-sources.js +99 -0
  14. package/dist/executors/all-verdicts/executor.d.ts +2 -1
  15. package/dist/executors/all-verdicts/executor.d.ts.map +1 -1
  16. package/dist/executors/all-verdicts/executor.js +48 -2
  17. package/dist/executors/all-verdicts/schema.d.ts +13 -0
  18. package/dist/executors/all-verdicts/schema.d.ts.map +1 -1
  19. package/dist/executors/all-verdicts/schema.js +7 -0
  20. package/dist/executors/bounded-exec/bounds.d.ts +57 -8
  21. package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
  22. package/dist/executors/bounded-exec/bounds.js +100 -11
  23. package/dist/executors/bounded-exec/executor.d.ts +2 -1
  24. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  25. package/dist/executors/bounded-exec/executor.js +270 -27
  26. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +2 -0
  27. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
  28. package/dist/executors/bounded-exec/hang-diagnostics.js +2 -2
  29. package/dist/executors/bounded-exec/mbx-pool.d.ts +20 -3
  30. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  31. package/dist/executors/bounded-exec/mbx-pool.js +84 -4
  32. package/dist/executors/bounded-exec/mbx-queue.d.ts +30 -0
  33. package/dist/executors/bounded-exec/mbx-queue.d.ts.map +1 -0
  34. package/dist/executors/bounded-exec/mbx-queue.js +229 -0
  35. package/dist/executors/bounded-exec/progress.d.ts +9 -2
  36. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  37. package/dist/executors/bounded-exec/progress.js +148 -22
  38. package/dist/executors/bounded-exec/test-result-writer.d.ts +30 -0
  39. package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -0
  40. package/dist/executors/bounded-exec/test-result-writer.js +126 -0
  41. package/dist/executors/bounded-exec/test-results.d.ts +33 -0
  42. package/dist/executors/bounded-exec/test-results.d.ts.map +1 -0
  43. package/dist/executors/bounded-exec/test-results.js +33 -0
  44. package/dist/executors/bounded-exec/verdict.d.ts +20 -2
  45. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  46. package/dist/executors/bounded-exec/verdict.js +102 -4
  47. package/dist/index.d.ts +3 -2
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +67 -65
  50. package/dist/nx-publish.d.ts +24 -0
  51. package/dist/nx-publish.d.ts.map +1 -0
  52. package/dist/nx-publish.js +150 -0
  53. package/dist/runtime-input-policy.d.ts +14 -0
  54. package/dist/runtime-input-policy.d.ts.map +1 -1
  55. package/dist/runtime-input-policy.js +1 -1
  56. package/dist/tool-bins.d.ts +6 -8
  57. package/dist/tool-bins.d.ts.map +1 -1
  58. package/dist/tool-bins.js +7 -5
  59. package/dist/workspace-index.d.ts +7 -0
  60. package/dist/workspace-index.d.ts.map +1 -1
  61. package/dist/workspace-index.js +11 -0
  62. package/executors.json +1 -1
  63. package/managed/raw/patches/ttsc@0.34.1.patch +75 -0
  64. package/managed/raw/tooling/devenv +1 -2
  65. package/managed/raw/tooling/direnv/developer-links.ts +3 -0
  66. package/managed/raw/tooling/direnv/devenv.smoo.nix +34 -15
  67. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -10
  68. package/managed/raw/tooling/direnv/setup-environment.ts +35 -20
  69. package/managed/raw/tooling/direnv/ttsc-env.sh +8 -6
  70. package/managed/templates/github/actions/cache-node-modules/action.yml +1 -1
  71. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +12 -11
  72. package/package.json +223 -7
  73. package/runtime/nx-publish/cache_adapter.js +45 -0
  74. package/runtime/nx-publish/nx_publish.js +60 -0
  75. package/runtime-input.sh +12 -3
  76. package/src/bin/smoo-nx-publish.ts +4 -0
  77. package/src/cargo-retention-hook.ts +16 -10
  78. package/src/eslint-file-set.ts +4 -1
  79. package/src/eslint-sources.ts +134 -0
  80. package/src/executors/all-verdicts/executor.ts +58 -3
  81. package/src/executors/all-verdicts/schema.json +4 -0
  82. package/src/executors/all-verdicts/schema.ts +14 -0
  83. package/src/executors/bounded-exec/bounds.ts +132 -15
  84. package/src/executors/bounded-exec/executor.ts +303 -27
  85. package/src/executors/bounded-exec/hang-diagnostics.ts +3 -2
  86. package/src/executors/bounded-exec/mbx-pool.ts +82 -5
  87. package/src/executors/bounded-exec/mbx-queue.ts +170 -0
  88. package/src/executors/bounded-exec/progress.ts +168 -15
  89. package/src/executors/bounded-exec/test-result-writer.ts +152 -0
  90. package/src/executors/bounded-exec/test-results.ts +39 -0
  91. package/src/executors/bounded-exec/verdict.ts +108 -5
  92. package/src/index.ts +74 -70
  93. package/src/nx-publish.ts +157 -0
  94. package/src/runtime-input-policy.ts +1 -1
  95. package/src/tool-bins.ts +13 -9
  96. package/src/workspace-index.ts +12 -0
  97. package/test-timings.json +8 -3
@@ -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,5 +1,7 @@
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';
4
+ import type { NativeTestResult } from './test-result-writer.js';
3
5
  import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
4
6
 
5
7
  /**
@@ -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
  }
@@ -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) !== -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) !== -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
@@ -0,0 +1,152 @@
1
+ import { type ClosedTestResult, parseTestSourceRef, TEST_FAILURES_FILE, TEST_RESULTS_FILE } from './test-results.js';
2
+
3
+ export { taskDirectory } from './verdict.js';
4
+
5
+ import { randomUUID } from 'node:crypto';
6
+ import { appendFileSync } from 'node:fs';
7
+ import { type FileHandle, open, rename, rm } from 'node:fs/promises';
8
+ import { join, relative, resolve, sep } from 'node:path';
9
+ /** A runner's closed observation, before the artifact writer assigns its references. */
10
+ export interface NativeTestResult {
11
+ readonly id: string;
12
+ readonly status: ClosedTestResult['status'];
13
+ readonly source: ClosedTestResult['source'];
14
+ readonly contradictsCursor?: number;
15
+ readonly durationMs: number | null;
16
+ readonly sourceRef: string | null;
17
+ readonly failureOutput: string | null;
18
+ }
19
+
20
+ export interface TestResultWriter {
21
+ /** Completes the evidence write before publishing its referring JSONL record. */
22
+ append(result: NativeTestResult): string | null;
23
+ reconcile(results: readonly NativeTestResult[]): string | null;
24
+ close(): Promise<string | null>;
25
+ }
26
+
27
+ /** The cursor includes its own decimal digits: converge on their width before publishing. */
28
+ function resultLine(result: Omit<ClosedTestResult, 'cursor'>, offset: number): { line: string; cursor: number } {
29
+ let cursor = offset;
30
+ for (;;) {
31
+ const line = `${JSON.stringify({ ...result, cursor })}\n`;
32
+ const next = offset + Buffer.byteLength(line);
33
+ if (next === cursor) return { line, cursor };
34
+ cursor = next;
35
+ }
36
+ }
37
+
38
+ function normalizeSourceRef(sourceRef: string | null, sourceRoot: { cwd: string; root: string }): string | null {
39
+ const location = parseTestSourceRef(sourceRef);
40
+ if (location === null) return null;
41
+ return `${relative(sourceRoot.root, resolve(sourceRoot.cwd, location.path)).split(sep).join('/')}:${location.line}`;
42
+ }
43
+ async function closeResultFiles(
44
+ files: readonly FileHandle[],
45
+ stagedPaths: readonly string[] = [],
46
+ ): Promise<string | null> {
47
+ const results = await Promise.allSettled([
48
+ ...files.map((file) => file.close()),
49
+ ...stagedPaths.map((path) => rm(path, { force: true })),
50
+ ]);
51
+ const failures = results.flatMap((result) => (result.status === 'rejected' ? [String(result.reason)] : []));
52
+ return failures.length === 0 ? null : `closing test result artifacts: ${failures.join('; ')}`;
53
+ }
54
+ /** A new inode and UUID identify a replacement run, even when it has the same byte length. */
55
+ export async function createTestResultWriter(
56
+ directory: string,
57
+ sourceRoot: { readonly cwd: string; readonly root: string },
58
+ ): Promise<{ readonly ok: true; readonly writer: TestResultWriter } | { readonly ok: false; readonly error: string }> {
59
+ const runId = randomUUID();
60
+ const target = join(directory, TEST_RESULTS_FILE);
61
+ const staged = `${target}.${runId}`;
62
+ const causePath = join(directory, TEST_FAILURES_FILE);
63
+ const stagedCauses = `${causePath}.${runId}`;
64
+ const [records, causes] = await Promise.allSettled([open(staged, 'ax'), open(stagedCauses, 'ax')]);
65
+ if (records.status !== 'fulfilled' || causes.status !== 'fulfilled') {
66
+ const cleanup = await closeResultFiles(
67
+ [
68
+ ...(records.status === 'fulfilled' ? [records.value] : []),
69
+ ...(causes.status === 'fulfilled' ? [causes.value] : []),
70
+ ],
71
+ [staged, stagedCauses],
72
+ );
73
+ const failures = [records, causes].flatMap((result) =>
74
+ result.status === 'rejected' ? [String(result.reason)] : [],
75
+ );
76
+ return {
77
+ ok: false,
78
+ error: `initializing test result stream ${target}: ${failures.join('; ')}${cleanup === null ? '' : `; ${cleanup}`}`,
79
+ };
80
+ }
81
+ try {
82
+ await rename(stagedCauses, causePath);
83
+ await rename(staged, target);
84
+ } catch (cause) {
85
+ const cleanup = await closeResultFiles([records.value, causes.value], [staged, stagedCauses]);
86
+ return {
87
+ ok: false,
88
+ error: `initializing test result stream ${target}: ${String(cause)}${cleanup === null ? '' : `; ${cleanup}`}`,
89
+ };
90
+ }
91
+ let offset = 0;
92
+ let causeOffset = 0;
93
+ let failure: string | null = null;
94
+ let closing: Promise<string | null> | null = null;
95
+ const provisional = new Map<string, { status: ClosedTestResult['status']; cursor: number }>();
96
+ return {
97
+ ok: true,
98
+ writer: {
99
+ append(result) {
100
+ if (failure !== null) return failure;
101
+ if (closing !== null) return `appending test result ${result.id} to ${target}: stream is closed`;
102
+ try {
103
+ let causeRef: ClosedTestResult['causeRef'] = null;
104
+ if (result.status === 'fail' && result.failureOutput !== null) {
105
+ const endByte = causeOffset + Buffer.byteLength(result.failureOutput);
106
+ appendFileSync(causes.value.fd, result.failureOutput);
107
+ causeRef = { path: TEST_FAILURES_FILE, startByte: causeOffset, endByte };
108
+ causeOffset = endByte;
109
+ }
110
+ const { line, cursor } = resultLine(
111
+ {
112
+ runId,
113
+ id: result.id,
114
+ status: result.status,
115
+ source: result.source,
116
+ ...(result.contradictsCursor === undefined ? {} : { contradictsCursor: result.contradictsCursor }),
117
+ durationMs: result.durationMs,
118
+ sourceRef: normalizeSourceRef(result.sourceRef, sourceRoot),
119
+ causeRef,
120
+ },
121
+ offset,
122
+ );
123
+ // appendFileSync has no user-space buffering: each LF is visible before append returns.
124
+ appendFileSync(records.value.fd, line);
125
+ offset = cursor;
126
+ if (result.source === 'progress') provisional.set(result.id, { status: result.status, cursor });
127
+ return null;
128
+ } catch (cause) {
129
+ failure = `appending test result ${result.id} to ${target}: ${String(cause)}`;
130
+ return failure;
131
+ }
132
+ },
133
+ reconcile(results) {
134
+ for (const result of results) {
135
+ const prior = provisional.get(result.id);
136
+ if (prior !== undefined && prior.status === result.status) continue;
137
+ const error = this.append({
138
+ ...result,
139
+ source: 'junit',
140
+ ...(prior === undefined ? {} : { contradictsCursor: prior.cursor }),
141
+ });
142
+ if (error !== null) return error;
143
+ }
144
+ return null;
145
+ },
146
+ close() {
147
+ closing ??= closeResultFiles([records.value, causes.value]);
148
+ return closing;
149
+ },
150
+ },
151
+ };
152
+ }
@@ -0,0 +1,39 @@
1
+ import typia, { type tags } from 'typia';
2
+
3
+ export const TEST_RESULTS_FILE = 'test-results.jsonl';
4
+ export const TEST_FAILURES_FILE = 'test-failures.log';
5
+
6
+ type ByteOffset = number & tags.Type<'uint64'> & tags.Maximum<9007199254740991>;
7
+
8
+ /** Live observations, not gate evidence: report.xml and verdict.json remain authoritative. */
9
+ export interface ClosedTestResult {
10
+ readonly runId: string & tags.Format<'uuid'>;
11
+ readonly id: string;
12
+ readonly status: 'pass' | 'fail' | 'skip';
13
+ readonly source: 'native' | 'progress' | 'junit';
14
+ /** Set only on a terminal JUnit correction of a provisional progress observation. */
15
+ readonly contradictsCursor?: ByteOffset;
16
+ /** Null when the native runner reports no duration (for example a skipped test). */
17
+ readonly durationMs: (number & tags.Minimum<0>) | null;
18
+ /** Native file:line, never a location inferred from human output. */
19
+ readonly sourceRef: string | null;
20
+ /** UTF-8 byte range [startByte, endByte), relative to the task artifact directory. */
21
+ readonly causeRef: {
22
+ readonly path: typeof TEST_FAILURES_FILE;
23
+ readonly startByte: ByteOffset;
24
+ readonly endByte: ByteOffset;
25
+ } | null;
26
+ /** Next UTF-8 byte offset in test-results.jsonl, including this record's LF. */
27
+ readonly cursor: ByteOffset;
28
+ }
29
+
30
+ export const isClosedTestResult = typia.createEquals<ClosedTestResult>();
31
+
32
+ /** A producer-normalized workspace-relative declaration location, never inferred from a test name. */
33
+ export function parseTestSourceRef(sourceRef: string | null): { path: string; line: number } | null {
34
+ if (sourceRef === null) return null;
35
+ const match = /^(.+):([1-9]\d*)$/.exec(sourceRef);
36
+ const path = match?.[1];
37
+ const line = Number(match?.[2]);
38
+ return path !== undefined && Number.isSafeInteger(line) ? { path, line } : null;
39
+ }