@parcha/agentrun-serve 0.1.0-beta.36 → 0.1.0-beta.37

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 (325) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/README.md +158 -52
  3. package/dist/attach-conformance-cli.d.ts +1 -0
  4. package/dist/attach-conformance-cli.js +18 -0
  5. package/dist/attach-conformance.d.ts +25 -0
  6. package/dist/attach-conformance.js +343 -0
  7. package/dist/browser-cost.d.ts +1 -1
  8. package/dist/browser-live.d.ts +20 -8
  9. package/dist/browser-live.js +50 -27
  10. package/dist/cap.d.ts +0 -17
  11. package/dist/cap.js +0 -91
  12. package/dist/catalog.d.ts +1 -12
  13. package/dist/catalog.js +3 -33
  14. package/dist/cli.js +13 -4
  15. package/dist/composio-preset.d.ts +36 -0
  16. package/dist/composio-preset.js +179 -0
  17. package/dist/computer-attach.d.ts +125 -0
  18. package/dist/computer-attach.js +330 -0
  19. package/dist/computer-calls.d.ts +48 -0
  20. package/dist/computer-calls.js +126 -0
  21. package/dist/computer-channel.d.ts +57 -0
  22. package/dist/computer-channel.js +115 -0
  23. package/dist/computer-commands.d.ts +1 -1
  24. package/dist/computer-held-routes.d.ts +35 -0
  25. package/dist/computer-held-routes.js +106 -0
  26. package/dist/computer-held.d.ts +48 -0
  27. package/dist/computer-held.js +87 -0
  28. package/dist/computer-keys.d.ts +21 -1
  29. package/dist/computer-keys.js +53 -11
  30. package/dist/computer-live.d.ts +160 -0
  31. package/dist/computer-live.js +643 -0
  32. package/dist/computer-view.d.ts +13 -0
  33. package/dist/computer-view.js +50 -3
  34. package/dist/computer-viewer.d.ts +6 -0
  35. package/dist/computer-viewer.js +297 -0
  36. package/dist/computers.d.ts +33 -7
  37. package/dist/computers.js +50 -8
  38. package/dist/console/assets/index-Cn6JX1uH.js +563 -0
  39. package/dist/console/assets/index-DRN62V5v.css +1 -0
  40. package/dist/console/index.html +2 -2
  41. package/dist/faux-computers.d.ts +9 -20
  42. package/dist/faux-computers.js +53 -25
  43. package/dist/github-connection.d.ts +10 -2
  44. package/dist/github-connection.js +65 -2
  45. package/dist/github-faux.js +1 -1
  46. package/dist/graph-view.d.ts +1 -1
  47. package/dist/harness.d.ts +1 -0
  48. package/dist/harness.js +35 -1
  49. package/dist/host-tools.d.ts +1 -1
  50. package/dist/hosted-search.d.ts +5 -4
  51. package/dist/hosted-search.js +2 -7
  52. package/dist/import-sources.d.ts +10 -0
  53. package/dist/import-sources.js +44 -2
  54. package/dist/import.d.ts +3 -3
  55. package/dist/import.js +37 -6
  56. package/dist/learn/check.d.ts +4 -1
  57. package/dist/learn/check.js +1 -1
  58. package/dist/learn/commit.d.ts +4 -2
  59. package/dist/learn/commit.js +5 -5
  60. package/dist/learn/folder.d.ts +6 -0
  61. package/dist/learn/folder.js +4 -1
  62. package/dist/learn/general.d.ts +66 -0
  63. package/dist/learn/general.js +73 -0
  64. package/dist/learn/grade.d.ts +4 -0
  65. package/dist/learn/grade.js +22 -1
  66. package/dist/learn/reflect.d.ts +10 -1
  67. package/dist/learn/reflect.js +14 -4
  68. package/dist/learn/skills.d.ts +33 -5
  69. package/dist/learn/skills.js +53 -7
  70. package/dist/learned-workflow.d.ts +24 -0
  71. package/dist/learned-workflow.js +66 -3
  72. package/dist/learning-batch.d.ts +283 -0
  73. package/dist/learning-batch.js +574 -0
  74. package/dist/learning-evals.d.ts +33 -0
  75. package/dist/learning-evals.js +98 -0
  76. package/dist/learning-routes.d.ts +25 -2
  77. package/dist/learning-routes.js +39 -6
  78. package/dist/learning-words.d.ts +6 -0
  79. package/dist/learning-words.js +6 -0
  80. package/dist/learning.d.ts +28 -15
  81. package/dist/learning.js +84 -44
  82. package/dist/live/stream.js +6 -3
  83. package/dist/live/translate.d.ts +4 -3
  84. package/dist/live/translate.js +8 -4
  85. package/dist/live-reach.d.ts +37 -0
  86. package/dist/live-reach.js +62 -0
  87. package/dist/mcp-gateway.d.ts +9 -0
  88. package/dist/mcp-gateway.js +60 -11
  89. package/dist/mcp-oauth.d.ts +57 -7
  90. package/dist/mcp-oauth.js +133 -18
  91. package/dist/mcp-private-hosts.d.ts +4 -4
  92. package/dist/mcp-private-hosts.js +20 -12
  93. package/dist/mcp-probe.d.ts +19 -2
  94. package/dist/mcp-probe.js +44 -9
  95. package/dist/mcp-secret-writes.js +4 -0
  96. package/dist/mcp-servers-store.d.ts +38 -15
  97. package/dist/mcp-servers-store.js +112 -20
  98. package/dist/mcp-store.d.ts +10 -4
  99. package/dist/mcp-store.js +17 -6
  100. package/dist/model-form.d.ts +26 -0
  101. package/dist/model-form.js +80 -0
  102. package/dist/model.js +23 -10
  103. package/dist/openapi.js +386 -21
  104. package/dist/page.d.ts +18 -0
  105. package/dist/page.js +48 -20
  106. package/dist/paid-calls.d.ts +1 -1
  107. package/dist/place-offers.d.ts +2 -3
  108. package/dist/place-offers.js +3 -7
  109. package/dist/place.js +1 -2
  110. package/dist/print.d.ts +15 -1
  111. package/dist/print.js +14 -3
  112. package/dist/propose.d.ts +3 -0
  113. package/dist/propose.js +13 -5
  114. package/dist/provider.js +3 -2
  115. package/dist/reflect-range.d.ts +8 -1
  116. package/dist/reflect-range.js +1 -1
  117. package/dist/run-launch.d.ts +14 -2
  118. package/dist/run-launch.js +9 -4
  119. package/dist/run-learning-served.d.ts +2 -2
  120. package/dist/run-reads-over-store.d.ts +1 -1
  121. package/dist/run-repo.d.ts +20 -0
  122. package/dist/run-repo.js +63 -0
  123. package/dist/run-timeline-served.d.ts +1 -1
  124. package/dist/run-view.d.ts +3 -3
  125. package/dist/run-view.js +14 -7
  126. package/dist/runner/adapter-loop.d.ts +23 -7
  127. package/dist/runner/adapter-loop.js +648 -62
  128. package/dist/runner/adapter-run-spec.d.ts +11 -3
  129. package/dist/runner/adapter-run-spec.js +77 -22
  130. package/dist/runner/adapters/pi-computer.d.ts +7 -0
  131. package/dist/runner/adapters/pi-computer.js +43 -0
  132. package/dist/runner/adapters/pi.d.ts +9 -4
  133. package/dist/runner/adapters/pi.js +76 -14
  134. package/dist/runner/beside.d.ts +7 -3
  135. package/dist/runner/beside.js +21 -11
  136. package/dist/runner/claim-secrets.d.ts +4 -0
  137. package/dist/runner/claim-secrets.js +6 -1
  138. package/dist/runner/cli-run-spec.d.ts +20 -3
  139. package/dist/runner/cli-run-spec.js +113 -43
  140. package/dist/runner/commands.d.ts +3 -1
  141. package/dist/runner/commands.js +28 -3
  142. package/dist/runner/computer-channel.d.ts +34 -0
  143. package/dist/runner/computer-channel.js +61 -0
  144. package/dist/runner/conformance/harness.js +80 -14
  145. package/dist/runner/conformance/subject.d.ts +1 -1
  146. package/dist/runner/dispatch.js +3 -0
  147. package/dist/runner/end-of-run.d.ts +22 -12
  148. package/dist/runner/end-of-run.js +38 -16
  149. package/dist/runner/github-app.d.ts +2 -1
  150. package/dist/runner/github-app.js +4 -2
  151. package/dist/runner/harness-loops.js +15 -1
  152. package/dist/runner/held-run.d.ts +65 -0
  153. package/dist/runner/held-run.js +158 -0
  154. package/dist/runner/journal-kinds.d.ts +20 -1
  155. package/dist/runner/journal-kinds.js +14 -0
  156. package/dist/runner/journal-reader.d.ts +5 -4
  157. package/dist/runner/journal-reader.js +12 -10
  158. package/dist/runner/learning-ask.d.ts +7 -1
  159. package/dist/runner/learning-ask.js +21 -6
  160. package/dist/runner/learning-dispatch.d.ts +22 -0
  161. package/dist/runner/learning-dispatch.js +77 -10
  162. package/dist/runner/learning-end.d.ts +22 -1
  163. package/dist/runner/learning-end.js +92 -12
  164. package/dist/runner/learning-factory.d.ts +1 -1
  165. package/dist/runner/learning-factory.js +11 -5
  166. package/dist/runner/learning-loop.d.ts +67 -7
  167. package/dist/runner/learning-loop.js +173 -21
  168. package/dist/runner/learning-seed.d.ts +7 -0
  169. package/dist/runner/learning-seed.js +13 -0
  170. package/dist/runner/live-ticker.js +1 -0
  171. package/dist/runner/loop-contract.d.ts +8 -1
  172. package/dist/runner/loop.d.ts +5 -1
  173. package/dist/runner/loop.js +2 -1
  174. package/dist/runner/main.d.ts +13 -1
  175. package/dist/runner/main.js +27 -5
  176. package/dist/runner/mcp-claim.d.ts +10 -7
  177. package/dist/runner/mcp-claim.js +113 -24
  178. package/dist/runner/mcp-github.js +1 -1
  179. package/dist/runner/mcp-run.d.ts +5 -5
  180. package/dist/runner/mcp-run.js +15 -3
  181. package/dist/runner/metrics.d.ts +11 -2
  182. package/dist/runner/metrics.js +12 -2
  183. package/dist/runner/pr-checkout.d.ts +1 -0
  184. package/dist/runner/pr-checkout.js +1 -1
  185. package/dist/runner/reflect-hook.d.ts +18 -0
  186. package/dist/runner/reflect-hook.js +20 -8
  187. package/dist/runner/runner.d.ts +26 -0
  188. package/dist/runner/runner.js +184 -15
  189. package/dist/runner/scratch-sweep.d.ts +13 -0
  190. package/dist/runner/scratch-sweep.js +62 -0
  191. package/dist/runner/sessions.d.ts +1 -1
  192. package/dist/runner/store-port.d.ts +4 -2
  193. package/dist/runner/store-secrets.js +4 -3
  194. package/dist/runner/subagent.js +3 -1
  195. package/dist/runner/workflow-host.d.ts +6 -1
  196. package/dist/runner/workflow-host.js +1 -1
  197. package/dist/runner/workflow-loop.d.ts +1 -1
  198. package/dist/runner/workflow-spec.d.ts +14 -2
  199. package/dist/runner/workflow-spec.js +56 -20
  200. package/dist/runs-over-store.d.ts +6 -1
  201. package/dist/runs-over-store.js +38 -9
  202. package/dist/seal-key.js +3 -3
  203. package/dist/sealed-settings-store.d.ts +5 -2
  204. package/dist/sealed-settings-store.js +10 -4
  205. package/dist/secret-writes.d.ts +5 -4
  206. package/dist/secret-writes.js +5 -4
  207. package/dist/server-secrets-store.d.ts +4 -3
  208. package/dist/server-secrets-store.js +15 -9
  209. package/dist/server-secrets.js +3 -2
  210. package/dist/server.d.ts +43 -14
  211. package/dist/server.js +472 -126
  212. package/dist/settings-wake.d.ts +21 -0
  213. package/dist/settings-wake.js +59 -0
  214. package/dist/standard-webhooks.d.ts +4 -1
  215. package/dist/standard-webhooks.js +8 -4
  216. package/dist/store/command-wake.d.ts +12 -0
  217. package/dist/store/command-wake.js +37 -0
  218. package/dist/store/dialect.d.ts +6 -0
  219. package/dist/store/dialect.js +5 -0
  220. package/dist/store/index.d.ts +1 -1
  221. package/dist/store/journal/storage.d.ts +3 -2
  222. package/dist/store/journal/storage.js +13 -5
  223. package/dist/store/postgres/dialect.js +4 -0
  224. package/dist/store/postgres/migrations/0005_learning_rounds.sql +14 -0
  225. package/dist/store/postgres/migrations/0006_run_changes_channel_attempts.sql +57 -0
  226. package/dist/store/postgres/migrations/0007_viewer_host.sql +11 -0
  227. package/dist/store/postgres/migrations/0008_paired_computers.sql +30 -0
  228. package/dist/store/postgres/migrations/0009_commands_answer.sql +10 -0
  229. package/dist/store/schema.d.ts +1 -1
  230. package/dist/store/schema.js +77 -8
  231. package/dist/store/sqlite/run-patch.d.ts +7 -5
  232. package/dist/store/sqlite/run-patch.js +28 -13
  233. package/dist/store/sqlite/store.js +272 -54
  234. package/dist/store/store.d.ts +268 -9
  235. package/dist/store/store.js +12 -1
  236. package/dist/store-reads/cap.d.ts +1 -1
  237. package/dist/store-reads/cap.js +8 -1
  238. package/dist/store-reads/decisions.d.ts +2 -2
  239. package/dist/store-reads/decisions.js +1 -1
  240. package/dist/store-reads/import-summary.d.ts +1 -1
  241. package/dist/store-reads/journal.d.ts +5 -4
  242. package/dist/store-reads/journal.js +73 -15
  243. package/dist/store-reads/learning.d.ts +20 -4
  244. package/dist/store-reads/learning.js +74 -18
  245. package/dist/store-reads/runs.d.ts +1 -1
  246. package/dist/store-reads/workflow.d.ts +1 -1
  247. package/dist/store-view.d.ts +5 -0
  248. package/dist/store-view.js +26 -11
  249. package/dist/template-eval.js +6 -4
  250. package/dist/template-github-permissions.d.ts +22 -8
  251. package/dist/template-github-permissions.js +35 -15
  252. package/dist/timeline/timeline.d.ts +27 -0
  253. package/dist/timeline/timeline.js +10 -9
  254. package/dist/timeline/util.d.ts +4 -0
  255. package/dist/timeline/util.js +14 -0
  256. package/dist/tool-budget.d.ts +19 -7
  257. package/dist/tool-budget.js +14 -8
  258. package/dist/tool-gate.d.ts +16 -9
  259. package/dist/tool-gate.js +53 -42
  260. package/dist/tool-plane.d.ts +16 -12
  261. package/dist/tool-plane.js +9 -4
  262. package/dist/tools.d.ts +13 -4
  263. package/dist/tools.js +13 -4
  264. package/dist/trigger-adapter.d.ts +4 -0
  265. package/dist/trigger-secrets-shown.d.ts +2 -2
  266. package/dist/trigger-secrets-shown.js +1 -1
  267. package/dist/trigger-signatures.d.ts +25 -6
  268. package/dist/trigger-signatures.js +41 -7
  269. package/dist/trigger-targets.d.ts +2 -0
  270. package/dist/trigger-targets.js +1 -0
  271. package/dist/trigger-webhook.js +5 -2
  272. package/dist/triggers.d.ts +7 -3
  273. package/dist/triggers.js +35 -5
  274. package/dist/turns.js +16 -1
  275. package/dist/validate.js +11 -1
  276. package/dist/web-fetch.d.ts +1 -1
  277. package/dist/web-search.d.ts +10 -5
  278. package/dist/web-search.js +7 -6
  279. package/dist/webhooks.d.ts +145 -102
  280. package/dist/webhooks.js +559 -322
  281. package/dist/workflow/decisions.d.ts +1 -1
  282. package/dist/workflow/node-runner.d.ts +2 -1
  283. package/dist/workflow/node.d.ts +2 -1
  284. package/dist/workflow/node.js +3 -1
  285. package/dist/workflow/pi-versions.d.ts +23 -0
  286. package/dist/workflow/pi-versions.js +57 -0
  287. package/dist/workflow/record-tool.d.ts +2 -1
  288. package/dist/workflow/store.d.ts +1 -1
  289. package/dist/workflow/task.js +3 -2
  290. package/dist/workflow-run.d.ts +1 -1
  291. package/dist/workspace/attached-computers.d.ts +41 -0
  292. package/dist/workspace/attached-computers.js +114 -0
  293. package/dist/workspace/computer-deps.d.ts +16 -9
  294. package/dist/workspace/computer-deps.js +19 -7
  295. package/dist/workspace/computer-for-claim.d.ts +17 -8
  296. package/dist/workspace/computer-for-claim.js +62 -21
  297. package/dist/workspace/computer-hold.d.ts +11 -27
  298. package/dist/workspace/computer-hold.js +36 -56
  299. package/dist/workspace/computer-ledger.d.ts +3 -1
  300. package/dist/workspace/computer-ledger.js +4 -0
  301. package/dist/workspace/computer-mcp.d.ts +25 -0
  302. package/dist/workspace/computer-mcp.js +41 -0
  303. package/dist/workspace/computer-ports.d.ts +4 -13
  304. package/dist/workspace/computer-ports.js +4 -107
  305. package/dist/workspace/computer-script.d.ts +37 -0
  306. package/dist/workspace/computer-script.js +138 -0
  307. package/dist/workspace/executors.d.ts +1 -1
  308. package/dist/workspace/index.d.ts +4 -2
  309. package/dist/workspace/index.js +3 -1
  310. package/dist/workspace/kind-skills/desktop/SKILL.md +119 -84
  311. package/dist/workspace/kind-skills/phone/SKILL.md +100 -85
  312. package/dist/workspace/resolve-computer.d.ts +3 -0
  313. package/dist/workspace/resolve-computer.js +8 -2
  314. package/dist/workspace/run-tools.d.ts +32 -15
  315. package/dist/workspace/run-tools.js +23 -30
  316. package/package.json +19 -16
  317. package/templates/market-monitor/agent.md +1 -1
  318. package/dist/console/assets/index-BI4Byidu.js +0 -550
  319. package/dist/console/assets/index-C19gUF2n.css +0 -1
  320. package/dist/run-db.d.ts +0 -53
  321. package/dist/run-db.js +0 -116
  322. package/dist/workflow/readers.d.ts +0 -36
  323. package/dist/workflow/readers.js +0 -145
  324. package/dist/workflow/wal.d.ts +0 -11
  325. package/dist/workflow/wal.js +0 -106
