@parcha/agentrun-kernel 0.1.0-beta.38 → 0.1.0-beta.39
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/kernel/cap/price.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
// The kernel cap's one pricing rule, the same for every harness
|
|
2
|
-
// provider adds, what its hosted tools read) is priced at the dearer of the
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// sent with, its thinking budget included.
|
|
1
|
+
// The kernel cap's one pricing rule, the same for every harness: a request at its worst case. Its input bound (its
|
|
2
|
+
// measured bytes, what its provider adds, what its hosted tools read) is priced uncached, every token at the dearer of the
|
|
3
|
+
// input and cache-write prices, so a cache miss costs no more than it was held at; its output at what it is sent with,
|
|
4
|
+
// its thinking budget included. No request can pass the cap.
|
|
6
5
|
import { providerInputOverhead } from "../../host/overhead.js";
|
|
7
|
-
/** How long after a request ended the next one in its conversation is expected to read its cache: 4 minutes, inside
|
|
8
|
-
* Anthropic's 5. */
|
|
9
|
-
export const CACHE_WARM_MS = 4 * 60_000;
|
|
10
6
|
/** The tokens a limited request keeps for its answer. */
|
|
11
7
|
export const ANSWER_TOKENS_LEAST = 1_024;
|
|
12
8
|
/** The least thinking budget Anthropic takes. */
|
|
@@ -33,70 +29,21 @@ export function ratesFor(price, inputTokens) {
|
|
|
33
29
|
}
|
|
34
30
|
return rates;
|
|
35
31
|
}
|
|
36
|
-
/** `price` as the cap prices with it:
|
|
37
|
-
*
|
|
38
|
-
* passes (ratesFor). A hold
|
|
32
|
+
/** `price` as the cap prices with it: every input token uncached, at the dearer of the input and cache-write rates (a
|
|
33
|
+
* cache read is cheaper and a cache write is the dearest an input token bills at), and its output at the output rate,
|
|
34
|
+
* each at the rates of the tier its whole input passes (ratesFor). A hold the cap prices is so never under what the
|
|
35
|
+
* provider bills for the same tokens, whatever the cache did. */
|
|
39
36
|
export function pricingOf(price) {
|
|
40
|
-
return (
|
|
41
|
-
const rates = ratesFor(price,
|
|
42
|
-
const read = rates.cacheRead ?? rates.input;
|
|
37
|
+
return (inputTokens, output) => {
|
|
38
|
+
const rates = ratesFor(price, inputTokens);
|
|
43
39
|
const fresh = Math.max(rates.input, rates.cacheWrite ?? rates.input);
|
|
44
|
-
return (
|
|
40
|
+
return (inputTokens * fresh + output * rates.output) / 1e6;
|
|
45
41
|
};
|
|
46
42
|
}
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
49
|
-
export
|
|
50
|
-
|
|
51
|
-
* on its model that ended under CACHE_WARM_MS ago (by a clock that does not read before their end) with a bound no bigger
|
|
52
|
-
* than its own (a conversation only grows between its own requests), the biggest, the newest of equal ones. So a smaller
|
|
53
|
-
* request between two of a conversation's own (a CLI's side request while a tool runs, a child's, a summary) leaves the
|
|
54
|
-
* conversation's next request warm from its last, and a conversation that compacted is warm again from its first request
|
|
55
|
-
* after the compaction. None: priced uncached. */
|
|
56
|
-
export function warmFrom(bound, recent, now, model) {
|
|
57
|
-
let best;
|
|
58
|
-
for (const one of recent ?? []) {
|
|
59
|
-
const gap = now - one.endedAt;
|
|
60
|
-
if (one.model !== model || !(gap >= 0 && gap < CACHE_WARM_MS) || !(one.boundTokens <= bound))
|
|
61
|
-
continue;
|
|
62
|
-
if (!best || one.boundTokens > best.boundTokens || (one.boundTokens === best.boundTokens && one.endedAt >= best.endedAt))
|
|
63
|
-
best = one;
|
|
64
|
-
}
|
|
65
|
-
return best;
|
|
66
|
-
}
|
|
67
|
-
/** The input of a request of `bound` tokens on `model` expected at `now` after `recent`: the cache of the request warmFrom
|
|
68
|
-
* picks at the cache-read price, and that request's input after the cache plus what the body grew by since at full
|
|
69
|
-
* price. Undefined (all of it uncached) when it picks none.
|
|
70
|
-
*
|
|
71
|
-
* Why a warm price opens no way past the cap. It is a guess: the request picked may be another conversation's whose
|
|
72
|
-
* bound happened to fit, or its cache may be gone. The guess lowers only what the request is held at (requestPrice's
|
|
73
|
-
* cost, never above its worst), never its worst: the whole bound uncached at the dearer of the input and cache-write
|
|
74
|
-
* prices whichever request was picked, the output limited to what it was admitted at. So a request's real cost is at
|
|
75
|
-
* most its hold plus its bound (Judged.boundUsd): a guess too low adds that much at most, the cache miss "expected" has
|
|
76
|
-
* always allowed and serve reports as the run's overshoot bound. The bound is counted wherever the run's room is read
|
|
77
|
-
* while the request is out (the wrap-up's gate and note count it at hold + bound, sessionSpendOf; a claim lost with it
|
|
78
|
-
* open books it cut at both); a CLI's own request admitted with its host's keep-back is limited so its worst leaves the
|
|
79
|
-
* keep-back free (rule.ts judgeModel), so even a miss stays under the cap; and once a request answers, its real cost
|
|
80
|
-
* is booked and the next admission reads it. "strict" reads no cache: every request held at its worst. */
|
|
81
|
-
export function expectedInput(bound, recent, now, model) {
|
|
82
|
-
const from = warmFrom(bound, recent, now, model);
|
|
83
|
-
return from && { cached: from.cachedTokens, fresh: from.inputTokens + (bound - from.boundTokens) };
|
|
84
|
-
}
|
|
85
|
-
/** The session's recent requests once `ended` has answered: `ended`, and those that ended under CACHE_WARM_MS before it
|
|
86
|
-
* but one on its model, measured its way and of its bound (`ended` is the newer of the two); the newest
|
|
87
|
-
* RECENT_REQUESTS_KEPT. None is dropped for being smaller or bigger than `ended`: warmFrom picks for each request. */
|
|
88
|
-
export function recentAfter(recent, ended) {
|
|
89
|
-
const same = (one) => one.model === ended.model && (one.counted === true) === (ended.counted === true) && one.boundTokens === ended.boundTokens;
|
|
90
|
-
return [...recent.filter((one) => ended.endedAt - one.endedAt < CACHE_WARM_MS && !same(one)), ended].slice(-RECENT_REQUESTS_KEPT);
|
|
91
|
-
}
|
|
92
|
-
/** A request priced under `mode`: `cost(output)`, what it is held at (its expected input, never more than uncached), and
|
|
93
|
-
* `worst(output)`, its input uncached; each with its hosted tools' most fee. */
|
|
94
|
-
export function requestPrice(pricing, bound, recent, now, mode, model, feeUsd = 0) {
|
|
95
|
-
const worst = (output) => pricing({ cached: 0, fresh: bound }, output) + feeUsd;
|
|
96
|
-
const parts = mode === "strict" ? undefined : expectedInput(bound, recent, now, model);
|
|
97
|
-
if (!parts)
|
|
98
|
-
return { cost: worst, worst };
|
|
99
|
-
return { cost: (output) => Math.min(pricing(parts, output) + feeUsd, worst(output)), worst };
|
|
43
|
+
/** A request of `bound` input tokens at its worst case: what it costs at `output` tokens, its hosted tools' most fee
|
|
44
|
+
* included. It is held at this, so it is booked at no more, answered, missed or cut. */
|
|
45
|
+
export function requestPrice(pricing, bound, feeUsd = 0) {
|
|
46
|
+
return (output) => pricing(bound, output) + feeUsd;
|
|
100
47
|
}
|
|
101
48
|
/** A request's whole input bound: its measured bytes, what its provider adds for what it carries, and what its hosted tools
|
|
102
49
|
* read. Undefined when its bytes were not measured. */
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { CapMark } from "./marks.js";
|
|
2
2
|
/** What the marks say: the cap's spend (null: unknown), the run's reported cost (null once any step's own cost is unknown,
|
|
3
|
-
* as a step a crash cut),
|
|
3
|
+
* as a step a crash cut), and how many requests were sent (a refused one is none). */
|
|
4
4
|
export type Spend = {
|
|
5
5
|
spentUsd: number | null;
|
|
6
6
|
reportedUsd: number | null;
|
|
7
|
-
boundUsd: number;
|
|
8
7
|
requests: number;
|
|
9
8
|
};
|
|
10
9
|
export declare function spendOf(marks: readonly CapMark[]): Spend;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export function spendOf(marks) {
|
|
2
2
|
let spent = 0;
|
|
3
3
|
let reported = 0;
|
|
4
|
-
let bound = 0;
|
|
5
4
|
let requests = 0;
|
|
6
5
|
const add = (usd, known) => {
|
|
7
6
|
spent = spent === null || usd === null ? null : spent + usd;
|
|
@@ -14,9 +13,8 @@ export function spendOf(marks) {
|
|
|
14
13
|
/** Ends read before any start of their id (marks that fall out of order): each closes the start that comes after it. */
|
|
15
14
|
const early = new Set();
|
|
16
15
|
const cut = (start) => {
|
|
17
|
-
// A crash cut it: it counts at what it was held at, and no one knows what it cost.
|
|
16
|
+
// A crash cut it: it counts at what it was held at (its worst case), and no one knows what it cost.
|
|
18
17
|
add(start.heldUsd, false);
|
|
19
|
-
bound += start.boundUsd ?? 0;
|
|
20
18
|
};
|
|
21
19
|
for (const mark of marks) {
|
|
22
20
|
if (mark.kind === "start") {
|
|
@@ -49,7 +47,7 @@ export function spendOf(marks) {
|
|
|
49
47
|
}
|
|
50
48
|
for (const start of open.values())
|
|
51
49
|
cut(start);
|
|
52
|
-
return { spentUsd: spent, reportedUsd: reported,
|
|
50
|
+
return { spentUsd: spent, reportedUsd: reported, requests };
|
|
53
51
|
}
|
|
54
52
|
/** A run's own priced answers from its marks, as the live cap counts them: each main-model request (never a side call or
|
|
55
53
|
* a paid call) whose end is an answer with a cost above 0, with its model. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ModelCapRequest, ToolCapRequest } from "./types.js";
|
|
2
2
|
/** What a judgement reads: the cap (read before this check), the run's spend (null: unknown) and its holds (null:
|
|
3
3
|
* unknown). `keepUsd`: what the host keeps back beside a model request for the steps it may still ask for while it
|
|
4
4
|
* streams (serve's wrap-up reserve: one typical step and one least answer); judgeModel says how it limits the output. */
|
|
@@ -6,28 +6,23 @@ export type JudgeState = {
|
|
|
6
6
|
capUsd: number;
|
|
7
7
|
spentUsd: number | null;
|
|
8
8
|
heldUsd: number | null;
|
|
9
|
-
mode: CapMode;
|
|
10
|
-
now: number;
|
|
11
9
|
keepUsd?: number;
|
|
12
10
|
};
|
|
13
11
|
/** A money figure the cap can count: a finite number of 0 or more. Anything else (null, NaN, Infinity, below 0) is
|
|
14
12
|
* unknown: it never reads as $0 and never frees room. */
|
|
15
13
|
export declare const isFigure: (usd: unknown) => usd is number;
|
|
16
|
-
/** A step admitted: what it is held at
|
|
17
|
-
* thinking budget, and what it counts at if stopped having streamed that many tokens (within its hold). */
|
|
14
|
+
/** A step admitted: what it is held at (its worst case: no answer, miss or cut books it past this), the output it may be
|
|
15
|
+
* sent with and its thinking budget, and what it counts at if stopped having streamed that many tokens (within its hold). */
|
|
18
16
|
export type Judged = {
|
|
19
17
|
heldUsd: number;
|
|
20
|
-
boundUsd: number;
|
|
21
|
-
worstUsd: number;
|
|
22
18
|
outputTokensMax?: number;
|
|
23
19
|
thinkingBudget?: number;
|
|
24
20
|
stoppedUsd(streamedTokens: number): number;
|
|
25
21
|
};
|
|
26
|
-
/** A model request priced: its output asked and least, and its
|
|
27
|
-
export declare function priced(request: ModelCapRequest
|
|
22
|
+
/** A model request priced: its output asked and least, and its worst case at an output (price.ts requestPrice). */
|
|
23
|
+
export declare function priced(request: ModelCapRequest): {
|
|
28
24
|
asked: number | undefined;
|
|
29
|
-
cost: (
|
|
30
|
-
worst: (output: number) => number;
|
|
25
|
+
cost: (_output: number) => number;
|
|
31
26
|
least: number;
|
|
32
27
|
} | undefined;
|
|
33
28
|
/** The one rule: admits `request` under `state`, or throws CapRefused. */
|
|
@@ -38,11 +33,10 @@ export declare const capRule: {
|
|
|
38
33
|
};
|
|
39
34
|
/** A main-model request's figures as the cap prices it, for what a run keeps back to answer: its least (an answer at the
|
|
40
35
|
* fewest output tokens the cap admits it at), its step-0 typical cost (its input and a WRAP_UP_STEP0_ANSWER_TOKENS answer,
|
|
41
|
-
* the wrap-up's own)
|
|
42
|
-
export declare function requestFigures(request: ModelCapRequest
|
|
36
|
+
* the wrap-up's own). Undefined when it cannot be priced. */
|
|
37
|
+
export declare function requestFigures(request: ModelCapRequest): {
|
|
43
38
|
leastUsd: number;
|
|
44
39
|
step0Usd: number;
|
|
45
|
-
worstUsd: number;
|
|
46
40
|
} | undefined;
|
|
47
41
|
/** What the wrap-up reads: the cap, spend and holds as admission counts them, the run's own priced answers on the
|
|
48
42
|
* request's model, and whether it was told. */
|
|
@@ -52,8 +46,6 @@ export type NoteState = {
|
|
|
52
46
|
heldUsd: number;
|
|
53
47
|
costs: readonly number[];
|
|
54
48
|
told: boolean;
|
|
55
|
-
mode: CapMode;
|
|
56
|
-
now: number;
|
|
57
49
|
};
|
|
58
50
|
/** The wrap-up for `request`, the run's next main-model request measured before the note is placed: due under three times
|
|
59
51
|
* the typical cost (step 0: its input and a 4,096-token answer) or twice its least, not yet told, never for a side call,
|
package/dist/kernel/cap/rule.js
CHANGED
|
@@ -10,17 +10,25 @@ import { noCeiling, noPrice, noToolPrice, paidRefusal, roomRefusal, unknownHeld,
|
|
|
10
10
|
/** A money figure the cap can count: a finite number of 0 or more. Anything else (null, NaN, Infinity, below 0) is
|
|
11
11
|
* unknown: it never reads as $0 and never frees room. */
|
|
12
12
|
export const isFigure = (usd) => typeof usd === "number" && Number.isFinite(usd) && usd >= 0;
|
|
13
|
-
/** A model request priced: its output asked and least, and its
|
|
14
|
-
export function priced(request
|
|
13
|
+
/** A model request priced: its output asked and least, and its worst case at an output (price.ts requestPrice). */
|
|
14
|
+
export function priced(request) {
|
|
15
|
+
// A plan's request costs the run nothing at any output: its least, its step-0 figure and its worst are $0.
|
|
16
|
+
if (request.model.plan)
|
|
17
|
+
return { asked: outputAsked(request), cost: (_output) => 0, least: 0 };
|
|
15
18
|
const bound = inputBound(request);
|
|
16
19
|
const price = request.model.price;
|
|
17
20
|
if (bound === undefined || price === null)
|
|
18
21
|
return undefined;
|
|
19
22
|
const asked = outputAsked(request);
|
|
20
|
-
const
|
|
21
|
-
return { asked, cost,
|
|
23
|
+
const cost = requestPrice(pricingOf(price), bound, request.feeUsd ?? 0);
|
|
24
|
+
return { asked, cost, least: asked === undefined ? 0 : outputLeast(request, asked) };
|
|
22
25
|
}
|
|
23
|
-
function judgeModel(request, { capUsd, spentUsd: spent, heldUsd: held,
|
|
26
|
+
function judgeModel(request, { capUsd, spentUsd: spent, heldUsd: held, keepUsd }) {
|
|
27
|
+
// A request on a person's subscription plan (CapModel.plan) is billed to the plan, never in dollars: held at $0, its
|
|
28
|
+
// output never limited (the plan's own limits apply), and admitted at any known spend, even one at the cap. Whatever else the run pays for (a
|
|
29
|
+
// paid tool, a model on a key) is judged as ever, so the cap still holds every dollar.
|
|
30
|
+
if (request.model.plan)
|
|
31
|
+
return { heldUsd: 0, stoppedUsd: () => 0 };
|
|
24
32
|
if (inputBound(request) === undefined)
|
|
25
33
|
throw new CapRefused(unmeasured(capUsd), { forGood: true, spentUsd: spent });
|
|
26
34
|
const price = request.model.price;
|
|
@@ -28,38 +36,29 @@ function judgeModel(request, { capUsd, spentUsd: spent, heldUsd: held, mode, now
|
|
|
28
36
|
throw new CapRefused(noPrice(capUsd), { forGood: true, spentUsd: spent });
|
|
29
37
|
if (request.model.outputCeiling === null && price.output > 0)
|
|
30
38
|
throw new CapRefused(noCeiling(capUsd), { forGood: true, spentUsd: spent });
|
|
31
|
-
const p = priced(request
|
|
39
|
+
const p = priced(request);
|
|
32
40
|
const left = capUsd - spent - held;
|
|
33
|
-
// The output it is sent with
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
// Why
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
// (The keep-back is kept with a float's tail to spare,
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
// here, so the request's real cost is at most its worst: its whole bound uncached at the dearer input price plus
|
|
45
|
-
// that output. The first branch admits only a worst that leaves the keep-back free, so a full miss still lands under
|
|
46
|
-
// the cap. Its real cost is then booked at the turn, so the next request is judged on what this one cost, never on
|
|
47
|
-
// its hold. Only the second branch (no keep-back, or a run wrapping up) admits on the hold, and there a miss passes
|
|
48
|
-
// the cap by at most its bound, the overshoot expected mode states; strict mode reads no cache and has none.
|
|
49
|
-
const kept = isFigure(keepUsd) && keepUsd > 0 ? mostOutput(p.worst, p.least, p.asked ?? 0, left - keepUsd - 2 * FIT_TOLERANCE) : undefined;
|
|
41
|
+
// The output it is sent with, clamped so its worst case fits: its whole input bound uncached and that output. With a
|
|
42
|
+
// keep-back from its host, first the most that leaves `keepUsd` free: a tool call the request makes while it streams
|
|
43
|
+
// then finds room for the steps after it (serve's wrap-up, which counts the request at its hold until it is booked).
|
|
44
|
+
// When even its least and the keep-back do not fit, the run is wrapping up and this request may be its answer: it gets
|
|
45
|
+
// the most what is left buys, as with no keep-back. Below its least, it is refused and the run parks for a raise.
|
|
46
|
+
// Why no request can pass the cap: every branch admits only an output whose worst case is at most what is left (the
|
|
47
|
+
// first at most what is left less the keep-back). The output is limited to that when sent, so whatever the cache did,
|
|
48
|
+
// and if it is cut with no usage, the request is booked at no more than its hold, which counts in the run's records
|
|
49
|
+
// while it is out; a stop counts what streamed, within the hold. (The keep-back is kept with a float's tail to spare,
|
|
50
|
+
// so the host's sum of the same figures never reads it as passed.)
|
|
51
|
+
const kept = isFigure(keepUsd) && keepUsd > 0 ? mostOutput(p.cost, p.least, p.asked ?? 0, left - keepUsd - 2 * FIT_TOLERANCE) : undefined;
|
|
50
52
|
const output = kept ?? mostOutput(p.cost, p.least, p.asked ?? 0, left);
|
|
51
53
|
if (output === undefined) {
|
|
52
|
-
const { text, figures } = roomRefusal(capUsd, spent, held, p.cost(p.least)
|
|
54
|
+
const { text, figures } = roomRefusal(capUsd, spent, held, p.cost(p.least));
|
|
53
55
|
throw new CapRefused(text, { forGood: false, spentUsd: spent, figures });
|
|
54
56
|
}
|
|
55
57
|
const heldUsd = p.cost(output);
|
|
56
|
-
const worstUsd = p.worst(output);
|
|
57
58
|
const stoppedPrice = (streamed) => p.cost(Math.min(output, streamed + STOP_ALLOWANCE_TOKENS));
|
|
58
59
|
const thinking = thinkingWithin(request, output);
|
|
59
60
|
return {
|
|
60
61
|
heldUsd,
|
|
61
|
-
boundUsd: Math.max(0, worstUsd - heldUsd),
|
|
62
|
-
worstUsd,
|
|
63
62
|
...(p.asked === undefined ? {} : { outputTokensMax: output }),
|
|
64
63
|
...(thinking === undefined ? {} : { thinkingBudget: thinking }),
|
|
65
64
|
stoppedUsd: (streamed) => Math.min(heldUsd, stoppedPrice(streamed)),
|
|
@@ -75,7 +74,7 @@ function judgeTool(request, { capUsd, spentUsd: spent, heldUsd: held }) {
|
|
|
75
74
|
const { text, figures } = paidRefusal(capUsd, spent, held, request.name, request.holdUsd, request.basis);
|
|
76
75
|
throw new CapRefused(text, { forGood: false, spentUsd: spent, figures });
|
|
77
76
|
}
|
|
78
|
-
return { heldUsd: request.holdUsd,
|
|
77
|
+
return { heldUsd: request.holdUsd, stoppedUsd: () => request.holdUsd };
|
|
79
78
|
}
|
|
80
79
|
/** The one rule: admits `request` under `state`, or throws CapRefused. */
|
|
81
80
|
function judge(request, state) {
|
|
@@ -91,20 +90,21 @@ function judge(request, state) {
|
|
|
91
90
|
export const capRule = { judge };
|
|
92
91
|
/** A main-model request's figures as the cap prices it, for what a run keeps back to answer: its least (an answer at the
|
|
93
92
|
* fewest output tokens the cap admits it at), its step-0 typical cost (its input and a WRAP_UP_STEP0_ANSWER_TOKENS answer,
|
|
94
|
-
* the wrap-up's own)
|
|
95
|
-
export function requestFigures(request
|
|
96
|
-
const p = priced(request
|
|
93
|
+
* the wrap-up's own). Undefined when it cannot be priced. */
|
|
94
|
+
export function requestFigures(request) {
|
|
95
|
+
const p = priced(request);
|
|
97
96
|
if (!p)
|
|
98
97
|
return undefined;
|
|
99
|
-
return { leastUsd: p.cost(p.least), step0Usd: p.cost(Math.min(p.asked ?? 0, WRAP_UP_STEP0_ANSWER_TOKENS))
|
|
98
|
+
return { leastUsd: p.cost(p.least), step0Usd: p.cost(Math.min(p.asked ?? 0, WRAP_UP_STEP0_ANSWER_TOKENS)) };
|
|
100
99
|
}
|
|
101
100
|
/** The wrap-up for `request`, the run's next main-model request measured before the note is placed: due under three times
|
|
102
101
|
* the typical cost (step 0: its input and a 4,096-token answer) or twice its least, not yet told, never for a side call,
|
|
103
102
|
* and never when the request fits what is left only without the note's bytes. */
|
|
104
103
|
export function noteFor(request, state) {
|
|
105
|
-
|
|
104
|
+
// A plan's request is never stopped by the cap, so the model on it has nothing to wrap up for.
|
|
105
|
+
if (request.side || request.model.plan || state.told || state.capUsd === undefined || state.spentUsd === null)
|
|
106
106
|
return undefined;
|
|
107
|
-
const p = priced(request
|
|
107
|
+
const p = priced(request);
|
|
108
108
|
if (!p)
|
|
109
109
|
return undefined;
|
|
110
110
|
const least = p.cost(p.least);
|
|
@@ -113,7 +113,7 @@ export function noteFor(request, state) {
|
|
|
113
113
|
if (!due)
|
|
114
114
|
return undefined;
|
|
115
115
|
const words = wrapUpWords(due.leftUsd, state.costs.length === 0);
|
|
116
|
-
const noted = priced({ ...request, inputTokens: (request.inputTokens ?? 0) + Buffer.byteLength(words, "utf8") }
|
|
116
|
+
const noted = priced({ ...request, inputTokens: (request.inputTokens ?? 0) + Buffer.byteLength(words, "utf8") });
|
|
117
117
|
if (noted && noted.cost(noted.least) > due.leftUsd + FIT_TOLERANCE && least <= due.leftUsd + FIT_TOLERANCE)
|
|
118
118
|
return undefined;
|
|
119
119
|
return { words, leftUsd: due.leftUsd };
|
|
@@ -2,7 +2,7 @@ import { type RunSessions } from "../../run-booking.js";
|
|
|
2
2
|
import { type BeforeRequest } from "../../runner.js";
|
|
3
3
|
import type { SessionRecord, SessionWriter } from "../../types.js";
|
|
4
4
|
import { type PricedRequest } from "./port.js";
|
|
5
|
-
import type { Admission,
|
|
5
|
+
import type { Admission, CapModel, DecisionCapRequest, ModelCapRequest, Settled } from "./types.js";
|
|
6
6
|
/** The session a run's workflow decisions book under. */
|
|
7
7
|
export declare const DECISION_SESSION = "decisions";
|
|
8
8
|
export interface RunCapOptions {
|
|
@@ -10,12 +10,8 @@ export interface RunCapOptions {
|
|
|
10
10
|
readonly sessions: RunSessions;
|
|
11
11
|
/** The run's cap, read before every check, so a raise holds from the next one. Undefined: no cap. */
|
|
12
12
|
capUsd(): number | undefined;
|
|
13
|
-
readonly mode?: CapMode;
|
|
14
|
-
readonly now?: () => number;
|
|
15
13
|
/** The run's model requests' models, by id and route (BeforeRequestOptions.modelOf; its decisions' are the cap's own). */
|
|
16
14
|
modelOf(id: string, route?: string): CapModel | undefined;
|
|
17
|
-
/** The session's recent requests, by the id their model is priced under (BeforeRequestOptions.recentOf). */
|
|
18
|
-
recentOf?(model: string): readonly RecentRequest[];
|
|
19
15
|
/** The run's priced tools' charges and holds beside its requests (beforeRequestOf's), when it has any. */
|
|
20
16
|
spentUsd?(): number | null;
|
|
21
17
|
heldUsd?(): number | null;
|
|
@@ -24,6 +20,17 @@ export interface RunCapOptions {
|
|
|
24
20
|
/** Told each request the rule judged, with the run's totals it was asked with, and its answer (an observer: a live
|
|
25
21
|
* view of the cap, a test's record). */
|
|
26
22
|
judged?(request: Parameters<BeforeRequest>[0], answer: Awaited<ReturnType<BeforeRequest>>): void;
|
|
23
|
+
/** The most model requests on free models (free.ts freeModel) the run makes, capped or not: the next one past it is
|
|
24
|
+
* refused for good, in freeCeilingEnd's words. Its paid tool calls, its decisions and ChatGPT-plan requests never
|
|
25
|
+
* count. Undefined: no such limit. */
|
|
26
|
+
freeRequests?: number;
|
|
27
|
+
/** Whether the run has parked (its host's pause): a model request waiting for room (runBooking) leaves the line then,
|
|
28
|
+
* refused in its own words, so nothing more is sent until a raise. */
|
|
29
|
+
parked?(): boolean;
|
|
30
|
+
/** How long a request waits for room with nothing it waits on settling (runBooking's WAIT_STALL_MS by default). */
|
|
31
|
+
waitStallMs?: number;
|
|
32
|
+
/** The run's stop: a step waiting for room (a paid call's or a decision's as a model request's) leaves at it, unsent. */
|
|
33
|
+
stopped?: AbortSignal;
|
|
27
34
|
}
|
|
28
35
|
/** A run's decisions on its one cap: each admitted before it is sent and settled once it answered. */
|
|
29
36
|
export interface RunDecisions {
|
|
@@ -39,8 +46,8 @@ export interface RunDecisions {
|
|
|
39
46
|
export interface RunCap {
|
|
40
47
|
/** The run's one rule for every model request (a model booking's beforeRequest), its decisions' models known to it. */
|
|
41
48
|
readonly before: BeforeRequest;
|
|
42
|
-
/** A model request as `before` prices it (port.ts capRequestOf, with the same models
|
|
43
|
-
*
|
|
49
|
+
/** A model request as `before` prices it (port.ts capRequestOf, with the same models): what the wrap-up's note and the
|
|
50
|
+
* reserve (requestFigures) price, so they price it as admission does. */
|
|
44
51
|
asked(request: PricedRequest): ModelCapRequest;
|
|
45
52
|
/** The run's decisions, booked on `writer` (the run's DECISION_SESSION) under the same rule. `flush`: makes what the
|
|
46
53
|
* journal holds survive a lost box, awaited once a decision's hold is written and before it is sent, as a model
|
|
@@ -62,7 +69,9 @@ export interface RunTools {
|
|
|
62
69
|
* for it, named there as `charged_by`; `tool` and `tag`, the tool whose charge it is and its session (ModelRequest.paid);
|
|
63
70
|
* `owed`, money its provider is already owed: held and booked, never refused. `hold`: a hold-only step: money
|
|
64
71
|
* reserved, not spent; given back on a lost claim, never booked as cut. `ref`: the one booking it names: a ref already
|
|
65
|
-
* booked (on the run's records, or on this claim) books nothing again, and the answer says `booked`.
|
|
72
|
+
* booked (on the run's records, or on this claim) books nothing again, and the answer says `booked`. `under`: the key
|
|
73
|
+
* of the model session whose tool call it runs in (ModelRequest.paid.under): it is judged beside that session's
|
|
74
|
+
* requests in flight and never waits on them, since one of them may be the request that asked for it. */
|
|
66
75
|
admit(request: {
|
|
67
76
|
readonly id: string;
|
|
68
77
|
readonly name: string;
|
|
@@ -75,6 +84,7 @@ export interface RunTools {
|
|
|
75
84
|
readonly owed?: true;
|
|
76
85
|
readonly hold?: true;
|
|
77
86
|
readonly ref?: string;
|
|
87
|
+
readonly under?: string;
|
|
78
88
|
}): Promise<Admission & {
|
|
79
89
|
readonly booked?: true;
|
|
80
90
|
}>;
|
|
@@ -88,11 +98,9 @@ export interface RunTools {
|
|
|
88
98
|
}
|
|
89
99
|
/** The session a run's paid tool calls book under. */
|
|
90
100
|
export declare const TOOL_SESSION = "paid-tools";
|
|
91
|
-
/** How a run ends once its booked spend passed its cap (overCapEnd), read from its model session's `records`
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* within that bound (a cut request's possible one included) pauses the run instead. Undefined while it fits, by such a
|
|
95
|
-
* miss, or with a spend unknown. */
|
|
101
|
+
/** How a run ends once its booked spend passed its cap (overCapEnd), read from its model session's `records` (each
|
|
102
|
+
* request at the figure the cap counts it at, sessionSpendOf's: one cut by a crash at its hold) and what its other
|
|
103
|
+
* sessions spent (`othersUsd`). Undefined while it fits, or with a spend unknown. */
|
|
96
104
|
export declare function overCapOf(records: readonly SessionRecord[], othersUsd: number, capUsd: number): string | undefined;
|
|
97
105
|
export declare function runCap(options: RunCapOptions): RunCap;
|
|
98
106
|
/** A run's paid tool calls booked on `writer` (its TOOL_SESSION) under `before`, the run's one rule (runCap's, or the
|