Skip to content

Commit 7f50281

Browse files
committed
fix: extend coercion to boolean strings and stringified JSON
Align with OpenCode's approach (anomalyco/opencode#7512): - Numeric strings: "3" → 3 - Boolean strings: "true"/"false" → true/false - Stringified JSON: "[\"a\"]" → ["a"], "{...}" → {...} Still does NOT coerce null, preserving rejection of invalid args like Write({ content: null }). Closes MoonshotAI#2118
1 parent 2a83119 commit 7f50281

3 files changed

Lines changed: 98 additions & 29 deletions

File tree

‎packages/agent-core-v2/src/tool/args-validator.ts‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,45 @@ export function compileToolArgsValidator(schema: Record<string, unknown>): ToolA
8484
}
8585

8686
/**
87-
* Recursively convert string values that look like numbers into actual
88-
* numbers. Models sometimes serialize numeric tool parameters as strings
89-
* (e.g. `"3"` instead of `3`). Unlike AJV's `coerceTypes`, this only
90-
* handles the string→number case and leaves `null`, booleans, and
91-
* non-numeric strings untouched.
87+
* Recursively coerce string values to their likely intended types.
88+
* Models sometimes serialize typed parameters as strings:
89+
* - `"3"` instead of `3` (number)
90+
* - `"true"` instead of `true` (boolean)
91+
* - `"[\"a\"]"` instead of `["a"]` (JSON array/object)
92+
*
93+
* Unlike AJV's `coerceTypes`, this does NOT coerce `null` → `''` or
94+
* `null` → `0`, so invalid args like `Write({ content: null })` are
95+
* still correctly rejected.
9296
*/
93-
function coerceNumericStrings(value: JsonType): JsonType {
97+
function coerceStringValues(value: JsonType): JsonType {
9498
if (typeof value === 'string') {
95-
if (value.trim() === '') return value;
96-
const num = Number(value);
99+
const trimmed = value.trim();
100+
if (trimmed === '') return value;
101+
102+
// Stringified JSON array/object — try JSON.parse
103+
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
104+
try {
105+
return JSON.parse(trimmed) as JsonType;
106+
} catch {
107+
return value;
108+
}
109+
}
110+
111+
// Boolean strings
112+
if (trimmed === 'true') return true;
113+
if (trimmed === 'false') return false;
114+
115+
// Numeric strings
116+
const num = Number(trimmed);
97117
return Number.isFinite(num) ? num : value;
98118
}
99119
if (Array.isArray(value)) {
100-
return value.map(coerceNumericStrings);
120+
return value.map(coerceStringValues);
101121
}
102122
if (typeof value === 'object' && value !== null) {
103123
const result: JsonObject = {};
104124
for (const [key, child] of Object.entries(value)) {
105-
result[key] = coerceNumericStrings(child);
125+
result[key] = coerceStringValues(child);
106126
}
107127
return result;
108128
}
@@ -114,11 +134,12 @@ export function validateToolArgs(validator: ToolArgsValidator, args: JsonType):
114134
return null;
115135
}
116136

