@indigoai-us/hq-cloud 6.14.17 → 6.14.19

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/.github/workflows/ci.yml +35 -0
  2. package/dist/active-company.d.ts +43 -0
  3. package/dist/active-company.d.ts.map +1 -0
  4. package/dist/active-company.js +132 -0
  5. package/dist/active-company.js.map +1 -0
  6. package/dist/active-company.test.d.ts +2 -0
  7. package/dist/active-company.test.d.ts.map +1 -0
  8. package/dist/active-company.test.js +149 -0
  9. package/dist/active-company.test.js.map +1 -0
  10. package/dist/bin/sync-runner-company.d.ts +7 -0
  11. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  12. package/dist/bin/sync-runner-company.js +21 -1
  13. package/dist/bin/sync-runner-company.js.map +1 -1
  14. package/dist/bin/sync-runner-planning.d.ts +27 -1
  15. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  16. package/dist/bin/sync-runner-planning.js +33 -4
  17. package/dist/bin/sync-runner-planning.js.map +1 -1
  18. package/dist/bin/sync-runner-planning.test.d.ts +2 -0
  19. package/dist/bin/sync-runner-planning.test.d.ts.map +1 -0
  20. package/dist/bin/sync-runner-planning.test.js +115 -0
  21. package/dist/bin/sync-runner-planning.test.js.map +1 -0
  22. package/dist/bin/sync-runner-watch-loop.d.ts +10 -2
  23. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  24. package/dist/bin/sync-runner-watch-loop.js +201 -25
  25. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  26. package/dist/bin/sync-runner-watch-routes.d.ts +1 -1
  27. package/dist/bin/sync-runner-watch-routes.d.ts.map +1 -1
  28. package/dist/bin/sync-runner-watch-routes.js +14 -2
  29. package/dist/bin/sync-runner-watch-routes.js.map +1 -1
  30. package/dist/bin/sync-runner.d.ts +39 -9
  31. package/dist/bin/sync-runner.d.ts.map +1 -1
  32. package/dist/bin/sync-runner.js +114 -20
  33. package/dist/bin/sync-runner.js.map +1 -1
  34. package/dist/bin/sync-runner.test.js +852 -8
  35. package/dist/bin/sync-runner.test.js.map +1 -1
  36. package/dist/cli/rescue-core.js +27 -1
  37. package/dist/cli/rescue-core.js.map +1 -1
  38. package/dist/cli/rescue-drop-dir-symlink.test.d.ts +2 -0
  39. package/dist/cli/rescue-drop-dir-symlink.test.d.ts.map +1 -0
  40. package/dist/cli/rescue-drop-dir-symlink.test.js +206 -0
  41. package/dist/cli/rescue-drop-dir-symlink.test.js.map +1 -0
  42. package/dist/cli/share.d.ts +15 -0
  43. package/dist/cli/share.d.ts.map +1 -1
  44. package/dist/cli/share.js +100 -55
  45. package/dist/cli/share.js.map +1 -1
  46. package/dist/cli/share.test.js +119 -6
  47. package/dist/cli/share.test.js.map +1 -1
  48. package/dist/cli/sync.d.ts.map +1 -1
  49. package/dist/cli/sync.js +19 -25
  50. package/dist/cli/sync.js.map +1 -1
  51. package/dist/cli/sync.test.js +121 -0
  52. package/dist/cli/sync.test.js.map +1 -1
  53. package/dist/journal.d.ts +3 -1
  54. package/dist/journal.d.ts.map +1 -1
  55. package/dist/journal.js +13 -2
  56. package/dist/journal.js.map +1 -1
  57. package/dist/journal.test.js +26 -1
  58. package/dist/journal.test.js.map +1 -1
  59. package/dist/manifest-reconcile.d.ts +118 -5
  60. package/dist/manifest-reconcile.d.ts.map +1 -1
  61. package/dist/manifest-reconcile.js +319 -62
  62. package/dist/manifest-reconcile.js.map +1 -1
  63. package/dist/manifest-reconcile.test.js +824 -2
  64. package/dist/manifest-reconcile.test.js.map +1 -1
  65. package/dist/personal-vault.d.ts +12 -0
  66. package/dist/personal-vault.d.ts.map +1 -1
  67. package/dist/personal-vault.js +20 -0
  68. package/dist/personal-vault.js.map +1 -1
  69. package/dist/qmd-reindex.d.ts +88 -11
  70. package/dist/qmd-reindex.d.ts.map +1 -1
  71. package/dist/qmd-reindex.js +354 -65
  72. package/dist/qmd-reindex.js.map +1 -1
  73. package/dist/qmd-reindex.test.d.ts +4 -0
  74. package/dist/qmd-reindex.test.d.ts.map +1 -1
  75. package/dist/qmd-reindex.test.js +285 -3
  76. package/dist/qmd-reindex.test.js.map +1 -1
  77. package/dist/s3.d.ts +13 -0
  78. package/dist/s3.d.ts.map +1 -1
  79. package/dist/s3.js +34 -1
  80. package/dist/s3.js.map +1 -1
  81. package/dist/s3.test.js +60 -1
  82. package/dist/s3.test.js.map +1 -1
  83. package/dist/telemetry.d.ts +22 -0
  84. package/dist/telemetry.d.ts.map +1 -1
  85. package/dist/telemetry.js +79 -0
  86. package/dist/telemetry.js.map +1 -1
  87. package/dist/telemetry.test.js +117 -1
  88. package/dist/telemetry.test.js.map +1 -1
  89. package/dist/watcher.d.ts +26 -1
  90. package/dist/watcher.d.ts.map +1 -1
  91. package/dist/watcher.js +75 -12
  92. package/dist/watcher.js.map +1 -1
  93. package/package.json +1 -1
  94. package/pnpm-workspace.yaml +1 -1
  95. package/src/active-company.test.ts +188 -0
  96. package/src/active-company.ts +168 -0
  97. package/src/bin/sync-runner-company.ts +29 -1
  98. package/src/bin/sync-runner-planning.test.ts +131 -0
  99. package/src/bin/sync-runner-planning.ts +60 -7
  100. package/src/bin/sync-runner-watch-loop.ts +322 -32
  101. package/src/bin/sync-runner-watch-routes.ts +14 -1
  102. package/src/bin/sync-runner.test.ts +1021 -16
  103. package/src/bin/sync-runner.ts +179 -28
  104. package/src/cli/rescue-core.ts +26 -1
  105. package/src/cli/rescue-drop-dir-symlink.test.ts +224 -0
  106. package/src/cli/share.test.ts +150 -6
  107. package/src/cli/share.ts +143 -48
  108. package/src/cli/sync.test.ts +150 -1
  109. package/src/cli/sync.ts +25 -29
  110. package/src/journal.test.ts +45 -0
  111. package/src/journal.ts +18 -1
  112. package/src/manifest-reconcile.test.ts +1019 -3
  113. package/src/manifest-reconcile.ts +424 -66
  114. package/src/personal-vault.ts +23 -0
  115. package/src/qmd-reindex.test.ts +324 -4
  116. package/src/qmd-reindex.ts +414 -76
  117. package/src/s3.test.ts +79 -0
  118. package/src/s3.ts +48 -1
  119. package/src/telemetry.test.ts +128 -0
  120. package/src/telemetry.ts +80 -0
  121. package/src/watcher.ts +111 -14
  122. package/test/e2e/watcher-real-chokidar.test.ts +62 -2
  123. package/test/e2e/watcher-recursive-backend.test.ts +67 -1
  124. package/test/joiner-manifest-reconcile.integration.test.ts +283 -0
