ff-backend-sqlite 0.12.0

FlowFabric EngineBackend impl — SQLite dev-only backend (RFC-023, Phase 1a scaffold)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! RFC-020 Wave 9 Spine-B read-model — SQLite port (RFC-023 Phase 3.3).
//!
//! Three read-only methods paralleling
//! `ff-backend-postgres/src/exec_core.rs` §4.1:
//!
//!   * [`read_execution_state_impl`] — single-column projection of
//!     `ff_exec_core.public_state`.
//!   * [`read_execution_info_impl`] — multi-column `ff_exec_core` +
//!     correlated-subquery attempt join into [`ExecutionInfo`].
//!   * [`get_execution_result_impl`] — `ff_exec_core.result` BLOB.
//!
//! Storage literals are a superset of the in-core enum variants
//! (Postgres and SQLite ports share the same authoring sites); the
//! `normalise_*` helpers collapse them to the `serde_snake_case`
//! wire-form expected by `json_enum!`. Unknown tokens surface as
//! `EngineError::Validation(Corruption)` — the `json_enum!` macro
//! deserialise from a quoted literal, so any mismatch is loud.
//!
//! All three paths are read-only; no `BEGIN IMMEDIATE` needed — we
//! execute directly on the pool. Mirrors PG's READ COMMITTED posture
//! for these three methods (§4.1).

use std::collections::HashMap;

use ff_core::contracts::{ExecutionContext, ExecutionInfo};
use ff_core::engine_error::{EngineError, ValidationKind};
use ff_core::state::{
    AttemptState, BlockingReason, EligibilityState, LifecyclePhase, OwnershipState, PublicState,
    StateVector, TerminalOutcome,
};
use ff_core::types::ExecutionId;
use serde_json::Value as JsonValue;
use sqlx::{Row, SqlitePool};
use uuid::Uuid;

use crate::errors::map_sqlx_error;
use crate::queries::reads as q_read;
use crate::tx_util::split_exec_id;

// ─── normalisation helpers (parity with PG exec_core) ──────────────
//
// Match arms are grounded in actual write sites in this crate + PG
// reference; unknown tokens fall through so `json_enum!` surfaces
// `Corruption` loudly per RFC-020 Rev 7 §4.1.

fn normalise_lifecycle_phase(raw: &str) -> &str {
    match raw {
        "cancelled" | "terminal" => "terminal",
        "pending" | "runnable" | "eligible" | "blocked" => "runnable",
        "active" => "active",
        "suspended" => "suspended",
        "submitted" => "submitted",
        other => other,
    }
}

fn normalise_ownership_state(raw: &str) -> &str {
    match raw {
        "released" | "unowned" => "unowned",
        "leased" => "leased",
        "lease_expired_reclaimable" => "lease_expired_reclaimable",
        "lease_revoked" => "lease_revoked",
        other => other,
    }
}

fn normalise_eligibility_state(raw: &str) -> &str {
    match raw {
        "cancelled" => "not_applicable",
        "pending_claim" => "eligible_now",
        other => other,
    }
}

fn normalise_attempt_state(raw: &str) -> &str {
    match raw {
        "pending" | "pending_claim" => "pending_first_attempt",
        "running" => "running_attempt",
        "cancelled" => "attempt_terminal",
        other => other,
    }
}

fn normalise_public_state(raw: &str) -> &str {
    match raw {
        "running" => "active",
        other => other,
    }
}

macro_rules! json_enum {
    ($ty:ty, $field:expr, $raw:expr) => {{
        let quoted = format!("\"{}\"", $raw);
        serde_json::from_str::<$ty>(&quoted).map_err(|e| EngineError::Validation {
            kind: ValidationKind::Corruption,
            detail: format!(
                "exec_core: {}: '{}' is not a known value: {}",
                $field, $raw, e
            ),
        })
    }};
}

fn derive_terminal_outcome(
    phase_norm: &str,
    phase_raw: &str,
    attempt_outcome: Option<&str>,
) -> TerminalOutcome {
    if phase_norm != "terminal" {
        return TerminalOutcome::None;
    }
    if phase_raw == "cancelled" {
        return TerminalOutcome::Cancelled;
    }
    match attempt_outcome {
        Some("success") => TerminalOutcome::Success,
        Some("failed") => TerminalOutcome::Failed,
        Some("cancelled") => TerminalOutcome::Cancelled,
        Some("expired") => TerminalOutcome::Expired,
        Some("skipped") => TerminalOutcome::Skipped,
        _ => TerminalOutcome::None,
    }
}

