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

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 (250) hide show
  1. package/CHANGELOG.md +135 -0
  2. package/README.md +230 -120
  3. package/dist/acp-package.d.ts +4 -0
  4. package/dist/agent-delete.d.ts +6 -9
  5. package/dist/agent-delete.js +5 -3
  6. package/dist/agent-package.js +11 -1
  7. package/dist/api-only.d.ts +21 -3
  8. package/dist/api-only.js +41 -6
  9. package/dist/archil-routes.d.ts +4 -1
  10. package/dist/archil-routes.js +15 -12
  11. package/dist/archil-settings.d.ts +18 -8
  12. package/dist/archil-settings.js +35 -17
  13. package/dist/browser-person.d.ts +1 -1
  14. package/dist/browser-person.js +1 -1
  15. package/dist/calendar/tools.js +1 -1
  16. package/dist/chatgpt-plan.d.ts +4 -2
  17. package/dist/chatgpt-plan.js +13 -3
  18. package/dist/claude-login-token.d.ts +5 -0
  19. package/dist/claude-login-token.js +8 -0
  20. package/dist/claude-login.d.ts +49 -3
  21. package/dist/claude-login.js +140 -10
  22. package/dist/cli-learning/connections.js +1 -1
  23. package/dist/cli.js +42 -10
  24. package/dist/console/assets/{backend-BztxEAvb.js → backend-C9ffXUR5.js} +1 -1
  25. package/dist/console/assets/index-BnZV3lvW.js +639 -0
  26. package/dist/console/assets/{share-L84tlUDW.js → share-B1o5mJ56.js} +2 -2
  27. package/dist/console/assets/{styles-CaRKlHke.js → styles-D_znYrHR.js} +31 -31
  28. package/dist/console/assets/styles-fjQBFfMv.css +1 -0
  29. package/dist/console/index.html +3 -3
  30. package/dist/console/share.html +3 -3
  31. package/dist/decisions.d.ts +1 -1
  32. package/dist/decisions.js +2 -2
  33. package/dist/errors.d.ts +1 -0
  34. package/dist/errors.js +2 -0
  35. package/dist/factory-activity.d.ts +68 -0
  36. package/dist/factory-activity.js +153 -0
  37. package/dist/factory-runs.d.ts +11 -0
  38. package/dist/factory-runs.js +52 -0
  39. package/dist/filed-tool.d.ts +32 -0
  40. package/dist/filed-tool.js +122 -0
  41. package/dist/harness-check.js +2 -1
  42. package/dist/harness-keys.d.ts +1 -2
  43. package/dist/harness-keys.js +1 -2
  44. package/dist/harness-recipe.d.ts +13 -2
  45. package/dist/harness-recipe.js +30 -7
  46. package/dist/harness-sign-in.js +2 -1
  47. package/dist/harness-table.d.ts +9 -3
  48. package/dist/harness-table.js +11 -5
  49. package/dist/harness.js +6 -2
  50. package/dist/import-worker.js +2 -2
  51. package/dist/import.d.ts +9 -0
  52. package/dist/import.js +11 -1
  53. package/dist/key-refusal.d.ts +5 -0
  54. package/dist/key-refusal.js +30 -18
  55. package/dist/learn/audit.d.ts +20 -22
  56. package/dist/learn/audit.js +72 -55
  57. package/dist/learn/check.js +1 -1
  58. package/dist/learn/commit.d.ts +35 -43
  59. package/dist/learn/commit.js +62 -270
  60. package/dist/learn/compile.d.ts +4 -4
  61. package/dist/learn/compile.js +6 -7
  62. package/dist/learn/corrections.js +1 -1
  63. package/dist/learn/delete.d.ts +5 -12
  64. package/dist/learn/delete.js +17 -30
  65. package/dist/learn/forget-run.d.ts +16 -0
  66. package/dist/learn/forget-run.js +53 -0
  67. package/dist/learn/index.js +1 -1
  68. package/dist/learn/learning-of.d.ts +0 -4
  69. package/dist/learn/learning-of.js +0 -5
  70. package/dist/learn/{folder.d.ts → legacy/folder.d.ts} +2 -1
  71. package/dist/learn/{folder.js → legacy/folder.js} +4 -7
  72. package/dist/learn/legacy/place.d.ts +16 -0
  73. package/dist/{workspace/learning-workspace.js → learn/legacy/place.js} +12 -42
  74. package/dist/learn/legacy/scrub.d.ts +28 -0
  75. package/dist/learn/legacy/scrub.js +258 -0
  76. package/dist/learn/promote.d.ts +40 -66
  77. package/dist/learn/promote.js +69 -207
  78. package/dist/learn/reset-ledger.d.ts +18 -0
  79. package/dist/learn/reset-ledger.js +14 -0
  80. package/dist/learn/rows.d.ts +186 -0
  81. package/dist/learn/rows.js +376 -0
  82. package/dist/learn/secrets.d.ts +2 -1
  83. package/dist/learn/secrets.js +2 -2
  84. package/dist/learn/workflow-draft.d.ts +4 -1
  85. package/dist/learn/workflow-draft.js +12 -2
  86. package/dist/learned-nodes.d.ts +36 -0
  87. package/dist/learned-nodes.js +23 -0
  88. package/dist/learning-reset.d.ts +1 -1
  89. package/dist/learning-reset.js +9 -11
  90. package/dist/learning-routes.d.ts +60 -81
  91. package/dist/learning-routes.js +187 -360
  92. package/dist/learning-words.d.ts +2 -0
  93. package/dist/learning-words.js +2 -0
  94. package/dist/learning.d.ts +18 -24
  95. package/dist/learning.js +35 -66
  96. package/dist/local-model.d.ts +144 -0
  97. package/dist/local-model.js +214 -0
  98. package/dist/mcp-gateway.d.ts +6 -0
  99. package/dist/mcp-gateway.js +44 -8
  100. package/dist/mcp-probe.d.ts +7 -0
  101. package/dist/mcp-probe.js +7 -0
  102. package/dist/mcp-resources.d.ts +1 -1
  103. package/dist/native/discovery.d.ts +6 -0
  104. package/dist/native/discovery.js +14 -1
  105. package/dist/native/remote.d.ts +5 -0
  106. package/dist/native/remote.js +16 -0
  107. package/dist/native/runtime.js +3 -2
  108. package/dist/observation/observer.d.ts +6 -6
  109. package/dist/ollama-provider.d.ts +35 -0
  110. package/dist/ollama-provider.js +108 -2
  111. package/dist/openapi.js +147 -35
  112. package/dist/place-offers.d.ts +3 -0
  113. package/dist/place-offers.js +3 -0
  114. package/dist/place.d.ts +35 -12
  115. package/dist/place.js +67 -29
  116. package/dist/print.js +1 -1
  117. package/dist/purged-workflow.d.ts +0 -33
  118. package/dist/purged-workflow.js +3 -71
  119. package/dist/redact/match.d.ts +36 -8
  120. package/dist/redact/match.js +179 -44
  121. package/dist/run-export.js +4 -2
  122. package/dist/run-launch.d.ts +3 -3
  123. package/dist/run-learning-served.d.ts +3 -3
  124. package/dist/run-learning-served.js +1 -1
  125. package/dist/run-listing.d.ts +1 -1
  126. package/dist/run-timeline-served.d.ts +1 -1
  127. package/dist/run-timeline-served.js +6 -5
  128. package/dist/run-usage.js +3 -2
  129. package/dist/runnable.js +25 -8
  130. package/dist/runner/adapter-loop.d.ts +8 -1
  131. package/dist/runner/adapter-loop.js +100 -20
  132. package/dist/runner/adapter-run-spec.d.ts +3 -5
  133. package/dist/runner/adapter-run-spec.js +35 -61
  134. package/dist/runner/adapters/pi.d.ts +3 -2
  135. package/dist/runner/adapters/pi.js +3 -1
  136. package/dist/runner/audit.d.ts +3 -5
  137. package/dist/runner/audit.js +28 -51
  138. package/dist/runner/cli-run-spec.js +19 -22
  139. package/dist/runner/compare-end.d.ts +1 -3
  140. package/dist/runner/compare-end.js +6 -2
  141. package/dist/runner/disk-free.d.ts +15 -4
  142. package/dist/runner/disk-free.js +25 -7
  143. package/dist/runner/dispatch.js +2 -2
  144. package/dist/runner/feedback-round.d.ts +1 -1
  145. package/dist/runner/feedback-round.js +2 -2
  146. package/dist/runner/harness-loops.d.ts +9 -0
  147. package/dist/runner/harness-loops.js +49 -7
  148. package/dist/runner/held.d.ts +4 -11
  149. package/dist/runner/held.js +0 -25
  150. package/dist/runner/journal-kinds.d.ts +4 -1
  151. package/dist/runner/journal-kinds.js +1 -1
  152. package/dist/runner/learned-first.d.ts +0 -2
  153. package/dist/runner/learned-first.js +3 -3
  154. package/dist/runner/learning-end.d.ts +12 -16
  155. package/dist/runner/learning-end.js +41 -75
  156. package/dist/runner/learning-seed.d.ts +13 -16
  157. package/dist/runner/learning-seed.js +13 -23
  158. package/dist/runner/live-ticker.js +3 -4
  159. package/dist/runner/main.js +1 -1
  160. package/dist/runner/mcp-claim.js +3 -2
  161. package/dist/runner/mcp-run.d.ts +23 -4
  162. package/dist/runner/mcp-run.js +31 -2
  163. package/dist/runner/metrics.d.ts +4 -0
  164. package/dist/runner/metrics.js +9 -3
  165. package/dist/runner/monid-run.js +3 -1
  166. package/dist/runner/native-loop.js +3 -4
  167. package/dist/runner/promotion.d.ts +19 -28
  168. package/dist/runner/promotion.js +50 -69
  169. package/dist/runner/redact.d.ts +3 -0
  170. package/dist/runner/redact.js +7 -0
  171. package/dist/runner/reflect-hook.d.ts +2 -2
  172. package/dist/runner/reflect-hook.js +4 -1
  173. package/dist/runner/run-child-host.d.ts +1 -9
  174. package/dist/runner/run-child-host.js +12 -68
  175. package/dist/runner/runner.d.ts +1 -1
  176. package/dist/runner/sdk-loop.js +7 -7
  177. package/dist/runner/workflow-calls.d.ts +4 -1
  178. package/dist/runner/workflow-calls.js +35 -18
  179. package/dist/runner/workflow-loop.d.ts +1 -1
  180. package/dist/runner/workflow-loop.js +3 -1
  181. package/dist/runner/workflow-spec.d.ts +4 -0
  182. package/dist/runner/workflow-spec.js +18 -13
  183. package/dist/runs-over-store.d.ts +2 -2
  184. package/dist/runs-over-store.js +1 -1
  185. package/dist/sdk/check.d.ts +1 -4
  186. package/dist/sdk/check.js +2 -3
  187. package/dist/sdk/runs.d.ts +20 -7
  188. package/dist/sdk/runs.js +105 -68
  189. package/dist/secret-cut.js +7 -4
  190. package/dist/secret-rules.js +97 -83
  191. package/dist/secrets.d.ts +4 -0
  192. package/dist/secrets.js +44 -10
  193. package/dist/server-fs.d.ts +1 -1
  194. package/dist/server-fs.js +1 -1
  195. package/dist/server-keys.d.ts +6 -0
  196. package/dist/server-keys.js +12 -0
  197. package/dist/server.d.ts +22 -15
  198. package/dist/server.js +311 -198
  199. package/dist/slack.js +2 -1
  200. package/dist/source-sync/auxiliary.js +2 -2
  201. package/dist/store/index.d.ts +1 -1
  202. package/dist/store/index.js +1 -1
  203. package/dist/store/postgres/migrate.js +3 -1
  204. package/dist/store/postgres/migrations/0032_learning_rows.sql +65 -0
  205. package/dist/store/schema.d.ts +28 -2
  206. package/dist/store/schema.js +81 -5
  207. package/dist/store/sqlite/learning-rows.d.ts +23 -0
  208. package/dist/store/sqlite/learning-rows.js +559 -0
  209. package/dist/store/sqlite/store.d.ts +7 -1
  210. package/dist/store/sqlite/store.js +84 -18
  211. package/dist/store/store.d.ts +219 -6
  212. package/dist/store/store.js +13 -0
  213. package/dist/store-reads/journal.d.ts +33 -3
  214. package/dist/store-reads/journal.js +99 -4
  215. package/dist/store-reads/learning.d.ts +23 -26
  216. package/dist/store-reads/learning.js +42 -30
  217. package/dist/store-reads/loop.js +1 -1
  218. package/dist/store-view.d.ts +24 -2
  219. package/dist/store-view.js +40 -11
  220. package/dist/templates.d.ts +10 -2
  221. package/dist/templates.js +31 -4
  222. package/dist/tool-plane.d.ts +27 -3
  223. package/dist/tool-plane.js +79 -4
  224. package/dist/tools.d.ts +12 -2
  225. package/dist/tools.js +42 -7
  226. package/dist/trigger-launch-hook.d.ts +2 -2
  227. package/dist/trigger-launch-hook.js +1 -1
  228. package/dist/triggers.d.ts +7 -4
  229. package/dist/triggers.js +4 -4
  230. package/dist/web-holds.d.ts +2 -0
  231. package/dist/web-holds.js +5 -2
  232. package/dist/web-run.js +2 -1
  233. package/dist/workspace/archil-exec.js +1 -1
  234. package/dist/workspace/archil.d.ts +1 -1
  235. package/dist/workspace/archil.js +1 -1
  236. package/dist/workspace/computer-deps.js +5 -3
  237. package/dist/workspace/computer-providers.d.ts +11 -1
  238. package/dist/workspace/computer-providers.js +29 -2
  239. package/dist/workspace/index.d.ts +1 -2
  240. package/dist/workspace/index.js +1 -2
  241. package/dist/workspace/paths.d.ts +0 -4
  242. package/dist/workspace/paths.js +0 -6
  243. package/dist/workspace/workspace.d.ts +1 -1
  244. package/dist/workspace/workspace.js +1 -1
  245. package/package.json +13 -13
  246. package/templates/TOOLS.md +46 -0
  247. package/dist/console/assets/index-LwNL4L9q.js +0 -619
  248. package/dist/console/assets/styles-DQo8rzY-.css +0 -1
  249. package/dist/workspace/learning-workspace.d.ts +0 -24
  250. /package/dist/learn/{learning.sh → legacy/learning.sh} +0 -0
