Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions changelog/unreleased/reasoning-ccswitch-passthrough.md
Original file line number Diff line number Diff line change
@@ -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」分支也一并修正。
11 changes: 11 additions & 0 deletions changelog/unreleased/reasoning-effort-degrade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### English

- 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` 永不被降级。
2 changes: 1 addition & 1 deletion internal/providers/kimi/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
94 changes: 75 additions & 19 deletions internal/providers/reasoning.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,70 @@ 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
}

// 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 {
Expand All @@ -148,16 +189,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
}
92 changes: 89 additions & 3 deletions internal/providers/reasoning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -55,3 +55,89 @@ 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)
}
}
}

// 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)
}
}
7 changes: 5 additions & 2 deletions internal/providers/workbuddy/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
34 changes: 27 additions & 7 deletions internal/providers/workbuddy/thinking_toggle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -41,13 +41,33 @@ 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"},
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
Expand All @@ -56,9 +76,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"},
Expand Down
Loading