@parcha/agentrun-serve 0.1.0-beta.57 → 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 (404) hide show
  1. package/CHANGELOG.md +376 -0
  2. package/README.md +393 -118
  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/api-only.d.ts +2 -4
  11. package/dist/api-only.js +2 -4
  12. package/dist/attach-conformance.js +5 -4
  13. package/dist/box-paths.d.ts +7 -5
  14. package/dist/box-paths.js +12 -8
  15. package/dist/browser-computer.d.ts +3 -8
  16. package/dist/browser-computer.js +12 -35
  17. package/dist/browser-cost.js +94 -20
  18. package/dist/capacity.d.ts +1 -1
  19. package/dist/capacity.js +3 -3
  20. package/dist/chatgpt-plan.d.ts +5 -0
  21. package/dist/chatgpt-plan.js +25 -11
  22. package/dist/claude-login-token.d.ts +121 -0
  23. package/dist/claude-login-token.js +797 -0
  24. package/dist/claude-login.d.ts +119 -34
  25. package/dist/claude-login.js +551 -79
  26. package/dist/claude-sign-in.d.ts +3 -0
  27. package/dist/claude-sign-in.js +10 -1
  28. package/dist/cli.js +325 -328
  29. package/dist/composio-made.d.ts +1 -6
  30. package/dist/composio-made.js +1 -6
  31. package/dist/composio-owners.d.ts +26 -0
  32. package/dist/composio-owners.js +36 -0
  33. package/dist/composio-preset.d.ts +56 -23
  34. package/dist/composio-preset.js +173 -77
  35. package/dist/composio-triggers.d.ts +4 -4
  36. package/dist/composio-triggers.js +6 -7
  37. package/dist/compute-cost.js +11 -2
  38. package/dist/computer-attach.d.ts +40 -19
  39. package/dist/computer-attach.js +326 -62
  40. package/dist/computer-held-routes.d.ts +1 -1
  41. package/dist/computer-held-routes.js +9 -4
  42. package/dist/computer-keys.d.ts +11 -1
  43. package/dist/computer-keys.js +26 -1
  44. package/dist/computer-record.js +3 -1
  45. package/dist/computer-view.d.ts +5 -3
  46. package/dist/computer-view.js +10 -6
  47. package/dist/computer-viewer.js +36 -7
  48. package/dist/computers.d.ts +12 -11
  49. package/dist/computers.js +11 -8
  50. package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
  51. package/dist/console/assets/backend-BztxEAvb.js +166 -0
  52. package/dist/console/assets/index-LwNL4L9q.js +619 -0
  53. package/dist/console/assets/share-L84tlUDW.js +6 -0
  54. package/dist/console/assets/styles-CaRKlHke.js +291 -0
  55. package/dist/console/assets/styles-DQo8rzY-.css +1 -0
  56. package/dist/console/index.html +4 -3
  57. package/dist/console/share.html +17 -0
  58. package/dist/decisions.d.ts +17 -7
  59. package/dist/decisions.js +41 -15
  60. package/dist/dial-relay.d.ts +25 -4
  61. package/dist/dial-relay.js +139 -5
  62. package/dist/docker-daemon.js +4 -5
  63. package/dist/docker-host-addresses.js +29 -11
  64. package/dist/email/account.d.ts +6 -0
  65. package/dist/email/account.js +6 -0
  66. package/dist/email/authentication.d.ts +21 -0
  67. package/dist/email/authentication.js +106 -0
  68. package/dist/email/email-faux.d.ts +11 -2
  69. package/dist/email/email-faux.js +21 -5
  70. package/dist/email/mail.d.ts +25 -0
  71. package/dist/email/mail.js +58 -2
  72. package/dist/email/names.js +2 -2
  73. package/dist/email/tools.d.ts +9 -4
  74. package/dist/email/tools.js +95 -13
  75. package/dist/env-switch.d.ts +13 -5
  76. package/dist/env-switch.js +24 -6
  77. package/dist/errors.d.ts +1 -0
  78. package/dist/errors.js +2 -0
  79. package/dist/fal-price.d.ts +12 -4
  80. package/dist/fal-price.js +110 -26
  81. package/dist/faux-computers.js +1 -1
  82. package/dist/filed-answer.d.ts +2 -2
  83. package/dist/filed-answer.js +2 -2
  84. package/dist/graph-served.d.ts +10 -5
  85. package/dist/graph-served.js +40 -10
  86. package/dist/handed-values.js +1 -2
  87. package/dist/harness-check.d.ts +2 -1
  88. package/dist/harness-check.js +45 -12
  89. package/dist/harness-keys.js +9 -6
  90. package/dist/harness-opencode/facts.js +2 -0
  91. package/dist/harness-recipe-entry.d.ts +9 -3
  92. package/dist/harness-recipe-entry.js +18 -9
  93. package/dist/harness-recipe.d.ts +21 -0
  94. package/dist/harness-recipe.js +25 -3
  95. package/dist/harness-table.d.ts +8 -0
  96. package/dist/harness-table.js +17 -6
  97. package/dist/helper-env.d.ts +4 -1
  98. package/dist/helper-env.js +4 -1
  99. package/dist/host-check.d.ts +2 -3
  100. package/dist/host-check.js +13 -7
  101. package/dist/hosted-search.js +1 -1
  102. package/dist/import.d.ts +11 -8
  103. package/dist/import.js +437 -186
  104. package/dist/key-refusal.d.ts +9 -2
  105. package/dist/key-refusal.js +86 -32
  106. package/dist/learn/audit.d.ts +8 -5
  107. package/dist/learn/audit.js +15 -7
  108. package/dist/learn/compile.d.ts +5 -2
  109. package/dist/learn/compile.js +45 -21
  110. package/dist/learn/field-check.d.ts +111 -0
  111. package/dist/learn/field-check.js +174 -0
  112. package/dist/learn/ledger.d.ts +173 -0
  113. package/dist/learn/ledger.js +225 -0
  114. package/dist/learn/promote.d.ts +82 -39
  115. package/dist/learn/promote.js +61 -66
  116. package/dist/learn/secrets.d.ts +4 -0
  117. package/dist/learn/secrets.js +12 -0
  118. package/dist/learned-nodes.d.ts +8 -5
  119. package/dist/learned-nodes.js +8 -5
  120. package/dist/learned-workflow.d.ts +2 -1
  121. package/dist/learned-workflow.js +13 -10
  122. package/dist/learning-cost.d.ts +40 -21
  123. package/dist/learning-cost.js +30 -21
  124. package/dist/learning-setting.d.ts +6 -0
  125. package/dist/learning-setting.js +11 -1
  126. package/dist/listen-first.d.ts +5 -4
  127. package/dist/listen-first.js +4 -5
  128. package/dist/live-reach.d.ts +9 -0
  129. package/dist/live-reach.js +9 -0
  130. package/dist/mcp-api-only-origins.d.ts +23 -3
  131. package/dist/mcp-api-only-origins.js +57 -17
  132. package/dist/mcp-gateway.d.ts +16 -2
  133. package/dist/mcp-gateway.js +41 -3
  134. package/dist/mcp-oauth.d.ts +1 -1
  135. package/dist/mcp-oauth.js +3 -2
  136. package/dist/mcp-probe.d.ts +3 -0
  137. package/dist/mcp-probe.js +3 -0
  138. package/dist/mcp-secret-writes.d.ts +24 -2
  139. package/dist/mcp-secret-writes.js +30 -2
  140. package/dist/mcp-servers-store.d.ts +52 -14
  141. package/dist/mcp-servers-store.js +165 -61
  142. package/dist/mcp-sources.d.ts +1 -2
  143. package/dist/mcp-sources.js +1 -2
  144. package/dist/mcp-store.d.ts +5 -5
  145. package/dist/mcp-store.js +3 -3
  146. package/dist/mcp.js +1 -1
  147. package/dist/micro.d.ts +2 -0
  148. package/dist/micro.js +2 -0
  149. package/dist/model-gateway.d.ts +1 -1
  150. package/dist/model-gateway.js +20 -36
  151. package/dist/model.d.ts +5 -4
  152. package/dist/model.js +7 -6
  153. package/dist/monid.d.ts +14 -1
  154. package/dist/monid.js +103 -26
  155. package/dist/ollama-provider.d.ts +23 -9
  156. package/dist/ollama-provider.js +49 -20
  157. package/dist/openapi.js +234 -61
  158. package/dist/otel/genai.d.ts +83 -7
  159. package/dist/otel/genai.js +69 -9
  160. package/dist/otel/tracer.d.ts +42 -13
  161. package/dist/otel/tracer.js +241 -56
  162. package/dist/page.d.ts +7 -1
  163. package/dist/page.js +76 -22
  164. package/dist/paid-calls.d.ts +6 -4
  165. package/dist/paid-calls.js +15 -7
  166. package/dist/place-offers.d.ts +59 -34
  167. package/dist/place-offers.js +100 -58
  168. package/dist/place.d.ts +13 -14
  169. package/dist/place.js +56 -41
  170. package/dist/print.js +1 -1
  171. package/dist/provider.d.ts +5 -4
  172. package/dist/provider.js +26 -13
  173. package/dist/redact/match.d.ts +27 -9
  174. package/dist/redact/match.js +266 -57
  175. package/dist/run-census.d.ts +1 -1
  176. package/dist/run-census.js +4 -5
  177. package/dist/run-cut.js +4 -5
  178. package/dist/run-export-store.d.ts +15 -0
  179. package/dist/run-export-store.js +30 -0
  180. package/dist/run-export.d.ts +139 -0
  181. package/dist/run-export.js +379 -0
  182. package/dist/run-file-cut.d.ts +5 -0
  183. package/dist/run-file-cut.js +13 -0
  184. package/dist/run-launch.js +16 -3
  185. package/dist/run-learning-served.d.ts +2 -2
  186. package/dist/run-model-key.d.ts +15 -6
  187. package/dist/run-model-key.js +30 -31
  188. package/dist/run-reads-over-store.js +4 -4
  189. package/dist/run-secrets.js +3 -5
  190. package/dist/run-timeline-served.js +3 -1
  191. package/dist/run-view.d.ts +1 -2
  192. package/dist/run-view.js +2 -1
  193. package/dist/runnable.js +2 -2
  194. package/dist/runner/adapter-loop.d.ts +35 -18
  195. package/dist/runner/adapter-loop.js +97 -63
  196. package/dist/runner/adapter-run-spec.d.ts +34 -14
  197. package/dist/runner/adapter-run-spec.js +120 -75
  198. package/dist/runner/adapters/pi.js +4 -2
  199. package/dist/runner/audit.d.ts +8 -5
  200. package/dist/runner/audit.js +37 -17
  201. package/dist/runner/beside.d.ts +9 -2
  202. package/dist/runner/beside.js +29 -13
  203. package/dist/runner/claim-secrets.d.ts +27 -18
  204. package/dist/runner/claim-secrets.js +61 -47
  205. package/dist/runner/cli-run-spec.d.ts +18 -0
  206. package/dist/runner/cli-run-spec.js +93 -46
  207. package/dist/runner/compare-end.d.ts +2 -1
  208. package/dist/runner/compare-end.js +25 -10
  209. package/dist/runner/conformance/harness.js +2 -3
  210. package/dist/runner/conformance/subject.d.ts +4 -1
  211. package/dist/runner/harness-loops.js +3 -3
  212. package/dist/runner/held.d.ts +19 -4
  213. package/dist/runner/journal-kinds.d.ts +4 -1
  214. package/dist/runner/journal-kinds.js +22 -4
  215. package/dist/runner/learned-first.js +20 -4
  216. package/dist/runner/learning-end.d.ts +2 -8
  217. package/dist/runner/learning-end.js +11 -19
  218. package/dist/runner/live-ticker.js +7 -5
  219. package/dist/runner/loop-contract.d.ts +0 -2
  220. package/dist/runner/loop.d.ts +7 -0
  221. package/dist/runner/loop.js +1 -0
  222. package/dist/runner/main.d.ts +11 -6
  223. package/dist/runner/main.js +24 -18
  224. package/dist/runner/mcp-claim.js +24 -7
  225. package/dist/runner/mcp-run.d.ts +3 -6
  226. package/dist/runner/mcp-run.js +3 -4
  227. package/dist/runner/meta-tools.js +3 -2
  228. package/dist/runner/metrics.d.ts +7 -0
  229. package/dist/runner/metrics.js +19 -0
  230. package/dist/runner/model-route.d.ts +10 -7
  231. package/dist/runner/model-route.js +79 -15
  232. package/dist/runner/native-loop.js +10 -8
  233. package/dist/runner/presence.d.ts +11 -4
  234. package/dist/runner/presence.js +11 -6
  235. package/dist/runner/promotion.d.ts +25 -13
  236. package/dist/runner/promotion.js +125 -50
  237. package/dist/runner/redact.d.ts +3 -1
  238. package/dist/runner/redact.js +5 -3
  239. package/dist/runner/reflect-hook.js +1 -1
  240. package/dist/runner/responses-fetch.d.ts +36 -0
  241. package/dist/runner/responses-fetch.js +511 -0
  242. package/dist/runner/run-child-host.d.ts +38 -12
  243. package/dist/runner/run-child-host.js +231 -83
  244. package/dist/runner/run-child.js +11 -4
  245. package/dist/runner/runner-check.js +5 -1
  246. package/dist/runner/runner.d.ts +5 -3
  247. package/dist/runner/runner.js +14 -10
  248. package/dist/runner/save-slots.js +2 -1
  249. package/dist/runner/sdk-loop.js +8 -8
  250. package/dist/runner/spec-holds.d.ts +8 -1
  251. package/dist/runner/spec-holds.js +9 -4
  252. package/dist/runner/subagent.d.ts +11 -0
  253. package/dist/runner/subagent.js +17 -3
  254. package/dist/runner/workflow-calls.d.ts +9 -0
  255. package/dist/runner/workflow-calls.js +150 -0
  256. package/dist/runner/workflow-first.d.ts +138 -37
  257. package/dist/runner/workflow-first.js +230 -99
  258. package/dist/runner/workflow-spec.js +2 -2
  259. package/dist/runs-over-store.js +8 -7
  260. package/dist/save-once.d.ts +14 -0
  261. package/dist/save-once.js +31 -0
  262. package/dist/scrub-command.d.ts +34 -31
  263. package/dist/scrub-command.js +116 -147
  264. package/dist/sdk/runs.d.ts +6 -0
  265. package/dist/sdk/runs.js +89 -10
  266. package/dist/seal-key.js +4 -4
  267. package/dist/secret-cut.d.ts +3 -0
  268. package/dist/secret-cut.js +7 -1
  269. package/dist/secret-rules.d.ts +8 -1
  270. package/dist/secret-rules.js +482 -53
  271. package/dist/secrets.d.ts +51 -9
  272. package/dist/secrets.js +311 -21
  273. package/dist/server-keys.d.ts +4 -3
  274. package/dist/server-keys.js +4 -3
  275. package/dist/server-secrets-store.js +2 -4
  276. package/dist/server.d.ts +33 -10
  277. package/dist/server.js +454 -136
  278. package/dist/share-context.d.ts +22 -0
  279. package/dist/share-context.js +76 -0
  280. package/dist/shell-lex.d.ts +105 -0
  281. package/dist/shell-lex.js +646 -0
  282. package/dist/sign-ins-store.d.ts +5 -7
  283. package/dist/sign-ins-store.js +12 -16
  284. package/dist/sign-ins.d.ts +7 -11
  285. package/dist/sign-ins.js +40 -41
  286. package/dist/skills/agentrun-serve/SKILL.md +5 -2
  287. package/dist/slots.d.ts +9 -0
  288. package/dist/slots.js +45 -0
  289. package/dist/start-switches.d.ts +6 -5
  290. package/dist/start-switches.js +8 -4
  291. package/dist/stats.d.ts +5 -1
  292. package/dist/stats.js +1 -1
  293. package/dist/store/agent-body.js +2 -2
  294. package/dist/store/dialect.d.ts +4 -0
  295. package/dist/store/dialect.js +1 -0
  296. package/dist/store/index.d.ts +2 -2
  297. package/dist/store/index.js +1 -1
  298. package/dist/store/mcp-record-keys.d.ts +10 -0
  299. package/dist/store/mcp-record-keys.js +13 -0
  300. package/dist/store/postgres/dialect.js +2 -0
  301. package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
  302. package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
  303. package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
  304. package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
  305. package/dist/store/postgres/store.js +4 -0
  306. package/dist/store/schema.d.ts +44 -4
  307. package/dist/store/schema.js +111 -11
  308. package/dist/store/seal.d.ts +4 -1
  309. package/dist/store/sqlite/run-launch.d.ts +3 -10
  310. package/dist/store/sqlite/run-launch.js +12 -23
  311. package/dist/store/sqlite/run-patch.d.ts +19 -2
  312. package/dist/store/sqlite/run-patch.js +67 -2
  313. package/dist/store/sqlite/store.d.ts +3 -0
  314. package/dist/store/sqlite/store.js +177 -32
  315. package/dist/store/store-schema.js +3 -4
  316. package/dist/store/store.d.ts +74 -25
  317. package/dist/store/store.js +14 -11
  318. package/dist/store-reads/analytics.d.ts +2 -1
  319. package/dist/store-reads/analytics.js +7 -5
  320. package/dist/store-reads/learning.d.ts +102 -136
  321. package/dist/store-reads/learning.js +251 -302
  322. package/dist/store-reads/stats.d.ts +2 -2
  323. package/dist/store-reads/stats.js +13 -7
  324. package/dist/store-view.js +10 -7
  325. package/dist/suggestions.d.ts +1 -1
  326. package/dist/suggestions.js +29 -13
  327. package/dist/template-eval.d.ts +91 -9
  328. package/dist/template-eval.js +448 -105
  329. package/dist/template-import.d.ts +0 -10
  330. package/dist/template-import.js +5 -10
  331. package/dist/timeline/labels.d.ts +1 -1
  332. package/dist/timeline/labels.js +4 -3
  333. package/dist/timeline/timeline.d.ts +23 -11
  334. package/dist/timeline/timeline.js +115 -31
  335. package/dist/timeline/web-calls.d.ts +14 -14
  336. package/dist/timeline/web-calls.js +18 -30
  337. package/dist/tool-gate.d.ts +13 -1
  338. package/dist/tool-gate.js +43 -3
  339. package/dist/tool-keys-store.js +2 -4
  340. package/dist/tool-keys.d.ts +11 -0
  341. package/dist/tool-keys.js +16 -0
  342. package/dist/tool-plane.d.ts +50 -11
  343. package/dist/tool-plane.js +94 -30
  344. package/dist/tools.d.ts +5 -1
  345. package/dist/tools.js +7 -7
  346. package/dist/trigger-email.js +12 -6
  347. package/dist/triggers.d.ts +15 -34
  348. package/dist/triggers.js +119 -126
  349. package/dist/validate.d.ts +8 -1
  350. package/dist/validate.js +163 -23
  351. package/dist/web-fake.d.ts +2 -1
  352. package/dist/web-fake.js +8 -6
  353. package/dist/web-fetch.d.ts +8 -6
  354. package/dist/web-fetch.js +13 -11
  355. package/dist/web-holds.d.ts +32 -0
  356. package/dist/web-holds.js +86 -0
  357. package/dist/web-run.d.ts +32 -5
  358. package/dist/web-run.js +75 -28
  359. package/dist/web-save.d.ts +4 -9
  360. package/dist/web-save.js +27 -55
  361. package/dist/web-search.js +14 -25
  362. package/dist/wires.d.ts +4 -3
  363. package/dist/wires.js +5 -4
  364. package/dist/workspace/archil-claim.d.ts +12 -5
  365. package/dist/workspace/archil-claim.js +45 -5
  366. package/dist/workspace/attach.js +52 -19
  367. package/dist/workspace/attached-computers.d.ts +38 -11
  368. package/dist/workspace/attached-computers.js +81 -16
  369. package/dist/workspace/computer-deps.d.ts +29 -7
  370. package/dist/workspace/computer-deps.js +36 -18
  371. package/dist/workspace/computer-for-claim.d.ts +21 -4
  372. package/dist/workspace/computer-for-claim.js +166 -57
  373. package/dist/workspace/computer-intervals.d.ts +2 -0
  374. package/dist/workspace/computer-intervals.js +1 -1
  375. package/dist/workspace/computer-janitor.d.ts +48 -14
  376. package/dist/workspace/computer-janitor.js +153 -44
  377. package/dist/workspace/computer-kept.d.ts +158 -32
  378. package/dist/workspace/computer-kept.js +288 -63
  379. package/dist/workspace/computer-providers.d.ts +8 -3
  380. package/dist/workspace/computer-providers.js +12 -5
  381. package/dist/workspace/index.d.ts +1 -1
  382. package/dist/workspace/index.js +1 -1
  383. package/dist/workspace/local.js +41 -1
  384. package/dist/workspace/mount-revoke-left.d.ts +31 -0
  385. package/dist/workspace/mount-revoke-left.js +72 -0
  386. package/dist/workspace/paired-route.d.ts +91 -0
  387. package/dist/workspace/paired-route.js +152 -0
  388. package/dist/workspace/resolve-computer.d.ts +5 -2
  389. package/dist/workspace/resolve-computer.js +9 -3
  390. package/dist/workspace/run-end-computers.d.ts +11 -5
  391. package/dist/workspace/run-end-computers.js +153 -28
  392. package/dist/workspace/run-tools.d.ts +22 -3
  393. package/dist/workspace/run-tools.js +17 -5
  394. package/dist/workspace/run-workspace.d.ts +14 -1
  395. package/dist/workspace/run-workspace.js +34 -1
  396. package/dist/workspace/workspace.d.ts +6 -0
  397. package/dist/wrapped.d.ts +8 -2
  398. package/dist/wrapped.js +21 -6
  399. package/package.json +12 -13
  400. package/skills/agentrun-serve/SKILL.md +5 -2
  401. package/templates/README.md +25 -9
  402. package/dist/console/assets/backend-DVhHzJX2.js +0 -166
  403. package/dist/console/assets/index-Bwn1oTem.css +0 -1
  404. package/dist/console/assets/index-okP-y8PK.js +0 -747
