@sema-agent/core 7.58.0 → 7.60.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 (519) hide show
  1. package/CHANGELOG.md +1340 -0
  2. package/README.md +6 -4
  3. package/dist/agents/agent-definition.d.ts +7 -0
  4. package/dist/agents/agent-definition.js +12 -0
  5. package/dist/agents/agent-transcript-tool.d.ts +2 -1
  6. package/dist/agents/agent-transcript-tool.js +3 -1
  7. package/dist/agents/cc-peer-directory.js +8 -8
  8. package/dist/agents/child-model-seat.d.ts +4 -2
  9. package/dist/agents/child-model-seat.js +9 -4
  10. package/dist/agents/cumulative-stats.js +0 -1
  11. package/dist/agents/inherited-tool-pool.d.ts +26 -1
  12. package/dist/agents/inherited-tool-pool.js +22 -0
  13. package/dist/agents/peer-directory.d.ts +9 -0
  14. package/dist/agents/peer-directory.js +6 -2
  15. package/dist/agents/retain-ledger.d.ts +11 -1
  16. package/dist/agents/retain-ledger.js +13 -2
  17. package/dist/agents/roster-store.d.ts +1 -1
  18. package/dist/agents/roster-store.js +3 -1
  19. package/dist/agents/send-message-tool.d.ts +8 -19
  20. package/dist/agents/send-message-tool.js +32 -50
  21. package/dist/agents/subagent.d.ts +51 -17
  22. package/dist/agents/subagent.js +150 -112
  23. package/dist/agents/tool-filter.d.ts +1 -3
  24. package/dist/agents/tool-filter.js +4 -12
  25. package/dist/bench/metrics.d.ts +3 -5
  26. package/dist/bench/metrics.js +0 -1
  27. package/dist/brain/anthropic.js +161 -45
  28. package/dist/brain/degrading.js +3 -1
  29. package/dist/brain/errors.d.ts +9 -3
  30. package/dist/brain/errors.js +7 -1
  31. package/dist/brain/failover.js +2 -1
  32. package/dist/brain/open-responses.js +3 -3
  33. package/dist/brain/openai.js +103 -53
  34. package/dist/brain/provider-dialect.d.ts +11 -0
  35. package/dist/brain/provider-dialect.js +5 -2
  36. package/dist/brain/reasoning-request.js +5 -4
  37. package/dist/brain/reasoning.d.ts +19 -0
  38. package/dist/brain/reasoning.js +17 -2
  39. package/dist/brain/request-route.d.ts +4 -1
  40. package/dist/brain/request-route.js +4 -1
  41. package/dist/brain/route-adjudicator.d.ts +84 -18
  42. package/dist/brain/route-adjudicator.js +90 -22
  43. package/dist/brain/route-conformance.js +1 -1
  44. package/dist/brain/status-sink.d.ts +7 -1
  45. package/dist/brain/status-sink.js +6 -0
  46. package/dist/brain/stream-engine.js +84 -49
  47. package/dist/brain/stream-shared.d.ts +30 -0
  48. package/dist/brain/stream-shared.js +34 -1
  49. package/dist/brain/timeout.d.ts +65 -6
  50. package/dist/brain/timeout.js +111 -11
  51. package/dist/core/a2a.d.ts +15 -5
  52. package/dist/core/a2a.js +34 -19
  53. package/dist/core/account-row.d.ts +61 -0
  54. package/dist/core/account-row.js +51 -0
  55. package/dist/core/advisor-verdict.d.ts +2 -1
  56. package/dist/core/advisor-verdict.js +1 -1
  57. package/dist/core/agent-definition.d.ts +20 -0
  58. package/dist/core/allow-entry.d.ts +20 -6
  59. package/dist/core/allow-entry.js +17 -7
  60. package/dist/core/approval-object.d.ts +29 -15
  61. package/dist/core/approval-object.js +47 -19
  62. package/dist/core/artifact-host.d.ts +0 -2
  63. package/dist/core/artifact-host.js +0 -1
  64. package/dist/core/auto-compaction.js +1 -1
  65. package/dist/core/auto-mode-defaults.d.ts +8 -0
  66. package/dist/core/auto-mode-defaults.js +1 -0
  67. package/dist/core/auto-mode-rebuild.d.ts +2 -1
  68. package/dist/core/auto-mode.d.ts +39 -4
  69. package/dist/core/auto-mode.js +63 -12
  70. package/dist/core/background-agent-store.d.ts +26 -4
  71. package/dist/core/background-agent-store.js +3 -2
  72. package/dist/core/background-shell.d.ts +12 -1
  73. package/dist/core/background-shell.js +12 -0
  74. package/dist/core/checkpoint-requirements.d.ts +13 -1
  75. package/dist/core/checkpoint-requirements.js +3 -1
  76. package/dist/core/checkpoint-store.d.ts +66 -25
  77. package/dist/core/checkpoint-store.js +22 -9
  78. package/dist/core/child-clamp-seats.d.ts +29 -0
  79. package/dist/core/child-clamp-seats.js +56 -1
  80. package/dist/core/compliance.d.ts +3 -3
  81. package/dist/core/compliance.js +1 -1
  82. package/dist/core/credential-echo.d.ts +52 -0
  83. package/dist/core/credential-echo.js +65 -0
  84. package/dist/core/credential-vocabulary.d.ts +13 -3
  85. package/dist/core/credential-vocabulary.js +12 -4
  86. package/dist/core/cross-boundary-taint.d.ts +4 -5
  87. package/dist/core/effective-path-target.d.ts +8 -2
  88. package/dist/core/effective-path-target.js +8 -3
  89. package/dist/core/engine-notice.d.ts +156 -23
  90. package/dist/core/engine-notice.js +19 -5
  91. package/dist/core/frame-settlement.d.ts +5 -0
  92. package/dist/core/frame-settlement.js +4 -0
  93. package/dist/core/gate-fold.js +8 -6
  94. package/dist/core/gate-lanes.js +47 -12
  95. package/dist/core/gate-outcome.d.ts +32 -3
  96. package/dist/core/gate-outcome.js +19 -1
  97. package/dist/core/governance-codes.d.ts +17 -1
  98. package/dist/core/governance-codes.js +25 -0
  99. package/dist/core/hands-band-screen.js +0 -1
  100. package/dist/core/hands-band.d.ts +0 -3
  101. package/dist/core/hooks.d.ts +70 -59
  102. package/dist/core/hooks.js +125 -80
  103. package/dist/core/image-admission.d.ts +106 -52
  104. package/dist/core/image-admission.js +184 -56
  105. package/dist/core/image-downsample.d.ts +36 -69
  106. package/dist/core/image-downsample.js +111 -73
  107. package/dist/core/integer-option.d.ts +21 -0
  108. package/dist/core/integer-option.js +13 -0
  109. package/dist/core/locked-config.d.ts +59 -5
  110. package/dist/core/locked-config.js +82 -50
  111. package/dist/core/mailbox-store.d.ts +45 -1
  112. package/dist/core/mailbox-store.js +53 -9
  113. package/dist/core/mcp-call-meta.d.ts +2 -2
  114. package/dist/core/mcp-call-meta.js +9 -10
  115. package/dist/core/mcp-failure.d.ts +122 -20
  116. package/dist/core/mcp-failure.js +73 -5
  117. package/dist/core/mcp-host-seat.d.ts +92 -0
  118. package/dist/core/mcp-host-seat.js +131 -0
  119. package/dist/core/mcp-probe.d.ts +7 -2
  120. package/dist/core/mcp-probe.js +7 -2
  121. package/dist/core/mcp-server-link.d.ts +22 -1
  122. package/dist/core/mcp-server-link.js +25 -1
  123. package/dist/core/mcp-server-spec.d.ts +38 -9
  124. package/dist/core/mcp-server-spec.js +55 -21
  125. package/dist/core/mcp.d.ts +27 -12
  126. package/dist/core/mcp.js +105 -76
  127. package/dist/core/memory-admission.d.ts +23 -6
  128. package/dist/core/memory-admission.js +19 -9
  129. package/dist/core/memory-engine/consolidation-driver.d.ts +2 -4
  130. package/dist/core/memory-engine/consolidation-driver.js +3 -3
  131. package/dist/core/memory-engine/consolidation.d.ts +4 -0
  132. package/dist/core/memory-engine/consolidation.js +1 -0
  133. package/dist/core/memory-engine/content-origin.d.ts +13 -0
  134. package/dist/core/memory-engine/content-origin.js +21 -3
  135. package/dist/core/memory-engine/delegation-settlement.js +5 -12
  136. package/dist/core/memory-engine/distiller.js +2 -2
  137. package/dist/core/memory-engine/dual-root.d.ts +50 -1
  138. package/dist/core/memory-engine/dual-root.js +44 -1
  139. package/dist/core/memory-engine/engine.d.ts +67 -14
  140. package/dist/core/memory-engine/engine.js +384 -212
  141. package/dist/core/memory-engine/export-bundle.d.ts +23 -0
  142. package/dist/core/memory-engine/export-bundle.js +23 -4
  143. package/dist/core/memory-engine/file-backend.d.ts +128 -55
  144. package/dist/core/memory-engine/file-backend.js +290 -117
  145. package/dist/core/memory-engine/index.d.ts +3 -2
  146. package/dist/core/memory-engine/index.js +2 -1
  147. package/dist/core/memory-engine/layout.d.ts +59 -11
  148. package/dist/core/memory-engine/layout.js +190 -40
  149. package/dist/core/memory-engine/memory-backend-contract.d.ts +1 -1
  150. package/dist/core/memory-engine/memory-backend-contract.js +39 -2
  151. package/dist/core/memory-engine/scope-contract.d.ts +13 -3
  152. package/dist/core/memory-engine/scope-contract.js +2 -2
  153. package/dist/core/memory-engine/sync-client.d.ts +51 -9
  154. package/dist/core/memory-engine/sync-client.js +107 -13
  155. package/dist/core/memory-engine/sync-report.d.ts +103 -0
  156. package/dist/core/memory-engine/sync-report.js +119 -0
  157. package/dist/core/memory-engine/sync.d.ts +8 -1
  158. package/dist/core/memory-engine/types.d.ts +6 -0
  159. package/dist/core/mode-policy.d.ts +9 -1
  160. package/dist/core/mode-policy.js +11 -1
  161. package/dist/core/model-receipt.d.ts +39 -0
  162. package/dist/core/model-receipt.js +139 -0
  163. package/dist/core/mods/agent-stations.d.ts +65 -0
  164. package/dist/core/mods/agent-stations.js +105 -0
  165. package/dist/core/mods/classic-events.d.ts +5 -1
  166. package/dist/core/mods/kernel/dispatch.d.ts +5 -1
  167. package/dist/core/mods/kernel/dispatch.js +6 -5
  168. package/dist/core/mods/kernel/events.d.ts +11 -4
  169. package/dist/core/mods/kernel/events.js +11 -7
  170. package/dist/core/mods/kernel/receive.d.ts +5 -0
  171. package/dist/core/mods/kernel/receive.js +10 -0
  172. package/dist/core/mods/kernel/registration.d.ts +4 -0
  173. package/dist/core/mods/kernel/registration.js +3 -0
  174. package/dist/core/mods/lifecycle-stations.d.ts +41 -0
  175. package/dist/core/mods/lifecycle-stations.js +35 -0
  176. package/dist/core/mods/stations.d.ts +73 -60
  177. package/dist/core/mods/stations.js +64 -40
  178. package/dist/core/mods/tool-call.d.ts +10 -4
  179. package/dist/core/mods/tool-call.js +28 -12
  180. package/dist/core/mounted-mcp.d.ts +5 -0
  181. package/dist/core/mounted-mcp.js +2 -2
  182. package/dist/core/namespaced-rule-name.d.ts +7 -1
  183. package/dist/core/namespaced-rule-name.js +13 -2
  184. package/dist/core/oracle-isolation.js +1 -1
  185. package/dist/core/own-property-receipt.d.ts +3 -0
  186. package/dist/core/own-property-receipt.js +3 -0
  187. package/dist/core/parent-spec-seat.d.ts +8 -3
  188. package/dist/core/parent-spec-seat.js +2 -0
  189. package/dist/core/park-selfcheck.js +14 -8
  190. package/dist/core/peer-seat.d.ts +133 -16
  191. package/dist/core/peer-seat.js +319 -42
  192. package/dist/core/permission-rule-consent.d.ts +0 -21
  193. package/dist/core/permission-rule-consent.js +11 -6
  194. package/dist/core/permission-rule-model.d.ts +20 -2
  195. package/dist/core/permission-rule-model.js +8 -7
  196. package/dist/core/present-plan-tool.d.ts +11 -3
  197. package/dist/core/present-plan-tool.js +2 -2
  198. package/dist/core/protocol-http-auth.d.ts +14 -2
  199. package/dist/core/protocol-http-auth.js +16 -7
  200. package/dist/core/read-face-seats.d.ts +7 -5
  201. package/dist/core/read-face-seats.js +4 -1
  202. package/dist/core/redaction-marks.d.ts +4 -3
  203. package/dist/core/redaction-marks.js +2 -2
  204. package/dist/core/remote-env.d.ts +146 -98
  205. package/dist/core/remote-env.js +113 -14
  206. package/dist/core/resume-startup-cause.js +1 -0
  207. package/dist/core/retention.js +37 -12
  208. package/dist/core/retired-keys.d.ts +6 -1
  209. package/dist/core/retired-keys.js +4 -1
  210. package/dist/core/rewrite-rejudge.d.ts +13 -5
  211. package/dist/core/rewrite-rejudge.js +9 -5
  212. package/dist/core/roles.d.ts +69 -3
  213. package/dist/core/roles.js +167 -80
  214. package/dist/core/rule-owner.d.ts +3 -0
  215. package/dist/core/runner/announce-once-ledger.d.ts +5 -4
  216. package/dist/core/runner/announce-once-ledger.js +4 -2
  217. package/dist/core/runner/assemble-result.d.ts +0 -5
  218. package/dist/core/runner/assemble-result.js +0 -1
  219. package/dist/core/runner/call-hook-stations.d.ts +17 -10
  220. package/dist/core/runner/call-hook-stations.js +23 -12
  221. package/dist/core/runner/child-tree.d.ts +7 -8
  222. package/dist/core/runner/child-tree.js +6 -6
  223. package/dist/core/runner/classifier-ladder.d.ts +10 -2
  224. package/dist/core/runner/classifier-ladder.js +39 -8
  225. package/dist/core/runner/construction-doors.js +5 -0
  226. package/dist/core/runner/contracts.d.ts +82 -57
  227. package/dist/core/runner/credential-origin-seat.js +4 -3
  228. package/dist/core/runner/cross-boundary-lane.d.ts +3 -5
  229. package/dist/core/runner/cross-boundary-lane.js +11 -2
  230. package/dist/core/runner/cwd-alignment-probe.d.ts +20 -0
  231. package/dist/core/runner/cwd-alignment-probe.js +21 -0
  232. package/dist/core/runner/decide-continuation.d.ts +2 -2
  233. package/dist/core/runner/env-capability-census.d.ts +15 -20
  234. package/dist/core/runner/env-capability-census.js +25 -34
  235. package/dist/core/runner/file-history-request.d.ts +2 -2
  236. package/dist/core/runner/file-history-request.js +0 -8
  237. package/dist/core/runner/gate-exit.d.ts +87 -11
  238. package/dist/core/runner/gate-exit.js +30 -8
  239. package/dist/core/runner/git-status-frame.d.ts +5 -2
  240. package/dist/core/runner/git-status-frame.js +3 -4
  241. package/dist/core/runner/image-removed-seat.d.ts +12 -0
  242. package/dist/core/runner/image-removed-seat.js +19 -0
  243. package/dist/core/runner/image.d.ts +7 -2
  244. package/dist/core/runner/image.js +14 -8
  245. package/dist/core/runner/inline-image.d.ts +10 -8
  246. package/dist/core/runner/inline-image.js +11 -9
  247. package/dist/core/runner/memory-consolidation-driver.js +5 -5
  248. package/dist/core/runner/park-commit.d.ts +1 -1
  249. package/dist/core/runner/park-commit.js +3 -0
  250. package/dist/core/runner/permission-rule-lanes.js +1 -1
  251. package/dist/core/runner/prepare-ask-lane.d.ts +1 -1
  252. package/dist/core/runner/prepare-ask-lane.js +2 -1
  253. package/dist/core/runner/prepare-boundary-parks.d.ts +2 -2
  254. package/dist/core/runner/prepare-boundary-parks.js +5 -5
  255. package/dist/core/runner/prepare-caps-and-workflow.d.ts +7 -5
  256. package/dist/core/runner/prepare-caps-and-workflow.js +6 -3
  257. package/dist/core/runner/prepare-config-doors.d.ts +6 -1
  258. package/dist/core/runner/prepare-config-doors.js +16 -9
  259. package/dist/core/runner/prepare-context-lane.js +2 -0
  260. package/dist/core/runner/prepare-delegation-surface.d.ts +9 -3
  261. package/dist/core/runner/prepare-delegation-surface.js +5 -4
  262. package/dist/core/runner/prepare-file-history.d.ts +10 -12
  263. package/dist/core/runner/prepare-file-history.js +3 -12
  264. package/dist/core/runner/prepare-gate-stations.d.ts +4 -3
  265. package/dist/core/runner/prepare-gate-stations.js +4 -3
  266. package/dist/core/runner/prepare-hands-readface.d.ts +7 -1
  267. package/dist/core/runner/prepare-hands-readface.js +19 -6
  268. package/dist/core/runner/prepare-inherited-gate.d.ts +6 -7
  269. package/dist/core/runner/prepare-inherited-gate.js +22 -31
  270. package/dist/core/runner/prepare-memory-engine-session.js +1 -1
  271. package/dist/core/runner/prepare-memory.d.ts +2 -2
  272. package/dist/core/runner/prepare-memory.js +12 -19
  273. package/dist/core/runner/prepare-park-ask.d.ts +4 -6
  274. package/dist/core/runner/prepare-park-ask.js +5 -9
  275. package/dist/core/runner/prepare-policy-chain.d.ts +8 -7
  276. package/dist/core/runner/prepare-policy-chain.js +136 -121
  277. package/dist/core/runner/prepare-project-context.d.ts +2 -2
  278. package/dist/core/runner/prepare-project-context.js +10 -7
  279. package/dist/core/runner/prepare-prompt-inputs.d.ts +5 -2
  280. package/dist/core/runner/prepare-prompt-inputs.js +10 -20
  281. package/dist/core/runner/prepare-protocol-tools.d.ts +5 -3
  282. package/dist/core/runner/prepare-protocol-tools.js +17 -2
  283. package/dist/core/runner/prepare-run-refs.d.ts +2 -3
  284. package/dist/core/runner/prepare-run-refs.js +4 -1
  285. package/dist/core/runner/prepare-suspend-saga.d.ts +0 -2
  286. package/dist/core/runner/prepare-suspend-saga.js +5 -7
  287. package/dist/core/runner/prepare-task.js +44 -28
  288. package/dist/core/runner/prepare-turn-wiring.d.ts +2 -0
  289. package/dist/core/runner/prepare-turn-wiring.js +7 -2
  290. package/dist/core/runner/prepare-wiring-manifest.d.ts +5 -4
  291. package/dist/core/runner/prepare-wiring-manifest.js +34 -19
  292. package/dist/core/runner/prepare-workspace-restore.js +3 -3
  293. package/dist/core/runner/reply-pricing.d.ts +30 -0
  294. package/dist/core/runner/reply-pricing.js +19 -0
  295. package/dist/core/runner/resume-admission.js +4 -2
  296. package/dist/core/runner/resume-apply.js +6 -1
  297. package/dist/core/runner/resume-checkpoint-screen.js +4 -4
  298. package/dist/core/runner/resume-policy-outcome.js +5 -5
  299. package/dist/core/runner/retired-runner-deps.js +4 -1
  300. package/dist/core/runner/role-seat-defaulted.d.ts +44 -0
  301. package/dist/core/runner/role-seat-defaulted.js +40 -0
  302. package/dist/core/runner/run-attachment-seats.js +3 -3
  303. package/dist/core/runner/run-brain-sinks.js +3 -0
  304. package/dist/core/runner/run-git-lane.d.ts +3 -1
  305. package/dist/core/runner/run-git-lane.js +1 -1
  306. package/dist/core/runner/run-harness-handlers.d.ts +3 -1
  307. package/dist/core/runner/run-harness-handlers.js +3 -1
  308. package/dist/core/runner/run-identity-wiring.d.ts +2 -5
  309. package/dist/core/runner/run-identity-wiring.js +6 -5
  310. package/dist/core/runner/run-leg.d.ts +2 -0
  311. package/dist/core/runner/run-leg.js +63 -56
  312. package/dist/core/runner/run-notification-lane.d.ts +3 -1
  313. package/dist/core/runner/run-notification-lane.js +16 -10
  314. package/dist/core/runner/run-settle-and-teardown.js +5 -1
  315. package/dist/core/runner/run-stop-and-final-verify.d.ts +4 -0
  316. package/dist/core/runner/run-stop-and-final-verify.js +14 -6
  317. package/dist/core/runner/run-terminal-adoption.d.ts +2 -5
  318. package/dist/core/runner/run-terminal-adoption.js +9 -3
  319. package/dist/core/runner/run-turn-boundary.js +89 -77
  320. package/dist/core/runner/runtask.d.ts +8 -5
  321. package/dist/core/runner/runtask.js +21 -13
  322. package/dist/core/runner/side-call-account.d.ts +1 -1
  323. package/dist/core/runner/side-call-account.js +3 -18
  324. package/dist/core/runner/skills-seat.js +2 -2
  325. package/dist/core/runner/sub-agent-deny-lists.d.ts +24 -0
  326. package/dist/core/runner/sub-agent-deny-lists.js +20 -7
  327. package/dist/core/runner/synthetic-tools.js +2 -2
  328. package/dist/core/runner/tool-disclosure.js +0 -2
  329. package/dist/core/runner/tool-end-body.js +2 -2
  330. package/dist/core/runner/tool-face-overlay.d.ts +1 -1
  331. package/dist/core/runner/tool-face-overlay.js +26 -19
  332. package/dist/core/runner/tool-occurrence.d.ts +1 -1
  333. package/dist/core/runner/tool-occurrence.js +2 -2
  334. package/dist/core/runner/tool-result-media-seat.d.ts +9 -1
  335. package/dist/core/runner/tool-result-media-seat.js +17 -1
  336. package/dist/core/runner/turn-attachments.d.ts +29 -27
  337. package/dist/core/runner/turn-attachments.js +27 -42
  338. package/dist/core/runner-deps.d.ts +32 -19
  339. package/dist/core/secret-env.d.ts +35 -4
  340. package/dist/core/secret-env.js +65 -3
  341. package/dist/core/sensitive-path-policy.js +3 -3
  342. package/dist/core/session-policy-store.d.ts +12 -3
  343. package/dist/core/session-policy-store.js +29 -10
  344. package/dist/core/session-store.d.ts +5 -7
  345. package/dist/core/session-store.js +50 -11
  346. package/dist/core/side-query.d.ts +11 -1
  347. package/dist/core/side-query.js +9 -0
  348. package/dist/core/skill-grant-binding.d.ts +31 -2
  349. package/dist/core/skill-grant-binding.js +31 -1
  350. package/dist/core/skill-grant.d.ts +17 -5
  351. package/dist/core/skill-grant.js +24 -6
  352. package/dist/core/store-contracts/background-agent-store-contract.js +27 -0
  353. package/dist/core/store-contracts/checkpoint-store-contract.js +3 -2
  354. package/dist/core/store-contracts/mailbox-store-contract.js +31 -1
  355. package/dist/core/store-contracts/tool-result-store-contract.d.ts +1 -1
  356. package/dist/core/store-contracts/tool-result-store-contract.js +37 -0
  357. package/dist/core/stub-env.d.ts +5 -5
  358. package/dist/core/stub-env.js +11 -1
  359. package/dist/core/swappable-deps.d.ts +17 -43
  360. package/dist/core/swappable-deps.js +15 -5
  361. package/dist/core/synthetic-read-verdict.d.ts +1 -1
  362. package/dist/core/synthetic-read-verdict.js +2 -1
  363. package/dist/core/task-event.d.ts +50 -10
  364. package/dist/core/task-notification.d.ts +88 -5
  365. package/dist/core/task-notification.js +146 -17
  366. package/dist/core/task-registry-agent.d.ts +18 -12
  367. package/dist/core/task-registry-agent.js +19 -6
  368. package/dist/core/task-registry-monitor.d.ts +7 -0
  369. package/dist/core/task-registry-monitor.js +28 -1
  370. package/dist/core/task-registry-shared.d.ts +43 -3
  371. package/dist/core/task-registry-shared.js +21 -7
  372. package/dist/core/task-registry-workflow.js +1 -1
  373. package/dist/core/task-registry.d.ts +50 -35
  374. package/dist/core/task-registry.js +64 -23
  375. package/dist/core/task-result.d.ts +3 -12
  376. package/dist/core/task-spec.d.ts +15 -7
  377. package/dist/core/task-stream.d.ts +1 -1
  378. package/dist/core/terminal-cause.d.ts +10 -0
  379. package/dist/core/tool-catalog-entries.js +1 -1
  380. package/dist/core/tool-check-reading.d.ts +1 -0
  381. package/dist/core/tool-check-reading.js +3 -3
  382. package/dist/core/tool-face-reader.d.ts +7 -6
  383. package/dist/core/tool-face-reader.js +4 -3
  384. package/dist/core/tool-name-aliases.d.ts +19 -0
  385. package/dist/core/tool-name-aliases.js +54 -2
  386. package/dist/core/tool-policy.d.ts +21 -4
  387. package/dist/core/tool-policy.js +1 -1
  388. package/dist/core/tool-registry.d.ts +5 -5
  389. package/dist/core/tool-registry.js +2 -2
  390. package/dist/core/tool-result-store.d.ts +88 -5
  391. package/dist/core/tool-result-store.js +51 -12
  392. package/dist/core/tool-roster.d.ts +112 -5
  393. package/dist/core/tool-roster.js +42 -1
  394. package/dist/core/tool-spec.d.ts +23 -5
  395. package/dist/core/trace.d.ts +0 -1
  396. package/dist/core/types.d.ts +5 -5
  397. package/dist/core/types.js +2 -2
  398. package/dist/core/untrusted-text.js +1 -1
  399. package/dist/core/user-attachment.d.ts +10 -5
  400. package/dist/core/user-attachment.js +15 -7
  401. package/dist/core/wiring-manifest.d.ts +6 -3
  402. package/dist/engine/compaction/compaction.js +29 -3
  403. package/dist/engine/execution-env/node-execution-env.d.ts +11 -5
  404. package/dist/engine/execution-env/node-execution-env.js +21 -13
  405. package/dist/engine/harness/agent-harness.d.ts +26 -3
  406. package/dist/engine/harness/agent-harness.js +49 -16
  407. package/dist/engine/harness/messages.d.ts +3 -1
  408. package/dist/engine/harness/messages.js +3 -2
  409. package/dist/engine/harness/types.d.ts +119 -28
  410. package/dist/engine/llm/rejected-images.d.ts +46 -0
  411. package/dist/engine/llm/rejected-images.js +54 -0
  412. package/dist/engine/llm/tool-turns.d.ts +2 -1
  413. package/dist/engine/llm/types.d.ts +18 -7
  414. package/dist/engine/loop/agent-loop.d.ts +20 -12
  415. package/dist/engine/loop/agent-loop.js +205 -36
  416. package/dist/engine/loop/types.d.ts +12 -3
  417. package/dist/engine/lsp/node-lsp-manager.d.ts +3 -3
  418. package/dist/engine/lsp/node-lsp-manager.js +32 -13
  419. package/dist/engine/session/path-to-root.d.ts +38 -0
  420. package/dist/engine/session/path-to-root.js +39 -0
  421. package/dist/engine/session/storage-base.js +4 -21
  422. package/dist/index.d.ts +35 -21
  423. package/dist/index.js +26 -17
  424. package/dist/internal/harness-types.d.ts +1 -1
  425. package/dist/internal/harness.d.ts +1 -0
  426. package/dist/internal/llm.d.ts +1 -0
  427. package/dist/orchestration/run-workflow-tool.d.ts +9 -3
  428. package/dist/orchestration/run-workflow-tool.js +59 -29
  429. package/dist/orchestration/workflow-governance.d.ts +1 -1
  430. package/dist/orchestration/workflow-governance.js +2 -1
  431. package/dist/orchestration/workflow-observe.d.ts +17 -1
  432. package/dist/orchestration/workflow-observe.js +20 -0
  433. package/dist/orchestration/workflow-types.d.ts +32 -7
  434. package/dist/orchestration/workflow.d.ts +4 -0
  435. package/dist/orchestration/workflow.js +762 -491
  436. package/dist/prompts/default.d.ts +1 -10
  437. package/dist/prompts/default.js +0 -2
  438. package/dist/shell/counting-pass.d.ts +50 -0
  439. package/dist/shell/counting-pass.js +502 -0
  440. package/dist/shell/lex.d.ts +4 -2
  441. package/dist/shell/lex.js +93 -34
  442. package/dist/shell/shell-words.d.ts +1 -1
  443. package/dist/shell/shell-words.js +7 -6
  444. package/dist/stores/file/background-agent-store.d.ts +1 -1
  445. package/dist/stores/file/background-agent-store.js +3 -1
  446. package/dist/stores/file/checkpoint-store.d.ts +127 -2
  447. package/dist/stores/file/checkpoint-store.js +19 -14
  448. package/dist/stores/file/file-history-store.d.ts +1 -1
  449. package/dist/stores/file/file-history-store.js +3 -1
  450. package/dist/stores/file/index.d.ts +4 -3
  451. package/dist/stores/file/index.js +17 -8
  452. package/dist/stores/file/mailbox-store.d.ts +1 -1
  453. package/dist/stores/file/mailbox-store.js +9 -11
  454. package/dist/stores/file/memory-store.d.ts +1 -1
  455. package/dist/stores/file/memory-store.js +3 -1
  456. package/dist/stores/file/session-policy-store.d.ts +1 -1
  457. package/dist/stores/file/session-policy-store.js +3 -1
  458. package/dist/stores/file/session-store.d.ts +1 -1
  459. package/dist/stores/file/session-store.js +3 -1
  460. package/dist/stores/file/shared-ledger.d.ts +26 -0
  461. package/dist/stores/file/shared-ledger.js +38 -9
  462. package/dist/stores/file/tool-result-store.d.ts +20 -2
  463. package/dist/stores/file/tool-result-store.js +74 -6
  464. package/dist/stores/file/usage-window-store.d.ts +1 -1
  465. package/dist/stores/file/usage-window-store.js +3 -1
  466. package/dist/stores/file/workflow-journal-store.d.ts +1 -11
  467. package/dist/stores/file/workflow-journal-store.js +3 -2
  468. package/dist/tools/fs/containment-face.d.ts +5 -0
  469. package/dist/tools/fs/encoding.d.ts +13 -4
  470. package/dist/tools/fs/encoding.js +17 -2
  471. package/dist/tools/fs/fs-bash-background-text.d.ts +7 -0
  472. package/dist/tools/fs/fs-bash-background-text.js +1 -0
  473. package/dist/tools/fs/fs-bash.d.ts +14 -4
  474. package/dist/tools/fs/fs-bash.js +29 -35
  475. package/dist/tools/fs/fs-pdf.d.ts +2 -2
  476. package/dist/tools/fs/fs-pdf.js +26 -21
  477. package/dist/tools/fs/fs-read.d.ts +5 -4
  478. package/dist/tools/fs/fs-read.js +35 -18
  479. package/dist/tools/fs/fs-search-tools.js +4 -1
  480. package/dist/tools/fs/fs-shared.d.ts +11 -13
  481. package/dist/tools/fs/fs-shared.js +11 -7
  482. package/dist/tools/fs/fs-write.js +5 -5
  483. package/dist/tools/fs/index.d.ts +14 -8
  484. package/dist/tools/fs/index.js +13 -5
  485. package/dist/tools/fs/program-read-model.d.ts +13 -0
  486. package/dist/tools/fs/program-read-model.js +15 -0
  487. package/dist/tools/fs/program-read-model.rows.js +1 -1
  488. package/dist/tools/fs/read-deny.d.ts +13 -10
  489. package/dist/tools/fs/read-deny.js +25 -5
  490. package/dist/tools/fs/read-floor.d.ts +62 -0
  491. package/dist/tools/fs/read-floor.js +58 -0
  492. package/dist/tools/fs/removal-targets.d.ts +9 -4
  493. package/dist/tools/fs/removal-targets.js +17 -9
  494. package/dist/tools/fs/search.d.ts +3 -3
  495. package/dist/tools/fs/search.js +4 -4
  496. package/dist/tools/fs/shell-read-check.d.ts +1 -1
  497. package/dist/tools/fs/shell-read-check.js +2 -1
  498. package/dist/tools/fs/shell-read-judge.d.ts +16 -8
  499. package/dist/tools/fs/shell-read-judge.js +10 -6
  500. package/dist/tools/fs/shell-read-plan.d.ts +6 -1
  501. package/dist/tools/fs/shell-read-plan.js +22 -15
  502. package/dist/tools/fs/shell-read-start.js +1 -0
  503. package/dist/tools/fs/shell-reentry-text.d.ts +15 -1
  504. package/dist/tools/fs/shell-reentry-text.js +25 -3
  505. package/dist/tools/monitor.d.ts +9 -4
  506. package/dist/tools/monitor.js +17 -8
  507. package/dist/tools/scheduler-tools.js +42 -4
  508. package/dist/tools/web.d.ts +0 -10
  509. package/dist/tools/web.js +1 -4
  510. package/package.json +5 -2
  511. package/test/export-surface.snapshot.json +161 -21
  512. package/wire-changes/core-stores-sql@7.59.0.json +29 -0
  513. package/wire-changes/core-stores-sql@7.60.0.json +30 -0
  514. package/wire-changes/core@7.59.0.json +2459 -0
  515. package/wire-changes/core@7.60.0.json +3274 -0
  516. package/wire-changes/mods-kernel@7.59.0.json +29 -0
  517. package/wire-changes/mods-kernel@7.60.0.json +62 -0
  518. package/wire-changes/primitives@7.59.0.json +29 -0
  519. package/wire-changes/primitives@7.60.0.json +30 -0
