@indigoai-us/hq-cloud 6.14.22 → 6.14.24

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 (120) hide show
  1. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  2. package/dist/bin/sync-runner-company.js +20 -0
  3. package/dist/bin/sync-runner-company.js.map +1 -1
  4. package/dist/bin/sync-runner-events.test.js +22 -0
  5. package/dist/bin/sync-runner-events.test.js.map +1 -1
  6. package/dist/bin/sync-runner-planning.d.ts +11 -0
  7. package/dist/bin/sync-runner-planning.d.ts.map +1 -1
  8. package/dist/bin/sync-runner-planning.js +37 -2
  9. package/dist/bin/sync-runner-planning.js.map +1 -1
  10. package/dist/bin/sync-runner-planning.test.js +1 -0
  11. package/dist/bin/sync-runner-planning.test.js.map +1 -1
  12. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  13. package/dist/bin/sync-runner-watch-loop.js +17 -1
  14. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  15. package/dist/bin/sync-runner.d.ts +33 -1
  16. package/dist/bin/sync-runner.d.ts.map +1 -1
  17. package/dist/bin/sync-runner.js +28 -3
  18. package/dist/bin/sync-runner.js.map +1 -1
  19. package/dist/bin/sync-runner.test.js +206 -2
  20. package/dist/bin/sync-runner.test.js.map +1 -1
  21. package/dist/cli/doctor.d.ts +119 -0
  22. package/dist/cli/doctor.d.ts.map +1 -0
  23. package/dist/cli/doctor.js +485 -0
  24. package/dist/cli/doctor.js.map +1 -0
  25. package/dist/cli/doctor.test.d.ts +13 -0
  26. package/dist/cli/doctor.test.d.ts.map +1 -0
  27. package/dist/cli/doctor.test.js +485 -0
  28. package/dist/cli/doctor.test.js.map +1 -0
  29. package/dist/cli/index.d.ts +2 -0
  30. package/dist/cli/index.d.ts.map +1 -1
  31. package/dist/cli/index.js +4 -0
  32. package/dist/cli/index.js.map +1 -1
  33. package/dist/cli/share.js +6 -1
  34. package/dist/cli/share.js.map +1 -1
  35. package/dist/cli/share.test.js +1 -0
  36. package/dist/cli/share.test.js.map +1 -1
  37. package/dist/cli/sync.d.ts +54 -0
  38. package/dist/cli/sync.d.ts.map +1 -1
  39. package/dist/cli/sync.js +223 -3
  40. package/dist/cli/sync.js.map +1 -1
  41. package/dist/cli/sync.test.js +394 -1
  42. package/dist/cli/sync.test.js.map +1 -1
  43. package/dist/cognito-auth.test.js +7 -0
  44. package/dist/cognito-auth.test.js.map +1 -1
  45. package/dist/index.d.ts +2 -0
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +3 -0
  48. package/dist/index.js.map +1 -1
  49. package/dist/lib/conflict-file.d.ts.map +1 -1
  50. package/dist/lib/conflict-file.js +5 -1
  51. package/dist/lib/conflict-file.js.map +1 -1
  52. package/dist/lib/conflict-index.d.ts +7 -0
  53. package/dist/lib/conflict-index.d.ts.map +1 -1
  54. package/dist/lib/conflict-index.js +10 -2
  55. package/dist/lib/conflict-index.js.map +1 -1
  56. package/dist/lib/conflict.test.js +31 -0
  57. package/dist/lib/conflict.test.js.map +1 -1
  58. package/dist/lib/machine-id.test.js +14 -0
  59. package/dist/lib/machine-id.test.js.map +1 -1
  60. package/dist/local-path-codec.d.ts +18 -0
  61. package/dist/local-path-codec.d.ts.map +1 -1
  62. package/dist/local-path-codec.js +63 -0
  63. package/dist/local-path-codec.js.map +1 -1
  64. package/dist/local-path-codec.test.d.ts +14 -0
  65. package/dist/local-path-codec.test.d.ts.map +1 -0
  66. package/dist/local-path-codec.test.js +102 -0
  67. package/dist/local-path-codec.test.js.map +1 -0
  68. package/dist/machine-auth.test.js +7 -0
  69. package/dist/machine-auth.test.js.map +1 -1
  70. package/dist/s3.d.ts +5 -0
  71. package/dist/s3.d.ts.map +1 -1
  72. package/dist/s3.js +129 -3
  73. package/dist/s3.js.map +1 -1
  74. package/dist/s3.symlink-materialize.test.d.ts +18 -0
  75. package/dist/s3.symlink-materialize.test.d.ts.map +1 -0
  76. package/dist/s3.symlink-materialize.test.js +394 -0
  77. package/dist/s3.symlink-materialize.test.js.map +1 -0
  78. package/dist/s3.test.js +19 -0
  79. package/dist/s3.test.js.map +1 -1
  80. package/dist/skill-telemetry.d.ts.map +1 -1
  81. package/dist/skill-telemetry.js +23 -2
  82. package/dist/skill-telemetry.js.map +1 -1
  83. package/dist/skill-telemetry.test.js +17 -0
  84. package/dist/skill-telemetry.test.js.map +1 -1
  85. package/dist/watcher.d.ts.map +1 -1
  86. package/dist/watcher.js +10 -1
  87. package/dist/watcher.js.map +1 -1
  88. package/dist/watcher.test.js +28 -0
  89. package/dist/watcher.test.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/bin/sync-runner-company.ts +18 -0
  92. package/src/bin/sync-runner-events.test.ts +31 -0
  93. package/src/bin/sync-runner-planning.test.ts +1 -0
  94. package/src/bin/sync-runner-planning.ts +46 -3
  95. package/src/bin/sync-runner-watch-loop.ts +19 -1
  96. package/src/bin/sync-runner.test.ts +236 -1
  97. package/src/bin/sync-runner.ts +57 -2
  98. package/src/cli/doctor.test.ts +581 -0
  99. package/src/cli/doctor.ts +640 -0
  100. package/src/cli/index.ts +14 -0
  101. package/src/cli/share.test.ts +1 -0
  102. package/src/cli/share.ts +6 -1
  103. package/src/cli/sync.test.ts +473 -1
  104. package/src/cli/sync.ts +310 -3
  105. package/src/cognito-auth.test.ts +5 -0
  106. package/src/index.ts +10 -0
  107. package/src/lib/conflict-file.ts +6 -1
  108. package/src/lib/conflict-index.ts +18 -2
  109. package/src/lib/conflict.test.ts +33 -0
  110. package/src/lib/machine-id.test.ts +10 -0
  111. package/src/local-path-codec.test.ts +138 -0
  112. package/src/local-path-codec.ts +66 -0
  113. package/src/machine-auth.test.ts +5 -0
  114. package/src/s3.symlink-materialize.test.ts +492 -0
  115. package/src/s3.test.ts +24 -0
  116. package/src/s3.ts +148 -4
  117. package/src/skill-telemetry.test.ts +19 -0
  118. package/src/skill-telemetry.ts +31 -2
  119. package/src/watcher.test.ts +33 -0
  120. package/src/watcher.ts +10 -1
