topodb-cli 0.0.12

Command-line interface for the TopoDB agent-memory engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
mod cli;
mod output;
mod resolve;

use std::io::{IsTerminal, Read};
use std::path::Path;
use std::str::FromStr;

use clap::Parser;
use cli::{Cli, Command};
use topodb::{
    Db, Direction, EdgeId, EdgeRecord, NodeId, Op, PropValue, Scope, TimeAxis, TopoError,
    TraversalQuery, ValidInterval, VectorQuery,
};

fn main() {
    let cli = Cli::parse();

    // Project config (nearest .topodb.toml on the path from cwd up).
    let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());
    let cfg = match resolve::load_project_config(&cwd) {
        Ok(c) => c,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let cfg = cfg.unwrap_or_default();
    for k in &cfg.unknown_keys {
        eprintln!("topodb: ignoring unknown key {k:?} in .topodb.toml");
    }
    let cfg_path = cfg.path.clone();
    let home = std::env::var("HOME")
        .ok()
        .or_else(|| std::env::var("USERPROFILE").ok());

    // db path.
    let db_r = resolve::resolve_db(
        cli.db.clone(),
        std::env::var("TOPODB_DB").ok(),
        cfg.db.as_deref().zip(cfg_path.as_deref()),
        home.as_deref(),
    );
    let db_path = db_r.value;

    // Create the parent directory for the default db path (~/.topodb/memory.redb).
    // For user-provided paths, let the database engine report errors if the parent doesn't exist.
    if matches!(db_r.source, resolve::Source::Default) {
        // Guard against unresolved home directory (HOME/USERPROFILE both unset).
        if db_path
            .to_str()
            .map(|p| p.starts_with("~"))
            .unwrap_or(false)
        {
            output::fail(
                "internal",
                "cannot resolve home directory (HOME/USERPROFILE unset) for the default db path",
                1,
            );
        }
        if let Some(parent) = db_path.parent() {
            if !parent.exists() {
                if let Err(e) = std::fs::create_dir_all(parent) {
                    output::fail("internal", &format!("creating db directory: {e}"), 1);
                }
            }
        }
    }

    // scope string → Scope. Errors name the source they came from.
    let scope_r = resolve::resolve_scope_str(
        cli.scope.clone(),
        std::env::var("TOPODB_SCOPE").ok(),
        cfg.scope.as_deref().zip(cfg_path.as_deref()),
    );
    let default_scope = match topodb_json::resolve_scope(Some(&scope_r.value), Scope::Shared) {
        Ok(s) => s,
        Err(e) => output::fail(
            "rejected",
            &format!("scope from {}: {e}", scope_r.source.label()),
            2,
        ),
    };

    let fmt = match resolve::resolve_format(
        cli.format.map(Into::into),
        std::env::var("TOPODB_FORMAT").ok(),
        cfg.format.as_deref().zip(cfg_path.as_deref()),
        std::io::stdout().is_terminal(),
    ) {
        Ok(r) => r.value,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let text_mode = matches!(fmt, resolve::Format::Text);
    let scope_display = scope_r.value.clone();
    let scope_source = scope_r.source.label();

    // Open using the file's own persisted index spec — no --spec flag on
    // this CLI. An EXISTING file always inherits its persisted spec exactly
    // (via `open_stored`), so a db another tool (e.g. topodb-mcp) already
    // populated is never reindexed or mis-declared. A brand-new file (no
    // `.redb` at this path yet) is created with the SAME canonical
    // `topodb_json::default_spec()` that topodb-mcp uses when `--spec` is
    // omitted — equality on `(Entity, name)`, text on `(Memory, content)` —
    // rather than the engine's bare `IndexSpec::default()` (which declares
    // nothing). This is what makes a CLI-created db and an MCP-created db
    // byte-identical in their persisted `index_spec`: serving one via the
    // other never reindexes, and both `find` and `search` work out of the box
    // on a fresh CLI db. `Path::exists` is safe here: the CLI is a single,
    // non-concurrent process per invocation, so there's no writer racing it.
    // Resolve any per-command --scope override BEFORE opening the db, so a
    // bad value never leaves an empty file behind — same contract as the
    // global --scope above.
    let write_scope = match &cli.cmd {
        Command::CreateMemory { scope, .. }
        | Command::CreateEntity { scope, .. }
        | Command::Link { scope, .. }
        | Command::Remember { scope, .. }
        | Command::Forget { scope, .. }
        | Command::ObsidianIngest { scope, .. } => {
            resolve_cmd_scope(scope.as_deref(), default_scope)
        }
        _ => default_scope,
    };

    let db = topodb_json::open_with_busy_retry(cli.lock_wait_ms, || {
        if db_path.exists() {
            // Inherit the persisted spec, but silently upgrade a db still on an
            // older STOCK default to the current one (`topodb_json::upgraded_spec`
            // — e.g. adding the (Entity, name) text index); a customized spec is
            // inherited verbatim. Mirrors topodb-mcp's open path exactly.
            Db::open_stored(&db_path).and_then(|db| {
                let persisted = db.index_spec();
                let upgraded = topodb_json::upgraded_spec(persisted.clone());
                if upgraded != persisted {
                    drop(db);
                    Db::open_with(&db_path, upgraded)
                } else {
                    Ok(db)
                }
            })
        } else {
            Db::open_with(&db_path, topodb_json::default_spec())
        }
    });
    let db = match db {
        Ok(db) => db,
        Err(TopoError::Busy) => output::fail(
            "busy",
            &format!(
                "another process holds {}; retried for {}ms (tune with --lock-wait-ms / TOPODB_LOCK_WAIT_MS)",
                db_path.display(),
                cli.lock_wait_ms
            ),
            3,
        ),
        Err(e) => output::fail_engine(&e),
    };

    match cli.cmd {
        Command::Info => info(&db, &db_path, default_scope, cli.pretty),
        Command::CreateMemory { content, props, .. } => {
            create_memory(&db, write_scope, content, props.as_deref(), cli.pretty)
        }
        Command::CreateEntity {
            name,
            props,
            always_create,
            ..
        } => create_entity(
            &db,
            write_scope,
            name,
            props.as_deref(),
            always_create,
            cli.pretty,
        ),
        Command::Link {
            from,
            to,
            ty,
            props,
            valid_from,
            ..
        } => link(
            &db,
            write_scope,
            &from,
            &to,
            ty,
            props.as_deref(),
            valid_from,
            cli.pretty,
        ),
        Command::Remember {
            content,
            content_flag,
            entity,
            edge_type,
            supersedes,
            props,
            kind,
            ..
        } => {
            let content = match content.or(content_flag) {
                Some(c) => c,
                None => output::fail(
                    "rejected",
                    "provide the fact as a positional argument or via --content",
                    2,
                ),
            };
            remember(
                &db,
                write_scope,
                content,
                entity,
                edge_type,
                supersedes,
                props.as_deref(),
                kind,
                text_mode,
                cli.pretty,
            )
        }
        Command::Forget { ids, .. } => forget(&db, write_scope, &ids, text_mode, cli.pretty),
        Command::ObsidianIngest { vault, dry_run, .. } => {
            obsidian_ingest(&db, &vault, write_scope, dry_run, cli.pretty)
        }
        Command::ObsidianSeed {
            vault,
            query,
            k,
            entity,
            hops,
            overwrite,
        } => obsidian_seed(
            &db,
            &vault,
            query,
            k,
            entity,
            hops,
            overwrite,
            default_scope,
            cli.pretty,
        ),
        Command::Get { id } => get(&db, default_scope, &id, text_mode, cli.pretty),
        Command::Find {
            label,
            prop,
            value,
            normalized,
        } => find(
            &db,
            default_scope,
            &label,
            &prop,
            &value,
            normalized,
            text_mode,
            &scope_display,
            &scope_source,
            cli.pretty,
        ),
        Command::Search {
            query,
            k,
            include_superseded,
            kinds,
            recency_weight,
            recency_half_life_days,
            created_after,
            created_before,
            no_temporal_rewrite,
        } => search(
            &db,
            default_scope,
            &query,
            k,
            include_superseded,
            &kinds,
            recency_weight,
            recency_half_life_days,
            created_after,
            created_before,
            no_temporal_rewrite,
            text_mode,
            &scope_display,
            &scope_source,
            cli.pretty,
        ),
        Command::Traverse {
            seed,
            max_hops,
            direction,
            edge_type,
            as_of,
            time_axis,
            valid_interval,
        } => traverse(
            &db,
            default_scope,
            &seed,
            max_hops,
            direction.into(),
            edge_type,
            as_of,
            time_axis.into(),
            allen_interval(&valid_interval),
            cli.pretty,
        ),
        Command::GetEdges {
            from,
            to,
            edge_type,
            open_only,
            as_of,
            direction,
            time_axis,
            valid_interval,
        } => get_edges(
            &db,
            default_scope,
            &from,
            to.as_deref(),
            edge_type.as_deref(),
            open_only,
            as_of,
            direction,
            time_axis.into(),
            allen_interval(&valid_interval),
            cli.pretty,
        ),
        Command::Stats { id } => stats(&db, default_scope, &id, cli.pretty),
        Command::LifecycleCandidates {
            limit,
            half_life_episodic_days,
            half_life_semantic_days,
            half_life_procedural_days,
            half_life_decision_days,
            now_ms,
        } => lifecycle_candidates(
            &db,
            default_scope,
            limit,
            half_life_episodic_days,
            half_life_semantic_days,
            half_life_procedural_days,
            half_life_decision_days,
            now_ms,
            cli.pretty,
        ),
        Command::Purge {
            tombstoned_before,
            yes,
        } => purge(&db, default_scope, tombstoned_before, yes, cli.pretty),
        Command::Changes { since } => changes(&db, since, cli.pretty),
        Command::Compact { keep_from } => compact(&db, keep_from, cli.pretty),
        Command::SetProps { id, props } => set_props(&db, &id, &props, cli.pretty),
        Command::RemoveNode { id } => remove_node(&db, &id, cli.pretty),
        Command::CloseEdge { id, valid_to } => close_edge(&db, &id, valid_to, cli.pretty),
        Command::SetEmbedding { id, model, vector } => {
            set_embedding(&db, &id, model, &vector, cli.pretty)
        }
        Command::SearchVector {
            model,
            vector,
            k,
            candidate,
        } => search_vector(&db, default_scope, model, &vector, k, candidate, cli.pretty),
        Command::Submit { input } => submit(&db, default_scope, &input, cli.pretty),
    }
}

/// Resolves a per-command `--scope` override against the global `--scope`.
/// Absent → the global default; present → parsed, a bad value being a
/// caller-fixable input error (rejected/exit-2). Routed through the same
/// `topodb_json::resolve_scope` the batch DSL uses, so `topodb link --scope X`
/// and `topodb submit '[{"op":"link", ..., "scope":"X"}]'` cannot drift apart.
fn resolve_cmd_scope(scope: Option<&str>, default: Scope) -> Scope {
    match topodb_json::resolve_scope(scope, default) {
        Ok(s) => s,
        Err(e) => output::fail("rejected", &e, 2),
    }
}

/// Resolves the four `--valid-*` flags (the pragmatic allen subset —
/// `topodb::ValidInterval`) into at most one predicate. More than one is a
/// caller-fixable conflict -> `fail("rejected", .., 2)` naming the flags;
/// range flags parse `A..B` into two Unix-ms timestamps. Uses the engine's
/// shared `ValidInterval::from_parts` to handle mutual-exclusion checks, range
/// inversion, and timestamp positivity validation. Timestamp errors from the
/// engine are surfaced verbatim to the CLI exit code contract (exit 2).
fn allen_interval(args: &cli::ValidIntervalArgs) -> Option<ValidInterval> {
    let parse_range = |flag: &str, s: &str| -> Option<(i64, i64)> {
        let parsed = s
            .split_once("..")
            .and_then(|(a, b)| Some((a.trim().parse().ok()?, b.trim().parse().ok()?)));
        match parsed {
            Some(pair) => Some(pair),
            None => output::fail(
                "rejected",
                &format!("parsing --{flag}: expected A..B with Unix-ms timestamps, got {s:?}"),
                2,
            ),
        }
    };

    let during = args
        .valid_during
        .as_deref()
        .and_then(|s| parse_range("valid-during", s));
    let overlaps = args
        .valid_overlaps
        .as_deref()
        .and_then(|s| parse_range("valid-overlaps", s));
    let before = args.valid_before;
    let after = args.valid_after;

    match ValidInterval::from_parts(during, overlaps, before, after) {
        Ok(iv) => iv,
        Err(e) => output::fail("rejected", &e, 2),
    }
}

/// Parses a `--value` arg per the CLI's find semantics: try it as a JSON
/// scalar first (so `42` -> `Int(42)`, `true` -> `Bool(true)`,
/// `"ada"` -> `Str("ada")`); if it doesn't parse as JSON at all, fall back to
/// treating the raw string as `PropValue::Str` (so `--value ada` and
/// `--value '"ada"'` are equivalent). A JSON value that parses but isn't a
/// scalar `json_to_prop_value` can represent (array/object/null) is a
/// caller-fixable input error -> `fail("rejected", .., 2)`.
fn parse_value_arg(value: &str) -> PropValue {
    match serde_json::from_str::<serde_json::Value>(value) {
        Ok(v) => match topodb_json::json_to_prop_value(&v) {
            Ok(pv) => pv,
            Err(e) => output::fail("rejected", &format!("parsing --value: {e}"), 2),
        },
        Err(_) => PropValue::Str(value.to_string()),
    }
}

fn get(db: &Db, scope: Scope, id: &str, text_mode: bool, pretty: bool) -> ! {
    let id = match NodeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid id {id:?}: {e}"), 2),
    };
    let scopes = topodb_json::scope_to_scope_set(scope);
    let value = match db.node(&scopes, id) {
        Some(n) => {
            let node = match topodb_json::node_to_json(&n) {
                Ok(v) => v,
                Err(e) => output::fail("internal", &e, 1),
            };
            serde_json::json!({ "found": true, "node": node })
        }
        None => serde_json::json!({ "found": false }),
    };
    let text = match value.get("found").and_then(|f| f.as_bool()) {
        Some(true) => value.get("node").map(|n| {
            let id = n.get("id").and_then(|v| v.as_str()).unwrap_or("?");
            let label = n.get("label").and_then(|v| v.as_str()).unwrap_or("?");
            format!("{id}  {label}")
        }),
        _ => Some("not found".to_string()),
    };
    output::render(&value, text, text_mode, pretty);
}

