@parcha/agentrun-kernel 0.1.0-beta.38 → 0.1.0-beta.40
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 +83 -0
- package/NOTICE +2 -0
- package/dist/booking.d.ts +25 -6
- package/dist/booking.js +15 -4
- package/dist/host/index.d.ts +4 -3
- package/dist/host/index.js +3 -2
- package/dist/host/o200k.d.ts +11 -0
- package/dist/host/o200k.js +185 -0
- package/dist/host/overhead.d.ts +72 -12
- package/dist/host/overhead.js +220 -38
- package/dist/host/proxy.d.ts +85 -7
- package/dist/host/proxy.js +374 -87
- package/dist/index.d.ts +6 -6
- package/dist/index.js +4 -4
- package/dist/kernel/cap/admit.d.ts +5 -8
- package/dist/kernel/cap/admit.js +13 -19
- package/dist/kernel/cap/free.d.ts +43 -0
- package/dist/kernel/cap/free.js +71 -0
- package/dist/kernel/cap/index.d.ts +6 -5
- package/dist/kernel/cap/index.js +5 -4
- package/dist/kernel/cap/marks.d.ts +2 -3
- package/dist/kernel/cap/park.d.ts +3 -4
- package/dist/kernel/cap/park.js +4 -5
- package/dist/kernel/cap/port.d.ts +8 -13
- package/dist/kernel/cap/port.js +10 -18
- package/dist/kernel/cap/price.d.ts +10 -49
- package/dist/kernel/cap/price.js +15 -68
- package/dist/kernel/cap/recover.d.ts +1 -2
- package/dist/kernel/cap/recover.js +2 -4
- package/dist/kernel/cap/rule.d.ts +8 -16
- package/dist/kernel/cap/rule.js +35 -35
- package/dist/kernel/cap/run-cap.d.ts +21 -13
- package/dist/kernel/cap/run-cap.js +74 -36
- package/dist/kernel/cap/types.d.ts +11 -31
- package/dist/kernel/cap/words.d.ts +13 -8
- package/dist/kernel/cap/words.js +23 -14
- package/dist/kernel/cap/wrap-up.d.ts +4 -0
- package/dist/kernel/cap/wrap-up.js +5 -1
- package/dist/kernel/registry/computer-surface.generated.d.ts +1 -1
- package/dist/kernel/registry/computer-surface.generated.js +9 -25
- package/dist/kernel/registry/computer.d.ts +14 -7
- package/dist/kernel/registry/computer.js +52 -12
- package/dist/kernel/registry/gate.d.ts +2 -2
- package/dist/kernel/registry/gate.js +4 -7
- package/dist/kernel/registry/hands.d.ts +3 -4
- package/dist/kernel/registry/index.d.ts +1 -1
- package/dist/kernel/registry/index.js +1 -1
- package/dist/kernel/registry/registry.js +6 -9
- package/dist/process/recovery.d.ts +4 -1
- package/dist/process/recovery.js +15 -3
- package/dist/process/resume.d.ts +5 -2
- package/dist/process/resume.js +15 -3
- package/dist/process/tools.d.ts +26 -1
- package/dist/process/tools.js +47 -4
- package/dist/run-booking.d.ts +21 -2
- package/dist/run-booking.js +215 -18
- package/dist/runner.d.ts +82 -21
- package/dist/runner.js +176 -59
- package/dist/testing.d.ts +4 -2
- package/dist/testing.js +2 -2
- package/dist/tool-deferral.d.ts +3 -3
- package/dist/tool-deferral.js +4 -4
- package/dist/types.d.ts +70 -14
- package/dist/types.js +8 -0
- package/package.json +5 -3
- package/vendor/o200k/LICENSE +61 -0
- package/vendor/o200k/o200k_base.tiktoken +199998 -0
package/dist/run-booking.js
CHANGED
|
@@ -2,59 +2,256 @@
|
|
|
2
2
|
// once it is an adapter), but a cap is the run's: a session is judged on what every session of the run has spent and
|
|
3
3
|
// holds, never on its own alone. runBooking wraps the host's beforeRequest (the cap's beforeRequestOf) so that each
|
|
4
4
|
// request it is asked about carries the run's totals:
|
|
5
|
-
// - `spentUsd`: every session's spend from its runner records (sessionSpendOf), summed
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
5
|
+
// - `spentUsd`: every session's spend from its runner records (sessionSpendOf), summed: what was booked, and a request
|
|
6
|
+
// recorded and never answered that no booking here admitted (a lost claim's, cut) at what its host held for it. A
|
|
7
|
+
// request this booking admitted and still in flight is no spend: it is held (below), never read as spent;
|
|
8
|
+
// - `inFlight`: the requests this booking admitted that no record shows answered yet, each at what it was held at, so two
|
|
9
|
+
// sessions asking at once never both take room only one of them fits. A hold-only step is marked `stays` (no request
|
|
10
|
+
// waits on it), and so is one admitted RECORD_WAIT_MS ago that no record shows at all (its write refused: a phantom);
|
|
9
11
|
// - what was spent beside its sessions on the run's behalf (`sessions.beside`: a run's learning, graded by runs of its
|
|
10
12
|
// own), read with them at each admission and added to `spentUsd`, so a run and what it pays for elsewhere never pass
|
|
11
13
|
// its cap together. Unknown (null) makes the run's spend unknown.
|
|
14
|
+
// - `freeRequestsMade`: the run's requests on free models so far (each runner.request marked `freeRequest`, and each
|
|
15
|
+
// admitted here not yet recorded), which the cap's ceiling on such requests reads (runCap, free.ts).
|
|
12
16
|
// Admissions run one at a time across the run's sessions. The priced tools' charges and holds are not here: they are the
|
|
13
17
|
// host's spentUsd and heldUsd hooks (beforeRequestOf), read beside these.
|
|
18
|
+
//
|
|
19
|
+
// Waiting for room. A refusal that `waits` (the cap's: the request fits beside the run's spend, only not beside the holds
|
|
20
|
+
// of steps in flight that settle on their own) is not handed on: the request waits in the run's line, first come first
|
|
21
|
+
// served, and is asked again once a step it waited on has settled (each settle books the step at what it cost, at most its
|
|
22
|
+
// hold, so the room comes back). Every step joins the line as it comes, before its first ask, and only the line's first
|
|
23
|
+
// asks, so a small one never takes the room an earlier one waits for, even when the two came together. Model requests,
|
|
24
|
+
// decisions and paid tool calls (a web fetch's, a search's) wait alike, but for one thing:
|
|
25
|
+
// - A paid call never waits on the request that asked for it. pi runs a tool once its request has answered, but a CLI
|
|
26
|
+
// may not: Claude Code books a request's cost after its calls, and Codex starts a call with its response still open,
|
|
27
|
+
// so that request settles only once the call returns. A paid call names the model session whose tool call it runs in
|
|
28
|
+
// (ModelRequest.paid.under); that session's requests in flight are its parents, which no record links to the call
|
|
29
|
+
// (the call's id is in the response, never on the request's records), so each of them counts as one. For the call,
|
|
30
|
+
// they are `stays`: held beside it, never waited on. It fits beside them or is refused now (a raise sends it), never
|
|
31
|
+
// admitted past the cap. It still waits on every other step, which settles without it (another session's requests
|
|
32
|
+
// included: pi's children, each booked before its own calls run).
|
|
33
|
+
// - A paid call whose own session has a request in flight goes ahead of the line's steps that are not such calls (after
|
|
34
|
+
// any that are, in the order they came): a step waiting for room may wait on that request, which cannot settle until
|
|
35
|
+
// the call is done, so behind it the call would wait on its own request too.
|
|
36
|
+
// - The hold ledger is the run's records (every session's runner.request and runner.turn), which every process of the run
|
|
37
|
+
// writes: a waiter reads them again on a short timer, from WAIT_POLL_FIRST_MS doubling to WAIT_POLL_MOST_MS, so a step
|
|
38
|
+
// that settles in this process or any other wakes it the same way. No process signals another.
|
|
39
|
+
// - No deadlock: a waiter holds nothing while it waits (nothing is admitted, recorded or held for it), and it waits only on
|
|
40
|
+
// steps that end by themselves (a request streaming, a decision, a paid call), never on a hold-only step, on a lost
|
|
41
|
+
// claim's request, or, for a paid call, on its own session's requests. A waiter whose steps stop settling for WAIT_STALL_MS is refused after all, in the words it waited
|
|
42
|
+
// under: the run parks for a raise, as before waiting existed.
|
|
43
|
+
// - A waiter leaves the line, admitted for nothing and holding nothing, when its client goes (its signal: a stop, a
|
|
44
|
+
// cancel, a lost claim), when the run stops (`stopped`, for a paid call or a decision too), when its booking stops (`halted`: thrown, as a stop that came during the check), or when the
|
|
45
|
+
// run parks meanwhile (`parked`: refused in its own words, so nothing more is sent until a raise).
|
|
46
|
+
// - Nothing is admitted once its signal is aborted: a step that comes, or reaches its turn, after its client went or the
|
|
47
|
+
// run stopped is refused with the signal's reason, before any read, so no call starts or is charged after a stop.
|
|
14
48
|
import { sessionSpendOf } from "./runner.js";
|
|
49
|
+
import { freeRequestsIn } from "./kernel/cap/free.js";
|
|
50
|
+
/** How soon a waiter reads the run's records again, first and at the slowest (it doubles between them). */
|
|
51
|
+
export const WAIT_POLL_FIRST_MS = 50;
|
|
52
|
+
export const WAIT_POLL_MOST_MS = 1_000;
|
|
53
|
+
/** How long a request waits for room with none of the steps it waits on settling: past it, it is refused for room and the
|
|
54
|
+
* run parks (a step that never settles, which nothing here expects, never hangs the run). */
|
|
55
|
+
export const WAIT_STALL_MS = 10 * 60_000;
|
|
56
|
+
/** How long an admission may go unrecorded before no one waits on it: its record is written just after its admission,
|
|
57
|
+
* so one missing this long never will be (its write refused, its booking stopped). It is still held, never freed. */
|
|
58
|
+
export const RECORD_WAIT_MS = 30_000;
|
|
15
59
|
const idOf = (key, turn) => `${key}\u0000${turn}`;
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
60
|
+
/** The error a waiter leaves with when its client goes. */
|
|
61
|
+
const gone = (signal) => (signal.reason instanceof Error ? signal.reason : new Error("the request's client went while it waited for room under the run's cap"));
|
|
62
|
+
/** `before`, asked about each request with the run's totals over `sessions` in place of its session's own; a refusal that
|
|
63
|
+
* waits is asked again once the room may be there (above). */
|
|
64
|
+
export function runBooking(sessions, before, options = {}) {
|
|
65
|
+
/** Admitted here and not yet answered, by session and turn, with when. One whose record never lands (its write refused,
|
|
66
|
+
* its process gone) stays held: the run's spend is never under-counted. */
|
|
20
67
|
const admitted = new Map();
|
|
21
68
|
let queue = Promise.resolve();
|
|
22
|
-
|
|
69
|
+
/** The requests waiting for room, in the order they came: only the first asks. */
|
|
70
|
+
const line = [];
|
|
71
|
+
const stallMs = options.stallMs ?? WAIT_STALL_MS;
|
|
72
|
+
/** One admission, in turn with every other: `before`'s verdict on `request` over the run's totals now, and the steps in
|
|
73
|
+
* flight it was judged beside that settle on their own (what a refusal that waits waits on). */
|
|
74
|
+
const ask = (request, signal) => {
|
|
23
75
|
const asked = queue.then(async () => {
|
|
76
|
+
// A step whose client went, or whose run stopped, while it waited its turn here is never admitted: nothing is held,
|
|
77
|
+
// recorded or charged for it, and the next one asks.
|
|
78
|
+
if (signal?.aborted)
|
|
79
|
+
throw gone(signal);
|
|
24
80
|
let spentUsd = 0;
|
|
81
|
+
// The run's requests on free models so far, each counted once: every one whose runner.request says so, answered, in
|
|
82
|
+
// flight or cut, in any session and any process, and below, each admitted here not yet recorded.
|
|
83
|
+
let freeRequestsMade = 0;
|
|
25
84
|
const recorded = new Set();
|
|
85
|
+
const answered = new Set();
|
|
26
86
|
for (const key of await sessions.keys()) {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// flight at its hold, until it is answered (a lost claim's, admitted by no booking here, counts nothing).
|
|
87
|
+
const records = await sessions.read(key);
|
|
88
|
+
const spend = sessionSpendOf(records);
|
|
89
|
+
freeRequestsMade += freeRequestsIn(records);
|
|
31
90
|
for (const turn of spend.turns)
|
|
32
|
-
|
|
33
|
-
|
|
91
|
+
recorded.add(idOf(key, turn));
|
|
92
|
+
for (const turn of spend.answered)
|
|
93
|
+
answered.add(idOf(key, turn));
|
|
94
|
+
// Each request's figure (sessionSpendOf's), but one this booking admitted and has not seen answered: that one is
|
|
95
|
+
// held, in flight below, never spent. A hold-only step still open is no figure there: held below too.
|
|
96
|
+
let own = 0;
|
|
97
|
+
for (const { turn, usd } of spend.figures) {
|
|
98
|
+
if (!spend.answered.has(turn) && admitted.has(idOf(key, turn)))
|
|
99
|
+
continue;
|
|
100
|
+
own = own === null || usd === null ? null : own + usd;
|
|
101
|
+
}
|
|
102
|
+
spentUsd = spentUsd === null || own === null ? null : spentUsd + own;
|
|
34
103
|
}
|
|
35
104
|
if (sessions.beside) {
|
|
36
105
|
const beside = await sessions.beside();
|
|
37
106
|
spentUsd = spentUsd === null || beside === null || !Number.isFinite(beside) ? null : spentUsd + beside;
|
|
38
107
|
}
|
|
39
108
|
for (const id of [...admitted.keys()])
|
|
40
|
-
if (
|
|
109
|
+
if (answered.has(id))
|
|
41
110
|
admitted.delete(id);
|
|
42
|
-
const
|
|
111
|
+
const now = Date.now();
|
|
112
|
+
const settling = new Set();
|
|
113
|
+
const inFlight = [];
|
|
114
|
+
const under = request.paid?.under;
|
|
115
|
+
for (const [id, { at, key, ...flight }] of admitted) {
|
|
116
|
+
if (flight.freeRequest && !recorded.has(id))
|
|
117
|
+
freeRequestsMade += 1;
|
|
118
|
+
// One that may never settle by itself (a hold-only step, an admission whose record never landed) stays: held, and
|
|
119
|
+
// never waited on. So does, for a paid call, a request of the session it runs in: it may be the one that asked
|
|
120
|
+
// for the call, which settles only once the call is done.
|
|
121
|
+
const stays = flight.stays === true || (!recorded.has(id) && now - at > RECORD_WAIT_MS) || (under !== undefined && key === under);
|
|
122
|
+
if (!stays)
|
|
123
|
+
settling.add(id);
|
|
124
|
+
inFlight.push(stays ? { ...flight, stays: true } : flight);
|
|
125
|
+
}
|
|
126
|
+
const verdict = await before({ ...request, spentUsd, inFlight, freeRequestsMade });
|
|
43
127
|
if (!(verdict && "refuse" in verdict)) {
|
|
44
128
|
const holdUsd = verdict && "holdUsd" in verdict ? verdict.holdUsd : undefined;
|
|
129
|
+
const freeRequest = !!verdict && "freeRequest" in verdict && verdict.freeRequest === true;
|
|
45
130
|
const outputTokensMax = verdict && "outputTokensMax" in verdict ? verdict.outputTokensMax : request.outputTokensAsked;
|
|
46
131
|
admitted.set(idOf(request.key, request.turn), {
|
|
132
|
+
at: Date.now(),
|
|
133
|
+
key: request.key,
|
|
47
134
|
turn: request.turn,
|
|
48
135
|
model: request.model,
|
|
49
136
|
...(request.inputTokensMax === undefined ? {} : { inputTokensMax: request.inputTokensMax }),
|
|
50
137
|
...(outputTokensMax === undefined ? {} : { outputTokensMax }),
|
|
51
138
|
...(typeof holdUsd === "number" ? { holdUsd } : {}),
|
|
52
139
|
...(request.host === undefined ? {} : { host: request.host }),
|
|
140
|
+
...(request.route === undefined ? {} : { route: request.route }),
|
|
141
|
+
...(freeRequest ? { freeRequest: true } : {}),
|
|
142
|
+
// A hold-only step, or money already owed (a browser session's reserve or release, settled when its session
|
|
143
|
+
// ends, not when a call answers): never waited on.
|
|
144
|
+
...(request.paid?.hold || request.paid?.owed ? { stays: true } : {}),
|
|
53
145
|
});
|
|
54
146
|
}
|
|
55
|
-
return verdict;
|
|
147
|
+
return { verdict, settling };
|
|
56
148
|
});
|
|
57
149
|
queue = asked.catch(() => undefined);
|
|
58
150
|
return asked;
|
|
59
151
|
};
|
|
152
|
+
/** Waits `ms`, or less: until the line's first leaves (`place.wake`) or the request's client goes. */
|
|
153
|
+
const nap = (place, ms, signal) => new Promise((resolve) => {
|
|
154
|
+
const done = () => {
|
|
155
|
+
clearTimeout(timer);
|
|
156
|
+
signal?.removeEventListener("abort", done);
|
|
157
|
+
if (place.wake === done)
|
|
158
|
+
place.wake = undefined;
|
|
159
|
+
resolve();
|
|
160
|
+
};
|
|
161
|
+
const timer = setTimeout(done, ms);
|
|
162
|
+
place.wake = done;
|
|
163
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
164
|
+
});
|
|
165
|
+
/** Leaves the line; the next one asks at once (the room the first waited for may fit it too). */
|
|
166
|
+
const leave = (place) => {
|
|
167
|
+
const at = line.indexOf(place);
|
|
168
|
+
if (at < 0)
|
|
169
|
+
return;
|
|
170
|
+
line.splice(at, 1);
|
|
171
|
+
if (at === 0)
|
|
172
|
+
line[0]?.wake?.();
|
|
173
|
+
};
|
|
174
|
+
/** Why a waiter leaves now, if it does: its client went, the run stopped or its booking did (thrown), or the run parked
|
|
175
|
+
* (refused). */
|
|
176
|
+
const leaving = (request, signal) => {
|
|
177
|
+
if (signal?.aborted)
|
|
178
|
+
throw gone(signal);
|
|
179
|
+
const halted = request.halted?.();
|
|
180
|
+
if (halted)
|
|
181
|
+
throw halted;
|
|
182
|
+
return options.parked?.() ? "parked" : undefined;
|
|
183
|
+
};
|
|
184
|
+
/** Whether `request` is a paid call whose own session (paid.under) has a request in flight: one admitted here that the
|
|
185
|
+
* session's records do not show answered. */
|
|
186
|
+
const underOpen = async (request) => {
|
|
187
|
+
const under = request.paid?.under;
|
|
188
|
+
if (under === undefined)
|
|
189
|
+
return false;
|
|
190
|
+
const turns = [...admitted.values()].filter((flight) => flight.key === under).map((flight) => flight.turn);
|
|
191
|
+
if (turns.length === 0)
|
|
192
|
+
return false;
|
|
193
|
+
const answered = sessionSpendOf(await sessions.read(under)).answered;
|
|
194
|
+
return turns.some((turn) => !answered.has(turn));
|
|
195
|
+
};
|
|
196
|
+
/** A refusal as handed on: one that waits is the plain refusal it is to anyone not waiting. */
|
|
197
|
+
const plain = (verdict) => {
|
|
198
|
+
if (!(verdict && "refuse" in verdict && verdict.waits))
|
|
199
|
+
return verdict;
|
|
200
|
+
const { waits: _waits, ...refusal } = verdict;
|
|
201
|
+
return refusal;
|
|
202
|
+
};
|
|
203
|
+
return async (request) => {
|
|
204
|
+
// What ends its wait: its own client's going (a model request's), or the run's stop (any step's: a paid call's or a
|
|
205
|
+
// decision's has no client of its own here).
|
|
206
|
+
const signal = request.signal && options.stopped ? AbortSignal.any([request.signal, options.stopped]) : (request.signal ?? options.stopped);
|
|
207
|
+
// Every step joins the line as it comes, before its first ask, and asks only once it is first: steps that come together
|
|
208
|
+
// are asked in the order they came, so one behind a step that must wait never goes ahead of it. The first leaving
|
|
209
|
+
// (admitted, refused or gone) wakes the next at once. A paid call whose own session has a request in flight goes ahead
|
|
210
|
+
// of every step that is not one: a step ahead may wait on that request, which settles only once the call is done.
|
|
211
|
+
const place = request.paid?.under !== undefined && (await underOpen(request)) ? { under: true } : {};
|
|
212
|
+
const ahead = place.under ? line.findIndex((other) => !other.under) : -1;
|
|
213
|
+
if (ahead < 0)
|
|
214
|
+
line.push(place);
|
|
215
|
+
else
|
|
216
|
+
line.splice(ahead, 0, place);
|
|
217
|
+
try {
|
|
218
|
+
let pause = WAIT_POLL_FIRST_MS;
|
|
219
|
+
while (line[0] !== place) {
|
|
220
|
+
// Not first yet: it has no verdict of its own to be refused in, so a park reaches it as its first ask does.
|
|
221
|
+
await nap(place, pause, signal);
|
|
222
|
+
if (signal?.aborted)
|
|
223
|
+
throw gone(signal);
|
|
224
|
+
const halted = request.halted?.();
|
|
225
|
+
if (halted)
|
|
226
|
+
throw halted;
|
|
227
|
+
pause = Math.min(2 * pause, WAIT_POLL_MOST_MS);
|
|
228
|
+
}
|
|
229
|
+
pause = WAIT_POLL_FIRST_MS;
|
|
230
|
+
let quietSince = Date.now();
|
|
231
|
+
let waitedOn;
|
|
232
|
+
for (;;) {
|
|
233
|
+
const { verdict, settling } = await ask(request, signal);
|
|
234
|
+
if (!(verdict && "refuse" in verdict && verdict.waits))
|
|
235
|
+
return verdict;
|
|
236
|
+
// A step it waited on has settled since it last asked: the wait goes on, from the first pause again.
|
|
237
|
+
if (waitedOn && [...waitedOn].some((id) => !settling.has(id))) {
|
|
238
|
+
quietSince = Date.now();
|
|
239
|
+
pause = WAIT_POLL_FIRST_MS;
|
|
240
|
+
}
|
|
241
|
+
waitedOn = settling;
|
|
242
|
+
if (Date.now() - quietSince >= stallMs)
|
|
243
|
+
return plain(verdict);
|
|
244
|
+
// It waits first in line: only the first asks again.
|
|
245
|
+
do {
|
|
246
|
+
await nap(place, pause, signal);
|
|
247
|
+
if (leaving(request, signal) === "parked")
|
|
248
|
+
return plain(verdict);
|
|
249
|
+
pause = Math.min(2 * pause, WAIT_POLL_MOST_MS);
|
|
250
|
+
} while (line[0] !== place);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
leave(place);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
60
257
|
}
|
package/dist/runner.d.ts
CHANGED
|
@@ -41,9 +41,6 @@ export type BeforeRequest = (request: {
|
|
|
41
41
|
/** The most input tokens this request can be billed for (the body's UTF-8 bytes plus what the provider adds, as the
|
|
42
42
|
* model proxy measured it), undefined when it was not measured: a budget treats that input as unknown, never 0. */
|
|
43
43
|
inputTokensMax?: number;
|
|
44
|
-
/** Its `inputTokensMax` is its provider's count, not its bytes (ModelRequest.inputTokensCounted): two bounds are compared
|
|
45
|
-
* (the cap's expected pricing) only when both were measured the same way. */
|
|
46
|
-
inputTokensCounted?: true;
|
|
47
44
|
/** The most output tokens it asks for, and the least it may be limited to and still answer, as the model proxy read
|
|
48
45
|
* them from its body (ModelRequest). */
|
|
49
46
|
outputTokensAsked?: number;
|
|
@@ -53,7 +50,8 @@ export type BeforeRequest = (request: {
|
|
|
53
50
|
/** A paid tool call, no model request (ModelRequest.paid): held at its fee alone, named so in a refusal. `call`: the
|
|
54
51
|
* tool call it was booked under, `charged_by`, who charges for it, and `tool`/`tag`, the tool it is and its session,
|
|
55
52
|
* all written on its runner.request. `owed`: money already owed at its provider, held and booked, never refused.
|
|
56
|
-
* `hold`: a hold-only step, given back on a lost claim; `ref`: the one booking it names
|
|
53
|
+
* `hold`: a hold-only step, given back on a lost claim; `ref`: the one booking it names; `under`: the model session
|
|
54
|
+
* whose tool call it runs in, whose requests in flight it never waits on (ModelRequest.paid). */
|
|
57
55
|
paid?: {
|
|
58
56
|
name: string;
|
|
59
57
|
basis: string;
|
|
@@ -64,11 +62,12 @@ export type BeforeRequest = (request: {
|
|
|
64
62
|
owed?: true;
|
|
65
63
|
hold?: true;
|
|
66
64
|
ref?: string;
|
|
65
|
+
under?: string;
|
|
67
66
|
};
|
|
68
67
|
/** A side call (ModelRequest.side): on another model than the session's (a client's), or the host's own (`host`). */
|
|
69
68
|
side?: true;
|
|
70
69
|
/** A request on the session's model that is no answer of the turn (ModelRequest.unanswered: a title, a summary, a
|
|
71
|
-
* child's): still the run's own to book, but
|
|
70
|
+
* child's): still the run's own to book, but no answer the host keeps its wrap-up's reserve back for. */
|
|
72
71
|
unanswered?: true;
|
|
73
72
|
/** It carries the host's note (ModelRequest.noted), and its input measured without it (`inputTokensMaxWithoutNote`):
|
|
74
73
|
* a host that admits it only without the note says so (Admission.withoutNote). */
|
|
@@ -84,18 +83,29 @@ export type BeforeRequest = (request: {
|
|
|
84
83
|
* (`holdUsd`): a budget holds each one until it answers, since `spentUsd` counts answered requests only. Requests are
|
|
85
84
|
* admitted one at a time, so each check sees every request admitted before it. */
|
|
86
85
|
inFlight: readonly InFlight[];
|
|
87
|
-
|
|
86
|
+
/** The run's requests on free models so far (runBooking, over every session of the run): each whose runner.request says
|
|
87
|
+
* so (`freeRequest`), answered, in flight or cut, and each admitted that no record shows yet. Absent: not counted here. */
|
|
88
|
+
freeRequestsMade?: number;
|
|
89
|
+
/** The request's client's signal (ModelRequest.signal): a host that makes it wait for room lets it go when it aborts. */
|
|
90
|
+
signal?: AbortSignal;
|
|
91
|
+
/** The booking's own stop or pause, now (its `stopped`, `draining`, or a cost it could not record): a request waiting
|
|
92
|
+
* for room leaves with this error once there is one, nothing written for it. */
|
|
93
|
+
halted?: () => Error | undefined;
|
|
94
|
+
}) => void | Refusal | Admitted | Promise<void | Refusal | Admitted>;
|
|
88
95
|
/** A host's refusal of a request: its words, and, from a spend cap, whether no raise could send it (`forGood`) and, for
|
|
89
|
-
* one a raise could, its figures (what the
|
|
90
|
-
* sends it): what the run's end is decided from (the kernel cap's parkOf).
|
|
96
|
+
* one a raise could, its figures (what the run had spent, never a hold; what its steps in flight held; what the request
|
|
97
|
+
* needs at the least; the least cap that sends it): what the run's end is decided from (the kernel cap's parkOf).
|
|
98
|
+
* `waits`: refused only because steps still in flight hold the room it needs, steps that settle on their own (it fits
|
|
99
|
+
* beside the run's spend with none of them out): a booking that can wait (runBooking) asks again once they settle, and
|
|
100
|
+
* any other reads it as the refusal it is. */
|
|
91
101
|
export type Refusal = {
|
|
92
102
|
refuse: string;
|
|
93
103
|
forGood?: boolean;
|
|
94
104
|
figures?: {
|
|
95
105
|
spentUsd: number;
|
|
106
|
+
heldUsd?: number;
|
|
96
107
|
needsUsd: number;
|
|
97
108
|
leastCapUsd: number;
|
|
98
|
-
boundUsd?: number;
|
|
99
109
|
step?: {
|
|
100
110
|
name: string;
|
|
101
111
|
holdUsd: number;
|
|
@@ -105,18 +115,21 @@ export type Refusal = {
|
|
|
105
115
|
/** The refusal's code, from a host that gives one (a spend cap's: `spend_cap_reached`): carried on the error that ends
|
|
106
116
|
* the node (HarnessRunError.refusal), so a reader knows the refusal by it, never by its words. */
|
|
107
117
|
code?: string;
|
|
118
|
+
waits?: true;
|
|
108
119
|
};
|
|
109
120
|
/** A request admitted and not yet answered, as a budget holds it: at what its host held for it (`holdUsd`), when it held
|
|
110
|
-
* one.
|
|
121
|
+
* one. `stays`: a hold-only step (ModelRequest.paid.hold, a browser session's reserve), let go only when its holder says,
|
|
122
|
+
* never by settling on its own: no request waits for it. */
|
|
111
123
|
export type InFlight = {
|
|
112
124
|
turn: number;
|
|
113
125
|
model: string;
|
|
114
126
|
inputTokensMax?: number;
|
|
115
127
|
outputTokensMax?: number;
|
|
116
128
|
holdUsd?: number;
|
|
117
|
-
boundUsd?: number;
|
|
118
129
|
host?: string;
|
|
119
130
|
route?: string;
|
|
131
|
+
freeRequest?: true;
|
|
132
|
+
stays?: true;
|
|
120
133
|
};
|
|
121
134
|
/** A host's admission: what it holds for the request (`holdUsd`), and the most output tokens, thinking included, it may
|
|
122
135
|
* be sent with (`outputTokensMax`, what is left of a cap can buy), which its ticket carries to the model proxy. A
|
|
@@ -124,17 +137,27 @@ export type InFlight = {
|
|
|
124
137
|
* streamed, in tokens (one a byte), never more than `holdUsd`; without it, at `holdUsd`. `withoutNote`: a request that
|
|
125
138
|
* carries its host's note is admitted only as it was without it (the note's bytes did not fit): it goes without it.
|
|
126
139
|
* `note`: the host's record of the note this request carries, written on the request's `runner.turn` record once it
|
|
127
|
-
* ends, beside how it was booked, so a later process reads whether the note reached an answer. `
|
|
128
|
-
*
|
|
140
|
+
* ends, beside how it was booked, so a later process reads whether the note reached an answer. `freeRequest`: one of
|
|
141
|
+
* the run's requests on a free model, which its ceiling on such requests counts: written on its runner.request (as
|
|
142
|
+
* `freeRequest`), so the count holds it in every process, and a cut one books at $0 (bookCut). */
|
|
129
143
|
export type Admission = {
|
|
130
144
|
holdUsd?: number;
|
|
131
|
-
boundUsd?: number;
|
|
132
145
|
outputTokensMax?: number;
|
|
133
146
|
stoppedUsd?: (streamedTokens: number) => number;
|
|
134
147
|
withoutNote?: true;
|
|
135
148
|
note?: {
|
|
136
149
|
[key: string]: JsonValue;
|
|
137
150
|
};
|
|
151
|
+
freeRequest?: true;
|
|
152
|
+
};
|
|
153
|
+
/** An admission as a host gives it: with `free` when the request is on a free model (its known price is $0 for every
|
|
154
|
+
* token, with no fee beside), so it costs nothing however it ends: booked a known $0 (booking.ts), its own cost 0, its
|
|
155
|
+
* runner.turn marked `free` for a reader to say why. `plan`: the request is billed to a person's subscription plan (a
|
|
156
|
+
* ChatGPT plan's model, CapModel.plan), never in dollars: it costs the run $0 however it ends, its runner.request and
|
|
157
|
+
* runner.turn marked so (`plan`, `billing: "plan"`), so a later process books it so too when a crash cut it. */
|
|
158
|
+
export type Admitted = Admission & {
|
|
159
|
+
free?: true;
|
|
160
|
+
plan?: true;
|
|
138
161
|
};
|
|
139
162
|
export interface HarnessRunnerOptions {
|
|
140
163
|
/** The run's store, through whoever owns it. */
|
|
@@ -226,12 +249,9 @@ export interface ModelBooking {
|
|
|
226
249
|
* only, so a reader of the run's spend that a request overlapping another must see adds this. Null while one in flight
|
|
227
250
|
* has no hold (never under a cap, which refuses a request it cannot price): unknown, never read as 0. */
|
|
228
251
|
heldUsd(): number | null;
|
|
229
|
-
/** What a cache miss adds to those holds (Admission.boundUsd), the requests still in flight together: the run's tools stop
|
|
230
|
-
* on the holds plus this (the cap counts a request that never answers at both), so a reader that must warn no later
|
|
231
|
-
* adds it too. 0 with none in flight or none with a bound. */
|
|
232
|
-
boundUsd(): number;
|
|
233
252
|
/** Books, once, each paid request a crash cut off (a `runner.request` with no `runner.turn`): a `runner.turn` marked
|
|
234
|
-
* `cut
|
|
253
|
+
* `cut`, at its hold as the most it can have cost (CUT_AT_HOLD) or, with none, at an unknown price (one billed to a
|
|
254
|
+
* plan, its runner.request's `plan`: at $0, `billing: "plan"`), and a `request_end` saying so. A booked request is answered, so this is idempotent.
|
|
235
255
|
* Call it before the session sends anything. */
|
|
236
256
|
bookCut(): Promise<void>;
|
|
237
257
|
}
|
|
@@ -247,7 +267,6 @@ export declare function modelPortFor(writer: SessionWriter, options: ModelPortOp
|
|
|
247
267
|
export declare function sessionSpendOf(records: readonly SessionRecord[]): {
|
|
248
268
|
turns: number[];
|
|
249
269
|
held: Map<number, number>;
|
|
250
|
-
bounds: Map<number, number>;
|
|
251
270
|
hosts: Map<number, string>;
|
|
252
271
|
answered: Set<number>;
|
|
253
272
|
openHolds: Set<number>;
|
|
@@ -258,6 +277,45 @@ export declare function sessionSpendOf(records: readonly SessionRecord[]): {
|
|
|
258
277
|
}[];
|
|
259
278
|
cost: SessionCost;
|
|
260
279
|
};
|
|
280
|
+
/** A cost's parts, each charge counted by turnCostOf: `known`, the sum of the figures that are a charge's own; `bounded`,
|
|
281
|
+
* the sum of those that are a cut request's hold (CUT_AT_HOLD), the most each can have cost, and `atMost`, how many those
|
|
282
|
+
* are; `unpriced`, how many charges have no figure at all. */
|
|
283
|
+
export type CostParts = {
|
|
284
|
+
readonly known: number;
|
|
285
|
+
readonly bounded: number;
|
|
286
|
+
readonly atMost: number;
|
|
287
|
+
readonly unpriced: number;
|
|
288
|
+
};
|
|
289
|
+
/** The one rule for a cost made of parts, as every reader of a run's cost says it (the session's, the run's stored cost,
|
|
290
|
+
* its view and its usage row). Every charge counted: `usd` is their sum, holds included, and `atMost` how many of them are
|
|
291
|
+
* a hold, so the cost is at most `usd`. Any charge with no figure: `usd` is null, `soFarUsd` the charges' own figures
|
|
292
|
+
* alone, and `unpriced` counts the held ones with those of no figure. A hold is a ceiling, never spend: beside a cost no
|
|
293
|
+
* one knows it bounds nothing, so it joins no known sum, where it would read as money spent. */
|
|
294
|
+
export declare function costOfParts(parts: readonly CostParts[]): SessionCost;
|
|
295
|
+
/** Whether a runner.turn books a request already booked: its turn (`key`, as the reader names it) is in `seen`, which it
|
|
296
|
+
* joins otherwise. Every reader of a booking (sessionSpendOf, serve's bookedSpend) skips such a record, so a request is
|
|
297
|
+
* counted once: two bookings of one session that each read it open, or a cut written twice, wrote it twice. A record
|
|
298
|
+
* with no turn (written before turns were named) is never a repeat. */
|
|
299
|
+
export declare function rebooked<K>(seen: Set<K>, key: K | undefined): boolean;
|
|
300
|
+
/** The rule (runner.turn `rule`) of a request cut with no answer that bookCut booked at what its host held for it: the
|
|
301
|
+
* most it can have cost (its whole input uncached and the output it was sent with), marked `atMost`. */
|
|
302
|
+
export declare const CUT_AT_HOLD = "cut";
|
|
303
|
+
/** A booked request's own cost, from its runner.turn, as every reader of a run's cost reads it (the session's cost, the
|
|
304
|
+
* run's stored cost and its views): what it was booked at, else its `usd`; null when no one knows it (an answer with no
|
|
305
|
+
* price of its own booked at its hold for the cap, answeredUnpriced; a request cut with no hold). `atMost`: a request cut
|
|
306
|
+
* with no answer, booked at its hold (CUT_AT_HOLD), so the figure is the most it can have cost, never less. One billed to
|
|
307
|
+
* a person's plan (`billing: "plan"`) is $0 to the run however it ended: the plan pays for it, never the run. */
|
|
308
|
+
export declare function turnCostOf(data: {
|
|
309
|
+
usd?: unknown;
|
|
310
|
+
bookedUsd?: unknown;
|
|
311
|
+
cut?: unknown;
|
|
312
|
+
rule?: unknown;
|
|
313
|
+
atMost?: unknown;
|
|
314
|
+
billing?: unknown;
|
|
315
|
+
}): {
|
|
316
|
+
usd: number | null;
|
|
317
|
+
atMost: boolean;
|
|
318
|
+
};
|
|
261
319
|
/** Whether a request ended with no price of its own (usd null) under a rule that books it at a figure of the host's: an
|
|
262
320
|
* answer with no cost (bookRequest's rule `answered`, or `answered:<label>`: its hold), or one our side stopped
|
|
263
321
|
* (`stopped`: what streamed, an estimate) or lost (`lost`, `lost:<label>`: its hold). Each counts for the cap at that
|
|
@@ -267,10 +325,13 @@ export declare const answeredUnpriced: (data: {
|
|
|
267
325
|
rule?: unknown;
|
|
268
326
|
}) => boolean;
|
|
269
327
|
/** A session's cost as a person is shown it: `usd` when every paid request has its own figure, else null with the known
|
|
270
|
-
* part so far (`soFarUsd
|
|
328
|
+
* part so far (`soFarUsd`, the charges' own figures alone) and how many have none (`unpriced`, a cut request's hold
|
|
329
|
+
* among them). `atMost`, set only while `usd` is known: how many of its figures are a cut request's hold (turnCostOf),
|
|
330
|
+
* so the cost is at most what it says (costOfParts). */
|
|
271
331
|
export type SessionCost = {
|
|
272
332
|
usd: number | null;
|
|
273
333
|
soFarUsd: number;
|
|
274
334
|
unpriced: number;
|
|
335
|
+
atMost?: number;
|
|
275
336
|
};
|
|
276
337
|
export declare function createHarnessRunner(adapter: AgentAdapter, options: HarnessRunnerOptions): HarnessRunner;
|