Skip to content

perf(agents): reduce repeated tool-result budgeting - #139156

Merged
steipete merged 1 commit into
mainfrom
codex/team-tool-result-budget-facts-74cd-20260905
Sep 5, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/team-tool-result-budget-facts-74cd-20260905

Conversation

@steipete

@steipete steipete commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Tool-heavy histories spend unnecessary synchronous work preparing prompts and estimating whether tool-result truncation can avoid compaction. Even when every result fits, the same text is measured twice during projection and three times during reduction estimation.

Why This Change Was Made

The replacement planner now measures each input once and carries that fact through its oversized and aggregate phases. Producers measure replacement messages once; application consumes those measurements. The reduction estimator reuses the planner's original totals.

Measurements are private to one synchronous plan. Returned messages, replacement descriptors, projection state, truncation policies and persisted data retain their existing shapes and behavior. This avoids a cache attached to mutable messages or shared across calls. Production delta: +94/-61 (net +33), for the explicit measurement ownership and boundary conversion; tests are unchanged.

User Impact

Less CPU work during tool-result projection and recovery planning, particularly for CJK-heavy results. Truncation output, frozen prompt prefixes, trailing-result protection, spill references and recovery counters are preserved.

Evidence

  • Before editing, a diagnostic around the complete owner observed 64 budget computations for 32 unchanged CJK results; the once-per-plan assertion failed. The implementation performs 32. Reduction estimation changes from 96 to 32 computations.
  • Thirteen synthetic cases preserve complete baseline output hashes and reduction metrics, including per-result/aggregate/both phases, ASCII/CJK/supplementary text, images, spill pointers, empty/single results, and duplicate or absent tool-call IDs. Additional original-versus-patch diagnostics preserve multi-turn frozen projections, mutations between calls and duplicate branch-ID application semantics.
  • Five alternating, separate-process Node 26.7 runs with 20 warmup calls show median CPU per call of 7.701 → 3.810 ms for below-cap CJK projection and 11.491 → 3.826 ms for reduction estimation. With both truncation phases active, projection is 27.986 → 24.534 ms. The below-cap fixture is 32 results, each containing "漢字カナ한글".repeat(1500), with 64,000 per-result and 2,000,000 aggregate budgets. These are local synthetic measurements of the uninstrumented owner with explicit dormant-import adapters, not production latency. Peak process RSS across the runs was 114,352 → 113,936 KiB.
  • 255 tests in 10 files pass on isolated Linux Node 24.19.0, using the verified source bundle and frozen dependency graph. Coverage includes actual SQLite session recovery, sticky cache-TTL replay, Unicode budgets, context guards, preemptive compaction and provider-bound truncation. This is deterministic application-boundary proof, not an authenticated model call.
  • Independent source review and isolated Codex review found no actionable P0/P1/P2 defects. Pinned formatter and git diff --check pass. The complete required changed check passes on the isolated Linux source bundle (12m57s), and exact-head CI 33972991872 is green. Native preparation also passes.

No permanent private-helper spy or wall-clock assertion was added; the retained failing diagnostic and before/after benchmark prove the performance premise, while existing public-boundary suites protect behavior.

The completed ClawSweeper review was read in full: no findings, no pre-merge requirements, and no Rank-up moves to apply.

@clawsweeper

clawsweeper Bot commented Sep 5, 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.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Sep 5, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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 Sep 5, 2026
@clawsweeper

clawsweeper Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 5, 2026, 10:56 AM ET / 14:56 UTC.

ClawSweeper review

What this changes

The PR reuses tool-output size measurements during prompt preparation and compaction planning, reducing repeated text scans.

Merge readiness

✅ Ready for maintainer review

Keep open: this is a useful optimization not present on inspected main. No actionable correctness or security findings were found.

Priority: P2
Reviewed head: f0f4accf68b7989e10f1cb45b83076b4b6516e4b

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused optimization with concrete comparative measurements, relevant existing coverage, and no identified blocking defect.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: The maintainer-author exemption applies to this internal optimization. The captured body reports Node 26.7 projection and estimation benchmarks on 32 CJK results, lower CPU, unchanged-output diagnostics, and supplemental tests; these were not independently rerun.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The maintainer-author exemption applies to this internal optimization. The captured body reports Node 26.7 projection and estimation benchmarks on 32 CJK results, lower CPU, unchanged-output diagnostics, and supplemental tests; these were not independently rerun.
Evidence reviewed 11 items Introduced scope and merge result: The pinned introduced change affects only the budgeting module. The inspected test merge has the supplied base followed by the exact PR head as parents, and its difference from that base also contains only this module.
Measurement ownership: The complete production module was read. Measurements are created inside one synchronous plan, updated with replacement messages, and removed from outgoing replacement descriptors; original totals remain distinct from reduced totals.
Current main still repeats measurements: Fetched main retains the original planner, including repeated budgeting in replacement application. Its module is identical to the pinned merge-base version, so the PR's optimization remains distinct.
Findings None None.
Security None None.

How this fits together

OpenClaw budgets tool outputs before sending conversation history to a model and when deciding whether truncation can avoid compaction. This planner produces bounded messages and estimates how much context can be recovered.

flowchart TD
  A[Conversation history] --> B[Measure tool outputs]
  B --> C[Apply per-result limits]
  C --> D[Apply aggregate budget]
  D --> E[Bounded model history]
  D --> F[Recovery estimates]
  F --> G[Truncate or compact]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +33. Total +33 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 94 61 +33
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 94 61 +33

Review metrics

Metric Value Why it matters
Production versus tests Production net +33; tests unchanged The added structure gives measurements an explicit plan-local lifetime without expanding public or persisted contracts.
Reported measurement work 32-result fixture: projection 64→32; estimation 96→32 The diagnostic directly measures the repeated work this optimization removes.

Technical review

Best possible solution:

Keep measurement ownership inside the synchronous planner while preserving message shapes, frozen projections, spill references, and recovery accounting.

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

Yes, for the performance premise: current main visibly repeats text budgeting, and the supplied body specifies a concrete CJK benchmark fixture; no benchmark was executed during this review.

Is this the best way to solve the issue?

Yes. The shared planner is the appropriate owner; the neighboring context estimator counts different content, and cross-call caching would introduce unnecessary mutation invalidation.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This is a bounded performance improvement to prompt preparation and compaction planning, with no established urgent user-facing regression.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The maintainer-author exemption applies to this internal optimization. The captured body reports Node 26.7 projection and estimation benchmarks on 32 CJK results, lower CPU, unchanged-output diagnostics, and supplemental tests; these were not independently rerun.

Label justifications:

  • P2: This is a bounded performance improvement to prompt preparation and compaction planning, with no established urgent user-facing regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit 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-author exemption applies to this internal optimization. The captured body reports Node 26.7 projection and estimation benchmarks on 32 CJK results, lower CPU, unchanged-output diagnostics, and supplemental tests; these were not independently rerun.

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)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Safzan Pirani: 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.

@steipete
steipete merged commit fb4e23e into main Sep 5, 2026
187 checks passed
@steipete
steipete deleted the codex/team-tool-result-budget-facts-74cd-20260905 branch September 5, 2026 15:05
@steipete

steipete commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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