#[allow(clippy::too_many_arguments)]
fn find(
    db: &Db,
    scope: Scope,
    label: &str,
    prop: &str,
    value: &str,
    normalized: bool,
    text_mode: bool,
    scope_display: &str,
    scope_source: &str,
    pretty: bool,
) -> ! {
    let pv = parse_value_arg(value);
    let scopes = topodb_json::scope_to_scope_set(scope);
    let hits = if normalized {
        db.nodes_by_prop_normalized(&scopes, label, prop, &pv)
    } else {
        db.nodes_by_prop(&scopes, label, prop, &pv)
    };
    let hits = match hits {
        Ok(hits) => hits,
        Err(e) => output::fail_engine(&e),
    };
    let nodes: Vec<serde_json::Value> = match hits.iter().map(topodb_json::node_to_json).collect() {
        Ok(nodes) => nodes,
        Err(e) => output::fail("internal", &e, 1),
    };
    if nodes.is_empty() {
        output::empty_scope_echo(scope_display, scope_source);
    }
    let text = Some(
        nodes
            .iter()
            .map(|n| {
                let id = n.get("id").and_then(|v| v.as_str()).unwrap_or("?");
                let label = n.get("label").and_then(|v| v.as_str()).unwrap_or("?");
                format!("{id}  {label}")
            })
            .collect::<Vec<_>>()
            .join("\n"),
    );
    let value = serde_json::Value::Array(nodes);
    output::render(&value, text, text_mode, pretty);
}

