@theoremjs/agents 0.0.0-stage → 0.3.0

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 (514) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1264 -2
  3. package/esm/_dnt.polyfills.d.ts +66 -0
  4. package/esm/_dnt.polyfills.js +51 -0
  5. package/esm/mod.d.ts +65 -0
  6. package/esm/mod.js +42 -0
  7. package/esm/package.json +3 -0
  8. package/esm/src/cli/bin.d.ts +7 -0
  9. package/esm/src/cli/bin.js +9 -0
  10. package/esm/src/cli/commands/bench.d.ts +6 -0
  11. package/esm/src/cli/commands/bench.js +458 -0
  12. package/esm/src/cli/commands/egress-compile.d.ts +17 -0
  13. package/esm/src/cli/commands/egress-compile.js +25 -0
  14. package/esm/src/cli/commands/eval.d.ts +37 -0
  15. package/esm/src/cli/commands/eval.js +190 -0
  16. package/esm/src/cli/commands/fuzz-canary.d.ts +15 -0
  17. package/esm/src/cli/commands/fuzz-canary.js +202 -0
  18. package/esm/src/cli/commands/fuzz-guardrails.d.ts +2 -0
  19. package/esm/src/cli/commands/fuzz-guardrails.js +5 -0
  20. package/esm/src/cli/commands/profile.d.ts +2 -0
  21. package/esm/src/cli/commands/profile.js +80 -0
  22. package/esm/src/cli/commands/run.d.ts +13 -0
  23. package/esm/src/cli/commands/run.js +45 -0
  24. package/esm/src/cli/commands/test.d.ts +25 -0
  25. package/esm/src/cli/commands/test.js +143 -0
  26. package/esm/src/cli/event-log.d.ts +15 -0
  27. package/esm/src/cli/event-log.js +136 -0
  28. package/esm/src/cli/index.d.ts +10 -0
  29. package/esm/src/cli/index.js +267 -0
  30. package/esm/src/cli/matrix/fixtures.d.ts +10 -0
  31. package/esm/src/cli/matrix/fixtures.js +72 -0
  32. package/esm/src/cli/matrix/synthesizer.d.ts +18 -0
  33. package/esm/src/cli/matrix/synthesizer.js +103 -0
  34. package/esm/src/evals/attachments.d.ts +4 -0
  35. package/esm/src/evals/attachments.js +53 -0
  36. package/esm/src/evals/breakdown.d.ts +28 -0
  37. package/esm/src/evals/breakdown.js +68 -0
  38. package/esm/src/evals/graders/answer.d.ts +13 -0
  39. package/esm/src/evals/graders/answer.js +133 -0
  40. package/esm/src/evals/graders/code.d.ts +64 -0
  41. package/esm/src/evals/graders/code.js +205 -0
  42. package/esm/src/evals/graders/judge.d.ts +20 -0
  43. package/esm/src/evals/graders/judge.js +365 -0
  44. package/esm/src/evals/graders/latency.d.ts +5 -0
  45. package/esm/src/evals/graders/latency.js +81 -0
  46. package/esm/src/evals/graders/live.d.ts +11 -0
  47. package/esm/src/evals/graders/live.js +40 -0
  48. package/esm/src/evals/graders/media.d.ts +18 -0
  49. package/esm/src/evals/graders/media.js +92 -0
  50. package/esm/src/evals/graders/shared.d.ts +11 -0
  51. package/esm/src/evals/graders/shared.js +62 -0
  52. package/esm/src/evals/graders/transcript.d.ts +17 -0
  53. package/esm/src/evals/graders/transcript.js +200 -0
  54. package/esm/src/evals/mod.d.ts +42 -0
  55. package/esm/src/evals/mod.js +30 -0
  56. package/esm/src/evals/record.d.ts +35 -0
  57. package/esm/src/evals/record.js +105 -0
  58. package/esm/src/evals/rubrics/catalog.d.ts +148 -0
  59. package/esm/src/evals/rubrics/catalog.js +846 -0
  60. package/esm/src/evals/rubrics/mod.d.ts +24 -0
  61. package/esm/src/evals/rubrics/mod.js +55 -0
  62. package/esm/src/evals/rubrics/mustache.d.ts +23 -0
  63. package/esm/src/evals/rubrics/mustache.js +154 -0
  64. package/esm/src/evals/rubrics/types.d.ts +54 -0
  65. package/esm/src/evals/rubrics/types.js +74 -0
  66. package/esm/src/evals/run.d.ts +118 -0
  67. package/esm/src/evals/run.js +452 -0
  68. package/esm/src/evals/suite.d.ts +19 -0
  69. package/esm/src/evals/suite.js +92 -0
  70. package/esm/src/evals/summary.d.ts +32 -0
  71. package/esm/src/evals/summary.js +30 -0
  72. package/esm/src/evals/trial.d.ts +27 -0
  73. package/esm/src/evals/trial.js +158 -0
  74. package/esm/src/evals/types.d.ts +260 -0
  75. package/esm/src/evals/types.js +126 -0
  76. package/esm/src/evals/verdict.d.ts +18 -0
  77. package/esm/src/evals/verdict.js +45 -0
  78. package/esm/src/guardrails/canary-gate.d.ts +21 -0
  79. package/esm/src/guardrails/canary-gate.js +40 -0
  80. package/esm/src/guardrails/canary.d.ts +112 -0
  81. package/esm/src/guardrails/canary.js +901 -0
  82. package/esm/src/guardrails/compile-egress.d.ts +11 -0
  83. package/esm/src/guardrails/compile-egress.js +11 -0
  84. package/esm/src/guardrails/corpus/canary-egress-attacks.d.ts +22 -0
  85. package/esm/src/guardrails/corpus/canary-egress-attacks.js +303 -0
  86. package/esm/src/guardrails/corpus/fuzz-inbound.d.ts +5 -0
  87. package/esm/src/guardrails/corpus/fuzz-inbound.js +202 -0
  88. package/esm/src/guardrails/corpus/inbound-payloads.d.ts +3 -0
  89. package/esm/src/guardrails/corpus/inbound-payloads.js +118 -0
  90. package/esm/src/guardrails/corpus/live-attacks.d.ts +13 -0
  91. package/esm/src/guardrails/corpus/live-attacks.js +224 -0
  92. package/esm/src/guardrails/corpus/mod.d.ts +7 -0
  93. package/esm/src/guardrails/corpus/mod.js +4 -0
  94. package/esm/src/guardrails/corpus/secrets.d.ts +12 -0
  95. package/esm/src/guardrails/corpus/secrets.js +16 -0
  96. package/esm/src/guardrails/corpus/strings.d.ts +23 -0
  97. package/esm/src/guardrails/corpus/strings.js +29 -0
  98. package/esm/src/guardrails/corpus/types.d.ts +32 -0
  99. package/esm/src/guardrails/corpus/types.js +1 -0
  100. package/esm/src/guardrails/egress-automata.d.ts +14 -0
  101. package/esm/src/guardrails/egress-automata.js +6059 -0
  102. package/esm/src/guardrails/egress-compiler.d.ts +33 -0
  103. package/esm/src/guardrails/egress-compiler.js +237 -0
  104. package/esm/src/guardrails/egress-patterns.d.ts +24 -0
  105. package/esm/src/guardrails/egress-patterns.js +32 -0
  106. package/esm/src/guardrails/egress-policy.d.ts +27 -0
  107. package/esm/src/guardrails/egress-policy.js +76 -0
  108. package/esm/src/guardrails/egress-rules.d.ts +54 -0
  109. package/esm/src/guardrails/egress-rules.js +49 -0
  110. package/esm/src/guardrails/egress-stream.d.ts +105 -0
  111. package/esm/src/guardrails/egress-stream.js +645 -0
  112. package/esm/src/guardrails/egress-urls.d.ts +83 -0
  113. package/esm/src/guardrails/egress-urls.js +917 -0
  114. package/esm/src/guardrails/egress.d.ts +106 -0
  115. package/esm/src/guardrails/egress.js +374 -0
  116. package/esm/src/guardrails/error.d.ts +26 -0
  117. package/esm/src/guardrails/error.js +91 -0
  118. package/esm/src/guardrails/event-schemas.d.ts +57 -0
  119. package/esm/src/guardrails/event-schemas.js +41 -0
  120. package/esm/src/guardrails/events.d.ts +9 -0
  121. package/esm/src/guardrails/events.js +42 -0
  122. package/esm/src/guardrails/hits.d.ts +8 -0
  123. package/esm/src/guardrails/hits.js +19 -0
  124. package/esm/src/guardrails/injection-patterns.d.ts +15 -0
  125. package/esm/src/guardrails/injection-patterns.js +94 -0
  126. package/esm/src/guardrails/injection.d.ts +14 -0
  127. package/esm/src/guardrails/injection.js +237 -0
  128. package/esm/src/guardrails/lexicon.d.ts +30 -0
  129. package/esm/src/guardrails/lexicon.js +478 -0
  130. package/esm/src/guardrails/live-outbound-gate.d.ts +54 -0
  131. package/esm/src/guardrails/live-outbound-gate.js +305 -0
  132. package/esm/src/guardrails/mod.d.ts +46 -0
  133. package/esm/src/guardrails/mod.js +29 -0
  134. package/esm/src/guardrails/network.d.ts +42 -0
  135. package/esm/src/guardrails/network.js +328 -0
  136. package/esm/src/guardrails/normalize.d.ts +6 -0
  137. package/esm/src/guardrails/normalize.js +132 -0
  138. package/esm/src/guardrails/policy.d.ts +19 -0
  139. package/esm/src/guardrails/policy.js +50 -0
  140. package/esm/src/guardrails/progressive-yield.d.ts +69 -0
  141. package/esm/src/guardrails/progressive-yield.js +237 -0
  142. package/esm/src/guardrails/prompt-echo.d.ts +38 -0
  143. package/esm/src/guardrails/prompt-echo.js +225 -0
  144. package/esm/src/guardrails/quota.d.ts +20 -0
  145. package/esm/src/guardrails/quota.js +80 -0
  146. package/esm/src/guardrails/rules.d.ts +56 -0
  147. package/esm/src/guardrails/rules.js +54 -0
  148. package/esm/src/guardrails/sanitize.d.ts +23 -0
  149. package/esm/src/guardrails/sanitize.js +184 -0
  150. package/esm/src/guardrails/sensitive.d.ts +29 -0
  151. package/esm/src/guardrails/sensitive.js +113 -0
  152. package/esm/src/guardrails/serialize.d.ts +14 -0
  153. package/esm/src/guardrails/serialize.js +37 -0
  154. package/esm/src/guardrails/testing.d.ts +9 -0
  155. package/esm/src/guardrails/testing.js +8 -0
  156. package/esm/src/guardrails/theorem-error.d.ts +18 -0
  157. package/esm/src/guardrails/theorem-error.js +53 -0
  158. package/esm/src/guardrails/thought-guard.d.ts +55 -0
  159. package/esm/src/guardrails/thought-guard.js +464 -0
  160. package/esm/src/guardrails/tool-directives.d.ts +19 -0
  161. package/esm/src/guardrails/tool-directives.js +109 -0
  162. package/esm/src/guardrails/tool-result.d.ts +61 -0
  163. package/esm/src/guardrails/tool-result.js +248 -0
  164. package/esm/src/guardrails/types.d.ts +242 -0
  165. package/esm/src/guardrails/types.js +54 -0
  166. package/esm/src/host/client-turn.d.ts +9 -0
  167. package/esm/src/host/client-turn.js +38 -0
  168. package/esm/src/host/mint-trace.d.ts +28 -0
  169. package/esm/src/host/mint-trace.js +59 -0
  170. package/esm/src/host/mod.d.ts +13 -0
  171. package/esm/src/host/mod.js +11 -0
  172. package/esm/src/host/readStreamingJsonStringField.d.ts +2 -0
  173. package/esm/src/host/readStreamingJsonStringField.js +68 -0
  174. package/esm/src/host/reply.d.ts +7 -0
  175. package/esm/src/host/reply.js +35 -0
  176. package/esm/src/interface/blocks.d.ts +8 -0
  177. package/esm/src/interface/blocks.js +213 -0
  178. package/esm/src/interface/composer-actions.d.ts +20 -0
  179. package/esm/src/interface/composer-actions.js +31 -0
  180. package/esm/src/interface/draft.d.ts +14 -0
  181. package/esm/src/interface/draft.js +28 -0
  182. package/esm/src/interface/from-profile.d.ts +12 -0
  183. package/esm/src/interface/from-profile.js +144 -0
  184. package/esm/src/interface/history.d.ts +40 -0
  185. package/esm/src/interface/history.js +269 -0
  186. package/esm/src/interface/inputs.d.ts +10 -0
  187. package/esm/src/interface/inputs.js +64 -0
  188. package/esm/src/interface/mod.d.ts +31 -0
  189. package/esm/src/interface/mod.js +21 -0
  190. package/esm/src/interface/models.d.ts +19 -0
  191. package/esm/src/interface/models.js +45 -0
  192. package/esm/src/interface/pending.d.ts +48 -0
  193. package/esm/src/interface/pending.js +146 -0
  194. package/esm/src/interface/profile-interface.d.ts +4 -0
  195. package/esm/src/interface/profile-interface.js +223 -0
  196. package/esm/src/interface/session.d.ts +64 -0
  197. package/esm/src/interface/session.js +121 -0
  198. package/esm/src/interface/tool-calls.d.ts +20 -0
  199. package/esm/src/interface/tool-calls.js +73 -0
  200. package/esm/src/interface/tool-invoke.d.ts +4 -0
  201. package/esm/src/interface/tool-invoke.js +20 -0
  202. package/esm/src/interface/tool-media.d.ts +9 -0
  203. package/esm/src/interface/tool-media.js +112 -0
  204. package/esm/src/interface/types.d.ts +213 -0
  205. package/esm/src/interface/types.js +4 -0
  206. package/esm/src/kernel/auth/credential-source.d.ts +23 -0
  207. package/esm/src/kernel/auth/credential-source.js +11 -0
  208. package/esm/src/kernel/auth/crypto.d.ts +51 -0
  209. package/esm/src/kernel/auth/crypto.js +161 -0
  210. package/esm/src/kernel/auth/mod.d.ts +5 -0
  211. package/esm/src/kernel/auth/mod.js +5 -0
  212. package/esm/src/kernel/auth/oauth.d.ts +17 -0
  213. package/esm/src/kernel/auth/oauth.js +438 -0
  214. package/esm/src/kernel/auth/scope-refusal.d.ts +9 -0
  215. package/esm/src/kernel/auth/scope-refusal.js +8 -0
  216. package/esm/src/kernel/auth/typed-secret.d.ts +13 -0
  217. package/esm/src/kernel/auth/typed-secret.js +23 -0
  218. package/esm/src/kernel/auth/types.d.ts +139 -0
  219. package/esm/src/kernel/auth/types.js +1 -0
  220. package/esm/src/kernel/default-scope.d.ts +34 -0
  221. package/esm/src/kernel/default-scope.js +72 -0
  222. package/esm/src/kernel/engine/compaction.d.ts +70 -0
  223. package/esm/src/kernel/engine/compaction.js +202 -0
  224. package/esm/src/kernel/engine/decision.d.ts +41 -0
  225. package/esm/src/kernel/engine/decision.js +412 -0
  226. package/esm/src/kernel/engine/hash.d.ts +6 -0
  227. package/esm/src/kernel/engine/hash.js +22 -0
  228. package/esm/src/kernel/engine/live-inbound.d.ts +9 -0
  229. package/esm/src/kernel/engine/live-inbound.js +19 -0
  230. package/esm/src/kernel/engine/live-ingress.d.ts +9 -0
  231. package/esm/src/kernel/engine/live-ingress.js +37 -0
  232. package/esm/src/kernel/engine/media-probe/audio.d.ts +7 -0
  233. package/esm/src/kernel/engine/media-probe/audio.js +342 -0
  234. package/esm/src/kernel/engine/media-probe/bytes.d.ts +6 -0
  235. package/esm/src/kernel/engine/media-probe/bytes.js +26 -0
  236. package/esm/src/kernel/engine/media-probe/image.d.ts +5 -0
  237. package/esm/src/kernel/engine/media-probe/image.js +171 -0
  238. package/esm/src/kernel/engine/media-probe/iso-bmff.d.ts +19 -0
  239. package/esm/src/kernel/engine/media-probe/iso-bmff.js +48 -0
  240. package/esm/src/kernel/engine/media-probe/matroska.d.ts +35 -0
  241. package/esm/src/kernel/engine/media-probe/matroska.js +226 -0
  242. package/esm/src/kernel/engine/media-probe/mp4.d.ts +22 -0
  243. package/esm/src/kernel/engine/media-probe/mp4.js +279 -0
  244. package/esm/src/kernel/engine/media-probe/opus.d.ts +4 -0
  245. package/esm/src/kernel/engine/media-probe/opus.js +23 -0
  246. package/esm/src/kernel/engine/media-probe/pdf.d.ts +5 -0
  247. package/esm/src/kernel/engine/media-probe/pdf.js +117 -0
  248. package/esm/src/kernel/engine/media-probe/video.d.ts +15 -0
  249. package/esm/src/kernel/engine/media-probe/video.js +49 -0
  250. package/esm/src/kernel/engine/record.d.ts +3 -0
  251. package/esm/src/kernel/engine/record.js +11 -0
  252. package/esm/src/kernel/engine/repair.d.ts +7 -0
  253. package/esm/src/kernel/engine/repair.js +36 -0
  254. package/esm/src/kernel/engine/runner/gates.d.ts +5 -0
  255. package/esm/src/kernel/engine/runner/gates.js +322 -0
  256. package/esm/src/kernel/engine/runner/mod.d.ts +15 -0
  257. package/esm/src/kernel/engine/runner/mod.js +574 -0
  258. package/esm/src/kernel/engine/runner/schema-validation.d.ts +14 -0
  259. package/esm/src/kernel/engine/runner/schema-validation.js +105 -0
  260. package/esm/src/kernel/engine/runner/stages.d.ts +28 -0
  261. package/esm/src/kernel/engine/runner/stages.js +48 -0
  262. package/esm/src/kernel/engine/runner/state.d.ts +76 -0
  263. package/esm/src/kernel/engine/runner/state.js +40 -0
  264. package/esm/src/kernel/engine/runner/steps.d.ts +14 -0
  265. package/esm/src/kernel/engine/runner/steps.js +360 -0
  266. package/esm/src/kernel/engine/runner/stream.d.ts +40 -0
  267. package/esm/src/kernel/engine/runner/stream.js +225 -0
  268. package/esm/src/kernel/engine/runner/usage.d.ts +45 -0
  269. package/esm/src/kernel/engine/runner/usage.js +150 -0
  270. package/esm/src/kernel/engine/session/mod.d.ts +31 -0
  271. package/esm/src/kernel/engine/session/mod.js +951 -0
  272. package/esm/src/kernel/engine/session/session-trace.d.ts +115 -0
  273. package/esm/src/kernel/engine/session/session-trace.js +489 -0
  274. package/esm/src/kernel/engine/token-estimate.d.ts +77 -0
  275. package/esm/src/kernel/engine/token-estimate.js +218 -0
  276. package/esm/src/kernel/engine/tool-trace.d.ts +58 -0
  277. package/esm/src/kernel/engine/tool-trace.js +131 -0
  278. package/esm/src/kernel/engine/tree.d.ts +2 -0
  279. package/esm/src/kernel/engine/tree.js +17 -0
  280. package/esm/src/kernel/engine/turn-trace.d.ts +118 -0
  281. package/esm/src/kernel/engine/turn-trace.js +863 -0
  282. package/esm/src/kernel/engine/usage.d.ts +30 -0
  283. package/esm/src/kernel/engine/usage.js +134 -0
  284. package/esm/src/kernel/interaction-parts.d.ts +10 -0
  285. package/esm/src/kernel/interaction-parts.js +22 -0
  286. package/esm/src/kernel/mod.d.ts +47 -0
  287. package/esm/src/kernel/mod.js +30 -0
  288. package/esm/src/kernel/profile-graph.d.ts +140 -0
  289. package/esm/src/kernel/profile-graph.js +151 -0
  290. package/esm/src/kernel/profile-presence.d.ts +9 -0
  291. package/esm/src/kernel/profile-presence.js +143 -0
  292. package/esm/src/kernel/profile-scope.d.ts +36 -0
  293. package/esm/src/kernel/profile-scope.js +262 -0
  294. package/esm/src/kernel/registry/attachments.d.ts +35 -0
  295. package/esm/src/kernel/registry/attachments.js +211 -0
  296. package/esm/src/kernel/registry/catalog.d.ts +19 -0
  297. package/esm/src/kernel/registry/catalog.js +78 -0
  298. package/esm/src/kernel/registry/ingress.d.ts +8 -0
  299. package/esm/src/kernel/registry/ingress.js +134 -0
  300. package/esm/src/kernel/registry/kernel-registry.d.ts +12 -0
  301. package/esm/src/kernel/registry/kernel-registry.js +8 -0
  302. package/esm/src/kernel/registry/profile-outputs.d.ts +3 -0
  303. package/esm/src/kernel/registry/profile-outputs.js +7 -0
  304. package/esm/src/kernel/registry/profiles.d.ts +92 -0
  305. package/esm/src/kernel/registry/profiles.js +698 -0
  306. package/esm/src/kernel/registry/provider-request.d.ts +6 -0
  307. package/esm/src/kernel/registry/provider-request.js +42 -0
  308. package/esm/src/kernel/registry/resolve.d.ts +14 -0
  309. package/esm/src/kernel/registry/resolve.js +237 -0
  310. package/esm/src/kernel/registry/schemas.d.ts +9 -0
  311. package/esm/src/kernel/registry/schemas.js +25 -0
  312. package/esm/src/kernel/registry/sole-model.d.ts +2 -0
  313. package/esm/src/kernel/registry/sole-model.js +4 -0
  314. package/esm/src/kernel/registry/system-prompt.d.ts +8 -0
  315. package/esm/src/kernel/registry/system-prompt.js +33 -0
  316. package/esm/src/kernel/registry/system-role.d.ts +3 -0
  317. package/esm/src/kernel/registry/system-role.js +9 -0
  318. package/esm/src/kernel/registry/vault.d.ts +13 -0
  319. package/esm/src/kernel/registry/vault.js +14 -0
  320. package/esm/src/kernel/schema.d.ts +172 -0
  321. package/esm/src/kernel/schema.js +653 -0
  322. package/esm/src/kernel/scope.d.ts +22 -0
  323. package/esm/src/kernel/scope.js +25 -0
  324. package/esm/src/kernel/stages.d.ts +141 -0
  325. package/esm/src/kernel/stages.js +388 -0
  326. package/esm/src/kernel/stop.d.ts +93 -0
  327. package/esm/src/kernel/stop.js +125 -0
  328. package/esm/src/kernel/system-parts.d.ts +26 -0
  329. package/esm/src/kernel/system-parts.js +98 -0
  330. package/esm/src/kernel/tools/activity-label.d.ts +16 -0
  331. package/esm/src/kernel/tools/activity-label.js +209 -0
  332. package/esm/src/kernel/tools/events.d.ts +51 -0
  333. package/esm/src/kernel/tools/events.js +149 -0
  334. package/esm/src/kernel/tools/execute.d.ts +64 -0
  335. package/esm/src/kernel/tools/execute.js +801 -0
  336. package/esm/src/kernel/tools/gate-answer.d.ts +50 -0
  337. package/esm/src/kernel/tools/gate-answer.js +54 -0
  338. package/esm/src/kernel/tools/harness.d.ts +16 -0
  339. package/esm/src/kernel/tools/harness.js +42 -0
  340. package/esm/src/kernel/tools/invoke.d.ts +10 -0
  341. package/esm/src/kernel/tools/invoke.js +129 -0
  342. package/esm/src/kernel/tools/mcp-sessions.d.ts +20 -0
  343. package/esm/src/kernel/tools/mcp-sessions.js +64 -0
  344. package/esm/src/kernel/tools/mod.d.ts +9 -0
  345. package/esm/src/kernel/tools/mod.js +6 -0
  346. package/esm/src/kernel/tools/model-text.d.ts +11 -0
  347. package/esm/src/kernel/tools/model-text.js +25 -0
  348. package/esm/src/kernel/tools/permission.d.ts +6 -0
  349. package/esm/src/kernel/tools/permission.js +38 -0
  350. package/esm/src/kernel/tools/project.d.ts +8 -0
  351. package/esm/src/kernel/tools/project.js +25 -0
  352. package/esm/src/kernel/tools/registry.d.ts +20 -0
  353. package/esm/src/kernel/tools/registry.js +84 -0
  354. package/esm/src/kernel/tools/remote.d.ts +110 -0
  355. package/esm/src/kernel/tools/remote.js +998 -0
  356. package/esm/src/kernel/tools/resolve.d.ts +28 -0
  357. package/esm/src/kernel/tools/resolve.js +259 -0
  358. package/esm/src/kernel/tools/schema.d.ts +10 -0
  359. package/esm/src/kernel/tools/schema.js +174 -0
  360. package/esm/src/kernel/tools/signed-in-fetch.d.ts +18 -0
  361. package/esm/src/kernel/tools/signed-in-fetch.js +45 -0
  362. package/esm/src/kernel/tools/stage-run.d.ts +93 -0
  363. package/esm/src/kernel/tools/stage-run.js +140 -0
  364. package/esm/src/kernel/tools/types.d.ts +287 -0
  365. package/esm/src/kernel/tools/types.js +1 -0
  366. package/esm/src/kernel/turn-events.d.ts +599 -0
  367. package/esm/src/kernel/turn-events.js +425 -0
  368. package/esm/src/kernel/types.d.ts +884 -0
  369. package/esm/src/kernel/types.js +1 -0
  370. package/esm/src/kernel/util/base64.d.ts +2 -0
  371. package/esm/src/kernel/util/base64.js +13 -0
  372. package/esm/src/kernel/util/exact-type.d.ts +31 -0
  373. package/esm/src/kernel/util/exact-type.js +1 -0
  374. package/esm/src/kernel/util/find-last.d.ts +3 -0
  375. package/esm/src/kernel/util/find-last.js +9 -0
  376. package/esm/src/kernel/util/mime.d.ts +2 -0
  377. package/esm/src/kernel/util/mime.js +4 -0
  378. package/esm/src/kernel/util/record.d.ts +1 -0
  379. package/esm/src/kernel/util/record.js +3 -0
  380. package/esm/src/observability/destinations.d.ts +9 -0
  381. package/esm/src/observability/destinations.js +38 -0
  382. package/esm/src/observability/jsonl.d.ts +18 -0
  383. package/esm/src/observability/jsonl.js +117 -0
  384. package/esm/src/observability/mod.d.ts +22 -0
  385. package/esm/src/observability/mod.js +16 -0
  386. package/esm/src/observability/openinference.d.ts +11 -0
  387. package/esm/src/observability/openinference.js +205 -0
  388. package/esm/src/observability/otlp.d.ts +68 -0
  389. package/esm/src/observability/otlp.js +98 -0
  390. package/esm/src/observability/phoenix.d.ts +29 -0
  391. package/esm/src/observability/phoenix.js +71 -0
  392. package/esm/src/observability/policy.d.ts +14 -0
  393. package/esm/src/observability/policy.js +69 -0
  394. package/esm/src/observability/resolve-policy.d.ts +8 -0
  395. package/esm/src/observability/resolve-policy.js +58 -0
  396. package/esm/src/observability/spans.d.ts +16 -0
  397. package/esm/src/observability/spans.js +56 -0
  398. package/esm/src/observability/trace-catalog.d.ts +111 -0
  399. package/esm/src/observability/trace-catalog.js +865 -0
  400. package/esm/src/observability/trace-record.d.ts +23 -0
  401. package/esm/src/observability/trace-record.js +223 -0
  402. package/esm/src/observability/trace-schema.d.ts +54 -0
  403. package/esm/src/observability/trace-schema.js +59 -0
  404. package/esm/src/observability/trace-sink.d.ts +13 -0
  405. package/esm/src/observability/trace-sink.js +3 -0
  406. package/esm/src/observability/trace-span.d.ts +92 -0
  407. package/esm/src/observability/trace-span.js +199 -0
  408. package/esm/src/observability/trace.d.ts +11 -0
  409. package/esm/src/observability/trace.js +35 -0
  410. package/esm/src/observability/types.d.ts +85 -0
  411. package/esm/src/observability/types.js +3 -0
  412. package/esm/src/presets/google/speech-voices.d.ts +8 -0
  413. package/esm/src/presets/google/speech-voices.js +38 -0
  414. package/esm/src/presets/google-limits.d.ts +12 -0
  415. package/esm/src/presets/google-limits.js +18 -0
  416. package/esm/src/presets/google.d.ts +104 -0
  417. package/esm/src/presets/google.js +188 -0
  418. package/esm/src/presets/mod.d.ts +10 -0
  419. package/esm/src/presets/mod.js +9 -0
  420. package/esm/src/presets/openrouter.d.ts +13 -0
  421. package/esm/src/presets/openrouter.js +13 -0
  422. package/esm/src/presets/typesafe.d.ts +2 -0
  423. package/esm/src/presets/typesafe.js +2 -0
  424. package/esm/src/providers/create-provider.d.ts +21 -0
  425. package/esm/src/providers/create-provider.js +90 -0
  426. package/esm/src/providers/decision/usage.d.ts +3 -0
  427. package/esm/src/providers/decision/usage.js +25 -0
  428. package/esm/src/providers/google/api-error.d.ts +8 -0
  429. package/esm/src/providers/google/api-error.js +123 -0
  430. package/esm/src/providers/google/grounding.d.ts +5 -0
  431. package/esm/src/providers/google/grounding.js +226 -0
  432. package/esm/src/providers/google/interactions/framing.d.ts +15 -0
  433. package/esm/src/providers/google/interactions/framing.js +263 -0
  434. package/esm/src/providers/google/interactions/mod.d.ts +2 -0
  435. package/esm/src/providers/google/interactions/mod.js +2 -0
  436. package/esm/src/providers/google/interactions/steps.d.ts +12 -0
  437. package/esm/src/providers/google/interactions/steps.js +200 -0
  438. package/esm/src/providers/google/interactions/stream.d.ts +24 -0
  439. package/esm/src/providers/google/interactions/stream.js +297 -0
  440. package/esm/src/providers/google/keys.d.ts +20 -0
  441. package/esm/src/providers/google/keys.js +87 -0
  442. package/esm/src/providers/google/live/framing.d.ts +51 -0
  443. package/esm/src/providers/google/live/framing.js +597 -0
  444. package/esm/src/providers/google/live/mod.d.ts +7 -0
  445. package/esm/src/providers/google/live/mod.js +7 -0
  446. package/esm/src/providers/google/live/openapi-schema.d.ts +1 -0
  447. package/esm/src/providers/google/live/openapi-schema.js +42 -0
  448. package/esm/src/providers/google/live/session.d.ts +14 -0
  449. package/esm/src/providers/google/live/session.js +150 -0
  450. package/esm/src/providers/google/live/stream.d.ts +51 -0
  451. package/esm/src/providers/google/live/stream.js +263 -0
  452. package/esm/src/providers/google/thinking.d.ts +3 -0
  453. package/esm/src/providers/google/thinking.js +8 -0
  454. package/esm/src/providers/google/urls.d.ts +6 -0
  455. package/esm/src/providers/google/urls.js +6 -0
  456. package/esm/src/providers/google/usage.d.ts +7 -0
  457. package/esm/src/providers/google/usage.js +49 -0
  458. package/esm/src/providers/local/local.d.ts +12 -0
  459. package/esm/src/providers/local/local.js +186 -0
  460. package/esm/src/providers/local/mod.d.ts +8 -0
  461. package/esm/src/providers/local/mod.js +8 -0
  462. package/esm/src/providers/mod.d.ts +11 -0
  463. package/esm/src/providers/mod.js +7 -0
  464. package/esm/src/providers/openrouter/cache-control.d.ts +5 -0
  465. package/esm/src/providers/openrouter/cache-control.js +4 -0
  466. package/esm/src/providers/openrouter/chat.d.ts +64 -0
  467. package/esm/src/providers/openrouter/chat.js +490 -0
  468. package/esm/src/providers/openrouter/image.d.ts +24 -0
  469. package/esm/src/providers/openrouter/image.js +187 -0
  470. package/esm/src/providers/openrouter/openai/compat.d.ts +19 -0
  471. package/esm/src/providers/openrouter/openai/compat.js +162 -0
  472. package/esm/src/providers/openrouter/openai/image-payload.d.ts +9 -0
  473. package/esm/src/providers/openrouter/openai/image-payload.js +115 -0
  474. package/esm/src/providers/openrouter/openai/sdk-messages.d.ts +10 -0
  475. package/esm/src/providers/openrouter/openai/sdk-messages.js +99 -0
  476. package/esm/src/providers/openrouter/openai/usage.d.ts +8 -0
  477. package/esm/src/providers/openrouter/openai/usage.js +56 -0
  478. package/esm/src/providers/openrouter/resolve-api-key.d.ts +3 -0
  479. package/esm/src/providers/openrouter/resolve-api-key.js +8 -0
  480. package/esm/src/providers/openrouter/speech.d.ts +18 -0
  481. package/esm/src/providers/openrouter/speech.js +109 -0
  482. package/esm/src/providers/probe.d.ts +5 -0
  483. package/esm/src/providers/probe.js +15 -0
  484. package/esm/src/providers/shared/builtin-wire.d.ts +4 -0
  485. package/esm/src/providers/shared/builtin-wire.js +9 -0
  486. package/esm/src/providers/shared/pcm.d.ts +14 -0
  487. package/esm/src/providers/shared/pcm.js +78 -0
  488. package/esm/src/providers/shared/response-identity.d.ts +11 -0
  489. package/esm/src/providers/shared/response-identity.js +12 -0
  490. package/esm/src/providers/shared/sse.d.ts +10 -0
  491. package/esm/src/providers/shared/sse.js +79 -0
  492. package/esm/src/providers/shared/structured-output.d.ts +4 -0
  493. package/esm/src/providers/shared/structured-output.js +11 -0
  494. package/esm/src/providers/shared/tool-args.d.ts +25 -0
  495. package/esm/src/providers/shared/tool-args.js +84 -0
  496. package/esm/src/providers/shared/upstream-tap.d.ts +8 -0
  497. package/esm/src/providers/shared/upstream-tap.js +85 -0
  498. package/esm/src/providers/shared/upstream-tape.d.ts +6 -0
  499. package/esm/src/providers/shared/upstream-tape.js +88 -0
  500. package/esm/src/providers/shared/vault.d.ts +13 -0
  501. package/esm/src/providers/shared/vault.js +41 -0
  502. package/esm/src/providers/types.d.ts +19 -0
  503. package/esm/src/providers/types.js +1 -0
  504. package/esm/src/surface/formats.d.ts +28 -0
  505. package/esm/src/surface/formats.js +139 -0
  506. package/esm/src/surface/mod.d.ts +14 -0
  507. package/esm/src/surface/mod.js +10 -0
  508. package/esm/src/surface/runtime.d.ts +40 -0
  509. package/esm/src/surface/runtime.js +439 -0
  510. package/esm/src/surface/tools.d.ts +15 -0
  511. package/esm/src/surface/tools.js +80 -0
  512. package/esm/src/surface/types.d.ts +104 -0
  513. package/esm/src/surface/types.js +4 -0
  514. package/package.json +108 -4
