Skip to content

fix(agent-core-v2): coerce string-typed tool arguments at the parse boundary#2175

Open
HelloWorldU wants to merge 3 commits into
MoonshotAI:mainfrom
HelloWorldU:fix/tool-args-normalization
Open

fix(agent-core-v2): coerce string-typed tool arguments at the parse boundary#2175
HelloWorldU wants to merge 3 commits into
MoonshotAI:mainfrom
HelloWorldU:fix/tool-args-normalization

Conversation

@HelloWorldU

@HelloWorldU HelloWorldU commented Jul 25, 2026

Copy link
Copy Markdown

Related Issue

Resolve #2118

Problem

Some third-party models emit tool-call arguments as JSON strings ("3") instead of the declared JSON types (3) — reported in the linked issue on 0.29.0 with Qwen. The arguments are decoded faithfully and validated strictly, so the call is rejected with Invalid args for tool ... and the turn stalls, even though the model's intent is unambiguous.

What changed

The fix sits at the tool-call parse boundary — the earliest point the harness controls — and keeps validation itself strict. Two moves, as discussed in the issue:

  1. A normalization layer before validation. It is generic and bounded by design, so it stays a single schema-driven rule rather than growing into a catalog of model-specific hacks: only lossless string → integer / number / boolean coercions, only when the schema does not already accept strings at that position; $ref nodes and ambiguous schemas are left untouched.

  2. A warning that keeps the provider's violation visible. Coerced calls succeed, but the tool result carries a model-visible note (rendered to the model at projection time, never shown in UIs) describing exactly what was coerced — the non-conforming output is absorbed without being silently hidden.

When coercion cannot fix the arguments, the rejection now says what actually arrived (e.g. /line_offset must be integer (received string "3")) so the model can self-correct, and duplicate type errors from union branches are deduplicated.

Scope: the v2 engine only, which is what the current CLI runs on. The v1 engine has an isomorphic parse/validate path; happy to mirror the change there if the team wants both covered.

Tests: unit coverage for the normalization rules and the new error wording, plus executor integration tests covering coercion with the warning note, note merging with the tool's own note, honest rejection, and clean passthrough. Roughly half of the +571/−11 diff is tests.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1cc8d4f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@HelloWorldU

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 64fa779d34

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/tool/args-normalize.ts
Comment thread packages/agent-core-v2/src/tool/args-normalize.ts Outdated
Comment thread packages/agent-core-v2/src/agent/toolExecutor/toolExecutorService.ts Outdated
Comment thread packages/agent-core-v2/test/tool/args-normalize.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Read tool rejects an integer line_offset with "must be integer" validation error

1 participant