@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
@@ -1,14 +1,12 @@
1
- import type { Workspace } from "../workspace/index.js";
1
+ import { type Store } from "../store/store.js";
2
2
  import type { AuditLoop } from "../learning-setting.js";
3
3
  import type { RunLaunch } from "../run-launch.js";
4
- import { AUDITS_FILE, type FolderChange, type FolderRead } from "./folder.js";
5
- import { type CostStop, type PromotionRecord, type Refused, type ServingState } from "./promote.js";
6
- export { AUDITS_FILE };
7
- /** The newest rows the ledger keeps: twice the widest window a definition may set (AUDIT_MAX), and a file well under the
8
- * folder's file cap (a line is a few hundred bytes); with them, the started lines of theirs, and every open one. */
9
- export declare const AUDITS_KEPT = 2000;
4
+ import { type CostStop, type PromotionRecord, type ServingState } from "./promote.js";
10
5
  /** What every shadow run's id starts with: its served run's id follows (shadowRunId). No server-minted id starts so. */
11
6
  export declare const SHADOW_PREFIX = "audit.";
7
+ /** The newest rows the ledger keeps: twice the widest window a definition may set (AUDIT_MAX); with them, the started
8
+ * lines of theirs, and every open one (addAuditLines trims it in the write that adds). */
9
+ export declare const AUDITS_KEPT = 2000;
12
10
  /** One finished audit. `run`: the served run; `shadow_run`: its shadow (null for a grade's row, or one never started);
13
11
  * `workflow` at `digest` served it, under the promotion begun at `promoted_at` (learn/promote.ts promotionStartOf: the
14
12
  * one serving when its shadow started, or at the grade; null: none); `agreed`: whether the served answer is right
@@ -75,22 +73,22 @@ export declare const auditsOffWords: (tool: string) => string;
75
73
  * uploaded files. A triggered run's context (a GitHub event's checkout, the thread it answers on) is never rebuilt for a
76
74
  * shadow, which could answer there too: its served run is not auditable (its record's metrics.not_auditable). */
77
75
  export declare const auditableLaunch: (launch: Pick<RunLaunch, "trigger" | "inputOrigin" | "files">) => boolean;
78
- /** The lines of a ledger's text, in the order written; one out of shape is skipped. */
79
- export declare function parseLedger(text: string): LedgerLine[];
80
- /** The ledger's text for `lines`: from the AUDITS_KEPT-th newest row on, and every open started line before it. */
81
- export declare function auditsText(lines: readonly LedgerLine[]): string;
82
- /** The ledger's lines on the folder's main as `folder` reads it (none before the first). */
83
- export declare function ledgerOf(folder: FolderRead): Promise<LedgerLine[]>;
84
- /** The ledger's rows on the folder's main: its finished audits, oldest first. */
85
- export declare const readAudits: (ws: Workspace) => Promise<AuditRow[]>;
76
+ /** The store's learning records, as the ledger is read and written. */
77
+ export type AuditRecords = Pick<Store["learning"], "records" | "changeRecords">;
78
+ /** The agent's ledger, in the order written (none before the first line). */
79
+ export declare const ledgerOf: (rows: Pick<AuditRecords, "records">, agentId: string) => Promise<LedgerLine[]>;
80
+ /** The ledger's rows: its finished audits, oldest first. */
81
+ export declare const readAudits: (rows: Pick<AuditRecords, "records">, agentId: string) => Promise<AuditRow[]>;
86
82
  /** A line's place in the ledger: one per served run and source (a started line's, its shadow's row, a grade's row). */
87
83
  export declare const lineKey: (line: LedgerLine) => string;
