@parcha/agentrun-serve 0.1.0-beta.51 → 0.1.0-beta.53

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 (322) hide show
  1. package/CHANGELOG.md +160 -1
  2. package/README.md +306 -36
  3. package/dist/adapters.js +1 -1
  4. package/dist/agent-delete.d.ts +14 -4
  5. package/dist/agent-delete.js +30 -58
  6. package/dist/agent-package.d.ts +71 -0
  7. package/dist/agent-package.js +345 -0
  8. package/dist/agents-store.d.ts +28 -18
  9. package/dist/agents-store.js +48 -34
  10. package/dist/api-only.d.ts +30 -8
  11. package/dist/api-only.js +40 -16
  12. package/dist/archil-routes.d.ts +1 -0
  13. package/dist/archil-routes.js +28 -2
  14. package/dist/claude-login.d.ts +1 -0
  15. package/dist/claude-login.js +1 -1
  16. package/dist/cli-learning/cli.d.ts +3 -0
  17. package/dist/cli-learning/cli.js +17 -3
  18. package/dist/cli-learning/connections.js +3 -2
  19. package/dist/cli-learning/schema.js +2 -1
  20. package/dist/cli.js +70 -7
  21. package/dist/composio-preset.d.ts +2 -1
  22. package/dist/composio-preset.js +4 -3
  23. package/dist/composio-triggers.d.ts +4 -0
  24. package/dist/composio-triggers.js +4 -1
  25. package/dist/computer-needs.d.ts +32 -0
  26. package/dist/computer-needs.js +80 -0
  27. package/dist/computer-setting.d.ts +9 -0
  28. package/dist/computer-setting.js +19 -1
  29. package/dist/computers.d.ts +6 -2
  30. package/dist/computers.js +2 -2
  31. package/dist/console/assets/index-BFgyZzF_.js +734 -0
  32. package/dist/console/assets/index-DbilxljH.css +1 -0
  33. package/dist/console/index.html +2 -2
  34. package/dist/console-links.d.ts +1 -0
  35. package/dist/console-links.js +2 -0
  36. package/dist/env-switch.d.ts +14 -0
  37. package/dist/env-switch.js +23 -0
  38. package/dist/errors.d.ts +1 -0
  39. package/dist/errors.js +3 -0
  40. package/dist/eval-previous.js +4 -3
  41. package/dist/fal-price.d.ts +76 -0
  42. package/dist/fal-price.js +365 -0
  43. package/dist/handed-secret.d.ts +4 -0
  44. package/dist/handed-secret.js +10 -0
  45. package/dist/harness-archive.d.ts +15 -5
  46. package/dist/harness-archive.js +71 -34
  47. package/dist/harness-check.d.ts +2 -1
  48. package/dist/harness-check.js +36 -95
  49. package/dist/harness-egress.d.ts +23 -0
  50. package/dist/harness-egress.js +69 -0
  51. package/dist/harness-recipe-entry.d.ts +71 -0
  52. package/dist/harness-recipe-entry.js +184 -0
  53. package/dist/harness-recipe.d.ts +73 -6
  54. package/dist/harness-recipe.js +185 -31
  55. package/dist/harness-sign-in.d.ts +29 -0
  56. package/dist/harness-sign-in.js +122 -0
  57. package/dist/harness-table.d.ts +21 -0
  58. package/dist/harness-table.js +58 -3
  59. package/dist/harness-zip.d.ts +6 -0
  60. package/dist/harness-zip.js +203 -0
  61. package/dist/ids.d.ts +11 -0
  62. package/dist/ids.js +36 -0
  63. package/dist/learn/commit.d.ts +19 -1
  64. package/dist/learn/commit.js +160 -2
  65. package/dist/learn/compile.d.ts +13 -5
  66. package/dist/learn/compile.js +46 -15
  67. package/dist/learn/delete.d.ts +12 -6
  68. package/dist/learn/delete.js +17 -11
  69. package/dist/learn/folder.d.ts +16 -0
  70. package/dist/learn/folder.js +30 -4
  71. package/dist/learn/learning-of.d.ts +8 -0
  72. package/dist/learn/learning-of.js +8 -0
  73. package/dist/learn/learning.sh +29 -4
  74. package/dist/learn/promote.d.ts +51 -7
  75. package/dist/learn/promote.js +51 -8
  76. package/dist/learn/secrets.js +30 -7
  77. package/dist/learning-reset.d.ts +18 -0
  78. package/dist/learning-reset.js +115 -0
  79. package/dist/learning-routes.d.ts +23 -11
  80. package/dist/learning-routes.js +110 -20
  81. package/dist/learning-words.d.ts +5 -0
  82. package/dist/learning-words.js +5 -0
  83. package/dist/learning.d.ts +15 -10
  84. package/dist/learning.js +14 -8
  85. package/dist/llms.txt +1 -0
  86. package/dist/mcp-gateway.d.ts +39 -2
  87. package/dist/mcp-gateway.js +56 -12
  88. package/dist/mcp-pin-first.js +1 -1
  89. package/dist/mcp-probe.d.ts +3 -0
  90. package/dist/mcp-probe.js +7 -2
  91. package/dist/mcp-resources.d.ts +44 -0
  92. package/dist/mcp-resources.js +100 -0
  93. package/dist/mcp-servers-store.d.ts +17 -12
  94. package/dist/mcp-servers-store.js +2 -1
  95. package/dist/media-duration.d.ts +2 -0
  96. package/dist/media-duration.js +136 -0
  97. package/dist/model.d.ts +4 -4
  98. package/dist/model.js +6 -6
  99. package/dist/native/cli.js +9 -2
  100. package/dist/native/connection.d.ts +2 -2
  101. package/dist/native/connection.js +5 -2
  102. package/dist/native/discovery.d.ts +2 -0
  103. package/dist/native/discovery.js +10 -6
  104. package/dist/native/remote-worker-bridge.js +36 -4
  105. package/dist/native/remote.d.ts +18 -1
  106. package/dist/native/remote.js +126 -37
  107. package/dist/native/schema.d.ts +6 -0
  108. package/dist/native/schema.js +31 -6
  109. package/dist/native/source-account.d.ts +13 -0
  110. package/dist/native/source-account.js +114 -0
  111. package/dist/native/source-capture-hooks.d.ts +5 -0
  112. package/dist/native/source-capture-hooks.js +102 -0
  113. package/dist/native/source-mcp-selection.d.ts +13 -0
  114. package/dist/native/source-mcp-selection.js +72 -0
  115. package/dist/native/source-runtime-tools.d.ts +19 -0
  116. package/dist/native/source-runtime-tools.js +103 -0
  117. package/dist/native/source-snapshot-types.d.ts +138 -0
  118. package/dist/native/source-snapshot-types.js +12 -0
  119. package/dist/native/source-snapshot.d.ts +8 -0
  120. package/dist/native/source-snapshot.js +1201 -0
  121. package/dist/observation/learning.js +3 -2
  122. package/dist/openapi.js +211 -47
  123. package/dist/print.js +1 -1
  124. package/dist/provider.d.ts +8 -0
  125. package/dist/provider.js +14 -2
  126. package/dist/purged-workflow.d.ts +12 -0
  127. package/dist/purged-workflow.js +43 -0
  128. package/dist/refusal-words.d.ts +0 -3
  129. package/dist/refusal-words.js +0 -3
  130. package/dist/request-metadata.d.ts +20 -0
  131. package/dist/request-metadata.js +24 -0
  132. package/dist/run-keys.d.ts +46 -0
  133. package/dist/run-keys.js +109 -0
  134. package/dist/run-launch.d.ts +29 -5
  135. package/dist/run-launch.js +44 -4
  136. package/dist/run-learning-served.d.ts +2 -2
  137. package/dist/run-model-key.d.ts +2 -2
  138. package/dist/run-model-key.js +5 -5
  139. package/dist/run-secrets.d.ts +2 -0
  140. package/dist/run-secrets.js +7 -4
  141. package/dist/run-usage.d.ts +48 -4
  142. package/dist/run-usage.js +156 -39
  143. package/dist/run-view.d.ts +18 -1
  144. package/dist/run-view.js +28 -8
  145. package/dist/runner/adapter-loop.d.ts +14 -4
  146. package/dist/runner/adapter-loop.js +150 -31
  147. package/dist/runner/adapter-run-spec.d.ts +1 -1
  148. package/dist/runner/adapter-run-spec.js +35 -22
  149. package/dist/runner/audit.d.ts +10 -3
  150. package/dist/runner/audit.js +30 -7
  151. package/dist/runner/beside.js +2 -1
  152. package/dist/runner/claim-secrets.d.ts +31 -4
  153. package/dist/runner/claim-secrets.js +43 -7
  154. package/dist/runner/cli-run-spec.d.ts +1 -1
  155. package/dist/runner/cli-run-spec.js +32 -20
  156. package/dist/runner/conformance/harness.js +4 -2
  157. package/dist/runner/dispatch.js +4 -2
  158. package/dist/runner/harness-loops.d.ts +6 -0
  159. package/dist/runner/harness-loops.js +16 -3
  160. package/dist/runner/held.d.ts +4 -0
  161. package/dist/runner/journal-kinds.d.ts +5 -0
  162. package/dist/runner/learned-first.d.ts +2 -0
  163. package/dist/runner/learned-first.js +1 -1
  164. package/dist/runner/learning-end.d.ts +9 -3
  165. package/dist/runner/learning-end.js +66 -29
  166. package/dist/runner/learning-seed.d.ts +9 -3
  167. package/dist/runner/learning-seed.js +2 -2
  168. package/dist/runner/main.d.ts +1 -0
  169. package/dist/runner/main.js +20 -13
  170. package/dist/runner/mcp-claim.js +7 -2
  171. package/dist/runner/mcp-run.d.ts +12 -0
  172. package/dist/runner/mcp-run.js +14 -1
  173. package/dist/runner/model-route.js +7 -4
  174. package/dist/runner/monid-run.js +9 -11
  175. package/dist/runner/project-checkpoint.d.ts +35 -0
  176. package/dist/runner/project-checkpoint.js +116 -0
  177. package/dist/runner/project-drain.d.ts +8 -0
  178. package/dist/runner/project-drain.js +25 -0
  179. package/dist/runner/project-host.d.ts +10 -0
  180. package/dist/runner/project-host.js +52 -0
  181. package/dist/runner/promotion.d.ts +51 -24
  182. package/dist/runner/promotion.js +206 -78
  183. package/dist/runner/redact.d.ts +13 -0
  184. package/dist/runner/redact.js +67 -1
  185. package/dist/runner/run-channel.d.ts +9 -0
  186. package/dist/runner/run-channel.js +15 -2
  187. package/dist/runner/run-child-host.d.ts +8 -4
  188. package/dist/runner/run-child-host.js +174 -42
  189. package/dist/runner/run-errors.js +2 -2
  190. package/dist/runner/runner-check.js +5 -2
  191. package/dist/runner/runner.js +41 -12
  192. package/dist/runner/sdk-loop.d.ts +2 -2
  193. package/dist/runner/sdk-loop.js +3 -3
  194. package/dist/runner/store-port.d.ts +3 -0
  195. package/dist/runner/store-secrets.js +7 -3
  196. package/dist/runner/workflow-first.d.ts +4 -0
  197. package/dist/runner/workflow-first.js +13 -0
  198. package/dist/runner/workflow-loop.d.ts +2 -1
  199. package/dist/runner/workflow-loop.js +2 -1
  200. package/dist/runner/workflow-spec.js +4 -4
  201. package/dist/runs-over-store.d.ts +39 -8
  202. package/dist/runs-over-store.js +147 -32
  203. package/dist/sdk/check.d.ts +6 -5
  204. package/dist/sdk/check.js +17 -16
  205. package/dist/sdk/runs.d.ts +11 -1
  206. package/dist/sdk/runs.js +185 -78
  207. package/dist/sdk/schema.js +12 -2
  208. package/dist/sdk/workflows.d.ts +1 -1
  209. package/dist/server.d.ts +68 -32
  210. package/dist/server.js +678 -124
  211. package/dist/skills/agentrun-serve/SKILL.md +2 -1
  212. package/dist/source-sync/adoption.d.ts +15 -0
  213. package/dist/source-sync/adoption.js +36 -0
  214. package/dist/source-sync/auxiliary.d.ts +23 -0
  215. package/dist/source-sync/auxiliary.js +64 -0
  216. package/dist/source-sync/capture-git.d.ts +5 -0
  217. package/dist/source-sync/capture-git.js +272 -0
  218. package/dist/source-sync/capture-project.d.ts +53 -0
  219. package/dist/source-sync/capture-project.js +206 -0
  220. package/dist/source-sync/capture-refusal.d.ts +7 -0
  221. package/dist/source-sync/capture-refusal.js +16 -0
  222. package/dist/source-sync/capture.d.ts +11 -0
  223. package/dist/source-sync/capture.js +22 -0
  224. package/dist/source-sync/checkpoint-gate.d.ts +21 -0
  225. package/dist/source-sync/checkpoint-gate.js +52 -0
  226. package/dist/source-sync/checkpoints.d.ts +66 -0
  227. package/dist/source-sync/checkpoints.js +168 -0
  228. package/dist/source-sync/claim.d.ts +11 -0
  229. package/dist/source-sync/claim.js +48 -0
  230. package/dist/source-sync/destination.d.ts +47 -0
  231. package/dist/source-sync/destination.js +239 -0
  232. package/dist/source-sync/io.d.ts +15 -0
  233. package/dist/source-sync/io.js +52 -0
  234. package/dist/source-sync/local.d.ts +43 -0
  235. package/dist/source-sync/local.js +229 -0
  236. package/dist/source-sync/prepare.d.ts +59 -0
  237. package/dist/source-sync/prepare.js +93 -0
  238. package/dist/source-sync/projects.d.ts +61 -0
  239. package/dist/source-sync/projects.js +130 -0
  240. package/dist/source-sync/registry.d.ts +95 -0
  241. package/dist/source-sync/registry.js +226 -0
  242. package/dist/source-sync/service.d.ts +33 -0
  243. package/dist/source-sync/service.js +118 -0
  244. package/dist/source-sync/skill-edits.d.ts +12 -0
  245. package/dist/source-sync/skill-edits.js +122 -0
  246. package/dist/source-sync/snapshots.d.ts +48 -0
  247. package/dist/source-sync/snapshots.js +448 -0
  248. package/dist/source-sync/types.d.ts +9 -0
  249. package/dist/source-sync/types.js +14 -0
  250. package/dist/source-sync/workspace.d.ts +66 -0
  251. package/dist/source-sync/workspace.js +273 -0
  252. package/dist/store/agent-body.d.ts +8 -0
  253. package/dist/store/agent-body.js +20 -6
  254. package/dist/store/index.d.ts +2 -2
  255. package/dist/store/index.js +1 -1
  256. package/dist/store/journal/storage.d.ts +2 -0
  257. package/dist/store/journal/storage.js +2 -1
  258. package/dist/store/postgres/migrations/0025_run_attempts.sql +27 -0
  259. package/dist/store/postgres/migrations/0026_run_disk.sql +23 -0
  260. package/dist/store/schema.d.ts +15 -1
  261. package/dist/store/schema.js +43 -4
  262. package/dist/store/seal.d.ts +5 -1
  263. package/dist/store/seal.js +12 -3
  264. package/dist/store/sqlite/project-checkpoints.d.ts +11 -0
  265. package/dist/store/sqlite/project-checkpoints.js +70 -0
  266. package/dist/store/sqlite/run-launch.d.ts +14 -0
  267. package/dist/store/sqlite/run-launch.js +58 -7
  268. package/dist/store/sqlite/run-patch.d.ts +2 -1
  269. package/dist/store/sqlite/run-patch.js +4 -3
  270. package/dist/store/sqlite/store.js +319 -153
  271. package/dist/store/store-schema.js +2 -2
  272. package/dist/store/store.d.ts +173 -33
  273. package/dist/store/store.js +14 -1
  274. package/dist/store-reads/learning.d.ts +12 -8
  275. package/dist/store-reads/learning.js +33 -24
  276. package/dist/store-reads/loop.d.ts +4 -3
  277. package/dist/store-reads/loop.js +7 -5
  278. package/dist/store-view.d.ts +3 -1
  279. package/dist/store-view.js +17 -5
  280. package/dist/template-eval.js +4 -4
  281. package/dist/template-import.d.ts +10 -2
  282. package/dist/template-import.js +116 -13
  283. package/dist/templates.d.ts +29 -5
  284. package/dist/templates.js +68 -14
  285. package/dist/timeline/index.d.ts +1 -1
  286. package/dist/timeline/index.js +1 -1
  287. package/dist/timeline/labels.js +1 -1
  288. package/dist/timeline/timeline.d.ts +5 -0
  289. package/dist/timeline/timeline.js +8 -0
  290. package/dist/tool-plane.d.ts +12 -12
  291. package/dist/tools.d.ts +8 -8
  292. package/dist/tools.js +1 -1
  293. package/dist/trigger-launch-hook.d.ts +47 -0
  294. package/dist/trigger-launch-hook.js +90 -0
  295. package/dist/trigger-targets.d.ts +14 -1
  296. package/dist/trigger-targets.js +20 -0
  297. package/dist/triggers.d.ts +54 -5
  298. package/dist/triggers.js +143 -8
  299. package/dist/uploads-archil.js +2 -1
  300. package/dist/uploads.d.ts +0 -3
  301. package/dist/uploads.js +2 -11
  302. package/dist/validate.js +4 -1
  303. package/dist/web-save.d.ts +3 -0
  304. package/dist/web-save.js +17 -2
  305. package/dist/workspace/archil-exec.js +1 -1
  306. package/dist/workspace/archil.js +1 -1
  307. package/dist/workspace/computer-for-claim.d.ts +2 -0
  308. package/dist/workspace/computer-for-claim.js +43 -0
  309. package/dist/workspace/paths.d.ts +6 -0
  310. package/dist/workspace/paths.js +6 -0
  311. package/dist/workspace/resolve-computer.d.ts +1 -0
  312. package/dist/workspace/revision-skills.js +24 -11
  313. package/dist/workspace/run-tools.d.ts +2 -0
  314. package/dist/workspace/run-tools.js +8 -0
  315. package/dist/workspace/synced-folder.d.ts +3 -3
  316. package/dist/workspace/synced-folder.js +7 -6
  317. package/launcher/agentrun-serve +1 -1
  318. package/package.json +10 -10
  319. package/skills/agentrun-serve/SKILL.md +2 -1
  320. package/templates/README.md +5 -2
  321. package/dist/console/assets/index-Bt1B51b7.js +0 -733
  322. package/dist/console/assets/index-sVftE9Ht.css +0 -1
