@indigoai-us/hq-cloud 6.16.7 → 6.16.9

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 (46) hide show
  1. package/dist/bin/sync-runner-post-sync-flags.d.ts +27 -0
  2. package/dist/bin/sync-runner-post-sync-flags.d.ts.map +1 -0
  3. package/dist/bin/sync-runner-post-sync-flags.js +58 -0
  4. package/dist/bin/sync-runner-post-sync-flags.js.map +1 -0
  5. package/dist/bin/sync-runner-post-sync-flags.test.d.ts +2 -0
  6. package/dist/bin/sync-runner-post-sync-flags.test.d.ts.map +1 -0
  7. package/dist/bin/sync-runner-post-sync-flags.test.js +104 -0
  8. package/dist/bin/sync-runner-post-sync-flags.test.js.map +1 -0
  9. package/dist/bin/sync-runner-watch-loop.d.ts +21 -7
  10. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-watch-loop.js +79 -21
  12. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.test.js +40 -1
  14. package/dist/bin/sync-runner-watch-loop.test.js.map +1 -1
  15. package/dist/bin/sync-runner.d.ts +6 -0
  16. package/dist/bin/sync-runner.d.ts.map +1 -1
  17. package/dist/bin/sync-runner.js +27 -5
  18. package/dist/bin/sync-runner.js.map +1 -1
  19. package/dist/bin/sync-runner.test.js +404 -38
  20. package/dist/bin/sync-runner.test.js.map +1 -1
  21. package/dist/context.d.ts.map +1 -1
  22. package/dist/context.js +7 -1
  23. package/dist/context.js.map +1 -1
  24. package/dist/context.test.js +24 -0
  25. package/dist/context.test.js.map +1 -1
  26. package/dist/credential-scope-error.d.ts +21 -0
  27. package/dist/credential-scope-error.d.ts.map +1 -0
  28. package/dist/credential-scope-error.js +57 -0
  29. package/dist/credential-scope-error.js.map +1 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1 -0
  33. package/dist/index.js.map +1 -1
  34. package/dist/sync/state-store.d.ts +7 -0
  35. package/dist/sync/state-store.d.ts.map +1 -1
  36. package/dist/sync/state-store.js +113 -17
  37. package/dist/sync/state-store.js.map +1 -1
  38. package/dist/sync/state-store.test.js +79 -1
  39. package/dist/sync/state-store.test.js.map +1 -1
  40. package/dist/vault-client.d.ts +3 -2
  41. package/dist/vault-client.d.ts.map +1 -1
  42. package/dist/vault-client.js +17 -2
  43. package/dist/vault-client.js.map +1 -1
  44. package/dist/vault-client.test.js +49 -0
  45. package/dist/vault-client.test.js.map +1 -1
  46. package/package.json +2 -1
@@ -14,6 +14,7 @@ import { runRunner, runRunnerWithLoop, resolveDeletePolicy, resolveSkipPersonal,
14
14
  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";
15
15
  import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
16
16
  import { setBandwidthGovernorForTesting } from "../bandwidth.js";
17
+ import { POST_SYNC_FLAG_KEYS, } from "./sync-runner-post-sync-flags.js";
17
18
  import { FakeClock, TreeWatcher, } from "../watcher.js";
18
19
  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";
19
20
  import { AreaLedgerMigration, openJournalStateStoreForMigration, PERSONAL_VAULT_JOURNAL_SLUG, readJournal, writeJournal, } from "../journal.js";
@@ -198,6 +199,21 @@ function makeDeps(overrides = {}) {
198
199
  corruptionQuarantineFailed: false,
199
200
  indexDir: null,
200
201
  })),
202
+ postSyncFlags: {
203
+ // Unit tests for the runner keep their existing direct-env contract and
204
+ // never start a real registry fetch. The evaluator itself is covered in
205
+ // sync-runner-post-sync-flags.test.ts with fake registry responses.
206
+ isEnabled: vi.fn((key) => {
207
+ const envVar = key === POST_SYNC_FLAG_KEYS.qmdReindex
208
+ ? "HQ_QMD_REINDEX_ON_SYNC"
209
+ : key === POST_SYNC_FLAG_KEYS.conflictPrune
210
+ ? "HQ_CONFLICT_PRUNE_ON_SYNC"
211
+ : "HQ_AGENT_CODEX_MATERIALIZE";
212
+ return process.env[envVar] !== "0";
213
+ }),
214
+ ready: () => Promise.resolve(),
215
+ refresh: () => Promise.resolve(),
216
+ },
201
217
  reconcileManifest: vi
