Skip to content

fix(ai): decode JSON-encoded array/object tool arguments before conversion - #6266

Merged
code-yeongyu merged 2 commits into
mainfrom
fix/6265-parallel-tool-array-args
Nov 9, 2026
Merged

code-yeongyu merged 2 commits into
mainfrom
fix/6265-parallel-tool-array-args

Conversation

@code-yeongyu

@code-yeongyu code-yeongyu commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

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 1234211):

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.Convert wrap)

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 parallel generate_image-shaped calls through agentLoop with 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.

Written for commit 278d1b4. Summary will update on new commits.

View guided diff Turn on auto-fix

…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
@code-yeongyu code-yeongyu added the run-full-ci Run the full macOS/Windows CI set on this PR label Nov 9, 2026

@surlymochan surlymochan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

  1. Mutation safety. decodeJsonEncodedStructures rewrites values in place (value[index] = ..., record[key] = ...), which would be a problem if it ran on the provider's arguments — but validateToolArguments deep-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.
  2. The protocolValidator early return. Tools on that path skip the decode pass entirely, but that is consistent by design: tool-argument-validator.ts says "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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-full-ci Run the full macOS/Windows CI set on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generate_image: reference_image_paths arrives as a JSON string (not an array) for every parallel call after the first

2 participants