@parcha/agentrun-serve 0.1.0-beta.58 → 0.1.0-beta.59

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 (375) hide show
  1. package/CHANGELOG.md +325 -1
  2. package/README.md +332 -88
  3. package/dist/acp-package.d.ts +5 -1
  4. package/dist/acp-package.js +19 -2
  5. package/dist/adapters.d.ts +23 -2
  6. package/dist/adapters.js +106 -33
  7. package/dist/agents-store.js +1 -1
  8. package/dist/answer-text.d.ts +3 -0
  9. package/dist/answer-text.js +22 -0
  10. package/dist/attach-conformance.js +5 -4
  11. package/dist/box-paths.d.ts +3 -2
  12. package/dist/box-paths.js +5 -4
  13. package/dist/browser-computer.d.ts +3 -8
  14. package/dist/browser-computer.js +12 -35
  15. package/dist/browser-cost.js +94 -20
  16. package/dist/capacity.d.ts +1 -1
  17. package/dist/capacity.js +3 -3
  18. package/dist/chatgpt-plan.d.ts +5 -0
  19. package/dist/chatgpt-plan.js +25 -11
  20. package/dist/claude-login-token.d.ts +15 -11
  21. package/dist/claude-login-token.js +167 -107
  22. package/dist/claude-login.d.ts +76 -31
  23. package/dist/claude-login.js +426 -79
  24. package/dist/claude-sign-in.d.ts +3 -0
  25. package/dist/claude-sign-in.js +10 -1
  26. package/dist/cli.js +323 -292
  27. package/dist/composio-made.d.ts +1 -6
  28. package/dist/composio-made.js +1 -6
  29. package/dist/composio-owners.d.ts +26 -0
  30. package/dist/composio-owners.js +36 -0
  31. package/dist/composio-preset.d.ts +56 -23
  32. package/dist/composio-preset.js +173 -77
  33. package/dist/composio-triggers.d.ts +4 -4
  34. package/dist/composio-triggers.js +6 -7
  35. package/dist/compute-cost.js +11 -2
  36. package/dist/computer-attach.d.ts +6 -3
  37. package/dist/computer-attach.js +41 -29
  38. package/dist/computer-held-routes.d.ts +1 -1
  39. package/dist/computer-held-routes.js +9 -4
  40. package/dist/computer-keys.d.ts +11 -1
  41. package/dist/computer-keys.js +26 -1
  42. package/dist/computer-record.js +3 -1
  43. package/dist/computer-view.d.ts +5 -3
  44. package/dist/computer-view.js +10 -6
  45. package/dist/computer-viewer.js +36 -7
  46. package/dist/computers.d.ts +2 -1
  47. package/dist/computers.js +6 -3
  48. package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
  49. package/dist/console/assets/backend-BztxEAvb.js +166 -0
  50. package/dist/console/assets/index-LwNL4L9q.js +619 -0
  51. package/dist/console/assets/share-L84tlUDW.js +6 -0
  52. package/dist/console/assets/styles-CaRKlHke.js +291 -0
  53. package/dist/console/assets/styles-DQo8rzY-.css +1 -0
  54. package/dist/console/index.html +4 -3
  55. package/dist/console/share.html +17 -0
  56. package/dist/decisions.d.ts +17 -7
  57. package/dist/decisions.js +41 -15
  58. package/dist/dial-relay.js +10 -4
  59. package/dist/docker-daemon.js +4 -5
  60. package/dist/docker-host-addresses.js +29 -11
  61. package/dist/email/account.d.ts +6 -0
  62. package/dist/email/account.js +6 -0
  63. package/dist/email/authentication.d.ts +21 -0
  64. package/dist/email/authentication.js +106 -0
  65. package/dist/email/email-faux.d.ts +11 -2
  66. package/dist/email/email-faux.js +21 -5
  67. package/dist/email/mail.d.ts +25 -0
  68. package/dist/email/mail.js +58 -2
  69. package/dist/email/names.js +2 -2
  70. package/dist/email/tools.d.ts +9 -4
  71. package/dist/email/tools.js +95 -13
  72. package/dist/env-switch.d.ts +12 -2
  73. package/dist/env-switch.js +23 -3
  74. package/dist/errors.d.ts +1 -0
  75. package/dist/errors.js +2 -0
  76. package/dist/fal-price.d.ts +11 -1
  77. package/dist/fal-price.js +33 -0
  78. package/dist/faux-computers.js +1 -1
  79. package/dist/filed-answer.d.ts +2 -2
  80. package/dist/filed-answer.js +2 -2
  81. package/dist/graph-served.d.ts +10 -5
  82. package/dist/graph-served.js +40 -10
  83. package/dist/handed-values.js +1 -2
  84. package/dist/harness-check.d.ts +2 -1
  85. package/dist/harness-check.js +45 -12
  86. package/dist/harness-keys.js +9 -6
  87. package/dist/harness-opencode/facts.js +2 -0
  88. package/dist/harness-recipe-entry.d.ts +9 -3
  89. package/dist/harness-recipe-entry.js +18 -9
  90. package/dist/harness-recipe.d.ts +21 -0
  91. package/dist/harness-recipe.js +25 -3
  92. package/dist/harness-table.d.ts +8 -0
  93. package/dist/harness-table.js +17 -6
  94. package/dist/helper-env.d.ts +4 -1
  95. package/dist/helper-env.js +4 -1
  96. package/dist/host-check.d.ts +2 -3
  97. package/dist/host-check.js +13 -7
  98. package/dist/hosted-search.js +1 -1
  99. package/dist/import.d.ts +11 -8
  100. package/dist/import.js +437 -186
  101. package/dist/key-refusal.d.ts +9 -2
  102. package/dist/key-refusal.js +86 -32
  103. package/dist/learn/audit.d.ts +8 -5
  104. package/dist/learn/audit.js +15 -7
  105. package/dist/learn/compile.d.ts +5 -2
  106. package/dist/learn/compile.js +45 -21
  107. package/dist/learn/field-check.d.ts +111 -0
  108. package/dist/learn/field-check.js +174 -0
  109. package/dist/learn/ledger.d.ts +32 -29
  110. package/dist/learn/ledger.js +26 -19
  111. package/dist/learn/promote.d.ts +79 -36
  112. package/dist/learn/promote.js +60 -65
  113. package/dist/learn/secrets.d.ts +4 -0
  114. package/dist/learn/secrets.js +12 -0
  115. package/dist/learned-nodes.d.ts +8 -5
  116. package/dist/learned-nodes.js +8 -5
  117. package/dist/learned-workflow.d.ts +2 -1
  118. package/dist/learned-workflow.js +13 -10
  119. package/dist/learning-cost.d.ts +25 -8
  120. package/dist/learning-cost.js +23 -8
  121. package/dist/learning-setting.d.ts +6 -0
  122. package/dist/learning-setting.js +11 -1
  123. package/dist/listen-first.d.ts +5 -4
  124. package/dist/listen-first.js +4 -5
  125. package/dist/live-reach.d.ts +7 -0
  126. package/dist/live-reach.js +7 -0
  127. package/dist/mcp-api-only-origins.d.ts +15 -0
  128. package/dist/mcp-api-only-origins.js +40 -0
  129. package/dist/mcp-gateway.d.ts +16 -2
  130. package/dist/mcp-gateway.js +41 -3
  131. package/dist/mcp-oauth.d.ts +1 -1
  132. package/dist/mcp-oauth.js +3 -2
  133. package/dist/mcp-probe.d.ts +3 -0
  134. package/dist/mcp-probe.js +3 -0
  135. package/dist/mcp-secret-writes.d.ts +24 -2
  136. package/dist/mcp-secret-writes.js +30 -2
  137. package/dist/mcp-servers-store.d.ts +52 -14
  138. package/dist/mcp-servers-store.js +165 -61
  139. package/dist/mcp-sources.d.ts +1 -2
  140. package/dist/mcp-sources.js +1 -2
  141. package/dist/mcp-store.d.ts +5 -5
  142. package/dist/mcp-store.js +3 -3
  143. package/dist/mcp.js +1 -1
  144. package/dist/model-gateway.d.ts +1 -1
  145. package/dist/model-gateway.js +20 -36
  146. package/dist/ollama-provider.d.ts +23 -9
  147. package/dist/ollama-provider.js +49 -20
  148. package/dist/openapi.js +202 -51
  149. package/dist/otel/genai.d.ts +83 -7
  150. package/dist/otel/genai.js +69 -9
  151. package/dist/otel/tracer.d.ts +42 -13
  152. package/dist/otel/tracer.js +241 -56
  153. package/dist/page.d.ts +7 -1
  154. package/dist/page.js +76 -22
  155. package/dist/paid-calls.d.ts +6 -4
  156. package/dist/paid-calls.js +15 -7
  157. package/dist/place-offers.d.ts +55 -34
  158. package/dist/place-offers.js +93 -54
  159. package/dist/place.d.ts +13 -14
  160. package/dist/place.js +56 -41
  161. package/dist/print.js +1 -1
  162. package/dist/provider.d.ts +3 -2
  163. package/dist/provider.js +17 -8
  164. package/dist/redact/match.d.ts +27 -9
  165. package/dist/redact/match.js +266 -57
  166. package/dist/run-census.d.ts +1 -1
  167. package/dist/run-census.js +4 -5
  168. package/dist/run-cut.js +4 -5
  169. package/dist/run-export-store.d.ts +15 -0
  170. package/dist/run-export-store.js +30 -0
  171. package/dist/run-export.d.ts +139 -0
  172. package/dist/run-export.js +379 -0
  173. package/dist/run-file-cut.d.ts +5 -0
  174. package/dist/run-file-cut.js +13 -0
  175. package/dist/run-launch.js +16 -3
  176. package/dist/run-model-key.d.ts +15 -6
  177. package/dist/run-model-key.js +30 -31
  178. package/dist/run-reads-over-store.js +4 -4
  179. package/dist/run-secrets.js +3 -5
  180. package/dist/run-timeline-served.js +3 -1
  181. package/dist/runnable.js +2 -2
  182. package/dist/runner/adapter-loop.d.ts +35 -18
  183. package/dist/runner/adapter-loop.js +94 -47
  184. package/dist/runner/adapter-run-spec.d.ts +14 -1
  185. package/dist/runner/adapter-run-spec.js +64 -28
  186. package/dist/runner/adapters/pi.js +4 -2
  187. package/dist/runner/audit.d.ts +8 -5
  188. package/dist/runner/audit.js +37 -17
  189. package/dist/runner/beside.js +5 -6
  190. package/dist/runner/claim-secrets.d.ts +10 -3
  191. package/dist/runner/claim-secrets.js +29 -13
  192. package/dist/runner/cli-run-spec.d.ts +18 -0
  193. package/dist/runner/cli-run-spec.js +81 -46
  194. package/dist/runner/compare-end.d.ts +2 -1
  195. package/dist/runner/compare-end.js +25 -10
  196. package/dist/runner/conformance/harness.js +2 -3
  197. package/dist/runner/conformance/subject.d.ts +4 -1
  198. package/dist/runner/harness-loops.js +3 -3
  199. package/dist/runner/held.d.ts +5 -2
  200. package/dist/runner/journal-kinds.d.ts +4 -1
  201. package/dist/runner/journal-kinds.js +22 -4
  202. package/dist/runner/learned-first.js +20 -4
  203. package/dist/runner/learning-end.d.ts +2 -8
  204. package/dist/runner/learning-end.js +11 -19
  205. package/dist/runner/live-ticker.js +7 -5
  206. package/dist/runner/loop-contract.d.ts +0 -2
  207. package/dist/runner/loop.d.ts +7 -0
  208. package/dist/runner/loop.js +1 -0
  209. package/dist/runner/main.js +12 -11
  210. package/dist/runner/mcp-claim.js +21 -5
  211. package/dist/runner/meta-tools.js +3 -2
  212. package/dist/runner/metrics.d.ts +7 -0
  213. package/dist/runner/metrics.js +19 -0
  214. package/dist/runner/model-route.d.ts +10 -7
  215. package/dist/runner/model-route.js +79 -15
  216. package/dist/runner/native-loop.js +10 -8
  217. package/dist/runner/presence.d.ts +1 -4
  218. package/dist/runner/presence.js +6 -8
  219. package/dist/runner/promotion.d.ts +25 -13
  220. package/dist/runner/promotion.js +125 -50
  221. package/dist/runner/redact.d.ts +3 -1
  222. package/dist/runner/redact.js +5 -3
  223. package/dist/runner/responses-fetch.d.ts +36 -0
  224. package/dist/runner/responses-fetch.js +511 -0
  225. package/dist/runner/run-child-host.d.ts +20 -5
  226. package/dist/runner/run-child-host.js +179 -77
  227. package/dist/runner/run-child.js +11 -4
  228. package/dist/runner/runner-check.js +5 -1
  229. package/dist/runner/runner.d.ts +2 -3
  230. package/dist/runner/runner.js +13 -9
  231. package/dist/runner/sdk-loop.js +8 -8
  232. package/dist/runner/spec-holds.d.ts +8 -1
  233. package/dist/runner/spec-holds.js +9 -4
  234. package/dist/runner/workflow-calls.d.ts +9 -0
  235. package/dist/runner/workflow-calls.js +150 -0
  236. package/dist/runner/workflow-first.d.ts +120 -32
  237. package/dist/runner/workflow-first.js +218 -94
  238. package/dist/runner/workflow-spec.js +1 -1
  239. package/dist/runs-over-store.js +8 -7
  240. package/dist/save-once.d.ts +14 -0
  241. package/dist/save-once.js +31 -0
  242. package/dist/scrub-command.d.ts +34 -31
  243. package/dist/scrub-command.js +116 -147
  244. package/dist/sdk/runs.d.ts +6 -0
  245. package/dist/sdk/runs.js +89 -10
  246. package/dist/seal-key.js +4 -4
  247. package/dist/secret-cut.d.ts +3 -0
  248. package/dist/secret-cut.js +7 -1
  249. package/dist/secret-rules.d.ts +8 -1
  250. package/dist/secret-rules.js +482 -53
  251. package/dist/secrets.d.ts +51 -9
  252. package/dist/secrets.js +311 -21
  253. package/dist/server-keys.d.ts +4 -3
  254. package/dist/server-keys.js +4 -3
  255. package/dist/server-secrets-store.js +2 -4
  256. package/dist/server.d.ts +15 -3
  257. package/dist/server.js +312 -80
  258. package/dist/share-context.d.ts +22 -0
  259. package/dist/share-context.js +76 -0
  260. package/dist/shell-lex.d.ts +105 -0
  261. package/dist/shell-lex.js +646 -0
  262. package/dist/sign-ins-store.d.ts +5 -7
  263. package/dist/sign-ins-store.js +12 -16
  264. package/dist/sign-ins.d.ts +7 -11
  265. package/dist/sign-ins.js +40 -41
  266. package/dist/skills/agentrun-serve/SKILL.md +5 -2
  267. package/dist/start-switches.d.ts +6 -5
  268. package/dist/start-switches.js +8 -4
  269. package/dist/stats.d.ts +5 -1
  270. package/dist/stats.js +1 -1
  271. package/dist/store/agent-body.js +2 -2
  272. package/dist/store/dialect.d.ts +4 -0
  273. package/dist/store/dialect.js +1 -0
  274. package/dist/store/index.d.ts +2 -2
  275. package/dist/store/index.js +1 -1
  276. package/dist/store/mcp-record-keys.d.ts +10 -0
  277. package/dist/store/mcp-record-keys.js +13 -0
  278. package/dist/store/postgres/dialect.js +2 -0
  279. package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
  280. package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
  281. package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
  282. package/dist/store/postgres/store.js +4 -0
  283. package/dist/store/schema.d.ts +33 -10
  284. package/dist/store/schema.js +70 -16
  285. package/dist/store/seal.d.ts +4 -1
  286. package/dist/store/sqlite/run-launch.d.ts +3 -10
  287. package/dist/store/sqlite/run-launch.js +12 -23
  288. package/dist/store/sqlite/run-patch.d.ts +17 -10
  289. package/dist/store/sqlite/run-patch.js +50 -13
  290. package/dist/store/sqlite/store.d.ts +3 -0
  291. package/dist/store/sqlite/store.js +116 -34
  292. package/dist/store/store-schema.js +3 -4
  293. package/dist/store/store.d.ts +67 -32
  294. package/dist/store/store.js +14 -11
  295. package/dist/store-reads/analytics.d.ts +2 -1
  296. package/dist/store-reads/analytics.js +7 -5
  297. package/dist/store-reads/learning.d.ts +35 -26
  298. package/dist/store-reads/learning.js +120 -92
  299. package/dist/store-reads/stats.d.ts +2 -2
  300. package/dist/store-reads/stats.js +13 -7
  301. package/dist/store-view.js +10 -7
  302. package/dist/suggestions.d.ts +1 -1
  303. package/dist/suggestions.js +29 -13
  304. package/dist/template-eval.d.ts +72 -9
  305. package/dist/template-eval.js +358 -64
  306. package/dist/timeline/labels.d.ts +1 -1
  307. package/dist/timeline/labels.js +4 -3
  308. package/dist/timeline/timeline.d.ts +23 -11
  309. package/dist/timeline/timeline.js +115 -31
  310. package/dist/timeline/web-calls.d.ts +8 -6
  311. package/dist/timeline/web-calls.js +10 -6
  312. package/dist/tool-gate.d.ts +13 -1
  313. package/dist/tool-gate.js +43 -3
  314. package/dist/tool-keys-store.js +2 -4
  315. package/dist/tool-keys.d.ts +11 -0
  316. package/dist/tool-keys.js +16 -0
  317. package/dist/tool-plane.d.ts +50 -11
  318. package/dist/tool-plane.js +94 -30
  319. package/dist/tools.d.ts +5 -1
  320. package/dist/tools.js +7 -7
  321. package/dist/trigger-email.js +12 -6
  322. package/dist/triggers.d.ts +15 -34
  323. package/dist/triggers.js +119 -126
  324. package/dist/validate.d.ts +8 -1
  325. package/dist/validate.js +163 -23
  326. package/dist/web-fake.d.ts +2 -1
  327. package/dist/web-fake.js +8 -6
  328. package/dist/web-fetch.d.ts +8 -6
  329. package/dist/web-fetch.js +13 -11
  330. package/dist/web-holds.d.ts +32 -0
  331. package/dist/web-holds.js +86 -0
  332. package/dist/web-run.d.ts +32 -5
  333. package/dist/web-run.js +75 -28
  334. package/dist/web-save.js +23 -12
  335. package/dist/web-search.js +14 -25
  336. package/dist/wires.d.ts +4 -3
  337. package/dist/wires.js +5 -4
  338. package/dist/workspace/archil-claim.d.ts +12 -5
  339. package/dist/workspace/archil-claim.js +45 -5
  340. package/dist/workspace/attach.js +52 -19
  341. package/dist/workspace/attached-computers.d.ts +6 -2
  342. package/dist/workspace/attached-computers.js +5 -4
  343. package/dist/workspace/computer-deps.d.ts +23 -6
  344. package/dist/workspace/computer-deps.js +35 -17
  345. package/dist/workspace/computer-for-claim.d.ts +21 -4
  346. package/dist/workspace/computer-for-claim.js +160 -54
  347. package/dist/workspace/computer-intervals.d.ts +2 -0
  348. package/dist/workspace/computer-intervals.js +1 -1
  349. package/dist/workspace/computer-janitor.d.ts +48 -14
  350. package/dist/workspace/computer-janitor.js +153 -44
  351. package/dist/workspace/computer-kept.d.ts +158 -32
  352. package/dist/workspace/computer-kept.js +288 -63
  353. package/dist/workspace/computer-providers.d.ts +8 -3
  354. package/dist/workspace/computer-providers.js +12 -5
  355. package/dist/workspace/index.d.ts +1 -1
  356. package/dist/workspace/index.js +1 -1
  357. package/dist/workspace/local.js +41 -1
  358. package/dist/workspace/mount-revoke-left.d.ts +31 -0
  359. package/dist/workspace/mount-revoke-left.js +72 -0
  360. package/dist/workspace/paired-route.js +4 -5
  361. package/dist/workspace/resolve-computer.d.ts +5 -2
  362. package/dist/workspace/resolve-computer.js +9 -3
  363. package/dist/workspace/run-end-computers.d.ts +11 -5
  364. package/dist/workspace/run-end-computers.js +153 -28
  365. package/dist/workspace/run-tools.d.ts +22 -3
  366. package/dist/workspace/run-tools.js +17 -5
  367. package/dist/workspace/run-workspace.d.ts +14 -1
  368. package/dist/workspace/run-workspace.js +34 -1
  369. package/dist/workspace/workspace.d.ts +6 -0
  370. package/package.json +12 -13
  371. package/skills/agentrun-serve/SKILL.md +5 -2
  372. package/templates/README.md +22 -6
  373. package/dist/console/assets/backend-CSAWUqtZ.js +0 -166
  374. package/dist/console/assets/index-Bwn1oTem.css +0 -1
  375. package/dist/console/assets/index-DPh29cyz.js +0 -747
