kayactl 0.1.42

Command-line interface for KayaDB: put/get/delete/scan, inspect WAL/SSTable/Manifest, stats, and recovery
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
use std::env;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
use std::time::Duration;

use kaya_core::{DurabilityMode, EngineConfig, KayaError, Result, WalConfig};
use kaya_engine::{
    recover as engine_recover, Engine, EngineStats, ReadOptions, RecoveryReport, ScanOptions,
    WriteOptions,
};
use kaya_io::FileDisk;
use kaya_lsm::{inspect_manifest_path, inspect_sstable_path, ManifestInspection, SstInspection};
use kaya_net::{
    decode_error_payload, decode_scan_response, decode_value_payload, encode_key_payload,
    encode_member_payload, encode_put_payload, encode_remove_member_payload, encode_scan_payload,
    roundtrip, STATUS_ERROR, STATUS_INVALID_ARGUMENT, STATUS_NOT_FOUND, STATUS_NOT_LEADER,
    STATUS_OK,
};
use kaya_wal::{inspect_wal_path, WalInspection};

fn main() {
    if let Err(error) = run() {
        eprintln!("ERROR: {error}");
        process::exit(error.exit_code());
    }
}

fn run() -> Result<()> {
    let mut args = env::args().skip(1).collect::<Vec<_>>();
    let json = remove_flag(&mut args, "--json");

    let server_addrs: Vec<SocketAddr> = remove_all_value_flags(&mut args, "--server")
        .into_iter()
        .map(|s| {
            s.parse::<SocketAddr>()
                .map_err(|e| KayaError::invalid_argument(format!("--server: {e}")))
        })
        .collect::<Result<Vec<_>>>()?;

    let timeout_ms: Option<u64> = remove_value_flag(&mut args, "--timeout")
        .map(|s| {
            s.parse::<u64>()
                .map_err(|e| KayaError::invalid_argument(format!("--timeout: {e}")))
        })
        .transpose()?;
    let timeout = timeout_ms.map(Duration::from_millis);

    let data_dir = remove_value_flag(&mut args, "--data").unwrap_or_else(|| "./data".to_owned());
    let durability = match remove_value_flag(&mut args, "--durability").as_deref() {
        Some("relaxed") => DurabilityMode::Relaxed,
        Some("strict") | None => DurabilityMode::Strict,
        Some(other) => {
            return Err(KayaError::invalid_argument(format!(
                "unknown durability mode: {other}; expected strict or relaxed"
            )));
        }
    };

    let latency_view = remove_flag(&mut args, "--latency");

    // ── eBPF observability (Linux experiments, M12) ───────────────────────────
    // Handled early so it works standalone or alongside --server/--data.
    if !args.is_empty() && args[0] == "ebpf" {
        let sub = if args.len() > 1 {
            args[1].clone()
        } else {
            "help".to_string()
        };
        let pid: Option<u32> = remove_value_flag(&mut args, "--pid").and_then(|s| s.parse().ok());
        let run = remove_flag(&mut args, "--run");
        let duration: Option<String> = remove_value_flag(&mut args, "--duration");
        return handle_ebpf(&sub, pid, run, duration, json);
    }

    // ── server mode ───────────────────────────────────────────────────────────
    if !server_addrs.is_empty() {
        return run_server_mode(args, server_addrs, json, timeout, latency_view);
    }

    // ── local engine mode (unchanged) ─────────────────────────────────────────
    match args.as_slice() {
        [] => {
            print_usage();
            Ok(())
        }
        [cmd] if cmd == "put" => Err(KayaError::invalid_argument(
            "usage: kayactl put <key> <value>",
        )),
        [cmd, key, value] if cmd == "put" => {
            let opts = WriteOptions {
                durability: Some(durability),
                idempotency_key: None,
            };
            let result = block_on(async {
                let mut engine = open_engine(data_dir, durability).await?;
                engine
                    .put(key.as_bytes().to_vec(), value.as_bytes().to_vec(), opts)
                    .await
            })?;
            if json {
                println!(
                    "{{\"ok\":true,\"sequence\":{},\"lsn\":{},\"durable\":{}}}",
                    result.sequence.get(),
                    result.lsn.get(),
                    result.durable
                );
            } else {
                println!(
                    "OK sequence={} lsn={} durable={}",
                    result.sequence.get(),
                    result.lsn.get(),
                    result.durable
                );
            }
            Ok(())
        }
        [cmd] if cmd == "get" => Err(KayaError::invalid_argument("usage: kayactl get <key>")),
        [cmd, key] if cmd == "get" => {
            let value = block_on(async {
                let mut engine = open_engine(data_dir, durability).await?;
                engine.get(key.as_bytes(), ReadOptions::default()).await
            })?;
            match value {
                Some(v) => {
                    let display = String::from_utf8_lossy(&v);
                    if json {
                        println!("{{\"found\":true,\"value\":{}}}", json_string(&display));
                    } else {
                        println!("{display}");
                    }
                    Ok(())
                }
                None => {
                    if json {
                        println!("{{\"found\":false}}");
                    } else {
                        println!("NOT_FOUND");
                    }
                    Err(KayaError::NotFound)
                }
            }
        }
        [cmd] if cmd == "delete" => Err(KayaError::invalid_argument("usage: kayactl delete <key>")),
        [cmd, key] if cmd == "delete" => {
            let opts = WriteOptions {
                durability: Some(durability),
                idempotency_key: None,
            };
            let result = block_on(async {
                let mut engine = open_engine(data_dir, durability).await?;
                engine.delete(key.as_bytes().to_vec(), opts).await
            })?;
            if json {
                println!(
                    "{{\"ok\":true,\"sequence\":{},\"lsn\":{},\"durable\":{}}}",
                    result.sequence.get(),
                    result.lsn.get(),
                    result.durable
                );
            } else {
                println!(
                    "OK sequence={} lsn={} durable={}",
                    result.sequence.get(),
                    result.lsn.get(),
                    result.durable
                );
            }
            Ok(())
        }
        [cmd] if cmd == "scan" => Err(KayaError::invalid_argument("usage: kayactl scan <prefix>")),
        [cmd, prefix] if cmd == "scan" => {
            let items = block_on(async {
                let mut engine = open_engine(data_dir, durability).await?;
                engine
                    .scan_prefix(prefix.as_bytes(), ScanOptions::default())
                    .await
            })?;
            if json {
                print!("{{\"items\":[");
                for (index, kv) in items.iter().enumerate() {
                    if index > 0 {
                        print!(",");
                    }
                    let k = String::from_utf8_lossy(&kv.key);
                    let v = String::from_utf8_lossy(&kv.value);
                    print!(
                        "{{\"key\":{},\"value\":{}}}",
                        json_string(&k),
                        json_string(&v)
                    );
                }
                println!("]}}");
            } else {
                for kv in &items {
                    let k = String::from_utf8_lossy(&kv.key);
                    let v = String::from_utf8_lossy(&kv.value);
                    println!("{k} {v}");
                }
            }
            Ok(())
        }
        [inspect, wal, path] if inspect == "inspect" && wal == "wal" => {
            let inspection = inspect_wal_path(path, WalConfig::default().max_record_bytes)?;
            if json {
                print_inspection_json(&inspection);
            } else {
                print_inspection_human(&inspection);
            }
            Ok(())
        }
        [inspect, sst, path] if inspect == "inspect" && sst == "sstable" => {
            let inspection = inspect_sstable_path(path)?;
            if json {
                print_sst_inspection_json(&inspection);
            } else {
                print_sst_inspection_human(&inspection);
            }
            Ok(())
        }
        [inspect, mani, path] if inspect == "inspect" && mani == "manifest" => {
            let inspection = inspect_manifest_path(path)?;
            if json {
                print_manifest_inspection_json(&inspection);
            } else {
                print_manifest_inspection_human(&inspection);
            }
            Ok(())
        }
        [cmd] if cmd == "stats" => {
            let engine = block_on(open_engine(data_dir, durability))?;
            let stats = engine.stats();
            let recovery = engine.last_recovery().clone();
            if json {
                print_stats_json(&stats, &recovery);
            } else if latency_view {
                print_latency_human(&stats);
            } else {
                print_stats_human(&stats, &recovery);
            }
            Ok(())
        }
        [cmd] if cmd == "flush" => {
            let (flush_res, stats) = block_on(async {
                let mut engine = open_engine(data_dir, durability).await?;
                let r = engine.flush().await?;
                let s = engine.stats();
                Ok::<_, KayaError>((r, s))
            })?;
            if json {
                println!(
                    "{{\"ok\":true,\"memtable_entries\":{},\"sstable_count\":{}}}",
                    flush_res.memtable_entries, flush_res.sstable_count
                );
            } else {
                println!(
                    "OK flushed {} memtable entries. Live SSTables: {}",
                    flush_res.memtable_entries, flush_res.sstable_count
                );
                println!();
                print_latency_human(&stats);
            }
            Ok(())
        }
        [cmd, flag] if cmd == "recover" && flag == "--dry-run" => {
            use kaya_core::DurabilityConfig;
            let config = EngineConfig {
                data_dir: PathBuf::from(&data_dir),
                durability: DurabilityConfig {
                    mode: durability,
                    ..DurabilityConfig::default()
                },
                ..EngineConfig::default()
            };
            let disk = Arc::new(FileDisk::new(config.data_dir.clone()));
            let recovery = block_on(engine_recover(config, disk))?;
            if json {
                print_recovery_json(&recovery);
            } else {
                print_recovery_human(&recovery);
            }
            Ok(())
        }
        _ => Err(KayaError::invalid_argument("unknown kayactl command")),
    }
}

