Remove Store::cached(), consolidate on access_local()

- Remove CACHED_STORE, cached(), is_stale(), set_store() - redundant
- Convert all Store::cached() callers to use access_local()
- Single Store::load() call remains in access() fallback path

All store access now goes through hippocampus::access() / access_local(),
which handles socket connection or local fallback with caching.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-13 18:11:58 -04:00
parent 09b30d64f2
commit 1d88293ccf
6 changed files with 17 additions and 51 deletions

View file

@ -1,7 +1,7 @@
// tools/memory.rs — Native memory graph operations
//
// Daemon: calls set_store() at startup for direct store access.
// Clients: lazy init tries socket, falls back to local store.
// Access via hippocampus::access() / access_local(). Clients try socket
// first, fall back to local store.
#![allow(unused_variables)] // macro-generated args for no-param tools
@ -17,7 +17,7 @@ pub use crate::hippocampus::{
journal_tail, journal_new, journal_update,
graph_topology, graph_health, graph_communities, graph_normalize_strengths,
graph_link_impact, graph_hubs, graph_trace,
set_store, socket_path,
socket_path,
};
// ── Macro for generating tool wrappers ─────────────────────────

View file

@ -44,14 +44,9 @@ pub enum StoreAccess {
None(String), // Error: couldn't get access
}
/// Set the global store handle. Call once at daemon startup (eager init).
pub fn set_store(store: Arc<crate::Mutex<Store>>) {
STORE_ACCESS.set(Some(store)).ok();
}
/// Get store access: daemon's store, socket, or local fallback.
pub fn access() -> StoreAccess {
// Daemon: already set via set_store()
// Check if already cached
if let Some(Some(store)) = STORE_ACCESS.get() {
return StoreAccess::Daemon(store.clone());
}

View file

@ -19,36 +19,8 @@ use std::collections::HashMap;
use std::fs;
use std::io::{BufReader, Seek};
use std::path::Path;
use std::sync::Arc;
/// Process-global cached store. Reloads only when log files change.
static CACHED_STORE: tokio::sync::OnceCell<Arc<crate::Mutex<Store>>> =
tokio::sync::OnceCell::const_new();
impl Store {
/// Get or create the process-global cached store.
/// Reloads from disk if log files have changed since last load.
pub async fn cached() -> Result<Arc<crate::Mutex<Store>>> {
let store = CACHED_STORE.get_or_try_init(|| async {
let s = Store::load()?;
Ok::<_, anyhow::Error>(Arc::new(crate::Mutex::new(s)))
}).await?;
{
let mut guard = store.lock().await;
if guard.is_stale() {
*guard = Store::load()?;
}
}
Ok(store.clone())
}
/// Check if the on-disk logs have grown since we loaded.
pub fn is_stale(&self) -> bool {
let nodes_size = fs::metadata(nodes_path()).map(|m| m.len()).unwrap_or(0);
let rels_size = fs::metadata(relations_path()).map(|m| m.len()).unwrap_or(0);
nodes_size != self.loaded_nodes_size || rels_size != self.loaded_rels_size
}
/// Load store from state.bin cache if fresh, otherwise rebuild from capnp logs.
///
/// Staleness check uses log file sizes (not mtimes). Since logs are

View file

@ -389,8 +389,8 @@ impl Mind {
pub async fn subconscious_snapshots(&self) -> Vec<SubconsciousSnapshot> {
// Lock ordering: subconscious → store (store is bottom-most).
let sub = self.subconscious.lock().await;
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
let store_arc = crate::hippocampus::access_local().ok();
let store_guard = match &store_arc {
Some(s) => Some(s.lock().await),
None => None,
};
@ -403,8 +403,8 @@ impl Mind {
pub async fn unconscious_snapshots(&self) -> Vec<UnconsciousSnapshot> {
let unc = self.unconscious.lock().await;
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
let store_arc = crate::hippocampus::access_local().ok();
let store_guard = match &store_arc {
Some(s) => Some(s.lock().await),
None => None,
};

View file

@ -526,8 +526,8 @@ impl Subconscious {
.collect()
};
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
let store_arc = crate::hippocampus::access_local().ok();
let store_guard = match &store_arc {
Some(s) => Some(s.lock().await),
None => None,
};
@ -604,8 +604,8 @@ impl Subconscious {
if to_run.is_empty() { return; }
// Query each agent's recent writes so they know what they already touched
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
let store_arc = crate::hippocampus::access_local().ok();
let store_guard = match &store_arc {
Some(s) => Some(s.lock().await),
None => None,
};

View file

@ -212,10 +212,9 @@ async fn start(cli: crate::user::CliArgs) -> Result<()> {
})
.expect("spawn UI thread");
// Initialize store and set global handle for memory tools
match crate::store::Store::cached().await {
Ok(store) => crate::agent::tools::memory::set_store(store),
Err(e) => eprintln!("Store init failed: {}", e),
// Initialize store - access_local() caches it in STORE_ACCESS
if let Err(e) = crate::hippocampus::access_local() {
eprintln!("Store init failed: {}", e);
}
// Start MCP server for external tool access
@ -420,8 +419,8 @@ async fn run(
unc.toggle(name).await;
}
}
let store = crate::store::Store::cached().await.ok();
let store_guard = match &store {
let store_arc = crate::hippocampus::access_local().ok();
let store_guard = match &store_arc {
Some(s) => Some(s.lock().await),
None => None,
};