Skip to content

perf(agents): avoid repeated turn and tool preparation - #133184

Merged
steipete merged 1 commit into
mainfrom
codex/perf50-turn-preparation
Aug 30, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf50-turn-preparation

Conversation

@steipete

@steipete steipete commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Turn preparation repeats work whose results are already available or never consumed. Historical captioned media is normalized and projected even though its caption wins. Transcript context matching repeatedly copies and scans the remaining context set. Schema normalization creates an intermediate tool wrapper that is immediately spread into another wrapper.

Why This Change Was Made

  • Project late-media markers only when filling a blank media turn. Preserve captions, timestamp envelopes, and transcript-storage ownership.
  • Reserve exact runtime-message identities first, then match the remaining contexts within timestamp buckets in the original order. Skip the placeholder result array when there are no contexts; the sole caller already accepts an absent projection.
  • Build one normalized tool wrapper and preserve the existing original-receiver argument preparer and canonical identity-metadata copy.

No persistent cache, provider policy, prompt format, runtime config, protocol, or storage change. The assertion-safety baseline decreases after removing redundant timestamp casts; it is tooling metadata, not a runtime configuration change.

Production +57/−36, net +21: context ownership/indexing +29, lazy media projection +2, wrapper deletion −10. Tooling baseline +1/−1. No new tests duplicate the existing behavior suites; existing tests plus complete-owner differential probes cover the changed paths.

Evidence

Two fresh Node 26.8.1 processes with opposite starting orders, uninstrumented runtime owners, paired samples, exact output checks, and retained source hashes:

Workload Measured change
100 captioned historical media messages 21–23% less preparation time; late-media variant about 40% less
500 turns with 32 cloned contexts 5.8–9% less complete-boundary time
One plain user message 6.9–8.5% less complete-boundary time
Normalize 100 optional-parameter tools 42.23–42.60 µs → 19.37–19.55 µs
Normalize 100 required-parameter tools 24.72–24.88 µs → 17.28–17.38 µs

The blank-media control is effectively unchanged. Isolated lookup-only one-clone work regresses by 0.06–0.13 µs, and isolated blank-media-only work by 5.3–11.1 µs; the complete measured boundary compositions improve. These are preparation measurements, not provider/network latency or memory claims.

Operation witnesses: 1,000 messages/32 cloned contexts reduce 32,000 identity comparisons, 31,008 structural comparisons, and 2,000 Set-to-array copies to 1,000 identity lookups, 32 structural comparisons, and no Set copies. Captioned-media normalization and unused marker projection drop from 100 each to zero. The optional tool path removes one intermediate object allocation and spread.

Correctness evidence includes 3,203 complete-boundary cases across seven compositions, 815 identity cases, and 864 argument comparisons across 108 schema/provider/preparer combinations. Output bytes, object/content identity, nonmutation, duplicate-context ordering, active-turn overrides, original preparer receiver, and action metadata remain covered. Other metadata families continue through the unchanged canonical copy helper and existing tests.

119 tests across four boundary/cache-stability/context-registry/schema suites pass, including a fresh run after rebasing onto merged main (21.39 seconds wall time). The complete changed-file gate passed core and test types, lint, assertion/boundary/import/dead-export guards. Initial assertion-ratchet failures were corrected by deleting unnecessary casts and lowering the baseline, not suppressing the guard. Independent source review and fresh complete automated Codex review reported no accepted findings at the configured P0 threshold. The rebase preserves the exact patch apart from Git blob-index lines.

At head 1cb67e30521b5805edc1f530f9e5f282df166d81, a fresh build passed in 166.56 seconds. An isolated real-OpenAI Gateway completed nine turns: seven in one conversation, including web fetch, plus session listing and native web search. Tool and terminal receipts passed, as did 30 assertions across 14 read-only RPCs, including unchanged baseline titles/previews, tool inventories, and model catalog. CPU profiles were retained and the task Gateway stopped cleanly. Live history was text and web-tool history; historical-media variants are proved by the complete-owner probes and canonical boundary suites. Live timings are observational.

Landing Review

Exact-head CI run 33300641780 is green at 1cb67e30521b5805edc1f530f9e5f282df166d81. The latest ClawSweeper review found no actionable code defect; its rank-up move was to finish the remaining exact-head checks, now satisfied. Maintainer landing review accepts the scoped indexing/lazy-projection tradeoff and the disclosed small isolated controls. The independent owner reviews, full changed-file checks, fresh automated review, build and live Gateway evidence above are complete.

