From 02ee313962ba0fca3b0c1203813dbba92005c3de Mon Sep 17 00:00:00 2001 From: sususu Date: Thu, 29 Oct 2026 09:46:47 +0800 Subject: [PATCH] fix(devin): classify JSON control character errors --- .../helps/devin_apply_patch_diagnostic.go | 6 +++++ .../devin_apply_patch_diagnostic_test.go | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/internal/runtime/executor/helps/devin_apply_patch_diagnostic.go b/internal/runtime/executor/helps/devin_apply_patch_diagnostic.go index 7b46f0ca98..7088d95d39 100644 --- a/internal/runtime/executor/helps/devin_apply_patch_diagnostic.go +++ b/internal/runtime/executor/helps/devin_apply_patch_diagnostic.go @@ -2,6 +2,7 @@ package helps import ( "fmt" + "strconv" "strings" "unicode/utf8" @@ -229,6 +230,11 @@ func devinPatchReasonCode(reason string) string { switch { case strings.HasSuffix(reason, "in string literal"): return "invalid control character in apply_patch input" + case strings.HasPrefix(reason, "decode apply_patch input value: invalid character ") && strings.HasSuffix(reason, " in string"): + quoted := strings.TrimSuffix(strings.TrimPrefix(reason, "decode apply_patch input value: invalid character "), " in string") + if character, errUnquote := strconv.Unquote(quoted); errUnquote == nil && len(character) == 1 && character[0] < 0x20 { + return "invalid control character in apply_patch input" + } case strings.Contains(reason, "in \\u hexadecimal character escape"): return "invalid Unicode escape in apply_patch input" case strings.Contains(reason, "in string escape code"): diff --git a/internal/runtime/executor/helps/devin_apply_patch_diagnostic_test.go b/internal/runtime/executor/helps/devin_apply_patch_diagnostic_test.go index a24d42a29f..72441febf8 100644 --- a/internal/runtime/executor/helps/devin_apply_patch_diagnostic_test.go +++ b/internal/runtime/executor/helps/devin_apply_patch_diagnostic_test.go @@ -2,9 +2,11 @@ package helps import ( "errors" + "fmt" "strings" "testing" + applypatch "github.com/router-for-me/CLIProxyAPI/v8/internal/client/codex/apply-patch" "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" ) @@ -58,6 +60,31 @@ func TestDevinApplyPatchDiagnosticsUsesTranslatorReason(t *testing.T) { } } +func TestDevinApplyPatchDiagnosticsControlCharacterReasons(t *testing.T) { + for c := byte(0); c < 0x20; c++ { + t.Run(fmt.Sprintf("control_%02x", c), func(t *testing.T) { + _, err := applypatch.UnwrapInput("{\"input\":\"secret" + string(c) + "secret\"}") + if err == nil { + t.Fatal("expected invalid JSON") + } + state := &common.ApplyPatchErrorState{} + state.SetToolInputError(err) + got := (*DevinApplyPatchDiagnostics)(nil).Render(state, "") + if !strings.Contains(got, `reason="invalid control character in apply_patch input"`) || strings.Contains(got, "secret") { + t.Fatalf("unexpected diagnostic: %s", got) + } + }) + } + for _, reason := range []string{ + `decode apply_patch input value: invalid character 'x' in string`, + `decode apply_patch input value: invalid character 'secret' in string`, + } { + if got := devinPatchReasonCode(reason); got != "other" { + t.Errorf("reason %q classified as %q", reason, got) + } + } +} + func TestDevinApplyPatchDiagnosticsBounded(t *testing.T) { if got := (*DevinApplyPatchDiagnostics)(nil).Render(nil, ""); got != ApplyPatchUpstreamErrorMessage { t.Fatalf("nil diagnostic = %q", got)