@indigoai-us/hq-cloud 6.16.50 → 6.16.51-alpha.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 (147) hide show
  1. package/dist/bin/rescue-runner.js +1 -2
  2. package/dist/bin/rescue-runner.js.map +1 -1
  3. package/dist/bin/sync-runner-company.d.ts +6 -0
  4. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  5. package/dist/bin/sync-runner-company.js +30 -1
  6. package/dist/bin/sync-runner-company.js.map +1 -1
  7. package/dist/bin/sync-runner-company.test.js +55 -0
  8. package/dist/bin/sync-runner-company.test.js.map +1 -1
  9. package/dist/bin/sync-runner-planning.d.ts +12 -3
  10. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-planning.js +65 -3
  12. package/dist/bin/sync-runner-planning.js.map +1 -1
  13. package/dist/bin/sync-runner-planning.test.js +108 -1
  14. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  15. package/dist/bin/sync-runner-watch-loop.d.ts +48 -2
  16. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-watch-loop.js +281 -113
  18. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  19. package/dist/bin/sync-runner.d.ts +33 -5
  20. package/dist/bin/sync-runner.d.ts.map +1 -1
  21. package/dist/bin/sync-runner.js +34 -4
  22. package/dist/bin/sync-runner.js.map +1 -1
  23. package/dist/bin/sync-runner.test.js +555 -78
  24. package/dist/bin/sync-runner.test.js.map +1 -1
  25. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +2 -0
  26. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +1 -0
  27. package/dist/cli/onepass-pull-planner-regressions.test.js +300 -0
  28. package/dist/cli/onepass-pull-planner-regressions.test.js.map +1 -0
  29. package/dist/cli/onepass-pull-planner.test.d.ts +2 -0
  30. package/dist/cli/onepass-pull-planner.test.d.ts.map +1 -0
  31. package/dist/cli/onepass-pull-planner.test.js +266 -0
  32. package/dist/cli/onepass-pull-planner.test.js.map +1 -0
  33. package/dist/cli/rescue-core.d.ts +0 -31
  34. package/dist/cli/rescue-core.d.ts.map +1 -1
  35. package/dist/cli/rescue-core.js +63 -596
  36. package/dist/cli/rescue-core.js.map +1 -1
  37. package/dist/cli/rescue-rsync-preflight.test.js +4 -256
  38. package/dist/cli/rescue-rsync-preflight.test.js.map +1 -1
  39. package/dist/cli/rescue.d.ts +0 -4
  40. package/dist/cli/rescue.d.ts.map +1 -1
  41. package/dist/cli/rescue.js +1 -20
  42. package/dist/cli/rescue.js.map +1 -1
  43. package/dist/cli/rescue.test.js +1 -16
  44. package/dist/cli/rescue.test.js.map +1 -1
  45. package/dist/cli/share-presign-transport.test.js +0 -2
  46. package/dist/cli/share-presign-transport.test.js.map +1 -1
  47. package/dist/cli/share.d.ts +19 -0
  48. package/dist/cli/share.d.ts.map +1 -1
  49. package/dist/cli/share.js +57 -37
  50. package/dist/cli/share.js.map +1 -1
  51. package/dist/cli/share.test.js +87 -93
  52. package/dist/cli/share.test.js.map +1 -1
  53. package/dist/cli/streaming-pull-planner.bench.test.d.ts +15 -0
  54. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +1 -0
  55. package/dist/cli/streaming-pull-planner.bench.test.js +273 -0
  56. package/dist/cli/streaming-pull-planner.bench.test.js.map +1 -0
  57. package/dist/cli/sync-scope.test.js +8 -0
  58. package/dist/cli/sync-scope.test.js.map +1 -1
  59. package/dist/cli/sync.d.ts +12 -0
  60. package/dist/cli/sync.d.ts.map +1 -1
  61. package/dist/cli/sync.js +226 -118
  62. package/dist/cli/sync.js.map +1 -1
  63. package/dist/cli/sync.test.js +215 -2
  64. package/dist/cli/sync.test.js.map +1 -1
  65. package/dist/cli/watch-event-push-conflict.test.js +0 -2
  66. package/dist/cli/watch-event-push-conflict.test.js.map +1 -1
  67. package/dist/cognito-auth.d.ts +0 -30
  68. package/dist/cognito-auth.d.ts.map +1 -1
  69. package/dist/cognito-auth.js +0 -175
  70. package/dist/cognito-auth.js.map +1 -1
  71. package/dist/index.d.ts +2 -2
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +1 -1
  74. package/dist/index.js.map +1 -1
  75. package/dist/journal-store.test.js +130 -7
  76. package/dist/journal-store.test.js.map +1 -1
  77. package/dist/journal.d.ts +9 -1
  78. package/dist/journal.d.ts.map +1 -1
  79. package/dist/journal.js +30 -21
  80. package/dist/journal.js.map +1 -1
  81. package/dist/lib/exit-codes.d.ts +15 -0
  82. package/dist/lib/exit-codes.d.ts.map +1 -1
  83. package/dist/lib/exit-codes.js +15 -0
  84. package/dist/lib/exit-codes.js.map +1 -1
  85. package/dist/machine-auth.test.js +0 -295
  86. package/dist/machine-auth.test.js.map +1 -1
  87. package/dist/object-io.d.ts +2 -2
  88. package/dist/object-io.d.ts.map +1 -1
  89. package/dist/object-io.js +3 -18
  90. package/dist/object-io.js.map +1 -1
  91. package/dist/personal-vault.d.ts.map +1 -1
  92. package/dist/personal-vault.js +10 -3
  93. package/dist/personal-vault.js.map +1 -1
  94. package/dist/personal-vault.test.js +35 -0
  95. package/dist/personal-vault.test.js.map +1 -1
  96. package/dist/remote-pull.d.ts.map +1 -1
  97. package/dist/remote-pull.js +9 -2
  98. package/dist/remote-pull.js.map +1 -1
  99. package/dist/remote-pull.test.js +59 -2
  100. package/dist/remote-pull.test.js.map +1 -1
  101. package/dist/s3.d.ts +1 -18
  102. package/dist/s3.d.ts.map +1 -1
  103. package/dist/s3.js +24 -47
  104. package/dist/s3.js.map +1 -1
  105. package/dist/s3.list-cost.test.d.ts +2 -0
  106. package/dist/s3.list-cost.test.d.ts.map +1 -0
  107. package/dist/s3.list-cost.test.js +51 -0
  108. package/dist/s3.list-cost.test.js.map +1 -0
  109. package/dist/s3.test.js +1 -27
  110. package/dist/s3.test.js.map +1 -1
  111. package/dist/sync/realtime-telemetry.d.ts +1 -1
  112. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  113. package/dist/sync/realtime-telemetry.js +14 -6
  114. package/dist/sync/realtime-telemetry.js.map +1 -1
  115. package/dist/sync/realtime-telemetry.test.js +136 -16
  116. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  117. package/dist/sync/remote-file-spool.d.ts +16 -0
  118. package/dist/sync/remote-file-spool.d.ts.map +1 -0
  119. package/dist/sync/remote-file-spool.js +91 -0
  120. package/dist/sync/remote-file-spool.js.map +1 -0
  121. package/dist/sync/remote-file-spool.test.d.ts +2 -0
  122. package/dist/sync/remote-file-spool.test.d.ts.map +1 -0
  123. package/dist/sync/remote-file-spool.test.js +77 -0
  124. package/dist/sync/remote-file-spool.test.js.map +1 -0
  125. package/dist/sync-core.d.ts +4 -6
  126. package/dist/sync-core.d.ts.map +1 -1
  127. package/dist/sync-core.js +4 -10
  128. package/dist/sync-core.js.map +1 -1
  129. package/dist/sync-core.test.js +1 -25
  130. package/dist/sync-core.test.js.map +1 -1
  131. package/package.json +2 -1
  132. package/dist/cli/rescue-breaker.test.d.ts +0 -2
  133. package/dist/cli/rescue-breaker.test.d.ts.map +0 -1
  134. package/dist/cli/rescue-breaker.test.js +0 -334
  135. package/dist/cli/rescue-breaker.test.js.map +0 -1
  136. package/dist/cli/rescue-realpath.test.d.ts +0 -2
  137. package/dist/cli/rescue-realpath.test.d.ts.map +0 -1
  138. package/dist/cli/rescue-realpath.test.js +0 -109
  139. package/dist/cli/rescue-realpath.test.js.map +0 -1
  140. package/dist/cli/rescue-snapshot-recovery.test.d.ts +0 -2
  141. package/dist/cli/rescue-snapshot-recovery.test.d.ts.map +0 -1
  142. package/dist/cli/rescue-snapshot-recovery.test.js +0 -260
  143. package/dist/cli/rescue-snapshot-recovery.test.js.map +0 -1
  144. package/dist/cli/rescue-source-switch-floor.test.d.ts +0 -2
  145. package/dist/cli/rescue-source-switch-floor.test.d.ts.map +0 -1
  146. package/dist/cli/rescue-source-switch-floor.test.js +0 -210
  147. package/dist/cli/rescue-source-switch-floor.test.js.map +0 -1
