Skip to content

fix(agents): preserve fresh tool result text under aggregate cap - #98955

Merged
clawsweeper[bot] merged 1 commit into
mainfrom
feature/issue-98874-tool-result-text
Jul 2, 2026
Merged

clawsweeper[bot] merged 1 commit into
mainfrom
feature/issue-98874-tool-result-text

Conversation

@momothemage

@momothemage momothemage commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #98874
Related: #96857

What Problem This Solves

Fixes an issue where users in long-lived WebChat, Discord, or other direct agent sessions could see fresh text tool outputs such as exec, read, browser, or session_status degrade into (see attached image) or otherwise disappear from the model-visible prompt when aggregate tool-result history was already saturated.

Why This Change Was Made

Prompt-history tool-result truncation now protects the trailing fresh tool-result batch first, then recovers aggregate budget from older projected history before falling back to trimming the fresh batch only when that batch itself exceeds the aggregate cap. Aggregate elision now uses a budget-bounded marker so the recovery plan cannot claim success while leaving counted tool-result text above the configured budget.

This stays scoped to the live prompt projection path; persisted session truncation and preemptive recovery estimates keep their existing aggregate-budget behavior.

User Impact

Agents can inspect small fresh command/file/status outputs even after old session history has filled the aggregate tool-result budget. Oversized or multi-tool fresh batches remain bounded, so context-overflow protection still applies without turning ordinary new text into an image-only placeholder.

Evidence

  • Reproduced the root class locally with a focused regression test: a saturated history projection followed by fresh ABC output previously risked clearing the fresh result; the new test preserves ABC while keeping total tool-result text within the 32k aggregate cap.
  • Added coverage for oversized fresh trailing results, multi-tool fresh batches over the aggregate cap, and tiny explicit aggregate budgets where elision markers themselves must fit the budget.
  • PR-head saturated prompt-history proof, run against branch feature/issue-98874-tool-result-text at 50069fdd6f. The redacted tsx terminal proof imports createToolResultPromptProjectionState, truncateOversizedToolResultsInMessages, and getToolResultTextLength, fills the aggregate history with 50 prior exec tool results, freezes that projection, then appends a fresh exec result containing ABC:
$ corepack pnpm exec tsx <<'TS'
# proof script builds saturated history, then appends fresh exec output "ABC"
TS
branch=feature/issue-98874-tool-result-text
commit=50069fdd6f
firstPass.truncatedCount=43
secondPass.truncatedCount=1
secondPass.freshToolCallId=fresh_exec
secondPass.freshText=ABC
secondPass.totalToolResultChars=32000
secondPass.aggregateBudgetChars=32000
freshPreserved=true
aggregateCapPreserved=true
imagePlaceholderPresent=false
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/tool-result-truncation.test.ts --maxWorkers=1 --reporter=dot passed: 1 file / 42 tests.
  • pnpm exec oxlint src/agents/embedded-agent-runner/tool-result-truncation.ts src/agents/embedded-agent-runner/tool-result-truncation.test.ts passed.
  • git diff --check passed.
  • .agents/skills/autoreview/scripts/autoreview --mode local passed with no accepted/actionable findings after the review-triggered fixes.

AI-assisted: yes, implemented and reviewed with Codex.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Jul 2, 2026
@clawsweeper

clawsweeper Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: passed. Reviewed July 2, 2026, 4:34 AM ET / 08:34 UTC.

Summary
The branch updates agent tool-result truncation so live prompt projection protects trailing fresh tool-result messages under the aggregate cap, adds a bounded aggregate elision marker, and extends focused truncation tests.

PR surface: Source +54, Tests +124. Total +178 across 2 files.

Reproducibility: yes. source-level: the linked issues give a deterministic saturated-history prompt projection path, and current main shows aggregate recovery can clear tool-result text to empty. I did not run a live WebChat or Discord session in this read-only review.

