ff-backend-postgres 0.11.0

FlowFabric EngineBackend impl — Postgres backend (RFC-v0.7, Wave 0 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! RFC-020 Wave 9 Spine-B — integration tests for the 3 read methods
//! (`read_execution_state`, `read_execution_info`, `get_execution_result`).
//!
//! Follows the `FF_PG_TEST_URL` / `#[ignore]` convention used by the
//! other Postgres integration suites.
//!
//! Coverage:
//!
//! * `read_execution_state_missing_returns_none` — `Ok(None)` when the
//!   execution id is unknown.
//! * `read_execution_state_returns_public_state` — point read against a
//!   seeded `ff_exec_core` row returns the parsed [`PublicState`].
//! * `read_execution_info_missing_returns_none` — `Ok(None)` when the
//!   execution id is unknown.
//! * `read_execution_info_lateral_joins_current_attempt` — seeds two
//!   attempt rows (index 0 + 1) + bumps `ff_exec_core.attempt_index = 1`;
//!   asserts the LATERAL join pins to attempt_index=1 and its
//!   `started_at_ms` flows into `ExecutionInfo::started_at`.
//! * `read_execution_info_terminal_derives_outcome` — terminal exec
//!   with attempt outcome=`success` surfaces `TerminalOutcome::Success`.
//! * `get_execution_result_missing_returns_none` — `Ok(None)` when the
//!   execution id is unknown.
//! * `get_execution_result_active_returns_none` — `Ok(None)` when the
//!   execution is seeded non-terminal with NULL `result`.
//! * `get_execution_result_terminal_returns_payload` — seeds a terminal
//!   execution with non-empty `result`; returns the bytes.

use ff_backend_postgres::PostgresBackend;
use ff_core::engine_backend::EngineBackend;
use ff_core::partition::PartitionConfig;
use ff_core::state::{LifecyclePhase, PublicState, TerminalOutcome};
use ff_core::types::{ExecutionId, LaneId, TimestampMs};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use uuid::Uuid;

// ── Fixture: pool + `PostgresBackend` ────────────────────────────

async fn setup_or_skip() -> Option<PgPool> {
    let url = std::env::var("FF_PG_TEST_URL").ok()?;

    let bootstrap = PgPoolOptions::new()
        .max_connections(1)
        .connect(&url)
        .await
        .expect("connect to FF_PG_TEST_URL");
    ff_backend_postgres::apply_migrations(&bootstrap)
        .await
        .expect("apply_migrations clean");
    bootstrap.close().await;

    let pool = PgPoolOptions::new()
        .max_connections(4)
        .connect(&url)
        .await
        .expect("connect pool");
    Some(pool)
}

struct Seed {
    backend: std::sync::Arc<dyn EngineBackend>,
    pool: PgPool,
    exec_id: ExecutionId,
    part: i16,
    exec_uuid: Uuid,
}

async fn seed_backend() -> Option<Seed> {
    let pool = setup_or_skip().await?;
    let lane = LaneId::new("default");
    let exec_id = ExecutionId::solo(&lane, &PartitionConfig::default());
    let part = exec_id.partition() as i16;
    let exec_uuid = Uuid::parse_str(exec_id.as_str().split_once("}:").unwrap().1).unwrap();
    let backend = PostgresBackend::from_pool(pool.clone(), PartitionConfig::default())
        as std::sync::Arc<dyn EngineBackend>;
    Some(Seed {
        backend,
        pool,
        exec_id,
        part,
        exec_uuid,
    })
}

#[allow(clippy::too_many_arguments)]
async fn insert_exec_core(
    pool: &PgPool,
    part: i16,
    exec_uuid: Uuid,
    lane_id: &str,
    lifecycle_phase: &str,
    ownership_state: &str,
    eligibility_state: &str,
    public_state: &str,
    attempt_state: &str,
    attempt_index: i32,
    now_ms: i64,
    terminal_at_ms: Option<i64>,
    result_payload: Option<Vec<u8>>,
    raw_fields: serde_json::Value,
) {
    sqlx::query(
        "INSERT INTO ff_exec_core \
           (partition_key, execution_id, lane_id, attempt_index, \
            lifecycle_phase, ownership_state, eligibility_state, \
            public_state, attempt_state, \
            priority, created_at_ms, terminal_at_ms, result, raw_fields) \
         VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 0, $10, $11, $12, $13)",
    )
    .bind(part)
    .bind(exec_uuid)
    .bind(lane_id)
    .bind(attempt_index)
    .bind(lifecycle_phase)
    .bind(ownership_state)
    .bind(eligibility_state)
    .bind(public_state)
    .bind(attempt_state)
    .bind(now_ms)
    .bind(terminal_at_ms)
    .bind(result_payload)
    .bind(raw_fields)
    .execute(pool)
    .await
    .expect("insert ff_exec_core");
}

async fn insert_attempt(
    pool: &PgPool,
    part: i16,
    exec_uuid: Uuid,
    attempt_index: i32,
    outcome: Option<&str>,
    started_at_ms: Option<i64>,
) {
    sqlx::query(
        "INSERT INTO ff_attempt \
           (partition_key, execution_id, attempt_index, lease_epoch, outcome, started_at_ms) \
         VALUES ($1, $2, $3, 0, $4, $5)",
    )
    .bind(part)
    .bind(exec_uuid)
    .bind(attempt_index)
    .bind(outcome)
    .bind(started_at_ms)
    .execute(pool)
    .await
    .expect("insert ff_attempt");
}

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

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_state_missing_returns_none() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let missing = ExecutionId::solo(&LaneId::new("default"), &PartitionConfig::default());
    let got = fx.backend.read_execution_state(&missing).await.expect("call ok");
    assert!(got.is_none(), "missing exec → Ok(None)");
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_state_returns_public_state() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "active",
        "leased",
        "not_applicable",
        "running", // Postgres writes `running`; read layer normalises → Active
        "running_attempt",
        0,
        now,
        None,
        None,
        serde_json::json!({}),
    )
    .await;

    let got = fx
        .backend
        .read_execution_state(&fx.exec_id)
        .await
        .expect("call ok");
    assert_eq!(got, Some(PublicState::Active));
}

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

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_missing_returns_none() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let missing = ExecutionId::solo(&LaneId::new("default"), &PartitionConfig::default());
    let got = fx.backend.read_execution_info(&missing).await.expect("call ok");
    assert!(got.is_none());
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_lateral_joins_both_attempts() {
    // Two LATERAL joins in `read_execution_info`:
    //   * current-attempt (attempt_index = ec.attempt_index) drives
    //     `outcome` → `TerminalOutcome`;
    //   * first-attempt (earliest started_at_ms) drives
    //     `ExecutionInfo.started_at` (first-claim timestamp, preserved
    //     across retries — Valkey-parity contract).
    //
    // Seed two attempts:
    //   * attempt 0 started at `first_start` (old first-claim);
    //   * attempt 1 started at `later_start` (current attempt);
    // then bump `ec.attempt_index = 1`.
    // The read must surface
    //   * `started_at = first_start` (NOT later_start)
    //   * `outcome`-derived state from attempt_index=1 (NULL → None)
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    let first_start = now - 10_000;
    let later_start = now - 100;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "workers",
        "active",
        "leased",
        "not_applicable",
        "running", // Postgres writes `running`; read layer normalises → Active
        "running_attempt",
        1,
        now,
        None,
        None,
        serde_json::json!({
            "namespace": "ns-a",
            "execution_kind": "task",
            "blocking_detail": "",
        }),
    )
    .await;
    // Attempt 0: first-claim row; drives `started_at`.
    insert_attempt(&fx.pool, fx.part, fx.exec_uuid, 0, Some("failed"), Some(first_start))
        .await;
    // Attempt 1: current attempt; drives `outcome` (no terminal outcome yet).
    insert_attempt(&fx.pool, fx.part, fx.exec_uuid, 1, None, Some(later_start)).await;

    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");

    assert_eq!(info.execution_id, fx.exec_id);
    assert_eq!(info.namespace, "ns-a");
    assert_eq!(info.lane_id, "workers");
    assert_eq!(info.execution_kind, "task");
    assert_eq!(info.public_state, PublicState::Active);
    assert_eq!(info.state_vector.lifecycle_phase, LifecyclePhase::Active);
    // Current-attempt (index=1) has no outcome → None (not the
    // attempt-0 `failed` outcome, which would be a LATERAL mispin).
    assert_eq!(info.state_vector.terminal_outcome, TerminalOutcome::None);
    assert_eq!(info.current_attempt_index, 1);
    // First-claim `started_at` (attempt_index=0 row); NOT the current
    // attempt's `later_start`.
    assert_eq!(
        info.started_at.as_deref(),
        Some(first_start.to_string().as_str()),
        "started_at must be first-claim timestamp (attempt 0), \
         not current-attempt (attempt 1)"
    );
    assert!(info.completed_at.is_none());
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_terminal_derives_outcome() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "terminal",
        "unowned",
        "not_applicable",
        "completed",
        "attempt_terminal",
        0,
        now,
        Some(now + 1_000),
        None,
        serde_json::json!({}),
    )
    .await;
    insert_attempt(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        0,
        Some("success"),
        Some(now - 500),
    )
    .await;

    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");

    assert_eq!(info.public_state, PublicState::Completed);
    assert_eq!(info.state_vector.lifecycle_phase, LifecyclePhase::Terminal);
    assert_eq!(info.state_vector.terminal_outcome, TerminalOutcome::Success);
    assert_eq!(
        info.completed_at.as_deref(),
        Some((now + 1_000).to_string().as_str())
    );
}

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

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn get_execution_result_missing_returns_none() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let missing = ExecutionId::solo(&LaneId::new("default"), &PartitionConfig::default());
    let got = fx
        .backend
        .get_execution_result(&missing)
        .await
        .expect("call ok");
    assert!(got.is_none());
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn get_execution_result_active_returns_none() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "active",
        "leased",
        "not_applicable",
        "running",
        "running_attempt",
        0,
        now,
        None,
        None,
        serde_json::json!({}),
    )
    .await;

    let got = fx
        .backend
        .get_execution_result(&fx.exec_id)
        .await
        .expect("call ok");
    assert!(got.is_none(), "active exec with NULL result → Ok(None)");
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn get_execution_result_terminal_returns_payload() {
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    let payload = b"hello-world-result".to_vec();
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "terminal",
        "unowned",
        "not_applicable",
        "completed",
        "attempt_terminal",
        0,
        now,
        Some(now + 1),
        Some(payload.clone()),
        serde_json::json!({}),
    )
    .await;

    let got = fx
        .backend
        .get_execution_result(&fx.exec_id)
        .await
        .expect("call ok");
    assert_eq!(got.as_deref(), Some(payload.as_slice()));
}

