@indigoai-us/hq-cloud 6.16.56 → 6.16.58-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 (124) hide show
  1. package/dist/bin/sync-runner-company.d.ts +14 -0
  2. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-company.js +88 -8
  4. package/dist/bin/sync-runner-company.js.map +1 -1
  5. package/dist/bin/sync-runner-company.test.js +204 -1
  6. package/dist/bin/sync-runner-company.test.js.map +1 -1
  7. package/dist/bin/sync-runner-planning.d.ts +12 -3
  8. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  9. package/dist/bin/sync-runner-planning.js +65 -3
  10. package/dist/bin/sync-runner-planning.js.map +1 -1
  11. package/dist/bin/sync-runner-planning.test.js +172 -1
  12. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.d.ts +51 -2
  14. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  15. package/dist/bin/sync-runner-watch-loop.js +409 -165
  16. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  17. package/dist/bin/sync-runner-watch-routes.d.ts +19 -2
  18. package/dist/bin/sync-runner-watch-routes.d.ts.map +1 -1
  19. package/dist/bin/sync-runner-watch-routes.js +40 -5
  20. package/dist/bin/sync-runner-watch-routes.js.map +1 -1
  21. package/dist/bin/sync-runner-watch-routes.test.js +47 -0
  22. package/dist/bin/sync-runner-watch-routes.test.js.map +1 -1
  23. package/dist/bin/sync-runner.d.ts +65 -5
  24. package/dist/bin/sync-runner.d.ts.map +1 -1
  25. package/dist/bin/sync-runner.js +48 -4
  26. package/dist/bin/sync-runner.js.map +1 -1
  27. package/dist/bin/sync-runner.test.js +1553 -192
  28. package/dist/bin/sync-runner.test.js.map +1 -1
  29. package/dist/cli/onepass-pull-planner-regressions.test.d.ts +2 -0
  30. package/dist/cli/onepass-pull-planner-regressions.test.d.ts.map +1 -0
  31. package/dist/cli/onepass-pull-planner-regressions.test.js +300 -0
  32. package/dist/cli/onepass-pull-planner-regressions.test.js.map +1 -0
  33. package/dist/cli/onepass-pull-planner.test.d.ts +2 -0
  34. package/dist/cli/onepass-pull-planner.test.d.ts.map +1 -0
  35. package/dist/cli/onepass-pull-planner.test.js +266 -0
  36. package/dist/cli/onepass-pull-planner.test.js.map +1 -0
  37. package/dist/cli/share.d.ts +19 -0
  38. package/dist/cli/share.d.ts.map +1 -1
  39. package/dist/cli/share.js +55 -0
  40. package/dist/cli/share.js.map +1 -1
  41. package/dist/cli/share.test.js +87 -1
  42. package/dist/cli/share.test.js.map +1 -1
  43. package/dist/cli/streaming-pull-planner.bench.test.d.ts +15 -0
  44. package/dist/cli/streaming-pull-planner.bench.test.d.ts.map +1 -0
  45. package/dist/cli/streaming-pull-planner.bench.test.js +273 -0
  46. package/dist/cli/streaming-pull-planner.bench.test.js.map +1 -0
  47. package/dist/cli/sync-scope.test.js +8 -0
  48. package/dist/cli/sync-scope.test.js.map +1 -1
  49. package/dist/cli/sync.d.ts +12 -0
  50. package/dist/cli/sync.d.ts.map +1 -1
  51. package/dist/cli/sync.js +297 -125
  52. package/dist/cli/sync.js.map +1 -1
  53. package/dist/cli/sync.test.js +255 -2
  54. package/dist/cli/sync.test.js.map +1 -1
  55. package/dist/journal-row-store.d.ts +40 -5
  56. package/dist/journal-row-store.d.ts.map +1 -1
  57. package/dist/journal-row-store.js +240 -21
  58. package/dist/journal-row-store.js.map +1 -1
  59. package/dist/journal-row-store.test.js +54 -0
  60. package/dist/journal-row-store.test.js.map +1 -1
  61. package/dist/journal-snapshot-v4.d.ts.map +1 -1
  62. package/dist/journal-snapshot-v4.js +11 -6
  63. package/dist/journal-snapshot-v4.js.map +1 -1
  64. package/dist/journal-store.test.js +324 -7
  65. package/dist/journal-store.test.js.map +1 -1
  66. package/dist/journal.d.ts +51 -16
  67. package/dist/journal.d.ts.map +1 -1
  68. package/dist/journal.js +303 -79
  69. package/dist/journal.js.map +1 -1
  70. package/dist/lib/exit-codes.d.ts +15 -0
  71. package/dist/lib/exit-codes.d.ts.map +1 -1
  72. package/dist/lib/exit-codes.js +15 -0
  73. package/dist/lib/exit-codes.js.map +1 -1
  74. package/dist/personal-vault.d.ts.map +1 -1
  75. package/dist/personal-vault.js +10 -3
  76. package/dist/personal-vault.js.map +1 -1
  77. package/dist/personal-vault.test.js +35 -0
  78. package/dist/personal-vault.test.js.map +1 -1
  79. package/dist/remote-pull.d.ts.map +1 -1
  80. package/dist/remote-pull.js +9 -2
  81. package/dist/remote-pull.js.map +1 -1
  82. package/dist/remote-pull.test.js +59 -2
  83. package/dist/remote-pull.test.js.map +1 -1
  84. package/dist/s3.d.ts +1 -0
  85. package/dist/s3.d.ts.map +1 -1
  86. package/dist/s3.js +20 -2
  87. package/dist/s3.js.map +1 -1
  88. package/dist/s3.list-cost.test.d.ts +2 -0
  89. package/dist/s3.list-cost.test.d.ts.map +1 -0
  90. package/dist/s3.list-cost.test.js +51 -0
  91. package/dist/s3.list-cost.test.js.map +1 -0
  92. package/dist/sync/area-ledger.d.ts +27 -3
  93. package/dist/sync/area-ledger.d.ts.map +1 -1
  94. package/dist/sync/area-ledger.js +149 -39
  95. package/dist/sync/area-ledger.js.map +1 -1
  96. package/dist/sync/area-ledger.test.js +192 -5
  97. package/dist/sync/area-ledger.test.js.map +1 -1
  98. package/dist/sync/parallel-sync-coordinator.test.js +29 -0
  99. package/dist/sync/parallel-sync-coordinator.test.js.map +1 -1
  100. package/dist/sync/realtime-telemetry.d.ts +1 -1
  101. package/dist/sync/realtime-telemetry.d.ts.map +1 -1
  102. package/dist/sync/realtime-telemetry.js +14 -6
  103. package/dist/sync/realtime-telemetry.js.map +1 -1
  104. package/dist/sync/realtime-telemetry.test.js +136 -16
  105. package/dist/sync/realtime-telemetry.test.js.map +1 -1
  106. package/dist/sync/remote-file-spool.d.ts +16 -0
  107. package/dist/sync/remote-file-spool.d.ts.map +1 -0
  108. package/dist/sync/remote-file-spool.js +91 -0
  109. package/dist/sync/remote-file-spool.js.map +1 -0
  110. package/dist/sync/remote-file-spool.test.d.ts +2 -0
  111. package/dist/sync/remote-file-spool.test.d.ts.map +1 -0
  112. package/dist/sync/remote-file-spool.test.js +77 -0
  113. package/dist/sync/remote-file-spool.test.js.map +1 -0
  114. package/dist/sync/state-store.d.ts +6 -0
  115. package/dist/sync/state-store.d.ts.map +1 -1
  116. package/dist/sync/state-store.js +14 -2
  117. package/dist/sync/state-store.js.map +1 -1
  118. package/dist/watcher.d.ts +12 -0
  119. package/dist/watcher.d.ts.map +1 -1
  120. package/dist/watcher.js +131 -0
  121. package/dist/watcher.js.map +1 -1
  122. package/dist/watcher.test.js +63 -0
  123. package/dist/watcher.test.js.map +1 -1
  124. package/package.json +2 -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";
@@ -166,6 +172,7 @@ function makeVaultStub(opts = {}) {
166
172
  bucketName: `bucket-${uid}`,
167
173
  status: "active",
168
174
  })),
175
+ findInMyNamespace: opts.findInMyNamespace ?? (() => Promise.resolve(null)),
169
176
  listByType: opts.listPersons ??
170
177
  (() => Promise.resolve([])),
171
178
  },
@@ -958,6 +965,164 @@ describe("target resolution", () => {
958
965
  const call = deps.sync.mock.calls[0][0];
959
966
  expect(call.company).toBe("cmp_abc");
960
967
  });
968
+ it("returns the unresolved-company exit for an empty single-company plan instead of emitting a clean all-complete", async () => {
969
+ const deps = makeDeps({
970
+ createVaultClient: () => makeVaultStub({
971
+ entityGet: () => Promise.resolve(undefined),
972
+ }),
973
+ });
974
+ expect(await runRunner(["--company", "cmp_missing"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
975
+ expect(deps.stderr.events()).toContainEqual({
976
+ type: "error",
977
+ company: "cmp_missing",
978
+ path: "(company-resolution)",
979
+ message: "company entity lookup returned no entity",
980
+ });
981
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
982
+ expect(deps.sync).not.toHaveBeenCalled();
983
+ });
984
+ it("returns the unresolved-company exit when a cmp-prefixed route gets a 404", async () => {
985
+ const client = makeVaultStub({
986
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
987
+ });
988
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
989
+ client.entity.findInMyNamespace = findInMyNamespace;
990
+ const deps = makeDeps({
991
+ createVaultClient: () => client,
992
+ });
993
+ expect(await runRunner(["--company", "cmp_ghost"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
994
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
995
+ expect(deps.stderr.events()).toContainEqual({
996
+ type: "error",
997
+ company: "cmp_ghost",
998
+ path: "(company-resolution)",
999
+ message: "company entity lookup returned 404",
1000
+ });
1001
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
1002
+ });
1003
+ it("reports an intentional skipCompanies skip without treating it as a failed pass", async () => {
1004
+ const previous = process.env.HQ_SYNC_SKIP_COMPANIES;
1005
+ process.env.HQ_SYNC_SKIP_COMPANIES = "paused";
1006
+ try {
1007
+ const deps = makeDeps({
1008
+ createVaultClient: () => makeVaultStub({
1009
+ entityGet: (uid) => Promise.resolve({
1010
+ uid,
1011
+ slug: "paused",
1012
+ type: "company",
1013
+ status: "active",
1014
+ }),
1015
+ }),
1016
+ });
1017
+ expect(await runRunner(["--company", "cmp_paused"], deps)).toBe(0);
1018
+ expect(deps.stdout.events()).toContainEqual({
1019
+ type: "company-skipped",
1020
+ company: "paused",
1021
+ reason: "skip-companies",
1022
+ });
1023
+ expect(deps.stderr.events().some((event) => event.type === "error")).toBe(false);
1024
+ expect(deps.stdout.events().some((event) => event.type === "all-complete")).toBe(false);
1025
+ expect(deps.sync).not.toHaveBeenCalled();
1026
+ }
1027
+ finally {
1028
+ if (previous === undefined)
1029
+ delete process.env.HQ_SYNC_SKIP_COMPANIES;
1030
+ else
1031
+ process.env.HQ_SYNC_SKIP_COMPANIES = previous;
1032
+ }
1033
+ });
1034
+ it("resolves a slug emitted by the company watch route to its namespace UID", async () => {
1035
+ const company = {
1036
+ uid: "cmp_indigo",
1037
+ slug: "indigo",
1038
+ type: "company",
1039
+ status: "active",
1040
+ bucketName: "bucket-cmp_indigo",
1041
+ };
1042
+ const client = makeVaultStub({
1043
+ entityGet: (uid) => Promise.resolve(uid === company.uid ? company : undefined),
1044
+ });
1045
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1046
+ client.entity.findInMyNamespace = findInMyNamespace;
1047
+ const deps = makeDeps({ createVaultClient: () => client });
1048
+ const argv = buildRoutePullArgv({ kind: "company", slug: "indigo" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1049
+ expect(argv).toEqual([
1050
+ "--company",
1051
+ "indigo",
1052
+ "--direction",
1053
+ "pull",
1054
+ "--hq-root",
1055
+ "/tmp/hq",
1056
+ ]);
1057
+ expect(await runRunner(argv, deps)).toBe(0);
1058
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1059
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: "cmp_indigo" }));
1060
+ expect(deps.stdout.events()).toContainEqual({
1061
+ type: "fanout-plan",
1062
+ companies: [
1063
+ expect.objectContaining({ uid: "cmp_indigo", slug: "indigo" }),
1064
+ ],
1065
+ });
1066
+ });
1067
+ it("resolves a cmp-prefixed directory slug in the caller namespace before treating it as a UID", async () => {
1068
+ const company = {
1069
+ uid: "cmp_01JTEAM000000000000000000",
1070
+ slug: "cmp_team",
1071
+ type: "company",
1072
+ status: "active",
1073
+ bucketName: "bucket-cmp-team",
1074
+ };
1075
+ const get = vi.fn().mockImplementation((uid) => Promise.resolve(uid === company.uid ? company : undefined));
1076
+ const client = makeVaultStub({ entityGet: get });
1077
+ const findInMyNamespace = vi.fn().mockResolvedValue(company);
1078
+ client.entity.findInMyNamespace = findInMyNamespace;
1079
+ const deps = makeDeps({ createVaultClient: () => client });
1080
+ const argv = buildRoutePullArgv({ kind: "company", slug: "cmp_team" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
1081
+ expect(await runRunner(argv, deps)).toBe(0);
1082
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_team");
1083
+ expect(get).toHaveBeenCalledWith(company.uid);
1084
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: company.uid }));
1085
+ expect(deps.stdout.events()).toContainEqual({
1086
+ type: "fanout-plan",
1087
+ companies: [expect.objectContaining({ uid: company.uid, slug: "cmp_team" })],
1088
+ });
1089
+ });
1090
+ it("falls back to a literal company UID after caller-namespace slug resolution misses", async () => {
1091
+ const uid = "cmp_01JUID0000000000000000000";
1092
+ const company = {
1093
+ uid,
1094
+ slug: "operator-company",
1095
+ type: "company",
1096
+ status: "active",
1097
+ bucketName: "bucket-operator-company",
1098
+ };
1099
+ const get = vi.fn().mockResolvedValue(company);
1100
+ const client = makeVaultStub({ entityGet: get });
1101
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1102
+ client.entity.findInMyNamespace = findInMyNamespace;
1103
+ const deps = makeDeps({ createVaultClient: () => client });
1104
+ expect(await runRunner(["--company", uid], deps)).toBe(0);
1105
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", uid);
1106
+ expect(get).toHaveBeenCalledWith(uid);
1107
+ expect(deps.sync).toHaveBeenCalledWith(expect.objectContaining({ company: uid }));
1108
+ });
1109
+ it("reports a foreign company slug when the literal UID fallback finds nothing", async () => {
1110
+ const get = vi.fn().mockRejectedValue(new VaultNotFoundError("company not found"));
1111
+ const client = makeVaultStub({ entityGet: get });
1112
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
1113
+ client.entity.findInMyNamespace = findInMyNamespace;
1114
+ const deps = makeDeps({ createVaultClient: () => client });
1115
+ expect(await runRunner(["--company", "indigo"], deps)).toBe(UNRESOLVED_COMPANY_EXIT);
1116
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "indigo");
1117
+ expect(get).toHaveBeenCalledWith("indigo");
1118
+ expect(deps.sync).not.toHaveBeenCalled();
1119
+ expect(deps.stderr.events()).toContainEqual({
1120
+ type: "error",
1121
+ company: "indigo",
1122
+ path: "(company-resolution)",
1123
+ message: "company entity lookup returned 404",
1124
+ });
1125
+ });
961
1126
  });
962
1127
  // ---------------------------------------------------------------------------
963
1128
  // --personal mode (personal-vault-only, skip company fanout)
@@ -1555,6 +1720,43 @@ describe("automatic journal maintenance", () => {
1555
1720
  },
1556
1721
  ]);