async fn open_engine(
    data_dir: String,
    default_durability: DurabilityMode,
) -> Result<Engine<FileDisk>> {
    use kaya_core::DurabilityConfig;
    let config = EngineConfig {
        data_dir: PathBuf::from(&data_dir),
        durability: DurabilityConfig {
            mode: default_durability,
            ..DurabilityConfig::default()
        },
        ..EngineConfig::default()
    };
    let disk = Arc::new(FileDisk::new(config.data_dir.clone()));
    Engine::open(config, disk).await
}

// ── server-mode dispatch ──────────────────────────────────────────────────────

async fn roundtrip_with_retry(
    endpoints: &[SocketAddr],
    opcode: u8,
    payload: &[u8],
    timeout: Option<Duration>,
) -> Result<(u16, Vec<u8>)> {
    let mut addr_idx = 0;
    let mut redirect_retries = 0;
    let mut current_addr = endpoints[0];

    loop {
        let request = roundtrip(current_addr, opcode, payload);
        let result = match timeout {
            Some(dur) => match tokio::time::timeout(dur, request).await {
                Ok(r) => r,
                Err(_) => {
                    return Err(KayaError::internal(format!(
                        "request to {current_addr} timed out after {}ms",
                        dur.as_millis()
                    )));
                }
            },
            None => request.await,
        };
        let (status, body) = result.map_err(|e| KayaError::internal(e.to_string()))?;

        if status == STATUS_NOT_LEADER && redirect_retries < 3 && !body.is_empty() {
            if let Ok(leader_addr_str) = String::from_utf8(body.clone()) {
                if let Ok(new_addr) = leader_addr_str.parse::<SocketAddr>() {
                    eprintln!("Redirecting to leader at {}...", new_addr);
                    current_addr = new_addr;
                    redirect_retries += 1;
                    continue;
                }
            }
        }

        // Single-node (or early election) case: the node replied NOT_LEADER with
        // no hint because it hasn't finished electing yet. Retry same target briefly.
        if status == STATUS_NOT_LEADER && redirect_retries < 6 {
            tokio::time::sleep(std::time::Duration::from_millis(80)).await;
            redirect_retries += 1;
            continue;
        }

        if status == STATUS_ERROR || status == STATUS_INVALID_ARGUMENT {
            if let Ok(msg) = decode_error_payload(&body) {
                if (msg.contains("connection") || msg.contains("unavailable"))
                    && addr_idx + 1 < endpoints.len()
                {
                    addr_idx += 1;
                    current_addr = endpoints[addr_idx];
                    eprintln!("Trying next endpoint: {current_addr}");
                    continue;
                }
            }
        }

        return Ok((status, body));
    }
}

fn run_server_mode(
    args: Vec<String>,
    endpoints: Vec<SocketAddr>,
    json: bool,
    timeout: Option<Duration>,
    latency_view: bool,
) -> Result<()> {
    block_on(
        async move { run_server_mode_async(args, endpoints, json, timeout, latency_view).await },
    )
}

