Skip to content

fix(agents): keep image history stable during tool loops - #140651

Merged
steipete merged 2 commits into
mainfrom
kv/history-image-prune-tool-loop
Sep 7, 2026
Merged

steipete merged 2 commits into
mainfrom
kv/history-image-prune-tool-loop

Conversation

@steipete

@steipete steipete commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor
Additional instructions

MUST: Keep Allow edits from maintainers enabled for this PR so maintainers
can help update the branch when needed.

What Problem This Solves

Fixes an issue where users continuing a conversation with image history would lose a stable prompt-cache prefix when the assistant entered a tool loop. With three completed turns and an image in turn 1, the first request of turn 4 preserved that image, but the next request replaced it after a tool-call assistant message appeared. Media-reference text had the same defect.

Why This Change Was Made

The shared history-cleanup owner now counts a prior answered turn only when a subsequent user message begins. Removing the end-of-history count keeps the active turn from advancing the cleanup window, including after its final assistant reply. The existing three-completed-turn retention window and replay-only cleanup remain intact.

The session-pruning documentation clarifies the user-turn boundary. Production code is +2/-4 lines; no new configuration, persistence, or provider-specific path is introduced.

User Impact

Recent image blocks and media references stay byte-identical throughout tool-loop requests. Cleanup still removes turn 1's processed media once turn 5 begins, preserving the documented retention window while avoiding this source of mid-turn cache invalidation.

Evidence

Before the fix, the new image and media-reference cases both failed at the retained-prefix byte comparison after a tool call/result; all 30 existing tests passed.

node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/history-image-prune.test.ts src/agents/embedded-agent-runner/run/attempt-setup.test.ts
Test Files  2 passed (2)
Tests       51 passed (51)
Duration    201.43s; wrapper completed in 221.13s

The regression also verifies retention after the final assistant reply, cleanup at the next user message, preservation of newer turns, and an unchanged source transcript. Existing cleanup fixtures now explicitly begin the next user turn; no assertions or snapshots were weakened.

Codex autoreview against the branch merge base, through P1: scoped-clean, zero accepted/actionable findings. Final summary: "The change keeps the pruning cutoff stable throughout the active tool loop and advances it when a subsequent user turn begins. The updated tests cover this behavior for images and media references. No actionable P0 or P1 defects are evident in the supplied patch."

Proof covers replay bytes and the setup boundary; live provider cache-hit metrics were not measured.

node scripts/check-changed.mjs: PASS (exit 0), including formatting, core and core-test typechecking, changed-file lint, dead-export scans, and all selected repository guards. git diff --check: PASS.

The first CI run exposed two Gateway media-ownership fixtures that also needed the next user-turn boundary. Both now assert no pruning before that boundary and retain their original cleanup/ownership assertions afterward. The entire src/gateway/server-methods/chat-send-user-turn.test.ts file passed: 20 tests, 459.05s.

That Gateway pass was the first phase of:

node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/history-image-prune.test.ts src/agents/embedded-agent-runner/run/attempt-setup.test.ts src/gateway/server-methods/chat-send-user-turn.test.ts

The redundant embedded phase then stalled before test execution (idle process, no worker/assertion output for ten minutes) and was terminated; the combined rerun exited 143. The earlier 51-test pass above covers the identical embedded source and tests. Fresh autoreview including the Gateway fixture changes is scoped-clean, with zero accepted/actionable findings through P1.

Final node scripts/check-changed.mjs rerun including the Gateway fixture update: PASS (exit 0). Production LOC remains +2/-4; the additional Gateway proof is +4/-0.

Final exact-head CI: GREEN, run 34079319344, head 90d6174cbacb698040dfd0aae940bbbaf3b745be. CI completed successfully with 108 successful jobs and 16 skipped jobs; the previously failing checks-node-compact-large-3 passed. The bounded PR watcher returned GREEN with zero pending checks.

@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review September 7, 2026 02:27
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Sep 7, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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 Sep 7, 2026
@clawsweeper

clawsweeper Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 6, 2026, 11:35 PM ET / September 7, 2026, 03:35 UTC.

ClawSweeper review

What this changes

The PR keeps recent images and media references unchanged throughout assistant tool loops, advancing history cleanup only when the next user turn begins, with matching tests and documentation.

Merge readiness

✅ Ready for maintainer review

The fix remains necessary on current main and v2026.9.2. The earlier Gateway fixture finding is resolved, and this review found no remaining actionable defect.

Priority: P2
Reviewed head: 90d6174cbacb698040dfd0aae940bbbaf3b745be

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with meaningful regression coverage and the earlier fixture issue resolved.
Proof confidence 🌊 off-meta tidepool Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate; supplied regression results exercise replay-byte stability and Gateway media cleanup, while live provider cache-hit gains remain unmeasured.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate; supplied regression results exercise replay-byte stability and Gateway media cleanup, while live provider cache-hit gains remain unmeasured.
Evidence reviewed 8 items Introduced production change: The verified merge-base-to-head patch removes only the trailing-turn completion count. Prior answered turns still enter the retention window at a subsequent user message; the retention count remains three.
Current main still contains the defect: Current main appends the trailing turn whenever any assistant reply exists. With three prior completed turns, adding a tool-call assistant message therefore moves the pruning cutoff during the active turn.
Latest release check: The v2026.9.2 source contains the same trailing-turn count. The tag resolves to commit 3928bad, so the requested correction is not present in the supplied latest release.
Findings None None.
Security None None.

How this fits together

OpenClaw prepares a replay view of conversation history before sending it to the model. Image cleanup removes older media from that view while preserving recent context and the original transcript.

flowchart TD
  A[Conversation history] --> B[Identify user turn boundaries]
  B --> C[Keep three recent completed turns]
  C --> D[Replace older media in replay]
  D --> E[Prepare model context]
  A --> F[Original transcript preserved]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -2, Tests +46, Docs +1. Total +45 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 1 2 4 -2
Tests 2 63 17 +46
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 66 21 +45

Review metrics

None.

Technical review

Best possible solution:

Keep the shared replay cutoff stable until the next user turn while preserving the existing retention window and original transcript.

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

Yes, from source: retain an image through three answered turns, begin the fourth, then append an assistant tool call; current main advances the cutoff and replaces the older image. No runtime reproduction was executed in this read-only review.

Is this the best way to solve the issue?

Yes. Removing the trailing-turn count repairs the shared boundary directly, and the regression cases cover both stable replay bytes and cleanup at the next user turn.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 43210c2a3e72.

Labels

Label justifications:

  • P2: This repairs a bounded prompt-cache stability defect in conversations containing image history.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate; supplied regression results exercise replay-byte stability and Gateway media cleanup, while live provider cache-hit gains remain unmeasured.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-07T02:34:00.243Z sha ca6fffa :: needs changes before merge. :: [P2] Update both Gateway fixtures to close the fourth turn
  • reviewed 2026-09-07T03:26:08.539Z sha 90d6174 :: needs maintainer review before merge. :: none

@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 Sep 7, 2026
@steipete
steipete merged commit de0b684 into main Sep 7, 2026
190 of 193 checks passed
@steipete
steipete deleted the kv/history-image-prune-tool-loop branch September 7, 2026 03:56
@steipete

steipete commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 7, 2026
…0651)

* fix(agents): keep image history stable during tool loops

* test(gateway): prune media fixtures at user-turn boundaries
necromman pushed a commit to necromman/openclaw that referenced this pull request Sep 20, 2026
…0651)

* fix(agents): keep image history stable during tool loops

* test(gateway): prune media fixtures at user-turn boundaries

(cherry picked from commit de0b684)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant