@parcha/agentrun-kernel 0.1.0-beta.38 → 0.1.0-beta.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
package/dist/types.d.ts CHANGED
@@ -36,6 +36,14 @@ export declare const HOST_RESULT_MAX_CHARS = 120000;
36
36
  * answer again. A host reads a run's end by it (agentrun-serve's `moderated`).
37
37
  */
38
38
  export declare const CONTENT_FILTERED = "content_filter: the model provider stopped this answer";
39
+ /**
40
+ * What a run on a person's ChatGPT plan ends with once the plan's limit is reached (its 429): the plan's own limit, never
41
+ * the run's spend cap, so nothing here can raise it and nothing tries again. A harness's model proxy answers its client
42
+ * these words (`ProviderPolicy.finalError`) and the harness halts the turn with them; a host shows them as they are.
43
+ */
44
+ export declare const PLAN_LIMIT_WORDS = "Your ChatGPT plan's limit is reached. Manage usage in your ChatGPT settings, then run it again.";
45
+ /** The code the proxy's answer carries beside PLAN_LIMIT_WORDS. */
46
+ export declare const PLAN_LIMIT_CODE = "plan_limit_reached";
39
47
  /** A tool the host gives a session. */
40
48
  export interface HostTool {
41
49
  name: string;
@@ -82,10 +90,13 @@ export interface HostTool {
82
90
  export interface NestedCalls {
83
91
  /** Call tool `name` (a session tool, by its host name) with `args`, under `key`: stable for the same call in a rerun
84
92
  * of the calling one. A deny answers `refused` with the gate's words; a park holds until the session lets go, then
85
- * rejects. */
86
- call(name: string, args: Record<string, unknown>, key: string): Promise<NestedResult>;
87
- /** `value` the first time `key` is asked in this call, and that first value again in every rerun of the call. */
88
- memo<T extends JsonValue>(key: string, value: T): Promise<T>;
93
+ * rejects. Given `signal`, the call runs on it as well as on the calling one's: a caller that cuts one part of its work
94
+ * (a learned workflow's node whose sibling failed) cuts that part's call. */
95
+ call(name: string, args: Record<string, unknown>, key: string, signal?: AbortSignal): Promise<NestedResult>;
96
+ /** `value` the first time `key` is asked in this call, and that first value again in every rerun of the call. Given a
97
+ * function, it is called only the first time: a rerun reads the recorded value and never calls it, so work it does (a
98
+ * model's request, a paid decision) is done and paid for once. */
99
+ memo<T extends JsonValue>(key: string, value: T | (() => T | Promise<T>)): Promise<T>;
89
100
  }
90
101
  /** A nested call's answer. */
91
102
  export interface NestedResult {
@@ -173,8 +184,6 @@ export interface ModelRequest {
173
184
  /** The most input tokens the request can be billed for (the model proxy's `inputTokensMaxOf`: the body's UTF-8 bytes
174
185
  * plus what the provider adds), undefined when it was not measured. */
175
186
  inputTokensMax?: number;
176
- /** Its `inputTokensMax` is its provider's count, not its bytes (ProxyRequest.inputTokensCounted). */
177
- inputTokensCounted?: true;
178
187
  /** The most output tokens the request asks for, when its body says (the proxy's policy reads it). */
179
188
  outputTokensAsked?: number;
180
189
  /** The least output it may be limited to and still answer: its answer's 1,024 tokens, plus the least thinking its
@@ -191,7 +200,11 @@ export interface ModelRequest {
191
200
  * release, the time a person used): held at `feeUsd` and booked, never asked of the cap's rule, so a run past its cap
192
201
  * or of unknown spend still counts it (main keeps it so: keepHold). `hold`: a hold-only step: money reserved, not
193
202
  * spent; given back on a lost claim, never booked as cut (its holder's charges book the money, each its own step).
194
- * `ref`: the one booking it names (a browser charge's session and time): a second admit of the same ref is not booked. */
203
+ * `ref`: the one booking it names (a browser charge's session and time): a second admit of the same ref is not booked.
204
+ * `under`: the key of the model session whose tool call it runs in (the CLI's session): a request of that session
205
+ * still in flight may be the one that asked for the call, unbooked until the call returns (Claude Code books its
206
+ * cost after its calls, Codex starts a call with its response open), so the call never waits on it (runBooking).
207
+ * Not written on its record. */
195
208
  paid?: {
196
209
  name: string;
197
210
  basis: string;
@@ -202,14 +215,16 @@ export interface ModelRequest {
202
215
  owed?: true;
203
216
  hold?: true;
204
217
  ref?: string;
218
+ under?: string;
205
219
  };
206
220
  /** A side call, never its turn's own: on another model than the session's (a client's title, a summary), or the
207
221
  * host's own request beside the harness's (`host`). */
208
222
  side?: true;
209
223
  /** On the session's model but no answer of the turn (a title, a summary, a child's request): booked as any request, but
210
- * never one of the session's recent requests for the cap's expected pricing, read or kept. */
224
+ * no answer the host keeps its wrap-up's reserve back for. */
211
225
  unanswered?: true;
212
- /** It carries the host's note (ModelPort.note) in its body: the note is delivered once this request is admitted.
226
+ /** It carries the host's note (ModelPort.note) in its body: admitted, it holds the note until it ends, and the note is
227
+ * told once it is answered (its afterRequest's `ended`), else the host gives it to a later request.
213
228
  * `inputTokensMaxWithoutNote`: its input measured as its client sent it, before the note was placed. */
214
229
  noted?: true;
215
230
  inputTokensMaxWithoutNote?: number;
@@ -221,6 +236,13 @@ export interface ModelRequest {
221
236
  * the session's, for a model of another tier): the host prices it by its model and this route, so one model id two
222
237
  * routes price apart is held at the price its route books it at. Absent: the session's own route. */
223
238
  route?: string;
239
+ /** The reasoning effort its body asks its model at, as its provider names it (the proxy's policy reads it:
240
+ * ProviderPolicy.effortOf), when the body names one. What its least output was read at, so words that name its
241
+ * figures can name it too. */
242
+ effort?: string;
243
+ /** Aborted when the request's client goes (a stop, a cancel, a lost claim): a request still waiting for room under the
244
+ * run's cap (runBooking) leaves its line then, admitted for nothing and holding nothing. */
245
+ signal?: AbortSignal;
224
246
  }
225
247
  /** What a paid request cost, once it completed. */
226
248
  export interface ModelUsage {
@@ -233,13 +255,20 @@ export interface ModelUsage {
233
255
  estimated?: boolean;
234
256
  /** Beside the provider's own figure, what the host's price table makes of the same usage: kept to audit the table. */
235
257
  tableUsd?: number | null;
258
+ /** `plan`: billed to a person's subscription plan (a ChatGPT plan's token), not by the token: `usd` is null. */
259
+ billing?: "plan";
236
260
  /** A request a crash cut off: it left, and no answer came back to this run. Its tokens are unknown (0 here) and its
237
261
  * price is null; the provider may have billed it. */
238
262
  cut?: boolean;
263
+ /** A cut request its host held a figure for: booked at that hold (runner.ts CUT_AT_HOLD), the most it can have cost. */
264
+ atMostUsd?: number;
239
265
  /** A paid step's hold given back unused (a workflow call node's, its effect never sent the paid request it held for):
240
266
  * nothing was sent and nothing charged, at usd 0. Its record says so, so no reader takes it for a $0 charge or an
241
267
  * unpriced one. */
242
268
  givenBack?: true;
269
+ /** On a free model (its known price $0 for every token): booked a known $0 however it ended, `usd` 0, set by the
270
+ * runner's booking (Admitted.free) on what it tells of the request's end, for a reader to say why it cost nothing. */
271
+ free?: true;
243
272
  /** The ids the provider's response gave the request, by header name (the provider's `request-id`, a gateway's call
244
273
  * id such as LiteLLM's `x-litellm-call-id`): what joins this booking to the provider's own log. A cut request has
245
274
  * them only if its response had begun. */
@@ -268,14 +297,15 @@ export interface ModelPort {
268
297
  responded?(ids: Record<string, string>, ticket?: ModelTicket): Promise<void>;
269
298
  /** A note its host has for the session's next main-model request (the spend cap's wrap-up), taken by this call; the
270
299
  * session's model proxy adds it to that request's body (ModelProxyHooks.note). `request`: that request as its client
271
- * sent it, measured before the note is placed (NoteRequest), so the host can price it as its admission will. */
300
+ * sent it, measured before the note is placed (NoteRequest), so the host prices it as its admission will. */
272
301
  note?(model: string, request?: NoteRequest): string | undefined;
273
302
  /** The note `note` gave back: the request that took it left before its admission (its client gone, a refusal or an
274
303
  * error before `beforeRequest`), so it was never sent, and the host may give it to the next request at once. */
275
304
  noteReturned?(note: string): void;
276
305
  }
277
- /** A request about to take its host's note, as its client sent it: its input bound and the output it asks for and needs
278
- * at least, as the model proxy reads them (ModelRequest's figures, without the note). */
306
+ /** A request about to take its host's note, as its client sent it: its input bound (the provider's count or the local
307
+ * count where the proxy took one, else its bytes) and the output it asks for and needs at least, as the model proxy reads
308
+ * them: ModelRequest's figures without the note (its `inputTokensMaxWithoutNote`), the same ones the cap admits it by. */
279
309
  export type NoteRequest = {
280
310
  inputTokensMax?: number;
281
311
  outputTokensAsked?: number;
@@ -321,6 +351,24 @@ export interface SessionIO {
321
351
  * a side effect, the host's hooks on the result). An adapter hands every tool it adds through it. Absent: the host
322
352
  * keeps no record of calls, and the tool runs as it is. */
323
353
  hostTool?(tool: HostTool): HostTool;
354
+ /** The host's record of a call of the harness's own that runs host tools inside it (Codex's code-mode `exec`): its row,
355
+ * and each host tool call it makes nested under it, as a host tool that `nests` (codemode) has its calls on every other
356
+ * harness. Absent: the host keeps no record of calls. */
357
+ ownCalls?: OwnCalls;
358
+ }
359
+ /** The record of a call of the harness's own that host tool calls nest in (SessionIO.ownCalls). Each is said from what the
360
+ * harness's own stream holds (the call's id, its input, its output), never a made-up id. */
361
+ export interface OwnCalls {
362
+ /** The call started, `input` what it was given (once per call id: a second says nothing). */
363
+ opened(callId: string, name: string, input: unknown): Promise<void>;
364
+ /** The host tool call `callId` runs inside the harness's call `parent`: said before the call reaches the gate, so its
365
+ * row (a refused or parked one's too) names its parent. */
366
+ nestedIn(callId: string, parent: string): void;
367
+ /** The call ended with `output`, `failed` when the harness's stream says it did not do its work (Codex's "Script failed"),
368
+ * so its row reads failed as a failed call's does (once: a call already ended says nothing). A crash leaves an opened call
369
+ * open on the host's record: the harness ends it after the resume, from its CLI's answer to it (Codex's recovered exec
370
+ * output), so its end comes after the rows of the calls it made. */
371
+ ended(callId: string, output: string, failed: boolean): Promise<void>;
324
372
  }
325
373
  /** `tool`, a tool the adapter adds of its own, made the host's (SessionIO.hostTool). */
326
374
  export declare const hostedTool: (io: Pick<SessionIO, "hostTool">, tool: HostTool) => HostTool;
@@ -511,11 +559,19 @@ export interface AdapterFacts {
511
559
  * shapes it drops or retypes, measured on its pinned build, so a grant of an MCP tool would offer the model another
512
560
  * tool than the one granted. None: it takes any schema the kernel's check does. */
513
561
  schemaProblems?(tool: string, schema: Record<string, unknown>): string[];
514
- /** A name its client calls a session's MCP tool by beside `mcp__<server>__<tool>` (OpenCode's `<server>_<tool>`). */
515
- readonly toolAlias?: RegExp;
562
+ /** The host's name for a host tool the model names its own way on this harness's tool channel (Claude's
563
+ * `mcp__hands__<name>`, OpenCode's `hands_<name>`, Codex's `mcp_<server>__<tool>`). Absent: the model's name is the
564
+ * host's (pi's). A name already the host's comes back as it is. */
565
+ readonly hostName?: (given: string) => string;
516
566
  /** Its CLI's own tools, by the name a call reaches the host by, each with the host's table tool it is (Claude's
517
567
  * `Bash` is `bash`): a host holds such a call to that tool's setting. None: its CLI calls only the host's tools. */
518
568
  readonly nativeTools?: Readonly<Record<string, string>>;
569
+ /** The input tokens its first model request carries before the agent's own prompt and the person's message: its own
570
+ * system prompt and tool definitions, as measured on its pinned build (the constant beside its pin says how). A host
571
+ * prices a cap's least step on it before any run (a save's warning that a cap is below one step and an answer). A
572
+ * count of what was measured, with o200k_base or from billing (the constant says which). Absent: not measured, read
573
+ * as 0 (a floor). */
574
+ readonly baseInputTokens?: number;
519
575
  /** The policy its model proxy applies, for a host that serves the same route itself (a test's faux harness). */
520
576
  policy(options: {
521
577
  model: string;
package/dist/types.js CHANGED
@@ -15,5 +15,13 @@ export const HOST_RESULT_MAX_CHARS = 120_000;
15
15
  * answer again. A host reads a run's end by it (agentrun-serve's `moderated`).
16
16
  */
17
17
  export const CONTENT_FILTERED = "content_filter: the model provider stopped this answer";
18
+ /**
19
+ * What a run on a person's ChatGPT plan ends with once the plan's limit is reached (its 429): the plan's own limit, never
20
+ * the run's spend cap, so nothing here can raise it and nothing tries again. A harness's model proxy answers its client
21
+ * these words (`ProviderPolicy.finalError`) and the harness halts the turn with them; a host shows them as they are.
22
+ */
23
+ export const PLAN_LIMIT_WORDS = "Your ChatGPT plan's limit is reached. Manage usage in your ChatGPT settings, then run it again.";
24
+ /** The code the proxy's answer carries beside PLAN_LIMIT_WORDS. */
25
+ export const PLAN_LIMIT_CODE = "plan_limit_reached";
18
26
  /** `tool`, a tool the adapter adds of its own, made the host's (SessionIO.hostTool). */
19
27
  export const hostedTool = (io, tool) => (io.hostTool ? io.hostTool(tool) : tool);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-kernel",
3
- "version": "0.1.0-beta.38",
3
+ "version": "0.1.0-beta.40",
4
4
  "description": "Agent harnesses other than Pi, starting with Codex, behind one seam: a DSL node's runNode and an AgentRun run on a harness adapter, durable in AgentRun's own store.",
5
5
  "keywords": [
6
6
  "agentrun",
@@ -50,7 +50,9 @@
50
50
  "CHANGELOG.md",
51
51
  "LICENSE",
52
52
  "NOTICE",
53
- "vendor/durable-harness/UPSTREAM.json"
53
+ "vendor/durable-harness/UPSTREAM.json",
54
+ "vendor/o200k/o200k_base.tiktoken",
55
+ "vendor/o200k/LICENSE"
54
56
  ],
55
57
  "sideEffects": false,
56
58
  "scripts": {
@@ -60,7 +62,7 @@
60
62
  "test": "node --test --test-concurrency=8 'test/*.test.mjs'"
61
63
  },
62
64
  "dependencies": {
63
- "@parcha/agentrun-dsl": "0.1.0-beta.38",
65
+ "@parcha/agentrun-dsl": "0.1.0-beta.40",
64
66
  "ajv": "8.18.0",
65
67
  "typebox": "1.3.27"
66
68
  },
@@ -0,0 +1,61 @@
1
+ The o200k_base tokenizer in src/host/o200k.ts (dist/host/o200k.js) is built from two MIT-licensed sources.
2
+
3
+ o200k_base.tiktoken, beside this file, is OpenAI's o200k_base vocabulary, unchanged: the file tiktoken loads, from
4
+ https://openaipublic.blob.core.windows.net/encodings/o200k_base.tiktoken, sha256
5
+ 446a9538cb6c348e3516120d7c08b09f57c36495e2acfffe59a5bf8b0cfb1a2d, as pinned in tiktoken at commit
6
+ 79dc2c0533685b893616c78160b5d9e40876e724 (https://github.com/openai/tiktoken).
7
+
8
+ The split pattern is tiktoken's o200k_base pattern as gpt-tokenizer spells it for JavaScript, at commit
9
+ 785c2695acbda31ae31ef8ab6343bcd7f1dde72f, release 4.1.2 (https://github.com/niieani/gpt-tokenizer).
10
+
11
+ ---
12
+
13
+ tiktoken
14
+
15
+ MIT License
16
+
17
+ Copyright (c) 2022 OpenAI, Shantanu Jain
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy
20
+ of this software and associated documentation files (the "Software"), to deal
21
+ in the Software without restriction, including without limitation the rights
22
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
+ copies of the Software, and to permit persons to whom the Software is
24
+ furnished to do so, subject to the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be included in all
27
+ copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35
+ SOFTWARE.
36
+
37
+ ---
38
+
39
+ gpt-tokenizer
40
+
41
+ MIT License
42
+
43
+ Copyright (c) 2023-2024 Bazyli Brzoska
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining a copy
46
+ of this software and associated documentation files (the "Software"), to deal
47
+ in the Software without restriction, including without limitation the rights
48
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49
+ copies of the Software, and to permit persons to whom the Software is
50
+ furnished to do so, subject to the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be included in all
53
+ copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61
+ SOFTWARE.