async fn run_server_mode_async(
    mut args: Vec<String>,
    endpoints: Vec<SocketAddr>,
    json: bool,
    timeout: Option<Duration>,
    latency_view: bool,
) -> Result<()> {
    let _operator_token = remove_value_flag(&mut args, "--operator-token").unwrap_or_default();

    match args.as_slice() {
        [] => {
            print_usage();
            Ok(())
        }
        [cmd] if cmd == "put" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> put <key> <value>",
        )),
        [cmd, key, value] if cmd == "put" => {
            let payload = encode_put_payload(key.as_bytes(), value.as_bytes());
            let (status, body) = roundtrip_with_retry(&endpoints, 1, &payload, timeout).await?;
            match status {
                STATUS_OK => {
                    if json {
                        println!("{{\"ok\":true}}");
                    } else {
                        println!("OK");
                    }
                    Ok(())
                }
                STATUS_NOT_LEADER => Err(KayaError::internal(
                    "not leader — retry on a different node",
                )),
                STATUS_ERROR | STATUS_INVALID_ARGUMENT => {
                    let msg = decode_error_payload(&body).unwrap_or_else(|_| "unknown".into());
                    Err(KayaError::invalid_argument(msg))
                }
                s => Err(KayaError::internal(format!("unexpected status: {s}"))),
            }
        }
        [cmd] if cmd == "get" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> get <key>",
        )),
        [cmd, key] if cmd == "get" => {
            let payload = encode_key_payload(key.as_bytes());
            let (status, body) = roundtrip_with_retry(&endpoints, 2, &payload, timeout).await?;
            match status {
                STATUS_OK => {
                    let value = decode_value_payload(&body).map_err(KayaError::internal)?;
                    let display = String::from_utf8_lossy(&value);
                    if json {
                        println!("{{\"found\":true,\"value\":{}}}", json_string(&display));
                    } else {
                        println!("{display}");
                    }
                    Ok(())
                }
                STATUS_NOT_FOUND => {
                    if json {
                        println!("{{\"found\":false}}");
                    } else {
                        println!("NOT_FOUND");
                    }
                    Err(KayaError::NotFound)
                }
                STATUS_NOT_LEADER => Err(KayaError::internal(
                    "not leader — retry on a different node",
                )),
                STATUS_ERROR | STATUS_INVALID_ARGUMENT => {
                    let msg = decode_error_payload(&body).unwrap_or_else(|_| "unknown".into());
                    Err(KayaError::invalid_argument(msg))
                }
                s => Err(KayaError::internal(format!("unexpected status: {s}"))),
            }
        }
        [cmd] if cmd == "delete" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> delete <key>",
        )),
        [cmd, key] if cmd == "delete" => {
            let payload = encode_key_payload(key.as_bytes());
            let (status, body) = roundtrip_with_retry(&endpoints, 3, &payload, timeout).await?;
            match status {
                STATUS_OK => {
                    if json {
                        println!("{{\"ok\":true}}");
                    } else {
                        println!("OK");
                    }
                    Ok(())
                }
                STATUS_NOT_LEADER => Err(KayaError::internal(
                    "not leader — retry on a different node",
                )),
                STATUS_ERROR | STATUS_INVALID_ARGUMENT => {
                    let msg = decode_error_payload(&body).unwrap_or_else(|_| "unknown".into());
                    Err(KayaError::invalid_argument(msg))
                }
                s => Err(KayaError::internal(format!("unexpected status: {s}"))),
            }
        }
        [cmd] if cmd == "scan" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> scan <prefix>",
        )),
        [cmd, prefix] if cmd == "scan" => {
            let payload = encode_scan_payload(prefix.as_bytes());
            let (status, body) = roundtrip_with_retry(&endpoints, 4, &payload, timeout).await?;
            match status {
                STATUS_OK => {
                    let items = decode_scan_response(&body).map_err(KayaError::internal)?;
                    if json {
                        print!("{{\"items\":[");
                        for (i, (k, v)) in items.iter().enumerate() {
                            if i > 0 {
                                print!(",");
                            }
                            let ks = String::from_utf8_lossy(k);
                            let vs = String::from_utf8_lossy(v);
                            print!(
                                "{{\"key\":{},\"value\":{}}}",
                                json_string(&ks),
                                json_string(&vs)
                            );
                        }
                        println!("]}}");
                    } else {
                        for (k, v) in &items {
                            let ks = String::from_utf8_lossy(k);
                            let vs = String::from_utf8_lossy(v);
                            println!("{ks} {vs}");
                        }
                    }
                    Ok(())
                }
                STATUS_NOT_LEADER => Err(KayaError::internal(
                    "not leader — retry on a different node",
                )),
                STATUS_ERROR | STATUS_INVALID_ARGUMENT => {
                    let msg = decode_error_payload(&body).unwrap_or_else(|_| "unknown".into());
                    Err(KayaError::invalid_argument(msg))
                }
                s => Err(KayaError::internal(format!("unexpected status: {s}"))),
            }
        }
        [cmd] if cmd == "health" => {
            let (status, body) = roundtrip_with_retry(&endpoints, 5, &[], timeout).await?;
            if status == STATUS_OK {
                let role = String::from_utf8_lossy(&body);
                if json {
                    println!("{{\"ok\":true,\"role\":\"{role}\"}}");
                } else {
                    println!("OK role={role}");
                }
                Ok(())
            } else {
                Err(KayaError::internal("health check failed"))
            }
        }
        [cmd] if cmd == "status" => {
            let (status, body) = roundtrip_with_retry(&endpoints, 6, &[], timeout).await?;
            if status == STATUS_OK {
                let stats_str =
                    String::from_utf8(body).map_err(|e| KayaError::corruption(e.to_string()))?;
                if json {
                    println!("{}", stats_str);
                } else if latency_view {
                    // Best-effort: the full human already includes latency fields now.
                    // For a focused view we still go through the extractor (it prints everything relevant).
                    print_human_stats_from_json(&stats_str);
                    // Future: could parse and call a pure latency printer, but enriched human is good.
                } else {
                    print_human_stats_from_json(&stats_str);
                }
                Ok(())
            } else {
                Err(KayaError::internal("status check failed"))
            }
        }
        [cmd] if cmd == "add-node" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> add-node <id> <raft-addr> <client-addr>",
        )),
        [cmd, id, raft_addr, client_addr] if cmd == "add-node" => {
            let node_id: u64 = id
                .parse()
                .map_err(|e| KayaError::invalid_argument(format!("node id: {e}")))?;
            let payload = encode_member_payload(node_id, raft_addr, client_addr);
            let (status, body) = roundtrip_with_retry(&endpoints, 7, &payload, timeout).await?;
            handle_membership_response(status, &body, json, "add-node")
        }
        [cmd] if cmd == "remove-node" => Err(KayaError::invalid_argument(
            "usage: kayactl --server <addr> remove-node <id>",
        )),
        [cmd, id] if cmd == "remove-node" => {
            let node_id: u64 = id
                .parse()
                .map_err(|e| KayaError::invalid_argument(format!("node id: {e}")))?;
            let payload = encode_remove_member_payload(node_id);
            let (status, body) = roundtrip_with_retry(&endpoints, 8, &payload, timeout).await?;
            handle_membership_response(status, &body, json, "remove-node")
        }
        _ => Err(KayaError::invalid_argument(
            "unknown command for --server mode",
        )),
    }
}

fn handle_membership_response(status: u16, body: &[u8], json: bool, op: &str) -> Result<()> {
    match status {
        STATUS_OK => {
            let msg = String::from_utf8_lossy(body);
            if json {
                println!(
                    "{{\"ok\":true,\"op\":\"{op}\",\"message\":{}}}",
                    json_string(&msg)
                );
            } else {
                println!("OK {msg}");
            }
            Ok(())
        }
        STATUS_NOT_LEADER => Err(KayaError::internal(
            "not leader — retry on a different node",
        )),
        STATUS_ERROR | STATUS_INVALID_ARGUMENT => {
            let msg = decode_error_payload(body).unwrap_or_else(|_| "unknown".into());
            Err(KayaError::invalid_argument(msg))
        }
        s => Err(KayaError::internal(format!("unexpected status: {s}"))),
    }
}