@@ -12,9 +12,9 @@ import * as os from "os";
12
12
  import * as path from "path";
13
13
  import { runRunner, runRunnerWithLoop, resolveDeletePolicy, resolveSkipPersonal, selectObjectIOFactory, routeChangeToTarget, buildTargetedPushArgv, resolvePullScope, readPinnedPrefixes, SESSIONS_SCOPE_PREFIX, defaultCollectTelemetry, AUTH_REQUIRED_PASS_EXIT, } from "./sync-runner.js";
14
14
  import { PresignObjectIO, S3SdkObjectIO } from "../object-io.js";
15
- import { FakeClock } from "../watcher.js";
16
- import { adaptivePollMs } from "./sync-runner-watch-loop.js";
17
- import { PERSONAL_VAULT_JOURNAL_SLUG } from "../journal.js";
15
+ import { FakeClock, } from "../watcher.js";
16
+ import { adaptivePollMs, journalEntriesForWatcherDelete, } from "./sync-runner-watch-loop.js";
17
+ import { PERSONAL_VAULT_JOURNAL_SLUG, readJournal, writeJournal, } from "../journal.js";
18
18
  import { HQ_CLOUD_VERSION } from "../version.js";
19
19
  import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
20
20
  import { TRANSIENT_NETWORK_EXIT } from "../lib/net-errors.js";
@@ -22,6 +22,8 @@ import { PARTIAL_SYNC_EXIT } from "../lib/exit-codes.js";
22
22
  import { isCoveredByAny } from "../prefix-coalesce.js";
23
23
  import { VaultAuthError, VaultNotFoundError } from "../vault-client.js";
24
24
  import { CognitoRefreshError } from "../cognito-auth.js";
25
+ import { reconcileCompanyManifest, } from "../manifest-reconcile.js";
26
+ import yaml from "js-yaml";
25
27
  // ---------------------------------------------------------------------------
26
28
  // Hermetic journal state — the runner now calls migratePersonalVaultJournal()
27
29
  // on every --companies fanout (one-time seed of the reserved vault journal
@@ -152,8 +154,13 @@ function makeDeps(overrides = {}) {
152
154
  updated: true,
153
155
  embedded: false,
154
156
  pendingDirty: false,
157
+ lockBusy: false,
158
+ timedOut: false,
159
+ corruptionQuarantined: false,
155
160
  })),
156
- reconcileManifest: vi.fn().mockResolvedValue(undefined),
161
+ reconcileManifest: vi
162
+ .fn()
163
+ .mockResolvedValue({ written: false, added: [], updated: [], skipped: [] }),
157
164
  ...overrides,
158
165
  stdout,
159
166
  stderr,
@@ -196,7 +203,9 @@ describe("argv parsing", () => {
196
203
  const code = await runRunner(["--companies", "--json"], deps);
197
204
  expect(code).toBe(0);
198
205
  // Empty memberships → setup-needed, not a parse error
199
- expect(deps.stdout.events()).toEqual([{ type: "setup-needed" }]);
206
+ expect(deps.stdout.events()).toEqual([
207
+ { type: "setup-needed", reason: "no-memberships" },
208
+ ]);
200
209
  });
201
210
  });
202
211
  // ---------------------------------------------------------------------------
@@ -499,7 +508,67 @@ describe("claim-dance", () => {
499
508
  expect(ensureSpy).not.toHaveBeenCalled();
500
509
  expect(claimSpy).not.toHaveBeenCalled();
501
510
  // No memberships, no invites — truly empty → setup-needed is correct here.
502
- expect(deps.stdout.events()).toEqual([{ type: "setup-needed" }]);
511
+ expect(deps.stdout.events()).toEqual([
512
+ { type: "setup-needed", reason: "no-memberships" },
513
+ ]);
514
+ });
515
+ // The michelle@boringecom.com shape: an invite exists, the claim attempt does
516
+ // not convert it into a membership, and the user is told they are solo. The
517
+ // count is what turns "you have no team" into "you have an invite to accept".
518
+ it("US-004: carries pendingInviteCount when invites did not become memberships", async () => {
519
+ const deps = makeDeps({
520
+ createVaultClient: () => makeVaultStub({
521
+ pendingInvites: [{ uid: "inv_1" }, { uid: "inv_2" }],
522
+ ensurePerson: vi
523
+ .fn()
524
+ .mockResolvedValue({ uid: "ent_person" }),
525
+ claim: vi
526
+ .fn()
527
+ .mockResolvedValue(undefined),
528
+ }),
529
+ getIdTokenClaims: () => claims,
530
+ });
531
+ expect(await runRunner(["--companies"], deps)).toBe(0);
532
+ expect(deps.stdout.events()).toEqual([
533
+ { type: "setup-needed", reason: "no-memberships", pendingInviteCount: 2 },
534
+ ]);
535
+ });
536
+ it("US-004: omits pendingInviteCount entirely when there were no invites", async () => {
537
+ const deps = makeDeps({
538
+ createVaultClient: () => makeVaultStub({ pendingInvites: [] }),
539
+ getIdTokenClaims: () => claims,
540
+ });
541
+ expect(await runRunner(["--companies"], deps)).toBe(0);
542
+ const [event] = deps.stdout.events();
543
+ expect(event).toEqual({ type: "setup-needed", reason: "no-memberships" });
544
+ expect("pendingInviteCount" in event).toBe(false);
545
+ });
546
+ it("US-004: reports claim-dance skipped as a diagnostic, not a bare stderr line", async () => {
547
+ const deps = makeDeps({
548
+ createVaultClient: () => makeVaultStub({
549
+ pendingInvites: [{ uid: "inv_1" }],
550
+ ensurePerson: vi
551
+ .fn()
552
+ .mockRejectedValue(new Error("BatchWriteItem denied")),
553
+ }),
554
+ getIdTokenClaims: () => claims,
555
+ });
556
+ // Non-throwing by contract: the failure never changes the exit code...
557
+ expect(await runRunner(["--companies"], deps)).toBe(0);
558
+ // ...but it is no longer invisible.
559
+ expect(deps.stderr.raw()).toContain("runner.claim_dance.skipped");
560
+ });
561
+ it("US-004: a claim-dance that cannot identify the caller still reports the pending invites", async () => {
562
+ const deps = makeDeps({
563
+ createVaultClient: () => makeVaultStub({ pendingInvites: [{ uid: "inv_1" }] }),
564
+ // No sub/name — the claim cannot proceed, but the invites are still real.
565
+ getIdTokenClaims: () => ({}),
566
+ });
567
+ expect(await runRunner(["--companies"], deps)).toBe(0);
568
+ expect(deps.stdout.events()).toEqual([
569
+ { type: "setup-needed", reason: "no-memberships", pendingInviteCount: 1 },
570
+ ]);
571
+ expect(deps.stderr.raw()).toContain("runner.claim_dance.skipped");
503
572
  });