Review metrics: 1 noteworthy metric.

  • Prompt recovery policy: 1 changed. The diff changes which tool-result text remains model-visible when live prompt-history aggregate recovery is already saturated.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #98874
Summary: This PR is the candidate fix for the concrete fresh text tool-result placeholder issue and partially overlaps the older broader placeholder-degradation thread.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Risk before merge

  • [P1] Merging intentionally lets older frozen prompt projections be further elided so fresh tool output stays model-visible under the aggregate cap; maintainers should accept that old model-visible tool text can shrink more aggressively in saturated sessions.
  • [P1] The after-fix proof exercises the prompt-projection helper directly rather than a full WebChat, Discord, or Telegram saturated session, so transport-level confidence still comes from the runtime wiring and linked live reports.

Maintainer options:

  1. Accept Fresh-Result Priority (recommended)
    Maintainers can accept the session-state tradeoff because the patch preserves current tool output while still enforcing the aggregate cap by spending recovery on older prompt history first.
  2. Request Transport-Level Proof
    Maintainers can pause merge until a redacted WebChat, Discord, or Telegram saturated-session run shows a fresh text tool result staying model-visible.
  3. Hold The Tradeoff
    Maintainers can hold or close the PR if preserving older prompt-projection byte stability is more important than recovering fresh tool output under saturation.

Next step before merge

  • [P2] The PR has no concrete code repair finding, but the protected maintainer label and session-state tradeoff require maintainer acceptance before merge.

Security
Cleared: The diff only changes agent prompt-history truncation code and colocated tests; no dependency, workflow, credential, permission, package, or code execution surface is changed.

Review details

Best possible solution:

Land this narrow prompt-projection fix after maintainers accept the fresh-result priority tradeoff and required merge gates remain green.

Do we have a high-confidence way to reproduce the issue?

Yes, source-level: the linked issues give a deterministic saturated-history prompt projection path, and current main shows aggregate recovery can clear tool-result text to empty. I did not run a live WebChat or Discord session in this read-only review.

Is this the best way to solve the issue?

Yes, this is the best focused fix I found: it repairs the live prompt-projection helper that wipes fresh tool text, rather than only masking provider placeholder fallbacks or changing persisted session truncation.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against f2abf57d32ad.

Label changes

Label changes:

  • add status: 🚀 automerge armed: This PR is in ClawSweeper's automerge lane. Sufficient (terminal): The PR body includes PR-head terminal output from a saturated prompt-history proof showing fresh ABC text preserved while the aggregate cap remains at 32k characters.
  • remove status: 👀 ready for maintainer look: Current PR status label is status: 🚀 automerge armed.

Label justifications:

  • P1: The PR targets a current regression where fresh text tool results can disappear from active agent sessions and break real task execution.
  • merge-risk: 🚨 session-state: The diff changes live prompt projection state and aggregate elision behavior, affecting which historical and fresh tool-result text the model sees on later turns.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 🚀 automerge armed: This PR is in ClawSweeper's automerge lane. Sufficient (terminal): The PR body includes PR-head terminal output from a saturated prompt-history proof showing fresh ABC text preserved while the aggregate cap remains at 32k characters.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes PR-head terminal output from a saturated prompt-history proof showing fresh ABC text preserved while the aggregate cap remains at 32k characters.
Evidence reviewed

PR surface:

Source +54, Tests +124. Total +178 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 71 17 +54
Tests 1 130 6 +124
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 201 23 +178