@@ -17,7 +17,7 @@ import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
17
17
  import { setBandwidthGovernorForTesting } from "../bandwidth.js";
18
18
  import { POST_SYNC_FLAG_KEYS, } from "./sync-runner-post-sync-flags.js";
19
19
  import { FakeClock, TreeWatcher, } from "../watcher.js";
20
- import { adaptivePollMs, advanceMonotonicDeadline, DEFAULT_EVENT_BATCH_LIMIT, DEFAULT_FAST_LANE_MAX_BYTES, DEFAULT_FAST_LANE_MAX_FILE_BYTES, DEFAULT_FAST_LANE_MAX_FILES, DEFAULT_FULL_RECONCILE_MS, EVENT_BATCH_LIMIT_ENV, FAST_LANE_MAX_BYTES_ENV, FAST_LANE_MAX_FILES_ENV, FULL_RECONCILE_MS_ENV, isFastLocalBatch, isFastReceiverBatch, partitionLocalBatch, journalEntriesForWatcherDelete, resolveEventBatchLimit, resolveFullReconcileMs, resolveWatchPollMs, normalizeCoordinatorPaths, shouldQuarantineWatcherDeleteIntents, shouldUseAreaStorage, WATCHER_REDISCOVERY_RETRY_MS, WATCHER_REDISCOVERY_RETRY_MAX_MS, } from "./sync-runner-watch-loop.js";
20
+ import { adaptivePollMs, advanceMonotonicDeadline, DEFAULT_EVENT_BATCH_LIMIT, DEFAULT_FAST_LANE_MAX_BYTES, DEFAULT_FAST_LANE_MAX_FILE_BYTES, DEFAULT_FAST_LANE_MAX_FILES, DEFAULT_FULL_RECONCILE_MS, EVENT_BATCH_LIMIT_ENV, FAST_LANE_MAX_BYTES_ENV, FAST_LANE_MAX_FILES_ENV, FULL_RECONCILE_MS_ENV, isFastLocalBatch, isFastReceiverBatch, partitionLocalBatch, journalEntriesForWatcherDelete, resolveEventBatchLimit, resolveFullReconcileMs, resolveWatchPollMs, CHORE_STARVATION_CAP_MS, CooperativePassScheduler, normalizeCoordinatorPaths, shouldQuarantineWatcherDeleteIntents, shouldUseAreaStorage, WATCHER_REDISCOVERY_RETRY_MS, WATCHER_REDISCOVERY_RETRY_MAX_MS, } from "./sync-runner-watch-loop.js";
21
21
  import { AreaLedgerMigration, JournalStore, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, writeJournal, } from "../journal.js";
22
22
  import { AreaResolver } from "../sync/area-resolver.js";