@clawsweeper

clawsweeper Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Aug 30, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Aug 30, 2026
@clawsweeper

clawsweeper Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 30, 2026, 4:14 AM ET / 08:14 UTC.

ClawSweeper review

What this changes

The PR avoids unused historical-media projection, indexes transcript contexts for matching, and removes an intermediate tool-schema wrapper during agent-turn preparation.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

No introduced correctness or security defect was found in the reviewed agent-preparation paths. This member-authored, maintainer-labelled performance PR remains for ordinary maintainer review once exact-head checks complete.

Priority: P3
Reviewed head: 1cb67e30521b5805edc1f530f9e5f282df166d81
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The focused patch preserves its reviewed caller and consumer contracts and has substantive existing boundary coverage.
Proof confidence 🌊 off-meta tidepool Not applicable: This repository-member PR is exempt from the external-contributor proof gate; its supplied live Gateway report and owner-boundary probes are supplemental rather than merge-gate proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This repository-member PR is exempt from the external-contributor proof gate; its supplied live Gateway report and owner-boundary probes are supplemental rather than merge-gate proof.
Evidence reviewed 5 items Introduced patch: The verified introduction delta changes only the agent tool-schema and LLM-boundary preparation paths plus the assertion baseline; the implementation preserves the existing metadata-copy owner after building the normalized wrapper.
Caller and consumer contract: The LLM boundary is the sole production caller of the transcript projection; its sender-context consumer accepts an absent projection, so no-context input remains equivalent to the prior all-empty result.
Existing boundary coverage: Existing tests exercise textless structural matching, blank and late-media projection bytes, nonmutation, and parameterless-tool argument preparation through the agent loop; the PR adds no tests but reuses these owner-boundary suites.
Findings None None.
Security None None.

How this fits together

Before a model request, OpenClaw normalizes prior messages, restores needed transcript context, and prepares the authorized tool set. These changes reduce work at that boundary while retaining the prepared model input and tool behavior.

flowchart LR
  A[Stored messages] --> B[Transcript context matching]
  C[Media attachments] --> D[Historical media projection]
  B --> E[Prepared agent turn]
  D --> E
  F[Authorized tools] --> G[Schema normalization]
  G --> E
  E --> H[Model request]
Loading

Decision needed

Question Recommendation
Should this member-authored performance cleanup proceed through ordinary maintainer review after the remaining exact-head checks complete? Proceed with normal review: Keep the PR open for maintainer approval after the current head’s required checks complete.

Why: The PR has a protected maintainer label and its author is a repository member, so automated cleanup must not close or merge it.

Before merge

  • Resolve merge risk (P1) - Two exact-head checks were still in progress in the supplied PR state, so merge readiness is not yet established.
  • Complete next step (P2) - This protected member-authored PR has no concrete automated repair; it needs the normal maintainer approval and completed exact-head checks.
Agent review details

Security

None.

PR surface

Source +21, Other 0. Total +21 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 3 57 36 +21
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 1 1 0
Total 4 58 37 +21

Review metrics

Metric Value Why it matters
Production versus test delta production +57/−36 (net +21); tests 0; tooling baseline +1/−1 The modest production growth implements indexing and lazy projection while relying on existing owner-boundary coverage.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Retain the single canonical preparation path, preserve its existing boundary contracts, and complete normal maintainer review after the current head’s checks finish.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Retain the single canonical preparation path, preserve its existing boundary contracts, and complete normal maintainer review after the current head’s checks finish.

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

Not applicable: the PR is a performance cleanup, not a report of broken existing behavior. Source inspection establishes the affected preparation paths and their existing behavioral coverage.

Is this the best way to solve the issue?

Yes: the change removes repeated work at the preparation owners while keeping the existing transcript projection and metadata-copy seams intact.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P3: This is a bounded performance optimization with no demonstrated regression or new user-facing capability.
  • 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: This repository-member PR is exempt from the external-contributor proof gate; its supplied live Gateway report and owner-boundary probes are supplemental rather than merge-gate proof.

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)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Wait for the remaining exact-head checks before treating the PR as merge-ready.

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 (1 earlier review cycle)
  • reviewed 2026-08-30T08:08:07.141Z sha 1cb67e3 :: needs maintainer review before merge. :: none

@steipete
steipete merged commit 5cb702d into main Aug 30, 2026
248 checks passed
@steipete
steipete deleted the codex/perf50-turn-preparation branch August 30, 2026 08:48
@steipete

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 P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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