202
218
  .fn()
203
219
  .mockResolvedValue({ written: false, added: [], updated: [], skipped: [] }),
@@ -2663,6 +2679,247 @@ describe("all-complete aggregate", () => {
2663
2679
  forceCollectionRefresh: true,
2664
2680
  }));
2665
2681
  });
2682
+ it('keeps qmd reindex enabled when HQ_QMD_REINDEX_ON_SYNC is "false" (only exact "0" opts out)', async () => {
2683
+ const previous = process.env.HQ_QMD_REINDEX_ON_SYNC;
2684
+ process.env.HQ_QMD_REINDEX_ON_SYNC = "false";
2685
+ const qmdReindex = vi.fn(() => ({
2686
+ qmdAvailable: true,
2687
+ collectionsAdded: [],
2688
+ pathDriftDetected: [],
2689
+ collectionsRepaired: [],
2690
+ updated: true,
2691
+ embedded: false,
2692
+ pendingDirty: false,
2693
+ lockBusy: false,
2694
+ timedOut: false,
2695
+ corruptionQuarantined: false,
2696
+ corruptionQuarantineFailed: false,
2697
+ indexDir: null,
2698
+ }));
2699
+ try {
2700
+ const deps = makeDeps({
2701
+ createVaultClient: () => makeVaultStub({
2702
+ memberships: [{ companyUid: "cmp_a" }],
2703
+ entityGet: (uid) => Promise.resolve({ uid, slug: "acme" }),
2704
+ }),
2705
+ sync: vi.fn().mockResolvedValue(defaultSyncResult({ changedPaths: ["knowledge/note.md"] })),
2706
+ qmdReindex,
2707
+ });
2708
+ expect(await runRunner(["--companies", "--hq-root", "/tmp/fake-hq"], deps)).toBe(0);
2709
+ expect(qmdReindex).toHaveBeenCalledTimes(1);
2710
+ }
2711
+ finally {
2712
+ if (previous === undefined)
2713
+ delete process.env.HQ_QMD_REINDEX_ON_SYNC;
2714
+ else
2715
+ process.env.HQ_QMD_REINDEX_ON_SYNC = previous;
2716
+ }
2717
+ });
2718
+ it("prunes the conflict ledger by default and preserves it only for exact HQ_CONFLICT_PRUNE_ON_SYNC=0", async () => {
2719
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-conflict-prune-gate-"));
2720
+ const indexPath = path.join(hqRoot, ".hq-conflicts", "index.json");
2721
+ const writeOrphan = () => {
2722
+ fs.mkdirSync(path.dirname(indexPath), { recursive: true });
2723
+ fs.writeFileSync(indexPath, JSON.stringify({
2724
+ version: 1,
2725
+ conflicts: [{
2726
+ id: "orphan",
2727
+ originalPath: "notes.md",
2728
+ conflictPath: "notes.conflict-2026-01-01T00-00-00Z-test.md",
2729
+ detectedAt: "2026-01-01T00:00:00.000Z",
2730
+ side: "pull",
2731
+ machineId: "test",
2732
+ localHash: "sha256:local",
2733
+ remoteHash: "sha256:remote",
2734
+ }],
2735
+ }));
2736
+ };
2737
+ const run = () => runRunner(["--companies", "--hq-root", hqRoot], makeDeps({
2738
+ createVaultClient: () => makeVaultStub({
2739
+ memberships: [{ companyUid: "cmp_a" }],
2740
+ entityGet: (uid) => Promise.resolve({ uid, slug: "acme" }),
2741
+ }),
2742
+ }));
2743
+ const previous = process.env.HQ_CONFLICT_PRUNE_ON_SYNC;
2744
+ try {
2745
+ delete process.env.HQ_CONFLICT_PRUNE_ON_SYNC;
2746
+ writeOrphan();
2747
+ expect(await run()).toBe(0);
2748
+ expect(JSON.parse(fs.readFileSync(indexPath, "utf8")).conflicts).toEqual([]);
2749
+ process.env.HQ_CONFLICT_PRUNE_ON_SYNC = "0";
2750
+ writeOrphan();
2751
+ expect(await run()).toBe(0);
2752
+ expect(JSON.parse(fs.readFileSync(indexPath, "utf8")).conflicts).toHaveLength(1);
2753
+ }
2754
+ finally {
2755
+ if (previous === undefined)
2756
+ delete process.env.HQ_CONFLICT_PRUNE_ON_SYNC;
2757
+ else
2758
+ process.env.HQ_CONFLICT_PRUNE_ON_SYNC = previous;
2759
+ fs.rmSync(hqRoot, { recursive: true, force: true });
2760
+ }
2761
+ });
2762
+ it("materializes Codex instructions for an agent by default, never for a person, and honors exact HQ_AGENT_CODEX_MATERIALIZE=0", async () => {
2763
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-agent-codex-gate-"));
2764
+ const codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "hq-agent-codex-home-"));
2765
+ const agentUid = "agt_01J9AGENTFLAGTEST0000000000";
2766
+ const agentClaims = {
2767
+ sub: "machine-agent",
2768
+ email: "agent@agents.getindigo.ai",
2769
+ "custom:entityType": "agent",
2770
+ "custom:entityUid": agentUid,
2771
+ };
2772
+ const agentToken = `header.${Buffer.from(JSON.stringify(agentClaims)).toString("base64url")}.signature`;
2773
+ const agentEntity = {
2774
+ uid: agentUid,
2775
+ type: "agent",
2776
+ slug: "agent",
2777
+ bucketName: "hq-vault-agent",
2778
+ status: "active",
2779
+ };
2780
+ fs.mkdirSync(path.join(hqRoot, "personal"), { recursive: true });
2781
+ fs.writeFileSync(path.join(hqRoot, "personal", "agents-profile.md"), "agent profile\n");
2782
+ const previousHome = process.env.CODEX_HOME;
2783
+ const previousGate = process.env.HQ_AGENT_CODEX_MATERIALIZE;
2784
+ const runAsAgent = () => runRunner(["--personal", "--hq-root", hqRoot], makeDeps({
2785
+ getAccessToken: vi.fn().mockResolvedValue(agentToken),
2786
+ getIdTokenClaims: () => null,
2787
+ createVaultClient: () => ({
2788
+ ...makeVaultStub(),
2789
+ entity: {
2790
+ get: () => Promise.resolve(undefined),
2791
+ listByType: () => Promise.resolve([agentEntity]),
2792
+ },
2793
+ }),
2794
+ }));
2795
+ try {
2796
+ process.env.CODEX_HOME = codexHome;
2797
+ delete process.env.HQ_AGENT_CODEX_MATERIALIZE;
2798
+ expect(await runAsAgent()).toBe(0);
2799
+ expect(fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8")).toContain("agent profile");
2800
+ fs.rmSync(path.join(codexHome, "AGENTS.md"));
2801
+ process.env.HQ_AGENT_CODEX_MATERIALIZE = "0";
2802
+ expect(await runAsAgent()).toBe(0);
2803
+ expect(fs.existsSync(path.join(codexHome, "AGENTS.md"))).toBe(false);
2804
+ delete process.env.HQ_AGENT_CODEX_MATERIALIZE;
2805
+ const personDeps = makeDeps({
2806
+ createVaultClient: () => makeVaultStub({
2807
+ listPersons: () => Promise.resolve([
2808
+ {
2809
+ uid: "prs_person",
2810
+ type: "person",
2811
+ slug: "person",
2812
+ bucketName: "hq-vault-person",
2813
+ status: "active",
2814
+ },
2815
+ ]),
2816
+ }),
2817
+ });
2818
+ expect(await runRunner(["--personal", "--hq-root", hqRoot], personDeps)).toBe(0);
2819
+ expect(fs.existsSync(path.join(codexHome, "AGENTS.md"))).toBe(false);
2820
+ }
2821
+ finally {
2822
+ if (previousHome === undefined)
2823
+ delete process.env.CODEX_HOME;
2824
+ else
2825
+ process.env.CODEX_HOME = previousHome;
2826
+ if (previousGate === undefined)
2827
+ delete process.env.HQ_AGENT_CODEX_MATERIALIZE;
2828
+ else
2829
+ process.env.HQ_AGENT_CODEX_MATERIALIZE = previousGate;
2830
+ fs.rmSync(hqRoot, { recursive: true, force: true });
2831
+ fs.rmSync(codexHome, { recursive: true, force: true });
2832
+ }
2833
+ });
2834
+ it("lets a registry true override all three legacy opt-outs through one injected evaluator", async () => {
2835
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-post-sync-flags-on-"));
2836
+ const codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "hq-post-sync-codex-on-"));
2837
+ const agentClaims = {
2838
+ sub: "machine-agent",
2839
+ "custom:entityType": "agent",
2840
+ "custom:entityUid": "agt_01J9AGENTFLAGON000000000000",
2841
+ };
2842
+ const agentToken = `header.${Buffer.from(JSON.stringify(agentClaims)).toString("base64url")}.signature`;
2843
+ const evaluator = {
2844
+ isEnabled: vi.fn().mockReturnValue(true),
2845
+ ready: () => Promise.resolve(),
2846
+ refresh: () => Promise.resolve(),
2847
+ };
2848
+ const qmdReindex = vi.fn(() => ({
2849
+ qmdAvailable: true, collectionsAdded: [], pathDriftDetected: [], collectionsRepaired: [],
2850
+ updated: true, embedded: false, pendingDirty: false, lockBusy: false, timedOut: false,
2851
+ corruptionQuarantined: false, corruptionQuarantineFailed: false, indexDir: null,
2852
+ }));
2853
+ const indexPath = path.join(hqRoot, ".hq-conflicts", "index.json");
2854
+ fs.mkdirSync(path.dirname(indexPath), { recursive: true });
2855
+ fs.writeFileSync(indexPath, JSON.stringify({ version: 1, conflicts: [{
2856
+ id: "orphan", originalPath: "notes.md", conflictPath: "notes.conflict-test.md",
2857
+ detectedAt: "2026-01-01T00:00:00.000Z", side: "pull", machineId: "test",
2858
+ localHash: "sha256:local", remoteHash: "sha256:remote",
2859
+ }] }));
2860
+ fs.mkdirSync(path.join(hqRoot, "personal"), { recursive: true });
2861
+ fs.writeFileSync(path.join(hqRoot, "personal", "agents-profile.md"), "agent profile\n");
2862
+ const previous = {
2863
+ qmd: process.env.HQ_QMD_REINDEX_ON_SYNC,
2864
+ prune: process.env.HQ_CONFLICT_PRUNE_ON_SYNC,
2865
+ codex: process.env.HQ_AGENT_CODEX_MATERIALIZE,
2866
+ home: process.env.CODEX_HOME,
2867
+ };
2868
+ try {
2869
+ process.env.HQ_QMD_REINDEX_ON_SYNC = "0";
2870
+ process.env.HQ_CONFLICT_PRUNE_ON_SYNC = "0";
2871
+ process.env.HQ_AGENT_CODEX_MATERIALIZE = "0";
2872
+ process.env.CODEX_HOME = codexHome;
2873
+ const agentEntity = {
2874
+ uid: agentClaims["custom:entityUid"], type: "agent", slug: "agent",
2875
+ bucketName: "hq-vault-agent", status: "active",
2876
+ };
2877
+ const deps = makeDeps({
2878
+ postSyncFlags: evaluator,
2879
+ qmdReindex,
2880
+ getAccessToken: vi.fn().mockResolvedValue(agentToken),
2881
+ getIdTokenClaims: () => null,
2882
+ createVaultClient: () => ({
2883
+ ...makeVaultStub({
2884
+ memberships: [{ companyUid: "cmp_a" }],
2885
+ entityGet: (uid) => Promise.resolve({ uid, slug: "acme" }),
2886
+ }),
2887
+ entity: {
2888
+ get: (uid) => Promise.resolve({ uid, slug: "acme" }),
2889
+ listByType: () => Promise.resolve([agentEntity]),
2890
+ },
2891
+ }),
2892
+ sync: vi.fn().mockResolvedValue(defaultSyncResult({ changedPaths: ["knowledge/note.md"] })),
2893
+ });
2894
+ expect(await runRunner(["--companies", "--hq-root", hqRoot], deps)).toBe(0);
2895
+ expect(qmdReindex).toHaveBeenCalledTimes(1);
2896
+ expect(JSON.parse(fs.readFileSync(indexPath, "utf8")).conflicts).toEqual([]);
2897
+ expect(fs.existsSync(path.join(codexHome, "AGENTS.md"))).toBe(true);
2898
+ expect(evaluator.isEnabled).toHaveBeenCalledWith(POST_SYNC_FLAG_KEYS.qmdReindex);
2899
+ expect(evaluator.isEnabled).toHaveBeenCalledWith(POST_SYNC_FLAG_KEYS.conflictPrune);
2900
+ expect(evaluator.isEnabled).toHaveBeenCalledWith(POST_SYNC_FLAG_KEYS.codexMaterialize);
2901
+ }
2902
+ finally {
2903
+ if (previous.qmd === undefined)
2904
+ delete process.env.HQ_QMD_REINDEX_ON_SYNC;
2905
+ else
2906
+ process.env.HQ_QMD_REINDEX_ON_SYNC = previous.qmd;
2907
+ if (previous.prune === undefined)
2908
+ delete process.env.HQ_CONFLICT_PRUNE_ON_SYNC;
2909
+ else
2910
+ process.env.HQ_CONFLICT_PRUNE_ON_SYNC = previous.prune;
2911
+ if (previous.codex === undefined)
2912
+ delete process.env.HQ_AGENT_CODEX_MATERIALIZE;
2913
+ else
2914
+ process.env.HQ_AGENT_CODEX_MATERIALIZE = previous.codex;
2915
+ if (previous.home === undefined)
2916
+ delete process.env.CODEX_HOME;
2917
+ else
2918
+ process.env.CODEX_HOME = previous.home;
2919
+ fs.rmSync(hqRoot, { recursive: true, force: true });
2920
+ fs.rmSync(codexHome, { recursive: true, force: true });
2921
+ }
2922
+ });
2666
2923
  it("collects company-level errors into the all-complete errors array", async () => {
2667
2924
  const slugs = { cmp_a: "acme", cmp_b: "beta" };
2668
2925
  const deps = makeDeps({
@@ -7496,11 +7753,51 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7496
7753
  restore: () => spy.mockRestore(),
7497
7754
  };
7498
7755
  }
7756
+ function captureWatchLoopJournal() {
7757
+ const decisions = [];
7758
+ const chunks = [];
7759
+ const spy = vi
7760
+ .spyOn(process.stderr, "write")
7761
+ .mockImplementation((chunk) => {
7762
+ if (typeof chunk === "string" && chunk.startsWith("{")) {
7763
+ const parsed = JSON.parse(String(chunk));
7764
+ if (typeof parsed === "object" &&
7765
+ parsed !== null &&
7766
+ parsed.type === "full-reconcile-decision") {
7767
+ decisions.push(parsed);
7768
+ }
7769
+ if (typeof parsed === "object" &&
7770
+ parsed !== null &&
7771
+ parsed.type === "watcher-batch-chunked") {
7772
+ chunks.push(parsed);
7773
+ }
7774
+ }
7775
+ return true;
7776
+ });
7777
+ return {
7778
+ decisions,
7779
+ chunks,
7780
+ clear: () => {
7781
+ decisions.splice(0);
7782
+ chunks.splice(0);
7783
+ },
7784
+ restore: () => spy.mockRestore(),
7785
+ };
7786
+ }
7787
+ function scopePathsFromArgv(argv) {
7788
+ const paths = [];
7789
+ for (let i = 0; i < argv.length; i++) {
7790
+ if (argv[i] === "--scope-path" && argv[i + 1] !== undefined) {
7791
+ paths.push(argv[i + 1]);
7792
+ i += 1;
7793
+ }
7794
+ }
7795
+ return paths;
7796
+ }
7499
7797
  const allTriggerFlags = (matched = []) => ({
7500
7798
  "event-push-surfaces-inactive": matched.includes("event-push-surfaces-inactive"),
7501
7799
  "first-tick": matched.includes("first-tick"),
7502
7800
  "bare-wake": matched.includes("bare-wake"),
7503
- "batch-over-limit": matched.includes("batch-over-limit"),
7504
7801
  "overflow-routes-unknown": matched.includes("overflow-routes-unknown"),
7505
7802
  "watcher-warmup-catch-up": matched.includes("watcher-warmup-catch-up"),
7506
7803
  "watcher-warmup-catch-up-failed": matched.includes("watcher-warmup-catch-up-failed"),
@@ -7604,15 +7901,38 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7604
7901
  await loop;
7605
7902
  capture.restore();
7606
7903
  });
7607
- it("logs batch-over-limit as the sole full-reconcile trigger", async () => {
7608
- const saved = process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
7609
- process.env.HQ_SYNC_EVENT_BATCH_LIMIT = "2";
7904
+ it("logs overflow-routes-unknown as the sole full-reconcile trigger", async () => {
7610
7905
  const capture = captureFullReconcileDecisions();
7906
+ const runPass = vi.fn().mockResolvedValue(completedPassOutcome());
7907
+ const { loop, watcher, tick, shutdown } = startBatchDrainLoop(runPass);
7908
+ await flushLoopMicrotasks();
7909
+ capture.clear();
7910
+ runPass.mockClear();
7911
+ watcher.emit("companies/indigo/a.md", {
7912
+ paths: new Map([["/tmp/hq/companies/indigo/a.md", "companies/indigo/a.md"]]),
7913
+ overflowed: true,
7914
+ });
7915
+ tick();
7916
+ await flushLoopMicrotasks();
7917
+ expectFullReconcileDecision(capture.records[0], "full", [
7918
+ "overflow-routes-unknown",
7919
+ ]);
7920
+ expect(runPass.mock.calls.map((call) => call[0])).toEqual([fullArgv]);
7921
+ shutdown();
7922
+ await loop;
7923
+ capture.restore();
7924
+ });
7925
+ it("does not escalate a fully-enumerated over-limit batch to a full reconcile", async () => {
7926
+ const savedLimit = process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
7927
+ const savedInterval = process.env.HQ_SYNC_FULL_RECONCILE_MS;
7928
+ process.env.HQ_SYNC_EVENT_BATCH_LIMIT = "2";
7929
+ process.env.HQ_SYNC_FULL_RECONCILE_MS = "10800000";
7930
+ const journal = captureWatchLoopJournal();
7611
7931
  try {
7612
7932
  const runPass = vi.fn().mockResolvedValue(completedPassOutcome());
7613
7933
  const { loop, watcher, tick, shutdown } = startBatchDrainLoop(runPass);
7614
7934
  await flushLoopMicrotasks();
7615
- capture.clear();
7935
+ journal.clear();
7616
7936
  runPass.mockClear();
7617
7937
  watcher.emit("companies/indigo/a.md", {
7618
7938
  paths: new Map([
@@ -7622,42 +7942,78 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
7622
7942
  ]),
7623
7943
  });
7624
7944
  tick();
7945
+ await flushLoopMicrotasks(40);
7946
+ const calls = runPass.mock.calls.map((call) => call[0]);
7947
+ expect(calls).not.toContainEqual(fullArgv);
7948
+ expect(journal.decisions.map((record) => record.mode)).not.toContain("full");
7949
+ expect(journal.chunks[0]).toMatchObject({
7950
+ type: "watcher-batch-chunked",
7951
+ batchPathCount: 3,
7952
+ chunkPathCount: 2,
7953
+ remainingPathCount: 1,
7954
+ });
7955
+ const pushScopePaths = calls
7956
+ .filter((argv) => argv.includes("--direction") && argv.includes("push"))
7957
+ .flatMap(scopePathsFromArgv);
7958
+ expect(new Set(pushScopePaths)).toEqual(new Set(["a.md", "b.md", "c.md"]));
7959
+ expect(Math.max(0, ...calls.map((argv) => scopePathsFromArgv(argv).length))).toBeLessThanOrEqual(2);
7960
+ shutdown();
7961
+ await loop;
7962
+ }
7963
+ finally {
7964
+ journal.restore();
7965
+ if (savedLimit === undefined)
7966
+ delete process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
7967
+ else
7968
+ process.env.HQ_SYNC_EVENT_BATCH_LIMIT = savedLimit;
7969
+ if (savedInterval === undefined)
7970
+ delete process.env.HQ_SYNC_FULL_RECONCILE_MS;
7971
+ else
7972
+ process.env.HQ_SYNC_FULL_RECONCILE_MS = savedInterval;
7973
+ }
7974
+ });
7975
+ it("still escalates an overflowed batch with unknown routes to a full reconcile", async () => {
7976
+ const savedLimit = process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
7977
+ const savedInterval = process.env.HQ_SYNC_FULL_RECONCILE_MS;
7978
+ process.env.HQ_SYNC_EVENT_BATCH_LIMIT = "2";
7979
+ process.env.HQ_SYNC_FULL_RECONCILE_MS = "10800000";
7980
+ const journal = captureWatchLoopJournal();
7981
+ try {
7982
+ const runPass = vi.fn().mockResolvedValue(completedPassOutcome());
7983
+ const { loop, watcher, tick, shutdown } = startBatchDrainLoop(runPass);
7625
7984
  await flushLoopMicrotasks();
7626
- expectFullReconcileDecision(capture.records[0], "full", ["batch-over-limit"]);
7627
- expect(capture.records[0]?.batchPathCount).toBe(3);
7985
+ journal.clear();
7986
+ runPass.mockClear();
7987
+ watcher.emit("companies/indigo/a.md", {
7988
+ paths: new Map([
7989
+ ["/tmp/hq/companies/indigo/a.md", "companies/indigo/a.md"],
7990
+ ["/tmp/hq/companies/indigo/b.md", "companies/indigo/b.md"],
7991
+ ["/tmp/hq/companies/indigo/c.md", "companies/indigo/c.md"],
7992
+ ]),
7993
+ overflowed: true,
7994
+ });
7995
+ tick();
7996
+ await flushLoopMicrotasks(40);
7997
+ expectFullReconcileDecision(journal.decisions[0], "full", [
7998
+ "overflow-routes-unknown",
7999
+ ]);
7628
8000
  expect(runPass.mock.calls.map((call) => call[0])).toEqual([fullArgv]);
8001
+ expect(journal.chunks).toEqual([]);
7629
8002
  shutdown();
7630
8003
  await loop;
7631
8004
  }
7632
8005
  finally {
7633
- capture.restore();
7634
- if (saved === undefined)
8006
+ journal.restore();
8007
+ if (savedLimit === undefined)
7635
8008
  delete process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
7636
8009
  else
7637
- process.env.HQ_SYNC_EVENT_BATCH_LIMIT = saved;
8010
+ process.env.HQ_SYNC_EVENT_BATCH_LIMIT = savedLimit;
8011
+ if (savedInterval === undefined)
8012
+ delete process.env.HQ_SYNC_FULL_RECONCILE_MS;
8013
+ else
8014
+ process.env.HQ_SYNC_FULL_RECONCILE_MS = savedInterval;
7638
8015
  }
7639
8016
  });
7640
- it("logs overflow-routes-unknown as the sole full-reconcile trigger", async () => {
7641
- const capture = captureFullReconcileDecisions();
7642
- const runPass = vi.fn().mockResolvedValue(completedPassOutcome());
7643
- const { loop, watcher, tick, shutdown } = startBatchDrainLoop(runPass);
7644
- await flushLoopMicrotasks();
7645
- capture.clear();
7646
- runPass.mockClear();
7647
- watcher.emit("companies/indigo/a.md", {
7648
- paths: new Map([["/tmp/hq/companies/indigo/a.md", "companies/indigo/a.md"]]),
7649
- overflowed: true,
7650
- });
7651
- tick();
7652
- await flushLoopMicrotasks();
7653
- expectFullReconcileDecision(capture.records[0], "full", [
7654
- "overflow-routes-unknown",
7655
- ]);
7656
- expect(runPass.mock.calls.map((call) => call[0])).toEqual([fullArgv]);
7657
- shutdown();
7658
- await loop;
7659
- capture.restore();
7660
- });
7661
8017
  it("logs scheduled-interval as the sole full-reconcile trigger on the legacy cadence", async () => {
7662
8018
  const saved = process.env.HQ_SYNC_FULL_RECONCILE_MS;
7663
8019
  delete process.env.HQ_SYNC_FULL_RECONCILE_MS;
@@ -8436,7 +8792,7 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8436
8792
  await loop;
8437
8793
  capture.restore();
8438
8794
  });
8439
- it("falls back to a full reconcile when an event batch exceeds HQ_SYNC_EVENT_BATCH_LIMIT", async () => {
8795
+ it("chunks an event batch that exceeds HQ_SYNC_EVENT_BATCH_LIMIT instead of a full reconcile", async () => {
8440
8796
  const saved = process.env.HQ_SYNC_EVENT_BATCH_LIMIT;
8441
8797
  process.env.HQ_SYNC_EVENT_BATCH_LIMIT = "2";
8442
8798
  try {
@@ -8464,9 +8820,13 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8464
8820
  ]),
8465
8821
  });
8466
8822
  sleep.tick();
8467
- await flushLoopMicrotasks();
8468
- // Mass change: one full reconcile, no per-path scoped passes.
8469
- expect(runPass.mock.calls.map((call) => call[0])).toEqual([fullArgv]);
8823
+ await flushLoopMicrotasks(40);
8824
+ const calls = runPass.mock.calls.map((call) => call[0]);
8825
+ expect(calls).not.toContainEqual(fullArgv);
8826
+ const pushScopePaths = calls
8827
+ .filter((argv) => argv.includes("push"))
8828
+ .flatMap(scopePathsFromArgv);
8829
+ expect(new Set(pushScopePaths)).toEqual(new Set(["a.md", "b.md", "c.md"]));
8470
8830
  triggerShutdown();
8471
8831
  await loop;
8472
8832
  }
@@ -8516,7 +8876,7 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8516
8876
  triggerShutdown();
8517
8877
  await loop;
8518
8878
  });
8519
- it("a batch above the shipped 10,000-path default forces a full reconcile before the interval", async () => {
8879
+ it("a batch above the shipped 10,000-path default stays on chunked targeted passes before the interval", async () => {
8520
8880
  const saved = process.env.HQ_SYNC_FULL_RECONCILE_MS;
8521
8881
  process.env.HQ_SYNC_FULL_RECONCILE_MS = "10800000";
8522
8882
  try {
@@ -8544,8 +8904,14 @@ describe("runRunnerWithLoop — accumulate-and-drain scoped push", () => {
8544
8904
  ])),
8545
8905
  });
8546
8906
  sleep.tick();
8547
- await flushLoopMicrotasks();
8548
- expect(runPass.mock.calls.map((call) => call[0])).toEqual([fullArgv]);
8907
+ await flushLoopMicrotasks(40);
8908
+ const calls = runPass.mock.calls.map((call) => call[0]);
8909
+ expect(calls).not.toContainEqual(fullArgv);
8910
+ const pushScopeCounts = calls
8911
+ .filter((argv) => argv.includes("push"))
8912
+ .map((argv) => scopePathsFromArgv(argv).length);
8913
+ expect(Math.max(0, ...pushScopeCounts)).toBeLessThanOrEqual(10_000);
8914
+ expect(pushScopeCounts.reduce((sum, count) => sum + count, 0)).toBe(10_001);
8549
8915
  triggerShutdown();
8550
8916
  await loop;
8551
8917
  }