awaken-contract 0.4.0

Core types, traits, and state model for the Awaken AI agent runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! Inference response types and override configuration.

use super::content::{ContentBlock, extract_text};
use super::message::ToolCall;
use serde::{Deserialize, Serialize};

/// Why the LLM stopped generating output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StopReason {
    /// Model finished naturally.
    EndTurn,
    /// Output hit the `max_tokens` limit.
    MaxTokens,
    /// Model emitted one or more tool-use calls.
    ToolUse,
    /// A stop sequence was matched.
    StopSequence,
}

/// Provider-neutral token usage.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TokenUsage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_tokens: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion_tokens: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_tokens: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_read_tokens: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_creation_tokens: Option<i32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thinking_tokens: Option<i32>,
}

/// Result of stream collection used by runtime and plugin phase contracts.
#[derive(Debug, Clone)]
pub struct StreamResult {
    /// Content blocks from the LLM response.
    pub content: Vec<ContentBlock>,
    /// Collected tool calls.
    pub tool_calls: Vec<ToolCall>,
    /// Token usage from the LLM response.
    pub usage: Option<TokenUsage>,
    /// Why the model stopped generating.
    pub stop_reason: Option<StopReason>,
    /// True when tool calls were started but their argument JSON was truncated
    /// (e.g. the response hit `MaxTokens` mid-generation).
    pub has_incomplete_tool_calls: bool,
}

impl StreamResult {
    /// Check if tool execution is needed.
    pub fn needs_tools(&self) -> bool {
        !self.tool_calls.is_empty()
    }

    /// Extract concatenated text from content blocks.
    pub fn text(&self) -> String {
        extract_text(&self.content)
    }

    /// Whether this result was truncated mid-generation and should be recovered
    /// via a continuation prompt.
    pub fn needs_truncation_recovery(&self) -> bool {
        self.stop_reason == Some(StopReason::MaxTokens)
            && (self.has_incomplete_tool_calls
                || (self.tool_calls.is_empty() && self.has_text_output()))
    }

    fn has_text_output(&self) -> bool {
        self.content
            .iter()
            .any(|block| matches!(block, ContentBlock::Text { text } if !text.is_empty()))
    }
}

/// Inference error emitted by the loop and consumed by telemetry plugins.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct InferenceError {
    /// Stable error class used for metrics/telemetry dimensions.
    #[serde(rename = "type")]
    pub error_type: String,
    /// Human-readable error message.
    pub message: String,
    /// Classified error category (e.g. `rate_limit`, `timeout`, `connection`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_class: Option<String>,
}

/// LLM response: success with a [`StreamResult`] or failure with an [`InferenceError`].
#[derive(Debug, Clone)]
pub struct LLMResponse {
    pub outcome: Result<StreamResult, InferenceError>,
}

impl LLMResponse {
    pub fn success(result: StreamResult) -> Self {
        Self {
            outcome: Ok(result),
        }
    }

    pub fn error(error: InferenceError) -> Self {
        Self {
            outcome: Err(error),
        }
    }
}

// ---------------------------------------------------------------------------
// Inference override / context types
// ---------------------------------------------------------------------------

/// Context window management policy.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ContextWindowPolicy {
    /// Model's total context window size in tokens.
    pub max_context_tokens: usize,
    /// Tokens reserved for model output.
    pub max_output_tokens: usize,
    /// Minimum number of recent messages to always preserve.
    pub min_recent_messages: usize,
    /// Whether to enable prompt caching.
    pub enable_prompt_cache: bool,
    /// Token count threshold that triggers auto-compaction. `None` disables.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub autocompact_threshold: Option<usize>,
    /// Auto-compaction strategy.
    #[serde(default)]
    pub compaction_mode: ContextCompactionMode,
    /// Number of recent raw messages to preserve in suffix compaction mode.
    #[serde(default = "default_compaction_raw_suffix_messages")]
    pub compaction_raw_suffix_messages: usize,
}

const fn default_compaction_raw_suffix_messages() -> usize {
    2
}

impl Default for ContextWindowPolicy {
    fn default() -> Self {
        Self {
            max_context_tokens: 200_000,
            max_output_tokens: 16_384,
            min_recent_messages: 10,
            enable_prompt_cache: true,
            autocompact_threshold: None,
            compaction_mode: ContextCompactionMode::KeepRecentRawSuffix,
            compaction_raw_suffix_messages: default_compaction_raw_suffix_messages(),
        }
    }
}

