| 1 | //! SQLite failure classification, reified sans-IO. When a storage operation |
| 2 | //! fails, does it POISON the actor — the engine destroyed an active transaction, |
| 3 | //! so every later operation on this scope must fail closed — or is it merely |
| 4 | //! RECOVERABLE, a statement error that left the transaction intact? Getting this |
| 5 | //! wrong is a safety bug in both directions: a poisoned actor kept serving |
| 6 | //! observes corrupt half-rolled-back state, while a healthy actor wrongly |
| 7 | //! poisoned is killed on a benign constraint violation. Production reads SQLite's |
| 8 | //! autocommit flag over FFI; the classification over the resulting scalars is |
| 9 | //! pure, and this is where it lives. |
| 10 | |
| 11 | /// SQLite primary result codes that mean the ENGINE failed (not just one |
| 12 | /// statement) — stable values from the SQLite C API. Production asserts these |
| 13 | /// match `rusqlite::ffi::*`, so any drift is caught in debug/test builds. |
| 14 | pub const SQLITE_NOMEM: i32 = 7; |
| 15 | pub const SQLITE_INTERRUPT: i32 = 9; |
| 16 | pub const SQLITE_IOERR: i32 = 10; |
| 17 | pub const SQLITE_FULL: i32 = 13; |
| 18 | |
| 19 | /// What a failed SQLite operation does to the actor. |
| 20 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 21 | pub enum Disposition { |
| 22 | /// A critical engine error destroyed an active transaction. The scope is |
| 23 | /// poisoned: fail every subsequent operation closed rather than serve state |
| 24 | /// SQLite half-rolled-back. |
| 25 | Poison, |
| 26 | /// A statement error that left the transaction open. The actor recovers. |
| 27 | Recoverable, |
| 28 | } |
| 29 | |
| 30 | /// Classify a failed SQLite operation. Poison ONLY when a critical engine error |
| 31 | /// (`FULL`/`IOERR`/`NOMEM`/`INTERRUPT`) coincides with a destroyed transaction — |
| 32 | /// SQLite rolled an active transaction back, observable as autocommit being |
| 33 | /// re-enabled. Extended result codes carry the primary code in the low byte, so |
| 34 | /// mask before comparing. Anything else — a statement error, a critical error |
| 35 | /// that spared the transaction, or a failure outside a transaction — recovers. |
| 36 | pub fn classify_failure( |
| 37 | result_code: i32, |
| 38 | started_in_transaction: bool, |
| 39 | now_autocommit: bool, |
| 40 | ) -> Disposition { |
| 41 | let primary = result_code & 0xff; |
| 42 | let critical = matches!( |
| 43 | primary, |
| 44 | SQLITE_FULL | SQLITE_IOERR | SQLITE_NOMEM | SQLITE_INTERRUPT |
| 45 | ); |
| 46 | let transaction_destroyed = started_in_transaction && now_autocommit; |
| 47 | if critical && transaction_destroyed { |
| 48 | Disposition::Poison |
| 49 | } else { |
| 50 | Disposition::Recoverable |
| 51 | } |
| 52 | } |