@indigoai-us/hq-cloud 6.18.11-alpha.1 → 6.18.11

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 (106) hide show
  1. package/dist/bin/sync-runner-company.d.ts +1 -1
  2. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  3. package/dist/bin/sync-runner-company.js +2 -2
  4. package/dist/bin/sync-runner-company.js.map +1 -1
  5. package/dist/bin/sync-runner-company.test.js +0 -31
  6. package/dist/bin/sync-runner-company.test.js.map +1 -1
  7. package/dist/bin/sync-runner-watch-loop.d.ts +2 -0
  8. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  9. package/dist/bin/sync-runner-watch-loop.js +51 -212
  10. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  11. package/dist/bin/sync-runner-watch-loop.test.js +20 -72
  12. package/dist/bin/sync-runner-watch-loop.test.js.map +1 -1
  13. package/dist/bin/sync-runner-watch-routes.d.ts +1 -2
  14. package/dist/bin/sync-runner-watch-routes.d.ts.map +1 -1
  15. package/dist/bin/sync-runner-watch-routes.js +15 -7
  16. package/dist/bin/sync-runner-watch-routes.js.map +1 -1
  17. package/dist/bin/sync-runner-watch-routes.test.js +16 -15
  18. package/dist/bin/sync-runner-watch-routes.test.js.map +1 -1
  19. package/dist/bin/sync-runner.d.ts +1 -69
  20. package/dist/bin/sync-runner.d.ts.map +1 -1
  21. package/dist/bin/sync-runner.js +10 -50
  22. package/dist/bin/sync-runner.js.map +1 -1
  23. package/dist/bin/sync-runner.test.js +40 -121
  24. package/dist/bin/sync-runner.test.js.map +1 -1
  25. package/dist/cli/share.d.ts.map +1 -1
  26. package/dist/cli/share.js +0 -2
  27. package/dist/cli/share.js.map +1 -1
  28. package/dist/cli/share.test.js +0 -76
  29. package/dist/cli/share.test.js.map +1 -1
  30. package/dist/cli/sync.d.ts +8 -11
  31. package/dist/cli/sync.d.ts.map +1 -1
  32. package/dist/cli/sync.js +23 -92
  33. package/dist/cli/sync.js.map +1 -1
  34. package/dist/cli/sync.test.js +182 -228
  35. package/dist/cli/sync.test.js.map +1 -1
  36. package/dist/ignore.d.ts.map +1 -1
  37. package/dist/ignore.js +0 -3
  38. package/dist/ignore.js.map +1 -1
  39. package/dist/ignore.test.js +0 -8
  40. package/dist/ignore.test.js.map +1 -1
  41. package/dist/journal.d.ts +0 -15
  42. package/dist/journal.d.ts.map +1 -1
  43. package/dist/journal.js +2 -66
  44. package/dist/journal.js.map +1 -1
  45. package/dist/journal.test.js +1 -16
  46. package/dist/journal.test.js.map +1 -1
  47. package/dist/manifest/build-manifest-resident-parity.test.js +3 -5
  48. package/dist/manifest/build-manifest-resident-parity.test.js.map +1 -1
  49. package/dist/sync/event-sync.d.ts.map +1 -1
  50. package/dist/sync/event-sync.js +1 -58
  51. package/dist/sync/event-sync.js.map +1 -1
  52. package/dist/sync/event-sync.test.js +0 -46
  53. package/dist/sync/event-sync.test.js.map +1 -1
  54. package/dist/sync/logger.test.js +0 -3
  55. package/dist/sync/logger.test.js.map +1 -1
  56. package/dist/sync/metrics.test.js +0 -3
  57. package/dist/sync/metrics.test.js.map +1 -1
  58. package/dist/sync/push-receiver.d.ts +0 -52
  59. package/dist/sync/push-receiver.d.ts.map +1 -1
  60. package/dist/sync/push-receiver.js +50 -416
  61. package/dist/sync/push-receiver.js.map +1 -1
  62. package/dist/sync/push-receiver.test.js +40 -673
  63. package/dist/sync/push-receiver.test.js.map +1 -1
  64. package/dist/sync/targeted-upsert-exact-list.d.ts +33 -0
  65. package/dist/sync/targeted-upsert-exact-list.d.ts.map +1 -0
  66. package/dist/sync/targeted-upsert-exact-list.js +137 -0
  67. package/dist/sync/targeted-upsert-exact-list.js.map +1 -0
  68. package/dist/sync/targeted-upsert-exact-list.test.d.ts +2 -0
  69. package/dist/sync/targeted-upsert-exact-list.test.d.ts.map +1 -0
  70. package/dist/sync/targeted-upsert-exact-list.test.js +151 -0
  71. package/dist/sync/targeted-upsert-exact-list.test.js.map +1 -0
  72. package/dist/watcher.d.ts +0 -18
  73. package/dist/watcher.d.ts.map +1 -1
  74. package/dist/watcher.js +13 -78
  75. package/dist/watcher.js.map +1 -1
  76. package/dist/watcher.test.js +0 -72
  77. package/dist/watcher.test.js.map +1 -1
  78. package/package.json +1 -2
  79. package/dist/bin/sync-runner-args.test.d.ts +0 -2
  80. package/dist/bin/sync-runner-args.test.d.ts.map +0 -1
  81. package/dist/bin/sync-runner-args.test.js +0 -82
  82. package/dist/bin/sync-runner-args.test.js.map +0 -1
  83. package/dist/bin/sync-runner-delete-snapshot-mixed.bench.test.d.ts +0 -2
  84. package/dist/bin/sync-runner-delete-snapshot-mixed.bench.test.d.ts.map +0 -1
  85. package/dist/bin/sync-runner-delete-snapshot-mixed.bench.test.js +0 -366
  86. package/dist/bin/sync-runner-delete-snapshot-mixed.bench.test.js.map +0 -1
  87. package/dist/bin/sync-runner-delete-snapshot.bench.test.d.ts +0 -2
  88. package/dist/bin/sync-runner-delete-snapshot.bench.test.d.ts.map +0 -1
  89. package/dist/bin/sync-runner-delete-snapshot.bench.test.js +0 -317
  90. package/dist/bin/sync-runner-delete-snapshot.bench.test.js.map +0 -1
  91. package/dist/sync/content-hash.d.ts +0 -4
  92. package/dist/sync/content-hash.d.ts.map +0 -1
  93. package/dist/sync/content-hash.js +0 -11
  94. package/dist/sync/content-hash.js.map +0 -1
  95. package/dist/sync/content-hash.test.d.ts +0 -2
  96. package/dist/sync/content-hash.test.d.ts.map +0 -1
  97. package/dist/sync/content-hash.test.js +0 -16
  98. package/dist/sync/content-hash.test.js.map +0 -1
  99. package/dist/sync/unchanged-push-events.d.ts +0 -16
  100. package/dist/sync/unchanged-push-events.d.ts.map +0 -1
  101. package/dist/sync/unchanged-push-events.js +0 -51
  102. package/dist/sync/unchanged-push-events.js.map +0 -1
  103. package/dist/sync/unchanged-push-events.test.d.ts +0 -2
  104. package/dist/sync/unchanged-push-events.test.d.ts.map +0 -1
  105. package/dist/sync/unchanged-push-events.test.js +0 -76
  106. package/dist/sync/unchanged-push-events.test.js.map +0 -1
