Skip to content

fix: preserve tool-result text across media fallback and context pressure - #99756

Merged
steipete merged 17 commits into
openclaw:mainfrom
AcosX:fix/issue-99241-text-tool-results-mainbase
Jul 11, 2026
Merged

steipete merged 17 commits into
openclaw:mainfrom
AcosX:fix/issue-99241-text-tool-results-mainbase

Conversation

@AcosX

@AcosX AcosX commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Refs #99241

What Problem This Solves

This PR covers the two interacting model-visible failure stages reported in #99241:

  1. Provider serializers could turn an empty text-only tool result into (see attached image), masking upstream text loss as a media result.
  2. Under aggregate tool-result pressure, recovery could reduce or clear a freshly completed result before provider dispatch whenever runtime context, steering, or a follow-up message made that result non-trailing.

The July 9 report on this PR was correct for the earlier head: the provider/media patch removed the placeholder symptom but did not stop prompt projection from emptying fresh text. This updated head fixes both stages.

Why This Change Was Made

The earlier beta fixes were important but incomplete:

This update adopts the general fresh-projection invariant proposed in #99524, with credit to @snowzlmbot, and carries forward this PR's runtime-context regression coverage on current main.

In aggregate recovery, an unfrozen projection that appears after a frozen baseline is treated as fresh. Recovery now reduces ordinary eligible history first, then frozen history, and only then fresh non-trailing results. The existing trailing batch remains fully protected. If historical reductions cannot satisfy the hard cap, fresh results remain eligible as the final fallback, so the cap is still enforced.

For provider serialization, a new internal media-only fallback helper prevents empty text-only results from becoming image placeholders. The exported describeToolResultMediaPlaceholder behavior remains identical to current main, preserving its public Plugin SDK contract for mixed text/media callers.

User Impact

Long-lived sessions with heavy exec, read, browser, or other tool traffic should keep the newest tool output model-readable across runtime-context, steering, and follow-up boundaries while older projected history is reduced first.

Media-only results retain a descriptive placeholder. Mixed text/media results preserve explicit text. No config change, migration, or new runtime option is required.

Evidence

Reproduction and focused coverage

  • The live dist instrumentation in Tool outputs sometimes render as image attachments and become unreadable to the agent #99241 showed a 234-character fresh result selected as the only eligible entry, reduced to 69 characters, then cleared while 256,138 frozen characters remained.
  • New regressions cover 234-character and 4,000-character sentinels followed by runtime-context, steering, and queued follow-up messages.
  • A hard-cap regression proves fresh text is eventually reduced when frozen history alone cannot satisfy the budget.
  • The existing real convertToLlm boundary regression still proves runtime-context carrier metadata survives provider conversion.

Automated validation

  • Focused provider, transport, serializer, and aggregate-truncation suite:
    • 10 files across 3 Vitest shards
    • 792 tests passed after the final merge from upstream/main
  • node scripts/run-oxlint.mjs <15 changed files>: passed.
  • oxfmt --check <15 changed files>: passed.
  • git diff --check upstream/main...HEAD: passed.
  • Clean-checkout build:
    • OPENCLAW_BUILD_ALL_NO_PNPM=1 OPENCLAW_RUN_NODE_SKIP_DTS_BUILD=1 node scripts/build-all.mjs gatewayWatch
    • passed.

Real Gateway / model proof

A clean checkout containing only current OpenClaw plus this PR patch was run through a real local Gateway with deepseek-v4-flash and a 128K context window:

  • 40 consecutive real exec tool calls in one session.
  • 272,000 total characters of tool-result text.
  • 40/40 unique sentinels were read and acknowledged by the model, from H01 through H40.
  • 14 compaction entries occurred; prompt usage crossed pressure and compacted during the run without losing the fresh sentinel.

This is direct Gateway/provider long-session proof. Browser UI proof is not included in this update.

Codex autoreview

Repository autoreview was run repeatedly with Codex (gpt-5.5, high reasoning):

  • One accepted finding identified a mixed text/media regression risk; the implementation was changed to use a non-exported media-only serializer helper.
  • A later public-helper finding was rejected after direct comparison with upstream/main: the exported helper is unchanged, and the internal helper is not exported through the Plugin SDK.
  • No accepted or actionable findings remain.

AI-assisted: reproduction analysis, implementation, validation, and final review were performed with Codex.

@clawsweeper

clawsweeper Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed July 11, 2026, 2:38 PM ET / 18:38 UTC.

Summary
The branch reorders aggregate recovery to preserve fresh non-trailing tool results and changes provider serializers to suppress generic media placeholders when explicit text blocks are present.

PR surface: Source +79, Tests +462. Total +541 across 17 files.

Reproducibility: yes. The reported text-loss stages and the introduced empty-text-plus-audio regression both have deterministic source paths; the latter occurs when an empty text block and audio block pass through a shared provider serializer.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: packages/ai/src/providers/openai-responses-shared.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #99241
Summary: This PR is the comprehensive candidate fix for the canonical issue; earlier merged changes cover narrower stages, and the closed alternative recovery PR was incorporated here.