504
573
  it("skips claim-dance entirely when no idToken claims are available", async () => {
505
574
  const pendingSpy = vi.fn().mockResolvedValue([]);
@@ -569,7 +638,9 @@ describe("target resolution", () => {
569
638
  const deps = makeDeps();
570
639
  const code = await runRunner(["--companies"], deps);
571
640
  expect(code).toBe(0);
572
- expect(deps.stdout.events()).toEqual([{ type: "setup-needed" }]);
641
+ expect(deps.stdout.events()).toEqual([
642
+ { type: "setup-needed", reason: "no-memberships" },
643
+ ]);
573
644
  // sync should NOT have been called — no targets
574
645
  expect(deps.sync).not.toHaveBeenCalled();
575
646
  });
@@ -856,7 +927,11 @@ describe("fanout-plan", () => {
856
927
  const plan = deps.stdout
857
928
  .events()
858
929
  .find((e) => e.type === "fanout-plan");
859
- expect(plan.companies).toEqual([{ uid: "cmp_live", slug: "live" }]);
930
+ // The surviving target now also carries the plan-time entity snapshot the
931
+ // manifest reconciler reads instead of re-fetching (US-002).
932
+ expect(plan.companies).toEqual([
933
+ { uid: "cmp_live", slug: "live", entityType: "company", entityStatus: "active" },
934
+ ]);
860
935
  });
861
936
  it("reconciles after the personal leg completes", async () => {
862
937
  const order = [];
@@ -887,6 +962,7 @@ describe("fanout-plan", () => {
887
962
  }),
888
963
  reconcileManifest: vi.fn().mockImplementation(async () => {
889
964
  order.push("reconcile");
965
+ return { written: false, added: [], updated: [], skipped: [] };
890
966
  }),
891
967
  });
892
968
  expect(await runRunner(["--companies"], deps)).toBe(0);
@@ -1727,6 +1803,9 @@ describe("all-complete aggregate", () => {
1727
1803
  updated: true,
1728
1804
  embedded: false,
1729
1805
  pendingDirty: false,
1806
+ lockBusy: false,
1807
+ timedOut: false,
1808
+ corruptionQuarantined: false,
1730
1809
  }));
1731
1810
  const deps = makeDeps({
1732
1811
  createVaultClient: () => makeVaultStub({
@@ -4583,6 +4662,90 @@ describe("runRunnerWithLoop — Phase 3 event-sync wiring (US-017/018/019)", ()
4583
4662
  shutdown();
4584
4663
  await loop;
4585
4664
  });
4665
+ it("does not publish an exit-zero deletion that the scoped push refused", async () => {
4666
+ const publishBatch = vi.fn();
4667
+ const startEventSync = vi.fn().mockResolvedValue({
4668
+ publishBatch,
4669
+ receiver: { start: async () => { }, dispose: async () => { }, connected: true },
4670
+ ownDeviceId: "dev-test",
4671
+ dispose: vi.fn().mockResolvedValue(undefined),
4672
+ });
4673
+ const runPass = vi
4674
+ .fn()
4675
+ .mockResolvedValueOnce(0)
4676
+ .mockResolvedValueOnce({
4677
+ exitCode: 0,
4678
+ result: {
4679
+ pushPathResults: [{
4680
+ relativePath: "companies/indigo/deleted.md",
4681
+ status: "refused",
4682
+ operation: "delete",
4683
+ reason: "missing-delete-intent",
4684
+ }],
4685
+ },
4686
+ })
4687
+ .mockResolvedValue(0);
4688
+ const { loop, watcher, tick, shutdown } = runLoop({
4689
+ claims: ENROLLED_CLAIMS,
4690
+ startEventSync,
4691
+ runPass,
4692
+ });
4693
+ await microtasks();
4694
+ watcher.emit("companies/indigo/deleted.md", {
4695
+ paths: new Map([[
4696
+ "/tmp/hq/companies/indigo/deleted.md",
4697
+ "companies/indigo/deleted.md",
4698
+ ]]),
4699
+ });
4700
+ tick();
4701
+ await microtasks();
4702
+ expect(publishBatch).not.toHaveBeenCalled();
4703
+ shutdown();
4704
+ await loop;
4705
+ });
4706
+ it("does not publish unlinkDir when a covered descendant delete was refused", async () => {
4707
+ const publishBatch = vi.fn();
4708
+ const startEventSync = vi.fn().mockResolvedValue({
4709
+ publishBatch,
4710
+ receiver: { start: async () => { }, dispose: async () => { }, connected: true },
4711
+ ownDeviceId: "dev-test",
4712
+ dispose: vi.fn().mockResolvedValue(undefined),
4713
+ });
4714
+ const runPass = vi
4715
+ .fn()
4716
+ .mockResolvedValueOnce(0)
4717
+ .mockResolvedValueOnce({
4718
+ exitCode: 0,
4719
+ result: {
4720
+ pushPathResults: [{
4721
+ relativePath: "companies/indigo/archive/deleted.md",
4722
+ status: "refused",
4723
+ operation: "delete",
4724
+ reason: "missing-delete-intent",
4725
+ }],
4726
+ },
4727
+ })
4728
+ .mockResolvedValue(0);
4729
+ const { loop, watcher, tick, shutdown } = runLoop({
4730
+ claims: ENROLLED_CLAIMS,
4731
+ startEventSync,
4732
+ runPass,
4733
+ });
4734
+ await microtasks();
4735
+ const absolutePath = "/tmp/hq/companies/indigo/archive";
4736
+ watcher.emit("companies/indigo/archive", {
4737
+ paths: new Map([[
4738
+ absolutePath,
4739
+ "companies/indigo/archive",
4740
+ ]]),
4741
+ changes: new Map([[absolutePath, { kind: "unlinkDir" }]]),
4742
+ });
4743
+ tick();
4744
+ await microtasks();
4745
+ expect(publishBatch).not.toHaveBeenCalled();
4746
+ shutdown();
4747
+ await loop;
4748
+ });
4586
4749
  it("F04: mixed-route watcher batches publish only routes that were pushed", async () => {
4587
4750
  const publishBatch = vi.fn();
4588
4751
  const startEventSync = vi.fn().mockResolvedValue({
@@ -5342,4 +5505,685 @@ describe("US-004 — --personal for an AGENT machine identity (HARD GATE)", () =
5342
5505
  expect(cmp(HQ_CLOUD_VERSION, FLOOR)).toBeGreaterThanOrEqual(0);
5343
5506
  });
5344
5507
  });
5508
+ // ---------------------------------------------------------------------------
5509
+ // US-002 — post-fanout manifest reconciliation wiring
5510
+ //
5511
+ // The reconciler itself is unit-tested in manifest-reconcile.test.ts. These
5512
+ // tests own the SEAM: which run modes reach it, which targets cross it, and
5513
+ // what the runner does with the result it hands back.
5514
+ // ---------------------------------------------------------------------------
5515
+ describe("US-002 — manifest reconciliation wiring", () => {
5516
+ const PERSON_ENTITY = {
5517
+ uid: "prs_me",
5518
+ slug: "me",
5519
+ type: "person",
5520
+ status: "active",
5521
+ bucketName: "hq-vault-prs-me",
5522
+ createdAt: "2026-01-01T00:00:00Z",
5523
+ };
5524
+ function noopReconcileResult() {
5525
+ return { written: false, added: [], updated: [], skipped: [] };
5526
+ }
5527
+ /** A vault stub whose companies all resolve to `cmp_x` → slug `x`. */
5528
+ function companiesClient(companyUids) {
5529
+ return makeVaultStub({
5530
+ memberships: companyUids.map((companyUid) => ({ companyUid })),
5531
+ entityGet: (uid) => Promise.resolve({
5532
+ uid,
5533
+ slug: uid.replace(/^cmp_/, ""),
5534
+ name: `${uid} Inc`,
5535
+ type: "company",
5536
+ status: "active",
5537
+ bucketName: `bucket-${uid}`,
5538
+ createdAt: "2026-01-01T00:00:00Z",
5539
+ }),
5540
+ listPersons: () => Promise.resolve([PERSON_ENTITY]),
5541
+ });
5542
+ }
5543
+ function reconcileArgs(reconcile) {
5544
+ return reconcile.mock.calls[0][0];
5545
+ }
5546
+ /** Diagnostics ride stderr as ndjson; pick out the manifest ones. */
5547
+ function manifestDiagnostics(stderr) {
5548
+ return stderr
5549
+ .lines()
5550
+ .flatMap((line) => {
5551
+ try {
5552
+ return [JSON.parse(line)];
5553
+ }
5554
+ catch {
5555
+ return [];
5556
+ }
5557
+ })
5558
+ .filter((entry) => entry.component === "manifest-reconcile");
5559
+ }
5560
+ it("AC2: an errored company is not eligible — only the completing company is", async () => {
5561
+ const reconcile = vi.fn().mockResolvedValue(noopReconcileResult());
5562
+ const deps = makeDeps({
5563
+ createVaultClient: () => companiesClient(["cmp_ok", "cmp_bad"]),
5564
+ sync: vi.fn().mockImplementation(async (options) => {
5565
+ if (options.company === "cmp_bad")
5566
+ throw new Error("pull leg exploded");
5567
+ return defaultSyncResult();
5568
+ }),
5569
+ reconcileManifest: reconcile,
5570
+ });
5571
+ await runRunner(["--companies"], deps);
5572
+ expect(reconcile).toHaveBeenCalledTimes(1);
5573
+ const args = reconcileArgs(reconcile);
5574
+ // The completion filter is `completedCompanySlugs`; assert the EFFECTIVE
5575
+ // eligible set, which is what actually reaches the manifest.
5576
+ const eligible = args.targets.filter((target) => args.completedCompanySlugs.has(target.slug));
5577
+ expect(eligible.map((target) => target.slug)).toEqual(["ok"]);
5578
+ expect(args.completedCompanySlugs.has("bad")).toBe(false);
5579
+ });
5580
+ it("AC3: the personal target is never passed to the reconciler", async () => {
5581
+ const reconcile = vi.fn().mockResolvedValue(noopReconcileResult());
5582
+ const deps = makeDeps({
5583
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5584
+ reconcileManifest: reconcile,
5585
+ });
5586
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5587
+ const args = reconcileArgs(reconcile);
5588
+ // The personal leg definitely ran — otherwise this assertion is vacuous.
5589
+ const plan = deps.stdout
5590
+ .events()
5591
+ .find((e) => e.type === "fanout-plan");
5592
+ expect(plan.companies.map((t) => t.slug)).toContain("personal");
5593
+ expect(args.targets.map((target) => target.slug)).toEqual(["acme"]);
5594
+ expect(args.targets.some((target) => target.personalMode === true)).toBe(false);
5595
+ expect(args.targets.some((target) => target.slug === "personal")).toBe(false);
5596
+ });
5597
+ it("AC4: --personal mode does not reconcile", async () => {
5598
+ const reconcile = vi.fn().mockResolvedValue(noopReconcileResult());
5599
+ const deps = makeDeps({
5600
+ createVaultClient: () => makeVaultStub({ listPersons: () => Promise.resolve([PERSON_ENTITY]) }),
5601
+ reconcileManifest: reconcile,
5602
+ });
5603
+ expect(await runRunner(["--personal"], deps)).toBe(0);
5604
+ expect(reconcile).not.toHaveBeenCalled();
5605
+ });
5606
+ it("AC4: single-company mode (--company <slug>) does not reconcile", async () => {
5607
+ const reconcile = vi.fn().mockResolvedValue(noopReconcileResult());
5608
+ const deps = makeDeps({
5609
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5610
+ reconcileManifest: reconcile,
5611
+ });
5612
+ expect(await runRunner(["--company", "cmp_acme"], deps)).toBe(0);
5613
+ expect(reconcile).not.toHaveBeenCalled();
5614
+ });
5615
+ it("AC5: company targets carry the plan-resolved bucketName, so no getEntity is passed", async () => {
5616
+ const reconcile = vi.fn().mockResolvedValue(noopReconcileResult());
5617
+ const deps = makeDeps({
5618
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5619
+ reconcileManifest: reconcile,
5620
+ });
5621
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5622
+ const args = reconcileArgs(reconcile);
5623
+ expect(args.targets[0]).toMatchObject({
5624
+ uid: "cmp_acme",
5625
+ slug: "acme",
5626
+ name: "cmp_acme Inc",
5627
+ bucketName: "bucket-cmp_acme",
5628
+ // Liveness evidence travels with the target. Dropping the round trip
5629
+ // must not drop the guarantee it used to provide.
5630
+ entityType: "company",
5631
+ entityStatus: "active",
5632
+ });
5633
+ // Zero extra API calls: the runner hands over no entity resolver at all.
5634
+ expect(args.getEntity).toBeUndefined();
5635
+ });
5636
+ it("END-TO-END: a suspended company completes its sync leg but never reaches the manifest", async () => {
5637
+ // Exercises the REAL reconciler through the runner — no stub — because the
5638
+ // defect this guards was invisible to every stubbed assertion.
5639
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "hq-runner-manifest-"));
5640
+ try {
5641
+ fs.mkdirSync(path.join(root, "companies", "live"), { recursive: true });
5642
+ fs.mkdirSync(path.join(root, "companies", "susp"), { recursive: true });
5643
+ const manifestPath = path.join(root, "companies", "manifest.yaml");
5644
+ fs.writeFileSync(manifestPath, "companies: {}\n");
5645
+ const deps = makeDeps({
5646
+ createVaultClient: () => makeVaultStub({
5647
+ memberships: [{ companyUid: "cmp_live" }, { companyUid: "cmp_susp" }],
5648
+ entityGet: (uid) => Promise.resolve({
5649
+ uid,
5650
+ slug: uid === "cmp_live" ? "live" : "susp",
5651
+ type: "company",
5652
+ // The suspended company is still a perfectly syncable target.
5653
+ status: uid === "cmp_live" ? "active" : "suspended",
5654
+ bucketName: `bucket-${uid}`,
5655
+ createdAt: "2026-01-01T00:00:00Z",
5656
+ }),
5657
+ listPersons: () => Promise.resolve([PERSON_ENTITY]),
5658
+ }),
5659
+ // The REAL reconciler, not the makeDeps stub — that stub is exactly
5660
+ // what let this defect hide from every other assertion here.
5661
+ reconcileManifest: reconcileCompanyManifest,
5662
+ });
5663
+ expect(await runRunner(["--companies", "--hq-root", root], deps)).toBe(0);
5664
+ // Both companies synced (the guard must not have leaked into sync).
5665
+ const syncedCompanies = deps.sync.mock.calls
5666
+ .map((call) => call[0].company)
5667
+ .filter(Boolean);
5668
+ expect(syncedCompanies).toContain("cmp_live");
5669
+ expect(syncedCompanies).toContain("cmp_susp");
5670
+ // ...but only the active one earned a manifest entry.
5671
+ const written = yaml.load(fs.readFileSync(manifestPath, "utf8"));
5672
+ expect(Object.keys(written.companies ?? {})).toEqual(["live"]);
5673
+ }
5674
+ finally {
5675
+ fs.rmSync(root, { recursive: true, force: true });
5676
+ }
5677
+ });
5678
+ it("US-003: seeds activeCompany from the completed company set", async () => {
5679
+ const seed = vi.fn().mockReturnValue({ written: true, activeCompany: "acme" });
5680
+ const deps = makeDeps({
5681
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5682
+ seedActiveCompany: seed,
5683
+ });
5684
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5685
+ expect(seed).toHaveBeenCalledTimes(1);
5686
+ expect(seed.mock.calls[0][0]).toMatchObject({ companySlugs: ["acme"] });
5687
+ expect(deps.stderr.raw()).toContain("active-company-seeded");
5688
+ });
5689
+ it("US-003: the personal target is never offered as an activeCompany candidate", async () => {
5690
+ const seed = vi.fn().mockReturnValue({ written: false, skippedReason: "already-set" });
5691
+ const deps = makeDeps({
5692
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5693
+ seedActiveCompany: seed,
5694
+ });
5695
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5696
+ expect(seed.mock.calls[0][0].companySlugs).not.toContain("personal");
5697
+ });
5698
+ // The two writes are independent repairs. A joiner whose manifest write fails
5699
+ // should still get routed, and vice versa.
5700
+ it("US-003: a failing manifest reconcile does not prevent activeCompany seeding", async () => {
5701
+ const seed = vi.fn().mockReturnValue({ written: true, activeCompany: "acme" });
5702
+ const deps = makeDeps({
5703
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5704
+ reconcileManifest: vi.fn().mockRejectedValue(new Error("manifest write blew up")),
5705
+ seedActiveCompany: seed,
5706
+ });
5707
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5708
+ expect(deps.stderr.raw()).toContain("runner.manifest_reconcile.failed");
5709
+ expect(seed).toHaveBeenCalledTimes(1);
5710
+ });
5711
+ it("US-003: a throwing seeder still emits all-complete and exits 0", async () => {
5712
+ const deps = makeDeps({
5713
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5714
+ seedActiveCompany: vi.fn().mockImplementation(() => {
5715
+ throw new Error("config write blew up");
5716
+ }),
5717
+ });
5718
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5719
+ expect(deps.stdout.events().some((e) => e.type === "all-complete")).toBe(true);
5720
+ expect(deps.stderr.raw()).toContain("runner.active_company.failed");
5721
+ });
5722
+ it("US-003: a no-op seeding is silent", async () => {
5723
+ const deps = makeDeps({
5724
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5725
+ seedActiveCompany: vi
5726
+ .fn()
5727
+ .mockReturnValue({ written: false, skippedReason: "already-set" }),
5728
+ });
5729
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5730
+ expect(deps.stderr.raw()).not.toContain("active-company-seeded");
5731
+ });
5732
+ it("US-003: single-company mode does not seed activeCompany", async () => {
5733
+ const seed = vi.fn();
5734
+ const deps = makeDeps({
5735
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5736
+ seedActiveCompany: seed,
5737
+ });
5738
+ await runRunner(["--company", "acme"], deps);
5739
+ expect(seed).not.toHaveBeenCalled();
5740
+ });
5741
+ it("AC6: a throwing reconciler still emits all-complete and exits 0", async () => {
5742
+ const deps = makeDeps({
5743
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5744
+ reconcileManifest: vi
5745
+ .fn()
5746
+ .mockRejectedValue(new Error("manifest write blew up")),
5747
+ });
5748
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5749
+ expect(deps.stdout.events().some((e) => e.type === "all-complete")).toBe(true);
5750
+ expect(deps.stderr.raw()).toContain("runner.manifest_reconcile.failed");
5751
+ });
5752
+ it("AC7: a write emits a manifest-reconciled diagnostic naming the slugs", async () => {
5753
+ const deps = makeDeps({
5754
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5755
+ reconcileManifest: vi.fn().mockResolvedValue({
5756
+ written: true,
5757
+ added: ["acme"],
5758
+ updated: ["beta"],
5759
+ skipped: [],
5760
+ }),
5761
+ });
5762
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5763
+ const diagnostics = manifestDiagnostics(deps.stderr);
5764
+ expect(diagnostics).toHaveLength(1);
5765
+ expect(diagnostics[0]).toMatchObject({
5766
+ component: "manifest-reconcile",
5767
+ event: "manifest-reconciled",
5768
+ added: ["acme"],
5769
+ updated: ["beta"],
5770
+ });
5771
+ });
5772
+ it("AC7: a no-op reconciliation is completely silent (steady-state sync)", async () => {
5773
+ const deps = makeDeps({
5774
+ createVaultClient: () => companiesClient(["cmp_acme"]),
5775
+ reconcileManifest: vi.fn().mockResolvedValue({
5776
+ written: false,
5777
+ added: [],
5778
+ updated: [],
5779
+ skipped: ["weird-slug"],
5780
+ }),
5781
+ });
5782
+ expect(await runRunner(["--companies"], deps)).toBe(0);
5783
+ expect(manifestDiagnostics(deps.stderr)).toEqual([]);
5784
+ expect(deps.stderr.raw()).not.toContain("manifest-reconciled");
5785
+ });
5786
+ });
5787
+ describe("watcher delete intents without pre-seeded authorization", () => {
5788
+ function startDeleteHarness(keys, options = {}) {
5789
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-watch-delete-"));
5790
+ const companyRoot = path.join(hqRoot, "companies", "indigo");
5791
+ fs.mkdirSync(companyRoot, { recursive: true });
5792
+ const remote = new Map();
5793
+ const files = {};
5794
+ for (const key of keys) {
5795
+ const absolutePath = path.join(companyRoot, key);
5796
+ const content = `synced:${key}`;
5797
+ fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
5798
+ fs.writeFileSync(absolutePath, content);
5799
+ remote.set(key, content);
5800
+ files[key] = {
5801
+ hash: `hash:${key}`,
5802
+ size: Buffer.byteLength(content),
5803
+ syncedAt: new Date().toISOString(),
5804
+ direction: "down",
5805
+ remoteEtag: `etag:${key}`,
5806
+ kind: "file",
5807
+ };
5808
+ }
5809
+ writeJournal("indigo", {
5810
+ version: "2", lastSync: new Date().toISOString(), files, pulls: [],
5811
+ });
5812
+ const watcher = makeBatchWatcherStub();
5813
+ const sleep = makeSteppableSleep();
5814
+ let capture;
5815
+ let triggerShutdown = () => { };
5816
+ let downloads = 0;
5817
+ let releaseInitialPull = () => { };
5818
+ const initialPullGate = new Promise((resolve) => {
5819
+ releaseInitialPull = resolve;
5820
+ });
5821
+ let initialPullGated = options.gateInitialPull === true;
5822
+ const runPass = vi.fn(async (argv) => {
5823
+ const direction = argv[argv.indexOf("--direction") + 1];
5824
+ if (direction === "push") {
5825
+ const journal = readJournal("indigo");
5826
+ const pathResults = [];
5827
+ const scopes = argv.flatMap((value, index) => argv[index - 1] === "--scope-path" ? [value] : []);
5828
+ for (const scope of scopes) {
5829
+ for (const key of Object.keys(journal.files)) {
5830
+ if (key !== scope && !key.startsWith(`${scope}/`))
5831
+ continue;
5832
+ const absolutePath = path.join(companyRoot, key);
5833
+ const entry = journal.files[key];
5834
+ if (fs.existsSync(absolutePath)) {
5835
+ remote.set(key, fs.readFileSync(absolutePath, "utf8"));
5836
+ continue;
5837
+ }
5838
+ const intent = entry.localDeleteIntent;
5839
+ const current = intent?.remoteEtag === entry.remoteEtag &&
5840
+ intent?.localHash === entry.hash &&
5841
+ intent?.localKind === entry.kind;
5842
+ if (current) {
5843
+ remote.delete(key);
5844
+ delete journal.files[key];
5845
+ pathResults.push({
5846
+ relativePath: `companies/indigo/${key}`,
5847
+ status: "accepted",
5848
+ operation: "delete",
5849
+ });
5850
+ }
5851
+ else {
5852
+ pathResults.push({
5853
+ relativePath: `companies/indigo/${key}`,
5854
+ status: "refused",
5855
+ operation: "delete",
5856
+ reason: "missing-delete-intent",
5857
+ });
5858
+ }
5859
+ }
5860
+ const added = path.join(companyRoot, scope);
5861
+ if (fs.existsSync(added) && fs.lstatSync(added).isFile()) {
5862
+ remote.set(scope, fs.readFileSync(added, "utf8"));
5863
+ }
5864
+ }
5865
+ writeJournal("indigo", journal);
5866
+ return { exitCode: 0, result: { pushPathResults: pathResults } };
5867
+ }
5868
+ if (direction === "pull" || direction === "both") {
5869
+ if (initialPullGated) {
5870
+ initialPullGated = false;
5871
+ await initialPullGate;
5872
+ }
5873
+ const journal = readJournal("indigo");
5874
+ for (const [key, content] of remote) {
5875
+ const absolutePath = path.join(companyRoot, key);
5876
+ if (fs.existsSync(absolutePath))
5877
+ continue;
5878
+ const entry = journal.files[key];
5879
+ const intent = entry?.localDeleteIntent;
5880
+ const currentIntent = intent?.remoteEtag === entry?.remoteEtag &&
5881
+ intent?.localHash === entry?.hash &&
5882
+ intent?.localKind === entry?.kind;
5883
+ if (currentIntent)
5884
+ continue;
5885
+ fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
5886
+ fs.writeFileSync(absolutePath, content);
5887
+ downloads++;
5888
+ }
5889
+ }
5890
+ return 0;
5891
+ });
5892
+ const loop = runRunnerWithLoop([
5893
+ "--companies", "--watch", "--event-push", "--direction", "both",
5894
+ "--hq-root", hqRoot, "--poll-remote-ms", "60000",
5895
+ ], {
5896
+ runPass,
5897
+ createWatcher: (opts) => {
5898
+ capture = opts.captureLocalDeleteSnapshots;
5899
+ return watcher;
5900
+ },
5901
+ sleep: sleep.sleep,
5902
+ onShutdownSignal: (handler) => {
5903
+ triggerShutdown = handler;
5904
+ return () => { };
5905
+ },
5906
+ });
5907
+ return {
5908
+ hqRoot, companyRoot, remote, watcher, loop, tick: sleep.tick,
5909
+ capture: () => capture, downloads: () => downloads,
5910
+ releaseInitialPull,
5911
+ shutdown: () => triggerShutdown(),
5912
+ };
5913
+ }
5914
+ async function settle() {
5915
+ await flushLoopMicrotasks(12);
5916
+ }
5917
+ async function stopHarness(h) {
5918
+ h.shutdown();
5919
+ h.tick();
5920
+ await h.loop;
5921
+ fs.rmSync(h.hqRoot, { recursive: true, force: true });
5922
+ }
5923
+ it("live unlink binds the pre-delete revision, deletes remote, and survives immediate plus later full pulls", async () => {
5924
+ const h = startDeleteHarness(["knowledge/gone.md"]);
5925
+ await settle();
5926
+ const absolutePath = path.join(h.companyRoot, "knowledge/gone.md");
5927
+ fs.unlinkSync(absolutePath);
5928
+ const snapshots = h.capture()("companies/indigo/knowledge/gone.md", "unlink");
5929
+ expect(snapshots).toHaveLength(1);
5930
+ expect(readJournal("indigo").files["knowledge/gone.md"]?.localDeleteIntent)
5931
+ .toMatchObject({
5932
+ remoteEtag: "etag:knowledge/gone.md",
5933
+ localHash: "hash:knowledge/gone.md",
5934
+ localKind: "file",
5935
+ });
5936
+ h.watcher.emit("companies/indigo/knowledge/gone.md", {
5937
+ paths: new Map([[absolutePath, "companies/indigo/knowledge/gone.md"]]),
5938
+ changes: new Map([
5939
+ [absolutePath, { kind: "unlink", deleteSnapshots: snapshots }],
5940
+ ]),
5941
+ });
5942
+ h.tick();
5943
+ await settle();
5944
+ expect(h.remote.has("knowledge/gone.md")).toBe(false);
5945
+ expect(fs.existsSync(absolutePath)).toBe(false);
5946
+ expect(h.downloads()).toBe(0);
5947
+ for (let i = 0; i < 8; i++) {
5948
+ h.tick();
5949
+ await settle();
5950
+ }
5951
+ expect(fs.existsSync(absolutePath)).toBe(false);
5952
+ expect(h.downloads()).toBe(0);
5953
+ await stopHarness(h);
5954
+ });
5955
+ it("persists the intent before an in-flight pull plans the missing path", async () => {
5956
+ const h = startDeleteHarness(["knowledge/in-flight.md"], { gateInitialPull: true });
5957
+ await settle();
5958
+ const relativePath = "companies/indigo/knowledge/in-flight.md";
5959
+ const absolutePath = path.join(h.companyRoot, "knowledge/in-flight.md");
5960
+ fs.unlinkSync(absolutePath);
5961
+ const snapshots = h.capture()(relativePath, "unlink");
5962
+ expect(snapshots).toHaveLength(1);
5963
+ expect(readJournal("indigo").files["knowledge/in-flight.md"]
5964
+ ?.localDeleteIntent).toBeDefined();
5965
+ h.watcher.emit(relativePath, {
5966
+ paths: new Map([[absolutePath, relativePath]]),
5967
+ changes: new Map([
5968
+ [absolutePath, { kind: "unlink", deleteSnapshots: snapshots }],
5969
+ ]),
5970
+ });
5971
+ h.releaseInitialPull();
5972
+ await settle();
5973
+ expect(h.downloads()).toBe(0);
5974
+ expect(fs.existsSync(absolutePath)).toBe(false);
5975
+ h.tick();
5976
+ await settle();
5977
+ expect(h.remote.has("knowledge/in-flight.md")).toBe(false);
5978
+ expect(h.downloads()).toBe(0);
5979
+ await stopHarness(h);
5980
+ });
5981
+ it("uses direct journal-key lookup for file unlink snapshots", () => {
5982
+ const exactEntry = {
5983
+ hash: "hash",
5984
+ size: 4,
5985
+ syncedAt: new Date().toISOString(),
5986
+ direction: "down",
5987
+ remoteEtag: "etag",
5988
+ kind: "file",
5989
+ };
5990
+ const files = new Proxy({ "knowledge/exact.md": exactEntry }, {
5991
+ ownKeys: () => {
5992
+ throw new Error("file unlink must not enumerate the journal");
5993
+ },
5994
+ });
5995
+ expect(journalEntriesForWatcherDelete(files, "knowledge/exact.md", "unlink")).toEqual([["knowledge/exact.md", exactEntry]]);
5996
+ });
5997
+ it("keeps a deleted top-level personal file in the scoped push delete roots", async () => {
5998
+ const hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-personal-delete-"));
5999
+ const key = "personal-note.md";
6000
+ const absolutePath = path.join(hqRoot, key);
6001
+ fs.writeFileSync(absolutePath, "personal content");
6002
+ const remote = new Map([[key, "personal content"]]);
6003
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
6004
+ version: "2",
6005
+ lastSync: new Date().toISOString(),
6006
+ pulls: [],
6007
+ files: {
6008
+ [key]: {
6009
+ hash: "personal-hash",
6010
+ size: 16,
6011
+ syncedAt: new Date().toISOString(),
6012
+ direction: "down",
6013
+ remoteEtag: "personal-etag",
6014
+ kind: "file",
6015
+ },
6016
+ },
6017
+ });
6018
+ const shareSpy = vi.fn(async (options) => {
6019
+ const journal = readJournal(PERSONAL_VAULT_JOURNAL_SLUG);
6020
+ const entry = journal.files[key];
6021
+ const intent = entry?.localDeleteIntent;
6022
+ const currentIntent = intent?.remoteEtag === entry?.remoteEtag &&
6023
+ intent?.localHash === entry?.hash &&
6024
+ intent?.localKind === entry?.kind;
6025
+ if (!fs.existsSync(absolutePath) &&
6026
+ options.deleteScopeRoots?.includes(key) &&
6027
+ currentIntent) {
6028
+ remote.delete(key);
6029
+ delete journal.files[key];
6030
+ writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, journal);
6031
+ return defaultShareResult({
6032
+ filesDeleted: 1,
6033
+ pathResults: [
6034
+ { path: key, status: "accepted", operation: "delete" },
6035
+ ],
6036
+ });
6037
+ }
6038
+ return defaultShareResult();
6039
+ });
6040
+ let downloads = 0;
6041
+ const syncSpy = vi.fn(async () => {
6042
+ if (remote.has(key) && !fs.existsSync(absolutePath)) {
6043
+ const entry = readJournal(PERSONAL_VAULT_JOURNAL_SLUG).files[key];
6044
+ const intent = entry?.localDeleteIntent;
6045
+ const currentIntent = intent?.remoteEtag === entry?.remoteEtag &&
6046
+ intent?.localHash === entry?.hash &&
6047
+ intent?.localKind === entry?.kind;
6048
+ if (!currentIntent) {
6049
+ fs.writeFileSync(absolutePath, remote.get(key));
6050
+ downloads++;
6051
+ }
6052
+ }
6053
+ return defaultSyncResult();
6054
+ });
6055
+ const runnerDeps = makeDeps({
6056
+ share: shareSpy,
6057
+ sync: syncSpy,
6058
+ createVaultClient: () => makeVaultStub({
6059
+ listPersons: () => Promise.resolve([
6060
+ {
6061
+ uid: "prs_personal",
6062
+ type: "person",
6063
+ bucketName: "hq-vault-personal",
6064
+ status: "active",
6065
+ },
6066
+ ]),
6067
+ }),
6068
+ operationLockAlreadyHeld: true,
6069
+ });
6070
+ const watcher = makeBatchWatcherStub();
6071
+ const sleep = makeSteppableSleep();
6072
+ let capture;
6073
+ let triggerShutdown = () => { };
6074
+ const loop = runRunnerWithLoop([
6075
+ "--personal",
6076
+ "--watch",
6077
+ "--event-push",
6078
+ "--direction",
6079
+ "both",
6080
+ "--hq-root",
6081
+ hqRoot,
6082
+ "--poll-remote-ms",
6083
+ "60000",
6084
+ ], {
6085
+ runPass: (passArgv) => runRunner(passArgv, runnerDeps),
6086
+ createWatcher: (options) => {
6087
+ capture = options.captureLocalDeleteSnapshots;
6088
+ return watcher;
6089
+ },
6090
+ sleep: sleep.sleep,
6091
+ onShutdownSignal: (handler) => {
6092
+ triggerShutdown = handler;
6093
+ return () => { };
6094
+ },
6095
+ });
6096
+ await vi.waitFor(() => expect(syncSpy).toHaveBeenCalled());
6097
+ await settle();
6098
+ shareSpy.mockClear();
6099
+ fs.unlinkSync(absolutePath);
6100
+ const snapshots = capture(key, "unlink");
6101
+ expect(snapshots).toHaveLength(1);
6102
+ expect(snapshots[0].deleteScopeRoot).toBe(key);
6103
+ expect(readJournal(PERSONAL_VAULT_JOURNAL_SLUG).files[key]
6104
+ ?.localDeleteIntent).toBeDefined();
6105
+ watcher.emit(key, {
6106
+ paths: new Map([[absolutePath, key]]),
6107
+ changes: new Map([
6108
+ [absolutePath, { kind: "unlink", deleteSnapshots: snapshots }],
6109
+ ]),
6110
+ });
6111
+ sleep.tick();
6112
+ await settle();
6113
+ expect(remote.has(key)).toBe(false);
6114
+ expect(downloads).toBe(0);
6115
+ expect(fs.existsSync(absolutePath)).toBe(false);
6116
+ const scopedShareOptions = shareSpy.mock.calls.at(-1)?.[0];
6117
+ expect(scopedShareOptions?.paths).not.toContain(absolutePath);
6118
+ expect(scopedShareOptions?.deleteScopeRoots).toEqual([key]);
6119
+ triggerShutdown();
6120
+ await loop;
6121
+ fs.rmSync(hqRoot, { recursive: true, force: true });
6122
+ });
6123
+ it("rename uploads the new path, deletes the old path, and neither resurrects", async () => {
6124
+ const h = startDeleteHarness(["knowledge/old.md"]);
6125
+ await settle();
6126
+ const oldPath = path.join(h.companyRoot, "knowledge/old.md");
6127
+ const newPath = path.join(h.companyRoot, "knowledge/new.md");
6128
+ const snapshots = h.capture()("companies/indigo/knowledge/old.md", "unlink");
6129
+ fs.renameSync(oldPath, newPath);
6130
+ h.watcher.emit("companies/indigo/knowledge/old.md", {
6131
+ paths: new Map([
6132
+ [oldPath, "companies/indigo/knowledge/old.md"],
6133
+ [newPath, "companies/indigo/knowledge/new.md"],
6134
+ ]),
6135
+ changes: new Map([
6136
+ [oldPath, { kind: "unlink", deleteSnapshots: snapshots }],
6137
+ [newPath, { kind: "add" }],
6138
+ ]),
6139
+ });
6140
+ h.tick();
6141
+ await settle();
6142
+ expect(h.remote.has("knowledge/old.md")).toBe(false);
6143
+ expect(h.remote.get("knowledge/new.md")).toBe("synced:knowledge/old.md");
6144
+ expect(fs.existsSync(oldPath)).toBe(false);
6145
+ expect(fs.existsSync(newPath)).toBe(true);
6146
+ await stopHarness(h);
6147
+ });
6148
+ it("unlinkDir records every tracked descendant across a 12-file breaker-sized subtree", async () => {
6149
+ const keys = Array.from({ length: 12 }, (_, i) => `knowledge/moved/f-${i}.md`);
6150
+ const h = startDeleteHarness(keys);
6151
+ await settle();
6152
+ const subtree = path.join(h.companyRoot, "knowledge/moved");
6153
+ const snapshots = h.capture()("companies/indigo/knowledge/moved", "unlinkDir");
6154
+ expect(snapshots).toHaveLength(12);
6155
+ fs.rmSync(subtree, { recursive: true, force: true });
6156
+ h.watcher.emit("companies/indigo/knowledge/moved", {
6157
+ paths: new Map([[subtree, "companies/indigo/knowledge/moved"]]),
6158
+ changes: new Map([
6159
+ [subtree, { kind: "unlinkDir", deleteSnapshots: snapshots }],
6160
+ ]),
6161
+ });
6162
+ h.tick();
6163
+ await settle();
6164
+ expect(h.remote.size).toBe(0);
6165
+ expect(Object.keys(readJournal("indigo").files)).toHaveLength(0);
6166
+ await stopHarness(h);
6167
+ });
6168
+ it("concurrent recreation before delete execution invalidates the observation and preserves remote", async () => {
6169
+ const h = startDeleteHarness(["knowledge/race.md"]);
6170
+ await settle();
6171
+ const absolutePath = path.join(h.companyRoot, "knowledge/race.md");
6172
+ fs.unlinkSync(absolutePath);
6173
+ const snapshots = h.capture()("companies/indigo/knowledge/race.md", "unlink");
6174
+ h.watcher.emit("companies/indigo/knowledge/race.md", {
6175
+ paths: new Map([[absolutePath, "companies/indigo/knowledge/race.md"]]),
6176
+ changes: new Map([
6177
+ [absolutePath, { kind: "unlink", deleteSnapshots: snapshots }],
6178
+ ]),
6179
+ });
6180
+ fs.writeFileSync(absolutePath, "concurrent recreation");
6181
+ h.tick();
6182
+ await settle();
6183
+ expect(h.remote.has("knowledge/race.md")).toBe(true);
6184
+ expect(readJournal("indigo").files["knowledge/race.md"]?.localDeleteIntent)
6185
+ .toBeUndefined();
6186
+ await stopHarness(h);
6187
+ });
6188
+ });
5345
6189
  //# sourceMappingURL=sync-runner.test.js.map