@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,50 @@
1
+ import { runBounded } from "../../hot-channels/signals-pass.js";
2
+ import type { ModelRunner, ModelUsage } from "../types.js";
3
+ /** The cheapest model. One setting raises it (see `resolveModel`). */
4
+ export declare const DEFAULT_ONTOLOGY_MODEL = "haiku";
5
+ /** Per-turn wall clock budget. A batch reply is longer than one signals reply. */
6
+ export declare const DEFAULT_MODEL_TURN_TIMEOUT_MS = 240000;
7
+ export type ModelRunnerFailure = "usage-limit" | "logged-out" | "timeout" | "provider-error";
8
+ /** A call that did not produce a reply. `failure` says whether calling again can help. */
9
+ export declare class ModelRunnerError extends Error {
10
+ readonly failure: ModelRunnerFailure;
11
+ constructor(failure: ModelRunnerFailure, message: string);
12
+ }
13
+ /** Usage with what the CLI reported beyond the two totals. */
14
+ export interface DetailedModelUsage extends ModelUsage {
15
+ /** True when the CLI reported no usage and the numbers are bytes / 4. */
16
+ estimated?: boolean;
17
+ /** Part of `inputTokens` read from the provider's prompt cache. */
18
+ cacheReadTokens?: number;
19
+ /** Part of `inputTokens` written to the provider's prompt cache. */
20
+ cacheCreationTokens?: number;
21
+ /** Part of `outputTokens` spent on thinking. */
22
+ thinkingTokens?: number;
23
+ /** List-price cost the CLI reported. On a subscription nothing is billed per call. */
24
+ costUsd?: number;
25
+ /** The model id the CLI says answered. */
26
+ servedBy?: string;
27
+ }
28
+ export interface ClaudeCliRunnerOptions {
29
+ /** The `claude` CLI; resolved on PATH by default. */
30
+ claudeBin?: string;
31
+ timeoutMs?: number;
32
+ authTimeoutMs?: number;
33
+ env?: NodeJS.ProcessEnv;
34
+ /** Test seam: the bounded spawn. */
35
+ run?: typeof runBounded;
36
+ }
37
+ /**
38
+ * Usage from a `claude -p --output-format json` envelope. Input is everything
39
+ * the turn sent: fresh tokens plus prompt-cache writes and reads. When the
40
+ * envelope has no usage the numbers are estimated from bytes and labeled.
41
+ */
42
+ export declare function usageFromEnvelope(envelope: Record<string, unknown>, sent: string, reply: string): DetailedModelUsage;
43
+ /**
44
+ * The default runner. The first call checks `claude auth status` once and
45
+ * refuses a login that is an API key or another provider, as the signals pass
46
+ * does. Every failure is a ModelRunnerError; the step turns it into `pending`
47
+ * work, never into a failed run.
48
+ */
49
+ export declare function createClaudeCliRunner(options?: ClaudeCliRunnerOptions): ModelRunner;
50
+ //# sourceMappingURL=runner.d.ts.map
@@ -0,0 +1,160 @@
1
+ /**
2
+ * WP-D: the model runner. One tool-less, single-turn `claude -p` call on the
3
+ * subscription sign-in, built from the same pieces the hot-channels signals
4
+ * pass uses (isolation flags, environment allowlist, bounded spawn, the
5
+ * API-key refusal). There is no second sign-in path here: the helpers are
6
+ * imported, not copied, and an API key never reaches the call because the
7
+ * environment is the signals pass's allowlist.
8
+ */
9
+ import * as os from "node:os";
10
+ import { AUTH_PREFLIGHT_TIMEOUT_MS, CLAUDE_ISOLATION_ARGS, CLAUDE_LOGGED_OUT_RE, claudeApiKeyLogin, claudeSignalsEnv, runBounded, } from "../../hot-channels/signals-pass.js";
11
+ import { isRuntimeUsageLimit } from "../../bot/runtime-failure.js";
12
+ import { redactErrorText } from "../../../utils/redact-error-text.js";
13
+ import { estimateTokens } from "./files.js";
14
+ /** The cheapest model. One setting raises it (see `resolveModel`). */
15
+ export const DEFAULT_ONTOLOGY_MODEL = "haiku";
16
+ /** Per-turn wall clock budget. A batch reply is longer than one signals reply. */
17
+ export const DEFAULT_MODEL_TURN_TIMEOUT_MS = 240_000;
18
+ /** A call that did not produce a reply. `failure` says whether calling again can help. */
19
+ export class ModelRunnerError extends Error {
20
+ failure;
21
+ constructor(failure, message) {
22
+ super(message);
23
+ this.name = "ModelRunnerError";
24
+ this.failure = failure;
25
+ }
26
+ }
27
+ function reason(text) {
28
+ const cut = redactErrorText(text).trim().slice(0, 300);
29
+ return cut ? `: ${cut}` : "";
30
+ }
31
+ function classify(text, fallback) {
32
+ if (isRuntimeUsageLimit(text))
33
+ return new ModelRunnerError("usage-limit", `claude is out of usage${reason(text)}`);
34
+ if (CLAUDE_LOGGED_OUT_RE.test(text))
35
+ return new ModelRunnerError("logged-out", `claude reported it is not logged in${reason(text)}`);
36
+ return new ModelRunnerError("provider-error", `${fallback}${reason(text)}`);
37
+ }
38
+ function envelopeError(envelope) {
39
+ const subtype = typeof envelope.subtype === "string" ? envelope.subtype : "";
40
+ if (envelope.is_error !== true && !subtype.startsWith("error"))
41
+ return null;
42
+ const parts = [];
43
+ if (typeof envelope.api_error_status === "number")
44
+ parts.push(`status ${envelope.api_error_status}`);
45
+ if (subtype.startsWith("error"))
46
+ parts.push(subtype);
47
+ if (typeof envelope.result === "string")
48
+ parts.push(envelope.result.slice(0, 300));
49
+ return parts.join(" ");
50
+ }
51
+ function num(value) {
52
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
53
+ }
54
+ /**
55
+ * Usage from a `claude -p --output-format json` envelope. Input is everything
56
+ * the turn sent: fresh tokens plus prompt-cache writes and reads. When the
57
+ * envelope has no usage the numbers are estimated from bytes and labeled.
58
+ */
59
+ export function usageFromEnvelope(envelope, sent, reply) {
60
+ const usage = envelope.usage;
61
+ if (!usage || typeof usage !== "object") {
62
+ return { inputTokens: estimateTokens(sent), outputTokens: estimateTokens(reply), estimated: true };
63
+ }
64
+ const u = usage;
65
+ const fresh = num(u.input_tokens);
66
+ const cacheCreationTokens = num(u.cache_creation_input_tokens);
67
+ const cacheReadTokens = num(u.cache_read_input_tokens);
68
+ const outputTokens = num(u.output_tokens);
69
+ if (fresh + cacheCreationTokens + cacheReadTokens + outputTokens === 0) {
70
+ return { inputTokens: estimateTokens(sent), outputTokens: estimateTokens(reply), estimated: true };
71
+ }
72
+ const details = u.output_tokens_details;
73
+ const thinkingTokens = details && typeof details === "object" ? num(details.thinking_tokens) : 0;
74
+ const out = {
75
+ inputTokens: fresh + cacheCreationTokens + cacheReadTokens,
76
+ outputTokens,
77
+ cacheReadTokens,
78
+ cacheCreationTokens,
79
+ thinkingTokens,
80
+ };
81
+ if (typeof envelope.total_cost_usd === "number")
82
+ out.costUsd = envelope.total_cost_usd;
83
+ const served = envelope.modelUsage;
84
+ if (served && typeof served === "object") {
85
+ const names = Object.keys(served);
86
+ if (names.length > 0)
87
+ out.servedBy = names.join(",");
88
+ }
89
+ return out;
90
+ }
91
+ /**
92
+ * The default runner. The first call checks `claude auth status` once and
93
+ * refuses a login that is an API key or another provider, as the signals pass
94
+ * does. Every failure is a ModelRunnerError; the step turns it into `pending`
95
+ * work, never into a failed run.
96
+ */
97
+ export function createClaudeCliRunner(options = {}) {
98
+ const claude = options.claudeBin ?? "claude";
99
+ const timeoutMs = options.timeoutMs ?? DEFAULT_MODEL_TURN_TIMEOUT_MS;
100
+ const authTimeoutMs = options.authTimeoutMs ?? AUTH_PREFLIGHT_TIMEOUT_MS;
101
+ const env = claudeSignalsEnv(options.env ?? process.env);
102
+ const run = options.run ?? runBounded;
103
+ // Outside the HQ tree, so no project context or hooks load.
104
+ const cwd = os.tmpdir();
105
+ let preflight;
106
+ const checkLogin = async () => {
107
+ const status = await run(claude, ["auth", "status"], { timeoutMs: authTimeoutMs, cwd, env });
108
+ if (status.timedOut)
109
+ throw new ModelRunnerError("timeout", "claude auth status timed out");
110
+ if (status.spawnError !== undefined || status.code !== 0) {
111
+ throw new ModelRunnerError("logged-out", "claude is not logged in (claude auth status failed)");
112
+ }
113
+ let parsed;
114
+ try {
115
+ parsed = JSON.parse(status.stdout);
116
+ }
117
+ catch {
118
+ return; // A plain-text status with exit 0 means logged in.
119
+ }
120
+ if (!parsed || typeof parsed !== "object")
121
+ return;
122
+ const record = parsed;
123
+ if (record.loggedIn === false)
124
+ throw new ModelRunnerError("logged-out", "claude is not logged in");
125
+ const notSubscription = claudeApiKeyLogin(record);
126
+ if (notSubscription) {
127
+ throw new ModelRunnerError("logged-out", `claude is signed in with an API key or another provider (${notSubscription}), not a Claude subscription login; the ontology model step never uses an API key`);
128
+ }
129
+ };
130
+ return async (call) => {
131
+ preflight ??= checkLogin();
132
+ await preflight;
133
+ const result = await run(claude, ["-p", call.prompt, ...CLAUDE_ISOLATION_ARGS, "--output-format", "json", "--max-turns", "1", "--model", call.model], { timeoutMs, input: call.input, cwd, env });
134
+ if (result.timedOut)
135
+ throw new ModelRunnerError("timeout", `claude timed out after ${timeoutMs}ms`);
136
+ if (result.spawnError !== undefined)
137
+ throw new ModelRunnerError("logged-out", `claude could not start: ${result.spawnError}`);
138
+ let envelope = null;
139
+ try {
140
+ const parsed = JSON.parse(result.stdout);
141
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
142
+ envelope = parsed;
143
+ }
144
+ catch {
145
+ envelope = null;
146
+ }
147
+ if (result.code !== 0) {
148
+ const text = `${envelope ? (envelopeError(envelope) ?? "") : ""}\n${result.stderr}`.trim();
149
+ throw classify(text, `claude exited with code ${String(result.code)}`);
150
+ }
151
+ if (!envelope)
152
+ throw new ModelRunnerError("provider-error", `claude printed no JSON envelope (stdout ${Buffer.byteLength(result.stdout)} bytes)`);
153
+ const failed = envelopeError(envelope);
154
+ if (failed !== null)
155
+ throw classify(failed, "claude reported an error");
156
+ const output = typeof envelope.result === "string" ? envelope.result : "";
157
+ return { output, usage: usageFromEnvelope(envelope, `${call.prompt}\n${call.input}`, output) };
158
+ };
159
+ }
160
+ //# sourceMappingURL=runner.js.map
@@ -0,0 +1,197 @@
1
+ /**
2
+ * WP-D: the cached, budgeted model step.
3
+ *
4
+ * - Cache: one file per item under `.state/cache/`, keyed by
5
+ * `sha256(promptVersion, model, kind, input)`. A second run over unchanged
6
+ * input makes 0 calls. The key holds nothing about the graph folder, and
7
+ * `stateDir` can put the cache outside it, so a graph that is rebuilt from
8
+ * scratch gets every earlier reply back as a cache hit.
9
+ * - Parallel turns: `concurrency` turns run at once (default 1).
10
+ * - Batches: many items per turn. The reply is one JSON object; each item's
11
+ * answer is validated on its own and cached on its own, so a later run with
12
+ * a different batch mix still hits the cache.
13
+ * - Bad output: a reply that is not JSON is retried once; items whose answer
14
+ * is missing or invalid are retried once; what is still bad is parked
15
+ * (`.state/model-parked.jsonl`) and not sent again.
16
+ * - Budget: per-day and per-run token caps. When a cap is reached, or the
17
+ * account is out of usage or signed out, calls stop and every unsent item
18
+ * gets a pending marker (`.state/model-pending.json`) that the next run picks
19
+ * up. A run never fails because of the model.
20
+ * - Ledger: one line per call in `.state/model-ledger.jsonl` with input and
21
+ * output tokens and wall time. Tokens are the CLI's numbers; when the CLI
22
+ * reports none they are bytes / 4 and the line says `estimated: true`.
23
+ */
24
+ import type { IsoDate, ModelBudget, ModelRunner, ModelStep, ModelStepKind, ModelUsage } from "../types.js";
25
+ /** The contract's kinds plus the graph pass's own (pass-tasks.ts). The kind is part of the cache key and names the task in the call ledger. */
26
+ export type ModelTaskKind = ModelStepKind | "extract-source" | "digest-source" | "profile-entity" | "retype-edge" | "decision-supersession";
27
+ export type PendingReason = "budget-day" | "budget-run" | "dry-run" | "usage-limit" | "logged-out" | "timeout" | "provider-error";
28
+ /** Left for the next run when an item could not be sent. */
29
+ export interface PendingMarker {
30
+ /** The item's cache key. */
31
+ key: string;
32
+ kind: ModelTaskKind;
33
+ /** The task's own id for the item (an entity ref, a pair id, a source ref). */
34
+ itemKey: string;
35
+ day: IsoDate;
36
+ reason: PendingReason;
37
+ promptVersion: string;
38
+ at: string;
39
+ /** The task item, so the next run can send it without rebuilding it. */
40
+ payload: unknown;
41
+ }
42
+ export interface ParkedRecord {
43
+ key: string;
44
+ kind: ModelTaskKind;
45
+ itemKey: string;
46
+ day: IsoDate;
47
+ reason: string;
48
+ attempts: number;
49
+ at: string;
50
+ }
51
+ /** One line of `.state/model-ledger.jsonl`. */
52
+ export interface ModelCallLedgerLine {
53
+ type: "call";
54
+ at: string;
55
+ run: string;
56
+ day: IsoDate;
57
+ kind: ModelTaskKind;
58
+ model: string;
59
+ promptVersion: string;
60
+ /** Items in the turn. */
61
+ items: number;
62
+ attempt: number;
63
+ outcome: "ok" | "partial" | "bad-json" | `error:${string}`;
64
+ inputTokens: number;
65
+ outputTokens: number;
66
+ /** True when the CLI reported no usage and the tokens are bytes / 4. */
67
+ estimated: boolean;
68
+ cacheReadTokens?: number;
69
+ cacheCreationTokens?: number;
70
+ thinkingTokens?: number;
71
+ costUsd?: number;
72
+ servedBy?: string;
73
+ promptBytes: number;
74
+ inputBytes: number;
75
+ outputBytes: number;
76
+ /** Wall time of the call. */
77
+ ms: number;
78
+ }
79
+ export interface BatchSpec<I, O> {
80
+ kind: ModelTaskKind;
81
+ /** Bumped whenever the prompt text or the reply shape changes. Part of the cache key. */
82
+ promptVersion: string;
83
+ /** Instructions. Sent as the `-p` prompt; the items go on stdin as data. */
84
+ prompt: string;
85
+ /** The day the calls are charged to. */
86
+ day: IsoDate;
87
+ itemKey(item: I): string;
88
+ /** Canonical, JSON-serializable form of the item. Part of the cache key. */
89
+ renderItem(item: I): unknown;
90
+ /** Checks one item's answer. Anything not `ok` is retried once, then parked. */
91
+ validate(item: I, raw: Record<string, unknown>): {
92
+ ok: true;
93
+ value: O;
94
+ } | {
95
+ ok: false;
96
+ reason: string;
97
+ };
98
+ /** Items per turn. Default 20. */
99
+ maxItems?: number;
100
+ /** Bytes of item data per turn. Default 48,000. */
101
+ maxInputBytes?: number;
102
+ }
103
+ export type BatchItemStatus = "ok" | "cached" | "pending" | "parked";
104
+ export interface BatchItemResult<I, O> {
105
+ item: I;
106
+ itemKey: string;
107
+ cacheKey: string;
108
+ status: BatchItemStatus;
109
+ value?: O;
110
+ /** Why the item is pending or parked. */
111
+ reason?: string;
112
+ /** This item's share of the calls it took part in. 0 for a cache hit. */
113
+ usage: ModelUsage;
114
+ }
115
+ export interface BatchRunStats {
116
+ items: number;
117
+ cacheHits: number;
118
+ calls: number;
119
+ ok: number;
120
+ pending: number;
121
+ parked: number;
122
+ usage: ModelUsage;
123
+ /** True when any call's tokens were estimated from bytes. */
124
+ estimated: boolean;
125
+ seconds: number;
126
+ }
127
+ export interface BatchRunResult<I, O> {
128
+ results: BatchItemResult<I, O>[];
129
+ stats: BatchRunStats;
130
+ }
131
+ export interface RunTotals {
132
+ calls: number;
133
+ cacheHits: number;
134
+ pending: number;
135
+ parked: number;
136
+ errors: number;
137
+ used: ModelUsage;
138
+ estimatedCalls: number;
139
+ wallMs: number;
140
+ /** Cache hits / (cache hits + items sent to the model). 1 when nothing was sent. */
141
+ cacheHitRate: number;
142
+ /** Items this step left pending, by why. `budget-day` is the daily cap, `budget-run` the run's own cap. */
143
+ pendingByReason: Partial<Record<PendingReason, number>>;
144
+ }
145
+ /** The step as this package's tasks use it. A superset of the contract's `ModelStep`. */
146
+ export interface BatchModelStep extends ModelStep {
147
+ readonly model: string;
148
+ readonly runId: string;
149
+ runBatch<I, O>(spec: BatchSpec<I, O>, items: readonly I[]): Promise<BatchRunResult<I, O>>;
150
+ /** Markers left by this or an earlier run, oldest first. */
151
+ pending(kind?: ModelTaskKind): PendingMarker[];
152
+ parked(kind?: ModelTaskKind): ParkedRecord[];
153
+ /** Totals since the step was created (not reset by `resetDay`). */
154
+ totals(): RunTotals;
155
+ /** Why calls have stopped for the rest of this run, if they have. */
156
+ halted(): PendingReason | null;
157
+ }
158
+ export interface ModelStepConfig {
159
+ graphRoot: string;
160
+ model: string;
161
+ /** Per-day caps. */
162
+ budget: ModelBudget;
163
+ /** Per-run caps (since the step was created). Default: no cap. */
164
+ runBudget?: ModelBudget;
165
+ runner: ModelRunner;
166
+ dryRun?: boolean;
167
+ /** Send parked items again. Default false. */
168
+ retryParked?: boolean;
169
+ /** Run id written to the ledger. */
170
+ runId?: string;
171
+ now?: () => Date;
172
+ /**
173
+ * Folder for the cache, the call ledger, pending markers and parked items.
174
+ * Default: `<graphRoot>/.state`. Point it outside the graph folder and the
175
+ * replies survive a rebuild that moves the folder aside.
176
+ */
177
+ stateDir?: string;
178
+ /** Turns in flight at once. Default 1. */
179
+ concurrency?: number;
180
+ }
181
+ /** Tokens a `claude -p` turn adds on top of our prompt and input (system prompt). Measured at about 3,000. */
182
+ export declare const CLI_OVERHEAD_TOKENS = 3000;
183
+ /**
184
+ * `sha256(promptVersion + "\n" + model + "\n" + kind + "\n" + input)`, hex.
185
+ * Without a model it is the contract's original form
186
+ * `sha256(promptVersion + "\n" + kind + "\n" + input)`.
187
+ */
188
+ export declare function cacheKeyFor(parts: {
189
+ promptVersion: string;
190
+ kind: string;
191
+ input: string;
192
+ model?: string;
193
+ }): string;
194
+ /** The reply as one JSON object, or null. One code fence around the object is tolerated; prose is not. */
195
+ export declare function parseStrictJsonObject(reply: string): Record<string, unknown> | null;
196
+ export declare function createBatchModelStep(config: ModelStepConfig): BatchModelStep;
197
+ //# sourceMappingURL=step.d.ts.map