khive-db 0.21.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use super::*;
use crate::backend::{StorageBackend, StoreSchemaGate};
use crate::pool::PoolConfig;
use crate::test_hooks::TestHooks;
use khive_storage::{SqlAccess, StorageCapability};
use rusqlite::hooks::{AuthAction, AuthContext, Authorization, TransactionOperation};
use std::sync::atomic::AtomicUsize;

fn seeded_pool(path: &Path, strict: bool, enabled: bool) -> Arc<ConnectionPool> {
    let conn = Connection::open(path).unwrap();
    conn.execute_batch("CREATE TABLE outcome_probe(id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
        .unwrap();
    drop(conn);
    Arc::new(
        ConnectionPool::new(PoolConfig {
            path: Some(path.to_path_buf()),
            write_queue_enabled: Some(enabled),
            write_routing_strict: strict,
            ..PoolConfig::for_test()
        })
        .unwrap(),
    )
}

fn raw_write_refuses(pool: &ConnectionPool) {
    let called = AtomicBool::new(false);
    let result = pool.writer().unwrap().transaction(|_| {
        called.store(true, Ordering::SeqCst);
        Ok(())
    });
    assert!(
        matches!(result, Err(SqliteError::WriterRouteViolation { .. })),
        "a lifecycle owner must not authorize a raw request write: {result:?}"
    );
    assert!(!called.load(Ordering::SeqCst));
}

#[tokio::test]
async fn committed_reply_loss_has_real_row_and_keeps_writer_live() {
    for strict in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("outcome.db");
        let pool = seeded_pool(&path, strict, true);
        let writer = pool.writer_task_handle().unwrap().unwrap();
        let probe = writer
            .send_top_level(|conn| {
                Ok(conn.test_suppress_commit_reply_after_update("outcome_probe", 2))
            })
            .await
            .unwrap();
        writer
            .send(|conn| {
                conn.execute("INSERT INTO outcome_probe VALUES(1,'unrelated')", [])
                    .map_err(|error| {
                        StorageError::driver(StorageCapability::Sql, "fixture", error)
                    })?;
                Ok(())
            })
            .await
            .unwrap();
        assert_eq!(
            probe.snapshot().matched_requests,
            0,
            "an unrelated request cannot consume the fault arm"
        );
        let result = writer
            .send(|conn| {
                conn.execute("INSERT INTO outcome_probe VALUES(2,'committed')", [])
                    .map_err(|error| {
                        StorageError::driver(StorageCapability::Sql, "fixture", error)
                    })?;
                Ok(())
            })
            .await;
        assert!(
            matches!(result, Err(StorageError::WriteOutcomeUnknown { .. })),
            "issued COMMIT with lost successful reply must keep its own typed outcome: {result:?}"
        );
        let snapshot = probe.snapshot();
        assert_eq!(
            (snapshot.matched_requests, snapshot.reply_suppressed),
            (1, 1)
        );
        assert!(snapshot.commit_issued && snapshot.commit_succeeded && snapshot.autocommit);
        let independent = Connection::open(&path).unwrap();
        let value: String = independent
            .query_row("SELECT value FROM outcome_probe WHERE id=2", [], |row| {
                row.get(0)
            })
            .unwrap();
        assert_eq!(
            value, "committed",
            "the missing reply must follow a real durable COMMIT"
        );
        let healthy = writer
            .send_top_level(|conn| Ok(conn.is_autocommit()))
            .await
            .unwrap();
        assert!(
            healthy,
            "loss of one reply must not imply writer termination"
        );
        writer
            .send(|conn| {
                conn.execute("INSERT INTO outcome_probe VALUES(3,'healthy')", [])
                    .map_err(|error| {
                        StorageError::driver(StorageCapability::Sql, "fixture", error)
                    })?;
                Ok(())
            })
            .await
            .unwrap();
        let later: String = independent
            .query_row("SELECT value FROM outcome_probe WHERE id=3", [], |row| {
                row.get(0)
            })
            .unwrap();
        assert_eq!(
            later, "healthy",
            "the same writer must commit a later ordinary request after reply loss"
        );
    }
}

