Repository navigation
fix(ai): decode JSON-encoded array/object tool arguments before conversion - #6266
Conversation
…rsion In a turn of parallel generate_image calls the later calls carried reference_image_paths as the array's JSON text. Value.Convert wrapped that text into a one-element array that passed validation, so the tool ran with the JSON text as a path (ENOENT). Decode such text to the schema-declared array/object before conversion. Fixes #6265
surlymochan
left a comment
There was a problem hiding this comment.
The fix addresses the reported failure at the right layer. decodeJsonEncodedStructures runs before Value.Convert, is schema-directed (only where the schema rejects strings), and recurses through properties, additionalProperties, and array items — so the later parallel generate_image calls' '["/refs/c.png"]' text is decoded to the declared array instead of being wrapped into a one-element array that validates and then ENOENTs as a path. The agent-loop test reproduces the observed wire exactly (call 1 a real array, calls 2–4 JSON text) and asserts all four calls run with their paths.
I verified the two things I was most suspicious of against the head ref (278d1b44):
- Mutation safety.
decodeJsonEncodedStructuresrewrites values in place (value[index] = ...,record[key] = ...), which would be a problem if it ran on the provider's arguments — butvalidateToolArgumentsdeep-clones first (structuredClone(toolCall.arguments),validation.ts:436), so the "does not rewrite the provider's tool call arguments" test holds for nested structures too, not just the top-level string it checks. - The
protocolValidatorearly return. Tools on that path skip the decode pass entirely, but that is consistent by design:tool-argument-validator.tssays "A protocol may own validation without permitting local argument coercion", and decoding is a coercion — so protocols that registered their own validator are deliberately out of scope here.
One boundary worth confirming: a top-level value that is double-encoded ('"[\"/a.png\"]"', JSON text of a JSON string) starts with " rather than [/{, so parseJsonStructure leaves it alone and Convert still wraps it — the same ENOENT one level down. The observed wire was single-encoded, so this is out of scope for #6265, but please confirm the single-level scope is intentional rather than a case the decode should also handle.
Fixes #6265
Root cause
In the observed turn, the later
generate_imagecalls carriedreference_image_pathsas the array's JSON text ('["/abs/ref.png"]') instead of an array.validateToolArguments(packages/ai/src/utils/validation.ts) runsValue.Convertbefore the schema check. For an array schema,Value.Convertwraps 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/main1234211):Fix
validateToolArgumentsnow runs a schema-directeddecodeJsonEncodedStructures()pass beforeValue.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,additionalPropertiesand array items. These cases are left unchanged:$ref)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.ts18/18 andagent-loop.test.ts+tool-argument-purity.test.ts70/70 pass. The roottsgo --noEmitexits 0, and the changelog gate printsPASS - 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
Written for commit 278d1b4. Summary will update on new commits.