AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(console-api): fail closed on view delete and auth upserts
Sebastien Tardif10d8c2d9e606
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fa7f3a253..ebdcfadfb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -94,6 +94,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- **Console delete/upsert no longer reports success after a refused write.**+ `DELETE /_xerj-console/api/v1/views/{id}` swallowed `delete_document`+ errors and returned `204` while the view stayed on disk (the same class+ as the passkey revoke fix). Auth and prefs writes used delete-then-create,+ so a replacement the engine rejected (write block, unparseable date)+ dropped the only live user, session, token, or prefs row. Deletes now+ propagate the engine error; upserts use a single `index_document`.+- **`sort` on an ES meta-field other than `_score` / `_doc` / `_id` resolved to`null` on every hit** ([#401](https://github.com/xerj-org/xerj/issues/401)).`compute_sort_values` special-cased exactly those three keys and sentdiff --git a/engine/crates/xerj-console-api/src/auth/store.rs b/engine/crates/xerj-console-api/src/auth/store.rsindex 2ad73c3af..c0a6a0427 100644--- a/engine/crates/xerj-console-api/src/auth/store.rs+++ b/engine/crates/xerj-console-api/src/auth/store.rs@@ -18,6 +18,14 @@ use xerj_engine::Engine;use crate::error::{ConsoleApiError, ConsoleResult};use crate::indices;+/// Single in-place upsert (one WAL append). delete-then-create used to+/// drop the live row when the replacement write was refused (write+/// block, unparseable date, crash between the two ops).+async fn replace_document(idx: &xerj_engine::Index, id: String, doc: Value) -> ConsoleResult<()> {+ idx.index_document(Some(id), doc).await?;+ Ok(())+}+// ─────────────────────────────────────────────────────────────────────────────// User// ─────────────────────────────────────────────────────────────────────────────@@ -74,12 +82,7 @@ pub async fn find_user_by_email(engine: &Engine, email: &str) -> ConsoleResult<Opub async fn upsert_user(engine: &Engine, user: &User) -> ConsoleResult<()> {let idx = engine.get_index(indices::USERS)?;let doc = serde_json::to_value(user)?;- // delete-then-create gives us upsert semantics that work for both- // first-write and re-write. Engine's `index_document` would also- // work but we want to be explicit about idempotency on retries.- let _ = idx.delete_document(&user.id).await;- idx.create_document(user.id.clone(), doc).await?;- Ok(())+ replace_document(&idx, user.id.clone(), doc).await}pub async fn count_active_users(engine: &Engine) -> ConsoleResult<u64> {@@ -144,17 +147,13 @@ pub async fn mark_magic_link_used(.ok_or_else(|| ConsoleApiError::NotFound("magic link not found at consume time".into()))?;link.used_at = Some(used_at_iso.to_string());let doc = serde_json::to_value(&link)?;- let _ = idx.delete_document(token_hash).await;- idx.create_document(token_hash.to_string(), doc).await?;- Ok(())+ replace_document(&idx, token_hash.to_string(), doc).await}pub async fn put_magic_link(engine: &Engine, link: &MagicLink) -> ConsoleResult<()> {let idx = engine.get_index(indices::MAGIC_LINKS)?;let doc = serde_json::to_value(link)?;- let _ = idx.delete_document(&link.id).await;- idx.create_document(link.id.clone(), doc).await?;- Ok(())+ replace_document(&idx, link.id.clone(), doc).await}// ─────────────────────────────────────────────────────────────────────────────@@ -189,9 +188,7 @@ pub async fn list_passkeys_for_user(pub async fn put_passkey(engine: &Engine, pk: &StoredPasskey) -> ConsoleResult<()> {let idx = engine.get_index(indices::PASSKEYS)?;let doc = serde_json::to_value(pk)?;- let _ = idx.delete_document(&pk.id).await;- idx.create_document(pk.id.clone(), doc).await?;- Ok(())+ replace_document(&idx, pk.id.clone(), doc).await}pub async fn count_passkeys_for_user(engine: &Engine, user_id: &str) -> ConsoleResult<u64> {@@ -239,9 +236,7 @@ pub struct Session {pub async fn put_session(engine: &Engine, sess: &Session) -> ConsoleResult<()> {let idx = engine.get_index(indices::SESSIONS)?;let doc = serde_json::to_value(sess)?;- let _ = idx.delete_document(&sess.id).await;- idx.create_document(sess.id.clone(), doc).await?;- Ok(())+ replace_document(&idx, sess.id.clone(), doc).await}pub async fn get_session(engine: &Engine, session_id: &str) -> ConsoleResult<Option<Session>> {@@ -284,9 +279,7 @@ pub struct ApiToken {pub async fn put_api_token(engine: &Engine, token: &ApiToken) -> ConsoleResult<()> {let idx = engine.get_index(indices::API_TOKENS)?;let doc = serde_json::to_value(token)?;- let _ = idx.delete_document(&token.id).await;- idx.create_document(token.id.clone(), doc).await?;- Ok(())+ replace_document(&idx, token.id.clone(), doc).await}pub async fn get_api_token(engine: &Engine, token_hash: &str) -> ConsoleResult<Option<ApiToken>> {diff --git a/engine/crates/xerj-console-api/src/data_sources.rs b/engine/crates/xerj-console-api/src/data_sources.rsindex 17df26e38..62c4009e3 100644--- a/engine/crates/xerj-console-api/src/data_sources.rs+++ b/engine/crates/xerj-console-api/src/data_sources.rs@@ -318,8 +318,7 @@ async fn ensure_builtin(state: &ConsoleState) -> ConsoleResult<()> {created_by: "system".into(),etag: "1".into(),};- let _ = idx.delete_document(BUILTIN_ID).await;- idx.create_document(BUILTIN_ID.into(), serde_json::to_value(&conn)?)+ idx.index_document(Some(BUILTIN_ID.into()), serde_json::to_value(&conn)?).await?;Ok(())}diff --git a/engine/crates/xerj-console-api/src/prefs.rs b/engine/crates/xerj-console-api/src/prefs.rsindex c353d4c23..29c66a014 100644--- a/engine/crates/xerj-console-api/src/prefs.rs+++ b/engine/crates/xerj-console-api/src/prefs.rs@@ -55,8 +55,9 @@ pub async fn put(.insert("updated_at".into(), Value::String(now_iso()));let idx = state.engine.get_index(indices::PREFS)?;- let _ = idx.delete_document(&sess.user.id).await;- idx.create_document(sess.user.id.clone(), body.clone())+ // One in-place upsert. delete-then-create used to drop the live prefs+ // row when the replacement write was refused.+ idx.index_document(Some(sess.user.id.clone()), body.clone()).await?;Ok(ok(body, None))}diff --git a/engine/crates/xerj-console-api/src/views.rs b/engine/crates/xerj-console-api/src/views.rsindex 5cd31bfd2..cf36959b2 100644--- a/engine/crates/xerj-console-api/src/views.rs+++ b/engine/crates/xerj-console-api/src/views.rs@@ -120,7 +120,10 @@ pub async fn delete(return Err(ConsoleApiError::NotFound("view".into()));}let idx = state.engine.get_index(indices::VIEWS)?;- let _ = idx.delete_document(&id).await;+ // Same contract as `store::delete_passkey`: `Ok(false)` is already-absent+ // (idempotent), so only a refused write becomes an error. Swallowing+ // that error used to return 204 while the view stayed on disk.+ idx.delete_document(&id).await?;Ok(no_content())}diff --git a/engine/crates/xerj-console-api/tests/phase3_user_state.rs b/engine/crates/xerj-console-api/tests/phase3_user_state.rsindex 16c64ddea..0439eca81 100644--- a/engine/crates/xerj-console-api/tests/phase3_user_state.rs+++ b/engine/crates/xerj-console-api/tests/phase3_user_state.rs@@ -450,6 +450,117 @@ async fn views_round_trip() {assert_eq!(r.status(), 204);}+/// A write-block on `.xerj_views` must not produce `204 No Content`.+/// `delete_document` returns `Err` when the index refuses writes; swallowing+/// that error told the SPA the view was gone while it stayed searchable.+#[tokio::test]+async fn views_delete_on_write_block_is_not_204() {+ let app = boot_with_session().await;++ let r = app+ .router+ .clone()+ .oneshot(req(+ "POST",+ "/_xerj-console/api/v1/dashboards",+ &app.cookie,+ Some(json!({ "name": "D", "visibility": "private" })),+ ))+ .await+ .unwrap();+ let (_, body) = body_json(r).await;+ let dash_id = body["data"]["id"].as_str().unwrap().to_string();++ let r = app+ .router+ .clone()+ .oneshot(req(+ "POST",+ "/_xerj-console/api/v1/views",+ &app.cookie,+ Some(json!({+ "dashboard_id": dash_id, "name": "Keep me",+ "time": { "from": "now-24h", "to": "now" }+ })),+ ))+ .await+ .unwrap();+ let (status, body) = body_json(r).await;+ assert_eq!(status, 201, "{body}");+ let view_id = body["data"]["id"].as_str().unwrap().to_string();++ app.engine+ .get_index(xerj_console_api::indices::VIEWS)+ .expect("views index")+ .set_block("write")+ .await+ .unwrap();++ let r = app+ .router+ .clone()+ .oneshot(req(+ "DELETE",+ &format!("/_xerj-console/api/v1/views/{view_id}"),+ &app.cookie,+ None,+ ))+ .await+ .unwrap();+ let (status, body) = body_json(r).await;+ assert_ne!(+ status, 204,+ "write-blocked delete must not report success: {body}"+ );+ assert_eq!(status, 500, "engine write-block maps to internal: {body}");++ let r = app+ .router+ .oneshot(req(+ "GET",+ &format!("/_xerj-console/api/v1/views/{view_id}"),+ &app.cookie,+ None,+ ))+ .await+ .unwrap();+ let (status, body) = body_json(r).await;+ assert_eq!(status, 200, "view must still be readable: {body}");+ assert_eq!(body["data"]["name"], "Keep me");+}++/// delete-then-create is not atomic: a replacement that the engine rejects+/// (here, an unparseable `Date` field) must leave the previous user row in+/// place. The old helper deleted first, so a failed create dropped the only+/// copy.+#[tokio::test]+async fn upsert_user_keeps_the_row_when_the_replacement_is_rejected() {+ let app = boot_with_session().await;+ let original = store::get_user(&app.engine, "owner-test")+ .await+ .unwrap()+ .expect("boot user");+ assert_eq!(original.email, "[email protected]");++ let mut bad = original.clone();+ bad.email = "[email protected]".into();+ bad.last_seen_at = Some("not-a-date".into());+ let err = store::upsert_user(&app.engine, &bad)+ .await+ .expect_err("invalid date must be rejected");+ let msg = err.to_string();+ assert!(+ msg.contains("last_seen_at") || msg.contains("date"),+ "expected a date-parse error, got {msg}"+ );++ let still = store::get_user(&app.engine, "owner-test")+ .await+ .unwrap()+ .expect("rejected replacement must not delete the live user");+ assert_eq!(still.email, "[email protected]");+}+// ─────────────────────────────────────────────────────────────────────────────// DATA SOURCES// ─────────────────────────────────────────────────────────────────────────────