/// Auto-compaction strategy.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum ContextCompactionMode {
    #[default]
    KeepRecentRawSuffix,
    CompactToSafeFrontier,
}

/// Reasoning effort hint, independent of any LLM provider SDK.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ReasoningEffort {
    None,
    Low,
    Medium,
    High,
    Max,
    /// Explicit token budget for reasoning/thinking.
    Budget(u32),
}

/// Unified per-inference override for model selection and inference parameters.
///
/// All fields are `Option` — `None` means "use the agent-level default".
/// Multiple plugins can emit overrides; fields are merged with last-wins semantics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InferenceOverride {
    /// Upstream model name to send to the already resolved provider.
    ///
    /// This does not re-resolve `AgentSpec.model_id` and therefore does not switch
    /// providers. Use a different `AgentSpec.model_id` or agent handoff when a run
    /// must move to another provider.
    pub upstream_model: Option<String>,
    /// Upstream fallback model names for the same resolved provider.
    pub fallback_upstream_models: Option<Vec<String>>,
    /// Sampling temperature.
    pub temperature: Option<f64>,
    /// Maximum output tokens.
    pub max_tokens: Option<u32>,
    /// Nucleus sampling (top-p).
    pub top_p: Option<f64>,
    /// Reasoning effort hint.
    pub reasoning_effort: Option<ReasoningEffort>,
}

impl InferenceOverride {
    /// Returns true if all fields are `None` (no override set).
    pub fn is_empty(&self) -> bool {
        self.upstream_model.is_none()
            && self.fallback_upstream_models.is_none()
            && self.temperature.is_none()
            && self.max_tokens.is_none()
            && self.top_p.is_none()
            && self.reasoning_effort.is_none()
    }

