@parcha/agentrun-kernel 0.1.0-beta.36 → 0.1.0-beta.38
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 +91 -0
- package/dist/host/index.d.ts +2 -4
- package/dist/host/index.js +2 -3
- package/dist/host/overhead.d.ts +42 -0
- package/dist/host/overhead.js +84 -10
- package/dist/host/proxy.d.ts +34 -15
- package/dist/host/proxy.js +99 -25
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/kernel/cap/index.d.ts +4 -4
- package/dist/kernel/cap/index.js +3 -3
- package/dist/kernel/cap/port.d.ts +14 -3
- package/dist/kernel/cap/port.js +29 -16
- package/dist/kernel/cap/price.d.ts +31 -6
- package/dist/kernel/cap/price.js +49 -15
- package/dist/kernel/cap/rule.d.ts +11 -1
- package/dist/kernel/cap/rule.js +30 -3
- package/dist/kernel/cap/run-cap.d.ts +9 -2
- package/dist/kernel/cap/run-cap.js +8 -3
- package/dist/kernel/cap/types.d.ts +11 -5
- package/dist/kernel/registry/computer-surface.generated.d.ts +9 -0
- package/dist/kernel/registry/computer-surface.generated.js +618 -0
- package/dist/kernel/registry/computer.d.ts +14 -0
- package/dist/kernel/registry/computer.js +75 -0
- package/dist/kernel/registry/defs.d.ts +1 -4
- package/dist/kernel/registry/defs.js +1 -13
- package/dist/kernel/registry/handoff.js +4 -4
- package/dist/kernel/registry/hands.d.ts +22 -115
- package/dist/kernel/registry/hands.js +0 -2
- package/dist/kernel/registry/index.d.ts +4 -4
- package/dist/kernel/registry/index.js +5 -5
- package/dist/kernel/registry/registry.d.ts +4 -5
- package/dist/kernel/registry/registry.js +27 -29
- package/dist/kernel/types.d.ts +10 -2
- package/dist/process/tools.d.ts +5 -1
- package/dist/process/tools.js +62 -8
- package/dist/run-booking.d.ts +3 -0
- package/dist/run-booking.js +7 -0
- package/dist/runner.d.ts +21 -2
- package/dist/runner.js +126 -11
- package/dist/testing.d.ts +6 -2
- package/dist/testing.js +5 -3
- package/dist/tool-deferral.d.ts +12 -0
- package/dist/tool-deferral.js +17 -0
- package/dist/types.d.ts +83 -39
- package/package.json +2 -2
- package/dist/host/mcp-route.d.ts +0 -14
- package/dist/host/mcp-route.js +0 -125
- package/dist/kernel/registry/desktop.d.ts +0 -11
- package/dist/kernel/registry/desktop.js +0 -90
- package/dist/kernel/registry/phone.d.ts +0 -15
- package/dist/kernel/registry/phone.js +0 -135
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,97 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.38 (2026-11-14)
|
|
6
|
+
|
|
7
|
+
- A run's MCP tools are host tools on every harness: each call passes the run's one tool gate before anything is sent, and the runner calls the server with the credential, books the call and records it, as it does any host tool. opencode now runs MCP tools (`features.mcp_client.harnesses` lists it, and an opencode agent granted `mcp:<server>/<tool>` saves and runs). claude-agent offers a granted tool as `mcp__hands__mcp__<server>__<tool>` and codex as `mcp_<server>__<tool>` (Codex refuses a dynamic tool named `mcp__*`); the Timeline, tool settings and approvals still name it `mcp:<server>/<tool>`. A call set to ask on claude-agent, codex or opencode holds the CLI's call while the run waits (no "waits for a person's approval" result row while it is parked); on the person's allow the resumed session gets the call's own result, no `[tool approval]` message. A codex run no longer fails at its start when a granted server can't be reached: the server's error shows at the first call, as on pi. On a code-mode Codex model, a script's tool call held for a person's approval runs once on their allow, under its own call id, and the script's cut-off `exec` reads its result (or, when the `exec` had yielded, the model's `wait` on its cell); before, the resume told the model the call may have run, and the call the model made again asked again, so the allow never reached a call.
|
|
8
|
+
|
|
9
|
+
Who breaks: a run parked on an MCP call at the MCP door before this version does not resume its call: cancel it and start it again. A Codex thread started before this version loses its MCP tools when it resumes (Codex resumes a thread with the dynamic tools it started with): start a new run. Code built on the kernel's MCP door breaks too. Removed: `SessionSpec.mcp`, `SessionMcp`, `McpClient`, `AdapterCapabilities` and `AgentAdapter.capabilities()`, `AdapterFacts.mcp`, `ModelProxyOptions.mcp`, `mcpRoute`, `McpHandler` and `McpRouteOptions` (`@parcha/agentrun-kernel/host`), and `McpCallResult.parked`. Hand a session its MCP tools as host tools in `SessionSpec.tools` instead.
|
|
10
|
+
- A provider-native web search's fee is one constant, `NATIVE_SEARCH_USD` ($0.01, $10 per 1,000), instead of three copies. The price does not change.
|
|
11
|
+
- Kernel: `@parcha/agentrun-kernel/host` exports `NATIVE_SEARCH_USD`. The Claude and Codex proxy policies and the runner's hosted search all read it.
|
|
12
|
+
- `@parcha/agentrun-harness-claude` no longer exports `CLAUDE_WEB_SEARCH_USD`; read `NATIVE_SEARCH_USD` from `@parcha/agentrun-kernel/host`.
|
|
13
|
+
- An agent can open an app on a Linux desktop. Before, every app an agent started there closed right away, so a person watching the Screen tab on E2B or Agent37 saw a black desktop for the whole run. `computer.shell.run` passes no `DISPLAY`, and its command's guard ends everything the command started, `setsid` and `&` included. Two changes fix this:
|
|
14
|
+
- `computer.processes.start(command, { cwd, env })` (on a computer with the processes group) starts a program that keeps running after the call. That program, and any app a launcher forks, runs until it exits or the computer ends. It answers `{ pid }`. In a run it is held to bash's setting, as `bash_background` is, so a read-only bash policy refuses it.
|
|
15
|
+
- On a desktop whose hello names a display, the surface adds `DISPLAY` to the env of `shell.run` and `processes.start`, under the call's own variables. An app opens where the screen is, and a tool such as `xdotool` finds it.
|
|
16
|
+
- The desktop-computer skill's Linux section says how to open an app.
|
|
17
|
+
- One discovery system. A run lists every tool until its catalog tools (MCP and monid) pass 20; then they are deferred, found with `tools_search` and run with `tools_call`. Under 20 there is no search or call, and Claude Code's own tool search follows the same threshold and gets neither of ours. `codemode`, `tools_search` and `tools_call` each have a tool-settings key (allow by default); a tool set to deny is not offered at all.
|
|
18
|
+
- Every harness (pi, Claude Code, OpenCode and Codex) now offers three host tools over the run's own tools: `tools_search`, `tools_call` and `codemode`. Codex gets the first two and keeps its own code mode. With MCP servers on a Claude Code run, the CLI's own tool search is on.
|
|
19
|
+
- `tools_search {query, limit?}` ranks the run's tools by what they do (BM25 over each tool's description and argument words) and answers each match's name, description and input schema. It lists only tools the run may call: never one set to deny, and never the three tools themselves.
|
|
20
|
+
- `tools_call {name, args}` calls one of the run's tools by name. The call is asked about, recorded and booked as that tool's own call, under `<callId>/call`. An ask parks the run, and once approved the call runs once. A deny refuses it in the gate's words. A call of an unknown name, of `tools_call` itself, of `tools_search` or of `codemode` is refused in words, and nothing runs.
|
|
21
|
+
- `codemode {script}` runs a JavaScript script in a QuickJS sandbox. Its `await tools.<name>(args)` calls are each the run's own call, gated, recorded and booked under `<callId>/s<n>`. The sandbox has the tools and nothing else of the run: no environment, keys, files, network or timers.
|
|
22
|
+
- A call inside a script that needs approval follows Codex's rule. The run parks. Once a person answers, the script runs again: recorded calls answer from their records, the parked call runs once (or is refused), its outcome is the script's answer, and the rest of the script does not run.
|
|
23
|
+
- A call made through `tools_call` or a script answers what a direct call answers: its text and images to the model, and in a script its value (a tool's structured result, else its text). On pi the harness's own tools (an extension's, the browser's) are found and called the same way as the host's.
|
|
24
|
+
- A refused call inside a script ends the script with the refusal's words.
|
|
25
|
+
- After a crash, a call that was cut off is told as possibly done, and it is not run again.
|
|
26
|
+
- A paid call made through `tools_call` or a script books exactly as a direct call does: held before it is sent, under the cap, and settled once. The cap's wrap-up still refuses the model's own next call.
|
|
27
|
+
- The three tools' definitions ride on every model request: about 2,000 bytes, 639 tokens on Sonnet. So the smallest caps the README measures rise. A pi desktop run on Sonnet through a gateway now needs $0.07 for its first request and $0.13 for five steps in `strict` mode, or $0.07 in `expected` mode (it was $0.06, $0.12 and $0.06). Straight to Anthropic, it needs $0.04 for its first request, and five steps still need $0.09 in `strict` mode and $0.06 in `expected` mode.
|
|
28
|
+
- Claude Code: with an MCP server's tools on the run, the CLI runs with `ENABLE_TOOL_SEARCH=true` and its ToolSearch. The run's MCP tools reach the model through `hands` only once ToolSearch finds them, and every other tool of the session loads as before. The session's proxy admits the tool search's beta (`advanced-tool-use-2025-11-20`), ToolSearch and its placeholder. It also admits a `tool_reference` block, inside a tool result only, that names one of the session's own tools. Without MCP tools, all of these stay refused.
|
|
29
|
+
- A server started by `agentrun-serve` (and so by `npx agentrun`) can start a worker thread from a file. The launcher no longer passes Node `--input-type`, which a worker inherits and Node refuses. Before this, a `codemode` script there failed with "--input-type can only be used with string input via --eval, --print, or STDIN", and so did pi's code mode on a desktop or a phone.
|
|
30
|
+
- A capped run no longer prices a request as if nothing were cached after an approval, a deny, a raise or a smaller request of its own, and a Codex or Claude Code run no longer has a tool call refused with "Budget nearly spent" while the request that asked for it is still streaming and the cap has room for both.
|
|
31
|
+
- The recent requests: the cap keeps the session's answered requests of the last 4 minutes and prices each request from the one on its model with the biggest body no bigger than its own. A smaller request between two of the conversation's own (Codex sends one while a tool runs; a summary, a child's) no longer resets the history, so the conversation's next request stays warm. Before, a $0.50 Codex run's next request after one was priced all fresh, held all that was left, and was told "About $0.43 left of this run's budget: finish now" on a $0.08 job.
|
|
32
|
+
- Resume: the recent requests are kept with the run, so the first request after a new claim is priced from them, warm when they ended under 4 minutes ago and uncached after that, the same rule as inside one claim, on every harness. Before, a $0.15 Codex run paused after a deny with "needs at least $0.22": its next request was priced all fresh.
|
|
33
|
+
- In flight: a CLI harness's request is now sent with the most output whose worst case still leaves the run's wrap-up reserve (one typical step and one least answer) free, so a call it makes mid-response finds room. A $0.50 Codex request that held $0.36 and a $0.12 bound at its whole 32,000-token ask is now sent with fewer tokens, and its call passes. When the request's least and the reserve do not both fit, it is admitted as before, with the most what is left buys, and a call it makes is refused for the reserve as before. pi starts a tool only once its response is complete, so its requests are sized as before.
|
|
34
|
+
- The cap's guarantees are unchanged: no request is held past what is left, a warm price that guesses wrong costs at most the request's bound beyond its hold (as any cache miss under `expected`), every hold settles to what the request cost, and turns sum to the run's cost.
|
|
35
|
+
- Kernel cap (private, not in the package's index): `ModelCapRequest.previous` is `recent` (a list of `RecentRequest`, was `Previous`), `previousOf` on `beforeRequestOf` and `runCap` is `recentOf`, and `previousAfter` is `recentAfter`, with `warmFrom` picking the request a price reads. `JudgeState.keepUsd`, and `keepBackUsd` on `beforeRequestOf` and `runCap`, tell the rule what the host keeps back beside a request.
|
|
36
|
+
- A desktop with no ui group (E2B's, Agent37's) can be driven by the tools alone, and its first screen no longer reads as broken.
|
|
37
|
+
- `computer_observe` and `computer_act` with `app` open that app, or bring it in front, before the screenshot or the input (apps.launch, as a script's `computer.apps.open` does). A screenshot with no app in front lists the app ids to name. Before, a tools-only agent had no way to open an app there. Such an observe counts as an app opening. While the person holds the desktop it waits, then is not done. After a crash it is not run again.
|
|
38
|
+
- The desktop's background is one slate colour (xsetroot, run before xfwm4), not black. Agent37's desktop recipe (`box/build-agent37.mjs`) adds x11-xserver-utils for it; rebuild `agentrun-box` once.
|
|
39
|
+
- `computer_act` offers no `expect` on a desktop with no ui group, which can't check one: its schema there has none, so a model is no longer offered a wait that is then refused.
|
|
40
|
+
- `GET /v2/runs/{run_id}/computer` on an ended run says `session: "closed"` for its desktop or phone, and for a browser step the run ended under, not `opening`.
|
|
41
|
+
- A provider's label takes its article: "an E2B sandbox", not "a E2B sandbox".
|
|
42
|
+
- A capped Codex, OpenCode or pi (on OpenAI's Responses) run's next-step estimate is closer to its real input, so it pauses for a raise later and less often: the input bound counts the text the model reads (a tool's output, a message, a call's arguments) at its own bytes instead of its JSON-escaped ones, some 4 to 6% less on a code-mode body full of fetched pages, which keeps more requests under the 272k-token long-context tier where every rate doubles. On a route straight to OpenAI the input is counted exactly by OpenAI's `POST /v1/responses/input_tokens`, asked with only the credential and account headers and never through a gateway; a count that fails or takes over 3 seconds leaves the byte bound. The cap still never spends past itself: tool definitions and encrypted reasoning keep every byte.
|
|
43
|
+
- Three follow-ups to the tools_search / tools_call / codemode rule, found in a live walk.
|
|
44
|
+
- Claude Code now follows the same switch as every other harness. With `tools_search` or `tools_call` set to deny, its own tool search is off and every tool is listed; before, it deferred a run's catalog tools behind its own ToolSearch whatever the setting said. The server makes the one decision (`SessionSpec.deferCatalog`) and Claude Code's adapter obeys it.
|
|
45
|
+
- Settings → Tools lists `tools_search`, `tools_call` and `codemode`, each allow or deny, allow by default. They are in `capabilities.tools` with `meta: true` (no grant: the agent editor does not offer them) and a one-line blurb.
|
|
46
|
+
- codemode's description names real functions of the run (`tools.<name>`, the first three) and says to leave off the prefix a harness shows in the model's own tool list (`hands_`, `mcp__hands__`). On OpenCode a script wrote `tools.hands_mcp__hn__…` five times in five runs and failed.
|
|
47
|
+
- A capped run on a CLI harness (Codex, Claude Code) prices its next request from the cache its previous request left warm, at admission, for the wrap-up note and for the room it keeps back to answer, so it is no longer told to finish, or refused a tool call, while what is left still buys many of its steps.
|
|
48
|
+
- Before, a CLI's request names its model as its provider does (`openai/gpt-6.1-sol`, `claude-sonnet-5-5`) while the cap prices it under the run's model id (`gpt-6.1-sol`, `anthropic/claude-sonnet-5.5`). The session's previous request was filed under the first and looked up under the second, so every request was priced as if nothing were cached. The note and the reserve never read the previous request at all, on any harness.
|
|
49
|
+
- Example: a Codex run under $0.50 was told to finish with $0.45 left, because its next request was priced at $0.23 all fresh; read from the cache, it needed about $0.05.
|
|
50
|
+
- Admission's rule is unchanged: a request whose least does not fit what is left is refused. A request with no previous one in hand (a run's first, or one after a compaction) is priced uncached, as before. A CLI request on a warm cache is now held at its warm price, as pi's already was.
|
|
51
|
+
- Kernel: `capRequestOf` builds a request as the cap prices it (its model, and the session's previous request when it continues the conversation, looked up by the priced model's id). `RunCap.asked(request)` returns it with the run's own pricing. `beforeRequestOf` uses the same function.
|
|
52
|
+
- A long answer through an OpenAI Responses gateway that writes its JSON with spaces is priced, a request that answered while its runner's claim was lost is booked at its cost when the run resumes, and a run whose spend is unknown never reads as a full cap to its learning.
|
|
53
|
+
- Before, the model proxy read a long answer's usage (its terminal event longer than the 64 KiB it keeps) only when the gateway wrote `"usage":{` with no space. A Python gateway (LiteLLM's `json.dumps`) writes `"usage": {`, so such a request was booked at an unknown cost though the harness counted its tokens: the run's cost read unknown with nothing unmetered. pi on the gateway, Codex and OpenCode read the usage the same way and are all fixed. Kernel: `lastObjectOf(text, key)` reads the last `"<key>": {...}` object of a cut answer, whatever its spacing.
|
|
54
|
+
- Before, a runner that lost a run's claim (its lease lapsed while the box stalled) and took the run again in the same process booked a request that had answered meanwhile as cut, at an unknown cost: the lost claim's write of its cost was refused, so the resumed run read the request unanswered. The run's cost read unknown though the request reported its usage on a priced model. Kernel: `SessionWriter.scope` names a session across claims in one process (serve's `journalSessions` sets the run's id beside the key); a booking whose cost write is refused hands it to the next booking of the same scope, whose `bookCut` waits (at most 10 s) for one still in flight and writes it as it would have been written. A request with no price stays unpriced and counts for the cap at its hold. A process that died hands nothing on: its requests are booked cut, as before. A workflow run's model booking now takes them too: its claim books an earlier claim's requests before the workflow resumes, where before it never did, so a workflow's request that answered under a lost claim stayed counted at its hold.
|
|
55
|
+
- Before, a run cancelled or stopped while its model request was being admitted could still send that request: the proxy forwarded it after its client had left, nothing stopped it, and it was booked only once the provider ended it, after the run had ended, so the run read the request as cut at an unknown cost. The proxy now books such a request at nothing (never left) and never sends it, so a run cancelled before any request reached the model costs $0. A request that reached the model and was cut keeps the cut rule: unpriced, counted for the cap at its hold.
|
|
56
|
+
- Before, a capped run whose shown cost was unknown gave its learning grade a cap of $0 ("Not graded: no room under the cap.") and its look-back a reached cap. The grade's cap and the look-back now read the run's spend as its own cap gates read it: an answer with no price counts at the figure the cap held for it, never $0 and never the whole cap. Only when that figure is unknown too, the grade is not queued and says why: "Not graded: this run's spend is unknown, so its budget can't cover the grade."
|
|
57
|
+
|
|
58
|
+
## 0.1.0-beta.37 (2026-11-13)
|
|
59
|
+
|
|
60
|
+
- Every harness sees a desktop or a phone through one surface: the tools `computer_observe`, `computer_act`, `computer_screenshot` and `computer_script`, the same answers from a script, an MCP client or a tool call. Breaking: the `desktop_*` and `phone_*` tools are gone, replaced by the `computer_*` tools (a desktop's and a phone's alike, held to the `desktop` or `phone` setting), and the script API's `apps.open(id)` returns nothing; bind the app with `computer.app(id)`.
|
|
61
|
+
- `computer_script` runs JavaScript in an isolate in serve whose only binding is `computer` (no network, no keys, no timers); its types are `spec/computer/computer.d.ts`, generated with the tools' schemas by `scripts/generate-computer-surface.mjs` (`--check` in CI). A model reads them compactly (no comments, one line a declaration); `computer_act`'s schema types its steps with no words, and its description declares them as TypeScript.
|
|
62
|
+
- A script's `computer.shell` and `computer.files` are held to the run's tool settings as the `bash`, `write` and `read` tools are (a deny or a read-only bash policy refuses inside the script; a setting that asks refuses, since a script can't wait for a person), and a refused step throws in the script, so the steps after it never run. Without the ui group a scroll goes as wheel clicks (about 120 pixels each), which every driver takes.
|
|
63
|
+
- pi offers its code mode (`codemode`) beside its tools with a desktop or a phone, `computer` bound in its scripts; the call is held to the computer kind's setting.
|
|
64
|
+
- In Codex's code mode a script's call of a computer tool answers the tool's value as JSON, its images' data in it; any other tool answers its words and a line saying its images were not sent (in code mode an image reaches a script only as text). A call a script repeats after a restart, of a computer tool that completed, gets that same JSON again.
|
|
65
|
+
- agentrun-serve's `computerMcp` serves a run's computer tools to an MCP client, each answer with `structuredContent`.
|
|
66
|
+
- The desktop-computer and phone-computer skills teach the route: observe, act by ref, check, and one script for steps already known.
|
|
67
|
+
- A run near its cap is told to wrap up again, on the runner, for every harness (part of #2080).
|
|
68
|
+
- Once what is left of the cap is about one typical request, the run's next model request tells the model, inside that request, to finish now ("This run's budget is about $X, …"). The note goes to the model only and never into the run's transcript.
|
|
69
|
+
- It is worked out from everything the run has spent and holds, including requests still in flight. A Claude Code or Codex run whose requests overlap is told on time, not late or never.
|
|
70
|
+
- It is told once per cap. A raised cap is told again when the run comes near the new cap.
|
|
71
|
+
- It composes with the cap's wrap-up as one story: the note tells the model what is left, and once what is left no longer covers what the run keeps back to answer (one typical step and the least answer), its tools stop. The note also comes due on that same rule, so the model is told no later than its first refused call.
|
|
72
|
+
- Kernel: `requestFigures(request, mode, now)` in the cap gives a main-model request's least, step-0 typical and worst cost, which the run's reserve is priced from.
|
|
73
|
+
- The Timeline shows the note as AgentRun's own row ("AgentRun: This run's budget is about $X, …"), written only once a request carrying it has been admitted.
|
|
74
|
+
- Kernel: `ModelPort` and the model proxy's hooks gain `noteReturned?(note)`. The proxy calls it when a request that took the host's note leaves before its admission (its client gone, a refusal, an error), so the host can give the note to the next request at once.
|
|
75
|
+
- Kernel, a change for anyone reading or implementing `ModelBooking`: it gains `heldUsd(): number | null`, the holds of the session's requests still in flight. It returns null (unknown, never 0) while one of them has no hold, and a reader must treat null as unknown. `spentUsd()` still counts answered requests only, so the run's total is `spentUsd()` plus `heldUsd()`. It also gains `boundUsd(): number`, what a cache miss adds to those holds (0 with none): the run's tools stop on the holds plus this, so the note counts both.
|
|
76
|
+
- A web_search setting of deny or ask now applies to a Claude Code or Codex run's own web search at its next search, including a run that started while it was allow. Before, the setting was read only when the run started, so a run that started with native search kept searching after the setting changed.
|
|
77
|
+
- Claude Code: each WebSearch asks the run's gate before it runs, as a host tool's call does. A deny refuses it in the gate's words. An ask parks the run on the person with nothing searched; once they allow it, the model's next search with the same query runs once, with no second card.
|
|
78
|
+
- Codex: its search runs inside the provider's model request, so its proxy reads web_search's setting before each request and offers the provider its search only while the setting is allow. A failed read offers no search.
|
|
79
|
+
- `SessionIO.permission(tool)` (agentrun-kernel) is the host's setting for a harness's own tool at the time of the read. The runner reads it from the store each time. `ProviderPolicy.apply` may return a promise.
|
|
80
|
+
- A runner run whose plane resolves web_search to native now gets the harness's own search (#2458). The runner had built the run's environment without its plane, so Claude Code and Codex runs on a runner never had it on.
|
|
81
|
+
- A web_search that resolves to the model provider's own search works again on the runner for pi (#2458). It used to answer "web_search has no model to search on" every time:
|
|
82
|
+
- A pi run's own web_search on an Anthropic or OpenAI model. That is what `auto` picks for pi, and `native` too. Each search now goes through the run's model proxy with the provider's search tool, and its searches are booked at the provider's price on top of its tokens.
|
|
83
|
+
- A learned workflow's call node whose web_search resolves native, on any harness.
|
|
84
|
+
|
|
85
|
+
Each run's search uses that run's own model route, never one shared by the whole process. Hand-written workflow agents were not affected: their web_search never resolves native. Both harness packages now export their `withWebSearches` price wrapper, and the Codex one its `webSearchUsd`. A claude-agent or codex run's own search is not switched on by this change.
|
|
86
|
+
|
|
87
|
+
A capped run holds a hosted search's fees with its tokens before the request is sent, at the most searches the request allows, so the fees can't pass the cap. The kernel's model proxy takes a policy's `feeMaxUsd(body)` and holds it as the request's `feeUsd`; a fee it can't bound leaves the request unmeasured. The proxy also hands `usageOf` the sent body as a second argument, so a pi request on OpenAI's Responses that offered no search reads 0 searches from its body, even when its answer names a search call.
|
|
88
|
+
- A capped run on the runner is priced the way the cap's `expected` mode says: each model request from the previous request of its session. The cache that request read or wrote counts at the cache-read price, and only its other input plus what the body grew by since counts at full price. Before, the runner gave the cap no previous request, so every step was held as a whole cache miss, and a run needed a cap several times what it spent (a 5-step Sonnet run that spends $0.04 needed $0.11). `strict` mode is unchanged: every token uncached. A side call (a title, a summary) is priced as a cache miss, as a host's own request already was.
|
|
89
|
+
- A pi run's cost counts every request it made. A learned workflow's model node, or a request on another model than the run's, is booked beside pi's own answers as a side call, and the run's view, its Timeline usage row and its stored cost now add it, where they read pi's answers alone (three requests booked at $0.000014 each read $0.000028). A request a lost runner left unanswered is booked cut once, however many of the next claim's bookings read it open: its hold and bound count once toward the cap, and its cost is unknown once, where a second booking could count it twice. A pi run's stored cost is now its booking's, as a CLI run's is: a run cancelled or stopped while its model's answer streamed reads its cost unknown (the cut request may be billed for what it streamed), never the $0 its cut answer carried.
|
|
90
|
+
- A capped run's cap counts what its learning spent on its behalf, so the run and its grades never pass the cap together.
|
|
91
|
+
- Before, a follow-up on a run that learned was held only to the run's own spend: an earlier round's grade, which runs as a learning run of its own, was not counted, and the run could end up past its cap by that grade.
|
|
92
|
+
- Every gate of the run now reads its own spend plus what its learning runs took: a grade that ended at its cost, and one still under way at its own cap, the most it may take. That covers the model and paid-tool gate on pi and on the CLI harnesses (claude-agent, codex, opencode), the end of a run whose spend passed its cap, the wrap-up note and the tools' wrap-up, the check before a later round looks back, and the cap each later grade runs under.
|
|
93
|
+
- A follow-up that doesn't fit parks the run for a raise, as at any spend limit. Its budget card counts the grades in what the run has spent. An uncapped run is unchanged.
|
|
94
|
+
- Kernel: `RunSessions.beside()`, when given, is added to the run's spend at each admission (`runBooking`).
|
|
95
|
+
|
|
5
96
|
## 0.1.0-beta.36 (2026-11-13)
|
|
6
97
|
|
|
7
98
|
- The kernel and the Claude Agent test kit drop exports nothing in AgentRun used. From `@parcha/agentrun-kernel`: the `delegate` tool (`delegateTool`, `DELEGATE_TOOL`, `DELEGATE_RESULT_SCHEMA`, `delegateProblems` and the `DelegateAgent`, `DelegateEvent` and `DelegateOptions` types). From `./kernel`: the follow-up turn (`followUpOnce`, `followUpState`, `followUpKey`, `FOLLOW_UP_EFFECT`, `FOLLOW_UP_BUDGET_REACHED`, `FOLLOW_UP_CANCELLED` and their types) and `BASH_MAX_TIMEOUT_S`. From `./process`: `lastTurnEnd`, `summarizeBatch`, `batchTranscript` and `zodFromJsonSchema`. From `@parcha/agentrun-harness-claude/testing`: `lastUserText`. An embedder that imported one of them keeps its own copy: each was a small helper, or a feature the server never wired (a follow-up turn, a delegate tool). `zodShapeFromJsonSchema` stays.
|
package/dist/host/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, anthropicImageTokens, GEMINI_HIGH_RESOLUTION_TOKENS, geminiImageTokens, imageInputOf, imageProviderOf, imageSizeOf, imageTokens, INPUT_OVERHEAD_APIS, messagesImagesIn, OPENAI_IMAGE_TOKENS_MAX, openaiImageTokens, openaiPatchMultiplier, providerInputOverhead, responsesImagesIn, type ImageInput, type InputShape } from "./overhead.js";
|
|
2
|
-
export { COUNT_TIMEOUT_MS, countedInputOf, hostHeaderProblems, ProxyRefusal, sameKey, startModelProxy } from "./proxy.js";
|
|
1
|
+
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, anthropicImageTokens, GEMINI_HIGH_RESOLUTION_TOKENS, geminiImageTokens, imageInputOf, imageProviderOf, imageSizeOf, imageTokens, INPUT_OVERHEAD_APIS, messagesImagesIn, OPENAI_IMAGE_TOKENS_MAX, openaiImageTokens, OPENAI_INPUT_COUNTER, openaiPatchMultiplier, providerInputOverhead, responsesEscapedBytes, responsesImagesIn, responsesInputOverhead, type ImageInput, type InputShape } from "./overhead.js";
|
|
2
|
+
export { COUNT_TIMEOUT_MS, countedInputOf, hostHeaderProblems, lastObjectOf, NATIVE_SEARCH_USD, ProxyRefusal, sameKey, startModelProxy } from "./proxy.js";
|
|
3
3
|
export type { InputCounter, ModelProxy, ModelProxyHooks, ModelProxyOptions, ModelUsageReport, ProviderPolicy, ProxyRequest, ProxyRequestContext, ProxyRoute, ProxyUsage } from "./proxy.js";
|
|
4
|
-
export { mcpRoute } from "./mcp-route.js";
|
|
5
|
-
export type { McpHandler, McpRouteOptions } from "./mcp-route.js";
|
|
6
4
|
export { processAlive } from "./alive.js";
|
|
7
5
|
export { serialWriter } from "./serial.js";
|
|
8
6
|
export { PARENT_WATCH_SHIM, parentWatched, parentWatchHere, SETPRIV } from "./parent-death.js";
|
package/dist/host/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// What a harness adapter runs beside its harness on the host: the session's model proxy, provider-neutral, with each
|
|
2
2
|
// provider's rules in a `ProviderPolicy`. Written for AgentRun.
|
|
3
|
-
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, anthropicImageTokens, GEMINI_HIGH_RESOLUTION_TOKENS, geminiImageTokens, imageInputOf, imageProviderOf, imageSizeOf, imageTokens, INPUT_OVERHEAD_APIS, messagesImagesIn, OPENAI_IMAGE_TOKENS_MAX, openaiImageTokens, openaiPatchMultiplier, providerInputOverhead, responsesImagesIn } from "./overhead.js";
|
|
4
|
-
export { COUNT_TIMEOUT_MS, countedInputOf, hostHeaderProblems, ProxyRefusal, sameKey, startModelProxy } from "./proxy.js";
|
|
5
|
-
export { mcpRoute } from "./mcp-route.js";
|
|
3
|
+
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, anthropicImageTokens, GEMINI_HIGH_RESOLUTION_TOKENS, geminiImageTokens, imageInputOf, imageProviderOf, imageSizeOf, imageTokens, INPUT_OVERHEAD_APIS, messagesImagesIn, OPENAI_IMAGE_TOKENS_MAX, openaiImageTokens, OPENAI_INPUT_COUNTER, openaiPatchMultiplier, providerInputOverhead, responsesEscapedBytes, responsesImagesIn, responsesInputOverhead } from "./overhead.js";
|
|
4
|
+
export { COUNT_TIMEOUT_MS, countedInputOf, hostHeaderProblems, lastObjectOf, NATIVE_SEARCH_USD, ProxyRefusal, sameKey, startModelProxy } from "./proxy.js";
|
|
6
5
|
export { processAlive } from "./alive.js";
|
|
7
6
|
export { serialWriter } from "./serial.js";
|
|
8
7
|
export { PARENT_WATCH_SHIM, parentWatched, parentWatchHere, SETPRIV } from "./parent-death.js";
|
package/dist/host/overhead.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { InputCounter } from "./proxy.js";
|
|
1
2
|
/** One image a request carries: the bytes its data takes in the body (its base64, or its data URL; 0 for an image the
|
|
2
3
|
* provider fetches by URL), which are counted at the image's billed size instead, and its pixel size when its data's own
|
|
3
4
|
* header says it (PNG, JPEG, GIF, WebP). */
|
|
@@ -119,6 +120,47 @@ export declare function messagesImagesIn(messages: unknown): ImageInput[];
|
|
|
119
120
|
* content, or a part of a function call's output, in every turn. Each is a data URL's, or a URL's or file id's (no
|
|
120
121
|
* bytes). An image-shaped object anywhere else is text to the provider: its bytes stay counted as they are. */
|
|
121
122
|
export declare function responsesImagesIn(input: unknown): ImageInput[];
|
|
123
|
+
/**
|
|
124
|
+
* The bytes JSON's escapes add to the text a Responses body carries for the model to read as it is, beyond that text's
|
|
125
|
+
* own UTF-8 bytes: in `instructions`, and in each `input` item, a message's text, a function call's arguments, a custom
|
|
126
|
+
* tool call's input, a tool's output (a string, or its text parts) and a reasoning summary's text.
|
|
127
|
+
*
|
|
128
|
+
* Why the input bound may count that text at its own bytes: the model reads it as the string itself, never as its JSON
|
|
129
|
+
* spelling in the body. OpenAI's counting recipe encodes each message's decoded value and adds a fixed few tokens a
|
|
130
|
+
* message (num_tokens_from_messages,
|
|
131
|
+
* https://github.com/openai/openai-cookbook/blob/db3144982aa26b87a9bdfb692b4fbedfdf8a14d5/examples/How_to_count_tokens_with_tiktoken.ipynb),
|
|
132
|
+
* and the harmony format OpenAI published for its models puts a message's content, a call's arguments and a tool's
|
|
133
|
+
* output between its special tokens as raw text (`<|message|>{"location":"San Francisco"}<|call|>`,
|
|
134
|
+
* https://github.com/openai/harmony/blob/e0ce87d1c3b9f59dec2e27de4770c769f03ebbd3/docs/format.md#receiving-tool-calls).
|
|
135
|
+
* A byte-level BPE token covers at least one byte of the text it encodes, so that text is at most its UTF-8 bytes in
|
|
136
|
+
* tokens; the escape characters JSON adds around it (`\"`, `\\`, `\n`, `\t`, `\u0000`) are bytes the body spends that no
|
|
137
|
+
* token of it is billed for. Every other byte is counted as sent, as before: the keys, ids and structure of each item
|
|
138
|
+
* still cover the framing the provider puts around it (a role, a recipient, a few special tokens: fewer than the bytes
|
|
139
|
+
* of the keys the item spends); the tool definitions (`tools`, and code mode's `additional_tools` item) are counted
|
|
140
|
+
* whole, since a template renders them into a prompt of its own that may quote their strings again (harmony's
|
|
141
|
+
* TypeScript namespace); and encrypted reasoning is base64, which JSON never escapes, so it keeps its own bytes.
|
|
142
|
+
*/
|
|
143
|
+
export declare function responsesEscapedBytes(body: Record<string, unknown>): number;
|
|
144
|
+
/**
|
|
145
|
+
* The input tokens the provider bills for a Responses `body` beyond its bytes (`providerInputOverhead`, with its
|
|
146
|
+
* sources), less the escapes in the text it carries (`responsesEscapedBytes`, with the argument for it). The Responses
|
|
147
|
+
* API documents no prompt of its own that it adds on these routes, and the framing it puts around each input item and
|
|
148
|
+
* tool definition is a few tokens each, fewer than the bytes of the JSON keys that item already spends in the body. An
|
|
149
|
+
* image is billed by its area, not its bytes: each counts at the most it can be billed as, in place of the bytes its
|
|
150
|
+
* data URL takes in the body. A file input (a PDF, billed by its pages) has no stated bound, so a body with one cannot
|
|
151
|
+
* be measured: this throws, and the request's input is unknown. Codex's and OpenCode's policies both use it.
|
|
152
|
+
*/
|
|
153
|
+
export declare function responsesInputOverhead(body: Record<string, unknown>): number;
|
|
154
|
+
/**
|
|
155
|
+
* OpenAI's own count of a Responses request's input tokens: POST /v1/responses/input_tokens on api.openai.com only, which
|
|
156
|
+
* "accepts the same input format as the Responses API" and answers `{object: "response.input_tokens", input_tokens}`,
|
|
157
|
+
* "the exact count the model will receive", formatting tokens, tools and images included ("Counting tokens",
|
|
158
|
+
* https://developers.openai.com/api/docs/guides/token-counting; the SDK's `responses.inputTokens.count`,
|
|
159
|
+
* https://github.com/openai/openai-node/blob/543ed94e6451d2eaf43f1a4a16a0fc8c8f42eade/src/resources/responses/input-tokens.ts).
|
|
160
|
+
* The model proxy asks it before each answer request on that origin (never through a gateway), as Anthropic's count is
|
|
161
|
+
* asked (ProviderPolicy.countInput): a count that fails, times out, or answers anything else leaves the byte bound.
|
|
162
|
+
*/
|
|
163
|
+
export declare const OPENAI_INPUT_COUNTER: InputCounter;
|
|
122
164
|
/** The image a body's `data` (base64, or a data URL) carries: the bytes it takes, and its size when its header says it.
|
|
123
165
|
* Anything else (an http URL, a file id): no bytes, no size. */
|
|
124
166
|
export declare function imageInputOf(data: unknown): ImageInput;
|
package/dist/host/overhead.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
// The input a provider bills for a request beyond the bytes of its body, by the provider's API: one place for the figures
|
|
2
|
-
// and their sources, used by the model proxy's policies and by a host that bounds a request it sends itself. Written for
|
|
3
|
-
// AgentRun.
|
|
4
|
-
//
|
|
5
|
-
// A request's input can never be more tokens than its body's UTF-8 bytes (`inputTokensMaxOf`), except for what the
|
|
6
|
-
// provider adds or bills by another measure: a prompt it adds itself when tools are present, and images, which it bills by
|
|
7
|
-
// their area after it resizes them, not by their bytes. An image is counted at what the routed model's provider bills it
|
|
8
|
-
// as by its size (Anthropic's, OpenAI's or Google's rule; the most of the three for another provider), in place of the
|
|
9
|
-
// bytes its data takes in the body: a screenshot's base64 is a megabyte, and Claude bills it as some 1,200 tokens. Each
|
|
10
|
-
// figure is the largest the provider documents for any current model, so no model is undercounted.
|
|
11
1
|
/**
|
|
12
2
|
* The tool-use system prompt the Messages API adds to a request with tools, in tokens, by tool_choice. Documented per
|
|
13
3
|
* model under "Pricing" in https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview, and taken at the largest
|
|
@@ -267,6 +257,90 @@ export function responsesImagesIn(input) {
|
|
|
267
257
|
}
|
|
268
258
|
return out;
|
|
269
259
|
}
|
|
260
|
+
/** The items of `type` in `value`, at any depth (a tool's output holds them). */
|
|
261
|
+
function itemsIn(value, type) {
|
|
262
|
+
if (Array.isArray(value))
|
|
263
|
+
return value.reduce((n, item) => n + itemsIn(item, type), 0);
|
|
264
|
+
if (!plain(value))
|
|
265
|
+
return 0;
|
|
266
|
+
return (value.type === type ? 1 : 0) + Object.values(value).reduce((n, item) => n + itemsIn(item, type), 0);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The bytes JSON's escapes add to the text a Responses body carries for the model to read as it is, beyond that text's
|
|
270
|
+
* own UTF-8 bytes: in `instructions`, and in each `input` item, a message's text, a function call's arguments, a custom
|
|
271
|
+
* tool call's input, a tool's output (a string, or its text parts) and a reasoning summary's text.
|
|
272
|
+
*
|
|
273
|
+
* Why the input bound may count that text at its own bytes: the model reads it as the string itself, never as its JSON
|
|
274
|
+
* spelling in the body. OpenAI's counting recipe encodes each message's decoded value and adds a fixed few tokens a
|
|
275
|
+
* message (num_tokens_from_messages,
|
|
276
|
+
* https://github.com/openai/openai-cookbook/blob/db3144982aa26b87a9bdfb692b4fbedfdf8a14d5/examples/How_to_count_tokens_with_tiktoken.ipynb),
|
|
277
|
+
* and the harmony format OpenAI published for its models puts a message's content, a call's arguments and a tool's
|
|
278
|
+
* output between its special tokens as raw text (`<|message|>{"location":"San Francisco"}<|call|>`,
|
|
279
|
+
* https://github.com/openai/harmony/blob/e0ce87d1c3b9f59dec2e27de4770c769f03ebbd3/docs/format.md#receiving-tool-calls).
|
|
280
|
+
* A byte-level BPE token covers at least one byte of the text it encodes, so that text is at most its UTF-8 bytes in
|
|
281
|
+
* tokens; the escape characters JSON adds around it (`\"`, `\\`, `\n`, `\t`, `\u0000`) are bytes the body spends that no
|
|
282
|
+
* token of it is billed for. Every other byte is counted as sent, as before: the keys, ids and structure of each item
|
|
283
|
+
* still cover the framing the provider puts around it (a role, a recipient, a few special tokens: fewer than the bytes
|
|
284
|
+
* of the keys the item spends); the tool definitions (`tools`, and code mode's `additional_tools` item) are counted
|
|
285
|
+
* whole, since a template renders them into a prompt of its own that may quote their strings again (harmony's
|
|
286
|
+
* TypeScript namespace); and encrypted reasoning is base64, which JSON never escapes, so it keeps its own bytes.
|
|
287
|
+
*/
|
|
288
|
+
export function responsesEscapedBytes(body) {
|
|
289
|
+
// What the proxy sends is JSON.stringify's spelling, so the escapes are measured by the same spelling.
|
|
290
|
+
const escaped = (text) => (typeof text === "string" ? Buffer.byteLength(JSON.stringify(text), "utf8") - 2 - Buffer.byteLength(text, "utf8") : 0);
|
|
291
|
+
const textOf = (content) => (typeof content === "string" ? escaped(content) : list(content).reduce((n, part) => n + (plain(part) && (part.type === "input_text" || part.type === "output_text" || part.type === "summary_text" || part.type === "reasoning_text") ? escaped(part.text) : 0), 0));
|
|
292
|
+
let sum = escaped(body.instructions);
|
|
293
|
+
for (const item of list(body.input)) {
|
|
294
|
+
if (!plain(item))
|
|
295
|
+
continue;
|
|
296
|
+
if (item.type === "message" || (item.type === undefined && typeof item.role === "string"))
|
|
297
|
+
sum += textOf(item.content);
|
|
298
|
+
else if (item.type === "function_call")
|
|
299
|
+
sum += escaped(item.arguments);
|
|
300
|
+
else if (item.type === "custom_tool_call")
|
|
301
|
+
sum += escaped(item.input);
|
|
302
|
+
else if (item.type === "function_call_output" || item.type === "custom_tool_call_output")
|
|
303
|
+
sum += textOf(item.output);
|
|
304
|
+
else if (item.type === "reasoning")
|
|
305
|
+
sum += textOf(item.summary) + textOf(item.content);
|
|
306
|
+
}
|
|
307
|
+
return sum;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The input tokens the provider bills for a Responses `body` beyond its bytes (`providerInputOverhead`, with its
|
|
311
|
+
* sources), less the escapes in the text it carries (`responsesEscapedBytes`, with the argument for it). The Responses
|
|
312
|
+
* API documents no prompt of its own that it adds on these routes, and the framing it puts around each input item and
|
|
313
|
+
* tool definition is a few tokens each, fewer than the bytes of the JSON keys that item already spends in the body. An
|
|
314
|
+
* image is billed by its area, not its bytes: each counts at the most it can be billed as, in place of the bytes its
|
|
315
|
+
* data URL takes in the body. A file input (a PDF, billed by its pages) has no stated bound, so a body with one cannot
|
|
316
|
+
* be measured: this throws, and the request's input is unknown. Codex's and OpenCode's policies both use it.
|
|
317
|
+
*/
|
|
318
|
+
export function responsesInputOverhead(body) {
|
|
319
|
+
if (itemsIn(body.input, "input_file") > 0)
|
|
320
|
+
throw new Error("a file input's billed size has no stated bound");
|
|
321
|
+
return providerInputOverhead("openai-responses", { tools: Array.isArray(body.tools) && body.tools.length > 0, images: responsesImagesIn(body.input), ...(typeof body.model === "string" ? { model: body.model } : {}) }) - responsesEscapedBytes(body);
|
|
322
|
+
}
|
|
323
|
+
/** The fields of a Responses body the input-token count takes (the rest, such as stream, store, include and
|
|
324
|
+
* max_output_tokens, change nothing it counts). */
|
|
325
|
+
const RESPONSES_COUNTED_FIELDS = ["model", "input", "instructions", "tools", "tool_choice", "parallel_tool_calls", "reasoning", "text"];
|
|
326
|
+
/** The request headers the count is sent with: the credential's and the account's, nothing else. */
|
|
327
|
+
const OPENAI_COUNT_HEADERS = new Set(["authorization", "openai-organization", "openai-project"]);
|
|
328
|
+
/**
|
|
329
|
+
* OpenAI's own count of a Responses request's input tokens: POST /v1/responses/input_tokens on api.openai.com only, which
|
|
330
|
+
* "accepts the same input format as the Responses API" and answers `{object: "response.input_tokens", input_tokens}`,
|
|
331
|
+
* "the exact count the model will receive", formatting tokens, tools and images included ("Counting tokens",
|
|
332
|
+
* https://developers.openai.com/api/docs/guides/token-counting; the SDK's `responses.inputTokens.count`,
|
|
333
|
+
* https://github.com/openai/openai-node/blob/543ed94e6451d2eaf43f1a4a16a0fc8c8f42eade/src/resources/responses/input-tokens.ts).
|
|
334
|
+
* The model proxy asks it before each answer request on that origin (never through a gateway), as Anthropic's count is
|
|
335
|
+
* asked (ProviderPolicy.countInput): a count that fails, times out, or answers anything else leaves the byte bound.
|
|
336
|
+
*/
|
|
337
|
+
export const OPENAI_INPUT_COUNTER = Object.freeze({
|
|
338
|
+
origin: "https://api.openai.com",
|
|
339
|
+
path: "/v1/responses/input_tokens",
|
|
340
|
+
body: (body) => Object.fromEntries(RESPONSES_COUNTED_FIELDS.filter((field) => body[field] !== undefined).map((field) => [field, body[field]])),
|
|
341
|
+
headers: (sent) => Object.fromEntries(Object.entries(sent).filter(([name]) => OPENAI_COUNT_HEADERS.has(name.toLowerCase()))),
|
|
342
|
+
tokensOf: (answer) => (plain(answer) && answer.object === "response.input_tokens" && typeof answer.input_tokens === "number" ? answer.input_tokens : undefined),
|
|
343
|
+
});
|
|
270
344
|
/** The image a body's `data` (base64, or a data URL) carries: the bytes it takes, and its size when its header says it.
|
|
271
345
|
* Anything else (an http URL, a file id): no bytes, no size. */
|
|
272
346
|
export function imageInputOf(data) {
|
package/dist/host/proxy.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IncomingHttpHeaders } from "node:http";
|
|
2
|
-
import { type
|
|
2
|
+
import { type NoteRequest } from "../types.js";
|
|
3
3
|
import { type Ended } from "../booking.js";
|
|
4
4
|
/** A route a policy admits: its method, its exact path, and the request headers forwarded with it. */
|
|
5
5
|
export interface ProxyRoute {
|
|
@@ -20,6 +20,10 @@ export interface ProxyUsage {
|
|
|
20
20
|
cacheRead: number;
|
|
21
21
|
cacheWrite?: number;
|
|
22
22
|
}
|
|
23
|
+
/** What one provider-native web search costs, in USD, beside the tokens: $10 per 1,000 searches (Anthropic's "Web search tool"
|
|
24
|
+
* and OpenAI's "Web search (all models)" list the same price). The one figure every fee reads: the proxy policies of the
|
|
25
|
+
* Claude and Codex harnesses and the runner's hosted search. */
|
|
26
|
+
export declare const NATIVE_SEARCH_USD = 0.01;
|
|
23
27
|
/** What one provider's API may carry through the proxy. */
|
|
24
28
|
export interface ProviderPolicy {
|
|
25
29
|
/** A name for messages. */
|
|
@@ -34,11 +38,14 @@ export interface ProviderPolicy {
|
|
|
34
38
|
header?(name: string, value: string): string | undefined;
|
|
35
39
|
/** The body the provider gets for a request on `route`: throw a `ProxyRefusal` to refuse it. `context.host`: the
|
|
36
40
|
* request is the host's own, beside the harness's (ModelProxy.sideKey), whose tools are its own client-side ones, not
|
|
37
|
-
* the session's.
|
|
38
|
-
|
|
41
|
+
* the session's. A policy that reads something of the host's first (a tool's permission now) answers with a promise,
|
|
42
|
+
* which may reject the same way. */
|
|
43
|
+
apply(body: unknown, route: ProxyRoute, context?: ProxyRequestContext): Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
39
44
|
/** The usage a completed response reports, if it reports one: from its whole text, or, for a long one, its first and
|
|
40
|
-
* last 64 KiB (a provider may report input tokens as a stream starts and output tokens as it ends).
|
|
41
|
-
|
|
45
|
+
* last 64 KiB (a provider may report input tokens as a stream starts and output tokens as it ends). `body`: the body
|
|
46
|
+
* its request was sent with (`apply`'s), so what the request offered (a hosted tool it did not offer never ran) is
|
|
47
|
+
* read from the request, never guessed from the answer's words. */
|
|
48
|
+
usageOf(text: string, body?: Record<string, unknown>): ProxyUsage | undefined;
|
|
42
49
|
/** Whether the provider stopped a completed answer on its content filter (a refusal), read from its text as `usageOf`
|
|
43
50
|
* reads it, never by matching its words, and from `lineStarts`, the start of each of its last whole lines (16 KiB of
|
|
44
51
|
* each, for a line longer than the text kept). Absent: the policy cannot tell, and no answer is read as stopped. */
|
|
@@ -76,10 +83,15 @@ export interface ProviderPolicy {
|
|
|
76
83
|
* below 0 is no bound (unmeasured). Undefined: the body has no input bound the policy can state (a hosted tool that
|
|
77
84
|
* reads pages into the input), so `inputTokensMax` is undefined, never 0 or the body's bytes alone. Absent: none. */
|
|
78
85
|
serverInputOverhead?(body: Record<string, unknown>): number | undefined;
|
|
86
|
+
/** What the body `apply` returned may charge beside its tokens at most, in USD: a hosted tool's per-use fees, at the
|
|
87
|
+
* most uses the body allows it. Held with its tokens (ProxyRequest.feeUsd), so a cap reserves the fees before the
|
|
88
|
+
* request leaves, not only once its answer reports them. A figure that is not finite (the body allows its tool any
|
|
89
|
+
* number of uses), or a throw: the request's worst case is unknown, as an unmeasured input's is. Absent: none. */
|
|
90
|
+
feeMaxUsd?(body: Record<string, unknown>): number;
|
|
79
91
|
/** The output tokens the body `apply` returned asks for at most (its own limit, as `apply` shaped it). Absent or
|
|
80
92
|
* undefined: the body does not say. */
|
|
81
93
|
outputAsked?(body: Record<string, unknown>): number | undefined;
|
|
82
|
-
/** The provider's own count of a request's input tokens (Anthropic's free count_tokens), which the proxy asks before
|
|
94
|
+
/** The provider's own count of a request's input tokens (Anthropic's free count_tokens, OpenAI's input_tokens), which the proxy asks before
|
|
83
95
|
* each of the session's answer requests (`countedInputOf`) when its upstream is exactly `origin`, the provider's own: never
|
|
84
96
|
* through a gateway or any other base, so the key and the request go nowhere the request itself would not. Its count,
|
|
85
97
|
* plus `margin`, is that request's input bound when it is below the byte bound. Absent: never counted. */
|
|
@@ -177,9 +189,15 @@ export interface ProxyRequest {
|
|
|
177
189
|
outputTokensAsked?: number;
|
|
178
190
|
/** The least output it may be limited to and still answer (the policy's `outputLeast`). */
|
|
179
191
|
outputTokensLeast?: number;
|
|
192
|
+
/** What it may charge beside its tokens at most (the policy's `feeMaxUsd`), in USD: held with them. */
|
|
193
|
+
feeUsd?: number;
|
|
180
194
|
/** A side call, never its turn's own: on another model than the session's (a client's small-model call: a title, a
|
|
181
195
|
* summary), or the host's own request beside the harness's (`host`). */
|
|
182
196
|
side?: true;
|
|
197
|
+
/** On the session's model but no answer of the turn (the policy's `answers` reads it so: a title, a summary, a child's
|
|
198
|
+
* request). Still the turn's to book, but a conversation of its own: the cap never prices the session's next request
|
|
199
|
+
* from it, nor it from the session's. */
|
|
200
|
+
unanswered?: true;
|
|
183
201
|
/** It carries the host's note (`ModelProxyHooks.note`), placed in its body: the host's to record once it is admitted. */
|
|
184
202
|
noted?: true;
|
|
185
203
|
/** The label of the side key it came under (ModelProxy.sideKey): the host's own request, beside the harness's. */
|
|
@@ -203,6 +221,8 @@ export interface ModelProxyHooks {
|
|
|
203
221
|
* proxy asks only for a paid request on the session's model whose policy can place one (`addNote`), with that request
|
|
204
222
|
* as its client sent it (NoteRequest: its input bound and output asks, measured before the note is placed). */
|
|
205
223
|
note?(model: string, request?: NoteRequest): string | undefined;
|
|
224
|
+
/** The note `note` gave back (ModelPort.noteReturned): the request that took it left before `beforeRequest`. */
|
|
225
|
+
noteReturned?(note: string): void;
|
|
206
226
|
}
|
|
207
227
|
export interface ModelProxyOptions {
|
|
208
228
|
policy: ProviderPolicy;
|
|
@@ -239,14 +259,6 @@ export interface ModelProxyOptions {
|
|
|
239
259
|
* after the client's, so the client can neither set nor override them, and before the credential's. Checked when the
|
|
240
260
|
* proxy starts (`hostHeaderProblems`). */
|
|
241
261
|
headers?: Readonly<Record<string, string>>;
|
|
242
|
-
/** The session's remote MCP servers, each mounted here at `/mcp/<server>` behind the session's key (mcp-route.ts),
|
|
243
|
-
* for the harness's own MCP client as its adapter declares it (`client`, AdapterCapabilities.mcpClient). One mount for
|
|
244
|
-
* every harness: an adapter hands the session's MCP, never a route of its own. */
|
|
245
|
-
mcp?: {
|
|
246
|
-
session: SessionMcp;
|
|
247
|
-
client: McpClient;
|
|
248
|
-
keepaliveMs?: number;
|
|
249
|
-
};
|
|
250
262
|
/** For tests: the fetch the proxy forwards with. */
|
|
251
263
|
fetch?: typeof fetch;
|
|
252
264
|
/** Told, with CONTENT_FILTERED, once an answer the provider stopped on its content filter has been passed and booked
|
|
@@ -273,7 +285,7 @@ export interface ModelProxy {
|
|
|
273
285
|
* harness. A request under it goes through the same policy, origin, credential, price and hooks as the harness's;
|
|
274
286
|
* its admission names the label (ModelRequest.host), the policy applies it as the host's (`ProxyRequestContext`),
|
|
275
287
|
* it is never the turn's answer request (a content-filter stop on it never halts the turn) and never watched. It opens
|
|
276
|
-
* the model routes only
|
|
288
|
+
* the model routes only. */
|
|
277
289
|
sideKey(label: string): string;
|
|
278
290
|
/** The harness opened its next turn: once the proxy has told it a turn's answer stopped (onCut, onFiltered), it refuses
|
|
279
291
|
* the session's answer requests, unsent and unbooked, until this is called. */
|
|
@@ -285,6 +297,13 @@ export interface ModelProxy {
|
|
|
285
297
|
export interface ProxyRequestContext {
|
|
286
298
|
readonly host?: string;
|
|
287
299
|
}
|
|
300
|
+
/** The last `"<key>": {...}` object in `text`, parsed: how a policy reads a field of an answer's terminal event when the
|
|
301
|
+
* event's line was longer than USAGE_WINDOW, so the proxy kept only its end. JSON may put whitespace on either side of
|
|
302
|
+
* the colon (a Python gateway's `json.dumps` writes `"usage": {`), and the client parses the event whatever its spacing,
|
|
303
|
+
* so this reads it too: a usage the harness counted is never booked at an unknown cost for its spacing. A key's quoted
|
|
304
|
+
* name inside a JSON string is escaped (`\"usage\"`), so it never reads as one. Undefined when there is none, or the
|
|
305
|
+
* last one does not close in `text` or does not parse. */
|
|
306
|
+
export declare function lastObjectOf(text: string, key: string): unknown;
|
|
288
307
|
/** The ids a response's headers give its request, by header name, so a run's books can be joined to the provider's (or
|
|
289
308
|
* a gateway's) request by request; undefined when there are none. Only id-named headers with id-shaped values. */
|
|
290
309
|
export declare function providerIdsOf(headers: Headers): Record<string, string> | undefined;
|