@@ -4,6 +4,7 @@
4
4
  // run on one ahead of them while its runner floor is at most what they know (migrate.ts runnerFloorOf).
5
5
  import { SchemaVersionUnknown, StoreAwaitsServer } from "../store.js";
6
6
  import { backfillNativeCommandOwners } from '../native-command-owner.js';
7
+ import { backfillLedger } from "../sqlite/run-patch.js";
7
8
  import { storeOver } from "../sqlite/store.js";
8
9
  import { PgConnection, SCHEMA_NAME } from "./connection.js";
9
10
  import { migrate, migrationsIn, placeOf, refuseBelowFloor, runnerFloorOf, versionOf } from "./migrate.js";
@@ -64,6 +65,9 @@ export async function openPostgresStore(options) {
64
65
  }
65
66
  if (role === "server")
66
67
  await backfillNativeCommandOwners(connection);
68
+ // Every run an earlier build ended gets its ledger row once, here, as on SQLite (run-patch.ts backfillLedger).
69
+ if (role === "server")
70
+ await backfillLedger(connection, now);
67
71
  if (role === "runner" && options.warn) {
68
72
  const gap = now() - (await connection.clock());
69
73
  if (Math.abs(gap) > CLOCK_WARN_MS) {
@@ -1,4 +1,4 @@
1
- export declare const SCHEMA_VERSION = 15;
1
+ export declare const SCHEMA_VERSION = 16;
2
2
  /** One answer per approval of a run: a second approve or deny command naming the same approval is refused
3
3
  * (StoreConstraint), so the first answer a person gives wins from the moment it is written, before the runner takes it
4
4
  * (runs-over-store.ts approve). In STORE_DDL, and added at a server open to a store an earlier build of this version made. */
@@ -31,9 +31,18 @@ export declare const TRIGGERS_TEMPLATE_ID = "ALTER TABLE triggers ADD COLUMN tem
31
31
  /** A run's census (RunLaunchRow.census, run-census.ts): in STORE_DDL, and added at a server open to a store an earlier
32
32
  * build of this version made, as RUNS_LABELS is (a run ended before reads as never handed a secret). */
33
33
  export declare const RUN_LAUNCH_CENSUS: string;
34
- /** Every launch holding sealed bytes when the census was added (RUN_LAUNCH_CENSUS) marked CENSUS_PENDING (store.ts): its
35
- * end counts them, or its files read as uncounted. */
36
- export declare const RUN_LAUNCH_CENSUS_PENDING = "UPDATE run_launch SET census = '\"pending\"' WHERE sealed IS NOT NULL AND census IS NULL";
34
+ /** The census "pending" of 0.1.0-beta.57, which marked a launch holding sealed bytes until an end counted them. This build
35
+ * keeps no such state: the server open that makes RUN_LAUNCH_CENSUS_ERASED settles each one left as this build reads it,
36
+ * as Postgres migration 0029 does. A launch still holding its sealed bytes gets null (its end counts them), and one an
37
+ * end erased gets "unscanned" (nothing counted them, so its files are withheld). */
38
+ export declare const RUN_LAUNCH_CENSUS_SETTLE = "UPDATE run_launch SET census = CASE WHEN sealed IS NULL THEN '\"unscanned\"' ELSE NULL END WHERE census = '\"pending\"'";
39
+ /** An update that erases a launch's sealed bytes and leaves its census null writes "unscanned", as Postgres migration 0029's
40
+ * trigger does. This build's end always names a census when it erases them, so only an end by a runner of
41
+ * 0.1.0-beta.56 or earlier, still live beside this server, does that: its run's files are withheld, never read cut of
42
+ * the server's values alone. */
43
+ export declare const RUN_LAUNCH_CENSUS_ERASED = "CREATE TRIGGER IF NOT EXISTS run_launch_census_erased AFTER UPDATE OF sealed ON run_launch\n WHEN OLD.sealed IS NOT NULL AND NEW.sealed IS NULL AND OLD.census IS NULL AND NEW.census IS NULL\n BEGIN UPDATE run_launch SET census = '\"unscanned\"' WHERE run_id = NEW.run_id; END";
44
+ /** Whether the store holds RUN_LAUNCH_CENSUS_ERASED: a store no server of this build opened yet holds none. */
45
+ export declare const RUN_LAUNCH_CENSUS_ERASED_MADE = "SELECT 1 AS found FROM sqlite_master WHERE type = 'trigger' AND name = 'run_launch_census_erased'";
37
46
  /** A template package imported from GitHub (POST /v2/templates/import), by its id (`github:<owner>/<repo>[/<path>]`): its
38
47
  * source as given (ref and all), the full commit SHA read, the GitHub account that publishes it, and its files as read
39
48
  * (`files`, a JSON object of path to base64). A re-import of the id replaces the row. The server checks the files again
@@ -103,6 +112,37 @@ export declare const HAND_BACKS_KEPT_MS: number;
103
112
  * added at a server open to a store an earlier build of this version made, as COMPUTER_INTERVALS is: a claim made before
104
113
  * it has no row. */
105
114
  export declare const RUN_ATTEMPTS = "CREATE TABLE IF NOT EXISTS run_attempts (\n run_id TEXT NOT NULL,\n epoch INTEGER NOT NULL,\n turn INTEGER NOT NULL CHECK (turn >= 1),\n attempt INTEGER NOT NULL CHECK (attempt >= 1),\n cause TEXT CHECK (cause IS NULL OR cause IN ('drain', 'crash', 'lease_lapse', 'retry')),\n cut TEXT CHECK (cut IS NULL OR cut IN ('drain', 'lease_lapse', 'retry')),\n runner TEXT NOT NULL,\n from_state TEXT NOT NULL,\n journal_from INTEGER NOT NULL,\n claimed_at INTEGER NOT NULL,\n PRIMARY KEY (run_id, epoch)\n ) STRICT";
115
+ /** The learning ledger (learn/ledger.ts): one row a run, written by each patch that leaves the run ended (applyRunPatch,
116
+ * in that patch's transaction) and by a healed cost: who answered it (`path`, null when its end named none), the
117
+ * revision that ran it, why it was not served alone, the job's own $ and its rounds' look-back, and whether its cost is
118
+ * known (`cost`). Gone with the run's purge or delete (its cost stays on the run's tombstone). Figures only, no run
119
+ * text. In STORE_DDL, and made at a server open on a store an earlier build of this version made, which drops the
120
+ * `epoch` an earlier build kept on it (LEARNING_LEDGER_EPOCH). `workflow_calls`: its learned workflow calls
121
+ * (LedgerRow.workflowCalls) as JSON, `[]` for none; added where missing at a server open (LEARNING_LEDGER_ADDED), before
122
+ * the open's backfill records rows (run-patch.ts backfillLedger). */
123
+ export declare const LEARNING_LEDGER = "CREATE TABLE IF NOT EXISTS learning_ledger (\n run_id TEXT PRIMARY KEY,\n agent_id TEXT NOT NULL,\n started_at INTEGER NOT NULL,\n path TEXT CHECK (path IS NULL OR path IN ('agent', 'handed_back', 'workflow', 'replay', 'audit')),\n workflow TEXT,\n digest TEXT,\n not_served TEXT CHECK (not_served IS NULL OR not_served IN ('cost_stop', 'cut')),\n job_usd REAL,\n look_back_usd REAL,\n cost TEXT NOT NULL CHECK (cost IN ('known', 'unknown', 'at_most')),\n workflow_calls TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(workflow_calls))\n ) STRICT";
124
+ /** The columns a learning_ledger an earlier build of this version made lacks, each added at a server open where missing:
125
+ * its rows' calls none (`[]`) until their run's next end writes them. */
126
+ export declare const LEARNING_LEDGER_ADDED: Readonly<Record<string, string>>;
127
+ export declare const LEARNING_LEDGER_AGENT = "CREATE INDEX IF NOT EXISTS learning_ledger_agent ON learning_ledger (agent_id)";
128
+ /** The agent's rows by path, newest first: what the read before the money rule's window walks (run-patch.ts
129
+ * BY_HAND_BEFORE), a few rows whatever the agent's history. In STORE_DDL, and made at every server open. */
130
+ export declare const LEARNING_LEDGER_BY_HAND = "CREATE INDEX IF NOT EXISTS learning_ledger_by_hand ON learning_ledger (agent_id, path, started_at, run_id)";
131
+ export declare const LEARNING_LEDGER_REPAIR: readonly string[];
132
+ /** Version 15 → 16 on SQLite (the server's open, sqlite/store.ts), given the ledger's columns now, after the open's DDL
133
+ * write (the mark's column, STORE_META_LEDGER_BACKFILLED, among it): where the ledger still has its epoch (one 15 kept),
134
+ * the rows that release could leave wrong deleted (LEARNING_LEDGER_REPAIR), the mark set back to 0 so the open's
135
+ * backfill records them again, and the epoch dropped; then the version, in one durable write. A store from 14 or before
136
+ * has a ledger made at this open, with no epoch: the version alone. */
137
+ export declare function ledgerUpgrade(ledgerColumns: ReadonlySet<string>): readonly string[];
138
+ export declare const STORE_META_LEDGER_BACKFILLED = "ALTER TABLE store_meta ADD COLUMN ledger_backfilled INTEGER NOT NULL DEFAULT 0 CHECK (ledger_backfilled IN (0, 1))";
139
+ /** Each learning cost, by the revision it is for (learn/ledger.ts SpendRow): a compile turn (`round`, the round that asked
140
+ * it), a check's replay run or an audit's shadow run (round 0, `of_run` the run it audits), with its $ (null: unknown).
141
+ * Written with its run's ledger row, replaced whole each time; gone with its run's purge or delete, and with the purge
142
+ * or delete of the run it audits. */
143
+ export declare const LEARNING_SPEND = "CREATE TABLE IF NOT EXISTS learning_spend (\n run_id TEXT NOT NULL,\n kind TEXT NOT NULL CHECK (kind IN ('compile', 'check', 'audit')),\n round INTEGER NOT NULL CHECK (round >= 0),\n agent_id TEXT NOT NULL,\n of_run TEXT,\n workflow TEXT,\n digest TEXT,\n usd REAL,\n at INTEGER NOT NULL,\n PRIMARY KEY (run_id, kind, round)\n ) STRICT";
144
+ export declare const LEARNING_SPEND_AGENT = "CREATE INDEX IF NOT EXISTS learning_spend_agent ON learning_spend (agent_id)";
145
+ export declare const LEARNING_SPEND_OF_RUN = "CREATE INDEX IF NOT EXISTS learning_spend_of_run ON learning_spend (of_run) WHERE of_run IS NOT NULL";
106
146
  /** Version 13 → 14 on SQLite (the server's open, sqlite/store.ts), given the runs' and the agents' columns now: when each
107
147
  * run and each agent was purged (null: none was), and the version, in one durable write. */
108
148
  export declare function purgeUpgrade(runColumns: ReadonlySet<string>, agentColumns: ReadonlySet<string>): readonly string[];
@@ -21,7 +21,8 @@
21
21
  // added the same way, and so is each runner's word that it is there and whether it refused to claim (runners, RUNNERS),
22
22
  // and each computer a run made, from its create to its proven delete (computer_intervals, COMPUTER_INTERVALS). Each runner's
23
23
  // version and slots (runners.version, .slots: RUNNERS_ADDED), each hand-back (hand_backs, HAND_BACKS) and each claim of a
24
- // run with the turn it works and its attempt at it (run_attempts, RUN_ATTEMPTS) are added the same way.
24
+ // run with the turn it works and its attempt at it (run_attempts, RUN_ATTEMPTS) are added the same way, and so is the
25
+ // learning ledger (learning_ledger, learning_spend: LEARNING_LEDGER, LEARNING_SPEND).
25
26
  // Conventions: every time is INTEGER epoch milliseconds; JSON is TEXT checked by json_valid; money is REAL US dollars;
26
27
  // sealed columns hold ciphertext only (sealed with the server's key outside the store), so the store never sees a secret
27
28
  // in the clear. Foreign keys are on (the connection's pragma).
@@ -38,9 +39,11 @@
38
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
39
40
  // an agent was purged (runs.purged_at, agents.purged_at; DELETE /runs/{id} and /agents/{id}, agent-delete.ts): its row is
40
41
  // then a tombstone, its content gone and its cost kept. Version 15 keeps the disk a run's launch named as its own
41
- // (runs.disk; a run on one learns nothing, learn/learning-of.ts). The server upgrades these in place at its open
42
- // (listingUpgrade from 11, relayUpgrade from 12, purgeUpgrade from 13, then diskUpgrade from 14); a runner waits for it.
43
- 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;
44
47
  /** One answer per approval of a run: a second approve or deny command naming the same approval is refused
45
48
  * (StoreConstraint), so the first answer a person gives wins from the moment it is written, before the runner takes it
46
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. */
@@ -84,9 +87,20 @@ export const TRIGGERS_TEMPLATE_ID = "ALTER TABLE triggers ADD COLUMN template_id
84
87
  /** A run's census (RunLaunchRow.census, run-census.ts): in STORE_DDL, and added at a server open to a store an earlier
85
88
  * build of this version made, as RUNS_LABELS is (a run ended before reads as never handed a secret). */
86
89
  export const RUN_LAUNCH_CENSUS = `ALTER TABLE run_launch ADD COLUMN ${json("census", true)}`;
87
- /** Every launch holding sealed bytes when the census was added (RUN_LAUNCH_CENSUS) marked CENSUS_PENDING (store.ts): its
88
- * end counts them, or its files read as uncounted. */
89
- 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'`;
90
104
  /** A template package imported from GitHub (POST /v2/templates/import), by its id (`github:<owner>/<repo>[/<path>]`): its
91
105
  * source as given (ref and all), the full commit SHA read, the GitHub account that publishes it, and its files as read
92
106
  * (`files`, a JSON object of path to base64). A re-import of the id replaces the row. The server checks the files again
@@ -223,6 +237,85 @@ export const RUN_ATTEMPTS = `CREATE TABLE IF NOT EXISTS run_attempts (
223
237
  claimed_at INTEGER NOT NULL,
224
238
  PRIMARY KEY (run_id, epoch)
225
239
  ) STRICT`;
240
+ /** The learning ledger (learn/ledger.ts): one row a run, written by each patch that leaves the run ended (applyRunPatch,
241
+ * in that patch's transaction) and by a healed cost: who answered it (`path`, null when its end named none), the
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
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). */
248
+ export const LEARNING_LEDGER = `CREATE TABLE IF NOT EXISTS learning_ledger (
249
+ run_id TEXT PRIMARY KEY,
250
+ agent_id TEXT NOT NULL,
251
+ started_at INTEGER NOT NULL,
252
+ path TEXT CHECK (path IS NULL OR path IN ('agent', 'handed_back', 'workflow', 'replay', 'audit')),
253
+ workflow TEXT,
254
+ digest TEXT,
255
+ not_served TEXT CHECK (not_served IS NULL OR not_served IN ('cost_stop', 'cut')),
256
+ job_usd REAL,
257
+ look_back_usd REAL,
258
+ cost TEXT NOT NULL CHECK (cost IN ('known', 'unknown', 'at_most')),
259
+ workflow_calls TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(workflow_calls))
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
+ };
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}`;
301
+ /** Each learning cost, by the revision it is for (learn/ledger.ts SpendRow): a compile turn (`round`, the round that asked
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).
303
+ * Written with its run's ledger row, replaced whole each time; gone with its run's purge or delete, and with the purge
304
+ * or delete of the run it audits. */
305
+ export const LEARNING_SPEND = `CREATE TABLE IF NOT EXISTS learning_spend (
306
+ run_id TEXT NOT NULL,
307
+ kind TEXT NOT NULL CHECK (kind IN ('compile', 'check', 'audit')),
308
+ round INTEGER NOT NULL CHECK (round >= 0),
309
+ agent_id TEXT NOT NULL,
310
+ of_run TEXT,
311
+ workflow TEXT,
312
+ digest TEXT,
313
+ usd REAL,
314
+ at INTEGER NOT NULL,
315
+ PRIMARY KEY (run_id, kind, round)
316
+ ) STRICT`;
317
+ export const LEARNING_SPEND_AGENT = "CREATE INDEX IF NOT EXISTS learning_spend_agent ON learning_spend (agent_id)";
318
+ export const LEARNING_SPEND_OF_RUN = "CREATE INDEX IF NOT EXISTS learning_spend_of_run ON learning_spend (of_run) WHERE of_run IS NOT NULL";
226
319
  /** Version 13 → 14 on SQLite (the server's open, sqlite/store.ts), given the runs' and the agents' columns now: when each
227
320
  * run and each agent was purged (null: none was), and the version, in one durable write. */
228
321
  export function purgeUpgrade(runColumns, agentColumns) {
@@ -333,7 +426,8 @@ export const STORE_DDL = [
333
426
  `CREATE TABLE IF NOT EXISTS store_meta (
334
427
  singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
335
428
  schema_version INTEGER NOT NULL,
336
- flushes INTEGER NOT NULL DEFAULT 0
429
+ flushes INTEGER NOT NULL DEFAULT 0,
430
+ ${LEDGER_BACKFILLED}
337
431
  ) STRICT`,
338
432
  // Agents and their revisions. A revision made from a template keeps its package: {name, version (SemVer), source
339
433
  // builtin|git, ref, sha, parameters}.
@@ -682,6 +776,12 @@ export const STORE_DDL = [
682
776
  COMPUTER_INTERVALS,
683
777
  COMPUTER_INTERVALS_RUN,
684
778
  RUN_ATTEMPTS,
779
+ LEARNING_LEDGER,
780
+ LEARNING_LEDGER_AGENT,
781
+ LEARNING_LEDGER_BY_HAND,
782
+ LEARNING_SPEND,
783
+ LEARNING_SPEND_AGENT,
784
+ LEARNING_SPEND_OF_RUN,
685
785
  // An agent's saved browser sign-ins (design 2.1, the browser sign-in rules): one live row per agent and browser provider, its
686
786
  // pointer (the vendor's context or profile id) sealed with the store's one rule (store/seal.ts, AAD
687
787
  // sign_ins|<agent>:<provider>|pointer), which the store never opens. The owner sees its name ("default") and saved_at,
@@ -756,9 +856,9 @@ export const STORE_DDL = [
756
856
  // written in the same transaction as the queued run, never after. `body` is the launch JSON (run-launch.ts: the first
757
857
  // input, title, place, effort, cap, and the trigger and origin that started it); `sealed` holds any secret sealed with
758
858
  // the server's key outside the store (AAD run_launch|<run_id>|sealed), or null when the launch holds none. `census` is
759
- // null when the launch never held any; "pending" from when it first does until an end counts them; then where in the
760
- // run's files they sat when that end erased them (run-census.ts), never the secrets, or "unscanned" when it erased them
761
- // 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).
762
862
  `CREATE TABLE IF NOT EXISTS run_launch (
763
863
  run_id TEXT PRIMARY KEY REFERENCES runs (id),
764
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";
@@ -45,7 +51,18 @@ export declare const runOf: (r: RunRecord) => RunRow;
45
51
  * `now` (the store's clock, read in the transaction). A final state with no `endedAt` is stamped `now`, and releases the
46
52
  * run in the same statement. `dialect`: the connection's (store/dialect.ts), SQLite's by default. `clock`: the deadlines'
47
53
  * clock (fence.ts storeNow) the fence and left_running_at read; `now` bound by default. */
48
- export declare function applyRunPatch(tx: Pick<SqliteExecutor, "run" | "get">, claim: Pick<Claim, "runId" | "epoch">, patch: RunPatch, also: readonly AlsoRow[], now: number, dialect?: Dialect, clock?: StoreNow): Promise<RunRow>;
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>;
55
+ /** Writes run `run`'s ledger row and spend rows (learn/ledger.ts record) through `tx`, from its row and every round its
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>;
49
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
50
67
  * with no runner (released: a runner claims it at once), its end's own fields cleared, its epoch, journal, cost and
51
68
  * metrics kept. `where` holds only for an agentic run that ended completed or that a person stopped (state 'stopped',
@@ -6,10 +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";
9
+ import { readableJson, readLaunch } from "./run-launch.js";
10
+ import { auditOfLaunch, learnsOfLaunch, record } from "../../learn/ledger.js";
10
11
  /** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
11
12
  * list of runs names. */
12
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 ?`;
13
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
14
25
  * the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
15
26
  export const SIGN_IN_HANDED_LOCK = "sign_in_handed";
@@ -101,7 +112,61 @@ export async function applyRunPatch(tx, claim, patch, also, now, dialect = SQLIT
101
112
  break;
102
113
  }
103
114
  }
104
- return runOf(row);
115
+ const run = runOf(row);
116
+ // A run this patch leaves ended has its ledger row and spend rows (learn/ledger.ts record) in this same commit, over
117
+ // every round its learning kept, this patch's included: an end, a later end after a follow-up and a patch of an ended
118
+ // run's metrics each write them whole again.
119
+ if (FINAL_STATES.includes(run.state))
120
+ await recordLedger(tx, run, now);
121
+ return run;
122
+ }
123
+ /** Writes run `run`'s ledger row and spend rows (learn/ledger.ts record) through `tx`, from its row and every round its
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). */
126
+ export async function recordLedger(tx, run, now) {
127
+ const rounds = (await tx.all("SELECT round, proposal FROM learning_proposals WHERE run_id = ? ORDER BY round", run.id))
128
+ .map((one) => ({ round: Number(one.round), proposal: JSON.parse(one.proposal) }));
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
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,
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));
137
+ await tx.run("DELETE FROM learning_spend WHERE run_id = ?", run.id);
138
+ for (const one of spend) {
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);
140
+ }
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));
105
170
  }
106
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
107
172
  * with no runner (released: a runner claims it at once), its end's own fields cleared, its epoch, journal, cost and
@@ -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. */