@indigoai-us/hq-cloud 6.16.51-alpha.0 → 6.16.51

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 (157) hide show
  1. package/dist/backup-prune.d.ts +20 -0
  2. package/dist/backup-prune.d.ts.map +1 -1
  3. package/dist/backup-prune.js +62 -3
  4. package/dist/backup-prune.js.map +1 -1
  5. package/dist/backup-prune.test.js +25 -0
  6. package/dist/backup-prune.test.js.map +1 -1
  7. package/dist/bin/rescue-runner.js +2 -1
  8. package/dist/bin/rescue-runner.js.map +1 -1
  9. package/dist/bin/sync-runner-company.d.ts +0 -6
  10. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-company.js +1 -30
  12. package/dist/bin/sync-runner-company.js.map +1 -1
  13. package/dist/bin/sync-runner-company.test.js +0 -55
  14. package/dist/bin/sync-runner-company.test.js.map +1 -1
  15. package/dist/bin/sync-runner-planning.d.ts +3 -12
  16. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-planning.js +3 -65
  18. package/dist/bin/sync-runner-planning.js.map +1 -1
  19. package/dist/bin/sync-runner-planning.test.js +1 -108
  20. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  21. package/dist/bin/sync-runner-watch-loop.d.ts +2 -48
  22. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  23. package/dist/bin/sync-runner-watch-loop.js +113 -281
  24. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  25. package/dist/bin/sync-runner.d.ts +5 -33
  26. package/dist/bin/sync-runner.d.ts.map +1 -1
  27. package/dist/bin/sync-runner.js +4 -34
  28. package/dist/bin/sync-runner.js.map +1 -1
  29. package/dist/bin/sync-runner.test.js +78 -555
  30. package/dist/bin/sync-runner.test.js.map +1 -1
  31. package/dist/cli/rescue-breaker.test.d.ts +2 -0
  32. package/dist/cli/rescue-breaker.test.d.ts.map +1 -0
  33. package/dist/cli/rescue-breaker.test.js +334 -0
  34. package/dist/cli/rescue-breaker.test.js.map +1 -0
  35. package/dist/cli/rescue-core.d.ts +40 -0
  36. package/dist/cli/rescue-core.d.ts.map +1 -1
  37. package/dist/cli/rescue-core.js +1205 -109
  38. package/dist/cli/rescue-core.js.map +1 -1
  39. package/dist/cli/rescue-realpath.test.d.ts +2 -0
  40. package/dist/cli/rescue-realpath.test.d.ts.map +1 -0
  41. package/dist/cli/rescue-realpath.test.js +109 -0
  42. package/dist/cli/rescue-realpath.test.js.map +1 -0
  43. package/dist/cli/rescue-rsync-preflight.test.js +256 -4
  44. package/dist/cli/rescue-rsync-preflight.test.js.map +1 -1
  45. package/dist/cli/rescue-snapshot-copy.test.d.ts +2 -0
  46. package/dist/cli/rescue-snapshot-copy.test.d.ts.map +1 -0
  47. package/dist/cli/rescue-snapshot-copy.test.js +435 -0
  48. package/dist/cli/rescue-snapshot-copy.test.js.map +1 -0
  49. package/dist/cli/rescue-snapshot-recovery.test.d.ts +2 -0
  50. package/dist/cli/rescue-snapshot-recovery.test.d.ts.map +1 -0
  51. package/dist/cli/rescue-snapshot-recovery.test.js +260 -0
  52. package/dist/cli/rescue-snapshot-recovery.test.js.map +1 -0
  53. package/dist/cli/rescue-source-switch-floor.test.d.ts +2 -0
  54. package/dist/cli/rescue-source-switch-floor.test.d.ts.map +1 -0
  55. package/dist/cli/rescue-source-switch-floor.test.js +210 -0
  56. package/dist/cli/rescue-source-switch-floor.test.js.map +1 -0
  57. package/dist/cli/rescue.d.ts +4 -0
  58. package/dist/cli/rescue.d.ts.map +1 -1
  59. package/dist/cli/rescue.js +20 -1
  60. package/dist/cli/rescue.js.map +1 -1
  61. package/dist/cli/rescue.test.js +16 -1
  62. package/dist/cli/rescue.test.js.map +1 -1
  63. package/dist/cli/share-presign-transport.test.js +2 -0
  64. package/dist/cli/share-presign-transport.test.js.map +1 -1
  65. package/dist/cli/share.d.ts +0 -19
  66. package/dist/cli/share.d.ts.map +1 -1
  67. package/dist/cli/share.js +37 -57
  68. package/dist/cli/share.js.map +1 -1
  69. package/dist/cli/share.test.js +93 -87
  70. package/dist/cli/share.test.js.map +1 -1
  71. package/dist/cli/sync-scope.test.js +0 -8
  72. package/dist/cli/sync-scope.test.js.map +1 -1
  73. package/dist/cli/sync.d.ts +0 -12
  74. package/dist/cli/sync.d.ts.map +1 -1
  75. package/dist/cli/sync.js +118 -226
  76. package/dist/cli/sync.js.map +1 -1
  77. package/dist/cli/sync.test.js +2 -215
  78. package/dist/cli/sync.test.js.map +1 -1
  79. package/dist/cli/watch-event-push-conflict.test.js +2 -0
  80. package/dist/cli/watch-event-push-conflict.test.js.map +1 -1
  81. package/dist/cognito-auth.d.ts +30 -0
  82. package/dist/cognito-auth.d.ts.map +1 -1
  83. package/dist/cognito-auth.js +175 -0
  84. package/dist/cognito-auth.js.map +1 -1
  85. package/dist/index.d.ts +2 -2
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.js +1 -1
  88. package/dist/index.js.map +1 -1
  89. package/dist/journal-store.test.js +7 -130
  90. package/dist/journal-store.test.js.map +1 -1
  91. package/dist/journal.d.ts +1 -9
  92. package/dist/journal.d.ts.map +1 -1
  93. package/dist/journal.js +21 -30
  94. package/dist/journal.js.map +1 -1
  95. package/dist/lib/exit-codes.d.ts +0 -15
  96. package/dist/lib/exit-codes.d.ts.map +1 -1
  97. package/dist/lib/exit-codes.js +0 -15
  98. package/dist/lib/exit-codes.js.map +1 -1
  99. package/dist/machine-auth.test.js +295 -0
  100. package/dist/machine-auth.test.js.map +1 -1
  101. package/dist/object-io.d.ts +2 -2
  102. package/dist/object-io.d.ts.map +1 -1
  103. package/dist/object-io.js +18 -3
  104. package/dist/object-io.js.map +1 -1
  105. package/dist/personal-vault.d.ts.map +1 -1
  106. package/dist/personal-vault.js +3 -10
  107. package/dist/personal-vault.js.map +1 -1
  108. package/dist/personal-vault.test.js +0 -35
  109. package/dist/personal-vault.test.js.map +1 -1
  110. package/dist/remote-pull.d.ts.map +1 -1
  111. package/dist/remote-pull.js +2 -9
  112. package/dist/remote-pull.js.map +1 -1
  113. package/dist/remote-pull.test.js +2 -59
  114. package/dist/remote-pull.test.js.map +1 -1
  115. package/dist/s3.d.ts +18 -1
  116. package/dist/s3.d.ts.map +1 -1
  117. package/dist/s3.js +47 -24
  118. package/dist/s3.js.map +1 -1
  119. package/dist/s3.test.js +27 -1
  120. package/dist/s3.test.js.map +1 -1
  121. package/dist/sync/realtime-telemetry.d.ts +1 -1
  122. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  123. package/dist/sync/realtime-telemetry.js +6 -14
  124. package/dist/sync/realtime-telemetry.js.map +1 -1
  125. package/dist/sync/realtime-telemetry.test.js +16 -136
  126. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  127. package/dist/sync-core.d.ts +6 -4
  128. package/dist/sync-core.d.ts.map +1 -1
  129. package/dist/sync-core.js +10 -4
  130. package/dist/sync-core.js.map +1 -1
  131. package/dist/sync-core.test.js +25 -1
  132. package/dist/sync-core.test.js.map +1 -1
  133. package/package.json +1 -2
  134. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +0 -2
  135. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +0 -1
  136. package/dist/cli/onepass-pull-planner-regressions.test.js +0 -300
  137. package/dist/cli/onepass-pull-planner-regressions.test.js.map +0 -1
  138. package/dist/cli/onepass-pull-planner.test.d.ts +0 -2
  139. package/dist/cli/onepass-pull-planner.test.d.ts.map +0 -1
  140. package/dist/cli/onepass-pull-planner.test.js +0 -266
  141. package/dist/cli/onepass-pull-planner.test.js.map +0 -1
  142. package/dist/cli/streaming-pull-planner.bench.test.d.ts +0 -15
  143. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +0 -1
  144. package/dist/cli/streaming-pull-planner.bench.test.js +0 -273
  145. package/dist/cli/streaming-pull-planner.bench.test.js.map +0 -1
  146. package/dist/s3.list-cost.test.d.ts +0 -2
  147. package/dist/s3.list-cost.test.d.ts.map +0 -1
  148. package/dist/s3.list-cost.test.js +0 -51
  149. package/dist/s3.list-cost.test.js.map +0 -1
  150. package/dist/sync/remote-file-spool.d.ts +0 -16
  151. package/dist/sync/remote-file-spool.d.ts.map +0 -1
  152. package/dist/sync/remote-file-spool.js +0 -91
  153. package/dist/sync/remote-file-spool.js.map +0 -1
  154. package/dist/sync/remote-file-spool.test.d.ts +0 -2
  155. package/dist/sync/remote-file-spool.test.d.ts.map +0 -1
  156. package/dist/sync/remote-file-spool.test.js +0 -77
  157. package/dist/sync/remote-file-spool.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, CHORE_STARVATION_CAP_MS, CooperativePassScheduler, 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, 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,10 +25,9 @@ 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, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
