@arnilo/prism-hosts 0.0.0-stage → 0.13.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 (322) hide show
  1. package/CHANGELOG.md +394 -0
  2. package/LICENSE +21 -0
  3. package/README.md +34 -2
  4. package/acp/README.md +87 -0
  5. package/acp/src/capabilities.ts +184 -0
  6. package/acp/src/errors.ts +24 -0
  7. package/acp/src/host.ts +36 -0
  8. package/acp/src/index.ts +25 -0
  9. package/acp/src/limits.ts +49 -0
  10. package/acp/src/mapper.ts +419 -0
  11. package/acp/src/permission.ts +37 -0
  12. package/acp/src/projection.ts +39 -0
  13. package/acp/src/prompt.ts +170 -0
  14. package/acp/src/serve.ts +345 -0
  15. package/acp/src/session-map.ts +49 -0
  16. package/ag-ui/README.md +56 -0
  17. package/ag-ui/src/a2ui.ts +570 -0
  18. package/ag-ui/src/ag-ui-mapper.ts +507 -0
  19. package/ag-ui/src/encrypted-value.ts +40 -0
  20. package/ag-ui/src/errors.ts +29 -0
  21. package/ag-ui/src/handler.ts +642 -0
  22. package/ag-ui/src/index.ts +177 -0
  23. package/ag-ui/src/input.ts +190 -0
  24. package/ag-ui/src/limits.ts +279 -0
  25. package/ag-ui/src/mcp-apps.ts +256 -0
  26. package/ag-ui/src/mcp.ts +94 -0
  27. package/ag-ui/src/projection.ts +201 -0
  28. package/ag-ui/src/projectors.ts +272 -0
  29. package/ag-ui/src/renderer/bind.ts +152 -0
  30. package/ag-ui/src/renderer/core.ts +270 -0
  31. package/ag-ui/src/renderer/index.ts +165 -0
  32. package/ag-ui/src/replay.ts +189 -0
  33. package/ag-ui/src/types.ts +66 -0
  34. package/channels/README.md +139 -0
  35. package/channels/src/admission.ts +8 -0
  36. package/channels/src/approvals.ts +262 -0
  37. package/channels/src/checkpoint-contract.ts +97 -0
  38. package/channels/src/delivery.ts +116 -0
  39. package/channels/src/host-ui.ts +356 -0
  40. package/channels/src/identity.ts +46 -0
  41. package/channels/src/index.ts +82 -0
  42. package/channels/src/limits.ts +49 -0
  43. package/channels/src/memory-stores.ts +112 -0
  44. package/channels/src/pairing.ts +178 -0
  45. package/channels/src/runtime-admit.ts +389 -0
  46. package/channels/src/runtime-core.ts +621 -0
  47. package/channels/src/runtime-reconcile.ts +299 -0
  48. package/channels/src/runtime-turn.ts +664 -0
  49. package/channels/src/runtime-types.ts +95 -0
  50. package/channels/src/runtime.ts +86 -0
  51. package/channels/src/secret-ref.ts +42 -0
  52. package/channels/src/signal.ts +790 -0
  53. package/channels/src/state.ts +355 -0
  54. package/channels/src/telegram.ts +1317 -0
  55. package/channels/src/types.ts +442 -0
  56. package/devtools/README.md +92 -0
  57. package/devtools/bin/prism-devtools.ts +63 -0
  58. package/devtools/src/args.ts +71 -0
  59. package/devtools/src/bun.d.ts +27 -0
  60. package/devtools/src/compare.ts +190 -0
  61. package/devtools/src/errors.ts +15 -0
  62. package/devtools/src/index.ts +25 -0
  63. package/devtools/src/replay.ts +36 -0
  64. package/devtools/src/security.ts +80 -0
  65. package/devtools/src/serve.ts +259 -0
  66. package/devtools/src/ui/assets.ts +108 -0
  67. package/devtools/src/ui/inspector.ts +182 -0
  68. package/driver-process/README.md +197 -0
  69. package/driver-process/src/abort.ts +74 -0
  70. package/driver-process/src/adapter.ts +211 -0
  71. package/driver-process/src/driver.ts +303 -0
  72. package/driver-process/src/errors.ts +17 -0
  73. package/driver-process/src/extend.ts +72 -0
  74. package/driver-process/src/index.ts +5 -0
  75. package/driver-process/src/inspect.ts +33 -0
  76. package/driver-process/src/session.ts +72 -0
  77. package/driver-process/src/testing.ts +394 -0
  78. package/driver-process/src/transports/json-rpc.ts +147 -0
  79. package/driver-process/src/transports/jsonl-stream.ts +12 -0
  80. package/driver-process/src/transports/types.ts +11 -0
  81. package/driver-process/src/turn.ts +106 -0
  82. package/evals/README.md +843 -0
  83. package/evals/src/calibration.ts +179 -0
  84. package/evals/src/citation-integrity.ts +98 -0
  85. package/evals/src/citations.ts +45 -0
  86. package/evals/src/compare-experiments.ts +153 -0
  87. package/evals/src/comparison.ts +230 -0
  88. package/evals/src/curate.ts +257 -0
  89. package/evals/src/dataset.ts +81 -0
  90. package/evals/src/erp-invariants.ts +163 -0
  91. package/evals/src/errors.ts +23 -0
  92. package/evals/src/experiment.ts +537 -0
  93. package/evals/src/failure-injection.ts +77 -0
  94. package/evals/src/feedback.ts +53 -0
  95. package/evals/src/index.ts +198 -0
  96. package/evals/src/injection.ts +247 -0
  97. package/evals/src/judge-panel.ts +109 -0
  98. package/evals/src/judge.ts +89 -0
  99. package/evals/src/limits.ts +50 -0
  100. package/evals/src/report-record.ts +47 -0
  101. package/evals/src/rubric-judge.ts +334 -0
  102. package/evals/src/scenario-runner.ts +317 -0
  103. package/evals/src/scenarios.ts +81 -0
  104. package/evals/src/score.ts +328 -0
  105. package/evals/src/scorer.ts +21 -0
  106. package/evals/src/state-match.ts +130 -0
  107. package/evals/src/stats.ts +635 -0
  108. package/evals/src/threshold.ts +117 -0
  109. package/evals/src/timeline.ts +38 -0
  110. package/evals/src/toolless-session.ts +33 -0
  111. package/evals/src/trace.ts +80 -0
  112. package/evals/src/trajectory.ts +623 -0
  113. package/evals/src/types.ts +672 -0
  114. package/evals/src/user-simulator.ts +101 -0
  115. package/evals/src/util.ts +207 -0
  116. package/headless/README.md +172 -0
  117. package/headless/src/answers.ts +91 -0
  118. package/headless/src/authority.ts +32 -0
  119. package/headless/src/errors.ts +36 -0
  120. package/headless/src/index.ts +17 -0
  121. package/headless/src/limits.ts +11 -0
  122. package/headless/src/pending.ts +16 -0
  123. package/headless/src/policy.ts +36 -0
  124. package/headless/src/resume.ts +103 -0
  125. package/headless/src/route.ts +153 -0
  126. package/headless/src/scheduler/clock.ts +22 -0
  127. package/headless/src/scheduler/cron.ts +19 -0
  128. package/headless/src/scheduler/firing.ts +58 -0
  129. package/headless/src/scheduler/index.ts +399 -0
  130. package/headless/src/scheduler/lease.ts +38 -0
  131. package/headless/src/scheduler/missed.ts +12 -0
  132. package/headless/src/scheduler/state.ts +132 -0
  133. package/host-builder/README.md +251 -0
  134. package/host-builder/bin/prism-host-builder.ts +169 -0
  135. package/host-builder/data/api.json +6197 -0
  136. package/host-builder/data/errors.json +558 -0
  137. package/host-builder/data/examples/acp.ts +63 -0
  138. package/host-builder/data/examples/adapters/conformance.ts +34 -0
  139. package/host-builder/data/examples/adapters/fixtures/rpc-server.json +26 -0
  140. package/host-builder/data/examples/adapters/fixtures/stream-cli.json +27 -0
  141. package/host-builder/data/examples/adapters/fixtures/workspace-config.json +28 -0
  142. package/host-builder/data/examples/adapters/rpc-server.dialect.ts +218 -0
  143. package/host-builder/data/examples/adapters/rpc-server.ts +309 -0
  144. package/host-builder/data/examples/adapters/stream-cli.dialect.ts +165 -0
  145. package/host-builder/data/examples/adapters/stream-cli.ts +256 -0
  146. package/host-builder/data/examples/adapters/workspace-config.dialect.ts +172 -0
  147. package/host-builder/data/examples/adapters/workspace-config.ts +251 -0
  148. package/host-builder/data/examples/advisor.ts +94 -0
  149. package/host-builder/data/examples/ag-ui-a2ui.ts +80 -0
  150. package/host-builder/data/examples/ag-ui-mcp-apps.ts +44 -0
  151. package/host-builder/data/examples/ag-ui-server.ts +118 -0
  152. package/host-builder/data/examples/agent-durable-approval.ts +43 -0
  153. package/host-builder/data/examples/api-key-auth.ts +23 -0
  154. package/host-builder/data/examples/approval.ts +92 -0
  155. package/host-builder/data/examples/attention-compiler.ts +81 -0
  156. package/host-builder/data/examples/autonomous-coding-loop.ts +343 -0
  157. package/host-builder/data/examples/behavior-evaluation.ts +303 -0
  158. package/host-builder/data/examples/build-workflow.ts +334 -0
  159. package/host-builder/data/examples/cache-aware-prompt-assembly.ts +139 -0
  160. package/host-builder/data/examples/caveman-ponytail.ts +197 -0
  161. package/host-builder/data/examples/cli.ts +31 -0
  162. package/host-builder/data/examples/coding-approval.ts +115 -0
  163. package/host-builder/data/examples/coding-browser-evaluation.ts +208 -0
  164. package/host-builder/data/examples/coding-compaction.ts +30 -0
  165. package/host-builder/data/examples/coding-goal-verify.ts +121 -0
  166. package/host-builder/data/examples/coding-tools-capability-gaps.ts +75 -0
  167. package/host-builder/data/examples/compaction.ts +36 -0
  168. package/host-builder/data/examples/connected-slack-mcp.ts +67 -0
  169. package/host-builder/data/examples/context.ts +29 -0
  170. package/host-builder/data/examples/custom-adapter.ts +256 -0
  171. package/host-builder/data/examples/custom-builders.ts +66 -0
  172. package/host-builder/data/examples/custom-loop.ts +89 -0
  173. package/host-builder/data/examples/custom-provider.ts +70 -0
  174. package/host-builder/data/examples/custom-session-store.ts +72 -0
  175. package/host-builder/data/examples/custom-tools-skills-context.ts +97 -0
  176. package/host-builder/data/examples/decision-models.ts +98 -0
  177. package/host-builder/data/examples/define-package.ts +188 -0
  178. package/host-builder/data/examples/delegate.ts +200 -0
  179. package/host-builder/data/examples/discover-skills.ts +65 -0
  180. package/host-builder/data/examples/drive-rag-sync.ts +44 -0
  181. package/host-builder/data/examples/durable-approval.ts +139 -0
  182. package/host-builder/data/examples/durable-investigation.ts +93 -0
  183. package/host-builder/data/examples/durable-loops-and-approvals.ts +94 -0
  184. package/host-builder/data/examples/enterprise-identity.ts +21 -0
  185. package/host-builder/data/examples/enterprise-policy-audit.ts +50 -0
  186. package/host-builder/data/examples/enterprise-work-connectors.ts +103 -0
  187. package/host-builder/data/examples/eval-injection.ts +73 -0
  188. package/host-builder/data/examples/eval-judges.ts +145 -0
  189. package/host-builder/data/examples/evals.ts +86 -0
  190. package/host-builder/data/examples/evaluation-gate.ts +40 -0
  191. package/host-builder/data/examples/execution-timeline.ts +173 -0
  192. package/host-builder/data/examples/external-agent.ts +541 -0
  193. package/host-builder/data/examples/external-app-db-backed.ts +344 -0
  194. package/host-builder/data/examples/governance-deny.ts +122 -0
  195. package/host-builder/data/examples/guardrail-packs.ts +240 -0
  196. package/host-builder/data/examples/headless-host.ts +245 -0
  197. package/host-builder/data/examples/hooks-audit-command.mjs +31 -0
  198. package/host-builder/data/examples/hooks-json.ts +87 -0
  199. package/host-builder/data/examples/host-builder-agent.ts +150 -0
  200. package/host-builder/data/examples/host-hooks.ts +71 -0
  201. package/host-builder/data/examples/host-step-loop-timeline.ts +212 -0
  202. package/host-builder/data/examples/hosted-sandbox.ts +48 -0
  203. package/host-builder/data/examples/instruction-injection.ts +127 -0
  204. package/host-builder/data/examples/jsonl-stores-branching.ts +49 -0
  205. package/host-builder/data/examples/long-term-memory.ts +158 -0
  206. package/host-builder/data/examples/manifest.ts +112 -0
  207. package/host-builder/data/examples/mcp-server.ts +48 -0
  208. package/host-builder/data/examples/mcp-trust.ts +173 -0
  209. package/host-builder/data/examples/memory-and-wiki.ts +315 -0
  210. package/host-builder/data/examples/memory-lifecycle.ts +302 -0
  211. package/host-builder/data/examples/memory-system.ts +255 -0
  212. package/host-builder/data/examples/messaging-agent.ts +70 -0
  213. package/host-builder/data/examples/minimal-host-app.ts +41 -0
  214. package/host-builder/data/examples/model-router-aggregate-budgets.ts +364 -0
  215. package/host-builder/data/examples/model-router.ts +106 -0
  216. package/host-builder/data/examples/obscura.ts +48 -0
  217. package/host-builder/data/examples/observability.ts +116 -0
  218. package/host-builder/data/examples/open-connector-sidecar.ts +105 -0
  219. package/host-builder/data/examples/openai-compatible.ts +70 -0
  220. package/host-builder/data/examples/package-skills.ts +85 -0
  221. package/host-builder/data/examples/phase9-coding-intelligence.ts +221 -0
  222. package/host-builder/data/examples/provider-registration.ts +29 -0
  223. package/host-builder/data/examples/provider-resolver.ts +39 -0
  224. package/host-builder/data/examples/rag.ts +40 -0
  225. package/host-builder/data/examples/remote-agent-server.ts +45 -0
  226. package/host-builder/data/examples/remote-agent.ts +124 -0
  227. package/host-builder/data/examples/research-ledger.ts +79 -0
  228. package/host-builder/data/examples/revocation-propagation.ts +287 -0
  229. package/host-builder/data/examples/run-feedback.ts +93 -0
  230. package/host-builder/data/examples/scanned-document-rag.ts +48 -0
  231. package/host-builder/data/examples/sdk-basics.ts +21 -0
  232. package/host-builder/data/examples/secure-agent.ts +55 -0
  233. package/host-builder/data/examples/semantic-memory.ts +52 -0
  234. package/host-builder/data/examples/serve-tools.ts +495 -0
  235. package/host-builder/data/examples/session-memory.ts +318 -0
  236. package/host-builder/data/examples/session-search.ts +131 -0
  237. package/host-builder/data/examples/session.ts +52 -0
  238. package/host-builder/data/examples/side-call.ts +68 -0
  239. package/host-builder/data/examples/signal-agent.ts +9 -0
  240. package/host-builder/data/examples/skills-progressive-disclosure.ts +88 -0
  241. package/host-builder/data/examples/skills.ts +19 -0
  242. package/host-builder/data/examples/sqlite-resume.ts +147 -0
  243. package/host-builder/data/examples/system-project-prompts.ts +74 -0
  244. package/host-builder/data/examples/system-prompts.ts +19 -0
  245. package/host-builder/data/examples/telegram-agent.ts +30 -0
  246. package/host-builder/data/examples/tool-narrowing-planes.ts +99 -0
  247. package/host-builder/data/examples/tools.ts +29 -0
  248. package/host-builder/data/examples/validator.ts +67 -0
  249. package/host-builder/data/examples/web-no-key.ts +85 -0
  250. package/host-builder/data/examples/web-research.ts +35 -0
  251. package/host-builder/data/examples/workflow-event-sink.ts +114 -0
  252. package/host-builder/data/examples/workflow-loop.ts +93 -0
  253. package/host-builder/data/examples/workflow-multimodal-document.ts +94 -0
  254. package/host-builder/data/examples/workflow-parallel-research.ts +111 -0
  255. package/host-builder/data/examples/workflow-postgres-resume.ts +75 -0
  256. package/host-builder/data/examples/workflow-research-and-review.ts +98 -0
  257. package/host-builder/data/examples/workflow-sqlite-resume.ts +163 -0
  258. package/host-builder/data/examples.json +130 -0
  259. package/host-builder/skills/prism-host-builder/SKILL.md +85 -0
  260. package/host-builder/skills/prism-host-builder/references/choosing.md +101 -0
  261. package/host-builder/skills/prism-host-builder/references/layout.md +52 -0
  262. package/host-builder/skills/prism-host-builder/references/pitfalls.md +50 -0
  263. package/host-builder/skills/prism-host-builder/references/recipes.md +129 -0
  264. package/host-builder/skills/prism-host-builder/references/security.md +52 -0
  265. package/host-builder/skills/prism-host-builder/references/upgrading.md +67 -0
  266. package/host-builder/skills/prism-host-builder/references/verification.md +108 -0
  267. package/host-builder/src/check/child.ts +108 -0
  268. package/host-builder/src/check/conformance.ts +13 -0
  269. package/host-builder/src/check/inspect.ts +20 -0
  270. package/host-builder/src/check/manifest.ts +23 -0
  271. package/host-builder/src/check/runner.ts +181 -0
  272. package/host-builder/src/check/targets.ts +65 -0
  273. package/host-builder/src/errors.ts +35 -0
  274. package/host-builder/src/index.ts +25 -0
  275. package/host-builder/src/indexes.ts +145 -0
  276. package/host-builder/src/install/install.ts +400 -0
  277. package/host-builder/src/install/targets.ts +50 -0
  278. package/host-builder/src/lookup/api.ts +127 -0
  279. package/host-builder/src/lookup/bound.ts +38 -0
  280. package/host-builder/src/lookup/docs.ts +238 -0
  281. package/host-builder/src/lookup/errors.ts +92 -0
  282. package/host-builder/src/lookup/examples.ts +117 -0
  283. package/host-builder/src/lookup/text.ts +60 -0
  284. package/host-builder/src/server.ts +300 -0
  285. package/host-builder/src/version.ts +38 -0
  286. package/mcp-server/README.md +219 -0
  287. package/mcp-server/src/bounds.ts +23 -0
  288. package/mcp-server/src/host.ts +31 -0
  289. package/mcp-server/src/index.ts +27 -0
  290. package/mcp-server/src/serve-tools.ts +408 -0
  291. package/mcp-server/src/serve.ts +329 -0
  292. package/mcp-server/src/server.ts +331 -0
  293. package/mcp-server/src/session-map.ts +47 -0
  294. package/mcp-server/src/skill-resources.ts +85 -0
  295. package/mcp-server/src/types.ts +215 -0
  296. package/package.json +130 -4
  297. package/remote/README.md +244 -0
  298. package/remote/src/attribution.ts +22 -0
  299. package/remote/src/auth.ts +30 -0
  300. package/remote/src/bun-module.d.ts +34 -0
  301. package/remote/src/channel.ts +86 -0
  302. package/remote/src/connect.ts +212 -0
  303. package/remote/src/dispatcher.ts +282 -0
  304. package/remote/src/errors.ts +59 -0
  305. package/remote/src/host-ui.ts +152 -0
  306. package/remote/src/http-gate.ts +43 -0
  307. package/remote/src/index.ts +53 -0
  308. package/remote/src/jsonl.ts +69 -0
  309. package/remote/src/limits.ts +43 -0
  310. package/remote/src/mirror.ts +90 -0
  311. package/remote/src/pending.ts +22 -0
  312. package/remote/src/protocol.ts +107 -0
  313. package/remote/src/serve.ts +560 -0
  314. package/remote/src/session-proxy.ts +219 -0
  315. package/remote/src/sse.ts +38 -0
  316. package/remote/src/stdio-client.ts +41 -0
  317. package/remote/src/testing-fixtures/serve-stdio.ts +32 -0
  318. package/remote/src/testing.ts +230 -0
  319. package/remote/src/transport.ts +139 -0
  320. package/remote/src/transports/client.ts +126 -0
  321. package/remote/src/transports/network.ts +268 -0
  322. package/remote/src/wire.ts +244 -0
