Skip to content

fix(code-mode): correct MCP array union declarations - #131528

Merged
steipete merged 1 commit into
mainfrom
steipete/code-mode-mcp-arrays-b4970f
Aug 28, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/code-mode-mcp-arrays-b4970f

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Closes #131476 — Code Mode MCP array-union declarations have incorrect precedence.

What Problem This Solves

Fixes an issue where agents using Code Mode's MCP declarations could be guided toward invalid arguments when an array's item schema contained a union. For example, an array of "red" or "blue" was advertised as "red" | "blue"[]: that accepts scalar "red" but rejects an array containing "red". Both API.read("mcp/<server>.d.ts") and MCP.<server>.$api() were affected.

Why This Change Was Made

Use Array<...> in the existing recursive MCP schema renderer, matching the compact schema-hint renderer's established grouping. Both declaration surfaces already share this owner, so one expression repairs enum, anyOf, oneOf, multi-type, and nested-array item grouping without a new helper, downstream workaround, or alternate path. Actual MCP schemas and validation are unchanged.

The incorrect expression was introduced in the typed MCP API work, #88678 (authored and merged by @steipete on May 31, 2026), and carried into the renderer extraction in #115459. It is also present in stable tag v2026.7.1-2; that is tagged-source evidence, not a run of the historical binary.

User Impact

Code Mode now advertises array arguments that agree with the MCP server's schema, including arrays mixing valid enum or union members. The existing namespace, call shape, permissions, and output behavior remain unchanged. No dependency, configuration, SQLite, protocol, or budget changes are included. The existing Code Mode documentation already describes these inferred declarations; this restores that documented contract.

Evidence

Production LOC: +1/-1 (net 0). Tests: +68/-0. Three files; no new production abstraction or test-only production seam.

  • Fresh regression run before the production edit: 8 failed, 19 passed. The failures showed the incorrect declarations, including the existing real QuickJS MCP test reaching its fixture executor and then failing on labels?: "red" | "blue"[].
  • After the repair: 40 focused tests passed across the namespace, real Code Mode/QuickJS MCP, and compact schema-hint suites. The table covers enum, anyOf, oneOf, multi-type, nested, simple, and object arrays on both declaration surfaces.
  • An unchanged independent TypeScript compiler/MCP SDK schema-validation probe went from 22 mismatches in 60 checks to zero mismatches in all 60. It includes valid arrays, scalar rejection, invalid element rejection, and simple/object controls.
  • Real local MCP transport proof: the SDK server advertised z.array(z.enum(["red", "blue"])); OpenClaw's stdio transport, namespace dispatcher, and built QuickJS worker completed eight exec/resume legs and six calls. Four valid arrays were accepted; scalar "red" and ["green"] were rejected by server validation. Both declaration surfaces returned the same corrected header and unchanged schema. The owned fixture process was closed and its exit verified.
  • The full scoped changed gate and pnpm build passed. No assertions, timeouts, baselines, or limits were weakened.

Focused regression command:

node scripts/run-vitest.mjs src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts src/agents/tool-schema-hints.test.ts

Scoped gate:

node scripts/check-changed.mjs -- src/agents/code-mode-mcp-api.ts src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts

Build:

pnpm build

Proof limits: the first standalone source-worker transport attempt exceeded its configured timeout. Its failed log was retained; the built-worker driver then passed with the same timeout and assertions. The source-startup cost was not diagnosed by this declaration repair. The transport proof is a real local MCP process, not an authenticated external provider or full Gateway/model-loop run. There is no Control UI change. Native Codex's freeform-source/V8 path is separate from this OpenClaw JSON-exec/QuickJS declaration owner.

AI-assisted implementation and verification with Codex; maintainer reviewed and accepted the scoped repair.

Render MCP array item types with Array<...> so API.read and MCP.$api preserve enum and union grouping. Keep advertised schemas and runtime validation unchanged.

