llm-stack 0.7.0

Core traits, types, and tools for the llm-stack SDK
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
//! Result caching for tool output.
//!
//! Large tool results are stored out-of-context and replaced with a compact
//! summary + preview. Agents retrieve slices on demand via a `result_cache`
//! tool, keeping the context window small while preserving full data access.
//!
//! # Architecture
//!
//! ```text
//! ToolResultProcessor → ResultCache::store() → CacheBackend (Text, …)
//!//!                     result_cache tool → CacheBackend::execute()
//! ```
//!
//! The [`CacheBackend`] trait is the extension point. Each backend knows how
//! to store one kind of data and expose operations on it. The [`ResultCache`]
//! manages entries, expiration, and disk budget.

use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

mod text;

pub use text::TextBackend;

// ── Backend trait ────────────────────────────────────────────────────

/// The kind of backend storing a cached result.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendKind {
    /// Plain-text file with line-oriented operations.
    Text,
}

impl fmt::Display for BackendKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Text => write!(f, "text"),
        }
    }
}

/// Operations that can be performed on a cached result.
#[derive(Debug, Clone)]
pub enum CacheOp {
    /// Read a range of lines (1-indexed, inclusive).
    Read {
        /// First line to read (1-indexed).
        start: usize,
        /// Last line to read (1-indexed, inclusive).
        end: usize,
    },
    /// Search for a regex pattern, returning matches with context.
    Grep {
        /// Regex pattern to search for.
        pattern: String,
        /// Number of context lines around each match.
        context_lines: usize,
    },
    /// Return the first N lines.
    Head {
        /// Number of lines to return.
        lines: usize,
    },
    /// Return the last N lines.
    Tail {
        /// Number of lines to return.
        lines: usize,
    },
    /// Return statistics about the cached data.
    Stats,
}

/// Statistics about a cached result.
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Total number of lines (for text) or rows (for tabular data).
    pub line_count: usize,
    /// Size on disk in bytes.
    pub disk_bytes: u64,
    /// Human-readable summary (e.g. "1,234 lines, 56 KB").
    pub summary: String,
}

/// Errors from cache operations.
#[derive(Debug, thiserror::Error)]
pub enum CacheError {
    /// An I/O error occurred reading or writing cache data.
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// A grep pattern failed to compile as a regex.
    #[error("invalid regex pattern: {0}")]
    InvalidPattern(String),

    /// The requested cache entry does not exist.
    #[error("cache entry not found: {ref_id}")]
    NotFound {
        /// The reference ID that was not found.
        ref_id: String,
    },

    /// The requested cache entry has expired.
    #[error("cache entry expired: {ref_id}")]
    Expired {
        /// The reference ID that expired.
        ref_id: String,
    },

    /// The requested line range is outside the cached data.
    #[error("line range out of bounds: requested {start}..{end}, have {total} lines")]
    OutOfBounds {
        /// Requested start line.
        start: usize,
        /// Requested end line.
        end: usize,
        /// Actual line count.
        total: usize,
    },
}

/// A backend that stores and operates on one cached result.
///
/// Implementations are created by [`ResultCache::store`] and live for the
/// lifetime of the cache entry. Each backend owns its backing storage
/// (file, buffer, etc.).
///
/// # Extending
///
/// To add a new backend (e.g. Parquet/Arrow for tabular data):
///
/// 1. Implement `CacheBackend` with the new storage format
/// 2. Add a variant to [`BackendKind`]
/// 3. Update [`ResultCache::store`] to select the new backend
pub trait CacheBackend: Send + Sync {
    /// What kind of backend this is.
    fn kind(&self) -> BackendKind;

    /// Execute an operation on the cached data.
    fn execute(&self, op: CacheOp) -> Result<String, CacheError>;

    /// Statistics about the cached data.
    fn stats(&self) -> Result<CacheStats, CacheError>;