// ── Normalization coverage for write-site literals ───────────────

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_fresh_create_literals() {
    // Mirrors exactly the literals `create_execution_impl` writes on
    // INSERT: submitted/unowned/eligible_now/waiting/pending. Exercises
    // `attempt_state = 'pending'` → `PendingFirstAttempt` normalisation.
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "submitted",
        "unowned",
        "eligible_now",
        "waiting",
        "pending", // Postgres create-time literal
        0,
        now,
        None,
        None,
        serde_json::json!({}),
    )
    .await;

    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");
    assert_eq!(info.public_state, PublicState::Waiting);
    // `pending` normalises to `PendingFirstAttempt`.
    assert_eq!(
        info.state_vector.attempt_state,
        ff_core::state::AttemptState::PendingFirstAttempt
    );
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_cancelled_row() {
    // Postgres cancel path writes lifecycle_phase='cancelled',
    // eligibility_state='cancelled', public_state='cancelled',
    // attempt_state='cancelled' (flow.rs:674 + attempt.rs cancel).
    // Exercises every normalisation branch on the terminal-cancel row.
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "cancelled",
        "unowned",
        "cancelled",
        "cancelled",
        "cancelled",
        0,
        now,
        Some(now + 1),
        None,
        serde_json::json!({}),
    )
    .await;

    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");
    assert_eq!(info.public_state, PublicState::Cancelled);
    assert_eq!(info.state_vector.lifecycle_phase, LifecyclePhase::Terminal);
    assert_eq!(info.state_vector.terminal_outcome, TerminalOutcome::Cancelled);
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_pending_claim_eligibility() {
    // Scheduler ClaimGrant transitional literal `pending_claim` on
    // `eligibility_state`; must normalise to `EligibleNow`.
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "runnable",
        "unowned",
        "pending_claim",
        "waiting",
        // Attempt_state must be a persisted literal; `pending_claim` is
        // only written on `eligibility_state`. Use the create-time
        // `pending` (covers the initial-INSERT path).
        "pending",
        0,
        now,
        None,
        None,
        serde_json::json!({}),
    )
    .await;
    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");
    assert_eq!(
        info.state_vector.eligibility_state,
        ff_core::state::EligibilityState::EligibleNow
    );
}