1557
1722
  });
1723
+ it("signals watch readiness after journal preparation and before fanout", async () => {
1724
+ const order = [];
1725
+ let releaseFanout = () => { };
1726
+ const fanout = new Promise((resolve) => { releaseFanout = resolve; });
1727
+ const deps = makeDeps({
1728
+ createVaultClient: () => makeVaultStub({
1729
+ memberships: [{ companyUid: "cmp_indigo" }],
1730
+ entityGet: (uid) => Promise.resolve({ uid, slug: "indigo" }),
1731
+ }),
1732
+ repairJournalStateIfNeeded: vi.fn((slug) => {
1733
+ order.push(`repair:${slug}`);
1734
+ return { status: "not-needed", slug };
1735
+ }),
1736
+ migratePersonalVaultJournal: vi.fn(() => {
1737
+ order.push("seed:personal");
1738
+ }),
1739
+ onJournalsReady: () => {
1740
+ order.push("journals-ready");
1741
+ },
1742
+ sync: vi.fn(async () => {
1743
+ order.push("fanout");
1744
+ await fanout;
1745
+ return defaultSyncResult();
1746
+ }),
1747
+ });
1748
+ const running = runRunner(["--companies", "--skip-personal"], deps);
1749
+ await vi.waitFor(() => expect(order).toContain("fanout"));
1750
+ expect(order).toEqual([
1751
+ "repair:indigo",
1752
+ "repair:personal",
1753
+ "seed:personal",
1754
+ "journals-ready",
1755
+ "fanout",
1756
+ ]);
1757
+ releaseFanout();
1758
+ await expect(running).resolves.toBe(0);
1759
+ });
1558
1760
  it("continues the pass when one journal repair is transiently lock-contended", async () => {
1559
1761
  const repaired = [];
1560
1762
  const sync = vi.fn().mockResolvedValue(defaultSyncResult());
@@ -4120,6 +4322,17 @@ describe("routeChangeToTarget", () => {
4120
4322
  });
4121
4323
  expect(routeChangeToTarget("companies/acme/projects/x/prd.json")).toEqual({ kind: "company", slug: "acme" });
4122
4324
  });
4325
+ it("keeps a company-root directory delete attributable without routing company-root files", () => {
4326
+ // A whole-company removal has no child path, but it still owns every
4327
+ // journal row below the slug and must reach the delete-intent guard.
4328
+ expect(routeChangeToTarget("companies/indigo", "unlinkDir")).toEqual({
4329
+ kind: "company",
4330
+ slug: "indigo",
4331
+ });
4332
+ // A file directly under companies/ is shared metadata, never a tenant.
4333
+ expect(routeChangeToTarget("companies/manifest.yaml", "change")).toBeNull();
4334
+ expect(routeChangeToTarget("companies/.DS_Store", "add")).toBeNull();
4335
+ });
4123
4336
  it("routes non-company top-levels to the personal target", () => {
4124
4337
  expect(routeChangeToTarget("personal/notes.md")).toEqual({
4125
4338
  kind: "personal",
@@ -4144,6 +4357,12 @@ describe("routeChangeToTarget", () => {
4144
4357
  // edit always lands at companies/<slug>/..., never at bare "companies".
4145
4358
  expect(routeChangeToTarget("companies")).toBeNull();
4146
4359
  expect(routeChangeToTarget("companies/")).toBeNull();
4360
+ // The companies directory itself contains shared manifest and scaffold
4361
+ // files. They do not belong to a company journal, so a direct child must
4362
+ // never become a bogus `--company <filename>` pass.
4363
+ expect(routeChangeToTarget("companies/manifest.yaml")).toBeNull();
4364
+ expect(routeChangeToTarget("companies/.DS_Store")).toBeNull();
4365
+ expect(routeChangeToTarget("companies/../personal/notes.md")).toBeNull();
4147
4366
  });
4148
4367
  it("does not route the company scaffold to cloud sync", () => {
4149
4368
  expect(routeChangeToTarget("companies/_template/knowledge/README.md")).toBeNull();
@@ -4225,6 +4444,26 @@ async function flushLoopMicrotasks(n = 8) {
4225
4444
  for (let i = 0; i < n; i++)
4226
4445
  await Promise.resolve();
4227
4446
  }
4447
+ /** Retained journal rows live in typed-array backing stores as well as heap. */
4448
+ function forceGarbageCollectionForTest() {
4449
+ const existing = globalThis.gc;
4450
+ if (typeof existing === "function") {
4451
+ existing();
4452
+ existing();
4453
+ return;
4454
+ }
4455
+ v8.setFlagsFromString("--expose-gc");
4456
+ const collect = vm.runInNewContext("gc");
4457
+ v8.setFlagsFromString("--no-expose-gc");
4458
+ if (typeof collect !== "function")
4459
+ throw new Error("test could not expose GC");
4460
+ collect();
4461
+ collect();
4462
+ }
4463
+ function retainedProcessBytes() {
4464
+ const usage = process.memoryUsage();
4465
+ return usage.heapUsed + usage.external + usage.arrayBuffers;
4466
+ }
4228
4467
  function makeSteppableSleep() {
4229
4468
  const pending = [];
4230
4469
  return {
@@ -4237,8 +4476,19 @@ function makeSteppableSleep() {
4237
4476
  throw new Error("poll sleep was not pending");
4238
4477
  resolve();
4239
4478
  },
4479
+ pendingCount() {
4480
+ return pending.length;
4481
+ },
4240
4482
  };
4241
4483
  }
4484
+ async function waitForPollSleep(sleep) {
4485
+ for (let attempt = 0; attempt < 200; attempt += 1) {
4486
+ if (sleep.pendingCount() > 0)
4487
+ return;
4488
+ await new Promise((resolve) => setImmediate(resolve));
4489
+ }
4490
+ throw new Error("timed out waiting for a poll sleep");
4491
+ }
4242
4492
  // ---------------------------------------------------------------------------
4243
4493
  // Adaptive remote-poll backoff — whole-box CPU aware (no --poll-remote-ms)
4244
4494
  // ---------------------------------------------------------------------------
@@ -5330,6 +5580,83 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5330
5580
  triggerShutdown();
5331
5581
  await loop;
5332
5582
  });
5583
+ it.each([
5584
+ ["serialized", "0"],
5585
+ ["parallel coordinator", "1"],
5586
+ ])("starts event ingress once after journals are ready while the first %s full pass is unfinished", async (_mode, coordinator) => {
5587
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
5588
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = coordinator;
5589
+ const watcher = makeWatcherStub();
5590
+ const createWatcher = vi.fn(() => watcher);
5591
+ let releaseFullPass = () => { };
5592
+ const fullPass = new Promise((resolve) => {
5593
+ releaseFullPass = resolve;
5594
+ });
5595
+ let activeDurableWriters = 0;
5596
+ let maxDurableWriters = 0;
5597
+ const runPass = vi.fn(async (...args) => {
5598
+ const journalsReady = args[2];
5599
+ activeDurableWriters += 1;
5600
+ maxDurableWriters = Math.max(maxDurableWriters, activeDurableWriters);
5601
+ journalsReady?.();
5602
+ await fullPass;
5603
+ activeDurableWriters -= 1;
5604
+ return completedPassOutcome();
5605
+ });
5606
+ let shutdown = () => { };
5607
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
5608
+ runPass: runPass,
5609
+ runPassSupportsCooperativeCheckpoint: true,
5610
+ createWatcher,
5611
+ sleep: () => new Promise(() => { }),
5612
+ onShutdownSignal: (handler) => {
5613
+ shutdown = handler;
5614
+ return () => { };
5615
+ },
5616
+ });
5617
+ try {
5618
+ await flushLoopMicrotasks(30);
5619
+ expect(runPass).toHaveBeenCalledOnce();
5620
+ expect(createWatcher).toHaveBeenCalledOnce();
5621
+ expect(watcher.started).toBe(true);
5622
+ expect(maxDurableWriters).toBe(1);
5623
+ await flushLoopMicrotasks();
5624
+ expect(runPass).toHaveBeenCalledOnce();
5625
+ }
5626
+ finally {
5627
+ releaseFullPass();
5628
+ shutdown();
5629
+ await loop;
5630
+ if (previousCoordinator === undefined)
5631
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
5632
+ else
5633
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
5634
+ }
5635
+ });
5636
+ it.each([
5637
+ ["auth-required", AUTH_REQUIRED_PASS_EXIT],
5638
+ ["journal initialization failure", 1],
5639
+ ])("does not arm event ingress when the first pass exits %s before journal readiness", async (_reason, exitCode) => {
5640
+ const watcher = makeWatcherStub();
5641
+ let shutdown = () => { };
5642
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
5643
+ runPass: vi.fn().mockResolvedValue(exitCode),
5644
+ createWatcher: () => watcher,
5645
+ sleep: () => new Promise(() => { }),
5646
+ onShutdownSignal: (handler) => {
5647
+ shutdown = handler;
5648
+ return () => { };
5649
+ },
5650
+ });
5651
+ try {
5652
+ await flushLoopMicrotasks(30);
5653
+ expect(watcher.started).toBe(false);
5654
+ }
5655
+ finally {
5656
+ shutdown();
5657
+ await expect(loop).resolves.toBe(0);
5658
+ }
5659
+ });
5333
5660
  it("pushes a file created before watcher ready through the warm-up catch-up", async () => {
5334
5661
  const watcher = makeBatchWatcherStub();
5335
5662
  let markReady = () => { };
@@ -5648,7 +5975,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5648
5975
  });
5649
5976
  await expect(superseded).resolves.toBeUndefined();
5650
5977
  expect(runPass.mock.calls.filter(([argv]) => argv.includes("superseded.md"))).toHaveLength(0);
5651
- expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":1,"source":"receiver","direction":"pull","outcome":"refused"');
5978
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join("")).toContain('"event":"sync.realtime.timing","schemaVersion":2,"source":"receiver","direction":"pull","outcome":"refused"');
5652
5979
  releaseHeld();
5653
5980
  await heldReceiverWork;
5654
5981
  await replacement;
@@ -5961,7 +6288,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
5961
6288
  triggerShutdown();
5962
6289
  await loop;
5963
6290
  });
