@parcha/agentrun-serve 0.1.0-beta.46 → 0.1.0-beta.48

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 (292) hide show
  1. package/CHANGELOG.md +211 -1
  2. package/NOTICE +4 -0
  3. package/README.md +350 -53
  4. package/dist/acp-package.d.ts +53 -6
  5. package/dist/acp-package.js +3 -3
  6. package/dist/adapter-facts.d.ts +3 -4
  7. package/dist/adapter-facts.js +4 -4
  8. package/dist/adapters.d.ts +3 -5
  9. package/dist/adapters.js +1 -31
  10. package/dist/agents-store.d.ts +5 -3
  11. package/dist/agents-store.js +13 -8
  12. package/dist/answer-json.d.ts +10 -0
  13. package/dist/answer-json.js +24 -0
  14. package/dist/api-only.d.ts +11 -11
  15. package/dist/api-only.js +16 -11
  16. package/dist/archil-routes.js +2 -1
  17. package/dist/browser-live.d.ts +9 -4
  18. package/dist/browser-live.js +18 -5
  19. package/dist/cap.js +10 -2
  20. package/dist/chatgpt-plan.js +2 -1
  21. package/dist/cli.js +109 -41
  22. package/dist/composio-made.d.ts +19 -2
  23. package/dist/composio-made.js +17 -5
  24. package/dist/composio-preset.d.ts +42 -7
  25. package/dist/composio-preset.js +106 -70
  26. package/dist/composio-triggers.d.ts +9 -4
  27. package/dist/composio-triggers.js +20 -13
  28. package/dist/computer-attach.d.ts +11 -10
  29. package/dist/computer-attach.js +13 -14
  30. package/dist/computer-commands.d.ts +3 -0
  31. package/dist/computer-commands.js +3 -2
  32. package/dist/computer-lost.d.ts +4 -0
  33. package/dist/computer-lost.js +8 -3
  34. package/dist/computer-routes.d.ts +8 -5
  35. package/dist/computer-routes.js +7 -5
  36. package/dist/computer-setting.d.ts +2 -0
  37. package/dist/computer-setting.js +2 -0
  38. package/dist/computers.d.ts +14 -76
  39. package/dist/computers.js +24 -68
  40. package/dist/conformance.d.ts +4 -0
  41. package/dist/conformance.js +6 -0
  42. package/dist/console/assets/index-CTGegcKt.css +1 -0
  43. package/dist/console/assets/index-D1Se6mgG.js +715 -0
  44. package/dist/console/index.html +2 -2
  45. package/dist/cost-heal.d.ts +13 -0
  46. package/dist/cost-heal.js +27 -0
  47. package/dist/dial-relay.d.ts +6 -2
  48. package/dist/dial-relay.js +9 -12
  49. package/dist/email/routes.js +2 -1
  50. package/dist/errors.d.ts +2 -0
  51. package/dist/errors.js +5 -0
  52. package/dist/exa-keyless.d.ts +1 -1
  53. package/dist/exa-keyless.js +1 -1
  54. package/dist/faux-computers.d.ts +2 -3
  55. package/dist/faux-computers.js +9 -31
  56. package/dist/github-connection.js +2 -1
  57. package/dist/harness-archive.d.ts +19 -2
  58. package/dist/harness-archive.js +49 -15
  59. package/dist/harness-check.d.ts +75 -0
  60. package/dist/harness-check.js +518 -0
  61. package/dist/harness-cursor.d.ts +28 -0
  62. package/dist/harness-cursor.js +42 -0
  63. package/dist/harness-goose.d.ts +4 -29
  64. package/dist/harness-goose.js +6 -86
  65. package/dist/harness-module.d.ts +4 -0
  66. package/dist/harness-module.js +1 -0
  67. package/dist/harness-owned.d.ts +52 -0
  68. package/dist/harness-owned.js +92 -0
  69. package/dist/harness-recipe.d.ts +127 -0
  70. package/dist/harness-recipe.js +335 -0
  71. package/dist/harness-table.d.ts +120 -5
  72. package/dist/harness-table.js +280 -6
  73. package/dist/harness.d.ts +2 -0
  74. package/dist/harness.js +5 -4
  75. package/dist/host-check.d.ts +6 -0
  76. package/dist/host-check.js +14 -0
  77. package/dist/host.d.ts +10 -3
  78. package/dist/host.js +17 -3
  79. package/dist/import-sources.d.ts +2 -0
  80. package/dist/import-sources.js +2 -0
  81. package/dist/import-summary.d.ts +15 -5
  82. package/dist/import-summary.js +24 -8
  83. package/dist/import.d.ts +24 -4
  84. package/dist/import.js +108 -26
  85. package/dist/key-refusal.d.ts +3 -1
  86. package/dist/key-refusal.js +26 -5
  87. package/dist/key-words.d.ts +1 -1
  88. package/dist/key-words.js +4 -4
  89. package/dist/learn/check.d.ts +3 -0
  90. package/dist/learn/check.js +31 -20
  91. package/dist/learn/compile.js +12 -5
  92. package/dist/learn/promote.d.ts +80 -2
  93. package/dist/learn/promote.js +95 -3
  94. package/dist/learn/secrets.d.ts +4 -0
  95. package/dist/learn/secrets.js +23 -0
  96. package/dist/learned-nodes.d.ts +4 -3
  97. package/dist/learned-workflow.js +4 -1
  98. package/dist/learning-cost.d.ts +6 -16
  99. package/dist/learning-cost.js +6 -12
  100. package/dist/learning-model.d.ts +18 -0
  101. package/dist/learning-model.js +77 -0
  102. package/dist/learning-words.d.ts +2 -0
  103. package/dist/learning-words.js +1 -1
  104. package/dist/listen-first.d.ts +10 -0
  105. package/dist/listen-first.js +25 -1
  106. package/dist/llms.txt +2 -1
  107. package/dist/mcp-gateway.d.ts +2 -1
  108. package/dist/mcp-gateway.js +31 -15
  109. package/dist/mcp-probe.d.ts +18 -6
  110. package/dist/mcp-probe.js +41 -8
  111. package/dist/mcp-secret-writes.d.ts +8 -0
  112. package/dist/mcp-secret-writes.js +104 -4
  113. package/dist/mcp-servers-store.d.ts +11 -5
  114. package/dist/mcp-servers-store.js +46 -20
  115. package/dist/model-form.d.ts +2 -1
  116. package/dist/model-form.js +30 -16
  117. package/dist/model-gateway.d.ts +113 -0
  118. package/dist/model-gateway.js +433 -0
  119. package/dist/model.d.ts +5 -3
  120. package/dist/model.js +26 -18
  121. package/dist/monid.js +1 -1
  122. package/dist/native/binding.d.ts +4 -0
  123. package/dist/native/binding.js +9 -0
  124. package/dist/native/cli.d.ts +3 -2
  125. package/dist/native/cli.js +6 -5
  126. package/dist/openapi.d.ts +2 -0
  127. package/dist/openapi.js +356 -43
  128. package/dist/owner.d.ts +4 -2
  129. package/dist/owner.js +10 -4
  130. package/dist/page.d.ts +9 -0
  131. package/dist/page.js +10 -2
  132. package/dist/port-models.js +17 -10
  133. package/dist/provider.d.ts +3 -3
  134. package/dist/provider.js +12 -3
  135. package/dist/public-address.d.ts +28 -0
  136. package/dist/public-address.js +62 -0
  137. package/dist/run-launch.d.ts +1 -1
  138. package/dist/run-launch.js +1 -1
  139. package/dist/run-secrets.d.ts +3 -2
  140. package/dist/run-secrets.js +7 -4
  141. package/dist/run-trace.js +6 -3
  142. package/dist/runner/adapter-loop.d.ts +14 -8
  143. package/dist/runner/adapter-loop.js +58 -24
  144. package/dist/runner/adapter-run-spec.d.ts +15 -2
  145. package/dist/runner/adapter-run-spec.js +48 -10
  146. package/dist/runner/adapters/index.d.ts +4 -2
  147. package/dist/runner/adapters/index.js +6 -26
  148. package/dist/runner/adapters/pi.d.ts +5 -2
  149. package/dist/runner/adapters/pi.js +7 -4
  150. package/dist/runner/audit.d.ts +7 -0
  151. package/dist/runner/audit.js +15 -6
  152. package/dist/runner/claim-secrets.js +5 -1
  153. package/dist/runner/cli-run-spec.js +22 -10
  154. package/dist/runner/compare-end.d.ts +43 -0
  155. package/dist/runner/compare-end.js +95 -0
  156. package/dist/runner/computer-journal.d.ts +3 -0
  157. package/dist/runner/computer-journal.js +22 -2
  158. package/dist/runner/conformance/host-tools.d.ts +4 -0
  159. package/dist/runner/conformance/host-tools.js +14 -0
  160. package/dist/runner/harness-loops.d.ts +6 -1
  161. package/dist/runner/harness-loops.js +94 -54
  162. package/dist/runner/held.d.ts +5 -4
  163. package/dist/runner/journal-kinds.d.ts +4 -1
  164. package/dist/runner/journal-kinds.js +3 -0
  165. package/dist/runner/loop-contract.d.ts +10 -0
  166. package/dist/runner/main.d.ts +5 -5
  167. package/dist/runner/main.js +62 -11
  168. package/dist/runner/mcp-claim.js +40 -12
  169. package/dist/runner/meta-tools.js +4 -2
  170. package/dist/runner/metrics.d.ts +26 -0
  171. package/dist/runner/metrics.js +44 -1
  172. package/dist/runner/presence.d.ts +44 -0
  173. package/dist/runner/presence.js +96 -0
  174. package/dist/runner/promotion.d.ts +70 -7
  175. package/dist/runner/promotion.js +178 -28
  176. package/dist/runner/run-child-host.js +40 -10
  177. package/dist/runner/runner.d.ts +5 -0
  178. package/dist/runner/runner.js +8 -0
  179. package/dist/runner/sdk-loop.js +25 -1
  180. package/dist/runner/store-port.d.ts +3 -0
  181. package/dist/runner/store-secrets.js +19 -7
  182. package/dist/runner/workflow-first.d.ts +5 -2
  183. package/dist/runner/workflow-first.js +6 -3
  184. package/dist/runner/workflow-spec.js +3 -2
  185. package/dist/runs-over-store.d.ts +7 -2
  186. package/dist/runs-over-store.js +12 -4
  187. package/dist/sdk/check.d.ts +31 -0
  188. package/dist/sdk/check.js +58 -0
  189. package/dist/sdk/runs.d.ts +11 -3
  190. package/dist/sdk/runs.js +90 -15
  191. package/dist/sdk/schema.js +12 -4
  192. package/dist/secret-writes.d.ts +16 -3
  193. package/dist/secret-writes.js +28 -3
  194. package/dist/server-secrets-store.js +5 -0
  195. package/dist/server.d.ts +34 -2
  196. package/dist/server.js +437 -90
  197. package/dist/sign-ins-secrets.js +4 -1
  198. package/dist/skills/agentrun-serve/SKILL.md +11 -0
  199. package/dist/slack-url.d.ts +5 -0
  200. package/dist/slack-url.js +11 -0
  201. package/dist/slack.d.ts +107 -0
  202. package/dist/slack.js +292 -0
  203. package/dist/start-switches.d.ts +28 -0
  204. package/dist/start-switches.js +37 -0
  205. package/dist/stats.d.ts +1 -1
  206. package/dist/stats.js +2 -1
  207. package/dist/stop-signals.d.ts +11 -0
  208. package/dist/stop-signals.js +22 -0
  209. package/dist/store/agent-body.d.ts +1 -1
  210. package/dist/store/agent-body.js +2 -2
  211. package/dist/store/index.d.ts +3 -3
  212. package/dist/store/index.js +1 -1
  213. package/dist/store/postgres/migrations/0020_runners.sql +18 -0
  214. package/dist/store/postgres/store.d.ts +1 -1
  215. package/dist/store/postgres/store.js +4 -2
  216. package/dist/store/schema.d.ts +13 -5
  217. package/dist/store/schema.js +20 -11
  218. package/dist/store/seal.d.ts +16 -4
  219. package/dist/store/sqlite/store.d.ts +1 -1
  220. package/dist/store/sqlite/store.js +112 -28
  221. package/dist/store/store.d.ts +78 -24
  222. package/dist/store/store.js +11 -0
  223. package/dist/store-reads/analytics.js +3 -2
  224. package/dist/store-reads/cap.d.ts +7 -0
  225. package/dist/store-reads/cap.js +11 -1
  226. package/dist/store-reads/journal.js +27 -1
  227. package/dist/store-reads/learning.d.ts +23 -2
  228. package/dist/store-reads/learning.js +29 -8
  229. package/dist/store-reads/runs.d.ts +9 -0
  230. package/dist/store-reads/runs.js +19 -6
  231. package/dist/store-reads/stats.d.ts +4 -1
  232. package/dist/store-reads/stats.js +15 -6
  233. package/dist/store-view.d.ts +125 -14
  234. package/dist/store-view.js +156 -21
  235. package/dist/telemetry.d.ts +7 -7
  236. package/dist/telemetry.js +38 -15
  237. package/dist/template-eval.js +5 -2
  238. package/dist/template-github-permissions.d.ts +5 -1
  239. package/dist/template-github-permissions.js +7 -3
  240. package/dist/timeline/index.d.ts +1 -1
  241. package/dist/timeline/index.js +1 -1
  242. package/dist/timeline/timeline.d.ts +8 -0
  243. package/dist/timeline/timeline.js +30 -0
  244. package/dist/tool-gate.d.ts +8 -4
  245. package/dist/tool-gate.js +6 -7
  246. package/dist/tool-keys-store.js +2 -2
  247. package/dist/tool-keys.js +2 -1
  248. package/dist/tool-plane.d.ts +4 -0
  249. package/dist/tool-plane.js +12 -12
  250. package/dist/trigger-adapter.d.ts +3 -4
  251. package/dist/trigger-adapter.js +2 -2
  252. package/dist/trigger-composio.js +3 -3
  253. package/dist/trigger-email.js +0 -2
  254. package/dist/trigger-feed.js +0 -2
  255. package/dist/trigger-schedule.js +3 -0
  256. package/dist/trigger-secrets-shown.d.ts +6 -3
  257. package/dist/trigger-secrets-shown.js +22 -13
  258. package/dist/trigger-targets.d.ts +12 -3
  259. package/dist/trigger-targets.js +90 -10
  260. package/dist/triggers.d.ts +2 -2
  261. package/dist/validate.js +5 -1
  262. package/dist/viewer-credential.d.ts +25 -0
  263. package/dist/viewer-credential.js +51 -0
  264. package/dist/webhooks.d.ts +82 -8
  265. package/dist/webhooks.js +128 -45
  266. package/dist/wires.d.ts +6 -1
  267. package/dist/wires.js +5 -0
  268. package/dist/workspace/attach.d.ts +3 -2
  269. package/dist/workspace/attach.js +2 -1
  270. package/dist/workspace/attached-computers.d.ts +6 -8
  271. package/dist/workspace/attached-computers.js +27 -24
  272. package/dist/workspace/computer-deps.d.ts +2 -3
  273. package/dist/workspace/computer-deps.js +3 -6
  274. package/dist/workspace/computer-for-claim.js +72 -96
  275. package/dist/workspace/computer-kept.d.ts +0 -7
  276. package/dist/workspace/computer-kept.js +0 -17
  277. package/dist/workspace/resolve-computer.d.ts +23 -14
  278. package/dist/workspace/resolve-computer.js +66 -61
  279. package/dist/workspace/synced-folder.d.ts +20 -15
  280. package/dist/workspace/synced-folder.js +33 -43
  281. package/launcher/agentrun-serve +4 -1
  282. package/package.json +18 -10
  283. package/skills/agentrun-serve/SKILL.md +11 -0
  284. package/templates/README.md +1 -1
  285. package/templates/code-review/agent.md +2 -3
  286. package/templates/code-review/evals/mentions-defused/case.yaml +0 -1
  287. package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +0 -27
  288. package/templates/code-review/evals/other-repo/case.yaml +0 -1
  289. package/templates/code-review/evals/other-repo/fixtures/github-mcp.json +0 -18
  290. package/templates/code-review/evals/other-repo/graders/no-other-repo.md +1 -1
  291. package/dist/console/assets/index-C2MYaUBa.css +0 -1
  292. package/dist/console/assets/index-CZZeCReB.js +0 -701