@@ -149,11 +149,20 @@ vi.mock("../remote-pull.js", async (importOriginal) => {
149
149
  const { vi: innerVi } = await import("vitest");
150
150
  return { ...actual, listRemoteForScope: innerVi.fn(actual.listRemoteForScope) };
151
151
  });
152
+ vi.mock("../sync/targeted-upsert-exact-list.js", async (importOriginal) => {
153
+ const actual = await importOriginal();
154
+ const { vi: innerVi } = await import("vitest");
155
+ return {
156
+ ...actual,
157
+ isTargetedUpsertExactListEnabled: innerVi.fn().mockResolvedValue(false),
158
+ };
159
+ });
152
160
  import { sync, reportNewFilesToNotify, applyRealtimeDeltaPage, isSyncMutationStdinCommand, runSyncMutationStdinJson, SYNC_MUTATION_STDIN_COMMAND, FULL_PULL_COOPERATIVE_CHECKPOINT_BATCH_SIZE, FULL_PULL_LIST_PAGE_CHECKPOINT_SIZE, setListCooperativeYieldMsForTesting, targetedJournalSessionCensusSizes, win32SymlinkFlavorBroken, _testing as syncTesting, } from "./sync.js";
153
161
  import { share } from "./share.js";
154
162
  import * as s3Module from "../s3.js";
155
163
  import * as conflictModule from "./conflict.js";
