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

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 (224) hide show
  1. package/CHANGELOG.md +138 -3
  2. package/README.md +346 -99
  3. package/dist/adapters.d.ts +10 -0
  4. package/dist/adapters.js +46 -0
  5. package/dist/agent-package.d.ts +3 -3
  6. package/dist/agent-package.js +7 -14
  7. package/dist/agent-rows.d.ts +1 -1
  8. package/dist/agents-store.js +7 -6
  9. package/dist/api-only.d.ts +17 -6
  10. package/dist/api-only.js +22 -9
  11. package/dist/cli.js +11 -4
  12. package/dist/composio-made.d.ts +26 -1
  13. package/dist/composio-made.js +39 -2
  14. package/dist/composio-preset.d.ts +6 -4
  15. package/dist/composio-preset.js +50 -17
  16. package/dist/computer-held-routes.js +2 -3
  17. package/dist/computer-needs.d.ts +1 -1
  18. package/dist/computer-needs.js +1 -1
  19. package/dist/computer-setting.d.ts +8 -4
  20. package/dist/computer-setting.js +0 -2
  21. package/dist/computers-running.d.ts +8 -0
  22. package/dist/computers-running.js +27 -0
  23. package/dist/computers.d.ts +0 -2
  24. package/dist/computers.js +1 -1
  25. package/dist/console/assets/backend-D5EmB4N3.js +166 -0
  26. package/dist/console/assets/index-DUYLaqiR.js +746 -0
  27. package/dist/console/assets/index-DYhU4adm.css +1 -0
  28. package/dist/console/index.html +2 -2
  29. package/dist/docker-host-addresses.d.ts +23 -3
  30. package/dist/docker-host-addresses.js +90 -18
  31. package/dist/errors.d.ts +4 -0
  32. package/dist/errors.js +7 -0
  33. package/dist/exa-keyless.d.ts +2 -3
  34. package/dist/exa-keyless.js +43 -127
  35. package/dist/firecrawl-keyless.d.ts +41 -0
  36. package/dist/firecrawl-keyless.js +152 -0
  37. package/dist/github-connection.d.ts +50 -1
  38. package/dist/github-connection.js +96 -3
  39. package/dist/github-faux.d.ts +11 -2
  40. package/dist/github-faux.js +138 -7
  41. package/dist/github-pull-request.d.ts +37 -0
  42. package/dist/github-pull-request.js +110 -0
  43. package/dist/handed-secret.d.ts +3 -1
  44. package/dist/handed-secret.js +4 -2
  45. package/dist/harness-keys.d.ts +98 -0
  46. package/dist/harness-keys.js +247 -0
  47. package/dist/harness-recipe-entry.d.ts +14 -9
  48. package/dist/harness-recipe-entry.js +25 -14
  49. package/dist/harness-table.d.ts +16 -1
  50. package/dist/harness-table.js +19 -7
  51. package/dist/harness.d.ts +4 -0
  52. package/dist/harness.js +12 -1
  53. package/dist/host-check.d.ts +2 -0
  54. package/dist/host-check.js +6 -0
  55. package/dist/import-summary.d.ts +1 -1
  56. package/dist/import-summary.js +1 -1
  57. package/dist/import.d.ts +5 -5
  58. package/dist/import.js +54 -67
  59. package/dist/keyless-mcp.d.ts +36 -0
  60. package/dist/keyless-mcp.js +127 -0
  61. package/dist/learn/audit.d.ts +3 -3
  62. package/dist/learn/audit.js +3 -3
  63. package/dist/learn/commit.d.ts +8 -7
  64. package/dist/learn/commit.js +60 -20
  65. package/dist/learn/compile.d.ts +4 -3
  66. package/dist/learn/compile.js +7 -5
  67. package/dist/learn/folder.d.ts +16 -3
  68. package/dist/learn/folder.js +46 -13
  69. package/dist/learn/promote.d.ts +37 -13
  70. package/dist/learn/promote.js +112 -40
  71. package/dist/learn/secrets.js +10 -2
  72. package/dist/learning-routes.d.ts +22 -17
  73. package/dist/learning-routes.js +40 -39
  74. package/dist/learning.js +2 -2
  75. package/dist/live-reach.d.ts +34 -7
  76. package/dist/live-reach.js +76 -11
  77. package/dist/manifest.d.ts +10 -0
  78. package/dist/manifest.js +30 -0
  79. package/dist/mcp-pin-first.d.ts +4 -2
  80. package/dist/mcp-pin-first.js +5 -2
  81. package/dist/mcp-probe.js +2 -2
  82. package/dist/mcp-secret-writes.d.ts +16 -7
  83. package/dist/mcp-secret-writes.js +35 -27
  84. package/dist/mcp-servers-store.d.ts +30 -6
  85. package/dist/mcp-servers-store.js +112 -28
  86. package/dist/mcp-sources.d.ts +10 -2
  87. package/dist/mcp-sources.js +9 -2
  88. package/dist/mcp-store.d.ts +13 -7
  89. package/dist/mcp-store.js +12 -9
  90. package/dist/model-form.d.ts +3 -2
  91. package/dist/model-form.js +11 -7
  92. package/dist/model.d.ts +20 -4
  93. package/dist/model.js +68 -9
  94. package/dist/native/history.d.ts +1 -1
  95. package/dist/native/history.js +2 -2
  96. package/dist/ollama-provider.d.ts +92 -0
  97. package/dist/ollama-provider.js +495 -0
  98. package/dist/openapi/agentrun-v2.openapi.json +2 -2
  99. package/dist/openapi.js +235 -41
  100. package/dist/page.d.ts +11 -3
  101. package/dist/page.js +22 -7
  102. package/dist/place-offers.d.ts +17 -1
  103. package/dist/place-offers.js +46 -5
  104. package/dist/print.js +1 -1
  105. package/dist/purged-workflow.d.ts +35 -7
  106. package/dist/purged-workflow.js +73 -26
  107. package/dist/run-computer.d.ts +2 -0
  108. package/dist/run-computer.js +2 -0
  109. package/dist/run-files.d.ts +4 -0
  110. package/dist/run-files.js +4 -0
  111. package/dist/run-keys.d.ts +2 -4
  112. package/dist/run-keys.js +6 -7
  113. package/dist/run-launch.d.ts +4 -0
  114. package/dist/run-launch.js +4 -0
  115. package/dist/run-reads-over-store.d.ts +3 -2
  116. package/dist/run-reads-over-store.js +66 -2
  117. package/dist/run-secrets.js +5 -2
  118. package/dist/runner/adapter-loop.d.ts +6 -3
  119. package/dist/runner/adapter-loop.js +34 -15
  120. package/dist/runner/adapter-run-spec.d.ts +3 -0
  121. package/dist/runner/adapter-run-spec.js +6 -4
  122. package/dist/runner/audit.js +8 -4
  123. package/dist/runner/beside.d.ts +2 -1
  124. package/dist/runner/beside.js +6 -2
  125. package/dist/runner/claim-secrets.d.ts +5 -8
  126. package/dist/runner/claim-secrets.js +6 -0
  127. package/dist/runner/cli-run-spec.d.ts +1 -1
  128. package/dist/runner/cli-run-spec.js +39 -7
  129. package/dist/runner/harness-loops.d.ts +22 -9
  130. package/dist/runner/harness-loops.js +21 -11
  131. package/dist/runner/held.d.ts +3 -0
  132. package/dist/runner/learning-end.d.ts +1 -1
  133. package/dist/runner/learning-end.js +8 -3
  134. package/dist/runner/loop-contract.d.ts +5 -4
  135. package/dist/runner/main.js +4 -2
  136. package/dist/runner/mcp-claim.d.ts +3 -0
  137. package/dist/runner/mcp-claim.js +26 -2
  138. package/dist/runner/mcp-run.d.ts +3 -0
  139. package/dist/runner/mcp-run.js +2 -1
  140. package/dist/runner/metrics.d.ts +10 -8
  141. package/dist/runner/metrics.js +20 -27
  142. package/dist/runner/model-route.d.ts +3 -0
  143. package/dist/runner/model-route.js +6 -2
  144. package/dist/runner/pr-checkout.d.ts +21 -4
  145. package/dist/runner/pr-checkout.js +103 -4
  146. package/dist/runner/promotion.d.ts +15 -9
  147. package/dist/runner/promotion.js +25 -15
  148. package/dist/runner/run-child-host.js +24 -11
  149. package/dist/runner/run-child.js +3 -1
  150. package/dist/runner/runner-check.d.ts +3 -0
  151. package/dist/runner/runner-check.js +23 -0
  152. package/dist/runner/sdk-loop.js +2 -0
  153. package/dist/runner/workflow-loop.d.ts +2 -2
  154. package/dist/runner/workflow-loop.js +1 -2
  155. package/dist/runner/workflow-spec.d.ts +1 -1
  156. package/dist/runner/workflow-spec.js +6 -6
  157. package/dist/sdk/check.d.ts +4 -1
  158. package/dist/sdk/check.js +2 -1
  159. package/dist/sdk/runs.d.ts +2 -2
  160. package/dist/sdk/runs.js +8 -6
  161. package/dist/secret-cut.js +2 -1
  162. package/dist/secret-writes.d.ts +3 -2
  163. package/dist/secret-writes.js +1 -1
  164. package/dist/server.d.ts +55 -8
  165. package/dist/server.js +363 -118
  166. package/dist/standard-webhooks.d.ts +11 -0
  167. package/dist/standard-webhooks.js +20 -2
  168. package/dist/store/agent-body.d.ts +2 -2
  169. package/dist/store/agent-body.js +2 -2
  170. package/dist/store/index.d.ts +1 -1
  171. package/dist/store/schema.js +5 -2
  172. package/dist/store/sqlite/store.js +90 -34
  173. package/dist/store/store.d.ts +54 -14
  174. package/dist/store-reads/journal.d.ts +7 -2
  175. package/dist/store-reads/journal.js +9 -6
  176. package/dist/store-reads/learning.d.ts +12 -9
  177. package/dist/store-reads/learning.js +18 -24
  178. package/dist/store-view.d.ts +0 -5
  179. package/dist/store-view.js +9 -8
  180. package/dist/suggestions.d.ts +282 -0
  181. package/dist/suggestions.js +919 -0
  182. package/dist/telemetry.d.ts +8 -1
  183. package/dist/telemetry.js +12 -4
  184. package/dist/template-eval.d.ts +49 -4
  185. package/dist/template-eval.js +168 -45
  186. package/dist/template-import.d.ts +17 -0
  187. package/dist/template-import.js +36 -2
  188. package/dist/templates.d.ts +83 -76
  189. package/dist/templates.js +29 -48
  190. package/dist/timeline/labels.d.ts +2 -2
  191. package/dist/timeline/labels.js +2 -2
  192. package/dist/timeline/timeline.d.ts +2 -2
  193. package/dist/timeline/timeline.js +1 -1
  194. package/dist/tool-plane.d.ts +17 -9
  195. package/dist/tool-plane.js +38 -17
  196. package/dist/tools.js +2 -2
  197. package/dist/trigger-launch-hook.d.ts +19 -9
  198. package/dist/trigger-launch-hook.js +35 -25
  199. package/dist/trigger-targets.d.ts +18 -8
  200. package/dist/trigger-targets.js +42 -19
  201. package/dist/triggers.d.ts +85 -23
  202. package/dist/triggers.js +303 -96
  203. package/dist/validate.d.ts +1 -1
  204. package/dist/validate.js +2 -2
  205. package/dist/web-fake.js +3 -3
  206. package/dist/web-fetch-direct.d.ts +4 -0
  207. package/dist/web-fetch-direct.js +8 -1
  208. package/dist/web-fetch.d.ts +12 -3
  209. package/dist/web-fetch.js +47 -14
  210. package/dist/web-search.d.ts +33 -4
  211. package/dist/web-search.js +100 -41
  212. package/dist/webhooks.js +4 -5
  213. package/dist/workspace/synced-folder.d.ts +1 -1
  214. package/dist/workspace/synced-folder.js +11 -14
  215. package/launcher/agentrun-serve +5 -1
  216. package/package.json +17 -13
  217. package/templates/README.md +5 -4
  218. package/templates/agent-scout/agent.md +46 -0
  219. package/templates/agent-scout/skills/scout/SKILL.md +88 -0
  220. package/templates/agent-scout/skills/scout/suggestions.schema.json +112 -0
  221. package/dist/console/assets/index-BFgyZzF_.js +0 -734
  222. package/dist/console/assets/index-DbilxljH.css +0 -1
  223. package/dist/import-sources.d.ts +0 -116
  224. package/dist/import-sources.js +0 -693
