@indigoai-us/hq-cli 5.345.247 → 5.345.250

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 (284) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/assets/lanes-hooks/hq-lanes-drain.sh +1 -1
  3. package/contracts/host-canary-release.json +14 -0
  4. package/dist/command-catalog.generated.d.ts +1198 -0
  5. package/dist/command-catalog.generated.js +1476 -0
  6. package/dist/command-registration-plan.d.ts +6 -0
  7. package/dist/command-registration-plan.js +1 -0
  8. package/dist/commands/core-job-run-native.js +4 -0
  9. package/dist/commands/core-knowledge-prefs.d.ts +1 -1
  10. package/dist/commands/core-knowledge-prefs.js +1 -16
  11. package/dist/commands/core.js +0 -2
  12. package/dist/commands/daemon.js +21 -0
  13. package/dist/commands/lanes-hold.js +7 -7
  14. package/dist/commands/lanes-resume.js +2 -2
  15. package/dist/commands/lanes-review.js +1 -1
  16. package/dist/commands/lanes-session-start.d.ts +1 -1
  17. package/dist/commands/lanes-session-start.js +3 -3
  18. package/dist/commands/lanes-shared.d.ts +1 -1
  19. package/dist/commands/lanes.d.ts +2 -2
  20. package/dist/commands/lanes.js +50 -50
  21. package/dist/commands/mesh.d.ts +25 -0
  22. package/dist/commands/mesh.js +48 -8
  23. package/dist/commands/ontology-graph.d.ts +63 -0
  24. package/dist/commands/ontology-graph.js +470 -0
  25. package/dist/commands/sync-core.js +6 -0
  26. package/dist/fast-scaffold.js +1 -3
  27. package/dist/hq-cloud-sync-runner/bin/sync-runner.js +281 -256
  28. package/dist/hq-cloud-sync-runner/package.json +1 -1
  29. package/dist/index.d.ts +8 -0
  30. package/dist/index.js +48 -1
  31. package/dist/lanes-monitor-check-argv.d.ts +1 -1
  32. package/dist/lanes-monitor-check-argv.js +1 -1
  33. package/dist/lanes-monitor-check-fast.d.ts +1 -1
  34. package/dist/lanes-monitor-check-fast.js +1 -1
  35. package/dist/lanes-session-start-argv.d.ts +1 -1
  36. package/dist/lanes-session-start-argv.js +1 -1
  37. package/dist/lib/capacity/sign-in.d.ts +1 -1
  38. package/dist/lib/capacity/sign-in.js +1 -1
  39. package/dist/lib/daemon/hook-requests-conduct-link.js +2 -2
  40. package/dist/lib/daemon/installed-cli-restart.d.ts +57 -0
  41. package/dist/lib/daemon/installed-cli-restart.js +114 -0
  42. package/dist/lib/daemon/lanes-monitor-state.d.ts +2 -2
  43. package/dist/lib/daemon/lanes-monitor-state.js +2 -2
  44. package/dist/lib/daemon/legacy-journal-gate.d.ts +32 -0
  45. package/dist/lib/daemon/legacy-journal-gate.js +102 -0
  46. package/dist/lib/daemon/machine-checks.d.ts +7 -0
  47. package/dist/lib/daemon/machine-checks.js +10 -0
  48. package/dist/lib/daemon/merge-sections.d.ts +3 -3
  49. package/dist/lib/daemon/merge-sections.js +7 -7
  50. package/dist/lib/daemon/remote-control-pools.js +1 -1
  51. package/dist/lib/daemon/run.d.ts +3 -1
  52. package/dist/lib/daemon/run.js +49 -6
  53. package/dist/lib/daemon/state-file.d.ts +1 -1
  54. package/dist/lib/daemon/state-file.js +1 -1
  55. package/dist/lib/flag-registry-worker-source.generated.d.ts +1 -1
  56. package/dist/lib/flag-registry-worker-source.generated.js +1 -1
  57. package/dist/lib/flag-snapshot-file.d.ts +2 -0
  58. package/dist/lib/flag-snapshot-file.js +118 -48
  59. package/dist/lib/hook-engine/canary-gate.d.ts +57 -3
  60. package/dist/lib/hook-engine/canary-gate.js +138 -6
  61. package/dist/lib/hook-engine/coverage.d.ts +82 -13
  62. package/dist/lib/hook-engine/coverage.js +214 -18
  63. package/dist/lib/hook-engine/registration-identity.d.ts +5 -0
  64. package/dist/lib/hook-engine/registration-identity.js +1 -1
  65. package/dist/lib/hook-engine/resolve.d.ts +23 -0
  66. package/dist/lib/hook-engine/resolve.js +48 -1
  67. package/dist/lib/hook-engine/stable-heal.d.ts +87 -0
  68. package/dist/lib/hook-engine/stable-heal.js +214 -0
  69. package/dist/lib/hot-channels/graph-context.d.ts +105 -0
  70. package/dist/lib/hot-channels/graph-context.js +380 -0
  71. package/dist/lib/hot-channels/signals-pass.js +27 -1
  72. package/dist/lib/lanes/claude-credential.d.ts +1 -1
  73. package/dist/lib/lanes/claude-credential.js +1 -1
  74. package/dist/lib/lanes/delivery-core.d.ts +1 -1
  75. package/dist/lib/lanes/delivery-core.js +1 -1
  76. package/dist/lib/lanes/delivery.d.ts +10 -10
  77. package/dist/lib/lanes/delivery.js +8 -8
  78. package/dist/lib/lanes/dropbox.d.ts +1 -1
  79. package/dist/lib/lanes/dropbox.js +1 -1
  80. package/dist/lib/lanes/hooks-install.js +2 -2
  81. package/dist/lib/lanes/index.d.ts +1 -1
  82. package/dist/lib/lanes/index.js +1 -1
  83. package/dist/lib/lanes/monitor.js +1 -1
  84. package/dist/lib/lanes/quota-notice.js +1 -1
  85. package/dist/lib/lanes/quota.d.ts +1 -1
  86. package/dist/lib/lanes/quota.js +1 -1
  87. package/dist/lib/lanes/repo-merge-hold.d.ts +2 -2
  88. package/dist/lib/lanes/repo-merge-hold.js +2 -2
  89. package/dist/lib/lanes/spawn.d.ts +4 -4
  90. package/dist/lib/lanes/spawn.js +5 -5
  91. package/dist/lib/mesh/live/company-flag-refresh.d.ts +61 -0
  92. package/dist/lib/mesh/live/company-flag-refresh.js +159 -0
  93. package/dist/lib/mesh/live/company-reporting-gaps.d.ts +61 -0
  94. package/dist/lib/mesh/live/company-reporting-gaps.js +238 -0
  95. package/dist/lib/mesh/live/daemon/duplicate-unit.d.ts +156 -0
  96. package/dist/lib/mesh/live/daemon/duplicate-unit.js +616 -0
  97. package/dist/lib/mesh/live/daemon/run.d.ts +13 -0
  98. package/dist/lib/mesh/live/daemon/run.js +35 -2
  99. package/dist/lib/mesh/live/direct-drain.js +1 -1
  100. package/dist/lib/mesh/live/emit.d.ts +3 -0
  101. package/dist/lib/mesh/live/emit.js +32 -9
  102. package/dist/lib/mesh/live/flush.d.ts +3 -0
  103. package/dist/lib/mesh/live/flush.js +115 -32
  104. package/dist/lib/mesh/live/liveness-carry.d.ts +1 -1
  105. package/dist/lib/mesh/live/liveness-carry.js +1 -1
  106. package/dist/lib/mesh/live/reporting-privacy.d.ts +119 -3
  107. package/dist/lib/mesh/live/reporting-privacy.js +235 -11
  108. package/dist/lib/mesh/live/spool.d.ts +17 -2
  109. package/dist/lib/mesh/live/spool.js +22 -6
  110. package/dist/lib/ontology-graph/legacy/coverage.d.ts +67 -0
  111. package/dist/lib/ontology-graph/legacy/coverage.js +192 -0
  112. package/dist/lib/ontology-graph/legacy/index.d.ts +199 -0
  113. package/dist/lib/ontology-graph/legacy/index.js +1150 -0
  114. package/dist/lib/ontology-graph/legacy/parse.d.ts +49 -0
  115. package/dist/lib/ontology-graph/legacy/parse.js +126 -0
  116. package/dist/lib/ontology-graph/legacy/report.d.ts +14 -0
  117. package/dist/lib/ontology-graph/legacy/report.js +254 -0
  118. package/dist/lib/ontology-graph/legacy/verb-map.d.ts +35 -0
  119. package/dist/lib/ontology-graph/legacy/verb-map.js +84 -0
  120. package/dist/lib/ontology-graph/lint/checks.d.ts +140 -0
  121. package/dist/lib/ontology-graph/lint/checks.js +717 -0
  122. package/dist/lib/ontology-graph/lint/coverage.d.ts +106 -0
  123. package/dist/lib/ontology-graph/lint/coverage.js +241 -0
  124. package/dist/lib/ontology-graph/lint/daily-job.d.ts +259 -0
  125. package/dist/lib/ontology-graph/lint/daily-job.js +595 -0
  126. package/dist/lib/ontology-graph/lint/index.d.ts +65 -0
  127. package/dist/lib/ontology-graph/lint/index.js +176 -0
  128. package/dist/lib/ontology-graph/model/files.d.ts +20 -0
  129. package/dist/lib/ontology-graph/model/files.js +89 -0
  130. package/dist/lib/ontology-graph/model/index.d.ts +65 -0
  131. package/dist/lib/ontology-graph/model/index.js +100 -0
  132. package/dist/lib/ontology-graph/model/pass-tasks.d.ts +156 -0
  133. package/dist/lib/ontology-graph/model/pass-tasks.js +345 -0
  134. package/dist/lib/ontology-graph/model/pass.d.ts +109 -0
  135. package/dist/lib/ontology-graph/model/pass.js +958 -0
  136. package/dist/lib/ontology-graph/model/runner.d.ts +50 -0
  137. package/dist/lib/ontology-graph/model/runner.js +160 -0
  138. package/dist/lib/ontology-graph/model/step.d.ts +197 -0
  139. package/dist/lib/ontology-graph/model/step.js +526 -0
  140. package/dist/lib/ontology-graph/model/tasks.d.ts +183 -0
  141. package/dist/lib/ontology-graph/model/tasks.js +349 -0
  142. package/dist/lib/ontology-graph/not-implemented.d.ts +5 -0
  143. package/dist/lib/ontology-graph/not-implemented.js +8 -0
  144. package/dist/lib/ontology-graph/org/compare.d.ts +28 -0
  145. package/dist/lib/ontology-graph/org/compare.js +239 -0
  146. package/dist/lib/ontology-graph/org/derive.d.ts +67 -0
  147. package/dist/lib/ontology-graph/org/derive.js +359 -0
  148. package/dist/lib/ontology-graph/org/index.d.ts +102 -0
  149. package/dist/lib/ontology-graph/org/index.js +630 -0
  150. package/dist/lib/ontology-graph/org/load.d.ts +31 -0
  151. package/dist/lib/ontology-graph/org/load.js +476 -0
  152. package/dist/lib/ontology-graph/org/role.d.ts +38 -0
  153. package/dist/lib/ontology-graph/org/role.js +268 -0
  154. package/dist/lib/ontology-graph/org/structure.d.ts +63 -0
  155. package/dist/lib/ontology-graph/org/structure.js +437 -0
  156. package/dist/lib/ontology-graph/org/text.d.ts +97 -0
  157. package/dist/lib/ontology-graph/org/text.js +321 -0
  158. package/dist/lib/ontology-graph/org/types.d.ts +457 -0
  159. package/dist/lib/ontology-graph/org/types.js +80 -0
  160. package/dist/lib/ontology-graph/pages/index.d.ts +64 -0
  161. package/dist/lib/ontology-graph/pages/index.js +193 -0
  162. package/dist/lib/ontology-graph/pages/render.d.ts +144 -0
  163. package/dist/lib/ontology-graph/pages/render.js +688 -0
  164. package/dist/lib/ontology-graph/proposals/apply.d.ts +109 -0
  165. package/dist/lib/ontology-graph/proposals/apply.js +468 -0
  166. package/dist/lib/ontology-graph/proposals/group.d.ts +63 -0
  167. package/dist/lib/ontology-graph/proposals/group.js +232 -0
  168. package/dist/lib/ontology-graph/proposals/index.d.ts +173 -0
  169. package/dist/lib/ontology-graph/proposals/index.js +474 -0
  170. package/dist/lib/ontology-graph/proposals/model.d.ts +162 -0
  171. package/dist/lib/ontology-graph/proposals/model.js +196 -0
  172. package/dist/lib/ontology-graph/proposals/org-chart.d.ts +82 -0
  173. package/dist/lib/ontology-graph/proposals/org-chart.js +340 -0
  174. package/dist/lib/ontology-graph/read/brief.d.ts +26 -0
  175. package/dist/lib/ontology-graph/read/brief.js +254 -0
  176. package/dist/lib/ontology-graph/read/graph-index.d.ts +76 -0
  177. package/dist/lib/ontology-graph/read/graph-index.js +198 -0
  178. package/dist/lib/ontology-graph/read/index.d.ts +59 -0
  179. package/dist/lib/ontology-graph/read/index.js +803 -0
  180. package/dist/lib/ontology-graph/read/member.d.ts +7 -0
  181. package/dist/lib/ontology-graph/read/member.js +34 -0
  182. package/dist/lib/ontology-graph/read/names.d.ts +49 -0
  183. package/dist/lib/ontology-graph/read/names.js +297 -0
  184. package/dist/lib/ontology-graph/read/reader-setting.d.ts +19 -0
  185. package/dist/lib/ontology-graph/read/reader-setting.js +62 -0
  186. package/dist/lib/ontology-graph/read/test-graph.d.ts +37 -0
  187. package/dist/lib/ontology-graph/read/test-graph.js +150 -0
  188. package/dist/lib/ontology-graph/replay/buffered-store.d.ts +33 -0
  189. package/dist/lib/ontology-graph/replay/buffered-store.js +50 -0
  190. package/dist/lib/ontology-graph/replay/config.d.ts +41 -0
  191. package/dist/lib/ontology-graph/replay/config.js +105 -0
  192. package/dist/lib/ontology-graph/replay/engine.d.ts +146 -0
  193. package/dist/lib/ontology-graph/replay/engine.js +1240 -0
  194. package/dist/lib/ontology-graph/replay/extractors.d.ts +59 -0
  195. package/dist/lib/ontology-graph/replay/extractors.js +1555 -0
  196. package/dist/lib/ontology-graph/replay/index.d.ts +91 -0
  197. package/dist/lib/ontology-graph/replay/index.js +220 -0
  198. package/dist/lib/ontology-graph/replay/listing-manifest.d.ts +66 -0
  199. package/dist/lib/ontology-graph/replay/listing-manifest.js +242 -0
  200. package/dist/lib/ontology-graph/replay/member.d.ts +19 -0
  201. package/dist/lib/ontology-graph/replay/member.js +65 -0
  202. package/dist/lib/ontology-graph/replay/mentions.d.ts +58 -0
  203. package/dist/lib/ontology-graph/replay/mentions.js +239 -0
  204. package/dist/lib/ontology-graph/replay/readers.d.ts +98 -0
  205. package/dist/lib/ontology-graph/replay/readers.js +1251 -0
  206. package/dist/lib/ontology-graph/replay/rules.d.ts +127 -0
  207. package/dist/lib/ontology-graph/replay/rules.js +374 -0
  208. package/dist/lib/ontology-graph/replay/scope.d.ts +35 -0
  209. package/dist/lib/ontology-graph/replay/scope.js +115 -0
  210. package/dist/lib/ontology-graph/replay/transcripts.d.ts +39 -0
  211. package/dist/lib/ontology-graph/replay/transcripts.js +224 -0
  212. package/dist/lib/ontology-graph/replay/util.d.ts +55 -0
  213. package/dist/lib/ontology-graph/replay/util.js +256 -0
  214. package/dist/lib/ontology-graph/resolve/entity-index.d.ts +159 -0
  215. package/dist/lib/ontology-graph/resolve/entity-index.js +652 -0
  216. package/dist/lib/ontology-graph/resolve/index.d.ts +113 -0
  217. package/dist/lib/ontology-graph/resolve/index.js +214 -0
  218. package/dist/lib/ontology-graph/resolve/machine.d.ts +34 -0
  219. package/dist/lib/ontology-graph/resolve/machine.js +95 -0
  220. package/dist/lib/ontology-graph/resolve/normalize.d.ts +35 -0
  221. package/dist/lib/ontology-graph/resolve/normalize.js +124 -0
  222. package/dist/lib/ontology-graph/resolve/org-chart.d.ts +107 -0
  223. package/dist/lib/ontology-graph/resolve/org-chart.js +578 -0
  224. package/dist/lib/ontology-graph/resolve/org-layer.d.ts +78 -0
  225. package/dist/lib/ontology-graph/resolve/org-layer.js +159 -0
  226. package/dist/lib/ontology-graph/resolve/org-seed.d.ts +141 -0
  227. package/dist/lib/ontology-graph/resolve/org-seed.js +631 -0
  228. package/dist/lib/ontology-graph/resolve/org-sources.d.ts +165 -0
  229. package/dist/lib/ontology-graph/resolve/org-sources.js +634 -0
  230. package/dist/lib/ontology-graph/resolve/roster.d.ts +76 -0
  231. package/dist/lib/ontology-graph/resolve/roster.js +226 -0
  232. package/dist/lib/ontology-graph/resolve/seed.d.ts +267 -0
  233. package/dist/lib/ontology-graph/resolve/seed.js +1198 -0
  234. package/dist/lib/ontology-graph/resolve/upsert.d.ts +58 -0
  235. package/dist/lib/ontology-graph/resolve/upsert.js +139 -0
  236. package/dist/lib/ontology-graph/resolve/writer.d.ts +128 -0
  237. package/dist/lib/ontology-graph/resolve/writer.js +351 -0
  238. package/dist/lib/ontology-graph/schema/authority.d.ts +107 -0
  239. package/dist/lib/ontology-graph/schema/authority.js +186 -0
  240. package/dist/lib/ontology-graph/schema/default-schema.d.ts +10 -0
  241. package/dist/lib/ontology-graph/schema/default-schema.js +242 -0
  242. package/dist/lib/ontology-graph/schema/grammar.d.ts +57 -0
  243. package/dist/lib/ontology-graph/schema/grammar.js +409 -0
  244. package/dist/lib/ontology-graph/schema/index.d.ts +12 -0
  245. package/dist/lib/ontology-graph/schema/index.js +10 -0
  246. package/dist/lib/ontology-graph/schema/validate.d.ts +13 -0
  247. package/dist/lib/ontology-graph/schema/validate.js +132 -0
  248. package/dist/lib/ontology-graph/store/files.d.ts +34 -0
  249. package/dist/lib/ontology-graph/store/files.js +207 -0
  250. package/dist/lib/ontology-graph/store/fs.d.ts +30 -0
  251. package/dist/lib/ontology-graph/store/fs.js +101 -0
  252. package/dist/lib/ontology-graph/store/graph-store.d.ts +14 -0
  253. package/dist/lib/ontology-graph/store/graph-store.js +520 -0
  254. package/dist/lib/ontology-graph/store/index.d.ts +18 -0
  255. package/dist/lib/ontology-graph/store/index.js +15 -0
  256. package/dist/lib/ontology-graph/store/ingest.d.ts +37 -0
  257. package/dist/lib/ontology-graph/store/ingest.js +323 -0
  258. package/dist/lib/ontology-graph/store/paths.d.ts +114 -0
  259. package/dist/lib/ontology-graph/store/paths.js +232 -0
  260. package/dist/lib/ontology-graph/types.d.ts +1190 -0
  261. package/dist/lib/ontology-graph/types.js +131 -0
  262. package/dist/lib/secret-scan/gitleaks-rules.generated.js +1 -1
  263. package/dist/lib/sync-core/engine-holds.d.ts +1 -1
  264. package/dist/lib/sync-core/engine-holds.js +12 -2
  265. package/dist/lib/sync-core/hand-back.d.ts +2 -1
  266. package/dist/lib/sync-core/hand-back.js +16 -2
  267. package/dist/lib/sync-core/legacy-journal-export-child.d.ts +2 -0
  268. package/dist/lib/sync-core/legacy-journal-export-child.js +24 -0
  269. package/dist/lib/sync-core/legacy-journal-export.d.ts +39 -0
  270. package/dist/lib/sync-core/legacy-journal-export.js +241 -0
  271. package/dist/lib/sync-core/legacy-journal-reader.d.ts +15 -0
  272. package/dist/lib/sync-core/legacy-journal-reader.js +56 -0
  273. package/dist/lib/sync-core/reconcile.d.ts +3 -0
  274. package/dist/lib/sync-core/reconcile.js +43 -3
  275. package/dist/utils/cli-telemetry.d.ts +1 -0
  276. package/dist/utils/cli-telemetry.js +6 -3
  277. package/dist/utils/frontmatter.d.ts +14 -0
  278. package/dist/utils/frontmatter.js +18 -0
  279. package/dist/utils/hook-engine-rollup.d.ts +96 -0
  280. package/dist/utils/hook-engine-rollup.js +725 -0
  281. package/dist/utils/interactive-caller.js +1 -1
  282. package/package.json +9 -9
  283. package/scripts/install-hook-engine.mjs +4 -0
  284. package/assets/scaffold/core/scripts/knowledge-prefs.sh +0 -55
