khive-runtime 0.14.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
use super::*;

#[test]
fn fixture_registry_cleanup_preserves_other_roots() {
    let live_dir = tempfile::tempdir().unwrap();
    let _live_guard = TestRegistryGuard::new(live_dir.path());
    let live_path = live_dir.path().join("events.db");
    let live_backend = direct_backend_for(&live_path).unwrap();

    let finished_dir = tempfile::tempdir().unwrap();
    let finished_backend = {
        let _finished_guard = TestRegistryGuard::new(finished_dir.path());
        let backend = direct_backend_for(&finished_dir.path().join("events.db")).unwrap();
        let weak = Arc::downgrade(&backend);
        drop(backend);
        assert!(
            weak.upgrade().is_some(),
            "registry retains the live fixture"
        );
        weak
    };

    assert!(
        finished_backend.upgrade().is_none(),
        "FD_FIXTURE_REGISTRY_RELEASED"
    );
    let same_live_backend = direct_backend_for(&live_path).unwrap();
    assert!(
        Arc::ptr_eq(&live_backend, &same_live_backend),
        "FD_FIXTURE_OTHER_ROOT_RETAINED"
    );
}

#[cfg(unix)]
mod target_filter_tests {
    include!("../events_split_target_tests.rs");
}

// The full daemon/forwarding test suite for this module lands with the
// final slice of this series; these tests cover the naming and
// classification contracts the module itself defines.

include!("../events_sidecar_path_tests.rs");

#[cfg(unix)]
mod store_trust_tests {
    use super::*;
    include!("../events_store_trust_tests.rs");
}

#[cfg(unix)]
#[test]
fn deep_symlink_chains_within_the_kernel_bound_derive_the_target_sidecar() {
    // Linux resolves up to 40 links per lookup; a 35-hop dangling chain
    // therefore opens fine, so it must ALSO derive the target's sidecar
    // — a resolver stopping short would split audit records between the
    // alias and target spellings of one database.
    let dir = tempfile::tempdir().unwrap();
    let real = dir.path().join("real.db");
    let mut prev = real.clone();
    for i in 0..35 {
        let link = dir.path().join(format!("hop{i}.db"));
        std::os::unix::fs::symlink(&prev, &link).unwrap();
        prev = link;
    }
    assert_eq!(
        events_db_path_beside(&prev),
        events_db_path_beside(&real),
        "a 35-hop dangling chain must derive the target's sidecar"
    );
    // Control: an unrelated dangling path still derives its own name.
    assert_ne!(
        events_db_path_beside(&dir.path().join("unrelated.db")),
        events_db_path_beside(&real)
    );
}

include!("../events_sidecar_hardening_tests.rs");
include!("../events_lock_search_parent_tests.rs");

#[cfg(unix)]
fn write_owner_only_test_file(path: &Path) {
    use std::os::unix::fs::PermissionsExt;
    std::fs::write(path, b"").unwrap();
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).unwrap();
}

#[cfg(unix)]
#[test]
fn unopened_check_refuses_same_mode_regular_file_replacement() {
    // Must fail with the old mode-only verifier: every replacement remains
    // a regular 0600 file. Creating it before rename guarantees a different
    // live inode, without relying on inode reuse after unlink or on timing.
    for target_index in 0..3 {
        let dir = tempfile::tempdir().unwrap();
        let db = dir.path().join("events.db");
        let targets = events_db_targets(&db);
        for path in &targets {
            write_owner_only_test_file(path);
        }
        let before_open = harden_events_db_sidecars(&db).unwrap();
        let replacement = dir.path().join("replacement");
        write_owner_only_test_file(&replacement);
        let replacement_id =
            EventsFileIdentity::from_metadata(&std::fs::symlink_metadata(&replacement).unwrap());
        assert_ne!(before_open[target_index], Some(replacement_id));
        // Keep a real SQLite connection open across the pathname change.
        // Do not run SQL against deliberately replaced files afterward.
        let _connection = rusqlite::Connection::open(&db).unwrap();
        verify_events_db_owner_only_unopened(&db, &before_open).unwrap();
        std::fs::rename(&replacement, &targets[target_index]).unwrap();
        let error = verify_events_db_owner_only_unopened(&db, &before_open)
            .unwrap_err()
            .to_string();
        assert!(error.contains("changed identity"), "{error}");
        assert!(
            error.contains(&targets[target_index].display().to_string()),
            "{error}"
        );
    }
}

#[cfg(unix)]
#[test]
fn unopened_check_accepts_sidecars_missing_at_check_time() {
    let dir = tempfile::tempdir().unwrap();
    let db = dir.path().join("events.db");
    let targets = events_db_targets(&db);
    for path in &targets {
        write_owner_only_test_file(path);
    }
    let before_open = harden_events_db_sidecars(&db).unwrap();
    let _connection = rusqlite::Connection::open(&db).unwrap();
    for path in targets.iter().skip(1) {
        std::fs::remove_file(path).unwrap();
        verify_events_db_owner_only_unopened(&db, &before_open)
            .expect("SQLite sidecars may disappear without losing the main database");
    }
}

#[cfg(unix)]
#[test]
fn unopened_check_refuses_main_database_removed_after_sqlite_open() {
    let dir = tempfile::tempdir().unwrap();
    let db = dir.path().join("events.db");
    ensure_events_db_owner_only(&db).unwrap();
    let before_open = harden_events_db_sidecars(&db).unwrap();
    let backend = StorageBackend::sqlite_for_test(&db).unwrap();
    backend.events().unwrap();
    verify_events_db_owner_only_unopened(&db, &before_open).unwrap();
    std::fs::remove_file(&db).unwrap();
    // Must fail with the old NotFound-for-every-target behavior. The live
    // connection can still name an unlinked inode; it must not be served.
    let error = verify_events_db_owner_only_unopened(&db, &before_open)
        .unwrap_err()
        .to_string();
    assert!(
        error.contains("main database") && error.contains("disappeared"),
        "{error}"
    );
}

#[cfg(unix)]
#[test]
fn unopened_check_accepts_fresh_sqlite_sidecars_and_unchanged_main() {
    let dir = tempfile::tempdir().unwrap();
    let db = dir.path().join("new.events.db");
    assert!(!db.exists());
    ensure_events_db_owner_only(&db).unwrap();
    let before_open = harden_events_db_sidecars(&db).unwrap();
    assert!(before_open[0].is_some());
    assert_eq!(&before_open[1..], &[None, None]);
    let backend = StorageBackend::sqlite_for_test(&db).unwrap();
    backend.events().unwrap();
    for path in events_db_targets(&db).iter().skip(1) {
        assert!(path.exists(), "SQLite creates {}", path.display());
    }
    verify_events_db_owner_only_unopened(&db, &before_open)
        .expect("new SQLite sidecars have no pre-open identity to contradict");
}

#[test]
fn socket_derives_beside_the_sidecar() {
    let db = events_db_path_beside(Path::new("/data/khive.db"));
    let socket = events_socket_path_beside(&db);
    assert!(socket.ends_with("khive.db.events.sock"), "got {socket:?}");
}

#[test]
fn bare_relative_main_db_yields_an_absolute_sidecar() {
    // A bare file name has `Some("")` for a parent; every downstream
    // consumer (lock parenting, socket-dir trust validation) needs a
    // real directory, so the sidecar path must come back anchored.
    let path = events_db_path_beside(Path::new("khive.db"));
    assert!(path.is_absolute(), "got relative {path:?}");
    assert!(path.ends_with("khive.db.events.db"), "got {path:?}");
}

