Skip to content

fix(agents): preserve fresh tool results during aggregate recovery - #99524

Closed
snowzlmbot wants to merge 1 commit into
openclaw:mainfrom
snowzlmbot:fix/99481-tool-result-empty
Closed

snowzlmbot wants to merge 1 commit into
openclaw:mainfrom
snowzlmbot:fix/99481-tool-result-empty

Conversation

@snowzlmbot

@snowzlmbot snowzlmbot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #99481
Related: #96857, #98955

Summary

Preserve the current fresh tool-result text during prompt-history aggregate recovery when the fresh result is followed by a runtime/context message before provider dispatch.

Breaking changes

None.

What Problem This Solves

#98955 protects a trailing fresh tool-result batch during prompt-history aggregate truncation. #99481 showed the empty tool-result symptom could still happen on 2026.7.1-beta.1, where that trailing protection was already present.

The remaining gap is non-trailing fresh tool results: runtime/context messages can be inserted after a fresh result before the provider call. The existing trailing-only scan then does not mark that fresh result as protected, so a saturated long-lived session can spend aggregate recovery on the current result and leave the model blind to the latest tool output.

Implementation

  • Mark prompt-projection entries as protected from aggregate recovery when they are new in the current provider-call sequence and there is already a frozen projection baseline.
  • Keep the first provider call free to trim historical saturated tool output by only applying the new projection protection after projection state has frozen at least one prior call.
  • Keep the existing trailing-batch protection intact as a separate protection source.
  • Use older unprotected projected history first, then ineligible older history when protected output exists, and only allow non-trailing fresh projection entries as last-resort recovery candidates.
  • Add regression coverage where saturated history is followed by a fresh exec-style tool result and then a runtime-context-like message.

Labels considered

  • agents
  • size: S
  • P1
  • proof: sufficient
  • merge-risk: 🚨 session-state
  • status: 👀 ready for maintainer look

Validation

  • Current PR head: 99aee9a7a6b4c2c946b1e2a257cf8be40aa509ae.
  • Merge state after rebasing on latest main: MERGEABLE / CLEAN.
  • Local pre-check on the current head:
    • git diff --check -- src/agents/embedded-agent-runner/tool-result-truncation.ts src/agents/embedded-agent-runner/tool-result-truncation.test.ts passed.
    • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/tool-result-truncation.test.ts passed: 1 file, 58 tests.
  • GitHub Actions on the current head:

Evidence source map

Risk

Low to moderate. The changed surface is the provider prompt-projection truncation/recovery path for saturated sessions. The tradeoff is intentional: when current fresh tool output and older projected history cannot both fit, recovery spends budget on older history before the current fresh result.

Compatibility risk is bounded by keeping trailing-batch protection and persisted trailing-batch behavior separate from the new non-trailing fresh-projection protection. Oversized fresh results still go through the per-result cap, and non-trailing protected fresh projection entries are only last-resort aggregate candidates after older candidates cannot recover enough budget.

Rollback

Revert this PR to restore the previous aggregate recovery ordering. No data migration or config rollback is required.

Docs updated

No docs change. This is an internal prompt-projection recovery fix; regression tests document the behavior boundary.

Related issue / PR

Not tested / Limitations

  • No transport-specific saturated-session WebChat, Discord, or Telegram run was added in this PR. Coverage is at the prompt-projection helper level plus the current GitHub Actions CI matrix for the touched agent surface.

Review context addressed

  • Resolved the merge conflict with latest main.
  • Preserved the existing trailing-batch and persisted trailing-batch protection behavior while adding the runtime-context-following fresh-result path.
  • Strengthened current-head validation and replaced older head evidence with current head 99aee9a7a6b4c2c946b1e2a257cf8be40aa509ae.

Maintainer / ClawSweeper review addressed

  • ClawSweeper noted that this PR changes which tool-result text survives under saturated aggregate recovery. The PR now states the intended tradeoff explicitly and keeps trailing protected batches separate from non-trailing fresh projection entries.
  • ClawSweeper noted that earlier proof was helper-level rather than a full transport run. The limitation remains documented, and the current head has focused helper coverage plus green GitHub Actions for the touched agent shard and broader CI checks.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jul 3, 2026
@snowzlmbot

Copy link
Copy Markdown
Contributor Author

PR body includes issue link, implementation scope, regression coverage, local validation boundary, and risk. @clawsweeper review-pr

@clawsweeper

clawsweeper Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close this PR as superseded by #99756.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #99481
Summary: This PR is the focused candidate fix for the canonical report; the broader open PR partially overlaps and retains the missing fallback ordering plus distinct serializer work.

Members:

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

So I’m closing this here and keeping the remaining discussion on #99756.

Review details

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

Yes. The regressions freeze saturated projected history, append a fresh tool result, and place runtime context after it; an insufficient-history variant can deterministically reproduce the remaining hard-cap defect.

Is this the best way to solve the issue?

No, not yet. Projection freshness is the correct general invariant, but non-trailing fresh projections must remain eligible as the final recovery fallback rather than being excluded from every pass.

Security review:

Security review cleared: The diff changes internal prompt projection and tests only, with no concrete security or supply-chain concern.

AGENTS.md: found and applied where relevant.

What I checked:

  • linked superseding PR: fix: preserve tool-result text across media fallback and context pressure #99756 (fix: preserve tool-result text across media fallback and context pressure) is still open as the canonical replacement.
  • cluster evidence: the durable review links that PR in the work cluster or recommended risk path.
  • no human follow-up: live comments and timeline hydrated by apply contain no non-automation activity after the ClawSweeper review.