23
23
  import { AreaMaintenanceCursorStore } from "../sync/per-area-maintenance.js";
@@ -25,9 +25,10 @@ import { HQ_CLOUD_VERSION } from "../version.js";
25
25
  import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
26
26
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
27
27
  import { getStateStoreSnapshotRecoveryCountForTest, resetStateStoreSnapshotRecoveryCountForTest, StateStoreLockError, } from "../sync/state-store.js";
28
- import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
28
+ import { PARTIAL_SYNC_EXIT, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
29
29
  import { isCoveredByAny } from "../prefix-coalesce.js";
30
30
  import { VaultAuthError, VaultNotFoundError } from "../vault-client.js";
31
+ import { buildRoutePullArgv } from "./sync-runner-watch-routes.js";
31
32
  import { CognitoAuthError, CognitoRefreshError } from "../cognito-auth.js";
32
33
  import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
33
34
  import yaml from "js-yaml";
@@ -162,6 +163,7 @@ function makeVaultStub(opts = {}) {
162
163
  bucketName: `bucket-${uid}`,
163
164
  status: "active",
164
165
  })),
166
+ findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
165
167
  listByType: opts.listPersons ??
166
168
  (() => Promise.resolve([])),
167
169
  },
@@ -954,6 +956,164 @@ describe("target resolution", () => {
954
956
  const call = deps.sync.mock.calls[0][0];
955
957
  expect(call.company).toBe("cmp_abc");
956
958
  });
959
+ it("returns the unresolved-company exit for an empty single-company plan instead of emitting a clean all-complete", async () => {
960
+ const deps = makeDeps({
961
+ createVaultClient: () => makeVaultStub({
962
+ entityGet: () => Promise.resolve(undefined),
963
+ }),
964
+ });
965
+ expect(await runRunner(["--company", "cmp_missing"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
966
+ expect(deps.stderr.events()).toContainEqual({
967
+ type: "error",
968
+ company: "cmp_missing",
969
+ path: "(company-resolution)",
970
+ message: "company entity lookup returned no entity",
971
+ });
972
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
973
+ expect(deps.sync).not.toHaveBeenCalled();
974
+ });
975
+ it("returns the unresolved-company exit when a cmp-prefixed route gets a 404", async () => {
976
+ const client = makeVaultStub({
977
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
978
+ });
979
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
980
+ client.entity.findInMyNamespace = findInMyNamespace;
981
+ const deps = makeDeps({
982
+ createVaultClient: () => client,
983
+ });
984
+ expect(await runRunner(["--company", "cmp_ghost"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
985
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
986
+ expect(deps.stderr.events()).toContainEqual({
987
+ type: "error",
988
+ company: "cmp_ghost",
989
+ path: "(company-resolution)",
990
+ message: "company entity lookup returned 404",
991
+ });
992
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
993
+ });
994
+ it("reports an intentional skipCompanies skip without treating it as a failed pass", async () => {
995
+ const previous = process.env.HQ_SYNC_SKIP_COMPANIES;
996
+ process.env.HQ_SYNC_SKIP_COMPANIES = "paused";
997
+ try {
998
+ const deps = makeDeps({
999
+ createVaultClient: () => makeVaultStub({
1000
+ entityGet: (uid) => Promise.resolve({
1001
+ uid,
1002
+ slug: "paused",
1003
+ type: "company",
1004
+ status: "active",
1005
+ }),
1006
+ }),
1007
+ });
1008
+ expect(await runRunner(["--company", "cmp_paused"], deps)).toBe(0);
1009
+ expect(deps.stdout.events()).toContainEqual({
1010
+ type: "company-skipped",
1011
+ company: "paused",
1012
+ reason: "skip-companies",
1013
+ });
1014
+ expect(deps.stderr.events().some((event) => event.type === "error")).toBe(false);
1015
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
1016
+ expect(deps.sync).not.toHaveBeenCalled();
1017
+ }
1018
+ finally {
1019
+ if (previous === undefined)
1020
+ delete process.env.HQ_SYNC_SKIP_COMPANIES;
1021
+ else
1022
+ process.env.HQ_SYNC_SKIP_COMPANIES = previous;
1023
+ }
1024
+ });
1025
+ it("resolves a slug emitted by the company watch route to its namespace UID", async () => {
1026
+ const company = {
1027
+ uid: "cmp_indigo",
1028
+ slug: "indigo",
1029
+ type: "company",
1030
+ status: "active",
1031
+ bucketName: "bucket-cmp_indigo",
1032
+ };
1033
+ const client = makeVaultStub({
1034
+ entityGet: (uid) => Promise.resolve(uid === company.uid ? company : undefined),
1035
+ });
1036
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1037
+ client.entity.findInMyNamespace = findInMyNamespace;
1038
+ const deps = makeDeps({ createVaultClient: () => client });
1039
+ const argv = buildRoutePullArgv({ kind: "company", slug: "indigo" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1040
+ expect(argv).toEqual([
1041
+ "--company",
1042
+ "indigo",
1043
+ "--direction",
1044
+ "pull",
1045
+ "--hq-root",
1046
+ "/tmp/hq",
1047
+ ]);
1048
+ expect(await runRunner(argv, deps)).toBe(0);
1049
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1050
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: "cmp_indigo" }));
1051
+ expect(deps.stdout.events()).toContainEqual({
1052
+ type: "fanout-plan",
1053
+ companies: [
1054
+ expect.objectContaining({ uid: "cmp_indigo", slug: "indigo" }),
1055
+ ],
1056
+ });
1057
+ });
1058
+ it("resolves a cmp-prefixed directory slug in the caller namespace before treating it as a UID", async () => {
1059
+ const company = {
1060
+ uid: "cmp_01JTEAM000000000000000000",
1061
+ slug: "cmp_team",
1062
+ type: "company",
1063
+ status: "active",
1064
+ bucketName: "bucket-cmp-team",
1065
+ };
1066
+ const get = vi.fn().mockImplementation((uid) => Promise.resolve(uid === company.uid ? company : undefined));
1067
+ const client = makeVaultStub({ entityGet: get });
1068
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1069
+ client.entity.findInMyNamespace = findInMyNamespace;
1070
+ const deps = makeDeps({ createVaultClient: () => client });
1071
+ const argv = buildRoutePullArgv({ kind: "company", slug: "cmp_team" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1072
+ expect(await runRunner(argv, deps)).toBe(0);
1073
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_team");
1074
+ expect(get).toHaveBeenCalledWith(company.uid);
1075
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: company.uid }));
1076
+ expect(deps.stdout.events()).toContainEqual({
1077
+ type: "fanout-plan",
1078
+ companies: [expect.objectContaining({ uid: company.uid, slug: "cmp_team" })],
1079
+ });
1080
+ });
1081
+ it("falls back to a literal company UID after caller-namespace slug resolution misses", async () => {
1082
+ const uid = "cmp_01JUID0000000000000000000";
1083
+ const company = {
1084
+ uid,
1085
+ slug: "operator-company",
1086
+ type: "company",
1087
+ status: "active",
1088
+ bucketName: "bucket-operator-company",
1089
+ };
1090
+ const get = vi.fn().mockResolvedValue(company);
1091
+ const client = makeVaultStub({ entityGet: get });
1092
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1093
+ client.entity.findInMyNamespace = findInMyNamespace;
1094
+ const deps = makeDeps({ createVaultClient: () => client });
1095
+ expect(await runRunner(["--company", uid], deps)).toBe(0);
1096
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", uid);
1097
+ expect(get).toHaveBeenCalledWith(uid);
1098
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: uid }));
1099
+ });
1100
+ it("reports a foreign company slug when the literal UID fallback finds nothing", async () => {
1101
+ const get = vi.fn().mockRejectedValue(new VaultNotFoundError("company not found"));
1102
+ const client = makeVaultStub({ entityGet: get });
1103
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1104
+ client.entity.findInMyNamespace = findInMyNamespace;
1105
+ const deps = makeDeps({ createVaultClient: () => client });
1106
+ expect(await runRunner(["--company", "indigo"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
1107
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1108
+ expect(get).toHaveBeenCalledWith("indigo");
1109
+ expect(deps.sync).not.toHaveBeenCalled();
1110
+ expect(deps.stderr.events()).toContainEqual({
1111
+ type: "error",
1112
+ company: "indigo",
1113
+ path: "(company-resolution)",
1114
+ message: "company entity lookup returned 404",
1115
+ });
1116
+ });
957
1117
  });
958
1118
  // ---------------------------------------------------------------------------
959
1119
  // --personal mode (personal-vault-only, skip company fanout)
@@ -5586,7 +5746,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5586
5746
  });
