fix(ai): decode JSON-encoded tool arguments for Records, typeless unions and protocol-validated schemas
Fixes #6271.
What changes
packages/ai/src/utils/validation.ts: decodeJsonEncodedStructures() now covers the three shapes the #6266 pass missed:
- Record values: the object branch resolves a key's schema through
patternPropertiesas well asproperties/additionalProperties, so{k: '["a"]'}againstRecord(String, Array(String))decodes to{k: ["a"]}instead of validating as['["a"]']. - Typeless unions: when a schema carries no top-level
type, decoding recurses into theanyOf/oneOfmembers that accept the value's kind, so JSON text nested inside e.g.Union([Array(Array(String)), Null])is decoded. - Protocol-validated (modern MCP) schemas:
validateToolArguments()now runs the decode before the protocol-validator early return, so those tools no longer keep the pre-#6266 behaviour for JSON-encoded arrays/objects.
packages/ai/test/validation.test.ts: table-driven regression tests for all three shapes (senpi#6271). All three failed before the fix for the intended reason (see below) and pass after it.
Test evidence
RED (before fix, test/validation.test.ts -t "6271"): 3 failed / 18 skipped — Record got { values: { k: ['["a"]'] } }, the union kept the inner '["a"]' string, the protocol-validated call threw Validation failed.
GREEN (after fix, full file): 21 passed / 0 failed.
Static checks: oxlint --deny-warnings 0 warnings, bun fmt --check clean, bun check --no-script no type errors on the two touched files, and the repo pre-commit hook (fmt + oxlint --fix + npm run check incl. browser-smoke) passed on commit.
Change fragment
changes/unreleased/<this PR number>.md follows in a second push (named for the PR number per changes/unreleased/README.md): tracker block for packages/ai/src/utils/changes.md correcting the #6266 MCP coverage sentence — the core fix covers MCP tools whose schemas no extension owns only for legacy MCP schemas; protocol-validated modern MCP schemas now get the decode too — plus a Fixed changelog bullet for @earendil-works/pi-ai.
Summary by cubic
Fixes #6271 by decoding JSON-encoded tool arguments in the three shapes the #6266 fix missed: Record values under patternProperties, JSON text nested in typeless unions, and protocol-validated modern MCP schemas.
- Record value schemas are now resolved through
patternPropertiesas well asproperties/additionalProperties. - Schemas without a top-level
typenow recurse intoanyOf/oneOfmembers that accept the value's kind. - The decode now runs before the protocol-validator early return, so protocol-validated MCP tools unescape JSON-encoded arrays and objects too.
- Adds regression tests covering all three shapes and a
changes/unreleased/6503.mdfragment logging the fix.