Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions agent/message_sanitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ def _escape_invalid_chars_in_json_strings(raw: str) -> str:
ch = raw[i]
if in_string:
if ch == "\\" and i + 1 < n:
if ord(raw[i + 1]) < 0x20:
# A backslash immediately before a literal control char is
# NOT a valid JSON escape. Escape the backslash itself and
# let the control-char branch below escape the following
# char on the next iteration (lossless: both preserved).
out.append("\\\\")
i += 1
continue
# Already-escaped char — pass through as-is
out.append(ch)
out.append(raw[i + 1])
Expand Down
9 changes: 9 additions & 0 deletions tests/run_agent/test_repair_tool_call_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ def test_control_chars_with_trailing_comma(self):
parsed = json.loads(result)
assert "line" in parsed["msg"]

def test_backslash_before_literal_control_char(self):
"""A backslash immediately before a literal newline is not a valid
JSON escape; the repair must escape the backslash (and the control
char) instead of silently dropping all args."""
raw = '{"path": "C:\\\ntmp"}'
result = _repair_tool_call_arguments(raw, "write_file")
parsed = json.loads(result) # must parse under strict=True
assert parsed["path"] == "C:\\\ntmp" # backslash + newline preserved

Loading