#[tokio::test]
async fn failed_commit_keeps_native_error_and_is_never_suppressed() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("failed.db");
    let pool = seeded_pool(&path, true, true);
    let writer = pool.writer_task_handle().unwrap().unwrap();
    let probe = writer
        .send_top_level(|conn| {
            let probe = conn.test_suppress_commit_reply_after_update("outcome_probe", 1);
            conn.test_authorizer(Some(|context: AuthContext<'_>| match context.action {
                AuthAction::Transaction {
                    operation: TransactionOperation::Unknown,
                } => Authorization::Deny,
                _ => Authorization::Allow,
            }))
            .unwrap();
            Ok(probe)
        })
        .await
        .unwrap();
    let result = writer
        .send(|conn| {
            conn.execute("INSERT INTO outcome_probe VALUES(1,'rolled back')", [])
                .map_err(|error| StorageError::driver(StorageCapability::Sql, "fixture", error))?;
            Ok(())
        })
        .await;
    let error = result.expect_err("real denied COMMIT must refuse");
    assert!(
        !error.is_write_outcome_unknown(),
        "native COMMIT refusal is stronger than delivery uncertainty"
    );
    let failure =
        crate::error::native_write_failure(&error, khive_storage::error::SqliteWriteStage::Commit)
            .unwrap();
    assert_eq!(failure.primary_code, rusqlite::ffi::SQLITE_AUTH);
    assert!(
        !failure.settlement_unknown,
        "successful rollback remains known"
    );
    let snapshot = probe.snapshot();
    assert_eq!(
        (snapshot.matched_requests, snapshot.reply_suppressed),
        (1, 0)
    );
    assert!(snapshot.commit_issued && !snapshot.commit_succeeded);
    let count: i64 = Connection::open(path)
        .unwrap()
        .query_row("SELECT count(*) FROM outcome_probe", [], |row| row.get(0))
        .unwrap();
    assert_eq!(
        count, 0,
        "failed COMMIT must preserve the real rollback result"
    );
}

#[tokio::test]
async fn reply_receipt_preserves_computed_errors_before_and_after_commit() {
    for issued in [false, true] {
        let (reply, reply_rx) = oneshot::channel::<Result<(), StorageError>>();
        let receipt = Arc::new(ReplyReceipt::default());
        receipt.commit_issued.store(issued, Ordering::Release);
        *receipt.completed() = Some(Err(StorageError::writer_task_terminated(
            WriterTaskRequestState::TransactionRolledBack,
        )));
        drop(reply);
        let result = PendingWrite { reply_rx, receipt }.wait().await;
        assert!(
            matches!(
                result,
                Err(StorageError::WriterTaskTerminated {
                    request_state: WriterTaskRequestState::TransactionRolledBack,
                    ..
                })
            ),
            "known rollback must outrank reply loss regardless of COMMIT evidence: {result:?}"
        );
    }
}

#[test]
fn raw_pooled_and_standalone_writes_refuse_strict_before_callbacks() {
    for strict in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let pool = seeded_pool(&dir.path().join("raw.db"), strict, false);
        let callbacks = AtomicUsize::new(0);
        let mut outcomes = Vec::new();
        outcomes.push(pool.writer().unwrap().autocommit(|conn| {
            callbacks.fetch_add(1, Ordering::SeqCst);
            conn.execute("INSERT INTO outcome_probe VALUES(1,'pooled')", [])?;
            Ok(())
        }));
        outcomes.push(pool.writer().unwrap().transaction(|conn| {
            callbacks.fetch_add(1, Ordering::SeqCst);
            conn.execute("INSERT INTO outcome_probe VALUES(2,'transaction')", [])?;
            Ok(())
        }));
        outcomes.push(pool.standalone_autocommit(|conn| {
            callbacks.fetch_add(1, Ordering::SeqCst);
            conn.execute("INSERT INTO outcome_probe VALUES(3,'standalone')", [])?;
            Ok(())
        }));
        outcomes.push(pool.standalone_transaction(|conn| {
            callbacks.fetch_add(1, Ordering::SeqCst);
            conn.execute(
                "INSERT INTO outcome_probe VALUES(4,'standalone transaction')",
                [],
            )?;
            Ok(())
        }));
        for result in outcomes {
            if strict {
                assert!(
                    matches!(result, Err(SqliteError::WriterRouteViolation { .. })),
                    "raw strict writes need a typed admission refusal: {result:?}"
                );
            } else {
                result.unwrap();
            }
        }
        let count: i64 = pool
            .reader()
            .unwrap()
            .query_row("SELECT count(*) FROM outcome_probe", [], |row| row.get(0))
            .unwrap();
        assert_eq!(
            (callbacks.load(Ordering::SeqCst), count),
            if strict { (0, 0) } else { (4, 4) },
            "strict refusal precedes all four callbacks; default writes and reads stay valid"
        );
    }
}

