@indigoai-us/hq-cloud 6.16.51 → 6.16.52-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 +311 -116
  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 +802 -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 +37 -12
  91. package/dist/journal.d.ts.map +1 -1
  92. package/dist/journal.js +291 -76
  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,7 @@ 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 { monitorEventLoopDelay } from "node:perf_hooks";
13
14
  import { runRunner, runRunnerWithLoop, resolveDeletePolicy, resolveSkipPersonal, resolveSkipCompanies, selectObjectIOFactory, routeChangeToTarget, buildTargetedPushArgv, resolvePullScope, readPinnedPrefixes, SESSIONS_SCOPE_PREFIX, defaultCollectTelemetry, AUTH_REQUIRED_PASS_EXIT, } from "./sync-runner.js";
14
15
  import { SESSION_LOGS_PUSH_ONLY_PREFIX } from "../sync/pull-scope.js";
15
16
  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 +18,18 @@ import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
17
18
  import { setBandwidthGovernorForTesting } from "../bandwidth.js";
18
19
  import { POST_SYNC_FLAG_KEYS, } from "./sync-runner-post-sync-flags.js";
19
20
  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";
21
+ 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";
22
+ import { AreaLedgerMigration, JournalStore, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, readJournalScoped, writeJournal, } from "../journal.js";
22
23
  import { AreaResolver } from "../sync/area-resolver.js";
23
24
  import { AreaMaintenanceCursorStore } from "../sync/per-area-maintenance.js";
24
25
  import { HQ_CLOUD_VERSION } from "../version.js";
25
26
  import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
26
27
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
27
28
  import { getStateStoreSnapshotRecoveryCountForTest, resetStateStoreSnapshotRecoveryCountForTest, StateStoreLockError, } from "../sync/state-store.js";
28
- import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
29
+ import { PARTIAL_SYNC_EXIT, UNRESOLVED_COMPANY_EXIT, } from "../lib/exit-codes.js";
29
30
  import { isCoveredByAny } from "../prefix-coalesce.js";
30
31
  import { VaultAuthError, VaultNotFoundError } from "../vault-client.js";
32
+ import { buildRoutePullArgv } from "./sync-runner-watch-routes.js";
31
33
  import { CognitoAuthError, CognitoRefreshError } from "../cognito-auth.js";
32
34
  import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
33
35
  import yaml from "js-yaml";
@@ -162,6 +164,7 @@ function makeVaultStub(opts = {}) {
162
164
  bucketName: `bucket-${uid}`,
163
165
  status: "active",
164
166
  })),
167
+ findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
165
168
  listByType: opts.listPersons ??
166
169
  (() => Promise.resolve([])),
167
170
  },
@@ -954,6 +957,164 @@ describe("target resolution", () => {
954
957
  const call = deps.sync.mock.calls[0][0];
955
958
  expect(call.company).toBe("cmp_abc");
956
959
  });
