Repository navigation
fix(tool-result-truncation): skip historical tool results in aggregate reduction to preserve prompt cache stability - #95264
Conversation
…e reduction to preserve prompt cache stability When the same tool result message is processed across multiple turns, the aggregate reduction logic in buildAggregateToolResultReplacements() can progressively re-truncate older entries to make room for new tool output. This changes the byte representation of historical tool results between adjacent model requests, breaking prompt cache prefix stability. The fix adds a truncation-marker detection for text content blocks and skips messages that already had a truncation marker before the oversized truncation step. This ensures that once a historical tool result has been projected into model-facing form, it stays stable across turns. Detection regex: /\[\.\.\. \d+ .*(chars|characters) truncated;/ This matches both the DEFAULT_SUFFIX and COMPACT_RECOVERY_SUFFIX patterns as well as the MIDDLE_OMISSION_MARKER. Fixes openclaw#95219 Co-Authored-By: Claude <[email protected]>
|
Thanks for the context here. I did a careful shell check against current Current main already contains a broader maintainer implementation for the same prompt-cache stability problem, so this branch is obsolete even though the idea was pointed at a real bug. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. So I’m closing this as already implemented rather than keeping a duplicate issue open. Review detailsBest possible solution: Keep the current main projection-state implementation from the merged maintainer PR and close overlapping contributor branches for this same fix path. Do we have a high-confidence way to reproduce the issue? Yes, at source level: the linked issue's failing shape is the live prompt projection path, and current main now has regression coverage for byte-stable projections as history grows. I did not run tests because this was a read-only review. Is this the best way to solve the issue? Yes for current main, not this branch: the merged projection-state implementation is the better fix because it reuses per-attempt projected bytes by tool-result identity instead of relying on marker detection in message text. Security review: Security review cleared: No concrete security or supply-chain concern was found; the PR diff touches agent prompt truncation code and a colocated test only. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against de60f4276793; fix evidence: commit 3288291a0869, main fix timestamp 2026-06-22T21:03:58+08:00. |
…runcated-between-turns-cache-stability
…nst aggregate budget Already-truncated tool results must remain byte-stable for prompt cache prefix stability, but their text length must still count against the aggregate reduction budget. Previously they were filtered from candidates before totalChars was computed, causing the system to underestimate prompt pressure when many historical truncated messages exist. - Partition candidates into skippedTruncated (historical, not re-truncatable) and eligible (fresh, truncatable) instead of filtering out historical ones - totalChars includes both partitions so the budget is correctly sized - Reduction is only applied to eligible entries, preserving historical stability Addresses review finding: - P1: Count skipped history against the aggregate budget Part of openclaw#95219
00522ce to
20cb986
Compare
|
@clawsweeper re-review Updated aggregate accounting: already-truncated historical tool results remain byte-stable but their text length still counts toward the aggregate budget via a partition (skippedTruncated vs eligible). totalChars now includes both partitions, while reduction is only applied to eligible entries. |
|
🦞🧹 I asked ClawSweeper to review this item again. |
- Rename truncatedCount to _truncatedCount (unused variable lint) - Replace !!originalEntry?.message with != null check (implicit coercion lint) Part of openclaw#95219
When already-truncated messages count against the aggregate budget, totalChars is at or near the budget cap rather than exceeding it by ~79 chars (the stable message text). Update test expectations. Part of openclaw#95219
|
ClawSweeper applied the proposed close for this PR.
|
Summary
When the same tool result message is processed across multiple turns, the aggregate reduction logic in
buildAggregateToolResultReplacements()can progressively re-truncate older entries to make room for new tool output. This changes the byte representation of historical tool results between adjacent model requests, breaking prompt cache prefix stability.Fixes #95219
Real behavior proof (required for external PRs)
Behavior addressed: Historical tool results are re-truncated between turns, causing the same message to appear differently across model requests (e.g., head-preserving → head+tail → pure placeholder).
Real setup tested:
Exact steps or command run after fix:
After-fix evidence:
Observed result after the fix: All 35 tests pass, including 2 new cross-turn stability tests that verify already-truncated messages are not re-truncated.
What was not tested: Full integration test with live LLM session (requires model access). The fix is verified at the unit-test level with realistic truncation marker patterns.
Tests and validation
New tests
[... N more characters truncated; ...]suffix are skipped by aggregate reduction⚠️ [... middle content omitted …]pattern are also skippedRisk checklist
Did user-visible behavior change? (
No)Did config, environment, or migration behavior change? (
No)Did security, auth, secrets, network, or tool execution behavior change? (
No)What is the highest-risk area?
How is that risk mitigated?
Current review state
What is the next action?