@nordhavnhq/runner 0.0.0-stage → 0.96.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 (338) hide show
  1. package/bin/cliclick +0 -0
  2. package/dist/brand.d.ts +5 -0
  3. package/dist/brand.js +18 -0
  4. package/dist/cli/agent.d.ts +9 -0
  5. package/dist/cli/agent.js +39 -0
  6. package/dist/cli/folder.d.ts +2 -0
  7. package/dist/cli/folder.js +103 -0
  8. package/dist/cli/init.d.ts +2 -0
  9. package/dist/cli/init.js +67 -0
  10. package/dist/cli/lifecycle.d.ts +3 -0
  11. package/dist/cli/lifecycle.js +149 -0
  12. package/dist/cli/link.d.ts +20 -0
  13. package/dist/cli/link.js +312 -0
  14. package/dist/cli/logs.d.ts +2 -0
  15. package/dist/cli/logs.js +144 -0
  16. package/dist/cli/prompts.d.ts +7 -0
  17. package/dist/cli/prompts.js +13 -0
  18. package/dist/cli/ps.d.ts +2 -0
  19. package/dist/cli/ps.js +80 -0
  20. package/dist/cli/relay.d.ts +4 -0
  21. package/dist/cli/relay.js +29 -0
  22. package/dist/cli/update.d.ts +6 -0
  23. package/dist/cli/update.js +131 -0
  24. package/dist/config.d.ts +166 -0
  25. package/dist/config.js +383 -0
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.js +135 -0
  28. package/dist/legacy-env.d.ts +4 -0
  29. package/dist/legacy-env.js +32 -0
  30. package/dist/paths.d.ts +37 -0
  31. package/dist/paths.js +92 -0
  32. package/dist/runtime/daemon.d.ts +57 -0
  33. package/dist/runtime/daemon.js +159 -0
  34. package/dist/runtime/dev-start.d.ts +31 -0
  35. package/dist/runtime/dev-start.js +110 -0
  36. package/dist/runtime/file-claims.d.ts +58 -0
  37. package/dist/runtime/file-claims.js +174 -0
  38. package/dist/runtime/fs-browse.d.ts +18 -0
  39. package/dist/runtime/fs-browse.js +104 -0
  40. package/dist/runtime/fs-checkout.d.ts +154 -0
  41. package/dist/runtime/fs-checkout.js +1049 -0
  42. package/dist/runtime/fs-git-index.d.ts +22 -0
  43. package/dist/runtime/fs-git-index.js +133 -0
  44. package/dist/runtime/fs-git.d.ts +16 -0
  45. package/dist/runtime/fs-git.js +424 -0
  46. package/dist/runtime/git-relay.d.ts +46 -0
  47. package/dist/runtime/git-relay.js +259 -0
  48. package/dist/runtime/git-unpushed.d.ts +36 -0
  49. package/dist/runtime/git-unpushed.js +93 -0
  50. package/dist/runtime/node-version.d.ts +12 -0
  51. package/dist/runtime/node-version.js +39 -0
  52. package/dist/runtime/preview-tunnel.d.ts +16 -0
  53. package/dist/runtime/preview-tunnel.js +135 -0
  54. package/dist/runtime/processes.d.ts +61 -0
  55. package/dist/runtime/processes.js +408 -0
  56. package/dist/runtime/project-node-download.d.ts +17 -0
  57. package/dist/runtime/project-node-download.js +137 -0
  58. package/dist/runtime/project-node.d.ts +65 -0
  59. package/dist/runtime/project-node.js +223 -0
  60. package/dist/runtime/reap-scratch.d.ts +22 -0
  61. package/dist/runtime/reap-scratch.js +112 -0
  62. package/dist/runtime/spill-tool-output.d.ts +14 -0
  63. package/dist/runtime/spill-tool-output.js +84 -0
  64. package/dist/runtime/up.d.ts +6 -0
  65. package/dist/runtime/up.js +1324 -0
  66. package/dist/tools/_coerce.d.ts +2 -0
  67. package/dist/tools/_coerce.js +26 -0
  68. package/dist/tools/_commit-scope.d.ts +14 -0
  69. package/dist/tools/_commit-scope.js +70 -0
  70. package/dist/tools/_jail.d.ts +68 -0
  71. package/dist/tools/_jail.js +209 -0
  72. package/dist/tools/_output.d.ts +24 -0
  73. package/dist/tools/_output.js +48 -0
  74. package/dist/tools/_ripgrep.d.ts +46 -0
  75. package/dist/tools/_ripgrep.js +148 -0
  76. package/dist/tools/_run-worktree.d.ts +10 -0
  77. package/dist/tools/_run-worktree.js +27 -0
  78. package/dist/tools/_shell-words.d.ts +15 -0
  79. package/dist/tools/_shell-words.js +43 -0
  80. package/dist/tools/_sonar/astro.d.ts +6 -0
  81. package/dist/tools/_sonar/astro.js +45 -0
  82. package/dist/tools/_sonar/blade.d.ts +3 -0
  83. package/dist/tools/_sonar/blade.js +53 -0
  84. package/dist/tools/_sonar/build.d.ts +44 -0
  85. package/dist/tools/_sonar/build.js +147 -0
  86. package/dist/tools/_sonar/changed.d.ts +21 -0
  87. package/dist/tools/_sonar/changed.js +230 -0
  88. package/dist/tools/_sonar/diagnose/delta.d.ts +27 -0
  89. package/dist/tools/_sonar/diagnose/delta.js +64 -0
  90. package/dist/tools/_sonar/diagnose/detect.d.ts +39 -0
  91. package/dist/tools/_sonar/diagnose/detect.js +117 -0
  92. package/dist/tools/_sonar/diagnose/index.d.ts +13 -0
  93. package/dist/tools/_sonar/diagnose/index.js +217 -0
  94. package/dist/tools/_sonar/diagnose/lint-worker.d.ts +1 -0
  95. package/dist/tools/_sonar/diagnose/lint-worker.js +48 -0
  96. package/dist/tools/_sonar/diagnose/pool.d.ts +22 -0
  97. package/dist/tools/_sonar/diagnose/pool.js +149 -0
  98. package/dist/tools/_sonar/diagnose/rules.d.ts +23 -0
  99. package/dist/tools/_sonar/diagnose/rules.js +131 -0
  100. package/dist/tools/_sonar/diagnose/types-worker.d.ts +1 -0
  101. package/dist/tools/_sonar/diagnose/types-worker.js +183 -0
  102. package/dist/tools/_sonar/files.d.ts +14 -0
  103. package/dist/tools/_sonar/files.js +121 -0
  104. package/dist/tools/_sonar/graph.d.ts +34 -0
  105. package/dist/tools/_sonar/graph.js +164 -0
  106. package/dist/tools/_sonar/hints.d.ts +116 -0
  107. package/dist/tools/_sonar/hints.js +545 -0
  108. package/dist/tools/_sonar/imports.d.ts +38 -0
  109. package/dist/tools/_sonar/imports.js +275 -0
  110. package/dist/tools/_sonar/index.d.ts +79 -0
  111. package/dist/tools/_sonar/index.js +462 -0
  112. package/dist/tools/_sonar/keys.d.ts +5 -0
  113. package/dist/tools/_sonar/keys.js +248 -0
  114. package/dist/tools/_sonar/laravel-routes.d.ts +15 -0
  115. package/dist/tools/_sonar/laravel-routes.js +263 -0
  116. package/dist/tools/_sonar/laravel.d.ts +64 -0
  117. package/dist/tools/_sonar/laravel.js +326 -0
  118. package/dist/tools/_sonar/location.d.ts +33 -0
  119. package/dist/tools/_sonar/location.js +137 -0
  120. package/dist/tools/_sonar/map.d.ts +43 -0
  121. package/dist/tools/_sonar/map.js +212 -0
  122. package/dist/tools/_sonar/parse.d.ts +3 -0
  123. package/dist/tools/_sonar/parse.js +76 -0
  124. package/dist/tools/_sonar/prewarm.d.ts +9 -0
  125. package/dist/tools/_sonar/prewarm.js +96 -0
  126. package/dist/tools/_sonar/prisma.d.ts +9 -0
  127. package/dist/tools/_sonar/prisma.js +51 -0
  128. package/dist/tools/_sonar/refs.d.ts +7 -0
  129. package/dist/tools/_sonar/refs.js +273 -0
  130. package/dist/tools/_sonar/render.d.ts +66 -0
  131. package/dist/tools/_sonar/render.js +498 -0
  132. package/dist/tools/_sonar/resolve.d.ts +63 -0
  133. package/dist/tools/_sonar/resolve.js +402 -0
  134. package/dist/tools/_sonar/roots.d.ts +4 -0
  135. package/dist/tools/_sonar/roots.js +42 -0
  136. package/dist/tools/_sonar/routes.d.ts +5 -0
  137. package/dist/tools/_sonar/routes.js +247 -0
  138. package/dist/tools/_sonar/serve.d.ts +12 -0
  139. package/dist/tools/_sonar/serve.js +104 -0
  140. package/dist/tools/_sonar/suggest.d.ts +23 -0
  141. package/dist/tools/_sonar/suggest.js +114 -0
  142. package/dist/tools/_sonar/tests.d.ts +21 -0
  143. package/dist/tools/_sonar/tests.js +134 -0
  144. package/dist/tools/_sonar/tsconfig.d.ts +34 -0
  145. package/dist/tools/_sonar/tsconfig.js +188 -0
  146. package/dist/tools/_sonar/types.d.ts +81 -0
  147. package/dist/tools/_sonar/types.js +6 -0
  148. package/dist/tools/_symbols.d.ts +27 -0
  149. package/dist/tools/_symbols.js +461 -0
  150. package/dist/tools/_test-report.d.ts +47 -0
  151. package/dist/tools/_test-report.js +203 -0
  152. package/dist/tools/_touched.d.ts +44 -0
  153. package/dist/tools/_touched.js +195 -0
  154. package/dist/tools/_upload.d.ts +26 -0
  155. package/dist/tools/_upload.js +132 -0
  156. package/dist/tools/_worktree-setup.d.ts +29 -0
  157. package/dist/tools/_worktree-setup.js +204 -0
  158. package/dist/tools/browser.d.ts +2 -0
  159. package/dist/tools/browser.js +8 -0
  160. package/dist/tools/commit-guard.d.ts +44 -0
  161. package/dist/tools/commit-guard.js +165 -0
  162. package/dist/tools/computer.d.ts +13 -0
  163. package/dist/tools/computer.js +952 -0
  164. package/dist/tools/console.d.ts +44 -0
  165. package/dist/tools/console.js +739 -0
  166. package/dist/tools/database.d.ts +28 -0
  167. package/dist/tools/database.js +152 -0
  168. package/dist/tools/filesystem.d.ts +36 -0
  169. package/dist/tools/filesystem.js +506 -0
  170. package/dist/tools/git-tag.d.ts +10 -0
  171. package/dist/tools/git-tag.js +164 -0
  172. package/dist/tools/git.d.ts +52 -0
  173. package/dist/tools/git.js +1272 -0
  174. package/dist/tools/http.d.ts +12 -0
  175. package/dist/tools/http.js +119 -0
  176. package/dist/tools/image.d.ts +8 -0
  177. package/dist/tools/image.js +551 -0
  178. package/dist/tools/index.d.ts +13 -0
  179. package/dist/tools/index.js +58 -0
  180. package/dist/tools/outline.d.ts +20 -0
  181. package/dist/tools/outline.js +98 -0
  182. package/dist/tools/search.d.ts +17 -0
  183. package/dist/tools/search.js +391 -0
  184. package/dist/tools/sonar.d.ts +17 -0
  185. package/dist/tools/sonar.js +231 -0
  186. package/dist/tools/testing.d.ts +36 -0
  187. package/dist/tools/testing.js +273 -0
  188. package/dist/tools/upload.d.ts +8 -0
  189. package/dist/tools/upload.js +106 -0
  190. package/dist/tools/webcam.d.ts +28 -0
  191. package/dist/tools/webcam.js +967 -0
  192. package/dist/update/install-kind.d.ts +35 -0
  193. package/dist/update/install-kind.js +109 -0
  194. package/dist/update/install.d.ts +32 -0
  195. package/dist/update/install.js +187 -0
  196. package/dist/update/manifest.d.ts +18 -0
  197. package/dist/update/manifest.js +73 -0
  198. package/dist/update/peers.d.ts +18 -0
  199. package/dist/update/peers.js +43 -0
  200. package/dist/update/policy.d.ts +20 -0
  201. package/dist/update/policy.js +49 -0
  202. package/dist/update/supervisor.d.ts +25 -0
  203. package/dist/update/supervisor.js +83 -0
  204. package/dist/update/updater.d.ts +44 -0
  205. package/dist/update/updater.js +116 -0
  206. package/dist/utils/format.d.ts +50 -0
  207. package/dist/utils/format.js +190 -0
  208. package/dist/version.d.ts +17 -0
  209. package/dist/version.js +63 -0
  210. package/dist/version.json +5 -0
  211. package/node_modules/@nordhavnhq/runner-browser/dist/act-tools.d.ts +15 -0
  212. package/node_modules/@nordhavnhq/runner-browser/dist/act-tools.js +541 -0
  213. package/node_modules/@nordhavnhq/runner-browser/dist/cast-hub.d.ts +25 -0
  214. package/node_modules/@nordhavnhq/runner-browser/dist/cast-hub.js +137 -0
  215. package/node_modules/@nordhavnhq/runner-browser/dist/guard.d.ts +21 -0
  216. package/node_modules/@nordhavnhq/runner-browser/dist/guard.js +104 -0
  217. package/node_modules/@nordhavnhq/runner-browser/dist/host-protocol.d.ts +47 -0
  218. package/node_modules/@nordhavnhq/runner-browser/dist/host-protocol.js +41 -0
  219. package/node_modules/@nordhavnhq/runner-browser/dist/host.d.ts +18 -0
  220. package/node_modules/@nordhavnhq/runner-browser/dist/host.js +15 -0
  221. package/node_modules/@nordhavnhq/runner-browser/dist/index.d.ts +13 -0
  222. package/node_modules/@nordhavnhq/runner-browser/dist/index.js +28 -0
  223. package/node_modules/@nordhavnhq/runner-browser/dist/live-input.d.ts +74 -0
  224. package/node_modules/@nordhavnhq/runner-browser/dist/live-input.js +93 -0
  225. package/node_modules/@nordhavnhq/runner-browser/dist/live-open.d.ts +4 -0
  226. package/node_modules/@nordhavnhq/runner-browser/dist/live-open.js +57 -0
  227. package/node_modules/@nordhavnhq/runner-browser/dist/live.d.ts +148 -0
  228. package/node_modules/@nordhavnhq/runner-browser/dist/live.js +179 -0
  229. package/node_modules/@nordhavnhq/runner-browser/dist/local-live.d.ts +31 -0
  230. package/node_modules/@nordhavnhq/runner-browser/dist/local-live.js +262 -0
  231. package/node_modules/@nordhavnhq/runner-browser/dist/marks.d.ts +36 -0
  232. package/node_modules/@nordhavnhq/runner-browser/dist/marks.js +173 -0
  233. package/node_modules/@nordhavnhq/runner-browser/dist/page-events.d.ts +27 -0
  234. package/node_modules/@nordhavnhq/runner-browser/dist/page-events.js +82 -0
  235. package/node_modules/@nordhavnhq/runner-browser/dist/page-tools.d.ts +13 -0
  236. package/node_modules/@nordhavnhq/runner-browser/dist/page-tools.js +526 -0
  237. package/node_modules/@nordhavnhq/runner-browser/dist/params.d.ts +12 -0
  238. package/node_modules/@nordhavnhq/runner-browser/dist/params.js +32 -0
  239. package/node_modules/@nordhavnhq/runner-browser/dist/pick.d.ts +24 -0
  240. package/node_modules/@nordhavnhq/runner-browser/dist/pick.js +76 -0
  241. package/node_modules/@nordhavnhq/runner-browser/dist/session.d.ts +225 -0
  242. package/node_modules/@nordhavnhq/runner-browser/dist/session.js +507 -0
  243. package/node_modules/@nordhavnhq/runner-browser/dist/shots.d.ts +11 -0
  244. package/node_modules/@nordhavnhq/runner-browser/dist/shots.js +103 -0
  245. package/node_modules/@nordhavnhq/runner-browser/dist/show.d.ts +10 -0
  246. package/node_modules/@nordhavnhq/runner-browser/dist/show.js +190 -0
  247. package/node_modules/@nordhavnhq/runner-browser/dist/target.d.ts +37 -0
  248. package/node_modules/@nordhavnhq/runner-browser/dist/target.js +82 -0
  249. package/node_modules/@nordhavnhq/runner-browser/dist/tour.d.ts +19 -0
  250. package/node_modules/@nordhavnhq/runner-browser/dist/tour.js +178 -0
  251. package/node_modules/@nordhavnhq/runner-browser/package.json +18 -0
  252. package/node_modules/@nordhavnhq/runner-runtime/dist/config.d.ts +96 -0
  253. package/node_modules/@nordhavnhq/runner-runtime/dist/config.js +25 -0
  254. package/node_modules/@nordhavnhq/runner-runtime/dist/connection.d.ts +276 -0
  255. package/node_modules/@nordhavnhq/runner-runtime/dist/connection.js +507 -0
  256. package/node_modules/@nordhavnhq/runner-runtime/dist/constants.d.ts +48 -0
  257. package/node_modules/@nordhavnhq/runner-runtime/dist/constants.js +95 -0
  258. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/batch.d.ts +94 -0
  259. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/batch.js +219 -0
  260. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/calls.d.ts +136 -0
  261. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/calls.js +214 -0
  262. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/fs.d.ts +735 -0
  263. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/fs.js +40 -0
  264. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/loop.d.ts +362 -0
  265. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/loop.js +703 -0
  266. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/own-files.d.ts +105 -0
  267. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/own-files.js +202 -0
  268. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/phases.d.ts +204 -0
  269. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/phases.js +497 -0
  270. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/pins.d.ts +45 -0
  271. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/pins.js +176 -0
  272. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/processes.d.ts +74 -0
  273. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/processes.js +72 -0
  274. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/release-tag.d.ts +59 -0
  275. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/release-tag.js +117 -0
  276. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/replay.d.ts +62 -0
  277. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/replay.js +84 -0
  278. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/tool-names.d.ts +27 -0
  279. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/tool-names.js +80 -0
  280. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/version.d.ts +22 -0
  281. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/version.js +22 -0
  282. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/wire.d.ts +595 -0
  283. package/node_modules/@nordhavnhq/runner-runtime/dist/contract/wire.js +85 -0
  284. package/node_modules/@nordhavnhq/runner-runtime/dist/index.d.ts +43 -0
  285. package/node_modules/@nordhavnhq/runner-runtime/dist/index.js +50 -0
  286. package/node_modules/@nordhavnhq/runner-runtime/dist/inference.d.ts +135 -0
  287. package/node_modules/@nordhavnhq/runner-runtime/dist/inference.js +945 -0
  288. package/node_modules/@nordhavnhq/runner-runtime/dist/mcp.d.ts +67 -0
  289. package/node_modules/@nordhavnhq/runner-runtime/dist/mcp.js +47 -0
  290. package/node_modules/@nordhavnhq/runner-runtime/dist/platform.d.ts +21 -0
  291. package/node_modules/@nordhavnhq/runner-runtime/dist/platform.js +25 -0
  292. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/auto-load.d.ts +39 -0
  293. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/auto-load.js +67 -0
  294. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/background-commands.d.ts +61 -0
  295. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/background-commands.js +58 -0
  296. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/chat-run-ends.d.ts +23 -0
  297. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/chat-run-ends.js +42 -0
  298. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/duration.d.ts +6 -0
  299. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/duration.js +12 -0
  300. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/events.d.ts +150 -0
  301. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/events.js +36 -0
  302. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/extract-json.d.ts +14 -0
  303. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/extract-json.js +49 -0
  304. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-chat.d.ts +109 -0
  305. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-chat.js +894 -0
  306. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-task.d.ts +73 -0
  307. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-task.js +765 -0
  308. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/loop.d.ts +246 -0
  309. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/loop.js +621 -0
  310. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/messages.d.ts +88 -0
  311. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/messages.js +188 -0
  312. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/pending-steers.d.ts +15 -0
  313. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/pending-steers.js +10 -0
  314. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/phased-loop.d.ts +16 -0
  315. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/phased-loop.js +97 -0
  316. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/project-instructions.d.ts +22 -0
  317. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/project-instructions.js +47 -0
  318. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/run-tools.d.ts +26 -0
  319. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/run-tools.js +48 -0
  320. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/scratch-prompt.d.ts +8 -0
  321. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/scratch-prompt.js +19 -0
  322. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/side-question.d.ts +76 -0
  323. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/side-question.js +155 -0
  324. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/task-validation.d.ts +43 -0
  325. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/task-validation.js +37 -0
  326. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/unload-toolset.d.ts +49 -0
  327. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/unload-toolset.js +104 -0
  328. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/write-scope.d.ts +16 -0
  329. package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/write-scope.js +6 -0
  330. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/delegate.d.ts +86 -0
  331. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/delegate.js +141 -0
  332. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/index.d.ts +179 -0
  333. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/index.js +35 -0
  334. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/mcp.d.ts +21 -0
  335. package/node_modules/@nordhavnhq/runner-runtime/dist/tools/mcp.js +179 -0
  336. package/node_modules/@nordhavnhq/runner-runtime/package.json +18 -0
  337. package/package.json +33 -4
  338. package/README.md +0 -3