#[allow(clippy::too_many_arguments)]
fn search(
    db: &Db,
    scope: Scope,
    query: &str,
    k: usize,
    include_superseded: bool,
    kinds: &[String],
    recency_weight: f32,
    recency_half_life_days: Option<f64>,
    created_after: Option<String>,
    created_before: Option<String>,
    no_temporal_rewrite: bool,
    text_mode: bool,
    scope_display: &str,
    scope_source: &str,
    pretty: bool,
) -> ! {
    let scopes = topodb_json::scope_to_scope_set(scope);
    // The kinds filter is policy over the engine's generic prop_retain:
    // this layer names the prop and maps "absent" to the default kind.
    let prop_retain = if kinds.is_empty() {
        None
    } else {
        for kind in kinds {
            if let Err(e) = topodb_json::validate_memory_kind(kind) {
                output::fail("rejected", &e, 2);
            }
        }
        Some(topodb::PropRetain {
            prop: topodb_json::MEMORY_KIND_PROP.to_string(),
            any_of: kinds.to_vec(),
            absent_as: Some(topodb_json::MEMORY_KIND_DEFAULT.to_string()),
        })
    };

    // Created-time filter (explicit flags; Task 8 adds the rewriter
    // branch with the same precedence as MCP search_memories). Applied
    // filters are echoed to stderr so operators see what ran.
    let (mut after_ms, mut before_ms) = (None, None);
    let mut filter_note: Option<(String, &str)> = None;
    let mut query = query.to_string();
    if created_after.is_some() || created_before.is_some() {
        let parse = |flag: &str, s: &str| {
            topodb_json::parse_iso_instant(s).unwrap_or_else(|| {
                output::fail(
                    "rejected",
                    &format!(
                        "parsing --{flag}: {s:?} is not an ISO date or UTC datetime \
                         (try 2026-08-01 or 2026-08-01T15:30:00Z)"
                    ),
                    2,
                )
            })
        };
        after_ms = created_after.as_deref().map(|s| parse("created-after", s));
        before_ms = created_before
            .as_deref()
            .map(|s| parse("created-before", s));
        let mut parts = Vec::new();
        if let Some(s) = &created_after {
            parts.push(format!("after {s}"));
        }
        if let Some(s) = &created_before {
            parts.push(format!("before {s}"));
        }
        filter_note = Some((parts.join(" "), "params"));
    } else if !no_temporal_rewrite {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0);
        if let Some(rw) = topodb_json::parse_temporal_query(&query, now) {
            after_ms = rw.after_ms;
            before_ms = rw.before_ms;
            query = rw.residual_query;
            filter_note = Some((rw.matched_phrase, "rewrite"));
        }
    }
    if let Some((desc, source)) = &filter_note {
        output::time_filter_echo(desc, source, after_ms, before_ms);
    }

    let options = topodb::SearchOptions {
        created_range: (after_ms.is_some() || before_ms.is_some()).then_some(
            topodb::CreatedRange {
                after_ms,
                before_ms,
            },
        ),
        prop_retain,
        recency_weight,
        recency_half_life_ms: recency_half_life_days
            .map(|d| (d * 86_400_000.0) as i64)
            .unwrap_or(30 * 24 * 60 * 60 * 1000),
        recency_half_life_by_prop: recency_half_life_days
            .is_none()
            .then(topodb_json::memory_kind_half_life),
        ..topodb::SearchOptions::default()
    };
    // Search is a recall surface: memories retired by `remember --supersedes`
    // are dropped by default (before top-k, unbumped), matching the MCP
    // server's `search_memories`. `--include-superseded` widens the corpus
    // to retired memories too — it does not touch scoring; `--recency-weight
    // 0` is what restores raw BM25. Forgotten memories are also dropped from
    // default search (same liveness model as superseded).
    let hits = if include_superseded {
        db.search_text_with(&scopes, &query, k, &options)
    } else {
        db.search_text_live(
            &scopes,
            &query,
            k,
            &options,
            &topodb_json::MEMORY_TOMBSTONE_PROPS,
        )
    };
    let hits = match hits {
        Ok(hits) => hits,
        Err(e) => output::fail_engine(&e),
    };
    let out: Result<Vec<serde_json::Value>, String> = hits
        .iter()
        .map(|(n, score)| {
            topodb_json::node_to_json(n)
                .map(|node| serde_json::json!({ "node": node, "score": score }))
        })
        .collect();
    let out = match out {
        Ok(out) => out,
        Err(e) => output::fail("internal", &e, 1),
    };
    if out.is_empty() {
        output::empty_scope_echo(scope_display, scope_source);
    }
    let text = Some(
        out.iter()
            .map(|hit| {
                let content = hit["node"]["props"]["content"].as_str().unwrap_or("");
                let content = if content.chars().count() > 140 {
                    let mut s: String = content.chars().take(139).collect();
                    s.push('…');
                    s
                } else {
                    content.to_string()
                };
                let id = hit["node"]["id"].as_str().unwrap_or("?");
                let score = hit["score"].as_f64().unwrap_or(0.0);
                format!("- [{score:.2}] {content}  {id}")
            })
            .collect::<Vec<_>>()
            .join("\n"),
    );
    let value = serde_json::Value::Array(out);
    output::render(&value, text, text_mode, pretty);
}