Members:

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

Merge readiness
Overall: 🦐 gold shrimp
Proof: 🦞 diamond lobster
Patch quality: 🦐 gold shrimp
Result: needs maintainer review before merge.

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

Rank-up moves:

  • Preserve omitted audio and other unsupported-media fallbacks for explicit empty text.
  • [P2] Add focused shared-provider and Anthropic transport regression coverage.

Risk before merge

  • [P2] Merging this head can turn explicit empty text plus audio into empty or (no output) model-visible results across multiple provider paths, replacing the established (see attached audio) fallback.
  • [P1] The intentional aggregate-recovery ordering can reduce more old frozen tool-result history to preserve the current result under pressure; the hard-cap regression bounds this tradeoff.

Maintainer options:

  1. Repair omitted-media handling (recommended)
    Preserve audio and other unsupported-media fallbacks when explicit text is empty, then rerun focused provider coverage.
  2. Split recovery from serializers
    Land the aggregate recovery separately only if owners prefer to review the provider fallback repair as an independent patch.
  3. Accept output loss
    Merge unchanged only with an explicit decision that empty text may hide unsupported audio or other media from the model.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Preserve model-readable audio and other omitted-media fallbacks when tool results include explicit empty text; keep image suppression delivery-aware, retain the exported helper contract, and add focused sibling-provider regressions.

Next step before merge

  • [P2] The blocker is a narrow mechanical fallback repair that can preserve the rest of the contributor's strongly proven branch.

Security
Cleared: No security or supply-chain concern was found; the diff does not change dependencies, workflows, permissions, secrets, downloads, or publishing.

Review findings

  • [P1] Preserve fallbacks for omitted audio media — packages/ai/src/providers/tool-result-text.ts:166-174
Review details

Best possible solution:

Keep the fresh-result recovery and image fixes, but make fallback suppression depend on actual media delivery so empty text never hides unsupported audio or other omitted media.

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

Yes. The reported text-loss stages and the introduced empty-text-plus-audio regression both have deterministic source paths; the latter occurs when an empty text block and audio block pass through a shared provider serializer.

Is this the best way to solve the issue?

No in the current head. The aggregate recovery design is bounded and appropriate, but the shared fallback abstraction is broader than the provider delivery contract and needs a narrow repair.

Full review comments:

  • [P1] Preserve fallbacks for omitted audio media — packages/ai/src/providers/tool-result-text.ts:166-174
    describeMediaOnlyToolResultPlaceholder returns undefined for any text block, including text: "". These shared provider callers do not deliver audio blocks, so empty text plus audio becomes empty or (no output) instead of the existing (see attached audio) contract. This was equally visible on the prior reviewed head and is being raised late after the sibling audio-contract audit; make suppression delivery-aware and add empty-text-plus-audio coverage.
    Confidence: 0.99
    Late finding: first raised on code an earlier review cycle already covered.

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): The PR provides direct after-fix Gateway/model evidence with 40 sequential exec sentinels surviving 14 compactions, convincingly proving the primary long-session behavior; it does not cover the separate omitted-audio regression.
  • remove rating: 🦞 diamond lobster: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.
  • remove status: 👀 ready for maintainer look: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P1: The PR addresses a broken agent workflow, but the introduced sibling regression can still hide model-visible tool output.
  • merge-risk: 🚨 compatibility: The patch changes the established type-aware media fallback contract across provider serializers.
  • merge-risk: 🚨 session-state: The patch changes aggregate recovery priority for long-session prompt projections.
  • merge-risk: 🚨 message-delivery: The new helper can suppress the only model-readable description for omitted audio or other media.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): The PR provides direct after-fix Gateway/model evidence with 40 sequential exec sentinels surviving 14 compactions, convincingly proving the primary long-session behavior; it does not cover the separate omitted-audio regression.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR provides direct after-fix Gateway/model evidence with 40 sequential exec sentinels surviving 14 compactions, convincingly proving the primary long-session behavior; it does not cover the separate omitted-audio regression.
Evidence reviewed

PR surface:

Source +79, Tests +462. Total +541 across 17 files.

View PR surface stats
Area Files Added Removed Net
Source 10 119 40 +79
Tests 7 466 4 +462
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 17 585 44 +541

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs packages/ai/src/providers/tool-result-text.test.ts packages/ai/src/providers/openai-responses-shared.test.ts packages/ai/src/providers/openai-completions.test.ts packages/ai/src/providers/anthropic.test.ts packages/ai/src/providers/mistral.test.ts packages/ai/src/providers/google-shared.convert.test.ts src/agents/anthropic-transport-stream.test.ts.
  • [P1] git diff --check.

What I checked:

