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

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 (179) hide show
  1. package/dist/backup-prune.d.ts +23 -1
  2. package/dist/backup-prune.d.ts.map +1 -1
  3. package/dist/backup-prune.js +92 -8
  4. package/dist/backup-prune.js.map +1 -1
  5. package/dist/backup-prune.test.js +36 -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 +126 -57
  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 +120 -318
  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 +79 -1079
  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 +1454 -162
  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 +627 -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 +467 -150
  68. package/dist/cli/share.js.map +1 -1
  69. package/dist/cli/share.test.js +608 -95
  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 +15 -13
  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-row-store.d.ts +5 -9
  90. package/dist/journal-row-store.d.ts.map +1 -1
  91. package/dist/journal-row-store.js +10 -70
  92. package/dist/journal-row-store.js.map +1 -1
  93. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  94. package/dist/journal-snapshot-v4.js +6 -11
  95. package/dist/journal-snapshot-v4.js.map +1 -1
  96. package/dist/journal-store.test.js +7 -259
  97. package/dist/journal-store.test.js.map +1 -1
  98. package/dist/journal.d.ts +15 -43
  99. package/dist/journal.d.ts.map +1 -1
  100. package/dist/journal.js +79 -297
  101. package/dist/journal.js.map +1 -1
  102. package/dist/lib/describe-error.d.ts.map +1 -1
  103. package/dist/lib/describe-error.js +7 -0
  104. package/dist/lib/describe-error.js.map +1 -1
  105. package/dist/lib/describe-error.test.js +11 -0
  106. package/dist/lib/describe-error.test.js.map +1 -1
  107. package/dist/lib/exit-codes.d.ts +0 -15
  108. package/dist/lib/exit-codes.d.ts.map +1 -1
  109. package/dist/lib/exit-codes.js +0 -15
  110. package/dist/lib/exit-codes.js.map +1 -1
  111. package/dist/machine-auth.test.js +295 -0
  112. package/dist/machine-auth.test.js.map +1 -1
  113. package/dist/object-io.d.ts +2 -2
  114. package/dist/object-io.d.ts.map +1 -1
  115. package/dist/object-io.js +18 -3
  116. package/dist/object-io.js.map +1 -1
  117. package/dist/personal-vault.d.ts.map +1 -1
  118. package/dist/personal-vault.js +3 -10
  119. package/dist/personal-vault.js.map +1 -1
  120. package/dist/personal-vault.test.js +0 -35
  121. package/dist/personal-vault.test.js.map +1 -1
  122. package/dist/remote-pull.d.ts.map +1 -1
  123. package/dist/remote-pull.js +2 -9
  124. package/dist/remote-pull.js.map +1 -1
  125. package/dist/remote-pull.test.js +2 -59
  126. package/dist/remote-pull.test.js.map +1 -1
  127. package/dist/s3.d.ts +18 -1
  128. package/dist/s3.d.ts.map +1 -1
  129. package/dist/s3.js +47 -24
  130. package/dist/s3.js.map +1 -1
  131. package/dist/s3.test.js +27 -1
  132. package/dist/s3.test.js.map +1 -1
  133. package/dist/sync/area-ledger.js +4 -4
  134. package/dist/sync/area-ledger.js.map +1 -1
  135. package/dist/sync/parallel-sync-coordinator.test.js +0 -29
  136. package/dist/sync/parallel-sync-coordinator.test.js.map +1 -1
  137. package/dist/sync/realtime-telemetry.d.ts +1 -1
  138. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  139. package/dist/sync/realtime-telemetry.js +6 -14
  140. package/dist/sync/realtime-telemetry.js.map +1 -1
  141. package/dist/sync/realtime-telemetry.test.js +16 -136
  142. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  143. package/dist/sync-core.d.ts +6 -4
  144. package/dist/sync-core.d.ts.map +1 -1
  145. package/dist/sync-core.js +10 -4
  146. package/dist/sync-core.js.map +1 -1
  147. package/dist/sync-core.test.js +25 -1
  148. package/dist/sync-core.test.js.map +1 -1
  149. package/dist/vault-client.d.ts +26 -5
  150. package/dist/vault-client.d.ts.map +1 -1
  151. package/dist/vault-client.js +43 -20
  152. package/dist/vault-client.js.map +1 -1
  153. package/dist/vault-client.test.js +32 -0
  154. package/dist/vault-client.test.js.map +1 -1
  155. package/package.json +1 -2
  156. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +0 -2
  157. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +0 -1
  158. package/dist/cli/onepass-pull-planner-regressions.test.js +0 -300
  159. package/dist/cli/onepass-pull-planner-regressions.test.js.map +0 -1
  160. package/dist/cli/onepass-pull-planner.test.d.ts +0 -2
  161. package/dist/cli/onepass-pull-planner.test.d.ts.map +0 -1
  162. package/dist/cli/onepass-pull-planner.test.js +0 -266
  163. package/dist/cli/onepass-pull-planner.test.js.map +0 -1
  164. package/dist/cli/streaming-pull-planner.bench.test.d.ts +0 -15
  165. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +0 -1
  166. package/dist/cli/streaming-pull-planner.bench.test.js +0 -273
  167. package/dist/cli/streaming-pull-planner.bench.test.js.map +0 -1
  168. package/dist/s3.list-cost.test.d.ts +0 -2
  169. package/dist/s3.list-cost.test.d.ts.map +0 -1
  170. package/dist/s3.list-cost.test.js +0 -51
  171. package/dist/s3.list-cost.test.js.map +0 -1
  172. package/dist/sync/remote-file-spool.d.ts +0 -16
  173. package/dist/sync/remote-file-spool.d.ts.map +0 -1
  174. package/dist/sync/remote-file-spool.js +0 -91
  175. package/dist/sync/remote-file-spool.js.map +0 -1
  176. package/dist/sync/remote-file-spool.test.d.ts +0 -2
  177. package/dist/sync/remote-file-spool.test.d.ts.map +0 -1
  178. package/dist/sync/remote-file-spool.test.js +0 -77
  179. package/dist/sync/remote-file-spool.test.js.map +0 -1
@@ -10,9 +10,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
10
10
  import * as fs from "fs";
11
11
  import * as os from "os";
12
12
  import * as path from "path";
13
- import * as v8 from "node:v8";
14
- import * as vm from "node:vm";
15
- import { monitorEventLoopDelay } from "node:perf_hooks";
16
13
  import { runRunner, runRunnerWithLoop, resolveDeletePolicy, resolveSkipPersonal, resolveSkipCompanies, selectObjectIOFactory, routeChangeToTarget, buildTargetedPushArgv, resolvePullScope, readPinnedPrefixes, SESSIONS_SCOPE_PREFIX, defaultCollectTelemetry, AUTH_REQUIRED_PASS_EXIT, } from "./sync-runner.js";
17
14
  import { SESSION_LOGS_PUSH_ONLY_PREFIX } from "../sync/pull-scope.js";
18
15
  import { AUTH_WAIT_INITIAL_RECHECK_MS, AUTH_WAIT_RESUME_MESSAGE, AUTH_WAIT_START_MESSAGE, AUTH_WAIT_STEADY_RECHECK_MS, } from "./sync-runner-auth-wait.js";
@@ -20,20 +17,17 @@ import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
20
17
  import { setBandwidthGovernorForTesting } from "../bandwidth.js";
21
18
  import { POST_SYNC_FLAG_KEYS, } from "./sync-runner-post-sync-flags.js";
22
19
  import { FakeClock, TreeWatcher, } from "../watcher.js";
23
- 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";
24
- import { AreaLedgerMigration, JournalStore, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, readJournalScoped, writeJournal, } from "../journal.js";
25
- import { AreaLedger } from "../sync/area-ledger.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
+ import { AreaLedgerMigration, JournalStore, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, writeJournal, } from "../journal.js";
26
22
  import { AreaResolver } from "../sync/area-resolver.js";
