Skip to content

fix(agents): scale aggregate tool-result budget with context window and compact on pressure - #100077

Merged
obviyus merged 2 commits into
mainfrom
codex/tool-result-aggregate-compaction
Jul 4, 2026
Merged

obviyus merged 2 commits into
mainfrom
codex/tool-result-aggregate-compaction

Conversation

@obviyus

@obviyus obviyus commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes #100042.

OpenClaw had a unit mismatch between window-relative compaction and an absolute aggregate tool-result cap. Large contextWindow sessions could hit the 256k-char aggregate tool-result clamp on every model call long before window-relative compaction saw pressure, silently truncating even fresh tool output to empty content while only emitting an INFO-level signal.

Why This Change Was Made

The aggregate tool-result budget now scales with the model context window: max(perResultMaxChars * 4, floor(contextWindowTokens * 4 * 0.5)). The 0.5 share mirrors safeguard compaction maxHistoryShare and SINGLE_TOOL_RESULT_CONTEXT_SHARE, while the 4x per-result floor preserves existing small-window behavior.

Trailing tool results are structurally excluded from aggregate clearing and protected with the 2000-char min-keep floor when truncation is still needed. Protected trailing aggregate pressure is reported separately from destructive replacements, so fresh output can request compaction without being erased.

Aggregate engagement is routed through the existing precheck overflow recovery as compact_then_truncate, so the session performs real compaction and then runs the persistent tool-result truncation backstop for forward progress. Failure mode is bounded: MAX_OVERFLOW_COMPACTION_ATTEMPTS = 3, then one persistent truncation attempt, then explicit give-up with the existing blocked context-overflow response; no unbounded compaction loop.

User Impact

Large-contextWindow sessions no longer silently lose fresh tool output just because the absolute aggregate cap was hit. Operators get a once-per-session WARN with /compact and /new hints when aggregate pressure engages. Small-window behavior is unchanged because the old 4x aggregate floor is preserved.

Evidence

Field evidence for the failure mode is documented in issue #100042.

Verification run after the final rebase:

  • pnpm tsgo — passed, exit code 0.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/tool-result-truncation.test.ts — passed, 47 tests.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts — passed, 69 tests.
  • git diff --check — passed.
  • .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --prompt-file <issue-context> — clean, no accepted/actionable findings.

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

clawsweeper Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 4, 2026, 3:51 PM ET / 19:51 UTC.

Summary
The PR changes embedded-agent tool-result truncation so the aggregate prompt-history budget scales with context window, protects trailing tool results during recovery, and routes aggregate pressure into compact-then-truncate recovery.

PR surface: Source +127, Tests +145. Total +272 across 6 files.

Reproducibility: Source-reproducible: current main still applies a fixed aggregate prompt-history cap while compaction routing is window-relative, and the linked issue includes logs. I did not run a live long-lived 1M-window provider replay in this read-only review.

Review metrics: 2 noteworthy metrics.

  • Aggregate budget policy: 1 formula changed. The prompt-history aggregate tool-result cap now scales with context window, which changes when compaction and transcript truncation engage.
  • Required type gate: 1 failing check-test-types job. The branch cannot land until the added test fixture satisfies the repository test TypeScript check.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #100042
Summary: This PR is the linked candidate fix for the open aggregate tool-result truncation and compaction-pressure issue.

Members:

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

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦪 silver shellfish
Result: blocked until real behavior proof from a real setup is added.

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

Rank-up moves:

  • [P1] Fix the TS2352 failure in the added context-engine test fixture.
  • [P1] Add redacted after-fix proof from a real or live-like long-context session showing aggregate pressure compacts and preserves fresh tool output.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body provides typecheck/unit-test/diff/autoreview evidence only, with no after-fix live session, logs, transcript, terminal output, or artifact showing the changed recovery behavior. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] The PR changes aggregate truncation and recovery behavior for existing large-context sessions, including sessions with explicit toolResultMaxChars, so maintainers need to accept that upgrade behavior before merge.
  • [P1] The PR body lists typechecks, unit tests, diff check, and autoreview only; it does not show an after-fix live session, redacted transcript, logs, or copied runtime output proving the long-context recovery path.

Maintainer options:

  1. Prove the recovery path before merge (recommended)
    Require a redacted live run, transcript, or logs showing aggregate pressure triggers compaction, preserves fresh tool output, and retries successfully in a large-context session.
  2. Accept the policy change explicitly
    Maintainers may choose to land the new aggregate-budget policy based on source and unit coverage, but should own the behavior change for configured context-limit users.
  3. Pause for narrower recovery design
    If the interaction with explicit toolResultMaxChars or compaction loops is not settled, pause this PR and keep the linked bug open for a smaller approved recovery policy.

