Repository navigation
Expand file tree
/
Copy pathstranded-request-inspection.test.ts
More file actions
875 lines (797 loc) · 41.2 KB
/
Copy pathstranded-request-inspection.test.ts
File metadata and controls
875 lines (797 loc) · 41.2 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Stranded terminal requests are found (#4469).
*
* #4420's failure shape: a request row flipped to `approved` (or `rejected`)
* while its `flow_run_id` points at a run that no longer exists — the decision
* landed, the flow never moved. #4460 stopped NEW ones being produced; the rows
* already stuck had no mechanism to find or release them.
*
* `releaseDeadRunRequests` cannot see them, and the reason is the interesting
* part: it scans `status: 'pending'`, and the very step that zombified the
* request is the one that took it OUT of `pending`. Breaking it removed it from
* the only sweeper's field of view. Its liveness oracle could not have answered
* anyway — `getRun` reads the execution LOG, which returns `null` for a
* perfectly alive suspended run after a restart.
*
* So the inspection uses BOTH oracles and reports only rows that fail both,
* skipping (never condemning) anything the stores could not answer for.
*
* ── #13909: the second oracle was too narrow ────────────────────────────────
*
* `if (terminal) continue` read the mere EXISTENCE of a history row as health.
* The engine consumes a suspension before running the downstream nodes
* (`forgetSuspendedRun(run, 'resumed')` precedes `traverseNext`), so a node that
* merely threw threw with the pause already gone and the catch arm wrote a
* terminal `failed` row — the decision durable, the continuation stopped
* half-way, nothing able to resume it. The row this inspection read as "it
* finished, it is not dangling" is written BY the failure that stranded it, so
* the one shape an operator most needs was reported as `0`.
*
* The widening is deliberately narrow, and the second half of this file pins
* that: `completed`, `cancelled`, `paused` and any status this code does not
* recognise are each STILL skipped, one test per reason. A widening that
* reported everything would bury the finding it exists to surface.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { ObjectQL } from '@objectstack/objectql';
import { SqlDriver } from '@objectstack/driver-sql';
import { ApprovalService } from './approval-service.js';
import { ApprovalsServicePlugin } from './approvals-plugin.js';
import { installDefaultOrganization } from './default-organization.testkit.js';
interface FakeRow { [k: string]: any }
function makeFakeEngine() {
const tables: Record<string, FakeRow[]> = {};
const ensure = (n: string) => (tables[n] ??= []);
function matches(row: FakeRow, filter: any): boolean {
if (!filter || typeof filter !== 'object') return true;
for (const [k, v] of Object.entries(filter)) {
if (k.startsWith('$')) throw new Error(`fake engine: unsupported filter operator ${k}`);
const rv = row[k];
if (v != null && typeof v === 'object' && '$in' in (v as any)) {
if (!(v as any).$in.includes(rv)) return false;
continue;
}
if (rv !== v) return false;
}
return true;
}
return {
_tables: tables,
async find(object: string, options?: any) {
const rows = ensure(object).filter(r => matches(r, options?.filter ?? options?.where));
return rows.slice(0, options?.limit ?? 1000);
},
async insert(object: string, data: any) { ensure(object).push({ ...data }); return { ...data }; },
async update(object: string, idOrData: any, _opts?: any) {
const data = typeof idOrData === 'object' ? idOrData : _opts;
const id = typeof idOrData === 'object' ? idOrData.id : idOrData;
const table = ensure(object);
const i = table.findIndex(r => r.id === id);
if (i >= 0) table[i] = { ...table[i], ...data };
return table[i];
},
async delete() { return {}; },
registerHook() {}, unregisterHooksByPackage() { return 0; }, async fire() {},
};
}
/** A terminal request row as the zombie leaves it: decision recorded, run gone. */
function requestRow(over: Record<string, any> = {}): FakeRow {
return {
id: 'areq_1',
process_name: 'flow:deal_approval',
object_name: 'opportunity',
record_id: 'opp1',
status: 'approved',
flow_run_id: 'run_1',
flow_node_id: 'co_sign',
organization_id: 't1',
completed_at: '2026-01-15T10:00:05.000Z',
node_config_json: JSON.stringify({
approvers: [{ type: 'user', value: 'u9' }],
behavior: 'first_response',
approvalStatusField: 'approval_status',
}),
...over,
};
}
/** What the #15358 third oracle answers, per run — see `ApprovalResumeSurface`. */
type Verdict =
| { repairable: true }
| { repairable: false; reason: 'RUN_SUSPENDED' | 'SNAPSHOT_DROPPED' | 'NO_CONSUMED_SUSPENSION' };
/**
* An automation surface with both oracles, each independently steerable — and,
* ONLY when `repairability` / `repairabilityThrows` is given, the #15358 third
* oracle (`inspectConsumedSuspension`). Its absence by default is deliberate:
* every test above the #15358 block drives a surface that cannot be asked,
* which is exactly the population the undifferentiated `'failed'` is kept for.
*/
function automation(opts: {
suspended?: Record<string, boolean>;
suspendedThrows?: boolean;
history?: Record<string, { status?: string }>;
historyThrows?: boolean;
repairability?: Record<string, Verdict>;
repairabilityThrows?: boolean;
/** Runs whose third-oracle read THROWS — a store outage on those rows alone. */
repairabilityThrowsFor?: string[];
/**
* Runs whose host RESOLVES `undefined` — a contract-violating implementation
* of its own declared surface (commit 8c7cca1ce, item 3). ⛔ Deliberately outside
* `Verdict`: pinning what happens when a host lies is the whole point, and
* the cast that makes it expressible is confined to this double.
*/
repairabilityMalformedFor?: string[];
} = {}) {
const inspectCalls: string[] = [];
const surface: any = {
inspectCalls,
async resume() { return { success: true }; },
async hasSuspendedRun(runId: string) {
if (opts.suspendedThrows) throw new Error('suspended-run store unreadable');
return opts.suspended?.[runId] ?? false;
},
async getRun(runId: string) {
if (opts.historyThrows) throw new Error('run history unreadable');
return opts.history?.[runId] ?? null;
},
};
if (
opts.repairability !== undefined || opts.repairabilityThrows
|| opts.repairabilityThrowsFor || opts.repairabilityMalformedFor
) {
surface.inspectConsumedSuspension = async (runId: string): Promise<Verdict> => {
inspectCalls.push(runId);
if (opts.repairabilityThrows || opts.repairabilityThrowsFor?.includes(runId)) {
throw new Error('run history unreadable for the consumed suspension');
}
if (opts.repairabilityMalformedFor?.includes(runId)) return undefined as unknown as Verdict;
const v = opts.repairability?.[runId];
if (!v) throw new Error(`test surface: no verdict scripted for ${runId}`);
return v;
};
}
return surface;
}
describe('stranded terminal request inspection (#4469)', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: ApprovalService;
beforeEach(() => {
engine = makeFakeEngine();
svc = new ApprovalService({ engine: engine as any });
});
it('the blind spot, stated: the existing pending-only sweep cannot see a terminal zombie', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation());
// `releaseDeadRunRequests` scans `status: 'pending'`; the zombie is
// `approved`, so its scan set is empty.
expect(await svc.releaseDeadRunRequests()).toEqual({ scanned: 0, released: 0 });
});
it('finds a terminal request whose run is neither suspended nor ever completed', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
svc.attachAutomation(automation());
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(1);
expect(out.undetermined).toBe(0);
expect(out.stranded).toHaveLength(1);
expect(out.stranded[0]).toMatchObject({
requestId: 'areq_1',
status: 'approved',
runId: 'run_1',
// #13909 — WHICH shape: no history row at all, the original #4469 zombie.
runState: 'missing',
nodeId: 'co_sign',
flowName: 'deal_approval',
objectName: 'opportunity',
recordId: 'opp1',
});
});
it('reports the stale mirrored status — what an operator actually sees on the record', async () => {
// The decision says `approved`; the business record still reads `pending`
// because the flow never resumed to move it. That disagreement is the
// human-facing symptom, so the report carries it.
engine._tables['sys_approval_request'] = [requestRow()];
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
svc.attachAutomation(automation());
const [row] = (await svc.inspectStrandedRequests()).stranded;
expect(row.mirrorField).toBe('approval_status');
expect(row.mirroredStatus).toBe('pending');
});
it('does NOT report a request whose run is still suspended — that approval is healthy', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ suspended: { run_1: true } }));
expect((await svc.inspectStrandedRequests()).stranded).toEqual([]);
});
it('does NOT report a request whose run COMPLETED — the decision advanced the flow', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ history: { run_1: { status: 'completed' } } }));
expect((await svc.inspectStrandedRequests()).stranded).toEqual([]);
});
it('SKIPS a row whose suspension store threw — an outage is unknown, not dead', async () => {
// The whole point of `hasSuspendedRun` rejecting rather than answering
// `false` (#4460): a storage blip must never be published as a lost run.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ suspendedThrows: true }));
const out = await svc.inspectStrandedRequests();
expect(out.stranded).toEqual([]);
// …and it is COUNTED, so "0 stranded" can never be read as "all clear"
// when nothing could actually be checked.
expect(out.undetermined).toBe(1);
});
it('SKIPS a row whose run history threw — same reasoning, second oracle', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ historyThrows: true }));
const out = await svc.inspectStrandedRequests();
expect(out.stranded).toEqual([]);
expect(out.undetermined).toBe(1);
});
it('ignores a request with no `flow_run_id` — no run was ever supposed to move', async () => {
engine._tables['sys_approval_request'] = [requestRow({ flow_run_id: null })];
svc.attachAutomation(automation());
expect((await svc.inspectStrandedRequests()).stranded).toEqual([]);
});
it('ignores a `recalled` request — a recall abandons its run deliberately', async () => {
// `recall` explicitly tolerates a run it cannot resume; reporting those
// would bury the real findings under expected ones.
engine._tables['sys_approval_request'] = [requestRow({ status: 'recalled' })];
svc.attachAutomation(automation());
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(0);
expect(out.stranded).toEqual([]);
});
it('covers `rejected` and `returned` too — both reach terminal only by resuming the run', async () => {
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_r', status: 'rejected', flow_run_id: 'run_r' }),
requestRow({ id: 'areq_v', status: 'returned', flow_run_id: 'run_v' }),
];
svc.attachAutomation(automation());
const ids = (await svc.inspectStrandedRequests()).stranded.map(s => s.requestId);
expect(ids).toEqual(['areq_r', 'areq_v']);
});
it('NEVER rewrites a stranded row — the decision really happened', async () => {
// Auto-rolling back would make the audit trail disagree with the facts.
// The remedy (re-run downstream actions vs re-open the approval) is an
// operator judgement call, so the sweep only makes the rows visible.
engine._tables['sys_approval_request'] = [requestRow()];
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
const before = JSON.stringify(engine._tables);
svc.attachAutomation(automation());
await svc.inspectStrandedRequests();
expect(JSON.stringify(engine._tables)).toBe(before);
expect(engine._tables['sys_approval_action'] ?? []).toHaveLength(0);
});
it('reports nothing when the engine offers no `hasSuspendedRun` — no oracle, no verdict', async () => {
// Without it there is no way to tell a live cross-restart pause from a dead
// run, and `getRun` alone would name every healthy paused approval stranded.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation({ async resume() { return {}; }, async getRun() { return null; } } as any);
expect(await svc.inspectStrandedRequests()).toEqual({ scanned: 0, stranded: [], undetermined: 0 });
});
it('reports nothing with no automation attached at all', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
expect(await svc.inspectStrandedRequests()).toEqual({ scanned: 0, stranded: [], undetermined: 0 });
});
});
/**
* The widening (#13909) — and, in equal measure, everything it must NOT widen.
*
* The positive is one test; the negatives are five, because "it now reports the
* bad one" says nothing about whether it started reporting the good ones too.
*/
describe('stranded inspection sees a run that FAILED mid-resume (#13909)', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: ApprovalService;
beforeEach(() => {
engine = makeFakeEngine();
svc = new ApprovalService({ engine: engine as any });
});
it('reports a terminal request whose run recorded a terminal `failed` row', async () => {
// The shape the card owns: the resume consumed the pause, a downstream node
// threw, the catch arm recorded `failed`. `hasSuspendedRun` is false because
// the suspension really is gone — that is the defect, not a healthy state.
engine._tables['sys_approval_request'] = [requestRow({ status: 'rejected' })];
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
svc.attachAutomation(automation({ history: { run_1: { status: 'failed' } } }));
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(1);
expect(out.undetermined).toBe(0);
expect(out.stranded).toHaveLength(1);
expect(out.stranded[0]).toMatchObject({
requestId: 'areq_1',
status: 'rejected',
runId: 'run_1',
runState: 'failed',
nodeId: 'co_sign',
objectName: 'opportunity',
recordId: 'opp1',
});
// The operator-facing symptom is carried for this shape too: the record's
// mirror still reads what it read before the decision.
expect(out.stranded[0].mirroredStatus).toBe('pending');
});
it('the OLD oracle would have skipped it — the terminal row is written BY the failure', async () => {
// Pins the mechanism rather than the outcome: `getRun` DOES answer for this
// run, which is exactly why `if (terminal) continue` reported all clear.
const auto = automation({ history: { run_1: { status: 'failed' } } });
expect(await auto.getRun('run_1')).not.toBeNull();
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(auto);
expect((await svc.inspectStrandedRequests()).stranded).toHaveLength(1);
});
// ── The negatives, one reason per test ─────────────────────────────────────
it('does NOT report a run that was CANCELLED — stopping it was the intent', async () => {
// `cancelRun` (ADR-0044) is an operator deliberately ending the run, the
// run-side twin of a `recalled` request. Reporting these would bury the
// real findings under expected ones.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ history: { run_1: { status: 'cancelled' } } }));
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(1);
expect(out.stranded).toEqual([]);
expect(out.undetermined).toBe(0);
});
it('does NOT report a run whose last history row says `paused` — that is ambiguous, not stranded', async () => {
// A resume in flight has already consumed the suspension and not yet
// written its terminal row: `hasSuspendedRun` false + history `paused` reads
// identically to a process that died in that window. Condemning it would
// name every concurrently resuming approval.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ history: { run_1: { status: 'paused' } } }));
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(1);
expect(out.stranded).toEqual([]);
expect(out.undetermined).toBe(0);
});
it('does NOT report a status it does not recognise — a new run state is not evidence of a strand', async () => {
// The spec's `ExecutionStatus` vocabulary is wider than the four statuses
// the engine writes (`timed_out`, `retrying`, …). The default arm stays
// silent so a future status cannot become a silent false positive.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ history: { run_1: { status: 'timed_out' } } }));
expect((await svc.inspectStrandedRequests()).stranded).toEqual([]);
});
it('does NOT report a FAILED run that is still suspended — the first oracle still gates', async () => {
// A run re-parked at a later node after an earlier failed leg is alive and
// resumable; the suspension oracle short-circuits before the run state is
// ever classified.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({
suspended: { run_1: true }, history: { run_1: { status: 'failed' } },
}));
expect((await svc.inspectStrandedRequests()).stranded).toEqual([]);
});
it('still SKIPS a failed-run row whose suspension store threw — an outage stays unknown', async () => {
// The widening must not turn an unreadable store into a verdict: the
// undetermined counter, not the stranded list, is where this belongs.
engine._tables['sys_approval_request'] = [requestRow()];
svc.attachAutomation(automation({ suspendedThrows: true, history: { run_1: { status: 'failed' } } }));
const out = await svc.inspectStrandedRequests();
expect(out.stranded).toEqual([]);
expect(out.undetermined).toBe(1);
});
it('separates the two shapes in one mixed population — and reports only those two', async () => {
// The aggregate pin: four terminal requests, four different run states, and
// exactly the two unrecoverable ones come back, each labelled.
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_missing', flow_run_id: 'run_missing' }),
requestRow({ id: 'areq_failed', flow_run_id: 'run_failed' }),
requestRow({ id: 'areq_done', flow_run_id: 'run_done' }),
requestRow({ id: 'areq_cancelled', flow_run_id: 'run_cancelled' }),
];
svc.attachAutomation(automation({
history: {
run_failed: { status: 'failed' },
run_done: { status: 'completed' },
run_cancelled: { status: 'cancelled' },
// `run_missing` deliberately absent — `getRun` answers null for it.
},
}));
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(4);
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([
['areq_missing', 'missing'],
['areq_failed', 'failed'],
]);
});
it('NEVER rewrites a failed-run row either — the decision really happened', async () => {
engine._tables['sys_approval_request'] = [requestRow()];
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
svc.attachAutomation(automation({ history: { run_1: { status: 'failed' } } }));
const before = JSON.stringify(engine._tables);
await svc.inspectStrandedRequests();
expect(JSON.stringify(engine._tables)).toBe(before);
expect(engine._tables['sys_approval_action'] ?? []).toHaveLength(0);
});
});
// ── #15358: the third oracle tells the `failed` rows apart ─────────────────
//
// `status === 'failed'` over-reports in one direction: a cascade-failed run
// (an ancestor `failAncestors` failed while parked at its `subflow` node —
// `failSuspendedRun` consumed its pause and journalled nothing, so nothing
// re-arms it; #15222) has the same terminal row as the #13909 strand that
// `restoreConsumedSuspension` repairs. The engine publishes the difference as
// a dedicated read-only member (ruling B′, 2026-09-07), never on the object
// `getRun` answers. This block pins the plugin's side of that contract on a
// scripted surface; `stranded-run-repairability.test.ts` drives the real
// engine through both shapes.
describe('#15358 — the third oracle splits `failed` three ways, and its ABSENCE is fail-closed', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: ApprovalService;
beforeEach(() => {
engine = makeFakeEngine();
svc = new ApprovalService({ engine: engine as any });
engine._tables['sys_approval_request'] = [requestRow()];
});
const failedRun = { history: { run_1: { status: 'failed' as const } } };
it('⭐ a surface WITHOUT the member reports the row `failed` — never `unrepairable`, never skipped', async () => {
// Absence of the discriminator is not evidence of anything. On a real
// engine it is absent from `getRun` for BOTH the repairable strand and the
// cascade-failed ancestor, so reading absence as "not a strand" would call
// the repairable row dead — #15555's false negative, one surface over.
const auto = automation(failedRun);
expect(typeof auto.inspectConsumedSuspension).toBe('undefined');
svc.attachAutomation(auto);
const out = await svc.inspectStrandedRequests();
expect(out.undetermined).toBe(0);
expect(out.stranded.map(s => s.runState)).toEqual(['failed']);
});
it('`repairable: true` → `repairable`', async () => {
svc.attachAutomation(automation({ ...failedRun, repairability: { run_1: { repairable: true } } }));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => s.runState)).toEqual(['repairable']);
expect(out.undetermined).toBe(0);
});
it('`SNAPSHOT_DROPPED` → `snapshot_dropped` — its own class, folded into neither neighbour', async () => {
svc.attachAutomation(automation({
...failedRun, repairability: { run_1: { repairable: false, reason: 'SNAPSHOT_DROPPED' } },
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => s.runState)).toEqual(['snapshot_dropped']);
});
it('`NO_CONSUMED_SUSPENSION` → `unrepairable` — the cascade-failed / never-paused shape', async () => {
svc.attachAutomation(automation({
...failedRun, repairability: { run_1: { repairable: false, reason: 'NO_CONSUMED_SUSPENSION' } },
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => s.runState)).toEqual(['unrepairable']);
});
it('`RUN_SUSPENDED` → not stranded: re-armed between the two reads, the run is alive', async () => {
svc.attachAutomation(automation({
...failedRun, repairability: { run_1: { repairable: false, reason: 'RUN_SUSPENDED' } },
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded).toEqual([]);
// Skipped as alive — NOT counted as unknown.
expect(out.undetermined).toBe(0);
});
it('⭐ [#16709 item 2] a THROWN read keeps the row REPORTED as `failed` — it never leaves the list', async () => {
// ⚠️ This assertion USED TO READ `expect(out.stranded).toEqual([])`: a
// thrown third read was counted `undetermined` and the row dropped, as for
// the other two oracles. Ruled the other way (PM seat, 2026-09-08).
//
// The two earlier oracles and this one are not asked the same question. A
// thrown `hasSuspendedRun` or `getRun` leaves it unknown WHETHER the row is
// stranded at all, and a storage outage must not be published as a lost
// run. By the time this oracle is asked, both have already answered: no
// live pause, terminal `failed`. It is asked only WHICH of the three
// shapes — so a read that could not be made is the textbook "could not
// differentiate", which is exactly what `'failed'` is kept for (#15358
// ruling, item 1). Dropping the row would let "nothing stranded" read TRUE
// while a row is in fact stuck, with a log line as its only trace; for a
// REPORT, fail-closed means showing the row.
svc.attachAutomation(automation({ ...failedRun, repairabilityThrows: true }));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([['areq_1', 'failed']]);
// The counter is KEPT, as telemetry — it and `stranded` now overlap by
// design, and neither alone sizes the scan's blind spot.
expect(out.undetermined).toBe(1);
});
it('an answer this build does not know stays `failed` — reported, undifferentiated', async () => {
// An engine ahead of this plugin. Fail-closed exactly as an absent member:
// a word this code cannot read condemns nothing.
svc.attachAutomation(automation({
...failedRun,
repairability: { run_1: { repairable: false, reason: 'SOMETHING_NEWER' as any } },
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => s.runState)).toEqual(['failed']);
expect(out.undetermined).toBe(0);
});
it('is asked for `failed` rows ONLY — with the failed row as the positive control', async () => {
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_missing', flow_run_id: 'run_missing' }),
requestRow({ id: 'areq_failed', flow_run_id: 'run_failed' }),
requestRow({ id: 'areq_done', flow_run_id: 'run_done' }),
requestRow({ id: 'areq_cancelled', flow_run_id: 'run_cancelled' }),
requestRow({ id: 'areq_parked', flow_run_id: 'run_parked' }),
];
const auto = automation({
suspended: { run_parked: true },
history: {
run_failed: { status: 'failed' },
run_done: { status: 'completed' },
run_cancelled: { status: 'cancelled' },
run_parked: { status: 'failed' },
},
repairability: { run_failed: { repairable: false, reason: 'NO_CONSUMED_SUSPENSION' } },
});
svc.attachAutomation(auto);
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([
['areq_missing', 'missing'],
['areq_failed', 'unrepairable'],
]);
// Exactly one read, for exactly the failed-and-not-suspended row: a
// `missing` run has nothing to ask about, a finished or cancelled run is
// not reported at all, and a parked run never reaches the second oracle.
expect(auto.inspectCalls).toEqual(['run_failed']);
});
it('one mixed population, every label distinct — nothing folded', async () => {
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_missing', flow_run_id: 'run_missing' }),
requestRow({ id: 'areq_repairable', flow_run_id: 'run_repairable' }),
requestRow({ id: 'areq_dropped', flow_run_id: 'run_dropped' }),
requestRow({ id: 'areq_cascade', flow_run_id: 'run_cascade' }),
];
svc.attachAutomation(automation({
history: {
run_repairable: { status: 'failed' },
run_dropped: { status: 'failed' },
run_cascade: { status: 'failed' },
},
repairability: {
run_repairable: { repairable: true },
run_dropped: { repairable: false, reason: 'SNAPSHOT_DROPPED' },
run_cascade: { repairable: false, reason: 'NO_CONSUMED_SUSPENSION' },
},
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([
['areq_missing', 'missing'],
['areq_repairable', 'repairable'],
['areq_dropped', 'snapshot_dropped'],
['areq_cascade', 'unrepairable'],
]);
expect(out.undetermined).toBe(0);
});
it('still NEVER rewrites anything — the third oracle is a read like the other two', async () => {
engine._tables['opportunity'] = [{ id: 'opp1', approval_status: 'pending' }];
svc.attachAutomation(automation({
...failedRun, repairability: { run_1: { repairable: false, reason: 'NO_CONSUMED_SUSPENSION' } },
}));
const before = JSON.stringify(engine._tables);
await svc.inspectStrandedRequests();
expect(JSON.stringify(engine._tables)).toBe(before);
});
});
// ── commit 8c7cca1ce: a failure to DIFFERENTIATE never costs a row its place, and never
// costs another row its answer ─────────────────────────────────────────────
//
// Two residues of the #15358 contract review, ruled together (PM seat,
// 2026-09-08):
//
// item 2 — a thrown third read counted `undetermined` and DROPPED the row.
// item 3 — `refineFailedRunState(verdict)` ran OUTSIDE the `try`, so a host
// that violates its own declared surface by resolving `undefined`
// threw a `TypeError` out of `inspectStrandedRequests` and the scan
// enumerated NOTHING.
//
// Both are the same mistake at two altitudes: this method exists to enumerate
// the rows that cannot advance, so a row it could not differentiate stays in
// the report as the undifferentiated `'failed'`, and a row it could not read
// at all costs no OTHER row its answer. ⛔ Neither is a new `StrandedRunState`
// member: `'failed'` already means "reported, could not differentiate".
describe('#16709 — a failure to differentiate keeps the row, and stays local to it', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: ApprovalService;
beforeEach(() => {
engine = makeFakeEngine();
svc = new ApprovalService({ engine: engine as any });
engine._tables['sys_approval_request'] = [requestRow()];
});
const failedRun = { history: { run_1: { status: 'failed' as const } } };
it('⭐ item 3 — a host resolving `undefined` is answered, not thrown out of the scan', async () => {
// The declared surface says this member resolves a verdict. A host that
// resolves `undefined` breaks that — and `refineFailedRunState` reads
// `verdict.repairable`, so the old code's `TypeError` escaped the method.
svc.attachAutomation(automation({ ...failedRun, repairabilityMalformedFor: ['run_1'] }));
await expect(svc.inspectStrandedRequests()).resolves.toMatchObject({ scanned: 1, undetermined: 1 });
const out = await svc.inspectStrandedRequests();
// Same disposition as a thrown read: reported, undifferentiated.
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([['areq_1', 'failed']]);
});
it('⭐ items 2+3 — one bad row costs ITSELF a label and every other row nothing', async () => {
// The harm the two items share, measured on one population: before the
// fix the malformed row alone turned this whole call into a rejection, so
// `areq_ok` — a perfectly readable, perfectly repairable strand — was
// never enumerated either. A PARTIAL answer became NO answer.
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_throw', flow_run_id: 'run_throw' }),
requestRow({ id: 'areq_malformed', flow_run_id: 'run_malformed' }),
requestRow({ id: 'areq_ok', flow_run_id: 'run_ok' }),
requestRow({ id: 'areq_missing', flow_run_id: 'run_missing' }),
];
const auto = automation({
history: {
run_throw: { status: 'failed' },
run_malformed: { status: 'failed' },
run_ok: { status: 'failed' },
// `run_missing` absent on purpose — it never reaches the third oracle.
},
repairability: { run_ok: { repairable: true } },
repairabilityThrowsFor: ['run_throw'],
repairabilityMalformedFor: ['run_malformed'],
});
svc.attachAutomation(auto);
const out = await svc.inspectStrandedRequests();
expect(out.scanned).toBe(4);
expect(out.stranded.map(s => [s.requestId, s.runState])).toEqual([
['areq_throw', 'failed'],
['areq_malformed', 'failed'],
['areq_ok', 'repairable'],
['areq_missing', 'missing'],
]);
// Both undifferentiated rows are counted, and only those two.
expect(out.undetermined).toBe(2);
// The third oracle really was reached for each `failed` row, and only
// those — so the labels above are its answers, not a skipped branch.
expect(auto.inspectCalls).toEqual(['run_throw', 'run_malformed', 'run_ok']);
});
it('⛔ item 2 does NOT widen to the two earlier oracles — those still SKIP their row', async () => {
// The control that makes the ruling legible. The distinction is not "a
// throw is fine now": it is WHICH question was being asked. A thrown first
// or second oracle leaves it unknown whether the row is stranded at all,
// and condemning on an outage is the harm those arms were written for.
engine._tables['sys_approval_request'] = [requestRow({ id: 'areq_h', flow_run_id: 'run_h' })];
svc.attachAutomation(automation({ suspendedThrows: true }));
expect(await svc.inspectStrandedRequests()).toMatchObject({ scanned: 1, stranded: [], undetermined: 1 });
svc.attachAutomation(automation({ historyThrows: true }));
expect(await svc.inspectStrandedRequests()).toMatchObject({ scanned: 1, stranded: [], undetermined: 1 });
// Positive control on the same row: with both stores readable and only the
// THIRD read failing, the row IS reported — so the empty lists above are
// those two oracles' posture, not a row that was never strandable.
svc.attachAutomation(automation({
history: { run_h: { status: 'failed' } }, repairabilityThrowsFor: ['run_h'],
}));
const out = await svc.inspectStrandedRequests();
expect(out.stranded.map(s => s.runState)).toEqual(['failed']);
expect(out.undetermined).toBe(1);
});
it('⛔ still no sixth `StrandedRunState`: the undifferentiated rows are literally `failed`', async () => {
// Item 2's ruling is a re-use of an existing member, not a new one — the
// reason it touches no barrel-exported type. Every label this scan can
// emit is one of the five, and both undifferentiated shapes emit the same
// string an ABSENT member emits.
engine._tables['sys_approval_request'] = [
requestRow({ id: 'areq_absent', flow_run_id: 'run_absent' }),
requestRow({ id: 'areq_throw', flow_run_id: 'run_throw' }),
requestRow({ id: 'areq_malformed', flow_run_id: 'run_malformed' }),
];
const history = {
run_absent: { status: 'failed' }, run_throw: { status: 'failed' }, run_malformed: { status: 'failed' },
};
// The member is absent for `run_absent`'s scan…
svc.attachAutomation(automation({ history }));
const blind = await svc.inspectStrandedRequests();
// …and present-but-failing for the other two.
svc.attachAutomation(automation({
history, repairabilityThrowsFor: ['run_throw', 'run_absent'],
repairabilityMalformedFor: ['run_malformed'],
}));
const failing = await svc.inspectStrandedRequests();
expect(new Set([...blind.stranded, ...failing.stranded].map(s => s.runState))).toEqual(new Set(['failed']));
});
});
/**
* #37808 — the inspection reads the rows that can be stranded, not an arbitrary
* page of every decided request.
*
* It used to read `limit` (500) rows of every `approved` / `rejected` /
* `returned` request with no order and no cursor. Past 500 decided requests the
* page a store hands back is arbitrary (SQLite: insertion order), so a request
* stranded today, behind 500 older decisions, was never inspected on any
* sweep. Driven on a real `ObjectQL` over `SqlDriver` (better-sqlite3,
* in-memory), so the order and the filters are the store's, not a double's.
*/
describe('#37808 — past 500 decided requests, a new strand is still reported', () => {
const ORG = 'org_strand_scan';
const silent = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } as any;
let engine: ObjectQL;
let seq = 0;
/** A healthy decided request: its run completed. */
const decided = (n: number, over: Record<string, any> = {}) => {
const at = new Date(Date.parse('2026-01-01T00:00:00.000Z') + n * 60_000).toISOString();
return requestRow({
id: `areq_${String(n).padStart(5, '0')}`,
flow_run_id: `run_${n}`,
record_id: `opp_${n}`,
organization_id: ORG,
created_at: at, updated_at: at, completed_at: at,
...over,
});
};
/** Every run completed, except the ones named stranded (no pause, no history). */
const surface = (strandedRuns: string[]) => ({
async resume() { return { success: true }; },
async hasSuspendedRun() { return false; },
async getRun(runId: string) { return strandedRuns.includes(runId) ? null : { status: 'completed' }; },
});
const freshService = (strandedRuns: string[]) => {
const svc = new ApprovalService({ engine: engine as any, logger: silent });
svc.attachAutomation(surface(strandedRuns) as any);
return svc;
};
// [#38223] Seeded in batches through the engine's own array insert, not one
// `insert` per row: 1200 serial single-row writes were ~90% of the slowest
// case's ~1 s locally and 5.8–13 s on loaded merge-queue runners, past the
// 5 s test default. The rows, their order and every assertion are unchanged;
// the batch is ≤ 200 rows because SQLite refuses a multi-row INSERT past its
// 500-term compound SELECT limit.
const SEED_BATCH = 200;
const insertAll = async (rows: FakeRow[]) => {
for (let i = 0; i < rows.length; i += SEED_BATCH) {
await engine.insert('sys_approval_request', rows.slice(i, i + SEED_BATCH), { context: { isSystem: true } } as any);
}
};
// A fresh in-memory store per test: each one counts rows from zero.
beforeEach(async () => {
engine = new ObjectQL();
engine.registerDriver(new SqlDriver({
client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true,
}), true);
await engine.init();
let manifest: any;
await new ApprovalsServicePlugin().init({
getService: (name: string) => {
if (name === 'manifest') return { register: (m: any) => { manifest = m; } };
throw new Error(`no service '${name}'`);
},
logger: silent,
} as any);
for (const def of manifest.objects) engine.registry.registerObject(def as any, 'approvals-test', 'approvals-test');
await engine.syncSchemas();
await installDefaultOrganization(engine);
seq = 0;
});
afterEach(async () => {
try { await engine?.destroy(); } catch { /* noop */ }
});
it('CONTROL: under 500 decided requests the strand is reported', async () => {
await insertAll(Array.from({ length: 50 }, () => decided(++seq)));
await insertAll([decided(++seq, { flow_run_id: 'run_new_strand' })]);
const out = await freshService(['run_new_strand']).inspectStrandedRequests();
expect(out.scanned).toBe(51);
expect(out.stranded.map(s => s.runId)).toEqual(['run_new_strand']);
});
it('a request stranded today, behind 600 older decisions, is reported on the first sweep', async () => {
await insertAll(Array.from({ length: 600 }, () => decided(++seq)));
// The newest decision: its run is gone.
await insertAll([decided(++seq, { flow_run_id: 'run_new_strand' })]);
const out = await freshService(['run_new_strand']).inspectStrandedRequests();
// Newest window (ids 102–601) plus the first walk page (ids 1–500): every one of the 601 seeded rows is read.
expect(out.scanned).toBe(601);
expect(out.stranded.map(s => s.runId)).toEqual(['run_new_strand']);
});
it('an older strand outside the newest window is reached by the cursor within a bounded number of sweeps', async () => {
// The strand is the OLDEST decision (last by `updated_at`), stored last.
await insertAll(Array.from({ length: 1200 }, () => decided(++seq + 10)));
await insertAll([decided(1, { id: 'areq_zz_old_strand', flow_run_id: 'run_old_strand' })]);
const svc = freshService(['run_old_strand']);
const found: number[] = [];
// 1201 rows, 500 per window: the cursor covers the set within ceil(1201 / 500) = 3 sweeps.
for (let sweep = 1; sweep <= 3; sweep++) {
const out = await svc.inspectStrandedRequests();
expect(out.scanned, `sweep ${sweep} reads at most two windows`).toBeLessThanOrEqual(1000);
if (out.stranded.some(s => s.runId === 'run_old_strand')) found.push(sweep);
}
// Its id sorts last, so the walk reaches it on the third sweep: not missed, and not reported by the newest window.
expect(found).toEqual([3]);
// The walk then starts again from the first id: the next cycle reaches it again.
const again: number[] = [];
for (let sweep = 4; sweep <= 6; sweep++) {
if ((await svc.inspectStrandedRequests()).stranded.some(s => s.runId === 'run_old_strand')) again.push(sweep);
}
expect(again).toEqual([6]);
});
});