    /// A short preview of the data (first N lines/rows).
    fn preview(&self, max_lines: usize) -> Result<String, CacheError>;

    /// Size on disk in bytes.
    fn disk_bytes(&self) -> Result<u64, CacheError>;
}

// ── Cache entry ──────────────────────────────────────────────────────

/// A single cached tool result.
pub struct CacheEntry {
    /// Unique reference ID (e.g. `ref_0001`).
    pub ref_id: String,
    /// The backend that stores and operates on this result.
    pub backend: Box<dyn CacheBackend>,
    /// Which tool produced this result.
    pub tool_name: String,
    /// When the entry was created.
    pub created_at: Instant,
    /// When the entry expires and can be evicted.
    pub expires_at: Instant,
}

impl fmt::Debug for CacheEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CacheEntry")
            .field("ref_id", &self.ref_id)
            .field("tool_name", &self.tool_name)
            .field("kind", &self.backend.kind())
            .finish_non_exhaustive()
    }
}

// ── Configuration ────────────────────────────────────────────────────

/// Configuration for the result cache.
#[derive(Debug, Clone)]
pub struct ResultCacheConfig {
    /// How long entries survive before expiration.
    pub ttl: Duration,
    /// Maximum total disk usage across all entries.
    pub max_disk_bytes: u64,
    /// Number of preview lines to include in the context summary.
    pub preview_lines: usize,
}

impl Default for ResultCacheConfig {
    fn default() -> Self {
        Self {
            ttl: Duration::from_secs(30 * 60), // 30 minutes
            max_disk_bytes: 100 * 1024 * 1024, // 100 MB
            preview_lines: 20,
        }
    }
}

// ── ResultCache ──────────────────────────────────────────────────────

/// Manages cached tool results with expiration and disk budgets.
///
/// The cache stores large tool outputs on disk and provides agents with
/// random-access operations (read, grep, head, tail, stats) via
/// [`CacheBackend`] implementations.
pub struct ResultCache {
    entries: HashMap<String, CacheEntry>,
    config: ResultCacheConfig,
    base_dir: PathBuf,
    next_id: u64,
}

impl fmt::Debug for ResultCache {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ResultCache")
            .field("entries", &self.entries.len())
            .field("base_dir", &self.base_dir)
            .finish_non_exhaustive()
    }
}

impl ResultCache {
    /// Create a new cache rooted at `base_dir`.
    ///
    /// The directory is created if it doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns `CacheError::Io` if the directory can't be created.
    pub fn new(
        base_dir: impl Into<PathBuf>,
        config: ResultCacheConfig,
    ) -> Result<Self, CacheError> {
        let base_dir = base_dir.into();
        std::fs::create_dir_all(&base_dir)?;
        Ok(Self {
            entries: HashMap::new(),
            config,
            base_dir,
            next_id: 0,
        })
    }

    /// Store a tool result, returning the reference ID.
    ///
    /// The `backend_kind` determines which backend stores the data.
    /// Currently only [`BackendKind::Text`] is supported.
    ///
    /// # Errors
    ///
    /// Returns `CacheError::Io` if writing to disk fails.
    pub fn store(
        &mut self,
        tool_name: &str,
        content: &str,
        backend_kind: BackendKind,
    ) -> Result<String, CacheError> {
        let ref_id = self.generate_ref_id();
        let now = Instant::now();

        let backend: Box<dyn CacheBackend> = match backend_kind {
            BackendKind::Text => {
                let path = self.base_dir.join(format!("{ref_id}.txt"));
                Box::new(TextBackend::store(content, &path)?)
            }
        };

        let entry = CacheEntry {
            ref_id: ref_id.clone(),
            backend,
            tool_name: tool_name.to_string(),
            created_at: now,
            expires_at: now + self.config.ttl,
        };

        self.entries.insert(ref_id.clone(), entry);
        Ok(ref_id)
    }