fn block_on<F: std::future::Future>(future: F) -> F::Output {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("tokio runtime")
        .block_on(future)
}

fn remove_flag(args: &mut Vec<String>, flag: &str) -> bool {
    let before = args.len();
    args.retain(|arg| arg != flag);
    args.len() != before
}

fn remove_value_flag(args: &mut Vec<String>, flag: &str) -> Option<String> {
    let pos = args.iter().position(|arg| arg == flag)?;
    if pos + 1 < args.len() {
        args.remove(pos);
        Some(args.remove(pos))
    } else {
        None
    }
}

fn remove_all_value_flags(args: &mut Vec<String>, flag: &str) -> Vec<String> {
    let mut values = Vec::new();
    while let Some(v) = remove_value_flag(args, flag) {
        values.push(v);
    }
    values
}

fn print_usage() {
    println!("kayactl — KayaDB command-line tool");
    println!();
    println!("LOCAL ENGINE COMMANDS");
    println!("  kayactl [--data <dir>] [--durability strict|relaxed] [--json] put <key> <value>");
    println!("  kayactl [--data <dir>] [--json] get <key>");
    println!("  kayactl [--data <dir>] [--durability strict|relaxed] [--json] delete <key>");
    println!("  kayactl [--data <dir>] [--json] scan <prefix>");
    println!("  kayactl [--data <dir>] [--durability strict|relaxed] [--json] flush   (force memtable -> SSTable for observability)");
    println!();
    println!("OBSERVABILITY COMMANDS");
    println!("  kayactl [--data <dir>] [--json] [--latency] stats   (add --latency for focused durability + flush/compaction timers)");
    println!("  kayactl [--data <dir>] [--durability ...] [--json] flush   (force publish to see latency numbers move; pairs with --latency and ebpf probes)");
    println!("  kayactl [--data <dir>] [--json] recover --dry-run");
    println!("  kayactl ebpf [fsync-latency|...|list|status|help] [--pid <pid>] [--run] [--duration 30s]   (Linux eBPF experiments, Track A)");
    println!();
    println!("CLUSTER MODE (via running kayadb-server)");
    println!("  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] put <key> <value>");
    println!(
        "  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] get <key>"
    );
    println!(
        "  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] delete <key>"
    );
    println!(
        "  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] scan <prefix>"
    );
    println!("  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] health");
    println!("  kayactl --server <addr> [--server <addr2> ...] [--timeout <ms>] [--json] [--latency] status");
    println!("  kayactl --server <addr> add-node <id> <raft-addr> <client-addr>");
    println!("  kayactl --server <addr> remove-node <id>");
    println!();
    println!("INSPECT COMMANDS");
    println!("  kayactl [--json] inspect wal <path>");
    println!("  kayactl [--json] inspect sstable <path>");
    println!("  kayactl [--json] inspect manifest <path>");
    println!();
    println!("DEFAULTS");
    println!("  --data ./data");
    println!("  --durability strict");
    println!("  --timeout (none)");
}

