symbi-runtime 1.10.0

Agent Runtime System for the Symbi platform
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
//! Context compaction pipeline for reclaiming tokens.
//!
//! Runs synchronously before each LLM call. Walks tiers from most aggressive
//! (truncate) to least aggressive (summarize), applying the first tier that
//! matches the current token usage level.

use serde::{Deserialize, Serialize};

use super::types::AccessLevel;

/// Per-agent compaction configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionConfig {
    /// Whether compaction is enabled.
    pub enabled: bool,
    /// Token usage ratio (0.0–1.0) at which Tier 1 (Summarize) triggers.
    pub summarize_threshold: f32,
    /// Token usage ratio at which Tier 2 (Compress Episodic) triggers.
    pub compress_threshold: f32,
    /// Token usage ratio at which Tier 3 (Archive to Memory) triggers.
    pub archive_threshold: f32,
    /// Token usage ratio at which Tier 4 (Truncate) triggers.
    pub truncate_threshold: f32,
    /// Model to use for summarization. `None` = agent's own model.
    pub compaction_model: Option<String>,
    /// Maximum tokens for generated summaries.
    pub max_summary_tokens: usize,
    /// Access levels whose items are never compacted.
    pub preserve_access_levels: Vec<AccessLevel>,
    /// Minimum conversation items before compaction is considered.
    pub min_items_to_compact: usize,
}

impl Default for CompactionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            summarize_threshold: 0.70,
            compress_threshold: 0.80,
            archive_threshold: 0.85,
            truncate_threshold: 0.90,
            compaction_model: None,
            max_summary_tokens: 500,
            preserve_access_levels: vec![AccessLevel::Secret, AccessLevel::Confidential],
            min_items_to_compact: 5,
        }
    }
}

/// Which compaction tier was applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompactionTier {
    /// Tier 1: LLM summarizes oldest conversation items (OSS).
    Summarize,
    /// Tier 2: Merge similar episodic memory items (enterprise).
    CompressEpisodic,
    /// Tier 3: Archive summaries and old items to MarkdownMemoryStore (enterprise).
    ArchiveToMemory,
    /// Tier 4: Drop oldest conversation items (OSS).
    Truncate,
}

impl std::fmt::Display for CompactionTier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompactionTier::Summarize => write!(f, "Summarize"),
            CompactionTier::CompressEpisodic => write!(f, "CompressEpisodic"),
            CompactionTier::ArchiveToMemory => write!(f, "ArchiveToMemory"),
            CompactionTier::Truncate => write!(f, "Truncate"),
        }
    }
}

/// Result of a compaction operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionResult {
    pub tier_applied: CompactionTier,
    pub tokens_before: usize,
    pub tokens_after: usize,
    pub tokens_saved: usize,
    pub items_affected: usize,
    pub duration_ms: u64,
    pub summary_generated: Option<String>,
}

use super::types::{ConversationItem, ConversationRole};

/// Tier 4: Truncate — drop oldest conversation items until count drops to
/// `target_ratio` of original. System messages are preserved.
pub fn truncate_items(
    items: &[ConversationItem],
    _config: &CompactionConfig,
    target_ratio: f32,
) -> (Vec<ConversationItem>, usize) {
    let total = items.len();
    let target_count = (total as f32 * target_ratio).ceil() as usize;

    // Partition: system messages vs candidates for removal (oldest first)
    let mut system_items: Vec<&ConversationItem> = Vec::new();
    let mut candidates: Vec<&ConversationItem> = Vec::new();

    for item in items {
        if matches!(item.role, ConversationRole::System) {
            system_items.push(item);
        } else {
            candidates.push(item);
        }
    }

    // Keep the newest candidates, drop the oldest
    let keep_count = target_count.saturating_sub(system_items.len());
    let drop_count = candidates.len().saturating_sub(keep_count);

    let mut result: Vec<ConversationItem> = system_items.into_iter().cloned().collect();
    result.extend(candidates.into_iter().skip(drop_count).cloned());

    (result, drop_count)
}

