@indigoai-us/hq-cloud 6.14.50 → 6.15.1

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 (340) hide show
  1. package/dist/bin/sync-mutation.d.ts +14 -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 +64 -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/reindex-knowledge.d.ts +7 -0
  34. package/dist/cli/reindex-knowledge.d.ts.map +1 -0
  35. package/dist/cli/reindex-knowledge.js +389 -0
  36. package/dist/cli/reindex-knowledge.js.map +1 -0
  37. package/dist/cli/reindex-knowledge.test.d.ts +12 -0
  38. package/dist/cli/reindex-knowledge.test.d.ts.map +1 -0
  39. package/dist/cli/reindex-knowledge.test.js +260 -0
  40. package/dist/cli/reindex-knowledge.test.js.map +1 -0
  41. package/dist/cli/reindex.d.ts.map +1 -1
  42. package/dist/cli/reindex.js +8 -0
  43. package/dist/cli/reindex.js.map +1 -1
  44. package/dist/cli/rescue-settings-reconcile.test.js +47 -0
  45. package/dist/cli/rescue-settings-reconcile.test.js.map +1 -1
  46. package/dist/cli/sync.d.ts +27 -0
  47. package/dist/cli/sync.d.ts.map +1 -1
  48. package/dist/cli/sync.js +52 -0
  49. package/dist/cli/sync.js.map +1 -1
  50. package/dist/cli/sync.test.js +31 -1
  51. package/dist/cli/sync.test.js.map +1 -1
  52. package/dist/index.d.ts +2 -0
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +1 -0
  55. package/dist/index.js.map +1 -1
  56. package/dist/skill-telemetry.d.ts +6 -0
  57. package/dist/skill-telemetry.d.ts.map +1 -1
  58. package/dist/skill-telemetry.js +14 -2
  59. package/dist/skill-telemetry.js.map +1 -1
  60. package/dist/skill-telemetry.test.js +79 -0
  61. package/dist/skill-telemetry.test.js.map +1 -1
  62. package/dist/sync/candidate-uploader.d.ts +88 -0
  63. package/dist/sync/candidate-uploader.d.ts.map +1 -0
  64. package/dist/sync/candidate-uploader.js +212 -0
  65. package/dist/sync/candidate-uploader.js.map +1 -0
  66. package/dist/sync/candidate-uploader.test.d.ts +2 -0
  67. package/dist/sync/candidate-uploader.test.d.ts.map +1 -0
  68. package/dist/sync/candidate-uploader.test.js +132 -0
  69. package/dist/sync/candidate-uploader.test.js.map +1 -0
  70. package/dist/sync/delta-client.d.ts +73 -0
  71. package/dist/sync/delta-client.d.ts.map +1 -0
  72. package/dist/sync/delta-client.js +201 -0
  73. package/dist/sync/delta-client.js.map +1 -0
  74. package/dist/sync/delta-client.test.d.ts +2 -0
  75. package/dist/sync/delta-client.test.d.ts.map +1 -0
  76. package/dist/sync/delta-client.test.js +97 -0
  77. package/dist/sync/delta-client.test.js.map +1 -0
  78. package/dist/sync/durable-apply.d.ts +76 -0
  79. package/dist/sync/durable-apply.d.ts.map +1 -0
  80. package/dist/sync/durable-apply.js +530 -0
  81. package/dist/sync/durable-apply.js.map +1 -0
  82. package/dist/sync/durable-apply.test.d.ts +2 -0
  83. package/dist/sync/durable-apply.test.d.ts.map +1 -0
  84. package/dist/sync/durable-apply.test.js +180 -0
  85. package/dist/sync/durable-apply.test.js.map +1 -0
  86. package/dist/sync/event-sync.d.ts +33 -1
  87. package/dist/sync/event-sync.d.ts.map +1 -1
  88. package/dist/sync/event-sync.js +149 -1
  89. package/dist/sync/event-sync.js.map +1 -1
  90. package/dist/sync/event-sync.test.js +142 -1
  91. package/dist/sync/event-sync.test.js.map +1 -1
  92. package/dist/sync/index.d.ts +2 -0
  93. package/dist/sync/index.d.ts.map +1 -1
  94. package/dist/sync/index.js +1 -0
  95. package/dist/sync/index.js.map +1 -1
  96. package/dist/sync/multipart-uploader.d.ts +99 -0
  97. package/dist/sync/multipart-uploader.d.ts.map +1 -0
  98. package/dist/sync/multipart-uploader.js +447 -0
  99. package/dist/sync/multipart-uploader.js.map +1 -0
  100. package/dist/sync/multipart-uploader.test.d.ts +2 -0
  101. package/dist/sync/multipart-uploader.test.d.ts.map +1 -0
  102. package/dist/sync/multipart-uploader.test.js +119 -0
  103. package/dist/sync/multipart-uploader.test.js.map +1 -0
  104. package/dist/sync/mutation-client.d.ts +82 -0
  105. package/dist/sync/mutation-client.d.ts.map +1 -0
  106. package/dist/sync/mutation-client.js +221 -0
  107. package/dist/sync/mutation-client.js.map +1 -0
  108. package/dist/sync/mutation-client.test.d.ts +2 -0
  109. package/dist/sync/mutation-client.test.d.ts.map +1 -0
  110. package/dist/sync/mutation-client.test.js +51 -0
  111. package/dist/sync/mutation-client.test.js.map +1 -0
  112. package/dist/sync/push-receiver.d.ts +45 -0
  113. package/dist/sync/push-receiver.d.ts.map +1 -1
  114. package/dist/sync/push-receiver.js +101 -0
  115. package/dist/sync/push-receiver.js.map +1 -1
  116. package/dist/sync/push-receiver.test.js +54 -2
  117. package/dist/sync/push-receiver.test.js.map +1 -1
  118. package/dist/sync/scope-inventory-client.d.ts +69 -0
  119. package/dist/sync/scope-inventory-client.d.ts.map +1 -0
  120. package/dist/sync/scope-inventory-client.js +210 -0
  121. package/dist/sync/scope-inventory-client.js.map +1 -0
  122. package/dist/sync/scope-inventory-client.test.d.ts +2 -0
  123. package/dist/sync/scope-inventory-client.test.d.ts.map +1 -0
  124. package/dist/sync/scope-inventory-client.test.js +94 -0
  125. package/dist/sync/scope-inventory-client.test.js.map +1 -0
  126. package/dist/sync/snapshot-client.d.ts +98 -0
  127. package/dist/sync/snapshot-client.d.ts.map +1 -0
  128. package/dist/sync/snapshot-client.js +402 -0
  129. package/dist/sync/snapshot-client.js.map +1 -0
  130. package/dist/sync/snapshot-client.test.d.ts +2 -0
  131. package/dist/sync/snapshot-client.test.d.ts.map +1 -0
  132. package/dist/sync/snapshot-client.test.js +169 -0
  133. package/dist/sync/snapshot-client.test.js.map +1 -0
  134. package/dist/sync/uploader-finalization.d.ts +97 -0
  135. package/dist/sync/uploader-finalization.d.ts.map +1 -0
  136. package/dist/sync/uploader-finalization.js +273 -0
  137. package/dist/sync/uploader-finalization.js.map +1 -0
  138. package/dist/sync/uploader-finalization.test.d.ts +2 -0
  139. package/dist/sync/uploader-finalization.test.d.ts.map +1 -0
  140. package/dist/sync/uploader-finalization.test.js +92 -0
  141. package/dist/sync/uploader-finalization.test.js.map +1 -0
  142. package/dist/telemetry.d.ts +11 -1
  143. package/dist/telemetry.d.ts.map +1 -1
  144. package/dist/telemetry.js +21 -2
  145. package/dist/telemetry.js.map +1 -1
  146. package/dist/telemetry.test.js +80 -0
  147. package/dist/telemetry.test.js.map +1 -1
  148. package/package.json +6 -1
  149. package/.claude/policies/hq-cloud-esm-cannot-spy-fs-builtins.md +0 -30
  150. package/.claude/policies/hq-cloud-strip-types-no-parameter-properties.md +0 -22
  151. package/.github/workflows/ci.yml +0 -84
  152. package/.github/workflows/publish.yml +0 -56
  153. package/.github/workflows/unreleased-commits-nag.yml +0 -256
  154. package/eslint.config.js +0 -67
  155. package/pnpm-workspace.yaml +0 -2
  156. package/scripts/presign-transport-e2e.mjs +0 -250
  157. package/scripts/vault-rebaseline.sh +0 -323
  158. package/scripts/vault-rescue.sh +0 -332
  159. package/src/active-company.test.ts +0 -188
  160. package/src/active-company.ts +0 -168
  161. package/src/agent-codex-instructions.test.ts +0 -332
  162. package/src/agent-codex-instructions.ts +0 -309
  163. package/src/auth.ts +0 -146
  164. package/src/backup-prune.test.ts +0 -98
  165. package/src/backup-prune.ts +0 -182
  166. package/src/bin/backup-prune-runner.ts +0 -33
  167. package/src/bin/rescue-runner.ts +0 -25
  168. package/src/bin/sync-runner-company.ts +0 -695
  169. package/src/bin/sync-runner-events.test.ts +0 -143
  170. package/src/bin/sync-runner-events.ts +0 -55
  171. package/src/bin/sync-runner-planning.test.ts +0 -311
  172. package/src/bin/sync-runner-planning.ts +0 -258
  173. package/src/bin/sync-runner-rollup.test.ts +0 -37
  174. package/src/bin/sync-runner-rollup.ts +0 -97
  175. package/src/bin/sync-runner-telemetry.ts +0 -15
  176. package/src/bin/sync-runner-watch-loop.ts +0 -1235
  177. package/src/bin/sync-runner-watch-routes.test.ts +0 -71
  178. package/src/bin/sync-runner-watch-routes.ts +0 -184
  179. package/src/bin/sync-runner.test.ts +0 -8767
  180. package/src/bin/sync-runner.ts +0 -2190
  181. package/src/cli/accept.ts +0 -124
  182. package/src/cli/conflict.ts +0 -119
  183. package/src/cli/doctor.test.ts +0 -581
  184. package/src/cli/doctor.ts +0 -642
  185. package/src/cli/index.ts +0 -49
  186. package/src/cli/invite.test.ts +0 -250
  187. package/src/cli/invite.ts +0 -214
  188. package/src/cli/promote.ts +0 -157
  189. package/src/cli/reindex.test.ts +0 -957
  190. package/src/cli/reindex.ts +0 -970
  191. package/src/cli/rescue-classify-ordering.test.ts +0 -548
  192. package/src/cli/rescue-clone-diagnostics.test.ts +0 -120
  193. package/src/cli/rescue-core.ts +0 -3011
  194. package/src/cli/rescue-drift-reconcile.test.ts +0 -179
  195. package/src/cli/rescue-drop-dir-symlink.test.ts +0 -224
  196. package/src/cli/rescue-exec-bit-preserve.test.ts +0 -187
  197. package/src/cli/rescue-hq-root-guard.test.ts +0 -232
  198. package/src/cli/rescue-journal-reconcile.test.ts +0 -215
  199. package/src/cli/rescue-mtime-preserve.test.ts +0 -203
  200. package/src/cli/rescue-settings-reconcile.test.ts +0 -583
  201. package/src/cli/rescue-snapshot.test.ts +0 -57
  202. package/src/cli/rescue-snapshot.ts +0 -51
  203. package/src/cli/rescue.reindex.test.ts +0 -63
  204. package/src/cli/rescue.test.ts +0 -131
  205. package/src/cli/rescue.ts +0 -182
  206. package/src/cli/share.test.ts +0 -7843
  207. package/src/cli/share.ts +0 -3663
  208. package/src/cli/sync-scope.test.ts +0 -652
  209. package/src/cli/sync.test.ts +0 -5207
  210. package/src/cli/sync.ts +0 -3470
  211. package/src/cli/tombstones.ts +0 -106
  212. package/src/cli/watch-event-push-conflict.test.ts +0 -234
  213. package/src/client-info.test.ts +0 -214
  214. package/src/client-info.ts +0 -121
  215. package/src/cognito-auth.test.ts +0 -712
  216. package/src/cognito-auth.ts +0 -1422
  217. package/src/company-resolver.test.ts +0 -618
  218. package/src/company-resolver.ts +0 -521
  219. package/src/context.test.ts +0 -583
  220. package/src/context.ts +0 -378
  221. package/src/daemon-worker.ts +0 -26
  222. package/src/daemon.ts +0 -99
  223. package/src/entity-resolver.test.ts +0 -315
  224. package/src/entity-resolver.ts +0 -180
  225. package/src/ignore.test.ts +0 -466
  226. package/src/ignore.ts +0 -469
  227. package/src/index.ts +0 -439
  228. package/src/journal.test.ts +0 -968
  229. package/src/journal.ts +0 -765
  230. package/src/lib/cloud-authoritative.test.ts +0 -45
  231. package/src/lib/cloud-authoritative.ts +0 -59
  232. package/src/lib/conflict-file.ts +0 -86
  233. package/src/lib/conflict-index.ts +0 -289
  234. package/src/lib/conflict.test.ts +0 -348
  235. package/src/lib/describe-error.test.ts +0 -100
  236. package/src/lib/describe-error.ts +0 -58
  237. package/src/lib/exit-codes.ts +0 -24
  238. package/src/lib/machine-id.test.ts +0 -231
  239. package/src/lib/machine-id.ts +0 -175
  240. package/src/lib/net-errors.test.ts +0 -65
  241. package/src/lib/net-errors.ts +0 -86
  242. package/src/lib/readlink-safe.test.ts +0 -43
  243. package/src/lib/readlink-safe.ts +0 -29
  244. package/src/local-path-codec.test.ts +0 -138
  245. package/src/local-path-codec.ts +0 -161
  246. package/src/machine-auth.test.ts +0 -1323
  247. package/src/manifest-reconcile.test.ts +0 -1123
  248. package/src/manifest-reconcile.ts +0 -518
  249. package/src/object-io.test.ts +0 -1221
  250. package/src/object-io.ts +0 -1306
  251. package/src/operation-lock.test.ts +0 -484
  252. package/src/operation-lock.ts +0 -680
  253. package/src/outcome-telemetry.test.ts +0 -498
  254. package/src/outcome-telemetry.ts +0 -639
  255. package/src/personal-vault-exclusions.test.ts +0 -308
  256. package/src/personal-vault-exclusions.ts +0 -354
  257. package/src/personal-vault.test.ts +0 -756
  258. package/src/personal-vault.ts +0 -496
  259. package/src/prefix-coalesce.test.ts +0 -240
  260. package/src/prefix-coalesce.ts +0 -273
  261. package/src/public-surface.test.ts +0 -117
  262. package/src/qmd-reindex.test.ts +0 -877
  263. package/src/qmd-reindex.ts +0 -842
  264. package/src/read-only-state-dir.test.ts +0 -188
  265. package/src/remote-pull.test.ts +0 -1130
  266. package/src/remote-pull.ts +0 -618
  267. package/src/s3.symlink-materialize.test.ts +0 -492
  268. package/src/s3.test.ts +0 -1789
  269. package/src/s3.ts +0 -1532
  270. package/src/schemas/signal-types.test.ts +0 -82
  271. package/src/schemas/signal-types.ts +0 -38
  272. package/src/schemas/source-channels.test.ts +0 -82
  273. package/src/schemas/source-channels.ts +0 -53
  274. package/src/scope-shrink.test.ts +0 -633
  275. package/src/scope-shrink.ts +0 -481
  276. package/src/signals/get.test.ts +0 -310
  277. package/src/signals/get.ts +0 -75
  278. package/src/signals/internals.ts +0 -195
  279. package/src/signals/list.test.ts +0 -420
  280. package/src/signals/list.ts +0 -79
  281. package/src/signals/parse.ts +0 -8
  282. package/src/signals/types.ts +0 -91
  283. package/src/skill-telemetry.test.ts +0 -1825
  284. package/src/skill-telemetry.ts +0 -1439
  285. package/src/sources/get.test.ts +0 -293
  286. package/src/sources/get.ts +0 -66
  287. package/src/sources/internals.ts +0 -198
  288. package/src/sources/list.test.ts +0 -402
  289. package/src/sources/list.ts +0 -84
  290. package/src/sources/parse.ts +0 -43
  291. package/src/sources/types.ts +0 -84
  292. package/src/sync/event-sync.test.ts +0 -594
  293. package/src/sync/event-sync.ts +0 -545
  294. package/src/sync/feature-flags.test.ts +0 -378
  295. package/src/sync/feature-flags.ts +0 -62
  296. package/src/sync/index.ts +0 -76
  297. package/src/sync/lease-client.test.ts +0 -128
  298. package/src/sync/lease-client.ts +0 -207
  299. package/src/sync/logger.test.ts +0 -242
  300. package/src/sync/logger.ts +0 -79
  301. package/src/sync/metrics.test.ts +0 -462
  302. package/src/sync/metrics.ts +0 -213
  303. package/src/sync/pull-scope.ts +0 -265
  304. package/src/sync/push-event.test.ts +0 -266
  305. package/src/sync/push-event.ts +0 -224
  306. package/src/sync/push-receiver.test.ts +0 -566
  307. package/src/sync/push-receiver.ts +0 -1048
  308. package/src/sync/push-transport.ts +0 -231
  309. package/src/sync/realtime-rollout.test.ts +0 -86
  310. package/src/sync/realtime-rollout.ts +0 -262
  311. package/src/sync/state-store.test.ts +0 -194
  312. package/src/sync/state-store.ts +0 -727
  313. package/src/sync-core.ts +0 -58
  314. package/src/sync-progress.test.ts +0 -94
  315. package/src/sync-progress.ts +0 -140
  316. package/src/telemetry-events.test.ts +0 -88
  317. package/src/telemetry-events.ts +0 -205
  318. package/src/telemetry.test.ts +0 -1280
  319. package/src/telemetry.ts +0 -1109
  320. package/src/types.ts +0 -314
  321. package/src/vault-client.test.ts +0 -1380
  322. package/src/vault-client.ts +0 -1694
  323. package/src/version.ts +0 -24
  324. package/src/watch-roots.test.ts +0 -278
  325. package/src/watch-roots.ts +0 -162
  326. package/src/watcher-event-gate.test.ts +0 -212
  327. package/src/watcher.test.ts +0 -1079
  328. package/src/watcher.ts +0 -1741
  329. package/test/e2e/sync/cross-tenant-isolation.test.ts +0 -630
  330. package/test/e2e/sync/skill-telemetry-oversized-transcript.test.ts +0 -124
  331. package/test/e2e/sync/transient-company-leg.test.ts +0 -384
  332. package/test/e2e/sync/windows-unreadable-link-leg.test.ts +0 -191
  333. package/test/e2e/watcher-real-chokidar.test.ts +0 -165
  334. package/test/e2e/watcher-recursive-backend.test.ts +0 -181
  335. package/test/e2e/watcher-scoped-coverage.test.ts +0 -381
  336. package/test/invite-flow.integration.test.ts +0 -244
  337. package/test/joiner-manifest-reconcile.integration.test.ts +0 -322
  338. package/test/share-sync.integration.test.ts +0 -213
  339. package/tsconfig.json +0 -19
  340. package/vitest.config.ts +0 -22
