Skip to content

refactor(agents): defer unused MCP mirror serialization - #148929

Merged
steipete merged 5 commits into
mainfrom
codex/r22-mcp-lazy-mirror
Sep 16, 2026
Merged

steipete merged 5 commits into
mainfrom
codex/r22-mcp-lazy-mirror

Conversation

@steipete

@steipete steipete commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

MCP structured results build a duplicate JSON string even when no text block needs comparison against it.

User Impact

JSON-transported MCP results retain canonical formatting, recovery text, media content, and exact mirror suppression.

Why This Change Was Made

Create the mirror string only when a text block needs it, then reuse it for subsequent comparisons.

Evidence

Earlier proof passed 34 MCP materialization cases and 21 node-plugin tool cases. Managed P2 review found no actionable findings within the transported-JSON contract. Retained exact-head CI run 34986844660 passed.

A separate composition run passed all 36 cases in src/agents/agent-bundle-mcp-tools.materialize.test.ts, including both newer cancelled-catalog cleanup cases. It combined this PR's projection with the materialization owner, complete fixture, and stable-stringify owner from d886b8e374ebe4652fd35204a87a44e9294bf244, using the existing dependency graph at 6ef6f0fb0c7bf1ccd3502c2f5d5880c9bba31dd2. All four source overlays were restored after verified process completion. This establishes the selected-owner composition in that fixed context; it is not a full current-main installation or live MCP transport test. The fixture supplies ordinary explicit MCP names and does not exercise the retained older allowlist alias branch.

Paired Projection Benchmark

Eight fresh Node 26.8.2 processes exercised the exported projectMcpCallToolResult adapter. Each process ran 32 warmups and 2,048 timed projections of the same synthetic 128-record search page (64,411 bytes of compact structured JSON). The no-text shape has an empty content array. The mirror shape has the exact pretty-JSON mirror plus pagination guidance. Complete projected output, unchanged input/details, structured-data identity, and raw guest-data preservation passed in every process. Input and output hashes matched between arms.

Pair 1 ran before then after; pair 2 reversed that order. Each arm ran no-text then mirror in separate processes. Only this PR's mcp-content.ts change differed between arms. Both used the selected stable-stringify.ts source from 5016e177ab99597874859b643f29f3f9fcf7715d with the existing dependency graph at 6ef6f0fb0c7bf1ccd3502c2f5d5880c9bba31dd2, including fs-safe 0.11.0. This qualifies that fixed source/dependency composition, not an exact current-main installation.

Order Pair Shape Arm Loop seconds Process real seconds Peak RSS bytes
1 1 no-text before 0.736810541 1.11 197820416
2 1 mirror before 0.731549083 1.04 198574080
3 1 no-text after 0.622204291 0.94 199753728
4 1 mirror after 0.734332750 1.05 199868416
5 2 no-text after 0.593246541 0.91 199966720
6 2 mirror after 0.725640417 1.03 200048640
7 2 no-text before 0.719046875 1.03 200540160
8 2 mirror before 0.733570333 1.04 199426048

The no-text loop took 15.6–17.5% less time in these samples. Mirror loop time changed by +0.38% and -1.08%. Mean peak RSS increased by 679,936 bytes for no-text and 958,464 bytes for mirror; these measurements show no RSS saving.

Loop timing excludes fixture preparation, warmup, and full acceptance checks. /usr/bin/time -l process seconds and peak RSS include those steps plus loader/import work. There were two observations per arm and shape, without competing campaign workloads; desktop/OS activity remained uncontrolled. These results cover synthetic adapter projection, not live MCP transport, Gateway performance, allocation totals, or retained heap. All eight processes completed successfully, and both source overlays and original file modes were restored after independently verified cleanup.

@clawsweeper

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

clawsweeper Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 16, 2026, 2:46 PM ET / 18:46 UTC (Revision 7).

ClawSweeper review

What this changes

Defer building the duplicate JSON text for MCP tool results until a text block needs comparison, then reuse it.

Merge readiness

✅ Ready for maintainer review

This remains a useful, narrowly scoped optimization absent from fetched main and the latest release. No actionable correctness or security findings remain.

Priority: P3
Reviewed head: c889892375273afa89604e9dc74b23b88da2cefa

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused optimization with appropriately bounded benchmark evidence and no actionable findings.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: The external-contributor gate does not apply to this verified repository administrator. The captured benchmark exercises the changed projection adapter and reports matching output with faster no-text processing; it does not establish live MCP or Gateway performance.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The external-contributor gate does not apply to this verified repository administrator. The captured benchmark exercises the changed projection adapter and reports matching output with faster no-text processing; it does not establish live MCP or Gateway performance.
Evidence reviewed 7 items Verified introduced change: The local pinned base-to-head diff matches the supplied introduction evidence: only mirror serialization timing changes; canonical formatting and exact equality remain intact.
Still necessary on main and release: Both fetched main and v2026.9.4 retain eager mirror serialization before checking content blocks; neither contains this optimization.
Production callers and preserved contracts: Configured MCP tools pass completed runtime results through this adapter; node-hosted MCP tools use the same projection. Existing materialization cases cover recovery text, mixed media, exact mirrors versus near matches, and structured-only deterministic output. These tests were inspected, not executed.
Findings None None.
Security None None.

