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