27
- import { AREA_LEGACY_MIRROR_LINGER_MS } from "../sync/area-ledger-migration.js";
28
23
  import { AreaMaintenanceCursorStore } from "../sync/per-area-maintenance.js";
29
24
  import { HQ_CLOUD_VERSION } from "../version.js";
30
25
  import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
31
26
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
32
27
  import { getStateStoreSnapshotRecoveryCountForTest, resetStateStoreSnapshotRecoveryCountForTest, StateStoreLockError, } from "../sync/state-store.js";
33
- import { PARTIAL_SYNC_EXIT, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
28
+ import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
34
29
  import { isCoveredByAny } from "../prefix-coalesce.js";
35
30
  import { VaultAuthError, VaultNotFoundError } from "../vault-client.js";
36
- import { buildRoutePullArgv } from "./sync-runner-watch-routes.js";
37
31
  import { CognitoAuthError, CognitoRefreshError } from "../cognito-auth.js";
38
32
  import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
39
33
  import yaml from "js-yaml";
@@ -168,7 +162,6 @@ function makeVaultStub(opts = {}) {
168
162
  bucketName: `bucket-${uid}`,
169
163
  status: "active",
170
164
  })),
171
- findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
172
165
  listByType: opts.listPersons ??
173
166
  (() => Promise.resolve([])),
174
167
  },
@@ -961,164 +954,6 @@ describe("target resolution", () => {
961
954
  const call = deps.sync.mock.calls[0][0];
962
955
  expect(call.company).toBe("cmp_abc");
963
956
  });
964
- it("returns the unresolved-company exit for an empty single-company plan instead of emitting a clean all-complete", async () => {
965
- const deps = makeDeps({
966
- createVaultClient: () => makeVaultStub({
967
- entityGet: () => Promise.resolve(undefined),
968
- }),
969
- });
970
- expect(await runRunner(["--company", "cmp_missing"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
971
- expect(deps.stderr.events()).toContainEqual({
972
- type: "error",
973
- company: "cmp_missing",
974
- path: "(company-resolution)",
975
- message: "company entity lookup returned no entity",
976
- });
977
- expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
978
- expect(deps.sync).not.toHaveBeenCalled();
979
- });
980
- it("returns the unresolved-company exit when a cmp-prefixed route gets a 404", async () => {
981
- const client = makeVaultStub({
982
- entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
983
- });
984
- const findInMyNamespace = vi.fn().mockResolvedValue(null);
985
- client.entity.findInMyNamespace = findInMyNamespace;
986
- const deps = makeDeps({
987
- createVaultClient: () => client,
988
- });
989
- expect(await runRunner(["--company", "cmp_ghost"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
990
- expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
991
- expect(deps.stderr.events()).toContainEqual({
992
- type: "error",
993
- company: "cmp_ghost",
994
- path: "(company-resolution)",
995
- message: "company entity lookup returned 404",
996
- });
997
- expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
998
- });
999
- it("reports an intentional skipCompanies skip without treating it as a failed pass", async () => {
1000
- const previous = process.env.HQ_SYNC_SKIP_COMPANIES;
1001
- process.env.HQ_SYNC_SKIP_COMPANIES = "paused";
1002
- try {
1003
- const deps = makeDeps({
1004
- createVaultClient: () => makeVaultStub({
1005
- entityGet: (uid) => Promise.resolve({
1006
- uid,
1007
- slug: "paused",
1008
- type: "company",
1009
- status: "active",
1010
- }),
1011
- }),
1012
- });
1013
- expect(await runRunner(["--company", "cmp_paused"], deps)).toBe(0);
1014
- expect(deps.stdout.events()).toContainEqual({
1015
- type: "company-skipped",
1016
- company: "paused",
1017
- reason: "skip-companies",
1018
- });
1019
- expect(deps.stderr.events().some((event) => event.type === "error")).toBe(false);
1020
- expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
1021
- expect(deps.sync).not.toHaveBeenCalled();
1022
- }
1023
- finally {
1024
- if (previous === undefined)
1025
- delete process.env.HQ_SYNC_SKIP_COMPANIES;
1026
- else
1027
- process.env.HQ_SYNC_SKIP_COMPANIES = previous;
1028
- }
1029
- });
1030
- it("resolves a slug emitted by the company watch route to its namespace UID", async () => {
1031
- const company = {
1032
- uid: "cmp_indigo",
1033
- slug: "indigo",
1034
- type: "company",
1035
- status: "active",
1036
- bucketName: "bucket-cmp_indigo",
1037
- };
1038
- const client = makeVaultStub({
1039
- entityGet: (uid) => Promise.resolve(uid === company.uid ? company : undefined),
1040
- });
1041
- const findInMyNamespace = vi.fn().mockResolvedValue(company);
1042
- client.entity.findInMyNamespace = findInMyNamespace;
1043
- const deps = makeDeps({ createVaultClient: () => client });
1044
- const argv = buildRoutePullArgv({ kind: "company", slug: "indigo" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1045
- expect(argv).toEqual([
1046
- "--company",
1047
- "indigo",
1048
- "--direction",
1049
- "pull",
1050
- "--hq-root",
1051
- "/tmp/hq",
1052
- ]);
1053
- expect(await runRunner(argv, deps)).toBe(0);
1054
- expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1055
- expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: "cmp_indigo" }));
1056
- expect(deps.stdout.events()).toContainEqual({
1057
- type: "fanout-plan",
1058
- companies: [
1059
- expect.objectContaining({ uid: "cmp_indigo", slug: "indigo" }),
1060
- ],
1061
- });
1062
- });
1063
- it("resolves a cmp-prefixed directory slug in the caller namespace before treating it as a UID", async () => {
1064
- const company = {
1065
- uid: "cmp_01JTEAM000000000000000000",
1066
- slug: "cmp_team",
1067
- type: "company",
1068
- status: "active",
1069
- bucketName: "bucket-cmp-team",
1070
- };
1071
- const get = vi.fn().mockImplementation((uid) => Promise.resolve(uid === company.uid ? company : undefined));
1072
- const client = makeVaultStub({ entityGet: get });
1073
- const findInMyNamespace = vi.fn().mockResolvedValue(company);
1074
- client.entity.findInMyNamespace = findInMyNamespace;
1075
- const deps = makeDeps({ createVaultClient: () => client });
1076
- const argv = buildRoutePullArgv({ kind: "company", slug: "cmp_team" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1077
- expect(await runRunner(argv, deps)).toBe(0);
1078
- expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_team");
1079
- expect(get).toHaveBeenCalledWith(company.uid);
1080
- expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: company.uid }));
1081
- expect(deps.stdout.events()).toContainEqual({
1082
- type: "fanout-plan",
1083
- companies: [expect.objectContaining({ uid: company.uid, slug: "cmp_team" })],
1084
- });
1085
- });
1086
- it("falls back to a literal company UID after caller-namespace slug resolution misses", async () => {
1087
- const uid = "cmp_01JUID0000000000000000000";
1088
- const company = {
1089
- uid,
1090
- slug: "operator-company",
1091
- type: "company",
1092
- status: "active",
1093
- bucketName: "bucket-operator-company",
1094
- };
1095
- const get = vi.fn().mockResolvedValue(company);
1096
- const client = makeVaultStub({ entityGet: get });
1097
- const findInMyNamespace = vi.fn().mockResolvedValue(null);
1098
- client.entity.findInMyNamespace = findInMyNamespace;
1099
- const deps = makeDeps({ createVaultClient: () => client });
1100
- expect(await runRunner(["--company", uid], deps)).toBe(0);
1101
- expect(findInMyNamespace).toHaveBeenCalledWith("company", uid);
1102
- expect(get).toHaveBeenCalledWith(uid);
1103
- expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: uid }));
1104
- });
1105
- it("reports a foreign company slug when the literal UID fallback finds nothing", async () => {
1106
- const get = vi.fn().mockRejectedValue(new VaultNotFoundError("company not found"));
1107
- const client = makeVaultStub({ entityGet: get });
1108
- const findInMyNamespace = vi.fn().mockResolvedValue(null);
1109
- client.entity.findInMyNamespace = findInMyNamespace;
1110
- const deps = makeDeps({ createVaultClient: () => client });
1111
- expect(await runRunner(["--company", "indigo"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
1112
- expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1113
- expect(get).toHaveBeenCalledWith("indigo");
1114
- expect(deps.sync).not.toHaveBeenCalled();
1115
- expect(deps.stderr.events()).toContainEqual({
1116
- type: "error",
1117
- company: "indigo",
1118
- path: "(company-resolution)",
1119
- message: "company entity lookup returned 404",
1120
- });
1121
- });
1122
957
  });
1123
958
  // ---------------------------------------------------------------------------
1124
959
  // --personal mode (personal-vault-only, skip company fanout)
@@ -4328,26 +4163,6 @@ async function flushLoopMicrotasks(n = 8) {
4328
4163
  for (let i = 0; i < n; i++)
4329
4164
  await Promise.resolve();
4330
4165
  }
4331
- /** Retained journal rows live in typed-array backing stores as well as heap. */
4332
- function forceGarbageCollectionForTest() {
4333
- const existing = globalThis.gc;
4334
- if (typeof existing === "function") {
4335
- existing();
4336
- existing();
4337
- return;
4338
- }
4339
- v8.setFlagsFromString("--expose-gc");
4340
- const collect = vm.runInNewContext("gc");
4341
- v8.setFlagsFromString("--no-expose-gc");
4342
- if (typeof collect !== "function")
4343
- throw new Error("test could not expose GC");
4344
- collect();
4345
- collect();
4346
- }
4347
- function retainedProcessBytes() {
4348
- const usage = process.memoryUsage();
4349
- return usage.heapUsed + usage.external + usage.arrayBuffers;
4350
- }
4351
4166
  function makeSteppableSleep() {
4352
4167
  const pending = [];
4353
4168
  return {
@@ -5771,7 +5586,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5771
5586
  });
5772
5587
  await expect(superseded).resolves.toBeUndefined();
5773
5588
  expect(runPass.mock.calls.filter(([argv]) => argv.includes("superseded.md"))).toHaveLength(0);
5774
- 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"');
5775
5590
  releaseHeld();
5776
5591
  await heldReceiverWork;
5777
5592
  await replacement;
@@ -6084,7 +5899,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6084
5899
  triggerShutdown();
6085
5900
  await loop;
6086
5901
  });