// ─── read_execution_state ──────────────────────────────────────────

/// RFC-020 §4.1 — point read of `public_state`. `Ok(None)` when the
/// execution is missing.
pub(crate) async fn read_execution_state_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<Option<PublicState>, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row: Option<(String,)> = sqlx::query_as(q_read::SELECT_PUBLIC_STATE_SQL)
        .bind(part)
        .bind(exec_uuid)
        .fetch_optional(pool)
        .await
        .map_err(map_sqlx_error)?;

    let Some((raw,)) = row else {
        return Ok(None);
    };
    let parsed: PublicState =
        json_enum!(PublicState, "public_state", normalise_public_state(&raw))?;
    Ok(Some(parsed))
}

// ─── read_execution_info ───────────────────────────────────────────

/// RFC-020 §4.1 — multi-column projection + current-attempt /
/// first-attempt joins into [`ExecutionInfo`]. `Ok(None)` when the
/// execution is missing.
pub(crate) async fn read_execution_info_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<Option<ExecutionInfo>, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row = sqlx::query(q_read::SELECT_EXECUTION_INFO_SQL)
        .bind(part)
        .bind(exec_uuid)
        .fetch_optional(pool)
        .await
        .map_err(map_sqlx_error)?;

    let Some(row) = row else {
        return Ok(None);
    };

    let flow_id_blob: Option<Vec<u8>> = row.try_get("flow_id").map_err(map_sqlx_error)?;
    let lane_id: String = row.try_get("lane_id").map_err(map_sqlx_error)?;
    let priority: i64 = row.try_get("priority").map_err(map_sqlx_error)?;
    let lifecycle_phase_raw: String =
        row.try_get("lifecycle_phase").map_err(map_sqlx_error)?;
    let ownership_state_raw: String =
        row.try_get("ownership_state").map_err(map_sqlx_error)?;
    let eligibility_state_raw: String =
        row.try_get("eligibility_state").map_err(map_sqlx_error)?;
    let public_state_raw: String = row.try_get("public_state").map_err(map_sqlx_error)?;
    let attempt_state_raw: String = row.try_get("attempt_state").map_err(map_sqlx_error)?;
    let blocking_reason_opt: Option<String> =
        row.try_get("blocking_reason").map_err(map_sqlx_error)?;
    let attempt_index: i64 = row.try_get("attempt_index").map_err(map_sqlx_error)?;
    let created_at_ms: i64 = row.try_get("created_at_ms").map_err(map_sqlx_error)?;
    let terminal_at_ms_opt: Option<i64> =
        row.try_get("terminal_at_ms").map_err(map_sqlx_error)?;
    let raw_fields_str: String = row.try_get("raw_fields").map_err(map_sqlx_error)?;
    let attempt_outcome_opt: Option<String> =
        row.try_get("attempt_outcome").map_err(map_sqlx_error)?;
    let started_at_ms_opt: Option<i64> =
        row.try_get("started_at_ms").map_err(map_sqlx_error)?;

    let raw_fields: JsonValue =
        serde_json::from_str(&raw_fields_str).map_err(|e| EngineError::Validation {
            kind: ValidationKind::Corruption,
            detail: format!("exec_core: raw_fields not valid JSON: {e}"),
        })?;

    let lifecycle_phase: LifecyclePhase = json_enum!(
        LifecyclePhase,
        "lifecycle_phase",
        normalise_lifecycle_phase(&lifecycle_phase_raw)
    )?;
    let ownership_state: OwnershipState = json_enum!(
        OwnershipState,
        "ownership_state",
        normalise_ownership_state(&ownership_state_raw)
    )?;
    let eligibility_state: EligibilityState = json_enum!(
        EligibilityState,
        "eligibility_state",
        normalise_eligibility_state(&eligibility_state_raw)
    )?;
    let public_state: PublicState = json_enum!(
        PublicState,
        "public_state",
        normalise_public_state(&public_state_raw)
    )?;
    let attempt_state: AttemptState = json_enum!(
        AttemptState,
        "attempt_state",
        normalise_attempt_state(&attempt_state_raw)
    )?;
    let blocking_reason: BlockingReason = match blocking_reason_opt
        .as_deref()
        .filter(|s| !s.is_empty())
    {
        None => BlockingReason::None,
        Some(raw) => json_enum!(BlockingReason, "blocking_reason", raw)?,
    };
    let terminal_outcome = derive_terminal_outcome(
        normalise_lifecycle_phase(&lifecycle_phase_raw),
        &lifecycle_phase_raw,
        attempt_outcome_opt.as_deref(),
    );

    let state_vector = StateVector {
        lifecycle_phase,
        ownership_state,
        eligibility_state,
        blocking_reason,
        terminal_outcome,
        attempt_state,
        public_state,
    };

    // Scalar raw_fields fields (namespace, execution_kind, blocking_detail).
    let mut namespace = String::new();
    let mut execution_kind = String::new();
    let mut blocking_detail = String::new();
    if let JsonValue::Object(map) = &raw_fields {
        if let Some(JsonValue::String(s)) = map.get("namespace") {
            namespace = s.clone();
        }
        if let Some(JsonValue::String(s)) = map.get("execution_kind") {
            execution_kind = s.clone();
        }
        if let Some(JsonValue::String(s)) = map.get("blocking_detail") {
            blocking_detail = s.clone();
        }
    }

    // `ExecutionInfo::flow_id` is the bare UUID wire form (per PG impl).
    let flow_id = flow_id_blob
        .as_deref()
        .and_then(|bytes| Uuid::from_slice(bytes).ok())
        .map(|u| u.to_string());

    // Checked integer conversions — surface storage-tier corruption
    // loudly instead of silently truncating (copilot review).
    let priority_i32: i32 = i32::try_from(priority).map_err(|e| EngineError::Validation {
        kind: ValidationKind::Corruption,
        detail: format!("exec_core: priority out of i32 range: {priority}: {e}"),
    })?;
    let attempt_index_u32: u32 = u32::try_from(attempt_index.max(0)).map_err(|e| {
        EngineError::Validation {
            kind: ValidationKind::Corruption,
            detail: format!(
                "exec_core: attempt_index out of u32 range: {attempt_index}: {e}"
            ),
        }
    })?;

    Ok(Some(ExecutionInfo {
        execution_id: id.clone(),
        namespace,
        lane_id,
        priority: priority_i32,
        execution_kind,
        state_vector,
        public_state,
        created_at: created_at_ms.to_string(),
        started_at: started_at_ms_opt.map(|v| v.to_string()),
        completed_at: terminal_at_ms_opt.map(|v| v.to_string()),
        current_attempt_index: attempt_index_u32,
        flow_id,
        blocking_detail,
    }))
}