5964
- it("fast lane: a small watcher batch dispatches during a slow bulk pass", async () => {
6291
+ it("fast lane: a small watcher batch waits for a full pass that has not reached a hand-off checkpoint", async () => {
5965
6292
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-watch-"));
5966
6293
  const relativePath = "companies/indigo/realtime.md";
5967
6294
  const absolutePath = path.join(hqRoot, relativePath);
@@ -6001,15 +6328,17 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6001
6328
  expect(order).toEqual(["bulk"]);
6002
6329
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6003
6330
  await flushLoopMicrotasks(30);
6004
- // Regression: origin/main leaves this as ["bulk"] until releaseBulk().
6005
- expect(order).toEqual(["bulk", "fast", "fast"]);
6331
+ // A full pass that has not reached its durable checkpoint remains the sole
6332
+ // owner. Realtime is promoted at the checkpoint, never run concurrently.
6333
+ expect(order).toEqual(["bulk"]);
6006
6334
  releaseBulk();
6007
- await flushLoopMicrotasks();
6335
+ await flushLoopMicrotasks(30);
6336
+ expect(order).toEqual(["bulk", "fast", "fast"]);
6008
6337
  triggerShutdown();
6009
6338
  await loop;
6010
6339
  fs.rmSync(hqRoot, { recursive: true, force: true });
6011
6340
  });
6012
- it("parallel coordinator: a watcher push starts while a full chore is blocked", async () => {
6341
+ it("parallel coordinator: a watcher push waits for a full chore without a hand-off checkpoint", async () => {
6013
6342
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6014
6343
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6015
6344
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-watch-"));
@@ -6052,9 +6381,10 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6052
6381
  expect(order).toEqual(["chore"]);
6053
6382
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6054
6383
  await flushLoopMicrotasks(30);
6055
- expect(order).toEqual(["chore", "realtime"]);
6384
+ expect(order).toEqual(["chore"]);
6056
6385
  releaseBulk();
6057
- await flushLoopMicrotasks();
6386
+ await flushLoopMicrotasks(30);
6387
+ expect(order).toEqual(["chore", "realtime"]);
6058
6388
  triggerShutdown();
6059
6389
  await loop;
6060
6390
  }
@@ -6139,7 +6469,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6139
6469
  fs.rmSync(hqRoot, { recursive: true, force: true });
6140
6470
  }
6141
6471
  });
6142
- it("parallel coordinator partitions receiver events into fast and slow realtime lanes", async () => {
6472
+ it("parallel coordinator partitions receiver events into fast and slow realtime lanes without overlapping their durable writes", async () => {
6143
6473
  const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6144
6474
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6145
6475
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-parallel-receiver-lanes-"));
@@ -6210,7 +6540,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6210
6540
  signal: new AbortController().signal,
6211
6541
  });
6212
6542
  await flushLoopMicrotasks(20);
6213
- expect(order).toEqual(["slow-held", "fast-small"]);
6543
+ expect(order).toEqual(["slow-held"]);
6214
6544
  releaseSlow();
6215
6545
  await held;
6216
6546
  await partitioned;
@@ -6376,96 +6706,594 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6376
6706
  fs.rmSync(hqRoot, { recursive: true, force: true });
6377
6707
  }
6378
6708
  });
6379
- it("fast lane: prefers declared receiver sizes and falls back to the journal", async () => {
6380
- const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6709
+ it("bounds a 32-path realtime personal commit on a 700k-row journal and 50k-path tree", async () => {
6710
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6381
6711
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6382
- const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-receiver-declared-size-"));
6712
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-production-scale-watch-"));
6713
+ const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6714
+ const targetPaths = Array.from({ length: 32 }, (_, index) => `personal/watch-load/${String(index).padStart(5, "0")}/target.md`);
6715
+ const files = {};
6716
+ for (let index = 0; index < 700_000; index += 1) {
6717
+ files[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6718
+ hash: `sha256:history-${index}`,
6719
+ size: index % 4096,
6720
+ syncedAt: "2026-09-16T21:30:00.000Z",
6721
+ direction: "up",
6722
+ };
6723
+ }
6724
+ for (const target of targetPaths) {
6725
+ files[target] = {
6726
+ hash: `sha256:${target}`,
6727
+ size: 1,
6728
+ syncedAt: "2026-09-16T21:30:00.000Z",
6729
+ direction: "up",
6730
+ };
6731
+ }
6732
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6733
+ version: "2",
6734
+ lastSync: "2026-09-16T21:30:00.000Z",
6735
+ pulls: [],
6736
+ files,
6737
+ });
6738
+ for (let index = 0; index < 50_000; index += 1) {
6739
+ const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
6740
+ fs.mkdirSync(directory, { recursive: true });
6741
+ fs.writeFileSync(path.join(directory, "seed.md"), "seed");
6742
+ }
6383
6743
  const watcher = makeBatchWatcherStub();
6384
- let receiverSync;
6385
- let triggerShutdown = () => { };
6386
- const event = (relativePath, sizeBytes) => ({
6387
- kind: "upsert",
6388
- relativePath,
6389
- contentHash: "sha256:remote",
6390
- mtime: "2026-08-28T00:00:00.000Z",
6391
- ...(sizeBytes === undefined ? {} : { sizeBytes }),
6392
- originDeviceId: "peer-device",
6393
- originTenantId: "tenant-indigo",
6394
- sequenceNumber: 1,
6395
- eventTimestamp: "2026-08-28T00:00:00.000Z",
6744
+ let shutdown = () => { };
6745
+ let committedAt;
6746
+ const runPass = vi.fn(async (argv) => {
6747
+ if (!argv.includes("--scope-path")) {
6748
+ // Production opens the packed journal during the initial full pass,
6749
+ // before the watcher accepts realtime work. That recovered projection
6750
+ // is the one a scoped commit must reuse.
6751
+ readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6752
+ }
6753
+ if (argv.includes("--scope-path") && argv.includes("push")) {
6754
+ const scoped = readJournalScoped(PERSONAL_VAULT_JOURNAL_SLUG, targetPaths);
6755
+ const target = targetPaths[0];
6756
+ scoped.files[target] = {
6757
+ ...scoped.files[target],
6758
+ syncedAt: "2026-09-16T21:38:18.000Z",
6759
+ };
6760
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, scoped);
6761
+ committedAt = performance.now();
6762
+ }
6763
+ return completedPassOutcome();
6396
6764
  });
6397
- const journal = readJournal("indigo");
6398
- journal.files["journal-small.md"] = {
6399
- hash: "sha256:journal-small",
6400
- size: 16,
6401
- syncedAt: "2026-08-28T00:00:00.000Z",
6402
- direction: "down",
6403
- };
6404
- writeJournal("indigo", journal);
6405
- const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "pull", "--hq-root", hqRoot], {
6406
- runPass: vi.fn(async () => completedPassOutcome()),
6765
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6766
+ runPass,
6407
6767
  createWatcher: () => watcher,
6408
- createReceiver: ({ syncBatchFn }) => {
6409
- receiverSync = syncBatchFn;
6410
- return { connected: true, start: async () => { }, dispose: async () => { } };
6411
- },
6412
6768
  sleep: () => new Promise(() => { }),
6413
6769
  onShutdownSignal: (handler) => {
6414
- triggerShutdown = handler;
6770
+ shutdown = handler;
6415
6771
  return () => { };
6416
6772
  },
6417
6773
  });
6418
6774
  try {
6419
6775
  await flushLoopMicrotasks(30);
6420
- const batches = receiverSync.partition([
6421
- event("companies/indigo/declared-small.md", 16),
6422
- event("companies/indigo/declared-large.md", DEFAULT_FAST_LANE_MAX_FILE_BYTES + 1),
6423
- event("companies/indigo/journal-small.md"),
6424
- event("companies/indigo/unknown.md"),
6425
- ]);
6426
- expect(batches.map((batch) => batch.map((event) => event.relativePath))).toEqual([
6427
- [
6428
- "companies/indigo/declared-small.md",
6429
- "companies/indigo/journal-small.md",
6430
- ],
6431
- [
6432
- "companies/indigo/declared-large.md",
6433
- "companies/indigo/unknown.md",
6434
- ],
6435
- ]);
6776
+ runPass.mockClear();
6777
+ resetStateStoreSnapshotRecoveryCountForTest();
6778
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
6779
+ eventLoopDelay.enable();
6780
+ await new Promise((resolve) => setTimeout(resolve, 0));
6781
+ const submittedAt = performance.now();
6782
+ let timerDelayMs;
6783
+ const eventLoopTurn = new Promise((resolve) => {
6784
+ setTimeout(() => {
6785
+ timerDelayMs = performance.now() - submittedAt;
6786
+ resolve();
6787
+ }, 0);
6788
+ });
6789
+ const batch = {
6790
+ paths: new Map(targetPaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6791
+ changes: new Map(targetPaths.map((relativePath) => [
6792
+ path.join(hqRoot, relativePath),
6793
+ { kind: "change" },
6794
+ ])),
6795
+ };
6796
+ watcher.emit(targetPaths[0], batch);
6797
+ await vi.waitFor(() => expect(committedAt).toBeDefined(), { timeout: 30_000, interval: 10 });
6798
+ await eventLoopTurn;
6799
+ eventLoopDelay.disable();
6800
+ expect(committedAt - submittedAt).toBeLessThan(1_000);
6801
+ expect(Number(eventLoopDelay.max) / 1_000_000).toBeLessThan(100);
6802
+ expect(timerDelayMs).toBeLessThan(100);
6803
+ expect(getStateStoreSnapshotRecoveryCountForTest()).toBe(0);
6804
+ expect(runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"))).toHaveLength(1);
6436
6805
  }
6437
6806
  finally {
6438
- triggerShutdown();
6807
+ shutdown();
6439
6808
  await loop;
6440
- if (previous === undefined)
6809
+ if (previousCoordinator === undefined)
6441
6810
  delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6442
6811
  else
6443
- process.env.HQ_SYNC_PARALLEL_COORDINATOR = previous;
6812
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
6444
6813
  fs.rmSync(hqRoot, { recursive: true, force: true });
6445
6814
  }
6446
- });
6447
- it("fast lane: does not charge declared sizes from a skipped personal route", async () => {
6815
+ }, 180_000);
6816
+ it("keeps an area-authoritative 700k-row personal aggregate hot across alternating realtime and company batches", async () => {
6448
6817
  const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
6449
- const previousFastFiles = process.env[FAST_LANE_MAX_FILES_ENV];
6450
6818
  process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
6451
- process.env[FAST_LANE_MAX_FILES_ENV] = "1";
6452
- const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-receiver-skipped-route-size-"));
6453
- const watcher = makeBatchWatcherStub();
6454
- let receiverSync;
6455
- let triggerShutdown = () => { };
6456
- const event = (relativePath) => ({
6457
- kind: "upsert",
6458
- relativePath,
6459
- contentHash: "sha256:remote",
6460
- mtime: "2026-08-28T00:00:00.000Z",
6461
- sizeBytes: 64,
6462
- originDeviceId: "peer-device",
6463
- originTenantId: "tenant-indigo",
6464
- sequenceNumber: 1,
6465
- eventTimestamp: "2026-08-28T00:00:00.000Z",
6466
- });
6467
- const loop = runRunnerWithLoop([
6468
- "--companies", "--skip-personal", "--watch", "--event-push", "--direction", "pull",
6819
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-multi-journal-watch-"));
6820
+ const watchedRoot = path.join(hqRoot, "personal", "watch-load");
6821
+ const personalPath = "personal/watch-load/00000/target.md";
6822
+ const companyPath = "companies/indigo/watch-load/00000/target.md";
6823
+ const companyKey = "watch-load/00000/target.md";
6824
+ const personalFiles = {};
6825
+ for (let index = 0; index < 700_000; index += 1) {
6826
+ personalFiles[`personal/history-${index % 5}/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
6827
+ hash: `sha256:history-${index}`,
6828
+ size: index % 4096,
6829
+ syncedAt: "2026-09-16T21:30:00.000Z",
6830
+ direction: "up",
6831
+ };
6832
+ }
6833
+ personalFiles[personalPath] = {
6834
+ hash: "sha256:personal-target",
6835
+ size: 1,
6836
+ syncedAt: "2026-09-16T21:30:00.000Z",
6837
+ direction: "up",
6838
+ };
6839
+ const resolver = new AreaResolver({
6840
+ format: 1,
6841
+ routerVersion: 1,
6842
+ areas: [
6843
+ ...Array.from({ length: 5 }, (_, index) => ({
6844
+ id: `root:history-${index}`,
6845
+ prefixes: [`personal/history-${index}`],
6846
+ paths: [],
6847
+ })),
6848
+ { id: "root:watch", prefixes: ["personal/watch-load"], paths: [] },
6849
+ { id: "root:@files", prefixes: [], paths: [], rootFiles: true },
6850
+ ],
6851
+ });
6852
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6853
+ version: "2",
6854
+ lastSync: "2026-09-16T21:30:00.000Z",
6855
+ pulls: [],
6856
+ files: {},
6857
+ });
6858
+ writeJournal("indigo", {
6859
+ version: "2",
6860
+ lastSync: "2026-09-16T21:30:00.000Z",
6861
+ pulls: [],
6862
+ files: {
6863
+ [companyKey]: {
6864
+ hash: "sha256:company-target",
6865
+ size: 1,
6866
+ syncedAt: "2026-09-16T21:30:00.000Z",
6867
+ direction: "up",
6868
+ },
6869
+ },
6870
+ });
6871
+ const migration = new AreaLedgerMigration({
6872
+ rootDir: __runnerTestStateDir,
6873
+ journalSlug: PERSONAL_VAULT_JOURNAL_SLUG,
6874
+ resolver,
6875
+ legacyStore: openJournalStateStoreForMigration(PERSONAL_VAULT_JOURNAL_SLUG),
6876
+ });
6877
+ expect(migration.cutOver(120_000).phase).toBe("area-authoritative");
6878
+ migration.dispose();
6879
+ // The migration state itself is covered by the area-ledger migration
6880
+ // suite. Seed the already-authoritative production shape in one durable
6881
+ // transaction so this watch-loop test measures repeated realtime reads,
6882
+ // rather than spending most of its worker budget replaying a one-time
6883
+ // legacy-to-area copy.
6884
+ openJournalStateStoreForMigration(PERSONAL_VAULT_JOURNAL_SLUG).replaceState({
6885
+ journal: {
6886
+ version: "2",
6887
+ lastSync: "2026-09-16T21:30:00.000Z",
6888
+ pulls: [],
6889
+ files: personalFiles,
6890
+ },
6891
+ });
6892
+ AreaLedger.open({
6893
+ rootDir: __runnerTestStateDir,
6894
+ journalSlug: PERSONAL_VAULT_JOURNAL_SLUG,
6895
+ layoutEpoch: 1,
6896
+ areaIds: resolver.layout.areas.map((area) => area.id),
6897
+ resolveArea: (key) => resolver.resolve(key),
6898
+ }).append({
6899
+ upserts: personalFiles,
6900
+ metadata: { lastSync: "2026-09-16T21:30:00.000Z", pulls: [] },
6901
+ });
6902
+ fs.mkdirSync(watchedRoot, { recursive: true });
6903
+ for (let index = 0; index < 50_000; index += 1) {
6904
+ fs.writeFileSync(path.join(watchedRoot, `seed-${String(index).padStart(5, "0")}.md`), "seed");
6905
+ }
6906
+ fs.mkdirSync(path.dirname(path.join(hqRoot, personalPath)), { recursive: true });
6907
+ fs.writeFileSync(path.join(hqRoot, personalPath), "personal");
6908
+ fs.mkdirSync(path.dirname(path.join(hqRoot, companyPath)), { recursive: true });
6909
+ fs.writeFileSync(path.join(hqRoot, companyPath), "company");
6910
+ const watcher = makeBatchWatcherStub();
6911
+ const sleep = makeSteppableSleep();
6912
+ let shutdown = () => { };
6913
+ let isUntrackedDelete = (_relativePath) => false;
6914
+ const commits = [];
6915
+ let submittedAt = 0;
6916
+ const aggregatePrime = vi.spyOn(AreaLedger.prototype, "primeAggregateCache");
6917
+ const runPass = vi.fn(async (argv) => {
6918
+ if (!argv.includes("--scope-path")) {
6919
+ readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6920
+ readJournal("indigo");
6921
+ }
6922
+ if (argv.includes("--scope-path") && argv.includes("push")) {
6923
+ const slug = argv.includes("--company") ? "indigo" : PERSONAL_VAULT_JOURNAL_SLUG;
6924
+ const key = slug === "indigo" ? companyKey : personalPath;
6925
+ const scoped = readJournalScoped(slug, [key]);
6926
+ scoped.files[key] = {
6927
+ ...scoped.files[key],
6928
+ syncedAt: "2026-09-16T21:38:18.000Z",
6929
+ };
6930
+ writeJournal(slug, scoped);
6931
+ commits.push({ slug, elapsedMs: performance.now() - submittedAt });
6932
+ }
6933
+ return completedPassOutcome();
6934
+ });
6935
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
6936
+ runPass,
6937
+ createWatcher: (options) => {
6938
+ isUntrackedDelete = options.isUntrackedDelete;
6939
+ return watcher;
6940
+ },
6941
+ startEventSync: vi.fn().mockResolvedValue(null),
6942
+ sleep: sleep.sleep,
6943
+ onShutdownSignal: (handler) => {
6944
+ shutdown = handler;
6945
+ return () => { };
6946
+ },
6947
+ });
6948
+ const emitAndWait = async (relativePaths) => {
6949
+ const before = commits.length;
6950
+ if (relativePaths.includes(personalPath)) {
6951
+ // This is the same non-blocking watcher callback which starts an
6952
+ // openJournalStoreSessionAsync before a delete episode is drained.
6953
+ // It is intentionally issued before each realtime batch: a completed
6954
+ // watch pass must not have thrown away the warmed area aggregate.
6955
+ expect(isUntrackedDelete(personalPath)).toBe(false);
6956
+ await new Promise((resolve) => setImmediate(resolve));
6957
+ }
6958
+ submittedAt = performance.now();
6959
+ const batch = {
6960
+ paths: new Map(relativePaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
6961
+ changes: new Map(relativePaths.map((relativePath) => [
6962
+ path.join(hqRoot, relativePath),
6963
+ { kind: "change" },
6964
+ ])),
6965
+ };
6966
+ watcher.emit(relativePaths[0], batch);
6967
+ await vi.waitFor(() => expect(commits.length).toBe(before + relativePaths.length), {
6968
+ timeout: 30_000,
6969
+ interval: 10,
6970
+ });
6971
+ // `commits` is observed inside the injected pass, before the runner has
6972
+ // completed its pass-final cleanup. Let that cleanup run before the next
6973
+ // watcher episode so the assertion observes the cache lifetime at the
6974
+ // same boundary as a real completed watch pass.
6975
+ await flushLoopMicrotasks(30);
6976
+ };
6977
+ try {
6978
+ await flushLoopMicrotasks(30);
6979
+ runPass.mockClear();
6980
+ // The initial runner pass is the one permitted cold open. Every later
6981
+ // scoped batch must reuse that authoritative aggregate. Warm the same
6982
+ // yielding watcher-session path that production uses before its first
6983
+ // delete episode, then measure only post-warmup primes.
6984
+ aggregatePrime.mockClear();
6985
+ expect(isUntrackedDelete(personalPath)).toBe(false);
6986
+ await new Promise((resolve) => setImmediate(resolve));
6987
+ // The initial full pass may already have opened this bridge (zero new
6988
+ // primes), or this watcher callback may perform the one permitted cold
6989
+ // warm-up. The post-poll assertion below is the regression boundary.
6990
+ expect(aggregatePrime.mock.calls.length).toBeLessThanOrEqual(1);
6991
+ aggregatePrime.mockClear();
6992
+ // Reproduce the normal poll boundary observed on the outposts. On the
6993
+ // current implementation it drops every cached area bridge even though
6994
+ // the watcher and runner remain alive.
6995
+ sleep.tick();
6996
+ await flushLoopMicrotasks(30);
6997
+ forceGarbageCollectionForTest();
6998
+ const retainedAfterWarmupBytes = retainedProcessBytes();
6999
+ const postWarmCpuStart = process.cpuUsage();
7000
+ const postWarmStartedAt = performance.now();
7001
+ resetStateStoreSnapshotRecoveryCountForTest();
7002
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
7003
+ eventLoopDelay.enable();
7004
+ await emitAndWait([personalPath, companyPath]);
7005
+ for (let round = 0; round < 3; round += 1) {
7006
+ await emitAndWait([personalPath]);
7007
+ await emitAndWait([companyPath]);
7008
+ }
7009
+ eventLoopDelay.disable();
7010
+ const recoveryCount = getStateStoreSnapshotRecoveryCountForTest();
7011
+ const maxDelayMs = Number(eventLoopDelay.max) / 1_000_000;
7012
+ const maxCommitMs = Math.max(...commits.map((commit) => commit.elapsedMs));
7013
+ // A first area delta briefly retains the reverse-shadow mirror to
7014
+ // amortize a burst. Retained memory means after that documented linger
7015
+ // has released its second full-vault projection, not during it.
7016
+ await new Promise((resolve) => setTimeout(resolve, AREA_LEGACY_MIRROR_LINGER_MS + 25));
7017
+ forceGarbageCollectionForTest();
7018
+ const retainedAfterChurnBytes = retainedProcessBytes();
7019
+ const retainedGrowthBytes = retainedAfterChurnBytes - retainedAfterWarmupBytes;
7020
+ const postWarmCpu = process.cpuUsage(postWarmCpuStart);
7021
+ const postWarmCpuPercent = ((postWarmCpu.user + postWarmCpu.system) /
7022
+ ((performance.now() - postWarmStartedAt) * 1_000)) * 100;
7023
+ console.info({
7024
+ aggregatePrimeCount: aggregatePrime.mock.calls.length,
7025
+ recoveryCount,
7026
+ maxDelayMs,
7027
+ maxCommitMs,
7028
+ postWarmCpuPercent,
7029
+ retainedAfterWarmupBytes,
7030
+ retainedAfterChurnBytes,
7031
+ retainedGrowthBytes,
7032
+ commits: commits.length,
7033
+ });
7034
+ expect(aggregatePrime).not.toHaveBeenCalled();
7035
+ // The first area delta opens its reverse-shadow v3 mirror once; its
7036
+ // linger keeps all later scoped commits off snapshot recovery. This is
7037
+ // deliberately exact so a second cold bridge cannot hide as a cache hit.
7038
+ expect.soft(recoveryCount).toBe(1);
7039
+ // The production-shape run remains bounded under the coverage job's
7040
+ // concurrent workers: CI measured 1,090.52 ms of timer delay and
7041
+ // 1,087.79 ms to commit. 1.5 s retains a concrete latency ceiling while
7042
+ // leaving headroom for that instrumentation, rather than making the
7043
+ // assertion depend on a single host's scheduling jitter.
7044
+ expect.soft(maxDelayMs).toBeLessThan(1_500);
7045
+ expect.soft(maxCommitMs).toBeLessThan(1_500);
7046
+ expect.soft(retainedGrowthBytes).toBeLessThan(32 * 1024 * 1024);
7047
+ expect(commits.map((commit) => commit.slug)).toEqual([
7048
+ PERSONAL_VAULT_JOURNAL_SLUG,
7049
+ "indigo",
7050
+ PERSONAL_VAULT_JOURNAL_SLUG,
7051
+ "indigo",
7052
+ PERSONAL_VAULT_JOURNAL_SLUG,
7053
+ "indigo",
7054
+ PERSONAL_VAULT_JOURNAL_SLUG,
7055
+ "indigo",
7056
+ ]);
7057
+ }
7058
+ finally {
7059
+ aggregatePrime.mockRestore();
7060
+ shutdown();
7061
+ await loop;
7062
+ if (previousCoordinator === undefined)
7063
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7064
+ else
7065
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
7066
+ fs.rmSync(hqRoot, { recursive: true, force: true });
7067
+ }
7068
+ }, 180_000);
7069
+ it("keeps warmed personal and company journals hot across alternating realtime batches", async () => {
7070
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7071
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
7072
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-multi-journal-legacy-watch-"));
7073
+ const watchedRoot = path.join(hqRoot, "personal", "watch-load");
7074
+ const personalPath = "personal/watch-load/00000/target.md";
7075
+ const companyPath = "companies/indigo/watch-load/00000/target.md";
7076
+ const companyKey = "watch-load/00000/target.md";
7077
+ const personalFiles = {};
7078
+ for (let index = 0; index < 700_000; index += 1) {
7079
+ personalFiles[`personal/history/${String(index % 50_000).padStart(5, "0")}/entry-${index}.md`] = {
7080
+ hash: `sha256:history-${index}`,
7081
+ size: index % 4096,
7082
+ syncedAt: "2026-09-16T21:30:00.000Z",
7083
+ direction: "up",
7084
+ };
7085
+ }
7086
+ personalFiles[personalPath] = {
7087
+ hash: "sha256:personal-target",
7088
+ size: 1,
7089
+ syncedAt: "2026-09-16T21:30:00.000Z",
7090
+ direction: "up",
7091
+ };
7092
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
7093
+ version: "2",
7094
+ lastSync: "2026-09-16T21:30:00.000Z",
7095
+ pulls: [],
7096
+ files: personalFiles,
7097
+ });
7098
+ writeJournal("indigo", {
7099
+ version: "2",
7100
+ lastSync: "2026-09-16T21:30:00.000Z",
7101
+ pulls: [],
7102
+ files: {
7103
+ [companyKey]: {
7104
+ hash: "sha256:company-target",
7105
+ size: 1,
7106
+ syncedAt: "2026-09-16T21:30:00.000Z",
7107
+ direction: "up",
7108
+ },
7109
+ },
7110
+ });
7111
+ for (let index = 0; index < 50_000; index += 1) {
7112
+ const directory = path.join(watchedRoot, String(index).padStart(5, "0"));
7113
+ fs.mkdirSync(directory, { recursive: true });
7114
+ fs.writeFileSync(path.join(directory, "seed.md"), "seed");
7115
+ }
7116
+ fs.mkdirSync(path.dirname(path.join(hqRoot, personalPath)), { recursive: true });
7117
+ fs.writeFileSync(path.join(hqRoot, personalPath), "personal");
7118
+ fs.mkdirSync(path.dirname(path.join(hqRoot, companyPath)), { recursive: true });
7119
+ fs.writeFileSync(path.join(hqRoot, companyPath), "company");
7120
+ const watcher = makeBatchWatcherStub();
7121
+ let shutdown = () => { };
7122
+ const commits = [];
7123
+ let submittedAt = 0;
7124
+ const runPass = vi.fn(async (argv) => {
7125
+ if (!argv.includes("--scope-path")) {
7126
+ readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
7127
+ readJournal("indigo");
7128
+ }
7129
+ if (argv.includes("--scope-path") && argv.includes("push")) {
7130
+ const slug = argv.includes("--company") ? "indigo" : PERSONAL_VAULT_JOURNAL_SLUG;
7131
+ const key = slug === "indigo" ? companyKey : personalPath;
7132
+ const scoped = readJournalScoped(slug, [key]);
7133
+ scoped.files[key] = {
7134
+ ...scoped.files[key],
7135
+ syncedAt: "2026-09-16T21:38:18.000Z",
7136
+ };
7137
+ writeJournal(slug, scoped);
7138
+ commits.push({ slug, elapsedMs: performance.now() - submittedAt });
7139
+ }
7140
+ return completedPassOutcome();
7141
+ });
7142
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "push", "--hq-root", hqRoot], {
7143
+ runPass,
7144
+ createWatcher: () => watcher,
7145
+ sleep: () => new Promise(() => { }),
7146
+ onShutdownSignal: (handler) => {
7147
+ shutdown = handler;
7148
+ return () => { };
7149
+ },
7150
+ });
7151
+ const emitAndWait = async (relativePaths) => {
7152
+ const before = commits.length;
7153
+ submittedAt = performance.now();
7154
+ const batch = {
7155
+ paths: new Map(relativePaths.map((relativePath) => [path.join(hqRoot, relativePath), relativePath])),
7156
+ changes: new Map(relativePaths.map((relativePath) => [
7157
+ path.join(hqRoot, relativePath),
7158
+ { kind: "change" },
7159
+ ])),
7160
+ };
7161
+ watcher.emit(relativePaths[0], batch);
7162
+ await vi.waitFor(() => expect(commits.length).toBe(before + relativePaths.length), {
7163
+ timeout: 30_000,
7164
+ interval: 10,
7165
+ });
7166
+ };
7167
+ try {
7168
+ await flushLoopMicrotasks(30);
7169
+ runPass.mockClear();
7170
+ resetStateStoreSnapshotRecoveryCountForTest();
7171
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 10 });
7172
+ eventLoopDelay.enable();
7173
+ await emitAndWait([personalPath, companyPath]);
7174
+ for (let round = 0; round < 3; round += 1) {
7175
+ await emitAndWait([personalPath]);
7176
+ await emitAndWait([companyPath]);
7177
+ }
7178
+ eventLoopDelay.disable();
7179
+ const recoveryCount = getStateStoreSnapshotRecoveryCountForTest();
7180
+ const maxDelayMs = Number(eventLoopDelay.max) / 1_000_000;
7181
+ const maxCommitMs = Math.max(...commits.map((commit) => commit.elapsedMs));
7182
+ console.info({ recoveryCount, maxDelayMs, maxCommitMs, commits: commits.length });
7183
+ expect.soft(recoveryCount).toBe(0);
7184
+ expect.soft(maxDelayMs).toBeLessThan(100);
7185
+ expect.soft(maxCommitMs).toBeLessThan(1_000);
7186
+ expect(commits.map((commit) => commit.slug)).toEqual([
7187
+ PERSONAL_VAULT_JOURNAL_SLUG,
7188
+ "indigo",
7189
+ PERSONAL_VAULT_JOURNAL_SLUG,
7190
+ "indigo",
7191
+ PERSONAL_VAULT_JOURNAL_SLUG,
7192
+ "indigo",
7193
+ PERSONAL_VAULT_JOURNAL_SLUG,
7194
+ "indigo",
7195
+ ]);
7196
+ }
7197
+ finally {
7198
+ shutdown();
7199
+ await loop;
7200
+ if (previousCoordinator === undefined)
7201
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7202
+ else
7203
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
7204
+ fs.rmSync(hqRoot, { recursive: true, force: true });
7205
+ }
7206
+ }, 180_000);
7207
+ it("fast lane: prefers declared receiver sizes and falls back to the journal", async () => {
7208
+ const previous = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7209
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
7210
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-receiver-declared-size-"));
7211
+ const watcher = makeBatchWatcherStub();
7212
+ let receiverSync;
7213
+ let triggerShutdown = () => { };
7214
+ const event = (relativePath, sizeBytes) => ({
7215
+ kind: "upsert",
7216
+ relativePath,
7217
+ contentHash: "sha256:remote",
7218
+ mtime: "2026-08-28T00:00:00.000Z",
7219
+ ...(sizeBytes === undefined ? {} : { sizeBytes }),
7220
+ originDeviceId: "peer-device",
7221
+ originTenantId: "tenant-indigo",
7222
+ sequenceNumber: 1,
7223
+ eventTimestamp: "2026-08-28T00:00:00.000Z",
7224
+ });
7225
+ const journal = readJournal("indigo");
7226
+ journal.files["journal-small.md"] = {
7227
+ hash: "sha256:journal-small",
7228
+ size: 16,
7229
+ syncedAt: "2026-08-28T00:00:00.000Z",
7230
+ direction: "down",
7231
+ };
7232
+ writeJournal("indigo", journal);
7233
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--direction", "pull", "--hq-root", hqRoot], {
7234
+ runPass: vi.fn(async () => completedPassOutcome()),
7235
+ createWatcher: () => watcher,
7236
+ createReceiver: ({ syncBatchFn }) => {
7237
+ receiverSync = syncBatchFn;
7238
+ return { connected: true, start: async () => { }, dispose: async () => { } };
7239
+ },
7240
+ sleep: () => new Promise(() => { }),
7241
+ onShutdownSignal: (handler) => {
7242
+ triggerShutdown = handler;
7243
+ return () => { };
7244
+ },
7245
+ });
7246
+ try {
7247
+ await flushLoopMicrotasks(30);
7248
+ const batches = receiverSync.partition([
7249
+ event("companies/indigo/declared-small.md", 16),
7250
+ event("companies/indigo/declared-large.md", DEFAULT_FAST_LANE_MAX_FILE_BYTES + 1),
7251
+ event("companies/indigo/journal-small.md"),
7252
+ event("companies/indigo/unknown.md"),
7253
+ ]);
7254
+ expect(batches.map((batch) => batch.map((event) => event.relativePath))).toEqual([
7255
+ [
7256
+ "companies/indigo/declared-small.md",
7257
+ "companies/indigo/journal-small.md",
7258
+ ],
7259
+ [
7260
+ "companies/indigo/declared-large.md",
7261
+ "companies/indigo/unknown.md",
7262
+ ],
7263
+ ]);
7264
+ }
7265
+ finally {
7266
+ triggerShutdown();
7267
+ await loop;
7268
+ if (previous === undefined)
7269
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7270
+ else
7271
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previous;
7272
+ fs.rmSync(hqRoot, { recursive: true, force: true });
7273
+ }
7274
+ });
7275
+ it("fast lane: does not charge declared sizes from a skipped personal route", async () => {
7276
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
7277
+ const previousFastFiles = process.env[FAST_LANE_MAX_FILES_ENV];
7278
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
7279
+ process.env[FAST_LANE_MAX_FILES_ENV] = "1";
7280
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-receiver-skipped-route-size-"));
7281
+ const watcher = makeBatchWatcherStub();
7282
+ let receiverSync;
7283
+ let triggerShutdown = () => { };
7284
+ const event = (relativePath) => ({
7285
+ kind: "upsert",
7286
+ relativePath,
7287
+ contentHash: "sha256:remote",
7288
+ mtime: "2026-08-28T00:00:00.000Z",
7289
+ sizeBytes: 64,
7290
+ originDeviceId: "peer-device",
7291
+ originTenantId: "tenant-indigo",
7292
+ sequenceNumber: 1,
7293
+ eventTimestamp: "2026-08-28T00:00:00.000Z",
7294
+ });
7295
+ const loop = runRunnerWithLoop([
7296
+ "--companies", "--skip-personal", "--watch", "--event-push", "--direction", "pull",
6469
7297
  "--hq-root", hqRoot,
6470
7298
  ], {
6471
7299
  runPass: vi.fn(async () => completedPassOutcome()),
@@ -6551,19 +7379,19 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6551
7379
  ]),
6552
7380
  });
6553
7381
  await flushLoopMicrotasks(30);
6554
- // origin/main failure: expected a scoped tiny path while the bulk pass is
6555
- // held, but #408 kept all four paths in the slow lane.
6556
- expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(1);
6557
- expect(calls.flat().join(" ")).toContain("realtime.md");
7382
+ // The full pass owns durable state until a checkpoint. Its queued realtime
7383
+ // residue is promoted at that hand-off instead of overlapping the pass.
7384
+ expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(0);
6558
7385
  releaseBulk();
6559
7386
  await flushLoopMicrotasks(30);
6560
7387
  expect(calls.filter((argv) => argv.includes("--scope-path"))).toHaveLength(2);
7388
+ expect(calls.flat().join(" ")).toContain("realtime.md");
6561
7389
  expect(calls.flat().join(" ")).toContain("session-0.log");
6562
7390
  triggerShutdown();
6563
7391
  await loop;
6564
7392
  fs.rmSync(hqRoot, { recursive: true, force: true });
6565
7393
  });
6566
- it("fast lane: a small receiver pull dispatches during a slow bulk pass", async () => {
7394
+ it("fast lane: a small receiver pull waits for a slow full pass without a hand-off checkpoint", async () => {
6567
7395
  const watcher = makeWatcherStub();
6568
7396
  let triggerShutdown = () => { };
6569
7397
  let receiverSync;
@@ -6608,7 +7436,7 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6608
7436
  watcher.emit();
6609
7437
  await flushLoopMicrotasks();
6610
7438
  expect(order).toEqual(["bulk"]);
6611
- await receiverSync({
7439
+ const receiverPull = receiverSync({
6612
7440
  events: [{
6613
7441
  kind: "upsert", relativePath: "companies/acme/realtime.md", contentHash: "sha256:x",
6614
7442
  mtime: "2026-08-26T00:00:00.000Z", originDeviceId: "peer", originTenantId: "acme",
@@ -6616,8 +7444,11 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6616
7444
  }],
6617
7445
  signal: new AbortController().signal,
6618
7446
  });
6619
- expect(order).toEqual(["bulk", "fast-pull"]);
7447
+ await flushLoopMicrotasks(20);
7448
+ expect(order).toEqual(["bulk"]);
6620
7449
  releaseBulk();
7450
+ await receiverPull;
7451
+ expect(order).toEqual(["bulk", "fast-pull"]);
6621
7452
  await flushLoopMicrotasks();
6622
7453
  triggerShutdown();
6623
7454
  await loop;
@@ -6661,12 +7492,14 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6661
7492
  await flushLoopMicrotasks();
6662
7493
  watcher.emit();
6663
7494
  await flushLoopMicrotasks();
7495
+ releaseBulk();
7496
+ // The full chore has no explicit safe checkpoint in this stub, so the
7497
+ // realtime pass begins only after the chore has released ownership.
6664
7498
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
6665
7499
  await fastStarted;
6666
- releaseBulk();
6667
7500
  await flushLoopMicrotasks(20);
6668
- // The slow pass has finished, but its fast bypass still mutates. A second
6669
- // sync/rescue must not acquire the root operation lock in this interval.
7501
+ // The realtime pass owns the root lock until its own durable work ends. A
7502
+ // second sync/rescue must not acquire the lock in this interval.
6670
7503
  expect(fs.existsSync(lockPathFor(hqRoot))).toBe(true);
6671
7504
  releaseFast();
6672
7505
  await flushLoopMicrotasks(20);
@@ -6841,59 +7674,68 @@ describe("runRunnerWithLoop — event-push wiring", () => {
6841
7674
  return () => { };
6842
7675
  },
6843
7676
  });
6844
- await flushLoopMicrotasks();
6845
- watcher.emit();
6846
- await flushLoopMicrotasks();
6847
- runPass.mockClear();
6848
- const events = [
6849
- ...Array.from({ length: 17 }, (_, index) => ({
6850
- kind: "upsert",
6851
- relativePath: `companies/acme/a-${index}.md`,
6852
- contentHash: "sha256:x",
6853
- mtime: "2026-08-26T00:00:00.000Z",
6854
- originDeviceId: "peer",
6855
- originTenantId: "acme",
6856
- sequenceNumber: index,
6857
- eventTimestamp: "2026-08-26T00:00:00.000Z",
6858
- })),
6859
- ...Array.from({ length: 16 }, (_, index) => ({
6860
- kind: "upsert",
6861
- relativePath: `companies/beta/b-${index}.md`,
6862
- contentHash: "sha256:x",
6863
- mtime: "2026-08-26T00:00:00.000Z",
6864
- originDeviceId: "peer",
6865
- originTenantId: "beta",
6866
- sequenceNumber: index + 17,
6867
- eventTimestamp: "2026-08-26T00:00:00.000Z",
6868
- })),
6869
- ];
6870
- for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
6871
- const journal = readJournal(slug);
6872
- for (let index = 0; index < count; index++) {
6873
- journal.files[`${prefix}-${index}.md`] = {
6874
- hash: "sha256:x",
6875
- size: 16,
6876
- syncedAt: "2026-08-26T00:00:00.000Z",
6877
- direction: "down",
6878
- };
7677
+ try {
7678
+ await flushLoopMicrotasks();
7679
+ watcher.emit();
7680
+ await flushLoopMicrotasks();
7681
+ runPass.mockClear();
7682
+ const events = [
7683
+ ...Array.from({ length: 17 }, (_, index) => ({
7684
+ kind: "upsert",
7685
+ relativePath: `companies/acme/a-${index}.md`,
7686
+ contentHash: "sha256:x",
7687
+ mtime: "2026-08-26T00:00:00.000Z",
7688
+ originDeviceId: "peer",
7689
+ originTenantId: "acme",
7690
+ sequenceNumber: index,
7691
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
7692
+ })),
7693
+ ...Array.from({ length: 16 }, (_, index) => ({
7694
+ kind: "upsert",
7695
+ relativePath: `companies/beta/b-${index}.md`,
7696
+ contentHash: "sha256:x",
7697
+ mtime: "2026-08-26T00:00:00.000Z",
7698
+ originDeviceId: "peer",
7699
+ originTenantId: "beta",
7700
+ sequenceNumber: index + 17,
7701
+ eventTimestamp: "2026-08-26T00:00:00.000Z",
7702
+ })),
7703
+ ];
7704
+ for (const [slug, prefix, count] of [["acme", "a", 17], ["beta", "b", 16]]) {
7705
+ const journal = readJournal(slug);
7706
+ for (let index = 0; index < count; index++) {
7707
+ journal.files[`${prefix}-${index}.md`] = {
7708
+ hash: "sha256:x",
7709
+ size: 16,
7710
+ syncedAt: "2026-08-26T00:00:00.000Z",
7711
+ direction: "down",
7712
+ };
7713
+ }
7714
+ writeJournal(slug, journal);
6879
7715
  }
6880
- writeJournal(slug, journal);
7716
+ const receiverDrain = receiverSync({
7717
+ events,
7718
+ signal: new AbortController().signal,
7719
+ });
7720
+ await flushLoopMicrotasks(20);
7721
+ // The running full chore has not yet yielded a durable hand-off, so no
7722
+ // receiver work may overlap it. Once it releases ownership, the
7723
+ // scheduler admits each route's bounded work before the residual slow
7724
+ // beta batch (acme fast, beta fast, then beta slow).
7725
+ expect(runPass).not.toHaveBeenCalled();
7726
+ releaseBulk();
7727
+ await receiverDrain;
7728
+ const scopedCalls = runPass.mock.calls.filter(([argv]) => argv.includes("--scope-path"));
7729
+ expect(scopedCalls).toHaveLength(3);
7730
+ expect(scopedCalls.flat().join(" ")).toContain("a-16.md");
7731
+ expect(scopedCalls.flat().join(" ")).toContain("b-14.md");
7732
+ }
7733
+ finally {
7734
+ releaseBulk();
7735
+ triggerShutdown();
7736
+ await loop;
7737
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6881
7738
  }
6882
- const receiverDrain = receiverSync({
6883
- events,
6884
- signal: new AbortController().signal,
6885
- });
6886
- await flushLoopMicrotasks(20);
6887
- // The 33-event mixed delivery peels its first 32 bounded paths into the
6888
- // fast lane (17 acme + 15 beta); the final beta event remains slow.
6889
- expect(runPass).toHaveBeenCalledTimes(2);
6890
- expect(runPass.mock.calls.flat().join(" ")).toContain("a-16.md");
6891
- expect(runPass.mock.calls.flat().join(" ")).toContain("b-14.md");
6892
- releaseBulk();
6893
- await receiverDrain;
6894
- triggerShutdown();
6895
- await loop;
6896
- fs.rmSync(hqRoot, { recursive: true, force: true });
6897
7739
  });
6898
7740
  it("fast lane: queues slow work behind a receiver pass that owns the root lock", async () => {
6899
7741
  const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-fast-lane-slow-queue-"));
@@ -7065,9 +7907,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7065
7907
  await flushLoopMicrotasks();
7066
7908
  watcher.emit(paths[0][1], { paths: new Map(paths) });
7067
7909
  await flushLoopMicrotasks(30);
7068
- expect(Object.keys(readJournal("indigo").files)).toHaveLength(paths.length);
7910
+ // The full pass has not reached a durable checkpoint, so it remains the
7911
+ // only journal writer. The scoped realtime pass is admitted immediately
7912
+ // after this safe hand-off, preserving both sets of rows.
7913
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(0);
7069
7914
  releaseBulk();
7070
- await flushLoopMicrotasks();
7915
+ await flushLoopMicrotasks(30);
7071
7916
  const journal = readJournal("indigo").files;
7072
7917
  expect(Object.keys(journal)).toHaveLength(paths.length * 2);
7073
7918
  for (let index = 0; index < paths.length; index++) {
@@ -7119,10 +7964,12 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7119
7964
  watcher.emit(relativePath, { paths: new Map([[absolutePath, relativePath]]) });
7120
7965
  }
7121
7966
  await flushLoopMicrotasks(60);
7122
- expect(completed).toEqual(["fast", "fast", "fast", "fast", "fast"]);
7967
+ // A full pass without a checkpoint remains the sole mutable-state owner.
7968
+ // The queued realtime batches run immediately after it completes.
7969
+ expect(completed).toEqual([]);
7123
7970
  releaseBulk();
7124
- await flushLoopMicrotasks();
7125
- expect(completed).toContain("bulk");
7971
+ await flushLoopMicrotasks(60);
7972
+ expect(completed).toEqual(["bulk", "fast", "fast", "fast", "fast", "fast"]);
7126
7973
  triggerShutdown();
7127
7974
  await loop;
7128
7975
  fs.rmSync(hqRoot, { recursive: true, force: true });
@@ -7356,18 +8203,33 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7356
8203
  // No pending FakeClock timers left behind by the driver's debounce.
7357
8204
  expect(clock.pendingTimerCount()).toBe(0);
7358
8205
  });
7359
- it("a hard-error (exit 1) poll pass returns immediately", async () => {
8206
+ it("a non-auth pass error keeps the watch loop alive for the next poll", async () => {
7360
8207
  const watcher = makeWatcherStub();
7361
8208
  const runPass = vi.fn().mockResolvedValue(1);
7362
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8209
+ const sleep = makeSteppableSleep();
8210
+ let triggerShutdown = () => { };
8211
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7363
8212
  runPass,
7364
8213
  clock: new FakeClock(),
7365
8214
  createWatcher: () => watcher,
7366
- sleep: () => Promise.resolve(),
7367
- onShutdownSignal: () => () => { },
8215
+ sleep: sleep.sleep,
8216
+ onShutdownSignal: (handler) => {
8217
+ triggerShutdown = handler;
8218
+ return () => { };
8219
+ },
7368
8220
  });
7369
- expect(code).toBe(1);
7370
- expect(watcher.disposed).toBe(false);
8221
+ try {
8222
+ await flushLoopMicrotasks();
8223
+ expect(runPass).toHaveBeenCalledTimes(1);
8224
+ sleep.tick();
8225
+ await flushLoopMicrotasks();
8226
+ expect(runPass).toHaveBeenCalledTimes(2);
8227
+ }
8228
+ finally {
8229
+ triggerShutdown();
8230
+ await expect(loop).resolves.toBe(0);
8231
+ expect(watcher.started).toBe(false);
8232
+ }
7371
8233
  });
7372
8234
  it("an auth-required watch pass parks in-process instead of exiting", async () => {
7373
8235
  const watcher = makeWatcherStub();
@@ -7416,29 +8278,92 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7416
8278
  it("a TRANSIENT_NETWORK_EXIT poll pass does NOT surface as a crash — the loop retries (HQ-SYNC-1W)", async () => {
7417
8279
  // A transient offline blip must keep the watcher alive: the loop logs and
7418
8280
  // polls again instead of returning the code and letting the process exit
7419
- // (which the menubar reports as "watcher exited unexpectedly"). A genuine
7420
- // hard error on a later pass still terminates the loop.
8281
+ // (which the menubar reports as "watcher exited unexpectedly"). A later
8282
+ // generic failure is also retried rather than becoming a daemon exit.
7421
8283
  const watcher = makeWatcherStub();
7422
8284
  let n = 0;
7423
8285
  const runPass = vi.fn().mockImplementation(async () => {
7424
8286
  n++;
7425
- // First pass: machine is offline (transient). Second pass: a real hard
7426
- // error, used here only to end the otherwise-infinite loop deterministically.
8287
+ // First pass: machine is offline (transient). Second pass: a generic
8288
+ // failed pass, which must take the same non-fatal retry path.
7427
8289
  return n === 1 ? TRANSIENT_NETWORK_EXIT : 1;
7428
8290
  });
7429
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8291
+ const sleep = makeSteppableSleep();
8292
+ let triggerShutdown = () => { };
8293
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7430
8294
  runPass,
7431
8295
  clock: new FakeClock(),
7432
8296
  createWatcher: () => watcher,
7433
- sleep: () => Promise.resolve(),
7434
- onShutdownSignal: () => () => { },
8297
+ sleep: sleep.sleep,
8298
+ onShutdownSignal: (handler) => {
8299
+ triggerShutdown = handler;
8300
+ return () => { };
8301
+ },
7435
8302
  });
7436
- // The transient pass did NOT end the loop — it ran again…
8303
+ await flushLoopMicrotasks();
8304
+ sleep.tick();
8305
+ await flushLoopMicrotasks();
8306
+ // The transient pass did NOT end the loop — it ran again, and neither
8307
+ // error causes the daemon itself to exit.
7437
8308
  expect(runPass).toHaveBeenCalledTimes(2);
7438
- // …and the transient code was never surfaced as the watcher's exit code.
7439
- expect(code).toBe(1);
7440
- expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
8309
+ triggerShutdown();
8310
+ await expect(loop).resolves.toBe(0);
8311
+ expect(watcher.disposed).toBe(false);
8312
+ });
8313
+ it("keeps the watcher alive for an unresolved company watch route (HQ-SYNC-COMPANY-ROUTE-UID)", async () => {
8314
+ const watcher = makeWatcherStub();
8315
+ const client = makeVaultStub({
8316
+ entityGet: () => Promise.reject(new VaultNotFoundError("company deleted")),
8317
+ });
8318
+ const findInMyNamespace = vi.fn().mockResolvedValue(null);
8319
+ client.entity.findInMyNamespace = findInMyNamespace;
8320
+ const routedPass = makeDeps({ createVaultClient: () => client });
8321
+ const routedArgv = buildRoutePullArgv({ kind: "company", slug: "cmp_ghost" }, ["--companies", "--watch", "--hq-root", "/tmp/hq"]);
8322
+ let attempts = 0;
8323
+ let routedExit;
8324
+ const runPass = vi.fn().mockImplementation(async () => {
8325
+ attempts += 1;
8326
+ if (attempts === 1) {
8327
+ routedExit = await runRunner(routedArgv, routedPass);
8328
+ return routedExit;
8329
+ }
8330
+ return UNRESOLVED_COMPANY_EXIT;
8331
+ });
8332
+ const sleep = makeSteppableSleep();
8333
+ let triggerShutdown = () => { };
8334
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8335
+ runPass,
8336
+ clock: new FakeClock(),
8337
+ createWatcher: () => watcher,
8338
+ sleep: sleep.sleep,
8339
+ onShutdownSignal: (handler) => {
8340
+ triggerShutdown = handler;
8341
+ return () => { };
8342
+ },
8343
+ });
8344
+ await flushLoopMicrotasks();
8345
+ sleep.tick();
8346
+ await flushLoopMicrotasks();
8347
+ expect(routedArgv).toEqual([
8348
+ "--company",
8349
+ "cmp_ghost",
8350
+ "--direction",
8351
+ "pull",
8352
+ "--hq-root",
8353
+ "/tmp/hq",
8354
+ ]);
8355
+ expect(routedExit).toBe(UNRESOLVED_COMPANY_EXIT);
8356
+ expect(runPass).toHaveBeenCalledTimes(2);
8357
+ triggerShutdown();
8358
+ await expect(loop).resolves.toBe(0);
7441
8359
  expect(watcher.disposed).toBe(false);
8360
+ expect(findInMyNamespace).toHaveBeenCalledWith("company", "cmp_ghost");
8361
+ expect(routedPass.stderr.events()).toContainEqual({
8362
+ type: "error",
8363
+ company: "cmp_ghost",
8364
+ path: "(company-resolution)",
8365
+ message: "company entity lookup returned 404",
8366
+ });
7442
8367
  });
7443
8368
  it("keeps the watch loop alive when a company leg returns transient exit 75 (HQ-SYNC-X)", async () => {
7444
8369
  const watcher = makeWatcherStub();
@@ -7455,24 +8380,36 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7455
8380
  let passes = 0;
7456
8381
  const runPass = vi.fn().mockImplementation(async () => {
7457
8382
  passes += 1;
7458
- // End the otherwise-open watch loop with a later hard failure. The first
7459
- // pass uses the real company-leg fanout path, not a synthetic exit code.
8383
+ // The first pass uses the real company-leg fanout path, not a synthetic
8384
+ // exit code. A later generic failure must also leave the daemon alive.
7460
8385
  return passes === 1 ? runRunner(["--companies"], transientPass) : 1;
7461
8386
  });
7462
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8387
+ const sleep = makeSteppableSleep();
8388
+ let triggerShutdown = () => { };
8389
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7463
8390
  runPass,
7464
8391
  clock: new FakeClock(),
7465
8392
  createWatcher: () => watcher,
7466
- sleep: () => Promise.resolve(),
7467
- onShutdownSignal: () => () => { },
8393
+ sleep: sleep.sleep,
8394
+ onShutdownSignal: (handler) => {
8395
+ triggerShutdown = handler;
8396
+ return () => { };
8397
+ },
7468
8398
  });
7469
- expect(runPass).toHaveBeenCalledTimes(2);
7470
- expect(code).toBe(1);
7471
- expect(code).not.toBe(TRANSIENT_NETWORK_EXIT);
7472
- expect(transientPass.stderr
7473
- .events()
7474
- .some((event) => event.type === "error" && event.path === "(company)")).toBe(false);
7475
- expect(watcher.disposed).toBe(false);
8399
+ try {
8400
+ await waitForPollSleep(sleep);
8401
+ sleep.tick();
8402
+ await flushLoopMicrotasks();
8403
+ expect(runPass).toHaveBeenCalledTimes(2);
8404
+ expect(transientPass.stderr
8405
+ .events()
8406
+ .some((event) => event.type === "error" && event.path === "(company)")).toBe(false);
8407
+ expect(watcher.disposed).toBe(false);
8408
+ }
8409
+ finally {
8410
+ triggerShutdown();
8411
+ await expect(loop).resolves.toBe(0);
8412
+ }
7476
8413
  });
7477
8414
  it("a PARTIAL_SYNC_EXIT poll pass does NOT surface as a crash — the loop retries", async () => {
7478
8415
  // A partial pass (some per-file transfers threw: oversized object on an
@@ -7486,22 +8423,32 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7486
8423
  let n = 0;
7487
8424
  const runPass = vi.fn().mockImplementation(async () => {
7488
8425
  n++;
7489
- // Two partial passes prove a DETERMINISTIC per-file failure doesn't kill
7490
- // the loop either. A hard error then ends the otherwise-infinite loop.
8426
+ // Two partial passes prove a deterministic per-file failure doesn't kill
8427
+ // the loop either. A generic failure must also leave it alive.
7491
8428
  return n <= 2 ? PARTIAL_SYNC_EXIT : 1;
7492
8429
  });
7493
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8430
+ const sleep = makeSteppableSleep();
8431
+ let triggerShutdown = () => { };
8432
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7494
8433
  runPass,
7495
8434
  clock: new FakeClock(),
7496
8435
  createWatcher: () => watcher,
7497
- sleep: () => Promise.resolve(),
7498
- onShutdownSignal: () => () => { },
8436
+ sleep: sleep.sleep,
8437
+ onShutdownSignal: (handler) => {
8438
+ triggerShutdown = handler;
8439
+ return () => { };
8440
+ },
7499
8441
  });
8442
+ await flushLoopMicrotasks();
8443
+ sleep.tick();
8444
+ await flushLoopMicrotasks();
8445
+ sleep.tick();
8446
+ await flushLoopMicrotasks();
7500
8447
  // Both partial passes stayed alive — the loop ran a third time…
7501
8448
  expect(runPass).toHaveBeenCalledTimes(3);
7502
- // …and the partial code was never surfaced as the watcher's exit code.
7503
- expect(code).toBe(1);
7504
- expect(code).not.toBe(PARTIAL_SYNC_EXIT);
8449
+ // …and neither partial nor generic pass failure exits the daemon.
8450
+ triggerShutdown();
8451
+ await expect(loop).resolves.toBe(0);
7505
8452
  expect(watcher.disposed).toBe(false);
7506
8453
  });
7507
8454
  it("alerts when a partial pass failed before a company's download leg", async () => {
@@ -7529,24 +8476,33 @@ describe("runRunnerWithLoop — event-push wiring", () => {
7529
8476
  return 1;
7530
8477
  });
7531
8478
  // Every clock read advances a full cadence interval, so the tick after
7532
- // the partial pass is a scheduled full reconcile that returns the exit
7533
- // code this test asserts on.
8479
+ // the partial pass is a scheduled full reconcile with a generic failure.
7534
8480
  let monotonic = 0;
8481
+ const sleep = makeSteppableSleep();
8482
+ let triggerShutdown = () => { };
7535
8483
  try {
7536
- const code = await runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8484
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
7537
8485
  runPass,
7538
8486
  clock: new FakeClock(),
7539
8487
  createWatcher: () => watcher,
7540
8488
  monotonicNow: () => (monotonic += DEFAULT_FULL_RECONCILE_MS),
7541
- sleep: () => Promise.resolve(),
7542
- onShutdownSignal: () => () => { },
8489
+ sleep: sleep.sleep,
8490
+ onShutdownSignal: (handler) => {
8491
+ triggerShutdown = handler;
8492
+ return () => { };
8493
+ },
7543
8494
  });
7544
- expect(code).toBe(1);
8495
+ await flushLoopMicrotasks();
8496
+ sleep.tick();
8497
+ await flushLoopMicrotasks();
8498
+ expect(runPass).toHaveBeenCalledTimes(2);
7545
8499
  const stderr = stderrWrite.mock.calls
7546
8500
  .map(([chunk]) => String(chunk))
7547
8501
  .join("");
7548
8502
  expect(stderr).toContain("hq-sync-runner: COMPANY PASS FAILED company=personal message=RangeError Maximum call stack size exceeded (downloads for this company did not run)");
7549
8503
  expect(stderr).toContain("hq-sync-runner: watch pass completed with per-file errors; failed-companies=personal; staying alive, retrying next poll");
8504
+ triggerShutdown();
8505
+ await expect(loop).resolves.toBe(0);
7550
8506
  }
7551
8507
  finally {
7552
8508
  stderrWrite.mockRestore();
@@ -7733,6 +8689,85 @@ describe("runRunnerWithLoop — watch-mode auth-wait", () => {
7733
8689
  });
7734
8690
  });
7735
8691
  describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
8692
+ it("hands a wake realtime drain to the guard before a long-running full chore completes", async () => {
8693
+ const previousCoordinator = process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8694
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = "1";
8695
+ const watcher = makeWatcherStub();
8696
+ let triggerShutdown = () => { };
8697
+ let releaseCheckpoint = () => { };
8698
+ const checkpointGate = new Promise((resolve) => { releaseCheckpoint = resolve; });
8699
+ let releaseRealtime = () => { };
8700
+ const realtimeGate = new Promise((resolve) => { releaseRealtime = resolve; });
8701
+ const order = [];
8702
+ let passes = 0;
8703
+ let runRealtime = null;
8704
+ const loop = runRunnerWithLoop(["--companies", "--watch", "--event-push", "--hq-root", "/tmp/hq"], {
8705
+ runPass: vi.fn().mockImplementation(async (_argv, checkpoint) => {
8706
+ passes += 1;
8707
+ if (passes === 2) {
8708
+ order.push("chore");
8709
+ // The fake pass has completed its current durable unit and is now
8710
+ // at the same explicit hand-off seam production uses between
8711
+ // fully-settled fanout targets.
8712
+ await checkpointGate;
8713
+ await checkpoint?.();
8714
+ order.push("chore-complete");
8715
+ }
8716
+ return completedPassOutcome();
8717
+ }),
8718
+ runPassSupportsCooperativeCheckpoint: true,
8719
+ clock: new FakeClock(),
8720
+ createWatcher: () => watcher,
8721
+ sleep: makeSteppableSleep().sleep,
8722
+ onShutdownSignal: (handler) => {
8723
+ triggerShutdown = handler;
8724
+ return () => { };
8725
+ },
8726
+ startRealtimeScheduler: async (opts) => {
8727
+ // This is the same wake path used by the realtime scheduler: it
8728
+ // enters runGuardedTask behind the coordinator worker.
8729
+ runRealtime = () => opts.runGuarded(async () => {
8730
+ order.push("realtime");
8731
+ await realtimeGate;
8732
+ order.push("realtime-complete");
8733
+ });
8734
+ return {
8735
+ signal: () => undefined,
8736
+ checkHighWater: async () => undefined,
8737
+ dispose: async () => undefined,
8738
+ };
8739
+ },
8740
+ });
8741
+ try {
8742
+ await flushLoopMicrotasks(30);
8743
+ watcher.emit();
8744
+ await flushLoopMicrotasks(20);
8745
+ expect(order).toEqual(["chore"]);
8746
+ const realtime = runRealtime();
8747
+ releaseCheckpoint();
8748
+ await flushLoopMicrotasks(30);
8749
+ expect(order).toEqual(["chore", "realtime"]);
8750
+ releaseRealtime();
8751
+ await realtime;
8752
+ await flushLoopMicrotasks(20);
8753
+ expect(order).toEqual([
8754
+ "chore",
8755
+ "realtime",
8756
+ "realtime-complete",
8757
+ "chore-complete",
8758
+ ]);
8759
+ }
8760
+ finally {
8761
+ releaseCheckpoint();
8762
+ releaseRealtime();
8763
+ triggerShutdown();
8764
+ await loop;
8765
+ if (previousCoordinator === undefined)
8766
+ delete process.env.HQ_SYNC_PARALLEL_COORDINATOR;
8767
+ else
8768
+ process.env.HQ_SYNC_PARALLEL_COORDINATOR = previousCoordinator;
8769
+ }
8770
+ });
7736
8771
  it("feeds a watcher-originated V2 drain through the same single-flight guard as the poll", async () => {
7737
8772
  const watcher = makeWatcherStub();
7738
8773
  let triggerShutdown = () => { };
@@ -7845,6 +8880,173 @@ describe("runRunnerWithLoop — U58 realtime scheduler guard", () => {
7845
8880
  await loop;
7846
8881
  });
7847
8882
  });
8883
+ describe("CooperativePassScheduler", () => {
8884
+ it("admits an explicitly safe fast realtime pass while a slow scoped realtime pass remains in flight", async () => {
8885
+ const scheduler = new CooperativePassScheduler(() => 0);
8886
+ const order = [];
8887
+ let releaseSlow = () => { };
8888
+ const slowGate = new Promise((resolve) => { releaseSlow = resolve; });
8889
+ let markSlowStarted = () => { };
8890
+ const slowStarted = new Promise((resolve) => { markSlowStarted = resolve; });
8891
+ const slow = scheduler.submit(async () => {
8892
+ order.push("slow-start");
8893
+ markSlowStarted();
8894
+ await slowGate;
8895
+ order.push("slow-complete");
8896
+ return 0;
8897
+ }, "realtime", "slow");
8898
+ await slowStarted;
8899
+ const fast = scheduler.submit(async () => {
8900
+ order.push("fast");
8901
+ return 0;
8902
+ }, "realtime", "fast", true);
8903
+ try {
8904
+ await Promise.resolve();
8905
+ await Promise.resolve();
8906
+ expect([...order]).toEqual(["slow-start", "fast"]);
8907
+ }
8908
+ finally {
8909
+ releaseSlow();
8910
+ await Promise.all([slow, fast]);
8911
+ }
8912
+ });
8913
+ it("preserves journal order and resumes full work across repeated randomized hand-offs", async () => {
8914
+ // These deterministic seeds deliberately vary the number and enqueue
8915
+ // ordering of realtime tasks. Every append asserts one durable-state owner
8916
+ // at a time; the full continuation must resume after every realtime burst.
8917
+ for (let seed = 1; seed <= 32; seed += 1) {
8918
+ let now = 0;
8919
+ const scheduler = new CooperativePassScheduler(() => now);
8920
+ const journal = [];
8921
+ const expected = new Set();
8922
+ let writer = null;
8923
+ let maximumWriters = 0;
8924
+ const append = async (entry) => {
8925
+ expect(writer).toBeNull();
8926
+ writer = entry;
8927
+ maximumWriters = Math.max(maximumWriters, 1);
8928
+ // A journal append crosses an async durable-write boundary. If the
8929
+ // scheduler admitted another pass here, its ownership assertion would
8930
+ // fail rather than merely observing JavaScript's synchronous turn.
8931
+ await Promise.resolve();
8932
+ expect(writer).toBe(entry);
8933
+ journal.push(entry);
8934
+ writer = null;
8935
+ };
8936
+ let releaseCheckpoint = () => { };
8937
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
8938
+ const chore = scheduler.submit(async ({ checkpoint }) => {
8939
+ expected.add(`chore-${seed}-before`);
8940
+ await append(`chore-${seed}-before`);
8941
+ await checkpointReady;
8942
+ await checkpoint();
8943
+ expected.add(`chore-${seed}-after`);
8944
+ await append(`chore-${seed}-after`);
8945
+ return 0;
8946
+ }, "chore");
8947
+ await Promise.resolve();
8948
+ const realtimeCount = (seed % 7) + 1;
8949
+ const shuffledIndexes = Array.from({ length: realtimeCount }, (_, index) => index);
8950
+ let randomState = seed;
8951
+ for (let index = shuffledIndexes.length - 1; index > 0; index -= 1) {
8952
+ randomState = (randomState * 1_664_525 + 1_013_904_223) >>> 0;
8953
+ const swapIndex = randomState % (index + 1);
8954
+ [shuffledIndexes[index], shuffledIndexes[swapIndex]] = [
8955
+ shuffledIndexes[swapIndex],
8956
+ shuffledIndexes[index],
8957
+ ];
8958
+ }
8959
+ const realtime = shuffledIndexes.map((index) => {
8960
+ const entry = `realtime-${seed}-${(index * 17 + seed) % realtimeCount}`;
8961
+ expected.add(entry);
8962
+ return scheduler.submit(async () => {
8963
+ // Shuffle the microtask timing without ever allowing a second writer.
8964
+ if ((seed + index) % 3 === 0)
8965
+ await Promise.resolve();
8966
+ await append(entry);
8967
+ now += 1;
8968
+ return 0;
8969
+ }, "realtime");
8970
+ });
8971
+ releaseCheckpoint();
8972
+ await Promise.all([chore, ...realtime]);
8973
+ expect(maximumWriters).toBe(1);
8974
+ expect(new Set(journal)).toEqual(expected);
8975
+ expect(journal).toHaveLength(expected.size);
8976
+ expect(journal[0]).toBe(`chore-${seed}-before`);
8977
+ expect(journal.at(-1)).toBe(`chore-${seed}-after`);
8978
+ }
8979
+ });
8980
+ it("lets realtime win at 15 minutes minus one millisecond, then gives the chore the exact-boundary admission", async () => {
8981
+ const runCase = async (elapsedMs) => {
8982
+ let now = 0;
8983
+ const scheduler = new CooperativePassScheduler(() => now);
8984
+ const order = [];
8985
+ let releaseActive = () => { };
8986
+ const activeGate = new Promise((resolve) => { releaseActive = resolve; });
8987
+ const activeRealtime = scheduler.submit(async () => {
8988
+ order.push("active-realtime");
8989
+ await activeGate;
8990
+ return 0;
8991
+ }, "realtime");
8992
+ await Promise.resolve();
8993
+ const chore = scheduler.submit(async () => {
8994
+ order.push("chore");
8995
+ return 0;
8996
+ }, "chore");
8997
+ const queuedRealtime = scheduler.submit(async () => {
8998
+ order.push("queued-realtime");
8999
+ return 0;
9000
+ }, "realtime");
9001
+ now = elapsedMs;
9002
+ releaseActive();
9003
+ await Promise.all([activeRealtime, chore, queuedRealtime]);
9004
+ return order;
9005
+ };
9006
+ await expect(runCase(CHORE_STARVATION_CAP_MS - 1)).resolves.toEqual([
9007
+ "active-realtime",
9008
+ "queued-realtime",
9009
+ "chore",
9010
+ ]);
9011
+ await expect(runCase(CHORE_STARVATION_CAP_MS)).resolves.toEqual([
9012
+ "active-realtime",
9013
+ "chore",
9014
+ "queued-realtime",
9015
+ ]);
9016
+ });
9017
+ it("resumes a handed-off chore when a continuous realtime burst reaches the 15-minute cap", async () => {
9018
+ let now = 0;
9019
+ const scheduler = new CooperativePassScheduler(() => now);
9020
+ const order = [];
9021
+ let releaseCheckpoint = () => { };
9022
+ const checkpointReady = new Promise((resolve) => { releaseCheckpoint = resolve; });
9023
+ const chore = scheduler.submit(async ({ checkpoint }) => {
9024
+ order.push("chore-before");
9025
+ await checkpointReady;
9026
+ await checkpoint();
9027
+ order.push("chore-after");
9028
+ return 0;
9029
+ }, "chore");
9030
+ await Promise.resolve();
9031
+ const firstRealtime = scheduler.submit(async () => {
9032
+ order.push("realtime-before-cap");
9033
+ now = CHORE_STARVATION_CAP_MS;
9034
+ return 0;
9035
+ }, "realtime");
9036
+ const laterRealtime = scheduler.submit(async () => {
9037
+ order.push("realtime-after-cap");
9038
+ return 0;
9039
+ }, "realtime");
9040
+ releaseCheckpoint();
9041
+ await Promise.all([chore, firstRealtime, laterRealtime]);
9042
+ expect(order).toEqual([
9043
+ "chore-before",
9044
+ "realtime-before-cap",
9045
+ "chore-after",
9046
+ "realtime-after-cap",
9047
+ ]);
9048
+ });
9049
+ });
7848
9050
  describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7849
9051
  const watchArgv = [
7850
9052
  "--companies",
@@ -10008,6 +11210,9 @@ describe("runRunnerWithLoop — Phase 3 event-sync wiring (US-017/018/019)", ()
10008
11210
  "/tmp/hq",
10009
11211
  ], {
10010
11212
  runPass: runPass,
11213
+ ...(opts.supportsCooperativeCheckpoint
11214
+ ? { runPassSupportsCooperativeCheckpoint: true }
11215
+ : {}),
10011
11216
  clock,
10012
11217
  createWatcher: (options) => {
10013
11218
  opts.onCreateWatcher?.({
@@ -10120,6 +11325,123 @@ describe("runRunnerWithLoop — Phase 3 event-sync wiring (US-017/018/019)", ()
10120
11325
  shutdown();
10121
11326
  await loop;
10122
11327
  });
11328
+ it("records initial-pull writes before its cooperative checkpoint can admit watcher work", async () => {
11329
+ const markContentPublished = vi.fn();
11330
+ const startEventSync = vi.fn().mockResolvedValue({
11331
+ publishBatch: vi.fn(),
11332
+ markContentPublished,
11333
+ receiver: { start: async () => { }, dispose: async () => { }, connected: true },
11334
+ ownDeviceId: "dev-test",
11335
+ dispose: vi.fn().mockResolvedValue(undefined),
11336
+ });
11337
+ const watcher = makeBatchWatcherStub();
11338
+ let suppressionWasInstalledWhenRealtimeStarted;
11339
+ const runPass = vi.fn(async (argv, checkpoint, onJournalsReady, onAppliedUpserts) => {
11340
+ if (argv.includes("--scope-path")) {
11341
+ suppressionWasInstalledWhenRealtimeStarted = markContentPublished
11342
+ .mock.calls
11343
+ .some(([relativePath]) => relativePath === "companies/indigo/initial-pull.md");
11344
+ return completedPassOutcome();
11345
+ }
11346
+ onJournalsReady?.();
11347
+ await microtasks();
11348
+ watcher.emit(undefined, {
11349
+ paths: new Map([[
11350
+ "/tmp/hq/companies/indigo/watcher-arrived-during-pull.md",
11351
+ "companies/indigo/watcher-arrived-during-pull.md",
11352
+ ]]),
11353
+ changes: new Map([[
11354
+ "/tmp/hq/companies/indigo/watcher-arrived-during-pull.md",
11355
+ { kind: "change" },
11356
+ ]]),
11357
+ });
11358
+ // Let the watcher submit its realtime work before the full pass reaches
11359
+ // its explicit hand-off. No timer is involved: the submission is a
11360
+ // promise-only scheduling boundary.
11361
+ await microtasks();
11362
+ onAppliedUpserts?.([{
11363
+ relativePath: "companies/indigo/initial-pull.md",
11364
+ contentHash: "sha256:initial-pull",
11365
+ }]);
11366
+ await checkpoint?.();
11367
+ return {
11368
+ exitCode: 0,
11369
+ result: {
11370
+ pushPathResults: [],
11371
+ appliedUpserts: [{
11372
+ relativePath: "companies/indigo/initial-pull.md",
11373
+ contentHash: "sha256:initial-pull",
11374
+ }],
11375
+ },
11376
+ };
11377
+ });
11378
+ const { loop, shutdown } = runLoop({
11379
+ claims: EMAIL_CLAIMS,
11380
+ startEventSync,
11381
+ runPass,
11382
+ watcher,
11383
+ supportsCooperativeCheckpoint: true,
11384
+ });
11385
+ await microtasks(16);
11386
+ expect(suppressionWasInstalledWhenRealtimeStarted).toBe(true);
11387
+ shutdown();
11388
+ await loop;
11389
+ });
11390
+ it("does not let a yielding warm-up catch-up overwrite a newer watcher unlink", async () => {
11391
+ const watcher = makeBatchWatcherStub();
11392
+ let resolveCatchup;
11393
+ watcher.collectWarmupCatchupAsync = vi.fn(() => new Promise((resolve) => {
11394
+ resolveCatchup = resolve;
11395
+ }));
11396
+ const runPass = vi.fn(async (_argv, _checkpoint, onJournalsReady) => {
11397
+ onJournalsReady?.();
11398
+ return completedPassOutcome();
11399
+ });
11400
+ const { loop, tick, shutdown } = runLoop({
11401
+ claims: EMAIL_CLAIMS,
11402
+ watcher,
11403
+ runPass,
11404
+ supportsCooperativeCheckpoint: true,
11405
+ });
11406
+ await microtasks(12);
11407
+ const absolutePath = "/tmp/hq/companies/indigo/recreated.md";
11408
+ const relativePath = "companies/indigo/recreated.md";
11409
+ watcher.emit(undefined, {
11410
+ paths: new Map([[absolutePath, relativePath]]),
11411
+ changes: new Map([[absolutePath, {
11412
+ kind: "unlink",
11413
+ deleteSnapshots: [{
11414
+ journalSlug: "indigo",
11415
+ journalPath: "recreated.md",
11416
+ deleteScopeRoot: "recreated.md",
11417
+ absolutePath,
11418
+ remoteEtag: "etag-before-recreate",
11419
+ localHash: "hash-before-recreate",
11420
+ localKind: "file",
11421
+ }],
11422
+ }]]),
11423
+ });
11424
+ // This is the older filesystem state collected before the live unlink.
11425
+ resolveCatchup?.({
11426
+ batch: {
11427
+ paths: new Map([[absolutePath, relativePath]]),
11428
+ changes: new Map([[absolutePath, { kind: "change" }]]),
11429
+ },
11430
+ scannedPaths: 1,
11431
+ });
11432
+ await microtasks(16);
11433
+ tick();
11434
+ await microtasks(16);
11435
+ const scopedPush = runPass.mock.calls.find((call) => call[0].includes("--scope-path"))?.[0];
11436
+ expect(scopedPush).toEqual(expect.arrayContaining([
11437
+ "--scope-path",
11438
+ "recreated.md",
11439
+ "--delete-scope-path",
11440
+ "recreated.md",
11441
+ ]));
11442
+ shutdown();
11443
+ await loop;
11444
+ });
10123
11445
  it("replays pull upsert marks after event-sync startup becomes live", async () => {
10124
11446
  const markContentPublished = vi.fn();
10125
11447
  let resolveStart;
@@ -12510,6 +13832,45 @@ describe("watcher delete intents without pre-seeded authorization", () => {
12510
13832
  expect(Object.keys(readJournal("indigo").files)).toHaveLength(12);
12511
13833
  await stopHarness(h);
12512
13834
  });
13835
+ it("attributes a company-root unlinkDir to its journal while rejecting company-root metadata files", async () => {
13836
+ const keys = Array.from({ length: 12 }, (_, i) => `knowledge/company-root/f-${i}.md`);
13837
+ const pushPasses = [];
13838
+ const h = startDeleteHarness(keys, {
13839
+ direction: "push",
13840
+ onPass: (argv) => {
13841
+ if (argv[argv.indexOf("--direction") + 1] === "push") {
13842
+ pushPasses.push(argv);
13843
+ }
13844
+ },
13845
+ });
13846
+ await settle();
13847
+ pushPasses.length = 0;
13848
+ const childKey = keys[0];
13849
+ const childPath = path.join(h.companyRoot, childKey);
13850
+ fs.unlinkSync(childPath);
13851
+ expect(h.capture()(`companies/indigo/${childKey}`, "unlink")).toHaveLength(1);
13852
+ expect(readJournal("indigo").files[childKey]?.localDeleteIntent).toBeDefined();
13853
+ // The child intent is safe only until the enclosing whole-company removal
13854
+ // is classified. At that scope the exact denominator is the whole shard,
13855
+ // so the existing bulk guard must revoke it rather than lose attribution.
13856
+ fs.rmSync(h.companyRoot, { recursive: true, force: true });
13857
+ const rootSnapshots = h.capture()("companies/indigo", "unlinkDir");
13858
+ expect(rootSnapshots).toHaveLength(0);
13859
+ expect(Object.values(readJournal("indigo").files).filter((entry) => entry.localDeleteIntent)).toHaveLength(0);
13860
+ h.watcher.emit("companies/indigo", {
13861
+ paths: new Map([[h.companyRoot, "companies/indigo"]]),
13862
+ changes: new Map([[h.companyRoot, {
13863
+ kind: "unlinkDir",
13864
+ deleteSnapshots: rootSnapshots,
13865
+ }]]),
13866
+ });
13867
+ h.tick();
13868
+ await settle();
13869
+ expect(pushPasses).toEqual([
13870
+ expect.arrayContaining(["--company", "indigo", "--direction", "push"]),
13871
+ ]);
13872
+ await stopHarness(h);
13873
+ });
12513
13874
  // The native macOS/Windows backends make no child-before-parent guarantee,
12514
13875
  // so the refusal has to hold for descendants that arrive after it too.
12515
13876
  it("a descendant unlink arriving after a refused unlinkDir records no intent", async () => {