@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,188 +0,0 @@
1
- import { afterEach, describe, expect, it } from "vitest";
2
- import * as fs from "node:fs";
3
- import * as os from "node:os";
4
- import * as path from "node:path";
5
-
6
- import { seedActiveCompany } from "./active-company.js";
7
-
8
- let hqRoot: string | undefined;
9
-
10
- afterEach(() => {
11
- if (hqRoot) {
12
- fs.rmSync(hqRoot, { recursive: true, force: true });
13
- hqRoot = undefined;
14
- }
15
- });
16
-
17
- function makeHqRoot(): string {
18
- return fs.mkdtempSync(path.join(os.tmpdir(), "hq-active-company-"));
19
- }
20
-
21
- function configPath(root: string): string {
22
- return path.join(root, ".hq", "config.json");
23
- }
24
-
25
- function writeConfig(root: string, contents: string): string {
26
- const p = configPath(root);
27
- fs.mkdirSync(path.dirname(p), { recursive: true });
28
- fs.writeFileSync(p, contents, "utf8");
29
- return p;
30
- }
31
-
32
- function readConfig(root: string): Record<string, unknown> {
33
- return JSON.parse(fs.readFileSync(configPath(root), "utf8")) as Record<string, unknown>;
34
- }
35
-
36
- describe("seedActiveCompany", () => {
37
- it("seeds the single company when activeCompany is unset", () => {
38
- hqRoot = makeHqRoot();
39
- writeConfig(hqRoot, JSON.stringify({ someOtherSetting: true }));
40
-
41
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
42
- written: true,
43
- activeCompany: "boring-ecom",
44
- });
45
- expect(readConfig(hqRoot)).toEqual({
46
- someOtherSetting: true,
47
- activeCompany: "boring-ecom",
48
- });
49
- });
50
-
51
- it("creates a missing config file", () => {
52
- hqRoot = makeHqRoot();
53
- expect(fs.existsSync(configPath(hqRoot))).toBe(false);
54
-
55
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
56
- written: true,
57
- activeCompany: "boring-ecom",
58
- });
59
- expect(readConfig(hqRoot)).toEqual({ activeCompany: "boring-ecom" });
60
- });
61
-
62
- it("no-ops on zero companies", () => {
63
- hqRoot = makeHqRoot();
64
- const before = writeConfig(hqRoot, JSON.stringify({ keep: 1 }));
65
- const raw = fs.readFileSync(before, "utf8");
66
-
67
- expect(seedActiveCompany({ hqRoot, companySlugs: [] })).toEqual({
68
- written: false,
69
- skippedReason: "no-companies",
70
- });
71
- expect(fs.readFileSync(before, "utf8")).toBe(raw);
72
- });
73
-
74
- // Two companies is a real choice. Guessing would route the user somewhere
75
- // arbitrary, which is indistinguishable from a bug to the person it happens to.
76
- it("no-ops on two or more companies rather than guessing", () => {
77
- hqRoot = makeHqRoot();
78
- const p = writeConfig(hqRoot, JSON.stringify({ keep: 1 }));
79
- const raw = fs.readFileSync(p, "utf8");
80
-
81
- expect(
82
- seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom", "acme"] }),
83
- ).toEqual({ written: false, skippedReason: "ambiguous" });
84
- expect(fs.readFileSync(p, "utf8")).toBe(raw);
85
- });
86
-
87
- it("never overwrites an existing activeCompany", () => {
88
- hqRoot = makeHqRoot();
89
- const p = writeConfig(hqRoot, JSON.stringify({ activeCompany: "chosen-by-user" }));
90
- const raw = fs.readFileSync(p, "utf8");
91
-
92
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
93
- written: false,
94
- skippedReason: "already-set",
95
- });
96
- expect(fs.readFileSync(p, "utf8")).toBe(raw);
97
- });
98
-
99
- // Even when the existing value names a company they have since left. Repairing
100
- // that is a separate, user-visible decision — not something a sync should do
101
- // silently underneath them.
102
- it("never overwrites activeCompany even for a company the user has left", () => {
103
- hqRoot = makeHqRoot();
104
- writeConfig(hqRoot, JSON.stringify({ activeCompany: "former-employer" }));
105
-
106
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
107
- written: false,
108
- skippedReason: "already-set",
109
- });
110
- expect(readConfig(hqRoot).activeCompany).toBe("former-employer");
111
- });
112
-
113
- // sync-core's resolveActiveCompany falls back to `companySlug`, so a config
114
- // carrying only that already HAS an effective answer. Writing activeCompany
115
- // would silently re-route the user.
116
- it("treats a legacy companySlug as already-set", () => {
117
- hqRoot = makeHqRoot();
118
- const p = writeConfig(hqRoot, JSON.stringify({ companySlug: "legacy-choice" }));
119
- const raw = fs.readFileSync(p, "utf8");
120
-
121
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
122
- written: false,
123
- skippedReason: "already-set",
124
- });
125
- expect(fs.readFileSync(p, "utf8")).toBe(raw);
126
- });
127
-
128
- it("leaves malformed JSON untouched and reports a diagnostic", () => {
129
- hqRoot = makeHqRoot();
130
- const p = writeConfig(hqRoot, '{"activeCompany": "half-edited"');
131
- const raw = fs.readFileSync(p, "utf8");
132
- const diagnostics: Array<{ event: string }> = [];
133
-
134
- expect(
135
- seedActiveCompany({
136
- hqRoot,
137
- companySlugs: ["boring-ecom"],
138
- reportDiagnostic: (d) => diagnostics.push(d),
139
- }),
140
- ).toEqual({ written: false, skippedReason: "unreadable-config" });
141
-
142
- expect(fs.readFileSync(p, "utf8")).toBe(raw);
143
- expect(diagnostics).toHaveLength(1);
144
- expect(diagnostics[0].event).toBe("runner.active_company.config_malformed");
145
- });
146
-
147
- it("leaves a non-object config untouched", () => {
148
- hqRoot = makeHqRoot();
149
- const p = writeConfig(hqRoot, "[1, 2, 3]");
150
- const raw = fs.readFileSync(p, "utf8");
151
-
152
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] })).toEqual({
153
- written: false,
154
- skippedReason: "unreadable-config",
155
- });
156
- expect(fs.readFileSync(p, "utf8")).toBe(raw);
157
- });
158
-
159
- it("preserves unknown keys and leaves no temp files behind", () => {
160
- hqRoot = makeHqRoot();
161
- writeConfig(
162
- hqRoot,
163
- JSON.stringify({ nested: { a: 1 }, list: [1, 2], flag: false }),
164
- );
165
-
166
- expect(seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom"] }).written).toBe(
167
- true,
168
- );
169
- expect(readConfig(hqRoot)).toEqual({
170
- nested: { a: 1 },
171
- list: [1, 2],
172
- flag: false,
173
- activeCompany: "boring-ecom",
174
- });
175
- const strays = fs
176
- .readdirSync(path.join(hqRoot, ".hq"))
177
- .filter((f) => f.startsWith(".config-"));
178
- expect(strays).toEqual([]);
179
- });
180
-
181
- it("collapses duplicate slugs — one company named twice is not ambiguous", () => {
182
- hqRoot = makeHqRoot();
183
- expect(
184
- seedActiveCompany({ hqRoot, companySlugs: ["boring-ecom", "boring-ecom"] }),
185
- ).toEqual({ written: true, activeCompany: "boring-ecom" });
186
- expect(readConfig(hqRoot).activeCompany).toBe("boring-ecom");
187
- });
188
- });
@@ -1,168 +0,0 @@
1
- /**
2
- * Seed `activeCompany` in `.hq/config.json` for a joiner who has exactly one
3
- * cloud company.
4
- *
5
- * A newly invited member arrives with no local company routing at all. The
6
- * manifest reconciler restores their `companies/manifest.yaml` entry, but
7
- * nothing tells HQ which company is *current*, so they stay unrouted even
8
- * though membership resolved fine. When the answer is unambiguous — one company
9
- * and no existing choice — recording it is strictly better than leaving the
10
- * user to guess.
11
- *
12
- * Everything here is deliberately conservative. This runs unattended on every
13
- * sync, against a file the user also edits by hand, so the bar for writing is
14
- * "the answer cannot be wrong": never overwrite an existing choice, never guess
15
- * between two companies, and never clobber a file we could not parse.
16
- */
17
-
18
- import { randomUUID } from "node:crypto";
19
- import * as fs from "node:fs";
20
- import * as path from "node:path";
21
-
22
- export interface SeedActiveCompanyOptions {
23
- hqRoot: string;
24
- /** Slugs of companies that reconciled successfully this run. */
25
- companySlugs: readonly string[];
26
- reportDiagnostic?: (diagnostic: {
27
- event: string;
28
- message: string;
29
- err: unknown;
30
- context: Record<string, unknown>;
31
- }) => void;
32
- }
33
-
34
- export interface SeedActiveCompanyResult {
35
- /** True only when `.hq/config.json` was actually written. */
36
- written: boolean;
37
- /** The slug that was seeded, when one was. */
38
- activeCompany?: string;
39
- /** Why nothing was written. Absent when `written` is true. */
40
- skippedReason?:
41
- | "ambiguous"
42
- | "no-companies"
43
- | "already-set"
44
- | "unreadable-config"
45
- | "write-failed";
46
- }
47
-
48
- interface HqConfig {
49
- activeCompany?: unknown;
50
- companySlug?: unknown;
51
- [key: string]: unknown;
52
- }
53
-
54
- function isPlainObject(value: unknown): value is Record<string, unknown> {
55
- if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
56
- const proto = Object.getPrototypeOf(value) as object | null;
57
- return proto === Object.prototype || proto === null;
58
- }
59
-
60
- function isNonEmptyString(value: unknown): value is string {
61
- return typeof value === "string" && value.length > 0;
62
- }
63
-
64
- /**
65
- * Write `config` to `configPath` via temp-file + rename, so a reader never
66
- * observes a half-written config and a crash cannot truncate the original.
67
- */
68
- function writeConfigAtomically(configPath: string, config: HqConfig): void {
69
- const dir = path.dirname(configPath);
70
- fs.mkdirSync(dir, { recursive: true });
71
- const temporaryPath = path.join(dir, `.config-${process.pid}-${randomUUID()}.json`);
72
- try {
73
- fs.writeFileSync(temporaryPath, `${JSON.stringify(config, null, 2)}\n`, "utf8");
74
- fs.renameSync(temporaryPath, configPath);
75
- } finally {
76
- // The rename consumed the temp file on the success path; this only fires
77
- // when writing or renaming threw, and must not mask the original error.
78
- try {
79
- fs.rmSync(temporaryPath, { force: true });
80
- } catch {
81
- // Best effort — a stray temp file is not worth failing a sync over.
82
- }
83
- }
84
- }
85
-
86
- /**
87
- * Seed `activeCompany` when — and only when — the answer is unambiguous.
88
- *
89
- * Never throws: the caller is a sync run whose exit code must not depend on
90
- * local routing convenience.
91
- */
92
- export function seedActiveCompany(
93
- options: SeedActiveCompanyOptions,
94
- ): SeedActiveCompanyResult {
95
- const slugs = Array.from(new Set(options.companySlugs.filter(isNonEmptyString)));
96
- if (slugs.length === 0) return { written: false, skippedReason: "no-companies" };
97
- // Two or more companies is a genuine choice and belongs to the user. Picking
98
- // one would route them somewhere arbitrary and look like a bug.
99
- if (slugs.length > 1) return { written: false, skippedReason: "ambiguous" };
100
-
101
- const activeCompany = slugs[0];
102
- const configPath = path.join(options.hqRoot, ".hq", "config.json");
103
-
104
- let config: HqConfig = {};
105
- let raw: string | undefined;
106
- try {
107
- raw = fs.readFileSync(configPath, "utf8");
108
- } catch (err) {
109
- if ((err as NodeJS.ErrnoException).code !== "ENOENT") {
110
- // An unreadable config is user state we cannot reason about. Leave it.
111
- options.reportDiagnostic?.({
112
- event: "runner.active_company.config_unreadable",
113
- message: "could not read .hq/config.json; activeCompany not seeded",
114
- err,
115
- context: { configPath },
116
- });
117
- return { written: false, skippedReason: "unreadable-config" };
118
- }
119
- }
120
-
121
- if (raw !== undefined) {
122
- let parsed: unknown;
123
- try {
124
- parsed = JSON.parse(raw);
125
- } catch (err) {
126
- // Malformed JSON is almost always a half-finished hand edit. Overwriting
127
- // it would destroy whatever the user was in the middle of doing.
128
- options.reportDiagnostic?.({
129
- event: "runner.active_company.config_malformed",
130
- message: ".hq/config.json is not valid JSON; left untouched",
131
- err,
132
- context: { configPath },
133
- });
134
- return { written: false, skippedReason: "unreadable-config" };
135
- }
136
- if (!isPlainObject(parsed)) {
137
- options.reportDiagnostic?.({
138
- event: "runner.active_company.config_malformed",
139
- message: ".hq/config.json is not a JSON object; left untouched",
140
- err: undefined,
141
- context: { configPath },
142
- });
143
- return { written: false, skippedReason: "unreadable-config" };
144
- }
145
- config = parsed;
146
- }
147
-
148
- // `companySlug` counts as already-set: sync-core resolves it as the active
149
- // company when `activeCompany` is absent, so writing `activeCompany` here
150
- // would silently re-route a user who already had an effective answer.
151
- if (isNonEmptyString(config.activeCompany) || isNonEmptyString(config.companySlug)) {
152
- return { written: false, skippedReason: "already-set" };
153
- }
154
-
155
- try {
156
- writeConfigAtomically(configPath, { ...config, activeCompany });
157
- } catch (err) {
158
- options.reportDiagnostic?.({
159
- event: "runner.active_company.write_failed",
160
- message: "failed to seed activeCompany in .hq/config.json",
161
- err,
162
- context: { configPath, activeCompany },
163
- });
164
- return { written: false, skippedReason: "write-failed" };
165
- }
166
-
167
- return { written: true, activeCompany };
168
- }
@@ -1,332 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2
- import * as fs from "fs";
3
- import * as os from "os";
4
- import * as path from "path";
5
- import {
6
- composeCodexAgentsMd,
7
- orderCapabilityFiles,
8
- gatherCodexAgentSections,
9
- materializeCodexAgents,
10
- codexHomeDir,
11
- CODEX_AGENTS_MARKER,
12
- AGENT_CONTRACT_FILE,
13
- AGENTS_PROFILE_REL,
14
- AGENT_CAPABILITIES_REL,
15
- type CodexSection,
16
- } from "./agent-codex-instructions.js";
17
-
18
- describe("composeCodexAgentsMd (pure)", () => {
19
- it("returns empty string for no sections", () => {
20
- expect(composeCodexAgentsMd([])).toBe("");
21
- });
22
-
23
- it("returns empty string when every section is whitespace-only", () => {
24
- const sections: CodexSection[] = [
25
- { name: "a", content: " \n\t " },
26
- { name: "b", content: "" },
27
- ];
28
- expect(composeCodexAgentsMd(sections)).toBe("");
29
- });
30
-
31
- it("emits the generated marker header then the joined body", () => {
32
- const out = composeCodexAgentsMd([
33
- { name: "profile", content: "I am the agent." },
34
- { name: "contract", content: "Build your own tools." },
35
- ]);
36
- expect(out.startsWith(CODEX_AGENTS_MARKER)).toBe(true);
37
- expect(out).toContain("I am the agent.");
38
- expect(out).toContain("Build your own tools.");
39
- // body sections separated by a blank line, trailing newline
40
- expect(out).toContain("I am the agent.\n\nBuild your own tools.");
41
- expect(out.endsWith("\n")).toBe(true);
42
- });
43
-
44
- it("trims sections and drops the empty ones", () => {
45
- const out = composeCodexAgentsMd([
46
- { name: "a", content: "\n\n kept \n\n" },
47
- { name: "b", content: " " },
48
- { name: "c", content: "also" },
49
- ]);
50
- expect(out).toContain("kept\n\nalso");
51
- expect(out).not.toMatch(/ {2}kept/);
52
- });
53
-
54
- it("is deterministic / carries no timestamp (idempotent bytes)", () => {
55
- const sections = [{ name: "x", content: "stable" }];
56
- expect(composeCodexAgentsMd(sections)).toBe(composeCodexAgentsMd(sections));
57
- });
58
- });
59
-
60
- describe("orderCapabilityFiles (pure)", () => {
61
- it("orders the contract first, then the rest alphabetically", () => {
62
- expect(
63
- orderCapabilityFiles(["slack.md", "zeta.md", AGENT_CONTRACT_FILE, "alpha.md"]),
64
- ).toEqual([AGENT_CONTRACT_FILE, "alpha.md", "slack.md", "zeta.md"]);
65
- });
66
-
67
- it("drops non-markdown entries", () => {
68
- expect(
69
- orderCapabilityFiles([AGENT_CONTRACT_FILE, "_seed-allowlist.yaml", "notes.txt", "x.md"]),
70
- ).toEqual([AGENT_CONTRACT_FILE, "x.md"]);
71
- });
72
-
73
- it("handles a missing contract gracefully", () => {
74
- expect(orderCapabilityFiles(["b.md", "a.md"])).toEqual(["a.md", "b.md"]);
75
- });
76
- });
77
-
78
- describe("gatherCodexAgentSections + materializeCodexAgents (IO)", () => {
79
- let hqRoot: string;
80
- let codexHome: string;
81
- const capDir = () => path.join(hqRoot, AGENT_CAPABILITIES_REL);
82
-
83
- beforeEach(() => {
84
- const base = fs.mkdtempSync(path.join(os.tmpdir(), "hq-codex-test-"));
85
- hqRoot = path.join(base, "hq");
86
- codexHome = path.join(base, ".codex");
87
- fs.mkdirSync(capDir(), { recursive: true });
88
- });
89
-
90
- afterEach(() => {
91
- try {
92
- fs.rmSync(path.dirname(hqRoot), { recursive: true, force: true });
93
- } catch {
94
- /* best-effort cleanup */
95
- }
96
- });
97
-
98
- const seedProfile = (s: string) =>
99
- fs.writeFileSync(path.join(hqRoot, AGENTS_PROFILE_REL), s);
100
- const seedCap = (name: string, s: string) =>
101
- fs.writeFileSync(path.join(capDir(), name), s);
102
-
103
- it("gathers profile + contract + others in the right order", () => {
104
- fs.mkdirSync(path.join(hqRoot, "personal"), { recursive: true });
105
- seedProfile("PROFILE briefing");
106
- seedCap("slack.md", "SLACK doc");
107
- seedCap(AGENT_CONTRACT_FILE, "CONTRACT body");
108
-
109
- const sections = gatherCodexAgentSections(hqRoot);
110
- expect(sections.map((s) => s.name)).toEqual([
111
- AGENTS_PROFILE_REL,
112
- `${AGENT_CAPABILITIES_REL}/${AGENT_CONTRACT_FILE}`,
113
- `${AGENT_CAPABILITIES_REL}/slack.md`,
114
- ]);
115
- });
116
-
117
- it("writes ~/.codex/AGENTS.md with contract ahead of other capabilities", () => {
118
- seedCap(AGENT_CONTRACT_FILE, "CONTRACT body");
119
- seedCap("slack.md", "SLACK doc");
120
-
121
- const res = materializeCodexAgents({ hqRoot, codexHome });
122
- expect(res.written).toBe(true);
123
- expect(res.sectionCount).toBe(2);
124
-
125
- const written = fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8");
126
- expect(written.startsWith(CODEX_AGENTS_MARKER)).toBe(true);
127
- expect(written.indexOf("CONTRACT body")).toBeLessThan(
128
- written.indexOf("SLACK doc"),
129
- );
130
- });
131
-
132
- it("SKIPS (no write, no delete) when the overlay is empty — never destructive", () => {
133
- // Pre-existing materialized file from a prior good sync.
134
- fs.mkdirSync(codexHome, { recursive: true });
135
- const existing = `${CODEX_AGENTS_MARKER} prior -->\n\nPRIOR CONTENT\n`;
136
- fs.writeFileSync(path.join(codexHome, "AGENTS.md"), existing);
137
-
138
- // Overlay is empty (transient unsynced tree).
139
- const res = materializeCodexAgents({ hqRoot, codexHome });
140
- expect(res.written).toBe(false);
141
- expect(res.skippedReason).toBe("empty-overlay");
142
- // The previously-materialized file is left intact.
143
- expect(fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8")).toBe(
144
- existing,
145
- );
146
- });
147
-
148
- it("creates the codex home dir if missing", () => {
149
- seedCap(AGENT_CONTRACT_FILE, "CONTRACT body");
150
- expect(fs.existsSync(codexHome)).toBe(false);
151
- const res = materializeCodexAgents({ hqRoot, codexHome });
152
- expect(res.written).toBe(true);
153
- expect(fs.existsSync(path.join(codexHome, "AGENTS.md"))).toBe(true);
154
- });
155
-
156
- it("is idempotent — two runs produce byte-identical output", () => {
157
- seedCap(AGENT_CONTRACT_FILE, "CONTRACT body");
158
- seedCap("slack.md", "SLACK doc");
159
- materializeCodexAgents({ hqRoot, codexHome });
160
- const first = fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8");
161
- materializeCodexAgents({ hqRoot, codexHome });
162
- const second = fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8");
163
- expect(second).toBe(first);
164
- });
165
-
166
- it("never throws on an unreadable hq-root (returns error result)", () => {
167
- const res = materializeCodexAgents({
168
- hqRoot: path.join(hqRoot, "does-not-exist"),
169
- codexHome,
170
- });
171
- expect(res.written).toBe(false);
172
- // No capability dir → empty overlay → skip (not a hard error).
173
- expect(res.skippedReason).toBe("empty-overlay");
174
- });
175
- });
176
-
177
- describe("materializeCodexAgents — hardening (security + failure modes)", () => {
178
- let base: string;
179
- let hqRoot: string;
180
- let codexHome: string;
181
- const capDir = () => path.join(hqRoot, AGENT_CAPABILITIES_REL);
182
-
183
- beforeEach(() => {
184
- base = fs.mkdtempSync(path.join(os.tmpdir(), "hq-codex-harden-"));
185
- hqRoot = path.join(base, "hq");
186
- codexHome = path.join(base, ".codex");
187
- fs.mkdirSync(capDir(), { recursive: true });
188
- });
189
- afterEach(() => {
190
- vi.restoreAllMocks();
191
- try {
192
- fs.rmSync(base, { recursive: true, force: true });
193
- } catch {
194
- /* best-effort */
195
- }
196
- });
197
-
198
- it("does NOT follow a symlinked capability file (no out-of-tree exfiltration)", () => {
199
- // A secret outside the overlay; a symlink inside capDir pointing at it.
200
- const secret = path.join(base, "secret.txt");
201
- fs.writeFileSync(secret, "TOP-SECRET-CREDENTIAL");
202
- fs.writeFileSync(path.join(capDir(), AGENT_CONTRACT_FILE), "CONTRACT body");
203
- fs.symlinkSync(secret, path.join(capDir(), "escape.md"));
204
-
205
- const sections = gatherCodexAgentSections(hqRoot);
206
- // The symlink is skipped; only the real contract file is gathered.
207
- expect(sections.map((s) => s.name)).toEqual([
208
- `${AGENT_CAPABILITIES_REL}/${AGENT_CONTRACT_FILE}`,
209
- ]);
210
-
211
- const res = materializeCodexAgents({ hqRoot, codexHome });
212
- const written = fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8");
213
- expect(written).not.toContain("TOP-SECRET-CREDENTIAL");
214
- expect(res.sectionCount).toBe(1);
215
- });
216
-
217
- it("skips a symlinked capability DIR (no descent through a symlinked capDir)", () => {
218
- // Replace capDir with a symlink to an external dir containing a real .md.
219
- const external = path.join(base, "external-caps");
220
- fs.mkdirSync(external, { recursive: true });
221
- fs.writeFileSync(path.join(external, AGENT_CONTRACT_FILE), "EXTERNAL body");
222
- fs.rmSync(capDir(), { recursive: true, force: true });
223
- fs.symlinkSync(external, capDir());
224
-
225
- const sections = gatherCodexAgentSections(hqRoot);
226
- expect(sections).toEqual([]);
227
- });
228
-
229
- it("refuses to clobber a SYMLINK target (preserves it; distinct skip reason)", () => {
230
- fs.writeFileSync(path.join(capDir(), AGENT_CONTRACT_FILE), "CONTRACT body");
231
- fs.mkdirSync(codexHome, { recursive: true });
232
- const realTarget = path.join(base, "managed-agents.md");
233
- fs.writeFileSync(realTarget, "MANAGED BY DOTFILES");
234
- fs.symlinkSync(realTarget, path.join(codexHome, "AGENTS.md"));
235
-
236
- const res = materializeCodexAgents({ hqRoot, codexHome });
237
- expect(res.written).toBe(false);
238
- expect(res.skippedReason).toBe("target-not-regular-file");
239
- // The symlink is intact and still points at the managed file.
240
- expect(fs.lstatSync(path.join(codexHome, "AGENTS.md")).isSymbolicLink()).toBe(
241
- true,
242
- );
243
- expect(fs.readFileSync(realTarget, "utf8")).toBe("MANAGED BY DOTFILES");
244
- });
245
-
246
- it("refuses when the target is a DIRECTORY (no EISDIR crash, distinct reason)", () => {
247
- fs.writeFileSync(path.join(capDir(), AGENT_CONTRACT_FILE), "CONTRACT body");
248
- fs.mkdirSync(path.join(codexHome, "AGENTS.md"), { recursive: true });
249
-
250
- const res = materializeCodexAgents({ hqRoot, codexHome });
251
- expect(res.written).toBe(false);
252
- expect(res.skippedReason).toBe("target-not-regular-file");
253
- expect(fs.statSync(path.join(codexHome, "AGENTS.md")).isDirectory()).toBe(true);
254
- });
255
-
256
- it("returns an error result and leaves no .hq-tmp orphan when the write fails", () => {
257
- // Root ignores mode bits, so this deterministic perms-based failure only
258
- // holds for a non-root user (agent boxes + CI run non-root).
259
- if (typeof process.getuid === "function" && process.getuid() === 0) return;
260
- fs.writeFileSync(path.join(capDir(), AGENT_CONTRACT_FILE), "CONTRACT body");
261
- // Read-only codex home → the temp write inside it fails (EACCES). The
262
- // write+rename block's catch must unlink any temp before rethrowing, so no
263
- // `*.hq-tmp` is left behind.
264
- fs.mkdirSync(codexHome, { recursive: true });
265
- fs.chmodSync(codexHome, 0o500);
266
- try {
267
- const res = materializeCodexAgents({ hqRoot, codexHome });
268
- expect(res.written).toBe(false);
269
- expect(res.skippedReason).toBe("error");
270
- const leftovers = fs
271
- .readdirSync(codexHome)
272
- .filter((f) => f.endsWith(".hq-tmp"));
273
- expect(leftovers).toEqual([]);
274
- } finally {
275
- fs.chmodSync(codexHome, 0o700); // restore so afterEach cleanup works
276
- }
277
- });
278
-
279
- it("writes the file with owner-only perms (0o600)", () => {
280
- fs.writeFileSync(path.join(capDir(), AGENT_CONTRACT_FILE), "CONTRACT body");
281
- materializeCodexAgents({ hqRoot, codexHome });
282
- const mode = fs.statSync(path.join(codexHome, "AGENTS.md")).mode & 0o777;
283
- expect(mode).toBe(0o600);
284
- });
285
-
286
- it("emits a distinct event when the overlay is empty but a prior file exists", () => {
287
- fs.mkdirSync(codexHome, { recursive: true });
288
- fs.writeFileSync(path.join(codexHome, "AGENTS.md"), "PRIOR\n");
289
- const events: string[] = [];
290
- const res = materializeCodexAgents({
291
- hqRoot, // capDir empty, no profile → empty overlay
292
- codexHome,
293
- log: (e) => events.push(e.event),
294
- });
295
- expect(res.skippedReason).toBe("empty-overlay");
296
- expect(events).toContain(
297
- "runner.agent_codex_instructions.skipped_empty_target_exists",
298
- );
299
- // Prior file left intact.
300
- expect(fs.readFileSync(path.join(codexHome, "AGENTS.md"), "utf8")).toBe(
301
- "PRIOR\n",
302
- );
303
- });
304
- });
305
-
306
- describe("codexHomeDir (CODEX_HOME validation)", () => {
307
- const orig = process.env.CODEX_HOME;
308
- afterEach(() => {
309
- if (orig === undefined) delete process.env.CODEX_HOME;
310
- else process.env.CODEX_HOME = orig;
311
- });
312
-
313
- it("defaults to ~/.codex when CODEX_HOME is unset", () => {
314
- delete process.env.CODEX_HOME;
315
- expect(codexHomeDir()).toBe(path.join(os.homedir(), ".codex"));
316
- });
317
-
318
- it("honors an absolute CODEX_HOME with no traversal", () => {
319
- process.env.CODEX_HOME = "/var/lib/agent-codex";
320
- expect(codexHomeDir()).toBe("/var/lib/agent-codex");
321
- });
322
-
323
- it("rejects a relative CODEX_HOME (falls back to ~/.codex)", () => {
324
- process.env.CODEX_HOME = "relative/codex";
325
- expect(codexHomeDir()).toBe(path.join(os.homedir(), ".codex"));
326
- });
327
-
328
- it("rejects a traversing CODEX_HOME (falls back to ~/.codex)", () => {
329
- process.env.CODEX_HOME = "/tmp/../../etc";
330
- expect(codexHomeDir()).toBe(path.join(os.homedir(), ".codex"));
331
- });
332
- });