960
+ it("returns the unresolved-company exit for an empty single-company plan instead of emitting a clean all-complete", async () => {
961
+ const deps = makeDeps({
962
+ createVaultClient: () => makeVaultStub({
963
+ entityGet: () => Promise.resolve(undefined),
964
+ }),
965
+ });
966
+ expect(await runRunner(["--company", "cmp_missing"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
967
+ expect(deps.stderr.events()).toContainEqual({
968
+ type: "error",
969
+ company: "cmp_missing",
970
+ path: "(company-resolution)",
971
+ message: "company entity lookup returned no entity",
972
+ });
973
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
974
+ expect(deps.sync).not.toHaveBeenCalled();
975
+ });
976
+ it("returns the unresolved-company exit when a cmp-prefixed route gets a 404", async () => {
977
+ const client = makeVaultStub({
978
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
979
+ });
980
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
981
+ client.entity.findInMyNamespace = findInMyNamespace;
982
+ const deps = makeDeps({
983
+ createVaultClient: () => client,
984
+ });
985
+ expect(await runRunner(["--company", "cmp_ghost"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
986
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
987
+ expect(deps.stderr.events()).toContainEqual({
988
+ type: "error",
989
+ company: "cmp_ghost",
990
+ path: "(company-resolution)",
991
+ message: "company entity lookup returned 404",
992
+ });
993
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
994
+ });
995
+ it("reports an intentional skipCompanies skip without treating it as a failed pass", async () => {
996
+ const previous = process.env.HQ_SYNC_SKIP_COMPANIES;
997
+ process.env.HQ_SYNC_SKIP_COMPANIES = "paused";
998
+ try {
999
+ const deps = makeDeps({
1000
+ createVaultClient: () => makeVaultStub({
1001
+ entityGet: (uid) => Promise.resolve({
1002
+ uid,
1003
+ slug: "paused",
1004
+ type: "company",
1005
+ status: "active",
1006
+ }),
1007
+ }),
1008
+ });
1009
+ expect(await runRunner(["--company", "cmp_paused"], deps)).toBe(0);
1010
+ expect(deps.stdout.events()).toContainEqual({
1011
+ type: "company-skipped",
1012
+ company: "paused",
1013
+ reason: "skip-companies",
1014
+ });
1015
+ expect(deps.stderr.events().some((event) => event.type === "error")).toBe(false);
1016
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
1017
+ expect(deps.sync).not.toHaveBeenCalled();
1018
+ }
1019
+ finally {
1020
+ if (previous === undefined)
1021
+ delete process.env.HQ_SYNC_SKIP_COMPANIES;
1022
+ else
1023
+ process.env.HQ_SYNC_SKIP_COMPANIES = previous;
1024
+ }
1025
+ });
1026
+ it("resolves a slug emitted by the company watch route to its namespace UID", async () => {
1027
+ const company = {
1028
+ uid: "cmp_indigo",
1029
+ slug: "indigo",
1030
+ type: "company",
1031
+ status: "active",
1032
+ bucketName: "bucket-cmp_indigo",
1033
+ };
1034
+ const client = makeVaultStub({
1035
+ entityGet: (uid) => Promise.resolve(uid === company.uid ? company : undefined),
1036
+ });
1037
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1038
+ client.entity.findInMyNamespace = findInMyNamespace;
1039
+ const deps = makeDeps({ createVaultClient: () => client });
1040
+ const argv = buildRoutePullArgv({ kind: "company", slug: "indigo" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1041
+ expect(argv).toEqual([
1042
+ "--company",
1043
+ "indigo",
1044
+ "--direction",
1045
+ "pull",
1046
+ "--hq-root",
1047
+ "/tmp/hq",
1048
+ ]);
1049
+ expect(await runRunner(argv, deps)).toBe(0);
1050
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1051
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: "cmp_indigo" }));
1052
+ expect(deps.stdout.events()).toContainEqual({
1053
+ type: "fanout-plan",
1054
+ companies: [
1055
+ expect.objectContaining({ uid: "cmp_indigo", slug: "indigo" }),
1056
+ ],
1057
+ });
1058
+ });
1059
+ it("resolves a cmp-prefixed directory slug in the caller namespace before treating it as a UID", async () => {
1060
+ const company = {
1061
+ uid: "cmp_01JTEAM000000000000000000",
1062
+ slug: "cmp_team",
1063
+ type: "company",
1064
+ status: "active",
1065
+ bucketName: "bucket-cmp-team",
1066
+ };
1067
+ const get = vi.fn().mockImplementation((uid) => Promise.resolve(uid === company.uid ? company : undefined));
1068
+ const client = makeVaultStub({ entityGet: get });
1069
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1070
+ client.entity.findInMyNamespace = findInMyNamespace;
1071
+ const deps = makeDeps({ createVaultClient: () => client });
1072
+ const argv = buildRoutePullArgv({ kind: "company", slug: "cmp_team" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1073
+ expect(await runRunner(argv, deps)).toBe(0);
1074
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_team");
1075
+ expect(get).toHaveBeenCalledWith(company.uid);
1076
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: company.uid }));
1077
+ expect(deps.stdout.events()).toContainEqual({
1078
+ type: "fanout-plan",
1079
+ companies: [expect.objectContaining({ uid: company.uid, slug: "cmp_team" })],
1080
+ });
1081
+ });
1082
+ it("falls back to a literal company UID after caller-namespace slug resolution misses", async () => {
1083
+ const uid = "cmp_01JUID0000000000000000000";
1084
+ const company = {
1085
+ uid,
1086
+ slug: "operator-company",
1087
+ type: "company",
1088
+ status: "active",
1089
+ bucketName: "bucket-operator-company",
1090
+ };
1091
+ const get = vi.fn().mockResolvedValue(company);
1092
+ const client = makeVaultStub({ entityGet: get });
1093
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1094
+ client.entity.findInMyNamespace = findInMyNamespace;
1095
+ const deps = makeDeps({ createVaultClient: () => client });
1096
+ expect(await runRunner(["--company", uid], deps)).toBe(0);
1097
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", uid);
1098
+ expect(get).toHaveBeenCalledWith(uid);
1099
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: uid }));
1100
+ });
1101
+ it("reports a foreign company slug when the literal UID fallback finds nothing", async () => {
1102
+ const get = vi.fn().mockRejectedValue(new VaultNotFoundError("company not found"));
1103
+ const client = makeVaultStub({ entityGet: get });
1104
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1105
+ client.entity.findInMyNamespace = findInMyNamespace;
1106
+ const deps = makeDeps({ createVaultClient: () => client });
1107
+ expect(await runRunner(["--company", "indigo"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
1108
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1109
+ expect(get).toHaveBeenCalledWith("indigo");
1110
+ expect(deps.sync).not.toHaveBeenCalled();
1111
+ expect(deps.stderr.events()).toContainEqual({
1112
+ type: "error",
1113
+ company: "indigo",
1114
+ path: "(company-resolution)",
1115
+ message: "company entity lookup returned 404",
1116
+ });
1117
+ });
957
1118
  });
958
1119
  // ---------------------------------------------------------------------------
959
1120
  // --personal mode (personal-vault-only, skip company fanout)
@@ -5586,7 +5747,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5586
5747
  });
5587
5748
  await expect(superseded).resolves.toBeUndefined();
5588
5749
  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"');
5750
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":2,"source":"receiver","direction":"pull","outcome":"refused"');
5590
5751
  releaseHeld();
5591
5752
  await heldReceiverWork;
5592
5753
  await replacement;
@@ -5899,7 +6060,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5899
6060
  triggerShutdown();
5900
6061
  await loop;
5901
6062
  });