@@ -1,73 +1,92 @@
1
- /**
2
- * The ONE admission an image's bytes pass before they reach the model as an `ImageContent` block —
3
- * whether a file supplied them (the Read tool's image lane), a caller did (`TaskSpec.images`' inline
4
- * arm, a user attachment) or a tool returned them (the tool-result seat below). Four steps, in this order:
5
- *
6
- * ① the claimed type is one the content API accepts (png / jpeg / gif / webp — the four
7
- * {@link imageMagicMatches} can verify);
8
- * ② the raw bytes are within the read cap (5 MiB without a downsampler, 64 MiB with one);
9
- * ③ the bytes carry the claimed type's signature — an empty / text / mis-named "png" would
10
- * otherwise become a malformed block that 400s the NEXT whole provider request;
11
- * ④ optional downsample (when a downsampler is supplied; a processing failure keeps the original
12
- * bytes, CC's catch → createImageResponse(original)), then the provider's hard limit, which is on the
13
- * BASE64 length, not raw bytes.
14
- *
15
- * The answer is structured; each carrier words its own refusal (the Read tool speaks to the model, the
16
- * task door to the caller). The block it admits is built from the bytes it checked — never from a
17
- * caller's string it did not decode. A document's bytes pass the same shape of admission
18
- * ({@link admitDocumentBytes}: `application/pdf`, the whole-send cap, the `%PDF-` signature).
19
- *
20
- * THE TOOL-RESULT SEAT ({@link admitToolResultMedia}): every executed call's result passes the loop's one
21
- * post-execution half (`finalizeToolOutcome`) — an engine tool's, an MCP tool's converted content, a host
22
- * tool's own return, a resumed approved call's, and whatever a `tool_result` hook put in their place — so
23
- * the media blocks of every supply are admitted there, once, after the last hand that can change them and
24
- * before the frame and the transcript read them. A block whose bytes the admission refuses becomes a text
25
- * block that names the tool, the refusal and the size — never the bytes, and never silently nothing.
26
- */
27
1
  import type { DocumentContent, ImageContent } from "../internal/llm.js";
