@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
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { type Bill, type CostRun, type CostSummary, type NotServed, type RevisionSpend, type RunPath } from "../learning-cost.js";
|
|
2
|
+
/** A run's own cost as its row stores it: `known`, an exact figure; `unknown`, none; `at_most`, only a bound (a request
|
|
3
|
+
* cut at its cap hold), never shown as a cost. */
|
|
4
|
+
export declare const COST_STATES: readonly ["known", "unknown", "at_most"];
|
|
5
|
+
export type CostState = (typeof COST_STATES)[number];
|
|
6
|
+
/** One ended run: who answered it (`path`, null when its end named none), the learned workflow revision that ran it
|
|
7
|
+
* (`workflow` at `digest`; for an audit's shadow, the revision it audits), why that workflow did not serve it alone,
|
|
8
|
+
* the job's own $ (`jobUsd`: what it had cost when its answer was final, its look-back left out) and everything its
|
|
9
|
+
* rounds looked back for (`lookBackUsd`: its compile turns and its follow-up rounds' turns); each null when unknown. */
|
|
10
|
+
export type LedgerRow = {
|
|
11
|
+
readonly runId: string;
|
|
12
|
+
readonly agentId: string;
|
|
13
|
+
readonly startedAt: number;
|
|
14
|
+
readonly path: RunPath | null;
|
|
15
|
+
readonly workflow: string | null;
|
|
16
|
+
readonly digest: string | null;
|
|
17
|
+
readonly notServed: NotServed | null;
|
|
18
|
+
readonly jobUsd: number | null;
|
|
19
|
+
readonly lookBackUsd: number | null;
|
|
20
|
+
readonly cost: CostState;
|
|
21
|
+
};
|
|
22
|
+
/** What a spend row paid for: a compile turn that drafted a revision (`compile`), a replay run checking one (`check`), or
|
|
23
|
+
* an audit's shadow run of a served run (`audit`). */
|
|
24
|
+
export declare const SPEND_KINDS: readonly ["compile", "check", "audit"];
|
|
25
|
+
export type SpendKind = (typeof SPEND_KINDS)[number];
|
|
26
|
+
/** One learning cost: the run whose end booked it (`runId`, with its `round` for a compile turn, 0 otherwise), the run
|
|
27
|
+
* an audit's shadow audited (`ofRun`), the revision it is for (`workflow` at `digest`; a compile that kept no draft drafted
|
|
28
|
+
* none: both null; one whose drafts failed their checks, the workflow alone) and its $ (null: unknown). */
|
|
29
|
+
export type SpendRow = {
|
|
30
|
+
readonly runId: string;
|
|
31
|
+
readonly round: number;
|
|
32
|
+
readonly agentId: string;
|
|
33
|
+
readonly kind: SpendKind;
|
|
34
|
+
readonly ofRun: string | null;
|
|
35
|
+
readonly workflow: string | null;
|
|
36
|
+
readonly digest: string | null;
|
|
37
|
+
readonly usd: number | null;
|
|
38
|
+
};
|
|
39
|
+
/** What record() reads of an ended run: its row (its cost, the bound beside it, its metrics: the run-end classifier's
|
|
40
|
+
* path, workflow and digest, runner/workflow-first.ts pathMetrics) and every round its learning kept (learning_proposals). */
|
|
41
|
+
export type RunEnd = {
|
|
42
|
+
readonly id: string;
|
|
43
|
+
readonly agentId: string;
|
|
44
|
+
readonly startedAt: number;
|
|
45
|
+
readonly costUsd: number | null;
|
|
46
|
+
readonly costAtMostUsd: number | null;
|
|
47
|
+
readonly metrics: unknown;
|
|
48
|
+
readonly rounds: readonly {
|
|
49
|
+
readonly round: number;
|
|
50
|
+
readonly proposal: unknown;
|
|
51
|
+
}[];
|
|
52
|
+
};
|
|
53
|
+
/** What a kept round says of its job as it answered (runner/reflect-hook.ts): when, what the job had cost by then, and
|
|
54
|
+
* what looking back cost after it. */
|
|
55
|
+
export type Answered = {
|
|
56
|
+
readonly answered_at: string;
|
|
57
|
+
readonly job_cost_usd: number | null;
|
|
58
|
+
readonly look_back_usd: number | null;
|
|
59
|
+
};
|
|
60
|
+
/** A kept round's Answered; undefined for one that never answered. */
|
|
61
|
+
export declare const answeredOf: (proposal: unknown) => Answered | undefined;
|
|
62
|
+
/** A run a follow-up opened again: its job is what it had cost when its first round answered, and everything its row
|
|
63
|
+
* (`rowUsd`) spent after that (each later round's follow-up turn and every round's look-back: feedback the run was given
|
|
64
|
+
* is learning, never the job) is its look-back; unknown when the row's cost is and the job's is known (a later round
|
|
65
|
+
* stopped mid-request keeps no figures, so the rounds' sum would read short as if settled); the rounds' look-backs added
|
|
66
|
+
* up when the job's cost is unknown. The impact read (store-reads/learning.ts impactLearningOf) takes the same. */
|
|
67
|
+
export declare function roundsLooked(rounds: Answered, rowUsd: number | null): Answered;
|
|
68
|
+
/** Over a run's kept rounds, oldest first: with two or more and one that answered, what they kept together (its first
|
|
69
|
+
* answer's job, every round's look-back added up, null once one is unknown); else undefined. */
|
|
70
|
+
export declare function roundsSpentIn(proposals: readonly unknown[]): Answered | undefined;
|
|
71
|
+
/** What a run's rounds kept as it answered, as its cost reads it: over every round (roundsLooked) for one a follow-up
|
|
72
|
+
* opened again, else its one round's; undefined for one that never answered. */
|
|
73
|
+
export declare function lookedOf(proposals: readonly unknown[], rowUsd: number | null): Answered | undefined;
|
|
74
|
+
/** The ledger row and spend rows a run's end writes, from its row and its kept rounds (RunEnd). The job's own $ is its
|
|
75
|
+
* row's cost less every look-back, never under what it had cost when it answered (a later turn's spend is the job's
|
|
76
|
+
* too); its row's cost for a run that did not look back. Its compile turns are spend rows of the revision each drafted.
|
|
77
|
+
* A replay run's cost is its revision's check, and an audit's shadow run's the run it audits: a spend row each, beside
|
|
78
|
+
* its own ledger row. */
|
|
79
|
+
export declare function record(end: RunEnd): {
|
|
80
|
+
row: LedgerRow;
|
|
81
|
+
spend: SpendRow[];
|
|
82
|
+
};
|
|
83
|
+
/** The audit spend row of shadow run `shadow` of run `ofRun`, from its row alone: for a shadow whose end named no audit
|
|
84
|
+
* (it failed before its end wrote its metrics), found by its id (learn/audit.ts shadowRunId). Its cost is the row's,
|
|
85
|
+
* null when unknown or only a bound. */
|
|
86
|
+
export declare function shadowSpend(shadow: Pick<RunEnd, "id" | "agentId" | "costUsd" | "costAtMostUsd">, ofRun: string): SpendRow;
|
|
87
|
+
/** An audit's shadow run of a run, as the reader finds it: `open` while it is queued or running (its cost not known yet),
|
|
88
|
+
* `ended` once it ended, stopped or failed. A run with none has no entry. */
|
|
89
|
+
export type Shadow = "open" | "ended";
|
|
90
|
+
/** A run's figures, every one null when unknown, never $0 for unknown:
|
|
91
|
+
* - `usd`: the job's own $ (GET /runs/{id} `cost.usd`, the series' `cost_usd`);
|
|
92
|
+
* - `compile_usd`, `followup_usd`, `shadow_usd`: its own compile turns, the rest of its look-back (its follow-up rounds'
|
|
93
|
+
* turns) and its audit's shadow run;
|
|
94
|
+
* - `share_usd`: its share of its revision's compile and check (amortize), 0 for a run that carries none;
|
|
95
|
+
* - `series_learning_usd` and `series_total_usd`: the cost series' `learning_usd` (its whole look-back and its shadow)
|
|
96
|
+
* and `total_usd`, the all-in both sides of the money rule read;
|
|
97
|
+
* - `learning_usd` and `all_in_usd`: GET /runs/{id}'s (its follow-ups, its shadow and its share: its compile turns are
|
|
98
|
+
* its revision's), 0 for a replay or a shadow run, whose spend is on the runs it serves. */
|
|
99
|
+
export type RunFigures = {
|
|
100
|
+
readonly usd: number | null;
|
|
101
|
+
readonly compile_usd: number | null;
|
|
102
|
+
readonly followup_usd: number | null;
|
|
103
|
+
readonly shadow_usd: number | null;
|
|
104
|
+
readonly share_usd: number | null;
|
|
105
|
+
readonly series_learning_usd: number | null;
|
|
106
|
+
readonly series_total_usd: number | null;
|
|
107
|
+
readonly learning_usd: number | null;
|
|
108
|
+
readonly all_in_usd: number | null;
|
|
109
|
+
};
|
|
110
|
+
/** One run's figures (RunFigures) from its row and the parts the spend rows give it (its compile turns, its shadow's
|
|
111
|
+
* cost, its share): the one formula every reader of a run's cost takes. */
|
|
112
|
+
export declare function figuresOf(row: Pick<LedgerRow, "path" | "jobUsd" | "lookBackUsd">, parts: {
|
|
113
|
+
readonly compile: number | null;
|
|
114
|
+
readonly shadow: number | null;
|
|
115
|
+
readonly share: number | null;
|
|
116
|
+
}): RunFigures;
|
|
117
|
+
/** The spend rows read by run: each run's compile turns (null once one is unknown) and each audited run's shadow cost. */
|
|
118
|
+
export declare function spendByRun(spend: readonly SpendRow[]): {
|
|
119
|
+
compile: Map<string, number | null>;
|
|
120
|
+
audit: Map<string, number | null>;
|
|
121
|
+
};
|
|
122
|
+
/** A run's shadow cost: 0 with none; unknown while it is at work, or once it ended with no spend row; its spend row's
|
|
123
|
+
* once it has one. */
|
|
124
|
+
export declare function shadowCostOf(runId: string, audit: ReadonlyMap<string, number | null>, shadows: ReadonlyMap<string, Shadow>): number | null;
|
|
125
|
+
/** A revision of a learned workflow as its records say it now (store-reads/learning.ts revisionsOf): the served runs its
|
|
126
|
+
* spend is spread over (`k`, its check's compile_after) and where it stands. Order is the records'. */
|
|
127
|
+
export type RevisionAt = {
|
|
128
|
+
readonly workflow: string;
|
|
129
|
+
readonly digest: string;
|
|
130
|
+
readonly k: number;
|
|
131
|
+
readonly state: RevisionSpend["state"];
|
|
132
|
+
};
|
|
133
|
+
/** What summarize reads:
|
|
134
|
+
* - `rows`, the runs of the series (ended, neither imported nor archived nor purged, no workflow revision's run, no
|
|
135
|
+
* shadow), any order, each with its path (null: its end named none);
|
|
136
|
+
* - `spend`, the agent's spend rows: a compile or a check of a run `rows` leaves out is left out as its run is, an
|
|
137
|
+
* audit's by the run it audits;
|
|
138
|
+
* - `shadows`, each run's audit shadow by the run it audits;
|
|
139
|
+
* - `learning`, the runs of `rows` whose spend and service make up a revision's (on no named disk: one on a named disk
|
|
140
|
+
* learns nothing and serves nothing, learn/learning-of.ts); absent, every one;
|
|
141
|
+
* - `revisions`, as the records say them; `compileAfter`, the K of a revision no record names;
|
|
142
|
+
* - `named`, the workflow name a run's series row shows (its row's when absent).
|
|
143
|
+
* The summary's `replay` is the records' (store-reads/learning.ts servingReplayOf), null here. */
|
|
144
|
+
export type LedgerRead = {
|
|
145
|
+
readonly rows: readonly LedgerRow[];
|
|
146
|
+
readonly spend: readonly SpendRow[];
|
|
147
|
+
readonly shadows: ReadonlyMap<string, Shadow>;
|
|
148
|
+
readonly learning?: ReadonlySet<string>;
|
|
149
|
+
readonly revisions: readonly RevisionAt[];
|
|
150
|
+
readonly compileAfter: number;
|
|
151
|
+
readonly named?: (row: LedgerRow) => string | null;
|
|
152
|
+
};
|
|
153
|
+
export type Summarized = {
|
|
154
|
+
runs: CostRun[];
|
|
155
|
+
summary: CostSummary;
|
|
156
|
+
bill: Omit<Bill, "holds">;
|
|
157
|
+
figures: Map<string, RunFigures>;
|
|
158
|
+
unknown_path_runs: number;
|
|
159
|
+
};
|
|
160
|
+
/** What the ledger says over an agent's series: each run of it (`runs`, oldest first, every one), what they show
|
|
161
|
+
* (`summary`), the owner's bill (`bill`, its holds aside: they are the records'), each run's figures by id (`figures`)
|
|
162
|
+
* and the rows whose end named no path (`unknown_path_runs`, left out of all of it). Every sum is over every run.
|
|
163
|
+
*
|
|
164
|
+
* A revision is its workflow and its digest, never the digest alone: two workflows of the same text share a digest, and
|
|
165
|
+
* each pays its own way. Its spend is its compile turns and its check's replay runs (spend rows of its workflow at its
|
|
166
|
+
* digest), whether or not a record names it or the records were trimmed: one no record names follows the ones that do,
|
|
167
|
+
* at `compileAfter`, `checking` while it is its workflow's newest spend, else `stopped`. It is spread over its first K
|
|
168
|
+
* served runs (amortize). What no run carries is the booked total less every run's all-in, unknown while any run's
|
|
169
|
+
* figures are. */
|
|
170
|
+
export declare function summarize(read: LedgerRead): Summarized;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// The ledger: one row a run, written by its end, and one spend row a learning cost (a compile turn, a check's replay run,
|
|
2
|
+
// an audit's shadow run), keyed to the workflow revision it is for. record() is what a run's end writes, in the end's own
|
|
3
|
+
// transaction (store/sqlite/run-patch.ts); summarize() is the one reading of them, which GET /agents/{id}/learning/cost,
|
|
4
|
+
// GET /runs/{id}'s cost and the money rule (runner/workflow-first.ts costLineOf, over RunFigures) all take. Pure: no
|
|
5
|
+
// store, no clock.
|
|
6
|
+
import { amortize, billOf, costSummary, JOB_PATHS, NOT_SERVED, noiseOff, RUN_PATHS, SERVED_PATHS, tookOverOf } from "../learning-cost.js";
|
|
7
|
+
/** A run's own cost as its row stores it: `known`, an exact figure; `unknown`, none; `at_most`, only a bound (a request
|
|
8
|
+
* cut at its cap hold), never shown as a cost. */
|
|
9
|
+
export const COST_STATES = ["known", "unknown", "at_most"];
|
|
10
|
+
/** What a spend row paid for: a compile turn that drafted a revision (`compile`), a replay run checking one (`check`), or
|
|
11
|
+
* an audit's shadow run of a served run (`audit`). */
|
|
12
|
+
export const SPEND_KINDS = ["compile", "check", "audit"];
|
|
13
|
+
const usdOrNull = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
14
|
+
const textOrNull = (value) => (typeof value === "string" ? value : null);
|
|
15
|
+
const objectOf = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
|
|
16
|
+
/** A sum of parts, null once one is unknown; 0 for none. */
|
|
17
|
+
const sumOf = (parts) => (parts.some((one) => one === null) ? null : noiseOff(parts.reduce((total, one) => total + one, 0)));
|
|
18
|
+
/** A kept round's Answered; undefined for one that never answered. */
|
|
19
|
+
export const answeredOf = (proposal) => {
|
|
20
|
+
const kept = objectOf(proposal);
|
|
21
|
+
return typeof kept.answered_at === "string" ? { answered_at: kept.answered_at, job_cost_usd: usdOrNull(kept.job_cost_usd), look_back_usd: usdOrNull(kept.look_back_usd) } : undefined;
|
|
22
|
+
};
|
|
23
|
+
/** A run a follow-up opened again: its job is what it had cost when its first round answered, and everything its row
|
|
24
|
+
* (`rowUsd`) spent after that (each later round's follow-up turn and every round's look-back: feedback the run was given
|
|
25
|
+
* is learning, never the job) is its look-back; unknown when the row's cost is and the job's is known (a later round
|
|
26
|
+
* stopped mid-request keeps no figures, so the rounds' sum would read short as if settled); the rounds' look-backs added
|
|
27
|
+
* up when the job's cost is unknown. The impact read (store-reads/learning.ts impactLearningOf) takes the same. */
|
|
28
|
+
export function roundsLooked(rounds, rowUsd) {
|
|
29
|
+
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);
|
|
30
|
+
return { answered_at: rounds.answered_at, job_cost_usd: rounds.job_cost_usd, look_back_usd: after };
|
|
31
|
+
}
|
|
32
|
+
/** Over a run's kept rounds, oldest first: with two or more and one that answered, what they kept together (its first
|
|
33
|
+
* answer's job, every round's look-back added up, null once one is unknown); else undefined. */
|
|
34
|
+
export function roundsSpentIn(proposals) {
|
|
35
|
+
if (proposals.length < 2)
|
|
36
|
+
return undefined;
|
|
37
|
+
let first;
|
|
38
|
+
let look = 0;
|
|
39
|
+
for (const proposal of proposals) {
|
|
40
|
+
const answered = answeredOf(proposal);
|
|
41
|
+
if (!first && answered)
|
|
42
|
+
first = answered;
|
|
43
|
+
// A round that never looked back (no figures kept) added none.
|
|
44
|
+
if (answered)
|
|
45
|
+
look = look === null || answered.look_back_usd === null ? null : look + answered.look_back_usd;
|
|
46
|
+
}
|
|
47
|
+
return first ? { answered_at: first.answered_at, job_cost_usd: first.job_cost_usd, look_back_usd: look } : undefined;
|
|
48
|
+
}
|
|
49
|
+
/** What a run's rounds kept as it answered, as its cost reads it: over every round (roundsLooked) for one a follow-up
|
|
50
|
+
* opened again, else its one round's; undefined for one that never answered. */
|
|
51
|
+
export function lookedOf(proposals, rowUsd) {
|
|
52
|
+
const together = roundsSpentIn(proposals);
|
|
53
|
+
return together ? roundsLooked(together, rowUsd) : proposals.length ? answeredOf(proposals.at(-1)) : undefined;
|
|
54
|
+
}
|
|
55
|
+
function compilesIn(rounds) {
|
|
56
|
+
return rounds.flatMap(({ round, proposal }) => {
|
|
57
|
+
const kept = objectOf(proposal);
|
|
58
|
+
const usd = "look_back_usd" in kept ? usdOrNull(kept.look_back_usd) : 0;
|
|
59
|
+
const compiled = objectOf(kept.compiled);
|
|
60
|
+
if (typeof compiled.workflow !== "string")
|
|
61
|
+
return usd === 0 ? [] : [{ round, workflow: null, digest: null, usd }];
|
|
62
|
+
const failed = Array.isArray(kept.draft_errors) && kept.draft_errors.some((one) => typeof one === "string");
|
|
63
|
+
const digest = typeof compiled.digest === "string" && /^[0-9a-f]{16}$/.test(compiled.digest) && !failed ? compiled.digest : null;
|
|
64
|
+
return [{ round, workflow: compiled.workflow, digest, usd }];
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** The ledger row and spend rows a run's end writes, from its row and its kept rounds (RunEnd). The job's own $ is its
|
|
68
|
+
* row's cost less every look-back, never under what it had cost when it answered (a later turn's spend is the job's
|
|
69
|
+
* too); its row's cost for a run that did not look back. Its compile turns are spend rows of the revision each drafted.
|
|
70
|
+
* A replay run's cost is its revision's check, and an audit's shadow run's the run it audits: a spend row each, beside
|
|
71
|
+
* its own ledger row. */
|
|
72
|
+
export function record(end) {
|
|
73
|
+
const metrics = objectOf(end.metrics);
|
|
74
|
+
const cost = typeof end.costAtMostUsd === "number" ? "at_most" : end.costUsd === null ? "unknown" : "known";
|
|
75
|
+
const rowUsd = cost === "known" ? end.costUsd : null;
|
|
76
|
+
const rounds = [...end.rounds].sort((a, b) => a.round - b.round);
|
|
77
|
+
const looked = lookedOf(rounds.map((one) => one.proposal), rowUsd);
|
|
78
|
+
const asAnswered = looked ? looked.job_cost_usd : rowUsd;
|
|
79
|
+
const back = looked ? looked.look_back_usd : 0;
|
|
80
|
+
// What the row holds past the answer and its look-back is a later turn's spend, unknown once the row's cost is; with the
|
|
81
|
+
// look-back's cost unknown the two can't be told apart, and the job is what it had cost when it answered.
|
|
82
|
+
const job = !looked || asAnswered === null || back === null ? asAnswered : rowUsd === null ? null : Math.max(asAnswered, rowUsd - back);
|
|
83
|
+
const path = RUN_PATHS.includes(metrics.path) ? metrics.path : null;
|
|
84
|
+
const audit = objectOf(metrics.audit);
|
|
85
|
+
const audited = path === "audit";
|
|
86
|
+
const workflow = textOrNull(audited ? audit.workflow : metrics.workflow);
|
|
87
|
+
const digest = textOrNull(audited ? audit.digest : metrics.workflow_digest);
|
|
88
|
+
// A hand-back a restart cut (pathMetrics workflow_cut) is `cut`, never the workflow's own `back`.
|
|
89
|
+
const cut = path === "handed_back" && metrics.workflow_cut === true;
|
|
90
|
+
const notServed = cut ? "cut" : NOT_SERVED.includes(metrics.not_served) ? metrics.not_served : null;
|
|
91
|
+
const row = {
|
|
92
|
+
runId: end.id, agentId: end.agentId, startedAt: end.startedAt, path, workflow, digest, notServed,
|
|
93
|
+
jobUsd: job === null ? null : noiseOff(job), lookBackUsd: back === null ? null : noiseOff(back), cost,
|
|
94
|
+
};
|
|
95
|
+
const base = { runId: end.id, agentId: end.agentId };
|
|
96
|
+
const spend = compilesIn(rounds).map((one) => ({ ...base, round: one.round, kind: "compile", ofRun: null, workflow: one.workflow, digest: one.digest, usd: one.usd }));
|
|
97
|
+
if (path === "replay")
|
|
98
|
+
spend.push({ ...base, round: 0, kind: "check", ofRun: null, workflow, digest, usd: rowUsd });
|
|
99
|
+
if (audited)
|
|
100
|
+
spend.push({ ...base, round: 0, kind: "audit", ofRun: textOrNull(audit.of), workflow, digest, usd: rowUsd });
|
|
101
|
+
return { row, spend };
|
|
102
|
+
}
|
|
103
|
+
/** The audit spend row of shadow run `shadow` of run `ofRun`, from its row alone: for a shadow whose end named no audit
|
|
104
|
+
* (it failed before its end wrote its metrics), found by its id (learn/audit.ts shadowRunId). Its cost is the row's,
|
|
105
|
+
* null when unknown or only a bound. */
|
|
106
|
+
export function shadowSpend(shadow, ofRun) {
|
|
107
|
+
const usd = typeof shadow.costAtMostUsd === "number" ? null : shadow.costUsd;
|
|
108
|
+
return { runId: shadow.id, agentId: shadow.agentId, round: 0, kind: "audit", ofRun, workflow: null, digest: null, usd };
|
|
109
|
+
}
|
|
110
|
+
/** One run's figures (RunFigures) from its row and the parts the spend rows give it (its compile turns, its shadow's
|
|
111
|
+
* cost, its share): the one formula every reader of a run's cost takes. */
|
|
112
|
+
export function figuresOf(row, parts) {
|
|
113
|
+
const { compile, shadow, share } = parts;
|
|
114
|
+
const back = row.lookBackUsd;
|
|
115
|
+
const followup = back === null || compile === null ? null : noiseOff(Math.max(0, back - compile));
|
|
116
|
+
const seriesLearning = sumOf([back, shadow]);
|
|
117
|
+
const learns = row.path !== "replay" && row.path !== "audit";
|
|
118
|
+
const learning = learns ? sumOf([followup, shadow, share]) : 0;
|
|
119
|
+
return {
|
|
120
|
+
usd: row.jobUsd, compile_usd: compile, followup_usd: followup, shadow_usd: shadow, share_usd: learns ? share : 0,
|
|
121
|
+
series_learning_usd: seriesLearning, series_total_usd: sumOf([row.jobUsd, seriesLearning]),
|
|
122
|
+
learning_usd: learning, all_in_usd: learns ? sumOf([row.jobUsd, learning]) : 0,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** The spend rows read by run: each run's compile turns (null once one is unknown) and each audited run's shadow cost. */
|
|
126
|
+
export function spendByRun(spend) {
|
|
127
|
+
const compile = new Map();
|
|
128
|
+
const audit = new Map();
|
|
129
|
+
const add = (map, key, usd) => map.set(key, map.has(key) ? sumOf([map.get(key), usd]) : usd);
|
|
130
|
+
for (const one of spend) {
|
|
131
|
+
if (one.kind === "compile")
|
|
132
|
+
add(compile, one.runId, one.usd);
|
|
133
|
+
else if (one.kind === "audit" && one.ofRun !== null)
|
|
134
|
+
add(audit, one.ofRun, one.usd);
|
|
135
|
+
}
|
|
136
|
+
return { compile, audit };
|
|
137
|
+
}
|
|
138
|
+
/** A run's shadow cost: 0 with none; unknown while it is at work, or once it ended with no spend row; its spend row's
|
|
139
|
+
* once it has one. */
|
|
140
|
+
export function shadowCostOf(runId, audit, shadows) {
|
|
141
|
+
const state = shadows.get(runId);
|
|
142
|
+
if (state === "open")
|
|
143
|
+
return null;
|
|
144
|
+
if (audit.has(runId))
|
|
145
|
+
return audit.get(runId);
|
|
146
|
+
return state === "ended" ? null : 0;
|
|
147
|
+
}
|
|
148
|
+
/** What the ledger says over an agent's series: each run of it (`runs`, oldest first, every one), what they show
|
|
149
|
+
* (`summary`), the owner's bill (`bill`, its holds aside: they are the records'), each run's figures by id (`figures`)
|
|
150
|
+
* and the rows whose end named no path (`unknown_path_runs`, left out of all of it). Every sum is over every run.
|
|
151
|
+
*
|
|
152
|
+
* A revision is its workflow and its digest, never the digest alone: two workflows of the same text share a digest, and
|
|
153
|
+
* each pays its own way. Its spend is its compile turns and its check's replay runs (spend rows of its workflow at its
|
|
154
|
+
* digest), whether or not a record names it or the records were trimmed: one no record names follows the ones that do,
|
|
155
|
+
* at `compileAfter`, `checking` while it is its workflow's newest spend, else `stopped`. It is spread over its first K
|
|
156
|
+
* served runs (amortize). What no run carries is the booked total less every run's all-in, unknown while any run's
|
|
157
|
+
* figures are. */
|
|
158
|
+
export function summarize(read) {
|
|
159
|
+
const ordered = read.rows.filter((row) => row.path !== null && row.path !== "audit").sort((a, b) => a.startedAt - b.startedAt || (a.runId < b.runId ? -1 : a.runId > b.runId ? 1 : 0));
|
|
160
|
+
const unknownPath = read.rows.filter((row) => row.path === null).length;
|
|
161
|
+
const place = new Map(ordered.map((row, at) => [row.runId, at]));
|
|
162
|
+
const learns = (runId) => place.has(runId) && (read.learning?.has(runId) ?? true);
|
|
163
|
+
const spend = read.spend.filter((one) => (one.kind === "audit" ? place.has(one.ofRun ?? "") : learns(one.runId)));
|
|
164
|
+
const { compile, audit } = spendByRun(spend);
|
|
165
|
+
const partsOf = (row, share) => ({ compile: compile.has(row.runId) ? compile.get(row.runId) : 0, shadow: shadowCostOf(row.runId, audit, read.shadows), share });
|
|
166
|
+
// One numbering, the bill's: the runs that do a job, 1 the first; a replay checks a revision and has none.
|
|
167
|
+
let jobs = 0;
|
|
168
|
+
const series = ordered.map((row) => {
|
|
169
|
+
const figures = figuresOf(row, partsOf(row, 0));
|
|
170
|
+
const path = row.path;
|
|
171
|
+
return {
|
|
172
|
+
n: JOB_PATHS.includes(path) ? (jobs += 1) : null, run_id: row.runId, started_at: new Date(row.startedAt).toISOString(), path,
|
|
173
|
+
workflow: path === "agent" ? null : read.named ? read.named(row) : row.workflow,
|
|
174
|
+
digest: path === "agent" || row.workflow === null ? null : row.digest,
|
|
175
|
+
audited: read.shadows.has(row.runId) || audit.has(row.runId),
|
|
176
|
+
cost_usd: figures.usd, cost_upper_bound: row.cost === "at_most", learning_usd: figures.series_learning_usd, total_usd: figures.series_total_usd,
|
|
177
|
+
compile_usd: figures.compile_usd, followup_usd: figures.followup_usd, shadow_usd: figures.shadow_usd, share_usd: 0, not_served: row.notServed,
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
const summary = costSummary(tookOverOf(series), null, new Set(series.flatMap((run) => (run.not_served === "cut" ? [run.run_id] : []))));
|
|
181
|
+
const booked = billOf(tookOverOf(series), summary);
|
|
182
|
+
// Each revision's compile and check, in spend order (the runs' order, then the rounds').
|
|
183
|
+
const key = (workflow, digest) => `${workflow}\0${digest}`;
|
|
184
|
+
const parts = new Map();
|
|
185
|
+
const newest = new Map();
|
|
186
|
+
// An audit's row is the run it audits' (its shadow is no run of the series): it is no revision's spend.
|
|
187
|
+
for (const one of spend.filter((each) => each.kind !== "audit").sort((a, b) => place.get(a.runId) - place.get(b.runId) || a.round - b.round)) {
|
|
188
|
+
if (one.workflow === null || one.digest === null)
|
|
189
|
+
continue;
|
|
190
|
+
const at = key(one.workflow, one.digest);
|
|
191
|
+
const entry = parts.get(at) ?? { workflow: one.workflow, digest: one.digest, compile: [], check: [] };
|
|
192
|
+
(one.kind === "compile" ? entry.compile : entry.check).push(one.usd);
|
|
193
|
+
parts.set(at, entry);
|
|
194
|
+
newest.set(one.workflow, at);
|
|
195
|
+
}
|
|
196
|
+
const spendOf = (workflow, digest, k, state) => {
|
|
197
|
+
const entry = parts.get(key(workflow, digest));
|
|
198
|
+
const compileUsd = sumOf(entry?.compile ?? []);
|
|
199
|
+
const checkUsd = sumOf(entry?.check ?? []);
|
|
200
|
+
return { workflow, digest, compile_usd: compileUsd, check_usd: checkUsd, spend_usd: sumOf([compileUsd, checkUsd]), k, state };
|
|
201
|
+
};
|
|
202
|
+
const named = new Set(read.revisions.map((one) => key(one.workflow, one.digest)));
|
|
203
|
+
const revisions = [
|
|
204
|
+
...read.revisions.map((one) => spendOf(one.workflow, one.digest, one.k, one.state)),
|
|
205
|
+
...[...parts].filter(([at]) => !named.has(at)).map(([at, entry]) => spendOf(entry.workflow, entry.digest, read.compileAfter, newest.get(entry.workflow) === at ? "checking" : "stopped")),
|
|
206
|
+
];
|
|
207
|
+
const served = ordered.flatMap((row) => (learns(row.runId) && SERVED_PATHS.includes(row.path) && row.workflow !== null && row.digest !== null ? [{ run_id: row.runId, workflow: row.workflow, digest: row.digest }] : []));
|
|
208
|
+
const split = amortize(revisions, served);
|
|
209
|
+
const figures = new Map(ordered.map((row) => [row.runId, figuresOf(row, partsOf(row, split.shares.has(row.runId) ? split.shares.get(row.runId) : 0))]));
|
|
210
|
+
const runs = tookOverOf(series.map((run) => ({ ...run, share_usd: figures.get(run.run_id).share_usd })));
|
|
211
|
+
let carried = 0;
|
|
212
|
+
for (const row of ordered) {
|
|
213
|
+
const all = row.path === "replay" ? 0 : row.cost === "at_most" ? null : figures.get(row.runId).all_in_usd;
|
|
214
|
+
carried = carried === null || all === null ? null : noiseOff(carried + all);
|
|
215
|
+
}
|
|
216
|
+
const unattributed = booked.withheld > 0 || carried === null ? null : noiseOff(booked.booked_usd - carried);
|
|
217
|
+
return { runs, summary, bill: { ...booked, revisions: split.revisions, unattributed_usd: unattributed }, figures, unknown_path_runs: unknownPath };
|
|
218
|
+
}
|
package/dist/learn/promote.d.ts
CHANGED
|
@@ -220,7 +220,7 @@ export declare const gradesKey: (agentId: string) => string;
|
|
|
220
220
|
export declare const promotionsKey: (agentId: string) => string;
|
|
221
221
|
/** A learned workflow revision an agent's learning reset deleted (DELETE /agents/{id}/learning, learning-reset.ts): its
|
|
222
222
|
* workflow, its digest and its K (its check's compile_after; null when its records named none), nothing else of its
|
|
223
|
-
* records. What the runs it served carry of its compile and check (store-reads/learning.ts
|
|
223
|
+
* records. What the runs it served carry of its compile and check (learn/ledger.ts summarize, store-reads/learning.ts revisionsOf) is read
|
|
224
224
|
* from it after the reset, so a kept run's cost reads as before. The runs keep the same names and digests. */
|
|
225
225
|
export interface ResetRevision {
|
|
226
226
|
readonly workflow: string;
|
|
@@ -232,7 +232,7 @@ export declare const resetLedgerKey: (agentId: string) => string;
|
|
|
232
232
|
export declare const resetLedgerOf: (settings: Pick<Settings, "get">, agentId: string) => Promise<ResetRevision[]>;
|
|
233
233
|
/** Each revision `records` name (one per workflow and digest, its newest record's K) added to the agent's reset ledger,
|
|
234
234
|
* before a reset deletes the records. A revision it holds already keeps its place and takes the records' K: while they
|
|
235
|
-
* stood, the ledger read their K (
|
|
235
|
+
* stood, the ledger read their K (store-reads/learning.ts revisionsOf), so the runs' shares read the same after this reset too. */
|
|
236
236
|
export declare function keepResetLedger(settings: Pick<Settings, "get" | "putIfAbsent" | "replaceIf">, agentId: string, records: readonly PromotionRecord[]): Promise<void>;
|
|
237
237
|
/** The agent's reset watermark: when its newest reset began (epoch ms). Learning reads only the runs after it
|
|
238
238
|
* (learn/compile.ts sinceReset): a run that started, or read its learning seed, at or before it learns nothing and is
|
|
@@ -427,7 +427,7 @@ export declare function comparedOf(value: unknown): Compared | undefined;
|
|
|
427
427
|
export declare const gradeView: (grade: Grade) => {
|
|
428
428
|
source: "feedback" | "eval";
|
|
429
429
|
graded_at: string;
|
|
430
|
-
path: "agent" | "
|
|
430
|
+
path: "agent" | "handed_back" | "workflow" | "replay" | "audit";
|
|
431
431
|
workflow: string | null;
|
|
432
432
|
workflow_digest: string | null;
|
|
433
433
|
feedback: "correction" | null;
|
package/dist/learn/promote.js
CHANGED
|
@@ -74,7 +74,7 @@ export const resetLedgerKey = (agentId) => `learning.reset-ledger.${agentId}`;
|
|
|
74
74
|
export const resetLedgerOf = async (settings, agentId) => itemsOf(await settings.get(resetLedgerKey(agentId)));
|
|
75
75
|
/** Each revision `records` name (one per workflow and digest, its newest record's K) added to the agent's reset ledger,
|
|
76
76
|
* before a reset deletes the records. A revision it holds already keeps its place and takes the records' K: while they
|
|
77
|
-
* stood, the ledger read their K (
|
|
77
|
+
* stood, the ledger read their K (store-reads/learning.ts revisionsOf), so the runs' shares read the same after this reset too. */
|
|
78
78
|
export async function keepResetLedger(settings, agentId, records) {
|
|
79
79
|
const newest = new Map();
|
|
80
80
|
for (const record of records)
|
package/dist/learning-cost.d.ts
CHANGED
|
@@ -9,30 +9,10 @@
|
|
|
9
9
|
* record at its end (metrics.path). */
|
|
10
10
|
export declare const RUN_PATHS: readonly ["agent", "handed_back", "workflow", "replay", "audit"];
|
|
11
11
|
export type RunPath = (typeof RUN_PATHS)[number];
|
|
12
|
-
/** What a run that looked back kept of its costs as it answered (runner/reflect-hook.ts: store-reads/learning.ts
|
|
13
|
-
* answeredOf): when the job answered, what it had cost by then and what looking back cost after it. */
|
|
14
|
-
export type CostLooked = {
|
|
15
|
-
answered_at?: string;
|
|
16
|
-
job_cost_usd?: number | null;
|
|
17
|
-
look_back_usd?: number | null;
|
|
18
|
-
};
|
|
19
12
|
/** A booked figure with its float noise rounded off, nothing more (runner/metrics.ts floatNoiseOff, the precision a run's
|
|
20
13
|
* own cost is kept at): a decision costs millionths of a dollar, so a figure rounded to the millionth would lose it, and
|
|
21
14
|
* the run's cost here would read other than its usage row (A8). `+ 0` makes a noise-sized negative a plain 0, never -0. */
|
|
22
15
|
export declare const noiseOff: (value: number) => number;
|
|
23
|
-
/** A run's own cost and what learning added for it, from its row's cost (`rowUsd`: the job, its look-back and any turn a
|
|
24
|
-
* person asked of it after) and what it kept as it answered (`looked`, none for a run that did not look back):
|
|
25
|
-
* - `cost_usd`: the job's own $: its row's cost less its look-back, never under what it had cost when it answered (a
|
|
26
|
-
* later turn's spend is the job's too); its row's cost for a run that did not look back;
|
|
27
|
-
* - `learning_usd`: what learning cost for it: its look-back (its compile turn) and what was spent beside it
|
|
28
|
-
* (`besideUsd`: its audit's shadow run), 0 for a run that does not learn; null when unknown (a turn stopped mid-ask
|
|
29
|
-
* spent what no one knows, or a shadow still running);
|
|
30
|
-
* - `total_usd`: their sum, the run's all-in $; null when either is. */
|
|
31
|
-
export declare function runCostOf(rowUsd: number | null, looked: CostLooked | null, besideUsd?: number | null): {
|
|
32
|
-
cost_usd: number | null;
|
|
33
|
-
learning_usd: number | null;
|
|
34
|
-
total_usd: number | null;
|
|
35
|
-
};
|
|
36
16
|
/** One run of the series, in the order the agent's runs started: `n` its number among the runs that do a job (JOB_PATHS,
|
|
37
17
|
* 1 the first: the bill's numbering), null for a replay, which checks a revision and does no job. */
|
|
38
18
|
export type CostRun = {
|
|
@@ -56,7 +36,29 @@ export type CostRun = {
|
|
|
56
36
|
cost_upper_bound: boolean;
|
|
57
37
|
learning_usd: number | null;
|
|
58
38
|
total_usd: number | null;
|
|
39
|
+
/** `learning_usd`'s parts, each null when unknown: its own compile turns (`compile_usd`, which GET /runs/{id} charges
|
|
40
|
+
* to the revision they drafted instead), its follow-up rounds' turns (`followup_usd`) and its audit's shadow run
|
|
41
|
+
* (`shadow_usd`). `share_usd`: its share of its revision's compile and check (amortize), which GET /runs/{id}'s
|
|
42
|
+
* `learning_usd` carries in place of `compile_usd`. */
|
|
43
|
+
compile_usd: number | null;
|
|
44
|
+
followup_usd: number | null;
|
|
45
|
+
shadow_usd: number | null;
|
|
46
|
+
share_usd: number | null;
|
|
47
|
+
/** Why a learned workflow did not serve it alone (NOT_SERVED), null when nothing says. */
|
|
48
|
+
not_served: NotServed | null;
|
|
59
49
|
};
|
|
50
|
+
/** Why a run's learned workflow did not serve it alone: `cost_stop`, the money rule stopped it from running first
|
|
51
|
+
* (runner/workflow-first.ts costLineOf: its WORKFLOW_FIRST entry's `needs_revision`, metrics.not_served); `cut`, a
|
|
52
|
+
* restart cut its call and the run was handed back (metrics.workflow_cut, servedOf `cut`), never the workflow's own
|
|
53
|
+
* hand-back: costSummary counts it in `hand_backs.cut`, not `back`. */
|
|
54
|
+
export declare const NOT_SERVED: readonly ["cost_stop", "cut"];
|
|
55
|
+
export type NotServed = (typeof NOT_SERVED)[number];
|
|
56
|
+
/** The by-hand side, the one baseline both the cost page's `before` and the money rule read (costSummary,
|
|
57
|
+
* runner/workflow-first.ts costLineOf): of `runs` (oldest first), the `agent` runs before the first run a learned
|
|
58
|
+
* workflow served (SERVED_PATHS), every one when none has been served. */
|
|
59
|
+
export declare function byHandSide<T extends {
|
|
60
|
+
readonly path: unknown;
|
|
61
|
+
}>(runs: readonly T[]): T[];
|
|
60
62
|
/** The fewest runs after the takeover a "times cheaper" figure needs: fewer say too little. */
|
|
61
63
|
export declare const CHEAPER_AFTER_RUNS = 20;
|
|
62
64
|
/** How many of a learned workflow's newest runs served first at one version the money rule reads
|
package/dist/learning-cost.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// An agent's cost per run, run after run (GET /agents/{id}/learning/cost): what each run of its cost, what learning added
|
|
2
2
|
// for it, and the path it took, so a person sees the cost fall once a learned workflow does the job, and by how much.
|
|
3
|
-
// Aggregates only: no run's input, answer or tool text.
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// Aggregates only: no run's input, answer or tool text. Each run's figures come from the ledger (learn/ledger.ts: one row
|
|
4
|
+
// a run written at its end, one spend row a learning cost, read by summarize); this module says what the series shows
|
|
5
|
+
// (costSummary, billOf), the rule the console, the CLI and the CSV all read. It imports only the errors, so the API
|
|
6
6
|
// document reads its words.
|
|
7
7
|
import { ApiError } from "./errors.js";
|
|
8
8
|
/** The path a run took: `workflow`, a learned workflow finished it alone (served first and answered, or its job's only
|
|
@@ -21,23 +21,17 @@ const usd = (value) => Math.round(value * 1e6) / 1e6;
|
|
|
21
21
|
* own cost is kept at): a decision costs millionths of a dollar, so a figure rounded to the millionth would lose it, and
|
|
22
22
|
* the run's cost here would read other than its usage row (A8). `+ 0` makes a noise-sized negative a plain 0, never -0. */
|
|
23
23
|
export const noiseOff = (value) => Math.round(value * 1e12) / 1e12 + 0;
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
export function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const back = answered ? (looked.look_back_usd ?? null) : 0;
|
|
36
|
-
// What the row holds past the answer and its look-back is a later turn's spend, unknown once the row's cost is; with the
|
|
37
|
-
// look-back's cost unknown the two can't be told apart, and the job is what it had cost when it answered.
|
|
38
|
-
const job = !answered || asAnswered === null || back === null ? asAnswered : rowUsd === null ? null : Math.max(asAnswered, rowUsd - back);
|
|
39
|
-
const spent = back === null || besideUsd === null ? null : back + besideUsd;
|
|
40
|
-
return { cost_usd: job === null ? null : noiseOff(job), learning_usd: spent === null ? null : noiseOff(spent), total_usd: job === null || spent === null ? null : noiseOff(job + spent) };
|
|
24
|
+
/** Why a run's learned workflow did not serve it alone: `cost_stop`, the money rule stopped it from running first
|
|
25
|
+
* (runner/workflow-first.ts costLineOf: its WORKFLOW_FIRST entry's `needs_revision`, metrics.not_served); `cut`, a
|
|
26
|
+
* restart cut its call and the run was handed back (metrics.workflow_cut, servedOf `cut`), never the workflow's own
|
|
27
|
+
* hand-back: costSummary counts it in `hand_backs.cut`, not `back`. */
|
|
28
|
+
export const NOT_SERVED = ["cost_stop", "cut"];
|
|
29
|
+
/** The by-hand side, the one baseline both the cost page's `before` and the money rule read (costSummary,
|
|
30
|
+
* runner/workflow-first.ts costLineOf): of `runs` (oldest first), the `agent` runs before the first run a learned
|
|
31
|
+
* workflow served (SERVED_PATHS), every one when none has been served. */
|
|
32
|
+
export function byHandSide(runs) {
|
|
33
|
+
const first = runs.findIndex((run) => SERVED_PATHS.includes(run.path));
|
|
34
|
+
return (first < 0 ? runs : runs.slice(0, first)).filter((run) => run.path === "agent");
|
|
41
35
|
}
|
|
42
36
|
/** The fewest runs after the takeover a "times cheaper" figure needs: fewer say too little. */
|
|
43
37
|
export const CHEAPER_AFTER_RUNS = 20;
|
|
@@ -69,7 +63,7 @@ export function tookOverOf(runs) {
|
|
|
69
63
|
export const AFTER_STATES = ["priced", "no_served_runs", "cost_unknown"];
|
|
70
64
|
export function costSummary(runs, replay = null, cut = new Set()) {
|
|
71
65
|
const first = runs.findIndex((run) => SERVED_PATHS.includes(run.path));
|
|
72
|
-
const beforeSide = (
|
|
66
|
+
const beforeSide = byHandSide(runs).map((run) => ({ value: run.cost_usd, bound: run.cost_upper_bound }));
|
|
73
67
|
// Served runs alone: a replay (a check of a new revision, at workflow cost) and a run by hand are never the after side.
|
|
74
68
|
const served = first < 0 ? [] : runs.slice(first).filter((run) => SERVED_PATHS.includes(run.path));
|
|
75
69
|
const afterSide = served.map((run) => ({ value: run.total_usd, bound: run.cost_upper_bound }));
|
package/dist/live-reach.d.ts
CHANGED
|
@@ -69,6 +69,8 @@ export declare const liveForRun: (live: RunnerLive, runId: string) => RunnerLive
|
|
|
69
69
|
export declare function provenFor(req: Pick<IncomingMessage, "headers">, live: Pick<RunnerLive, "key">, viewerId: string, now?: number, windowMs?: number): boolean;
|
|
70
70
|
/** How far a relay's proof's time may be from the runner's clock: the one hop's time, and no more. */
|
|
71
71
|
export declare const RELAY_PROOF_MS = 10000;
|
|
72
|
+
/** How long the server waits for a runner's attach listener to take a relayed dial. */
|
|
73
|
+
export declare const RELAY_CONNECT_MS = 10000;
|
|
72
74
|
/** What a relay's proof binds: the machine's token (its SHA-256), the runner its row names, and that runner's
|
|
73
75
|
* incarnation. A viewer's id has no newline, so no relay subject is ever a viewer's. */
|
|
74
76
|
export type RelayTo = {
|
package/dist/live-reach.js
CHANGED
|
@@ -143,6 +143,8 @@ export function provenFor(req, live, viewerId, now = Date.now(), windowMs = LIVE
|
|
|
143
143
|
// runner and nowhere else. Its window is RELAY_PROOF_MS, and the runner takes each proof once (relayProofs).
|
|
144
144
|
/** How far a relay's proof's time may be from the runner's clock: the one hop's time, and no more. */
|
|
145
145
|
export const RELAY_PROOF_MS = 10_000;
|
|
146
|
+
/** How long the server waits for a runner's attach listener to take a relayed dial. */
|
|
147
|
+
export const RELAY_CONNECT_MS = 10_000;
|
|
146
148
|
const relaySubject = (to) => `attach relay\n${to.tokenSha256}\n${to.runner}\n${to.incarnation}`;
|
|
147
149
|
/** The server's proof for relaying the dial of token `to.tokenSha256` to runner `to.runner`, at `at` (ms). */
|
|
148
150
|
export const relayProof = (reach, to, at = Date.now()) => liveProof(reach, relaySubject(to), at);
|
|
@@ -18,13 +18,18 @@ export declare function holdApiOnlyMcpOrigins(store: SettingsWriter, apiOnly: bo
|
|
|
18
18
|
/** The list the store holds, or undefined when it holds none (a full server's store). */
|
|
19
19
|
export declare function heldApiOnlyMcpOrigins(store: SettingsReader): Promise<readonly unknown[] | undefined>;
|
|
20
20
|
/** Why the saved server `name` (`row`) can't be used, or undefined when it can. It can't be used when it is a per_run
|
|
21
|
-
* server the API token saved, the store holds an API-only list, and its address's origin is not on that list.
|
|
21
|
+
* server the API token saved, the store holds an API-only list, and its address's origin is not on that list. `held`:
|
|
22
|
+
* the list a caller already read (heldApiOnlyMcpOrigins); without it, the list is read here, for a per_run row alone.
|
|
23
|
+
* Undefined means "not read", never "none held": a caller passes the list only when the store holds one. */
|
|
22
24
|
export declare function offApiOnlyMcpOrigins(store: SettingsReader, name: string, row: {
|
|
23
25
|
auth: string;
|
|
24
26
|
url: string;
|
|
25
27
|
createdAt?: number;
|
|
26
|
-
}): Promise<string | undefined>;
|
|
28
|
+
}, held?: readonly unknown[]): Promise<string | undefined>;
|
|
29
|
+
/** The words a per_run server the token saved at an origin off the list is refused with (offApiOnlyMcpOrigins). */
|
|
30
|
+
export declare const offApiOnlyMcpOriginsWords: (name: string) => string;
|
|
27
31
|
/** The names of the saved servers offApiOnlyMcpOrigins refuses now, sorted: none when the store holds no list. What an
|
|
28
|
-
* API-only start says once in its log (server.ts), by name alone, never an address
|
|
32
|
+
* API-only start says once in its log (server.ts), by name alone, never an address: on an upgrade with the setting
|
|
33
|
+
* unset, the only notice before a run that grants one fails. */
|
|
29
34
|
export declare function offApiOnlyMcpServers(store: SettingsReader & Parameters<typeof listMcpServers>[0]): Promise<string[]>;
|
|
30
35
|
export {};
|