#[cfg(unix)]
#[tokio::test]
async fn over_cap_query_page_limit_is_refused_before_materialization() {
    // `PageRequest.limit` arrives client-controlled off the wire; the
    // daemon must refuse an over-cap page with a typed, actionable error
    // instead of materializing and serializing it — and must never
    // silently clamp, or the short page reads as end-of-data.
    let backend = Arc::new(StorageBackend::memory().unwrap());
    let stores: NamespaceStores = Arc::new(std::sync::Mutex::new(std::collections::HashMap::new()));
    let request = |limit: u32| EventsRequest::QueryEvents {
        protocol_version: EVENTS_PROTOCOL_VERSION,
        namespace: "local".to_string(),
        filter: EventFilter::default(),
        page: PageRequest { offset: 0, limit },
    };
    let refused =
        dispatch_events_request(request(MAX_QUERY_EVENTS_PAGE_ROWS + 1), &backend, &stores).await;
    match refused {
        EventsResponse::Error {
            message,
            retryable,
            writer_task_failure,
            ..
        } => {
            assert!(
                message.contains(&MAX_QUERY_EVENTS_PAGE_ROWS.to_string()),
                "refusal must name the cap: {message}"
            );
            assert!(!retryable, "an over-cap page is not transient");
            assert!(writer_task_failure.is_none());
        }
        other => panic!("over-cap query must be refused, got {other:?}"),
    }
    // Control: a request AT the cap passes admission and reaches the
    // store — an empty page, not a refusal.
    let at_cap =
        dispatch_events_request(request(MAX_QUERY_EVENTS_PAGE_ROWS), &backend, &stores).await;
    assert!(
        matches!(at_cap, EventsResponse::Pageful { .. }),
        "at-cap query must reach the store, got {at_cap:?}"
    );
}

#[cfg(unix)]
#[test]
fn lease_contention_crosses_events_wire_as_retryable() {
    assert_eq!(EVENTS_PROTOCOL_VERSION, 6);
    for (phase, expected_retryable) in [
        (khive_storage::CapacityUnavailablePhase::LockContended, true),
        (khive_storage::CapacityUnavailablePhase::Lock, false),
    ] {
        let response = storage_error_response(&StorageError::CapacityUnavailable {
            capability: khive_storage::StorageCapability::Sql,
            phase,
            message: "lease admission failed".into(),
        });
        let encoded = serde_json::to_vec(&response).expect("serialize server error");
        let decoded: EventsResponse =
            serde_json::from_slice(&encoded).expect("decode unchanged protocol");
        let EventsResponse::Error { retryable, .. } = decoded else {
            panic!("capacity failure must be an error frame");
        };
        assert_eq!(retryable, expected_retryable, "{phase:?}");
    }
}

#[cfg(unix)]
#[test]
fn side_effects_unknown_state_crosses_the_wire_as_terminal_failure() {
    let response = storage_error_response(&StorageError::writer_task_terminated(
        khive_storage::WriterTaskRequestState::SideEffectsUnknown,
    ));
    assert!(
        matches!(
            response,
            EventsResponse::Error {
                writer_task_failure: Some(WireWriterTaskFailure::TaskTerminated {
                    request_state: WireWriterTaskState::SideEffectsUnknown,
                    sqlite_full_codes: None,
                }),
                ..
            }
        ),
        "an unknown-commit-state termination must carry its state on the wire"
    );
    // Any other refusal must NOT carry a writer state.
    let busy = storage_error_response(&StorageError::WriterTaskBusy { timeout_ms: 5 });
    assert!(matches!(
        busy,
        EventsResponse::Error {
            writer_task_failure: None,
            sqlite_write_failure: None,
            read_failure: None,
            ..
        }
    ));
}

#[cfg(unix)]
#[test]
fn proven_rollback_crosses_the_wire_without_claiming_task_termination() {
    let response = storage_error_response(&StorageError::WriterTaskRequestFailed {
        request_state: khive_storage::WriterTaskRequestState::TransactionRolledBack,
        source: Box::new(StorageError::Pool {
            operation: "writer_task_commit".into(),
            message: "commit refused".into(),
        }),
    });
    assert!(matches!(
        response,
        EventsResponse::Error {
            writer_task_failure: Some(WireWriterTaskFailure::RequestFailed {
                request_state: WireWriterTaskState::TransactionRolledBack,
            }),
            ..
        }
    ));
}

#[test]
fn error_frames_without_writer_failure_still_parse() {
    // `writer_task_failure` is `#[serde(default)]`: an Error frame for a
    // non-writer failure (parse error, version refusal) omits it and the
    // client must read `None`, not fail the parse.
    let bytes = br#"{"kind":"error","message":"boom","retryable":true}"#;
    let parsed: EventsResponse = serde_json::from_slice(bytes).expect("stateless frame parses");
    assert!(matches!(
        parsed,
        EventsResponse::Error {
            retryable: true,
            writer_task_failure: None,
            sqlite_write_failure: None,
            read_failure: None,
            ..
        }
    ));
}

fn split_retry_event(verb: &str) -> Event {
    Event::new(
        "test",
        verb,
        khive_types::EventKind::RecallExecuted,
        khive_types::SubstrateKind::Note,
        "agent:test",
    )
}

fn store_pair(dir: &Path) -> (Arc<dyn EventStore>, Arc<dyn EventStore>) {
    let legacy = direct_backend_for(&dir.join("legacy.db"))
        .expect("legacy backend")
        .events_for_namespace("test")
        .expect("legacy store");
    let lane = direct_backend_for(&dir.join("lane.db"))
        .expect("lane backend")
        .events_for_namespace("test")
        .expect("lane store");
    (legacy, lane)
}

/// A retried audit batch whose first attempt landed on the legacy store
/// before the cutover must be answered by the legacy store's comparison,
/// never re-inserted into the lane — otherwise merged reads and counts
/// double-count the id.
#[tokio::test]
async fn idempotent_retry_of_legacy_resident_rows_does_not_duplicate() {
    use khive_storage::event::EventAppendDisposition;

    let dir = tempfile::tempdir().unwrap();
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let (legacy, lane) = store_pair(dir.path());

    let resident = split_retry_event("recall");
    legacy
        .append_events_idempotent(vec![resident.clone()])
        .await
        .expect("pre-cutover landing");

    let split = SplitEventStore::new(Arc::clone(&legacy), Arc::clone(&lane));
    let fresh = split_retry_event("search");
    let result = split
        .append_events_idempotent(vec![resident.clone(), fresh.clone()])
        .await
        .expect("mixed retry batch");
    assert_eq!(
        result.rows,
        vec![
            EventAppendDisposition::AlreadyPresentIdentical,
            EventAppendDisposition::Inserted,
        ],
        "input order must be preserved across the two sub-batches"
    );
    assert_eq!(
        lane.count_events(EventFilter::default()).await.unwrap(),
        1,
        "the legacy-resident row must not reach the lane"
    );
    assert_eq!(
        split.count_events(EventFilter::default()).await.unwrap(),
        2,
        "merged count must not double-count the retried id"
    );

    // A conflicting retry of the resident row reports the conflict from
    // the legacy comparison and inserts nowhere.
    let mut mutated = resident.clone();
    mutated.verb = "other".to_string();
    let conflict = split
        .append_events_idempotent(vec![mutated])
        .await
        .expect("conflicting retry");
    assert_eq!(
        conflict.rows,
        vec![EventAppendDisposition::IdentityConflict]
    );
    assert_eq!(split.count_events(EventFilter::default()).await.unwrap(), 2);
}

