…xtraction
This commit fixes two related issues in JSON validation:
1. **Single-quote normalization**: AI responses sometimes contain JavaScript
syntax with single quotes (e.g., `['*']`) instead of valid JSON with double
quotes (`["*"]`). Added `normalizeJsonQuotes()` function to convert single
quotes to double quotes when extracting JSON.
2. **Incorrect fragment extraction**: The single-backtick pattern was extracting
inline code from within JSON objects (e.g., extracting `['*']` from
`{"text": "... `['*']` ..."}` instead of preserving the full object).
Fixed by adding anchors (`^...$`) to only match when the entire input is
a single-backtick code block.
**Changes:**
- Added `normalizeJsonQuotes()` helper function to convert JavaScript array/object
syntax to valid JSON
- Integrated normalization at all JSON extraction points in `cleanSchemaResponse()`
- Fixed single-backtick pattern to prevent extraction from within larger structures
- Added comprehensive test suite (17 tests) to verify the fix
**Impact:**
- Fixes "Unexpected token '''" errors when validating attempt_completion results
- Preserves full JSON objects instead of extracting embedded code fragments
- All 1114 existing tests continue to pass
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Summary
This PR fixes two related issues in JSON validation that were causing
attempt_completionresults to fail with "Unexpected token '''" errors:Problem
When AI returned
attempt_completionwith results like:{ "text": "When `allowedTools` contains `['*']`, all tools are enabled", "intent": "comment_reply" }The system would incorrectly extract
['*'](14 chars) from the inline code instead of preserving the full JSON object (1060+ chars), then fail validation with:Solution
1. Added
normalizeJsonQuotes()FunctionConverts JavaScript array/object syntax to valid JSON by replacing single quotes with double quotes:
['*']→["*"]{'key': 'value'}→{"key": "value"}2. Fixed Single-Backtick Pattern
Changed from
/\([{[][\s\S]?[}]])`/to/^`([{[][\s\S]?[}]])`$/` with anchors to:`{"test": "value"}`{"text": "... `['*']` ..."}Testing
New Test Suite
Added
single-quote-json-bug.test.jswith 17 comprehensive tests:Test Results
Impact
Files Changed
npm/src/agent/schemaUtils.js: Added normalization and fixed extractionnpm/tests/unit/single-quote-json-bug.test.js: Comprehensive test coverage🤖 Generated with Claude Code