second-brain-cli 0.10.0

Universal memory layer for AI coding agents — ingest, recall, and sync across Claude Code, Cursor, and more
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
use std::path::PathBuf;

use anyhow::{Context, Result};

mod consolidate;
mod tokens;

fn main() {
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive("second_brain=info".parse().unwrap()),
        )
        .init();

    let args: Vec<String> = std::env::args().collect();
    let cmd = args.get(1).map(|s| s.as_str()).unwrap_or("help");

    if let Err(e) = run(cmd, &args) {
        eprintln!("error: {e:#}");
        std::process::exit(1);
    }
}

fn run(cmd: &str, args: &[String]) -> Result<()> {
    match cmd {
        "ingest" => {
            let source = args.get(2).map(|s| s.as_str()).unwrap_or("claude-code");
            cmd_ingest(source)
        }
        "recall" => {
            let query = args.get(2).map(|s| s.as_str()).unwrap_or("");
            cmd_recall(query)
        }
        "remember" => {
            let content = args[2..].join(" ");
            cmd_remember(&content)
        }
        "status" => cmd_status(),
        "whoami" => cmd_whoami(),
        "setup" => cmd_setup(),
        "watch" => {
            eprintln!("watch is deprecated — the daemon handles file watching automatically");
            Ok(())
        }
        "sync" => {
            let peer = args
                .get(2)
                .ok_or_else(|| anyhow::anyhow!("usage: sb sync <peer-host>"))?;
            cmd_sync(peer, args)
        }
        "sync-export" => {
            let after_seq = parse_after_seq(args);
            cmd_sync_export(after_seq)
        }
        "sync-import" => cmd_sync_import(),
        "sync-status" => cmd_sync_status(),
        "sync-key" => cmd_sync_key(),
        "sync-backfill" => cmd_sync_backfill(),
        "backfill-session-dates" => {
            let dry_run = args.iter().any(|a| a == "--dry-run");
            let reemit = args.iter().any(|a| a == "--reemit");
            cmd_backfill_session_dates(dry_run, reemit)
        }
        "sync-compact" => {
            let dry_run = args.iter().any(|a| a == "--dry-run");
            let ops: Vec<String> = args
                .windows(2)
                .find(|w| w[0] == "--ops")
                .map(|w| w[1].split(',').map(|s| s.trim().to_string()).collect())
                .unwrap_or_else(|| vec!["delete".to_string()]);
            cmd_sync_compact(&ops, dry_run)
        }
        "prune-relations" => cmd_prune_relations(),
        "doctor" => {
            let repair = args.iter().any(|a| a == "--repair");
            let dry_run = args.iter().any(|a| a == "--dry-run");
            cmd_doctor(repair, dry_run)
        }
        "vacuum" => cmd_vacuum(),
        "sync-trust" => {
            let host = args
                .get(2)
                .ok_or_else(|| anyhow::anyhow!("usage: sb sync-trust <host>"))?;
            cmd_sync_trust(host)
        }
        "start" => cmd_daemon("start"),
        "stop" => cmd_daemon("stop"),
        "restart" => cmd_daemon("restart"),
        "reingest" => {
            let clean = args.iter().any(|a| a == "--clean");
            cmd_reingest(clean)
        }
        "consolidate" => {
            let dry_run = args.iter().any(|a| a == "--dry-run");
            let limit = args
                .windows(2)
                .find(|w| w[0] == "--limit")
                .and_then(|w| w[1].parse().ok())
                .unwrap_or(50);
            let model = args
                .windows(2)
                .find(|w| w[0] == "--model")
                .map(|w| w[1].clone())
                .unwrap_or_else(|| "gemma-4-E4B-it-Q4_K_M".to_string());
            let llm_url = args
                .windows(2)
                .find(|w| w[0] == "--llm-url")
                .map(|w| w[1].clone())
                .unwrap_or_else(|| "http://localhost:8081".to_string());
            let concurrency: usize = args
                .windows(2)
                .find(|w| w[0] == "--concurrency")
                .and_then(|w| w[1].parse().ok())
                .unwrap_or(3);
            let guard = args
                .windows(2)
                .find(|w| w[0] == "--guard")
                .map(|w| consolidate::Guard::parse(&w[1]))
                .transpose()?
                .unwrap_or(consolidate::Guard::Retry);
            consolidate::cmd_consolidate(dry_run, limit, &model, &llm_url, concurrency, guard)
        }
        "daily-pipeline" => {
            let llm_url = args
                .windows(2)
                .find(|w| w[0] == "--llm-url")
                .map(|w| w[1].clone())
                .unwrap_or_else(|| "http://localhost:8081".to_string());
            let model = args
                .windows(2)
                .find(|w| w[0] == "--model")
                .map(|w| w[1].clone())
                .unwrap_or_else(|| "gemma-4-E4B-it-Q4_K_M".to_string());
            let consolidate_limit: usize = args
                .windows(2)
                .find(|w| w[0] == "--consolidate-limit")
                .and_then(|w| w[1].parse().ok())
                .unwrap_or(200);
            let enhance = args.iter().any(|a| a == "--enhance");

            cmd_daily_pipeline(&llm_url, &model, consolidate_limit, enhance)
        }
        _ => {
            println!("second-brain CLI");
            println!();
            println!("Usage: sb <command> [args]");
            println!();
            println!("Commands:");
            println!(
                "  ingest [source]     Ingest conversation history (claude-code, cursor, all)"
            );
            println!("  recall <query>      Search the brain");
            println!("  remember <text>     Store new knowledge");
            println!(
                "  setup               One-command install: systemd service, MCP config, first ingest"
            );
            println!("  start               Start the daemon");
            println!("  stop                Stop the daemon");
            println!("  restart             Restart the daemon");
            println!("  status              Show brain stats");
            println!("  sync <peer>         Bidirectional sync with a peer");
            println!("  sync-export         Export changes as JSONL to stdout");
            println!("  sync-import         Import JSONL changes from stdin");
            println!("  sync-status         Show sync state for all peers");
            println!("  sync-key            Print the base64 sync key to copy to paired devices");
            println!("  sync-backfill       Backfill sync log for existing data");
            println!(
                "  backfill-session-dates  Date memories from this machine's transcripts (--dry-run, --reemit)"
            );
            println!(
                "  sync-compact        Collapse duplicate sync_log rows (--dry-run, --ops delete,create)"
            );
            println!("  prune-relations     Delete relations whose endpoints no longer resolve");
            println!(
                "  doctor              Report store integrity (--repair to fix, --dry-run to preview)"
            );
            println!("  vacuum              Reclaim free pages in brain.db after a repair");
            println!(
                "  sync-trust <host>   Fetch and pin a host's SSH key after fingerprint review"
            );
            println!(
                "  reingest [--clean]  Re-ingest all conversations (--clean removes old memories first)"
            );
            println!("  consolidate         Distill raw memories via local LLM");
            println!("  daily-pipeline      End-of-day: consolidate → daily → export → enhance");
            Ok(())
        }
    }
}