/// Handle `kayactl ebpf ...` subcommands.
/// Linux eBPF observability experiments (Track A / M12). Non-Linux and missing tools are handled gracefully.
/// If `run` is true we attempt to exec bpftrace (requires appropriate privileges on Linux).
/// `duration` (e.g. "30s") is best-effort for --run (uses `timeout` on Unix if available).
fn handle_ebpf(
    sub: &str,
    explicit_pid: Option<u32>,
    run: bool,
    duration: Option<String>,
    _json: bool,
) -> Result<()> {
    const LINUX_ONLY_MSG: &str = "eBPF probes are Linux-only. This command provides ready-to-run bpftrace commands and guidance (see scripts/ebpf/).";

    if !cfg!(target_os = "linux") {
        println!("{}", LINUX_ONLY_MSG);
        println!(
            "Scripts live in scripts/ebpf/ and work on any Linux machine with bpftrace + sudo."
        );
        return Ok(());
    }

    // Try to auto-detect a KayaDB server PID if none supplied.
    let pid = explicit_pid.or_else(|| {
        // Best-effort: look for kayadb-server first, then kayactl (for local engine tests)
        std::process::Command::new("pgrep")
            .args(["-f", "kayadb-server"])
            .output()
            .ok()
            .and_then(|o| {
                String::from_utf8(o.stdout)
                    .ok()
                    .and_then(|s| s.lines().next().and_then(|l| l.trim().parse::<u32>().ok()))
            })
            .or_else(|| {
                std::process::Command::new("pgrep")
                    .args(["-f", "kayactl"])
                    .output()
                    .ok()
                    .and_then(|o| {
                        String::from_utf8(o.stdout).ok().and_then(|s| {
                            s.lines().next().and_then(|l| l.trim().parse::<u32>().ok())
                        })
                    })
            })
    });

    let pid_str = pid
        .map(|p| p.to_string())
        .unwrap_or_else(|| "<PID>".to_string());
    let pid_hint = if pid.is_some() {
        format!("(auto-detected PID {})", pid_str)
    } else {
        "(pass --pid <N> or run against a live kayadb-server)".to_string()
    };

    match sub {
        "fsync-latency" | "fsync" => {
            println!("KayaDB eBPF: fsync / fdatasync latency (microseconds)");
            println!("PID hint: {}", pid_hint);
            println!();
            println!("Recommended (copy-paste):");
            println!(
                "  sudo bpftrace -p {} scripts/ebpf/fsync-latency.bt",
                pid_str
            );
            println!();
            println!("Alternative one-liner:");
            println!("  sudo bpftrace -e '");
            println!("    kprobe:sys_fsync, kprobe:sys_fdatasync {{ @start[tid] = nsecs; }}");
            println!("    kretprobe:sys_fsync, kretprobe:sys_fdatasync /@start[tid]/ {{");
            println!("      $us = (nsecs - @start[tid]) / 1000;");
            println!("      @fsync_us = hist($us); delete(@start[tid]);");
            println!("    }}' -p {}", pid_str);
            println!();
            println!("See: scripts/ebpf/README.md and spec/docs/observability-spec.md");
            // Attempt to run if bpftrace exists and we have a real PID (best effort, user will see permission errors)
            if pid.is_some()
                && std::process::Command::new("bpftrace")
                    .arg("--version")
                    .output()
                    .is_ok()
            {
                println!("\n[bpftrace found] You can now run the sudo command above.");
            }
            if run {
                return try_run_bpftrace("fsync-latency", &pid_str, duration.as_deref());
            }
            Ok(())
        }
        "block-latency" | "bio" | "block" => {
            println!("KayaDB eBPF: block I/O (device) latency histograms (us)");
            println!("PID hint: {}", pid_hint);
            println!();
            println!("Recommended (copy-paste):");
            println!(
                "  sudo bpftrace -p {} scripts/ebpf/block-io-latency.bt",
                pid_str
            );
            println!();
            println!("This shows time spent in the storage stack / scheduler / device after the fsync syscall.");
            println!("See: scripts/ebpf/README.md");
            if pid.is_some()
                && std::process::Command::new("bpftrace")
                    .arg("--version")
                    .output()
                    .is_ok()
            {
                println!("\n[bpftrace found] Ready to attach.");
            }
            if run {
                return try_run_bpftrace("block-io-latency", &pid_str, duration.as_deref());
            }
            Ok(())
        }
        "syscall-timeline" | "timeline" | "sys" => {
            println!("KayaDB eBPF: syscall timeline (write, fsync, fdatasync, rename, unlink, fsyncdir) + TID correlation");
            println!("PID hint: {}", pid_hint);
            println!();
            println!("Recommended (copy-paste):");
            println!(
                "  sudo bpftrace -p {} scripts/ebpf/syscall-timeline.bt",
                pid_str
            );
            println!();
            println!("This script correlates writes with their fsyncs by TID and shows rename/unlink for flush/compaction publish points.");
            println!("See: scripts/ebpf/README.md (Track A addition)");
            if pid.is_some()
                && std::process::Command::new("bpftrace")
                    .arg("--version")
                    .output()
                    .is_ok()
            {
                println!("\n[bpftrace found] Ready to attach.");
            }
            if run {
                return try_run_bpftrace("syscall-timeline", &pid_str, duration.as_deref());
            }
            Ok(())
        }
        "list" => {
            println!("kayactl ebpf list — active KayaDB / bpftrace processes (best effort)");
            if !cfg!(target_os = "linux") {
                println!("{}", LINUX_ONLY_MSG);
                return Ok(());
            }
            // List kayadb-server instances (cluster friendly)
            println!("\n--- kayadb-server processes ---");
            if let Ok(out) = std::process::Command::new("pgrep")
                .args(["-a", "kayadb-server"])
                .output()
            {
                let s = String::from_utf8_lossy(&out.stdout);
                if s.trim().is_empty() {
                    println!("  (none found)");
                } else {
                    for line in s.lines() {
                        println!("  {}", line.trim());
                    }
                }
            } else {
                println!("  pgrep unavailable");
            }
            // List kayactl (embedded tests)
            println!("\n--- kayactl processes ---");
            if let Ok(out) = std::process::Command::new("pgrep")
                .args(["-a", "kayactl"])
                .output()
            {
                let s = String::from_utf8_lossy(&out.stdout);
                if s.trim().is_empty() {
                    println!("  (none found)");
                } else {
                    for line in s.lines() {
                        println!("  {}", line.trim());
                    }
                }
            }
            // List running bpftrace traces (useful for "status" of traces)
            println!("\n--- bpftrace / trace processes ---");
            if let Ok(out) = std::process::Command::new("pgrep")
                .args(["-a", "bpftrace"])
                .output()
            {
                let s = String::from_utf8_lossy(&out.stdout);
                if s.trim().is_empty() {
                    println!("  (none found — no active traces)");
                } else {
                    for line in s.lines() {
                        println!("  {}", line.trim());
                    }
                }
            } else {
                println!("  pgrep bpftrace unavailable");
            }
            println!("\nUse the printed PIDs with --pid or the auto-detector.");
            Ok(())
        }
        "status" => {
            println!("kayactl ebpf status — quick view of local nodes + any attached traces");
            if !cfg!(target_os = "linux") {
                println!("{}", LINUX_ONLY_MSG);
                return Ok(());
            }
            // Reuse list logic but summarized
            let servers: Vec<String> = std::process::Command::new("pgrep")
                .args(["-f", "kayadb-server"])
                .output()
                .ok()
                .and_then(|o| String::from_utf8(o.stdout).ok())
                .map(|s| {
                    s.lines()
                        .filter(|l| !l.trim().is_empty())
                        .map(|l| l.trim().to_string())
                        .collect()
                })
                .unwrap_or_default();
            println!(
                "Detected kayadb-server PIDs: {}",
                if servers.is_empty() {
                    "(none)".into()
                } else {
                    servers.join(", ")
                }
            );
            let traces: Vec<String> = std::process::Command::new("pgrep")
                .args(["-a", "bpftrace"])
                .output()
                .ok()
                .and_then(|o| String::from_utf8(o.stdout).ok())
                .map(|s| {
                    s.lines()
                        .filter(|l| !l.trim().is_empty())
                        .map(|l| l.trim().to_string())
                        .collect()
                })
                .unwrap_or_default();
            println!(
                "Active bpftrace traces: {}",
                if traces.is_empty() {
                    "0 (no kernel probes attached)".into()
                } else {
                    traces.len().to_string()
                }
            );
            println!("Tip: kayactl ebpf list   for full details.");
            Ok(())
        }
        _ => {
            println!("kayactl ebpf — Linux eBPF observability experiments (Track A / M12)");
            println!();
            println!("Subcommands:");
            println!(
                "  kayactl ebpf fsync-latency [--pid <pid>] [--run]        Trace fsync/fdatasync latency (us)"
            );
            println!(
                "  kayactl ebpf block-latency  [--pid <pid>] [--run]        Trace block device I/O latency (us)"
            );
            println!(
                "  kayactl ebpf syscall-timeline [--pid <pid>] [--run]    Trace writes + fsync/rename etc + simple TID correlation (new)"
            );
            println!("  kayactl ebpf list                                    List local kayadb-server + active bpftrace processes (multi-node friendly)");
            println!("  kayactl ebpf status                                  Summary of nodes + attached traces");
            println!("  kayactl ebpf help");
            println!();
            println!("These commands print ready-to-run bpftrace invocations. Use --run [--duration 30s] to try executing directly (you will likely need sudo).");
            println!("--run improvements: best-effort duration limit via `timeout` (Unix), output shown to terminal; for clusters run multiple in separate terminals or use external timeout.");
            println!("Auto-detect + 'list'/'status' find all kayadb-server PIDs (good for 3-node local clusters).");
            println!("Tip for Track A: `kayactl --data ... flush` (or repeated puts) + `stats --latency` generates visible activity for the probes.");
            println!("They require a Linux host with bpftrace installed and sufficient capabilities (usually sudo).");
            println!();
            println!("Full documentation: scripts/ebpf/README.md");
            println!("Observability spec:   spec/docs/observability-spec.md (section 7)");
            println!("Roadmap:              ROADMAP.md (Track A)");
            if !cfg!(target_os = "linux") {
                println!("\n{}", LINUX_ONLY_MSG);
            } else if pid.is_none() {
                println!("\nTip: start a server first (kayadb-server or scripts/start-cluster.sh), then re-run. Use 'ebpf list' to discover PIDs.");
            }
            Ok(())
        }
    }
}

