fix(runtime): 上游错误体统一走 trace.UpstreamError,正文不进 error

M41 的「LLM / embedding / 视觉 上游错误体」一族(M38 #5 #6 #7 #8 #9 #14):
非 2xx 时把响应体截断后拼进 error,而 error 会被 chat run / loop step /
retrieve_fallback 打日志,等于把她的提问、记忆正文、要念的台词写进日志。

- model:openai/anthropic 两处 upstreamError 收口到 upstream.go 的
  providerUpstreamError;anthropic 流内 error 事件的 evt.Error.Message
  (原本无上限)整段丢弃,只保留令牌形状的 type
- embedding:readErrorBody 删除,改走 helper(input 就是她的记忆正文)
- vision:错误路径不再沿用取图的 50 MiB 上限,改为 4 KiB 有界读且不拼原文
- voice:classifyDashscopeStatus 只解 code/request_id,正文只留在内存里做
  分类判据;错误体读取同样改为 4 KiB 有界
- 测试:五个上游各一条「日志/error 里搜不到哨兵正文」用例,且能变异打红
This commit is contained in:
AskaEth committed 2026-10-26 13:44:32 +08:00
1 parent 46a6da13e0
commit 66dda084cd
16 files changed
+570 -183

No files matched your search

+11 -18
View File
@@ -14,6 +14,7 @@ import (
"time"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
)
// maxResponseBytes caps how much of an /embeddings response body is read, so an
@@ -21,8 +22,9 @@ import (
// for a normal batch fall far below this; the cap is a guard, not a limit.
const maxResponseBytes = 16 << 20
// maxErrorBodyBytes caps how much of a non-OK response body is read for the
// error message.
// maxErrorBodyBytes 限制错误路径**读多少字节**(只用来数长度、取摘要、抽
// code/type):正文一个字节都不进 error,因此它不再决定「错误里带多少原文」。
// 非 2xx 是攻击面而不是数据面,上界刻意小。
const maxErrorBodyBytes = 4096
// HTTP client tuning values, matching the recommended pool in
@@ -234,8 +236,13 @@ func (e *APIEmbedder) embedChunk(ctx context.Context, texts []string) ([]Vector,
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
msg := readErrorBody(resp.Body)
return nil, fmt.Errorf("embedding: status %d: %s", resp.StatusCode, msg)
// 这一处是全片最敏感的一条:/embeddings 的 **input 就是她的记忆正文**,
// 而兼容层常把 input 原样回显在错误体里(`Invalid value: '<正文>'`)。
// 因此正文只用来数长度、取摘要、抽 code/type,一个字节都不进 error ——
// 口径由 trace.UpstreamError(M40 共享 helper)统一决定。
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrorBodyBytes))
return nil, fmt.Errorf("embedding: %w", trace.UpstreamError(
trace.HostOf(e.cfg.APIURL), resp.StatusCode, resp.Header.Get("Content-Type"), body))
}
data, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes))
@@ -300,20 +307,6 @@ func (e *APIEmbedder) newEmbeddingRequest(ctx context.Context, texts []string) (
return req, nil
}
// readErrorBody drains a small prefix of a non-OK response body into a message,
// capped at maxErrorBodyBytes so an error response cannot exhaust memory.
func readErrorBody(r io.Reader) string {
raw, err := io.ReadAll(io.LimitReader(r, maxErrorBodyBytes))
if err != nil {
return err.Error()
}
msg := strings.TrimSpace(string(raw))
if msg == "" {
return "bad status"
}
return msg
}
// defaultHTTPClient builds the HTTP client configuration described in
// DevDocs/02-development-constraints.md §2.3: tuned idle/TLS timeouts plus a
// total request timeout. A fresh client is returned per call (never a package
+36
View File
@@ -135,6 +135,42 @@ func TestAPIEmbedder_Non200Errors(t *testing.T) {
}
}
// TestAPIEmbedder_Non200DropsEchoedInput 覆盖 M41 里最敏感的一处:/embeddings 的
// **input 就是她的记忆正文**,而兼容层常把 input 原样回显在错误体里。旧实现读
// 4096 字节响应体、整个拼进 error,而 error 会被 retrieve_fallback 等位置打日志。
//
// 断言的是「文本里搜不到正文」,不是「error 非空」—— 后者在旧实现下同样成立,
// 打不红。
func TestAPIEmbedder_Non200DropsEchoedInput(t *testing.T) {
t.Parallel()
const memo = "SENTINEL-MEMORY-INPUT-4a7d"
rt := roundTripFunc(func(r *http.Request) (*http.Response, error) {
body := `{"error":{"code":"invalid_request_error","param":"input",` +
`"message":"Invalid value: '` + memo + `'"}}`
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(body)),
Header: http.Header{"Content-Type": []string{"application/json"}},
}, nil
})
emb := NewAPI(
config.EmbeddingConfig{APIURL: "https://embed.example.com/v1/embeddings"},
WithRoundTripper(rt),
)
_, err := emb.Embed(context.Background(), []string{memo})
if err == nil {
t.Fatal("expected error on 400, got nil")
}
if strings.Contains(err.Error(), memo) {
t.Errorf("error text echoes the memory text: %v", err)
}
if !strings.Contains(err.Error(), "400") {
t.Errorf("err = %v, want the status kept for triage", err)
}
}
func TestAPIEmbedder_MismatchedCountErrors(t *testing.T) {
t.Parallel()