fn cmd_ingest(_source: &str) -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(600))
        .build()?;

    eprintln!("ingesting via daemon...");

    let resp = client
        .post(format!("{api}/v1/ingest"))
        .json(&serde_json::json!({}))
        .send()?;

    if !resp.status().is_success() {
        anyhow::bail!(
            "ingest failed: {} {}",
            resp.status(),
            resp.text().unwrap_or_default()
        );
    }

    let body: serde_json::Value = resp.json()?;
    eprintln!(
        "ingested: {} memories, {} entities, {} conversations ({} dupes, {} unchanged, {} errors)",
        body["new_memories"],
        body["new_entities"],
        body["new_conversations"],
        body["skipped_dupes"],
        body["unchanged"],
        body["errors"],
    );

    Ok(())
}
fn cmd_reingest(clean: bool) -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(600))
        .build()?;

    eprintln!("reingest via daemon (clean={clean})...");

    let resp = client
        .post(format!("{api}/v1/reingest"))
        .json(&serde_json::json!({"clean": clean}))
        .send()?;

    if !resp.status().is_success() {
        anyhow::bail!(
            "reingest failed: {} {}",
            resp.status(),
            resp.text().unwrap_or_default()
        );
    }

    let body: serde_json::Value = resp.json()?;
    if clean {
        eprintln!(
            "cleaned {} claude-code + {} cursor memories",
            body["cleaned_claude"], body["cleaned_cursor"]
        );
    }
    eprintln!("cleared {} ingest log entries", body["cleared_log"]);
    eprintln!(
        "ingested: {} memories, {} entities, {} conversations ({} dupes, {} unchanged, {} errors)",
        body["new_memories"],
        body["new_entities"],
        body["new_conversations"],
        body["skipped_dupes"],
        body["unchanged"],
        body["errors"],
    );

    Ok(())
}
fn cmd_recall(query: &str) -> Result<()> {
    if query.is_empty() {
        anyhow::bail!("usage: sb recall <query>");
    }

    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(30))
        .build()?;

    let resp = client
        .post(format!("{api}/v1/recall"))
        .json(&serde_json::json!({ "query": query, "limit": 10 }))
        .send()
        .context("daemon not reachable — is it running?")?
        .error_for_status()
        .context("recall request failed")?;

    let body: serde_json::Value = resp.json()?;
    let memories = body["memories"].as_array();

    if memories.is_none() || memories.unwrap().is_empty() {
        println!("no memories found for: \"{query}\"");
        return Ok(());
    }

    for (i, m) in memories.unwrap().iter().enumerate() {
        let score = m["score"]
            .as_f64()
            .map(|s| format!("score: {:.4}, ", s))
            .unwrap_or_default();
        let mtype = m["memory_type"].as_str().unwrap_or("unknown");
        let source = m["source"].as_str().unwrap_or("unknown");
        let machine = m["machine_name"].as_str().unwrap_or("");
        let project = m["project_path"].as_str().unwrap_or("");
        let content = m["content"].as_str().unwrap_or("");
        let preview: String = content.chars().take(200).collect();

        let mut meta = format!("{score}type: {mtype}, source: {source}");
        if !project.is_empty() {
            meta.push_str(&format!(", project: {project}"));
        }
        if !machine.is_empty() {
            meta.push_str(&format!(", machine: {machine}"));
        }
        println!("{}. ({meta})", i + 1);
        println!("   {preview}\n");
    }

    Ok(())
}

fn cmd_remember(content: &str) -> Result<()> {
    if content.is_empty() {
        anyhow::bail!("usage: sb remember <text>");
    }

    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(30))
        .build()?;

    let resp = client
        .post(format!("{api}/v1/remember"))
        .json(&serde_json::json!({
            "content": content,
            "source": "cli",
        }))
        .send()
        .context("daemon not reachable — is it running?")?
        .error_for_status()
        .context("remember request failed")?;

    let body: serde_json::Value = resp.json()?;
    let id = body["id"].as_str().unwrap_or("unknown");
    println!("remembered [{id}]: {content}");
    Ok(())
}

fn cmd_daemon(action: &str) -> Result<()> {
    let status = std::process::Command::new("systemctl")
        .args(["--user", action, "second-brain"])
        .status()
        .context("failed to run systemctl")?;
    if !status.success() {
        anyhow::bail!("systemctl --user {action} second-brain failed");
    }
    Ok(())
}

