From 454f0cdd3645e378ee22304f1ffbefc8e2d3018e Mon Sep 17 00:00:00 2001 From: guyu233 Date: Wed, 25 Nov 2026 23:05:20 +0800 Subject: [PATCH 1/4] providers: degrade undeclared reasoning levels downward (CC Switch ladder) --- .../unreleased/reasoning-effort-degrade.md | 7 ++ internal/providers/kimi/flows_test.go | 2 +- internal/providers/reasoning.go | 75 ++++++++++++++----- internal/providers/reasoning_test.go | 48 +++++++++++- .../workbuddy/thinking_toggle_test.go | 15 ++-- 5 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 changelog/unreleased/reasoning-effort-degrade.md diff --git a/changelog/unreleased/reasoning-effort-degrade.md b/changelog/unreleased/reasoning-effort-degrade.md new file mode 100644 index 00000000..131d8b0f --- /dev/null +++ b/changelog/unreleased/reasoning-effort-degrade.md @@ -0,0 +1,7 @@ +### English + +Reasoning effort now degrades the CC Switch way: when a client asks for a level the model does not declare, the gateway sends the nearest lower level the model does advertise and never upgrades. A request below the model's floor (for example `reasoning_effort: none` against a model that only offers low/high/max) lands on the cheapest declared level instead of the model default, so "do not think hard" no longer becomes full-strength thinking. Levels the model declares are forwarded untouched, and `max` is never downgraded. + +### 中文 + +思考强度改为按 CC Switch 的方式降级:客户端要求模型没有声明的档位时,网关下发它总声明过的最近更低档,且永不升档。请求低于模型最低档时(例如对只提供 low/high/max 的模型传 `reasoning_effort: none`),落到最低可用档而不是模型默认档,因此「不要高强度思考」不会再变成满档思考。模型声明了的档位原样下发,`max` 永不被降级。 diff --git a/internal/providers/kimi/flows_test.go b/internal/providers/kimi/flows_test.go index bc08f4b9..0b3a41f9 100644 --- a/internal/providers/kimi/flows_test.go +++ b/internal/providers/kimi/flows_test.go @@ -835,7 +835,7 @@ func TestReasoningPriority(t *testing.T) { {"catalog default when nothing is saved", "", "", "high"}, {"saved console default beats the catalog default", "low", "", "low"}, {"client value beats the saved default", "low", "high", "high"}, - {"saved default is clamped to the declared ladder", "medium", "", "high"}, + {"saved default is clamped to the declared ladder", "medium", "", "low"}, {"client value is clamped to the declared ladder", "", "xhigh", "high"}, } for _, tc := range cases { diff --git a/internal/providers/reasoning.go b/internal/providers/reasoning.go index 5aa6f964..a90d1078 100644 --- a/internal/providers/reasoning.go +++ b/internal/providers/reasoning.go @@ -108,29 +108,51 @@ func ResolveReasoningLevel(level string, caps ModelCapabilities) string { } normalized := NormalizeReasoningLevel(level) if normalized != "" { - for _, option := range caps.ReasoningOptions { - if option == normalized { - return normalized - } + if containsReasoningOption(caps.ReasoningOptions, normalized) { + return normalized } - if chain := reasoningDegradationChain(normalized); chain != nil { - for _, candidate := range chain { + // Declared ladder: a level the model does not advertise degrades to + // the nearest lower level it does advertise (CC Switch behavior), and + // never upgrades. A request below the model's floor (for example "none" + // against low/high/max) lands on the lowest declared level instead of + // the model default, so "do not think hard" never becomes "think hard". + if rank := reasoningRank(normalized); rank >= 0 { + for _, candidate := range reasoningDegradationChain(normalized) { if containsReasoningOption(caps.ReasoningOptions, candidate) { return candidate } } + if lowest := lowestReasoningOption(caps); lowest != "" { + return lowest + } } } if fallback := NormalizeReasoningLevel(caps.ReasoningDefault); fallback != "" { - for _, option := range caps.ReasoningOptions { - if option == fallback { - return fallback - } + if containsReasoningOption(caps.ReasoningOptions, fallback) { + return fallback } } return caps.ReasoningOptions[0] } +// lowestReasoningOption returns the cheapest declared level in the neutral +// vocabulary, so a client asking for less thinking than a model supports still +// gets the minimum instead of the model's default tier. +func lowestReasoningOption(caps ModelCapabilities) string { + lowest := "" + lowestRank := len(reasoningLadder) + for _, option := range caps.ReasoningOptions { + normalized := NormalizeReasoningLevel(option) + if normalized == "" { + continue + } + if rank := reasoningRank(normalized); rank >= 0 && rank < lowestRank { + lowest, lowestRank = normalized, rank + } + } + return lowest +} + func containsReasoningOption(options []string, level string) bool { for _, option := range options { if NormalizeReasoningLevel(option) == level { @@ -148,16 +170,31 @@ func ReasoningOptionIncludes(options []string, level string) bool { return containsReasoningOption(options, NormalizeReasoningLevel(level)) } -// reasoningDegradationChain returns lower tiers ordered from nearest to -// farthest. Only high tiers need explicit degradation; lower/unknown values -// fall through to the model default in ResolveReasoningLevel. +// reasoningLadder is the neutral effort ladder from cheapest to most expensive. +// CC Switch uses the same tiers when converting effort into thinking budgets +// (2048 / 8192 / 16384 / 24576), so ranking them here keeps one vocabulary. +var reasoningLadder = []string{"none", "low", "medium", "high", "xhigh", "max"} + +func reasoningRank(level string) int { + for i, candidate := range reasoningLadder { + if candidate == level { + return i + } + } + return -1 +} + +// reasoningDegradationChain returns every lower tier ordered from nearest to +// farthest, and nil for an unknown level. Callers pick the first one the model +// actually declares. func reasoningDegradationChain(level string) []string { - switch level { - case "max": - return []string{"xhigh", "high", "medium", "low", "none"} - case "xhigh": - return []string{"high", "medium", "low", "none"} - default: + rank := reasoningRank(level) + if rank <= 0 { return nil } + chain := make([]string, 0, rank) + for i := rank - 1; i >= 0; i-- { + chain = append(chain, reasoningLadder[i]) + } + return chain } diff --git a/internal/providers/reasoning_test.go b/internal/providers/reasoning_test.go index 26f40e99..d5eb50dc 100644 --- a/internal/providers/reasoning_test.go +++ b/internal/providers/reasoning_test.go @@ -43,10 +43,10 @@ func TestReasoningLevelFromThinkingBudget(t *testing.T) { } } -func TestResolveReasoningLevelKeepsOlderFallbackForUnknownAndLowerTiers(t *testing.T) { +func TestResolveReasoningLevelDegradesLowerTiersToNearestDeclared(t *testing.T) { caps := ModelCapabilities{ReasoningOptions: []string{"low", "high"}, ReasoningDefault: "high"} - if got := ResolveReasoningLevel("medium", caps); got != "high" { - t.Fatalf("medium clamp=%q, want default high", got) + if got := ResolveReasoningLevel("medium", caps); got != "low" { + t.Fatalf("medium clamp=%q, want low", got) } if got := ResolveReasoningLevel("bogus", caps); got != "high" { t.Fatalf("unknown clamp=%q, want default high", got) @@ -55,3 +55,45 @@ func TestResolveReasoningLevelKeepsOlderFallbackForUnknownAndLowerTiers(t *testi t.Fatalf("empty caps clamp=%q, want empty", got) } } + +func TestResolveReasoningLevelBelowFloorUsesLowestDeclared(t *testing.T) { + // deepseek-v4.1-flash on WorkBuddy advertises low/high/max with high as the + // default. A client that disables thinking satisfies none of those, and the + // answer is the cheapest declared tier — never the default high. + caps := ModelCapabilities{ReasoningOptions: []string{"low", "high", "max"}, ReasoningDefault: "high"} + if got := ResolveReasoningLevel("none", caps); got != "low" { + t.Fatalf("none clamp=%q, want low", got) + } + if got := ResolveReasoningLevel("off", caps); got != "low" { + t.Fatalf("off clamp=%q, want low", got) + } + if got := ResolveReasoningLevel("max", caps); got != "max" { + t.Fatalf("max clamp=%q, want max (never downgraded)", got) + } + if got := ResolveReasoningLevel("", caps); got != "high" { + t.Fatalf("empty request=%q, want default high", got) + } +} + +func TestResolveReasoningLevelKeepsDeclaredOff(t *testing.T) { + caps := ModelCapabilities{ReasoningOptions: []string{"none", "low", "high"}, ReasoningDefault: "high"} + if got := ResolveReasoningLevel("none", caps); got != "none" { + t.Fatalf("none on an off-capable model=%q, want none", got) + } +} + +func TestResolveReasoningLevelDegradesDownOnly(t *testing.T) { + caps := ModelCapabilities{ReasoningOptions: []string{"low", "medium", "high"}, ReasoningDefault: "medium"} + for requested, want := range map[string]string{ + "max": "high", + "xhigh": "high", + "high": "high", + "medium": "medium", + "low": "low", + "none": "low", + } { + if got := ResolveReasoningLevel(requested, caps); got != want { + t.Fatalf("%s resolved to %q, want %q", requested, got, want) + } + } +} diff --git a/internal/providers/workbuddy/thinking_toggle_test.go b/internal/providers/workbuddy/thinking_toggle_test.go index ffa57c5c..e9ca7f4a 100644 --- a/internal/providers/workbuddy/thinking_toggle_test.go +++ b/internal/providers/workbuddy/thinking_toggle_test.go @@ -18,12 +18,12 @@ func TestApplyChatReasoningDisabledThinking(t *testing.T) { CanDisableThinking: false, } disabled := translate.ChatRequest{ - Model: "deepseek-v4.1-flash", + Model: "deepseek-v4.1-flash", Thinking: json.RawMessage(`{"type":"disabled"}`), } obj := map[string]any{ - "reasoning": map[string]any{"effort": "stale"}, - "reasoning_effort": "max", + "reasoning": map[string]any{"effort": "stale"}, + "reasoning_effort": "max", "reasoning_summary": "auto", "verbosity": "high", } @@ -46,8 +46,9 @@ func TestApplyChatReasoningDisabledDoesNotHijackLevels(t *testing.T) { ReasoningOptions: []string{"low", "high", "max"}, ReasoningDefault: "high", } - // Negative controls: enabled thinking and reasoning_effort:none must keep - // the existing catalog-clamped behaviour. + // Negative controls: enabled thinking keeps the catalog default, while + // reasoning_effort:none degrades to the cheapest declared tier instead of + // being clamped back up to the default. for _, tc := range []struct { name string req translate.ChatRequest @@ -56,9 +57,9 @@ func TestApplyChatReasoningDisabledDoesNotHijackLevels(t *testing.T) { {"enabled thinking", translate.ChatRequest{ Model: "deepseek-v4.1-flash", Thinking: json.RawMessage(`{"type":"enabled"}`), }, "high"}, - {"effort none clamped", translate.ChatRequest{ + {"effort none degrades to lowest declared", translate.ChatRequest{ Model: "deepseek-v4.1-flash", ReasoningEffort: json.RawMessage(`"none"`), - }, "high"}, + }, "low"}, {"effort low preserved", translate.ChatRequest{ Model: "deepseek-v4.1-flash", ReasoningEffort: json.RawMessage(`"low"`), }, "low"}, From 4ea57865164f31e745cf2009346f6811a3ee18f8 Mon Sep 17 00:00:00 2001 From: guyu233 Date: Thu, 26 Nov 2026 10:02:54 +0800 Subject: [PATCH 2/4] docs: state the CC Switch reasoning pass-through rule --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 1d9a1811..0a7e5002 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -48,7 +48,7 @@ live-account acceptance, and release status separate. - Keep architecture: auth / endpoint / executor / translate, plus `internal/store` (SQLite), `internal/control` (console facade: accounts, keys, settings, catalog, login, import), `internal/runtime` (Manager lifecycle), `internal/providers/qoder` (Qoder HOME/CLI/worker protocol and Adapter), `internal/gateway` (public protocol HTTP), `internal/console` (operator HTTP), `internal/server` (routes/middleware/webui), and `internal/app` (process assembly). `internal/api` is a test-only compatibility facade (`api.New` → `app.New`); do not add business there. Account entities stay in `accounts`; Pool/Item/RouteQuery/Classify and request Prepare live in `executor`. Display catalog cache lives in `control.Catalog`; catalog aggregation, identity filter, and settings decoration live in `control`, not `app`. Public `/v1/chat/completions`, `/v1/messages`, `/v1/responses`, and `/v1/models` live in `gateway`; console `/api/*` lives in `console`; `internal/server` registers both. Console HTTP decodes and maps errors; persist/apply for system settings and console-key rotation live in `control.System` / `control.KeyRotation`. Update job/maintenance lives in `internal/update.Coordinator`. SQLite lives in `internal/store`; process tables live in runtime. Runtime constructs the one Pool and injects it into executor. PAT-backed Qoder accounts (`qoder-pat-v1`) run login, catalog, quota, check-in, and chat in-process through the qauth Bridge; do not spawn a Node worker for them. Accounts that still carry the legacy `qoder-native-v1` CLI blob keep one child process per HOME until the operator re-logs them with a PAT. The registered Adapter includes a Prober and sends chat through the in-process COSY client. `cmd/server` constructs `app.New`. `accounts` must not import `runtime` or `executor`. Executor Prepare must not take `*http.Request` or import store. Gateway, console, and server must not import store or runtime Manager. App/server/gateway/console must not import `internal/api`. Provider packages must not receive `http.ResponseWriter` or import executor taxonomy; OAuth loopback HTTP stays in `auth.ServeLoopback`. Adapter error classification and cooldown math live in `executor`; gateway only formats the result. - Prefer direct HTTP/SSE to Qoder cloud APIs - Pin qodercli / qoderclicn hooks in `worker/src/compat.mjs`; fail loudly on mismatch. Qoder CN is `provider=qoder` + `region=cn`, not a new family -- Reasoning levels are catalog-driven: map client values through `internal/providers/reasoning.go` (`none`/`low`/`medium`/`high`/`xhigh`/`max`), clamp anything the model does not allow back to an allowed level, and treat the console value as a default only (it never locks a call or caps a higher client value) +- Reasoning levels follow CC Switch: a level the client sends is forwarded untouched whenever the model declares it, an undeclared level degrades to the nearest lower declared tier (never upward, never to the model default), a request below the floor lands on the cheapest declared tier, and a reasoning mismatch must never move the request to another provider - Console UI: React + Tailwind v4 + **HeroUI only** for components - Follow `docs/DESIGN.md` (taste v1 adapted for this console) - Keep iterating Qoder login, usage, and account routing. Keep scheduling focused on personal-account routing rather than commercial gateway features From ea6d983251562cc52dd36bae2500469453cecdda Mon Sep 17 00:00:00 2001 From: guyu233 Date: Fri, 27 Nov 2026 09:39:22 +0800 Subject: [PATCH 3/4] providers: never upgrade a client reasoning level --- .../reasoning-ccswitch-passthrough.md | 7 +++ internal/providers/reasoning.go | 19 ++++++++ internal/providers/reasoning_test.go | 44 +++++++++++++++++++ internal/providers/workbuddy/mapping.go | 7 ++- .../workbuddy/thinking_toggle_test.go | 19 ++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/reasoning-ccswitch-passthrough.md diff --git a/changelog/unreleased/reasoning-ccswitch-passthrough.md b/changelog/unreleased/reasoning-ccswitch-passthrough.md new file mode 100644 index 00000000..2cdbf00d --- /dev/null +++ b/changelog/unreleased/reasoning-ccswitch-passthrough.md @@ -0,0 +1,7 @@ +### English + +- Reasoning effort now follows the CC Switch rule end to end: a level the client sends is passed through untouched whenever the model declares it, an undeclared level only degrades to the nearest lower declared level, and it is never promoted to the model default — including the WorkBuddy "none" path that used to fall back to the default tier. + +### 中文 + +- 思考强度按 CC Switch 规则处理:客户端传来的档位只要模型声明过就原样透传,未声明时只向下退到最近的已声明档位,绝不再被提升为模型默认档——WorkBuddy 原先回落到默认档的「none」分支也一并修正。 diff --git a/internal/providers/reasoning.go b/internal/providers/reasoning.go index a90d1078..79ba0cf0 100644 --- a/internal/providers/reasoning.go +++ b/internal/providers/reasoning.go @@ -153,6 +153,25 @@ func lowestReasoningOption(caps ModelCapabilities) string { return lowest } +// LowestThinkingOption returns the cheapest declared level that still thinks. +// Adapters use it when a client asks for "none" on a model that cannot turn +// thinking off: the level degrades down to this tier and is never promoted to +// the model default, which could be higher than what the client asked for. +func LowestThinkingOption(caps ModelCapabilities) string { + lowest := "" + lowestRank := len(reasoningLadder) + for _, option := range caps.ReasoningOptions { + normalized := NormalizeReasoningLevel(option) + if normalized == "" || normalized == "none" { + continue + } + if rank := reasoningRank(normalized); rank >= 0 && rank < lowestRank { + lowest, lowestRank = normalized, rank + } + } + return lowest +} + func containsReasoningOption(options []string, level string) bool { for _, option := range options { if NormalizeReasoningLevel(option) == level { diff --git a/internal/providers/reasoning_test.go b/internal/providers/reasoning_test.go index d5eb50dc..1818a8c4 100644 --- a/internal/providers/reasoning_test.go +++ b/internal/providers/reasoning_test.go @@ -97,3 +97,47 @@ func TestResolveReasoningLevelDegradesDownOnly(t *testing.T) { } } } + +// TestResolveReasoningLevelNeverUpgrades guards the CC Switch rule: a level the +// client sent is passed through when the model declares it, and otherwise only +// ever degrades downward. Replying with a stronger level than requested is never +// acceptable, whatever the model's default happens to be. +func TestResolveReasoningLevelNeverUpgrades(t *testing.T) { + sets := [][]string{ + {"low", "high"}, + {"none", "low", "high"}, + {"low", "high", "max"}, + {"none", "low", "medium", "high", "xhigh", "max"}, + } + for _, options := range sets { + caps := ModelCapabilities{ReasoningOptions: options, ReasoningDefault: "high"} + for _, requested := range reasoningLadder { + requestedRank := reasoningRank(requested) + resolved := ResolveReasoningLevel(requested, caps) + if resolved == "" { + t.Fatalf("options=%v requested=%s resolved empty", options, requested) + } + if requestedRank == 0 { + // "do not think" below the model floor can only land on the + // cheapest declared tier; it must never be the default tier. + if got := reasoningRank(resolved); got != reasoningRank(lowestReasoningOption(caps)) { + t.Fatalf("options=%v requested=%s resolved=%s, want the model floor", options, requested, resolved) + } + continue + } + if got := reasoningRank(resolved); got > requestedRank { + t.Fatalf("options=%v requested=%s resolved=%s upgraded", options, requested, resolved) + } + } + } +} + +func TestLowestThinkingOptionSkipsNone(t *testing.T) { + caps := ModelCapabilities{ReasoningOptions: []string{"none", "medium", "high"}} + if got := LowestThinkingOption(caps); got != "medium" { + t.Fatalf("lowest thinking option=%q want medium", got) + } + if got := LowestThinkingOption(ModelCapabilities{ReasoningOptions: []string{"none"}}); got != "" { + t.Fatalf("lowest thinking option=%q want empty", got) + } +} diff --git a/internal/providers/workbuddy/mapping.go b/internal/providers/workbuddy/mapping.go index fb0f16a0..9c6fedf8 100644 --- a/internal/providers/workbuddy/mapping.go +++ b/internal/providers/workbuddy/mapping.go @@ -52,8 +52,11 @@ func applyChatReasoning(obj map[string]any, req translate.ChatRequest, storedLev } if level == "none" { if !caps.CanDisableThinking { - level = providers.ResolveReasoningLevel(caps.ReasoningDefault, caps) - if level == "" || level == "none" { + // The model declares "none" but cannot actually turn thinking off. + // Degrade down to the cheapest thinking tier; never fall back to the + // model default, which could upgrade the level the client asked for. + level = providers.LowestThinkingOption(caps) + if level == "" { clearChatReasoning(obj) return "" } diff --git a/internal/providers/workbuddy/thinking_toggle_test.go b/internal/providers/workbuddy/thinking_toggle_test.go index e9ca7f4a..619c7a66 100644 --- a/internal/providers/workbuddy/thinking_toggle_test.go +++ b/internal/providers/workbuddy/thinking_toggle_test.go @@ -41,6 +41,25 @@ func TestApplyChatReasoningDisabledThinking(t *testing.T) { } } +func TestApplyChatReasoningNoneNeverUpgrades(t *testing.T) { + // A model that declares "none" but cannot turn thinking off must degrade + // down to the cheapest thinking tier, never up to the model default. + caps := providers.ModelCapabilities{ + ReasoningOptions: []string{"none", "medium", "high"}, + ReasoningDefault: "high", + CanDisableThinking: false, + } + req := translate.ChatRequest{Model: "glm-5.3", ReasoningEffort: json.RawMessage(`"none"`)} + obj := map[string]any{"reasoning_effort": "stale"} + if got := applyChatReasoning(obj, req, "", caps); got != "medium" { + t.Fatalf("resolved level=%q want medium (cheapest thinking tier)", got) + } + reasoning, ok := obj["reasoning"].(map[string]any) + if !ok || reasoning["effort"] != "medium" { + t.Fatalf("reasoning=%v want effort medium", obj["reasoning"]) + } +} + func TestApplyChatReasoningDisabledDoesNotHijackLevels(t *testing.T) { caps := providers.ModelCapabilities{ ReasoningOptions: []string{"low", "high", "max"}, From 935e4fccb7fa893b835196e520d1eb201e4444e9 Mon Sep 17 00:00:00 2001 From: guyu233 Date: Wed, 25 Nov 2026 23:10:40 +0800 Subject: [PATCH 4/4] changelog: bullet reasoning effort degrade note --- changelog/unreleased/reasoning-effort-degrade.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/changelog/unreleased/reasoning-effort-degrade.md b/changelog/unreleased/reasoning-effort-degrade.md index 131d8b0f..159c6a22 100644 --- a/changelog/unreleased/reasoning-effort-degrade.md +++ b/changelog/unreleased/reasoning-effort-degrade.md @@ -1,7 +1,11 @@ ### English -Reasoning effort now degrades the CC Switch way: when a client asks for a level the model does not declare, the gateway sends the nearest lower level the model does advertise and never upgrades. A request below the model's floor (for example `reasoning_effort: none` against a model that only offers low/high/max) lands on the cheapest declared level instead of the model default, so "do not think hard" no longer becomes full-strength thinking. Levels the model declares are forwarded untouched, and `max` is never downgraded. +- Reasoning effort now degrades the CC Switch way: a level the model does not declare resolves to the nearest lower level it does declare, and never upgrades. +- A request below the model's floor (for example `reasoning_effort: none` against a model that only offers low/high/max) lands on the cheapest declared level instead of the model default, so "do not think hard" no longer becomes full-strength thinking. +- Declared levels are forwarded untouched, and `max` is never downgraded. ### 中文 -思考强度改为按 CC Switch 的方式降级:客户端要求模型没有声明的档位时,网关下发它总声明过的最近更低档,且永不升档。请求低于模型最低档时(例如对只提供 low/high/max 的模型传 `reasoning_effort: none`),落到最低可用档而不是模型默认档,因此「不要高强度思考」不会再变成满档思考。模型声明了的档位原样下发,`max` 永不被降级。 +- 思考强度改为按 CC Switch 的方式降级:模型没有声明的档位解析为它声明过的最近更低档,且永不升档。 +- 请求低于模型最低档时(例如对只提供 low/high/max 的模型传 `reasoning_effort: none`),落到最低可用档而不是模型默认档,因此「不要高强度思考」不会再变成满档思考。 +- 模型声明了的档位原样下发,`max` 永不被降级。