@@ -0,0 +1,248 @@
1
+ import { lexiconText } from './lexicon.js';
2
+ import { detectionForTrust } from './policy.js';
3
+ import { TOOL_RULES } from './rules.js';
4
+ import { sanitizeText } from './sanitize.js';
5
+ import { anySensitive } from './sensitive.js';
6
+ import { textForScan } from './serialize.js';
7
+ import { advisoryLevel, directiveHits } from './tool-directives.js';
8
+ const TOOL_CLOSE = '</tool_data>';
9
+ const TOOL_OPEN = '<tool_data';
10
+ /** Origins whose bytes the host does not author and cannot vouch for. */
11
+ const REMOTE_ORIGINS = new Set(['http', 'mcp', 'delegated']);
12
+ function isRemoteOrigin(origin) {
13
+ return REMOTE_ORIGINS.has(origin);
14
+ }
15
+ /**
16
+ * Strip fence markers a tool result tried to forge before wrapping it.
17
+ * Linear scan — avoids polynomial regex on forged `<tool_data…>` runs.
18
+ */
19
+ function stripToolFences(text) {
20
+ const lower = text.toLowerCase();
21
+ let out = '';
22
+ let i = 0;
23
+ while (i < text.length) {
24
+ const openAt = lower.indexOf(TOOL_OPEN, i);
25
+ const closeAt = lower.indexOf(TOOL_CLOSE, i);
26
+ let next = -1;
27
+ let kind = null;
28
+ if (openAt >= 0 && (closeAt < 0 || openAt <= closeAt)) {
29
+ next = openAt;
30
+ kind = 'open';
31
+ }
32
+ else if (closeAt >= 0) {
33
+ next = closeAt;
34
+ kind = 'close';
35
+ }
36
+ if (next < 0 || kind === null) {
37
+ out += text.slice(i);
38
+ break;
39
+ }
40
+ out += text.slice(i, next);
41
+ if (kind === 'close') {
42
+ i = next + TOOL_CLOSE.length;
43
+ continue;
44
+ }
45
+ const gt = text.indexOf('>', next + TOOL_OPEN.length);
46
+ if (gt < 0) {
47
+ out += text.slice(next);
48
+ break;
49
+ }
50
+ i = gt + 1;
51
+ }
52
+ return out;
53
+ }
54
+ /**
55
+ * The origin is on the tag rather than in prose so a result cannot claim a
56
+ * friendlier provenance than it has by writing one into its own body.
57
+ */
58
+ function wrapToolData(text, provenance, advisory = 'none', lexicon) {
59
+ const guidance = lexiconText('advisory.guidance', {}, lexicon).trim();
60
+ const attrs = `tool="${provenance.tool}" origin="${provenance.origin}"` +
61
+ (advisory === 'none' ? '' : ` advisory="${advisory}"`);
62
+ const notice = advisory === 'none'
63
+ ? ''
64
+ : `${advisoryNotice(advisory, lexicon)}${guidance ? ` ${guidance}` : ''}\n`;
65
+ return `<${'tool_data'} ${attrs}>\n${notice}${stripToolFences(text)}\n${TOOL_CLOSE}`;
66
+ }
67
+ /**
68
+ * Deliberately an observation, not an instruction: what the agent should do about
69
+ * it is product behaviour, supplied by the host as lexicon `advisory.guidance`. Emitted
70
+ * only when signals fired, so it stays rare enough to carry weight — a warning on
71
+ * every fetch is one the model learns to skip.
72
+ */
73
+ function advisoryNotice(advisory, lexicon) {
74
+ const key = advisory === 'high' ? 'advisory.notice_high' : 'advisory.notice_elevated';
75
+ return lexiconText(key, {}, lexicon);
76
+ }
77
+ /**
78
+ * `finding` is the tool's summary and `data` its structured payload; both reach
79
+ * the model, so both are guarded together rather than only the prose half.
80
+ */
81
+ function composeToolText(finding, data) {
82
+ if (data === undefined) {
83
+ return finding;
84
+ }
85
+ const rendered = textForScan(data);
86
+ if (rendered.unscannable) {
87
+ return finding;
88
+ }
89
+ return `${finding}\n${rendered.text}`;
90
+ }
91
+ /**
92
+ * Local host tools are still detected — a host tool reading a database returns
93
+ * data the host did not write — but only remote origins are fenced, because
94
+ * fencing a local tool's output would change prompts hosts have already tuned.
95
+ */
96
+ function guardToolResult(finding, data, provenance, policy, callableTools = [], lexicon) {
97
+ const composed = composeToolText(finding, data);
98
+ const options = detectionForTrust(policy, 'untrusted');
99
+ const redacted = sanitizeText(composed, options);
100
+ const changed = redacted !== composed;
101
+ // Directive detection runs on remote content only: a local tool's output is
102
+ // bytes the host's own code produced.
103
+ const remote = isRemoteOrigin(provenance.origin);
104
+ const suspicious = remote ? directiveHits(composed, callableTools) : [];
105
+ const advisory = advisoryLevel(suspicious);
106
+ const fenced = remote ? wrapToolData(redacted, provenance, advisory, lexicon) : redacted;
107
+ const hits = [
108
+ ...(changed ? [{ rule: TOOL_RULES.resultRedacted, severity: 'medium' }] : []),
109
+ ...suspicious,
110
+ ];
111
+ if (hits.length === 0) {
112
+ return { text: fenced };
113
+ }
114
+ return {
115
+ text: fenced,
116
+ ...(suspicious.length > 0 ? { suspicious } : {}),
117
+ event: {
118
+ stage: 'tool_result',
119
+ trust: 'untrusted',
120
+ // Redaction changed the text; directive signals only annotate it.
121
+ action: changed ? 'redact' : 'flag',
122
+ hits,
123
+ provenance,
124
+ },
125
+ };
126
+ }
127
+ /**
128
+ * A remote server authors its own error strings, so an unguarded failure message
129
+ * is the cleanest injection path across this boundary: it reaches the model
130
+ * verbatim and is framed by the kernel as a system report.
131
+ */
132
+ function guardToolFailureText(message, provenance, policy) {
133
+ const options = detectionForTrust(policy, 'untrusted');
134
+ const redacted = sanitizeText(stripToolFences(message), options);
135
+ if (redacted === message) {
136
+ return { text: redacted };
137
+ }
138
+ return {
139
+ text: redacted,
140
+ event: {
141
+ stage: 'tool_result',
142
+ trust: 'untrusted',
143
+ action: 'redact',
144
+ hits: [{ rule: TOOL_RULES.failureRedacted, severity: 'medium' }],
145
+ provenance,
146
+ },
147
+ };
148
+ }
149
+ /**
150
+ * Arguments are model-authored, so the risk is not instruction smuggling but
151
+ * exfiltration: a credential lifted from context and posted outward as a
152
+ * parameter. Detection reports rather than rewrites — silently altering a tool
153
+ * argument would make the call succeed against something the model did not ask
154
+ * for.
155
+ */
156
+ function inspectToolArguments(args, policy) {
157
+ if (!anySensitive(policy.redactSensitive)) {
158
+ return { action: 'allow' };
159
+ }
160
+ const rendered = textForScan(args);
161
+ if (rendered.unscannable) {
162
+ return { action: 'allow' };
163
+ }
164
+ const options = { sanitizeInput: false, redactSensitive: policy.redactSensitive };
165
+ if (sanitizeText(rendered.text, options) === rendered.text) {
166
+ return { action: 'allow' };
167
+ }
168
+ return {
169
+ action: 'flag',
170
+ hits: [{ rule: TOOL_RULES.sensitiveArgument, severity: 'high' }],
171
+ };
172
+ }
173
+ function toolCallEvent(verdict, provenance) {
174
+ if (verdict.action === 'allow') {
175
+ return undefined;
176
+ }
177
+ return {
178
+ stage: 'tool_call',
179
+ trust: 'untrusted',
180
+ action: verdict.action,
181
+ hits: verdict.hits,
182
+ provenance,
183
+ };
184
+ }
185
+ /**
186
+ * Only remote origins taint: a local host tool returns bytes the host's own code
187
+ * produced, and treating those as attacker-influenceable would make the gate
188
+ * useless in practice.
189
+ */
190
+ function recordTaint(taint, provenance, suspicious = []) {
191
+ const sources = taint?.sources ?? [];
192
+ const prior = taint?.suspicious ?? [];
193
+ if (!isRemoteOrigin(provenance.origin)) {
194
+ return { sources, suspicious: prior };
195
+ }
196
+ return { sources: [...sources, provenance], suspicious: [...prior, ...suspicious] };
197
+ }
198
+ function isTainted(taint) {
199
+ return (taint?.sources.length ?? 0) > 0;
200
+ }
201
+ function isSuspicious(taint) {
202
+ return (taint?.suspicious.length ?? 0) > 0;
203
+ }
204
+ /** Capability rank, so a single threshold can express "this and anything worse". */
205
+ const CAPABILITY_RANK = {
206
+ 'read-only': 0,
207
+ 'read-write': 1,
208
+ destructive: 2,
209
+ };
210
+ const GATE_RANK = {
211
+ off: Number.POSITIVE_INFINITY,
212
+ destructive: 2,
213
+ write: 1,
214
+ };
215
+ /**
216
+ * A `flag` says the call is happening on a tainted turn and is worth recording; a
217
+ * `block` says the profile asked for it to be refused. Reporting happens whether
218
+ * or not enforcement is configured, so the risk is visible before a host opts in.
219
+ */
220
+ function checkTaintGate(taint, access, policy, lexicon) {
221
+ if (!isTainted(taint)) {
222
+ return { action: 'allow' };
223
+ }
224
+ const rank = CAPABILITY_RANK[access] ?? 0;
225
+ if (rank === 0) {
226
+ return { action: 'allow' };
227
+ }
228
+ const suspicious = isSuspicious(taint);
229
+ const hits = [
230
+ {
231
+ rule: suspicious ? TOOL_RULES.steeredTurn : TOOL_RULES.taintedTurn,
232
+ severity: 'high',
233
+ },
234
+ ];
235
+ // Gated on origin alone. Whether the content looked directive changes the rule
236
+ // reported, never whether the call is refused.
237
+ if (rank < GATE_RANK[policy.taint?.afterRemoteRead ?? 'off']) {
238
+ return { action: 'flag', hits };
239
+ }
240
+ const read = taint?.sources.map((s) => s.tool).join(', ') ?? '';
241
+ const reason = lexiconText(suspicious ? 'taint.reason_steered' : 'taint.reason_tainted', {}, lexicon);
242
+ return {
243
+ action: 'block',
244
+ hits,
245
+ rejection: lexiconText('taint.blocked', { access, sources: read, reason }, lexicon),
246
+ };
247
+ }
248
+ export { checkTaintGate, composeToolText, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint, TOOL_CLOSE, toolCallEvent, wrapToolData, };
@@ -0,0 +1,242 @@
1
+ import type { EgressChecks } from './egress.js';
2
+ import type { GivenUrls } from './egress-urls.js';
3
+ import type { GuardrailEvent, GuardrailHit, Provenance } from './event-schemas.js';
4
+ import type { LexiconOverrides } from './lexicon.js';
5
+ import type { SensitiveGroups, SensitiveSelection } from './sensitive.js';
6
+ export type { GuardrailEvent, GuardrailHit, Provenance };
7
+ /**
8
+ * - `trusted` — author-time profile text (`identity.system`). Sensitive redaction
9
+ * only; injection redaction would mangle the host's own instructions.
10
+ * - `assembled` — host-built per turn (`req.system`). Interpolates retrieval and
11
+ * user data, so it is permeable and takes full detection.
12
+ * - `untrusted` — user input, tool results, attachments, delegated agents.
13
+ */
14
+ export declare const TRUST_LEVELS: readonly ["trusted", "assembled", "untrusted"];
15
+ export type TrustLevel = (typeof TRUST_LEVELS)[number];
16
+ export declare const GUARDRAIL_STAGES: readonly ["input", "history", "system", "attachment", "tool_call", "tool_result", "output_delta", "output_final", "thought", "network", "live_inbound", "live_outbound", "trace"];
17
+ export type GuardrailStage = (typeof GUARDRAIL_STAGES)[number];
18
+ /** Does not decide what happens next; that is `onBlock`. */
19
+ export declare const SEVERITIES: readonly ["info", "low", "medium", "high"];
20
+ export type Severity = (typeof SEVERITIES)[number];
21
+ export declare const EGRESS_ON_BLOCK: readonly ["reject_to_agent", "refuse_to_user"];
22
+ export type EgressOnBlock = (typeof EGRESS_ON_BLOCK)[number];
23
+ /**
24
+ * A discriminated union so a new variant fails every unhandled `switch` at
25
+ * compile time rather than falling through at runtime.
26
+ */
27
+ export type Verdict = {
28
+ action: 'allow';
29
+ } | {
30
+ action: 'redact';
31
+ text: string;
32
+ hits: GuardrailHit[];
33
+ } | {
34
+ action: 'flag';
35
+ hits: GuardrailHit[];
36
+ } | {
37
+ action: 'block';
38
+ hits: GuardrailHit[];
39
+ /**
40
+ * Sent to the model on a repair turn when `onBlock` is `reject_to_agent`.
41
+ * With `refuse_to_user` the user reads the lexicon's `egress.refusal`.
42
+ */
43
+ rejection: string;
44
+ /**
45
+ * Why the check blocked, for the builder only: it reaches the host and the
46
+ * trace (content-gated), never the model or a client (`forClient`).
47
+ */
48
+ errorInternal?: string;
49
+ };
50
+ export declare const GUARDRAIL_ACTIONS: readonly ["allow", "redact", "flag", "block"];
51
+ export type GuardrailAction = (typeof GUARDRAIL_ACTIONS)[number];
52
+ /**
53
+ * `local` is host TypeScript the profile registered; `http` and `mcp` are remote
54
+ * services whose bytes the host does not control. `delegated` is another agent
55
+ * answering through the tool boundary — its output is model-generated prose that
56
+ * reads as authoritative, which is why depth is tracked separately.
57
+ */
58
+ export declare const TOOL_ORIGINS: readonly ["local", "builtin", "http", "mcp", "delegated"];
59
+ export type ToolOrigin = (typeof TOOL_ORIGINS)[number];
60
+ /**
61
+ * Once a turn has read attacker-influenceable bytes, a later tool call is a
62
+ * confused-deputy risk: the content can ask the agent to act, and the agent has
63
+ * authority the content does not. Sources are kept in order so a policy can reason
64
+ * about depth as well as presence.
65
+ */
66
+ export interface TurnTaint {
67
+ sources: Provenance[];
68
+ /**
69
+ * Separates "read something remote" from "read something that tried to steer
70
+ * me". The second is far rarer, so a gate keyed on it refuses far less
71
+ * legitimate work.
72
+ */
73
+ suspicious: GuardrailHit[];
74
+ }
75
+ /**
76
+ * Not a probability: there is no calibrated model behind it. `elevated` means one
77
+ * directive signal alongside an external destination; `high` means the content
78
+ * named a tool the model can call, or several signals agreed.
79
+ */
80
+ export declare const ADVISORY_LEVELS: readonly ["none", "elevated", "high"];
81
+ export type AdvisoryLevel = (typeof ADVISORY_LEVELS)[number];
82
+ export declare const TAINT_GATES: readonly ["off", "destructive", "write"];
83
+ export type TaintGate = (typeof TAINT_GATES)[number];
84
+ /**
85
+ * Enforcement is opt-in: every remote read is reported, but refusing tool calls
86
+ * changes what working agents may do, so the host declares which access levels
87
+ * to gate rather than having the kernel guess.
88
+ *
89
+ * Only structural facts gate tool calls. Content signals from `tool-directives.ts` deliberately have no gate here. They
90
+ * are pattern matches with no measured precision, and refusing a tool call on an
91
+ * unpredictable signal makes an agent unreliable rather than safe — the failure is
92
+ * invisible to the user and looks like the agent being stupid. Those signals
93
+ * annotate the fence and raise telemetry; the model still gets to decide, and the
94
+ * host still gets to see.
95
+ */
96
+ export interface TaintGuardrailSpec {
97
+ /**
98
+ * - `off` (default) — report only.
99
+ * - `destructive` — refuse hard-to-undo calls.
100
+ * - `write` — refuse those and any state-changing call.
101
+ *
102
+ * Stated as a capability threshold rather than a list of tool `access` values so
103
+ * the guardrail vocabulary stays independent of the tool registry; the kernel
104
+ * maps a tool's declared access onto it.
105
+ */
106
+ afterRemoteRead?: TaintGate;
107
+ }
108
+ /** Facts a check may read. Deliberately excludes the full profile. */
109
+ export interface GuardrailContext {
110
+ stage: GuardrailStage;
111
+ trust: TrustLevel;
112
+ profileId: string;
113
+ canary?: string;
114
+ /**
115
+ * The private stretches of the system prompt as sent (`BoundSystem.private`),
116
+ * when the profile guards it against echo (`guardrails.promptEcho`): a reply
117
+ * repeating one is a leak.
118
+ */
119
+ privateSystem?: readonly string[];
120
+ role?: string;
121
+ slots?: Record<string, string>;
122
+ /** Set on tool-shaped stages; absent for user and system text. */
123
+ provenance?: Provenance;
124
+ /**
125
+ * Canonical absolute URLs in what the model was given this turn or session:
126
+ * the system prompt, the user's input and history, and apart, tool results.
127
+ * An image or link in the reply to any other URL can carry data to its
128
+ * server. The kernel adds to them as the model is given more. Unset: none.
129
+ */
130
+ givenUrls?: GivenUrls;
131
+ /** The profile's lexicon, so a policy's rejection reads in the host's wording. */
132
+ lexicon?: LexiconOverrides;
133
+ }
134
+ /**
135
+ * Structured output travels alongside text so a profile with `outputs.structured`
136
+ * is not invisible to its own egress policy.
137
+ */
138
+ export interface OutboundPayload {
139
+ /** Concatenated user-visible text for this attempt. */
140
+ text: string;
141
+ structured?: unknown;
142
+ }
143
+ export type EgressEnforcer = (payload: OutboundPayload, context: GuardrailContext) => Verdict | Promise<Verdict>;
144
+ /** `enforce` or `checks`, never both. */
145
+ export interface ProfileEgressSpec {
146
+ /** The host's own check. */
147
+ enforce?: EgressEnforcer;
148
+ /**
149
+ * The bundled policy's checks: `true` runs each at its default, `false` none
150
+ * but the system-prompt leak checks, and an object switches the ones it names.
151
+ */
152
+ checks?: boolean | EgressChecks;
153
+ onBlock?: EgressOnBlock;
154
+ maxRetries?: number;
155
+ /**
156
+ * For a host `enforce` only: characters the progressive gate holds back so
157
+ * `enforce` sees a match split across stream chunks before any of it is
158
+ * released (default `DEFAULT_HOLDBACK`, 256; on Live `LIVE_DEFAULT_HOLDBACK`,
159
+ * 96). The bundled `standardEgressEnforce` holds exactly what could still
160
+ * become a match, and setting this with it is a profile error.
161
+ */
162
+ holdback?: number;
163
+ }
164
+ export interface NetworkGuardrailSpec {
165
+ /** Allows localhost, loopback and private subnets (local dev and testing). Default false. */
166
+ allowPrivateNetworks?: boolean;
167
+ /** Hostnames or IP addresses permitted regardless of private subnet status. */
168
+ allowedHosts?: string[];
169
+ /**
170
+ * Allowed URL schemes. Defaults to `['https']`, or `['http', 'https']` when
171
+ * `allowPrivateNetworks` is set.
172
+ */
173
+ allowedSchemes?: string[];
174
+ }
175
+ /** Optional daily turn quota consumed by host HTTP middleware. */
176
+ export interface QuotaGuardrailSpec {
177
+ perDay: number;
178
+ }
179
+ export interface ProfileGuardrailsSpec {
180
+ /** Omitted means quota enforcement is not configured. */
181
+ quota?: QuotaGuardrailSpec;
182
+ /**
183
+ * Mint a per-turn canary into the system prompt. Default true. The note
184
+ * that binds it is the lexicon's `canary.bind_note` (the profile's `lexicon`
185
+ * may replace it).
186
+ */
187
+ canary?: boolean;
188
+ /**
189
+ * With the canary on, also treat a reply that repeats `PROMPT_ECHO_WORDS`
190
+ * (12) consecutive words of the system prompt as a leak: the dump the token
191
+ * alone cannot see. Default true; set false when the prompt holds text the
192
+ * agent is meant to quote word for word.
193
+ */
194
+ promptEcho?: boolean;
195
+ sanitizeInput?: boolean;
196
+ /**
197
+ * Redact sensitive data from untrusted text before the model reads it:
198
+ * `true` (the default) every group, `false` none, an object the groups it
199
+ * switches, the rest on (`ids`, `financial`, `network`, `credentials`).
200
+ */
201
+ redactSensitive?: SensitiveSelection;
202
+ egress?: ProfileEgressSpec;
203
+ network?: NetworkGuardrailSpec;
204
+ taint?: TaintGuardrailSpec;
205
+ }
206
+ /** Pre-dispatch policy for structured state leaving a decision profile. */
207
+ export type DecisionDisclosureVerdict = Extract<Verdict, {
208
+ action: 'allow' | 'block';
209
+ }>;
210
+ export type DecisionDisclosureEnforcer = (state: unknown, context: {
211
+ destination: 'typesafe' | 'openrouter';
212
+ profileId: string;
213
+ model: string;
214
+ questionIds: string[];
215
+ }) => DecisionDisclosureVerdict | Promise<DecisionDisclosureVerdict>;
216
+ /**
217
+ * Keeps the shared guardrail vocabulary structurally compatible while the
218
+ * decision registry rejects every inherited field as inert in this release.
219
+ */
220
+ export interface DecisionGuardrailsSpec extends Partial<ProfileGuardrailsSpec> {
221
+ disclosure?: {
222
+ enforce: DecisionDisclosureEnforcer;
223
+ };
224
+ }
225
+ /** The guardrail field names a `host` profile may set. */
226
+ export declare const HOST_GUARDRAIL_FIELDS: readonly ["sanitizeInput", "redactSensitive", "network"];
227
+ /** The guardrails that fire on the `invokeTool` path; the rest guard a model turn. */
228
+ export type HostGuardrailsSpec = Pick<ProfileGuardrailsSpec, (typeof HOST_GUARDRAIL_FIELDS)[number]>;
229
+ export interface ResolvedGuardrailPolicy {
230
+ sanitizeInput: boolean;
231
+ redactSensitive: SensitiveGroups;
232
+ canary: boolean;
233
+ promptEcho: boolean;
234
+ /** `checks` resolved to the bundled policy's `enforce`. */
235
+ egress?: ResolvedEgressSpec;
236
+ network?: NetworkGuardrailSpec;
237
+ quota?: QuotaGuardrailSpec;
238
+ taint?: TaintGuardrailSpec;
239
+ }
240
+ export type ResolvedEgressSpec = Omit<ProfileEgressSpec, 'enforce' | 'checks'> & {
241
+ enforce: EgressEnforcer;
242
+ };
@@ -0,0 +1,54 @@
1
+ // Must not import from `src/kernel/`: the kernel type-imports `ProfileGuardrailsSpec`, and that edge
2
+ // stays one-directional. Other modules under `src/guardrails/` may import kernel types.
3
+ /**
4
+ * - `trusted` — author-time profile text (`identity.system`). Sensitive redaction
5
+ * only; injection redaction would mangle the host's own instructions.
6
+ * - `assembled` — host-built per turn (`req.system`). Interpolates retrieval and
7
+ * user data, so it is permeable and takes full detection.
8
+ * - `untrusted` — user input, tool results, attachments, delegated agents.
9
+ */
10
+ export const TRUST_LEVELS = ['trusted', 'assembled', 'untrusted'];
11
+ export const GUARDRAIL_STAGES = [
12
+ 'input',
13
+ 'history',
14
+ 'system',
15
+ 'attachment',
16
+ 'tool_call',
17
+ 'tool_result',
18
+ 'output_delta',
19
+ 'output_final',
20
+ 'thought',
21
+ 'network',
22
+ 'live_inbound',
23
+ 'live_outbound',
24
+ 'trace',
25
+ ];
26
+ /** Does not decide what happens next; that is `onBlock`. */
27
+ export const SEVERITIES = ['info', 'low', 'medium', 'high'];
28
+ export const EGRESS_ON_BLOCK = ['reject_to_agent', 'refuse_to_user'];
29
+ export const GUARDRAIL_ACTIONS = [
30
+ 'allow',
31
+ 'redact',
32
+ 'flag',
33
+ 'block',
34
+ ];
35
+ /**
36
+ * `local` is host TypeScript the profile registered; `http` and `mcp` are remote
37
+ * services whose bytes the host does not control. `delegated` is another agent
38
+ * answering through the tool boundary — its output is model-generated prose that
39
+ * reads as authoritative, which is why depth is tracked separately.
40
+ */
41
+ export const TOOL_ORIGINS = ['local', 'builtin', 'http', 'mcp', 'delegated'];
42
+ /**
43
+ * Not a probability: there is no calibrated model behind it. `elevated` means one
44
+ * directive signal alongside an external destination; `high` means the content
45
+ * named a tool the model can call, or several signals agreed.
46
+ */
47
+ export const ADVISORY_LEVELS = ['none', 'elevated', 'high'];
48
+ export const TAINT_GATES = ['off', 'destructive', 'write'];
49
+ /** The guardrail field names a `host` profile may set. */
50
+ export const HOST_GUARDRAIL_FIELDS = [
51
+ 'sanitizeInput',
52
+ 'redactSensitive',
53
+ 'network',
54
+ ];
@@ -0,0 +1,9 @@
1
+ import type { TurnEvent } from '../kernel/types.js';
2
+ export interface ClientTurnOptions {
3
+ /** Keep provider-native payloads on `evidence` events. Default: false; parsed fields remain. */
4
+ includeEvidenceRaw?: boolean;
5
+ }
6
+ /** Strips host-only diagnostics, so the copy is safe to forward to browsers or end-user SSE. */
7
+ declare function forClient(event: TurnEvent, options?: ClientTurnOptions): TurnEvent;
8
+ declare function forClientEvents(events: TurnEvent[], options?: ClientTurnOptions): TurnEvent[];
9
+ export { forClient, forClientEvents };
@@ -0,0 +1,38 @@
1
+ import { projectGuardrailTurnEvent } from '../guardrails/events.js';
2
+ /** Raw diagnostics ride on error events, an ended session's close, and a tool call's failure. */
3
+ function stripErrorInternal(event) {
4
+ if ((event.type !== 'error' && event.type !== 'session' && event.type !== 'tool') ||
5
+ event.errorInternal === undefined) {
6
+ return event;
7
+ }
8
+ const { errorInternal: _internal, ...rest } = event;
9
+ return rest;
10
+ }
11
+ function stripGuardrailInternal(event) {
12
+ if (event.type !== 'guardrail' || !event.guardrail?.errorInternal) {
13
+ return event;
14
+ }
15
+ const { errorInternal: _internal, ...guardrail } = event.guardrail;
16
+ return { ...event, guardrail };
17
+ }
18
+ function stripEvidenceRaw(event) {
19
+ if (event.type !== 'evidence' || !event.evidence?.raw) {
20
+ return event;
21
+ }
22
+ const { raw: _raw, ...evidence } = event.evidence;
23
+ return { ...event, evidence };
24
+ }
25
+ /** Strips host-only diagnostics, so the copy is safe to forward to browsers or end-user SSE. */
26
+ function forClient(event, options) {
27
+ let out = stripGuardrailInternal(stripErrorInternal(event));
28
+ if (!options?.includeEvidenceRaw) {
29
+ out = stripEvidenceRaw(out);
30
+ }
31
+ // Clients never receive matched substrings, even if the host opted into guardrailMatchPreview.
32
+ out = projectGuardrailTurnEvent(out, false);
33
+ return out;
34
+ }
35
+ function forClientEvents(events, options) {
36
+ return events.map((event) => forClient(event, options));
37
+ }
38
+ export { forClient, forClientEvents };
@@ -0,0 +1,28 @@
1
+ import type { Profile } from '../kernel/types.js';
2
+ import { type TraceRecord } from '../observability/trace-record.js';
3
+ import type { TraceSink } from '../observability/trace-sink.js';
4
+ interface CutoutTape {
5
+ ok: boolean;
6
+ /** How long the cutout took; the span ends now and starts this long before. */
7
+ ms: number;
8
+ url?: string;
9
+ inSha256?: string;
10
+ outSha256?: string;
11
+ /** The upstream exchange, recorded as a `theorem.upstream.row` event. */
12
+ http?: unknown;
13
+ error?: string;
14
+ }
15
+ /**
16
+ * Writes the held turn record, then one `cutout` span parented on its root, so both read as one
17
+ * trace. Without a `sink` nothing is written.
18
+ */
19
+ declare function flushMintTrace(args: {
20
+ /** The profile the held turn ran on; its observability governs both writes. */
21
+ profile: Profile;
22
+ held: TraceRecord[];
23
+ app: Record<string, unknown>;
24
+ cutout: CutoutTape;
25
+ sink?: TraceSink;
26
+ }): Promise<void>;
27
+ export type { CutoutTape };
28
+ export { flushMintTrace };
@@ -0,0 +1,59 @@
1
+ import { TheoremError } from '../guardrails/error.js';
2
+ import { urlAttributes } from '../kernel/engine/turn-trace.js';
3
+ import { resolveTraceWriter } from '../observability/policy.js';
4
+ import { writeTrace } from '../observability/trace.js';
5
+ import { buildRecord } from '../observability/trace-record.js';
6
+ import { formatTraceparent, startTrace, traceContent, traceJson, } from '../observability/trace-span.js';
7
+ const NANOS_PER_MS = 1_000_000;
8
+ const AGENT_NAME = 'gen_ai.agent.name';
9
+ function cutoutAttributes(cutout) {
10
+ return {
11
+ ...urlAttributes(cutout.url),
12
+ ...(cutout.inSha256 ? { 'theorem.cutout.input.sha256': cutout.inSha256 } : {}),
13
+ ...(cutout.outSha256 ? { 'theorem.cutout.output.sha256': cutout.outSha256 } : {}),
14
+ };
15
+ }
16
+ /**
17
+ * Writes the held turn record, then one `cutout` span parented on its root, so both read as one
18
+ * trace. Without a `sink` nothing is written.
19
+ */
20
+ async function flushMintTrace(args) {
21
+ const [record] = args.held;
22
+ const root = record?.spans[0];
23
+ if (!record || !root || !args.sink) {
24
+ return;
25
+ }
26
+ const ranOn = root.attributes[AGENT_NAME];
27
+ if (ranOn !== args.profile.id) {
28
+ throw new TheoremError('config', `flushMintTrace: the held turn ran on '${String(ranOn)}', not '${args.profile.id}'`);
29
+ }
30
+ const { sink, policy } = resolveTraceWriter({
31
+ override: args.sink,
32
+ observability: args.profile.observability,
33
+ });
34
+ await writeTrace(sink, Promise.resolve(record), policy);
35
+ const { cutout } = args;
36
+ // One instant: the cutout was reported done now and ran for `ms` before it.
37
+ const now = BigInt(Date.now()) * BigInt(NANOS_PER_MS);
38
+ const tree = startTrace('cutout', {
39
+ clock: { nowUnixNano: () => now },
40
+ kind: 'CLIENT',
41
+ traceparent: formatTraceparent(root.traceId, root.spanId),
42
+ startTimeUnixNano: String(now - BigInt(Math.round(cutout.ms * NANOS_PER_MS))),
43
+ attributes: cutoutAttributes(cutout),
44
+ });
45
+ if (cutout.http !== undefined) {
46
+ tree.root.event('theorem.upstream.row', { row: traceJson(cutout.http) });
47
+ }
48
+ // The host's error text is free text: stored by hash under the scrub, like any exception.
49
+ if (cutout.error) {
50
+ tree.root.event('exception', { 'exception.message': traceContent(cutout.error) });
51
+ }
52
+ tree.root.end({ code: cutout.ok ? 'OK' : 'ERROR' });
53
+ await writeTrace(sink, buildRecord({
54
+ spans: tree.collect(),
55
+ policy,
56
+ metadata: { ...record.metadata, app: args.app },
57
+ }), policy);
58
+ }
59
+ export { flushMintTrace };