@indigoai-us/hq-cloud 6.15.0 → 6.15.2

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 (329) hide show
  1. package/dist/bin/sync-mutation.d.ts +16 -0
  2. package/dist/bin/sync-mutation.d.ts.map +1 -0
  3. package/dist/bin/sync-mutation.js +60 -0
  4. package/dist/bin/sync-mutation.js.map +1 -0
  5. package/dist/bin/sync-mutation.test.d.ts +2 -0
  6. package/dist/bin/sync-mutation.test.d.ts.map +1 -0
  7. package/dist/bin/sync-mutation.test.js +165 -0
  8. package/dist/bin/sync-mutation.test.js.map +1 -0
  9. package/dist/bin/sync-runner-company.d.ts +8 -0
  10. package/dist/bin/sync-runner-company.d.ts.map +1 -1
  11. package/dist/bin/sync-runner-company.js +16 -0
  12. package/dist/bin/sync-runner-company.js.map +1 -1
  13. package/dist/bin/sync-runner-company.test.d.ts +2 -0
  14. package/dist/bin/sync-runner-company.test.d.ts.map +1 -0
  15. package/dist/bin/sync-runner-company.test.js +36 -0
  16. package/dist/bin/sync-runner-company.test.js.map +1 -0
  17. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  18. package/dist/bin/sync-runner-watch-loop.js +98 -8
  19. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  20. package/dist/bin/sync-runner.d.ts +17 -0
  21. package/dist/bin/sync-runner.d.ts.map +1 -1
  22. package/dist/bin/sync-runner.js.map +1 -1
  23. package/dist/bin/sync-runner.test.js +109 -0
  24. package/dist/bin/sync-runner.test.js.map +1 -1
  25. package/dist/cli/conflict-recovery.test.d.ts +2 -0
  26. package/dist/cli/conflict-recovery.test.d.ts.map +1 -0
  27. package/dist/cli/conflict-recovery.test.js +201 -0
  28. package/dist/cli/conflict-recovery.test.js.map +1 -0
  29. package/dist/cli/conflict.d.ts +60 -0
  30. package/dist/cli/conflict.d.ts.map +1 -1
  31. package/dist/cli/conflict.js +333 -0
  32. package/dist/cli/conflict.js.map +1 -1
  33. package/dist/cli/sync.d.ts +35 -0
  34. package/dist/cli/sync.d.ts.map +1 -1
  35. package/dist/cli/sync.js +100 -0
  36. package/dist/cli/sync.js.map +1 -1
  37. package/dist/cli/sync.test.js +85 -1
  38. package/dist/cli/sync.test.js.map +1 -1
  39. package/dist/index.d.ts +2 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +1 -0
  42. package/dist/index.js.map +1 -1
  43. package/dist/skill-telemetry.d.ts +6 -0
  44. package/dist/skill-telemetry.d.ts.map +1 -1
  45. package/dist/skill-telemetry.js +14 -2
  46. package/dist/skill-telemetry.js.map +1 -1
  47. package/dist/skill-telemetry.test.js +79 -0
  48. package/dist/skill-telemetry.test.js.map +1 -1
  49. package/dist/sync/candidate-uploader.d.ts +88 -0
  50. package/dist/sync/candidate-uploader.d.ts.map +1 -0
  51. package/dist/sync/candidate-uploader.js +212 -0
  52. package/dist/sync/candidate-uploader.js.map +1 -0
  53. package/dist/sync/candidate-uploader.test.d.ts +2 -0
  54. package/dist/sync/candidate-uploader.test.d.ts.map +1 -0
  55. package/dist/sync/candidate-uploader.test.js +132 -0
  56. package/dist/sync/candidate-uploader.test.js.map +1 -0
  57. package/dist/sync/delta-client.d.ts +73 -0
  58. package/dist/sync/delta-client.d.ts.map +1 -0
  59. package/dist/sync/delta-client.js +201 -0
  60. package/dist/sync/delta-client.js.map +1 -0
  61. package/dist/sync/delta-client.test.d.ts +2 -0
  62. package/dist/sync/delta-client.test.d.ts.map +1 -0
  63. package/dist/sync/delta-client.test.js +97 -0
  64. package/dist/sync/delta-client.test.js.map +1 -0
  65. package/dist/sync/durable-apply.d.ts +76 -0
  66. package/dist/sync/durable-apply.d.ts.map +1 -0
  67. package/dist/sync/durable-apply.js +530 -0
  68. package/dist/sync/durable-apply.js.map +1 -0
  69. package/dist/sync/durable-apply.test.d.ts +2 -0
  70. package/dist/sync/durable-apply.test.d.ts.map +1 -0
  71. package/dist/sync/durable-apply.test.js +180 -0
  72. package/dist/sync/durable-apply.test.js.map +1 -0
  73. package/dist/sync/event-sync.d.ts +33 -1
  74. package/dist/sync/event-sync.d.ts.map +1 -1
  75. package/dist/sync/event-sync.js +149 -1
  76. package/dist/sync/event-sync.js.map +1 -1
  77. package/dist/sync/event-sync.test.js +142 -1
  78. package/dist/sync/event-sync.test.js.map +1 -1
  79. package/dist/sync/index.d.ts +2 -0
  80. package/dist/sync/index.d.ts.map +1 -1
  81. package/dist/sync/index.js +1 -0
  82. package/dist/sync/index.js.map +1 -1
  83. package/dist/sync/multipart-uploader.d.ts +99 -0
  84. package/dist/sync/multipart-uploader.d.ts.map +1 -0
  85. package/dist/sync/multipart-uploader.js +447 -0
  86. package/dist/sync/multipart-uploader.js.map +1 -0
  87. package/dist/sync/multipart-uploader.test.d.ts +2 -0
  88. package/dist/sync/multipart-uploader.test.d.ts.map +1 -0
  89. package/dist/sync/multipart-uploader.test.js +119 -0
  90. package/dist/sync/multipart-uploader.test.js.map +1 -0
  91. package/dist/sync/mutation-client.d.ts +85 -0
  92. package/dist/sync/mutation-client.d.ts.map +1 -0
  93. package/dist/sync/mutation-client.js +245 -0
  94. package/dist/sync/mutation-client.js.map +1 -0
  95. package/dist/sync/mutation-client.test.d.ts +2 -0
  96. package/dist/sync/mutation-client.test.d.ts.map +1 -0
  97. package/dist/sync/mutation-client.test.js +51 -0
  98. package/dist/sync/mutation-client.test.js.map +1 -0
  99. package/dist/sync/push-receiver.d.ts +45 -0
  100. package/dist/sync/push-receiver.d.ts.map +1 -1
  101. package/dist/sync/push-receiver.js +101 -0
  102. package/dist/sync/push-receiver.js.map +1 -1
  103. package/dist/sync/push-receiver.test.js +54 -2
  104. package/dist/sync/push-receiver.test.js.map +1 -1
  105. package/dist/sync/scope-inventory-client.d.ts +69 -0
  106. package/dist/sync/scope-inventory-client.d.ts.map +1 -0
  107. package/dist/sync/scope-inventory-client.js +210 -0
  108. package/dist/sync/scope-inventory-client.js.map +1 -0
  109. package/dist/sync/scope-inventory-client.test.d.ts +2 -0
  110. package/dist/sync/scope-inventory-client.test.d.ts.map +1 -0
  111. package/dist/sync/scope-inventory-client.test.js +94 -0
  112. package/dist/sync/scope-inventory-client.test.js.map +1 -0
  113. package/dist/sync/snapshot-client.d.ts +98 -0
  114. package/dist/sync/snapshot-client.d.ts.map +1 -0
  115. package/dist/sync/snapshot-client.js +402 -0
  116. package/dist/sync/snapshot-client.js.map +1 -0
  117. package/dist/sync/snapshot-client.test.d.ts +2 -0
  118. package/dist/sync/snapshot-client.test.d.ts.map +1 -0
  119. package/dist/sync/snapshot-client.test.js +169 -0
  120. package/dist/sync/snapshot-client.test.js.map +1 -0
  121. package/dist/sync/uploader-finalization.d.ts +97 -0
  122. package/dist/sync/uploader-finalization.d.ts.map +1 -0
  123. package/dist/sync/uploader-finalization.js +273 -0
  124. package/dist/sync/uploader-finalization.js.map +1 -0
  125. package/dist/sync/uploader-finalization.test.d.ts +2 -0
  126. package/dist/sync/uploader-finalization.test.d.ts.map +1 -0
  127. package/dist/sync/uploader-finalization.test.js +92 -0
  128. package/dist/sync/uploader-finalization.test.js.map +1 -0
  129. package/dist/telemetry.d.ts +11 -1
  130. package/dist/telemetry.d.ts.map +1 -1
  131. package/dist/telemetry.js +21 -2
  132. package/dist/telemetry.js.map +1 -1
  133. package/dist/telemetry.test.js +80 -0
  134. package/dist/telemetry.test.js.map +1 -1
  135. package/package.json +6 -1
  136. package/.claude/policies/hq-cloud-esm-cannot-spy-fs-builtins.md +0 -30
  137. package/.claude/policies/hq-cloud-strip-types-no-parameter-properties.md +0 -22
  138. package/.github/workflows/ci.yml +0 -84
  139. package/.github/workflows/publish.yml +0 -56
  140. package/.github/workflows/unreleased-commits-nag.yml +0 -256
  141. package/eslint.config.js +0 -67
  142. package/pnpm-workspace.yaml +0 -2
  143. package/scripts/presign-transport-e2e.mjs +0 -250
  144. package/scripts/vault-rebaseline.sh +0 -323
  145. package/scripts/vault-rescue.sh +0 -332
  146. package/src/active-company.test.ts +0 -188
  147. package/src/active-company.ts +0 -168
  148. package/src/agent-codex-instructions.test.ts +0 -332
  149. package/src/agent-codex-instructions.ts +0 -309
  150. package/src/auth.ts +0 -146
  151. package/src/backup-prune.test.ts +0 -98
  152. package/src/backup-prune.ts +0 -182
  153. package/src/bin/backup-prune-runner.ts +0 -33
  154. package/src/bin/rescue-runner.ts +0 -25
  155. package/src/bin/sync-runner-company.ts +0 -695
  156. package/src/bin/sync-runner-events.test.ts +0 -143
  157. package/src/bin/sync-runner-events.ts +0 -55
  158. package/src/bin/sync-runner-planning.test.ts +0 -311
  159. package/src/bin/sync-runner-planning.ts +0 -258
  160. package/src/bin/sync-runner-rollup.test.ts +0 -37
  161. package/src/bin/sync-runner-rollup.ts +0 -97
  162. package/src/bin/sync-runner-telemetry.ts +0 -15
  163. package/src/bin/sync-runner-watch-loop.ts +0 -1235
  164. package/src/bin/sync-runner-watch-routes.test.ts +0 -71
  165. package/src/bin/sync-runner-watch-routes.ts +0 -184
  166. package/src/bin/sync-runner.test.ts +0 -8767
  167. package/src/bin/sync-runner.ts +0 -2190
  168. package/src/cli/accept.ts +0 -124
  169. package/src/cli/conflict.ts +0 -119
  170. package/src/cli/doctor.test.ts +0 -581
  171. package/src/cli/doctor.ts +0 -642
  172. package/src/cli/index.ts +0 -49
  173. package/src/cli/invite.test.ts +0 -250
  174. package/src/cli/invite.ts +0 -214
  175. package/src/cli/promote.ts +0 -157
  176. package/src/cli/reindex-knowledge.test.ts +0 -307
  177. package/src/cli/reindex-knowledge.ts +0 -450
  178. package/src/cli/reindex.test.ts +0 -957
  179. package/src/cli/reindex.ts +0 -979
  180. package/src/cli/rescue-classify-ordering.test.ts +0 -548
  181. package/src/cli/rescue-clone-diagnostics.test.ts +0 -120
  182. package/src/cli/rescue-core.ts +0 -3011
  183. package/src/cli/rescue-drift-reconcile.test.ts +0 -179
  184. package/src/cli/rescue-drop-dir-symlink.test.ts +0 -224
  185. package/src/cli/rescue-exec-bit-preserve.test.ts +0 -187
  186. package/src/cli/rescue-hq-root-guard.test.ts +0 -232
  187. package/src/cli/rescue-journal-reconcile.test.ts +0 -215
  188. package/src/cli/rescue-mtime-preserve.test.ts +0 -203
  189. package/src/cli/rescue-settings-reconcile.test.ts +0 -637
  190. package/src/cli/rescue-snapshot.test.ts +0 -57
  191. package/src/cli/rescue-snapshot.ts +0 -51
  192. package/src/cli/rescue.reindex.test.ts +0 -63
  193. package/src/cli/rescue.test.ts +0 -131
  194. package/src/cli/rescue.ts +0 -182
  195. package/src/cli/share.test.ts +0 -7843
  196. package/src/cli/share.ts +0 -3663
  197. package/src/cli/sync-scope.test.ts +0 -652
  198. package/src/cli/sync.test.ts +0 -5207
  199. package/src/cli/sync.ts +0 -3470
  200. package/src/cli/tombstones.ts +0 -106
  201. package/src/cli/watch-event-push-conflict.test.ts +0 -234
  202. package/src/client-info.test.ts +0 -214
  203. package/src/client-info.ts +0 -121
  204. package/src/cognito-auth.test.ts +0 -712
  205. package/src/cognito-auth.ts +0 -1422
  206. package/src/company-resolver.test.ts +0 -618
  207. package/src/company-resolver.ts +0 -521
  208. package/src/context.test.ts +0 -583
  209. package/src/context.ts +0 -378
  210. package/src/daemon-worker.ts +0 -26
  211. package/src/daemon.ts +0 -99
  212. package/src/entity-resolver.test.ts +0 -315
  213. package/src/entity-resolver.ts +0 -180
  214. package/src/ignore.test.ts +0 -466
  215. package/src/ignore.ts +0 -469
  216. package/src/index.ts +0 -439
  217. package/src/journal.test.ts +0 -968
  218. package/src/journal.ts +0 -765
  219. package/src/lib/cloud-authoritative.test.ts +0 -45
  220. package/src/lib/cloud-authoritative.ts +0 -59
  221. package/src/lib/conflict-file.ts +0 -86
  222. package/src/lib/conflict-index.ts +0 -289
  223. package/src/lib/conflict.test.ts +0 -348
  224. package/src/lib/describe-error.test.ts +0 -100
  225. package/src/lib/describe-error.ts +0 -58
  226. package/src/lib/exit-codes.ts +0 -24
  227. package/src/lib/machine-id.test.ts +0 -231
  228. package/src/lib/machine-id.ts +0 -175
  229. package/src/lib/net-errors.test.ts +0 -65
  230. package/src/lib/net-errors.ts +0 -86
  231. package/src/lib/readlink-safe.test.ts +0 -43
  232. package/src/lib/readlink-safe.ts +0 -29
  233. package/src/local-path-codec.test.ts +0 -138
  234. package/src/local-path-codec.ts +0 -161
  235. package/src/machine-auth.test.ts +0 -1323
  236. package/src/manifest-reconcile.test.ts +0 -1123
  237. package/src/manifest-reconcile.ts +0 -518
  238. package/src/object-io.test.ts +0 -1221
  239. package/src/object-io.ts +0 -1306
  240. package/src/operation-lock.test.ts +0 -484
  241. package/src/operation-lock.ts +0 -680
  242. package/src/outcome-telemetry.test.ts +0 -498
  243. package/src/outcome-telemetry.ts +0 -639
  244. package/src/personal-vault-exclusions.test.ts +0 -308
  245. package/src/personal-vault-exclusions.ts +0 -354
  246. package/src/personal-vault.test.ts +0 -756
  247. package/src/personal-vault.ts +0 -496
  248. package/src/prefix-coalesce.test.ts +0 -240
  249. package/src/prefix-coalesce.ts +0 -273
  250. package/src/public-surface.test.ts +0 -117
  251. package/src/qmd-reindex.test.ts +0 -877
  252. package/src/qmd-reindex.ts +0 -842
  253. package/src/read-only-state-dir.test.ts +0 -188
  254. package/src/remote-pull.test.ts +0 -1130
  255. package/src/remote-pull.ts +0 -618
  256. package/src/s3.symlink-materialize.test.ts +0 -492
  257. package/src/s3.test.ts +0 -1789
  258. package/src/s3.ts +0 -1532
  259. package/src/schemas/signal-types.test.ts +0 -82
  260. package/src/schemas/signal-types.ts +0 -38
  261. package/src/schemas/source-channels.test.ts +0 -82
  262. package/src/schemas/source-channels.ts +0 -53
  263. package/src/scope-shrink.test.ts +0 -633
  264. package/src/scope-shrink.ts +0 -481
  265. package/src/signals/get.test.ts +0 -310
  266. package/src/signals/get.ts +0 -75
  267. package/src/signals/internals.ts +0 -195
  268. package/src/signals/list.test.ts +0 -420
  269. package/src/signals/list.ts +0 -79
  270. package/src/signals/parse.ts +0 -8
  271. package/src/signals/types.ts +0 -91
  272. package/src/skill-telemetry.test.ts +0 -1825
  273. package/src/skill-telemetry.ts +0 -1439
  274. package/src/sources/get.test.ts +0 -293
  275. package/src/sources/get.ts +0 -66
  276. package/src/sources/internals.ts +0 -198
  277. package/src/sources/list.test.ts +0 -402
  278. package/src/sources/list.ts +0 -84
  279. package/src/sources/parse.ts +0 -43
  280. package/src/sources/types.ts +0 -84
  281. package/src/sync/event-sync.test.ts +0 -594
  282. package/src/sync/event-sync.ts +0 -545
  283. package/src/sync/feature-flags.test.ts +0 -378
  284. package/src/sync/feature-flags.ts +0 -62
  285. package/src/sync/index.ts +0 -76
  286. package/src/sync/lease-client.test.ts +0 -128
  287. package/src/sync/lease-client.ts +0 -207
  288. package/src/sync/logger.test.ts +0 -242
  289. package/src/sync/logger.ts +0 -79
  290. package/src/sync/metrics.test.ts +0 -462
  291. package/src/sync/metrics.ts +0 -213
  292. package/src/sync/pull-scope.ts +0 -265
  293. package/src/sync/push-event.test.ts +0 -266
  294. package/src/sync/push-event.ts +0 -224
  295. package/src/sync/push-receiver.test.ts +0 -566
  296. package/src/sync/push-receiver.ts +0 -1048
  297. package/src/sync/push-transport.ts +0 -231
  298. package/src/sync/realtime-rollout.test.ts +0 -86
  299. package/src/sync/realtime-rollout.ts +0 -262
  300. package/src/sync/state-store.test.ts +0 -194
  301. package/src/sync/state-store.ts +0 -727
  302. package/src/sync-core.ts +0 -58
  303. package/src/sync-progress.test.ts +0 -94
  304. package/src/sync-progress.ts +0 -140
  305. package/src/telemetry-events.test.ts +0 -88
  306. package/src/telemetry-events.ts +0 -205
  307. package/src/telemetry.test.ts +0 -1280
  308. package/src/telemetry.ts +0 -1109
  309. package/src/types.ts +0 -314
  310. package/src/vault-client.test.ts +0 -1380
  311. package/src/vault-client.ts +0 -1694
  312. package/src/version.ts +0 -24
  313. package/src/watch-roots.test.ts +0 -278
  314. package/src/watch-roots.ts +0 -162
  315. package/src/watcher-event-gate.test.ts +0 -212
  316. package/src/watcher.test.ts +0 -1079
  317. package/src/watcher.ts +0 -1741
  318. package/test/e2e/sync/cross-tenant-isolation.test.ts +0 -630
  319. package/test/e2e/sync/skill-telemetry-oversized-transcript.test.ts +0 -124
  320. package/test/e2e/sync/transient-company-leg.test.ts +0 -384
  321. package/test/e2e/sync/windows-unreadable-link-leg.test.ts +0 -191
  322. package/test/e2e/watcher-real-chokidar.test.ts +0 -165
  323. package/test/e2e/watcher-recursive-backend.test.ts +0 -181
  324. package/test/e2e/watcher-scoped-coverage.test.ts +0 -381
  325. package/test/invite-flow.integration.test.ts +0 -244
  326. package/test/joiner-manifest-reconcile.integration.test.ts +0 -322
  327. package/test/share-sync.integration.test.ts +0 -213
  328. package/tsconfig.json +0 -19
  329. package/vitest.config.ts +0 -22