fn cmd_status() -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(5))
        .build()?;

    let resp = client.get(format!("{api}/v1/status")).send();

    match resp {
        Ok(r) if r.status().is_success() => {
            let body: serde_json::Value = r.json()?;
            println!("memories: {}", body["memory_count"]);
            println!("ingested files: {}", body["ingested_file_count"]);
            println!("version: {}", body["version"]);
            println!("embedder: {}", body["embedder_available"]);
            let c = &body["consolidation"];
            if c.is_object() {
                println!(
                    "consolidation: {} distilled, {} raw pending, {} retired, {} awaiting retirement",
                    c["distilled_live"],
                    c["raw_pending"],
                    c["raw_retired"],
                    c["raw_retire_pending"]
                );
            }
            match &body["maintenance"] {
                m if m.is_object() => {
                    println!("last repair: {}", m["ran_at"].as_str().unwrap_or("?"));
                    for (name, value) in drift_fields(&m["report_after"]) {
                        println!("WARN {name}: {value}");
                    }
                }
                _ => println!("last repair: none since the daemon started"),
            }
            if let Some(diag) = body["diagnostic"].as_str()
                && !diag.is_empty()
            {
                println!("\n{diag}");
            }
        }
        Ok(r) => anyhow::bail!("daemon returned {}", r.status()),
        Err(_) => anyhow::bail!("daemon not reachable at {api} — is it running?"),
    }
    Ok(())
}

fn cmd_whoami() -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(5))
        .build()?;
    let body: serde_json::Value = client
        .get(format!("{api}/v1/status"))
        .send()
        .context("daemon not reachable")?
        .error_for_status()?
        .json()?;
    let id = body["machine_id"]
        .as_str()
        .context("daemon response missing machine_id")?;
    println!("{id}");
    Ok(())
}

fn cmd_setup() -> Result<()> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let home = PathBuf::from(&home);

    eprintln!("=== second-brain setup ===\n");

    // 1. Create data directory
    let db_dir = home.join(".second-brain");
    std::fs::create_dir_all(&db_dir)?;
    eprintln!("[1/5] data dir: {}", db_dir.display());

    // 2. Locate binaries
    let (api_bin, mcp_bin, cli_bin) = locate_binaries()?;
    eprintln!("[2/5] binaries:");
    eprintln!("  api: {}", api_bin.display());
    eprintln!("  mcp: {}", mcp_bin.display());
    eprintln!("  cli: {}", cli_bin.display());

    // 3. Install systemd user service
    let systemd_dir = home.join(".config/systemd/user");
    std::fs::create_dir_all(&systemd_dir)?;
    let service_path = systemd_dir.join("second-brain.service");
    let db_path = db_dir.join("brain.db");

    let service_content = format!(
        "[Unit]\n\
         Description=Second Brain daemon (KuzuDB, embedder, file watcher)\n\
         After=network.target\n\
         \n\
         [Service]\n\
         Type=simple\n\
         ExecStart={api}\n\
         Environment=SECOND_BRAIN_DB={db}\n\
         Environment=SECOND_BRAIN_BIND=127.0.0.1:7200\n\
         Restart=on-failure\n\
         RestartSec=5\n\
         TimeoutStartSec=60\n\
         \n\
         [Install]\n\
         WantedBy=default.target\n",
        api = api_bin.display(),
        db = db_path.display(),
    );

    std::fs::write(&service_path, &service_content)?;
    eprintln!("[3/5] systemd service: {}", service_path.display());

    let reload = std::process::Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .status();
    if reload.is_ok_and(|s| s.success()) {
        let enable = std::process::Command::new("systemctl")
            .args(["--user", "enable", "--now", "second-brain.service"])
            .status();
        match enable {
            Ok(s) if s.success() => eprintln!("  enabled and started"),
            _ => eprintln!("  warning: could not enable service (systemd not available?)"),
        }
    } else {
        eprintln!(
            "  warning: systemctl not available, start daemon manually: {}",
            api_bin.display()
        );
    }

    // 4. Configure Claude Code MCP
    let claude_dir = home.join(".claude");
    std::fs::create_dir_all(&claude_dir)?;
    let settings_path = claude_dir.join("settings.json");

    let mut settings: serde_json::Value = if settings_path.exists() {
        let content = std::fs::read_to_string(&settings_path)?;
        serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}))
    } else {
        serde_json::json!({})
    };

    let mcp_servers = settings
        .as_object_mut()
        .unwrap()
        .entry("mcpServers")
        .or_insert_with(|| serde_json::json!({}));

    mcp_servers["second-brain"] = serde_json::json!({
        "command": mcp_bin.to_string_lossy(),
        "env": {
            "SECOND_BRAIN_API": "http://127.0.0.1:7200",
            "SECOND_BRAIN_DAEMON_BIN": api_bin.to_string_lossy(),
        }
    });

    std::fs::write(&settings_path, serde_json::to_string_pretty(&settings)?)?;
    eprintln!("[4/5] claude code MCP: {}", settings_path.display());

    // 5. Wait for daemon, run first ingest
    eprintln!("[5/5] running first ingest...");
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(600))
        .build()?;

    let api = api_base_url();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    let mut ready = false;
    while std::time::Instant::now() < deadline {
        if client
            .get(format!("{api}/health"))
            .timeout(std::time::Duration::from_secs(2))
            .send()
            .is_ok_and(|r| r.status().is_success())
        {
            ready = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(500));
    }

    if !ready {
        eprintln!("  daemon not ready after 30s — run 'second-brain-cli ingest' later");
        return Ok(());
    }

    match client
        .post(format!("{api}/v1/ingest"))
        .json(&serde_json::json!({}))
        .send()
    {
        Ok(resp) if resp.status().is_success() => {
            let body: serde_json::Value = resp.json().unwrap_or_default();
            eprintln!(
                "  ingested: {} memories, {} conversations",
                body["new_memories"], body["new_conversations"]
            );
        }
        Ok(resp) => eprintln!("  ingest warning: {}", resp.status()),
        Err(e) => eprintln!("  ingest warning: {e}"),
    }

    eprintln!("\n=== setup complete ===");
    eprintln!("daemon:  systemctl --user status second-brain");
    eprintln!("recall:  sb recall \"your query\"");
    eprintln!("status:  sb status");
    eprintln!("\nRun your bootstrapper (harness) for wiki vault + daily timer setup.");

    Ok(())
}

