-
Notifications
You must be signed in to change notification settings - Fork 757
fix(agent-core-v2): coerce string-typed tool arguments at the parse boundary #2175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HelloWorldU
wants to merge
3
commits into
MoonshotAI:main
Choose a base branch
from
HelloWorldU:fix/tool-args-normalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
64fa779
fix(agent-core-v2): coerce string-typed tool arguments at the parse b…
HelloWorldU cdd8f74
fix(agent-core-v2): honor string-accepting schemas and allOf composit…
HelloWorldU 1cc8d4f
fix(agent-core-v2): tighten number coercion, array identity, and warn…
HelloWorldU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Fix tool calls failing validation when a model sends numeric or boolean arguments as strings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| /** | ||
| * `tool` domain (L3) — pre-validation tool-args normalization. | ||
| * | ||
| * Model tool-call output is untrusted input: some providers emit primitive | ||
| * arguments as JSON strings (`"3"` for an integer) instead of the declared | ||
| * types. The harness absorbs that at the parse/validation boundary — the | ||
| * earliest point it controls — while keeping validation itself strict. | ||
| * | ||
| * The rules are deliberately generic and bounded so this layer stays a single | ||
| * schema-driven rule instead of growing into a catalog of model-specific | ||
| * hacks: | ||
| * - only string → integer / number / boolean coercions are attempted | ||
| * - only when the coercion is lossless and the schema does not already | ||
| * accept strings at that position | ||
| * - `$ref` nodes and ambiguous (string-accepting) schemas are left alone | ||
| * - composition keywords are consulted for type discovery only, never as a | ||
| * proof of validity; under-coercion is deliberate, since whatever this | ||
| * layer misses still faces strict validation below | ||
| * | ||
| * Whatever cannot be normalized still fails validation afterwards, where the | ||
| * error message reports what was actually received. Pure helper; no scoped | ||
| * service. | ||
| */ | ||
|
|
||
| export type CoercionTarget = 'integer' | 'number' | 'boolean'; | ||
|
|
||
| export interface ArgCoercion { | ||
| readonly path: string; | ||
| readonly received: string; | ||
| readonly expected: CoercionTarget; | ||
| } | ||
|
|
||
| export interface ArgsNormalization { | ||
| readonly args: unknown; | ||
| readonly coercions: readonly ArgCoercion[]; | ||
| } | ||
|
|
||
| const INTEGER_TEXT = /^[+-]?\d+$/; | ||
| const NUMBER_TEXT = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/; | ||
|
|
||
| type PrimitiveType = 'string' | 'integer' | 'number' | 'boolean' | 'object' | 'array' | 'null'; | ||
|
|
||
| function collectPrimitiveTypes(node: Record<string, unknown>, into: Set<PrimitiveType>): void { | ||
| const type = node['type']; | ||
| if (typeof type === 'string') into.add(type as PrimitiveType); | ||
| if (Array.isArray(type)) { | ||
| for (const entry of type) { | ||
| if (typeof entry === 'string') into.add(entry as PrimitiveType); | ||
| } | ||
| } | ||
| for (const keyword of ['anyOf', 'oneOf', 'allOf'] as const) { | ||
| const branches = node[keyword]; | ||
| if (Array.isArray(branches)) { | ||
| for (const branch of branches) { | ||
| if (branch !== null && typeof branch === 'object' && !Array.isArray(branch)) { | ||
| collectPrimitiveTypes(branch as Record<string, unknown>, into); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function isSchemaNode(value: unknown): value is Record<string, unknown> { | ||
| return value !== null && typeof value === 'object' && !Array.isArray(value); | ||
| } | ||
|
|
||
| function isUnconstrained(node: Record<string, unknown>): boolean { | ||
| return ( | ||
| node['type'] === undefined && | ||
| node['const'] === undefined && | ||
| node['enum'] === undefined && | ||
| node['$ref'] === undefined | ||
| ); | ||
| } | ||
|
|
||
| function stringAlreadyValid(node: Record<string, unknown>, raw: string): boolean { | ||
| if (node['const'] === raw) return true; | ||
| const enumValues = node['enum']; | ||
| if (Array.isArray(enumValues) && enumValues.includes(raw)) return true; | ||
| for (const keyword of ['anyOf', 'oneOf'] as const) { | ||
| const branches = node[keyword]; | ||
| if (!Array.isArray(branches)) continue; | ||
| for (const branch of branches) { | ||
| if (!isSchemaNode(branch)) continue; | ||
| if (isUnconstrained(branch) || stringAlreadyValid(branch, raw)) return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function coerceString( | ||
| raw: string, | ||
| expected: ReadonlySet<PrimitiveType>, | ||
| ): { readonly value: number | boolean; readonly target: CoercionTarget } | undefined { | ||
| if (expected.has('integer') && INTEGER_TEXT.test(raw)) { | ||
| const parsed = Number(raw); | ||
| if (Number.isSafeInteger(parsed)) return { value: parsed, target: 'integer' }; | ||
| return undefined; | ||
| } | ||
| if (expected.has('number') && NUMBER_TEXT.test(raw)) { | ||
| const parsed = Number(raw); | ||
| const lossless = INTEGER_TEXT.test(raw) ? Number.isSafeInteger(parsed) : Number.isFinite(parsed); | ||
| if (lossless) return { value: parsed, target: 'number' }; | ||
| return undefined; | ||
| } | ||
| if (expected.has('boolean') && (raw === 'true' || raw === 'false')) { | ||
| return { value: raw === 'true', target: 'boolean' }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export function describeReceivedValue(value: unknown): string { | ||
| if (typeof value === 'string') return `string ${JSON.stringify(value)}`; | ||
| if (typeof value === 'number') return `number ${String(value)}`; | ||
| if (typeof value === 'boolean') return `boolean ${String(value)}`; | ||
| if (value === null) return 'null'; | ||
| if (value === undefined) return 'undefined'; | ||
| if (Array.isArray(value)) return 'array'; | ||
| return 'object'; | ||
| } | ||
|
|
||
| function normalizeValue( | ||
| node: Record<string, unknown>, | ||
| value: unknown, | ||
| path: string, | ||
| coercions: ArgCoercion[], | ||
| ): unknown { | ||
| if (typeof value === 'string') { | ||
| const expected = new Set<PrimitiveType>(); | ||
| collectPrimitiveTypes(node, expected); | ||
| if (!expected.has('string') && !stringAlreadyValid(node, value)) { | ||
| const coerced = coerceString(value, expected); | ||
| if (coerced !== undefined) { | ||
| coercions.push({ | ||
| path, | ||
| received: describeReceivedValue(value), | ||
| expected: coerced.target, | ||
| }); | ||
| return coerced.value; | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| if (Array.isArray(value)) { | ||
| const items = node['items']; | ||
| if (items !== null && typeof items === 'object' && !Array.isArray(items)) { | ||
| const itemSchema = items as Record<string, unknown>; | ||
| if (itemSchema['$ref'] === undefined) { | ||
| let changed: unknown[] | undefined; | ||
| for (let index = 0; index < value.length; index += 1) { | ||
| const normalized = normalizeValue( | ||
| itemSchema, | ||
| value[index], | ||
| `${path}/${String(index)}`, | ||
| coercions, | ||
| ); | ||
| if (normalized !== value[index]) { | ||
| changed ??= [...value]; | ||
| changed[index] = normalized; | ||
| } | ||
| } | ||
| return changed ?? value; | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| if (value !== null && typeof value === 'object') { | ||
| const properties = node['properties']; | ||
| if (properties !== null && typeof properties === 'object' && !Array.isArray(properties)) { | ||
| const record = value as Record<string, unknown>; | ||
| let changed: Record<string, unknown> | undefined; | ||
| for (const [key, child] of Object.entries(properties)) { | ||
| if (child === null || typeof child !== 'object' || Array.isArray(child)) continue; | ||
| const childSchema = child as Record<string, unknown>; | ||
| if (childSchema['$ref'] !== undefined) continue; | ||
| if (!(key in record)) continue; | ||
| const normalized = normalizeValue( | ||
| childSchema, | ||
| record[key], | ||
| `${path}/${key.replace(/~/g, '~0').replace(/\//g, '~1')}`, | ||
| coercions, | ||
| ); | ||
| if (normalized !== record[key]) { | ||
| changed ??= { ...record }; | ||
| changed[key] = normalized; | ||
| } | ||
| } | ||
| return changed ?? value; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| export function normalizeToolArgs( | ||
| schema: Record<string, unknown>, | ||
| args: unknown, | ||
| ): ArgsNormalization { | ||
| const coercions: ArgCoercion[] = []; | ||
| if (schema['$ref'] !== undefined) return { args, coercions }; | ||
| const normalized = normalizeValue(schema, args, '', coercions); | ||
| return { args: normalized, coercions }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.