5587
5747
  await expect(superseded).resolves.toBeUndefined();
5588
5748
  expect(runPass.mock.calls.filter(([argv]) => argv.includes("superseded.md"))).toHaveLength(0);
5589
- expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":1,"source":"receiver","direction":"pull","outcome":"refused"');
5749
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":2,"source":"receiver","direction":"pull","outcome":"refused"');
5590
5750
  releaseHeld();
5591
5751
  await heldReceiverWork;
5592
5752
  await replacement;
@@ -5899,7 +6059,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5899
6059
  triggerShutdown();
5900
6060
  await loop;
5901
6061
  });
5902
- it("fast lane: a small watcher batch dispatches during a slow bulk pass", async () => {
6062
+ it("fast lane: a small watcher batch waits for a full pass that has not reached a hand-off checkpoint", async () => {
5903
6063
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
5904
6064
  const relativePath = "companies/indigo/realtime.md";
5905
6065
  const absolutePath = path.join(hqRoot, relativePath);
@@ -5939,15 +6099,17 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5939
6099
  expect(order).toEqual(["bulk"]);
5940
6100
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5941
6101
  await flushLoopMicrotasks(30);
5942
- // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
5943
- expect(order).toEqual(["bulk", "fast", "fast"]);
6102
+ // A full pass that has not reached its durable checkpoint remains the sole
6103
+ // owner. Realtime is promoted at the checkpoint, never run concurrently.
6104
+ expect(order).toEqual(["bulk"]);
5944
6105
  releaseBulk();
5945
- await flushLoopMicrotasks();
6106
+ await flushLoopMicrotasks(30);
6107
+ expect(order).toEqual(["bulk", "fast", "fast"]);
5946
6108
  triggerShutdown();
5947
6109
  await loop;
5948
6110
  fs.rmSync(hqRoot, { recursive: true, force: true });
5949
6111
  });
5950
- it("parallel coordinator: a watcher push starts while a full chore is blocked", async () => {
6112
+ it("parallel coordinator: a watcher push waits for a full chore without a hand-off checkpoint", async () => {
5951
6113
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
5952
6114
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
5953
6115
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -5990,9 +6152,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5990
6152
  expect(order).toEqual(["chore"]);
5991
6153
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5992
6154
  await flushLoopMicrotasks(30);
5993
- expect(order).toEqual(["chore", "realtime"]);
6155
+ expect(order).toEqual(["chore"]);
5994
6156
  releaseBulk();
5995
- await flushLoopMicrotasks();
6157
+ await flushLoopMicrotasks(30);
6158
+ expect(order).toEqual(["chore", "realtime"]);
5996
6159
  triggerShutdown();
5997
6160
  await loop;
5998
6161
  }
@@ -6077,7 +6240,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6077
6240
  fs.rmSync(hqRoot, { recursive: true, force: true });
6078
6241
  }
6079
6242
  });
6080
- it("parallel coordinator partitions receiver events into fast and slow realtime lanes", async () => {
6243
+ it("parallel coordinator partitions receiver events into fast and slow realtime lanes without overlapping their durable writes", async () => {
6081
6244
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6082
6245
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6083
6246
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6148,7 +6311,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6148
6311
  signal: new AbortController().signal,
6149
6312
  });
6150
6313
  await flushLoopMicrotasks(20);
6151
- expect(order).toEqual(["slow-held", "fast-small"]);
6314
+ expect(order).toEqual(["slow-held"]);
6152
6315
  releaseSlow();
6153
6316
  await held;
6154
6317
  await partitioned;
@@ -6489,19 +6652,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6489
6652
  ]),
6490
6653
  });
