@indigoai-us/hq-cloud 5.22.0 → 5.24.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 (78) hide show
  1. package/dist/bin/sync-runner.d.ts +20 -0
  2. package/dist/bin/sync-runner.d.ts.map +1 -1
  3. package/dist/bin/sync-runner.js +18 -0
  4. package/dist/bin/sync-runner.js.map +1 -1
  5. package/dist/bin/sync-runner.test.js +46 -2
  6. package/dist/bin/sync-runner.test.js.map +1 -1
  7. package/dist/cli/share.d.ts +77 -20
  8. package/dist/cli/share.d.ts.map +1 -1
  9. package/dist/cli/share.js +278 -61
  10. package/dist/cli/share.js.map +1 -1
  11. package/dist/cli/share.test.js +484 -3
  12. package/dist/cli/share.test.js.map +1 -1
  13. package/dist/cli/sync.d.ts +27 -0
  14. package/dist/cli/sync.d.ts.map +1 -1
  15. package/dist/cli/sync.js.map +1 -1
  16. package/dist/index.d.ts +9 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +9 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/journal.d.ts +76 -1
  21. package/dist/journal.d.ts.map +1 -1
  22. package/dist/journal.js +148 -1
  23. package/dist/journal.js.map +1 -1
  24. package/dist/journal.test.js +251 -5
  25. package/dist/journal.test.js.map +1 -1
  26. package/dist/prefix-coalesce.d.ts +38 -0
  27. package/dist/prefix-coalesce.d.ts.map +1 -0
  28. package/dist/prefix-coalesce.js +69 -0
  29. package/dist/prefix-coalesce.js.map +1 -0
  30. package/dist/prefix-coalesce.test.d.ts +2 -0
  31. package/dist/prefix-coalesce.test.d.ts.map +1 -0
  32. package/dist/prefix-coalesce.test.js +77 -0
  33. package/dist/prefix-coalesce.test.js.map +1 -0
  34. package/dist/public-surface.test.d.ts +15 -0
  35. package/dist/public-surface.test.d.ts.map +1 -0
  36. package/dist/public-surface.test.js +105 -0
  37. package/dist/public-surface.test.js.map +1 -0
  38. package/dist/remote-pull.d.ts +145 -1
  39. package/dist/remote-pull.d.ts.map +1 -1
  40. package/dist/remote-pull.js +258 -1
  41. package/dist/remote-pull.js.map +1 -1
  42. package/dist/remote-pull.test.js +470 -2
  43. package/dist/remote-pull.test.js.map +1 -1
  44. package/dist/scope-shrink.d.ts +109 -0
  45. package/dist/scope-shrink.d.ts.map +1 -0
  46. package/dist/scope-shrink.js +196 -0
  47. package/dist/scope-shrink.js.map +1 -0
  48. package/dist/scope-shrink.test.d.ts +13 -0
  49. package/dist/scope-shrink.test.d.ts.map +1 -0
  50. package/dist/scope-shrink.test.js +342 -0
  51. package/dist/scope-shrink.test.js.map +1 -0
  52. package/dist/types.d.ts +48 -1
  53. package/dist/types.d.ts.map +1 -1
  54. package/dist/vault-client.d.ts +178 -0
  55. package/dist/vault-client.d.ts.map +1 -1
  56. package/dist/vault-client.js +73 -0
  57. package/dist/vault-client.js.map +1 -1
  58. package/dist/vault-client.test.js +226 -0
  59. package/dist/vault-client.test.js.map +1 -1
  60. package/package.json +1 -1
  61. package/src/bin/sync-runner.test.ts +56 -2
  62. package/src/bin/sync-runner.ts +39 -0
  63. package/src/cli/share.test.ts +577 -3
  64. package/src/cli/share.ts +395 -85
  65. package/src/cli/sync.ts +28 -0
  66. package/src/index.ts +67 -0
  67. package/src/journal.test.ts +284 -5
  68. package/src/journal.ts +167 -2
  69. package/src/prefix-coalesce.test.ts +95 -0
  70. package/src/prefix-coalesce.ts +72 -0
  71. package/src/public-surface.test.ts +112 -0
  72. package/src/remote-pull.test.ts +540 -3
  73. package/src/remote-pull.ts +419 -2
  74. package/src/scope-shrink.test.ts +402 -0
  75. package/src/scope-shrink.ts +264 -0
  76. package/src/types.ts +49 -1
  77. package/src/vault-client.test.ts +335 -0
  78. package/src/vault-client.ts +223 -0
