Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(auto-improve): exclude sessions/ from the reviewer's recent-page context (#834)
AkitaOnRails2b04c00e7aa1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2583dba2..503ef368 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -32,6 +32,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0model ids, and which model goes through the Responses endpoint (#763).### Fixed+- The auto-improve reviewer now excludes `sessions/` pages from its own+ recent-page context so those slots go to durable pages (`decisions/`,+ `gotchas/`, `_rules/`, …) it might otherwise re-propose. Session pages are+ never valid proposal targets and previously dominated the recency-ordered+ list, crowding durable knowledge out of the reviewer's view. The exclusion is+ scoped to the reviewer only — the SessionStart briefing and `memory_briefing`+ still include session pages. `docs/auto-improvement-loop.md` now documents+ that only `_rules/`/`procedures/` page bodies reach the reviewer and that the+ recent-page list is recency-ordered, with configurable patchable prefixes and+ embedding-nearest dedup noted as deferred future work. (#834)- Auto-improve proposal staging no longer discards an entire run when oneproposal is a create/update misclassification. A `Create` whose target pagealready exists, or an `Update`/patch whose target is missing, previouslydiff --git a/crates/ai-memory-consolidate/src/auto_improve.rs b/crates/ai-memory-consolidate/src/auto_improve.rsindex 166b824e..fee6ce1b 100644--- a/crates/ai-memory-consolidate/src/auto_improve.rs+++ b/crates/ai-memory-consolidate/src/auto_improve.rs@@ -476,6 +476,20 @@ pub async fn run_auto_improve_review(false,).await?;+ // The reviewer's recent-page context exists to surface durable knowledge+ // (`decisions/`, `gotchas/`, `_rules/`, …) so it is not re-proposed. The+ // shared briefing is recency-ordered and dominated by `sessions/` pages,+ // which the reviewer must never target (session pages come from session-end+ // consolidation). Drop them here — auto-improve only — so those slots go to+ // durable pages. This does NOT touch the shared `briefing_for_project`+ // reader, so the SessionStart briefing and `memory_briefing`, where session+ // pages legitimately belong, are unaffected.+ let reviewer_recent_pages: Vec<_> = briefing+ .recent_pages+ .iter()+ .filter(|page| !page.path.starts_with("sessions/"))+ .cloned()+ .collect();let session_page_path = format!("sessions/{session_id}.md");let session_page = reader.page_body_by_ids(workspace_id, project_id, &session_page_path)@@ -484,7 +498,7 @@ pub async fn run_auto_improve_review(reader,workspace_id,project_id,- &briefing.recent_pages,+ &reviewer_recent_pages,&cfg,).await?;@@ -494,7 +508,7 @@ pub async fn run_auto_improve_review(&observations,duration,session_page.as_ref(),- &briefing.recent_pages,+ &reviewer_recent_pages,&patchable_pages,&rejection_context,&cfg,@@ -505,7 +519,7 @@ pub async fn run_auto_improve_review(.cloned().collect();let existing_index =- ExistingPageIndex::from_pages(&briefing.recent_pages, &prompt_patchable_pages);+ ExistingPageIndex::from_pages(&reviewer_recent_pages, &prompt_patchable_pages);let estimated_input_tokens = estimate_tokens(&prompt_input.prompt);let request = ChatRequest {system: Some(AUTO_IMPROVE_SYSTEM_PROMPT.to_string()),@@ -1921,6 +1935,47 @@ mod tests {}}+ /// Records the reviewer prompt so a test can assert what context it saw,+ /// then returns an empty proposal set (the prompt is the subject under test).+ #[derive(Clone, Default)]+ struct CapturingLlm {+ prompt: std::sync::Arc<std::sync::Mutex<Option<String>>>,+ }++ #[async_trait::async_trait]+ impl LlmProvider for CapturingLlm {+ fn name(&self) -> &'static str {+ "fake"+ }++ fn model(&self) -> &str {+ "fake-model"+ }++ async fn complete(&self, _request: ChatRequest) -> LlmResult<ChatResponse> {+ Ok(ChatResponse {+ text: "unused".into(),+ usage: None,+ model: "fake-model".into(),+ })+ }++ async fn complete_structured_raw(+ &self,+ request: ChatRequest,+ _schema: serde_json::Value,+ ) -> LlmResult<serde_json::Value> {+ if let Some(message) = request.messages.first() {+ *self.prompt.lock().unwrap() = Some(message.content.clone());+ }+ Ok(serde_json::json!({+ "summary": "no durable lesson",+ "proposals": [],+ "rejected_candidates": []+ }))+ }+ }+fn cfg() -> AutoImproveReviewConfig {AutoImproveReviewConfig {min_observations: 3,@@ -2329,6 +2384,120 @@ mod tests {assert!(report.rejected_candidates.is_empty());}+ /// The reviewer's recent-page context must surface durable pages and drop+ /// `sessions/` pages, so those slots go to knowledge the reviewer might+ /// re-propose (#834). Both pages are seeded at the same tier, so exclusion+ /// is by path family, not by tier.+ #[tokio::test]+ async fn reviewer_recent_context_excludes_session_pages() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "proj", None)+ .await+ .unwrap();++ let seed_page = |path: &str| ai_memory_core::NewPage {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new(path).unwrap(),+ title: path.to_string(),+ body: format!("# {path}\n\nbody"),+ tier: ai_memory_core::Tier::Semantic,+ frontmatter_json: serde_json::json!({}),+ pinned: false,+ links: Vec::new(),+ author_id: None,+ expires_at: None,+ entities: Vec::new(),+ evidence: Vec::new(),+ };+ store+ .writer+ .upsert_page(seed_page("decisions/durable.md"))+ .await+ .unwrap();+ store+ .writer+ .upsert_page(seed_page("sessions/old-session.md"))+ .await+ .unwrap();++ let session_id = ai_memory_core::SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ id: session_id,+ workspace_id: ws,+ project_id: proj,+ agent_kind: AgentKind::Other,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ for i in 0..3 {+ store+ .writer+ .insert_observation(Sanitized::new(+ NewObservation {+ session_id,+ workspace_id: ws,+ project_id: proj,+ kind: if i == 0 {+ ObservationKind::SessionStart+ } else {+ ObservationKind::UserPrompt+ },+ extension: None,+ source_event: None,+ title: format!("event {i}"),+ body: "run the full gate before release".into(),+ importance: 5,+ },+ &Sanitizer::builtin(),+ ))+ .await+ .unwrap();+ }++ let llm = CapturingLlm::default();+ run_auto_improve_review(+ &store.reader,+ &llm,+ ws,+ proj,+ session_id,+ AutoImproveReviewConfig {+ min_session_duration_secs: 0,+ ..cfg()+ },+ )+ .await+ .unwrap();++ let prompt = llm+ .prompt+ .lock()+ .unwrap()+ .clone()+ .expect("the reviewer must have called the LLM");+ assert!(+ prompt.contains("decisions/durable.md"),+ "the durable decisions page must reach the reviewer's recent context"+ );+ assert!(+ !prompt.contains("sessions/old-session.md"),+ "session pages must be excluded from the reviewer's recent context"+ );+ }+#[test]fn validation_accepts_procedure_pages() {let raw = AutoImproveLlmResponse {diff --git a/docs/auto-improvement-loop.md b/docs/auto-improvement-loop.mdindex 1255c3c7..5abd1fc6 100644--- a/docs/auto-improvement-loop.md+++ b/docs/auto-improvement-loop.md@@ -498,6 +498,40 @@ unsupported paths, low confidence, oversized bodies, duplicate existing paths ortitles, and normalizes a missing H1 by prepending the proposal title before finalvalidation.+#### Reviewer context is bounded (and how it can miss existing durable pages)++The reviewer does not see every page. Two limits shape what durable knowledge it+can notice already exists, and therefore what it may redundantly re-propose:++- **Only `_rules/` and `procedures/` page *bodies* are loaded** for patch+ context (`load_patchable_pages`). Durable pages in other families+ (`decisions/`, `gotchas/`, `concepts/`, `notes/`) are not sent to the reviewer+ in full.+- **The "recent pages" list is recency-ordered, one line per page** (path,+ title, kind, updated_at), drawn from the project briefing. Because it is a flat+ bounded list, a project with many pages only shows the reviewer the most+ recently updated ones. To keep those slots for durable knowledge, the+ auto-improve reviewer excludes `sessions/` pages from *its* recent-page context+ (session pages are never valid proposal targets and would otherwise dominate+ the list). This exclusion is scoped to the reviewer only — the SessionStart+ briefing and `memory_briefing` still include session pages, where they belong.++The practical consequence: a durable page that lives outside `_rules/` /+`procedures/` and is not among the most-recent pages may be invisible to the+reviewer, so the same lesson can be proposed again. Reviewer duplicates are+caught downstream (validation rejects a proposal whose path/title duplicates an+existing page it *can* see, and staging enforces one pending proposal per+target), but a proposal against a durable page the reviewer never saw is not+prevented at review time.++Deferred improvements (future work, not in this line):++- **Configurable patchable prefixes**, so `decisions/`/`gotchas/` bodies can+ reach the reviewer without hard-coding the family list.+- **Embedding-nearest dedup**, replacing the recency-ordered flat list with a+ retrieval of the pages semantically closest to the session under review, so+ relevant durable pages are surfaced regardless of recency.+Tests:1. Empty/no-activity sessions produce no proposals.