@gpustack/loopwright 0.0.0-stage → 0.0.3

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 (477) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +356 -2
  3. package/bin/loopwright.js +3 -0
  4. package/node_modules/@loopwright/agent-pi/dist/adapter-v2.d.ts +9 -0
  5. package/node_modules/@loopwright/agent-pi/dist/adapter-v2.js +122 -0
  6. package/node_modules/@loopwright/agent-pi/dist/adapter.d.ts +30 -0
  7. package/node_modules/@loopwright/agent-pi/dist/adapter.js +340 -0
  8. package/node_modules/@loopwright/agent-pi/dist/admission.d.ts +25 -0
  9. package/node_modules/@loopwright/agent-pi/dist/admission.js +65 -0
  10. package/node_modules/@loopwright/agent-pi/dist/artifacts.d.ts +15 -0
  11. package/node_modules/@loopwright/agent-pi/dist/artifacts.js +76 -0
  12. package/node_modules/@loopwright/agent-pi/dist/boundary.d.ts +18 -0
  13. package/node_modules/@loopwright/agent-pi/dist/boundary.js +113 -0
  14. package/node_modules/@loopwright/agent-pi/dist/broker-channel.d.ts +4 -0
  15. package/node_modules/@loopwright/agent-pi/dist/broker-channel.js +121 -0
  16. package/node_modules/@loopwright/agent-pi/dist/candidate.d.ts +96 -0
  17. package/node_modules/@loopwright/agent-pi/dist/candidate.js +1712 -0
  18. package/node_modules/@loopwright/agent-pi/dist/capture.d.ts +39 -0
  19. package/node_modules/@loopwright/agent-pi/dist/capture.js +146 -0
  20. package/node_modules/@loopwright/agent-pi/dist/cas.d.ts +51 -0
  21. package/node_modules/@loopwright/agent-pi/dist/cas.js +836 -0
  22. package/node_modules/@loopwright/agent-pi/dist/catalog-api.d.ts +3 -0
  23. package/node_modules/@loopwright/agent-pi/dist/catalog-api.js +2 -0
  24. package/node_modules/@loopwright/agent-pi/dist/catalog.d.ts +39 -0
  25. package/node_modules/@loopwright/agent-pi/dist/catalog.js +280 -0
  26. package/node_modules/@loopwright/agent-pi/dist/circuit-breaker-limits.d.ts +9 -0
  27. package/node_modules/@loopwright/agent-pi/dist/circuit-breaker-limits.js +12 -0
  28. package/node_modules/@loopwright/agent-pi/dist/containers.d.ts +29 -0
  29. package/node_modules/@loopwright/agent-pi/dist/containers.js +160 -0
  30. package/node_modules/@loopwright/agent-pi/dist/context.d.ts +56 -0
  31. package/node_modules/@loopwright/agent-pi/dist/context.js +351 -0
  32. package/node_modules/@loopwright/agent-pi/dist/daemon.d.ts +8 -0
  33. package/node_modules/@loopwright/agent-pi/dist/daemon.js +52 -0
  34. package/node_modules/@loopwright/agent-pi/dist/deadline.d.ts +6 -0
  35. package/node_modules/@loopwright/agent-pi/dist/deadline.js +18 -0
  36. package/node_modules/@loopwright/agent-pi/dist/decode-capture.d.ts +39 -0
  37. package/node_modules/@loopwright/agent-pi/dist/decode-capture.js +287 -0
  38. package/node_modules/@loopwright/agent-pi/dist/descriptor.d.ts +12 -0
  39. package/node_modules/@loopwright/agent-pi/dist/descriptor.js +17 -0
  40. package/node_modules/@loopwright/agent-pi/dist/docker-absence.d.ts +2 -0
  41. package/node_modules/@loopwright/agent-pi/dist/docker-absence.js +9 -0
  42. package/node_modules/@loopwright/agent-pi/dist/errors.d.ts +7 -0
  43. package/node_modules/@loopwright/agent-pi/dist/errors.js +19 -0
  44. package/node_modules/@loopwright/agent-pi/dist/event-sequence.d.ts +6 -0
  45. package/node_modules/@loopwright/agent-pi/dist/event-sequence.js +64 -0
  46. package/node_modules/@loopwright/agent-pi/dist/events.d.ts +19 -0
  47. package/node_modules/@loopwright/agent-pi/dist/events.js +132 -0
  48. package/node_modules/@loopwright/agent-pi/dist/execution-guard.d.ts +9 -0
  49. package/node_modules/@loopwright/agent-pi/dist/execution-guard.js +25 -0
  50. package/node_modules/@loopwright/agent-pi/dist/execution-lease.d.ts +24 -0
  51. package/node_modules/@loopwright/agent-pi/dist/execution-lease.js +316 -0
  52. package/node_modules/@loopwright/agent-pi/dist/execution.d.ts +32 -0
  53. package/node_modules/@loopwright/agent-pi/dist/execution.js +476 -0
  54. package/node_modules/@loopwright/agent-pi/dist/external-mcp.d.ts +87 -0
  55. package/node_modules/@loopwright/agent-pi/dist/external-mcp.js +526 -0
  56. package/node_modules/@loopwright/agent-pi/dist/factory-bootstrap.d.ts +15 -0
  57. package/node_modules/@loopwright/agent-pi/dist/factory-bootstrap.js +74 -0
  58. package/node_modules/@loopwright/agent-pi/dist/host-session.d.ts +51 -0
  59. package/node_modules/@loopwright/agent-pi/dist/host-session.js +2094 -0
  60. package/node_modules/@loopwright/agent-pi/dist/http-mcp.d.ts +32 -0
  61. package/node_modules/@loopwright/agent-pi/dist/http-mcp.js +353 -0
  62. package/node_modules/@loopwright/agent-pi/dist/index.d.ts +7 -0
  63. package/node_modules/@loopwright/agent-pi/dist/index.js +4 -0
  64. package/node_modules/@loopwright/agent-pi/dist/lifecycle.d.ts +13 -0
  65. package/node_modules/@loopwright/agent-pi/dist/lifecycle.js +11 -0
  66. package/node_modules/@loopwright/agent-pi/dist/mcp-process.d.ts +35 -0
  67. package/node_modules/@loopwright/agent-pi/dist/mcp-process.js +92 -0
  68. package/node_modules/@loopwright/agent-pi/dist/mcp-transport.d.ts +25 -0
  69. package/node_modules/@loopwright/agent-pi/dist/mcp-transport.js +63 -0
  70. package/node_modules/@loopwright/agent-pi/dist/mcp.d.ts +23 -0
  71. package/node_modules/@loopwright/agent-pi/dist/mcp.js +212 -0
  72. package/node_modules/@loopwright/agent-pi/dist/operation.d.ts +5 -0
  73. package/node_modules/@loopwright/agent-pi/dist/operation.js +28 -0
  74. package/node_modules/@loopwright/agent-pi/dist/policy-v2.d.ts +10 -0
  75. package/node_modules/@loopwright/agent-pi/dist/policy-v2.js +64 -0
  76. package/node_modules/@loopwright/agent-pi/dist/policy.d.ts +13 -0
  77. package/node_modules/@loopwright/agent-pi/dist/policy.js +260 -0
  78. package/node_modules/@loopwright/agent-pi/dist/preflight.d.ts +60 -0
  79. package/node_modules/@loopwright/agent-pi/dist/preflight.js +700 -0
  80. package/node_modules/@loopwright/agent-pi/dist/progress-summary.d.ts +1 -0
  81. package/node_modules/@loopwright/agent-pi/dist/progress-summary.js +119 -0
  82. package/node_modules/@loopwright/agent-pi/dist/protocol.d.ts +17 -0
  83. package/node_modules/@loopwright/agent-pi/dist/protocol.js +131 -0
  84. package/node_modules/@loopwright/agent-pi/dist/provider.d.ts +62 -0
  85. package/node_modules/@loopwright/agent-pi/dist/provider.js +217 -0
  86. package/node_modules/@loopwright/agent-pi/dist/receipt.d.ts +3 -0
  87. package/node_modules/@loopwright/agent-pi/dist/receipt.js +64 -0
  88. package/node_modules/@loopwright/agent-pi/dist/recovery.d.ts +6 -0
  89. package/node_modules/@loopwright/agent-pi/dist/recovery.js +188 -0
  90. package/node_modules/@loopwright/agent-pi/dist/resources.d.ts +13 -0
  91. package/node_modules/@loopwright/agent-pi/dist/resources.js +81 -0
  92. package/node_modules/@loopwright/agent-pi/dist/response.d.ts +20 -0
  93. package/node_modules/@loopwright/agent-pi/dist/response.js +259 -0
  94. package/node_modules/@loopwright/agent-pi/dist/runtime-profile.d.ts +6 -0
  95. package/node_modules/@loopwright/agent-pi/dist/runtime-profile.js +37 -0
  96. package/node_modules/@loopwright/agent-pi/dist/sdk-worker.d.ts +14 -0
  97. package/node_modules/@loopwright/agent-pi/dist/sdk-worker.js +633 -0
  98. package/node_modules/@loopwright/agent-pi/dist/sender.d.ts +46 -0
  99. package/node_modules/@loopwright/agent-pi/dist/sender.js +553 -0
  100. package/node_modules/@loopwright/agent-pi/dist/session-host.d.ts +21 -0
  101. package/node_modules/@loopwright/agent-pi/dist/session-host.js +315 -0
  102. package/node_modules/@loopwright/agent-pi/dist/snapshot-transfer.d.ts +14 -0
  103. package/node_modules/@loopwright/agent-pi/dist/snapshot-transfer.js +102 -0
  104. package/node_modules/@loopwright/agent-pi/dist/snapshot.d.ts +19 -0
  105. package/node_modules/@loopwright/agent-pi/dist/snapshot.js +121 -0
  106. package/node_modules/@loopwright/agent-pi/dist/tool-process.d.ts +32 -0
  107. package/node_modules/@loopwright/agent-pi/dist/tool-process.js +85 -0
  108. package/node_modules/@loopwright/agent-pi/dist/tool-worker.d.ts +2 -0
  109. package/node_modules/@loopwright/agent-pi/dist/tool-worker.js +105 -0
  110. package/node_modules/@loopwright/agent-pi/dist/tools.d.ts +36 -0
  111. package/node_modules/@loopwright/agent-pi/dist/tools.js +128 -0
  112. package/node_modules/@loopwright/agent-pi/dist/types-v2.d.ts +71 -0
  113. package/node_modules/@loopwright/agent-pi/dist/types-v2.js +1 -0
  114. package/node_modules/@loopwright/agent-pi/dist/types.d.ts +314 -0
  115. package/node_modules/@loopwright/agent-pi/dist/types.js +1 -0
  116. package/node_modules/@loopwright/agent-pi/dist/usage.d.ts +18 -0
  117. package/node_modules/@loopwright/agent-pi/dist/usage.js +314 -0
  118. package/node_modules/@loopwright/agent-pi/dist/workspace-mcp.d.ts +27 -0
  119. package/node_modules/@loopwright/agent-pi/dist/workspace-mcp.js +142 -0
  120. package/node_modules/@loopwright/agent-pi/package.json +24 -0
  121. package/node_modules/@loopwright/agent-pi/runtime/Dockerfile +8 -0
  122. package/node_modules/@loopwright/agent-pi/runtime/seccomp.json +1 -0
  123. package/node_modules/@loopwright/cli/dist/arguments.d.ts +19 -0
  124. package/node_modules/@loopwright/cli/dist/arguments.js +358 -0
  125. package/node_modules/@loopwright/cli/dist/closure.d.ts +93 -0
  126. package/node_modules/@loopwright/cli/dist/closure.js +230 -0
  127. package/node_modules/@loopwright/cli/dist/draft.d.ts +9 -0
  128. package/node_modules/@loopwright/cli/dist/draft.js +106 -0
  129. package/node_modules/@loopwright/cli/dist/failure-matrix.d.ts +9 -0
  130. package/node_modules/@loopwright/cli/dist/failure-matrix.js +701 -0
  131. package/node_modules/@loopwright/cli/dist/index.d.ts +6 -0
  132. package/node_modules/@loopwright/cli/dist/index.js +5 -0
  133. package/node_modules/@loopwright/cli/dist/main.d.ts +22 -0
  134. package/node_modules/@loopwright/cli/dist/main.js +1855 -0
  135. package/node_modules/@loopwright/cli/package.json +16 -0
  136. package/node_modules/@loopwright/controller/dist/acceptance.d.ts +43 -0
  137. package/node_modules/@loopwright/controller/dist/acceptance.js +446 -0
  138. package/node_modules/@loopwright/controller/dist/benchmark-gate.d.ts +273 -0
  139. package/node_modules/@loopwright/controller/dist/benchmark-gate.js +3147 -0
  140. package/node_modules/@loopwright/controller/dist/budget-observations.d.ts +25 -0
  141. package/node_modules/@loopwright/controller/dist/budget-observations.js +139 -0
  142. package/node_modules/@loopwright/controller/dist/bundle.d.ts +26 -0
  143. package/node_modules/@loopwright/controller/dist/bundle.js +64 -0
  144. package/node_modules/@loopwright/controller/dist/capture.d.ts +89 -0
  145. package/node_modules/@loopwright/controller/dist/capture.js +587 -0
  146. package/node_modules/@loopwright/controller/dist/compile.d.ts +3 -0
  147. package/node_modules/@loopwright/controller/dist/compile.js +402 -0
  148. package/node_modules/@loopwright/controller/dist/config.d.ts +22 -0
  149. package/node_modules/@loopwright/controller/dist/config.js +255 -0
  150. package/node_modules/@loopwright/controller/dist/controller.d.ts +177 -0
  151. package/node_modules/@loopwright/controller/dist/controller.js +2135 -0
  152. package/node_modules/@loopwright/controller/dist/credentials.d.ts +29 -0
  153. package/node_modules/@loopwright/controller/dist/credentials.js +189 -0
  154. package/node_modules/@loopwright/controller/dist/deadlines.d.ts +18 -0
  155. package/node_modules/@loopwright/controller/dist/deadlines.js +71 -0
  156. package/node_modules/@loopwright/controller/dist/errors.d.ts +27 -0
  157. package/node_modules/@loopwright/controller/dist/errors.js +41 -0
  158. package/node_modules/@loopwright/controller/dist/events.d.ts +52 -0
  159. package/node_modules/@loopwright/controller/dist/events.js +228 -0
  160. package/node_modules/@loopwright/controller/dist/files.d.ts +51 -0
  161. package/node_modules/@loopwright/controller/dist/files.js +386 -0
  162. package/node_modules/@loopwright/controller/dist/finalize.d.ts +25 -0
  163. package/node_modules/@loopwright/controller/dist/finalize.js +32 -0
  164. package/node_modules/@loopwright/controller/dist/gate.d.ts +84 -0
  165. package/node_modules/@loopwright/controller/dist/gate.js +640 -0
  166. package/node_modules/@loopwright/controller/dist/git-objects.d.ts +11 -0
  167. package/node_modules/@loopwright/controller/dist/git-objects.js +59 -0
  168. package/node_modules/@loopwright/controller/dist/git-reader.d.ts +17 -0
  169. package/node_modules/@loopwright/controller/dist/git-reader.js +40 -0
  170. package/node_modules/@loopwright/controller/dist/handlers.d.ts +6 -0
  171. package/node_modules/@loopwright/controller/dist/handlers.js +45 -0
  172. package/node_modules/@loopwright/controller/dist/handoff.d.ts +71 -0
  173. package/node_modules/@loopwright/controller/dist/handoff.js +554 -0
  174. package/node_modules/@loopwright/controller/dist/index.d.ts +41 -0
  175. package/node_modules/@loopwright/controller/dist/index.js +21 -0
  176. package/node_modules/@loopwright/controller/dist/inspect.d.ts +30 -0
  177. package/node_modules/@loopwright/controller/dist/inspect.js +372 -0
  178. package/node_modules/@loopwright/controller/dist/interpolation.d.ts +9 -0
  179. package/node_modules/@loopwright/controller/dist/interpolation.js +79 -0
  180. package/node_modules/@loopwright/controller/dist/ownership.d.ts +41 -0
  181. package/node_modules/@loopwright/controller/dist/ownership.js +666 -0
  182. package/node_modules/@loopwright/controller/dist/parser.d.ts +5 -0
  183. package/node_modules/@loopwright/controller/dist/parser.js +200 -0
  184. package/node_modules/@loopwright/controller/dist/progress.d.ts +41 -0
  185. package/node_modules/@loopwright/controller/dist/progress.js +904 -0
  186. package/node_modules/@loopwright/controller/dist/publication.d.ts +60 -0
  187. package/node_modules/@loopwright/controller/dist/publication.js +329 -0
  188. package/node_modules/@loopwright/controller/dist/reconcile.d.ts +26 -0
  189. package/node_modules/@loopwright/controller/dist/reconcile.js +1378 -0
  190. package/node_modules/@loopwright/controller/dist/recovery.d.ts +10 -0
  191. package/node_modules/@loopwright/controller/dist/recovery.js +315 -0
  192. package/node_modules/@loopwright/controller/dist/refusal-record.d.ts +43 -0
  193. package/node_modules/@loopwright/controller/dist/refusal-record.js +150 -0
  194. package/node_modules/@loopwright/controller/dist/registry.d.ts +6 -0
  195. package/node_modules/@loopwright/controller/dist/registry.js +374 -0
  196. package/node_modules/@loopwright/controller/dist/repair.d.ts +31 -0
  197. package/node_modules/@loopwright/controller/dist/repair.js +102 -0
  198. package/node_modules/@loopwright/controller/dist/report.d.ts +64 -0
  199. package/node_modules/@loopwright/controller/dist/report.js +596 -0
  200. package/node_modules/@loopwright/controller/dist/retained.d.ts +284 -0
  201. package/node_modules/@loopwright/controller/dist/retained.js +193 -0
  202. package/node_modules/@loopwright/controller/dist/rules.d.ts +31 -0
  203. package/node_modules/@loopwright/controller/dist/rules.js +110 -0
  204. package/node_modules/@loopwright/controller/dist/run-eval.d.ts +115 -0
  205. package/node_modules/@loopwright/controller/dist/run-eval.js +959 -0
  206. package/node_modules/@loopwright/controller/dist/scheduler.d.ts +10 -0
  207. package/node_modules/@loopwright/controller/dist/scheduler.js +41 -0
  208. package/node_modules/@loopwright/controller/dist/schema.d.ts +17 -0
  209. package/node_modules/@loopwright/controller/dist/schema.js +78 -0
  210. package/node_modules/@loopwright/controller/dist/source.d.ts +69 -0
  211. package/node_modules/@loopwright/controller/dist/source.js +872 -0
  212. package/node_modules/@loopwright/controller/dist/step.d.ts +38 -0
  213. package/node_modules/@loopwright/controller/dist/step.js +1776 -0
  214. package/node_modules/@loopwright/controller/dist/storage.d.ts +90 -0
  215. package/node_modules/@loopwright/controller/dist/storage.js +1345 -0
  216. package/node_modules/@loopwright/controller/dist/submission.d.ts +51 -0
  217. package/node_modules/@loopwright/controller/dist/submission.js +292 -0
  218. package/node_modules/@loopwright/controller/dist/types.d.ts +585 -0
  219. package/node_modules/@loopwright/controller/dist/types.js +9 -0
  220. package/node_modules/@loopwright/controller/dist/verification.d.ts +124 -0
  221. package/node_modules/@loopwright/controller/dist/verification.js +1308 -0
  222. package/node_modules/@loopwright/controller/dist/verifier-protocol.d.ts +105 -0
  223. package/node_modules/@loopwright/controller/dist/verifier-protocol.js +1312 -0
  224. package/node_modules/@loopwright/controller/dist/verifier-worker.d.ts +1 -0
  225. package/node_modules/@loopwright/controller/dist/verifier-worker.js +33 -0
  226. package/node_modules/@loopwright/controller/package.json +12 -0
  227. package/node_modules/@loopwright/core/dist/acceptance.d.ts +108 -0
  228. package/node_modules/@loopwright/core/dist/acceptance.js +327 -0
  229. package/node_modules/@loopwright/core/dist/artifacts.d.ts +63 -0
  230. package/node_modules/@loopwright/core/dist/artifacts.js +356 -0
  231. package/node_modules/@loopwright/core/dist/boundary.d.ts +21 -0
  232. package/node_modules/@loopwright/core/dist/boundary.js +484 -0
  233. package/node_modules/@loopwright/core/dist/contracts.d.ts +287 -0
  234. package/node_modules/@loopwright/core/dist/contracts.js +694 -0
  235. package/node_modules/@loopwright/core/dist/diagnostics.d.ts +21 -0
  236. package/node_modules/@loopwright/core/dist/diagnostics.js +51 -0
  237. package/node_modules/@loopwright/core/dist/evidence.d.ts +92 -0
  238. package/node_modules/@loopwright/core/dist/evidence.js +85 -0
  239. package/node_modules/@loopwright/core/dist/graph.d.ts +8 -0
  240. package/node_modules/@loopwright/core/dist/graph.js +44 -0
  241. package/node_modules/@loopwright/core/dist/identity.d.ts +162 -0
  242. package/node_modules/@loopwright/core/dist/identity.js +80 -0
  243. package/node_modules/@loopwright/core/dist/index.d.ts +30 -0
  244. package/node_modules/@loopwright/core/dist/index.js +39 -0
  245. package/node_modules/@loopwright/core/dist/limits.d.ts +99 -0
  246. package/node_modules/@loopwright/core/dist/limits.js +59 -0
  247. package/node_modules/@loopwright/core/dist/results.d.ts +161 -0
  248. package/node_modules/@loopwright/core/dist/results.js +98 -0
  249. package/node_modules/@loopwright/core/dist/scope.d.ts +13 -0
  250. package/node_modules/@loopwright/core/dist/scope.js +154 -0
  251. package/node_modules/@loopwright/core/dist/scrubber.d.ts +13 -0
  252. package/node_modules/@loopwright/core/dist/scrubber.js +63 -0
  253. package/node_modules/@loopwright/core/dist/usage.d.ts +68 -0
  254. package/node_modules/@loopwright/core/dist/usage.js +56 -0
  255. package/node_modules/@loopwright/core/package.json +12 -0
  256. package/node_modules/@loopwright/decisions/dist/benchmark.d.ts +23 -0
  257. package/node_modules/@loopwright/decisions/dist/benchmark.js +337 -0
  258. package/node_modules/@loopwright/decisions/dist/client.d.ts +74 -0
  259. package/node_modules/@loopwright/decisions/dist/client.js +292 -0
  260. package/node_modules/@loopwright/decisions/dist/index.d.ts +15 -0
  261. package/node_modules/@loopwright/decisions/dist/index.js +8 -0
  262. package/node_modules/@loopwright/decisions/dist/nodes.d.ts +63 -0
  263. package/node_modules/@loopwright/decisions/dist/nodes.js +282 -0
  264. package/node_modules/@loopwright/decisions/dist/primitives.d.ts +36 -0
  265. package/node_modules/@loopwright/decisions/dist/primitives.js +300 -0
  266. package/node_modules/@loopwright/decisions/dist/redaction.d.ts +23 -0
  267. package/node_modules/@loopwright/decisions/dist/redaction.js +146 -0
  268. package/node_modules/@loopwright/decisions/dist/router.d.ts +55 -0
  269. package/node_modules/@loopwright/decisions/dist/router.js +248 -0
  270. package/node_modules/@loopwright/decisions/dist/schema.d.ts +25 -0
  271. package/node_modules/@loopwright/decisions/dist/schema.js +536 -0
  272. package/node_modules/@loopwright/decisions/dist/types.d.ts +394 -0
  273. package/node_modules/@loopwright/decisions/dist/types.js +80 -0
  274. package/node_modules/@loopwright/decisions/package.json +12 -0
  275. package/node_modules/@loopwright/github-app/dist/action.d.ts +90 -0
  276. package/node_modules/@loopwright/github-app/dist/action.js +2429 -0
  277. package/node_modules/@loopwright/github-app/dist/config.d.ts +23 -0
  278. package/node_modules/@loopwright/github-app/dist/config.js +297 -0
  279. package/node_modules/@loopwright/github-app/dist/digests.d.ts +1 -0
  280. package/node_modules/@loopwright/github-app/dist/digests.js +1 -0
  281. package/node_modules/@loopwright/github-app/dist/index.d.ts +14 -0
  282. package/node_modules/@loopwright/github-app/dist/index.js +13 -0
  283. package/node_modules/@loopwright/github-app/dist/models-config.d.ts +1 -0
  284. package/node_modules/@loopwright/github-app/dist/models-config.js +1 -0
  285. package/node_modules/@loopwright/github-app/dist/output-format.d.ts +1 -0
  286. package/node_modules/@loopwright/github-app/dist/output-format.js +1 -0
  287. package/node_modules/@loopwright/github-app/dist/preflight.d.ts +1 -0
  288. package/node_modules/@loopwright/github-app/dist/preflight.js +1 -0
  289. package/node_modules/@loopwright/github-app/dist/publisher.d.ts +58 -0
  290. package/node_modules/@loopwright/github-app/dist/publisher.js +1368 -0
  291. package/node_modules/@loopwright/github-app/dist/qualification.d.ts +1 -0
  292. package/node_modules/@loopwright/github-app/dist/qualification.js +1 -0
  293. package/node_modules/@loopwright/github-app/dist/sandbox.d.ts +1 -0
  294. package/node_modules/@loopwright/github-app/dist/sandbox.js +1 -0
  295. package/node_modules/@loopwright/github-app/dist/schema.d.ts +1 -0
  296. package/node_modules/@loopwright/github-app/dist/schema.js +1 -0
  297. package/node_modules/@loopwright/github-app/dist/service.d.ts +52 -0
  298. package/node_modules/@loopwright/github-app/dist/service.js +1658 -0
  299. package/node_modules/@loopwright/github-app/dist/suite-pr-lookup.d.ts +90 -0
  300. package/node_modules/@loopwright/github-app/dist/suite-pr-lookup.js +392 -0
  301. package/node_modules/@loopwright/github-app/dist/taskpack.d.ts +1 -0
  302. package/node_modules/@loopwright/github-app/dist/taskpack.js +1 -0
  303. package/node_modules/@loopwright/github-app/dist/tenant-queue.d.ts +44 -0
  304. package/node_modules/@loopwright/github-app/dist/tenant-queue.js +423 -0
  305. package/node_modules/@loopwright/github-app/dist/token-broker.d.ts +50 -0
  306. package/node_modules/@loopwright/github-app/dist/token-broker.js +262 -0
  307. package/node_modules/@loopwright/github-app/dist/turnkey-memory.d.ts +35 -0
  308. package/node_modules/@loopwright/github-app/dist/turnkey-memory.js +1654 -0
  309. package/node_modules/@loopwright/github-app/dist/turnkey.d.ts +16 -0
  310. package/node_modules/@loopwright/github-app/dist/turnkey.js +34 -0
  311. package/node_modules/@loopwright/github-app/dist/types.d.ts +220 -0
  312. package/node_modules/@loopwright/github-app/dist/types.js +14 -0
  313. package/node_modules/@loopwright/github-app/dist/upstreams.d.ts +13 -0
  314. package/node_modules/@loopwright/github-app/dist/upstreams.js +65 -0
  315. package/node_modules/@loopwright/github-app/dist/webhook.d.ts +34 -0
  316. package/node_modules/@loopwright/github-app/dist/webhook.js +241 -0
  317. package/node_modules/@loopwright/github-app/dist/yaml.d.ts +1 -0
  318. package/node_modules/@loopwright/github-app/dist/yaml.js +1 -0
  319. package/node_modules/@loopwright/github-app/package.json +12 -0
  320. package/node_modules/@loopwright/hardware-runner/dist/config.d.ts +48 -0
  321. package/node_modules/@loopwright/hardware-runner/dist/config.js +719 -0
  322. package/node_modules/@loopwright/hardware-runner/dist/evaluator.d.ts +230 -0
  323. package/node_modules/@loopwright/hardware-runner/dist/evaluator.js +3178 -0
  324. package/node_modules/@loopwright/hardware-runner/dist/index.d.ts +13 -0
  325. package/node_modules/@loopwright/hardware-runner/dist/index.js +9 -0
  326. package/node_modules/@loopwright/hardware-runner/dist/provenance.d.ts +128 -0
  327. package/node_modules/@loopwright/hardware-runner/dist/provenance.js +1500 -0
  328. package/node_modules/@loopwright/hardware-runner/dist/queue.d.ts +81 -0
  329. package/node_modules/@loopwright/hardware-runner/dist/queue.js +1081 -0
  330. package/node_modules/@loopwright/hardware-runner/dist/relay-server.d.ts +13 -0
  331. package/node_modules/@loopwright/hardware-runner/dist/relay-server.js +2997 -0
  332. package/node_modules/@loopwright/hardware-runner/dist/types.d.ts +239 -0
  333. package/node_modules/@loopwright/hardware-runner/dist/types.js +12 -0
  334. package/node_modules/@loopwright/hardware-runner/dist/worker.d.ts +49 -0
  335. package/node_modules/@loopwright/hardware-runner/dist/worker.js +1648 -0
  336. package/node_modules/@loopwright/hardware-runner/package.json +12 -0
  337. package/node_modules/@loopwright/memory/dist/cas.d.ts +21 -0
  338. package/node_modules/@loopwright/memory/dist/cas.js +510 -0
  339. package/node_modules/@loopwright/memory/dist/compaction.d.ts +18 -0
  340. package/node_modules/@loopwright/memory/dist/compaction.js +110 -0
  341. package/node_modules/@loopwright/memory/dist/finalizer.d.ts +23 -0
  342. package/node_modules/@loopwright/memory/dist/finalizer.js +538 -0
  343. package/node_modules/@loopwright/memory/dist/index.d.ts +9 -0
  344. package/node_modules/@loopwright/memory/dist/index.js +8 -0
  345. package/node_modules/@loopwright/memory/dist/schema.d.ts +36 -0
  346. package/node_modules/@loopwright/memory/dist/schema.js +510 -0
  347. package/node_modules/@loopwright/memory/dist/selector.d.ts +7 -0
  348. package/node_modules/@loopwright/memory/dist/selector.js +157 -0
  349. package/node_modules/@loopwright/memory/dist/store.d.ts +51 -0
  350. package/node_modules/@loopwright/memory/dist/store.js +554 -0
  351. package/node_modules/@loopwright/memory/dist/types.d.ts +442 -0
  352. package/node_modules/@loopwright/memory/dist/types.js +8 -0
  353. package/node_modules/@loopwright/memory/dist/verifier.d.ts +5 -0
  354. package/node_modules/@loopwright/memory/dist/verifier.js +328 -0
  355. package/node_modules/@loopwright/memory/package.json +12 -0
  356. package/node_modules/@loopwright/state/dist/admission.d.ts +321 -0
  357. package/node_modules/@loopwright/state/dist/admission.js +199 -0
  358. package/node_modules/@loopwright/state/dist/boundary.d.ts +53 -0
  359. package/node_modules/@loopwright/state/dist/boundary.js +194 -0
  360. package/node_modules/@loopwright/state/dist/checkpoint.d.ts +16 -0
  361. package/node_modules/@loopwright/state/dist/checkpoint.js +84 -0
  362. package/node_modules/@loopwright/state/dist/clock.d.ts +8 -0
  363. package/node_modules/@loopwright/state/dist/clock.js +27 -0
  364. package/node_modules/@loopwright/state/dist/connection.d.ts +55 -0
  365. package/node_modules/@loopwright/state/dist/connection.js +391 -0
  366. package/node_modules/@loopwright/state/dist/errors.d.ts +13 -0
  367. package/node_modules/@loopwright/state/dist/errors.js +34 -0
  368. package/node_modules/@loopwright/state/dist/index.d.ts +3 -0
  369. package/node_modules/@loopwright/state/dist/index.js +1 -0
  370. package/node_modules/@loopwright/state/dist/journal.d.ts +119 -0
  371. package/node_modules/@loopwright/state/dist/journal.js +72 -0
  372. package/node_modules/@loopwright/state/dist/model.d.ts +40 -0
  373. package/node_modules/@loopwright/state/dist/model.js +58 -0
  374. package/node_modules/@loopwright/state/dist/ownership.d.ts +14 -0
  375. package/node_modules/@loopwright/state/dist/ownership.js +45 -0
  376. package/node_modules/@loopwright/state/dist/pages.d.ts +11 -0
  377. package/node_modules/@loopwright/state/dist/pages.js +41 -0
  378. package/node_modules/@loopwright/state/dist/receipts.d.ts +33 -0
  379. package/node_modules/@loopwright/state/dist/receipts.js +97 -0
  380. package/node_modules/@loopwright/state/dist/schema.d.ts +28 -0
  381. package/node_modules/@loopwright/state/dist/schema.js +41 -0
  382. package/node_modules/@loopwright/state/dist/settlement.d.ts +10 -0
  383. package/node_modules/@loopwright/state/dist/settlement.js +129 -0
  384. package/node_modules/@loopwright/state/dist/store.d.ts +52 -0
  385. package/node_modules/@loopwright/state/dist/store.js +100 -0
  386. package/node_modules/@loopwright/state/dist/types.d.ts +315 -0
  387. package/node_modules/@loopwright/state/dist/types.js +1 -0
  388. package/node_modules/@loopwright/state/package.json +12 -0
  389. package/node_modules/@loopwright/taskpack/dist/baseline-align.d.ts +195 -0
  390. package/node_modules/@loopwright/taskpack/dist/baseline-align.js +1847 -0
  391. package/node_modules/@loopwright/taskpack/dist/digests.d.ts +61 -0
  392. package/node_modules/@loopwright/taskpack/dist/digests.js +715 -0
  393. package/node_modules/@loopwright/taskpack/dist/index.d.ts +18 -0
  394. package/node_modules/@loopwright/taskpack/dist/index.js +17 -0
  395. package/node_modules/@loopwright/taskpack/dist/models-config.d.ts +112 -0
  396. package/node_modules/@loopwright/taskpack/dist/models-config.js +1026 -0
  397. package/node_modules/@loopwright/taskpack/dist/output-format.d.ts +28 -0
  398. package/node_modules/@loopwright/taskpack/dist/output-format.js +157 -0
  399. package/node_modules/@loopwright/taskpack/dist/preflight.d.ts +130 -0
  400. package/node_modules/@loopwright/taskpack/dist/preflight.js +1497 -0
  401. package/node_modules/@loopwright/taskpack/dist/qualification.d.ts +23 -0
  402. package/node_modules/@loopwright/taskpack/dist/qualification.js +120 -0
  403. package/node_modules/@loopwright/taskpack/dist/sandbox.d.ts +121 -0
  404. package/node_modules/@loopwright/taskpack/dist/sandbox.js +2642 -0
  405. package/node_modules/@loopwright/taskpack/dist/schema.d.ts +197 -0
  406. package/node_modules/@loopwright/taskpack/dist/schema.js +4189 -0
  407. package/node_modules/@loopwright/taskpack/dist/scrubber.d.ts +47 -0
  408. package/node_modules/@loopwright/taskpack/dist/scrubber.js +283 -0
  409. package/node_modules/@loopwright/taskpack/dist/suite.d.ts +124 -0
  410. package/node_modules/@loopwright/taskpack/dist/suite.js +3256 -0
  411. package/node_modules/@loopwright/taskpack/dist/taskpack.d.ts +154 -0
  412. package/node_modules/@loopwright/taskpack/dist/taskpack.js +3634 -0
  413. package/node_modules/@loopwright/taskpack/dist/turnkey-memory.d.ts +122 -0
  414. package/node_modules/@loopwright/taskpack/dist/turnkey-memory.js +483 -0
  415. package/node_modules/@loopwright/taskpack/dist/turnkey.d.ts +437 -0
  416. package/node_modules/@loopwright/taskpack/dist/turnkey.js +7335 -0
  417. package/node_modules/@loopwright/taskpack/dist/types.d.ts +1274 -0
  418. package/node_modules/@loopwright/taskpack/dist/types.js +99 -0
  419. package/node_modules/@loopwright/taskpack/dist/upstreams.d.ts +261 -0
  420. package/node_modules/@loopwright/taskpack/dist/upstreams.js +2242 -0
  421. package/node_modules/@loopwright/taskpack/dist/yaml.d.ts +18 -0
  422. package/node_modules/@loopwright/taskpack/dist/yaml.js +167 -0
  423. package/node_modules/@loopwright/taskpack/package.json +12 -0
  424. package/node_modules/@loopwright/taskpack-runner/dist/compile-taskpack.d.ts +137 -0
  425. package/node_modules/@loopwright/taskpack-runner/dist/compile-taskpack.js +474 -0
  426. package/node_modules/@loopwright/taskpack-runner/dist/corpus.d.ts +8 -0
  427. package/node_modules/@loopwright/taskpack-runner/dist/corpus.js +129 -0
  428. package/node_modules/@loopwright/taskpack-runner/dist/index.d.ts +13 -0
  429. package/node_modules/@loopwright/taskpack-runner/dist/index.js +7 -0
  430. package/node_modules/@loopwright/taskpack-runner/dist/intake.d.ts +149 -0
  431. package/node_modules/@loopwright/taskpack-runner/dist/intake.js +135 -0
  432. package/node_modules/@loopwright/taskpack-runner/dist/parity.d.ts +130 -0
  433. package/node_modules/@loopwright/taskpack-runner/dist/parity.js +927 -0
  434. package/node_modules/@loopwright/taskpack-runner/dist/python-bridge.d.ts +68 -0
  435. package/node_modules/@loopwright/taskpack-runner/dist/python-bridge.js +517 -0
  436. package/node_modules/@loopwright/taskpack-runner/dist/service.d.ts +149 -0
  437. package/node_modules/@loopwright/taskpack-runner/dist/service.js +1796 -0
  438. package/node_modules/@loopwright/taskpack-runner/dist/types.d.ts +29 -0
  439. package/node_modules/@loopwright/taskpack-runner/dist/types.js +11 -0
  440. package/node_modules/@loopwright/taskpack-runner/package.json +12 -0
  441. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/README.md +77 -0
  442. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/docs/support-records.md +57 -0
  443. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/docs/supported-runners.md +98 -0
  444. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/manifest.json +205 -0
  445. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/pack/expand_matrix.sh +273 -0
  446. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/pack/matrix.yaml +280 -0
  447. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/pack/resolve_dockerfile.sh +24 -0
  448. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/pyproject.toml +46 -0
  449. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/runtime_runner/runner.py.json +5270 -0
  450. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/fixtures/discovery/legacy-support.md +86 -0
  451. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/fixtures/github/service.py +401 -0
  452. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/fixtures/proposals/analysis.json +95 -0
  453. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/fixtures/proposals/ready.json +109 -0
  454. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_assemble.py +263 -0
  455. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_check_draft.py +333 -0
  456. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_checks.py +1099 -0
  457. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_discovery.py +817 -0
  458. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_proposal.py +658 -0
  459. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_publish.py +987 -0
  460. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tests/auto_sync/test_summary.py +107 -0
  461. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/agent.py +610 -0
  462. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/assemble.py +84 -0
  463. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/check_draft.py +94 -0
  464. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/checks.py +1002 -0
  465. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/discovery.py +789 -0
  466. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/model.py +557 -0
  467. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/progress.py +174 -0
  468. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/prompts/analysis.md +40 -0
  469. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/prompts/proposal.md +65 -0
  470. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/prompts/repair.md +17 -0
  471. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/proposal.py +903 -0
  472. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/publish.py +997 -0
  473. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/summary.py +131 -0
  474. package/node_modules/@loopwright/taskpack-runner/vendor/runner-auto-sync/tools/auto_sync/tool-versions.json +133 -0
  475. package/node_modules/package-lock.json +3025 -0
  476. package/package-lock.json +3025 -0
  477. package/package.json +52 -4
