agency-lang 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (574) hide show
  1. package/dist/lib/agents/agency-agent/agent.agency +307 -111
  2. package/dist/lib/agents/agency-agent/agent.js +1864 -524
  3. package/dist/lib/agents/agency-agent/images/images.js +2 -1
  4. package/dist/lib/agents/agency-agent/lib/config.agency +3 -3
  5. package/dist/lib/agents/agency-agent/lib/config.js +2 -1
  6. package/dist/lib/agents/agency-agent/lib/defaultPolicy.agency +33 -1
  7. package/dist/lib/agents/agency-agent/lib/defaultPolicy.js +164 -4
  8. package/dist/lib/agents/agency-agent/lib/utils.js +19 -1
  9. package/dist/lib/agents/agency-agent/prompts/codeSample.js +21 -2
  10. package/dist/lib/agents/agency-agent/shared.agency +101 -2
  11. package/dist/lib/agents/agency-agent/shared.js +541 -3
  12. package/dist/lib/agents/agency-agent/subagents/code.agency +77 -49
  13. package/dist/lib/agents/agency-agent/subagents/code.js +144 -96
  14. package/dist/lib/agents/agency-agent/subagents/explorer.agency +11 -13
  15. package/dist/lib/agents/agency-agent/subagents/explorer.js +49 -21
  16. package/dist/lib/agents/agency-agent/subagents/oracle.agency +11 -12
  17. package/dist/lib/agents/agency-agent/subagents/oracle.js +49 -19
  18. package/dist/lib/agents/agency-agent/subagents/research.agency +9 -3
  19. package/dist/lib/agents/agency-agent/subagents/research.js +21 -6
  20. package/dist/lib/agents/agency-agent/subagents/review.agency +6 -6
  21. package/dist/lib/agents/agency-agent/subagents/review.js +28 -61
  22. package/dist/lib/agents/agency-agent/tests/agentTurn.agency +15 -0
  23. package/dist/lib/agents/agency-agent/{subagents/oneShot.js → tests/agentTurn.js} +78 -76
  24. package/dist/lib/agents/agency-agent/tests/agentTurn.test.json +12 -0
  25. package/dist/lib/agents/agency-agent/tests/execPolicy.agency +46 -0
  26. package/dist/lib/agents/agency-agent/tests/execPolicy.js +795 -0
  27. package/dist/lib/agents/agency-agent/tests/execPolicy.test.json +34 -0
  28. package/dist/lib/agents/agency-agent/tests/toolWiring.agency +60 -0
  29. package/dist/lib/agents/agency-agent/tests/toolWiring.js +726 -0
  30. package/dist/lib/agents/agency-agent/tests/toolWiring.test.json +32 -0
  31. package/dist/lib/agents/docs/cli/cli/eval-judge.md +7 -7
  32. package/dist/lib/agents/docs/cli/cli/eval.md +26 -91
  33. package/dist/lib/agents/docs/cli/cli/optimize.md +181 -0
  34. package/dist/lib/agents/docs/cli/eval-judge.md +7 -7
  35. package/dist/lib/agents/docs/cli/eval.md +26 -91
  36. package/dist/lib/agents/docs/cli/optimize.md +181 -0
  37. package/dist/lib/agents/docs/guide/effects-and-raises.md +156 -0
  38. package/dist/lib/agents/docs/guide/guide/effects-and-raises.md +156 -0
  39. package/dist/lib/agents/docs/guide/guide/handlers.md +2 -2
  40. package/dist/lib/agents/docs/guide/guide/observability.md +6 -0
  41. package/dist/lib/agents/docs/guide/guide/partial-application.md +26 -0
  42. package/dist/lib/agents/docs/guide/guide/policies.md +3 -3
  43. package/dist/lib/agents/docs/guide/guide/serving.md +1 -1
  44. package/dist/lib/agents/docs/guide/guide/structured-interrupts.md +8 -8
  45. package/dist/lib/agents/docs/guide/guide/troubleshooting.md +56 -1
  46. package/dist/lib/agents/docs/guide/guide/ts-interop.md +1 -1
  47. package/dist/lib/agents/docs/guide/handlers.md +2 -2
  48. package/dist/lib/agents/docs/guide/observability.md +6 -0
  49. package/dist/lib/agents/docs/guide/partial-application.md +26 -0
  50. package/dist/lib/agents/docs/guide/policies.md +3 -3
  51. package/dist/lib/agents/docs/guide/serving.md +1 -1
  52. package/dist/lib/agents/docs/guide/structured-interrupts.md +8 -8
  53. package/dist/lib/agents/docs/guide/troubleshooting.md +56 -1
  54. package/dist/lib/agents/docs/guide/ts-interop.md +1 -1
  55. package/dist/lib/agents/eval/goalJudge.agency +24 -0
  56. package/dist/lib/agents/eval/goalJudge.js +313 -0
  57. package/dist/lib/agents/{judge.js → eval/judge.js} +12 -7
  58. package/dist/lib/agents/eval/judgePairwise.js +313 -0
  59. package/dist/lib/agents/optimize/gepaReflect.agency +58 -0
  60. package/dist/lib/agents/optimize/gepaReflect.js +325 -0
  61. package/dist/lib/agents/{mutatePrompt.agency → optimize/mutatePrompt.agency} +7 -1
  62. package/dist/lib/agents/{mutatePrompt.js → optimize/mutatePrompt.js} +22 -10
  63. package/dist/lib/agents/policy/agent.agency +14 -14
  64. package/dist/lib/agents/policy/agent.js +72 -37
  65. package/dist/lib/agents/review/agent.js +12 -2
  66. package/dist/lib/analysis/interrupts.d.ts +3 -3
  67. package/dist/lib/analysis/interrupts.js +1 -1
  68. package/dist/lib/analysis/interrupts.test.js +6 -6
  69. package/dist/lib/backends/agencyGenerator.d.ts +2 -0
  70. package/dist/lib/backends/agencyGenerator.js +32 -5
  71. package/dist/lib/backends/anthropicApiKey.codegen.test.js +34 -0
  72. package/dist/lib/backends/maxToolResultChars.codegen.test.js +32 -0
  73. package/dist/lib/backends/raisesFormat.test.d.ts +1 -0
  74. package/dist/lib/backends/raisesFormat.test.js +59 -0
  75. package/dist/lib/backends/typescriptBuilder/assignmentEmitter.d.ts +5 -2
  76. package/dist/lib/backends/typescriptBuilder/assignmentEmitter.js +10 -7
  77. package/dist/lib/backends/typescriptBuilder/scopeManager.d.ts +11 -0
  78. package/dist/lib/backends/typescriptBuilder/scopeManager.js +20 -0
  79. package/dist/lib/backends/typescriptBuilder/scopeManager.test.d.ts +1 -0
  80. package/dist/lib/backends/typescriptBuilder/scopeManager.test.js +29 -0
  81. package/dist/lib/backends/typescriptBuilder.js +53 -22
  82. package/dist/lib/backends/typescriptGenerator/typeToString.d.ts +0 -3
  83. package/dist/lib/backends/typescriptGenerator/typeToString.js +19 -0
  84. package/dist/lib/cli/commands.js +30 -2
  85. package/dist/lib/cli/doc.js +11 -11
  86. package/dist/lib/cli/doc.test.js +9 -9
  87. package/dist/lib/cli/doctor.d.ts +5 -0
  88. package/dist/lib/cli/doctor.js +43 -0
  89. package/dist/lib/cli/eval/optimize.d.ts +14 -10
  90. package/dist/lib/cli/eval/optimize.js +137 -68
  91. package/dist/lib/cli/eval/optimize.test.js +132 -129
  92. package/dist/lib/cli/eval/run.d.ts +22 -15
  93. package/dist/lib/cli/eval/run.js +38 -26
  94. package/dist/lib/cli/eval/run.test.js +22 -22
  95. package/dist/lib/cli/evalJudge.d.ts +1 -1
  96. package/dist/lib/cli/evalJudge.js +24 -24
  97. package/dist/lib/cli/evalJudge.test.js +17 -17
  98. package/dist/lib/cli/interrupts.js +2 -2
  99. package/dist/lib/cli/interrupts.test.js +9 -9
  100. package/dist/lib/cli/policy.js +11 -11
  101. package/dist/lib/cli/runAgencyAgent.js +5 -1
  102. package/dist/lib/cli/runAgencyAgent.test.js +6 -3
  103. package/dist/lib/cli/serve.js +24 -15
  104. package/dist/lib/cli/util.d.ts +45 -1
  105. package/dist/lib/cli/util.js +31 -20
  106. package/dist/lib/cli/util.test.d.ts +1 -0
  107. package/dist/lib/cli/util.test.js +19 -0
  108. package/dist/lib/compilationUnit.d.ts +4 -4
  109. package/dist/lib/compilationUnit.js +13 -11
  110. package/dist/lib/compiler/compileClosure.d.ts +1 -1
  111. package/dist/lib/compiler/compileClosure.js +12 -5
  112. package/dist/lib/config.d.ts +30 -0
  113. package/dist/lib/config.js +11 -0
  114. package/dist/lib/eval/extract.d.ts +9 -0
  115. package/dist/lib/eval/extract.js +24 -7
  116. package/dist/lib/eval/extract.test.js +68 -28
  117. package/dist/lib/eval/ids.d.ts +1 -1
  118. package/dist/lib/eval/ids.js +2 -2
  119. package/dist/lib/eval/judge/pairwise.d.ts +3 -3
  120. package/dist/lib/eval/judge/pairwise.js +8 -8
  121. package/dist/lib/eval/judge/pairwise.test.js +6 -6
  122. package/dist/lib/eval/judge/suite.d.ts +8 -8
  123. package/dist/lib/eval/judge/suite.js +42 -41
  124. package/dist/lib/eval/judge/suite.test.js +37 -37
  125. package/dist/lib/eval/judge/types.d.ts +6 -5
  126. package/dist/lib/eval/loadInputs.d.ts +11 -0
  127. package/dist/lib/eval/loadInputs.js +97 -0
  128. package/dist/lib/eval/loadInputs.test.d.ts +1 -0
  129. package/dist/lib/eval/loadInputs.test.js +79 -0
  130. package/dist/lib/eval/readRun.d.ts +5 -5
  131. package/dist/lib/eval/readRun.js +12 -12
  132. package/dist/lib/eval/readRun.test.js +37 -37
  133. package/dist/lib/eval/runArtifacts.d.ts +18 -18
  134. package/dist/lib/eval/runArtifacts.js +40 -39
  135. package/dist/lib/eval/runArtifacts.test.js +29 -29
  136. package/dist/lib/eval/{runEvalTask.d.ts → runEvalInput.d.ts} +13 -13
  137. package/dist/lib/eval/{runEvalTask.js → runEvalInput.js} +15 -14
  138. package/dist/lib/eval/runTypes.d.ts +22 -8
  139. package/dist/lib/eval/statelogParser.d.ts +1 -1
  140. package/dist/lib/eval/statelogParser.js +2 -2
  141. package/dist/lib/eval/statelogParser.test.js +2 -2
  142. package/dist/lib/eval/types.d.ts +5 -5
  143. package/dist/lib/importPaths.d.ts +5 -0
  144. package/dist/lib/importPaths.js +7 -0
  145. package/dist/lib/ir/builders.d.ts +1 -1
  146. package/dist/lib/ir/builders.js +2 -2
  147. package/dist/lib/ir/prettyPrint.js +9 -0
  148. package/dist/lib/ir/prettyPrint.test.js +11 -0
  149. package/dist/lib/ir/tsIR.d.ts +5 -0
  150. package/dist/lib/logsViewer/conversation.js +4 -1
  151. package/dist/lib/logsViewer/conversation.test.js +18 -0
  152. package/dist/lib/logsViewer/follow.js +16 -2
  153. package/dist/lib/logsViewer/follow.test.js +31 -0
  154. package/dist/lib/logsViewer/help.js +2 -1
  155. package/dist/lib/logsViewer/input.js +42 -4
  156. package/dist/lib/logsViewer/input.test.js +51 -18
  157. package/dist/lib/logsViewer/render.d.ts +1 -0
  158. package/dist/lib/logsViewer/render.e2e.test.d.ts +1 -0
  159. package/dist/lib/logsViewer/render.e2e.test.js +156 -0
  160. package/dist/lib/logsViewer/render.flatten.test.d.ts +1 -0
  161. package/dist/lib/logsViewer/render.flatten.test.js +240 -0
  162. package/dist/lib/logsViewer/render.js +112 -33
  163. package/dist/lib/logsViewer/render.test.js +38 -0
  164. package/dist/lib/logsViewer/search.js +41 -12
  165. package/dist/lib/logsViewer/search.test.js +84 -0
  166. package/dist/lib/logsViewer/summary.js +146 -15
  167. package/dist/lib/logsViewer/summary.test.js +151 -0
  168. package/dist/lib/logsViewer/tree.js +31 -31
  169. package/dist/lib/logsViewer/tree.test.js +47 -17
  170. package/dist/lib/lsp/builtinHover.js +2 -2
  171. package/dist/lib/lsp/diagnostics.js +2 -2
  172. package/dist/lib/lsp/semantics.d.ts +3 -3
  173. package/dist/lib/lsp/semantics.js +20 -20
  174. package/dist/lib/optimize/artifacts.d.ts +1 -1
  175. package/dist/lib/optimize/artifacts.js +1 -1
  176. package/dist/lib/optimize/artifacts.test.js +3 -3
  177. package/dist/lib/optimize/baseOptimizer.d.ts +109 -0
  178. package/dist/lib/optimize/baseOptimizer.js +217 -0
  179. package/dist/lib/optimize/baseOptimizer.test.d.ts +1 -0
  180. package/dist/lib/optimize/baseOptimizer.test.js +180 -0
  181. package/dist/lib/optimize/candidatePool.d.ts +17 -0
  182. package/dist/lib/optimize/candidatePool.js +20 -0
  183. package/dist/lib/optimize/candidatePool.test.d.ts +1 -0
  184. package/dist/lib/optimize/candidatePool.test.js +25 -0
  185. package/dist/lib/optimize/evalCache.d.ts +11 -0
  186. package/dist/lib/optimize/evalCache.js +16 -0
  187. package/dist/lib/optimize/evalCache.test.d.ts +1 -0
  188. package/dist/lib/optimize/evalCache.test.js +28 -0
  189. package/dist/lib/optimize/gepaReflect.d.ts +9 -0
  190. package/dist/lib/optimize/gepaReflect.js +9 -0
  191. package/dist/lib/optimize/gepaReflect.test.d.ts +1 -0
  192. package/dist/lib/optimize/gepaReflect.test.js +20 -0
  193. package/dist/lib/optimize/goalJudgeFile.d.ts +12 -0
  194. package/dist/lib/optimize/goalJudgeFile.js +17 -0
  195. package/dist/lib/optimize/goalJudgeFile.test.d.ts +1 -0
  196. package/dist/lib/optimize/goalJudgeFile.test.js +18 -0
  197. package/dist/lib/optimize/gradeBreakdown.d.ts +22 -0
  198. package/dist/lib/optimize/gradeBreakdown.js +20 -0
  199. package/dist/lib/optimize/gradeBreakdown.test.d.ts +1 -0
  200. package/dist/lib/optimize/gradeBreakdown.test.js +31 -0
  201. package/dist/lib/optimize/grading/agencyRunner.d.ts +31 -0
  202. package/dist/lib/optimize/grading/agencyRunner.js +46 -0
  203. package/dist/lib/optimize/grading/agencyRunner.test.d.ts +1 -0
  204. package/dist/lib/optimize/grading/agencyRunner.test.js +19 -0
  205. package/dist/lib/optimize/grading/aggregate.d.ts +10 -0
  206. package/dist/lib/optimize/grading/aggregate.js +23 -0
  207. package/dist/lib/optimize/grading/aggregate.test.d.ts +1 -0
  208. package/dist/lib/optimize/grading/aggregate.test.js +33 -0
  209. package/dist/lib/optimize/grading/baseGrader.d.ts +26 -0
  210. package/dist/lib/optimize/grading/baseGrader.js +52 -0
  211. package/dist/lib/optimize/grading/baseGrader.test.d.ts +1 -0
  212. package/dist/lib/optimize/grading/baseGrader.test.js +63 -0
  213. package/dist/lib/optimize/grading/functionGrader.d.ts +32 -0
  214. package/dist/lib/optimize/grading/functionGrader.js +63 -0
  215. package/dist/lib/optimize/grading/functionGrader.test.d.ts +1 -0
  216. package/dist/lib/optimize/grading/functionGrader.test.js +49 -0
  217. package/dist/lib/optimize/grading/getPath.d.ts +8 -0
  218. package/dist/lib/optimize/grading/getPath.js +26 -0
  219. package/dist/lib/optimize/grading/getPath.test.d.ts +1 -0
  220. package/dist/lib/optimize/grading/getPath.test.js +29 -0
  221. package/dist/lib/optimize/grading/grade.d.ts +7 -0
  222. package/dist/lib/optimize/grading/grade.js +10 -0
  223. package/dist/lib/optimize/grading/grade.test.d.ts +1 -0
  224. package/dist/lib/optimize/grading/grade.test.js +15 -0
  225. package/dist/lib/optimize/grading/graders/builtinGraders.d.ts +34 -0
  226. package/dist/lib/optimize/grading/graders/builtinGraders.js +104 -0
  227. package/dist/lib/optimize/grading/graders/builtinGraders.test.d.ts +1 -0
  228. package/dist/lib/optimize/grading/graders/builtinGraders.test.js +77 -0
  229. package/dist/lib/optimize/grading/graders/humanGrader.d.ts +36 -0
  230. package/dist/lib/optimize/grading/graders/humanGrader.js +75 -0
  231. package/dist/lib/optimize/grading/graders/humanGrader.test.d.ts +1 -0
  232. package/dist/lib/optimize/grading/graders/humanGrader.test.js +70 -0
  233. package/dist/lib/optimize/grading/graders/llmJudge.d.ts +17 -0
  234. package/dist/lib/optimize/grading/graders/llmJudge.js +36 -0
  235. package/dist/lib/optimize/grading/graders/llmJudge.test.d.ts +1 -0
  236. package/dist/lib/optimize/grading/graders/llmJudge.test.js +47 -0
  237. package/dist/lib/optimize/grading/scorecard.d.ts +29 -0
  238. package/dist/lib/optimize/grading/scorecard.js +37 -0
  239. package/dist/lib/optimize/grading/scorecard.test.d.ts +1 -0
  240. package/dist/lib/optimize/grading/scorecard.test.js +105 -0
  241. package/dist/lib/optimize/grading/types.d.ts +51 -0
  242. package/dist/lib/optimize/grading/types.js +1 -0
  243. package/dist/lib/optimize/gradingModule.d.ts +10 -0
  244. package/dist/lib/optimize/gradingModule.js +47 -0
  245. package/dist/lib/optimize/gradingModule.test.d.ts +1 -0
  246. package/dist/lib/optimize/gradingModule.test.js +40 -0
  247. package/dist/lib/optimize/inputs.d.ts +2 -0
  248. package/dist/lib/optimize/inputs.js +9 -0
  249. package/dist/lib/optimize/inputs.test.d.ts +1 -0
  250. package/dist/lib/optimize/inputs.test.js +17 -0
  251. package/dist/lib/optimize/loop.d.ts +2 -2
  252. package/dist/lib/optimize/loop.js +38 -35
  253. package/dist/lib/optimize/loop.test.js +11 -11
  254. package/dist/lib/optimize/mutator.d.ts +28 -4
  255. package/dist/lib/optimize/mutator.js +19 -13
  256. package/dist/lib/optimize/mutator.test.js +25 -12
  257. package/dist/lib/optimize/optimizer.d.ts +30 -0
  258. package/dist/lib/optimize/optimizer.js +1 -0
  259. package/dist/lib/optimize/optimizerModule.d.ts +9 -0
  260. package/dist/lib/optimize/optimizerModule.js +47 -0
  261. package/dist/lib/optimize/optimizerModule.test.d.ts +1 -0
  262. package/dist/lib/optimize/optimizerModule.test.js +30 -0
  263. package/dist/lib/optimize/optimizers/example.d.ts +48 -0
  264. package/dist/lib/optimize/optimizers/example.js +82 -0
  265. package/dist/lib/optimize/optimizers/example.test.d.ts +1 -0
  266. package/dist/lib/optimize/optimizers/example.test.js +67 -0
  267. package/dist/lib/optimize/optimizers/gepa.d.ts +37 -0
  268. package/dist/lib/optimize/optimizers/gepa.js +139 -0
  269. package/dist/lib/optimize/optimizers/gepa.test.d.ts +1 -0
  270. package/dist/lib/optimize/optimizers/gepa.test.js +131 -0
  271. package/dist/lib/optimize/optimizers/greedyReflective.d.ts +35 -0
  272. package/dist/lib/optimize/optimizers/greedyReflective.js +132 -0
  273. package/dist/lib/optimize/optimizers/greedyReflective.test.d.ts +1 -0
  274. package/dist/lib/optimize/optimizers/greedyReflective.test.js +134 -0
  275. package/dist/lib/optimize/pareto.d.ts +13 -0
  276. package/dist/lib/optimize/pareto.js +17 -0
  277. package/dist/lib/optimize/pareto.test.d.ts +1 -0
  278. package/dist/lib/optimize/pareto.test.js +28 -0
  279. package/dist/lib/optimize/public.d.ts +24 -0
  280. package/dist/lib/optimize/public.js +18 -0
  281. package/dist/lib/optimize/public.test.d.ts +1 -0
  282. package/dist/lib/optimize/public.test.js +22 -0
  283. package/dist/lib/optimize/reflectionFeedback.d.ts +9 -0
  284. package/dist/lib/optimize/reflectionFeedback.js +71 -0
  285. package/dist/lib/optimize/reflectionFeedback.test.d.ts +1 -0
  286. package/dist/lib/optimize/reflectionFeedback.test.js +58 -0
  287. package/dist/lib/optimize/registry.d.ts +5 -0
  288. package/dist/lib/optimize/registry.js +24 -0
  289. package/dist/lib/optimize/registry.test.d.ts +1 -0
  290. package/dist/lib/optimize/registry.test.js +38 -0
  291. package/dist/lib/optimize/report.d.ts +14 -0
  292. package/dist/lib/optimize/report.js +58 -0
  293. package/dist/lib/optimize/report.test.d.ts +1 -0
  294. package/dist/lib/optimize/report.test.js +32 -0
  295. package/dist/lib/optimize/reporter.d.ts +56 -6
  296. package/dist/lib/optimize/reporter.js +75 -3
  297. package/dist/lib/optimize/reporter.test.js +70 -11
  298. package/dist/lib/optimize/rng.d.ts +8 -0
  299. package/dist/lib/optimize/rng.js +34 -0
  300. package/dist/lib/optimize/rng.test.d.ts +1 -0
  301. package/dist/lib/optimize/rng.test.js +25 -0
  302. package/dist/lib/optimize/sourceMutator.d.ts +2 -0
  303. package/dist/lib/optimize/sourceMutator.js +16 -2
  304. package/dist/lib/optimize/sourceMutator.test.js +16 -2
  305. package/dist/lib/optimize/targets.d.ts +2 -0
  306. package/dist/lib/optimize/targets.js +4 -0
  307. package/dist/lib/optimize/types.d.ts +13 -3
  308. package/dist/lib/optimize/validationSplit.d.ts +8 -0
  309. package/dist/lib/optimize/validationSplit.js +14 -0
  310. package/dist/lib/optimize/validationSplit.test.d.ts +1 -0
  311. package/dist/lib/optimize/validationSplit.test.js +25 -0
  312. package/dist/lib/optimize/workspace.d.ts +42 -0
  313. package/dist/lib/optimize/workspace.js +100 -0
  314. package/dist/lib/optimize/workspace.test.d.ts +1 -0
  315. package/dist/lib/optimize/workspace.test.js +103 -0
  316. package/dist/lib/parser.js +2 -2
  317. package/dist/lib/parsers/effectSet.test.d.ts +1 -0
  318. package/dist/lib/parsers/effectSet.test.js +96 -0
  319. package/dist/lib/parsers/handleBlock.test.js +20 -1
  320. package/dist/lib/parsers/interruptStatement.test.js +29 -8
  321. package/dist/lib/parsers/parsers.d.ts +4 -0
  322. package/dist/lib/parsers/parsers.js +154 -14
  323. package/dist/lib/parsers/raiseStatement.test.d.ts +1 -0
  324. package/dist/lib/parsers/raiseStatement.test.js +64 -0
  325. package/dist/lib/parsers/raisesClause.test.d.ts +1 -0
  326. package/dist/lib/parsers/raisesClause.test.js +87 -0
  327. package/dist/lib/preprocessors/resolveReExports.js +39 -2
  328. package/dist/lib/preprocessors/resolveReExports.test.js +117 -0
  329. package/dist/lib/preprocessors/typescriptPreprocessor.d.ts +16 -0
  330. package/dist/lib/preprocessors/typescriptPreprocessor.js +127 -52
  331. package/dist/lib/runtime/agencyFunction.d.ts +14 -0
  332. package/dist/lib/runtime/agencyFunction.js +28 -0
  333. package/dist/lib/runtime/agencyFunction.test.js +49 -0
  334. package/dist/lib/runtime/agencyInterrupt.d.ts +3 -3
  335. package/dist/lib/runtime/agencyInterrupt.js +2 -2
  336. package/dist/lib/runtime/agencyInterrupt.test.js +18 -18
  337. package/dist/lib/runtime/call.js +13 -0
  338. package/dist/lib/runtime/configOverrides.d.ts +1 -0
  339. package/dist/lib/runtime/deterministicClient.d.ts +1 -1
  340. package/dist/lib/runtime/deterministicClient.test.js +9 -9
  341. package/dist/lib/runtime/errors.d.ts +63 -4
  342. package/dist/lib/runtime/errors.js +58 -6
  343. package/dist/lib/runtime/errors.test.js +70 -1
  344. package/dist/lib/runtime/guard.d.ts +3 -0
  345. package/dist/lib/runtime/guard.js +39 -1
  346. package/dist/lib/runtime/guard.test.js +18 -0
  347. package/dist/lib/runtime/index.d.ts +1 -1
  348. package/dist/lib/runtime/index.js +1 -1
  349. package/dist/lib/runtime/interrupts.d.ts +18 -4
  350. package/dist/lib/runtime/interrupts.js +37 -12
  351. package/dist/lib/runtime/interrupts.test.js +58 -5
  352. package/dist/lib/runtime/ipc.configOverrides.test.js +4 -4
  353. package/dist/lib/runtime/ipc.d.ts +2 -2
  354. package/dist/lib/runtime/ipc.js +3 -3
  355. package/dist/lib/runtime/isDebugger.test.js +3 -3
  356. package/dist/lib/runtime/node.js +3 -0
  357. package/dist/lib/runtime/policy.d.ts +1 -1
  358. package/dist/lib/runtime/policy.js +1 -1
  359. package/dist/lib/runtime/policy.test.js +22 -22
  360. package/dist/lib/runtime/prompt.d.ts +31 -0
  361. package/dist/lib/runtime/prompt.js +229 -63
  362. package/dist/lib/runtime/prompt.test.d.ts +1 -0
  363. package/dist/lib/runtime/prompt.test.js +79 -0
  364. package/dist/lib/runtime/promptRunner.d.ts +1 -1
  365. package/dist/lib/runtime/promptRunner.js +11 -0
  366. package/dist/lib/runtime/result.js +43 -17
  367. package/dist/lib/runtime/result.test.js +65 -1
  368. package/dist/lib/runtime/runBatch.d.ts +4 -2
  369. package/dist/lib/runtime/runBatch.js +20 -3
  370. package/dist/lib/runtime/runBatch.test.js +41 -1
  371. package/dist/lib/runtime/runner.d.ts +8 -0
  372. package/dist/lib/runtime/runner.js +50 -25
  373. package/dist/lib/runtime/runner.test.js +69 -2
  374. package/dist/lib/runtime/state/context.d.ts +25 -1
  375. package/dist/lib/runtime/state/context.js +73 -22
  376. package/dist/lib/runtime/state/globalStore.js +6 -0
  377. package/dist/lib/runtime/state/stateStack.d.ts +29 -0
  378. package/dist/lib/runtime/state/stateStack.js +63 -0
  379. package/dist/lib/runtime/types.d.ts +1 -1
  380. package/dist/lib/runtime/utils.d.ts +15 -0
  381. package/dist/lib/runtime/utils.js +37 -1
  382. package/dist/lib/runtime/utils.test.js +60 -1
  383. package/dist/lib/serve/discovery.d.ts +2 -2
  384. package/dist/lib/serve/discovery.js +7 -7
  385. package/dist/lib/serve/discovery.test.js +9 -9
  386. package/dist/lib/serve/http/adapter.js +52 -2
  387. package/dist/lib/serve/http/adapter.test.js +41 -8
  388. package/dist/lib/serve/mcp/adapter.d.ts +7 -1
  389. package/dist/lib/serve/mcp/adapter.js +68 -22
  390. package/dist/lib/serve/mcp/adapter.test.js +51 -16
  391. package/dist/lib/serve/mcp/httpTransport.d.ts +5 -0
  392. package/dist/lib/serve/mcp/httpTransport.js +2 -0
  393. package/dist/lib/serve/mcp/httpTransport.test.js +1 -1
  394. package/dist/lib/serve/mcp/interruptLoop.test.js +2 -2
  395. package/dist/lib/serve/types.d.ts +3 -3
  396. package/dist/lib/statelogClient.d.ts +24 -6
  397. package/dist/lib/statelogClient.js +34 -11
  398. package/dist/lib/statelogClient.test.js +48 -0
  399. package/dist/lib/stdlib/abortable.js +20 -9
  400. package/dist/lib/stdlib/agencyEval.d.ts +18 -18
  401. package/dist/lib/stdlib/agencyEval.js +29 -29
  402. package/dist/lib/stdlib/cli.d.ts +63 -0
  403. package/dist/lib/stdlib/cli.js +320 -24
  404. package/dist/lib/stdlib/fs.d.ts +5 -8
  405. package/dist/lib/stdlib/fs.js +28 -18
  406. package/dist/lib/stdlib/layout/axis.d.ts +3 -0
  407. package/dist/lib/stdlib/layout/axis.js +25 -2
  408. package/dist/lib/stdlib/layout/barchart.d.ts +33 -0
  409. package/dist/lib/stdlib/layout/barchart.js +217 -0
  410. package/dist/lib/stdlib/layout/box.d.ts +2 -0
  411. package/dist/lib/stdlib/layout/box.js +12 -1
  412. package/dist/lib/stdlib/layout/nodes.d.ts +7 -1
  413. package/dist/lib/stdlib/layout/nodes.js +33 -2
  414. package/dist/lib/stdlib/layout/render.d.ts +4 -5
  415. package/dist/lib/stdlib/layout/render.js +60 -124
  416. package/dist/lib/stdlib/layout/sizing.d.ts +15 -0
  417. package/dist/lib/stdlib/layout/sizing.js +38 -0
  418. package/dist/lib/stdlib/layout/table.d.ts +2 -0
  419. package/dist/lib/stdlib/layout/table.js +7 -0
  420. package/dist/lib/stdlib/layout.d.ts +12 -1
  421. package/dist/lib/stdlib/layout.js +11 -7
  422. package/dist/lib/stdlib/llm.d.ts +29 -0
  423. package/dist/lib/stdlib/llm.js +29 -0
  424. package/dist/lib/stdlib/memory.d.ts +1 -0
  425. package/dist/lib/stdlib/memory.js +5 -0
  426. package/dist/lib/stdlib/shell.d.ts +1 -1
  427. package/dist/lib/stdlib/shell.js +38 -5
  428. package/dist/lib/stdlib/statelog.d.ts +2 -2
  429. package/dist/lib/stdlib/statelog.js +5 -5
  430. package/dist/lib/stdlib/syntax-themes.d.ts +17 -0
  431. package/dist/lib/stdlib/syntax-themes.js +423 -0
  432. package/dist/lib/stdlib/syntax.d.ts +13 -1
  433. package/dist/lib/stdlib/syntax.js +81 -69
  434. package/dist/lib/stdlib/thread.d.ts +12 -0
  435. package/dist/lib/stdlib/thread.js +15 -0
  436. package/dist/lib/stdlib/ui.d.ts +1 -1
  437. package/dist/lib/stdlib/ui.js +27 -2
  438. package/dist/lib/symbolTable.d.ts +8 -5
  439. package/dist/lib/symbolTable.js +12 -8
  440. package/dist/lib/symbolTable.test.js +6 -6
  441. package/dist/lib/templates/backends/typescriptGenerator/blockSetup.d.ts +2 -1
  442. package/dist/lib/templates/backends/typescriptGenerator/blockSetup.js +1 -0
  443. package/dist/lib/templates/backends/typescriptGenerator/forkBlockSetup.d.ts +2 -1
  444. package/dist/lib/templates/backends/typescriptGenerator/forkBlockSetup.js +1 -0
  445. package/dist/lib/templates/backends/typescriptGenerator/functionCatchFailure.d.ts +1 -1
  446. package/dist/lib/templates/backends/typescriptGenerator/functionCatchFailure.js +9 -0
  447. package/dist/lib/templates/backends/typescriptGenerator/imports.d.ts +1 -1
  448. package/dist/lib/templates/backends/typescriptGenerator/imports.js +2 -1
  449. package/dist/lib/templates/backends/typescriptGenerator/interruptAssignment.d.ts +2 -2
  450. package/dist/lib/templates/backends/typescriptGenerator/interruptAssignment.js +1 -1
  451. package/dist/lib/templates/backends/typescriptGenerator/interruptReturn.d.ts +2 -2
  452. package/dist/lib/templates/backends/typescriptGenerator/interruptReturn.js +1 -1
  453. package/dist/lib/templates/cli/optimizeReport.d.ts +9 -0
  454. package/dist/lib/templates/cli/optimizeReport.js +18 -0
  455. package/dist/lib/templates/cli/standaloneHttp.d.ts +2 -2
  456. package/dist/lib/templates/cli/standaloneHttp.js +2 -2
  457. package/dist/lib/templates/cli/standaloneMcp.d.ts +2 -2
  458. package/dist/lib/templates/cli/standaloneMcp.js +7 -6
  459. package/dist/lib/templates/cli/standaloneMcpHttp.d.ts +2 -2
  460. package/dist/lib/templates/cli/standaloneMcpHttp.js +7 -5
  461. package/dist/lib/typeChecker/agencyFunctionMethods.test.d.ts +1 -0
  462. package/dist/lib/typeChecker/agencyFunctionMethods.test.js +91 -0
  463. package/dist/lib/typeChecker/builtins.d.ts +17 -0
  464. package/dist/lib/typeChecker/builtins.js +36 -0
  465. package/dist/lib/typeChecker/effectSetImport.test.d.ts +1 -0
  466. package/dist/lib/typeChecker/effectSetImport.test.js +46 -0
  467. package/dist/lib/typeChecker/effectSets.d.ts +26 -0
  468. package/dist/lib/typeChecker/effectSets.js +64 -0
  469. package/dist/lib/typeChecker/effectSets.test.d.ts +1 -0
  470. package/dist/lib/typeChecker/effectSets.test.js +48 -0
  471. package/dist/lib/typeChecker/index.d.ts +1 -1
  472. package/dist/lib/typeChecker/index.js +12 -8
  473. package/dist/lib/typeChecker/interruptAnalysis.d.ts +6 -6
  474. package/dist/lib/typeChecker/interruptAnalysis.js +20 -20
  475. package/dist/lib/typeChecker/interruptAnalysis.test.js +16 -16
  476. package/dist/lib/typeChecker/interruptCallGraph.test.js +2 -2
  477. package/dist/lib/typeChecker/raisesDiagnostic.d.ts +18 -0
  478. package/dist/lib/typeChecker/raisesDiagnostic.js +58 -0
  479. package/dist/lib/typeChecker/raisesDiagnostic.test.d.ts +1 -0
  480. package/dist/lib/typeChecker/raisesDiagnostic.test.js +91 -0
  481. package/dist/lib/typeChecker/synthesizer.js +8 -29
  482. package/dist/lib/typeChecker/testUtils.d.ts +13 -0
  483. package/dist/lib/typeChecker/testUtils.js +37 -0
  484. package/dist/lib/typeChecker/types.d.ts +3 -3
  485. package/dist/lib/types/function.d.ts +6 -0
  486. package/dist/lib/types/graphNode.d.ts +3 -0
  487. package/dist/lib/types/handleBlock.d.ts +1 -0
  488. package/dist/lib/types/interruptStatement.d.ts +4 -1
  489. package/dist/lib/types/literals.d.ts +3 -0
  490. package/dist/lib/types/typeHints.d.ts +13 -0
  491. package/dist/lib/types.d.ts +3 -0
  492. package/dist/lib/utils/diff.d.ts +37 -5
  493. package/dist/lib/utils/diff.js +288 -24
  494. package/dist/lib/utils/diff.test.js +157 -19
  495. package/dist/lib/utils/node.js +9 -1
  496. package/dist/lib/utils/termcolors.d.ts +13 -0
  497. package/dist/lib/utils/termcolors.js +22 -0
  498. package/dist/scripts/agency.js +67 -34
  499. package/dist/scripts/agency.test.js +15 -2
  500. package/package.json +9 -2
  501. package/stdlib/agency/eval.agency +57 -56
  502. package/stdlib/agency/eval.js +89 -71
  503. package/stdlib/agency.js +51 -1
  504. package/stdlib/agent.js +30 -1
  505. package/stdlib/args.js +6 -1
  506. package/stdlib/array.js +42 -1
  507. package/stdlib/browser.js +6 -1
  508. package/stdlib/calendar.js +21 -1
  509. package/stdlib/chart.agency +157 -0
  510. package/stdlib/chart.js +982 -0
  511. package/stdlib/cli.agency +6 -0
  512. package/stdlib/cli.js +31 -5
  513. package/stdlib/clipboard.js +9 -1
  514. package/stdlib/concurrency.js +6 -1
  515. package/stdlib/date.js +48 -1
  516. package/stdlib/email.js +12 -1
  517. package/stdlib/fs.agency +35 -107
  518. package/stdlib/fs.js +254 -450
  519. package/stdlib/http.js +12 -1
  520. package/stdlib/imessage.js +6 -1
  521. package/stdlib/index.agency +64 -2
  522. package/stdlib/index.js +563 -29
  523. package/stdlib/keyring.js +15 -1
  524. package/stdlib/layout.agency +2 -228
  525. package/stdlib/layout.js +58 -1227
  526. package/stdlib/llm.agency +91 -0
  527. package/stdlib/llm.js +787 -0
  528. package/stdlib/markdown.js +15 -1
  529. package/stdlib/math.js +15 -1
  530. package/stdlib/memory.agency +19 -2
  531. package/stdlib/memory.js +152 -5
  532. package/stdlib/oauth.js +15 -1
  533. package/stdlib/object.js +18 -1
  534. package/stdlib/path.js +24 -1
  535. package/stdlib/policy.agency +180 -73
  536. package/stdlib/policy.js +650 -78
  537. package/stdlib/schemas.js +2 -1
  538. package/stdlib/search.js +6 -1
  539. package/stdlib/shell.agency +44 -2
  540. package/stdlib/shell.js +331 -62
  541. package/stdlib/skills.agency +50 -6
  542. package/stdlib/skills.js +224 -29
  543. package/stdlib/sms.js +6 -1
  544. package/stdlib/speech.js +12 -1
  545. package/stdlib/statelog.agency +15 -15
  546. package/stdlib/statelog.js +58 -38
  547. package/stdlib/strategy.js +20 -1
  548. package/stdlib/syntax.agency +175 -2
  549. package/stdlib/syntax.js +667 -9
  550. package/stdlib/system.js +36 -1
  551. package/stdlib/table.agency +202 -0
  552. package/stdlib/table.js +1427 -0
  553. package/stdlib/thread.agency +24 -0
  554. package/stdlib/thread.js +145 -3
  555. package/stdlib/threads.js +18 -1
  556. package/stdlib/types.js +2 -1
  557. package/stdlib/ui.agency +20 -1
  558. package/stdlib/ui.js +244 -19
  559. package/stdlib/validators.js +36 -1
  560. package/stdlib/weather.js +12 -1
  561. package/stdlib/wikipedia.js +12 -1
  562. package/dist/lib/agents/agency-agent/code.js +0 -532
  563. package/dist/lib/agents/agency-agent/research.js +0 -311
  564. package/dist/lib/agents/agency-agent/subagents/oneShot.agency +0 -49
  565. package/dist/lib/eval/loadTasks.d.ts +0 -6
  566. package/dist/lib/eval/loadTasks.js +0 -81
  567. package/dist/lib/eval/loadTasks.test.js +0 -60
  568. package/dist/lib/optimize/tasks.d.ts +0 -2
  569. package/dist/lib/optimize/tasks.js +0 -9
  570. package/dist/lib/optimize/tasks.test.js +0 -17
  571. /package/dist/lib/agents/{judge.agency → eval/judge.agency} +0 -0
  572. /package/dist/lib/agents/{judgePairwise.agency → eval/judgePairwise.agency} +0 -0
  573. /package/dist/lib/{eval/loadTasks.test.d.ts → backends/anthropicApiKey.codegen.test.d.ts} +0 -0
  574. /package/dist/lib/{optimize/tasks.test.d.ts → backends/maxToolResultChars.codegen.test.d.ts} +0 -0