/// The merged plane refuses to materialize an unbounded offset prefix;
/// the refusal names the cursor remedy, and the bound itself is usable.
#[tokio::test]
async fn merged_offset_window_is_bounded() {
    let dir = tempfile::tempdir().unwrap();
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let (legacy, lane) = store_pair(dir.path());
    let split = SplitEventStore::new(legacy, lane);

    let err = split
        .query_events(
            EventFilter::default(),
            PageRequest {
                offset: SplitEventStore::MAX_MERGED_WINDOW_ROWS,
                limit: 1,
            },
        )
        .await
        .expect_err("a window past the bound must be refused");
    assert!(
        matches!(err, StorageError::InvalidInput { .. }),
        "got {err:?}"
    );
    assert!(
        err.to_string().contains("before"),
        "the refusal must name the cursor remedy: {err}"
    );

    let at_bound = split
        .query_events(
            EventFilter::default(),
            PageRequest {
                offset: SplitEventStore::MAX_MERGED_WINDOW_ROWS - 1,
                limit: 1,
            },
        )
        .await
        .expect("a window at the bound is admitted");
    assert!(at_bound.items.is_empty());
}

#[cfg(unix)]
#[tokio::test]
async fn stateless_non_retryable_error_maps_terminal_not_writer_terminated() {
    // The terminal arm of the client mapping, pinned deliberately: an
    // Error frame with `retryable: false` and NO `writer_task_failure` is
    // a non-writer failure (parse error, version refusal) and must map
    // to terminal `InvalidInput` — never to `WriterTaskTerminated`
    // (there is no state to reconstruct) and never to the retryable
    // `Pool` arm (retrying a version refusal cannot succeed).
    let client = EventsSplitClient::new(std::path::PathBuf::from("/tmp/never-bound.sock"))
        .expect("client builds");
    let store = ForwardingEventStore::new("test", client);
    let bytes = br#"{"kind":"error","message":"refused","retryable":false}"#;
    let parsed: EventsResponse = serde_json::from_slice(bytes).expect("frame parses");
    assert!(matches!(
        store.unexpected("append", parsed),
        StorageError::InvalidInput { .. }
    ));
    // Control: the same non-retryable frame WITH a writer state must
    // take the reconstruction arm instead — proving the terminal arm
    // above is selected by the field's absence, not by `retryable`.
    let with_state = br#"{"kind":"error","message":"died","retryable":false,"writer_task_failure":{"kind":"task_terminated","request_state":"side_effects_unknown"}}"#;
    let parsed: EventsResponse = serde_json::from_slice(with_state).expect("stateful frame parses");
    assert!(matches!(
        store.unexpected("append", parsed),
        StorageError::WriterTaskTerminated { .. }
    ));
}

#[cfg(unix)]
#[tokio::test]
async fn writer_task_states_cross_the_wire_verbatim() {
    // The ADR-133 retry classifier decides per-state, so every
    // WriterTaskTerminated state must survive server → wire → client
    // reconstruction exactly. Before this field existed, NotStarted and
    // TransactionRolledBack crossed as a generic non-retryable refusal
    // and came back as terminal InvalidInput — a broken retry contract.
    use khive_storage::WriterTaskRequestState as S;
    let dir = tempfile::tempdir().unwrap();
    let client =
        EventsSplitClient::new(dir.path().join("never-bound.sock")).expect("client builds");
    let store = ForwardingEventStore::new("test", client);
    for state in [
        S::NotStarted,
        S::TransactionRolledBack,
        S::SideEffectsUnknown,
    ] {
        let response = storage_error_response(&StorageError::writer_task_terminated(state));
        // Round-trip through serde like the socket does.
        let bytes = serde_json::to_vec(&response).unwrap();
        let parsed: EventsResponse = serde_json::from_slice(&bytes).unwrap();
        let err = store.unexpected("append_events_idempotent", parsed);
        assert!(
            matches!(
                err,
                StorageError::WriterTaskTerminated { request_state, .. } if request_state == state
            ),
            "state {state:?} did not survive the socket: got {err:?}"
        );
    }
    // Control: a non-writer-task refusal must NOT carry the field.
    let busy = storage_error_response(&StorageError::WriterTaskBusy { timeout_ms: 5 });
    assert!(matches!(
        busy,
        EventsResponse::Error {
            writer_task_failure: None,
            sqlite_write_failure: None,
            read_failure: None,
            ..
        }
    ));
}

#[cfg(unix)]
#[tokio::test]
async fn native_write_stages_survive_direct_and_request_error_frames() {
    use khive_storage::error::{SqliteWriteFailure, SqliteWriteStage};
    use khive_storage::WriterTaskRequestState;
    let dir = tempfile::tempdir().unwrap();
    let client = EventsSplitClient::new(dir.path().join("never-bound.sock")).unwrap();
    let store = ForwardingEventStore::new("test", client);
    for (stage, extended) in [
        (SqliteWriteStage::Begin, rusqlite::ffi::SQLITE_BUSY_RECOVERY),
        (
            SqliteWriteStage::Statement,
            rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE,
        ),
        (SqliteWriteStage::Commit, rusqlite::ffi::SQLITE_IOERR_FSYNC),
        (SqliteWriteStage::Statement, rusqlite::ffi::SQLITE_FULL),
    ] {
        for request in [false, true] {
            for retryable in [false, true] {
                let failure = SqliteWriteFailure {
                    stage,
                    primary_code: extended & 0xff,
                    extended_code: extended,
                    settlement_unknown: false,
                };
                let source = if retryable {
                    StorageError::Pool {
                        operation: "append".into(),
                        message: "no code in this message".into(),
                    }
                } else {
                    StorageError::InvalidInput {
                        capability: khive_storage::StorageCapability::Events,
                        operation: "append".into(),
                        message: "no code in this message".into(),
                    }
                }
                .with_sqlite_write_failure(failure);
                let source = if request {
                    StorageError::WriterTaskRequestFailed {
                        request_state: WriterTaskRequestState::TransactionRolledBack,
                        source: Box::new(source),
                    }
                } else {
                    source
                };
                let bytes = serde_json::to_vec(&storage_error_response(&source)).unwrap();
                let error = store.unexpected("append", serde_json::from_slice(&bytes).unwrap());
                assert_eq!(error.sqlite_write_failure(), Some(failure));
                assert_eq!(error.is_retryable(), retryable);
                assert_eq!(
                    matches!(
                        &error,
                        StorageError::WriterTaskRequestFailed {
                            request_state: WriterTaskRequestState::TransactionRolledBack,
                            ..
                        }
                    ),
                    request
                );
                let wire =
                    crate::runtime_error_value(error.into(), crate::DomainDisposition::Unknown);
                let expected = if extended == rusqlite::ffi::SQLITE_FULL {
                    "sqlite_disk_full"
                } else {
                    stage.as_str()
                };
                assert_eq!(wire["stage"], expected);
                assert_eq!(wire["sqlite_primary_code"], failure.primary_code);
                assert_eq!(wire["sqlite_extended_code"], extended);
                if request {
                    assert_eq!(wire["task_terminated"], false);
                }
            }
        }
    }
}