/// Try to exec the named bpftrace script (best-effort, for developer convenience).
/// On Linux + bpftrace present this will (optionally with duration) run the trace.
/// The user is responsible for privileges (the script will usually need sudo or
/// CAP_BPF+CAP_PERFMON). We deliberately do *not* auto-sudo here.
/// duration: optional e.g. "30s" — on Unix we prefix with `timeout <duration>` if the binary exists.
fn try_run_bpftrace(kind: &str, pid_str: &str, duration: Option<&str>) -> Result<()> {
    if !cfg!(target_os = "linux") {
        eprintln!("--run is only supported on Linux.");
        return Ok(());
    }

    let script = match kind {
        "fsync-latency" => "scripts/ebpf/fsync-latency.bt",
        "block-io-latency" => "scripts/ebpf/block-io-latency.bt",
        "syscall-timeline" => "scripts/ebpf/syscall-timeline.bt",
        _ => {
            eprintln!("unknown eBPF script kind");
            return Ok(());
        }
    };

    if pid_str == "<PID>" {
        eprintln!("Cannot --run without a concrete PID. Use --pid N or ensure a kayadb-server is running for auto-detect.");
        return Ok(());
    }

    // Check bpftrace exists
    if std::process::Command::new("bpftrace")
        .arg("--version")
        .output()
        .is_err()
    {
        eprintln!("bpftrace not found in PATH. Install it first (see scripts/ebpf/README.md).");
        return Ok(());
    }

    eprintln!("WARNING: launching bpftrace for PID {}.", pid_str);
    if let Some(d) = duration {
        eprintln!("Duration limited to {} (best effort via timeout).", d);
    }
    eprintln!("This may require elevated privileges. If it fails with permission errors, re-run the printed sudo command manually.");
    eprintln!("Press Ctrl-C to stop the trace.\n");

    let mut cmd = std::process::Command::new("bpftrace");
    if let Some(d) = duration {
        // Best effort: use timeout(1) when available (common on Linux). Fall back to plain run.
        if std::process::Command::new("timeout")
            .arg("--version")
            .output()
            .is_ok()
        {
            cmd.arg(d); // timeout <duration> bpftrace ...
        } else {
            eprintln!("Note: 'timeout' command not found; running without duration limit.");
        }
    }
    let status = cmd.args(["-p", pid_str, script]).status();

    match status {
        Ok(s) => {
            if !s.success() {
                eprintln!("bpftrace exited with status: {}", s);
            }
        }
        Err(e) => {
            eprintln!("Failed to spawn bpftrace: {}", e);
        }
    }
    Ok(())
}

fn print_human_stats_from_json(json: &str) {
    println!("=== KayaDB Cluster Node Status ===");
    let extract = |key: &str| -> Option<String> {
        let pattern = format!("\"{}\":", key);
        if let Some(pos) = json.find(&pattern) {
            let start = pos + pattern.len();
            let mut end = start;
            let bytes = json.as_bytes();
            let mut in_quotes = false;
            while end < bytes.len() {
                let c = bytes[end] as char;
                if c == '"' {
                    in_quotes = !in_quotes;
                } else if !in_quotes && (c == ',' || c == '}' || c == '{') {
                    break;
                }
                end += 1;
            }
            let val = &json[start..end];
            return Some(val.replace("\"", "").trim().to_string());
        }
        None
    };

    if let Some(role) = extract("role") {
        println!("Role:           {}", role);
    }
    if let Some(term) = extract("term") {
        println!("Term:           {}", term);
    }
    if let Some(commit) = extract("commit_index") {
        println!("Commit Index:   {}", commit);
    }
    if let Some(applied) = extract("applied_index") {
        println!("Applied Index:  {}", applied);
    }
    if let Some(peers) = extract("peer_count") {
        println!("Peer Count:     {}", peers);
    }

    println!("\n--- LSM Storage Engine Metrics ---");
    if let Some(put) = extract("put_count") {
        println!("PUT Operations:       {}", put);
    }
    if let Some(get) = extract("get_count") {
        println!("GET Operations:       {}", get);
    }
    if let Some(delete) = extract("delete_count") {
        println!("DELETE Operations:    {}", delete);
    }
    if let Some(scan) = extract("scan_count") {
        println!("SCAN Operations:      {}", scan);
    }
    if let Some(wal_bytes) = extract("wal_bytes_written") {
        println!("WAL Bytes Written:    {} bytes", wal_bytes);
    }
    if let Some(wal_fsync) = extract("wal_fsync_count") {
        println!("WAL Fsync Count:      {}", wal_fsync);
    }
    if let Some(total_us) = extract("wal_fsync_total_us") {
        println!("WAL Fsync Total Us:   {}", total_us);
    }
    if let Some(max_us) = extract("wal_fsync_max_us") {
        println!("WAL Fsync Max Us:     {}", max_us);
    }
    if let Some(mem_entries) = extract("memtable_entries") {
        println!("Memtable Entries:     {}", mem_entries);
    }
    if let Some(sst_count) = extract("sstable_count") {
        println!("SSTable Count:        {}", sst_count);
    }
    if let Some(last_seq) = extract("last_sequence") {
        println!("Last Sequence Number: {}", last_seq);
    }
    if let Some(f_cnt) = extract("flush_count") {
        println!("Flush Count:            {}", f_cnt);
    }
    if let Some(f_tot) = extract("flush_total_us") {
        println!("Flush Total Us:         {}", f_tot);
    }
    if let Some(f_max) = extract("flush_max_us") {
        println!("Flush Max Us:           {}", f_max);
    }
    if let Some(f_avg) = extract("flush_avg_us") {
        println!("Flush Avg Us:           {}", f_avg);
    }
    if let Some(c_cnt) = extract("compaction_count") {
        println!("Compaction Count:       {}", c_cnt);
    }
    if let Some(c_tot) = extract("compaction_total_us") {
        println!("Compaction Total Us:    {}", c_tot);
    }
    if let Some(c_max) = extract("compaction_max_us") {
        println!("Compaction Max Us:      {}", c_max);
    }
    if let Some(c_avg) = extract("compaction_avg_us") {
        println!("Compaction Avg Us:      {}", c_avg);
    }
    println!("==================================");
}