@@ -0,0 +1,633 @@
1
+ import { createAgentSession, ModelRuntime, SessionManager } from '@earendil-works/pi-coding-agent';
2
+ import { mkdtemp } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { pathToFileURL } from 'node:url';
5
+ import { Channel } from './protocol.js';
6
+ import { compileTrustedFactories } from './factory-bootstrap.js';
7
+ import { toolArgumentsDigest } from './usage.js';
8
+ import { bridgedTools } from './tools.js';
9
+ import { parseBoundedJson } from './context.js';
10
+ import { isExternalMcpToolName } from './external-mcp.js';
11
+ import { nativeMcpFactories, OwnerMcpTransport } from './mcp-transport.js';
12
+ import { parseJsonRpcMessage } from '@earendil-works/pi-mcp';
13
+ import { SnapshotTransfer } from './snapshot-transfer.js';
14
+ import { rowBytes, NativeSnapshot } from './snapshot.js';
15
+ import { verifyNativeCompletion } from './lifecycle.js';
16
+ function sliceCodepoints(text, maxChars) {
17
+ return Array.from(text).slice(0, maxChars).join('');
18
+ }
19
+ function compactToolArguments(args, maxStrChars) {
20
+ let modified = false;
21
+ const out = {};
22
+ for (const [k, v] of Object.entries(args)) {
23
+ if (typeof v === 'string' && v.length > maxStrChars + 64) {
24
+ out[k] = sliceCodepoints(v, maxStrChars) + '...[compacted]';
25
+ modified = true;
26
+ }
27
+ else {
28
+ out[k] = v;
29
+ }
30
+ }
31
+ return modified ? out : args;
32
+ }
33
+ /**
34
+ * Compact older bulky toolResult / non-signed thinking blocks in a shallow-cloned outbound Context
35
+ * when multi-turn history approaches the finalized request byte budget, leaving SessionManager untouched.
36
+ */
37
+ export function pruneContextForBudget(context, maxBodyBytes, api) {
38
+ if (maxBodyBytes < 8192 || !context.messages.some(msg => msg.role === 'toolResult'))
39
+ return context;
40
+ const toolCount = context.tools?.length ?? 0;
41
+ const wireHeadroom = Math.min(9_216, Math.max(4_096, Math.floor(maxBodyBytes / 7) + toolCount * 48));
42
+ const rawTargetBytes = Math.max(4_096, maxBodyBytes - wireHeadroom);
43
+ const baseBytes = Buffer.byteLength(JSON.stringify({ ...context, messages: [] }));
44
+ const minWireMargin = Math.min(4_096, 512 + toolCount * 40);
45
+ const targetBytes = baseBytes + 2_048 > rawTargetBytes
46
+ ? Math.max(rawTargetBytes, maxBodyBytes - minWireMargin)
47
+ : rawTargetBytes;
48
+ if (Buffer.byteLength(JSON.stringify(context)) <= targetBytes)
49
+ return context;
50
+ const availableMessageBytes = Math.max(1_024, targetBytes - baseBytes);
51
+ const messages = structuredClone(context.messages);
52
+ const candidate = () => ({ ...context, messages });
53
+ const withinBudget = () => Buffer.byteLength(JSON.stringify(candidate())) <= targetBytes;
54
+ const toolResultIndices = [];
55
+ for (let i = 0; i < messages.length; i++) {
56
+ if (messages[i]?.role === 'toolResult')
57
+ toolResultIndices.push(i);
58
+ }
59
+ // Pass 1: compact older tool results first while keeping the 2 most recent tool results intact.
60
+ const olderIndices = toolResultIndices.length > 2 ? toolResultIndices.slice(0, -2) : [];
61
+ for (const idx of olderIndices) {
62
+ const msg = messages[idx];
63
+ if (!msg || msg.role !== 'toolResult')
64
+ continue;
65
+ let changed = false;
66
+ for (const block of msg.content) {
67
+ if (block.type === 'text' && block.text.length > 360) {
68
+ block.text = sliceCodepoints(block.text, 220) + '\n...[earlier tool output compacted to fit context budget; re-read specific lines with offset/limit if needed]...';
69
+ changed = true;
70
+ }
71
+ }
72
+ if (changed && withinBudget())
73
+ return candidate();
74
+ }
75
+ // Pass 2: on APIs without signed thinking blocks, compact older assistant reasoning traces.
76
+ let lastAssistant = -1;
77
+ for (let i = messages.length - 1; i >= 0; i--) {
78
+ if (messages[i]?.role === 'assistant') {
79
+ lastAssistant = i;
80
+ break;
81
+ }
82
+ }
83
+ if (api !== 'anthropic-messages') {
84
+ for (let i = 0; i < lastAssistant; i++) {
85
+ const msg = messages[i];
86
+ if (!msg || msg.role !== 'assistant')
87
+ continue;
88
+ let changed = false;
89
+ for (const block of msg.content) {
90
+ if (block.type === 'thinking' && block.thinking.length > 200) {
91
+ block.thinking = sliceCodepoints(block.thinking, 120) + '...';
92
+ changed = true;
93
+ }
94
+ }
95
+ if (changed && withinBudget())
96
+ return candidate();
97
+ }
98
+ }
99
+ // Pass 3: if still above budget (e.g. parallel large reads or heavy MCP tool schemas on a tight window),
100
+ // size recent tool results against availableMessageBytes after subtracting fixed tools + systemPrompt overhead.
101
+ const recentIndices = toolResultIndices.slice(-2);
102
+ const perRecentChars = Math.max(240, Math.floor((availableMessageBytes / 3) / Math.max(1, recentIndices.length)));
103
+ for (const idx of recentIndices) {
104
+ const msg = messages[idx];
105
+ if (!msg || msg.role !== 'toolResult')
106
+ continue;
107
+ let changed = false;
108
+ for (const block of msg.content) {
109
+ if (block.type === 'text' && block.text.length > perRecentChars + 120) {
110
+ block.text = sliceCodepoints(block.text, perRecentChars) + '\n...[tool output compacted to fit context budget; use smaller limit or filter output]...';
111
+ changed = true;
112
+ }
113
+ }
114
+ if (changed && withinBudget())
115
+ return candidate();
116
+ }
117
+ // Pass 4 fallback: tighten remaining toolResult stubs (160 chars) and compact older non-final assistant blocks/arguments
118
+ // so outbound context stays within targetBytes whenever baseBytes < targetBytes.
119
+ if (!withinBudget()) {
120
+ for (const idx of toolResultIndices) {
121
+ const msg = messages[idx];
122
+ if (!msg || msg.role !== 'toolResult')
123
+ continue;
124
+ let changed = false;
125
+ for (const block of msg.content) {
126
+ if (block.type === 'text' && block.text.length > 180) {
127
+ block.text = sliceCodepoints(block.text, 160) + '\n...[compacted]...';
128
+ changed = true;
129
+ }
130
+ }
131
+ if (changed && withinBudget())
132
+ return candidate();
133
+ }
134
+ for (let i = 0; i < lastAssistant; i++) {
135
+ const msg = messages[i];
136
+ if (!msg || msg.role !== 'assistant')
137
+ continue;
138
+ let changed = false;
139
+ for (const block of msg.content) {
140
+ if (block.type === 'text' && block.text.length > 180) {
141
+ block.text = sliceCodepoints(block.text, 160) + '...';
142
+ changed = true;
143
+ }
144
+ else if (block.type === 'thinking' && api !== 'anthropic-messages' && block.thinking.length > 0) {
145
+ block.thinking = '';
146
+ changed = true;
147
+ }
148
+ else if (block.type === 'toolCall' && block.arguments && typeof block.arguments === 'object') {
149
+ const compactedArgs = compactToolArguments(block.arguments, 160);
150
+ if (compactedArgs !== block.arguments) {
151
+ block.arguments = compactedArgs;
152
+ changed = true;
153
+ }
154
+ }
155
+ }
156
+ if (changed && withinBudget())
157
+ return candidate();
158
+ }
159
+ // If many accumulated turns still exceed targetBytes while baseBytes < targetBytes,
160
+ // drop the oldest complete (assistant + matching toolResult*) turn groups, preserving at least the last 2 turns.
161
+ if (!withinBudget() && baseBytes + 512 < targetBytes) {
162
+ let safety = 64;
163
+ while (!withinBudget() && safety-- > 0) {
164
+ const assistantIndices = messages
165
+ .map((m, i) => (m?.role === 'assistant' ? i : -1))
166
+ .filter(i => i >= 0);
167
+ if (assistantIndices.length <= 2)
168
+ break;
169
+ const dropStart = assistantIndices[0];
170
+ let dropEnd = dropStart + 1;
171
+ while (dropEnd < messages.length && messages[dropEnd]?.role === 'toolResult') {
172
+ dropEnd++;
173
+ }
174
+ // Only drop a clean paired assistant + toolResult+ span so role alternation and callId pairing stay intact.
175
+ if (dropEnd <= dropStart + 1)
176
+ break;
177
+ messages.splice(dropStart, dropEnd - dropStart);
178
+ }
179
+ }
180
+ }
181
+ return candidate();
182
+ }
183
+ /** Internal trusted constructor; fixture tools are supplied only by separate test entrypoints. */
184
+ export function runSdkWorker(nonce, options = {}) {
185
+ const isHostSession = process.env.LOOPWRIGHT_HOST_SESSION === '1';
186
+ const channel = new Channel(nonce, process.stdin, process.stdout), snapshots = new SnapshotTransfer(channel);
187
+ let session = null;
188
+ let runtime, active = false, outputTokens = 1024, ordinal = 0, toolOrdinal = 0, commandOrdinal = 0;
189
+ let receipts = [];
190
+ let toolArgsBytes = 65536, toolResultBytes = 65536, sessionBytes = 8388608, safeSnapshot = false;
191
+ let snapshotPolicy = null;
192
+ let native = { agentEnds: 0, agentSettles: 0, aborted: false, compactionEnds: 0, compacted: false, parserAcks: 0 };
193
+ let queued = 0, restoredEntries = null, restoring = false;
194
+ const invocationWaiters = new Map();
195
+ const parsedWaiters = new Map();
196
+ const toolWaiters = new Map();
197
+ const executionWaiters = new Map();
198
+ const roots = new Map();
199
+ const codemodeRoots = new Set();
200
+ const nativeWaiters = new Map();
201
+ const mcp = new OwnerMcpTransport(channel, () => { void channel.send('runtime-fault', { code: 'MCP_FAILED' }).catch(() => channel.fail()); });
202
+ let pendingFetch = null;
203
+ const rejectPending = () => { snapshots.close(); for (const waiter of invocationWaiters.values())
204
+ waiter.reject(new Error('owner channel closed')); invocationWaiters.clear(); for (const waiter of parsedWaiters.values())
205
+ waiter.reject(new Error('owner receipt unavailable')); parsedWaiters.clear(); pendingFetch?.reject(new Error('owner channel closed')); pendingFetch = null; for (const waiter of toolWaiters.values())
206
+ waiter(false); toolWaiters.clear(); for (const waiter of executionWaiters.values())
207
+ waiter.reject(new Error('owner execution unavailable')); executionWaiters.clear(); for (const waiter of nativeWaiters.values())
208
+ waiter.reject(new Error('Owner native result unavailable')); nativeWaiters.clear(); };
209
+ channel.on('failed', (reason) => { process.stderr.write(JSON.stringify({ protocolFailure: reason }) + '\n'); rejectPending(); void session?.abort(); process.exitCode = 1; process.stdin.destroy(); });
210
+ const authorization = pi => {
211
+ if (options.onAgentEnd)
212
+ pi.on('agent_end', async () => { if (session)
213
+ await options.onAgentEnd(session); });
214
+ pi.on('tool_call', async (event) => {
215
+ options.onToolCall?.();
216
+ const parent = event.parentToolCallId ?? event.toolCallId, producer = roots.get(parent);
217
+ if (!producer)
218
+ return { block: true, reason: 'No admitted model parent' };
219
+ const query = ++toolOrdinal, call = { invocation: producer.invocation, slot: producer.slot, callId: event.toolCallId, name: event.toolName, parentId: event.parentToolCallId ?? null };
220
+ const toolInput = isHostSession && call.parentId === null && producer.rawArguments !== undefined ? producer.rawArguments : event.input;
221
+ const args = Buffer.from(JSON.stringify(toolInput));
222
+ if (args.length > toolArgsBytes)
223
+ return { block: true, reason: 'Argument bound' };
224
+ const answer = new Promise(resolve => { toolWaiters.set(query, resolve); });
225
+ await channel.sendBytes('root-tool', { query, ...call }, args);
226
+ if (!await answer)
227
+ return { block: true, reason: 'Model parent not admitted' };
228
+ roots.set(event.toolCallId, {
229
+ invocation: producer.invocation,
230
+ slot: producer.slot,
231
+ parentId: call.parentId,
232
+ ...(call.parentId === null && producer.rawArguments !== undefined ? { rawArguments: producer.rawArguments } : {}),
233
+ });
234
+ if (event.toolName === 'codemode' && call.parentId === null)
235
+ codemodeRoots.add(event.toolCallId);
236
+ if (event.toolName === 'mcp__workspace__read_file')
237
+ mcp.admit(call, event.input);
238
+ });
239
+ pi.on('tool_result', async (event) => {
240
+ const result = { content: event.content, details: event.details ?? null, isError: event.isError, ...(event.structuredContent === undefined ? {} : { structuredContent: event.structuredContent }) };
241
+ const producer = roots.get(event.toolCallId);
242
+ const rawInput = isHostSession && producer?.parentId === null && producer.rawArguments !== undefined ? producer.rawArguments : event.input;
243
+ const body = Buffer.from(JSON.stringify({ argumentsDigest: toolArgumentsDigest(rawInput), result }));
244
+ if (body.length > toolResultBytes || event.content.some(block => block.type !== 'text')) {
245
+ await channel.send('runtime-fault', { code: 'OUTPUT_LIMIT' });
246
+ return { content: [{ type: 'text', text: 'OUTPUT_LIMIT' }], details: null, isError: true };
247
+ }
248
+ if (!codemodeRoots.has(event.toolCallId))
249
+ return;
250
+ if (!producer)
251
+ throw new Error('Missing native result parent');
252
+ const query = ++toolOrdinal, answer = new Promise((resolve, reject) => { nativeWaiters.set(query, { resolve, reject }); });
253
+ void answer.catch(() => { });
254
+ try {
255
+ await channel.sendBytes('native-result', { query, invocation: producer.invocation, slot: producer.slot, callId: event.toolCallId, name: event.toolName, parentId: null }, body);
256
+ await answer;
257
+ }
258
+ finally {
259
+ nativeWaiters.delete(query);
260
+ codemodeRoots.delete(event.toolCallId);
261
+ }
262
+ });
263
+ };
264
+ const executeTool = async (name, id, args, signal) => {
265
+ const producer = roots.get(id);
266
+ if (!active || !producer || executionWaiters.size >= 32)
267
+ throw new Error('No admitted tool parent');
268
+ const effectiveArgs = isHostSession && producer.parentId === null && producer.rawArguments !== undefined ? producer.rawArguments : args;
269
+ const bytes = Buffer.from(JSON.stringify(effectiveArgs));
270
+ if (bytes.length > toolArgsBytes)
271
+ throw new Error('Tool argument bound');
272
+ const query = ++toolOrdinal, answer = new Promise((resolve, reject) => { executionWaiters.set(query, { resolve, reject }); });
273
+ void answer.catch(() => { });
274
+ const call = { invocation: producer.invocation, slot: producer.slot, callId: id, name, parentId: producer.parentId }, cancel = () => { void channel.send('tool-cancel', call).catch(() => channel.fail()); };
275
+ signal?.addEventListener('abort', cancel, { once: true });
276
+ try {
277
+ await channel.sendBytes('tool', { query, ...call }, bytes);
278
+ if (signal?.aborted)
279
+ cancel();
280
+ return await answer;
281
+ }
282
+ finally {
283
+ executionWaiters.delete(query);
284
+ signal?.removeEventListener('abort', cancel);
285
+ }
286
+ };
287
+ function historyBound() {
288
+ if (!session)
289
+ return;
290
+ let bytes = 0;
291
+ for (const entry of [session.sessionManager.getHeader(), ...session.sessionManager.getEntries()]) {
292
+ if (!entry)
293
+ throw new Error('Missing native history');
294
+ bytes += Buffer.byteLength(JSON.stringify(entry)) + 1;
295
+ if (bytes > sessionBytes) {
296
+ void channel.send('runtime-fault', { code: 'OUTPUT_LIMIT' }).catch(() => channel.fail());
297
+ throw new Error('Native transcript bound');
298
+ }
299
+ }
300
+ }
301
+ async function handle(message) {
302
+ if (await snapshots.receive(message))
303
+ return;
304
+ if (message.kind === 'restore-start') {
305
+ if (session || active || restoring || restoredEntries)
306
+ throw new Error('Duplicate restore');
307
+ restoring = true;
308
+ const value = message.body;
309
+ if (!Number.isSafeInteger(value.sessionBytes) || value.sessionBytes < 1 || value.sessionBytes > 67108864)
310
+ throw new Error('Restore bound');
311
+ const validator = new NativeSnapshot({ selection: value.selection, tools: value.toolNames, limits: { sessionBytes: value.sessionBytes } }), expected = snapshots.expect(async (bytes) => { validator.push(bytes); }, value.sessionBytes, 90000);
312
+ void expected.done.then(async () => { restoredEntries = validator.finish(); restoring = false; await channel.send('restore-complete', null); }).catch(() => channel.fail());
313
+ await channel.send('restore-ready', null);
314
+ return;
315
+ }
316
+ if (message.kind === 'capture') {
317
+ if (!session || active || !safeSnapshot || !session.isIdle || !snapshotPolicy)
318
+ throw new Error('Unsafe snapshot');
319
+ active = true;
320
+ const query = message.body.query;
321
+ if (!Number.isSafeInteger(query) || query < 1)
322
+ throw new Error('Snapshot query');
323
+ const validator = new NativeSnapshot(snapshotPolicy, sessionBytes, false), current = session;
324
+ async function* rows() { const header = current.sessionManager.getHeader(); if (!header)
325
+ throw new Error('Missing snapshot header'); for (const entry of [header, ...current.sessionManager.getEntries()]) {
326
+ const bytes = rowBytes(entry);
327
+ validator.push(bytes);
328
+ yield bytes;
329
+ } validator.finish(); }
330
+ try {
331
+ await snapshots.send(query, rows(), sessionBytes, () => { active = false; });
332
+ }
333
+ finally {
334
+ active = false;
335
+ }
336
+ return;
337
+ }
338
+ if (message.kind === 'init') {
339
+ if (session || restoring)
340
+ throw new Error('duplicate init');
341
+ const value = message.body;
342
+ if (!Array.isArray(value.toolNames)
343
+ || value.toolNames.length > 128
344
+ || new Set(value.toolNames).size !== value.toolNames.length
345
+ || value.toolNames.some(name => !['read', 'bash', 'write', 'edit', 'delete', 'codemode', 'mcp__workspace__read_file'].includes(name) && !isExternalMcpToolName(name)))
346
+ throw new Error('Tool allowlist');
347
+ if (!Number.isSafeInteger(value.sessionBytes) || value.sessionBytes < 1 || value.sessionBytes > 67108864)
348
+ throw new Error('Session bound');
349
+ sessionBytes = value.sessionBytes;
350
+ snapshotPolicy = { selection: value.selection, tools: value.toolNames, limits: { sessionBytes } };
351
+ const initToolArgsBytes = value.toolArgsBytes ?? 65536;
352
+ if (!Number.isSafeInteger(initToolArgsBytes) || initToolArgsBytes < 1 || initToolArgsBytes > 4194304)
353
+ throw new Error('Tool argument bound');
354
+ toolArgsBytes = initToolArgsBytes;
355
+ if (!Number.isSafeInteger(value.toolResultBytes) || value.toolResultBytes < 1 || value.toolResultBytes > 4194304)
356
+ throw new Error('Tool result bound');
357
+ toolResultBytes = value.toolResultBytes;
358
+ if (!Number.isSafeInteger(value.outputTokens) || value.outputTokens < 1 || value.outputTokens > 262144)
359
+ throw new Error('output bound');
360
+ outputTokens = value.outputTokens;
361
+ const maxRequestBytes = value.contextPolicy?.maxRequestBytes ?? 65536;
362
+ const workingWindowCeiling = value.contextPolicy?.workingWindowCeiling ?? 98304;
363
+ const serviceMargin = value.contextPolicy?.serviceMargin ?? 32768;
364
+ const toolReserve = value.contextPolicy?.toolReserve ?? 16384;
365
+ if (!Number.isSafeInteger(maxRequestBytes) || maxRequestBytes < 1 || maxRequestBytes > 16777216)
366
+ throw new Error('request byte bound');
367
+ if (!Number.isSafeInteger(workingWindowCeiling) || workingWindowCeiling < 1 || workingWindowCeiling > 16777216)
368
+ throw new Error('window bound');
369
+ if (!Number.isSafeInteger(serviceMargin) || serviceMargin < 32768 || !Number.isSafeInteger(toolReserve) || toolReserve < 16384)
370
+ throw new Error('margin bound');
371
+ const directory = await mkdtemp(join(process.env.TMPDIR || '/tmp', 'loopwright-sdk-'));
372
+ runtime = await ModelRuntime.create({ credentials: { read: async () => ({ type: 'api_key', key: 'loopwright-non-secret-placeholder' }), list: async () => [], modify: async () => undefined, delete: async () => { } }, modelsPath: null, refreshOnCreate: false, allowModelNetwork: false });
373
+ if (value.selection.customProvider) {
374
+ const cp = value.selection.customProvider;
375
+ runtime.registerProvider(value.selection.provider, {
376
+ baseUrl: cp.baseUrl,
377
+ api: value.selection.api,
378
+ apiKey: 'loopwright-non-secret-placeholder',
379
+ ...(cp.authHeader !== undefined ? { authHeader: cp.authHeader } : {}),
380
+ ...(cp.headers ? { headers: { ...cp.headers } } : {}),
381
+ models: [{
382
+ id: value.selection.model,
383
+ name: value.selection.model,
384
+ reasoning: cp.reasoning,
385
+ input: ['text'],
386
+ cost: {
387
+ input: cp.cost?.input ?? 0,
388
+ output: cp.cost?.output ?? 0,
389
+ cacheRead: cp.cost?.cacheRead ?? 0,
390
+ cacheWrite: cp.cost?.cacheWrite ?? 0,
391
+ },
392
+ contextWindow: cp.contextWindow,
393
+ maxTokens: cp.maxTokens,
394
+ ...(cp.thinkingLevelMap ? { thinkingLevelMap: { ...cp.thinkingLevelMap } } : {}),
395
+ ...(cp.compat ? { compat: { ...cp.compat } } : {}),
396
+ ...(cp.samplingParams ? { samplingParams: { ...cp.samplingParams } } : {}),
397
+ ...(cp.samplingParamsByThinkingLevel ? { samplingParamsByThinkingLevel: structuredClone(cp.samplingParamsByThinkingLevel) } : {}),
398
+ }],
399
+ });
400
+ }
401
+ const model = runtime.getModel(value.selection.provider, value.selection.model);
402
+ if (!model || model.api !== value.selection.api) {
403
+ await channel.send('refused', { code: 'MODEL_UNAVAILABLE' });
404
+ return;
405
+ }
406
+ const compiled = await compileTrustedFactories([{ name: 'loopwright-authorization', factory: authorization }, ...(value.toolNames.includes('codemode') || value.mcp.names.length ? nativeMcpFactories({ tools: value.toolNames, mcp: value.mcp }, mcp) : [])], value.systemPrompt);
407
+ const customTools = options.customTools ?? bridgedTools(value.toolNames, executeTool, value.externalMcpTools ?? [], { isHostSession });
408
+ const created = await createAgentSession({ cwd: '/workspace/candidate', agentDir: directory, modelRuntime: runtime, model, thinkingLevel: value.selection.thinking, tools: options.customTools ? customTools.map(tool => tool.name) : value.toolNames, customTools,
409
+ resourceLoader: compiled.loader, settingsManager: compiled.settings, sessionManager: SessionManager.inMemory('/workspace/candidate', undefined, restoredEntries ?? undefined) });
410
+ session = created.session;
411
+ safeSnapshot = restoredEntries !== null;
412
+ restoredEntries = null;
413
+ session.subscribe(event => {
414
+ if (event.type === 'agent_end')
415
+ native = { ...native, agentEnds: native.agentEnds + 1 };
416
+ if (event.type === 'agent_settled')
417
+ native = { ...native, agentSettles: native.agentSettles + 1, aborted: event.aborted };
418
+ if (event.type === 'compaction_end' && event.reason === 'manual')
419
+ native = { ...native, compactionEnds: native.compactionEnds + 1, compacted: !!event.result && !event.aborted && !event.willRetry, aborted: event.aborted };
420
+ if (event.type === 'queue_update')
421
+ queued = event.steering.length + event.followUp.length;
422
+ });
423
+ if (created.extensionsResult !== compiled.extensions || created.extensionsResult.runtime !== compiled.extensions.runtime)
424
+ throw new Error('native runtime binding');
425
+ if (session.thinkingLevel !== value.selection.thinking || session.model?.id !== model.id) {
426
+ await channel.send('refused', { code: 'THINKING_UNSUPPORTED' });
427
+ return;
428
+ }
429
+ const maxBodyBytes = Math.min(maxRequestBytes, Math.min(model.contextWindow, workingWindowCeiling) - serviceMargin - outputTokens - toolReserve);
430
+ session.agent.streamFunction = (selectedModel, context, streamOptions = {}) => {
431
+ historyBound();
432
+ const invocation = ++ordinal;
433
+ let used = false, slot = null;
434
+ const grant = new Promise((resolve, reject) => { invocationWaiters.set(invocation, { resolve, reject }); });
435
+ void grant.catch(() => { });
436
+ void channel.send('invoke', { invocation, provider: value.selection.provider, model: value.selection.model, thinking: value.selection.thinking, api: value.selection.api }).catch(() => channel.fail());
437
+ const fetch = async (url, init) => {
438
+ if (used || !active || pendingFetch)
439
+ throw new Error('unadmitted fetch');
440
+ used = true;
441
+ slot = await grant;
442
+ if (typeof init?.body !== 'string')
443
+ throw new Error('unsupported final body');
444
+ const bytes = Buffer.from(init.body);
445
+ if (bytes.length > maxRequestBytes)
446
+ throw new Error('request bytes');
447
+ const headers = new Headers(init.headers);
448
+ const auth = headers.get('authorization');
449
+ if (auth && auth !== 'Bearer loopwright-non-secret-placeholder')
450
+ throw new Error('unexpected SDK credential');
451
+ headers.delete('authorization');
452
+ const xApiKey = headers.get('x-api-key');
453
+ if (xApiKey && xApiKey !== 'loopwright-non-secret-placeholder')
454
+ throw new Error('unexpected SDK credential');
455
+ headers.delete('x-api-key');
456
+ const response = new Promise((resolve, reject) => { pendingFetch = { invocation, slot: slot, resolve, reject }; });
457
+ const [, answer] = await Promise.all([channel.sendBytes('fetch', { invocation, slot, url: String(url), method: init.method, headers: Object.fromEntries(headers) }, bytes), response]);
458
+ return answer;
459
+ };
460
+ const restStreamOptions = { ...streamOptions };
461
+ delete restStreamOptions.reasoning;
462
+ const reasoningPart = value.selection.thinking === 'off' ? {} : { reasoning: value.selection.thinking };
463
+ const sendContext = pruneContextForBudget(context, maxBodyBytes, value.selection.api);
464
+ const native = runtime.streamSimple(selectedModel, sendContext, { ...restStreamOptions, ...reasoningPart, fetch: fetch, apiKey: 'loopwright-non-secret-placeholder', env: {}, maxTokens: outputTokens, maxRetries: 0, cacheRetention: 'short' });
465
+ const receipt = native.result().then(async (message) => {
466
+ const rawToolCalls = message.content.filter(block => block.type === 'toolCall');
467
+ const toolCalls = rawToolCalls.map(call => ({ id: call.id, name: call.name, argumentsDigest: toolArgumentsDigest(call.arguments) }));
468
+ if (slot)
469
+ for (const call of rawToolCalls)
470
+ roots.set(call.id, { invocation, slot, parentId: null, rawArguments: call.arguments });
471
+ let granted;
472
+ try {
473
+ granted = await grant;
474
+ }
475
+ catch {
476
+ return;
477
+ }
478
+ const candidate = { invocation, slot: granted, stopReason: message.stopReason, rawStopReason: message.rawStopReason ?? null,
479
+ usage: { input: message.usage.input, output: message.usage.output, cacheRead: message.usage.cacheRead, cacheWrite: message.usage.cacheWrite }, toolCalls };
480
+ const acknowledged = new Promise((resolve, reject) => { parsedWaiters.set(invocation, { resolve, reject }); });
481
+ void acknowledged.catch(() => { });
482
+ await channel.send('parsed', candidate);
483
+ await acknowledged;
484
+ });
485
+ receipts.push(receipt);
486
+ void receipt.catch(() => { });
487
+ return native;
488
+ };
489
+ await session.bindExtensions({});
490
+ await channel.send('ready', { selection: value.selection, contextWindow: model.contextWindow, maxTokens: model.maxTokens, thinkingLevel: session.thinkingLevel, node: process.version, platform: process.platform, architecture: process.arch });
491
+ return;
492
+ }
493
+ if (message.kind === 'invocation' || message.kind === 'invocation-denied') {
494
+ const value = message.body, waiter = invocationWaiters.get(value.invocation);
495
+ if (!waiter)
496
+ throw new Error('unknown invocation');
497
+ invocationWaiters.delete(value.invocation);
498
+ if (message.kind === 'invocation')
499
+ waiter.resolve(value.slot);
500
+ else
501
+ waiter.reject(new Error('Owner refused invocation'));
502
+ return;
503
+ }
504
+ if (message.kind === 'parsed-ack' || message.kind === 'parsed-denied') {
505
+ const value = message.body, waiter = parsedWaiters.get(value.invocation);
506
+ if (!waiter)
507
+ throw new Error('unknown parser ack');
508
+ parsedWaiters.delete(value.invocation);
509
+ if (message.kind === 'parsed-ack') {
510
+ native = { ...native, parserAcks: native.parserAcks + 1 };
511
+ waiter.resolve();
512
+ }
513
+ else
514
+ waiter.reject(new Error('Owner rejected parser receipt'));
515
+ return;
516
+ }
517
+ if (message.kind === 'root-tool-result') {
518
+ const value = message.body, waiter = toolWaiters.get(value.query);
519
+ if (!waiter)
520
+ throw new Error('unknown tool ack');
521
+ toolWaiters.delete(value.query);
522
+ waiter(value.admitted === true);
523
+ return;
524
+ }
525
+ if (message.kind === 'native-result-ack' || message.kind === 'native-result-denied') {
526
+ const value = message.body, waiter = nativeWaiters.get(value.query);
527
+ if (!waiter)
528
+ throw new Error('Unknown native result');
529
+ nativeWaiters.delete(value.query);
530
+ if (message.kind === 'native-result-ack')
531
+ waiter.resolve();
532
+ else
533
+ waiter.reject(new Error('Owner refused native result'));
534
+ return;
535
+ }
536
+ if (message.kind === 'mcp-message') {
537
+ const value = message.body;
538
+ if (value.bytes.length > 65536)
539
+ throw new Error('MCP message bound');
540
+ mcp.receive(parseJsonRpcMessage(parseBoundedJson(new TextDecoder('utf-8', { fatal: true }).decode(value.bytes))));
541
+ return;
542
+ }
543
+ if (message.kind === 'mcp-refused') {
544
+ mcp.refused();
545
+ return;
546
+ }
547
+ if (message.kind === 'tool-response') {
548
+ const value = message.body, waiter = executionWaiters.get(value.metadata.query);
549
+ if (!waiter || value.bytes.length > Math.max(65536, toolResultBytes * 2))
550
+ throw new Error('Unknown or oversized tool result');
551
+ executionWaiters.delete(value.metadata.query);
552
+ waiter.resolve(parseBoundedJson(new TextDecoder('utf-8', { fatal: true }).decode(value.bytes), { nodes: 262144, array: 4096, properties: 512 }));
553
+ return;
554
+ }
555
+ if (message.kind === 'tool-denied') {
556
+ const value = message.body, waiter = executionWaiters.get(value.query);
557
+ if (!waiter)
558
+ throw new Error('Unknown tool denial');
559
+ executionWaiters.delete(value.query);
560
+ waiter.reject(new Error('Owner refused tool execution'));
561
+ return;
562
+ }
563
+ if (message.kind === 'response') {
564
+ if (!pendingFetch)
565
+ throw new Error('unexpected response');
566
+ const value = message.body;
567
+ if (value.metadata.invocation !== pendingFetch.invocation || value.metadata.slot !== pendingFetch.slot)
568
+ throw new Error('response binding');
569
+ const pending = pendingFetch;
570
+ pendingFetch = null;
571
+ pending.resolve(new Response(Buffer.from(value.bytes), { status: value.metadata.status, headers: value.metadata.headers }));
572
+ return;
573
+ }
574
+ if (message.kind === 'deny') {
575
+ if (!pendingFetch)
576
+ throw new Error('unexpected denial');
577
+ const pending = pendingFetch;
578
+ pendingFetch = null;
579
+ pending.reject(new Error('Owner refused send'));
580
+ return;
581
+ }
582
+ if (message.kind === 'cancel') {
583
+ rejectPending();
584
+ await session?.abort();
585
+ return;
586
+ }
587
+ if (message.kind === 'close') {
588
+ rejectPending();
589
+ await session?.abort();
590
+ session?.dispose();
591
+ channel.close();
592
+ process.stdin.destroy();
593
+ return;
594
+ }
595
+ if (message.kind === 'execute' || message.kind === 'compact') {
596
+ if (!session || active)
597
+ throw new Error('session not ready');
598
+ active = true;
599
+ safeSnapshot = false;
600
+ receipts = [];
601
+ const firstInvocation = ordinal, command = ++commandOrdinal;
602
+ native = { agentEnds: 0, agentSettles: 0, aborted: false, compactionEnds: 0, compacted: false, parserAcks: 0 };
603
+ try {
604
+ if (message.kind === 'compact')
605
+ await session.compact();
606
+ else {
607
+ const value = message.body;
608
+ if (typeof value.text !== 'string' || value.text.trimStart().startsWith('/'))
609
+ throw new Error('literal text only');
610
+ await session.prompt(value.text);
611
+ }
612
+ await Promise.all(receipts);
613
+ const final = session.messages.filter(item => item.role === 'assistant').at(-1);
614
+ const completion = { ...native, idle: session.isIdle, invocations: ordinal - firstInvocation,
615
+ pending: queued + invocationWaiters.size + parsedWaiters.size + toolWaiters.size + executionWaiters.size + nativeWaiters.size + codemodeRoots.size + (pendingFetch ? 1 : 0) };
616
+ historyBound();
617
+ verifyNativeCompletion(completion, message.kind);
618
+ safeSnapshot = true;
619
+ active = false;
620
+ await channel.send('settled', { stopReason: final?.stopReason ?? null, native: completion, fixture: options.onSettled?.() ?? null });
621
+ }
622
+ finally {
623
+ if (command === commandOrdinal)
624
+ active = false;
625
+ }
626
+ return;
627
+ }
628
+ throw new Error('unsupported owner command');
629
+ }
630
+ channel.on('message', (message) => { void handle(message).catch(() => channel.send('refused', { code: 'PROVIDER_RESPONSE_INVALID' }).catch(() => channel.fail())); });
631
+ }
632
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
633
+ runSdkWorker(process.argv[2] ?? '');