@@ -28,17 +28,14 @@ import { isWebHostTool } from "../tool-list.js";
28
28
  import { deniedText, noApproverText, runGate } from "../tool-gate.js";
29
29
  import { DECISION_PROVIDER_KEYS, loadToolSettings, recheckPlane, TOOL_PLANE_ENV } from "../tool-plane.js";
30
30
  import { readApprovers } from "../server-approvers.js";
31
- import { ASK_TOOL, askPerson, toolOf } from "../tools.js";
31
+ import { ASK_TOOL, askPerson, toolOf, webAnswerCut } from "../tools.js";
32
32
  import { hostToolOf } from "../host-tools.js";
33
33
  import { WEB_SEARCH_TOOL } from "../web-search.js";
34
- import { webCut } from "../timeline/web-calls.js";
35
- import { withheldWhole } from "../key-refusal.js";
36
34
  import { environmentLine, runZone } from "../environment.js";
37
35
  import { effortGuidance } from "../effort-guidance.js";
38
36
  import { outputContract } from "../answer-json.js";
39
- import { laidLearned, learningFolderOf, secretsShown } from "../learning.js";
37
+ import { laidLearned, secretsShown } from "../learning.js";
40
38
  import { laidOf, layLearningSeed } from "./learning-seed.js";
41
- import { learningWorkspace } from "../learn/folder.js";
42
39
  import { learningEndOfRun, spentBesideOf } from "./learning-end.js";