@@ -39,9 +39,11 @@
39
39
  // such runner takes the dials the server relays to it (runners.host and .port, dial-relay.ts). Version 14 keeps when a run or
40
40
  // an agent was purged (runs.purged_at, agents.purged_at; DELETE /runs/{id} and /agents/{id}, agent-delete.ts): its row is
41
41
  // then a tombstone, its content gone and its cost kept. Version 15 keeps the disk a run's launch named as its own
42
- // (runs.disk; a run on one learns nothing, learn/learning-of.ts). The server upgrades these in place at its open
43
- // (listingUpgrade from 11, relayUpgrade from 12, purgeUpgrade from 13, then diskUpgrade from 14); a runner waits for it.
44
- export const SCHEMA_VERSION = 15;
42
+ // (runs.disk; a run on one learns nothing, learn/learning-of.ts). Version 16 keeps no epoch on a learning ledger row
43
+ // (learning_ledger; 15 kept one, which a runner of 15 writes at each end and this build's end does not). The server
44
+ // upgrades these in place at its open (listingUpgrade from 11, relayUpgrade from 12, purgeUpgrade from 13, diskUpgrade
45
+ // from 14, then ledgerUpgrade from 15); a runner waits for it.
46
+ export const SCHEMA_VERSION = 16;
45
47
  /** One answer per approval of a run: a second approve or deny command naming the same approval is refused
46
48
  * (StoreConstraint), so the first answer a person gives wins from the moment it is written, before the runner takes it
47
49
  * (runs-over-store.ts approve). In STORE_DDL, and added at a server open to a store an earlier build of this version made. */