fn locate_binaries() -> Result<(PathBuf, PathBuf, PathBuf)> {
    let cli_exe = std::env::current_exe().context("cannot determine own binary path")?;
    let bin_dir = cli_exe.parent().unwrap();

    let api_candidate = bin_dir.join("second-brain-api");
    let mcp_candidate = bin_dir.join("second-brain-mcp");

    let api_bin = if api_candidate.exists() {
        api_candidate
    } else {
        which_bin("second-brain-api")?
    };

    let mcp_bin = if mcp_candidate.exists() {
        mcp_candidate
    } else {
        which_bin("second-brain-mcp")?
    };

    Ok((api_bin, mcp_bin, cli_exe))
}

fn which_bin(name: &str) -> Result<PathBuf> {
    let home = std::env::var("HOME").unwrap_or_default();
    let cargo_bin = PathBuf::from(&home).join(".cargo/bin").join(name);
    if cargo_bin.exists() {
        return Ok(cargo_bin);
    }

    let output = std::process::Command::new("which")
        .arg(name)
        .output()
        .context(format!("{name} not found"))?;

    if output.status.success() {
        let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
        Ok(PathBuf::from(path))
    } else {
        anyhow::bail!(
            "{name} not found in PATH or ~/.cargo/bin — install with: cargo install {name}"
        )
    }
}

fn parse_after_seq(args: &[String]) -> u64 {
    for (i, arg) in args.iter().enumerate() {
        if arg == "--after-seq"
            && let Some(val) = args.get(i + 1)
        {
            return val.parse().unwrap_or(0);
        }
    }
    0
}

fn cmd_sync_trust(host: &str) -> Result<()> {
    use std::io::Write;

    let scan = std::process::Command::new("ssh-keyscan")
        .arg("-T")
        .arg("10")
        .arg(host)
        .output()
        .context("running ssh-keyscan (is openssh-client installed?)")?;

    if !scan.status.success() {
        let stderr = String::from_utf8_lossy(&scan.stderr);
        anyhow::bail!(
            "ssh-keyscan {host} failed (exit {}): {}",
            scan.status,
            stderr.trim()
        );
    }

    let raw = String::from_utf8(scan.stdout).context("ssh-keyscan returned non-UTF8 output")?;
    let lines = second_brain_sync::ssh::parse_keyscan_output(&raw);

    if lines.is_empty() {
        anyhow::bail!("ssh-keyscan {host} returned no host keys — host unreachable or filtered");
    }

    // pipe the canonical keyscan lines through ssh-keygen -lf - so the displayed
    // fingerprint exactly matches what OpenSSH would compute on connect.
    let canonical: String = lines
        .iter()
        .map(second_brain_sync::ssh::compose_known_host_entry)
        .collect();

    let mut fp_child = std::process::Command::new("ssh-keygen")
        .arg("-lf")
        .arg("-")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::inherit())
        .spawn()
        .context("running ssh-keygen (is openssh-client installed?)")?;

    {
        let stdin = fp_child.stdin.take().unwrap();
        let mut writer = std::io::BufWriter::new(stdin);
        writer.write_all(canonical.as_bytes())?;
    }

    let fp_out = fp_child.wait_with_output()?;
    if !fp_out.status.success() {
        anyhow::bail!("ssh-keygen -lf failed: exit {}", fp_out.status);
    }
    let fingerprints = String::from_utf8_lossy(&fp_out.stdout);

    eprintln!("Host: {host}");
    eprintln!("Fetched {} key(s). Fingerprints:", lines.len());
    for fp_line in fingerprints.lines() {
        eprintln!("  {fp_line}");
    }
    eprintln!();
    eprintln!("Verify these fingerprints out-of-band before trusting them.");
    eprint!("Pin to ~/.second-brain/known_hosts? [y/N] ");
    std::io::stderr().flush().ok();

    let mut answer = String::new();
    std::io::stdin()
        .read_line(&mut answer)
        .context("reading confirmation")?;
    let trimmed = answer.trim();
    if !trimmed.eq_ignore_ascii_case("y") && !trimmed.eq_ignore_ascii_case("yes") {
        eprintln!("aborted; nothing written.");
        return Ok(());
    }

    let path = second_brain_sync::ssh::known_hosts_path()?;
    second_brain_sync::ssh::append_known_host(&path, &lines)?;
    eprintln!(
        "pinned {} key(s) for {host} -> {}",
        lines.len(),
        path.display()
    );
    Ok(())
}

fn cmd_sync(peer_host: &str, args: &[String]) -> Result<()> {
    let api_base = api_base_url();

    let mut sb_binary = "sb".to_string();
    for (i, arg) in args.iter().enumerate() {
        if arg == "--sb-path"
            && let Some(val) = args.get(i + 1)
        {
            sb_binary = val.clone();
        }
    }

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(600))
        .build()?;

    let peer_machine_id = resolve_peer_machine_id(peer_host, &sb_binary)
        .context("resolving peer machine id (is the peer's daemon running and reachable?)")?;

    eprintln!("pulling from {peer_host}...");
    let pull_stats =
        sync_pull_via_daemon(&client, &api_base, peer_host, &peer_machine_id, &sb_binary)?;
    eprintln!(
        "  {} created, {} updated, {} deleted, {} skipped",
        pull_stats.created, pull_stats.updated, pull_stats.deleted, pull_stats.skipped
    );
    if pull_stats.dangling > 0 {
        eprintln!(
            "  warning: {} records dropped with unresolvable endpoints; \
             run `sb prune-relations` on the peer if this persists",
            pull_stats.dangling
        );
    }

    eprintln!("pushing to {peer_host}...");
    let max_seq =
        sync_push_via_daemon(&client, &api_base, peer_host, &peer_machine_id, &sb_binary)?;
    eprintln!("  pushed through seq {max_seq}");

    eprintln!("sync complete.");
    Ok(())
}

