fix(devin): classify JSON control character errors

This commit is contained in:
sususu
2026-10-29 09:46:47 +08:00
parent fa42f390c1
commit 02ee313962
2 changed files with 33 additions and 0 deletions

View File

@@ -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"):

View File

@@ -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)