iron-core 0.1.37

Core AgentIron loop, session state, and tool registry
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
//! Model switching: turn-boundary model changes with context adaptation.
//!
//! This module implements model switching as a first-class operation that
//! preserves session identity while adapting context and reconciling capabilities.

use serde::{Deserialize, Serialize};

/// Plan for adapting a session before changing its active model.
///
/// Planning does not mutate a session or provider. The runtime applies the
/// plan at an idle turn boundary so session identity and durable history remain
/// continuous across the switch.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelSwitchPlan {
    /// Source model identifier.
    pub source_model: String,
    /// Target model identifier.
    pub target_model: String,
    /// Source managed-provider slug, if any.
    pub source_provider: Option<String>,
    /// Target managed-provider slug, if any.
    pub target_provider: Option<String>,
    /// Context adaptation required before the target can be used.
    pub context_adaptation: ContextAdaptationPlan,
    /// Capability differences between source and target.
    pub capability_diff: CapabilityDiff,
    /// Whether planning determined that compaction is required.
    pub compaction_triggered: bool,
    /// Estimated retained token count after adaptation.
    pub estimated_tokens_after: usize,
    /// Target context-window size, if known.
    pub target_window: Option<usize>,
}

/// Planned retention needed to fit context into target constraints.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContextAdaptationPlan {
    /// Whether context needs to be compacted.
    pub needs_compaction: bool,
    /// Number of recent messages to retain.
    pub tail_messages: usize,
    /// Whether the estimated retained tail fits the target window.
    pub tail_fits: bool,
    /// Estimated tokens in retained context.
    pub retained_tokens: usize,
}

/// Capabilities lost or restricted when moving to a target model.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CapabilityDiff {
    /// Tools unavailable in the target model.
    pub hidden_tools: Vec<String>,
    /// Source modalities unsupported by the target, such as `image` or `pdf`.
    pub unsupported_modalities: Vec<String>,
    /// Number of tokens by which the target window is smaller.
    pub window_shrink: Option<usize>,
    /// Source features unsupported by the target.
    pub unsupported_features: Vec<String>,
    /// Whether the target supports tool calling.
    pub tools_supported: bool,
    /// Whether the target supports streaming.
    pub streaming_supported: bool,
}

impl Default for CapabilityDiff {
    fn default() -> Self {
        Self {
            hidden_tools: Vec::new(),
            unsupported_modalities: Vec::new(),
            window_shrink: None,
            unsupported_features: Vec::new(),
            tools_supported: true,
            streaming_supported: true,
        }
    }
}

/// Owned provider selection queued for a model switch.
#[derive(Debug, Clone, PartialEq)]
pub enum ModelSwitchRequest {
    /// Switch to a managed provider resolved through configured credentials.
    Managed {
        /// Managed-provider slug.
        provider_slug: String,
        /// Target model identifier.
        model: String,
        /// Explicit provider API key, when supplied.
        api_key: Option<String>,
    },
    /// Switch to an unmanaged provider instance.
    Unmanaged {
        /// Target model identifier.
        model: String,
        /// Display name of the provider instance.
        provider_name: String,
    },
}

/// Durable history record of an applied model switch.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelSwitchRecord {
    /// Source model.
    pub from_model: String,
    /// Target model.
    pub to_model: String,
    /// Source provider, if recorded.
    pub from_provider: Option<String>,
    /// Target provider, if recorded.
    pub to_provider: Option<String>,
    /// Whether context or capabilities required adaptation.
    pub adapted: bool,
    /// Capability restrictions discovered for the target.
    pub capability_diff: CapabilityDiff,
    /// UTC timestamp at which the switch was recorded.
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Metrics from an automatic compaction performed during a model switch.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompactionInfo {
    /// Estimated tokens before compaction.
    pub tokens_before: u32,
    /// Estimated tokens after compaction.
    pub tokens_after: u32,
    /// Method used (e.g. "auto_compaction").
    pub method: String,
}

/// Model switch request waiting for an idle turn boundary.
#[derive(Debug, Clone)]
pub struct PendingModelSwitch {
    /// Owned provider and model selection to apply.
    pub request: ModelSwitchRequest,
    /// UTC timestamp at which the request was queued.
    pub requested_at: chrono::DateTime<chrono::Utc>,
}

impl ModelSwitchPlan {
    /// Creates a plan with no adaptation and a default 20-message tail.
    pub fn new(source_model: impl Into<String>, target_model: impl Into<String>) -> Self {
        Self {
            source_model: source_model.into(),
            target_model: target_model.into(),
            source_provider: None,
            target_provider: None,
            context_adaptation: ContextAdaptationPlan {
                needs_compaction: false,
                tail_messages: 20,
                tail_fits: true,
                retained_tokens: 0,
            },
            capability_diff: CapabilityDiff::default(),
            compaction_triggered: false,
            estimated_tokens_after: 0,
            target_window: None,
        }
    }