/// Resolves the peer's stable machine id over the same ssh channel sync
/// already uses, so sync_state can be keyed by identity instead of the raw
/// `sync-peers` connection string: a 2026-07-28 edit from `pi.local` to
/// `anyesh@pi.local` silently reset both cursors to zero and forced a full
/// re-pull of the peer's entire history, which is what seeded the
/// 2026-08-24 sync_log bloat incident. Keying by machine id makes editing
/// that file safe.
fn resolve_peer_machine_id(peer_host: &str, sb_binary: &str) -> Result<String> {
    let known_hosts = second_brain_sync::ssh::known_hosts_path()?;
    let output = std::process::Command::new("ssh")
        .args(second_brain_sync::ssh::ssh_args(&known_hosts))
        .arg(peer_host)
        .arg(sb_binary)
        .arg("whoami")
        .output()
        .context("spawning ssh to resolve peer machine id")?;
    if !output.status.success() {
        anyhow::bail!(
            "ssh whoami on {peer_host} exited with {}: {}",
            output.status,
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }
    let id = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if id.is_empty() {
        anyhow::bail!("peer {peer_host} returned an empty machine id");
    }
    Ok(id)
}

fn api_base_url() -> String {
    std::env::var("SECOND_BRAIN_API").unwrap_or_else(|_| "http://127.0.0.1:7200".to_string())
}

fn sync_pull_via_daemon(
    client: &reqwest::blocking::Client,
    api_base: &str,
    peer_host: &str,
    peer_machine_id: &str,
    sb_binary: &str,
) -> Result<SyncImportStats> {
    #[derive(serde::Deserialize)]
    struct PeerInfo {
        peers: Vec<PeerState>,
    }
    #[derive(serde::Deserialize)]
    struct PeerState {
        peer_id: String,
        last_seq: u64,
    }

    let status: PeerInfo = client
        .get(format!("{api_base}/v1/sync/status"))
        .send()
        .context("getting sync status")?
        .json()?;

    // Prefer the machine-id-keyed cursor; fall back to the legacy
    // peer_host-keyed one so migrating to identity-based keys adopts the
    // existing position instead of resetting to 0 and re-pulling the peer's
    // entire history, the exact failure this rekey exists to prevent.
    let after_seq = status
        .peers
        .iter()
        .find(|p| p.peer_id == peer_machine_id)
        .or_else(|| status.peers.iter().find(|p| p.peer_id == peer_host))
        .map(|p| p.last_seq)
        .unwrap_or(0);

    let known_hosts = second_brain_sync::ssh::known_hosts_path()?;
    let mut child = std::process::Command::new("ssh")
        .args(second_brain_sync::ssh::ssh_args(&known_hosts))
        .arg(peer_host)
        .arg(sb_binary)
        .arg("sync-export")
        .arg("--after-seq")
        .arg(after_seq.to_string())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::inherit())
        .spawn()
        .context("spawning ssh for pull")?;

    let stdout = child.stdout.take().unwrap();
    let reader = std::io::BufReader::new(stdout);

    #[derive(serde::Deserialize)]
    struct ImportResp {
        created: u64,
        updated: u64,
        deleted: u64,
        skipped: u64,
        #[allow(dead_code)]
        errors: u64,
        max_seq: u64,
        #[serde(default)]
        skipped_by_reason: std::collections::BTreeMap<String, u64>,
    }

    // Post the import in bounded chunks as the ssh stream is read, instead of
    // buffering the peer's whole changelog into one Vec first: a cold or long-lagging
    // peer's export can be tens of millions of records, and holding all of them as
    // parsed serde_json::Value before posting anything grows RSS without bound and
    // OOMs the puller. The export stream is globally ordered so dependencies land in
    // an earlier-or-same chunk, and the import is idempotent on re-runs.
    const IMPORT_CHUNK: usize = 1000;
    let encryptor = default_sync_encryptor()?;
    let mut totals = SyncImportStats {
        created: 0,
        updated: 0,
        deleted: 0,
        skipped: 0,
        dangling: 0,
    };
    let mut overall_max_seq = 0u64;
    let mut chunk = Vec::with_capacity(IMPORT_CHUNK);

    let post_chunk = |chunk: &[serde_json::Value],
                      client: &reqwest::blocking::Client,
                      totals: &mut SyncImportStats,
                      overall_max_seq: &mut u64|
     -> Result<()> {
        if chunk.is_empty() {
            return Ok(());
        }
        let resp: ImportResp = client
            .post(format!("{api_base}/v1/sync/import"))
            .json(&serde_json::json!({"records": chunk}))
            .send()
            .context("posting import to daemon")?
            .error_for_status()
            .context("import returned error")?
            .json()?;

        totals.created += resp.created;
        totals.updated += resp.updated;
        totals.deleted += resp.deleted;
        totals.skipped += resp.skipped;
        totals.dangling += resp
            .skipped_by_reason
            .get("dangling_endpoint")
            .copied()
            .unwrap_or(0);
        if resp.max_seq > *overall_max_seq {
            *overall_max_seq = resp.max_seq;
        }
        Ok(())
    };

    for line in std::io::BufRead::lines(reader) {
        let line = line?;
        if line.trim().is_empty() {
            continue;
        }
        let plaintext = second_brain_sync::crypto::open_line(encryptor.as_ref(), &line)?;
        let val: serde_json::Value = serde_json::from_slice(&plaintext)?;
        chunk.push(val);
        if chunk.len() >= IMPORT_CHUNK {
            post_chunk(&chunk, client, &mut totals, &mut overall_max_seq)?;
            chunk.clear();
        }
    }
    post_chunk(&chunk, client, &mut totals, &mut overall_max_seq)?;
    chunk.clear();

    let status = child.wait()?;
    if !status.success() {
        anyhow::bail!(
            "ssh pull from {peer_host} exited with {status}.\n  hint: {}",
            second_brain_sync::ssh::untrusted_host_hint(peer_host)
        );
    }

    if overall_max_seq > after_seq {
        client
            .post(format!("{api_base}/v1/sync/state"))
            .json(&serde_json::json!({"peer_id": peer_machine_id, "last_seq": overall_max_seq}))
            .send()
            .context("updating sync state")?;
    }

    Ok(totals)
}

struct SyncImportStats {
    created: u64,
    updated: u64,
    deleted: u64,
    skipped: u64,
    dangling: u64,
}

