Skip to content

improve: reuse UTF-8 encoding while fitting Code Mode output - #137132

Merged
steipete merged 1 commit into
mainfrom
codex/perf6-code-mode-prefix
Sep 3, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf6-code-mode-prefix

Conversation

@steipete

@steipete steipete commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fitting Code Mode output to a JSON budget repeatedly encoded the same string during binary-search trials.

Why This Change Was Made

The result owner now prepares one bounded UTF-8 encoding per synchronous prefix search and shares the existing boundary decoder. One-shot truncation retains its allocation-free full-fit path. Nonpositive bounds return before encoding; prepared limits cannot exceed the bound used to create the helper.

User Impact

Output, error text, Unicode handling and byte/context budgets remain unchanged. Production +18 LOC expresses the bound-owned prepared encoding and shared decoder; tests +13. No cache, public option or plugin SDK export is added.

Evidence

  • All 672 actual-owner projections match exactly across byte/context limits, errors, repeated waits and network content. Another 1,224 supported-bound comparisons match one-shot truncation, including fractional limits and lone surrogates.
  • Ten fixed projections perform 340 string-to-Buffer conversions instead of 4,770. Their summed encoded byte volume falls from 45,335,820 to 4,406,570. Buffer pooling means this is conversion work, not physical allocation or retained memory.
  • Independent review caught eager encoding for negative bounds in the first candidate. The corrected version eliminates that conversion; its original source, adverse allocation observation and correction proof remain retained. No whole-Gateway RSS or general throughput improvement is claimed.
  • UTF-8, Code Mode output and result-budget tests pass, as does the canonical changed-file gate. Independent source review and managed Codex review through P2 are clean. Real Gateway oversized-Unicode output verification passed; details below.

The first hosted CI run failed on unchanged src/node-host/runner.test.ts exceeding the repository line limit (1,006 versus 1,000). That main-branch test cleanup is being repaired separately; this PR will not land until its checks pass.

Real Gateway integration (2026-09-03)

A prebuilt, isolated Gateway with a real OpenAI API key passed four fixed turns: ordinary completion, real web_fetch of example.com, a 1.31 MB Unicode return through Code Mode, and a real MCP stdio tool with default and explicit arguments. Fifty-seven Gateway RPCs verified three session identities, full/repeated/paginated/subscribed list rows, descriptions and final previews, transcript completion, an explicit reconnect, empty approval replay and unsubscribe.

The MCP server observed the default id: alpha before server-side validation, followed by explicit id: beta; both nested call identities/order, structured results and metadata projection matched. The Unicode prefix remained well formed and within its byte budget with exact omitted-byte accounting. All four turns completed without retries.

The local immutable composite 3bb6e3164a06e5c35fb50b368cc63cbb05826384 contains the twelve published campaign changes through #137148, including this PR’s reviewed source. Source/build/dependency identity was checked before and after. Both clients closed, the Gateway exited successfully without escalation, all recorded MCP processes exited, and private state/port cleanup passed. This is functional integration proof; it does not attribute memory or latency differences to this PR.

Scoped wall-time and RSS benchmark

A fixed ABBA run used fresh Node 26.8.1 processes, 10 warmup projections and 100 measured projections of the same oversized Unicode output/value through the actual Code Mode JSON owner. Every timed result digest matched across all four processes; source hashes were checked before and after. No Inspector sampling or forced GC was used.

Arm 100-projection wall time Whole-process wall time Peak RSS
Baseline A1 1291.46 ms 1.61 s 145.06 MiB
Candidate B1 904.45 ms 1.19 s 131.33 MiB
Candidate B2 896.91 ms 1.17 s 130.73 MiB
Baseline A2 1286.85 ms 1.61 s 145.97 MiB

Peak RSS is the macOS /usr/bin/time -l whole-process high-water mark, including imports and warmup. This narrow workload improved in both pairs, but two observations per arm do not establish a general throughput or retained-memory improvement. The separate real Gateway comparison is a different multi-change experiment and is not attributed to this PR. Earlier allocation samples, including the initial candidate's adverse result, remain recorded.

Benchmark source SHA-256: 9bb9264d3f749689961ee0f5ea960ca96097ec34361b5ff45c720490b34c7585. Fixed output digest: 3419d9fdb044f9ddbe4a1fd92251b66a4275a2c0f8b3d25715de56c9a3c7ef24. All four supervised processes exited zero; their process groups and private runtime directories were removed.

@clawsweeper

clawsweeper Bot commented Sep 3, 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: XS maintainer Maintainer-authored PR labels Sep 3, 2026
@steipete
steipete marked this pull request as ready for review September 3, 2026 07:20
@steipete

steipete commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Real Gateway follow-up: all four fixed real OpenAI turns and 57 RPC checks passed on the immutable candidate composite containing this PR. The PR body now records web fetch, oversized Unicode, MCP schema/default/dispatch and session/subscription/reconnect evidence, plus the source identity and clean shutdown limits. This supersedes the earlier pending integration note.

@clawsweeper

clawsweeper Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 3, 2026, 5:09 AM ET / 09:09 UTC.

ClawSweeper review

What this changes

The PR prepares one bounded UTF-8 encoding for Code Mode’s JSON-prefix search and adds contract coverage for reusable prefix reads.

Merge readiness

⛔ Blocked before merge - 2 items remain

Keep this maintainer-authored PR open for normal maintainer approval. The current-main base repeatedly re-encodes each binary-search prefix; the introduced bounded encoder preserves the documented UTF-8 and serialized-budget contract, with no actionable correctness or security finding.