@@ -42,7 +42,7 @@ vi.mock("../s3.js", async (importOriginal) => {
42
42
  vi.mock("./reindex.js", () => ({
43
43
  reindex: vi.fn(() => ({ status: 0 })),
44
44
  }));
45
- import { sync, reportNewFilesToNotify } from "./sync.js";
45
+ import { sync, reportNewFilesToNotify, win32SymlinkFlavorBroken } from "./sync.js";
46
46
  import { share } from "./share.js";
47
47
  import * as s3Module from "../s3.js";
48
48
  import { reindex } from "./reindex.js";
@@ -3164,6 +3164,177 @@ describe("sync", () => {
3164
3164
  expect(errOutput).toContain("manual");
3165
3165
  expect(errOutput).not.toContain("stale personal-overlay marker");
3166
3166
  });
3167
+ it("directory-overlay marker whose children are already materialized: skips cleanly, no 'rm -rf' advice, children still pull (feedback_d2082110)", async () => {
3168
+ // Reporter feedback_d2082110 (cody@jonesroadbeauty.com): the DPP vault
3169
+ // stored a cloud symlink marker at `skills/cody-copywriter` while the local
3170
+ // path was a real directory holding that overlay's 13 materialized child
3171
+ // objects. The vault stores a directory overlay as a symlink RECORD at the
3172
+ // key itself AND the directory's contents as separate child objects beneath
3173
+ // it. Pre-fix, the pull planner hit the generic dir-vs-object collision
3174
+ // branch and emitted, on EVERY sync cycle, a "manual reconciliation required
3175
+ // (rm -rf the local directory to pull)" warning — advice that here would
3176
+ // DELETE the materialized (and possibly locally-authored) contents to install
3177
+ // an inert link. The marker "never resolved" and the noisy warning recurred.
3178
+ // Fix: recognize that the marker's key ALSO carries child objects in the same
3179
+ // LIST (a real directory never syncs as a single object at its own key), keep
3180
+ // the local directory, skip the marker quietly, and let the children pull via
3181
+ // their own plan items.
3182
+ const markerKey = "skills/cody-copywriter";
3183
+ const childKey = "skills/cody-copywriter/SKILL.md";
3184
+ const localDir = path.join(tmpDir, "companies", "acme", "skills", "cody-copywriter");
3185
+ fs.mkdirSync(localDir, { recursive: true });
3186
+ // A child that is already materialized on this device (stands in for the
3187
+ // reporter's 13 present child files) — it must survive untouched.
3188
+ fs.writeFileSync(path.join(localDir, "materialized.md"), "already here");
3189
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3190
+ { key: markerKey, size: 30, lastModified: new Date(), etag: '"overlay-marker"' },
3191
+ { key: childKey, size: 18, lastModified: new Date(), etag: '"child-obj"' },
3192
+ ]);
3193
+ // Deterministic child materialization, independent of the ambient default
3194
+ // downloadFile mock (which earlier tests in this suite may have replaced).
3195
+ // The marker is skipped by the planner, so this Once impl serves the single
3196
+ // child download.
3197
+ vi.mocked(s3Module.downloadFile).mockImplementationOnce(async (_ctx, _key, localPath) => {
3198
+ const dir = path.dirname(localPath);
3199
+ if (!fs.existsSync(dir))
3200
+ fs.mkdirSync(dir, { recursive: true });
3201
+ fs.writeFileSync(localPath, "child content");
3202
+ return { metadata: {} };
3203
+ });
3204
+ const errSpy = vi.spyOn(console, "error").mockImplementation(() => { });
3205
+ let crashed = false;
3206
+ let result;
3207
+ try {
3208
+ result = await sync({
3209
+ company: "acme",
3210
+ vaultConfig: mockConfig,
3211
+ hqRoot: tmpDir,
3212
+ });
3213
+ }
3214
+ catch {
3215
+ crashed = true;
3216
+ }
3217
+ const errOutput = errSpy.mock.calls.map((c) => c.join(" ")).join("\n");
3218
+ errSpy.mockRestore();
3219
+ expect(crashed).toBe(false);
3220
+ expect(result).toBeDefined();
3221
+ // The marker is a benign skip; the child object still downloads.
3222
+ expect(result.filesDownloaded).toBe(1);
3223
+ expect(result.filesSkipped).toBeGreaterThanOrEqual(1);
3224
+ // Local directory and its already-materialized contents are untouched...
3225
+ expect(fs.readFileSync(path.join(localDir, "materialized.md"), "utf-8")).toBe("already here");
3226
+ // ...and the child materialized under the kept directory.
3227
+ expect(fs.existsSync(path.join(localDir, "SKILL.md"))).toBe(true);
3228
+ // The dangerous, recurring reconciliation advice is GONE...
3229
+ expect(errOutput).not.toContain("rm -rf");
3230
+ expect(errOutput).not.toContain("manual");
3231
+ // ...replaced by a calm, no-action-needed note naming the marker.
3232
+ expect(errOutput).toContain("directory-overlay marker");
3233
+ expect(errOutput).toContain(markerKey);
3234
+ });
3235
+ it("company dir-vs-object collision with NO children in the LIST still warns to reconcile (boundary)", async () => {
3236
+ // Boundary guard for the feedback_d2082110 fix: the quiet overlay-marker
3237
+ // skip is scoped to a remote object whose key ALSO has child objects under
3238
+ // it (a directory overlay). A single remote object at a company key that is
3239
+ // locally a real directory but has NO children in the LIST is a genuine
3240
+ // file-vs-directory structural collision the operator must resolve, so the
3241
+ // original warning + skip-local-only classification must remain intact.
3242
+ const key = "skills/lonely-file";
3243
+ const localDir = path.join(tmpDir, "companies", "acme", "skills", "lonely-file");
3244
+ fs.mkdirSync(localDir, { recursive: true });
3245
+ fs.writeFileSync(path.join(localDir, "inside.md"), "local content");
3246
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3247
+ { key, size: 40, lastModified: new Date(), etag: '"single-obj"' },
3248
+ ]);
3249
+ const errSpy = vi.spyOn(console, "error").mockImplementation(() => { });
3250
+ let crashed = false;
3251
+ let result;
3252
+ try {
3253
+ result = await sync({
3254
+ company: "acme",
3255
+ vaultConfig: mockConfig,
3256
+ hqRoot: tmpDir,
3257
+ });
3258
+ }
3259
+ catch {
3260
+ crashed = true;
3261
+ }
3262
+ const errOutput = errSpy.mock.calls.map((c) => c.join(" ")).join("\n");
3263
+ errSpy.mockRestore();
3264
+ expect(crashed).toBe(false);
3265
+ expect(result.filesDownloaded).toBe(0);
3266
+ expect(result.filesSkipped).toBe(1);
3267
+ expect(fs.existsSync(path.join(localDir, "inside.md"))).toBe(true);
3268
+ // Generic collision path preserved: reconciliation warning still emitted,
3269
+ // NOT the overlay-marker note.
3270
+ expect(errOutput).toContain("manual");
3271
+ expect(errOutput).not.toContain("directory-overlay marker");
3272
+ });
3273
+ it("shared-mode leg surfaces the materialization gap and names the shared→all lever (feedback_d2082110)", async () => {
3274
+ // Reporter feedback_d2082110 (cody@jonesroadbeauty.com): four memberships
3275
+ // were configured as `shared` rather than `all`, so not all vault content
3276
+ // materialized on the second device — a SILENT gap the reporter had to
3277
+ // notice and work around by flipping each membership to `all` by hand.
3278
+ // `SyncResult.filesOutOfScope` counted the skipped keys but nothing SURFACED
3279
+ // the gap or the lever. Fix: a `shared`/`custom` leg that skips one or more
3280
+ // keys as out-of-scope emits a single `scope-materialization-gap` event so
3281
+ // full materialization becomes a deliberate, visible choice.
3282
+ const events = [];
3283
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3284
+ { key: "knowledge/present.md", size: 12, lastModified: new Date(), etag: '"in-scope"' },
3285
+ { key: "projects/secret.md", size: 20, lastModified: new Date(), etag: '"out-of-scope"' },
3286
+ ]);
3287
+ // Deterministic materialization of the single in-scope download, independent
3288
+ // of the ambient default downloadFile mock (earlier tests in this suite may
3289
+ // have replaced it — clearAllMocks resets call history, not implementations).
3290
+ vi.mocked(s3Module.downloadFile).mockImplementationOnce(async (_ctx, _key, localPath) => {
3291
+ const dir = path.dirname(localPath);
3292
+ if (!fs.existsSync(dir))
3293
+ fs.mkdirSync(dir, { recursive: true });
3294
+ fs.writeFileSync(localPath, "in-scope content");
3295
+ return { metadata: {} };
3296
+ });
3297
+ const result = await sync({
3298
+ company: "acme",
3299
+ vaultConfig: mockConfig,
3300
+ hqRoot: tmpDir,
3301
+ syncMode: "shared",
3302
+ prefixSet: ["knowledge/"],
3303
+ onEvent: (e) => events.push(e),
3304
+ });
3305
+ // The in-scope key pulled; the out-of-scope key was NOT materialized.
3306
+ expect(result.filesDownloaded).toBe(1);
3307
+ expect(result.filesOutOfScope).toBe(1);
3308
+ expect(fs.existsSync(path.join(tmpDir, "companies", "acme", "knowledge", "present.md"))).toBe(true);
3309
+ expect(fs.existsSync(path.join(tmpDir, "companies", "acme", "projects", "secret.md"))).toBe(false);
3310
+ // The gap is now SURFACED with the count, the active mode, and the lever.
3311
+ const gap = events.find((e) => e.type === "scope-materialization-gap");
3312
+ expect(gap).toBeDefined();
3313
+ expect(gap.count).toBe(1);
3314
+ expect(gap.syncMode).toBe("shared");
3315
+ expect(gap.samplePaths).toEqual(["projects/secret.md"]);
3316
+ });
3317
+ it("all-mode leg (default) never emits a materialization gap — no scoping, no noise", async () => {
3318
+ // Boundary for the feedback_d2082110 gap surface: in `all` mode every key is
3319
+ // in scope, so the gap event must stay silent even when the LIST is fully
3320
+ // downloaded. Prevents the guidance from firing on the common full-access
3321
+ // path where there is nothing for the operator to act on.
3322
+ const events = [];
3323
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3324
+ { key: "knowledge/a.md", size: 12, lastModified: new Date(), etag: '"a"' },
3325
+ { key: "projects/b.md", size: 20, lastModified: new Date(), etag: '"b"' },
3326
+ ]);
3327
+ const result = await sync({
3328
+ company: "acme",
3329
+ vaultConfig: mockConfig,
3330
+ hqRoot: tmpDir,
3331
+ // syncMode omitted → defaults to "all".
3332
+ onEvent: (e) => events.push(e),
3333
+ });
3334
+ expect(result.filesDownloaded).toBe(2);
3335
+ expect(result.filesOutOfScope).toBe(0);
3336
+ expect(events.find((e) => e.type === "scope-materialization-gap")).toBeUndefined();
3337
+ });
3167
3338
  it("pulls a remote symlink record under an .hqinclude dir-only allowlist pattern", async () => {
3168
3339
  // Codex round-7 P1 follow-up: pre-fix, computePullPlan called
3169
3340
  // shouldSync(localPath) with the default isDir=false. LIST gives
@@ -3703,4 +3874,226 @@ describe("scope-invalid key hardening (companies/ prefix in a company vault —
3703
3874
  expect(fs.existsSync(path.join(tmpDir, "companies", "localco", "notes.md"))).toBe(true);
3704
3875
  });
3705
3876
  });
