@parcha/agentrun-serve 0.1.0-beta.40 → 0.1.0-beta.42
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 +130 -0
- package/NOTICE +2 -0
- package/README.md +149 -21
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.js +3 -1
- package/dist/api-only.d.ts +3 -1
- package/dist/api-only.js +4 -2
- package/dist/api-token.d.ts +30 -0
- package/dist/api-token.js +89 -0
- 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 +22 -6
- package/dist/composio-made.d.ts +54 -0
- package/dist/composio-made.js +83 -0
- package/dist/composio-preset.d.ts +16 -8
- package/dist/composio-preset.js +106 -43
- package/dist/composio-triggers.d.ts +7 -2
- package/dist/composio-triggers.js +13 -7
- package/dist/computer-attach.d.ts +29 -11
- package/dist/computer-attach.js +141 -47
- package/dist/computer-keys.d.ts +4 -0
- package/dist/computer-keys.js +5 -0
- 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.d.ts +11 -1
- package/dist/computers.js +23 -26
- package/dist/console/assets/{index-B8Zn1Iqn.css → index-C3gNPz58.css} +1 -1
- package/dist/console/assets/index-DYJ_wj27.js +711 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.js +9 -2
- package/dist/eval-feed.d.ts +26 -0
- package/dist/eval-feed.js +72 -0
- package/dist/eval-mailbox.d.ts +4 -1
- package/dist/eval-mailbox.js +11 -3
- package/dist/exa-keyless.d.ts +1 -1
- package/dist/exa-keyless.js +1 -1
- package/dist/feed/feed-faux.d.ts +62 -0
- package/dist/feed/feed-faux.js +123 -0
- package/dist/feed/fetch.d.ts +41 -0
- package/dist/feed/fetch.js +128 -0
- package/dist/feed/parse.d.ts +42 -0
- package/dist/feed/parse.js +291 -0
- 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/learn/check.d.ts +17 -1
- package/dist/learn/check.js +240 -5
- package/dist/learn/compile.d.ts +48 -27
- package/dist/learn/compile.js +134 -101
- package/dist/learn/promote.d.ts +61 -5
- package/dist/learn/promote.js +12 -2
- package/dist/learned-nodes.d.ts +21 -4
- package/dist/learned-nodes.js +21 -8
- package/dist/learned-workflow.d.ts +3 -3
- package/dist/learned-workflow.js +14 -5
- package/dist/learning-setting.d.ts +23 -0
- package/dist/learning-setting.js +51 -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 +23 -1
- package/dist/mcp-oauth.js +46 -5
- package/dist/mcp-pin-first.d.ts +4 -2
- package/dist/mcp-pin-first.js +4 -3
- package/dist/mcp-private-hosts.d.ts +13 -0
- package/dist/mcp-private-hosts.js +16 -0
- package/dist/mcp-servers-store.d.ts +58 -5
- package/dist/mcp-servers-store.js +136 -17
- package/dist/mcp.d.ts +2 -2
- package/dist/mcp.js +2 -2
- package/dist/observation/http.d.ts +4 -1
- package/dist/observation/http.js +5 -4
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +151 -21
- package/dist/print.d.ts +6 -3
- package/dist/print.js +4 -2
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- package/dist/run-labels.d.ts +23 -0
- package/dist/run-labels.js +79 -0
- package/dist/run-learning-served.d.ts +1 -1
- 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/run-secrets.js +12 -4
- package/dist/runner/adapter-loop.d.ts +1 -1
- package/dist/runner/adapter-loop.js +94 -16
- package/dist/runner/adapter-run-spec.d.ts +19 -0
- package/dist/runner/adapter-run-spec.js +57 -36
- package/dist/runner/beside.d.ts +6 -3
- package/dist/runner/beside.js +9 -7
- package/dist/runner/cli-run-spec.js +41 -32
- package/dist/runner/conformance/harness.js +69 -14
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/feedback-round.d.ts +9 -25
- package/dist/runner/feedback-round.js +22 -40
- package/dist/runner/learning-end.d.ts +17 -7
- package/dist/runner/learning-end.js +79 -53
- package/dist/runner/main.d.ts +12 -1
- package/dist/runner/main.js +31 -4
- package/dist/runner/mcp-claim.d.ts +3 -0
- package/dist/runner/mcp-claim.js +1 -1
- package/dist/runner/mcp-run.d.ts +6 -2
- package/dist/runner/mcp-run.js +5 -3
- package/dist/runner/model-route.d.ts +4 -2
- package/dist/runner/model-route.js +8 -8
- package/dist/runner/promotion.d.ts +44 -4
- package/dist/runner/promotion.js +323 -21
- package/dist/runner/reflect-hook.d.ts +5 -0
- package/dist/runner/reflect-hook.js +22 -14
- package/dist/runner/store-secrets.js +3 -1
- package/dist/runner/workflow-first.d.ts +6 -11
- package/dist/runner/workflow-first.js +16 -20
- package/dist/runs-over-store.d.ts +1 -1
- package/dist/runs-over-store.js +5 -2
- package/dist/sdk/runs.d.ts +7 -1
- package/dist/sdk/runs.js +21 -7
- package/dist/secret-cut.js +3 -1
- package/dist/secrets.js +5 -3
- package/dist/server.d.ts +31 -5
- package/dist/server.js +240 -61
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/dialect.d.ts +3 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/postgres/dialect.js +1 -0
- package/dist/store/postgres/migrations/0015_run_labels.sql +5 -0
- package/dist/store/postgres/migrations/0016_minted_tokens.sql +21 -0
- package/dist/store/schema.d.ts +9 -0
- package/dist/store/schema.js +26 -2
- package/dist/store/sqlite/connection.js +11 -3
- package/dist/store/sqlite/run-patch.d.ts +2 -1
- package/dist/store/sqlite/run-patch.js +3 -2
- package/dist/store/sqlite/store.js +64 -10
- package/dist/store/store.d.ts +38 -1
- package/dist/store-reads/journal.js +2 -1
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/learning.js +6 -1
- package/dist/store-reads/loop.d.ts +1 -0
- package/dist/store-reads/loop.js +14 -4
- package/dist/store-reads/runs.d.ts +4 -3
- package/dist/store-reads/runs.js +3 -2
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +8 -2
- package/dist/template-eval.d.ts +11 -0
- package/dist/template-eval.js +177 -52
- package/dist/templates.d.ts +13 -1
- package/dist/templates.js +89 -18
- package/dist/timeline/computer.js +2 -1
- package/dist/timeline/origin.d.ts +1 -1
- package/dist/timeline/origin.js +2 -2
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +11 -0
- package/dist/trigger-adapter.d.ts +9 -4
- package/dist/trigger-adapters.js +2 -1
- package/dist/trigger-feed.d.ts +12 -0
- package/dist/trigger-feed.js +420 -0
- package/dist/triggers.d.ts +2 -1
- package/dist/triggers.js +23 -5
- package/dist/validate.js +7 -2
- package/dist/webhooks.js +9 -5
- package/dist/workspace/computer-deps.d.ts +5 -1
- package/dist/workspace/computer-deps.js +2 -2
- package/dist/workspace/computer-for-claim.d.ts +7 -0
- package/dist/workspace/computer-for-claim.js +12 -2
- package/dist/workspace/index.d.ts +1 -0
- package/dist/workspace/index.js +2 -0
- package/dist/workspace/paths.d.ts +4 -0
- package/dist/workspace/paths.js +9 -0
- package/dist/workspace/resolve-computer.d.ts +35 -4
- package/dist/workspace/resolve-computer.js +47 -9
- package/launcher/agentrun-serve +34 -22
- package/package.json +11 -9
- package/templates/README.md +38 -4
- package/dist/console/assets/index-Jbxjcmdp.js +0 -705
package/dist/runner/promotion.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
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";
|
|
@@ -6,12 +8,13 @@ import { openRunMcpAuth, sealRunMcpAuth } from "../store/seal.js";
|
|
|
6
8
|
import { isCliRecord, piShaped, runEntries, runTimeline } from "../store-reads/journal.js";
|
|
7
9
|
import { recordedCalls, replayCalls, SKIPPED_WRITE, toolClassOf, toolWord, unmarkedWords } from "../learn/replay-calls.js";
|
|
8
10
|
import { isObject } from "../validate.js";
|
|
9
|
-
import {
|
|
11
|
+
import { lastOwnRuns } from "../learn/compile.js";
|
|
12
|
+
import { answersAlone, championOf, checkPasses, checkShares, CHECK_TEXT_MAX, clears, gradesOf, PROMOTE_BAR, sameAnswer, PROMOTIONS_KEPT, promotionOf, promotionsOf, recordPromotion, REFIT_EVERY, REPLAY_INPUTS, REPLAY_MIN, replayingOf, scoreOf, skippedToolsOf, thresholdsOf, withinBar } 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
16
|
import { refitGuards, refitRowsOf, scoreRows } from "../learn/refit.js";
|
|
14
|
-
import { fits, servedOf, workflowDigest } from "./workflow-first.js";
|
|
17
|
+
import { fits, laidFor, servedOf, workflowDigest } from "./workflow-first.js";
|
|
15
18
|
import { learningCommit } from "../learn/commit.js";
|
|
16
19
|
import { recordOnly } from "../learn/promote.js";
|
|
17
20
|
import { workflowsAt } from "./learning-seed.js";
|
|
@@ -85,6 +88,48 @@ export function replayLaid(launch, laid) {
|
|
|
85
88
|
const { workflow, digest } = launch.replay;
|
|
86
89
|
return laid.flatMap((one) => (one.name !== workflow ? [one] : workflowDigest(one) === digest ? [{ ...one, workflow: recordOnly(one.workflow) }] : []));
|
|
87
90
|
}
|
|
91
|
+
/** The learned workflows a run's learned_workflow tool runs, once its first choice is made: what serves, never a
|
|
92
|
+
* revision its promotion has not promoted. The one its first choice served or the money rule stopped (laidFor: its
|
|
93
|
+
* champion in place of the laid revision) as chosen; any other the records name, its champion (championOf: the laid
|
|
94
|
+
* text when that is the champion, else its text at its record's commit), and none while nothing of it serves (none
|
|
95
|
+
* promoted yet, or the champion held): a revision replaying, queued, held or never decided is never handed to the
|
|
96
|
+
* agent to run in its place. One the records never named (no run's input fit it, so no claim asked) runs as laid. A
|
|
97
|
+
* replay run's as laid (replayLaid). The records unread: only the chosen one, as the gate leaves a run to its agent. */
|
|
98
|
+
export async function toolFlowsFor(deps, launch, laid, choice) {
|
|
99
|
+
const chosen = laidFor(laid, choice);
|
|
100
|
+
if (launch.replay || !chosen.length)
|
|
101
|
+
return chosen;
|
|
102
|
+
const name = choice.serve?.name ?? choice.revise?.workflow;
|
|
103
|
+
let records;
|
|
104
|
+
try {
|
|
105
|
+
records = await promotionsOf(deps.store.settings, deps.agentId);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
deps.log(`workflow-first: the promotions could not be read (${error?.message ?? error}); the learned_workflow tool runs only the workflow chosen first`);
|
|
109
|
+
return chosen.filter((one) => one.name === name);
|
|
110
|
+
}
|
|
111
|
+
const out = [];
|
|
112
|
+
for (const one of chosen) {
|
|
113
|
+
if (one.name === name || !records.some((record) => record.workflow === one.name)) {
|
|
114
|
+
out.push(one);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const digest = workflowDigest(one);
|
|
118
|
+
const champion = championOf(records, one.name);
|
|
119
|
+
if (champion?.digest === digest) {
|
|
120
|
+
out.push(one);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const text = champion?.commit && deps.champion ? await deps.champion(one.name, champion.commit).catch(() => undefined) : undefined;
|
|
124
|
+
if (text && workflowDigest(text) === champion.digest) {
|
|
125
|
+
deps.log(`workflow-first: the learned_workflow tool runs ${one.name}'s champion at ${champion.digest}, not ${digest}, which is not promoted`);
|
|
126
|
+
out.push(text);
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
deps.log(`workflow-first: the learned_workflow tool leaves out ${one.name}: ${digest} is not promoted, and ${champion ? `its champion at ${champion.digest} could not be read from its learning commit` : "no revision of it serves"}`);
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
88
133
|
const iso = (ms) => new Date(ms).toISOString();
|
|
89
134
|
/** The claim's seed as a record's `commit`: the learning commit the laid workflow's text was read from. */
|
|
90
135
|
const seedOf = (deps) => (deps.launch.learning?.seed ? { commit: deps.launch.learning.seed } : {});
|
|
@@ -143,8 +188,11 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
143
188
|
return undefined;
|
|
144
189
|
}
|
|
145
190
|
const serving = (text, under) => (under ? { workflow: text, ...(under.since !== undefined ? { promotion: under.since } : {}) } : undefined);
|
|
191
|
+
// A champion its check promoted serves as it is: it was decided once on the agent's own answers, and no grade, refit or
|
|
192
|
+
// second replay ever moves it (a hold or its owner does).
|
|
193
|
+
const kept = (text, under) => (under.source === "check" ? Promise.resolve(under) : stillServing(deps, text, under.digest, under));
|
|
146
194
|
if (champion.digest === digest)
|
|
147
|
-
return laid ? serving(workflow, await
|
|
195
|
+
return laid ? serving(workflow, await kept(workflow, champion)) : undefined;
|
|
148
196
|
if (!deps.champion)
|
|
149
197
|
return undefined;
|
|
150
198
|
const text = champion.commit ? await deps.champion(workflow.name, champion.commit).catch(() => undefined) : undefined;
|
|
@@ -152,7 +200,7 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
152
200
|
deps.log(`workflow-first: ${workflow.name}'s champion at ${champion.digest} could not be read from its learning commit; the agent takes this run`);
|
|
153
201
|
return undefined;
|
|
154
202
|
}
|
|
155
|
-
const under = await stillServing({ ...deps, commit: undefined }, text, champion.digest, champion);
|
|
203
|
+
const under = champion.source === "check" ? champion : await stillServing({ ...deps, commit: undefined }, text, champion.digest, champion);
|
|
156
204
|
if (!under)
|
|
157
205
|
return undefined;
|
|
158
206
|
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"}`);
|
|
@@ -198,11 +246,15 @@ async function decide(deps, workflow, digest) {
|
|
|
198
246
|
}
|
|
199
247
|
if (record?.decided === "replaying") {
|
|
200
248
|
await relaunch(deps, workflow, digest, record);
|
|
201
|
-
const decided = await settle(deps, workflow, digest, record);
|
|
249
|
+
const decided = record.source === "check" ? await settleCheck(deps, workflow, digest, record) : await settle(deps, workflow, digest, record);
|
|
202
250
|
if (!decided)
|
|
203
|
-
log(`workflow-first: ${workflow.name} at ${digest} is replaying on ${plural(record.runs?.length ?? 0, "graded input")}; it does not serve this run`);
|
|
251
|
+
log(`workflow-first: ${workflow.name} at ${digest} is ${record.source === "check" ? `being checked on ${record.runs?.length ?? 0} of the agent's inputs` : `replaying on ${plural(record.runs?.length ?? 0, "graded input")}`}; it does not serve this run`);
|
|
204
252
|
return;
|
|
205
253
|
}
|
|
254
|
+
// A new revision: its one check on the agent's own last runs (startCheck). Nothing else moves for it: no queue, no
|
|
255
|
+
// rival settled, no dry-run promote. Only a revision a replay's queue holds already moves as it did.
|
|
256
|
+
if (!record)
|
|
257
|
+
return startCheck(deps, workflow, digest);
|
|
206
258
|
// A new revision, or a queued one. One replay at a time, and a running replay finishes: the laid one moves only when it
|
|
207
259
|
// is the newest revision (the one the learning folder's main holds now); a run pinned to an older commit lays an older
|
|
208
260
|
// text and starts nothing, whenever it was launched (a queued one it lays is skipped for the newest). With the records at
|
|
@@ -252,7 +304,7 @@ async function decide(deps, workflow, digest) {
|
|
|
252
304
|
}
|
|
253
305
|
return;
|
|
254
306
|
}
|
|
255
|
-
const reserved = await reserve(deps, workflow, digest, graded, `replaying on the agent's last ${plural(graded.length, "graded input")} it fits${after}`, fresh, [], skips);
|
|
307
|
+
const reserved = await reserve(deps, workflow, digest, gradedRuns(graded), `replaying on the agent's last ${plural(graded.length, "graded input")} it fits${after}`, fresh, [], skips);
|
|
256
308
|
if (!reserved.won) {
|
|
257
309
|
const rival = replayingOf(await promotionsOf(store.settings, agentId), workflow.name, digest)[0];
|
|
258
310
|
log(`workflow-first: ${workflow.name} at ${digest}: ${rival ? `${rival.digest} began replaying first` : "the queue moved since this claim read it (a claim that read a newer revision acted first)"}; it does not serve this run`);
|
|
@@ -261,6 +313,223 @@ async function decide(deps, workflow, digest) {
|
|
|
261
313
|
skipped();
|
|
262
314
|
log(`workflow-first: ${workflow.name} at ${digest} is ${record ? "next" : "new"}: replaying it on the agent's last ${plural(graded.length, "graded input")} before it serves`);
|
|
263
315
|
}
|
|
316
|
+
/** The loop's numbers (learning-setting.ts) of the revision the claim runs: its definition's `learning`, each one it
|
|
317
|
+
* leaves out at its default (a revision that cannot be read: every default). */
|
|
318
|
+
async function loopOfRevision(deps) {
|
|
319
|
+
try {
|
|
320
|
+
const revision = await deps.store.agents.revision(deps.revisionId);
|
|
321
|
+
return loopOf(revision?.body?.learning);
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return loopOf(undefined);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** Which check of a compile a new revision's is: 2 when the workflow's newest record of another revision is a first
|
|
328
|
+
* check's hold (this revision is its one recompile), else 1. A stop with nothing checked (stopUnchecked) is no first
|
|
329
|
+
* check's hold: it spends no recompile. */
|
|
330
|
+
function attemptOf(records, name, digest) {
|
|
331
|
+
const before = records.findLast((one) => one.workflow === name && one.digest !== digest);
|
|
332
|
+
return before?.decided === "hold" && before.source === "check" && before.check?.attempt === 1 && !before.check.stopped ? 2 : 1;
|
|
333
|
+
}
|
|
334
|
+
/** The inputs a check runs its draft on: the agent's last `n` completed runs of its own (its learned workflow did not
|
|
335
|
+
* answer them alone, and none is a replay; never the claimed run, an archived or imported one), newest first, picked
|
|
336
|
+
* before anything else limits them: runs its workflow served since, however many, never push them out of the read (it
|
|
337
|
+
* reads more of the agent's ended runs until it has `n`, or there are no more). Of those, the ones whose input `workflow`
|
|
338
|
+
* takes (fits), each with its launch read back; one with an uploaded file is never an input. With how many of the
|
|
339
|
+
* agent's runs it looked at (`looked`, at most `n`) and why each it looked at is no input (`unfit`, `files`). */
|
|
340
|
+
async function checkInputs(deps, workflow, n) {
|
|
341
|
+
const { resolveSchemaForWorkflow } = await import("@parcha/agentrun-dsl");
|
|
342
|
+
const own = await lastOwnRuns(deps.store, deps.agentId, n, deps.runId);
|
|
343
|
+
const out = [];
|
|
344
|
+
let files = 0;
|
|
345
|
+
for (const row of own) {
|
|
346
|
+
const launch = await deps.store.runs.launch(row.id).then((one) => readLaunchBody(one.body), () => undefined);
|
|
347
|
+
if (launch?.files?.length)
|
|
348
|
+
files += 1;
|
|
349
|
+
if (!launch || launch.files?.length || launch.replay || !fits(workflow, launch.input, resolveSchemaForWorkflow))
|
|
350
|
+
continue;
|
|
351
|
+
out.push({ run: row.id, launch });
|
|
352
|
+
}
|
|
353
|
+
return { inputs: out, looked: own.length, unfit: own.length - out.length - files, files };
|
|
354
|
+
}
|
|
355
|
+
/** Starts a new revision's one check: a replay run of it on each input of the agent's last compile_after completed runs
|
|
356
|
+
* (checkInputs), at workflow cost, the agent taking the runs meanwhile; reserved with the bar its definition says now
|
|
357
|
+
* (CheckBar) and only while no other revision of the workflow is replaying (one at a time). A later claim, or the
|
|
358
|
+
* replay's read, decides it once its runs ended (settleCheck). With no input it takes (a workflow landed before any run
|
|
359
|
+
* it fits), it is decided at once: never promoted, it stops with nothing checked (stopUnchecked). */
|
|
360
|
+
async function startCheck(deps, workflow, digest) {
|
|
361
|
+
const { store, agentId, log } = deps;
|
|
362
|
+
// Only the learning folder's newest revision is checked: a run pinned to an older commit lays an older text.
|
|
363
|
+
const newest = deps.newest ? await deps.newest(workflow.name) : undefined;
|
|
364
|
+
if (newest !== undefined && newest !== digest) {
|
|
365
|
+
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`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
// One check at a time, and one at work finishes: decided here once its runs ended (its verdict stands), never
|
|
369
|
+
// cancelled. While it runs, this revision waits, recording nothing: the claim after it is decided starts this one.
|
|
370
|
+
for (const rival of replayingOf(await promotionsOf(store.settings, agentId), workflow.name, digest))
|
|
371
|
+
await settleRival(deps, workflow.name, rival);
|
|
372
|
+
const records = await promotionsOf(store.settings, agentId);
|
|
373
|
+
const rival = replayingOf(records, workflow.name, digest)[0];
|
|
374
|
+
if (rival) {
|
|
375
|
+
log(`workflow-first: ${workflow.name} at ${digest} waits: ${rival.digest} is ${rival.source === "check" ? "being checked" : "replaying"} (one check at a time); it does not serve this run`);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const loop = await loopOfRevision(deps);
|
|
379
|
+
const bar = { compile_after: loop.compile_after, agree_min: loop.agree_min, alone_min: loop.alone_min, attempt: attemptOf(records, workflow.name, digest) };
|
|
380
|
+
const { inputs, looked, unfit, files } = await checkInputs(deps, workflow, loop.compile_after);
|
|
381
|
+
if (!inputs.length) {
|
|
382
|
+
// Nothing of the agent's to check it on (a workflow landed before any run it fits): never promoted unchecked. It
|
|
383
|
+
// stops, its record naming why (the compile's draft error), and asks no recompile: a revision would have nothing to be
|
|
384
|
+
// checked on either.
|
|
385
|
+
const why = !looked
|
|
386
|
+
? "the agent has no completed run of its own to check it against"
|
|
387
|
+
: `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` : ""})` : ""}`;
|
|
388
|
+
await stopUnchecked(deps, workflow.name, digest, bar, why, (was) => was === undefined);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const said = `checking it on ${plural(inputs.length, "input")} of the agent's last ${plural(bar.compile_after, "completed run")} against its own answers`;
|
|
392
|
+
const reserved = await reserve(deps, workflow, digest, inputs, said, (was) => was === undefined, [], [], bar);
|
|
393
|
+
if (!reserved.won) {
|
|
394
|
+
log(`workflow-first: ${workflow.name} at ${digest}: another claim's decision on it came first; it does not serve this run`);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
log(`workflow-first: ${workflow.name} at ${digest} is new: ${said} before it serves`);
|
|
398
|
+
}
|
|
399
|
+
/** The words a stopped check's record starts with when it had nothing to check its draft on. */
|
|
400
|
+
export const NOTHING_TO_CHECK = "nothing to check it on";
|
|
401
|
+
/** 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
|
|
402
|
+
* write): held, never promoted unchecked, its record `stopped` (nothing compiles again until a hold or its owner acts)
|
|
403
|
+
* and naming the cause, which the compile's draft errors show (store-reads/loop.ts). It asks no recompile: a revision
|
|
404
|
+
* would have nothing to be checked on either, and a stop with nothing checked never counts as a compile's first check
|
|
405
|
+
* (attemptOf). Kept only while `still` holds of the digest's record (the one write). */
|
|
406
|
+
async function stopUnchecked(deps, name, digest, bar, why, still, check = {}, extra = {}) {
|
|
407
|
+
const reason = `${NOTHING_TO_CHECK}: ${why}. It is not promoted, and it stops: nothing compiles again until a hold or its owner acts`;
|
|
408
|
+
const decided = await recordPromotion(deps.store.settings, deps.agentId, { workflow: name, digest, at: iso((deps.now ?? Date.now)()), decided: "hold", source: "check", reason, check: { ...bar, n: 0, agreed: 0, alone: 0, of: [], ...check, stopped: true }, ...seedOf(deps), ...extra }, still);
|
|
409
|
+
if (decided.won)
|
|
410
|
+
deps.log(`workflow-first: ${name} at ${digest} stopped: ${reason}`);
|
|
411
|
+
return decided;
|
|
412
|
+
}
|
|
413
|
+
/** The guard (or recorded escalate) of a check row that would hand its input back when serving: a replay reads every
|
|
414
|
+
* guard record-only and records an escalate on a judge's field instead of obeying it (recordOnly, softEscalates), so a
|
|
415
|
+
* row it answered is alone only when each of its rows' p clears the workflow's serving threshold (`thresholds`, by step
|
|
416
|
+
* label; a row's own threshold for a step with none there: an escalate's). The scorer's rule (learn/refit.ts scoreAt). */
|
|
417
|
+
const handsBackAt = (row, thresholds) => row.guards.find((guard) => guard.p === null || guard.p < (thresholds.get(guard.node) ?? guard.threshold));
|
|
418
|
+
/** A check's rows, once every replay run ended (or REPLAY_DEADLINE_MS passed); undefined while one is at work or not
|
|
419
|
+
* created yet. One row per reserved input, so every one counts: a run not ended by the deadline failed, and so did one
|
|
420
|
+
* never created by then (`unstarted`: the server stopped after reserving it, say), its input read from the agent's run
|
|
421
|
+
* the record names for it. With what its runs cost, their outcomes, and how many were never created by the deadline.
|
|
422
|
+
* No grade is read: the label is the agent's own recorded answer. */
|
|
423
|
+
async function checkRows(deps, record) {
|
|
424
|
+
const { store, context } = deps;
|
|
425
|
+
const runs = await Promise.all((record.runs ?? []).map((id) => store.runs.get(id)));
|
|
426
|
+
const late = (deps.now ?? Date.now)() - Date.parse(record.at) > REPLAY_DEADLINE_MS;
|
|
427
|
+
if (!late && runs.some((row) => !row || !FINAL_STATES.includes(row.state)))
|
|
428
|
+
return undefined;
|
|
429
|
+
const rows = [];
|
|
430
|
+
for (const [i, run] of runs.entries()) {
|
|
431
|
+
const reservedOf = record.inputs?.[i];
|
|
432
|
+
const launch = run ? await store.runs.launch(run.id).then((one) => readLaunchBody(one.body), () => undefined) : undefined;
|
|
433
|
+
const replayed = launch?.replay?.of;
|
|
434
|
+
if (!run || !launch || !replayed) {
|
|
435
|
+
// Never created (or its launch unreadable): a failed input, never left out of n.
|
|
436
|
+
const of = reservedOf;
|
|
437
|
+
const from = of ? await store.runs.launch(of).then((one) => readLaunchBody(one.body), () => undefined) : undefined;
|
|
438
|
+
rows.push({ of: of ?? record.runs[i], input: from ? inputText(from.input) : "", agent: of ? await answerOf(store, of, context) : null, outcome: "failed", answer: null, guards: [], ...(run ? {} : { unstarted: true }) });
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
const outcome = outcomeOf(run);
|
|
442
|
+
const stoppedAt = outcome === "skipped_write" ? metricsOf(run).replay?.skipped : undefined;
|
|
443
|
+
const guards = metricsOf(run).guards;
|
|
444
|
+
rows.push({
|
|
445
|
+
of: replayed, input: inputText(launch.input), agent: await answerOf(store, replayed, context), outcome, guards: Array.isArray(guards) ? guards : [],
|
|
446
|
+
answer: outcome === "answered" ? await answerOf(store, run.id, context) : null,
|
|
447
|
+
...(outcome === "handed_back" ? { escalated: (await handedBackOf(store, run.id, context)) ?? "" } : {}),
|
|
448
|
+
...(stoppedAt ? { skipped: stoppedAt } : {}),
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
return { rows, cost: replayCost(runs, late), outcomes: outcomesOf(runs, late), unstarted: runs.filter((row) => !row).length };
|
|
452
|
+
}
|
|
453
|
+
/** A run's input as a check's disagreement shows it: its text, else its JSON. */
|
|
454
|
+
const inputText = (input) => (typeof input === "string" ? input : JSON.stringify(input) ?? "");
|
|
455
|
+
/** What a replay run's workflow said when it handed its input back: its learned_workflow call's result. */
|
|
456
|
+
async function handedBackOf(store, runId, context) {
|
|
457
|
+
try {
|
|
458
|
+
const rows = await runTimeline(store, runId, context, { settled: true, caps: { text: Number.POSITIVE_INFINITY } });
|
|
459
|
+
return rows.findLast((row) => row.kind === "tool_result" && row.tool_name === LEARNED_WORKFLOW_TOOL)?.tool_result_preview ?? undefined;
|
|
460
|
+
}
|
|
461
|
+
catch {
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** A replaying check's decision, once its replay runs ended (checkRows); undefined while one is at work. */
|
|
466
|
+
async function settleCheck(deps, workflow, digest, record) {
|
|
467
|
+
const { name } = workflow;
|
|
468
|
+
const read = await checkRows(deps, record);
|
|
469
|
+
if (!read)
|
|
470
|
+
return undefined;
|
|
471
|
+
const bar = record.check ?? { ...(await loopOfRevision(deps)), attempt: 1 };
|
|
472
|
+
const decided = await decideCheck(deps, name, digest, bar, read, thresholdsOf(workflow.workflow), (was) => sameReplay(was, record), { ...(record.runs ? { runs: record.runs } : {}), ...(record.replay_id ? { replay_id: record.replay_id } : {}), ...(record.commit ? { commit: record.commit } : {}) });
|
|
473
|
+
if (decided.won) {
|
|
474
|
+
const cancelled = await cancelUnended(deps, record);
|
|
475
|
+
if (cancelled)
|
|
476
|
+
deps.log(`workflow-first: ${name} at ${digest}: ${plural(cancelled, "replay run")} still at work past the check's deadline cancelled`);
|
|
477
|
+
}
|
|
478
|
+
return decided.record?.decided === "replaying" ? undefined : decided.record;
|
|
479
|
+
}
|
|
480
|
+
/** Decides a check on its rows: each input it ran on (a skipped write is none) agreed when the draft answered it alone
|
|
481
|
+
* with the agent's recorded answer (learn/promote.ts sameAnswer), alone when it answered it with no escalate, no failed
|
|
482
|
+
* step, and no guard or recorded escalate that hands it back at the workflow's serving `thresholds` (handsBackAt). Promoted (source check) when it clears the bar (checkPasses); else held with the inputs it did not
|
|
483
|
+
* agree on, which the agent's next run's compile turn reads as data (its one recompile); a recompile's check that holds
|
|
484
|
+
* it too stops: nothing compiles again. No grade, refit, queue or second replay. Kept only while `still` holds of the
|
|
485
|
+
* digest's record (the one write: of two claims at once, one decides). */
|
|
486
|
+
export async function decideCheck(deps, name, digest, bar, read, thresholds, still, extra) {
|
|
487
|
+
const now = deps.now ?? Date.now;
|
|
488
|
+
const ran = read.rows.filter((row) => row.outcome !== "skipped_write");
|
|
489
|
+
const alone = (row) => row.outcome === "answered" && !handsBackAt(row, thresholds);
|
|
490
|
+
const agrees = (row) => alone(row) && sameAnswer(row.answer, row.agent);
|
|
491
|
+
const score = { n: ran.length, agreed: ran.filter(agrees).length, alone: ran.filter(alone).length };
|
|
492
|
+
const ok = checkPasses(score, bar);
|
|
493
|
+
// Each text is cut of the host's secrets whole, then shortened: shortened first, a secret straddling the cut would leave
|
|
494
|
+
// its prefix, which no redactor matches.
|
|
495
|
+
const shown = await deps.shown();
|
|
496
|
+
const cut = (text) => {
|
|
497
|
+
const clean = shown.text(text);
|
|
498
|
+
return clean.length > CHECK_TEXT_MAX ? `${clean.slice(0, CHECK_TEXT_MAX)}…` : clean;
|
|
499
|
+
};
|
|
500
|
+
// An input it answered that a guard (or a recorded escalate) hands back when serving is a hand-back, named by its step.
|
|
501
|
+
const servedBack = (row) => {
|
|
502
|
+
const guard = handsBackAt(row, thresholds);
|
|
503
|
+
return guard ? `step ${guard.node} hands it back when serving (${guard.signal === "escalate" ? "its escalate fired" : `p ${guard.p ?? "unknown"} under ${thresholds.get(guard.node) ?? guard.threshold}`})` : undefined;
|
|
504
|
+
};
|
|
505
|
+
const disagreements = ran.filter((row) => !agrees(row)).map((row) => ({
|
|
506
|
+
run: row.of, input: cut(row.input), agent: row.agent === null ? null : cut(row.agent),
|
|
507
|
+
...(row.outcome === "answered" ? (servedBack(row) ? { escalated: servedBack(row) } : { draft: cut(row.answer ?? "") }) : row.outcome === "handed_back" ? { escalated: cut(row.escalated ?? "") } : { failed: true }),
|
|
508
|
+
}));
|
|
509
|
+
const stopped = !ok && bar.attempt === 2;
|
|
510
|
+
const pct = (value) => `${(value * 100).toFixed(1)}%`;
|
|
511
|
+
const shares = checkShares(score);
|
|
512
|
+
// Inputs stopped at a write the recording lacks are no evidence either way; one stopped at an MCP tool its server listed
|
|
513
|
+
// with no readOnlyHint names that tool (skipWordsOf), never a data problem.
|
|
514
|
+
const tools = skippedToolsOf(read.rows);
|
|
515
|
+
const { skipped_write: skipped, failed } = read.outcomes;
|
|
516
|
+
const { hint, why: skipWhy } = skipWordsOf(tools, skipped);
|
|
517
|
+
const numbers = { of: read.rows.map((row) => row.of), cost_usd: read.cost, outcomes: read.outcomes, ...(tools.length ? { skipped_tools: tools } : {}) };
|
|
518
|
+
// Every input skipped at a write (each reserved input counts: one never created or never ended failed, so this is the
|
|
519
|
+
// only way to none): nothing to check it on, so it stops, never promoted unchecked, and asks no recompile.
|
|
520
|
+
if (!score.n) {
|
|
521
|
+
const why = skipped ? `${plural(skipped, "input")} skipped because ${hint ? skipWhy : `each ${SKIPPED_WRITE} (a check never runs a write)`}` : "it ran on no input";
|
|
522
|
+
return stopUnchecked(deps, name, digest, bar, why, still, numbers, extra);
|
|
523
|
+
}
|
|
524
|
+
const aside = [...(failed ? [`${failed} 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("; ");
|
|
525
|
+
const said = `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})` : ""}`;
|
|
526
|
+
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`;
|
|
527
|
+
const check = { ...bar, ...score, ...numbers, ...(disagreements.length ? { disagreements } : {}), ...(stopped ? { stopped: true } : {}) };
|
|
528
|
+
const decided = await recordPromotion(deps.store.settings, deps.agentId, { workflow: name, digest, at: iso(now()), decided: ok ? "promote" : "hold", source: "check", reason, check, ...seedOf(deps), ...extra }, still);
|
|
529
|
+
if (decided.won)
|
|
530
|
+
deps.log(`workflow-first: ${name} at ${digest} ${ok ? "promoted" : "held"}: ${reason}`);
|
|
531
|
+
return decided;
|
|
532
|
+
}
|
|
264
533
|
/** Whether `was`, the laid revision's newest record now, is still `record`, the one its claim read: none, or the same
|
|
265
534
|
* queued record. */
|
|
266
535
|
const ownStill = (record) => (was) => (record ? was?.decided === "queued" && was.at === record.at : was === undefined);
|
|
@@ -317,12 +586,18 @@ async function skip(deps, record, by) {
|
|
|
317
586
|
* it then). */
|
|
318
587
|
async function settleRival(deps, name, running) {
|
|
319
588
|
const text = running.commit && deps.champion ? await deps.champion(name, running.commit).catch(() => undefined) : undefined;
|
|
320
|
-
|
|
589
|
+
const readable = text !== undefined && workflowDigest(text) === running.digest;
|
|
590
|
+
const ports = { ...deps, commit: undefined };
|
|
591
|
+
if (!readable) {
|
|
321
592
|
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`);
|
|
322
593
|
return;
|
|
323
594
|
}
|
|
324
|
-
const ports = { ...deps, commit: undefined };
|
|
325
595
|
await relaunch(ports, text, running.digest, running);
|
|
596
|
+
// A check ahead (source check) is decided on its runs and the agent's answers, its guards at the text's thresholds.
|
|
597
|
+
if (running.source === "check") {
|
|
598
|
+
await settleCheck(ports, text, running.digest, running);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
326
601
|
const decided = await settle(ports, text, running.digest, running);
|
|
327
602
|
if (decided?.decided === "hold")
|
|
328
603
|
deps.log(`workflow-first: ${name} at ${running.digest} held with a newer revision waiting: no refit of it is written`);
|
|
@@ -333,16 +608,17 @@ async function settleRival(deps, name, running) {
|
|
|
333
608
|
* a claim of the same digest, or of another revision, or a person's ask, at once, starts none. A claim's (decide) and a
|
|
334
609
|
* person's (askReplay) are this one reservation. `owed`: the held runs among `graded` it must answer on (heldOn). `also`:
|
|
335
610
|
* records kept in the same write (a claim's skips of the revisions queued before it). */
|
|
336
|
-
async function reserve(deps, workflow, digest,
|
|
611
|
+
async function reserve(deps, workflow, digest, inputs, reason, still, owed = [], also = [], check) {
|
|
337
612
|
const now = deps.now ?? Date.now;
|
|
338
|
-
const ids =
|
|
613
|
+
const ids = inputs.map(() => uuidv7());
|
|
339
614
|
const launch = replayLaunchOf(deps);
|
|
340
|
-
const reserved = await recordPromotion(deps.store.settings, deps.agentId, { workflow: workflow.name, digest, at: iso(now()), decided: "replaying", source: "replay", reason, runs: ids, inputs:
|
|
615
|
+
const reserved = await recordPromotion(deps.store.settings, deps.agentId, { workflow: workflow.name, digest, at: iso(now()), decided: "replaying", source: check ? "check" : "replay", reason, runs: ids, inputs: inputs.map((one) => one.run), replay_id: uuidv7(), launch, ...seedOf(deps), ...(owed.length ? { held_on: owed } : {}), ...(check ? { check } : {}) }, (was, items) => still(was, items) && replayingOf(items, workflow.name, digest).length === 0, also);
|
|
341
616
|
if (reserved.won)
|
|
342
|
-
for (const [i, one] of
|
|
343
|
-
await startReplay(deps, workflow, digest, one.
|
|
617
|
+
for (const [i, one] of inputs.entries())
|
|
618
|
+
await startReplay(deps, workflow, digest, one.run, one.launch, ids[i], launch);
|
|
344
619
|
return reserved;
|
|
345
620
|
}
|
|
621
|
+
const gradedRuns = (inputs) => inputs.map((one) => ({ run: one.grade.run, launch: one.launch }));
|
|
346
622
|
/** The launch a replay's runs all run with (PromotionRecord.launch), chosen once by the claim or person reserving it:
|
|
347
623
|
* `deps.launch` as a replay run takes it (a claim's claimed run's; a person's ask makes one now), the revision it runs,
|
|
348
624
|
* and the claimed run as the one whose per_run MCP credentials its runs are handed, when the server can seal them. A
|
|
@@ -586,6 +862,15 @@ async function replayRows(deps, record, partial = false) {
|
|
|
586
862
|
}
|
|
587
863
|
return { rows, scored: refitRowsOf(rows), cost: replayCost(runs, late), outcomes: outcomesOf(runs, late), unstarted: runs.filter((row) => !row).length };
|
|
588
864
|
}
|
|
865
|
+
/** Why `skipped` inputs of a replay were skipped, in one phrase (`why`): the MCP tools among `tools` its server listed
|
|
866
|
+
* with no readOnlyHint, named (`hint`: the server may only lack the hint), with the rest in the recording's words;
|
|
867
|
+
* with none of those, the recording's words alone (SKIPPED_WRITE). */
|
|
868
|
+
function skipWordsOf(tools, skipped) {
|
|
869
|
+
const unmarked = tools.filter((one) => one.no_read_only_hint);
|
|
870
|
+
const writes = skipped - unmarked.reduce((sum, one) => sum + one.inputs, 0);
|
|
871
|
+
const hint = unmarked.length ? unmarkedWords(unmarked.map((one) => toolWord(one.tool))) : undefined;
|
|
872
|
+
return { hint, why: !hint ? SKIPPED_WRITE : writes ? `${hint} (${writes} more ${writes === 1 ? "needs" : "need"} a write the recording doesn't have)` : hint };
|
|
873
|
+
}
|
|
589
874
|
/** A replaying digest's decision, once its replay runs ended: promote within the bar when it answered alone at least once,
|
|
590
875
|
* else hold, then one refit (of its guards and recorded escalates). Undefined while a replay run is at work. */
|
|
591
876
|
async function settle(deps, workflow, digest, record) {
|
|
@@ -617,10 +902,7 @@ async function settle(deps, workflow, digest, record) {
|
|
|
617
902
|
// Inputs stopped at an MCP tool its server listed with no readOnlyHint: the hold names those tools (the server may only
|
|
618
903
|
// lack the hint); any other skip (a destructive tool, a built-in write) keeps the recording's words.
|
|
619
904
|
const tools = skippedToolsOf(read.rows);
|
|
620
|
-
const
|
|
621
|
-
const writes = skipped - unmarked.reduce((sum, one) => sum + one.inputs, 0);
|
|
622
|
-
const hint = unmarked.length ? unmarkedWords(unmarked.map((one) => toolWord(one.tool))) : undefined;
|
|
623
|
-
const skipWhy = !hint ? SKIPPED_WRITE : writes ? `${hint} (${writes} more ${writes === 1 ? "needs" : "need"} a write the recording doesn't have)` : hint;
|
|
905
|
+
const { hint, why: skipWhy } = skipWordsOf(tools, skipped);
|
|
624
906
|
const aside = [...(failed ? [`${failed} failed: a step failed, no evidence either way`] : []), ...(skipped ? [`${skipped} skipped: ${skipWhy}`] : []),
|
|
625
907
|
...(read.unstarted ? [`${read.unstarted} never started by the replay's deadline`] : [])].join("; ");
|
|
626
908
|
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})` : ""}`;
|
|
@@ -805,15 +1087,33 @@ export async function askReplay(ask, name, digest, inputs) {
|
|
|
805
1087
|
const { launch, revisionId } = await ask.launchOf(graded[0].launch.input);
|
|
806
1088
|
if (!launch.learning || launch.learning.notesOnly)
|
|
807
1089
|
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).`);
|
|
808
|
-
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 } : {}) };
|
|
1090
|
+
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 } : {}) };
|
|
809
1091
|
const said = `replaying on ${plural(graded.length, "graded input")}${owed.length ? ` (the ${plural(owed.length, "served run")} graded wrong it was held on among them)` : ""}, as its owner asked`;
|
|
810
1092
|
// Kept only while the revision's newest record is still the one read here: each record write stamps a new `at`.
|
|
811
|
-
const reserved = await reserve(deps, found.workflow, digest, graded, said, (was) => was?.at === own?.at && was?.decided === own?.decided, owed);
|
|
1093
|
+
const reserved = await reserve(deps, found.workflow, digest, gradedRuns(graded), said, (was) => was?.at === own?.at && was?.decided === own?.decided, owed);
|
|
812
1094
|
if (!reserved.won || !reserved.record?.replay_id)
|
|
813
1095
|
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.`);
|
|
814
1096
|
ask.log(`workflow-first: ${name} at ${digest}: ${said}`);
|
|
815
1097
|
return { replay: reserved.record.replay_id, inputs: graded.length };
|
|
816
1098
|
}
|
|
1099
|
+
/** A check's replay as its read answers it (replayView): decided, its record's numbers (CheckRecord), else its runs ended
|
|
1100
|
+
* so far. Its `score` in the replay's shape with the agent's own answers as the label: `agree` the inputs the draft
|
|
1101
|
+
* answered alone as the agent did, `agent_acc` 1, `wf_acc` those plus the ones handed back (a hand-back is the agent's
|
|
1102
|
+
* answer), over the inputs it ran on. */
|
|
1103
|
+
async function checkView(ask, record, id, name, digest) {
|
|
1104
|
+
const inputs = record.runs?.length ?? 0;
|
|
1105
|
+
const decided = record.decided === "promote" || record.decided === "hold" ? record.decided : undefined;
|
|
1106
|
+
const check = record.check;
|
|
1107
|
+
if (decided && check?.outcomes) {
|
|
1108
|
+
const n = check.n ?? 0;
|
|
1109
|
+
const handedBack = check.outcomes.handed_back;
|
|
1110
|
+
const wf = n ? Math.round(((check.agreed ?? 0) + handedBack) / n * 1e4) / 1e4 : 0;
|
|
1111
|
+
const neverStarted = inputs > 0 && Object.values(check.outcomes).every((count) => count === 0);
|
|
1112
|
+
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 };
|
|
1113
|
+
}
|
|
1114
|
+
const runs = await Promise.all((record.runs ?? []).map((run) => ask.store.runs.get(run)));
|
|
1115
|
+
return { replay: id, workflow: name, digest, status: decided ? "done" : "running", inputs, outcomes: outcomesOf(runs, false), score: scoreRows([], new Map()), cost_usd: replayCost(runs, false), ...(decided ? { decided, reason: record.reason } : {}) };
|
|
1116
|
+
}
|
|
817
1117
|
/** The claim's ports a replay's read settles it through (replayView), with the launch its record chose when it was
|
|
818
1118
|
* reserved (PromotionRecord.launch, the one every replay run of it runs with): its revision, and its learning commit (a
|
|
819
1119
|
* refit lands as a merge from it). Undefined when that launch cannot be read. */
|
|
@@ -840,9 +1140,11 @@ export async function replayView(ask, name, digest, id) {
|
|
|
840
1140
|
const ports = record.decided === "replaying" && text && record.launch ? readPorts(ask, record.launch) : undefined;
|
|
841
1141
|
if (ports && text) {
|
|
842
1142
|
// Read back by its id: whatever settled it (this read, a claim, another read), never another replay's record.
|
|
843
|
-
await settle(ports, text, digest, record);
|
|
1143
|
+
await (record.source === "check" ? settleCheck(ports, text, digest, record) : settle(ports, text, digest, record));
|
|
844
1144
|
record = (await find()) ?? record;
|
|
845
1145
|
}
|
|
1146
|
+
if (record.source === "check")
|
|
1147
|
+
return checkView(ask, record, id, name, digest);
|
|
846
1148
|
// Only a replaying, promote or hold record carries a replay's id.
|
|
847
1149
|
const decided = record.decided === "promote" || record.decided === "hold" ? record.decided : undefined;
|
|
848
1150
|
const inputs = record.runs?.length ?? 0;
|
|
@@ -43,6 +43,11 @@ export interface ReflectDeps {
|
|
|
43
43
|
* asked its end-of-run turn (no model request, nothing spent looking back), and its learning is dropped `empty` in
|
|
44
44
|
* said.onlyWorkflow's words. Absent, or a read that throws: every run is asked. */
|
|
45
45
|
readonly nothingNew?: (runId: string) => Promise<boolean>;
|
|
46
|
+
/** For a job that compiles workflows: whether the run carries its owner's feedback (learning-end.ts ownerSaid: a grade's
|
|
47
|
+
* words, or a message after its first). A run without it is not asked its look-back (no model request, $0 looking
|
|
48
|
+
* back), and its compile turn, when one is due, is asked alone; with nothing due its learning is dropped `empty` in
|
|
49
|
+
* said.noFeedback's words. Absent (a notes job), or a read that throws: the run is asked its look-back. */
|
|
50
|
+
readonly ownerSaid?: (runId: string) => Promise<boolean>;
|
|
46
51
|
/** The run's facts for reflectSkip: whether it learns, a person's cancel, its cap reached, its cap, its cost so far as a
|
|
47
52
|
* person is shown it (`costUsd`, kept as the job's), and under a cap its spend as the cap's gates read it (`spentUsd`,
|
|
48
53
|
* learning-end.ts capSpendOf: a request with no price of its own at its held figure; null: unknown). */
|
|
@@ -86,18 +86,26 @@ export function createReflectHook(deps) {
|
|
|
86
86
|
// its workflow, asked while its champion serves) looking back cost nothing, $0 whether or not the job's own cost is
|
|
87
87
|
// known. Its words are the server's own.
|
|
88
88
|
const workflowOnly = input.outcome === "completed" && deps.nothingNew !== undefined && (await deps.nothingNew(input.runId).catch(() => false));
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
// A run of a job that compiles workflows with no word from its owner is not asked its look-back either (ownerSaid):
|
|
90
|
+
// its job's trace is what a compile reads, and a look-back on every agent run cost more than the run. The same way:
|
|
91
|
+
// its compile turn, when due, asked alone, else $0 looking back.
|
|
92
|
+
const unheard = !workflowOnly && deps.ownerSaid !== undefined && !(await deps.ownerSaid(input.runId).catch(() => true));
|
|
93
|
+
const alone = workflowOnly || unheard;
|
|
94
|
+
const notAsked = workflowOnly ? said.onlyWorkflow : said.noFeedback;
|
|
95
|
+
const onlyWorkflow = () => ({ also: [droppedRow(null, "empty", notAsked, { answered_at: answeredAt, job_cost_usd: job, look_back_usd: 0, ...followUp() })] });
|
|
96
|
+
if (alone && !deps.compileFacts)
|
|
91
97
|
return onlyWorkflow();
|
|
92
98
|
// What the run must never learn, read once, before it is asked anything: a run whose list can't be read learns
|
|
93
99
|
// nothing, never over a shorter one.
|
|
94
100
|
const read = deps.refuse ? await deps.refuse(input.runId).catch(() => "unreadable") : [];
|
|
95
101
|
const refuse = read === "unreadable" ? [] : read;
|
|
96
|
-
// The compile turn's facts (learn/compile.ts), read once: a
|
|
102
|
+
// The compile turn's facts (learn/compile.ts), read once: a run not looked back on with none due ends here, as above.
|
|
97
103
|
const compileOf = async () => (deps.compileFacts && read !== "unreadable" ? await deps.compileFacts({ runId: input.runId, agentId: input.agentId, outcome: input.outcome, refuse }).catch(() => undefined) : undefined);
|
|
98
|
-
const early =
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
const early = alone ? await compileOf() : undefined;
|
|
105
|
+
// None due now, but a compile request this run already asked and kept (a crash cut it, another run merged a workflow
|
|
106
|
+
// since) was paid: its cost stays the look-back's, as every other cut's does (`begun`), never the task's.
|
|
107
|
+
if (alone && !early)
|
|
108
|
+
return begun ? unasked(droppedRow(null, "empty", notAsked)) : onlyWorkflow();
|
|
101
109
|
// Every end from here closes through this one function: looked's timings first, then the cut LAST, over every
|
|
102
110
|
// string the kept rows hold, whatever looked adds now or later.
|
|
103
111
|
const close = async (rows) => ({ also: (await looked({ also: rows })).also.map((row) => cutRow(row, refuse)) });
|
|
@@ -116,11 +124,11 @@ export function createReflectHook(deps) {
|
|
|
116
124
|
return undefined;
|
|
117
125
|
}
|
|
118
126
|
};
|
|
119
|
-
// The look-back: asked of every run but a workflow-only one
|
|
120
|
-
//
|
|
127
|
+
// The look-back: asked of every run but a workflow-only one or one its owner said nothing on, whose compile turn is
|
|
128
|
+
// asked alone, from no answer of its own: nothing the look-back kept goes with its draft.
|
|
121
129
|
let turn = null;
|
|
122
130
|
let checked = { state: "dropped", reason: "empty" };
|
|
123
|
-
if (
|
|
131
|
+
if (alone)
|
|
124
132
|
heard = await deps.asked(input.runId);
|
|
125
133
|
else {
|
|
126
134
|
const { active } = await deps.promptFacts(input.runId, input.agentId);
|
|
@@ -172,10 +180,10 @@ export function createReflectHook(deps) {
|
|
|
172
180
|
// The compile turn (learn/compile.ts): the job as a learned workflow, or a revision of one, checked as a draft is,
|
|
173
181
|
// its refusals sent back up to COMPILE_RETRIES times. A draft that passed goes in the proposal beside the notes; one
|
|
174
182
|
// that never did is never silent: its errors go with the learning, and its problems to its end.
|
|
175
|
-
const compile =
|
|
183
|
+
const compile = alone ? early : await compileOf();
|
|
176
184
|
let compiled;
|
|
177
185
|
if (compile) {
|
|
178
|
-
// A compile turn asked alone (
|
|
186
|
+
// A compile turn asked alone (no look-back) is the round's first ask: it keeps the job's figures
|
|
179
187
|
// with its prompt as the look-back's does, and a turn an earlier claim asked first answers with that ask's (`kept`),
|
|
180
188
|
// so a restart mid compile never moves what the earlier attempt spent compiling into the job's. Each attempt keeps
|
|
181
189
|
// the figures as they stand once the attempts before it answered (an earlier claim's, when it asked first).
|
|
@@ -189,11 +197,11 @@ export function createReflectHook(deps) {
|
|
|
189
197
|
let capped = false;
|
|
190
198
|
const result = await compileTurn({
|
|
191
199
|
ask: async (prompt, attempt, left) => {
|
|
192
|
-
const keep = { ...(
|
|
200
|
+
const keep = { ...(alone ? { answered_at: answeredAt, job_cost_usd: job } : {}), ...(revised ? { compile_base: revised.base } : {}) };
|
|
193
201
|
const said_ = await input.ask(prompt, { ...ask, deadlineMs: left, step: `compile-${attempt}`, ...(Object.keys(keep).length ? { keep } : {}) });
|
|
194
202
|
if ("failed" in said_ && CAP_REFUSAL.test(said_.failed.message))
|
|
195
203
|
capped = true;
|
|
196
|
-
if (
|
|
204
|
+
if (alone)
|
|
197
205
|
firstAsk(said_.kept);
|
|
198
206
|
const base = said_.kept?.compile_base;
|
|
199
207
|
if (revised && typeof base === "string")
|
|
@@ -233,7 +241,7 @@ export function createReflectHook(deps) {
|
|
|
233
241
|
}
|
|
234
242
|
const compiledFields = compiled ? { compiled: compiled.record, ...(compiled.draft_errors?.length ? { draft_errors: compiled.draft_errors } : {}) } : {};
|
|
235
243
|
if (checked.state === "dropped") {
|
|
236
|
-
const detail = checked.reason === "empty" ? (
|
|
244
|
+
const detail = checked.reason === "empty" ? (alone ? notAsked : said.empty) : checked.reason === "unparsed" ? said.unparsed : said.checker(checked.problems);
|
|
237
245
|
return close([droppedRow(turn, checked.reason, detail, { ...checked, ...compiledFields })]);
|
|
238
246
|
}
|
|
239
247
|
// The learning run's cap: what the run's cap leaves now its look-back is spent, read after the checks. A spend under
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
// (standard-webhooks.ts secretForms), a PEM key whole and line by line. A value that does not open under the runner's key
|
|
13
13
|
// is left out: the store never holds one in the clear, and nothing of this runner can be handed it.
|
|
14
14
|
import { CHATGPT_PLAN_ROW } from "../chatgpt-plan.js";
|
|
15
|
+
import { calendarRowSecrets } from "../calendar/address.js";
|
|
16
|
+
import { CALENDAR_ROW } from "../calendar/names.js";
|
|
15
17
|
import { accountRowSecrets, EMAIL_ACCOUNT_ROW } from "../email/account.js";
|
|
16
18
|
import { unseal } from "../sealing.js";
|
|
17
19
|
import { besideTheStore, ServerSecrets } from "../server-secrets.js";
|
|
@@ -98,7 +100,7 @@ function secretsOf(row, plain, adapters) {
|
|
|
98
100
|
case "run_launch":
|
|
99
101
|
return { listed: plain.trimStart().startsWith("{") ? launchSecrets(plain) : [plain], whole: [] };
|
|
100
102
|
case "sealed_keys":
|
|
101
|
-
return { listed: row.where.name === CHATGPT_PLAN_ROW ? planSecrets(plain) : row.where.name === EMAIL_ACCOUNT_ROW ? accountRowSecrets(plain) : [plain], whole: [] };
|
|
103
|
+
return { listed: row.where.name === CHATGPT_PLAN_ROW ? planSecrets(plain) : row.where.name === EMAIL_ACCOUNT_ROW ? accountRowSecrets(plain) : row.where.name === CALENDAR_ROW ? calendarRowSecrets(plain) : [plain], whole: [] };
|
|
102
104
|
default:
|
|
103
105
|
return { listed: [plain], whole: [] };
|
|
104
106
|
}
|