What I checked:

  • Repository policy read: Root AGENTS.md and scoped src/agents/AGENTS.md were read in full; the root policy treats session/context behavior as merge-sensitive and requires whole-path review, while the scoped guide affects agent test/runtime import expectations. (AGENTS.md:14, f2abf57d32ad)
  • Maintainer notes check: No .agents/maintainer-notes files were present for this review, so there were no matching internal maintainer decisions to apply.
  • Live PR context: Live GitHub data shows this PR is open, cleanly mergeable at head 50069fd, has the protected maintainer label, is opted into clawsweeper:automerge, and closes Tool text results sometimes render as image attachments #98874. (50069fdd6f7f)
  • Current main failure path: Current main builds aggregate replacements from aggregate-eligible tool results and, when remaining reduction is still needed, clears text blocks to an empty string; this matches the deterministic root cause described in the related issue comments. (src/agents/embedded-agent-runner/tool-result-truncation.ts:675, f2abf57d32ad)
  • Runtime caller: The embedded attempt runner applies truncateOversizedToolResultsInMessages with one ToolResultPromptProjectionState at both the prompt-history and provider stream transform boundaries, so this helper is on the model-visible live prompt path. (src/agents/embedded-agent-runner/run/attempt.ts:4276, f2abf57d32ad)
  • Provider fallback boundary: OpenAI-compatible and Responses conversion use extracted tool-result text with no-output or media placeholders when text is empty, so preserving non-empty prompt text in the truncation helper addresses the reported placeholder/empty-content symptom at the right layer. (src/llm/providers/openai-completions.ts:1162, f2abf57d32ad)

Likely related people:

  • vincentkoc: Current blame on the live prompt truncation helper and attempt runner points to Vincent Koc, and recent commits added cache-stable live tool-result prompts, memoized prompt projections, and aggregate prompt reduction in this area. (role: recent projection and truncation owner; confidence: high; commits: 38442853e104, a60947fb3e92, 30925601aef7; files: src/agents/embedded-agent-runner/tool-result-truncation.ts, src/agents/embedded-agent-runner/tool-result-truncation.test.ts, src/agents/embedded-agent-runner/run/attempt.ts)
  • ooiuuii: Commit f49a3e4 is titled "fix: bound aggregate prompt tool results" and introduced the aggregate budget boundary this PR changes. (role: introduced aggregate prompt boundary; confidence: high; commits: f49a3e4c266c; files: src/agents/embedded-agent-runner/tool-result-truncation.ts)
  • AdrianIp0204: Merged PR Handle Codex toolResult blocks in tool-result truncation #87912 added adjacent Codex toolResult block handling in the same truncation module and tests. (role: adjacent truncation contributor; confidence: medium; commits: dbd4c98b025c; files: src/agents/embedded-agent-runner/tool-result-truncation.ts, src/agents/embedded-agent-runner/tool-result-truncation.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 2, 2026
@momothemage

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 2, 2026
@momothemage

Copy link
Copy Markdown
Contributor Author

My maintainer take on the remaining decision point: I would accept the fresh-result priority tradeoff and merge this after normal review.

In saturated sessions, the newest tool result is the output the model most needs for the next step. Older tool-result history has already exceeded the aggregate budget, so preserving cache/projection stability for old text is less valuable than keeping a fresh exec, read, browser, or session_status result model-visible. This PR still enforces the aggregate cap; it just spends the recovery budget on older projected history before trimming the fresh trailing batch.

I also think the current proof is sufficient for this specific root cause. The PR-head saturated prompt-history proof exercises the live projection helper that sits on the provider prompt boundary and shows fresh ABC preserved while the 32k aggregate cap remains intact. A full WebChat/Discord/Telegram saturated-session recording would be nice extra confidence, but I would not make it a merge blocker unless a maintainer specifically wants transport-level evidence.

So my recommendation is option 1 from ClawSweeper: accept the fresh-result priority. The remaining risk is understood and documented: old model-visible tool output may shrink more aggressively under aggregate pressure, but that is the right behavior for recovering useful current tool output.

@momothemage

Copy link
Copy Markdown
Contributor Author

@clawsweeper approve

@clawsweeper

clawsweeper Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@momothemage

Copy link
Copy Markdown
Contributor Author

@clawsweeper automerge

@clawsweeper

clawsweeper Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

🦞✅
ClawSweeper merged this PR after the passing review.

Source: clawsweeper[bot]
Feedback: structured ClawSweeper verdict: pass (sha=50069fdd6f7fe394611bd05c3ef2e428c29a17d0)
Merge status: merged by ClawSweeper automerge
Merged at: 2026-07-02T08:35:21Z
Merge commit: d45b8be939a1