Likely related people:

  • snowzlmbot: Authored the merged provider replay work that established shared structured-text extraction and type-aware media placeholders across the affected providers. (role: introduced provider replay behavior; confidence: high; commits: b63e06f68aa0; files: packages/ai/src/providers/tool-result-text.ts, packages/ai/src/providers/openai-responses-shared.ts, extensions/google/transport-stream.ts)
  • obviyus: Merged the provider replay work and authored recent aggregate-budget and compaction changes in the same truncation path. (role: merger and recent adjacent owner; confidence: high; commits: b63e06f68aa0, 4682364fa6f4; files: packages/ai/src/providers/tool-result-text.ts, src/agents/embedded-agent-runner/tool-result-truncation.ts)
  • momothemage: Authored the merged trailing fresh-result aggregate recovery behavior that this PR generalizes to non-trailing results. (role: introduced fresh-result protection; confidence: high; commits: d45b8be939a1; 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.
Review history (8 earlier review cycles)
  • reviewed 2026-07-04T07:17:29.735Z sha 6522759 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-05T09:06:56.842Z sha 81a791b :: needs maintainer review before merge. :: none
  • reviewed 2026-07-05T15:36:52.591Z sha 81a791b :: needs maintainer review before merge. :: none
  • reviewed 2026-07-10T08:39:52.868Z sha 346d309 :: needs real behavior proof before merge. :: [P1] Preserve the exported media-description contract
  • reviewed 2026-07-10T21:10:30.670Z sha 8e1c39d :: needs maintainer review before merge. :: none
  • reviewed 2026-07-10T21:20:22.434Z sha 8e1c39d :: needs changes before merge. :: [P1] Preserve a fallback when Google omits the image
  • reviewed 2026-07-10T21:28:31.402Z sha 8e1c39d :: needs changes before merge. :: [P1] Preserve a fallback when Google omits the image
  • reviewed 2026-07-10T23:35:08.697Z sha ff4e543 :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Jul 4, 2026
@AcosX
AcosX force-pushed the fix/issue-99241-text-tool-results-mainbase branch from 31dc153 to 6522759 Compare July 4, 2026 07:02
@AcosX

AcosX commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Addressed the two Anthropic parity findings:

  • src/llm/providers/anthropic.ts: explicit text blocks, including empty/truncated text, no longer fall through to the hard-coded (see attached image) fallback in the image branch.
  • src/agents/anthropic-transport-stream.ts: applied the same suppression for the transport path.
  • Added focused provider and transport regression coverage for empty text plus image tool results.

Validation:

  • node scripts/run-vitest.mjs src/llm/providers/anthropic.test.ts src/agents/anthropic-transport-stream.test.ts src/llm/providers/tool-result-text.test.ts — 3 files / 132 tests passed
  • node scripts/run-vitest.mjs src/llm/providers/openai-responses-shared.test.ts src/llm/providers/openai-completions.test.ts src/llm/providers/google-shared.convert.test.ts extensions/google/transport-stream.test.ts src/llm/providers/mistral.test.ts — 5 files / 165 tests passed
  • pnpm check:test-types — passed

@clawsweeper

clawsweeper Bot commented Jul 4, 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 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: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jul 4, 2026
@steipete
steipete merged commit abd6cbb into openclaw:main Jul 11, 2026
108 of 114 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

steipete added a commit that referenced this pull request Jul 11, 2026
…sure (#99756)

* fix: keep text tool results ahead of stale media placeholders

* fix(agents): preserve fresh results before runtime context

* fix(agents): defer fresh results during aggregate recovery

* fix(google): preserve omitted media fallback

* fix(openai): preserve omitted media fallback

* fix(agents): narrow tool-result recovery scope

* fix(agents): keep one fresh-result recovery path

* chore: keep sweep changelog neutral

---------

Co-authored-by: AcosX <[email protected]>
Co-authored-by: Acos_X <[email protected]>
Co-authored-by: Colin Johnson <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit abd6cbb)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 12, 2026
…sure (openclaw#99756)

* fix: keep text tool results ahead of stale media placeholders

* fix(agents): preserve fresh results before runtime context

* fix(agents): defer fresh results during aggregate recovery

* fix(google): preserve omitted media fallback

* fix(openai): preserve omitted media fallback

* fix(agents): narrow tool-result recovery scope

* fix(agents): keep one fresh-result recovery path

* chore: keep sweep changelog neutral

---------

Co-authored-by: AcosX <[email protected]>
Co-authored-by: Acos_X <[email protected]>
Co-authored-by: Colin Johnson <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
@AcosX
AcosX deleted the fix/issue-99241-text-tool-results-mainbase branch July 22, 2026 09:11
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…sure (openclaw#99756)

* fix: keep text tool results ahead of stale media placeholders

* fix(agents): preserve fresh results before runtime context

* fix(agents): defer fresh results during aggregate recovery

* fix(google): preserve omitted media fallback

* fix(openai): preserve omitted media fallback

* fix(agents): narrow tool-result recovery scope

* fix(agents): keep one fresh-result recovery path

* chore: keep sweep changelog neutral

---------

Co-authored-by: AcosX <[email protected]>
Co-authored-by: Acos_X <[email protected]>
Co-authored-by: Colin Johnson <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit abd6cbb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. 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: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants