Diffinery

homeflare/celldpublic

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