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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/dist/backup-prune.d.ts +0 -20
  2. package/dist/backup-prune.d.ts.map +1 -1
  3. package/dist/backup-prune.js +3 -62
  4. package/dist/backup-prune.js.map +1 -1
  5. package/dist/backup-prune.test.js +0 -25
  6. package/dist/backup-prune.test.js.map +1 -1
  7. package/dist/bin/rescue-runner.js +1 -2
  8. package/dist/bin/rescue-runner.js.map +1 -1
  9. package/dist/bin/sync-runner-company.d.ts +6 -0
  10. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-company.js +30 -1
  12. package/dist/bin/sync-runner-company.js.map +1 -1
  13. package/dist/bin/sync-runner-company.test.js +55 -0
  14. package/dist/bin/sync-runner-company.test.js.map +1 -1
  15. package/dist/bin/sync-runner-planning.d.ts +12 -3
  16. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  17. package/dist/bin/sync-runner-planning.js +65 -3
  18. package/dist/bin/sync-runner-planning.js.map +1 -1
  19. package/dist/bin/sync-runner-planning.test.js +108 -1
  20. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  21. package/dist/bin/sync-runner-watch-loop.d.ts +48 -2
  22. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  23. package/dist/bin/sync-runner-watch-loop.js +318 -120
  24. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  25. package/dist/bin/sync-runner.d.ts +33 -5
  26. package/dist/bin/sync-runner.d.ts.map +1 -1
  27. package/dist/bin/sync-runner.js +34 -4
  28. package/dist/bin/sync-runner.js.map +1 -1
  29. package/dist/bin/sync-runner.test.js +1079 -79
  30. package/dist/bin/sync-runner.test.js.map +1 -1
  31. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +2 -0
  32. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +1 -0
  33. package/dist/cli/onepass-pull-planner-regressions.test.js +300 -0
  34. package/dist/cli/onepass-pull-planner-regressions.test.js.map +1 -0
  35. package/dist/cli/onepass-pull-planner.test.d.ts +2 -0
  36. package/dist/cli/onepass-pull-planner.test.d.ts.map +1 -0
  37. package/dist/cli/onepass-pull-planner.test.js +266 -0
  38. package/dist/cli/onepass-pull-planner.test.js.map +1 -0
  39. package/dist/cli/rescue-core.d.ts +0 -40
  40. package/dist/cli/rescue-core.d.ts.map +1 -1
  41. package/dist/cli/rescue-core.js +109 -1205
  42. package/dist/cli/rescue-core.js.map +1 -1
  43. package/dist/cli/rescue-rsync-preflight.test.js +4 -256
  44. package/dist/cli/rescue-rsync-preflight.test.js.map +1 -1
  45. package/dist/cli/rescue.d.ts +0 -4
  46. package/dist/cli/rescue.d.ts.map +1 -1
  47. package/dist/cli/rescue.js +1 -20
  48. package/dist/cli/rescue.js.map +1 -1
  49. package/dist/cli/rescue.test.js +1 -16
  50. package/dist/cli/rescue.test.js.map +1 -1
  51. package/dist/cli/share-presign-transport.test.js +0 -2
  52. package/dist/cli/share-presign-transport.test.js.map +1 -1
  53. package/dist/cli/share.d.ts +19 -0
  54. package/dist/cli/share.d.ts.map +1 -1
  55. package/dist/cli/share.js +57 -37
  56. package/dist/cli/share.js.map +1 -1
  57. package/dist/cli/share.test.js +87 -93
  58. package/dist/cli/share.test.js.map +1 -1
  59. package/dist/cli/streaming-pull-planner.bench.test.d.ts +15 -0
  60. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +1 -0
  61. package/dist/cli/streaming-pull-planner.bench.test.js +273 -0
  62. package/dist/cli/streaming-pull-planner.bench.test.js.map +1 -0
  63. package/dist/cli/sync-scope.test.js +8 -0
  64. package/dist/cli/sync-scope.test.js.map +1 -1
  65. package/dist/cli/sync.d.ts +12 -0
  66. package/dist/cli/sync.d.ts.map +1 -1
  67. package/dist/cli/sync.js +226 -118
  68. package/dist/cli/sync.js.map +1 -1
  69. package/dist/cli/sync.test.js +215 -2
  70. package/dist/cli/sync.test.js.map +1 -1
  71. package/dist/cli/watch-event-push-conflict.test.js +0 -2
  72. package/dist/cli/watch-event-push-conflict.test.js.map +1 -1
  73. package/dist/cognito-auth.d.ts +0 -30
  74. package/dist/cognito-auth.d.ts.map +1 -1
  75. package/dist/cognito-auth.js +0 -175
  76. package/dist/cognito-auth.js.map +1 -1
  77. package/dist/index.d.ts +2 -2
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +1 -1
  80. package/dist/index.js.map +1 -1
  81. package/dist/journal-row-store.d.ts +9 -5
  82. package/dist/journal-row-store.d.ts.map +1 -1
  83. package/dist/journal-row-store.js +70 -10
  84. package/dist/journal-row-store.js.map +1 -1
  85. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  86. package/dist/journal-snapshot-v4.js +11 -6
  87. package/dist/journal-snapshot-v4.js.map +1 -1
  88. package/dist/journal-store.test.js +259 -7
  89. package/dist/journal-store.test.js.map +1 -1
  90. package/dist/journal.d.ts +43 -15
  91. package/dist/journal.d.ts.map +1 -1
  92. package/dist/journal.js +297 -79
  93. package/dist/journal.js.map +1 -1
  94. package/dist/lib/exit-codes.d.ts +15 -0
  95. package/dist/lib/exit-codes.d.ts.map +1 -1
  96. package/dist/lib/exit-codes.js +15 -0
  97. package/dist/lib/exit-codes.js.map +1 -1
  98. package/dist/machine-auth.test.js +0 -295
  99. package/dist/machine-auth.test.js.map +1 -1
  100. package/dist/object-io.d.ts +2 -2
  101. package/dist/object-io.d.ts.map +1 -1
  102. package/dist/object-io.js +3 -18
  103. package/dist/object-io.js.map +1 -1
  104. package/dist/personal-vault.d.ts.map +1 -1
  105. package/dist/personal-vault.js +10 -3
  106. package/dist/personal-vault.js.map +1 -1
  107. package/dist/personal-vault.test.js +35 -0
  108. package/dist/personal-vault.test.js.map +1 -1
  109. package/dist/remote-pull.d.ts.map +1 -1
  110. package/dist/remote-pull.js +9 -2
  111. package/dist/remote-pull.js.map +1 -1
  112. package/dist/remote-pull.test.js +59 -2
  113. package/dist/remote-pull.test.js.map +1 -1
  114. package/dist/s3.d.ts +1 -18
  115. package/dist/s3.d.ts.map +1 -1
  116. package/dist/s3.js +24 -47
  117. package/dist/s3.js.map +1 -1
  118. package/dist/s3.list-cost.test.d.ts +2 -0
  119. package/dist/s3.list-cost.test.d.ts.map +1 -0
  120. package/dist/s3.list-cost.test.js +51 -0
  121. package/dist/s3.list-cost.test.js.map +1 -0
  122. package/dist/s3.test.js +1 -27
  123. package/dist/s3.test.js.map +1 -1
  124. package/dist/sync/area-ledger.js +4 -4
  125. package/dist/sync/area-ledger.js.map +1 -1
  126. package/dist/sync/parallel-sync-coordinator.test.js +29 -0
  127. package/dist/sync/parallel-sync-coordinator.test.js.map +1 -1
  128. package/dist/sync/realtime-telemetry.d.ts +1 -1
  129. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  130. package/dist/sync/realtime-telemetry.js +14 -6
  131. package/dist/sync/realtime-telemetry.js.map +1 -1
  132. package/dist/sync/realtime-telemetry.test.js +136 -16
  133. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  134. package/dist/sync/remote-file-spool.d.ts +16 -0
  135. package/dist/sync/remote-file-spool.d.ts.map +1 -0
  136. package/dist/sync/remote-file-spool.js +91 -0
  137. package/dist/sync/remote-file-spool.js.map +1 -0
  138. package/dist/sync/remote-file-spool.test.d.ts +2 -0
  139. package/dist/sync/remote-file-spool.test.d.ts.map +1 -0
  140. package/dist/sync/remote-file-spool.test.js +77 -0
  141. package/dist/sync/remote-file-spool.test.js.map +1 -0
  142. package/dist/sync-core.d.ts +4 -6
  143. package/dist/sync-core.d.ts.map +1 -1
  144. package/dist/sync-core.js +4 -10
  145. package/dist/sync-core.js.map +1 -1
  146. package/dist/sync-core.test.js +1 -25
  147. package/dist/sync-core.test.js.map +1 -1
  148. package/package.json +2 -1
  149. package/dist/cli/rescue-breaker.test.d.ts +0 -2
  150. package/dist/cli/rescue-breaker.test.d.ts.map +0 -1
  151. package/dist/cli/rescue-breaker.test.js +0 -334
  152. package/dist/cli/rescue-breaker.test.js.map +0 -1
  153. package/dist/cli/rescue-realpath.test.d.ts +0 -2
  154. package/dist/cli/rescue-realpath.test.d.ts.map +0 -1
  155. package/dist/cli/rescue-realpath.test.js +0 -109
  156. package/dist/cli/rescue-realpath.test.js.map +0 -1
  157. package/dist/cli/rescue-snapshot-copy.test.d.ts +0 -2
  158. package/dist/cli/rescue-snapshot-copy.test.d.ts.map +0 -1
  159. package/dist/cli/rescue-snapshot-copy.test.js +0 -435
  160. package/dist/cli/rescue-snapshot-copy.test.js.map +0 -1
  161. package/dist/cli/rescue-snapshot-recovery.test.d.ts +0 -2
  162. package/dist/cli/rescue-snapshot-recovery.test.d.ts.map +0 -1
  163. package/dist/cli/rescue-snapshot-recovery.test.js +0 -260
  164. package/dist/cli/rescue-snapshot-recovery.test.js.map +0 -1
  165. package/dist/cli/rescue-source-switch-floor.test.d.ts +0 -2
  166. package/dist/cli/rescue-source-switch-floor.test.d.ts.map +0 -1
  167. package/dist/cli/rescue-source-switch-floor.test.js +0 -210
  168. package/dist/cli/rescue-source-switch-floor.test.js.map +0 -1