6491
6654
  await flushLoopMicrotasks(30);
6492
- // origin/main failure: expected a scoped tiny path while the bulk pass is
6493
- // held, but #408 kept all four paths in the slow lane.
6494
- expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(1);
6495
- expect(calls.flat().join(" ")).toContain("realtime.md");
6655
+ // The full pass owns durable state until a checkpoint. Its queued realtime
6656
+ // residue is promoted at that hand-off instead of overlapping the pass.
6657
+ expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(0);
6496
6658
  releaseBulk();
6497
6659
  await flushLoopMicrotasks(30);
6498
6660
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
6661
+ expect(calls.flat().join(" ")).toContain("realtime.md");
6499
6662
  expect(calls.flat().join(" ")).toContain("session-0.log");
6500
6663
  triggerShutdown();
6501
6664
  await loop;
6502
6665
  fs.rmSync(hqRoot, { recursive: true, force: true });
6503
6666
  });
6504
- it("fast lane: a small receiver pull dispatches during a slow bulk pass", async () => {
6667
+ it("fast lane: a small receiver pull waits for a slow full pass without a hand-off checkpoint", async () => {
6505
6668
  const watcher = makeWatcherStub();
6506
6669
  let triggerShutdown = () => { };
6507
6670
  let receiverSync;
@@ -6546,7 +6709,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6546
6709
  watcher.emit();
6547
6710
  await flushLoopMicrotasks();
6548
6711
  expect(order).toEqual(["bulk"]);
6549
- await receiverSync({
6712
+ const receiverPull = receiverSync({
6550
6713
  events: [{
6551
6714
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
6552
6715
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -6554,8 +6717,11 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6554
6717
  }],
6555
6718
  signal: new AbortController().signal,
6556
6719
  });
6557
- expect(order).toEqual(["bulk", "fast-pull"]);
6720
+ await flushLoopMicrotasks(20);
6721
+ expect(order).toEqual(["bulk"]);
6558
6722
  releaseBulk();
6723
+ await receiverPull;
6724
+ expect(order).toEqual(["bulk", "fast-pull"]);
6559
6725
  await flushLoopMicrotasks();
6560
6726
  triggerShutdown();
6561
6727
  await loop;
@@ -6599,12 +6765,14 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6599
6765
  await flushLoopMicrotasks();
6600
6766
  watcher.emit();
6601
6767
  await flushLoopMicrotasks();
6768
+ releaseBulk();
6769
+ // The full chore has no explicit safe checkpoint in this stub, so the
6770
+ // realtime pass begins only after the chore has released ownership.
6602
6771
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6603
6772
  await fastStarted;
6604
- releaseBulk();
6605
6773
  await flushLoopMicrotasks(20);
6606
- // The slow pass has finished, but its fast bypass still mutates. A second
6607
- // sync/rescue must not acquire the root operation lock in this interval.
6774
+ // The realtime pass owns the root lock until its own durable work ends. A
6775
+ // second sync/rescue must not acquire the lock in this interval.
6608
6776
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
6609
6777
  releaseFast();
6610
6778
  await flushLoopMicrotasks(20);
@@ -6779,59 +6947,68 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6779
6947
  return () => { };
6780
6948
  },
6781
6949
  });
6782
- await flushLoopMicrotasks();
6783
- watcher.emit();
6784
- await flushLoopMicrotasks();
6785
- runPass.mockClear();
6786
- const events = [
6787
- ...Array.from({ length: 17 }, (_, index) => ({
6788
- kind: "upsert",
6789
- relativePath: `companies/acme/a-${index}.md`,
6790
- contentHash: "sha256:x",
6791
- mtime: "2026-08-26T00:00:00.000Z",
6792
- originDeviceId: "peer",
6793
- originTenantId: "acme",
6794
- sequenceNumber: index,
6795
- eventTimestamp: "2026-08-26T00:00:00.000Z",
6796
- })),
6797
- ...Array.from({ length: 16 }, (_, index) => ({
6798
- kind: "upsert",
6799
- relativePath: `companies/beta/b-${index}.md`,
6800
- contentHash: "sha256:x",
6801
- mtime: "2026-08-26T00:00:00.000Z",
6802
- originDeviceId: "peer",
6803
- originTenantId: "beta",
6804
- sequenceNumber: index + 17,
6805
- eventTimestamp: "2026-08-26T00:00:00.000Z",
6806
- })),
6807
- ];
6808
- for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
6809
- const journal = readJournal(slug);
6810
- for (let index = 0; index < count; index++) {
6811
- journal.files[`${prefix}-${index}.md`] = {
6812
- hash: "sha256:x",
6813
- size: 16,
6814
- syncedAt: "2026-08-26T00:00:00.000Z",
6815
- direction: "down",
6816
- };
6950
+ try {
6951
+ await flushLoopMicrotasks();
6952
+ watcher.emit();
6953
+ await flushLoopMicrotasks();
6954
+ runPass.mockClear();
6955
+ const events = [
6956
+ ...Array.from({ length: 17 }, (_, index) => ({
6957
+ kind: "upsert",
6958
+ relativePath: `companies/acme/a-${index}.md`,
6959
+ contentHash: "sha256:x",
6960
+ mtime: "2026-08-26T00:00:00.000Z",
6961
+ originDeviceId: "peer",
6962
+ originTenantId: "acme",
6963
+ sequenceNumber: index,
6964
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
6965
+ })),
6966
+ ...Array.from({ length: 16 }, (_, index) => ({
6967
+ kind: "upsert",
6968
+ relativePath: `companies/beta/b-${index}.md`,
6969
+ contentHash: "sha256:x",
6970
+ mtime: "2026-08-26T00:00:00.000Z",
6971
+ originDeviceId: "peer",
6972
+ originTenantId: "beta",
6973
+ sequenceNumber: index + 17,
6974
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
6975
+ })),
6976
+ ];
6977
+ for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
6978
+ const journal = readJournal(slug);
6979
+ for (let index = 0; index < count; index++) {
6980
+ journal.files[`${prefix}-${index}.md`] = {
6981
+ hash: "sha256:x",
6982
+ size: 16,
6983
+ syncedAt: "2026-08-26T00:00:00.000Z",
6984
+ direction: "down",
6985
+ };
6986
+ }
6987
+ writeJournal(slug, journal);
6817
6988
  }
6818
- writeJournal(slug, journal);
6989
+ const receiverDrain = receiverSync({
6990
+ events,
6991
+ signal: new AbortController().signal,
6992
+ });
6993
+ await flushLoopMicrotasks(20);
6994
+ // The running full chore has not yet yielded a durable hand-off, so no
6995
+ // receiver work may overlap it. Once it releases ownership, the
6996
+ // scheduler admits each route's bounded work before the residual slow
6997
+ // beta batch (acme fast, beta fast, then beta slow).
6998
+ expect(runPass).not.toHaveBeenCalled();
6999
+ releaseBulk();
7000
+ await receiverDrain;
7001
+ const scopedCalls = runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"));
7002
+ expect(scopedCalls).toHaveLength(3);
7003
+ expect(scopedCalls.flat().join(" ")).toContain("a-16.md");
7004
+ expect(scopedCalls.flat().join(" ")).toContain("b-14.md");
7005
+ }
7006
+ finally {
7007
+ releaseBulk();
7008
+ triggerShutdown();
7009
+ await loop;
7010
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6819
7011
  }
6820
- const receiverDrain = receiverSync({
6821
- events,
6822
- signal: new AbortController().signal,
6823
- });
6824
- await flushLoopMicrotasks(20);
6825
- // The 33-event mixed delivery peels its first 32 bounded paths into the
6826
- // fast lane (17 acme + 15 beta); the final beta event remains slow.
6827
- expect(runPass).toHaveBeenCalledTimes(2);
6828
- expect(runPass.mock.calls.flat().join(" ")).toContain("a-16.md");
6829
- expect(runPass.mock.calls.flat().join(" ")).toContain("b-14.md");
6830
- releaseBulk();
6831
- await receiverDrain;
6832
- triggerShutdown();
6833
- await loop;
6834
- fs.rmSync(hqRoot, { recursive: true, force: true });
6835
7012
  });
6836
7013
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
6837
7014
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7003,9 +7180,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7003
7180
  await flushLoopMicrotasks();
7004
7181
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7005
7182
  await flushLoopMicrotasks(30);
7006
- expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
7183
+ // The full pass has not reached a durable checkpoint, so it remains the
7184
+ // only journal writer. The scoped realtime pass is admitted immediately
7185
+ // after this safe hand-off, preserving both sets of rows.
7186
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(0);
7007
7187
  releaseBulk();
7008
- await flushLoopMicrotasks();
7188
+ await flushLoopMicrotasks(30);
7009
7189
  const journal = readJournal("indigo").files;
7010
7190
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7011
7191
  for (let index = 0; index < paths.length; index++) {
@@ -7057,10 +7237,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7057
7237
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7058
7238
  }
7059
7239
  await flushLoopMicrotasks(60);
7060
- expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
7240
+ // A full pass without a checkpoint remains the sole mutable-state owner.
7241
+ // The queued realtime batches run immediately after it completes.
7242
+ expect(completed).toEqual([]);
7061
7243
  releaseBulk();
7062
- await flushLoopMicrotasks();
7063
- expect(completed).toContain("bulk");
7244
+ await flushLoopMicrotasks(60);
7245
+ expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7064
7246
  triggerShutdown();
7065
7247
  await loop;
7066
7248
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -7378,6 +7560,55 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7378
7560
  expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
7379
7561
  expect(watcher.disposed).toBe(false);
7380
7562
  });