28
+ import { PARTIAL_SYNC_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";
32
31
  import { CognitoAuthError, CognitoRefreshError } from "../cognito-auth.js";
33
32
  import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
34
33
  import yaml from "js-yaml";
@@ -163,7 +162,6 @@ function makeVaultStub(opts = {}) {
163
162
  bucketName: `bucket-${uid}`,
164
163
  status: "active",
165
164
  })),
166
- findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
167
165
  listByType: opts.listPersons ??
168
166
  (() => Promise.resolve([])),
169
167
  },
@@ -956,164 +954,6 @@ describe("target resolution", () => {
956
954
  const call = deps.sync.mock.calls[0][0];
957
955
  expect(call.company).toBe("cmp_abc");
958
956
  });
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
- });
1117
957
  });
1118
958
  // ---------------------------------------------------------------------------
1119
959
  // --personal mode (personal-vault-only, skip company fanout)
@@ -5746,7 +5586,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5746
5586
  });
5747
5587
  await expect(superseded).resolves.toBeUndefined();
5748
5588
  expect(runPass.mock.calls.filter(([argv]) => argv.includes("superseded.md"))).toHaveLength(0);
5749
- expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":2,"source":"receiver","direction":"pull","outcome":"refused"');
5589
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":1,"source":"receiver","direction":"pull","outcome":"refused"');
5750
5590
  releaseHeld();
