diff --git a/internal/agent/tools/json_repair.go b/internal/agent/tools/json_repair.go index eb8bf121..1ef16e49 100644 --- a/internal/agent/tools/json_repair.go +++ b/internal/agent/tools/json_repair.go @@ -13,6 +13,7 @@ import ( // - Unquoted keys // - Invalid backslash escapes inside strings (e.g. regex patterns like // "C\+\+", "\d+", "\.log$") where the LLM forgot to double-escape. +// - Extra tokens after the top-level value (e.g. `{}""`, `{"a":1}{"a":1}`) // // Returns the repaired JSON string. If repair is not possible, // returns the original string unchanged (caller should handle parse errors). @@ -36,6 +37,9 @@ func RepairJSON(s string) string { // strings confuse the comma/bracket trackers below. s = fixInvalidEscapes(s) + // Drop anything after the first complete top-level value + s = trimAfterTopLevelValue(s) + // Fix trailing commas: ,} or ,] s = fixTrailingCommas(s) @@ -102,6 +106,51 @@ func fixInvalidEscapes(s string) string { return out.String() } +// trimAfterTopLevelValue cuts everything that follows the first balanced +// top-level object/array. Streaming providers occasionally emit a stray +// argument fragment after the arguments are already complete — a lone `""` +// or a repeat of the whole payload — and the concatenated result (`{}""`) +// fails to parse even though the leading value is perfectly good. +// Input that never closes its top-level value is returned unchanged so that +// balanceBrackets can still recover a truncated payload. +func trimAfterTopLevelValue(s string) string { + depth := 0 + inString := false + escaped := false + + for i, r := range s { + if escaped { + escaped = false + continue + } + if inString { + switch r { + case '\\': + escaped = true + case '"': + inString = false + } + continue + } + switch r { + case '"': + inString = true + case '{', '[': + depth++ + case '}', ']': + depth-- + if depth == 0 { + end := i + len(string(r)) + if strings.TrimSpace(s[end:]) == "" { + return s + } + return s[:end] + } + } + } + return s +} + // fixTrailingCommas removes trailing commas before closing brackets/braces. func fixTrailingCommas(s string) string { // Simple state machine to handle strings diff --git a/internal/agent/tools/json_repair_test.go b/internal/agent/tools/json_repair_test.go index bcfea7a3..8860bcac 100644 --- a/internal/agent/tools/json_repair_test.go +++ b/internal/agent/tools/json_repair_test.go @@ -116,6 +116,35 @@ func TestRepairJSON(t *testing.T) { assert.Equal(t, once, twice) }) + t.Run("stray fragment after empty object", func(t *testing.T) { + // Streaming providers sometimes append a lone `""` fragment after the + // arguments are already complete. + result := RepairJSON(`{}""`) + assert.Equal(t, "{}", result) + }) + + t.Run("stray fragment after complete object", func(t *testing.T) { + result := RepairJSON(`{"path": "/workspace/output"}""`) + assert.True(t, json.Valid([]byte(result)), "result: %s", result) + + var parsed struct { + Path string `json:"path"` + } + require.NoError(t, json.Unmarshal([]byte(result), &parsed)) + assert.Equal(t, "/workspace/output", parsed.Path) + }) + + t.Run("duplicated payload keeps first value", func(t *testing.T) { + result := RepairJSON(`{"a": 1}{"a": 1}`) + assert.Equal(t, `{"a": 1}`, result) + }) + + t.Run("brace inside string does not trigger trim", func(t *testing.T) { + input := `{"query": "}", "limit": 1}` + result := RepairJSON(input) + assert.Equal(t, input, result) + }) + t.Run("valid escapes are preserved", func(t *testing.T) { // Valid JSON escapes must pass through untouched. input := `{"path": "C:\\Users\\x", "line": "a\nb", "quote": "say \"hi\""}`