7563
+ it("keeps the watcher alive for an unresolved company watch route (HQ-SYNC-COMPANY-ROUTE-UID)", async () => {
7564
+ const watcher = makeWatcherStub();
7565
+ const client = makeVaultStub({
7566
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
7567
+ });
7568
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
7569
+ client.entity.findInMyNamespace = findInMyNamespace;
7570
+ const routedPass = makeDeps({ createVaultClient: () => client });
7571
+ const routedArgv = buildRoutePullArgv({ kind: "company", slug: "cmp_ghost" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
7572
+ let attempts = 0;
7573
+ let routedExit;
7574
+ const runPass = vi.fn().mockImplementation(async () => {
7575
+ attempts += 1;
7576
+ if (attempts === 1) {
7577
+ routedExit = await runRunner(routedArgv, routedPass);
7578
+ return routedExit;
7579
+ }
7580
+ // Terminate the otherwise-open loop only after it proves the unresolved
7581
+ // route did not make the watcher exit.
7582
+ return 1;
7583
+ });
7584
+ const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7585
+ runPass,
7586
+ clock: new FakeClock(),
7587
+ createWatcher: () => watcher,
7588
+ sleep: () => Promise.resolve(),
7589
+ onShutdownSignal: () => () => { },
7590
+ });
7591
+ expect(routedArgv).toEqual([
7592
+ "--company",
7593
+ "cmp_ghost",
7594
+ "--direction",
7595
+ "pull",
7596
+ "--hq-root",
7597
+ "/tmp/hq",
7598
+ ]);
7599
+ expect(routedExit).toBe(UNRESOLVED_COMPANY_EXIT);
7600
+ expect(runPass).toHaveBeenCalledTimes(2);
7601
+ expect(code).toBe(1);
7602
+ expect(code).not.toBe(UNRESOLVED_COMPANY_EXIT);
7603
+ expect(watcher.disposed).toBe(false);
7604
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
7605
+ expect(routedPass.stderr.events()).toContainEqual({
7606
+ type: "error",
7607
+ company: "cmp_ghost",
7608
+ path: "(company-resolution)",
7609
+ message: "company entity lookup returned 404",
7610
+ });
7611
+ });
7381
7612
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
7382
7613
  const watcher = makeWatcherStub();