28
- import { type ImageDimensions, type ImageDownsampler } from "./image-downsample.js";
2
+ import type { ImageDimensions, ImageDownsampler } from "./image-downsample.js";
29
3
  import { type MediaBlock } from "./media-byte-cap.js";
30
- /** Byte cap on a Read-image WITHOUT a downsampler: an image file is returned AS a visual
31
- * `ImageContent` block (base64) rather than refused. Bounded so a huge image cannot blow the request.
32
- * This stat-time cap only bounds the READ; the accept/refuse decision is the base64 gate below (the
33
- * provider limit is on base64 LENGTH, so the effective no-sharp accept bound is raw ≤ 3.75MB). */
34
- export declare const MAX_IMAGE_READ_BYTES: number;
35
- /** Byte cap on a Read-image WITH a downsampler: sharp can shrink a big photo to CC's 2000×2000/3.75MB
36
- * target, so the smaller hard refusal does not apply (CC readImageWithTokenBudget reads the file
37
- * unbounded; we keep an OOM sanity bound — deliberate delta, camera photos fit comfortably). */
38
- export declare const MAX_IMAGE_DOWNSAMPLE_INPUT_BYTES: number;
39
4
  /** Max raw size sent whole as a document block (CC `PDF_TARGET_RAW_SIZE`). */
