diff --git a/llmpool/pool.go b/llmpool/pool.go index 5feba40..39840f4 100644 --- a/llmpool/pool.go +++ b/llmpool/pool.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" "sync/atomic" + "unicode/utf8" "github.com/Autumn-27/norma/llm" ) @@ -262,7 +263,11 @@ func isHardFailure(err error) bool { func trimErr(err error) string { s := strings.TrimSpace(strings.ReplaceAll(err.Error(), "\n", " ")) if len(s) > 300 { - s = s[:300] + "…" + cut := 300 + for cut > 0 && !utf8.RuneStart(s[cut]) { + cut-- + } + s = s[:cut] + "…" } return s } diff --git a/llmpool/pool_test.go b/llmpool/pool_test.go index 21b7568..23cbef9 100644 --- a/llmpool/pool_test.go +++ b/llmpool/pool_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" "time" + "unicode/utf8" "github.com/Autumn-27/norma/llm" ) @@ -368,3 +369,16 @@ func TestExpiredWindowIsClosed(t *testing.T) { t.Fatal("expired window should read as closed") } } + +// A provider error body in Chinese is cut at 300 bytes; the cut must not split +// a rune, or the breaker's last_error is rejected by PostgreSQL as invalid UTF-8. +func TestTrimErrKeepsValidUTF8(t *testing.T) { + err := errors.New("openai: status 503: " + strings.Repeat("服务暂时不可用,请稍后重试。", 10)) + got := trimErr(err) + if !utf8.ValidString(got) { + t.Fatalf("trimErr returned invalid UTF-8: %q", got) + } + if !strings.HasSuffix(got, "…") || len(got) > 300+len("…") { + t.Fatalf("trimErr = %q (%d bytes), want at most 300 bytes plus …", got, len(got)) + } +}