1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Deterministic JSON argument repair for malformed tool-call inputs.
//!
//! DeepSeek streams `tool_calls.function.arguments` as deltas. Two failure
//! shapes are common: (a) SSE chunk boundary cuts inside a JSON string and
//! reassembly leaves a trailing comma or unclosed brace; (b) some local
//! backends emit literal control characters inside JSON string values.
//!
//! The repair ladder runs five stages before falling back to an empty object:
//!
//! 1. Strict parse — done if it parses.
//! 2. Strip literal control chars inside string values.
//! 3. Strip trailing commas before `}` or `]`.
//! 4. Balance braces/brackets (append closers).
//! 5. Strip excess closers if delta is negative.
//! 6. Fallback: empty object `{}`.
use serde_json::{Map, Value};
/// Maximum raw argument length we'll attempt to repair (1 MiB).
const MAX_ARG_LEN: usize = 1024 * 1024;
#[derive(Debug, thiserror::Error)]
pub enum ArgRepairError {
#[error("argument exceeded {0} chars; refusing to repair")]
TooLarge(usize),
}
/// Repair a raw JSON argument string into a valid `serde_json::Value`.
///
/// Runs the deterministic ladder; on success returns the parsed value.
/// The final fallback is an empty object `{}` so dispatch always proceeds.
pub fn repair(raw: &str) -> Result<Value, ArgRepairError> {
if raw.len() > MAX_ARG_LEN {
return Err(ArgRepairError::TooLarge(raw.len()));
}
// Stage 1: strict parse
if let Ok(v) = serde_json::from_str(raw) {
return Ok(v);
}
// Stage 2: strip control chars inside strings
let mut s = strip_control_chars_in_strings(raw);
if let Ok(v) = serde_json::from_str(&s) {
return Ok(v);
}
// Stage 3: strip trailing commas
s = strip_trailing_commas(&s);
if let Ok(v) = serde_json::from_str(&s) {
return Ok(v);
}
// Stage 4: balance braces
s = balance_braces(&s, 50);
if let Ok(v) = serde_json::from_str(&s) {
return Ok(v);
}
// Stage 5: strip excess closers
s = strip_excess_closers(&s);
if let Ok(v) = serde_json::from_str(&s) {
return Ok(v);
}
// Fallback: empty object
Ok(Value::Object(Map::new()))
}
/// Strip ASCII control characters (0x00–0x1F except \t, \n, \r) that appear
/// inside JSON string values. We walk character-by-character tracking whether
/// we're inside a string (between unescaped double-quotes).
fn strip_control_chars_in_strings(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut in_string = false;
let mut escape = false;
for ch in s.chars() {
if escape {
out.push(ch);
escape = false;
continue;
}
if ch == '\\' {
escape = true;
out.push(ch);
continue;
}
if ch == '"' {
in_string = !in_string;
out.push(ch);
continue;
}
if in_string && (ch as u32) < 0x20 && ch != '\t' && ch != '\n' && ch != '\r' {
// Drop control characters inside strings
continue;
}
out.push(ch);
}
out
}
/// Strip trailing commas before `}` or `]`.
fn strip_trailing_commas(s: &str) -> String {
// Repeatedly replace ",}" and ",]" until stable (handles nested cases).
let mut out = s.to_string();
loop {
let prev = out.clone();
out = out.replace(",}", "}").replace(",]", "]");
// Handle trailing comma at end of string
out = out.trim_end_matches(',').to_string();
if out == prev {
break;
}
}
out
}
/// Balance braces and brackets: count `{`/`}` and `[`/`]`, append closers if
/// positive delta (more opens than closes). Caps iterations so a
/// catastrophically broken input doesn't loop forever.
fn balance_braces(s: &str, max_iter: usize) -> String {
let mut out = s.to_string();
for _ in 0..max_iter {
let brace_delta: i32 = out
.chars()
.map(|ch| match ch {
'{' => 1,
'}' => -1,
_ => 0,
})
.sum();
let bracket_delta: i32 = out
.chars()
.map(|ch| match ch {
'[' => 1,
']' => -1,
_ => 0,
})
.sum();
if brace_delta <= 0 && bracket_delta <= 0 {
break;
}
// Append needed closers in reverse order (brackets before braces
// for correct nesting when both are unbalanced).
for _ in 0..bracket_delta.max(0) {
out.push(']');
}
for _ in 0..brace_delta.max(0) {
out.push('}');
}
}
out
}
/// Strip excess closers when the delta is negative (more closes than opens).
fn strip_excess_closers(s: &str) -> String {
let mut brace_depth: i32 = 0;
let mut bracket_depth: i32 = 0;
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
match ch {
'}' => {
if brace_depth > 0 {
brace_depth -= 1;
out.push(ch);
}
// else drop excess closer
}
']' => {
if bracket_depth > 0 {
bracket_depth -= 1;
out.push(ch);
}
}
'{' => {
brace_depth += 1;
out.push(ch);
}
'[' => {
bracket_depth += 1;
out.push(ch);
}
_ => out.push(ch),
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn strict_parse_passes_through() {
let v = repair(r#"{"path": "hello.txt"}"#).unwrap();
assert_eq!(v, json!({"path": "hello.txt"}));
}
#[test]
fn repairs_trailing_comma() {
let v = repair(r#"{"path": "hello.txt",}"#).unwrap();
assert_eq!(v, json!({"path": "hello.txt"}));
}
#[test]
fn repairs_trailing_comma_in_array() {
let v = repair(r#"["a", "b",]"#).unwrap();
assert_eq!(v, json!(["a", "b"]));
}
#[test]
fn repairs_missing_close_brace() {
let v = repair(r#"{"path": "hello.txt""#).unwrap();
assert_eq!(v, json!({"path": "hello.txt"}));
}
#[test]
fn repairs_missing_close_bracket() {
let v = repair(r#"["a", "b""#).unwrap();
assert_eq!(v, json!(["a", "b"]));
}
#[test]
fn strips_embedded_control_chars() {
// Raw \x0B (vertical tab) inside a string value
let raw = "{\"key\": \"val\x0Bue\"}";
let v = repair(raw).unwrap();
assert_eq!(v, json!({"key": "value"}));
}
#[test]
fn handles_empty_string() {
let v = repair("").unwrap();
assert_eq!(v, json!({}));
}
#[test]
fn handles_gibberish() {
let v = repair("not json at all").unwrap();
assert_eq!(v, json!({}));
}
#[test]
fn balances_nested_braces() {
let v = repair(r#"{"outer": {"inner": "val""#).unwrap();
assert_eq!(v, json!({"outer": {"inner": "val"}}));
}
#[test]
fn strips_excess_closers() {
let v = repair(r#"{"key": "val"}}"#).unwrap();
assert_eq!(v, json!({"key": "val"}));
}
#[test]
fn handles_double_encoded_json() {
// This is a valid JSON string containing a JSON object literal.
// repair parses it as a string; the engine's existing fallback
// (parse_tool_input) will unwrap the string and re-parse.
let v = repair(r#""{\"path\": \"hello.txt\"}""#).unwrap();
assert_eq!(v, Value::String(r#"{"path": "hello.txt"}"#.to_string()));
}
#[test]
fn oversize_input_rejected() {
let big = "x".repeat(MAX_ARG_LEN + 1);
assert!(repair(&big).is_err());
}
#[test]
fn repairs_brace_balance_with_trailing_comma() {
let v = repair(r#"{"a": 1,"#).unwrap();
assert_eq!(v, json!({"a": 1}));
}
}