| 1 | //! Reasoning preferences and effective tiers (#5316). |
| 2 | //! |
| 3 | //! Separated from TUI app state so engine, client routing, settings, and |
| 4 | //! tools can resolve and normalize reasoning effort without depending on |
| 5 | //! presentation types. |
| 6 | |
| 7 | use codewhale_config::ProviderKind; |
| 8 | use codewhale_models::reasoning_effort_tier::ReasoningEffortTier; |
| 9 | |
| 10 | /// Reasoning-effort tier, mirrored across DeepSeek and Codex effort pickers. |
| 11 | /// |
| 12 | /// The config file accepts every supported string value for forward-compat with |
| 13 | /// providers that expose the full spectrum; first-party DeepSeek routes send |
| 14 | /// `low`/`high`/`max`, so `Minimal` → `low`, `Medium`/`XHigh` → `high`, and |
| 15 | /// `Ultra` → `max`. OpenAI Codex normalizes inherited DeepSeek-only |
| 16 | /// `Off` to `Low` and keeps `XHigh`, `Max`, and `Ultra` distinct at the |
| 17 | /// provider boundary. The default keyboard cycler walks the three DeepSeek-distinct |
| 18 | /// tiers: `Off` → `High` → `Max` → `Off`; provider-aware callers should use |
| 19 | /// [`ReasoningEffort::cycle_next_in`] with the route's picker ladder |
| 20 | /// (`picker_efforts_for_route`), which also covers Auto routing. |
| 21 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 22 | pub enum ReasoningEffort { |
| 23 | Off, |
| 24 | Minimal, |
| 25 | Low, |
| 26 | Medium, |
| 27 | High, |
| 28 | XHigh, |
| 29 | Ultra, |
| 30 | #[default] |
| 31 | Auto, |
| 32 | Max, |
| 33 | } |
| 34 | |
| 35 | /// Provider-effective reasoning state used by durable receipts and visible |
| 36 | /// requested-to-effective labels. |
| 37 | /// |
| 38 | /// Some routes, notably first-party GLM-5-Turbo, support a thinking toggle but |
| 39 | /// publish no effort tiers. Keeping that state distinct prevents a requested |
| 40 | /// `max` from being displayed or persisted as an effective `max` claim. |
| 41 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 42 | pub enum EffectiveReasoningEffort { |
| 43 | Tier(ReasoningEffort), |
| 44 | ThinkingEnabledGranularityUnavailable, |
| 45 | Unavailable, |
| 46 | } |
| 47 | |
| 48 | impl EffectiveReasoningEffort { |
| 49 | /// Reconstruct a safe request tier for cache replay and inspection. |
| 50 | /// |
| 51 | /// Routes with an enabled-but-untiered receipt collapse every non-Off |
| 52 | /// request to the same wire toggle, so High is the canonical value that |
| 53 | /// keeps reasoning enabled without claiming a granular effective tier. |
| 54 | #[must_use] |
| 55 | pub const fn request_tier_for_replay(self) -> Option<ReasoningEffort> { |
| 56 | match self { |
| 57 | Self::Tier(tier) => Some(tier), |
| 58 | Self::ThinkingEnabledGranularityUnavailable => Some(ReasoningEffort::High), |
| 59 | Self::Unavailable => None, |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl From<EffectiveReasoningEffort> for ReasoningEffortTier { |
| 65 | fn from(value: EffectiveReasoningEffort) -> Self { |
| 66 | match value { |
| 67 | EffectiveReasoningEffort::Tier(tier) => tier.into(), |
| 68 | EffectiveReasoningEffort::ThinkingEnabledGranularityUnavailable => { |
| 69 | Self::ThinkingEnabledGranularityUnavailable |
| 70 | } |
| 71 | EffectiveReasoningEffort::Unavailable => Self::Unavailable, |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl From<ReasoningEffortTier> for EffectiveReasoningEffort { |
| 77 | fn from(value: ReasoningEffortTier) -> Self { |
| 78 | match value { |
| 79 | ReasoningEffortTier::Off => Self::Tier(ReasoningEffort::Off), |
| 80 | ReasoningEffortTier::Minimal => Self::Tier(ReasoningEffort::Minimal), |
| 81 | ReasoningEffortTier::Low => Self::Tier(ReasoningEffort::Low), |
| 82 | ReasoningEffortTier::Medium => Self::Tier(ReasoningEffort::Medium), |
| 83 | ReasoningEffortTier::High => Self::Tier(ReasoningEffort::High), |
| 84 | ReasoningEffortTier::XHigh => Self::Tier(ReasoningEffort::XHigh), |
| 85 | ReasoningEffortTier::Ultra => Self::Tier(ReasoningEffort::Ultra), |
| 86 | ReasoningEffortTier::Auto => Self::Tier(ReasoningEffort::Auto), |
| 87 | ReasoningEffortTier::Max => Self::Tier(ReasoningEffort::Max), |
| 88 | ReasoningEffortTier::ThinkingEnabledGranularityUnavailable => { |
| 89 | Self::ThinkingEnabledGranularityUnavailable |
| 90 | } |
| 91 | ReasoningEffortTier::Unavailable => Self::Unavailable, |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl From<ReasoningEffort> for ReasoningEffortTier { |
| 97 | fn from(value: ReasoningEffort) -> Self { |
| 98 | match value { |
| 99 | ReasoningEffort::Off => Self::Off, |
| 100 | ReasoningEffort::Minimal => Self::Minimal, |
| 101 | ReasoningEffort::Low => Self::Low, |
| 102 | ReasoningEffort::Medium => Self::Medium, |
| 103 | ReasoningEffort::High => Self::High, |
| 104 | ReasoningEffort::XHigh => Self::XHigh, |
| 105 | ReasoningEffort::Ultra => Self::Ultra, |
| 106 | ReasoningEffort::Auto => Self::Auto, |
| 107 | ReasoningEffort::Max => Self::Max, |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl ReasoningEffort { |
| 113 | /// Parse an operator-supplied effort value. |
| 114 | /// |
| 115 | /// This is deliberately the one canonical spelling table for every |
| 116 | /// human-facing route. Every canonical setting spelling round-trips: |
| 117 | /// `parse_strict(as_setting(effort)) == effort`, including `minimal`. |
| 118 | /// Callers that read an old persisted config may use [`Self::from_setting`] |
| 119 | /// for its compatibility fallback, but a new CLI, settings, or tool input |
| 120 | /// must reject an unknown value instead of quietly turning it into `max`. |
| 121 | pub fn parse_strict(value: &str) -> Result<Self, String> { |
| 122 | let canonical = codewhale_config::settings_schema::canonical_reasoning_effort(value)?; |
| 123 | Ok([ |
| 124 | Self::Off, |
| 125 | Self::Minimal, |
| 126 | Self::Low, |
| 127 | Self::Medium, |
| 128 | Self::High, |
| 129 | Self::XHigh, |
| 130 | Self::Ultra, |
| 131 | Self::Auto, |
| 132 | Self::Max, |
| 133 | ] |
| 134 | .into_iter() |
| 135 | .find(|effort| effort.as_setting() == canonical) |
| 136 | .unwrap_or_default()) |
| 137 | } |
| 138 | |
| 139 | /// Parse a persisted config-file string into an effort tier. Unknown |
| 140 | /// legacy values fall back to the default (`Auto`) so an old malformed |
| 141 | /// settings file never prevents startup. New user input should use |
| 142 | /// [`Self::parse_strict`] instead. |
| 143 | #[must_use] |
| 144 | pub fn from_setting(value: &str) -> Self { |
| 145 | Self::parse_strict(value).unwrap_or_default() |
| 146 | } |
| 147 | |
| 148 | #[must_use] |
| 149 | pub fn from_setting_for_provider(value: &str, provider: ProviderKind) -> Self { |
| 150 | Self::from_setting(value).normalize_for_provider(provider) |
| 151 | } |
| 152 | |
| 153 | /// Canonical lowercase label used for config storage and UI hints. |
| 154 | #[must_use] |
| 155 | pub fn as_setting(self) -> &'static str { |
| 156 | match self { |
| 157 | Self::Off => "off", |
| 158 | Self::Minimal => "minimal", |
| 159 | Self::Low => "low", |
| 160 | Self::Medium => "medium", |
| 161 | Self::High => "high", |
| 162 | Self::XHigh => "xhigh", |
| 163 | Self::Ultra => "ultra", |
| 164 | Self::Auto => "auto", |
| 165 | Self::Max => "max", |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// Short label for the header chip. |
| 170 | #[must_use] |
| 171 | pub fn short_label(self) -> &'static str { |
| 172 | match self { |
| 173 | Self::Off => "off", |
| 174 | Self::Minimal => "minimal", |
| 175 | Self::Low => "low", |
| 176 | Self::Medium => "med", |
| 177 | Self::High => "high", |
| 178 | Self::XHigh => "xhigh", |
| 179 | Self::Ultra => "ultra", |
| 180 | Self::Auto => "auto", |
| 181 | Self::Max => "max", |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// Provider-facing label for user-visible surfaces. |
| 186 | #[must_use] |
| 187 | pub fn display_label_for_provider(self, provider: ProviderKind) -> &'static str { |
| 188 | match (provider, self.normalize_for_provider(provider)) { |
| 189 | (ProviderKind::OpenaiCodex, Self::Minimal) => "low", |
| 190 | (ProviderKind::OpenaiCodex, Self::Low) => "low", |
| 191 | (ProviderKind::OpenaiCodex, Self::Medium) => "medium", |
| 192 | (ProviderKind::OpenaiCodex, Self::High) => "high", |
| 193 | // `xhigh`, `max` and `ultra` are three distinct rungs the Codex |
| 194 | // roster publishes per model; collapsing them onto "xhigh" dates |
| 195 | // from when xhigh was the ceiling and made the top tiers |
| 196 | // unreachable and indistinguishable. |
| 197 | (ProviderKind::OpenaiCodex, Self::XHigh) => "xhigh", |
| 198 | (ProviderKind::OpenaiCodex, Self::Ultra) => "ultra", |
| 199 | (ProviderKind::OpenaiCodex, Self::Max) => "max", |
| 200 | (ProviderKind::Xai, Self::XHigh) => "xhigh", |
| 201 | (_, effort) => effort.short_label(), |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// Value forwarded to the engine/client. `None` means "provider default" |
| 206 | /// (for `Off` we still emit `"off"` so the client can inject |
| 207 | /// `thinking = {"type": "disabled"}`). |
| 208 | #[must_use] |
| 209 | pub fn api_value(self) -> Option<&'static str> { |
| 210 | Some(self.as_setting()) |
| 211 | } |
| 212 | |
| 213 | #[must_use] |
| 214 | pub fn normalize_for_provider(self, provider: ProviderKind) -> Self { |
| 215 | if provider != ProviderKind::OpenaiCodex { |
| 216 | return self; |
| 217 | } |
| 218 | match self { |
| 219 | Self::Off => Self::Low, |
| 220 | Self::Auto => Self::Medium, |
| 221 | other => other, |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /// Resolve an effort against the exact provider route that will receive |
| 226 | /// the request. Both K3 routes are always-thinking, so `off` becomes the |
| 227 | /// lowest supported tier. The Kimi Code membership route otherwise keeps |
| 228 | /// its low/high/max mapping; direct Moonshot K3 additionally maps `medium` |
| 229 | /// to `high`. First-party DeepSeek routes keep `low` (the wire documents |
| 230 | /// low/high/max) while rounding `medium` up to `high`. Models that publish |
| 231 | /// a Models.dev `reasoning_options` effort list keep that vocabulary |
| 232 | /// instead of the historic Low/Medium collapse. Generic Moonshot and |
| 233 | /// every other non-Codex route retain the historic high coercion. |
| 234 | /// This intentionally does not change [`Self::normalize_for_provider`], |
| 235 | /// whose generic wire semantics are used by older callers that do not yet |
| 236 | /// have a route receipt. |
| 237 | #[must_use] |
| 238 | pub fn normalize_for_route( |
| 239 | self, |
| 240 | provider: ProviderKind, |
| 241 | base_url: &str, |
| 242 | wire_model: &str, |
| 243 | ) -> Self { |
| 244 | self.normalize_for_route_identity(provider, provider.as_str(), base_url, wire_model) |
| 245 | } |
| 246 | |
| 247 | #[must_use] |
| 248 | pub fn normalize_for_route_identity( |
| 249 | self, |
| 250 | provider: ProviderKind, |
| 251 | identity: &str, |
| 252 | base_url: &str, |
| 253 | wire_model: &str, |
| 254 | ) -> Self { |
| 255 | let normalized = self.normalize_for_provider(provider); |
| 256 | if codewhale_runtime::config::is_exact_kimi_code_k3_route(provider, base_url, wire_model) { |
| 257 | return match normalized { |
| 258 | Self::Off => Self::Low, |
| 259 | other => other, |
| 260 | }; |
| 261 | } |
| 262 | if codewhale_runtime::config::is_exact_direct_moonshot_k3_route( |
| 263 | provider, base_url, wire_model, |
| 264 | ) { |
| 265 | return match normalized { |
| 266 | Self::Off => Self::Low, |
| 267 | Self::Medium => Self::High, |
| 268 | other => other, |
| 269 | }; |
| 270 | } |
| 271 | if provider == ProviderKind::OpenaiCodex { |
| 272 | return normalized; |
| 273 | } |
| 274 | // First-party DeepSeek routes document `reasoning_effort` low/high/max |
| 275 | // on the wire (no medium), so `low` is a real, cheaper tier there and |
| 276 | // must reach the wire as low; `medium` rounds up to high because the |
| 277 | // dialect has no such value (#52). `minimal`, `xhigh`, and `ultra` |
| 278 | // collapse exactly as `client::deepseek_effort` sends them, so the |
| 279 | // effective tier names what the wire receives (#6650). |
| 280 | if matches!(provider, ProviderKind::Deepseek) { |
| 281 | let effective = match normalized { |
| 282 | Self::Minimal => Self::Low, |
| 283 | Self::Medium | Self::XHigh => Self::High, |
| 284 | Self::Ultra => Self::Max, |
| 285 | other => other, |
| 286 | }; |
| 287 | if codewhale_config::provider_base_url_is_official(provider, base_url) |
| 288 | && let Some(offering) = crate::provider_lake::catalog_offering_for_route( |
| 289 | provider, identity, base_url, wire_model, |
| 290 | ) |
| 291 | { |
| 292 | if let Some(values) = Self::catalog_effort_values_for_offering(&offering) { |
| 293 | return Self::clamp_to_catalog_efforts( |
| 294 | effective, |
| 295 | Self::catalog_default_for_offering(&offering), |
| 296 | &values, |
| 297 | ); |
| 298 | } |
| 299 | if offering.reasoning_options.iter().any(|option| { |
| 300 | option.get("type").and_then(|value| value.as_str()) == Some("effort") |
| 301 | }) { |
| 302 | return Self::Auto; |
| 303 | } |
| 304 | } |
| 305 | return effective; |
| 306 | } |
| 307 | // Ollama's current OpenAI-compatible Chat Completions contract |
| 308 | // documents the complete none/low/medium/high/max ladder. Keep every |
| 309 | // real tier distinct for normal turns; only Codewhale-only synonyms |
| 310 | // are folded onto the nearest documented spelling. |
| 311 | if provider == ProviderKind::OllamaCloud { |
| 312 | return match normalized { |
| 313 | Self::Minimal => Self::Low, |
| 314 | Self::XHigh | Self::Ultra => Self::Max, |
| 315 | other => other, |
| 316 | }; |
| 317 | } |
| 318 | if provider == ProviderKind::Vllm |
| 319 | && let Some(tier) = codewhale_config::local_reasoning::local_reasoning_spec(wire_model) |
| 320 | .and_then(|spec| spec.normalize(normalized.as_setting())) |
| 321 | { |
| 322 | return Self::from_setting(tier); |
| 323 | } |
| 324 | let catalog_identity = if provider == ProviderKind::Custom { |
| 325 | identity |
| 326 | } else { |
| 327 | provider.as_str() |
| 328 | }; |
| 329 | if let Some(offering) = crate::provider_lake::catalog_offering_for_route( |
| 330 | provider, |
| 331 | catalog_identity, |
| 332 | base_url, |
| 333 | wire_model, |
| 334 | ) && let Some(values) = Self::catalog_effort_values_for_offering(&offering) |
| 335 | { |
| 336 | if normalized == Self::Off |
| 337 | && provider != ProviderKind::Custom |
| 338 | && !codewhale_config::provider_base_url_is_official(provider, base_url) |
| 339 | { |
| 340 | return Self::Off; |
| 341 | } |
| 342 | return Self::clamp_to_catalog_efforts( |
| 343 | normalized, |
| 344 | Self::catalog_default_for_offering(&offering), |
| 345 | &values, |
| 346 | ); |
| 347 | } |
| 348 | match normalized { |
| 349 | Self::Low | Self::Medium => Self::High, |
| 350 | other => other, |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | pub fn offering_for_reasoning( |
| 355 | provider: ProviderKind, |
| 356 | base_url: &str, |
| 357 | wire_model: &str, |
| 358 | ) -> Option<codewhale_config::catalog::CatalogOffering> { |
| 359 | crate::provider_lake::catalog_offering_for_route( |
| 360 | provider, |
| 361 | provider.as_str(), |
| 362 | base_url, |
| 363 | wire_model, |
| 364 | ) |
| 365 | } |
| 366 | |
| 367 | pub fn catalog_default( |
| 368 | provider: ProviderKind, |
| 369 | base_url: &str, |
| 370 | wire_model: &str, |
| 371 | ) -> Option<Self> { |
| 372 | let offering = Self::offering_for_reasoning(provider, base_url, wire_model)?; |
| 373 | Self::catalog_default_for_offering(&offering) |
| 374 | } |
| 375 | |
| 376 | fn catalog_default_for_offering( |
| 377 | offering: &codewhale_config::catalog::CatalogOffering, |
| 378 | ) -> Option<Self> { |
| 379 | offering.reasoning_options.iter().find_map(|option| { |
| 380 | option |
| 381 | .get("type") |
| 382 | .and_then(|value| value.as_str()) |
| 383 | .filter(|kind| kind.eq_ignore_ascii_case("effort"))?; |
| 384 | option |
| 385 | .get("default") |
| 386 | .and_then(|value| value.as_str()) |
| 387 | .and_then(Self::from_catalog_token) |
| 388 | }) |
| 389 | } |
| 390 | |
| 391 | pub fn catalog_effort_values( |
| 392 | provider: ProviderKind, |
| 393 | base_url: &str, |
| 394 | wire_model: &str, |
| 395 | ) -> Option<Vec<Self>> { |
| 396 | let offering = Self::offering_for_reasoning(provider, base_url, wire_model)?; |
| 397 | Self::catalog_effort_values_for_offering(&offering) |
| 398 | } |
| 399 | |
| 400 | pub fn catalog_effort_values_for_offering( |
| 401 | offering: &codewhale_config::catalog::CatalogOffering, |
| 402 | ) -> Option<Vec<Self>> { |
| 403 | let mut efforts = Vec::new(); |
| 404 | let mut has_toggle = false; |
| 405 | for option in &offering.reasoning_options { |
| 406 | let kind = option.get("type").and_then(|value| value.as_str()); |
| 407 | if kind.is_some_and(|kind| kind.eq_ignore_ascii_case("toggle")) { |
| 408 | has_toggle = true; |
| 409 | continue; |
| 410 | } |
| 411 | if !kind.is_some_and(|kind| kind.eq_ignore_ascii_case("effort")) { |
| 412 | continue; |
| 413 | } |
| 414 | let Some(values) = option.get("values").and_then(|value| value.as_array()) else { |
| 415 | continue; |
| 416 | }; |
| 417 | for value in values { |
| 418 | if let Some(effort) = value.as_str().and_then(Self::from_catalog_token) |
| 419 | && !efforts.contains(&effort) |
| 420 | { |
| 421 | efforts.push(effort); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | // A Models.dev `toggle` beside an effort ladder means reasoning can also |
| 426 | // be switched off (#6396); an explicit off must not round up. |
| 427 | if has_toggle && !efforts.is_empty() && !efforts.contains(&Self::Off) { |
| 428 | efforts.insert(0, Self::Off); |
| 429 | } |
| 430 | (!efforts.is_empty()).then_some(efforts) |
| 431 | } |
| 432 | |
| 433 | pub fn from_catalog_token(raw: &str) -> Option<Self> { |
| 434 | match raw.trim().to_ascii_lowercase().as_str() { |
| 435 | "off" | "disabled" | "none" | "false" => Some(Self::Off), |
| 436 | "minimal" | "minimum" => Some(Self::Minimal), |
| 437 | "low" | "light" => Some(Self::Low), |
| 438 | "medium" | "mid" => Some(Self::Medium), |
| 439 | "high" => Some(Self::High), |
| 440 | "xhigh" => Some(Self::XHigh), |
| 441 | "ultra" | "ultracode" => Some(Self::Ultra), |
| 442 | "max" | "maximum" => Some(Self::Max), |
| 443 | "auto" | "automatic" | "adaptive" | "default" => Some(Self::Auto), |
| 444 | _ => None, |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fn clamp_to_catalog_efforts(normalized: Self, default: Option<Self>, values: &[Self]) -> Self { |
| 449 | if matches!(normalized, Self::Auto) || values.contains(&normalized) { |
| 450 | return normalized; |
| 451 | } |
| 452 | let aliased = match normalized { |
| 453 | Self::Minimal if values.contains(&Self::Low) => Self::Low, |
| 454 | Self::Max | Self::Ultra if values.contains(&Self::XHigh) => Self::XHigh, |
| 455 | Self::Off => default.unwrap_or(Self::High), |
| 456 | other => other, |
| 457 | }; |
| 458 | if values.contains(&aliased) { |
| 459 | return aliased; |
| 460 | } |
| 461 | if let Some(default) = default { |
| 462 | return default; |
| 463 | } |
| 464 | if values.contains(&Self::High) { |
| 465 | return Self::High; |
| 466 | } |
| 467 | normalized.nearest_in(values).unwrap_or(Self::High) |
| 468 | } |
| 469 | |
| 470 | #[must_use] |
| 471 | pub fn api_value_for_provider(self, provider: ProviderKind) -> Option<&'static str> { |
| 472 | if provider != ProviderKind::OpenaiCodex { |
| 473 | return self.api_value(); |
| 474 | } |
| 475 | Some(match self.normalize_for_provider(provider) { |
| 476 | Self::Minimal => "low", |
| 477 | Self::Low => "low", |
| 478 | Self::Medium => "medium", |
| 479 | Self::High => "high", |
| 480 | Self::XHigh => "xhigh", |
| 481 | Self::Ultra => "ultra", |
| 482 | Self::Max => "max", |
| 483 | Self::Off => "low", |
| 484 | Self::Auto => "medium", |
| 485 | }) |
| 486 | } |
| 487 | |
| 488 | /// Provider-facing value after exact-route normalization. |
| 489 | #[must_use] |
| 490 | pub fn api_value_for_route( |
| 491 | self, |
| 492 | provider: ProviderKind, |
| 493 | base_url: &str, |
| 494 | wire_model: &str, |
| 495 | ) -> Option<&'static str> { |
| 496 | self.normalize_for_route(provider, base_url, wire_model) |
| 497 | .api_value_for_provider(provider) |
| 498 | } |
| 499 | |
| 500 | #[must_use] |
| 501 | pub fn as_setting_for_provider(self, provider: ProviderKind) -> &'static str { |
| 502 | self.api_value_for_provider(provider) |
| 503 | .unwrap_or_else(|| self.as_setting()) |
| 504 | } |
| 505 | |
| 506 | /// Persist the canonical setting after exact-route normalization. |
| 507 | #[must_use] |
| 508 | pub fn as_setting_for_route( |
| 509 | self, |
| 510 | provider: ProviderKind, |
| 511 | base_url: &str, |
| 512 | wire_model: &str, |
| 513 | ) -> &'static str { |
| 514 | self.normalize_for_route(provider, base_url, wire_model) |
| 515 | .as_setting_for_provider(provider) |
| 516 | } |
| 517 | |
| 518 | /// Cycle through the three behaviorally distinct tiers. |
| 519 | #[must_use] |
| 520 | pub fn cycle_next(self) -> Self { |
| 521 | match self { |
| 522 | Self::Off => Self::High, |
| 523 | Self::Auto => Self::Off, |
| 524 | Self::Minimal | Self::Low | Self::Medium | Self::High | Self::XHigh | Self::Ultra => { |
| 525 | Self::Max |
| 526 | } |
| 527 | Self::Max => Self::Off, |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /// Advance through an exact-route effort list. |
| 532 | /// |
| 533 | /// A value that is literally on the ladder advances one rung and wraps. |
| 534 | /// An off-ladder value enters at [`Self::nearest_in`]'s rung and advances |
| 535 | /// from there, so the cycler only ever moves forward: a persisted `max` on |
| 536 | /// an `xhigh` ladder, `ultra` on a `max` ladder, or `minimal` on a ladder |
| 537 | /// starting at `off` still walks upward instead of reversing. |
| 538 | #[must_use] |
| 539 | pub fn cycle_next_in(self, efforts: &[Self]) -> Self { |
| 540 | if efforts.is_empty() { |
| 541 | return self.cycle_next(); |
| 542 | } |
| 543 | if let Some(index) = self.index_in(efforts) { |
| 544 | return efforts[(index + 1) % efforts.len()]; |
| 545 | } |
| 546 | let Some(entry) = self.nearest_in(efforts) else { |
| 547 | return self.cycle_next(); |
| 548 | }; |
| 549 | let index = entry.index_in(efforts).unwrap_or(0); |
| 550 | efforts[(index + 1) % efforts.len()] |
| 551 | } |
| 552 | |
| 553 | /// Ladder rank of a concrete tier. |
| 554 | /// |
| 555 | /// `Auto` is the unresolved sentinel: it names no rung, so it has no rank |
| 556 | /// and is never a projection target. |
| 557 | const fn rung_rank(self) -> Option<u8> { |
| 558 | Some(match self { |
| 559 | Self::Off => 0, |
| 560 | Self::Minimal => 1, |
| 561 | Self::Low => 2, |
| 562 | Self::Medium => 3, |
| 563 | Self::High => 4, |
| 564 | Self::XHigh => 5, |
| 565 | Self::Ultra => 6, |
| 566 | Self::Max => 7, |
| 567 | Self::Auto => return None, |
| 568 | }) |
| 569 | } |
| 570 | |
| 571 | /// Project an effort onto the rungs a route actually offers. |
| 572 | /// |
| 573 | /// One deterministic rule replaces the hand-written alias buckets: the |
| 574 | /// highest allowed rung at or below the current one, or the lowest allowed |
| 575 | /// rung when the current value sits below every rung or names no rung at |
| 576 | /// all (`Auto`). `None` only when the ladder publishes no rung. |
| 577 | #[must_use] |
| 578 | pub fn nearest_in(self, efforts: &[Self]) -> Option<Self> { |
| 579 | let own_rank = self.rung_rank(); |
| 580 | let mut nearest_below: Option<Self> = None; |
| 581 | let mut lowest: Option<Self> = None; |
| 582 | for effort in efforts.iter().copied() { |
| 583 | let Some(rank) = effort.rung_rank() else { |
| 584 | continue; |
| 585 | }; |
| 586 | if lowest.is_none_or(|current: Self| rank < current.rung_rank().unwrap_or(u8::MAX)) { |
| 587 | lowest = Some(effort); |
| 588 | } |
| 589 | if own_rank.is_some_and(|own| rank <= own) |
| 590 | && nearest_below.is_none_or(|current: Self| rank > current.rung_rank().unwrap_or(0)) |
| 591 | { |
| 592 | nearest_below = Some(effort); |
| 593 | } |
| 594 | } |
| 595 | nearest_below.or(lowest) |
| 596 | } |
| 597 | |
| 598 | /// Position of a literally listed value on the ladder. Values that are not |
| 599 | /// listed are handled by [`Self::nearest_in`], never by an alias table. |
| 600 | fn index_in(self, efforts: &[Self]) -> Option<usize> { |
| 601 | efforts.iter().position(|&effort| effort == self) |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | #[cfg(test)] |
| 606 | mod tests { |
| 607 | use super::ReasoningEffort; |
| 608 | |
| 609 | const CANONICAL: [ReasoningEffort; 9] = [ |
| 610 | ReasoningEffort::Auto, |
| 611 | ReasoningEffort::Off, |
| 612 | ReasoningEffort::Minimal, |
| 613 | ReasoningEffort::Low, |
| 614 | ReasoningEffort::Medium, |
| 615 | ReasoningEffort::High, |
| 616 | ReasoningEffort::XHigh, |
| 617 | ReasoningEffort::Ultra, |
| 618 | ReasoningEffort::Max, |
| 619 | ]; |
| 620 | |
| 621 | #[test] |
| 622 | fn reasoning_effort_parse_strict_round_trips_every_canonical_spelling() { |
| 623 | for effort in CANONICAL { |
| 624 | assert_eq!( |
| 625 | ReasoningEffort::parse_strict(effort.as_setting()), |
| 626 | Ok(effort), |
| 627 | "{} must parse back to itself", |
| 628 | effort.as_setting() |
| 629 | ); |
| 630 | assert_eq!( |
| 631 | ReasoningEffort::from_setting(effort.as_setting()), |
| 632 | effort, |
| 633 | "a persisted {} must load as itself", |
| 634 | effort.as_setting() |
| 635 | ); |
| 636 | } |
| 637 | assert_eq!( |
| 638 | ReasoningEffort::parse_strict("minimal"), |
| 639 | Ok(ReasoningEffort::Minimal) |
| 640 | ); |
| 641 | assert_eq!( |
| 642 | ReasoningEffort::parse_strict("minimum"), |
| 643 | Ok(ReasoningEffort::Low) |
| 644 | ); |
| 645 | assert_eq!( |
| 646 | ReasoningEffort::parse_strict("light"), |
| 647 | Ok(ReasoningEffort::Low) |
| 648 | ); |
| 649 | } |
| 650 | |
| 651 | #[test] |
| 652 | fn reasoning_effort_parse_strict_error_lists_the_complete_vocabulary() { |
| 653 | let error = ReasoningEffort::parse_strict("xhihg").expect_err("unknown value must fail"); |
| 654 | for spelling in [ |
| 655 | "auto", "off", "minimal", "low", "medium", "high", "xhigh", "ultra", "max", |
| 656 | ] { |
| 657 | assert!( |
| 658 | error.contains(spelling), |
| 659 | "the error must name {spelling}, got {error:?}" |
| 660 | ); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | #[test] |
| 665 | fn reasoning_effort_nearest_in_projects_one_value_per_direction() { |
| 666 | use ReasoningEffort::*; |
| 667 | let full = [Off, Minimal, Low, Medium, High, XHigh, Ultra, Max]; |
| 668 | // Literal members project onto themselves. |
| 669 | for effort in full { |
| 670 | assert_eq!(effort.nearest_in(&full), Some(effort)); |
| 671 | } |
| 672 | // A capped ladder: the highest rung at or below wins, never a rung above. |
| 673 | let capped = [Off, Low, Medium, High, XHigh]; |
| 674 | assert_eq!(Max.nearest_in(&capped), Some(XHigh)); |
| 675 | assert_eq!(Ultra.nearest_in(&capped), Some(XHigh)); |
| 676 | assert_eq!(Minimal.nearest_in(&capped), Some(Off)); |
| 677 | // Below every rung: the lowest allowed rung is the entry point. |
| 678 | assert_eq!(Off.nearest_in(&[Low, Medium, High, Max]), Some(Low)); |
| 679 | // `Auto` names no rung, so it enters at the lowest allowed rung. |
| 680 | assert_eq!(Auto.nearest_in(&[Low, Medium, High, Max]), Some(Low)); |
| 681 | assert_eq!(Auto.nearest_in(&[Off, Low, High, Max]), Some(Off)); |
| 682 | // A ladder that publishes no rung has nothing to project onto. |
| 683 | assert_eq!(Medium.nearest_in(&[Auto]), None); |
| 684 | assert_eq!(Medium.nearest_in(&[]), None); |
| 685 | } |
| 686 | |
| 687 | #[test] |
| 688 | fn reasoning_effort_cycle_next_in_only_moves_forward_on_any_ladder() { |
| 689 | use ReasoningEffort::*; |
| 690 | // Off-ladder values enter at their nearest rung and advance one rung. |
| 691 | let capped = [Off, Low, Medium, High, XHigh]; |
| 692 | assert_eq!( |
| 693 | Max.cycle_next_in(&capped), |
| 694 | Off, |
| 695 | "xhigh-capped ladder wraps forward" |
| 696 | ); |
| 697 | assert_eq!(Ultra.cycle_next_in(&capped), Off); |
| 698 | assert_eq!(Minimal.cycle_next_in(&capped), Low); |
| 699 | // A value below every rung enters at the lowest rung. |
| 700 | assert_eq!(Off.cycle_next_in(&[Low, Medium, High, Max]), Medium); |
| 701 | // `Auto` enters at the ladder's lowest rung. |
| 702 | assert_eq!(Auto.cycle_next_in(&[Off, Low, High, Max]), Low); |
| 703 | // Literal members advance and wrap without an alias table. |
| 704 | assert_eq!(High.cycle_next_in(&capped), XHigh); |
| 705 | assert_eq!(XHigh.cycle_next_in(&capped), Off); |
| 706 | // An empty ladder falls back to the provider-neutral cycle. |
| 707 | assert_eq!(Off.cycle_next_in(&[]), Off.cycle_next()); |
| 708 | } |
| 709 | } |
| 710 |