@parcha/agentrun-harness-codex 0.1.0-beta.43 → 0.1.0-beta.45

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 CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## 0.1.0-beta.44 (2026-11-17)
6
+
7
+ - A Codex run under a small spend cap no longer stops as early on a hold that overstated its next request. <!-- changes/codex-cap-hold-billed-after.md -->
8
+
9
+ A request that only adds items to the session's last billed request is now held at that bill plus the bytes of the items it adds, with the local count's margin. The bill is all of that request's `input_tokens`, cached tokens included. Any other difference falls back to the local count, as before: changed instructions, tools, reasoning or model, or an earlier item edited, removed or moved.
10
+
11
+ Once the run has seen its model reason, a request's least output is 1,024 tokens plus twice the most reasoning one answer reported, within its effort's allowance. Before that it is the whole allowance, as before.
12
+
13
+ The request is still sent with all the output the cap fits, and the hold is still its worst case, so the cap is never passed. When the provider cuts an answer at the output the cap limited it to, the turn says so. A final answer cut that way ends the run with "the run's $0.50 cap stopped the model at N tokens before it finished its answer: raise the cap to at least $X to let it finish", where $X is the cap that sends that request whole with the run's wrap-up reserve still kept beside it.
14
+
15
+ An image in a custom tool call's output (Codex code mode's exec) is now counted at its billed tokens, as one in a function call's output already was.
16
+
5
17
  ## 0.1.0-beta.39 (2026-11-16)
6
18
 
7
19
  - A Codex or OpenCode request straight to OpenAI is no longer counted by OpenAI before it is sent: it is held at the kernel's local o200k_base count with its margin, as a request through a gateway already is, so it never waits up to 3 seconds on `POST /v1/responses/input_tokens` first. A capped run on such a route may pause a little sooner than it did on OpenAI's exact count; the cap is still never passed.
package/dist/adapter.js CHANGED
@@ -53,6 +53,13 @@ function adapterEvent(event, hostName) {
53
53
  return { type: "tool_end", callId: String(e.callId ?? ""), name: hostName(e.name), isError: e.isError === true, ...(e.truncated === true ? { truncated: true } : {}) };
54
54
  return undefined;
55
55
  }
56
+ /** A turn entry's `capCut` when it is one (two money figures), as `{ capCut }`. */
57
+ const capCutOf = (value) => {
58
+ if (typeof value !== "object" || value === null)
59
+ return undefined;
60
+ const { capUsd, raiseToUsd } = value;
61
+ return typeof capUsd === "number" && Number.isFinite(capUsd) && typeof raiseToUsd === "number" && Number.isFinite(raiseToUsd) ? { capCut: { capUsd, raiseToUsd } } : undefined;
62
+ };
56
63
  const turnsIn = (drive) => transcriptOf(drive.state()).filter((r) => r.data?.type === "turn");
57
64
  /** Where Codex finds a user's skills: `$CODEX_HOME/skills`. It lists those to the model in a turn with no environment
58
65
  * (this adapter's, with the built-ins off), where it lists none of a working directory's `.agents/skills`. */