Fixes #131476. Release context: Code Mode MCP declarations now correctly describe mixed enum and union arrays, including nested arrays.
@clawsweeper

clawsweeper Bot commented Aug 28, 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 28, 2026
@steipete
steipete marked this pull request as ready for review August 28, 2026 04:30
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 28, 2026
@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 28, 2026, 12:40 AM ET / 04:40 UTC.

ClawSweeper review

What this changes

The PR renders Code Mode MCP array declarations as Array&lt;item type> and adds tests for enum, union, nested-array, and object item schemas.

Merge readiness

✅ Ready for maintainer review

No blocking correctness or security finding. Current main still has the array-union precedence bug, and this member-owned PR repairs the shared declaration owner with focused coverage; keep it open for normal merge gating.

Priority: P2
Reviewed head: 0b0249f081a914326dd8e40795996164cb2a0a30

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) PR readiness rating was derived from proof quality, review findings, security review, and reviewer confidence.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): ClawSweeper live verification passed on the reviewed terminal surface.
Patch quality 🦀 challenger crab (6/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): ClawSweeper live verification passed on the reviewed terminal surface.
Evidence reviewed 7 items Current-main defect remains: The fetched current-main revision still appends [] directly to a recursively rendered item type, which changes the meaning of union item declarations.
Introduced repair targets the owner: The verified PR hunk changes only the array case in the recursive schema renderer from postfix brackets to grouped generic-array notation.
Caller and runtime boundary: The namespace model derives MCP parameter documentation through buildMcpParamDocs, and the execution path passes the resulting virtual API files into the Code Mode worker.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts src/agents/tool-schema-hints.test.ts

Result: PASS (completed)

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-131528-ktFn2g/target

 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › exposes MCP tools only through the MCP namespace 1221ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › preserves native MCP results through bundled materialization and the guest namespa
ce 1808ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › moves MCP guest ownership across transcript snapshots and consumes it exactly once
 1ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › rejects MCP namespace results without an owned guest projection 456ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › renames MCP namespace identifiers that would be unsafe path segments 448ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › reserved MCP tool names › renders and executes reserved MCP tool name delete safel
y 1ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › reserved MCP tool names › renders and executes reserved MCP tool name default safe
ly 0ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › reserved MCP tool names › renders and executes reserved MCP tool name return safel
y 1ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › reserved MCP tool names › renders and executes reserved MCP tool name enum safely
1ms
 ✓  agents-core  src/agents/code-mode.mcp.test.ts › Code Mode MCP namespace › reserved MCP tool names › renders and executes reserved MCP tool name class safely
 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › keeps run-owned namespace descriptors and virtual API files in sync 5
ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › preserves 'enum' item grouping in MCP API declarations 1ms
 ✓  age
… output truncated …
code-mode-namespaces.test.ts › Code Mode MCP namespace model › preserves 'simple array' item grouping in MCP API declarations 0ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › preserves 'object' item grouping in MCP API declarations 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › does not satisfy required MCP argument constructor from Object.protot
ype 2ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › does not satisfy required MCP argument toString from Object.prototype
 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › does not satisfy required MCP argument __proto__ from Object.prototyp
e 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › applies MCP argument default constructor as a safe own property 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › applies MCP argument default toString as a safe own property 0ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › applies MCP argument default __proto__ as a safe own property 0ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › keeps forbidden and JavaScript-keyword namespace identifiers callable
 and escaped 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › rejects prototype and NUL-delimited paths before calling tools 1ms
 ✓  agents-core  src/agents/code-mode-namespaces.test.ts › Code Mode MCP namespace model › does not split UTF-16 surrogate pairs in node display names at the 12
8-char boundary 1ms

 Test Files  2 passed (2)
      Tests  27 passed (27)
   Start at  04:41:25
   Duration  24.16s (transform 14.28s, setup 1000ms, import 16.36s, tests 6.43s, environment 0ms)

[test] passed 2 Vitest shards in 39.28s

