🕰️ stop the boot sweep from killing chat turns still being answered
Browse files at this commitf77d48ae57495e4e856ee311ffacc2bb588e4153Parent: 21f4643- fix
- 🕰️ stop the boot sweep from killing chat turns still being answered
2 changed files +60 -6
- src/constant.rs +10 -4
- src/database.rs +50 -2
src/constant.rsmodified+10 -4
| @@ -1190,11 +1190,17 @@ pub const BACKFILL: &str = " | |||
| 1190 | -- which is what they were presented as all along. | 1190 | -- which is what they were presented as all along. |
| 1191 | UPDATE chatbot_message SET truncated = false WHERE truncated = NONE; | 1191 | UPDATE chatbot_message SET truncated = false WHERE truncated = NONE; |
| 1192 | 1192 | ||
| 1193 | - -- An assistant turn is answered by an in-process task, so a restart leaves | 1193 | + -- An assistant turn is answered by an in-process task, so a restart usually |
| 1194 | - -- its row `pending` with nobody left to complete it: fail it at boot rather | 1194 | + -- leaves its row `pending` with nobody left to complete it. Only rows past |
| 1195 | - -- than let a reader wait out `CHATBOT_PENDING_STALE_SECS` on every load. | 1195 | + -- the stale horizon ($stale_ms, from `CHATBOT_PENDING_STALE_SECS`) are |
| 1196 | + -- certainly abandoned though: a young one may still be being answered on the | ||
| 1197 | + -- other side of the AI bridge, and a deploy must not shoot down a turn | ||
| 1198 | + -- dispatched seconds ago. Nothing is lost by waiting — a reader already | ||
| 1199 | + -- presents an over-age `pending` row as failed, and the next boot sweeps for | ||
| 1200 | + -- real whatever crossed the horizon in the meantime. | ||
| 1196 | UPDATE chatbot_message SET status = 'failed', error_code = 'interrupted', | 1201 | UPDATE chatbot_message SET status = 'failed', error_code = 'interrupted', |
| 1197 | - completed_at = time::unix(time::now()) * 1000 WHERE status = 'pending'; | 1202 | + completed_at = time::unix(time::now()) * 1000 |
| 1203 | + WHERE status = 'pending' AND created_at < time::unix(time::now()) * 1000 - $stale_ms; | ||
| 1198 | 1204 | ||
| 1199 | -- Promotion out of student now deletes the user's enrollments (2026-07-18); | 1205 | -- Promotion out of student now deletes the user's enrollments (2026-07-18); |
| 1200 | -- this sweeps rows promoted before that fix. A deleted user reads as | 1206 | -- this sweeps rows promoted before that fix. A deleted user reads as |
src/database.rsmodified+50 -2
| @@ -5,7 +5,7 @@ use surrealdb::engine::any::Any; | |||
| 5 | use surrealdb::opt::auth::Root; | 5 | use surrealdb::opt::auth::Root; |
| 6 | 6 | ||
| 7 | use crate::config::Config; | 7 | use crate::config::Config; |
| 8 | -use crate::constant::{BACKFILL, MIGRATION, PRE_REPAIR}; | 8 | +use crate::constant::{BACKFILL, CHATBOT_PENDING_STALE_SECS, MIGRATION, PRE_REPAIR}; |
| 9 | use crate::error::AppError; | 9 | use crate::error::AppError; |
| 10 | 10 | ||
| 11 | /// The shared database handle. | 11 | /// The shared database handle. |
| @@ -79,7 +79,12 @@ pub async fn init_mem() -> Result<Database, AppError> { | |||
| 79 | pub async fn migrate(db: &Surreal<Any>) -> Result<(), AppError> { | 79 | pub async fn migrate(db: &Surreal<Any>) -> Result<(), AppError> { |
| 80 | db.query(PRE_REPAIR).await?.check()?; | 80 | db.query(PRE_REPAIR).await?.check()?; |
| 81 | db.query(MIGRATION).await?.check()?; | 81 | db.query(MIGRATION).await?.check()?; |
| 82 | - db.query(BACKFILL).await?.check()?; | 82 | + // Bound, not baked into the SQL string: a `const` cannot be interpolated |
| 83 | + // into another `const`, and a hand-copied 300000 would drift silently. | ||
| 84 | + db.query(BACKFILL) | ||
| 85 | + .bind(("stale_ms", CHATBOT_PENDING_STALE_SECS * 1_000)) | ||
| 86 | + .await? | ||
| 87 | + .check()?; | ||
| 83 | Ok(()) | 88 | Ok(()) |
| 84 | } | 89 | } |
| 85 | 90 | ||
| @@ -120,6 +125,49 @@ mod tests { | |||
| 120 | assert!(interrupted["completed_at"].as_i64().unwrap() > 0); | 125 | assert!(interrupted["completed_at"].as_i64().unwrap() > 0); |
| 121 | } | 126 | } |
| 122 | 127 | ||
| 128 | + /// The other half of the sweep: a restart seconds after a dispatch must not | ||
| 129 | + /// kill the turn — the answering task may still be alive on the other side | ||
| 130 | + /// of the bridge, and the row is not yet past the stale horizon. | ||
| 131 | + #[tokio::test] | ||
| 132 | + async fn boot_spares_a_chatbot_message_pending_since_just_now() { | ||
| 133 | + let db = super::init_mem().await.unwrap(); | ||
| 134 | + db.query( | ||
| 135 | + "CREATE user:u SET username = 'u', password_hash = 'x'; | ||
| 136 | + CREATE chatbot_thread:c SET user_id = user:u, created_at = 1, updated_at = 1; | ||
| 137 | + CREATE chatbot_message:fresh SET thread_id = chatbot_thread:c, user_id = user:u, | ||
| 138 | + role = 'assistant', content = '', status = 'pending', | ||
| 139 | + created_at = time::unix(time::now()) * 1000 - 2000; | ||
| 140 | + CREATE chatbot_message:old SET thread_id = chatbot_thread:c, user_id = user:u, | ||
| 141 | + role = 'assistant', content = '', status = 'pending', | ||
| 142 | + created_at = time::unix(time::now()) * 1000 | ||
| 143 | + - ($stale + 1) * 1000;", | ||
| 144 | + ) | ||
| 145 | + .bind(("stale", crate::constant::CHATBOT_PENDING_STALE_SECS)) | ||
| 146 | + .await | ||
| 147 | + .unwrap() | ||
| 148 | + .check() | ||
| 149 | + .unwrap(); | ||
| 150 | + | ||
| 151 | + super::migrate(&db).await.unwrap(); | ||
| 152 | + | ||
| 153 | + let mut res = db | ||
| 154 | + .query( | ||
| 155 | + "SELECT status, error_code FROM ONLY chatbot_message:fresh; | ||
| 156 | + SELECT VALUE status FROM ONLY chatbot_message:old;", | ||
| 157 | + ) | ||
| 158 | + .await | ||
| 159 | + .unwrap(); | ||
| 160 | + let fresh: Option<serde_json::Value> = res.take(0).unwrap(); | ||
| 161 | + let fresh = fresh.unwrap(); | ||
| 162 | + let old: Option<String> = res.take(1).unwrap(); | ||
| 163 | + assert_eq!( | ||
| 164 | + fresh["status"], "pending", | ||
| 165 | + "a two-second-old turn is still being answered" | ||
| 166 | + ); | ||
| 167 | + assert_eq!(fresh["error_code"], serde_json::Value::Null); | ||
| 168 | + assert_eq!(old.as_deref(), Some("failed"), "past the horizon it goes"); | ||
| 169 | + } | ||
| 170 | + | ||
| 123 | /// A row written before choices had ids survives the retyped columns: it is | 171 | /// A row written before choices had ids survives the retyped columns: it is |
| 124 | /// converted, it is writable again, and its stored *position* still names | 172 | /// converted, it is writable again, and its stored *position* still names |
| 125 | /// the same option — a wrong remap here silently regrades exams. | 173 | /// the same option — a wrong remap here silently regrades exams. |