// ─── get_execution_result ──────────────────────────────────────────

/// RFC-020 §4.1 + §7.8 — `ff_exec_core.result` BLOB point read.
/// Current-attempt semantics (Fork 3 Rev 7). `Ok(None)` when the
/// execution is missing or `result` is NULL.
pub(crate) async fn get_execution_result_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<Option<Vec<u8>>, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row: Option<(Option<Vec<u8>>,)> = sqlx::query_as(q_read::SELECT_EXECUTION_RESULT_SQL)
        .bind(part)
        .bind(exec_uuid)
        .fetch_optional(pool)
        .await
        .map_err(map_sqlx_error)?;

    match row {
        None => Ok(None),
        Some((payload,)) => Ok(payload),
    }
}

// ─── read_execution_context (v0.12 agnostic-SDK prep, PR-1) ────────

/// Point-read of `(input_payload, execution_kind, tags)` from
/// `ff_exec_core`. Mirrors the Postgres impl
/// (`ff-backend-postgres::exec_core::read_execution_context_impl`):
/// payload is the `payload` BLOB column; `execution_kind` + `tags`
/// live inside the `raw_fields` JSON text column.
///
/// Returns [`EngineError::Validation { kind: InvalidInput, .. }`](EngineError::Validation)
/// when the execution does not exist — the SDK worker only calls this
/// post-claim, so a missing row is an invariant violation rather than
/// a routine `Ok(None)`.
pub(crate) async fn read_execution_context_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<ExecutionContext, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row = sqlx::query(
        r#"
        SELECT payload, raw_fields
          FROM ff_exec_core
         WHERE partition_key = ?1 AND execution_id = ?2
        "#,
    )
    .bind(part)
    .bind(exec_uuid)
    .fetch_optional(pool)
    .await
    .map_err(map_sqlx_error)?;

    let Some(row) = row else {
        return Err(EngineError::Validation {
            kind: ValidationKind::InvalidInput,
            detail: format!("read_execution_context: execution not found: {id}"),
        });
    };

    let payload: Option<Vec<u8>> = row.try_get("payload").map_err(map_sqlx_error)?;
    let raw_fields_str: String = row.try_get("raw_fields").map_err(map_sqlx_error)?;
    let raw_fields: JsonValue =
        serde_json::from_str(&raw_fields_str).map_err(|e| EngineError::Validation {
            kind: ValidationKind::Corruption,
            detail: format!("exec_core: raw_fields not valid JSON: {e}"),
        })?;

    let input_payload = payload.unwrap_or_default();

    let (execution_kind, tags) = match &raw_fields {
        JsonValue::Object(map) => {
            let kind = map
                .get("execution_kind")
                .and_then(|v| v.as_str())
                .map(|s| s.to_owned())
                .unwrap_or_default();
            let tags: HashMap<String, String> = match map.get("tags") {
                Some(JsonValue::Object(tag_map)) => tag_map
                    .iter()
                    .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_owned())))
                    .collect(),
                _ => HashMap::new(),
            };
            (kind, tags)
        }
        _ => (String::new(), HashMap::new()),
    };

    Ok(ExecutionContext::new(input_payload, execution_kind, tags))
}