40
5
  export declare const PDF_TARGET_RAW_SIZE: number;
41
- /** Verify the raw bytes actually ARE the image format the extension claims (magic-number sniff). Extension alone
42
- * is forgeable: a `.png` holding text / an empty file / a truncated blob would otherwise become a malformed
43
- * `ImageContent` that makes the NEXT provider request fail (400). Pure — no I/O. */
6
+ /**
7
+ * Every number an image passes on its way to the model, in one table — the admission and its processor read it, and so
8
+ * does every carrier that words a refusal; a downstream that bounds an image by the same limit reads the same field.
9
+ * - `hard`: what refuses. `formats` (the header rows above), `readBytes` (the raw read cap — larger when a processor can
10
+ * shrink what was read; the 64 MiB bound is an OOM sanity bound, CC reads the file whole), `urlBytes` (a fetched
11
+ * image's body), `rejectEdge` (the longest edge the bytes that ship may state — `fI` (cli309.js:253044), the 2000 box,
12
+ * its no-flag reject width), `imageBase64Chars` (the provider's single-image limit, on the base64 length — CC `fI`
13
+ * `maxBase64Size`).
14
+ * - `soft`: what the processor aims at, best effort. `frameEdge` and `rawBytes` are the frame ladder's box and byte rung
15
+ * (CC `fI` `maxWidth` / `targetRawSize`: 3.75 MB raw = 5 MiB base64); `targetBytes` is the byte-target step's goal
16
+ * (`SZ` (cli309.js:253044), the bytes `N$o` compresses toward).
17
+ * The request's media budget is not a row of this table yet.
18
+ */
19
+ export declare const IMAGE_ADMISSION: Readonly<{
20
+ readonly hard: Readonly<{
21
+ formats: Readonly<Record<"image/jpeg" | "image/png" | "image/gif" | "image/webp", (b: Uint8Array) => boolean>>;
22
+ readBytes: Readonly<{
23
+ withoutProcessor: number;
24
+ withProcessor: number;
25
+ }>;
26
+ urlBytes: number;
27
+ rejectEdge: 2000;
28
+ imageBase64Chars: number;
29
+ }>;
30
+ readonly soft: Readonly<{
31
+ frameEdge: 2000;
32
+ rawBytes: number;
33
+ targetBytes: 512000;
34
+ }>;
35
+ }>;
36
+ /** Verify the raw bytes actually ARE the image format the claimed type names (`IMAGE_ADMISSION.hard.formats`). Extension
37
+ * alone is forgeable: a `.png` holding text / an empty file / a truncated blob / an Apple-optimized PNG would otherwise
38
+ * become a malformed `ImageContent` that makes the NEXT provider request fail (400). Pure — no I/O. */
44
39
  export declare function imageMagicMatches(bytes: Uint8Array, mimeType: string): boolean;
