@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,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
- });