How this fits together

OpenClaw converts MCP server results into content the agent can consume. This adapter preserves structured data, recovery text, and media while removing exact duplicate JSON text.

flowchart TD
  A[MCP server result] --> B[Result projection]
  B --> C[Canonical structured text]
  B --> D{Text block present?}
  D -->|Yes| E[Build and reuse mirror comparison]
  D -->|No| F[Preserve media and other content]
  E --> G[Agent result]
  F --> G
  C --> G
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +5. Total +5 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 7 2 +5
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 7 2 +5

Review metrics

Metric Value Why it matters
Reported adapter timing 15.6–17.5% faster no-text loops; no RSS saving The paired benchmark supports the narrow serialization optimization without claiming whole-Gateway improvement.

Technical review

Best possible solution:

Keep the optimization inside the existing result adapter while preserving exact mirror suppression and all other projected content.

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

Not applicable as a behavior bug: source confirms the unnecessary eager serialization, and the supplied benchmark measures its cost; no code was executed during this read-only review.

Is this the best way to solve the issue?

Yes. Lazy initialization inside the existing filter avoids unused work without adding another owner or changing the transported-JSON output contract.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P3: This is a bounded internal performance improvement with no intended user-visible behavior change.
  • 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 external-contributor gate does not apply to this verified repository administrator. The captured benchmark exercises the changed projection adapter and reports matching output with faster no-text processing; it does not establish live MCP or Gateway performance.

Evidence

What I checked:

  • Verified introduced change: The local pinned base-to-head diff matches the supplied introduction evidence: only mirror serialization timing changes; canonical formatting and exact equality remain intact. (src/agents/mcp-content.ts:111, c88989237527)
  • Still necessary on main and release: Both fetched main and v2026.9.4 retain eager mirror serialization before checking content blocks; neither contains this optimization. (src/agents/mcp-content.ts:111, eb8bca1326c4)
  • Production callers and preserved contracts: Configured MCP tools pass completed runtime results through this adapter; node-hosted MCP tools use the same projection. Existing materialization cases cover recovery text, mixed media, exact mirrors versus near matches, and structured-only deterministic output. These tests were inspected, not executed. (src/agents/agent-bundle-mcp-tools.materialize.test.ts:496, c88989237527)
  • Captured benchmark evidence: The supplied body, captured under sourceRevision 12d2aa4e85ee4d1ef95684f825ddd523909d1057bdc8ec5229dd430ea3c1e1d8, reports eight Node processes exercising the exported adapter. No-text loops took 15.6–17.5% less time, output hashes matched, and RSS did not improve. The body explicitly limits this to a fixed source/dependency composition, not live MCP transport or whole-Gateway performance. This satisfies the scoped request to report before/after seconds and RSS. (src/agents/AGENTS.md, c88989237527)
  • Feature-history routing: Main history includes the recovery-text change at d33ba00. GitHub commit metadata identifies steipete and shows the exact-mirror filtering patch; raw commit inspection confirms its recorded parent. Local blame and older-head comparison encountered unavailable historical blobs, so no additional introduction or continuity claim is inferred. (src/agents/mcp-content.ts:121, d33ba0015dcc)
  • Author role verified: Although the pull endpoint reports CONTRIBUTOR, the repository permission endpoint confirms steipete has admin access, consistent with the supplied maintainer classification and protected label.

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; 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 (6 earlier review cycles)
  • reviewed 2026-09-15T08:40:43.337Z sha c301468 :: blocked before merge. :: none
  • reviewed 2026-09-15T12:22:09.014Z sha 81ab116 :: blocked before merge. :: none
  • reviewed 2026-09-15T12:56:56.318Z sha 81ab116 :: blocked before merge. :: none
  • reviewed 2026-09-15T15:18:54.688Z sha 337e657 :: blocked before merge. :: none
  • reviewed 2026-09-16T14:52:48.500Z sha 337e657 :: blocked before merge. :: none
  • reviewed 2026-09-16T16:25:36.987Z sha 337e657 :: needs maintainer review before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot reopened this Sep 15, 2026
@steipete
steipete merged commit f0a8b6d into main Sep 16, 2026
186 checks passed
@steipete
steipete deleted the codex/r22-mcp-lazy-mirror branch September 16, 2026 19:03
@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: 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