Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(auto-improve): skip misclassified proposals instead of failing the run (#832)
AkitaOnRailsbe7d6c75711e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 62074689..2583dba2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -32,6 +32,15 @@ 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+- Auto-improve proposal staging no longer discards an entire run when one+ proposal is a create/update misclassification. A `Create` whose target page+ already exists, or an `Update`/patch whose target is missing, previously+ aborted the staging transaction, dropping every sibling proposal and the run+ row over one probabilistic LLM mislabel. Those two cases now skip just the+ offending proposal (reported as `skipped`, like a pending-target collision)+ and keep the rest of the run. Two proposals in one run targeting the same+ path remain a hard error, and a create-on-existing is never coerced to an+ update (the page could be pinned). (#832)- The Windows Docker wrapper (`bin/ai-memory.ps1`) now forwards the sameprovider credentials and host-config env vars as the POSIX wrapper into thehelper container. A host-exported `GEMINI_API_KEY` / `GOOGLE_API_KEY`,diff --git a/crates/ai-memory-store/src/auto_improve.rs b/crates/ai-memory-store/src/auto_improve.rsindex 923d1b8c..3a42c58c 100644--- a/crates/ai-memory-store/src/auto_improve.rs+++ b/crates/ai-memory-store/src/auto_improve.rs@@ -743,10 +743,17 @@ fn stage_run_impl() = match (proposal.operation, target_snapshot) {(AutoImproveProposalOperation::Create, None) => (None, None, None),(AutoImproveProposalOperation::Create, Some(_)) => {- return Err(StoreError::InvalidState(format!(- "create proposal target already exists: {}",- proposal.target_path- )));+ // A create/update misclassification is an ordinary LLM error, not+ // corrupt state. Skip just this proposal so the rest of the run+ // still stages and a run row is recorded, rather than discarding+ // every sibling. Do NOT coerce Create->Update: the existing page+ // can be pinned, and auto-applying would rewrite it (Safety+ // Invariant #10). Skip is the conservative fix.+ skipped.push(SkippedProposal {+ target_path: proposal.target_path.to_string(),+ reason: "create proposal target already exists".into(),+ });+ continue;}(AutoImproveProposalOperation::Update, Some(snapshot)) => (Some(snapshot.page_id),@@ -754,10 +761,13 @@ fn stage_run_impl(Some(snapshot.updated_at),),(AutoImproveProposalOperation::Update, None) => {- return Err(StoreError::InvalidState(format!(- "update proposal target does not exist: {}",- proposal.target_path- )));+ // Symmetric misclassification: an update aimed at a page that does+ // not exist. Skip this proposal, keep the run and its siblings.+ skipped.push(SkippedProposal {+ target_path: proposal.target_path.to_string(),+ reason: "update proposal target does not exist".into(),+ });+ continue;}};if edit_mode == "patch" {diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rsindex 9ba87d21..718d4c2c 100644--- a/crates/ai-memory-store/src/lib.rs+++ b/crates/ai-memory-store/src/lib.rs@@ -795,20 +795,26 @@ mod tests {assert_eq!(update.target_body_sha256_at_stage, Some(latest_hash));assert_eq!(update.target_updated_at_at_stage, Some(latest_updated));+ // A Create whose target already exists is a create/update+ // misclassification (ordinary LLM error), not corrupt state: it is+ // skipped, not fatal, so the run still records. See+ // `a_create_on_an_existing_page_is_skipped_not_fatal`.+ let misclassified = store+ .writer+ .stage_auto_improve_run(stage_input(+ ws,+ proj,+ vec![proposal(+ "notes/update.md",+ AutoImproveProposalOperation::Create,+ "bad",+ )],+ ))+ .await+ .unwrap();assert!(- store- .writer- .stage_auto_improve_run(stage_input(- ws,- proj,- vec![proposal(- "notes/update.md",- AutoImproveProposalOperation::Create,- "bad"- )],- ))- .await- .is_err()+ misclassified.proposal_ids.is_empty(),+ "the misclassified create is skipped, not staged");let out_of_scope_session = SessionId::new();diff --git a/crates/ai-memory-store/tests/suite/auto_improve_staging.rs b/crates/ai-memory-store/tests/suite/auto_improve_staging.rsindex a336921c..8be4e62e 100644--- a/crates/ai-memory-store/tests/suite/auto_improve_staging.rs+++ b/crates/ai-memory-store/tests/suite/auto_improve_staging.rs@@ -6,7 +6,7 @@//! that produced them were all lost. It fires with a single operator, through//! the path the prompt itself recommends.-use ai_memory_core::{ActorContext, IdentityKey, PagePath, ProjectId, WorkspaceId};+use ai_memory_core::{ActorContext, IdentityKey, NewPage, PagePath, ProjectId, Tier, WorkspaceId};use ai_memory_store::{AutoImproveProposalOperation, NewAutoImproveProposal, StageAutoImproveRun, Store,};@@ -28,6 +28,35 @@ fn proposal(path: &str, title: &str) -> NewAutoImproveProposal {}}+fn update_proposal(path: &str, title: &str) -> NewAutoImproveProposal {+ NewAutoImproveProposal {+ operation: AutoImproveProposalOperation::Update,+ ..proposal(path, title)+ }+}++async fn seed_page(store: &Store, ws: WorkspaceId, proj: ProjectId, path: &str) {+ store+ .writer+ .upsert_page(NewPage {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new(path).unwrap(),+ title: path.to_string(),+ body: format!("# {path}\n\nexisting body"),+ tier: Tier::Semantic,+ frontmatter_json: serde_json::json!({}),+ pinned: false,+ links: Vec::new(),+ author_id: None,+ expires_at: None,+ entities: Vec::new(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();+}+fn run(ws: WorkspaceId,proj: ProjectId,@@ -378,3 +407,110 @@ async fn an_unrelated_unique_failure_is_not_reported_as_a_pending_collision() {"the real constraint must reach the caller: {error}");}++/// A create/update misclassification is ordinary LLM error, not corrupt state.+///+/// A `Create` proposal whose target already exists (or an `Update` whose target+/// is missing) used to `return Err` inside the staging transaction, discarding+/// the whole run — every sibling proposal and the run row — over one probabilistic+/// mislabel. Skip just the misclassified proposal, keep the rest, and report it,+/// exactly like a pending-target collision. Never coerce Create->Update: the+/// existing page can be pinned, and auto-applying would rewrite it.+#[tokio::test]+async fn a_create_on_an_existing_page_is_skipped_not_fatal() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = scope(&store).await;++ // A real, published page — not a pending proposal — at the collided path.+ seed_page(&store, ws, proj, "_rules/existing.md").await;++ let report = store+ .writer+ .stage_auto_improve_run_for_owner(+ run(+ ws,+ proj,+ vec![+ proposal("_rules/existing.md", "Collides with a live page"),+ proposal("_rules/fresh.md", "Perfectly fine"),+ ],+ ),+ None,+ )+ .await+ .unwrap();++ assert_eq!(+ report.proposal_ids.len(),+ 1,+ "the non-colliding proposal must still be staged (a run row exists)"+ );+ assert_eq!(+ report.skipped.len(),+ 1,+ "the misclassified create is reported"+ );+ assert_eq!(report.skipped[0].target_path, "_rules/existing.md");+ assert_eq!(+ report.skipped[0].reason,+ "create proposal target already exists"+ );++ let staged = store+ .reader+ .list_auto_improve_proposals(ws, proj, None, 10)+ .await+ .unwrap();+ assert_eq!(staged.len(), 1, "only the fresh proposal is persisted");+ assert_eq!(staged[0].target_path.as_str(), "_rules/fresh.md");+}++/// The symmetric misclassification: an `Update` aimed at a page that does not+/// exist must skip that proposal and keep the run and its siblings.+#[tokio::test]+async fn an_update_on_a_missing_page_is_skipped_not_fatal() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = scope(&store).await;++ let report = store+ .writer+ .stage_auto_improve_run_for_owner(+ run(+ ws,+ proj,+ vec![+ update_proposal("_rules/nonexistent.md", "Update of nothing"),+ proposal("_rules/fresh.md", "Perfectly fine"),+ ],+ ),+ None,+ )+ .await+ .unwrap();++ assert_eq!(+ report.proposal_ids.len(),+ 1,+ "the non-colliding proposal must still be staged (a run row exists)"+ );+ assert_eq!(+ report.skipped.len(),+ 1,+ "the misclassified update is reported"+ );+ assert_eq!(report.skipped[0].target_path, "_rules/nonexistent.md");+ assert_eq!(+ report.skipped[0].reason,+ "update proposal target does not exist"+ );++ let staged = store+ .reader+ .list_auto_improve_proposals(ws, proj, None, 10)+ .await+ .unwrap();+ assert_eq!(staged.len(), 1, "only the fresh proposal is persisted");+ assert_eq!(staged[0].target_path.as_str(), "_rules/fresh.md");+}