#[cfg(unix)]
#[tokio::test]
async fn terminal_native_evidence_keeps_unknown_outcome_precedence() {
    use khive_storage::error::{SqliteWriteFailure, SqliteWriteStage};
    use khive_storage::WriterTaskRequestState;
    let dir = tempfile::tempdir().unwrap();
    let client = EventsSplitClient::new(dir.path().join("never-bound.sock")).unwrap();
    let store = ForwardingEventStore::new("test", client);
    for terminal in [false, true] {
        for extended in [
            rusqlite::ffi::SQLITE_IOERR_FSYNC,
            rusqlite::ffi::SQLITE_FULL,
        ] {
            let failure = SqliteWriteFailure {
                stage: SqliteWriteStage::Commit,
                primary_code: extended & 0xff,
                extended_code: extended,
                settlement_unknown: false,
            };
            // Even serialized false evidence cannot override an existing unknown outcome.
            let source = if terminal {
                StorageError::WriterTaskTerminated {
                    request_state: WriterTaskRequestState::SideEffectsUnknown,
                    sqlite_full_codes: (extended == rusqlite::ffi::SQLITE_FULL)
                        .then_some((extended, extended)),
                }
            } else {
                StorageError::WriterTaskRequestFailed {
                    request_state: WriterTaskRequestState::SideEffectsUnknown,
                    source: Box::new(StorageError::Internal("original cause".into())),
                }
            }
            .with_sqlite_write_failure(failure);
            let bytes = serde_json::to_vec(&storage_error_response(&source)).unwrap();
            let error = store.unexpected("append", serde_json::from_slice(&bytes).unwrap());
            assert_eq!(error.sqlite_write_failure(), Some(failure));
            if terminal {
                assert!(std::error::Error::source(error.without_sqlite_write_stage()).is_none());
            }
            let wire = crate::runtime_error_value(error.into(), crate::DomainDisposition::Unknown);
            let expected = if extended == rusqlite::ffi::SQLITE_FULL {
                "sqlite_disk_full"
            } else if terminal {
                "writer_task_terminated"
            } else {
                "writer_task_request_failed"
            };
            assert_eq!(wire["code"], expected);
            assert_eq!(wire["stage"], expected);
            assert_eq!(wire["sqlite_write_stage"], "sqlite_commit_failure");
            assert_eq!(wire["sqlite_extended_code"], extended);
            assert_eq!(wire["request_state"], "side_effects_unknown");
            assert_eq!(wire["task_terminated"], terminal);
            assert_eq!(wire["retryable"], false);
        }
    }
}

#[cfg(unix)]
#[tokio::test]
async fn poisoned_commit_evidence_does_not_claim_definite_failure() {
    use khive_storage::error::{SqliteWriteFailure, SqliteWriteStage};
    let dir = tempfile::tempdir().unwrap();
    let client = EventsSplitClient::new(dir.path().join("never-bound.sock")).unwrap();
    let store = ForwardingEventStore::new("test", client);
    let failure = SqliteWriteFailure {
        stage: SqliteWriteStage::Commit,
        primary_code: rusqlite::ffi::SQLITE_AUTH,
        extended_code: rusqlite::ffi::SQLITE_AUTH,
        settlement_unknown: true,
    };
    let original = StorageError::driver(
        khive_storage::StorageCapability::Sql,
        "execute_batch",
        rusqlite::Error::SqliteFailure(rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_AUTH), None),
    );
    let source = original.with_sqlite_write_failure(failure);
    let bytes = serde_json::to_vec(&storage_error_response(&source)).unwrap();
    let error = store.unexpected("append", serde_json::from_slice(&bytes).unwrap());
    assert_eq!(error.sqlite_write_failure(), Some(failure));
    assert!(!error.is_retryable());
    let wire = crate::runtime_error_value(error.into(), crate::DomainDisposition::Unknown);
    assert!(wire.get("code").is_none());
    assert!(wire.get("stage").is_none());
    assert_eq!(wire["sqlite_write_stage"], "sqlite_commit_failure");
    assert_eq!(wire["sqlite_primary_code"], rusqlite::ffi::SQLITE_AUTH);
    assert_eq!(wire["domain_disposition"], "unknown");
}

#[cfg(unix)]
#[tokio::test]
async fn proven_rollback_round_trips_as_non_terminal_request_failure() {
    use khive_storage::WriterTaskRequestState as S;

    let dir = tempfile::tempdir().unwrap();
    let client =
        EventsSplitClient::new(dir.path().join("never-bound.sock")).expect("client builds");
    let store = ForwardingEventStore::new("test", client);
    let response = storage_error_response(&StorageError::WriterTaskRequestFailed {
        request_state: S::TransactionRolledBack,
        source: Box::new(StorageError::Pool {
            operation: "writer_task_commit".into(),
            message: "commit refused".into(),
        }),
    });
    let bytes = serde_json::to_vec(&response).unwrap();
    let parsed: EventsResponse = serde_json::from_slice(&bytes).unwrap();
    let err = store.unexpected("append_events_idempotent", parsed);
    assert!(
        matches!(
            err,
            StorageError::WriterTaskRequestFailed {
                request_state: S::TransactionRolledBack,
                ..
            }
        ),
        "proven rollback must not reconstruct as a terminal writer: {err:?}"
    );
}

#[cfg(unix)]
#[test]
fn direct_backend_hardens_preexisting_db_and_sidecars() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempfile::tempdir().unwrap();
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let db = dir.path().join("pre-existing.events.db");
    let wal = dir.path().join("pre-existing.events.db-wal");
    std::fs::write(&db, b"").unwrap();
    std::fs::write(&wal, b"").unwrap();
    for path in [&db, &wal] {
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o644)).unwrap();
    }
    // Control: the loose mode is really in place before the open.
    assert_eq!(
        std::fs::metadata(&db).unwrap().permissions().mode() & 0o777,
        0o644
    );
    direct_backend_for(&db).expect("writable open succeeds");
    for path in [&db, &wal] {
        assert_eq!(
            std::fs::metadata(path).unwrap().permissions().mode() & 0o777,
            0o600,
            "pre-existing {} must be tightened to owner-only",
            path.display()
        );
    }
}

#[cfg(unix)]
#[test]
fn namespace_store_cache_is_bounded_and_trim_normalized() {
    let backend = StorageBackend::memory().unwrap();
    let stores: NamespaceStores = Arc::new(std::sync::Mutex::new(std::collections::HashMap::new()));
    // cap=1: the first namespace caches; the second evicts it and takes
    // the slot — bounded, and never condemned to per-request rebuilds.
    namespace_store_with_cap(&backend, &stores, "alpha", 1).expect("first store");
    namespace_store_with_cap(&backend, &stores, "beta", 1).expect("second store evicts");
    {
        let map = stores.lock().unwrap();
        assert_eq!(map.len(), 1, "cache must not grow past its cap");
        assert!(
            map.contains_key("beta"),
            "the newest namespace must be admitted at the cap"
        );
    }
    // Trimmed spelling of a cached namespace hits the same entry rather
    // than minting a duplicate handle (the backend trims namespaces).
    namespace_store_with_cap(&backend, &stores, "beta", 1).expect("cached beta");
    namespace_store_with_cap(&backend, &stores, "  beta  ", 1).expect("trimmed spelling");
    {
        let map = stores.lock().unwrap();
        assert_eq!(
            map.len(),
            1,
            "spellings of one namespace must share one entry"
        );
        assert!(map.contains_key("beta"), "trimmed key is the cache key");
    }
}