156
164
  import * as remotePullModule from "../remote-pull.js";
165
+ import * as targetedUpsertExactListModule from "../sync/targeted-upsert-exact-list.js";
157
166
  import { CooperativePassScheduler } from "../bin/sync-runner-watch-loop.js";
158
167
  import { reindex } from "./reindex.js";
159
168
  import { readSyncProgress } from "../sync-progress.js";
@@ -287,6 +296,9 @@ describe("sync", () => {
287
296
  let journalPath;
288
297
  beforeEach(() => {
289
298
  clearContextCache();
299
+ vi.mocked(targetedUpsertExactListModule.isTargetedUpsertExactListEnabled)
300
+ .mockReset()
301
+ .mockResolvedValue(false);
290
302
  tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-sync-test-"));
291
303
  // Journal moved to ~/.hq/sync-journal.{slug}.json (ADR-0001 Phase 5).
292
304
  // Redirect to a tmp dir via HQ_STATE_DIR so the test doesn't pollute the
@@ -307,6 +319,176 @@ describe("sync", () => {
307
319
  it("keeps classifyVaultKey available from the partial S3 mock", () => {
308
320
  expect(s3Module.classifyVaultKey("docs/readme.md", "company")).toBeNull();
309
321
  });
322
+ it("pulls a remote upsert within the normal event latency using one exact listing when enabled", async () => {
323
+ vi.mocked(targetedUpsertExactListModule.isTargetedUpsertExactListEnabled)
324
+ .mockResolvedValue(true);
325
+ vi.mocked(remotePullModule.listRemoteForScope).mockResolvedValue([
326
+ {
327
+ key: "docs/remote-change.md",
328
+ size: 18,
329
+ lastModified: new Date(),
330
+ etag: '"remote-change"',
331
+ },
332
+ ]);
333
+ const startedAt = Date.now();
334
+ const result = await sync({
335
+ company: "acme",
336
+ vaultConfig: mockConfig,
337
+ hqRoot: tmpDir,
338
+ syncMode: "custom",
339
+ prefixSet: ["docs/"],
340
+ pullPrefixes: ["docs/remote-change.md"],
341
+ pullExactPaths: ["docs/remote-change.md"],
342
+ skipReindex: true,
343
+ });
344
+ expect(result.filesDownloaded).toBe(1);
345
+ expect(fs.existsSync(path.join(tmpDir, "companies", "acme", "docs", "remote-change.md")))
346
+ .toBe(true);
347
+ expect(Date.now() - startedAt).toBeLessThan(60_000);
348
+ expect(remotePullModule.listRemoteForScope).toHaveBeenCalledWith(expect.objectContaining({
349
+ scope: expect.objectContaining({
350
+ prefixSet: ["docs/remote-change.md"],
351
+ strategy: "vend-fanout",
352
+ }),
353
+ }));
354
+ });
355
+ it("does not wait for a never-settling flag read and keeps the pull on legacy scope", async () => {
356
+ let markFlagFetchStarted;
357
+ const flagFetchStarted = new Promise((resolve) => {
358
+ markFlagFetchStarted = resolve;
359
+ });
360
+ const neverSettles = new Promise(() => { });
361
+ const fallbackFetch = globalThis.fetch;
362
+ const fetch = vi.fn((input, init) => {
363
+ if (String(input).includes("/v1/flags/resolve")) {
364
+ markFlagFetchStarted();
365
+ return neverSettles;
366
+ }
367
+ return fallbackFetch(input, init);
368
+ });
369
+ vi.stubGlobal("fetch", fetch);
370
+ const gate = targetedUpsertExactListModule.createTargetedUpsertExactListGate({
371
+ env: { HQ_FLAGS_API_URL: "https://flags.example.test" },
372
+ });
373
+ vi.mocked(targetedUpsertExactListModule.isTargetedUpsertExactListEnabled)
374
+ .mockImplementation(gate);
375
+ vi.mocked(remotePullModule.listRemoteForScope).mockResolvedValue([]);
376
+ const pull = sync({
377
+ company: "acme",
378
+ vaultConfig: mockConfig,
379
+ hqRoot: tmpDir,
380
+ syncMode: "custom",
381
+ prefixSet: ["docs/"],
382
+ pullPrefixes: ["docs/remote-change.md"],
383
+ pullExactPaths: ["docs/remote-change.md"],
384
+ skipReindex: true,
385
+ });
386
+ const completion = await Promise.race([
387
+ pull.then(() => "completed"),
388
+ new Promise((resolve) => setTimeout(() => resolve("delayed"), 500)),
389
+ ]);
390
+ expect(completion).toBe("completed");
391
+ await flagFetchStarted;
392
+ expect(remotePullModule.listRemoteForScope).toHaveBeenCalledWith(expect.objectContaining({
393
+ scope: expect.objectContaining({
394
+ prefixSet: ["docs/remote-change.md", "docs/remote-change.md/"],
395
+ strategy: "vend-fanout",
396
+ }),
397
+ }));
398
+ });
399
+ it("uses a healthy flag snapshot for a later pull while its refresh is pending", async () => {
400
+ let now = 100;
401
+ let markNextRefreshStarted;
402
+ const nextRefreshStarted = new Promise((resolve) => {
403
+ markNextRefreshStarted = resolve;
404
+ });
405
+ const neverSettles = new Promise(() => { });
406
+ const fallbackFetch = globalThis.fetch;
407
+ let flagFetchCount = 0;
408
+ const fetch = vi.fn((input, init) => {
409
+ if (String(input).includes("/v1/flags/resolve")) {
410
+ flagFetchCount += 1;
411
+ if (flagFetchCount === 1) {
412
+ return Promise.resolve(new Response(JSON.stringify({
413
+ version: 1,
414
+ flags: { [targetedUpsertExactListModule.TARGETED_UPSERT_EXACT_LIST_FLAG]: true },
415
+ }), { status: 200, headers: { etag: "\"v1\"" } }));
416
+ }
417
+ markNextRefreshStarted();
418
+ return neverSettles;
419
+ }
420
+ return fallbackFetch(input, init);
421
+ });
422
+ vi.stubGlobal("fetch", fetch);
423
+ const gate = targetedUpsertExactListModule.createTargetedUpsertExactListGate({
424
+ env: { HQ_FLAGS_API_URL: "https://flags.example.test" },
425
+ now: () => now,
426
+ });
427
+ vi.mocked(targetedUpsertExactListModule.isTargetedUpsertExactListEnabled)
428
+ .mockImplementation(gate);
429
+ vi.mocked(remotePullModule.listRemoteForScope).mockResolvedValue([]);
430
+ await sync({
431
+ company: "acme",
432
+ vaultConfig: mockConfig,
433
+ hqRoot: tmpDir,
434
+ syncMode: "custom",
435
+ prefixSet: ["docs/"],
436
+ pullPrefixes: ["docs/remote-change.md"],
437
+ pullExactPaths: ["docs/remote-change.md"],
438
+ skipReindex: true,
439
+ });
440
+ expect(remotePullModule.listRemoteForScope).toHaveBeenLastCalledWith(expect.objectContaining({
441
+ scope: expect.objectContaining({
442
+ prefixSet: ["docs/remote-change.md", "docs/remote-change.md/"],
443
+ strategy: "vend-fanout",
444
+ }),
445
+ }));
446
+ await vi.waitFor(async () => {
447
+ await expect(gate(mockConfig, mockEntity.uid)).resolves.toBe(true);
448
+ });
449
+ now += 5 * 60_000 + 1;
450
+ const pull = sync({
451
+ company: "acme",
452
+ vaultConfig: mockConfig,
453
+ hqRoot: tmpDir,
454
+ syncMode: "custom",
455
+ prefixSet: ["docs/"],
456
+ pullPrefixes: ["docs/remote-change.md"],
457
+ pullExactPaths: ["docs/remote-change.md"],
458
+ skipReindex: true,
459
+ });
460
+ const completion = await Promise.race([
461
+ pull.then(() => "completed"),
462
+ new Promise((resolve) => setTimeout(() => resolve("delayed"), 500)),
463
+ ]);
464
+ expect(completion).toBe("completed");
465
+ await nextRefreshStarted;
466
+ expect(remotePullModule.listRemoteForScope).toHaveBeenLastCalledWith(expect.objectContaining({
467
+ scope: expect.objectContaining({
468
+ prefixSet: ["docs/remote-change.md"],
469
+ strategy: "vend-fanout",
470
+ }),
471
+ }));
472
+ });
473
+ it("keeps exact upserts and descendant scopes when the default-off flag is missing", async () => {
474
+ vi.mocked(remotePullModule.listRemoteForScope).mockResolvedValue([]);
475
+ await sync({
476
+ company: "acme",
477
+ vaultConfig: mockConfig,
478
+ hqRoot: tmpDir,
479
+ syncMode: "custom",
480
+ prefixSet: ["docs/"],
481
+ pullPrefixes: ["docs/remote-change.md"],
482
+ pullExactPaths: ["docs/remote-change.md"],
483
+ skipReindex: true,
484
+ });
485
+ expect(remotePullModule.listRemoteForScope).toHaveBeenCalledWith(expect.objectContaining({
486
+ scope: expect.objectContaining({
487
+ prefixSet: ["docs/remote-change.md", "docs/remote-change.md/"],
488
+ strategy: "vend-fanout",
489
+ }),
490
+ }));
491
+ });
310
492
  it("keeps Deep Archive session logs remote and journaled without a GET, restore, materialization, or tombstone", async () => {
311
493
  const key = "workspace/.session-logs/codex/2026/08/31/rollout.jsonl";
312
494
  writeJournal("acme", {
@@ -1467,7 +1649,6 @@ describe("sync", () => {
1467
1649
  },
1468
1650
  deleteMessage: async ({ receiptHandle }) => { deleted.push(receiptHandle); },
1469
1651
  changeMessageVisibility: async () => { },
1470
- changeMessageVisibilityBatch: async () => ({ failedEntries: [] }),
1471
1652
  };
1472
1653
  const results = [];
1473
1654
  const receiver = new SqsPushReceiver({
@@ -1524,7 +1705,6 @@ describe("sync", () => {
1524
1705
  },
1525
1706
  deleteMessage: async ({ receiptHandle }) => { deleted.push(receiptHandle); },
1526
1707
  changeMessageVisibility: async () => { },
1527
- changeMessageVisibilityBatch: async () => ({ failedEntries: [] }),
1528
1708
  };
1529
1709
  let attempts = 0;
1530
1710
  const receiver = new SqsPushReceiver({
@@ -3225,232 +3405,6 @@ describe("sync", () => {
3225
3405
  }
3226
3406
  expect(fs.readFileSync(localPath, "utf-8")).toBe("unchanged body");
3227
3407
  });
3228
- it("downloads the listed revision when an older same-size announcement arrives late", async () => {
3229
- const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
3230
- fs.mkdirSync(companyDocs, { recursive: true });
3231
- const localPath = path.join(companyDocs, "handoff.md");
3232
- const revisionA = "revision A body\n";
3233
- const revisionB = "revision B body\n";
3234
- expect(Buffer.byteLength(revisionA)).toBe(Buffer.byteLength(revisionB));
3235
- fs.writeFileSync(localPath, revisionA);
3236
- const stat = fs.lstatSync(localPath);
3237
- const hashA = crypto.createHash("sha256").update(revisionA).digest("hex");
3238
- const hashB = crypto.createHash("sha256").update(revisionB).digest("hex");
3239
- const listedRevisionB = {
3240
- key: "docs/handoff.md",
3241
- size: Buffer.byteLength(revisionB),
3242
- lastModified: new Date(),
3243
- etag: '"revision-b-etag"',
3244
- };
3245
- writeJournal("acme", {
3246
- version: "2",
3247
- lastSync: new Date().toISOString(),
3248
- pulls: [],
3249
- files: {
3250
- "docs/handoff.md": {
3251
- hash: hashA,
3252
- size: stat.size,
3253
- mtimeMs: stat.mtimeMs,
3254
- ctimeMs: stat.ctimeMs,
3255
- remoteEtag: "revision-a-etag",
3256
- syncedAt: new Date(Date.now() - 3600000).toISOString(),
3257
- direction: "down",
3258
- kind: "file",
3259
- },
3260
- },
3261
- });
3262
- const listRemoteFiles = vi.mocked(s3Module.listRemoteFiles);
3263
- const originalListImplementation = listRemoteFiles.getMockImplementation();
3264
- listRemoteFiles.mockResolvedValue([listedRevisionB]);
3265
- vi.mocked(s3Module.headRemoteFile).mockResolvedValueOnce({
3266
- etag: '"revision-b-etag"',
3267
- size: Buffer.byteLength(revisionB),
3268
- lastModified: new Date(),
3269
- metadata: { [s3Module.FILE_CONTENT_SHA256_META_KEY]: hashB },
3270
- });
3271
- vi.mocked(s3Module.downloadFile).mockImplementationOnce(async (_ctx, _key, stagedPath, options) => {
3272
- fs.mkdirSync(path.dirname(stagedPath), { recursive: true });
3273
- options?.beforeReplace?.();
3274
- fs.writeFileSync(stagedPath, revisionB);
3275
- return {
3276
- metadata: { [s3Module.FILE_CONTENT_SHA256_META_KEY]: hashB },
3277
- contentHash: hashB,
3278
- contentSize: Buffer.byteLength(revisionB),
3279
- };
3280
- });
3281
- try {
3282
- await sync({
3283
- company: "acme",
3284
- onConflict: "keep",
3285
- vaultConfig: mockConfig,
3286
- hqRoot: tmpDir,
3287
- pullPrefixes: ["docs/handoff.md"],
3288
- // This delayed event describes A, while LIST and HEAD describe B.
3289
- pullContentHashes: { "docs/handoff.md": `sha256:${hashA}` },
3290
- skipReindex: true,
3291
- });
3292
- expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
3293
- expect(fs.readFileSync(localPath, "utf8")).toBe(revisionB);
3294
- expect(readJournal("acme").files["docs/handoff.md"]).toMatchObject({
3295
- hash: hashB,
3296
- remoteEtag: "revision-b-etag",
3297
- });
3298
- // B's later announcement sees B's bytes and ETag already committed.
3299
- await sync({
3300
- company: "acme",
3301
- onConflict: "keep",
3302
- vaultConfig: mockConfig,
3303
- hqRoot: tmpDir,
3304
- pullPrefixes: ["docs/handoff.md"],
3305
- pullContentHashes: { "docs/handoff.md": `sha256:${hashB}` },
3306
- skipReindex: true,
3307
- });
3308
- expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
3309
- expect(fs.readFileSync(localPath, "utf8")).toBe(revisionB);
3310
- }
3311
- finally {
3312
- if (originalListImplementation) {
3313
- listRemoteFiles.mockImplementation(originalListImplementation);
3314
- }
3315
- }
3316
- });
3317
- it("updates the remote ETag without downloading when a targeted event matches clean journal bytes", async () => {
3318
- const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
3319
- fs.mkdirSync(companyDocs, { recursive: true });
3320
- const localPath = path.join(companyDocs, "handoff.md");
3321
- const localContent = "unchanged body";
3322
- fs.writeFileSync(localPath, localContent);
3323
- const stat = fs.lstatSync(localPath);
3324
- const hash = crypto.createHash("sha256").update(localContent).digest("hex");
3325
- writeJournal("acme", {
3326
- version: "2",
3327
- lastSync: new Date().toISOString(),
3328
- pulls: [],
3329
- files: {
3330
- "docs/handoff.md": {
3331
- hash,
3332
- size: stat.size,
3333
- mtimeMs: stat.mtimeMs,
3334
- ctimeMs: stat.ctimeMs,
3335
- remoteEtag: "old-etag",
3336
- syncedAt: new Date(Date.now() - 3600000).toISOString(),
3337
- direction: "down",
3338
- kind: "file",
3339
- },
3340
- },
3341
- });
3342
- vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3343
- { key: "docs/handoff.md", size: stat.size, lastModified: new Date(), etag: '"new-etag"' },
3344
- ]);
3345
- vi.mocked(s3Module.headRemoteFile).mockResolvedValueOnce({
3346
- etag: '"new-etag"',
3347
- size: stat.size,
3348
- lastModified: new Date(),
3349
- metadata: { [s3Module.FILE_CONTENT_SHA256_META_KEY]: hash },
3350
- });
3351
- const result = await sync({
3352
- company: "acme",
3353
- onConflict: "keep",
3354
- vaultConfig: mockConfig,
3355
- hqRoot: tmpDir,
3356
- pullPrefixes: ["docs/handoff.md"],
3357
- pullContentHashes: { "docs/handoff.md": `sha256:${hash}` },
3358
- skipReindex: true,
3359
- });
3360
- expect(vi.mocked(s3Module.downloadFile)).not.toHaveBeenCalled();
3361
- expect(result.filesDownloaded).toBe(0);
3362
- expect(readJournal("acme").files["docs/handoff.md"]?.remoteEtag).toBe("new-etag");
3363
- expect(fs.readFileSync(localPath, "utf8")).toBe(localContent);
3364
- });
3365
- it.each([
3366
- "changed remote content hash",
3367
- "locally modified file",
3368
- "missing local file",
3369
- "missing journal row",
3370
- ])("still GETs through the normal pull path for a %s", async (caseName) => {
3371
- const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
3372
- fs.mkdirSync(companyDocs, { recursive: true });
3373
- const localPath = path.join(companyDocs, "handoff.md");
3374
- const baseline = "unchanged body";
3375
- fs.writeFileSync(localPath, baseline);
3376
- const stat = fs.lstatSync(localPath);
3377
- const baselineHash = crypto.createHash("sha256").update(baseline).digest("hex");
3378
- const announcedBody = caseName === "changed remote content hash"
3379
- ? "changed body!!"
3380
- : baseline;
3381
- const announcedHash = crypto.createHash("sha256").update(announcedBody).digest("hex");
3382
- if (caseName !== "missing journal row") {
3383
- writeJournal("acme", {
3384
- version: "2",
3385
- lastSync: new Date().toISOString(),
3386
- pulls: [],
3387
- files: {
3388
- "docs/handoff.md": {
3389
- hash: baselineHash,
3390
- size: stat.size,
3391
- mtimeMs: stat.mtimeMs,
3392
- ctimeMs: stat.ctimeMs,
3393
- remoteEtag: "old-etag",
3394
- syncedAt: new Date(Date.now() - 3600000).toISOString(),
3395
- direction: "down",
3396
- kind: "file",
3397
- },
3398
- },
3399
- });
3400
- }
3401
- if (caseName === "locally modified file") {
3402
- fs.writeFileSync(localPath, "changed body!!");
3403
- }
3404
- else if (caseName === "missing local file") {
3405
- fs.unlinkSync(localPath);
3406
- }
3407
- vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3408
- { key: "docs/handoff.md", size: stat.size, lastModified: new Date(), etag: '"new-etag"' },
3409
- ]);
3410
- await sync({
3411
- company: "acme",
3412
- onConflict: "keep",
3413
- vaultConfig: mockConfig,
3414
- hqRoot: tmpDir,
3415
- pullPrefixes: ["docs/handoff.md"],
3416
- pullContentHashes: { "docs/handoff.md": `sha256:${announcedHash}` },
3417
- skipReindex: true,
3418
- });
3419
- expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalled();
3420
- });
3421
- it("keeps local bytes in place when an unjournaled file already matches the announced content", async () => {
3422
- const companyDocs = path.join(tmpDir, "companies", "acme", "docs");
3423
- fs.mkdirSync(companyDocs, { recursive: true });
3424
- const localPath = path.join(companyDocs, "handoff.md");
3425
- const content = "already has incoming bytes";
3426
- fs.writeFileSync(localPath, content);
3427
- const before = fs.lstatSync(localPath);
3428
- const hash = crypto.createHash("sha256").update(content).digest("hex");
3429
- vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3430
- { key: "docs/handoff.md", size: before.size, lastModified: new Date(), etag: '"incoming-etag"' },
3431
- ]);
3432
- vi.mocked(s3Module.downloadFile).mockImplementationOnce(async (_ctx, _key, probePath, options) => {
3433
- fs.mkdirSync(path.dirname(probePath), { recursive: true });
3434
- options?.beforeReplace?.();
3435
- fs.writeFileSync(probePath, content);
3436
- return { metadata: {} };
3437
- });
3438
- const result = await sync({
3439
- company: "acme",
3440
- onConflict: "keep",
3441
- vaultConfig: mockConfig,
3442
- hqRoot: tmpDir,
3443
- pullPrefixes: ["docs/handoff.md"],
3444
- pullContentHashes: { "docs/handoff.md": `sha256:${hash}` },
3445
- skipReindex: true,
3446
- });
3447
- const after = fs.lstatSync(localPath);
3448
- expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
3449
- expect(fs.readFileSync(localPath, "utf8")).toBe(content);
3450
- expect(after.ctimeMs).toBe(before.ctimeMs);
3451
- expect(result.filesDownloaded).toBe(0);
3452
- expect(result.conflicts).toBe(0);
3453
- });
3454
3408
  it("anti-rollback guard is direction-gated: a SMALLER remote on a clean CONSUMER copy (direction=down) downloads, not conflict", async () => {
3455
3409
  // The device only ever PULLED this file (direction: "down") — a consumer
3456
3410
  // copy with no local authorship to protect. The gardener legitimately