@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.
Files changed (67) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/NOTICE +2 -0
  3. package/dist/booking.d.ts +25 -6
  4. package/dist/booking.js +15 -4
  5. package/dist/host/index.d.ts +4 -3
  6. package/dist/host/index.js +3 -2
  7. package/dist/host/o200k.d.ts +11 -0
  8. package/dist/host/o200k.js +185 -0
  9. package/dist/host/overhead.d.ts +72 -12
  10. package/dist/host/overhead.js +220 -38
  11. package/dist/host/proxy.d.ts +85 -7
  12. package/dist/host/proxy.js +374 -87
  13. package/dist/index.d.ts +6 -6
  14. package/dist/index.js +4 -4
  15. package/dist/kernel/cap/admit.d.ts +5 -8
  16. package/dist/kernel/cap/admit.js +13 -19
  17. package/dist/kernel/cap/free.d.ts +43 -0
  18. package/dist/kernel/cap/free.js +71 -0
  19. package/dist/kernel/cap/index.d.ts +6 -5
  20. package/dist/kernel/cap/index.js +5 -4
  21. package/dist/kernel/cap/marks.d.ts +2 -3
  22. package/dist/kernel/cap/park.d.ts +3 -4
  23. package/dist/kernel/cap/park.js +4 -5
  24. package/dist/kernel/cap/port.d.ts +8 -13
  25. package/dist/kernel/cap/port.js +10 -18
  26. package/dist/kernel/cap/price.d.ts +10 -49
  27. package/dist/kernel/cap/price.js +15 -68
  28. package/dist/kernel/cap/recover.d.ts +1 -2
  29. package/dist/kernel/cap/recover.js +2 -4
  30. package/dist/kernel/cap/rule.d.ts +8 -16
  31. package/dist/kernel/cap/rule.js +35 -35
  32. package/dist/kernel/cap/run-cap.d.ts +21 -13
  33. package/dist/kernel/cap/run-cap.js +74 -36
  34. package/dist/kernel/cap/types.d.ts +11 -31
  35. package/dist/kernel/cap/words.d.ts +13 -8
  36. package/dist/kernel/cap/words.js +23 -14
  37. package/dist/kernel/cap/wrap-up.d.ts +4 -0
  38. package/dist/kernel/cap/wrap-up.js +5 -1
  39. package/dist/kernel/registry/computer-surface.generated.d.ts +1 -1
  40. package/dist/kernel/registry/computer-surface.generated.js +9 -25
  41. package/dist/kernel/registry/computer.d.ts +14 -7
  42. package/dist/kernel/registry/computer.js +52 -12
  43. package/dist/kernel/registry/gate.d.ts +2 -2
  44. package/dist/kernel/registry/gate.js +4 -7
  45. package/dist/kernel/registry/hands.d.ts +3 -4
  46. package/dist/kernel/registry/index.d.ts +1 -1
  47. package/dist/kernel/registry/index.js +1 -1
  48. package/dist/kernel/registry/registry.js +6 -9
  49. package/dist/process/recovery.d.ts +4 -1
  50. package/dist/process/recovery.js +15 -3
  51. package/dist/process/resume.d.ts +5 -2
  52. package/dist/process/resume.js +15 -3
  53. package/dist/process/tools.d.ts +26 -1
  54. package/dist/process/tools.js +47 -4
  55. package/dist/run-booking.d.ts +21 -2
  56. package/dist/run-booking.js +215 -18
  57. package/dist/runner.d.ts +82 -21
  58. package/dist/runner.js +176 -59
  59. package/dist/testing.d.ts +4 -2
  60. package/dist/testing.js +2 -2
  61. package/dist/tool-deferral.d.ts +3 -3
  62. package/dist/tool-deferral.js +4 -4
  63. package/dist/types.d.ts +70 -14
  64. package/dist/types.js +8 -0
  65. package/package.json +5 -3
  66. package/vendor/o200k/LICENSE +61 -0
  67. package/vendor/o200k/o200k_base.tiktoken +199998 -0