@@ -1,8 +1,8 @@
1
1
  import { getVersion } from "std::agency"
2
- import { route } from "std::agent"
3
2
  import { parseArgs } from "std::args"
4
3
  import { clearMessages, pushMessage, repl, clearScreen } from "std::cli"
5
4
  import { today } from "std::date"
5
+ import { setAgentCwd } from "std::index"
6
6
  import { box, render } from "std::layout"
7
7
  import { setMemoryId } from "std::memory"
8
8
  import {
@@ -15,7 +15,7 @@ import { exists } from "std::shell"
15
15
  import { commandsDir, expandSlash } from "std::skills"
16
16
  import { highlight } from "std::syntax"
17
17
  import { cwd, env, isTTY, readStdin, setTitle } from "std::system"
18
- import { getCost, getTokens, systemMessage } from "std::thread"
18
+ import { getCost, getModelCosts, systemMessage } from "std::thread"
19
19
  import { chooseOption, ChoiceItem } from "std::ui"
20
20
 
21
21
  import { figs, title } from "./images/images.agency"
@@ -25,15 +25,11 @@ import {
25
25
  recommendedAutoApprovePolicy,
26
26
  } from "./lib/defaultPolicy.agency"
27
27
  import { truncate, formatArgs, formatToolResponse } from "./lib/utils.agency"
28
- import { codeSysPrompt, codeTools, codeAgent } from "./subagents/code.agency"
28
+ import { configureModels } from "./shared.agency"
29
+ import { codeAgent } from "./subagents/code.agency"
29
30
  import { explorerAgent } from "./subagents/explorer.agency"
30
- import { oneShotAgent } from "./subagents/oneShot.agency"
31
31
  import { oracleAgent } from "./subagents/oracle.agency"
32
- import {
33
- researchSysPrompt,
34
- researchTools,
35
- researchAgent,
36
- } from "./subagents/research.agency"
32
+ import { researchAgent } from "./subagents/research.agency"
37
33
  import { reviewAgent } from "./subagents/review.agency"
38
34
 
39
35
 
@@ -65,8 +61,8 @@ import { reviewAgent } from "./subagents/review.agency"
65
61
  *
66
62
  * This file's responsibilities collapse to:
67
63
  * 1. Per-turn callback (`_runTurn`) wiring the user message into
68
- * route() and printing the reply into the scroll output.
69
- * 2. Per-kind ALWAYS_FIELDS map for the policy handler's
64
+ * `mainAgent` and printing the reply into the scroll output.
65
+ * 2. Per-effect ALWAYS_FIELDS map for the policy handler's
70
66
  * "approve-always-here" option.
71
67
  * 3. Wire the two specialists into one `RouterConfig`.
72
68
  *
@@ -91,17 +87,28 @@ let VERBOSE: boolean = false
91
87
  // scroll-output writes when stdout is destined for a Unix pipe.
92
88
  let _isInteractive: boolean = true
93
89
 
90
+ // Live tool-call tracing. Always shown in the interactive REPL; in
91
+ // one-shot mode it's suppressed so piped output is just the reply,
92
+ // unless the user opts in with `--verbose` / `--debug` (AGENT_DEBUG).
93
+ def _showTraces(): boolean {
94
+ return _isInteractive || VERBOSE || AGENT_DEBUG
95
+ }
96
+
94
97
  callback("onToolCallStart") as data {
95
- pushMessage(color.yellow("⏺ ${data.toolName}(${formatArgs(data.args)})"))
98
+ if (_showTraces()) {
99
+ pushMessage(color.yellow("⏺ ${data.toolName}(${formatArgs(data.args)})"))
100
+ }
96
101
  }
97
102
 
98
103
  callback("onToolCallEnd") as data {
99
- if (data.result is success(_result)) {
100
- pushMessage(color.dim.green("${formatToolResponse(_result)}"))
101
- } else if (data.result is failure(_error)) {
102
- pushMessage(color.red(" ⎿ Error: ${_error}"))
103
- } else {
104
- pushMessage(color.dim("${formatToolResponse(data.result)}"))
104
+ if (_showTraces()) {
105
+ if (data.result is success(_result)) {
106
+ pushMessage(color.dim.green("${formatToolResponse(_result)}"))
107
+ } else if (data.result is failure(_error)) {
108
+ pushMessage(color.red(" ⎿ Error: ${_error}"))
109
+ } else {
110
+ pushMessage(color.dim("${formatToolResponse(data.result)}"))
111
+ }
105
112
  }
106
113
  }
107
114
 
@@ -178,6 +185,7 @@ def builtinPalette(): Record<string, string> {
178
185
  "/exit": "Exit the agent",
179
186
  "/clear": "Clear the conversation transcript",
180
187
  "/cost": "Show cumulative LLM cost and tokens",
188
+ "/paste": "Multi-line paste mode (Ctrl+D submits, Ctrl+C cancels)",
181
189
  "/help": "Show available slash commands"
182
190
  }
183
191
  }
@@ -220,18 +228,38 @@ def _runTurn(msg: string): boolean {
220
228
  return true
221
229
  }
222
230
  if (trimmed == "/help") {
223
- pushMessage("Commands: /exit, /clear, /cost, /help")
231
+ pushMessage("Commands: /exit, /clear, /cost, /paste, /help")
224
232
  return true
225
233
  }
226
234
  if (trimmed == "/cost") {
227
- pushMessage("Cost: $${getCost().toFixed(4)}")
228
- pushMessage("Tokens: ${getTokens()}")
235
+ // Derive BOTH the header total and the per-model breakdown from the
236
+ // same process-wide accumulator (getModelCosts), so the `Cost:` /
237
+ // `Tokens:` lines and the rows below them can never visibly disagree.
238
+ // (getCost()/getTokens() read the per-branch accumulator, a different
239
+ // scope — adjacent in the output, they could in principle diverge.)
240
+ // Rows are sorted by cost descending by getModelCosts(); a subagent
241
+ // on a pricier model (e.g. the oracle on opus-4.8) shows up here.
242
+ const byModel = getModelCosts()
243
+ let totalCost = 0.0
244
+ let totalTokens = 0
245
+ for (m in byModel) {
246
+ totalCost = totalCost + m.cost
247
+ totalTokens = totalTokens + m.inputTokens + m.outputTokens
248
+ }
249
+ pushMessage("Cost: $${totalCost.toFixed(4)}")
250
+ pushMessage("Tokens: ${totalTokens}")
251
+ if (byModel.length > 0) {
252
+ pushMessage("")
253
+ pushMessage("By model:")
254
+ for (m in byModel) {
255
+ pushMessage(" ${m.model} ↑${m.inputTokens} ↓${m.outputTokens} $${m.cost.toFixed(4)}")
256
+ }
257
+ }
229
258
  return true
230
259
  }
231
- // Expand `/foo args` to the rendered command body. Unknown `/foo`
232
- // inputs fall through unchanged matches Claude Code.
233
- const prompt = expandSlash(msg, projectCommands)
234
- const reply = mainAgent(prompt)
260
+ // Hand the message to the agent. `agentReply` does the slash-command
261
+ // expansion and the actual agent work; the REPL just renders the reply.
262
+ const reply = agentReply(msg)
235
263
  if (reply != "" && reply != null && reply != undefined) {
236
264
  pushMessage(highlight("${reply}\n", language: "markdown"))
237
265
  } else {
@@ -241,6 +269,12 @@ def _runTurn(msg: string): boolean {
241
269
  }
242
270
 
243
271
  let first = true
272
+
273
+ // Valid `--agent` targets. Empty / "main" routes through the coordinator;
274
+ // the rest route the starting prompt's first turn directly to that
275
+ // subagent. Used to validate the flag and to dispatch in `agentReplyVia`.
276
+ static const START_AGENTS = ["main", "code", "research", "oracle", "explorer", "review"]
277
+
244
278
  static const mainAgentSystemPrompt = """
245
279
  You are the top-level coordinator of an Agency-language assistant. You
246
280
  receive every user message and decide how to respond.
@@ -386,6 +420,15 @@ parse → SymbolTable.build → preprocess → TypeScriptBuilder → printTs
386
420
  Keep diagrams small. Skip them where prose or code is clearer —
387
421
  diagrams earn their space by showing **relationships** or **flow**.
388
422
 
423
+ ## Be proactive
424
+
425
+ When the user asks you to look at, debug, or change a file or some code,
426
+ **delegate to `codeAgent` to do it** — don't ask the user to paste a file
427
+ or describe code a subagent could read. The code agent has `read`, `glob`,
428
+ and `ls` and resolves relative paths against the user's working directory
429
+ automatically, so a bare filename like `foo.agency` is enough. Only ask
430
+ the user for information you genuinely cannot obtain through a subagent.
431
+
389
432
  ## Answer before action
390
433
 
391
434
  When the user asks a question, asks for an opinion, or asks how to
@@ -408,7 +451,10 @@ def mainAgent(prompt: string): string {
408
451
  thread(label: "main", summarize: true, session: "main") {
409
452
  setMemoryId("main")
410
453
  if (first) {
411
- systemMessage(mainAgentSystemPrompt)
454
+ // `_context` carries the per-run grounding (date, cwd, AGENTS.md),
455
+ // set by `setupSession`. Appending it to the system prompt is what
456
+ // actually gets that context to the LLM.
457
+ systemMessage(mainAgentSystemPrompt + _context)
412
458
  first = false
413
459
  }
414
460
  const result = llm(prompt, {
@@ -419,28 +465,39 @@ def mainAgent(prompt: string): string {
419
465
  return result
420
466
  }
421
467
 
422
- def routeAgent(prompt: string): string {
423
- const reply = route(
424
- {
425
- start: "code",
426
- agents: {
427
- code: {
428
- type: "agent",
429
- name: "code",
430
- agent: codeAgent
431
- },
432
- research: {
433
- type: "agent",
434
- name: "research",
435
- agent: researchAgent
436
- }
437
- },
438
- maxHops: 3,
439
- context: _context
440
- },
441
- prompt,
442
- )
443
- return reply
468
+ // Dispatch one turn. `target` empty (or "main") runs the coordinator
469
+ // `mainAgent`, which routes as usual; any subagent name routes the turn
470
+ // directly to that subagent. All subagents share `(userMsg, allowHandoff)`
471
+ // and the seed turn never hands off (allowHandoff: false). Used by both
472
+ // the one-shot path and the interactive seed turn.
473
+ export def agentReplyVia(target: string, userMsg: string): string {
474
+ const expanded = expandSlash(userMsg, projectCommands)
475
+ if (target == "code") {
476
+ return codeAgent(expanded, false)
477
+ }
478
+ if (target == "research") {
479
+ return researchAgent(expanded, false)
480
+ }
481
+ if (target == "oracle") {
482
+ return oracleAgent(expanded, false)
483
+ }
484
+ if (target == "explorer") {
485
+ return explorerAgent(expanded, false)
486
+ }
487
+ if (target == "review") {
488
+ return reviewAgent(expanded, false)
489
+ }
490
+ return mainAgent(expanded)
491
+ }
492
+
493
+ // The agent's core turn, decoupled from the terminal: expand any project
494
+ // slash command, then run the prompt through `mainAgent` and return the
495
+ // reply. The REPL (`main` / `_runTurn`) owns input, output, and built-in
496
+ // commands; everything the *agent* does for a user message lives here, so
497
+ // it can be driven and tested without a terminal or user input. See
498
+ // `tests/agentTurn.agency`.
499
+ export def agentReply(userMsg: string): string {
500
+ return agentReplyVia("", userMsg)
444
501
  }
445
502
 
446
503
  def roundedCost(): string {
@@ -461,9 +518,15 @@ def sample(arr: any[]): any {
461
518
 
462
519
  def printHeader() {
463
520
  const fig = sample(figs)
464
- const data = box(title: "Agency", padding: 1, borderColor: "cyan", titleColor: "cyan") as b {
521
+ const data = box(
522
+ title: "Agency",
523
+ padding: 1,
524
+ borderColor: "cyan",
525
+ titleColor: "cyan",
526
+ width: "full",
527
+ ) as b {
465
528
  b.row(gap: 1) as r {
466
- r.column() as left {
529
+ r.column(width: "66%") as left {
467
530
  left.text("Welcome to the Agency Agent!", bold: true)
468
531
  left.text("Ask me to write code, look up docs, or just chat.")
469
532
  left.text("All costs are estimates. Actual costs may be higher.", dim: true)
@@ -472,8 +535,8 @@ def printHeader() {
472
535
  left.text("/help for commands · /exit to quit", dim: true)
473
536
  }
474
537
  r.vline()
475
- r.column() as right {
476
- right.raw(fig)
538
+ r.column(width: "30%", align: "center") as right {
539
+ right.raw(fig, align: "center")
477
540
  }
478
541
  }
479
542
  }
@@ -501,6 +564,103 @@ def givePolicyChoice() {
501
564
  }
502
565
  }
503
566
 
567
+ // Shared session setup for both interactive and one-shot modes. Builds
568
+ // the per-run grounding context, loads (or initializes) the policy, and
569
+ // returns the installed CLI policy handler. `interactive` gates the
570
+ // first-run policy-choice prompt: one-shot has no user to answer it, so
571
+ // it falls back to the recommended default (reads/web auto-approved;
572
+ // writes still require approval, which a non-interactive run can't
573
+ // grant — those tasks block, which is the safe behavior).
574
+ def setupSession(interactive: boolean): any {
575
+ // Point every path-taking tool at the user's working directory so the
576
+ // agent's relative file/shell commands resolve against where the user
577
+ // launched it. The user (via the agent) can change this later with
578
+ // setAgentCwd.
579
+ setAgentCwd(cwd())
580
+
581
+ // Grounding: the LLM shouldn't have to ask where it is or what day it
582
+ // is. Project context: inline an AGENTS.md from the workspace root if
583
+ // present so the LLM follows the project's conventions.
584
+ const projectContext = loadAgentsMd(cwd()) with approve
585
+ _context = "\n\nCurrent date: ${today()}\nCurrent working directory: ${cwd()}${projectContext}"
586
+
587
+ const policy = parsePolicyFile(POLICY_PATH)
588
+ if (policy is failure(f)) {
589
+ if (f.status == "doesnt-exist") {
590
+ if (interactive) {
591
+ print(color.yellow("No existing policy found at ${POLICY_PATH}."))
592
+ setPolicy(POLICY_PATH, givePolicyChoice())
593
+ } else {
594
+ setPolicy(POLICY_PATH, recommendedAutoApprovePolicy)
595
+ }
596
+ } else {
597
+ print(color.red("Failed to load policy: ${JSON.stringify(f.error)}"))
598
+ process.exit(1)
599
+ }
600
+ } else {
601
+ setPolicy(POLICY_PATH, policy.value)
602
+ }
603
+ // Bind the handler to a local var so `with handler` parses (the
604
+ // `with` clause only accepts an identifier, not a call expression).
605
+ return cliPolicyHandler(file: POLICY_PATH, fields: ALWAYS_FIELDS)
606
+ }
607
+
608
+ // One-shot entry: run a single turn through the same `mainAgent` the
609
+ // REPL uses, with the same session setup and policy handler, and return
610
+ // the reply. Used for `agency agent -p "..."`, a positional query, or
611
+ // piped stdin. Slash commands are expanded so `-p /foo bar` works like
612
+ // typing it in the REPL; the loop-only built-ins (`/exit` etc.) are
613
+ // meaningless here and aren't checked.
614
+ def oneShotAgent(target: string, prompt: string): string {
615
+ _isInteractive = false
616
+ const handler = setupSession(false)
617
+ let reply: string = ""
618
+ handle {
619
+ reply = agentReplyVia(target, prompt)
620
+ } with (data) {
621
+ return handler(data)
622
+ }
623
+ return reply
624
+ }
625
+
626
+ // Render one seeded turn into the REPL scroll area: echo the auto-run
627
+ // prompt so the user sees what was asked, then push the agent's reply.
628
+ // Mirrors the reply-rendering half of `_runTurn`.
629
+ def _runSeedTurn(target: string, msg: string) {
630
+ pushMessage(color.dim("> ${msg}"))
631
+ const reply = agentReplyVia(target, msg)
632
+ if (reply != "" && reply != null && reply != undefined) {
633
+ pushMessage(highlight("${reply}\n", language: "markdown"))
634
+ } else {
635
+ pushMessage(color.red("No reply generated."))
636
+ }
637
+ }
638
+
639
+ // Start the interactive REPL. When `seedPrompt` is non-empty it is run as
640
+ // the first turn (routed via `seedTarget`) *inside* the handle block so
641
+ // its interrupts reach the policy `handler`; the user is then left at the
642
+ // prompt. `seedPrompt == ""` is the plain REPL with no seed. Subsequent
643
+ // turns always go through `_runTurn` (the coordinator) regardless of
644
+ // `seedTarget` — the target only kicks off the first turn.
645
+ def startInteractive(handler: any, seedTarget: string, seedPrompt: string) {
646
+ handle {
647
+ if (seedPrompt != "") {
648
+ _runSeedTurn(seedTarget, seedPrompt)
649
+ }
650
+ repl(
651
+ status: _buildStatus,
652
+ onSubmit: _runTurn,
653
+ prompt: "> ",
654
+ historyFile: HISTORY_PATH,
655
+ historyMax: 1000,
656
+ paletteCommands: mergedPalette(),
657
+ )
658
+ } with (data) {
659
+ return handler(data)
660
+ }
661
+ print(color.cyan("\nGoodbye!"))
662
+ }
663
+
504
664
  node main() {
505
665
  // Parse CLI flags first — `parseArgs` exits on --help / --version /
506
666
  // usage errors, before any handlers or the TUI are installed.
@@ -522,6 +682,31 @@ node main() {
522
682
  verbose: {
523
683
  type: "boolean",
524
684
  description: "Echo tool-call starts to stdout in non-interactive mode"
685
+ },
686
+ model: {
687
+ type: "string",
688
+ description: "Model for all LLM calls (overrides provider auto-detection)"
689
+ },
690
+ fastmodel: {
691
+ type: "string",
692
+ description: "Model for ordinary work (default: per detected provider)"
693
+ },
694
+ slowmodel: {
695
+ type: "string",
696
+ description: "Model for deep reasoning, e.g. the oracle/explorer subagents"
697
+ },
698
+ provider: {
699
+ type: "string",
700
+ description: "Force the LLM provider (use when a model name doesn't imply one)"
701
+ },
702
+ interactive: {
703
+ type: "boolean",
704
+ short: "i",
705
+ description: "Run the given prompt as the first turn of an interactive session, then hand over the REPL (instead of one-shot)"
706
+ },
707
+ agent: {
708
+ type: "string",
709
+ description: "Route the starting prompt to a named subagent: code, research, oracle, explorer, review (default: coordinator)"
525
710
  }
526
711
  }
527
712
  },
@@ -534,74 +719,85 @@ node main() {
534
719
  VERBOSE = true
535
720
  }
536
721
 
537
- setTitle("Agency Agent")
538
- clearScreen()
722
+ // Resolve which models to use. Explicit flags win; otherwise detect a
723
+ // provider from API-key env vars and use that provider's defaults. The
724
+ // fast model becomes the run-wide default via `setModel`; the slow
725
+ // model is read by the deep-reasoning subagents (oracle, explorer).
726
+ // Resolve + apply the fast/slow models. Absent string flags read as
727
+ // `undefined`, and agency's `== null` does not match `undefined`, so
728
+ // normalize via `??` (which does treat `undefined` as nullish) to "".
729
+ configureModels(
730
+ args.flags.model ?? "",
731
+ args.flags.fastmodel ?? "",
732
+ args.flags.slowmodel ?? "",
733
+ args.flags.provider ?? "",
734
+ )
735
+
539
736
  // Positional args (everything after flags) are joined with spaces to
540
- // form the one-shot prompt. `--` ends flag parsing if a positional
737
+ // form the starting prompt. `--` ends flag parsing if a positional
541
738
  // would otherwise look like a flag.
542
739
  const positionalQuery = args.positionals.join(" ")
543
740
  const hasQuery = positionalQuery != ""
741
+ const startAgent = args.flags.agent ?? ""
742
+ const wantInteractive = args.flags.interactive
743
+
744
+ // Validate the target subagent name up front (empty = coordinator).
745
+ if (startAgent != "" && !START_AGENTS.includes(startAgent)) {
746
+ print(
747
+ color.red(
748
+ "Unknown --agent value: ${startAgent}. Valid: ${START_AGENTS.join(", ")}",
749
+ ),
750
+ )
751
+ process.exit(1)
752
+ }
544
753
 
545
- // Non-interactive (piped) invocation: read the whole prompt from
546
- // stdin, run one turn of `route()`, write the reply to stdout, and
547
- // exit. No banner, no REPL, no slash commands. This lets the agent
548
- // behave like a normal Unix filter: `echo "..." | pnpm run agency agent`.
549
- //
550
- // Also entered when `--print` / `-p` is passed, or when a positional
551
- // query is given. In those cases stdin isn't read — the prompt comes
552
- // from the positional.
553
- const forceOneShot = args.flags.print || hasQuery
554
- if (forceOneShot || !isTTY()) {
555
- const result = oneShotAgent()
556
- print(result)
754
+ // Seeded interactive: run the given prompt as the first turn, then hand
755
+ // the REPL to the user. Requires a seed prompt and a TTY (the REPL reads
756
+ // stdin). `--print` wins if both are passed its whole job is to print
757
+ // and exit with no REPL. Without a TTY (or with `--print`) we fall
758
+ // through to one-shot below; `--interactive` with no prompt falls through
759
+ // to the plain REPL.
760
+ if (wantInteractive && hasQuery && isTTY() && !args.flags.print) {
761
+ setTitle("Agency Agent")
762
+ clearScreen()
763
+ printHeader()
764
+ const seededHandler = setupSession(true)
765
+ startInteractive(seededHandler, startAgent, positionalQuery)
557
766
  process.exit(0)
558
767
  }
559
768
 
560
- printHeader()
561
-
562
- // Grounding: the LLM should not have to ask the user where it is
563
- // or what day it is. Both are appended once per run so they ride
564
- // with the first system message in each specialist's thread.
565
- //
566
- // Project context: if the user keeps an AGENTS.md at the workspace
567
- // root (Anthropic / Pi / Aider all read it), inline it so the LLM
568
- // automatically follows the project's conventions.
569
- const projectContext = loadAgentsMd(cwd()) with approve
570
- _context = "\n\nCurrent date: ${today()}\nCurrent working directory: ${cwd()}${projectContext}"
571
-
572
- const policy = parsePolicyFile(POLICY_PATH)
573
- if (policy is failure(f)) {
574
- const { status, error } = f
575
- print(f, status, error)
576
- if (status == "doesnt-exist") {
577
- print(color.yellow("No existing policy found at ${POLICY_PATH}."))
578
- const choice = givePolicyChoice()
579
- setPolicy(POLICY_PATH, choice)
580
- } else {
581
- print(color.red("Failed to load policy: ${JSON.stringify(error)}"))
582
- process.exit(1)
769
+ // One-shot invocation: run one turn through the same agent the REPL uses
770
+ // and write the reply to stdout. No banner, no REPL. Entered when
771
+ // `--print` / `-p` is passed, a positional query is given, or stdin is
772
+ // piped (no TTY) so the agent behaves like a Unix filter:
773
+ // `echo "..." | agency agent`. `--interactive` is NOT a one-shot trigger
774
+ // on its own: with a TTY and no prompt it falls through to the plain
775
+ // REPL; without a TTY the `!isTTY()` check below routes it here to
776
+ // degrade gracefully. The prompt comes from the positional when present,
777
+ // otherwise from stdin. (Title/clearScreen are skipped so piped output
778
+ // stays free of escape codes.)
779
+ const forceOneShot = args.flags.print || hasQuery
780
+ if (forceOneShot || !isTTY()) {
781
+ let prompt = positionalQuery
782
+ if (!hasQuery) {
783
+ const fromStdin = readStdin()
784
+ if (fromStdin == null || fromStdin == "") {
785
+ process.exit(0)
786
+ }
787
+ prompt = fromStdin
583
788
  }
584
- } else {
585
- setPolicy(POLICY_PATH, policy.value)
789
+ print(oneShotAgent(startAgent, prompt))
790
+ process.exit(0)
586
791
  }
587
- // Bind the handler to a local var so `with handler` parses (the
588
- // `with` clause only accepts an identifier, not a call expression).
589
- const handler = cliPolicyHandler(file: POLICY_PATH, fields: ALWAYS_FIELDS)
590
792
 
591
- // The `repl()` widget owns the runloop; every Enter dispatches into
592
- // `_runTurn` which calls `route()`. Interrupts raised inside
593
- // route() propagate to the policy handler installed by the enclosing
594
- // `handle` block via the AsyncLocalStorage handler stack.
595
- handle {
596
- repl(
597
- status: _buildStatus,
598
- onSubmit: _runTurn,
599
- prompt: "> ",
600
- historyFile: HISTORY_PATH,
601
- historyMax: 1000,
602
- paletteCommands: mergedPalette(),
603
- )
604
- } with handler
605
-
606
- print(color.cyan("\nGoodbye!"))
793
+ // Plain interactive REPL (no seed). `startInteractive` owns the runloop;
794
+ // every Enter dispatches into `_runTurn` which calls `mainAgent`.
795
+ // Interrupts raised during a turn propagate to the policy handler
796
+ // installed by the enclosing `handle` block via the AsyncLocalStorage
797
+ // handler stack.
798
+ setTitle("Agency Agent")
799
+ clearScreen()
800
+ printHeader()
801
+ const handler = setupSession(true)
802
+ startInteractive(handler, "", "")
607
803
  }