@@ -10,6 +10,9 @@ 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";
13
16
  import { runRunner, runRunnerWithLoop, resolveDeletePolicy, resolveSkipPersonal, resolveSkipCompanies, selectObjectIOFactory, routeChangeToTarget, buildTargetedPushArgv, resolvePullScope, readPinnedPrefixes, SESSIONS_SCOPE_PREFIX, defaultCollectTelemetry, AUTH_REQUIRED_PASS_EXIT, } from "./sync-runner.js";
14
17
  import { SESSION_LOGS_PUSH_ONLY_PREFIX } from "../sync/pull-scope.js";
15
18
  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";
@@ -17,17 +20,20 @@ import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
17
20
  import { setBandwidthGovernorForTesting } from "../bandwidth.js";
18
21
  import { POST_SYNC_FLAG_KEYS, } from "./sync-runner-post-sync-flags.js";
19
22
  import { FakeClock, TreeWatcher, } from "../watcher.js";
20
- import { adaptivePollMs, advanceMonotonicDeadline, DEFAULT_EVENT_BATCH_LIMIT, DEFAULT_FAST_LANE_MAX_BYTES, DEFAULT_FAST_LANE_MAX_FILE_BYTES, DEFAULT_FAST_LANE_MAX_FILES, DEFAULT_FULL_RECONCILE_MS, EVENT_BATCH_LIMIT_ENV, FAST_LANE_MAX_BYTES_ENV, FAST_LANE_MAX_FILES_ENV, FULL_RECONCILE_MS_ENV, isFastLocalBatch, isFastReceiverBatch, partitionLocalBatch, journalEntriesForWatcherDelete, resolveEventBatchLimit, resolveFullReconcileMs, resolveWatchPollMs, normalizeCoordinatorPaths, shouldQuarantineWatcherDeleteIntents, shouldUseAreaStorage, WATCHER_REDISCOVERY_RETRY_MS, WATCHER_REDISCOVERY_RETRY_MAX_MS, } from "./sync-runner-watch-loop.js";
21
- import { AreaLedgerMigration, JournalStore, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, writeJournal, } from "../journal.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";
22
26
  import { AreaResolver } from "../sync/area-resolver.js";