6087
- 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 () => {
6088
5903
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
6089
5904
  const relativePath = "companies/indigo/realtime.md";
6090
5905
  const absolutePath = path.join(hqRoot, relativePath);
@@ -6124,17 +5939,15 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6124
5939
  expect(order).toEqual(["bulk"]);
6125
5940
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6126
5941
  await flushLoopMicrotasks(30);
6127
- // A full pass that has not reached its durable checkpoint remains the sole
6128
- // owner. Realtime is promoted at the checkpoint, never run concurrently.
6129
- expect(order).toEqual(["bulk"]);
6130
- releaseBulk();
6131
- await flushLoopMicrotasks(30);
5942
+ // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
6132
5943
  expect(order).toEqual(["bulk", "fast", "fast"]);
5944
+ releaseBulk();
5945
+ await flushLoopMicrotasks();
6133
5946
  triggerShutdown();
6134
5947
  await loop;
6135
5948
  fs.rmSync(hqRoot, { recursive: true, force: true });
6136
5949
  });
6137
- 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 () => {
6138
5951
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6139
5952
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6140
5953
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -6177,10 +5990,9 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6177
5990
  expect(order).toEqual(["chore"]);
6178
5991
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6179
5992
  await flushLoopMicrotasks(30);
6180
- expect(order).toEqual(["chore"]);
6181
- releaseBulk();
6182
- await flushLoopMicrotasks(30);
6183
5993
  expect(order).toEqual(["chore", "realtime"]);
5994
+ releaseBulk();
5995
+ await flushLoopMicrotasks();
6184
5996
  triggerShutdown();
6185
5997
  await loop;
6186
5998
  }
@@ -6265,7 +6077,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6265
6077
  fs.rmSync(hqRoot, { recursive: true, force: true });
6266
6078
  }
6267
6079
  });
6268
- 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 () => {
6269
6081
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6270
6082
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6271
6083
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6336,7 +6148,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6336
6148
  signal: new AbortController().signal,
6337
6149
  });
6338
6150
  await flushLoopMicrotasks(20);
6339
- expect(order).toEqual(["slow-held"]);
6151
+ expect(order).toEqual(["slow-held", "fast-small"]);
6340
6152
  releaseSlow();
6341
6153
  await held;
6342
6154
  await partitioned;
@@ -6502,504 +6314,6 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6502
6314
  fs.rmSync(hqRoot, { recursive: true, force: true });
6503
6315
  }
6504
6316
  });