5902
- it("fast lane: a small watcher batch dispatches during a slow bulk pass", async () => {
6063
+ it("fast lane: a small watcher batch waits for a full pass that has not reached a hand-off checkpoint", async () => {
5903
6064
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
5904
6065
  const relativePath = "companies/indigo/realtime.md";
5905
6066
  const absolutePath = path.join(hqRoot, relativePath);
@@ -5939,15 +6100,17 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5939
6100
  expect(order).toEqual(["bulk"]);
5940
6101
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5941
6102
  await flushLoopMicrotasks(30);
5942
- // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
5943
- expect(order).toEqual(["bulk", "fast", "fast"]);
6103
+ // A full pass that has not reached its durable checkpoint remains the sole
6104
+ // owner. Realtime is promoted at the checkpoint, never run concurrently.
6105
+ expect(order).toEqual(["bulk"]);
5944
6106
  releaseBulk();
5945
- await flushLoopMicrotasks();
6107
+ await flushLoopMicrotasks(30);
6108
+ expect(order).toEqual(["bulk", "fast", "fast"]);
5946
6109
  triggerShutdown();
5947
6110
  await loop;
5948
6111
  fs.rmSync(hqRoot, { recursive: true, force: true });
5949
6112
  });
5950
- it("parallel coordinator: a watcher push starts while a full chore is blocked", async () => {
6113
+ it("parallel coordinator: a watcher push waits for a full chore without a hand-off checkpoint", async () => {
5951
6114
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
5952
6115
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
5953
6116
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -5990,9 +6153,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5990
6153
  expect(order).toEqual(["chore"]);
5991
6154
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
5992
6155
  await flushLoopMicrotasks(30);
5993
- expect(order).toEqual(["chore", "realtime"]);
6156
+ expect(order).toEqual(["chore"]);
5994
6157
  releaseBulk();
5995
- await flushLoopMicrotasks();
6158
+ await flushLoopMicrotasks(30);
6159
+ expect(order).toEqual(["chore", "realtime"]);
5996
6160
  triggerShutdown();
5997
6161
  await loop;
5998
6162
  }
@@ -6077,7 +6241,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6077
6241
  fs.rmSync(hqRoot, { recursive: true, force: true });
6078
6242
  }
6079
6243
  });
6080
- it("parallel coordinator partitions receiver events into fast and slow realtime lanes", async () => {
6244
+ it("parallel coordinator partitions receiver events into fast and slow realtime lanes without overlapping their durable writes", async () => {
6081
6245
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6082
6246
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6083
6247
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6148,7 +6312,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6148
6312
  signal: new AbortController().signal,
6149
6313
  });
6150
6314
  await flushLoopMicrotasks(20);
6151
- expect(order).toEqual(["slow-held", "fast-small"]);
6315
+ expect(order).toEqual(["slow-held"]);
6152
6316
  releaseSlow();
6153
6317
  await held;
6154
6318
  await partitioned;
@@ -6314,6 +6478,251 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6314
6478
  fs.rmSync(hqRoot, { recursive: true, force: true });
6315
6479
  }
6316
6480
  });
6481
+ it("bounds a 32-path realtime personal commit on a 700k-row journal and 50k-path tree", async () => {
6482
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6483
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6484
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-production-scale-watch-"));
6485
+ const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6486
+ const targetPaths = Array.from({ length: 32 }, (_, index) => `personal/watch-load/${String(index).padStart(5, "0")}/target.md`);
6487
+ const files = {};
6488
+ for (let index = 0; index < 700_000; index += 1) {
6489
+ files[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6490
+ hash: `sha256:history-${index}`,
6491
+ size: index % 4096,
6492
+ syncedAt: "2026-09-16T21:30:00.000Z",
6493
+ direction: "up",
6494
+ };
6495
+ }
6496
+ for (const target of targetPaths) {
6497
+ files[target] = {
6498
+ hash: `sha256:${target}`,
6499
+ size: 1,
6500
+ syncedAt: "2026-09-16T21:30:00.000Z",
6501
+ direction: "up",
6502
+ };
6503
+ }
6504
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6505
+ version: "2",
6506
+ lastSync: "2026-09-16T21:30:00.000Z",
6507
+ pulls: [],
6508
+ files,
6509
+ });
6510
+ for (let index = 0; index < 50_000; index += 1) {
6511
+ const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
6512
+ fs.mkdirSync(directory, { recursive: true });
6513
+ fs.writeFileSync(path.join(directory, "seed.md"), "seed");
6514
+ }
6515
+ const watcher = makeBatchWatcherStub();
6516
+ let shutdown = () => { };
6517
+ let committedAt;
6518
+ const runPass = vi.fn(async (argv) => {
6519
+ if (!argv.includes("--scope-path")) {
6520
+ // Production opens the packed journal during the initial full pass,
6521
+ // before the watcher accepts realtime work. That recovered projection
6522
+ // is the one a scoped commit must reuse.
6523
+ readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6524
+ }
6525
+ if (argv.includes("--scope-path") && argv.includes("push")) {
6526
+ const scoped = readJournalScoped(PERSONAL_VAULT_JOURNAL_SLUG, targetPaths);
6527
+ const target = targetPaths[0];
6528
+ scoped.files[target] = {
6529
+ ...scoped.files[target],
6530
+ syncedAt: "2026-09-16T21:38:18.000Z",
6531
+ };
6532
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, scoped);
6533
+ committedAt = performance.now();
6534
+ }
6535
+ return completedPassOutcome();
6536
+ });
6537
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6538
+ runPass,
6539
+ createWatcher: () => watcher,
6540
+ sleep: () => new Promise(() => { }),
6541
+ onShutdownSignal: (handler) => {
6542
+ shutdown = handler;
6543
+ return () => { };
6544
+ },
6545
+ });
6546
+ try {
6547
+ await flushLoopMicrotasks(30);
6548
+ runPass.mockClear();
6549
+ resetStateStoreSnapshotRecoveryCountForTest();
6550
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6551
+ eventLoopDelay.enable();
6552
+ await new Promise((resolve) => setTimeout(resolve, 0));
6553
+ const submittedAt = performance.now();
6554
+ let timerDelayMs;
6555
+ const eventLoopTurn = new Promise((resolve) => {
6556
+ setTimeout(() => {
6557
+ timerDelayMs = performance.now() - submittedAt;
6558
+ resolve();
6559
+ }, 0);
6560
+ });
6561
+ const batch = {
6562
+ paths: new Map(targetPaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6563
+ changes: new Map(targetPaths.map((relativePath) => [
6564
+ path.join(hqRoot, relativePath),
6565
+ { kind: "change" },
6566
+ ])),
6567
+ };
6568
+ watcher.emit(targetPaths[0], batch);
6569
+ await vi.waitFor(() => expect(committedAt).toBeDefined(), { timeout: 30_000, interval: 10 });
6570
+ await eventLoopTurn;
6571
+ eventLoopDelay.disable();
6572
+ expect(committedAt - submittedAt).toBeLessThan(1_000);
6573
+ expect(Number(eventLoopDelay.max) / 1_000_000).toBeLessThan(100);
6574
+ expect(timerDelayMs).toBeLessThan(100);
6575
+ expect(getStateStoreSnapshotRecoveryCountForTest()).toBe(0);
6576
+ expect(runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"))).toHaveLength(1);
6577
+ }
6578
+ finally {
6579
+ shutdown();
6580
+ await loop;
6581
+ if (previousCoordinator === undefined)
6582
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6583
+ else
6584
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
6585
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6586
+ }
6587
+ }, 180_000);
6588
+ it("keeps warmed personal and company journals hot across alternating realtime batches", async () => {
6589
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6590
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6591
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-multi-journal-watch-"));
6592
+ const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6593
+ const personalPath = "personal/watch-load/00000/target.md";
6594
+ const companyPath = "companies/indigo/watch-load/00000/target.md";
6595
+ const companyKey = "watch-load/00000/target.md";
6596
+ const personalFiles = {};
6597
+ for (let index = 0; index < 700_000; index += 1) {
6598
+ personalFiles[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6599
+ hash: `sha256:history-${index}`,
6600
+ size: index % 4096,
6601
+ syncedAt: "2026-09-16T21:30:00.000Z",
6602
+ direction: "up",
6603
+ };
6604
+ }
6605
+ personalFiles[personalPath] = {
6606
+ hash: "sha256:personal-target",
6607
+ size: 1,
6608
+ syncedAt: "2026-09-16T21:30:00.000Z",
6609
+ direction: "up",
6610
+ };
6611
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6612
+ version: "2",
6613
+ lastSync: "2026-09-16T21:30:00.000Z",
6614
+ pulls: [],
6615
+ files: personalFiles,
6616
+ });
6617
+ writeJournal("indigo", {
6618
+ version: "2",
6619
+ lastSync: "2026-09-16T21:30:00.000Z",
6620
+ pulls: [],
6621
+ files: {
6622
+ [companyKey]: {
6623
+ hash: "sha256:company-target",
6624
+ size: 1,
6625
+ syncedAt: "2026-09-16T21:30:00.000Z",
6626
+ direction: "up",
6627
+ },
6628
+ },
6629
+ });
6630
+ for (let index = 0; index < 50_000; index += 1) {
6631
+ const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
6632
+ fs.mkdirSync(directory, { recursive: true });
6633
+ fs.writeFileSync(path.join(directory, "seed.md"), "seed");
6634
+ }
6635
+ fs.mkdirSync(path.dirname(path.join(hqRoot, personalPath)), { recursive: true });
6636
+ fs.writeFileSync(path.join(hqRoot, personalPath), "personal");
6637
+ fs.mkdirSync(path.dirname(path.join(hqRoot, companyPath)), { recursive: true });
6638
+ fs.writeFileSync(path.join(hqRoot, companyPath), "company");
6639
+ const watcher = makeBatchWatcherStub();
6640
+ let shutdown = () => { };
6641
+ const commits = [];
6642
+ let submittedAt = 0;
6643
+ const runPass = vi.fn(async (argv) => {
6644
+ if (!argv.includes("--scope-path")) {
6645
+ readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6646
+ readJournal("indigo");
6647
+ }
6648
+ if (argv.includes("--scope-path") && argv.includes("push")) {
6649
+ const slug = argv.includes("--company") ? "indigo" : PERSONAL_VAULT_JOURNAL_SLUG;
6650
+ const key = slug === "indigo" ? companyKey : personalPath;
6651
+ const scoped = readJournalScoped(slug, [key]);
6652
+ scoped.files[key] = {
6653
+ ...scoped.files[key],
6654
+ syncedAt: "2026-09-16T21:38:18.000Z",
6655
+ };
6656
+ writeJournal(slug, scoped);
6657
+ commits.push({ slug, elapsedMs: performance.now() - submittedAt });
6658
+ }
6659
+ return completedPassOutcome();
6660
+ });
6661
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6662
+ runPass,
6663
+ createWatcher: () => watcher,
6664
+ sleep: () => new Promise(() => { }),
6665
+ onShutdownSignal: (handler) => {
6666
+ shutdown = handler;
6667
+ return () => { };
6668
+ },
6669
+ });
6670
+ const emitAndWait = async (relativePaths) => {
6671
+ const before = commits.length;
6672
+ submittedAt = performance.now();
6673
+ const batch = {
6674
+ paths: new Map(relativePaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6675
+ changes: new Map(relativePaths.map((relativePath) => [
6676
+ path.join(hqRoot, relativePath),
6677
+ { kind: "change" },
6678
+ ])),
6679
+ };
6680
+ watcher.emit(relativePaths[0], batch);
6681
+ await vi.waitFor(() => expect(commits.length).toBe(before + relativePaths.length), {
6682
+ timeout: 30_000,
6683
+ interval: 10,
6684
+ });
6685
+ };
6686
+ try {
6687
+ await flushLoopMicrotasks(30);
6688
+ runPass.mockClear();
6689
+ resetStateStoreSnapshotRecoveryCountForTest();
6690
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6691
+ eventLoopDelay.enable();
6692
+ await emitAndWait([personalPath, companyPath]);
6693
+ for (let round = 0; round < 3; round += 1) {
6694
+ await emitAndWait([personalPath]);
6695
+ await emitAndWait([companyPath]);
6696
+ }
6697
+ eventLoopDelay.disable();
6698
+ const recoveryCount = getStateStoreSnapshotRecoveryCountForTest();
6699
+ const maxDelayMs = Number(eventLoopDelay.max) / 1_000_000;
6700
+ const maxCommitMs = Math.max(...commits.map((commit) => commit.elapsedMs));
6701
+ console.info({ recoveryCount, maxDelayMs, maxCommitMs, commits: commits.length });
6702
+ expect.soft(recoveryCount).toBe(0);
6703
+ expect.soft(maxDelayMs).toBeLessThan(100);
6704
+ expect.soft(maxCommitMs).toBeLessThan(1_000);
6705
+ expect(commits.map((commit) => commit.slug)).toEqual([
6706
+ PERSONAL_VAULT_JOURNAL_SLUG,
6707
+ "indigo",
6708
+ PERSONAL_VAULT_JOURNAL_SLUG,
6709
+ "indigo",
6710
+ PERSONAL_VAULT_JOURNAL_SLUG,
6711
+ "indigo",
6712
+ PERSONAL_VAULT_JOURNAL_SLUG,
6713
+ "indigo",
6714
+ ]);
6715
+ }
6716
+ finally {
6717
+ shutdown();
6718
+ await loop;
6719
+ if (previousCoordinator === undefined)
6720
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6721
+ else
6722
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
6723
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6724
+ }
6725
+ }, 180_000);
6317
6726
  it("fast lane: prefers declared receiver sizes and falls back to the journal", async () => {
6318
6727
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6319
6728
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
@@ -6489,19 +6898,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6489
6898
  ]),