27
+ import { AREA_LEGACY_MIRROR_LINGER_MS } from "../sync/area-ledger-migration.js";
23
28
  import { AreaMaintenanceCursorStore } from "../sync/per-area-maintenance.js";
24
29
  import { HQ_CLOUD_VERSION } from "../version.js";
25
30
  import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
26
31
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
27
32
  import { getStateStoreSnapshotRecoveryCountForTest, resetStateStoreSnapshotRecoveryCountForTest, StateStoreLockError, } from "../sync/state-store.js";
28
- import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
33
+ import { PARTIAL_SYNC_EXIT, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
29
34
  import { isCoveredByAny } from "../prefix-coalesce.js";
30
35
  import { VaultAuthError, VaultNotFoundError } from "../vault-client.js";
36
+ import { buildRoutePullArgv } from "./sync-runner-watch-routes.js";
31
37
  import { CognitoAuthError, CognitoRefreshError } from "../cognito-auth.js";
32
38
  import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
33
39
  import yaml from "js-yaml";
@@ -162,6 +168,7 @@ function makeVaultStub(opts = {}) {
162
168
  bucketName: `bucket-${uid}`,
163
169
  status: "active",
164
170
  })),
171
+ findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
165
172
  listByType: opts.listPersons ??
166
173
  (() => Promise.resolve([])),