@@ -28,6 +28,7 @@ import { Hearing } from "../heard.js";
28
28
  import { readSnapshot } from "../learn/folder.js";
29
29
  import { DRAFT_AFTER_RUNS } from "../learn/reflect.js";
30
30
  import { SKILL_NAME } from "../learn/skills.js";
31
+ import { DECISION_PROVIDER_KEYS } from "../tool-plane.js";
31
32
  import { handedValuesOf } from "../handed-values.js";
32
33
  import { MIN_SECRET } from "../learn/secrets.js";
33
34
  import { LEARNED_WORKFLOW_TOOL } from "../learned-workflow.js";
@@ -46,29 +47,37 @@ import { runnerSecrets } from "./redact.js";
46
47
  * does not learn is asked nothing and keeps nothing), else the reflect hook over the run's store. */
47
48
  export function learningEndOfRun(launch, deps) {
48
49
  const start = learningStartOf(launch);
49
- if (!start)
50
+ // A run that only uses what its agent learned (`learning: "use"`) is asked nothing and keeps nothing, at every end.
51
+ if (!start || start.use)
50
52
  return undefined;
51
53
  const env = deps.env ?? process.env;
52
54
  // The grade model the server named at the run's start (its learning start's), none: the agent's own grades it.
53
55
  const gradeModel = start.gradeModel;
54
- const hookFor = (runId) => createReflectHook({
56
+ const hookFor = (runId, round, before) => createReflectHook({
57
+ round,
58
+ ...(before ? { before } : {}),
55
59
  // reflectSkip's facts: a run its cap stopped has no room left, and one whose spend is unknown under a cap cannot be
56
60
  // kept within it. The cap is the run's now (capNowOf: its newest raise's, else its launch's), the one its requests
57
61
  // were admitted under, never the launch's alone. A person's stop comes to the hook as its `cut`.
62
+ // What its earlier rounds' grades took beside it counts toward that cap as its own spend does (Learning.spentBeside, the
63
+ // figure its gates read): unknown, no room.
58
64
  facts: async (runId) => {
59
65
  const costUsd = costOf(await runTimeline(deps.store, runId, BACKGROUND_CONTEXT, { settled: false }));
60
66
  const capUsd = launch.maxCostUsd === undefined ? undefined : capNowOf(launch.maxCostUsd, await runEntries(deps.store, runId, BACKGROUND_CONTEXT));
61
- return { learns: true, costUsd, ...(capUsd !== undefined ? { capUsd, capReached: costUsd !== null && costUsd >= capUsd } : {}) };
67
+ if (capUsd === undefined)
68
+ return { learns: true, costUsd };
69
+ const beside = await deps.store.learning.spentBeside(runId);
70
+ return { learns: true, costUsd, capUsd, capReached: costUsd !== null && (beside === null || costUsd + beside >= capUsd) };
62
71
  },
63
72
  nothingNew: async (runId) => onlyLearnedWorkflows(await runTimeline(deps.store, runId, BACKGROUND_CONTEXT, { settled: false })),
64
73
  // A draft asked for or not never stops the turn: a count that can't be read asks for none.
65
74
  promptFacts: async (runId, agentId) => {
66
- const { tools } = await draftTools(deps, runId);
75
+ const { tools, decisions } = await draftTools(deps, runId);
67
76
  // A notes run (`learning: "notes"`) is asked for no workflow draft, and its prompt describes none.
68
77
  const draftFor = start.notesOnly ? [] : await draftForOf(deps, agentId, runId, start.skills ?? []).catch(() => []);
69
78
  // With a draft asked for, what each tool its call nodes may run takes and gives, in the server's own words.
70
79
  const described = draftFor.length ? await describedOf(tools, deps.store) : [];
71
- return { active: start.seedNotes ?? start.given.length, tools, draftFor, described, ...(start.notesOnly ? { workflows: false } : {}) };
80
+ return { active: start.seedNotes ?? start.given.length, tools, draftFor, described, ...(decisions ? { decisions } : {}), ...(start.notesOnly ? { workflows: false } : {}) };
72
81
  },
73
82
  asked: async (runId) => {
74
83
  const [entries, reflect] = await Promise.all([runEntries(deps.store, runId, BACKGROUND_CONTEXT), runReflect(deps.store, runId, BACKGROUND_CONTEXT)]);
@@ -98,10 +107,17 @@ export function learningEndOfRun(launch, deps) {
98
107
  check: async ({ runId, agentId, turn, outcome, refuse }) => {
99
108
  if (start.seed === null)
100
109
  throw new Error("the run's learning start names no commit of main to propose against");
101
- return checkedOf(deps, { runId, agentId, seed: start.seed, turn, outcome, refuse, ...(start.notesOnly ? { workflows: false } : {}) });
110
+ // A later round checks against the main the earlier rounds merged into, not the seed the run started from: a note
111
+ // round 1 added and a reviewer corrected is a supersedes target round 2 may name.
112
+ // The skills and workflows keep the seed the run was laid from as their conflict base (the merge refuses a file main
113
+ // changed since), so only the notes are checked against that main.
114
+ const notesSeed = round > 1 ? await mainOf(deps, agentId) : undefined;
115
+ return checkedOf(deps, { runId, agentId, seed: start.seed, ...(notesSeed ? { notesSeed } : {}), turn, outcome, refuse, ...(start.notesOnly ? { workflows: false } : {}) });
102
116
  },
103
- // The learning run's id, one per run: a replayed end queues the same one (the store adds it once).
104
- newRunId: () => `${runId}-learning`,
117
+ // The learning run's id, one per round of the run: a replayed end queues the same one (the store adds it once).
118
+ newRunId: () => learningRunIdOf(runId, round),
119
+ // The cap its grade runs under (none for an uncapped run): what the run's cap leaves now.
120
+ ...(launch.maxCostUsd !== undefined ? { gradeCap: (runId) => gradeCapOf(deps.store, runId, round, launch.maxCostUsd) } : {}),
105
121
  ...(gradeModel ? { gradeModel } : {}),
106
122
  });
107
123
  return async (input) => {
@@ -111,11 +127,50 @@ export function learningEndOfRun(launch, deps) {
111
127
  if (seedFailed(start)) {
112
128
  const failed = start.seedFailed;
113
129
  const why = failed === "unreadable" ? STOPPED.unreadable : failed === "not_copied" ? STOPPED.notCopied : STOPPED.noSeed;
130
+ // Every end of it: the first's drop stands for a later one (the store keeps one a round, so it adds nothing new).
114
131
  return { also: [{ table: "learning_proposals", row: { turn: null, proposal: { state: "dropped", reason: "seed_failed", detail: said.stopped(why) } } }] };
115
132
  }
116
- return hookFor(input.runId)(input);
133
+ // Which end of the run this is: the round after the newest it kept (a follow-up opened the run again after it), else
134
+ // its first. A replayed end reads the same round: its rows are written only with the end that closes it.
135
+ const prior = await deps.store.learning.proposal(input.runId);
136
+ const round = prior ? prior.round + 1 : 1;
137
+ const kept = prior?.proposal;
138
+ const spent = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
139
+ const before = prior ? { job_cost_usd: spent(kept?.job_cost_usd), look_back_usd: spent(kept?.look_back_usd), looked: prior.turn !== null } : undefined;
140
+ return hookFor(input.runId, round, before)(input);
117
141
  };
118
142
  }
143
+ /** What every cap gate of run `runId` reads as spent beside it on its behalf (adapter-loop.ts RunCapConfig.beside): its
144
+ * learning runs' grades (Store.learning.spentBeside, the one figure), for a capped run that learns, the only run whose
145
+ * ends queue them; none for any other, whose gates read only its own sessions. */
146
+ export function spentBesideOf(launch, store, runId) {
147
+ const start = learningStartOf(launch);
148
+ if (launch.maxCostUsd === undefined || !start || start.use)
149
+ return undefined;
150
+ return () => store.learning.spentBeside(runId);
151
+ }
152
+ /** The cap the grade of round `round` of run `runId` runs under (its learning run's max_cost_usd), so the run's grades
153
+ * never take it past its cap: the cap the run is held to now (capNowOf: its newest raise's, else `launchCapUsd`), less
154
+ * what the run spent (its job, its follow-ups and every look-back, its timeline's cost) and what was spent beside it on its
155
+ * behalf by its earlier rounds (Learning.spentBeside, the figure the run's own gates read: each earlier grade at its cost
156
+ * once it ended, at its own cap while it has not). $0, no room, when any of those is unknown (a cost null, or a read that
157
+ * fails): a grade whose room can't be read is skipped, never run past the cap. */
158
+ export async function gradeCapOf(store, runId, round, launchCapUsd) {
159
+ try {
160
+ const capUsd = capNowOf(launchCapUsd, await runEntries(store, runId, BACKGROUND_CONTEXT));
161
+ const spent = costOf(await runTimeline(store, runId, BACKGROUND_CONTEXT, { settled: false }));
162
+ const beside = await store.learning.spentBeside(runId, { beforeRound: round });
163
+ if (capUsd === undefined || spent === null || !Number.isFinite(spent) || beside === null)
164
+ return 0;
165
+ return Math.max(0, capUsd - spent - beside);
166
+ }
167
+ catch {
168
+ return 0;
169
+ }
170
+ }
171
+ /** The learning run of round `round` of run `runId`'s learning: `<runId>-learning` for its first, `<runId>-learning-<n>`
172
+ * for each later one. */
173
+ export const learningRunIdOf = (runId, round = 1) => (round > 1 ? `${runId}-learning-${round}` : `${runId}-learning`);
119
174
  /** learned_workflow by every name a harness calls it: its own, and claude-agent's host-tool alias (`mcp__hands__<name>`).
120
175
  * A harness that names it another way reads as another tool, so its run is asked as before. */
121
176
  export const LEARNED_NAMES = new Set([LEARNED_WORKFLOW_TOOL, `mcp__hands__${LEARNED_WORKFLOW_TOOL}`]);
@@ -298,21 +353,46 @@ async function draftTools(deps, runId) {
298
353
  // `deniedBy`: where each deny was set (the agent's tool settings, the server's, a default), which a left-out draft's
299
354
  // words name.
300
355
  const { tools, gated, deniedBy, asked } = learnedDraftTools(granted, (name) => permissionFrom(name, server, agent, approvals).permission, undefined, (name) => permissionFrom(name, server, agent, approvals).from, { mcp: true, approvers: await readApprovers(deps.store.settings) });
301
- return { tools, gated, deniedBy, asked };
356
+ // A draft's decision nodes (LS-07): the host's decision providers, each held to the gate by its bare id as the run's
357
+ // gate holds it (tool-plane.ts DECISION_PROVIDER_KEYS); deny alone refuses one.
358
+ const providers = await deps.decisionProviders?.();
359
+ if (!providers)
360
+ return { tools, gated, deniedBy, asked };
361
+ const decided = {};
362
+ const decisionDeniedBy = {};
363
+ for (const id of DECISION_PROVIDER_KEYS) {
364
+ const { permission, from } = permissionFrom(id, server, agent, approvals);
365
+ if (permission === "allow")
366
+ continue;
367
+ decided[id] = permission;
368
+ if (permission === "deny")
369
+ decisionDeniedBy[id] = from;
370
+ }
371
+ return { tools, gated, deniedBy, asked, decisions: { providers: providers.providers, default: providers.default, gated: decided, deniedBy: decisionDeniedBy } };
372
+ }
373
+ /** The commit main of the agent's learning folder is at now; none for a folder with no main yet. */
374
+ async function mainOf(deps, agentId) {
375
+ const folder = deps.learningFolder(agentId);
376
+ try {
377
+ return (await readSnapshot(folder.workspace))?.main ?? undefined;
378
+ }
379
+ finally {
380
+ await folder.end();
381
+ }
302
382
  }
303
383
  /** proposeRun's checks over the turn, against seed `seed`, in a directory of the step's own: the proposal it keeps there
304
384
  * is read back for the hook to store with the run's end, and the directory is removed. */
305
385
  async function checkedOf(deps, input) {
306
386
  const runDir = await mkdtemp(join(tmpdir(), "agentrun-learning-end-"));
307
387
  try {
308
- const { tools, gated, deniedBy, asked } = await draftTools(deps, input.runId);
388
+ const { tools, gated, deniedBy, asked, decisions } = await draftTools(deps, input.runId);
309
389
  // #2424's card rule (learnedAskFits): a draft whose ask-set MCP tools would not all show on its approval card is refused.
310
390
  const { learnedAskFits } = await import("../learned-workflow.js");
311
391
  const turn = { answer: input.turn.answer, asked: [...input.turn.asked], ...(input.turn.triggered?.length ? { triggered: input.turn.triggered.map((one) => ({ ...one })) } : {}) };
312
392
  const folder = deps.learningFolder(input.agentId);
313
393
  let proposed;
314
394
  try {
315
- proposed = await proposeRun({ folder: folder.workspace, runDir, seed: input.seed, turn, refuse: input.refuse, outcome: input.outcome, tools, ...(Object.keys(gated).length ? { gated } : {}), ...(Object.keys(deniedBy).length ? { deniedBy } : {}), ask: { asked, fits: learnedAskFits }, ...(input.workflows === false ? { workflows: false } : {}) });
395
+ proposed = await proposeRun({ folder: folder.workspace, runDir, seed: input.seed, ...(input.notesSeed ? { notesSeed: input.notesSeed } : {}), turn, refuse: input.refuse, outcome: input.outcome, tools, ...(Object.keys(gated).length ? { gated } : {}), ...(Object.keys(deniedBy).length ? { deniedBy } : {}), ask: { asked, fits: learnedAskFits }, ...(decisions ? { decisions } : {}), ...(input.workflows === false ? { workflows: false } : {}) });
316
396
  }
317
397
  finally {
318
398
  await folder.end();
@@ -3,7 +3,7 @@ import { type LearningLoopDeps } from "./learning-loop.js";
3
3
  import type { LoopFactory } from "./loop-contract.js";
4
4
  /** The harness a learning run's launch names (store/sqlite/run-patch.ts learningLaunchOf). */
5
5
  export declare const LEARNING_HARNESS = "learning";
6
- export interface LearningFactoryDeps extends Pick<LearningLoopDeps, "ask" | "now"> {
6
+ export interface LearningFactoryDeps extends Pick<LearningLoopDeps, "ask" | "now" | "general" | "generalThreshold"> {
7
7
  readonly store: Pick<Store, "runs" | "agents" | "learning">;
8
8
  /** The server's data directory: where each agent's learning folder is. */
9
9
  readonly data: string;
@@ -8,7 +8,8 @@
8
8
  // readMain main as the grader reads it, from the folder's snapshot (learn/commit.ts readMain);
9
9
  // commit the folder's one write (learn/commit.ts learningCommit);
10
10
  // merged the run's merge when it is on main already, with the texts its own commit holds (learn/commit.ts mergedAt),
11
- // for a resume after its commit.
11
+ // for a resume after its commit;
12
+ // general the generality pre-check (learn/general.ts), on this host's Jev, when it has one (passed in by the wiring).
12
13
  import { learningCommit, mainTexts, mergedAt, readMain } from "../learn/commit.js";
13
14
  import { learningWorkspace } from "../learn/folder.js";
14
15
  import { createLearningLoop } from "./learning-loop.js";
@@ -20,22 +21,27 @@ export function learningLoopFactory(deps) {
20
21
  return createLearningLoop({
21
22
  sourceOf: async (learningRunId) => {
22
23
  const launch = await store.runs.launch(learningRunId);
23
- const runId = objectOf(objectOf(launch.body).input).runId;
24
+ const input = objectOf(objectOf(launch.body).input);
25
+ const runId = input.runId;
24
26
  if (typeof runId !== "string")
25
27
  return undefined;
26
28
  const source = await store.runs.get(runId);
27
29
  if (!source)
28
30
  return undefined;
29
31
  const instructions = objectOf((await store.agents.revision(source.revisionId))?.body).instructions;
30
- return { runId, agentId: source.agentId, ...(typeof instructions === "string" ? { instructions } : {}) };
32
+ const round = Number.isSafeInteger(input.round) && input.round > 1 ? { round: input.round } : {};
33
+ return { runId, ...round, agentId: source.agentId, ...(typeof instructions === "string" ? { instructions } : {}) };
31
34
  },
32
- proposal: (runId) => store.learning.proposal(runId),
35
+ proposal: (runId, round) => store.learning.proposal(runId, round),
33
36
  ask: deps.ask,
34
37
  folder: (agentId) => learningWorkspace({ mode: "local", agentId, data: deps.data, ...(deps.log ? { log: deps.log } : {}) }),
35
38
  readMain: async (ws, proposal) => (await readMain(ws, { notes: proposal.notes })),
36
39
  commit: async (ws, change) => (await learningCommit(ws, change)),
37
40
  mainTexts: (ws, paths) => mainTexts(ws, paths),
38
- merged: (ws, runId, paths) => mergedAt(ws, runId, paths),
41
+ merged: (ws, runId, paths, round) => mergedAt(ws, runId, paths, round),
39
42
  ...(deps.now ? { now: deps.now } : {}),
43
+ ...(deps.general ? { general: deps.general } : {}),
44
+ ...(deps.generalThreshold !== undefined ? { generalThreshold: deps.generalThreshold } : {}),
45
+ ...(deps.log ? { log: deps.log } : {}),
40
46
  });
41
47
  }
@@ -1,4 +1,5 @@
1
1
  import type { LearningProposal } from "../store/store.js";
2
+ import { type GeneralScores, type GeneralSource } from "../learn/general.js";
2
3
  import { type IndexEntry, type NoteText, type Verdict } from "../learn/grade.js";
3
4
  import type { LearnedFile, NoteLearned, Tokens } from "../learning.js";
4
5
  import type { Journal, Loop, LoopDeps } from "./loop-contract.js";
@@ -47,10 +48,37 @@ export type Problem = {
47
48
  readonly path?: string;
48
49
  readonly detail: string;
49
50
  };
51
+ /** A grader call its learning run's cap refused before it was sent (learning-ask.ts bookedAsk: the run's cap left, its
52
+ * launch's max_cost_usd): nothing of it was spent, and the grade is skipped, said so (said.noRoomToGrade), never run past
53
+ * the cap, never a failure. `forGood`: no room could ever send it (its spend or price unknown), not only none now. */
54
+ export declare class GradeOverCap extends Error {
55
+ readonly forGood: boolean;
56
+ readonly booked?: GradeBooked | undefined;
57
+ /** `booked`: the grade's records when it was refused (GradeBooked), what it spent before it. */
58
+ constructor(message: string, forGood: boolean, booked?: GradeBooked | undefined);
59
+ }
60
+ /** A generality check its learning run's cap refused before any of its requests left (learning-dispatch.ts general: its
61
+ * worst case over the cap left): nothing of it was sent or spent, and it is skipped as `unchecked` "no_budget"
62
+ * (GENERAL_NO_BUDGET), said so, the grader then judged against the cap on its own. `forGood`: no room could ever send it
63
+ * (its provider's price unknown here), not only none now. */
64
+ export declare class GeneralOverCap extends Error {
65
+ readonly forGood: boolean;
66
+ constructor(message: string, forGood: boolean);
67
+ }
68
+ /** The grade's own records after an ask (learning-ask.ts bookedAsk: its session's booking): what every grade request of
69
+ * the learning run spent, an earlier attempt's included (null: unknown, as a cut one's), and how many left. What the
70
+ * grade ends with is read from these, never summed in memory alone: a runner that restarted before the grade was saved
71
+ * holds no sum of what its earlier attempt spent. */
72
+ export type GradeBooked = {
73
+ readonly usd: number | null;
74
+ readonly turns: number;
75
+ };
50
76
  /** The folder's one write (learn/commit.ts learningCommit), a merge. */
51
77
  export interface MergeChange {
52
78
  readonly kind: "merge";
53
79
  readonly runId: string;
80
+ /** The round of the run's looks back it merges, past the first (learn/folder.ts runDiffKey). */
81
+ readonly round?: number;
54
82
  readonly reason: string;
55
83
  readonly files: readonly {
56
84
  readonly path: string;
@@ -95,14 +123,16 @@ export interface GraderRun {
95
123
  flush(): Promise<void>;
96
124
  }
97
125
  export interface LearningLoopDeps {
98
- /** The run that learned, from the learning run's launch row (its input's runId). */
126
+ /** The run that learned, from the learning run's launch row (its input's runId), and the round of its looks back the
127
+ * learning run grades (its input's round; absent, the first). */
99
128
  sourceOf(learningRunId: string): Promise<{
100
129
  readonly runId: string;
130
+ readonly round?: number;
101
131
  readonly agentId: string;
102
132
  readonly instructions?: string;
103
133
  } | undefined>;
104
- /** Its kept proposal (store.learning.proposal). */
105
- proposal(runId: string): Promise<LearningProposal | undefined>;
134
+ /** Its kept proposal of that round (store.learning.proposal). */
135
+ proposal(runId: string, round: number): Promise<LearningProposal | undefined>;
106
136
  /** The agent's learning folder (learn/folder.ts learningWorkspace): its Workspace, and its end, called once the run is
107
137
  * done with it, whichever way it goes out. */
108
138
  folder(agentId: string): {
@@ -113,14 +143,18 @@ export interface LearningLoopDeps {
113
143
  readMain(ws: unknown, proposal: KeptProposal): Promise<GradeMain | {
114
144
  readonly problems: readonly Problem[];
115
145
  }>;
116
- /** One grader call, through the kernel's model proxy and the cap, booked to the agent's learning meter. */
146
+ /** One grader call, through the kernel's model proxy and the cap, booked to the agent's learning meter. `beside`: what
147
+ * the grade spent outside its records, at most (the generality check's dollars, else its worst case), which the cap's
148
+ * room for the call leaves out. */
117
149
  ask(request: {
118
150
  readonly prompt: string;
119
151
  readonly maxTokens: number;
152
+ readonly beside?: number;
120
153
  }, signal: AbortSignal, run: GraderRun): Promise<{
121
154
  readonly text: unknown;
122
155
  readonly usd: number | null;
123
156
  readonly tokens?: Tokens | null;
157
+ readonly booked?: GradeBooked;
124
158
  }>;
125
159
  /** The folder's one write. */
126
160
  commit(ws: unknown, change: MergeChange): Promise<LearningCommitResult>;
@@ -130,21 +164,35 @@ export interface LearningLoopDeps {
130
164
  /** Run `runId`'s merge, when it is on main already (learn/folder.ts readRunDiff: its diff file, named by the run as its
131
165
  * commit's Run-Id trailer names it; a resume after its commit, before its end was recorded): the texts its own commit
132
166
  * holds at these note paths, as that merge saved them, whatever main says of them since. Undefined when it is not. */
133
- merged(ws: unknown, runId: string, paths: readonly string[]): Promise<{
167
+ merged(ws: unknown, runId: string, paths: readonly string[], round?: number): Promise<{
134
168
  readonly texts: Readonly<Record<string, string>>;
135
169
  } | undefined>;
136
170
  /** The clock (epoch ms), for a parked run's wake. */
137
171
  now?(): number;
172
+ /** The generality pre-check (learn/general.ts): each proposed note's Jev probability that it holds for inputs other
173
+ * than the one it came from, by file, and what it cost. Undefined, or one that answers undefined (no Jev key on this
174
+ * host), skips it: every note goes to the grader. One that throws (Jev down) skips it too: what it spent is unknown, and
175
+ * the skip is kept on the grade's entry (`unchecked`) and said in the log. `sending` is called once, before its first
176
+ * paid request leaves, with their worst case under a cap (`held`): the loop marks the check sent there, durably
177
+ * (journal-kinds.ts learningGeneralSent). Under a cap, `before` is what an earlier attempt's cut check may have spent
178
+ * (its worst case), which the room for this one leaves out; one that doesn't fit throws GeneralOverCap, sending nothing. */
179
+ general?(notes: readonly NoteText[], source: GeneralSource, signal: AbortSignal, run: string, sending?: (held?: number) => Promise<void>, before?: number): Promise<GeneralScores | undefined>;
180
+ /** The bar a note's score must reach to go on to the grader (default GENERAL_THRESHOLD). */
181
+ generalThreshold?: number;
182
+ /** One line of the runner's log. */
183
+ log?(line: string): void;
138
184
  }
139
185
  /** The journal's learning entries' data (journal-kinds.ts learningGraded, learningDropped, learningMerged), as the reader
140
186
  * of a run's learning end reads them (store-reads/learning.ts). What a grade spent: its dollars and tokens, each null when
141
- * any ask's are unknown. */
187
+ * any ask's are unknown. `unchecked`: why the generality pre-check could not run (its provider's error), so the grader read
188
+ * every note; the run's learning end says the check was skipped. */
142
189
  export type GradedData = {
143
190
  readonly source: string;
144
191
  readonly verdict: Verdict;
145
192
  readonly usd: number | null;
146
193
  readonly tokens?: Tokens | null;
147
194
  readonly graded: Readonly<Record<string, string>>;
195
+ readonly unchecked?: string;
148
196
  };
149
197
  export type DroppedData = {
150
198
  readonly source: string;
@@ -153,6 +201,7 @@ export type DroppedData = {
153
201
  readonly problems?: readonly Problem[];
154
202
  readonly usd?: number | null;
155
203
  readonly tokens?: Tokens | null;
204
+ readonly unchecked?: string;
156
205
  };
157
206
  /** `verdict`: each item's final decision once the merge settled it (takenOnMain's and the commit's left-out notes dropped,
158
207
  * with their words), which the run's learning end reads in place of the grade's (store-reads/learning.ts). */
@@ -164,6 +213,17 @@ export type MergedData = {
164
213
  readonly detail: string;
165
214
  readonly verdict: Verdict;
166
215
  };
216
+ /** The generality check's end (journal-kinds.ts learningGeneral): its scores, or why it could not run (GENERAL_NO_BUDGET:
217
+ * no room under the cap), and what it spent (null once unknown: a request that failed, or one an earlier attempt sent and
218
+ * a restart cut). `held`: under a cap, the most every attempt's requests may have spent (each sent mark's worst case), the
219
+ * bound the grader's room leaves out when `usd` is unknown. */
220
+ export type GeneralData = {
221
+ readonly source: string;
222
+ readonly scores?: Readonly<Record<string, number>>;
223
+ readonly unchecked?: string;
224
+ readonly usd: number | null;
225
+ readonly held?: number;
226
+ };
167
227
  /** The why of a note whose name main already holds with other text, which it does not supersede: never written over,
168
228
  * and the rest of what the run learned merges. */
169
229
  export declare const NOTE_NAME_TAKEN: (name: string) => string;
@@ -186,7 +246,7 @@ export declare function keptOf(proposal: KeptProposal, verdict: Verdict): KeptPr
186
246
  * the origin its item gives (`you` for the user's own words, else the run's) and held for its owner's OK when its item
187
247
  * is pending; a skill's SKILL.md or a draft's README.md held when its item is (a draft's workflow.json is held by its
188
248
  * README); and the seed the source run started from, so a skill's or draft's file changed on main since is a conflict. */
189
- export declare function mergeChangeOf(runId: string, merging: KeptProposal, verdict: Verdict, graded: Readonly<Record<string, string>>): MergeChange;
249
+ export declare function mergeChangeOf(runId: string, merging: KeptProposal, verdict: Verdict, graded: Readonly<Record<string, string>>, round?: number): MergeChange;
190
250
  /** What a merge of `merging` learned, as the server's grade named it on the run (learning-run.ts grade): the notes it
191
251
  * added that runs read, with what each supersedes; the ones waiting for their owner's OK; its skills and workflow
192
252
  * drafts, each pending when it waits. The merge's words and the reader of a run's learning end (store-reads/learning.ts)