7383
7614
  const dnsError = Object.assign(new Error("getaddrinfo ENOTFOUND vault"), {
@@ -7671,6 +7902,85 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
7671
7902
  });
7672
7903
  });
7673
7904
  describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
7905
+ it("hands a wake realtime drain to the guard before a long-running full chore completes", async () => {
7906
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7907
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
7908
+ const watcher = makeWatcherStub();
7909
+ let triggerShutdown = () => { };
7910
+ let releaseCheckpoint = () => { };
7911
+ const checkpointGate = new Promise((resolve) => { releaseCheckpoint = resolve; });
7912
+ let releaseRealtime = () => { };
7913
+ const realtimeGate = new Promise((resolve) => { releaseRealtime = resolve; });
7914
+ const order = [];
7915
+ let passes = 0;
7916
+ let runRealtime = null;
7917
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7918
+ runPass: vi.fn().mockImplementation(async (_argv, checkpoint) => {
7919
+ passes += 1;
7920
+ if (passes === 2) {
7921
+ order.push("chore");
7922
+ // The fake pass has completed its current durable unit and is now
7923
+ // at the same explicit hand-off seam production uses between
7924
+ // fully-settled fanout targets.
7925
+ await checkpointGate;
7926
+ await checkpoint?.();
7927
+ order.push("chore-complete");
7928
+ }
7929
+ return completedPassOutcome();
7930
+ }),
7931
+ runPassSupportsCooperativeCheckpoint: true,
7932
+ clock: new FakeClock(),
7933
+ createWatcher: () => watcher,
7934
+ sleep: makeSteppableSleep().sleep,
7935
+ onShutdownSignal: (handler) => {
7936
+ triggerShutdown = handler;
7937
+ return () => { };
7938
+ },
7939
+ startRealtimeScheduler: async (opts) => {
7940
+ // This is the same wake path used by the realtime scheduler: it
7941
+ // enters runGuardedTask behind the coordinator worker.
7942
+ runRealtime = () => opts.runGuarded(async () => {
7943
+ order.push("realtime");
7944
+ await realtimeGate;
7945
+ order.push("realtime-complete");
7946
+ });
7947
+ return {
7948
+ signal: () => undefined,
7949
+ checkHighWater: async () => undefined,
7950
+ dispose: async () => undefined,
7951
+ };
7952
+ },
7953
+ });
7954
+ try {
7955
+ await flushLoopMicrotasks(30);
7956
+ watcher.emit();
7957
+ await flushLoopMicrotasks(20);
7958
+ expect(order).toEqual(["chore"]);
7959
+ const realtime = runRealtime();
7960
+ releaseCheckpoint();
7961
+ await flushLoopMicrotasks(30);
7962
+ expect(order).toEqual(["chore", "realtime"]);
7963
+ releaseRealtime();
7964
+ await realtime;
7965
+ await flushLoopMicrotasks(20);
7966
+ expect(order).toEqual([
7967
+ "chore",
7968
+ "realtime",
7969
+ "realtime-complete",
7970
+ "chore-complete",
7971
+ ]);
7972
+ }
7973
+ finally {
7974
+ releaseCheckpoint();
7975
+ releaseRealtime();
7976
+ triggerShutdown();
7977
+ await loop;
7978
+ if (previousCoordinator === undefined)
7979
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7980
+ else
7981
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
7982
+ }
7983
+ });
7674
7984
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
7675
7985
  const watcher = makeWatcherStub();
7676
7986
  let triggerShutdown = () => { };
@@ -7783,6 +8093,173 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
7783
8093
  await loop;
7784
8094
  });
7785
8095
  });
