Files
Cyrene_v2/internal/runtime/model/persona_router_test.go
T
AskaEth fa33c79e11
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(model,capacity,vision,io): per-persona 路由 + 本地三级(默认关) + L3 分类 + OCR 独立模型 + image-crop 工具(裁决 03.4.1/03.4.3/14.4.1/14.4.4/14.4.5)
- 03.4.1:per-persona 路由载体选 models.json(组内新增 personaModels/personaProviders + 校验 + PersonaRoute()),
  Router/RuntimeLLM 快照绑定、loop 流式路径按该 turn 的 persona 路由;主步用路由结果、降级步钉自己的模型
- 03.4.3:本地第三级降级链(LLM_LOCAL_*,Configured() 含 Enabled ⇒ **默认关**),warn 标注来源
- 14.4.4:capacity 接入 L3(分类器接口 + TTL 缓存 + 严格 JSON 解析),CAPACITY_L3_ENABLED **默认 false**
- 14.4.1:语义修正 —— group.multimodal 才是「主模型能否收图」(env 路径不再臆断),LLM_VISION_MODEL 只配视觉工具;
  vision/OCR 可各用不同模型;runtime 不再因有图抢路由(vision.Router 是唯一分层权威)
- 14.4.5:新增 image-crop 工具(工具数 5→6,策略默认 owner-only),schema 支持按区域裁切
- 组合根:wire_runtime 读 group.Multimodal;capacity 装配拆出 wire_capacity.go(守 500 行)
- 复核:`go build`/`go vet`/各包 `go test -count=1` 全绿;**6 条变异反证**先红后还原(含作者自查出的 2 条假阳性变异)
- 未落:按 provider 的 thinking 开关(需 openai_encode.go/openai.go 文件面)、persona awakening(需 loader 文件面)
2026-10-04 12:44:52 +08:00

120 lines
4.9 KiB
Go

package model
import (
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config/runtimeconfig"
)
// TestRouter_RouteRequestForPersona pins 裁决 03.4.1: the "role" of per-role
// routing is the persona a turn answers as, and a persona without a binding keeps
// the message-role behavior.
func TestRouter_RouteRequestForPersona(t *testing.T) {
t.Parallel()
r := NewRouter("default",
WithRoleModel(RoleUser, "user-model"),
WithPersonaModel("cyrene", "persona-model"),
WithPersonaProvider("cyrene", ProviderAnthropic),
WithDefaultProvider(ProviderOpenAI),
)
req := Request{Messages: []Message{TextMessage(RoleUser, "hi")}}
if got, _ := r.RouteRequestForPersona("cyrene", req); got != "persona-model" {
t.Errorf("RouteRequestForPersona(cyrene) = %q, want persona-model", got)
}
if got, _ := r.RouteRequestForPersona("other", req); got != "user-model" {
t.Errorf("RouteRequestForPersona(other) = %q, want user-model (role fallback)", got)
}
if got, _ := r.RouteRequestForPersona("", req); got != "user-model" {
t.Errorf("RouteRequestForPersona(\"\") = %q, want user-model", got)
}
if got := r.RouteForPersona("cyrene"); got != "persona-model" {
t.Errorf("RouteForPersona(cyrene) = %q, want persona-model", got)
}
if got := r.RouteProviderForPersona("cyrene"); got != ProviderAnthropic {
t.Errorf("RouteProviderForPersona(cyrene) = %q, want anthropic", got)
}
if got := r.RouteProviderForPersona("other"); got != ProviderOpenAI {
t.Errorf("RouteProviderForPersona(other) = %q, want the default provider openai", got)
}
}
// TestRouter_PersonaBindingIgnoresBlankAndImageDoesNotSwitch pins two edges: a
// blank persona/model cannot create a binding, and a persona-routed image request
// is not silently re-routed to a vision model (that decision belongs to
// vision.Router, 裁决 14.4.1/14.4.5).
func TestRouter_PersonaBindingIgnoresBlankAndImageDoesNotSwitch(t *testing.T) {
t.Parallel()
r := NewRouter("default",
WithPersonaModel(" ", "x"),
WithPersonaModel("cyrene", ""),
WithVisionModel("vision-model"),
)
if got := r.RouteForPersona("cyrene"); got != "default" {
t.Errorf("RouteForPersona(cyrene) = %q, want default (no binding stored)", got)
}
img := Request{Messages: []Message{ImageMessage(RoleUser, "", "data:image/png;base64,abc")}}
got, needsVision := r.RouteRequestForPersona("cyrene", img)
if !needsVision {
t.Error("needsVision = false, want true for an image-bearing request")
}
if got == "vision-model" {
t.Errorf("RouteRequestForPersona = %q, want the persona/role model, not the vision model", got)
}
}
// TestRuntimeLLM_PersonaRoutesFromModelsJSON pins the carrier: a per-persona
// binding written on the models.json group reaches the router through the hot
// snapshot, and a reload picks up a changed binding (裁决 03.4.1 载体 = models.json)。
func TestRuntimeLLM_PersonaRoutesFromModelsJSON(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
PersonaModels: map[string]string{"cyrene": "persona-model"},
PersonaProviders: map[string]string{"cyrene": ProviderAnthropic},
}})
m := NewRuntimeLLM(path, rtEnv(), nil)
req := Request{Messages: []Message{TextMessage(RoleUser, "hi")}}
got, _ := m.RouteRequestForPersona("cyrene", req)
if got != "persona-model" {
t.Fatalf("RouteRequestForPersona(cyrene) = %q, want persona-model", got)
}
if got, _ := m.RouteRequestForPersona("other", req); got != "group-model" {
t.Fatalf("RouteRequestForPersona(other) = %q, want group-model", got)
}
if err := runtimeconfig.SaveModels(path, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
}}); err != nil {
t.Fatalf("SaveModels: %v", err)
}
if err := m.Reload(); err != nil {
t.Fatalf("Reload: %v", err)
}
if got, _ := m.RouteRequestForPersona("cyrene", req); got != "group-model" {
t.Fatalf("after reload RouteRequestForPersona(cyrene) = %q, want group-model (binding removed)", got)
}
}
// TestRuntimeLLM_NoVisionPreemptionOnImage pins that the runtime snapshot no longer
// carries the env vision model: a configured LLM_VISION_MODEL must not switch an
// image-bearing request to another model, because image routing is owned by
// vision.Router (裁决 14.4.1/14.4.5).
func TestRuntimeLLM_NoVisionPreemptionOnImage(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
}})
env := rtEnv()
env.VisionModel = "vision-model"
m := NewRuntimeLLM(path, env, nil)
req := Request{Messages: []Message{ImageMessage(RoleUser, "", "data:image/png;base64,abc")}}
got, needsVision := m.RouteRequest(req)
if !needsVision {
t.Error("needsVision = false, want true for an image-bearing request")
}
if got != "group-model" {
t.Errorf("RouteRequest = %q, want group-model (no vision pre-emption)", got)
}
}