Assertions:

  • PASS expect_output: Test Files

How this fits together

Code Mode converts MCP tool JSON Schemas into virtual TypeScript-style declarations that guide agent tool calls. The generated declarations are delivered to the QuickJS worker and must agree with the original MCP schema.

flowchart LR
  A[MCP tool schema] --> B[Namespace model]
  B --> C[Declaration renderer]
  C --> D[Virtual API files]
  D --> E[QuickJS Code Mode]
  E --> F[MCP tool dispatch]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source 0, Tests +68. Total +68 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 1 1 0
Tests 2 68 0 +68
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 69 1 +68

Review metrics

Metric Value Why it matters
Production versus tests production +1/-1 (net 0); tests +68/-0 The fix replaces one renderer expression and adds focused regression coverage without expanding the production design.

Technical review

Best possible solution:

Merge the single owner-level grouping correction once exact-head required checks complete; retain the two declaration-surface tests as the regression boundary.

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

Yes: current main's exact array-rendering expression visibly produces an ungrouped union followed by []; the added tests exercise the affected virtual-file and inline declaration outputs. This read-only review did not execute the tests.

Is this the best way to solve the issue?

Yes: grouping the existing recursive item rendering at the shared array case fixes every affected declaration surface without changing validation, configuration, or transport behavior.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: The declaration mismatch can steer Code Mode toward invalid MCP arguments, but MCP runtime validation remains unchanged.
  • add proof: sufficient: Contributor real behavior proof is sufficient. ClawSweeper live verification passed on the reviewed terminal surface.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦀 challenger crab.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): ClawSweeper live verification passed on the reviewed terminal surface.

Label justifications:

  • P2: The declaration mismatch can steer Code Mode toward invalid MCP arguments, but MCP runtime validation remains unchanged.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦀 challenger crab.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): ClawSweeper live verification passed on the reviewed terminal surface.
  • proof: sufficient: Contributor real behavior proof is sufficient. ClawSweeper live verification passed on the reviewed terminal surface.

Evidence

What I checked:

  • Current-main defect remains: The fetched current-main revision still appends [] directly to a recursively rendered item type, which changes the meaning of union item declarations. (src/agents/code-mode-mcp-api.ts:143, ec3a8343c5de)
  • Introduced repair targets the owner: The verified PR hunk changes only the array case in the recursive schema renderer from postfix brackets to grouped generic-array notation. (src/agents/code-mode-mcp-api.ts:143, 0b0249f081a9)
  • Caller and runtime boundary: The namespace model derives MCP parameter documentation through buildMcpParamDocs, and the execution path passes the resulting virtual API files into the Code Mode worker. (src/agents/code-mode-namespaces.ts:421, 0b0249f081a9)
  • Existing sibling uses grouped arrays: The compact schema-hint renderer already emits arrays with Array&lt;...>, supporting the same grouping rule without adding a parallel declaration path. (src/agents/tool-schema-hints.ts:358, 0b0249f081a9)
  • Boundary coverage: The new table covers enum, anyOf, oneOf, multi-type, nested, simple, and object arrays on virtual-file and inline $api() outputs; the QuickJS harness test also reads both declaration surfaces. (src/agents/code-mode-namespaces.test.ts:77, 0b0249f081a9)
  • Documented contract and implementation boundary: The documentation defines these as OpenClaw QuickJS virtual MCP declarations and explicitly distinguishes them from Codex Code Mode, so no Codex-runtime dependency gate applies. Public docs: docs/tools/code-mode.md. (docs/tools/code-mode.md:803, 0b0249f081a9)

Likely related people:

  • Peter Steinberger: Authored the original typed MCP API, the namespace refactor, and prior Code Mode MCP work visible in path history. (role: feature-history owner; confidence: high; commits: 4150c6ff8258, 86b68bc8e3c5; files: src/agents/code-mode-mcp-api.ts, src/agents/code-mode-namespaces.ts)
  • Josh Avant: Blame assigns the unchanged surrounding renderer implementation to the commit that introduced the current owner module. (role: current module introducer; confidence: high; commits: 14bc69814131; files: src/agents/code-mode-mcp-api.ts)

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