    /// Returns whether compaction or any capability restriction is required.
    pub fn requires_adaptation(&self) -> bool {
        self.context_adaptation.needs_compaction
            || !self.capability_diff.hidden_tools.is_empty()
            || !self.capability_diff.unsupported_modalities.is_empty()
            || !self.capability_diff.unsupported_features.is_empty()
    }
}

impl ContextAdaptationPlan {
    /// Returns whether no compaction is required and the retained tail fits.
    pub fn fits(&self) -> bool {
        self.tail_fits && !self.needs_compaction
    }
}

/// Stateless planner for model-switch context and capability adaptation.
pub struct ModelSwitchPlanner;

impl ModelSwitchPlanner {
    /// Creates a plan for switching from a source model to a target model.
    ///
    /// Estimates current context size and determines if compaction is needed
    /// based on the target model's context window.
    pub fn create_plan(
        source_model: &str,
        target_model: &str,
        target_window: Option<usize>,
        current_tokens: usize,
    ) -> ModelSwitchPlan {
        Self::create_plan_with_capabilities(
            source_model,
            target_model,
            target_window,
            current_tokens,
            None,
            None,
        )
    }

    /// Creates a plan and compares capabilities when both metadata records exist.
    ///
    /// When `target_window` is unknown, the full current estimate is assumed to
    /// fit. When it is exceeded, retained tail size uses a coarse 20-message
    /// average and never plans fewer than five messages before the 20-message cap.
    pub fn create_plan_with_capabilities(
        source_model: &str,
        target_model: &str,
        target_window: Option<usize>,
        current_tokens: usize,
        source_capabilities: Option<&ModelCapabilityMetadata>,
        target_capabilities: Option<&ModelCapabilityMetadata>,
    ) -> ModelSwitchPlan {
        let mut plan = ModelSwitchPlan::new(source_model, target_model);
        plan.target_window = target_window;

        // Compute capability diff if both metadata are available
        if let (Some(source), Some(target)) = (source_capabilities, target_capabilities) {
            plan.capability_diff = compare_capabilities(source, target);
        }

        if let Some(window) = target_window {
            let _estimated_tail_tokens = Self::estimate_tail_tokens(current_tokens);

            if current_tokens > window {
                // Context exceeds target window - need compaction
                plan.context_adaptation.needs_compaction = true;
                plan.compaction_triggered = true;

                // Estimate how many messages we can retain
                let avg_tokens_per_message = if current_tokens > 0 {
                    current_tokens / 20 // rough estimate based on default tail
                } else {
                    100
                };

                let max_tail_messages = (window / avg_tokens_per_message.max(1)).max(5);
                plan.context_adaptation.tail_messages = max_tail_messages.min(20);
                plan.context_adaptation.retained_tokens =
                    plan.context_adaptation.tail_messages * avg_tokens_per_message;
                plan.context_adaptation.tail_fits =
                    plan.context_adaptation.retained_tokens <= window;
                plan.estimated_tokens_after = plan.context_adaptation.retained_tokens;
            } else {
                // Context fits within target window
                plan.context_adaptation.needs_compaction = false;
                plan.context_adaptation.tail_messages = 20;
                plan.context_adaptation.tail_fits = true;
                plan.context_adaptation.retained_tokens = current_tokens;
                plan.estimated_tokens_after = current_tokens;
            }
        } else {
            // Unknown target window - assume it fits
            plan.context_adaptation.needs_compaction = false;
            plan.context_adaptation.tail_fits = true;
            plan.context_adaptation.retained_tokens = current_tokens;
            plan.estimated_tokens_after = current_tokens;
        }

        plan
    }

    /// Estimate the token count of the tail (recent messages)
    ///
    /// Uses a simple heuristic: recent messages typically contain
    /// most of the tokens in an active conversation.
    fn estimate_tail_tokens(total_tokens: usize) -> usize {
        // Tail is typically ~60% of total context
        (total_tokens as f64 * 0.6) as usize
    }

    /// Estimates total context from uncompacted tokens and block summary length.
    ///
    /// Compressed summaries use a four-characters-per-token heuristic.
    pub fn estimate_session_tokens(
        uncompacted_tokens: usize,
        compressed_blocks: &[crate::context::models::CompressedBlock],
    ) -> usize {
        let compressed_estimate: usize = compressed_blocks
            .iter()
            .map(|block| block.summary.len() / 4) // rough token estimate
            .sum();

        uncompacted_tokens + compressed_estimate
    }
}