@@ -13,10 +13,27 @@
13
13
  * `decideRemotePulls` in `./remote-pull.ts`. Per the project test-first
14
14
  * rule, the implementation lands AFTER these tests are validated.
15
15
  */
16
- import { describe, expect, it } from "vitest";
17
- import { decideRemotePulls } from "./remote-pull.js";
16
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
17
+ import * as fs from "fs";
18
+ import * as os from "os";
19
+ import * as path from "path";
20
+ import * as crypto from "crypto";
21
+ import {
22
+ batchPrefixesForVend,
23
+ decideRemotePulls,
24
+ listRemoteForScope,
25
+ POST_FILTER_THRESHOLD,
26
+ pullCompany,
27
+ resolveCompanyScope,
28
+ VEND_PATH_CAP,
29
+ } from "./remote-pull.js";
18
30
  import type { RemoteFile } from "./s3.js";
19
- import type { SyncJournal } from "./types.js";
31
+ import type { EntityContext, SyncJournal } from "./types.js";
32
+ import type {
33
+ ExplicitGrant,
34
+ MembershipSyncConfig,
35
+ } from "./vault-client.js";
36
+ import { ScopeShrinkBlockedError } from "./scope-shrink.js";
20
37
 
21
38
  function remote(partial: Partial<RemoteFile> & { key: string }): RemoteFile {
22
39
  return {
@@ -239,3 +256,523 @@ describe("decideRemotePulls", () => {
239
256
  expect(result.download.map((f) => f.key)).toEqual(["docs/legacy.md"]);
240
257
  });
241
258
  });
