@parcha/agentrun-serve 0.1.0-beta.41 → 0.1.0-beta.43
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.
- package/CHANGELOG.md +74 -0
- package/NOTICE +2 -0
- package/README.md +85 -19
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.js +3 -1
- package/dist/calendar/address.d.ts +49 -0
- package/dist/calendar/address.js +153 -0
- package/dist/calendar/fetch.d.ts +24 -0
- package/dist/calendar/fetch.js +146 -0
- package/dist/calendar/ics-faux.d.ts +39 -0
- package/dist/calendar/ics-faux.js +236 -0
- package/dist/calendar/labels.d.ts +3 -0
- package/dist/calendar/labels.js +61 -0
- package/dist/calendar/names.d.ts +52 -0
- package/dist/calendar/names.js +54 -0
- package/dist/calendar/parse.d.ts +108 -0
- package/dist/calendar/parse.js +625 -0
- package/dist/calendar/routes.d.ts +17 -0
- package/dist/calendar/routes.js +81 -0
- package/dist/calendar/tools.d.ts +23 -0
- package/dist/calendar/tools.js +124 -0
- package/dist/cli.js +3 -1
- package/dist/computer-record.d.ts +55 -0
- package/dist/computer-record.js +315 -0
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +15 -2
- package/dist/computers.js +2 -14
- package/dist/console/assets/index-BrcDSBD8.css +1 -0
- package/dist/console/assets/index-Bz6IWncl.js +711 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.js +0 -4
- package/dist/eval-feed.d.ts +2 -2
- package/dist/eval-feed.js +9 -5
- package/dist/feed/parse.d.ts +3 -2
- package/dist/feed/parse.js +9 -18
- package/dist/feed/pointer.d.ts +2 -0
- package/dist/feed/pointer.js +17 -0
- package/dist/file-types.js +1 -0
- package/dist/host-command.d.ts +3 -0
- package/dist/host-command.js +18 -0
- package/dist/host.d.ts +3 -0
- package/dist/host.js +3 -0
- package/dist/import-sources.js +25 -5
- package/dist/learn/audit.d.ts +123 -0
- package/dist/learn/audit.js +159 -0
- package/dist/learn/check.d.ts +14 -0
- package/dist/learn/check.js +194 -14
- package/dist/learn/compile.d.ts +75 -78
- package/dist/learn/compile.js +175 -176
- package/dist/learn/folder.d.ts +9 -0
- package/dist/learn/folder.js +13 -2
- package/dist/learn/promote.d.ts +138 -196
- package/dist/learn/promote.js +68 -184
- package/dist/learn/replay-calls.d.ts +5 -0
- package/dist/learn/replay-calls.js +12 -1
- package/dist/learn/secrets.d.ts +8 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learn/skills.js +2 -4
- package/dist/learn/workflow-nodes.d.ts +1 -2
- package/dist/learn/workflow-nodes.js +2 -3
- package/dist/learned-nodes.d.ts +9 -34
- package/dist/learned-nodes.js +11 -23
- package/dist/learned-workflow.d.ts +6 -8
- package/dist/learned-workflow.js +18 -57
- package/dist/learning-batch.d.ts +13 -5
- package/dist/learning-batch.js +34 -8
- package/dist/learning-cost.d.ts +11 -4
- package/dist/learning-cost.js +4 -2
- package/dist/learning-routes.js +1 -1
- package/dist/learning-setting.d.ts +38 -0
- package/dist/learning-setting.js +83 -0
- package/dist/learning-words.d.ts +3 -0
- package/dist/learning-words.js +3 -0
- package/dist/learning.d.ts +3 -2
- package/dist/learning.js +5 -3
- package/dist/mcp-oauth.d.ts +8 -0
- package/dist/mcp-oauth.js +7 -0
- package/dist/mcp-private-hosts.d.ts +13 -0
- package/dist/mcp-private-hosts.js +16 -0
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +195 -59
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- package/dist/run-launch.d.ts +6 -3
- package/dist/run-launch.js +2 -0
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-reads-over-store.d.ts +5 -1
- package/dist/run-reads-over-store.js +8 -4
- package/dist/run-recordings.d.ts +20 -0
- package/dist/run-recordings.js +133 -0
- package/dist/runner/adapter-loop.d.ts +5 -1
- package/dist/runner/adapter-loop.js +20 -8
- package/dist/runner/adapter-run-spec.d.ts +16 -1
- package/dist/runner/adapter-run-spec.js +36 -11
- package/dist/runner/audit.d.ts +64 -0
- package/dist/runner/audit.js +338 -0
- package/dist/runner/beside.js +2 -2
- package/dist/runner/cli-run-spec.js +25 -12
- package/dist/runner/feedback-round.d.ts +9 -25
- package/dist/runner/feedback-round.js +19 -40
- package/dist/runner/harness-loops.js +1 -15
- package/dist/runner/journal-kinds.d.ts +0 -7
- package/dist/runner/journal-kinds.js +0 -7
- package/dist/runner/learning-dispatch.d.ts +0 -16
- package/dist/runner/learning-dispatch.js +4 -47
- package/dist/runner/learning-end.d.ts +62 -13
- package/dist/runner/learning-end.js +180 -74
- package/dist/runner/learning-factory.d.ts +1 -1
- package/dist/runner/learning-factory.js +1 -4
- package/dist/runner/learning-loop.d.ts +2 -38
- package/dist/runner/learning-loop.js +8 -116
- package/dist/runner/main.d.ts +11 -0
- package/dist/runner/main.js +30 -3
- package/dist/runner/mcp-run.d.ts +2 -2
- package/dist/runner/promotion.d.ts +78 -50
- package/dist/runner/promotion.js +371 -503
- package/dist/runner/reflect-hook.d.ts +15 -0
- package/dist/runner/reflect-hook.js +28 -14
- package/dist/runner/store-secrets.js +3 -1
- package/dist/runner/workflow-first.d.ts +6 -11
- package/dist/runner/workflow-first.js +24 -25
- package/dist/runs-over-store.d.ts +1 -1
- package/dist/sdk/runs.js +2 -1
- package/dist/secrets.js +2 -2
- package/dist/server.d.ts +4 -3
- package/dist/server.js +132 -26
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/sqlite/store.js +1 -0
- package/dist/store/store.d.ts +3 -0
- package/dist/store-reads/learning.d.ts +18 -7
- package/dist/store-reads/learning.js +36 -23
- package/dist/store-reads/loop.d.ts +5 -39
- package/dist/store-reads/loop.js +21 -80
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +16 -2
- package/dist/template-eval.d.ts +7 -0
- package/dist/template-eval.js +54 -6
- package/dist/templates.d.ts +3 -1
- package/dist/templates.js +14 -1
- package/dist/timeline/computer.js +2 -1
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +11 -0
- package/dist/trigger-feed.js +67 -18
- package/dist/validate.js +7 -2
- package/dist/workspace/computer-for-claim.d.ts +7 -0
- package/dist/workspace/computer-for-claim.js +12 -2
- package/dist/workspace/paths.d.ts +4 -0
- package/dist/workspace/paths.js +9 -0
- package/dist/workspace/resolve-computer.d.ts +3 -1
- package/dist/workspace/run-tools.d.ts +1 -1
- package/dist/workspace/run-tools.js +14 -3
- package/launcher/agentrun-serve +31 -24
- package/package.json +10 -9
- package/templates/README.md +11 -2
- package/dist/console/assets/index-C3gNPz58.css +0 -1
- package/dist/console/assets/index-COGhx_2Z.js +0 -711
- package/dist/learn/general.d.ts +0 -69
- package/dist/learn/general.js +0 -75
- package/dist/learn/refit.d.ts +0 -48
- package/dist/learn/refit.js +0 -264
- package/dist/runner/step-guard.d.ts +0 -62
- package/dist/runner/step-guard.js +0 -93
package/dist/runner/promotion.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { LEARNING_LABEL } from "../learning.js";
|
|
2
|
+
import { loopOf } from "../learning-setting.js";
|
|
3
|
+
import { LEARNED_WORKFLOW_TOOL } from "../learned-workflow.js";
|
|
2
4
|
import { launchBody, readLaunchBody } from "../run-launch.js";
|
|
3
5
|
import { uuidv7 } from "../runs-over-store.js";
|
|
4
6
|
import { FINAL_STATES, RunFinal, StoreConstraint } from "../store/store.js";
|
|
5
7
|
import { openRunMcpAuth, sealRunMcpAuth } from "../store/seal.js";
|
|
6
8
|
import { isCliRecord, piShaped, runEntries, runTimeline } from "../store-reads/journal.js";
|
|
7
|
-
import { recordedCalls, replayCalls, SKIPPED_WRITE, toolClassOf, toolWord, unmarkedWords } from "../learn/replay-calls.js";
|
|
9
|
+
import { pinAnnotations, recordedCalls, replayCalls, SKIPPED_WRITE, toolClassOf, toolWord, unmarkedWords } from "../learn/replay-calls.js";
|
|
8
10
|
import { isObject } from "../validate.js";
|
|
9
|
-
import {
|
|
11
|
+
import { agentRun, lastOwnRuns, newCompileAttempt } from "../learn/compile.js";
|
|
12
|
+
import { checkPasses, checkShares, CHECK_TEXT_MAX, championOf, promotionOf, promotionsOf, recordPromotion, replayingOf, sameAnswer, skippedToolsOf } from "../learn/promote.js";
|
|
10
13
|
import { sendCommand } from "./commands.js";
|
|
11
14
|
import { ClaimRefused } from "./loop-contract.js";
|
|
12
15
|
import { readSnapshot } from "../learn/folder.js";
|
|
13
|
-
import { refitGuards, refitRowsOf, scoreRows } from "../learn/refit.js";
|
|
14
16
|
import { fits, laidFor, servedOf, workflowDigest } from "./workflow-first.js";
|
|
15
|
-
import {
|
|
16
|
-
import { recordOnly } from "../learn/promote.js";
|
|
17
|
+
import { excerptShown } from "../learn/secrets.js";
|
|
17
18
|
import { workflowsAt } from "./learning-seed.js";
|
|
18
19
|
/** A replay run still not ended this long after its replay started counts as failed (no evidence), and is cancelled once
|
|
19
20
|
* the replay is decided without it. */
|
|
@@ -21,27 +22,13 @@ export const REPLAY_DEADLINE_MS = 60 * 60 * 1000;
|
|
|
21
22
|
/** A reserved replay run still not created this long after its record was reserved is a launch that was interrupted (the
|
|
22
23
|
* reserving claim crashed, or its create failed): the next claim creates it (relaunch). Before then it is pending. */
|
|
23
24
|
export const REPLAY_LAUNCH_GRACE_MS = 5 * 60 * 1000;
|
|
24
|
-
/** The claim's promotion ports: `folder`, the agent's learning folder where the run's place keeps it, a
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* the workflow an earlier claim of this run served (`kept`, from its journal: none on a first claim). */
|
|
25
|
+
/** The claim's promotion ports: `folder`, the agent's learning folder where the run's place keeps it, a champion read
|
|
26
|
+
* from it at its record's commit, cut of the server's secrets (`shown`) as the claim's lay is; and the workflow an earlier
|
|
27
|
+
* claim of this run served (`kept`, from its journal: none on a first claim). */
|
|
28
28
|
export function claimPromotion(deps) {
|
|
29
|
-
const seed = deps.launch.learning?.seed;
|
|
30
29
|
return {
|
|
31
30
|
...deps,
|
|
32
31
|
kept: async () => (deps.runId ? servedOf(await runEntries(deps.store, deps.runId, deps.context).catch(() => [])) : undefined),
|
|
33
|
-
commit: async (files, reason, id) => {
|
|
34
|
-
const folder = deps.folder();
|
|
35
|
-
try {
|
|
36
|
-
const done = await learningCommit(folder.workspace, { kind: "merge", runId: id, reason, files, graded: {}, ...(seed ? { seed } : {}) });
|
|
37
|
-
if (!done.ok)
|
|
38
|
-
deps.log(`workflow-first: a refit could not be written (${done.reason})`);
|
|
39
|
-
return done.ok ? done.commit : null;
|
|
40
|
-
}
|
|
41
|
-
finally {
|
|
42
|
-
await folder.end();
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
32
|
champion: async (name, commit) => (await workflowsAt({ folder: deps.folder, shown: deps.shown }, commit)).find((one) => one.name === name),
|
|
46
33
|
newest: async (name) => {
|
|
47
34
|
const folder = deps.folder();
|
|
@@ -69,29 +56,27 @@ export async function replayClaimed(store, agentId, runId, launch, choice) {
|
|
|
69
56
|
const { workflow, digest } = launch.replay;
|
|
70
57
|
const record = promotionOf(await promotionsOf(store.settings, agentId), workflow, digest);
|
|
71
58
|
if (record && record.decided !== "replaying")
|
|
72
|
-
throw new ClaimRefused(`This replay of ${workflow} at ${digest} did not run: that revision is ${record.decided === "
|
|
59
|
+
throw new ClaimRefused(`This replay of ${workflow} at ${digest} did not run: that revision is ${record.decided === "promote" ? "promoted already" : "held already"}.`);
|
|
73
60
|
if (record && !record.runs?.includes(runId))
|
|
74
61
|
throw new ClaimRefused(`This replay of ${workflow} at ${digest} did not run: that revision replays again${record.replay_id ? ` (replay ${record.replay_id})` : ""}, and this run is not one of its runs.`);
|
|
75
62
|
if (choice.serve?.replay !== true || choice.serve.name !== workflow)
|
|
76
63
|
throw new ClaimRefused(`This replay of ${workflow} at ${digest} did not run: its claim laid no workflow by that name at that digest. The agent was not asked.`);
|
|
77
64
|
}
|
|
78
|
-
/** The workflows a replay run's claim lays (`laid`, the seed lay's): the one it replays
|
|
79
|
-
*
|
|
80
|
-
* the laid one is not the digest it replays (the replay then fails, never scoring another revision). Any other run's,
|
|
81
|
-
* as laid. */
|
|
65
|
+
/** The workflows a replay run's claim lays (`laid`, the seed lay's): the one it replays, or none by that name when the
|
|
66
|
+
* laid one is not the digest it replays (the replay then fails, never scoring another revision). Any other run's, as laid. */
|
|
82
67
|
export function replayLaid(launch, laid) {
|
|
83
68
|
if (!launch.replay)
|
|
84
69
|
return [...laid];
|
|
85
70
|
const { workflow, digest } = launch.replay;
|
|
86
|
-
return laid.
|
|
71
|
+
return laid.filter((one) => one.name !== workflow || workflowDigest(one) === digest);
|
|
87
72
|
}
|
|
88
73
|
/** The learned workflows a run's learned_workflow tool runs, once its first choice is made: what serves, never a
|
|
89
74
|
* revision its promotion has not promoted. The one its first choice served or the money rule stopped (laidFor: its
|
|
90
75
|
* champion in place of the laid revision) as chosen; any other the records name, its champion (championOf: the laid
|
|
91
76
|
* text when that is the champion, else its text at its record's commit), and none while nothing of it serves (none
|
|
92
|
-
* promoted yet, or the champion held): a revision replaying,
|
|
93
|
-
*
|
|
94
|
-
*
|
|
77
|
+
* promoted yet, or the champion held): a revision replaying, held or never decided is never handed to the agent to run
|
|
78
|
+
* in its place. One the records never named (no run's input fit it, so no claim asked) runs as laid. A replay run's as
|
|
79
|
+
* laid (replayLaid). The records unread: only the chosen one, as the gate leaves a run to its agent. */
|
|
95
80
|
export async function toolFlowsFor(deps, launch, laid, choice) {
|
|
96
81
|
const chosen = laidFor(laid, choice);
|
|
97
82
|
if (launch.replay || !chosen.length)
|
|
@@ -133,10 +118,10 @@ const seedOf = (deps) => (deps.launch.learning?.seed ? { commit: deps.launch.lea
|
|
|
133
118
|
const plural = (n, one) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
|
134
119
|
/** The gate workflowFirstOf asks (its `promoted`), at every run's claim, a queued run's included: the workflow that serves
|
|
135
120
|
* this run first. A run already executing keeps the revision an earlier claim of it served (keptFor). Else the laid
|
|
136
|
-
* revision (`workflow` at `digest`, the folder's revision as the run's seed holds it) has its
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
121
|
+
* revision (`workflow` at `digest`, the folder's revision as the run's seed holds it) has its check moved on (decide:
|
|
122
|
+
* started, or settled once its runs ended), and then servingNow alone says who serves: the champion (championOf), the
|
|
123
|
+
* laid text when it is the champion, its own text read from its record's commit when not; undefined when none serves
|
|
124
|
+
* (the agent takes the run). A digest's own record never says it serves. */
|
|
140
125
|
export const promotionGate = (deps) => {
|
|
141
126
|
const failed = (workflow, error) => {
|
|
142
127
|
// A promotion that could not be read or written leaves the run to its agent: never a workflow served unchecked.
|
|
@@ -155,7 +140,7 @@ export const promotionGate = (deps) => {
|
|
|
155
140
|
return failed(workflow, error);
|
|
156
141
|
}
|
|
157
142
|
};
|
|
158
|
-
// The champion alone, for a laid revision that does not take the run's input (workflowFirstOf): nothing
|
|
143
|
+
// The champion alone, for a laid revision that does not take the run's input (workflowFirstOf): nothing is checked.
|
|
159
144
|
const champion = async (workflow, digest) => {
|
|
160
145
|
try {
|
|
161
146
|
const kept = await keptFor(deps, workflow, digest);
|
|
@@ -169,12 +154,9 @@ export const promotionGate = (deps) => {
|
|
|
169
154
|
};
|
|
170
155
|
/** Who serves `workflow` now, the one answer every serving path asks: its champion (championOf, read fresh from the
|
|
171
156
|
* records), never a digest's own record, so a revision promoted before another was (or before the champion was held)
|
|
172
|
-
* never serves again. The laid revision at `digest` when it is the champion
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* with no refit (a refit written now would land over the laid revision on the folder's main). With the champion's
|
|
176
|
-
* promotion (its since), which the run keeps (WorkflowFirst.promotion). Undefined when none serves: none promoted, the
|
|
177
|
-
* champion held, or its text cannot be read (the agent takes the run). */
|
|
157
|
+
* never serves again. The laid revision at `digest` when it is the champion; `laid` false: it is not asked (the laid text
|
|
158
|
+
* does not take the input). Another champion: its text at its record's commit (its digest checked). Undefined when none
|
|
159
|
+
* serves: none promoted, the champion held, or its text cannot be read (the agent takes the run). */
|
|
178
160
|
async function servingNow(deps, workflow, digest, laid) {
|
|
179
161
|
const records = await promotionsOf(deps.store.settings, deps.agentId);
|
|
180
162
|
const champion = championOf(records, workflow.name);
|
|
@@ -184,9 +166,8 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
184
166
|
deps.log(`workflow-first: ${workflow.name} at ${digest} was promoted, but the revision promoted after it is held; no revision serves, the agent takes this run`);
|
|
185
167
|
return undefined;
|
|
186
168
|
}
|
|
187
|
-
const serving = (text, under) => (under ? { workflow: text, ...(under.since !== undefined ? { promotion: under.since } : {}) } : undefined);
|
|
188
169
|
if (champion.digest === digest)
|
|
189
|
-
return laid ?
|
|
170
|
+
return laid ? { workflow } : undefined;
|
|
190
171
|
if (!deps.champion)
|
|
191
172
|
return undefined;
|
|
192
173
|
const text = champion.commit ? await deps.champion(workflow.name, champion.commit).catch(() => undefined) : undefined;
|
|
@@ -194,11 +175,8 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
194
175
|
deps.log(`workflow-first: ${workflow.name}'s champion at ${champion.digest} could not be read from its learning commit; the agent takes this run`);
|
|
195
176
|
return undefined;
|
|
196
177
|
}
|
|
197
|
-
const under = await stillServing({ ...deps, commit: undefined }, text, champion.digest, champion);
|
|
198
|
-
if (!under)
|
|
199
|
-
return undefined;
|
|
200
178
|
deps.log(`workflow-first: ${workflow.name}'s champion at ${champion.digest} serves this run while ${digest} ${own?.decided === "promote" ? "is a revision promoted before it" : "is not promoted"}`);
|
|
201
|
-
return
|
|
179
|
+
return { workflow: text };
|
|
202
180
|
}
|
|
203
181
|
/** A run already executing keeps its revision: when an earlier claim of it served `workflow` first (its WORKFLOW_FIRST
|
|
204
182
|
* entry, answered or handed back: `deps.kept`), a re-claim serves the same digest, whoever the champion is now (the loop
|
|
@@ -210,7 +188,7 @@ async function keptFor(deps, workflow, digest) {
|
|
|
210
188
|
if (!kept || kept.workflow !== workflow.name || !kept.digest)
|
|
211
189
|
return undefined;
|
|
212
190
|
if (kept.digest !== digest) {
|
|
213
|
-
// Its newest record naming a commit
|
|
191
|
+
// Its newest record naming a commit.
|
|
214
192
|
const record = (await promotionsOf(deps.store.settings, deps.agentId)).findLast((one) => one.workflow === workflow.name && one.digest === kept.digest && one.commit);
|
|
215
193
|
const text = record?.commit && deps.champion ? await deps.champion(workflow.name, record.commit).catch(() => undefined) : undefined;
|
|
216
194
|
if (text && workflowDigest(text) === kept.digest) {
|
|
@@ -221,12 +199,10 @@ async function keptFor(deps, workflow, digest) {
|
|
|
221
199
|
deps.log(`workflow-first: ${workflow.name} at ${kept.digest} served this run on an earlier claim; it keeps that revision${kept.digest === digest ? "" : ` (its text could not be read: the laid ${digest} stands in, the kept outcome recovered as it is)`}`);
|
|
222
200
|
return workflow;
|
|
223
201
|
}
|
|
224
|
-
/** Moves the laid revision's
|
|
225
|
-
*
|
|
226
|
-
* started. It decides nothing about who serves (servingNow does). */
|
|
202
|
+
/** Moves the laid revision's check on: a new digest is checked (startCheck), a replaying one settled once its runs ended
|
|
203
|
+
* (after finishing a launch that was interrupted). It decides nothing about who serves (servingNow does). */
|
|
227
204
|
async function decide(deps, workflow, digest) {
|
|
228
205
|
const { store, agentId, log } = deps;
|
|
229
|
-
const now = deps.now ?? Date.now;
|
|
230
206
|
const record = promotionOf(await promotionsOf(store.settings, agentId), workflow.name, digest);
|
|
231
207
|
if (record?.decided === "promote")
|
|
232
208
|
return;
|
|
@@ -234,155 +210,280 @@ async function decide(deps, workflow, digest) {
|
|
|
234
210
|
log(`workflow-first: ${workflow.name} at ${digest} is held (${record.reason}); it does not serve this run`);
|
|
235
211
|
return;
|
|
236
212
|
}
|
|
237
|
-
if (record?.decided === "skipped") {
|
|
238
|
-
log(`workflow-first: ${workflow.name} at ${digest} was skipped (${record.reason}); it does not serve this run`);
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
213
|
if (record?.decided === "replaying") {
|
|
242
214
|
await relaunch(deps, workflow, digest, record);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
log(`workflow-first: ${workflow.name} at ${digest} is replaying on ${plural(record.runs?.length ?? 0, "graded input")}; it does not serve this run`);
|
|
215
|
+
if (!(await settleCheck(deps, workflow, digest, record)))
|
|
216
|
+
log(`workflow-first: ${workflow.name} at ${digest} is being checked on ${record.runs?.length ?? 0} of the agent's inputs; it does not serve this run`);
|
|
246
217
|
return;
|
|
247
218
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
219
|
+
return startCheck(deps, workflow, digest);
|
|
220
|
+
}
|
|
221
|
+
/** The loop's numbers (learning-setting.ts) of the revision the claim runs: its definition's `learning`, each one it
|
|
222
|
+
* leaves out at its default (a revision that cannot be read: every default). */
|
|
223
|
+
async function loopOfRevision(deps) {
|
|
224
|
+
try {
|
|
225
|
+
const revision = await deps.store.agents.revision(deps.revisionId);
|
|
226
|
+
return loopOf(revision?.body?.learning);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return loopOf(undefined);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** Which check of a compile a new revision's is: 2 when the workflow's newest record of another revision is a first
|
|
233
|
+
* check's hold or an audit's hold, or a first compile's failed draft (this revision is its one recompile), else 1. A
|
|
234
|
+
* stop with nothing checked (stopUnchecked) is no first check's hold: it spends no recompile. A workflow's first
|
|
235
|
+
* revision after a first compile whose drafts failed under another name (learn/compile.ts newCompileAttempt) is that
|
|
236
|
+
* compile's recompile too. */
|
|
237
|
+
function attemptOf(records, name, digest) {
|
|
238
|
+
const before = records.findLast((one) => one.workflow === name && one.digest !== digest);
|
|
239
|
+
if (!before)
|
|
240
|
+
return newCompileAttempt(records.filter((one) => one.digest !== digest));
|
|
241
|
+
if (before.decided !== "hold")
|
|
242
|
+
return 1;
|
|
243
|
+
return before.audit ? (before.audit.stopped ? 1 : 2) : before.check?.attempt === 1 && !before.check.stopped ? 2 : 1;
|
|
244
|
+
}
|
|
245
|
+
/** The shadow runs a recompile's check runs on beside the agent's last runs: the disagreeing audit rows the workflow's
|
|
246
|
+
* newest audit hold names (a grade's row has no answer of the agent's to check against), when that hold is the
|
|
247
|
+
* workflow's newest record of another revision. */
|
|
248
|
+
function auditInputsOf(records, name, digest) {
|
|
249
|
+
const before = records.findLast((one) => one.workflow === name && one.digest !== digest);
|
|
250
|
+
if (before?.decided !== "hold" || !before.audit || before.audit.stopped)
|
|
251
|
+
return [];
|
|
252
|
+
return before.audit.disagreements.filter((one) => !one.graded && one.agent !== null).map((one) => one.run);
|
|
253
|
+
}
|
|
254
|
+
/** An agent run a check may read its input from: a completed run of its own (agentRun: no learned workflow answered it
|
|
255
|
+
* alone, and it is no replay), neither archived nor imported, and never the claimed run (lastOwnRuns' own rule). */
|
|
256
|
+
const checkable = (deps, row) => row.id !== deps.runId && agentRun(row) && row.archivedAt === null && !row.importedFrom;
|
|
257
|
+
/** The inputs a check runs its draft on: the agent's runs `pick.of` names (a check's own, asked again; each still
|
|
258
|
+
* checkable), then the agent's last `pick.n` completed runs of its own (lastOwnRuns: never the claimed run, an archived
|
|
259
|
+
* or imported one), newest first, picked before anything else limits them: runs its workflow served since, however many,
|
|
260
|
+
* never push them out of the read. And `audited`: an audit hold's disagreeing shadow runs (auditInputsOf), each ended.
|
|
261
|
+
* Of those, the ones whose input `workflow` takes (fits), each with its launch read back; one with an uploaded file is
|
|
262
|
+
* never an input. With how many of the agent's runs it looked at (`looked`) and why each it looked at is no input
|
|
263
|
+
* (`unfit`, `files`). */
|
|
264
|
+
async function checkInputs(deps, workflow, pick, audited = []) {
|
|
265
|
+
const { resolveSchemaForWorkflow } = await import("@parcha/agentrun-dsl");
|
|
266
|
+
const named = (await Promise.all(pick.of.map((id) => deps.store.runs.get(id)))).filter((row) => row !== undefined && checkable(deps, row));
|
|
267
|
+
const last = pick.n ? await lastOwnRuns(deps.store, deps.agentId, pick.n + pick.of.length, deps.runId) : [];
|
|
268
|
+
const own = [...named, ...last.filter((row) => !pick.of.includes(row.id)).slice(0, pick.n)];
|
|
269
|
+
// And an audit hold's disagreeing shadow runs: the agent's answers the recompile must say too.
|
|
270
|
+
for (const id of audited) {
|
|
271
|
+
const row = await deps.store.runs.get(id);
|
|
272
|
+
if (row && row.state === "ended" && !own.some((one) => one.id === id))
|
|
273
|
+
own.push(row);
|
|
274
|
+
}
|
|
275
|
+
const out = [];
|
|
276
|
+
let files = 0;
|
|
277
|
+
for (const row of own) {
|
|
278
|
+
const launch = await deps.store.runs.launch(row.id).then((one) => readLaunchBody(one.body), () => undefined);
|
|
279
|
+
if (launch?.files?.length)
|
|
280
|
+
files += 1;
|
|
281
|
+
if (!launch || launch.files?.length || launch.replay || !fits(workflow, launch.input, resolveSchemaForWorkflow))
|
|
282
|
+
continue;
|
|
283
|
+
out.push({ run: row.id, launch });
|
|
284
|
+
}
|
|
285
|
+
return { inputs: out, looked: own.length, unfit: own.length - out.length - files, files };
|
|
286
|
+
}
|
|
287
|
+
/** Starts a new revision's one check: a replay run of it on each input of the agent's last compile_after completed runs
|
|
288
|
+
* (checkInputs), at workflow cost, the agent taking the runs meanwhile; reserved with the bar its definition says now
|
|
289
|
+
* (CheckBar) and only while no other revision of the workflow is replaying (one at a time). A later claim, or the
|
|
290
|
+
* replay's read, decides it once its runs ended (settleCheck). With no input it takes (a workflow landed before any run
|
|
291
|
+
* it fits), it is decided at once: never promoted, it stops with nothing checked (stopUnchecked). */
|
|
292
|
+
async function startCheck(deps, workflow, digest) {
|
|
293
|
+
const { store, agentId, log } = deps;
|
|
294
|
+
// Only the learning folder's newest revision is checked: a run pinned to an older commit lays an older text.
|
|
295
|
+
const newest = deps.newest ? await deps.newest(workflow.name) : undefined;
|
|
296
|
+
if (newest !== undefined && newest !== digest) {
|
|
297
|
+
log(`workflow-first: ${workflow.name} at ${digest} is not the learning folder's newest revision of it (${newest}); it is not checked and does not serve this run`);
|
|
295
298
|
return;
|
|
296
299
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
300
|
+
// One check at a time, and one at work finishes: decided here once its runs ended (its verdict stands), never
|
|
301
|
+
// cancelled. While it runs, this revision waits, recording nothing: the claim after it is decided starts this one.
|
|
302
|
+
for (const ahead of replayingOf(await promotionsOf(store.settings, agentId), workflow.name, digest))
|
|
303
|
+
await settleAhead(deps, workflow.name, ahead);
|
|
304
|
+
const records = await promotionsOf(store.settings, agentId);
|
|
305
|
+
const rival = replayingOf(records, workflow.name, digest)[0];
|
|
306
|
+
if (rival) {
|
|
307
|
+
log(`workflow-first: ${workflow.name} at ${digest} waits: ${rival.digest} is being checked (one check at a time); it does not serve this run`);
|
|
301
308
|
return;
|
|
302
309
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
* and the stale claim's write is refused. A refused claim that was the newest after all (two read the same queue)
|
|
315
|
-
* changes nothing either; the next claim of the newest revision, which every new run lays, makes the change. */
|
|
316
|
-
const sameQueue = (items, seen, name, digest) => queueOf(items, name, digest) === queueOf(seen, name, digest);
|
|
317
|
-
/** Queues `workflow` at `digest`, the newest revision, behind `running`, the replay at work (a record of it already
|
|
318
|
-
* queued is left as it is), and skips each revision queued before it (only the newest queued one replays next), in one
|
|
319
|
-
* guarded write: kept only while its own record is still the one read and the queue is still `seen` (sameQueue). */
|
|
320
|
-
async function enqueue(deps, workflow, digest, record, running, seen) {
|
|
321
|
-
const { store, agentId, log } = deps;
|
|
322
|
-
const now = deps.now ?? Date.now;
|
|
323
|
-
const behind = `${running.digest}'s replay on ${plural(running.runs?.length ?? 0, "graded input")}`;
|
|
324
|
-
const reason = `queued behind ${behind}: a running replay finishes; the newest revision queued meanwhile replays next`;
|
|
325
|
-
const older = replayingOf(seen, workflow.name, digest, "queued");
|
|
326
|
-
const writes = [...(record ? [] : [{ workflow: workflow.name, digest, at: iso(now()), decided: "queued", source: "replay", reason, ...seedOf(deps) }]), ...older.map((one) => skippedFor(deps, one, digest))];
|
|
327
|
-
if (!writes.length) {
|
|
328
|
-
log(`workflow-first: ${workflow.name} at ${digest} is queued behind ${behind}; it does not serve this run`);
|
|
310
|
+
const loop = await loopOfRevision(deps);
|
|
311
|
+
const bar = { compile_after: loop.compile_after, agree_min: loop.agree_min, alone_min: loop.alone_min, attempt: attemptOf(records, workflow.name, digest) };
|
|
312
|
+
const { inputs, looked, unfit, files } = await checkInputs(deps, workflow, { of: [], n: loop.compile_after }, auditInputsOf(records, workflow.name, digest));
|
|
313
|
+
if (!inputs.length) {
|
|
314
|
+
// Nothing of the agent's to check it on (a workflow landed before any run it fits): never promoted unchecked. It
|
|
315
|
+
// stops, its record naming why (the compile's draft error), and asks no recompile: a revision would have nothing to be
|
|
316
|
+
// checked on either.
|
|
317
|
+
const why = !looked
|
|
318
|
+
? "the agent has no completed run of its own to check it against"
|
|
319
|
+
: `none of the agent's last ${plural(looked, "completed run")} has an input it takes${files ? ` (${files} came with an uploaded file, which a check never replays${unfit ? `; ${unfit} do not fit its input` : ""})` : ""}`;
|
|
320
|
+
await stopUnchecked(deps, workflow.name, digest, bar, why, (was) => was === undefined);
|
|
329
321
|
return;
|
|
330
322
|
}
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
323
|
+
const said = `checking it on ${plural(inputs.length, "input")} of the agent's last ${plural(bar.compile_after, "completed run")} against its own answers`;
|
|
324
|
+
const reserved = await reserve(deps, workflow, digest, inputs, said, (was) => was === undefined, bar);
|
|
325
|
+
if (!reserved.won) {
|
|
326
|
+
log(`workflow-first: ${workflow.name} at ${digest}: another claim's decision on it came first; it does not serve this run`);
|
|
334
327
|
return;
|
|
335
328
|
}
|
|
336
|
-
log(`workflow-first: ${workflow.name} at ${digest} is
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
*
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
async function settleRival(deps, name, running) {
|
|
361
|
-
const text = running.commit && deps.champion ? await deps.champion(name, running.commit).catch(() => undefined) : undefined;
|
|
362
|
-
if (!text || workflowDigest(text) !== running.digest) {
|
|
363
|
-
deps.log(`workflow-first: ${name} at ${running.digest} replays, and its text could not be read from its learning commit to decide it at this claim`);
|
|
329
|
+
log(`workflow-first: ${workflow.name} at ${digest} is new: ${said} before it serves`);
|
|
330
|
+
}
|
|
331
|
+
/** The words a stopped check's record starts with when it had nothing to check its draft on. */
|
|
332
|
+
export const NOTHING_TO_CHECK = "nothing to check it on";
|
|
333
|
+
/** Stops a draft its check had nothing to run on (`why`: no input of the agent's it takes, or every input skipped at a
|
|
334
|
+
* write): held, never promoted unchecked, its record `stopped` (nothing compiles again until a hold or its owner acts)
|
|
335
|
+
* and naming the cause, which the compile's draft errors show (store-reads/loop.ts). It asks no recompile: a revision
|
|
336
|
+
* would have nothing to be checked on either, and a stop with nothing checked never counts as a compile's first check
|
|
337
|
+
* (attemptOf). Kept only while `still` holds of the digest's record (the one write). */
|
|
338
|
+
async function stopUnchecked(deps, name, digest, bar, why, still, check = {}, extra = {}) {
|
|
339
|
+
const reason = `${NOTHING_TO_CHECK}: ${why}. It is not promoted, and it stops: nothing compiles again until a hold or its owner acts`;
|
|
340
|
+
const decided = await recordPromotion(deps.store.settings, deps.agentId, { workflow: name, digest, at: iso((deps.now ?? Date.now)()), decided: "hold", reason, check: { ...bar, n: 0, agreed: 0, alone: 0, of: [], ...check, stopped: true }, ...seedOf(deps), ...extra }, still);
|
|
341
|
+
if (decided.won)
|
|
342
|
+
deps.log(`workflow-first: ${name} at ${digest} stopped: ${reason}`);
|
|
343
|
+
return decided;
|
|
344
|
+
}
|
|
345
|
+
/** Decides `ahead`, the check at work for a revision older than the laid (newest) one, as a claim laying it would
|
|
346
|
+
* (relaunch, then settleCheck) once its runs ended: its text read at its record's commit, its verdict standing (promote
|
|
347
|
+
* or hold) though a newer revision waits. Left replaying while a run of it is at work, or when its text cannot be read
|
|
348
|
+
* (its read, replayView, decides it then). */
|
|
349
|
+
async function settleAhead(deps, name, ahead) {
|
|
350
|
+
const text = ahead.commit && deps.champion ? await deps.champion(name, ahead.commit).catch(() => undefined) : undefined;
|
|
351
|
+
if (text === undefined || workflowDigest(text) !== ahead.digest) {
|
|
352
|
+
deps.log(`workflow-first: ${name} at ${ahead.digest} is being checked, and its text could not be read from its learning commit to decide it at this claim`);
|
|
364
353
|
return;
|
|
365
354
|
}
|
|
366
|
-
|
|
367
|
-
await
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
355
|
+
await relaunch(deps, text, ahead.digest, ahead);
|
|
356
|
+
await settleCheck(deps, text, ahead.digest, ahead);
|
|
357
|
+
}
|
|
358
|
+
/** A check's rows, once every replay run ended (or REPLAY_DEADLINE_MS passed); undefined while one is at work or not
|
|
359
|
+
* created yet. One row per reserved input, so every one counts: a run not ended by the deadline failed, and so did one
|
|
360
|
+
* never created by then (`unstarted`: the server stopped after reserving it, say), its input read from the agent's run
|
|
361
|
+
* the record names for it. With what its runs cost, the rows' outcomes (one per row, the never created failed), and how
|
|
362
|
+
* many were never created by the deadline.
|
|
363
|
+
* No grade is read: the label is the agent's own recorded answer. */
|
|
364
|
+
async function checkRows(deps, record) {
|
|
365
|
+
const { store, context } = deps;
|
|
366
|
+
const runs = await Promise.all((record.runs ?? []).map((id) => store.runs.get(id)));
|
|
367
|
+
const late = (deps.now ?? Date.now)() - Date.parse(record.at) > REPLAY_DEADLINE_MS;
|
|
368
|
+
if (!late && runs.some((row) => !row || !FINAL_STATES.includes(row.state)))
|
|
369
|
+
return undefined;
|
|
370
|
+
const rows = [];
|
|
371
|
+
for (const [i, run] of runs.entries()) {
|
|
372
|
+
const reservedOf = record.inputs?.[i];
|
|
373
|
+
const launch = run ? await store.runs.launch(run.id).then((one) => readLaunchBody(one.body), () => undefined) : undefined;
|
|
374
|
+
const replayed = launch?.replay?.of;
|
|
375
|
+
if (!run || !launch || !replayed) {
|
|
376
|
+
// Never created (or its launch unreadable): a failed input, never left out of n.
|
|
377
|
+
const of = reservedOf;
|
|
378
|
+
const from = of ? await store.runs.launch(of).then((one) => readLaunchBody(one.body), () => undefined) : undefined;
|
|
379
|
+
rows.push({ of: of ?? record.runs[i], input: from ? inputText(from.input) : "", agent: of ? await answerOf(store, of, context) : null, outcome: "failed", answer: null, ...(run ? {} : { unstarted: true }) });
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
const outcome = outcomeOf(run);
|
|
383
|
+
const stoppedAt = outcome === "skipped_write" ? metricsOf(run).replay?.skipped : undefined;
|
|
384
|
+
rows.push({
|
|
385
|
+
of: replayed, input: inputText(launch.input), agent: await answerOf(store, replayed, context), outcome,
|
|
386
|
+
answer: outcome === "answered" ? await answerOf(store, run.id, context) : null,
|
|
387
|
+
...(outcome === "handed_back" ? { escalated: (await handedBackOf(store, run.id, context)) ?? "" } : {}),
|
|
388
|
+
...(stoppedAt ? { skipped: stoppedAt } : {}),
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
// The outcomes are the rows', so they add up to its n: a reserved run never created is a failed input there too.
|
|
392
|
+
const outcomes = { ...NO_OUTCOMES };
|
|
393
|
+
for (const row of rows)
|
|
394
|
+
outcomes[row.outcome] += 1;
|
|
395
|
+
return { rows, cost: replayCost(runs, late), outcomes, unstarted: rows.filter((row) => row.unstarted).length };
|
|
396
|
+
}
|
|
397
|
+
/** A run's input as a check's disagreement shows it: its text, else its JSON. */
|
|
398
|
+
const inputText = (input) => (typeof input === "string" ? input : JSON.stringify(input) ?? "");
|
|
399
|
+
/** What a replay run's workflow said when it handed its input back: its learned_workflow call's result. */
|
|
400
|
+
async function handedBackOf(store, runId, context) {
|
|
401
|
+
try {
|
|
402
|
+
const rows = await runTimeline(store, runId, context, { settled: true, caps: { text: Number.POSITIVE_INFINITY } });
|
|
403
|
+
return rows.findLast((row) => row.kind === "tool_result" && row.tool_name === LEARNED_WORKFLOW_TOOL)?.tool_result_preview ?? undefined;
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
371
408
|
}
|
|
372
|
-
/**
|
|
409
|
+
/** A replaying check's decision, once its replay runs ended (checkRows); undefined while one is at work. */
|
|
410
|
+
async function settleCheck(deps, workflow, digest, record) {
|
|
411
|
+
const { name } = workflow;
|
|
412
|
+
const read = await checkRows(deps, record);
|
|
413
|
+
if (!read)
|
|
414
|
+
return undefined;
|
|
415
|
+
const bar = record.check ?? { ...(await loopOfRevision(deps)), attempt: 1 };
|
|
416
|
+
const decided = await decideCheck(deps, name, digest, bar, read, (was) => sameReplay(was, record), { ...(record.runs ? { runs: record.runs } : {}), ...(record.replay_id ? { replay_id: record.replay_id } : {}), ...(record.commit ? { commit: record.commit } : {}) });
|
|
417
|
+
if (decided.won) {
|
|
418
|
+
const cancelled = await cancelUnended(deps, record);
|
|
419
|
+
if (cancelled)
|
|
420
|
+
deps.log(`workflow-first: ${name} at ${digest}: ${plural(cancelled, "replay run")} still at work past the check's deadline cancelled`);
|
|
421
|
+
}
|
|
422
|
+
return decided.record?.decided === "replaying" ? undefined : decided.record;
|
|
423
|
+
}
|
|
424
|
+
/** Decides a check on its rows: each input it ran on (a skipped write is none) agreed when the draft answered it alone
|
|
425
|
+
* with the agent's recorded answer (learn/promote.ts sameAnswer), alone when it answered it with no escalate and no
|
|
426
|
+
* failed step. Promoted when it clears the bar (checkPasses); else held with the inputs it did not agree on, which the
|
|
427
|
+
* agent's next run's compile turn reads as data (its one recompile); a recompile's check that holds it too stops:
|
|
428
|
+
* nothing compiles again. No grade, refit, queue or second replay. Kept only while `still` holds of the digest's record
|
|
429
|
+
* (the one write: of two claims at once, one decides). */
|
|
430
|
+
export async function decideCheck(deps, name, digest, bar, read, still, extra) {
|
|
431
|
+
const now = deps.now ?? Date.now;
|
|
432
|
+
const ran = read.rows.filter((row) => row.outcome !== "skipped_write");
|
|
433
|
+
const alone = (row) => row.outcome === "answered";
|
|
434
|
+
const agrees = (row) => alone(row) && sameAnswer(row.answer, row.agent);
|
|
435
|
+
const score = { n: ran.length, agreed: ran.filter(agrees).length, alone: ran.filter(alone).length };
|
|
436
|
+
const ok = checkPasses(score, bar);
|
|
437
|
+
// Each text is cut of the host's secrets whole, then shortened (learn/secrets.ts excerptShown).
|
|
438
|
+
const shown = await deps.shown();
|
|
439
|
+
const cut = (text) => excerptShown(text, shown, CHECK_TEXT_MAX);
|
|
440
|
+
const disagreements = ran.filter((row) => !agrees(row)).map((row) => ({
|
|
441
|
+
run: row.of, input: cut(row.input), agent: row.agent === null ? null : cut(row.agent),
|
|
442
|
+
...(row.outcome === "answered" ? { draft: cut(row.answer ?? "") } : row.outcome === "handed_back" ? { escalated: cut(row.escalated ?? "") } : { failed: true }),
|
|
443
|
+
}));
|
|
444
|
+
const stopped = !ok && bar.attempt === 2;
|
|
445
|
+
const pct = (value) => `${(value * 100).toFixed(1)}%`;
|
|
446
|
+
const shares = checkShares(score);
|
|
447
|
+
// Inputs stopped at a write the recording lacks are no evidence either way; one stopped at an MCP tool its server listed
|
|
448
|
+
// with no readOnlyHint names that tool (skipWordsOf), never a data problem.
|
|
449
|
+
const tools = skippedToolsOf(read.rows);
|
|
450
|
+
const { skipped_write: skipped, failed } = read.outcomes;
|
|
451
|
+
const { hint, why: skipWhy } = skipWordsOf(tools, skipped);
|
|
452
|
+
const numbers = { of: read.rows.map((row) => row.of), cost_usd: read.cost, outcomes: read.outcomes, ...(read.unstarted ? { unstarted: read.unstarted } : {}), ...(tools.length ? { skipped_tools: tools } : {}) };
|
|
453
|
+
// Every input skipped at a write (each reserved input counts: one never created or never ended failed, so this is the
|
|
454
|
+
// only way to none): nothing to check it on, so it stops, never promoted unchecked, and asks no recompile.
|
|
455
|
+
if (!score.n) {
|
|
456
|
+
const why = skipped ? `${plural(skipped, "input")} skipped because ${hint ? skipWhy : `each ${SKIPPED_WRITE} (a check never runs a write)`}` : "it ran on no input";
|
|
457
|
+
return stopUnchecked(deps, name, digest, bar, why, still, numbers, extra);
|
|
458
|
+
}
|
|
459
|
+
// A run never created is a failed outcome too, said once: as never started.
|
|
460
|
+
const ranFailed = failed - read.unstarted;
|
|
461
|
+
const aside = [...(ranFailed ? [`${ranFailed} failed: a step failed, or it was still at work at the check's deadline`] : []), ...(skipped ? [`${skipped} skipped: ${skipWhy}`] : []), ...(read.unstarted ? [`${read.unstarted} never started by the check's deadline (the server stopped after reserving it, say), each a failed input`] : [])].join("; ");
|
|
462
|
+
// Every input reserved and none started (a launch interrupted before its first run existed): each counts failed in n,
|
|
463
|
+
// and the words say none ran.
|
|
464
|
+
const said = read.unstarted === score.n
|
|
465
|
+
? `none of its ${plural(score.n, "check run")} started by the check's deadline (its launch was interrupted: the server stopped after reserving it, say): nothing was checked`
|
|
466
|
+
: `checked on ${score.n} of the agent's inputs: answered ${score.alone} alone (${pct(shares.alone)}, the bar ${pct(bar.alone_min)}), and ${score.agreed} of those said what the agent said (${pct(shares.agreement)}, the bar ${pct(bar.agree_min)})${aside ? ` (${aside})` : ""}`;
|
|
467
|
+
const reason = ok ? `${said}: promoted` : stopped ? `${said}: held, and this was the recompile's check, so it stops: nothing compiles again until a hold or its owner acts` : `${said}: held; the agent's next run recompiles it once with the inputs it did not agree on`;
|
|
468
|
+
const check = { ...bar, ...score, ...numbers, ...(disagreements.length ? { disagreements } : {}), ...(stopped ? { stopped: true } : {}) };
|
|
469
|
+
const decided = await recordPromotion(deps.store.settings, deps.agentId, { workflow: name, digest, at: iso(now()), decided: ok ? "promote" : "hold", reason, check, ...seedOf(deps), ...extra }, still);
|
|
470
|
+
if (decided.won)
|
|
471
|
+
deps.log(`workflow-first: ${name} at ${digest} ${ok ? "promoted" : "held"}: ${reason}`);
|
|
472
|
+
return decided;
|
|
473
|
+
}
|
|
474
|
+
/** Reserves a check of `workflow` at `digest` on `inputs`, then starts its runs: its replaying record names the replay
|
|
373
475
|
* runs it will start (their ids, and the replay's own, minted here), kept only when `still` holds of the digest's record
|
|
374
476
|
* now and no other revision of the workflow replays, in the one guarded write. Only the writer that kept it starts them:
|
|
375
|
-
* a claim of the same digest, or of another revision, or a person's ask, at once, starts none. A claim's (
|
|
376
|
-
* person's (askReplay) are this one reservation.
|
|
377
|
-
|
|
378
|
-
async function reserve(deps, workflow, digest, graded, reason, still, owed = [], also = []) {
|
|
477
|
+
* a claim of the same digest, or of another revision, or a person's ask, at once, starts none. A claim's (startCheck) and
|
|
478
|
+
* a person's (askReplay) are this one reservation. */
|
|
479
|
+
async function reserve(deps, workflow, digest, inputs, reason, still, check) {
|
|
379
480
|
const now = deps.now ?? Date.now;
|
|
380
|
-
const ids =
|
|
481
|
+
const ids = inputs.map(() => uuidv7());
|
|
381
482
|
const launch = replayLaunchOf(deps);
|
|
382
|
-
const reserved = await recordPromotion(deps.store.settings, deps.agentId, { workflow: workflow.name, digest, at: iso(now()), decided: "replaying",
|
|
483
|
+
const reserved = await recordPromotion(deps.store.settings, deps.agentId, { workflow: workflow.name, digest, at: iso(now()), decided: "replaying", reason, runs: ids, inputs: inputs.map((one) => one.run), replay_id: uuidv7(), launch, ...seedOf(deps), check }, (was, items) => still(was) && replayingOf(items, workflow.name, digest).length === 0);
|
|
383
484
|
if (reserved.won)
|
|
384
|
-
for (const [i, one] of
|
|
385
|
-
await startReplay(deps, workflow, digest, one.
|
|
485
|
+
for (const [i, one] of inputs.entries())
|
|
486
|
+
await startReplay(deps, workflow, digest, one.run, one.launch, ids[i], launch);
|
|
386
487
|
return reserved;
|
|
387
488
|
}
|
|
388
489
|
/** The launch a replay's runs all run with (PromotionRecord.launch), chosen once by the claim or person reserving it:
|
|
@@ -414,7 +515,7 @@ function replayLaunchOf(deps) {
|
|
|
414
515
|
* be replayed again), are not: an older replay's late decision never lands over a newer one. */
|
|
415
516
|
const sameReplay = (was, record) => was?.decided === "replaying" && was.replay_id === record.replay_id;
|
|
416
517
|
/** Cancels each of `record`'s replay runs not ended, as a person's cancel is (an in-flight one stops, what it spent booked
|
|
417
|
-
* on its row): once its
|
|
518
|
+
* on its row): once its check is decided with runs still at work (past its deadline). How many were sent their cancel. The
|
|
418
519
|
* record is the durable half: a cancel that is lost (a crash after the record, a write that fails) leaves a run whose
|
|
419
520
|
* claim refuses it (replayClaimed: its revision no longer replays), so each cancel is tried and a failed one only logged. */
|
|
420
521
|
async function cancelUnended(deps, record) {
|
|
@@ -435,58 +536,6 @@ async function cancelUnended(deps, record) {
|
|
|
435
536
|
}
|
|
436
537
|
return cancelled;
|
|
437
538
|
}
|
|
438
|
-
/** The paths a graded run a replay runs on took: the agent's (`agent`, or `handed_back`: the agent finished it), or a
|
|
439
|
-
* learned workflow's alone (`workflow`). Each has a recorded answer (the run's final answer, whoever wrote it) and its
|
|
440
|
-
* grade, which is what a replay scores against (answerCorrect); the path says only whose answer it was (agentRight: on a
|
|
441
|
-
* `workflow` run graded wrong the agent counts right); a replay run never is one. */
|
|
442
|
-
const REPLAYABLE = new Set(["agent", "handed_back", "workflow"]);
|
|
443
|
-
/** The agent's graded runs a replay runs on (REPLAYABLE), newest first: the runs `pick.of` names, then the newest
|
|
444
|
-
* `pick.n` (a run both name is taken once). Each with a launch read back, no uploaded file, an input `workflow` fits,
|
|
445
|
-
* never the run being claimed, nor an archived or imported one. */
|
|
446
|
-
async function gradedInputs(deps, workflow, pick) {
|
|
447
|
-
const { resolveSchemaForWorkflow } = await import("@parcha/agentrun-dsl");
|
|
448
|
-
const out = [];
|
|
449
|
-
const of = new Set(pick.of);
|
|
450
|
-
let newest = 0;
|
|
451
|
-
let named = 0;
|
|
452
|
-
const grades = (await gradesOf(deps.store.settings, deps.agentId)).filter((grade) => REPLAYABLE.has(grade.path));
|
|
453
|
-
for (const grade of grades.reverse()) {
|
|
454
|
-
if (newest >= pick.n && named >= of.size)
|
|
455
|
-
break;
|
|
456
|
-
const asked = of.has(grade.run);
|
|
457
|
-
if ((!asked && newest >= pick.n) || grade.run === deps.runId)
|
|
458
|
-
continue;
|
|
459
|
-
const row = await deps.store.runs.get(grade.run);
|
|
460
|
-
if (!row || row.archivedAt !== null || row.importedFrom)
|
|
461
|
-
continue;
|
|
462
|
-
const launch = await deps.store.runs.launch(grade.run).then((one) => readLaunchBody(one.body), () => undefined);
|
|
463
|
-
if (!launch || launch.files?.length || launch.replay || !fits(workflow, launch.input, resolveSchemaForWorkflow))
|
|
464
|
-
continue;
|
|
465
|
-
out.push({ grade, launch });
|
|
466
|
-
if (asked)
|
|
467
|
-
named += 1;
|
|
468
|
-
if (newest < pick.n)
|
|
469
|
-
newest += 1;
|
|
470
|
-
}
|
|
471
|
-
return out;
|
|
472
|
-
}
|
|
473
|
-
/** What a replay of a revision owes before it may promote it: the served runs graded wrong it was held on
|
|
474
|
-
* (PromotionRecord.held_on), none when `record` owes none. Its replay puts each among its inputs (inputsFor), and its
|
|
475
|
-
* settle holds a replay that did not answer or hand back on each. */
|
|
476
|
-
const heldOn = (record) => record?.held_on ?? [];
|
|
477
|
-
/** The graded inputs a replay of `workflow` at `digest` runs on (gradedInputs), the one picker of both starts: always the
|
|
478
|
-
* served runs graded wrong its newest record holds it on (heldOn), then `held`, the inputs of its newest record that
|
|
479
|
-
* names any (a replay's), else, with none and nothing owed, the agent's newest REPLAY_INPUTS; or a number, the agent's
|
|
480
|
-
* newest that many. A claim's start (decide) is `held` on a revision with no record: the newest REPLAY_INPUTS. `owed`:
|
|
481
|
-
* the held runs among the picked; `named`: how many runs the records named (owed or decided on), picked or not. */
|
|
482
|
-
async function inputsFor(deps, workflow, digest, records, inputs) {
|
|
483
|
-
const held = heldOn(promotionOf(records, workflow.name, digest));
|
|
484
|
-
const last = inputs === "held" ? records.findLast((one) => one.workflow === workflow.name && one.digest === digest && (one.inputs?.length || one.replay?.of.length)) : undefined;
|
|
485
|
-
const of = [...new Set([...held, ...(last ? (last.inputs ?? last.replay.of) : [])])];
|
|
486
|
-
const n = typeof inputs === "number" ? inputs : of.length ? 0 : REPLAY_INPUTS;
|
|
487
|
-
const graded = await gradedInputs(deps, workflow, { of, n });
|
|
488
|
-
return { graded, owed: graded.filter((one) => held.includes(one.grade.run)).map((one) => one.grade.run), named: of.length };
|
|
489
|
-
}
|
|
490
539
|
/** The per_run MCP credentials run `launch.handed_from` was handed (its launch's sealed column, opened under the seal
|
|
491
540
|
* key), sealed for the replay run `id` alone, as the server's start seals a run's mcp_auth (store/seal.ts
|
|
492
541
|
* sealRunMcpAuth): the replay's claim reads them where any run's claim does (runner/mcp-claim.ts). None when the replay
|
|
@@ -500,7 +549,7 @@ async function handedTo(deps, launch, id) {
|
|
|
500
549
|
const handed = sealed === null ? undefined : openRunMcpAuth(deps.sealKey, from, sealed);
|
|
501
550
|
return handed && Object.keys(handed).length ? sealRunMcpAuth(deps.sealKey, id, handed) : undefined;
|
|
502
551
|
}
|
|
503
|
-
/** Starts the replay of `workflow` at `digest` on
|
|
552
|
+
/** Starts the replay of `workflow` at `digest` on agent run `of` (its launch `from`) as run `id` (the id its reserved
|
|
504
553
|
* record names), with the replay's chosen `launch` (replayLaunchOf: its run launch, revision and handed credentials,
|
|
505
554
|
* handedTo), whichever claim creates it. False when a run by that id exists already: another claim created it (a
|
|
506
555
|
* relaunch and the reserving claim at once), and it is left to that one. */
|
|
@@ -522,10 +571,10 @@ async function startReplay(deps, workflow, digest, of, from, id, launch) {
|
|
|
522
571
|
}
|
|
523
572
|
}
|
|
524
573
|
/** Finishes a launch that was interrupted: `record`'s reserved replay runs still not created REPLAY_LAUNCH_GRACE_MS after
|
|
525
|
-
* its reservation (and before its deadline) are created now, each under its reserved id on the
|
|
574
|
+
* its reservation (and before its deadline) are created now, each under its reserved id on the agent run its record
|
|
526
575
|
* names for it (`inputs`), with the launch the record chose when it was reserved (`launch`: its model, revision and
|
|
527
|
-
* handed credentials, an owner's replay none), never this claim's: one
|
|
528
|
-
*
|
|
576
|
+
* handed credentials, an owner's replay none), never this claim's: one check runs on one set of settings. One whose
|
|
577
|
+
* agent run's launch can no longer be read stays missing: past the deadline it is counted as never started. How many
|
|
529
578
|
* this claim created. */
|
|
530
579
|
async function relaunch(deps, workflow, digest, record) {
|
|
531
580
|
const { store } = deps;
|
|
@@ -543,29 +592,33 @@ async function relaunch(deps, workflow, digest, record) {
|
|
|
543
592
|
made += 1;
|
|
544
593
|
}
|
|
545
594
|
if (made)
|
|
546
|
-
deps.log(`workflow-first: ${workflow.name} at ${digest}: its
|
|
595
|
+
deps.log(`workflow-first: ${workflow.name} at ${digest}: its check's launch was interrupted; ${plural(made, "replay run")} it reserved started now`);
|
|
547
596
|
return made;
|
|
548
597
|
}
|
|
549
|
-
/** A run's final answer, whole (its last final answer row that is not its end-of-run turn's), or null: none,
|
|
550
|
-
* record could not be read
|
|
551
|
-
|
|
598
|
+
/** A run's final answer row, its text whole (its last final answer row that is not its end-of-run turn's), or null: none,
|
|
599
|
+
* or its record could not be read. `turn`: the row of that id instead (the answer an audit is of, runner/audit.ts), so an
|
|
600
|
+
* answer a follow-up gave since is never read for it. */
|
|
601
|
+
export async function answerRowOf(store, runId, context, turn) {
|
|
552
602
|
try {
|
|
553
603
|
const rows = await runTimeline(store, runId, context, { settled: true, caps: { text: Number.POSITIVE_INFINITY } });
|
|
554
|
-
|
|
604
|
+
const row = rows.findLast((one) => one.kind === "final_answer" && one.agent_label !== LEARNING_LABEL && (turn === undefined || one.id === turn));
|
|
605
|
+
return typeof row?.text === "string" ? { id: row.id, text: row.text } : null;
|
|
555
606
|
}
|
|
556
607
|
catch {
|
|
557
608
|
return null;
|
|
558
609
|
}
|
|
559
610
|
}
|
|
611
|
+
/** A run's final answer, whole (answerRowOf's text; `turn`: that row's), or null. */
|
|
612
|
+
export const answerOf = async (store, runId, context, turn) => (await answerRowOf(store, runId, context, turn))?.text ?? null;
|
|
560
613
|
const metricsOf = (row) => (isObject(row?.metrics) ? row.metrics : {});
|
|
561
614
|
/** What a replay run did, read from its row alone: answered, handed back or skipped at a write once it ended as such; any
|
|
562
|
-
* other end, or one not ended (past the
|
|
615
|
+
* other end, or one not ended (past the check's deadline), failed: no evidence either way. */
|
|
563
616
|
const outcomeOf = (run) => {
|
|
564
617
|
const m = metricsOf(run);
|
|
565
618
|
return run.state === "ended" && (m.replay?.outcome === "answered" || m.replay?.outcome === "handed_back" || m.replay?.outcome === "skipped_write") ? m.replay.outcome : "failed";
|
|
566
619
|
};
|
|
567
620
|
const NO_OUTCOMES = { answered: 0, handed_back: 0, failed: 0, skipped_write: 0 };
|
|
568
|
-
/** How many of a
|
|
621
|
+
/** How many of a check's runs ended each way so far (ReplayOutcomes): one not ended counts only past the deadline
|
|
569
622
|
* (`late`), as failed; one never created, nothing. */
|
|
570
623
|
function outcomesOf(runs, late) {
|
|
571
624
|
const counts = { ...NO_OUTCOMES };
|
|
@@ -575,7 +628,7 @@ function outcomesOf(runs, late) {
|
|
|
575
628
|
return counts;
|
|
576
629
|
}
|
|
577
630
|
/** What `record`'s replay runs cost together, at `now`: null while one's is unknown (one not created yet before the
|
|
578
|
-
* deadline, one not ended, one ended with no cost)
|
|
631
|
+
* deadline, one not ended, one ended with no cost); a run never created by the deadline spent nothing. */
|
|
579
632
|
function replayCost(runs, late) {
|
|
580
633
|
let cost = 0;
|
|
581
634
|
for (const run of runs) {
|
|
@@ -590,199 +643,14 @@ function replayCost(runs, late) {
|
|
|
590
643
|
}
|
|
591
644
|
return Math.round(cost * 1e6) / 1e6;
|
|
592
645
|
}
|
|
593
|
-
/**
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
|
|
597
|
-
* many reserved runs were never created by the deadline (`unstarted`). `partial`: the rows of the runs ended so far, at
|
|
598
|
-
* any time (a replay's read while it runs). A row needs its graded run's grade (what it is scored against): a run whose
|
|
599
|
-
* grade the agent's newer grades rolled out of those kept has none, and is counted (`outcomes`) but not scored. */
|
|
600
|
-
async function replayRows(deps, record, partial = false) {
|
|
601
|
-
const { store, context } = deps;
|
|
602
|
-
const now = (deps.now ?? Date.now)();
|
|
603
|
-
const grades = new Map((await gradesOf(store.settings, deps.agentId)).map((grade) => [grade.run, grade]));
|
|
604
|
-
const runs = await Promise.all((record.runs ?? []).map((id) => store.runs.get(id)));
|
|
605
|
-
const late = now - Date.parse(record.at) > REPLAY_DEADLINE_MS;
|
|
606
|
-
if (!late && !partial && runs.some((row) => !row || !FINAL_STATES.includes(row.state)))
|
|
607
|
-
return undefined;
|
|
608
|
-
const rows = [];
|
|
609
|
-
for (const run of runs) {
|
|
610
|
-
if (!run || (partial && !late && !FINAL_STATES.includes(run.state)))
|
|
611
|
-
continue;
|
|
612
|
-
const launch = await store.runs.launch(run.id).then((one) => readLaunchBody(one.body), () => undefined);
|
|
613
|
-
const of = launch?.replay?.of;
|
|
614
|
-
const grade = of ? grades.get(of) : undefined;
|
|
615
|
-
if (!of || !grade)
|
|
616
|
-
continue;
|
|
617
|
-
const m = metricsOf(run);
|
|
618
|
-
const outcome = outcomeOf(run);
|
|
619
|
-
const stoppedAt = outcome === "skipped_write" ? m.replay?.skipped : undefined;
|
|
620
|
-
rows.push({
|
|
621
|
-
of,
|
|
622
|
-
recorded: { answer: await answerOf(store, of, context), correct: grade.correct, path: grade.path, ...(grade.expected ? { expected: grade.expected } : {}) },
|
|
623
|
-
outcome,
|
|
624
|
-
...(stoppedAt ? { skipped: stoppedAt } : {}),
|
|
625
|
-
answer: outcome === "answered" ? await answerOf(store, run.id, context) : null,
|
|
626
|
-
guards: Array.isArray(m.guards) ? m.guards : [],
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
return { rows, scored: refitRowsOf(rows), cost: replayCost(runs, late), outcomes: outcomesOf(runs, late), unstarted: runs.filter((row) => !row).length };
|
|
630
|
-
}
|
|
631
|
-
/** A replaying digest's decision, once its replay runs ended: promote within the bar when it answered alone at least once,
|
|
632
|
-
* else hold, then one refit (of its guards and recorded escalates). Undefined while a replay run is at work. */
|
|
633
|
-
async function settle(deps, workflow, digest, record) {
|
|
634
|
-
const { store, agentId, log } = deps;
|
|
635
|
-
const now = deps.now ?? Date.now;
|
|
636
|
-
const read = await replayRows(deps, record);
|
|
637
|
-
if (!read)
|
|
638
|
-
return undefined;
|
|
639
|
-
// Scored: the inputs the workflow answered or handed back on. One it failed on (a node failed: a credential or a tool the
|
|
640
|
-
// replay ran without, say) is no evidence either way, never the agent's answer in its name.
|
|
641
|
-
// Skipped: the inputs whose workflow needed a write the graded run's recording does not have (learn/replay-calls.ts): a
|
|
642
|
-
// replay never runs one, so they are no evidence either, and never wrong.
|
|
643
|
-
const scored = read.scored;
|
|
644
|
-
const thresholds = thresholdsOf(workflow.workflow);
|
|
645
|
-
const score = scoreRows(scored, thresholds);
|
|
646
|
-
// A revision held on served runs graded wrong promotes only once its replay answered (or handed back) on each of them,
|
|
647
|
-
// and is within the bar on them alone (the one scorer over their rows; a hand-back on each is the agent's, right): a
|
|
648
|
-
// pass on other inputs never puts it back, nor pays for repeating the wrong answers it was held on.
|
|
649
|
-
const owedRuns = heldOn(record);
|
|
650
|
-
const unanswered = owedRuns.filter((run) => !read.rows.some((row) => row.of === run && (row.outcome === "answered" || row.outcome === "handed_back")));
|
|
651
|
-
const owedScore = owedRuns.length ? scoreRows(scored.filter((row) => row.of !== undefined && owedRuns.includes(row.of)), thresholds) : undefined;
|
|
652
|
-
const owedWrong = owedScore !== undefined && owedScore.n > 0 && !withinBar(owedScore);
|
|
653
|
-
const passed = scored.length >= REPLAY_MIN && clears(score);
|
|
654
|
-
const ok = passed && !unanswered.length && !owedWrong;
|
|
655
|
-
const pts = (value) => `${(value * 100).toFixed(1)}%`;
|
|
656
|
-
// Its runs' outcomes, the one count the record keeps (a run whose grade rolled out of those kept counts too).
|
|
657
|
-
const { skipped_write: skipped, failed } = read.outcomes;
|
|
658
|
-
const ended = Object.values(read.outcomes).reduce((sum, one) => sum + one, 0);
|
|
659
|
-
// Inputs stopped at an MCP tool its server listed with no readOnlyHint: the hold names those tools (the server may only
|
|
660
|
-
// lack the hint); any other skip (a destructive tool, a built-in write) keeps the recording's words.
|
|
661
|
-
const tools = skippedToolsOf(read.rows);
|
|
646
|
+
/** Why `skipped` inputs of a replay were skipped, in one phrase (`why`): the MCP tools among `tools` its server listed
|
|
647
|
+
* with no readOnlyHint, named (`hint`: the server may only lack the hint), with the rest in the recording's words;
|
|
648
|
+
* with none of those, the recording's words alone (SKIPPED_WRITE). */
|
|
649
|
+
function skipWordsOf(tools, skipped) {
|
|
662
650
|
const unmarked = tools.filter((one) => one.no_read_only_hint);
|
|
663
651
|
const writes = skipped - unmarked.reduce((sum, one) => sum + one.inputs, 0);
|
|
664
652
|
const hint = unmarked.length ? unmarkedWords(unmarked.map((one) => toolWord(one.tool))) : undefined;
|
|
665
|
-
|
|
666
|
-
const aside = [...(failed ? [`${failed} failed: a step failed, no evidence either way`] : []), ...(skipped ? [`${skipped} skipped: ${skipWhy}`] : []),
|
|
667
|
-
...(read.unstarted ? [`${read.unstarted} never started by the replay's deadline`] : [])].join("; ");
|
|
668
|
-
const said = `on ${plural(score.n, "graded input")}: the workflow ${pts(score.wf_acc)} (${score.handed_back} handed back, the agent's answer), the agent ${pts(score.agent_acc)}, ${score.delta >= 0 ? "+" : ""}${(score.delta * 100).toFixed(1)} pts${aside ? ` (${aside})` : ""}`;
|
|
669
|
-
const reserved = record.runs?.length ?? 0;
|
|
670
|
-
const reason = reserved && read.unstarted === reserved
|
|
671
|
-
? `none of its ${plural(reserved, "replay run")} started by the replay's deadline (its launch was interrupted: the server stopped after reserving it, say): nothing was replayed; start it again with POST .../learning/workflows/${workflow.name}/revisions/${digest}/replays`
|
|
672
|
-
: ended && skipped === ended
|
|
673
|
-
? hint ? `nothing was replayed on its ${plural(skipped, "graded input")}: ${skipWhy}` : `every one of its ${plural(skipped, "graded input")} ${SKIPPED_WRITE}: a replay never runs a write, so nothing was replayed`
|
|
674
|
-
: scored.length < REPLAY_MIN
|
|
675
|
-
? `only ${plural(scored.length, "replay")} answered or handed back${aside ? ` (${aside})` : ""}, fewer than ${REPLAY_MIN}`
|
|
676
|
-
: passed && unanswered.length ? `replayed ${said}, but it did not answer on ${plural(unanswered.length, "graded run")} it was held on (its held_on): it serves again only once a replay answers on them`
|
|
677
|
-
: passed && owedScore && owedWrong ? `replayed ${said}, but on the ${plural(owedScore.n, "graded run")} it was held on the workflow ${pts(owedScore.wf_acc)}, the agent ${pts(owedScore.agent_acc)}: more than ${PROMOTE_BAR * 100} pts under; it serves again only once it answers them right`
|
|
678
|
-
: ok ? `replayed ${said}: within ${PROMOTE_BAR * 100} pts`
|
|
679
|
-
: !answersAlone(score) ? `it handed back every one of its ${plural(score.n, "replayed input")}: it would never answer alone (${said})`
|
|
680
|
-
: `replayed ${said}: more than ${PROMOTE_BAR * 100} pts under the agent`;
|
|
681
|
-
// Promoted: named by the agent's newest grade (`since`), which every run it serves keeps, so a revision that served
|
|
682
|
-
// before (promoted, held, now replayed again) is checked only on the runs this promotion served, never again on the
|
|
683
|
-
// ones it was held on. Held: it still owes the runs it was held on.
|
|
684
|
-
const since = ok ? Math.max(0, ...(await gradesOf(store.settings, agentId)).map((grade) => grade.seq ?? 0)) : undefined;
|
|
685
|
-
const owed = heldOn(record);
|
|
686
|
-
const decided = await recordPromotion(store.settings, agentId, {
|
|
687
|
-
workflow: workflow.name, digest, at: iso(now()), decided: ok ? "promote" : "hold", source: "replay", reason,
|
|
688
|
-
replay: { ...score, of: read.rows.map((row) => row.of), cost_usd: read.cost, skipped_tools: tools, outcomes: read.outcomes }, ...(record.runs ? { runs: record.runs } : {}),
|
|
689
|
-
...(record.commit ? { commit: record.commit } : seedOf(deps)), ...(record.replay_id ? { replay_id: record.replay_id } : {}),
|
|
690
|
-
...(since !== undefined ? { since } : owed.length ? { held_on: owed } : {}),
|
|
691
|
-
}, (was) => sameReplay(was, record));
|
|
692
|
-
// Another claim's decision came first: it logs, cancels and refits; this one reads it.
|
|
693
|
-
if (!decided.won)
|
|
694
|
-
return decided.record?.decided === "replaying" ? undefined : decided.record;
|
|
695
|
-
log(`workflow-first: ${workflow.name} at ${digest} ${ok ? "promoted" : "held"}: ${reason}`);
|
|
696
|
-
// Decided past its deadline with runs still at work: they are cancelled, never left spending on a decided replay.
|
|
697
|
-
const cancelled = await cancelUnended(deps, record);
|
|
698
|
-
if (cancelled)
|
|
699
|
-
log(`workflow-first: ${workflow.name} at ${digest}: ${plural(cancelled, "replay run")} still at work past the replay's deadline cancelled`);
|
|
700
|
-
if (!ok && scored.length >= REPLAY_MIN && !unanswered.length && !owedWrong) {
|
|
701
|
-
// Held: one refit over the replay's scored rows (each step's p known: its guards read record-only, its judge-driven
|
|
702
|
-
// escalates recorded). One with neither changes nothing. Never one that still owes runs it was held on: a refit is
|
|
703
|
-
// promoted on all its rows, and they did not answer on those, or answered them under the bar.
|
|
704
|
-
await refitAndWrite(deps, workflow, digest, scored, "its replay held it");
|
|
705
|
-
}
|
|
706
|
-
return decided.record;
|
|
707
|
-
}
|
|
708
|
-
/** A serving digest, checked once per REFIT_EVERY more graded runs it served: its thresholds refit over those runs and its
|
|
709
|
-
* replay's rows (a new revision when they move and still clear the bar), and held when its graded runs' accuracy fell
|
|
710
|
-
* more than PROMOTE_BAR under the agent's. The promotion it serves this run under (`record`, or the digest's newer
|
|
711
|
-
* promotion when one replaced it meanwhile), undefined when it does not serve it. */
|
|
712
|
-
async function stillServing(deps, workflow, digest, record) {
|
|
713
|
-
const { store, agentId, log } = deps;
|
|
714
|
-
const now = deps.now ?? Date.now;
|
|
715
|
-
const grades = await gradesOf(store.settings, agentId);
|
|
716
|
-
// Only the grades of runs this promotion served (each keeps its `since`, Grade.promotion): promoted again after a hold,
|
|
717
|
-
// never the ones it was held on, nor a run served under the promotion before and graded after this one.
|
|
718
|
-
const served = grades.filter((grade) => grade.workflow === workflow.name && grade.digest === digest && (grade.path === "workflow" || grade.path === "handed_back") && grade.promotion === record.since);
|
|
719
|
-
// Each write is kept only while the promotion this check read is still the digest's (the same since): a check that read
|
|
720
|
-
// an older one, replaced meanwhile (held, replayed and promoted again), writes nothing over the newer one.
|
|
721
|
-
const same = (was) => was?.decided === "promote" && was.since === record.since;
|
|
722
|
-
// New since the last check: by seq (putGrade), a cursor the trim of the kept grades never moves back.
|
|
723
|
-
const checked = record.checked ?? 0;
|
|
724
|
-
const through = Math.max(checked, ...served.map((grade) => grade.seq ?? 0));
|
|
725
|
-
if (served.filter((grade) => (grade.seq ?? 0) > checked).length < REFIT_EVERY)
|
|
726
|
-
return record;
|
|
727
|
-
const promoted = (one) => (one?.decided === "promote" ? one : undefined);
|
|
728
|
-
// The agent's accuracy by hand: its newest graded runs on the agent path, else the replay's.
|
|
729
|
-
const byHand = grades.filter((grade) => grade.path === "agent").slice(-REPLAY_INPUTS);
|
|
730
|
-
const agentAcc = byHand.length ? byHand.filter((grade) => grade.correct).length / byHand.length : record.replay?.agent_acc;
|
|
731
|
-
// Marked checked at this count first, so two claims never check the same window twice.
|
|
732
|
-
// Only the claim whose write marked it refits: one that lost the write reads the winner's record back, the same cursor.
|
|
733
|
-
const marked = await recordPromotion(store.settings, agentId, { ...record, at: iso(now()), checked: through }, (was) => same(was) && (was?.checked ?? 0) === checked);
|
|
734
|
-
if (!marked.won || agentAcc === undefined)
|
|
735
|
-
return promoted(marked.record);
|
|
736
|
-
const rows = [];
|
|
737
|
-
for (const grade of served) {
|
|
738
|
-
const run = await store.runs.get(grade.run);
|
|
739
|
-
const m = metricsOf(run);
|
|
740
|
-
const guards = Array.isArray(m.guards) ? m.guards : [];
|
|
741
|
-
rows.push(grade.path === "workflow" ? { guards, answered: true, wfCorrect: grade.correct, agentCorrect: agentAcc } : { guards, answered: false, wfCorrect: null, agentCorrect: grade.correct ? 1 : 0 });
|
|
742
|
-
}
|
|
743
|
-
const replayed = record.source === "replay" && record.runs?.length ? (await replayRows(deps, record))?.scored ?? [] : [];
|
|
744
|
-
const wrote = await refitAndWrite(deps, workflow, digest, [...rows, ...replayed], `${plural(served.length, "graded run")} it served`);
|
|
745
|
-
// What actually happened on the served runs: the recorded grades (a workflow-path grade is the workflow's answer, a
|
|
746
|
-
// handed-back one the agent's). Recorded, not rescored: the guards on a run may belong to other workflows the agent called
|
|
747
|
-
// after a hand-back, and rescoring over them would count a right answer as a hand-back at the agent's accuracy.
|
|
748
|
-
const servedAcc = served.filter((grade) => grade.correct).length / served.length;
|
|
749
|
-
if (!wrote && servedAcc < agentAcc - PROMOTE_BAR - 1e-9) {
|
|
750
|
-
const reason = `${plural(served.length, "graded run")} it served were right ${(servedAcc * 100).toFixed(1)}% of the time, more than ${PROMOTE_BAR * 100} pts under the agent's ${(agentAcc * 100).toFixed(1)}% by hand`;
|
|
751
|
-
// It keeps the served runs graded wrong it was held on, newest first: its next replay owes them (heldOn).
|
|
752
|
-
const wrong = served.filter((grade) => !grade.correct).map((grade) => grade.run).reverse().slice(0, REPLAY_INPUTS);
|
|
753
|
-
const held = await recordPromotion(store.settings, agentId, { workflow: workflow.name, digest, at: iso(now()), decided: "hold", source: "graded", reason, held_on: wrong }, same);
|
|
754
|
-
if (held.won)
|
|
755
|
-
log(`workflow-first: ${workflow.name} at ${digest} held: ${reason}`);
|
|
756
|
-
return promoted(held.record);
|
|
757
|
-
}
|
|
758
|
-
return marked.record;
|
|
759
|
-
}
|
|
760
|
-
/** One refit of `workflow` over `rows`; when it moves a guard and the rows clear the bar at the new thresholds, the new
|
|
761
|
-
* text is written as a new revision (a merge from the claim's seed) and promoted with the rows' numbers. True when one
|
|
762
|
-
* was written. */
|
|
763
|
-
async function refitAndWrite(deps, workflow, digest, rows, why) {
|
|
764
|
-
if (!deps.commit || rows.length < REPLAY_MIN)
|
|
765
|
-
return false;
|
|
766
|
-
const now = deps.now ?? Date.now;
|
|
767
|
-
const refit = refitGuards(workflow.workflow, rows, iso(now()));
|
|
768
|
-
if (!refit.text || !clears(refit))
|
|
769
|
-
return false;
|
|
770
|
-
const next = workflowDigest({ workflow: refit.text });
|
|
771
|
-
const moved = refit.guards.map((one) => (one.escalate ? `${one.node} escalate off` : one.threshold === null ? `${one.node} guard off` : `${one.node} ${one.was} -> ${one.threshold}${one.fit ? " (fit)" : ""}`)).join(", ");
|
|
772
|
-
const reason = `refit ${workflow.name}'s guards over ${why} (${plural(rows.length, "row")}): ${moved}`;
|
|
773
|
-
const landed = await deps.commit([{ path: `workflows/${workflow.name}/workflow.json`, text: refit.text }], reason, `refit-${digest}-${next}`);
|
|
774
|
-
if (landed === null) {
|
|
775
|
-
deps.log(`workflow-first: ${workflow.name}'s refit did not land on its learning folder; it keeps its thresholds`);
|
|
776
|
-
return false;
|
|
777
|
-
}
|
|
778
|
-
await recordPromotion(deps.store.settings, deps.agentId, {
|
|
779
|
-
workflow: workflow.name, digest: next, at: iso(now()), decided: "promote", source: "refit",
|
|
780
|
-
reason: `${reason}: the workflow ${(refit.wf_acc * 100).toFixed(1)}%, the agent ${(refit.agent_acc * 100).toFixed(1)}% on those rows`,
|
|
781
|
-
replay: { ...scoreOf(refit), of: rows.flatMap((row) => (row.of === undefined ? [] : [row.of])) },
|
|
782
|
-
refit: { from: digest, guards: refit.guards }, commit: landed,
|
|
783
|
-
}, (was) => was === undefined);
|
|
784
|
-
deps.log(`workflow-first: ${reason}; the new revision (${next}) serves from the next run`);
|
|
785
|
-
return true;
|
|
653
|
+
return { hint, why: !hint ? SKIPPED_WRITE : writes ? `${hint} (${writes} more ${writes === 1 ? "needs" : "need"} a write the recording doesn't have)` : hint };
|
|
786
654
|
}
|
|
787
655
|
/** Why a replay a person asked for (askReplay) or read (replayView) was refused: `missing`, no such workflow, revision or
|
|
788
656
|
* replay of the agent (404); `conflict`, the revision cannot replay now (409), in words that say why. */
|
|
@@ -816,15 +684,21 @@ async function revisionOf(ask, records, name, digest) {
|
|
|
816
684
|
}
|
|
817
685
|
return { named: onMain || records.some((one) => one.workflow === name) };
|
|
818
686
|
}
|
|
819
|
-
/**
|
|
820
|
-
*
|
|
821
|
-
*
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
687
|
+
/** The agent runs `name` at `digest` was last checked on: its newest record that names them, a decided
|
|
688
|
+
* check's `check.of` or a reservation's `inputs`. A check's reservation leaves the kept history (trimPromotions) while
|
|
689
|
+
* its decision stays, so the decision's own list is read first. Undefined when no record names any. */
|
|
690
|
+
function heldInputsOf(records, name, digest) {
|
|
691
|
+
const checked = (one) => (one.decided !== "replaying" && one.check?.of?.length ? one.check.of : undefined);
|
|
692
|
+
const last = records.findLast((one) => one.workflow === name && one.digest === digest && (checked(one) ?? one.inputs)?.length);
|
|
693
|
+
return last && (checked(last) ?? last.inputs);
|
|
694
|
+
}
|
|
695
|
+
/** A check of `name` at `digest` a person asked for: the claim path's check (reserve, startReplay), on `inputs`: `held`,
|
|
696
|
+
* the agent runs its last check ran on (else, with none, the agent's last compile_after completed runs), or a number,
|
|
697
|
+
* the agent's newest that many (each a completed run of its own the workflow fits). Its runs end as a claim's do, and it
|
|
698
|
+
* is decided by settleCheck: at a claim that lays the revision, or at its read (replayView). Refused (ReplayRefused): no
|
|
699
|
+
* such workflow or revision; one replaying already, or another revision of it replaying (one at a time); a promoted one
|
|
700
|
+
* (it serves already); no input it fits (nothing is reserved); an agent that does not learn here. The replay's id and
|
|
701
|
+
* its inputs. */
|
|
828
702
|
export async function askReplay(ask, name, digest, inputs) {
|
|
829
703
|
const records = await promotionsOf(ask.store.settings, ask.agentId);
|
|
830
704
|
const found = await revisionOf(ask, records, name, digest);
|
|
@@ -837,75 +711,75 @@ export async function askReplay(ask, name, digest, inputs) {
|
|
|
837
711
|
throw new ReplayRefused("conflict", `${name} at ${digest} is promoted: it serves already. A replay checks a revision that does not serve yet, or was held.`);
|
|
838
712
|
const rival = replayingOf(records, name, digest)[0];
|
|
839
713
|
if (rival)
|
|
840
|
-
throw new ReplayRefused("conflict", `${name} at ${rival.digest} is replaying: one revision of a workflow
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
}
|
|
847
|
-
|
|
714
|
+
throw new ReplayRefused("conflict", `${name} at ${rival.digest} is replaying: one revision of a workflow is checked at a time, and a running check finishes. Ask again once it ends.`);
|
|
715
|
+
const last = inputs === "held" ? heldInputsOf(records, name, digest) : undefined;
|
|
716
|
+
// The agent's own numbers (its current revision's definition, the one its replay runs launch on): with no earlier
|
|
717
|
+
// check, its last compile_after completed runs, as the claim's check picks them (startCheck), never the default's.
|
|
718
|
+
const current = (await ask.store.agents.get(ask.agentId).catch(() => undefined))?.currentRevision;
|
|
719
|
+
const loop = current ? await loopOfRevision({ store: ask.store, revisionId: current }) : loopOf(undefined);
|
|
720
|
+
const { inputs: picked } = await checkInputs({ store: ask.store, agentId: ask.agentId }, found.workflow, { of: last ?? [], n: typeof inputs === "number" ? inputs : last ? 0 : loop.compile_after });
|
|
721
|
+
if (!picked.length)
|
|
722
|
+
throw new ReplayRefused("conflict", `${name} at ${digest} has no input to replay on: none of ${typeof inputs === "number" ? `the agent's newest ${plural(inputs, "completed run")}` : last ? "the runs it was last checked on" : "the agent's last completed runs"} has an input it fits. Nothing was started.`);
|
|
723
|
+
const { launch, revisionId } = await ask.launchOf(picked[0].launch.input);
|
|
848
724
|
if (!launch.learning || launch.learning.notesOnly)
|
|
849
725
|
throw new ReplayRefused("conflict", `A replay runs ${name} as a run of the agent that learns does, and the agent's runs do not take learned workflows here (its learning is off, notes only, or this server's is off).`);
|
|
850
|
-
const deps = { store: ask.store, agentId: ask.agentId, revisionId, launch: { ...launch, learning: { ...launch.learning, seed: found.commit } }, context: ask.context, log: ask.log, ...(ask.now ? { now: ask.now } : {}) };
|
|
851
|
-
const
|
|
726
|
+
const deps = { store: ask.store, agentId: ask.agentId, revisionId, launch: { ...launch, learning: { ...launch.learning, seed: found.commit } }, context: ask.context, log: ask.log, shown: ask.shown, ...(ask.now ? { now: ask.now } : {}) };
|
|
727
|
+
const bar = { compile_after: loop.compile_after, agree_min: loop.agree_min, alone_min: loop.alone_min, attempt: attemptOf(records, name, digest) };
|
|
728
|
+
const said = `checking it on ${plural(picked.length, "input")} of the agent's own runs, as its owner asked`;
|
|
852
729
|
// Kept only while the revision's newest record is still the one read here: each record write stamps a new `at`.
|
|
853
|
-
const reserved = await reserve(deps, found.workflow, digest,
|
|
730
|
+
const reserved = await reserve(deps, found.workflow, digest, picked, said, (was) => was?.at === own?.at && was?.decided === own?.decided, bar);
|
|
854
731
|
if (!reserved.won || !reserved.record?.replay_id)
|
|
855
732
|
throw new ReplayRefused("conflict", `${name} at ${digest} changed while this replay was asked for (another replay or decision came first): read its promotions and ask again.`);
|
|
856
733
|
ask.log(`workflow-first: ${name} at ${digest}: ${said}`);
|
|
857
|
-
return { replay: reserved.record.replay_id, inputs:
|
|
734
|
+
return { replay: reserved.record.replay_id, inputs: picked.length };
|
|
735
|
+
}
|
|
736
|
+
const NO_SCORE = { n: 0, agree: 0, agent_acc: 0, wf_acc: 0, delta: 0, handed_back: 0 };
|
|
737
|
+
/** A check's replay as its read answers it (replayView): decided, its record's numbers (CheckRecord), else its runs ended
|
|
738
|
+
* so far. */
|
|
739
|
+
async function checkView(ask, record, id, name, digest) {
|
|
740
|
+
const inputs = record.runs?.length ?? 0;
|
|
741
|
+
const decided = record.decided === "promote" || record.decided === "hold" ? record.decided : undefined;
|
|
742
|
+
const check = record.check;
|
|
743
|
+
if (decided && check?.outcomes) {
|
|
744
|
+
const n = check.n ?? 0;
|
|
745
|
+
const handedBack = check.outcomes.handed_back;
|
|
746
|
+
const wf = n ? Math.round(((check.agreed ?? 0) + handedBack) / n * 1e4) / 1e4 : 0;
|
|
747
|
+
const neverStarted = inputs > 0 && check.unstarted === inputs;
|
|
748
|
+
return { replay: id, workflow: name, digest, status: neverStarted ? "failed" : "done", inputs, outcomes: check.outcomes, score: { n, agree: check.agreed ?? 0, agent_acc: n ? 1 : 0, wf_acc: wf, delta: n ? Math.round((wf - 1) * 1e4) / 1e4 : 0, handed_back: handedBack }, cost_usd: check.cost_usd ?? null, decided, reason: record.reason };
|
|
749
|
+
}
|
|
750
|
+
const runs = await Promise.all((record.runs ?? []).map((run) => ask.store.runs.get(run)));
|
|
751
|
+
return { replay: id, workflow: name, digest, status: decided ? "done" : "running", inputs, outcomes: outcomesOf(runs, false), score: NO_SCORE, cost_usd: replayCost(runs, false), ...(decided ? { decided, reason: record.reason } : {}) };
|
|
858
752
|
}
|
|
859
753
|
/** The claim's ports a replay's read settles it through (replayView), with the launch its record chose when it was
|
|
860
|
-
* reserved (PromotionRecord.launch, the one every replay run of it runs with): its revision, and its learning commit
|
|
861
|
-
*
|
|
754
|
+
* reserved (PromotionRecord.launch, the one every replay run of it runs with): its revision, and its learning commit.
|
|
755
|
+
* Undefined when that launch cannot be read. */
|
|
862
756
|
function readPorts(ask, launch) {
|
|
863
757
|
const body = readLaunchBody(launch.body);
|
|
864
758
|
return body ? claimPromotion({ ...ask, revisionId: launch.revision, launch: body }) : undefined;
|
|
865
759
|
}
|
|
866
|
-
/** Reads replay `id` of `name` at `digest`, settling it first as a claim of its revision would (
|
|
867
|
-
*
|
|
868
|
-
*
|
|
869
|
-
*
|
|
870
|
-
*
|
|
760
|
+
/** Reads replay `id` of `name` at `digest`, settling it first as a claim of its revision would (settleCheck) once its runs
|
|
761
|
+
* ended or its deadline passed: it writes only the decision. A read never starts a run: a replay whose launch was
|
|
762
|
+
* interrupted (reserved runs never created) reads running until its deadline, then is decided held, read as failed, its
|
|
763
|
+
* reason saying none of its runs started and to start it again with POST. So a replay of a revision no claim lays (an
|
|
764
|
+
* older, held one) is decided too, and never running forever. Once decided, it reads only its record. Refused
|
|
871
765
|
* (missing) when the agent has no such replay. */
|
|
872
766
|
export async function replayView(ask, name, digest, id) {
|
|
873
767
|
const find = async () => (await promotionsOf(ask.store.settings, ask.agentId)).findLast((one) => one.workflow === name && one.digest === digest && one.replay_id === id);
|
|
874
768
|
let record = await find();
|
|
875
769
|
if (!record)
|
|
876
770
|
throw new ReplayRefused("missing", `${name} at ${digest} has no replay ${id}.`);
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
}
|
|
888
|
-
// Only a replaying, promote or hold record carries a replay's id.
|
|
889
|
-
const decided = record.decided === "promote" || record.decided === "hold" ? record.decided : undefined;
|
|
890
|
-
const inputs = record.runs?.length ?? 0;
|
|
891
|
-
const said = decided ? { decided, reason: record.reason } : {};
|
|
892
|
-
const kept = record.replay && decided ? record.replay : undefined;
|
|
893
|
-
if (kept) {
|
|
894
|
-
// Decided: its record's numbers alone. With not one of its reserved runs ever created (none counted: a decided
|
|
895
|
-
// replay counted every run there was, and none is created after), nothing was replayed (settle's reason says so).
|
|
896
|
-
const outcomes = kept.outcomes ?? NO_OUTCOMES;
|
|
897
|
-
const neverStarted = inputs > 0 && Object.values(outcomes).every((count) => count === 0);
|
|
898
|
-
return { replay: id, workflow: name, digest, status: neverStarted ? "failed" : "done", inputs, outcomes, score: scoreOf(kept), cost_usd: kept.cost_usd ?? null, ...said };
|
|
899
|
-
}
|
|
900
|
-
const read = await replayRows(ask, record, true);
|
|
901
|
-
text ??= await textOf(record);
|
|
902
|
-
return {
|
|
903
|
-
replay: id, workflow: name, digest, status: decided === undefined ? "running" : "done", inputs,
|
|
904
|
-
outcomes: read?.outcomes ?? NO_OUTCOMES,
|
|
905
|
-
score: scoreRows(read?.scored ?? [], text ? thresholdsOf(text.workflow) : new Map()), cost_usd: read?.cost ?? null, ...said,
|
|
906
|
-
};
|
|
771
|
+
if (record.decided === "replaying" && record.commit && record.launch) {
|
|
772
|
+
const text = (await workflowsAt(ask, record.commit).catch(() => [])).find((one) => one.name === name && workflowDigest(one) === digest);
|
|
773
|
+
const ports = text ? readPorts(ask, record.launch) : undefined;
|
|
774
|
+
if (ports && text) {
|
|
775
|
+
// Read back by its id: whatever settled it (this read, a claim, another read), never another replay's record.
|
|
776
|
+
await settleCheck(ports, text, digest, record);
|
|
777
|
+
record = (await find()) ?? record;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return checkView(ask, record, id, name, digest);
|
|
907
781
|
}
|
|
908
|
-
/** A replay run's call nodes (launch.replay; learn/replay-calls.ts): the
|
|
782
|
+
/** A replay run's call nodes (launch.replay; learn/replay-calls.ts): the agent run's recorded calls, read from its journal
|
|
909
783
|
* (a CLI's record shaped as pi's), and each tool classed by the claim's MCP pin (`runId`'s, its servers' annotations as
|
|
910
784
|
* they listed them) or the built-in list. Undefined for any other run. A recording that cannot be read is an empty one:
|
|
911
785
|
* every write is then skipped, never run. */
|
|
@@ -915,12 +789,6 @@ export async function replayCallsFor(store, runId, launch, context) {
|
|
|
915
789
|
const entries = await runEntries(store, launch.replay.of, context).catch(() => []);
|
|
916
790
|
const recorded = recordedCalls(isCliRecord(entries) ? piShaped(entries) : entries);
|
|
917
791
|
const pin = (await store.mcp.pin(runId).catch(() => undefined))?.body;
|
|
918
|
-
const annotations = (
|
|
919
|
-
const named = /^mcp:([^/]+)\/(.+)$/.exec(tool);
|
|
920
|
-
const server = named && pin && Object.hasOwn(pin, named[1]) ? pin[named[1]] : undefined;
|
|
921
|
-
const listed = server?.tools.find((one) => one.name === named[2]);
|
|
922
|
-
// Listed with none: `{}`, so a tool its server left unannotated reads unmarked, never as one it does not list.
|
|
923
|
-
return listed ? (listed.annotations ?? {}) : undefined;
|
|
924
|
-
};
|
|
792
|
+
const annotations = pinAnnotations(pin);
|
|
925
793
|
return replayCalls(recorded, (tool) => toolClassOf(tool, annotations));
|
|
926
794
|
}
|