use std::future::Future;
use std::time::Instant;

/// Tier 1: Summarize — LLM condenses the oldest N conversation items into a
/// single system message. Preserves system messages and items with protected
/// access levels.
///
/// The `summarizer` closure takes the concatenated text and returns a summary.
pub async fn summarize_items<F, Fut>(
    items: &[ConversationItem],
    config: &CompactionConfig,
    items_to_summarize: usize,
    summarizer: F,
) -> Result<Option<(Vec<ConversationItem>, CompactionResult)>, String>
where
    F: FnOnce(String) -> Fut,
    Fut: Future<Output = Result<String, String>>,
{
    if items.len() < config.min_items_to_compact {
        return Ok(None);
    }

    let start = Instant::now();

    // Collect items eligible for summarization (not system, not protected)
    let mut to_summarize: Vec<(usize, &ConversationItem)> = Vec::new();
    let mut to_keep: Vec<(usize, &ConversationItem)> = Vec::new();

    for (idx, item) in items.iter().enumerate() {
        if matches!(item.role, ConversationRole::System) {
            to_keep.push((idx, item));
        } else if to_summarize.len() < items_to_summarize {
            to_summarize.push((idx, item));
        } else {
            to_keep.push((idx, item));
        }
    }

    if to_summarize.is_empty() {
        return Ok(None);
    }

    // Build the text to summarize
    let text_to_summarize: String = to_summarize
        .iter()
        .map(|(_, item)| format!("{:?}: {}", item.role, item.content))
        .collect::<Vec<_>>()
        .join("\n\n");

    let summary = summarizer(text_to_summarize).await?;

    // Build the replacement item
    let summary_item = ConversationItem {
        id: super::types::ContextId::new(),
        role: ConversationRole::System,
        content: format!("[Compacted summary] {summary}"),
        timestamp: std::time::SystemTime::now(),
        context_used: vec![],
        knowledge_used: vec![],
    };

    // Reconstruct: system items + summary + remaining items (in original order)
    let mut result_items: Vec<ConversationItem> = Vec::new();

    // Add system items that came before the summarized range
    for (_, item) in to_keep.iter().filter(|(idx, _)| *idx < to_summarize[0].0) {
        result_items.push((*item).clone());
    }

    // Insert summary
    result_items.push(summary_item);

    // Add remaining items after the summarized range
    for (_, item) in to_keep
        .iter()
        .filter(|(idx, _)| *idx > to_summarize.last().unwrap().0)
    {
        result_items.push((*item).clone());
    }

    let duration = start.elapsed();

    let compaction_result = CompactionResult {
        tier_applied: CompactionTier::Summarize,
        tokens_before: 0, // Caller fills in actual token counts
        tokens_after: 0,
        tokens_saved: 0,
        items_affected: to_summarize.len(),
        duration_ms: duration.as_millis() as u64,
        summary_generated: Some(summary),
    };

    Ok(Some((result_items, compaction_result)))
}

/// Tier 2: Compress Episodic — merge similar memory items (enterprise only).
#[cfg(feature = "enterprise-compaction")]
pub fn tier_compress_episodic() -> Option<CompactionResult> {
    todo!("enterprise: compress episodic memory items by cosine similarity")
}

#[cfg(not(feature = "enterprise-compaction"))]
pub fn tier_compress_episodic() -> Option<CompactionResult> {
    None
}

/// Tier 3: Archive to Memory — flush old items to MarkdownMemoryStore (enterprise only).
#[cfg(feature = "enterprise-compaction")]
pub fn tier_archive_to_memory() -> Option<CompactionResult> {
    todo!("enterprise: archive items to MarkdownMemoryStore daily log")
}

#[cfg(not(feature = "enterprise-compaction"))]
pub fn tier_archive_to_memory() -> Option<CompactionResult> {
    None
}

