@parcha/agentrun-serve 0.1.0-beta.57 → 0.1.0-beta.58
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 +52 -0
- package/README.md +64 -33
- package/dist/api-only.d.ts +2 -4
- package/dist/api-only.js +2 -4
- package/dist/box-paths.d.ts +4 -3
- package/dist/box-paths.js +7 -4
- package/dist/claude-login-token.d.ts +117 -0
- package/dist/claude-login-token.js +737 -0
- package/dist/claude-login.d.ts +52 -12
- package/dist/claude-login.js +164 -39
- package/dist/cli.js +7 -41
- package/dist/computer-attach.d.ts +35 -17
- package/dist/computer-attach.js +301 -49
- package/dist/computers.d.ts +10 -10
- package/dist/computers.js +5 -5
- package/dist/console/assets/{backend-DVhHzJX2.js → backend-CSAWUqtZ.js} +1 -1
- package/dist/console/assets/index-DPh29cyz.js +747 -0
- package/dist/console/index.html +1 -1
- package/dist/dial-relay.d.ts +25 -4
- package/dist/dial-relay.js +129 -1
- package/dist/env-switch.d.ts +1 -3
- package/dist/env-switch.js +1 -3
- package/dist/fal-price.d.ts +1 -3
- package/dist/fal-price.js +77 -26
- package/dist/learn/ledger.d.ts +170 -0
- package/dist/learn/ledger.js +218 -0
- package/dist/learn/promote.d.ts +3 -3
- package/dist/learn/promote.js +1 -1
- package/dist/learning-cost.d.ts +22 -20
- package/dist/learning-cost.js +15 -21
- package/dist/live-reach.d.ts +2 -0
- package/dist/live-reach.js +2 -0
- package/dist/mcp-api-only-origins.d.ts +8 -3
- package/dist/mcp-api-only-origins.js +17 -17
- package/dist/micro.d.ts +2 -0
- package/dist/micro.js +2 -0
- package/dist/model.d.ts +5 -4
- package/dist/model.js +7 -6
- package/dist/monid.d.ts +14 -1
- package/dist/monid.js +103 -26
- package/dist/openapi.js +34 -12
- package/dist/place-offers.d.ts +4 -0
- package/dist/place-offers.js +7 -4
- package/dist/provider.d.ts +2 -2
- package/dist/provider.js +9 -5
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-view.d.ts +1 -2
- package/dist/run-view.js +2 -1
- package/dist/runner/adapter-loop.js +3 -16
- package/dist/runner/adapter-run-spec.d.ts +20 -13
- package/dist/runner/adapter-run-spec.js +57 -48
- package/dist/runner/beside.d.ts +9 -2
- package/dist/runner/beside.js +24 -7
- package/dist/runner/claim-secrets.d.ts +17 -15
- package/dist/runner/claim-secrets.js +32 -34
- package/dist/runner/cli-run-spec.js +21 -9
- package/dist/runner/held.d.ts +16 -4
- package/dist/runner/main.d.ts +11 -6
- package/dist/runner/main.js +12 -7
- package/dist/runner/mcp-claim.js +3 -2
- package/dist/runner/mcp-run.d.ts +3 -6
- package/dist/runner/mcp-run.js +3 -4
- package/dist/runner/presence.d.ts +11 -1
- package/dist/runner/presence.js +7 -0
- package/dist/runner/reflect-hook.js +1 -1
- package/dist/runner/run-child-host.d.ts +19 -8
- package/dist/runner/run-child-host.js +80 -34
- package/dist/runner/runner.d.ts +3 -0
- package/dist/runner/runner.js +1 -1
- package/dist/runner/save-slots.js +2 -1
- package/dist/runner/subagent.d.ts +11 -0
- package/dist/runner/subagent.js +17 -3
- package/dist/runner/workflow-first.d.ts +27 -14
- package/dist/runner/workflow-first.js +29 -22
- package/dist/runner/workflow-spec.js +1 -1
- package/dist/server.d.ts +18 -7
- package/dist/server.js +150 -64
- package/dist/slots.d.ts +9 -0
- package/dist/slots.js +45 -0
- package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
- package/dist/store/schema.d.ts +17 -0
- package/dist/store/schema.js +47 -1
- package/dist/store/sqlite/run-patch.d.ts +11 -1
- package/dist/store/sqlite/run-patch.js +29 -1
- package/dist/store/sqlite/store.js +72 -9
- package/dist/store/store.d.ts +14 -0
- package/dist/store-reads/learning.d.ts +81 -124
- package/dist/store-reads/learning.js +207 -286
- package/dist/template-eval.d.ts +19 -0
- package/dist/template-eval.js +92 -43
- package/dist/template-import.d.ts +0 -10
- package/dist/template-import.js +5 -10
- package/dist/timeline/web-calls.d.ts +12 -14
- package/dist/timeline/web-calls.js +14 -30
- package/dist/web-save.d.ts +4 -9
- package/dist/web-save.js +4 -43
- package/dist/workspace/attached-computers.d.ts +32 -9
- package/dist/workspace/attached-computers.js +77 -13
- package/dist/workspace/computer-deps.d.ts +6 -1
- package/dist/workspace/computer-deps.js +1 -1
- package/dist/workspace/computer-for-claim.js +6 -3
- package/dist/workspace/paired-route.d.ts +91 -0
- package/dist/workspace/paired-route.js +153 -0
- package/dist/wrapped.d.ts +8 -2
- package/dist/wrapped.js +21 -6
- package/package.json +12 -12
- package/templates/README.md +3 -3
- package/dist/console/assets/index-okP-y8PK.js +0 -747
|
@@ -10,11 +10,12 @@ import { runLearningBody } from "../learning-routes.js";
|
|
|
10
10
|
import { learningStartOf, learningStartOfStored, readLaunchBody, seedFailed } from "../run-launch.js";
|
|
11
11
|
import { said, seedStopped } from "../learning-words.js";
|
|
12
12
|
import { learningImpact } from "../learning-run.js";
|
|
13
|
-
import {
|
|
13
|
+
import { JOB_PATHS, RUN_PATHS, SERVED_PATHS } from "../learning-cost.js";
|
|
14
|
+
import { answeredOf, figuresOf, record, roundsLooked, roundsSpentIn, shadowCostOf, shadowSpend, spendByRun, summarize } from "../learn/ledger.js";
|
|
14
15
|
import { COMPILE_AFTER_DEFAULT } from "../learning-setting.js";
|
|
15
16
|
import { modelsOwn, runPathOf, workflowNameOf } from "../runner/learning-end.js";
|
|
16
17
|
import { feedbackStatus } from "../runner/feedback-round.js";
|
|
17
|
-
import { costStopOf, PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
|
|
18
|
+
import { costStopOf, NO_PAST_RUNS, PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
|
|
18
19
|
import { reflects } from "../reflect-range.js";
|
|
19
20
|
import { correctionCount } from "../learn/corrections.js";
|
|
20
21
|
import { runEntries, runReflect, runTimeline, withoutFeedbackRounds } from "./journal.js";
|
|
@@ -24,14 +25,7 @@ import { promotedOf, sinceReset } from "../learn/compile.js";
|
|
|
24
25
|
import { isShadowRun, shadowRunId } from "../learn/audit.js";
|
|
25
26
|
import { championOf, checkShares, gradesOf, gradeView, promotionsOf, resetLedgerOf } from "../learn/promote.js";
|
|
26
27
|
const timeOf = (ms) => new Date(ms).toISOString();
|
|
27
|
-
const usdOrNull = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
28
28
|
const textOrNull = (value) => (typeof value === "string" ? value : null);
|
|
29
|
-
/** What a kept row says of a run that looked back (its compile turn's figures): none for one that did not. */
|
|
30
|
-
function answeredOf(row) {
|
|
31
|
-
if (typeof row.answered_at !== "string")
|
|
32
|
-
return {};
|
|
33
|
-
return { answered_at: row.answered_at, job_cost_usd: usdOrNull(row.job_cost_usd), look_back_usd: usdOrNull(row.look_back_usd) };
|
|
34
|
-
}
|
|
35
29
|
/** Agent `agentId`'s runs' records read at once, whatever their number: its runs' launches (Store.runs.learningStarts)
|
|
36
30
|
* and every round its runs' learning kept (Store.learning.kept), two reads beside `listed` (its runs, as runs.list read
|
|
37
31
|
* them). Each answers as the store's per-run read does: a run of `listed` with no launch row is StoreLaunchMissing, one
|
|
@@ -57,7 +51,7 @@ export async function agentRecordsOf(store, agentId, listed) {
|
|
|
57
51
|
return learningStartOfStored(startOf.get(runId));
|
|
58
52
|
},
|
|
59
53
|
// An audit's shadow run is the agent's: one not among `listed` (every run of the agent) was never started, so a
|
|
60
|
-
// run's shadow
|
|
54
|
+
// run's shadow is no read a run.
|
|
61
55
|
get: async (id) => rows.get(id) ?? (isShadowRun(id) ? undefined : store.runs.get(id)),
|
|
62
56
|
},
|
|
63
57
|
learning: {
|
|
@@ -208,59 +202,108 @@ export async function agentLearningOverStore(store, agentId) {
|
|
|
208
202
|
return { counts, impact: learningImpact(rows, (row) => learnings.get(row.id) ?? null) };
|
|
209
203
|
}
|
|
210
204
|
export async function agentCostOverStore(store, agentId, limit = Infinity, refused) {
|
|
211
|
-
const
|
|
205
|
+
const { out, promotions, skip, listed } = await agentLedgerOverStore(store, agentId, refused);
|
|
206
|
+
const { runs } = out;
|
|
207
|
+
// Each learned workflow that ran first, where it stands against the money rule, over the figures this series shows: a
|
|
208
|
+
// workflow over the line shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
|
|
209
|
+
const workflows = workflowCostsOf(pastRunsOf(out, listed, await sinceReset(store, agentId)));
|
|
210
|
+
const summary = { ...out.summary, replay: servingReplayOf(promotions, runs, skip) };
|
|
211
|
+
const bill = { ...out.bill, holds: holdsOf(promotions, runs) };
|
|
212
|
+
return { runs: runs.slice(Math.max(0, runs.length - limit)), summary, total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: out.unknown_path_runs, workflows, bill };
|
|
213
|
+
}
|
|
214
|
+
/** The agent's ledger read over the one store and summed (learn/ledger.ts summarize): its runs (as runs.list reads
|
|
215
|
+
* them), each one's ledger row and spend rows (Learning.ledger), its promotion records (the revisions' K and state,
|
|
216
|
+
* revisionsOf, skipping what `refused` names) and its audits' shadow runs. `records`: where a run's launch and rounds
|
|
217
|
+
* are read (the agent's at once by default, agentRecordsOf). */
|
|
218
|
+
export async function agentLedgerOverStore(store, agentId, refused, records) {
|
|
212
219
|
const listed = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId);
|
|
213
|
-
|
|
214
|
-
const records = await agentRecordsOf(store, agentId, listed);
|
|
215
|
-
// An audit's shadow run is no run of the series: its cost is its served run's learning (allInOf).
|
|
216
|
-
const ended = listed.filter((run) => FINAL_STATES.includes(run.state) && !run.importedFrom && run.archivedAt === null && !isShadowRun(run.id)).sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
|
|
217
|
-
const rows = [];
|
|
218
|
-
let unknownPath = 0;
|
|
219
|
-
// The hand-backs a restart cut (metrics.workflow_cut: pathMetrics), counted apart from the workflow's own (costSummary).
|
|
220
|
-
const cut = new Set();
|
|
221
|
-
// One numbering, the bill's: the runs that do a job, 1 the first; a replay checks a revision and has none.
|
|
222
|
-
let jobs = 0;
|
|
223
|
-
for (const run of ended) {
|
|
224
|
-
if (!kinds.has(run.revisionId))
|
|
225
|
-
kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind);
|
|
226
|
-
if (kinds.get(run.revisionId) === "workflow")
|
|
227
|
-
continue;
|
|
228
|
-
// A run that learns whose record names no path is left out and counted, never `agent` at $0.
|
|
229
|
-
const placed = pathOfRow(run.metrics, await learningStartOfRun(records, run.id));
|
|
230
|
-
if (!placed) {
|
|
231
|
-
unknownPath += 1;
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
if (placed.path === "handed_back" && run.metrics?.workflow_cut === true)
|
|
235
|
-
cut.add(run.id);
|
|
236
|
-
rows.push(await costRunOf(records, run, JOB_PATHS.includes(placed.path) ? (jobs += 1) : null, placed));
|
|
237
|
-
}
|
|
238
|
-
// Where each revision took over, over every run before the limit cuts the read (the first is the summary's takeover).
|
|
239
|
-
const runs = tookOverOf(rows);
|
|
240
|
-
// Each learned workflow that ran first, where it stands against the money rule (workflow-first.ts workflowCostsOf), over
|
|
241
|
-
// the runs the run's launch reads (pastRunsOverStore) at the all-in it reads (moneyAllInOf): a workflow over the line
|
|
242
|
-
// shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
|
|
243
|
-
const workflows = workflowCostsOf(await pastRunsOverStore(store, agentId, new Map(runs.map((run) => [run.run_id, run.total_usd])), records));
|
|
220
|
+
const read = await ledgerReadOf(store, agentId, listed, records ?? (await agentRecordsOf(store, agentId, listed)));
|
|
244
221
|
const promotions = await promotionsOf(store.settings, agentId);
|
|
245
222
|
const skip = refused ? await refused(agentId, promotions) : undefined;
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
223
|
+
const revisions = revisionsOf(promotions, await resetLedgerOf(store.settings, agentId), skip);
|
|
224
|
+
return { out: summarize({ ...read, revisions }), promotions, skip, listed };
|
|
225
|
+
}
|
|
226
|
+
/** How many runs a ledger read reads at once (ledgerReadOf: a run's rounds, its launch, its revision). */
|
|
227
|
+
const READS_AT_ONCE = 16;
|
|
228
|
+
/** `each` of `items`, READS_AT_ONCE at a time, in their order. */
|
|
229
|
+
async function inBatches(items, each) {
|
|
230
|
+
const out = [];
|
|
231
|
+
for (let at = 0; at < items.length; at += READS_AT_ONCE)
|
|
232
|
+
out.push(...(await Promise.all(items.slice(at, at + READS_AT_ONCE).map(each))));
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
/** `keys` paired with `values`, by place. */
|
|
236
|
+
const zipWith = (keys, values) => keys.map((key, at) => [key, values[at]]);
|
|
237
|
+
/** What summarize reads of agent `agentId` (LedgerRead, its revisions aside), over `listed` (its runs, as runs.list read
|
|
238
|
+
* them): the ledger rows of its ended runs that do a job or check one (neither imported nor archived, no workflow
|
|
239
|
+
* revision's run, no shadow), a run that does not learn on the `agent` path, each with the workflow name its launch
|
|
240
|
+
* offered; every spend row; each run's audit shadow (open or ended); and the runs on no named disk (learn/learning-of.ts:
|
|
241
|
+
* one on a named disk learns nothing and serves nothing). A run that ended with no row (it ended before the ledger, or
|
|
242
|
+
* under a runner of an earlier build), or whose row no longer stands for it (ledgerRowStands), is recorded here from its
|
|
243
|
+
* row and its rounds (learn/ledger.ts record), as its end would have; its next end writes it. `runIds`: the runs whose
|
|
244
|
+
* ledger is read (Learning.ledger), the agent's every run when absent. `named` false: no workflow name is read for the
|
|
245
|
+
* series' rows (a launch's learning start is read only for a row whose end named no path). */
|
|
246
|
+
async function ledgerReadOf(store, agentId, listed, records, { runIds, named = true } = {}) {
|
|
247
|
+
const ledger = await store.learning.ledger(agentId, runIds);
|
|
248
|
+
const byId = new Map(listed.map((run) => [run.id, run]));
|
|
249
|
+
// A row that no longer stands for its run (ledgerRowStands: an earlier build ended it again or healed its cost) is
|
|
250
|
+
// recorded again below, with its spend rows, as one with no row is.
|
|
251
|
+
const stale = new Set(ledger.rows.flatMap((row) => (byId.has(row.runId) && !ledgerRowStands(row, byId.get(row.runId)) ? [row.runId] : [])));
|
|
252
|
+
const recorded = new Map(ledger.rows.flatMap((row) => (stale.has(row.runId) ? [] : [[row.runId, row]])));
|
|
253
|
+
const spend = ledger.spend.filter((one) => !stale.has(one.runId));
|
|
254
|
+
// The rest are read a few at a time (READS_AT_ONCE), each run's reads in one batch: a claim reads up to PAST_RUNS runs.
|
|
255
|
+
const unrecorded = listed.filter((run) => !run.importedFrom && FINAL_STATES.includes(run.state) && !recorded.has(run.id));
|
|
256
|
+
const made = await inBatches(unrecorded, async (run) => record(await runEndOf(records, run)));
|
|
257
|
+
unrecorded.forEach((run, at) => {
|
|
258
|
+
recorded.set(run.id, made[at].row);
|
|
259
|
+
spend.push(...made[at].spend);
|
|
260
|
+
});
|
|
261
|
+
const ended = listed.filter((run) => FINAL_STATES.includes(run.state) && !run.importedFrom && run.archivedAt === null && !isShadowRun(run.id));
|
|
262
|
+
const revisionIds = [...new Set(ended.map((run) => run.revisionId))];
|
|
263
|
+
const kinds = new Map(zipWith(revisionIds, await inBatches(revisionIds, async (id) => (await store.agents.revision(id))?.kind)));
|
|
264
|
+
const jobs = ended.filter((run) => kinds.get(run.revisionId) !== "workflow");
|
|
265
|
+
// Its launch's learning start: the workflow names it offered, and, for a row whose end named no path, whether it learns.
|
|
266
|
+
const starts = await inBatches(jobs, async (run) => (named || recorded.get(run.id).path === null ? learningStartOfRun(records, run.id) : undefined));
|
|
267
|
+
const offered = new Map();
|
|
268
|
+
const rows = [];
|
|
269
|
+
const shadows = new Map();
|
|
270
|
+
const learning = new Set();
|
|
271
|
+
const audited = new Set(spend.flatMap((one) => (one.kind === "audit" && one.ofRun !== null ? [one.ofRun] : [])));
|
|
272
|
+
for (const [run, started] of zipWith(jobs, starts)) {
|
|
273
|
+
const row = recorded.get(run.id);
|
|
274
|
+
// A run that does not learn names no path: it is the agent's. One that learns and names none is unknown.
|
|
275
|
+
rows.push(row.path === null && !started ? { ...row, path: "agent" } : row);
|
|
276
|
+
if (named)
|
|
277
|
+
offered.set(run.id, started?.workflows);
|
|
278
|
+
if (run.disk === null)
|
|
279
|
+
learning.add(run.id);
|
|
280
|
+
const shadow = byId.get(shadowRunId(run.id));
|
|
281
|
+
if (!shadow)
|
|
282
|
+
continue;
|
|
283
|
+
const over = FINAL_STATES.includes(shadow.state);
|
|
284
|
+
shadows.set(run.id, over ? "ended" : "open");
|
|
285
|
+
// A shadow that ended with no audit row (its end named no audit: it failed before its end wrote its metrics) is the
|
|
286
|
+
// run's by its id.
|
|
287
|
+
if (over && !audited.has(run.id))
|
|
288
|
+
spend.push(shadowSpend(shadow, run.id));
|
|
260
289
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
290
|
+
// Only a name the launch offered is shown.
|
|
291
|
+
return { rows, spend, shadows, learning, compileAfter: COMPILE_AFTER_DEFAULT, ...(named ? { named: (row) => offeredName(row.workflow, offered.get(row.runId)) } : {}) };
|
|
292
|
+
}
|
|
293
|
+
/** Whether run `run`'s ledger row stands for it as it is now: written for the end the run has (`epoch`, the run's epoch
|
|
294
|
+
* after the end that wrote it: run-patch.ts recordLedger), with no cost healed since. A runner or server of the build
|
|
295
|
+
* before the ledger writes none: its end of a run again (a follow-up's, or a grade's round that keeps the end it had, at
|
|
296
|
+
* the same ended_at) moves the run's epoch, as every end and every claim does, and its heal of a cost (Runs.healCost:
|
|
297
|
+
* only over an unknown cost, at the same epoch and end) makes the cost known. Either leaves the row for the run as it
|
|
298
|
+
* was, and its reader records it again from the run's row (record), as for a run with no row. A cancel that wins an
|
|
299
|
+
* end moves its ended_at alone (run-patch.ts refuseLeftovers): the row stands. */
|
|
300
|
+
export function ledgerRowStands(row, run) {
|
|
301
|
+
return row.epoch === run.epoch && (row.cost !== "unknown" || (run.costUsd === null && run.costAtMostUsd === null));
|
|
302
|
+
}
|
|
303
|
+
/** What record() reads of an ended run: its row and every round its learning kept. */
|
|
304
|
+
async function runEndOf(records, run) {
|
|
305
|
+
const rounds = (await roundsOf(records, run.id)).map((one) => ({ round: one.round, proposal: one.proposal }));
|
|
306
|
+
return { id: run.id, agentId: run.agentId, startedAt: run.startedAt, costUsd: run.costUsd, costAtMostUsd: run.costAtMostUsd, metrics: run.metrics, rounds };
|
|
264
307
|
}
|
|
265
308
|
/** Each hold of the agent's learned workflows (its promotion records that say hold), placed on the series: at the first
|
|
266
309
|
* run that does a job from it on, by its number among them (the points' `n`), null when none has run since. */
|
|
@@ -271,127 +314,92 @@ function holdsOf(records, runs) {
|
|
|
271
314
|
return { n: at < 0 ? null : at + 1, at: one.at, workflow: one.workflow, by: one.audit ? "audit" : one.check ? "check" : "dsl", reason: one.reason };
|
|
272
315
|
});
|
|
273
316
|
}
|
|
274
|
-
const metricOf = (row) => (row.metrics ?? {});
|
|
275
317
|
/** The revision a run ran (metrics.workflow and metrics.workflow_digest, workflow-first.ts pathMetrics), undefined when it
|
|
276
318
|
* names none: two workflows of the same text share a digest, so a revision is never its digest alone. */
|
|
277
319
|
const revisionOfRow = (row) => {
|
|
278
|
-
const { workflow, workflow_digest: digest } =
|
|
320
|
+
const { workflow, workflow_digest: digest } = (row.metrics ?? {});
|
|
279
321
|
return typeof workflow === "string" && typeof digest === "string" ? { workflow, digest } : undefined;
|
|
280
322
|
};
|
|
281
|
-
/**
|
|
282
|
-
*
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// after an earlier claim asked it: runner/reflect-hook.ts `unasked`) spent with no revision to serve: its spend is
|
|
290
|
-
// unattributed, never its run's.
|
|
291
|
-
if (!compiled)
|
|
292
|
-
return usd === 0 ? [] : [{ workflow: null, digest: null, usd }];
|
|
293
|
-
return [{ workflow: compiled.workflow, digest: compiled.digest && !draft_errors?.length ? compiled.digest : null, usd }];
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
/** A run's v2 learning (A8): what learning cost for it as the cost series counts it (its compile turns, any follow-up
|
|
297
|
-
* round's turns and its audit shadow: `run.learning_usd`) less its compile turns (each its revision's), with its share of
|
|
298
|
-
* its revision's compile and check (`shares`: a missing entry is none, an unknown share stays unknown). Null while any
|
|
299
|
-
* part is unknown. */
|
|
300
|
-
function learningV2Of(run, compiles, shares) {
|
|
301
|
-
const compiled = compiles.reduce((sum, one) => (sum === null || one.usd === null ? null : sum + one.usd), 0);
|
|
302
|
-
const share = shares.has(run.run_id) ? shares.get(run.run_id) : 0;
|
|
303
|
-
if (run.learning_usd === null || compiled === null || share === null)
|
|
304
|
-
return null;
|
|
305
|
-
return noiseOff(Math.max(0, run.learning_usd - compiled) + share);
|
|
306
|
-
}
|
|
307
|
-
/** A run's v2 all-in: its own $ (the job's) and its v2 learning (learningV2Of); null when either is. */
|
|
308
|
-
function allInV2Of(run, compiles, shares) {
|
|
309
|
-
const learning = learningV2Of(run, compiles, shares);
|
|
310
|
-
return learning === null || run.cost_usd === null ? null : noiseOff(run.cost_usd + learning);
|
|
311
|
-
}
|
|
312
|
-
/** The agent's revision ledger (revisionLedgerOf) over `listed` (its runs, as runs.list read them): its ended runs that do
|
|
313
|
-
* a job or check one (no shadow, no imported or archived run), and the compile turns their rounds asked, by run. */
|
|
314
|
-
async function agentLedgerOf(records, promotions, listed, reset, refused) {
|
|
315
|
-
const rows = listed.filter((one) => FINAL_STATES.includes(one.state) && !one.importedFrom && one.archivedAt === null && !isShadowRun(one.id));
|
|
316
|
-
const compiles = new Map();
|
|
317
|
-
for (const row of rows) {
|
|
318
|
-
const asked = await compilesOf(records, row.id);
|
|
319
|
-
if (asked.length)
|
|
320
|
-
compiles.set(row.id, asked);
|
|
321
|
-
}
|
|
322
|
-
return { ...revisionLedgerOf(promotions, rows, [...compiles.values()].flat(), reset, refused), compiles };
|
|
323
|
-
}
|
|
324
|
-
/** Each revision of the agent's learned workflows (one per workflow and digest its promotion records name, in the order
|
|
325
|
-
* they were first recorded) with its learning spend, and what its served runs carry of it (amortize), over `rows`: the
|
|
326
|
-
* agent's ended runs that do a job or check one (no shadow, no imported or archived run). A revision is its workflow and
|
|
327
|
-
* its digest, never the digest alone: two workflows of the same text share a digest, and each pays its own way. Its
|
|
328
|
-
* spend is the compile turns that drafted it (`compiles` of its workflow at its digest: a recompile drafts a revision of
|
|
329
|
-
* its own) and its check (the replay runs that ran it: metrics.path `replay` of its workflow at its digest); unknown
|
|
330
|
-
* once a part's cost is (null, or only a bound). A compile whose drafts all failed drafted none: no revision carries it
|
|
331
|
-
* (the bill's unattributed does). It is spread over its first K served runs (`workflow` or `handed_back` of its workflow
|
|
332
|
-
* at its digest), K its check's compile_after; `state` from its newest record. A revision an earlier reset of the agent's
|
|
333
|
-
* learning deleted (`reset`, learn/promote.ts keepResetLedger) is one too, first, stopped, at the K it was kept with,
|
|
334
|
-
* unless the records name it again. Serving is the champion's (championOf), skipping what `refused` names (the purge
|
|
335
|
-
* gate's), so a purged run's revision is never said to serve. */
|
|
336
|
-
export function revisionLedgerOf(records, rows, compiles = [], reset = [], refused) {
|
|
337
|
-
const ordered = [...rows].sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
|
|
323
|
+
/** Each revision of the agent's learned workflows its promotion records name (one per workflow and digest, in the order
|
|
324
|
+
* they were first recorded), with the served runs its spend is spread over (K, its check's compile_after) and `state`
|
|
325
|
+
* from its newest record: `checking` while its check replays, `serving` for the champion (championOf, skipping what
|
|
326
|
+
* `refused` names: the purge gate's, so a purged run's revision is never said to serve), else `stopped`. A revision an
|
|
327
|
+
* earlier reset of the agent's learning deleted (`reset`, learn/promote.ts keepResetLedger) comes first, stopped, at the
|
|
328
|
+
* K it was kept with, unless the records name it again. What each one spent is the ledger's (learn/ledger.ts summarize),
|
|
329
|
+
* whatever the records say. */
|
|
330
|
+
export function revisionsOf(records, reset = [], refused) {
|
|
338
331
|
const newest = new Map();
|
|
339
|
-
for (const
|
|
340
|
-
newest.set(`${
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
return { workflow, digest, compile_usd: compile, check_usd: check, spend_usd: sum([compile, check]), k, state };
|
|
348
|
-
};
|
|
349
|
-
const revisions = [
|
|
350
|
-
...reset.filter((one) => !newest.has(`${one.workflow}\0${one.digest}`)).map((one) => spendOf(one.workflow, one.digest, one.k ?? COMPILE_AFTER_DEFAULT, "stopped")),
|
|
351
|
-
...[...newest.values()].map((record) => spendOf(record.workflow, record.digest, record.check?.compile_after ?? COMPILE_AFTER_DEFAULT, record.decided === "replaying" ? "checking" : championOf(records, record.workflow, refused)?.digest === record.digest ? "serving" : "stopped")),
|
|
332
|
+
for (const one of records)
|
|
333
|
+
newest.set(`${one.workflow}\0${one.digest}`, one);
|
|
334
|
+
return [
|
|
335
|
+
...reset.filter((one) => !newest.has(`${one.workflow}\0${one.digest}`)).map((one) => ({ workflow: one.workflow, digest: one.digest, k: one.k ?? COMPILE_AFTER_DEFAULT, state: "stopped" })),
|
|
336
|
+
...[...newest.values()].map((one) => ({
|
|
337
|
+
workflow: one.workflow, digest: one.digest, k: one.check?.compile_after ?? COMPILE_AFTER_DEFAULT,
|
|
338
|
+
state: one.decided === "replaying" ? "checking" : championOf(records, one.workflow, refused)?.digest === one.digest ? "serving" : "stopped",
|
|
339
|
+
})),
|
|
352
340
|
];
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
* and the bill's `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null;
|
|
367
|
-
* null while unknown (its shadow still at work, a compile's or a check's cost unknown), never $0. Undefined for an
|
|
368
|
-
* unknown run. Its revision's champion skips what `refused` names of the agent's promotion records (agentCostOverStore). */
|
|
341
|
+
}
|
|
342
|
+
/** Run `runId`'s path and its costs as GET /runs/{id} shows them (A8), the figures GET .../learning/cost reads for it
|
|
343
|
+
* (learn/ledger.ts figuresOf over its ledger row: one formula): `path` its record's (metrics.path, RUN_PATHS: `agent`
|
|
344
|
+
* for a run that does not learn; null while it has not ended, for a workflow revision's run, and for a run that learns
|
|
345
|
+
* whose end named none); `usd` its own spend, the job's (its look-back left out); `learning_usd` what learning spent for
|
|
346
|
+
* it: its follow-up rounds' turns, its audit shadow's cost, and its share of its revision's compile and check (its own
|
|
347
|
+
* compile turns are its revision's: `compile_usd`); `all_in_usd` the two; and `learning_usd`'s parts. A replay or a
|
|
348
|
+
* shadow run carries none: its spend is on the runs it serves, so its `learning_usd` and `all_in_usd` are 0, and the sum
|
|
349
|
+
* of every run's `all_in_usd` and the bill's `unattributed_usd` is the booked total. `rowUsd`: the cost its view reads
|
|
350
|
+
* (its row's when absent); one other than its row's (an imported run's estimate, a booking where its end stored none)
|
|
351
|
+
* is its job's, less its look-back. While the run is at work `usd` is `rowUsd` and the rest null; null while unknown (its shadow still at work, a compile's or
|
|
352
|
+
* a check's cost unknown), never $0. Undefined for an unknown run. A served run's share is the agent's ledger's
|
|
353
|
+
* (agentLedgerOverStore), its revision's champion skipping what `refused` names of the agent's promotion records. */
|
|
369
354
|
export async function runAllInOverStore(store, runId, rowUsd, refused) {
|
|
370
355
|
const run = await store.runs.get(runId);
|
|
371
356
|
if (!run)
|
|
372
357
|
return undefined;
|
|
373
|
-
const row = rowUsd === undefined ? (typeof run.costAtMostUsd === "number" ? null : run.costUsd) : rowUsd;
|
|
374
358
|
const ended = FINAL_STATES.includes(run.state);
|
|
375
359
|
const kind = (await store.agents.revision(run.revisionId))?.kind;
|
|
376
360
|
const started = kind === "workflow" ? undefined : await learningStartOfRun(store, run.id).catch(() => undefined);
|
|
377
361
|
const path = !ended || kind === "workflow" ? null : (pathOfRow(run.metrics, started)?.path ?? null);
|
|
378
|
-
if (!ended)
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
362
|
+
if (!ended) {
|
|
363
|
+
const row = rowUsd === undefined ? (typeof run.costAtMostUsd === "number" ? null : run.costUsd) : rowUsd;
|
|
364
|
+
return { path, usd: row, learning_usd: null, all_in_usd: null, compile_usd: null, followup_usd: null, shadow_usd: null, share_usd: null };
|
|
365
|
+
}
|
|
366
|
+
// A served run carries a share of its revision's spend: its figures are the series' own, over the agent's ledger.
|
|
367
|
+
const served = path !== null && SERVED_PATHS.includes(path) && revisionOfRow(run) !== undefined;
|
|
368
|
+
const figures = (served ? (await agentLedgerOverStore(store, run.agentId, refused)).out.figures.get(run.id) : undefined) ?? (await runFiguresOf(store, run));
|
|
369
|
+
// The run's view priced it other than its row does (`rowUsd`: an imported run's catalog estimate, or its booking where
|
|
370
|
+
// its end stored none): its job is that figure, as its end would have recorded it at that cost (record: less its
|
|
371
|
+
// look-back), its learning's parts the ledger's.
|
|
372
|
+
const { usd, learning_usd, all_in_usd, compile_usd, followup_usd, shadow_usd, share_usd } = typeof rowUsd === "number" && rowUsd !== run.costUsd ? await pricedAt(store, run, rowUsd, figures) : figures;
|
|
373
|
+
return { path, usd, learning_usd, all_in_usd, compile_usd, followup_usd, shadow_usd, share_usd };
|
|
374
|
+
}
|
|
375
|
+
/** Run `run`'s figures with its row's cost read as `usd` (runAllInOverStore's `rowUsd`): its row as its end would have
|
|
376
|
+
* recorded it at that cost, with `figures`' compile, shadow and share. */
|
|
377
|
+
async function pricedAt(store, run, usd, figures) {
|
|
378
|
+
const row = record(await runEndOf(store, { ...run, costUsd: usd, costAtMostUsd: null })).row;
|
|
379
|
+
return figuresOf(isShadowRun(run.id) ? { ...row, path: "audit" } : row, { compile: figures.compile_usd, shadow: figures.shadow_usd, share: figures.share_usd });
|
|
380
|
+
}
|
|
381
|
+
/** One run's figures (learn/ledger.ts figuresOf) from its own ledger row and spend, with no share of its revision's: a
|
|
382
|
+
* run that carries none (not served, or out of the series: archived). Its row, or what its end would have recorded
|
|
383
|
+
* (record) for one that has none or whose row no longer stands for it (ledgerRowStands); its audit's shadow read as
|
|
384
|
+
* summarize reads it (shadowCostOf). */
|
|
385
|
+
async function runFiguresOf(store, run) {
|
|
386
|
+
const ledger = await store.learning.ledger(run.agentId, [run.id]);
|
|
387
|
+
const mine = ledger.rows.find((row) => row.runId === run.id);
|
|
388
|
+
const own = mine && ledgerRowStands(mine, run) ? { row: mine, spend: ledger.spend.filter((one) => one.runId === run.id) } : record(await runEndOf(store, run));
|
|
389
|
+
const spend = [...own.spend, ...ledger.spend.filter((one) => one.kind === "audit" && one.ofRun === run.id)];
|
|
390
|
+
const shadows = new Map();
|
|
391
|
+
const shadow = isShadowRun(run.id) ? undefined : await store.runs.get(shadowRunId(run.id));
|
|
392
|
+
if (shadow) {
|
|
393
|
+
const ended = FINAL_STATES.includes(shadow.state);
|
|
394
|
+
shadows.set(run.id, ended ? "ended" : "open");
|
|
395
|
+
// A shadow that ended with no audit row (its end named no audit, or it has no ledger row yet) is the run's by its id.
|
|
396
|
+
if (ended && !spend.some((one) => one.kind === "audit"))
|
|
397
|
+
spend.push(shadowSpend(shadow, run.id));
|
|
391
398
|
}
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
399
|
+
const { compile, audit } = spendByRun(spend);
|
|
400
|
+
// A shadow run is an audit's whatever its end named: its spend is on the run it audits.
|
|
401
|
+
const row = isShadowRun(run.id) ? { ...own.row, path: "audit" } : own.row;
|
|
402
|
+
return figuresOf(row, { compile: compile.has(run.id) ? compile.get(run.id) : 0, shadow: shadowCostOf(run.id, audit, shadows), share: 0 });
|
|
395
403
|
}
|
|
396
404
|
/** The check the serving promotion was decided on, as the cost summary says it: the champion (championOf) of the learned
|
|
397
405
|
* workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
|
|
@@ -406,18 +414,6 @@ export function servingReplayOf(records, runs, refused) {
|
|
|
406
414
|
const agreed = check.agreed ?? 0, alone = check.alone ?? 0;
|
|
407
415
|
return { n: check.n, workflow_pct: Math.round(checkShares({ n: check.n, agreed, alone }).agreement * 1000) / 10, agent_pct: 100, cleared: true, agreed, alone, agree_min: check.agree_min };
|
|
408
416
|
}
|
|
409
|
-
/** What run `runId`'s learning spent, as its cost per run counts it, over every round a follow-up opened (roundsOf): what
|
|
410
|
-
* it kept as it answered (answeredOf). A run a follow-up opened again (roundsSpendOf's figures): its job is what it had
|
|
411
|
-
* cost when its first round answered, and everything after (each later round's follow-up turn and every round's
|
|
412
|
-
* look-back: feedback the run was given is learning, never the job) is its look-back here, `rowUsd` (the run's row cost)
|
|
413
|
-
* less that job (roundsLooked). */
|
|
414
|
-
export async function learningSpendOf(store, runId, rowUsd) {
|
|
415
|
-
const rounds = await roundsOf(store, runId);
|
|
416
|
-
if (!rounds.length)
|
|
417
|
-
return null;
|
|
418
|
-
const more = roundsSpendIn(rounds, roundsSpentOf(rounds));
|
|
419
|
-
return more ? roundsLooked(more, rowUsd) : answeredOf((rounds.at(-1).proposal ?? {}));
|
|
420
|
-
}
|
|
421
417
|
/** Every round run `runId` kept (one a follow-up opened again is a round past its first), oldest first: none when it kept
|
|
422
418
|
* no learning. The store's proposal(runId) is the newest round alone; a read of what the run's learning did or spent
|
|
423
419
|
* reads them all. */
|
|
@@ -434,104 +430,40 @@ export async function roundsOf(store, runId) {
|
|
|
434
430
|
out.push(newest);
|
|
435
431
|
return out;
|
|
436
432
|
}
|
|
437
|
-
/**
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
export async function
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
if (!shadow)
|
|
467
|
-
return { open: false, usd: 0 };
|
|
468
|
-
return FINAL_STATES.includes(shadow.state) ? { open: false, usd: shadow.costUsd ?? null } : { open: true, usd: null };
|
|
469
|
-
}
|
|
470
|
-
/** One run of the cost series, as number `n` of it (null for a replay), `placed` its path and workflow as pathOfRow reads
|
|
471
|
-
* them off its record (agentCostOverStore has them): its figures (allInOf), the workflow
|
|
472
|
-
* revision that ran it and whether an audit's shadow ran for it (agentCostOverStore's loop body); `took_over` is the
|
|
473
|
-
* series' (tookOverOf). */
|
|
474
|
-
export async function costRunOf(store, run, n, { path, workflow }) {
|
|
475
|
-
const { cost_usd, learning_usd, total_usd } = await allInOf(store, run);
|
|
476
|
-
// The run row's stored bound (RunRow.costAtMostUsd, written with a null cost_usd): its cost counts a request cut at what its cap held.
|
|
477
|
-
const cost_upper_bound = typeof run.costAtMostUsd === "number";
|
|
478
|
-
// The revision its metrics name (pathMetrics): the bill's revisions are read the same way, offered or not.
|
|
479
|
-
const digest = path === "agent" ? null : revisionOfRow(run)?.digest ?? null;
|
|
480
|
-
const audited = (await store.runs.get(shadowRunId(run.id))) !== undefined;
|
|
481
|
-
return { n, run_id: run.id, started_at: new Date(run.startedAt).toISOString(), path, workflow, digest, audited, cost_usd, cost_upper_bound, learning_usd, total_usd };
|
|
482
|
-
}
|
|
483
|
-
/** The runs the money rule reads for agent `agentId` (runner/workflow-first.ts costLineOf), one set for the run's launch
|
|
484
|
-
* and the cost page: of its newest PAST_RUNS runs on no named disk (learn/learning-of.ts: one on a named disk learns
|
|
485
|
-
* nothing), each that ended (ended, stopped or failed) since its learning's reset (learn/compile.ts sinceReset: a
|
|
486
|
-
* workflow before it is not the one it learned after, whatever its name), is neither imported nor archived nor a
|
|
487
|
-
* workflow revision's run, with its all-in cost as the money rule reads it (moneyAllInOf). `known`: all-in costs already
|
|
488
|
-
* read (allInOf), by run id (the cost page's series), read again for no served run; `records`, where any other's rows
|
|
489
|
-
* are read (the store's by default). */
|
|
490
|
-
export async function pastRunsOverStore(store, agentId, known, records = store) {
|
|
491
|
-
const kinds = new Map();
|
|
492
|
-
const kept = [];
|
|
493
|
-
const since = await sinceReset(store, agentId);
|
|
494
|
-
const page = (await store.runs.list({ agentId, disk: null, limit: PAST_RUNS })).filter(since);
|
|
495
|
-
// Each run's audit shadow is read off the page (allInOf's shadowSpendOf): a shadow starts after the run it audits ends,
|
|
496
|
-
// so it is newer than that run and on the same page of the newest runs; one not on it was never started.
|
|
497
|
-
const shadows = new Map(page.filter((run) => isShadowRun(run.id)).map((run) => [run.id, run]));
|
|
498
|
-
const learningStart = records.runs.learningStart;
|
|
499
|
-
const paged = {
|
|
500
|
-
learning: records.learning,
|
|
501
|
-
runs: {
|
|
502
|
-
launch: (id) => records.runs.launch(id),
|
|
503
|
-
get: (id) => (isShadowRun(id) ? Promise.resolve(shadows.get(id)) : records.runs.get(id)),
|
|
504
|
-
...(learningStart ? { learningStart: (id) => learningStart.call(records.runs, id) } : {}),
|
|
505
|
-
},
|
|
506
|
-
};
|
|
507
|
-
for (const run of page) {
|
|
508
|
-
if (run.agentId !== agentId || !FINAL_STATES.includes(run.state) || run.importedFrom || run.archivedAt !== null || isShadowRun(run.id))
|
|
509
|
-
continue;
|
|
510
|
-
if (!kinds.has(run.revisionId))
|
|
511
|
-
kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind);
|
|
512
|
-
if (kinds.get(run.revisionId) !== "workflow")
|
|
513
|
-
kept.push(run);
|
|
514
|
-
}
|
|
515
|
-
// A few reads a run, a batch at a time: a launch reads up to PAST_RUNS of them.
|
|
516
|
-
const past = [];
|
|
517
|
-
for (let at = 0; at < kept.length; at += 16) {
|
|
518
|
-
past.push(...(await Promise.all(kept.slice(at, at + 16).map(async (run) => ({ metrics: run.metrics, startedAt: run.startedAt, allInUsd: await moneyAllInOf(paged, run, known) })))));
|
|
519
|
-
}
|
|
520
|
-
return past;
|
|
521
|
-
}
|
|
522
|
-
/** A run's all-in as the money rule reads it (runner/workflow-first.ts PastRun): a run a learned workflow served
|
|
523
|
-
* (SERVED_PATHS) at its all-in (allInOf, or `known`'s); any other as the run view reads it (runAllInOverStore's
|
|
524
|
-
* allInV2Of), its compile turns its revision's, never the agent's by-hand cost, and no revision's share (only a served
|
|
525
|
-
* run carries one). Its recurring learning (a follow-up's rounds, its audit shadow) stays on it. */
|
|
526
|
-
async function moneyAllInOf(records, run, known) {
|
|
527
|
-
if (SERVED_PATHS.includes(metricOf(run).path))
|
|
528
|
-
return known?.has(run.id) ? known.get(run.id) : (await allInOf(records, run)).total_usd;
|
|
529
|
-
return allInV2Of({ run_id: run.id, ...(await allInOf(records, run)) }, await compilesOf(records, run.id), new Map());
|
|
433
|
+
/** What the money rule reads of the agent (runner/workflow-first.ts PastRuns) off its summed ledger (`out`, a
|
|
434
|
+
* summarize over `listed`, its runs as runs.list read them, newest first): of its newest PAST_RUNS runs on no named disk
|
|
435
|
+
* (learn/learning-of.ts: one on a named disk learns nothing), those since its learning's reset (`since`,
|
|
436
|
+
* learn/compile.ts sinceReset: a workflow before it is not the one it learned after, whatever its name), each series
|
|
437
|
+
* run among them with its all-in cost as the series reads it (RunFigures.series_total_usd), and the by-hand side over
|
|
438
|
+
* those same runs: the mean job's own $ (RunFigures.usd: no compile turn, follow-up or shadow) of the ones on the
|
|
439
|
+
* `agent` path, with the ones its mean left out (cost unknown or only a bound). The served side and the by-hand side
|
|
440
|
+
* read one set of runs: the cost page and a run's launch read the same window (pastRunsOverStore). */
|
|
441
|
+
export function pastRunsOf(out, listed, since) {
|
|
442
|
+
const scope = listed.filter((run) => run.disk === null).slice(0, PAST_RUNS).filter(since);
|
|
443
|
+
const runs = scope.flatMap((run) => {
|
|
444
|
+
const figures = out.figures.get(run.id);
|
|
445
|
+
return figures ? [{ metrics: run.metrics, startedAt: run.startedAt, allInUsd: figures.series_total_usd }] : [];
|
|
446
|
+
});
|
|
447
|
+
const ids = new Set(scope.map((run) => run.id));
|
|
448
|
+
const byHand = out.runs.filter((run) => ids.has(run.run_id) && run.path === "agent");
|
|
449
|
+
const known = byHand.flatMap((run) => (run.cost_usd !== null && !run.cost_upper_bound ? [run.cost_usd] : []));
|
|
450
|
+
const by_hand = known.length ? { runs: known.length, mean_usd: Math.round((known.reduce((a, b) => a + b, 0) / known.length) * 1e6) / 1e6 } : null;
|
|
451
|
+
return { runs, by_hand, by_hand_unknown: byHand.length - known.length };
|
|
452
|
+
}
|
|
453
|
+
/** What the money rule reads for agent `agentId` at a run's launch (pastRunsOf), bounded whatever the agent's history:
|
|
454
|
+
* its newest PAST_RUNS runs on no named disk (one runs.list page), the ledger of those runs alone (Learning.ledger with
|
|
455
|
+
* their ids), and each one's launch or rounds read only where its row needs them (no ledger row, a stale one, or no
|
|
456
|
+
* path). Summed with no revision records: the money rule reads each run's own all-in and job $, never a revision's
|
|
457
|
+
* share. The cost page reads the same window of the same figures (agentCostOverStore). */
|
|
458
|
+
export async function pastRunsOverStore(store, agentId) {
|
|
459
|
+
const listed = (await store.runs.list({ agentId, disk: null, limit: PAST_RUNS })).filter((run) => run.agentId === agentId);
|
|
460
|
+
const read = await ledgerReadOf(store, agentId, listed, store, { runIds: listed.map((run) => run.id), named: false });
|
|
461
|
+
return pastRunsOf(summarize({ ...read, revisions: [] }), listed, await sinceReset(store, agentId));
|
|
530
462
|
}
|
|
531
463
|
/** The money rule's decision for agent `agentId` (runner/workflow-first.ts costStopOf) over the runs a run's launch reads
|
|
532
464
|
* (pastRunsOverStore), as the launch reads them: runs that can't be read now stop nothing (workflowFirstOf serves). What
|
|
533
465
|
* the promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
|
|
534
|
-
export const costStopOverStore = async (store, agentId) => costStopOf(await pastRunsOverStore(store, agentId).catch(() =>
|
|
466
|
+
export const costStopOverStore = async (store, agentId) => costStopOf(await pastRunsOverStore(store, agentId).catch(() => NO_PAST_RUNS));
|
|
535
467
|
/** The path run `runId` took and the learned workflow it ran, the one classifier its run record keeps (metrics.path,
|
|
536
468
|
* metrics.workflow, metrics.workflow_digest: workflow-first.ts pathMetrics, written at the run's end) and its cost per run
|
|
537
469
|
* reads (pathOfRow). A run its learned workflow served first (its WORKFLOW_FIRST entry, servedOf): `workflow` when it
|
|
@@ -575,7 +507,7 @@ function pathOfRow(metrics, started) {
|
|
|
575
507
|
return { path, workflow: path === "agent" || typeof m.workflow !== "string" ? null : offeredName(m.workflow, started?.workflows) };
|
|
576
508
|
}
|
|
577
509
|
/** The path a run's views say it took (GET /runs/{id} and each runs list row: `path`, and `workflow`), read off its record
|
|
578
|
-
* as the cost per run reads it (pathOfRow, revisionOfRow:
|
|
510
|
+
* as the cost per run reads it (pathOfRow, revisionOfRow: the ledger read's own, so the run views and GET
|
|
579
511
|
* .../learning/cost never disagree). `path` is null for a run not ended, a workflow revision's run, and a run that learns
|
|
580
512
|
* whose record names none; `workflow` names the learned workflow and its revision only on a run it served (path
|
|
581
513
|
* `workflow` or `handed_back`; the name only when its launch offered one, the digest null when its record names none,
|
|
@@ -642,23 +574,12 @@ export function laidOf(entries) {
|
|
|
642
574
|
}
|
|
643
575
|
return { workflows: [...workflows] };
|
|
644
576
|
}
|
|
645
|
-
/** What
|
|
646
|
-
* what
|
|
647
|
-
*
|
|
648
|
-
* unknown when the row's cost is and the job's is known (the request no one priced came after the job answered: a later
|
|
649
|
-
* round stopped mid-request keeps no figures, so the rounds' sum would read short as if settled); the rounds'
|
|
650
|
-
* look-backs added up when the job's cost is unknown. */
|
|
651
|
-
export function roundsLooked(rounds, rowUsd) {
|
|
652
|
-
const after = rounds.job_cost_usd === null ? rounds.look_back_usd : rowUsd === null ? null : Math.max(0, Math.round((rowUsd - rounds.job_cost_usd) * 1e9) / 1e9);
|
|
653
|
-
return { answered_at: rounds.answered_at, job_cost_usd: rounds.job_cost_usd, look_back_usd: after };
|
|
654
|
-
}
|
|
577
|
+
/** What every round of run `runId` spent when a follow-up opened it again (learn/ledger.ts roundsSpentIn): when its first
|
|
578
|
+
* round's job answered and what the job cost to then, and the look-backs of every round added up (null once one is
|
|
579
|
+
* unknown); undefined for a run with one round, or none that answered. */
|
|
655
580
|
export async function roundsSpendOf(store, runId) {
|
|
656
|
-
|
|
657
|
-
return rounds.length < 2 ? undefined : roundsSpendIn(rounds, roundsSpentOf(rounds));
|
|
581
|
+
return roundsSpentIn((await roundsOf(store, runId)).map((one) => one.proposal));
|
|
658
582
|
}
|
|
659
|
-
/** The one reader of a run's rounds' spend (roundsSpendOf, learningSpendOf): undefined for fewer than two rounds, or when
|
|
660
|
-
* the first one's answer time is unknown. */
|
|
661
|
-
const roundsSpendIn = (rounds, { first, look }) => rounds.length > 1 && first && typeof first.answered_at === "string" ? { answered_at: first.answered_at, job_cost_usd: first.job_cost_usd ?? null, look_back_usd: look } : undefined;
|
|
662
583
|
/** What a run's learning end tells its impact: how it ended and when, the corrections and workflows it started from, when
|
|
663
584
|
* its job answered and its costs once it looked back (a look-back of 0 for an ended one that did not). */
|
|
664
585
|
export function impactLearningOf(end, started, laid, rounds, rowUsd = null) {
|