167
174
  },
@@ -954,6 +961,164 @@ describe("target resolution", () => {
954
961
  const call = deps.sync.mock.calls[0][0];
955
962
  expect(call.company).toBe("cmp_abc");
956
963
  });
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
+ });
957
1122
  });
958
1123
  // ---------------------------------------------------------------------------
959
1124
  // --personal mode (personal-vault-only, skip company fanout)
@@ -4163,6 +4328,26 @@ async function flushLoopMicrotasks(n = 8) {
4163
4328
  for (let i = 0; i < n; i++)
4164
4329
  await Promise.resolve();
4165
4330
  }
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
+ }
4166
4351
  function makeSteppableSleep() {
4167
4352
  const pending = [];
4168
4353
  return {
@@ -5586,7 +5771,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5586
5771
  });
5587
5772
  await expect(superseded).resolves.toBeUndefined();
5588
5773
  expect(runPass.mock.calls.filter(([argv]) => argv.includes("superseded.md"))).toHaveLength(0);
5589
- expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":1,"source":"receiver","direction":"pull","outcome":"refused"');
5774
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":2,"source":"receiver","direction":"pull","outcome":"refused"');
5590
5775
  releaseHeld();
5591
5776
  await heldReceiverWork;
5592
5777
  await replacement;
@@ -5899,7 +6084,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5899
6084
  triggerShutdown();
5900
6085
  await loop;
5901
6086
  });