6490
6899
  });
6491
6900
  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");
6901
+ // The full pass owns durable state until a checkpoint. Its queued realtime
6902
+ // residue is promoted at that hand-off instead of overlapping the pass.
6903
+ expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(0);
6496
6904
  releaseBulk();
6497
6905
  await flushLoopMicrotasks(30);
6498
6906
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
6907
+ expect(calls.flat().join(" ")).toContain("realtime.md");
6499
6908
  expect(calls.flat().join(" ")).toContain("session-0.log");
6500
6909
  triggerShutdown();
6501
6910
  await loop;
6502
6911
  fs.rmSync(hqRoot, { recursive: true, force: true });
6503
6912
  });
6504
- it("fast lane: a small receiver pull dispatches during a slow bulk pass", async () => {
6913
+ it("fast lane: a small receiver pull waits for a slow full pass without a hand-off checkpoint", async () => {
6505
6914
  const watcher = makeWatcherStub();
6506
6915
  let triggerShutdown = () => { };
6507
6916
  let receiverSync;
@@ -6546,7 +6955,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6546
6955
  watcher.emit();
6547
6956
  await flushLoopMicrotasks();
6548
6957
  expect(order).toEqual(["bulk"]);
6549
- await receiverSync({
6958
+ const receiverPull = receiverSync({
6550
6959
  events: [{
6551
6960
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
6552
6961
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -6554,8 +6963,11 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6554
6963
  }],
6555
6964
  signal: new AbortController().signal,
6556
6965
  });
6557
- expect(order).toEqual(["bulk", "fast-pull"]);
6966
+ await flushLoopMicrotasks(20);
6967
+ expect(order).toEqual(["bulk"]);
6558
6968
  releaseBulk();
6969
+ await receiverPull;
6970
+ expect(order).toEqual(["bulk", "fast-pull"]);
6559
6971
  await flushLoopMicrotasks();
6560
6972
  triggerShutdown();
6561
6973
  await loop;
@@ -6599,12 +7011,14 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6599
7011
  await flushLoopMicrotasks();
6600
7012
  watcher.emit();
6601
7013
  await flushLoopMicrotasks();
7014
+ releaseBulk();
7015
+ // The full chore has no explicit safe checkpoint in this stub, so the
7016
+ // realtime pass begins only after the chore has released ownership.
6602
7017
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6603
7018
  await fastStarted;
6604
- releaseBulk();
6605
7019
  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.
7020
+ // The realtime pass owns the root lock until its own durable work ends. A
7021
+ // second sync/rescue must not acquire the lock in this interval.
6608
7022
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
6609
7023
  releaseFast();
6610
7024
  await flushLoopMicrotasks(20);
@@ -6779,59 +7193,68 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6779
7193
  return () => { };
6780
7194
  },
6781
7195
  });
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
- };
7196
+ try {
7197
+ await flushLoopMicrotasks();
7198
+ watcher.emit();
7199
+ await flushLoopMicrotasks();
7200
+ runPass.mockClear();
7201
+ const events = [
7202
+ ...Array.from({ length: 17 }, (_, index) => ({
7203
+ kind: "upsert",
7204
+ relativePath: `companies/acme/a-${index}.md`,
7205
+ contentHash: "sha256:x",
7206
+ mtime: "2026-08-26T00:00:00.000Z",
7207
+ originDeviceId: "peer",
7208
+ originTenantId: "acme",
7209
+ sequenceNumber: index,
7210
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
7211
+ })),
7212
+ ...Array.from({ length: 16 }, (_, index) => ({
7213
+ kind: "upsert",
7214
+ relativePath: `companies/beta/b-${index}.md`,
7215
+ contentHash: "sha256:x",
7216
+ mtime: "2026-08-26T00:00:00.000Z",
7217
+ originDeviceId: "peer",
7218
+ originTenantId: "beta",
7219
+ sequenceNumber: index + 17,
7220
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
7221
+ })),
7222
+ ];
7223
+ for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
7224
+ const journal = readJournal(slug);
7225
+ for (let index = 0; index < count; index++) {
7226
+ journal.files[`${prefix}-${index}.md`] = {
7227
+ hash: "sha256:x",
7228
+ size: 16,
7229
+ syncedAt: "2026-08-26T00:00:00.000Z",
7230
+ direction: "down",
7231
+ };
7232
+ }
7233
+ writeJournal(slug, journal);
6817
7234
  }
6818
- writeJournal(slug, journal);
7235
+ const receiverDrain = receiverSync({
7236
+ events,
7237
+ signal: new AbortController().signal,
7238
+ });
7239
+ await flushLoopMicrotasks(20);
7240
+ // The running full chore has not yet yielded a durable hand-off, so no
7241
+ // receiver work may overlap it. Once it releases ownership, the
7242
+ // scheduler admits each route's bounded work before the residual slow
7243
+ // beta batch (acme fast, beta fast, then beta slow).
7244
+ expect(runPass).not.toHaveBeenCalled();
7245
+ releaseBulk();
7246
+ await receiverDrain;
7247
+ const scopedCalls = runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"));
7248
+ expect(scopedCalls).toHaveLength(3);
7249
+ expect(scopedCalls.flat().join(" ")).toContain("a-16.md");
7250
+ expect(scopedCalls.flat().join(" ")).toContain("b-14.md");
7251
+ }
7252
+ finally {
7253
+ releaseBulk();
7254
+ triggerShutdown();
7255
+ await loop;
7256
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6819
7257
  }
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
7258
  });
