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,6 +11,7 @@ import (
"strings"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
)
// visionMaxTokens 限制 vision/OCR 响应的长度。
@@ -170,8 +171,14 @@ func (a *OpenAICompatibleAdapter) ask(ctx context.Context, model string, img Ima
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, maxFetchedBody))
return "", fmt.Errorf("vision: %w: status %d: %s", ErrUpstream, resp.StatusCode, strings.TrimSpace(string(snippet)))
// 错误路径**不再整段读**:这里原来沿用取图的 maxFetchedBody(50 MiB),
// 等于「最多 50 MiB 的错误体进内存、再进 error」。错误体是攻击面,因此
// 只读 maxErrorBodyBytes,并且一个字节都不进 error —— 只留 status /
// contentType / bodyLen / sha256 与 code/type(trace.UpstreamError)。
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrorBodyBytes))
upstream := trace.UpstreamError(trace.HostOf(a.cfg.BaseURL), resp.StatusCode,
resp.Header.Get("Content-Type"), body)
return "", fmt.Errorf("vision: %w: %w", ErrUpstream, upstream)
}
return a.decode(resp.Body)
}