@@ -0,0 +1,621 @@
1
+ // MIRROR: studio apps/server/server/utils/internal-runtime/loop.ts
2
+ import { COMPACTION_MIN_ROUNDS, COMPACTION_RETRY_ROUNDS, compactionThresholdTokens, foldTranscript, roundsInChain, scheduleInMemoryCompaction, toolResultCaps, truncateToolResult, FORCE_SYNTHESIS_MAX_ATTEMPTS, FORCE_SYNTHESIS_MESSAGE, MAX_IDENTICAL_TOOL_CALLS, MAX_REPEAT_GUARD_HITS, MAX_TOOL_FAILURES_IN_A_ROW, RUN_STOPPED_PREFIX, writeRunHandoff, askedRunReply, isQuestionAsked, LOOP_CLOSING_TOOLS, LOOP_CLOSING_REFUSAL, STEER_SKIPPED_RESULT, roundCutResult, loopClosingMessage, isRunStoppedResult, repeatGuardKey, repeatGuardMessage, budgetStopOf, BUDGET_STOP_REPLY, HANDOFF_CALL_PURPOSE, earlyReadIndexes, isReadStep, PIN_NUDGE_AFTER_READS, PIN_NUDGE_NOTE, TODO_NUDGE_AFTER_CALLS, MAX_TODO_NUDGES, countsForTodoNudge, todosFromArgs, todoNudgeNote, openTodosAtEndNote, } from '../contract/loop.js';
3
+ import { CALL_TOOL, callResultCap, fitCallResult, readableCallResult } from '../contract/calls.js';
4
+ import { byToolName, orderRunTools, trackLoadedTools } from './run-tools.js';
5
+ import { SKILL_PIN_MAX_CHARS, applyPin, clipSkillPin, composeSystemPrompt, pinSlugFromArgs } from '../contract/pins.js';
6
+ import { buildAssistantToolCallsMessage, buildToolResultMessage } from './messages.js';
7
+ export { byToolName };
8
+ export async function runAgenticLoop(opts) {
9
+ const { systemPrompt, initialMessages, registry, callInference, callSummaryInference, spillToolOutput, limits, signal, toolChoice, omitToolsWhenEmpty, startedAtMs, hooks, compactInMemory, writeHandoff, closeOnLoop, nudgePins, nudgeTodos, } = opts;
10
+ // Tools that came into the registry mid-chain, with the call that brought
11
+ // them: they go last and deferred, so a load never breaks the cache prefix.
12
+ const loadedBy = new Map();
13
+ const executeTool = trackLoadedTools(registry, loadedBy, opts.executeTool);
14
+ const messages = [
15
+ { role: 'system', content: systemPrompt },
16
+ ...initialMessages,
17
+ ];
18
+ // Durable "Task context": skill playbooks the agent loads (auto) and facts
19
+ // it pins with studio_pin, rendered into the system message. It is the one
20
+ // channel that survives a fold into a summary, the fix for agents losing
21
+ // load-bearing knowledge and re-discovering it. Mirrors the studio side in
22
+ // server/utils/internal-runtime/loop.ts.
23
+ const basePrompt = systemPrompt;
24
+ const pinnedSkills = new Map();
25
+ const pinnedNotes = new Map(opts.initialPinnedNotes ?? []);
26
+ // The system message (with the Task-context pins) is rendered only where the
27
+ // chain starts: the first round and a fold into a summary. The transcript is
28
+ // append-only (see the agent contract's "Keeping the context bounded"): a
29
+ // system prompt rewritten mid-run re-caches everything after it and
30
+ // invalidates every thinking block. Until the next fold a pin is in the
31
+ // transcript already, as the studio_pin call itself.
32
+ let systemStale = true;
33
+ // The tools of the last round, so a closing call (the handoff, a fold) asks
34
+ // with the same prefix and is a cache read rather than a re-write.
35
+ let lastTools = [];
36
+ // What a fold into a summary keeps verbatim: the request this run works on.
37
+ const ask = [...initialMessages].reverse().find(m => m.role === 'user') ?? null;
38
+ // What earlier folds replaced, so the working set still names the whole run.
39
+ const folded = [];
40
+ // A fold the agent asked for with studio_compact, run at the next round.
41
+ let compactRequested = false;
42
+ // No fold before this round (set after one that failed).
43
+ let foldBlockedUntilRound = 0;
44
+ const totalUsage = { inputTokens: 0, outputTokens: 0, costMicros: 0 };
45
+ let finalReply = '';
46
+ let finalThoughts = null;
47
+ // Most recent non-empty assistant narration emitted alongside a tool-calls
48
+ // round. Salvaged as the final reply if the run ends with no authored text
49
+ // (see the force-synthesis fallback), so a blank close still reads as a
50
+ // wrap-up instead of the caller's canned "No output generated.".
51
+ let lastNarration = '';
52
+ let finalModel = '';
53
+ let rounds = 0;
54
+ // Default covers the one exit with no explicit break: the for-loop running
55
+ // out of rounds. Every `break` site overwrites it first.
56
+ let stopReason = 'max-rounds';
57
+ const startedAt = startedAtMs ?? Date.now();
58
+ // Fold one inference call's usage into the running totals. `contextTokens`
59
+ // is ASSIGNED (not summed): after the last call it holds that call's input
60
+ // size = the true end-of-run context occupancy. See AgenticUsage.
61
+ // `updatesContext: false` folds a SIDE call's spend into the totals without
62
+ // letting it define `contextTokens`. The summarization call's input is the
63
+ // large folded prefix, not the conversation the agent continues with - if it
64
+ // set the snapshot and the round's real call then failed, that inflated value
65
+ // would be persisted as the turn's context occupancy and spike the UI meter.
66
+ // Cost/token totals still accumulate: that call was really paid for.
67
+ const applyUsage = (r, opts = {}) => {
68
+ totalUsage.inputTokens += r.usage.inputTokens;
69
+ totalUsage.outputTokens += r.usage.outputTokens;
70
+ totalUsage.costMicros += r.usage.costMicros;
71
+ if (opts.updatesContext !== false)
72
+ totalUsage.contextTokens = r.usage.inputTokens;
73
+ if (r.usage.cacheCreationTokens) {
74
+ totalUsage.cacheCreationTokens = (totalUsage.cacheCreationTokens ?? 0) + r.usage.cacheCreationTokens;
75
+ }
76
+ if (r.usage.cacheReadTokens) {
77
+ totalUsage.cacheReadTokens = (totalUsage.cacheReadTokens ?? 0) + r.usage.cacheReadTokens;
78
+ }
79
+ if (r.model)
80
+ finalModel = r.model;
81
+ };
82
+ // Tracks how many times each (name, arguments) pair has been executed this
83
+ // run, for the repeat-call guard below.
84
+ const toolCallCounts = new Map();
85
+ // Loop breakers: how often the repeat guard had to step in, and how many
86
+ // tool calls in a row failed. Past either bound the run is going in
87
+ // circles and is ended, instead of burning tokens until a cap trips.
88
+ let repeatGuardHits = 0;
89
+ let failuresInARow = 0;
90
+ let loopReason = null;
91
+ let studioStop = null;
92
+ // Set once a question reached the person. The turn ends after this round:
93
+ // the answer is their next message, so there is nothing to wait for.
94
+ let askedUser = false;
95
+ // What the run says once it stopped on that question (`askedRunReply`).
96
+ let askedReply = '';
97
+ // Set when the studio refused a model call (402, the task's budget): the
98
+ // run stops with reason `budget` and still writes its handoff.
99
+ let budgetStop = null;
100
+ // The pin nudge: reads since the run started without one pin, once.
101
+ let pinnedAny = pinnedNotes.size > 0;
102
+ let readsWithoutPin = 0;
103
+ let pinNudged = false;
104
+ // The todo nudge: the last list the run set, and the calls since.
105
+ let lastTodos = null;
106
+ let callsSinceTodos = 0;
107
+ let todoNudges = 0;
108
+ // The close check: a reply ending the run with open todos, once per run.
109
+ let openTodosAtEndNudged = false;
110
+ /** A side call (forced synthesis, the closing round) the budget may refuse:
111
+ * null then, with `budgetStop` set, instead of failing the run. Keep in
112
+ * sync with the studio's internal loop. */
113
+ const unlessBudgetStops = async (call) => {
114
+ try {
115
+ return await call();
116
+ }
117
+ catch (err) {
118
+ const refused = budgetStopOf(err);
119
+ if (refused == null)
120
+ throw err;
121
+ budgetStop = refused;
122
+ return null;
123
+ }
124
+ };
125
+ /** Reuse the last thing the model actually authored - its most recent mid-loop
126
+ * narration, or failing that its reasoning - so a turn that ends with no
127
+ * authored text still surfaces a real wrap-up instead of the caller's canned
128
+ * fallback. No-op when a reply already exists. Called from every exit that can
129
+ * leave `finalReply` blank: the breaks at the top of the round (which fire
130
+ * mid-tool-work, where `finalReply` is still '') and the no-tool-calls close. */
131
+ const salvageAuthored = () => {
132
+ if (finalReply.trim())
133
+ return;
134
+ if (lastNarration.trim())
135
+ finalReply = lastNarration;
136
+ else if ((finalThoughts ?? '').trim())
137
+ finalReply = finalThoughts;
138
+ };
139
+ /** The safety net: fold the whole transcript into one summary and go on with
140
+ * a fresh chain (system prompt, the request, the summary) that replays no
141
+ * earlier turn and no earlier thinking block. Only between rounds; the rules
142
+ * and thresholds are the agent contract's (`foldTranscript`). Best-effort: a
143
+ * fold that fails leaves the transcript as it was. Keep in sync with the
144
+ * studio's internal-runtime loop. */
145
+ const compactTranscript = async () => {
146
+ const tools = lastTools;
147
+ const summary = await foldTranscript({
148
+ messages,
149
+ ask,
150
+ earlier: folded,
151
+ separateModel: !!callSummaryInference,
152
+ summarize: async (request, forceNoTools) => {
153
+ const res = await (callSummaryInference ?? callInference)({ messages: request, tools, toolChoice: forceNoTools ? 'none' : toolChoice }, signal);
154
+ // Side call: bill it, but never let it define the context snapshot.
155
+ applyUsage(res, { updatesContext: false });
156
+ return res.message;
157
+ },
158
+ });
159
+ if (!summary || signal.aborted)
160
+ return false;
161
+ folded.push(...messages.slice(1));
162
+ messages.splice(1, messages.length - 1, summary);
163
+ systemStale = true;
164
+ // A new chain: the loaded tools' results are folded away, so every tool
165
+ // is a plain definition again.
166
+ loadedBy.clear();
167
+ return true;
168
+ };
169
+ /** Cut a tool result that is too long for the conversation, once, as it is
170
+ * appended: the transcript is never rewritten later. A result the host kept
171
+ * whole (`spillToolOutput`) is cut hard and the note says where the copy
172
+ * is; one with no copy keeps the larger cap (`toolResultCaps`). The
173
+ * answers of `studio_call_agent` have a budget of their own, shared per
174
+ * answer (`fitCallResult`): they are what the call was placed for. */
175
+ const caps = toolResultCaps(limits.toolResultTruncateChars);
176
+ const callCap = callResultCap(limits.toolResultTruncateChars);
177
+ const fitToolResult = async (call, text) => {
178
+ if (call.name === CALL_TOOL) {
179
+ // The model reads a batch as one section per answer, the copy kept on
180
+ // disk too, so a ranged read or a grep finds `results[i]`.
181
+ const readable = readableCallResult(text);
182
+ if (readable.length <= callCap)
183
+ return readable;
184
+ const keptAt = spillToolOutput ? await spillToolOutput(call.id, call.name, readable).catch(() => null) : null;
185
+ return fitCallResult(readable, callCap, keptAt);
186
+ }
187
+ if (text.length <= caps.spilled)
188
+ return text;
189
+ const keptAt = spillToolOutput ? await spillToolOutput(call.id, call.name, text).catch(() => null) : null;
190
+ return keptAt ? truncateToolResult(text, caps.spilled, keptAt) : truncateToolResult(text, caps.kept);
191
+ };
192
+ /** The closing round of a task run that went in circles (see
193
+ * `loopClosingMessage`). Only `LOOP_CLOSING_TOOLS` run; the tools are the
194
+ * same as every round so the prompt cache stays a read. Keep in sync with
195
+ * the studio's internal loop. */
196
+ const runClosingRound = async (reason, tools, round) => {
197
+ messages.push({ role: 'user', content: loopClosingMessage(reason) });
198
+ const res = await unlessBudgetStops(() => callInference({ messages, tools, toolChoice }, signal));
199
+ if (!res)
200
+ return { asked: false, askedReply: '', ended: false, reply: '' };
201
+ applyUsage(res);
202
+ const reply = (res.message ?? '').trim();
203
+ if (!res.toolCalls?.length) {
204
+ if (reply)
205
+ messages.push({ role: 'assistant', content: reply });
206
+ return { asked: false, askedReply: '', ended: false, reply };
207
+ }
208
+ messages.push(buildAssistantToolCallsMessage(res.message, res.toolCalls, res.assistantContent));
209
+ let asked = false;
210
+ let askedReply = '';
211
+ for (const call of res.toolCalls) {
212
+ if (!LOOP_CLOSING_TOOLS.has(call.name)) {
213
+ messages.push(buildToolResultMessage(call.id, call.name, LOOP_CLOSING_REFUSAL, { isError: true }));
214
+ continue;
215
+ }
216
+ const outcome = await executeTool(call, registry.get(call.name));
217
+ messages.push(buildToolResultMessage(call.id, call.name, await fitToolResult(call, outcome.result), { isError: outcome.isError }));
218
+ if (isQuestionAsked(call.name, outcome)) {
219
+ asked = true;
220
+ askedReply = askedRunReply(call.name, call.arguments, reply, outcome.result);
221
+ }
222
+ }
223
+ const ended = !asked && !!hooks?.onAfterRound?.(round + 1)?.stop;
224
+ return { asked, askedReply, ended, reply };
225
+ };
226
+ for (let round = 0; round < limits.maxRounds; round++) {
227
+ rounds = round + 1;
228
+ if (signal.aborted) {
229
+ stopReason = 'aborted';
230
+ salvageAuthored();
231
+ break;
232
+ }
233
+ if (Date.now() - startedAt > limits.maxLoopMs) {
234
+ stopReason = 'max-loop-ms';
235
+ salvageAuthored();
236
+ break;
237
+ }
238
+ if (limits.maxCostMicros !== undefined && totalUsage.costMicros >= limits.maxCostMicros) {
239
+ stopReason = 'max-cost';
240
+ salvageAuthored();
241
+ break;
242
+ }
243
+ // The safety net, at a round boundary and before anything new is appended
244
+ // this round: once the last call's context passed the threshold (or the
245
+ // agent asked with studio_compact), fold the whole transcript into a
246
+ // summary. Nothing else ever rewrites the history.
247
+ const overCeiling = (totalUsage.contextTokens ?? 0) >= compactionThresholdTokens(limits.contextWindowTokens);
248
+ if ((overCeiling || compactRequested)
249
+ && round >= foldBlockedUntilRound
250
+ && roundsInChain(messages) >= COMPACTION_MIN_ROUNDS
251
+ && !signal.aborted) {
252
+ compactRequested = false;
253
+ if (!await compactTranscript())
254
+ foldBlockedUntilRound = round + COMPACTION_RETRY_ROUNDS;
255
+ }
256
+ if (hooks?.onRoundStart) {
257
+ const r = await hooks.onRoundStart(round);
258
+ if (r && r.injectUserMessage) {
259
+ messages.push({ role: 'user', content: r.injectUserMessage });
260
+ }
261
+ }
262
+ // Sorted by name, not in registry order: a tool loaded mid-turn lands at
263
+ // the end of the registry, while the next turn's dispatch ships the same
264
+ // set in its own order. The tools open the prompt cache's prefix, so any
265
+ // difference there re-writes the whole cache on the next turn's first call.
266
+ const allTools = orderRunTools(registry, loadedBy);
267
+ const tools = omitToolsWhenEmpty && allTools.length === 0 ? [] : allTools;
268
+ lastTools = tools;
269
+ // Render the durable Task-context block into the system message, only where
270
+ // a chain starts (see `systemStale`).
271
+ if (systemStale) {
272
+ messages[0] = { role: 'system', content: composeSystemPrompt(basePrompt, pinnedSkills, pinnedNotes) };
273
+ systemStale = false;
274
+ }
275
+ hooks?.onInferenceInput?.(round, messages, tools);
276
+ // Bounded by whatever is left of the turn, when the caller asked for it.
277
+ // `AbortSignal.any` keeps the caller's own signal live, so a user cancel
278
+ // still reads as a cancel: the two reasons must stay distinguishable or a
279
+ // budget cutoff would be settled as somebody pressing stop.
280
+ const budgetLeftMs = limits.maxLoopMs - (Date.now() - startedAt);
281
+ const callSignal = limits.abortInferenceAtBudget
282
+ ? AbortSignal.any([signal, AbortSignal.timeout(Math.max(1, budgetLeftMs))])
283
+ : signal;
284
+ let result;
285
+ try {
286
+ result = await callInference({ messages, tools, toolChoice }, callSignal);
287
+ }
288
+ catch (err) {
289
+ // The studio refused the call (the task's budget): stop and hand over.
290
+ const refused = budgetStopOf(err);
291
+ if (refused != null) {
292
+ stopReason = 'budget';
293
+ salvageAuthored();
294
+ if (!finalReply.trim())
295
+ finalReply = BUDGET_STOP_REPLY;
296
+ break;
297
+ }
298
+ // Only OUR budget timeout is handled here. The caller's signal being
299
+ // aborted means a user cancel or a shutdown, which must keep propagating:
300
+ // swallowing one would settle a stop as a budget cutoff.
301
+ if (signal.aborted || !limits.abortInferenceAtBudget || Date.now() - startedAt < limits.maxLoopMs)
302
+ throw err;
303
+ // The same outcome the between-rounds check produces, so the turn ends
304
+ // through the ordinary breaker path with its cutoff notice rather than as
305
+ // a failure. What the call had generated is gone: a reply is only ever
306
+ // read off a call that returned.
307
+ stopReason = 'max-loop-ms';
308
+ salvageAuthored();
309
+ break;
310
+ }
311
+ applyUsage(result);
312
+ hooks?.onInferenceOutput?.(round, result);
313
+ if (!result.toolCalls || result.toolCalls.length === 0) {
314
+ finalReply = result.message ?? '';
315
+ finalThoughts = result.thoughts;
316
+ if (hooks?.onEmptyToolCalls) {
317
+ const r = await hooks.onEmptyToolCalls(round, finalReply);
318
+ if (r && r.continueWithUserMessage) {
319
+ // Only replay the assistant turn when it actually said something -
320
+ // AI SDK v6 + Anthropic reject an empty text content block, so
321
+ // pushing `{ content: '' }` would make the very next inference call
322
+ // throw. The nudge (below) still runs, so a silent empty round gets
323
+ // a corrective user message and one more chance to produce a reply.
324
+ if (result.assistantContent?.length)
325
+ messages.push({ role: 'assistant', content: result.assistantContent });
326
+ else if (finalReply.trim())
327
+ messages.push({ role: 'assistant', content: finalReply });
328
+ messages.push({ role: 'user', content: r.continueWithUserMessage });
329
+ continue;
330
+ }
331
+ }
332
+ // Closing with open todos: review them first (do what is left, or tick
333
+ // it off), once per run. Keep in sync with the studio's loop.
334
+ const openTodosNote = nudgeTodos && !openTodosAtEndNudged && !signal.aborted && budgetStop == null
335
+ ? openTodosAtEndNote(lastTodos)
336
+ : null;
337
+ if (openTodosNote) {
338
+ openTodosAtEndNudged = true;
339
+ if (result.assistantContent?.length)
340
+ messages.push({ role: 'assistant', content: result.assistantContent });
341
+ else if (finalReply.trim())
342
+ messages.push({ role: 'assistant', content: finalReply });
343
+ messages.push({ role: 'user', content: openTodosNote });
344
+ continue;
345
+ }
346
+ // Reasoning-only round: the model closed its turn with no text and no
347
+ // tool call - typical of thinking models that "finish" inside reasoning
348
+ // after a tool result. The soft nudge above (if any) already had its
349
+ // chance with tools available; force one or more rounds with
350
+ // toolChoice:'none' so the model MUST emit a plain-text answer instead
351
+ // of shipping a blank turn the caller papers over with a canned
352
+ // fallback. A single re-roll can still come back empty for thinking
353
+ // models, so we retry the forced call up to FORCE_SYNTHESIS_MAX_ATTEMPTS
354
+ // times - re-calling with the same messages (no extra user turns)
355
+ // re-samples and usually yields text within a couple of tries.
356
+ if (!finalReply.trim() && !signal.aborted) {
357
+ messages.push({ role: 'user', content: FORCE_SYNTHESIS_MESSAGE });
358
+ // The first try keeps the chain's tool choice, so it is a cache read
359
+ // (tool use off sends no tools: another prefix, the whole run written
360
+ // again); only a try that reached for a tool anyway is followed by
361
+ // forced ones. Keep in sync with the studio's loop.
362
+ for (let attempt = 0; attempt < FORCE_SYNTHESIS_MAX_ATTEMPTS && !finalReply.trim() && !signal.aborted && budgetStop == null; attempt++) {
363
+ hooks?.onInferenceInput?.(round, messages, tools);
364
+ const forced = await unlessBudgetStops(() => callInference({ messages, tools, toolChoice: attempt === 0 ? toolChoice : 'none' }, signal));
365
+ if (!forced)
366
+ break;
367
+ applyUsage(forced);
368
+ hooks?.onInferenceOutput?.(round, forced);
369
+ if (forced.toolCalls?.length)
370
+ continue;
371
+ if (forced.message && forced.message.trim()) {
372
+ finalReply = forced.message;
373
+ finalThoughts = forced.thoughts ?? finalThoughts;
374
+ }
375
+ }
376
+ }
377
+ // Classify the close BEFORE salvage: `empty` means the model never
378
+ // authored a wrap-up itself, even if the salvage below fills finalReply
379
+ // with recycled narration. Task callers use this to avoid recording an
380
+ // unfinished run as a successful completion.
381
+ stopReason = budgetStop != null ? 'budget' : finalReply.trim() ? 'reply' : (signal.aborted ? 'aborted' : 'empty');
382
+ // Last-resort salvage: every forced re-roll still came back empty.
383
+ salvageAuthored();
384
+ break;
385
+ }
386
+ messages.push(buildAssistantToolCallsMessage(result.message, result.toolCalls, result.assistantContent));
387
+ const narration = (result.message || '').trim();
388
+ if (narration) {
389
+ lastNarration = narration;
390
+ if (hooks?.onAssistantNarration)
391
+ hooks.onAssistantNarration(round, narration, result.thoughts);
392
+ }
393
+ // When the round ended at the output-token cap (`finishReason === 'length'`)
394
+ // the model was cut off mid-generation, so the LAST tool call's arguments
395
+ // are incomplete. The provider/SDK often auto-closes the truncated JSON,
396
+ // which hides the truncation - executing it would write corrupt or partial
397
+ // content (e.g. a half-written file that no longer parses). Never run it;
398
+ // hand back a directive so the model redoes it in smaller pieces. Earlier
399
+ // tool calls in the same round were emitted in full and run normally.
400
+ const truncatedCallIdx = result.finishReason === 'length' && result.toolCalls.length > 0
401
+ ? result.toolCalls.length - 1
402
+ : -1;
403
+ // Reads the model asked for together start together (the agent contract's
404
+ // `earlyReadIndexes`: only reads before the first call that changes
405
+ // something); every result is still appended in order below. Keep in sync
406
+ // with the studio's internal loop.
407
+ const early = new Map();
408
+ // Answer the calls from `from` on without running them (a steer when
409
+ // `kind` is null, else a loop breaker or studio stop).
410
+ const answerRestOfRound = (from, kind) => {
411
+ for (const left of result.toolCalls.slice(from)) {
412
+ messages.push(buildToolResultMessage(left.id, left.name, kind ? roundCutResult(kind) : STEER_SKIPPED_RESULT, { isError: false }));
413
+ }
414
+ };
415
+ for (const i of earlyReadIndexes(result.toolCalls, name => registry.get(name), toolCallCounts, truncatedCallIdx === -1 ? undefined : truncatedCallIdx)) {
416
+ const call = result.toolCalls[i];
417
+ if (!call)
418
+ continue;
419
+ const started = executeTool(call, registry.get(call.name));
420
+ started.catch(() => undefined);
421
+ early.set(i, started);
422
+ }
423
+ for (let callIdx = 0; callIdx < result.toolCalls.length; callIdx++) {
424
+ const call = result.toolCalls[callIdx];
425
+ if (!call || signal.aborted)
426
+ break;
427
+ // A steer came in: answer every call left in the round without running
428
+ // it, so the next round (whose `onRoundStart` drains the steer) starts
429
+ // now. Every call still gets its result, as the provider requires. An
430
+ // early read already started just finishes unused.
431
+ if (hooks?.hasPendingSteer?.()) {
432
+ answerRestOfRound(callIdx, null);
433
+ break;
434
+ }
435
+ if (callIdx === truncatedCallIdx) {
436
+ messages.push(buildToolResultMessage(call.id, call.name, truncatedCallMessage(call.name), { isError: true }));
437
+ continue;
438
+ }
439
+ // studio_pin is handled in-loop (no MCP endpoint): it mutates the durable
440
+ // Task-context block re-injected into the system message each round.
441
+ // Intercepted before the repeat-guard so re-pinning is never blocked.
442
+ if (call.name === 'studio_pin') {
443
+ const pinResult = applyPin(call.arguments, pinnedNotes);
444
+ if (!pinResult.startsWith('Error:')) {
445
+ pinnedAny = true;
446
+ opts.onPinnedNotesChanged?.([...pinnedNotes]);
447
+ }
448
+ messages.push(buildToolResultMessage(call.id, call.name, pinResult, { isError: false }));
449
+ continue;
450
+ }
451
+ // studio_compact in a task has no persisted conversation to archive - its
452
+ // context IS this in-memory transcript. It schedules the safety-net fold
453
+ // for the next round boundary (folding here would cut the tool round this
454
+ // call is part of), rather than routing to the MCP endpoint, which would
455
+ // return "requires a conversation context". Placed before the
456
+ // repeat-guard so asking again is never blocked. Chat runs leave
457
+ // compactInMemory false and fall through to the MCP archival path.
458
+ if (compactInMemory && call.name === 'studio_compact') {
459
+ const compact = scheduleInMemoryCompaction(messages);
460
+ if (compact.scheduled)
461
+ compactRequested = true;
462
+ messages.push(buildToolResultMessage(call.id, call.name, compact.reply, { isError: false }));
463
+ continue;
464
+ }
465
+ // Repeat-call guard: once the agent has run this exact (name, arguments)
466
+ // call MAX_IDENTICAL_TOOL_CALLS times, stop executing it and return a
467
+ // directive instead of the (expensive) real result. Bounds runaway
468
+ // re-fetch loops without enlarging the context.
469
+ const dupKey = repeatGuardKey(call.name, call.arguments);
470
+ const priorCount = toolCallCounts.get(dupKey) ?? 0;
471
+ toolCallCounts.set(dupKey, priorCount + 1);
472
+ if (priorCount >= MAX_IDENTICAL_TOOL_CALLS) {
473
+ messages.push(buildToolResultMessage(call.id, call.name, repeatGuardMessage(call.name, priorCount + 1), { isError: false }));
474
+ if (++repeatGuardHits >= MAX_REPEAT_GUARD_HITS) {
475
+ loopReason = `it kept calling ${call.name} with the same arguments after being told to stop`;
476
+ answerRestOfRound(callIdx + 1, 'repeat-guard');
477
+ break;
478
+ }
479
+ continue;
480
+ }
481
+ const toolDef = registry.get(call.name);
482
+ const outcome = await (early.get(callIdx) ?? executeTool(call, toolDef));
483
+ if (outcome.isError && isRunStoppedResult(outcome.result)) {
484
+ studioStop = outcome.result.slice(RUN_STOPPED_PREFIX.length);
485
+ messages.push(buildToolResultMessage(call.id, call.name, outcome.result, { isError: true }));
486
+ answerRestOfRound(callIdx + 1, 'stopped');
487
+ break;
488
+ }
489
+ failuresInARow = outcome.isError ? failuresInARow + 1 : 0;
490
+ if (failuresInARow >= MAX_TOOL_FAILURES_IN_A_ROW) {
491
+ messages.push(buildToolResultMessage(call.id, call.name, await fitToolResult(call, outcome.result), { isError: true }));
492
+ loopReason = `its last ${failuresInARow} tool calls all failed (the last one: ${call.name})`;
493
+ answerRestOfRound(callIdx + 1, 'failures');
494
+ break;
495
+ }
496
+ // Cut once, as it arrives: the transcript is never rewritten later.
497
+ // Images go as they are appended, never dropped afterwards.
498
+ let capped = await fitToolResult(call, outcome.result);
499
+ if (nudgePins && !pinnedAny && !pinNudged && !outcome.isError && isReadStep(call.name) && ++readsWithoutPin >= PIN_NUDGE_AFTER_READS) {
500
+ pinNudged = true;
501
+ capped += PIN_NUDGE_NOTE;
502
+ }
503
+ if (call.name === 'studio_manage_todos') {
504
+ if (!outcome.isError) {
505
+ lastTodos = todosFromArgs(call.arguments) ?? lastTodos;
506
+ callsSinceTodos = 0;
507
+ }
508
+ }
509
+ else if (nudgeTodos && lastTodos && todoNudges < MAX_TODO_NUDGES && !outcome.isError && countsForTodoNudge(call.name) && ++callsSinceTodos >= TODO_NUDGE_AFTER_CALLS) {
510
+ const note = todoNudgeNote(lastTodos);
511
+ if (note) {
512
+ capped += note;
513
+ todoNudges++;
514
+ }
515
+ callsSinceTodos = 0;
516
+ }
517
+ const hasImages = !outcome.isError && outcome.images && outcome.images.length > 0;
518
+ messages.push(buildToolResultMessage(call.id, call.name, capped, hasImages ? { images: outcome.images } : { isError: outcome.isError }));
519
+ if (isQuestionAsked(call.name, outcome)) {
520
+ askedUser = true;
521
+ askedReply = askedRunReply(call.name, call.arguments, result.message, outcome.result);
522
+ }
523
+ // Auto-pin loaded skill playbooks: they are load-bearing for the whole
524
+ // task, so the Task-context block carries them through a fold into a
525
+ // summary.
526
+ if (call.name === 'studio_get_skill' && !outcome.isError) {
527
+ const slug = pinSlugFromArgs(call.arguments);
528
+ if (slug)
529
+ pinnedSkills.set(slug, clipSkillPin(outcome.result, SKILL_PIN_MAX_CHARS));
530
+ }
531
+ }
532
+ if (studioStop != null) {
533
+ stopReason = 'stopped';
534
+ finalReply = finalReply || `Stopped by the studio: ${studioStop}`;
535
+ break;
536
+ }
537
+ if (loopReason != null) {
538
+ // One closing round for a task: finish, or ask a person. Without a
539
+ // blocker of its own the studio opens one as the run ends.
540
+ let closing = null;
541
+ if (closeOnLoop && !signal.aborted) {
542
+ try {
543
+ closing = await runClosingRound(loopReason, tools, round);
544
+ }
545
+ catch {
546
+ // The work may be done already: a failing closing round must not fail the run.
547
+ }
548
+ }
549
+ if (budgetStop != null) {
550
+ stopReason = 'budget';
551
+ finalReply = finalReply || `Stopped: ${loopReason}.`;
552
+ break;
553
+ }
554
+ if (closing?.asked) {
555
+ stopReason = 'awaiting-user';
556
+ finalReply = closing.askedReply;
557
+ break;
558
+ }
559
+ if (closing?.ended) {
560
+ stopReason = 'hook-stop';
561
+ finalReply = closing.reply || finalReply;
562
+ break;
563
+ }
564
+ stopReason = 'loop';
565
+ finalReply = closing?.reply || finalReply || `Stopped: ${loopReason}.`;
566
+ break;
567
+ }
568
+ if (askedUser) {
569
+ // In a chat the question is the agent's message and anything narrated
570
+ // beside it would land below the card as a second one; a task's blocking
571
+ // question keeps the agent's own line instead (`askedRunReply`).
572
+ stopReason = 'awaiting-user';
573
+ finalReply = askedReply;
574
+ break;
575
+ }
576
+ const after = hooks?.onAfterRound?.(round);
577
+ if (after?.injectUserMessage) {
578
+ messages.push({ role: 'user', content: after.injectUserMessage });
579
+ }
580
+ if (after?.stop) {
581
+ stopReason = 'hook-stop';
582
+ if (after.overrideFinalReply) {
583
+ finalReply = after.overrideFinalReply;
584
+ }
585
+ else if (!finalReply) {
586
+ finalReply = (result.message ?? '').trim();
587
+ finalThoughts = result.thoughts;
588
+ }
589
+ break;
590
+ }
591
+ // If we exhaust maxRounds while still receiving tool calls, salvage the
592
+ // last assistant narration as the final reply so callers don't ship an
593
+ // empty message.
594
+ if (round === limits.maxRounds - 1) {
595
+ finalReply = result.message ?? finalReply;
596
+ finalThoughts = result.thoughts;
597
+ }
598
+ }
599
+ // The handoff call says what it is: past a spent budget the studio lets
600
+ // exactly this one through. It asks with the chain's own tools and tool
601
+ // choice, so the run stays a cache read (`writeRunHandoff`); text beside a
602
+ // tool call is narration, not notes. Keep in sync with the studio's loop.
603
+ const handoffChoice = typeof writeHandoff === 'function' ? writeHandoff(stopReason) : (writeHandoff ?? false);
604
+ const handoff = handoffChoice && !signal.aborted
605
+ ? await writeRunHandoff(messages, stopReason, async (asked, forceNoTools) => {
606
+ const res = await callInference({ messages: asked, tools: lastTools, toolChoice: forceNoTools ? 'none' : toolChoice, purpose: HANDOFF_CALL_PURPOSE }, signal);
607
+ applyUsage(res, { updatesContext: false });
608
+ return res.toolCalls?.length ? null : res.message;
609
+ }, folded, handoffChoice === 'review' ? 'review' : 'handoff')
610
+ : null;
611
+ return { finalReply, stopReason, finalThoughts, messages, totalUsage, finalModel, rounds, handoff };
612
+ }
613
+ // The guards both loops share (repeat guard, loop breakers, forced synthesis,
614
+ // run-stopped and ask-user markers) are the agent contract's (contract/loop.ts),
615
+ // the same code the studio's internal loop runs.
616
+ /** Directive fed back in place of executing a tool call whose arguments were
617
+ * truncated at the output-token cap (see the `truncatedCallIdx` gate). Keeps
618
+ * the corrupt/partial args off disk and tells the model how to recover. */
619
+ function truncatedCallMessage(toolName) {
620
+ return `[output truncated] Your response hit the output-token limit while still writing the arguments for ${toolName}, so this call was NOT executed - its arguments were incomplete and running it would have written corrupt or partial content. Redo it in smaller pieces: write the first chunk, then continue with follow-up calls (use append mode for large files) so each call stays well under the token limit.`;
621
+ }