fn sync_push_via_daemon(
    client: &reqwest::blocking::Client,
    api_base: &str,
    peer_host: &str,
    peer_machine_id: &str,
    sb_binary: &str,
) -> Result<u64> {
    let push_peer = format!("push:{}", peer_machine_id);
    let legacy_push_peer = format!("push:{}", peer_host);
    const BATCH_SIZE: usize = 2000;

    #[derive(serde::Deserialize)]
    struct PeerInfo {
        peers: Vec<PeerState>,
    }
    #[derive(serde::Deserialize)]
    struct PeerState {
        peer_id: String,
        last_seq: u64,
    }

    let status: PeerInfo = client
        .get(format!("{api_base}/v1/sync/status"))
        .send()
        .context("getting sync status")?
        .json()?;

    // Same migration fallback as the pull side: adopt the legacy
    // push:<peer_host>-keyed cursor if the new push:<machine_id> key hasn't
    // been written yet.
    let mut cursor = status
        .peers
        .iter()
        .find(|p| p.peer_id == push_peer)
        .or_else(|| status.peers.iter().find(|p| p.peer_id == legacy_push_peer))
        .map(|p| p.last_seq)
        .unwrap_or(0);

    #[derive(serde::Deserialize)]
    struct ExportResp {
        records: Vec<serde_json::Value>,
        max_seq: u64,
    }

    let known_hosts = second_brain_sync::ssh::known_hosts_path()?;
    let mut child = std::process::Command::new("ssh")
        .args(second_brain_sync::ssh::ssh_args(&known_hosts))
        .arg("-o")
        .arg("ServerAliveInterval=60")
        .arg("-o")
        .arg("ServerAliveCountMax=60")
        .arg(peer_host)
        .arg(sb_binary)
        .arg("sync-import")
        .stdin(std::process::Stdio::piped())
        .stderr(std::process::Stdio::inherit())
        .spawn()
        .context("spawning ssh for push")?;

    let mut total_pushed = 0u64;

    let encryptor = default_sync_encryptor()?;
    {
        let stdin = child.stdin.take().unwrap();
        let mut writer = std::io::BufWriter::new(stdin);

        loop {
            let export: ExportResp = client
                .post(format!("{api_base}/v1/sync/export"))
                .json(&serde_json::json!({"after_seq": cursor, "limit": BATCH_SIZE}))
                .send()
                .context("getting export from daemon")?
                .error_for_status()
                .context("export returned error")?
                .json()?;

            if export.records.is_empty() {
                break;
            }

            let batch_count = export.records.len();

            for record in &export.records {
                use std::io::Write;
                let plaintext = serde_json::to_vec(record)?;
                let line = second_brain_sync::crypto::seal_line(encryptor.as_ref(), &plaintext)?;
                writer.write_all(line.as_bytes())?;
                writer.write_all(b"\n")?;
            }
            use std::io::Write;
            writer.flush()?;

            total_pushed += batch_count as u64;
            cursor = export.max_seq;

            eprintln!("  batch: {} records (through seq {})", batch_count, cursor);

            if batch_count < BATCH_SIZE {
                break;
            }
        }
    }

    let exit = child.wait()?;
    if !exit.success() {
        anyhow::bail!(
            "ssh push to {peer_host} exited with {exit}.\n  hint: {}",
            second_brain_sync::ssh::untrusted_host_hint(peer_host)
        );
    }

    if total_pushed > 0 {
        client
            .post(format!("{api_base}/v1/sync/state"))
            .json(&serde_json::json!({"peer_id": push_peer, "last_seq": cursor}))
            .send()
            .context("updating push sync state")?;
    }

    eprintln!("  total: {} records pushed", total_pushed);
    Ok(cursor)
}

fn cmd_sync_export(after_seq: u64) -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(120))
        .build()?;

    #[derive(serde::Deserialize)]
    struct ExportResp {
        records: Vec<serde_json::Value>,
        max_seq: u64,
    }

    // Page the export the same way the push path does: an unlimited request makes the
    // daemon buffer the whole changelog as bytes, then as a String, then as parsed JSON,
    // which on a memory-capped peer gets the process OOM-killed mid-response and shows up
    // here as a closed connection. The log is ordered by local_seq, so paging preserves
    // the global ordering the importer relies on.
    const BATCH_SIZE: usize = 2000;

    let encryptor = default_sync_encryptor()?;
    let stdout = std::io::stdout();
    let mut writer = std::io::BufWriter::new(stdout.lock());

    let mut cursor = after_seq;
    let mut total = 0u64;

    loop {
        let resp: ExportResp = client
            .post(format!("{api_base}/v1/sync/export"))
            .json(&serde_json::json!({"after_seq": cursor, "limit": BATCH_SIZE}))
            .send()
            .context("calling sync/export on daemon")?
            .error_for_status()
            .context("sync/export returned error")?
            .json()?;

        if resp.records.is_empty() {
            break;
        }

        let batch_count = resp.records.len();

        for record in &resp.records {
            use std::io::Write;
            let plaintext = serde_json::to_vec(record)?;
            let line = second_brain_sync::crypto::seal_line(encryptor.as_ref(), &plaintext)?;
            writer.write_all(line.as_bytes())?;
            writer.write_all(b"\n")?;
        }
        use std::io::Write;
        writer.flush()?;

        total += batch_count as u64;
        cursor = resp.max_seq;

        if batch_count < BATCH_SIZE {
            break;
        }
    }

    eprintln!("exported {total} records through seq {cursor}");
    Ok(())
}

fn cmd_prune_relations() -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(1800))
        .build()?;

    #[derive(serde::Deserialize)]
    struct PruneResp {
        pruned: u64,
    }

    let resp: PruneResp = client
        .post(format!("{api_base}/v1/sync/prune-relations"))
        .send()
        .context("calling prune-relations on daemon")?
        .error_for_status()?
        .json()?;

    eprintln!("pruned {} dangling relations", resp.pruned);
    Ok(())
}

/// The report fields a repair is expected to zero, mirroring
/// `IntegrityReport::drift` so status and doctor warn about the same things.
fn drift_fields(report: &serde_json::Value) -> Vec<(&'static str, u64)> {
    let mut out = Vec::new();
    for name in [
        "raw_retire_pending",
        "duplicate_hot_groups",
        "orphaned_distillation_markers",
        "dead_vec_keys",
        "dangling_relations",
        "sync_log_reclaimable_rows",
        "stale_peer_cursors",
        "missing_embeddings",
    ] {
        let value = report[name].as_u64().unwrap_or(0);
        if value > 0 {
            out.push((name, value));
        }
    }
    if report["index_consistent"] == serde_json::Value::Bool(false) {
        out.push(("index_inconsistent", 1));
    }
    out
}

fn print_report(report: &serde_json::Value) {
    let Some(map) = report.as_object() else {
        println!("{report}");
        return;
    };
    let width = map.keys().map(|k| k.len()).max().unwrap_or(0);
    for (key, value) in map {
        let shown = match value {
            serde_json::Value::Null => "n/a".to_string(),
            v => v.to_string(),
        };
        println!("{key:width$}  {shown}");
    }
    let drift = drift_fields(report);
    if drift.is_empty() {
        println!("\nno drift");
    } else {
        for (name, value) in drift {
            println!("WARN {name}: {value}");
        }
    }
}

fn cmd_doctor(repair: bool, dry_run: bool) -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(3600))
        .build()?;

    if !repair {
        let report: serde_json::Value = client
            .get(format!("{api}/v1/health"))
            .send()
            .context("calling health on daemon")?
            .error_for_status()?
            .json()?;
        print_report(&report);
        return Ok(());
    }

    let summary: serde_json::Value = client
        .post(format!("{api}/v1/maintenance/repair"))
        .json(&serde_json::json!({"dry_run": dry_run}))
        .send()
        .context("calling repair on daemon")?
        .error_for_status()?
        .json()?;
    println!(
        "{}",
        if dry_run {
            "repair (dry run):"
        } else {
            "repair:"
        }
    );
    if let Some(stats) = summary["stats"].as_object() {
        for (key, value) in stats {
            if key != "dry_run" {
                println!("  {key}: {value}");
            }
        }
    }
    println!("\nreport after:");
    print_report(&summary["report_after"]);
    Ok(())
}

fn cmd_vacuum() -> Result<()> {
    let api = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(3600))
        .build()?;
    let resp: serde_json::Value = client
        .post(format!("{api}/v1/maintenance/vacuum"))
        .send()
        .context("calling vacuum on daemon")?
        .error_for_status()?
        .json()?;
    let before = resp["bytes_before"].as_u64().unwrap_or(0);
    let after = resp["bytes_after"].as_u64().unwrap_or(0);
    println!(
        "vacuumed brain.db: {:.1} MB -> {:.1} MB",
        before as f64 / 1e6,
        after as f64 / 1e6
    );
    Ok(())
}

fn cmd_sync_key() -> Result<()> {
    let path = second_brain_sync::crypto::default_key_path()?;
    let key = second_brain_sync::crypto::load_or_create_key(&path)?;
    let enc = second_brain_sync::crypto::XChaChaEncryptor::new(key);
    println!("{}", enc.key_base64());
    eprintln!("key file: {}", path.display());
    eprintln!(
        "copy this base64 value into ~/.second-brain/sync.key on each paired device \
         (run `sb sync-key` once on a device to generate, then replicate it everywhere)."
    );
    Ok(())
}

fn default_sync_encryptor() -> Result<Box<dyn second_brain_sync::crypto::SyncEncryptor>> {
    let path = second_brain_sync::crypto::default_key_path()?;
    let key = second_brain_sync::crypto::load_or_create_key(&path)?;
    Ok(Box::new(second_brain_sync::crypto::XChaChaEncryptor::new(
        key,
    )))
}

fn cmd_sync_import() -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(600))
        .build()?;
    const BATCH_SIZE: usize = 500;

    let stdin = std::io::stdin();
    let reader = std::io::BufReader::new(stdin.lock());

    #[derive(serde::Deserialize)]
    struct ImportResp {
        created: u64,
        updated: u64,
        deleted: u64,
        skipped: u64,
        errors: u64,
        max_seq: u64,
    }

    let mut totals = (0u64, 0u64, 0u64, 0u64, 0u64, 0u64);
    let mut batch: Vec<serde_json::Value> = Vec::with_capacity(BATCH_SIZE);
    // Tracks the lowest watermark from any batch that reported errors, so a
    // failing batch cannot let a later successful batch advance the cursor past
    // the point where data may be missing.
    let mut min_error_watermark: Option<u64> = None;

    let flush_batch = |batch: &mut Vec<serde_json::Value>,
                       totals: &mut (u64, u64, u64, u64, u64, u64),
                       min_error_watermark: &mut Option<u64>|
     -> Result<()> {
        if batch.is_empty() {
            return Ok(());
        }
        let resp: ImportResp = client
            .post(format!("{api_base}/v1/sync/import"))
            .json(&serde_json::json!({"records": *batch}))
            .send()
            .context("calling sync/import on daemon")?
            .error_for_status()
            .context("sync/import returned error")?
            .json()?;
        totals.0 += resp.created;
        totals.1 += resp.updated;
        totals.2 += resp.deleted;
        totals.3 += resp.skipped;
        totals.4 += resp.errors;
        if resp.errors > 0 {
            let candidate = resp.max_seq.saturating_sub(1);
            *min_error_watermark = Some(
                min_error_watermark
                    .map(|prev| prev.min(candidate))
                    .unwrap_or(candidate),
            );
        } else {
            totals.5 = totals.5.max(resp.max_seq);
        }
        batch.clear();
        Ok(())
    };

    let encryptor = default_sync_encryptor()?;
    for line in std::io::BufRead::lines(reader) {
        let line = line?;
        if line.trim().is_empty() {
            continue;
        }
        let plaintext = second_brain_sync::crypto::open_line(encryptor.as_ref(), &line)?;
        batch.push(serde_json::from_slice(&plaintext)?);

        if batch.len() >= BATCH_SIZE {
            flush_batch(&mut batch, &mut totals, &mut min_error_watermark)?;
        }
    }

    flush_batch(&mut batch, &mut totals, &mut min_error_watermark)?;

    // If any batch had errors, the watermark must not advance past the lowest
    // failing point, regardless of how many later batches succeeded.
    if let Some(err_wm) = min_error_watermark {
        totals.5 = totals.5.min(err_wm);
    }

    eprintln!(
        "imported: {} created, {} updated, {} deleted, {} skipped, {} errors (through seq {})",
        totals.0, totals.1, totals.2, totals.3, totals.4, totals.5
    );
    Ok(())
}