43
40
  import { learnedAskShown, LEARNED_WORKFLOW_TOOL, learnedMcpCalls, learnedWorkflowTool } from "../learned-workflow.js";
44
41
  import { PORT_PROVIDER, tieredModels, tierProvider } from "../port-models.js";
@@ -68,7 +65,7 @@ import { journalEntries } from "./journal-reader.js";
68
65
  import { hashedMcpOf, piShaped } from "../store-reads/journal.js";
69
66
  import { replayCallsOver } from "./promotion.js";
70
67
  import { learnedFirstWhereHeld } from "./learned-first.js";
71
- import { servedOf } from "./workflow-first.js";
68
+ import { servedOf, workflowDigest } from "./workflow-first.js";
72
69
  import { AUDIT_PARKED, withAudit } from "./audit.js";
73
70
  import { runPathMetricsOverStore } from "../store-reads/learning.js";
74
71
  import { specBuilt } from "./spec-holds.js";
@@ -77,7 +74,7 @@ import { loadArchilSettings } from "../archil-settings.js";
77
74
  import { settingOfDefinition } from "../computer-setting.js";
78
75
  import { computerFilesNote, computerKeptWords, computerReadyWords, computerReplacedNote, computerStartingWords, imageWaitWords } from "../workspace/computer-deps.js";
79
76
  import { computerTurnedOff, hadComputer, recordComputerOff } from "./computer-journal.js";
80
- import { heldRedactor, holdClaimed, runnerSecrets, secretRedactor } from "./redact.js";
77
+ import { holdClaimed, mustHold, runnerSecrets } from "./redact.js";
81
78
  import { planBeforeRun, PLAN_PROVIDER } from "../chatgpt-plan.js";
82
79
  import { heldSpecOf } from "./held-run.js";
83
80
  import { loopComputerOf } from "./computer-channel.js";
@@ -334,11 +331,18 @@ export async function adapterRunGate(store, claim, revision, root, shell, option
334
331
  return refused(noApproverText(asks[0].tool, "no"));
335
332
  return asks.length ? { ask: learnedAskShown(asks) } : decision;
336
333
  };
334
+ /** The learned workflow of the run's a learned_workflow call's arguments name, if any. */
335
+ const flowOf = (args) => learned.find((one) => one.name === args?.name);
337
336
  /** The MCP actions the learned workflow a learned_workflow call names can call; none for another call or workflow. */
338
337
  const mcpOf = async (args) => {
339
- const flow = learned.find((one) => one.name === args?.name);
338
+ const flow = flowOf(args);
340
339
  return flow ? learnedMcpCalls(flow.workflow) : [];
341
340
  };
341
+ /** That workflow's name and digest (SpecGate.learnedOf): what the record of a call of it the gate refuses names. */
342
+ const learnedOf = (args) => {
343
+ const flow = flowOf(args);
344
+ return flow ? { workflow: flow.name, digest: workflowDigest(flow) } : undefined;
345
+ };
342
346
  // An ask is named as main's pending approval is (RunGate.askedAs: its settings key), beside the decision, which is
343
347
  // unchanged. The permissions are the learned workflow's (main's toolGate.policy and denyFrom), over the same settings.
344
348
  const permissions = { policy: (tool) => gate.policy(tool), denyFrom: (tool) => gate.denyFrom(tool), refresh: async () => { current = await storeSettings(store); } };
@@ -349,7 +353,7 @@ export async function adapterRunGate(store, claim, revision, root, shell, option
349
353
  current = await storeSettings(store);
350
354
  return current.unreadable ? "deny" : gate.policy(tool);
351
355
  };
352
- return Object.assign(decide, { askedAs: (tool, args) => gate.askedAs(tool, args), permissions, permission, learn: (flows) => void (learned = flows), askedOn: (made) => void (host = made), computerOff: () => gate.computerOff !== undefined, writes: () => gate.writeBar() === undefined });
356
+ return Object.assign(decide, { askedAs: (tool, args) => gate.askedAs(tool, args), permissions, permission, learnedOf, learn: (flows) => void (learned = flows), askedOn: (made) => void (host = made), computerOff: () => gate.computerOff !== undefined, writes: () => gate.writeBar() === undefined });
353
357
  }
354
358
  /** The server's tool settings from the store now; a read that throws is unread (no value of it said). A workflow's call
355
359
  * and model nodes read theirs through it too (workflow-spec.ts callPolicyOf), so both gates fail closed alike. */
@@ -369,13 +373,14 @@ export function adapterRunSpecFromLaunch(launch, run, deps, claim, runner = RUNN
369
373
  return specBuilt((holds) => adapterSpec(launch, run, deps, claim, holds, runner));
370
374
  }
371
375
  async function adapterSpec(launch, run, deps, claim, holds, runner) {
376
+ mustHold(run.redact, "A pi run's spec");
372
377
  const context = BACKGROUND_CONTEXT;
373
378
  // What this claim may reach on loopback, read once for its MCP servers and its calendar_events alike (mcpReachOf).
374
379
  const reach = await mcpReachOf(deps);
375
380
  // The runner's one redactor (the loop's, LoopDeps.redact), for every text a secret could reach: the spec's log lines
376
- // (a release, a workspace's end), the model route's translation and the session's model proxy. A spec built with no
377
- // runner around it cuts every secret the runner's env holds. It holds the keys the claim opens (below), too.
378
- const cut = heldRedactor(run.redact ?? secretRedactor(runnerSecrets(deps.env)));
381
+ // (a release, a workspace's end), the model route's translation and the session's model proxy. It holds the keys the
382
+ // claim opens (below), too.
383
+ const cut = run.redact;
379
384
  const log = (line) => (deps.log ?? (() => { }))(cut(line));
380
385
  holds.logTo(log);
381
386
  const runRow = await deps.store.runs.get(claim.runId);
@@ -445,21 +450,19 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
445
450
  // setting, the one computerForClaim starts) brings its kind's skill, named in the prompt beside the revision's own.
446
451
  const kindSkills = movedProject ? [] : await layOwnSkills(ws.workspace, (revisionRow.skills ?? {}), { computer: settingOfDefinition(body)?.computer.kind });
447
452
  const currentSkills = movedProject ? await movedSkillFiles(ws.workspace, body.skills ?? {}, settingOfDefinition(body)?.computer.kind) : [...Object.keys(body.skills ?? {}), ...kindSkills];
448
- // A learning run's seed, read once from its learning folder where the run's place keeps it (runner/learning-seed.ts):
449
- // its learned workflows, cut of the server's secrets, the ones its learned_workflow tool runs. Read before the run's
450
- // tools are bound. A learning fault, or a seed that failed (seedFailed), gives none. None a purged run wrote, by the
451
- // store's runs; a run's own process (no seal key) reads no other run, and its runner reads the seed again
452
- // (learned-first.ts learnedFirstWhereHeld).
453
+ // A learning run's seed, read once from its agent's learning rows (runner/learning-seed.ts): its learned workflows,
454
+ // cut of the server's secrets, the ones its learned_workflow tool runs. Read before the run's tools are bound. A
455
+ // learning fault, or a seed that failed (seedFailed), gives none.
453
456
  const shown = async () => secretsShown(runnerSecrets(deps.env, await storeSecrets(deps.store, deps.sealKey, deps.data)), { local: true });
454
- const learningFolder = () => (archil === undefined ? learningFolderOf(deps.data, runRow.agentId) : learningWorkspace({ mode: "archil", agentId: runRow.agentId, archil }));
455
457
  const seedLaid = start && !seedFailed(start)
456
- ? await layLearningSeed({ seed: start.seed, folder: learningFolder, store: deps.sealKey ? deps.store : null, shown, log, ...(start.noWorkflows ? { workflows: false } : {}) })
458
+ ? await layLearningSeed({ seed: start.seed, store: deps.store, agentId: runRow.agentId, shown, log, ...(start.noWorkflows ? { workflows: false } : {}) })
457
459
  : undefined;
458
460
  // The run's MCP for this claim (mcp-run.ts mcpForClaim): its pin, the one its revision's save made, read, its
459
461
  // credentials opened under the server's seal key into this claim's memory, one gateway for the claim. First, before
460
462
  // anything the spec would have to let go: a refusal (a granted tool its pin does not hold, a pinned meta tool) ends the run in its
461
463
  // words before the session opens. The seal key is the runner's own (deps.sealKey), not re-read from the environment.
462
- // A credential the claim opens mid-run (a GitHub App token, a saved or per_run server's) joins the run's redactor.
464
+ // A credential the claim opens mid-run (a GitHub App token, a saved or per_run server's) joins the run's redactor. A
465
+ // per_run server the run was handed no credential for is left out of its tools, and its timeline says so.
463
466
  // A held run its model kept from running at its create (hold.refused) opens none: nothing can call one.
464
467
  const heldRefusal = launch.hold?.refused?.message;
465
468
  // What its gateway files (a long answer, a server's results) goes in the run's own files. In Mode 1 that is the run's
@@ -470,7 +473,7 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
470
473
  // and a call reaches it only through the session this spec returns, after `tools` is made.
471
474
  let filedIn;
472
475
  const hold = (values) => cut.hold(values);
473
- const mcp = heldRefusal !== undefined ? undefined : await mcpWhereHeld({ claim, store: deps.store, sealKey: deps.sealKey, grants: granted, harness: run.harness, work: () => (mode === 2 ? filedIn ?? ws.workspace : ws.workspace), launchInput: launch.input, hold, githubAppAccess: (store, key, input) => githubAppAccess(store, key, input, { ...(deps.githubApi ? { api: deps.githubApi } : {}), fromGitHub: launch.inputOrigin?.kind === "trigger" && launch.inputOrigin.source === "github", hold, ...(launch.repo !== undefined ? { repo: launch.repo } : {}) }), ...(deps.githubMcpOrigin ? { githubMcpOrigin: deps.githubMcpOrigin } : {}), ...(deps.composioMcpOrigin ? { composioMcpOrigin: deps.composioMcpOrigin } : {}), ...reach, ...claimMetering(deps, fakes?.web, launch) });
476
+ const mcp = heldRefusal !== undefined ? undefined : await mcpWhereHeld({ claim, store: deps.store, sealKey: deps.sealKey, grants: granted, harness: run.harness, toolSettings: body.toolSettings, work: () => (mode === 2 ? filedIn ?? ws.workspace : ws.workspace), launchInput: launch.input, hold, githubAppAccess: (store, key, input) => githubAppAccess(store, key, input, { ...(deps.githubApi ? { api: deps.githubApi } : {}), fromGitHub: launch.inputOrigin?.kind === "trigger" && launch.inputOrigin.source === "github", hold, ...(launch.repo !== undefined ? { repo: launch.repo } : {}) }), ...(deps.githubMcpOrigin ? { githubMcpOrigin: deps.githubMcpOrigin } : {}), ...(deps.composioMcpOrigin ? { composioMcpOrigin: deps.composioMcpOrigin } : {}), ...reach, say: (words) => run.phase?.(words), ...claimMetering(deps, fakes?.web, launch) });
474
477
  // The claim's MCP goes to the loop, which lets it go; a build that throws lets it go itself.
475
478
  holds.hold({ passed: mcp });
476
479
  // The run's computer, from the one shared claim-time function (the CLI spec takes the same): none for a runner place, a
@@ -670,12 +673,12 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
670
673
  // The audits that finished since are written first (runner/audit.ts settleAudits): a hold they make decides this claim.
671
674
  const admitAuxiliary = runnerSeam() ? undefined : sourceAuxiliaryAdmitter(deps.store, { data: deps.data, sealKey: deps.sealKey, env: deps.env, control: { ...deps.mountControl, ...(deps.makeArchilDisk ? { makeDisk: deps.makeArchilDisk } : {}) } });
672
675
  // None for a held run, so its claim reads no saved key for spend beside it (claimSpendOff).
673
- const audits = heldRefusal === undefined ? { store: deps.store, agentId: runRow.agentId, revisionId: runRow.revisionId, context, log, folder: learningFolder, shown, sealKey: deps.sealKey, spendOff: await claimSpendOff({ store: deps.store, sealKey: deps.sealKey, env: deps.env }, runKey), admitAuxiliary } : undefined;
676
+ const audits = heldRefusal === undefined ? { store: deps.store, agentId: runRow.agentId, revisionId: runRow.revisionId, context, log, shown, sealKey: deps.sealKey, spendOff: await claimSpendOff({ store: deps.store, sealKey: deps.sealKey, env: deps.env }, runKey), admitAuxiliary } : undefined;
674
677
  // Each step writes the store or reads another run (runner/learned-first.ts): in a run's own process its runner takes
675
678
  // them (held.ts learnedFirst), over the workflows it reads at the claim's seed itself. A held run its model kept from
676
679
  // running takes none of them: it settles no audit and starts no replay run, starts on no workflow, and its tool runs
677
680
  // none (so no learned tier is routed, below).
678
- const learned = audits ? await learnedFirstWhereHeld(deps, { agentId: runRow.agentId, runId: claim.runId, revisionId: runRow.revisionId, kind: revision.kind, outputSchema: revision.body.outputSchema, launch, context, log, folder: learningFolder, shown, spendOff: audits.spendOff, admitAuxiliary }, seedLaid?.workflows ?? []) : { choice: { reason: "held" }, workflows: [] };
681
+ const learned = audits ? await learnedFirstWhereHeld(deps, { agentId: runRow.agentId, runId: claim.runId, revisionId: runRow.revisionId, kind: revision.kind, outputSchema: revision.body.outputSchema, launch, context, log, shown, spendOff: audits.spendOff, admitAuxiliary }, seedLaid?.workflows ?? []) : { choice: { reason: "held" }, workflows: [] };
679
682
  const firstChoice = learned.choice;
680
683
  // A replay run's call nodes: reads live through a cache seeded from the graded run's recorded calls, writes only ever
681
684
  // answered from that recording (runner/promotion.ts replayCallsOver, learn/replay-calls.ts).
@@ -744,7 +747,7 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
744
747
  // The run's system prompt, as main's per-run process builds it (app.ts): the environment line first (environment.ts, as
745
748
  // pi installs it before every other section), then the revision's instructions with what the agent learned laid in
746
749
  // (learning.ts laidLearned, from the launch's learning start — the # Learned section decided once at launch, so the runner
747
- // reads no learning folder), its own skills through skills.ts promptOf, the output schema it answers in (answer-json.ts
750
+ // reads no learning rows), its own skills through skills.ts promptOf, the output schema it answers in (answer-json.ts
748
751
  // outputContract, the CLI spec's too), then the effort guidance (the launch's effort, never this process's environment).
749
752
  // The one path for every harness.
750
753
  // A start whose seed failed tells the run nothing: it is never asked what to keep (learning-end.ts drops it).
@@ -801,15 +804,13 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
801
804
  const input = await firstMessage(inputMessage(launch.input), ws.workspace);
802
805
  // The run's end-of-run step, when it learns (learningEndOfRun from the launch's learning start; none otherwise): the
803
806
  // compile turn and the owner's correction over the run's store, asked in the end turn while the claim is held
804
- // (adapter-loop.ts stepOf). Its learning folder is where the run's place keeps it — Mode 1 the agent's folder under the
805
- // data directory, Mode 2 the agent's folder on THIS claim's Archil disk (the draft is checked against the seed on the disk, never this machine's
806
- // local path) — so the end-of-run step reads the run's own Workspace, the one the run worked in.
807
+ // (adapter-loop.ts stepOf), its draft checked against the seed in the agent's learning rows.
807
808
  // A replay run or an audit's shadow run learns nothing at its end: it is the promotion's, never the agent's to learn
808
809
  // from. With a grader it compares its answer with the other one (runner/compare-end.ts), on its own cap.
809
810
  // A run the promoted workflow answered (path `workflow`) starts its shadow audit after the step, drawn at 1 in k (runner/audit.ts
810
811
  // withAudit): at its end, so the shadow's cap is what the run's own left.
811
812
  // A held run (no audits) takes none, and its runner refuses one too (run-child-host.ts heldFor).
812
- const endOfRun = audits === undefined ? undefined : launch.replay || launch.audit ? withGrader(compareEndOfRun(launch, { store: deps.store, folder: learningFolder, log, ...(run.redact ? { redact: run.redact } : {}) }), graderRoute ? { model: graderRoute.model, models: () => run.ledger.models(GRADER, GRADER) } : undefined) : withAudit(learningEndOfRun(launch, {
813
+ const endOfRun = audits === undefined ? undefined : launch.replay || launch.audit ? withGrader(compareEndOfRun(launch, { store: deps.store, log, redact: run.redact }), graderRoute ? { model: graderRoute.model, models: () => run.ledger.models(GRADER, GRADER) } : undefined) : withAudit(learningEndOfRun(launch, {
813
814
  store: deps.store,
814
815
  data: deps.data,
815
816
  ...(deps.env ? { env: deps.env } : {}),
@@ -822,10 +823,7 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
822
823
  log,
823
824
  // A workflow draft's decision nodes are checked against the host's decision providers, on its keys (LS-07).
824
825
  decisionProviders: async () => decisionProvidersIn({ ...(deps.env ?? {}), ...(await hostDecisionKeys({ store: deps.store, sealKey: deps.sealKey, env: deps.env }, DECISION_PROVIDER_KEYS)) }),
825
- learningFolder: archil === undefined
826
- ? (agentId) => learningFolderOf(deps.data, agentId)
827
- : (agentId) => learningWorkspace({ mode: "archil", agentId, archil }),
828
- }), { ...audits, launch, ...(run.redact ? { redact: run.redact } : {}) });
826
+ }), { ...audits, launch, redact: run.redact });
829
827
  return {
830
828
  adapter,
831
829
  facts,
@@ -893,7 +891,7 @@ async function adapterSpec(launch, run, deps, claim, holds, runner) {
893
891
  // The path it took, the learned revision it ran on, its learned workflow calls and why its first choice served or
894
892
  // did not, for a run that learns (workflow-first.ts pathMetrics), by the one writer every harness's end calls
895
893
  // (runPathMetricsOverStore).
896
- const path = await runPathMetricsOverStore(deps.store, claim.runId, launch, { ...(run.redact ? { redact: run.redact } : {}), choice: firstChoice.reason });
894
+ const path = await runPathMetricsOverStore(deps.store, claim.runId, launch, { redact: run.redact, choice: firstChoice.reason });
897
895
  // A run its learned workflow served first: that call is on the loop's record (no pi answer carries its tokens), so its
898
896
  // metrics are its booking's, as a CLI harness's run reads them: the workflow's nodes and the agent's own after a handback.
899
897
  // An MCP tool called by a hashed wire name is counted under its catalog name, as its timeline names it.
@@ -1067,29 +1065,16 @@ export function liveViewersOf(store, sealKey, claim, cut) {
1067
1065
  };
1068
1066
  }
1069
1067
  /** `tool` with its answer's text, and a throw's message, cut by `cut` (the run's one redactor). A web call's answer is cut
1070
- * string by string inside its JSON (timeline/web-calls.ts webCut), so what the harness stores still parses and keeps
1071
- * its links whole for a grader's link check; one whose strings, read joined, hold a held value no cut in place takes (a
1072
- * value split across two of them) is "[redacted]" (webHeld). The join is read against the values `cut` holds
1073
- * (key-refusal.ts withheldWhole), or through `cut` itself when it does not know them (joinCut). Each string is cut once
1074
- * in a call, for the join's read and the answer both. */
1068
+ * string by string inside its JSON (tools.ts webAnswerCut), so what the harness stores still parses and keeps its links
1069
+ * whole for a grader's link check; one whose strings, read joined, hold a held value split across two of them is
1070
+ * "[redacted]". */
1075
1071
  export function answersCut(tool, cut) {
1076
1072
  return {
1077
1073
  ...tool,
1078
1074
  call: async (args, context) => {
1079
1075
  try {
1080
1076
  const answer = await tool.call(args, context);
1081
- const seen = new Map();
1082
- const once = (text) => {
1083
- let out = seen.get(text);
1084
- if (out === undefined)
1085
- seen.set(text, (out = cut(text)));
1086
- return out;
1087
- };
1088
- const holds = (joined, parts) => {
1089
- const values = cut.heldValues?.();
1090
- return values ? withheldWhole(joined, values) : joinCut(joined, parts, once);
1091
- };
1092
- return { ...answer, text: webCut(tool.name, answer.text, once, holds) ?? cut(answer.text) };
1077
+ return { ...answer, text: webAnswerCut(cut)(tool.name, answer.text) ?? cut(answer.text) };
1093
1078
  }
1094
1079
  catch (error) {
1095
1080
  if (error instanceof Error) {
@@ -1103,17 +1088,6 @@ export function answersCut(tool, cut) {
1103
1088
  },
1104
1089
  };
1105
1090
  }
1106
- /** Whether `cut`, a redactor that does not know its values, takes from `joined` (`parts` joined by line breaks) what it
1107
- * does not take from the parts each read alone: a held value split across two parts. Its cut of the join whole being
1108
- * "[redacted]" is not enough: it reads a text longer than a piece in pieces of 8,192 characters (key-refusal.ts
1109
- * redactedAnswer), and a value split inside one piece withholds only that piece, the rest of the join served. It misses
1110
- * nothing: no value split across parts is in a part to cut, so the join's cut and the parts' differ wherever the join
1111
- * holds one; where nothing spans two parts they are the same, as every piece ends at whitespace or inside a run longer
1112
- * than a piece, and a line break ends each part. */
1113
- function joinCut(joined, parts, cut) {
1114
- const whole = cut(joined);
1115
- return whole === "[redacted]" || whole !== parts.map(cut).join("\n");
1116
- }
1117
1091
  /** The ToolHost a run's non-hand tools bind through, the one both the pi spec and the CLI spec build: the run's folder and
1118
1092
  * the claim's environment (claim-secrets.ts: the run's keys from the store, its plane set), the run's registry for an
1119
1093
  * extension a binding installs, the install lifecycle (`life`, run by the adapter loop from the spec's `lifecycle`), the
@@ -7,6 +7,7 @@ import { type SessionLike, type SessionModelPort } from "../../port-models.js";
7
7
  import { type CallStatus } from "../journal-kinds.js";
8
8
  import { CALL_REFUSED } from "../../timeline/index.js";
9
9
  import { type ComputerCallRecord } from "../../computer-calls.js";
10
+ import { type HeldRedact } from "../redact.js";
10
11
  /** What the adapter loop lends pi for one claim: the run's Harness, its registry, and where pi's models go. */
11
12
  export interface PiRun {
12
13
  readonly harness: Harness;
@@ -74,7 +75,7 @@ export interface PiAdapterOptions {
74
75
  readonly fetch?: typeof fetch;
75
76
  /** The runner's one redactor, which a provider's error passes through in the session's proxy before pi has it
76
77
  * (ModelProxyOptions.redact; the proxy's own cut of the credential comes first). */
77
- readonly redact?: (text: string) => string;
78
+ readonly redact: HeldRedact;
78
79
  /** pi-native extensions the run's root agent carries beside the session's own (the browser's durable extension, as a
79
80
  * pi DurableRun on main installs it): installed in the claim's registry, so their tools run as pi's own tool tasks (a
80
81
  * taskId, a memo, conversations they own) and their own task kinds (a browser.release) under pi's recovery, all in the
@@ -119,7 +120,7 @@ export interface TierProxies {
119
120
  * keys, AdapterSession.port's `tier`), sent by pi-ai's client of the tier model's own wire (port-models.ts), so a tier
120
121
  * proxy speaks its model's route (model-route.ts routeOf, its translation fetch for a model of neither wire), never the
121
122
  * harness's. One that fails to start closes those started. */
122
- export declare function openTierProxies(tiers: ReadonlyMap<string, TierModel>, hooks: SessionIO["models"], redact?: (text: string) => string, tools?: readonly string[]): Promise<TierProxies>;
123
+ export declare function openTierProxies(tiers: ReadonlyMap<string, TierModel>, hooks: SessionIO["models"], redact: (text: string) => string, tools?: readonly string[]): Promise<TierProxies>;
123
124
  /** The session proxy's policy on Messages: claude-agent's (no fork), with pi's tools, the host's own names (`tools`), as
124
125
  * the names its client offers by themselves (its builtins). The CLI's own `mcp__hands__` prefix is not pi's: under it
125
126
  * every one of pi's tool definitions would be dropped and the model offered none. Every other tool definition is
@@ -13,6 +13,7 @@ import { sessionProvider, tierProvider } from "../../port-models.js";
13
13
  import { KINDS } from "../journal-kinds.js";
14
14
  import { CALL_REFUSED } from "../../timeline/index.js";
15
15
  import { recordingCalls } from "../../computer-calls.js";
16
+ import { mustHold } from "../redact.js";
16
17
  /** `extension` with each of its tools run inside recordingCalls, its calls kept by `keep` under the call's id. A call pi
17
18
  * runs again after a restart keeps what that attempt makes: a code mode script's replay hands back the results of the
18
19
  * calls its earlier attempt made, never making them again, so none is kept twice. The earlier attempt's own record goes
@@ -64,6 +65,7 @@ const HOST_VALUE = "agentrun.value";
64
65
  const INPUT_REQUEST = "agentrun:input";
65
66
  /** pi as an AgentAdapter for one claim's run. */
66
67
  export function piAdapter(options) {
68
+ mustHold(options.redact, "A pi adapter");
67
69
  return {
68
70
  // pi is the default harness: its id is the harness table's, never a literal of its own.
69
71
  id: DEFAULT_HARNESS,
@@ -116,7 +118,7 @@ async function routeProxy(route, hooks, tools, redact, tier, webSearch = false)
116
118
  ...(price ? { price: price } : {}),
117
119
  ...(route.provider.headers ? { headers: route.provider.headers } : {}),
118
120
  ...(route.fetch ? { fetch: route.fetch } : {}),
119
- ...(redact ? { redact } : {}),
121
+ redact,
120
122
  }, randomBytes(32).toString("base64url"));
121
123
  }
122
124
  /** A run's models of other tiers than its own (a learned workflow's node of a tier: AGENT_MODEL_FAST, AGENT_MODEL_STRONG),
@@ -3,12 +3,11 @@ import type { AuxiliaryAdmitter } from "../source-sync/auxiliary.js";
3
3
  import { type AuditLoop } from "../learning-setting.js";
4
4
  import { type RunLaunch } from "../run-launch.js";
5
5
  import { type RunRow, type Store } from "../store/store.js";
6
- import { type LearningFolder } from "../learn/folder.js";
7
6
  import type { Shown } from "../learning.js";
8
7
  import type { EndOfRunTurn } from "./loop-contract.js";
9
8
  import { type WorkflowFirst } from "./workflow-first.js";
10
9
  /** What the audit reads and writes at a claim (or the server's grade): the agent, the run claimed (none for a grade),
11
- * its revision (whose definition holds the numbers) and launch, the agent's learning folder, the host's secrets as a
10
+ * its revision (whose definition holds the numbers) and launch, the host's secrets as a
12
11
  * hold's texts are cut of them (`shown`, as a check's are), the seal key (a shadow is handed the per_run MCP
13
12
  * credentials its served run was). */
14
13
  export type AuditDeps = {
@@ -16,7 +15,6 @@ export type AuditDeps = {
16
15
  readonly agentId: string;
17
16
  readonly revisionId: string;
18
17
  readonly context: Context;
19
- readonly folder: () => LearningFolder;
20
18
  readonly shown: () => Promise<Pick<Shown, "text">>;
21
19
  readonly log: (line: string) => void;
22
20
  readonly sealKey?: Buffer;
@@ -36,7 +34,7 @@ export declare function auditLoopOfRevision(store: Pick<Store, "agents">, revisi
36
34
  export declare function agreeOnOfRevision(store: Pick<Store, "agents">, revisionId: string): Promise<readonly string[] | undefined>;
37
35
  /** The served answer shadow run `shadowRun` audits: its open line's `turn`'s answer, read from the served run by that row's
38
36
  * id (what settleAudits compares the shadow's answer with); null when no open line names it, or it names no turn. */
39
- export declare function auditedAnswerOf(deps: Pick<AuditDeps, "store" | "context" | "folder">, shadowRun: string): Promise<string | null>;
37
+ export declare function auditedAnswerOf(deps: Pick<AuditDeps, "store" | "agentId" | "context">, shadowRun: string): Promise<string | null>;
40
38
  /** How many of the newest stop_window runs `workflow` at `digest` served were not auditable (auditableLaunch: a trigger
41
39
  * started them): what the promotions read counts beside the audit line. */
42
40
  export declare function notAuditableOf(store: Pick<Store, "runs" | "settings">, agentId: string, workflow: string, digest: string, loop: Pick<AuditLoop, "stop_window">): Promise<number>;
@@ -51,7 +49,7 @@ export declare const AUDIT_PARKED = "the audit ended where it would wait on a pe
51
49
  * could never be audited (no learning start, or not auditableLaunch) keeps `step` as it is. */
52
50
  export declare function withAudit(step: EndOfRunTurn | undefined, deps: AuditDeps & {
53
51
  readonly launch: RunLaunch;
54
- readonly redact?: (text: string) => string;
52
+ readonly redact: (text: string) => string;
55
53
  }): EndOfRunTurn | undefined;
56
54
  /** Starts the shadow of run `runId`, at its end, when the learned workflow `serve` served it first and its id is drawn:
57
55
  * never for a replay or a shadow, a run that does not learn, one not auditable (auditableLaunch: a trigger started it,
@@ -1,6 +1,6 @@
1
1
  // The shadow audit of a serving learned workflow, at a run's claim and its end (learn/audit.ts keeps the ledger).
2
2
  // startAudit (withAudit: at the served run's end, before the end commits): a run the promoted workflow answered (path
3
- // `workflow`; a run it handed back is never drawn and never counts as served) is drawn at 1 in k (auditDrawn, from its id), and a drawn one appends a `started` line to audits.jsonl (with the
3
+ // `workflow`; a run it handed back is never drawn and never counts as served) is drawn at 1 in k (auditDrawn, from its id), and a drawn one adds a `started` audit record (with the
4
4
  // promotion serving it and the answer it audits, `turn`: the id of the run's newest answer row now), then starts its
5
5
  // shadow run: the same agent and revision on the same input with learning off (no seed, no compile turn), marked as the
6
6
  // audit of the served run (launch.audit), no row of the run list, billed to the agent as learning cost. Its cap is
@@ -37,11 +37,9 @@ import { runPathOverStore } from "../store-reads/learning.js";
37
37
  import { FINAL_STATES, StoreConstraint } from "../store/store.js";
38
38
  import { openRunMcpAuth, sealRunMcpAuth } from "../store/seal.js";
39
39
  import { isObject } from "../validate.js";
40
- import { auditableLaunch, auditChange, auditEveryOf, auditDrawn, auditsOffWords, auditWriterOf, holdOf, ledgerOf, lineKey, openAudits, rowsOf, shadowRunId } from "../learn/audit.js";
40
+ import { addAuditLines, auditableLaunch, auditEveryOf, auditDrawn, auditsOffWords, auditWriterOf, holdOf, ledgerOf, lineKey, openAudits, rowsOf, shadowRunId } from "../learn/audit.js";
41
41
  import { lastRunsOf, sinceReset } from "../learn/compile.js";
42
- import { readLearning, writeLearning } from "../learn/folder.js";
43
42
  import { CHECK_TEXT_MAX, comparedGrade, comparedOf, gradesOf, promotionOf, promotionsOf, promotionStartOf, recordPromotion, servingOf } from "../learn/promote.js";
44
- import { refusedIn } from "../purged-workflow.js";
45
43
  import { excerptShown } from "../learn/secrets.js";
46
44
  import { auditRoomOf } from "./learning-end.js";
47
45
  import { answerJudged, answerOf, answerRowOf } from "./promotion.js";
@@ -184,7 +182,7 @@ export async function startAudit(deps, serve) {
184
182
  return;
185
183
  if ((await readLedger(deps)).some((line) => line.run === runId && line.source === "shadow"))
186
184
  return;
187
- const promoted = promotionStartOf(await promotionsOf(store.settings, agentId), serve.name, serve.digest);
185
+ const promoted = promotionStartOf(await promotionsOf(store.learning, agentId), serve.name, serve.digest);
188
186
  // The answer audited: the run's newest answer now, at the end that drew it, kept by its row's id so a follow-up's
189
187
  // answer since is never the one compared.
190
188
  const turn = turnOf((await answerRowOf(store, runId, deps.context))?.id);
@@ -215,7 +213,14 @@ export async function startAudit(deps, serve) {
215
213
  audit: { of: runId, workflow: serve.name, digest: serve.digest },
216
214
  };
217
215
  const started = { source: "started", run: runId, shadow_run: id, workflow: serve.name, digest: serve.digest, promoted_at: promoted?.at ?? null, ...turn, at: iso((deps.now ?? Date.now)()) };
218
- await addLines(deps, [started]);
216
+ // The ledger after (`all`, never `added`): a started line already there (a crash between it and its shadow) is in it,
217
+ // and its shadow is made now. None there: the append refused it, as a purge took the digest since the draw (or the
218
+ // run), and settleAudits would never settle a shadow with no line, so none is made.
219
+ const { all } = await addLines(deps, [started]);
220
+ if (!all.some((line) => lineKey(line) === lineKey(started))) {
221
+ log(`workflow-first: ${serve.name} at ${serve.digest} served this run, drawn for its audit (1 in ${k}); no shadow: the workflow's version was purged since`);
222
+ return;
223
+ }
219
224
  const sealed = await handedTo(deps, runId, id);
220
225
  try {
221
226
  if (sourceSyncOf(revision?.body))
@@ -251,7 +256,7 @@ export async function settleAudits(deps) {
251
256
  const { store, log } = deps;
252
257
  try {
253
258
  // An agent with no promotion record never served a learned workflow: it has no ledger to read.
254
- if (!(await promotionsOf(store.settings, deps.agentId)).length)
259
+ if (!(await promotionsOf(store.learning, deps.agentId)).length)
255
260
  return;
256
261
  const lines = await readLedger(deps);
257
262
  const now = (deps.now ?? Date.now)();
@@ -305,7 +310,7 @@ export async function gradeRow(deps, run, correct, expected) {
305
310
  return;
306
311
  if ((run.disk ?? null) !== null || !(await sinceReset(deps.store, deps.agentId))(run))
307
312
  return;
308
- const promoted = promotionStartOf(await promotionsOf(deps.store.settings, deps.agentId), m.workflow, m.workflow_digest);
313
+ const promoted = promotionStartOf(await promotionsOf(deps.store.learning, deps.agentId), m.workflow, m.workflow_digest);
309
314
  // The answer graded: the run's newest now, kept by its row's id (as a shadow's audit keeps its turn).
310
315
  const turn = turnOf((await answerRowOf(deps.store, run.id, deps.context))?.id);
311
316
  const row = { run: run.id, shadow_run: null, workflow: m.workflow, digest: m.workflow_digest, promoted_at: promoted?.at ?? null, source: "grade", agreed: correct, served_cost: run.costUsd, shadow_cost: null, ...turn, at: iso((deps.now ?? Date.now)()) };
@@ -321,58 +326,30 @@ export async function gradeRow(deps, run, correct, expected) {
321
326
  }, true);
322
327
  await holdByLedger(deps, rowsOf(all));
323
328
  }
324
- /** The ledger's lines on the agent's learning folder's main. */
325
- async function readLedger(deps) {
326
- const folder = deps.folder();
327
- try {
328
- return await readLearning(folder.workspace, ledgerOf);
329
- }
330
- finally {
331
- await folder.end().catch(() => undefined);
332
- }
333
- }
329
+ /** The agent's ledger (learn/audit.ts ledgerOf). */
330
+ const readLedger = (deps) => ledgerOf(deps.store.learning, deps.agentId);
334
331
  /** Appends `lines` (each not in the ledger yet, lineKey; a row's verdict read by `agreed` once it is known to be new) in
335
332
  * their order, and answers the ledger after (`all`) with the lines it appended. A grade's row (`again`) replaces an
336
333
  * earlier grade of the same run. */
337
334
  async function addLines(deps, lines, agreed, again = false) {
338
- const folder = deps.folder();
339
- try {
340
- const verdicts = new Map();
341
- const done = await writeLearning(folder.workspace, async (read) => {
342
- const before = (await ledgerOf(read)).filter((one) => !again || !lines.some((line) => lineKey(line) === lineKey(one)));
343
- const known = new Set(before.map(lineKey));
344
- const fresh = [];
345
- for (const line of lines) {
346
- const key = lineKey(line);
347
- if (known.has(key))
348
- continue;
349
- if (line.source === "started" || !agreed) {
350
- fresh.push(line);
351
- continue;
352
- }
353
- if (!verdicts.has(key))
354
- verdicts.set(key, await agreed(line));
355
- fresh.push({ ...line, agreed: verdicts.get(key) });
356
- }
357
- const change = auditChange(before, fresh);
358
- return { result: { all: [...before, ...change.added], added: change.added }, ...(change.change ? { change: change.change } : {}) };
359
- });
360
- return done.result;
361
- }
362
- finally {
363
- await folder.end().catch(() => undefined);
335
+ const known = new Set(again ? [] : (await readLedger(deps)).map(lineKey));
336
+ const fresh = [];
337
+ for (const line of lines) {
338
+ if (known.has(lineKey(line)))
339
+ continue;
340
+ fresh.push(line.source === "started" || !agreed ? line : { ...line, agreed: await agreed(line) });
364
341
  }
342
+ return addAuditLines(deps.store.learning, deps.agentId, fresh, again);
365
343
  }
366
344
  /** The hold rule over the ledger's rows `all` for each workflow that serves now (its champion's digest, skipping a
367
- * record the purge gate refuses as a claim's does: purged-workflow.ts refusedOf), whether or not a row was just added:
345
+ * as a claim's), whether or not a row was just added:
368
346
  * idempotent (applyHold holds only a digest whose newest record still promotes it). */
369
347
  async function holdByLedger(deps, all) {
370
348
  if (!all.length)
371
349
  return;
372
350
  const loop = await auditLoopOfRevision(deps.store, deps.revisionId);
373
- const records = await promotionsOf(deps.store.settings, deps.agentId);
374
- const refused = await refusedIn(deps.folder, deps.store, deps.log)(records);
375
- for (const { workflow, digest } of servingOf(records, refused)) {
351
+ const records = await promotionsOf(deps.store.learning, deps.agentId);
352
+ for (const { workflow, digest } of servingOf(records)) {
376
353
  if (digest !== null)
377
354
  await applyHold(deps, all, loop, workflow, digest);
378
355
  }
@@ -384,7 +361,7 @@ async function holdByLedger(deps, all) {
384
361
  * one holds). */
385
362
  async function applyHold(deps, rows, loop, workflow, digest) {
386
363
  const { store, agentId, log } = deps;
387
- const records = await promotionsOf(store.settings, agentId);
364
+ const records = await promotionsOf(store.learning, agentId);
388
365
  const current = promotionOf(records, workflow, digest);
389
366
  if (current?.decided !== "promote")
390
367
  return;
@@ -400,7 +377,7 @@ async function applyHold(deps, rows, loop, workflow, digest) {
400
377
  const cut = (text) => excerptShown(text, shown, CHECK_TEXT_MAX);
401
378
  const disagreements = [];
402
379
  // A grade's row shows the right answer its grade gave, the known answer the served one missed.
403
- const grades = hold.disagreed.some((row) => row.source === "grade") ? await gradesOf(store.settings, agentId) : [];
380
+ const grades = hold.disagreed.some((row) => row.source === "grade") ? await gradesOf(store.learning, agentId) : [];
404
381
  for (const row of hold.disagreed) {
405
382
  const input = await store.runs.launch(row.run).then((one) => inputText(readLaunchBody(one.body)?.input ?? null), () => "");
406
383
  // The answer the row compared (its turn's), as the verdict read it: never a follow-up's since.
@@ -416,7 +393,7 @@ async function applyHold(deps, rows, loop, workflow, digest) {
416
393
  audit: { window: loop.hold_window, disagree: loop.hold_disagree, rows: hold.rows.length, disagreements, ...(stopped ? { stopped: true } : {}) },
417
394
  ...(current.commit ? { commit: current.commit } : {}),
418
395
  };
419
- const kept = await recordPromotion(store.settings, agentId, record, (was) => was?.decided === "promote" && was.at === current.at, refusedIn(deps.folder, store, log));
396
+ const kept = await recordPromotion(store.learning, agentId, record, (was) => was?.decided === "promote" && was.at === current.at);
420
397
  if (kept.won)
421
398
  log(`workflow-first: ${workflow} at ${digest} ${stopped ? `stopped (${said}, a second hold within ${loop.stop_window} served runs)` : reason}`);
422
399
  }