#[allow(clippy::too_many_arguments)]
fn traverse(
    db: &Db,
    scope: Scope,
    seed: &str,
    max_hops: u8,
    direction: Direction,
    edge_type: Vec<String>,
    as_of: Option<i64>,
    time_axis: TimeAxis,
    valid_interval: Option<ValidInterval>,
    pretty: bool,
) -> ! {
    let seed = match NodeId::from_str(seed) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid seed id {seed:?}: {e}"), 2),
    };
    // Validate as_of: must be positive if provided.
    if let Some(ts) = as_of {
        if ts <= 0 {
            output::fail(
                "rejected",
                "as-of must be a positive Unix-millisecond timestamp",
                2,
            );
        }
    }
    // Composition rules for an interval predicate, checked here so the
    // errors name CLI flags (the engine re-checks with its own wording).
    if valid_interval.is_some() {
        if as_of.is_some() {
            output::fail(
                "rejected",
                "a --valid-* interval predicate and --as-of are mutually exclusive — a \
                 point-in-time query is a degenerate --valid-overlaps",
                2,
            );
        }
        if time_axis == TimeAxis::Recorded {
            output::fail(
                "rejected",
                "--valid-* interval predicates gate the valid axis only — omit --time-axis recorded",
                2,
            );
        }
    }
    let scopes = topodb_json::scope_to_scope_set(scope);
    // Empty --edge-type (none given) -> None, follow every edge type; the
    // engine treats `Some(vec![])` as "match nothing", which would silently
    // strand the traversal at the seed — so an empty CLI list must map to
    // `None`, not `Some(vec![])`.
    let edge_types = if edge_type.is_empty() {
        None
    } else {
        Some(edge_type.into_iter().map(Into::into).collect())
    };
    let query = TraversalQuery {
        scopes,
        seeds: vec![seed],
        max_hops,
        edge_types,
        direction,
        as_of,
        time_axis,
    };
    let sg = match valid_interval {
        Some(iv) => db.traverse_interval(&query, iv),
        None => db.traverse(&query),
    };
    let sg = match sg {
        Ok(sg) => sg,
        Err(e) => output::fail_engine(&e),
    };
    let subgraph = match topodb_json::subgraph_to_json(&sg) {
        Ok(v) => v,
        Err(e) => output::fail("internal", &e, 1),
    };
    output::ok(&serde_json::json!({ "subgraph": subgraph }), pretty);
}

