Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(openai-codex): skip output parts for empty text() calls

Jack Rudenkoe3f0df15e178
diff --git a/ai-docs/architecture/code-mode.md b/ai-docs/architecture/code-mode.md
index 90155638..4028c4f2 100644
--- a/ai-docs/architecture/code-mode.md
+++ b/ai-docs/architecture/code-mode.md
@@ -308,7 +308,7 @@ promise that can never settle`). `blocked([])` is unrepresentable.
| calls per batch / per script | 32 / 256 | the excess call rejects with a string, so `Promise.allSettled` still finishes |
| one call input / one batch | 2 MiB / 8 MiB | that call rejects |
| tool result into the script | 1 MiB each (truncated, marked) / 8 MiB per script | rejects |
-| `text()` output | 1 MiB per exec, enforced by the HOST on what it receives (a tampered prelude can waste one bounded copy, never raise the total) | dropped, with a warning line |
+| `text()` output | 1 MiB per exec, enforced by the HOST on what it receives (a tampered prelude can waste one bounded copy, never raise the total); `text("")` adds no part; ≤ 1,024 parts (the store counts 64 bytes per part) | dropped, with a warning line; past 1,024 parts, text joins the last part |
| output tokens | `max_output_tokens` pragma clamped to [1, 50,000]; default discovery's `truncation_policy.limit` in tokens, else 10,000 | Codex's truncation header, at 4 B/token |
Why 32 MiB and 2 s: the heap limit stops one runtime's ordinary allocations; it bounds nothing
diff --git a/packages/cli/src/code-mode/sandbox/script-runtime.ts b/packages/cli/src/code-mode/sandbox/script-runtime.ts
index 93ad0025..95702128 100644
--- a/packages/cli/src/code-mode/sandbox/script-runtime.ts
+++ b/packages/cli/src/code-mode/sandbox/script-runtime.ts
@@ -104,6 +104,8 @@ export interface SandboxLimits {
resultBytes: number;
resultBytesPerScript: number;
outputBytes: number;
+ /** Output parts kept separate; past this, text joins the last text part. */
+ outputParts: number;
}
export const DEFAULT_SANDBOX_LIMITS: SandboxLimits = {
@@ -118,6 +120,7 @@ export const DEFAULT_SANDBOX_LIMITS: SandboxLimits = {
resultBytes: 1024 * 1024,
resultBytesPerScript: 8 * 1024 * 1024,
outputBytes: 1024 * 1024,
+ outputParts: 1_024,
};
export interface CreateScriptRuntimeOptions {
@@ -466,10 +469,8 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {
const length = ctx.getNumber(lengthH);
lengthH.dispose();
const remaining = this.limits.outputBytes - this.outputUsed;
- if (!(length > 0)) {
- this.parts.push({ type: "text", text: "" });
- return;
- }
+ // An empty string adds nothing the model can read, and would cost no budget.
+ if (!(length > 0)) return;
if (remaining <= 0) {
this.outputDropped += length;
return;
@@ -498,7 +499,24 @@ class QuickJSScriptRuntime implements ScriptRuntime, LeasedRuntime {
const kept = received.length > remaining ? received.slice(0, remaining) : received;
this.outputDropped += Math.max(0, length - kept.length);
this.outputUsed += kept.length;
- this.parts.push({ type: "text", text: kept });
+ if (kept.length === 0) return;
+ this.pushText(kept);
+ }
+
+ /**
+ * One text part per `text()` call, as Codex, up to `outputParts`; past that
+ * the text joins the last text part, so a loop of one-character calls keeps
+ * its bytes (already budgeted) without one host object per call.
+ */
+ private pushText(text: string): void {
+ if (this.parts.length >= this.limits.outputParts) {
+ const last = this.parts[this.parts.length - 1];
+ if (last?.type === "text") {
+ last.text += text;
+ return;
+ }
+ }
+ this.parts.push({ type: "text", text });
}
// ─── slices and settlement ──────────────────────────────────────────
diff --git a/packages/cli/src/code-mode/script-store.ts b/packages/cli/src/code-mode/script-store.ts
index b7857663..72e1a4d2 100644
--- a/packages/cli/src/code-mode/script-store.ts
+++ b/packages/cli/src/code-mode/script-store.ts
@@ -28,7 +28,8 @@
* Every store has a byte ceiling the model cannot raise (architecture §5.8):
* settled records (anything but running/suspended) are capped at 2,000 records
* AND 32 MiB, counting the UTF-8 length of the source, every stored output's
- * text and the reasoning items' JSON (images are references, never bytes).
+ * text plus 64 bytes per output part, and the reasoning items' JSON (images
+ * are references, never bytes).
* Eviction is LRU by `lastTouched`; an evicted record's id goes to a bounded
* FIFO set, so the input builder can say "evicted" rather than "restart".
* Suspended records are bounded by the sandbox's live-runtime cap (32) and a
@@ -159,9 +160,18 @@ function utf8Bytes(text: string): number {
return Buffer.byteLength(text, "utf8");
}
+/**
+ * What one stored output part costs beyond its text: the host object and its
+ * Responses part. Without it, parts with little or no text were free.
+ */
+const PART_OVERHEAD_BYTES = 64;
+
function outputBytes(output: ExecOutput): number {
let bytes = output.error ? utf8Bytes(output.error) : 0;
- for (const part of output.parts) if (part.type === "text") bytes += utf8Bytes(part.text);
+ for (const part of output.parts) {
+ bytes += PART_OVERHEAD_BYTES;
+ if (part.type === "text") bytes += utf8Bytes(part.text);
+ }
return bytes;
}