/// Select the compaction tier based on current token usage ratio.
///
/// Walks from most aggressive (Truncate at 90%) down to least (Summarize at 70%).
/// Enterprise tiers (CompressEpisodic, ArchiveToMemory) are only available when
/// the `enterprise-compaction` feature is enabled.
pub fn select_tier(usage_ratio: f32, config: &CompactionConfig) -> Option<CompactionTier> {
    if !config.enabled {
        return None;
    }

    if usage_ratio >= config.truncate_threshold {
        return Some(CompactionTier::Truncate);
    }

    #[cfg(feature = "enterprise-compaction")]
    if usage_ratio >= config.archive_threshold {
        return Some(CompactionTier::ArchiveToMemory);
    }

    #[cfg(feature = "enterprise-compaction")]
    if usage_ratio >= config.compress_threshold {
        return Some(CompactionTier::CompressEpisodic);
    }

    if usage_ratio >= config.summarize_threshold {
        return Some(CompactionTier::Summarize);
    }

    None
}

/// Enterprise audit entry for compaction events.
#[cfg(feature = "enterprise-compaction")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionAuditEntry {
    pub agent_id: crate::types::AgentId,
    pub session_id: super::types::SessionId,
    pub timestamp: std::time::SystemTime,
    pub tier: CompactionTier,
    pub result: CompactionResult,
    pub items_before: Vec<super::types::ContextId>,
    pub items_after: Vec<super::types::ContextId>,
}

#[cfg(test)]
mod tests {
    use super::super::token_counter::TokenCounter;
    use super::super::types::{ContextId, ConversationItem, ConversationRole};
    use super::*;
    use std::time::SystemTime;

    fn make_conversation_items(count: usize) -> Vec<ConversationItem> {
        (0..count)
            .map(|i| ConversationItem {
                id: ContextId::new(),
                role: if i == 0 {
                    ConversationRole::System
                } else {
                    ConversationRole::User
                },
                content: format!("Message number {i} with some content to take up tokens"),
                timestamp: SystemTime::now(),
                context_used: vec![],
                knowledge_used: vec![],
            })
            .collect()
    }

    #[test]
    fn default_config_has_correct_thresholds() {
        let config = CompactionConfig::default();
        assert!(config.enabled);
        assert!((config.summarize_threshold - 0.70).abs() < f32::EPSILON);
        assert!((config.compress_threshold - 0.80).abs() < f32::EPSILON);
        assert!((config.archive_threshold - 0.85).abs() < f32::EPSILON);
        assert!((config.truncate_threshold - 0.90).abs() < f32::EPSILON);
        assert_eq!(config.max_summary_tokens, 500);
        assert_eq!(config.min_items_to_compact, 5);
        assert_eq!(config.preserve_access_levels.len(), 2);
    }

    #[test]
    fn compaction_tier_display() {
        assert_eq!(CompactionTier::Summarize.to_string(), "Summarize");
        assert_eq!(CompactionTier::Truncate.to_string(), "Truncate");
    }

