Skip to main content

lean_ctx/core/context_prefetch/
mod.rs

1//! Predictive context pre-loading (OSS stub).
2//!
3//! Enterprise pre-caches files based on task triage predictions.
4//! OSS: no-op (no prefetching).
5
6/// Warms the cache for predicted paths (OSS: no-op).
7pub fn warm_predictions<T>(_paths: &[String], _cache: Option<&T>) {}
8
9/// Plans prefetch after triage (OSS: no-op).
10pub fn plan_after_triage(_task_class: &str) {}
11
12/// Count of successfully warmed predictions (OSS: 0).
13pub fn warmed_count() -> u64 {
14    0
15}
16
17/// Count of skipped predictions (OSS: 0).
18pub fn skipped_count() -> u64 {
19    0
20}
21
22/// An entry in the prefetch plan.
23#[derive(Debug, Clone)]
24pub struct PrefetchEntry {
25    pub path: String,
26    pub score: f64,
27}
28
29/// A prefetch plan containing predicted files.
30#[derive(Debug, Clone, Default)]
31pub struct PrefetchPlan {
32    pub files: Vec<PrefetchEntry>,
33}
34
35/// File trajectory tracker (OSS stub).
36pub struct FileTrajectory {
37    paths: Vec<(String, u32)>,
38    capacity: usize,
39}
40
41impl FileTrajectory {
42    pub fn new(capacity: usize) -> Self {
43        Self {
44            paths: Vec::new(),
45            capacity,
46        }
47    }
48
49    pub fn record(&mut self, path: &str) {
50        if let Some(entry) = self.paths.iter_mut().find(|(p, _)| p == path) {
51            entry.1 += 1;
52        } else {
53            if self.paths.len() >= self.capacity {
54                self.paths.remove(0);
55            }
56            self.paths.push((path.to_owned(), 1));
57        }
58    }
59
60    pub fn predict(&self, top_k: usize) -> Vec<(String, f64)> {
61        let mut scored: Vec<(String, f64)> = self
62            .paths
63            .iter()
64            .map(|(path, count)| (path.clone(), *count as f64))
65            .collect();
66        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
67        scored.truncate(top_k);
68        scored
69    }
70}
71
72/// Builds a prefetch plan from trajectory data (OSS: basic frequency-based).
73pub fn build_prefetch_plan(
74    trajectory: &FileTrajectory,
75    _exclude: &[String],
76    top_k: usize,
77    _min_score: f64,
78) -> PrefetchPlan {
79    let files = trajectory
80        .predict(top_k)
81        .into_iter()
82        .map(|(path, score)| PrefetchEntry { path, score })
83        .collect();
84    PrefetchPlan { files }
85}
86
87/// Records a file read for trajectory learning (OSS: no-op).
88pub fn record_file_read(_path: &str) {}
89
90/// Checks if a path was predicted by the prefetch engine (OSS: always false).
91pub fn is_prefetch_prediction(_path: &str) -> bool {
92    false
93}