Skip to content

fix(llmpool): keep trimmed provider errors on a rune boundary - #338

Merged
BaaRai merged 1 commit into
BaaRai:mainfrom
Dev-next-gen:fix/llmpool-trimerr-utf8
Nov 18, 2026
Merged

BaaRai merged 1 commit into
BaaRai:mainfrom
Dev-next-gen:fix/llmpool-trimerr-utf8

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor

When an LLM profile's provider answers a failover-eligible error in Chinese, the circuit-breaker state for that profile never reaches the database. The error text is <prefix>: status <code>: <body>, and trimErr in llmpool/pool.go shortens it with s[:300], which can cut a multi-byte character in half. That string becomes State.LastError, and healthSync writes it to llm_profile_health.last_error (TEXT). PostgreSQL rejects the invalid UTF-8, so the row is not saved, the op is put back and retried every second with the same error, and a cooling-off window does not survive a restart.

With the error openai: status 503: followed by 服务暂时不可用,请稍后重试。 repeated, trimErr returns a string ending in \xe7…. Copying that value into the llm_profile_health table from db/schema.sql on postgres:16 fails with invalid byte sequence for encoding "UTF8": 0xe7 0xe2 0x80.

The fix moves the cut back to the start of the rune, so the result is valid UTF-8 and still at most 300 bytes plus the ellipsis.

TestTrimErrKeepsValidUTF8 fails before the fix with the trailing \xe7 and passes after. go test ./llmpool passes, and the fixed value copies into the same postgres:16 table without error.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.

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.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cutting back to the previous rune start keeps the value valid UTF-8, and the Chinese-body test pins the PostgreSQL failure mode. Thanks.

@BaaRai
BaaRai merged commit 9b8ddf0 into BaaRai:main Nov 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants