@milaboratories/pl-middle-layer 2.2.0 → 2.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 (41) hide show
  1. package/dist/middle_layer/block_status_fold.cjs +382 -0
  2. package/dist/middle_layer/block_status_fold.cjs.map +1 -0
  3. package/dist/middle_layer/block_status_fold.js +373 -0
  4. package/dist/middle_layer/block_status_fold.js.map +1 -0
  5. package/dist/middle_layer/block_status_tree.cjs +287 -0
  6. package/dist/middle_layer/block_status_tree.cjs.map +1 -0
  7. package/dist/middle_layer/block_status_tree.js +283 -0
  8. package/dist/middle_layer/block_status_tree.js.map +1 -0
  9. package/dist/middle_layer/build_stamp.cjs +1 -1
  10. package/dist/middle_layer/build_stamp.js +1 -1
  11. package/dist/middle_layer/project.cjs +73 -2
  12. package/dist/middle_layer/project.cjs.map +1 -1
  13. package/dist/middle_layer/project.d.ts +22 -1
  14. package/dist/middle_layer/project.d.ts.map +1 -1
  15. package/dist/middle_layer/project.js +74 -3
  16. package/dist/middle_layer/project.js.map +1 -1
  17. package/dist/model/project_model.cjs +27 -0
  18. package/dist/model/project_model.cjs.map +1 -1
  19. package/dist/model/project_model.js +25 -1
  20. package/dist/model/project_model.js.map +1 -1
  21. package/dist/mutator/project.cjs +2 -1
  22. package/dist/mutator/project.cjs.map +1 -1
  23. package/dist/mutator/project.d.ts.map +1 -1
  24. package/dist/mutator/project.js +3 -2
  25. package/dist/mutator/project.js.map +1 -1
  26. package/dist/mutator/template/render_block.cjs +24 -0
  27. package/dist/mutator/template/render_block.cjs.map +1 -1
  28. package/dist/mutator/template/render_block.js +23 -2
  29. package/dist/mutator/template/render_block.js.map +1 -1
  30. package/package.json +14 -14
  31. package/src/middle_layer/block_status_bench.test.ts +197 -0
  32. package/src/middle_layer/block_status_fold.test.ts +694 -0
  33. package/src/middle_layer/block_status_fold.ts +563 -0
  34. package/src/middle_layer/block_status_tree.test.ts +253 -0
  35. package/src/middle_layer/block_status_tree.ts +391 -0
  36. package/src/middle_layer/project.ts +111 -2
  37. package/src/middle_layer/project_pruning.test.ts +59 -0
  38. package/src/model/project_model.ts +30 -0
  39. package/src/mutator/project.ts +7 -1
  40. package/src/mutator/project_action_context.test.ts +13 -3
  41. package/src/mutator/template/render_block.ts +32 -2