@@ -85,9 +87,20 @@ export const TRIGGERS_TEMPLATE_ID = "ALTER TABLE triggers ADD COLUMN template_id
85
87
  /** A run's census (RunLaunchRow.census, run-census.ts): in STORE_DDL, and added at a server open to a store an earlier
86
88
  * build of this version made, as RUNS_LABELS is (a run ended before reads as never handed a secret). */
87
89
  export const RUN_LAUNCH_CENSUS = `ALTER TABLE run_launch ADD COLUMN ${json("census", true)}`;
88
- /** Every launch holding sealed bytes when the census was added (RUN_LAUNCH_CENSUS) marked CENSUS_PENDING (store.ts): its
89
- * end counts them, or its files read as uncounted. */
90
- export const RUN_LAUNCH_CENSUS_PENDING = `UPDATE run_launch SET census = '"pending"' WHERE sealed IS NOT NULL AND census IS NULL`;
90
+ /** The census "pending" of 0.1.0-beta.57, which marked a launch holding sealed bytes until an end counted them. This build
91
+ * keeps no such state: the server open that makes RUN_LAUNCH_CENSUS_ERASED settles each one left as this build reads it,
92
+ * as Postgres migration 0029 does. A launch still holding its sealed bytes gets null (its end counts them), and one an
93
+ * end erased gets "unscanned" (nothing counted them, so its files are withheld). */
94
+ export const RUN_LAUNCH_CENSUS_SETTLE = `UPDATE run_launch SET census = CASE WHEN sealed IS NULL THEN '"unscanned"' ELSE NULL END WHERE census = '"pending"'`;
95
+ /** An update that erases a launch's sealed bytes and leaves its census null writes "unscanned", as Postgres migration 0029's
96
+ * trigger does. This build's end always names a census when it erases them, so only an end by a runner of
97
+ * 0.1.0-beta.56 or earlier, still live beside this server, does that: its run's files are withheld, never read cut of
98
+ * the server's values alone. */
99
+ export const RUN_LAUNCH_CENSUS_ERASED = `CREATE TRIGGER IF NOT EXISTS run_launch_census_erased AFTER UPDATE OF sealed ON run_launch
100
+ WHEN OLD.sealed IS NOT NULL AND NEW.sealed IS NULL AND OLD.census IS NULL AND NEW.census IS NULL
101
+ BEGIN UPDATE run_launch SET census = '"unscanned"' WHERE run_id = NEW.run_id; END`;
102
+ /** Whether the store holds RUN_LAUNCH_CENSUS_ERASED: a store no server of this build opened yet holds none. */
103
+ export const RUN_LAUNCH_CENSUS_ERASED_MADE = `SELECT 1 AS found FROM sqlite_master WHERE type = 'trigger' AND name = 'run_launch_census_erased'`;
91
104
  /** A template package imported from GitHub (POST /v2/templates/import), by its id (`github:<owner>/<repo>[/<path>]`): its
92
105
  * source as given (ref and all), the full commit SHA read, the GitHub account that publishes it, and its files as read
93
106
  * (`files`, a JSON object of path to base64). A re-import of the id replaces the row. The server checks the files again
@@ -227,11 +240,11 @@ export const RUN_ATTEMPTS = `CREATE TABLE IF NOT EXISTS run_attempts (
227
240
  /** The learning ledger (learn/ledger.ts): one row a run, written by each patch that leaves the run ended (applyRunPatch,
228
241
  * in that patch's transaction) and by a healed cost: who answered it (`path`, null when its end named none), the
229
242
  * revision that ran it, why it was not served alone, the job's own $ and its rounds' look-back, and whether its cost is
230
- * known (`cost`), and the end it was written for (`epoch`: the run's epoch after that end, which every end moves).
231
- * Gone with the run's purge or delete (its cost stays on the run's tombstone). Figures only, no run text. In STORE_DDL,
232
- * and made at a server open on a store an earlier build of this version made: a run that ended before it has no row,
233
- * and one a runner of that build ended again has a row for an earlier end; its reader records it from its run row
234
- * either way (store-reads/learning.ts). */
243
+ * known (`cost`). Gone with the run's purge or delete (its cost stays on the run's tombstone). Figures only, no run
244
+ * text. In STORE_DDL, and made at a server open on a store an earlier build of this version made, which drops the
245
+ * `epoch` an earlier build kept on it (LEARNING_LEDGER_EPOCH). `workflow_calls`: its learned workflow calls
246
+ * (LedgerRow.workflowCalls) as JSON, `[]` for none; added where missing at a server open (LEARNING_LEDGER_ADDED), before
247
+ * the open's backfill records rows (run-patch.ts backfillLedger). */
235
248
  export const LEARNING_LEDGER = `CREATE TABLE IF NOT EXISTS learning_ledger (
236
249
  run_id TEXT PRIMARY KEY,
237
250
  agent_id TEXT NOT NULL,
@@ -243,9 +256,48 @@ export const LEARNING_LEDGER = `CREATE TABLE IF NOT EXISTS learning_ledger (
243
256
  job_usd REAL,
244
257
  look_back_usd REAL,
245
258
  cost TEXT NOT NULL CHECK (cost IN ('known', 'unknown', 'at_most')),
246
- epoch INTEGER NOT NULL
259
+ workflow_calls TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(workflow_calls))
247
260
  ) STRICT`;
261
+ /** The columns a learning_ledger an earlier build of this version made lacks, each added at a server open where missing:
262
+ * its rows' calls none (`[]`) until their run's next end writes them. */
263
+ export const LEARNING_LEDGER_ADDED = {
264
+ workflow_calls: "ALTER TABLE learning_ledger ADD COLUMN workflow_calls TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(workflow_calls))",
265
+ };
248
266
  export const LEARNING_LEDGER_AGENT = "CREATE INDEX IF NOT EXISTS learning_ledger_agent ON learning_ledger (agent_id)";
267
+ /** The agent's rows by path, newest first: what the read before the money rule's window walks (run-patch.ts
268
+ * BY_HAND_BEFORE), a few rows whatever the agent's history. In STORE_DDL, and made at every server open. */
269
+ export const LEARNING_LEDGER_BY_HAND = "CREATE INDEX IF NOT EXISTS learning_ledger_by_hand ON learning_ledger (agent_id, path, started_at, run_id)";
270
+ /** The `epoch` version 15 kept on each ledger row (the run's epoch at the end that wrote it), dropped at the server open
271
+ * that brings the store to 16 (ledgerUpgrade; postgres 0030_learning_ledger_epoch.sql). */
272
+ const LEARNING_LEDGER_EPOCH = "ALTER TABLE learning_ledger DROP COLUMN epoch";
273
+ /** The rows the ledger's first release (the build with LEARNING_LEDGER_EPOCH) could leave wrong once no reader corrects
274
+ * them: a row whose end named no path (a run by hand it left null, a shadow that failed first, with no audit spend), one
275
+ * behind its run's epoch (an end of an earlier runner after it), an unknown cost its run has now, and an audit's shadow
276
+ * on another path. Their rows and spend rows are deleted, before the epoch is, and the open's backfill records them
277
+ * again from their runs (run-patch.ts backfillLedger). The same statements as postgres 0030_learning_ledger_epoch.sql. */
278
+ const WRONG_LEDGER_ROWS = `SELECT l.run_id FROM learning_ledger l JOIN runs ON runs.id = l.run_id
279
+ WHERE l.path IS NULL OR l.epoch <> runs.epoch OR (l.cost = 'unknown' AND runs.cost_usd IS NOT NULL)
280
+ OR (substr(l.run_id, 1, 6) = 'audit.' AND l.path <> 'audit')`;
281
+ export const LEARNING_LEDGER_REPAIR = [
282
+ `DELETE FROM learning_spend WHERE run_id IN (${WRONG_LEDGER_ROWS})`,
283
+ `DELETE FROM learning_ledger WHERE run_id IN (${WRONG_LEDGER_ROWS})`,
284
+ ];
285
+ /** Version 15 → 16 on SQLite (the server's open, sqlite/store.ts), given the ledger's columns now, after the open's DDL
286
+ * write (the mark's column, STORE_META_LEDGER_BACKFILLED, among it): where the ledger still has its epoch (one 15 kept),
287
+ * the rows that release could leave wrong deleted (LEARNING_LEDGER_REPAIR), the mark set back to 0 so the open's
288
+ * backfill records them again, and the epoch dropped; then the version, in one durable write. A store from 14 or before
289
+ * has a ledger made at this open, with no epoch: the version alone. */
290
+ export function ledgerUpgrade(ledgerColumns) {
291
+ return [
292
+ ...(ledgerColumns.has("epoch") ? [...LEARNING_LEDGER_REPAIR, "UPDATE store_meta SET ledger_backfilled = 0 WHERE singleton = 1", LEARNING_LEDGER_EPOCH] : []),
293
+ "UPDATE store_meta SET schema_version = 16 WHERE singleton = 1",
294
+ ];
295
+ }
296
+ /** Whether every final run not purged has its ledger row (1), which backfillLedger writes once it has recorded the last:
297
+ * no later open reads the runs again. 0 on a store with runs no server recorded yet. In STORE_DDL, and added at a server
298
+ * open to a store an earlier build of this version made. */
299
+ const LEDGER_BACKFILLED = "ledger_backfilled INTEGER NOT NULL DEFAULT 0 CHECK (ledger_backfilled IN (0, 1))";
300
+ export const STORE_META_LEDGER_BACKFILLED = `ALTER TABLE store_meta ADD COLUMN ${LEDGER_BACKFILLED}`;
249
301
  /** Each learning cost, by the revision it is for (learn/ledger.ts SpendRow): a compile turn (`round`, the round that asked
250
302
  * it), a check's replay run or an audit's shadow run (round 0, `of_run` the run it audits), with its $ (null: unknown).
251
303
  * Written with its run's ledger row, replaced whole each time; gone with its run's purge or delete, and with the purge
@@ -374,7 +426,8 @@ export const STORE_DDL = [
374
426
  `CREATE TABLE IF NOT EXISTS store_meta (
375
427
  singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
376
428
  schema_version INTEGER NOT NULL,
377
- flushes INTEGER NOT NULL DEFAULT 0
429
+ flushes INTEGER NOT NULL DEFAULT 0,
430
+ ${LEDGER_BACKFILLED}
378
431
  ) STRICT`,
379
432
  // Agents and their revisions. A revision made from a template keeps its package: {name, version (SemVer), source
380
433
  // builtin|git, ref, sha, parameters}.
@@ -725,6 +778,7 @@ export const STORE_DDL = [
725
778
  RUN_ATTEMPTS,
726
779
  LEARNING_LEDGER,
727
780
  LEARNING_LEDGER_AGENT,
781
+ LEARNING_LEDGER_BY_HAND,
728
782
  LEARNING_SPEND,
729
783
  LEARNING_SPEND_AGENT,
730
784
  LEARNING_SPEND_OF_RUN,
@@ -802,9 +856,9 @@ export const STORE_DDL = [
802
856
  // written in the same transaction as the queued run, never after. `body` is the launch JSON (run-launch.ts: the first
803
857
  // input, title, place, effort, cap, and the trigger and origin that started it); `sealed` holds any secret sealed with
804
858
  // the server's key outside the store (AAD run_launch|<run_id>|sealed), or null when the launch holds none. `census` is
805
- // null when the launch never held any; "pending" from when it first does until an end counts them; then where in the
806
- // run's files they sat when that end erased them (run-census.ts), never the secrets, or "unscanned" when it erased them
807
- // with no census made under them (store.ts CENSUS_PENDING, CENSUS_UNSCANNED).
859
+ // null until an end erases what the launch holds; then where in the run's files they sat when that end erased them
860
+ // (run-census.ts), never the secrets, or "unscanned" when it erased them with no census made under them (store.ts
861
+ // CENSUS_UNSCANNED).
808
862
  `CREATE TABLE IF NOT EXISTS run_launch (
809
863
  run_id TEXT PRIMARY KEY REFERENCES runs (id),
810
864
  ${json("body")},
@@ -50,10 +50,13 @@ export declare function openMcpCredential(key: Buffer, row: {
50
50
  readonly sealed: Uint8Array | null;
51
51
  }): OpenedMcpCredential;
52
52
  /** A run's binding of a per-run Composio server: the Composio user its calls run as, and the connected account when the
53
- * caller names one. Never kept on the server's row: sealed in the run's launch alone. */
53
+ * caller names one. Never kept on the server's row: sealed in the run's launch alone. `by`: who handed it, the API token
54
+ * or the owner's session, sealed with it so the run can't change it; each claim reads it against the owner's Composio
55
+ * users (composio-owners.ts). A binding sealed before the mark has none. */
54
56
  export type ComposioBinding = {
55
57
  readonly user_id: string;
56
58
  readonly connected_account_id?: string;
59
+ readonly by?: "token" | "owner";
57
60
  };
58
61
  /** What a run's caller handed one MCP server (`mcp_auth.<server>`): a per_run server's headers, or a per-run Composio
59
62
  * server's binding (its key stays the server's own); each with the origin of the server's url when it was handed, so a
@@ -7,22 +7,15 @@ import type { SqliteExecutor } from "./connection.js";
7
7
  * member as written (readUsageMetadata, readLearningStarts) reads the column and retries on postgresUnicodeRefusal. */
8
8
  export declare const readableJson: (dialect: Dialect, column: string) => string;
9
9
  /** Writes a run's launch row inside `tx` (the same transaction as the queued run it belongs to): the run path and the
10
- * trigger path both call it, so the row lands with its run or not at all. A launch with sealed bytes takes the census
11
- * CENSUS_PENDING with them. */
10
+ * trigger path both call it, so the row lands with its run or not at all. Its census is null until an end writes one. */
12
11
  export declare function writeLaunch(tx: Pick<SqliteExecutor, "run">, runId: string, launch: RunLaunchInput, at: number): Promise<void>;
13
- /** The census a launch whose census is `census` and sealed bytes `sealed` takes when `next` is written as its sealed
14
- * bytes: as it is when `next` holds none, or an end counted what it held before (its census); CENSUS_PENDING when it
15
- * never held any; CENSUS_UNSCANNED over a CENSUS_PENDING whose sealed bytes an end erased without writing a census (a
16
- * runner from before the census), so the files that end left are still read as uncounted. On Postgres the table holds
17
- * the same rule too (migration 0027's trigger), for a server of an earlier build still writing during a rolling deploy.
18
- * A SQLite store has one server, so no earlier server writes beside this one: the rule lives here alone. */
19
- export declare function censusOnSeal(census: string | null, sealed: Uint8Array | null, next: Uint8Array | null): string | null;
20
12
  /** The disk a launch body names as the run's own (runs.disk, RunRow.disk): its `archil_disk` of kind "launch", by its id
21
13
  * (run-launch.ts launchBody writes it so); null for none, or its agent's own disk (the default disk). The one read of the
22
14
  * body's disk the store makes, at every create of a run, so no path that queues a run can leave it out. */
23
15
  export declare function launchDiskOf(launch: RunLaunchInput | undefined): string | null;
24
16
  /** A message's launch secrets written over the run's (runs.command, runs.reopen), inside `tx`: the bytes as given, or a
25
- * function's answer to the bytes the launch holds now, read in the same transaction. */
17
+ * function's answer to the bytes the launch holds now, read in the same transaction. The census stays as the last end
18
+ * wrote it: that end's count of where its turn's secrets sat, which the next end merges with its own (censusAfter). */
26
19
  export declare function writeLaunchSealed(tx: Pick<SqliteExecutor, "run" | "get">, runId: string, launchSealed: LaunchSealed): Promise<void>;
27
20
  /** The run's launch row, read by the runner at its claim (and the server for views). StoreLaunchMissing when the run has
28
21
  * none, never a default. */
@@ -2,7 +2,7 @@
2
2
  // trigger path (store.triggers.fire) both write a run's launch row inside their own transaction, through one
3
3
  // helper, with no second copy of the SQL. The launch body itself is opaque here (run-launch.ts owns its shape); sealed
4
4
  // bytes are the caller's, which the store never opens.
5
- import { CENSUS_PENDING, CENSUS_UNSCANNED, StoreLaunchMissing } from "../store.js";
5
+ import { StoreLaunchMissing } from "../store.js";
6
6
  const RUN_LAUNCH_COLUMNS = "run_id, body, sealed, census, created_at";
7
7
  const runLaunchOf = (r) => ({ runId: r.run_id, body: JSON.parse(r.body), sealed: r.sealed === null ? null : Uint8Array.from(r.sealed), census: r.census, createdAt: r.created_at });
8
8
  /** JSON text `column` (a launch body, a command's body, a journal record) as every read of one id, number or flag out of
@@ -11,24 +11,9 @@ const runLaunchOf = (r) => ({ runId: r.run_id, body: JSON.parse(r.body), sealed:
11
11
  * member as written (readUsageMetadata, readLearningStarts) reads the column and retries on postgresUnicodeRefusal. */
12
12
  export const readableJson = (dialect, column) => (dialect.name === "postgres" ? postgresReadableJson(column) : column);
13
13
  /** Writes a run's launch row inside `tx` (the same transaction as the queued run it belongs to): the run path and the
14
- * trigger path both call it, so the row lands with its run or not at all. A launch with sealed bytes takes the census
15
- * CENSUS_PENDING with them. */
14
+ * trigger path both call it, so the row lands with its run or not at all. Its census is null until an end writes one. */
16
15
  export function writeLaunch(tx, runId, launch, at) {
17
- const sealed = launch.sealed ?? null;
18
- return tx.run("INSERT INTO run_launch (run_id, body, sealed, census, created_at) VALUES (?, ?, ?, ?, ?)", runId, JSON.stringify(launch.body ?? null), sealed, sealed === null ? null : CENSUS_PENDING, at);
19
- }
20
- /** The census a launch whose census is `census` and sealed bytes `sealed` takes when `next` is written as its sealed
21
- * bytes: as it is when `next` holds none, or an end counted what it held before (its census); CENSUS_PENDING when it
22
- * never held any; CENSUS_UNSCANNED over a CENSUS_PENDING whose sealed bytes an end erased without writing a census (a
23
- * runner from before the census), so the files that end left are still read as uncounted. On Postgres the table holds
24
- * the same rule too (migration 0027's trigger), for a server of an earlier build still writing during a rolling deploy.
25
- * A SQLite store has one server, so no earlier server writes beside this one: the rule lives here alone. */
26
- export function censusOnSeal(census, sealed, next) {
27
- if (next === null)
28
- return census;
29
- if (census === null)
30
- return CENSUS_PENDING;
31
- return census === CENSUS_PENDING && sealed === null ? CENSUS_UNSCANNED : census;
16
+ return tx.run("INSERT INTO run_launch (run_id, body, sealed, created_at) VALUES (?, ?, ?, ?)", runId, JSON.stringify(launch.body ?? null), launch.sealed ?? null, at);
32
17
  }
33
18
  /** The disk a launch body names as the run's own (runs.disk, RunRow.disk): its `archil_disk` of kind "launch", by its id
34
19
  * (run-launch.ts launchBody writes it so); null for none, or its agent's own disk (the default disk). The one read of the
@@ -43,13 +28,17 @@ export function launchDiskOf(launch) {
43
28
  const { disk_id: id, kind } = disk;
44
29
  return kind === "launch" && typeof id === "string" && id !== "" ? id : null;
45
30
  }
31
+ /** The sealed bytes run `runId`'s launch holds now, read inside `tx`. */
32
+ async function sealedOf(tx, runId) {
33
+ const row = await tx.get("SELECT sealed FROM run_launch WHERE run_id = ?", runId);
34
+ return row?.sealed == null ? null : Uint8Array.from(row.sealed);
35
+ }
46
36
  /** A message's launch secrets written over the run's (runs.command, runs.reopen), inside `tx`: the bytes as given, or a
47
- * function's answer to the bytes the launch holds now, read in the same transaction. */
37
+ * function's answer to the bytes the launch holds now, read in the same transaction. The census stays as the last end
38
+ * wrote it: that end's count of where its turn's secrets sat, which the next end merges with its own (censusAfter). */
48
39
  export async function writeLaunchSealed(tx, runId, launchSealed) {
49
- const row = await tx.get("SELECT sealed, census FROM run_launch WHERE run_id = ?", runId);
50
- const sealed = row?.sealed == null ? null : Uint8Array.from(row.sealed);
51
- const next = typeof launchSealed === "function" ? launchSealed(sealed) : launchSealed;
52
- await tx.run("UPDATE run_launch SET sealed = ?, census = ? WHERE run_id = ?", next, censusOnSeal(row?.census ?? null, sealed, next), runId);
40
+ const next = typeof launchSealed === "function" ? launchSealed(await sealedOf(tx, runId)) : launchSealed;
41
+ await tx.run("UPDATE run_launch SET sealed = ? WHERE run_id = ?", next, runId);
53
42
  }
54
43
  /** The run's launch row, read by the runner at its claim (and the server for views). StoreLaunchMissing when the run has
55
44
  * none, never a default. */
@@ -1,10 +1,16 @@
1
1
  import { type AlsoRow, type Claim, type RunPatch, type RunRow, type RunState } from "../store.js";
2
2
  import { type Dialect } from "../dialect.js";
3
- import type { SqliteExecutor } from "./connection.js";
3
+ import type { SqliteExecutor, StoreConnection } from "./connection.js";
4
4
  import { type StoreNow } from "../fence.js";
5
5
  /** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
6
6
  * list of runs names. */
7
7
  export declare const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
8
+ /** Learning.byHandBefore on both stores (agent, the window's oldest run's start and id, how many): the agent's newest
9
+ * rows on the `agent` path (the one by-hand rule, learn/ledger.ts record) before that run, of a known cost, on no named
10
+ * disk, neither imported, archived, purged, a learning run nor a workflow revision's run (a standalone workflow's row is
11
+ * on the `agent` path, and the window, store-reads/learning.ts ledgerReadOf, leaves those runs out too). It walks
12
+ * schema.ts LEARNING_LEDGER_BY_HAND newest first and stops at `limit`: no sort of the agent's ledger. */
13
+ export declare const BY_HAND_BEFORE = "SELECT l.run_id, l.started_at, l.job_usd FROM learning_ledger l JOIN runs ON runs.id = l.run_id\n WHERE l.agent_id = ? AND l.path = 'agent' AND (l.started_at, l.run_id) < (?, ?) AND l.cost = 'known' AND l.job_usd IS NOT NULL\n AND runs.id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL) AND runs.disk IS NULL AND runs.imported_from IS NULL AND runs.archived_at IS NULL AND runs.purged_at IS NULL\n AND NOT EXISTS (SELECT 1 FROM revisions WHERE revisions.id = runs.revision_id AND revisions.kind = 'workflow')\n ORDER BY l.started_at DESC, l.run_id DESC LIMIT ?";
8
14
  /** The lock a write of a run's sign_in_handed and a sign-in's forget both take (Dialect.lockKey): the forget's check of
9
15
  * the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
10
16
  export declare const SIGN_IN_HANDED_LOCK = "sign_in_handed";
@@ -47,15 +53,16 @@ export declare const runOf: (r: RunRecord) => RunRow;
47
53
  * clock (fence.ts storeNow) the fence and left_running_at read; `now` bound by default. */
48
54
  export declare function applyRunPatch(tx: Pick<SqliteExecutor, "run" | "get" | "all">, claim: Pick<Claim, "runId" | "epoch">, patch: RunPatch, also: readonly AlsoRow[], now: number, dialect?: Dialect, clock?: StoreNow): Promise<RunRow>;
49
55
  /** Writes run `run`'s ledger row and spend rows (learn/ledger.ts record) through `tx`, from its row and every round its
50
- * learning kept, in place of any it had: the one writer of learning_ledger and learning_spend (a run's end, and a heal
51
- * of its cost). Each row says the end it was written for (`epoch`: the run's epoch after that end), which a reader
52
- * checks against the run's (store-reads/learning.ts ledgerRowStands). Every end moves the epoch (applyRunPatch's
53
- * `ending`, Runs.failUnclaimed), on every build, and so does every claim before one; an end that keeps its job's end
54
- * (`keepsEnd`: a grade's round) leaves ended_at where it was, so ended_at never says which end a row is for. A runner
55
- * or server of the build before the ledger, ending the run again, writes none, and its row reads as stale until a later
56
- * end writes it again. A cancel that wins an end (refuseLeftovers, after this) moves ended_at, never the epoch: the row
57
- * still stands. */
58
- export declare function recordLedger(tx: Pick<SqliteExecutor, "run" | "all">, run: RunRow, now: number): Promise<void>;
56
+ * learning kept, in place of any it had: the one writer of learning_ledger and learning_spend (a run's end, a heal of
57
+ * its cost, an import, and the server's open: backfillLedger). */
58
+ export declare function recordLedger(tx: Pick<SqliteExecutor, "run" | "all" | "get">, run: RunRow, now: number): Promise<void>;
59
+ /** Records every final run that has no ledger row (UNRECORDED), as its end would have (recordLedger), at a server's open
60
+ * on either store: by id, BACKFILL_PAGE runs a transaction, each read again in it, so a stop partway keeps each page it
61
+ * committed and the next open records the rest. Then it marks the ledger whole (store_meta.ledger_backfilled), and no
62
+ * later open reads the runs again: each end, heal and import writes its own row, and no reader records one. Each run's
63
+ * row is locked before it is read again (a no-op on SQLite's one writer): on Postgres another server's heal or end of it
64
+ * in flight commits first and the read sees its row, so the backfill never writes over it with what it read before. */
65
+ export declare function backfillLedger(connection: Pick<StoreConnection, "all" | "dialect" | "get" | "transaction">, now: () => number): Promise<void>;
59
66
  /** The one reopen a run that ended may take (runs.reopen, and a run's end with a person's message waiting): back to running
60
67
  * with no runner (released: a runner claims it at once), its end's own fields cleared, its epoch, journal, cost and
61
68
  * metrics kept. `where` holds only for an agentic run that ended completed or that a person stopped (state 'stopped',
@@ -6,11 +6,21 @@
6
6
  import { CENSUS_UNSCANNED, endedReason, FINAL_STATES, Fenced } from "../store.js";
7
7
  import { settledBody, SQLITE } from "../dialect.js";
8
8
  import { claimFence, storeNow } from "../fence.js";
9
- import { readableJson } from "./run-launch.js";
10
- import { record } from "../../learn/ledger.js";
9
+ import { readableJson, readLaunch } from "./run-launch.js";
10
+ import { auditOfLaunch, learnsOfLaunch, record } from "../../learn/ledger.js";
11
11
  /** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
12
12
  * list of runs names. */
13
13
  export const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
14
+ /** Learning.byHandBefore on both stores (agent, the window's oldest run's start and id, how many): the agent's newest
15
+ * rows on the `agent` path (the one by-hand rule, learn/ledger.ts record) before that run, of a known cost, on no named
16
+ * disk, neither imported, archived, purged, a learning run nor a workflow revision's run (a standalone workflow's row is
17
+ * on the `agent` path, and the window, store-reads/learning.ts ledgerReadOf, leaves those runs out too). It walks
18
+ * schema.ts LEARNING_LEDGER_BY_HAND newest first and stops at `limit`: no sort of the agent's ledger. */
19
+ export const BY_HAND_BEFORE = `SELECT l.run_id, l.started_at, l.job_usd FROM learning_ledger l JOIN runs ON runs.id = l.run_id
20
+ WHERE l.agent_id = ? AND l.path = 'agent' AND (l.started_at, l.run_id) < (?, ?) AND l.cost = 'known' AND l.job_usd IS NOT NULL
21
+ AND runs.${NOT_A_LEARNING_RUN} AND runs.disk IS NULL AND runs.imported_from IS NULL AND runs.archived_at IS NULL AND runs.purged_at IS NULL
22
+ AND NOT EXISTS (SELECT 1 FROM revisions WHERE revisions.id = runs.revision_id AND revisions.kind = 'workflow')
23
+ ORDER BY l.started_at DESC, l.run_id DESC LIMIT ?`;
14
24
  /** The lock a write of a run's sign_in_handed and a sign-in's forget both take (Dialect.lockKey): the forget's check of
15
25
  * the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
16
26
  export const SIGN_IN_HANDED_LOCK = "sign_in_handed";
@@ -111,26 +121,53 @@ export async function applyRunPatch(tx, claim, patch, also, now, dialect = SQLIT
111
121
  return run;
112
122
  }
113
123
  /** Writes run `run`'s ledger row and spend rows (learn/ledger.ts record) through `tx`, from its row and every round its
114
- * learning kept, in place of any it had: the one writer of learning_ledger and learning_spend (a run's end, and a heal
115
- * of its cost). Each row says the end it was written for (`epoch`: the run's epoch after that end), which a reader
116
- * checks against the run's (store-reads/learning.ts ledgerRowStands). Every end moves the epoch (applyRunPatch's
117
- * `ending`, Runs.failUnclaimed), on every build, and so does every claim before one; an end that keeps its job's end
118
- * (`keepsEnd`: a grade's round) leaves ended_at where it was, so ended_at never says which end a row is for. A runner
119
- * or server of the build before the ledger, ending the run again, writes none, and its row reads as stale until a later
120
- * end writes it again. A cancel that wins an end (refuseLeftovers, after this) moves ended_at, never the epoch: the row
121
- * still stands. */
124
+ * learning kept, in place of any it had: the one writer of learning_ledger and learning_spend (a run's end, a heal of
125
+ * its cost, an import, and the server's open: backfillLedger). */
122
126
  export async function recordLedger(tx, run, now) {
123
127
  const rounds = (await tx.all("SELECT round, proposal FROM learning_proposals WHERE run_id = ? ORDER BY round", run.id))
124
128
  .map((one) => ({ round: Number(one.round), proposal: JSON.parse(one.proposal) }));
125
- const { row, spend } = record({ id: run.id, agentId: run.agentId, startedAt: run.startedAt, costUsd: run.costUsd, costAtMostUsd: run.costAtMostUsd, metrics: run.metrics, rounds });
126
- await tx.run(`INSERT INTO learning_ledger (run_id, agent_id, started_at, path, workflow, digest, not_served, job_usd, look_back_usd, cost, epoch) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
129
+ // An audit's shadow is booked on the run its launch audits, whatever its end named (one that failed first names none);
130
+ // a run whose end named no path is the agent's when its launch does not learn (learnsOfLaunch).
131
+ const body = (await readLaunch(tx, run.id).catch(() => undefined))?.body;
132
+ const auditOf = auditOfLaunch(run.id, body);
133
+ const { row, spend } = record({ id: run.id, agentId: run.agentId, startedAt: run.startedAt, costUsd: run.costUsd, costAtMostUsd: run.costAtMostUsd, metrics: run.metrics, rounds, learns: learnsOfLaunch(body), ...(auditOf === undefined ? {} : { auditOf }) });
134
+ await tx.run(`INSERT INTO learning_ledger (run_id, agent_id, started_at, path, workflow, digest, not_served, job_usd, look_back_usd, cost, workflow_calls) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
127
135
  ON CONFLICT (run_id) DO UPDATE SET agent_id = excluded.agent_id, started_at = excluded.started_at, path = excluded.path, workflow = excluded.workflow, digest = excluded.digest,
128
- not_served = excluded.not_served, job_usd = excluded.job_usd, look_back_usd = excluded.look_back_usd, cost = excluded.cost, epoch = excluded.epoch`, row.runId, row.agentId, row.startedAt, row.path, row.workflow, row.digest, row.notServed, row.jobUsd, row.lookBackUsd, row.cost, run.epoch);
136
+ not_served = excluded.not_served, job_usd = excluded.job_usd, look_back_usd = excluded.look_back_usd, cost = excluded.cost, workflow_calls = excluded.workflow_calls`, row.runId, row.agentId, row.startedAt, row.path, row.workflow, row.digest, row.notServed, row.jobUsd, row.lookBackUsd, row.cost, JSON.stringify(row.workflowCalls));
129
137
  await tx.run("DELETE FROM learning_spend WHERE run_id = ?", run.id);
130
138
  for (const one of spend) {
131
139
  await tx.run("INSERT INTO learning_spend (run_id, kind, round, agent_id, of_run, workflow, digest, usd, at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", one.runId, one.kind, one.round, one.agentId, one.ofRun, one.workflow, one.digest, one.usd, now);
132
140
  }
133
141
  }
142
+ /** A final run with no ledger row, not purged (its purge took its row): one an earlier build ended. */
143
+ const UNRECORDED = `state IN (${FINAL_STATES.map((state) => `'${state}'`).join(", ")}) AND purged_at IS NULL AND NOT EXISTS (SELECT 1 FROM learning_ledger WHERE learning_ledger.run_id = runs.id)`;
144
+ /** How many runs backfillLedger records in one transaction. */
145
+ const BACKFILL_PAGE = 64;
146
+ /** Records every final run that has no ledger row (UNRECORDED), as its end would have (recordLedger), at a server's open
147
+ * on either store: by id, BACKFILL_PAGE runs a transaction, each read again in it, so a stop partway keeps each page it
148
+ * committed and the next open records the rest. Then it marks the ledger whole (store_meta.ledger_backfilled), and no
149
+ * later open reads the runs again: each end, heal and import writes its own row, and no reader records one. Each run's
150
+ * row is locked before it is read again (a no-op on SQLite's one writer): on Postgres another server's heal or end of it
151
+ * in flight commits first and the read sees its row, so the backfill never writes over it with what it read before. */
152
+ export async function backfillLedger(connection, now) {
153
+ if (Number((await connection.get("SELECT ledger_backfilled AS whole FROM store_meta WHERE singleton = 1"))?.whole) === 1)
154
+ return;
155
+ for (let after = "";;) {
156
+ const page = await connection.all(`SELECT id FROM runs WHERE id > ? AND ${UNRECORDED} ORDER BY id LIMIT ${BACKFILL_PAGE}`, after);
157
+ if (page.length === 0)
158
+ break;
159
+ await connection.transaction(async (tx) => {
160
+ for (const { id } of page) {
161
+ await connection.dialect.lockRow(tx, "runs", "id", id);
162
+ const row = await tx.get(`SELECT ${RUN_COLUMNS} FROM runs WHERE id = ? AND ${UNRECORDED}`, id);
163
+ if (row)
164
+ await recordLedger(tx, runOf(row), now());
165
+ }
166
+ });
167
+ after = page.at(-1).id;
168
+ }
169
+ await connection.transaction((tx) => tx.run("UPDATE store_meta SET ledger_backfilled = 1 WHERE singleton = 1").then(() => undefined));
170
+ }
134
171
  /** The one reopen a run that ended may take (runs.reopen, and a run's end with a person's message waiting): back to running
135
172
  * with no runner (released: a runner claims it at once), its end's own fields cleared, its epoch, journal, cost and
136
173
  * metrics kept. `where` holds only for an agentic run that ended completed or that a person stopped (state 'stopped',
@@ -22,6 +22,9 @@ export type SqliteStoreOptions = (
22
22
  /** How long a write waits for another process's lock before StoreBusy. */
23
23
  busyTimeoutMs?: number;
24
24
  };
25
+ /** The store's earlier versions the server upgrades in place at its open (openSqliteStore), which a runner waits for
26
+ * (StoreAwaitsServer); store-schema reports from the same set. */
27
+ export declare const SQLITE_UPGRADES: ReadonlySet<number>;
25
28
  /** The columns a runner of this build needs that an earlier build of this same version may not have: the server adds each
26
29
  * at its start (openSqliteStore), and a runner on a file without one waits for that server (StoreAwaitsServer, in `why`'s
27
30
  * words; exit 3). store-schema reads the same list, so its report says "waits" where the runner would. */