6836
7259
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
6837
7260
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7003,9 +7426,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7003
7426
  await flushLoopMicrotasks();
7004
7427
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7005
7428
  await flushLoopMicrotasks(30);
7006
- expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
7429
+ // The full pass has not reached a durable checkpoint, so it remains the
7430
+ // only journal writer. The scoped realtime pass is admitted immediately
7431
+ // after this safe hand-off, preserving both sets of rows.
7432
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(0);
7007
7433
  releaseBulk();
7008
- await flushLoopMicrotasks();
7434
+ await flushLoopMicrotasks(30);
7009
7435
  const journal = readJournal("indigo").files;
7010
7436
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7011
7437
  for (let index = 0; index < paths.length; index++) {
@@ -7057,10 +7483,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7057
7483
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7058
7484
  }
7059
7485
  await flushLoopMicrotasks(60);
7060
- expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
7486
+ // A full pass without a checkpoint remains the sole mutable-state owner.
7487
+ // The queued realtime batches run immediately after it completes.
7488
+ expect(completed).toEqual([]);
7061
7489
  releaseBulk();
7062
- await flushLoopMicrotasks();
7063
- expect(completed).toContain("bulk");
7490
+ await flushLoopMicrotasks(60);
7491
+ expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7064
7492
  triggerShutdown();