/// Run `fetch` with the normalized form of `edge_type`, then — when the raw
/// form differs — again with the raw form, merging results (legacy edges carry
/// the raw spelling). `None` = no type filter, single probe. Engine errors
/// exit via `output::fail_engine`, matching every other CLI fetch.
fn fetch_typed<F>(edge_type: Option<&str>, fetch: F) -> Vec<EdgeRecord>
where
    F: Fn(Option<&str>) -> Result<Vec<EdgeRecord>, TopoError>,
{
    let run = |t: Option<&str>| fetch(t).unwrap_or_else(|e| output::fail_engine(&e));
    match edge_type {
        None => run(None),
        Some(raw) => {
            let norm = match topodb_json::normalize_edge_type(raw) {
                Ok(n) => n,
                Err(e) => output::fail("rejected", &e, 2),
            };
            let mut es = run(Some(&norm));
            if norm != raw {
                es.extend(run(Some(raw)));
            }
            es
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn get_edges(
    db: &Db,
    scope: Scope,
    from: &str,
    to: Option<&str>,
    edge_type: Option<&str>,
    open_only: Option<bool>,
    as_of: Option<i64>,
    direction: cli::DirectionArg,
    time_axis: TimeAxis,
    valid_interval: Option<ValidInterval>,
    pretty: bool,
) -> ! {
    // Validate as_of timestamp first (so as_of: 0 gets the timestamp error,
    // not the exclusivity one), matching the MCP handler error order.
    if let Some(timestamp) = as_of {
        if timestamp <= 0 {
            output::fail(
                "rejected",
                "as-of must be a positive Unix-millisecond timestamp",
                2,
            );
        }
    }

    let from_id = match NodeId::from_str(from) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid from id {from:?}: {e}"), 2),
    };
    #[allow(clippy::manual_map)]
    let to_id = match to {
        Some(s) => Some(match NodeId::from_str(s) {
            Ok(id) => id,
            Err(e) => output::fail("rejected", &format!("invalid to id {s:?}: {e}"), 2),
        }),
        None => None,
    };

    // Check mutually exclusive parameters: as_of and open_only cannot both
    // be specified. When as_of is present, omit open_only entirely.
    if as_of.is_some() && open_only.is_some() {
        output::fail(
            "rejected",
            "as_of and open_only are mutually exclusive — omit open_only when passing as_of (as_of already means \"open at that instant\")",
            2,
        );
    }

    let scopes = topodb_json::scope_to_scope_set(scope);

    // An interval predicate REPLACES the open-only/as-of gating: an explicit
    // temporal flag alongside it is a conflict, and only the valid axis is
    // in scope. Passing predicates then routes to the `*_interval` reads,
    // which gate on the adjacency interval fields (no post-filter here).
    if valid_interval.is_some() {
        if as_of.is_some() {
            output::fail(
                "rejected",
                "a --valid-* interval predicate and --as-of are mutually exclusive — a \
                 point-in-time query is a degenerate --valid-overlaps",
                2,
            );
        }
        if open_only.is_some() {
            output::fail(
                "rejected",
                "a --valid-* interval predicate replaces --open-only — omit --open-only; the \
                 predicate already says which edges qualify",
                2,
            );
        }
        if time_axis == TimeAxis::Recorded {
            output::fail(
                "rejected",
                "--valid-* interval predicates gate the valid axis only — omit --time-axis recorded",
                2,
            );
        }
    }

    // Determine whether to fetch only open edges: when as_of is present,
    // always fetch with open_only=false to see the full history, then filter
    // below. When as_of is absent, use the provided open_only or default to true.
    let open_only_to_use = if as_of.is_some() {
        false
    } else {
        open_only.unwrap_or(true)
    };

    // Fold the interval conditional INSIDE the fetch closures so there is one
    // shared tail (mirrors the MCP server's shape at server.rs ~4153-4172).
    let fetch_from = |t: Option<&str>| match valid_interval {
        Some(iv) => db.edges_from_interval(&scopes, from_id, to_id, t, iv),
        None => db.edges_from(&scopes, from_id, to_id, t, open_only_to_use, time_axis),
    };
    let fetch_to = |t: Option<&str>| match valid_interval {
        Some(iv) => db.edges_to_interval(&scopes, from_id, to_id, t, iv),
        None => db.edges_to(&scopes, from_id, to_id, t, open_only_to_use, time_axis),
    };

    let mut edges = match direction {
        cli::DirectionArg::Out => fetch_typed(edge_type, fetch_from),
        cli::DirectionArg::In => fetch_typed(edge_type, fetch_to),
        cli::DirectionArg::Both => {
            let mut es = fetch_typed(edge_type, fetch_from);
            es.extend(fetch_typed(edge_type, fetch_to));
            es
        }
    };

    edges.sort_by_key(|e| e.id);
    edges.dedup_by_key(|e| e.id);

    // If as_of is set, filter edges to only those live at that timestamp on
    // the requested axis (inclusive lower bound, exclusive upper bound):
    // valid axis gates on valid_from/valid_to (world time), recorded axis
    // gates on recorded_at/superseded_at (belief time).
    if let Some(timestamp) = as_of {
        match time_axis {
            TimeAxis::Valid => edges.retain(|e| topodb_json::edge_live_at(e, timestamp)),
            TimeAxis::Recorded => edges.retain(|e| topodb_json::edge_believed_at(e, timestamp)),
        }
    }

    let edges: Vec<serde_json::Value> = match edges
        .iter()
        .map(topodb_json::edge_to_json)
        .collect::<Result<Vec<_>, _>>()
    {
        Ok(edges) => edges,
        Err(e) => output::fail("internal", &e, 1),
    };

    output::ok(&serde_json::json!({ "edges": edges }), pretty);
}

fn stats(db: &Db, scope: Scope, id: &str, pretty: bool) -> ! {
    let id = match NodeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid id {id:?}: {e}"), 2),
    };
    let scopes = topodb_json::scope_to_scope_set(scope);
    let value = match db.access_stats(&scopes, id) {
        Ok(Some(s)) => serde_json::json!({
            "found": true,
            "access_stats": {
                "access_count": s.access_count,
                "last_accessed_at": s.last_accessed_at,
            }
        }),
        Ok(None) => serde_json::json!({ "found": false }),
        Err(e) => output::fail_engine(&e),
    };
    output::ok(&value, pretty);
}

/// Phase C decay sweep: delegate to the shared
/// `topodb_json::lifecycle_candidates` (read-only, unbumped) and print
/// the evidence array. Day flags convert to ms here — the shared layer
/// speaks ms.
#[allow(clippy::too_many_arguments)]
fn lifecycle_candidates(
    db: &Db,
    scope: Scope,
    limit: usize,
    half_life_episodic_days: f64,
    half_life_semantic_days: f64,
    half_life_procedural_days: f64,
    half_life_decision_days: f64,
    now_ms: Option<i64>,
    pretty: bool,
) -> ! {
    let scopes = topodb_json::scope_to_scope_set(scope);
    let params = topodb_json::LifecycleParams {
        limit,
        half_life_episodic_ms: (half_life_episodic_days * 86_400_000.0) as i64,
        half_life_semantic_ms: (half_life_semantic_days * 86_400_000.0) as i64,
        half_life_procedural_ms: (half_life_procedural_days * 86_400_000.0) as i64,
        half_life_decision_ms: (half_life_decision_days * 86_400_000.0) as i64,
    };
    let now = now_ms.unwrap_or_else(|| {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0)
    });
    let candidates = match topodb_json::lifecycle_candidates(db, &scopes, &params, now) {
        Ok(c) => c,
        Err(topodb_json::ComposeError::Invalid(m)) => output::fail("rejected", &m, 2),
        Err(topodb_json::ComposeError::Engine(e)) => output::fail_engine(&e),
    };
    let value = match serde_json::to_value(&candidates) {
        Ok(v) => v,
        Err(e) => output::fail("internal", &e.to_string(), 1),
    };
    output::ok(&value, pretty);
}

/// Phase E purge: plan via the shared topodb_json builder, then either
/// report (dry-run, the default — no write path is even reachable) or
/// submit the whole RemoveNode batch atomically. An empty batch under
/// --yes skips the submit and reports seq: null.
fn purge(db: &Db, scope: Scope, tombstoned_before: i64, yes: bool, pretty: bool) -> ! {
    let scopes = topodb_json::scope_to_scope_set(scope);
    let (ops, ids) = match topodb_json::plan_purge(db, &scopes, tombstoned_before) {
        Ok(plan) => plan,
        Err(topodb_json::ComposeError::Invalid(m)) => output::fail("rejected", &m, 2),
        Err(topodb_json::ComposeError::Engine(e)) => output::fail_engine(&e),
    };
    let count = ids.len();
    if !yes {
        output::ok(
            &serde_json::json!({ "dry_run": true, "count": count, "ids": ids }),
            pretty,
        );
    }
    let seq = if ops.is_empty() {
        serde_json::Value::Null
    } else {
        match db.submit(ops) {
            Ok(applied) => serde_json::json!(applied.last_seq),
            Err(e) => output::fail_engine(&e),
        }
    };
    output::ok(
        &serde_json::json!({ "dry_run": false, "count": count, "ids": ids, "seq": seq }),
        pretty,
    );
}