@@ -0,0 +1,181 @@
1
+ import { spawn } from "node:child_process";
2
+ import { mkdtempSync, readFileSync, rmSync, statSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { redactSecrets } from "@arnilo/prism";
6
+
7
+ /** The wall-clock limit of one check when the host sets none, in milliseconds. */
8
+ export const DEFAULT_CHECK_TIMEOUT_MS = 30_000;
9
+ /** The most a host may raise the limit to. */
10
+ export const MAX_CHECK_TIMEOUT_MS = 120_000;
11
+ /** The most bytes of the child's result, and of each of its output streams, that the runner keeps. */
12
+ export const MAX_CHECK_OUTPUT_BYTES = 1024 * 1024;
13
+
14
+ const CHILD = join(import.meta.dirname, "child.ts");
15
+ /** The only variables of the server's own environment a check child inherits, besides `--pass-env` names. */
16
+ const INHERITED_ENV = ["PATH", "HOME", "TMPDIR", "PRISM_HOME"] as const;
17
+ /** A value shorter than this is not treated as a secret to mask: it would shred unrelated text. */
18
+ const MIN_SECRET_LENGTH = 6;
19
+
20
+ export type CheckKind = "inspect" | "manifest" | "conformance";
21
+
22
+ export interface CheckFailure {
23
+ /** A stable `ERR_PRISM_*` code when the failure has one. */
24
+ readonly code?: string;
25
+ readonly message: string;
26
+ }
27
+
28
+ /** What a check returns. Nothing else is added: the typed `result` is the check's own. */
29
+ export interface CheckOutcome<T> {
30
+ readonly ok: boolean;
31
+ readonly result?: T;
32
+ readonly error?: CheckFailure;
33
+ /** The child was killed at the time limit. */
34
+ readonly timedOut: boolean;
35
+ /** The result or an output stream was cut at {@linkcode MAX_CHECK_OUTPUT_BYTES}. */
36
+ readonly truncated: boolean;
37
+ }
38
+
39
+ export interface RunCheckOptions {
40
+ readonly kind: CheckKind;
41
+ /** The host project: the child's working directory, and where it resolves `@arnilo/prism` from. */
42
+ readonly root: string;
43
+ /** Absolute path of the file to load for `inspect` or `conformance`. */
44
+ readonly file?: string;
45
+ /** Name of the export to use (`inspect`, `conformance`). */
46
+ readonly exportName?: string;
47
+ /** `manifest` only: let project-scope packages be imported. */
48
+ readonly trustProject?: boolean;
49
+ readonly timeoutMs?: number;
50
+ /** Extra variables the child may see, by name. Their values are also masked in the result. */
51
+ readonly passEnv?: readonly string[];
52
+ /** The environment to filter. Default `process.env`. */
53
+ readonly env?: NodeJS.ProcessEnv;
54
+ }
55
+
56
+ /** The child's environment: the four inherited names that are set, the `--pass-env` names, and the check marker. */
57
+ export function childEnv(passEnv: readonly string[] = [], source: NodeJS.ProcessEnv = process.env): Record<string, string> {
58
+ const env: Record<string, string> = {};
59
+ for (const name of [...INHERITED_ENV, ...passEnv]) {
60
+ const value = source[name];
61
+ if (value !== undefined) env[name] = value;
62
+ }
63
+ env.PRISM_HOST_BUILDER_CHECK = "1";
64
+ return env;
65
+ }
66
+
67
+ function capped(chunks: Buffer[], size: { bytes: number }, chunk: Buffer, state: { truncated: boolean }): void {
68
+ const room = MAX_CHECK_OUTPUT_BYTES - size.bytes;
69
+ if (room <= 0) {
70
+ state.truncated = true;
71
+ return;
72
+ }
73
+ const kept = chunk.byteLength > room ? chunk.subarray(0, room) : chunk;
74
+ if (kept.byteLength < chunk.byteLength) state.truncated = true;
75
+ chunks.push(kept);
76
+ size.bytes += kept.byteLength;
77
+ }
78
+
79
+ function killGroup(pid: number | undefined): void {
80
+ if (pid === undefined) return;
81
+ try {
82
+ // A negative pid is the process group: the child leads one (`detached`), so its own children go with it.
83
+ process.kill(-pid, "SIGKILL");
84
+ } catch {
85
+ // ESRCH: already gone.
86
+ }
87
+ }
88
+
89
+ function tail(text: string, secrets: readonly string[]): string {
90
+ const cut = text.length > 2000 ? `…${text.slice(-2000)}` : text;
91
+ return redactSecrets(cut, secrets).trim();
92
+ }
93
+
94
+ /**
95
+ * Runs one check in a child `bun` process and returns its outcome. The child gets the allowlisted environment
96
+ * of {@linkcode childEnv} and the host project as its working directory; it is killed with its whole process
97
+ * group at the time limit and again after it exits, so nothing it started outlives the call. The result comes
98
+ * back through a file, so host code that prints to stdout cannot corrupt it. Every string in the outcome is
99
+ * masked with the values of the `--pass-env` variables.
100
+ */
101
+ export async function runCheck<T>(options: RunCheckOptions): Promise<CheckOutcome<T>> {
102
+ const timeoutMs = Math.min(options.timeoutMs ?? DEFAULT_CHECK_TIMEOUT_MS, MAX_CHECK_TIMEOUT_MS);
103
+ const env = childEnv(options.passEnv, options.env);
104
+ const secrets = (options.passEnv ?? [])
105
+ .map((name) => (options.env ?? process.env)[name])
106
+ .filter((value): value is string => value !== undefined && value.length >= MIN_SECRET_LENGTH);
107
+ const work = mkdtempSync(join(tmpdir(), "host-builder-check-"));
108
+ const resultFile = join(work, "result.json");
109
+ const args = [CHILD, options.kind, resultFile];
110
+ if (options.file !== undefined) args.push(options.file, options.exportName ?? "default");
111
+ if (options.trustProject === true) args.push("--trust-project");
112
+
113
+ const out: Buffer[] = [];
114
+ const err: Buffer[] = [];
115
+ const outSize = { bytes: 0 };
116
+ const errSize = { bytes: 0 };
117
+ const state = { truncated: false };
118
+ let timedOut = false;
119
+ try {
120
+ const child = spawn(process.execPath, args, { cwd: options.root, env, detached: true, stdio: ["ignore", "pipe", "pipe"] });
121
+ child.stdout.on("data", (chunk: Buffer) => capped(out, outSize, chunk, state));
122
+ child.stderr.on("data", (chunk: Buffer) => capped(err, errSize, chunk, state));
123
+ const timer = setTimeout(() => {
124
+ timedOut = true;
125
+ killGroup(child.pid);
126
+ }, timeoutMs);
127
+ const code = await new Promise<number | null>((resolve) => {
128
+ child.once("error", () => resolve(null));
129
+ child.once("close", (exit) => resolve(exit));
130
+ });
131
+ clearTimeout(timer);
132
+ killGroup(child.pid);
133
+
134
+ if (timedOut)
135
+ return {
136
+ ok: false,
137
+ error: { message: `The check did not finish within ${timeoutMs / 1000}s and was killed.` },
138
+ timedOut: true,
139
+ truncated: state.truncated,
140
+ };
141
+
142
+ let raw: string | undefined;
143
+ try {
144
+ if (statSync(resultFile).size > MAX_CHECK_OUTPUT_BYTES) {
145
+ return {
146
+ ok: false,
147
+ error: { message: `The check result is larger than ${MAX_CHECK_OUTPUT_BYTES} bytes and was dropped.` },
148
+ timedOut: false,
149
+ truncated: true,
150
+ };
151
+ }
152
+ raw = readFileSync(resultFile, "utf8");
153
+ } catch {
154
+ raw = undefined;
155
+ }
156
+ if (raw === undefined) {
157
+ const detail = tail(Buffer.concat(err).toString("utf8") || Buffer.concat(out).toString("utf8"), secrets);
158
+ return {
159
+ ok: false,
160
+ error: {
161
+ message: `The check exited with ${code === null ? "an error" : `code ${code}`} before it reported a result.${detail ? ` ${detail}` : ""}`,
162
+ },
163
+ timedOut: false,
164
+ truncated: state.truncated,
165
+ };
166
+ }
167
+ const parsed = JSON.parse(raw) as { ok: boolean; value?: T; code?: string; message?: string };
168
+ const outcome: CheckOutcome<T> = parsed.ok
169
+ ? { ok: true, result: parsed.value as T, timedOut: false, truncated: state.truncated }
170
+ : {
171
+ ok: false,
172
+ error: { ...(parsed.code === undefined ? {} : { code: parsed.code }), message: parsed.message ?? "The check failed." },
173
+ ...(parsed.value === undefined ? {} : { result: parsed.value }),
174
+ timedOut: false,
175
+ truncated: state.truncated,
176
+ };
177
+ return redactSecrets(outcome, secrets);
178
+ } finally {
179
+ rmSync(work, { recursive: true, force: true });
180
+ }
181
+ }
@@ -0,0 +1,65 @@
1
+ import { realpathSync } from "node:fs";
2
+ import { isAbsolute, relative, resolve, sep } from "node:path";
3
+ import { HostBuilderError } from "../errors.js";
4
+
5
+ /** One configured check target: a file inside the host project and the name of the export to use. */
6
+ export interface CheckTarget {
7
+ /** The name a tool call passes: the path relative to the project root, `#`, the export name. */
8
+ readonly name: string;
9
+ /** Real path of the file. */
10
+ readonly file: string;
11
+ readonly exportName: string;
12
+ }
13
+
14
+ export interface CheckTargets {
15
+ readonly entries: readonly CheckTarget[];
16
+ readonly packages: readonly CheckTarget[];
17
+ }
18
+
19
+ function entryError(message: string): HostBuilderError {
20
+ return new HostBuilderError("ERR_PRISM_HOST_BUILDER_ENTRY", message);
21
+ }
22
+
23
+ /** `file#export` resolved under `root`; refuses a path that does not exist or whose real path leaves the real root. */
24
+ export function parseTarget(root: string, spec: string, flag: "--entry" | "--package"): CheckTarget {
25
+ const hash = spec.lastIndexOf("#");
26
+ const path = hash < 0 ? spec : spec.slice(0, hash);
27
+ const exportName = hash < 0 ? "default" : spec.slice(hash + 1);
28
+ if (path.length === 0 || exportName.length === 0) throw entryError(`${flag} "${spec}" must be <file>#<export>.`);
29
+ let realRoot: string;
30
+ let file: string;
31
+ try {
32
+ realRoot = realpathSync(root);
33
+ } catch {
34
+ throw entryError(`${flag} "${spec}": the project root ${root} does not exist.`);
35
+ }
36
+ try {
37
+ file = realpathSync(isAbsolute(path) ? path : resolve(root, path));
38
+ } catch {
39
+ throw entryError(`${flag} "${spec}": ${path} does not exist under ${root}.`);
40
+ }
41
+ const inside = relative(realRoot, file);
42
+ if (inside === "" || inside.startsWith(`..${sep}`) || inside === ".." || isAbsolute(inside))
43
+ throw entryError(`${flag} "${spec}": ${path} resolves outside the project root ${realRoot}.`);
44
+ return { name: `${inside.split(sep).join("/")}#${exportName}`, file, exportName };
45
+ }
46
+
47
+ /** Parses every start flag at once so a bad one refuses the start, not the first tool call. */
48
+ export function resolveCheckTargets(root: string, entries: readonly string[] = [], packages: readonly string[] = []): CheckTargets {
49
+ return {
50
+ entries: entries.map((spec) => parseTarget(root, spec, "--entry")),
51
+ packages: packages.map((spec) => parseTarget(root, spec, "--package")),
52
+ };
53
+ }
54
+
55
+ /** The target a tool call named: by the configured text or its normalized name. `undefined` when none matches. */
56
+ export function findTarget(root: string, targets: readonly CheckTarget[], wanted: string): CheckTarget | undefined {
57
+ const exact = targets.find((target) => target.name === wanted);
58
+ if (exact) return exact;
59
+ try {
60
+ const normalized = parseTarget(root, wanted, "--entry").name;
61
+ return targets.find((target) => target.name === normalized);
62
+ } catch {
63
+ return undefined;
64
+ }
65
+ }
@@ -0,0 +1,35 @@
1
+ /** An index that is missing, unreadable, or written for another Prism version. */
2
+ const INDEX = "ERR_PRISM_HOST_BUILDER_INDEX";
3
+ /** An example path that is not in the shipped examples index. */
4
+ const EXAMPLE = "ERR_PRISM_HOST_BUILDER_EXAMPLE";
5
+ /** A project whose installed `@arnilo/prism` is not the version the indexes describe, or has none installed. */
6
+ const VERSION = "ERR_PRISM_HOST_BUILDER_VERSION";
7
+ /** A check target (`--entry`, `--package`) that is malformed, missing, outside `--root`, or whose export is not what the check needs. */
8
+ const ENTRY = "ERR_PRISM_HOST_BUILDER_ENTRY";
9
+ /** An inspection factory that ran a session or made an outbound request, which a check refuses. */
10
+ const CHECK_MODEL_CALL = "ERR_PRISM_HOST_BUILDER_CHECK_MODEL_CALL";
11
+ /** An install that would overwrite a skill copy or an MCP entry that is not an unmodified earlier install; `--force` overrides. */
12
+ const INSTALL_CONFLICT = "ERR_PRISM_HOST_BUILDER_INSTALL_CONFLICT";
13
+ /** An MCP configuration file the installer cannot merge safely: malformed JSON, or a server key that is not an object. It is never rewritten. */
14
+ const INSTALL_CONFIG = "ERR_PRISM_HOST_BUILDER_INSTALL_CONFIG";
15
+ /** An install whose root is not a directory, or whose destination resolves outside `--root`. */
16
+ const INSTALL_ROOT = "ERR_PRISM_HOST_BUILDER_INSTALL_ROOT";
17
+
18
+ /** A host-builder failure with a stable code. */
19
+ export class HostBuilderError extends Error {
20
+ readonly code:
21
+ | typeof INDEX
22
+ | typeof EXAMPLE
23
+ | typeof VERSION
24
+ | typeof ENTRY
25
+ | typeof CHECK_MODEL_CALL
26
+ | typeof INSTALL_CONFLICT
27
+ | typeof INSTALL_CONFIG
28
+ | typeof INSTALL_ROOT;
29
+
30
+ constructor(code: HostBuilderError["code"], message: string) {
31
+ super(message);
32
+ this.name = "HostBuilderError";
33
+ this.code = code;
34
+ }
35
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * `@arnilo/prism-hosts/host-builder`: version-exact reference indexes for building a Prism host.
3
+ *
4
+ * The indexes in `data/` are generated at release time from the same declaration model the compat gate uses
5
+ * (`bun scripts/host-builder-index.mjs`), so they describe exactly the `@arnilo/prism` version this package
6
+ * ships with. They load lazily, on the first call that needs them, and never at import time.
7
+ */
8
+
9
+ export { HostBuilderError } from "./errors.js";
10
+ export {
11
+ type ApiEntry,
12
+ type ApiExport,
13
+ type ApiIndex,
14
+ type ApiPackage,
15
+ type ErrorCodeRecord,
16
+ type ErrorsIndex,
17
+ type ExampleRecord,
18
+ type ExamplesIndex,
19
+ type HostBuilderIndexes,
20
+ type HostBuilderIndexHeader,
21
+ type HostBuilderIndexName,
22
+ type LoadHostBuilderIndexOptions,
23
+ loadHostBuilderIndex,
24
+ readExampleSource,
25
+ } from "./indexes.js";
@@ -0,0 +1,145 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { isAbsolute, join, normalize, sep } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { HostBuilderError } from "./errors.js";
5
+
6
+ export type HostBuilderIndexName = "api" | "errors" | "examples";
7
+
8
+ /** Written by `scripts/host-builder-index.mjs` at the top of every index. */
9
+ export interface HostBuilderIndexHeader {
10
+ readonly schemaVersion: 1;
11
+ /** The `@arnilo/prism` version the index describes. */
12
+ readonly prismVersion: string;
13
+ /** SHA-256 of the index body. */
14
+ readonly contentHash: string;
15
+ }
16
+
17
+ export interface ApiExport {
18
+ readonly name: string;
19
+ readonly kind: "function" | "class" | "const" | "let" | "var" | "enum" | "interface" | "type" | "namespace" | "reexport";
20
+ readonly signature: string;
21
+ /** For an interface or class: its members, one declaration per string, comments removed. */
22
+ readonly members?: readonly string[];
23
+ readonly summary?: string;
24
+ /** Project-relative paths into the installed tarballs (`node_modules/@arnilo/<pkg>/...`) that mention the name in backticks. */
25
+ readonly docs?: readonly string[];
26
+ /** Paths of CI-tested examples that import the name. */
27
+ readonly examples?: readonly string[];
28
+ }
29
+
30
+ export interface ApiEntry {
31
+ /** The `exports` key: `.` or a subpath such as `./testing`. */
32
+ readonly subpath: string;
33
+ /** The import specifier, for example `@arnilo/prism/testing`. */
34
+ readonly specifier: string;
35
+ readonly exports: readonly ApiExport[];
36
+ }
37
+
38
+ export interface ApiPackage {
39
+ readonly name: string;
40
+ readonly version: string;
41
+ readonly path: string;
42
+ readonly experimental: boolean;
43
+ readonly entries: readonly ApiEntry[];
44
+ }
45
+
46
+ export interface ApiIndex extends HostBuilderIndexHeader {
47
+ readonly packages: readonly ApiPackage[];
48
+ }
49
+
50
+ export interface ErrorCodeRecord {
51
+ readonly code: string;
52
+ readonly namespace: "ERR_PRISM" | "short";
53
+ readonly package: string;
54
+ readonly file: string;
55
+ readonly line: number;
56
+ readonly type?: string;
57
+ readonly errorClass: string | null;
58
+ /** The doc comment at the declaration, or null. */
59
+ readonly meaning: string | null;
60
+ /** The message at the first throw site, or null. */
61
+ readonly sampleMessage: string | null;
62
+ readonly docs: readonly string[];
63
+ }
64
+
65
+ export interface ErrorsIndex extends HostBuilderIndexHeader {
66
+ readonly codes: readonly ErrorCodeRecord[];
67
+ }
68
+
69
+ export interface ExampleRecord {
70
+ readonly path: string;
71
+ readonly title: string;
72
+ /** `executed`, or `skip:<reason>` for an example the execution gate skips. */
73
+ readonly gate: string;
74
+ readonly imports: readonly string[];
75
+ readonly requiresEnv: readonly string[];
76
+ readonly needsCredentials: boolean;
77
+ }
78
+
79
+ export interface ExamplesIndex extends HostBuilderIndexHeader {
80
+ readonly examples: readonly ExampleRecord[];
81
+ /** Files an example imports or reads (helpers, adapters, sidecar sources). */
82
+ readonly support: readonly string[];
83
+ }
84
+
85
+ export interface HostBuilderIndexes {
86
+ readonly api: ApiIndex;
87
+ readonly errors: ErrorsIndex;
88
+ readonly examples: ExamplesIndex;
89
+ }
90
+
91
+ export interface LoadHostBuilderIndexOptions {
92
+ /** Directory holding `api.json`, `errors.json`, `examples.json` and `examples/`. Defaults to this package's `data/`. */
93
+ readonly dataDir?: string;
94
+ }
95
+
96
+ const DEFAULT_DATA_DIR = new URL("../data/", import.meta.url);
97
+ const cache = new Map<string, unknown>();
98
+
99
+ function dataDirOf(options: LoadHostBuilderIndexOptions | undefined): string {
100
+ return options?.dataDir ?? fileURLToPath(DEFAULT_DATA_DIR);
101
+ }
102
+
103
+ /**
104
+ * Loads one index. The file is read and parsed on the first call for a directory and cached after that.
105
+ * Fails with `ERR_PRISM_HOST_BUILDER_INDEX` when the file is missing, is not JSON, or lacks the header.
106
+ */
107
+ export async function loadHostBuilderIndex<N extends HostBuilderIndexName>(
108
+ name: N,
109
+ options?: LoadHostBuilderIndexOptions,
110
+ ): Promise<HostBuilderIndexes[N]> {
111
+ const dir = dataDirOf(options);
112
+ const file = join(dir, `${name}.json`);
113
+ const cached = cache.get(file);
114
+ if (cached) return cached as HostBuilderIndexes[N];
115
+ let parsed: unknown;
116
+ try {
117
+ parsed = JSON.parse(readFileSync(file, "utf8"));
118
+ } catch (error) {
119
+ throw new HostBuilderError(
120
+ "ERR_PRISM_HOST_BUILDER_INDEX",
121
+ `host-builder index "${name}" cannot be read at ${file}: ${error instanceof Error ? error.message : String(error)}. Reinstall @arnilo/prism-hosts.`,
122
+ );
123
+ }
124
+ const header = parsed as Partial<HostBuilderIndexHeader> | null;
125
+ if (header?.schemaVersion !== 1 || typeof header.prismVersion !== "string" || typeof header.contentHash !== "string") {
126
+ throw new HostBuilderError("ERR_PRISM_HOST_BUILDER_INDEX", `host-builder index "${name}" has no schemaVersion 1 header.`);
127
+ }
128
+ cache.set(file, parsed);
129
+ return parsed as HostBuilderIndexes[N];
130
+ }
131
+
132
+ /**
133
+ * The verbatim source of an indexed example, by its repository path (`examples/define-package.ts`).
134
+ * Only paths the examples index lists (an example or a support file) are served, so a traversal path or a
135
+ * file that is not shipped fails with `ERR_PRISM_HOST_BUILDER_EXAMPLE`.
136
+ */
137
+ export async function readExampleSource(path: string, options?: LoadHostBuilderIndexOptions): Promise<string> {
138
+ const index = await loadHostBuilderIndex("examples", options);
139
+ const listed = new Set<string>([...index.examples.map((example) => example.path), ...index.support]);
140
+ const clean = normalize(path);
141
+ if (isAbsolute(path) || clean.split(sep).includes("..") || !listed.has(path)) {
142
+ throw new HostBuilderError("ERR_PRISM_HOST_BUILDER_EXAMPLE", `"${path}" is not an indexed example.`);
143
+ }
144
+ return readFileSync(join(dataDirOf(options), path), "utf8");
145
+ }