5751
5591
  await heldReceiverWork;
5752
5592
  await replacement;
@@ -6059,7 +5899,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6059
5899
  triggerShutdown();
6060
5900
  await loop;
6061
5901
  });
6062
- it("fast lane: a small watcher batch waits for a full pass that has not reached a hand-off checkpoint", async () => {
5902
+ it("fast lane: a small watcher batch dispatches during a slow bulk pass", async () => {
6063
5903
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
6064
5904
  const relativePath = "companies/indigo/realtime.md";
6065
5905
  const absolutePath = path.join(hqRoot, relativePath);
@@ -6099,17 +5939,15 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6099
5939
  expect(order).toEqual(["bulk"]);
6100
5940
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6101
5941
  await flushLoopMicrotasks(30);
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"]);
6105
- releaseBulk();
6106
- await flushLoopMicrotasks(30);
5942
+ // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
6107
5943
  expect(order).toEqual(["bulk", "fast", "fast"]);
5944
+ releaseBulk();
5945
+ await flushLoopMicrotasks();
6108
5946
  triggerShutdown();
6109
5947
  await loop;
6110
5948
  fs.rmSync(hqRoot, { recursive: true, force: true });
6111
5949
  });
6112
- it("parallel coordinator: a watcher push waits for a full chore without a hand-off checkpoint", async () => {
5950
+ it("parallel coordinator: a watcher push starts while a full chore is blocked", async () => {
6113
5951
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6114
5952
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6115
5953
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -6152,10 +5990,9 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6152
5990
  expect(order).toEqual(["chore"]);
6153
5991
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6154
5992
  await flushLoopMicrotasks(30);
6155
- expect(order).toEqual(["chore"]);
6156
- releaseBulk();
6157
- await flushLoopMicrotasks(30);
6158
5993
  expect(order).toEqual(["chore", "realtime"]);
5994
+ releaseBulk();
5995
+ await flushLoopMicrotasks();
6159
5996
  triggerShutdown();
6160
5997
  await loop;
6161
5998
  }
@@ -6240,7 +6077,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6240
6077
  fs.rmSync(hqRoot, { recursive: true, force: true });
6241
6078
  }
6242
6079
  });
6243
- it("parallel coordinator partitions receiver events into fast and slow realtime lanes without overlapping their durable writes", async () => {
6080
+ it("parallel coordinator partitions receiver events into fast and slow realtime lanes", async () => {
6244
6081
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6245
6082
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6246
6083
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6311,7 +6148,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6311
6148
  signal: new AbortController().signal,
6312
6149
  });
6313
6150
  await flushLoopMicrotasks(20);
6314
- expect(order).toEqual(["slow-held"]);
6151
+ expect(order).toEqual(["slow-held", "fast-small"]);
6315
6152
  releaseSlow();
6316
6153
  await held;
6317
6154
  await partitioned;
@@ -6652,19 +6489,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6652
6489
  ]),
6653
6490
  });