@@ -73,10 +73,14 @@ export type RunModelKey = {
73
73
  readonly key: string;
74
74
  readonly origin: string;
75
75
  };
76
- /** What a run's launch holds sealed: its handed MCP credentials and its own model key, each when it has one. */
76
+ /** A run's own tool keys (`keys` at its start or with a message, run-keys.ts), by name. */
77
+ export type RunToolKeys = Readonly<Record<string, string>>;
78
+ /** What a run's launch holds sealed: its handed MCP credentials, its own model key and its own tool keys, each when it
79
+ * has one. */
77
80
  export type RunLaunchSecrets = {
78
81
  readonly mcp?: RunMcpAuth;
79
82
  readonly model?: RunModelKey;
83
+ readonly keys?: RunToolKeys;
80
84
  };
81
85
  /** Seals a run's launch secrets as its launch's sealed column (AAD run_launch|<run_id>|sealed); null when it holds none. */
82
86
  export declare function sealRunLaunch(key: Buffer, runId: string, secrets: RunLaunchSecrets): Uint8Array | null;
@@ -73,10 +73,18 @@ export function openMcpCredential(key, row) {
73
73
  /** Seals a run's launch secrets as its launch's sealed column (AAD run_launch|<run_id>|sealed); null when it holds none. */
74
74
  export function sealRunLaunch(key, runId, secrets) {
75
75
  const mcp = secrets.mcp && Object.keys(secrets.mcp).length ? secrets.mcp : undefined;
76
- if (!mcp && !secrets.model)
76
+ const keys = secrets.keys && Object.keys(secrets.keys).length ? secrets.keys : undefined;
77
+ if (!mcp && !secrets.model && !keys)
77
78
  return null;
78
- return sealColumn(key, sealedAt.runLaunch(runId), JSON.stringify({ ...(mcp ? { mcp } : {}), ...(secrets.model ? { model: secrets.model } : {}) }));
79
+ return sealColumn(key, sealedAt.runLaunch(runId), JSON.stringify({ ...(mcp ? { mcp } : {}), ...(secrets.model ? { model: secrets.model } : {}), ...(keys ? { keys } : {}) }));
79
80
  }
81
+ /** The tool keys of an opened launch: each name with text for its value, none else. */
82
+ const toolKeysOf = (keys) => {
83
+ if (typeof keys !== "object" || keys === null || Array.isArray(keys))
84
+ return undefined;
85
+ const out = Object.fromEntries(Object.entries(keys).filter((entry) => typeof entry[1] === "string"));
86
+ return Object.keys(out).length ? out : undefined;
87
+ };
80
88
  /** A run's launch secrets, opened: none when its launch holds none; undefined when the bytes do not open under `key` for
81
89
  * this run (sealed for another run, or under another key). */
82
90
  export function openRunLaunch(key, runId, sealed) {
@@ -85,7 +93,8 @@ export function openRunLaunch(key, runId, sealed) {
85
93
  try {
86
94
  const opened = JSON.parse(unsealColumn(key, sealedAt.runLaunch(runId), sealed).toString("utf8"));
87
95
  const model = typeof opened.model?.key === "string" && typeof opened.model.origin === "string" ? { key: opened.model.key, origin: opened.model.origin } : undefined;
88
- return { ...(opened.mcp ? { mcp: opened.mcp } : {}), ...(model ? { model } : {}) };
96
+ const keys = toolKeysOf(opened.keys);
97
+ return { ...(opened.mcp ? { mcp: opened.mcp } : {}), ...(model ? { model } : {}), ...(keys ? { keys } : {}) };
89
98
  }
90
99
  catch {
91
100
  return undefined;
@@ -0,0 +1,11 @@
1
+ import type { Dialect } from "../dialect.js";
2
+ import type { Runs } from "../store.js";
3
+ import type { SqliteExecutor } from "./connection.js";
4
+ /** Uses the ordinary store connection. As with usageMetadata, PostgreSQL's precise JSON Unicode refusal permits a
5
+ * read/parse compatibility fallback; generic DB errors propagate. Not called inside a Store.snapshot transaction. */
6
+ export declare function readProjectCheckpoints(executor: Pick<SqliteExecutor, "all"> & {
7
+ readonly dialect: Dialect;
8
+ }, filter: {
9
+ agentId: string;
10
+ runId?: string;
11
+ }): ReturnType<Runs["projectCheckpoints"]>;
@@ -0,0 +1,70 @@
1
+ import { PI_TABLE } from "../schema.js";
2
+ const fields = ["version", "status", "sourceId", "runId", "epoch", "baselineRevision", "baselineDigest", "revision", "digest", "reason", "stage"];
3
+ const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
4
+ const pick = (value) => Object.fromEntries(fields.flatMap(field => object(value)[field] === undefined ? [] : [[field, object(value)[field]]]));
5
+ const terminal = (state) => state === "ended" || state === "failed" || state === "stopped";
6
+ /** Uses the ordinary store connection. As with usageMetadata, PostgreSQL's precise JSON Unicode refusal permits a
7
+ * read/parse compatibility fallback; generic DB errors propagate. Not called inside a Store.snapshot transaction. */
8
+ export async function readProjectCheckpoints(executor, filter) {
9
+ const d = executor.dialect;
10
+ const where = `r.agent_id = ? AND r.purged_at IS NULL${filter.runId === undefined ? "" : " AND r.id = ?"}`;
11
+ const from = `FROM ${PI_TABLE.entries} e JOIN runs r ON r.id = e.run_id LEFT JOIN run_launch l ON l.run_id = r.id WHERE ${where} AND e.conversation_id = 1`;
12
+ const params = [filter.agentId, ...(filter.runId === undefined ? [] : [filter.runId])];
13
+ const columns = fields.map((field, index) => `${d.jsonValue("e.record", ["data", "projectCheckpoint", field])} AS field_${index}`);
14
+ // Binding is structural here; the recovery domain additionally checks the revision's source identity/digest.
15
+ // Read individual members, never input, output or sealed launch data on the ordinary projection path.
16
+ const placed = `${d.jsonValue("l.body", ["source_project", "version"])} = '1' AND ${d.jsonText("l.body", ["source_project", "baselineRevision"])} = r.revision_id`;
17
+ const end = (alias) => `${d.jsonText(`${alias}.record`, ["kind"])} = 'agentrun.end' AND ${d.jsonText(`${alias}.record`, ["data", "state"])} IN ('ended', 'failed', 'stopped')`;
18
+ const latest = `LEFT JOIN run_attempts a ON a.run_id = r.id AND a.epoch = (SELECT max(prior.epoch) FROM run_attempts prior WHERE prior.run_id = r.id)`;
19
+ const owner = `(SELECT max(own.epoch) FROM run_attempts own WHERE own.run_id = r.id AND own.journal_from <= e.id)`;
20
+ try {
21
+ // One statement observes end rows and missing-end attempts consistently: a terminal commit between two reads
22
+ // must not disappear from both results. Real entries precede the latest attempt's synthetic recovery status.
23
+ const rows = await executor.all(`SELECT r.id AS run_id, r.revision_id, e.id AS entry_id, ${owner} AS attempt_epoch, 0 AS missing_order, ${columns.join(", ")} ${from}
24
+ AND ${end("e")} AND (${d.jsonValue("e.record", ["data", "projectCheckpoint"])} IS NOT NULL OR (${placed}))
25
+ UNION ALL
26
+ SELECT r.id AS run_id, r.revision_id, NULL AS entry_id, a.epoch AS attempt_epoch, 1 AS missing_order, ${fields.map((_, index) => `NULL AS field_${index}`).join(", ")}
27
+ FROM runs r JOIN run_launch l ON l.run_id = r.id ${latest}
28
+ WHERE ${where} AND r.state IN ('ended', 'failed', 'stopped') AND a.epoch IS NOT NULL AND (${placed})
29
+ AND NOT EXISTS (SELECT 1 FROM ${PI_TABLE.entries} ended WHERE ended.run_id = r.id AND ended.conversation_id = 1 AND ended.id >= a.journal_from AND ${end("ended")})
30
+ ORDER BY run_id, missing_order, entry_id`, ...params, ...params);
31
+ return rows.map(row => ({ runId: String(row.run_id), revisionId: String(row.revision_id), entryId: row.entry_id === null ? null : Number(row.entry_id),
32
+ ...(row.attempt_epoch === null ? {} : { attemptEpoch: Number(row.attempt_epoch) }), checkpoint: Object.fromEntries(fields.flatMap((field, index) => {
33
+ const value = row[`field_${index}`];
34
+ return value == null ? [] : [[field, JSON.parse(String(value))]];
35
+ })) }));
36
+ }
37
+ catch (error) {
38
+ let unicode = false;
39
+ for (let cause = error, depth = 0; cause && depth < 6; cause = cause.cause, depth++)
40
+ if (cause.code === "22P05" || (cause.code === "22P02" && cause.reason === "json_surrogate"))
41
+ unicode = true;
42
+ if (d.name !== "postgres" || !unicode)
43
+ throw error;
44
+ // Exceptional old text may contain a NUL/surrogate in an unrelated answer or launch input. PostgreSQL cannot
45
+ // extract any member then; preserve JS semantics without reading sealed bytes or dropping recovery evidence.
46
+ const rows = await executor.all(`SELECT r.id AS run_id, r.revision_id, r.state, l.body, e.id AS entry_id, e.record, a.epoch AS attempt_epoch, ${owner} AS entry_attempt_epoch, a.journal_from
47
+ FROM runs r LEFT JOIN run_launch l ON l.run_id = r.id ${latest}
48
+ LEFT JOIN ${PI_TABLE.entries} e ON e.run_id = r.id AND e.conversation_id = 1 WHERE ${where} ORDER BY r.id, e.id`, ...params);
49
+ const results = [];
50
+ const missing = new Map();
51
+ const ended = new Set();
52
+ for (const row of rows) {
53
+ const launch = object(row.body === null ? undefined : JSON.parse(row.body)), source = object(launch.source_project);
54
+ const bound = source.version === 1 && source.baselineRevision === row.revision_id;
55
+ const entry = object(row.record === null ? undefined : JSON.parse(row.record)), data = object(entry.data);
56
+ const isEnd = entry.kind === "agentrun.end" && terminal(data.state);
57
+ if (row.entry_id !== null && isEnd && (data.projectCheckpoint !== undefined || bound))
58
+ results.push({ runId: row.run_id, revisionId: row.revision_id, entryId: Number(row.entry_id),
59
+ ...(row.entry_attempt_epoch === null ? {} : { attemptEpoch: Number(row.entry_attempt_epoch) }), checkpoint: pick(data.projectCheckpoint) });
60
+ if (isEnd && row.entry_id !== null && row.journal_from !== null && Number(row.entry_id) >= Number(row.journal_from))
61
+ ended.add(row.run_id);
62
+ if (bound && terminal(row.state) && row.attempt_epoch !== null)
63
+ missing.set(row.run_id, { revisionId: row.revision_id, attemptEpoch: Number(row.attempt_epoch) });
64
+ }
65
+ for (const [runId, row] of missing)
66
+ if (!ended.has(runId))
67
+ results.push({ runId, ...row, entryId: null, checkpoint: {} });
68
+ return results.sort((a, b) => a.runId < b.runId ? -1 : a.runId > b.runId ? 1 : a.entryId === null ? 1 : b.entryId === null ? -1 : a.entryId - b.entryId);
69
+ }
70
+ }
@@ -1,9 +1,18 @@
1
1
  import { type LaunchSealed, type RunLaunchInput, type RunLaunchRow, type Runs } from "../store.js";
2
2
  import type { Dialect } from "../dialect.js";
3
3
  import type { SqliteExecutor } from "./connection.js";
4
+ /** JSON text `column` (a launch body, a command's body, a journal record) as every read of one id, number or flag out of
5
+ * it takes it: on Postgres its copy with escaped NULs and lone surrogates blanked (postgresReadableJson), so a run's text
6
+ * never makes the read fail; on SQLite (whose json functions read them) the column itself. Only a read that returns a
7
+ * member as written (readUsageMetadata, readLearningStarts) reads the column and retries on postgresUnicodeRefusal. */
8
+ export declare const readableJson: (dialect: Dialect, column: string) => string;
4
9
  /** Writes a run's launch row inside `tx` (the same transaction as the queued run it belongs to): the run path and the
5
10
  * trigger path both call it, so the row lands with its run or not at all. */
6
11
  export declare function writeLaunch(tx: Pick<SqliteExecutor, "run">, runId: string, launch: RunLaunchInput, at: number): Promise<void>;
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
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
14
+ * body's disk the store makes, at every create of a run, so no path that queues a run can leave it out. */
15
+ export declare function launchDiskOf(launch: RunLaunchInput | undefined): string | null;
7
16
  /** A message's launch secrets written over the run's (runs.command, runs.reopen), inside `tx`: the bytes as given, or a
8
17
  * function's answer to the bytes the launch holds now, read in the same transaction. */
9
18
  export declare function writeLaunchSealed(tx: Pick<SqliteExecutor, "run" | "get">, runId: string, launchSealed: LaunchSealed): Promise<void>;
@@ -28,3 +37,8 @@ export declare function readLearningStarts(executor: Pick<SqliteExecutor, "all">
28
37
  runId: string;
29
38
  learning: unknown;
30
39
  }[]>;
40
+ /** The ids of agent `agentId`'s runs started after `at` whose learning seed was read at or before it (the launch body's
41
+ * `learning.seededAt`), in one read of that one number a run started after `at`: never a run's section or input. */
42
+ export declare function readSeededBefore(executor: Pick<SqliteExecutor, "all"> & {
43
+ readonly dialect: Dialect;
44
+ }, agentId: string, at: number): Promise<string[]>;
@@ -5,11 +5,29 @@
5
5
  import { StoreLaunchMissing } from "../store.js";
6
6
  const RUN_LAUNCH_COLUMNS = "run_id, body, sealed, 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), createdAt: r.created_at });
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
9
+ * it takes it: on Postgres its copy with escaped NULs and lone surrogates blanked (postgresReadableJson), so a run's text
10
+ * never makes the read fail; on SQLite (whose json functions read them) the column itself. Only a read that returns a
11
+ * member as written (readUsageMetadata, readLearningStarts) reads the column and retries on postgresUnicodeRefusal. */
12
+ export const readableJson = (dialect, column) => (dialect.name === "postgres" ? postgresReadableJson(column) : column);
8
13
  /** Writes a run's launch row inside `tx` (the same transaction as the queued run it belongs to): the run path and the
9
14
  * trigger path both call it, so the row lands with its run or not at all. */
10
15
  export function writeLaunch(tx, runId, launch, at) {
11
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);
12
17
  }
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
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
20
+ * body's disk the store makes, at every create of a run, so no path that queues a run can leave it out. */
21
+ export function launchDiskOf(launch) {
22
+ const body = launch?.body;
23
+ if (typeof body !== "object" || body === null)
24
+ return null;
25
+ const disk = body.archil_disk;
26
+ if (typeof disk !== "object" || disk === null)
27
+ return null;
28
+ const { disk_id: id, kind } = disk;
29
+ return kind === "launch" && typeof id === "string" && id !== "" ? id : null;
30
+ }
13
31
  /** A message's launch secrets written over the run's (runs.command, runs.reopen), inside `tx`: the bytes as given, or a
14
32
  * function's answer to the bytes the launch holds now, read in the same transaction. */
15
33
  export async function writeLaunchSealed(tx, runId, launchSealed) {
@@ -30,6 +48,19 @@ export async function readLaunch(executor, runId) {
30
48
  throw new StoreLaunchMissing(runId);
31
49
  return runLaunchOf(row);
32
50
  }
51
+ /** Whether `error` (or a cause of it, six deep) is Postgres refusing a JSON document for an escaped NUL or a lone
52
+ * surrogate in it: the text type keeps them, but every json function decodes the whole document's strings. */
53
+ function postgresUnicodeRefusal(error) {
54
+ for (let cause = error, depth = 0; cause && depth < 6; cause = cause.cause, depth++)
55
+ if (cause.code === "22P05" || (cause.code === "22P02" && cause.reason === "json_surrogate"))
56
+ return true;
57
+ return false;
58
+ }
59
+ /** Postgres SQL for JSON text `column` with its escaped NULs and surrogates (\u0000, \ud800-\udfff) read as escaped
60
+ * spaces, for a read of one number or id out of a document whose other strings may hold one (0026_run_disk.sql blanks
61
+ * them for its backfill). A replaced escape stays an escape, so it is still JSON. Only an escape is replaced: u0000 after
62
+ * an escaped backslash (the text \u0000 in an id, written \\u0000) is text and is kept, so an id reads as it is. */
63
+ const postgresReadableJson = (column) => `regexp_replace(${column}, '(?<!\\\\)((?:\\\\\\\\)*)\\\\u(0000|[dD][89a-fA-F][0-9a-fA-F]{2})', '\\1\\\\u0020', 'g')`;
33
64
  /** Project accounting facts before crossing the database boundary. An SDK inbox can contain megabytes of replay;
34
65
  * the raw-JSON projection preserves strings, nulls and invalid scalar shapes equally on SQLite and Postgres.
35
66
  * Uses an ordinary store connection: a PostgreSQL Unicode refusal is retried outside a failed transaction. */
@@ -53,11 +84,7 @@ export async function readUsageMetadata(executor, runId, { completion }) {
53
84
  // PostgreSQL accepts escaped NUL/lone-surrogate strings in JSON TEXT but cannot extract ANY member of
54
85
  // such a document. Only its exact Unicode refusal permits the former JS read/parse cost for that row.
55
86
  // Never sanitize native identities or silently lose known counters to an unrelated replay string.
56
- let unicode = false;
57
- for (let cause = error, depth = 0; cause && depth < 6; cause = cause.cause, depth++)
58
- if (cause.code === "22P05" || (cause.code === "22P02" && cause.reason === "json_surrogate"))
59
- unicode = true;
60
- if (executor.dialect.name !== "postgres" || !unicode)
87
+ if (executor.dialect.name !== "postgres" || !postgresUnicodeRefusal(error))
61
88
  throw error;
62
89
  const raw = await executor.get(`SELECT l.body${completion ? ", s.value AS inbox" : ""} ${from}`, ...params);
63
90
  if (!raw)
@@ -79,6 +106,30 @@ export async function readUsageMetadata(executor, runId, { completion }) {
79
106
  * and never the sealed bytes, so an agent with thousands of runs reads kilobytes a run, not each run's input. `learning`
80
107
  * is the member's JSON text, null when the run's body names none. */
81
108
  export async function readLearningStarts(executor, agentId, where) {
82
- const rows = await executor.all(`SELECT run_id, ${executor.dialect.jsonText("body", ["learning"])} AS learning FROM run_launch WHERE run_id IN (SELECT id FROM runs WHERE agent_id = ? AND ${where})`, agentId);
83
- return rows.map((row) => ({ runId: row.run_id, learning: row.learning === null ? undefined : JSON.parse(row.learning) }));
109
+ const learning = executor.dialect.jsonText("body", ["learning"]);
110
+ const runs = `run_id IN (SELECT id FROM runs WHERE agent_id = ? AND ${where})`;
111
+ let rows;
112
+ try {
113
+ rows = await executor.all(`SELECT run_id, ${learning} AS learning FROM run_launch WHERE ${runs}`, agentId);
114
+ }
115
+ catch (error) {
116
+ // A body holding an escaped NUL or a lone surrogate anywhere (a run's input) makes Postgres refuse the whole read. Read
117
+ // again: `learning` out of each body that holds none, and each body that does whole, parsed here, so a run's notes
118
+ // come back as written, never blanked. On an ordinary store connection, as readUsageMetadata's, so the read again is
119
+ // outside a failed transaction.
120
+ if (executor.dialect.name !== "postgres" || !postgresUnicodeRefusal(error))
121
+ throw error;
122
+ rows = await executor.all(`SELECT run_id, CASE WHEN odd THEN NULL ELSE ${learning} END AS learning, CASE WHEN odd THEN body END AS body
123
+ FROM (SELECT run_id, body, ${postgresReadableJson("body")} <> body AS odd FROM run_launch WHERE ${runs}) AS launch`, agentId);
124
+ }
125
+ return rows.map((row) => ({
126
+ runId: row.run_id,
127
+ learning: row.body != null ? (JSON.parse(row.body)?.learning ?? undefined) : row.learning === null ? undefined : JSON.parse(row.learning),
128
+ }));
129
+ }
130
+ /** The ids of agent `agentId`'s runs started after `at` whose learning seed was read at or before it (the launch body's
131
+ * `learning.seededAt`), in one read of that one number a run started after `at`: never a run's section or input. */
132
+ export async function readSeededBefore(executor, agentId, at) {
133
+ const rows = await executor.all(`SELECT run_id, ${executor.dialect.jsonText(readableJson(executor.dialect, "body"), ["learning", "seededAt"])} AS seeded_at FROM run_launch WHERE run_id IN (SELECT id FROM runs WHERE agent_id = ? AND started_at > ?)`, agentId, at);
134
+ return rows.flatMap((row) => (row.seeded_at !== null && Number(row.seeded_at) <= at ? [row.run_id] : []));
84
135
  }
@@ -27,11 +27,12 @@ export type RunRecord = {
27
27
  imported_from: string | null;
28
28
  sign_in_handed: string | null;
29
29
  labels: string | null;
30
+ disk: string | null;
30
31
  started_at: number;
31
32
  ended_at: number | null;
32
33
  purged_at: number | null;
33
34
  };
34
- export declare const RUN_COLUMNS = "id, agent_id, revision_id, state, epoch, runner, claimed_until, wake_at, backoff_until, cost_usd, cost_upper_bound, metrics, summary, outcome, archived_at, imported_from, sign_in_handed, labels, started_at, ended_at, purged_at";
35
+ export declare const RUN_COLUMNS = "id, agent_id, revision_id, state, epoch, runner, claimed_until, wake_at, backoff_until, cost_usd, cost_upper_bound, metrics, summary, outcome, archived_at, imported_from, sign_in_handed, labels, disk, started_at, ended_at, purged_at";
35
36
  /** A run's exact cost in SQL, for a query that reads runs.cost_usd itself (`alias`: its runs table's): NULL when the
36
37
  * stored figure is only the most the run can have cost, as runOf reads it. Every raw read of the column goes through it. */
37
38
  export declare const EXACT_COST_USD: (alias: string) => string;
@@ -6,13 +6,14 @@
6
6
  import { 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
10
  /** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
10
11
  * list of runs names. */
11
12
  export const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
12
13
  /** 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
13
14
  * the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
14
15
  export const SIGN_IN_HANDED_LOCK = "sign_in_handed";
15
- export const RUN_COLUMNS = "id, agent_id, revision_id, state, epoch, runner, claimed_until, wake_at, backoff_until, cost_usd, cost_upper_bound, metrics, summary, outcome, archived_at, imported_from, sign_in_handed, labels, started_at, ended_at, purged_at";
16
+ export const RUN_COLUMNS = "id, agent_id, revision_id, state, epoch, runner, claimed_until, wake_at, backoff_until, cost_usd, cost_upper_bound, metrics, summary, outcome, archived_at, imported_from, sign_in_handed, labels, disk, started_at, ended_at, purged_at";
16
17
  /** A run's exact cost in SQL, for a query that reads runs.cost_usd itself (`alias`: its runs table's): NULL when the
17
18
  * stored figure is only the most the run can have cost, as runOf reads it. Every raw read of the column goes through it. */
18
19
  export const EXACT_COST_USD = (alias) => `CASE WHEN ${alias}.cost_upper_bound = 1 THEN NULL ELSE ${alias}.cost_usd END`;
@@ -25,7 +26,7 @@ export const runOf = (r) => ({
25
26
  wakeAt: r.wake_at, backoffUntil: r.backoff_until, ...costOf(r.cost_usd, r.cost_upper_bound), metrics: r.metrics === null ? null : JSON.parse(r.metrics), summary: r.summary,
26
27
  outcome: r.outcome ?? null, archivedAt: r.archived_at, importedFrom: r.imported_from === null ? null : JSON.parse(r.imported_from),
27
28
  signInHanded: r.sign_in_handed === null ? null : JSON.parse(r.sign_in_handed), labels: r.labels === null ? {} : JSON.parse(r.labels),
28
- startedAt: r.started_at, endedAt: r.ended_at, purgedAt: r.purged_at ?? null,
29
+ disk: r.disk, startedAt: r.started_at, endedAt: r.ended_at, purgedAt: r.purged_at ?? null,
29
30
  });
30
31
  /** Applies `patch` to the claim's run, then writes `also`, through `tx` (an open transaction's handle). Fenced, with nothing
31
32
  * of it written once the caller rolls back, when the run's epoch is not the claim's or the claim's lease ran out before
@@ -110,7 +111,7 @@ export async function applyRunPatch(tx, claim, patch, also, now, dialect = SQLIT
110
111
  export const REOPEN_SET = "state = 'running', runner = NULL, claimed_until = NULL, wake_at = NULL, backoff_until = NULL, ended_at = NULL, summary = NULL, outcome = NULL";
111
112
  export const reopenableWhere = (d) => `(state = 'ended' OR (state = 'stopped' AND outcome = 'stopped')) AND imported_from IS NULL AND purged_at IS NULL
112
113
  AND EXISTS (SELECT 1 FROM revisions WHERE revisions.id = runs.revision_id AND revisions.kind = 'agentic')
113
- AND NOT EXISTS (SELECT 1 FROM run_launch WHERE run_launch.run_id = runs.id AND ${d.jsonText("run_launch.body", ["harness"])} = 'learning')`;
114
+ AND NOT EXISTS (SELECT 1 FROM run_launch WHERE run_launch.run_id = runs.id AND ${d.jsonText(readableJson(d, "run_launch.body"), ["harness"])} = 'learning')`;
114
115
  /** After a write that made the run final has taken its own rows: every command row of the run still untaken is refused
115
116
  * with the end's reason, in that same transaction. The store takes no new row on a final run (RunFinal), so a row the
116
117
  * end left untaken would stay so for good, as it would if the runner died right after the end.