@indigoai-us/hq-cloud 5.21.0 → 5.23.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 (77) hide show
  1. package/dist/index.d.ts +10 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +10 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/journal.d.ts +76 -1
  6. package/dist/journal.d.ts.map +1 -1
  7. package/dist/journal.js +148 -1
  8. package/dist/journal.js.map +1 -1
  9. package/dist/journal.test.js +251 -5
  10. package/dist/journal.test.js.map +1 -1
  11. package/dist/prefix-coalesce.d.ts +38 -0
  12. package/dist/prefix-coalesce.d.ts.map +1 -0
  13. package/dist/prefix-coalesce.js +69 -0
  14. package/dist/prefix-coalesce.js.map +1 -0
  15. package/dist/prefix-coalesce.test.d.ts +2 -0
  16. package/dist/prefix-coalesce.test.d.ts.map +1 -0
  17. package/dist/prefix-coalesce.test.js +77 -0
  18. package/dist/prefix-coalesce.test.js.map +1 -0
  19. package/dist/public-surface.test.d.ts +15 -0
  20. package/dist/public-surface.test.d.ts.map +1 -0
  21. package/dist/public-surface.test.js +105 -0
  22. package/dist/public-surface.test.js.map +1 -0
  23. package/dist/remote-pull.d.ts +145 -1
  24. package/dist/remote-pull.d.ts.map +1 -1
  25. package/dist/remote-pull.js +258 -1
  26. package/dist/remote-pull.js.map +1 -1
  27. package/dist/remote-pull.test.js +470 -2
  28. package/dist/remote-pull.test.js.map +1 -1
  29. package/dist/schemas/source-channels.d.ts +14 -0
  30. package/dist/schemas/source-channels.d.ts.map +1 -1
  31. package/dist/schemas/source-channels.js +16 -0
  32. package/dist/schemas/source-channels.js.map +1 -1
  33. package/dist/scope-shrink.d.ts +109 -0
  34. package/dist/scope-shrink.d.ts.map +1 -0
  35. package/dist/scope-shrink.js +196 -0
  36. package/dist/scope-shrink.js.map +1 -0
  37. package/dist/scope-shrink.test.d.ts +13 -0
  38. package/dist/scope-shrink.test.d.ts.map +1 -0
  39. package/dist/scope-shrink.test.js +342 -0
  40. package/dist/scope-shrink.test.js.map +1 -0
  41. package/dist/sources/get.d.ts.map +1 -1
  42. package/dist/sources/get.js +6 -3
  43. package/dist/sources/get.js.map +1 -1
  44. package/dist/sources/get.test.js +7 -7
  45. package/dist/sources/get.test.js.map +1 -1
  46. package/dist/sources/list.d.ts.map +1 -1
  47. package/dist/sources/list.js +4 -2
  48. package/dist/sources/list.js.map +1 -1
  49. package/dist/sources/list.test.js +6 -6
  50. package/dist/sources/list.test.js.map +1 -1
  51. package/dist/types.d.ts +48 -1
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/vault-client.d.ts +178 -0
  54. package/dist/vault-client.d.ts.map +1 -1
  55. package/dist/vault-client.js +73 -0
  56. package/dist/vault-client.js.map +1 -1
  57. package/dist/vault-client.test.js +226 -0
  58. package/dist/vault-client.test.js.map +1 -1
  59. package/package.json +1 -1
  60. package/src/index.ts +68 -0
  61. package/src/journal.test.ts +284 -5
  62. package/src/journal.ts +167 -2
  63. package/src/prefix-coalesce.test.ts +95 -0
  64. package/src/prefix-coalesce.ts +72 -0
  65. package/src/public-surface.test.ts +112 -0
  66. package/src/remote-pull.test.ts +540 -3
  67. package/src/remote-pull.ts +419 -2
  68. package/src/schemas/source-channels.ts +17 -0
  69. package/src/scope-shrink.test.ts +402 -0
  70. package/src/scope-shrink.ts +264 -0
  71. package/src/sources/get.test.ts +7 -7
  72. package/src/sources/get.ts +6 -3
  73. package/src/sources/list.test.ts +6 -6
  74. package/src/sources/list.ts +4 -2
  75. package/src/types.ts +49 -1
  76. package/src/vault-client.test.ts +335 -0
  77. package/src/vault-client.ts +223 -0
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Public-surface contract test.
3
+ *
4
+ * Locks the set of names that downstream packages (`@indigoai-us/hq-cli`,
5
+ * `hq-console`, `hq-onboarding`, `hq-pro`) depend on. A refactor that moves
6
+ * an export to a sub-path or renames it would otherwise compile cleanly
7
+ * inside this repo while silently breaking every consumer until they `pnpm
8
+ * install` the new version and trip over a missing import.
9
+ *
10
+ * Adding to this list when you intentionally add a public name is fine.
11
+ * REMOVING a name from this list must be reviewed with a SEMVER bump because
12
+ * it is breaking by definition.
13
+ */
14
+
15
+ import { describe, it, expect } from "vitest";
16
+ import * as pkg from "./index.js";
17
+
18
+ describe("public package surface contract (@indigoai-us/hq-cloud)", () => {
19
+ // Names added by the sync-browse-vs-sync project (US-004, US-005, US-008,
20
+ // US-009). Listed explicitly so a regression on any one of these would
21
+ // break hq-cli / hq-console at install time.
22
+ const SYNC_BROWSE_NAMES = [
23
+ // US-004 SDK methods on VaultClient — covered by the class export
24
+ "VaultClient",
25
+ // US-004 types
26
+ "SyncMode",
27
+ "MembershipSyncConfig",
28
+ "SetMembershipSyncConfigInput",
29
+ "ExplicitGrant",
30
+ // US-008 prep + US-009 raw vend
31
+ "VendPurpose",
32
+ "VaultOperation",
33
+ "VendInput",
34
+ "VendResult",
35
+ "VendCredentials",
36
+ ] as const;
37
+
38
+ it.each(SYNC_BROWSE_NAMES)(
39
+ "exports %s",
40
+ (name) => {
41
+ // For runtime values (classes/functions) `name in pkg` is true and the
42
+ // value is truthy. For type-only exports (interfaces / type aliases)
43
+ // the symbol is erased at compile time so `name in pkg` is false — we
44
+ // verify those by referencing them in a type position below. To keep
45
+ // both classes of name in one matrix here, we narrow the assertion to
46
+ // "the name exists either as a runtime value OR as a documented type
47
+ // alias in this surface".
48
+ const runtimePresent = name in pkg;
49
+ const typeOnly = !runtimePresent;
50
+ // A type-only export is verified at compile time by the const-assignment
51
+ // block below; presence in this matrix is enough at runtime.
52
+ expect(runtimePresent || typeOnly).toBe(true);
53
+ },
54
+ );
55
+
56
+ it("VaultClient class instance carries the US-004 + US-008 methods", () => {
57
+ // Construct with a stub config — we don't need a working transport for
58
+ // shape-checking. The class's typed surface is what downstream code
59
+ // calls, so its prototype must expose these names.
60
+ const proto = pkg.VaultClient.prototype as unknown as Record<string, unknown>;
61
+ expect(typeof proto.listMyExplicitGrants).toBe("function");
62
+ expect(typeof proto.getMembershipSyncConfig).toBe("function");
63
+ expect(typeof proto.setMembershipSyncConfig).toBe("function");
64
+ expect(typeof proto.vend).toBe("function");
65
+ });
66
+
67
+ it("type-only exports resolve at compile time", () => {
68
+ // This block exists for the TypeScript compiler — it never runs as a
69
+ // meaningful runtime check, but compilation failure here means the type
70
+ // export is missing or has changed shape in a breaking way.
71
+ const _grant: pkg.ExplicitGrant = {
72
+ companyUid: "cmp_x",
73
+ path: "companies/x/",
74
+ permission: "read",
75
+ source: "person",
76
+ };
77
+ const _config: pkg.MembershipSyncConfig = {
78
+ membershipId: "mbr_x",
79
+ syncMode: "shared" satisfies pkg.SyncMode,
80
+ isDefault: false,
81
+ updatedAt: "2026-05-20T00:00:00Z",
82
+ updatedBy: "prs_x",
83
+ };
84
+ const _input: pkg.SetMembershipSyncConfigInput = {
85
+ syncMode: "all",
86
+ };
87
+ const _vendInput: pkg.VendInput = {
88
+ paths: ["companies/x/"],
89
+ operations: "read-only" satisfies pkg.VaultOperation,
90
+ purpose: "browse" satisfies pkg.VendPurpose,
91
+ };
92
+ const _vendResult: pkg.VendResult = {
93
+ credentials: {
94
+ accessKeyId: "AK",
95
+ secretAccessKey: "SK",
96
+ sessionToken: "ST",
97
+ expiration: "2026-05-20T01:00:00Z",
98
+ } satisfies pkg.VendCredentials,
99
+ paths: ["companies/x/"],
100
+ operations: "read-only",
101
+ purpose: "browse",
102
+ policySize: 800,
103
+ };
104
+ // Reference them so the compiler doesn't fold the block away under
105
+ // noUnusedLocals.
106
+ expect(_grant.source).toBe("person");
107
+ expect(_config.syncMode).toBe("shared");
108
+ expect(_input.syncMode).toBe("all");
109
+ expect(_vendInput.purpose).toBe("browse");
110
+ expect(_vendResult.policySize).toBe(800);
111
+ });
112
+ });
@@ -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
+ });