Likely related people:

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

@snowzlmbot
snowzlmbot force-pushed the fix/99481-tool-result-empty branch from 3661f2c to 01d341e Compare July 3, 2026 12:11
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 3, 2026
@snowzlmbot

This comment was marked as outdated.

@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 3, 2026
@snowzlmbot
snowzlmbot force-pushed the fix/99481-tool-result-empty branch from 01d341e to 22e36a0 Compare July 3, 2026 12:17
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 3, 2026
@snowzlmbot

This comment was marked as outdated.

@snowzlmbot

Copy link
Copy Markdown
Contributor Author

Added current-head after-fix behavior proof and strengthened the regression assertion.

Current head: b542fdc042e4c9f4437522ac016eeeddb4a358f5

After-fix terminal proof from the tool-result truncation helper with saturated history, a fresh fresh_exec tool result, and a following runtime-context-like user message:

{
  "firstPassTruncatedCount": 43,
  "secondPassTruncatedCount": 1,
  "freshToolCallId": "fresh_exec",
  "freshVisibleText": "ABC",
  "freshPreservedAfterRuntimeContext": true,
  "aggregateToolResultChars": 32000,
  "aggregateBudgetChars": 32000,
  "aggregateCapHeld": true,
  "historicalToolResultsReduced": true,
  "totalMessagesAfterRuntimeContext": 104
}

This directly covers the review request: the fresh tool result remains visible after a runtime/context message, the aggregate cap still holds, and old history is the reduced side of the tradeoff.

GitHub Actions for the new head is running now; I will request a fresh ClawSweeper review after the current-head checks settle.

@snowzlmbot

Copy link
Copy Markdown
Contributor Author

Current head b542fdc042e4c9f4437522ac016eeeddb4a358f5 is updated with after-fix behavior proof in the PR body and comment thread. GitHub Actions latest check table is green: 66 pass / 28 skip / 0 fail / 0 pending.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 3, 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. 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 3, 2026
@snowzlmbot
snowzlmbot force-pushed the fix/99481-tool-result-empty branch 3 times, most recently from 0fcad43 to 456e831 Compare July 6, 2026 10:41
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 6, 2026
@clawsweeper

clawsweeper Bot commented Jul 6, 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.

@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 6, 2026
@snowzlmbot
snowzlmbot force-pushed the fix/99481-tool-result-empty branch from 456e831 to 99aee9a Compare July 6, 2026 11:16
@snowzlmbot

Copy link
Copy Markdown
Contributor Author

Refreshed PR #99524 onto a no-content new head after fixing the PR context heading for the real-behavior gate.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 6, 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.

@markr9805

Copy link
Copy Markdown

+1 — hitting this on a Hermes-hosted agent running glm-5.2:cloud (Zhipu via ollama-cloud). Symptom presents identically to #99481: tool calls execute and produce real output (verified in the trajectory .jsonl), but the agent's session JSONL contains toolCall blocks with no paired toolResult entries — the results get pruned during aggregate recovery before they reach the model's context. From the model's seat every tool "returns empty," so it loops retrying.

Repro shape: long-lived session, heavy exec/read traffic (transcription pipeline orchestration), then a fresh exec: echo ok comes back as (no output) to the agent even though it ran fine. Fresh restart clears it temporarily; recurs within ~30 tool calls.

The non-trailing fresh-result protection in this PR is exactly the missing piece — the trailing-only scan in #98955 doesn't cover the case where a runtime/context message lands between the fresh result and provider dispatch. Thanks for landing this.

@stevenjj33

Copy link
Copy Markdown

+1 from our side. Hitting this in a Feishu DM deployment running OpenClaw 2026.6.11 with MiniMax-M3.

Repro shape (matches yours exactly):

  • Long-lived DM session (4-8h, context saturated at ~200K tokens)
  • Heavy read/exec/memory_search traffic; first break typically after ~30-50 calls
  • From the model's seat: tool calls return empty/silent (no stdout, no error)
  • Verified at the system level (trajectory.jsonl + user-side observation): tools actually execute fine; retry succeeds on 2nd call
  • /new clears it temporarily; recurs within ~30-50 tool calls

Also confirmed via openclaw logs: same sessions show per-chat task exceeded 300000ms cap ... evicting from queue (#92076) — possibly a related but distinct trigger. Either way, this PR's non-trailing fresh-result protection is exactly the missing piece that #98955 doesn't cover. Thanks for the focused fix.

@snowzlmbot
snowzlmbot force-pushed the fix/99481-tool-result-empty branch from 6b5d99f to bcc5aa5 Compare July 11, 2026 13:34
@clawsweeper clawsweeper Bot added 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. and removed 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. labels Jul 11, 2026
@clawsweeper

clawsweeper Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

@clawsweeper clawsweeper Bot closed this Jul 11, 2026
@clawsweeper

clawsweeper Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper could not autoclose this item.

Reason: structured ClawSweeper close marker: close-required (sha=bcc5aa5f92ecaa0dcf4c4b399f96ad6091c3a12a)

Usage: /autoclose <maintainer close reason>. I will close this item and any open same-repo items explicitly referenced in the command text.

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: 🚨 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.

Tool result channel becomes empty after several calls on 2026.7.1-beta.1

3 participants