@@ -1,232 +0,0 @@
1
- /**
2
- * Regression test for the rescue HQ-root sanity gate in src/cli/rescue-core.ts.
3
- *
4
- * Bug (DEV-1741): the gate required BOTH `companies/` AND `personal/`. The
5
- * `personal/` directory was only introduced in v15 — a faithful v14.0.0 install
6
- * ships NEITHER `personal/` NOR `core/` (its scaffold lived in top-level dirs
7
- * like `scripts/`, `workers/`, `knowledge/`, plus `.claude/`). So every v14.0.0
8
- * user hit `error: ... missing companies/ or personal/. Aborting.` (exit 3) and
9
- * had no supported upgrade path to v15.
10
- *
11
- * The gate now anchors on `companies/` plus ANY ONE of `.claude/`, `core/`, or
12
- * `personal/`. `.claude/` exists on every release from v14 through v15, so the
13
- * relaxed gate admits the full upgrade range while still rejecting a directory
14
- * that is not an HQ root at all.
15
- *
16
- * Like the sibling drift test, the rescue clones its source from GitHub, so we
17
- * shim `git clone` to a local fixture repo. All runs are `--dry-run` (no
18
- * destructive ops, no backup) and we assert on the gate's behavior (exit code +
19
- * message), not the full overlay.
20
- */
21
- import { describe, it, expect, beforeAll, afterAll } from "vitest";
22
- import { execFileSync } from "child_process";
23
- import * as fs from "fs";
24
- import * as os from "os";
25
- import * as path from "path";
26
- import { isUsableRescueRoot, runRescue } from "./rescue-core.js";
27
-
28
- function hasGit(): boolean {
29
- try {
30
- execFileSync("git", ["--version"], { stdio: "ignore" });
31
- return true;
32
- } catch {
33
- return false;
34
- }
35
- }
36
-
37
- const gitAvailable = hasGit();
38
-
39
- /** Run the rescue in-process, capturing its stdout/stderr. */
40
- function runRescueCapture(argv: string[], env: NodeJS.ProcessEnv) {
41
- let stdout = "";
42
- let stderr = "";
43
- const origOut = process.stdout.write.bind(process.stdout);
44
- const origErr = process.stderr.write.bind(process.stderr);
45
- process.stdout.write = ((chunk: unknown) => {
46
- stdout += String(chunk);
47
- return true;
48
- }) as typeof process.stdout.write;
49
- process.stderr.write = ((chunk: unknown) => {
50
- stderr += String(chunk);
51
- return true;
52
- }) as typeof process.stderr.write;
53
- let status: number;
54
- try {
55
- status = runRescue(argv, { env }).status;
56
- } finally {
57
- process.stdout.write = origOut;
58
- process.stderr.write = origErr;
59
- }
60
- return { status, stdout, stderr };
61
- }
62
-
63
- describe.skipIf(!gitAvailable)("rescue HQ-root sanity gate", () => {
64
- let workDir: string;
65
- let upstream: string;
66
- let shimDir: string;
67
- let floorSha: string;
68
- let env: NodeJS.ProcessEnv;
69
-
70
- const git = (cwd: string, ...args: string[]) =>
71
- execFileSync("git", args, {
72
- cwd,
73
- stdio: ["ignore", "pipe", "pipe"],
74
- env: {
75
- ...process.env,
76
- GIT_AUTHOR_NAME: "t",
77
- GIT_AUTHOR_EMAIL: "t@t",
78
- GIT_COMMITTER_NAME: "t",
79
- GIT_COMMITTER_EMAIL: "t@t",
80
- },
81
- })
82
- .toString()
83
- .trim();
84
-
85
- beforeAll(() => {
86
- workDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-rescue-gate-"));
87
-
88
- // --- Minimal local "upstream" repo (one core file, floor + HEAD). ---
89
- upstream = path.join(workDir, "upstream");
90
- fs.mkdirSync(path.join(upstream, "core"), { recursive: true });
91
- git(workDir, "init", "-b", "main", "upstream");
92
- fs.writeFileSync(path.join(upstream, "core/x.md"), "v1\n");
93
- git(upstream, "add", "-A");
94
- git(upstream, "commit", "-m", "floor");
95
- floorSha = git(upstream, "rev-parse", "HEAD");
96
- fs.writeFileSync(path.join(upstream, "core/x.md"), "v2\n");
97
- git(upstream, "add", "-A");
98
- git(upstream, "commit", "-m", "head");
99
-
100
- // --- git shim: rewrite `git clone <github-url> dest` to the fixture. ---
101
- const realGit =
102
- execFileSync("bash", ["-lc", "command -v git"]).toString().trim() || "/usr/bin/git";
103
- shimDir = path.join(workDir, "shim");
104
- fs.mkdirSync(shimDir, { recursive: true });
105
- const shim = `#!/usr/bin/env bash
106
- if [ "$1" = "clone" ]; then
107
- args=()
108
- for a in "$@"; do
109
- case "$a" in
110
- https://github.com/*) a=${JSON.stringify(upstream)} ;;
111
- esac
112
- args+=("$a")
113
- done
114
- exec ${JSON.stringify(realGit)} "\${args[@]}"
115
- fi
116
- exec ${JSON.stringify(realGit)} "$@"
117
- `;
118
- fs.writeFileSync(path.join(shimDir, "git"), shim, { mode: 0o755 });
119
- env = { ...process.env, PATH: `${shimDir}:${process.env.PATH ?? ""}` };
120
- }, 60_000); // git init + commits can exceed vitest's 10s default under parallel load
121
-
122
- afterAll(() => {
123
- if (workDir) fs.rmSync(workDir, { recursive: true, force: true });
124
- });
125
-
126
- function makeRoot(name: string, dirs: string[]): string {
127
- const root = path.join(workDir, name);
128
- for (const d of dirs) fs.mkdirSync(path.join(root, d), { recursive: true });
129
- return root;
130
- }
131
-
132
- function rescueDry(hqRoot: string) {
133
- return runRescueCapture(
134
- [
135
- "--hq-root", hqRoot,
136
- "--source", "test/repo",
137
- "--ref", "main",
138
- "--floor-sha", floorSha,
139
- "--dry-run",
140
- "--yes",
141
- "--no-backup",
142
- ],
143
- env,
144
- );
145
- }
146
-
147
- it("admits a faithful v14.0.0 root (companies/ + .claude/, NO personal/, NO core/)", () => {
148
- // Exactly the shape that aborted before the fix.
149
- const root = makeRoot("v14", ["companies", ".claude", "repos", "workspace"]);
150
- expect(fs.existsSync(path.join(root, "personal"))).toBe(false);
151
- expect(fs.existsSync(path.join(root, "core"))).toBe(false);
152
-
153
- const r = rescueDry(root);
154
- const out = `${r.stdout}\n${r.stderr}`;
155
- // Must get PAST the gate — not the exit-3 abort.
156
- expect(r.status, out).not.toBe(3);
157
- expect(out).not.toContain("does not look like an HQ root");
158
- expect(r.status, out).toBe(0);
159
- });
160
-
161
- it("admits a v15 root (companies/ + core/ + personal/)", () => {
162
- const root = makeRoot("v15", ["companies", ".claude", "core", "personal", "repos", "workspace"]);
163
- const r = rescueDry(root);
164
- const out = `${r.stdout}\n${r.stderr}`;
165
- expect(r.status, out).toBe(0);
166
- expect(out).not.toContain("does not look like an HQ root");
167
- });
168
-
169
- it("admits a core-only root (companies/ + core/, no .claude/ or personal/)", () => {
170
- const root = makeRoot("coreonly", ["companies", "core"]);
171
- const r = rescueDry(root);
172
- const out = `${r.stdout}\n${r.stderr}`;
173
- expect(r.status, out).not.toBe(3);
174
- expect(out).not.toContain("does not look like an HQ root");
175
- });
176
-
177
- it("still rejects a non-HQ directory (companies/ only — no scaffold marker)", () => {
178
- const root = makeRoot("bare", ["companies"]);
179
- const r = rescueDry(root);
180
- const out = `${r.stdout}\n${r.stderr}`;
181
- expect(r.status, out).toBe(3);
182
- expect(out).toContain("does not look like an HQ root");
183
- expect(out).toContain("need companies/ plus one of .claude/, core/, or personal/");
184
- });
185
-
186
- it("still rejects a directory with a marker but no companies/", () => {
187
- const root = makeRoot("nocompanies", [".claude", "core", "personal"]);
188
- const r = rescueDry(root);
189
- const out = `${r.stdout}\n${r.stderr}`;
190
- expect(r.status, out).toBe(3);
191
- expect(out).toContain("does not look like an HQ root");
192
- });
193
-
194
- // 2026-08-02 Windows field failure: `--hq-root C:` (a path mangled upstream
195
- // of this process) crashed with an uncaught EISDIR from fs.realpathSync on
196
- // every daily background attempt. The gate must fail CLOSED — clean exit 1,
197
- // no throw — and echo the received argv so the log identifies the caller.
198
- it("rejects a bare drive letter --hq-root with a clean exit and argv echo", () => {
199
- const r = rescueDry("C:");
200
- const out = `${r.stdout}\n${r.stderr}`;
201
- expect(r.status, out).toBe(1);
202
- expect(out).toContain("--hq-root must be an absolute path");
203
- expect(out).toContain('"C:"');
204
- expect(out).toContain("argv:");
205
- });
206
-
207
- it("rejects a relative --hq-root with a clean exit", () => {
208
- const r = rescueDry("some/relative/dir");
209
- const out = `${r.stdout}\n${r.stderr}`;
210
- expect(r.status, out).toBe(1);
211
- expect(out).toContain("--hq-root must be an absolute path");
212
- });
213
- });
214
-
215
- describe("isUsableRescueRoot (pure)", () => {
216
- it("rejects bare drive letters under both path flavors", () => {
217
- expect(isUsableRescueRoot("C:", path.win32)).toBe(false);
218
- expect(isUsableRescueRoot("z:", path.win32)).toBe(false);
219
- expect(isUsableRescueRoot("C:", path.posix)).toBe(false);
220
- });
221
-
222
- it("accepts absolute roots for the matching platform", () => {
223
- expect(isUsableRescueRoot("C:\\Users\\caio\\HQ", path.win32)).toBe(true);
224
- expect(isUsableRescueRoot("/Users/caio/HQ", path.posix)).toBe(true);
225
- });
226
-
227
- it("rejects relative and drive-relative paths", () => {
228
- expect(isUsableRescueRoot("Users\\caio\\HQ", path.win32)).toBe(false);
229
- expect(isUsableRescueRoot("C:Users\\caio\\HQ", path.win32)).toBe(false);
230
- expect(isUsableRescueRoot("some/dir", path.posix)).toBe(false);
231
- });
232
- });
@@ -1,215 +0,0 @@
1
- /**
2
- * Regression for the rescue <-> sync-journal interaction (6.2.4 semantics).
3
- *
4
- * History: 6.2.1/6.2.3 "reconciled" the journal after a rescue by re-stamping
5
- * changed scaffold entries to the current local hash. That MASKED the rescue's
6
- * changes from the sync engine: the push leg saw localChanged=false and never
7
- * uploaded the regenerated scaffold, so the vault silently kept stale bytes
8
- * while the journal claimed convergence (verified live 2026-06-10: 4 scaffold
9
- * files byte-diverged from the vault under a 0-conflict sync).
10
- *
11
- * Correct semantics (this test): the rescue must NOT touch the personal-vault
12
- * sync journal at all. Rescue-changed scaffold stays visible as "local
13
- * changed", the next bidirectional sync pushes it up, and the pull-side
14
- * byte-identical convergence probe (6.2.0) absorbs cross-machine races.
15
- *
16
- * Also covered: the core/core.yaml provenance stamp must be DETERMINISTIC —
17
- * a pure function of the source SHA (committer time, not wall-clock now) — so
18
- * every machine rescuing the same release writes byte-identical core.yaml.
19
- * The wall-clock stamp was the one genuine cross-machine divergence engine
20
- * behind `core.yaml.conflict-*` mirrors.
21
- */
22
- import { describe, it, expect, beforeAll, afterAll } from "vitest";
23
- import { execFileSync } from "child_process";
24
- import * as fs from "fs";
25
- import * as os from "os";
26
- import * as path from "path";
27
- import { runRescue } from "./rescue-core.js";
28
- import {
29
- hashFile,
30
- readJournal,
31
- writeJournal,
32
- PERSONAL_VAULT_JOURNAL_SLUG,
33
- } from "../journal.js";
34
-
35
- function hasGit(): boolean {
36
- try {
37
- execFileSync("git", ["--version"], { stdio: "ignore" });
38
- return true;
39
- } catch {
40
- return false;
41
- }
42
- }
43
- const gitAvailable = hasGit();
44
-
45
- function runRescueCapture(argv: string[], env: NodeJS.ProcessEnv) {
46
- let stdout = "";
47
- const origOut = process.stdout.write.bind(process.stdout);
48
- const origErr = process.stderr.write.bind(process.stderr);
49
- process.stdout.write = ((c: unknown) => ((stdout += String(c)), true)) as typeof process.stdout.write;
50
- process.stderr.write = (() => true) as typeof process.stderr.write;
51
- let status: number;
52
- try {
53
- status = runRescue(argv, { env }).status;
54
- } finally {
55
- process.stdout.write = origOut;
56
- process.stderr.write = origErr;
57
- }
58
- return { status, stdout };
59
- }
60
-
61
- const SCAFFOLD = ["core/a.md", "core/docs/b.md", ".claude/c.sh", "core/core.yaml"];
62
-
63
- describe.skipIf(!gitAvailable)("rescue leaves the sync journal untouched + stamps core.yaml deterministically", () => {
64
- let workDir: string, upstream: string, hqRoot: string, stateDir: string, floorSha: string;
65
- let env: NodeJS.ProcessEnv;
66
- let savedStateDir: string | undefined;
67
- let headCommitterIso: string;
68
- let seededHashes: Record<string, string>;
69
-
70
- const git = (cwd: string, ...args: string[]) =>
71
- execFileSync("git", args, {
72
- cwd,
73
- stdio: ["ignore", "pipe", "pipe"],
74
- env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" },
75
- }).toString().trim();
76
-
77
- const w = (root: string, rel: string, body: string) => {
78
- const p = path.join(root, rel);
79
- fs.mkdirSync(path.dirname(p), { recursive: true });
80
- fs.writeFileSync(p, body);
81
- };
82
-
83
- beforeAll(() => {
84
- workDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-rescue-journal-"));
85
-
86
- // --- upstream: floor (all scaffold = v1), HEAD advances the 3 docs to v2 ---
87
- upstream = path.join(workDir, "upstream");
88
- fs.mkdirSync(upstream, { recursive: true });
89
- git(workDir, "init", "-b", "main", "upstream");
90
- w(upstream, "core/a.md", "v1\n");
91
- w(upstream, "core/docs/b.md", "v1\n");
92
- w(upstream, ".claude/c.sh", "v1\n");
93
- w(upstream, "core/core.yaml", "version: 1\n");
94
- git(upstream, "add", "-A"); git(upstream, "commit", "-m", "floor");
95
- floorSha = git(upstream, "rev-parse", "HEAD");
96
- w(upstream, "core/a.md", "v2\n");
97
- w(upstream, "core/docs/b.md", "v2\n");
98
- w(upstream, ".claude/c.sh", "v2\n");
99
- git(upstream, "add", "-A"); git(upstream, "commit", "-m", "head");
100
- headCommitterIso = git(upstream, "show", "-s", "--format=%cI", "HEAD");
101
-
102
- // --- local HQ root: scaffold == floor (overlaid to v2); a pending personal/ edit ---
103
- hqRoot = path.join(workDir, "hq");
104
- fs.mkdirSync(path.join(hqRoot, "companies"), { recursive: true });
105
- w(hqRoot, "core/a.md", "v1\n");
106
- w(hqRoot, "core/docs/b.md", "v1\n");
107
- w(hqRoot, ".claude/c.sh", "v1\n");
108
- w(hqRoot, "core/core.yaml", "version: 1\n");
109
- w(hqRoot, "personal/edited.md", "USER_LOCAL\n"); // local pending edit
110
-
111
- // --- state dir + seeded journal: pre-rescue baselines for scaffold + a divergent personal/ entry ---
112
- stateDir = path.join(workDir, "state");
113
- fs.mkdirSync(stateDir, { recursive: true });
114
- savedStateDir = process.env.HQ_STATE_DIR;
115
- process.env.HQ_STATE_DIR = stateDir;
116
- const entry = (rel: string, hash: string) => ({
117
- hash,
118
- size: fs.statSync(path.join(hqRoot, rel)).size,
119
- syncedAt: new Date(0).toISOString(),
120
- direction: "down" as const,
121
- remoteEtag: "seed-etag",
122
- mtimeMs: fs.statSync(path.join(hqRoot, rel)).mtimeMs,
123
- });
124
- const files: Record<string, unknown> = {};
125
- seededHashes = {};
126
- for (const rel of SCAFFOLD) {
127
- const h = hashFile(path.join(hqRoot, rel)); // hash of v1/version:1
128
- seededHashes[rel] = h;
129
- files[rel] = entry(rel, h);
130
- }
131
- // personal/ pending edit: journal records a DIFFERENT (already-synced) hash than local.
132
- files["personal/edited.md"] = { ...entry("personal/edited.md", "remote-side-hash-differs-from-local") };
133
- writeJournal(PERSONAL_VAULT_JOURNAL_SLUG, {
134
- version: "2", lastSync: new Date(0).toISOString(), files, pulls: [],
135
- } as never);
136
-
137
- // --- git shim: redirect clone to the local fixture ---
138
- const realGit = execFileSync("bash", ["-lc", "command -v git"]).toString().trim() || "/usr/bin/git";
139
- const shimDir = path.join(workDir, "shim");
140
- fs.mkdirSync(shimDir, { recursive: true });
141
- fs.writeFileSync(
142
- path.join(shimDir, "git"),
143
- `#!/usr/bin/env bash
144
- if [ "$1" = "clone" ]; then
145
- args=(); for a in "$@"; do case "$a" in https://github.com/*) a=${JSON.stringify(upstream)} ;; esac; args+=("$a"); done
146
- exec ${JSON.stringify(realGit)} "\${args[@]}"
147
- fi
148
- exec ${JSON.stringify(realGit)} "$@"
149
- `,
150
- { mode: 0o755 },
151
- );
152
- env = { ...process.env, PATH: `${shimDir}:${process.env.PATH ?? ""}`, HQ_STATE_DIR: stateDir };
153
- });
154
-
155
- afterAll(() => {
156
- if (savedStateDir === undefined) delete process.env.HQ_STATE_DIR;
157
- else process.env.HQ_STATE_DIR = savedStateDir;
158
- if (workDir) fs.rmSync(workDir, { recursive: true, force: true });
159
- });
160
-
161
- it("leaves EVERY journal entry untouched so the push leg uploads rescue output (vault converges)", () => {
162
- const r = runRescueCapture(
163
- ["--hq-root", hqRoot, "--source", "test/repo", "--ref", "main", "--floor-sha", floorSha, "--yes", "--no-backup"],
164
- env,
165
- );
166
- expect(r.status, r.stdout).toBe(0);
167
-
168
- // the 3 docs were overlaid v1 -> v2
169
- for (const rel of ["core/a.md", "core/docs/b.md", ".claude/c.sh"]) {
170
- expect(fs.readFileSync(path.join(hqRoot, rel), "utf-8")).toBe("v2\n");
171
- }
172
-
173
- const j = readJournal(PERSONAL_VAULT_JOURNAL_SLUG).files;
174
-
175
- // INVARIANT: the rescue never rewrites journal baselines. Every scaffold
176
- // entry still carries its PRE-rescue hash -> localChanged=true on the next
177
- // sync -> the push leg uploads the regenerated scaffold and the VAULT
178
- // converges to the rescue output. (Masking these — 6.2.1/6.2.3 — left the
179
- // vault silently stale.)
180
- for (const rel of SCAFFOLD) {
181
- expect(j[rel].hash, `${rel} baseline was rewritten`).toBe(seededHashes[rel]);
182
- expect(j[rel].remoteEtag, `${rel} remote side touched`).toBe("seed-etag");
183
- // and the local file genuinely changed, so the entry is push-visible
184
- expect(j[rel].hash).not.toBe(hashFile(path.join(hqRoot, rel)));
185
- }
186
-
187
- // SAFETY: the personal/ pending edit is untouched too — still uploads.
188
- expect(j["personal/edited.md"].hash).toBe("remote-side-hash-differs-from-local");
189
- expect(j["personal/edited.md"].hash).not.toBe(hashFile(path.join(hqRoot, "personal/edited.md")));
190
-
191
- // the masking reconcile is gone
192
- expect(r.stdout).not.toContain("Reconciled sync-journal baseline");
193
- });
194
-
195
- it("stamps core.yaml deterministically: byte-identical across runs, last_sync_at = source committer time", () => {
196
- const coreYaml = path.join(hqRoot, "core/core.yaml");
197
- const firstRun = fs.readFileSync(coreYaml, "utf-8");
198
-
199
- // last_sync_at must be derived from the HEAD commit, not wall-clock now.
200
- // utcStamp(_, "colon") renders UTC as YYYY-MM-DDTHH:MM:SSZ — compare on
201
- // the epoch second, not the string, to stay offset-agnostic.
202
- const stamped = /last_sync_at:\s*["']?([0-9TZ:.-]+)["']?/.exec(firstRun);
203
- expect(stamped, `no last_sync_at in:\n${firstRun}`).not.toBeNull();
204
- expect(new Date(stamped![1]).getTime()).toBe(new Date(headCommitterIso).getTime());
205
-
206
- // a second rescue of the SAME SHA must write byte-identical core.yaml —
207
- // this is the cross-machine `core.yaml.conflict-*` mirror regression.
208
- const r2 = runRescueCapture(
209
- ["--hq-root", hqRoot, "--source", "test/repo", "--ref", "main", "--floor-sha", floorSha, "--yes", "--no-backup"],
210
- env,
211
- );
212
- expect(r2.status, r2.stdout).toBe(0);
213
- expect(fs.readFileSync(coreYaml, "utf-8")).toBe(firstRun);
214
- });
215
- });
@@ -1,203 +0,0 @@
1
- /**
2
- * Integration regression for the rescue mtime-preservation fix in
3
- * src/cli/rescue-core.ts.
4
- *
5
- * The bug: rescue rebuilds the tree by `git clone` + `rsync -a`. A git
6
- * checkout stamps every file with clone-time (git stores no per-file mtimes),
7
- * so the overlay reset EVERY rescued file's mtime to "whenever rescue ran" —
8
- * collapsing real history onto one instant (the exact fingerprint that flagged
9
- * this: hundreds of files sharing one mtime, mtime == ctime == birth).
10
- *
11
- * The fix has three parts, all asserted below:
12
- * - Layer 1: UNCHANGED files are left in place; the overlay excludes them, so
13
- * their existing mtime survives.
14
- * - Layer 2: files the overlay DOES write (upstream-changed, brand-new) get
15
- * the git committer-date of their last-modifying commit, not wall-clock.
16
- * - Layer 3: full history is used (history_floor mode clones full history;
17
- * the shallow path unshallows) so per-file commit times are real.
18
- *
19
- * The rescue clones from GitHub, so we shim `git clone` to a local fixture
20
- * (same technique as rescue-drift-reconcile.test.ts) and run for real
21
- * (non-dry-run, --no-backup) so the overlay actually lays files down.
22
- */
23
- import { describe, it, expect, beforeAll, afterAll } from "vitest";
24
- import { execFileSync } from "child_process";
25
- import * as fs from "fs";
26
- import * as os from "os";
27
- import * as path from "path";
28
- import { runRescue } from "./rescue-core.js";
29
-
30
- function has(bin: string, ...args: string[]): boolean {
31
- try {
32
- execFileSync(bin, args, { stdio: "ignore" });
33
- return true;
34
- } catch {
35
- return false;
36
- }
37
- }
38
-
39
- // Needs git (fixture + clone) and rsync (overlay). The mtime restore is now
40
- // native (fs.utimesSync), so perl is no longer required.
41
- const toolsAvailable = has("git", "--version") && has("rsync", "--version");
42
-
43
- // Fixed commit epochs so assertions are exact and machine-independent.
44
- const FLOOR_EPOCH = 1577836800; // 2020-01-01T00:00:00Z
45
- const HEAD_EPOCH = 1609459200; // 2021-01-01T00:00:00Z
46
- const KEEP_PRESET_EPOCH = 1546300800; // 2019-01-01T00:00:00Z (local, pre-rescue)
47
-
48
- /** Run the rescue in-process, capturing its stdout/stderr. */
49
- function runRescueCapture(argv: string[], env: NodeJS.ProcessEnv) {
50
- let stdout = "";
51
- let stderr = "";
52
- const origOut = process.stdout.write.bind(process.stdout);
53
- const origErr = process.stderr.write.bind(process.stderr);
54
- process.stdout.write = ((chunk: unknown) => {
55
- stdout += String(chunk);
56
- return true;
57
- }) as typeof process.stdout.write;
58
- process.stderr.write = ((chunk: unknown) => {
59
- stderr += String(chunk);
60
- return true;
61
- }) as typeof process.stderr.write;
62
- let status: number;
63
- try {
64
- status = runRescue(argv, { env }).status;
65
- } finally {
66
- process.stdout.write = origOut;
67
- process.stderr.write = origErr;
68
- }
69
- return { status, stdout, stderr };
70
- }
71
-
72
- describe.skipIf(!toolsAvailable)("rescue preserves mtimes (no clone-time flattening)", () => {
73
- let workDir: string;
74
- let upstream: string;
75
- let hqRoot: string;
76
- let shimDir: string;
77
- let floorSha: string;
78
- let env: NodeJS.ProcessEnv;
79
-
80
- const gitAt = (cwd: string, epoch: number, ...args: string[]) =>
81
- execFileSync("git", args, {
82
- cwd,
83
- stdio: ["ignore", "pipe", "pipe"],
84
- env: {
85
- ...process.env,
86
- GIT_AUTHOR_NAME: "t",
87
- GIT_AUTHOR_EMAIL: "t@t",
88
- GIT_COMMITTER_NAME: "t",
89
- GIT_COMMITTER_EMAIL: "t@t",
90
- GIT_AUTHOR_DATE: `${epoch} +0000`,
91
- GIT_COMMITTER_DATE: `${epoch} +0000`,
92
- },
93
- })
94
- .toString()
95
- .trim();
96
-
97
- const mtimeSec = (p: string) => Math.floor(fs.statSync(p).mtimeMs / 1000);
98
-
99
- beforeAll(() => {
100
- workDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-rescue-mtime-"));
101
-
102
- // --- "upstream" repo ----------------------------------------------------
103
- // floor commit (2020): x.md=v1, edited.md=base, keep.md=same
104
- // HEAD commit (2021): x.md=v2 (changed) + new.md added; keep.md untouched
105
- upstream = path.join(workDir, "upstream");
106
- fs.mkdirSync(path.join(upstream, "core"), { recursive: true });
107
- gitAt(workDir, FLOOR_EPOCH, "init", "-b", "main", "upstream");
108
- fs.writeFileSync(path.join(upstream, "core/x.md"), "v1\n");
109
- fs.writeFileSync(path.join(upstream, "core/edited.md"), "base\n");
110
- fs.writeFileSync(path.join(upstream, "core/keep.md"), "same\n");
111
- gitAt(upstream, FLOOR_EPOCH, "add", "-A");
112
- gitAt(upstream, FLOOR_EPOCH, "commit", "-m", "floor");
113
- floorSha = gitAt(upstream, FLOOR_EPOCH, "rev-parse", "HEAD");
114
- fs.writeFileSync(path.join(upstream, "core/x.md"), "v2\n");
115
- fs.writeFileSync(path.join(upstream, "core/new.md"), "fresh\n");
116
- gitAt(upstream, HEAD_EPOCH, "add", "-A");
117
- gitAt(upstream, HEAD_EPOCH, "commit", "-m", "head");
118
-
119
- // --- local HQ root being rescued ----------------------------------------
120
- hqRoot = path.join(workDir, "hq");
121
- fs.mkdirSync(path.join(hqRoot, "core"), { recursive: true });
122
- fs.mkdirSync(path.join(hqRoot, "personal"), { recursive: true });
123
- fs.mkdirSync(path.join(hqRoot, "companies"), { recursive: true });
124
- // Unchanged vs floor but upstream advanced v1 -> v2: overlay rewrites it.
125
- fs.writeFileSync(path.join(hqRoot, "core/x.md"), "v1\n");
126
- // User-edited: rescued to personal/ (not asserted here).
127
- fs.writeFileSync(path.join(hqRoot, "core/edited.md"), "MINE\n");
128
- // Identical to upstream HEAD: UNCHANGED -> left in place. Pre-stamp an old
129
- // mtime that must survive the rescue.
130
- const keep = path.join(hqRoot, "core/keep.md");
131
- fs.writeFileSync(keep, "same\n");
132
- fs.utimesSync(keep, KEEP_PRESET_EPOCH, KEEP_PRESET_EPOCH);
133
-
134
- // --- git shim: redirect `git clone <github-url>` to the local fixture ----
135
- const realGit = execFileSync("bash", ["-lc", "command -v git"]).toString().trim() || "/usr/bin/git";
136
- shimDir = path.join(workDir, "shim");
137
- fs.mkdirSync(shimDir, { recursive: true });
138
- const shim = `#!/usr/bin/env bash
139
- if [ "$1" = "clone" ]; then
140
- args=()
141
- for a in "$@"; do
142
- case "$a" in
143
- https://github.com/*) a=${JSON.stringify(upstream)} ;;
144
- esac
145
- args+=("$a")
146
- done
147
- exec ${JSON.stringify(realGit)} "\${args[@]}"
148
- fi
149
- exec ${JSON.stringify(realGit)} "$@"
150
- `;
151
- fs.writeFileSync(path.join(shimDir, "git"), shim, { mode: 0o755 });
152
- env = { ...process.env, PATH: `${shimDir}:${process.env.PATH ?? ""}` };
153
-
154
- // --- run the real rescue (non-dry-run) ----------------------------------
155
- const r = runRescueCapture(
156
- [
157
- "--hq-root", hqRoot,
158
- "--source", "test/repo",
159
- "--ref", "main",
160
- "--floor-sha", floorSha,
161
- "--yes",
162
- "--no-backup",
163
- ],
164
- env,
165
- );
166
- // Surface output on failure for debuggability.
167
- if (r.status !== 0) {
168
- throw new Error(`rescue failed (${r.status}):\n${r.stdout}\n${r.stderr}`);
169
- }
170
- });
171
-
172
- afterAll(() => {
173
- if (workDir) fs.rmSync(workDir, { recursive: true, force: true });
174
- });
175
-
176
- it("rewrites an upstream-changed file with its git commit mtime (not now)", () => {
177
- const x = path.join(hqRoot, "core/x.md");
178
- expect(fs.readFileSync(x, "utf-8")).toBe("v2\n");
179
- expect(mtimeSec(x)).toBe(HEAD_EPOCH);
180
- });
181
-
182
- it("creates a brand-new upstream file with its git commit mtime", () => {
183
- const n = path.join(hqRoot, "core/new.md");
184
- expect(fs.existsSync(n)).toBe(true);
185
- expect(mtimeSec(n)).toBe(HEAD_EPOCH);
186
- });
187
-
188
- it("leaves an UNCHANGED file in place, preserving its existing mtime (Layer 1)", () => {
189
- const keep = path.join(hqRoot, "core/keep.md");
190
- expect(fs.readFileSync(keep, "utf-8")).toBe("same\n");
191
- expect(mtimeSec(keep)).toBe(KEEP_PRESET_EPOCH);
192
- });
193
-
194
- it("never stamps an overlaid file with wall-clock-now (the original bug)", () => {
195
- const now = Math.floor(Date.now() / 1000);
196
- for (const rel of ["core/x.md", "core/new.md", "core/keep.md"]) {
197
- const age = now - mtimeSec(path.join(hqRoot, rel));
198
- // Every asserted file predates the run by years; a clone-time stamp
199
- // would be within seconds of now.
200
- expect(age, `${rel} mtime looks like wall-clock-now`).toBeGreaterThan(60);
201
- }
202
- });
203
- });