    /// Merge `other` into `self` with last-wins semantics per field.
    pub fn merge(&mut self, other: InferenceOverride) {
        if other.upstream_model.is_some() {
            self.upstream_model = other.upstream_model;
        }
        if other.fallback_upstream_models.is_some() {
            self.fallback_upstream_models = other.fallback_upstream_models;
        }
        if other.temperature.is_some() {
            self.temperature = other.temperature;
        }
        if other.max_tokens.is_some() {
            self.max_tokens = other.max_tokens;
        }
        if other.top_p.is_some() {
            self.top_p = other.top_p;
        }
        if other.reasoning_effort.is_some() {
            self.reasoning_effort = other.reasoning_effort;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn default_policy_uses_suffix_compaction_defaults() {
        let policy = ContextWindowPolicy::default();
        assert_eq!(
            policy.compaction_mode,
            ContextCompactionMode::KeepRecentRawSuffix
        );
        assert_eq!(policy.compaction_raw_suffix_messages, 2);
    }

    #[test]
    fn policy_deserialization_backfills_new_compaction_fields() {
        let value = json!({
            "max_context_tokens": 4096,
            "max_output_tokens": 512,
            "min_recent_messages": 4,
            "enable_prompt_cache": false,
            "autocompact_threshold": 2048
        });

        let policy: ContextWindowPolicy = serde_json::from_value(value).unwrap();
        assert_eq!(
            policy.compaction_mode,
            ContextCompactionMode::KeepRecentRawSuffix
        );
        assert_eq!(policy.compaction_raw_suffix_messages, 2);
    }

    #[test]
    fn policy_serialization_roundtrip_preserves_frontier_mode() {
        let policy = ContextWindowPolicy {
            max_context_tokens: 8192,
            max_output_tokens: 1024,
            min_recent_messages: 6,
            enable_prompt_cache: false,
            autocompact_threshold: Some(4096),
            compaction_mode: ContextCompactionMode::CompactToSafeFrontier,
            compaction_raw_suffix_messages: 5,
        };

        let encoded = serde_json::to_value(&policy).unwrap();
        assert_eq!(encoded["compaction_mode"], "compact_to_safe_frontier");
        assert_eq!(encoded["compaction_raw_suffix_messages"], 5);

        let restored: ContextWindowPolicy = serde_json::from_value(encoded).unwrap();
        assert_eq!(
            restored.compaction_mode,
            ContextCompactionMode::CompactToSafeFrontier
        );
        assert_eq!(restored.compaction_raw_suffix_messages, 5);
    }

    #[test]
    fn inference_override_merge_last_wins() {
        let mut base = InferenceOverride {
            upstream_model: Some("model-a".into()),
            temperature: Some(0.5),
            ..Default::default()
        };
        base.merge(InferenceOverride {
            upstream_model: Some("model-b".into()),
            reasoning_effort: Some(ReasoningEffort::High),
            ..Default::default()
        });
        assert_eq!(base.upstream_model.as_deref(), Some("model-b"));
        assert_eq!(base.temperature, Some(0.5));
        assert_eq!(base.reasoning_effort, Some(ReasoningEffort::High));
    }

    #[test]
    fn inference_override_merge_none_preserves_existing() {
        let mut base = InferenceOverride {
            max_tokens: Some(1024),
            top_p: Some(0.9),
            ..Default::default()
        };
        base.merge(InferenceOverride::default());
        assert_eq!(base.max_tokens, Some(1024));
        assert_eq!(base.top_p, Some(0.9));
    }

    #[test]
    fn inference_override_uses_canonical_names() {
        let canonical = InferenceOverride {
            upstream_model: Some("gpt-4o".into()),
            fallback_upstream_models: Some(vec!["gpt-4o-mini".into()]),
            ..Default::default()
        };

        let encoded = serde_json::to_value(&canonical).unwrap();
        assert_eq!(encoded["upstream_model"], "gpt-4o");
        assert_eq!(encoded["fallback_upstream_models"][0], "gpt-4o-mini");
    }

    #[test]
    fn inference_override_rejects_legacy_model_fields() {
        let legacy = serde_json::from_value::<InferenceOverride>(json!({
            "model": "gpt-4o",
            "fallback_models": ["gpt-4o-mini"]
        }));
        assert!(legacy.is_err());
    }

    #[test]
    fn reasoning_effort_serde_roundtrip() {
        let effort = ReasoningEffort::Budget(4096);
        let json = serde_json::to_string(&effort).unwrap();
        let restored: ReasoningEffort = serde_json::from_str(&json).unwrap();
        assert_eq!(restored, effort);
    }

    #[test]
    fn stream_result_needs_tools() {
        let with_tools = StreamResult {
            content: vec![],
            tool_calls: vec![ToolCall::new("c1", "search", json!({}))],
            usage: None,
            stop_reason: Some(StopReason::ToolUse),
            has_incomplete_tool_calls: false,
        };
        assert!(with_tools.needs_tools());

        let without = StreamResult {
            content: vec![ContentBlock::text("hello")],
            tool_calls: vec![],
            usage: None,
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: false,
        };
        assert!(!without.needs_tools());
    }

    #[test]
    fn llm_response_success_and_error() {
        let success = LLMResponse::success(StreamResult {
            content: vec![ContentBlock::text("hi")],
            tool_calls: vec![],
            usage: Some(TokenUsage {
                prompt_tokens: Some(10),
                completion_tokens: Some(5),
                total_tokens: Some(15),
                ..Default::default()
            }),
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: false,
        });
        assert!(success.outcome.is_ok());

        let error = LLMResponse::error(InferenceError {
            error_type: "rate_limit".into(),
            message: "too many requests".into(),
            error_class: Some("rate_limit".into()),
        });
        assert!(error.outcome.is_err());
    }

    #[test]
    fn token_usage_serde_omits_none_fields() {
        let usage = TokenUsage {
            prompt_tokens: Some(100),
            ..Default::default()
        };
        let json = serde_json::to_string(&usage).unwrap();
        assert!(json.contains("prompt_tokens"));
        assert!(!json.contains("completion_tokens"));
    }

    #[test]
    fn inference_error_serde_roundtrip() {
        let err = InferenceError {
            error_type: "timeout".into(),
            message: "request timed out".into(),
            error_class: Some("connection".into()),
        };
        let json = serde_json::to_string(&err).unwrap();
        let parsed: InferenceError = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, err);
    }

    // ── StreamResult tests (migrated from uncarve) ──

    #[test]
    fn stream_result_text_concatenates() {
        let result = StreamResult {
            content: vec![
                ContentBlock::text("Hello "),
                ContentBlock::image_url("img.png"),
                ContentBlock::text("World"),
            ],
            tool_calls: vec![],
            usage: None,
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: false,
        };
        assert_eq!(result.text(), "Hello World");
    }

    #[test]
    fn stream_result_text_empty_no_text_blocks() {
        let result = StreamResult {
            content: vec![ContentBlock::image_url("img.png")],
            tool_calls: vec![],
            usage: None,
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: false,
        };
        assert_eq!(result.text(), "");
    }

    #[test]
    fn stream_result_needs_truncation_recovery_true() {
        let result = StreamResult {
            content: vec![],
            tool_calls: vec![ToolCall::new("c1", "search", json!({}))],
            usage: None,
            stop_reason: Some(StopReason::MaxTokens),
            has_incomplete_tool_calls: true,
        };
        assert!(result.needs_truncation_recovery());
    }

    #[test]
    fn stream_result_needs_truncation_recovery_for_text_max_tokens() {
        let result = StreamResult {
            content: vec![ContentBlock::text("partial answer")],
            tool_calls: vec![],
            usage: None,
            stop_reason: Some(StopReason::MaxTokens),
            has_incomplete_tool_calls: false,
        };
        assert!(result.needs_truncation_recovery());
    }

    #[test]
    fn stream_result_no_truncation_recovery_end_turn() {
        let result = StreamResult {
            content: vec![],
            tool_calls: vec![ToolCall::new("c1", "search", json!({}))],
            usage: None,
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: true,
        };
        assert!(!result.needs_truncation_recovery());
    }

    #[test]
    fn stream_result_no_truncation_recovery_complete_tools() {
        let result = StreamResult {
            content: vec![],
            tool_calls: vec![ToolCall::new("c1", "search", json!({}))],
            usage: None,
            stop_reason: Some(StopReason::MaxTokens),
            has_incomplete_tool_calls: false,
        };
        assert!(!result.needs_truncation_recovery());
    }

    #[test]
    fn stream_result_no_truncation_recovery_empty_max_tokens() {
        let result = StreamResult {
            content: vec![],
            tool_calls: vec![],
            usage: None,
            stop_reason: Some(StopReason::MaxTokens),
            has_incomplete_tool_calls: false,
        };
        assert!(!result.needs_truncation_recovery());
    }

    // ── StopReason tests (migrated from uncarve) ──

    #[test]
    fn stop_reason_serde_roundtrip_all_variants() {
        for reason in [
            StopReason::EndTurn,
            StopReason::MaxTokens,
            StopReason::ToolUse,
            StopReason::StopSequence,
        ] {
            let json = serde_json::to_string(&reason).unwrap();
            let parsed: StopReason = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, reason);
        }
    }

