effect-mq 0.4.2 → 0.6.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 (64) hide show
  1. package/README.md +85 -17
  2. package/dist/Flow.d.ts +381 -0
  3. package/dist/Flow.d.ts.map +1 -0
  4. package/dist/Flow.js +340 -0
  5. package/dist/Flow.js.map +1 -0
  6. package/dist/Job.d.ts +37 -6
  7. package/dist/Job.d.ts.map +1 -1
  8. package/dist/Job.js +17 -2
  9. package/dist/Job.js.map +1 -1
  10. package/dist/JobSchedules.d.ts +112 -0
  11. package/dist/JobSchedules.d.ts.map +1 -0
  12. package/dist/JobSchedules.js +106 -0
  13. package/dist/JobSchedules.js.map +1 -0
  14. package/dist/JobStore.d.ts +320 -10
  15. package/dist/JobStore.d.ts.map +1 -1
  16. package/dist/JobStore.js.map +1 -1
  17. package/dist/MemoryJobStore.d.ts.map +1 -1
  18. package/dist/MemoryJobStore.js +336 -8
  19. package/dist/MemoryJobStore.js.map +1 -1
  20. package/dist/Metrics.d.ts +31 -0
  21. package/dist/Metrics.d.ts.map +1 -1
  22. package/dist/Metrics.js +39 -0
  23. package/dist/Metrics.js.map +1 -1
  24. package/dist/Worker.d.ts +120 -11
  25. package/dist/Worker.d.ts.map +1 -1
  26. package/dist/Worker.js +452 -26
  27. package/dist/Worker.js.map +1 -1
  28. package/dist/drizzle-postgres/DrizzleJobStore.d.ts +19 -1
  29. package/dist/drizzle-postgres/DrizzleJobStore.d.ts.map +1 -1
  30. package/dist/drizzle-postgres/DrizzleJobStore.js +662 -81
  31. package/dist/drizzle-postgres/DrizzleJobStore.js.map +1 -1
  32. package/dist/drizzle-postgres/schema.d.ts +310 -3
  33. package/dist/drizzle-postgres/schema.d.ts.map +1 -1
  34. package/dist/drizzle-postgres/schema.js +68 -1
  35. package/dist/drizzle-postgres/schema.js.map +1 -1
  36. package/dist/index.d.ts +14 -0
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +14 -0
  39. package/dist/index.js.map +1 -1
  40. package/dist/redis/RedisJobStore.d.ts.map +1 -1
  41. package/dist/redis/RedisJobStore.js +221 -19
  42. package/dist/redis/RedisJobStore.js.map +1 -1
  43. package/dist/redis/scripts.d.ts +118 -11
  44. package/dist/redis/scripts.d.ts.map +1 -1
  45. package/dist/redis/scripts.js +497 -28
  46. package/dist/redis/scripts.js.map +1 -1
  47. package/dist/testing/conformance.d.ts +6 -0
  48. package/dist/testing/conformance.d.ts.map +1 -1
  49. package/dist/testing/conformance.js +765 -1
  50. package/dist/testing/conformance.js.map +1 -1
  51. package/package.json +1 -1
  52. package/src/Flow.ts +778 -0
  53. package/src/Job.ts +42 -11
  54. package/src/JobSchedules.ts +223 -0
  55. package/src/JobStore.ts +347 -9
  56. package/src/MemoryJobStore.ts +372 -8
  57. package/src/Metrics.ts +43 -0
  58. package/src/Worker.ts +726 -37
  59. package/src/drizzle-postgres/DrizzleJobStore.ts +827 -82
  60. package/src/drizzle-postgres/schema.ts +94 -0
  61. package/src/index.ts +16 -0
  62. package/src/redis/RedisJobStore.ts +291 -8
  63. package/src/redis/scripts.ts +529 -26
  64. package/src/testing/conformance.ts +989 -1
@@ -14,6 +14,12 @@
14
14
  * Effect `Clock` (e.g. pass `now` into queries as a bind parameter) — never
15
15
  * from the database server's clock — so this works against real storage too.
16
16
  *
17
+ * Beyond the core queue contract, dedicated sections pin the flow contract:
18
+ * parent-side ownership (the FanOut ack, dependency rows, batched
19
+ * `recordChildResults`, every settle decision) and the child-side outbox
20
+ * (terminal transitions of envelope-carrying jobs staging reports for the
21
+ * relay to another store).
22
+ *
17
23
  * @since 0.1.0
18
24
  */
19
25
  import * as JobStore from "../JobStore.js";
@@ -34,6 +40,7 @@ const baseRequest = (overrides) => ({
34
40
  timeoutMs: undefined,
35
41
  dedupe: undefined,
36
42
  trace: undefined,
43
+ parent: undefined,
37
44
  delayMs: 0,
38
45
  ...overrides
39
46
  });
@@ -429,6 +436,7 @@ export const jobStoreConformance = (name, storeLayer) => {
429
436
  waiting: 1,
430
437
  delayed: 1,
431
438
  active: 1,
439
+ "waiting-children": 0,
432
440
  completed: 0,
433
441
  failed: 0,
434
442
  cancelled: 0
@@ -565,6 +573,7 @@ export const jobStoreConformance = (name, storeLayer) => {
565
573
  backoff: { _tag: "fixed", delayMs: 1_000 },
566
574
  keep: undefined,
567
575
  timeoutMs: 5_000,
576
+ group: undefined,
568
577
  nextRunAt: 60_000
569
578
  };
570
579
  yield* store.upsertSchedule(schedule);
@@ -601,8 +610,41 @@ export const jobStoreConformance = (name, storeLayer) => {
601
610
  backoff: undefined,
602
611
  keep: undefined,
603
612
  timeoutMs: undefined,
613
+ group: undefined,
604
614
  nextRunAt: 60_000
605
615
  });