What merged:

  • The branch updates agent tool-result truncation so live prompt projection protects trailing fresh tool-resul ... ges under the aggregate cap, adds a bounded aggregate elision marker, and extends focused truncation tests.
  • PR surface: Source +54, Tests +124. Total +178 across 2 files.
  • Reproducibility: yes. source-level: the linked issues give a deterministic saturated-history prompt projecti ... clear tool-result text to empty. I did not run a live WebChat or Discord session in this read-only review.

Automerge notes:

  • No ClawSweeper repair was needed after automerge opt-in.

The automerge loop is complete.

Automerge progress:

  • 2026-07-02 07:56:25 UTC review queued 50069fdd6f7f (queued)
  • 2026-07-02 08:35:09 UTC review passed 50069fdd6f7f (structured ClawSweeper verdict: pass (sha=50069fdd6f7fe394611bd05c3ef2e428c29a1...)
  • 2026-07-02 08:35:24 UTC merged 50069fdd6f7f (merged by ClawSweeper automerge)

@clawsweeper clawsweeper Bot added clawsweeper:automerge Maintainer opted this PR into bounded ClawSweeper-reviewed automerge status: 🚀 automerge armed This PR is in ClawSweeper's automerge lane. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jul 2, 2026
@clawsweeper
clawsweeper Bot merged commit d45b8be into main Jul 2, 2026
196 of 213 checks passed
@clawsweeper
clawsweeper Bot deleted the feature/issue-98874-tool-result-text branch July 2, 2026 08:35
zeekay pushed a commit to hanzoai/bot that referenced this pull request Sep 11, 2026
…955)

Summary:
- The branch updates agent tool-result truncation so live prompt projection protects trailing fresh tool-resul ... ges under the aggregate cap, adds a bounded aggregate elision marker, and extends focused truncation tests.
- PR surface: Source +54, Tests +124. Total +178 across 2 files.
- Reproducibility: yes. source-level: the linked issues give a deterministic saturated-history prompt projecti ...  clear tool-result text to empty. I did not run a live WebChat or Discord session in this read-only review.

Automerge notes:
- No ClawSweeper repair was needed after automerge opt-in.

Validation:
- ClawSweeper review passed for head 50069fdd6f7fe394611bd05c3ef2e428c29a17d0.
- Required merge gates passed before the squash merge.

Prepared head SHA: 50069fdd6f7fe394611bd05c3ef2e428c29a17d0
Review: openclaw/openclaw#98955 (comment)

Co-authored-by: momothemage <[email protected]>
Approved-by: momothemage
zeekay pushed a commit to hanzoai/bot that referenced this pull request Sep 11, 2026
…955)

Summary:
- The branch updates agent tool-result truncation so live prompt projection protects trailing fresh tool-resul ... ges under the aggregate cap, adds a bounded aggregate elision marker, and extends focused truncation tests.
- PR surface: Source +54, Tests +124. Total +178 across 2 files.
- Reproducibility: yes. source-level: the linked issues give a deterministic saturated-history prompt projecti ...  clear tool-result text to empty. I did not run a live WebChat or Discord session in this read-only review.

Automerge notes:
- No ClawSweeper repair was needed after automerge opt-in.

Validation:
- ClawSweeper review passed for head 50069fdd6f7fe394611bd05c3ef2e428c29a17d0.
- Required merge gates passed before the squash merge.

Prepared head SHA: 50069fdd6f7fe394611bd05c3ef2e428c29a17d0
Review: openclaw/openclaw#98955 (comment)

Co-authored-by: momothemage <[email protected]>
Approved-by: momothemage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper:automerge Maintainer opted this PR into bounded ClawSweeper-reviewed automerge maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 🚀 automerge armed This PR is in ClawSweeper's automerge lane.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool text results sometimes render as image attachments

1 participant