Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(mcp): answer an unknown pending-write id with 404, not 500
yubr-zzh4d30bea5e5ba
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a8a8f6c9..18de8a42 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -32,6 +32,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0`workdir` points at another project, is no longer judged only by thedestination's policy. (#1199)+- `POST /admin/pending-writes/{id}/approve` and `/reject` now answer+ `404 Not Found` when the id does not exist or belongs to a+ different workspace, matching the `/detail` and `/diff` routes.+ Previously an unknown id surfaced as `500 Internal Server Error`+ because the routes skipped the lookup and let the wiki method+ surface a generic error. Approve and reject now share the same+ `pending_detail` helper the detail and diff routes use, so all+ four routes agree on `400` for an unparsable id, `404` for an+ unknown or foreign-scope id, and `500` only for unexpected+ internal errors. Six new tests in `admin::tests` cover the+ four unknown / foreign / unparsable / happy-path combinations+ for both `approve` and `reject`. (#1205)+## [2.6.3] - 2026-10-09### Fixeddiff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex 3daed612..a7c5d7d3 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -3409,6 +3409,13 @@ async fn handle_pending_write_approve(skip_webhooks,..AdmissionContext::default()});+ // #1205: an unknown or foreign-scope id must answer 404, not 500.+ // Validate up front through the same helper the detail and diff+ // routes already use; a missing id becomes 404 here too, and an+ // unparsable id (handled inside the helper) stays 400.+ if let Err(e) = pending_detail(&state, &id, &query).await {+ return e;+ }match state.wiki.approve_auto_improve_proposal(ws, proj, proposal_id, actor, author_id, admission_ctx)@@ -3443,6 +3450,13 @@ async fn handle_pending_write_reject();}};+ // #1205: an unknown or foreign-scope id must answer 404, not 500.+ // Validate up front through the same helper the detail and diff+ // routes already use; a missing id becomes 404 here too, and an+ // unparsable id (handled inside the helper) stays 400.+ if let Err(e) = pending_detail(&state, &id, &query).await {+ return e;+ }let (ws, proj) = match lookup_ws_proj_no_create(&state, &query.workspace, &query.project).await{Ok(ids) => ids,@@ -12110,6 +12124,7 @@ mod tests {let router = admin_router(admin_state_for_store(&tmp, &store, wiki));let resp = router+ .clone().oneshot(Request::builder().method("POST")@@ -12135,6 +12150,275 @@ mod tests {assert_eq!(page.body, "# Stored\n\nfrom stored proposal");}+ /// #1205: a `POST /admin/pending-writes/{id}/approve` whose `id` does+ /// not exist in the requested workspace (or belongs to a different+ /// one) must answer `404 Not Found`, not `500 Internal Server Error`.+ /// The detail and diff routes already do this through `pending_detail`+ /// -- they look the proposal up before reading it. Approve and reject+ /// skipped the lookup and let the underlying wiki method surface a+ /// 500-shaped error, which hid a not-found case from the caller and+ /// could not be distinguished from a real server fault. The fix is to+ /// thread the same `pending_detail` helper through approve/reject so+ /// the three routes agree on `400` for an unparsable id, `404` for+ /// an unknown or foreign-scope id, and `500` for any other error.+ #[tokio::test]+ async fn pending_write_approve_unknown_id_returns_404() {+ 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());+ // No `stage_pending_write` call: the DB is empty, the id is+ // brand-new. Approve must surface it as a 404, not a 500.+ let router = admin_router(admin_state_for_store(&tmp, &store, wiki));+ let unknown_id = AutoImproveProposalId::new();++ let resp = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(format!(+ "/admin/pending-writes/{unknown_id}/approve?workspace=default&project=scratch"+ ))+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::NOT_FOUND,+ "approve of an unknown id must be 404, not 500 (regression for #1205): {}",+ resp.status(),+ );+ }++ /// #1205: same expectation as approve, for the reject route.+ #[tokio::test]+ async fn pending_write_reject_unknown_id_returns_404() {+ 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 router = admin_router(admin_state_for_store(&tmp, &store, wiki));+ let unknown_id = AutoImproveProposalId::new();++ let resp = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(format!(+ "/admin/pending-writes/{unknown_id}/reject?workspace=default&project=scratch"+ ))+ .header("content-type", "application/json")+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::from(+ serde_json::to_vec(&serde_json::json!({"reason":"not now"})).unwrap(),+ ))+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::NOT_FOUND,+ "reject of an unknown id must be 404, not 500 (regression for #1205): {}",+ resp.status(),+ );+ }++ /// #1205: an id that exists in another workspace must still be 404+ /// from a request scoped to a different workspace, so a caller cannot+ /// use approve/reject as a cross-scope side door.+ #[tokio::test]+ async fn pending_write_approve_foreign_scope_returns_404() {+ 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());+ // Stage the write in workspace `alpha`.+ let (_ws_alpha, _proj_alpha, proposal_id) = stage_pending_write(+ &store,+ "alpha",+ "scratch",+ "notes/foreign.md",+ "# Foreign+",+ )+ .await;+ let router = admin_router(admin_state_for_store(&tmp, &store, wiki));++ // Try to approve it as if it lived in `default`. The lookup must+ // say "not found in scope" -- 404, not 200.+ let resp = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(format!(+ "/admin/pending-writes/{proposal_id}/approve?workspace=default&project=scratch"+ ))+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::NOT_FOUND,+ "approve of a foreign-scope id must be 404, not 500 (regression for #1205): {}",+ resp.status(),+ );+ }++ /// #1205: same expectation as approve, for the reject route.+ #[tokio::test]+ async fn pending_write_reject_foreign_scope_returns_404() {+ 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_alpha, _proj_alpha, proposal_id) = stage_pending_write(+ &store,+ "alpha",+ "scratch",+ "notes/foreign.md",+ "# Foreign\n",+ )+ .await;+ let router = admin_router(admin_state_for_store(&tmp, &store, wiki));++ let resp = router+ .clone()+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(format!(+ "/admin/pending-writes/{proposal_id}/reject?workspace=alpha&project=scratch"+ ))+ .header("content-type", "application/json")+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::from(+ serde_json::to_vec(&serde_json::json!({"reason":"not now"})).unwrap(),+ ))+ .unwrap(),+ )+ .await+ .unwrap();+ // Note: a reject that targets the *real* workspace must be 404 too+ // for symmetry (no reject of a foreign id via the foreign scope+ // either). This exercises the same lookup path from the other side.+ assert_eq!(+ resp.status(),+ StatusCode::OK,+ "reject of an in-scope id must still succeed (regression guard)",+ );++ // And the foreign-scope request returns 404.+ let resp = router+ .clone()+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(format!(+ "/admin/pending-writes/{proposal_id}/reject?workspace=default&project=scratch"+ ))+ .header("content-type", "application/json")+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::from(+ serde_json::to_vec(&serde_json::json!({"reason":"not now"})).unwrap(),+ ))+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::NOT_FOUND,+ "reject of a foreign-scope id must be 404, not 500 (regression for #1205): {}",+ resp.status(),+ );+ }++ /// #1205: an unparsable id stays at 400 (the existing behaviour+ /// before this fix). Approve and reject must not regress to 500.+ #[tokio::test]+ async fn pending_write_approve_unparsable_id_returns_400() {+ 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 router = admin_router(admin_state_for_store(&tmp, &store, wiki));++ let resp = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri("/admin/pending-writes/not-a-uuid/approve?workspace=default&project=scratch")+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::BAD_REQUEST);+ }++ /// #1205: same for the reject route.+ #[tokio::test]+ async fn pending_write_reject_unparsable_id_returns_400() {+ 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 router = admin_router(admin_state_for_store(&tmp, &store, wiki));++ let resp = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri(+ "/admin/pending-writes/not-a-uuid/reject?workspace=default&project=scratch",+ )+ .header("content-type", "application/json")+ .extension(ai_memory_core::ActorContext {+ user: Some("reviewer".into()),+ ..ai_memory_core::ActorContext::default()+ })+ .body(Body::from(+ serde_json::to_vec(&serde_json::json!({"reason":"not now"})).unwrap(),+ ))+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::BAD_REQUEST);+ }+#[tokio::test]async fn read_page_missing_path_returns_404() {let (_tmp, router) = read_page_test_router();