From 864a60d48b8e1e05066d1cfaf9c77168d5d44371 Mon Sep 17 00:00:00 2001 From: Leo Camus Date: Wed, 18 Nov 2026 13:07:45 +0000 Subject: [PATCH] fix(llmpool): keep trimmed provider errors on a rune boundary trimErr cut the error text at byte 300, which can split a multi-byte character. The result is stored as llm_profile_health.last_error, and PostgreSQL rejects the invalid UTF-8, so the breaker state of a profile whose provider answers in Chinese is never persisted and the write is retried every second. --- llmpool/pool.go | 7 ++++++- llmpool/pool_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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)) + } +}