File size: 3,646 Bytes
e48a2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package server

import (
	"encoding/json"
	"testing"

	"github.com/misakano7545/workbuddy2api-panel/internal/upstream"
)

// TestStarvedRetryBodyRaisesBudget 救援段必须带上预算下限。
//
// 线上实测(2026-11-08,cn:deepseek-v4.1-flash,effort=high、客户端 max_tokens=1024):
// 首段被抬到 8192,但这 8192 全被思考吃掉、正文 0B → 触发救援;救援段从客户端**原始**
// body 重建,只拿到 1024,正文写 1946 字就断。这条钉住修复:救援段至少 floor。
func TestStarvedRetryBodyRaisesBudget(t *testing.T) {
	raw, ok := starvedRetryBodyWithFloor(
		[]byte(`{"model":"deepseek-v4.1-flash","max_tokens":1024,"reasoning_effort":"high","messages":[]}`),
		upstream.MinOutputBudget("high"))
	if !ok {
		t.Fatal("应构造出救援体")
	}
	var obj map[string]any
	if err := json.Unmarshal(raw, &obj); err != nil {
		t.Fatalf("decode: %v", err)
	}
	if got, _ := obj["max_tokens"].(float64); int64(got) != 8192 {
		t.Fatalf("救援段 max_tokens=%v want 8192(high 档下限)", obj["max_tokens"])
	}
	// 关思考的约定不变:救援靠的是「把预算从思考拿回来」。
	if th, _ := obj["thinking"].(map[string]any); th == nil || th["type"] != "disabled" {
		t.Fatalf("救援段应关思考,实际 thinking=%v", obj["thinking"])
	}
	if _, has := obj["reasoning_effort"]; has {
		t.Fatal("关思考时不得带档位")
	}
}

// TestStarvedRetryBodyKeepsLargeBudget 客户端额度本来就够大时不动它。
func TestStarvedRetryBodyKeepsLargeBudget(t *testing.T) {
	raw, ok := starvedRetryBodyWithFloor(
		[]byte(`{"model":"deepseek-v4.1-flash","max_tokens":65536,"thinking":{"type":"enabled"},"messages":[]}`),
		upstream.MinOutputBudget("max"))
	if !ok {
		t.Fatal("应构造出救援体")
	}
	var obj map[string]any
	json.Unmarshal(raw, &obj)
	if got, _ := obj["max_tokens"].(float64); int64(got) != 65536 {
		t.Fatalf("max_tokens=%v want 65536(不缩小客户端额度)", obj["max_tokens"])
	}
}

// TestContinuationBodyCarriesFloor 续写段同样带下限,且不放大档位对应的思考预算。
func TestContinuationBodyCarriesFloor(t *testing.T) {
	r := &continueReader{
		body:        []byte(`{"model":"cn:deepseek-v4.1-flash","max_tokens":2048,"reasoning_effort":"max","messages":[{"role":"user","content":"hi"}]}`),
		budgetFloor: upstream.MinOutputBudget("max"),
		limitSet:    true,
	}
	r.text.WriteString("半句正文")
	r.segCompletion, r.segThink = 2048, 1900

	raw, err := r.continuationBody()
	if err != nil {
		t.Fatalf("continuationBody: %v", err)
	}
	var obj map[string]any
	json.Unmarshal(raw, &obj)
	if got, _ := obj["max_tokens"].(float64); int64(got) != 16384 {
		t.Fatalf("续写段 max_tokens=%v want 16384(max 档下限)", obj["max_tokens"])
	}
	// 思考吃光形态:续写段关思考(否则同一份额度再被烧一遍)。
	if th, _ := obj["thinking"].(map[string]any); th == nil || th["type"] != "disabled" {
		t.Fatalf("思考吃光的续写段应关思考,实际 thinking=%v", obj["thinking"])
	}
}

// TestMinOutputBudgetTable 下限表本身:未知档位返回 0(不猜),大小单调。
func TestMinOutputBudgetTable(t *testing.T) {
	if got := upstream.MinOutputBudget(""); got != 0 {
		t.Fatalf("未知档位应返回 0,got %d", got)
	}
	if got := upstream.MinOutputBudget("nonsense"); got != 0 {
		t.Fatalf("未知档位应返回 0,got %d", got)
	}
	prev := int64(0)
	for _, e := range []string{"low", "medium", "high", "xhigh", "max"} {
		v := upstream.MinOutputBudget(e)
		if v <= prev {
			t.Fatalf("%s 的下限 %d 未随档位递增(prev=%d)", e, v, prev)
		}
		prev = v
	}
}