#[cfg(unix)]
#[tokio::test]
async fn frame_budget_admits_before_allocating_and_releases_after() {
    let (mut client, mut server) = UnixStream::pair().expect("socketpair");
    let budget = Arc::new(tokio::sync::Semaphore::new(1024));
    let payload = vec![7u8; 100];
    write_frame(&mut client, &payload).await.expect("write");
    let (bytes, permit) = read_frame_budgeted(&mut server, &budget)
        .await
        .expect("budgeted read");
    assert_eq!(bytes, payload);
    // The permit holds exactly the frame's bytes against the budget…
    assert_eq!(budget.available_permits(), 1024 - 100);
    // …and releases them when the request lifecycle ends.
    drop(permit);
    assert_eq!(budget.available_permits(), 1024);
    // A frame past the per-frame cap is refused before admission: the
    // budget is untouched, so an oversized declaration cannot starve it.
    let mut oversized = Vec::from(((crate::daemon::MAX_FRAME_BYTES + 1) as u32).to_be_bytes());
    oversized.extend_from_slice(&[0u8; 8]);
    use tokio::io::AsyncWriteExt;
    client.write_all(&oversized).await.expect("raw prefix");
    let err = read_frame_budgeted(&mut server, &budget)
        .await
        .expect_err("oversized declaration must refuse");
    assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
    assert_eq!(
        budget.available_permits(),
        1024,
        "refusal must not consume budget"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn dispatch_rejects_invalid_wire_namespace() {
    let backend = Arc::new(StorageBackend::memory().unwrap());
    let stores: NamespaceStores = Arc::new(std::sync::Mutex::new(std::collections::HashMap::new()));
    // An attacker-shaped namespace: far past the 256-byte Namespace
    // bound. Must be refused as a typed non-retryable error before it
    // can become a cache key or a store.
    let huge = "n".repeat(64 * 1024);
    let response = dispatch_events_request(
        EventsRequest::CountEvents {
            protocol_version: EVENTS_PROTOCOL_VERSION,
            namespace: huge,
            operator_read_budget_secs: Some(30),
            filter: Default::default(),
        },
        &backend,
        &stores,
    )
    .await;
    assert!(
        matches!(
            &response,
            EventsResponse::Error {
                retryable: false,
                writer_task_failure: None,
                sqlite_write_failure: None,
                read_failure: None,
                ..
            }
        ),
        "oversized namespace must be a typed refusal, got {response:?}"
    );
    assert_eq!(
        stores.lock().unwrap().len(),
        0,
        "a rejected namespace must never enter the cache"
    );
    // Control: a valid namespace on the same dispatch path succeeds.
    let ok = dispatch_events_request(
        EventsRequest::CountEvents {
            protocol_version: EVENTS_PROTOCOL_VERSION,
            namespace: "local".to_string(),
            operator_read_budget_secs: Some(30),
            filter: Default::default(),
        },
        &backend,
        &stores,
    )
    .await;
    assert!(
        matches!(ok, EventsResponse::Count { .. }),
        "valid namespace must dispatch, got {ok:?}"
    );
}

include!("../events_split_shutdown_tests.rs");
include!("../events_split_readiness_tests.rs");

#[cfg(unix)]
#[tokio::test]
async fn forwarder_abandons_hung_delivery() {
    // A daemon that accepts the connection and then never responds must
    // not park the forwarder forever: the delivery deadline fires, the
    // batch is dropped and counted, and the task stays live.
    let dir = tempfile::tempdir().unwrap();
    let socket = dir.path().join("hung.sock");
    let listener = tokio::net::UnixListener::bind(&socket).unwrap();
    // Accept and hold connections open without ever reading or replying.
    let _server = tokio::spawn(async move {
        let mut held = Vec::new();
        loop {
            if let Ok((stream, _)) = listener.accept().await {
                held.push(stream);
            }
        }
    });
    let client = EventsSplitClient::new_with_queue_depth_and_delivery_timeout(
        socket.clone(),
        4,
        Duration::from_millis(100),
    )
    .expect("client builds");
    let event = Event::new(
        "test",
        "noop",
        khive_types::EventKind::Audit,
        khive_types::SubstrateKind::Event,
        "tester",
    );
    client.enqueue("test", vec![event]);
    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    loop {
        if client.metrics().dropped_batches >= 1 {
            break;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "forwarder never abandoned the hung delivery: {:?}",
            client.metrics()
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

#[cfg(unix)]
#[test]
fn wire_retryability_follows_the_storage_classifier() {
    let busy = storage_error_response(&StorageError::WriterTaskBusy { timeout_ms: 5 });
    assert!(
        matches!(
            busy,
            EventsResponse::Error {
                retryable: true,
                ..
            }
        ),
        "transient writer contention must stay retryable across the socket"
    );
    // A terminated writer task is not retryable per the storage layer's
    // own classifier; the wire response must agree rather than widen it.
    let terminated = storage_error_response(&StorageError::writer_task_terminated(
        khive_storage::WriterTaskRequestState::SideEffectsUnknown,
    ));
    assert!(
        matches!(
            terminated,
            EventsResponse::Error {
                retryable: false,
                ..
            }
        ),
        "a terminated writer must not be reported transient"
    );
}

use khive_storage::event::EventAppendDisposition;
use khive_types::{EventKind, SubstrateKind};

fn test_event(namespace: &str) -> Event {
    Event::new(
        namespace,
        "test.verb",
        EventKind::Audit,
        SubstrateKind::Event,
        "actor:test",
    )
}

/// Boot a real events daemon on temp paths, poll until its socket accepts.
#[cfg(unix)]
async fn boot_daemon(dir: &tempfile::TempDir) -> (PathBuf, PathBuf) {
    let db = dir.path().join("events.db");
    let socket = dir.path().join("events.sock");
    let (db_clone, socket_clone) = (db.clone(), socket.clone());
    tokio::spawn(async move {
        let _ = run_events_daemon(&db_clone, &socket_clone).await;
    });
    for _ in 0..100 {
        if UnixStream::connect(&socket).await.is_ok() {
            return (db, socket);
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    panic!("events daemon did not come up on {}", socket.display());
}

/// The split store's routing contract: plain appends land ONLY in the
/// legacy store (whose raw-SQL consumers depend on finding them there),
/// the idempotent audit lane lands ONLY in the events lane, and reads
/// merge both sides into one event plane.
#[tokio::test]
async fn split_store_routes_plain_to_legacy_idempotent_to_lane_and_merges_reads() {
    let dir = tempfile::tempdir().expect("tempdir");
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let legacy_backend = direct_backend_for(&dir.path().join("legacy.db")).expect("legacy backend");
    let lane_backend = direct_backend_for(&dir.path().join("lane.db")).expect("lane backend");
    let legacy = legacy_backend
        .events_for_namespace("local")
        .expect("legacy store");
    let lane = lane_backend
        .events_for_namespace("local")
        .expect("lane store");
    let split = SplitEventStore::new(Arc::clone(&legacy), Arc::clone(&lane));

    let plain = test_event("local");
    let plain_id = plain.id;
    split.append_event(plain).await.expect("plain append");

    let audit = test_event("local");
    let audit_id = audit.id;
    let result = split
        .append_events_idempotent(vec![audit])
        .await
        .expect("idempotent append");
    assert_eq!(result.rows, vec![EventAppendDisposition::Inserted]);

    // Routing: each row is in exactly its own side.
    assert!(legacy
        .get_event(plain_id)
        .await
        .expect("legacy get")
        .is_some());
    assert!(lane.get_event(plain_id).await.expect("lane get").is_none());
    assert!(legacy
        .get_event(audit_id)
        .await
        .expect("legacy get")
        .is_none());
    assert!(lane.get_event(audit_id).await.expect("lane get").is_some());

    // Merged reads observe one event plane whichever side holds the row.
    assert!(split
        .get_event(plain_id)
        .await
        .expect("split get")
        .is_some());
    assert!(split
        .get_event(audit_id)
        .await
        .expect("split get")
        .is_some());
    assert_eq!(
        split
            .count_events(EventFilter::default())
            .await
            .expect("split count"),
        2
    );
    let page = split
        .query_events(
            EventFilter::default(),
            PageRequest {
                offset: 0,
                limit: 10,
            },
        )
        .await
        .expect("split query");
    let ids: Vec<Uuid> = page.items.iter().map(|e| e.id).collect();
    assert!(ids.contains(&plain_id) && ids.contains(&audit_id));

    // Windowing across the merge: page size 1 at offsets 0 and 1 yields
    // the two rows exactly once each.
    let mut seen = Vec::new();
    for offset in 0..2 {
        let page = split
            .query_events(EventFilter::default(), PageRequest { offset, limit: 1 })
            .await
            .expect("windowed query");
        assert_eq!(page.items.len(), 1);
        seen.push(page.items[0].id);
    }
    seen.sort();
    let mut expected = vec![plain_id, audit_id];
    expected.sort();
    assert_eq!(seen, expected);
}

/// Mechanized co-residency guard: the amendment's load-bearing claim is
/// that every class a raw-SQL consumer of the legacy `events` table
/// depends on still LANDS in that table. Two of the three enumerated
/// consumers (kg projection guard, graph-query union) fail silently if
/// the classification drifts, so the classification is guarded here by
/// the consumers' own access pattern — a raw SQL read against the legacy
/// backend — rather than by enumeration. Arm 1 reddens if plain appends
/// (the provenance/domain class) ever stop reaching the legacy table.
/// Arm 2 is the boundary's other face and doubles as the positive
/// control for the instrument: the same query DOES find lane-routed rows
/// on the lane's own backend, so an empty arm-1 result could not be a
/// broken query.
#[tokio::test]
async fn raw_sql_consumers_of_the_legacy_events_table_still_see_plain_appends() {
    use khive_storage::types::{SqlStatement, SqlValue};

    let dir = tempfile::tempdir().expect("tempdir");
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let legacy_backend =
        direct_backend_for(&dir.path().join("legacy-guard.db")).expect("legacy backend");
    let lane_backend = direct_backend_for(&dir.path().join("lane-guard.db")).expect("lane backend");
    let split = SplitEventStore::new(
        legacy_backend
            .events_for_namespace("local")
            .expect("legacy store"),
        lane_backend
            .events_for_namespace("local")
            .expect("lane store"),
    );

    // The schedule pack's creator-provenance write is a plain append; the
    // audit flusher's write is an idempotent batch. Route one of each.
    let provenance = test_event("local");
    let provenance_id = provenance.id;
    split.append_event(provenance).await.expect("plain append");
    let audit = test_event("local");
    let audit_id = audit.id;
    split
        .append_events_idempotent(vec![audit])
        .await
        .expect("idempotent append");

    let count_by_raw_sql = |backend: Arc<StorageBackend>, id: Uuid| async move {
        let mut reader = backend.sql().reader().await.expect("sql reader");
        let rows = reader
            .query_all(SqlStatement {
                sql: "SELECT actor FROM events WHERE id = ?1".to_string(),
                params: vec![SqlValue::Text(id.to_string())],
                label: None,
            })
            .await
            .expect("raw events query");
        rows.len()
    };

    // Arm 1: the co-residency contract. A raw-SQL consumer of the legacy
    // table finds the plain-append row there.
    assert_eq!(
        count_by_raw_sql(Arc::clone(&legacy_backend), provenance_id).await,
        1,
        "plain appends must stay visible to raw-SQL consumers of the legacy events table"
    );
    // Arm 2a: the moved class is genuinely gone from the legacy table —
    // this is the silent-failure face of the boundary, held visible.
    assert_eq!(
        count_by_raw_sql(Arc::clone(&legacy_backend), audit_id).await,
        0,
        "audit-lane rows must not land in the legacy events table"
    );
    // Arm 2b: positive control for the instrument — the identical query
    // finds the moved row on the lane backend, so arm 2a's zero (and any
    // future arm-1 zero) is a routing fact, not a dead query.
    assert_eq!(
        count_by_raw_sql(Arc::clone(&lane_backend), audit_id).await,
        1,
        "the raw query must prove it can find rows where they actually live"
    );
}

/// End to end over the real socket: the idempotent lane returns true
/// dispositions, and the row is readable back through the same store.
#[cfg(unix)]
#[tokio::test]
async fn idempotent_append_round_trips_through_the_daemon() {
    let dir = tempfile::tempdir().expect("tempdir");
    let (_db, socket) = boot_daemon(&dir).await;
    let client = EventsSplitClient::new(socket).expect("client");
    let store = ForwardingEventStore::new("local", client);

    let event = test_event("local");
    let id = event.id;
    let result = store
        .append_events_idempotent(vec![event.clone()])
        .await
        .expect("idempotent append over socket");
    assert_eq!(result.rows, vec![EventAppendDisposition::Inserted]);

    // Retry of the identical row reports AlreadyPresentIdentical — the
    // daemon ran the real idempotent path, not a blind re-insert.
    let retry = store
        .append_events_idempotent(vec![event])
        .await
        .expect("idempotent retry");
    assert_eq!(
        retry.rows,
        vec![EventAppendDisposition::AlreadyPresentIdentical]
    );

    let fetched = store.get_event(id).await.expect("get over socket");
    assert_eq!(fetched.map(|e| e.id), Some(id));
    let count = store
        .count_events(EventFilter::default())
        .await
        .expect("count over socket");
    assert_eq!(count, 1);
}

/// The fire-and-forget lane delivers to the daemon store: enqueue returns
/// immediately and the row becomes visible to a subsequent count.
#[cfg(unix)]
#[tokio::test]
async fn fire_and_forget_append_lands_in_the_daemon_store() {
    let dir = tempfile::tempdir().expect("tempdir");
    let (_db, socket) = boot_daemon(&dir).await;
    let client = EventsSplitClient::new(socket).expect("client");
    let store = ForwardingEventStore::new("local", Arc::clone(&client));

    store
        .append_event(test_event("local"))
        .await
        .expect("append_event is fire-and-forget");

    // Poll for BOTH effects: the row landing daemon-side and the
    // forwarder's own counter — the row can be visible a beat before the
    // forwarder task is rescheduled to record the delivery.
    let (count, metrics) = tokio::time::timeout(Duration::from_secs(30), async {
        loop {
            let count = store
                .count_events(EventFilter::default())
                .await
                .expect("count over socket");
            let metrics = client.metrics();
            if (count == 1 && metrics.forwarded_events >= 1) || metrics.dropped_events > 0 {
                break (count, metrics);
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
    })
    .await
    .expect("forwarder must deliver or report a drop within 30 seconds");
    assert_eq!(
        metrics.dropped_events, 0,
        "forwarder dropped an event before it reached the daemon store"
    );
    assert_eq!(count, 1, "forwarded event must land in the daemon store");
    assert!(metrics.forwarded_events >= 1, "delivery must be counted");
}

/// R4 overflow arm: with the daemon dead, appends beyond the queue bound
/// are DROPPED and counted while every append still returns Ok — the
/// domain path proceeds. The drop counter is the loss-tolerance contract.
#[cfg(unix)]
#[tokio::test]
async fn queue_overflow_drops_and_counts_but_never_errors() {
    let dir = tempfile::tempdir().expect("tempdir");
    let dead_socket = dir.path().join("nobody-home.sock");
    let client = EventsSplitClient::new_with_queue_depth(dead_socket, 2).expect("client");
    let store = ForwardingEventStore::new("local", Arc::clone(&client));

    for _ in 0..20 {
        store
            .append_event(test_event("local"))
            .await
            .expect("append_event must not error under overflow");
    }
    let metrics = client.metrics();
    assert!(
        metrics.dropped_events > 0,
        "overflow must register in the drop counter, got {metrics:?}"
    );
}

/// R4 dead-socket arm: synchronous lanes fail with a typed retryable
/// storage error (never hang, never a generic panic), and the offline
/// preflight validator keeps working with zero I/O.
#[cfg(unix)]
#[tokio::test]
async fn dead_socket_reads_fail_typed_and_preflight_stays_local() {
    let dir = tempfile::tempdir().expect("tempdir");
    let dead_socket = dir.path().join("nobody-home.sock");
    let client = EventsSplitClient::new(dead_socket).expect("client");
    let store = ForwardingEventStore::new("local", client);

    let error = store
        .get_event(Uuid::new_v4())
        .await
        .expect_err("read against a dead socket must fail");
    assert!(
        matches!(error, StorageError::Pool { .. }),
        "expected the typed unreachable error, got {error:?}"
    );

    // The audit-batch seam contract: preflight is local and functional
    // with the daemon down.
    assert!(store.supports_idempotent_audit_batch());
    store
        .preflight_event(&test_event("local"))
        .expect("offline preflight validates a well-formed event");
}

/// A connected peer that consumed the request but sent no response is a
/// broken response, not evidence that no daemon answered the socket.
#[cfg(unix)]
#[tokio::test]
async fn connected_events_peer_closing_without_response_is_not_unreachable() {
    let dir = tempfile::tempdir().unwrap();
    let socket = dir.path().join("closes-after-request.sock");
    let listener = UnixListener::bind(&socket).unwrap();
    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();
        read_frame(&mut stream)
            .await
            .expect("complete request arrives");
        // Drop the connection without writing a response frame.
    });
    let client = EventsSplitClient::new(socket).unwrap();
    let store = ForwardingEventStore::new("local", client);
    let error = store.get_event(Uuid::new_v4()).await.unwrap_err();
    assert!(
        matches!(error, StorageError::Serialization { .. }),
        "a post-connect response failure must not be Pool/unreachable: {error:?}"
    );
    server.await.unwrap();
}

/// 4096 ordinary 3-KiB events obey the row limit yet exceed one IPC
/// frame. The daemon must answer with a non-retryable size refusal so a
/// caller can use a narrower page on the same healthy socket.
#[cfg(unix)]
#[tokio::test]
async fn query_page_over_frame_cap_returns_typed_size_refusal() {
    let dir = tempfile::tempdir().unwrap();
    let socket = dir.path().join("large-query.sock");
    let backend = Arc::new(StorageBackend::memory().unwrap());
    let event_store = backend.events_for_namespace("local").unwrap();
    let events: Vec<_> = (0..MAX_QUERY_EVENTS_PAGE_ROWS)
        .map(|_| {
            test_event("local").with_payload(serde_json::json!({"data": "x".repeat(3 * 1024)}))
        })
        .collect();
    event_store
        .append_events(events)
        .await
        .expect("seed large page");

    let listener = UnixListener::bind(&socket).unwrap();
    let stores: NamespaceStores = Arc::new(std::sync::Mutex::new(std::collections::HashMap::new()));
    let server = tokio::spawn(async move {
        let (stream, _) = listener.accept().await.unwrap();
        serve_events_conn(
            stream,
            backend,
            stores,
            Arc::new(tokio::sync::Semaphore::new(MAX_INFLIGHT_REQUEST_BYTES)),
        )
        .await;
    });
    let client = EventsSplitClient::new(socket).unwrap();
    let store = ForwardingEventStore::new("local", client);
    let error = store
        .query_events(
            EventFilter::default(),
            PageRequest {
                offset: 0,
                limit: MAX_QUERY_EVENTS_PAGE_ROWS,
            },
        )
        .await
        .expect_err("the full page cannot fit one frame");
    assert!(
        matches!(error, StorageError::InvalidInput { .. }),
        "expected non-retryable frame-size refusal, got {error:?}"
    );
    assert!(
        error.to_string().contains("response_frame_size_limit"),
        "{error}"
    );
    assert!(
        error.to_string().contains("request a narrower page"),
        "{error}"
    );
    server.abort();
}

/// The byte budget counts the in-flight batch as well as queued batches.
/// A stalled daemon therefore cannot retain another large batch just
/// because the mpsc channel still has spare batch slots.
#[cfg(unix)]
#[tokio::test]
async fn forwarding_queue_enforces_serialized_byte_and_frame_limits_with_drop_metrics() {
    let dir = tempfile::tempdir().unwrap();
    let socket = dir.path().join("stalled-forwarder.sock");
    let listener = UnixListener::bind(&socket).unwrap();
    let server = tokio::spawn(async move {
        let (stream, _) = listener.accept().await.unwrap();
        let _hold_open = stream;
        std::future::pending::<()>().await;
    });
    let event = test_event("local").with_payload(serde_json::json!({"data": "x".repeat(4096)}));
    let first = vec![event.clone(), event.clone()];
    let request_bytes = serde_json::to_vec(&EventsRequest::AppendEvents {
        protocol_version: EVENTS_PROTOCOL_VERSION,
        namespace: "local".into(),
        events: first.clone(),
    })
    .unwrap()
    .len();
    let byte_budget = request_bytes + 64;
    let client = EventsSplitClient::new_with_limits_and_delivery_timeout(
        socket,
        8,
        byte_budget,
        Duration::from_secs(30),
    )
    .unwrap();
    let store = ForwardingEventStore::new("local", Arc::clone(&client));
    store.append_events(first.clone()).await.unwrap();
    assert_eq!(client.metrics().queued_bytes, request_bytes);

    store.append_events(first).await.unwrap();
    let metrics = client.metrics();
    assert_eq!(metrics.dropped_batches, 1);
    assert_eq!(metrics.dropped_events, 2);
    assert_eq!(metrics.queued_bytes, request_bytes);
    assert!(metrics.queued_bytes <= byte_budget);

    // One append request larger than the daemon frame cannot ever be
    // delivered; it must be refused before occupying a queue slot.
    let oversized = test_event("local")
        .with_payload(serde_json::json!({"data": "x".repeat(crate::daemon::MAX_FRAME_BYTES)}));
    store.append_event(oversized).await.unwrap();
    let metrics = client.metrics();
    assert_eq!(metrics.dropped_batches, 2);
    assert_eq!(metrics.dropped_events, 3);
    assert_eq!(metrics.queued_bytes, request_bytes);
    server.abort();
}

/// Version-skew arm: a frame carrying an unknown protocol version gets a
/// typed non-retryable refusal, not a deserialization failure.
#[cfg(unix)]
#[tokio::test]
async fn protocol_version_skew_is_a_typed_refusal() {
    let dir = tempfile::tempdir().expect("tempdir");
    let (_db, socket) = boot_daemon(&dir).await;

    let mut stream = UnixStream::connect(&socket).await.expect("connect");
    let request = EventsRequest::CountEvents {
        protocol_version: EVENTS_PROTOCOL_VERSION + 1,
        namespace: "local".into(),
        operator_read_budget_secs: Some(30),
        filter: EventFilter::default(),
    };
    let payload = serde_json::to_vec(&request).expect("serialize");
    write_frame(&mut stream, &payload).await.expect("write");
    let bytes = read_frame(&mut stream).await.expect("read");
    let response: EventsResponse = serde_json::from_slice(&bytes).expect("parse");
    match response {
        EventsResponse::Error {
            message, retryable, ..
        } => {
            assert!(!retryable, "version skew is not retryable");
            assert!(message.contains("protocol version"), "message: {message}");
        }
        other => panic!("expected a typed refusal, got {other:?}"),
    }
}

/// The per-socket daemon guard is exclusive while held and reusable after
/// release — the mechanism that keeps a supervisor respawn race down to
/// one surviving daemon.
#[cfg(unix)]
#[test]
fn events_daemon_guard_is_exclusive_then_reusable() {
    let dir = tempfile::tempdir().expect("tempdir");
    let socket = dir.path().join("events.sock");
    let first = match acquire_events_daemon_guard_outcome(&socket) {
        EventsDaemonGuardAcquisition::Held(guard) => guard,
        other => panic!("first acquire must succeed: {other:?}"),
    };
    let second = acquire_events_daemon_guard_outcome(&socket);
    assert!(
        matches!(second, EventsDaemonGuardAcquisition::Contended),
        "second acquire must report contention while the first guard is held: {second:?}"
    );
    drop(first);
    // Another test may fork while the first description is open. A child
    // can retain that flock until exec or exit; retry only that diagnosed
    // contention, never an open/hardening error, and never without a bound.
    const MAX_ATTEMPTS: usize = 100;
    for attempt in 1..=MAX_ATTEMPTS {
        match acquire_events_daemon_guard_outcome(&socket) {
            EventsDaemonGuardAcquisition::Held(_) => return,
            EventsDaemonGuardAcquisition::Contended if attempt < MAX_ATTEMPTS => {
                std::thread::sleep(Duration::from_millis(10));
            }
            other => panic!(
                "acquire must succeed after release within {MAX_ATTEMPTS} attempts; \
                 attempt {attempt}: {other:?}"
            ),
        }
    }
    unreachable!("the final acquisition attempt returns or reports its refusal");
}

#[cfg(unix)]
#[test]
fn events_daemon_guard_open_failure_is_not_contention() {
    let dir = tempfile::tempdir().expect("tempdir");
    let clean = dir.path().join("clean.sock");
    assert!(matches!(
        acquire_events_daemon_guard_outcome(&clean),
        EventsDaemonGuardAcquisition::Held(_)
    ));

    assert!(
        try_acquire_events_daemon_guard(&clean).is_some(),
        "the public Option entrance must preserve a successful acquisition"
    );

    // Opening a directory for writing must fail even when no process
    // holds a daemon lock. Exercise the real open, not an injected result.
    let socket = dir.path().join("blocked.sock");
    let lock_path = socket.with_extension("lock");
    std::fs::create_dir(&lock_path).expect("create directory at lock entry");
    let outcome = acquire_events_daemon_guard_outcome(&socket);
    assert!(
        matches!(outcome, EventsDaemonGuardAcquisition::OpenFailed(_)),
        "an actual lock-file open failure must not report contention: {outcome:?}"
    );
    assert!(
        try_acquire_events_daemon_guard(&socket).is_none(),
        "the public Option entrance must refuse an actual open failure"
    );
    assert!(
        lock_path.is_dir(),
        "the refused entry must remain a directory"
    );
}

/// A read-only file-backed runtime with the split configured must never
/// create or schema-initialize an events database. Missing events db →
/// `events()` serves the legacy store alone and leaves the filesystem
/// untouched; a pre-existing events db (minted by an earlier writable
/// host) is opened read-only and its rows merge into reads.
/// (unix-gated for the sidecar-freeze harness step only.)
#[cfg(unix)]
#[tokio::test]
async fn read_only_runtime_never_creates_an_events_db() {
    use crate::{KhiveRuntime, Namespace, RuntimeConfig};

    let dir = tempfile::tempdir().expect("tempdir");
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let main_db = dir.path().join("main.db");
    // Materialize + migrate the main database with a writable runtime.
    drop(
        KhiveRuntime::new_for_test(RuntimeConfig {
            db_path: Some(main_db.clone()),
            ..RuntimeConfig::no_embeddings()
        })
        .expect("create main db"),
    );
    let events_db = events_db_path_beside(&main_db);
    assert!(!events_db.exists(), "precondition: no events db yet");

    // Freeze the WAL sidecars the writable runtime left behind — the
    // read-only opener refuses a snapshot with a writable -shm.
    khive_storage::test_support::freeze_snapshot_sidecars(&main_db);

    let split_config = |db: PathBuf| RuntimeConfig {
        db_path: Some(main_db.clone()),
        events_split: Some(EventsSplitConfig {
            db_path: db,
            socket_path: None,
        }),
        ..RuntimeConfig::no_embeddings()
    };

    // Arm 1: read-only runtime, no events db on disk. Reads work through
    // the legacy store and nothing is minted.
    let ro = KhiveRuntime::new_readonly_for_test(split_config(events_db.clone()))
        .expect("read-only runtime");
    let token = ro.authorize(Namespace::local()).expect("token");
    let store = ro.events(&token).expect("events store");
    let count = store
        .count_events(EventFilter::default())
        .await
        .expect("count through legacy-only plane");
    assert_eq!(count, 0);
    assert!(
        !events_db.exists(),
        "a read-only runtime must not mint the events database"
    );

    // Arm 2: a writable host mints the events db and lands a lane row;
    // the read-only runtime then opens it read-only and merges the row.
    // The writer is scoped (not the process-global registry) and its WAL
    // sidecars frozen after drop — the read-only opener rightly refuses a
    // snapshot with a live writer, and that refusal is not this test's
    // subject.
    {
        let lane_backend = StorageBackend::sqlite_for_test(&events_db).expect("writable lane");
        lane_backend
            .events_for_namespace("local")
            .expect("lane store")
            .append_event(test_event("local"))
            .await
            .expect("seed lane row");
    }
    khive_storage::test_support::freeze_snapshot_sidecars(&events_db);
    let store = ro.events(&token).expect("events store with lane present");
    let count = store
        .count_events(EventFilter::default())
        .await
        .expect("merged count");
    assert_eq!(count, 1, "the pre-existing lane row must merge into reads");
}

/// Embedded (direct) mode: the process-global backend writes and reads
/// `events.db` without any daemon.
#[tokio::test]
async fn direct_mode_appends_and_reads_without_a_daemon() {
    let dir = tempfile::tempdir().expect("tempdir");
    let _registry_guard = TestRegistryGuard::new(dir.path());
    let db = dir.path().join("events.db");
    let backend = direct_backend_for(&db).expect("direct backend");
    let store = backend.events_for_namespace("local").expect("store");
    store
        .append_event(test_event("local"))
        .await
        .expect("direct append");
    let count = store
        .count_events(EventFilter::default())
        .await
        .expect("count");
    assert_eq!(count, 1);
}