Repository navigation
Expand file tree
/
Copy pathrun-tests.ts
More file actions
1001 lines (963 loc) · 36.3 KB
/
Copy pathrun-tests.ts
File metadata and controls
1001 lines (963 loc) · 36.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { randomUUID } from "node:crypto";
import { appendFile, mkdir, readFile, realpath, writeFile } from "node:fs/promises";
import { availableParallelism, cpus } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { sanitizeError } from "./failure-redaction.ts";
import { startAgentNamespaceCapture } from "./k3d-diagnostics.ts";
import { isOpenShellLane } from "./openshell-full-integration.ts";
import { collectInstallationPhases } from "./openshell-installation-phase.ts";
import { openShellRequiredEnv } from "./openshell-model-inputs.ts";
import { aggregateGroup } from "./runner-aggregate.ts";
import {
case2PhaseEnv,
openK3dCase2Directory,
readK3dCase2Phase,
removeK3dCase2Directory,
type Case2Directory,
} from "./runner-case2-receipt.ts";
import {
failureDetailFiles,
optionalReporterField,
recordFailure,
} from "./runner-failure-diagnostics.ts";
import {
auditManifest,
defaultManifestPath,
exists,
fileConcurrency,
fileInputEnv,
issue,
logIssues,
loadRunnerManifest,
mergeEnv,
oneLine,
parseArgs,
repoRelativePath,
resolveRepoPath,
selectLane,
sourceEvidence,
startOrder,
usage,
type RunnerFile,
type RunnerIssue,
type RunnerLane,
type RunnerManifest,
} from "./runner-inputs.ts";
import { isRecord } from "./records.ts";
import { runCompleteLifecycle } from "./runner-lifecycle.ts";
import {
abandonedSteps,
cleanupLimitMs,
fileTimeout,
laneDeadlineIssue,
laneDeadlinePassed,
loadPrepare,
preparationFailureText,
prepareLimitMs,
sanitizePreparationError,
StepTimeoutError,
validatePreparedEnv,
withinLimit,
type PrepareFile,
} from "./runner-preparation.ts";
import { jobCancelling, killDescendants, runNode, type NodeResult } from "./runner-process.ts";
import {
readFileEvidence,
validateExpectedTests,
type FileEvidence,
} from "./runner-reporter-output.ts";
import { preparationIdentity, readRuntimeProvenance } from "./runtime-provenance.ts";
const entryURL = new URL(import.meta.url);
const repositoryRoot = resolve(fileURLToPath(new URL("../..", entryURL)));
const reporterPath = fileURLToPath(new URL("./reporter.ts", entryURL));
type FileLane = Pick<RunnerLane, "name" | "env" | "requiredEnv" | "optionalEnv">;
type Setup = <T>(step: () => T | PromiseLike<T>) => Promise<T>;
type FilePreparation =
| { statePath: string; prepareFile: PrepareFile | null; preparationId: string }
| { statePath: null; prepareFile: null; preparationId?: undefined };
type FileCleanup = () => unknown;
type CleanupResult =
| { status: "passed" }
| { status: "timeout"; timeoutMs: number }
| { status: "cancelled" | "failed"; error: ReturnType<typeof sanitizeError> };
function isFileCleanup(value: unknown): value is FileCleanup {
return typeof value === "function";
}
function emptyFileResult(path: string, issues: RunnerIssue[]) {
const tests: FileEvidence["tests"] = [];
return {
path,
status: "failed" as const,
nodeExitCode: null,
signal: null,
counts: { passed: 0, failed: 0, skipped: 0, todo: 0, total: 0 },
tests,
issues,
cleanup: null,
};
}
function imageDigests(env: NodeJS.ProcessEnv) {
const names = {
controller: "OCC_TEST_PRODUCTION_CONTROLLER_IMAGE",
runtime: "OCC_TEST_KUBERNETES_RUNTIME_IMAGE",
controllerUpgrade: "OCC_TEST_PRODUCTION_UPGRADE_CONTROLLER_IMAGE",
runtimeUpgrade: "OCC_TEST_PRODUCTION_UPGRADE_RUNTIME_IMAGE",
nativeBaselineController: "OCC_TEST_NATIVE_BASELINE_CONTROLLER_IMAGE",
nativeBaselineRuntime: "OCC_TEST_NATIVE_BASELINE_RUNTIME_IMAGE",
repositoryCredentials: "OCC_TEST_REPOSITORY_CREDENTIALS_IMAGE",
postgres: "OCC_TEST_PRODUCTION_POSTGRES_IMAGE",
node: "OCC_TEST_PRODUCTION_NODE_IMAGE",
fixture: "OCC_TEST_KUBERNETES_IMAGE",
gateway: "OCC_TEST_KUBERNETES_GATEWAY_IMAGE",
codex: "OCC_TEST_KUBERNETES_AGENT_IMAGE",
collector: "OCC_TEST_OBSERVABILITY_COLLECTOR_IMAGE",
prometheus: "OCC_TEST_OBSERVABILITY_PROMETHEUS_IMAGE",
grafana: "OCC_TEST_OBSERVABILITY_GRAFANA_IMAGE",
loki: "OCC_TEST_OBSERVABILITY_LOKI_IMAGE",
pairController: "OCC_PROBE_CONTROLLER_IMAGE",
pairBroker: "OCC_PROBE_BROKER_IMAGE",
pairOldController: "OCC_PROBE_OLD_CONTROLLER_IMAGE",
pairOldBroker: "OCC_PROBE_OLD_BROKER_IMAGE",
releasedController: "OCC_TEST_RELEASED_CONTROLLER_IMAGE",
candidateController: "OCC_TEST_CANDIDATE_CONTROLLER_IMAGE",
};
return Object.fromEntries<string>(
Object.entries(names).flatMap<[string, string]>(([role, name]) => {
const digest = env[name]?.match(/@(sha256:[a-f0-9]{64})$/)?.[1];
return digest ? [[role, digest]] : [];
}),
);
}
async function runFile(
root: string,
lane: FileLane,
file: RunnerFile,
{ statePath, prepareFile, preparationId }: FilePreparation,
setup: Setup,
testName?: unknown,
) {
const issues: RunnerIssue[] = [];
const relativePath = repoRelativePath(root, file.path);
const absolutePath = resolveRepoPath(root, file.path, issues, relativePath);
const prepared: {
defaults: NodeJS.ProcessEnv;
env: NodeJS.ProcessEnv;
cleanup: FileCleanup | null;
} = { defaults: {}, env: {}, cleanup: null };
let cleanupResult: CleanupResult | null = null;
if (!absolutePath || !(await exists(absolutePath))) {
issues.push(
issue("missing-file", `selected file is missing: ${relativePath}`, { file: relativePath }),
);
return emptyFileResult(relativePath, issues);
}
// A file queued behind a hung one starts nothing past the lane deadline.
if (laneDeadlinePassed()) {
issues.push(laneDeadlineIssue(relativePath));
return emptyFileResult(relativePath, issues);
}
// An abandoned preparation or cleanup most likely still holds the CI state lock. This file's
// preparation would wait for it until the deadline and then report a prepare-timeout of its own.
if (abandonedSteps > 0) {
issues.push(
issue(
"setup-abandoned",
`not started: an earlier file's preparation or cleanup did not finish: ${relativePath}`,
{ file: relativePath },
),
);
return emptyFileResult(relativePath, issues);
}
const inputs = fileInputEnv(lane, file);
const modelInputs = mergeEnv(inputs, file.env);
if (prepareFile) {
try {
const result =
(await setup(() =>
withinLimit(
() =>
prepareFile({
lane: { name: lane.name, env: lane.env, requiredEnv: lane.requiredEnv },
file,
statePath,
modelSelectors: {
OCC_TEST_OPENSHELL_HARNESS: modelInputs.OCC_TEST_OPENSHELL_HARNESS,
OCC_TEST_OPENSHELL_MODEL_BASE_URL: modelInputs.OCC_TEST_OPENSHELL_MODEL_BASE_URL,
},
}),
prepareLimitMs(),
),
)) ?? {};
const cleanup = optionalReporterField(result, "cleanup") ?? null;
if (cleanup !== null && !isFileCleanup(cleanup)) {
throw new Error("prepareFile cleanup must be a function");
}
// A returned resource owner still needs cleanup when its output is invalid.
prepared.cleanup = cleanup;
prepared.defaults = validatePreparedEnv(optionalReporterField(result, "defaults"));
prepared.env = validatePreparedEnv(optionalReporterField(result, "env"));
} catch (error) {
if (error instanceof StepTimeoutError) {
// Its turn in the setup queue came after the deadline, or it ran into the deadline.
issues.push(
error.started
? issue(
"prepare-timeout",
`prepareFile did not finish within ${error.limitMs} ms, cut to the lane deadline: ${relativePath}`,
{ file: relativePath, step: "prepare", timeoutMs: error.limitMs },
)
: laneDeadlineIssue(relativePath),
);
return emptyFileResult(relativePath, issues);
}
const sanitized = sanitizePreparationError(error);
issues.push(
issue("prepare-failed", `prepareFile failed for ${relativePath}`, {
file: relativePath,
error: sanitized,
}),
);
await recordFailure(
setup,
statePath,
lane.name,
relativePath,
{
reason: "prepare",
error: { ...sanitized, ...(await preparationFailureText(error, lane, statePath, root)) },
},
preparationId,
);
if (!prepared.cleanup) {
return emptyFileResult(relativePath, issues);
}
}
}
// A preparation that ended just as its limit ran out; the cleanup below still runs.
if (laneDeadlinePassed()) {
issues.push(laneDeadlineIssue(relativePath));
}
const requiredEnv = [...lane.requiredEnv, ...file.requiredEnv];
const env = mergeEnv(inputs, prepared.defaults, file.env, prepared.env);
for (const name of openShellRequiredEnv(lane.name, relativePath, requiredEnv, env)) {
if (env[name] === undefined || env[name] === "") {
issues.push(
issue("missing-env", `lane ${lane.name} requires env ${name}`, {
lane: lane.name,
env: name,
}),
);
}
}
let nodeResult: NodeResult | null = null;
const evidence: FileEvidence = {
tests: [],
skippedSuites: [],
measurements: [],
testRunExercised: false,
};
let nodeElapsedMs: number | undefined;
let agentActivity: Awaited<ReturnType<typeof startAgentNamespaceCapture>>;
const installationProgress =
["openshell", "openshell-policy"].includes(lane.name) ||
(lane.name === "openshell-installers" &&
relativePath === "tests/integration/openshell-upstream-real.test.ts");
const installationPhases = collectInstallationPhases();
let timeout: ReturnType<typeof fileTimeout> | undefined;
let case2Directory: Case2Directory | undefined;
let case2Phase: ReturnType<typeof readK3dCase2Phase>;
try {
if (issues.length === 0) {
// An externally prepared release case has no diagnostic collection state.
if (statePath) {
agentActivity = await startAgentNamespaceCapture({
statePath,
lane: lane.name,
file: relativePath,
preparationId,
}).catch(() => {
console.error(
`[run:${lane.name}] Agent namespace activity unavailable for ${relativePath}`,
);
return undefined;
});
}
// The capture names a private directory for the file's container log records.
Object.assign(env, agentActivity?.env);
delete env[case2PhaseEnv];
if (
lane.name === "k3d-model" &&
relativePath === "tests/integration/harness-topology-k3d-real.test.mjs"
) {
case2Directory = openK3dCase2Directory(statePath);
if (case2Directory) {
const { dev, ino } = case2Directory.identity;
env[case2PhaseEnv] = JSON.stringify({ path: case2Directory.path, dev, ino });
}
}
if (installationProgress) {
env.OCC_TEST_OPENSHELL_INSTALLATION_PROGRESS = "1";
}
const nodeStarted = performance.now();
timeout = fileTimeout();
const args = ["--test", "--test-reporter", reporterPath];
if (testName !== undefined) {
// Only the Node child needs a regex. Parent accounting uses the literal
// name, and rejects any extra cases selected by Node's ancestry rules.
if (typeof testName !== "string") {
throw new TypeError("The test name must be a string.");
}
const escaped = testName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
args.push("--test-name-pattern", new RegExp(`^${escaped}$`).toString());
}
// Node reports physical case locations but names its file wrapper after the argument.
const reporterRoot = await realpath(root);
args.push(absolutePath);
nodeResult = await runNode(args, {
cwd: root,
env,
timeout: timeout.timeoutMs,
onProgress: (progress) => {
const { kind, stage } = progress;
if (kind === "openshell-installation") {
if (installationProgress) {
installationPhases.accept(progress);
}
return;
}
if (kind === "openshell-stage-failure") {
process.stderr.write(
`[run:${lane.name}] ${relativePath}: ${kind} ${stage} ${progress.category} code=${progress.code ?? "unknown"} workflow=${progress.location}\n`,
);
return;
}
process.stderr.write(
`[run:${lane.name}] ${relativePath}: ${kind} ${stage} ${progress.outcome}\n`,
);
},
});
nodeElapsedMs = Math.round(performance.now() - nodeStarted);
readFileEvidence(
nodeResult,
{
root: reporterRoot,
absolutePath,
relativePath,
laneName: lane.name,
expectedTests: file.expectedTests,
env,
focused: testName !== undefined,
},
evidence,
issues,
);
}
} catch (error) {
issues.push(
issue("runner-error", `runner failed to read Node reporter output for ${relativePath}`, {
file: relativePath,
error: sanitizeError(error),
}),
);
} finally {
delete env[case2PhaseEnv];
if (case2Directory) {
case2Phase = readK3dCase2Phase(case2Directory);
removeK3dCase2Directory(case2Directory);
}
// Capture before cleanup so passing k3d runs keep their Agent Pod timeline.
// The capture updates the lane's diagnostics file; keep it out of other setup.
await setup(async () => agentActivity?.finish({ env }));
const { cleanup } = prepared;
if (cleanup) {
try {
await setup(() => withinLimit(cleanup, cleanupLimitMs()));
cleanupResult = { status: "passed" };
} catch (error) {
if (error instanceof StepTimeoutError) {
cleanupResult = { status: "timeout", timeoutMs: error.limitMs };
issues.push(
issue(
"cleanup-timeout",
error.started
? `cleanup did not finish within ${error.limitMs} ms: ${relativePath}`
: `the cleanup window after the lane deadline closed before cleanup started: ${relativePath}`,
{ file: relativePath, step: "cleanup", timeoutMs: error.limitMs },
),
);
} else if (jobCancelling()) {
// Keep the sanitized error: a parent that died for another reason (OOM-killed step
// shell, closed terminal) also reads as a cancellation.
cleanupResult = { status: "cancelled", error: sanitizeError(error) };
issues.push(
issue("cleanup-cancelled", `cleanup stopped by job cancellation for ${relativePath}`, {
file: relativePath,
}),
);
} else {
cleanupResult = { status: "failed", error: sanitizeError(error) };
issues.push(
issue("cleanup-failed", `cleanup failed for ${relativePath}`, { file: relativePath }),
);
}
}
}
}
const {
tests,
skippedSuites,
fileFailure,
details,
measurements,
unfinishedStage,
openShellReplacement,
testRunExercised,
} = evidence;
if (
nodeResult &&
(tests.length === 0 ||
(testName !== undefined && !tests.some((testCase) => testCase.name === testName)))
) {
issues.push(
issue("selected-zero", `selected file produced no test cases: ${relativePath}`, {
file: relativePath,
}),
);
}
if (
nodeResult &&
testName !== undefined &&
(tests.some((testCase) => testCase.name !== testName) || tests.length > 1)
) {
issues.push(
issue("unexpected-test-selection", "--test-name must execute only one exactly named case", {
file: relativePath,
}),
);
}
const timedOut = timeout && nodeResult?.error?.code === "ETIMEDOUT" ? timeout : undefined;
if (timedOut) {
// Name the tests it was running in the job log too: the diagnostics report is an artifact.
const names = (details?.interruptedTests ?? []).map(({ name }) => JSON.stringify(name));
const running = names.slice(0, 5);
if (names.length > running.length) {
running.push(`and ${names.length - running.length} more`);
}
issues.push(
issue(
"test-timeout",
`selected file exceeded the runner timeout (${timedOut.timeoutMs} ms${timedOut.laneDeadline ? ", cut to the lane deadline" : ""}): ${relativePath}${running.length === 0 ? "; no test was running (stopped in a hook, teardown or top-level code)" : ` while running ${running.join(", ")}`}`,
{ file: relativePath },
),
);
}
if (nodeResult) {
validateExpectedTests(relativePath, file.expectedTests, tests, issues);
for (const skipped of tests.filter((testCase) =>
["skipped", "todo"].includes(testCase.status),
)) {
issues.push(
issue("unexpected-skip", `selected test did not run to completion: ${skipped.name}`, {
file: relativePath,
name: skipped.name,
status: skipped.status,
}),
);
}
for (const suite of skippedSuites) {
issues.push(
issue("unexpected-skip", `selected suite did not run its tests: ${suite.name}`, {
file: relativePath,
name: suite.name,
...(suite.source ? { source: suite.source } : {}),
status: "skipped",
}),
);
}
}
const counts = { passed: 0, failed: 0, skipped: 0, todo: 0, total: tests.length };
for (const testCase of tests) {
counts[testCase.status] += 1;
}
const nodeExitCode = nodeResult ? (nodeResult.status ?? (nodeResult.signal ? 1 : 0)) : null;
const status = nodeExitCode === 0 && issues.length === 0 ? "passed" : "failed";
// A file the runner timeout stopped always gets a record, even with nothing captured.
if (statePath && status === "failed" && (details !== undefined || timedOut)) {
await recordFailure(
setup,
statePath,
lane.name,
relativePath,
{
...(timedOut
? {
reason: "timeout",
timeoutMs: timedOut.timeoutMs,
...(timedOut.laneDeadline ? { laneDeadline: true } : {}),
elapsedMs: nodeElapsedMs,
}
: { reason: "failed" }),
...(details ?? { tests: [], omittedTests: 0, output: { lines: [], omittedLines: 0 } }),
},
preparationId,
);
}
const [firstTest] = tests;
const exercised =
testRunExercised &&
issues.every(({ code, status }) => code === "expected-test-not-passed" && status === "failed");
return {
path: relativePath,
status,
nodeExitCode,
signal: nodeResult?.signal ?? null,
...(exercised ? { exercised: true } : {}),
...(nodeResult?.processGroupId ? { processGroupId: nodeResult.processGroupId } : {}),
...(fileFailure ? { fileFailure } : {}),
counts,
tests,
...(measurements.length > 0 ? { measurements } : {}),
...(unfinishedStage &&
((timedOut && tests.length === 0) ||
(tests.length === 1 &&
firstTest?.status === "failed" &&
file.expectedTests?.length === 1 &&
firstTest.name === file.expectedTests[0]))
? { openShellUnfinishedStage: unfinishedStage }
: {}),
...(openShellReplacement ? { openShellReplacement } : {}),
...(case2Phase ? { case2Phase } : {}),
...(installationProgress ? { installationPhases: installationPhases.result() } : {}),
issues,
cleanup: cleanupResult,
imageDigests: imageDigests(env),
};
}
export type PreparedSigninRunFile = (
root: string,
lane: FileLane,
file: RunnerFile,
) => ReturnType<typeof runFile>;
type FileMode = "serial" | "parallel";
type ScheduledFileResult = Awaited<ReturnType<typeof runFile>> & {
mode: FileMode;
startOffsetMs: number;
wallDurationMs: number;
};
async function runLane(
root: string,
manifest: RunnerManifest,
laneName: unknown,
statePath: unknown,
resultsPath: unknown,
selectedFile: unknown,
testName: unknown,
) {
const source = sourceEvidence(root);
const { lane, issues } = selectLane(manifest, laneName, selectedFile, testName);
issues.push(...source.issues);
const files: ScheduledFileResult[] = [];
let preservedNodeExitCode = 0;
let preparationId: string = randomUUID();
try {
if (typeof statePath === "string") {
const state: unknown = JSON.parse(await readFile(statePath, "utf8"));
if (optionalReporterField(state, "lane") === laneName) {
preparationId =
preparationIdentity(optionalReporterField(state, "prefix")) ?? preparationId;
}
}
} catch {
// Stateless runs still share one fresh identity across their files.
}
const identity = { lane: laneName, preparationId };
const initialRuntimeProvenance = await readRuntimeProvenance(statePath, identity);
const prepareFile = await loadPrepare(root);
const startedAt = new Date().toISOString();
const laneStarted = performance.now();
const concurrency = lane ? fileConcurrency(lane, issues) : 1;
if (lane && lane.fileConcurrency > 1) {
const measured = lane.parallelFiles.filter((path) => manifest.durations.has(path)).length;
process.stderr.write(
`run-tests: ${lane.name} runs up to ${concurrency} files at a time (fileConcurrency ${lane.fileConcurrency}, available parallelism ${availableParallelism()}, CPUs ${cpus().length})${measured > 0 ? `, longest first (${measured} of ${lane.parallelFiles.length} shared files measured)` : ""}\n`,
);
}
if (lane && issues.length === 0) {
if (typeof statePath !== "string") {
throw new TypeError("The lane state path must be a string.");
}
await mkdir(dirname(statePath), { recursive: true });
// prepareFile and cleanup update the lane state file; never interleave them.
let setupQueue: Promise<unknown> = Promise.resolve();
const setup: Setup = (step) => {
const next = setupQueue.then(step);
setupQueue = next.catch(() => {});
return next;
};
const results = new Array<ScheduledFileResult>(lane.files.length);
let annotateFailure = process.env.GITHUB_ACTIONS === "true";
// The Full Integration view splits the OpenShell lane before selection.
const splitOpenShellLane =
process.env.OCC_CI_SUITE_VIEW === "full-integration" && isOpenShellLane(lane.name);
const originalFileOrder =
lane.files.length === manifest.lanes.get(lane.name)?.files.length && !splitOpenShellLane;
const runAt = async (index: number, mode: FileMode) => {
const file = lane.files[index];
if (!file) {
throw new Error("Runner schedule refers to a missing manifest file.");
}
const fileStarted = performance.now();
const result = await runFile(
root,
lane,
file,
{ statePath, prepareFile, preparationId },
setup,
testName,
);
// Round both ends, so start + duration is the rounded end and never passes a
// later file's start.
const startOffsetMs = Math.round(fileStarted - laneStarted);
const wallDurationMs = Math.round(performance.now() - laneStarted) - startOffsetMs;
results[index] = { ...result, mode, startOffsetMs, wallDurationMs };
if (annotateFailure && result.status === "failed") {
annotateFailure = false;
// A workflow command must never contain the child test's name, path, or output.
const message =
result.counts.failed > 0 && result.cleanup?.status === "passed"
? "A test file reported failed tests and its cleanup passed."
: "A test file failed.";
const ordinal = originalFileOrder ? index + 1 : "UNKNOWN";
process.stderr.write(
`::error title=Test lane failure::${message} Final lane results will follow.\n` +
`::notice title=Test lane first failed file hint::Original manifest file ordinal ${ordinal}.\n`,
);
if (splitOpenShellLane && selectedFile === undefined && testName === undefined) {
process.stderr.write(
`::notice title=Test lane first failed split file hint::Split-lane file position ${index + 1}.\n`,
);
}
}
process.stderr.write(
`run-tests: ${result.status} ${result.path} ${(wallDurationMs / 1000).toFixed(1)}s${mode === "parallel" ? " (parallel)" : ""}\n`,
);
};
const indexes = lane.files.map((_, index) => index);
const parallel =
concurrency > 1
? startOrder(lane.parallelFiles, manifest.durations).map((path) =>
lane.files.findIndex((file) => file.path === path),
)
: [];
// Serial files run first and alone, in manifest order; then audited files share
// `concurrency` slots, started longest first (see fileDurations).
for (const index of indexes.filter((entry) => !parallel.includes(entry))) {
await runAt(index, "serial");
}
const queue = [...parallel];
await Promise.all(
Array.from({ length: Math.min(concurrency, queue.length) }, async () => {
let index: number | undefined;
while ((index = queue.shift()) !== undefined) {
await runAt(index, "parallel");
}
}),
);
for (const result of results) {
files.push(result);
if (preservedNodeExitCode === 0 && result.nodeExitCode && result.nodeExitCode !== 0) {
preservedNodeExitCode = result.nodeExitCode;
}
}
}
const allIssues = [...issues, ...files.flatMap((file) => file.issues)];
const status =
allIssues.length === 0 && files.every((file) => file.status === "passed") ? "passed" : "failed";
const summary = {
version: 1,
command: "run",
sourceSha: source.sha,
runtimeProvenance:
(await readRuntimeProvenance(statePath, identity)) ?? initialRuntimeProvenance,
lane: laneName,
scope: selectedFile === undefined ? "full" : "focused",
...(testName === undefined ? {} : { testName }),
status,
exitCode: preservedNodeExitCode || (status === "passed" ? 0 : 1),
fileConcurrency: concurrency,
startedAt,
endedAt: new Date().toISOString(),
counts: files.reduce(
(counts, file) => ({
passed: counts.passed + file.counts.passed,
failed: counts.failed + file.counts.failed,
skipped: counts.skipped + file.counts.skipped,
todo: counts.todo + file.counts.todo,
total: counts.total + file.counts.total,
}),
{ passed: 0, failed: 0, skipped: 0, todo: 0, total: 0 },
),
issues: allIssues,
files,
};
await writeSummary(resultsPath, summary);
logFailures(files);
logIssues(allIssues);
for (const path of failureDetailFiles) {
process.stderr.write(
`run-tests: whole failure messages, stacks and output tails are in ${path} (artifact diagnostics-<prefix>-${laneName}-attempt-${process.env.GITHUB_RUN_ATTEMPT || "<N>"})\n`,
);
}
return summary.exitCode;
}
// The job log keeps every attempt, so name each failure there as well. The
// reporter has already bounded and redacted the message.
function logFailures(files: readonly ScheduledFileResult[]) {
for (const file of files) {
const failures: { name: unknown; line: unknown; error: unknown }[] = file.tests.filter(
(testCase) => testCase.status === "failed",
);
if ("fileFailure" in file && file.fileFailure) {
failures.push({ name: "(file)", line: undefined, error: file.fileFailure.error });
}
for (const { name, line, error } of failures) {
const location = optionalReporterField(error, "location");
const at = optionalReporterField(location, "line") ?? line;
const failureMessage = optionalReporterField(error, "message");
const frame = optionalReporterField(error, "frame");
const message = oneLine(failureMessage);
process.stderr.write(
`run-tests: failed ${file.path}${at ? `:${at}` : ""} ${JSON.stringify(name)}${message ? `: ${message}` : ""}${frame ? ` (${oneLine(frame)})` : ""}\n`,
);
}
}
}
async function writeSummary(path: unknown, summary: unknown) {
if (typeof path !== "string") {
throw new TypeError("The result path must be a string.");
}
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(summary, null, 2)}\n`);
}
// Complete mode owns a fresh attempt. Phase children keep their group leader
// alive until the parent acknowledges the result, so its final signal cannot
// target a reused group ID. Ordinary externally managed runs keep their behavior.
async function completePhaseChild() {
if (!process.send || process.env.OPENCLAW_CI_COMPLETE_PHASE !== "1") {
throw new Error("A lifecycle phase requires its owning IPC channel.");
}
const stop = () => process.kill(-process.pid, "SIGKILL");
process.once("disconnect", stop);
process.once("exit", stop);
process.on("message", (message: unknown) => {
if (optionalReporterField(message, "type") === "ci-lifecycle-ack") {
stop();
}
});
const request = await new Promise<unknown>((resolveRequest) => {
process.once("message", resolveRequest);
});
let exitCode = 1;
let uncertain = false;
let error: { code: "phase-failed" } | undefined;
try {
// Null has no fields. Other JSON values with no phase leave the default reply.
if (request === null || request === undefined) {
throw new TypeError("Missing lifecycle phase request.");
}
const { phase, root, lane, statePath, resultsPath, manifestPath, file, testName } = isRecord(
request,
)
? request
: {};
if (phase === "prepare") {
if (typeof root !== "string") {
throw new Error("Invalid lifecycle phase root.");
}
const owner: unknown = await import(pathToFileURL(join(root, "scripts/ci/prepare.ts")).href);
if (!isRecord(owner) || typeof owner.prepareLane !== "function") {
throw new Error("Invalid lifecycle preparation owner.");
}
const prepared: unknown = Reflect.apply(owner.prepareLane, owner, [
{ lane, statePath, file },
]);
await prepared;
exitCode = 0;
} else if (phase === "cleanup") {
if (typeof root !== "string") {
throw new Error("Invalid lifecycle phase root.");
}
const owner: unknown = await import(pathToFileURL(join(root, "scripts/ci/cleanup.ts")).href);
if (!isRecord(owner) || typeof owner.cleanupState !== "function") {
throw new Error("Invalid lifecycle cleanup owner.");
}
const cleaned: unknown = Reflect.apply(owner.cleanupState, owner, [statePath]);
await cleaned;
exitCode = 0;
} else if (phase === "run") {
if (typeof manifestPath !== "string") {
throw new Error("Invalid lifecycle manifest path.");
}
const manifest = loadRunnerManifest(manifestPath, process.env.OCC_CI_SUITE_VIEW);
if (typeof root !== "string") {
throw new Error("Invalid lifecycle phase root.");
}
exitCode = await runLane(root, manifest, lane, statePath, resultsPath, file, testName);
uncertain = abandonedSteps > 0;
}
} catch {
// Command owners can embed opaque argv and output in Error.message. Only
// this closed category crosses the phase boundary, never exception text.
error = { code: "phase-failed" };
exitCode = 1;
}
process.send({ type: "ci-lifecycle-result", exitCode, uncertain, error });
await new Promise(() => {});
}
async function main() {
const { command, positionals, options } = parseArgs(process.argv.slice(2));
if (command === "prepared-signin") {
if (positionals.length || Object.keys(options).length) {
throw new Error(
"prepared-signin accepts no file, command, preparation or manifest overrides",
);
}
const { runPreparedSigninCase } = await import("./prepared-signin-case.ts");
const exitCode: number = await runPreparedSigninCase(repositoryRoot, (root, lane, file) =>
runFile(root, lane, file, { statePath: null, prepareFile: null }, async (step) => step()),
);
return exitCode;
}
const root = resolve(options.root ?? repositoryRoot);
const testName = options["test-name"];
if (testName !== undefined) {
if (command !== "run" || options.file === undefined) {
throw new Error("--test-name requires run with --file.");
}
if (testName.trim().length === 0 || Buffer.byteLength(testName, "utf8") > 256) {
throw new Error("--test-name must be a nonempty literal of at most 256 UTF-8 bytes.");
}
if (testName === "<root>" || testName === resolve(root, options.file)) {
throw new Error("--test-name cannot select Node's reserved root or file-wrapper name.");
}
process.stderr.write(
`run-tests: focused file ${JSON.stringify(options.file)} testName ${JSON.stringify(testName)}\n`,
);
}
const manifestPath = resolve(root, options.manifest ?? defaultManifestPath);
if (
command === "run" &&
options.lifecycle === "complete" &&
manifestPath !== resolve(root, defaultManifestPath)
) {
throw new Error(
"Complete lifecycle requires the canonical scripts/ci/test-suites.json manifest under --root.",
);
}
const manifest = loadRunnerManifest(manifestPath, process.env.OCC_CI_SUITE_VIEW);
if (options.lifecycle !== undefined && (command !== "run" || options.lifecycle !== "complete")) {
throw new Error("--lifecycle supports only run --lifecycle complete.");
}
if (options.attempt !== undefined && (command !== "run" || options.lifecycle !== "complete")) {
throw new Error("--attempt requires run --lifecycle complete.");
}
if (command === "check-selection") {
if (!positionals[0] || options.file === undefined) {
throw new Error(`check-selection requires <lane> and --file\n${usage()}`);
}
const { issues } = selectLane(manifest, positionals[0], options.file);
logIssues(issues);
return issues.length === 0 ? 0 : 1;
}
if (command === "audit") {
const summary = await auditManifest(root, manifest);
process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
return summary.status === "passed" ? 0 : 1;
}
if (command === "run") {
const laneName = positionals[0];
if (options.lifecycle === "complete") {
if (
!laneName ||
!options.attempt ||
options.state !== undefined ||
options.results !== undefined
) {
throw new Error(
"Complete lifecycle requires <lane> and --attempt, without --state or --results.",
);
}
return runCompleteLifecycle(
entryURL,
root,
manifest,
manifestPath,
laneName,
resolve(root, options.attempt),
options.file,
testName,
);
}
if (!laneName || !options.state || !options.results) {
throw new Error(`run requires <lane>, --state, and --results\n${usage()}`);
}
return runLane(
root,
manifest,
laneName,
resolve(root, options.state),
resolve(root, options.results),
options.file,
testName,
);
}
const groupName = positionals[0];
if (!groupName || !options["results-dir"]) {
throw new Error(`aggregate requires <group> and --results-dir\n${usage()}`);
}
const summary = await aggregateGroup(
root,
manifest,
groupName,
resolve(root, options["results-dir"]),
options.needs ? resolve(root, options.needs) : null,
options.lanes,
options["release-selection"] ? resolve(root, options["release-selection"]) : null,
);
if (
options["full-coverage-marker"] &&
summary.fullCoverageExercised &&
process.env.GITHUB_REF === "refs/heads/main" &&
process.env.GITHUB_RUN_ATTEMPT === "1" &&
summary.sourceSha === process.env.GITHUB_SHA &&
process.env.GITHUB_OUTPUT
) {
try {
await writeFile(options["full-coverage-marker"], "1\n", { flag: "wx" });
await appendFile(process.env.GITHUB_OUTPUT, "full_coverage_exercised=true\n");
} catch {
process.stderr.write("run-tests: full coverage marker could not be written.\n");
}
}
process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
return summary.status === "passed" ? 0 : 1;
}
try {
if (process.argv[2] === "__lifecycle-phase") {
await completePhaseChild();
} else {
process.exitCode = await main();
}
} catch (error) {
console.error(optionalReporterField(error, "message"));
process.exitCode = 1;
}
// An abandoned preparation or cleanup can keep this process alive (a child it waits for, a
// timer); the results are written, so stop here. Its commands run in this process's group and
// would outlive it as orphans on a self-hosted runner (finding 1225), so end them first. Every
// test file's process group has exited by now.
if (abandonedSteps > 0) {
killDescendants();
process.exit();