Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(audit): close recent review gaps

AkitaOnRailsc949c795d8d2
diff --git a/companions/ai-memory-wikisync/src/sync.rs b/companions/ai-memory-wikisync/src/sync.rs
index 2b121384..0ac19411 100644
--- a/companions/ai-memory-wikisync/src/sync.rs
+++ b/companions/ai-memory-wikisync/src/sync.rs
@@ -159,7 +159,9 @@ pub fn baseline_for(
state::ScopeCheck::Adopted => {
if !state.pages.is_empty() {
eprintln!(
- "ai-memory-wikisync: this destination's baseline predates origin tracking; adopting it for {}/{}. A baseline from another project cannot be detected, so check the plan before --apply.",
+ "ai-memory-wikisync: this destination's baseline predates origin tracking; \
+ adopting it for {}/{}. A baseline from another project cannot be detected, \
+ so check the plan before --apply.",
scope.workspace, scope.project
);
}
@@ -179,7 +181,8 @@ pub fn baseline_for(
);
}
eprintln!(
- "ai-memory-wikisync: discarding the baseline recorded for {}/{}, adopting this destination for {}/{}",
+ "ai-memory-wikisync: discarding the baseline recorded for {}/{}, adopting this \
+ destination for {}/{}",
recorded.workspace, recorded.project, scope.workspace, scope.project
);
state = state::SyncState {
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 0c9947e5..2810150c 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -6927,7 +6927,7 @@ async fn move_project_core(
.reader
.project_record_counts(src_ws, src_proj)
.await
- .unwrap_or_default();
+ .map_err(|error| internal_err(error.to_string()))?;
return Err((
StatusCode::CONFLICT,
Json(serde_json::json!({
diff --git a/crates/ai-memory-mcp/tests/suite/admin_move.rs b/crates/ai-memory-mcp/tests/suite/admin_move.rs
index 3c822590..c872ee3a 100644
--- a/crates/ai-memory-mcp/tests/suite/admin_move.rs
+++ b/crates/ai-memory-mcp/tests/suite/admin_move.rs
@@ -529,6 +529,40 @@ async fn a_copy_purge_merge_requires_its_own_acknowledgement() {
assert_eq!(resp.status(), StatusCode::OK);
}
+/// The acknowledgement refusal reports the source rows the merge would lose.
+/// If those counts cannot be read, it must fail closed rather than displaying
+/// zeros that could mislead the operator into approving the destructive path.
+#[tokio::test]
+async fn a_copy_purge_preflight_propagates_count_failures() {
+ let tmp = TempDir::new().unwrap();
+ let (state, store) = make_state(&tmp).await;
+ seed_page(&store, &state.wiki, "src", "proj", "notes/a.md", "body a").await;
+ seed_page(&store, &state.wiki, "dst", "proj", "notes/b.md", "body b").await;
+ rusqlite::Connection::open(store.db_path())
+ .unwrap()
+ .execute_batch("ALTER TABLE handoffs RENAME TO unavailable_handoffs")
+ .unwrap();
+
+ let resp = post(
+ state,
+ "/admin/move-project",
+ json!({ "from_workspace": "src", "project": "proj", "to_workspace": "dst", "confirm": true }),
+ )
+ .await;
+
+ assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
+ let body = body_json(resp).await;
+ let error = body["error"].as_str().unwrap_or_default();
+ assert!(error.contains("handoffs"), "{body}");
+ assert!(body.get("discarded").is_none(), "{body}");
+ assert!(body.get("strategy").is_none(), "{body}");
+ assert_eq!(
+ store.reader.list_pages("src", "proj").await.unwrap().len(),
+ 1,
+ "a failed preflight must leave the source untouched"
+ );
+}
+
#[tokio::test]
async fn move_project_merges_into_existing_dest() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 7d5672bb..cb54982f 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -2242,7 +2242,7 @@ pub struct ProjectSummary {
/// operator is about to delete.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct ProjectRecordCounts {
- /// Unxpired latest pages.
+ /// Unexpired latest pages.
pub pages: u64,
/// Sessions recorded in the project.
pub sessions: u64,
@@ -12480,6 +12480,7 @@ impl ReaderPool {
home: Option<&str>,
) -> StoreResult<ContaminationReport> {
let scoped = scope.is_some();
+ let scoped_project = scope.as_ref().map(|(_, project)| *project);
let home = home.map(|h| normalize_cwd(h).into_owned());
let scope_params: Vec<Value> = match &scope {
Some((ws, proj)) => vec![
@@ -12495,7 +12496,9 @@ impl ReaderPool {
// boundary and safety rules as the runtime resolver.
type Candidate = (String, WorkspaceId, ProjectId, String, String, String);
type Prefix = (WorkspaceId, ProjectId, String, String);
- // CHECK B's input: every project in scope with its counts.
+ // CHECK B's input: every project in scope's workspace with its counts.
+ // A project-scoped audit still needs its peers to detect a collision;
+ // only the emitted findings are restricted to the requested project.
type ProjectRow = (String, String, Vec<u8>, String, u64, u64);
let (mut findings, candidates, prefixes, projects): (
Vec<ContaminationFinding>,
@@ -12630,12 +12633,17 @@ impl ReaderPool {
FROM projects p JOIN workspaces wl ON wl.id = p.workspace_id",
);
if scoped {
- p_sql.push_str(" WHERE p.workspace_id = ? AND p.id = ?");
+ p_sql.push_str(" WHERE p.workspace_id = ?");
}
let mut projects: Vec<ProjectRow> = Vec::new();
{
let mut stmt = conn.prepare(&p_sql)?;
- let rows = stmt.query_map(params_from_iter(scope_params.iter()), |row| {
+ let params = if scoped {
+ &scope_params[..1]
+ } else {
+ &scope_params[..0]
+ };
+ let rows = stmt.query_map(params_from_iter(params.iter()), |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
@@ -12696,6 +12704,11 @@ impl ReaderPool {
}
for (_, peers) in groups.iter().filter(|(_, peers)| peers.len() > 1) {
for project in peers {
+ if scoped_project
+ .is_some_and(|selected| project.2.as_slice() != selected.as_bytes())
+ {
+ continue;
+ }
let others: Vec<&str> = peers
.iter()
.filter(|peer| peer.0 != project.0)
diff --git a/crates/ai-memory-store/tests/suite/audit_contamination.rs b/crates/ai-memory-store/tests/suite/audit_contamination.rs
index c9fac437..f39e25ae 100644
--- a/crates/ai-memory-store/tests/suite/audit_contamination.rs
+++ b/crates/ai-memory-store/tests/suite/audit_contamination.rs
@@ -369,8 +369,8 @@ fn project_name_fold_key_matches_spelling_not_meaning() {
assert_eq!(key(" api "), "api");
}
-/// A scoped audit still reports collisions inside the requested project, and
-/// nothing outside it.
+/// A scoped audit compares the requested project with its workspace peers,
+/// then reports only the requested side of a collision.
#[tokio::test]
async fn scoped_audit_limits_collisions_to_the_requested_project() {
let tmp = tempfile::tempdir().unwrap();
@@ -407,7 +407,10 @@ async fn scoped_audit_limits_collisions_to_the_requested_project() {
.await
.unwrap();
assert_eq!(
- scoped.summary.project_name_collisions, 0,
- "a single scoped project cannot collide with itself in the same workspace filter"
+ scoped.summary.project_name_collisions, 1,
+ "the selected project must still be compared with its workspace peers"
);
+ assert_eq!(scoped.findings.len(), 1);
+ assert_eq!(scoped.findings[0].landed_project, "api");
+ assert_eq!(scoped.findings[0].peer_projects.as_deref(), Some("API"));
}