fn changes(db: &Db, since: u64, pretty: bool) -> ! {
    let events = match db.ops_since(since) {
        Ok(events) => events,
        // `Compacted` (the requested range is below the retained floor) is a
        // caller-fixable condition — the caller re-anchors from current
        // state — so it routes to rejected/exit-2, not fail_engine's
        // internal/exit-1 default for non-Rejected variants. Every other
        // error (Storage, Closed, ...) is a genuine internal failure.
        Err(e @ TopoError::Compacted { .. }) => output::fail("rejected", &e.to_string(), 2),
        Err(e) => output::fail_engine(&e),
    };
    let out: Vec<serde_json::Value> = events
        .into_iter()
        .map(|ev| serde_json::json!({ "seq": ev.seq, "op": serde_json::to_value(&*ev.op).unwrap_or(serde_json::Value::Null) }))
        .collect();
    output::ok(&serde_json::Value::Array(out), pretty);
}

fn compact(db: &Db, keep_from: u64, pretty: bool) -> ! {
    if let Err(e) = db.compact_ops(keep_from) {
        output::fail_engine(&e);
    }
    output::ok(&serde_json::json!({ "oldest": keep_from }), pretty);
}

fn info(db: &Db, path: &std::path::Path, default_scope: Scope, pretty: bool) -> ! {
    let current_seq = match db.current_seq() {
        Ok(seq) => seq,
        Err(e) => output::fail_engine(&e),
    };
    let value = serde_json::json!({
        "path": path.to_string_lossy(),
        "format_version": db.format_version(),
        "current_seq": current_seq,
        "index_spec": db.index_spec(),
        "default_scope": topodb_json::scope_to_json(default_scope),
    });
    output::ok(&value, pretty);
}

/// Parses an optional `--props` JSON-object-string arg into a `Value`, for
/// handing to `merge_required_prop`/`json_to_props`. A malformed JSON string
/// is a caller-fixable input error -> `fail("rejected", .., 2)`, matching the
/// exit-code contract for bad input (never a panic).
fn parse_props_arg(props: Option<&str>) -> Option<serde_json::Value> {
    props.map(|s| match serde_json::from_str(s) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &format!("parsing --props as JSON: {e}"), 2),
    })
}

fn create_memory(db: &Db, scope: Scope, content: String, props: Option<&str>, pretty: bool) -> ! {
    // Parse --props first so malformed JSON is rejected (exit 2) even on a dedup hit.
    let extra = parse_props_arg(props);
    // Validate reserved keys BEFORE the dedup check (so reserved keys are always rejected).
    let props = match topodb_json::memory_props(&content, extra.as_ref()) {
        Ok(p) => p,
        Err(e) => output::fail("rejected", &e, 2),
    };
    // Dedup: re-storing an identical (whitespace-normalized) fact returns
    // the existing node — same contract as the MCP create_memory tool.
    match topodb_json::existing_memory(db, scope, &content) {
        Ok(Some(id)) => output::ok(
            &serde_json::json!({ "id": id.to_string(), "deduplicated": true }),
            pretty,
        ),
        Ok(None) => {}
        Err(e) => output::fail_engine(&e),
    }
    let id = NodeId::new();
    let op = Op::CreateNode {
        id,
        scope,
        label: topodb_json::MEMORY_LABEL.into(),
        props,
    };
    if let Err(e) = db.submit(vec![op]) {
        output::fail_engine(&e);
    }
    output::ok(
        &serde_json::json!({ "id": id.to_string(), "deduplicated": false }),
        pretty,
    );
}

fn create_entity(
    db: &Db,
    scope: Scope,
    name: String,
    props: Option<&str>,
    always_create: bool,
    pretty: bool,
) -> ! {
    let extra = parse_props_arg(props);
    if !always_create {
        // Same collision surface as `remember`: write scope + shared.
        let lookup = topodb_json::scopes_to_scope_set(&[scope, Scope::Shared]);
        let existing = match topodb_json::find_existing_entity(db, &lookup, &name) {
            Ok(hit) => hit,
            Err(e) => output::fail_engine(&e),
        };
        if let Some(node) = existing {
            // Merge only NEW metadata keys; never overwrite, never touch name.
            let incoming = match topodb_json::merge_required_prop(
                topodb_json::ENTITY_NAME_PROP,
                PropValue::Str(name.clone()),
                extra.as_ref(),
            ) {
                Ok(p) => p,
                Err(e) => output::fail("rejected", &e, 2),
            };
            let new_keys: std::collections::BTreeMap<String, Option<PropValue>> = incoming
                .into_iter()
                .filter(|(k, _)| k != topodb_json::ENTITY_NAME_PROP && !node.props.contains_key(k))
                .map(|(k, v)| (k, Some(v)))
                .collect();
            if !new_keys.is_empty() {
                if let Err(e) = db.submit(vec![Op::SetNodeProps {
                    id: node.id,
                    props: new_keys,
                }]) {
                    output::fail_engine(&e);
                }
            }
            output::ok(
                &serde_json::json!({ "id": node.id.to_string(), "created": false }),
                pretty,
            );
        }
    }
    let props = match topodb_json::merge_required_prop(
        topodb_json::ENTITY_NAME_PROP,
        PropValue::Str(name),
        extra.as_ref(),
    ) {
        Ok(p) => p,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let id = NodeId::new();
    let op = Op::CreateNode {
        id,
        scope,
        label: topodb_json::ENTITY_LABEL.into(),
        props,
    };
    if let Err(e) = db.submit(vec![op]) {
        output::fail_engine(&e);
    }
    output::ok(
        &serde_json::json!({ "id": id.to_string(), "created": true }),
        pretty,
    );
}

#[allow(clippy::too_many_arguments)]
fn link(
    db: &Db,
    scope: Scope,
    from: &str,
    to: &str,
    ty: String,
    props: Option<&str>,
    valid_from: Option<i64>,
    pretty: bool,
) -> ! {
    let from = match NodeId::from_str(from) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid --from id {from:?}: {e}"), 2),
    };
    let to = match NodeId::from_str(to) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid --to id {to:?}: {e}"), 2),
    };
    let props = match parse_props_arg(props) {
        Some(v) => match topodb_json::json_to_props(&v) {
            Ok(p) => p,
            Err(e) => output::fail("rejected", &e, 2),
        },
        None => topodb::Props::new(),
    };
    // Same edge-type vocabulary normalization as the MCP `link` tool and the
    // batch DSL — the three write paths must not fragment the type dict.
    let ty = match topodb_json::normalize_edge_type(&ty) {
        Ok(t) => t,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let id = EdgeId::new();
    let op = Op::CreateEdge {
        id,
        scope,
        ty: ty.clone().into(),
        from,
        to,
        props,
        valid_from,
        recorded_at: None,
    };
    if let Err(e) = db.submit(vec![op]) {
        output::fail_engine(&e);
    }
    let write_set = topodb_json::scope_to_scope_set(scope);
    let conflicts: Vec<serde_json::Value> = db
        .edges_from(&write_set, from, None, Some(&ty), true, TimeAxis::Valid)
        .unwrap_or_default()
        .into_iter()
        .filter(|e| e.id != id)
        .map(|e| {
            serde_json::json!({
                "edge_id": e.id.to_string(),
                "to": e.to.to_string(),
                "valid_from": e.valid_from,
            })
        })
        .collect();
    let mut value = serde_json::json!({ "id": id.to_string() });
    if !conflicts.is_empty() {
        value["conflicts"] = serde_json::Value::Array(conflicts);
    }
    output::ok(&value, pretty);
}