@@ -1,12 +1,8 @@
1
- // The kernel cap's one pricing rule, the same for every harness. A request's input bound (its measured bytes, what its
2
- // provider adds, what its hosted tools read) is priced at the dearer of the input and cache-write prices; under "expected"
3
- // the cache of one of the session's recent requests (warmFrom) counts at the cache-read price, so only a cache miss can
4
- // pass the cap, by at most the request's bound; under "strict" every token uncached. Its output is held at what it is
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: a request's cached input at the cache-read rate, its fresh input at the dearer of
37
- * the input and cache-write rates, and its output at the output rate, each at the rates of the tier its whole input
38
- * passes (ratesFor). A hold, a worst case and a booking the cap prices are so never under what the provider bills. */
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 (parts, output) => {
41
- const rates = ratesFor(price, parts.cached + parts.fresh);
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 (parts.cached * read + parts.fresh * fresh + output * rates.output) / 1e6;
40
+ return (inputTokens * fresh + output * rates.output) / 1e6;
45
41
  };
46
42
  }
47
- /** How many of a session's recent requests it keeps (recentAfter), the newest: enough for a conversation's last request
48
- * to outlast the smaller requests a CLI sends between two of its own. Fewer only prices a request colder, never under. */
49
- export const RECENT_REQUESTS_KEPT = 16;
50
- /** The recent request whose cache a request of `bound` tokens on `model` is expected to read at `now`: of `recent`, those
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), the bound of the steps still open, and how many requests were sent (a refused one is none). */
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, boundUsd: bound, requests };
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 { CapMode, ModelCapRequest, ToolCapRequest } from "./types.js";
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, the most a cache miss adds, its worst case, the output it may be sent with and its
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 cost and worst case at an output. */
27
- export declare function priced(request: ModelCapRequest, mode: CapMode, now: number): {
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: (output: number) => number;
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) and its worst (its full asked output, its bound included). Undefined when it cannot be priced. */
42
- export declare function requestFigures(request: ModelCapRequest, mode: CapMode, now: number): {
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,
@@ -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 cost and worst case at an output. */
14
- export function priced(request, mode, now) {
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 { cost, worst } = requestPrice(pricingOf(price), bound, request.recent, now, mode, request.model.id, request.feeUsd ?? 0);
21
- return { asked, cost, worst, least: asked === undefined ? 0 : outputLeast(request, asked) };
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, mode, now, keepUsd }) {
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, mode, now);
39
+ const p = priced(request);
32
40
  const left = capUsd - spent - held;
33
- // The output it is sent with. With a keep-back from its host, first the most whose WORST case (its hold and the cache
34
- // miss its bound covers, as a request in flight counts in the run's records) leaves `keepUsd` free: a tool call the
35
- // request makes while it streams then finds room for the steps after it (serve's wrap-up, which counts the request
36
- // at its worst until it is booked). When even its least and the keep-back do not fit, the run is wrapping up and this
37
- // request may be its answer: it gets the most what is left buys, as with no keep-back.
38
- // Why the cap still holds: the first branch only lowers the output, never below the least: cost(out) <= worst(out) <=
39
- // left - keepUsd <= left. The second is the rule as it was. So every hold is at most what is left, as before; the
40
- // hold, its bound, what a stop counts and what is booked are priced by the same p at the output sent, unchanged.
41
- // (The keep-back is kept with a float's tail to spare, so the host's sum of the same figures never reads it as passed.)
42
- // When the warm guess is wrong (expected mode prices the bound's head as cached from a recent request, and the cache
43
- // is gone): the hold is too low, and three things still bound the spend. The output is limited to what is sent
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), p.worst(p.least) - 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, boundUsd: 0, worstUsd: request.holdUsd, stoppedUsd: () => 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) and its worst (its full asked output, its bound included). Undefined when it cannot be priced. */
95
- export function requestFigures(request, mode, now) {
96
- const p = priced(request, mode, now);
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)), worstUsd: p.worst(p.asked ?? p.least) };
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
- if (request.side || state.told || state.capUsd === undefined || state.spentUsd === null)
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, state.mode, state.now);
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") }, state.mode, state.now);
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, CapMode, CapModel, DecisionCapRequest, ModelCapRequest, RecentRequest, Settled } from "./types.js";
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 and the session's recent
43
- * requests): what the wrap-up's note and the reserve (requestFigures) price, so they read a warm cache as admission does. */
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` and what
92
- * its other sessions spent (`othersUsd`): each request at the figure the cap counts it at (sessionSpendOf's figures: one
93
- * cut by a crash at its hold plus its bound), and the bound of the requests since the spend last fit, so a cache miss
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