40
+ /**
41
+ * The pixel size an image's own header states, read without decoding (`WN` (cli309.js:257745)): PNG's `IHDR`, GIF's screen
42
+ * descriptor, a JPEG's first start-of-frame segment, WebP's `VP8 ` / `VP8L` / `VP8X` header. `undefined` when the header
43
+ * states none this reader finds. Pure — the admission reads it for the bytes that ship.
44
+ */
45
+ export declare function readImageHeaderDimensions(b: Uint8Array): {
46
+ width: number;
47
+ height: number;
48
+ } | undefined;
45
49
  /** True when the bytes start with the `%PDF-` magic. Magic outranks extension both ways: a mis-named
46
50
  * `.pdf` without it is refused, and a non-`.pdf` file carrying it is read AS a PDF. */
47
51
  export declare function pdfMagicMatches(bytes: Uint8Array): boolean;
48
52
  /** The image types the content API accepts — the only ones a signature check can verify. */
49
53
  export declare const ADMITTED_IMAGE_MIME_TYPES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp"];
50
- /** The raw-byte read cap for an image, with or without a downsampler. */
51
- export declare function imageReadCap(downsampler: ImageDownsampler | undefined): number;
54
+ /** The raw-byte read cap for an image, with or without a processor (`IMAGE_ADMISSION.hard.readBytes`). */
55
+ export declare function imageReadCap(processor: ImageDownsampler | "none"): number;
52
56
  export type ImageAdmission = {
53
57
  ok: true;
54
58
  block: ImageContent;
55
- /** The raw byte count of the bytes checked (before any downsample). */
59
+ /** The raw byte count of the bytes checked (before any processing). */
56
60
  originalSize: number;
57
- /** Measured by the downsampler; absent without one (never fabricated). */
61
+ /** Measured by the processor; absent when the bytes went on as they are (never fabricated). */
58
62
  dimensions?: ImageDimensions;
59
- /** True when the downsampler actually changed the pixel size. */
63
+ /** True when the processor actually changed the pixel size. */
60
64
  resized: boolean;
61
65
  } | {
62
66
  ok: false;
63
67
  reason: "mime_unsupported" | "magic_mismatch";
64
68
  rawBytes: number;
69
+ }
70
+ /** The bytes that would ship state no pixel size in their header. */
71
+ | {
72
+ ok: false;
73
+ reason: "dimensions_unreadable";
74
+ rawBytes: number;
65
75
  } | {
66
76
  ok: false;
67
77
  reason: "raw_too_large";
68
78
  rawBytes: number;
69
79
  cap: number;
70
80
  }