88
- /** The change that appends `lines` to the ledger `before` (none when each is there already: lineKey), with the lines it
89
- * appends. */
90
- export declare function auditChange(before: readonly LedgerLine[], lines: readonly LedgerLine[]): {
84
+ /** Appends `lines` to the ledger, each not there yet (lineKey), in one transaction: `replace`, a line whose key is there
85
+ * takes the place of the one there (a grade's row of a run graded again). The ledger after (`all`) with the lines it
86
+ * appended. A line of a digest no version of its workflow holds (a run's purge took it since the line was read), or of a
87
+ * run that was purged, is never appended (Store.learning changeRecords refuses it): the others are. */
88
+ export declare function addAuditLines(rows: AuditRecords, agentId: string, lines: readonly LedgerLine[], replace?: boolean): Promise<{
89
+ all: LedgerLine[];
91
90
  added: LedgerLine[];
92
- change?: FolderChange;
93
- };
91
+ }>;
94
92
  /** The rows the hold rule counts for `workflow` at `digest` under the promotion begun at `promotedAt` (learn/promote.ts
95
93
  * promotionStartOf; none counted for null): those with a verdict, the newest `hold_window` of them. A served run an
96
94
  * owner graded is counted by its grade's row alone: the grade is its known answer, as it is the check's, so its
@@ -112,7 +110,7 @@ export declare function auditLineOf(rows: readonly AuditRow[], workflow: string,
112
110
  /** What the promotions read says of each learned workflow beside what serves (learn/promote.ts servingOf, in its order):
113
111
  * `state` (learn/promote.ts servingStateOf with the money rule's decision, `costStop`: the word the learning view's
114
112
  * `status` says too), its newest decided check's numbers, and its audit line over the rows of the serving (else the
115
- * newest) revision's newest promotion (the ones the hold rule counts). Its champions skip `refused` (championOf). */
113
+ * newest) revision's newest promotion (the ones the hold rule counts). */
116
114
  export type ServingLine = {
117
115
  readonly state: ServingState;
118
116
  readonly check: {
@@ -123,4 +121,4 @@ export type ServingLine = {
123
121
  } | null;
124
122
  readonly audit: AuditLine | null;
125
123
  };
126
- export declare function servingLinesOf(records: readonly PromotionRecord[], rows: readonly AuditRow[], loop: Pick<AuditLoop, "hold_window" | "hold_disagree">, costStop: CostStop, refused?: Refused): ServingLine[];
124
+ export declare function servingLinesOf(records: readonly PromotionRecord[], rows: readonly AuditRow[], loop: Pick<AuditLoop, "hold_window" | "hold_disagree">, costStop: CostStop): ServingLine[];
@@ -1,5 +1,5 @@
1
- // The audit ledger: audits.jsonl at the top of an agent's git learning folder, one JSON line each, appended by the host
2
- // alone (runner/audit.ts, and the server for an owner's grade). A `started` line is a shadow audit begun (written before
1
+ // The audit ledger: the agent's audit records (Store.learning records of kind audit), one line each, appended by the
2
+ // host alone (runner/audit.ts, and the server for an owner's grade). A `started` line is a shadow audit begun (written before
3
3
  // its shadow run is created); a row is one finished audit of a run a promoted learned workflow served first: its shadow
4
4
  // run's answer (the same agent on the same input, learning off) against the served one (source `shadow`, the started
5
5
  // line's result), or an owner's grade of the served run (source `grade`: the served answer against the grade's known
@@ -10,15 +10,14 @@
10
10
  // run cost.
11
11
  import { createHash } from "node:crypto";
12
12
  import { COMPUTER_TOOLS } from "@parcha/agentrun-kernel/kernel";
13
+ import { LearningRecordRefused } from "../store/store.js";
13
14
  import { pinAnnotations, toolClassOf, toolWord } from "./replay-calls.js";
14
- import { AUDITS_FILE, readLearning } from "./folder.js";
15
15
  import { promotionStartOf, servingOf, servingStateOf } from "./promote.js";
16
- export { AUDITS_FILE };
17
- /** The newest rows the ledger keeps: twice the widest window a definition may set (AUDIT_MAX), and a file well under the
18
- * folder's file cap (a line is a few hundred bytes); with them, the started lines of theirs, and every open one. */
19
- export const AUDITS_KEPT = 2_000;
20
16
  /** What every shadow run's id starts with: its served run's id follows (shadowRunId). No server-minted id starts so. */
21
17
  export const SHADOW_PREFIX = "audit.";
18
+ /** The newest rows the ledger keeps: twice the widest window a definition may set (AUDIT_MAX); with them, the started
19
+ * lines of theirs, and every open one (addAuditLines trims it in the write that adds). */
20
+ export const AUDITS_KEPT = 2_000;
22
21
  /** The rows of `lines` (the finished audits), oldest first. */
23
22
  export const rowsOf = (lines) => lines.filter((line) => line.source !== "started");
24
23
  /** The started lines of `lines` with no result row yet: the audits settlement still owes, oldest first. */
@@ -76,57 +75,75 @@ const isLine = (value) => {
76
75
  const noVerdictOf = (line) => line.agreed !== null ? {} : line.skipped === "no allowance" ? { skipped: "no allowance" } : line.parked === true ? { parked: true } : {};
77
76
  /** A line's turn (AuditRow.turn), as written: none when it has none. */
78
77
  const turnOf = (line) => (line.turn === undefined ? {} : { turn: line.turn });
79
- /** The lines of a ledger's text, in the order written; one out of shape is skipped. */
80
- export function parseLedger(text) {
81
- const out = [];
82
- for (const raw of text.split("\n")) {
83
- if (!raw.trim())
84
- continue;
78
+ /** A ledger line as read back, or undefined for one out of shape. */
79
+ function lineOf(value) {
80
+ if (!isLine(value))
81
+ return undefined;
82
+ const { run, workflow, digest, promoted_at, at } = value;
83
+ return value.source === "started"
84
+ ? { source: "started", run, shadow_run: value.shadow_run, workflow, digest, promoted_at, ...turnOf(value), at }
85
+ : { run, shadow_run: value.shadow_run, workflow, digest, promoted_at, source: value.source, agreed: value.agreed, served_cost: value.served_cost, shadow_cost: value.shadow_cost, ...turnOf(value), ...noVerdictOf(value), at };
86
+ }
87
+ const linesOf = (records) => records.flatMap((one) => { const line = lineOf(one.data); return line ? [line] : []; });
88
+ /** A line's record: its served run, so the run's purge deletes it. */
89
+ const lineWrite = (line) => ({ runId: line.run, workflow: line.workflow, digest: line.digest, data: line });
90
+ /** The agent's ledger, in the order written (none before the first line). */
91
+ export const ledgerOf = async (rows, agentId) => linesOf(await rows.records(agentId, "audit"));
92
+ /** The ledger's rows: its finished audits, oldest first. */
93
+ export const readAudits = async (rows, agentId) => rowsOf(await ledgerOf(rows, agentId));
94
+ /** A line's place in the ledger: one per served run and source (a started line's, its shadow's row, a grade's row). */
95
+ export const lineKey = (line) => `${line.source}:${line.run}`;
96
+ /** Appends `lines` to the ledger, each not there yet (lineKey), in one transaction: `replace`, a line whose key is there
97
+ * takes the place of the one there (a grade's row of a run graded again). The ledger after (`all`) with the lines it
98
+ * appended. A line of a digest no version of its workflow holds (a run's purge took it since the line was read), or of a
99
+ * run that was purged, is never appended (Store.learning changeRecords refuses it): the others are. */
100
+ export async function addAuditLines(rows, agentId, lines, replace = false) {
101
+ let left = lines;
102
+ for (let tries = 0;; tries++) {
85
103
  try {
86
- const value = JSON.parse(raw);
87
- if (!isLine(value))
88
- continue;
89
- const { run, workflow, digest, promoted_at, at } = value;
90
- out.push(value.source === "started"
91
- ? { source: "started", run, shadow_run: value.shadow_run, workflow, digest, promoted_at, ...turnOf(value), at }
92
- : { run, shadow_run: value.shadow_run, workflow, digest, promoted_at, source: value.source, agreed: value.agreed, served_cost: value.served_cost, shadow_cost: value.shadow_cost, ...turnOf(value), ...noVerdictOf(value), at });
104
+ return await appendLines(rows, agentId, left, replace);
93
105
  }
94
- catch {
95
- // A torn line is no line.
106
+ catch (error) {
107
+ // A purged run's line, refused in the write's own transaction: the write again without it.
108
+ if (!(error instanceof LearningRecordRefused) || tries >= 2)
109
+ throw error;
110
+ const refused = new Set(error.records.map((one) => one.runId));
111
+ const kept = left.filter((line) => !refused.has(line.run));
112
+ if (kept.length === left.length)
113
+ throw error;
114
+ left = kept;
96
115
  }
97
116
  }
98
- return out;
99
- }
100
- /** The ledger's text for `lines`: from the AUDITS_KEPT-th newest row on, and every open started line before it. */
101
- export function auditsText(lines) {
102
- const rows = lines.flatMap((line, i) => (line.source === "started" ? [] : [i]));
103
- const from = rows.length > AUDITS_KEPT ? rows[rows.length - AUDITS_KEPT] : 0;
104
- const open = new Set(openAudits(lines));
105
- return lines.filter((line, i) => i >= from || open.has(line)).map((line) => JSON.stringify(line)).join("\n") + "\n";
106
117
  }
107
- /** The ledger's lines on the folder's main as `folder` reads it (none before the first). */
108
- export async function ledgerOf(folder) {
109
- const at = folder.snapshot?.audits;
110
- return at ? parseLedger(await folder.body(AUDITS_FILE, at.oid)) : [];
118
+ function appendLines(rows, agentId, lines, replace) {
119
+ return rows.changeRecords(agentId, "audit", (records, held) => {
120
+ const keys = new Set(lines.map(lineKey));
121
+ const kept = records.filter((one) => { const line = lineOf(one.data); return !replace || !line || !keys.has(lineKey(line)); });
122
+ const before = linesOf(kept);
123
+ const known = new Set(before.map(lineKey));
124
+ const added = [];
125
+ for (const line of lines) {
126
+ if (known.has(lineKey(line)))
127
+ continue;
128
+ if (held !== undefined && !held(line.workflow, line.digest))
129
+ continue;
130
+ known.add(lineKey(line));
131
+ added.push(line);
132
+ }
133
+ if (!added.length && kept.length === records.length)
134
+ return { result: { all: before, added } };
135
+ const writes = trimmed([...kept.map((one) => ({ seq: one.seq, runId: one.runId, workflow: one.workflow, digest: one.digest, data: one.data })), ...added.map(lineWrite)]);
136
+ return { records: writes, result: { all: linesOf(writes), added } };
137
+ });
111
138
  }
112
- /** The ledger's rows on the folder's main: its finished audits, oldest first. */
113
- export const readAudits = async (ws) => rowsOf(await readLearning(ws, ledgerOf));
114
- /** A line's place in the ledger: one per served run and source (a started line's, its shadow's row, a grade's row). */
115
- export const lineKey = (line) => `${line.source}:${line.run}`;
116
- /** The change that appends `lines` to the ledger `before` (none when each is there already: lineKey), with the lines it
117
- * appends. */
118
- export function auditChange(before, lines) {
119
- const known = new Set(before.map(lineKey));
120
- const added = [];
121
- for (const line of lines) {
122
- if (known.has(lineKey(line)))
123
- continue;
124
- known.add(lineKey(line));
125
- added.push(line);
126
- }
127
- const rows = rowsOf(added).length;
128
- const said = rows ? `${rows} audit row${rows === 1 ? "" : "s"}` : `${added.length} audit${added.length === 1 ? "" : "s"} started`;
129
- return added.length ? { added, change: { by: { host: said }, writes: [{ path: AUDITS_FILE, text: auditsText([...before, ...added]) }] } } : { added };
139
+ /** The ledger's records from the AUDITS_KEPT-th newest row on, and every open started line before it (main's folder
140
+ * ledger kept the same): so it never grows past its cap. */
141
+ function trimmed(writes) {
142
+ const lines = writes.map((one) => lineOf(one.data));
143
+ const rows = lines.flatMap((line, i) => (line && line.source !== "started" ? [i] : []));
144
+ const from = rows.length > AUDITS_KEPT ? rows[rows.length - AUDITS_KEPT] : 0;
145
+ const open = new Set(openAudits(lines.filter((line) => line !== undefined)));
146
+ return writes.filter((_, i) => i >= from || (lines[i] !== undefined && open.has(lines[i])));
130
147
  }
131
148
  /** The rows the hold rule counts for `workflow` at `digest` under the promotion begun at `promotedAt` (learn/promote.ts
132
149
  * promotionStartOf; none counted for null): those with a verdict, the newest `hold_window` of them. A served run an
@@ -148,14 +165,14 @@ export function auditLineOf(rows, workflow, digest, promotedAt, loop) {
148
165
  const counted = windowOf(rows, workflow, digest, promotedAt, loop);
149
166
  return { window: loop.hold_window, disagree: loop.hold_disagree, rows: counted.length, disagreed: counted.filter((row) => row.agreed === false).length };
150
167
  }
151
- export function servingLinesOf(records, rows, loop, costStop, refused) {
152
- return servingOf(records, refused).map(({ workflow, digest }) => {
168
+ export function servingLinesOf(records, rows, loop, costStop) {
169
+ return servingOf(records).map(({ workflow, digest }) => {
153
170
  const own = records.filter((one) => one.workflow === workflow);
154
171
  const newest = own.at(-1);
155
172
  const checked = own.findLast((one) => one.check?.n !== undefined)?.check;
156
173
  const at = digest ?? newest?.digest;
157
174
  return {
158
- state: servingStateOf(records, workflow, costStop, refused),
175
+ state: servingStateOf(records, workflow, costStop),
159
176
  check: checked ? { n: checked.n, agreed: checked.agreed ?? 0, alone: checked.alone ?? 0, attempt: checked.attempt } : null,
160
177
  audit: at === undefined ? null : auditLineOf(rows, workflow, at, promotionStartOf(records, workflow, at)?.at ?? null, loop),
161
178
  };
@@ -1,4 +1,4 @@
1
- // The checker of a workflow draft a run's compile turn gives its agent's learning folder (SPEC.md): its workflow.json and
1
+ // The checker of a workflow draft a run's compile turn gives its agent's learning (SPEC.md): its workflow.json and
2
2
  // README.md, each a change against what the folder holds. Structure only, and what must never be stored; nothing the
3
3
  // checker refuses is ever written to git. Every problem it reports is printable: its path and detail are cut of the
4
4
  // host's secrets and of text shaped like one, and a field over PRINT_MAX is redacted whole.
@@ -1,13 +1,12 @@
1
- import { type Workspace } from "../workspace/index.js";
2
1
  import type { Problem } from "./check.js";
3
- import { type Snapshot } from "./folder.js";
2
+ import { type AgentLearning, type LearningRead } from "./rows.js";
4
3
  /** A run's merge, one a round: its compiled draft's files, already checked, and its owner's correction. */
5
4
  export type LearningChange = {
6
- /** The run that learned: the commit's Run-Id trailer and, with `round`, the diff file's name (runDiffKey). */
5
+ /** The run that learned, and with `round` its merge key (runDiffKey). */
7
6
  runId: string;
8
7
  /** Which of the run's merges this is (absent: its first): a follow-up's later round merges once of its own. */
9
8
  round?: number;
10
- /** Why: the commit's message and the log's `reason`. */
9
+ /** Why: the log's `reason`. */
11
10
  reason: string;
12
11
  /** What merges: a draft's files, each whole. */
13
12
  files: {
@@ -20,16 +19,15 @@ export type LearningChange = {
20
19
  refuseValues?: readonly string[];
21
20
  /** Paths written with `status: pending` (waiting for the owner's OK): a draft's README.md. */
22
21
  held?: string[];
23
- /** The commit the run started from: a file that is not on main as it was on it (a person retired or approved it,
24
- * another run's merge changed it) is a conflict. The script checks it on main as it commits. */
22
+ /** The save the run started from: a file that is not at the newest version as it was at it (a person retired or
23
+ * approved it, another run's merge changed it) is a conflict. */
25
24
  seed?: string;
26
- /** A commit the folder must still hold for the merge to land, its correction alone included (which no `same` covers):
27
- * the run's seed, when this folder held it as the run's end began (runner/learning-end.ts). A reset of the agent's
28
- * learning since removed it, so the whole merge is a conflict that says so. Absent when the run's seed was read from
29
- * another folder than this one (a Mode 2 run's end writes the folder on its disk). */
25
+ /** A save the agent's learning must still hold for the merge to land, its correction alone included (which no `same`
26
+ * covers): the run's seed, when it was held as the run's end began (runner/learning-end.ts). A reset of the agent's
27
+ * learning since removed it, so the whole merge is a conflict that says so. */
30
28
  mustHold?: string;
31
- /** A file checked against another commit than `seed`, by its path: a workflow the run's compile turn revised from
32
- * main's text, against the commit it read that text at. */
29
+ /** A file checked against another save than `seed`, by its path: a workflow the run's compile turn revised from the
30
+ * newest text, against the save it read that text at. */
33
31
  bases?: Readonly<Record<string, string>>;
34
32
  };
35
33
  export type LearningCommitResult =
@@ -50,52 +48,60 @@ export type LearningCommitResult =
50
48
  ok: false;
51
49
  reason: "refused";
52
50
  problems: Problem[];
51
+ }
52
+ /** A run's purge came after the save the run's merge was read at (its seed): that purge rewrote what the run started
53
+ * from, so its draft can't be told apart from the purged run's bytes, and nothing of it is written. */
54
+ | {
55
+ ok: false;
56
+ reason: "purged";
57
+ problems: Problem[];
53
58
  } | {
54
59
  ok: false;
55
60
  reason: "unavailable";
56
61
  error: string;
57
62
  };
58
63
  /**
59
- * A run's merge on the folder's main, one exec of the learning script: its files (a held README with `status: pending`)
60
- * and its owner's correction appended to corrections.md. `ok` with the new main and the paths the commit changed;
61
- * `conflict` when a file is not on main as it was on the run's seed; `refused` when the correction holds what must never
62
- * be stored, or, with no draft beside it, would take corrections.md past its cap (nothing written); `unavailable` when
63
- * the workspace or the exec failed. The draft and the correction are judged apart: a correction past the cap beside a
64
- * draft (the file filled by another write after the run's check, correctionRefused) is left out alone, and the draft
65
- * merges, `ok` with the cap's refusal as `refusedCorrection`. A merge whose run already merged here (this round) is `ok`
66
- * with the commit that holds it, and writes nothing: checked before the write, again on each main it is computed on, and
67
- * by the script in the commit's own ref transaction, so two calls racing for one round (a reclaimed run) merge it once.
64
+ * A run's merge on the agent's learning, one save: its files (a held README with `status: pending`) and its owner's
65
+ * correction appended to corrections.md. `ok` with the new version and the paths it changed; `conflict` when a file is
66
+ * not at the newest version as it was at the run's seed; `purged` when the merge gives files and a run's purge came after
67
+ * its seed (or a revised workflow's base), nothing written; `refused` when the correction holds what must never be stored,
68
+ * or, with no draft beside it, would take corrections.md past its cap (nothing written); `unavailable` when the store
69
+ * failed. The draft and the correction are judged apart: a correction past the cap beside a draft (the file filled by
70
+ * another write after the run's check, correctionRefused) is left out alone, and the draft merges, `ok` with the cap's
71
+ * refusal as `refusedCorrection`. A merge whose run already merged (this round) is `ok` with the save that holds it, and
72
+ * writes nothing: its merge key is taken once, in the save's own transaction, so two calls racing for one round (a
73
+ * reclaimed run) merge it once.
68
74
  */
69
- export declare function learningCommit(ws: Workspace, change: LearningChange, opts?: {
75
+ export declare function learningCommit(learning: AgentLearning, change: LearningChange, opts?: {
70
76
  retries?: number;
71
77
  }): Promise<LearningCommitResult>;
72
78
  /** Why run `runId`'s correction (`text`) would be refused on main as it is now: its line's own problems, else the cap's;
73
79
  * empty when it would be taken. Read before the run's merge, so a refused correction never holds its draft back. A
74
80
  * round (`merge`) whose merge is on main already (a stop after its commit, before the run's end was saved) is never
75
81
  * checked for room again, its own line counted twice: empty, and its merge (learningCommit) gives back what it did. */
76
- export declare function correctionRefused(ws: Workspace, text: string, refuseValues?: readonly string[], merge?: {
82
+ export declare function correctionRefused(learning: AgentLearning, text: string, refuseValues?: readonly string[], merge?: {
77
83
  runId: string;
78
84
  round?: number;
79
85
  }): Promise<Problem[]>;
80
86
  /** One line of an owner's feedback appended to corrections.md on main, by run `runId`'s merge (learningCommit's
81
87
  * `correction`, with no draft beside it). */
82
- export declare const appendCorrection: (ws: Workspace, input: {
88
+ export declare const appendCorrection: (learning: AgentLearning, input: {
83
89
  runId: string;
84
90
  round?: number;
85
91
  text: string;
86
92
  refuseValues?: readonly string[];
87
93
  }) => Promise<LearningCommitResult>;
88
- /** corrections.md as a person reads it: its text and its ETag's blob (an empty file's when main holds none). */
89
- export declare function readCorrections(snapshot: Snapshot | undefined, body: (path: string, oid: string) => Promise<string>): Promise<{
94
+ /** corrections.md as a person reads it: its text and its ETag's blob (an empty file's when it holds none). */
95
+ export declare function readCorrections(read: LearningRead): {
90
96
  text: string;
91
97
  oid: string;
92
- }>;
98
+ };
93
99
  /**
94
100
  * corrections.md replaced whole by a person: `ifMatch`, the blob they read (an ETag, quoted or not; an empty file's when
95
- * main held none), must be main's now, else a conflict. Refused, nothing written, as appendCorrection refuses. `ok` with
101
+ * the agent held none), must be the newest's now, else a conflict. Refused, nothing written, as appendCorrection refuses. `ok` with
96
102
  * the new text and its blob.
97
103
  */
98
- export declare function putCorrections(ws: Workspace, input: {
104
+ export declare function putCorrections(learning: AgentLearning, input: {
99
105
  text: string;
100
106
  ifMatch: string;
101
107
  refuseValues?: readonly string[];
@@ -108,17 +114,3 @@ export declare function putCorrections(ws: Workspace, input: {
108
114
  reason: "conflict" | "refused";
109
115
  problems: Problem[];
110
116
  }>;
111
- /**
112
- * Run `runId`'s text taken off the folder's current files, at its purge (agent-delete.ts purgeRun): first its learned
113
- * workflows (forgetRunWorkflows; `purged`: whether another run is purged), whether or not it left a diff file; then the
114
- * lines each of its merges added to corrections.md, as its diff file keeps them, dropped from main in one host commit
115
- * per merge (a line a person edited since is theirs and stays); each other merge's diff that shows such a line as
116
- * context written without it; then the run's diff files deleted, last, so a call stopped part way finds what it has
117
- * left to do. Its merges stay in the folder's git history until a learning reset or its agent's purge deletes the
118
- * folder. Idempotent: each merge's scrub lands once, nothing left to drop makes no commit, and a run with no diff file
119
- * and no workflow here is no work.
120
- * `commit`: the host's last commit, when one was made.
121
- */
122
- export declare function forgetRunText(ws: Workspace, runId: string, purged: (run: string) => Promise<boolean>): Promise<{
123
- commit?: string;
124
- }>;