    /// Get a cache entry by reference ID.
    pub fn get(&self, ref_id: &str) -> Result<&CacheEntry, CacheError> {
        let entry = self
            .entries
            .get(ref_id)
            .ok_or_else(|| CacheError::NotFound {
                ref_id: ref_id.to_string(),
            })?;

        if Instant::now() >= entry.expires_at {
            return Err(CacheError::Expired {
                ref_id: ref_id.to_string(),
            });
        }

        Ok(entry)
    }

    /// Execute an operation on a cached entry.
    pub fn execute_op(&self, ref_id: &str, op: CacheOp) -> Result<String, CacheError> {
        let entry = self.get(ref_id)?;
        entry.backend.execute(op)
    }

    /// Remove all expired entries, returning the count removed.
    pub fn evict_expired(&mut self) -> usize {
        let now = Instant::now();
        let expired: Vec<String> = self
            .entries
            .iter()
            .filter(|(_, e)| now >= e.expires_at)
            .map(|(k, _)| k.clone())
            .collect();

        let count = expired.len();
        for ref_id in &expired {
            if let Some(entry) = self.entries.remove(ref_id) {
                // Best-effort cleanup of backing files
                self.cleanup_entry(&entry);
            }
        }
        count
    }

    /// Total disk bytes across all entries.
    pub fn total_bytes(&self) -> u64 {
        self.entries
            .values()
            .filter_map(|e| e.backend.disk_bytes().ok())
            .sum()
    }

