feat(parsing): single-quote JSON repair + raw bracket span (#28)#45
feat(parsing): single-quote JSON repair + raw bracket span (#28)#45haasonsaas merged 1 commit intomainfrom
Conversation
- repair_json_candidates: convert single-quoted keys/values to double-quoted via convert_single_quoted_json_to_double (issue #28) - find_balanced_bracket_span: extract raw [..] or {..} when valid JSON not found, so repair can fix and parse - ROADMAP: document #28 repairs; add #25 (dynamic context) to Shipped - Test: parse_json_with_single_quotes Made-with: Cursor
PR SummaryMedium Risk Overview Extends Written by Cursor Bugbot for commit 0fed671. This will update automatically on new commits. Configure here. |
| } else { | ||
| out.push(c); | ||
| } |
There was a problem hiding this comment.
Bug: The convert_single_quoted_json_to_double function incorrectly handles backslash escape sequences in single-quoted strings, causing it to produce invalid JSON for inputs containing escapes like \n.
Severity: HIGH
Suggested Fix
In the inner loop of convert_single_quoted_json_to_double that processes single-quoted strings, ensure the escape_next flag is reset to false after processing the character that follows a backslash. This should be done in the else block and after handling an escaped single quote if the logic is to continue processing.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/parsing/llm_response.rs#L455-L457
Potential issue: The function `convert_single_quoted_json_to_double` incorrectly
processes backslash escape sequences within single-quoted strings. When a backslash is
encountered, an `escape_next` flag is set to true. However, this flag is not reset if
the following character is anything other than a single quote. This causes the flag to
persist across subsequent characters in the string. As a result, when the actual closing
single quote is found, it is incorrectly treated as an escaped character, leading to a
malformed output string that is not valid JSON (e.g., it may be missing a closing
double-quote). This will cause JSON parsing to fail for any LLM response containing
single-quoted strings with common escape sequences like `\n` or `\t`.
Did we get this right? 👍 / 👎 to inform future reviews.
| out.push('"'); | ||
| } else { | ||
| out.push(c); | ||
| } |
There was a problem hiding this comment.
Inner loop never resets escape_next for non-quote chars
High Severity
In convert_single_quoted_json_to_double, the inner for loop processing single-quoted string contents sets escape_next = true when it encounters \, but only checks/resets escape_next inside the c == '\'' branch. For any non-quote character following a backslash (e.g. \n, \t, \\), escape_next remains true indefinitely. This causes the actual closing ' to be misidentified as an escaped quote, producing corrupted output or an unclosed string. The outer loop and find_balanced_json both correctly handle this by checking escape_next at the top of the loop — the inner loop needs the same pattern.


Summary
[{'line': 9, 'issue': 'Bug'}]),convert_single_quoted_json_to_doubleconverts to valid JSON;find_balanced_bracket_spanextracts raw[..]/{..}when valid parse fails so repair can run.find_enclosing_boundary_linein context).Test plan
cargo test parsing::llm_response(includesparse_json_with_single_quotes)cargo clippy --all-targetsMade with Cursor