117-
// Retry with numeric strings coerced to numbers — models sometimes emit
118-
// "3" instead of 3 for integer/number parameters.
119-
const coerced = coerceNumericStrings(args);
137+
// Retry with string values coerced to their likely intended types —
138+
// models sometimes emit "3" instead of 3, "true" instead of true,
139+
// or "[...]" instead of [...] for typed parameters.
140+
const coerced = coerceStringValues(args);
120141
if (coerced !== args && validator(coerced)) {
121-
// Mutate the original args in place so downstream tools see numbers.
142+
// Mutate the original args in place so downstream tools see correct types.
122143
if (typeof args === 'object' && args !== null && typeof coerced === 'object' && coerced !== null) {
123144
for (const [key, val] of Object.entries(coerced)) {
124145
(args as JsonObject)[key] = val;

‎packages/agent-core-v2/test/tool/args-validator.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,36 @@ describe('args-validator (Ajv, format support)', () => {
5555
expect(validate(schema, { line_offset: '3', path: 'main.go' })).toBeNull();
5656
// Non-numeric strings should still fail.
5757
expect(validate(schema, { line_offset: 'abc', path: 'main.go' })).toContain('must be integer');
58+
// Garbage strings like "%" or "a" should still fail.
59+
expect(validate(schema, { line_offset: '%', path: 'main.go' })).toContain('must be integer');
5860
});
5961

60-
it('does NOT coerce null or booleans (unlike AJV coerceTypes)', () => {
62+
it('coerces boolean strings on validation failure', () => {
63+
const schema = {
64+
type: 'object',
65+
properties: {
66+
replaceAll: { type: 'boolean' },
67+
},
68+
};
69+
expect(validate(schema, { replaceAll: 'true' })).toBeNull();
70+
expect(validate(schema, { replaceAll: 'false' })).toBeNull();
71+
});
72+
73+
it('coerces stringified JSON arrays/objects on validation failure', () => {
74+
const schema = {
75+
type: 'object',
76+
properties: {
77+
todos: { type: 'array', items: { type: 'string' } },
78+
meta: { type: 'object' },
79+
},
80+
};
81+
expect(validate(schema, { todos: '["a","b"]' })).toBeNull();
82+
expect(validate(schema, { meta: '{"key":"val"}' })).toBeNull();
83+
// Invalid JSON string should still fail.
84+
expect(validate(schema, { todos: '[broken' })).toContain('must be array');
85+
});
86+
87+
it('does NOT coerce null (unlike AJV coerceTypes)', () => {
6188
const schema = {
6289
type: 'object',
6390
properties: {

‎packages/agent-core/src/tools/args-validator.ts‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,45 @@ export function compileToolArgsValidator(schema: Record<string, unknown>): ToolA
7979
}
8080

8181
/**
82-
* Recursively convert string values that look like numbers into actual
83-
* numbers. Models sometimes serialize numeric tool parameters as strings
84-
* (e.g. `"3"` instead of `3`). Unlike AJV's `coerceTypes`, this only
85-
* handles the string→number case and leaves `null`, booleans, and
86-
* non-numeric strings untouched.
82+
* Recursively coerce string values to their likely intended types.
83+
* Models sometimes serialize typed parameters as strings:
84+
* - `"3"` instead of `3` (number)
85+
* - `"true"` instead of `true` (boolean)
86+
* - `"[\"a\"]"` instead of `["a"]` (JSON array/object)
87+
*
88+
* Unlike AJV's `coerceTypes`, this does NOT coerce `null` → `''` or
89+
* `null` → `0`, so invalid args like `Write({ content: null })` are
90+
* still correctly rejected.
8791
*/
88-
function coerceNumericStrings(value: JsonType): JsonType {
92+
function coerceStringValues(value: JsonType): JsonType {
8993
if (typeof value === 'string') {
90-
if (value.trim() === '') return value;
91-
const num = Number(value);
94+
const trimmed = value.trim();
95+
if (trimmed === '') return value;
96+
97+
// Stringified JSON array/object — try JSON.parse
98+
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
99+
try {
100+
return JSON.parse(trimmed) as JsonType;
101+
} catch {
102+
return value;
103+
}
104+
}
105+
106+
// Boolean strings
107+
if (trimmed === 'true') return true;
108+
if (trimmed === 'false') return false;
109+
110+
// Numeric strings
111+
const num = Number(trimmed);
92112
return Number.isFinite(num) ? num : value;
93113
}
94114
if (Array.isArray(value)) {
95-
return value.map(coerceNumericStrings);
115+
return value.map(coerceStringValues);
96116
}
97117
if (typeof value === 'object' && value !== null) {
98118
const result: JsonObject = {};
99119
for (const [key, child] of Object.entries(value)) {
100-
result[key] = coerceNumericStrings(child);
120+
result[key] = coerceStringValues(child);
101121
}
102122
return result;
103123
}
@@ -109,11 +129,12 @@ export function validateToolArgs(validator: ToolArgsValidator, args: JsonType):
109129
return null;
110130
}
111131

112-
// Retry with numeric strings coerced to numbers — models sometimes emit
113-
// "3" instead of 3 for integer/number parameters.
114-
const coerced = coerceNumericStrings(args);
132+
// Retry with string values coerced to their likely intended types —
133+
// models sometimes emit "3" instead of 3, "true" instead of true,
134+
// or "[...]" instead of [...] for typed parameters.
135+
const coerced = coerceStringValues(args);
115136
if (coerced !== args && validator(coerced)) {
116-
// Mutate the original args in place so downstream tools see numbers.
137+
// Mutate the original args in place so downstream tools see correct types.
117138
if (typeof args === 'object' && args !== null && typeof coerced === 'object' && coerced !== null) {
118139
for (const [key, val] of Object.entries(coerced)) {
119140
(args as JsonObject)[key] = val;

0 commit comments

Comments
 (0)