Skip to content

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

Rust

fix(admin): surface a failed page read in the pending-write diff

lhzapatae87812d0a175
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fee7fa0b..c52b9594 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,11 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Fixed `GET /admin/pending-writes/{id}/diff` (`ai-memory pending-writes diff`)
+ answering 200 with an empty "before" when reading the target page failed,
+ which made an update proposal look like a new page; the route now returns 500
+ with the store error, and a page that does not exist yet still diffs against
+ an empty body ([#1399](https://github.com/akitaonrails/ai-memory/pull/1399)).
- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint pass
now omits expired pages before evaluating episodic staleness, rule suggestions,
duplicate titles, and vector or LLM contradiction candidates, preventing
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index ead356ea..4cad047a 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -3523,14 +3523,14 @@ async fn handle_pending_write_diff(
match pending_detail(&state, &id, &query).await {
Ok((ws, proj, owned)) => {
let detail = &owned.detail;
- let before = state
+ let before = match state
.reader
.page_body_by_ids(ws, proj, detail.summary.target_path.as_str())
.await
- .ok()
- .flatten()
- .map(|p| p.body)
- .unwrap_or_default();
+ {
+ Ok(page) => page.map(|p| p.body).unwrap_or_default(),
+ Err(e) => return internal_err(e.to_string()),
+ };
let diff = format!(
"--- before/{path}\n+++ after/{path}\n@@\n{before}\n--- proposed ---\n{after}\n",
path = detail.summary.target_path.as_str(),
@@ -12346,6 +12346,60 @@ mod tests {
);
}
+ /// A failed read of the target page must not be rendered as a diff
+ /// against an empty page: that is the same response a proposal for a page
+ /// that does not exist yet gets, so a reviewer would see a create where
+ /// the proposal updates an existing page.
+ #[tokio::test]
+ async fn pending_write_diff_surfaces_a_failed_page_read() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let wiki = Wiki::new(tmp.path(), store.writer.clone())
+ .unwrap()
+ .with_store_reader(store.reader.clone());
+ let (ws, proj, proposal_id) = stage_pending_write(
+ &store,
+ "default",
+ "scratch",
+ "notes/pending.md",
+ "# Pending\n\nproposed body",
+ )
+ .await;
+ let router = admin_router(admin_state_for_store(&tmp, &store, wiki));
+ let request = || {
+ Request::builder()
+ .uri(format!(
+ "/admin/pending-writes/{proposal_id}/diff?workspace=default&project=scratch"
+ ))
+ .body(Body::empty())
+ .unwrap()
+ };
+
+ // Control: an absent target page still diffs against an empty body.
+ let resp = router.clone().oneshot(request()).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ // Break only the page read; the proposal tables stay readable.
+ rusqlite::Connection::open(store.db_path())
+ .unwrap()
+ .execute_batch("ALTER TABLE pages RENAME TO pages_unreadable")
+ .unwrap();
+ assert!(
+ store
+ .reader
+ .page_body_by_ids(ws, proj, "notes/pending.md")
+ .await
+ .is_err(),
+ "fixture must make the page read fail"
+ );
+
+ let resp = router.oneshot(request()).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
+ let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
+ let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert!(body.get("diff").is_none(), "no fabricated diff: {body}");
+ }
+
#[tokio::test]
async fn pending_write_approve_writes_stored_body() {
let tmp = TempDir::new().unwrap();