fn print_stats_human(stats: &EngineStats, recovery: &RecoveryReport) {
    println!("put_count:         {}", stats.put_count);
    println!("get_count:         {}", stats.get_count);
    println!("delete_count:      {}", stats.delete_count);
    println!("scan_count:        {}", stats.scan_count);
    println!("wal_bytes_written: {}", stats.wal_bytes_written);
    println!("wal_fsync_count:   {}", stats.wal_fsync_count);
    println!("wal_fsync_total_us:{}", stats.wal_fsync_total_us);
    println!("wal_fsync_max_us:  {}", stats.wal_fsync_max_us);
    if let Some(avg) = stats.wal_fsync_total_us.checked_div(stats.wal_fsync_count) {
        println!("wal_fsync_avg_us:  {} (total/count)", avg);
    }
    println!("memtable_entries:  {}", stats.memtable_entries);
    println!("sstable_count:     {}", stats.sstable_count);
    println!("last_sequence:     {}", stats.last_sequence);
    // Track A latency metrics
    println!("flush_count:       {}", stats.flush_count);
    println!("flush_total_us:    {}", stats.flush_total_us);
    println!("flush_max_us:      {}", stats.flush_max_us);
    if let Some(avg) = stats.flush_total_us.checked_div(stats.flush_count) {
        println!("flush_avg_us:      {} (total/count)", avg);
    }
    println!("compaction_count:  {}", stats.compaction_count);
    println!("compaction_total_us: {}", stats.compaction_total_us);
    println!("compaction_max_us:   {}", stats.compaction_max_us);
    if let Some(avg) = stats
        .compaction_total_us
        .checked_div(stats.compaction_count)
    {
        println!("compaction_avg_us: {} (total/count)", avg);
    }
    println!();
    println!(
        "recovery.manifest_records_replayed: {}",
        recovery.manifest_records_replayed
    );
    println!(
        "recovery.live_sstable_count:        {}",
        recovery.live_sstable_count
    );
    println!(
        "recovery.wal_records_replayed:      {}",
        recovery.wal_records_replayed
    );
    println!(
        "recovery.wal_truncated_bytes:       {}",
        recovery.wal_truncated_bytes
    );
    println!(
        "recovery.tmp_files_removed:         {}",
        recovery.tmp_files_removed
    );
    println!(
        "recovery.last_lsn:                  {}",
        recovery.last_lsn.map_or(0, |l| l.get())
    );
    println!(
        "recovery.last_sequence:             {}",
        recovery.last_sequence.map_or(0, |s| s.get())
    );
    println!(
        "recovery.records_replayed:          {}",
        recovery.records_replayed
    );
    for w in &recovery.warnings {
        println!("recovery.warning: {w}");
    }
}

fn print_stats_json(stats: &EngineStats, recovery: &RecoveryReport) {
    print!(
        "{{\"put_count\":{},\"get_count\":{},\"delete_count\":{},\"scan_count\":{},",
        stats.put_count, stats.get_count, stats.delete_count, stats.scan_count
    );
    print!(
        "\"wal_bytes_written\":{},\"wal_fsync_count\":{},\"wal_fsync_total_us\":{},\"wal_fsync_max_us\":{},\"memtable_entries\":{},\"sstable_count\":{},\"last_sequence\":{},\"flush_total_us\":{},\"flush_max_us\":{},\"flush_count\":{},\"compaction_total_us\":{},\"compaction_max_us\":{},\"compaction_count\":{},",
        stats.wal_bytes_written, stats.wal_fsync_count, stats.wal_fsync_total_us, stats.wal_fsync_max_us, stats.memtable_entries, stats.sstable_count, stats.last_sequence,
        stats.flush_total_us, stats.flush_max_us, stats.flush_count,
        stats.compaction_total_us, stats.compaction_max_us, stats.compaction_count
    );
    print!(
        "\"recovery\":{{\"manifest_records_replayed\":{},\"live_sstable_count\":{},\"wal_records_replayed\":{},\"wal_truncated_bytes\":{},\"tmp_files_removed\":{},\"last_lsn\":{},\"last_sequence\":{},\"records_replayed\":{},\"warnings\":[",
        recovery.manifest_records_replayed,
        recovery.live_sstable_count,
        recovery.wal_records_replayed,
        recovery.wal_truncated_bytes,
        recovery.tmp_files_removed,
        recovery.last_lsn.map_or(0, |l| l.get()),
        recovery.last_sequence.map_or(0, |s| s.get()),
        recovery.records_replayed
    );
    for (i, w) in recovery.warnings.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        print!("{}", json_string(&w.to_string()));
    }
    println!("]}}}}");
}

/// Focused latency view (Track A). Use with `kayactl [--data] stats --latency` or pipe server status JSON.
fn print_latency_human(stats: &EngineStats) {
    println!("=== KayaDB Latency / Durability (Track A) ===");
    println!("WAL fsyncs (strict durability cost):");
    println!("  count:   {}", stats.wal_fsync_count);
    println!("  total_us:{}", stats.wal_fsync_total_us);
    println!("  max_us:  {}", stats.wal_fsync_max_us);
    if let Some(avg) = stats.wal_fsync_total_us.checked_div(stats.wal_fsync_count) {
        println!("  avg_us:  {avg}");
    }
    println!();
    println!("Flush (memtable -> SSTable publish, manifest, dir fsyncs):");
    println!("  count:   {}", stats.flush_count);
    println!("  total_us:{}", stats.flush_total_us);
    println!("  max_us:  {}", stats.flush_max_us);
    if let Some(avg) = stats.flush_total_us.checked_div(stats.flush_count) {
        println!("  avg_us:  {avg}");
    }
    println!();
    println!("Compaction (L0 merge + publish):");
    println!("  count:   {}", stats.compaction_count);
    println!("  total_us:{}", stats.compaction_total_us);
    println!("  max_us:  {}", stats.compaction_max_us);
    if let Some(avg) = stats
        .compaction_total_us
        .checked_div(stats.compaction_count)
    {
        println!("  avg_us:  {avg}");
    }
    println!();
    println!("Cross-reference with:");
    println!("  kayactl ebpf fsync-latency   (kernel fsync hist)");
    println!("  kayactl ebpf syscall-timeline (write/fsync/rename timeline + TID corr)");
    println!("  (Linux only; see scripts/ebpf/README.md)");
    println!("========================================");
}

fn print_recovery_human(recovery: &RecoveryReport) {
    println!(
        "manifest_records_replayed: {}",
        recovery.manifest_records_replayed
    );
    println!("live_sstable_count:        {}", recovery.live_sstable_count);
    println!(
        "wal_records_replayed:      {}",
        recovery.wal_records_replayed
    );
    println!(
        "wal_truncated_bytes:       {}",
        recovery.wal_truncated_bytes
    );
    println!("tmp_files_removed:         {}", recovery.tmp_files_removed);
    println!(
        "last_lsn:                  {}",
        recovery.last_lsn.map_or(0, |l| l.get())
    );
    println!(
        "last_sequence:             {}",
        recovery.last_sequence.map_or(0, |s| s.get())
    );
    println!("records_replayed:          {}", recovery.records_replayed);
    for w in &recovery.warnings {
        println!("warning: {w}");
    }
    if recovery.warnings.is_empty() {
        println!("warnings:            none");
    }
}