@@ -0,0 +1,694 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ActionContextNode } from "./block_status_fold";
3
+ import { failureHeadline, foldBlockStatus } from "./block_status_fold";
4
+ import type { BlockStatusJobs, BlockStatusState } from "@milaboratories/pl-model-middle-layer";
5
+
6
+ // ── Fixtures ─────────────────────────────────────────────────────────────────
7
+
8
+ /** One transition, written the way the reporters write it. */
9
+ type TransitionFixture = {
10
+ namespace: string;
11
+ /** The resource that reported. Defaults to the context's own render. */
12
+ reporter?: string;
13
+ /** Milliseconds since the epoch; the key carries nanoseconds. */
14
+ atMs: number;
15
+ state: BlockStatusState | string;
16
+ detail?: string;
17
+ reason?: string;
18
+ };
19
+
20
+ type ContextFixture = {
21
+ id: string;
22
+ data?: Record<string, unknown>;
23
+ transitions?: TransitionFixture[];
24
+ children?: ContextFixture[];
25
+ /** Extra KV entries: readings, and malformed input. */
26
+ kv?: Record<string, string>;
27
+ };
28
+
29
+ /** Builds an {@link ActionContextNode} over a fixture, with the backend's key layout. */
30
+ function context(fixture: ContextFixture): ActionContextNode {
31
+ const kv = new Map<string, string>(Object.entries(fixture.kv ?? {}));
32
+ for (const t of fixture.transitions ?? []) {
33
+ const reporter = t.reporter ?? fixture.id;
34
+ const ns = String(BigInt(t.atMs) * 1_000_000n);
35
+ kv.set(
36
+ `transition/${t.namespace}/${reporter}/${ns}`,
37
+ JSON.stringify({ state: t.state, detail: t.detail ?? "", reason: t.reason ?? "" }),
38
+ );
39
+ }
40
+
41
+ const children = (fixture.children ?? []).map(context);
42
+ const childFields = new Map(children.map((c) => [`child/${c.id}`, c]));
43
+
44
+ return {
45
+ id: fixture.id,
46
+ getData: () => fixture.data,
47
+ listKeyValuesAsString: (prefix: string) =>
48
+ [...kv.entries()]
49
+ .filter(([key]) => key.startsWith(prefix))
50
+ .map(([key, value]) => ({ key, value })),
51
+ listFields: () => [...childFields.keys()],
52
+ traverse: (field: string) => childFields.get(field),
53
+ };
54
+ }
55
+
56
+ /** The jobs not ended, as `<namespace>/<state>: count`. */
57
+ function counts(jobs: BlockStatusJobs): Record<string, number> {
58
+ return Object.fromEntries(jobs.byState.map((g) => [`${g.namespace}/${g.state}`, g.count]));
59
+ }
60
+
61
+ /** One group of the jobs not ended. */
62
+ function group(jobs: BlockStatusJobs, key: string) {
63
+ return jobs.byState.find((g) => `${g.namespace}/${g.state}` === key);
64
+ }
65
+
66
+ const t0 = Date.UTC(2026, 9, 27, 12, 0, 0);
67
+ const ms = (offset: number) => t0 + offset;
68
+
69
+ /** The render's own end, as the workflow controller records it. */
70
+ const renderCompleted = (atMs: number, detail?: string): TransitionFixture => ({
71
+ namespace: "render",
72
+ atMs,
73
+ state: "Completed",
74
+ detail,
75
+ });
76
+
77
+ // ── The shape of the summary ─────────────────────────────────────────────────
78
+
79
+ describe("foldBlockStatus", () => {
80
+ it("reports a context that has not reported as live with no state", () => {
81
+ const summary = foldBlockStatus(context({ id: "r1" }));
82
+
83
+ expect(summary).toMatchObject({ activeContexts: 1, totalContexts: 1, done: false });
84
+ expect(summary.startedAt).toBeUndefined();
85
+ });
86
+
87
+ it("carries the run id from the root context's baggage", () => {
88
+ const summary = foldBlockStatus(context({ id: "r1", data: { "run-id": "run-7" } }));
89
+
90
+ expect(summary.runId).toBe("run-7");
91
+ });
92
+ });
93
+
94
+ // ── Which state holds a node ─────────────────────────────────────────────────
95
+
96
+ describe("the state holding a node", () => {
97
+ it("lets a wait outrank the render's Running heartbeat, however old the wait is", () => {
98
+ // The workflow controller records Running again on every evaluation.
99
+ const summary = foldBlockStatus(
100
+ context({
101
+ id: "r1",
102
+ transitions: [
103
+ { namespace: "render", atMs: ms(0), state: "Running", detail: "init" },
104
+ { namespace: "exec", reporter: "cmd1", atMs: ms(100), state: "WaitingForMachine" },
105
+ { namespace: "render", atMs: ms(500), state: "Running", detail: "eval" },
106
+ ],
107
+ }),
108
+ );
109
+
110
+ expect(counts(summary.jobs)).toEqual({ "exec/WaitingForMachine": 1 });
111
+ expect(group(summary.jobs, "exec/WaitingForMachine")?.since).toBe(ms(100));
112
+ });
113
+
114
+ it("lets a command running on a machine outrank the render's heartbeat", () => {
115
+ const summary = foldBlockStatus(
116
+ context({
117
+ id: "r1",
118
+ transitions: [
119
+ { namespace: "exec", reporter: "cmd1", atMs: ms(100), state: "Running" },
120
+ { namespace: "render", atMs: ms(500), state: "Running", detail: "eval" },
121
+ ],
122
+ }),
123
+ );
124
+
125
+ expect(counts(summary.jobs)).toEqual({ "exec/Running": 1 });
126
+ });
127
+
128
+ it("takes the latest transition of a reporter, not the first", () => {
129
+ const summary = foldBlockStatus(
130
+ context({
131
+ id: "r1",
132
+ transitions: [
133
+ { namespace: "exec", reporter: "cmd1", atMs: ms(0), state: "WaitingForScheduler" },
134
+ { namespace: "exec", reporter: "cmd1", atMs: ms(50), state: "Running" },
135
+ ],
136
+ }),
137
+ );
138
+
139
+ expect(counts(summary.jobs)).toEqual({ "exec/Running": 1 });
140
+ });
141
+ });
142
+
143
+ // ── When a node has ended ────────────────────────────────────────────────────
144
+
145
+ describe("a node that has ended", () => {
146
+ it("ends when the render reports its own completion and nothing else is running", () => {
147
+ const summary = foldBlockStatus(
148
+ context({
149
+ id: "r1",
150
+ transitions: [
151
+ { namespace: "render", atMs: ms(0), state: "Running" },
152
+ { namespace: "exec", reporter: "cmd1", atMs: ms(50), state: "Completed" },
153
+ renderCompleted(ms(100)),
154
+ ],
155
+ }),
156
+ );
157
+
158
+ expect(summary).toMatchObject({ activeContexts: 0, totalContexts: 1, done: true });
159
+ });
160
+
161
+ it("stays live while a command it started is still running after its outputs are set", () => {
162
+ // The render's Completed means its own outputs are set; a command's work stays visible.
163
+ const summary = foldBlockStatus(
164
+ context({
165
+ id: "r1",
166
+ transitions: [
167
+ renderCompleted(ms(100)),
168
+ { namespace: "exec", reporter: "cmd1", atMs: ms(50), state: "WaitingForScheduler" },
169
+ ],
170
+ }),
171
+ );
172
+
173
+ expect(summary).toMatchObject({ activeContexts: 1, done: false });
174
+ expect(counts(summary.jobs)).toEqual({ "exec/WaitingForScheduler": 1 });
175
+ });
176
+
177
+ it("stays live while only its commands have finished", () => {
178
+ // A render whose first command completed is about to create the next one.
179
+ const summary = foldBlockStatus(
180
+ context({
181
+ id: "r1",
182
+ transitions: [
183
+ { namespace: "render", atMs: ms(0), state: "Running" },
184
+ { namespace: "exec", reporter: "cmd1", atMs: ms(100), state: "Completed" },
185
+ ],
186
+ }),
187
+ );
188
+
189
+ expect(summary).toMatchObject({ activeContexts: 1, done: false });
190
+ });
191
+
192
+ it("counts a deduplicated render as done", () => {
193
+ // The backend ends a duplicate's context with render/Completed, detail deduplicated, and
194
+ // names the original in a reading. The original is not followed: the reading holds a bare
195
+ // render ID the client cannot read, and the original may live in another user's tree.
196
+ const summary = foldBlockStatus(
197
+ context({
198
+ id: "r1",
199
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
200
+ children: [
201
+ {
202
+ id: "dup",
203
+ transitions: [renderCompleted(ms(10), "deduplicated")],
204
+ kv: { "reading/render/dup/original": "12345" },
205
+ },
206
+ ],
207
+ }),
208
+ );
209
+
210
+ expect(summary).toMatchObject({ totalContexts: 2, activeContexts: 1 });
211
+ });
212
+
213
+ it("does not count the root done while a child is still live", () => {
214
+ const summary = foldBlockStatus(
215
+ context({
216
+ id: "r1",
217
+ transitions: [renderCompleted(ms(100))],
218
+ children: [
219
+ {
220
+ id: "a",
221
+ transitions: [{ namespace: "exec", atMs: ms(50), state: "WaitingForMachine" }],
222
+ },
223
+ ],
224
+ }),
225
+ );
226
+
227
+ // The root's own outputs are set, but work under it still runs: not done.
228
+ expect(summary).toMatchObject({ activeContexts: 1, done: false });
229
+ expect(counts(summary.jobs)).toEqual({ "exec/WaitingForMachine": 1 });
230
+ });
231
+ });
232
+
233
+ // ── Failure ──────────────────────────────────────────────────────────────────
234
+
235
+ describe("a run that ended in a failure", () => {
236
+ it("reports the root render's termination with its reason", () => {
237
+ const summary = foldBlockStatus(
238
+ context({
239
+ id: "r1",
240
+ transitions: [
241
+ { namespace: "render", atMs: ms(0), state: "Running" },
242
+ { namespace: "render", atMs: ms(300), state: "Terminated", reason: "template error" },
243
+ ],
244
+ }),
245
+ );
246
+
247
+ expect(summary).toMatchObject({ done: true, failure: "template error" });
248
+ });
249
+
250
+ it("fails without a reason when the render ended without one", () => {
251
+ const summary = foldBlockStatus(
252
+ context({
253
+ id: "r1",
254
+ transitions: [{ namespace: "render", atMs: ms(100), state: "Terminated" }],
255
+ }),
256
+ );
257
+
258
+ expect(summary).toMatchObject({ done: true, failure: "" });
259
+ });
260
+
261
+ it("fails on a terminated sub-render, and ends once nothing is working", () => {
262
+ const summary = foldBlockStatus(
263
+ context({
264
+ id: "root",
265
+ transitions: [renderCompleted(ms(100))],
266
+ children: [
267
+ {
268
+ id: "process",
269
+ transitions: [
270
+ { namespace: "render", atMs: ms(200), state: "Running" },
271
+ { namespace: "render", atMs: ms(900), state: "Terminated", reason: "late" },
272
+ ],
273
+ },
274
+ {
275
+ id: "prep",
276
+ transitions: [
277
+ { namespace: "render", atMs: ms(300), state: "Terminated", reason: "axesSpec" },
278
+ ],
279
+ },
280
+ // Waits on the failed render's outputs and never reports again.
281
+ {
282
+ id: "export",
283
+ transitions: [{ namespace: "render", atMs: ms(400), state: "WaitingForInputs" }],
284
+ },
285
+ ],
286
+ }),
287
+ );
288
+
289
+ expect(summary).toMatchObject({ done: true, failure: "axesSpec", activeContexts: 1 });
290
+ });
291
+
292
+ it("counts a render that failed on its own as a failed job, not the renders passing it up", () => {
293
+ const failedAt = (atMs: number) => ({
294
+ namespace: "render",
295
+ atMs,
296
+ state: "Terminated",
297
+ reason: "docker is not set",
298
+ });
299
+ const summary = foldBlockStatus(
300
+ context({
301
+ id: "root",
302
+ transitions: [renderCompleted(ms(100))],
303
+ children: [
304
+ {
305
+ id: "process",
306
+ transitions: [failedAt(ms(400))],
307
+ children: [
308
+ { id: "save", transitions: [failedAt(ms(300))] },
309
+ {
310
+ id: "cmd",
311
+ transitions: [
312
+ { namespace: "exec", reporter: "c", atMs: ms(200), state: "Completed" },
313
+ renderCompleted(ms(250)),
314
+ ],
315
+ },
316
+ ],
317
+ },
318
+ ],
319
+ }),
320
+ );
321
+
322
+ expect(summary.jobs).toMatchObject({ total: 2, done: 1, failed: 1 });
323
+ });
324
+
325
+ it("is not done after a failure while a job still runs", () => {
326
+ const summary = foldBlockStatus(
327
+ context({
328
+ id: "root",
329
+ transitions: [renderCompleted(ms(100))],
330
+ children: [
331
+ { id: "bad", transitions: [{ namespace: "render", atMs: ms(200), state: "Terminated" }] },
332
+ {
333
+ id: "job",
334
+ transitions: [{ namespace: "exec", reporter: "cmd", atMs: ms(300), state: "Running" }],
335
+ },
336
+ ],
337
+ }),
338
+ );
339
+
340
+ expect(summary).toMatchObject({ done: false, failure: "" });
341
+ expect(counts(summary.jobs)).toEqual({ "exec/Running": 1 });
342
+ });
343
+
344
+ it("does not report a failure for a command the run recovered from", () => {
345
+ const summary = foldBlockStatus(
346
+ context({
347
+ id: "r1",
348
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
349
+ children: [
350
+ {
351
+ id: "r2",
352
+ transitions: [
353
+ {
354
+ namespace: "exec",
355
+ reporter: "cmd1",
356
+ atMs: ms(100),
357
+ state: "Terminated",
358
+ reason: "OOM killed",
359
+ },
360
+ { namespace: "exec", reporter: "cmd2", atMs: ms(200), state: "WaitingForMachine" },
361
+ ],
362
+ },
363
+ ],
364
+ }),
365
+ );
366
+
367
+ expect(summary).toMatchObject({ done: false, failure: undefined });
368
+ expect(counts(summary.jobs)).toEqual({ "exec/WaitingForMachine": 1 });
369
+ });
370
+
371
+ it("does not report a failure when the render completed despite a terminated command", () => {
372
+ const summary = foldBlockStatus(
373
+ context({
374
+ id: "r1",
375
+ transitions: [
376
+ {
377
+ namespace: "exec",
378
+ reporter: "cmd1",
379
+ atMs: ms(100),
380
+ state: "Terminated",
381
+ reason: "OOM killed",
382
+ },
383
+ renderCompleted(ms(500)),
384
+ ],
385
+ }),
386
+ );
387
+
388
+ expect(summary).toMatchObject({ done: true, activeContexts: 0, failure: undefined });
389
+ });
390
+ });
391
+
392
+ // ── Aggregation over a tree ──────────────────────────────────────────────────
393
+
394
+ describe("aggregation over a tree", () => {
395
+ /** `count` sibling contexts, each held by `state` from `from` on. */
396
+ function fanOut(count: number, state: BlockStatusState, from: number): ContextFixture[] {
397
+ return Array.from({ length: count }, (_, i) => ({
398
+ id: `${state}-${i}`,
399
+ transitions: [{ namespace: "exec", reporter: `cmd-${state}-${i}`, atMs: from + i, state }],
400
+ }));
401
+ }
402
+
403
+ it("counts every node and job of a fanned-out run", () => {
404
+ const summary = foldBlockStatus(
405
+ context({
406
+ id: "r1",
407
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
408
+ children: [
409
+ ...fanOut(382, "WaitingForMachine", ms(1000)),
410
+ ...fanOut(17, "PopulatingWorkdir", ms(2000)),
411
+ ],
412
+ }),
413
+ );
414
+
415
+ expect(summary).toMatchObject({ totalContexts: 400, activeContexts: 400 });
416
+ expect(summary.jobs.total).toBe(399);
417
+ expect(counts(summary.jobs)).toEqual({
418
+ "exec/WaitingForMachine": 382,
419
+ "exec/PopulatingWorkdir": 17,
420
+ });
421
+ });
422
+
423
+ it("walks nested children and spans the whole tree in its timestamps", () => {
424
+ const summary = foldBlockStatus(
425
+ context({
426
+ id: "r1",
427
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
428
+ children: [
429
+ {
430
+ id: "r2",
431
+ transitions: [{ namespace: "render", atMs: ms(100), state: "Running" }],
432
+ children: [
433
+ {
434
+ id: "r3",
435
+ transitions: [{ namespace: "exec", atMs: ms(900), state: "WaitingForMachine" }],
436
+ },
437
+ ],
438
+ },
439
+ ],
440
+ }),
441
+ );
442
+
443
+ expect(summary).toMatchObject({ totalContexts: 3, startedAt: ms(0), lastChangeAt: ms(900) });
444
+ });
445
+ });
446
+
447
+ // ── Input the fold must survive ──────────────────────────────────────────────
448
+
449
+ describe("malformed input", () => {
450
+ it("skips a transition whose value is not the JSON the reporters write", () => {
451
+ const summary = foldBlockStatus(
452
+ context({
453
+ id: "r1",
454
+ transitions: [{ namespace: "workdir", atMs: ms(0), state: "PopulatingWorkdir" }],
455
+ kv: { [`transition/workdir/r1/${String(BigInt(ms(100)) * 1_000_000n)}`]: "not json" },
456
+ }),
457
+ );
458
+
459
+ expect(counts(summary.jobs)).toEqual({ "workdir/PopulatingWorkdir": 1 });
460
+ });
461
+
462
+ it("skips JSON that is not an object, in transitions and readings", () => {
463
+ const key = (offset: number) =>
464
+ `transition/workdir/r1/${String(BigInt(ms(offset)) * 1_000_000n)}`;
465
+ const summary = foldBlockStatus(
466
+ context({
467
+ id: "r1",
468
+ transitions: [{ namespace: "workdir", atMs: ms(0), state: "PopulatingWorkdir" }],
469
+ kv: {
470
+ [key(100)]: "null",
471
+ [key(200)]: "42",
472
+ [key(300)]: '["Completed"]',
473
+ "reading/workdir/wd/progress": "null",
474
+ },
475
+ }),
476
+ );
477
+
478
+ expect(counts(summary.jobs)).toEqual({ "workdir/PopulatingWorkdir": 1 });
479
+ });
480
+
481
+ it("passes on a state this build does not know, as the reporter wrote it", () => {
482
+ const summary = foldBlockStatus(
483
+ context({
484
+ id: "r1",
485
+ transitions: [
486
+ { namespace: "workdir", atMs: ms(0), state: "PopulatingWorkdir" },
487
+ {
488
+ namespace: "exec",
489
+ reporter: "cmd1",
490
+ atMs: ms(90_000),
491
+ state: "WaitingForLicenceServer",
492
+ },
493
+ ],
494
+ }),
495
+ );
496
+
497
+ expect(counts(summary.jobs)).toEqual({ "exec/WaitingForLicenceServer": 1 });
498
+ expect(summary.lastChangeAt).toBe(ms(90_000));
499
+ });
500
+
501
+ it("ignores keys that are not transitions, and stamps with leading zeros", () => {
502
+ const summary = foldBlockStatus(
503
+ context({
504
+ id: "r1",
505
+ transitions: [{ namespace: "workdir", atMs: ms(0), state: "PopulatingWorkdir" }],
506
+ kv: {
507
+ "transition/render/r1": JSON.stringify({ state: "Completed" }),
508
+ "transition/render/r1/007": JSON.stringify({ state: "Completed" }),
509
+ "reading/exec/cmd1/progress": "40",
510
+ },
511
+ }),
512
+ );
513
+
514
+ expect(summary).toMatchObject({ done: false, activeContexts: 1 });
515
+ });
516
+
517
+ it("visits a context reachable twice only once", () => {
518
+ const shared = context({
519
+ id: "shared",
520
+ transitions: [{ namespace: "exec", atMs: ms(100), state: "WaitingForMachine" }],
521
+ });
522
+ const root: ActionContextNode = {
523
+ id: "r1",
524
+ getData: () => undefined,
525
+ listKeyValuesAsString: () => [],
526
+ listFields: () => ["child/shared", "child/shared-again"],
527
+ traverse: () => shared,
528
+ };
529
+
530
+ expect(foldBlockStatus(root)).toMatchObject({ totalContexts: 2, activeContexts: 2 });
531
+ });
532
+ });
533
+
534
+ describe("a run the user stopped", () => {
535
+ const stopped = () =>
536
+ context({
537
+ id: "root",
538
+ transitions: [
539
+ { namespace: "render", atMs: ms(0), state: "Running" },
540
+ {
541
+ namespace: "core",
542
+ reporter: "root",
543
+ atMs: ms(90_000),
544
+ state: "Terminated",
545
+ reason: "cancelled",
546
+ },
547
+ ],
548
+ children: [
549
+ {
550
+ id: "c1",
551
+ transitions: [{ namespace: "exec", reporter: "cmd", atMs: ms(10_000), state: "Running" }],
552
+ },
553
+ ],
554
+ });
555
+
556
+ it("is done and cancelled, not failed, and its time ends at the stop", () => {
557
+ const summary = foldBlockStatus(stopped());
558
+
559
+ expect(summary).toMatchObject({ done: true, cancelled: true, lastChangeAt: ms(90_000) });
560
+ expect(summary.failure).toBeUndefined();
561
+ });
562
+
563
+ it("keeps the counters, to say where each node was when it stopped", () => {
564
+ expect(counts(foldBlockStatus(stopped()).jobs)).toEqual({ "exec/Running": 1 });
565
+ });
566
+
567
+ it("is not cancelled while only the backend has reported", () => {
568
+ const running = context({
569
+ id: "root",
570
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
571
+ });
572
+ expect(foldBlockStatus(running).cancelled).toBeUndefined();
573
+ });
574
+ });
575
+
576
+ describe("the run's jobs", () => {
577
+ const job = (id: string, ...transitions: TransitionFixture[]): ContextFixture => ({
578
+ id,
579
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }, ...transitions],
580
+ });
581
+ const exec = (atMs: number, state: string): TransitionFixture => ({
582
+ namespace: "exec",
583
+ reporter: "cmd",
584
+ atMs,
585
+ state,
586
+ });
587
+ const workdir = (atMs: number, state: string): TransitionFixture => ({
588
+ namespace: "workdir",
589
+ reporter: "wd",
590
+ atMs,
591
+ state,
592
+ });
593
+
594
+ const summary = foldBlockStatus(
595
+ context({
596
+ id: "root",
597
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
598
+ children: [
599
+ // A template render that only prepares jobs is not one.
600
+ { id: "prepare", transitions: [{ namespace: "render", atMs: ms(1), state: "Running" }] },
601
+ {
602
+ ...job("copy-1", workdir(ms(5), "PopulatingWorkdir")),
603
+ kv: { "reading/workdir/wd/progress": '{"done":3,"total":4}' },
604
+ },
605
+ {
606
+ ...job("copy-2", workdir(ms(6), "PopulatingWorkdir")),
607
+ kv: { "reading/workdir/wd/progress": '{"done":1,"total":2}' },
608
+ },
609
+ job("queued-late", exec(ms(30), "WaitingSubmissionGate")),
610
+ job("queued-early", exec(ms(20), "WaitingSubmissionGate")),
611
+ job("pending", exec(ms(40), "WaitingForScheduler")),
612
+ job("running", exec(ms(50), "Running")),
613
+ job("saving", workdir(ms(60), "CollectingOutputs")),
614
+ job("done", exec(ms(70), "Completed"), renderCompleted(ms(71))),
615
+ job("failed", exec(ms(80), "Terminated"), {
616
+ namespace: "render",
617
+ atMs: ms(81),
618
+ state: "Terminated",
619
+ reason: "exit 137",
620
+ }),
621
+ ],
622
+ }),
623
+ );
624
+
625
+ it("counts each job once, by where it is now", () => {
626
+ expect(summary.jobs).toMatchObject({ total: 9, done: 1, failed: 1 });
627
+ expect(counts(summary.jobs)).toEqual({
628
+ "workdir/PopulatingWorkdir": 2,
629
+ "exec/WaitingSubmissionGate": 2,
630
+ "exec/WaitingForScheduler": 1,
631
+ "exec/Running": 1,
632
+ "workdir/CollectingOutputs": 1,
633
+ });
634
+ });
635
+
636
+ it("sums the files copied by the jobs in each state", () => {
637
+ expect(group(summary.jobs, "workdir/PopulatingWorkdir")?.files).toEqual({ done: 4, total: 6 });
638
+ });
639
+
640
+ it("dates each state from the earliest job still in it", () => {
641
+ expect(group(summary.jobs, "exec/WaitingSubmissionGate")?.since).toBe(ms(20));
642
+ expect(group(summary.jobs, "exec/WaitingForScheduler")?.since).toBe(ms(40));
643
+ });
644
+
645
+ it("lists the largest group first", () => {
646
+ expect(summary.jobs.byState.map((g) => g.count)).toEqual([2, 2, 1, 1, 1]);
647
+ });
648
+
649
+ it("has no jobs before any command or work folder reports", () => {
650
+ const empty = foldBlockStatus(
651
+ context({
652
+ id: "root",
653
+ transitions: [{ namespace: "render", atMs: ms(0), state: "Running" }],
654
+ }),
655
+ );
656
+ expect(empty.jobs).toEqual({ total: 0, done: 0, failed: 0, byState: [] });
657
+ });
658
+ });
659
+
660
+ describe("failureHeadline", () => {
661
+ it("keeps the innermost error of a nested chain, without stack frames", () => {
662
+ const reason = [
663
+ 'cannot eval code: cannot eval template: tengo template error: input resource has errors: "[I] "NG:0x1/data": has input errors: [O] "NG:0x2/outputs/binPath": has input errors: Command ID: "NG:0x3"',
664
+ 'Working directory: "workdirs/0x4"',
665
+ 'Command name: "python"',
666
+ "Failed to start.",
667
+ 'Command: "python" "-c" "import platform"',
668
+ "Error: docker is not set",
669
+ "@platforma-sdk/workflow-tengo:exec.exec-impl:97:1",
670
+ ].join("\n");
671
+
672
+ expect(failureHeadline(reason)).toBe("python failed: docker is not set");
673
+ });
674
+
675
+ it("reads a command failure written on one line", () => {
676
+ const reason =
677
+ 'cannot eval code: input resource has errors: "[O] "NG:0x1/outputs/binDir": has input errors: Command ID: "NG:0x2" Working directory: "workdirs/0x3" Command name: "python" Failed to start. Command: "python" "-c" "x" Error: docker is not set\n@platforma-sdk/workflow-tengo:ll:24:2';
678
+
679
+ expect(failureHeadline(reason)).toBe("python failed: docker is not set");
680
+ });
681
+
682
+ it("drops the template wrappers of a template's own error", () => {
683
+ const reason =
684
+ 'cannot eval code: cannot eval template: cannot get element from strictMap: key "axesSpec" not found\n@platforma-open/milaboratories.mixcr-clonotyping-2.workflow:process:80:32';
685
+
686
+ expect(failureHeadline(reason)).toBe(
687
+ 'cannot get element from strictMap: key "axesSpec" not found',
688
+ );
689
+ });
690
+
691
+ it("caps a long message", () => {
692
+ expect(failureHeadline("x".repeat(500))).toHaveLength(200);
693
+ });
694
+ });