Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(scope): seed the active-project fallback from disk on startup (#678)
rafaelkenedyefefb3c17d80
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fa333692..1ae5eaa7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0now uses the import instead of a prose pointer (#680).### Fixed+- Reads no longer report an empty project after the daemon restarts+ mid-session. The active-project pointer lives in process memory, so a+ restart — the one the packages' own post-upgrade note tells you to run —+ dropped it, and `memory_status`, `memory_briefing`, and every other+ unscoped read then resolved through the baked default scope and answered+ zero counts through the success path, with nothing in the log to+ distinguish "scope unresolved" from "project genuinely empty". `serve` now+ seeds the shared fallback slot at startup from the most recently active+ project recorded in the database, bounded by the same TTL as a per-key+ entry, so a keyed miss right after a restart degrades to real data. Keyed+ per-actor entries are never reconstructed and the first hook event+ publishes over the seed, so pre-publish and eviction reads keep degrading+ exactly as they do today (#678).- The from-source AUR `PKGBUILD` now builds and tests on constrained AURbuilders. Release LTO was disabled (`options=('!debug' '!lto')`) so thefinal link no longer gets OOM-killed on low-memory build hosts, and thediff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex f2b01fed..1da598f6 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -909,6 +909,7 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {service.waiting().await?;}TransportKind::Http => {+ seed_active_project_fallback(&store.reader, &active_project).await;let bind = args.bind.unwrap_or_else(|| config.bind.clone());let cancel = CancellationToken::new();let (session_consolidation_notify, session_consolidation_task) =@@ -2138,6 +2139,58 @@ fn host_allowed(host: &str, allowed_hosts: &[String]) -> bool {})}+/// Seed the single-slot active-project fallback from the most recently active+/// project already on disk.+///+/// The pointer lives only in process memory, so restarting the daemon+/// mid-session drops it. An unscoped read then resolves through the baked+/// default scope and answers zero counts for a project holding thousands of+/// observations — through the SUCCESS path, with nothing in the log, so+/// neither the agent nor the operator can tell it apart from a genuinely+/// empty project (#678). Failing such a read closed is not the fix: a keyed+/// miss is also the normal shape of the pre-publish window (hooks are+/// fire-and-forget) and of TTL/cap eviction, both of which must keep+/// degrading gracefully.+///+/// So make the degraded answer a real one. Only the shared slot is seeded, so+/// a keyed hit still wins and the first hook event publishes straight over it.+/// The recency bound is the pointer's own per-key TTL: activity older than+/// that would have aged out of a live pointer anyway. Non-fatal — a server+/// that cannot read this starts exactly as it does today.+async fn seed_active_project_fallback(reader: &ReaderPool, active_project: &ActiveProject) {+ if active_project.get().is_some() {+ return;+ }+ let ttl_us = i64::try_from(active_project.per_key_ttl().as_micros()).unwrap_or(i64::MAX);+ let since = jiff::Timestamp::now()+ .as_microsecond()+ .saturating_sub(ttl_us);+ match reader.most_recently_active_scope(since).await {+ Ok(Some((workspace_id, project_id))) => {+ active_project.set(workspace_id, project_id);+ let workspace = reader+ .workspace_name_by_id(workspace_id)+ .await+ .ok()+ .flatten();+ let project = reader+ .project_name_by_id(workspace_id, project_id)+ .await+ .ok()+ .flatten();+ info!(+ workspace = workspace.as_deref().unwrap_or("<unknown>"),+ project = project.as_deref().unwrap_or("<unknown>"),+ "seeded active-project fallback from the last recorded activity"+ );+ }+ Ok(None) => {}+ Err(e) => {+ tracing::warn!(error = %e, "active-project fallback seed skipped (non-fatal)");+ }+ }+}+fn host_without_port(host: &str) -> &str {if let Some(rest) = host.strip_prefix('[')&& let Some((inside, _)) = rest.split_once(']')@@ -2933,6 +2986,116 @@ mod tests {}}+ /// #678: the pointer is process memory, so `systemctl restart` mid-session+ /// drops it. An unscoped read then resolved through the baked default scope+ /// and reported zero counts for a project holding thousands of observations,+ /// through the success path. Seeding the shared slot from the last recorded+ /// activity makes that degraded answer a real one.+ #[tokio::test]+ async fn a_restart_seeds_the_active_project_fallback_from_the_last_activity() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let workspace_id = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ // The baked default scope: empty, and where the bug parked every read.+ let scratch = store+ .writer+ .get_or_create_project(workspace_id, "scratch", None)+ .await+ .unwrap();+ let worked_in = store+ .writer+ .get_or_create_project(workspace_id, "real-project", None)+ .await+ .unwrap();+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ id: session_id,+ workspace_id,+ project_id: worked_in,+ agent_kind: AgentKind::ClaudeCode,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ store+ .writer+ .insert_observation(Sanitized::new(+ NewObservation {+ session_id,+ workspace_id,+ project_id: worked_in,+ kind: ObservationKind::UserPrompt,+ extension: None,+ source_event: None,+ title: "work".into(),+ body: "the session that outlived the daemon".into(),+ importance: 5,+ },+ &Sanitizer::default(),+ ))+ .await+ .unwrap();++ // A pointer as empty as it is one instruction after a restart.+ let active_project = ActiveProject::new();+ seed_active_project_fallback(&store.reader, &active_project).await;+ assert_eq!(+ active_project.get(),+ Some((workspace_id, worked_in)),+ "the shared slot must name the project the last activity landed in"+ );++ // The live session's keyed entry died with the process, so its read+ // misses the map — and must now degrade to real data, not to `scratch`.+ let actor = ai_memory_core::ActorKey {+ user: None,+ session_id: Some(session_id.to_string()),+ };+ let resolved = ai_memory_store::ScopeResolver::new(&store.reader, workspace_id, scratch)+ .with_active_project(&active_project)+ .resolve_read_args(None, None, &actor)+ .await+ .unwrap();+ assert_eq!(+ resolved.as_tuple(),+ (workspace_id, worked_in),+ "an unscoped read after a restart must not silently answer for the baked default"+ );+ }++ #[tokio::test]+ async fn seeding_never_overwrites_a_pointer_a_hook_already_published() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let workspace_id = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let published = store+ .writer+ .get_or_create_project(workspace_id, "published", None)+ .await+ .unwrap();++ let active_project = ActiveProject::new();+ active_project.set(workspace_id, published);+ seed_active_project_fallback(&store.reader, &active_project).await;++ assert_eq!(+ active_project.get(),+ Some((workspace_id, published)),+ "a live pointer outranks anything the DB remembers"+ );+ }+#[tokio::test]async fn session_consolidation_worker_consumes_and_completes_durable_job() {let tmp = TempDir::new().unwrap();diff --git a/crates/ai-memory-core/src/active_project.rs b/crates/ai-memory-core/src/active_project.rsindex a8812ac5..10589f1c 100644--- a/crates/ai-memory-core/src/active_project.rs+++ b/crates/ai-memory-core/src/active_project.rs@@ -415,6 +415,18 @@ impl ActiveProject {self.mode}+ /// The effective TTL of a per-key entry, after the zero-means-default+ /// normalization the backing map applies.+ ///+ /// `serve` uses it as the recency bound when it seeds the single-slot+ /// fallback from disk at startup (#678): activity older than this would+ /// have aged out of the live pointer anyway, so it must not come back+ /// through the restart path either.+ #[must_use]+ pub fn per_key_ttl(&self) -> Duration {+ self.per_actor.read().unwrap_or_else(|e| e.into_inner()).ttl+ }+/// Publish the project the agent is currently active in. Called by the/// hook router after it resolves an event's `cwd` to a real project.///diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex 380e44f1..9ce812ff 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -2143,6 +2143,43 @@ impl ReaderPool {.await}+ /// Return the `(workspace_id, project_id)` with the most recent recorded+ /// activity at or after `since_us` (wall clock, microseconds).+ ///+ /// Observations are the complete activity log — every lifecycle hook event+ /// lands as one, stamped with the scope it was routed to — so the newest+ /// row is the project the operator was last working in. Used at startup to+ /// seed the in-memory active-project fallback, which a restart otherwise+ /// drops (#678). The caller supplies the cutoff so a long-idle server does+ /// not resurrect a scope the live pointer would have expired anyway.+ ///+ /// # Errors+ /// Propagates any SQL or pool error.+ pub async fn most_recently_active_scope(+ &self,+ since_us: i64,+ ) -> StoreResult<Option<(WorkspaceId, ProjectId)>> {+ self.with_conn(move |conn| {+ let row = conn+ .query_row(+ "SELECT workspace_id, project_id FROM observations \+ WHERE created_at >= ?1 \+ ORDER BY created_at DESC LIMIT 1",+ params![since_us],+ |row| Ok((row.get::<_, Vec<u8>>(0)?, row.get::<_, Vec<u8>>(1)?)),+ )+ .optional()?;+ match row {+ Some((ws, proj)) => Ok(Some((+ WorkspaceId::from_slice(&ws)?,+ ProjectId::from_slice(&proj)?,+ ))),+ None => Ok(None),+ }+ })+ .await+ }+/// Return the latest completed session for a project.////// Used by read-only review tools that need a natural default when the userdiff --git a/crates/ai-memory-store/tests/suite/mod.rs b/crates/ai-memory-store/tests/suite/mod.rsindex 46df6f92..b90342df 100644--- a/crates/ai-memory-store/tests/suite/mod.rs+++ b/crates/ai-memory-store/tests/suite/mod.rs@@ -9,6 +9,7 @@ mod auto_improve_staging;mod client_activity;mod fts_drift_status;mod handoff_ownership;+mod most_recently_active_scope;mod multi_session;mod session_ids_touching_scope;mod session_observations;diff --git a/crates/ai-memory-store/tests/suite/most_recently_active_scope.rs b/crates/ai-memory-store/tests/suite/most_recently_active_scope.rsnew file mode 100644index 00000000..152e3368--- /dev/null+++ b/crates/ai-memory-store/tests/suite/most_recently_active_scope.rs@@ -0,0 +1,123 @@+//! Integration tests for `most_recently_active_scope`.+//!+//! The active-project pointer lives in process memory, so a daemon restart+//! mid-session drops it and unscoped reads silently answer from the baked+//! default scope (#678). This reader query is what `serve` seeds the shared+//! fallback slot from at startup: the newest observation names the project the+//! operator was actually in, and the caller's cutoff keeps a long-idle server+//! from resurrecting a scope the live pointer would have expired.++use ai_memory_core::{ProjectId, WorkspaceId};+use ai_memory_store::Store;+use rusqlite::{Connection, params};++const NOW: i64 = 1_700_000_000_000_000;++fn id(n: u8) -> [u8; 16] {+ let mut b = [0u8; 16];+ b[15] = n;+ b+}++/// Two projects in one workspace, each with observations: `proj-a` holds the+/// older activity, `proj-b` the newer.+fn seed(db_path: &std::path::Path) {+ let conn = Connection::open(db_path).unwrap();+ let (ws, a, b) = (id(1), id(2), id(3));+ let session = id(10);++ conn.execute(+ "INSERT INTO workspaces (id, name, created_at) VALUES (?1, 'w', ?2)",+ params![&ws[..], NOW],+ )+ .unwrap();+ for (pid, name, rp) in [(&a, "proj-a", "/w/a"), (&b, "proj-b", "/w/b")] {+ conn.execute(+ "INSERT INTO projects (id, workspace_id, name, repo_path, created_at) \+ VALUES (?1, ?2, ?3, ?4, ?5)",+ params![&pid[..], &ws[..], name, rp, NOW],+ )+ .unwrap();+ }+ conn.execute(+ "INSERT INTO sessions (id, workspace_id, project_id, agent_kind, cwd, started_at) \+ VALUES (?1, ?2, ?3, 'claude-code', ?4, ?5)",+ params![&session[..], &ws[..], &a[..], "/w/a", NOW],+ )+ .unwrap();+ // proj-a first, proj-b last: the pointer must follow the newest row.+ for (n, proj, ts) in [+ (20u8, &a, NOW),+ (21, &a, NOW + 1_000),+ (22, &b, NOW + 2_000),+ ] {+ conn.execute(+ "INSERT INTO observations \+ (id, session_id, workspace_id, project_id, kind, title, body, created_at) \+ VALUES (?1, ?2, ?3, ?4, 'note', 't', 'x', ?5)",+ params![&id(n)[..], &session[..], &ws[..], &proj[..], ts],+ )+ .unwrap();+ }+}++#[tokio::test]+async fn resolves_the_scope_of_the_newest_observation() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ seed(store.db_path());++ let ws = WorkspaceId::from_slice(&id(1)).unwrap();+ let proj_b = ProjectId::from_slice(&id(3)).unwrap();++ assert_eq!(+ store+ .reader+ .most_recently_active_scope(NOW - 1)+ .await+ .unwrap(),+ Some((ws, proj_b)),+ "the newest observation names the last active project"+ );+}++#[tokio::test]+async fn activity_older_than_the_cutoff_is_not_resurrected() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ seed(store.db_path());++ // One microsecond past the newest row: a server that was down long enough+ // for the pointer TTL to elapse must start with no fallback at all rather+ // than answer for a project nobody is in any more.+ assert_eq!(+ store+ .reader+ .most_recently_active_scope(NOW + 2_001)+ .await+ .unwrap(),+ None,+ );+ // ...and a cutoff that admits only the newest row still resolves.+ assert_eq!(+ store+ .reader+ .most_recently_active_scope(NOW + 2_000)+ .await+ .unwrap(),+ Some((+ WorkspaceId::from_slice(&id(1)).unwrap(),+ ProjectId::from_slice(&id(3)).unwrap(),+ )),+ );+}++#[tokio::test]+async fn a_store_with_no_activity_has_no_scope_to_seed() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ assert_eq!(+ store.reader.most_recently_active_scope(0).await.unwrap(),+ None,+ );+}diff --git a/docs/auto-scope.md b/docs/auto-scope.mdindex 2cf95dae..dfbb9c26 100644--- a/docs/auto-scope.md+++ b/docs/auto-scope.md@@ -192,6 +192,37 @@ no `users` row) only when the client/bridge forwards the session id onMCP calls. Claude Code has the opt-in bridge above; with other stock static MCPconfigs, use explicit `workspace` + `project` arguments for concurrent windows.+## Surviving a restart++The pointer is process memory: restarting the daemon — which is exactly what+the packages tell you to do after an upgrade — empties it, including for+sessions that are still open. Until the next foreground hook event lands, every+keyed read misses, and an unscoped read used to resolve through the baked+default scope and report an empty project through the success path. Nothing in+the answer or the log said "scope unresolved", so an agent asking "what do we+have here?" was told "nothing" while thousands of observations sat in the DB.++`serve` therefore seeds the **shared slot** at startup from the most recently+active project already recorded in SQLite, so a keyed miss right after a+restart degrades to real data instead of an empty default. Three bounds keep+that narrow:++- Only the shared fallback slot is seeded. Keyed entries are never+ reconstructed, so a keyed hit still wins and per-actor isolation is+ unchanged.+- The seed is bounded by the same TTL as a per-key entry (`session_ttl_secs`,+ default one hour). Activity older than that would have aged out of a live+ pointer, so a server that was down overnight starts with no fallback at all.+- The first foreground hook event publishes straight over it, exactly as it+ overwrites any other shared-slot value.++Reads deliberately do **not** fail closed on an unresolved pointer. A keyed+miss is also the normal shape of the pre-publish window (hooks are+fire-and-forget, so a session's first read can arrive before its `SessionStart`+write lands) and of TTL/cap eviction on a busy server. Erroring there would+turn both into spurious failures; seeding a real fallback fixes the silent+answer without touching them.+## Memory footprintPer-key entries are tiny: two `Uuid`-sized ids + an `Instant`. With