// ─── read_current_attempt_index (v0.12 agnostic-SDK prep, PR-3) ────

/// Point-read of `attempt_index` from `ff_exec_core`. Used by the SDK
/// worker on the `claim_from_resume_grant` path before dispatching
/// `claim_resumed_execution`. Missing row → `InvalidInput` (same
/// convention as [`read_execution_context_impl`]).
pub(crate) async fn read_current_attempt_index_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<ff_core::types::AttemptIndex, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row = sqlx::query(
        r#"
        SELECT attempt_index
          FROM ff_exec_core
         WHERE partition_key = ?1 AND execution_id = ?2
        "#,
    )
    .bind(part)
    .bind(exec_uuid)
    .fetch_optional(pool)
    .await
    .map_err(map_sqlx_error)?;

    let Some(row) = row else {
        return Err(EngineError::Validation {
            kind: ValidationKind::InvalidInput,
            detail: format!(
                "read_current_attempt_index: execution not found: {id}"
            ),
        });
    };
    let attempt_index_i: i64 = row.try_get("attempt_index").map_err(map_sqlx_error)?;
    let attempt_index_u: u32 = u32::try_from(attempt_index_i.max(0)).map_err(|e| {
        EngineError::Validation {
            kind: ValidationKind::Corruption,
            detail: format!(
                "exec_core: attempt_index out of u32 range: {attempt_index_i}: {e}"
            ),
        }
    })?;
    Ok(ff_core::types::AttemptIndex::new(attempt_index_u))
}

// ─── read_total_attempt_count (v0.12 PR-5.5 retry-path fix) ────────

/// Point-read of `total_attempt_count` from `ff_exec_core.raw_fields`
/// JSON. The field is stored as a JSON string inside the raw_fields
/// bag (seeded to `"0"` by `build_raw_fields`) — mirrors PG.
///
/// SQL uses `CAST(json_extract(raw_fields, '$.total_attempt_count')
/// AS INTEGER)`; the same idiom `queries/operator.rs` uses for
/// `replay_count`. Missing row → `InvalidInput`; missing/null field →
/// `0` (FCALL surface handles the loud error case).
pub(crate) async fn read_total_attempt_count_impl(
    pool: &SqlitePool,
    id: &ExecutionId,
) -> Result<ff_core::types::AttemptIndex, EngineError> {
    let (part, exec_uuid) = split_exec_id(id)?;

    let row = sqlx::query(
        r#"
        SELECT CAST(json_extract(raw_fields, '$.total_attempt_count')
                    AS INTEGER) AS total_attempt_count
          FROM ff_exec_core
         WHERE partition_key = ?1 AND execution_id = ?2
        "#,
    )
    .bind(part)
    .bind(exec_uuid)
    .fetch_optional(pool)
    .await
    .map_err(map_sqlx_error)?;

    let Some(row) = row else {
        return Err(EngineError::Validation {
            kind: ValidationKind::InvalidInput,
            detail: format!(
                "read_total_attempt_count: execution not found: {id}"
            ),
        });
    };
    let total_i: Option<i64> = row
        .try_get("total_attempt_count")
        .map_err(map_sqlx_error)?;
    let count = total_i
        .and_then(|v| u32::try_from(v.max(0)).ok())
        .unwrap_or(0);
    Ok(ff_core::types::AttemptIndex::new(count))
}