8096
+ describe("CooperativePassScheduler", () => {
8097
+ it("admits an explicitly safe fast realtime pass while a slow scoped realtime pass remains in flight", async () => {
8098
+ const scheduler = new CooperativePassScheduler(() => 0);
8099
+ const order = [];
8100
+ let releaseSlow = () => { };
8101
+ const slowGate = new Promise((resolve) => { releaseSlow = resolve; });
8102
+ let markSlowStarted = () => { };
8103
+ const slowStarted = new Promise((resolve) => { markSlowStarted = resolve; });
8104
+ const slow = scheduler.submit(async () => {
8105
+ order.push("slow-start");
8106
+ markSlowStarted();
8107
+ await slowGate;
8108
+ order.push("slow-complete");
8109
+ return 0;
8110
+ }, "realtime", "slow");
8111
+ await slowStarted;
8112
+ const fast = scheduler.submit(async () => {
8113
+ order.push("fast");
8114
+ return 0;
8115
+ }, "realtime", "fast", true);
8116
+ try {
8117
+ await Promise.resolve();
8118
+ await Promise.resolve();
8119
+ expect([...order]).toEqual(["slow-start", "fast"]);
8120
+ }
8121
+ finally {
8122
+ releaseSlow();
8123
+ await Promise.all([slow, fast]);
8124
+ }
8125
+ });
8126
+ it("preserves journal order and resumes full work across repeated randomized hand-offs", async () => {
8127
+ // These deterministic seeds deliberately vary the number and enqueue
8128
+ // ordering of realtime tasks. Every append asserts one durable-state owner
8129
+ // at a time; the full continuation must resume after every realtime burst.
8130
+ for (let seed = 1; seed <= 32; seed += 1) {
8131
+ let now = 0;
8132
+ const scheduler = new CooperativePassScheduler(() => now);
8133
+ const journal = [];
8134
+ const expected = new Set();
8135
+ let writer = null;
8136
+ let maximumWriters = 0;
8137
+ const append = async (entry) => {
8138
+ expect(writer).toBeNull();
8139
+ writer = entry;
8140
+ maximumWriters = Math.max(maximumWriters, 1);
8141
+ // A journal append crosses an async durable-write boundary. If the
8142
+ // scheduler admitted another pass here, its ownership assertion would
8143
+ // fail rather than merely observing JavaScript's synchronous turn.
8144
+ await Promise.resolve();
8145
+ expect(writer).toBe(entry);
8146
+ journal.push(entry);
8147
+ writer = null;
8148
+ };
8149
+ let releaseCheckpoint = () => { };
8150
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8151
+ const chore = scheduler.submit(async ({ checkpoint }) => {
8152
+ expected.add(`chore-${seed}-before`);
8153
+ await append(`chore-${seed}-before`);
8154
+ await checkpointReady;
8155
+ await checkpoint();
8156
+ expected.add(`chore-${seed}-after`);
8157
+ await append(`chore-${seed}-after`);
8158
+ return 0;
8159
+ }, "chore");
8160
+ await Promise.resolve();
8161
+ const realtimeCount = (seed % 7) + 1;
8162
+ const shuffledIndexes = Array.from({ length: realtimeCount }, (_, index) => index);
8163
+ let randomState = seed;
8164
+ for (let index = shuffledIndexes.length - 1; index > 0; index -= 1) {
8165
+ randomState = (randomState * 1_664_525 + 1_013_904_223) >>> 0;
8166
+ const swapIndex = randomState % (index + 1);
8167
+ [shuffledIndexes[index], shuffledIndexes[swapIndex]] = [
8168
+ shuffledIndexes[swapIndex],
8169
+ shuffledIndexes[index],
8170
+ ];
8171
+ }
8172
+ const realtime = shuffledIndexes.map((index) => {
8173
+ const entry = `realtime-${seed}-${(index * 17 + seed) % realtimeCount}`;
8174
+ expected.add(entry);
8175
+ return scheduler.submit(async () => {
8176
+ // Shuffle the microtask timing without ever allowing a second writer.
8177
+ if ((seed + index) % 3 === 0)
8178
+ await Promise.resolve();
8179
+ await append(entry);
8180
+ now += 1;
8181
+ return 0;
8182
+ }, "realtime");
8183
+ });
8184
+ releaseCheckpoint();
8185
+ await Promise.all([chore, ...realtime]);
8186
+ expect(maximumWriters).toBe(1);
8187
+ expect(new Set(journal)).toEqual(expected);
8188
+ expect(journal).toHaveLength(expected.size);
8189
+ expect(journal[0]).toBe(`chore-${seed}-before`);
8190
+ expect(journal.at(-1)).toBe(`chore-${seed}-after`);
8191
+ }
8192
+ });
8193
+ it("lets realtime win at 15 minutes minus one millisecond, then gives the chore the exact-boundary admission", async () => {
8194
+ const runCase = async (elapsedMs) => {
8195
+ let now = 0;
8196
+ const scheduler = new CooperativePassScheduler(() => now);
8197
+ const order = [];
8198
+ let releaseActive = () => { };
8199
+ const activeGate = new Promise((resolve) => { releaseActive = resolve; });
8200
+ const activeRealtime = scheduler.submit(async () => {
8201
+ order.push("active-realtime");
8202
+ await activeGate;
8203
+ return 0;
8204
+ }, "realtime");
8205
+ await Promise.resolve();
8206
+ const chore = scheduler.submit(async () => {
8207
+ order.push("chore");
8208
+ return 0;
8209
+ }, "chore");
8210
+ const queuedRealtime = scheduler.submit(async () => {
8211
+ order.push("queued-realtime");
8212
+ return 0;
8213
+ }, "realtime");
8214
+ now = elapsedMs;
8215
+ releaseActive();
8216
+ await Promise.all([activeRealtime, chore, queuedRealtime]);
8217
+ return order;
8218
+ };
8219
+ await expect(runCase(CHORE_STARVATION_CAP_MS - 1)).resolves.toEqual([
8220
+ "active-realtime",
8221
+ "queued-realtime",
8222
+ "chore",
8223
+ ]);
8224
+ await expect(runCase(CHORE_STARVATION_CAP_MS)).resolves.toEqual([
8225
+ "active-realtime",
8226
+ "chore",
8227
+ "queued-realtime",
8228
+ ]);
8229
+ });
8230
+ it("resumes a handed-off chore when a continuous realtime burst reaches the 15-minute cap", async () => {
8231
+ let now = 0;
8232
+ const scheduler = new CooperativePassScheduler(() => now);
8233
+ const order = [];
8234
+ let releaseCheckpoint = () => { };
8235
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8236
+ const chore = scheduler.submit(async ({ checkpoint }) => {
8237
+ order.push("chore-before");
8238
+ await checkpointReady;
8239
+ await checkpoint();
8240
+ order.push("chore-after");
8241
+ return 0;
8242
+ }, "chore");
8243
+ await Promise.resolve();
8244
+ const firstRealtime = scheduler.submit(async () => {
8245
+ order.push("realtime-before-cap");
8246
+ now = CHORE_STARVATION_CAP_MS;
8247
+ return 0;
8248
+ }, "realtime");
8249
+ const laterRealtime = scheduler.submit(async () => {
8250
+ order.push("realtime-after-cap");
8251
+ return 0;
8252
+ }, "realtime");
8253
+ releaseCheckpoint();
8254
+ await Promise.all([chore, firstRealtime, laterRealtime]);
8255
+ expect(order).toEqual([
8256
+ "chore-before",
8257
+ "realtime-before-cap",
8258
+ "chore-after",
8259
+ "realtime-after-cap",
8260
+ ]);
8261
+ });
8262
+ });
7786
8263
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7787
8264
  const watchArgv = [
7788
8265
  "--companies",