@@ -131,16 +131,18 @@ export interface ListedFilter {
131
131
  export interface ListedRun {
132
132
  readonly row: RunRow;
133
133
  readonly agentArchivedAt: number | null;
134
- readonly listing: {
135
- readonly item: Record<string, unknown>;
136
- readonly cut: string;
137
- } | null;
134
+ readonly listing: KeptListing | null;
138
135
  readonly listStatus: string | null;
139
136
  /** The run's list_mark now: the mark a listing built from here is written at. */
140
137
  readonly listMark: number;
141
138
  /** The listing is behind now (Runs.behind's three ways): a list builds it before it shows it. */
142
139
  readonly behind: boolean;
143
140
  }
141
+ /** A run's listing as its row keeps it (Runs.putListing's item and cut). */
142
+ export interface KeptListing {
143
+ readonly item: Record<string, unknown>;
144
+ readonly cut: string;
145
+ }
144
146
  /** Whether `run`, of a revision of `kind`, was stopped by a person and waits for them (state 'stopped', outcome 'stopped',
145
147
  * an agentic run started here): its turn is over, the run is not. A workflow's stop, a cancel and an imported run's stop
146
148
  * are over for good. */
@@ -707,10 +709,12 @@ export interface Agents {
707
709
  /** Adds a revision and makes it the agent's current one, in one commit. With `ifCurrent` (a revision id, or null for
708
710
  * none), only while that is still the agent's current revision, compared in the same transaction under the agent row's
709
711
  * lock: RevisionMoved, and nothing written, when another writer (this server's, or another server's on the same store)
710
- * moved it first. */
712
+ * moved it first. `description` (a string, or null for none), when given, is the agent's new description, written in the
713
+ * same commit: a revision refused by `ifCurrent` leaves it as it was. Absent, the description stays. */
711
714
  addRevision(revision: NewRevision & {
712
715
  agentId: string;
713
716
  ifCurrent?: string | null;
717
+ description?: string | null;
714
718
  }): Promise<void>;
715
719
  /** Makes `revisionId`, one of the agent's own, its current one. StoreConstraint when it is not a revision of `agentId`. */
716
720
  setCurrent(agentId: string, revisionId: string): Promise<void>;
@@ -948,6 +952,26 @@ export interface Runs {
948
952
  flags: number;
949
953
  agentId?: string;
950
954
  }): Promise<number>;
955
+ /** Writes `cost`, what its booking gives by the end's own rule (metrics.ts endCostOf: a figure, the most the run can
956
+ * have cost when `costUpperBound`), as ended run `runId`'s stored cost, as its end would have, only while that cost is
957
+ * still unknown (null, no bound) and the run is the end it was read at (`epoch`, `endedAt`): one conditional write, so a
958
+ * stored figure is never overwritten, a run reopened or ended again since keeps what its end wrote, and a second heal
959
+ * writes nothing. True when it was written. */
960
+ healCost(runId: string, at: {
961
+ readonly epoch: number;
962
+ readonly endedAt: number;
963
+ }, cost: {
964
+ readonly costUsd: number;
965
+ readonly costUpperBound: boolean;
966
+ }): Promise<boolean>;
967
+ /** Ended runs, not imported, whose stored cost is unknown (null, no bound), by id after `after`, at most `limit`: what
968
+ * the server's start sweep reads to heal (cost-heal.ts). Left out are those the heal's rule refuses by what the store
969
+ * holds alone: a person's stop of an agentic run, a native run, a run with no booked answer (sqlite/store.ts says
970
+ * which are kept, and why). */
971
+ costUnknown(options: {
972
+ limit: number;
973
+ after?: string;
974
+ }): Promise<string[]>;
951
975
  /** Archives the run (its own archive time), or unarchives it: idempotent. */
952
976
  archive(runId: string, archived: boolean): Promise<void>;
953
977
  /** Every run of agent `agentId` with its state, oldest first, its built-in learning runs included (which list leaves
@@ -979,6 +1003,12 @@ export interface Runs {
979
1003
  * other state (or none) is left as it is, false. Its claim, if one holds it while it parks, is let go as before: the
980
1004
  * release sets wake_at as the loop's end says. */
981
1005
  wake(runId: string): Promise<boolean>;
1006
+ /** Ends `failed`, its summary `summary`, every run still queued that no runner ever claimed (epoch 0), each in a
1007
+ * transaction of its own, as an end does: its untaken commands refused and its launch's secrets erased in the same
1008
+ * write. A run a runner claimed (at any epoch since) is left as it is, and so is one a claim takes meanwhile: the
1009
+ * write is fenced on epoch 0. Answers the runs it ended. What the server does with runs no runner here can claim
1010
+ * (runner/presence.ts). */
1011
+ failUnclaimed(summary: string): Promise<string[]>;
982
1012
  /** The runs a runner may claim, oldest first: queued; parked with an untaken command or its wake_at passed; running with
983
1013
  * no runner (released at a runner's shutdown: it stays running, a run that started keeps that fact) or its claim run
984
1014
  * out. */
@@ -1420,27 +1450,46 @@ export interface MintedTokens {
1420
1450
  * its machine clears it. Nothing when no row has that token. */
1421
1451
  refuse(tokenSha256: string, words: string): Promise<void>;
1422
1452
  }
1423
- /** Where a runner in a process of its own takes the dials relayed to it (store schema `runner_relays`, dial-relay.ts): its
1424
- * attach listener's host and port, under the runner's name and its incarnation (its process since its start), and when
1425
- * it last said it was there. */
1426
- export interface RunnerRelayRow {
1453
+ /** A runner's word that it is there (store schema `runners`, runner/presence.ts): its name, its incarnation (its process
1454
+ * since its start: the one its relay's minted tokens and proofs name), why it refused to claim any run (its words; null
1455
+ * for one that claims), where its relay's attach listener takes the dials the server relays to it (dial-relay.ts; null
1456
+ * for a runner with no relay) and when it last said it was there. */
1457
+ export interface RunnerRow {
1427
1458
  readonly runner: string;
1428
1459
  readonly incarnation: string;
1429
- /** The host the server reaches it at: loopback, or the runner's live host (live-reach.ts). */
1430
- readonly host: string;
1431
- readonly port: number;
1460
+ readonly refused: string | null;
1461
+ /** The host the server reaches its relay at: loopback, or the runner's live host (live-reach.ts). Null with no relay. */
1462
+ readonly host: string | null;
1463
+ readonly port: number | null;
1432
1464
  readonly seenAt: number;
1433
1465
  }
1434
- /** The runners' attach listeners (dial-relay.ts). */
1435
- export interface RunnerRelays {
1436
- /** Keeps `row` for its runner, replacing any earlier incarnation's, seen now. */
1437
- put(row: Omit<RunnerRelayRow, "seenAt">): Promise<void>;
1438
- /** Marks the runner's row seen now, while it is still this incarnation's. False when it is not. */
1439
- seen(runner: string, incarnation: string): Promise<boolean>;
1440
- /** The runner's row, whichever incarnation's, or undefined. */
1441
- get(runner: string): Promise<RunnerRelayRow | undefined>;
1442
- /** Every runner's row. */
1443
- list(): Promise<RunnerRelayRow[]>;
1466
+ /** How long a runner's row is kept once it stopped saying it is there (a runner that died without its close): its next
1467
+ * put removes every row older than this. */
1468
+ export declare const RUNNER_ROWS_KEPT_MS: number;
1469
+ /** Every runner's word that it is there (runner/presence.ts). */
1470
+ export interface Runners {
1471
+ /** Keeps `row` for its runner, replacing any earlier incarnation's, seen now; rows no runner renewed for
1472
+ * RUNNER_ROWS_KEPT_MS go in the same write. Its host and port go together: both absent (or null) for no relay. With
1473
+ * `unlessLiveMs`, not over another incarnation's row of the same name seen within that: a process's put again of a row
1474
+ * that went (pruned, a store restored) never takes back a later process's live one. False when such a row is there and
1475
+ * nothing was written. */
1476
+ put(row: Omit<RunnerRow, "seenAt" | "host" | "port"> & {
1477
+ readonly host?: string | null;
1478
+ readonly port?: number | null;
1479
+ }, options?: {
1480
+ readonly unlessLiveMs?: number;
1481
+ }): Promise<boolean>;
1482
+ /** Marks the runner's row seen now and says on it where its relay answers (`at`, or null for nowhere), while the row is
1483
+ * still this incarnation's. False when it is not (or there is none), and nothing was written. */
1484
+ renew(runner: string, incarnation: string, at: {
1485
+ readonly host: string;
1486
+ readonly port: number;
1487
+ } | null): Promise<boolean>;
1488
+ /** Every runner's row, by name. */
1489
+ list(): Promise<RunnerRow[]>;
1490
+ /** The rows seen within `staleMs`, by name. Every row's time (a put's, a seen's) and this "now" are the deadlines'
1491
+ * clock (fence.ts storeNow: the database's on Postgres), never one process's: runners on other hosts are judged alike. */
1492
+ live(staleMs: number): Promise<RunnerRow[]>;
1444
1493
  /** The runner's row gone, while it is still this incarnation's. */
1445
1494
  remove(runner: string, incarnation: string): Promise<void>;
1446
1495
  }
@@ -1697,8 +1746,8 @@ export interface Store {
1697
1746
  readonly pairedComputers: PairedComputers;
1698
1747
  /** The tokens minted for hosted machines that dial this server, each kept as its SHA-256. */
1699
1748
  readonly mintedTokens: MintedTokens;
1700
- /** Where each runner in a process of its own takes the dials relayed to it. */
1701
- readonly runnerRelays: RunnerRelays;
1749
+ /** Every runner's word that it is there, whether it refused to claim, and where its relay takes the dials relayed to it. */
1750
+ readonly runners: Runners;
1702
1751
  /** The template packages imported from GitHub, their files kept as read. */
1703
1752
  readonly importedTemplates: ImportedTemplates;
1704
1753
  /** Each trigger source's sealed settings (design 2.1): a value per adapter, scope and field. */
@@ -1853,6 +1902,11 @@ export declare class StoreFromEarlierVersion extends Error {
1853
1902
  readonly found: readonly string[];
1854
1903
  constructor(dataDir: string, found: readonly string[]);
1855
1904
  }
1905
+ /** A store this release's server upgrades in place at its start, which a runner never does: the runner waits for that
1906
+ * start. `what`: what the server does to it. */
1907
+ export declare class StoreAwaitsServer extends Error {
1908
+ constructor(what: string);
1909
+ }
1856
1910
  /** A store whose schema version this build does not know (or one the server has not set up yet). */
1857
1911
  export declare class SchemaVersionUnknown extends Error {
1858
1912
  readonly found: number | null;
@@ -78,6 +78,9 @@ export const STORE_WRITE_DEADLINE_MS = 10_000;
78
78
  * slow commit is still landing a set whose token the provider already spent, and a waiter then takes over and refreshes
79
79
  * with the spent one. Made from the two bounds so it cannot drift. */
80
80
  export const MCP_REFRESH_LEASE_MS = MCP_REFRESH_CALL_MS + STORE_WRITE_DEADLINE_MS + 2_000;
81
+ /** How long a runner's row is kept once it stopped saying it is there (a runner that died without its close): its next
82
+ * put removes every row older than this. */
83
+ export const RUNNER_ROWS_KEPT_MS = 24 * 60 * 60_000;
81
84
  /** A write under a claim whose epoch is no longer the run's: refused, and nothing written. */
82
85
  export class Fenced extends Error {
83
86
  runId;
@@ -155,6 +158,14 @@ export class StoreFromEarlierVersion extends Error {
155
158
  this.found = found;
156
159
  }
157
160
  }
161
+ /** A store this release's server upgrades in place at its start, which a runner never does: the runner waits for that
162
+ * start. `what`: what the server does to it. */
163
+ export class StoreAwaitsServer extends Error {
164
+ constructor(what) {
165
+ super(`Start the AgentRun server to ${what} before opening a runner. The runner does not upgrade this store.`);
166
+ this.name = "StoreAwaitsServer";
167
+ }
168
+ }
158
169
  /** A store whose schema version this build does not know (or one the server has not set up yet). */
159
170
  export class SchemaVersionUnknown extends Error {
160
171
  found;
@@ -5,7 +5,7 @@
5
5
  // the totals alone; its metrics, cost and effort wait for its end. No journal is read: the figures are the row's.
6
6
  import { sdkOf } from "../sdk/runs.js";
7
7
  import { DEFAULT_HARNESS } from "../harness.js";
8
- import { runReadState } from "./runs.js";
8
+ import { costOfRun, runReadState } from "./runs.js";
9
9
  import { isShadowRun, shadowRunId } from "../learn/audit.js";
10
10
  const PENDING_ENDS = new Set(["completed", "escalated", "failed", "stopped"]);
11
11
  const num = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
@@ -52,7 +52,8 @@ export async function analytics(store, filter = {}) {
52
52
  facts.push({
53
53
  startedMs: run.startedAt,
54
54
  endedMs: run.endedAt,
55
- costUsd: run.costUsd,
55
+ // As its read says it (costOfRun): an imported session's catalog estimate when its source priced none.
56
+ costUsd: costOfRun(run),
56
57
  running,
57
58
  pending: isPending,
58
59
  state: isPending ? pending[run.id] : read,
@@ -1,8 +1,15 @@
1
1
  import type { Context } from "@earendil-works/chord/context";
2
2
  import { type Meters } from "../cap.js";
3
3
  import type { Store } from "../store/store.js";
4
+ import { type CostBasis } from "../runner/metrics.js";
4
5
  /** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
5
6
  export declare function spentOverStore(store: Store, runId: string, context: Context): Promise<number | null>;
7
+ /** What a run has spent (spentOverStore) and how its model requests were priced so far (metrics.ts costBasisOf), from one
8
+ * read of its journal; `basis` undefined while it booked no model request. */
9
+ export declare function spendOverStore(store: Store, runId: string, context: Context): Promise<{
10
+ usd: number | null;
11
+ basis: CostBasis | undefined;
12
+ }>;
6
13
  /** /usage's meters for a run that ended, over the one store's journal (design 2.1): cap.ts meterFiguresFrom over A's
7
14
  * entries, with the run's paid tool charges folded back from its paid-tools session (journal.ts withPaidCosts) as the
8
15
  * file-backed run held each on its root. A run with no journal reads as a store that was never there: every figure 0, tokens unknown. */
@@ -1,12 +1,22 @@
1
1
  import { meterFiguresFrom, spentFrom } from "../cap.js";
2
- import { bookedListSpend, bookedSpend, cliModelWorked, costIsBooked } from "../runner/metrics.js";
2
+ import { bookedListSpend, bookedSpend, cliModelWorked, costBasisOf, costIsBooked } from "../runner/metrics.js";
3
3
  import { isCliRecord, runEntries, withPaidCosts } from "./journal.js";
4
4
  /** A CLI run whose model answered or called a tool with no model request booked (metrics.ts cliModelWorked): what it
5
5
  * spent no booking read, so its spend is unknown. */
6
6
  const unbookedCli = (entries) => isCliRecord(entries) && bookedSpend(entries, "model").requests === 0 && cliModelWorked(entries);
7
7
  /** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
8
8
  export async function spentOverStore(store, runId, context) {
9
+ return spentOf(await runEntries(store, runId, context));
10
+ }
11
+ /** What a run has spent (spentOverStore) and how its model requests were priced so far (metrics.ts costBasisOf), from one
12
+ * read of its journal; `basis` undefined while it booked no model request. */
13
+ export async function spendOverStore(store, runId, context) {
9
14
  const entries = await runEntries(store, runId, context);
15
+ const model = bookedSpend(entries, "model");
16
+ return { usd: spentOf(entries), basis: model.requests > 0 ? costBasisOf(model) : undefined };
17
+ }
18
+ /** spentOverStore over a run's journal `entries`. */
19
+ function spentOf(entries) {
10
20
  // A run whose record carries no cost of its own (a CLI harness's, a workflow's) reads what its sessions booked for the
11
21
  // requests that answered (metrics.ts bookedListSpend), its record's answers reading $0: 0 before any request, an
12
22
  // unknown booked spend unknown (null). A request still out is held, not spent: its hold never shows as spend.
@@ -16,6 +16,7 @@ import { reflectRanges } from "../runner/end-of-run.js";
16
16
  import { journalEntries, journalRecords, recordOf } from "../runner/journal-reader.js";
17
17
  import { endedAnswer, KINDS } from "../runner/journal-kinds.js";
18
18
  import { bookedSpend, cliModelWorked, costIsBooked, costWordsOf } from "../runner/metrics.js";
19
+ import { costOfRun } from "./runs.js";
19
20
  import { mcpNameOf, mcpToolName } from "../tool-names.js";
20
21
  import { Turns } from "../turns.js";
21
22
  /** A run's entries from the one store, in log order (by id across every conversation), as the old "SELECT record FROM
@@ -235,9 +236,34 @@ export async function runTimeline(store, runId, context, options = {}) {
235
236
  // the run's rows are rendered again with those names' catalog names, so each is labelled by its own.
236
237
  const hashed = hashedMcp(catalog?.values() ?? []);
237
238
  const rendered = hashed.size ? render(hashed) : first;
238
- const rows = booked ? bookedUsage(rendered, entries, options) : withFreeUsage(rendered, entries);
239
+ const priced = booked ? bookedUsage(rendered, entries, options) : withFreeUsage(rendered, entries);
240
+ // An imported run's usage row says the cost its run reads (costOfRun), never a sum of its transcript's unpriced requests.
241
+ const run = await store.runs.get(runId);
242
+ const rows = run?.importedFrom ? importedUsage(priced, run, options) : priced;
239
243
  return labelled(reflect, withComputerCalls(withLearnedNodes(await datedRows(store, runId, catalogNamed(rows, catalog)), entries, options.redact), entries, options.redact));
240
244
  }
245
+ /** An imported run's `rows` with its usage row in its run's cost (store-reads/runs.ts costOfRun): what its source
246
+ * recorded, else the catalog's estimate the import kept beside it, else unknown. The renderer's row sums the transcript's
247
+ * requests, which a source that priced the session as a whole (Claude Code) or not at all (Codex) left unpriced, so it
248
+ * read "$0 known, 4 not priced" beside a run whose cost is known. The catalog's estimate (the source recorded none, its
249
+ * stored cost null) is said as one, "$0.0016 est.", as the run's cost reads in the console; a recorded cost is said
250
+ * exact. A row that already says the run's recorded cost, or a free model's $0, is kept. */
251
+ function importedUsage(rows, run, options) {
252
+ const usd = costOfRun(run);
253
+ const estimated = run.costUsd === null && usd !== null;
254
+ const at = rows.findLastIndex((row) => row.kind === "usage");
255
+ const row = rows[at];
256
+ const requests = row?.usage?.requests;
257
+ if (!row?.usage || typeof requests !== "number")
258
+ return [...rows];
259
+ if (row.usage.usd === usd && row.usage.so_far_usd === undefined && (!estimated || row.usage.free === true))
260
+ return [...rows];
261
+ const spent = usd === null ? "unknown" : `${usd === 0 ? "$0" : `$${usd.toFixed(4)}`}${estimated ? " est." : ""}`;
262
+ const notMetered = row.usage.not_metered ?? 0;
263
+ const summary = `${costLabel(options.ended === true)}: ${spent} over ${requests} model request${requests === 1 ? "" : "s"}${toolCallWords(0, notMetered)}`;
264
+ const usage = { usd, requests, ...(notMetered ? { not_metered: notMetered } : {}) };
265
+ return rows.map((each, i) => (i === at ? { ...row, summary, usage } : each));
266
+ }
241
267
  /** The run's MCP tools and monid endpoints by the wire name its harness called each (catalog.ts wireName of each pinned
242
268
  * tool), read from the run's pins by lookup, never parsed back; undefined when no row names one by a wire name. */
243
269
  async function runCatalog(store, runId, rows) {
@@ -168,10 +168,14 @@ export declare function shadowSpendOf(store: {
168
168
  open: boolean;
169
169
  usd: number | null;
170
170
  }>;
171
- /** One run of the cost series, as number `n` of it (null for a replay): its figures (allInOf), its path, the workflow
171
+ /** One run of the cost series, as number `n` of it (null for a replay), `placed` its path and workflow as pathOfRow reads
172
+ * them off its record (agentCostOverStore has them): its figures (allInOf), the workflow
172
173
  * revision that ran it and whether an audit's shadow ran for it (agentCostOverStore's loop body); `took_over` is the
173
174
  * series' (tookOverOf). */
174
- export declare function costRunOf(store: RunRecords, run: RunRecord, n: number | null): Promise<Omit<CostRun, "took_over">>;
175
+ export declare function costRunOf(store: RunRecords, run: RunRecord, n: number | null, { path, workflow }: {
176
+ path: RunPath;
177
+ workflow: string | null;
178
+ }): Promise<Omit<CostRun, "took_over">>;
175
179
  /** The runs the money rule reads for agent `agentId` (runner/workflow-first.ts costLineOf), one set for the run's launch
176
180
  * and the cost page: of its newest PAST_RUNS runs, each that ended (ended, stopped or failed), is neither imported nor
177
181
  * archived nor a workflow revision's run, with its all-in cost (allInOf). `known`: all-in costs already read, by run id
@@ -195,6 +199,23 @@ export declare function runPathOverStore(store: Store, runId: string, started: R
195
199
  digest?: string;
196
200
  promotion?: number;
197
201
  }>;
202
+ /** The path a run's views say it took (GET /runs/{id} and each runs list row: `path`, and `workflow`), read off its record
203
+ * as the cost per run reads it (pathOfRow, revisionOfRow: costRunOf's own read, so the run views and GET
204
+ * .../learning/cost never disagree). `path` is null for a run not ended, a workflow revision's run, and a run that learns
205
+ * whose record names none; `workflow` names the learned workflow and its revision only on a run it served (path
206
+ * `workflow` or `handed_back`; the name only when its launch offered one, the digest null when its record names none,
207
+ * and whether its answer re-picked, repickOfRow), else null. */
208
+ export declare function pathViewOf(run: Pick<RunRecord, "metrics" | "state">, kind: string, started: RunLaunch["learning"]): {
209
+ path: RunPath | null;
210
+ workflow: {
211
+ name: string | null;
212
+ digest: string | null;
213
+ repick: Repick | null;
214
+ } | null;
215
+ };
216
+ /** How a served run's answer re-picked (learn/check.ts: a quote miss re-picks it once before it hands back): `found` when
217
+ * its second pass found its quotes, `missed` when it still missed them. */
218
+ export type Repick = "found" | "missed";
198
219
  /** What a run's claim laid of its learning seed: the learned workflows it was given, by name. */
199
220
  type Laid = {
200
221
  readonly workflows: readonly string[];
@@ -236,7 +236,7 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
236
236
  unknownPath += 1;
237
237
  continue;
238
238
  }
239
- rows.push(await costRunOf(records, run, JOB_PATHS.includes(placed.path) ? (jobs += 1) : null));
239
+ rows.push(await costRunOf(records, run, JOB_PATHS.includes(placed.path) ? (jobs += 1) : null, placed));
240
240
  }
241
241
  // Where each revision took over, over every run before the limit cuts the read (the first is the summary's takeover).
242
242
  const runs = tookOverOf(rows);
@@ -250,7 +250,8 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
250
250
  // summary's takeover: read from the run rows by their place, as the promotion feed is trimmed and its oldest record need
251
251
  // not be the first promotion), and each revision's compile and check over the served runs that carry it.
252
252
  const ledger = await agentLedgerOf(records, promotions, listed);
253
- const booked = billOf(runs);
253
+ const summary = costSummary(runs, replay);
254
+ const booked = billOf(runs, summary);
254
255
  // What no run carries is the booked total less every run's all-in (A8), so a revision whose records aged out of the
255
256
  // feed lands here, never nowhere: unknown while any run's figures are.
256
257
  let carried = 0;
@@ -260,7 +261,7 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
260
261
  }
261
262
  const unattributed = booked.withheld > 0 || carried === null ? null : noiseOff(booked.booked_usd - carried);
262
263
  const bill = { ...booked, revisions: ledger.revisions, unattributed_usd: unattributed, holds: holdsOf(promotions, runs) };
263
- return { runs: runs.slice(Math.max(0, runs.length - limit)), summary: costSummary(runs, replay), total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: unknownPath, workflows, bill };
264
+ return { runs: runs.slice(Math.max(0, runs.length - limit)), summary, total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: unknownPath, workflows, bill };
264
265
  }
265
266
  /** Each hold of the agent's learned workflows (its promotion records that say hold), placed on the series: at the first
266
267
  * run that does a job from it on, by its number among them (the points' `n`), null when none has run since. */
@@ -458,13 +459,11 @@ export async function shadowSpendOf(store, runId) {
458
459
  return { open: false, usd: 0 };
459
460
  return FINAL_STATES.includes(shadow.state) ? { open: false, usd: shadow.costUsd ?? null } : { open: true, usd: null };
460
461
  }
461
- /** One run of the cost series, as number `n` of it (null for a replay): its figures (allInOf), its path, the workflow
462
+ /** One run of the cost series, as number `n` of it (null for a replay), `placed` its path and workflow as pathOfRow reads
463
+ * them off its record (agentCostOverStore has them): its figures (allInOf), the workflow
462
464
  * revision that ran it and whether an audit's shadow ran for it (agentCostOverStore's loop body); `took_over` is the
463
465
  * series' (tookOverOf). */
464
- export async function costRunOf(store, run, n) {
465
- const started = await learningStartOfRun(store, run.id);
466
- // A run with no path named is placed as the agent's here, and left out by the series.
467
- const { path, workflow } = pathOfRow(run.metrics, started) ?? { path: "agent", workflow: null };
466
+ export async function costRunOf(store, run, n, { path, workflow }) {
468
467
  const { cost_usd, learning_usd, total_usd } = await allInOf(store, run);
469
468
  // The run row's stored bound (RunRow.costAtMostUsd, written with a null cost_usd): its cost counts a request cut at what its cap held.
470
469
  const cost_upper_bound = typeof run.costAtMostUsd === "number";
@@ -553,6 +552,28 @@ function pathOfRow(metrics, started) {
553
552
  const path = named ? m.path : "agent";
554
553
  return { path, workflow: path === "agent" || typeof m.workflow !== "string" ? null : offeredName(m.workflow, started?.workflows) };
555
554
  }
555
+ /** The path a run's views say it took (GET /runs/{id} and each runs list row: `path`, and `workflow`), read off its record
556
+ * as the cost per run reads it (pathOfRow, revisionOfRow: costRunOf's own read, so the run views and GET
557
+ * .../learning/cost never disagree). `path` is null for a run not ended, a workflow revision's run, and a run that learns
558
+ * whose record names none; `workflow` names the learned workflow and its revision only on a run it served (path
559
+ * `workflow` or `handed_back`; the name only when its launch offered one, the digest null when its record names none,
560
+ * and whether its answer re-picked, repickOfRow), else null. */
561
+ export function pathViewOf(run, kind, started) {
562
+ const placed = kind === "workflow" || !FINAL_STATES.includes(run.state) ? undefined : pathOfRow(run.metrics, started);
563
+ const served = placed && SERVED_PATHS.includes(placed.path);
564
+ return { path: placed?.path ?? null, workflow: served ? { name: placed.workflow, digest: revisionOfRow(run)?.digest ?? null, repick: repickOfRow(run) } : null };
565
+ }
566
+ /** Whether the workflow a run's record names re-picked its answer, from its metrics' loop rows (workflow-first.ts
567
+ * pathMetrics `loops`, LoopRow): its last re-pick loop (marked `repick`, learn/check.ts repickLabels) of that workflow
568
+ * that ran a second pass, found when its `until` held. Null when none did: an ordinary loop that ran twice is no
569
+ * re-pick, nor is a loop of another workflow the run called. */
570
+ function repickOfRow(row) {
571
+ const { workflow, loops } = (row.metrics ?? {});
572
+ if (typeof workflow !== "string" || !Array.isArray(loops))
573
+ return null;
574
+ const last = loops.findLast((loop) => loop?.repick === true && loop.workflow === workflow && typeof loop.iterations === "number" && loop.iterations >= 2);
575
+ return last ? (last.met === true ? "found" : "missed") : null;
576
+ }
556
577
  /** What a run's claims laid of the workflows it was offered (laidOf over its journal): read once for a settled run and kept
557
578
  * by its epoch (settledLaid), else read now. */
558
579
  async function journalLaidOf(store, run) {
@@ -1,5 +1,6 @@
1
1
  import type { Context } from "@earendil-works/chord/context";
2
2
  import { type ImportedFrom, type RunLabels, type RunRow, type Store } from "../store/store.js";
3
+ import { type CostBasis } from "../runner/metrics.js";
3
4
  /** A revision's kind, as the row names it. */
4
5
  export type RevisionKind = "agentic" | "workflow";
5
6
  /** The run state the v2 API serves, as the old store kept it: a non-terminal run is 'running' (the live view refines its
@@ -20,6 +21,9 @@ export interface RunRowView {
20
21
  /** An ended run's stored cost when that is the most it can have cost, not its cost (RunRow.costAtMostUsd): said "at
21
22
  * most", never summed as spend. Absent otherwise. */
22
23
  readonly costAtMostUsd?: number;
24
+ /** How its cost was reached (metrics.ts CostBasis): a run still going, by its booking so far; an ended one, as its end
25
+ * stored it (storedBasisOf). */
26
+ readonly costBasis: CostBasis;
23
27
  readonly summary?: string;
24
28
  readonly archivedAt?: string;
25
29
  readonly importedFrom?: ImportedFrom;
@@ -29,6 +33,11 @@ export interface RunRowView {
29
33
  * stopped (`kind`: its revision's), whose turn ended and never the run, as main's stop leaves one (runs.ts statusOf: a
30
34
  * stopped run is input_required and idle). A cancel, a workflow's stop and an imported run's read 'stopped'. */
31
35
  export declare function runReadState(run: RunRow, kind: RevisionKind): RunReadState;
36
+ /** An ended run's cost as every reader shows it (its read, the list, its usage row, the stats, analytics and usage report):
37
+ * its stored cost, else the catalog's estimate an import kept beside it (import.ts `metrics.costUsdEst`, basis catalog)
38
+ * for a session its source did not price. The stored column stays unknown, since no one recorded that amount; the
39
+ * estimate reads as the server's own figures do, an estimate. A recorded cost always wins; with neither, null. */
40
+ export declare function costOfRun(run: Pick<RunRow, "costUsd" | "metrics">): number | null;
32
41
  /** The runs, newest first; `agent`, `revision` and `labels` (every one held) narrow it. Archived runs are listed too (the
33
42
  * server filters them), with the archive time coalesced onto each row, and a not-ended run's cost its booked-so-far spend. */
34
43
  export declare function listRuns(store: Store, filter: {
@@ -1,5 +1,6 @@
1
1
  import { stoppedByPerson } from "../store/store.js";
2
- import { spentOverStore } from "./cap.js";
2
+ import { spendOverStore } from "./cap.js";
3
+ import { storedBasisOf } from "../runner/metrics.js";
3
4
  const isoOf = (ms) => new Date(ms).toISOString();
4
5
  /** A run's v2 state from A's state and its outcome: an escalation reads 'escalated', a plain completion 'completed';
5
6
  * stopped and failed are their own; queued, running and parked read 'running', and so does an agentic run a person
@@ -20,9 +21,19 @@ function stateOf(run, kind) {
20
21
  return "running"; // queued | running | parked
21
22
  }
22
23
  }
24
+ /** An ended run's cost as every reader shows it (its read, the list, its usage row, the stats, analytics and usage report):
25
+ * its stored cost, else the catalog's estimate an import kept beside it (import.ts `metrics.costUsdEst`, basis catalog)
26
+ * for a session its source did not price. The stored column stays unknown, since no one recorded that amount; the
27
+ * estimate reads as the server's own figures do, an estimate. A recorded cost always wins; with neither, null. */
28
+ export function costOfRun(run) {
29
+ if (run.costUsd !== null)
30
+ return run.costUsd;
31
+ const est = run.metrics?.costUsdEst;
32
+ return typeof est === "number" && Number.isFinite(est) && est >= 0 ? est : null;
33
+ }
23
34
  /** A run row as the server serves it, with its agent's archive time (for the coalesce), its revision's kind, and its cost
24
- * (the booked-so-far spend for a run not ended, else its stored cost). */
25
- function viewOf(run, agentArchivedAt, kind, cost) {
35
+ * (the booked-so-far spend for a run not ended, else costOfRun). */
36
+ function viewOf(run, agentArchivedAt, kind, cost, basis) {
26
37
  const archivedAt = run.archivedAt ?? agentArchivedAt;
27
38
  const state = stateOf(run, kind);
28
39
  // A run that reads running has no end to show: a person's stop keeps its time and words in the store, for its reopen
@@ -38,6 +49,7 @@ function viewOf(run, agentArchivedAt, kind, cost) {
38
49
  ...(over && run.endedAt !== null ? { endedAt: isoOf(run.endedAt) } : {}),
39
50
  ...(cost !== null ? { costUsd: cost } : {}),
40
51
  ...(over && run.costAtMostUsd !== null ? { costAtMostUsd: run.costAtMostUsd } : {}),
52
+ costBasis: basis ?? storedBasisOf(over ? run.metrics : undefined, cost ?? run.costAtMostUsd),
41
53
  ...(over && run.summary !== null ? { summary: run.summary } : {}),
42
54
  ...(archivedAt !== null ? { archivedAt: isoOf(archivedAt) } : {}),
43
55
  ...(run.importedFrom !== null ? { importedFrom: { source: run.importedFrom.source, session: run.importedFrom.session } } : {}),
@@ -54,9 +66,10 @@ function joins(store, context) {
54
66
  if (!kinds.has(run.revisionId))
55
67
  kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind ?? "agentic");
56
68
  // A run that has not ended shows what it has spent so far over the journal (spentOverStore: never the hold of a step
57
- // still running), since cost_usd is written only at the end; an ended run shows its stored cost.
58
- const cost = runReadState(run, kinds.get(run.revisionId)) === "running" ? await spentOverStore(store, run.id, context) : run.costUsd;
59
- return viewOf(run, agentArchived.get(run.agentId), kinds.get(run.revisionId), cost);
69
+ // still running), since cost_usd is written only at the end; an ended run shows its stored cost (costOfRun), its
70
+ // basis as its end stored it (an imported run's catalog estimate reads as an estimate: storedBasisOf).
71
+ const spend = runReadState(run, kinds.get(run.revisionId)) === "running" ? await spendOverStore(store, run.id, context) : { usd: costOfRun(run), basis: undefined };
72
+ return viewOf(run, agentArchived.get(run.agentId), kinds.get(run.revisionId), spend.usd, spend.basis);
60
73
  },
61
74
  };
62
75
  }
@@ -1,16 +1,19 @@
1
1
  import type { Store } from "../store/store.js";
2
2
  import { type RunReadState } from "./runs.js";
3
+ import { type CostBasis } from "../runner/metrics.js";
3
4
  /** The run states the stats count by, as the old store named them. */
4
5
  export type RunState = RunReadState;
5
6
  export type RunCounts = Record<RunState, number> & {
6
7
  total: number;
7
8
  };
8
9
  /** `usd` sums the `known` runs' costs (those that ended with one); null when there are none, never 0. A run still running,
9
- * or one that ended with no cost, is `unknown`. */
10
+ * or one that ended with no cost, is `unknown`. `basis`: how the summed costs were reached (metrics.ts joinedBasis over
11
+ * each one's stored basis, storedBasisOf). */
10
12
  export type CostSum = {
11
13
  usd: number | null;
12
14
  known: number;
13
15
  unknown: number;
16
+ basis: CostBasis;
14
17
  };
15
18
  export type StoreStats = {
16
19
  agents: number;
@@ -1,5 +1,6 @@
1
- import { runReadState } from "./runs.js";
1
+ import { costOfRun, runReadState } from "./runs.js";
2
2
  import { isShadowRun, shadowRunId } from "../learn/audit.js";
3
+ import { joinedBasis, storedBasisOf } from "../runner/metrics.js";
3
4
  const RUN_STATES = ["running", "completed", "failed", "stopped", "escalated"];
4
5
  const emptyCounts = () => ({ total: 0, running: 0, completed: 0, failed: 0, stopped: 0, escalated: 0 });
5
6
  /** Every agent, by name then id (the old order), with its archive and name. */
@@ -37,18 +38,24 @@ export async function stats(store) {
37
38
  }
38
39
  let g = shown.get(run.agentId);
39
40
  if (g === undefined)
40
- shown.set(run.agentId, (g = { counts: emptyCounts(), costUsd: 0, known: 0, shadowUsd: 0 }));
41
+ shown.set(run.agentId, (g = { counts: emptyCounts(), costUsd: 0, known: 0, shadowUsd: 0, bases: [] }));
41
42
  const shadow = shadows.get(shadowRunId(run.id));
42
- if (shadow && runReadState(shadow, "agentic") !== "running" && shadow.costUsd !== null)
43
+ if (shadow && runReadState(shadow, "agentic") !== "running" && shadow.costUsd !== null) {
43
44
  g.shadowUsd += shadow.costUsd;
45
+ g.bases.push(storedBasisOf(shadow.metrics, shadow.costUsd));
46
+ }
44
47
  g.counts.total += 1;
45
48
  g.counts[state] += 1;
46
- if (state !== "running" && run.costUsd !== null) {
47
- g.costUsd += run.costUsd;
49
+ // Its cost as its read says it (costOfRun): an imported session's catalog estimate when its source priced none.
50
+ const cost = costOfRun(run);
51
+ if (state !== "running" && cost !== null) {
52
+ g.costUsd += cost;
48
53
  g.known += 1;
54
+ g.bases.push(storedBasisOf(run.metrics, cost));
49
55
  }
50
56
  }
51
- const stats = { agents: 0, archivedAgents: 0, runs: emptyCounts(), archivedRuns: 0, cost: { usd: null, known: 0, unknown: 0 }, byAgent: [], running };
57
+ const stats = { agents: 0, archivedAgents: 0, runs: emptyCounts(), archivedRuns: 0, cost: { usd: null, known: 0, unknown: 0, basis: "unpriced" }, byAgent: [], running };
58
+ const bases = [];
52
59
  for (const agent of agents) {
53
60
  const g = shown.get(agent.id);
54
61
  const counts = g?.counts ?? emptyCounts();
@@ -68,6 +75,8 @@ export async function stats(store) {
68
75
  stats.cost.usd = (stats.cost.usd ?? 0) + (g?.costUsd ?? 0) + (g?.shadowUsd ?? 0);
69
76
  stats.cost.known += known;
70
77
  stats.cost.unknown += counts.total - known;
78
+ bases.push(...(g?.bases ?? []));
71
79
  }
80
+ stats.cost.basis = joinedBasis(bases);
72
81
  return stats;
73
82
  }