#[tokio::test]
async fn strict_memory_sql_and_atomic_refuse_while_default_still_writes() {
    for strict in [false, true] {
        let pool = Arc::new(
            ConnectionPool::new(PoolConfig {
                write_routing_strict: strict,
                write_queue_enabled: Some(false),
                ..PoolConfig::for_test()
            })
            .unwrap(),
        );
        let bridge = crate::sql_bridge::SqlBridge::new(Arc::clone(&pool), false);
        let writer = bridge.writer().await;
        if strict {
            assert!(
                matches!(writer, Err(StorageError::WriterOwnerUnavailable { .. })),
                "strict memory writer must not bypass owner resolution"
            );
        } else {
            writer
                .unwrap()
                .execute_script("CREATE TABLE memory_probe(value INTEGER)".into())
                .await
                .unwrap();
        }
        let called = Arc::new(AtomicBool::new(false));
        let inside = Arc::clone(&called);
        let result = bridge
            .atomic_unit(Box::new(move |writer| {
                Box::pin(async move {
                    inside.store(true, Ordering::SeqCst);
                    writer
                        .execute_script("CREATE TABLE atomic_probe(value INTEGER)".into())
                        .await?;
                    Ok(Box::new(()) as Box<dyn std::any::Any + Send>)
                })
            }))
            .await;
        if strict {
            assert!(
                matches!(result, Err(StorageError::WriterOwnerUnavailable { .. })),
                "strict memory atomic must fail before operation"
            );
        } else {
            result.unwrap();
        }
        assert_eq!(called.load(Ordering::SeqCst), !strict);
    }
}

#[tokio::test]
async fn strict_stopped_owner_refuses_before_queue_acceptance() {
    let (tx, rx) = mpsc::channel::<Box<dyn AnyWriteRequest + Send>>(1);
    let writer = WriterTaskHandle {
        tx,
        backend_key: None,
        db: "strict stopped".into(),
        slow_write_threshold: None,
        attempt_log: false,
        enqueue_timeout: Duration::from_secs(1),
        admission_metrics: WriterAdmissionMetrics::new(),
        strict_routing: true,
    };
    let dir = tempfile::tempdir().unwrap();
    let pool = seeded_pool(&dir.path().join("stopped.db"), true, true);
    let live = pool
        .writer_task_for_write(Some(&writer), "stored_write")
        .unwrap()
        .expect("a live strict owner must resolve before the close race");
    drop(rx);
    let resolved = pool.writer_task_for_write(Some(&writer), "stored_write");
    assert!(
        matches!(resolved, Err(StorageError::WriterOwnerUnavailable { .. })),
        "cached stopped owner is a preadmission failure"
    );
    let ran = Arc::new(AtomicBool::new(false));
    let inside = Arc::clone(&ran);
    let result = live
        .send(move |_| {
            inside.store(true, Ordering::SeqCst);
            Ok(())
        })
        .await;
    assert!(
        matches!(result, Err(StorageError::WriterOwnerUnavailable { .. })),
        "closed reservation race retains strict owner taxonomy"
    );
    assert!(!ran.load(Ordering::SeqCst));
}

