同一份上游报文,兄弟出口 streamEventError 守的正是 type,而 providerUpstreamError 只守了 error.code —— 网关/兼容层把回显塞进 type 时会被原样渲染进错误与日志。 两个字段现在用同一道 trace.ErrorToken:不像令牌的变空串,status/code/body_bytes 等诊断面不受影响。
129 lines
5.6 KiB
Go
129 lines
5.6 KiB
Go
package model
|
|
|
|
// 本文件钉住 providerUpstreamError 的两条出镜边界:
|
|
//
|
|
// - **error.code / error.type 是自由文本面**:它们在主流上游是枚举,但网关、代理与
|
|
// 兼容层可以往里面塞任何东西(包括回显的输入)。只有像令牌才出镜,其余变空串 ——
|
|
// 而 status / 体积摘要这些诊断面不受影响。两个字段必须同一道守卫:同一份上游报文
|
|
// 的两个出口(本文件与 streamEventError)口径不一致,就是一个漏点。
|
|
// - **响应体正文一个字节都不进错误**:这是 providerUpstreamError 存在的理由
|
|
// (旧实现把体截到 512B 拼进 error,而 `chat run` / `loop step` 会把它落日志)。
|
|
//
|
|
// 每条用例都是「把它放宽就变红」的形状:断言的是错误的文本,也就是真正进日志的字符串。
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
|
)
|
|
|
|
// upstreamErrResp 造一条非 2xx 响应:只带状态码、Content-Type 与体,够
|
|
// providerUpstreamError 走完整条收敛路径。
|
|
func upstreamErrResp(status int, body string) *http.Response {
|
|
return &http.Response{
|
|
StatusCode: status,
|
|
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
|
Body: io.NopCloser(strings.NewReader(body)),
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorDropsFreeTextCode code 里是回显的用户原文 ⇒ 该字段整体消失
|
|
// (连 `code=` 这个键都不出现),而 status / type 仍在。去掉令牌守卫就变红。
|
|
func TestProviderUpstreamErrorDropsFreeTextCode(t *testing.T) {
|
|
t.Parallel()
|
|
const echoed = "Invalid value: '她刚才说的那句话'"
|
|
body := `{"error":{"code":"` + echoed + `","type":"invalid_request_error"}}`
|
|
|
|
err := providerUpstreamError("openai", "https://api.example.com/v1/chat/completions", upstreamErrResp(http.StatusBadRequest, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
msg := err.Error()
|
|
if strings.Contains(msg, echoed) {
|
|
t.Fatalf("code 里的用户原文进了错误(会随 chat run 落日志):%v", msg)
|
|
}
|
|
if strings.Contains(msg, "code=") {
|
|
t.Fatalf("非令牌形状的 code 没有被丢弃:%v", msg)
|
|
}
|
|
// 诊断面不能一起丢:状态、协议枚举的 type、体积摘要都还在。
|
|
for _, want := range []string{"status=400", "type=invalid_request_error", "body_bytes=", "api.example.com"} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Fatalf("错误里缺少 %q(诊断面不能一起丢):%v", want, msg)
|
|
}
|
|
}
|
|
if !errors.Is(err, ErrUpstream) || !errors.Is(err, trace.ErrUpstream) {
|
|
t.Fatalf("降级链的哨兵断了:%v", err)
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorKeepsTokenShapedCode 真实枚举原样出镜:丢掉它只是少一条
|
|
// 分类提示,但既然它像令牌就该留下。
|
|
func TestProviderUpstreamErrorKeepsTokenShapedCode(t *testing.T) {
|
|
t.Parallel()
|
|
body := `{"error":{"code":"Arrearage","type":"invalid_request_error"}}`
|
|
|
|
err := providerUpstreamError("deepseek", "https://api.deepseek.com/v1/chat/completions",
|
|
upstreamErrResp(http.StatusPaymentRequired, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
if msg := err.Error(); !strings.Contains(msg, "code=Arrearage") {
|
|
t.Fatalf("令牌形状的 code 被误杀:%v", msg)
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorDropsBodyText 体里除了 code/type 之外的字节一个都不进错误:
|
|
// 上游把用户的提问回显在 error.message 里是真实写法。
|
|
func TestProviderUpstreamErrorDropsBodyText(t *testing.T) {
|
|
t.Parallel()
|
|
const prompt = "她的原始提问:帮我看看这段代码"
|
|
body := `{"error":{"code":"invalid_request_error","type":"invalid_request_error",` +
|
|
`"message":"Invalid value: '` + prompt + `'"}}`
|
|
|
|
err := providerUpstreamError("openai", "https://api.example.com/v1/chat/completions",
|
|
upstreamErrResp(http.StatusBadRequest, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
if strings.Contains(err.Error(), prompt) {
|
|
t.Fatalf("error.message 的回显进了错误:%v", err)
|
|
}
|
|
}
|
|
|
|
// TestProviderUpstreamErrorDropsFreeTextType type 里是带空格标点的散文(网关 / 兼容层
|
|
// 把回显塞进 type 是真实写法)⇒ 该字段整体消失(连 ` type=` 这个键都不出现),而同一
|
|
// 份报文里的 status / code / body_bytes 仍在。去掉 type 的令牌守卫就变红 —— 兄弟出口
|
|
// streamEventError 守的正是 type,两个出口对同一份上游报文必须口径一致。
|
|
func TestProviderUpstreamErrorDropsFreeTextType(t *testing.T) {
|
|
t.Parallel()
|
|
const prose = "Invalid value: 她刚才说的那句话"
|
|
body := `{"error":{"code":"invalid_request_error","type":"` + prose + `"}}`
|
|
|
|
err := providerUpstreamError("openai", "https://api.example.com/v1/chat/completions",
|
|
upstreamErrResp(http.StatusBadRequest, body))
|
|
if err == nil {
|
|
t.Fatal("非 2xx 却报成功")
|
|
}
|
|
msg := err.Error()
|
|
if strings.Contains(msg, prose) {
|
|
t.Fatalf("type 里的回显进了错误(会随 chat run 落日志):%v", msg)
|
|
}
|
|
// ` type=` 带前导空格:content_type= 的尾巴里也有 "type=",不能用裸子串判。
|
|
if strings.Contains(msg, " type=") {
|
|
t.Fatalf("非令牌形状的 type 没有被丢弃:%v", msg)
|
|
}
|
|
// 诊断面不能一起丢:状态、令牌形状的 code、体积摘要都还在。
|
|
for _, want := range []string{"status=400", "code=invalid_request_error", "body_bytes=", "api.example.com"} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Fatalf("错误里缺少 %q(诊断面不能一起丢):%v", want, msg)
|
|
}
|
|
}
|
|
if !errors.Is(err, ErrUpstream) || !errors.Is(err, trace.ErrUpstream) {
|
|
t.Fatalf("降级链的哨兵断了:%v", err)
|
|
}
|
|
}
|