Priority: P3
Reviewed head: 6685a285f8d3b29b4065d1e04bdbd6013cd09d9a
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, contract-preserving optimization with useful utility-level coverage and no actionable review finding.
Proof confidence 🌊 off-meta tidepool Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body nevertheless records an isolated real-Gateway oversized-Unicode result through the changed Code Mode output owner.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body nevertheless records an isolated real-Gateway oversized-Unicode result through the changed Code Mode output owner.
Evidence reviewed 8 items Current-main behavior: The verified PR base is current main; its prefix-fitting loop calls one-shot UTF-8 truncation for every binary-search trial.
Introduced owner-boundary change: The PR prepares the bounded encoding once and uses it only inside the existing JSON-prefix binary search.
Output contract: Code Mode documentation requires cumulative output and terminal data to share a serialized UTF-8 budget, with UTF-8-safe truncated prefixes and omitted-byte accounting.
Findings None None.
Security None None.

How this fits together

Code Mode bounds tool-result JSON before it returns to the agent. The execution and headless paths use the output-state owner to fit Unicode-safe prefixes within the shared result budget.

flowchart LR
  A[Tool or guest output] --> B[Code Mode execution]
  B --> C[Output-state owner]
  C --> D[Prepared UTF-8 prefix]
  D --> E[JSON budget check]
  E --> F[Bounded agent result]
Loading

Decision needed

Question Recommendation
Should this maintainer-authored Code Mode optimization be approved once exact-head CI is green? Approve after exact-head CI: Accept the focused performance change because its only production caller stays within the prepared bound and preserves the documented result contract.

Why: Repository policy keeps MEMBER-authored and maintainer-labeled work open for explicit maintainer handling even when the patch review is clean.

Before merge

  • Complete next step (P2) - A listed maintainer should approve the performance trade-off after exact-head CI completes successfully.
  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.
Agent review details

Security

None.

PR surface

Source +18, Tests +13. Total +31 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 26 8 +18
Tests 1 14 1 +13
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 40 9 +31

Review metrics

Metric Value Why it matters
Production versus test LOC production +18, tests +13 The small production increase is directly tied to one reusable bounded encoding and shared boundary decoder; tests cover equivalence and repeated-limit behavior.

Technical review

Best possible solution:

Preserve the existing bounded-result contract and merge the focused optimization once a listed maintainer accepts the measured trade-off and exact-head checks are green.

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

Not applicable: this PR optimizes an internal existing path rather than reporting a user-visible defect; source comparison identifies the repeated encoding at the current-main owner.

Is this the best way to solve the issue?

Yes. Preparing the encoding at the existing binary-search owner avoids repeated conversion without changing the caller’s JSON-budget decision or one-shot truncation callers.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 590ee376ad0c.

Labels

Label justifications:

  • P3: This is a small internal performance improvement with no intended user-visible behavior change.
  • 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: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body nevertheless records an isolated real-Gateway oversized-Unicode result through the changed Code Mode output owner.

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-03T07:48:33.053Z sha 82cdb01f9b0e2d204c8562aa9a7b8b5323730c8d :: blocked before merge. :: [P3] Record the required performance benchmark
  • reviewed 2026-09-03T08:21:33.540Z sha 81bed82a666bdff75095454d11bee79d1a8b434f :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the codex/perf6-code-mode-prefix branch from 82cdb01 to 81bed82 Compare September 3, 2026 08:08
@steipete

steipete commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the benchmark finding and rank-up request in the PR body. Fixed ABBA runs of 100 oversized-Unicode projections measured baseline 1291.46/1286.85 ms and 145.06/145.97 MiB peak process RSS, versus candidate 904.45/896.91 ms and 131.33/130.73 MiB. Every result digest matched; all four processes cleaned up. These scoped observations do not establish a general Gateway or retained-memory improvement. The branch includes the upstream main lint fix #137149 with reviewed source hashes unchanged; required CI is running.

@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 Sep 3, 2026
@steipete
steipete force-pushed the codex/perf6-code-mode-prefix branch from 81bed82 to 6685a28 Compare September 3, 2026 09:01
@steipete

steipete commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Final composite 565e5bb09052372405cbbb13e756f453547e86fe passed 10 real OpenAI turns across 14 memory stages, plus a separate four-turn smoke: 62 RPCs, two closed clients, stable identities, clean unforced shutdown and runtime/port cleanup.

Oversized Unicode output retained a well-formed prefix, its byte budget and exact omitted-byte accounting. The separate encoded-buffer counters remain the direct prefix-preparation evidence. The composite’s sampled reduction, predominantly in an anonymous/non-arm group, cannot be assigned specifically to this PR.

One fixed pair: main-isolate Unicode allocation estimates −35.0037%/−31.3677%; idle post-GC RSS/heap deltas +4.203125/−0.125908 MiB (Code Mode), −3.718750/−0.012688 MiB (Tool Search). RSS covers the Gateway process; heap covers its main isolate. This supports no per-PR attribution or general RSS claim.

Exact-head CI and native landing gates remain required.

@steipete

steipete commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Maintainer disposition: accepting the prepared UTF-8 optimization for landing now that exact-head CI is green. The additional 18 production lines establish one bounded reusable encoding while preserving the serialized output contract. The fixed ABBA fixture measured 1,291/1,287 ms before and 904/897 ms after; the contract comparisons and 169 focused tests passed. The composite live Unicode allocation reduction is supporting integration evidence, not a per-PR RSS or latency claim.

@steipete
steipete merged commit c674d6b into main Sep 3, 2026
186 checks passed
@steipete
steipete deleted the codex/perf6-code-mode-prefix branch September 3, 2026 09:36
@steipete

steipete commented Sep 3, 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 4, 2026
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: XS 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