Repository navigation
fix: make tool validation and truncation feedback actionable - #201795
Conversation
Co-authored-by: jacobtomlinson <[email protected]>
Worked on by: - @jacobtomlinson Co-authored-by: jacobtomlinson <[email protected]> OpenClaw-Publication: cd7ed16f-26c4-8ec1-94ab-e525f6e80b0c
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
Co-authored-by: jacobtomlinson <[email protected]>
Worked on by: - @jacobtomlinson Co-authored-by: jacobtomlinson <[email protected]> OpenClaw-Publication: fc7b69ad-8ad9-8b40-a881-dfdf5d2dec27
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Codex review: needs maintainer review before merge. What this changesThis PR adds exact schema-inspection guidance to validation errors and preserves readable recovery notices in oversized multi-block tool results. Example: A tool returns 1,000 text records under a 16,000-character result cap.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR addresses concrete feedback failures with no actionable findings remaining; exact-head CI is still in progress. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherTool Search validates agent calls, while the embedded runner budgets returned content and restores frozen projections before sending subsequent provider requests. flowchart TD
A[Agent tool call] --> B[Catalog validation]
B --> C[Schema recovery guidance]
B --> D[Tool execution]
D --> E[Shared result budgeting]
F[Frozen projection checkpoint] --> E
E --> G[Provider request]
Technical reviewBest possible solution: Retain the focused repair through existing validation, budgeting, and projection owners. Do we have a high-confidence way to reproduce the issue? Base source proportionally clips per-block notices when their combined cost exceeds the cap and omits exact schema-recovery guidance; the supplied before/after captures exercise both failures. Is this the best way to solve the issue? The patch repairs the shared allocator and existing error formatter, preserves canonical non-text content and frozen replay, and adds distinct regressions without unnecessary production machinery. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 44267df6c5ea. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +66, Tests +166, Docs +1, Other 0. Total +233 across 11 files. Added test files: 2. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (2 earlier review cycles)Reviewed November 19, 2026, 10:12 AM ET / 15:12 UTC (Revision 3). |
… images Co-authored-by: jacobtomlinson <[email protected]>
Worked on by: - @jacobtomlinson Co-authored-by: jacobtomlinson <[email protected]> OpenClaw-Publication: 0add01a0-079b-8383-b1d0-624a4713e4c3
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Co-authored-by: jacobtomlinson <[email protected]>
Worked on by: - @jacobtomlinson Co-authored-by: jacobtomlinson <[email protected]> OpenClaw-Publication: fca8f024-bb61-81d3-a65b-2053246258a8
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
…ecovered (#206880) ## What Problem This Solves When a tool returns more text than its context budget, OpenClaw keeps only the start, discards the rest, and tells the model to "rerun with narrower args". The end of the output, where commands print errors, totals and exit status, is lost unless a keyword list happens to match it. MCP and plugin tools have no other copy, so the model cannot get the cut text back. Production signal (a production deployment, 2.76 days of Gateway logs): - 102 `[tool-result-truncation] Truncated N tool result(s) for prompt history (maxChars=64000 …)` lines, up to 96 results in one session. - 310 context-overflow diagnostics in the same window, driven by long sessions full of tool output. Reproduced on main (`ab296c23d2c4`) through the real record path (`guardSessionManager`, 200k-token window). The fixture is a 180,015-character plain log, the same for exec, read, web_fetch, an MCP result and a plugin result: | | main | this PR | |---|---|---| | Characters the model sees | 63,973 | 63,961 | | Last line kept | no | yes | | Full output recoverable | no (dropped) | yes, 180,015 chars in a file | | Notice | `[... 116116 more characters truncated; rerun with narrower args if needed]` | `[... 116221 chars omitted. Full output: <path> (page it with read offset/limit)]` | ## Fix The bound has one owner, at record time: - `boundToolResult` (`src/agents/embedded-agent-runner/tool-result-bound.ts`) runs once at the end of `afterToolCall` (`run/attempt-setup.ts`), after tool-result hooks and before the result is first sent. It applies to every executed tool: core, MCP and plugin. - It keeps the head and the tail and puts the notice at the cut. The full text goes to an owner-only file through the existing `writePrivateTempFile` (tmpdir, 0600; staged in the workspace for sandboxed sessions), the same rule as `exec` and `web_fetch` output files. A tool that already saved its own file (`exec`, `web_fetch`) keeps that file, and the notice names it. - The budget comes from the transcript guard's registry, so the existing synchronous cap leaves a bounded result byte for byte. Prefix stability holds, because a recorded result never changes after it is recorded. - If the file write fails, the result passes on unchanged, and the existing transcript cap bounds it with head, tail and the character count. Deleted: - `context-truncation-notice.ts`: the "rerun with narrower args" notice and its `sessions_send` variant. - `hasImportantTail`, a keyword regex that decided whether the tail survived, and the head-only branch it guarded. Head+tail is now unconditional for any window over 4,000 characters. Smaller windows keep a whole prefix, so short records are not split. Fixed on the way (exposed by the shorter notice): - The aggregate planner sized notices with a count of 1, so a notice carrying a real count could be cut. That dropped the spill path from a disclosed `Full output:` pointer. It now sizes the notice for the largest count it can show. - A multi-block result made only of short records keeps whole leading records with one notice (#201795) instead of cutting every record. ## User impact - Gateway and one-shot (`agent --local`) users on the embedded runtime. Both run `installEmbeddedAttemptContextGuards` (`src/agents/embedded-agent-runner/run/attempt-session-runtime-prepare.ts:515`). - A large tool result keeps its last lines (errors, totals, exit status), and the model can read the omitted middle from the named file instead of rerunning the tool. - No config change. Results within budget are unchanged. - Not covered: the Codex app-server and Claude CLI runtimes, which do not run this loop. ## Evidence - `src/agents/tool-result-bounds.test.ts`: one case per tool family through the real guard registry: - exec (`buildExecForegroundResult`), MCP (`projectMcpCallToolResult`) and plugin: head, tail, spill file with the full text, and the recorded bytes equal to the bound bytes (fixed point); - web_fetch reuses its own spill file; - read reaches the middle of a 180k one-line spill page by page, and no page needs a second spill; - a failed spill write falls back to head, tail and the count, with no path. - `run/attempt-context-guards.test.ts`: the installed `afterToolCall` bounds the result an earlier hook returned and restores the previous hook on removal. - Touched suites pass (8 files), and `agent-tools.prefix-stability` passes. All core test typecheck shards pass. - The prompt-cache prefix-stability journeys fail the same way on the merge base `ab296c23d2c4`, so these failures are not from this change: - They expect a summary marker ("compacted into the following summary") that #205584 replaced. - Google also counts 2 compactions in its overflow run where the test expects 1 (`expected 2 to be 1`, reproduced on the base). - The breaks they report are the allowed compactions; no tool-result bytes change after they are recorded. - On main, the same contract test fails 5 of 5 families (63,973 of 180,015 characters kept, tail lost, no file). - **End to end, real model.** Isolated `openclaw agent --local` (scratch HOME, state and config), `openai/gpt-5.4-mini` on the embedded runtime. The agent runs `seq 1 60000 | sed 's/$/ padding padding padding/'` through `exec`, calls no other tool, and replies with the last line of the output: - main `ab296c23d2c4`: `UNKNOWN` (the recorded result kept only the head of exec's retained window); - this PR: `60000 padding padding padding`. The notice points at exec's own output file; no second file is written. - Review fixes, each with a test that is red on the earlier head: - a notice that gains a digit no longer pushes head+tail over the cap; - the separator newlines are reserved at the caller's weight (the context guard counts 2 per char), so a dense single-line result keeps its tail. - Workspace-only host sessions cannot open tmpdir paths with `read`; exec and web_fetch spill paths are in the same position on main. Letting `read` open this session's own spill paths is the follow-up PR. Co-authored-by: Ayaan Zaidi <[email protected]>
…ecovered (#206880) ## What Problem This Solves When a tool returns more text than its context budget, OpenClaw keeps only the start, discards the rest, and tells the model to "rerun with narrower args". The end of the output, where commands print errors, totals and exit status, is lost unless a keyword list happens to match it. MCP and plugin tools have no other copy, so the model cannot get the cut text back. Production signal (a production deployment, 2.76 days of Gateway logs): - 102 `[tool-result-truncation] Truncated N tool result(s) for prompt history (maxChars=64000 …)` lines, up to 96 results in one session. - 310 context-overflow diagnostics in the same window, driven by long sessions full of tool output. Reproduced on main (`ab296c23d2c4`) through the real record path (`guardSessionManager`, 200k-token window). The fixture is a 180,015-character plain log, the same for exec, read, web_fetch, an MCP result and a plugin result: | | main | this PR | |---|---|---| | Characters the model sees | 63,973 | 63,961 | | Last line kept | no | yes | | Full output recoverable | no (dropped) | yes, 180,015 chars in a file | | Notice | `[... 116116 more characters truncated; rerun with narrower args if needed]` | `[... 116221 chars omitted. Full output: <path> (page it with read offset/limit)]` | ## Fix The bound has one owner, at record time: - `boundToolResult` (`src/agents/embedded-agent-runner/tool-result-bound.ts`) runs once at the end of `afterToolCall` (`run/attempt-setup.ts`), after tool-result hooks and before the result is first sent. It applies to every executed tool: core, MCP and plugin. - It keeps the head and the tail and puts the notice at the cut. The full text goes to an owner-only file through the existing `writePrivateTempFile` (tmpdir, 0600; staged in the workspace for sandboxed sessions), the same rule as `exec` and `web_fetch` output files. A tool that already saved its own file (`exec`, `web_fetch`) keeps that file, and the notice names it. - The budget comes from the transcript guard's registry, so the existing synchronous cap leaves a bounded result byte for byte. Prefix stability holds, because a recorded result never changes after it is recorded. - If the file write fails, the result passes on unchanged, and the existing transcript cap bounds it with head, tail and the character count. Deleted: - `context-truncation-notice.ts`: the "rerun with narrower args" notice and its `sessions_send` variant. - `hasImportantTail`, a keyword regex that decided whether the tail survived, and the head-only branch it guarded. Head+tail is now unconditional for any window over 4,000 characters. Smaller windows keep a whole prefix, so short records are not split. Fixed on the way (exposed by the shorter notice): - The aggregate planner sized notices with a count of 1, so a notice carrying a real count could be cut. That dropped the spill path from a disclosed `Full output:` pointer. It now sizes the notice for the largest count it can show. - A multi-block result made only of short records keeps whole leading records with one notice (#201795) instead of cutting every record. ## User impact - Gateway and one-shot (`agent --local`) users on the embedded runtime. Both run `installEmbeddedAttemptContextGuards` (`src/agents/embedded-agent-runner/run/attempt-session-runtime-prepare.ts:515`). - A large tool result keeps its last lines (errors, totals, exit status), and the model can read the omitted middle from the named file instead of rerunning the tool. - No config change. Results within budget are unchanged. - Not covered: the Codex app-server and Claude CLI runtimes, which do not run this loop. ## Evidence - `src/agents/tool-result-bounds.test.ts`: one case per tool family through the real guard registry: - exec (`buildExecForegroundResult`), MCP (`projectMcpCallToolResult`) and plugin: head, tail, spill file with the full text, and the recorded bytes equal to the bound bytes (fixed point); - web_fetch reuses its own spill file; - read reaches the middle of a 180k one-line spill page by page, and no page needs a second spill; - a failed spill write falls back to head, tail and the count, with no path. - `run/attempt-context-guards.test.ts`: the installed `afterToolCall` bounds the result an earlier hook returned and restores the previous hook on removal. - Touched suites pass (8 files), and `agent-tools.prefix-stability` passes. All core test typecheck shards pass. - The prompt-cache prefix-stability journeys fail the same way on the merge base `ab296c23d2c4`, so these failures are not from this change: - They expect a summary marker ("compacted into the following summary") that #205584 replaced. - Google also counts 2 compactions in its overflow run where the test expects 1 (`expected 2 to be 1`, reproduced on the base). - The breaks they report are the allowed compactions; no tool-result bytes change after they are recorded. - On main, the same contract test fails 5 of 5 families (63,973 of 180,015 characters kept, tail lost, no file). - **End to end, real model.** Isolated `openclaw agent --local` (scratch HOME, state and config), `openai/gpt-5.4-mini` on the embedded runtime. The agent runs `seq 1 60000 | sed 's/$/ padding padding padding/'` through `exec`, calls no other tool, and replies with the last line of the output: - main `ab296c23d2c4`: `UNKNOWN` (the recorded result kept only the head of exec's retained window); - this PR: `60000 padding padding padding`. The notice points at exec's own output file; no second file is written. - Review fixes, each with a test that is red on the earlier head: - a notice that gains a digit no longer pushes head+tail over the cap; - the separator newlines are reserved at the caller's weight (the context guard counts 2 per char), so a dense single-line result keeps its tail. - Workspace-only host sessions cannot open tmpdir paths with `read`; exec and web_fetch spill paths are in the same position on main. Letting `read` open this session's own spill paths is the follow-up PR. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 5c3c123)
…ecovered (openclaw#206880) ## What Problem This Solves When a tool returns more text than its context budget, OpenClaw keeps only the start, discards the rest, and tells the model to "rerun with narrower args". The end of the output, where commands print errors, totals and exit status, is lost unless a keyword list happens to match it. MCP and plugin tools have no other copy, so the model cannot get the cut text back. Production signal (a production deployment, 2.76 days of Gateway logs): - 102 `[tool-result-truncation] Truncated N tool result(s) for prompt history (maxChars=64000 …)` lines, up to 96 results in one session. - 310 context-overflow diagnostics in the same window, driven by long sessions full of tool output. Reproduced on main (`856cbaecaad8`) through the real record path (`guardSessionManager`, 200k-token window). The fixture is a 180,015-character plain log, the same for exec, read, web_fetch, an MCP result and a plugin result: | | main | this PR | |---|---|---| | Characters the model sees | 63,973 | 63,961 | | Last line kept | no | yes | | Full output recoverable | no (dropped) | yes, 180,015 chars in a file | | Notice | `[... 116116 more characters truncated; rerun with narrower args if needed]` | `[... 116221 chars omitted. Full output: <path> (page it with read offset/limit)]` | ## Fix The bound has one owner, at record time: - `boundToolResult` (`src/agents/embedded-agent-runner/tool-result-bound.ts`) runs once at the end of `afterToolCall` (`run/attempt-setup.ts`), after tool-result hooks and before the result is first sent. It applies to every executed tool: core, MCP and plugin. - It keeps the head and the tail and puts the notice at the cut. The full text goes to an owner-only file through the existing `writePrivateTempFile` (tmpdir, 0600; staged in the workspace for sandboxed sessions), the same rule as `exec` and `web_fetch` output files. A tool that already saved its own file (`exec`, `web_fetch`) keeps that file, and the notice names it. - The budget comes from the transcript guard's registry, so the existing synchronous cap leaves a bounded result byte for byte. Prefix stability holds, because a recorded result never changes after it is recorded. - If the file write fails, the result passes on unchanged, and the existing transcript cap bounds it with head, tail and the character count. Deleted: - `context-truncation-notice.ts`: the "rerun with narrower args" notice and its `sessions_send` variant. - `hasImportantTail`, a keyword regex that decided whether the tail survived, and the head-only branch it guarded. Head+tail is now unconditional for any window over 4,000 characters. Smaller windows keep a whole prefix, so short records are not split. Fixed on the way (exposed by the shorter notice): - The aggregate planner sized notices with a count of 1, so a notice carrying a real count could be cut. That dropped the spill path from a disclosed `Full output:` pointer. It now sizes the notice for the largest count it can show. - A multi-block result made only of short records keeps whole leading records with one notice (openclaw#201795) instead of cutting every record. ## User impact - Gateway and one-shot (`agent --local`) users on the embedded runtime. Both run `installEmbeddedAttemptContextGuards` (`src/agents/embedded-agent-runner/run/attempt-session-runtime-prepare.ts:515`). - A large tool result keeps its last lines (errors, totals, exit status), and the model can read the omitted middle from the named file instead of rerunning the tool. - No config change. Results within budget are unchanged. - Not covered: the Codex app-server and Claude CLI runtimes, which do not run this loop. ## Evidence - `src/agents/tool-result-bounds.test.ts`: one case per tool family through the real guard registry: - exec (`buildExecForegroundResult`), MCP (`projectMcpCallToolResult`) and plugin: head, tail, spill file with the full text, and the recorded bytes equal to the bound bytes (fixed point); - web_fetch reuses its own spill file; - read reaches the middle of a 180k one-line spill page by page, and no page needs a second spill; - a failed spill write falls back to head, tail and the count, with no path. - `run/attempt-context-guards.test.ts`: the installed `afterToolCall` bounds the result an earlier hook returned and restores the previous hook on removal. - Touched suites pass (8 files), and `agent-tools.prefix-stability` passes. All core test typecheck shards pass. - The prompt-cache prefix-stability journeys fail the same way on the merge base `856cbaecaad8`, so these failures are not from this change: - They expect a summary marker ("compacted into the following summary") that openclaw#205584 replaced. - Google also counts 2 compactions in its overflow run where the test expects 1 (`expected 2 to be 1`, reproduced on the base). - The breaks they report are the allowed compactions; no tool-result bytes change after they are recorded. - On main, the same contract test fails 5 of 5 families (63,973 of 180,015 characters kept, tail lost, no file). - **End to end, real model.** Isolated `openclaw agent --local` (scratch HOME, state and config), `openai/gpt-5.4-mini` on the embedded runtime. The agent runs `seq 1 60000 | sed 's/$/ padding padding padding/'` through `exec`, calls no other tool, and replies with the last line of the output: - main `856cbaecaad8`: `UNKNOWN` (the recorded result kept only the head of exec's retained window); - this PR: `60000 padding padding padding`. The notice points at exec's own output file; no second file is written. - Review fixes, each with a test that is red on the earlier head: - a notice that gains a digit no longer pushes head+tail over the cap; - the separator newlines are reserved at the caller's weight (the context guard counts 2 per char), so a dense single-line result keeps its tail. - Workspace-only host sessions cannot open tmpdir paths with `read`; exec and web_fetch spill paths are in the same position on main. Letting `read` open this session's own spill paths is the follow-up PR. Co-authored-by: Ayaan Zaidi <[email protected]>
What Problem This Solves
When a tool call is rejected, a compact schema hint can hide the field names the agent needs. When a large result has many text blocks, the runtime can cut every truncation warning off before it says what happened or how to recover.
User Impact
Agents get the exact schema-inspection step after a validation failure. Oversized multi-block results keep whole leading records plus one readable recovery notice, without re-expanding previously sent results or dropping images that fit.
Why This Change Was Made
Reuse
tool_describein structured Tool Search and handledescribe()in Code Mode rather than expanding the full schema in every error. When per-block notices do not fit, keep whole leading records and place one notice at the first omitted text position. This lets replay restore the exact text prefix alongside canonical non-text blocks, and lets the context guard retain a fitting image when text is intentionally omitted. Ordinary single-block truncation is unchanged.Reported and co-authored by @jacobtomlinson.
Work sessions
Linked for team reference; these sessions aren’t public.
Evidence
Built CLI and latest-stable upgrade proof
The real
openclaw agent --local --jsonCLI, not an in-process agent harness, ran in separate OS processes in an isolated profile with a loopback-only HTTP Responses fixture. The fixture scripts model decisions and records actual provider-bound requests; no external inference, live Gateway, or operator session data was used. The latest stable npm tarball[email protected](e13cf15) matched npm's published SHA-512 integrity. The candidate runtime was built at6e27254b9d9c09a9171d88b0a26d889b9c811604; a full-treegit diff --exit-codeagainst the reviewed PR head was empty.dispatch_actionrejected the unknown nested key and returned the exacttool_describeID; inspection exposeddisplayPreference24andfilters.pageSize; the corrected call executed and the turn completed.input_image. The turn completed.doctor --fix --non-interactiveon that same state, migrating the agent DB from schema 38 to 43. The saved projection digest was identical in the archived stable DB and upgraded DB. Two separately launched candidate CLI invocations reopened the same session at 131,072, saw the prior reply, and sent the same shortened tool output as stable at the tighter cap.Redacted captures and fingerprints
The 32,768-token mixed-result CLI attempt stopped at the separate total-prompt precheck; the positive mixed-image CLI proof above used 65,536. A first candidate reopen before Doctor correctly refused the older DB schema; the upgrade proof used the supported Doctor path, not a manual migration. The proof covers existing stable-created state, a forward migration, and cold replay; it does not claim downgrade or updater-package installation coverage.
Original reproducer
Reran the same two synthetic tools in a 32,768-token mock agent loop, capturing the result handed to the next model call. Baseline:
8a23d7c11d37183b00f5bd0ba4c942bbfe03e116.Expected inputelides fields; no inspection steptool_describeand the exact tool ID; inspection returns the missing keys and the corrected retry succeedsBefore and after: captured model-visible excerpts
ClawSweeper follow-up
Both findings from the initial review reproduced on the previous PR head
4dac7d4ea0c0using 1,000 text records with an interleaved image. The re-review confirmed both code fixes and requested additional CLI and upgrade evidence; those captures are above.The original probe output was unchanged after the follow-up. 130 distinct focused tests passed, covering truncation, checkpoint replay, the context guard, schema recovery, and prefix stability. Scoped typed lint, core and agents-tools type checks, formatting, and
git diff --checkpassed. Local autoreview found no actionable P0–P2 findings in the follow-up diff. Hosted CI is separate; this evidence update does not change CI or the PR head.Worked on by