81
+ /** The bytes that would ship state an edge over `IMAGE_ADMISSION.hard.rejectEdge` (`limit`). */
82
+ | {
83
+ ok: false;
84
+ reason: "dimensions_too_large";
85
+ rawBytes: number;
86
+ width: number;
87
+ height: number;
88
+ limit: number;
89
+ }
71
90
  /** `base64` is the encoding that failed the limit (the caller words the refusal from its length). */
72
91
  | {
73
92
  ok: false;
@@ -75,7 +94,21 @@ export type ImageAdmission = {
75
94
  rawBytes: number;
76
95
  base64: string;
77
96
  };
78
- export declare function admitImageBytes(bytes: Uint8Array, claimedMime: string, downsampler: ImageDownsampler | undefined): Promise<ImageAdmission>;
97
+ /** Why the bytes that would have shipped were refused on their stated size (`dimensions_unreadable` / `dimensions_too_large`)
98
+ * — the one wording every carrier's refusal and the tool-result seat's placeholder read. */
99
+ export declare function unprocessedSizeClause(a: {
100
+ reason: "dimensions_unreadable";
101
+ } | {
102
+ reason: "dimensions_too_large";
103
+ width: number;
104
+ height: number;
105
+ limit: number;
106
+ }): string;
107
+ /** One image's bytes through the admission (see the module head). `processor` is the run's, resolved from its one seat
108
+ * (`resolveImageProcessor`), or `"none"`: it is never optional, so no carrier admits without saying which. */
109
+ export declare function admitImageBytes(bytes: Uint8Array, claimedMime: string, processor: ImageDownsampler | "none"): Promise<ImageAdmission>;
110
+ /** The admitted type the bytes' own header names (the first of the four header rows they open with), or `undefined`. */
111
+ export declare function sniffImageMime(bytes: Uint8Array): (typeof ADMITTED_IMAGE_MIME_TYPES)[number] | undefined;
79
112
  /** The document types the content API accepts — the one a signature check can verify. */
80
113
  export declare const ADMITTED_DOCUMENT_MIME_TYPES: readonly ["application/pdf"];
81
114
  export type DocumentAdmission = {
@@ -97,14 +130,17 @@ export type DocumentAdmission = {
97
130
  export declare function admitDocumentBytes(bytes: Uint8Array, claimedMime: string): DocumentAdmission;
98
131
  /** Why the tool-result seat refused a media block: the admissions' own words (an image's four — a document's are among
99
132
  * them), or `undecodable` — the block carried no base64 string to decode. */
100
- export type MediaRefusalReason = "undecodable" | "mime_unsupported" | "raw_too_large" | "magic_mismatch" | "base64_too_large";
133
+ export type MediaRefusalReason = "undecodable" | "mime_unsupported" | "raw_too_large" | "magic_mismatch" | "dimensions_unreadable" | "dimensions_too_large" | "base64_too_large";
101
134
  /** One media block the seat refused: its type, the refusal, its decoded size, the bound it exceeded (the raw-byte cap for
102
- * `raw_too_large`, the base64 length limit for `base64_too_large`), and its media type when it can be one. */
135
+ * `raw_too_large`, the reject edge for `dimensions_too_large` — with the size its header states —, the base64 length
136
+ * limit for `base64_too_large`), and its media type when it can be one. */
103
137
  export interface ToolResultMediaRefusal {
104
138
  type: MediaBlock["type"];
105
139
  reason: MediaRefusalReason;
106
140
  bytes: number;
107
141
  limit?: number;
142
+ width?: number;
143
+ height?: number;
108
144
  mimeType?: string;
109
145
  }
110
146
  /** What the seat made of one result's content: every media block rebuilt from its checked bytes or replaced, in its slot,
@@ -114,9 +150,27 @@ export interface ToolResultMediaAdmission {
114
150
  refused: ToolResultMediaRefusal[];
115
151
  }
116
152
  /**
117
- * The tool-result seat's admission over one executed call's content: `undefined` when no block claims a media type (the
118
- * content stands as it is, and nothing is awaited); otherwise every block is read once into a new array, each media
119
- * block admitted — rebuilt from the bytes it checked, or replaced in its slot by a placeholder naming `toolName` and the
120
- * refusal. Total: a content whose blocks cannot be read is not media this seat can admit or refuse, and is left as it is.
153
+ * The decoded size of the first image block that goes to the model over `IMAGE_ADMISSION.soft.targetBytes` with no
154
+ * processor to bring it down — the one fact the run's operator notice reads (`config.image_processor_unavailable`), at
155
+ * every place images enter a run (the tool-result seat, the task's images, a person's attachments). `undefined` with a
156
+ * processor, or when none does.
157
+ */
158
+ export declare function firstUnprocessedOverTarget(blocks: readonly unknown[], processor: ImageDownsampler | "none"): number | undefined;
159
+ /**
160
+ * The tool-result seat's one read of an executed call's content: its blocks, read once into a new array, when one claims a
161
+ * media type; `undefined` otherwise (the content stands as it is). Synchronous and total — a content whose blocks cannot be
162
+ * read is not media this seat can admit or refuse. A carrier resolves the run's processor only once this answers blocks,
163
+ * so a call with no media in its result awaits nothing.
164
+ */
165
+ export declare function toolResultMediaBlocks(content: unknown): unknown[] | undefined;
166
+ /**
167
+ * The tool-result seat's admission over blocks {@link toolResultMediaBlocks} read: each media block admitted with the run's
168
+ * `processor` — rebuilt from the bytes it checked (an image a carrier already brought within the target and the frame passes
169
+ * unchanged), or replaced in its slot by a placeholder naming `toolName` and the refusal.
170
+ */
171
+ export declare function admitToolResultMediaBlocks(blocks: readonly unknown[], toolName: string, processor: ImageDownsampler | "none"): Promise<ToolResultMediaAdmission>;
172
+ /**
173
+ * The tool-result seat over one executed call's content with a processor in hand: {@link toolResultMediaBlocks}, then
174
+ * {@link admitToolResultMediaBlocks} — `undefined` when no block claims a media type (nothing is awaited).
121
175
  */
122
- export declare function admitToolResultMedia(content: unknown, toolName: string): Promise<ToolResultMediaAdmission> | undefined;
176
+ export declare function admitToolResultMedia(content: unknown, toolName: string, processor: ImageDownsampler | "none"): Promise<ToolResultMediaAdmission> | undefined;
@@ -1,60 +1,167 @@
1
- import { MCP_IMAGE_MAX_BASE64 } from "./image-downsample.js";
1
+ import { createHash } from "node:crypto";
2
2
  import { claimedMediaType } from "./media-byte-cap.js";
3
3
  import { readMember } from "./thrown-value.js";
4
- export const MAX_IMAGE_READ_BYTES = 5 * 1024 * 1024;
5
- export const MAX_IMAGE_DOWNSAMPLE_INPUT_BYTES = 64 * 1024 * 1024;
6
4
  export const PDF_TARGET_RAW_SIZE = 20 * 1024 * 1024;
7
- export function imageMagicMatches(bytes, mimeType) {
8
- const b = bytes;
9
- switch (mimeType) {
10
- case "image/png":
11
- return b.length >= 8 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47 && b[4] === 0x0d && b[5] === 0x0a && b[6] === 0x1a && b[7] === 0x0a;
12
- case "image/jpeg":
13
- return b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff;
14
- case "image/gif":
15
- return b.length >= 6 && b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x38 && (b[4] === 0x37 || b[4] === 0x39) && b[5] === 0x61;
16
- case "image/webp":
17
- return b.length >= 12 && b[0] === 0x52 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x46 && b[8] === 0x57 && b[9] === 0x45 && b[10] === 0x42 && b[11] === 0x50;
18
- default:
5
+ const spells = (b, at, text) => {
6
+ if (b.length < at + text.length)
7
+ return false;
8
+ for (let i = 0; i < text.length; i++)
9
+ if (b[at + i] !== text.charCodeAt(i))
19
10
  return false;
11
+ return true;
12
+ };
13
+ const u32be = (b, at) => ((b[at] << 24) >>> 0) + (b[at + 1] << 16) + (b[at + 2] << 8) + b[at + 3];
14
+ const u16le = (b, at) => b[at] + (b[at + 1] << 8);
15
+ const u16be = (b, at) => (b[at] << 8) + b[at + 1];
16
+ const u24le = (b, at) => b[at] + (b[at + 1] << 8) + (b[at + 2] << 16);
17
+ const u32le = (b, at) => (u24le(b, at) + (b[at + 3] << 24)) >>> 0;
18
+ const IMAGE_HEADER_CHECKS = Object.freeze({
19
+ "image/png": (b) => b.length >= 24 && spells(b, 0, "\x89PNG\r\n\x1a\n") && u32be(b, 8) === 13 && spells(b, 12, "IHDR") && u32be(b, 16) > 0 && u32be(b, 20) > 0,
20
+ "image/jpeg": (b) => b.length >= 4 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff && b[3] >= 0xc0,
21
+ "image/gif": (b) => b.length >= 13 && (spells(b, 0, "GIF87a") || spells(b, 0, "GIF89a")) && u16le(b, 6) > 0 && u16le(b, 8) > 0,
22
+ "image/webp": (b) => b.length >= 16 && spells(b, 0, "RIFF") && spells(b, 8, "WEBP") && (spells(b, 12, "VP8 ") || spells(b, 12, "VP8L") || spells(b, 12, "VP8X")),
23
+ });
24
+ const MiB = 1024 * 1024;
25
+ export const IMAGE_ADMISSION = Object.freeze({
26
+ hard: Object.freeze({
27
+ formats: IMAGE_HEADER_CHECKS,
28
+ readBytes: Object.freeze({ withoutProcessor: 5 * MiB, withProcessor: 64 * MiB }),
29
+ urlBytes: 20 * MiB,
30
+ rejectEdge: 2000,
31
+ imageBase64Chars: 5 * MiB,
32
+ }),
33
+ soft: Object.freeze({ frameEdge: 2000, rawBytes: (5 * MiB * 3) / 4, targetBytes: 512_000 }),
34
+ });
35
+ export function imageMagicMatches(bytes, mimeType) {
36
+ const formats = IMAGE_ADMISSION.hard.formats;
37
+ return Object.hasOwn(formats, mimeType) && formats[mimeType](bytes);
38
+ }
39
+ function jpegFrameAt(b) {
40
+ if (b.length < 4 || b[0] !== 0xff || b[1] !== 0xd8)
41
+ return undefined;
42
+ let at = 2;
43
+ while (at + 9 < b.length) {
44
+ if (b[at] !== 0xff) {
45
+ at++;
46
+ continue;
47
+ }
48
+ const marker = b[at + 1];
49
+ if (marker === 0xff) {
50
+ at++;
51
+ continue;
52
+ }
53
+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc)
54
+ return at;
55
+ if ((marker >= 0xd0 && marker <= 0xd9) || marker === 0x01) {
56
+ at += 2;
57
+ continue;
58
+ }
59
+ const length = u16be(b, at + 2);
60
+ if (length < 2)
61
+ return undefined;
62
+ at += 2 + length;
63
+ }
64
+ return undefined;
65
+ }
66
+ export function readImageHeaderDimensions(b) {
67
+ if (b.length < 10)
68
+ return undefined;
69
+ if (b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47)
70
+ return b.length >= 24 ? { width: u32be(b, 16), height: u32be(b, 20) } : undefined;
71
+ if (b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46)
72
+ return { width: u16le(b, 6), height: u16le(b, 8) };
73
+ const frame = jpegFrameAt(b);
74
+ if (frame !== undefined)
75
+ return { height: u16be(b, frame + 5), width: u16be(b, frame + 7) };
76
+ if (b[0] === 0xff && b[1] === 0xd8)
77
+ return undefined;
78
+ if (b.length >= 30 && spells(b, 0, "RIFF") && spells(b, 8, "WEBP")) {
79
+ if (spells(b, 12, "VP8 "))
80
+ return { width: u16le(b, 26) & 0x3fff, height: u16le(b, 28) & 0x3fff };
81
+ if (spells(b, 12, "VP8L")) {
82
+ const bits = u32le(b, 21);
83
+ return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
84
+ }
85
+ if (spells(b, 12, "VP8X"))
86
+ return { width: u24le(b, 24) + 1, height: u24le(b, 27) + 1 };
20
87
  }
88
+ return undefined;
21
89
  }
22
90
  export function pdfMagicMatches(bytes) {
23
91
  return bytes.length >= 5 && bytes[0] === 0x25 && bytes[1] === 0x50 && bytes[2] === 0x44 && bytes[3] === 0x46 && bytes[4] === 0x2d;
24
92
  }
25
93
  export const ADMITTED_IMAGE_MIME_TYPES = Object.freeze(["image/png", "image/jpeg", "image/gif", "image/webp"]);
26
- export function imageReadCap(downsampler) {
27
- return downsampler ? MAX_IMAGE_DOWNSAMPLE_INPUT_BYTES : MAX_IMAGE_READ_BYTES;
94
+ export function imageReadCap(processor) {
95
+ const { readBytes } = IMAGE_ADMISSION.hard;
96
+ return processor === "none" ? readBytes.withoutProcessor : readBytes.withProcessor;
28
97
  }
29
- export async function admitImageBytes(bytes, claimedMime, downsampler) {
98
+ export function unprocessedSizeClause(a) {
99
+ return a.reason === "dimensions_unreadable"
100
+ ? "it could not be processed and its header states no pixel size — convert it to PNG, JPEG, GIF, or WebP"
101
+ : `at ${a.width}x${a.height}px it is over the ${a.limit}x${a.limit}px limit and could not be shrunk — resize it to fit within ${a.limit}x${a.limit}px`;
102
+ }
103
+ export async function admitImageBytes(bytes, claimedMime, processor) {
30
104
  const rawBytes = bytes.byteLength;
31
105
  if (!ADMITTED_IMAGE_MIME_TYPES.includes(claimedMime))
32
106
  return { ok: false, reason: "mime_unsupported", rawBytes };
33
- const cap = imageReadCap(downsampler);
107
+ const cap = imageReadCap(processor);
34
108
  if (rawBytes > cap)
35
109
  return { ok: false, reason: "raw_too_large", rawBytes, cap };
36
110
  if (!imageMagicMatches(bytes, claimedMime))
37
111
  return { ok: false, reason: "magic_mismatch", rawBytes };
38
- let base64;
39
- let mimeType = claimedMime;
40
- let dimensions;
41
- if (downsampler) {
42
- const out = await downsampler(Buffer.from(bytes), claimedMime).catch(() => undefined);
43
- if (out) {
44
- base64 = out.base64;
45
- mimeType = out.mimeType;
46
- dimensions = out.dimensions;
47
- }
112
+ let shipped;
113
+ const memory = processor !== "none" ? producedBy(processor) : undefined;
114
+ const inputDigest = memory !== undefined ? digestOf(bytes) : undefined;
115
+ const known = memory !== undefined && inputDigest !== undefined && memory.has(inputDigest);
116
+ if (processor !== "none" && !known) {
117
+ const out = await Promise.resolve()
118
+ .then(() => processor(Buffer.from(bytes), claimedMime))
119
+ .catch(() => undefined);
120
+ const outBytes = out !== undefined ? Buffer.from(out.base64, "base64") : undefined;
121
+ const outMime = outBytes !== undefined ? sniffImageMime(outBytes) : undefined;
122
+ if (out !== undefined && outBytes !== undefined && outMime !== undefined)
123
+ shipped = { bytes: outBytes, base64: out.base64, mimeType: outMime, ...(out.dimensions !== undefined ? { dimensions: out.dimensions } : {}) };
48
124
  }
49
- base64 ??= Buffer.from(bytes).toString("base64");
50
- if (base64.length > MCP_IMAGE_MAX_BASE64)
125
+ const fromProcessor = shipped !== undefined;
126
+ shipped ??= { bytes, base64: Buffer.from(bytes).toString("base64"), mimeType: claimedMime };
127
+ const header = readImageHeaderDimensions(shipped.bytes);
128
+ if (header === undefined)
129
+ return { ok: false, reason: "dimensions_unreadable", rawBytes };
130
+ const limit = IMAGE_ADMISSION.hard.rejectEdge;
131
+ if (header.width > limit || header.height > limit)
132
+ return { ok: false, reason: "dimensions_too_large", rawBytes, width: header.width, height: header.height, limit };
133
+ const { base64, mimeType, dimensions } = shipped;
134
+ if (base64.length > IMAGE_ADMISSION.hard.imageBase64Chars)
51
135
  return { ok: false, reason: "base64_too_large", rawBytes, base64 };
52
136
  const resized = dimensions !== undefined &&
53
137
  dimensions.displayWidth > 0 &&
54
138
  dimensions.displayHeight > 0 &&
55
139
  (dimensions.originalWidth !== dimensions.displayWidth || dimensions.originalHeight !== dimensions.displayHeight);
140
+ if (memory !== undefined && (fromProcessor || known))
141
+ memory.add(fromProcessor ? digestOf(shipped.bytes) : inputDigest);
56
142
  return { ok: true, block: { type: "image", data: base64, mimeType }, originalSize: rawBytes, ...(dimensions !== undefined ? { dimensions } : {}), resized };
57
143
  }
144
+ const PRODUCED_KEPT = 256;
145
+ const produced = new WeakMap();
146
+ function producedBy(processor) {
147
+ let kept = produced.get(processor);
148
+ if (kept === undefined)
149
+ produced.set(processor, (kept = new Set()));
150
+ const set = kept;
151
+ return {
152
+ has: (digest) => set.has(digest),
153
+ add: (digest) => {
154
+ set.delete(digest);
155
+ set.add(digest);
156
+ if (set.size > PRODUCED_KEPT)
157
+ set.delete(set.values().next().value);
158
+ },
159
+ };
160
+ }
161
+ const digestOf = (bytes) => createHash("sha256").update(bytes).digest("base64");
162
+ export function sniffImageMime(bytes) {
163
+ return ADMITTED_IMAGE_MIME_TYPES.find((mime) => imageMagicMatches(bytes, mime));
164
+ }
58
165
  export const ADMITTED_DOCUMENT_MIME_TYPES = Object.freeze(["application/pdf"]);
59
166
  export function admitDocumentBytes(bytes, claimedMime) {
60
167
  const rawBytes = bytes.byteLength;
@@ -72,7 +179,9 @@ const REFUSAL_CLAUSE = {
72
179
  undecodable: () => "it carried no base64 payload",
73
180
  mime_unsupported: (r) => `${r.mimeType ?? "its media type"} is not a ${r.type} type the model accepts (${ADMITTED_FOR[r.type].join(", ")})`,
74
181
  raw_too_large: (r) => `its ${r.bytes} bytes exceed the ${r.limit}-byte cap`,
75
- magic_mismatch: (r) => `its ${r.bytes} bytes do not carry the ${r.mimeType} signature`,
182
+ magic_mismatch: (r) => `its ${r.bytes} bytes do not open with a valid ${r.mimeType ?? `${r.type}`} header`,
183
+ dimensions_unreadable: () => unprocessedSizeClause({ reason: "dimensions_unreadable" }),
184
+ dimensions_too_large: (r) => unprocessedSizeClause({ reason: "dimensions_too_large", width: r.width, height: r.height, limit: r.limit }),
76
185
  base64_too_large: (r) => `its base64 encoding exceeds the ${r.limit}-character provider limit (${r.bytes} bytes)`,
77
186
  };
78
187
  const placeholderOf = (toolName, r) => ({
@@ -80,15 +189,15 @@ const placeholderOf = (toolName, r) => ({
80
189
  text: `[${r.type} from the ${JSON.stringify(toolName)} tool not shown: ${REFUSAL_CLAUSE[r.reason](r)} (media admission: ${r.reason})]`,
81
190
  });
82
191
  const claimsMedia = (block) => claimedMediaType(block) !== undefined;
83
- async function admitBlock(block) {
192
+ async function admitBlock(block, processor) {
84
193
  const type = claimedMediaType(block);
85
194
  if (type === undefined)
86
195
  return { block };
87
196
  const data = readMember(block, "data");
88
197
  const mime = readMember(block, "mimeType");
89
198
  const echo = mediaTypeEcho(mime);
90
- const refusal = (reason, bytes, limit) => ({
91
- refusal: { type, reason, bytes, ...(limit !== undefined ? { limit } : {}), ...(echo !== undefined ? { mimeType: echo } : {}) },
199
+ const refusal = (reason, bytes, limit, size) => ({
200
+ refusal: { type, reason, bytes, ...(limit !== undefined ? { limit } : {}), ...(size ?? {}), ...(echo !== undefined ? { mimeType: echo } : {}) },
92
201
  });
93
202
  if (typeof data !== "string")
94
203
  return refusal("undecodable", 0);
@@ -96,7 +205,7 @@ async function admitBlock(block) {
96
205
  try {
97
206
  const bytes = Buffer.from(data, "base64");
98
207
  const claimed = typeof mime === "string" ? mime : "";
99
- a = type === "image" ? await admitImageBytes(bytes, claimed, undefined) : admitDocumentBytes(bytes, claimed);
208
+ a = type === "image" ? await admitImageBytes(bytes, claimed, processor) : admitDocumentBytes(bytes, claimed);
100
209
  }
101
210
  catch {
102
211
  return refusal("undecodable", 0);
@@ -105,35 +214,54 @@ async function admitBlock(block) {
105
214
  return { block: a.block };
106
215
  if (a.reason === "raw_too_large")
107
216
  return refusal(a.reason, a.rawBytes, a.cap);
217
+ if (a.reason === "dimensions_too_large")
218
+ return refusal(a.reason, a.rawBytes, a.limit, { width: a.width, height: a.height });
108
219
  if (a.reason === "base64_too_large")
109
- return refusal(a.reason, a.rawBytes, MCP_IMAGE_MAX_BASE64);
220
+ return refusal(a.reason, a.rawBytes, IMAGE_ADMISSION.hard.imageBase64Chars);
110
221
  return refusal(a.reason, a.rawBytes);
111
222
  }
112
- export function admitToolResultMedia(content, toolName) {
113
- let blocks;
223
+ export function firstUnprocessedOverTarget(blocks, processor) {
224
+ if (processor !== "none")
225
+ return undefined;
226
+ for (const b of blocks) {
227
+ if (claimedMediaType(b) !== "image")
228
+ continue;
229
+ const data = readMember(b, "data");
230
+ if (typeof data !== "string")
231
+ continue;
232
+ const bytes = Buffer.byteLength(data, "base64");
233
+ if (bytes > IMAGE_ADMISSION.soft.targetBytes)
234
+ return bytes;
235
+ }
236
+ return undefined;
237
+ }
238
+ export function toolResultMediaBlocks(content) {
114
239
  try {
115
240
  if (!Array.isArray(content))
116
241
  return undefined;
117
- blocks = Array.from(content);
118
- if (!blocks.some(claimsMedia))
119
- return undefined;
242
+ const blocks = Array.from(content);
243
+ return blocks.some(claimsMedia) ? blocks : undefined;
120
244
  }
121
245
  catch {
122
246
  return undefined;
123
247
  }
124
- return (async () => {
125
- const refused = [];
126
- const out = [];
127
- for (const b of blocks) {
128
- const r = await admitBlock(b);
129
- if ("block" in r) {
130
- out.push(r.block);
131
- }
132
- else {
133
- refused.push(r.refusal);
134
- out.push(placeholderOf(toolName, r.refusal));
135
- }
248
+ }
249
+ export async function admitToolResultMediaBlocks(blocks, toolName, processor) {
250
+ const refused = [];
251
+ const out = [];
252
+ for (const b of blocks) {
253
+ const r = await admitBlock(b, processor);
254
+ if ("block" in r) {
255
+ out.push(r.block);
136
256
  }
137
- return { content: out, refused };
138
- })();
257
+ else {
258
+ refused.push(r.refusal);
259
+ out.push(placeholderOf(toolName, r.refusal));
260
+ }
261
+ }
262
+ return { content: out, refused };
263
+ }
264
+ export function admitToolResultMedia(content, toolName, processor) {
265
+ const blocks = toolResultMediaBlocks(content);
266
+ return blocks === undefined ? undefined : admitToolResultMediaBlocks(blocks, toolName, processor);
139
267
  }