/// Composed store+link (see the spec): plan via the shared
/// `topodb_json::plan_remember`, submit the one batch, echo the plan.
#[allow(clippy::too_many_arguments)]
fn remember(
    db: &Db,
    scope: Scope,
    content: String,
    entities: Vec<String>,
    edge_type: Option<String>,
    supersedes: Vec<String>,
    props: Option<&str>,
    kind: Option<String>,
    text_mode: bool,
    pretty: bool,
) -> ! {
    let extra = parse_props_arg(props);
    let req = topodb_json::RememberRequest {
        content,
        entities,
        edge_type,
        supersedes,
        props: extra,
        kind,
    };
    // Collision surface: the write scope plus shared — a shared entity must
    // be found from a project-scoped write, not shadowed by a local twin.
    let lookup = topodb_json::scopes_to_scope_set(&[scope, Scope::Shared]);
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0);
    let plan = match topodb_json::plan_remember(db, scope, &lookup, now, &req) {
        Ok(p) => p,
        Err(topodb_json::ComposeError::Invalid(m)) => output::fail("rejected", &m, 2),
        Err(topodb_json::ComposeError::Engine(e)) => output::fail_engine(&e),
    };
    let topodb_json::RememberPlan {
        ops,
        memory_id,
        deduplicated,
        new_memory,
        entities,
        edge_ids,
        superseded,
        ..
    } = plan;
    if !ops.is_empty() {
        if let Err(e) = db.submit(ops) {
            output::fail_engine(&e);
        }
    }
    let supersession_candidates: Vec<serde_json::Value> = match &new_memory {
        Some(content) => {
            let scope_set = topodb_json::scope_to_scope_set(scope);
            let content_tokens = topodb_json::tokens(content);
            let mut scored: Vec<(String, String, f64)> = db
                .search_text_unbumped(&scope_set, content, topodb_json::TEXT_NEAR_DUP_CANDIDATES)
                .unwrap_or_default()
                .into_iter()
                .filter_map(|(n, _)| {
                    if n.label != topodb_json::MEMORY_LABEL
                        || topodb_json::MEMORY_TOMBSTONE_PROPS
                            .iter()
                            .any(|p| n.props.contains_key(*p))
                        || n.id == memory_id
                    {
                        return None;
                    }
                    let existing = match n.props.get(topodb_json::MEMORY_CONTENT_PROP) {
                        Some(PropValue::Str(c)) => c.clone(),
                        _ => return None,
                    };
                    let existing_tokens = topodb_json::tokens(&existing);
                    let containment =
                        topodb_json::containment_of_sets(&content_tokens, &existing_tokens);
                    if containment >= topodb_json::TEXT_NEAR_DUP_CONTAINMENT {
                        Some((n.id.to_string(), existing, containment))
                    } else {
                        None
                    }
                })
                .collect();
            // Rank by containment (descending) and cap at the same top-K as MCP's
            // ranked near-duplicate probe, for parity between the two write paths.
            scored.sort_by(|a, b| b.2.total_cmp(&a.2));
            scored.truncate(topodb_json::NEAR_DUP_K);
            scored
                .into_iter()
                .map(|(id, existing, containment)| {
                    serde_json::json!({
                        "memory_id": id,
                        "relation": topodb_json::dup_relation(content, &existing),
                        "score": containment,
                    })
                })
                .collect()
        }
        None => Vec::new(),
    };
    let entities: Vec<serde_json::Value> = entities
        .into_iter()
        .map(
            |e| serde_json::json!({ "name": e.name, "id": e.id.to_string(), "created": e.created }),
        )
        .collect();
    let mut value = serde_json::json!({
        "memory_id": memory_id.to_string(),
        "deduplicated": deduplicated,
        "entities": entities,
        "edge_ids": edge_ids,
        "superseded": superseded,
    });
    if !supersession_candidates.is_empty() {
        value["supersession_candidates"] = serde_json::Value::Array(supersession_candidates);
    }
    let text = {
        let id = value["memory_id"].as_str().unwrap_or("?");
        let mut line = format!("remembered {id}");
        if value["deduplicated"].as_bool().unwrap_or(false) {
            line.push_str(" (deduplicated)");
        }
        let sup = value["superseded"].as_array().map(|a| a.len()).unwrap_or(0);
        if sup > 0 {
            line.push_str(&format!(" superseding {sup}"));
        }
        Some(line)
    };
    output::render(&value, text, text_mode, pretty);
}

/// Soft-retire memories: plan via `topodb_json::plan_forget` (strict — any
/// invalid id rejects the whole call), submit the one batch, echo the ids.
fn forget(db: &Db, scope: Scope, ids: &[String], text_mode: bool, pretty: bool) -> ! {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0);
    let (ops, forgotten) = match topodb_json::plan_forget(db, scope, ids, now) {
        Ok(plan) => plan,
        Err(topodb_json::ComposeError::Invalid(m)) => output::fail("rejected", &m, 2),
        Err(topodb_json::ComposeError::Engine(e)) => output::fail_engine(&e),
    };
    if let Err(e) = db.submit(ops) {
        output::fail_engine(&e);
    }
    let value = serde_json::json!({ "forgotten": forgotten });
    let ids: Vec<&str> = value["forgotten"]
        .as_array()
        .map(|a| a.iter().filter_map(|v| v.as_str()).collect())
        .unwrap_or_default();
    let text = Some(format!("forgot {}: {}", ids.len(), ids.join(" ")));
    output::render(&value, text, text_mode, pretty);
}

fn obsidian_ingest(
    db: &Db,
    vault: &std::path::Path,
    write_scope: Scope,
    dry_run: bool,
    pretty: bool,
) -> ! {
    let lookup = topodb_json::scopes_to_scope_set(&[write_scope, Scope::Shared]);
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0);
    match topodb_obsidian::ingest_vault(db, vault, write_scope, &lookup, now, dry_run, None) {
        Ok(report) => output::ok(
            &serde_json::to_value(&report).expect("report serializes"),
            pretty,
        ),
        Err(m) => output::fail("rejected", &m, 2),
    }
}

