@parcha/agentrun-serve 0.1.0-beta.42 → 0.1.0-beta.44
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 +126 -0
- package/README.md +221 -31
- package/dist/cli.js +42 -8
- package/dist/composio-preset.d.ts +4 -3
- package/dist/composio-preset.js +12 -4
- package/dist/composio-triggers.d.ts +20 -2
- package/dist/composio-triggers.js +183 -17
- package/dist/computer-lost.d.ts +11 -0
- package/dist/computer-lost.js +19 -0
- package/dist/computer-record.d.ts +10 -2
- package/dist/computer-record.js +50 -18
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +2 -2
- package/dist/console/assets/index-BUktRvS-.css +1 -0
- package/dist/console/assets/index-nB9kTfIN.js +711 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.js +0 -4
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +3 -0
- package/dist/harness-table.d.ts +51 -3
- package/dist/harness-table.js +101 -13
- package/dist/harness.d.ts +18 -1
- package/dist/harness.js +21 -9
- package/dist/host.d.ts +12 -10
- package/dist/host.js +18 -16
- package/dist/import-sources.js +25 -5
- package/dist/import-worker.d.ts +3 -0
- package/dist/import-worker.js +7 -5
- package/dist/import.d.ts +22 -1
- package/dist/import.js +141 -28
- package/dist/learn/audit.d.ts +123 -0
- package/dist/learn/audit.js +159 -0
- package/dist/learn/check.d.ts +3 -0
- package/dist/learn/check.js +1 -1
- package/dist/learn/compile.d.ts +50 -74
- package/dist/learn/compile.js +90 -124
- package/dist/learn/delete.d.ts +17 -0
- package/dist/learn/delete.js +44 -0
- package/dist/learn/folder.d.ts +9 -0
- package/dist/learn/folder.js +13 -2
- package/dist/learn/promote.d.ts +92 -200
- package/dist/learn/promote.js +65 -187
- package/dist/learn/replay-calls.d.ts +5 -0
- package/dist/learn/replay-calls.js +12 -1
- package/dist/learn/secrets.d.ts +8 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learn/skills.d.ts +4 -0
- package/dist/learn/skills.js +19 -5
- package/dist/learn/workflow-nodes.d.ts +23 -3
- package/dist/learn/workflow-nodes.js +30 -12
- package/dist/learned-nodes.d.ts +9 -34
- package/dist/learned-nodes.js +11 -23
- package/dist/learned-workflow.d.ts +6 -8
- package/dist/learned-workflow.js +18 -57
- package/dist/learning-batch.d.ts +13 -5
- package/dist/learning-batch.js +34 -8
- package/dist/learning-cost.d.ts +111 -4
- package/dist/learning-cost.js +120 -3
- package/dist/learning-evals.d.ts +2 -0
- package/dist/learning-evals.js +4 -3
- package/dist/learning-routes.d.ts +8 -0
- package/dist/learning-routes.js +18 -1
- package/dist/learning-setting.d.ts +16 -1
- package/dist/learning-setting.js +34 -2
- package/dist/learning.d.ts +1 -1
- package/dist/listen-first.d.ts +30 -0
- package/dist/listen-first.js +115 -0
- package/dist/llms.txt +2 -1
- package/dist/mcp-servers-store.d.ts +12 -12
- package/dist/model.d.ts +8 -4
- package/dist/model.js +16 -8
- package/dist/openapi.js +203 -87
- package/dist/owner.d.ts +8 -0
- package/dist/owner.js +11 -0
- package/dist/paid-calls.d.ts +6 -0
- package/dist/paid-calls.js +11 -1
- package/dist/place.d.ts +8 -3
- package/dist/place.js +13 -9
- package/dist/provider.js +15 -1
- package/dist/run-launch.d.ts +6 -3
- package/dist/run-launch.js +2 -0
- package/dist/run-learning-served.d.ts +1 -1
- package/dist/run-model-key.d.ts +64 -0
- package/dist/run-model-key.js +147 -0
- package/dist/runner/adapter-loop.d.ts +10 -0
- package/dist/runner/adapter-loop.js +26 -4
- package/dist/runner/adapter-run-spec.d.ts +6 -3
- package/dist/runner/adapter-run-spec.js +83 -25
- package/dist/runner/adapters/index.d.ts +4 -2
- package/dist/runner/adapters/index.js +13 -4
- package/dist/runner/audit.d.ts +68 -0
- package/dist/runner/audit.js +349 -0
- package/dist/runner/beside.d.ts +2 -1
- package/dist/runner/beside.js +6 -3
- package/dist/runner/claim-secrets.d.ts +49 -0
- package/dist/runner/claim-secrets.js +110 -1
- package/dist/runner/cli-run-spec.d.ts +2 -1
- package/dist/runner/cli-run-spec.js +44 -15
- package/dist/runner/commands.d.ts +3 -6
- package/dist/runner/feedback-round.d.ts +3 -2
- package/dist/runner/feedback-round.js +8 -7
- package/dist/runner/harness-loops.js +31 -23
- package/dist/runner/journal-kinds.d.ts +5 -7
- package/dist/runner/journal-kinds.js +0 -7
- package/dist/runner/learning-dispatch.d.ts +0 -16
- package/dist/runner/learning-dispatch.js +4 -47
- package/dist/runner/learning-end.d.ts +55 -13
- package/dist/runner/learning-end.js +128 -44
- package/dist/runner/learning-factory.d.ts +1 -1
- package/dist/runner/learning-factory.js +1 -4
- package/dist/runner/learning-loop.d.ts +2 -38
- package/dist/runner/learning-loop.js +8 -116
- package/dist/runner/loop-contract.d.ts +6 -0
- package/dist/runner/loop-contract.js +4 -0
- package/dist/runner/loop.js +6 -2
- package/dist/runner/main.d.ts +1 -0
- package/dist/runner/main.js +25 -8
- package/dist/runner/memory-claim.d.ts +9 -6
- package/dist/runner/memory-claim.js +12 -9
- package/dist/runner/promotion.d.ts +55 -54
- package/dist/runner/promotion.js +232 -585
- package/dist/runner/reflect-hook.d.ts +14 -0
- package/dist/runner/reflect-hook.js +28 -3
- package/dist/runner/runner.js +57 -4
- package/dist/runner/store-secrets.js +9 -5
- package/dist/runner/workflow-first.d.ts +6 -5
- package/dist/runner/workflow-first.js +17 -12
- package/dist/runner/workflow-spec.js +10 -2
- package/dist/runs-over-store.d.ts +5 -4
- package/dist/runs-over-store.js +13 -3
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/server.d.ts +64 -50
- package/dist/server.js +516 -134
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/seal.d.ts +20 -2
- package/dist/store/seal.js +24 -8
- package/dist/store/sqlite/run-launch.d.ts +4 -1
- package/dist/store/sqlite/run-launch.js +12 -0
- package/dist/store/sqlite/store.d.ts +3 -2
- package/dist/store/sqlite/store.js +119 -16
- package/dist/store/store.d.ts +41 -3
- package/dist/store-reads/analytics.js +11 -1
- package/dist/store-reads/learning.d.ts +57 -7
- package/dist/store-reads/learning.js +194 -28
- package/dist/store-reads/loop.d.ts +4 -39
- package/dist/store-reads/loop.js +10 -79
- package/dist/store-reads/stats.js +13 -3
- package/dist/store-view.d.ts +5 -1
- package/dist/store-view.js +12 -1
- package/dist/telemetry.d.ts +59 -0
- package/dist/telemetry.js +153 -0
- package/dist/tool-keys-store.d.ts +1 -1
- package/dist/tool-keys.d.ts +2 -2
- package/dist/tool-plane.d.ts +12 -12
- package/dist/tools.d.ts +11 -11
- package/dist/tools.js +18 -5
- package/dist/trigger-adapter.d.ts +3 -0
- package/dist/trigger-adapters.d.ts +4 -0
- package/dist/trigger-adapters.js +13 -0
- package/dist/trigger-composio.d.ts +58 -1
- package/dist/trigger-composio.js +160 -6
- package/dist/triggers.d.ts +4 -0
- package/dist/triggers.js +22 -3
- package/dist/validate.d.ts +11 -7
- package/dist/validate.js +8 -7
- package/dist/workspace/computer-for-claim.d.ts +15 -5
- package/dist/workspace/computer-for-claim.js +228 -32
- package/dist/workspace/resolve-computer.d.ts +8 -2
- package/dist/workspace/resolve-computer.js +1 -1
- package/dist/workspace/run-tools.d.ts +1 -1
- package/dist/workspace/run-tools.js +14 -3
- package/package.json +10 -9
- package/dist/console/assets/index-C3gNPz58.css +0 -1
- package/dist/console/assets/index-DYJ_wj27.js +0 -711
- package/dist/learn/general.d.ts +0 -69
- package/dist/learn/general.js +0 -75
- package/dist/learn/refit.d.ts +0 -48
- package/dist/learn/refit.js +0 -264
- package/dist/runner/step-guard.d.ts +0 -62
- package/dist/runner/step-guard.js +0 -93
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ItemStatus, type Learning, type NoteUsed, type Shown } from "../learning.js";
|
|
2
2
|
import { type RunLaunch } from "../run-launch.js";
|
|
3
3
|
import { type Impact, type ImpactLearning } from "../learning-run.js";
|
|
4
|
-
import { type CostReplay, type CostRun, type CostSummary, type RunPath, runCostOf } from "../learning-cost.js";
|
|
4
|
+
import { amortize, type Bill, type CostReplay, type CostRun, type CostSummary, type RunPath, runCostOf } from "../learning-cost.js";
|
|
5
5
|
import { type PastRun, workflowCostsOf } from "../runner/workflow-first.js";
|
|
6
6
|
import { type Reflect } from "../reflect-range.js";
|
|
7
7
|
import type { NoteRuns } from "../learning.js";
|
|
@@ -24,6 +24,8 @@ type KeptRow = {
|
|
|
24
24
|
readonly answered_at?: unknown;
|
|
25
25
|
readonly job_cost_usd?: unknown;
|
|
26
26
|
readonly look_back_usd?: unknown;
|
|
27
|
+
/** What of its look-back its compile turn spent, when it asked one. */
|
|
28
|
+
readonly compile_usd?: unknown;
|
|
27
29
|
};
|
|
28
30
|
/** What a kept row says of a run that looked back (learning-run.ts closeLearning's fields): none for one that did not. */
|
|
29
31
|
declare function answeredOf(row: KeptRow): Pick<Learning, "answered_at" | "job_cost_usd" | "look_back_usd">;
|
|
@@ -39,7 +41,8 @@ export type RunRecords = {
|
|
|
39
41
|
/** Agent `agentId`'s runs' records read at once, whatever their number: its runs' launches (Store.runs.launches) and every
|
|
40
42
|
* round its runs' learning kept with the learning runs they name (Store.learning.kept), two reads beside `listed` (its
|
|
41
43
|
* runs, as runs.list read them). Each answers as the store's per-run read does: a run of `listed` with no launch row is
|
|
42
|
-
* StoreLaunchMissing, one that kept no learning has no round. A run not of `listed` is read from the store
|
|
44
|
+
* StoreLaunchMissing, one that kept no learning has no round. A run not of `listed` is read from the store, but for an
|
|
45
|
+
* audit's shadow run: `listed` is every run of the agent, its shadows among them. */
|
|
43
46
|
export declare function agentRecordsOf(store: Store, agentId: string, listed: readonly RunRecord[]): Promise<RunRecords>;
|
|
44
47
|
/** Run `runId`'s learning end (its newest round's, or round `round`'s); null when the run kept no learning. Proposed while its learning run has not ended. `records`: where its rows are read (the store's per-run reads by default). */
|
|
45
48
|
export declare function runLearningEnd(store: Store, runId: string, round?: number, records?: RunRecords): Promise<RunLearningEnd | null>;
|
|
@@ -103,11 +106,49 @@ export declare function agentCostOverStore(store: Store, agentId: string, limit?
|
|
|
103
106
|
unknown_cost_runs: number;
|
|
104
107
|
unknown_path_runs: number;
|
|
105
108
|
workflows: ReturnType<typeof workflowCostsOf>;
|
|
109
|
+
bill: Bill;
|
|
106
110
|
}>;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
+
type LedgerRow = Pick<RunRecord, "id" | "metrics" | "startedAt" | "costUsd" | "costAtMostUsd">;
|
|
112
|
+
/** A compile turn a run's look-back asked (runner/reflect-hook.ts), as its kept round says it: the revision it drafted
|
|
113
|
+
* (`digest`, null when every draft failed its checks or the cap stopped the turn, so it drafted none; `workflow` null for
|
|
114
|
+
* the latter, which kept no draft's name) and what its turn spent (`usd`, the round's `compile_usd`: null when unknown;
|
|
115
|
+
* 0 for a round that kept no split, whose look-back holds its compile turn). */
|
|
116
|
+
export type CompileSpend = {
|
|
117
|
+
readonly workflow: string | null;
|
|
118
|
+
readonly digest: string | null;
|
|
119
|
+
readonly usd: number | null;
|
|
120
|
+
};
|
|
121
|
+
/** Each revision of the agent's learned workflows (one per workflow and digest its promotion records name, in the order
|
|
122
|
+
* they were first recorded) with its learning spend, and what its served runs carry of it (amortize), over `rows`: the
|
|
123
|
+
* agent's ended runs that do a job or check one (no shadow, no imported or archived run). A revision is its workflow and
|
|
124
|
+
* its digest, never the digest alone: two workflows of the same text share a digest, and each pays its own way. Its
|
|
125
|
+
* spend is the compile turns that drafted it (`compiles` of its workflow at its digest: a recompile drafts a revision of
|
|
126
|
+
* its own) and its check (the replay runs that ran it: metrics.path `replay` of its workflow at its digest); unknown
|
|
127
|
+
* once a part's cost is (null, or only a bound). A compile whose drafts all failed drafted none: no revision carries it
|
|
128
|
+
* (the bill's unattributed does). It is spread over its first K served runs (`workflow` or `handed_back` of its workflow
|
|
129
|
+
* at its digest), K its check's compile_after; `state` from its newest record. */
|
|
130
|
+
export declare function revisionLedgerOf(records: readonly PromotionRecord[], rows: readonly LedgerRow[], compiles?: readonly CompileSpend[]): ReturnType<typeof amortize>;
|
|
131
|
+
/** Run `runId`'s path and its costs as GET /runs/{id} shows them (A8): `path` its record's (metrics.path, RUN_PATHS:
|
|
132
|
+
* `agent` for a run that does not learn; null while it has not ended, for a workflow revision's run, and for a run that
|
|
133
|
+
* learns whose end named none); `usd` its own spend, the job's (runCostOf's `cost_usd`: its row's cost, `rowUsd` when
|
|
134
|
+
* given, less its look-back and compile turns; null while unknown); `learning_usd` what learning spent for it: its
|
|
135
|
+
* look-back less its compile turns, its grades, its audit shadow's cost, and its share of its revision's compile and
|
|
136
|
+
* check (revisionLedgerOf); `all_in_usd` the two. A replay or a shadow run carries none: its spend is on the runs it
|
|
137
|
+
* serves, so its `learning_usd` and `all_in_usd` are 0, and the sum of every run's `all_in_usd` and the bill's
|
|
138
|
+
* `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null; null while
|
|
139
|
+
* unknown (its shadow still at work, a grade's, a compile's or a check's cost unknown), never $0. Undefined for an
|
|
140
|
+
* unknown run. */
|
|
141
|
+
export declare function runAllInOverStore(store: Store, runId: string, rowUsd?: number | null): Promise<{
|
|
142
|
+
path: RunPath | null;
|
|
143
|
+
usd: number | null;
|
|
144
|
+
learning_usd: number | null;
|
|
145
|
+
all_in_usd: number | null;
|
|
146
|
+
} | undefined>;
|
|
147
|
+
/** The check the serving promotion was decided on, as the cost summary says it: the champion (championOf) of the learned
|
|
148
|
+
* workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
|
|
149
|
+
* (the share of the inputs it ran on that said what the agent said, against the agent's own 100%). `cleared`: it passed
|
|
150
|
+
* (a champion is promoted), with the agreement check it passed (`agreed` of `alone`, its bar `agree_min`). Null when no run was served, nothing serves now (held, or never promoted), or it was promoted
|
|
151
|
+
* on no input (a dry run). */
|
|
111
152
|
export declare function servingReplayOf(records: readonly PromotionRecord[], runs: readonly CostRun[]): CostReplay | null;
|
|
112
153
|
/** What run `runId`'s learning spent, as its cost per run counts it, over every round a follow-up opened (roundsOf): what
|
|
113
154
|
* it kept as it answered (`looked`: answeredOf), and its grades' spend (`grade`): each round's learning run's cost once
|
|
@@ -130,6 +171,15 @@ type RunRecord = NonNullable<Awaited<ReturnType<Store["runs"]["get"]>>>;
|
|
|
130
171
|
* never the newest round's alone), else its one round's (learningSpendOf). `total_usd` is the run's all-in cost, the one
|
|
131
172
|
* the cost page shows and the money rule reads (pastRunsOverStore); null when any part is unknown. */
|
|
132
173
|
export declare function allInOf(store: RunRecords, run: RunRecord): Promise<ReturnType<typeof runCostOf>>;
|
|
174
|
+
/** What run `runId`'s audit shadow run (runner/audit.ts) adds to its all-in cost: `open` while it is queued or running
|
|
175
|
+
* (its cost not known yet: `usd` null, never $0), else its cost once it ended, stopped or failed (null: unknown); 0 when
|
|
176
|
+
* none was started, and for a shadow itself. */
|
|
177
|
+
export declare function shadowSpendOf(store: {
|
|
178
|
+
readonly runs: Pick<RunRecords["runs"], "get">;
|
|
179
|
+
}, runId: string): Promise<{
|
|
180
|
+
open: boolean;
|
|
181
|
+
usd: number | null;
|
|
182
|
+
}>;
|
|
133
183
|
/** One run of the cost series, as number `n` of it: its figures (allInOf) and its path (agentCostOverStore's loop body,
|
|
134
184
|
* and a learning batch's row once the run's learning has settled). */
|
|
135
185
|
export declare function costRunOf(store: RunRecords, run: RunRecord, n: number): Promise<CostRun>;
|
|
@@ -309,7 +359,7 @@ export declare function runLearningOverStore(store: Store, runId: string, shown:
|
|
|
309
359
|
feedback_status: "running" | "ended" | "not_run" | null;
|
|
310
360
|
source: "feedback" | "eval";
|
|
311
361
|
graded_at: string;
|
|
312
|
-
path: "agent" | "workflow" | "handed_back" | "
|
|
362
|
+
path: "agent" | "workflow" | "replay" | "handed_back" | "audit";
|
|
313
363
|
workflow_digest: string | null;
|
|
314
364
|
feedback: "look_back" | null;
|
|
315
365
|
run_id: string;
|
|
@@ -12,7 +12,8 @@ import { runLearningBody } from "../learning-routes.js";
|
|
|
12
12
|
import { learningStartOf, learningStartOfStored, readLaunchBody, seedFailed } from "../run-launch.js";
|
|
13
13
|
import { said, STOPPED } from "../learning-words.js";
|
|
14
14
|
import { learningImpact, noteRuns, notesRead } from "../learning-run.js";
|
|
15
|
-
import { costSummary, RUN_PATHS, runCostOf } from "../learning-cost.js";
|
|
15
|
+
import { amortize, billOf, costSummary, JOB_PATHS, noiseOff, RUN_PATHS, runCostOf, SERVED_PATHS } from "../learning-cost.js";
|
|
16
|
+
import { COMPILE_AFTER_DEFAULT } from "../learning-setting.js";
|
|
16
17
|
import { modelsOwn, runPathOf, workflowNameOf } from "../runner/learning-end.js";
|
|
17
18
|
import { feedbackStatus } from "../runner/feedback-round.js";
|
|
18
19
|
import { PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
|
|
@@ -24,8 +25,8 @@ import { journalEntries } from "../runner/journal-reader.js";
|
|
|
24
25
|
import { KINDS } from "../runner/journal-kinds.js";
|
|
25
26
|
import { keptOf, learnedOf, lostOf } from "../runner/learning-loop.js";
|
|
26
27
|
import { promotedOf } from "../learn/compile.js";
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
28
|
+
import { isShadowRun, shadowRunId } from "../learn/audit.js";
|
|
29
|
+
import { championOf, checkShares, gradesOf, gradeView, promotionsOf } from "../learn/promote.js";
|
|
29
30
|
const timeOf = (ms) => new Date(ms).toISOString();
|
|
30
31
|
const usdOrNull = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
31
32
|
/** What a kept row says of a run that looked back (learning-run.ts closeLearning's fields): none for one that did not. */
|
|
@@ -37,7 +38,8 @@ function answeredOf(row) {
|
|
|
37
38
|
/** Agent `agentId`'s runs' records read at once, whatever their number: its runs' launches (Store.runs.launches) and every
|
|
38
39
|
* round its runs' learning kept with the learning runs they name (Store.learning.kept), two reads beside `listed` (its
|
|
39
40
|
* runs, as runs.list read them). Each answers as the store's per-run read does: a run of `listed` with no launch row is
|
|
40
|
-
* StoreLaunchMissing, one that kept no learning has no round. A run not of `listed` is read from the store
|
|
41
|
+
* StoreLaunchMissing, one that kept no learning has no round. A run not of `listed` is read from the store, but for an
|
|
42
|
+
* audit's shadow run: `listed` is every run of the agent, its shadows among them. */
|
|
41
43
|
export async function agentRecordsOf(store, agentId, listed) {
|
|
42
44
|
const [starts, kept] = await Promise.all([store.runs.learningStarts(agentId), store.learning.kept(agentId)]);
|
|
43
45
|
const ours = new Set(listed.map((run) => run.id));
|
|
@@ -57,7 +59,9 @@ export async function agentRecordsOf(store, agentId, listed) {
|
|
|
57
59
|
throw new StoreLaunchMissing(runId);
|
|
58
60
|
return learningStartOfStored(startOf.get(runId));
|
|
59
61
|
},
|
|
60
|
-
|
|
62
|
+
// An audit's shadow run is the agent's: one not among `listed` (every run of the agent) was never started, so a
|
|
63
|
+
// run's shadow (allInOf) is no read a run.
|
|
64
|
+
get: async (id) => rows.get(id) ?? (isShadowRun(id) ? undefined : store.runs.get(id)),
|
|
61
65
|
},
|
|
62
66
|
learning: {
|
|
63
67
|
proposal: async (runId, round) => {
|
|
@@ -118,26 +122,20 @@ async function endOfKept(store, kept, row, records = store) {
|
|
|
118
122
|
// What the grade spent: the graded entry's, or what a grade that failed spent before it did.
|
|
119
123
|
const spent = graded ?? (dropped && "usd" in dropped ? { usd: dropped.usd ?? null, tokens: dropped.tokens ?? null } : undefined);
|
|
120
124
|
const cost = spent ? { cost_usd: { grade: spent.usd }, tokens: { grade: spent.tokens ?? null } } : {};
|
|
121
|
-
// A generality check that could not run (learning-loop.ts `unchecked`) is said after the end's words: the grader read
|
|
122
|
-
// every note. Its provider's error stays in the journal.
|
|
123
|
-
// One skipped for no room under the cap (GENERAL_NO_BUDGET) says so.
|
|
124
|
-
const unchecked = graded?.unchecked ?? dropped?.unchecked;
|
|
125
|
-
const skip = unchecked === undefined ? undefined : unchecked === GENERAL_NO_BUDGET ? GENERAL_NO_ROOM : GENERAL_UNCHECKED;
|
|
126
|
-
const withSkip = (detail) => (skip ? [detail, skip].filter(Boolean).join(" ") : detail);
|
|
127
125
|
if (merged && graded) {
|
|
128
126
|
// Each item as the merge settled it: its record's verdict (a note it left out is no longer kept, and says why). A merge
|
|
129
127
|
// recorded with none (before the record kept it) reads no item decisions.
|
|
130
128
|
const verdict = merged.verdict;
|
|
131
129
|
const what = verdict ? learnedOf(keptOf(row.proposal, verdict), verdict) : { learned: [], pending: [], skills: [], workflows: [] };
|
|
132
130
|
return {
|
|
133
|
-
state: "merged", reason: null, detail:
|
|
131
|
+
state: "merged", reason: null, detail: merged.detail, at: endedAt, ...left, ...(verdict ? { verdict } : {}), graded: { ...graded.graded }, ...cost, merged: merged.commit,
|
|
134
132
|
learned: what.learned, ...(what.pending.length ? { pending: what.pending } : {}), ...(what.skills.length ? { skills: what.skills } : {}), ...(what.workflows.length ? { workflows: what.workflows } : {}),
|
|
135
133
|
};
|
|
136
134
|
}
|
|
137
135
|
const reason = (dropped?.reason ?? null);
|
|
138
136
|
const lost = reason === "grader" ? lostOf(row.proposal) : [];
|
|
139
137
|
return {
|
|
140
|
-
state: "dropped", reason, detail:
|
|
138
|
+
state: "dropped", reason, detail: dropped?.detail ?? null, at: endedAt,
|
|
141
139
|
...(dropped?.problems?.length ? { problems: [...dropped.problems] } : left),
|
|
142
140
|
// The verdict, when one was given; the blobs it read only go with a merge (the server kept them for its merge alone).
|
|
143
141
|
...(graded ? { verdict: graded.verdict } : {}), ...cost, ...(lost.length ? { lost } : {}),
|
|
@@ -265,7 +263,8 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
|
|
|
265
263
|
const listed = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId);
|
|
266
264
|
// Every run's launch and learning in a few reads (agentRecordsOf), never a read a run.
|
|
267
265
|
const records = await agentRecordsOf(store, agentId, listed);
|
|
268
|
-
|
|
266
|
+
// An audit's shadow run is no run of the series: its cost is its served run's learning (allInOf).
|
|
267
|
+
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));
|
|
269
268
|
const runs = [];
|
|
270
269
|
let unknownPath = 0;
|
|
271
270
|
for (const run of ended) {
|
|
@@ -284,20 +283,160 @@ export async function agentCostOverStore(store, agentId, limit = Infinity) {
|
|
|
284
283
|
// the runs the run's launch reads (pastRunsOverStore) at the all-in cost this series shows: a workflow over the line
|
|
285
284
|
// shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
|
|
286
285
|
const workflows = workflowCostsOf(await pastRunsOverStore(store, agentId, new Map(runs.map((run) => [run.run_id, run.total_usd])), records));
|
|
287
|
-
const
|
|
288
|
-
|
|
286
|
+
const promotions = await promotionsOf(store.settings, agentId);
|
|
287
|
+
const replay = servingReplayOf(promotions, runs);
|
|
288
|
+
// The owner's bill: booked $, every run of the series (billOf), from the first run on a served path (read from the
|
|
289
|
+
// run rows: the promotion feed is trimmed, so its oldest record need not be the first promotion), and each revision's
|
|
290
|
+
// compile and check over the served runs that carry it.
|
|
291
|
+
const ledger = await agentLedgerOf(records, promotions, listed);
|
|
292
|
+
const servedFrom = listed.filter((run) => !run.importedFrom && FROM_PATHS.includes(metricOf(run).path)).map((run) => run.startedAt).sort((a, b) => a - b)[0];
|
|
293
|
+
const booked = billOf(runs, servedFrom === undefined ? null : new Date(servedFrom).toISOString());
|
|
294
|
+
// What no run carries is the booked total less every run's all-in (A8), so a revision whose records aged out of the
|
|
295
|
+
// feed lands here, never nowhere: unknown while any run's figures are.
|
|
296
|
+
let carried = 0;
|
|
297
|
+
for (const run of runs) {
|
|
298
|
+
const all = run.path === "replay" ? 0 : run.cost_upper_bound ? null : allInV2Of(run, ledger.compiles.get(run.run_id) ?? [], ledger.shares);
|
|
299
|
+
carried = carried === null || all === null ? null : noiseOff(carried + all);
|
|
300
|
+
}
|
|
301
|
+
const unattributed = booked.withheld > 0 || carried === null ? null : noiseOff(booked.booked_usd - carried);
|
|
302
|
+
const bill = { ...booked, revisions: ledger.revisions, unattributed_usd: unattributed, holds: holdsOf(promotions, runs) };
|
|
303
|
+
return { runs: runs.slice(Math.max(0, runs.length - limit)), summary: costSummary(runs, replay), total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: unknownPath, workflows, bill };
|
|
304
|
+
}
|
|
305
|
+
/** The paths the bill's baseline ends at: the first run a learned workflow served, or an audit audited. */
|
|
306
|
+
const FROM_PATHS = ["workflow", "handed_back", "audit"];
|
|
307
|
+
/** Each hold of the agent's learned workflows (its promotion records that say hold), placed on the series: at the first
|
|
308
|
+
* run that does a job from it on, by its number among them (the points' `n`), null when none has run since. */
|
|
309
|
+
function holdsOf(records, runs) {
|
|
310
|
+
const jobs = runs.filter((run) => JOB_PATHS.includes(run.path));
|
|
311
|
+
return records.filter((one) => one.decided === "hold").map((one) => {
|
|
312
|
+
const at = jobs.findIndex((run) => run.started_at >= one.at);
|
|
313
|
+
return { n: at < 0 ? null : at + 1, at: one.at, workflow: one.workflow, by: one.audit ? "audit" : one.check ? "check" : "dsl", reason: one.reason };
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
const metricOf = (row) => (row.metrics ?? {});
|
|
317
|
+
/** The revision a run ran (metrics.workflow and metrics.workflow_digest, workflow-first.ts pathMetrics), undefined when it
|
|
318
|
+
* names none: two workflows of the same text share a digest, so a revision is never its digest alone. */
|
|
319
|
+
const revisionOfRow = (row) => {
|
|
320
|
+
const { workflow, workflow_digest: digest } = metricOf(row);
|
|
321
|
+
return typeof workflow === "string" && typeof digest === "string" ? { workflow, digest } : undefined;
|
|
322
|
+
};
|
|
323
|
+
/** The compile turns run `runId`'s rounds asked, oldest first: none for a run that compiled nothing. */
|
|
324
|
+
async function compilesOf(store, runId) {
|
|
325
|
+
return (await roundsOf(store, runId)).flatMap((kept) => {
|
|
326
|
+
const row = (kept.proposal ?? {});
|
|
327
|
+
const { compiled, draft_errors } = compiledOf(row);
|
|
328
|
+
// A compile the cap stopped (runner/reflect-hook.ts `capped`) closed its round with its turn's spend and no draft: no
|
|
329
|
+
// revision to serve, so its spend is unattributed, never its run's look-back.
|
|
330
|
+
if (!compiled)
|
|
331
|
+
return "compile_usd" in row ? [{ workflow: null, digest: null, usd: usdOrNull(row.compile_usd) }] : [];
|
|
332
|
+
const usd = "compile_usd" in row ? usdOrNull(row.compile_usd) : 0;
|
|
333
|
+
return [{ workflow: compiled.workflow, digest: compiled.digest && !draft_errors?.length ? compiled.digest : null, usd }];
|
|
334
|
+
});
|
|
289
335
|
}
|
|
290
|
-
/**
|
|
291
|
-
*
|
|
292
|
-
* and
|
|
293
|
-
|
|
336
|
+
/** A run's v2 learning (A8): what learning cost for it as the cost series counts it (its look-back, grades and audit
|
|
337
|
+
* shadow: `run.learning_usd`) less its compile turns (each its revision's), with its share of its revision's compile
|
|
338
|
+
* and check (`shares`: a missing entry is none, an unknown share stays unknown). Null while any part is unknown. */
|
|
339
|
+
function learningV2Of(run, compiles, shares) {
|
|
340
|
+
const compiled = compiles.reduce((sum, one) => (sum === null || one.usd === null ? null : sum + one.usd), 0);
|
|
341
|
+
const share = shares.has(run.run_id) ? shares.get(run.run_id) : 0;
|
|
342
|
+
if (run.learning_usd === null || compiled === null || share === null)
|
|
343
|
+
return null;
|
|
344
|
+
return noiseOff(Math.max(0, run.learning_usd - compiled) + share);
|
|
345
|
+
}
|
|
346
|
+
/** A run's v2 all-in: its own $ (the job's) and its v2 learning (learningV2Of); null when either is. */
|
|
347
|
+
function allInV2Of(run, compiles, shares) {
|
|
348
|
+
const learning = learningV2Of(run, compiles, shares);
|
|
349
|
+
return learning === null || run.cost_usd === null ? null : noiseOff(run.cost_usd + learning);
|
|
350
|
+
}
|
|
351
|
+
/** The agent's revision ledger (revisionLedgerOf) over `listed` (its runs, as runs.list read them): its ended runs that do
|
|
352
|
+
* a job or check one (no shadow, no imported or archived run), and the compile turns their rounds asked, by run. */
|
|
353
|
+
async function agentLedgerOf(records, promotions, listed) {
|
|
354
|
+
const rows = listed.filter((one) => FINAL_STATES.includes(one.state) && !one.importedFrom && one.archivedAt === null && !isShadowRun(one.id));
|
|
355
|
+
const compiles = new Map();
|
|
356
|
+
for (const row of rows) {
|
|
357
|
+
const asked = await compilesOf(records, row.id);
|
|
358
|
+
if (asked.length)
|
|
359
|
+
compiles.set(row.id, asked);
|
|
360
|
+
}
|
|
361
|
+
return { ...revisionLedgerOf(promotions, rows, [...compiles.values()].flat()), compiles };
|
|
362
|
+
}
|
|
363
|
+
/** Each revision of the agent's learned workflows (one per workflow and digest its promotion records name, in the order
|
|
364
|
+
* they were first recorded) with its learning spend, and what its served runs carry of it (amortize), over `rows`: the
|
|
365
|
+
* agent's ended runs that do a job or check one (no shadow, no imported or archived run). A revision is its workflow and
|
|
366
|
+
* its digest, never the digest alone: two workflows of the same text share a digest, and each pays its own way. Its
|
|
367
|
+
* spend is the compile turns that drafted it (`compiles` of its workflow at its digest: a recompile drafts a revision of
|
|
368
|
+
* its own) and its check (the replay runs that ran it: metrics.path `replay` of its workflow at its digest); unknown
|
|
369
|
+
* once a part's cost is (null, or only a bound). A compile whose drafts all failed drafted none: no revision carries it
|
|
370
|
+
* (the bill's unattributed does). It is spread over its first K served runs (`workflow` or `handed_back` of its workflow
|
|
371
|
+
* at its digest), K its check's compile_after; `state` from its newest record. */
|
|
372
|
+
export function revisionLedgerOf(records, rows, compiles = []) {
|
|
373
|
+
const ordered = [...rows].sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
|
|
374
|
+
const newest = new Map();
|
|
375
|
+
for (const record of records)
|
|
376
|
+
newest.set(`${record.workflow}\0${record.digest}`, record);
|
|
377
|
+
const sum = (parts) => (parts.some((one) => one === null) ? null : noiseOff(parts.reduce((total, one) => total + one, 0)));
|
|
378
|
+
const ran = (row, record) => { const at = revisionOfRow(row); return at?.workflow === record.workflow && at.digest === record.digest; };
|
|
379
|
+
const revisions = [...newest.values()].map((record) => {
|
|
380
|
+
const replays = ordered.filter((row) => metricOf(row).path === "replay" && ran(row, record));
|
|
381
|
+
const check = sum(replays.map((row) => (typeof row.costAtMostUsd === "number" ? null : row.costUsd)));
|
|
382
|
+
const compile = sum(compiles.filter((one) => one.workflow === record.workflow && one.digest === record.digest).map((one) => one.usd));
|
|
383
|
+
const state = record.decided === "replaying" ? "checking" : championOf(records, record.workflow)?.digest === record.digest ? "serving" : "stopped";
|
|
384
|
+
return { workflow: record.workflow, digest: record.digest, compile_usd: compile, check_usd: check, spend_usd: sum([compile, check]), k: record.check?.compile_after ?? COMPILE_AFTER_DEFAULT, state };
|
|
385
|
+
});
|
|
386
|
+
const served = ordered.flatMap((row) => {
|
|
387
|
+
const at = revisionOfRow(row);
|
|
388
|
+
return SERVED_PATHS.includes(metricOf(row).path) && at ? [{ run_id: row.id, ...at }] : [];
|
|
389
|
+
});
|
|
390
|
+
return amortize(revisions, served);
|
|
391
|
+
}
|
|
392
|
+
/** Run `runId`'s path and its costs as GET /runs/{id} shows them (A8): `path` its record's (metrics.path, RUN_PATHS:
|
|
393
|
+
* `agent` for a run that does not learn; null while it has not ended, for a workflow revision's run, and for a run that
|
|
394
|
+
* learns whose end named none); `usd` its own spend, the job's (runCostOf's `cost_usd`: its row's cost, `rowUsd` when
|
|
395
|
+
* given, less its look-back and compile turns; null while unknown); `learning_usd` what learning spent for it: its
|
|
396
|
+
* look-back less its compile turns, its grades, its audit shadow's cost, and its share of its revision's compile and
|
|
397
|
+
* check (revisionLedgerOf); `all_in_usd` the two. A replay or a shadow run carries none: its spend is on the runs it
|
|
398
|
+
* serves, so its `learning_usd` and `all_in_usd` are 0, and the sum of every run's `all_in_usd` and the bill's
|
|
399
|
+
* `unattributed_usd` is the booked total. While the run is at work `usd` is `rowUsd` and the rest null; null while
|
|
400
|
+
* unknown (its shadow still at work, a grade's, a compile's or a check's cost unknown), never $0. Undefined for an
|
|
401
|
+
* unknown run. */
|
|
402
|
+
export async function runAllInOverStore(store, runId, rowUsd) {
|
|
403
|
+
const run = await store.runs.get(runId);
|
|
404
|
+
if (!run)
|
|
405
|
+
return undefined;
|
|
406
|
+
const row = rowUsd === undefined ? (typeof run.costAtMostUsd === "number" ? null : run.costUsd) : rowUsd;
|
|
407
|
+
const ended = FINAL_STATES.includes(run.state);
|
|
408
|
+
const kind = (await store.agents.revision(run.revisionId))?.kind;
|
|
409
|
+
const started = kind === "workflow" ? undefined : await learningStartOfRun(store, run.id).catch(() => undefined);
|
|
410
|
+
const path = !ended || kind === "workflow" ? null : (pathOfRow(run.metrics, started)?.path ?? null);
|
|
411
|
+
if (!ended)
|
|
412
|
+
return { path, usd: row, learning_usd: null, all_in_usd: null };
|
|
413
|
+
const { looked, grade } = await learningSpendOf(store, run.id, row);
|
|
414
|
+
const shadow = (await shadowSpendOf(store, run.id)).usd;
|
|
415
|
+
const cost = runCostOf(row, looked, grade === null || shadow === null ? null : grade + shadow);
|
|
416
|
+
if (path === "replay" || path === "audit" || isShadowRun(run.id))
|
|
417
|
+
return { path, usd: cost.cost_usd, learning_usd: 0, all_in_usd: 0 };
|
|
418
|
+
// Its share of its revision's compile and check: a served run's, over the agent's ledger.
|
|
419
|
+
let shares = new Map();
|
|
420
|
+
if (path !== null && SERVED_PATHS.includes(path) && revisionOfRow(run)) {
|
|
421
|
+
const listed = (await store.runs.list({ agentId: run.agentId })).filter((one) => one.agentId === run.agentId);
|
|
422
|
+
shares = (await agentLedgerOf(await agentRecordsOf(store, run.agentId, listed), await promotionsOf(store.settings, run.agentId), listed)).shares;
|
|
423
|
+
}
|
|
424
|
+
const figures = { run_id: run.id, ...cost };
|
|
425
|
+
const compiles = await compilesOf(store, run.id);
|
|
426
|
+
return { path, usd: cost.cost_usd, learning_usd: learningV2Of(figures, compiles, shares), all_in_usd: allInV2Of(figures, compiles, shares) };
|
|
427
|
+
}
|
|
428
|
+
/** The check the serving promotion was decided on, as the cost summary says it: the champion (championOf) of the learned
|
|
429
|
+
* workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
|
|
430
|
+
* (the share of the inputs it ran on that said what the agent said, against the agent's own 100%). `cleared`: it passed
|
|
431
|
+
* (a champion is promoted), with the agreement check it passed (`agreed` of `alone`, its bar `agree_min`). Null when no run was served, nothing serves now (held, or never promoted), or it was promoted
|
|
432
|
+
* on no input (a dry run). */
|
|
294
433
|
export function servingReplayOf(records, runs) {
|
|
295
434
|
const name = runs.findLast((run) => (run.path === "workflow" || run.path === "handed_back") && run.workflow !== null)?.workflow;
|
|
296
|
-
const
|
|
297
|
-
if (!
|
|
435
|
+
const check = name ? championOf(records, name)?.check : undefined;
|
|
436
|
+
if (!check?.n)
|
|
298
437
|
return null;
|
|
299
|
-
const
|
|
300
|
-
return { n:
|
|
438
|
+
const agreed = check.agreed ?? 0, alone = check.alone ?? 0;
|
|
439
|
+
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 };
|
|
301
440
|
}
|
|
302
441
|
/** What run `runId`'s learning spent, as its cost per run counts it, over every round a follow-up opened (roundsOf): what
|
|
303
442
|
* it kept as it answered (`looked`: answeredOf), and its grades' spend (`grade`): each round's learning run's cost once
|
|
@@ -355,7 +494,21 @@ async function roundsSpentOf(store, rounds) {
|
|
|
355
494
|
export async function allInOf(store, run) {
|
|
356
495
|
// Every round's (learningSpendOf over the rounds: what its row spent after its job answered is learning, roundsLooked).
|
|
357
496
|
const { looked, grade } = await learningSpendOf(store, run.id, run.costUsd);
|
|
358
|
-
|
|
497
|
+
// An audit's shadow run of it (runner/audit.ts) is its learning too (shadowSpendOf): unknown while it is queued or
|
|
498
|
+
// running, never $0.
|
|
499
|
+
const audited = (await shadowSpendOf(store, run.id)).usd;
|
|
500
|
+
return runCostOf(run.costUsd, looked, grade === null || audited === null ? null : grade + audited);
|
|
501
|
+
}
|
|
502
|
+
/** What run `runId`'s audit shadow run (runner/audit.ts) adds to its all-in cost: `open` while it is queued or running
|
|
503
|
+
* (its cost not known yet: `usd` null, never $0), else its cost once it ended, stopped or failed (null: unknown); 0 when
|
|
504
|
+
* none was started, and for a shadow itself. */
|
|
505
|
+
export async function shadowSpendOf(store, runId) {
|
|
506
|
+
if (isShadowRun(runId))
|
|
507
|
+
return { open: false, usd: 0 };
|
|
508
|
+
const shadow = await store.runs.get(shadowRunId(runId));
|
|
509
|
+
if (!shadow)
|
|
510
|
+
return { open: false, usd: 0 };
|
|
511
|
+
return FINAL_STATES.includes(shadow.state) ? { open: false, usd: shadow.costUsd ?? null } : { open: true, usd: null };
|
|
359
512
|
}
|
|
360
513
|
/** One run of the cost series, as number `n` of it: its figures (allInOf) and its path (agentCostOverStore's loop body,
|
|
361
514
|
* and a learning batch's row once the run's learning has settled). */
|
|
@@ -375,8 +528,21 @@ export async function costRunOf(store, run, n) {
|
|
|
375
528
|
export async function pastRunsOverStore(store, agentId, known, records = store) {
|
|
376
529
|
const kinds = new Map();
|
|
377
530
|
const kept = [];
|
|
378
|
-
|
|
379
|
-
|
|
531
|
+
const page = await store.runs.list({ agentId, limit: PAST_RUNS });
|
|
532
|
+
// Each run's audit shadow is read off the page (allInOf's shadowSpendOf): a shadow starts after the run it audits ends,
|
|
533
|
+
// so it is newer than that run and on the same page of the newest runs; one not on it was never started.
|
|
534
|
+
const shadows = new Map(page.filter((run) => isShadowRun(run.id)).map((run) => [run.id, run]));
|
|
535
|
+
const learningStart = records.runs.learningStart;
|
|
536
|
+
const paged = {
|
|
537
|
+
learning: records.learning,
|
|
538
|
+
runs: {
|
|
539
|
+
launch: (id) => records.runs.launch(id),
|
|
540
|
+
get: (id) => (isShadowRun(id) ? Promise.resolve(shadows.get(id)) : records.runs.get(id)),
|
|
541
|
+
...(learningStart ? { learningStart: (id) => learningStart.call(records.runs, id) } : {}),
|
|
542
|
+
},
|
|
543
|
+
};
|
|
544
|
+
for (const run of page) {
|
|
545
|
+
if (run.agentId !== agentId || !FINAL_STATES.includes(run.state) || run.importedFrom || run.archivedAt !== null || isShadowRun(run.id))
|
|
380
546
|
continue;
|
|
381
547
|
if (!kinds.has(run.revisionId))
|
|
382
548
|
kinds.set(run.revisionId, (await store.agents.revision(run.revisionId))?.kind);
|
|
@@ -386,7 +552,7 @@ export async function pastRunsOverStore(store, agentId, known, records = store)
|
|
|
386
552
|
// A few reads a run, a batch at a time: a launch reads up to PAST_RUNS of them.
|
|
387
553
|
const past = [];
|
|
388
554
|
for (let at = 0; at < kept.length; at += 16) {
|
|
389
|
-
past.push(...(await Promise.all(kept.slice(at, at + 16).map(async (run) => ({ metrics: run.metrics, startedAt: run.startedAt, allInUsd: known?.has(run.id) ? known.get(run.id) : (await allInOf(
|
|
555
|
+
past.push(...(await Promise.all(kept.slice(at, at + 16).map(async (run) => ({ metrics: run.metrics, startedAt: run.startedAt, allInUsd: known?.has(run.id) ? known.get(run.id) : (await allInOf(paged, run)).total_usd })))));
|
|
390
556
|
}
|
|
391
557
|
return past;
|
|
392
558
|
}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { type Store } from "../store/store.js";
|
|
2
|
-
import type { GuardRow } from "../learned-nodes.js";
|
|
3
2
|
/** How many compile turns the read gives, the newest. */
|
|
4
3
|
export declare const LOOP_COMPILES_MAX = 50;
|
|
5
|
-
/** How many runs' p a guarded step gives, its newest. */
|
|
6
|
-
export declare const LOOP_POINTS_MAX = 200;
|
|
7
4
|
/** One compile turn of a run, in the round of it that compiled (a follow-up opens a round past the first, and each
|
|
8
5
|
* round's end may compile): `errors` its draft's when none passed (never silent). */
|
|
9
6
|
export type LoopCompile = {
|
|
@@ -20,43 +17,11 @@ export type LoopCompile = {
|
|
|
20
17
|
promoted: boolean | null;
|
|
21
18
|
draft_errors: string[];
|
|
22
19
|
};
|
|
23
|
-
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
run_id: string;
|
|
28
|
-
p: number | null;
|
|
29
|
-
escalated: boolean;
|
|
30
|
-
correct: boolean | null;
|
|
31
|
-
};
|
|
32
|
-
/** One guarded step of one revision of a learned workflow, over the runs it served first on (each run once, however many
|
|
33
|
-
* checks a map made of it): its threshold as those runs read it (null when only replays ran it), how many it guarded and
|
|
34
|
-
* handed back, how many of those the workflow answered
|
|
35
|
-
* alone were graded right and wrong, each over every such run, and the newest runs' p. A replay run reads its guards at
|
|
36
|
-
* RECORD_ONLY_THRESHOLD (learn/promote.ts: it hands back on nothing it reads), so its row says nothing of a hand-back
|
|
37
|
-
* at the serving threshold: it is counted apart (`replays`) and in none of the others. */
|
|
38
|
-
export type LoopGuard = {
|
|
39
|
-
workflow: string;
|
|
40
|
-
digest: string;
|
|
41
|
-
node: string;
|
|
42
|
-
signal: GuardRow["signal"];
|
|
43
|
-
threshold: number | null;
|
|
44
|
-
runs: number;
|
|
45
|
-
escalated: number;
|
|
46
|
-
right: number;
|
|
47
|
-
wrong: number;
|
|
48
|
-
/** Runs it answered alone whose grade, if one was given, is no longer kept (the agent keeps its newest GRADES_KEPT
|
|
49
|
-
* grades): in neither `right` nor `wrong`, never read as "not graded". */
|
|
50
|
-
grades_past: number;
|
|
51
|
-
replays: number;
|
|
52
|
-
points: LoopPoint[];
|
|
53
|
-
};
|
|
54
|
-
/** The agent's compile turns (every round of each run, roundsOf: newest first, LOOP_COMPILES_MAX) and its learned
|
|
55
|
-
* workflows' guarded steps, over its ended runs (not imported, not archived). A compile's `promoted` is its revision's
|
|
56
|
-
* promotion as the records say now (null while undecided): whether it serves now is the promotions read's `serving`. A step's `correct` is the run's grade only on a run its workflow answered alone (path `workflow`): a
|
|
57
|
-
* hand-back's answer was the agent's, and a replay run is never graded. */
|
|
20
|
+
/** The agent's compile turns (every round of each run, roundsOf: newest first, LOOP_COMPILES_MAX), over its ended runs
|
|
21
|
+
* (not imported, not archived), and how many completed runs of its own count toward its compile. A compile's `promoted`
|
|
22
|
+
* is its revision's promotion as the records say now (null while undecided): whether it serves now is the promotions
|
|
23
|
+
* read's `serving`. */
|
|
58
24
|
export declare function agentLoopOverStore(store: Store, agentId: string): Promise<{
|
|
59
25
|
compiles: LoopCompile[];
|
|
60
|
-
guards: LoopGuard[];
|
|
61
26
|
completed_runs: number;
|
|
62
27
|
}>;
|
package/dist/store-reads/loop.js
CHANGED
|
@@ -1,53 +1,19 @@
|
|
|
1
1
|
// GET /agents/{id}/learning/loop from the one store: what the console's Learning tab tells of the self-learning loop
|
|
2
2
|
// beside the cost series. Each compile turn the agent's runs took (learn/compile.ts: the workflow it wrote, the notes it
|
|
3
|
-
// absorbed, each attempt and what the checks refused, the draft's digest and whether that revision serves)
|
|
4
|
-
//
|
|
5
|
-
// how often it handed the run back, and each run's p, with the run's grade when the workflow's own answer was graded.
|
|
6
|
-
// Aggregates and the checks' own words only: no input, no answer.
|
|
3
|
+
// absorbed, each attempt and what the checks refused, the draft's digest and whether that revision serves). The checks'
|
|
4
|
+
// own words only: no input, no answer.
|
|
7
5
|
import { FINAL_STATES } from "../store/store.js";
|
|
8
|
-
import {
|
|
6
|
+
import { promotionOf, promotionsOf } from "../learn/promote.js";
|
|
9
7
|
import { lastOwnRuns, promotedOf } from "../learn/compile.js";
|
|
10
8
|
import { loopOf } from "../learning-setting.js";
|
|
11
9
|
import { roundsOf } from "./learning.js";
|
|
12
10
|
/** How many compile turns the read gives, the newest. */
|
|
13
11
|
export const LOOP_COMPILES_MAX = 50;
|
|
14
|
-
/** How many runs' p a guarded step gives, its newest. */
|
|
15
|
-
export const LOOP_POINTS_MAX = 200;
|
|
16
12
|
const texts = (list) => (Array.isArray(list) ? list.filter((one) => typeof one === "string") : []);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
function guardRowsOf(metrics, workflow) {
|
|
22
|
-
const rows = metrics?.guards;
|
|
23
|
-
if (!Array.isArray(rows))
|
|
24
|
-
return [];
|
|
25
|
-
return rows.filter((row) => {
|
|
26
|
-
const r = row;
|
|
27
|
-
return !!r && r.workflow === workflow && typeof r.node === "string" && (r.signal === "self" || r.signal === "jev" || r.signal === "both") && typeof r.threshold === "number" && typeof r.escalated === "boolean" && (r.p === null || typeof r.p === "number");
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
/** A run's checks of each step, one outcome per step (a step in a map checks once per item): handed back when any check
|
|
31
|
-
* did, its p the least one read (null when one read none), its threshold the last check's; in the order each step first
|
|
32
|
-
* checked. */
|
|
33
|
-
function perStep(rows) {
|
|
34
|
-
const steps = new Map();
|
|
35
|
-
for (const row of rows) {
|
|
36
|
-
const was = steps.get(row.node);
|
|
37
|
-
if (!was) {
|
|
38
|
-
steps.set(row.node, { node: row.node, signal: row.signal, threshold: row.threshold, p: row.p, escalated: row.escalated });
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
was.threshold = row.threshold;
|
|
42
|
-
was.escalated ||= row.escalated;
|
|
43
|
-
was.p = was.p === null || row.p === null ? null : Math.min(was.p, row.p);
|
|
44
|
-
}
|
|
45
|
-
return [...steps.values()];
|
|
46
|
-
}
|
|
47
|
-
/** The agent's compile turns (every round of each run, roundsOf: newest first, LOOP_COMPILES_MAX) and its learned
|
|
48
|
-
* workflows' guarded steps, over its ended runs (not imported, not archived). A compile's `promoted` is its revision's
|
|
49
|
-
* promotion as the records say now (null while undecided): whether it serves now is the promotions read's `serving`. A step's `correct` is the run's grade only on a run its workflow answered alone (path `workflow`): a
|
|
50
|
-
* hand-back's answer was the agent's, and a replay run is never graded. */
|
|
13
|
+
/** The agent's compile turns (every round of each run, roundsOf: newest first, LOOP_COMPILES_MAX), over its ended runs
|
|
14
|
+
* (not imported, not archived), and how many completed runs of its own count toward its compile. A compile's `promoted`
|
|
15
|
+
* is its revision's promotion as the records say now (null while undecided): whether it serves now is the promotions
|
|
16
|
+
* read's `serving`. */
|
|
51
17
|
export async function agentLoopOverStore(store, agentId) {
|
|
52
18
|
const ended = (await store.runs.list({ agentId })).filter((run) => run.agentId === agentId && FINAL_STATES.includes(run.state) && !run.importedFrom && run.archivedAt === null).sort((a, b) => a.startedAt - b.startedAt || (a.id < b.id ? -1 : 1));
|
|
53
19
|
// The completed runs of the agent's own toward its compile (what compileDue counts), at most compile_after: the tab
|
|
@@ -55,43 +21,9 @@ export async function agentLoopOverStore(store, agentId) {
|
|
|
55
21
|
const current = (await store.agents.get(agentId))?.currentRevision;
|
|
56
22
|
const body = current ? (await store.agents.revision(current))?.body : undefined;
|
|
57
23
|
const completed_runs = (await lastOwnRuns(store, agentId, loopOf(body?.learning).compile_after)).length;
|
|
58
|
-
const
|
|
59
|
-
const graded = new Map(grades.map((grade) => [grade.run, grade.correct]));
|
|
60
|
-
// The grades kept are the newest GRADES_KEPT (putGrade trims the oldest): once full, a run that ended before the oldest
|
|
61
|
-
// grade kept and has none kept may have had one trimmed, so its grade is unknown. A run that ended after it with none
|
|
62
|
-
// kept was never graded (a grade of it would be newer than one kept). Not full: nothing was trimmed.
|
|
63
|
-
const kept = grades.map((grade) => Date.parse(grade.at)).filter(Number.isFinite);
|
|
64
|
-
const keptSince = grades.length >= GRADES_KEPT ? (kept.length ? Math.min(...kept) : Infinity) : -Infinity;
|
|
24
|
+
const promotions = await promotionsOf(store.settings, agentId).catch(() => []);
|
|
65
25
|
const compiles = [];
|
|
66
|
-
const steps = new Map();
|
|
67
26
|
for (const run of ended) {
|
|
68
|
-
const metrics = run.metrics;
|
|
69
|
-
const workflow = typeof metrics?.workflow === "string" ? metrics.workflow : null;
|
|
70
|
-
const digest = typeof metrics?.workflow_digest === "string" ? metrics.workflow_digest : null;
|
|
71
|
-
if (workflow && digest) {
|
|
72
|
-
const alone = metrics?.path === "workflow";
|
|
73
|
-
for (const row of perStep(guardRowsOf(run.metrics, workflow))) {
|
|
74
|
-
const key = `${workflow}\u0000${digest}\u0000${row.node}`;
|
|
75
|
-
const step = steps.get(key) ?? { workflow, digest, node: row.node, signal: row.signal, threshold: null, runs: 0, escalated: 0, right: 0, wrong: 0, grades_past: 0, replays: 0, points: [] };
|
|
76
|
-
steps.set(key, step);
|
|
77
|
-
if (metrics?.path === "replay") {
|
|
78
|
-
step.replays += 1;
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
const correct = alone ? (graded.get(run.id) ?? null) : null;
|
|
82
|
-
step.threshold = row.threshold;
|
|
83
|
-
step.runs += 1;
|
|
84
|
-
if (row.escalated)
|
|
85
|
-
step.escalated += 1;
|
|
86
|
-
if (correct === true)
|
|
87
|
-
step.right += 1;
|
|
88
|
-
if (correct === false)
|
|
89
|
-
step.wrong += 1;
|
|
90
|
-
if (alone && correct === null && (run.endedAt ?? run.startedAt) < keptSince)
|
|
91
|
-
step.grades_past += 1;
|
|
92
|
-
step.points.push({ run_id: run.id, p: row.p === null ? null : round4(row.p), escalated: row.escalated, correct });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
27
|
for (const kept of await roundsOf(store, run.id).catch(() => [])) {
|
|
96
28
|
const row = (kept.proposal ?? null);
|
|
97
29
|
if (!row)
|
|
@@ -106,16 +38,15 @@ export async function agentLoopOverStore(store, agentId) {
|
|
|
106
38
|
// A draft its check had nothing to run on stopped (runner/promotion.ts stopUnchecked): its record's reason is one
|
|
107
39
|
// of the draft's errors, never silent.
|
|
108
40
|
const own = named && kept16 ? promotionOf(promotions, named.workflow, kept16) : undefined;
|
|
109
|
-
const unchecked = own?.decided === "hold" && own.
|
|
41
|
+
const unchecked = own?.decided === "hold" && own.check?.stopped && own.check.n === 0 ? [own.reason] : [];
|
|
110
42
|
compiles.push({
|
|
111
43
|
createdAt: kept.createdAt, run_id: run.id, round: kept.round, at: new Date(kept.createdAt).toISOString(), workflow: named ? named.workflow : null, from_notes: texts(named?.from_notes), attempts,
|
|
112
44
|
digest: kept16, promoted: named && kept16 ? (promotedOf(promotions, named.workflow, kept16) ?? null) : null, draft_errors: [...errors, ...unchecked],
|
|
113
45
|
});
|
|
114
46
|
}
|
|
115
47
|
}
|
|
116
|
-
const guards = [...steps.values()].map((step) => ({ ...step, points: step.points.slice(-LOOP_POINTS_MAX) }));
|
|
117
48
|
// Newest first by when each round kept it (a later round of an earlier run can be newer than a later run's), the run's
|
|
118
49
|
// start and its round breaking a tie.
|
|
119
50
|
const newest = compiles.map((one, i) => ({ one, i })).sort((a, b) => b.one.createdAt - a.one.createdAt || b.i - a.i).slice(0, LOOP_COMPILES_MAX);
|
|
120
|
-
return { compiles: newest.map(({ one: { createdAt: _createdAt, ...one } }) => one),
|
|
51
|
+
return { compiles: newest.map(({ one: { createdAt: _createdAt, ...one } }) => one), completed_runs };
|
|
121
52
|
}
|