fn cmd_sync_status() -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::new();

    #[derive(serde::Deserialize)]
    struct PeerInfo {
        peers: Vec<PeerState>,
    }
    #[derive(serde::Deserialize)]
    struct PeerState {
        peer_id: String,
        last_seq: u64,
        last_sync_at: String,
    }

    let resp: PeerInfo = client
        .get(format!("{api_base}/v1/sync/status"))
        .send()
        .context("getting sync status from daemon")?
        .error_for_status()?
        .json()?;

    if resp.peers.is_empty() {
        println!("no sync peers configured");
    } else {
        for state in &resp.peers {
            println!(
                "peer: {}, last_seq: {}, last_sync: {}",
                state.peer_id, state.last_seq, state.last_sync_at
            );
        }
    }
    Ok(())
}

fn cmd_sync_backfill() -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::new();

    #[derive(serde::Deserialize)]
    struct BackfillResp {
        backfilled: usize,
        #[serde(default)]
        tombstones_backfilled: usize,
    }

    let resp: BackfillResp = client
        .post(format!("{api_base}/v1/sync/backfill"))
        .send()
        .context("calling backfill on daemon")?
        .error_for_status()?
        .json()?;

    eprintln!(
        "backfilled {} create entries, {} delete entries",
        resp.backfilled, resp.tombstones_backfilled
    );
    Ok(())
}

fn cmd_backfill_session_dates(dry_run: bool, reemit: bool) -> Result<()> {
    let api_base = api_base_url();
    // Every transcript on the machine is read once, which takes minutes.
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(1800))
        .build()?;

    let report: serde_json::Value = client
        .post(format!("{api_base}/v1/backfill-session-at"))
        .json(&serde_json::json!({"dry_run": dry_run, "reemit": reemit}))
        .send()
        .context("calling backfill-session-at on daemon")?
        .error_for_status()?
        .json()?;

    let tiers = [
        "transcript_line",
        "conversation_start",
        "transcript_start",
        "file_mtime",
        "remembered_at",
        "unknown",
    ];
    println!(
        "{}undated rows: {}",
        if dry_run { "dry run, " } else { "" },
        report["candidates"]
    );
    println!("{:<20} {:>8} {:>8}", "evidence", "all", "live");
    for tier in tiers {
        println!(
            "{:<20} {:>8} {:>8}",
            tier,
            report["all"][tier].as_u64().unwrap_or(0),
            report["live"][tier].as_u64().unwrap_or(0)
        );
    }
    println!(
        "fallback conversations skipped: {}",
        report["fallback_conversations_skipped"]
    );
    println!(
        "written: {}, reemitted: {}",
        report["written"], report["reemitted"]
    );
    for s in report["sample"].as_array().into_iter().flatten() {
        let content: String = s["content"]
            .as_str()
            .unwrap_or("")
            .chars()
            .take(80)
            .map(|c| if c == '\n' { ' ' } else { c })
            .collect();
        println!(
            "  {} {:<18} created {} {}: {}",
            s["session_at"]
                .as_str()
                .unwrap_or("")
                .get(..10)
                .unwrap_or(""),
            s["tier"].as_str().unwrap_or(""),
            s["created_at"]
                .as_str()
                .unwrap_or("")
                .get(..10)
                .unwrap_or(""),
            s["source"].as_str().unwrap_or(""),
            content
        );
    }
    Ok(())
}

fn cmd_sync_compact(ops: &[String], dry_run: bool) -> Result<()> {
    let api_base = api_base_url();
    let client = reqwest::blocking::Client::new();

    #[derive(serde::Deserialize)]
    struct CompactResp {
        groups_with_duplicates: u64,
        rows_deleted: u64,
        max_local_seq_before: u64,
        max_local_seq_after: u64,
    }

    let resp: CompactResp = client
        .post(format!("{api_base}/v1/sync/compact"))
        .json(&serde_json::json!({"ops": ops, "dry_run": dry_run}))
        .send()
        .context("calling compact on daemon")?
        .error_for_status()?
        .json()?;

    if resp.max_local_seq_before != resp.max_local_seq_after {
        anyhow::bail!(
            "compact reported MAX(local_seq) moved from {} to {} -- aborting, this must never happen",
            resp.max_local_seq_before,
            resp.max_local_seq_after
        );
    }

    if dry_run {
        eprintln!(
            "dry run: {} groups have duplicates, {} rows would be deleted (max_local_seq {} unaffected)",
            resp.groups_with_duplicates, resp.rows_deleted, resp.max_local_seq_after
        );
    } else {
        eprintln!(
            "compacted {} groups, deleted {} duplicate rows (max_local_seq {} unchanged)",
            resp.groups_with_duplicates, resp.rows_deleted, resp.max_local_seq_after
        );
    }
    Ok(())
}

fn cmd_daily_pipeline(
    llm_url: &str,
    model: &str,
    consolidate_limit: usize,
    _enhance: bool,
) -> Result<()> {
    println!("=== daily pipeline ===\n");

    println!(
        "[1/1] consolidating up to {} memories...",
        consolidate_limit
    );
    match consolidate::cmd_consolidate(
        false,
        consolidate_limit,
        model,
        llm_url,
        3,
        consolidate::Guard::Retry,
    ) {
        Ok(()) => println!("      consolidation complete"),
        Err(e) => println!("      consolidation failed (non-fatal): {}", e),
    }

    println!("\n=== pipeline complete ===");
    Ok(())
}