/// Capability metadata used to compare two model/provider pairs.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelCapabilityMetadata {
    /// Model identifier.
    pub model: String,
    /// Provider identifier.
    pub provider: String,
    /// Maximum context-window size in tokens.
    pub context_window: usize,
    /// Whether tool calling is supported.
    pub supports_tools: bool,
    /// Whether streamed responses are supported.
    pub supports_streaming: bool,
    /// Whether reasoning-effort selection is supported.
    #[serde(default)]
    pub supports_reasoning_effort: bool,
    /// Accepted reasoning-effort values.
    #[serde(default)]
    pub reasoning_effort_values: Vec<String>,
    /// Input modalities supported by the model.
    pub supported_modalities: Vec<String>,
    /// Tool names explicitly unavailable for this model.
    pub unsupported_tools: Vec<String>,
}

/// Compares source capabilities with restrictions on a target model.
///
/// The result records capabilities lost during the switch; it is not a full
/// independent description of either model.
pub fn compare_capabilities(
    source: &ModelCapabilityMetadata,
    target: &ModelCapabilityMetadata,
) -> CapabilityDiff {
    let mut diff = CapabilityDiff::default();

    // Check context window shrink
    if target.context_window < source.context_window {
        diff.window_shrink = Some(source.context_window - target.context_window);
    }

    // Check tool support
    if !target.supports_tools {
        diff.tools_supported = false;
    }

    // Check streaming support
    if !target.supports_streaming {
        diff.streaming_supported = false;
    }

    // Check reasoning effort support
    if source.supports_reasoning_effort && !target.supports_reasoning_effort {
        diff.unsupported_features.push("reasoning_effort".into());
    }

    // Check modalities
    for modality in &source.supported_modalities {
        if !target.supported_modalities.contains(modality) {
            diff.unsupported_modalities.push(modality.clone());
        }
    }

    // Check tools
    for tool in &target.unsupported_tools {
        diff.hidden_tools.push(tool.clone());
    }

    diff
}

/// In-memory registry keyed by provider and model identifier.
#[derive(Debug, Clone, Default)]
pub struct ModelCapabilityRegistry {
    models: std::collections::HashMap<(String, String), ModelCapabilityMetadata>,
}

impl ModelCapabilityRegistry {
    /// Creates an empty capability registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Inserts or replaces metadata for its provider/model pair.
    pub fn register(&mut self, metadata: ModelCapabilityMetadata) {
        let key = (metadata.provider.clone(), metadata.model.clone());
        self.models.insert(key, metadata);
    }

    /// Returns metadata for an exact provider/model pair.
    pub fn get(&self, provider: &str, model: &str) -> Option<&ModelCapabilityMetadata> {
        self.models.get(&(provider.to_string(), model.to_string()))
    }

