@parcha/agentrun-serve 0.1.0-beta.41 → 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 +53 -0
- package/NOTICE +2 -0
- package/README.md +77 -12
- 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-DYJ_wj27.js +711 -0
- package/dist/console/index.html +1 -1
- 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/learn/check.d.ts +14 -0
- package/dist/learn/check.js +194 -14
- 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/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 +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 +105 -9
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- 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/runner/adapter-loop.d.ts +1 -1
- package/dist/runner/adapter-loop.js +3 -6
- package/dist/runner/adapter-run-spec.d.ts +16 -1
- package/dist/runner/adapter-run-spec.js +20 -6
- package/dist/runner/beside.js +1 -1
- package/dist/runner/cli-run-spec.js +10 -7
- 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 +11 -0
- package/dist/runner/main.js +29 -2
- package/dist/runner/mcp-run.d.ts +2 -2
- package/dist/runner/promotion.d.ts +36 -4
- package/dist/runner/promotion.js +280 -20
- 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 +0 -6
- package/dist/runner/workflow-first.js +7 -13
- 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 +72 -11
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/loop.d.ts +1 -0
- package/dist/store-reads/loop.js +14 -4
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +4 -0
- 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/launcher/agentrun-serve +31 -24
- package/package.json +10 -9
- package/templates/README.md +11 -2
- package/dist/console/assets/index-COGhx_2Z.js +0 -711
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,7 +8,8 @@ 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";
|
|
@@ -185,8 +188,11 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
185
188
|
return undefined;
|
|
186
189
|
}
|
|
187
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));
|
|
188
194
|
if (champion.digest === digest)
|
|
189
|
-
return laid ? serving(workflow, await
|
|
195
|
+
return laid ? serving(workflow, await kept(workflow, champion)) : undefined;
|
|
190
196
|
if (!deps.champion)
|
|
191
197
|
return undefined;
|
|
192
198
|
const text = champion.commit ? await deps.champion(workflow.name, champion.commit).catch(() => undefined) : undefined;
|
|
@@ -194,7 +200,7 @@ async function servingNow(deps, workflow, digest, laid) {
|
|
|
194
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`);
|
|
195
201
|
return undefined;
|
|
196
202
|
}
|
|
197
|
-
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);
|
|
198
204
|
if (!under)
|
|
199
205
|
return undefined;
|
|
200
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"}`);
|
|
@@ -240,11 +246,15 @@ async function decide(deps, workflow, digest) {
|
|
|
240
246
|
}
|
|
241
247
|
if (record?.decided === "replaying") {
|
|
242
248
|
await relaunch(deps, workflow, digest, record);
|
|
243
|
-
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);
|
|
244
250
|
if (!decided)
|
|
245
|
-
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`);
|
|
246
252
|
return;
|
|
247
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);
|
|
248
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
|
|
249
259
|
// is the newest revision (the one the learning folder's main holds now); a run pinned to an older commit lays an older
|
|
250
260
|
// text and starts nothing, whenever it was launched (a queued one it lays is skipped for the newest). With the records at
|
|
@@ -294,7 +304,7 @@ async function decide(deps, workflow, digest) {
|
|
|
294
304
|
}
|
|
295
305
|
return;
|
|
296
306
|
}
|
|
297
|
-
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);
|
|
298
308
|
if (!reserved.won) {
|
|
299
309
|
const rival = replayingOf(await promotionsOf(store.settings, agentId), workflow.name, digest)[0];
|
|
300
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`);
|
|
@@ -303,6 +313,223 @@ async function decide(deps, workflow, digest) {
|
|
|
303
313
|
skipped();
|
|
304
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`);
|
|
305
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
|
+
}
|
|
306
533
|
/** Whether `was`, the laid revision's newest record now, is still `record`, the one its claim read: none, or the same
|
|
307
534
|
* queued record. */
|
|
308
535
|
const ownStill = (record) => (was) => (record ? was?.decided === "queued" && was.at === record.at : was === undefined);
|
|
@@ -359,12 +586,18 @@ async function skip(deps, record, by) {
|
|
|
359
586
|
* it then). */
|
|
360
587
|
async function settleRival(deps, name, running) {
|
|
361
588
|
const text = running.commit && deps.champion ? await deps.champion(name, running.commit).catch(() => undefined) : undefined;
|
|
362
|
-
|
|
589
|
+
const readable = text !== undefined && workflowDigest(text) === running.digest;
|
|
590
|
+
const ports = { ...deps, commit: undefined };
|
|
591
|
+
if (!readable) {
|
|
363
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`);
|
|
364
593
|
return;
|
|
365
594
|
}
|
|
366
|
-
const ports = { ...deps, commit: undefined };
|
|
367
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
|
+
}
|
|
368
601
|
const decided = await settle(ports, text, running.digest, running);
|
|
369
602
|
if (decided?.decided === "hold")
|
|
370
603
|
deps.log(`workflow-first: ${name} at ${running.digest} held with a newer revision waiting: no refit of it is written`);
|
|
@@ -375,16 +608,17 @@ async function settleRival(deps, name, running) {
|
|
|
375
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
|
|
376
609
|
* person's (askReplay) are this one reservation. `owed`: the held runs among `graded` it must answer on (heldOn). `also`:
|
|
377
610
|
* records kept in the same write (a claim's skips of the revisions queued before it). */
|
|
378
|
-
async function reserve(deps, workflow, digest,
|
|
611
|
+
async function reserve(deps, workflow, digest, inputs, reason, still, owed = [], also = [], check) {
|
|
379
612
|
const now = deps.now ?? Date.now;
|
|
380
|
-
const ids =
|
|
613
|
+
const ids = inputs.map(() => uuidv7());
|
|
381
614
|
const launch = replayLaunchOf(deps);
|
|
382
|
-
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);
|
|
383
616
|
if (reserved.won)
|
|
384
|
-
for (const [i, one] of
|
|
385
|
-
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);
|
|
386
619
|
return reserved;
|
|
387
620
|
}
|
|
621
|
+
const gradedRuns = (inputs) => inputs.map((one) => ({ run: one.grade.run, launch: one.launch }));
|
|
388
622
|
/** The launch a replay's runs all run with (PromotionRecord.launch), chosen once by the claim or person reserving it:
|
|
389
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,
|
|
390
624
|
* and the claimed run as the one whose per_run MCP credentials its runs are handed, when the server can seal them. A
|
|
@@ -628,6 +862,15 @@ async function replayRows(deps, record, partial = false) {
|
|
|
628
862
|
}
|
|
629
863
|
return { rows, scored: refitRowsOf(rows), cost: replayCost(runs, late), outcomes: outcomesOf(runs, late), unstarted: runs.filter((row) => !row).length };
|
|
630
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
|
+
}
|
|
631
874
|
/** A replaying digest's decision, once its replay runs ended: promote within the bar when it answered alone at least once,
|
|
632
875
|
* else hold, then one refit (of its guards and recorded escalates). Undefined while a replay run is at work. */
|
|
633
876
|
async function settle(deps, workflow, digest, record) {
|
|
@@ -659,10 +902,7 @@ async function settle(deps, workflow, digest, record) {
|
|
|
659
902
|
// Inputs stopped at an MCP tool its server listed with no readOnlyHint: the hold names those tools (the server may only
|
|
660
903
|
// lack the hint); any other skip (a destructive tool, a built-in write) keeps the recording's words.
|
|
661
904
|
const tools = skippedToolsOf(read.rows);
|
|
662
|
-
const
|
|
663
|
-
const writes = skipped - unmarked.reduce((sum, one) => sum + one.inputs, 0);
|
|
664
|
-
const hint = unmarked.length ? unmarkedWords(unmarked.map((one) => toolWord(one.tool))) : undefined;
|
|
665
|
-
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);
|
|
666
906
|
const aside = [...(failed ? [`${failed} failed: a step failed, no evidence either way`] : []), ...(skipped ? [`${skipped} skipped: ${skipWhy}`] : []),
|
|
667
907
|
...(read.unstarted ? [`${read.unstarted} never started by the replay's deadline`] : [])].join("; ");
|
|
668
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})` : ""}`;
|
|
@@ -847,15 +1087,33 @@ export async function askReplay(ask, name, digest, inputs) {
|
|
|
847
1087
|
const { launch, revisionId } = await ask.launchOf(graded[0].launch.input);
|
|
848
1088
|
if (!launch.learning || launch.learning.notesOnly)
|
|
849
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).`);
|
|
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 } : {}) };
|
|
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 } : {}) };
|
|
851
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`;
|
|
852
1092
|
// 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, 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);
|
|
854
1094
|
if (!reserved.won || !reserved.record?.replay_id)
|
|
855
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.`);
|
|
856
1096
|
ask.log(`workflow-first: ${name} at ${digest}: ${said}`);
|
|
857
1097
|
return { replay: reserved.record.replay_id, inputs: graded.length };
|
|
858
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
|
+
}
|
|
859
1117
|
/** The claim's ports a replay's read settles it through (replayView), with the launch its record chose when it was
|
|
860
1118
|
* reserved (PromotionRecord.launch, the one every replay run of it runs with): its revision, and its learning commit (a
|
|
861
1119
|
* refit lands as a merge from it). Undefined when that launch cannot be read. */
|
|
@@ -882,9 +1140,11 @@ export async function replayView(ask, name, digest, id) {
|
|
|
882
1140
|
const ports = record.decided === "replaying" && text && record.launch ? readPorts(ask, record.launch) : undefined;
|
|
883
1141
|
if (ports && text) {
|
|
884
1142
|
// Read back by its id: whatever settled it (this read, a claim, another read), never another replay's record.
|
|
885
|
-
await settle(ports, text, digest, record);
|
|
1143
|
+
await (record.source === "check" ? settleCheck(ports, text, digest, record) : settle(ports, text, digest, record));
|
|
886
1144
|
record = (await find()) ?? record;
|
|
887
1145
|
}
|
|
1146
|
+
if (record.source === "check")
|
|
1147
|
+
return checkView(ask, record, id, name, digest);
|
|
888
1148
|
// Only a replaying, promote or hold record carries a replay's id.
|
|
889
1149
|
const decided = record.decided === "promote" || record.decided === "hold" ? record.decided : undefined;
|
|
890
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
|
}
|
|
@@ -159,12 +159,6 @@ export declare function servedOf(entries: readonly {
|
|
|
159
159
|
digest?: string;
|
|
160
160
|
promotion?: number;
|
|
161
161
|
} | undefined;
|
|
162
|
-
/** Why a run's learned workflow did not run first, from its root entries: the money rule's WORKFLOW_FIRST entry
|
|
163
|
-
* (outcome needs_revision) with its two numbers; undefined when there is none. */
|
|
164
|
-
export declare function reviseOf(entries: readonly {
|
|
165
|
-
readonly kind: string;
|
|
166
|
-
readonly data?: unknown;
|
|
167
|
-
}[]): Revise | undefined;
|
|
168
162
|
/** The run-record fields a learning run's metrics carry, from the run-end classifier's `classified` path
|
|
169
163
|
* (runPathOverStore): the path it took, the learned revision it ran on, the learned workflow it ran (served first, or
|
|
170
164
|
* called by its agent) and, for one served first, its digest and the promotion it served under (`workflow_promotion`,
|
|
@@ -8,15 +8,17 @@
|
|
|
8
8
|
// and the run's input passes it at the top level (validate.ts inputIssues, the check a run's input gets at start). Whether
|
|
9
9
|
// the workflow can do the job is its own guards' call, never the engine's. Exactly one fitting active workflow serves;
|
|
10
10
|
// none, or more than one (said in one log line), leaves the run to the agent. A launch's `workflowFirst: false` (RunCreate
|
|
11
|
-
// `workflow_first`) forces the agent path. A handback is an escalation, never a failure: it asks
|
|
12
|
-
//
|
|
11
|
+
// `workflow_first`) forces the agent path. A handback is an escalation, never a failure: it asks for no revision (one run
|
|
12
|
+
// is one sample; a revision is asked only of a workflow its promotion holds), and never keeps the workflow from serving.
|
|
13
|
+
// One money rule does (costLineOf): a workflow
|
|
13
14
|
// serves while its all-in cost a run over its last COST_WINDOW served runs at its digest stays under the agent's by-hand
|
|
14
15
|
// cost (its `agent` runs' mean all-in cost; with none, its runs before the workflow's first). Both sides read one all-in
|
|
15
16
|
// cost a run, the cost page's (store-reads/learning.ts allInOf): its booked cost (the workflow and any handback's agent
|
|
16
17
|
// work together) and every learning round booked for it (its look-backs, its grades, a follow-up's rounds). A run whose
|
|
17
18
|
// all-in is unknown anywhere is left out of its side's mean and counted, never read as 0. Over that line it stops running
|
|
18
|
-
// first: the run's journal keeps why with the two numbers (WORKFLOW_FIRST, outcome
|
|
19
|
-
// agent's cost per run shows it
|
|
19
|
+
// first: the agent takes the run, the run's journal keeps why with the two numbers (WORKFLOW_FIRST, outcome
|
|
20
|
+
// needs_revision), and the agent's cost per run shows it stopped. The stop asks for no compile. A new revision of it (a
|
|
21
|
+
// new digest: one its hold asked, or its owner's) serves again from zero. Fewer than
|
|
20
22
|
// COST_WINDOW served runs, or no by-hand cost at all (said in the log): it serves. Every input is the run record's path
|
|
21
23
|
// (metrics.path, one classifier: store-reads/learning.ts runPathOverStore). The rule catches a workflow that costs more
|
|
22
24
|
// than doing the job by hand; it does not catch one that answers wrong with confidence. The spec builders
|
|
@@ -205,7 +207,7 @@ export async function workflowFirstOf(kind, workflows, launch, log, past, promot
|
|
|
205
207
|
return serve;
|
|
206
208
|
const revise = { workflow: chosen.name, digest, served_usd: line.served.mean_usd, by_hand_usd: line.by_hand.mean_usd, runs: line.served.runs, revision: launch.learning.seed, ...(outputSchema !== undefined ? { outputSchema } : {}) };
|
|
207
209
|
const unknown = line.unknown_runs ? ` (${line.unknown_runs} run${line.unknown_runs === 1 ? "" : "s"} of unknown cost left out)` : "";
|
|
208
|
-
log?.(`workflow-first: ${chosen.name} cost $${revise.served_usd} a run over its last ${revise.runs} served runs, at or over $${revise.by_hand_usd} by hand, both all-in${unknown}; it does not run first until a new revision of it
|
|
210
|
+
log?.(`workflow-first: ${chosen.name} cost $${revise.served_usd} a run over its last ${revise.runs} served runs, at or over $${revise.by_hand_usd} by hand, both all-in${unknown}; it does not run first until a new revision of it; the agent takes this run`);
|
|
209
211
|
return { ...champion, revise };
|
|
210
212
|
}
|
|
211
213
|
/** What a run's workflow-first step did, from its root entries (the WORKFLOW_FIRST entry): the workflow, its digest, the
|
|
@@ -217,14 +219,6 @@ export function servedOf(entries) {
|
|
|
217
219
|
return undefined;
|
|
218
220
|
return { outcome: served.outcome, workflow: served.workflow, ...(typeof served.digest === "string" ? { digest: served.digest } : {}), ...(typeof served.promotion === "number" ? { promotion: served.promotion } : {}) };
|
|
219
221
|
}
|
|
220
|
-
/** Why a run's learned workflow did not run first, from its root entries: the money rule's WORKFLOW_FIRST entry
|
|
221
|
-
* (outcome needs_revision) with its two numbers; undefined when there is none. */
|
|
222
|
-
export function reviseOf(entries) {
|
|
223
|
-
const kept = entries.findLast((entry) => entry.kind === WORKFLOW_FIRST && entry.data?.outcome === "needs_revision")?.data;
|
|
224
|
-
if (!kept || typeof kept.workflow !== "string" || typeof kept.digest !== "string" || typeof kept.served_usd !== "number" || typeof kept.by_hand_usd !== "number" || typeof kept.runs !== "number")
|
|
225
|
-
return undefined;
|
|
226
|
-
return { workflow: kept.workflow, digest: kept.digest, served_usd: kept.served_usd, by_hand_usd: kept.by_hand_usd, runs: kept.runs };
|
|
227
|
-
}
|
|
228
222
|
/** The run-record fields a learning run's metrics carry, from the run-end classifier's `classified` path
|
|
229
223
|
* (runPathOverStore): the path it took, the learned revision it ran on, the learned workflow it ran (served first, or
|
|
230
224
|
* called by its agent) and, for one served first, its digest and the promotion it served under (`workflow_promotion`,
|
|
@@ -94,7 +94,7 @@ type StartBody = {
|
|
|
94
94
|
readonly maxCostUsd?: number;
|
|
95
95
|
readonly place?: unknown;
|
|
96
96
|
readonly tools?: readonly unknown[];
|
|
97
|
-
readonly learning?:
|
|
97
|
+
readonly learning?: import("./learning-setting.js").LearningSetting;
|
|
98
98
|
readonly thinking?: unknown;
|
|
99
99
|
};
|
|
100
100
|
/** The pieces a start needs that live in the server's request layer, not here: the leaf validators (the create body's field
|