diff --git a/src/constant.rs b/src/constant.rs index 16bd649..f3e518d 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -1190,11 +1190,17 @@ pub const BACKFILL: &str = " -- which is what they were presented as all along. UPDATE chatbot_message SET truncated = false WHERE truncated = NONE; - -- An assistant turn is answered by an in-process task, so a restart leaves - -- its row `pending` with nobody left to complete it: fail it at boot rather - -- than let a reader wait out `CHATBOT_PENDING_STALE_SECS` on every load. + -- An assistant turn is answered by an in-process task, so a restart usually + -- leaves its row `pending` with nobody left to complete it. Only rows past + -- the stale horizon ($stale_ms, from `CHATBOT_PENDING_STALE_SECS`) are + -- certainly abandoned though: a young one may still be being answered on the + -- other side of the AI bridge, and a deploy must not shoot down a turn + -- dispatched seconds ago. Nothing is lost by waiting — a reader already + -- presents an over-age `pending` row as failed, and the next boot sweeps for + -- real whatever crossed the horizon in the meantime. UPDATE chatbot_message SET status = 'failed', error_code = 'interrupted', - completed_at = time::unix(time::now()) * 1000 WHERE status = 'pending'; + completed_at = time::unix(time::now()) * 1000 + WHERE status = 'pending' AND created_at < time::unix(time::now()) * 1000 - $stale_ms; -- Promotion out of student now deletes the user's enrollments (2026-07-18); -- this sweeps rows promoted before that fix. A deleted user reads as diff --git a/src/database.rs b/src/database.rs index fbb4617..a38537a 100644 --- a/src/database.rs +++ b/src/database.rs @@ -5,7 +5,7 @@ use surrealdb::engine::any::Any; use surrealdb::opt::auth::Root; use crate::config::Config; -use crate::constant::{BACKFILL, MIGRATION, PRE_REPAIR}; +use crate::constant::{BACKFILL, CHATBOT_PENDING_STALE_SECS, MIGRATION, PRE_REPAIR}; use crate::error::AppError; /// The shared database handle. @@ -79,7 +79,12 @@ pub async fn init_mem() -> Result { pub async fn migrate(db: &Surreal) -> Result<(), AppError> { db.query(PRE_REPAIR).await?.check()?; db.query(MIGRATION).await?.check()?; - db.query(BACKFILL).await?.check()?; + // Bound, not baked into the SQL string: a `const` cannot be interpolated + // into another `const`, and a hand-copied 300000 would drift silently. + db.query(BACKFILL) + .bind(("stale_ms", CHATBOT_PENDING_STALE_SECS * 1_000)) + .await? + .check()?; Ok(()) } @@ -120,6 +125,49 @@ mod tests { assert!(interrupted["completed_at"].as_i64().unwrap() > 0); } + /// The other half of the sweep: a restart seconds after a dispatch must not + /// kill the turn — the answering task may still be alive on the other side + /// of the bridge, and the row is not yet past the stale horizon. + #[tokio::test] + async fn boot_spares_a_chatbot_message_pending_since_just_now() { + let db = super::init_mem().await.unwrap(); + db.query( + "CREATE user:u SET username = 'u', password_hash = 'x'; + CREATE chatbot_thread:c SET user_id = user:u, created_at = 1, updated_at = 1; + CREATE chatbot_message:fresh SET thread_id = chatbot_thread:c, user_id = user:u, + role = 'assistant', content = '', status = 'pending', + created_at = time::unix(time::now()) * 1000 - 2000; + CREATE chatbot_message:old SET thread_id = chatbot_thread:c, user_id = user:u, + role = 'assistant', content = '', status = 'pending', + created_at = time::unix(time::now()) * 1000 + - ($stale + 1) * 1000;", + ) + .bind(("stale", crate::constant::CHATBOT_PENDING_STALE_SECS)) + .await + .unwrap() + .check() + .unwrap(); + + super::migrate(&db).await.unwrap(); + + let mut res = db + .query( + "SELECT status, error_code FROM ONLY chatbot_message:fresh; + SELECT VALUE status FROM ONLY chatbot_message:old;", + ) + .await + .unwrap(); + let fresh: Option = res.take(0).unwrap(); + let fresh = fresh.unwrap(); + let old: Option = res.take(1).unwrap(); + assert_eq!( + fresh["status"], "pending", + "a two-second-old turn is still being answered" + ); + assert_eq!(fresh["error_code"], serde_json::Value::Null); + assert_eq!(old.as_deref(), Some("failed"), "past the horizon it goes"); + } + /// A row written before choices had ids survives the retyped columns: it is /// converted, it is writable again, and its stored *position* still names /// the same option — a wrong remap here silently regrades exams.