    /// Compares two registered provider/model pairs.
    ///
    /// Returns `None` if either pair is absent.
    pub fn compare(
        &self,
        source_provider: &str,
        source_model: &str,
        target_provider: &str,
        target_model: &str,
    ) -> Option<CapabilityDiff> {
        let source = self.get(source_provider, source_model)?;
        let target = self.get(target_provider, target_model)?;
        Some(compare_capabilities(source, target))
    }
}

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

    #[test]
    fn test_model_switch_plan_requires_adaptation() {
        let plan = ModelSwitchPlan {
            source_model: "model-a".into(),
            target_model: "model-b".into(),
            source_provider: None,
            target_provider: None,
            context_adaptation: ContextAdaptationPlan {
                needs_compaction: true,
                tail_messages: 10,
                tail_fits: true,
                retained_tokens: 5000,
            },
            capability_diff: CapabilityDiff::default(),
            compaction_triggered: true,
            estimated_tokens_after: 5000,
            target_window: Some(10000),
        };

        assert!(plan.requires_adaptation());
    }

    #[test]
    fn test_model_switch_plan_no_adaptation_needed() {
        let plan = ModelSwitchPlan::new("model-a", "model-b");
        assert!(!plan.requires_adaptation());
        assert!(plan.context_adaptation.fits());
    }

    #[test]
    fn test_model_switch_planner_context_fits() {
        let plan = ModelSwitchPlanner::create_plan("model-a", "model-b", Some(100000), 50000);
        assert!(!plan.context_adaptation.needs_compaction);
        assert!(plan.context_adaptation.fits());
        assert_eq!(plan.context_adaptation.tail_messages, 20);
    }

    #[test]
    fn test_model_switch_planner_context_exceeds_window() {
        let plan = ModelSwitchPlanner::create_plan("model-a", "model-b", Some(10000), 50000);
        assert!(plan.context_adaptation.needs_compaction);
        assert!(plan.compaction_triggered);
        assert!(!plan.context_adaptation.fits());
        assert!(plan.context_adaptation.tail_messages < 20);
    }

    #[test]
    fn test_model_switch_planner_unknown_window() {
        let plan = ModelSwitchPlanner::create_plan("model-a", "model-b", None, 50000);
        assert!(!plan.context_adaptation.needs_compaction);
        assert!(plan.context_adaptation.fits());
    }

    #[test]
    fn test_estimate_session_tokens() {
        let blocks = vec![crate::context::models::CompressedBlock::new(
            "c0001",
            "Test topic",
            "m0001-m0010",
            "a".repeat(400),
        )];
        let tokens = ModelSwitchPlanner::estimate_session_tokens(1000, &blocks);
        assert_eq!(tokens, 1100); // 1000 + 400/4
    }

    #[test]
    fn test_capability_diff_default() {
        let diff = CapabilityDiff::default();
        assert!(diff.hidden_tools.is_empty());
        assert!(diff.unsupported_modalities.is_empty());
        assert!(diff.unsupported_features.is_empty());
        assert!(diff.tools_supported);
        assert!(diff.streaming_supported);
        assert_eq!(diff.window_shrink, None);
    }

    #[test]
    fn test_compare_capabilities_window_shrink() {
        let source = ModelCapabilityMetadata {
            model: "model-a".into(),
            provider: "provider-a".into(),
            context_window: 100000,
            supports_tools: true,
            supports_streaming: true,
            supports_reasoning_effort: true,
            reasoning_effort_values: vec!["low".into(), "medium".into(), "high".into()],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec![],
        };

        let target = ModelCapabilityMetadata {
            model: "model-b".into(),
            provider: "provider-b".into(),
            context_window: 50000,
            supports_tools: true,
            supports_streaming: true,
            supports_reasoning_effort: true,
            reasoning_effort_values: vec!["low".into(), "medium".into(), "high".into()],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec![],
        };

        let diff = compare_capabilities(&source, &target);
        assert_eq!(diff.window_shrink, Some(50000));
        assert!(diff.tools_supported);
    }

    #[test]
    fn test_compare_capabilities_tool_loss() {
        let source = ModelCapabilityMetadata {
            model: "model-a".into(),
            provider: "provider-a".into(),
            context_window: 100000,
            supports_tools: true,
            supports_streaming: true,
            supports_reasoning_effort: true,
            reasoning_effort_values: vec!["low".into(), "medium".into(), "high".into()],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec![],
        };

        let target = ModelCapabilityMetadata {
            model: "model-b".into(),
            provider: "provider-b".into(),
            context_window: 100000,
            supports_tools: false,
            supports_streaming: true,
            supports_reasoning_effort: false,
            reasoning_effort_values: vec![],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec!["tool1".into()],
        };

        let diff = compare_capabilities(&source, &target);
        assert!(!diff.tools_supported);
        assert_eq!(diff.hidden_tools, vec!["tool1"]);
        assert_eq!(diff.unsupported_features, vec!["reasoning_effort"]);
    }

    #[test]
    fn test_model_capability_metadata_exposes_reasoning_effort() {
        let meta = ModelCapabilityMetadata {
            model: "o3".into(),
            provider: "openai".into(),
            context_window: 200000,
            supports_tools: true,
            supports_streaming: true,
            supports_reasoning_effort: true,
            reasoning_effort_values: vec!["low".into(), "medium".into(), "high".into()],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec![],
        };

        assert!(meta.supports_reasoning_effort);
        assert_eq!(meta.reasoning_effort_values, vec!["low", "medium", "high"]);
    }

    #[test]
    fn test_model_capability_registry() {
        let mut registry = ModelCapabilityRegistry::new();
        let meta = ModelCapabilityMetadata {
            model: "gpt-4".into(),
            provider: "openai".into(),
            context_window: 8192,
            supports_tools: true,
            supports_streaming: true,
            supports_reasoning_effort: false,
            reasoning_effort_values: vec![],
            supported_modalities: vec!["text".into()],
            unsupported_tools: vec![],
        };
        registry.register(meta);

        assert!(registry.get("openai", "gpt-4").is_some());
        assert!(registry.get("openai", "gpt-3").is_none());

        let diff = registry.compare("openai", "gpt-4", "openai", "gpt-4");
        assert!(diff.is_some());
        let diff = diff.unwrap();
        assert_eq!(diff.window_shrink, None);
        assert!(diff.tools_supported);
    }
}