#[tokio::test]
#[ignore = "requires a live Postgres; set FF_PG_TEST_URL"]
async fn read_execution_info_flow_id_returns_bare_uuid() {
    // `ExecutionInfo.flow_id` is the bare UUID wire form of `FlowId`
    // (per `uuid_id!` macro, no hash-tag prefix).
    let Some(fx) = seed_backend().await else {
        return;
    };
    let now = TimestampMs::now().0;
    // Seed a row and then UPDATE flow_id to a known uuid (the helper
    // doesn't take flow_id; a direct update keeps the fixture surface
    // small).
    insert_exec_core(
        &fx.pool,
        fx.part,
        fx.exec_uuid,
        "default",
        "submitted",
        "unowned",
        "eligible_now",
        "waiting",
        "pending",
        0,
        now,
        None,
        None,
        serde_json::json!({}),
    )
    .await;
    let flow_uuid = Uuid::new_v4();
    sqlx::query("UPDATE ff_exec_core SET flow_id = $1 WHERE partition_key = $2 AND execution_id = $3")
        .bind(flow_uuid)
        .bind(fx.part)
        .bind(fx.exec_uuid)
        .execute(&fx.pool)
        .await
        .expect("update flow_id");

    let info = fx
        .backend
        .read_execution_info(&fx.exec_id)
        .await
        .expect("call ok")
        .expect("exec present");
    assert_eq!(
        info.flow_id.as_deref(),
        Some(flow_uuid.to_string().as_str()),
        "flow_id is bare UUID, not hash-tagged"
    );
}