5902
- it("fast lane: a small watcher batch dispatches during a slow bulk pass", async () => {
6087
+ it("fast lane: a small watcher batch waits for a full pass that has not reached a hand-off checkpoint", async () => {
5903
6088
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
5904
6089
  const relativePath = "companies/indigo/realtime.md";
5905
6090
  const absolutePath = path.join(hqRoot, relativePath);
@@ -5939,15 +6124,17 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5939
6124
  expect(order).toEqual(["bulk"]);
5940
6125
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5941
6126
  await flushLoopMicrotasks(30);
5942
- // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
5943
- expect(order).toEqual(["bulk", "fast", "fast"]);
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"]);
5944
6130
  releaseBulk();
5945
- await flushLoopMicrotasks();
6131
+ await flushLoopMicrotasks(30);
6132
+ expect(order).toEqual(["bulk", "fast", "fast"]);
5946
6133
  triggerShutdown();
5947
6134
  await loop;
5948
6135
  fs.rmSync(hqRoot, { recursive: true, force: true });
5949
6136
  });
5950
- it("parallel coordinator: a watcher push starts while a full chore is blocked", async () => {
6137
+ it("parallel coordinator: a watcher push waits for a full chore without a hand-off checkpoint", async () => {
5951
6138
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
5952
6139
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
5953
6140
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -5990,9 +6177,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5990
6177
  expect(order).toEqual(["chore"]);
5991
6178
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5992
6179
  await flushLoopMicrotasks(30);
5993
- expect(order).toEqual(["chore", "realtime"]);
6180
+ expect(order).toEqual(["chore"]);
5994
6181
  releaseBulk();
5995
- await flushLoopMicrotasks();
6182
+ await flushLoopMicrotasks(30);
6183
+ expect(order).toEqual(["chore", "realtime"]);
5996
6184
  triggerShutdown();
5997
6185
  await loop;
5998
6186
  }
@@ -6077,7 +6265,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6077
6265
  fs.rmSync(hqRoot, { recursive: true, force: true });
6078
6266
  }
6079
6267
  });
6080
- it("parallel coordinator partitions receiver events into fast and slow realtime lanes", async () => {
6268
+ it("parallel coordinator partitions receiver events into fast and slow realtime lanes without overlapping their durable writes", async () => {
6081
6269
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6082
6270
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6083
6271
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6148,7 +6336,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6148
6336
  signal: new AbortController().signal,
6149
6337
  });
6150
6338
  await flushLoopMicrotasks(20);
6151
- expect(order).toEqual(["slow-held", "fast-small"]);
6339
+ expect(order).toEqual(["slow-held"]);
6152
6340
  releaseSlow();
6153
6341
  await held;
6154
6342
  await partitioned;
@@ -6314,6 +6502,504 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6314
6502
  fs.rmSync(hqRoot, { recursive: true, force: true });
6315
6503
  }
6316
6504
  });
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);
6317
7003
  it("fast lane: prefers declared receiver sizes and falls back to the journal", async () => {
6318
7004
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6319
7005
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
@@ -6489,19 +7175,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6489
7175
  ]),
6490
7176
  });
6491
7177
  await flushLoopMicrotasks(30);
6492
- // origin/main failure: expected a scoped tiny path while the bulk pass is
6493
- // held, but #408 kept all four paths in the slow lane.
6494
- expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(1);
6495
- expect(calls.flat().join(" ")).toContain("realtime.md");
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);
6496
7181
  releaseBulk();
6497
7182
  await flushLoopMicrotasks(30);
6498
7183
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
7184
+ expect(calls.flat().join(" ")).toContain("realtime.md");
6499
7185
  expect(calls.flat().join(" ")).toContain("session-0.log");
6500
7186
  triggerShutdown();
6501
7187
  await loop;
6502
7188
  fs.rmSync(hqRoot, { recursive: true, force: true });
6503
7189
  });