    /// Number of active entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Iterates over all cache entries.
    ///
    /// Returns `(ref_id, entry)` pairs in arbitrary order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &CacheEntry)> {
        self.entries.iter().map(|(k, v)| (k.as_str(), v))
    }

    /// The configured number of preview lines.
    pub fn preview_lines(&self) -> usize {
        self.config.preview_lines
    }

    /// The base directory for cache files.
    pub fn base_dir(&self) -> &Path {
        &self.base_dir
    }

    fn generate_ref_id(&mut self) -> String {
        self.next_id += 1;
        format!("ref_{:04x}", self.next_id)
    }

    fn cleanup_entry(&self, entry: &CacheEntry) {
        match entry.backend.kind() {
            BackendKind::Text => {
                let path = self.base_dir.join(format!("{}.txt", entry.ref_id));
                let _ = std::fs::remove_file(path);
            }
        }
    }
}

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

    fn test_cache(dir: &Path) -> ResultCache {
        ResultCache::new(dir, ResultCacheConfig::default()).unwrap()
    }

    #[test]
    fn test_store_and_get() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());

        let ref_id = cache
            .store("db_sql", "line1\nline2\nline3", BackendKind::Text)
            .unwrap();
        assert!(ref_id.starts_with("ref_"));

        let entry = cache.get(&ref_id).unwrap();
        assert_eq!(entry.tool_name, "db_sql");
        assert_eq!(entry.backend.kind(), BackendKind::Text);
    }

    #[test]
    fn test_get_not_found() {
        let dir = tempfile::tempdir().unwrap();
        let cache = test_cache(dir.path());

        let result = cache.get("ref_nonexistent");
        assert!(matches!(result, Err(CacheError::NotFound { .. })));
    }

    #[test]
    fn test_execute_op() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());

        let ref_id = cache
            .store("test", "alpha\nbeta\ngamma\ndelta", BackendKind::Text)
            .unwrap();

        let result = cache
            .execute_op(&ref_id, CacheOp::Head { lines: 2 })
            .unwrap();
        assert_eq!(result, "alpha\nbeta");
    }

    #[test]
    fn test_evict_expired() {
        let dir = tempfile::tempdir().unwrap();
        let config = ResultCacheConfig {
            ttl: Duration::from_millis(1),
            ..Default::default()
        };
        let mut cache = ResultCache::new(dir.path(), config).unwrap();

        cache.store("test", "data", BackendKind::Text).unwrap();
        assert_eq!(cache.len(), 1);

        // Wait for expiry
        std::thread::sleep(Duration::from_millis(10));

        let evicted = cache.evict_expired();
        assert_eq!(evicted, 1);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_expired_entry_returns_error() {
        let dir = tempfile::tempdir().unwrap();
        let config = ResultCacheConfig {
            ttl: Duration::from_millis(1),
            ..Default::default()
        };
        let mut cache = ResultCache::new(dir.path(), config).unwrap();

        let ref_id = cache.store("test", "data", BackendKind::Text).unwrap();

        std::thread::sleep(Duration::from_millis(10));

        assert!(matches!(
            cache.get(&ref_id),
            Err(CacheError::Expired { .. })
        ));
    }

    #[test]
    fn test_total_bytes() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());

        cache
            .store("test", "hello world", BackendKind::Text)
            .unwrap();
        assert!(cache.total_bytes() > 0);
    }

    #[test]
    fn test_multiple_entries() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());

        let r1 = cache.store("t1", "data1", BackendKind::Text).unwrap();
        let r2 = cache.store("t2", "data2", BackendKind::Text).unwrap();

        assert_ne!(r1, r2);
        assert_eq!(cache.len(), 2);

        assert_eq!(cache.get(&r1).unwrap().tool_name, "t1");
        assert_eq!(cache.get(&r2).unwrap().tool_name, "t2");
    }

    #[test]
    fn test_iter_returns_all_entries() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());

        let r1 = cache
            .store("db_sql", "SELECT 1", BackendKind::Text)
            .unwrap();
        let r2 = cache
            .store("web_search", "results here", BackendKind::Text)
            .unwrap();
        let r3 = cache
            .store("db_sql", "SELECT 2", BackendKind::Text)
            .unwrap();

        let entries: HashMap<String, String> = cache
            .iter()
            .map(|(ref_id, entry)| (ref_id.to_string(), entry.tool_name.clone()))
            .collect();

        assert_eq!(entries.len(), 3);
        assert_eq!(entries[&r1], "db_sql");
        assert_eq!(entries[&r2], "web_search");
        assert_eq!(entries[&r3], "db_sql");
    }

    #[test]
    fn test_backend_kind_display() {
        assert_eq!(format!("{}", BackendKind::Text), "text");
    }

    #[test]
    fn test_cache_debug() {
        let dir = tempfile::tempdir().unwrap();
        let cache = test_cache(dir.path());
        let debug = format!("{cache:?}");
        assert!(debug.contains("ResultCache"));
    }

    #[test]
    fn test_entry_debug() {
        let dir = tempfile::tempdir().unwrap();
        let mut cache = test_cache(dir.path());
        let ref_id = cache.store("tool", "data", BackendKind::Text).unwrap();
        let entry = cache.get(&ref_id).unwrap();
        let debug = format!("{entry:?}");
        assert!(debug.contains("CacheEntry"));
        assert!(debug.contains(&ref_id));
    }

    #[test]
    fn test_config_default() {
        let config = ResultCacheConfig::default();
        assert_eq!(config.ttl, Duration::from_secs(30 * 60));
        assert_eq!(config.max_disk_bytes, 100 * 1024 * 1024);
        assert_eq!(config.preview_lines, 20);
    }

    #[test]
    fn test_evict_cleans_files() {
        let dir = tempfile::tempdir().unwrap();
        let config = ResultCacheConfig {
            ttl: Duration::from_millis(1),
            ..Default::default()
        };
        let mut cache = ResultCache::new(dir.path(), config).unwrap();

        let ref_id = cache.store("test", "data", BackendKind::Text).unwrap();
        let file_path = dir.path().join(format!("{ref_id}.txt"));
        assert!(file_path.exists());

        std::thread::sleep(Duration::from_millis(10));
        cache.evict_expired();

        assert!(!file_path.exists());
    }
}