7065
7493
  await loop;
7066
7494
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -7378,6 +7806,55 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7378
7806
  expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
7379
7807
  expect(watcher.disposed).toBe(false);
7380
7808
  });
7809
+ it("keeps the watcher alive for an unresolved company watch route (HQ-SYNC-COMPANY-ROUTE-UID)", async () => {
7810
+ const watcher = makeWatcherStub();
7811
+ const client = makeVaultStub({
7812
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
7813
+ });
7814
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
7815
+ client.entity.findInMyNamespace = findInMyNamespace;
7816
+ const routedPass = makeDeps({ createVaultClient: () => client });
7817
+ const routedArgv = buildRoutePullArgv({ kind: "company", slug: "cmp_ghost" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
7818
+ let attempts = 0;
7819
+ let routedExit;
7820
+ const runPass = vi.fn().mockImplementation(async () => {
7821
+ attempts += 1;
7822
+ if (attempts === 1) {
7823
+ routedExit = await runRunner(routedArgv, routedPass);
7824
+ return routedExit;
7825
+ }
7826
+ // Terminate the otherwise-open loop only after it proves the unresolved
7827
+ // route did not make the watcher exit.
7828
+ return 1;
7829
+ });
7830
+ const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7831
+ runPass,
7832
+ clock: new FakeClock(),
7833
+ createWatcher: () => watcher,
7834
+ sleep: () => Promise.resolve(),
7835
+ onShutdownSignal: () => () => { },
7836
+ });
7837
+ expect(routedArgv).toEqual([
7838
+ "--company",
7839
+ "cmp_ghost",
7840
+ "--direction",
7841
+ "pull",
7842
+ "--hq-root",
7843
+ "/tmp/hq",
7844
+ ]);
7845
+ expect(routedExit).toBe(UNRESOLVED_COMPANY_EXIT);
7846
+ expect(runPass).toHaveBeenCalledTimes(2);
7847
+ expect(code).toBe(1);
7848
+ expect(code).not.toBe(UNRESOLVED_COMPANY_EXIT);
7849
+ expect(watcher.disposed).toBe(false);
7850
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
7851
+ expect(routedPass.stderr.events()).toContainEqual({
7852
+ type: "error",
7853
+ company: "cmp_ghost",
7854
+ path: "(company-resolution)",
7855
+ message: "company entity lookup returned 404",
7856
+ });
7857
+ });
7381
7858
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
7382
7859
  const watcher = makeWatcherStub();
7383
7860
  const dnsError = Object.assign(new Error("getaddrinfo ENOTFOUND vault"), {
@@ -7671,6 +8148,85 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
7671
8148
  });
