- 窗口声明:models.json 组级 contextWindow + env LLM_CONTEXT_WINDOW(per-group materialize 播种,0 不写盘、负数报错)。 - 预算三态:CONTEXT_MAX_TOKENS 未设置 ⇒ 按窗口推导;>0 显式覆盖;0|负 关闭压缩(不变)。推导 budget = min(窗口×比例, 窗口−输出保留),比例默认 2/3(CONTEXT_BUDGET_RATIO,1M ⇒ 699050 tokens ≈ 683Ki);输出保留 = max(32768, 该组 maxTokens) 且封顶窗口/2(防配置矛盾时退化成 1 token);窗口未声明 ⇒ 回落 131072 + 恰好一条 WARN。接线在组合根 wire_context.go(唯一决策点)。 - usage/缓存可观测(此前**完全不可观测**):Response 增 CacheHit/CacheMiss/Reasoning tokens + CacheHitRate;解析 OpenAI 兼容(prompt_cache_hit_tokens/prompt_cache_miss_tokens/cached_tokens/reasoning_tokens)与 anthropic(cache_read_input_tokens/cache_creation_input_tokens)两种形状;记录点在适配器 record(换入口不丢成本日志),日志无凭据;单次输入 ≥预算 90% 记 WARN(不阻断)。 - 根治清单「续读」假承诺:抽取期不再按注入预算截断(sidecar 存全文,只受 MaxSidecarBytes=8 MiB 安全上限,ExtractorVersion v1→v2 旧 sidecar 失效重抽);注入预算改在装配期生效 ⇒ ref.Chars/Truncated 是注入侧事实、游标真是继续读的起点;document-read 的 limit 上限改为独立具名常量。 - 顺手修越界:config.go 505 行(超 500)拆出 llm.go。 - 主会话独立验收:隔离 worktree(HEAD + 本切片 39 文件,排除在途 gateway)build/vet 绿、全树 59 包 0 FAIL、linux+darwin 交叉编译绿;变异独立复现(比例改 0.25 ⇒ 推导用例红 262144 vs 699050)。
368 lines
15 KiB
Go
368 lines
15 KiB
Go
package model
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"log/slog"
|
||
"net/http"
|
||
"strings"
|
||
"testing"
|
||
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||
)
|
||
|
||
// captureLog 返回一个把文本记录写进缓冲的日志器,外加那个
|
||
// 缓冲,好让测试能断言适配器发出的 WARN。
|
||
func captureLog() (*slog.Logger, *bytes.Buffer) {
|
||
buf := &bytes.Buffer{}
|
||
return slog.New(slog.NewTextHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug})), buf
|
||
}
|
||
|
||
// boolPtr 是三态 Thinking.Enable 的便利构造。
|
||
func boolPtr(v bool) *bool { return &v }
|
||
|
||
// TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer 就是 design/04 §3.2
|
||
// 的红线:大到能吃光 max_tokens 的预算不得照原样发送——
|
||
// 适配器会抬高 max_tokens,让思考与可见答案都放得下,
|
||
// 并记录这次调整(绝不是一次静默改写)。
|
||
func TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan anthropicCaptured, 1)
|
||
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
||
log, buf := captureLog()
|
||
ad := NewAnthropicChat(
|
||
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
||
WithAnthropicLogger(log))
|
||
|
||
const budget = 4096
|
||
// max_tokens 恰好等于预算:没有这道守护,整个预算会被
|
||
// 思考吃光,答案将空着返回。
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "claude-test",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
MaxTokens: budget,
|
||
Thinking: &Thinking{BudgetTokens: budget},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
body := (<-ch).body
|
||
think, ok := body["thinking"].(map[string]any)
|
||
if !ok {
|
||
t.Fatalf("body.thinking = %v, want an enabled directive", body["thinking"])
|
||
}
|
||
if think["type"] != anthropicThinkingTypeEnabled {
|
||
t.Errorf("thinking.type = %v, want %q", think["type"], anthropicThinkingTypeEnabled)
|
||
}
|
||
gotBudget, _ := think["budget_tokens"].(float64)
|
||
maxTokens, _ := body["max_tokens"].(float64)
|
||
if int(gotBudget) != budget {
|
||
t.Errorf("thinking.budget_tokens = %v, want %d", gotBudget, budget)
|
||
}
|
||
if maxTokens <= gotBudget {
|
||
t.Fatalf("max_tokens = %v, want > budget_tokens %v (thinking would eat the answer)", maxTokens, gotBudget)
|
||
}
|
||
if maxTokens < gotBudget+anthropicThinkingBodyReserve {
|
||
t.Errorf("max_tokens = %v, want >= budget %v + body reserve %d", maxTokens, gotBudget, anthropicThinkingBodyReserve)
|
||
}
|
||
if !strings.Contains(buf.String(), "max_tokens raised") {
|
||
t.Errorf("log = %q, want a WARN explaining the max_tokens raise", buf.String())
|
||
}
|
||
}
|
||
|
||
// TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged 钉住未饱和
|
||
// 的情形:调用方的 max_tokens 本已留出空间时,它原样发送,
|
||
// 而预算严格低于它。
|
||
func TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan anthropicCaptured, 1)
|
||
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
||
log, buf := captureLog()
|
||
ad := NewAnthropicChat(
|
||
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
||
WithAnthropicLogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "claude-test",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
MaxTokens: anthropicDefaultMaxTokens,
|
||
Thinking: &Thinking{BudgetTokens: 4096},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
body := (<-ch).body
|
||
think := body["thinking"].(map[string]any)
|
||
gotBudget, _ := think["budget_tokens"].(float64)
|
||
maxTokens, _ := body["max_tokens"].(float64)
|
||
if maxTokens != float64(anthropicDefaultMaxTokens) {
|
||
t.Errorf("max_tokens = %v, want the requested %d (unchanged)", maxTokens, anthropicDefaultMaxTokens)
|
||
}
|
||
if gotBudget >= maxTokens {
|
||
t.Errorf("budget_tokens %v >= max_tokens %v", gotBudget, maxTokens)
|
||
}
|
||
if strings.Contains(buf.String(), "max_tokens raised") {
|
||
t.Errorf("unexpected adjustment log: %q", buf.String())
|
||
}
|
||
}
|
||
|
||
// TestAnthropicChat_ThinkingDisabledOmitsField 断言显式关闭开关
|
||
// 完全不发送 thinking 指令(Anthropic 把字段缺失视为关闭)。
|
||
func TestAnthropicChat_ThinkingDisabledOmitsField(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan anthropicCaptured, 1)
|
||
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
||
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"})
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "claude-test",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
Thinking: &Thinking{BudgetTokens: 4096, Enable: boolPtr(false)},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
if body := (<-ch).body; body["thinking"] != nil {
|
||
t.Fatalf("body.thinking = %v, want the field omitted", body["thinking"])
|
||
}
|
||
}
|
||
|
||
// TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum 断言低于
|
||
// API 最小值的预算会被抬高(照发会 400)并上报。
|
||
func TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan anthropicCaptured, 1)
|
||
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
||
log, buf := captureLog()
|
||
ad := NewAnthropicChat(
|
||
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
||
WithAnthropicLogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "claude-test",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
Thinking: &Thinking{BudgetTokens: 512},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
think := (<-ch).body["thinking"].(map[string]any)
|
||
if got, _ := think["budget_tokens"].(float64); int(got) != anthropicMinThinkingBudget {
|
||
t.Errorf("thinking.budget_tokens = %v, want the minimum %d", got, anthropicMinThinkingBudget)
|
||
}
|
||
if !strings.Contains(buf.String(), "protocol minimum") {
|
||
t.Errorf("log = %q, want a WARN about the minimum budget", buf.String())
|
||
}
|
||
}
|
||
|
||
// TestOpenAIChat_ReasoningEffortPassthrough 断言已知提供商上的
|
||
// 受支持档位被原样转发,且不发出警告。
|
||
func TestOpenAIChat_ReasoningEffortPassthrough(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan capturedRequest, 1)
|
||
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
||
log, buf := captureLog()
|
||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-test"}, WithOpenAILogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "gpt-4o",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
ReasoningEffort: "high",
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
if got := (<-ch).body["reasoning_effort"]; got != "high" {
|
||
t.Fatalf("body.reasoning_effort = %v, want high", got)
|
||
}
|
||
// 用量日志(每次调用一条 Info)不算「意外日志」:这里断言的是**没有 WARN**
|
||
// —— 受支持的档位不该被上报成问题。
|
||
if strings.Contains(buf.String(), "level=WARN") {
|
||
t.Errorf("unexpected WARN for a supported value: %q", buf.String())
|
||
}
|
||
}
|
||
|
||
// TestOpenAIChat_ReasoningEffortAliasMappedForDeepSeek 断言 DeepSeek 的
|
||
// 兼容别名被归一化为服务采用的档位(按官方映射表 medium →
|
||
// high)并上报,绝不按我们自己挑的
|
||
// 更弱档位降级。
|
||
func TestOpenAIChat_ReasoningEffortAliasMappedForDeepSeek(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan capturedRequest, 1)
|
||
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
||
log, buf := captureLog()
|
||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "deepseek-flash"}, WithOpenAILogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "deepseek-flash",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
ReasoningEffort: "medium",
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
if got := (<-ch).body["reasoning_effort"]; got != "high" {
|
||
t.Fatalf("body.reasoning_effort = %v, want the officially mapped high", got)
|
||
}
|
||
rec := buf.String()
|
||
for _, want := range []string{"level=WARN", "reasoning_effort", "deepseek", "medium", "high"} {
|
||
if !strings.Contains(rec, want) {
|
||
t.Errorf("log %q missing %q", rec, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider 断言无法识别的
|
||
// 提供商/模型会让请求保持不动,并上报被
|
||
// 忽略的字段(design/04 §3.2:绝不静默失效)。
|
||
func TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan capturedRequest, 1)
|
||
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
||
log, buf := captureLog()
|
||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "mystery-1"}, WithOpenAILogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "mystery-1",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
ReasoningEffort: "high",
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
body := (<-ch).body
|
||
if _, present := body["reasoning_effort"]; present {
|
||
t.Fatalf("body.reasoning_effort = %v, want the field dropped", body["reasoning_effort"])
|
||
}
|
||
rec := buf.String()
|
||
for _, want := range []string{"level=WARN", "reasoning_effort", "unknown", "high"} {
|
||
if !strings.Contains(rec, want) {
|
||
t.Errorf("log %q missing %q", rec, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// TestOpenAIChat_QwenEnableThinkingReportedNotForwarded 记录了针对 Qwen3
|
||
// 硬开关的刻意选择:OpenAI 兼容协议没有 enable_thinking 的
|
||
// 可移植线格式,所以本版本上报它,并让请求
|
||
// 逐字节保持原样,而不是去猜一个可能导致 400 的形状。
|
||
func TestOpenAIChat_QwenEnableThinkingReportedNotForwarded(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan capturedRequest, 1)
|
||
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
||
log, buf := captureLog()
|
||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "qwen3-max"}, WithOpenAILogger(log))
|
||
|
||
plain := make(chan capturedRequest, 1)
|
||
plainSrv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, plain)
|
||
plainAd := NewOpenAIChat(config.LLMConfig{BaseURL: plainSrv.URL, Model: "qwen3-max"})
|
||
if _, err := plainAd.Chat(context.Background(), Request{
|
||
Model: "qwen3-max",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "qwen3-max",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
Thinking: &Thinking{Enable: boolPtr(false)},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
body := (<-ch).body
|
||
for _, key := range []string{"enable_thinking", "chat_template_kwargs", "extra_body", "thinking"} {
|
||
if _, present := body[key]; present {
|
||
t.Errorf("body.%s = %v, want the request unchanged", key, body[key])
|
||
}
|
||
}
|
||
if len(body) != len((<-plain).body) {
|
||
t.Errorf("request shape changed with a thinking directive: %v", body)
|
||
}
|
||
rec := buf.String()
|
||
for _, want := range []string{"level=WARN", "enable_thinking", "qwen"} {
|
||
if !strings.Contains(rec, want) {
|
||
t.Errorf("log %q missing %q", rec, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// TestOpenAIChat_ThinkingBudgetReportedIgnored 断言 token 预算
|
||
// (Anthropic 风格的指令)在 OpenAI 兼容端点上会被上报为
|
||
// 无法映射,而不是被静默丢弃。
|
||
func TestOpenAIChat_ThinkingBudgetReportedIgnored(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan capturedRequest, 1)
|
||
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
||
log, buf := captureLog()
|
||
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-4o"}, WithOpenAILogger(log))
|
||
|
||
if _, err := ad.Chat(context.Background(), Request{
|
||
Model: "gpt-4o",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
Thinking: &Thinking{BudgetTokens: 8192},
|
||
}, Options{}); err != nil {
|
||
t.Fatalf("Chat error: %v", err)
|
||
}
|
||
if _, present := (<-ch).body["thinking"]; present {
|
||
t.Fatal("body.thinking was sent to an OpenAI-compatible endpoint")
|
||
}
|
||
rec := buf.String()
|
||
for _, want := range []string{"level=WARN", "thinking.budget_tokens", "gpt"} {
|
||
if !strings.Contains(rec, want) {
|
||
t.Errorf("log %q missing %q", rec, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer 在流式
|
||
// 路径上跑同一条红线检查,该路径会自建请求体。
|
||
func TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) {
|
||
t.Parallel()
|
||
ch := make(chan anthropicCaptured, 1)
|
||
const sse = "event: message_start\ndata: {\"type\":\"message_start\"}\n\n" +
|
||
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n" +
|
||
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n" +
|
||
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
|
||
srv := newAnthropicServer(t, http.StatusOK, "text/event-stream", sse, ch)
|
||
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"})
|
||
|
||
if _, err := ad.ChatStream(context.Background(), Request{
|
||
Model: "claude-test",
|
||
Messages: []Message{TextMessage(RoleUser, "hi")},
|
||
MaxTokens: 2048,
|
||
Thinking: &Thinking{BudgetTokens: 2048},
|
||
}, func(StreamEvent) {}); err != nil {
|
||
t.Fatalf("ChatStream error: %v", err)
|
||
}
|
||
body := (<-ch).body
|
||
think, ok := body["thinking"].(map[string]any)
|
||
if !ok {
|
||
t.Fatalf("stream body.thinking = %v, want an enabled directive", body["thinking"])
|
||
}
|
||
gotBudget, _ := think["budget_tokens"].(float64)
|
||
maxTokens, _ := body["max_tokens"].(float64)
|
||
if gotBudget >= maxTokens {
|
||
t.Fatalf("stream max_tokens = %v, want > budget_tokens %v", maxTokens, gotBudget)
|
||
}
|
||
}
|
||
|
||
// TestReasoningVendorFor_Classification 锁住白名单所依赖的厂商
|
||
// 检测,包括回退到 base URL 的那条路径。
|
||
func TestReasoningVendorFor_Classification(t *testing.T) {
|
||
t.Parallel()
|
||
|
||
cases := []struct {
|
||
model string
|
||
baseURL string
|
||
want reasoningVendor
|
||
}{
|
||
{model: "deepseek-v4-pro", want: vendorDeepSeek},
|
||
{model: "Qwen3-Max", want: vendorQwen},
|
||
{model: "gpt-4o", want: vendorOpenAI},
|
||
{model: "o3-mini", want: vendorOpenAI},
|
||
{model: "chatgpt-4o-latest", want: vendorOpenAI},
|
||
{model: "claude-sonnet-4", baseURL: "https://api.openai.com/v1", want: vendorOpenAI},
|
||
{model: "qwen-plus", baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1", want: vendorQwen},
|
||
{model: "some-model", baseURL: "https://api.deepseek.com/v1", want: vendorDeepSeek},
|
||
{model: "some-model", baseURL: "http://localhost:8000/v1", want: vendorUnknown},
|
||
}
|
||
for _, tt := range cases {
|
||
if got := reasoningVendorFor(tt.model, tt.baseURL); got != tt.want {
|
||
t.Errorf("reasoningVendorFor(%q, %q) = %q, want %q", tt.model, tt.baseURL, got, tt.want)
|
||
}
|
||
}
|
||
}
|