    #[test]
    fn stop_reason_debug_output() {
        assert_eq!(format!("{:?}", StopReason::EndTurn), "EndTurn");
        assert_eq!(format!("{:?}", StopReason::MaxTokens), "MaxTokens");
        assert_eq!(format!("{:?}", StopReason::ToolUse), "ToolUse");
        assert_eq!(format!("{:?}", StopReason::StopSequence), "StopSequence");
    }

    // ── InferenceOverride.is_empty tests (migrated from uncarve) ──

    #[test]
    fn inference_override_is_empty_default() {
        assert!(InferenceOverride::default().is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_upstream_model() {
        let ovr = InferenceOverride {
            upstream_model: Some("model-a".into()),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_temperature() {
        let ovr = InferenceOverride {
            temperature: Some(0.5),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_max_tokens() {
        let ovr = InferenceOverride {
            max_tokens: Some(1024),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_reasoning_effort() {
        let ovr = InferenceOverride {
            reasoning_effort: Some(ReasoningEffort::High),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_top_p() {
        let ovr = InferenceOverride {
            top_p: Some(0.9),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    #[test]
    fn inference_override_not_empty_with_fallback_upstream_models() {
        let ovr = InferenceOverride {
            fallback_upstream_models: Some(vec!["m1".into()]),
            ..Default::default()
        };
        assert!(!ovr.is_empty());
    }

    // ── TokenUsage tests (migrated from uncarve) ──

    #[test]
    fn token_usage_full_serde_roundtrip() {
        let usage = TokenUsage {
            prompt_tokens: Some(100),
            completion_tokens: Some(50),
            total_tokens: Some(150),
            cache_read_tokens: Some(30),
            cache_creation_tokens: Some(10),
            thinking_tokens: Some(5),
        };
        let json = serde_json::to_string(&usage).unwrap();
        let parsed: TokenUsage = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, usage);
    }

    #[test]
    fn token_usage_default_is_all_none() {
        let usage = TokenUsage::default();
        assert!(usage.prompt_tokens.is_none());
        assert!(usage.completion_tokens.is_none());
        assert!(usage.total_tokens.is_none());
        assert!(usage.cache_read_tokens.is_none());
        assert!(usage.cache_creation_tokens.is_none());
        assert!(usage.thinking_tokens.is_none());
    }

    #[test]
    fn token_usage_default_serializes_to_empty_object() {
        let usage = TokenUsage::default();
        let json = serde_json::to_string(&usage).unwrap();
        assert_eq!(json, "{}");
    }

    // ── ReasoningEffort tests (migrated from uncarve) ──

    #[test]
    fn reasoning_effort_all_variants_serde_roundtrip() {
        for effort in [
            ReasoningEffort::None,
            ReasoningEffort::Low,
            ReasoningEffort::Medium,
            ReasoningEffort::High,
            ReasoningEffort::Max,
            ReasoningEffort::Budget(2048),
        ] {
            let json = serde_json::to_string(&effort).unwrap();
            let parsed: ReasoningEffort = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, effort);
        }
    }

    // ── ContextCompactionMode tests (migrated from uncarve) ──

    #[test]
    fn context_compaction_mode_serde_roundtrip() {
        for mode in [
            ContextCompactionMode::KeepRecentRawSuffix,
            ContextCompactionMode::CompactToSafeFrontier,
        ] {
            let json = serde_json::to_string(&mode).unwrap();
            let parsed: ContextCompactionMode = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, mode);
        }
    }

    #[test]
    fn context_compaction_mode_default_is_keep_recent() {
        assert_eq!(
            ContextCompactionMode::default(),
            ContextCompactionMode::KeepRecentRawSuffix
        );
    }

    #[test]
    fn context_compaction_mode_serialization_values() {
        assert_eq!(
            serde_json::to_string(&ContextCompactionMode::KeepRecentRawSuffix).unwrap(),
            "\"keep_recent_raw_suffix\""
        );
        assert_eq!(
            serde_json::to_string(&ContextCompactionMode::CompactToSafeFrontier).unwrap(),
            "\"compact_to_safe_frontier\""
        );
    }

    // ── InferenceError tests (migrated from uncarve) ──

    #[test]
    fn inference_error_omits_none_error_class() {
        let err = InferenceError {
            error_type: "timeout".into(),
            message: "timed out".into(),
            error_class: None,
        };
        let json = serde_json::to_string(&err).unwrap();
        assert!(!json.contains("error_class"));
    }

    #[test]
    fn inference_error_type_field_serializes_as_type() {
        let err = InferenceError {
            error_type: "rate_limit".into(),
            message: "too many requests".into(),
            error_class: None,
        };
        let json_val: serde_json::Value = serde_json::to_value(&err).unwrap();
        assert_eq!(json_val["type"], "rate_limit");
        assert!(json_val.get("error_type").is_none());
    }

    // ── ContextWindowPolicy tests (migrated from uncarve) ──

    #[test]
    fn context_window_policy_default_values() {
        let policy = ContextWindowPolicy::default();
        assert_eq!(policy.max_context_tokens, 200_000);
        assert_eq!(policy.max_output_tokens, 16_384);
        assert_eq!(policy.min_recent_messages, 10);
        assert!(policy.enable_prompt_cache);
        assert!(policy.autocompact_threshold.is_none());
    }

    #[test]
    fn context_window_policy_full_roundtrip() {
        let policy = ContextWindowPolicy {
            max_context_tokens: 4096,
            max_output_tokens: 512,
            min_recent_messages: 4,
            enable_prompt_cache: false,
            autocompact_threshold: Some(2048),
            compaction_mode: ContextCompactionMode::CompactToSafeFrontier,
            compaction_raw_suffix_messages: 3,
        };
        let json = serde_json::to_value(&policy).unwrap();
        let restored: ContextWindowPolicy = serde_json::from_value(json).unwrap();
        assert_eq!(restored.max_context_tokens, 4096);
        assert_eq!(
            restored.compaction_mode,
            ContextCompactionMode::CompactToSafeFrontier
        );
        assert_eq!(restored.compaction_raw_suffix_messages, 3);
    }

    // ── InferenceOverride merge tests (migrated from uncarve) ──

    #[test]
    fn inference_override_merge_fallback_upstream_models() {
        let mut base = InferenceOverride::default();
        base.merge(InferenceOverride {
            fallback_upstream_models: Some(vec!["model-a".into(), "model-b".into()]),
            ..Default::default()
        });
        assert_eq!(
            base.fallback_upstream_models,
            Some(vec!["model-a".into(), "model-b".into()])
        );
    }

    #[test]
    fn inference_override_merge_top_p() {
        let mut base = InferenceOverride {
            top_p: Some(0.9),
            ..Default::default()
        };
        base.merge(InferenceOverride {
            top_p: Some(0.7),
            ..Default::default()
        });
        assert_eq!(base.top_p, Some(0.7));
    }
}