fix(ai): decode JSON-encoded array/object tool arguments before conversion
Fixes #6265
Root cause
In the observed turn, the later generate_image calls carried reference_image_paths as the array's JSON text ('["/abs/ref.png"]') instead of an array. validateToolArguments (packages/ai/src/utils/validation.ts) runs Value.Convert before the schema check. For an array schema, Value.Convert wraps a non-array value into a one-element array, so the text became ['["/abs/ref.png"]']. That passes validation (an array of non-empty strings), and the tool resolved the JSON text as one relative path, which is the exact ENOENT in the issue. The provider stream adapters parse each tool call's JSON independently. The difference between the first call and the later ones is in how each call's argument was encoded, and the shared decoder turned a recoverable encoding into a wrong value instead of decoding it.
Measured before the fix (RED, both on origin/main 1234211db5):
validation.test.ts: gives every parallel call in a turn its declared array
- "/refs/c.png",
+ "[\"/refs/c.png\"]",
agent-loop.test.ts: runs every parallel call with its declared array argument (#6265)
+ "[\"/refs/d.png\",\"/refs/e.png\"]",
Fix
validateToolArguments now runs a schema-directed decodeJsonEncodedStructures() pass before Value.Convert, for TypeBox and plain JSON schemas alike, so MCP tools are covered as well as builtin tools. A string is replaced only when its schema declares an array or object, does not accept a string, and the text parses to that declared array or object. The pass recurses through properties, additionalProperties and array items. These cases are left unchanged:
- string-typed fields, and unions that accept a string
- schemas whose type is not stated (const/enum/
$ref) - text that does not parse to the declared structure (a bare single path keeps the existing
Value.Convertwrap)
The provider's tool call is not mutated, because the pass runs on the existing structuredClone.
Tests
packages/ai/test/validation.test.ts: covers a 4-call turn (call 1 with an array, calls 2-4 with JSON text), plain JSON schemas, nested arrays and objects, the strings left alone above, and a check that the provider's arguments are not rewritten.packages/agent/test/agent-loop.test.ts: runs 4 parallelgenerate_image-shaped calls throughagentLoopwith parallel execution and asserts that every call runs with its declared paths and none errors.
After the fix: validation.test.ts 18/18 and agent-loop.test.ts + tool-argument-purity.test.ts 70/70 pass. The root tsgo --noEmit exits 0, and the changelog gate prints PASS - changes.md coverage complete (1 production path(s) covered).
Summary by cubic
Fixes parallel tool calls failing when an array or object argument arrives as its JSON text: the text was wrapped into a one-element array that passed validation, and the tool then resolved the text as a path, causing ENOENT.
Bug Fixes
- Decodes JSON-encoded array and object tool arguments back to their declared structure before conversion, for TypeBox and plain JSON schemas (MCP tools included).
- Only decodes where the schema declares an array or object, rejects strings, and the text parses; string-typed fields, untyped schemas, and single paths are left unchanged.
- Runs on a clone, so the provider's original tool call arguments are not rewritten.