259
+
260
+ // ─── US-005 — ACL-aware narrowing in the engine layer ────────────────────────
261
+
262
+ function sha256(s: string): string {
263
+ return crypto.createHash("sha256").update(s).digest("hex");
264
+ }
265
+
266
+ function makeCtx(): EntityContext {
267
+ return {
268
+ uid: "cmp_indigo",
269
+ slug: "indigo",
270
+ bucketName: "cmp-indigo-vault",
271
+ region: "us-east-1",
272
+ credentials: {
273
+ accessKeyId: "k",
274
+ secretAccessKey: "s",
275
+ sessionToken: "t",
276
+ },
277
+ expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
278
+ };
279
+ }
280
+
281
+ function makeSyncConfig(
282
+ partial: Partial<MembershipSyncConfig> & { syncMode: MembershipSyncConfig["syncMode"] },
283
+ ): MembershipSyncConfig {
284
+ return {
285
+ membershipId: "mb_test",
286
+ isDefault: false,
287
+ ...partial,
288
+ };
289
+ }
290
+
291
+ function makeGrant(p: string): ExplicitGrant {
292
+ return {
293
+ companyUid: "cmp_indigo",
294
+ path: p,
295
+ permission: "read",
296
+ source: "person",
297
+ };
298
+ }
299
+
300
+ describe("resolveCompanyScope", () => {
301
+ it("syncMode='all' returns strategy=all with the company prefix", () => {
302
+ const scope = resolveCompanyScope({
303
+ companyUid: "cmp_indigo",
304
+ companyPrefix: "companies/indigo/",
305
+ syncConfig: makeSyncConfig({ syncMode: "all" }),
306
+ });
307
+ expect(scope.strategy).toBe("all");
308
+ expect(scope.prefixSet).toEqual(["companies/indigo/"]);
309
+ expect(scope.syncMode).toBe("all");
310
+ });
311
+
312
+ it("syncMode='shared' coalesces explicit grants and picks vend-fanout when ≤ POST_FILTER_THRESHOLD", () => {
313
+ const scope = resolveCompanyScope({
314
+ companyUid: "cmp_indigo",
315
+ companyPrefix: "companies/indigo/",
316
+ syncConfig: makeSyncConfig({ syncMode: "shared" }),
317
+ explicitGrants: [
318
+ makeGrant("companies/indigo/meetings/"),
319
+ makeGrant("companies/indigo/meetings/2026/"), // nested — collapsed
320
+ makeGrant("companies/indigo/scratch/jacob/"),
321
+ ],
322
+ });
323
+ expect(scope.strategy).toBe("vend-fanout");
324
+ expect(scope.prefixSet).toEqual([
325
+ "companies/indigo/meetings/",
326
+ "companies/indigo/scratch/jacob/",
327
+ ]);
328
+ });
329
+
330
+ it("syncMode='shared' with no grants returns empty prefixSet (short-circuit)", () => {
331
+ const scope = resolveCompanyScope({
332
+ companyUid: "cmp_indigo",
333
+ companyPrefix: "companies/indigo/",
334
+ syncConfig: makeSyncConfig({ syncMode: "shared" }),
335
+ explicitGrants: [],
336
+ });
337
+ expect(scope.prefixSet).toEqual([]);
338
+ });
339
+
340
+ it("syncMode='shared' with > POST_FILTER_THRESHOLD coalesced prefixes picks broad-postfilter", () => {
341
+ const grants: ExplicitGrant[] = [];
342
+ for (let i = 0; i < POST_FILTER_THRESHOLD + 5; i++) {
343
+ grants.push(makeGrant(`companies/indigo/p${i}/`));
344
+ }
345
+ const scope = resolveCompanyScope({
346
+ companyUid: "cmp_indigo",
347
+ companyPrefix: "companies/indigo/",
348
+ syncConfig: makeSyncConfig({ syncMode: "shared" }),
349
+ explicitGrants: grants,
350
+ });
351
+ expect(scope.strategy).toBe("broad-postfilter");
352
+ expect(scope.prefixSet.length).toBe(POST_FILTER_THRESHOLD + 5);
353
+ });
354
+
355
+ it("syncMode='custom' coalesces customPaths", () => {
356
+ const scope = resolveCompanyScope({
357
+ companyUid: "cmp_indigo",
358
+ companyPrefix: "companies/indigo/",
359
+ syncConfig: makeSyncConfig({
360
+ syncMode: "custom",
361
+ customPaths: [
362
+ "companies/indigo/a/",
363
+ "companies/indigo/a/b/", // nested
364
+ "companies/indigo/c/",
365
+ ],
366
+ }),
367
+ });
368
+ expect(scope.prefixSet).toEqual([
369
+ "companies/indigo/a/",
370
+ "companies/indigo/c/",
371
+ ]);
372
+ });
373
+ });
374
+
375
+ describe("batchPrefixesForVend", () => {
376
+ it("batches into chunks of VEND_PATH_CAP", () => {
377
+ const prefixes = Array.from({ length: 23 }, (_, i) => `p${i}/`);
378
+ const batches = batchPrefixesForVend(prefixes);
379
+ expect(batches).toHaveLength(3);
380
+ expect(batches[0]).toHaveLength(VEND_PATH_CAP);
381
+ expect(batches[1]).toHaveLength(VEND_PATH_CAP);
382
+ expect(batches[2]).toHaveLength(3);
383
+ });
384
+
385
+ it("respects an explicit cap override", () => {
386
+ const prefixes = ["a/", "b/", "c/", "d/", "e/"];
387
+ const batches = batchPrefixesForVend(prefixes, 2);
388
+ expect(batches.map((b) => b.length)).toEqual([2, 2, 1]);
389
+ });
390
+
391
+ it("returns [] for empty input", () => {
392
+ expect(batchPrefixesForVend([])).toEqual([]);
393
+ });
394
+ });
395
+
396
+ describe("listRemoteForScope", () => {
397
+ it("strategy=all calls list once with the company prefix", async () => {
398
+ const calls: Array<string | undefined> = [];
399
+ const files = await listRemoteForScope({
400
+ ctx: makeCtx(),
401
+ scope: {
402
+ companyUid: "cmp_indigo",
403
+ syncMode: "all",
404
+ prefixSet: ["companies/indigo/"],
405
+ strategy: "all",
406
+ },
407
+ listFn: async (_ctx, prefix) => {
408
+ calls.push(prefix);
409
+ return [remote({ key: "companies/indigo/a.md", etag: "1" })];
410
+ },
411
+ });
412
+ expect(calls).toEqual(["companies/indigo/"]);
413
+ expect(files.map((f) => f.key)).toEqual(["companies/indigo/a.md"]);
414
+ });
415
+
416
+ it("strategy=vend-fanout issues one list per prefix and unions+dedupes", async () => {
417
+ const calls: Array<string | undefined> = [];
418
+ const files = await listRemoteForScope({
419
+ ctx: makeCtx(),
420
+ scope: {
421
+ companyUid: "cmp_indigo",
422
+ syncMode: "shared",
423
+ prefixSet: [
424
+ "companies/indigo/meetings/",
425
+ "companies/indigo/scratch/jacob/",
426
+ ],
427
+ strategy: "vend-fanout",
428
+ },
429
+ listFn: async (_ctx, prefix) => {
430
+ calls.push(prefix);
431
+ if (prefix === "companies/indigo/meetings/") {
432
+ return [
433
+ remote({ key: "companies/indigo/meetings/a.md", etag: "1" }),
434
+ remote({ key: "companies/indigo/meetings/shared.md", etag: "1" }),
435
+ ];
436
+ }
437
+ return [
438
+ remote({ key: "companies/indigo/scratch/jacob/draft.md", etag: "2" }),
439
+ // dedup target — same key reported by both prefixes if they overlap
440
+ remote({ key: "companies/indigo/meetings/shared.md", etag: "1" }),
441
+ ];
442
+ },
443
+ });
444
+ expect(calls.sort()).toEqual([
445
+ "companies/indigo/meetings/",
446
+ "companies/indigo/scratch/jacob/",
447
+ ]);
448
+ expect(files.map((f) => f.key).sort()).toEqual([
449
+ "companies/indigo/meetings/a.md",
450
+ "companies/indigo/meetings/shared.md",
451
+ "companies/indigo/scratch/jacob/draft.md",
452
+ ]);
453
+ });
454
+
455
+ it("strategy=vend-fanout with > VEND_PATH_CAP prefixes batches and lists all of them", async () => {
456
+ const prefixes = Array.from(
457
+ { length: VEND_PATH_CAP + 3 },
458
+ (_, i) => `companies/indigo/p${i}/`,
459
+ );
460
+ const calls = new Set<string | undefined>();
461
+ await listRemoteForScope({
462
+ ctx: makeCtx(),
463
+ scope: {
464
+ companyUid: "cmp_indigo",
465
+ syncMode: "shared",
466
+ prefixSet: prefixes,
467
+ strategy: "vend-fanout",
468
+ },
469
+ listFn: async (_ctx, prefix) => {
470
+ calls.add(prefix);
471
+ return [];
472
+ },
473
+ });
474
+ expect(calls.size).toBe(VEND_PATH_CAP + 3); // every prefix listed
475
+ });
476
+
477
+ it("strategy=vend-fanout uses vendForBatchFn to narrow credentials per batch", async () => {
478
+ const vendCalls: Array<{ paths: string[] }> = [];
479
+ await listRemoteForScope({
480
+ ctx: makeCtx(),
481
+ scope: {
482
+ companyUid: "cmp_indigo",
483
+ syncMode: "shared",
484
+ prefixSet: ["companies/indigo/a/", "companies/indigo/b/"],
485
+ strategy: "vend-fanout",
486
+ },
487
+ listFn: async () => [],
488
+ vendForBatchFn: async (ctx, paths) => {
489
+ vendCalls.push({ paths });
490
+ return ctx;
491
+ },
492
+ });
493
+ expect(vendCalls).toHaveLength(1); // one batch (≤ VEND_PATH_CAP)
494
+ expect(vendCalls[0]?.paths).toEqual([
495
+ "companies/indigo/a/",
496
+ "companies/indigo/b/",
497
+ ]);
498
+ });
499
+
500
+ it("strategy=broad-postfilter issues one wide list + client-side filter", async () => {
501
+ const calls: Array<string | undefined> = [];
502
+ const files = await listRemoteForScope({
503
+ ctx: makeCtx(),
504
+ scope: {
505
+ companyUid: "cmp_indigo",
506
+ syncMode: "shared",
507
+ prefixSet: ["companies/indigo/meetings/"],
508
+ strategy: "broad-postfilter",
509
+ },
510
+ listFn: async (_ctx, prefix) => {
511
+ calls.push(prefix);
512
+ return [
513
+ remote({ key: "companies/indigo/meetings/a.md" }),
514
+ remote({ key: "companies/indigo/scratch/jacob/draft.md" }),
515
+ ];
516
+ },
517
+ });
518
+ expect(calls).toEqual([undefined]); // one broad list, no prefix
519
+ expect(files.map((f) => f.key)).toEqual([
520
+ "companies/indigo/meetings/a.md",
521
+ ]);
522
+ });
523
+
524
+ it("strategy=vend-fanout short-circuits to [] on empty prefixSet", async () => {
525
+ let listed = false;
526
+ const files = await listRemoteForScope({
527
+ ctx: makeCtx(),
528
+ scope: {
529
+ companyUid: "cmp_indigo",
530
+ syncMode: "shared",
531
+ prefixSet: [],
532
+ strategy: "vend-fanout",
533
+ },
534
+ listFn: async () => {
535
+ listed = true;
536
+ return [];
537
+ },
538
+ });
539
+ expect(listed).toBe(false);
540
+ expect(files).toEqual([]);
541
+ });
542
+ });
543
+
544
+ describe("pullCompany (engine orchestrator)", () => {
545
+ let hqRoot: string;
546
+
547
+ beforeEach(() => {
548
+ hqRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hq-pull-company-"));
549
+ });
550
+
551
+ afterEach(() => {
552
+ fs.rmSync(hqRoot, { recursive: true, force: true });
553
+ });
554
+
555
+ it("records syncMode + prefixSet on the PullRecord for an 'all' pull", async () => {
556
+ const journal: SyncJournal = {
557
+ version: "2",
558
+ lastSync: "",
559
+ files: {},
560
+ pulls: [],
561
+ };
562
+ const result = await pullCompany({
563
+ ctx: makeCtx(),
564
+ journal,
565
+ hqRoot,
566
+ scope: {
567
+ companyUid: "cmp_indigo",
568
+ syncMode: "all",
569
+ prefixSet: ["companies/indigo/"],
570
+ strategy: "all",
571
+ },
572
+ listFn: async () => [],
573
+ });
574
+ expect(result.pullRecord.syncMode).toBe("all");
575
+ expect(result.pullRecord.prefixSet).toEqual(["companies/indigo/"]);
576
+ expect(result.pullRecord.scopeChangeDetected).toBe(false);
577
+ expect(journal.pulls).toHaveLength(1);
578
+ });
579
+
580
+ it("aborts with ScopeShrinkBlockedError on dirty orphan (default mode)", async () => {
581
+ const abs = path.join(
582
+ hqRoot,
583
+ "companies/indigo/scratch/notes.md",
584
+ );
585
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
586
+ fs.writeFileSync(abs, "MODIFIED");
587
+
588
+ const journal: SyncJournal = {
589
+ version: "2",
590
+ lastSync: "",
591
+ files: {
592
+ "companies/indigo/scratch/notes.md": {
593
+ hash: sha256("ORIGINAL"),
594
+ size: 8,
595
+ syncedAt: new Date().toISOString(),
596
+ direction: "down",
597
+ },
598
+ },
599
+ pulls: [
600
+ {
601
+ pullId: "01PREV",
602
+ companyUid: "cmp_indigo",
603
+ startedAt: "2026-05-19T00:00:00.000Z",
604
+ completedAt: "2026-05-19T00:00:05.000Z",
605
+ syncMode: "all",
606
+ prefixSet: ["companies/indigo/"],
607
+ scopeChangeDetected: false,
608
+ orphansRemoved: 0,
609
+ orphansBlocked: 0,
610
+ },
611
+ ],
612
+ };
613
+
614
+ await expect(
615
+ pullCompany({
616
+ ctx: makeCtx(),
617
+ journal,
618
+ hqRoot,
619
+ scope: {
620
+ companyUid: "cmp_indigo",
621
+ syncMode: "shared",
622
+ prefixSet: ["companies/indigo/meetings/"],
623
+ strategy: "vend-fanout",
624
+ },
625
+ listFn: async () => [],
626
+ }),
627
+ ).rejects.toBeInstanceOf(ScopeShrinkBlockedError);
628
+ });
629
+
630
+ it("applies scope-shrink + records orphansBlocked when forceScopeShrink=true", async () => {
631
+ const dirtyAbs = path.join(
632
+ hqRoot,
633
+ "companies/indigo/scratch/dirty.md",
634
+ );
635
+ const cleanAbs = path.join(
636
+ hqRoot,
637
+ "companies/indigo/scratch/clean.md",
638
+ );
639
+ fs.mkdirSync(path.dirname(dirtyAbs), { recursive: true });
640
+ fs.writeFileSync(dirtyAbs, "MODIFIED");
641
+ fs.writeFileSync(cleanAbs, "clean");
642
+ const past = Date.now() - 60_000;
643
+ fs.utimesSync(cleanAbs, past / 1000, past / 1000);
644
+
645
+ const journal: SyncJournal = {
646
+ version: "2",
647
+ lastSync: "",
648
+ files: {
649
+ "companies/indigo/scratch/dirty.md": {
650
+ hash: sha256("ORIGINAL"),
651
+ size: 8,
652
+ syncedAt: new Date().toISOString(),
653
+ direction: "down",
654
+ },
655
+ "companies/indigo/scratch/clean.md": {
656
+ hash: sha256("clean"),
657
+ size: 5,
658
+ syncedAt: new Date().toISOString(),
659
+ direction: "down",
660
+ },
661
+ },
662
+ pulls: [
663
+ {
664
+ pullId: "01PREV",
665
+ companyUid: "cmp_indigo",
666
+ startedAt: "2026-05-19T00:00:00.000Z",
667
+ completedAt: "2026-05-19T00:00:05.000Z",
668
+ syncMode: "all",
669
+ prefixSet: ["companies/indigo/"],
670
+ scopeChangeDetected: false,
671
+ orphansRemoved: 0,
672
+ orphansBlocked: 0,
673
+ },
674
+ ],
675
+ };
676
+
677
+ const result = await pullCompany({
678
+ ctx: makeCtx(),
679
+ journal,
680
+ hqRoot,
681
+ forceScopeShrink: true,
682
+ scope: {
683
+ companyUid: "cmp_indigo",
684
+ syncMode: "shared",
685
+ prefixSet: ["companies/indigo/meetings/"],
686
+ strategy: "vend-fanout",
687
+ },
688
+ listFn: async () => [],
689
+ });
690
+
691
+ expect(result.pullRecord.scopeChangeDetected).toBe(true);
692
+ expect(result.pullRecord.orphansRemoved).toBe(1); // clean
693
+ expect(result.pullRecord.orphansBlocked).toBe(1); // dirty tombstoned
694
+ expect(fs.existsSync(cleanAbs)).toBe(false); // deleted
695
+ expect(fs.existsSync(dirtyAbs)).toBe(true); // preserved
696
+ expect(
697
+ journal.files["companies/indigo/scratch/dirty.md"]?.removedAt,
698
+ ).toBeTruthy();
699
+ });
700
+
701
+ it("v1 → v2 migration: empty pulls[] history treats last scope as company-prefix-wide", async () => {
702
+ // No previous PullRecord → engine derives `companies/indigo/` as the
703
+ // "last scope" so a scope shrink to a narrower prefix is correctly
704
+ // detected on the FIRST v2 pull after upgrade.
705
+ const journal: SyncJournal = {
706
+ version: "1", // simulate pre-upgrade
707
+ lastSync: "",
708
+ files: {
709
+ "companies/indigo/scratch/jacob/draft.md": {
710
+ hash: sha256("draft"),
711
+ size: 5,
712
+ syncedAt: new Date(Date.now() - 60_000).toISOString(),
713
+ direction: "down",
714
+ },
715
+ },
716
+ };
717
+ const draftAbs = path.join(
718
+ hqRoot,
719
+ "companies/indigo/scratch/jacob/draft.md",
720
+ );
721
+ fs.mkdirSync(path.dirname(draftAbs), { recursive: true });
722
+ fs.writeFileSync(draftAbs, "draft");
723
+ const past = Date.now() - 60_000;
724
+ fs.utimesSync(draftAbs, past / 1000, past / 1000);
725
+
726
+ const result = await pullCompany({
727
+ ctx: makeCtx(),
728
+ journal,
729
+ hqRoot,
730
+ scope: {
731
+ companyUid: "cmp_indigo",
732
+ syncMode: "shared",
733
+ prefixSet: ["companies/indigo/meetings/"],
734
+ strategy: "vend-fanout",
735
+ },
736
+ listFn: async () => [],
737
+ });
738
+
739
+ expect(result.pullRecord.scopeChangeDetected).toBe(true);
740
+ expect(result.pullRecord.orphansRemoved).toBe(1);
741
+ expect(journal.version).toBe("2"); // migrated by appendPullRecord
742
+ });
743
+
744
+ it("GC's expired tombstones at the start of every leg", async () => {
745
+ const old = new Date(
746
+ Date.now() - 31 * 24 * 60 * 60 * 1000,
747
+ ).toISOString();
748
+ const journal: SyncJournal = {
749
+ version: "2",
750
+ lastSync: "",
751
+ files: {
752
+ "old-tombstone.md": {
753
+ hash: "h",
754
+ size: 1,
755
+ syncedAt: "",
756
+ direction: "down",
757
+ removedAt: old,
758
+ removedReason: "scope_shrink",
759
+ },
760
+ },
761
+ pulls: [],
762
+ };
763
+ const result = await pullCompany({
764
+ ctx: makeCtx(),
765
+ journal,
766
+ hqRoot,
767
+ scope: {
768
+ companyUid: "cmp_indigo",
769
+ syncMode: "all",
770
+ prefixSet: ["companies/indigo/"],
771
+ strategy: "all",
772
+ },
773
+ listFn: async () => [],
774
+ });
775
+ expect(result.tombstonesGcd).toBe(1);
776
+ expect(journal.files["old-tombstone.md"]).toBeUndefined();
777
+ });
778
+ });