3877
+ describe("win32SymlinkFlavorBroken (round-8 flavor-health probe)", () => {
3878
+ let tmp;
3879
+ beforeEach(() => {
3880
+ tmp = fs.mkdtempSync(path.join(os.tmpdir(), "flavor-probe-"));
3881
+ });
3882
+ afterEach(() => {
3883
+ fs.rmSync(tmp, { recursive: true, force: true });
3884
+ });
3885
+ it("false for a healthy dir link, false for a dangling target, always false off win32", () => {
3886
+ const target = path.join(tmp, "real-dir");
3887
+ fs.mkdirSync(target);
3888
+ const healthy = path.join(tmp, "healthy-link");
3889
+ fs.symlinkSync(target, healthy, "dir");
3890
+ expect(win32SymlinkFlavorBroken(healthy, true)).toBe(false);
3891
+ expect(win32SymlinkFlavorBroken(healthy, false)).toBe(false);
3892
+ const dangling = path.join(tmp, "dangling-link");
3893
+ fs.symlinkSync(path.join(tmp, "missing"), dangling);
3894
+ expect(win32SymlinkFlavorBroken(dangling, true)).toBe(false);
3895
+ });
3896
+ it.runIf(process.platform === "win32")("true for a real FILE-flavor link at a DIRECTORY target (the broken shape the repair branch re-downloads)", () => {
3897
+ // Only constructible on Windows: flavor is a win32-only concept. The
3898
+ // physical shape from the incident — link minted 'file' while the
3899
+ // target was absent, target directory created afterwards.
3900
+ const target = path.join(tmp, "late-dir");
3901
+ const link = path.join(tmp, "wrong-flavor-link");
3902
+ fs.symlinkSync(target, link, "file");
3903
+ fs.mkdirSync(target);
3904
+ expect(win32SymlinkFlavorBroken(link, true)).toBe(true);
3905
+ });
3906
+ });
3907
+ describe("junk key-spelling guard (US-002 mixed-version amplifier)", () => {
3908
+ let tmpDir;
3909
+ let stateDir;
3910
+ let journalPath;
3911
+ // Journal keys are company-root-relative; sync() resolves the company to
3912
+ // slug "acme", so entries land in sync-journal.acme.json and materialize
3913
+ // under {hqRoot}/companies/acme/.
3914
+ const canonicalKey = ".claude/skills/indigo:call-prep-brief/scripts";
3915
+ const junkKey = ".claude/skills/indigo%3Acall-prep-brief/scripts";
3916
+ function seedJournal(files) {
3917
+ fs.writeFileSync(journalPath, JSON.stringify({
3918
+ version: "2",
3919
+ lastSync: new Date().toISOString(),
3920
+ files,
3921
+ pulls: [],
3922
+ }));
3923
+ }
3924
+ function journalEntry(remoteEtag) {
3925
+ return {
3926
+ hash: "0".repeat(64),
3927
+ size: 0,
3928
+ syncedAt: new Date().toISOString(),
3929
+ direction: "up",
3930
+ remoteEtag,
3931
+ kind: "symlink",
3932
+ };
3933
+ }
3934
+ async function runSync() {
3935
+ const events = [];
3936
+ const result = await sync({
3937
+ company: "acme",
3938
+ vaultConfig: mockConfig,
3939
+ hqRoot: tmpDir,
3940
+ skipReindex: true,
3941
+ onEvent: (e) => events.push(e),
3942
+ });
3943
+ return { events, result };
3944
+ }
3945
+ beforeEach(() => {
3946
+ clearContextCache();
3947
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-sync-junkkey-test-"));
3948
+ stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-state-junkkey-test-"));
3949
+ process.env.HQ_STATE_DIR = stateDir;
3950
+ journalPath = path.join(stateDir, "sync-journal.acme.json");
3951
+ setupFetchMock();
3952
+ });
3953
+ afterEach(() => {
3954
+ vi.unstubAllGlobals();
3955
+ vi.clearAllMocks();
3956
+ fs.rmSync(tmpDir, { recursive: true, force: true });
3957
+ fs.rmSync(stateDir, { recursive: true, force: true });
3958
+ delete process.env.HQ_STATE_DIR;
3959
+ });
3960
+ it("skips a remote %3A twin of an already-journaled canonical key — loudly, never as an error", async () => {
3961
+ seedJournal({ [canonicalKey]: journalEntry("aaa111") });
3962
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3963
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"junk1"' },
3964
+ ]);
3965
+ const { events, result } = await runSync();
3966
+ // The twin never downloads, never materializes, never journals.
3967
+ expect(vi.mocked(s3Module.downloadFile)).not.toHaveBeenCalled();
3968
+ const skips = events.filter((e) => e.type === "skip-junk-key-spelling");
3969
+ expect(skips).toHaveLength(1);
3970
+ expect(skips[0]).toMatchObject({ path: junkKey, journaledKey: canonicalKey });
3971
+ // Policy hq-sync-deliberate-skip-not-fatal-error-exit2: a deliberate
3972
+ // per-file skip must never surface as a fatal {type:"error"} event.
3973
+ expect(events.filter((e) => e.type === "error")).toHaveLength(0);
3974
+ expect(result.aborted).toBe(false);
3975
+ expect(result.filesSkipped).toBe(1);
3976
+ const journalAfter = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
3977
+ expect(Object.keys(journalAfter.files)).not.toContain(junkKey);
3978
+ });
3979
+ it("still pulls a junk-spelling key that is ALREADY journaled under that exact spelling", async () => {
3980
+ // Pre-existing fork (both machines journaled the %3A family before the
3981
+ // guard shipped): the established spelling keeps syncing untouched —
3982
+ // collapsing the family is the vault doctor's job (US-003), not the
3983
+ // planner's.
3984
+ seedJournal({ [junkKey]: journalEntry("aaa111") });
3985
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3986
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"bbb222"' },
3987
+ ]);
3988
+ const { events, result } = await runSync();
3989
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
3990
+ expect(vi.mocked(s3Module.downloadFile).mock.calls[0][1]).toBe(junkKey);
3991
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
3992
+ expect(result.filesDownloaded).toBe(1);
3993
+ });
3994
+ it("downloads a %3A key with NO journaled twin — the guard needs an established spelling to defend", async () => {
3995
+ seedJournal({});
3996
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
3997
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"junk1"' },
3998
+ ]);
3999
+ const { events, result } = await runSync();
4000
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
4001
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
4002
+ expect(result.filesDownloaded).toBe(1);
4003
+ });
4004
+ it("an exact FILE_TOMBSTONE for the journaled canonical key beats the junk-twin protection (authoritative delete propagates)", async () => {
4005
+ // Round-7 review P2: a deliberately-deleted wrapper must not be kept
4006
+ // alive by a stale junk spelling lingering in the LIST.
4007
+ seedJournal({ [canonicalKey]: journalEntry("aaa111") });
4008
+ setupFetchMock({
4009
+ tombstones: [{ key: canonicalKey, deletedAt: new Date().toISOString() }],
4010
+ });
4011
+ // Earlier suites install persistent keyed headRemoteFile implementations;
4012
+ // the HEAD-verify pass must see the canonical key as absent here.
4013
+ vi.mocked(s3Module.headRemoteFile).mockResolvedValue(null);
4014
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4015
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"junk1"' },
4016
+ ]);
4017
+ const { events, result } = await runSync();
4018
+ // Junk twin still gated; the canonical journal entry tombstones out.
4019
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(1);
4020
+ expect(result.filesTombstoned).toBe(1);
4021
+ const journalAfter = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
4022
+ expect(Object.keys(journalAfter.files)).not.toContain(canonicalKey);
4023
+ });
4024
+ it("always downloads a CANONICAL remote key even when the journal holds only its junk twin (post-doctor convergence)", async () => {
4025
+ // Round-6 review P1: after `hq sync doctor` collapses the vault to the
4026
+ // canonical spelling, a machine whose journal still carries the junk
4027
+ // spelling must pick the canonical object up — gating it would deadlock
4028
+ // that machine out of the wrapper (and all future updates) forever.
4029
+ seedJournal({ [junkKey]: journalEntry("aaa111") });
4030
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4031
+ { key: canonicalKey, size: 20, lastModified: new Date(), etag: '"can1"' },
4032
+ ]);
4033
+ const { events, result } = await runSync();
4034
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
4035
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
4036
+ expect(vi.mocked(s3Module.downloadFile).mock.calls[0][1]).toBe(canonicalKey);
4037
+ expect(result.filesDownloaded).toBe(1);
4038
+ const journalAfter = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
4039
+ expect(Object.keys(journalAfter.files)).toContain(canonicalKey);
4040
+ });
4041
+ it("does not tombstone a journaled canonical key when the LIST holds only its junk twin", async () => {
4042
+ // Round-4 review P1: skip-junk-key-spelling alone is self-defeating if
4043
+ // the delete-resync pass then classes the canonical journal entry as
4044
+ // remote-deleted (raw remoteKeySet miss) — it would unlink the good
4045
+ // local wrapper and drop the journal entry, and the NEXT pull would
4046
+ // materialize the junk spelling the guard just refused.
4047
+ seedJournal({ [canonicalKey]: journalEntry("aaa111") });
4048
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4049
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"junk1"' },
4050
+ ]);
4051
+ const { events, result } = await runSync();
4052
+ // Junk twin skipped, canonical entry NOT tombstoned.
4053
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(1);
4054
+ const journalAfter = JSON.parse(fs.readFileSync(journalPath, "utf-8"));
4055
+ expect(Object.keys(journalAfter.files)).toContain(canonicalKey);
4056
+ expect(result.filesTombstoned ?? 0).toBe(0);
4057
+ expect(events.filter((e) => e.type === "error")).toHaveLength(0);
4058
+ });
4059
+ it("never gates COLON-FREE skill names — foo%20bar and foo bar are distinct skills (round-9)", async () => {
4060
+ seedJournal({ [".claude/skills/foo bar/SKILL.md"]: journalEntry("aaa111") });
4061
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4062
+ { key: ".claude/skills/foo%20bar/SKILL.md", size: 20, lastModified: new Date(), etag: '"pct1"' },
4063
+ ]);
4064
+ const { events, result } = await runSync();
4065
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
4066
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
4067
+ expect(result.filesDownloaded).toBe(1);
4068
+ });
4069
+ it("never gates keys outside .claude/skills/ — percent-decode collisions there are distinct files", async () => {
4070
+ // `notes/a b.md` and `notes/a%20b.md` canonicalize to the same fixpoint
4071
+ // string, but outside the skill farm they are two legitimate files (the
4072
+ // win32 codec encodes the literal '%' as '%25' locally). The guard must
4073
+ // not skip the unjournaled one.
4074
+ seedJournal({ ["notes/a b.md"]: journalEntry("aaa111") });
4075
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4076
+ { key: "notes/a%20b.md", size: 20, lastModified: new Date(), etag: '"pct1"' },
4077
+ ]);
4078
+ const { events, result } = await runSync();
4079
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(1);
4080
+ expect(vi.mocked(s3Module.downloadFile).mock.calls[0][1]).toBe("notes/a%20b.md");
4081
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
4082
+ expect(result.filesDownloaded).toBe(1);
4083
+ });
4084
+ it("keeps syncing BOTH spellings when both are already journaled (no retroactive gating)", async () => {
4085
+ seedJournal({
4086
+ [canonicalKey]: journalEntry("aaa111"),
4087
+ [junkKey]: journalEntry("ccc333"),
4088
+ });
4089
+ vi.mocked(s3Module.listRemoteFiles).mockResolvedValueOnce([
4090
+ { key: canonicalKey, size: 20, lastModified: new Date(), etag: '"bbb222"' },
4091
+ { key: junkKey, size: 20, lastModified: new Date(), etag: '"ddd444"' },
4092
+ ]);
4093
+ const { events, result } = await runSync();
4094
+ expect(vi.mocked(s3Module.downloadFile)).toHaveBeenCalledTimes(2);
4095
+ expect(events.filter((e) => e.type === "skip-junk-key-spelling")).toHaveLength(0);
4096
+ expect(result.filesDownloaded).toBe(2);
4097
+ });
4098
+ });
3706
4099
  //# sourceMappingURL=sync.test.js.map