6505
- it("bounds a 32-path realtime personal commit on a 700k-row journal and 50k-path tree", async () => {
6506
- const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6507
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6508
- const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-production-scale-watch-"));
6509
- const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6510
- const targetPaths = Array.from({ length: 32 }, (_, index) => `personal/watch-load/${String(index).padStart(5, "0")}/target.md`);
6511
- const files = {};
6512
- for (let index = 0; index < 700_000; index += 1) {
6513
- files[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6514
- hash: `sha256:history-${index}`,
6515
- size: index % 4096,
6516
- syncedAt: "2026-09-16T21:30:00.000Z",
6517
- direction: "up",
6518
- };
6519
- }
6520
- for (const target of targetPaths) {
6521
- files[target] = {
6522
- hash: `sha256:${target}`,
6523
- size: 1,
6524
- syncedAt: "2026-09-16T21:30:00.000Z",
6525
- direction: "up",
6526
- };
6527
- }
6528
- writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6529
- version: "2",
6530
- lastSync: "2026-09-16T21:30:00.000Z",
6531
- pulls: [],
6532
- files,
6533
- });
6534
- for (let index = 0; index < 50_000; index += 1) {
6535
- const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
6536
- fs.mkdirSync(directory, { recursive: true });
6537
- fs.writeFileSync(path.join(directory, "seed.md"), "seed");
6538
- }
6539
- const watcher = makeBatchWatcherStub();
6540
- let shutdown = () => { };
6541
- let committedAt;
6542
- const runPass = vi.fn(async (argv) => {
6543
- if (!argv.includes("--scope-path")) {
6544
- // Production opens the packed journal during the initial full pass,
6545
- // before the watcher accepts realtime work. That recovered projection
6546
- // is the one a scoped commit must reuse.
6547
- readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6548
- }
6549
- if (argv.includes("--scope-path") && argv.includes("push")) {
6550
- const scoped = readJournalScoped(PERSONAL_VAULT_JOURNAL_SLUG, targetPaths);
6551
- const target = targetPaths[0];
6552
- scoped.files[target] = {
6553
- ...scoped.files[target],
6554
- syncedAt: "2026-09-16T21:38:18.000Z",
6555
- };
6556
- writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, scoped);
6557
- committedAt = performance.now();
6558
- }
6559
- return completedPassOutcome();
6560
- });
6561
- const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6562
- runPass,
6563
- createWatcher: () => watcher,
6564
- sleep: () => new Promise(() => { }),
6565
- onShutdownSignal: (handler) => {
6566
- shutdown = handler;
6567
- return () => { };
6568
- },
6569
- });
6570
- try {
6571
- await flushLoopMicrotasks(30);
6572
- runPass.mockClear();
6573
- resetStateStoreSnapshotRecoveryCountForTest();
6574
- const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6575
- eventLoopDelay.enable();
6576
- await new Promise((resolve) => setTimeout(resolve, 0));
6577
- const submittedAt = performance.now();
6578
- let timerDelayMs;
6579
- const eventLoopTurn = new Promise((resolve) => {
6580
- setTimeout(() => {
6581
- timerDelayMs = performance.now() - submittedAt;
6582
- resolve();
6583
- }, 0);
6584
- });
6585
- const batch = {
6586
- paths: new Map(targetPaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6587
- changes: new Map(targetPaths.map((relativePath) => [
6588
- path.join(hqRoot, relativePath),
6589
- { kind: "change" },
6590
- ])),
6591
- };
6592
- watcher.emit(targetPaths[0], batch);
6593
- await vi.waitFor(() => expect(committedAt).toBeDefined(), { timeout: 30_000, interval: 10 });
6594
- await eventLoopTurn;
6595
- eventLoopDelay.disable();
6596
- expect(committedAt - submittedAt).toBeLessThan(1_000);
6597
- expect(Number(eventLoopDelay.max) / 1_000_000).toBeLessThan(100);
6598
- expect(timerDelayMs).toBeLessThan(100);
6599
- expect(getStateStoreSnapshotRecoveryCountForTest()).toBe(0);
6600
- expect(runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"))).toHaveLength(1);
6601
- }
6602
- finally {
6603
- shutdown();
6604
- await loop;
6605
- if (previousCoordinator === undefined)
6606
- delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6607
- else
6608
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
6609
- fs.rmSync(hqRoot, { recursive: true, force: true });
6610
- }
6611
- }, 180_000);
6612
- it("keeps an area-authoritative 700k-row personal aggregate hot across alternating realtime and company batches", async () => {
6613
- const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6614
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6615
- const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-multi-journal-watch-"));
6616
- const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6617
- const personalPath = "personal/watch-load/00000/target.md";
6618
- const companyPath = "companies/indigo/watch-load/00000/target.md";
6619
- const companyKey = "watch-load/00000/target.md";
6620
- const personalFiles = {};
6621
- for (let index = 0; index < 700_000; index += 1) {
6622
- personalFiles[`personal/history-${index % 5}/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6623
- hash: `sha256:history-${index}`,
6624
- size: index % 4096,
6625
- syncedAt: "2026-09-16T21:30:00.000Z",
6626
- direction: "up",
6627
- };
6628
- }
6629
- personalFiles[personalPath] = {
6630
- hash: "sha256:personal-target",
6631
- size: 1,
6632
- syncedAt: "2026-09-16T21:30:00.000Z",
6633
- direction: "up",
6634
- };
6635
- const resolver = new AreaResolver({
6636
- format: 1,
6637
- routerVersion: 1,
6638
- areas: [
6639
- ...Array.from({ length: 5 }, (_, index) => ({
6640
- id: `root:history-${index}`,
6641
- prefixes: [`personal/history-${index}`],
6642
- paths: [],
6643
- })),
6644
- { id: "root:watch", prefixes: ["personal/watch-load"], paths: [] },
6645
- { id: "root:@files", prefixes: [], paths: [], rootFiles: true },
6646
- ],
6647
- });
6648
- writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6649
- version: "2",
6650
- lastSync: "2026-09-16T21:30:00.000Z",
6651
- pulls: [],
6652
- files: {},
6653
- });
6654
- writeJournal("indigo", {
6655
- version: "2",
6656
- lastSync: "2026-09-16T21:30:00.000Z",
6657
- pulls: [],
6658
- files: {
6659
- [companyKey]: {
6660
- hash: "sha256:company-target",
6661
- size: 1,
6662
- syncedAt: "2026-09-16T21:30:00.000Z",
6663
- direction: "up",
6664
- },
6665
- },
6666
- });
6667
- const migration = new AreaLedgerMigration({
6668
- rootDir: __runnerTestStateDir,
6669
- journalSlug: PERSONAL_VAULT_JOURNAL_SLUG,
6670
- resolver,
6671
- legacyStore: openJournalStateStoreForMigration(PERSONAL_VAULT_JOURNAL_SLUG),
6672
- });
6673
- expect(migration.cutOver(120_000).phase).toBe("area-authoritative");
6674
- migration.dispose();
6675
- // The migration state itself is covered by the area-ledger migration
6676
- // suite. Seed the already-authoritative production shape in one durable
6677
- // transaction so this watch-loop test measures repeated realtime reads,
6678
- // rather than spending most of its worker budget replaying a one-time
6679
- // legacy-to-area copy.
6680
- openJournalStateStoreForMigration(PERSONAL_VAULT_JOURNAL_SLUG).replaceState({
6681
- journal: {
6682
- version: "2",
6683
- lastSync: "2026-09-16T21:30:00.000Z",
6684
- pulls: [],
6685
- files: personalFiles,
6686
- },
6687
- });
6688
- AreaLedger.open({
6689
- rootDir: __runnerTestStateDir,
6690
- journalSlug: PERSONAL_VAULT_JOURNAL_SLUG,
6691
- layoutEpoch: 1,
6692
- areaIds: resolver.layout.areas.map((area) => area.id),
6693
- resolveArea: (key) => resolver.resolve(key),
6694
- }).append({
6695
- upserts: personalFiles,
6696
- metadata: { lastSync: "2026-09-16T21:30:00.000Z", pulls: [] },
6697
- });
6698
- fs.mkdirSync(watchedRoot, { recursive: true });
6699
- for (let index = 0; index < 50_000; index += 1) {
6700
- fs.writeFileSync(path.join(watchedRoot, `seed-${String(index).padStart(5, "0")}.md`), "seed");
6701
- }
6702
- fs.mkdirSync(path.dirname(path.join(hqRoot, personalPath)), { recursive: true });
6703
- fs.writeFileSync(path.join(hqRoot, personalPath), "personal");
6704
- fs.mkdirSync(path.dirname(path.join(hqRoot, companyPath)), { recursive: true });
6705
- fs.writeFileSync(path.join(hqRoot, companyPath), "company");
6706
- const watcher = makeBatchWatcherStub();
6707
- const sleep = makeSteppableSleep();
6708
- let shutdown = () => { };
6709
- let isUntrackedDelete = (_relativePath) => false;
6710
- const commits = [];
6711
- let submittedAt = 0;
6712
- const aggregatePrime = vi.spyOn(AreaLedger.prototype, "primeAggregateCache");
6713
- const runPass = vi.fn(async (argv) => {
6714
- if (!argv.includes("--scope-path")) {
6715
- readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6716
- readJournal("indigo");
6717
- }
6718
- if (argv.includes("--scope-path") && argv.includes("push")) {
6719
- const slug = argv.includes("--company") ? "indigo" : PERSONAL_VAULT_JOURNAL_SLUG;
6720
- const key = slug === "indigo" ? companyKey : personalPath;
6721
- const scoped = readJournalScoped(slug, [key]);
6722
- scoped.files[key] = {
6723
- ...scoped.files[key],
6724
- syncedAt: "2026-09-16T21:38:18.000Z",
6725
- };
6726
- writeJournal(slug, scoped);
6727
- commits.push({ slug, elapsedMs: performance.now() - submittedAt });
6728
- }
6729
- return completedPassOutcome();
6730
- });
6731
- const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6732
- runPass,
6733
- createWatcher: (options) => {
6734
- isUntrackedDelete = options.isUntrackedDelete;
6735
- return watcher;
6736
- },
6737
- startEventSync: vi.fn().mockResolvedValue(null),
6738
- sleep: sleep.sleep,
6739
- onShutdownSignal: (handler) => {
6740
- shutdown = handler;
6741
- return () => { };
6742
- },
6743
- });
6744
- const emitAndWait = async (relativePaths) => {
6745
- const before = commits.length;
6746
- if (relativePaths.includes(personalPath)) {
6747
- // This is the same non-blocking watcher callback which starts an
6748
- // openJournalStoreSessionAsync before a delete episode is drained.
6749
- // It is intentionally issued before each realtime batch: a completed
6750
- // watch pass must not have thrown away the warmed area aggregate.
6751
- expect(isUntrackedDelete(personalPath)).toBe(false);
6752
- await new Promise((resolve) => setImmediate(resolve));
6753
- }
6754
- submittedAt = performance.now();
6755
- const batch = {
6756
- paths: new Map(relativePaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6757
- changes: new Map(relativePaths.map((relativePath) => [
6758
- path.join(hqRoot, relativePath),
6759
- { kind: "change" },
6760
- ])),
6761
- };
6762
- watcher.emit(relativePaths[0], batch);
6763
- await vi.waitFor(() => expect(commits.length).toBe(before + relativePaths.length), {
6764
- timeout: 30_000,
6765
- interval: 10,
6766
- });
6767
- // `commits` is observed inside the injected pass, before the runner has
6768
- // completed its pass-final cleanup. Let that cleanup run before the next
6769
- // watcher episode so the assertion observes the cache lifetime at the
6770
- // same boundary as a real completed watch pass.
6771
- await flushLoopMicrotasks(30);
6772
- };
6773
- try {
6774
- await flushLoopMicrotasks(30);
6775
- runPass.mockClear();
6776
- // The initial runner pass is the one permitted cold open. Every later
6777
- // scoped batch must reuse that authoritative aggregate. Warm the same
6778
- // yielding watcher-session path that production uses before its first
6779
- // delete episode, then measure only post-warmup primes.
6780
- aggregatePrime.mockClear();
6781
- expect(isUntrackedDelete(personalPath)).toBe(false);
6782
- await new Promise((resolve) => setImmediate(resolve));
6783
- // The initial full pass may already have opened this bridge (zero new
6784
- // primes), or this watcher callback may perform the one permitted cold
6785
- // warm-up. The post-poll assertion below is the regression boundary.
6786
- expect(aggregatePrime.mock.calls.length).toBeLessThanOrEqual(1);
6787
- aggregatePrime.mockClear();
6788
- // Reproduce the normal poll boundary observed on the outposts. On the
6789
- // current implementation it drops every cached area bridge even though
6790
- // the watcher and runner remain alive.
6791
- sleep.tick();
6792
- await flushLoopMicrotasks(30);
6793
- forceGarbageCollectionForTest();
6794
- const retainedAfterWarmupBytes = retainedProcessBytes();
6795
- const postWarmCpuStart = process.cpuUsage();
6796
- const postWarmStartedAt = performance.now();
6797
- resetStateStoreSnapshotRecoveryCountForTest();
6798
- const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6799
- eventLoopDelay.enable();
6800
- await emitAndWait([personalPath, companyPath]);
6801
- for (let round = 0; round < 3; round += 1) {
6802
- await emitAndWait([personalPath]);
6803
- await emitAndWait([companyPath]);
6804
- }
6805
- eventLoopDelay.disable();
6806
- const recoveryCount = getStateStoreSnapshotRecoveryCountForTest();
6807
- const maxDelayMs = Number(eventLoopDelay.max) / 1_000_000;
6808
- const maxCommitMs = Math.max(...commits.map((commit) => commit.elapsedMs));
6809
- // A first area delta briefly retains the reverse-shadow mirror to
6810
- // amortize a burst. Retained memory means after that documented linger
6811
- // has released its second full-vault projection, not during it.
6812
- await new Promise((resolve) => setTimeout(resolve, AREA_LEGACY_MIRROR_LINGER_MS + 25));
6813
- forceGarbageCollectionForTest();
6814
- const retainedAfterChurnBytes = retainedProcessBytes();
6815
- const retainedGrowthBytes = retainedAfterChurnBytes - retainedAfterWarmupBytes;
6816
- const postWarmCpu = process.cpuUsage(postWarmCpuStart);
6817
- const postWarmCpuPercent = ((postWarmCpu.user + postWarmCpu.system) /
6818
- ((performance.now() - postWarmStartedAt) * 1_000)) * 100;
6819
- console.info({
6820
- aggregatePrimeCount: aggregatePrime.mock.calls.length,
6821
- recoveryCount,
6822
- maxDelayMs,
6823
- maxCommitMs,
6824
- postWarmCpuPercent,
6825
- retainedAfterWarmupBytes,
6826
- retainedAfterChurnBytes,
6827
- retainedGrowthBytes,
6828
- commits: commits.length,
6829
- });
6830
- expect(aggregatePrime).not.toHaveBeenCalled();
6831
- // The first area delta opens its reverse-shadow v3 mirror once; its
6832
- // linger keeps all later scoped commits off snapshot recovery. This is
6833
- // deliberately exact so a second cold bridge cannot hide as a cache hit.
6834
- expect.soft(recoveryCount).toBe(1);
6835
- // The production-shape run remains bounded under the coverage job's
6836
- // concurrent workers: CI measured 1,090.52 ms of timer delay and
6837
- // 1,087.79 ms to commit. 1.5 s retains a concrete latency ceiling while
6838
- // leaving headroom for that instrumentation, rather than making the
6839
- // assertion depend on a single host's scheduling jitter.
6840
- expect.soft(maxDelayMs).toBeLessThan(1_500);
6841
- expect.soft(maxCommitMs).toBeLessThan(1_500);
6842
- expect.soft(retainedGrowthBytes).toBeLessThan(32 * 1024 * 1024);
6843
- expect(commits.map((commit) => commit.slug)).toEqual([
6844
- PERSONAL_VAULT_JOURNAL_SLUG,
6845
- "indigo",
6846
- PERSONAL_VAULT_JOURNAL_SLUG,
6847
- "indigo",
6848
- PERSONAL_VAULT_JOURNAL_SLUG,
6849
- "indigo",
6850
- PERSONAL_VAULT_JOURNAL_SLUG,
6851
- "indigo",
6852
- ]);
6853
- }
6854
- finally {
6855
- aggregatePrime.mockRestore();
6856
- shutdown();
6857
- await loop;
6858
- if (previousCoordinator === undefined)
6859
- delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6860
- else
6861
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
6862
- fs.rmSync(hqRoot, { recursive: true, force: true });
6863
- }
6864
- }, 180_000);
6865
- it("keeps warmed personal and company journals hot across alternating realtime batches", async () => {
6866
- const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6867
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6868
- const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-multi-journal-legacy-watch-"));
6869
- const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6870
- const personalPath = "personal/watch-load/00000/target.md";
6871
- const companyPath = "companies/indigo/watch-load/00000/target.md";
6872
- const companyKey = "watch-load/00000/target.md";
6873
- const personalFiles = {};
6874
- for (let index = 0; index < 700_000; index += 1) {
6875
- personalFiles[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6876
- hash: `sha256:history-${index}`,
6877
- size: index % 4096,
6878
- syncedAt: "2026-09-16T21:30:00.000Z",
6879
- direction: "up",
6880
- };
6881
- }
6882
- personalFiles[personalPath] = {
6883
- hash: "sha256:personal-target",
6884
- size: 1,
6885
- syncedAt: "2026-09-16T21:30:00.000Z",
6886
- direction: "up",
6887
- };
6888
- writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6889
- version: "2",
6890
- lastSync: "2026-09-16T21:30:00.000Z",
6891
- pulls: [],
6892
- files: personalFiles,
6893
- });
6894
- writeJournal("indigo", {
6895
- version: "2",
6896
- lastSync: "2026-09-16T21:30:00.000Z",
6897
- pulls: [],
6898
- files: {
6899
- [companyKey]: {
6900
- hash: "sha256:company-target",
6901
- size: 1,
6902
- syncedAt: "2026-09-16T21:30:00.000Z",
6903
- direction: "up",
6904
- },
6905
- },
6906
- });
6907
- for (let index = 0; index < 50_000; index += 1) {
6908
- const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
6909
- fs.mkdirSync(directory, { recursive: true });
6910
- fs.writeFileSync(path.join(directory, "seed.md"), "seed");
6911
- }
6912
- fs.mkdirSync(path.dirname(path.join(hqRoot, personalPath)), { recursive: true });
6913
- fs.writeFileSync(path.join(hqRoot, personalPath), "personal");
6914
- fs.mkdirSync(path.dirname(path.join(hqRoot, companyPath)), { recursive: true });
6915
- fs.writeFileSync(path.join(hqRoot, companyPath), "company");
6916
- const watcher = makeBatchWatcherStub();
6917
- let shutdown = () => { };
6918
- const commits = [];
6919
- let submittedAt = 0;
6920
- const runPass = vi.fn(async (argv) => {
6921
- if (!argv.includes("--scope-path")) {
6922
- readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6923
- readJournal("indigo");
6924
- }
6925
- if (argv.includes("--scope-path") && argv.includes("push")) {
6926
- const slug = argv.includes("--company") ? "indigo" : PERSONAL_VAULT_JOURNAL_SLUG;
6927
- const key = slug === "indigo" ? companyKey : personalPath;
6928
- const scoped = readJournalScoped(slug, [key]);
6929
- scoped.files[key] = {
6930
- ...scoped.files[key],
6931
- syncedAt: "2026-09-16T21:38:18.000Z",
6932
- };
6933
- writeJournal(slug, scoped);
6934
- commits.push({ slug, elapsedMs: performance.now() - submittedAt });
6935
- }
6936
- return completedPassOutcome();
6937
- });
6938
- const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6939
- runPass,
6940
- createWatcher: () => watcher,
6941
- sleep: () => new Promise(() => { }),
6942
- onShutdownSignal: (handler) => {
6943
- shutdown = handler;
6944
- return () => { };
6945
- },
6946
- });
6947
- const emitAndWait = async (relativePaths) => {
6948
- const before = commits.length;
6949
- submittedAt = performance.now();
6950
- const batch = {
6951
- paths: new Map(relativePaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6952
- changes: new Map(relativePaths.map((relativePath) => [
6953
- path.join(hqRoot, relativePath),
6954
- { kind: "change" },
6955
- ])),
6956
- };
6957
- watcher.emit(relativePaths[0], batch);
6958
- await vi.waitFor(() => expect(commits.length).toBe(before + relativePaths.length), {
6959
- timeout: 30_000,
6960
- interval: 10,
6961
- });
6962
- };
6963
- try {
6964
- await flushLoopMicrotasks(30);
6965
- runPass.mockClear();
6966
- resetStateStoreSnapshotRecoveryCountForTest();
6967
- const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6968
- eventLoopDelay.enable();
6969
- await emitAndWait([personalPath, companyPath]);
6970
- for (let round = 0; round < 3; round += 1) {
6971
- await emitAndWait([personalPath]);
6972
- await emitAndWait([companyPath]);
6973
- }
6974
- eventLoopDelay.disable();
6975
- const recoveryCount = getStateStoreSnapshotRecoveryCountForTest();
6976
- const maxDelayMs = Number(eventLoopDelay.max) / 1_000_000;
6977
- const maxCommitMs = Math.max(...commits.map((commit) => commit.elapsedMs));
6978
- console.info({ recoveryCount, maxDelayMs, maxCommitMs, commits: commits.length });
6979
- expect.soft(recoveryCount).toBe(0);
6980
- expect.soft(maxDelayMs).toBeLessThan(100);
6981
- expect.soft(maxCommitMs).toBeLessThan(1_000);
6982
- expect(commits.map((commit) => commit.slug)).toEqual([
6983
- PERSONAL_VAULT_JOURNAL_SLUG,
6984
- "indigo",
6985
- PERSONAL_VAULT_JOURNAL_SLUG,
6986
- "indigo",
6987
- PERSONAL_VAULT_JOURNAL_SLUG,
6988
- "indigo",
6989
- PERSONAL_VAULT_JOURNAL_SLUG,
6990
- "indigo",
6991
- ]);
6992
- }
6993
- finally {
6994
- shutdown();
6995
- await loop;
6996
- if (previousCoordinator === undefined)
6997
- delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6998
- else
6999
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
7000
- fs.rmSync(hqRoot, { recursive: true, force: true });
7001
- }
7002
- }, 180_000);
7003
6317
  it("fast lane: prefers declared receiver sizes and falls back to the journal", async () => {
7004
6318
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7005
6319
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
@@ -7175,19 +6489,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7175
6489
  ]),
7176
6490
  });
7177
6491
  await flushLoopMicrotasks(30);
7178
- // The full pass owns durable state until a checkpoint. Its queued realtime
7179
- // residue is promoted at that hand-off instead of overlapping the pass.
7180
- 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");
7181
6496
  releaseBulk();
7182
6497
  await flushLoopMicrotasks(30);
7183
6498
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
7184
- expect(calls.flat().join(" ")).toContain("realtime.md");
7185
6499
  expect(calls.flat().join(" ")).toContain("session-0.log");
7186
6500
  triggerShutdown();
7187
6501
  await loop;
7188
6502
  fs.rmSync(hqRoot, { recursive: true, force: true });
7189
6503
  });
7190
- 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 () => {
7191
6505
  const watcher = makeWatcherStub();
7192
6506
  let triggerShutdown = () => { };
7193
6507
  let receiverSync;
@@ -7232,7 +6546,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7232
6546
  watcher.emit();
7233
6547
  await flushLoopMicrotasks();
7234
6548
  expect(order).toEqual(["bulk"]);
7235
- const receiverPull = receiverSync({
6549
+ await receiverSync({
7236
6550
  events: [{
7237
6551
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
7238
6552
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -7240,11 +6554,8 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7240
6554
  }],
7241
6555
  signal: new AbortController().signal,
7242
6556
  });
7243
- await flushLoopMicrotasks(20);
7244
- expect(order).toEqual(["bulk"]);
7245
- releaseBulk();
7246
- await receiverPull;
7247
6557
  expect(order).toEqual(["bulk", "fast-pull"]);
6558
+ releaseBulk();
7248
6559
  await flushLoopMicrotasks();
7249
6560
  triggerShutdown();
7250
6561
  await loop;
@@ -7288,14 +6599,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7288
6599
  await flushLoopMicrotasks();
7289
6600
  watcher.emit();
7290
6601
  await flushLoopMicrotasks();
7291
- releaseBulk();
7292
- // The full chore has no explicit safe checkpoint in this stub, so the
7293
- // realtime pass begins only after the chore has released ownership.
7294
6602
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7295
6603
  await fastStarted;
6604
+ releaseBulk();
7296
6605
  await flushLoopMicrotasks(20);
7297
- // The realtime pass owns the root lock until its own durable work ends. A
7298
- // 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.
7299
6608
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
7300
6609
  releaseFast();
7301
6610
  await flushLoopMicrotasks(20);
@@ -7470,68 +6779,59 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7470
6779
  return () => { };
7471
6780
  },
7472
6781
  });
7473
- try {
7474
- await flushLoopMicrotasks();
7475
- watcher.emit();
7476
- await flushLoopMicrotasks();
7477
- runPass.mockClear();
7478
- const events = [
7479
- ...Array.from({ length: 17 }, (_, index) => ({
7480
- kind: "upsert",
7481
- relativePath: `companies/acme/a-${index}.md`,
7482
- contentHash: "sha256:x",
7483
- mtime: "2026-08-26T00:00:00.000Z",
7484
- originDeviceId: "peer",
7485
- originTenantId: "acme",
7486
- sequenceNumber: index,
7487
- eventTimestamp: "2026-08-26T00:00:00.000Z",
7488
- })),
7489
- ...Array.from({ length: 16 }, (_, index) => ({
7490
- kind: "upsert",
7491
- relativePath: `companies/beta/b-${index}.md`,
7492
- contentHash: "sha256:x",
7493
- mtime: "2026-08-26T00:00:00.000Z",
7494
- originDeviceId: "peer",
7495
- originTenantId: "beta",
7496
- sequenceNumber: index + 17,
7497
- eventTimestamp: "2026-08-26T00:00:00.000Z",
7498
- })),
7499
- ];
7500
- for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
7501
- const journal = readJournal(slug);
7502
- for (let index = 0; index < count; index++) {
7503
- journal.files[`${prefix}-${index}.md`] = {
7504
- hash: "sha256:x",
7505
- size: 16,
7506
- syncedAt: "2026-08-26T00:00:00.000Z",
7507
- direction: "down",
7508
- };
7509
- }
7510
- 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
+ };
7511
6817
  }
7512
- const receiverDrain = receiverSync({
7513
- events,
7514
- signal: new AbortController().signal,
7515
- });
7516
- await flushLoopMicrotasks(20);
7517
- // The running full chore has not yet yielded a durable hand-off, so no
7518
- // receiver work may overlap it. Once it releases ownership, the
7519
- // scheduler admits each route's bounded work before the residual slow
7520
- // beta batch (acme fast, beta fast, then beta slow).
7521
- expect(runPass).not.toHaveBeenCalled();
7522
- releaseBulk();
7523
- await receiverDrain;
7524
- const scopedCalls = runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"));
7525
- expect(scopedCalls).toHaveLength(3);
7526
- expect(scopedCalls.flat().join(" ")).toContain("a-16.md");
7527
- expect(scopedCalls.flat().join(" ")).toContain("b-14.md");
7528
- }
7529
- finally {
7530
- releaseBulk();
7531
- triggerShutdown();
7532
- await loop;
7533
- fs.rmSync(hqRoot, { recursive: true, force: true });
6818
+ writeJournal(slug, journal);
7534
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 });
7535
6835
  });
7536
6836
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
7537
6837
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7703,12 +7003,9 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7703
7003
  await flushLoopMicrotasks();
7704
7004
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7705
7005
  await flushLoopMicrotasks(30);
7706
- // The full pass has not reached a durable checkpoint, so it remains the
7707
- // only journal writer. The scoped realtime pass is admitted immediately
7708
- // after this safe hand-off, preserving both sets of rows.
7709
- expect(Object.keys(readJournal("indigo").files)).toHaveLength(0);
7006
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
7710
7007
  releaseBulk();
7711
- await flushLoopMicrotasks(30);
7008
+ await flushLoopMicrotasks();
7712
7009
  const journal = readJournal("indigo").files;
7713
7010
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7714
7011
  for (let index = 0; index < paths.length; index++) {
@@ -7760,12 +7057,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7760
7057
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7761
7058
  }
7762
7059
  await flushLoopMicrotasks(60);
7763
- // A full pass without a checkpoint remains the sole mutable-state owner.
7764
- // The queued realtime batches run immediately after it completes.
7765
- expect(completed).toEqual([]);
7060
+ expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
7766
7061
  releaseBulk();
7767
- await flushLoopMicrotasks(60);
7768
- expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7062
+ await flushLoopMicrotasks();
7063
+ expect(completed).toContain("bulk");
7769
7064
  triggerShutdown();
7770
7065
  await loop;
7771
7066
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -8083,55 +7378,6 @@ describe("runRunnerWithLoop — event-push wiring", () => {
8083
7378
  expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
8084
7379
  expect(watcher.disposed).toBe(false);
8085
7380
  });
8086
- it("keeps the watcher alive for an unresolved company watch route (HQ-SYNC-COMPANY-ROUTE-UID)", async () => {
8087
- const watcher = makeWatcherStub();
8088
- const client = makeVaultStub({
8089
- entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
8090
- });
8091
- const findInMyNamespace = vi.fn().mockResolvedValue(null);
8092
- client.entity.findInMyNamespace = findInMyNamespace;
8093
- const routedPass = makeDeps({ createVaultClient: () => client });
8094
- const routedArgv = buildRoutePullArgv({ kind: "company", slug: "cmp_ghost" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
8095
- let attempts = 0;
8096
- let routedExit;
8097
- const runPass = vi.fn().mockImplementation(async () => {
8098
- attempts += 1;
8099
- if (attempts === 1) {
8100
- routedExit = await runRunner(routedArgv, routedPass);
8101
- return routedExit;
8102
- }
8103
- // Terminate the otherwise-open loop only after it proves the unresolved
8104
- // route did not make the watcher exit.
8105
- return 1;
8106
- });
8107
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8108
- runPass,
8109
- clock: new FakeClock(),
8110
- createWatcher: () => watcher,
8111
- sleep: () => Promise.resolve(),
8112
- onShutdownSignal: () => () => { },
8113
- });
8114
- expect(routedArgv).toEqual([
8115
- "--company",
8116
- "cmp_ghost",
8117
- "--direction",
8118
- "pull",
8119
- "--hq-root",
8120
- "/tmp/hq",
8121
- ]);
8122
- expect(routedExit).toBe(UNRESOLVED_COMPANY_EXIT);
8123
- expect(runPass).toHaveBeenCalledTimes(2);
8124
- expect(code).toBe(1);
8125
- expect(code).not.toBe(UNRESOLVED_COMPANY_EXIT);
8126
- expect(watcher.disposed).toBe(false);
8127
- expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
8128
- expect(routedPass.stderr.events()).toContainEqual({
8129
- type: "error",
8130
- company: "cmp_ghost",
8131
- path: "(company-resolution)",
8132
- message: "company entity lookup returned 404",
8133
- });
8134
- });
8135
7381
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
8136
7382
  const watcher = makeWatcherStub();
8137
7383
  const dnsError = Object.assign(new Error("getaddrinfo ENOTFOUND vault"), {
@@ -8425,85 +7671,6 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
8425
7671
  });
8426
7672
  });
8427
7673
  describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
8428
- it("hands a wake realtime drain to the guard before a long-running full chore completes", async () => {
8429
- const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8430
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
8431
- const watcher = makeWatcherStub();
8432
- let triggerShutdown = () => { };
8433
- let releaseCheckpoint = () => { };
8434
- const checkpointGate = new Promise((resolve) => { releaseCheckpoint = resolve; });
8435
- let releaseRealtime = () => { };
8436
- const realtimeGate = new Promise((resolve) => { releaseRealtime = resolve; });
8437
- const order = [];
8438
- let passes = 0;
8439
- let runRealtime = null;
8440
- const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8441
- runPass: vi.fn().mockImplementation(async (_argv, checkpoint) => {
8442
- passes += 1;
8443
- if (passes === 2) {
8444
- order.push("chore");
8445
- // The fake pass has completed its current durable unit and is now
8446
- // at the same explicit hand-off seam production uses between
8447
- // fully-settled fanout targets.
8448
- await checkpointGate;
8449
- await checkpoint?.();
8450
- order.push("chore-complete");
8451
- }
8452
- return completedPassOutcome();
8453
- }),
8454
- runPassSupportsCooperativeCheckpoint: true,
8455
- clock: new FakeClock(),
8456
- createWatcher: () => watcher,
8457
- sleep: makeSteppableSleep().sleep,
8458
- onShutdownSignal: (handler) => {
8459
- triggerShutdown = handler;
8460
- return () => { };
8461
- },
8462
- startRealtimeScheduler: async (opts) => {
8463
- // This is the same wake path used by the realtime scheduler: it
8464
- // enters runGuardedTask behind the coordinator worker.
8465
- runRealtime = () => opts.runGuarded(async () => {
8466
- order.push("realtime");
8467
- await realtimeGate;
8468
- order.push("realtime-complete");
8469
- });
8470
- return {
8471
- signal: () => undefined,
8472
- checkHighWater: async () => undefined,
8473
- dispose: async () => undefined,
8474
- };
8475
- },
8476
- });
8477
- try {
8478
- await flushLoopMicrotasks(30);
8479
- watcher.emit();
8480
- await flushLoopMicrotasks(20);
8481
- expect(order).toEqual(["chore"]);
8482
- const realtime = runRealtime();
8483
- releaseCheckpoint();
8484
- await flushLoopMicrotasks(30);
8485
- expect(order).toEqual(["chore", "realtime"]);
8486
- releaseRealtime();
8487
- await realtime;
8488
- await flushLoopMicrotasks(20);
8489
- expect(order).toEqual([
8490
- "chore",
8491
- "realtime",
8492
- "realtime-complete",
8493
- "chore-complete",
8494
- ]);
8495
- }
8496
- finally {
8497
- releaseCheckpoint();
8498
- releaseRealtime();
8499
- triggerShutdown();
8500
- await loop;
8501
- if (previousCoordinator === undefined)
8502
- delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8503
- else
8504
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
8505
- }
8506
- });
8507
7674
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
8508
7675
  const watcher = makeWatcherStub();
8509
7676
  let triggerShutdown = () => { };
@@ -8616,173 +7783,6 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
8616
7783
  await loop;
8617
7784
  });
8618
7785
  });
8619
- describe("CooperativePassScheduler", () => {
8620
- it("admits an explicitly safe fast realtime pass while a slow scoped realtime pass remains in flight", async () => {
8621
- const scheduler = new CooperativePassScheduler(() => 0);
8622
- const order = [];
8623
- let releaseSlow = () => { };
8624
- const slowGate = new Promise((resolve) => { releaseSlow = resolve; });
8625
- let markSlowStarted = () => { };
8626
- const slowStarted = new Promise((resolve) => { markSlowStarted = resolve; });
8627
- const slow = scheduler.submit(async () => {
8628
- order.push("slow-start");
8629
- markSlowStarted();
8630
- await slowGate;
8631
- order.push("slow-complete");
8632
- return 0;
8633
- }, "realtime", "slow");
8634
- await slowStarted;
8635
- const fast = scheduler.submit(async () => {
8636
- order.push("fast");
8637
- return 0;
8638
- }, "realtime", "fast", true);
8639
- try {
8640
- await Promise.resolve();
8641
- await Promise.resolve();
8642
- expect([...order]).toEqual(["slow-start", "fast"]);
8643
- }
8644
- finally {
8645
- releaseSlow();
8646
- await Promise.all([slow, fast]);
8647
- }
8648
- });
8649
- it("preserves journal order and resumes full work across repeated randomized hand-offs", async () => {
8650
- // These deterministic seeds deliberately vary the number and enqueue
8651
- // ordering of realtime tasks. Every append asserts one durable-state owner
8652
- // at a time; the full continuation must resume after every realtime burst.
8653
- for (let seed = 1; seed <= 32; seed += 1) {
8654
- let now = 0;
8655
- const scheduler = new CooperativePassScheduler(() => now);
8656
- const journal = [];
8657
- const expected = new Set();
8658
- let writer = null;
8659
- let maximumWriters = 0;
8660
- const append = async (entry) => {
8661
- expect(writer).toBeNull();
8662
- writer = entry;
8663
- maximumWriters = Math.max(maximumWriters, 1);
8664
- // A journal append crosses an async durable-write boundary. If the
8665
- // scheduler admitted another pass here, its ownership assertion would
8666
- // fail rather than merely observing JavaScript's synchronous turn.
8667
- await Promise.resolve();
8668
- expect(writer).toBe(entry);
8669
- journal.push(entry);
8670
- writer = null;
8671
- };
8672
- let releaseCheckpoint = () => { };
8673
- const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8674
- const chore = scheduler.submit(async ({ checkpoint }) => {
8675
- expected.add(`chore-${seed}-before`);
8676
- await append(`chore-${seed}-before`);
8677
- await checkpointReady;
8678
- await checkpoint();
8679
- expected.add(`chore-${seed}-after`);
8680
- await append(`chore-${seed}-after`);
8681
- return 0;
8682
- }, "chore");
8683
- await Promise.resolve();
8684
- const realtimeCount = (seed % 7) + 1;
8685
- const shuffledIndexes = Array.from({ length: realtimeCount }, (_, index) => index);
8686
- let randomState = seed;
8687
- for (let index = shuffledIndexes.length - 1; index > 0; index -= 1) {
8688
- randomState = (randomState * 1_664_525 + 1_013_904_223) >>> 0;
8689
- const swapIndex = randomState % (index + 1);
8690
- [shuffledIndexes[index], shuffledIndexes[swapIndex]] = [
8691
- shuffledIndexes[swapIndex],
8692
- shuffledIndexes[index],
8693
- ];
8694
- }
8695
- const realtime = shuffledIndexes.map((index) => {
8696
- const entry = `realtime-${seed}-${(index * 17 + seed) % realtimeCount}`;
8697
- expected.add(entry);
8698
- return scheduler.submit(async () => {
8699
- // Shuffle the microtask timing without ever allowing a second writer.
8700
- if ((seed + index) % 3 === 0)
8701
- await Promise.resolve();
8702
- await append(entry);
8703
- now += 1;
8704
- return 0;
8705
- }, "realtime");
8706
- });
8707
- releaseCheckpoint();
8708
- await Promise.all([chore, ...realtime]);
8709
- expect(maximumWriters).toBe(1);
8710
- expect(new Set(journal)).toEqual(expected);
8711
- expect(journal).toHaveLength(expected.size);
8712
- expect(journal[0]).toBe(`chore-${seed}-before`);
8713
- expect(journal.at(-1)).toBe(`chore-${seed}-after`);
8714
- }
8715
- });
8716
- it("lets realtime win at 15 minutes minus one millisecond, then gives the chore the exact-boundary admission", async () => {
8717
- const runCase = async (elapsedMs) => {
8718
- let now = 0;
8719
- const scheduler = new CooperativePassScheduler(() => now);
8720
- const order = [];
8721
- let releaseActive = () => { };
8722
- const activeGate = new Promise((resolve) => { releaseActive = resolve; });
8723
- const activeRealtime = scheduler.submit(async () => {
8724
- order.push("active-realtime");
8725
- await activeGate;
8726
- return 0;
8727
- }, "realtime");
8728
- await Promise.resolve();
8729
- const chore = scheduler.submit(async () => {
8730
- order.push("chore");
8731
- return 0;
8732
- }, "chore");
8733
- const queuedRealtime = scheduler.submit(async () => {
8734
- order.push("queued-realtime");
8735
- return 0;
8736
- }, "realtime");
8737
- now = elapsedMs;
8738
- releaseActive();
8739
- await Promise.all([activeRealtime, chore, queuedRealtime]);
8740
- return order;
8741
- };
8742
- await expect(runCase(CHORE_STARVATION_CAP_MS - 1)).resolves.toEqual([
8743
- "active-realtime",
8744
- "queued-realtime",
8745
- "chore",
8746
- ]);
8747
- await expect(runCase(CHORE_STARVATION_CAP_MS)).resolves.toEqual([
8748
- "active-realtime",
8749
- "chore",
8750
- "queued-realtime",
8751
- ]);
8752
- });
8753
- it("resumes a handed-off chore when a continuous realtime burst reaches the 15-minute cap", async () => {
8754
- let now = 0;
8755
- const scheduler = new CooperativePassScheduler(() => now);
8756
- const order = [];
8757
- let releaseCheckpoint = () => { };
8758
- const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8759
- const chore = scheduler.submit(async ({ checkpoint }) => {
8760
- order.push("chore-before");
8761
- await checkpointReady;
8762
- await checkpoint();
8763
- order.push("chore-after");
8764
- return 0;
8765
- }, "chore");
8766
- await Promise.resolve();
8767
- const firstRealtime = scheduler.submit(async () => {
8768
- order.push("realtime-before-cap");
8769
- now = CHORE_STARVATION_CAP_MS;
8770
- return 0;
8771
- }, "realtime");
8772
- const laterRealtime = scheduler.submit(async () => {
8773
- order.push("realtime-after-cap");
8774
- return 0;
8775
- }, "realtime");
8776
- releaseCheckpoint();
8777
- await Promise.all([chore, firstRealtime, laterRealtime]);
8778
- expect(order).toEqual([
8779
- "chore-before",
8780
- "realtime-before-cap",
8781
- "chore-after",
8782
- "realtime-after-cap",
8783
- ]);
8784
- });
8785
- });
8786
7786
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8787
7787
  const watchArgv = [
8788
7788
  "--companies",