7672
8149
  });
7673
8150
  describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
8151
+ it("hands a wake realtime drain to the guard before a long-running full chore completes", async () => {
8152
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8153
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
8154
+ const watcher = makeWatcherStub();
8155
+ let triggerShutdown = () => { };
8156
+ let releaseCheckpoint = () => { };
8157
+ const checkpointGate = new Promise((resolve) => { releaseCheckpoint = resolve; });
8158
+ let releaseRealtime = () => { };
8159
+ const realtimeGate = new Promise((resolve) => { releaseRealtime = resolve; });
8160
+ const order = [];
8161
+ let passes = 0;
8162
+ let runRealtime = null;
8163
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8164
+ runPass: vi.fn().mockImplementation(async (_argv, checkpoint) => {
8165
+ passes += 1;
8166
+ if (passes === 2) {
8167
+ order.push("chore");
8168
+ // The fake pass has completed its current durable unit and is now
8169
+ // at the same explicit hand-off seam production uses between
8170
+ // fully-settled fanout targets.
8171
+ await checkpointGate;
8172
+ await checkpoint?.();
8173
+ order.push("chore-complete");
8174
+ }
8175
+ return completedPassOutcome();
8176
+ }),
8177
+ runPassSupportsCooperativeCheckpoint: true,
8178
+ clock: new FakeClock(),
8179
+ createWatcher: () => watcher,
8180
+ sleep: makeSteppableSleep().sleep,
8181
+ onShutdownSignal: (handler) => {
8182
+ triggerShutdown = handler;
8183
+ return () => { };
8184
+ },
8185
+ startRealtimeScheduler: async (opts) => {
8186
+ // This is the same wake path used by the realtime scheduler: it
8187
+ // enters runGuardedTask behind the coordinator worker.
8188
+ runRealtime = () => opts.runGuarded(async () => {
8189
+ order.push("realtime");
8190
+ await realtimeGate;
8191
+ order.push("realtime-complete");
8192
+ });
8193
+ return {
8194
+ signal: () => undefined,
8195
+ checkHighWater: async () => undefined,
8196
+ dispose: async () => undefined,
8197
+ };
8198
+ },
8199
+ });
8200
+ try {
8201
+ await flushLoopMicrotasks(30);
8202
+ watcher.emit();
8203
+ await flushLoopMicrotasks(20);
8204
+ expect(order).toEqual(["chore"]);
8205
+ const realtime = runRealtime();
8206
+ releaseCheckpoint();
8207
+ await flushLoopMicrotasks(30);
8208
+ expect(order).toEqual(["chore", "realtime"]);
8209
+ releaseRealtime();
8210
+ await realtime;
8211
+ await flushLoopMicrotasks(20);
8212
+ expect(order).toEqual([
8213
+ "chore",
8214
+ "realtime",
8215
+ "realtime-complete",
8216
+ "chore-complete",
8217
+ ]);
8218
+ }
8219
+ finally {
8220
+ releaseCheckpoint();
8221
+ releaseRealtime();
8222
+ triggerShutdown();
8223
+ await loop;
8224
+ if (previousCoordinator === undefined)
8225
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8226
+ else
8227
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
8228
+ }
8229
+ });
7674
8230
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
7675
8231
  const watcher = makeWatcherStub();
7676
8232
  let triggerShutdown = () => { };
@@ -7783,6 +8339,173 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
7783
8339
  await loop;
7784
8340
  });
7785
8341
  });
