Skip to main content

lean_ctx/core/
shared_context.rs

1//! Durable facts agents share through `ctx_memory`, partitioned by project.
2//!
3//! Each project has its own `shared_context/<project hash>.jsonl` in the
4//! LeanCTX data directory, and every entry records the project it was stored
5//! for; that stored root, not the file name, decides whether an entry is
6//! served. Releases before 3.11.5 kept one installation-wide
7//! `~/.local/share/lean-ctx/shared_context.jsonl` without a project. Those
8//! entries carry no provable scope: the file is left in place, unread, and is
9//! never served to any project.
10
11use std::collections::{BTreeMap, HashSet};
12use std::fs;
13use std::io::{BufRead, BufReader};
14use std::path::{Path, PathBuf};
15use std::time::{SystemTime, UNIX_EPOCH};
16
17use serde::{Deserialize, Serialize};
18
19/// A fact shared between independent agent sessions.
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub struct SharedContextEntry {
22    pub id: String,
23    pub content: String,
24    pub agent: String,
25    pub timestamp: u64,
26    pub category: String,
27    /// Confidence assigned to this fact, from 0.0 (untrusted) to 1.0 (certain).
28    #[serde(default = "default_confidence")]
29    pub confidence: f32,
30    pub access_count: u32,
31    pub last_accessed: u64,
32    /// Unix timestamp when an outcome last confirmed this fact.
33    #[serde(default)]
34    pub last_verified: Option<u64>,
35    /// Number of successful verification events recorded for this fact.
36    #[serde(default)]
37    pub verification_count: u32,
38    /// Exponential-moving-average correlation between this fact and successful outcomes.
39    #[serde(default)]
40    pub outcome_correlation: f32,
41    /// Optional per-fact verification window before the fact becomes stale.
42    #[serde(default)]
43    pub stale_after_days: Option<u16>,
44    /// Agent or user that first added this fact.
45    #[serde(default)]
46    pub created_by: String,
47    /// Hash of the fact that supersedes this one.
48    #[serde(default)]
49    pub superseded_by: Option<String>,
50    /// The project this fact was stored for; the only authority for serving it.
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub project_root: Option<String>,
53}
54
55/// Backward-compatible name for an entry in shared fact memory.
56pub type FactEntry = SharedContextEntry;
57
58/// Summary of the contents of a [`SharedContext`] store.
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
60pub struct SharedContextStats {
61    pub total_entries: usize,
62    pub unique_agents: usize,
63    pub categories: BTreeMap<String, usize>,
64    pub total_accesses: u64,
65}
66
67/// Quality and contributor summary across shared-memory sessions.
68#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
69pub struct MemoryStats {
70    pub total_facts: usize,
71    pub verified_pct: f32,
72    pub avg_confidence: f32,
73    /// Contributors ordered by descending fact count, then contributor name.
74    pub top_contributors: Vec<(String, usize)>,
75}
76
77/// Durable cross-agent memory backed by an appendable JSONL file.
78#[derive(Debug, Clone)]
79pub struct SharedContext {
80    path: PathBuf,
81    /// Set for a project partition: only entries stored for this root load.
82    project_root: Option<String>,
83}
84
85impl SharedContext {
86    /// An unpartitioned store at `path` (tests and explicit tooling).
87    pub fn new(path: PathBuf) -> Self {
88        Self {
89            path,
90            project_root: None,
91        }
92    }
93
94    /// The installation-wide store of releases before 3.11.5. Served to no
95    /// project; read for the explicit `lean-ctx migrate legacy-context`
96    /// adoption only.
97    pub fn legacy() -> Option<Self> {
98        // Those releases wrote under `dirs::home_dir()`. Tests redirect the
99        // home through `HOME`, which `dirs` ignores on Windows (known folder):
100        // without this they would share the runner's real profile.
101        let home = if crate::core::data_dir::sandboxed() {
102            crate::core::home::resolve_home_dir()
103        } else {
104            dirs::home_dir()
105        };
106        home.map(|home| Self::new(home.join(".local/share/lean-ctx/shared_context.jsonl")))
107    }
108
109    /// Facts left in a partition that is no longer the current one of the
110    /// root they recorded: that project's identity (remote, package name)
111    /// changed since. Served to no project until adopted.
112    pub fn orphaned_entries() -> Vec<(PathBuf, SharedContextEntry)> {
113        let Ok(dir) = crate::core::data_dir::lean_ctx_data_dir().map(|d| d.join("shared_context"))
114        else {
115            return Vec::new();
116        };
117        let Ok(files) = fs::read_dir(&dir) else {
118            return Vec::new();
119        };
120        let mut paths: Vec<PathBuf> = files
121            .flatten()
122            .map(|file| file.path())
123            .filter(|path| path.extension().is_some_and(|ext| ext == "jsonl"))
124            .collect();
125        paths.sort();
126        let mut orphaned = Vec::new();
127        for path in paths {
128            let stem = path
129                .file_stem()
130                .map(|stem| stem.to_string_lossy().into_owned())
131                .unwrap_or_default();
132            for entry in Self::new(path.clone()).load().unwrap_or_default() {
133                if entry
134                    .project_root
135                    .as_deref()
136                    .is_some_and(|root| crate::core::project_hash::hash_project_root(root) != stem)
137                {
138                    orphaned.push((path.clone(), entry));
139                }
140            }
141        }
142        orphaned
143    }
144
145    /// The orphaned facts that recorded `project_root` (canonically).
146    pub(crate) fn drifted_entries(project_root: &str) -> Vec<(PathBuf, SharedContextEntry)> {
147        let canonical =
148            |root: &str| crate::core::pathutil::safe_canonicalize_bounded(Path::new(root), 2000);
149        let wanted = canonical(project_root);
150        Self::orphaned_entries()
151            .into_iter()
152            .filter(|(_, entry)| {
153                entry
154                    .project_root
155                    .as_deref()
156                    .is_some_and(|root| canonical(root) == wanted)
157            })
158            .collect()
159    }
160
161    /// The shared facts of `project_root`, in its own partition of the data
162    /// directory.
163    pub fn for_project(project_root: &str) -> Result<Self, String> {
164        if project_root.trim().is_empty() {
165            return Err("shared memory needs a project".into());
166        }
167        let path = crate::core::data_dir::lean_ctx_data_dir()?
168            .join("shared_context")
169            .join(format!(
170                "{}.jsonl",
171                crate::core::project_hash::hash_project_root(project_root)
172            ));
173        Ok(Self {
174            path,
175            project_root: Some(project_root.to_string()),
176        })
177    }
178
179    pub fn path(&self) -> &Path {
180        &self.path
181    }
182
183    pub fn put(
184        &self,
185        content: impl Into<String>,
186        agent: impl Into<String>,
187        category: impl Into<String>,
188    ) -> Result<String, String> {
189        let content = content.into();
190        let agent = agent.into();
191        let category = normalize_category(&category.into())?;
192        let id = blake3::hash(content.as_bytes()).to_hex().to_string();
193        let now = unix_timestamp();
194        let _writer = self.write_lock()?;
195        let mut entries = self.load()?;
196
197        if let Some(existing) = entries.iter_mut().find(|entry| entry.id == id) {
198            existing.agent.clone_from(&agent);
199            existing.category.clone_from(&category);
200            existing.timestamp = now;
201            existing.last_accessed = now;
202            self.save(&entries)?;
203            return Ok(id);
204        }
205
206        entries.push(SharedContextEntry {
207            id: id.clone(),
208            content,
209            agent: agent.clone(),
210            timestamp: now,
211            category,
212            confidence: default_confidence(),
213            access_count: 0,
214            last_accessed: now,
215            last_verified: None,
216            verification_count: 0,
217            outcome_correlation: 0.0,
218            stale_after_days: None,
219            created_by: agent,
220            superseded_by: None,
221            project_root: self.project_root.clone(),
222        });
223        self.save(&entries)?;
224        Ok(id)
225    }
226
227    pub fn get_relevant(
228        &self,
229        query: &str,
230        limit: usize,
231    ) -> Result<Vec<SharedContextEntry>, String> {
232        if limit == 0 {
233            return Ok(Vec::new());
234        }
235        let _writer = self.write_lock()?;
236        let mut entries = self.load()?;
237        let query_terms = terms(query);
238        if query_terms.is_empty() {
239            return Ok(Vec::new());
240        }
241        let document_count = entries.len() as f64;
242        let average_length = entries
243            .iter()
244            .map(|entry| terms(&entry.content).len() as f64)
245            .sum::<f64>()
246            / document_count.max(1.0);
247        let document_frequency = query_terms
248            .iter()
249            .map(|term| {
250                (
251                    term,
252                    entries
253                        .iter()
254                        .filter(|entry| terms(&entry.content).contains(term))
255                        .count() as f64,
256                )
257            })
258            .collect::<BTreeMap<_, _>>();
259
260        let mut scored = entries
261            .iter()
262            .enumerate()
263            .filter_map(|(index, entry)| {
264                let entry_terms = terms(&entry.content);
265                let length = entry_terms.len() as f64;
266                let score = query_terms.iter().fold(0.0, |score, term| {
267                    let frequency =
268                        entry_terms.iter().filter(|value| *value == term).count() as f64;
269                    if frequency == 0.0 {
270                        return score;
271                    }
272                    let df = document_frequency.get(term).copied().unwrap_or_default();
273                    let idf = ((document_count - df + 0.5) / (df + 0.5) + 1.0).ln();
274                    let k1 = 1.2;
275                    let b = 0.75;
276                    score
277                        + idf * frequency * (k1 + 1.0)
278                            / (frequency + k1 * (1.0 - b + b * length / average_length.max(1.0)))
279                });
280                (score > 0.0).then_some((index, score))
281            })
282            .collect::<Vec<_>>();
283        scored.sort_by(|left, right| {
284            right
285                .1
286                .total_cmp(&left.1)
287                .then_with(|| left.0.cmp(&right.0))
288        });
289
290        let now = unix_timestamp();
291        let selected = scored
292            .into_iter()
293            .take(limit)
294            .map(|(index, _)| index)
295            .collect::<Vec<_>>();
296        for index in &selected {
297            let entry = &mut entries[*index];
298            entry.access_count = entry.access_count.saturating_add(1);
299            entry.last_accessed = now;
300        }
301        let results = selected
302            .into_iter()
303            .map(|index| entries[index].clone())
304            .collect();
305        self.save(&entries)?;
306        Ok(results)
307    }
308
309    pub fn get_recent(&self, limit: usize) -> Result<Vec<SharedContextEntry>, String> {
310        let mut entries = self.load()?;
311        entries.sort_by(|left, right| {
312            right
313                .timestamp
314                .cmp(&left.timestamp)
315                .then_with(|| right.id.cmp(&left.id))
316        });
317        Ok(entries.into_iter().take(limit).collect())
318    }
319
320    pub fn get_by_category(&self, category: &str) -> Result<Vec<SharedContextEntry>, String> {
321        let category = normalize_category(category)?;
322        let mut entries = self
323            .load()?
324            .into_iter()
325            .filter(|entry| entry.category == category)
326            .collect::<Vec<_>>();
327        entries.sort_by(|left, right| {
328            right
329                .timestamp
330                .cmp(&left.timestamp)
331                .then_with(|| right.id.cmp(&left.id))
332        });
333        Ok(entries)
334    }
335
336    pub fn prune(&self, max_age_seconds: u64, max_entries: usize) -> Result<usize, String> {
337        let now = unix_timestamp();
338        let _writer = self.write_lock()?;
339        let mut entries = self.load()?;
340        let original_len = entries.len();
341        entries.retain(|entry| now.saturating_sub(entry.last_accessed) <= max_age_seconds);
342        entries.sort_by(|left, right| {
343            right
344                .last_accessed
345                .cmp(&left.last_accessed)
346                .then_with(|| right.access_count.cmp(&left.access_count))
347                .then_with(|| right.timestamp.cmp(&left.timestamp))
348        });
349        entries.truncate(max_entries);
350        let removed = original_len.saturating_sub(entries.len());
351        self.save(&entries)?;
352        Ok(removed)
353    }
354
355    /// Record an explicit confirmation of a fact.
356    pub fn verify_fact(&self, hash: &str) -> Result<(), String> {
357        let _writer = self.write_lock()?;
358        let mut entries = self.load()?;
359        let entry = find_fact_mut(&mut entries, hash)?;
360        entry.last_verified = Some(unix_timestamp());
361        entry.verification_count = entry.verification_count.saturating_add(1);
362        self.save(&entries)
363    }
364
365    /// Lower the confidence of a fact identified as stale without removing it.
366    pub fn mark_stale(&self, hash: &str) -> Result<(), String> {
367        let _writer = self.write_lock()?;
368        let mut entries = self.load()?;
369        find_fact_mut(&mut entries, hash)?.confidence = 0.3;
370        self.save(&entries)
371    }
372
373    /// Mark `old_hash` as replaced by the existing fact identified by `new_fact`.
374    pub fn supersede(&self, old_hash: &str, new_fact: &str) -> Result<(), String> {
375        let _writer = self.write_lock()?;
376        let mut entries = self.load()?;
377        if old_hash == new_fact {
378            return Err("a fact cannot supersede itself".to_string());
379        }
380        if !entries.iter().any(|entry| entry.id == new_fact) {
381            return Err(format!("fact '{new_fact}' does not exist"));
382        }
383        find_fact_mut(&mut entries, old_hash)?.superseded_by = Some(new_fact.to_string());
384        self.save(&entries)
385    }
386
387    /// Remove facts whose latest verification (or creation) predates their expiry window.
388    pub fn prune_stale(&self, max_age_days: u16) -> Result<usize, String> {
389        let now = unix_timestamp();
390        let _writer = self.write_lock()?;
391        let mut entries = self.load()?;
392        let original_len = entries.len();
393        entries.retain(|entry| {
394            let age_days = entry.stale_after_days.unwrap_or(max_age_days);
395            let reference_time = entry.last_verified.unwrap_or(entry.timestamp);
396            now.saturating_sub(reference_time) <= u64::from(age_days) * SECONDS_PER_DAY
397        });
398        let removed = original_len.saturating_sub(entries.len());
399        self.save(&entries)?;
400        Ok(removed)
401    }
402
403    /// Remove facts consistently associated with unsuccessful outcomes.
404    pub fn prune_negative(&self) -> Result<usize, String> {
405        let _writer = self.write_lock()?;
406        let mut entries = self.load()?;
407        let original_len = entries.len();
408        entries.retain(|entry| entry.outcome_correlation >= NEGATIVE_CORRELATION_THRESHOLD);
409        let removed = original_len.saturating_sub(entries.len());
410        self.save(&entries)?;
411        Ok(removed)
412    }
413
414    /// Return query-matching facts ordered by their provenance quality score.
415    pub fn get_best(&self, query: &str, limit: usize) -> Result<Vec<SharedContextEntry>, String> {
416        if limit == 0 {
417            return Ok(Vec::new());
418        }
419        let _writer = self.write_lock()?;
420        let mut entries = self.load()?;
421        let query_terms = terms(query);
422        if query_terms.is_empty() {
423            return Ok(Vec::new());
424        }
425
426        let mut matches = entries
427            .iter()
428            .enumerate()
429            .filter_map(|(index, entry)| {
430                let entry_terms = terms(&entry.content);
431                let relevance = query_terms
432                    .iter()
433                    .filter(|term| entry_terms.contains(term))
434                    .count();
435                (relevance > 0).then_some((index, relevance))
436            })
437            .collect::<Vec<_>>();
438        matches.sort_by(|left, right| {
439            let left_quality = entries[left.0].confidence * entries[left.0].outcome_correlation;
440            let right_quality = entries[right.0].confidence * entries[right.0].outcome_correlation;
441            right_quality
442                .total_cmp(&left_quality)
443                .then_with(|| right.1.cmp(&left.1))
444                .then_with(|| entries[left.0].id.cmp(&entries[right.0].id))
445        });
446
447        let now = unix_timestamp();
448        let selected = matches
449            .into_iter()
450            .take(limit)
451            .map(|(index, _)| index)
452            .collect::<Vec<_>>();
453        for index in &selected {
454            let entry = &mut entries[*index];
455            entry.access_count = entry.access_count.saturating_add(1);
456            entry.last_accessed = now;
457        }
458        let results = selected
459            .into_iter()
460            .map(|index| entries[index].clone())
461            .collect();
462        self.save(&entries)?;
463        Ok(results)
464    }
465
466    /// Fold an outcome into a fact's correlation score with an exponential moving average.
467    pub fn record_fact_outcome(&self, hash: &str, success: bool) -> Result<(), String> {
468        let _writer = self.write_lock()?;
469        let mut entries = self.load()?;
470        let entry = find_fact_mut(&mut entries, hash)?;
471        let outcome = if success { 1.0 } else { -1.0 };
472        entry.outcome_correlation = (entry.outcome_correlation * (1.0 - OUTCOME_EMA_ALPHA)
473            + outcome * OUTCOME_EMA_ALPHA)
474            .clamp(-1.0, 1.0);
475        self.save(&entries)
476    }
477
478    /// Summarize provenance quality and the agents or users that contributed facts.
479    pub fn cross_session_stats(&self) -> Result<MemoryStats, String> {
480        let entries = self.load()?;
481        let total_facts = entries.len();
482        let verified = entries
483            .iter()
484            .filter(|entry| entry.verification_count > 0)
485            .count();
486        let avg_confidence = (total_facts > 0).then(|| {
487            entries.iter().map(|entry| entry.confidence).sum::<f32>() / total_facts as f32
488        });
489        let mut contributors = BTreeMap::new();
490        for entry in &entries {
491            let contributor = if entry.created_by.is_empty() {
492                &entry.agent
493            } else {
494                &entry.created_by
495            };
496            *contributors.entry(contributor.clone()).or_insert(0) += 1;
497        }
498        let mut top_contributors = contributors.into_iter().collect::<Vec<_>>();
499        top_contributors
500            .sort_by(|left, right| right.1.cmp(&left.1).then_with(|| left.0.cmp(&right.0)));
501
502        Ok(MemoryStats {
503            total_facts,
504            verified_pct: percentage(verified, total_facts),
505            avg_confidence: avg_confidence.unwrap_or(0.0),
506            top_contributors,
507        })
508    }
509
510    pub fn stats(&self) -> Result<SharedContextStats, String> {
511        let entries = self.load()?;
512        let unique_agents = entries
513            .iter()
514            .map(|entry| entry.agent.as_str())
515            .collect::<HashSet<_>>()
516            .len();
517        let mut categories = BTreeMap::new();
518        for entry in &entries {
519            *categories.entry(entry.category.clone()).or_insert(0) += 1;
520        }
521        Ok(SharedContextStats {
522            total_entries: entries.len(),
523            unique_agents,
524            categories,
525            total_accesses: entries
526                .iter()
527                .map(|entry| u64::from(entry.access_count))
528                .sum(),
529        })
530    }
531
532    /// Every read-modify-write of this store holds an exclusive lock on a
533    /// sibling `.lock` file from its load to its save, so writers in other
534    /// threads or processes (two agents of one project) cannot replace the
535    /// file with a copy that lacks their entries. Released on drop.
536    fn write_lock(&self) -> Result<crate::core::file_lock::FlockGuard, String> {
537        let lock_path = self.path.with_extension("lock");
538        if let Some(parent) = lock_path.parent() {
539            fs::create_dir_all(parent)
540                .map_err(|error| format!("create {}: {error}", parent.display()))?;
541        }
542        // Windows `LockFileEx` needs a handle with GENERIC_READ or
543        // GENERIC_WRITE; an append-only handle lacks FILE_WRITE_DATA, so every
544        // lock attempt failed with "Access is denied" (os error 5).
545        let file = fs::OpenOptions::new()
546            .create(true)
547            .read(true)
548            .write(true)
549            .truncate(false)
550            .open(&lock_path)
551            .map_err(|error| format!("open {}: {error}", lock_path.display()))?;
552        crate::core::file_lock::FlockGuard::exclusive_timeout(file, WRITE_LOCK_WAIT)
553            .map_err(|error| format!("shared memory is busy: {error}"))
554    }
555
556    pub fn load(&self) -> Result<Vec<SharedContextEntry>, String> {
557        if !self.path.exists() {
558            return Ok(Vec::new());
559        }
560        let file = fs::File::open(&self.path)
561            .map_err(|error| format!("open {}: {error}", self.path.display()))?;
562        let mut entries = BufReader::new(file)
563            .lines()
564            .filter(|line| line.as_ref().is_ok_and(|value| !value.trim().is_empty()))
565            .map(|line| {
566                let line =
567                    line.map_err(|error| format!("read {}: {error}", self.path.display()))?;
568                serde_json::from_str(&line)
569                    .map_err(|error| format!("parse {}: {error}", self.path.display()))
570            })
571            .collect::<Result<Vec<SharedContextEntry>, String>>()?;
572        for entry in &mut entries {
573            apply_provenance_defaults(entry);
574        }
575        if let Some(root) = &self.project_root {
576            // The file name is a hash; the stored root is the authority.
577            entries.retain(|entry| {
578                entry
579                    .project_root
580                    .as_deref()
581                    .is_some_and(|stored| same_root(stored, root))
582            });
583        }
584        Ok(entries)
585    }
586
587    fn save(&self, entries: &[SharedContextEntry]) -> Result<(), String> {
588        let parent = self
589            .path
590            .parent()
591            .ok_or_else(|| format!("{} has no parent", self.path.display()))?;
592        fs::create_dir_all(parent)
593            .map_err(|error| format!("create {}: {error}", parent.display()))?;
594        let mut content = String::new();
595        for entry in entries {
596            content.push_str(
597                &serde_json::to_string(entry)
598                    .map_err(|error| format!("serialize entry: {error}"))?,
599            );
600            content.push('\n');
601        }
602        crate::config_io::write_atomic(&self.path, &content)
603            .map_err(|error| format!("replace {}: {error}", self.path.display()))
604    }
605}
606
607fn same_root(stored: &str, requested: &str) -> bool {
608    use crate::core::pathutil::normalize_tool_path;
609    normalize_tool_path(stored).trim_end_matches('/')
610        == normalize_tool_path(requested).trim_end_matches('/')
611}
612
613/// How long a writer waits for another writer of the same store.
614const WRITE_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(5);
615const SECONDS_PER_DAY: u64 = 24 * 60 * 60;
616const NEGATIVE_CORRELATION_THRESHOLD: f32 = -0.3;
617const OUTCOME_EMA_ALPHA: f32 = 0.2;
618
619fn default_confidence() -> f32 {
620    0.8
621}
622
623fn find_fact_mut<'a>(
624    entries: &'a mut [SharedContextEntry],
625    hash: &str,
626) -> Result<&'a mut SharedContextEntry, String> {
627    entries
628        .iter_mut()
629        .find(|entry| entry.id == hash)
630        .ok_or_else(|| format!("fact '{hash}' does not exist"))
631}
632
633fn apply_provenance_defaults(entry: &mut SharedContextEntry) {
634    entry.confidence = entry.confidence.clamp(0.0, 1.0);
635    entry.outcome_correlation = entry.outcome_correlation.clamp(-1.0, 1.0);
636    if entry.created_by.is_empty() {
637        entry.created_by.clone_from(&entry.agent);
638    }
639}
640
641fn percentage(numerator: usize, denominator: usize) -> f32 {
642    if denominator == 0 {
643        0.0
644    } else {
645        numerator as f32 / denominator as f32 * 100.0
646    }
647}
648
649fn normalize_category(category: &str) -> Result<String, String> {
650    match category {
651        "fact" | "decision" | "blocker" | "pattern" => Ok(category.to_string()),
652        other => Err(format!(
653            "invalid category '{other}' (expected fact|decision|blocker|pattern)"
654        )),
655    }
656}
657
658fn terms(value: &str) -> Vec<String> {
659    value
660        .split(|character: char| !character.is_alphanumeric())
661        .filter(|term| !term.is_empty())
662        .map(str::to_lowercase)
663        .collect()
664}
665
666/// The newest shared facts of `project_root` as a context hint, re-admitted
667/// under the originless rule; nothing when the project has none or the hint
668/// may not be served now.
669pub fn session_start_hint(project_root: &str) -> Option<String> {
670    crate::core::legacy_adoption::auto_adopt_shared_context(project_root);
671    let entries = SharedContext::for_project(project_root)
672        .ok()?
673        .get_recent(3)
674        .ok()?;
675    if entries.is_empty() {
676        return None;
677    }
678    let facts = entries
679        .iter()
680        .map(|entry| format!("- [{} · {}] {}", entry.category, entry.agent, entry.content))
681        .collect::<Vec<_>>()
682        .join("\n");
683    crate::core::admitted_sources::serve_originless(
684        &format!("--- SHARED CONTEXT ---\n{facts}"),
685        "shared memory",
686    )
687    .ok()
688}
689
690fn unix_timestamp() -> u64 {
691    SystemTime::now()
692        .duration_since(UNIX_EPOCH)
693        .map_or(0, |duration| duration.as_secs())
694}
695
696#[cfg(test)]
697mod tests {
698    use super::*;
699
700    fn store(name: &str) -> SharedContext {
701        let path = std::env::temp_dir().join(format!(
702            "lean-ctx-shared-context-{name}-{}.jsonl",
703            uuid::Uuid::new_v4()
704        ));
705        SharedContext::new(path)
706    }
707
708    /// AP05-G2: one installation-wide file served every project's facts, and
709    /// the session hint decorated every tool result with them.
710    #[test]
711    fn shared_facts_and_hint_stay_in_their_project() {
712        let _data = crate::core::data_dir::isolated_data_dir();
713        let _community = crate::core::policy::runtime::TestPolicyOverride::set(None);
714        let a = tempfile::tempdir().unwrap();
715        let b = tempfile::tempdir().unwrap();
716        let (a, b) = (a.path().to_str().unwrap(), b.path().to_str().unwrap());
717        SharedContext::for_project(a)
718            .unwrap()
719            .put("project-a-marker lives in net/retry.rs", "codex", "fact")
720            .unwrap();
721        let foreign = SharedContext::for_project(b).unwrap();
722        assert!(foreign.get_recent(10).unwrap().is_empty());
723        assert!(
724            foreign
725                .get_relevant("project-a-marker", 10)
726                .unwrap()
727                .is_empty()
728        );
729        assert_eq!(session_start_hint(b), None);
730        let own = session_start_hint(a).expect("own project hint");
731        assert!(own.contains("project-a-marker"), "{own}");
732    }
733
734    /// Several projects through several stores at once, in one data
735    /// directory (AP01 §1/§3; ABNAHME I02, C01): a scope that leaks through
736    /// process-wide state (cwd, environment, a "current project" global) or a
737    /// store without a writer lock shows up as a value in the wrong partition
738    /// or as a lost acknowledged write.
739    mod partitions {
740        use std::collections::BTreeSet;
741        use std::path::PathBuf;
742        use std::sync::{Arc, Barrier};
743
744        use crate::core::agents::AgentRegistry;
745        use crate::core::knowledge::ProjectKnowledge;
746        use crate::core::memory_policy::MemoryPolicy;
747        use crate::core::session::{SessionState, TaskInfo};
748        use crate::core::shared_context::SharedContext;
749
750        /// The same fact keys in every project: a partition mix-up overwrites.
751        const KEYS: [&str; 4] = ["db", "queue", "cache", "auth"];
752
753        fn in_project<T>(root: &str, f: impl FnOnce() -> T) -> T {
754            crate::core::policy::runtime::REQUEST_PROJECT
755                .sync_scope(std::cell::RefCell::new(Some(PathBuf::from(root))), f)
756        }
757
758        fn project(dir: &tempfile::TempDir, name: &str) -> String {
759            let root = dir.path().join(name);
760            std::fs::create_dir_all(&root).unwrap();
761            root.canonicalize().unwrap().to_string_lossy().into_owned()
762        }
763
764        /// Every write one writer makes, each acknowledged before the writer ends.
765        fn write_everywhere(root: &str, key: &str, value: &str) {
766            in_project(root, || {
767                let policy = MemoryPolicy::default();
768                let (knowledge, ()) = ProjectKnowledge::mutate_locked(root, |k| {
769                    k.remember("decision", key, value, "partition-test", 0.9, &policy);
770                })
771                .expect("knowledge write acknowledged");
772                let fact = knowledge
773                    .facts
774                    .iter()
775                    .find(|f| f.key == key && f.value == value)
776                    .cloned()
777                    .expect("acknowledged fact is in the returned store");
778                crate::core::memory_lifecycle::archive_facts(&[fact])
779                    .expect("archive acknowledged");
780                SharedContext::for_project(root)
781                    .and_then(|store| store.put(format!("{key} {value}"), key, "decision"))
782                    .expect("shared memory write acknowledged");
783                let mut session = SessionState::new();
784                session.project_root = Some(root.to_owned());
785                session.task = Some(TaskInfo {
786                    description: value.to_owned(),
787                    intent: None,
788                    progress_pct: None,
789                });
790                session.save().expect("session save acknowledged");
791                // The registry lock is a polled try-lock (750 ms, not a fair
792                // queue, GitLab #1295): with every writer released by one
793                // barrier, a write can be refused under load. A refused write
794                // was never acknowledged and is retried; the property under
795                // test is that no acknowledged write is lost.
796                let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
797                loop {
798                    match AgentRegistry::mutate_locked(|registry| {
799                        registry.share_knowledge(
800                            root,
801                            key,
802                            "decision",
803                            &[(key.into(), value.into())],
804                        )
805                    }) {
806                        Ok((_, shared)) => {
807                            shared.expect("shared knowledge acknowledged");
808                            break;
809                        }
810                        Err(error)
811                            if error.contains("timed out")
812                                && std::time::Instant::now() < deadline => {}
813                        Err(error) => panic!("registry lock: {error}"),
814                    }
815                }
816            });
817        }
818
819        /// What each store of `root` holds now, as one set of values per store.
820        fn partition_contents(root: &str) -> Vec<(&'static str, BTreeSet<String>)> {
821            in_project(root, || {
822                let knowledge: BTreeSet<String> = ProjectKnowledge::load(root)
823                    .map(|k| {
824                        k.facts
825                            .iter()
826                            .filter(|f| f.is_current())
827                            .map(|f| format!("{} {}", f.key, f.value))
828                            .collect()
829                    })
830                    .unwrap_or_default();
831                let archive: BTreeSet<String> = crate::core::memory_lifecycle::list_archives(root)
832                    .iter()
833                    .flat_map(|path| {
834                        crate::core::memory_lifecycle::restore_archive(
835                            root,
836                            &path.to_string_lossy(),
837                        )
838                        .expect("own archive restores")
839                    })
840                    .map(|f| format!("{} {}", f.key, f.value))
841                    .collect();
842                let shared: BTreeSet<String> = SharedContext::for_project(root)
843                    .and_then(|store| store.get_recent(usize::MAX))
844                    .expect("shared memory")
845                    .into_iter()
846                    .map(|entry| entry.content)
847                    .collect();
848                let sessions: BTreeSet<String> =
849                    SessionState::load_recent_for_project_root(root, usize::MAX)
850                        .into_iter()
851                        .filter_map(|session| session.task.map(|task| task.description))
852                        .map(|value| {
853                            let key = KEYS
854                                .iter()
855                                .find(|key| value.ends_with(&format!("-{key}")))
856                                .expect("task names its key");
857                            format!("{key} {value}")
858                        })
859                        .collect();
860                let registry: BTreeSet<String> = AgentRegistry::new()
861                    .receive_shared_knowledge(root, "partition-reader")
862                    .expect("shared knowledge")
863                    .into_iter()
864                    .map(|fact| fact.text.replacen('=', " ", 1))
865                    .collect();
866                vec![
867                    ("knowledge", knowledge),
868                    ("archive", archive),
869                    ("shared memory", shared),
870                    ("sessions", sessions),
871                    ("shared knowledge", registry),
872                ]
873            })
874        }
875
876        /// I02/C01: writers of two projects start together (one barrier) and write
877        /// the same fact keys into one data directory through knowledge, archive,
878        /// shared memory, sessions and shared agent knowledge. Every acknowledged
879        /// write is kept, each in its own project only.
880        #[test]
881        fn concurrent_writes_into_two_projects_keep_every_write_in_its_own_partition() {
882            let _data = crate::core::data_dir::isolated_data_dir();
883            let _community = crate::core::policy::runtime::TestPolicyOverride::set(None);
884            let dir = tempfile::tempdir().unwrap();
885            let projects = [("p1", project(&dir, "p1")), ("p2", project(&dir, "p2"))];
886            let start = Arc::new(Barrier::new(projects.len() * KEYS.len()));
887            let start = &start;
888            let writers: Vec<_> = projects
889                .iter()
890                .flat_map(|(name, root)| {
891                    KEYS.iter().map(move |key| {
892                        let (start, root) = (Arc::clone(start), root.clone());
893                        let value = format!("canary-{name}-{key}");
894                        std::thread::spawn(move || {
895                            start.wait();
896                            write_everywhere(&root, key, &value);
897                        })
898                    })
899                })
900                .collect();
901            for writer in writers {
902                writer.join().expect("writer acknowledged every write");
903            }
904
905            for (name, root) in &projects {
906                let expected: BTreeSet<String> = KEYS
907                    .iter()
908                    .map(|key| format!("{key} canary-{name}-{key}"))
909                    .collect();
910                for (store, held) in partition_contents(root) {
911                    assert_eq!(held, expected, "{store} of {name}");
912                }
913            }
914        }
915    }
916
917    #[test]
918    fn put_and_get_round_trip() {
919        let context = store("round-trip");
920        let id = context
921            .put("Use cargo test --lib before commit", "codex", "fact")
922            .unwrap();
923        let entries = context.get_relevant("cargo commit", 10).unwrap();
924        assert_eq!(entries.len(), 1);
925        assert_eq!(entries[0].id, id);
926        assert_eq!(entries[0].agent, "codex");
927        assert_eq!(entries[0].access_count, 1);
928    }
929
930    #[test]
931    fn duplicate_content_updates_in_place() {
932        let context = store("dedup");
933        let first = context
934            .put("The parser needs UTF-8 input", "claude", "fact")
935            .unwrap();
936        let second = context
937            .put("The parser needs UTF-8 input", "codex", "decision")
938            .unwrap();
939        assert_eq!(first, second);
940        let stats = context.stats().unwrap();
941        assert_eq!(stats.total_entries, 1);
942        assert_eq!(stats.unique_agents, 1);
943        assert_eq!(context.get_recent(1).unwrap()[0].category, "decision");
944    }
945
946    #[test]
947    fn bm25_prefers_more_relevant_entries() {
948        let context = store("bm25");
949        context
950            .put("SQLite migration preserves agent memory", "claude", "fact")
951            .unwrap();
952        context
953            .put(
954                "Agent memory migration migration migration is complete",
955                "cursor",
956                "fact",
957            )
958            .unwrap();
959        context
960            .put("Rendering uses SVG sprites", "codex", "pattern")
961            .unwrap();
962        let entries = context.get_relevant("agent memory migration", 2).unwrap();
963        assert_eq!(entries[0].agent, "cursor");
964        assert_eq!(entries.len(), 2);
965    }
966
967    #[test]
968    fn prune_removes_expired_and_excess_entries() {
969        let context = store("prune");
970        context.put("old", "claude", "fact").unwrap();
971        context.put("new one", "codex", "fact").unwrap();
972        context.put("new two", "cursor", "fact").unwrap();
973        let mut entries = context.load().unwrap();
974        entries
975            .iter_mut()
976            .find(|entry| entry.content == "old")
977            .unwrap()
978            .last_accessed = 0;
979        context.save(&entries).unwrap();
980        // An hour, not a second: a slow runner must not age the fresh entries
981        // out. "old" expires by age, and one fresh entry goes to the cap.
982        assert_eq!(context.prune(3600, 1).unwrap(), 2);
983        assert_eq!(context.stats().unwrap().total_entries, 1);
984    }
985
986    #[test]
987    fn verify_fact_records_timestamp_and_count() {
988        let context = store("verify");
989        let id = context
990            .put("Cargo checks are required", "codex", "fact")
991            .unwrap();
992
993        context.verify_fact(&id).unwrap();
994        context.verify_fact(&id).unwrap();
995
996        let entry = context.get_recent(1).unwrap().pop().unwrap();
997        assert_eq!(entry.verification_count, 2);
998        assert!(entry.last_verified.is_some());
999    }
1000
1001    #[test]
1002    fn prune_stale_removes_old_unverified_facts() {
1003        let context = store("stale");
1004        context.put("outdated command", "codex", "fact").unwrap();
1005        context.put("current command", "codex", "fact").unwrap();
1006        let mut entries = context.load().unwrap();
1007        entries[0].timestamp = unix_timestamp().saturating_sub(2 * SECONDS_PER_DAY);
1008        entries[1].last_verified = Some(unix_timestamp());
1009        context.save(&entries).unwrap();
1010
1011        assert_eq!(context.prune_stale(1).unwrap(), 1);
1012        assert_eq!(
1013            context.get_recent(10).unwrap()[0].content,
1014            "current command"
1015        );
1016    }
1017
1018    #[test]
1019    fn get_best_prioritizes_high_quality_facts() {
1020        let context = store("best");
1021        context
1022            .put("cargo test validates changes", "claude", "fact")
1023            .unwrap();
1024        context
1025            .put("cargo test captures regressions", "codex", "fact")
1026            .unwrap();
1027        let mut entries = context.load().unwrap();
1028        entries[0].confidence = 0.4;
1029        entries[0].outcome_correlation = 0.5;
1030        entries[1].confidence = 0.9;
1031        entries[1].outcome_correlation = 0.9;
1032        context.save(&entries).unwrap();
1033
1034        let best = context.get_best("cargo test", 2).unwrap();
1035        assert_eq!(best[0].agent, "codex");
1036    }
1037
1038    #[test]
1039    fn recording_outcomes_updates_correlation_with_ema() {
1040        let context = store("outcomes");
1041        let id = context.put("test outcome", "codex", "fact").unwrap();
1042
1043        context.record_fact_outcome(&id, true).unwrap();
1044        let positive = context
1045            .get_recent(1)
1046            .unwrap()
1047            .pop()
1048            .unwrap()
1049            .outcome_correlation;
1050        context.record_fact_outcome(&id, false).unwrap();
1051        let mixed = context
1052            .get_recent(1)
1053            .unwrap()
1054            .pop()
1055            .unwrap()
1056            .outcome_correlation;
1057
1058        assert!((positive - 0.2).abs() < f32::EPSILON);
1059        assert!(mixed < positive);
1060    }
1061
1062    #[test]
1063    fn legacy_jsonl_entries_receive_provenance_defaults() {
1064        let context = store("legacy");
1065        fs::write(
1066            context.path(),
1067            r#"{"id":"legacy","content":"legacy fact","agent":"cursor","timestamp":7,"category":"fact","confidence":1.0,"access_count":0,"last_accessed":7}"#,
1068        )
1069        .unwrap();
1070
1071        let entry = context.get_recent(1).unwrap().pop().unwrap();
1072        assert_eq!(entry.confidence, 1.0);
1073        assert_eq!(entry.created_by, "cursor");
1074        assert_eq!(entry.last_verified, None);
1075        assert_eq!(entry.verification_count, 0);
1076        assert_eq!(entry.outcome_correlation, 0.0);
1077        assert_eq!(entry.stale_after_days, None);
1078        assert_eq!(entry.superseded_by, None);
1079    }
1080
1081    #[test]
1082    fn superseding_and_negative_pruning_track_quality() {
1083        let context = store("supersede");
1084        let old = context.put("old guidance", "codex", "fact").unwrap();
1085        let new = context.put("new guidance", "codex", "fact").unwrap();
1086        context.supersede(&old, &new).unwrap();
1087        context.record_fact_outcome(&old, false).unwrap();
1088        context.record_fact_outcome(&old, false).unwrap();
1089
1090        let entries = context.get_recent(2).unwrap();
1091        let old_entry = entries
1092            .iter()
1093            .find(|e| e.content == "old guidance")
1094            .unwrap();
1095        assert_eq!(old_entry.superseded_by, Some(new));
1096        assert_eq!(context.prune_negative().unwrap(), 1);
1097    }
1098}