6504
- it("fast lane: a small receiver pull dispatches during a slow bulk pass", async () => {
7190
+ it("fast lane: a small receiver pull waits for a slow full pass without a hand-off checkpoint", async () => {
6505
7191
  const watcher = makeWatcherStub();
6506
7192
  let triggerShutdown = () => { };
6507
7193
  let receiverSync;
@@ -6546,7 +7232,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6546
7232
  watcher.emit();
6547
7233
  await flushLoopMicrotasks();
6548
7234
  expect(order).toEqual(["bulk"]);
6549
- await receiverSync({
7235
+ const receiverPull = receiverSync({
6550
7236
  events: [{
6551
7237
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
6552
7238
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -6554,8 +7240,11 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6554
7240
  }],
6555
7241
  signal: new AbortController().signal,
6556
7242
  });
6557
- expect(order).toEqual(["bulk", "fast-pull"]);
7243
+ await flushLoopMicrotasks(20);
7244
+ expect(order).toEqual(["bulk"]);
6558
7245
  releaseBulk();
7246
+ await receiverPull;
7247
+ expect(order).toEqual(["bulk", "fast-pull"]);
6559
7248
  await flushLoopMicrotasks();
6560
7249
  triggerShutdown();
6561
7250
  await loop;
@@ -6599,12 +7288,14 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6599
7288
  await flushLoopMicrotasks();
6600
7289
  watcher.emit();
6601
7290
  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.
6602
7294
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6603
7295
  await fastStarted;
6604
- releaseBulk();
6605
7296
  await flushLoopMicrotasks(20);
6606
- // The slow pass has finished, but its fast bypass still mutates. A second
6607
- // sync/rescue must not acquire the root operation lock in this interval.
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.
6608
7299
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
6609
7300
  releaseFast();
6610
7301
  await flushLoopMicrotasks(20);
@@ -6779,59 +7470,68 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6779
7470
  return () => { };
6780
7471
  },
6781
7472
  });
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
- };
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);
6817
7511
  }
6818
- writeJournal(slug, journal);
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 });
6819
7534
  }
6820
- const receiverDrain = receiverSync({
6821
- events,
6822
- signal: new AbortController().signal,
6823
- });
6824
- await flushLoopMicrotasks(20);
6825
- // The 33-event mixed delivery peels its first 32 bounded paths into the
6826
- // fast lane (17 acme + 15 beta); the final beta event remains slow.
6827
- expect(runPass).toHaveBeenCalledTimes(2);
6828
- expect(runPass.mock.calls.flat().join(" ")).toContain("a-16.md");
6829
- expect(runPass.mock.calls.flat().join(" ")).toContain("b-14.md");
6830
- releaseBulk();
6831
- await receiverDrain;
6832
- triggerShutdown();
6833
- await loop;
6834
- fs.rmSync(hqRoot, { recursive: true, force: true });
6835
7535
  });
6836
7536
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
6837
7537
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7003,9 +7703,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7003
7703
  await flushLoopMicrotasks();
7004
7704
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7005
7705
  await flushLoopMicrotasks(30);
7006
- expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
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);
7007
7710
  releaseBulk();
7008
- await flushLoopMicrotasks();
7711
+ await flushLoopMicrotasks(30);
7009
7712
  const journal = readJournal("indigo").files;
7010
7713
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7011
7714
  for (let index = 0; index < paths.length; index++) {
@@ -7057,10 +7760,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7057
7760
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7058
7761
  }
7059
7762
  await flushLoopMicrotasks(60);
7060
- expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
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([]);
7061
7766
  releaseBulk();
7062
- await flushLoopMicrotasks();
7063
- expect(completed).toContain("bulk");
7767
+ await flushLoopMicrotasks(60);
7768
+ expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7064
7769
  triggerShutdown();
7065
7770
  await loop;
7066
7771
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -7378,6 +8083,55 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7378
8083
  expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
7379
8084
  expect(watcher.disposed).toBe(false);
7380
8085
  });
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
+ });
7381
8135
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
7382
8136
  const watcher = makeWatcherStub();
7383
8137
  const dnsError = Object.assign(new Error("getaddrinfo ENOTFOUND vault"), {
@@ -7671,6 +8425,85 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
7671
8425
  });
7672
8426
  });
7673
8427
  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
+ });
7674
8507
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
7675
8508
  const watcher = makeWatcherStub();
7676
8509
  let triggerShutdown = () => { };
@@ -7783,6 +8616,173 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
7783
8616
  await loop;
7784
8617
  });
7785
8618
  });
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
+ });
7786
8786
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7787
8787
  const watchArgv = [
7788
8788
  "--companies",