8342
+ describe("CooperativePassScheduler", () => {
8343
+ it("admits an explicitly safe fast realtime pass while a slow scoped realtime pass remains in flight", async () => {
8344
+ const scheduler = new CooperativePassScheduler(() => 0);
8345
+ const order = [];
8346
+ let releaseSlow = () => { };
8347
+ const slowGate = new Promise((resolve) => { releaseSlow = resolve; });
8348
+ let markSlowStarted = () => { };
8349
+ const slowStarted = new Promise((resolve) => { markSlowStarted = resolve; });
8350
+ const slow = scheduler.submit(async () => {
8351
+ order.push("slow-start");
8352
+ markSlowStarted();
8353
+ await slowGate;
8354
+ order.push("slow-complete");
8355
+ return 0;
8356
+ }, "realtime", "slow");
8357
+ await slowStarted;
8358
+ const fast = scheduler.submit(async () => {
8359
+ order.push("fast");
8360
+ return 0;
8361
+ }, "realtime", "fast", true);
8362
+ try {
8363
+ await Promise.resolve();
8364
+ await Promise.resolve();
8365
+ expect([...order]).toEqual(["slow-start", "fast"]);
8366
+ }
8367
+ finally {
8368
+ releaseSlow();
8369
+ await Promise.all([slow, fast]);
8370
+ }
8371
+ });
8372
+ it("preserves journal order and resumes full work across repeated randomized hand-offs", async () => {
8373
+ // These deterministic seeds deliberately vary the number and enqueue
8374
+ // ordering of realtime tasks. Every append asserts one durable-state owner
8375
+ // at a time; the full continuation must resume after every realtime burst.
8376
+ for (let seed = 1; seed <= 32; seed += 1) {
8377
+ let now = 0;
8378
+ const scheduler = new CooperativePassScheduler(() => now);
8379
+ const journal = [];
8380
+ const expected = new Set();
8381
+ let writer = null;
8382
+ let maximumWriters = 0;
8383
+ const append = async (entry) => {
8384
+ expect(writer).toBeNull();
8385
+ writer = entry;
8386
+ maximumWriters = Math.max(maximumWriters, 1);
8387
+ // A journal append crosses an async durable-write boundary. If the
8388
+ // scheduler admitted another pass here, its ownership assertion would
8389
+ // fail rather than merely observing JavaScript's synchronous turn.
8390
+ await Promise.resolve();
8391
+ expect(writer).toBe(entry);
8392
+ journal.push(entry);
8393
+ writer = null;
8394
+ };
8395
+ let releaseCheckpoint = () => { };
8396
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8397
+ const chore = scheduler.submit(async ({ checkpoint }) => {
8398
+ expected.add(`chore-${seed}-before`);
8399
+ await append(`chore-${seed}-before`);
8400
+ await checkpointReady;
8401
+ await checkpoint();
8402
+ expected.add(`chore-${seed}-after`);
8403
+ await append(`chore-${seed}-after`);
8404
+ return 0;
8405
+ }, "chore");
8406
+ await Promise.resolve();
8407
+ const realtimeCount = (seed % 7) + 1;
8408
+ const shuffledIndexes = Array.from({ length: realtimeCount }, (_, index) => index);
8409
+ let randomState = seed;
8410
+ for (let index = shuffledIndexes.length - 1; index > 0; index -= 1) {
8411
+ randomState = (randomState * 1_664_525 + 1_013_904_223) >>> 0;
8412
+ const swapIndex = randomState % (index + 1);
8413
+ [shuffledIndexes[index], shuffledIndexes[swapIndex]] = [
8414
+ shuffledIndexes[swapIndex],
8415
+ shuffledIndexes[index],
8416
+ ];
8417
+ }
8418
+ const realtime = shuffledIndexes.map((index) => {
8419
+ const entry = `realtime-${seed}-${(index * 17 + seed) % realtimeCount}`;
8420
+ expected.add(entry);
8421
+ return scheduler.submit(async () => {
8422
+ // Shuffle the microtask timing without ever allowing a second writer.
8423
+ if ((seed + index) % 3 === 0)
8424
+ await Promise.resolve();
8425
+ await append(entry);
8426
+ now += 1;
8427
+ return 0;
8428
+ }, "realtime");
8429
+ });
8430
+ releaseCheckpoint();
8431
+ await Promise.all([chore, ...realtime]);
8432
+ expect(maximumWriters).toBe(1);
8433
+ expect(new Set(journal)).toEqual(expected);
8434
+ expect(journal).toHaveLength(expected.size);
8435
+ expect(journal[0]).toBe(`chore-${seed}-before`);
8436
+ expect(journal.at(-1)).toBe(`chore-${seed}-after`);
8437
+ }
8438
+ });
8439
+ it("lets realtime win at 15 minutes minus one millisecond, then gives the chore the exact-boundary admission", async () => {
8440
+ const runCase = async (elapsedMs) => {
8441
+ let now = 0;
8442
+ const scheduler = new CooperativePassScheduler(() => now);
8443
+ const order = [];
8444
+ let releaseActive = () => { };
8445
+ const activeGate = new Promise((resolve) => { releaseActive = resolve; });
8446
+ const activeRealtime = scheduler.submit(async () => {
8447
+ order.push("active-realtime");
8448
+ await activeGate;
8449
+ return 0;
8450
+ }, "realtime");
8451
+ await Promise.resolve();
8452
+ const chore = scheduler.submit(async () => {
8453
+ order.push("chore");
8454
+ return 0;
8455
+ }, "chore");
8456
+ const queuedRealtime = scheduler.submit(async () => {
8457
+ order.push("queued-realtime");
8458
+ return 0;
8459
+ }, "realtime");
8460
+ now = elapsedMs;
8461
+ releaseActive();
8462
+ await Promise.all([activeRealtime, chore, queuedRealtime]);
8463
+ return order;
8464
+ };
8465
+ await expect(runCase(CHORE_STARVATION_CAP_MS - 1)).resolves.toEqual([
8466
+ "active-realtime",
8467
+ "queued-realtime",
8468
+ "chore",
8469
+ ]);
8470
+ await expect(runCase(CHORE_STARVATION_CAP_MS)).resolves.toEqual([
8471
+ "active-realtime",
8472
+ "chore",
8473
+ "queued-realtime",
8474
+ ]);
8475
+ });
8476
+ it("resumes a handed-off chore when a continuous realtime burst reaches the 15-minute cap", async () => {
8477
+ let now = 0;
8478
+ const scheduler = new CooperativePassScheduler(() => now);
8479
+ const order = [];
8480
+ let releaseCheckpoint = () => { };
8481
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8482
+ const chore = scheduler.submit(async ({ checkpoint }) => {
8483
+ order.push("chore-before");
8484
+ await checkpointReady;
8485
+ await checkpoint();
8486
+ order.push("chore-after");
8487
+ return 0;
8488
+ }, "chore");
8489
+ await Promise.resolve();
8490
+ const firstRealtime = scheduler.submit(async () => {
8491
+ order.push("realtime-before-cap");
8492
+ now = CHORE_STARVATION_CAP_MS;
8493
+ return 0;
8494
+ }, "realtime");
8495
+ const laterRealtime = scheduler.submit(async () => {
8496
+ order.push("realtime-after-cap");
8497
+ return 0;
8498
+ }, "realtime");
8499
+ releaseCheckpoint();
8500
+ await Promise.all([chore, firstRealtime, laterRealtime]);
8501
+ expect(order).toEqual([
8502
+ "chore-before",
8503
+ "realtime-before-cap",
8504
+ "chore-after",
8505
+ "realtime-after-cap",
8506
+ ]);
8507
+ });
8508
+ });
7786
8509
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7787
8510
  const watchArgv = [
7788
8511
  "--companies",