@@ -1,957 +0,0 @@
1
- /**
2
- * Unit tests for `hq reindex` (the formerly-bash hq-core hook, now shipped
3
- * in this package and invoked via the CLI).
4
- *
5
- * The logic is implemented natively in TypeScript in ./reindex.ts; these tests
6
- * exercise reindex() directly against a temp HQ tree, asserting the observable
7
- * filesystem outcomes (skill wrappers, retired personal-overlay mirror cleanup)
8
- * rather than re-deriving the implementation internals.
9
- */
10
-
11
- import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
12
- import * as fs from "fs";
13
- import * as path from "path";
14
- import * as os from "os";
15
- import { reindex } from "./reindex.js";
16
- import { lockPathFor, OPERATION_LOCKED_EXIT } from "../operation-lock.js";
17
-
18
- // HQ-B0: simulate the Windows filesystem rejecting a ':' in a path segment.
19
- // The flag is off by default, so every other test sees the real `mkdirSync`;
20
- // the regression test below flips it on only for its own run.
21
- const hoisted = vi.hoisted(() => ({
22
- failNamespacedMkdir: false,
23
- // When set, any mkdir whose path contains this substring throws ENOENT —
24
- // lets a test simulate a mkdir failure at a NON-wrapper site (e.g. the
25
- // `.claude/skills` parent or a `core/<type>` mirror dir).
26
- failMkdirContaining: "" as string,
27
- // HQ-CLI-4: when set, any symlink whose LINK path contains this substring
28
- // throws EPERM — simulates Windows WITHOUT Developer Mode, where symlink
29
- // creation is a privileged operation. Off by default so other tests see the
30
- // real `symlinkSync`.
31
- failSymlinkContaining: "" as string,
32
- }));
33
- vi.mock("fs", async (importOriginal) => {
34
- const actual = await importOriginal<typeof import("fs")>();
35
- const mkdirSync = ((p: unknown, opts: unknown) => {
36
- if (
37
- typeof p === "string" &&
38
- ((hoisted.failNamespacedMkdir && /[:][^/\\]*$/.test(p)) || // colon in final segment
39
- (hoisted.failMkdirContaining !== "" && p.includes(hoisted.failMkdirContaining)))
40
- ) {
41
- throw Object.assign(
42
- new Error(`ENOENT: no such file or directory, mkdir '${p}'`),
43
- { code: "ENOENT" },
44
- );
45
- }
46
- return (actual.mkdirSync as (p: unknown, opts: unknown) => unknown)(p, opts);
47
- }) as typeof actual.mkdirSync;
48
- const symlinkSync = ((target: unknown, linkPath: unknown, type?: unknown) => {
49
- if (
50
- hoisted.failSymlinkContaining !== "" &&
51
- typeof linkPath === "string" &&
52
- linkPath.includes(hoisted.failSymlinkContaining)
53
- ) {
54
- throw Object.assign(
55
- new Error(`EPERM: operation not permitted, symlink '${target}' -> '${linkPath}'`),
56
- { code: "EPERM" },
57
- );
58
- }
59
- return (actual.symlinkSync as (t: unknown, l: unknown, ty?: unknown) => unknown)(
60
- target,
61
- linkPath,
62
- type,
63
- );
64
- }) as typeof actual.symlinkSync;
65
- const patched = { ...actual, mkdirSync, symlinkSync };
66
- return { ...patched, default: patched };
67
- });
68
-
69
- describe("reindex", () => {
70
- let root: string;
71
- let stateDir: string;
72
-
73
- beforeEach(() => {
74
- root = fs.mkdtempSync(path.join(os.tmpdir(), "ms-test-"));
75
- // Redirect the operation lock into a throwaway dir so reindex's default
76
- // locking never touches the real ~/.hq during tests.
77
- stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "ms-state-"));
78
- process.env.HQ_STATE_DIR = stateDir;
79
- });
80
-
81
- afterEach(() => {
82
- fs.rmSync(root, { recursive: true, force: true });
83
- fs.rmSync(stateDir, { recursive: true, force: true });
84
- delete process.env.HQ_STATE_DIR;
85
- });
86
-
87
- function writeSkill(rel: string): void {
88
- const dir = path.join(root, rel);
89
- fs.mkdirSync(dir, { recursive: true });
90
- fs.writeFileSync(path.join(dir, "SKILL.md"), "skill\n");
91
- }
92
-
93
- // Write a namespaced slash-command source file, e.g.
94
- // writeCommand("core/commands/demo.md") → root/core/commands/demo.md.
95
- function writeCommand(rel: string, body = "command\n"): void {
96
- const abs = path.join(root, rel);
97
- fs.mkdirSync(path.dirname(abs), { recursive: true });
98
- fs.writeFileSync(abs, body);
99
- }
100
-
101
- it("surfaces namespaced skills as .claude/skills/<ns>:<skill>/ wrappers", () => {
102
- writeSkill("core/skills/demo");
103
- fs.writeFileSync(path.join(root, "core/skills/demo/helper.md"), "h\n");
104
- writeSkill("companies/acme/skills/widget");
105
-
106
- const { status } = reindex({ repoRoot: root });
107
- expect(status).toBe(0);
108
-
109
- const coreWrapper = path.join(root, ".claude/skills/core:demo");
110
- expect(fs.lstatSync(path.join(coreWrapper, "SKILL.md")).isSymbolicLink()).toBe(true);
111
- expect(fs.readlinkSync(path.join(coreWrapper, "SKILL.md"))).toBe(
112
- "../../../core/skills/demo/SKILL.md",
113
- );
114
- // Every source file is mirrored, not just SKILL.md.
115
- expect(fs.existsSync(path.join(coreWrapper, "helper.md"))).toBe(true);
116
-
117
- const acmeWrapper = path.join(root, ".claude/skills/acme:widget");
118
- expect(fs.readlinkSync(path.join(acmeWrapper, "SKILL.md"))).toBe(
119
- "../../../companies/acme/skills/widget/SKILL.md",
120
- );
121
- });
122
-
123
- // ── namespaced slash-command surfacing (parallel to skills) ─────────────
124
- // reindex mirrors <ns>/commands/<cmd>.md into .claude/commands/<ns>:<cmd>.md
125
- // as a single symlink per source file, using the same <ns>:<name> naming as
126
- // skills so a company/core/personal/pack command shows up as /<ns>:<cmd>.
127
-
128
- it("surfaces namespaced commands as .claude/commands/<ns>:<cmd>.md symlinks", () => {
129
- writeCommand("core/commands/demo.md");
130
- writeCommand("companies/acme/commands/widget.md");
131
- writeCommand("personal/commands/note.md");
132
- writeCommand("core/packages/hq-pack-demo/commands/hello.md");
133
-
134
- const { status } = reindex({ repoRoot: root });
135
- expect(status).toBe(0);
136
-
137
- const link = (name: string) => path.join(root, ".claude/commands", name);
138
-
139
- expect(fs.lstatSync(link("core:demo.md")).isSymbolicLink()).toBe(true);
140
- expect(fs.readlinkSync(link("core:demo.md"))).toBe("../../core/commands/demo.md");
141
-
142
- expect(fs.readlinkSync(link("acme:widget.md"))).toBe(
143
- "../../companies/acme/commands/widget.md",
144
- );
145
- expect(fs.readlinkSync(link("personal:note.md"))).toBe(
146
- "../../personal/commands/note.md",
147
- );
148
- expect(fs.readlinkSync(link("hq-pack-demo:hello.md"))).toBe(
149
- "../../core/packages/hq-pack-demo/commands/hello.md",
150
- );
151
- });
152
-
153
- it("skips `_`-prefixed, dotfile, and non-.md command files", () => {
154
- writeCommand("core/commands/_partial.md");
155
- writeCommand("core/commands/.hidden.md");
156
- writeCommand("core/commands/notes.txt");
157
- writeCommand("core/commands/real.md");
158
-
159
- reindex({ repoRoot: root });
160
-
161
- const cmds = path.join(root, ".claude/commands");
162
- expect(fs.existsSync(path.join(cmds, "core:real.md"))).toBe(true);
163
- expect(fs.existsSync(path.join(cmds, "core:_partial.md"))).toBe(false);
164
- expect(fs.existsSync(path.join(cmds, "core:.hidden.md"))).toBe(false);
165
- expect(fs.existsSync(path.join(cmds, "core:notes.txt"))).toBe(false);
166
- });
167
-
168
- it("prunes orphan managed command links when the source command disappears", () => {
169
- writeCommand("core/commands/demo.md");
170
- reindex({ repoRoot: root });
171
- expect(fs.existsSync(path.join(root, ".claude/commands/core:demo.md"))).toBe(true);
172
-
173
- fs.rmSync(path.join(root, "core/commands/demo.md"));
174
- reindex({ repoRoot: root });
175
- expect(fs.existsSync(path.join(root, ".claude/commands/core:demo.md"))).toBe(false);
176
- });
177
-
178
- it("is idempotent for commands — a second run keeps the same link", () => {
179
- writeCommand("core/commands/demo.md");
180
- expect(reindex({ repoRoot: root }).status).toBe(0);
181
- expect(reindex({ repoRoot: root }).status).toBe(0);
182
- expect(fs.readlinkSync(path.join(root, ".claude/commands/core:demo.md"))).toBe(
183
- "../../core/commands/demo.md",
184
- );
185
- });
186
-
187
- it("falls back to a hardlink when a command-wrapper symlink fails with EPERM", () => {
188
- writeCommand("core/commands/demo.md", "run me\n");
189
-
190
- hoisted.failSymlinkContaining = path.join(".claude", "commands");
191
- try {
192
- expect(reindex({ repoRoot: root }).status).toBe(0);
193
- } finally {
194
- hoisted.failSymlinkContaining = "";
195
- }
196
-
197
- const link = path.join(root, ".claude/commands/core:demo.md");
198
- expect(fs.existsSync(link)).toBe(true);
199
- // Bridged as a hardlink to the source, not skipped or left as a broken link.
200
- expect(fs.lstatSync(link).isSymbolicLink()).toBe(false);
201
- expect(fs.readFileSync(link, "utf8")).toBe("run me\n");
202
- expect(fs.lstatSync(link).ino).toBe(
203
- fs.statSync(path.join(root, "core/commands/demo.md")).ino,
204
- );
205
- });
206
-
207
- it("leaves hand-authored / non-managed command files alone", () => {
208
- const cmds = path.join(root, ".claude/commands");
209
- fs.mkdirSync(cmds, { recursive: true });
210
- // A real file with no namespace colon → preserved.
211
- fs.writeFileSync(path.join(cmds, "manual.md"), "manual\n");
212
- // A real (non-symlink) file that happens to use a managed namespace prefix
213
- // → still preserved; reindex only ever prunes symlinks it created.
214
- fs.writeFileSync(path.join(cmds, "core:hand.md"), "hand\n");
215
- // A composite symlink under an UNMANAGED namespace → preserved.
216
- writeCommand("core/commands/other.md");
217
- fs.symlinkSync("../../core/commands/other.md", path.join(cmds, "vendor:tool.md"));
218
-
219
- reindex({ repoRoot: root });
220
-
221
- expect(fs.existsSync(path.join(cmds, "manual.md"))).toBe(true);
222
- expect(fs.existsSync(path.join(cmds, "core:hand.md"))).toBe(true);
223
- expect(fs.lstatSync(path.join(cmds, "vendor:tool.md")).isSymbolicLink()).toBe(true);
224
- });
225
-
226
- it("preserves a hand-authored alias under a managed namespace (target basename differs)", () => {
227
- // A live source command...
228
- writeCommand("core/commands/demo.md");
229
- // ...plus a hand-authored alias in the SAME (managed) namespace pointing at
230
- // it under a different command name, with NO core/commands/alias.md source.
231
- // A prefix-only ownership test would wrongly treat this as a reindex link
232
- // and delete it; the exact-target cross-check must keep it.
233
- const cmds = path.join(root, ".claude/commands");
234
- fs.mkdirSync(cmds, { recursive: true });
235
- fs.symlinkSync("../../core/commands/demo.md", path.join(cmds, "core:alias.md"));
236
-
237
- reindex({ repoRoot: root });
238
-
239
- // The alias survives, and the real managed link is still created.
240
- expect(fs.lstatSync(path.join(cmds, "core:alias.md")).isSymbolicLink()).toBe(true);
241
- expect(fs.readlinkSync(path.join(cmds, "core:alias.md"))).toBe("../../core/commands/demo.md");
242
- expect(fs.existsSync(path.join(cmds, "core:demo.md"))).toBe(true);
243
- });
244
-
245
- it("does not create an empty .claude/commands when there are no command sources", () => {
246
- writeSkill("core/skills/demo");
247
- reindex({ repoRoot: root });
248
- expect(fs.existsSync(path.join(root, ".claude/commands"))).toBe(false);
249
- });
250
-
251
- it("does NOT mirror the personal overlay into core/<type>/ (read directly now)", () => {
252
- fs.mkdirSync(path.join(root, "personal/policies"), { recursive: true });
253
- fs.writeFileSync(path.join(root, "personal/policies/myrule.md"), "rule\n");
254
-
255
- const { status } = reindex({ repoRoot: root });
256
- expect(status).toBe(0);
257
-
258
- // personal is the sole read source — reindex must NOT create a mirror
259
- // symlink in core/policies. The HQ tooling reads personal/policies directly,
260
- // so a mirror would only double-surface every entry.
261
- expect(fs.existsSync(path.join(root, "core/policies/myrule.md"))).toBe(false);
262
- });
263
-
264
- it("collapses per-file wrapper-skip spam into ONE summary line (HQ-SYNC-2)", () => {
265
- // A REAL file sits where a managed skill-wrapper symlink would go — the
266
- // legitimate no-op that used to spam one stderr line per entry and flood the
267
- // Sentry breadcrumb ring, evicting the real per-company sync error.
268
- writeSkill("core/skills/demo");
269
- reindex({ repoRoot: root });
270
- // Replace the managed per-file symlink with a real (non-symlink) file.
271
- const slot = path.join(root, ".claude/skills/core:demo/SKILL.md");
272
- fs.rmSync(slot);
273
- fs.writeFileSync(slot, "REAL file, not a link\n");
274
-
275
- const writes: string[] = [];
276
- const spy = vi
277
- .spyOn(process.stderr, "write")
278
- .mockImplementation(((chunk: unknown) => {
279
- writes.push(String(chunk));
280
- return true;
281
- }) as typeof process.stderr.write);
282
- let status: number;
283
- try {
284
- status = reindex({ repoRoot: root }).status;
285
- } finally {
286
- spy.mockRestore();
287
- }
288
- const stderr = writes.join("");
289
-
290
- expect(status).toBe(0);
291
- // The pre-existing real file is left untouched (never overwritten).
292
- expect(fs.lstatSync(slot).isSymbolicLink()).toBe(false);
293
- // No per-file spam…
294
- expect(stderr).not.toContain("already exists and is not a symlink; skipping");
295
- // …just ONE collapsed summary naming the count.
296
- expect(stderr).toMatch(/left 1 pre-existing non-symlink entry untouched/);
297
- expect(stderr).toContain("HQ_REINDEX_VERBOSE=1");
298
- });
299
-
300
- it("keeps per-file wrapper-skip detail under HQ_REINDEX_VERBOSE=1", () => {
301
- writeSkill("core/skills/demo");
302
- reindex({ repoRoot: root });
303
- const slot = path.join(root, ".claude/skills/core:demo/SKILL.md");
304
- fs.rmSync(slot);
305
- fs.writeFileSync(slot, "REAL\n");
306
-
307
- const writes: string[] = [];
308
- const spy = vi
309
- .spyOn(process.stderr, "write")
310
- .mockImplementation(((chunk: unknown) => {
311
- writes.push(String(chunk));
312
- return true;
313
- }) as typeof process.stderr.write);
314
- process.env.HQ_REINDEX_VERBOSE = "1";
315
- try {
316
- reindex({ repoRoot: root });
317
- } finally {
318
- spy.mockRestore();
319
- delete process.env.HQ_REINDEX_VERBOSE;
320
- }
321
- const stderr = writes.join("");
322
- expect(stderr).toContain(
323
- ".claude/skills/core:demo/SKILL.md already exists and is not a symlink; skipping",
324
- );
325
- });
326
-
327
- it("prunes orphan managed wrappers when the source skill disappears", () => {
328
- writeSkill("core/skills/demo");
329
- reindex({ repoRoot: root });
330
- expect(fs.existsSync(path.join(root, ".claude/skills/core:demo"))).toBe(true);
331
-
332
- fs.rmSync(path.join(root, "core/skills/demo"), { recursive: true, force: true });
333
- reindex({ repoRoot: root });
334
- expect(fs.existsSync(path.join(root, ".claude/skills/core:demo"))).toBe(false);
335
- });
336
-
337
- it("is idempotent — a second run is a no-op", () => {
338
- writeSkill("core/skills/demo");
339
- expect(reindex({ repoRoot: root }).status).toBe(0);
340
- expect(reindex({ repoRoot: root }).status).toBe(0);
341
- expect(
342
- fs.readlinkSync(path.join(root, ".claude/skills/core:demo/SKILL.md")),
343
- ).toBe("../../../core/skills/demo/SKILL.md");
344
- });
345
-
346
- // ── Migration cleanup: retired personal-overlay mirror symlinks ──────────
347
- // reindex no longer CREATES core/<type>/<entry> -> ../../personal/<type>/<entry>
348
- // mirror symlinks — personal is read directly. A managed mirror symlink a
349
- // PRIOR release left behind must be pruned on the next reindex, WHETHER OR NOT
350
- // its personal source still exists, so it can never double-surface alongside
351
- // the direct read.
352
-
353
- // Forge a legacy managed mirror symlink exactly as the old reindex would have.
354
- function legacyMirror(type: string, entry: string): string {
355
- const coreDir = path.join(root, "core", type);
356
- fs.mkdirSync(coreDir, { recursive: true });
357
- const link = path.join(coreDir, entry);
358
- fs.symlinkSync(`../../personal/${type}/${entry}`, link);
359
- return link;
360
- }
361
-
362
- it("prunes a legacy mirror symlink even when its personal source still exists", () => {
363
- fs.mkdirSync(path.join(root, "personal/policies"), { recursive: true });
364
- fs.writeFileSync(path.join(root, "personal/policies/myrule.md"), "rule\n");
365
- const link = legacyMirror("policies", "myrule.md");
366
- expect(fs.lstatSync(link).isSymbolicLink()).toBe(true);
367
-
368
- expect(reindex({ repoRoot: root }).status).toBe(0);
369
-
370
- // Pruned — personal/policies/myrule.md is read directly now, no mirror.
371
- expect(fs.existsSync(link)).toBe(false);
372
- expect(() => fs.lstatSync(link)).toThrow();
373
- // The personal source itself is untouched.
374
- expect(fs.existsSync(path.join(root, "personal/policies/myrule.md"))).toBe(true);
375
- });
376
-
377
- it("prunes a legacy mirror symlink whose personal source was deleted", () => {
378
- const link = legacyMirror("policies", "gone.md"); // dangling: no source
379
- expect(fs.lstatSync(link).isSymbolicLink()).toBe(true);
380
-
381
- expect(reindex({ repoRoot: root }).status).toBe(0);
382
- expect(fs.existsSync(link)).toBe(false);
383
- });
384
-
385
- it("prunes a legacy mirror DIRECTORY symlink without recursing into the source (DEV-1767 shape)", () => {
386
- fs.mkdirSync(path.join(root, "personal/knowledge/my-kb"), { recursive: true });
387
- fs.writeFileSync(path.join(root, "personal/knowledge/my-kb/note.md"), "n\n");
388
- const link = legacyMirror("knowledge", "my-kb"); // directory-shaped symlink
389
- expect(fs.lstatSync(link).isSymbolicLink()).toBe(true);
390
-
391
- expect(reindex({ repoRoot: root }).status).toBe(0);
392
- expect(fs.existsSync(link)).toBe(false);
393
- // rmSync on the symlink must NOT recurse into the resolved directory.
394
- expect(fs.existsSync(path.join(root, "personal/knowledge/my-kb/note.md"))).toBe(true);
395
- });
396
-
397
- it("leaves a real core file and a foreign symlink in core/<type>/ untouched (cross-check invariant)", () => {
398
- fs.mkdirSync(path.join(root, "core/policies"), { recursive: true });
399
- // (a) a real release-shipped core file — not a symlink, never ours.
400
- fs.writeFileSync(path.join(root, "core/policies/shipped.md"), "shipped\n");
401
- // (b) a human-authored symlink in core/policies pointing SOMEWHERE ELSE
402
- // (not the managed ../../personal/policies/<name> shape) — even though
403
- // it is dangling, its target doesn't match, so it must survive.
404
- fs.symlinkSync("../../somewhere/else.md", path.join(root, "core/policies/foreign.md"));
405
- // (c) a symlink whose target's basename disagrees with its own name — the
406
- // exact cross-check catches this: name 'mismatch.md' but target points
407
- // at personal/policies/other.md → NOT managed, left alone.
408
- fs.symlinkSync("../../personal/policies/other.md", path.join(root, "core/policies/mismatch.md"));
409
-
410
- expect(reindex({ repoRoot: root }).status).toBe(0);
411
-
412
- expect(fs.lstatSync(path.join(root, "core/policies/shipped.md")).isSymbolicLink()).toBe(false);
413
- expect(fs.existsSync(path.join(root, "core/policies/shipped.md"))).toBe(true);
414
- expect(fs.lstatSync(path.join(root, "core/policies/foreign.md")).isSymbolicLink()).toBe(true);
415
- expect(fs.lstatSync(path.join(root, "core/policies/mismatch.md")).isSymbolicLink()).toBe(true);
416
- });
417
-
418
- it("skips `_`-prefixed and dotfile skill folders", () => {
419
- writeSkill("core/skills/_template");
420
- fs.mkdirSync(path.join(root, "core/skills/.hidden"), { recursive: true });
421
- fs.writeFileSync(path.join(root, "core/skills/.hidden/SKILL.md"), "x\n");
422
- writeSkill("core/skills/real");
423
-
424
- reindex({ repoRoot: root });
425
-
426
- expect(fs.existsSync(path.join(root, ".claude/skills/core:real"))).toBe(true);
427
- expect(fs.existsSync(path.join(root, ".claude/skills/core:_template"))).toBe(false);
428
- expect(fs.existsSync(path.join(root, ".claude/skills/core:.hidden"))).toBe(false);
429
- });
430
-
431
- it("removes legacy .claude/commands/<ns>/<skill>.md bridge symlinks", () => {
432
- const cmdDir = path.join(root, ".claude/commands/core");
433
- fs.mkdirSync(cmdDir, { recursive: true });
434
- // Legacy bridge symlink (matches the cleanup pattern) → removed.
435
- fs.symlinkSync("../../../core/skills/demo/SKILL.md", path.join(cmdDir, "demo.md"));
436
- // A real file (non-symlink) → preserved.
437
- fs.writeFileSync(path.join(cmdDir, "keep.md"), "manual\n");
438
-
439
- reindex({ repoRoot: root });
440
-
441
- expect(fs.existsSync(path.join(cmdDir, "demo.md"))).toBe(false);
442
- expect(fs.existsSync(path.join(cmdDir, "keep.md"))).toBe(true);
443
- });
444
-
445
- it("leaves hand-authored composite wrappers (unmanaged namespace) alone", () => {
446
- // A wrapper whose namespace ('vendor') is not a managed namespace and
447
- // whose target points into core/skills/<other> — must not be pruned.
448
- const wrapper = path.join(root, ".claude/skills/vendor:tool");
449
- fs.mkdirSync(wrapper, { recursive: true });
450
- writeSkill("core/skills/helper");
451
- fs.symlinkSync("../../../core/skills/helper/SKILL.md", path.join(wrapper, "SKILL.md"));
452
-
453
- reindex({ repoRoot: root });
454
-
455
- expect(fs.existsSync(wrapper)).toBe(true);
456
- expect(fs.lstatSync(path.join(wrapper, "SKILL.md")).isSymbolicLink()).toBe(true);
457
- });
458
-
459
- // ── symlink-to-directory reindex links (feedback_731b88d9) ──────────────
460
- // reindex emits two kinds of link whose TARGET is a directory:
461
- // (a) a skill folder that contains a sub-directory (e.g. `agents/`) → the
462
- // wrapper gets a per-entry symlink that resolves to a directory;
463
- // (b) a personal/<type>/<entry> that is itself a directory → mirrored into
464
- // core/<type>/<entry> as a directory symlink (the DEV-1767 shape:
465
- // `core/knowledge/<kb> -> ../../personal/knowledge/<kb>`).
466
- // The bash-era reindex followed these into the directory and aborted with
467
- // ERR_FS_EISDIR, dropping the link. These lock that they are created,
468
- // resolve to the directory, survive an idempotent re-run, and never crash.
469
-
470
- it("creates a symlink-to-DIR wrapper entry for a skill sub-directory and preserves it", () => {
471
- writeSkill("core/skills/demo");
472
- // A sub-directory inside the skill folder → its wrapper link targets a dir.
473
- fs.mkdirSync(path.join(root, "core/skills/demo/agents"), { recursive: true });
474
- fs.writeFileSync(path.join(root, "core/skills/demo/agents/helper.md"), "h\n");
475
-
476
- expect(reindex({ repoRoot: root }).status).toBe(0);
477
-
478
- const agentsLink = path.join(root, ".claude/skills/core:demo/agents");
479
- expect(fs.lstatSync(agentsLink).isSymbolicLink()).toBe(true);
480
- expect(fs.readlinkSync(agentsLink)).toBe("../../../core/skills/demo/agents");
481
- // The link resolves to the directory (the EISDIR-prone dereference path).
482
- expect(fs.statSync(agentsLink).isDirectory()).toBe(true);
483
- expect(fs.readFileSync(path.join(agentsLink, "helper.md"), "utf-8")).toBe("h\n");
484
-
485
- // Idempotent re-run: the prune pass walks the symlink-to-dir and must not
486
- // follow it / drop it / throw EISDIR.
487
- expect(reindex({ repoRoot: root }).status).toBe(0);
488
- expect(fs.lstatSync(agentsLink).isSymbolicLink()).toBe(true);
489
- expect(fs.readlinkSync(agentsLink)).toBe("../../../core/skills/demo/agents");
490
- });
491
-
492
- it("does NOT mirror a personal/<type> DIRECTORY entry into core/ (read directly now)", () => {
493
- // personal/knowledge/<kb>/ is a directory of notes. It used to mirror as a
494
- // core/ directory symlink (the DEV-1767 shape rescue/update-hq choked on).
495
- // The mirror is retired: qmd indexes personal/knowledge directly, so reindex
496
- // must leave core/knowledge alone — nothing to choke on anymore.
497
- fs.mkdirSync(path.join(root, "personal/knowledge/my-kb"), { recursive: true });
498
- fs.writeFileSync(path.join(root, "personal/knowledge/my-kb/note.md"), "owned by personal\n");
499
-
500
- expect(reindex({ repoRoot: root }).status).toBe(0);
501
-
502
- expect(fs.existsSync(path.join(root, "core/knowledge/my-kb"))).toBe(false);
503
- // The personal source is untouched.
504
- expect(fs.readFileSync(path.join(root, "personal/knowledge/my-kb/note.md"), "utf-8")).toBe(
505
- "owned by personal\n",
506
- );
507
- });
508
-
509
- // ── operation lock: reindex uses a SEPARATE scope from sync/rescue ──────
510
-
511
- // Regression for feedback_ed98d810 (the "hq reindex indefinite hang"): when
512
- // a watch-mode hq-sync-runner holds the per-root "operation" lock for its
513
- // whole lifetime, a shared lock starved `hq reindex` forever. reindex now
514
- // takes its own "reindex" scope, so it must run to completion even while the
515
- // sync/watch "operation" lock is held by a LIVE holder.
516
- it("completes while the sync/watch operation lock is held (no starvation)", () => {
517
- // Simulate the long-lived watch runner holding the "operation"-scope lock.
518
- // pid = this process → the lock probes it as ALIVE (a live same-PID holder
519
- // is treated as busy, never reclaimed), so it is a genuine live holder.
520
- const syncLock = lockPathFor(root); // default "operation" scope
521
- fs.mkdirSync(path.dirname(syncLock), { recursive: true });
522
- fs.writeFileSync(
523
- syncLock,
524
- JSON.stringify({
525
- pid: process.pid,
526
- command: "sync",
527
- startedAt: new Date(0).toISOString(),
528
- hqRoot: root,
529
- }),
530
- );
531
- // Even with a zero-wait bound (never block), reindex acquires its OWN scope
532
- // and finishes — proving the operation lock no longer gates it.
533
- const { status } = reindex({ repoRoot: root, lockTimeoutSec: 0 });
534
- expect(status).toBe(0);
535
- expect(fs.existsSync(path.join(root, ".claude/skills"))).toBe(true);
536
- // The sync/watch holder is untouched.
537
- expect(fs.existsSync(syncLock)).toBe(true);
538
- });
539
-
540
- it("refuses (OPERATION_LOCKED_EXIT) when another reindex holds the reindex-scope lock (lockTimeoutSec:0)", () => {
541
- // reindex-vs-reindex is still mutually exclusive. A live holder on the
542
- // "reindex" scope → with lockTimeoutSec:0 reindex refuses immediately and
543
- // does no work. (The wait-then-acquire path is in operation-lock.test.ts.)
544
- const lp = lockPathFor(root, "reindex");
545
- fs.mkdirSync(path.dirname(lp), { recursive: true });
546
- fs.writeFileSync(
547
- lp,
548
- JSON.stringify({ pid: 1, command: "reindex", startedAt: new Date(0).toISOString(), hqRoot: root }),
549
- );
550
- const before = fs.existsSync(path.join(root, ".claude/skills"));
551
- const { status } = reindex({ repoRoot: root, lockTimeoutSec: 0 });
552
- expect(status).toBe(OPERATION_LOCKED_EXIT);
553
- // It refused before doing any work (didn't create .claude/skills).
554
- expect(fs.existsSync(path.join(root, ".claude/skills"))).toBe(before);
555
- });
556
-
557
- it("skipLock bypasses the reindex lock (internal sync/rescue caller path)", () => {
558
- const lp = lockPathFor(root, "reindex");
559
- fs.mkdirSync(path.dirname(lp), { recursive: true });
560
- fs.writeFileSync(
561
- lp,
562
- JSON.stringify({ pid: 1, command: "reindex", startedAt: new Date(0).toISOString(), hqRoot: root }),
563
- );
564
- // Even with a live reindex holder on record, the internal caller (which
565
- // serializes reindex itself at the tail of a pass) runs to completion.
566
- const { status } = reindex({ repoRoot: root, skipLock: true });
567
- expect(status).toBe(0);
568
- expect(fs.existsSync(path.join(root, ".claude/skills"))).toBe(true);
569
- });
570
-
571
- it("releases the reindex-scope lock after a normal run (no leftover lock file)", () => {
572
- reindex({ repoRoot: root });
573
- expect(fs.existsSync(lockPathFor(root, "reindex"))).toBe(false);
574
- // ...and never created the sync/rescue "operation" lock.
575
- expect(fs.existsSync(lockPathFor(root))).toBe(false);
576
- });
577
-
578
- // ── HQ-B0: wrapper dir name uses ':' which is illegal on Windows ─────────
579
- // `<ns>:<skill>` wrapper dirs contain a colon — fine on macOS/Linux, but a
580
- // reserved drive/ADS separator on Windows, where mkdirSync throws ENOENT.
581
- // A single un-creatable wrapper must not abort the entire reindex run.
582
-
583
- it("does not abort reindex when a namespaced wrapper dir cannot be created (':' illegal on Windows)", () => {
584
- writeSkill("core/skills/demo");
585
- writeSkill("companies/acme/skills/widget");
586
-
587
- hoisted.failNamespacedMkdir = true;
588
- try {
589
- // Without the guard the wrapper mkdir throws ENOENT and aborts the whole
590
- // command; with it, the run completes and the bad wrappers are skipped.
591
- expect(reindex({ repoRoot: root }).status).toBe(0);
592
- } finally {
593
- hoisted.failNamespacedMkdir = false;
594
- }
595
-
596
- // The un-creatable wrappers are skipped (not half-written).
597
- expect(fs.existsSync(path.join(root, ".claude/skills/core:demo"))).toBe(false);
598
- expect(fs.existsSync(path.join(root, ".claude/skills/acme:widget"))).toBe(false);
599
- });
600
-
601
- // ── HQ-B0 (residual): PR #98 guarded only the wrapper mkdir, leaving the
602
- // `.claude/skills` parent and the `core/<type>` mirror mkdirs unguarded.
603
- // A failure at EITHER must also degrade gracefully, not abort reindex.
604
- it("does not abort reindex when the .claude/skills parent mkdir fails", () => {
605
- writeSkill("core/skills/demo");
606
-
607
- hoisted.failMkdirContaining = path.join(".claude", "skills");
608
- try {
609
- // The parent mkdir fails AND every recursive wrapper mkdir under it fails,
610
- // so nothing is surfaced — but reindex must still complete cleanly.
611
- expect(reindex({ repoRoot: root }).status).toBe(0);
612
- } finally {
613
- hoisted.failMkdirContaining = "";
614
- }
615
- });
616
-
617
- // ── HQ-CLI-4: symlink creation is privileged on Windows. Without Developer
618
- // Mode, fs.symlinkSync throws EPERM. The HQ-B0 work guarded every mkdir
619
- // site but left the symlink sites unguarded, so `hq reindex` aborted on
620
- // Windows. safeBridgeLink must degrade gracefully like safeMkdir.
621
- it("does not abort reindex when a skill-wrapper symlink fails with EPERM (Windows no Developer Mode)", () => {
622
- writeSkill("core/skills/demo");
623
-
624
- hoisted.failSymlinkContaining = path.join(".claude", "skills");
625
- try {
626
- // Every per-file wrapper symlink throws EPERM, but reindex must still
627
- // complete cleanly (exit 0) rather than crash + flood Sentry.
628
- expect(reindex({ repoRoot: root }).status).toBe(0);
629
- } finally {
630
- hoisted.failSymlinkContaining = "";
631
- }
632
-
633
- // The wrapper DIR was still created.
634
- expect(fs.existsSync(path.join(root, ".claude/skills/core:demo"))).toBe(true);
635
- });
636
-
637
- // ── Windows symlink fallback: when symlink creation fails with EPERM, the
638
- // bridge must NOT be left empty (that made bridged skills silently never
639
- // surface as slash commands). Fall back to a hardlink so the wrapper still
640
- // contains a working, in-sync copy of every source entry.
641
- it("falls back to a hardlink when a skill-wrapper symlink fails with EPERM", () => {
642
- writeSkill("core/skills/demo");
643
- fs.writeFileSync(path.join(root, "core/skills/demo/helper.md"), "help\n");
644
-
645
- hoisted.failSymlinkContaining = path.join(".claude", "skills");
646
- try {
647
- expect(reindex({ repoRoot: root }).status).toBe(0);
648
- } finally {
649
- hoisted.failSymlinkContaining = "";
650
- }
651
-
652
- const wrapper = path.join(root, ".claude/skills/core:demo");
653
- const skillLink = path.join(wrapper, "SKILL.md");
654
- const helperLink = path.join(wrapper, "helper.md");
655
- // Every source entry is bridged (not skipped), so the skill surfaces.
656
- expect(fs.existsSync(skillLink)).toBe(true);
657
- expect(fs.existsSync(helperLink)).toBe(true);
658
- // It is a hardlink, not a symlink: same content, same inode as the source,
659
- // and readable without the symlink privilege the platform denied.
660
- const st = fs.lstatSync(skillLink);
661
- expect(st.isSymbolicLink()).toBe(false);
662
- expect(fs.readFileSync(skillLink, "utf8")).toBe("skill\n");
663
- expect(st.ino).toBe(fs.statSync(path.join(root, "core/skills/demo/SKILL.md")).ino);
664
- });
665
-
666
- // ── Repair: an older build left an EMPTY wrapper dir (mkdir succeeded, every
667
- // symlink was skipped). A later reindex — even one still unable to symlink —
668
- // must repair it by bridging the missing entries via the fallback.
669
- it("repairs a pre-existing empty bridge directory on the next run", () => {
670
- writeSkill("core/skills/demo");
671
- // Simulate the stale state: the wrapper dir exists but is empty.
672
- const wrapper = path.join(root, ".claude/skills/core:demo");
673
- fs.mkdirSync(wrapper, { recursive: true });
674
- expect(fs.readdirSync(wrapper)).toHaveLength(0);
675
-
676
- hoisted.failSymlinkContaining = path.join(".claude", "skills");
677
- try {
678
- expect(reindex({ repoRoot: root }).status).toBe(0);
679
- } finally {
680
- hoisted.failSymlinkContaining = "";
681
- }
682
-
683
- // The empty bridge is now populated with a working entry.
684
- expect(fs.existsSync(path.join(wrapper, "SKILL.md"))).toBe(true);
685
- expect(fs.readFileSync(path.join(wrapper, "SKILL.md"), "utf8")).toBe("skill\n");
686
- });
687
-
688
- // --- multi-harness session-log capture into workspace/.session-logs/<harness>/
689
- describe("session-log capture", () => {
690
- let claudeDir: string; // CLAUDE_CONFIG_DIR
691
- let codexHome: string; // CODEX_HOME
692
- let grokHome: string; // GROK_HOME
693
-
694
- // Per-harness cwd encodings the harnesses actually use.
695
- const claudeSlug = (p: string): string => p.replace(/[^a-zA-Z0-9]/g, "-");
696
- const grokKey = (p: string): string => encodeURIComponent(p);
697
-
698
- const dest = (reindexRoot = root): string =>
699
- path.join(reindexRoot, "workspace", ".session-logs");
700
-
701
- beforeEach(() => {
702
- claudeDir = fs.mkdtempSync(path.join(os.tmpdir(), "ms-claude-"));
703
- codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "ms-codex-"));
704
- grokHome = fs.mkdtempSync(path.join(os.tmpdir(), "ms-grok-"));
705
- process.env.CLAUDE_CONFIG_DIR = claudeDir;
706
- process.env.CODEX_HOME = codexHome;
707
- process.env.GROK_HOME = grokHome;
708
- });
709
-
710
- afterEach(() => {
711
- for (const d of [claudeDir, codexHome, grokHome]) {
712
- fs.rmSync(d, { recursive: true, force: true });
713
- }
714
- delete process.env.CLAUDE_CONFIG_DIR;
715
- delete process.env.CODEX_HOME;
716
- delete process.env.GROK_HOME;
717
- });
718
-
719
- // Seed a Claude project folder for cwd `cwdPath`, return its dest dir name.
720
- function seedClaude(cwdPath: string, body = "claude\n"): string {
721
- const name = claudeSlug(cwdPath);
722
- const projDir = path.join(claudeDir, "projects", name);
723
- fs.mkdirSync(path.join(projDir, "tool-results"), { recursive: true });
724
- fs.writeFileSync(path.join(projDir, "session.jsonl"), body);
725
- fs.writeFileSync(path.join(projDir, "tool-results", "r.txt"), "res\n");
726
- return name;
727
- }
728
-
729
- // Seed a Grok session folder for cwd `cwdPath`, return its dest dir name.
730
- function seedGrok(cwdPath: string, body = "grok\n"): string {
731
- const name = grokKey(cwdPath);
732
- const sess = path.join(grokHome, "sessions", name, "uuid-1");
733
- fs.mkdirSync(sess, { recursive: true });
734
- fs.writeFileSync(path.join(sess, "chat_history.jsonl"), body);
735
- return name;
736
- }
737
-
738
- // Seed a Codex rollout at YYYY/MM/DD/<file> whose session_meta records `cwd`.
739
- function seedCodex(cwd: string, date: [string, string, string], file: string): string {
740
- const [y, m, d] = date;
741
- const dir = path.join(codexHome, "sessions", y, m, d);
742
- fs.mkdirSync(dir, { recursive: true });
743
- const meta = JSON.stringify({ type: "session_meta", payload: { cwd } });
744
- const event = JSON.stringify({ type: "event_msg", payload: { text: "hi" } });
745
- const abs = path.join(dir, file);
746
- fs.writeFileSync(abs, `${meta}\n${event}\n`);
747
- return abs;
748
- }
749
-
750
- // Build the real root under a canonical tmp path so this fixture remains
751
- // stable even when the platform itself exposes its temp directory via a
752
- // symlink (for example, /tmp -> /private/tmp on macOS).
753
- function makeSymlinkRoot(): { realRoot: string; linkRoot: string; cleanup: () => void } | null {
754
- const fixtureDir = fs.mkdtempSync(
755
- path.join(fs.realpathSync.native(os.tmpdir()), "ms-session-log-alias-"),
756
- );
757
- const realRoot = path.join(fixtureDir, "real-hq");
758
- const linkRoot = path.join(fixtureDir, "hq-alias");
759
- fs.mkdirSync(realRoot);
760
- try {
761
- fs.symlinkSync(realRoot, linkRoot, "dir");
762
- } catch (err) {
763
- fs.rmSync(fixtureDir, { recursive: true, force: true });
764
- if ((err as NodeJS.ErrnoException).code === "EPERM") return null;
765
- throw err;
766
- }
767
- return {
768
- realRoot,
769
- linkRoot,
770
- cleanup: () => fs.rmSync(fixtureDir, { recursive: true, force: true }),
771
- };
772
- }
773
-
774
- // ── Claude ──────────────────────────────────────────────────────────────
775
- it("claude: captures the HQ-root project AND descendant projects under claude/", () => {
776
- const rootName = seedClaude(root);
777
- const subName = seedClaude(path.join(root, "workspace", "worktrees", "x"));
778
- // An unrelated project (different root) must NOT be captured.
779
- seedClaude("/some/other/place");
780
-
781
- expect(reindex({ repoRoot: root }).status).toBe(0);
782
-
783
- const c = path.join(dest(), "claude");
784
- expect(fs.readFileSync(path.join(c, rootName, "session.jsonl"), "utf8")).toBe("claude\n");
785
- expect(fs.existsSync(path.join(c, subName, "session.jsonl"))).toBe(true);
786
- expect(fs.existsSync(path.join(c, claudeSlug("/some/other/place")))).toBe(false);
787
- // Namespaced: claude logs are NOT dumped at the .session-logs root anymore.
788
- expect(fs.existsSync(path.join(dest(), "session.jsonl"))).toBe(false);
789
- });
790
-
791
- it("claude: overwrites an existing dest file but never prunes dest-only entries", () => {
792
- const rootName = seedClaude(root, "fresh\n");
793
- const c = path.join(dest(), "claude");
794
- fs.mkdirSync(path.join(c, rootName), { recursive: true });
795
- fs.writeFileSync(path.join(c, rootName, "session.jsonl"), "STALE\n");
796
- fs.writeFileSync(path.join(dest(), "orphan.txt"), "keep\n");
797
-
798
- expect(reindex({ repoRoot: root }).status).toBe(0);
799
-
800
- expect(fs.readFileSync(path.join(c, rootName, "session.jsonl"), "utf8")).toBe("fresh\n");
801
- expect(fs.readFileSync(path.join(dest(), "orphan.txt"), "utf8")).toBe("keep\n");
802
- });
803
-
804
- it("claude: captures a realpath-keyed project when reindex uses a symlink alias", () => {
805
- const fixture = makeSymlinkRoot();
806
- if (!fixture) return; // Symlink-restricted host (for example, Windows without Developer Mode).
807
- try {
808
- const realName = seedClaude(fixture.realRoot);
809
- const aliasName = claudeSlug(fixture.linkRoot);
810
-
811
- expect(reindex({ repoRoot: fixture.linkRoot }).status).toBe(0);
812
-
813
- const c = path.join(dest(fixture.realRoot), "claude");
814
- expect(fs.readFileSync(path.join(c, realName, "session.jsonl"), "utf8")).toBe("claude\n");
815
- expect(fs.existsSync(path.join(c, aliasName))).toBe(false);
816
- } finally {
817
- fixture.cleanup();
818
- }
819
- });
820
-
821
- // ── Grok ────────────────────────────────────────────────────────────────
822
- it("grok: captures the HQ-root and descendant cwd folders under grok/, excludes others", () => {
823
- const rootName = seedGrok(root);
824
- const subName = seedGrok(path.join(root, "repos", "private", "x"));
825
- seedGrok("/tmp/benchmark"); // unrelated cwd → excluded
826
-
827
- expect(reindex({ repoRoot: root }).status).toBe(0);
828
-
829
- const g = path.join(dest(), "grok");
830
- expect(fs.readFileSync(path.join(g, rootName, "uuid-1", "chat_history.jsonl"), "utf8")).toBe("grok\n");
831
- expect(fs.existsSync(path.join(g, subName, "uuid-1", "chat_history.jsonl"))).toBe(true);
832
- expect(fs.existsSync(path.join(g, grokKey("/tmp/benchmark")))).toBe(false);
833
- });
834
-
835
- it("grok: captures a realpath-keyed session when reindex uses a symlink alias", () => {
836
- const fixture = makeSymlinkRoot();
837
- if (!fixture) return; // Symlink-restricted host (for example, Windows without Developer Mode).
838
- try {
839
- const realName = seedGrok(fixture.realRoot);
840
-
841
- expect(reindex({ repoRoot: fixture.linkRoot }).status).toBe(0);
842
-
843
- expect(
844
- fs.readFileSync(
845
- path.join(dest(fixture.realRoot), "grok", realName, "uuid-1", "chat_history.jsonl"),
846
- "utf8",
847
- ),
848
- ).toBe("grok\n");
849
- } finally {
850
- fixture.cleanup();
851
- }
852
- });
853
-
854
- // ── Codex ───────────────────────────────────────────────────────────────
855
- it("codex: captures rollouts whose recorded cwd is the HQ tree, mirroring the date path", () => {
856
- seedCodex(root, ["2026", "07", "13"], "rollout-root.jsonl");
857
- seedCodex(path.join(root, "workspace", "worktrees", "y"), ["2026", "07", "13"], "rollout-sub.jsonl");
858
- seedCodex("/elsewhere/repo", ["2026", "07", "13"], "rollout-other.jsonl");
859
-
860
- expect(reindex({ repoRoot: root }).status).toBe(0);
861
-
862
- const cx = path.join(dest(), "codex", "2026", "07", "13");
863
- expect(fs.existsSync(path.join(cx, "rollout-root.jsonl"))).toBe(true);
864
- expect(fs.existsSync(path.join(cx, "rollout-sub.jsonl"))).toBe(true);
865
- // A rollout run from outside the HQ tree is never captured (tenant scope).
866
- expect(fs.existsSync(path.join(cx, "rollout-other.jsonl"))).toBe(false);
867
- });
868
-
869
- it("codex: captures a realpath-keyed rollout when reindex uses a symlink alias", () => {
870
- const fixture = makeSymlinkRoot();
871
- if (!fixture) return; // Symlink-restricted host (for example, Windows without Developer Mode).
872
- try {
873
- seedCodex(path.join(fixture.realRoot, "workspace", "worktrees", "x"), ["2026", "07", "13"], "rollout-real.jsonl");
874
-
875
- expect(reindex({ repoRoot: fixture.linkRoot }).status).toBe(0);
876
-
877
- expect(
878
- fs.existsSync(
879
- path.join(dest(fixture.realRoot), "codex", "2026", "07", "13", "rollout-real.jsonl"),
880
- ),
881
- ).toBe(true);
882
- } finally {
883
- fixture.cleanup();
884
- }
885
- });
886
-
887
- it("codex: skips a rollout already captured and unchanged (mtime guard)", () => {
888
- const src = seedCodex(root, ["2026", "07", "13"], "rollout-a.jsonl");
889
- expect(reindex({ repoRoot: root }).status).toBe(0);
890
-
891
- // Tamper the dest copy and push its mtime into the future; an unchanged
892
- // src must be skipped, leaving the sentinel in place.
893
- const destFile = path.join(dest(), "codex", "2026", "07", "13", "rollout-a.jsonl");
894
- fs.writeFileSync(destFile, "SENTINEL\n");
895
- const future = Date.now() / 1000 + 3600;
896
- fs.utimesSync(destFile, future, future);
897
-
898
- expect(reindex({ repoRoot: root }).status).toBe(0);
899
- expect(fs.readFileSync(destFile, "utf8")).toBe("SENTINEL\n");
900
-
901
- // But when the src is newer, it IS re-copied (overwriting the sentinel).
902
- fs.writeFileSync(src, `${JSON.stringify({ type: "session_meta", payload: { cwd: root } })}\nNEW\n`);
903
- const soon = Date.now() / 1000 + 7200;
904
- fs.utimesSync(src, soon, soon);
905
- expect(reindex({ repoRoot: root }).status).toBe(0);
906
- expect(fs.readFileSync(destFile, "utf8")).toContain("NEW");
907
- });
908
-
909
- it("captures exact-root and descendant entries once when the root is already canonical", () => {
910
- const canonicalRoot = fs.realpathSync.native(root);
911
- const claudeNames = [
912
- seedClaude(canonicalRoot),
913
- seedClaude(path.join(canonicalRoot, "workspace", "worktrees", "x")),
914
- ];
915
- const grokNames = [
916
- seedGrok(canonicalRoot),
917
- seedGrok(path.join(canonicalRoot, "repos", "private", "x")),
918
- ];
919
- seedCodex(canonicalRoot, ["2026", "07", "15"], "rollout-root.jsonl");
920
- seedCodex(
921
- path.join(canonicalRoot, "workspace", "worktrees", "x"),
922
- ["2026", "07", "15"],
923
- "rollout-sub.jsonl",
924
- );
925
-
926
- expect(reindex({ repoRoot: canonicalRoot }).status).toBe(0);
927
-
928
- expect(fs.readdirSync(path.join(dest(canonicalRoot), "claude")).sort()).toEqual(
929
- claudeNames.sort(),
930
- );
931
- expect(fs.readdirSync(path.join(dest(canonicalRoot), "grok")).sort()).toEqual(grokNames.sort());
932
- expect(fs.readdirSync(path.join(dest(canonicalRoot), "codex", "2026", "07", "15")).sort()).toEqual([
933
- "rollout-root.jsonl",
934
- "rollout-sub.jsonl",
935
- ]);
936
- });
937
-
938
- // ── Cross-harness ─────────────────────────────────────────────────────────
939
- it("all three harnesses land side-by-side under their own subfolders", () => {
940
- seedClaude(root);
941
- seedGrok(root);
942
- seedCodex(root, ["2026", "07", "14"], "rollout-z.jsonl");
943
-
944
- expect(reindex({ repoRoot: root }).status).toBe(0);
945
-
946
- expect(fs.existsSync(path.join(dest(), "claude", claudeSlug(root), "session.jsonl"))).toBe(true);
947
- expect(fs.existsSync(path.join(dest(), "grok", grokKey(root), "uuid-1", "chat_history.jsonl"))).toBe(true);
948
- expect(fs.existsSync(path.join(dest(), "codex", "2026", "07", "14", "rollout-z.jsonl"))).toBe(true);
949
- });
950
-
951
- it("no-ops (still exit 0) when no harness has logs for this root", () => {
952
- // Home dirs exist but are empty → nothing to capture, no dest created.
953
- expect(reindex({ repoRoot: root }).status).toBe(0);
954
- expect(fs.existsSync(path.join(root, "workspace", ".session-logs"))).toBe(false);
955
- });
956
- });
957
- });