@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
@@ -0,0 +1,640 @@
1
+ /**
2
+ * `hq sync doctor` — bounded skill-key dedupe + local farm GC (US-003,
3
+ * hq-sync-windows-skill-symlink-collision).
4
+ *
5
+ * The mixed-version amplifier (see `canonicalVaultKeySpelling` in
6
+ * local-path-codec.ts) leaves the vault holding several percent-encoded
7
+ * GENERATIONS of every `.claude/skills/<ns>:<skill>` wrapper key (`:`, `%3A`,
8
+ * `%253A`, `%25253A`, …) and the local farm holding junk-named wrapper dirs.
9
+ * US-001/US-002 stop NEW generations from being minted; this doctor is the
10
+ * one-shot reconcile that collapses the families already in the wild:
11
+ *
12
+ * 1. Vault dedupe — list keys under `.claude/skills/`, group them by their
13
+ * decode-to-fixpoint canonical form, let the newest-bodied spelling win,
14
+ * (re)write the canonical `:` key when it is missing or stale, and delete
15
+ * every non-canonical spelling. Deletion is gated hard: only bodies that
16
+ * start with the `hq-symlink:` marker, or that are byte-identical to the
17
+ * canonical twin, are auto-deleted — anything else is surfaced for manual
18
+ * review, never silently removed.
19
+ * 2. Local farm GC — remove `.claude/skills/` entries whose names decode to
20
+ * a canonical wrapper that already exists on disk and whose content is
21
+ * exclusively symlink-farm material. A real file anywhere is a refusal.
22
+ *
23
+ * Dry-run is the DEFAULT: the full keep/rewrite/tombstone/rm plan is printed
24
+ * (counts + every key/path) and nothing is written until `--yes`
25
+ * (`options.yes`). Remote deletes respect the sync-delete-bulk-asymmetry-guard
26
+ * policy — same thresholds, env override, and refusal semantics as
27
+ * `computeDeletePlan` in share.ts. Per-item failures are loud warnings, never
28
+ * process-fatal (hq-sync-deliberate-skip-not-fatal-error-exit2).
29
+ */
30
+
31
+ import * as fs from "fs";
32
+ import * as path from "path";
33
+ import { resolveEntityContext } from "../context.js";
34
+ import { resolveObjectIO, type ObjectIO } from "../object-io.js";
35
+ import type { ListedRemoteObject } from "../object-io.js";
36
+ import { SYMLINK_BODY_PREFIX } from "../s3.js";
37
+ import {
38
+ canonicalVaultKeySpelling,
39
+ canonicalVaultSegmentSpelling,
40
+ encodeLocalVaultSegment,
41
+ } from "../local-path-codec.js";
42
+ import { withOperationLock } from "../operation-lock.js";
43
+ import type { VaultServiceConfig } from "../types.js";
44
+
45
+ /** Vault prefix the doctor is bounded to — the skill wrapper link farm. */
46
+ export const SKILLS_KEY_PREFIX = ".claude/skills/";
47
+
48
+ /**
49
+ * Bulk-asymmetry circuit-breaker, per the sync-delete-bulk-asymmetry-guard
50
+ * policy: any new delete-propagation site must ship the guard inline with the
51
+ * SAME threshold defaults and env-var name as the reference implementation
52
+ * (share.ts `BULK_ASYMMETRY_*` + `isBulkAsymmetryOverride`). Here the
53
+ * numerator is the planned tombstone count and the denominator is every
54
+ * in-scope `.claude/skills/` key — a dedupe pass that wants to delete a large
55
+ * slice of the farm at once must be explicitly confirmed, because that shape
56
+ * is indistinguishable from a planner bug eating the vault.
57
+ */
58
+ const BULK_ASYMMETRY_RATIO = 0.10;
59
+ const BULK_ASYMMETRY_MIN_ABS = 10;
60
+
61
+ function isBulkDeleteOverride(explicit: boolean | undefined): boolean {
62
+ if (explicit) return true;
63
+ const v = (process.env.HQ_SYNC_DELETE_BULK_OVERRIDE ?? "").toLowerCase();
64
+ return v === "1" || v === "true" || v === "yes";
65
+ }
66
+
67
+ /**
68
+ * The narrow object-store seam the doctor needs. Structurally satisfied by
69
+ * every `ObjectIO` transport; tests inject an in-memory fake (the same
70
+ * dependency-injection convention the s3.ts suites use via
71
+ * `setObjectIOFactory`).
72
+ */
73
+ export type DoctorStore = Pick<
74
+ ObjectIO,
75
+ "listObjects" | "getObject" | "putObject" | "deleteObject"
76
+ >;
77
+
78
+ export interface SyncDoctorOptions {
79
+ /** Entity whose vault to reconcile (prs_/agt_ uid, cmp_ uid, or slug). */
80
+ entity: string;
81
+ /** Vault service config (auth + region), as for `sync()`/`share()`. */
82
+ vaultConfig: VaultServiceConfig;
83
+ /** HQ root whose local `.claude/skills/` farm the GC pass walks. */
84
+ hqRoot: string;
85
+ /** Apply the plan. Default false = dry-run: print the plan, write nothing. */
86
+ yes?: boolean;
87
+ /**
88
+ * Explicit bulk-delete override (the `--force-bulk` CLI flag). Equivalent to
89
+ * `HQ_SYNC_DELETE_BULK_OVERRIDE=1`; without either, a tombstone plan that
90
+ * trips the asymmetry thresholds is refused (writes and local GC still run).
91
+ */
92
+ bulkDeleteOverride?: boolean;
93
+ /** DI seam for tests; defaults to the entity's resolved ObjectIO. */
94
+ store?: DoctorStore;
95
+ /**
96
+ * Platform override for local farm-name encoding (local-path-codec
97
+ * convention). Decode-to-fixpoint always uses win32 semantics — junk
98
+ * spellings are a key-space phenomenon, not a host one — but the canonical
99
+ * LOCAL wrapper name a junk dir must collapse into is host-specific
100
+ * (`ns%3Askill` on win32, `ns:skill` on POSIX).
101
+ */
102
+ win32?: boolean;
103
+ /**
104
+ * The doctor takes the same per-root operation lock as `sync()`. Set when a
105
+ * caller (runner/daemon) already holds it for this root.
106
+ */
107
+ operationLockAlreadyHeld?: boolean;
108
+ }
109
+
110
+ /** One canonical key the doctor will (re)write, and the spelling it copies. */
111
+ export interface SkillKeyRewrite {
112
+ key: string;
113
+ /** Winning (newest-bodied) spelling whose body becomes the canonical body. */
114
+ fromSpelling: string;
115
+ }
116
+
117
+ export interface DoctorManualReviewItem {
118
+ key: string;
119
+ reason: string;
120
+ }
121
+
122
+ export interface LocalGcManualReviewItem {
123
+ path: string;
124
+ reason: string;
125
+ }
126
+
127
+ export interface SyncDoctorPlan {
128
+ /** Every listed `.claude/skills/` key (the bulk-guard denominator). */
129
+ inScopeKeys: number;
130
+ /** Canonical-spelling keys that need no action. */
131
+ keep: string[];
132
+ /** Canonical keys to (re)write from their winning spelling's body. */
133
+ rewrites: SkillKeyRewrite[];
134
+ /** Non-canonical spellings safe to delete (marker body or byte-identical). */
135
+ tombstones: string[];
136
+ /** Keys the safety gate refused to auto-delete — surfaced, never removed. */
137
+ manualReview: DoctorManualReviewItem[];
138
+ /** Junk-named local farm entries (symlink-farm content only) to remove. */
139
+ localRemovals: string[];
140
+ /** Junk-named local entries the GC refused to touch — surfaced only. */
141
+ localManualReview: LocalGcManualReviewItem[];
142
+ /** True when the tombstone plan tripped the bulk-asymmetry breaker. */
143
+ bulkRefused: boolean;
144
+ }
145
+
146
+ export interface SyncDoctorResult {
147
+ plan: SyncDoctorPlan;
148
+ /** False on dry-run (the default) and when `--yes` was absent. */
149
+ applied: boolean;
150
+ keysRewritten: number;
151
+ keysDeleted: number;
152
+ dirsRemoved: number;
153
+ /** Loud non-fatal warnings emitted during plan + apply. */
154
+ warnings: number;
155
+ }
156
+
157
+ /** A group member whose body was fetched for winner selection / gating. */
158
+ interface FetchedSpelling {
159
+ key: string;
160
+ lastModified: Date;
161
+ body: Buffer;
162
+ metadata?: Record<string, string>;
163
+ }
164
+
165
+ /** Payload for one canonical-key rewrite, staged at plan time. */
166
+ interface RewritePayload {
167
+ key: string;
168
+ body: Buffer;
169
+ metadata?: Record<string, string>;
170
+ /** CAS fence: canonical key was listed with this ETag. */
171
+ ifMatch?: string;
172
+ /** CAS fence: canonical key was absent at list time. */
173
+ ifNoneMatch?: "*";
174
+ }
175
+
176
+ function isSymlinkMarkerBody(body: Buffer): boolean {
177
+ return (
178
+ body.subarray(0, SYMLINK_BODY_PREFIX.length).toString("utf-8") ===
179
+ SYMLINK_BODY_PREFIX
180
+ );
181
+ }
182
+
183
+ /** Exhaustively page the skills prefix (the transport caps pages at ~1000). */
184
+ async function listSkillObjects(
185
+ store: DoctorStore,
186
+ ): Promise<ListedRemoteObject[]> {
187
+ const objects: ListedRemoteObject[] = [];
188
+ let continuationToken: string | undefined;
189
+ do {
190
+ const page = await store.listObjects({
191
+ prefix: SKILLS_KEY_PREFIX,
192
+ continuationToken,
193
+ });
194
+ objects.push(...page.objects);
195
+ continuationToken = page.nextContinuationToken;
196
+ } while (continuationToken);
197
+ return objects;
198
+ }
199
+
200
+ /**
201
+ * Winner ordering: newest lastModified first ("newest-bodied spelling wins");
202
+ * ties prefer the canonical spelling, then the lexicographically smallest key
203
+ * so the plan is deterministic under equal timestamps.
204
+ */
205
+ function pickWinner(
206
+ members: FetchedSpelling[],
207
+ canonicalKey: string,
208
+ ): FetchedSpelling {
209
+ return members.reduce((best, m) => {
210
+ if (m.lastModified.getTime() !== best.lastModified.getTime()) {
211
+ return m.lastModified.getTime() > best.lastModified.getTime() ? m : best;
212
+ }
213
+ if ((m.key === canonicalKey) !== (best.key === canonicalKey)) {
214
+ return m.key === canonicalKey ? m : best;
215
+ }
216
+ return m.key < best.key ? m : best;
217
+ });
218
+ }
219
+
220
+ /**
221
+ * True when every entry under `dir` (recursively) is symlink-farm material:
222
+ * symlinks, or directories that themselves contain only symlink-farm
223
+ * material. Any regular file — or anything that is neither a symlink nor a
224
+ * directory (fifo, socket) — makes the tree untouchable.
225
+ */
226
+ function containsOnlySymlinkFarmContent(dir: string): boolean {
227
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
228
+ for (const entry of entries) {
229
+ if (entry.isSymbolicLink()) continue;
230
+ if (entry.isDirectory()) {
231
+ if (!containsOnlySymlinkFarmContent(path.join(dir, entry.name))) {
232
+ return false;
233
+ }
234
+ continue;
235
+ }
236
+ return false;
237
+ }
238
+ return true;
239
+ }
240
+
241
+ /** lstat-based existence probe — a broken symlink wrapper still "exists". */
242
+ function entryExists(p: string): boolean {
243
+ try {
244
+ fs.lstatSync(p);
245
+ return true;
246
+ } catch {
247
+ return false;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Plan the local `.claude/skills/` farm GC. Pure planning — no removal here.
253
+ * An entry qualifies when its name percent-decodes (to fixpoint) to a
254
+ * canonical wrapper name DIFFERENT from its own spelling, that canonical
255
+ * wrapper already exists on disk, and the entry is a symlink or a directory
256
+ * of exclusively symlink-farm content. Everything else that is junk-named is
257
+ * listed for manual review with the reason it was refused.
258
+ */
259
+ function planLocalSkillFarmGc(
260
+ skillsDir: string,
261
+ win32: boolean,
262
+ warn: (message: string) => void,
263
+ ): { removals: string[]; manualReview: LocalGcManualReviewItem[] } {
264
+ const removals: string[] = [];
265
+ const manualReview: LocalGcManualReviewItem[] = [];
266
+ if (!entryExists(skillsDir)) return { removals, manualReview };
267
+
268
+ let entries: fs.Dirent[];
269
+ try {
270
+ entries = fs.readdirSync(skillsDir, { withFileTypes: true });
271
+ } catch (err) {
272
+ warn(`local GC skipped — cannot read ${skillsDir}: ${String(err)}`);
273
+ return { removals, manualReview };
274
+ }
275
+
276
+ for (const entry of entries) {
277
+ // Fixpoint-decode the name (win32 key-space semantics unconditionally —
278
+ // a mac's literal `ns%3Askill` junk dir is junk too), then re-encode for
279
+ // THIS host to get the canonical local wrapper spelling.
280
+ const canonicalName = canonicalVaultSegmentSpelling(entry.name);
281
+ // A no-op canonicalization means this name is NOT an amplifier
282
+ // generation: generated wrapper generations always decode back to a
283
+ // `<ns>:<skill>` name, and canonicalVaultSegmentSpelling deliberately
284
+ // returns colon-free names unchanged (`foo%25bar` is a legitimate
285
+ // wrapper for the canonical skill name `foo%bar`, distinct from a
286
+ // literal `foo%bar` dir). Encoding such a name for the host would
287
+ // re-escape it into a different spelling (`foo%2525bar` on win32) and,
288
+ // if that double-encoded sibling happens to exist, `--yes` would delete
289
+ // the legitimate wrapper. Bail before the host encode — the amplifier
290
+ // family this GC exists to collapse always changes spelling here.
291
+ if (canonicalName === entry.name) continue;
292
+ const canonicalLocalName = encodeLocalVaultSegment(canonicalName, win32);
293
+ if (entry.name === canonicalLocalName) continue; // already canonical
294
+
295
+ const fullPath = path.join(skillsDir, entry.name);
296
+ if (!entryExists(path.join(skillsDir, canonicalLocalName))) {
297
+ manualReview.push({
298
+ path: fullPath,
299
+ reason: `canonical wrapper '${canonicalLocalName}' missing on disk — junk-named entry may be the only copy`,
300
+ });
301
+ continue;
302
+ }
303
+ if (entry.isSymbolicLink()) {
304
+ removals.push(fullPath);
305
+ continue;
306
+ }
307
+ if (entry.isDirectory()) {
308
+ let farmOnly = false;
309
+ try {
310
+ farmOnly = containsOnlySymlinkFarmContent(fullPath);
311
+ } catch (err) {
312
+ warn(`local GC cannot inspect ${fullPath}: ${String(err)}`);
313
+ manualReview.push({ path: fullPath, reason: "unreadable — not inspected" });
314
+ continue;
315
+ }
316
+ if (farmOnly) {
317
+ removals.push(fullPath);
318
+ } else {
319
+ manualReview.push({
320
+ path: fullPath,
321
+ reason: "contains real (non-symlink) files — never auto-deleted",
322
+ });
323
+ }
324
+ continue;
325
+ }
326
+ manualReview.push({
327
+ path: fullPath,
328
+ reason: "junk-named regular file — never auto-deleted",
329
+ });
330
+ }
331
+ return { removals, manualReview };
332
+ }
333
+
334
+ /** Remove one farm entry: unlink symlinks (rmdir for win32 dir links), rm dirs. */
335
+ function removeFarmEntry(fullPath: string): void {
336
+ const st = fs.lstatSync(fullPath);
337
+ if (st.isSymbolicLink()) {
338
+ try {
339
+ fs.unlinkSync(fullPath);
340
+ } catch (err) {
341
+ const code = (err as NodeJS.ErrnoException).code;
342
+ // A Windows directory symlink is a directory reparse point; unlink()
343
+ // refuses it (EPERM/EISDIR) and rmdir removes the LINK, not the target.
344
+ if (code === "EPERM" || code === "EISDIR") {
345
+ fs.rmdirSync(fullPath);
346
+ } else {
347
+ throw err;
348
+ }
349
+ }
350
+ return;
351
+ }
352
+ fs.rmSync(fullPath, { recursive: true });
353
+ }
354
+
355
+ /**
356
+ * Run the doctor: build the dedupe + GC plan, print it in full, and — only
357
+ * with `yes: true` — apply it. See the module doc for semantics.
358
+ */
359
+ export async function syncDoctor(
360
+ options: SyncDoctorOptions,
361
+ ): Promise<SyncDoctorResult> {
362
+ if (options.operationLockAlreadyHeld) {
363
+ return syncDoctorWithOperationLockHeld(options);
364
+ }
365
+ return withOperationLock(options.hqRoot, "sync-doctor", () =>
366
+ syncDoctorWithOperationLockHeld(options),
367
+ );
368
+ }
369
+
370
+ async function syncDoctorWithOperationLockHeld(
371
+ options: SyncDoctorOptions,
372
+ ): Promise<SyncDoctorResult> {
373
+ let warnings = 0;
374
+ const warn = (message: string): void => {
375
+ warnings++;
376
+ console.error(` ! ${message}`);
377
+ };
378
+
379
+ const win32 = options.win32 ?? process.platform === "win32";
380
+ const store =
381
+ options.store ??
382
+ resolveObjectIO(
383
+ await resolveEntityContext(options.entity, options.vaultConfig),
384
+ );
385
+
386
+ // ── Vault leg: plan ────────────────────────────────────────────────────────
387
+ const listed = await listSkillObjects(store);
388
+ const groups = new Map<string, ListedRemoteObject[]>();
389
+ for (const obj of listed) {
390
+ const canonicalKey = canonicalVaultKeySpelling(obj.key);
391
+ const members = groups.get(canonicalKey);
392
+ if (members) {
393
+ members.push(obj);
394
+ } else {
395
+ groups.set(canonicalKey, [obj]);
396
+ }
397
+ }
398
+
399
+ const keep: string[] = [];
400
+ const rewrites: SkillKeyRewrite[] = [];
401
+ const rewritePayloads: RewritePayload[] = [];
402
+ const tombstones: string[] = [];
403
+ const manualReview: DoctorManualReviewItem[] = [];
404
+
405
+ for (const [canonicalKey, members] of groups) {
406
+ // Fast path: one spelling and it is already canonical — nothing to do,
407
+ // and no body fetch (the common case must stay O(list), not O(get)).
408
+ if (members.length === 1 && members[0]!.key === canonicalKey) {
409
+ keep.push(canonicalKey);
410
+ continue;
411
+ }
412
+
413
+ // Actionable family: fetch every member's body. A fetch failure demotes
414
+ // that spelling to manual review (unknown bytes are never deleted) and
415
+ // never aborts the run.
416
+ const fetched: FetchedSpelling[] = [];
417
+ for (const member of members) {
418
+ try {
419
+ const res = await store.getObject(member.key);
420
+ fetched.push({
421
+ key: member.key,
422
+ lastModified: member.lastModified,
423
+ body: res.body,
424
+ metadata: res.metadata,
425
+ });
426
+ } catch (err) {
427
+ warn(`body fetch failed for ${member.key}: ${String(err)} — left for manual review`);
428
+ manualReview.push({
429
+ key: member.key,
430
+ reason: "body fetch failed — bytes unknown, not touched",
431
+ });
432
+ }
433
+ }
434
+ if (fetched.length === 0) continue; // every member failed; all warned above
435
+
436
+ // A canonical spelling that is LISTED but whose body fetch failed means
437
+ // the canonical bytes are unknown — rewriting from a junk sibling could
438
+ // overwrite a non-marker object behind a transient read fault. Park the
439
+ // whole family: no rewrite, no deletes; re-run once the read recovers.
440
+ const canonicalListed = members.some((m) => m.key === canonicalKey);
441
+ const canonicalMember = fetched.find((m) => m.key === canonicalKey);
442
+ if (canonicalListed && canonicalMember === undefined) {
443
+ for (const member of fetched) {
444
+ manualReview.push({
445
+ key: member.key,
446
+ reason: "canonical body unknown (fetch failed) — family not touched",
447
+ });
448
+ }
449
+ continue;
450
+ }
451
+
452
+ const winner = pickWinner(fetched, canonicalKey);
453
+
454
+ // The body the canonical key will hold after this pass: the newest-bodied
455
+ // spelling's. (Re)write when the canonical key is missing entirely or its
456
+ // bytes are stale relative to the winner.
457
+ const finalBody = winner.body;
458
+ if (!canonicalMember || !canonicalMember.body.equals(finalBody)) {
459
+ rewrites.push({ key: canonicalKey, fromSpelling: winner.key });
460
+ // CAS fence (same contract as the push path): if the canonical key was
461
+ // listed, the PUT lands only while its ETag is unchanged; if it was
462
+ // absent, only while it is still absent. A concurrent upload from
463
+ // another machine turns the rewrite into a 412 warning (and the
464
+ // failed-rewrite guard below keeps the family's tombstones), never a
465
+ // clobber of bytes this run has not seen.
466
+ const listedCanonical = members.find((m) => m.key === canonicalKey);
467
+ rewritePayloads.push({
468
+ key: canonicalKey,
469
+ body: finalBody,
470
+ metadata: winner.metadata,
471
+ ...(listedCanonical
472
+ ? { ifMatch: listedCanonical.etag }
473
+ : { ifNoneMatch: "*" as const }),
474
+ });
475
+ } else {
476
+ keep.push(canonicalKey);
477
+ }
478
+
479
+ // Safety gate, per spelling: auto-delete ONLY symlink-marker bodies or
480
+ // bodies byte-identical to the canonical twin's final content.
481
+ for (const member of fetched) {
482
+ if (member.key === canonicalKey) continue;
483
+ if (isSymlinkMarkerBody(member.body) || member.body.equals(finalBody)) {
484
+ tombstones.push(member.key);
485
+ } else {
486
+ manualReview.push({
487
+ key: member.key,
488
+ reason: "body is neither a symlink marker nor byte-identical to the canonical key — not auto-deleted",
489
+ });
490
+ }
491
+ }
492
+ }
493
+
494
+ // ── Bulk-asymmetry decision (policy: sync-delete-bulk-asymmetry-guard) ────
495
+ const bulkRefused =
496
+ !isBulkDeleteOverride(options.bulkDeleteOverride) &&
497
+ tombstones.length >= BULK_ASYMMETRY_MIN_ABS &&
498
+ listed.length > 0 &&
499
+ tombstones.length / listed.length >= BULK_ASYMMETRY_RATIO;
500
+
501
+ // ── Local leg: plan ───────────────────────────────────────────────────────
502
+ const skillsDir = path.join(options.hqRoot, ".claude", "skills");
503
+ const localGc = planLocalSkillFarmGc(skillsDir, win32, warn);
504
+
505
+ const plan: SyncDoctorPlan = {
506
+ inScopeKeys: listed.length,
507
+ keep,
508
+ rewrites,
509
+ tombstones,
510
+ manualReview,
511
+ localRemovals: localGc.removals,
512
+ localManualReview: localGc.manualReview,
513
+ bulkRefused,
514
+ };
515
+ printPlan(plan);
516
+
517
+ const result: SyncDoctorResult = {
518
+ plan,
519
+ applied: false,
520
+ keysRewritten: 0,
521
+ keysDeleted: 0,
522
+ dirsRemoved: 0,
523
+ warnings,
524
+ };
525
+
526
+ // ── Dry-run is the default: full plan printed, nothing written. ───────────
527
+ if (!options.yes) {
528
+ console.log("\nDry-run (default): nothing written. Re-run with --yes to apply.");
529
+ result.warnings = warnings;
530
+ return result;
531
+ }
532
+
533
+ // ── Apply: canonical rewrites first (additive), then gated deletes, then
534
+ // local GC. Per-item failures warn and continue — never fatal. ─────────
535
+ // Canonical keys whose rewrite failed: their families' tombstones must NOT
536
+ // be deleted below — when the canonical body was missing or stale, the junk
537
+ // spelling may hold the newest (or only) remote copy, and deleting it after
538
+ // a failed rewrite would drop that body permanently.
539
+ const failedRewriteCanonicals = new Set<string>();
540
+ for (const payload of rewritePayloads) {
541
+ try {
542
+ await store.putObject({
543
+ key: payload.key,
544
+ body: payload.body,
545
+ // Content-type is not consulted by the engine's read path; marker
546
+ // records upload as octet-stream (s3.ts uploadSymlink convention).
547
+ contentType: "application/octet-stream",
548
+ metadata: payload.metadata,
549
+ ifMatch: payload.ifMatch,
550
+ ifNoneMatch: payload.ifNoneMatch,
551
+ });
552
+ result.keysRewritten++;
553
+ } catch (err) {
554
+ failedRewriteCanonicals.add(payload.key);
555
+ warn(`canonical rewrite failed for ${payload.key}: ${String(err)}`);
556
+ }
557
+ }
558
+
559
+ if (bulkRefused) {
560
+ // Refusal is event-only (policy req #4): nothing deleted, nothing
561
+ // journaled away — re-run with the override once the plan is reviewed.
562
+ console.error(
563
+ ` ! bulk-asymmetry circuit-breaker tripped: ${tombstones.length}/${listed.length} in-scope skill keys planned for deletion.\n` +
564
+ ` Refusing the delete leg — review the plan above, then re-run with --force-bulk (or HQ_SYNC_DELETE_BULK_OVERRIDE=1) to apply it.`,
565
+ );
566
+ } else {
567
+ for (const key of tombstones) {
568
+ if (failedRewriteCanonicals.has(canonicalVaultKeySpelling(key))) {
569
+ warn(
570
+ `skipping delete of ${key} — its canonical rewrite failed; re-run the doctor once the write path recovers`,
571
+ );
572
+ continue;
573
+ }
574
+ try {
575
+ await store.deleteObject(key);
576
+ result.keysDeleted++;
577
+ } catch (err) {
578
+ warn(`delete failed for ${key}: ${String(err)}`);
579
+ }
580
+ }
581
+ }
582
+
583
+ for (const fullPath of plan.localRemovals) {
584
+ try {
585
+ // Re-verify at apply time: the entry may have changed between plan and
586
+ // apply (sync pass, user edit). Positive check — remove ONLY what is
587
+ // still a symlink or a farm-only directory; anything else (a regular
588
+ // file that replaced the planned entry, a dir that grew real files) is
589
+ // refused rather than trusted from the plan.
590
+ const st = fs.lstatSync(fullPath);
591
+ const stillFarmEntry =
592
+ st.isSymbolicLink() ||
593
+ (st.isDirectory() && containsOnlySymlinkFarmContent(fullPath));
594
+ if (!stillFarmEntry) {
595
+ warn(`skipped ${fullPath} — no longer symlink-farm content (changed since planning)`);
596
+ continue;
597
+ }
598
+ removeFarmEntry(fullPath);
599
+ result.dirsRemoved++;
600
+ } catch (err) {
601
+ warn(`local removal failed for ${fullPath}: ${String(err)}`);
602
+ }
603
+ }
604
+
605
+ result.applied = true;
606
+ result.warnings = warnings;
607
+ console.log(
608
+ `\nApplied: ${result.keysRewritten} canonical key(s) written, ${result.keysDeleted} spelling(s) deleted, ${result.dirsRemoved} local entr(ies) removed.`,
609
+ );
610
+ return result;
611
+ }
612
+
613
+ /** Print the full plan: counts plus every key/path in each bucket. */
614
+ function printPlan(plan: SyncDoctorPlan): void {
615
+ console.log(`hq sync doctor — skill-key dedupe plan (${SKILLS_KEY_PREFIX})`);
616
+ console.log(` in-scope keys: ${plan.inScopeKeys}`);
617
+ console.log(` keep (canonical, no action): ${plan.keep.length}`);
618
+ for (const key of plan.keep) console.log(` = ${key}`);
619
+ console.log(` rewrite canonical: ${plan.rewrites.length}`);
620
+ for (const r of plan.rewrites) {
621
+ console.log(` + ${r.key} (body from ${r.fromSpelling})`);
622
+ }
623
+ console.log(` tombstone (delete non-canonical spelling): ${plan.tombstones.length}`);
624
+ for (const key of plan.tombstones) console.log(` - ${key}`);
625
+ console.log(` manual review (NOT deleted): ${plan.manualReview.length}`);
626
+ for (const item of plan.manualReview) {
627
+ console.log(` ? ${item.key} — ${item.reason}`);
628
+ }
629
+ console.log(` local farm GC — remove: ${plan.localRemovals.length}`);
630
+ for (const p of plan.localRemovals) console.log(` - ${p}`);
631
+ console.log(` local farm GC — manual review: ${plan.localManualReview.length}`);
632
+ for (const item of plan.localManualReview) {
633
+ console.log(` ? ${item.path} — ${item.reason}`);
634
+ }
635
+ if (plan.bulkRefused) {
636
+ console.log(
637
+ ` bulk-asymmetry: delete leg WOULD BE REFUSED (${plan.tombstones.length}/${plan.inScopeKeys} keys; override with --force-bulk or HQ_SYNC_DELETE_BULK_OVERRIDE=1)`,
638
+ );
639
+ }
640
+ }
package/src/cli/index.ts CHANGED
@@ -33,3 +33,17 @@ export type { ReindexOptions, ReindexResult } from "./reindex.js";
33
33
  // menubar app). Now shared between the app and `hq rescue`.
34
34
  export { rescue, buildRescueArgs } from "./rescue.js";
35
35
  export type { RescueOptions, RescueResult } from "./rescue.js";
36
+
37
+ // Bounded skill-key dedupe + local `.claude/skills/` farm GC for the
38
+ // mixed-version amplifier (`hq sync doctor`, US-003
39
+ // hq-sync-windows-skill-symlink-collision). Dry-run by default; --yes applies.
40
+ export { syncDoctor, SKILLS_KEY_PREFIX } from "./doctor.js";
41
+ export type {
42
+ SyncDoctorOptions,
43
+ SyncDoctorResult,
44
+ SyncDoctorPlan,
45
+ SkillKeyRewrite,
46
+ DoctorManualReviewItem,
47
+ LocalGcManualReviewItem,
48
+ DoctorStore,
49
+ } from "./doctor.js";
@@ -1673,6 +1673,7 @@ describe("share", () => {
1673
1673
  e.type === "new-files" ||
1674
1674
  e.type === "personal-vault-out-of-policy" ||
1675
1675
  e.type === "scope-excluded" ||
1676
+ e.type === "scope-materialization-gap" ||
1676
1677
  e.type === "ignore-excluded" ||
1677
1678
  e.type === "delete-refused-bulk-asymmetry"
1678
1679
  ) return;
package/src/cli/share.ts CHANGED
@@ -1604,7 +1604,12 @@ async function executeDeletes(
1604
1604
  item.intentVersion !== null &&
1605
1605
  (!hasCurrentLocalDeleteIntent(entry) ||
1606
1606
  entry!.localDeleteIntent!.version !== item.intentVersion ||
1607
- !isLocallyAbsent(path.join(run.syncRoot, relativePath)))
1607
+ // Codec boundary: journal keys are canonical; the last-second
1608
+ // absence re-check must probe the ENCODED local path or a win32
1609
+ // colon key always looks absent (a raw `:` name can never exist
1610
+ // on disk) and the remote delete proceeds while the encoded local
1611
+ // file is still present.
1612
+ !isLocallyAbsent(localPathForVaultKey(run.syncRoot, relativePath)))
1608
1613
  ) {
1609
1614
  if (entry?.localDeleteIntent) {
1610
1615
  delete entry.localDeleteIntent;