#[allow(clippy::too_many_arguments)]
fn obsidian_seed(
    db: &Db,
    vault: &std::path::Path,
    query: Option<String>,
    k: usize,
    entity: Option<String>,
    hops: u8,
    overwrite: bool,
    scope: Scope,
    pretty: bool,
) -> ! {
    let scopes = topodb_json::scopes_to_scope_set(&[scope, Scope::Shared]);
    let memories = match (&query, &entity) {
        (Some(q), None) => topodb_obsidian::select_by_query(db, &scopes, q, k, None)
            .unwrap_or_else(|e| output::fail_engine(&e)),
        (None, Some(name)) => match topodb_obsidian::select_by_entity(db, &scopes, name, hops) {
            Ok(m) => m,
            Err(topodb_json::ComposeError::Invalid(m)) => output::fail("rejected", &m, 2),
            Err(topodb_json::ComposeError::Engine(e)) => output::fail_engine(&e),
        },
        _ => output::fail(
            "rejected",
            "exactly one of --query or --entity is required",
            2,
        ),
    };
    match topodb_obsidian::seed_vault(db, &scopes, vault, &memories, overwrite) {
        Ok(report) => output::ok(
            &serde_json::to_value(&report).expect("report serializes"),
            pretty,
        ),
        Err(m) => output::fail("rejected", &m, 2),
    }
}

fn set_props(db: &Db, id: &str, props: &str, pretty: bool) -> ! {
    let id = match NodeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid id {id:?}: {e}"), 2),
    };
    let value: serde_json::Value = match serde_json::from_str(props) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &format!("parsing --props as JSON: {e}"), 2),
    };
    let changes = match topodb_json::json_to_prop_changes(&value) {
        Ok(c) => c,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let applied = match db.submit(vec![Op::SetNodeProps { id, props: changes }]) {
        Ok(a) => a,
        Err(e) => output::fail_engine(&e),
    };
    output::ok(&serde_json::json!({ "seq": applied.last_seq }), pretty);
}

fn remove_node(db: &Db, id: &str, pretty: bool) -> ! {
    let id = match NodeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid id {id:?}: {e}"), 2),
    };
    let applied = match db.submit(vec![Op::RemoveNode { id }]) {
        Ok(a) => a,
        Err(e) => output::fail_engine(&e),
    };
    output::ok(&serde_json::json!({ "seq": applied.last_seq }), pretty);
}

fn close_edge(db: &Db, id: &str, valid_to: Option<i64>, pretty: bool) -> ! {
    let id = match EdgeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid edge id {id:?}: {e}"), 2),
    };
    let applied = match db.submit(vec![Op::CloseEdge {
        id,
        valid_to,
        superseded_at: None,
    }]) {
        Ok(a) => a,
        Err(e) => output::fail_engine(&e),
    };
    output::ok(&serde_json::json!({ "seq": applied.last_seq }), pretty);
}

fn set_embedding(db: &Db, id: &str, model: String, vector: &str, pretty: bool) -> ! {
    let id = match NodeId::from_str(id) {
        Ok(id) => id,
        Err(e) => output::fail("rejected", &format!("invalid id {id:?}: {e}"), 2),
    };
    let vector_json: serde_json::Value = match serde_json::from_str(vector) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &format!("parsing --vector as JSON: {e}"), 2),
    };
    let vector = match topodb_json::json_to_f32_vec(&vector_json) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &e, 2),
    };
    let applied = match db.submit(vec![Op::SetEmbedding { id, model, vector }]) {
        Ok(a) => a,
        Err(e) => output::fail_engine(&e),
    };
    output::ok(&serde_json::json!({ "seq": applied.last_seq }), pretty);
}

#[allow(clippy::too_many_arguments)]
fn search_vector(
    db: &Db,
    scope: Scope,
    model: String,
    vector: &str,
    k: usize,
    candidate: Vec<String>,
    pretty: bool,
) -> ! {
    let vector_json: serde_json::Value = match serde_json::from_str(vector) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &format!("parsing --vector as JSON: {e}"), 2),
    };
    let vector = match topodb_json::json_to_f32_vec(&vector_json) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &e, 2),
    };
    // Empty --candidate -> None (score the whole scope); a non-empty list is
    // parsed to NodeIds, any bad id being a caller-fixable rejected/exit-2.
    let candidates = if candidate.is_empty() {
        None
    } else {
        let mut ids = Vec::with_capacity(candidate.len());
        for c in &candidate {
            match NodeId::from_str(c) {
                Ok(id) => ids.push(id),
                Err(e) => {
                    output::fail("rejected", &format!("invalid --candidate id {c:?}: {e}"), 2)
                }
            }
        }
        Some(ids)
    };
    let scopes = topodb_json::scope_to_scope_set(scope);
    let query = VectorQuery {
        scopes,
        model,
        vector,
        k,
        candidates,
    };
    let hits = match db.search_vector(&query) {
        Ok(h) => h,
        Err(e) => output::fail_engine(&e),
    };
    let out: Result<Vec<serde_json::Value>, String> = hits
        .iter()
        .map(|(n, score)| {
            topodb_json::node_to_json(n)
                .map(|node| serde_json::json!({ "node": node, "score": score }))
        })
        .collect();
    let out = match out {
        Ok(out) => out,
        Err(e) => output::fail("internal", &e, 1),
    };
    output::ok(&serde_json::Value::Array(out), pretty);
}

fn submit(db: &Db, default_scope: Scope, input: &str, pretty: bool) -> ! {
    let raw = if input == "-" {
        let mut buf = String::new();
        if let Err(e) = std::io::stdin().read_to_string(&mut buf) {
            output::fail("internal", &format!("reading stdin: {e}"), 1);
        }
        buf
    } else {
        match std::fs::read_to_string(input) {
            Ok(s) => s,
            Err(e) => output::fail("rejected", &format!("reading {input:?}: {e}"), 2),
        }
    };
    let batch: serde_json::Value = match serde_json::from_str(&raw) {
        Ok(v) => v,
        Err(e) => output::fail("rejected", &format!("parsing batch as JSON: {e}"), 2),
    };
    let (ops, ids) = match topodb_json::resolve_batch(&batch, default_scope) {
        Ok(pair) => pair,
        Err(e) => output::fail("rejected", &e, 2),
    };
    if let Err(e) = db.submit(ops) {
        output::fail_engine(&e);
    }
    let ids: Vec<serde_json::Value> = ids
        .into_iter()
        .map(|o| {
            o.map(serde_json::Value::String)
                .unwrap_or(serde_json::Value::Null)
        })
        .collect();
    output::ok(&serde_json::json!({ "ids": ids }), pretty);
}