    #[test]
    fn compaction_result_serialization() {
        let result = CompactionResult {
            tier_applied: CompactionTier::Truncate,
            tokens_before: 10_000,
            tokens_after: 5_000,
            tokens_saved: 5_000,
            items_affected: 12,
            duration_ms: 3,
            summary_generated: None,
        };
        let json = serde_json::to_string(&result).unwrap();
        let deserialized: CompactionResult = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.tokens_saved, 5_000);
    }

    #[tokio::test]
    async fn summarize_replaces_items_with_summary() {
        let items = make_conversation_items(10);
        let config = CompactionConfig {
            min_items_to_compact: 3,
            ..CompactionConfig::default()
        };

        // Mock summarizer that returns a fixed string
        let summarizer = |_text: String| {
            Box::pin(async {
                Ok::<String, String>("This is a summary of the conversation.".to_string())
            })
        };

        let result = summarize_items(&items, &config, 5, summarizer)
            .await
            .unwrap();

        assert!(result.is_some(), "should produce a result");
        let (new_items, compaction) = result.unwrap();
        assert!(new_items.len() < items.len(), "should have fewer items");
        assert!(compaction.summary_generated.is_some());
        let summary_item = new_items
            .iter()
            .find(|i| i.content.contains("[Compacted summary]"));
        assert!(
            summary_item.is_some(),
            "should contain compacted summary item"
        );
    }

    #[test]
    fn truncate_drops_oldest_non_system_items() {
        let items = make_conversation_items(20);
        let config = CompactionConfig::default();

        let (remaining, affected) = truncate_items(&items, &config, 0.70);

        assert!(affected > 0, "should have dropped items");
        assert!(
            remaining
                .iter()
                .any(|i| matches!(i.role, ConversationRole::System)),
            "system messages should be preserved"
        );
        assert!(remaining.len() < items.len());
    }

    #[test]
    fn select_tier_at_70_percent() {
        let config = CompactionConfig::default();
        assert_eq!(select_tier(0.72, &config), Some(CompactionTier::Summarize));
    }

    #[test]
    fn select_tier_at_90_percent() {
        let config = CompactionConfig::default();
        assert_eq!(select_tier(0.92, &config), Some(CompactionTier::Truncate));
    }

    #[test]
    fn select_tier_below_threshold() {
        let config = CompactionConfig::default();
        assert_eq!(select_tier(0.50, &config), None);
    }

    #[test]
    fn select_tier_at_85_percent_oss_falls_to_summarize() {
        // Without enterprise-compaction, tiers 2 and 3 are unavailable,
        // so 85% should fall through to Summarize
        let config = CompactionConfig::default();
        let tier = select_tier(0.86, &config);
        assert!(tier.is_some());
    }

    #[test]
    fn enterprise_tiers_return_none_without_feature() {
        let result = tier_compress_episodic();
        assert!(result.is_none());

        let result = tier_archive_to_memory();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn full_pipeline_truncates_when_over_90_percent() {
        use super::super::token_counter::HeuristicTokenCounter;

        let items = make_conversation_items(100);

        // Use a tiny context limit so we're way over 90%
        let counter = HeuristicTokenCounter::new(500);
        let current_tokens = counter.count_messages(&items);
        let limit = counter.model_context_limit();
        let ratio = current_tokens as f32 / limit as f32;

        assert!(ratio > 0.90, "ratio {ratio} should be > 0.90 for this test");

        let config = CompactionConfig::default();
        let tier = select_tier(ratio, &config);
        assert_eq!(tier, Some(CompactionTier::Truncate));

        // Run truncation
        let (new_items, affected) = truncate_items(&items, &config, config.summarize_threshold);
        assert!(affected > 0);
        assert!(new_items.len() < items.len());

        // Verify token count decreased
        let new_tokens = counter.count_messages(&new_items);
        assert!(
            new_tokens < current_tokens,
            "tokens should decrease: {new_tokens} < {current_tokens}"
        );
    }

    #[tokio::test]
    async fn full_pipeline_summarizes_when_between_70_and_90() {
        let items = make_conversation_items(20);
        let config = CompactionConfig {
            min_items_to_compact: 3,
            ..CompactionConfig::default()
        };

        // Mock: simulate being at 75% usage
        let tier = select_tier(0.75, &config);
        assert_eq!(tier, Some(CompactionTier::Summarize));

        // Run summarization with mock
        let summarizer = |_text: String| {
            Box::pin(async { Ok::<String, String>("Summary of old messages.".to_string()) })
        };

        let result = summarize_items(&items, &config, 10, summarizer)
            .await
            .unwrap();
        assert!(result.is_some());

        let (new_items, compaction) = result.unwrap();
        assert_eq!(compaction.tier_applied, CompactionTier::Summarize);
        assert_eq!(compaction.items_affected, 10);
        assert!(new_items.len() < items.len());
    }
}