Files
ModelRouter/internal/scheduler/empty_response_test.go
JianFeeeee c19b8e6394 fix(scheduler): AUTO 遇空内容响应降级到下一个 slot(客户端曾报 "no content")
生产故障:pi 客户端报 `model "AUTO" returned a completed response with no content`,
重试延迟 8 秒。实测 AUTO 20 次有 2 次返回空 content,全部是 claude-opus-4-8。

根因(直连上游抓包确认):思考型模型先吐 reasoning_content,max_tokens 小到
思考阶段就把预算用完时,上游返回 200 / finish_reason=length,28 个 chunk 全是
reasoning_content、content 一片空白。runTier 只看 err == nil 就当成功返回,
客户端拿到一个空响应。

修复:
- resultIsEmpty:非流式路径把「无 content、无 tool_calls、无 image」的响应当作
  slot 失败继续降级。注意 ReasoningContent 不算内容——客户端要的是文本,为
  另一个模型的思考阶段扣住请求比降级更糟。
- peekStream:流式路径在出现首个真实内容前缓冲 reasoning 前导,流结束仍无内容
  则回报空结果,让 chainDrive 换 slot。缓冲只覆盖思考前导,拿到内容后立即
  转发。tool_calls delta 算内容,agent 回合不会被误判。
- 3 条判据 + 3 个变异(恒 false / 恒 true / reasoning 算内容)全部被捕获。

同时修三个 WebUI 布局缺陷(都靠截图而非 DOM 断言发现):
- 插件侧栏项只渲染图标没有标题:btn.innerHTML 只塞 pluginIconHTML(pg.icon),
  与原生页的「图标 + <span>标题</span>」不一致,侧栏是一排无名图标。
- 计费维度表 8 列挤在 465px 卡片里:table-layout:fixed 把每列压到 62px,
  23/80 个单元格溢出、数字互相重叠。改为 6 列(token 细分合并为
  「输入(新鲜+缓存)」,细分进 title)+ table-layout:auto,实测 0/60 溢出。
- 数字列 word-break:break-all 让每个字符独占一行(USD 0.56 竖排成 U/S/D),
  改 nowrap + 容器横向滚动。
2026-10-02 15:10:01 +08:00

128 lines
4.4 KiB
Go

package scheduler
import (
"context"
"encoding/json"
"strings"
"testing"
"time"
"llmsproxy/internal/types"
)
// TestResultIsEmptyMatchesTheReasoningOnlyResponse guards the production bug
// behind "model AUTO returned a completed response with no content".
//
// Production measurement: 2 of 20 AUTO requests returned zero content, both
// served by claude-opus-4-8. Direct upstream capture showed 28 chunks of
// `reasoning_content` and NO `content`, finish_reason=length — the token budget
// was consumed by thinking before any text was emitted. runTier accepted it
// because err == nil.
func TestResultIsEmptyMatchesTheReasoningOnlyResponse(t *testing.T) {
// Exactly what the upstream returned, as a decoded response.
onlyReasoning := &types.UnifiedResponse{
Model: "claude-opus-4-8",
ReasoningContent: "Alright, the user just said \"hi\". Simple greeting…",
FinishReason: "length",
TokenUsage: types.TokenUsage{Prompt: 14, Completion: 30, Total: 44},
}
if !resultIsEmpty(onlyReasoning) {
t.Error("a reasoning-only response with usage must count as empty; " +
"it is what the client sees as \"completed with no content\"")
}
// The same response WITH text is a normal answer.
if resultIsEmpty(&types.UnifiedResponse{Content: "Hi! 👋", ReasoningContent: "hmm"}) {
t.Error("a response with content is not empty")
}
// A tool-calling agent turn has no text and is perfectly valid.
if resultIsEmpty(&types.UnifiedResponse{
ToolCalls: []types.ToolCall{{Name: "read_file", Arguments: map[string]interface{}{"path": "x"}}},
}) {
t.Error("a tool-call-only response must NOT count as empty — agents " +
"legitimately produce tool calls with no text")
}
// An image slot returns no text by design.
if resultIsEmpty(&types.UnifiedResponse{
ImageData: []types.ImageData{{URL: "http://x/y.png"}},
}) {
t.Error("an image response must NOT count as empty")
}
// Whitespace-only content is as useless to a client as none at all.
if !resultIsEmpty(&types.UnifiedResponse{Content: " \n\t"}) {
t.Error("whitespace-only content must count as empty")
}
if resultIsEmpty(nil) {
t.Error("nil is not an empty response; it is an absent one")
}
}
// TestPeekStreamHoldsReasoningAndReportsEmpty is the streaming half: a channel
// of reasoning-only chunks must be reported as empty so chainDrive degrades.
func TestPeekStreamHoldsReasoningAndReportsEmpty(t *testing.T) {
in := make(chan types.UnifiedChunk, 8)
for i := 0; i < 5; i++ {
in <- types.UnifiedChunk{ReasoningContent: "thinking…"}
}
in <- types.UnifiedChunk{Done: true, FinishReason: "length"}
close(in)
_, peek := peekStream(in)
if peek() {
t.Error("a reasoning-only stream must be reported empty")
}
}
// TestPeekStreamForwardsContentAfterReasoning: the common case still works,
// and the reasoning preamble is not forwarded ahead of the content (holding it
// back is what keeps the degrade path available).
func TestPeekStreamForwardsContentAfterReasoning(t *testing.T) {
in := make(chan types.UnifiedChunk, 8)
in <- types.UnifiedChunk{ReasoningContent: "let me think"}
in <- types.UnifiedChunk{Content: "Hi"}
in <- types.UnifiedChunk{Content: "!"}
in <- types.UnifiedChunk{Done: true, FinishReason: "stop"}
close(in)
out, peek := peekStream(in)
if !peek() {
t.Fatal("a stream with content must be reported as having content")
}
var got []string
for ck := range out {
if ck.Content != "" {
got = append(got, ck.Content)
}
if strings.TrimSpace(ck.ReasoningContent) != "" {
t.Error("reasoning preamble must not be forwarded before content; " +
"that is what pins the client to a stream it cannot escape")
}
}
if strings.Join(got, "") != "Hi!" {
t.Errorf("forwarded content = %q, want %q", got, "Hi!")
}
}
// TestPeekStreamDoesNotDeadlockOnToolCalls: a tool-call delta is content for
// this purpose and must unblock peek immediately.
func TestPeekStreamDoesNotDeadlockOnToolCalls(t *testing.T) {
raw, _ := json.Marshal([]types.ToolCall{{Name: "ls"}})
in := make(chan types.UnifiedChunk, 4)
in <- types.UnifiedChunk{ToolCalls: raw}
in <- types.UnifiedChunk{Done: true, FinishReason: "tool_calls"}
close(in)
_, peek := peekStream(in)
done := make(chan bool, 1)
go func() { done <- peek() }()
select {
case ok := <-done:
if !ok {
t.Error("a tool-call delta must count as content")
}
case <-time.After(2 * time.Second):
t.Fatal("peek blocked on a tool-call-only stream")
}
_ = context.Background()
}