616
+ it.effect("schedule group labels persist and filter listSchedules", () => withStore((store) => Effect.gen(function* () {
617
+ const base = minutelySchedule();
618
+ yield* store.upsertSchedule({
619
+ ...base,
620
+ key: JobStore.ScheduleKey("TestJob/labeled"),
621
+ group: "svc-a"
622
+ });
623
+ yield* store.upsertSchedule({
624
+ ...base,
625
+ key: JobStore.ScheduleKey("TestJob/other"),
626
+ group: "svc-b"
627
+ });
628
+ yield* store.upsertSchedule({ ...base, key: JobStore.ScheduleKey("TestJob/plain") });
629
+ const labeled = yield* store.listSchedules({ group: "svc-a" });
630
+ expect(labeled.map((schedule) => schedule.key)).toEqual(["TestJob/labeled"]);
631
+ expect(labeled[0]?.group).toBe("svc-a");
632
+ const all = yield* store.listSchedules();
633
+ expect(all).toHaveLength(3);
634
+ expect(all.find((schedule) => schedule.key === "TestJob/plain")?.group).toBeUndefined();
635
+ // Re-upsert relabels; an unchanged cadence still keeps its next
636
+ // occurrence (the reconciler re-registers on every startup).
637
+ yield* store.upsertSchedule({
638
+ ...base,
639
+ key: JobStore.ScheduleKey("TestJob/labeled"),
640
+ group: "svc-c",
641
+ nextRunAt: 999_999
642
+ });
643
+ const relabeled = yield* store.listSchedules({ group: "svc-c" });
644
+ expect(relabeled.map((schedule) => schedule.key)).toEqual(["TestJob/labeled"]);
645
+ expect(relabeled[0]?.nextRunAt).toBe(60_000);
646
+ expect(yield* store.listSchedules({ group: "svc-a" })).toEqual([]);
647
+ })));
606
648
  it.effect("tickSchedule fires a slot exactly once and advances atomically", () => withStore((store) => Effect.gen(function* () {
607
649
  const schedule = minutelySchedule();
608
650
  yield* store.upsertSchedule(schedule);
@@ -758,7 +800,14 @@ export const jobStoreConformance = (name, storeLayer) => {
758
800
  backoff: { _tag: "fixed", delayMs: 2_000 },
759
801
  keep: { completed: { count: 2, ageMs: undefined } },
760
802
  timeoutMs: 9_000,
761
- trace: { traceId: "trace-1", spanId: "span-1", sampled: true, delayed: false }
803
+ trace: { traceId: "trace-1", spanId: "span-1", sampled: true, delayed: false },
804
+ parent: {
805
+ flowName: "rich-flow",
806
+ flowId: JobId("rich-parent"),
807
+ childKey: "rich-child",
808
+ parentStoreKey: "effect-mq/JobStore",
809
+ depth: 1
810
+ }
762
811
  });
763
812
  yield* store.enqueue(richRequest("rich-single"));
764
813
  expect(yield* store.enqueueMany([richRequest("rich-batch")]))
@@ -785,6 +834,8 @@ export const jobStoreConformance = (name, storeLayer) => {
785
834
  timeoutMs: job.timeoutMs,
786
835
  cancelRequested: job.cancelRequested,
787
836
  trace: job.trace,
837
+ parent: job.parent,
838
+ flow: job.flow,
788
839
  runAt: job.runAt,
789
840
  enqueuedAt: job.enqueuedAt
790
841
  });
@@ -803,11 +854,27 @@ export const jobStoreConformance = (name, storeLayer) => {
803
854
  delayed: false
804
855
  });
805
856
  expect(expected.payload).toEqual({ big: 1234567890123456, nested: { arr: [1, 2, 3] } });
857
+ expect(expected.parent).toEqual({
858
+ flowName: "rich-flow",
859
+ flowId: "rich-parent",
860
+ childKey: "rich-child",
861
+ parentStoreKey: "effect-mq/JobStore",
862
+ depth: 1
863
+ });
864
+ expect(expected.flow).toBeUndefined();
806
865
  for (const id of [JobId("rich-batch"), JobId("rich-tick")]) {
807
866
  const job = yield* store.getJob(id);
808
867
  assert(Option.isSome(job));
809
868
  expect(project(job.value)).toEqual(expected);
810
869
  }
870
+ // `list` must return the same complete records as `getJob` —
871
+ // drivers with hand-written SELECT lists can silently drop fields
872
+ // there while every getJob-path test stays green.
873
+ const listed = yield* store.list({ name: "TestJob" });
874
+ expect(listed.items).toHaveLength(3);
875
+ for (const job of listed.items) {
876
+ expect(project(job)).toEqual(expected);
877
+ }
811
878
  })));
