fix: normalize malformed Bash tool arguments from OpenAI-compatible providers (#385)
* fix: normalize malformed Bash tool arguments from OpenAI-compatible providers
* fix: keep invalid Bash tool args from becoming commands
* fix: preserve malformed Bash JSON literals
* test: stabilize rebased PR 385 checks
* test: isolate provider profile env assertions
* fix: extend tool argument normalization to all tools and harden edge cases
- Extend STRING_ARGUMENT_TOOL_FIELDS to normalize Read, Write, Edit,
Glob, and Grep plain-string arguments (fixes "Invalid tool parameters"
errors reported by VennDev)
- Normalize streaming Bash args regardless of finish_reason, not only
when finish_reason is 'tool_calls'
- Broaden isLikelyStructuredObjectLiteral to catch malformed object-shaped
strings like {command:"pwd"} and {'command':'pwd'} (fixes CR2 from
Vasanthdev2004)
- Apply blank/object-literal guard to all tools, not just Bash
- Extract duplicated JSON repair suffix combinations into shared constant
- Add 32 isolated unit tests for toolArgumentNormalization
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: skip streaming normalization on finish_reason length
Truncated tool calls (finish_reason: 'length') now preserve the raw
buffer instead of normalizing into executable commands, preventing
incomplete commands from becoming runnable.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: comprehensive tool argument normalization hardening
- Remove all { raw: ... } returns that caused InputValidationError with
z.strictObject schemas — return {} instead for clean Zod errors
- Extend normalizeAtStop buffering to all mapped tools (Read, Write,
Edit, Glob, Grep) so streaming paths also get normalized
- Make repairPossiblyTruncatedObjectJson generic — repair any valid
JSON object, not just ones with a command field
- Export hasToolFieldMapping for streaming normalizeAtStop decision
- Skip normalization on finish_reason: length to preserve raw truncated
buffer
- Update all test expectations to match new behavior
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
180
src/services/api/toolArgumentNormalization.test.ts
Normal file
180
src/services/api/toolArgumentNormalization.test.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import { normalizeToolArguments } from './toolArgumentNormalization'
|
||||
|
||||
describe('normalizeToolArguments', () => {
|
||||
describe('Bash tool', () => {
|
||||
test('wraps plain string into { command }', () => {
|
||||
expect(normalizeToolArguments('Bash', 'pwd')).toEqual({ command: 'pwd' })
|
||||
})
|
||||
|
||||
test('wraps multi-word command', () => {
|
||||
expect(normalizeToolArguments('Bash', 'ls -la /tmp')).toEqual({
|
||||
command: 'ls -la /tmp',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments('Bash', '{"command":"echo hi"}'),
|
||||
).toEqual({ command: 'echo hi' })
|
||||
})
|
||||
|
||||
test('returns empty object for blank string', () => {
|
||||
expect(normalizeToolArguments('Bash', '')).toEqual({})
|
||||
expect(normalizeToolArguments('Bash', ' ')).toEqual({})
|
||||
})
|
||||
|
||||
test('returns parsed blank for JSON-encoded blank string', () => {
|
||||
expect(normalizeToolArguments('Bash', '""')).toEqual('')
|
||||
expect(normalizeToolArguments('Bash', '" "')).toEqual(' ')
|
||||
})
|
||||
|
||||
test('returns empty object for malformed structured object literal', () => {
|
||||
expect(normalizeToolArguments('Bash', '{ "command": "pwd"')).toEqual({})
|
||||
})
|
||||
|
||||
test.each([
|
||||
['{command:"pwd"}'],
|
||||
["{'command':'pwd'}"],
|
||||
['{command: pwd}'],
|
||||
])(
|
||||
'returns empty object for malformed object-shaped string %s (does not wrap into command)',
|
||||
(input) => {
|
||||
expect(normalizeToolArguments('Bash', input)).toEqual({})
|
||||
},
|
||||
)
|
||||
|
||||
test.each([
|
||||
['false', false],
|
||||
['null', null],
|
||||
['[]', [] as unknown[]],
|
||||
['0', 0],
|
||||
['true', true],
|
||||
['123', 123],
|
||||
])(
|
||||
'preserves JSON literal %s as-is (does not wrap into command)',
|
||||
(input, expected) => {
|
||||
expect(normalizeToolArguments('Bash', input)).toEqual(expected)
|
||||
},
|
||||
)
|
||||
|
||||
test('wraps JSON-encoded string into { command }', () => {
|
||||
expect(normalizeToolArguments('Bash', '"pwd"')).toEqual({
|
||||
command: 'pwd',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('undefined arguments', () => {
|
||||
test('returns empty object for undefined', () => {
|
||||
expect(normalizeToolArguments('Bash', undefined)).toEqual({})
|
||||
expect(normalizeToolArguments('UnknownTool', undefined)).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Read tool', () => {
|
||||
test('wraps plain string into { file_path }', () => {
|
||||
expect(normalizeToolArguments('Read', '/home/user/file.txt')).toEqual({
|
||||
file_path: '/home/user/file.txt',
|
||||
})
|
||||
})
|
||||
|
||||
test('wraps JSON-encoded string into { file_path }', () => {
|
||||
expect(normalizeToolArguments('Read', '"/home/user/file.txt"')).toEqual({
|
||||
file_path: '/home/user/file.txt',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments('Read', '{"file_path":"/tmp/f.txt","limit":10}'),
|
||||
).toEqual({ file_path: '/tmp/f.txt', limit: 10 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Write tool', () => {
|
||||
test('wraps plain string into { file_path }', () => {
|
||||
expect(normalizeToolArguments('Write', '/tmp/out.txt')).toEqual({
|
||||
file_path: '/tmp/out.txt',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments(
|
||||
'Write',
|
||||
'{"file_path":"/tmp/out.txt","content":"hello"}',
|
||||
),
|
||||
).toEqual({ file_path: '/tmp/out.txt', content: 'hello' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Edit tool', () => {
|
||||
test('wraps plain string into { file_path }', () => {
|
||||
expect(normalizeToolArguments('Edit', '/tmp/edit.ts')).toEqual({
|
||||
file_path: '/tmp/edit.ts',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments(
|
||||
'Edit',
|
||||
'{"file_path":"/tmp/f.ts","old_string":"a","new_string":"b"}',
|
||||
),
|
||||
).toEqual({ file_path: '/tmp/f.ts', old_string: 'a', new_string: 'b' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Glob tool', () => {
|
||||
test('wraps plain string into { pattern }', () => {
|
||||
expect(normalizeToolArguments('Glob', '**/*.ts')).toEqual({
|
||||
pattern: '**/*.ts',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments('Glob', '{"pattern":"*.js","path":"/src"}'),
|
||||
).toEqual({ pattern: '*.js', path: '/src' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Grep tool', () => {
|
||||
test('wraps plain string into { pattern }', () => {
|
||||
expect(normalizeToolArguments('Grep', 'TODO')).toEqual({
|
||||
pattern: 'TODO',
|
||||
})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments('Grep', '{"pattern":"fixme","path":"/src"}'),
|
||||
).toEqual({ pattern: 'fixme', path: '/src' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('unknown tools', () => {
|
||||
test('returns empty object for plain string (no known field mapping)', () => {
|
||||
expect(normalizeToolArguments('UnknownTool', 'some value')).toEqual({})
|
||||
})
|
||||
|
||||
test('passes through structured JSON object', () => {
|
||||
expect(
|
||||
normalizeToolArguments('UnknownTool', '{"key":"val"}'),
|
||||
).toEqual({ key: 'val' })
|
||||
})
|
||||
|
||||
test('preserves JSON literals as-is', () => {
|
||||
expect(normalizeToolArguments('UnknownTool', 'false')).toEqual(false)
|
||||
expect(normalizeToolArguments('UnknownTool', 'null')).toEqual(null)
|
||||
expect(normalizeToolArguments('UnknownTool', '[]')).toEqual([])
|
||||
})
|
||||
|
||||
test('returns parsed string for JSON-encoded string on unknown tools', () => {
|
||||
expect(normalizeToolArguments('UnknownTool', '"hello"')).toEqual(
|
||||
'hello',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user