1use serde::{Deserialize, Serialize};
4
5pub mod auto_route_evidence;
6pub mod hand_pricing;
7pub mod image_window;
8pub mod reasoning_effort_tier;
9pub mod schema_canonicalize;
10pub mod schema_sanitize;
11pub mod tool_projection;
12pub mod turn_classify;
13
14pub const LEGACY_DEEPSEEK_CONTEXT_WINDOW_TOKENS: u32 = 128_000;
17pub use codewhale_config::catalog::reviewed::constants::DEEPSEEK_V4_CONTEXT_WINDOW_TOKENS;
18pub const KIMI_CODE_K3_CONTEXT_WINDOW_TOKENS: u32 = 262_144;
26pub use codewhale_config::catalog::reviewed::constants::KIMI_K3_CONTEXT_WINDOW_TOKENS;
31pub use codewhale_config::catalog::reviewed::constants::KIMI_K3_DEFAULT_MAX_COMPLETION_TOKENS;
36pub const DIRECT_KIMI_K3_MAX_OUTPUT_TOKENS: u32 = 1_048_576;
40pub const DEFAULT_COMPACTION_TOKEN_THRESHOLD: usize = 102_400;
48pub fn canonical_official_deepseek_model_id(model: &str) -> Option<&'static str> {
49 codewhale_config::catalog::reviewed::official_deepseek_model_id(model)
50}
51
52#[cfg(any(test, feature = "test-support"))]
53const COMPACTION_THRESHOLD_PERCENT: u32 = 80;
54
55#[allow(unused_imports)]
64pub use codewhale_core::request::{
65 CacheControl, ContentBlock, ConversationTitleConfig, INTERRUPTED_ASSISTANT_CONTEXT_PREFIX,
66 INTERRUPTED_ASSISTANT_ROLE, ImageUrlContent, Message, MessageRequest, OpaqueReasoningState,
67 SafetyCheckConfig, SystemBlock, SystemPrompt, Tool, ToolCallKey, ToolCaller,
68};
69#[allow(unused_imports)]
70pub use codewhale_core::role::Role;
71
72#[derive(Debug, Serialize, Deserialize, Clone)]
74pub struct ContainerInfo {
75 pub id: String,
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub expires_at: Option<String>,
78}
79
80#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
82pub struct ServerToolUsage {
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub code_execution_requests: Option<u32>,
85 #[serde(skip_serializing_if = "Option::is_none")]
86 pub tool_search_requests: Option<u32>,
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub web_search_requests: Option<u32>,
89 #[serde(default, skip_serializing_if = "Option::is_none")]
90 pub web_fetch_requests: Option<u32>,
91 #[serde(default, skip_serializing_if = "Option::is_none")]
92 pub tool_search: Option<u32>,
93 #[serde(default, skip_serializing_if = "Option::is_none")]
94 pub tool_fetch: Option<u32>,
95 #[serde(default, skip_serializing_if = "Option::is_none")]
96 pub tool_image_search: Option<u32>,
97}
98
99#[derive(Debug, Serialize, Deserialize, Clone)]
101pub struct MessageResponse {
102 pub id: String,
103 pub r#type: String,
104 pub role: String,
105 pub content: Vec<ContentBlock>,
106 pub model: String,
107 pub stop_reason: Option<String>,
108 pub stop_sequence: Option<String>,
109 #[serde(skip_serializing_if = "Option::is_none")]
110 pub container: Option<ContainerInfo>,
111 pub usage: Usage,
112 #[serde(default, skip_serializing_if = "Option::is_none")]
113 pub title: Option<String>,
114 #[serde(default, skip_serializing_if = "Option::is_none")]
115 pub stop_details: Option<serde_json::Value>,
116 #[serde(default, skip_serializing_if = "Option::is_none")]
117 pub refusal: Option<String>,
118}
119
120#[must_use]
123pub fn is_output_limit_stop_reason(reason: Option<&str>) -> bool {
124 reason.is_some_and(|reason| {
125 let reason = reason
126 .trim()
127 .strip_prefix("incomplete:")
128 .unwrap_or_else(|| reason.trim());
129 matches!(
130 reason.to_ascii_lowercase().as_str(),
131 "length"
132 | "max_tokens"
133 | "max_output_tokens"
134 | "model_length"
135 | "output_limit"
136 | "max_completion_tokens"
137 )
138 })
139}
140
141pub const OUTPUT_CEILING_STOP_REASON: &str = "length";
145
146#[must_use]
159pub fn stop_contradicts_output_ceiling(
160 reason: Option<&str>,
161 reported_output_tokens: Option<u32>,
162 requested_max_tokens: u32,
163) -> bool {
164 requested_max_tokens > 0
165 && reported_output_tokens == Some(requested_max_tokens)
166 && reason.is_some_and(|reason| reason.trim().eq_ignore_ascii_case("stop"))
167}
168
169#[must_use]
170pub fn is_refusal_stop_reason(reason: Option<&str>) -> bool {
171 reason.is_some_and(|reason| {
172 matches!(
173 reason.trim().to_ascii_lowercase().as_str(),
174 "refusal" | "content_filter" | "incomplete:content_filter"
175 )
176 })
177}
178
179#[must_use]
183pub fn is_incomplete_stop_reason(reason: Option<&str>) -> bool {
184 is_output_limit_stop_reason(reason)
185 || is_refusal_stop_reason(reason)
186 || reason.is_some_and(|reason| {
187 let reason = reason.trim().to_ascii_lowercase();
188 reason.starts_with("incomplete:")
189 || matches!(
190 reason.as_str(),
191 "content_filter"
192 | "refusal"
193 | "model_context_window_exceeded"
194 | "insufficient_system_resource"
195 | "aborted"
196 )
197 })
198}
199
200#[must_use]
201pub fn stop_reason_detail(reason: Option<&str>) -> &str {
202 reason
203 .map(str::trim)
204 .and_then(|reason| reason.strip_prefix("incomplete:").or(Some(reason)))
205 .filter(|reason| !reason.is_empty())
206 .unwrap_or("unknown")
207}
208
209#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
211pub struct Usage {
212 pub input_tokens: u32,
213 pub output_tokens: u32,
214 #[serde(skip_serializing_if = "Option::is_none")]
215 pub prompt_cache_hit_tokens: Option<u32>,
216 #[serde(skip_serializing_if = "Option::is_none")]
217 pub prompt_cache_miss_tokens: Option<u32>,
218 #[serde(skip_serializing_if = "Option::is_none")]
221 pub prompt_cache_write_tokens: Option<u32>,
222 #[serde(skip_serializing_if = "Option::is_none")]
223 pub reasoning_tokens: Option<u32>,
224 #[serde(default, skip_serializing_if = "Option::is_none")]
225 pub input_audio_tokens: Option<u32>,
226 #[serde(default, skip_serializing_if = "Option::is_none")]
227 pub output_audio_tokens: Option<u32>,
228 #[serde(default, skip_serializing_if = "Vec::is_empty")]
229 pub request_usages: Vec<Usage>,
230 #[serde(skip_serializing_if = "Option::is_none")]
235 pub reasoning_replay_tokens: Option<u32>,
236 #[serde(skip_serializing_if = "Option::is_none")]
237 pub server_tool_use: Option<ServerToolUsage>,
238 #[serde(default, skip_serializing_if = "Option::is_none")]
239 pub safety_tokens: Option<u32>,
240}
241
242#[must_use]
248pub fn context_window_for_model(model: &str) -> Option<u32> {
249 reviewed_model_limit(model, |row| row.context_window)
250 .or_else(|| explicit_context_window_hint(&model.to_ascii_lowercase()))
251}
252
253fn reviewed_model_limit(
254 model: &str,
255 limit: impl Fn(codewhale_config::catalog::reviewed::IntrinsicModel) -> Option<u32>,
256) -> Option<u32> {
257 codewhale_config::catalog::reviewed::intrinsic_model(model)
258 .and_then(&limit)
259 .or_else(|| {
260 reviewed_snapshot_model(model)
261 .and_then(codewhale_config::catalog::reviewed::intrinsic_model)
262 .and_then(limit)
263 })
264}
265
266#[must_use]
267pub fn generation_default_for_model(model: &str) -> Option<u32> {
268 reviewed_model_limit(model, |row| row.generation_default)
269}
270
271#[must_use]
272pub fn max_output_tokens_for_model(model: &str) -> Option<u32> {
273 reviewed_model_limit(model, |row| row.generation_default.or(row.max_output))
274}
275
276fn reviewed_model_flag(
277 model: &str,
278 flag: impl Fn(codewhale_config::catalog::reviewed::IntrinsicModel) -> Option<bool>,
279) -> Option<bool> {
280 codewhale_config::catalog::reviewed::intrinsic_model(model)
281 .and_then(&flag)
282 .or_else(|| {
283 reviewed_snapshot_model(model)
284 .and_then(codewhale_config::catalog::reviewed::intrinsic_model)
285 .and_then(flag)
286 })
287}
288
289#[must_use]
296pub fn model_reasoning_capability(model: &str) -> Option<bool> {
297 reviewed_model_flag(model, |row| row.reasoning)
298}
299
300#[must_use]
301pub fn model_supports_reasoning(model: &str) -> bool {
302 model_reasoning_capability(model).unwrap_or(false)
303}
304
305#[must_use]
306pub fn model_requires_thought_signatures(model: &str) -> bool {
307 reviewed_model_flag(model, |row| row.thought_signatures_required).unwrap_or(false)
308}
309
310#[must_use]
311pub fn model_can_disable_thinking(model: &str) -> bool {
312 reviewed_model_flag(model, |row| row.thinking_can_disable).unwrap_or(false)
313}
314
315#[must_use]
319pub fn effective_muse_wire_id(model: &str) -> &str {
320 model
321}
322
323#[must_use]
324pub fn model_is_openai_reasoning_family(model: &str) -> bool {
325 let lower = model.to_ascii_lowercase();
326 let reviewed = codewhale_config::catalog::reviewed::bundled_reviewed();
327 reviewed.openai_reasoning_ids.contains_key(&lower)
328 || reviewed_snapshot_model(&lower)
332 .is_some_and(|target| reviewed.openai_reasoning_ids.contains_key(target))
333}
334
335pub fn is_openai_gpt_56_api_model(model_lower: &str) -> bool {
336 codewhale_config::catalog::reviewed::bundled_reviewed()
337 .openai_reasoning_ids
338 .get(model_lower)
339 .is_some_and(|kind| kind == "gpt_56")
340}
341
342pub fn has_date_snapshot_suffix(model_lower: &str, prefix: &str) -> bool {
343 let Some(rest) = model_lower.strip_prefix(prefix) else {
344 return false;
345 };
346 let bytes = rest.as_bytes();
347 bytes.len() == 10
348 && bytes[4] == b'-'
349 && bytes[7] == b'-'
350 && bytes
351 .iter()
352 .enumerate()
353 .all(|(idx, byte)| idx == 4 || idx == 7 || byte.is_ascii_digit())
354}
355
356fn reviewed_snapshot_model(model: &str) -> Option<&'static str> {
372 let reviewed = codewhale_config::catalog::reviewed::bundled_reviewed();
373 let via_authored_contract = |candidate: &str| {
374 reviewed
375 .snapshot_prefixes
376 .iter()
377 .find_map(|(prefix, target)| {
378 has_date_snapshot_suffix(candidate, prefix).then_some(target.as_str())
379 })
380 };
381 let mut candidate = model.to_ascii_lowercase();
382 for _ in 0..3 {
383 if let Some(target) = via_authored_contract(&candidate) {
384 return Some(target);
385 }
386 let stripped = strip_snapshot_or_variant_suffix(&candidate)?;
387 if let Some((key, _)) = reviewed.intrinsic.get_key_value(&stripped) {
388 return Some(key.as_str());
389 }
390 candidate = stripped;
391 }
392 None
393}
394
395fn strip_snapshot_or_variant_suffix(id: &str) -> Option<String> {
399 for marker in ["-vision-exp", "-vision", "-exp"] {
400 if let Some(base) = id.strip_suffix(marker)
401 && !base.is_empty()
402 {
403 return Some(base.to_string());
404 }
405 }
406 strip_date_stamp(id)
407}
408
409fn strip_date_stamp(id: &str) -> Option<String> {
414 if id.len() > 11 && id.is_char_boundary(id.len() - 10) {
418 let (head, tail) = id.split_at(id.len() - 10);
419 if let Some(head) = head.strip_suffix('-')
420 && !head.is_empty()
421 && has_date_snapshot_suffix(tail, "")
422 {
423 return Some(head.to_string());
424 }
425 }
426 let (head, tail) = id.rsplit_once('-')?;
427 if head.is_empty() || tail.is_empty() {
428 return None;
429 }
430 let digits = tail.as_bytes();
431 if digits.len() == 4
432 && digits.iter().all(u8::is_ascii_digit)
433 && valid_month_day(&digits[0..2], &digits[2..4])
434 {
435 return Some(head.to_string());
436 }
437 None
438}
439
440fn valid_month_day(month_digits: &[u8], day_digits: &[u8]) -> bool {
442 let two = |bytes: &[u8]| -> Option<u32> {
443 (bytes.len() == 2 && bytes.iter().all(u8::is_ascii_digit))
444 .then(|| u32::from(bytes[0] - b'0') * 10 + u32::from(bytes[1] - b'0'))
445 };
446 match (two(month_digits), two(day_digits)) {
447 (Some(month), Some(day)) => (1..=12).contains(&month) && (1..=31).contains(&day),
448 _ => false,
449 }
450}
451
452#[must_use]
461pub fn name_suffix_context_window_hint(model: &str) -> Option<u32> {
462 if codewhale_config::catalog::reviewed::intrinsic_model(model)
463 .and_then(|row| row.context_window)
464 .is_some()
465 || reviewed_snapshot_model(model).is_some()
466 {
467 return None;
468 }
469 explicit_context_window_hint(&model.to_ascii_lowercase())
470}
471
472fn explicit_context_window_hint(model_lower: &str) -> Option<u32> {
475 let bytes = model_lower.as_bytes();
476 let mut i = 0usize;
477 while i < bytes.len() {
478 if bytes[i].is_ascii_digit() {
479 let start = i;
480 while i < bytes.len() && bytes[i].is_ascii_digit() {
481 i += 1;
482 }
483 if i >= bytes.len() || bytes[i] != b'k' {
484 continue;
485 }
486
487 let before_ok = start == 0 || !bytes[start - 1].is_ascii_alphanumeric();
488 let after_ok = i + 1 >= bytes.len() || !bytes[i + 1].is_ascii_alphanumeric();
489 if !before_ok || !after_ok {
490 continue;
491 }
492
493 if let Ok(kilo_tokens) = model_lower[start..i].parse::<u32>()
494 && (8..=1024).contains(&kilo_tokens)
495 {
496 return Some(kilo_tokens.saturating_mul(1000));
497 }
498 } else {
499 i += 1;
500 }
501 }
502 None
503}
504
505#[must_use]
508#[cfg(any(test, feature = "test-support"))]
509pub fn compaction_threshold_for_model_at_percent(model: &str, percent: f64) -> usize {
510 let Some(window) = context_window_for_model(model) else {
511 return DEFAULT_COMPACTION_TOKEN_THRESHOLD;
512 };
513
514 let percent = percent.clamp(10.0, 100.0);
515 let threshold = (f64::from(window) * percent / 100.0).round();
516 let threshold = if threshold.is_finite() && threshold > 0.0 {
517 threshold as u64
518 } else {
519 u64::from(window) * u64::from(COMPACTION_THRESHOLD_PERCENT) / 100
520 };
521 usize::try_from(threshold).unwrap_or(DEFAULT_COMPACTION_TOKEN_THRESHOLD)
522}
523
524#[must_use]
528#[cfg(test)]
529pub fn auto_compact_default_for_model(model: &str) -> bool {
530 context_window_for_model(model).is_some()
531}
532
533fn deserialize_redacted_data<'de, D>(deserializer: D) -> Result<String, D::Error>
540where
541 D: serde::Deserializer<'de>,
542{
543 Ok(Option::<String>::deserialize(deserializer)?.unwrap_or_default())
544}
545
546#[derive(Debug, Deserialize, Clone)]
547#[serde(tag = "type")]
548pub enum StreamEvent {
550 #[serde(rename = "tool_projection_warning")]
553 ToolProjectionWarning {
554 provider: String,
555 omitted_tool_names: Vec<String>,
556 omitted_tool_count: usize,
557 },
558 #[serde(rename = "message_start")]
559 MessageStart { message: MessageResponse },
560 #[serde(rename = "content_block_start")]
561 ContentBlockStart {
562 index: u32,
563 content_block: ContentBlockStart,
564 },
565 #[serde(rename = "content_block_delta")]
566 ContentBlockDelta { index: u32, delta: Delta },
567 #[serde(rename = "content_block_stop")]
568 ContentBlockStop { index: u32 },
569 #[serde(rename = "message_delta")]
570 MessageDelta {
571 delta: MessageDelta,
572 usage: Option<Usage>,
573 },
574 #[serde(rename = "message_stop")]
575 MessageStop,
576 #[serde(rename = "ping")]
577 Ping,
578 #[serde(rename = "error")]
580 Error { error: serde_json::Value },
581}
582
583#[derive(Debug, Deserialize, Clone)]
584#[serde(tag = "type")]
585pub enum ContentBlockStart {
587 #[serde(rename = "text")]
588 Text { text: String },
589 #[serde(rename = "thinking")]
590 Thinking { thinking: String },
591 #[serde(rename = "redacted_thinking")]
598 RedactedThinking {
599 #[serde(default, deserialize_with = "deserialize_redacted_data")]
606 data: String,
607 },
608 #[serde(rename = "tool_use")]
609 ToolUse {
610 id: String,
611 name: String,
612 input: serde_json::Value, #[serde(skip_serializing_if = "Option::is_none")]
614 caller: Option<ToolCaller>,
615 #[serde(skip_serializing_if = "Option::is_none")]
618 thought_signature: Option<String>,
619 },
620 #[serde(rename = "server_tool_use")]
621 ServerToolUse {
622 id: String,
623 name: String,
624 input: serde_json::Value,
625 #[serde(default, skip_serializing_if = "Option::is_none")]
626 kind: Option<String>,
627 #[serde(default, skip_serializing_if = "Option::is_none")]
628 caller: Option<ToolCaller>,
629 },
630 #[serde(rename = "web_search_tool_result")]
631 WebSearchToolResult {
632 tool_use_id: String,
633 content: serde_json::Value,
634 #[serde(default, skip_serializing_if = "Option::is_none")]
635 kind: Option<String>,
636 },
637 #[serde(rename = "web_fetch_tool_result")]
638 WebFetchToolResult {
639 tool_use_id: String,
640 content: serde_json::Value,
641 #[serde(default, skip_serializing_if = "Option::is_none")]
642 kind: Option<String>,
643 #[serde(default, skip_serializing_if = "Option::is_none")]
644 signature: Option<String>,
645 },
646 #[serde(rename = "web_search_call")]
647 WebSearchCall {
648 id: String,
649 status: String,
650 #[serde(default, skip_serializing_if = "Option::is_none")]
651 kind: Option<String>,
652 #[serde(default, skip_serializing_if = "Option::is_none")]
653 action: Option<serde_json::Value>,
654 },
655 #[serde(rename = "server_tool_calls")]
656 ServerToolCalls { steps: Vec<serde_json::Value> },
657}
658
659#[allow(clippy::enum_variant_names, clippy::large_enum_variant)]
661#[derive(Debug, Deserialize, Clone)]
662#[serde(tag = "type")]
663pub enum Delta {
665 #[serde(rename = "text_delta")]
666 TextDelta { text: String },
667 #[serde(rename = "thinking_delta")]
668 ThinkingDelta { thinking: String },
669 #[serde(rename = "input_json_delta")]
670 InputJsonDelta { partial_json: String },
671 #[serde(skip)]
672 ToolCallIdDelta { id: String },
673 #[serde(rename = "signature_delta")]
676 SignatureDelta { signature: String },
677 #[serde(rename = "thought_signature_delta")]
678 ThoughtSignatureDelta { thought_signature: String },
679 #[serde(rename = "reasoning_state_delta")]
682 ReasoningStateDelta { state: OpaqueReasoningState },
683 #[serde(rename = "citations_delta")]
684 CitationsDelta { citation: serde_json::Value },
685 #[serde(rename = "refusal_delta")]
686 RefusalDelta { refusal: String },
687 #[serde(rename = "web_search_call_delta")]
688 WebSearchCallDelta {
689 #[serde(default, skip_serializing_if = "Option::is_none")]
690 status: Option<String>,
691 #[serde(default, skip_serializing_if = "Option::is_none")]
692 kind: Option<String>,
693 #[serde(default, skip_serializing_if = "Option::is_none")]
694 action: Option<serde_json::Value>,
695 #[serde(default, skip_serializing_if = "Option::is_none")]
696 sources: Option<serde_json::Value>,
697 #[serde(default, skip_serializing_if = "Option::is_none")]
698 page: Option<serde_json::Value>,
699 #[serde(default, skip_serializing_if = "Option::is_none")]
700 signature: Option<String>,
701 #[serde(default, skip_serializing_if = "Option::is_none")]
702 error: Option<serde_json::Value>,
703 },
704 #[serde(rename = "server_tool_calls_delta")]
705 ServerToolCallsDelta { steps: Vec<serde_json::Value> },
706}
707
708#[derive(Debug, Deserialize, Clone, Default)]
709pub struct MessageDelta {
711 pub stop_reason: Option<String>,
712 pub stop_sequence: Option<String>,
713 #[serde(default, skip_serializing_if = "Option::is_none")]
714 pub title: Option<String>,
715 #[serde(default, skip_serializing_if = "Option::is_none")]
716 pub stop_details: Option<serde_json::Value>,
717 #[serde(default, skip_serializing_if = "Option::is_none")]
718 pub refusal: Option<String>,
719}
720
721#[cfg(test)]
722mod tests {
723 use super::*;
724 use std::any::TypeId;
725
726 #[test]
727 fn redacted_thinking_stream_start_decodes_with_its_payload() {
728 let block: ContentBlockStart = serde_json::from_value(serde_json::json!({
732 "type": "redacted_thinking",
733 "data": "ENC_REDACTED_PAYLOAD",
734 }))
735 .expect("redacted_thinking must decode");
736 match block {
737 ContentBlockStart::RedactedThinking { data } => {
738 assert_eq!(data, "ENC_REDACTED_PAYLOAD");
739 }
740 other => panic!("expected RedactedThinking, got {other:?}"),
741 }
742 }
743
744 #[test]
745 fn redacted_thinking_start_tolerates_an_explicit_null_payload() {
746 let block: ContentBlockStart = serde_json::from_value(serde_json::json!({
751 "type": "redacted_thinking",
752 "data": null,
753 }))
754 .expect("an explicit null payload must not fail the event decode");
755 match block {
756 ContentBlockStart::RedactedThinking { data } => {
757 assert_eq!(data, "");
758 }
759 other => panic!("expected RedactedThinking, got {other:?}"),
760 }
761 }
762
763 #[test]
771 fn unknown_reasoning_capability_is_observable() {
772 assert_eq!(
773 model_reasoning_capability("not-a-real-model-xyz"),
774 None,
775 "unknown must not collapse to false at this layer"
776 );
777 assert!(
778 !model_supports_reasoning("not-a-real-model-xyz"),
779 "legacy bool wrapper still defaults unknown to false"
780 );
781 assert_eq!(model_reasoning_capability("kimi-for-coding"), Some(true));
782 }
783
784 #[test]
785 fn catalog_alone_covers_the_models_removed_from_the_heuristic_pile() {
786 let removed = [
787 "claude-opus-4-8",
788 "claude-opus-5",
789 "claude-sonnet-4-6",
790 "claude-sonnet-5",
791 "claude-fable-5",
792 "gpt-5-codex",
793 "gpt-5.3-codex",
794 "trinity-mini",
795 "trinity-large-thinking",
796 "moonshotai/kimi-k2.7-code",
797 "kimi-k2.7-code",
798 "minimax/minimax-m3",
799 "minimax/minimax-m2.7",
800 "minimax-m2.7",
801 "qwen/qwen3.6-flash",
802 "mimo-v2.5",
803 "mimo-v2.5-pro",
804 "mimo-v2.5-pro-ultraspeed",
805 "z-ai/glm-5.2",
806 "z-ai/glm-5.3",
807 "z-ai/glm-5.3-flash",
808 "glm-5.2",
809 "glm-5.3",
810 "glm-5.3-flash",
811 "muse-spark-1.1",
812 "muse-spark-1.2",
813 "muse-spark-1.2-contributor",
814 "kimi-for-coding",
818 "kimi-for-coding-highspeed",
819 "kimi-k2.5",
820 "kimi-k2.6",
821 "qwen3.5-flash",
822 "qwen3.5-plus",
823 "qwen3.6-flash",
824 "qwen3.6-plus",
825 "qwen3.7-max",
826 "qwen3.7-plus",
827 "qwen3.8-flash",
828 "qwen3.8-max",
829 "qwen3.8-max-preview",
830 ];
831 for model in removed {
832 assert_eq!(
833 codewhale_config::catalog::reviewed::intrinsic_model(model)
834 .and_then(|row| row.reasoning),
835 Some(true),
836 "{model} no longer has a catalog row, but its heuristic arm was \
837 deleted in #6032 — restore the row, or put the arm back"
838 );
839 assert!(
840 model_supports_reasoning(model),
841 "{model} must still classify as reasoning-capable"
842 );
843 }
844 }
845
846 #[test]
852 fn models_dev_bundled_asset_covers_the_arms_deleted_from_the_heuristic_pile() {
853 let catalog = codewhale_config::catalog::bundled_models_dev_catalog();
854 let deleted = [
855 "qwen/qwen3.8-flash",
856 "qwen/qwen3.6-35b-a3b",
857 "qwen/qwen3.6-plus",
858 "qwen/qwen3.7-plus",
859 "glm-5.1",
860 "grok-4.6",
861 "grok-4.5",
862 "grok-4.3",
863 ];
864 for model in deleted {
865 assert_eq!(
866 catalog.reasoning_support(model),
867 Some(true),
868 "{model} lost its bundled Models.dev row — restore its reviewed \
869 source row (#6032)"
870 );
871 assert!(
872 model_supports_reasoning(model),
873 "{model} must still classify as reasoning-capable"
874 );
875 }
876 }
877
878 #[test]
883 fn remaining_reasoning_heuristic_arms_are_unsourced_in_models_dev_bundled_asset() {
884 for model in [
887 "kimi-k2.9",
888 "magistral-medium",
889 "claude-private-server",
890 "gpt-5.6-private",
891 ] {
892 assert_eq!(model_reasoning_capability(model), None, "{model}");
893 }
894 for model in [
895 "mistral-medium-latest",
896 "mistral-small-latest",
897 "gpt-5.5-2026-06-01",
898 "gpt-5.1-codex-max",
899 "codex-gpt-5.5-preview",
900 "chatgpt-gpt-5.5",
901 ] {
902 assert_eq!(model_reasoning_capability(model), Some(true), "{model}");
903 }
904 }
905
906 #[test]
907 fn output_limit_stop_reason_accepts_provider_aliases_only() {
908 for reason in [
909 "length",
910 "max_tokens",
911 "max_output_tokens",
912 " MAX_TOKENS ",
913 "incomplete:max_output_tokens",
914 ] {
915 assert!(is_output_limit_stop_reason(Some(reason)), "{reason}");
916 }
917 for reason in [None, Some("end_turn"), Some("tool_use"), Some("")] {
918 assert!(!is_output_limit_stop_reason(reason), "{reason:?}");
919 }
920 }
921
922 #[test]
923 fn stop_at_the_requested_ceiling_is_an_output_limit_stop() {
924 assert!(stop_contradicts_output_ceiling(
926 Some("stop"),
927 Some(16_000),
928 16_000
929 ));
930 assert!(stop_contradicts_output_ceiling(
931 Some(" STOP "),
932 Some(50),
933 50
934 ));
935 assert!(is_output_limit_stop_reason(Some(
936 OUTPUT_CEILING_STOP_REASON
937 )));
938 assert!(is_incomplete_stop_reason(Some(OUTPUT_CEILING_STOP_REASON)));
939
940 assert!(!stop_contradicts_output_ceiling(
942 Some("stop"),
943 Some(15_999),
944 16_000
945 ));
946 assert!(!stop_contradicts_output_ceiling(
948 Some("stop"),
949 Some(20_000),
950 16_000
951 ));
952 assert!(!stop_contradicts_output_ceiling(Some("stop"), None, 16_000));
954 assert!(!stop_contradicts_output_ceiling(Some("stop"), Some(0), 0));
956 for reason in [None, Some("end_turn"), Some("tool_calls"), Some("length")] {
958 assert!(
959 !stop_contradicts_output_ceiling(reason, Some(16_000), 16_000),
960 "{reason:?}"
961 );
962 }
963 }
964
965 #[test]
966 fn incomplete_stop_reason_never_accepts_unknown_responses_failures() {
967 assert!(is_incomplete_stop_reason(Some("incomplete:content_filter")));
968 assert!(is_incomplete_stop_reason(Some("content_filter")));
969 assert!(is_incomplete_stop_reason(Some(
970 "model_context_window_exceeded"
971 )));
972 assert!(is_incomplete_stop_reason(Some("max_tokens")));
973 assert!(!is_incomplete_stop_reason(Some("end_turn")));
974 assert_eq!(
975 stop_reason_detail(Some("incomplete:content_filter")),
976 "content_filter"
977 );
978 }
979
980 #[test]
981 fn historical_tui_request_path_is_the_core_request_type() {
982 assert_eq!(
983 TypeId::of::<MessageRequest>(),
984 TypeId::of::<codewhale_core::request::MessageRequest>()
985 );
986
987 let via_tui_path = MessageRequest {
988 model: "model".to_string(),
989 messages: vec![],
990 max_tokens: 1024,
991 system: None,
992 tools: None,
993 tool_choice: None,
994 metadata: None,
995 thinking: None,
996 reasoning_effort: None,
997 stream: Some(true),
998 temperature: None,
999 top_p: None,
1000 safety_check: None,
1001 title: None,
1002 };
1003 let via_core_path: codewhale_core::request::MessageRequest = via_tui_path.clone();
1004 assert_eq!(
1005 serde_json::to_vec(&via_tui_path).expect("serialize TUI path"),
1006 serde_json::to_vec(&via_core_path).expect("serialize core path")
1007 );
1008 }
1009
1010 #[test]
1011 fn interrupted_assistant_role_round_trips_as_distinct_session_item() {
1012 let message = Message {
1013 role: Role::InterruptedAssistant,
1014 content: vec![ContentBlock::Text {
1015 text: "partial output".to_string(),
1016 cache_control: None,
1017 }],
1018 };
1019 let encoded = serde_json::to_string(&message).expect("message should serialize");
1020 let decoded: Message = serde_json::from_str(&encoded).expect("message should deserialize");
1021 assert_eq!(decoded, message);
1022 assert_ne!(decoded.role, "assistant");
1023 }
1024
1025 #[test]
1026 fn unrecognized_deepseek_models_do_not_inherit_sibling_metadata() {
1027 for model in [
1028 "deepseek-v4-flash-20260423",
1029 "deepseek-v4-pro-20260423",
1030 "deepseek-coder",
1031 "deepseek-v3.2-0324",
1032 "deepseek-v4.1-flash-expires-on-0910",
1033 ] {
1034 assert_eq!(context_window_for_model(model), None, "{model}");
1035 }
1036 assert!(!model_supports_reasoning(
1037 "deepseek-v4.1-flash-expires-on-0910"
1038 ));
1039 }
1040
1041 #[test]
1042 fn deepseek_v4_models_map_to_1m_context_window() {
1043 assert_eq!(
1044 context_window_for_model("deepseek-v4-pro"),
1045 Some(DEEPSEEK_V4_CONTEXT_WINDOW_TOKENS)
1046 );
1047 assert_eq!(
1048 context_window_for_model("deepseek-v4-flash"),
1049 Some(DEEPSEEK_V4_CONTEXT_WINDOW_TOKENS)
1050 );
1051 assert_eq!(
1052 context_window_for_model("deepseek-ai/deepseek-v4-pro"),
1053 Some(DEEPSEEK_V4_CONTEXT_WINDOW_TOKENS)
1054 );
1055 }
1056
1057 #[test]
1058 fn deepseek_v4_output_caps_require_exact_catalog_metadata() {
1059 for model in [
1068 "deepseek-v4.1-flash-expires-on-0910",
1069 "deepseek-v4.1-pro",
1070 "vendor/deepseek-v4.1-flash",
1071 "deepseek-v4-flash-vendor",
1072 ] {
1073 assert!(
1074 codewhale_config::catalog::reviewed::intrinsic_model(model).is_none(),
1075 "{model}"
1076 );
1077 assert_eq!(max_output_tokens_for_model(model), None, "{model}");
1078 }
1079
1080 for model in [
1081 "deepseek-v4-flash",
1082 "deepseek-v4-pro",
1083 "deepseek-v4-flash-vision-exp",
1084 "deepseek-v4.1-flash",
1085 "DEEPSEEK-V4-FLASH",
1086 ] {
1087 assert_eq!(max_output_tokens_for_model(model), Some(384_000), "{model}");
1088 }
1089 }
1090
1091 #[test]
1092 fn recent_openrouter_large_models_have_static_windows() {
1093 for (model, expected_window) in [
1094 ("arcee-ai/trinity-large-thinking", 262_144),
1095 ("trinity-large-thinking", 262_144),
1096 (concat!("qwen/", "qwen3.8-flash"), 1_000_000),
1097 (concat!("qwen/", "qwen3.6-flash"), 1_000_000),
1098 (concat!("qwen/", "qwen3.6-35b-a3b"), 262_144),
1099 (concat!("qwen/", "qwen3.6-max-preview"), 262_144),
1100 (concat!("qwen/", "qwen3.6-plus"), 1_000_000),
1101 (concat!("xiaomi/", "mimo-v2.5-pro"), 1_000_000),
1102 ("mimo-v2.5-pro", 1_000_000),
1103 ("mimo-v2.5-pro-ultraspeed", 1_000_000),
1104 ("mimo-v2.5", 1_000_000),
1105 ("minimax/minimax-m3", 1_000_000),
1106 ("minimax/minimax-m2.7", 204_800),
1107 ("moonshotai/kimi-k2.7-code", 262_144),
1108 ("moonshotai/kimi-k2.6", 262_144),
1109 ("google/gemma-4-31b-it", 262_144),
1110 ("z-ai/glm-5.1", 202_752),
1111 ("z-ai/glm-5.2", 1_000_000),
1112 ("z-ai/glm-5.3", 1_000_000),
1113 ("z-ai/glm-5.3-flash", 1_000_000),
1114 ] {
1115 assert_eq!(context_window_for_model(model), Some(expected_window));
1116 assert!(model_supports_reasoning(model));
1117 }
1118 }
1119
1120 #[test]
1121 fn openai_api_and_codex_models_have_verified_context_metadata() {
1122 for model in ["gpt-5.6", "gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"] {
1123 assert_eq!(context_window_for_model(model), Some(1_050_000));
1124 assert_eq!(max_output_tokens_for_model(model), Some(128_000));
1125 assert!(model_supports_reasoning(model));
1126 assert_eq!(
1127 compaction_threshold_for_model_at_percent(model, 80.0),
1128 840_000
1129 );
1130 }
1131
1132 for model in [
1133 "gpt-5.5",
1134 "gpt-5.5-pro",
1135 "gpt-5.5-2026-04-23",
1136 "gpt-5.5-pro-2026-04-23",
1137 ] {
1138 assert_eq!(context_window_for_model(model), Some(1_050_000));
1139 assert_eq!(max_output_tokens_for_model(model), Some(128_000));
1140 assert!(model_supports_reasoning(model));
1141 assert_eq!(
1142 compaction_threshold_for_model_at_percent(model, 80.0),
1143 840_000
1144 );
1145 }
1146
1147 for model in [
1148 "gpt-5-codex",
1149 "gpt-5.1-codex",
1150 "gpt-5.1-codex-mini",
1151 "gpt-5.1-codex-max",
1152 "gpt-5.2-codex",
1153 "gpt-5.3-codex",
1154 "codex-gpt-5.5",
1155 "chatgpt-gpt-5.5",
1156 "gpt-5.5-codex",
1157 "gpt-5.5-codex-preview",
1158 ] {
1159 assert_eq!(context_window_for_model(model), Some(400_000));
1160 assert_eq!(max_output_tokens_for_model(model), Some(128_000));
1161 assert!(model_supports_reasoning(model));
1162 assert_eq!(
1163 compaction_threshold_for_model_at_percent(model, 80.0),
1164 320_000
1165 );
1166 }
1167
1168 assert_eq!(context_window_for_model("gpt-5.5-nano"), None);
1169 assert_eq!(max_output_tokens_for_model("gpt-5.5-nano"), None);
1170 assert!(!model_supports_reasoning("gpt-5.5-nano"));
1171 }
1172
1173 #[test]
1174 fn anthropic_stepfun_and_sakana_limits_match_2026_07_09_audit() {
1175 assert_eq!(
1179 max_output_tokens_for_model("claude-sonnet-4-6"),
1180 Some(128_000)
1181 );
1182 assert_eq!(
1183 max_output_tokens_for_model("claude-haiku-4-5"),
1184 Some(64_000)
1185 );
1186 assert_eq!(max_output_tokens_for_model("step-3.7-flash"), Some(256_000));
1190 assert_eq!(context_window_for_model("step-3.7-flash"), Some(256_000));
1191 for model in ["fugu-ultra", "fugu-ultra-20260615"] {
1195 assert_eq!(context_window_for_model(model), Some(1_000_000), "{model}");
1196 assert_eq!(max_output_tokens_for_model(model), Some(131_000), "{model}");
1197 }
1198 }
1199
1200 #[test]
1201 fn stepfun_current_coding_models_have_verified_metadata() {
1202 assert_eq!(context_window_for_model("step-5-preview"), Some(1_000_000));
1203 assert_eq!(
1204 max_output_tokens_for_model("step-5-preview"),
1205 Some(1_000_000)
1206 );
1207 for model in [
1208 "step-5-preview",
1209 "step-3.7-flash",
1210 "step-3.5-flash",
1211 "step-3.5-flash-2603",
1212 ] {
1213 assert!(model_supports_reasoning(model), "{model}");
1214 }
1215 for model in ["step-3.5-flash", "step-3.5-flash-2603"] {
1216 assert_eq!(context_window_for_model(model), Some(256_000));
1217 assert_eq!(max_output_tokens_for_model(model), None);
1218 }
1219 }
1220
1221 #[test]
1222 fn claude_fable_5_and_sonnet_5_have_verified_metadata() {
1223 for model in ["claude-fable-5", "claude-sonnet-5"] {
1226 assert_eq!(context_window_for_model(model), Some(1_000_000), "{model}");
1227 assert_eq!(max_output_tokens_for_model(model), Some(128_000), "{model}");
1228 assert!(model_supports_reasoning(model), "{model}");
1229 }
1230 }
1231
1232 #[test]
1233 fn claude_opus_5_has_verified_metadata() {
1234 assert_eq!(context_window_for_model("claude-opus-5"), Some(1_000_000));
1238 assert_eq!(max_output_tokens_for_model("claude-opus-5"), Some(128_000));
1239 assert!(model_supports_reasoning("claude-opus-5"));
1240 }
1241
1242 #[test]
1243 fn kimi_k2_7_code_highspeed_shares_the_k2_7_code_limits() {
1244 for model in [
1247 "kimi-k2.7-code-highspeed",
1248 "moonshotai/kimi-k2.7-code-highspeed",
1249 ] {
1250 assert_eq!(context_window_for_model(model), Some(262_144), "{model}");
1251 assert_eq!(max_output_tokens_for_model(model), Some(32_768), "{model}");
1252 assert!(model_supports_reasoning(model), "{model}");
1253 }
1254 }
1255
1256 #[test]
1257 fn gemini_api_models_have_documented_token_limits() {
1258 for model in [
1262 "gemini-4-carbon",
1263 "gemini-4-argon",
1264 "gemini-3.7-flash",
1265 "gemini-3.6-flash",
1266 "gemini-3.5-flash",
1267 "gemini-3.5-flash-lite",
1268 "gemini-3.1-pro-preview",
1269 "gemini-3-pro-preview",
1270 "gemini-2.5-pro",
1271 "gemini-2.5-flash",
1272 ] {
1273 assert_eq!(context_window_for_model(model), Some(1_048_576), "{model}");
1274 assert_eq!(max_output_tokens_for_model(model), Some(65_536), "{model}");
1275 }
1276 }
1277
1278 #[test]
1279 fn muse_spark_has_verified_context_and_reasoning_metadata() {
1280 assert_eq!(context_window_for_model("muse-spark-1.1"), Some(1_000_000));
1281 assert_eq!(max_output_tokens_for_model("muse-spark-1.1"), Some(32_000));
1282 assert!(model_supports_reasoning("muse-spark-1.1"));
1283 assert_eq!(context_window_for_model("muse-spark-1.2"), Some(1_000_000));
1285 assert_eq!(max_output_tokens_for_model("muse-spark-1.2"), Some(32_000));
1286 assert!(model_supports_reasoning("muse-spark-1.2"));
1287 assert_eq!(
1289 context_window_for_model("muse-spark-1.2-contributor"),
1290 Some(1_000_000)
1291 );
1292 assert_eq!(
1293 max_output_tokens_for_model("muse-spark-1.2-contributor"),
1294 Some(32_000)
1295 );
1296 assert!(model_supports_reasoning("muse-spark-1.2-contributor"));
1297 }
1298
1299 #[test]
1300 fn modelstudio_qwen38_max_is_1m_context_not_128k() {
1301 for model in ["qwen3.8-max", "qwen3.8-max-preview"] {
1304 assert_eq!(context_window_for_model(model), Some(1_000_000), "{model}");
1305 assert_eq!(max_output_tokens_for_model(model), Some(131_072), "{model}");
1306 }
1307 }
1308
1309 #[test]
1310 fn openrouter_qwen38_flash_is_1m_context_with_128k_output() {
1311 for model in ["qwen/qwen3.8-flash", "qwen3.8-flash"] {
1314 assert_eq!(context_window_for_model(model), Some(1_000_000), "{model}");
1315 assert_eq!(max_output_tokens_for_model(model), Some(131_072), "{model}");
1316 assert!(model_supports_reasoning(model), "{model}");
1317 }
1318 }
1319
1320 #[test]
1321 fn modelstudio_bare_qwen_models_support_reasoning() {
1322 for model in [
1326 "qwen3.8-max",
1327 "qwen3.8-max-preview",
1328 "qwen3.7-max",
1329 "qwen3.7-plus",
1330 "qwen3.6-plus",
1331 "qwen3.6-flash",
1332 "qwen3.5-plus",
1333 "qwen3.5-flash",
1334 ] {
1335 assert!(model_supports_reasoning(model), "{model}");
1336 }
1337 }
1338
1339 #[test]
1340 fn model_metadata_catalog_override_flows_through_models_chokepoint() {
1341 let private=codewhale_config::models_dev::ModelsDevCatalog::parse_json(r#"{"providers":{"private":{"models":{"catalog-only-model":{"limit":{"context":777000,"output":55000},"reasoning":true}}}}}"#).unwrap();
1342 assert!(
1343 codewhale_config::catalog::reviewed::intrinsic_model_in(&private, "catalog-only-model")
1344 .is_none()
1345 );
1346 assert_eq!(context_window_for_model("catalog-only-model"), None);
1347 assert_eq!(max_output_tokens_for_model("catalog-only-model"), None);
1348 assert_eq!(model_reasoning_capability("catalog-only-model"), None);
1349 }
1350
1351 #[test]
1352 fn moonshot_native_kimi_ids_support_reasoning_including_coding_route() {
1353 assert!(model_supports_reasoning("kimi-k2.7-code"));
1356 assert!(model_supports_reasoning("kimi-k2.6"));
1357 assert!(model_supports_reasoning("kimi-for-coding"));
1358 assert!(model_supports_reasoning("kimi-for-coding-highspeed"));
1359 assert!(model_supports_reasoning("kimi-k2.5"));
1360 }
1361
1362 #[test]
1363 fn xai_grok_models_have_static_context_metadata() {
1364 for (model, expected_window, supports_reasoning) in [
1365 ("grok-4.6", 500_000, true),
1366 ("grok-4.5", 500_000, true),
1367 ("grok-4.3", 1_000_000, true),
1368 ("grok-build", 512_000, true),
1369 ("grok-composer-2.5-fast", 200_000, false),
1370 ("grok-4.20-0309-reasoning", 2_000_000, true),
1371 ("grok-4.20-0309-non-reasoning", 2_000_000, false),
1372 ] {
1373 assert_eq!(context_window_for_model(model), Some(expected_window));
1374 assert_eq!(max_output_tokens_for_model(model), None);
1375 assert_eq!(model_supports_reasoning(model), supports_reasoning);
1376 }
1377 }
1378
1379 #[test]
1380 fn arcee_direct_models_preserve_verified_capabilities_only() {
1381 assert_eq!(
1382 context_window_for_model("trinity-large-preview"),
1383 Some(262_144)
1384 );
1385 assert!(!model_supports_reasoning("trinity-large-preview"));
1386 assert_eq!(context_window_for_model("trinity-mini"), Some(128_000));
1387 assert_eq!(max_output_tokens_for_model("trinity-mini"), None);
1388 assert!(model_supports_reasoning("trinity-mini"));
1389 }
1390
1391 #[test]
1392 fn qwen37_plus_and_inkling_reasoning_do_not_invent_limits() {
1393 for model in ["qwen/qwen3.7-plus", "thinkingmachines/inkling"] {
1394 assert_eq!(context_window_for_model(model), None, "{model}");
1395 assert_eq!(max_output_tokens_for_model(model), None, "{model}");
1396 assert!(model_supports_reasoning(model), "{model}");
1397 }
1398 }
1399
1400 #[test]
1401 fn recent_openrouter_large_models_have_known_output_caps() {
1402 assert_eq!(
1403 max_output_tokens_for_model("arcee-ai/trinity-large-thinking"),
1404 Some(262_144)
1405 );
1406 assert_eq!(
1407 max_output_tokens_for_model("trinity-large-thinking"),
1408 Some(262_144)
1409 );
1410 assert_eq!(
1411 max_output_tokens_for_model(concat!("qwen/", "qwen3.8-flash")),
1412 Some(131_072)
1413 );
1414 assert_eq!(
1415 max_output_tokens_for_model(concat!("qwen/", "qwen3.6-flash")),
1416 Some(65_536)
1417 );
1418 assert_eq!(
1419 max_output_tokens_for_model(concat!("qwen/", "qwen3.6-max-preview")),
1420 Some(65_536)
1421 );
1422 assert_eq!(
1423 max_output_tokens_for_model(concat!("qwen/", "qwen3.6-plus")),
1424 Some(65_536)
1425 );
1426 assert_eq!(
1427 max_output_tokens_for_model(concat!("xiaomi/", "mimo-v2.5-pro")),
1428 Some(131_072)
1429 );
1430 assert_eq!(max_output_tokens_for_model("mimo-v2.5-pro"), Some(131_072));
1431 assert_eq!(
1432 max_output_tokens_for_model("mimo-v2.5-pro-ultraspeed"),
1433 Some(131_072)
1434 );
1435 assert_eq!(max_output_tokens_for_model("mimo-v2.5"), Some(131_072));
1436 assert_eq!(
1437 max_output_tokens_for_model("minimax/minimax-m3"),
1438 Some(524_288)
1439 );
1440 assert_eq!(max_output_tokens_for_model("z-ai/glm-5.1"), Some(131_072));
1441 assert_eq!(max_output_tokens_for_model("z-ai/glm-5.2"), Some(131_072));
1442 assert_eq!(max_output_tokens_for_model("z-ai/glm-5.3"), Some(131_072));
1443 assert_eq!(
1444 max_output_tokens_for_model("z-ai/glm-5-turbo"),
1445 Some(131_072)
1446 );
1447 assert_eq!(max_output_tokens_for_model("glm-5-turbo"), Some(131_072));
1448 }
1449
1450 #[test]
1451 fn k3_route_ids_use_verified_contracts_not_legacy_128k() {
1452 assert_eq!(context_window_for_model("kimi-k3"), Some(1_048_576));
1454 assert_eq!(
1455 context_window_for_model("opencode-go/kimi-k3"),
1456 Some(1_048_576)
1457 );
1458 assert_eq!(context_window_for_model("k3"), Some(262_144));
1462 assert_eq!(context_window_for_model("k3-256k"), Some(262_144));
1463 assert_eq!(max_output_tokens_for_model("k3"), Some(131_072));
1464 assert_eq!(max_output_tokens_for_model("k3-256k"), Some(131_072));
1465 assert_eq!(max_output_tokens_for_model("kimi-k3"), Some(131_072));
1466 assert_ne!(
1468 context_window_for_model("k3"),
1469 max_output_tokens_for_model("k3")
1470 );
1471 assert_ne!(
1472 context_window_for_model("kimi-k3"),
1473 max_output_tokens_for_model("kimi-k3")
1474 );
1475 }
1476
1477 #[test]
1478 fn kimi_code_membership_ids_mirror_their_family_facts() {
1479 assert_eq!(
1485 context_window_for_model("kimi-for-coding-highspeed"),
1486 Some(262_144)
1487 );
1488 assert_eq!(
1489 max_output_tokens_for_model("kimi-for-coding-highspeed"),
1490 None
1491 );
1492 assert_eq!(max_output_tokens_for_model("kimi-for-coding"), None);
1493 assert!(model_supports_reasoning("kimi-for-coding-highspeed"));
1494 }
1495
1496 #[test]
1497 fn bare_provider_model_ids_mirror_vendor_prefixed_rows() {
1498 for (model, expected_window) in [
1502 ("kimi-k3", 1_048_576),
1503 ("kimi-k2.7-code", 262_144),
1504 ("kimi-k2.6", 262_144),
1505 ("minimax-m3", 1_000_000),
1506 ("minimax-m2.7", 204_800),
1507 ("minimax-m2.5-highspeed", 204_800),
1508 ("minimax-m2", 204_800),
1509 ("glm-5.1", 202_752),
1510 ("glm-5.2", 1_000_000),
1511 ("glm-5.3", 1_000_000),
1513 ("glm-5.3-flash", 1_000_000),
1514 ("glm-5-turbo", 202_752),
1515 ] {
1516 assert_eq!(context_window_for_model(model), Some(expected_window));
1517 assert!(model_supports_reasoning(model));
1518 }
1519 assert_eq!(context_window_for_model("kimi-for-coding"), Some(262_144));
1520 assert!(model_supports_reasoning("kimi-for-coding"));
1521 assert_eq!(context_window_for_model("glm-5v-turbo"), Some(202_752));
1522 assert!(!model_supports_reasoning("glm-5v-turbo"));
1523 assert_eq!(context_window_for_model("z-ai/glm-5-turbo"), Some(202_752));
1526 assert!(model_supports_reasoning("z-ai/glm-5-turbo"));
1527 assert_eq!(
1528 codewhale_config::catalog::reviewed::intrinsic_model("kimi-k2.7-code")
1529 .and_then(|row| row.generation_default.or(row.max_output)),
1530 Some(32_768)
1531 );
1532 assert_eq!(max_output_tokens_for_model("kimi-k2.7-code"), Some(32_768));
1533 assert_eq!(max_output_tokens_for_model("kimi-k2.6"), Some(32_768));
1534 assert_eq!(max_output_tokens_for_model("kimi-for-coding"), None);
1535 assert_eq!(max_output_tokens_for_model("kimi-k3"), Some(131_072));
1536 assert_eq!(max_output_tokens_for_model("minimax-m3"), Some(524_288));
1537 assert_eq!(max_output_tokens_for_model("glm-5.1"), Some(131_072));
1538 assert_eq!(max_output_tokens_for_model("glm-5.2"), Some(131_072));
1539 assert_eq!(max_output_tokens_for_model("glm-5.3"), Some(131_072));
1540 assert_eq!(max_output_tokens_for_model("glm-5.3-flash"), Some(131_072));
1541 }
1542
1543 #[test]
1544 fn deepseek_models_with_k_suffix_use_hint() {
1545 assert_eq!(context_window_for_model("deepseek-v3.2-32k"), Some(32_000));
1546 assert_eq!(
1547 context_window_for_model("deepseek-v3.2-256k-preview"),
1548 Some(256_000)
1549 );
1550 assert_eq!(context_window_for_model("deepseek-v3.2-2k-preview"), None);
1551 }
1552
1553 #[test]
1559 fn deepseek_v4_snapshot_and_variant_ids_inherit_reviewed_facts() {
1560 for model in [
1561 "deepseek-v4-pro-0813",
1562 "DeepSeek-V4-Pro-0813",
1563 "deepseek-v4-pro-2025-08-13",
1564 "deepseek-v4-flash-vision",
1565 "deepseek-v4-flash-vision-0813",
1566 ] {
1567 assert_eq!(context_window_for_model(model), Some(1_000_000), "{model}");
1568 assert_eq!(max_output_tokens_for_model(model), Some(384_000), "{model}");
1569 assert!(model_supports_reasoning(model), "{model}");
1570 }
1571 for model in [
1575 "not-a-model-0813",
1576 "deepseek-v4-pro-9913",
1577 "deepseek-v4-pro-08130",
1578 "deepseek-v4-pro-x0813",
1579 "deepseek-v4-pro-20250813",
1580 ] {
1581 assert_eq!(context_window_for_model(model), None, "{model}");
1582 assert_eq!(model_reasoning_capability(model), None, "{model}");
1583 }
1584 assert!(!model_is_openai_reasoning_family("deepseek-v4-pro-0813"));
1587 assert!(model_is_openai_reasoning_family("gpt-5.5-2026-06-01"));
1588 }
1589
1590 #[test]
1594 fn non_ascii_ids_do_not_panic_the_snapshot_scan() {
1595 for model in ["模型模型模型模型", "ローカルモデル-2026", "モデル-0813"] {
1596 assert_eq!(context_window_for_model(model), None, "{model}");
1597 assert_eq!(model_reasoning_capability(model), None, "{model}");
1598 }
1599 }
1600
1601 #[test]
1602 fn compaction_threshold_scales_with_context_window() {
1603 assert_eq!(
1604 compaction_threshold_for_model_at_percent("deepseek-v3.2-128k", 80.0),
1605 102_400
1606 );
1607 assert_eq!(
1614 compaction_threshold_for_model_at_percent("unknown-model", 80.0),
1615 102_400
1616 );
1617 }
1618
1619 #[test]
1620 fn compaction_scales_for_deepseek_v4_1m_context() {
1621 assert_eq!(
1622 compaction_threshold_for_model_at_percent("deepseek-v4-pro", 80.0),
1623 800_000
1624 );
1625 }
1626
1627 #[test]
1628 fn compaction_threshold_honors_configured_percent() {
1629 assert_eq!(
1630 compaction_threshold_for_model_at_percent("deepseek-v4-pro", 75.0),
1631 750_000
1632 );
1633 assert_eq!(
1634 compaction_threshold_for_model_at_percent("trinity-large-thinking", 80.0),
1635 209_715
1636 );
1637 }
1638
1639 #[test]
1640 fn auto_compaction_defaults_on_for_known_supported_model_windows() {
1641 assert!(auto_compact_default_for_model("trinity-large-thinking"));
1642 assert!(auto_compact_default_for_model("deepseek-v3.2-128k"));
1643 assert!(auto_compact_default_for_model("deepseek-v4-pro"));
1644 assert!(auto_compact_default_for_model("mimo-v2.5-pro"));
1645 assert!(!auto_compact_default_for_model("unknown-model"));
1646 }
1647
1648 #[test]
1649 fn deepseek_chatbot_models_usage_and_refusal_helpers() {
1650 assert!(is_refusal_stop_reason(Some("refusal")));
1651 assert!(is_refusal_stop_reason(Some("content_filter")));
1652 assert!(!is_refusal_stop_reason(Some("end_turn")));
1653 assert!(is_incomplete_stop_reason(Some("refusal")));
1654
1655 let usage: Usage = serde_json::from_value(serde_json::json!({
1656 "input_tokens": 120,
1657 "output_tokens": 45,
1658 "safety_tokens": 12,
1659 "server_tool_use": {
1660 "web_search_requests": 2,
1661 "web_fetch_requests": 1,
1662 "tool_search": 2,
1663 "tool_fetch": 1,
1664 "tool_image_search": 3
1665 }
1666 }))
1667 .expect("usage deserializes");
1668 assert_eq!(usage.safety_tokens, Some(12));
1669 let server_tool_use = usage.server_tool_use.expect("server_tool_use present");
1670 assert_eq!(server_tool_use.web_search_requests, Some(2));
1671 assert_eq!(server_tool_use.web_fetch_requests, Some(1));
1672 assert_eq!(server_tool_use.tool_search, Some(2));
1673 assert_eq!(server_tool_use.tool_fetch, Some(1));
1674 assert_eq!(server_tool_use.tool_image_search, Some(3));
1675
1676 let delta: MessageDelta = serde_json::from_value(serde_json::json!({
1677 "stop_reason": "refusal",
1678 "title": "Rust 2024 Summary",
1679 "refusal": "Policy blocked output",
1680 "stop_details": {"reason": "safety"}
1681 }))
1682 .expect("delta deserializes");
1683 assert_eq!(delta.title.as_deref(), Some("Rust 2024 Summary"));
1684 assert_eq!(delta.refusal.as_deref(), Some("Policy blocked output"));
1685 assert!(delta.stop_details.is_some());
1686 }
1687
1688 #[test]
1689 fn offline_seed_models_resolve_limits_and_reasoning() {
1690 for (model, context, output) in [
1691 ("gemini-4-neon", 1_048_576, 65_536),
1692 ("gpt-6.2-astra", 1_050_000, 128_000),
1693 ("gpt-6.2-sol", 1_050_000, 128_000),
1694 ("muse-ember-1.0", 1_048_576, 524_288),
1695 ] {
1696 assert_eq!(context_window_for_model(model), Some(context), "{model}");
1697 assert_eq!(max_output_tokens_for_model(model), Some(output), "{model}");
1698 assert!(model_supports_reasoning(model), "{model}");
1699 }
1700 assert!(!model_can_disable_thinking("gemini-4-neon"));
1701 }
1702}