812
879
  it.effect("enqueueMany wakes parked takers", () => withStore((store) => Effect.gen(function* () {
813
880
  const empty = yield* store.claim(claimOptions());
@@ -887,6 +954,7 @@ export const jobStoreConformance = (name, storeLayer) => {
887
954
  backoff: undefined,
888
955
  keep: undefined,
889
956
  timeoutMs: undefined,
957
+ group: undefined,
890
958
  nextRunAt: 60_000
891
959
  };
892
960
  yield* store.upsertSchedule(base);
@@ -916,6 +984,7 @@ export const jobStoreConformance = (name, storeLayer) => {
916
984
  backoff: undefined,
917
985
  keep: undefined,
918
986
  timeoutMs: undefined,
987
+ group: undefined,
919
988
  nextRunAt: 90_000
920
989
  };
921
990
  const cronSchedule = {
@@ -972,6 +1041,7 @@ export const jobStoreConformance = (name, storeLayer) => {
972
1041
  waiting: 1,
973
1042
  delayed: 0,
974
1043
  active: 0,
1044
+ "waiting-children": 0,
975
1045
  completed: 0,
976
1046
  failed: 0,
977
1047
  cancelled: 1
@@ -993,6 +1063,7 @@ export const jobStoreConformance = (name, storeLayer) => {
993
1063
  backoff: undefined,
994
1064
  keep: undefined,
995
1065
  timeoutMs: undefined,
1066
+ group: undefined,
996
1067
  nextRunAt: 60_000
997
1068
  };
998
1069
  yield* store.upsertSchedule(schedule);
@@ -1206,6 +1277,699 @@ export const jobStoreConformance = (name, storeLayer) => {
1206
1277
  assert(Option.isSome(job));
1207
1278
  expect(job.value.state).toBe("waiting");
1208
1279
  })));
1280
+ // ----------------------------------------------------------------------
1281
+ // Flows (parent-child). The parent store owns the flow: the FanOut ack,
1282
+ // dependency rows, pending counter, and every settle decision are pinned
1283
+ // here. `storeKey` strings are opaque to the store.
1284
+ // ----------------------------------------------------------------------
1285
+ const parentEnvelope = (flowId, key) => ({
1286
+ flowName: "test-flow",
1287
+ flowId,
1288
+ childKey: key,
1289
+ parentStoreKey: "main",
1290
+ depth: 1
1291
+ });
1292
+ const childSpec = (flowId, key, overrides) => ({
1293
+ childKey: key,
1294
+ storeKey: "effect-mq/JobStore/children",
1295
+ request: baseRequest({
1296
+ id: JobId(`flow/main/${flowId}/${key}`),
1297
+ name: "ChildJob",
1298
+ parent: parentEnvelope(flowId, key),
1299
+ ...overrides
1300
+ })
1301
+ });
1302
+ const fanOutParent = (store, options) => Effect.gen(function* () {
1303
+ const { id } = yield* store.enqueue(baseRequest({ payload: { parent: true } }));
1304
+ const claim = yield* store.claim(claimOptions({ token: "t-parent" }));
1305
+ assert(claim._tag === "Claimed");
1306
+ expect(claim.job.id).toBe(id);
1307
+ const keys = options?.children ?? ["a", "b"];
1308
+ yield* store.ack(id, "t-parent", {
1309
+ _tag: "FanOut",
1310
+ failFast: options?.failFast ?? false,
1311
+ children: keys.map((key) => childSpec(id, key))
1312
+ });
1313
+ return id;
1314
+ });
1315
+ const report = (flowId, key, outcome, overrides) => ({
1316
+ flowId,
1317
+ childKey: key,
1318
+ outcome,
1319
+ exit: { ok: outcome === "completed" },
1320
+ failedReason: undefined,
1321
+ ...overrides
1322
+ });
1323
+ // Batch-of-one sugar for the single-report pins below; the batch
1324
+ // semantics get their own section.
1325
+ const recordOne = (store, value) => Effect.map(store.recordChildResults([value]), (results) => results[0] ?? { applied: false, parentSettled: false });
1326
+ const flowCounts = (overrides) => ({
1327
+ failFast: false,
1328
+ pending: 0,
1329
+ completed: 0,
1330
+ failed: 0,
1331
+ cancelled: 0,
1332
+ ...overrides
1333
+ });
1334
+ it.effect("FanOut parks the parent with its manifest, rows, and ledger entry", () => withStore((store) => Effect.gen(function* () {
1335
+ const flowId = yield* fanOutParent(store);
1336
+ const parent = yield* store.getJob(flowId);
1337
+ assert(Option.isSome(parent));
1338
+ expect(parent.value.state).toBe("waiting-children");
1339
+ expect(parent.value.flow).toEqual(flowCounts({ pending: 2 }));
1340
+ // A fan-out is a phase transition, not a completed run.
1341
+ expect(parent.value.attemptsMade).toBe(0);
1342
+ const attempts = yield* store.getAttempts(flowId);
1343
+ expect(attempts.map((attempt) => attempt.outcome)).toEqual(["fanned-out"]);
1344
+ const rows = yield* store.listChildResults(flowId);
1345
+ expect(rows.cursor).toBeUndefined();
1346
+ expect(rows.items.map((row) => ({
1347
+ childKey: row.childKey,
1348
+ name: row.name,
1349
+ storeKey: row.storeKey,
1350
+ childJobId: row.childJobId,
1351
+ status: row.status,
1352
+ cascaded: row.cascaded
1353
+ }))).toEqual([
1354
+ {
1355
+ childKey: "a",
1356
+ name: "ChildJob",
1357
+ storeKey: "effect-mq/JobStore/children",
1358
+ childJobId: `flow/main/${flowId}/a`,
1359
+ status: "pending",
1360
+ cascaded: false
1361
+ },
1362
+ {
1363
+ childKey: "b",
1364
+ name: "ChildJob",
1365
+ storeKey: "effect-mq/JobStore/children",
1366
+ childJobId: `flow/main/${flowId}/b`,
1367
+ status: "pending",
1368
+ cascaded: false
1369
+ }
1370
+ ]);
1371
+ // Parked parents are never claimable and show in counts.
1372
+ const claim = yield* store.claim(claimOptions({ token: "t-again" }));
1373
+ expect(claim._tag).toBe("Empty");
1374
+ expect((yield* store.counts())["waiting-children"]).toBe(1);
1375
+ })));
1376
+ it.effect("FanOut is lock-token-guarded and validates child ids", () => withStore((store) => Effect.gen(function* () {
1377
+ const { id } = yield* store.enqueue(baseRequest());
1378
+ const claim = yield* store.claim(claimOptions({ token: "t-owner" }));
1379
+ assert(claim._tag === "Claimed");
1380
+ const stale = yield* Effect.exit(store.ack(id, "t-wrong", {
1381
+ _tag: "FanOut",
1382
+ failFast: false,
1383
+ children: [childSpec(id, "a")]
1384
+ }));
1385
+ assert(Exit.isFailure(stale));
1386
+ // A spec without an explicit id fails loudly and leaves the job
1387
+ // active (the ack can be retried with a fixed spec).
1388
+ const bad = yield* Effect.exit(store.ack(id, "t-owner", {
1389
+ _tag: "FanOut",
1390
+ failFast: false,
1391
+ children: [{ ...childSpec(id, "a"), request: baseRequest({ id: undefined }) }]
1392
+ }));
1393
+ assert(Exit.isFailure(bad));
1394
+ const job = yield* store.getJob(id);
1395
+ assert(Option.isSome(job));
1396
+ expect(job.value.state).toBe("active");
1397
+ yield* store.ack(id, "t-owner", {
1398
+ _tag: "FanOut",
1399
+ failFast: false,
1400
+ children: [childSpec(id, "a")]
1401
+ });
1402
+ })));
1403
+ it.effect("an empty FanOut settles straight to runnable collect", () => withStore((store) => Effect.gen(function* () {
1404
+ const flowId = yield* fanOutParent(store, { children: [] });
1405
+ const parent = yield* store.getJob(flowId);
1406
+ assert(Option.isSome(parent));
1407
+ expect(parent.value.state).toBe("waiting");
1408
+ expect(parent.value.flow).toEqual(flowCounts());
1409
+ const claim = yield* store.claim(claimOptions({ token: "t-resume" }));
1410
+ assert(claim._tag === "Claimed");
1411
+ expect(claim.job.id).toBe(flowId);
1412
+ expect(claim.job.flow).toEqual(flowCounts());
1413
+ })));
1414
+ it.effect("recordChildResults applies once, decrements, and settles on the last report", () => withStore((store) => Effect.gen(function* () {
1415
+ const flowId = yield* fanOutParent(store);
1416
+ const first = yield* recordOne(store, report(flowId, "a", "completed"));
1417
+ expect(first).toEqual({ applied: true, parentSettled: false });
1418
+ const midway = yield* store.getJob(flowId);
1419
+ assert(Option.isSome(midway));
1420
+ expect(midway.value.state).toBe("waiting-children");
1421
+ expect(midway.value.flow?.pending).toBe(1);
1422
+ // Duplicates and unknowns drop on the dependency row.
1423
+ expect(yield* recordOne(store, report(flowId, "a", "failed")))
1424
+ .toEqual({ applied: false, parentSettled: false });
1425
+ expect(yield* recordOne(store, report(flowId, "ghost", "completed")))
1426
+ .toEqual({ applied: false, parentSettled: false });
1427
+ expect(yield* recordOne(store, report(JobId("no-such-flow"), "a", "completed")))
1428
+ .toEqual({ applied: false, parentSettled: false });
1429
+ const last = yield* recordOne(store, report(flowId, "b", "failed", {
1430
+ exit: { boom: true }
1431
+ }));
1432
+ expect(last).toEqual({ applied: true, parentSettled: true });
1433
+ // Settled: runnable now, phase collect, results recorded exactly.
1434
+ const parent = yield* store.getJob(flowId);
1435
+ assert(Option.isSome(parent));
1436
+ expect(parent.value.state).toBe("waiting");
1437
+ // The counters mirror the recorded outcomes exactly — via getJob,
1438
+ // via the claimed record (what `collect` reads its counts from),
1439
+ // and via list (what dashboards read). A driver whose claim/list
1440
+ // projections drop the counter columns fails here, not in prod.
1441
+ expect(parent.value.flow).toEqual(flowCounts({ completed: 1, failed: 1 }));
1442
+ const claim = yield* store.claim(claimOptions({ token: "t-resume" }));
1443
+ assert(claim._tag === "Claimed");
1444
+ expect(claim.job.id).toBe(flowId);
1445
+ expect(claim.job.flow).toEqual(flowCounts({ completed: 1, failed: 1 }));
1446
+ const listed = yield* store.list({ name: "TestJob" });
1447
+ expect(listed.items.find((job) => job.id === flowId)?.flow)
1448
+ .toEqual(flowCounts({ completed: 1, failed: 1 }));
1449
+ const rows = yield* store.listChildResults(flowId);
1450
+ const byKey = new Map(rows.items.map((row) => [row.childKey, row]));
1451
+ expect(byKey.get("a")?.status).toBe("completed");
1452
+ expect(byKey.get("a")?.exit).toEqual({ ok: true });
1453
+ // A recorded outcome came FROM the child's store: nothing to cascade.
1454
+ expect(byKey.get("a")?.cascaded).toBe(true);
1455
+ expect(byKey.get("b")?.status).toBe("failed");
1456
+ expect(byKey.get("b")?.exit).toEqual({ boom: true });
1457
+ })));
1458
+ it.effect("recordChildResults wakes a taker parked on the parent's queue", () => withStore((store) => Effect.gen(function* () {
1459
+ const flowId = yield* fanOutParent(store, { children: ["only"] });
1460
+ const empty = yield* store.claim(claimOptions({ token: "t-idle" }));
1461
+ assert(empty._tag === "Empty");
1462
+ const waiter = yield* Effect.forkChild(store.awaitWake([QueueName("default")], empty.wakeToken));
1463
+ yield* TestClock.adjust(1);
1464
+ yield* recordOne(store, report(flowId, "only", "completed"));
1465
+ yield* TestClock.adjust(1);
1466
+ expect(yield* Fiber.join(waiter)).toBeUndefined();
1467
+ })));
1468
+ it.effect("concurrent last reports settle the parent exactly once", () => withStore((store) => Effect.gen(function* () {
1469
+ const flowId = yield* fanOutParent(store);
1470
+ const results = yield* Effect.all([
1471
+ recordOne(store, report(flowId, "a", "completed")),
1472
+ recordOne(store, report(flowId, "b", "completed")),
1473
+ recordOne(store, report(flowId, "a", "completed")),
1474
+ recordOne(store, report(flowId, "b", "completed"))
1475
+ ], { concurrency: 4 });
1476
+ expect(results.filter((result) => result.applied).length).toBe(2);
1477
+ expect(results.filter((result) => result.parentSettled).length).toBe(1);
1478
+ })));
1479
+ it.effect("fail-fast settles the parent terminally and marks remaining rows", () => withStore((store) => Effect.gen(function* () {
1480
+ const flowId = yield* fanOutParent(store, { children: ["a", "b", "c"], failFast: true });
1481
+ yield* recordOne(store, report(flowId, "a", "completed"));
1482
+ const settle = yield* recordOne(store, report(flowId, "b", "failed"));
1483
+ expect(settle).toEqual({ applied: true, parentSettled: true });
1484
+ const parent = yield* store.getJob(flowId);
1485
+ assert(Option.isSome(parent));
1486
+ expect(parent.value.state).toBe("failed");
1487
+ expect(parent.value.failedReason).toContain("b");
1488
+ expect(parent.value.exit).toBeUndefined();
1489
+ const rows = yield* store.listChildResults(flowId);
1490
+ const remaining = rows.items.find((row) => row.childKey === "c");
1491
+ expect(remaining?.status).toBe("cancelled");
1492
+ // Marked by the settle — the sweeper still owes a real cancel.
1493
+ expect(remaining?.cascaded).toBe(false);
1494
+ // Settle-time marking lands in the counters too.
1495
+ expect(parent.value.flow).toEqual(flowCounts({ failFast: true, completed: 1, failed: 1, cancelled: 1 }));
1496
+ // A late completion finds its row terminal and drops.
1497
+ expect(yield* recordOne(store, report(flowId, "c", "completed")))
1498
+ .toEqual({ applied: false, parentSettled: false });
1499
+ })));
1500
+ it.effect("cancelling a waiting-children parent settles and marks its pending rows", () => withStore((store) => Effect.gen(function* () {
1501
+ const flowId = yield* fanOutParent(store);
1502
+ yield* recordOne(store, report(flowId, "a", "completed"));
1503
+ yield* store.cancel(flowId);
1504
+ const parent = yield* store.getJob(flowId);
1505
+ assert(Option.isSome(parent));
1506
+ expect(parent.value.state).toBe("cancelled");
1507
+ // The settle marking moves the counters too.
1508
+ expect(parent.value.flow).toEqual(flowCounts({ completed: 1, cancelled: 1 }));
1509
+ const rows = yield* store.listChildResults(flowId);
1510
+ const byKey = new Map(rows.items.map((row) => [row.childKey, row]));
1511
+ expect(byKey.get("a")?.status).toBe("completed");
1512
+ expect(byKey.get("b")?.status).toBe("cancelled");
1513
+ expect(byKey.get("b")?.cascaded).toBe(false);
1514
+ expect(yield* recordOne(store, report(flowId, "b", "completed")))
1515
+ .toEqual({ applied: false, parentSettled: false });
1516
+ })));
1517
+ it.effect("a cancel that races the fan-out wins and marks the rows", () => withStore((store) => Effect.gen(function* () {
1518
+ const { id } = yield* store.enqueue(baseRequest());
1519
+ const claim = yield* store.claim(claimOptions({ token: "t-parent" }));
1520
+ assert(claim._tag === "Claimed");
1521
+ // Cancel the ACTIVE parent (sets cancelRequested), then the worker
1522
+ // acks its fan-out: cancellation wins over parking.
1523
+ yield* store.cancel(id);
1524
+ yield* store.ack(id, "t-parent", {
1525
+ _tag: "FanOut",
1526
+ failFast: false,
1527
+ children: [childSpec(id, "a"), childSpec(id, "b")]
1528
+ });
1529
+ const parent = yield* store.getJob(id);
1530
+ assert(Option.isSome(parent));
1531
+ expect(parent.value.state).toBe("cancelled");
1532
+ expect(parent.value.flow).toEqual(flowCounts({ cancelled: 2 }));
1533
+ // The manifest landed and every row was marked for cascade, so
1534
+ // the sweeper delivers (mostly no-op) cancels to the child store.
1535
+ const rows = yield* store.listChildResults(id);
1536
+ expect(rows.items.map((row) => row.status)).toEqual(["cancelled", "cancelled"]);
1537
+ expect(rows.items.every((row) => !row.cascaded)).toBe(true);
1538
+ const work = yield* store.flowSweepWork({ pendingAgeMs: 0 });
1539
+ expect(work.cascade[0]?.children.map((child) => child.childKey)).toEqual(["a", "b"]);
1540
+ })));
1541
+ it.effect("promote and retry reject a waiting-children parent", () => withStore((store) => Effect.gen(function* () {
1542
+ const flowId = yield* fanOutParent(store);
1543
+ const promoted = yield* Effect.exit(store.promote(flowId));
1544
+ assert(Exit.isFailure(promoted));
1545
+ const retried = yield* Effect.exit(store.retry(flowId));
1546
+ assert(Exit.isFailure(retried));
1547
+ })));
1548
+ it.effect("retrying a fail-fast-failed parent re-enters collect with its manifest", () => withStore((store) => Effect.gen(function* () {
1549
+ const flowId = yield* fanOutParent(store, { children: ["a", "b"], failFast: true });
1550
+ yield* recordOne(store, report(flowId, "a", "failed"));
1551
+ yield* store.retry(flowId);
1552
+ const parent = yield* store.getJob(flowId);
1553
+ assert(Option.isSome(parent));
1554
+ expect(parent.value.state).toBe("waiting");
1555
+ // The manifest survives: a re-claimed parent dispatches collect,
1556
+ // never a second fan-out.
1557
+ expect(parent.value.flow).toBeDefined();
1558
+ expect((yield* store.listChildResults(flowId)).items.length).toBe(2);
1559
+ })));
1560
+ it.effect("a second FanOut converges on the persisted manifest", () => withStore((store) => Effect.gen(function* () {
1561
+ const flowId = yield* fanOutParent(store, { children: ["a"] });
1562
+ yield* recordOne(store, report(flowId, "a", "completed"));
1563
+ // Parent settled to waiting; claim and (bug-path) fan out again
1564
+ // with DIFFERENT children.
1565
+ const claim = yield* store.claim(claimOptions({ token: "t-double" }));
1566
+ assert(claim._tag === "Claimed");
1567
+ yield* store.ack(flowId, "t-double", {
1568
+ _tag: "FanOut",
1569
+ failFast: false,
1570
+ children: [childSpec(flowId, "x"), childSpec(flowId, "y")]
1571
+ });
1572
+ // The original manifest is untouched; state follows its pending
1573
+ // count (0 → runnable collect again).
1574
+ const rows = yield* store.listChildResults(flowId);
1575
+ expect(rows.items.map((row) => row.childKey)).toEqual(["a"]);
1576
+ const parent = yield* store.getJob(flowId);
1577
+ assert(Option.isSome(parent));
1578
+ expect(parent.value.state).toBe("waiting");
1579
+ })));
1580
+ it.effect("flowSweepWork scopes reconcile by parent state and pending age", () => withStore((store) => Effect.gen(function* () {
1581
+ const flowId = yield* fanOutParent(store);
1582
+ // Fresh rows are the push path's business.
1583
+ const fresh = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1584
+ expect(fresh.reconcile).toEqual([]);
1585
+ expect(fresh.cascade).toEqual([]);
1586
+ yield* TestClock.adjust(30_000);
1587
+ const due = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1588
+ expect(due.reconcile.length).toBe(1);
1589
+ expect(due.reconcile[0]?.flowId).toBe(flowId);
1590
+ expect(due.reconcile[0]?.children.map((child) => child.childKey)).toEqual(["a", "b"]);
1591
+ // The stored spec is the complete original request.
1592
+ expect(due.reconcile[0]?.children[0]?.request).toEqual(childSpec(flowId, "a").request);
1593
+ // Returned rows are re-armed: they leave the page for another full
1594
+ // age, so a sweep page rotates instead of pinning its head.
1595
+ const rearmed = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1596
+ expect(rearmed.reconcile).toEqual([]);
1597
+ // A recorded row leaves the reconcile set for good; a settled
1598
+ // parent leaves it entirely.
1599
+ yield* recordOne(store, report(flowId, "a", "completed"));
1600
+ yield* TestClock.adjust(30_000);
1601
+ const partial = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1602
+ expect(partial.reconcile[0]?.children.map((child) => child.childKey)).toEqual(["b"]);
1603
+ yield* recordOne(store, report(flowId, "b", "completed"));
1604
+ yield* TestClock.adjust(30_000);
1605
+ const settled = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1606
+ expect(settled.reconcile).toEqual([]);
1607
+ })));
1608
+ it.effect("a fail-fast report that is also the last pending row settles as failed", () => withStore((store) => Effect.gen(function* () {
1609
+ const flowId = yield* fanOutParent(store, { children: ["a", "b"], failFast: true });
1610
+ yield* recordOne(store, report(flowId, "a", "completed"));
1611
+ // This report triggers BOTH settle rules: pending hits zero AND it
1612
+ // is the first failure under fail-fast. Fail-fast wins: terminal
1613
+ // `failed`, never a resume into collect.
1614
+ const last = yield* recordOne(store, report(flowId, "b", "failed"));
1615
+ expect(last).toEqual({ applied: true, parentSettled: true });
1616
+ const parent = yield* store.getJob(flowId);
1617
+ assert(Option.isSome(parent));
1618
+ expect(parent.value.state).toBe("failed");
1619
+ expect(parent.value.failedReason).toContain("b");
1620
+ })));
1621
+ it.effect("an empty FanOut wakes takers parked on the parent's queue", () => withStore((store) => Effect.gen(function* () {
1622
+ const { id } = yield* store.enqueue(baseRequest());
1623
+ const claim = yield* store.claim(claimOptions({ token: "t-parent" }));
1624
+ assert(claim._tag === "Claimed");
1625
+ const empty = yield* store.claim(claimOptions({ token: "t-idle" }));
1626
+ assert(empty._tag === "Empty");
1627
+ const waiter = yield* Effect.forkChild(store.awaitWake([QueueName("default")], empty.wakeToken));
1628
+ yield* TestClock.adjust(1);
1629
+ yield* store.ack(id, "t-parent", { _tag: "FanOut", failFast: false, children: [] });
1630
+ yield* TestClock.adjust(1);
1631
+ expect(yield* Fiber.join(waiter)).toBeUndefined();
1632
+ })));
1633
+ it.effect("automatic retention spares a settled parent that still owes cascades", () => withStore((store) => Effect.gen(function* () {
1634
+ // A fail-fast settle marks rows for cascade in the same op that
1635
+ // makes the parent prunable — retention must not race the sweeper
1636
+ // out of its only record that cancels are still owed.
1637
+ const keep = { failed: { count: 1, ageMs: undefined } };
1638
+ const { id: flowId } = yield* store.enqueue(baseRequest({ keep }));
1639
+ const claim = yield* store.claim(claimOptions({ token: "t-parent" }));
1640
+ assert(claim._tag === "Claimed");
1641
+ yield* store.ack(flowId, "t-parent", {
1642
+ _tag: "FanOut",
1643
+ failFast: true,
1644
+ children: [childSpec(flowId, "a"), childSpec(flowId, "b")]
1645
+ });
1646
+ yield* recordOne(store, report(flowId, "a", "failed"));
1647
+ // A newer failed peer would evict the flow parent under count: 1 —
1648
+ // but its "b" row is cancelled and not yet cascaded.
1649
+ const { id: peer1 } = yield* store.enqueue(baseRequest({ keep }));
1650
+ const claim1 = yield* store.claim(claimOptions({ token: "t-p1" }));
1651
+ assert(claim1._tag === "Claimed");
1652
+ yield* store.ack(peer1, "t-p1", { _tag: "Fail", exit: undefined });
1653
+ const spared = yield* store.getJob(flowId);
1654
+ assert(Option.isSome(spared));
1655
+ expect(spared.value.state).toBe("failed");
1656
+ expect((yield* store.listChildResults(flowId)).items.length).toBe(2);
1657
+ // Once the cascade is delivered, retention applies normally.
1658
+ yield* store.markChildrenCascaded(flowId, ["b"]);
1659
+ const { id: peer2 } = yield* store.enqueue(baseRequest({ keep }));
1660
+ const claim2 = yield* store.claim(claimOptions({ token: "t-p2" }));
1661
+ assert(claim2._tag === "Claimed");
1662
+ yield* store.ack(peer2, "t-p2", { _tag: "Fail", exit: undefined });
1663
+ expect(Option.isNone(yield* store.getJob(flowId))).toBe(true);
1664
+ expect((yield* store.listChildResults(flowId)).items).toEqual([]);
1665
+ })));
1666
+ it.effect("flowSweepWork yields cascade work until rows are marked cascaded", () => withStore((store) => Effect.gen(function* () {
1667
+ const flowId = yield* fanOutParent(store, { children: ["a", "b"], failFast: true });
1668
+ yield* recordOne(store, report(flowId, "a", "failed"));
1669
+ const work = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1670
+ // Settled flow: nothing to reconcile, but "b" owes a cascade.
1671
+ expect(work.reconcile).toEqual([]);
1672
+ expect(work.cascade.length).toBe(1);
1673
+ expect(work.cascade[0]?.children).toEqual([{
1674
+ childKey: "b",
1675
+ storeKey: "effect-mq/JobStore/children",
1676
+ childJobId: `flow/main/${flowId}/b`
1677
+ }]);
1678
+ yield* store.markChildrenCascaded(flowId, ["b"]);
1679
+ const after = yield* store.flowSweepWork({ pendingAgeMs: 30_000 });
1680
+ expect(after.cascade).toEqual([]);
1681
+ // Idempotent (unknown keys included).
1682
+ yield* store.markChildrenCascaded(flowId, ["b", "ghost"]);
1683
+ })));
1684
+ it.effect("listChildResults paginates in child-key order", () => withStore((store) => Effect.gen(function* () {
1685
+ const flowId = yield* fanOutParent(store, { children: ["c", "a", "b"] });
1686
+ const first = yield* store.listChildResults(flowId, { limit: 2 });
1687
+ expect(first.items.map((row) => row.childKey)).toEqual(["a", "b"]);
1688
+ expect(first.cursor).toBeDefined();
1689
+ const second = yield* store.listChildResults(flowId, {
1690
+ cursor: first.cursor,
1691
+ limit: 2
1692
+ });
1693
+ expect(second.items.map((row) => row.childKey)).toEqual(["c"]);
1694
+ expect(second.cursor).toBeUndefined();
1695
+ })));
1696
+ it.effect("remove refuses a waiting-children parent and deletes rows with a settled one", () => withStore((store) => Effect.gen(function* () {
1697
+ const flowId = yield* fanOutParent(store, { children: ["a"] });
1698
+ expect(yield* store.remove(flowId)).toBe(false);
1699
+ yield* recordOne(store, report(flowId, "a", "failed", { exit: undefined }));
1700
+ // continue-policy: the parent settled to waiting; cancel it so it
1701
+ // is removable, then remove it — the dependency rows go with it.
1702
+ yield* store.cancel(flowId);
1703
+ expect(yield* store.remove(flowId)).toBe(true);
1704
+ expect((yield* store.listChildResults(flowId)).items).toEqual([]);
1705
+ })));
1706
+ it.effect("store-side child failures carry failedReason on the row", () => withStore((store) => Effect.gen(function* () {
1707
+ const flowId = yield* fanOutParent(store, { children: ["a"] });
1708
+ yield* recordOne(store, report(flowId, "a", "failed", {
1709
+ exit: undefined,
1710
+ failedReason: "job stalled more than allowable limit"
1711
+ }));
1712
+ const rows = yield* store.listChildResults(flowId);
1713
+ expect(rows.items[0]?.exit).toBeUndefined();
1714
+ expect(rows.items[0]?.failedReason).toBe("job stalled more than allowable limit");
1715
+ })));
1716
+ // ----------------------------------------------------------------------
1717
+ // Batched reports + the child-side outbox. The outbox is how a CHILD
1718
+ // store reports terminal transitions to a parent living in another
1719
+ // store: append on the transition, peek/delete from the relay.
1720
+ // ----------------------------------------------------------------------
1721
+ it.effect("recordChildResults applies a batch positionally and keeps counters exact", () => withStore((store) => Effect.gen(function* () {
1722
+ const flowId = yield* fanOutParent(store, { children: ["a", "b", "c"] });
1723
+ const results = yield* store.recordChildResults([
1724
+ report(flowId, "a", "completed"),
1725
+ report(flowId, "a", "completed"), // duplicate inside the batch
1726
+ report(flowId, "ghost", "completed"),
1727
+ report(flowId, "b", "failed")
1728
+ ]);
1729
+ expect(results).toEqual([
1730
+ { applied: true, parentSettled: false },
1731
+ { applied: false, parentSettled: false },
1732
+ { applied: false, parentSettled: false },
1733
+ { applied: true, parentSettled: false }
1734
+ ]);
1735
+ const parent = yield* store.getJob(flowId);
1736
+ assert(Option.isSome(parent));
1737
+ expect(parent.value.state).toBe("waiting-children");
1738
+ expect(parent.value.flow).toEqual(flowCounts({ pending: 1, completed: 1, failed: 1 }));
1739
+ })));
1740
+ it.effect("a batch that empties pending settles once, on its last applied report", () => withStore((store) => Effect.gen(function* () {
1741
+ const flowId = yield* fanOutParent(store);
1742
+ const results = yield* store.recordChildResults([
1743
+ report(flowId, "a", "completed"),
1744
+ report(flowId, "b", "completed")
1745
+ ]);
1746
+ expect(results).toEqual([
1747
+ { applied: true, parentSettled: false },
1748
+ { applied: true, parentSettled: true }
1749
+ ]);
1750
+ const parent = yield* store.getJob(flowId);
1751
+ assert(Option.isSome(parent));
1752
+ expect(parent.value.state).toBe("waiting");
1753
+ })));
1754
+ it.effect("fail-fast wins inside a batch, after every batch-mate applied", () => withStore((store) => Effect.gen(function* () {
1755
+ const flowId = yield* fanOutParent(store, { children: ["a", "b", "c"], failFast: true });
1756
+ const results = yield* store.recordChildResults([
1757
+ report(flowId, "b", "failed"),
1758
+ report(flowId, "c", "completed")
1759
+ ]);
1760
+ // Row updates apply BEFORE the settle decision: "c" keeps its real
1761
+ // completed outcome even though "b" settles the flow.
1762
+ expect(results).toEqual([
1763
+ { applied: true, parentSettled: true },
1764
+ { applied: true, parentSettled: false }
1765
+ ]);
1766
+ const parent = yield* store.getJob(flowId);
1767
+ assert(Option.isSome(parent));
1768
+ expect(parent.value.state).toBe("failed");
1769
+ expect(parent.value.failedReason).toContain("b");
1770
+ expect(parent.value.flow).toEqual(flowCounts({ failFast: true, completed: 1, failed: 1, cancelled: 1 }));
1771
+ const rows = yield* store.listChildResults(flowId);
1772
+ const byKey = new Map(rows.items.map((row) => [row.childKey, row.status]));
1773
+ expect(byKey.get("c")).toBe("completed");
1774
+ expect(byKey.get("a")).toBe("cancelled");
1775
+ })));
1776
+ it.effect("a batch may span flows and settles each independently", () => withStore((store) => Effect.gen(function* () {
1777
+ const first = yield* fanOutParent(store, { children: ["a"] });
1778
+ const second = yield* fanOutParent(store, { children: ["b"] });
1779
+ const results = yield* store.recordChildResults([
1780
+ report(first, "a", "completed"),
1781
+ report(second, "b", "completed")
1782
+ ]);
1783
+ expect(results).toEqual([
1784
+ { applied: true, parentSettled: true },
1785
+ { applied: true, parentSettled: true }
1786
+ ]);
1787
+ })));
1788
+ it.effect("a cancelled child report moves the cancelled counter", () => withStore((store) => Effect.gen(function* () {
1789
+ const flowId = yield* fanOutParent(store);
1790
+ yield* recordOne(store, report(flowId, "a", "cancelled", { exit: undefined }));
1791
+ const parent = yield* store.getJob(flowId);
1792
+ assert(Option.isSome(parent));
1793
+ expect(parent.value.flow).toEqual(flowCounts({ pending: 1, cancelled: 1 }));
1794
+ })));
1795
+ it.effect("peekOutbox pages past prior entries with `after`, even deleted ones", () => withStore((store) => Effect.gen(function* () {
1796
+ const enqueueChild = (key) => Effect.gen(function* () {
1797
+ const { id } = yield* store.enqueue(baseRequest({
1798
+ parent: parentEnvelope(JobId("remote-flow-3"), key)
1799
+ }));
1800
+ const claim = yield* store.claim(claimOptions({ token: `t-${key}` }));
1801
+ assert(claim._tag === "Claimed");
1802
+ yield* store.ack(id, `t-${key}`, { _tag: "Complete", exit: { key } });
1803
+ });
1804
+ yield* enqueueChild("one");
1805
+ yield* enqueueChild("two");
1806
+ yield* enqueueChild("three");
1807
+ const first = yield* store.peekOutbox({ limit: 2 });
1808
+ expect(first.map((entry) => entry.report.childKey)).toEqual(["one", "two"]);
1809
+ const cursor = first[first.length - 1]?.id;
1810
+ assert(cursor !== undefined);
1811
+ const rest = yield* store.peekOutbox({ limit: 2, after: cursor });
1812
+ expect(rest.map((entry) => entry.report.childKey)).toEqual(["three"]);
1813
+ // The cursor keeps working when the entry it names is gone.
1814
+ yield* store.deleteOutbox([cursor]);
1815
+ const restAgain = yield* store.peekOutbox({ limit: 2, after: cursor });
1816
+ expect(restAgain.map((entry) => entry.report.childKey)).toEqual(["three"]);
1817
+ })));
1818
+ it.effect("cancels honoured by retry acks, the stall sweep, and on a parked parent land in the outbox", () => withStore((store) => Effect.gen(function* () {
1819
+ const envelope = (key) => parentEnvelope(JobId("remote-flow-4"), key);
1820
+ // A cancel honoured when a RETRY ack finds the flag set.
1821
+ const retried = yield* store.enqueue(baseRequest({ parent: envelope("retry-cancel") }));
1822
+ const claimA = yield* store.claim(claimOptions({ token: "t-a" }));
1823
+ assert(claimA._tag === "Claimed");
1824
+ yield* store.cancel(retried.id);
1825
+ yield* store.ack(retried.id, "t-a", { _tag: "Retry", delayMs: 0, exit: undefined });
1826
+ // A cancel honoured when the stall sweep recovers a dead worker.
1827
+ const stalled = yield* store.enqueue(baseRequest({ parent: envelope("stall-cancel") }));
1828
+ const claimB = yield* store.claim(claimOptions({ token: "t-b", lockDurationMs: 1_000 }));
1829
+ assert(claimB._tag === "Claimed");
1830
+ yield* store.cancel(stalled.id);
1831
+ yield* TestClock.adjust(2_000);
1832
+ yield* store.recoverStalled({ maxStalledCount: 5 });
1833
+ // A direct cancel of a PARKED nested parent (waiting-children).
1834
+ const parked = yield* store.enqueue(baseRequest({ parent: envelope("parked-cancel") }));
1835
+ const claimC = yield* store.claim(claimOptions({ token: "t-c" }));
1836
+ assert(claimC._tag === "Claimed");
1837
+ yield* store.ack(parked.id, "t-c", {
1838
+ _tag: "FanOut",
1839
+ failFast: false,
1840
+ children: [childSpec(parked.id, "a")]
1841
+ });
1842
+ yield* store.cancel(parked.id);
1843
+ const entries = yield* store.peekOutbox({ limit: 10 });
1844
+ expect(entries.map((entry) => [entry.report.childKey, entry.report.outcome])).toEqual([
1845
+ ["retry-cancel", "cancelled"],
1846
+ ["stall-cancel", "cancelled"],
1847
+ ["parked-cancel", "cancelled"]
1848
+ ]);
1849
+ })));
1850
+ it.effect("terminal transitions of envelope-carrying jobs land in the outbox", () => withStore((store) => Effect.gen(function* () {
1851
+ const envelope = (key) => parentEnvelope(JobId("remote-flow-1"), key);
1852
+ // A plain job's terminal ack appends nothing.
1853
+ const plain = yield* store.enqueue(baseRequest());
1854
+ const plainClaim = yield* store.claim(claimOptions({ token: "t-plain" }));
1855
+ assert(plainClaim._tag === "Claimed");
1856
+ yield* store.ack(plain.id, "t-plain", { _tag: "Complete", exit: { ok: true } });
1857
+ expect(yield* store.peekOutbox({ limit: 10 })).toEqual([]);
1858
+ // Ack Complete → outbox entry with the exit.
1859
+ const acked = yield* store.enqueue(baseRequest({ parent: envelope("acked") }));
1860
+ const claim = yield* store.claim(claimOptions({ token: "t-child" }));
1861
+ assert(claim._tag === "Claimed");
1862
+ expect(claim.job.id).toBe(acked.id);
1863
+ yield* store.ack(acked.id, "t-child", { _tag: "Complete", exit: { sent: 1 } });
1864
+ // Direct cancel of a delayed child → outbox entry.
1865
+ const cancelled = yield* store.enqueue(baseRequest({ parent: envelope("cancelled"), delayMs: 60_000 }));
1866
+ yield* store.cancel(cancelled.id);
1867
+ // Stall exhaustion → outbox entry carrying the failedReason.
1868
+ yield* store.enqueue(baseRequest({ parent: envelope("stalled") }));
1869
+ const stalledClaim = yield* store.claim(claimOptions({ token: "t-stall", lockDurationMs: 1_000 }));
1870
+ assert(stalledClaim._tag === "Claimed");
1871
+ yield* TestClock.adjust(2_000);
1872
+ const recovered = yield* store.recoverStalled({ maxStalledCount: 0 });
1873
+ expect(recovered).toEqual([{ id: stalledClaim.job.id, failed: true }]);
1874
+ // Oldest first, `limit` respected, full entry shape.
1875
+ const firstPage = yield* store.peekOutbox({ limit: 2 });
1876
+ expect(firstPage.map((entry) => entry.report.childKey)).toEqual(["acked", "cancelled"]);
1877
+ const head = firstPage[0];
1878
+ assert(head !== undefined);
1879
+ expect(head.flowName).toBe("test-flow");
1880
+ expect(head.parentStoreKey).toBe("main");
1881
+ expect(head.report.flowId).toBe("remote-flow-1");
1882
+ expect(head.report.outcome).toBe("completed");
1883
+ expect(head.report.exit).toEqual({ sent: 1 });
1884
+ const all = yield* store.peekOutbox({ limit: 10 });
1885
+ expect(all.map((entry) => entry.report.outcome)).toEqual([
1886
+ "completed",
1887
+ "cancelled",
1888
+ "failed"
1889
+ ]);
1890
+ expect(all[2]?.report.exit).toBeUndefined();
1891
+ expect(all[2]?.report.failedReason).toBe("job stalled more than allowable limit");
1892
+ // Peek does not consume; delete does, idempotently.
1893
+ yield* store.deleteOutbox([head.id, "ghost-id"]);
1894
+ const rest = yield* store.peekOutbox({ limit: 10 });
1895
+ expect(rest.map((entry) => entry.report.childKey)).toEqual(["cancelled", "stalled"]);
1896
+ yield* store.deleteOutbox([head.id]);
1897
+ expect((yield* store.peekOutbox({ limit: 10 })).length).toBe(2);
1898
+ })));
1899
+ it.effect("cancels honoured off the ack path still land in the outbox", () => withStore((store) => Effect.gen(function* () {
1900
+ // A cancel that arrives while the child runs, honoured when the
1901
+ // worker RELEASES the job (shutdown) instead of acking it.
1902
+ const { id } = yield* store.enqueue(baseRequest({
1903
+ parent: parentEnvelope(JobId("remote-flow-2"), "released")
1904
+ }));
1905
+ const claim = yield* store.claim(claimOptions({ token: "t-run" }));
1906
+ assert(claim._tag === "Claimed");
1907
+ yield* store.cancel(id);
1908
+ yield* store.release(id, "t-run");
1909
+ const job = yield* store.getJob(id);
1910
+ assert(Option.isSome(job));
1911
+ expect(job.value.state).toBe("cancelled");
1912
+ const entries = yield* store.peekOutbox({ limit: 10 });
1913
+ expect(entries.map((entry) => entry.report.outcome)).toEqual(["cancelled"]);
1914
+ expect(entries[0]?.report.childKey).toBe("released");
1915
+ })));
1916
+ it.effect("a cancel that races a nested parent's fan-out reports upward through the outbox", () => withStore((store) => Effect.gen(function* () {
1917
+ // The parent being fanned out is itself a flow child; the raced
1918
+ // cancel settles it terminally inside the FanOut ack.
1919
+ const inner = yield* store.enqueue(baseRequest({
1920
+ parent: {
1921
+ flowName: "outer-flow",
1922
+ flowId: JobId("outer-2"),
1923
+ childKey: "inner-raced",
1924
+ parentStoreKey: "outer-store",
1925
+ depth: 1
1926
+ }
1927
+ }));
1928
+ const claim = yield* store.claim(claimOptions({ token: "t-race" }));
1929
+ assert(claim._tag === "Claimed");
1930
+ yield* store.cancel(inner.id);
1931
+ yield* store.ack(inner.id, "t-race", {
1932
+ _tag: "FanOut",
1933
+ failFast: false,
1934
+ children: [childSpec(inner.id, "a")]
1935
+ });
1936
+ const parent = yield* store.getJob(inner.id);
1937
+ assert(Option.isSome(parent));
1938
+ expect(parent.value.state).toBe("cancelled");
1939
+ const entries = yield* store.peekOutbox({ limit: 10 });
1940
+ expect(entries.map((entry) => entry.report.outcome)).toEqual(["cancelled"]);
1941
+ expect(entries[0]?.report.childKey).toBe("inner-raced");
1942
+ expect(entries[0]?.flowName).toBe("outer-flow");
1943
+ })));
1944
+ it.effect("a fail-fast settle of a nested parent reports upward through the outbox", () => withStore((store) => Effect.gen(function* () {
1945
+ // The inner parent is itself a flow child; its terminal transition
1946
+ // happens store-side (the settle), with no worker ack to hook.
1947
+ const inner = yield* store.enqueue(baseRequest({
1948
+ parent: {
1949
+ flowName: "outer-flow",
1950
+ flowId: JobId("outer-1"),
1951
+ childKey: "inner",
1952
+ parentStoreKey: "outer-store",
1953
+ depth: 1
1954
+ }
1955
+ }));
1956
+ const claim = yield* store.claim(claimOptions({ token: "t-inner" }));
1957
+ assert(claim._tag === "Claimed");
1958
+ yield* store.ack(inner.id, "t-inner", {
1959
+ _tag: "FanOut",
1960
+ failFast: true,
1961
+ children: [childSpec(inner.id, "a")]
1962
+ });
1963
+ // Parking is not terminal: nothing in the outbox yet.
1964
+ expect(yield* store.peekOutbox({ limit: 10 })).toEqual([]);
1965
+ yield* recordOne(store, report(inner.id, "a", "failed"));
1966
+ const entries = yield* store.peekOutbox({ limit: 10 });
1967
+ expect(entries.length).toBe(1);
1968
+ expect(entries[0]?.flowName).toBe("outer-flow");
1969
+ expect(entries[0]?.report.childKey).toBe("inner");
1970
+ expect(entries[0]?.report.outcome).toBe("failed");
1971
+ expect(entries[0]?.report.failedReason).toContain('"a" failed');
1972
+ })));
1209
1973
  });
1210
1974
  };
1211
1975
  //# sourceMappingURL=conformance.js.map