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

+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()