@@ -231,13 +238,13 @@ export function codexAdapter(options) {
231
238
  running = (async () => {
232
239
  let outcome;
233
240
  // A turn whose answer the model cut at its output limit says so on its turn_end: stopReason "length", with the cut
234
- // answer's output tokens when the provider said them.
241
+ // answer's output tokens when the provider said them, and the cap's figures when the limit was the cap's.
235
242
  let stop = {};
236
243
  try {
237
244
  await harness.drive(current);
238
245
  const ended = turnsIn(current).slice(before).at(-1)?.data;
239
246
  if (ended?.stopReason === "length")
240
- stop = { stopReason: "length", ...(typeof ended.outputTokens === "number" ? { outputTokens: ended.outputTokens } : {}) };
247
+ stop = { stopReason: "length", ...(typeof ended.outputTokens === "number" ? { outputTokens: ended.outputTokens } : {}), ...(capCutOf(ended.capCut) ?? {}) };
241
248
  outcome = ended?.status ? { status: ended.status, ...(ended.error ? { error: ended.error } : {}) } : { status: "failed", error: "the turn ended with no outcome in the session's records" };
242
249
  }
243
250
  catch (error) {
package/dist/harness.js CHANGED
@@ -758,7 +758,7 @@ export function codexHarness(options = {}) {
758
758
  active = undefined;
759
759
  // Its answer cut at the output limit: the turn was interrupted for it, and ended there, its answer unfinished.
760
760
  if (t.cut)
761
- return { status: 'completed', stopReason: 'length', ...(t.cut.outputTokens !== undefined ? { outputTokens: t.cut.outputTokens } : {}) };
761
+ return { status: 'completed', stopReason: 'length', ...(t.cut.outputTokens !== undefined ? { outputTokens: t.cut.outputTokens } : {}), ...(t.cut.capCut ? { capCut: t.cut.capCut } : {}) };
762
762
  const status = result.status === 'completed' ? 'completed' : result.status === 'interrupted' ? 'interrupted' : 'failed';
763
763
  const error = status === 'completed' ? undefined : (t.error ?? result.error?.message ?? undefined);
764
764
  return { status, ...(error ? { error } : {}) };
@@ -1,4 +1,4 @@
1
- import { type ModelProxyOptions, type ProviderPolicy, type ProxyRoute, type ProxyUsage } from "@parcha/agentrun-kernel/host";
1
+ import { type ModelProxyOptions, type ProviderPolicy, type ProxyRoute, type ProxyUsage, type ReasoningSeen } from "@parcha/agentrun-kernel/host";
2
2
  export declare const CODEX_RESPONSES_ROUTES: readonly ProxyRoute[];
3
3
  /** The price of one web search call on `model`, or undefined when it is not known. */
4
4
  export declare function webSearchUsd(model: string): number | undefined;
@@ -78,9 +78,12 @@ export declare const ANSWER_TOKENS_LEAST = 1024;
78
78
  export declare const REASONING_TOKENS_ALLOWED: Readonly<Record<string, number>>;
79
79
  /** The output tokens a Responses body asks for at most: its `max_output_tokens` (as `apply` clamped it). */
80
80
  export declare function responsesOutputAsked(body: Record<string, unknown>): number | undefined;
81
- /** The least output a Responses body may be limited to and still answer: 1,024 tokens, plus its reasoning effort's
82
- * allowance when it reasons. */
83
- export declare function responsesOutputLeast(body: Record<string, unknown>): number;
81
+ /** The least output a Responses body may be limited to and still answer: 1,024 tokens, plus, when it reasons, its
82
+ * reasoning effort's allowance, or, once the run has seen its model reason (`seen.most`, the most reasoning tokens one
83
+ * of its answers reported), twice that most, never past the allowance. OpenAI's reasoning shares the output limit, so a
84
+ * limit is a least, not a budget: the request is still sent with all the output the cap fits, and an answer the limit
85
+ * cuts is said to be the cap's cut (ModelTicket.capCut). */
86
+ export declare function responsesOutputLeast(body: Record<string, unknown>, seen?: ReasoningSeen): number;
84
87
  /** The reasoning effort a Responses body asks its model at (`reasoning.effort`), when it names one. */
85
88
  export declare function responsesEffort(body: Record<string, unknown>): string | undefined;
86
89
  /** The reasoning tokens a Responses request may bill that its stream never shows (only a summary of its reasoning
@@ -1,5 +1,5 @@
1
1
  import { PLAN_LIMIT_CODE, PLAN_LIMIT_WORDS } from "@parcha/agentrun-kernel";
2
- import { lastObjectOf, NATIVE_SEARCH_USD, ProxyRefusal, responsesInputOverhead, responsesLocalInputTokens } from "@parcha/agentrun-kernel/host";
2
+ import { lastObjectOf, NATIVE_SEARCH_USD, ProxyRefusal, responsesInputOverhead, responsesInputAfter, responsesLocalInputTokens } from "@parcha/agentrun-kernel/host";
3
3
  export const CODEX_RESPONSES_ROUTES = Object.freeze([
4
4
  { method: "GET", path: "/", local: 404 },
5
5
  { method: "POST", path: "/v1/responses", headers: ["accept"] },
@@ -141,7 +141,10 @@ function usageFrom(usage) {
141
141
  const details = isPlain(usage.input_tokens_details) ? usage.input_tokens_details : {};
142
142
  const cacheRead = count(details.cached_tokens) ?? 0;
143
143
  const cacheWrite = count(details.cache_write_tokens) ?? 0;
144
- return { input: Math.max(0, input - cacheRead - cacheWrite), output, cacheRead, cacheWrite };
144
+ // The reasoning tokens of its output, when it says them: what the run has seen its model reason (ProxyUsage.reasoning).
145
+ // Its billed input, input + cacheRead + cacheWrite, is `input_tokens` whole: the cached tokens are part of it.
146
+ const reasoning = isPlain(usage.output_tokens_details) ? count(usage.output_tokens_details.reasoning_tokens) : undefined;
147
+ return { input: Math.max(0, input - cacheRead - cacheWrite), output, cacheRead, cacheWrite, ...(reasoning === undefined ? {} : { reasoning }) };
145
148
  }
146
149
  /** The object starting at `text[start]` (a `{`), as JSON text, or undefined when it does not close in `text`. Strings are
147
150
  * skipped whole, so a brace inside one is not counted. */
@@ -480,11 +483,17 @@ export function responsesOutputAsked(body) {
480
483
  const asked = body.max_output_tokens;
481
484
  return typeof asked === "number" && Number.isSafeInteger(asked) && asked > 0 ? asked : undefined;
482
485
  }
483
- /** The least output a Responses body may be limited to and still answer: 1,024 tokens, plus its reasoning effort's
484
- * allowance when it reasons. */
485
- export function responsesOutputLeast(body) {
486
+ /** The least output a Responses body may be limited to and still answer: 1,024 tokens, plus, when it reasons, its
487
+ * reasoning effort's allowance, or, once the run has seen its model reason (`seen.most`, the most reasoning tokens one
488
+ * of its answers reported), twice that most, never past the allowance. OpenAI's reasoning shares the output limit, so a
489
+ * limit is a least, not a budget: the request is still sent with all the output the cap fits, and an answer the limit
490
+ * cuts is said to be the cap's cut (ModelTicket.capCut). */
491
+ export function responsesOutputLeast(body, seen) {
486
492
  const effort = responsesEffort(body);
487
- return ANSWER_TOKENS_LEAST + (effort === undefined ? 0 : REASONING_TOKENS_ALLOWED[effort] ?? 0);
493
+ const allowed = effort === undefined ? 0 : REASONING_TOKENS_ALLOWED[effort] ?? 0;
494
+ const most = seen?.most;
495
+ const reasoning = typeof most === "number" && Number.isSafeInteger(most) && most >= 0 ? Math.min(allowed, 2 * most) : allowed;
496
+ return ANSWER_TOKENS_LEAST + reasoning;
488
497
  }
489
498
  /** The reasoning effort a Responses body asks its model at (`reasoning.effort`), when it names one. */
490
499
  export function responsesEffort(body) {
@@ -610,6 +619,8 @@ export function codexPolicy(options) {
610
619
  ...(plan ? {} : {
611
620
  limitOutput: limitResponsesOutput,
612
621
  countInputLocally: (body) => (search && Array.isArray(body.tools) && body.tools.some((tool) => isPlain(tool) && tool.type === "web_search") ? undefined : responsesLocalInputTokens(body)),
622
+ // A later request of the session, bounded by the earlier one its provider billed and the items it appends.
623
+ inputAfter: responsesInputAfter,
613
624
  }),
614
625
  // The content a web search feeds the model is billed as input, and neither it nor the searches one response runs has a
615
626
  // stated bound: a body that offers web search has no input bound (undefined), so its input is unmeasured (and never
@@ -1,5 +1,5 @@
1
1
  import { toolBridge as kernelToolBridge, type ToolBridge, type ToolBridgeOptions } from "@parcha/agentrun-kernel/process";
2
- import type { JsonValue, ToolImage } from "@parcha/agentrun-kernel";
2
+ import type { CapCut, JsonValue, ToolImage } from "@parcha/agentrun-kernel";
3
3
  import type { TomlValue } from "../config.js";
4
4
  import type { DriveContext } from "./kernel.js";
5
5
  export { boundResult, BuiltinCalls, failureText, Mutex, NativeJournal, openTurn, placeSubmissions, recoveryNote, recoverySystemNote, recoveryUserTurn, restartEvent, resumeUnstartedCalls, toolKey, transcriptOf, } from "@parcha/agentrun-kernel/process";
@@ -87,6 +87,7 @@ export interface SessionMachine {
87
87
  * piece that carries the stop (its output tokens, when the answer said them): the harness ends the turn there. */
88
88
  onCut?(listener: (stop: {
89
89
  outputTokens?: number;
90
+ capCut?: CapCut;
90
91
  }) => void): void;
91
92
  /** The harness opens a turn: the model proxy, which refuses answer requests once it has told the harness a turn's answer
92
93
  * stopped, admits them again (ModelProxy.openTurn). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-harness-codex",
3
- "version": "0.1.0-beta.43",
3
+ "version": "0.1.0-beta.45",
4
4
  "description": "Codex as an AgentRun harness: a tool-less Codex app-server behind a loopback model proxy, durable in AgentRun's own store, with the host's tools as its only tools.",
5
5
  "keywords": [
6
6
  "agentrun",
@@ -44,7 +44,7 @@
44
44
  "test": "node --test --test-concurrency=8 'test/*.test.mjs'"
45
45
  },
46
46
  "dependencies": {
47
- "@parcha/agentrun-kernel": "0.1.0-beta.43"
47
+ "@parcha/agentrun-kernel": "0.1.0-beta.45"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "@openai/codex": "0.164.0"
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "devDependencies": {
58
58
  "@openai/codex": "0.164.0",
59
- "@parcha/agentrun-dsl": "0.1.0-beta.43"
59
+ "@parcha/agentrun-dsl": "0.1.0-beta.45"
60
60
  },
61
61
  "repository": {
62
62
  "type": "git",