Repository navigation
Conversation
- boundToolResult bounds at the strictest budget (per-result cap and the context guard's weighted share incl. images), keeps head and tail, saves the full text; size-only notice when the reader cannot open the file. - Applied in runAgentHarnessToolInvocation (Codex dynamic tools, Agents API) and the Copilot bridge; embedded keeps its afterToolCall call site. - Deleted: Codex prefix-only cut (left the untrusted-content wrapper open), Agents API silent prefix cut, Tool Search whole-result omission.
Contributor
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
obviyus
force-pushed
the
core/result-bound-followup
branch
from
November 28, 2026 04:00
4e567f6 to
48cb75b
Compare
obviyus
force-pushed
the
core/result-bound-followup
branch
from
November 28, 2026 04:17
48cb75b to
d2017f7
Compare
…MCP serving paths - boundToolResult unwraps every text block that is exactly one envelope, reserves the frames, cuts bodies, and puts each surviving body (or the notice replacing it) back inside its own frame. - Gateway MCP loopback and plugin tools MCP server results go through boundToolResult (size-only notice) before content normalization.
obviyus
force-pushed
the
core/result-bound-followup
branch
from
November 28, 2026 04:33
d2017f7 to
aee9907
Compare
- A text block that is one envelope whose frame does not fit beside the other blocks is left out whole; one trusted notice with the true count follows the cut, outside every frame. Untrusted text never reaches the model unwrapped.
obviyus
added a commit
that referenced
this pull request
Nov 28, 2026
…wrapper intact (#207101) ## Problem A tool result that is too big for the model is cut differently on each route, and on two of them the cut is unsafe or loses data: - **Codex app-server dynamic tools** kept a prefix only and appended "rerun with narrower args". When the result was untrusted network content, the cut removed the wrapper's end marker, so the rest of the turn ran with an open untrusted-content region. Production (one team deployment, 7 days): 719 cut results in 102 sessions, **292 of them with the wrapper left open** (`github_read` 227, code-mode `exec` 59, `sessions_history` 7, `wait` 1). - **Agents API** cut a prefix with no notice and no file. - **Copilot** forwarded results with no bound at all (no production Copilot sessions in the window; found by reading the code). - **Embedded** (#206880) bounded at the per-result cap, which is looser than the context guard's budget below 32k-token windows and with images. The guard then cut again with no file path. ## Solution One bound, `boundToolResult`, for every route that executes OpenClaw tools: - **Budget:** the strictest one the result meets later: the per-result cap, then the context guard's weighted share with images and other blocks reserved first. A bounded result is a fixed point of both the transcript cap and the context guard, so the saved-file path survives. Unknown window: the 16k default. - **Cut:** head and tail (the existing truncator); the full text goes to an owner-only file (`writePrivateTempFile`). The notice is harness-neutral: `[... N chars omitted. Full output: <path>]`. A route whose model cannot open a Gateway file gets the same cut with a size-only notice: the Codex app-server (its native file tools run under its own sandbox policy, local or remote) and the Agents API (file tools run in the hosted VM). A path is named only where OpenClaw's own `read` opens it (embedded, Copilot). If the budget cannot hold the whole path, the notice names the size only. - **Untrusted content fails closed, per frame:** a frame is one `wrapExternalContent` region (warning, start marker and metadata header, body, end marker with the same id), found inside any text block, raw or JSON-escaped, so several wrapped values in one JSON block (`web_search`) count one by one (`findExternalContentFrames`, next to the writer). Frames and the text between them are kept whole, in order, while they fit; the first piece that does not fit keeps its ends and loses only body (head and tail); everything after it is left out. One trusted notice, `[... N chars omitted. Full output: <path>]`, follows the cut outside every frame, with the true count. A frame whose ends cannot fit (forged or hostile metadata, an image taking a small window's share) is left out whole: untrusted text never reaches the model outside a frame. When images alone exceed the context share, the bound reserves what the embedded context guard will keep (the images that fit plus its image-omission notice, one shared formatter), so the guard drops images without cutting the framed text again. Results with no frames keep the head-and-tail cut. - **Where:** `runAgentHarnessToolInvocation` after result middleware (Codex dynamic tools, Agents API); the Copilot bridge calls it directly (`boundAgentHarnessToolResult`, private harness SDK subpath); embedded keeps its `afterToolCall` call site. Copilot registers the file to the run's owner (the sandbox workspace for sandboxed runs), so its own `read` can open it. Codex `skills_read` stays whole-or-error: its check now uses the same budget as the bound, so a skill that passes is never cut later (an attempt budget smaller than the model window used to let a 12k skill pass and then be cut while reported as a success). Deleted: the Codex prefix-only cut and its "rerun" notice, the Agents API silent prefix cut. **Agents API serialization:** the adapter joins the bounded blocks with newlines and a 54-char placeholder per image into one function-output string, then applies its own cap. The bound reserves exactly what that serialization adds (`resultBound.reservedChars`, computed from the adapter's one `serializeFunctionOutput`), so the single cut leaves room for it and the adapter's cap never cuts a bounded result. The bound never adds a block (its notice goes into a text block it keeps; it only removes others), so the reserve computed before the cut covers the output. One cut, so the omitted count in the notice is true. Not in this PR: Tool Search's whole-result omission (3 unrecoverable omissions in 7 days in production) stays. Removing it is safe only where the model can read the saved file; on Codex and Agents API a partial page would show a continuation cursor with no way to read the omitted records. It returns once those routes can read the file. ## Security boundary This changes how untrusted content is cut on every route. Before: the Codex route dropped end markers (292 open wrappers in 7 days), and on the other routes several frames in one result could lose markers or leave untrusted text outside a frame. After: every frame the finder matches is either whole, or cut in its body with both ends kept, or left out; the omission notice is ours and sits outside every frame. Residuals, named, not fixed here: - A forged frame (raw tool output that copies the marker format) whose metadata is oversized is left out whole, and so is everything after it. Accepted: fail closed is the rule, and the saved file (where the reader can open it) still holds everything. - A start marker with no matching end marker is plain text and gets the plain cut; markers in another encoding (`\u003c`, HTML entities) are not found. The writers produce neither. - A cut JSON block is no longer valid JSON; its frames stay whole. - A spill file read back with `read` has no wrapper (same as the `web_fetch` precedent). - The two MCP serving paths stay unbounded: there, wrapped values sit inside JSON the tools build, and that needs an owner at the tools that wrap them. ## Follow-ups (not in this PR) - A recovery hint for size-only routes (Codex, Agents API), which only report the size today. - One fit predicate for `skills_read` and the bound, with the default budget inside `resolveToolResultBudget`. - Move the bound out of `embedded-agent-runner/` under one name. - Check the bound is not above Codex's own tool-output limit for dynamic tools. - Spill provenance so `read` wraps a spilled untrusted result; a spill size cap with a truthful notice; a retention rule. - Stage the spill file into the executor's filesystem for Codex and the Agents API; Tool Search's omission can go after that. - A per-turn aggregate budget on the Codex, Agents API and Copilot routes. - Carry provenance on the block (wrap after the cut) instead of finding frames in text. - The two unbounded MCP serving paths, once the tools that wrap JSON values own their bound. - A truthful notice when a tiny budget (reserve close to the cap) shortens the notice itself. ## Gaps - Two MCP serving paths (`mcp-http.handlers.ts`, `src/mcp/plugin-tools-handlers.ts`) have no tool-result bound; out of scope. - Agents API: if image placeholders alone exceed the per-result budget (hundreds of images in one result), the text budget clamps to zero and the adapter's cap cuts the placeholders. - Codex and Agents API results are cut with a size-only notice; the omitted middle is not recoverable there (it was not before either). Staging the file into the executor's filesystem is a follow-up. - CJK text is over-cut when the context budget binds. - Images: when images alone exceed the context share, the bound does not drop them; the embedded context guard does (as on `main`). On Codex and Copilot such images pass as on `main`, where no route budgeted images. Moving image omission into the bound is a separate change. ## Evidence Red on `main`, green here (same tests): | Route | Test | |---|---| | Codex | `skills_read` with a 16k-token attempt budget and a complete 12k skill: refused whole, not cut; a wrapped network result at 128k through the dynamic-tool bridge: one closed frame with head and tail, a size-only notice and no Gateway path; mixed text + image within the shared budget. The small-window and image cases live in the bound's fail-closed suite below, which every route calls | | Copilot | oversized result bounded before the SDK, file equals the full text | | Agents API | an image block before one oversized wrapped page: output starts with the image placeholder, keeps the warning, start and end markers with one id, and the notice's omitted count equals exactly the chars cut (at the previous head the output ended `<<<END_EXTE`); a header plus two wrapped pages that fit the 32k budget by one char before the joining newlines: all four markers kept, output within the cap | | All routes (`tool-result-bound.multi-block.test.ts`, 14 of these red on the previous head) | one helper checks every case: no untrusted text or filler outside a whole frame, every start marker closed with its id, exactly one notice outside frames with count = original − kept, and the real context guard, run after the bound, passes every text block unchanged. Cases: header + page at 4k/8k/16k/128k; two pages at 4k/16k/128k (second left out whole); 14 pages at 4k/8k; 200 small pages at 128k keep whole bodies; one JSON block with 5 wrapped values at 4k/8k, cut mid-way; image beside a page at 4k/8k/16k (a browser snapshot's shape; red one commit earlier, where the guard re-cut the frame after dropping the image); spill on: the notice names the file and the file holds the full text | | Embedded | a forged envelope with a 200k metadata header is left out whole with the notice; 16k-token window: the bound's output passes the context guard unchanged and keeps its path; spill write failure keeps head + tail with a size-only notice | Consumer suites pass; tests that pinned the Codex prefix cut were rewritten to the bound's contract. A producer's own spill path is also withheld from size-only routes. `agent-tools.prefix-stability` passes. Typecheck: core, core tests, extensions, extension tests. Supersedes #207205: its multi-block end-marker cases are covered here frame by frame; its MCP-path bounding is left out (see Security boundary). Co-authored-by: Ayaan Zaidi <[email protected]>
Collaborator
Author
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
Nov 29, 2026
…wrapper intact (openclaw#207101) ## Problem A tool result that is too big for the model is cut differently on each route, and on two of them the cut is unsafe or loses data: - **Codex app-server dynamic tools** kept a prefix only and appended "rerun with narrower args". When the result was untrusted network content, the cut removed the wrapper's end marker, so the rest of the turn ran with an open untrusted-content region. Production (one team deployment, 7 days): 719 cut results in 102 sessions, **292 of them with the wrapper left open** (`github_read` 227, code-mode `exec` 59, `sessions_history` 7, `wait` 1). - **Agents API** cut a prefix with no notice and no file. - **Copilot** forwarded results with no bound at all (no production Copilot sessions in the window; found by reading the code). - **Embedded** (openclaw#206880) bounded at the per-result cap, which is looser than the context guard's budget below 32k-token windows and with images. The guard then cut again with no file path. ## Solution One bound, `boundToolResult`, for every route that executes OpenClaw tools: - **Budget:** the strictest one the result meets later: the per-result cap, then the context guard's weighted share with images and other blocks reserved first. A bounded result is a fixed point of both the transcript cap and the context guard, so the saved-file path survives. Unknown window: the 16k default. - **Cut:** head and tail (the existing truncator); the full text goes to an owner-only file (`writePrivateTempFile`). The notice is harness-neutral: `[... N chars omitted. Full output: <path>]`. A route whose model cannot open a Gateway file gets the same cut with a size-only notice: the Codex app-server (its native file tools run under its own sandbox policy, local or remote) and the Agents API (file tools run in the hosted VM). A path is named only where OpenClaw's own `read` opens it (embedded, Copilot). If the budget cannot hold the whole path, the notice names the size only. - **Untrusted content fails closed, per frame:** a frame is one `wrapExternalContent` region (warning, start marker and metadata header, body, end marker with the same id), found inside any text block, raw or JSON-escaped, so several wrapped values in one JSON block (`web_search`) count one by one (`findExternalContentFrames`, next to the writer). Frames and the text between them are kept whole, in order, while they fit; the first piece that does not fit keeps its ends and loses only body (head and tail); everything after it is left out. One trusted notice, `[... N chars omitted. Full output: <path>]`, follows the cut outside every frame, with the true count. A frame whose ends cannot fit (forged or hostile metadata, an image taking a small window's share) is left out whole: untrusted text never reaches the model outside a frame. When images alone exceed the context share, the bound reserves what the embedded context guard will keep (the images that fit plus its image-omission notice, one shared formatter), so the guard drops images without cutting the framed text again. Results with no frames keep the head-and-tail cut. - **Where:** `runAgentHarnessToolInvocation` after result middleware (Codex dynamic tools, Agents API); the Copilot bridge calls it directly (`boundAgentHarnessToolResult`, private harness SDK subpath); embedded keeps its `afterToolCall` call site. Copilot registers the file to the run's owner (the sandbox workspace for sandboxed runs), so its own `read` can open it. Codex `skills_read` stays whole-or-error: its check now uses the same budget as the bound, so a skill that passes is never cut later (an attempt budget smaller than the model window used to let a 12k skill pass and then be cut while reported as a success). Deleted: the Codex prefix-only cut and its "rerun" notice, the Agents API silent prefix cut. **Agents API serialization:** the adapter joins the bounded blocks with newlines and a 54-char placeholder per image into one function-output string, then applies its own cap. The bound reserves exactly what that serialization adds (`resultBound.reservedChars`, computed from the adapter's one `serializeFunctionOutput`), so the single cut leaves room for it and the adapter's cap never cuts a bounded result. The bound never adds a block (its notice goes into a text block it keeps; it only removes others), so the reserve computed before the cut covers the output. One cut, so the omitted count in the notice is true. Not in this PR: Tool Search's whole-result omission (3 unrecoverable omissions in 7 days in production) stays. Removing it is safe only where the model can read the saved file; on Codex and Agents API a partial page would show a continuation cursor with no way to read the omitted records. It returns once those routes can read the file. ## Security boundary This changes how untrusted content is cut on every route. Before: the Codex route dropped end markers (292 open wrappers in 7 days), and on the other routes several frames in one result could lose markers or leave untrusted text outside a frame. After: every frame the finder matches is either whole, or cut in its body with both ends kept, or left out; the omission notice is ours and sits outside every frame. Residuals, named, not fixed here: - A forged frame (raw tool output that copies the marker format) whose metadata is oversized is left out whole, and so is everything after it. Accepted: fail closed is the rule, and the saved file (where the reader can open it) still holds everything. - A start marker with no matching end marker is plain text and gets the plain cut; markers in another encoding (`\u003c`, HTML entities) are not found. The writers produce neither. - A cut JSON block is no longer valid JSON; its frames stay whole. - A spill file read back with `read` has no wrapper (same as the `web_fetch` precedent). - The two MCP serving paths stay unbounded: there, wrapped values sit inside JSON the tools build, and that needs an owner at the tools that wrap them. ## Follow-ups (not in this PR) - A recovery hint for size-only routes (Codex, Agents API), which only report the size today. - One fit predicate for `skills_read` and the bound, with the default budget inside `resolveToolResultBudget`. - Move the bound out of `embedded-agent-runner/` under one name. - Check the bound is not above Codex's own tool-output limit for dynamic tools. - Spill provenance so `read` wraps a spilled untrusted result; a spill size cap with a truthful notice; a retention rule. - Stage the spill file into the executor's filesystem for Codex and the Agents API; Tool Search's omission can go after that. - A per-turn aggregate budget on the Codex, Agents API and Copilot routes. - Carry provenance on the block (wrap after the cut) instead of finding frames in text. - The two unbounded MCP serving paths, once the tools that wrap JSON values own their bound. - A truthful notice when a tiny budget (reserve close to the cap) shortens the notice itself. ## Gaps - Two MCP serving paths (`mcp-http.handlers.ts`, `src/mcp/plugin-tools-handlers.ts`) have no tool-result bound; out of scope. - Agents API: if image placeholders alone exceed the per-result budget (hundreds of images in one result), the text budget clamps to zero and the adapter's cap cuts the placeholders. - Codex and Agents API results are cut with a size-only notice; the omitted middle is not recoverable there (it was not before either). Staging the file into the executor's filesystem is a follow-up. - CJK text is over-cut when the context budget binds. - Images: when images alone exceed the context share, the bound does not drop them; the embedded context guard does (as on `main`). On Codex and Copilot such images pass as on `main`, where no route budgeted images. Moving image omission into the bound is a separate change. ## Evidence Red on `main`, green here (same tests): | Route | Test | |---|---| | Codex | `skills_read` with a 16k-token attempt budget and a complete 12k skill: refused whole, not cut; a wrapped network result at 128k through the dynamic-tool bridge: one closed frame with head and tail, a size-only notice and no Gateway path; mixed text + image within the shared budget. The small-window and image cases live in the bound's fail-closed suite below, which every route calls | | Copilot | oversized result bounded before the SDK, file equals the full text | | Agents API | an image block before one oversized wrapped page: output starts with the image placeholder, keeps the warning, start and end markers with one id, and the notice's omitted count equals exactly the chars cut (at the previous head the output ended `<<<END_EXTE`); a header plus two wrapped pages that fit the 32k budget by one char before the joining newlines: all four markers kept, output within the cap | | All routes (`tool-result-bound.multi-block.test.ts`, 14 of these red on the previous head) | one helper checks every case: no untrusted text or filler outside a whole frame, every start marker closed with its id, exactly one notice outside frames with count = original − kept, and the real context guard, run after the bound, passes every text block unchanged. Cases: header + page at 4k/8k/16k/128k; two pages at 4k/16k/128k (second left out whole); 14 pages at 4k/8k; 200 small pages at 128k keep whole bodies; one JSON block with 5 wrapped values at 4k/8k, cut mid-way; image beside a page at 4k/8k/16k (a browser snapshot's shape; red one commit earlier, where the guard re-cut the frame after dropping the image); spill on: the notice names the file and the file holds the full text | | Embedded | a forged envelope with a 200k metadata header is left out whole with the notice; 16k-token window: the bound's output passes the context guard unchanged and keeps its path; spill write failure keeps head + tail with a size-only notice | Consumer suites pass; tests that pinned the Codex prefix cut were rewritten to the bound's contract. A producer's own spill path is also withheld from size-only routes. `agent-tools.prefix-stability` passes. Typecheck: core, core tests, extensions, extension tests. Supersedes openclaw#207205: its multi-block end-marker cases are covered here frame by frame; its MCP-path bounding is left out (see Security boundary). Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot
pushed a commit
that referenced
this pull request
Nov 30, 2026
…wrapper intact (#207101) ## Problem A tool result that is too big for the model is cut differently on each route, and on two of them the cut is unsafe or loses data: - **Codex app-server dynamic tools** kept a prefix only and appended "rerun with narrower args". When the result was untrusted network content, the cut removed the wrapper's end marker, so the rest of the turn ran with an open untrusted-content region. Production (one team deployment, 7 days): 719 cut results in 102 sessions, **292 of them with the wrapper left open** (`github_read` 227, code-mode `exec` 59, `sessions_history` 7, `wait` 1). - **Agents API** cut a prefix with no notice and no file. - **Copilot** forwarded results with no bound at all (no production Copilot sessions in the window; found by reading the code). - **Embedded** (#206880) bounded at the per-result cap, which is looser than the context guard's budget below 32k-token windows and with images. The guard then cut again with no file path. ## Solution One bound, `boundToolResult`, for every route that executes OpenClaw tools: - **Budget:** the strictest one the result meets later: the per-result cap, then the context guard's weighted share with images and other blocks reserved first. A bounded result is a fixed point of both the transcript cap and the context guard, so the saved-file path survives. Unknown window: the 16k default. - **Cut:** head and tail (the existing truncator); the full text goes to an owner-only file (`writePrivateTempFile`). The notice is harness-neutral: `[... N chars omitted. Full output: <path>]`. A route whose model cannot open a Gateway file gets the same cut with a size-only notice: the Codex app-server (its native file tools run under its own sandbox policy, local or remote) and the Agents API (file tools run in the hosted VM). A path is named only where OpenClaw's own `read` opens it (embedded, Copilot). If the budget cannot hold the whole path, the notice names the size only. - **Untrusted content fails closed, per frame:** a frame is one `wrapExternalContent` region (warning, start marker and metadata header, body, end marker with the same id), found inside any text block, raw or JSON-escaped, so several wrapped values in one JSON block (`web_search`) count one by one (`findExternalContentFrames`, next to the writer). Frames and the text between them are kept whole, in order, while they fit; the first piece that does not fit keeps its ends and loses only body (head and tail); everything after it is left out. One trusted notice, `[... N chars omitted. Full output: <path>]`, follows the cut outside every frame, with the true count. A frame whose ends cannot fit (forged or hostile metadata, an image taking a small window's share) is left out whole: untrusted text never reaches the model outside a frame. When images alone exceed the context share, the bound reserves what the embedded context guard will keep (the images that fit plus its image-omission notice, one shared formatter), so the guard drops images without cutting the framed text again. Results with no frames keep the head-and-tail cut. - **Where:** `runAgentHarnessToolInvocation` after result middleware (Codex dynamic tools, Agents API); the Copilot bridge calls it directly (`boundAgentHarnessToolResult`, private harness SDK subpath); embedded keeps its `afterToolCall` call site. Copilot registers the file to the run's owner (the sandbox workspace for sandboxed runs), so its own `read` can open it. Codex `skills_read` stays whole-or-error: its check now uses the same budget as the bound, so a skill that passes is never cut later (an attempt budget smaller than the model window used to let a 12k skill pass and then be cut while reported as a success). Deleted: the Codex prefix-only cut and its "rerun" notice, the Agents API silent prefix cut. **Agents API serialization:** the adapter joins the bounded blocks with newlines and a 54-char placeholder per image into one function-output string, then applies its own cap. The bound reserves exactly what that serialization adds (`resultBound.reservedChars`, computed from the adapter's one `serializeFunctionOutput`), so the single cut leaves room for it and the adapter's cap never cuts a bounded result. The bound never adds a block (its notice goes into a text block it keeps; it only removes others), so the reserve computed before the cut covers the output. One cut, so the omitted count in the notice is true. Not in this PR: Tool Search's whole-result omission (3 unrecoverable omissions in 7 days in production) stays. Removing it is safe only where the model can read the saved file; on Codex and Agents API a partial page would show a continuation cursor with no way to read the omitted records. It returns once those routes can read the file. ## Security boundary This changes how untrusted content is cut on every route. Before: the Codex route dropped end markers (292 open wrappers in 7 days), and on the other routes several frames in one result could lose markers or leave untrusted text outside a frame. After: every frame the finder matches is either whole, or cut in its body with both ends kept, or left out; the omission notice is ours and sits outside every frame. Residuals, named, not fixed here: - A forged frame (raw tool output that copies the marker format) whose metadata is oversized is left out whole, and so is everything after it. Accepted: fail closed is the rule, and the saved file (where the reader can open it) still holds everything. - A start marker with no matching end marker is plain text and gets the plain cut; markers in another encoding (`\u003c`, HTML entities) are not found. The writers produce neither. - A cut JSON block is no longer valid JSON; its frames stay whole. - A spill file read back with `read` has no wrapper (same as the `web_fetch` precedent). - The two MCP serving paths stay unbounded: there, wrapped values sit inside JSON the tools build, and that needs an owner at the tools that wrap them. ## Follow-ups (not in this PR) - A recovery hint for size-only routes (Codex, Agents API), which only report the size today. - One fit predicate for `skills_read` and the bound, with the default budget inside `resolveToolResultBudget`. - Move the bound out of `embedded-agent-runner/` under one name. - Check the bound is not above Codex's own tool-output limit for dynamic tools. - Spill provenance so `read` wraps a spilled untrusted result; a spill size cap with a truthful notice; a retention rule. - Stage the spill file into the executor's filesystem for Codex and the Agents API; Tool Search's omission can go after that. - A per-turn aggregate budget on the Codex, Agents API and Copilot routes. - Carry provenance on the block (wrap after the cut) instead of finding frames in text. - The two unbounded MCP serving paths, once the tools that wrap JSON values own their bound. - A truthful notice when a tiny budget (reserve close to the cap) shortens the notice itself. ## Gaps - Two MCP serving paths (`mcp-http.handlers.ts`, `src/mcp/plugin-tools-handlers.ts`) have no tool-result bound; out of scope. - Agents API: if image placeholders alone exceed the per-result budget (hundreds of images in one result), the text budget clamps to zero and the adapter's cap cuts the placeholders. - Codex and Agents API results are cut with a size-only notice; the omitted middle is not recoverable there (it was not before either). Staging the file into the executor's filesystem is a follow-up. - CJK text is over-cut when the context budget binds. - Images: when images alone exceed the context share, the bound does not drop them; the embedded context guard does (as on `main`). On Codex and Copilot such images pass as on `main`, where no route budgeted images. Moving image omission into the bound is a separate change. ## Evidence Red on `main`, green here (same tests): | Route | Test | |---|---| | Codex | `skills_read` with a 16k-token attempt budget and a complete 12k skill: refused whole, not cut; a wrapped network result at 128k through the dynamic-tool bridge: one closed frame with head and tail, a size-only notice and no Gateway path; mixed text + image within the shared budget. The small-window and image cases live in the bound's fail-closed suite below, which every route calls | | Copilot | oversized result bounded before the SDK, file equals the full text | | Agents API | an image block before one oversized wrapped page: output starts with the image placeholder, keeps the warning, start and end markers with one id, and the notice's omitted count equals exactly the chars cut (at the previous head the output ended `<<<END_EXTE`); a header plus two wrapped pages that fit the 32k budget by one char before the joining newlines: all four markers kept, output within the cap | | All routes (`tool-result-bound.multi-block.test.ts`, 14 of these red on the previous head) | one helper checks every case: no untrusted text or filler outside a whole frame, every start marker closed with its id, exactly one notice outside frames with count = original − kept, and the real context guard, run after the bound, passes every text block unchanged. Cases: header + page at 4k/8k/16k/128k; two pages at 4k/16k/128k (second left out whole); 14 pages at 4k/8k; 200 small pages at 128k keep whole bodies; one JSON block with 5 wrapped values at 4k/8k, cut mid-way; image beside a page at 4k/8k/16k (a browser snapshot's shape; red one commit earlier, where the guard re-cut the frame after dropping the image); spill on: the notice names the file and the file holds the full text | | Embedded | a forged envelope with a 200k metadata header is left out whole with the notice; 16k-token window: the bound's output passes the context guard unchanged and keeps its path; spill write failure keeps head + tail with a size-only notice | Consumer suites pass; tests that pinned the Codex prefix cut were rewritten to the bound's contract. A producer's own spill path is also withheld from size-only routes. `agent-tools.prefix-stability` passes. Typecheck: core, core tests, extensions, extension tests. Supersedes #207205: its multi-block end-marker cases are covered here frame by frame; its MCP-path bounding is left out (see Security boundary). Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit e1599de) Release adaptation: Use the stable result truncator for embedded context and size caps; preserve paired untrusted-content markers under a single weighted budget. Bound the actual serialized Agents API and Copilot text without changing binary/image handling, tool receipts, existing spill access, or native Codex. Companion audit: PR #207101; pinned scan passed. The selected donor already covers the framed result cases from superseded #207205; leave native Codex and MCP-path changes out. All three baseline assertions failed at the expected tail/budget boundaries; the negative proof was reused after normalizing ANSI output. Co-authored-by: RomneyDa <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #207101 (stacked on its head
2ad43647). Merge only after #207101, and only with the owner's OK: this touches the same untrusted-content boundary.Problem
Two gaps left open by #207101's one tool-result bound (
boundToolResult):src/gateway/mcp-http.handlers.ts, used by CLI backends) and the plugin tools MCP server (src/mcp/plugin-tools-handlers.ts).Solution
boundToolResult(one owner), every text block that is exactly one envelope is unwrapped to its body; the frames are reserved in both budgets; after the cut each surviving body goes back inside its own whole frame. The body carries its frame on a module-private symbol key, which the truncator copies with the block. When the truncator keeps whole leading blocks and replaces the first omitted block with a notice, that notice is placed inside the omitted block's frame. If the summed frames do not fit beside the non-text blocks (hostile metadata), the text is cut as plain text, as in fix(agents): one tool-result bound on every route, untrusted-content wrapper intact #207101.boundToolResult(result, undefined, false)on results with a content array, before content normalization. Observers, receipts andisErrorstill read the raw result. Plain strings and other JSON results keep their existing normalization.Decision for the owner (MCP paths)
Gaps (named, not fixed)
Agents API (from fix(agents): one tool-result bound on every route, untrusted-content wrapper intact #207101, not changed here): the adapter joins the bounded blocks with newlines and image placeholders, then cuts the joined string with a raw prefix slice at the cap. When the joining overhead pushes the string over the cap, that slice can cut the last characters of a closing marker (reproduced: a header and two wrapped pages that fit by one char before joining). Two fixes were tried and dropped: bounding the joined string again replaces the first notice with a false count ("67 chars omitted" where 217k were cut); dropping the final cap lets output exceed the cap by one char per block and 54 per image. A correct fix needs the owner pass to budget the adapter's serialization, which is a design choice for the owner; the tried patch is kept on a parked branch.
Frames are all-or-nothing: with many envelope blocks in one result (for example 12+ at a 4,000-token window, 25+ at 8,000), the summed frames do not fit and every block is cut as plain text. No known producer emits that many.
A notice placed inside an omitted block's frame sits in an untrusted region and its count covers following blocks too (same class as fix(agents): one tool-result bound on every route, untrusted-content wrapper intact #207101's residual).
MCP results whose
contentis a string, or that have no content array, are not bounded (unchanged).Evidence
Red on #207101's head, green here:
undefined/null/ number results still succeedConsumer suites for #207101's routes (embedded, Codex, Copilot, Agents API, Tool Search) and the MCP server suites pass; prefix stability passes. Typecheck: core, core tests.