@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
package/dist/sdk/check.js CHANGED
@@ -25,14 +25,13 @@ function notBaseline(inbox, reported) {
25
25
  * the check (sdkCheckTurn: kept until the check holds compile_after rows, the one that makes compile_after deciding on
26
26
  * them all); any other turn is given back (releaseSdkCheck) and the check waits for the next turn; a turn the backend
27
27
  * never completed before its inbox expired is one failed row (sdkCheckDeadline). Exact retries and reclaims compete on
28
- * the reservation. `refused`: what the purge gate refuses of the records, so the promotions trim keeps the champion its
29
- * readers serve (purged-workflow.ts refusedIn). */
28
+ * the reservation. */
30
29
  export async function settleSdkCheck(deps) {
31
30
  const { inbox } = deps;
32
31
  const check = inbox.learning_check;
33
32
  if (!check)
34
33
  return;
35
- const record = promotionOf(await promotionsOf(deps.store.settings, deps.agentId), check.workflow.name, check.digest);
34
+ const record = promotionOf(await promotionsOf(deps.store.learning, deps.agentId), check.workflow.name, check.digest);
36
35
  if (record?.decided !== "replaying" || record.via !== "sdk" || record.replay_id !== check.id || !record.check || record.commit !== check.revision || record.runs?.[0] !== inbox.run_id)
37
36
  return;
38
37
  const completion = inbox.completion;
@@ -64,8 +64,10 @@ export type SdkInbox = {
64
64
  }[];
65
65
  /** Exact promoted executable workflows delivered by the start receipt. */
66
66
  learning_workflows?: LearnedWorkflow[];
67
- /** The learning commit each of learning_workflows was read at, by name (its seed's, or its champion record's): where
68
- * the receipt asks whether a purged run wrote it (receipt). */
67
+ /** The runs whose text learning_workflows and learning_check's draft are, every file's writer (a README.md's as well
68
+ * as a workflow.json's): a receipt sent again after one of them is purged answers 410 purged (receipt). */
69
+ learning_writers?: string[];
70
+ /** Legacy: a server before learning rows wrote this (the save each workflow was read at) and no learning_writers. */
69
71
  learning_commits?: Record<string, string>;
70
72
  /** The learned workflow a learned_workflow start names, by its call's id as stored, where its input is stored withheld (a held
71
73
  * value split across its arguments, withheldParts): the parent check of a callback nested in it reads its name here
@@ -86,6 +88,14 @@ export declare const sdkRunKey: (id: string) => string;
86
88
  export declare const SDK_MAX_OPEN_MS = 86400000;
87
89
  export declare const sdkOf: (body: unknown) => SdkBinding | undefined;
88
90
  export declare function sdkInbox(store: Pick<Store, "settings">, runId: string): Promise<SdkInbox>;
91
+ /** What every SDK run's inbox holds of run `runId`'s text, gone (its purge; `agentId` the run's agent): an inbox whose
92
+ * workflows or check draft it wrote (`learning_writers` names it) keeps none of them, its writers kept, so its receipt
93
+ * answers 410 purged. An inbox that names no writers (unwritten) is emptied by the purge of any run of its own agent,
94
+ * since the run may be one of them, its learning_commits kept, so its receipt is refused still. A run of another agent
95
+ * never wrote that agent's learning, so its purge writes no row of such an inbox. Run after the run's purge committed,
96
+ * and a start checks its writers after its inbox is written (start), so one of the two always sees the other: no
97
+ * inbox keeps a purged run's workflow text. */
98
+ export declare function forgetSdkCopies(store: Pick<Store, "settings" | "runs">, agentId: string, runId: string): Promise<void>;
89
99
  /** Completion and closure compete on the same inbox. A stale runner cannot close it, and a completion that won
90
100
  * remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
91
101
  * An expiry is checked against the row it replaces: an events batch that moved the deadline keeps the inbox open. */
@@ -137,12 +147,15 @@ export declare class SdkRuns {
137
147
  workflows: LearnedWorkflow[];
138
148
  };
139
149
  }>;
140
- /** The purged run (purgeGate) that wrote a workflow of `pinned`, each read at the learning commit a start took its
141
- * text from. One read-only exec per commit. */
142
- private purgedPinned;
143
- /** The workflows a start delivers and the learning commit each was read at (`commits`: the seed's, or its champion
144
- * record's), and the drafts a check may take. */
150
+ /** The workflows a start delivers, each the seed's or its champion's text read at its record's save, and the runs whose
151
+ * text each is (`writers`: every run that wrote one of its files there, its README.md's description as well as its
152
+ * workflow.json's text); the drafts a check may take, and the runs whose text each is (`drafts`, by name). A text and
153
+ * its writer are two reads, and a purge rewrites the saves they read in place: it adds a save of its own after the
154
+ * newest (Store.learning purgeRun), so a selection a purge save moved under is read again (a normal save rewrites
155
+ * nothing it read), and one that keeps moving refuses the start, to start again. Never the purged run's text named as
156
+ * no run's. */
145
157
  private selectedWorkflows;
158
+ private selectedOnce;
146
159
  private modify;
147
160
  private writable;
148
161
  events(runId: string, body: Record<string, unknown>): Promise<{
package/dist/sdk/runs.js CHANGED
@@ -10,15 +10,15 @@ import { uuidv7 } from "../ids.js";
10
10
  import { launchBody } from "../run-launch.js";
11
11
  import { learningStartFor } from "../runs-over-store.js";
12
12
  import { labelsIssues, labelsOf } from "../run-labels.js";
13
- import { chooseWorkflows, learningFolderOf, seedAt, secretsShown, withAgentRepo } from "../learning.js";
13
+ import { chooseWorkflows, inLearningQueue, seedAt, secretsShown } from "../learning.js";
14
14
  import { INSTRUCTION_EVIDENCE_BYTES } from "../native/instructions.js";
15
15
  import { CLI_POLICY_BYTES } from "../cli-learning/policy.js";
16
16
  import { refuseWhileNoRunner } from "../runner/presence.js";
17
17
  import { secretRedactor } from "../runner/redact.js";
18
18
  import { redactedOutput, withheldWhole } from "../key-refusal.js";
19
19
  import { decodedStrings, draftProblems } from "../learn/workflow-draft.js";
20
- import { snapshotAt } from "../learn/folder.js";
21
- import { purgedRun, purgeGate, refusedIn, refusedOf } from "../purged-workflow.js";
20
+ import { learningOf } from "../learn/rows.js";
21
+ import { purgedRun, purgedRows } from "../purged-workflow.js";
22
22
  import { sdkWorkflowManifest, sdkDraftTools } from "./workflows.js";
23
23
  import { promotionOf, promotionsOf, resetAtOf } from "../learn/promote.js";
24
24
  import { championTextOf, releaseSdkCheck, reserveSdkCheck } from "../runner/promotion.js";
@@ -105,6 +105,36 @@ export async function sdkInbox(store, runId) {
105
105
  throw new ApiError("not_found", "No backend SDK run with that id.");
106
106
  return row;
107
107
  }
108
+ /** An inbox a server before learning rows wrote: it holds learning and names no writers (a start now always names them,
109
+ * none too), so whether a purged run wrote its text can't be known. Its receipt is refused (receipt). */
110
+ const unwritten = (inbox) => inbox.learning_writers === undefined && (inbox.learning_commits !== undefined || !!inbox.learning_workflows?.length || !!inbox.learning_check);
111
+ /** What every SDK run's inbox holds of run `runId`'s text, gone (its purge; `agentId` the run's agent): an inbox whose
112
+ * workflows or check draft it wrote (`learning_writers` names it) keeps none of them, its writers kept, so its receipt
113
+ * answers 410 purged. An inbox that names no writers (unwritten) is emptied by the purge of any run of its own agent,
114
+ * since the run may be one of them, its learning_commits kept, so its receipt is refused still. A run of another agent
115
+ * never wrote that agent's learning, so its purge writes no row of such an inbox. Run after the run's purge committed,
116
+ * and a start checks its writers after its inbox is written (start), so one of the two always sees the other: no
117
+ * inbox keeps a purged run's workflow text. */
118
+ export async function forgetSdkCopies(store, agentId, runId) {
119
+ for (const row of await store.settings.all({ prefix: keyOf("") })) {
120
+ for (let value = row.value, retry = 0; retry < 20; retry++) {
121
+ if (!isObject(value))
122
+ break;
123
+ const inbox = value;
124
+ if (!inbox.learning_workflows?.length && !inbox.learning_check)
125
+ break;
126
+ const legacy = unwritten(inbox);
127
+ if (!legacy && !inbox.learning_writers?.includes(runId))
128
+ break;
129
+ if (legacy && (await store.runs.get(inbox.run_id))?.agentId !== agentId)
130
+ break;
131
+ const { learning_check: _check, ...rest } = inbox;
132
+ if (await store.settings.replaceIf(row.key, value, { ...rest, learning_workflows: [], ...(legacy ? { learning_commits: inbox.learning_commits ?? {} } : {}) }))
133
+ break;
134
+ value = await store.settings.get(row.key);
135
+ }
136
+ }
137
+ }
108
138
  /** Completion and closure compete on the same inbox. A stale runner cannot close it, and a completion that won
109
139
  * remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
110
140
  * An expiry is checked against the row it replaces: an events batch that moved the deadline keeps the inbox open. */
@@ -340,7 +370,7 @@ export class SdkRuns {
340
370
  if (!cli)
341
371
  await compile.check(learning.model);
342
372
  if (learning.seedFailed)
343
- throw new ApiError("unavailable", "The learning seed could not be read. Retry after restoring the agent learning folder.");
373
+ throw new ApiError("unavailable", "The learning seed could not be read. Retry the start.");
344
374
  }
345
375
  const revision = entry.revision;
346
376
  const admit = async () => {
@@ -349,17 +379,18 @@ export class SdkRuns {
349
379
  const reset = learning ? await resetAtOf(this.store.settings, entry.agent.id) : undefined;
350
380
  if (learning && reset !== undefined && !(learning.seededAt !== undefined && learning.seededAt > reset))
351
381
  throw new ApiError("unavailable", "The agent's learning was reset while this run started: start it again.", { retry_after_ms: 1_000 });
352
- const selected = learning && !learning.noWorkflows && workflow ? await this.selectedWorkflows(entry.agent.id, learning.seed, workflow) : { workflows: [], commits: {}, candidates: [] };
382
+ const selected = learning && !learning.noWorkflows && workflow ? await this.selectedWorkflows(entry.agent.id, learning.seed, workflow) : { workflows: [], writers: new Map(), candidates: [], drafts: {} };
353
383
  const cleaned = this.clean({ instructions, ...(model ? { model } : {}) });
354
384
  const runId = uuidv7();
355
385
  const redacted = cleanedInput !== input || cleaned.instructions !== instructions;
356
386
  // A turn is a check only when it can be one's baseline: no protected value in its task or policy, and an input the
357
387
  // draft takes (sdkTakes). A prose turn whose input the client cannot resolve is given back (releaseSdkCheck).
358
388
  const takes = (flow) => !redacted && sdkTakes(flow, input, resolveSchemaForWorkflow);
359
- const checkDeps = { store: this.store, agentId: entry.agent.id, log: () => { }, shown: async () => secretsShown(this.secrets(), { local: true }), refused: refusedIn(() => learningFolderOf(this.data, entry.agent.id), this.store) };
389
+ const checkDeps = { store: this.store, agentId: entry.agent.id, log: () => { }, shown: async () => secretsShown(this.secrets(), { local: true }) };
360
390
  const check = learning ? await reserveSdkCheck(checkDeps, { runId, revision: learning.seed, learning: revisionBody.learning, flows: selected.candidates, takes }) : undefined;
361
391
  // A shadow check needs the original agent's own answer, not an answer produced by its existing champion.
362
392
  const workflows = check ? [] : selected.workflows;
393
+ const writers = [...new Set([...workflows.flatMap((flow) => selected.writers.get(flow) ?? []), ...(check ? (selected.drafts[check.workflow.name] ?? []) : [])])];
363
394
  const launch = {
364
395
  input: { content: cleanedInput },
365
396
  title: title === undefined ? cleanedInput.trim().replace(/\s+/g, " ").slice(0, 80) : this.clean({ title }).title,
@@ -381,11 +412,16 @@ export class SdkRuns {
381
412
  expires_at: now + (cli ? SDK_MAX_OPEN_MS : SDK_OPEN_MS),
382
413
  batches: [],
383
414
  learning_workflows: workflows,
384
- ...(workflows.length ? { learning_commits: selected.commits } : {}),
415
+ // Named whenever it holds learning, none too: an inbox without the key is an earlier server's (unwritten).
416
+ ...(workflows.length || check ? { learning_writers: writers } : {}),
385
417
  ...(check ? { learning_check: check } : {}),
386
418
  ...(redacted ? { check_redacted: true } : {}),
387
419
  ...(cli && cleanedInput !== input ? { evidence_incomplete: true } : {}),
388
420
  });
421
+ // A writer purged since the selection read it (the purge's own scrub of inboxes, forgetSdkCopies, runs after it
422
+ // commits, so it sees this inbox unless this read sees the purge): the start is refused, its inbox removed.
423
+ if (writers.length && (await purgedRows(this.store, writers)).size)
424
+ throw new ApiError("unavailable", "The agent's learning changed while this run started: start it again.", { retry_after_ms: 1_000 });
389
425
  await this.store.runs.create({ id: runId, agentId: entry.agent.id, revisionId: revision.id, ...(cli ? { expectedCurrent, settingsGuards: sourceGuards } : {}), ...(origin ? { origin } : {}), ...(labels ? { labels } : {}), ...(startedAt !== undefined ? { startedAt } : {}) }, { body: launchBody(launch) });
390
426
  }
391
427
  catch (error) {
@@ -393,7 +429,7 @@ export class SdkRuns {
393
429
  // The reservation comes first because the launch it is created with withholds the champion from a check's turn.
394
430
  // No turn ran, so the check waits for the next one, the release uncounted: never a hold of a draft never compared.
395
431
  if (check) {
396
- const record = promotionOf(await promotionsOf(this.store.settings, entry.agent.id).catch(() => []), check.workflow.name, check.digest);
432
+ const record = promotionOf(await promotionsOf(this.store.learning, entry.agent.id).catch(() => []), check.workflow.name, check.digest);
397
433
  if (record?.decided === "replaying" && record.replay_id === check.id)
398
434
  await releaseSdkCheck(checkDeps, record, "its start could not be admitted", false).catch(() => undefined);
399
435
  }
@@ -411,11 +447,11 @@ export class SdkRuns {
411
447
  }
412
448
  return runId;
413
449
  };
414
- // A learning start is admitted in its agent's learning queue (learning.ts withAgentRepo), the one a reset of the
450
+ // A learning start is admitted in its agent's learning queue (learning.ts inLearningQueue), the one a reset of the
415
451
  // agent's learning deletes it in: either the reset comes first, and the seed read before it is refused above, or this
416
452
  // run's row is made first, and the reset refuses while it goes (409 learning_busy). No check of a draft the reset
417
453
  // deleted is reserved, and no run starts on it.
418
- const runId = learning ? await withAgentRepo(this.data, entry.agent.id, admit) : await admit();
454
+ const runId = learning ? await inLearningQueue(this.data, entry.agent.id, admit) : await admit();
419
455
  return this.receipt(runId, consoleOrigin);
420
456
  }
421
457
  async receipt(runId, consoleOrigin) {
@@ -425,13 +461,10 @@ export class SdkRuns {
425
461
  const launch = (await this.store.runs.launch(runId)).body;
426
462
  const inbox = await sdkInbox(this.store, runId);
427
463
  const workflows = inbox.learning_workflows ?? [];
464
+ if (unwritten(inbox))
465
+ throw new ApiError("not_continuable", "This SDK start was pinned before a server upgrade, so the runs that wrote its learning can't be checked. Start a new run with a new Idempotency-Key.");
428
466
  // A start sent again answers the receipt it pinned, never a workflow a purged run wrote: 410 purged, as its file read.
429
- // Each is read at the commit it was taken from (learning_commits; the check's draft at the seed).
430
- const seed = launch.learning?.seed ?? null;
431
- const purged = seed === null ? undefined : await this.purgedPinned(run.agentId, [
432
- ...workflows.map((flow) => [flow, inbox.learning_commits?.[flow.name] ?? seed]),
433
- ...(inbox.learning_check ? [[inbox.learning_check.workflow, seed]] : []),
434
- ]);
467
+ const purged = [...(await purgedRows(this.store, inbox.learning_writers ?? [])).values()][0];
435
468
  if (purged)
436
469
  throw purgedRun(purged);
437
470
  const shown = secretsShown(this.secrets(), { local: true });
@@ -455,62 +488,66 @@ export class SdkRuns {
455
488
  },
456
489
  };
457
490
  }
458
- /** The purged run (purgeGate) that wrote a workflow of `pinned`, each read at the learning commit a start took its
459
- * text from. One read-only exec per commit. */
460
- async purgedPinned(agentId, pinned) {
461
- if (pinned.length === 0)
462
- return undefined;
463
- const folder = learningFolderOf(this.data, agentId);
464
- try {
465
- const reads = new Map();
466
- const at = (commit) => reads.get(commit) ?? reads.set(commit, snapshotAt(folder.workspace, commit).then(async (snapshot) => (await purgeGate(snapshot, this.store)).workflows)).get(commit);
467
- for (const [flow, commit] of pinned) {
468
- const row = (await at(commit)).get(flow.name);
469
- if (row)
470
- return row;
471
- }
472
- return undefined;
473
- }
474
- finally {
475
- await folder.end();
491
+ /** The workflows a start delivers, each the seed's or its champion's text read at its record's save, and the runs whose
492
+ * text each is (`writers`: every run that wrote one of its files there, its README.md's description as well as its
493
+ * workflow.json's text); the drafts a check may take, and the runs whose text each is (`drafts`, by name). A text and
494
+ * its writer are two reads, and a purge rewrites the saves they read in place: it adds a save of its own after the
495
+ * newest (Store.learning purgeRun), so a selection a purge save moved under is read again (a normal save rewrites
496
+ * nothing it read), and one that keeps moving refuses the start, to start again. Never the purged run's text named as
497
+ * no run's. */
498
+ async selectedWorkflows(agentId, revision, binding) {
499
+ const newest = () => this.store.learning.newestPurge(agentId);
500
+ for (let attempt = 0; attempt < 3; attempt++) {
501
+ const before = await newest();
502
+ const selected = await this.selectedOnce(agentId, revision, binding);
503
+ if ((await newest()) === before)
504
+ return selected;
476
505
  }
506
+ throw new ApiError("unavailable", "The agent's learning changed while this run started: start it again.", { retry_after_ms: 1_000 });
477
507
  }
478
- /** The workflows a start delivers and the learning commit each was read at (`commits`: the seed's, or its champion
479
- * record's), and the drafts a check may take. */
480
- async selectedWorkflows(agentId, revision, binding) {
481
- const folder = learningFolderOf(this.data, agentId);
482
- try {
483
- const seed = await seedAt(folder.workspace, revision, this.store);
484
- const { flows } = chooseWorkflows(seed, secretsShown(this.secrets(), { local: true }));
485
- const dsl = await import("@parcha/agentrun-dsl");
486
- const { tools, gated, deniedBy } = sdkDraftTools(binding.workflow_tools);
487
- const records = await promotionsOf(this.store.settings, agentId);
488
- let room = 256 * 1024;
489
- const compatible = (flow) => {
490
- const bytes = Buffer.byteLength(JSON.stringify(flow));
491
- if (bytes > room || draftProblems(dsl, flow.workflow, tools, gated, deniedBy, undefined, binding.workflow_adapters).length)
492
- return false;
493
- room -= bytes;
494
- return true;
495
- };
496
- const workflows = [], candidates = [], commits = {};
497
- const atCommit = async (name, commit) => chooseWorkflows(await seedAt(folder.workspace, commit, this.store), secretsShown(this.secrets(), { local: true })).flows.find((one) => one.name === name);
498
- // The champion skips a record the purge gate refuses at its commit (purged-workflow.ts refusedOf), as a claim's does.
499
- const refused = async (all, names) => refusedOf(all, names ?? all.map((one) => one.workflow), folder.workspace, this.store);
500
- for (const flow of flows) {
501
- const { text: serving, champion } = await championTextOf(records, flow, atCommit, refused);
502
- if (serving && compatible(serving)) {
503
- workflows.push(serving);
504
- commits[serving.name] = serving === flow ? seed.sha : champion.commit;
505
- }
506
- if ((!serving || workflowDigest(serving) !== workflowDigest(flow)) && compatible(flow))
507
- candidates.push(flow);
508
+ async selectedOnce(agentId, revision, binding) {
509
+ const learning = learningOf(this.store, agentId);
510
+ const reads = new Map();
511
+ const readSave = (save) => reads.get(save) ?? reads.set(save, this.store.learning.itemsAt(agentId, save)).get(save);
512
+ // Every run whose file of workflow `name` save `save` holds: its README.md's (the description delivered) as well as
513
+ // its workflow.json's (the item's run), and any other file's, so a purge of any of them refuses the receipt.
514
+ const writersAt = async (name, save) => {
515
+ const item = save === null ? undefined : (await readSave(save))?.items.find((one) => one.kind === "workflow" && one.name === name);
516
+ if (!item)
517
+ return [];
518
+ return [...new Set([item.byRun, ...Object.values(item.writers ?? {})].filter((run) => typeof run === "string"))];
519
+ };
520
+ const seed = await seedAt(learning, revision);
521
+ const { flows } = chooseWorkflows(seed, secretsShown(this.secrets(), { local: true }));
522
+ const dsl = await import("@parcha/agentrun-dsl");
523
+ const { tools, gated, deniedBy } = sdkDraftTools(binding.workflow_tools);
524
+ const records = await promotionsOf(this.store.learning, agentId);
525
+ let room = 256 * 1024;
526
+ const compatible = (flow) => {
527
+ const bytes = Buffer.byteLength(JSON.stringify(flow));
528
+ if (bytes > room || draftProblems(dsl, flow.workflow, tools, gated, deniedBy, undefined, binding.workflow_adapters).length)
529
+ return false;
530
+ room -= bytes;
531
+ return true;
532
+ };
533
+ const workflows = [], candidates = [], writers = new Map(), drafts = {};
534
+ const atSave = async (name, save) => chooseWorkflows(await seedAt(learning, save), secretsShown(this.secrets(), { local: true })).flows.find((one) => one.name === name);
535
+ for (const flow of flows) {
536
+ const { text: serving, champion } = await championTextOf(records, flow, atSave);
537
+ if (serving && compatible(serving)) {
538
+ workflows.push(serving);
539
+ const runs = await writersAt(serving.name, serving === flow ? seed.sha : champion.commit ?? null);
540
+ if (runs.length)
541
+ writers.set(serving, runs);
542
+ }
543
+ if ((!serving || workflowDigest(serving) !== workflowDigest(flow)) && compatible(flow)) {
544
+ candidates.push(flow);
545
+ const runs = await writersAt(flow.name, seed.sha);
546
+ if (runs.length)
547
+ drafts[flow.name] = runs;
508
548
  }
509
- return { workflows, commits, candidates };
510
- }
511
- finally {
512
- await folder.end();
513
549
  }
550
+ return { workflows, writers, candidates, drafts };
514
551
  }
515
552
  async modify(runId, change, limit = TOTAL_LIMIT) {
516
553
  for (let retry = 0; retry < 20; retry++) {
@@ -10,6 +10,7 @@ secretLintProfiler.setEnabled(false);
10
10
  import { keyShaped } from "./secret-name.js";
11
11
  import { secretValues } from "./api-token.js";
12
12
  import { MIN_SECRET, valueMatcher } from "./learn/secrets.js";
13
+ import { SEPARATOR } from "./redact/match.js";
13
14
  import { userInfo } from "node:os";
14
15
  import { commandReadPath, decodedTimes, inputSpan, isPlaceholder, KEY_RUN_MAX, lineBounds, lineCount, RESTORABLE, SCRUB_RULES, shapeOf, viewsOf } from "./secret-rules.js";
15
16
  export { isPlaceholder, RESTORABLE, RULE_PATTERNS, SCRUB_RULES, viewsOf } from "./secret-rules.js";
@@ -378,7 +379,9 @@ function contextOf(view, opts) {
378
379
  /** Whether a cut text holds nothing the scrub must cut: no known format the format rules still find in any view of any
379
380
  * chunk of it (each marker read as a blank, so a marker is never one), and no held value in any form (valueMatcher:
380
381
  * split by whitespace, partly encoded) in the text as written or in any decoding of it, each marker read as a
381
- * character no form holds. The time budget holds here too (a throw). */
382
+ * character no form holds and every view keeps (SEPARATOR, read with valueMatcher's `separated`: a NUL would not do,
383
+ * as the views drop control characters). A U+FFFF the text brought is read as a NUL first, nothing to the views, so
384
+ * only a marker separates. The time budget holds here too (a throw). */
382
385
  async function clean(rules, out, opts, run) {
383
386
  const blanked = out.replace(MARKER, (found) => " ".repeat(found.length));
384
387
  const formats = rules.filter((rule) => rule.kind === "format");
@@ -395,9 +398,9 @@ async function clean(rules, out, opts, run) {
395
398
  const held = heldOf(opts);
396
399
  if (!held.length)
397
400
  return true;
398
- const matches = valueMatcher(held);
399
- for (const view of viewsOf(out.replace(MARKER, "\u0000"))) {
400
- if (matches(view.source))
401
+ const matches = valueMatcher(held, { separated: true });
402
+ for (const view of viewsOf(out)) {
403
+ if (matches(view.source.replaceAll(SEPARATOR, "\u0000").replace(MARKER, SEPARATOR)))
401
404
  return false;
402
405
  inTime(run);
403
406
  }