@@ -207,17 +207,7 @@ export async function agentLearningOverStore(store, agentId) {
207
207
  }
208
208
  return { counts, impact: learningImpact(rows, (row) => learnings.get(row.id) ?? null) };
209
209
  }
210
- /** Agent `agentId`'s cost per run from the one store (GET /agents/{id}/learning/cost, learning-cost.ts): each of its
211
- * agentic runs that ended (ended, stopped or failed), oldest first, each run that does a job numbered from 1 (a replay
212
- * none: the bill's numbering), with its own cost and what learning added for it (runCostOf over its row's cost and its
213
- * learning's spend, learningSpendOf), the path it took and the learned workflow it ran (its run record's, pathOfRow), the
214
- * revision of that workflow (metrics.workflow_digest) and whether an audit's shadow ran for it; and what
215
- * the series shows (costSummary). A run still at work, an imported or archived one and a workflow revision's run are left
216
- * out. A run that learns whose record names no path (its end's patch was late or failed, or no runner wrote its end) is
217
- * left out too and counted (`unknown_path_runs`): its path and cost are not known, never `agent` at $0. `limit`: the
218
- * latest runs shown, each keeping its number; the summary is over every run; `bill` is the owner's bill over all of
219
- * them (billOf, with the revision ledger: agentLedgerOf). */
220
- export async function agentCostOverStore(store, agentId, limit = Infinity) {
210
+ export async function agentCostOverStore(store, agentId, limit = Infinity, refused) {
221
211
  const kinds = new Map();
222
212
  const listed = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId);
223
213
  // Every run's launch and learning in a few reads (agentRecordsOf), never a read a run.
@@ -248,12 +238,13 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
248
238
  // shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
249
239
  const workflows = workflowCostsOf(await pastRunsOverStore(store, agentId, new Map(runs.map((run) => [run.run_id, run.total_usd])), records));
250
240
  const promotions = await promotionsOf(store.settings, agentId);
251
- const replay = servingReplayOf(promotions, runs);
241
+ const skip = refused ? await refused(agentId, promotions) : undefined;
242
+ const replay = servingReplayOf(promotions, runs, skip);
252
243
  // The owner's bill: booked $, every run of the series (billOf), from the series' first run on a served path (the
253
244
  // summary's takeover: read from the run rows by their place, as the promotion feed is trimmed and its oldest record need
254
245
  // not be the first promotion), and each revision's compile and check over the served runs that carry it: runs on no
255
246
  // named disk alone, as a run on one learns nothing and serves nothing (learn/learning-of.ts).
256
- const ledger = await agentLedgerOf(records, promotions, listed.filter((run) => run.disk === null), await resetLedgerOf(store.settings, agentId));
247
+ const ledger = await agentLedgerOf(records, promotions, listed.filter((run) => run.disk === null), await resetLedgerOf(store.settings, agentId), skip);
257
248
  const summary = costSummary(runs, replay);
258
249
  const booked = billOf(runs, summary);
259
250
  // What no run carries is the booked total less every run's all-in (A8), so a revision whose records aged out of the
@@ -316,7 +307,7 @@ function allInV2Of(run, compiles, shares) {
316
307
  }
317
308
  /** The agent's revision ledger (revisionLedgerOf) over `listed` (its runs, as runs.list read them): its ended runs that do
318
309
  * a job or check one (no shadow, no imported or archived run), and the compile turns their rounds asked, by run. */
319
- async function agentLedgerOf(records, promotions, listed, reset) {
310
+ async function agentLedgerOf(records, promotions, listed, reset, refused) {
320
311
  const rows = listed.filter((one) => FINAL_STATES.includes(one.state) && !one.importedFrom && one.archivedAt === null && !isShadowRun(one.id));
321
312
  const compiles = new Map();
322
313
  for (const row of rows) {
@@ -324,7 +315,7 @@ async function agentLedgerOf(records, promotions, listed, reset) {
324
315
  if (asked.length)
325
316
  compiles.set(row.id, asked);
326
317
  }
327
- return { ...revisionLedgerOf(promotions, rows, [...compiles.values()].flat(), reset), compiles };
318
+ return { ...revisionLedgerOf(promotions, rows, [...compiles.values()].flat(), reset, refused), compiles };
328
319
  }
329
320
  /** Each revision of the agent's learned workflows (one per workflow and digest its promotion records name, in the order
330
321
  * they were first recorded) with its learning spend, and what its served runs carry of it (amortize), over `rows`: the
@@ -336,8 +327,9 @@ async function agentLedgerOf(records, promotions, listed, reset) {
336
327
  * (the bill's unattributed does). It is spread over its first K served runs (`workflow` or `handed_back` of its workflow
337
328
  * at its digest), K its check's compile_after; `state` from its newest record. A revision an earlier reset of the agent's
338
329
  * learning deleted (`reset`, learn/promote.ts keepResetLedger) is one too, first, stopped, at the K it was kept with,
339
- * unless the records name it again. */
340
- export function revisionLedgerOf(records, rows, compiles = [], reset = []) {
330
+ * unless the records name it again. Serving is the champion's (championOf), skipping what `refused` names (the purge
331
+ * gate's), so a purged run's revision is never said to serve. */
332
+ export function revisionLedgerOf(records, rows, compiles = [], reset = [], refused) {
341
333
  const ordered = [...rows].sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
342
334
  const newest = new Map();
343
335
  for (const record of records)
@@ -352,7 +344,7 @@ export function revisionLedgerOf(records, rows, compiles = [], reset = []) {
352
344
  };
353
345
  const revisions = [
354
346
  ...reset.filter((one) => !newest.has(`${one.workflow}\0${one.digest}`)).map((one) => spendOf(one.workflow, one.digest, one.k ?? COMPILE_AFTER_DEFAULT, "stopped")),
355
- ...[...newest.values()].map((record) => spendOf(record.workflow, record.digest, record.check?.compile_after ?? COMPILE_AFTER_DEFAULT, record.decided === "replaying" ? "checking" : championOf(records, record.workflow)?.digest === record.digest ? "serving" : "stopped")),
347
+ ...[...newest.values()].map((record) => spendOf(record.workflow, record.digest, record.check?.compile_after ?? COMPILE_AFTER_DEFAULT, record.decided === "replaying" ? "checking" : championOf(records, record.workflow, refused)?.digest === record.digest ? "serving" : "stopped")),
356
348
  ];
357
349
  const served = ordered.flatMap((row) => {
358
350
  const at = revisionOfRow(row);
@@ -369,8 +361,8 @@ export function revisionLedgerOf(records, rows, compiles = [], reset = []) {
369
361
  * spend is on the runs it serves, so its `learning_usd` and `all_in_usd` are 0, and the sum of every run's `all_in_usd`
370
362
  * and the bill's `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null;
371
363
  * null while unknown (its shadow still at work, a compile's or a check's cost unknown), never $0. Undefined for an
372
- * unknown run. */
373
- export async function runAllInOverStore(store, runId, rowUsd) {
364
+ * unknown run. Its revision's champion skips what `refused` names of the agent's promotion records (agentCostOverStore). */
365
+ export async function runAllInOverStore(store, runId, rowUsd, refused) {
374
366
  const run = await store.runs.get(runId);
375
367
  if (!run)
376
368
  return undefined;
@@ -389,7 +381,9 @@ export async function runAllInOverStore(store, runId, rowUsd) {
389
381
  if (path !== null && SERVED_PATHS.includes(path) && revisionOfRow(run)) {
390
382
  // Over the agent's runs on no named disk, as the cost page's ledger reads (learn/learning-of.ts).
391
383
  const listed = (await store.runs.list({ agentId: run.agentId, disk: null })).filter((one) => one.agentId === run.agentId);
392
- shares = (await agentLedgerOf(await agentRecordsOf(store, run.agentId, listed), await promotionsOf(store.settings, run.agentId), listed, await resetLedgerOf(store.settings, run.agentId))).shares;
384
+ const promotions = await promotionsOf(store.settings, run.agentId);
385
+ const skip = refused ? await refused(run.agentId, promotions) : undefined;
386
+ shares = (await agentLedgerOf(await agentRecordsOf(store, run.agentId, listed), promotions, listed, await resetLedgerOf(store.settings, run.agentId), skip)).shares;
393
387
  }
394
388
  const figures = { run_id: run.id, ...cost };
395
389
  const compiles = await compilesOf(store, run.id);
@@ -399,10 +393,10 @@ export async function runAllInOverStore(store, runId, rowUsd) {
399
393
  * workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
400
394
  * (the share of the inputs it ran on that said what the agent said, against the agent's own 100%). `cleared`: it passed
401
395
  * (a champion is promoted), with the agreement check it passed (`agreed` of `alone`, its bar `agree_min`). Null when no run was served, nothing serves now (held, or never promoted), or it was promoted
402
- * on no input (a dry run). */
403
- export function servingReplayOf(records, runs) {
396
+ * on no input (a dry run). The champion skips what `refused` names (the purge gate's). */
397
+ export function servingReplayOf(records, runs, refused) {
404
398
  const name = runs.findLast((run) => (run.path === "workflow" || run.path === "handed_back") && run.workflow !== null)?.workflow;
405
- const check = name ? championOf(records, name)?.check : undefined;
399
+ const check = name ? championOf(records, name, refused)?.check : undefined;
406
400
  if (!check?.n)
407
401
  return null;
408
402
  const agreed = check.agreed ?? 0, alone = check.alone ?? 0;
@@ -106,11 +106,6 @@ interface StoreRun {
106
106
  /** What the run's decisions booked (their own session, metrics.ts bookedSpend), undefined when it booked none: the run's cost
107
107
  * adds it to its usage row's, as the timeline adds their receipts. */
108
108
  readonly decisions: BookedSpend | undefined;
109
- /** Its model requests billed to a ChatGPT plan (metrics.ts planSpend), undefined when it made none. */
110
- readonly plan: {
111
- readonly requests: number;
112
- readonly tokens: number;
113
- } | undefined;
114
109
  /** What its model requests booked (metrics.ts bookedSpend "model": every session but its paid tools' and its
115
110
  * decisions'): their tokens and how they were priced, for its cost's `tokens` and `basis`. */
116
111
  readonly model: BookedSpend;
@@ -22,7 +22,7 @@ import { momentOf, pendingOf, stoppedTurn, approvalOf, asksOf, answersOf, active
22
22
  import { reflects } from "./reflect-range.js";
23
23
  import { freeCeilingMessage, overCapMessage, SPEND_CAP_REACHED } from "@parcha/agentrun-kernel/kernel/cap";
24
24
  import { booksDecisions, decisionsOverStore, parallelChargedDecisions } from "./store-reads/decisions.js";
25
- import { bookedDecisionsOf, bookedSpend, cliModelWorked, costBasisOf, costWordsOf, endCostOf, planSpend, runCostOf, storedBasisOf } from "./runner/metrics.js";
25
+ import { bookedDecisionsOf, bookedSpend, cliModelWorked, costBasisOf, costWordsOf, endCostOf, runCostOf, storedBasisOf } from "./runner/metrics.js";
26
26
  import { journalRecords, recordOf } from "./runner/journal-reader.js";
27
27
  import { capNowOf, KINDS } from "./runner/journal-kinds.js";
28
28
  import { laidOf, pathViewOf, roundsOf, runLearningEnd, usedOnly } from "./store-reads/learning.js";
@@ -259,7 +259,7 @@ async function storeRunIn(store, runId, deps, cut, since) {
259
259
  const terminal = row.state !== "running";
260
260
  // Its structured answer, from the final answer as the journal keeps it: never the served text, which a held value
261
261
  // (a number among them) or a long answer's cut would have changed, so a block that meets the schema reads as written.
262
- const rawAnswer = outputSchema !== undefined && terminal ? answerOf(await runTimeline(store, runId, context, { settled: true, caps: { text: Number.POSITIVE_INFINITY } })) : null;
262
+ const rawAnswer = outputSchema !== undefined && terminal ? answerOf(await runTimeline(store, runId, context, { settled: true, caps: { text: Number.POSITIVE_INFINITY } }, records)) : null;
263
263
  const structured = outputSchema !== undefined && terminal ? (rawAnswer !== null ? structuredAnswer(rawAnswer, outputSchema) : { why: "the run gave no final answer" }) : undefined;
264
264
  // The timeline, then the waiting row a run asking a question shows (withAsks), as the file path's rows carried it, so a
265
265
  // run waiting on a question reads the same status and pending.
@@ -267,7 +267,9 @@ async function storeRunIn(store, runId, deps, cut, since) {
267
267
  // A run that ended reads settled, and its usage row says its cost ("Cost: …"), never what it has cost so far. One
268
268
  // looking back is still going: its look-back's rows come yet.
269
269
  const timelineOptions = { settled: terminal && !lookingBack, ended: terminal && !lookingBack, redact: (text) => redacted(text, values) };
270
- const timeline = withAsks(await wholeAnswers(store, runId, context, await runTimeline(store, runId, context, timelineOptions), timelineOptions, wholeAnswerFor(values), since));
270
+ // Rendered from the records read above, so its usage row and the cost's `plan` (bookedSpend over the same entries) are
271
+ // of one read.
272
+ const timeline = withAsks(await wholeAnswers(store, runId, context, await runTimeline(store, runId, context, timelineOptions, records), timelineOptions, wholeAnswerFor(values), since));
271
273
  const revisions = await store.agents.revisions(row.agent);
272
274
  const version = revisions.findIndex((r) => r.id === row.revision) + 1;
273
275
  const ended = await runLearningEnd(store, runId);
@@ -407,8 +409,6 @@ async function storeRunIn(store, runId, deps, cut, since) {
407
409
  // What its decisions booked (their own session, never on the usage row, which counts its model requests and paid tools).
408
410
  const booked = bookedSpend(records.entries, "decisions");
409
411
  const decisions = booked.requests ? booked : undefined;
410
- const onPlan = planSpend(records.entries);
411
- const plan = onPlan.requests ? onPlan : undefined;
412
412
  const model = bookedSpend(records.entries, "model");
413
413
  // An ended run's booking by the one rule its end stored its cost by (metrics.ts runCostOf), read in the same snapshot as
414
414
  // its state: its so-far figures beside a stored unknown (costOfView reads the figure itself from the stored column). A
@@ -422,7 +422,7 @@ async function storeRunIn(store, runId, deps, cut, since) {
422
422
  const endSaidCost = endCost && !lookingBack ? endSaid !== undefined : false;
423
423
  const endUnread = !lookingBack && bookedCost !== undefined && !endCost && endSaid === null;
424
424
  const heal = lookingBack ? undefined : healOf(endCost, runRow);
425
- return { ...(cli_learning ? { cli_learning } : {}), ...(sdk ? { sdk } : {}), ...(native ? { native } : {}), row, runRow, launch, rows, version, harness, learningEnd: shownEnd, shownEnd, view, unshown, values, live, updatedAtMs, lastProgressAt, budget, paused, browserAsk, computerAsk, approvalsMode, output, ...(review ? { review } : {}), structured, decisions, plan, model, windDown, ...(endCost ? { endCost, endSaidCost } : {}), ...(endUnread ? { endUnread } : {}), ...(heal ? { heal } : {}), ...(lookingBack ? { lookingBack: true } : {}) };
425
+ return { ...(cli_learning ? { cli_learning } : {}), ...(sdk ? { sdk } : {}), ...(native ? { native } : {}), row, runRow, launch, rows, version, harness, learningEnd: shownEnd, shownEnd, view, unshown, values, live, updatedAtMs, lastProgressAt, budget, paused, browserAsk, computerAsk, approvalsMode, output, ...(review ? { review } : {}), structured, decisions, model, windDown, ...(endCost ? { endCost, endSaidCost } : {}), ...(endUnread ? { endUnread } : {}), ...(heal ? { heal } : {}), ...(lookingBack ? { lookingBack: true } : {}) };
426
426
  }
427
427
  /** Run read `read` (a run still going, as the store holds it) as the end `ended` keeps (its state, summary and end time:
428
428
  * the end a run looking back keeps), its cost as read. */
@@ -481,8 +481,9 @@ function costOfView(run) {
481
481
  const upper = words.upper_bound ? { upper_bound: true } : undefined;
482
482
  // A request billed to a person's plan costs no dollars, however it ended (the kernel's turnCostOf): every reader counts it
483
483
  // $0, so it is in no unpriced count; what it used on the plan is said beside the figure, and which plan: a run on the
484
- // login's harness was on Claude Code's login (claude-login.ts), any other on the ChatGPT sign-in.
485
- const plan = run.plan;
484
+ // login's harness was on Claude Code's login (claude-login.ts), any other on the ChatGPT sign-in. Read with its model
485
+ // requests (bookedSpend "model"): a request booked twice is one, as it is one request.
486
+ const plan = run.model.plan.requests ? run.model.plan : undefined;
486
487
  const on = run.harness === LOGIN_HARNESS ? "claude_code" : "chatgpt";
487
488
  // Beside its figure: while it is unknown, what is known so far; at $0 with every model request on a free model (and no
488
489
  // decision of its own), that it is free.
@@ -0,0 +1,282 @@
1
+ import { type InputIssue } from "./errors.js";
2
+ import type { GitHubConnectionView, RepoInstallation } from "./github-connection.js";
3
+ import { type NewFile } from "./github-pull-request.js";
4
+ import type { Settings } from "./store/index.js";
5
+ import type { UsageEventName } from "./telemetry.js";
6
+ import { type Json } from "./validate.js";
7
+ /** The bundled template a scan runs. */
8
+ export declare const SCOUT_TEMPLATE = "agent-scout";
9
+ /** Where in the repository a created agent's package goes: `<dir>/<key>/agent.md`. */
10
+ export declare const PACKAGE_DIR = ".agentrun/agents";
11
+ /** What the token a pull request is made with holds, and no more: one repository, these permissions. */
12
+ export declare const PULL_PERMISSIONS: {
13
+ readonly contents: "write";
14
+ readonly pull_requests: "write";
15
+ readonly metadata: "read";
16
+ };
17
+ /** A create's claim on its suggestion, kept on its row in the shared store: one older than this was left by a server
18
+ * that stopped mid-create, and the next create takes it over. A create's GitHub calls and apply take seconds. */
19
+ export declare const CREATE_CLAIM_MS: number;
20
+ /** A scan's start, kept on its repository's row while its run is started: one older than this was left by a server that
21
+ * stopped mid-start, and the next scan takes it over. */
22
+ export declare const SCAN_CLAIM_MS: number;
23
+ declare const INPUT_TYPES: readonly ["string", "number", "integer", "boolean"];
24
+ declare const OUTPUT_TYPES: readonly ["string", "number", "integer", "boolean", "string_list"];
25
+ declare const LANGUAGES: {
26
+ readonly typescript: readonly [".ts", ".mts", ".cts"];
27
+ readonly javascript: readonly [".mjs", ".cjs"];
28
+ readonly python: readonly [".py"];
29
+ readonly other: readonly [".md"];
30
+ };
31
+ declare const TIERS: readonly ["fast", "balanced", "strong"];
32
+ declare const IMPACTS: readonly ["high", "medium", "low"];
33
+ export type Language = keyof typeof LANGUAGES;
34
+ export type InputField = {
35
+ name: string;
36
+ type: (typeof INPUT_TYPES)[number];
37
+ description: string;
38
+ required: boolean;
39
+ };
40
+ export type OutputField = {
41
+ name: string;
42
+ type: (typeof OUTPUT_TYPES)[number];
43
+ description: string;
44
+ enum?: string[];
45
+ };
46
+ export type Line = {
47
+ path: string;
48
+ line: number;
49
+ quote: string;
50
+ };
51
+ /** One suggestion, as checked here from the scout's answer. */
52
+ export type Suggestion = {
53
+ key: string;
54
+ title: string;
55
+ summary: string;
56
+ why: string;
57
+ impact: (typeof IMPACTS)[number];
58
+ evidence: Line[];
59
+ agent: {
60
+ name: string;
61
+ description: string;
62
+ instructions: string;
63
+ inputs: InputField[];
64
+ output: OutputField[];
65
+ tools: string[];
66
+ tier: (typeof TIERS)[number];
67
+ };
68
+ integration: {
69
+ language: Language;
70
+ path: string;
71
+ call_site: {
72
+ path: string;
73
+ line: number;
74
+ note: string;
75
+ };
76
+ };
77
+ };
78
+ export type Status = "open" | "discarded" | "created";
79
+ /** A suggestion as it is kept: what the scan found, where, and what a person decided. */
80
+ export type Item = {
81
+ id: string;
82
+ repo: string;
83
+ key: string;
84
+ status: Status;
85
+ suggestion: Suggestion;
86
+ scan: {
87
+ run_id: string;
88
+ commit: string;
89
+ at: number;
90
+ };
91
+ decided_at?: number;
92
+ created?: {
93
+ agent_id: string;
94
+ template_id: string;
95
+ branch?: string;
96
+ commit?: string;
97
+ pull_request?: {
98
+ number: number;
99
+ url: string;
100
+ };
101
+ pull_error?: string;
102
+ };
103
+ /** A create in flight, on any server sharing the store: `by` is that create's own id. Taken off when it ends. */
104
+ creating?: Claim;
105
+ /** The branch a create pushed, kept before it makes the agent: a create after a failed write finds the agent made
106
+ * from that template at that commit and reuses it, or deletes the branch when none was made. */
107
+ pushed?: {
108
+ branch: string;
109
+ commit: string;
110
+ template_id: string;
111
+ base: string;
112
+ };
113
+ };
114
+ /** A claim on a row in the shared store: when it was taken, and by which request (a random id of its own). */
115
+ export type Claim = {
116
+ at: number;
117
+ by: string;
118
+ };
119
+ /** A scan as it is kept: its run, and what was read of it once it ended. */
120
+ export type Scan = {
121
+ repo: string;
122
+ run_id: string;
123
+ agent_id: string;
124
+ started_at: number;
125
+ read?: {
126
+ status: "completed" | "failed" | "cancelled";
127
+ at: number;
128
+ app?: string;
129
+ commit?: string;
130
+ found: number;
131
+ dropped: string[];
132
+ skipped: number;
133
+ error?: string;
134
+ };
135
+ /** A new scan of the repository being started, on any server sharing the store; the row is that scan's once it has. */
136
+ starting?: Claim;
137
+ /** A reader writing this scan's suggestions, on any server sharing the store; taken off when it marks the scan read. */
138
+ settling?: Claim;
139
+ };
140
+ /** A run of the scout, as its view says. */
141
+ export type ScanRun = {
142
+ status: string;
143
+ output?: unknown;
144
+ error?: string;
145
+ endedAt?: number;
146
+ };
147
+ export type SuggestionsDeps = {
148
+ readonly settings: Pick<Settings, "get" | "put" | "putIfAbsent" | "replaceIf" | "removeIf" | "all">;
149
+ readonly github: {
150
+ view(): GitHubConnectionView | undefined;
151
+ settingsUrl(): string | undefined;
152
+ readonly api: string;
153
+ repoInstallation(repo: string, signal?: AbortSignal): Promise<RepoInstallation | {
154
+ unusable: string;
155
+ reason: "not_connected" | "not_installed" | "github_failed";
156
+ }>;
157
+ tokenWith(id: number, repo: string, permissions: Record<string, "read" | "write">, signal?: AbortSignal): Promise<{
158
+ token: string;
159
+ expiresAt: number;
160
+ } | {
161
+ unusable: string;
162
+ }>;
163
+ revokeToken(token: string): Promise<void>;
164
+ };
165
+ /** The scout agent's id: one agent-scout made here and not archived, else a new one applied from the bundled package. */
166
+ scoutAgent(): Promise<string>;
167
+ /** A run of the scout agent started with `content`: its id. */
168
+ startScan(agentId: string, content: string): Promise<string>;
169
+ /** A run's state; undefined when there is no such run. */
170
+ readRun(runId: string): Promise<ScanRun | undefined>;
171
+ /** A scan's run cancelled: one that started but whose scan could not be kept, so nothing would ever read it. */
172
+ cancelScan(runId: string): Promise<void>;
173
+ /** The package (its files, under the id and origin given) made into an agent here: applied, then kept beside the other
174
+ * imported templates. Throws what the apply refuses (a missing model key, say), with nothing made. */
175
+ makeAgent(id: string, origin: {
176
+ source: string;
177
+ sha: string;
178
+ publisher: string;
179
+ }, files: ReadonlyMap<string, Buffer>): Promise<{
180
+ agentId: string;
181
+ warnings: InputIssue[];
182
+ }>;
183
+ /** The agent made here from the imported template `id` at commit `sha`, not archived; undefined when there is none. */
184
+ agentFrom(id: string, sha: string): Promise<string | undefined>;
185
+ /** This server's public URL, which the pull request names as AGENTRUN_URL. */
186
+ readonly publicUrl?: string;
187
+ /** Told each time a scan starts, a suggestion is discarded and a suggested agent's pull request opens: the anonymous
188
+ * counts' `used` (telemetry.ts), which hears the event's name and nothing else. It never fails the request. */
189
+ readonly used?: (event: UsageEventName) => void;
190
+ readonly now?: () => number;
191
+ };
192
+ /** A suggestion's id: the same repository and key always give the same one, so a decision outlives every rescan. */
193
+ export declare const suggestionId: (repo: string, key: string) => string;
194
+ /** A path in a repository: relative, in forward slashes, with no `.` or `..` part, at most 300, and no character that
195
+ * ends a line in any language a client is written in: no control character (C0 or C1), no U+2028 or U+2029. */
196
+ export declare function repoPath(value: unknown): string | undefined;
197
+ /** One suggestion of the scout's answer, checked whole; or why it is dropped. */
198
+ export declare function checkSuggestion(raw: unknown): Suggestion | string;
199
+ /** The scout's answer read: what the app is, the commit it read, its suggestions that check (at most five, keys
200
+ * distinct), and why each other one was dropped; or why the answer itself is not one. */
201
+ export declare function readScoutAnswer(output: unknown, repo: string): {
202
+ app: string;
203
+ commit: string;
204
+ suggestions: Suggestion[];
205
+ dropped: string[];
206
+ } | {
207
+ invalid: string;
208
+ };
209
+ /** The environment variable that names a created agent's id in the app. */
210
+ export declare const agentVariable: (key: string) => string;
211
+ /** The answer's JSON Schema, from its fields. */
212
+ export declare function outputSchema(s: Suggestion): Json;
213
+ /** The agent's template package, as files from the package's own folder: agent.md and output.schema.json. */
214
+ export declare function packageFiles(s: Suggestion, name?: string): Map<string, Buffer>;
215
+ /** Whether the package checks as the server checks a bundled or imported one: its problems, or none. */
216
+ export declare function packageProblems(s: Suggestion, name?: string): string[];
217
+ /** The client a created agent's pull request adds, in the call site's language: no dependency, fetch or urllib alone. */
218
+ export declare function clientFile(s: Suggestion, repo: string, commit: string): NewFile;
219
+ /** The pull request's title and body. */
220
+ export declare function pullText(s: Suggestion, at: {
221
+ repo: string;
222
+ commit: string;
223
+ agentId: string;
224
+ publicUrl?: string;
225
+ }): {
226
+ title: string;
227
+ body: string;
228
+ };
229
+ /** The repository a body or query names, as owner/name; 422 under `field` for anything else. */
230
+ export declare function repoOf(value: unknown, field?: string): string;
231
+ /** Why a repository's installation can't open a pull request: the permissions it lacks, and where to grant them. */
232
+ export declare function missingPermissions(installation: RepoInstallation): string[];
233
+ export declare class Suggestions {
234
+ private readonly deps;
235
+ constructor(deps: SuggestionsDeps);
236
+ private now;
237
+ /** Whether a claim is held: taken, and not so old that the server holding it must have stopped. */
238
+ private held;
239
+ /** The anonymous counts told what was done: a hook that throws is the counts' problem, never the request's. */
240
+ private used;
241
+ private item;
242
+ private need;
243
+ /** `item` written over the one read as `was`: false when another write moved it first. */
244
+ private write;
245
+ /** Each scan as kept. */
246
+ private scans;
247
+ private items;
248
+ /** A scan's run read once it ended: its suggestions kept (a decided one left as it was decided, an open one of an
249
+ * older scan replaced, one the new answer no longer holds removed), and what was read kept on the scan. */
250
+ private settle;
251
+ private mark;
252
+ /** GET /v2/suggestions: GitHub's state, each scan, and the suggestions (`status`: open and created by default). */
253
+ list(query: {
254
+ repo?: string;
255
+ status?: string;
256
+ }): Promise<Json>;
257
+ /** POST /v2/suggestions/scans: a scan of `repo` started (or the one still running answered). */
258
+ scan(body: Json): Promise<{
259
+ status: 200 | 202;
260
+ body: Json;
261
+ }>;
262
+ /** GET /v2/suggestions/{id}: the suggestion whole, the files a create would add (rendered now), and whether this
263
+ * server's GitHub App can open its pull request (read from GitHub now), with what to do when it can't. */
264
+ get(id: string, signal?: AbortSignal, name?: string): Promise<Json>;
265
+ /** Whether a pull request can be opened on `repo` now, and if not, why and where to fix it. */
266
+ private readiness;
267
+ /** POST /v2/suggestions/{id}/discard: it goes away, and later scans are told not to suggest it again. */
268
+ discard(id: string): Promise<Json>;
269
+ /** POST /v2/suggestions/{id}/restore: a discarded suggestion back among the open ones (an undo). */
270
+ restore(id: string): Promise<Json>;
271
+ private decide;
272
+ /** POST /v2/suggestions/{id}/create: the agent made here and its pull request opened (or what is missing to). A
273
+ * suggestion created already answers as it is; one whose agent was made but whose pull request failed opens only that. */
274
+ create(id: string, body: Json, signal?: AbortSignal): Promise<{
275
+ status: 200 | 201;
276
+ body: Json;
277
+ }>;
278
+ /** A create's claim taken off its suggestion's row, whatever the create wrote there; never another create's. */
279
+ private release;
280
+ private creating;
281
+ }
282
+ export {};