Copy link
Copy Markdown
Contributor Author

Land-ready proof for 0b0249f081a914326dd8e40795996164cb2a0a30.

The existing MCP schema renderer now uses Array<...> to preserve array-item union grouping on both API.read declarations and MCP.<server>.$api() headers. Schemas, input validation, permissions, and execution are unchanged. Production +1/-1 (net 0); tests +68/-0. The one mechanical rebase preserved the full reviewed diff byte-for-byte.

Exact-head CI run 33142017262 completed successfully. The repository CI watcher exited 0 with GREEN; no CI rerun or bypass was used. Native review artifacts validate for this exact head with zero findings. Fresh isolated Codex autoreview completed successfully with no accepted/actionable P0 findings; its private raw log is not published.

Validation commands and results:

node scripts/run-vitest.mjs src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts
# Before the production edit: 8 failed, 19 passed.

node scripts/run-vitest.mjs src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts src/agents/tool-schema-hints.test.ts
# After the repair: 40 passed.

node scripts/check-changed.mjs -- src/agents/code-mode-mcp-api.ts src/agents/code-mode-namespaces.test.ts src/agents/code-mode.mcp.test.ts
# Passed.

pnpm build
# Passed.

node --import tsx .artifacts/qa-e2e/code-mode-repair-b4970f/mcp-arrays/semantic-contract.mts
# Unchanged independent TypeScript/MCP schema probe: 22/60 mismatches before, 0/60 after.

node --import tsx .artifacts/qa-e2e/code-mode-repair-b4970f/mcp-arrays/mcp-wire-built-proof.mts
# Real local SDK stdio server + OpenClaw transport/namespace + compiled QuickJS:
# 8 exec/resume legs, 6 calls, 4 valid arrays accepted, 2 invalid inputs rejected.
# Owned fixture process exit verified.

node scripts/watch-pr-ci.mjs 131528 0b0249f081a914326dd8e40795996164cb2a0a30
# GREEN, exit 0.

The original standalone source-worker transport attempt timed out and its failed log remains retained. The compiled-worker proof passed with the same budgets and assertions. No authenticated external-provider or full Gateway/model-loop run was performed; this is a declaration-only repair with real local MCP transport proof, not UI work or native Codex runtime work. Native Codex's separate freeform/V8 contract was checked directly in upstream execute_spec.rs:16–41 and v8_init.rs:42–51 at 92cbfb4d2431bdc53dc03507aea2dc5b8e932e40.

Live rules require the GitHub Actions openclaw/ci-gate, now satisfied; no independent approval rule is enforced and no review thread is unresolved. The fresh ClawSweeper review reports no correctness/security findings and no before-merge or Rank-up actions. It also reran the focused test command successfully on this head; no re-review was requested.

Native preparation: OPENCLAW_TESTBOX=1 scripts/pr prepare-run 131528 passed hosted CI/Testbox gates and confirmed the remote branch already equals the prepared head; no extra push or code change was needed.

The native hosted-gate verifier accepted exact-head CI #33142017262 and Workflow Sanity #33141991323; no separate Testbox lease was applicable.

@steipete
steipete merged commit 7573b91 into main Aug 28, 2026
310 checks passed
@steipete
steipete deleted the steipete/code-mode-mcp-arrays-b4970f branch August 28, 2026 04:52
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 28, 2026
Render MCP array item types with Array<...> so API.read and MCP.$api preserve enum and union grouping. Keep advertised schemas and runtime validation unchanged.

Fixes openclaw#131476. Release context: Code Mode MCP declarations now correctly describe mixed enum and union arrays, including nested arrays.
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. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor 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.

[Bug]: Code Mode MCP array-union declarations have incorrect precedence

1 participant