#[test]
fn strict_core_and_pack_schema_keep_closed_lifecycle_authority() {
    let dir = tempfile::tempdir().unwrap();
    let backend = StorageBackend::sqlite_with_pool_config(
        dir.path().join("schema.db"),
        PoolConfig {
            write_routing_strict: true,
            write_queue_enabled: Some(false),
            ..PoolConfig::for_test()
        },
        None,
    )
    .unwrap();
    let version = backend.prepare_core_schema().unwrap();
    assert!(
        version > 0,
        "real core migrations must retain their closed owner"
    );
    raw_write_refuses(backend.pool());
    backend
        .apply_pack_ddl_statements_with_columns(
            &["CREATE TABLE IF NOT EXISTS pack_probe(value INTEGER)"],
            &[],
        )
        .unwrap();
    let exists: i64 = backend
        .pool()
        .reader()
        .unwrap()
        .query_row(
            "SELECT count(*) FROM sqlite_schema WHERE name='pack_probe'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(exists, 1, "strict pack schema must execute its real DDL");
    raw_write_refuses(backend.pool());
    backend.notes().unwrap();
    backend.graph().unwrap();
    backend
        .register_embedding_model("fixture", "model", "v1", 3)
        .unwrap();
    raw_write_refuses(backend.pool());
}

#[tokio::test]
async fn strict_lazy_index_repair_remains_schema_only() {
    use crate::stores::index_repair::{run_indexed_read, IndexReadKind, IndexRepairContext};
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("index.db");
    let conn = Connection::open(&path).unwrap();
    crate::stores::note::ensure_notes_schema(&conn).unwrap();
    conn.execute_batch("DROP INDEX idx_notes_message_recipient_direction")
        .unwrap();
    drop(conn);
    let pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: Some(path),
            write_routing_strict: true,
            write_queue_enabled: Some(false),
            ..PoolConfig::for_test()
        })
        .unwrap(),
    );
    let gates = std::array::from_fn(|_| Arc::new(StoreSchemaGate::default()));
    let repair = IndexRepairContext::new(Arc::clone(&pool), gates, IndexReadKind::Notes);
    let count = run_indexed_read(
        Arc::clone(&pool),
        Some(repair),
        StorageCapability::Notes,
        "index_fixture",
        |conn| {
            conn.query_row(
                "SELECT count(*) FROM notes INDEXED BY idx_notes_message_recipient_direction WHERE deleted_at IS NULL",
                [],
                |row| row.get::<_, i64>(0),
            )
        },
    )
    .await
    .unwrap();
    assert_eq!(
        count, 0,
        "repair recreates the actual forced index without creating note rows"
    );
    raw_write_refuses(&pool);
}

#[tokio::test]
async fn strict_checkpoint_and_queued_vacuum_do_not_authorize_raw_writes() {
    let dir = tempfile::tempdir().unwrap();
    let pool = seeded_pool(&dir.path().join("maintenance.db"), true, true);
    pool.open_standalone_checkpoint()
        .unwrap()
        .passive()
        .unwrap();
    raw_write_refuses(&pool);
    let writer = pool.writer_task_handle().unwrap().unwrap();
    let value = writer
        .send(|conn| {
            conn.execute("INSERT INTO outcome_probe VALUES(1,'queue')", [])
                .map_err(|e| StorageError::driver(StorageCapability::Sql, "fixture", e))?;
            Ok(7)
        })
        .await
        .unwrap();
    assert_eq!(
        value, 7,
        "the real queue owner remains admitted in strict mode"
    );
    writer
        .send_vacuum_bounded(|conn| {
            conn.execute_batch("VACUUM")
                .map_err(|e| StorageError::driver(StorageCapability::Sql, "fixture_vacuum", e))?;
            Ok(())
        })
        .await
        .unwrap();
    raw_write_refuses(&pool);
    let count: i64 = pool
        .reader()
        .unwrap()
        .query_row("SELECT count(*) FROM outcome_probe", [], |row| row.get(0))
        .unwrap();
    assert_eq!(count, 1, "queued VACUUM preserves committed data");
}