Next step before merge

  • [P1] Human follow-up is needed because the contributor must supply real behavior proof and the branch has a visible type-check failure; ClawSweeper should not repair while the proof gate is missing.

Security
Cleared: No concrete security or supply-chain concern found; the diff does not change dependencies, workflows, secrets, install scripts, or external code execution surfaces.

Review findings

  • [P2] Cast the assistant fixture through unknown — src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts:3183-3187
Review details

Best possible solution:

Fix the failing test type error, add redacted after-fix runtime proof for the aggregate-pressure recovery path, and land this only after maintainers accept the new session recovery policy.

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

Source-reproducible: current main still applies a fixed aggregate prompt-history cap while compaction routing is window-relative, and the linked issue includes logs. I did not run a live long-lived 1M-window provider replay in this read-only review.

Is this the best way to solve the issue?

No, not as submitted: the implementation is in the right runtime area, but the branch currently fails a required type check and lacks after-fix real behavior proof. Maintainers also need to accept the aggregate-pressure recovery policy before merge.

Full review comments:

  • [P2] Cast the assistant fixture through unknown — src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts:3183-3187
    check-test-types is failing with TS2352 on this newly added fixture because the object is cast directly to AgentMessage; match the surrounding test pattern and cast through unknown first so the required test TypeScript check can pass.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.93

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add P1: The linked bug affects long-lived agent sessions where the model can lose visibility into tool output, breaking real workflows without a crash.
  • add merge-risk: 🚨 compatibility: The PR changes aggregate truncation heuristics around a documented context-limit knob that existing users may have tuned.
  • add merge-risk: 🚨 session-state: The PR changes compaction, prompt-history projection, and persistent transcript truncation behavior for existing sessions.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.

Label justifications:

  • P1: The linked bug affects long-lived agent sessions where the model can lose visibility into tool output, breaking real workflows without a crash.
  • merge-risk: 🚨 compatibility: The PR changes aggregate truncation heuristics around a documented context-limit knob that existing users may have tuned.
  • merge-risk: 🚨 session-state: The PR changes compaction, prompt-history projection, and persistent transcript truncation behavior for existing sessions.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.
Evidence reviewed

PR surface:

Source +127, Tests +145. Total +272 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 3 175 48 +127
Tests 3 163 18 +145
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 338 66 +272

What I checked:

Likely related people:

  • steipete: Recent GitHub history shows multiple embedded-runner attempt and run-path changes near the context assembly and recovery surface. (role: recent area contributor; confidence: high; commits: 79d2e568a0aa, ab2f6f564272, 3d6a2216ea77; files: src/agents/embedded-agent-runner/run/attempt.ts, src/agents/embedded-agent-runner/run.ts)
  • vincentkoc: Recent history for tool-result-truncation.ts includes several commits on aggregate prompt reduction, frozen prompt bytes, prompt projection output, and transformed tool-result preservation. (role: recent tool-result projection contributor; confidence: high; commits: efe3cbd695c8, 79fac9fda92c, ef6dc8f7e5b1; files: src/agents/embedded-agent-runner/tool-result-truncation.ts)
  • momothemage: Authored the recent fresh tool-result preservation change under the aggregate cap, directly adjacent to the fresh-output loss part of the reported behavior. (role: adjacent predecessor fix author; confidence: high; commits: d45b8be939a1; files: src/agents/embedded-agent-runner/tool-result-truncation.ts, src/agents/embedded-agent-runner/tool-result-truncation.test.ts)
  • yetval: Recent history for preemptive-compaction.ts includes a focused fix to count additional turn types in pre-prompt overflow routing, the sibling decision surface this PR changes. (role: pre-prompt overflow precheck contributor; confidence: medium; commits: 24626e5266b7; files: src/agents/embedded-agent-runner/run/preemptive-compaction.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.

@obviyus obviyus self-assigned this Jul 4, 2026
@obviyus
obviyus force-pushed the codex/tool-result-aggregate-compaction branch from cdbf11b to dc15eb3 Compare July 4, 2026 23:18
@obviyus
obviyus merged commit 4682364 into main Jul 4, 2026
96 of 98 checks passed
@obviyus
obviyus deleted the codex/tool-result-aggregate-compaction branch July 4, 2026 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: M

Projects

None yet

1 participant