Skip to main content

codetether_agent/session/index/
cache_async.rs

1//! Async [`SummaryIndex::summary_for`] — cache-miss producer pattern.
2//!
3//! On cache hit, returns immediately. On miss, calls a caller-supplied
4//! async producer (typically RLM summarisation) then stores the result.
5
6use super::cache::SummaryIndex;
7use super::types::{SummaryNode, SummaryRange};
8
9impl SummaryIndex {
10    /// Main read path for derivation policies.
11    ///
12    /// Cache hit: returns immediately. Cache miss: calls `producer`
13    /// which encapsulates RLM summarisation, then caches the result.
14    ///
15    /// # Errors
16    ///
17    /// Returns the producer's error if the RLM call fails.
18    pub async fn summary_for<F, Fut>(
19        &mut self,
20        range: SummaryRange,
21        producer: F,
22    ) -> anyhow::Result<SummaryNode>
23    where
24        F: FnOnce(SummaryRange) -> Fut,
25        Fut: std::future::Future<Output = anyhow::Result<SummaryNode>>,
26    {
27        // Clone first to release the immutable borrow before touch_lru.
28        if let Some(node) = self.tree.get(&range).cloned() {
29            self.touch_lru(range);
30            return Ok(node);
31        }
32        let node = producer(range).await?;
33        self.insert(range, node.clone());
34        Ok(node)
35    }
36}