#[test]
fn strict_attachment_cutover_keeps_closed_lifecycle_authority() {
    use crate::migrations::AttachmentCutoverStatus;
    use khive_storage::{Attachment, AttachmentSubstrate};

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("attachment-cutover.db");
    let id = uuid::Uuid::new_v4();
    let content = khive_storage::blob::ContentRef::from_hex("a".repeat(64)).unwrap();
    let verified = khive_storage::blob::ContentRef::from_hex("b".repeat(64)).unwrap();
    let mut conn = Connection::open(&path).unwrap();
    conn.execute_batch(include_str!("../sql/schema-migrations-table.sql"))
        .unwrap();
    // Build the actual package-local V20 schema before strict routing is enabled.
    // The test must perform the cutover, not an already-complete no-op.
    for migration in crate::migrations::MIGRATIONS
        .iter()
        .filter(|migration| migration.version <= 20)
    {
        let tx = conn.transaction().unwrap();
        tx.execute_batch(migration.up).unwrap();
        tx.execute(
            "INSERT INTO _schema_migrations (version, name, applied_at) VALUES (?1, ?2, 0)",
            rusqlite::params![migration.version, migration.name],
        )
        .unwrap();
        tx.commit().unwrap();
    }
    conn.execute(
        "INSERT INTO entities \
         (id, namespace, kind, entity_type, name, tags, created_at, updated_at, content_ref) \
         VALUES (?1, 'local', 'document', NULL, 'cutover owner', '[]', 1, 1, ?2)",
        rusqlite::params![id.to_string(), content.as_str()],
    )
    .unwrap();
    drop(conn);

    let backend = StorageBackend::sqlite_with_pool_config(
        &path,
        PoolConfig {
            write_routing_strict: true,
            write_queue_enabled: Some(false),
            ..PoolConfig::for_test()
        },
        None,
    )
    .unwrap();
    let owner = crate::stores::blob::acquire_database_gc_owner_for_path_blocking(
        backend.sql().database_path(),
    )
    .unwrap();
    assert_eq!(
        backend.attachment_cutover_status().unwrap(),
        AttachmentCutoverStatus::Pending,
        "the strict lifecycle fixture must enter a real pending cutover"
    );
    raw_write_refuses(backend.pool());

    backend.stage_attachment_cutover(&owner).unwrap();
    assert_eq!(
        backend.attachment_cutover_status().unwrap(),
        AttachmentCutoverStatus::Incomplete
    );
    let backfilled: String = backend
        .pool()
        .reader()
        .unwrap()
        .query_row(
            "SELECT content_ref FROM attachments WHERE record_uuid=?1 AND role='content'",
            [id.to_string()],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(
        backfilled,
        content.as_str(),
        "strict cutover stage must preserve the actual legacy content attachment"
    );
    raw_write_refuses(backend.pool());

    backend
        .apply_verified_attachments(
            &owner,
            &[Attachment {
                record_uuid: id,
                substrate: AttachmentSubstrate::Entity,
                role: "verified-source".into(),
                content_ref: verified.clone(),
                media_type: Some("application/octet-stream".into()),
                size_bytes: Some(7),
                created_at: 2,
            }],
        )
        .unwrap();
    let stored: (String, String, i64, i64) = backend
        .pool()
        .reader()
        .unwrap()
        .query_row(
            "SELECT content_ref, media_type, size_bytes, created_at FROM attachments \
             WHERE record_uuid=?1 AND role='verified-source'",
            [id.to_string()],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
        )
        .unwrap();
    assert_eq!(
        stored,
        (
            verified.as_str().to_owned(),
            "application/octet-stream".into(),
            7,
            2
        ),
        "strict verified-attachment publication must persist its actual metadata"
    );
    raw_write_refuses(backend.pool());

    backend.finalize_attachment_cutover(&owner).unwrap();
    assert_eq!(
        backend.attachment_cutover_status().unwrap(),
        AttachmentCutoverStatus::Complete,
        "strict finalization must commit the actual V21 cutover"
    );
    let final_state: (i64, i64, i64, String) = backend
        .pool()
        .reader()
        .unwrap()
        .query_row(
            "SELECT (SELECT max(version) FROM _schema_migrations), \
             (SELECT count(*) FROM pragma_table_info('entities') WHERE name='content_ref'), \
             (SELECT count(*) FROM attachments WHERE record_uuid=?1), \
             (SELECT name FROM entities WHERE id=?1)",
            [id.to_string()],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
        )
        .unwrap();
    assert_eq!(
        final_state,
        (21, 0, 2, "cutover owner".into()),
        "strict cutover must retain the owner and both attachments while recording V21 and removing the legacy column"
    );
    raw_write_refuses(backend.pool());
}

#[test]
fn strict_service_migration_keeps_closed_lifecycle_authority() {
    use crate::migrations::{Migration, ServiceSchemaPlan};

    static STEPS: &[Migration] = &[Migration {
        id: "001_strict_service",
        up_sql: "CREATE TABLE service_probe(value INTEGER NOT NULL); INSERT INTO service_probe VALUES(7);",
        down_sql: None,
        is_already_applied: None,
    }];
    static PLAN: ServiceSchemaPlan = ServiceSchemaPlan {
        service: "strict_service_fixture",
        sqlite: STEPS,
        postgres: &[],
    };
    let dir = tempfile::tempdir().unwrap();
    let backend = StorageBackend::sqlite_with_pool_config(
        dir.path().join("service.db"),
        PoolConfig {
            write_routing_strict: true,
            write_queue_enabled: Some(false),
            ..PoolConfig::for_test()
        },
        None,
    )
    .unwrap();
    for _ in 0..2 {
        backend.apply_schema(&PLAN).unwrap();
        let stored: (i64, i64, i64) = backend
            .pool()
            .reader()
            .unwrap()
            .query_row(
                "SELECT (SELECT count(*) FROM service_probe), \
                 (SELECT value FROM service_probe), \
                 (SELECT count(*) FROM _schema_versions WHERE service='strict_service_fixture' AND migration_id='001_strict_service')",
                [],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
            )
            .unwrap();
        assert_eq!(
            stored,
            (1, 7, 1),
            "strict service migration and replay must retain one actual effect and one version row"
        );
        raw_write_refuses(backend.pool());
    }
}

async fn assert_labelled_commit_reply_loss(use_statement_labels: bool) {
    let route = if use_statement_labels {
        "send_labelled"
    } else {
        "send_at"
    };
    for strict in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("labelled-outcome.db");
        let pool = seeded_pool(&path, strict, true);
        let writer = pool.writer_task_handle().unwrap().unwrap();
        let probe = writer
            .send_top_level(|conn| {
                Ok(conn.test_suppress_commit_reply_after_update("outcome_probe", 1))
            })
            .await
            .unwrap();
        let write = |conn: &Connection| {
            conn.execute(
                "INSERT INTO outcome_probe VALUES(1,'labelled committed')",
                [],
            )
            .map_err(|error| {
                StorageError::driver(StorageCapability::Sql, "labelled_fixture", error)
            })?;
            Ok(())
        };
        // The test caller stays alive and awaits the real labelled submit path.
        let result = if use_statement_labels {
            writer
                .send_labelled(write, vec!["labelled_commit_probe".to_owned()])
                .await
        } else {
            writer.send_at(write, "labelled_commit_probe").await
        };
        let snapshot = probe.snapshot();
        assert_eq!(
            (snapshot.matched_requests, snapshot.reply_suppressed),
            (1, 1),
            "{route} must lose only its actual targeted successful reply"
        );
        assert!(
            snapshot.commit_issued && snapshot.commit_succeeded && snapshot.autocommit,
            "{route} must reach real successful COMMIT and autocommit before reply loss"
        );
        let independent = Connection::open(&path).unwrap();
        let committed: (i64, String) = independent
            .query_row(
                "SELECT count(*), (SELECT value FROM outcome_probe WHERE id=1) FROM outcome_probe",
                [],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .unwrap();
        assert_eq!(
            committed,
            (1, "labelled committed".into()),
            "{route} reply loss must leave exactly the literal committed row"
        );
        assert!(
            matches!(result, Err(StorageError::WriteOutcomeUnknown { .. })),
            "{route} must preserve issued-COMMIT receipt evidence after lost reply: {result:?}"
        );
        assert!(
            !writer.is_closed(),
            "{route} reply loss must not stop the owner"
        );
        let follow_up = |conn: &Connection| {
            conn.execute("INSERT INTO outcome_probe VALUES(2,'labelled healthy')", [])
                .map_err(|error| {
                    StorageError::driver(StorageCapability::Sql, "labelled_follow_up", error)
                })?;
            Ok(())
        };
        let healthy = if use_statement_labels {
            writer
                .send_labelled(follow_up, vec!["labelled_healthy_probe".to_owned()])
                .await
        } else {
            writer.send_at(follow_up, "labelled_healthy_probe").await
        };
        assert!(
            healthy.is_ok(),
            "{route} must accept a later write: {healthy:?}"
        );
        let persisted: (i64, String, String) = independent
            .query_row(
                "SELECT count(*), (SELECT value FROM outcome_probe WHERE id=1), \
                 (SELECT value FROM outcome_probe WHERE id=2) FROM outcome_probe",
                [],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
            )
            .unwrap();
        assert_eq!(
            persisted,
            (2, "labelled committed".into(), "labelled healthy".into()),
            "{route} must preserve the first commit and commit the later healthy write"
        );
        assert_eq!(probe.snapshot().reply_suppressed, 1);
    }
}

#[tokio::test]
async fn send_at_lost_commit_reply_preserves_receipt_and_writer() {
    assert_labelled_commit_reply_loss(false).await;
}

#[tokio::test]
async fn send_labelled_lost_commit_reply_preserves_receipt_and_writer() {
    assert_labelled_commit_reply_loss(true).await;
}