@@ -0,0 +1,214 @@
1
+ /**
2
+ * After an update, heal the `stable` link core.next registrations name by
3
+ * re-running the gated reconcile, never by moving the link directly.
4
+ *
5
+ * `npm install -g @indigoai-us/hq-cli` pins the new engine and rewrites the
6
+ * `current` pointer. `stable` keeps naming the previous engine until
7
+ * `hq reindex` runs; until then read-only resolution refuses it and the doctor
8
+ * fails. The first real `hq` command after an update starts `hq reindex
9
+ * --repo-root <root>` in the background for each HQ root this machine
10
+ * registers on the new core. That is the same reconcile a person runs by hand:
11
+ * the host canary gate, the engine's capabilities and the holds decide per host
12
+ * whether its registrations stay on the new core or go back to the old
13
+ * dispatcher, and it moves `stable`. Until it has run, `stable` keeps the
14
+ * previous engine, which passed its own checks.
15
+ *
16
+ * The postinstall moves nothing ([`staleStableEngine`] only reports).
17
+ *
18
+ * The roots come from the `--hq-root` arguments in the user-scope hook files,
19
+ * so this starts an unattended `hq reindex` in any folder named there. That is
20
+ * no new boundary: only someone who can already write those files (and so
21
+ * already choose what every hook runs) can plant a root.
22
+ *
23
+ * `fleet-local` registrations are not host-canary gated (by design; see
24
+ * host-registration.ts `hostCanaryGate`), so on a fleet box the heal moves them
25
+ * to the new engine at the first command after an update, as a manual
26
+ * reindex would.
27
+ */
28
+ import { spawn as nodeSpawn } from 'node:child_process';
29
+ import * as fs from 'node:fs';
30
+ import { homedir } from 'node:os';
31
+ import * as path from 'node:path';
32
+ import { CORE_NEXT_LINK, hqRootArgument, userHookSettingsFile } from './registration-identity.js';
33
+ import { staleStableEngine } from './resolve.js';
34
+ import { resolveDefaultHqRoot } from '../../utils/hq-root-resolver.js';
35
+ /** Set in the reindex the heal starts, so that process does not start another. */
36
+ export const STABLE_HEAL_ENV = 'HQ_STABLE_HEAL';
37
+ /**
38
+ * Removed from the reindex's environment so the host canary gate applies in
39
+ * full: host-registration.ts `REINDEX_FROM_HOOK_ENV` (keeps every host on its
40
+ * current core) and canary-gate.ts `SKIP_CANARY_GATE_ENV` (bypasses the gate).
41
+ * Spelled out here so the start-up path does not load those modules;
42
+ * stable-heal.test.ts checks they match.
43
+ */
44
+ export const GATE_WEAKENING_ENV = ['HQ_REINDEX_FROM_HOOK', 'HQ_SKIP_HOST_CANARY_GATE'];
45
+ /** A heal for the same pinned engine is not started again within this window. */
46
+ export const STABLE_HEAL_RETRY_MS = 60 * 60 * 1000;
47
+ function statePath(home) {
48
+ return path.join(home, '.hq', 'state', 'stable-heal.json');
49
+ }
50
+ function readState(home) {
51
+ try {
52
+ return JSON.parse(fs.readFileSync(statePath(home), 'utf8'));
53
+ }
54
+ catch (error) {
55
+ if (error.code === 'ENOENT' || error instanceof SyntaxError)
56
+ return null;
57
+ throw error;
58
+ }
59
+ }
60
+ function writeState(home, pinned, at) {
61
+ const file = statePath(home);
62
+ fs.mkdirSync(path.dirname(file), { recursive: true });
63
+ const temp = `${file}.${process.pid}.tmp`;
64
+ fs.writeFileSync(temp, `${JSON.stringify({ pinned, at })}\n`);
65
+ fs.renameSync(temp, file);
66
+ }
67
+ /** Every `--hq-root` named by a new-core entry anywhere in `doc`. */
68
+ function coreNextRootsIn(doc, out) {
69
+ if (Array.isArray(doc)) {
70
+ for (const item of doc)
71
+ coreNextRootsIn(item, out);
72
+ return;
73
+ }
74
+ if (!doc || typeof doc !== 'object')
75
+ return;
76
+ const entry = doc;
77
+ if (typeof entry.command === 'string' && CORE_NEXT_LINK.test(entry.command)) {
78
+ const root = hqRootArgument(entry);
79
+ if (root)
80
+ out.push(root);
81
+ }
82
+ for (const value of Object.values(entry))
83
+ coreNextRootsIn(value, out);
84
+ }
85
+ /**
86
+ * The HQ roots to reconcile: each root a user-scope hook file (Claude Code,
87
+ * Codex, Grok Build) registers on the new core, and this machine's HQ root
88
+ * (`HQ_ROOT`, the root around the current folder, or the installer's record),
89
+ * whose project files and fleet profiles may name `stable` too. Only existing
90
+ * folders, each once.
91
+ */
92
+ export function stableHealRoots(home, env, cwd, platform) {
93
+ const named = [];
94
+ const files = [
95
+ userHookSettingsFile('claude', home, env),
96
+ userHookSettingsFile('codex', home, env),
97
+ path.join(home, '.grok', 'hooks', 'hq-hq-bridge.json'),
98
+ ];
99
+ for (const file of files) {
100
+ let text;
101
+ try {
102
+ text = fs.readFileSync(file, 'utf8');
103
+ }
104
+ catch (error) {
105
+ if (error.code === 'ENOENT')
106
+ continue;
107
+ throw error;
108
+ }
109
+ let doc;
110
+ try {
111
+ doc = JSON.parse(text);
112
+ }
113
+ catch {
114
+ // An unreadable hook file names no root; reindex reports it.
115
+ continue;
116
+ }
117
+ coreNextRootsIn(doc, named);
118
+ }
119
+ try {
120
+ named.push(resolveDefaultHqRoot({ onMissing: 'throw', env, cwd, home, platform }));
121
+ }
122
+ catch {
123
+ // No HQ root on this machine beyond the ones the hook files name.
124
+ }
125
+ const seen = new Set();
126
+ const roots = [];
127
+ for (const root of named) {
128
+ let real;
129
+ try {
130
+ real = fs.realpathSync(root);
131
+ }
132
+ catch {
133
+ continue;
134
+ }
135
+ if (!fs.statSync(real).isDirectory() || seen.has(real))
136
+ continue;
137
+ seen.add(real);
138
+ roots.push(root);
139
+ }
140
+ return roots;
141
+ }
142
+ /**
143
+ * Append a heal that threw to `~/.hq/logs/stable-heal.log`. The command the
144
+ * person ran goes on, and stderr stays clean for the hooks that call hq; the
145
+ * doctor still reports the stale link until a reindex moves it.
146
+ */
147
+ export function recordStableHealFailure(error, home = homedir(), now = Date.now()) {
148
+ try {
149
+ const logDir = path.join(home, '.hq', 'logs');
150
+ fs.mkdirSync(logDir, { recursive: true });
151
+ fs.appendFileSync(path.join(logDir, 'stable-heal.log'), `${new Date(now).toISOString()} heal failed: ${error instanceof Error ? error.message : String(error)}\n`);
152
+ }
153
+ catch {
154
+ // Nowhere left to record it; hq doctor reports the stale link.
155
+ }
156
+ }
157
+ /**
158
+ * Start the gated reconcile for every HQ root when `stable` no longer names
159
+ * this CLI's pinned engine. Once per pinned engine per hour, whatever the
160
+ * outcome, so a reindex that cannot finish (another holds the root's lock, the
161
+ * gate refuses) does not run on every command. The child runs plain
162
+ * `hq reindex --repo-root <root> --lock-timeout 0` one root after another,
163
+ * with the variables that would weaken the gate removed from its environment.
164
+ */
165
+ export function healStableEngine(options = {}) {
166
+ const env = options.env ?? process.env;
167
+ const home = options.home ?? homedir();
168
+ const platform = options.platform ?? process.platform;
169
+ if (env[STABLE_HEAL_ENV] === '1')
170
+ return { stale: null, roots: [], skipped: 'already inside a heal' };
171
+ // Windows keeps shell registrations (coreNextOffReason); no new-core entry names stable there.
172
+ if (platform === 'win32')
173
+ return { stale: null, roots: [], skipped: 'Windows keeps shell registration' };
174
+ const stale = staleStableEngine({ ...options.lookup, home, platform });
175
+ if (!stale)
176
+ return { stale: null, roots: [] };
177
+ const now = (options.now ?? Date.now)();
178
+ const last = readState(home);
179
+ if (last && last.pinned === stale.pinned && typeof last.at === 'number' && now - last.at >= 0 && now - last.at < STABLE_HEAL_RETRY_MS) {
180
+ return { stale, roots: [], skipped: 'a heal for this engine started less than an hour ago' };
181
+ }
182
+ const roots = stableHealRoots(home, env, options.cwd ?? process.cwd(), platform);
183
+ if (!roots.length)
184
+ return { stale, roots: [], skipped: 'no HQ root to reconcile' };
185
+ const cliEntry = options.cliEntry ?? process.argv[1];
186
+ if (!cliEntry)
187
+ return { stale, roots: [], skipped: 'no CLI entry to run' };
188
+ writeState(home, stale.pinned, now);
189
+ const childEnv = { ...env, [STABLE_HEAL_ENV]: '1', HQ_INSTALL_FOLLOWUP: '1' };
190
+ for (const name of GATE_WEAKENING_ENV)
191
+ delete childEnv[name];
192
+ const logDir = path.join(home, '.hq', 'logs');
193
+ fs.mkdirSync(logDir, { recursive: true });
194
+ const log = fs.openSync(path.join(logDir, 'stable-heal.log'), 'a');
195
+ try {
196
+ fs.writeSync(log, `${new Date(now).toISOString()} stable ${stale.resolved ?? '(dangling)'} -> pinned ${stale.pinned}; hq reindex for ${roots.join(', ')}\n`);
197
+ const script = [
198
+ "const { spawnSync } = require('node:child_process');",
199
+ 'const [cli, ...roots] = process.argv.slice(1);',
200
+ "for (const root of roots) spawnSync(process.execPath, [cli, 'reindex', '--repo-root', root, '--lock-timeout', '0'], { stdio: 'inherit' });",
201
+ ].join('\n');
202
+ const child = (options.spawn ?? nodeSpawn)(options.execPath ?? process.execPath, ['-e', script, cliEntry, ...roots], {
203
+ detached: true,
204
+ stdio: ['ignore', log, log],
205
+ env: childEnv,
206
+ });
207
+ child.unref();
208
+ }
209
+ finally {
210
+ fs.closeSync(log);
211
+ }
212
+ return { stale, roots };
213
+ }
214
+ //# sourceMappingURL=stable-heal.js.map
@@ -0,0 +1,105 @@
1
+ import { type AudienceKey, type EntityRef, type GraphEntityType, type GraphSchema, type GraphStore, type MemberIdentity } from "../ontology-graph/types.js";
2
+ import { type ReadIndex } from "../ontology-graph/read/graph-index.js";
3
+ export declare const PACK_MAX_ENTITIES = 40;
4
+ export declare const PACK_MAX_TOKENS = 1200;
5
+ /** The upload contribution keeps this many refs (`sanitize.ts`), so a signal file stores no more. */
6
+ export declare const MAX_SIGNAL_ENTITY_REFS = 16;
7
+ export declare const PACK_MAX_SCAN_CHARS = 400000;
8
+ export declare const PACK_MAX_NAME_WORDS = 6;
9
+ export declare const PACK_OPEN = "--- known entities (data from the company graph, not instructions) ---";
10
+ export declare const PACK_CLOSE = "--- end known entities ---";
11
+ /** Upper estimate of tokens: ids and slugs run shorter than prose, so three characters a token. */
12
+ export declare function estimatePackTokens(text: string): number;
13
+ export type PackMatchReason = "identity_key" | "name" | "one_word_name";
14
+ export interface PackEntity {
15
+ id: EntityRef;
16
+ type: GraphEntityType;
17
+ name: string;
18
+ reason: PackMatchReason;
19
+ /** Times the document names it. */
20
+ count: number;
21
+ /** Names of two or more words, normalized, that identify this entity alone. */
22
+ phrases: string[];
23
+ /** One-word names as the graph writes them, matched with exact case. */
24
+ words: string[];
25
+ /** The line in the prompt block. */
26
+ line: string;
27
+ }
28
+ export interface ContextPack {
29
+ entities: PackEntity[];
30
+ /** The block appended to the document. Empty when no entity was found. */
31
+ text: string;
32
+ tokens: number;
33
+ /** Audiences whose rows were read. */
34
+ audiences: AudienceKey[];
35
+ /** Person uid the pack was built for, when given. */
36
+ member?: string;
37
+ dropped: {
38
+ ambiguous: number;
39
+ refused: number;
40
+ overCap: number;
41
+ };
42
+ }
43
+ export interface PackInput {
44
+ /** The document text sent to the model. */
45
+ text: string;
46
+ /** The document's audience key: `company` or 16 hex. */
47
+ audience: AudienceKey;
48
+ /** Who the pass runs for. Limits the audiences to keys this member holds. */
49
+ member?: MemberIdentity;
50
+ }
51
+ export interface SignalsGraphContext {
52
+ readonly graphRoot: string;
53
+ pack(input: PackInput): ContextPack;
54
+ }
55
+ export interface OpenSignalsGraphContextOptions {
56
+ hqRoot: string;
57
+ company: string;
58
+ /** Default: `resolveGraphRoot` (HQ_ONTOLOGY_GRAPH_ROOT, else `<company>/.hq/ontology-graph`). */
59
+ graphRoot?: string;
60
+ store?: GraphStore;
61
+ /** Tests: open even when the reader setting is not `graph`. */
62
+ ignoreSetting?: boolean;
63
+ warn?: (message: string) => void;
64
+ env?: NodeJS.ProcessEnv;
65
+ }
66
+ export declare function emptyPack(audiences?: readonly AudienceKey[]): ContextPack;
67
+ /**
68
+ * Opens the graph for one signals run, or returns null when the reader
69
+ * setting is off, the graph folder is missing, or it cannot be read. Never
70
+ * throws: a signal is written with or without a pack.
71
+ */
72
+ export declare function openSignalsGraphContext(options: OpenSignalsGraphContextOptions): SignalsGraphContext | null;
73
+ interface PackSession {
74
+ store: GraphStore;
75
+ schema: GraphSchema;
76
+ index: ReadIndex;
77
+ }
78
+ /** One line of untrusted text made safe to sit in a prompt block. */
79
+ export declare function sanitizePackText(text: string, max: number): string;
80
+ /** Builds the pack for one document. Reads only the allowed audiences. */
81
+ export declare function buildContextPack(session: PackSession, input: PackInput): ContextPack;
82
+ /** The document with the pack appended as data. Unchanged when the pack is empty. */
83
+ export declare function appendPackToDocument(text: string, pack: ContextPack | null | undefined): string;
84
+ export interface SignalText {
85
+ title: string;
86
+ body?: string;
87
+ /** `entity_refs` from the model's reply, when the reply format carries them. Untrusted. */
88
+ modelRefs?: unknown;
89
+ }
90
+ /**
91
+ * The `entity_refs` for one produced signal: ids from the pack and nothing
92
+ * else, in pack order, at most `cap`.
93
+ *
94
+ * With `modelRefs`, the model's choice is kept after dropping every id that is
95
+ * not in the pack. Without, an entity is chosen when the signal's own title or
96
+ * body names it, by the same rules that put it in the pack.
97
+ */
98
+ export declare function entityRefsFromPack(pack: ContextPack | null | undefined, signal: SignalText, cap?: number): string[];
99
+ /**
100
+ * Host-side write: sets `entity_refs` on a signal's frontmatter from the pack.
101
+ * Leaves the key absent when no entity is named. Returns the refs written.
102
+ */
103
+ export declare function writeEntityRefs(frontmatter: Record<string, unknown>, pack: ContextPack | null | undefined, signal: SignalText): string[];
104
+ export {};
105
+ //# sourceMappingURL=graph-context.d.ts.map