6654
6491
  await flushLoopMicrotasks(30);
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);
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");
6658
6496
  releaseBulk();
6659
6497
  await flushLoopMicrotasks(30);
6660
6498
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
6661
- expect(calls.flat().join(" ")).toContain("realtime.md");
6662
6499
  expect(calls.flat().join(" ")).toContain("session-0.log");
6663
6500
  triggerShutdown();
6664
6501
  await loop;
6665
6502
  fs.rmSync(hqRoot, { recursive: true, force: true });
6666
6503
  });
6667
- it("fast lane: a small receiver pull waits for a slow full pass without a hand-off checkpoint", async () => {
6504
+ it("fast lane: a small receiver pull dispatches during a slow bulk pass", async () => {
6668
6505
  const watcher = makeWatcherStub();
6669
6506
  let triggerShutdown = () => { };
6670
6507
  let receiverSync;
@@ -6709,7 +6546,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6709
6546
  watcher.emit();
6710
6547
  await flushLoopMicrotasks();
6711
6548
  expect(order).toEqual(["bulk"]);
6712
- const receiverPull = receiverSync({
6549
+ await receiverSync({
6713
6550
  events: [{
6714
6551
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
6715
6552
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -6717,11 +6554,8 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6717
6554
  }],
6718
6555
  signal: new AbortController().signal,
6719
6556
  });
6720
- await flushLoopMicrotasks(20);
6721
- expect(order).toEqual(["bulk"]);
6722
- releaseBulk();
6723
- await receiverPull;
6724
6557
  expect(order).toEqual(["bulk", "fast-pull"]);
6558
+ releaseBulk();
6725
6559
  await flushLoopMicrotasks();
6726
6560
  triggerShutdown();
6727
6561
  await loop;
@@ -6765,14 +6599,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6765
6599
  await flushLoopMicrotasks();
6766
6600
  watcher.emit();
6767
6601
  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.
6771
6602
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6772
6603
  await fastStarted;
6604
+ releaseBulk();
6773
6605
  await flushLoopMicrotasks(20);
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.
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.
6776
6608
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
6777
6609
  releaseFast();
6778
6610
  await flushLoopMicrotasks(20);
@@ -6947,68 +6779,59 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6947
6779
  return () => { };
6948
6780
  },
6949
6781
  });
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);
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
+ };
6988
6817
  }
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 });
6818
+ writeJournal(slug, journal);
7011
6819
  }
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 });
7012
6835
  });
7013
6836
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
7014
6837
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7180,12 +7003,9 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7180
7003
  await flushLoopMicrotasks();
7181
7004
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7182
7005
  await flushLoopMicrotasks(30);
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);
7006
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
7187
7007
  releaseBulk();
7188
- await flushLoopMicrotasks(30);
7008
+ await flushLoopMicrotasks();
7189
7009
  const journal = readJournal("indigo").files;
7190
7010
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7191
7011
  for (let index = 0; index < paths.length; index++) {
@@ -7237,12 +7057,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7237
7057
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7238
7058
  }
7239
7059
  await flushLoopMicrotasks(60);
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([]);
7060
+ expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
7243
7061
  releaseBulk();
7244
- await flushLoopMicrotasks(60);
7245
- expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7062
+ await flushLoopMicrotasks();
7063
+ expect(completed).toContain("bulk");
7246
7064
  triggerShutdown();
7247
7065
  await loop;
7248
7066
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -7560,55 +7378,6 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7560
7378
  expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
7561
7379
  expect(watcher.disposed).toBe(false);
7562
7380
  });
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
- });
7612
7381
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
7613
7382
  const watcher = makeWatcherStub();
7614
7383
  const dnsError = Object.assign(new Error("getaddrinfo ENOTFOUND vault"), {
@@ -7902,85 +7671,6 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
7902
7671
  });
7903
7672
  });
7904
7673
  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
- });
7984
7674
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
7985
7675
  const watcher = makeWatcherStub();
7986
7676
  let triggerShutdown = () => { };
@@ -8093,173 +7783,6 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
8093
7783
  await loop;
8094
7784
  });
8095
7785
  });
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
- });
8263
7786
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8264
7787
  const watchArgv = [
8265
7788
  "--companies",