fn print_recovery_json(recovery: &RecoveryReport) {
    print!(
        "{{\"manifest_records_replayed\":{},\"live_sstable_count\":{},\"wal_records_replayed\":{},\"wal_truncated_bytes\":{},\"tmp_files_removed\":{},\"last_lsn\":{},\"last_sequence\":{},\"records_replayed\":{},\"warnings\":[",
        recovery.manifest_records_replayed,
        recovery.live_sstable_count,
        recovery.wal_records_replayed,
        recovery.wal_truncated_bytes,
        recovery.tmp_files_removed,
        recovery.last_lsn.map_or(0, |l| l.get()),
        recovery.last_sequence.map_or(0, |s| s.get()),
        recovery.records_replayed
    );
    for (i, w) in recovery.warnings.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        print!("{}", json_string(&w.to_string()));
    }
    println!("]}}");
}

fn print_inspection_human(inspection: &WalInspection) {
    println!("segment: {}", inspection.segment);
    println!("records: {}", inspection.rows.len());
    println!();
    for row in &inspection.rows {
        match row.value_len {
            Some(value_len) => println!(
                "offset={} lsn={} seq={} type={} key_len={} value_len={} checksum=ok",
                row.offset,
                row.lsn,
                row.sequence,
                row.record_type,
                row.key_len.unwrap_or_default(),
                value_len
            ),
            None => println!(
                "offset={} lsn={} seq={} type={} key_len={} checksum=ok",
                row.offset,
                row.lsn,
                row.sequence,
                row.record_type,
                row.key_len.unwrap_or_default()
            ),
        }
    }
    for warning in &inspection.warnings {
        println!("CORRUPTION {warning}");
    }
}

fn print_inspection_json(inspection: &WalInspection) {
    print!(
        "{{\"segment\":\"{}\",\"records\":[",
        json_escape(&inspection.segment)
    );
    for (index, row) in inspection.rows.iter().enumerate() {
        if index > 0 {
            print!(",");
        }
        print!(
            "{{\"offset\":{},\"lsn\":{},\"sequence\":{},\"type\":\"{}\",\"key_len\":{}",
            row.offset,
            row.lsn,
            row.sequence,
            row.record_type,
            option_usize_json(row.key_len)
        );
        print!(",\"value_len\":{}}}", option_usize_json(row.value_len));
    }
    print!("],\"warnings\":[");
    for (index, warning) in inspection.warnings.iter().enumerate() {
        if index > 0 {
            print!(",");
        }
        print!("\"{}\"", json_escape(&warning.to_string()));
    }
    println!("]}}");
}

fn option_usize_json(value: Option<usize>) -> String {
    value.map_or_else(|| "null".to_owned(), |value| value.to_string())
}

fn json_string(value: &str) -> String {
    format!("\"{}\"", json_escape(value))
}

fn json_escape(value: &str) -> String {
    value
        .chars()
        .flat_map(|ch| match ch {
            '"' => "\\\"".chars().collect::<Vec<_>>(),
            '\\' => "\\\\".chars().collect::<Vec<_>>(),
            '\n' => "\\n".chars().collect::<Vec<_>>(),
            '\r' => "\\r".chars().collect::<Vec<_>>(),
            '\t' => "\\t".chars().collect::<Vec<_>>(),
            other => vec![other],
        })
        .collect()
}

fn print_sst_inspection_human(inspection: &SstInspection) {
    println!("sstable: {}", inspection.path);
    let f = &inspection.footer;
    println!(
        "version={} entries={} min_seq={} max_seq={}",
        f.format_version, f.entry_count, f.table_min_seq, f.table_max_seq
    );
    println!();
    for entry in &inspection.entries {
        let key = String::from_utf8_lossy(&entry.key);
        match &entry.value {
            Some(v) => {
                let value = String::from_utf8_lossy(v);
                println!(
                    "seq={} PUT key={key} value_len={}  {value}",
                    entry.sequence.get(),
                    v.len()
                );
            }
            None => {
                println!("seq={} DEL key={key}", entry.sequence.get());
            }
        }
    }
    for warning in &inspection.warnings {
        println!("WARNING {warning}");
    }
}

fn print_sst_inspection_json(inspection: &SstInspection) {
    let f = &inspection.footer;
    print!(
        "{{\"path\":{},\"version\":{},\"entry_count\":{},\"min_seq\":{},\"max_seq\":{},\"entries\":[",
        json_string(&inspection.path),
        f.format_version,
        f.entry_count,
        f.table_min_seq,
        f.table_max_seq
    );
    for (i, entry) in inspection.entries.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        let key = String::from_utf8_lossy(&entry.key);
        match &entry.value {
            Some(v) => {
                let value = String::from_utf8_lossy(v);
                print!(
                    "{{\"seq\":{},\"type\":\"put\",\"key\":{},\"value\":{}}}",
                    entry.sequence.get(),
                    json_string(&key),
                    json_string(&value)
                );
            }
            None => {
                print!(
                    "{{\"seq\":{},\"type\":\"del\",\"key\":{}}}",
                    entry.sequence.get(),
                    json_string(&key)
                );
            }
        }
    }
    print!("],\"warnings\":[");
    for (i, w) in inspection.warnings.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        print!("{}", json_string(w));
    }
    println!("]}}");
}

fn print_manifest_inspection_human(inspection: &ManifestInspection) {
    println!("manifest: {}", inspection.path);
    let s = &inspection.state;
    println!(
        "live_tables={} last_sequence={} last_edit_seq={}",
        s.live_tables.len(),
        s.last_sequence.get(),
        s.last_edit_seq
    );
    println!();
    for t in &s.live_tables {
        let sk = String::from_utf8_lossy(&t.smallest_key);
        let lk = String::from_utf8_lossy(&t.largest_key);
        println!(
            "table_id={} level={} entries={} path={} min_seq={} max_seq={} smallest={sk} largest={lk}",
            t.table_id, t.level, t.entry_count, t.path,
            t.min_sequence.get(), t.max_sequence.get()
        );
    }
    for warning in &inspection.warnings {
        println!("WARNING {warning}");
    }
}

fn print_manifest_inspection_json(inspection: &ManifestInspection) {
    let s = &inspection.state;
    print!(
        "{{\"path\":{},\"last_sequence\":{},\"last_edit_seq\":{},\"live_tables\":[",
        json_string(&inspection.path),
        s.last_sequence.get(),
        s.last_edit_seq
    );
    for (i, t) in s.live_tables.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        let sk = String::from_utf8_lossy(&t.smallest_key);
        let lk = String::from_utf8_lossy(&t.largest_key);
        print!(
            "{{\"table_id\":{},\"level\":{},\"path\":{},\"entries\":{},\"min_seq\":{},\"max_seq\":{},\"smallest\":{},\"largest\":{}}}",
            t.table_id,
            t.level,
            json_string(&t.path),
            t.entry_count,
            t.min_sequence.get(),
            t.max_sequence.get(),
            json_string(&sk),
            json_string(&lk)
        );
    }
    print!("],\"warnings\":[");
    for (i, w) in inspection.warnings.iter().enumerate() {
        if i > 0 {
            print!(",");
        }
        print!("{}", json_string(&w.to_string()));
    }
    println!("]}}");
}