innate 0.1.6

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

use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};

use chrono::Utc;
use serde_json::{json, Value};

const SKILL_MD: &str = include_str!("../assets/SKILL.md");

const INNATE_TOOLS: &[&str] = &[
    "innate_recall",
    "innate_record",
    "innate_add",
    "innate_spark",
    "innate_evolve",
    "innate_inspect",
    "innate_approve",
    "innate_archive",
    "innate_invalidate",
    "innate_restore",
    "innate_mature_spark",
    "innate_promote_spark",
    "innate_drop_spark",
];

// ── Clack-style output ────────────────────────────────────────────────────────

fn tty() -> bool {
    #[cfg(unix)]
    unsafe {
        use std::os::unix::io::AsRawFd;
        libc::isatty(io::stdout().as_raw_fd()) == 1 && std::env::var("NO_COLOR").is_err()
    }
    #[cfg(not(unix))]
    false
}

fn c(s: &str, code: u8) -> String {
    if tty() {
        format!("\x1b[{code}m{s}\x1b[0m")
    } else {
        s.to_string()
    }
}

fn green(s: &str) -> String {
    c(s, 32)
}
fn gray(s: &str) -> String {
    c(s, 90)
}
fn bold(s: &str) -> String {
    c(s, 1)
}
fn cyan(s: &str) -> String {
    c(s, 36)
}
fn yellow(s: &str) -> String {
    c(s, 33)
}
fn dim(s: &str) -> String {
    c(s, 2)
}

/// `┌  title`
fn box_open(title: &str) {
    println!("{}", green(&format!("{}", bold(title))));
    println!("{}", gray(""));
}

/// `└  message`
fn box_close(msg: &str) {
    println!("{}", gray(""));
    println!("{}", green(&format!("{msg}")));
}

/// Gray vertical bar separator
fn sep() {
    println!("{}", gray(""));
}

/// `◇  question`
fn question(q: &str) {
    println!("{}", cyan(&format!("{q}")));
}

/// `│  text`
fn info(text: &str) {
    println!("{}  {text}", gray(""));
}

/// `◆  text`  (result / completed step)
fn result_line(text: &str) {
    println!("{}  {text}", green(""));
}

/// `◆  text` in yellow (warning / unchanged)
fn warn_line(text: &str) {
    println!("{}  {}", yellow(""), text);
}

// ── Interactive prompts ───────────────────────────────────────────────────────

/// Multi-select prompt. Returns a `Vec<bool>` parallel to `options`.
/// `selected[i] = true` means the option was confirmed.
///
/// Display:
/// ```text
/// ◇  prompt
/// │  [1] ✓ option A
/// │  [2] ✓ option B
/// │  [3] ✗ option C
////// │  ENTER to confirm, type a number to toggle:
/// │  >
/// ```
fn prompt_multi_select(prompt: &str, options: &[(&str, bool)]) -> Vec<bool> {
    question(prompt);
    let mut selected: Vec<bool> = options.iter().map(|(_, s)| *s).collect();

    loop {
        for (i, (name, _)) in options.iter().enumerate() {
            let mark = if selected[i] {
                green("")
            } else {
                gray("")
            };
            info(&format!("[{}] {mark} {name}", i + 1));
        }
        sep();
        info("ENTER to confirm, or type a number to toggle:");
        print!("{}  {} ", gray(""), dim(""));
        io::stdout().flush().ok();

        let line = read_line().trim().to_string();
        if line.is_empty() {
            break;
        }

        if let Ok(n) = line.parse::<usize>() {
            if n >= 1 && n <= options.len() {
                selected[n - 1] = !selected[n - 1];
                // Redraw — clear the lines we just printed (options + sep + info + prompt)
                let lines_back = options.len() + 3;
                if tty() {
                    print!("\x1b[{}A\x1b[J", lines_back);
                    io::stdout().flush().ok();
                }
                continue;
            }
        }
        break;
    }

    // Print the confirmed answer line.
    let chosen: Vec<&str> = options
        .iter()
        .zip(selected.iter())
        .filter(|(_, &s)| s)
        .map(|((name, _), _)| *name)
        .collect();
    let answer = if chosen.is_empty() {
        "none".to_string()
    } else {
        chosen.join(", ")
    };
    info(&green(&answer));
    sep();

    selected
}

/// Yes/No confirm. Returns true if the user says yes (default can be true or false).
fn prompt_confirm(prompt: &str, default_yes: bool) -> bool {
    let hint = if default_yes { "Y/n" } else { "y/N" };
    question(&format!("{prompt} ({hint})"));
    print!("{}  {} ", gray(""), dim(""));
    io::stdout().flush().ok();

    let line = read_line().trim().to_lowercase();
    let result = if line.is_empty() {
        default_yes
    } else {
        line.starts_with('y')
    };
    info(&green(if result { "Yes" } else { "No" }));
    sep();
    result
}

/// Single-select from a list of options. Returns the chosen index.
fn prompt_select(prompt: &str, options: &[&str]) -> usize {
    question(prompt);
    for (i, opt) in options.iter().enumerate() {
        info(&format!("[{}] {opt}", i + 1));
    }
    sep();
    print!("{}  {} ", gray(""), dim(""));
    io::stdout().flush().ok();

    let line = read_line().trim().to_string();
    let idx = line
        .parse::<usize>()
        .unwrap_or(1)
        .saturating_sub(1)
        .min(options.len() - 1);
    info(&green(options[idx]));
    sep();
    idx
}

fn read_line() -> String {
    let stdin = io::stdin();
    let mut line = String::new();
    stdin.lock().read_line(&mut line).ok();
    // In pipe/non-TTY mode stdin has no echo, so we need an explicit newline
    // to move past the ▶ prompt before printing the answer.
    #[cfg(unix)]
    if unsafe { libc::isatty(0) } == 0 {
        println!();
    }
    line
}

/// Free-text input prompt. Returns the user's input, or `default` if empty.
fn prompt_text(prompt: &str, default: &str, hint: &str) -> String {
    if hint.is_empty() {
        question(&format!("{prompt} {}", dim(&format!("(default: {default})"))));
    } else {
        question(&format!("{prompt} {}", dim(hint)));
    }
    print!("{}  {} ", gray(""), dim(""));
    io::stdout().flush().ok();
    let line = read_line().trim().to_string();
    let result = if line.is_empty() {
        default.to_string()
    } else {
        line
    };
    info(&green(if result.is_empty() { "skipped" } else { &result }));
    sep();
    result
}

/// Masked API key input — same as prompt_text but prints "••••••••" in the result line.
fn prompt_secret(prompt: &str, hint: &str) -> String {
    question(&format!("{prompt} {}", dim(hint)));
    print!("{}  {} ", gray(""), dim(""));
    io::stdout().flush().ok();
    let line = read_line().trim().to_string();
    let display = if line.is_empty() {
        "skipped"
    } else {
        "••••••••"
    };
    info(&green(display));
    sep();
    line
}

// ── Agent detection ───────────────────────────────────────────────────────────

#[derive(Debug)]
struct Agent {
    id: &'static str,
    label: String, // display name with "(detected)" suffix if found
    detected: bool,
    config: PathBuf,
}

fn detect_agents(global: bool) -> Vec<Agent> {
    let home = home_dir();

    // Claude Code: global = ~/.claude.json (User MCPs), project = .claude/settings.json
    let claude_global = home.join(".claude.json");
    let claude_project = PathBuf::from(".claude").join("settings.json");
    let claude_config = if global {
        claude_global.clone()
    } else {
        claude_project
    };
    let claude_detected =
        claude_global.exists() || home.join(".claude").exists() || which_binary("claude").is_some();

    // Codex CLI: always global
    let codex_config = home.join(".codex").join("config.toml");
    let codex_detected = codex_config.exists() || which_binary("codex").is_some();

    // opencode: always global
    let opencode_config = home.join(".config").join("opencode").join("opencode.jsonc");
    let opencode_detected = opencode_config.exists() || which_binary("opencode").is_some();

    vec![
        Agent {
            id: "claude",
            label: if claude_detected {
                format!("Claude Code {}", gray("(detected)"))
            } else {
                "Claude Code".to_string()
            },
            detected: claude_detected,
            config: claude_config,
        },
        Agent {
            id: "codex",
            label: if codex_detected {
                format!("Codex CLI {}", gray("(detected)"))
            } else {
                "Codex CLI".to_string()
            },
            detected: codex_detected,
            config: codex_config,
        },
        Agent {
            id: "opencode",
            label: if opencode_detected {
                format!("opencode {}", gray("(detected)"))
            } else {
                "opencode".to_string()
            },
            detected: opencode_detected,
            config: opencode_config,
        },
    ]
}

fn binary_name() -> &'static str {
    if cfg!(windows) { "innate.exe" } else { "innate" }
}

fn path_sep() -> char {
    if cfg!(windows) { ';' } else { ':' }
}

fn which_binary(name: &str) -> Option<PathBuf> {
    let exe = if cfg!(windows) && !name.ends_with(".exe") {
        format!("{name}.exe")
    } else {
        name.to_string()
    };
    std::env::var("PATH").ok().and_then(|path| {
        path.split(path_sep()).find_map(|dir| {
            let p = PathBuf::from(dir).join(&exe);
            if p.exists() { Some(p) } else { None }
        })
    })
}

// ── Config writers ────────────────────────────────────────────────────────────

#[derive(Debug)]
enum ConfigStatus {
    Updated(PathBuf),
    Unchanged(PathBuf),
    Skipped(String),
    Error(String),
}

fn configure_claude(agent: &Agent, binary: &Path, auto_allow: bool) -> ConfigStatus {
    let path = &agent.config;
    let mut settings: Value = read_json(path).unwrap_or(json!({}));

    let binary_str = binary.to_string_lossy().to_string();

    // Check current state
    let existing_cmd = settings
        .pointer("/mcpServers/innate/command")
        .and_then(Value::as_str)
        .unwrap_or("");
    let already_allowed = !auto_allow
        || settings
            .pointer("/permissions/allow")
            .and_then(Value::as_array)
            .map(|arr| arr.iter().any(|v| v.as_str() == Some("mcp__innate__*")))
            .unwrap_or(false);

    if existing_cmd == binary_str && already_allowed {
        return ConfigStatus::Unchanged(path.clone());
    }

    // Set mcpServers.innate
    settings
        .as_object_mut()
        .unwrap()
        .entry("mcpServers")
        .or_insert(json!({}))
        .as_object_mut()
        .unwrap()
        .insert(
            "innate".to_string(),
            json!({
                "type": "stdio",
                "command": binary_str,
                "args": ["mcp"]
            }),
        );

    // Set permissions.allow
    if auto_allow {
        let allow = settings
            .as_object_mut()
            .unwrap()
            .entry("permissions")
            .or_insert(json!({}))
            .as_object_mut()
            .unwrap()
            .entry("allow")
            .or_insert(json!([]));
        let arr = allow.as_array_mut().unwrap();
        let pat = "mcp__innate__*";
        if !arr.iter().any(|v| v.as_str() == Some(pat)) {
            arr.push(json!(pat));
        }
    }

    match write_json(path, &settings) {
        Ok(()) => ConfigStatus::Updated(path.clone()),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn configure_codex(agent: &Agent, binary: &Path, auto_allow: bool) -> ConfigStatus {
    let path = &agent.config;
    if !path.parent().map(|p| p.exists()).unwrap_or(false) {
        return ConfigStatus::Skipped("~/.codex/ not found — install Codex CLI first".to_string());
    }

    let existing = std::fs::read_to_string(path).unwrap_or_default();
    let binary_str = binary.to_string_lossy();

    // Check if already configured
    let already = existing.contains("[mcp_servers.innate]");
    if already {
        // Check if command matches
        if existing.contains(&format!("command = \"{binary_str}\"")) {
            return ConfigStatus::Unchanged(path.clone());
        }
    }

    let mut addition =
        format!("\n[mcp_servers.innate]\ncommand = \"{binary_str}\"\nargs = [\"mcp\"]\n");

    if auto_allow {
        for tool in INNATE_TOOLS {
            addition.push_str(&format!(
                "\n[mcp_servers.innate.tools.{tool}]\napproval_mode = \"auto\"\n"
            ));
        }
    }

    let new_content = if already {
        // Replace existing innate section (simplified: just append updated block at end)
        // Strip old innate block and append fresh one
        let stripped = strip_toml_section(&existing, "mcp_servers.innate");
        stripped + &addition
    } else {
        existing + &addition
    };

    match std::fs::write(path, new_content) {
        Ok(()) => ConfigStatus::Updated(path.clone()),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn configure_opencode(agent: &Agent, binary: &Path, _auto_allow: bool) -> ConfigStatus {
    let path = &agent.config;
    if !path.exists() {
        return ConfigStatus::Skipped("opencode.jsonc not found".into());
    }

    let txt = match std::fs::read_to_string(path) {
        Ok(t) => t,
        Err(e) => return ConfigStatus::Error(e.to_string()),
    };

    let stripped = strip_jsonc_comments(&txt);
    let mut config: Value = match serde_json::from_str(&stripped) {
        Ok(v) => v,
        Err(e) => return ConfigStatus::Error(format!("parse error: {e}")),
    };

    let binary_str = binary.to_string_lossy().to_string();

    // Check if already configured
    if let Some(existing_cmd) = config.pointer("/mcp/innate/command") {
        let already = existing_cmd
            .as_array()
            .and_then(|a| a.first())
            .and_then(Value::as_str)
            == Some(&binary_str);
        if already {
            return ConfigStatus::Unchanged(path.clone());
        }
    }

    config
        .as_object_mut()
        .unwrap()
        .entry("mcp")
        .or_insert(json!({}))
        .as_object_mut()
        .unwrap()
        .insert(
            "innate".to_string(),
            json!({
                "type": "local",
                "command": [binary_str, "mcp"],
                "enabled": true
            }),
        );

    match write_json(path, &config) {
        Ok(()) => ConfigStatus::Updated(path.clone()),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

// ── Slash-command install ─────────────────────────────────────────────────────

/// A slash command definition parsed from a `command` fenced block in SKILL.md.
struct SkillCommand {
    name: String,
    body: String,
}

/// Parse every ` ```command … ``` ` block from `skill_md`.
/// Each block has a `name: <n>` header before a bare `---` separator; body follows after.
fn parse_skill_commands(skill_md: &str) -> Vec<SkillCommand> {
    let mut cmds = Vec::new();
    let lines: Vec<&str> = skill_md.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        if lines[i].trim() == "```command" {
            i += 1;
            let block_start = i;
            while i < lines.len() && lines[i].trim() != "```" {
                i += 1;
            }
            let block_lines = &lines[block_start..i];
            if let Some(sep) = block_lines.iter().position(|l| l.trim() == "---") {
                let mut name = String::new();
                for line in &block_lines[..sep] {
                    if let Some(v) = line.strip_prefix("name:") {
                        name = v.trim().to_string();
                    }
                }
                if !name.is_empty() {
                    cmds.push(SkillCommand {
                        name,
                        body: block_lines[sep + 1..].join("\n"),
                    });
                }
            }
        }
        i += 1;
    }
    cmds
}

/// Install slash commands to `~/.claude/commands/`. Returns one status per command.
fn install_commands() -> Vec<(String, ConfigStatus)> {
    let commands_dir = home_dir().join(".claude").join("commands");
    if let Err(e) = std::fs::create_dir_all(&commands_dir) {
        return vec![("*".into(), ConfigStatus::Error(e.to_string()))];
    }
    parse_skill_commands(SKILL_MD)
        .into_iter()
        .map(|cmd| {
            let path = commands_dir.join(format!("{}.md", cmd.name));
            let current = std::fs::read_to_string(&path).unwrap_or_default();
            if current == cmd.body {
                return (cmd.name, ConfigStatus::Unchanged(path));
            }
            match std::fs::write(&path, &cmd.body) {
                Ok(()) => (cmd.name, ConfigStatus::Updated(path)),
                Err(e) => (cmd.name, ConfigStatus::Error(e.to_string())),
            }
        })
        .collect()
}

/// Remove slash commands from `~/.claude/commands/` (uninstall path).
fn remove_commands() -> Vec<(String, ConfigStatus)> {
    let commands_dir = home_dir().join(".claude").join("commands");
    parse_skill_commands(SKILL_MD)
        .into_iter()
        .map(|cmd| {
            let path = commands_dir.join(format!("{}.md", cmd.name));
            if !path.exists() {
                return (cmd.name, ConfigStatus::Skipped("not installed".into()));
            }
            match std::fs::remove_file(&path) {
                Ok(()) => (cmd.name, ConfigStatus::Updated(path)),
                Err(e) => (cmd.name, ConfigStatus::Error(e.to_string())),
            }
        })
        .collect()
}

fn skill_content_hash() -> String {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut h = DefaultHasher::new();
    SKILL_MD.hash(&mut h);
    let v = h.finish();
    format!("{v:016x}{:016x}{:08x}", !v, 0u32)
}

fn update_skill_lock(installing: bool) {
    let lock_path = home_dir().join(".agents").join(".skill-lock.json");
    let mut lock: Value =
        read_json(&lock_path).unwrap_or_else(|| json!({"version": 3, "skills": {}}));

    if installing {
        let now = Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();
        let installed_at = lock
            .pointer("/skills/innate-memory/installedAt")
            .and_then(Value::as_str)
            .map(str::to_string)
            .unwrap_or_else(|| now.clone());

        lock.as_object_mut()
            .unwrap()
            .entry("skills")
            .or_insert(json!({}))
            .as_object_mut()
            .unwrap()
            .insert(
                "innate-memory".to_string(),
                json!({
                    "source": "local",
                    "sourceType": "local",
                    "sourceUrl": null,
                    "skillPath": "skills/innate-memory/SKILL.md",
                    "skillFolderHash": skill_content_hash(),
                    "installedAt": installed_at,
                    "updatedAt": now,
                }),
            );
    } else if let Some(skills) = lock.pointer_mut("/skills") {
        if let Some(obj) = skills.as_object_mut() {
            obj.remove("innate-memory");
        }
    }

    let _ = write_json(&lock_path, &lock);
}

fn install_skill() -> ConfigStatus {
    let agents_dir = home_dir()
        .join(".agents")
        .join("skills")
        .join("innate-memory");
    let skill_file = agents_dir.join("SKILL.md");
    let claude_link = home_dir()
        .join(".claude")
        .join("skills")
        .join("innate-memory");

    // Already current?
    let up_to_date = std::fs::read_to_string(&skill_file)
        .map(|s| s == SKILL_MD)
        .unwrap_or(false)
        && (claude_link.is_symlink() || claude_link.exists());
    if up_to_date {
        return ConfigStatus::Unchanged(skill_file);
    }

    // Write SKILL.md into ~/.agents/skills/innate-memory/
    if let Err(e) = std::fs::create_dir_all(&agents_dir) {
        return ConfigStatus::Error(e.to_string());
    }
    if let Err(e) = std::fs::write(&skill_file, SKILL_MD) {
        return ConfigStatus::Error(e.to_string());
    }

    // Ensure ~/.claude/skills/ exists
    let claude_skills = home_dir().join(".claude").join("skills");
    if let Err(e) = std::fs::create_dir_all(&claude_skills) {
        return ConfigStatus::Error(e.to_string());
    }

    // Remove existing link/dir at ~/.claude/skills/innate-memory
    if claude_link.is_symlink() || claude_link.exists() {
        let _ = std::fs::remove_file(&claude_link);
        let _ = std::fs::remove_dir_all(&claude_link);
    }

    // Symlink ~/.claude/skills/innate-memory -> ../../.agents/skills/innate-memory
    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        if let Err(e) = symlink("../../.agents/skills/innate-memory", &claude_link) {
            return ConfigStatus::Error(format!("symlink: {e}"));
        }
    }
    #[cfg(not(unix))]
    {
        // Windows fallback: copy directly
        let dest = claude_link.join("SKILL.md");
        if let Err(e) = std::fs::create_dir_all(&claude_link)
            .and_then(|_| std::fs::write(&dest, SKILL_MD))
        {
            return ConfigStatus::Error(e.to_string());
        }
    }

    update_skill_lock(true);
    ConfigStatus::Updated(skill_file)
}

// ── Uninstall helpers ────────────────────────────────────────────────────────

fn remove_claude_config(config_path: &Path) -> ConfigStatus {
    if !config_path.exists() {
        return ConfigStatus::Skipped("not found".into());
    }
    let mut settings: Value = match read_json(config_path) {
        Some(v) => v,
        None => return ConfigStatus::Skipped("could not parse".into()),
    };
    let mut changed = false;

    if let Some(mcp) = settings.pointer_mut("/mcpServers") {
        if let Some(obj) = mcp.as_object_mut() {
            if obj.remove("innate").is_some() {
                changed = true;
            }
        }
    }
    if let Some(allow) = settings.pointer_mut("/permissions/allow") {
        if let Some(arr) = allow.as_array_mut() {
            let before = arr.len();
            arr.retain(|v| {
                !v.as_str()
                    .map(|s| s.starts_with("mcp__innate__"))
                    .unwrap_or(false)
            });
            if arr.len() != before {
                changed = true;
            }
        }
    }

    if !changed {
        return ConfigStatus::Unchanged(config_path.to_path_buf());
    }
    match write_json(config_path, &settings) {
        Ok(()) => ConfigStatus::Updated(config_path.to_path_buf()),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn remove_codex_config() -> ConfigStatus {
    let path = home_dir().join(".codex").join("config.toml");
    if !path.exists() {
        return ConfigStatus::Skipped("~/.codex/config.toml not found".into());
    }
    let content = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(e) => return ConfigStatus::Error(e.to_string()),
    };
    if !content.contains("[mcp_servers.innate]") {
        return ConfigStatus::Unchanged(path);
    }
    let stripped = strip_toml_section(&content, "mcp_servers.innate");
    match std::fs::write(&path, stripped) {
        Ok(()) => ConfigStatus::Updated(path),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn remove_opencode_config() -> ConfigStatus {
    let path = home_dir()
        .join(".config")
        .join("opencode")
        .join("opencode.jsonc");
    if !path.exists() {
        return ConfigStatus::Skipped("opencode.jsonc not found".into());
    }
    let txt = match std::fs::read_to_string(&path) {
        Ok(t) => t,
        Err(e) => return ConfigStatus::Error(e.to_string()),
    };
    let stripped = strip_jsonc_comments(&txt);
    let mut config: Value = match serde_json::from_str(&stripped) {
        Ok(v) => v,
        Err(e) => return ConfigStatus::Error(format!("parse error: {e}")),
    };
    let removed = config
        .pointer_mut("/mcp")
        .and_then(Value::as_object_mut)
        .and_then(|obj| obj.remove("innate"))
        .is_some();
    if !removed {
        return ConfigStatus::Unchanged(path);
    }
    match write_json(&path, &config) {
        Ok(()) => ConfigStatus::Updated(path),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn remove_skill() -> ConfigStatus {
    let agents_dir = home_dir()
        .join(".agents")
        .join("skills")
        .join("innate-memory");
    let claude_link = home_dir()
        .join(".claude")
        .join("skills")
        .join("innate-memory");

    let mut removed = false;

    if agents_dir.exists() {
        match std::fs::remove_dir_all(&agents_dir) {
            Ok(()) => removed = true,
            Err(e) => return ConfigStatus::Error(e.to_string()),
        }
    }

    if claude_link.is_symlink() || claude_link.exists() {
        if claude_link.is_symlink() {
            let _ = std::fs::remove_file(&claude_link);
        } else {
            let _ = std::fs::remove_dir_all(&claude_link);
        }
        removed = true;
    }

    if !removed {
        return ConfigStatus::Skipped("skill not installed".into());
    }

    update_skill_lock(false);
    ConfigStatus::Updated(agents_dir)
}

fn remove_binary_from_path() -> ConfigStatus {
    let dest = home_dir().join(".local").join("bin").join(binary_name());
    if !dest.exists() && !dest.is_symlink() {
        return ConfigStatus::Skipped(format!("~/.local/bin/{} not found", binary_name()));
    }
    match std::fs::remove_file(&dest) {
        Ok(()) => ConfigStatus::Updated(dest),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

fn remove_data_dir() -> ConfigStatus {
    let data = home_dir().join(".innate");
    if !data.exists() {
        return ConfigStatus::Skipped("~/.innate/ not found".into());
    }
    match std::fs::remove_dir_all(&data) {
        Ok(()) => ConfigStatus::Updated(data),
        Err(e) => ConfigStatus::Error(e.to_string()),
    }
}

pub fn run_uninstall(yes: bool, purge_data: bool) -> anyhow::Result<()> {
    let version = env!("CARGO_PKG_VERSION");
    box_open(&format!("Innate v{version} — Uninstall"));

    // ── 1. Confirm ─────────────────────────────────────────────────────────
    if !yes && !prompt_confirm("Remove Innate from your system?", false) {
        println!("{}", gray(""));
        box_close("Cancelled — nothing changed.");
        return Ok(());
    }

    // ── 2. Data prompt ─────────────────────────────────────────────────────
    let remove_data = purge_data
        || (!yes
            && prompt_confirm(
                "Also delete knowledge data (~/.innate/)? This cannot be undone.",
                false,
            ));

    // ── 3. Remove agent configs ────────────────────────────────────────────
    let global_claude = home_dir().join(".claude.json");
    // Also clean up the old incorrect path for users who installed with older versions.
    let global_claude_legacy = home_dir().join(".claude").join("settings.json");
    let project_claude = PathBuf::from(".claude").join("settings.json");

    for (label, path) in &[
        ("claude (global)", global_claude.as_path()),
        ("claude (global, legacy)", global_claude_legacy.as_path()),
        ("claude (project)", project_claude.as_path()),
    ] {
        match remove_claude_config(path) {
            ConfigStatus::Updated(p) => {
                result_line(&format!(
                    "{}: Removed MCP config {}",
                    bold(label),
                    gray(&tilde_path(&p))
                ));
            }
            ConfigStatus::Error(e) => {
                warn_line(&format!("{}: \x1b[31mError — {e}\x1b[0m", bold(label)));
            }
            _ => {}
        }
    }

    match remove_codex_config() {
        ConfigStatus::Updated(p) => {
            result_line(&format!(
                "{}: Removed MCP config {}",
                bold("codex"),
                gray(&tilde_path(&p))
            ));
        }
        ConfigStatus::Error(e) => {
            warn_line(&format!("{}: \x1b[31mError — {e}\x1b[0m", bold("codex")));
        }
        _ => {}
    }

    match remove_opencode_config() {
        ConfigStatus::Updated(p) => {
            result_line(&format!(
                "{}: Removed MCP config {}",
                bold("opencode"),
                gray(&tilde_path(&p))
            ));
        }
        ConfigStatus::Error(e) => {
            warn_line(&format!(
                "{}: \x1b[31mError — {e}\x1b[0m",
                bold("opencode")
            ));
        }
        _ => {}
    }

    // ── 4. Remove skill + slash commands ──────────────────────────────────
    match remove_skill() {
        ConfigStatus::Updated(p) => {
            result_line(&format!(
                "{}: Removed skill {}",
                bold("claude"),
                gray(&tilde_path(&p))
            ));
        }
        ConfigStatus::Error(e) => {
            warn_line(&format!(
                "{}: \x1b[31mSkill error — {e}\x1b[0m",
                bold("claude")
            ));
        }
        _ => {}
    }

    for (name, status) in remove_commands() {
        match status {
            ConfigStatus::Updated(p) => {
                result_line(&format!(
                    "{}: Removed /{name} {}",
                    bold("claude"),
                    gray(&tilde_path(&p))
                ));
            }
            ConfigStatus::Error(e) => {
                warn_line(&format!(
                    "{}: \x1b[31mCommand /{name} error — {e}\x1b[0m",
                    bold("claude")
                ));
            }
            _ => {}
        }
    }

    // ── 5. Remove binary ───────────────────────────────────────────────────
    match remove_binary_from_path() {
        ConfigStatus::Updated(p) => {
            result_line(&format!("Removed binary {}", gray(&tilde_path(&p))));
        }
        ConfigStatus::Error(e) => {
            warn_line(&format!("\x1b[31mCould not remove binary: {e}\x1b[0m"));
        }
        _ => {}
    }

    // ── 6. Remove data ─────────────────────────────────────────────────────
    if remove_data {
        match remove_data_dir() {
            ConfigStatus::Updated(p) => {
                result_line(&format!("Removed data {}", gray(&tilde_path(&p))));
            }
            ConfigStatus::Error(e) => {
                warn_line(&format!("\x1b[31mCould not remove data: {e}\x1b[0m"));
            }
            _ => {}
        }
    } else {
        info(&gray("Knowledge data (~/.innate/) kept."));
    }

    sep();
    box_close("Innate uninstalled. Restart your agents.");
    Ok(())
}

// ── PATH installation ─────────────────────────────────────────────────────────

fn check_on_path() -> Option<PathBuf> {
    which_binary("innate")
}

fn install_to_path(current_exe: &Path) -> anyhow::Result<PathBuf> {
    let local_bin = home_dir().join(".local").join("bin");
    std::fs::create_dir_all(&local_bin)?;
    let dest = local_bin.join(binary_name());

    if dest.exists() || dest.is_symlink() {
        std::fs::remove_file(&dest)?;
    }

    #[cfg(unix)]
    std::os::unix::fs::symlink(current_exe, &dest)?;
    #[cfg(not(unix))]
    std::fs::copy(current_exe, &dest)?;

    Ok(dest)
}

fn path_has_local_bin() -> bool {
    let local_bin = home_dir().join(".local").join("bin");
    std::env::var("PATH")
        .map(|p| p.split(path_sep()).any(|d| PathBuf::from(d) == local_bin))
        .unwrap_or(false)
}

fn write_path_to_profiles() -> Vec<PathBuf> {
    #[cfg(windows)]
    { write_path_windows() }
    #[cfg(not(windows))]
    { write_path_unix() }
}

/// Linux / macOS: append export line to every shell profile that exists and
/// doesn't already mention `.local/bin`.  Returns the list of files written.
#[cfg(not(windows))]
fn write_path_unix() -> Vec<PathBuf> {
    let home = home_dir();
    let export_line = r#"export PATH="$HOME/.local/bin:$PATH""#;
    let block = format!("\n# innate\n{export_line}\n");
    // .zprofile covers macOS login shells (Catalina+); .zshrc covers interactive
    let profiles = [".bashrc", ".zshrc", ".zprofile", ".bash_profile", ".profile"];
    let mut updated = Vec::new();

    for name in &profiles {
        let path = home.join(name);
        if !path.exists() {
            continue;
        }
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        if content.contains(".local/bin") {
            continue;
        }
        use std::io::Write;
        if let Ok(mut f) = std::fs::OpenOptions::new().append(true).open(&path) {
            if f.write_all(block.as_bytes()).is_ok() {
                updated.push(path);
            }
        }
    }

    // Fish shell: ~/.config/fish/config.fish
    let fish_config = home.join(".config").join("fish").join("config.fish");
    if fish_config.exists() {
        let content = std::fs::read_to_string(&fish_config).unwrap_or_default();
        if !content.contains(".local/bin") {
            let fish_block = "\n# innate\nfish_add_path \"$HOME/.local/bin\"\n";
            use std::io::Write;
            if let Ok(mut f) = std::fs::OpenOptions::new().append(true).open(&fish_config) {
                if f.write_all(fish_block.as_bytes()).is_ok() {
                    updated.push(fish_config);
                }
            }
        }
    }

    updated
}

/// Windows: modify the User-level PATH in the registry via PowerShell.
/// No new crate dependency — PowerShell ships with every modern Windows.
#[cfg(windows)]
fn write_path_windows() -> Vec<PathBuf> {
    let local_bin = home_dir().join(".local").join("bin");
    // Escape single-quotes for PowerShell string literal
    let dir = local_bin.to_string_lossy().replace('\'', "''");
    let ps = format!(
        "$dir='{dir}';\
        $old=[Environment]::GetEnvironmentVariable('PATH','User');\
        if($old -notlike \"*$dir*\"){{\
        [Environment]::SetEnvironmentVariable('PATH',$old+';'+$dir,'User')\
        }}"
    );
    let ok = std::process::Command::new("powershell.exe")
        .args(["-NoProfile", "-NonInteractive", "-Command", &ps])
        .status()
        .map(|s| s.success())
        .unwrap_or(false);
    if ok {
        vec![PathBuf::from("User PATH (Windows registry)")]
    } else {
        vec![]
    }
}

// ── Main entry point ──────────────────────────────────────────────────────────

pub fn run_install() -> anyhow::Result<()> {
    let version = env!("CARGO_PKG_VERSION");
    box_open(&format!("Innate v{version}"));

    let current_exe = std::env::current_exe()?;

    // ── 1. Scope: global vs project ────────────────────────────────────────
    let scope_options = ["All projects (global)", "Just this project"];
    let scope_idx = prompt_select(
        "Apply agent configs to all your projects, or just this one?",
        &scope_options,
    );
    let global = scope_idx == 0;

    // ── 2. Agent selection ─────────────────────────────────────────────────
    let agents = detect_agents(global);
    let options: Vec<(&str, bool)> = agents
        .iter()
        .map(|a| (a.label.as_str(), a.detected))
        .collect();
    let selected = prompt_multi_select("Which agents should Innate configure?", &options);
    let chosen_agents: Vec<&Agent> = agents
        .iter()
        .zip(selected.iter())
        .filter(|(_, &s)| s)
        .map(|(a, _)| a)
        .collect();

    // ── 3. PATH installation ───────────────────────────────────────────────
    // When the binary is on PATH, write just "innate" into agent configs so
    // the config is portable across machines and users.  Only fall back to an
    // absolute path when the user declines PATH installation.
    let on_path = check_on_path();
    let binary_path: PathBuf = if let Some(p) = &on_path {
        question("Install innate CLI on your PATH?");
        info(&format!(
            "Already on PATH {}",
            gray(&format!("({})", p.display()))
        ));
        sep();
        PathBuf::from(binary_name())
    } else {
        let do_install = prompt_confirm(
            "Install innate CLI on your PATH? (Required for agents to launch the MCP server)",
            true,
        );
        if do_install {
            match install_to_path(&current_exe) {
                Ok(dest) => {
                    result_line(&format!(
                        "Installed innate to {}",
                        bold(&dest.display().to_string())
                    ));
                    if !path_has_local_bin() {
                        let written = write_path_to_profiles();
                        if written.is_empty() {
                            #[cfg(windows)]
                            warn_line(&yellow(
                                "Add .local\\bin to PATH:\
                                \n│    [Environment]::SetEnvironmentVariable('PATH', $env:USERPROFILE + '\\.local\\bin;' + $env:PATH, 'User')",
                            ));
                            #[cfg(not(windows))]
                            warn_line(&yellow(
                                "Add ~/.local/bin to PATH in your shell profile:\
                                \n│    export PATH=\"$HOME/.local/bin:$PATH\"",
                            ));
                        } else {
                            for p in &written {
                                result_line(&format!(
                                    "Added PATH export to {}",
                                    bold(&tilde_path(p))
                                ));
                            }
                            #[cfg(windows)]
                            info(&dim("Open a new terminal for the PATH change to take effect"));
                            #[cfg(not(windows))]
                            info(&dim("Run: source ~/.bashrc  (or open a new terminal)"));
                        }
                    }
                    sep();
                    PathBuf::from(binary_name())
                }
                Err(e) => {
                    warn_line(&format!("Could not install to PATH: {e}"));
                    info("Falling back to current binary location");
                    sep();
                    current_exe.clone()
                }
            }
        } else {
            current_exe.clone()
        }
    };

    // ── 4. Auto-allow ──────────────────────────────────────────────────────
    let auto_allow = prompt_confirm(
        "Auto-allow Innate MCP tools? (Skips permission prompts in agents)",
        true,
    );

    // ── 5. LLM configuration (optional) ───────────────────────────────────
    configure_llm_interactive();

    // ── 5b. Daemon watch dirs (optional) ──────────────────────────────────
    configure_daemon_interactive();

    // ── 6. Apply configs ───────────────────────────────────────────────────
    for agent in &chosen_agents {
        let status = match agent.id {
            "claude" => configure_claude(agent, &binary_path, auto_allow),
            "codex" => configure_codex(agent, &binary_path, auto_allow),
            "opencode" => configure_opencode(agent, &binary_path, auto_allow),
            _ => ConfigStatus::Skipped("unknown agent".into()),
        };

        match &status {
            ConfigStatus::Updated(p) => {
                result_line(&format!(
                    "{}: Updated {}",
                    bold(agent.id),
                    gray(&tilde_path(p))
                ));
            }
            ConfigStatus::Unchanged(p) => {
                result_line(&format!(
                    "{}: {}",
                    bold(agent.id),
                    gray(&format!("Unchanged {}", tilde_path(p)))
                ));
            }
            ConfigStatus::Skipped(reason) => {
                warn_line(&format!(
                    "{}: {}",
                    bold(agent.id),
                    yellow(&format!("Skipped — {reason}"))
                ));
            }
            ConfigStatus::Error(e) => {
                warn_line(&format!("{}: \x1b[31mError — {e}\x1b[0m", bold(agent.id)));
            }
        }

        if agent.id == "claude" {
            match install_skill() {
                ConfigStatus::Updated(p) => {
                    result_line(&format!(
                        "{}: Installed skill {}",
                        bold("claude"),
                        gray(&tilde_path(&p))
                    ));
                }
                ConfigStatus::Unchanged(p) => {
                    result_line(&format!(
                        "{}: {}",
                        bold("claude"),
                        gray(&format!("Skill unchanged {}", tilde_path(&p)))
                    ));
                }
                ConfigStatus::Skipped(reason) => {
                    warn_line(&format!(
                        "{}: {}",
                        bold("claude"),
                        yellow(&format!("Skill skipped — {reason}"))
                    ));
                }
                ConfigStatus::Error(e) => {
                    warn_line(&format!(
                        "{}: \x1b[31mSkill error — {e}\x1b[0m",
                        bold("claude")
                    ));
                }
            }

            // Install slash commands alongside the skill
            for (name, status) in install_commands() {
                match status {
                    ConfigStatus::Updated(p) => {
                        result_line(&format!(
                            "{}: /{name} {}",
                            bold("claude"),
                            gray(&tilde_path(&p))
                        ));
                    }
                    ConfigStatus::Unchanged(_) => {}
                    ConfigStatus::Skipped(_) => {}
                    ConfigStatus::Error(e) => {
                        warn_line(&format!(
                            "{}: \x1b[31mCommand /{name} error — {e}\x1b[0m",
                            bold("claude")
                        ));
                    }
                }
            }
        }
    }
    sep();

    // ── 7. Quick start ─────────────────────────────────────────────────────
    // Box: inner display width = 28.  Total row width = │  {28}│ = 32 chars.
    // Header dashes = 28 - 12 ("Quick start ") = 16.
    // Bottom dashes = 28 + 2 = 30.
    const INNER: usize = 28;
    let bar = gray("");
    let qs_top = format!(
        "{}  Quick start {}{}",
        cyan(""),
        gray(&"".repeat(INNER - 12)),
        gray("")
    );
    let qs_row = |s: &str| -> String {
        let pad = INNER.saturating_sub(s.chars().count());
        format!("{bar}  {s}{}{bar}", " ".repeat(pad))
    };
    let qs_empty = qs_row("");
    let qs_sep = format!("{}{}", gray(""), gray(&"".repeat(INNER + 2)));

    println!("{qs_top}");
    println!("{qs_empty}");
    println!("{}", qs_row("innate recall \"query\""));
    println!("{}", qs_row("innate record <trace_id>"));
    println!("{}", qs_row("innate evolve"));
    println!("{qs_empty}");
    println!("{qs_sep}");

    box_close("Done! Restart your agents to use Innate.");
    Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn home_dir() -> PathBuf {
    dirs_next::home_dir().unwrap_or_else(|| PathBuf::from("."))
}

fn tilde_path(p: &Path) -> String {
    let home = home_dir();
    if let Ok(rel) = p.strip_prefix(&home) {
        format!("~/{}", rel.display())
    } else {
        p.display().to_string()
    }
}

fn read_json(path: &Path) -> Option<Value> {
    let txt = std::fs::read_to_string(path).ok()?;
    serde_json::from_str(&txt).ok()
}

fn write_json(path: &Path, value: &Value) -> anyhow::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let txt = serde_json::to_string_pretty(value)?;
    std::fs::write(path, txt + "\n")?;
    Ok(())
}

/// Strip `//` and `/* */` comments from a JSONC string.
fn strip_jsonc_comments(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    let mut in_str = false;
    let mut escape = false;

    while let Some(c) = chars.next() {
        if escape {
            out.push(c);
            escape = false;
            continue;
        }
        if in_str {
            if c == '\\' {
                escape = true;
                out.push(c);
                continue;
            }
            if c == '"' {
                in_str = false;
            }
            out.push(c);
            continue;
        }
        if c == '"' {
            in_str = true;
            out.push(c);
            continue;
        }
        if c == '/' {
            match chars.peek() {
                Some('/') => {
                    for nc in chars.by_ref() {
                        if nc == '\n' {
                            out.push('\n');
                            break;
                        }
                    }
                    continue;
                }
                Some('*') => {
                    chars.next();
                    while let Some(nc) = chars.next() {
                        if nc == '*' && chars.peek() == Some(&'/') {
                            chars.next();
                            break;
                        }
                    }
                    continue;
                }
                _ => {}
            }
        }
        out.push(c);
    }
    out
}

/// Remove all `[prefix.*]` TOML sections (and their keys) from a TOML string.
/// Used to replace an existing innate block when re-configuring.
fn strip_toml_section(toml: &str, section_prefix: &str) -> String {
    let mut out = String::new();
    let mut skip = false;
    for line in toml.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('[') {
            // New section header — check if it belongs to the prefix we're stripping.
            let header = trimmed.trim_start_matches('[').trim_end_matches(']');
            skip = header == section_prefix || header.starts_with(&format!("{section_prefix}."));
        }
        if !skip {
            out.push_str(line);
            out.push('\n');
        }
    }
    out
}

// ── LLM configuration step ────────────────────────────────────────────────────

fn configure_llm_interactive() {
    use crate::settings::{self, EmbeddingConfig, LlmConfig};

    let want = prompt_confirm(
        "Configure an LLM for smarter knowledge distillation? (optional)",
        false,
    );
    if !want {
        return;
    }

    // Load existing settings (preserve any already-set values).
    let mut s = settings::load();

    // ── Provider ──────────────────────────────────────────────────────────
    let provider_idx = prompt_select(
        "LLM API format:",
        &["OpenAI  (ChatGPT / DeepSeek / Ollama / …)", "Anthropic  (Claude)"],
    );
    let provider = if provider_idx == 1 {
        "anthropic".to_string()
    } else {
        "openai".to_string()
    };

    // ── Base URL (only ask when using OpenAI format, to allow custom endpoints) ──
    let default_base_url = match provider.as_str() {
        "anthropic" => "https://api.anthropic.com",
        _ => "https://api.openai.com/v1",
    };
    let base_url_raw = prompt_text(
        "Base URL",
        default_base_url,
        "(leave blank for default)",
    );
    let base_url = if base_url_raw == default_base_url || base_url_raw.is_empty() {
        None
    } else {
        Some(base_url_raw)
    };

    // ── Model ID ──────────────────────────────────────────────────────────
    let default_model = match provider.as_str() {
        "anthropic" => "claude-haiku-4-5-20251001",
        _ => "gpt-4o-mini",
    };
    let model_id = prompt_text("Model ID", default_model, "");

    // ── API key ───────────────────────────────────────────────────────────
    let env_hint = match provider.as_str() {
        "anthropic" => "(stored 0600; or set ANTHROPIC_API_KEY env var to skip)",
        _ => "(stored 0600; or set OPENAI_API_KEY env var to skip)",
    };
    let api_key_raw = prompt_secret("API key", env_hint);
    let api_key = if api_key_raw.is_empty() {
        None
    } else {
        Some(api_key_raw)
    };

    let llm_cfg = LlmConfig {
        provider: provider.clone(),
        base_url: base_url.clone(),
        model_id: model_id.clone(),
        api_key: api_key.clone(),
    };

    // ── Optional connection test ──────────────────────────────────────────
    let do_test = prompt_confirm("Test LLM connection now?", true);
    if do_test {
        info("Testing connection…");
        match crate::llm::test_llm(&llm_cfg) {
            Ok(msg) => result_line(&format!("LLM OK — {msg}")),
            Err(e) => warn_line(&format!("LLM test failed: {e}")),
        }
    }

    s.llm = Some(llm_cfg);

    // ── Embedding model (optional, defaults to same API as LLM) ──────────
    let want_embed = prompt_confirm(
        "Configure an embedding model too? (enables semantic recall — optional)",
        false,
    );
    if want_embed {
        let embed_default_url = base_url.clone().unwrap_or_else(|| "https://api.openai.com/v1".to_string());
        let embed_base_url_raw = prompt_text(
            "Embedding base URL",
            &embed_default_url,
            "(leave blank to reuse LLM base URL)",
        );
        let embed_base_url = if embed_base_url_raw == embed_default_url || embed_base_url_raw.is_empty() {
            base_url.clone()
        } else {
            Some(embed_base_url_raw)
        };

        let embed_model = prompt_text(
            "Embedding model ID",
            "text-embedding-3-small",
            "",
        );

        let embed_key_raw = prompt_secret(
            "Embedding API key",
            "(leave blank to reuse LLM API key)",
        );
        let embed_key = if embed_key_raw.is_empty() {
            api_key.clone()
        } else {
            Some(embed_key_raw)
        };

        let embed_cfg = EmbeddingConfig {
            provider: "openai".to_string(),
            base_url: embed_base_url,
            model_id: embed_model.clone(),
            api_key: embed_key.clone(),
            dim: 1536,
        };

        let do_test_embed = prompt_confirm("Test embedding connection now?", true);
        if do_test_embed {
            info("Testing embedding…");
            match crate::llm::test_embedding(&embed_cfg) {
                Ok(dim) => result_line(&format!("Embedding OK — dim={dim} model={embed_model}")),
                Err(e) => warn_line(&format!("Embedding test failed: {e}")),
            }
        }

        s.embedding = Some(embed_cfg);
    }

    // ── Save settings ─────────────────────────────────────────────────────
    match settings::save(&s) {
        Ok(()) => result_line(&format!(
            "LLM config saved to {}",
            bold(&settings::settings_path().display().to_string())
        )),
        Err(e) => warn_line(&format!("Could not save settings: {e}")),
    }
    sep();
}

// ── Daemon configuration step ─────────────────────────────────────────────────

fn configure_daemon_interactive() {
    use crate::settings::{self, DaemonConfig};

    let want = prompt_confirm(
        "Configure daemon auto-start? (auto-learns from agent conversations)",
        false,
    );
    if !want {
        return;
    }

    info("The daemon watches directories for .log files written by agents.");
    info("Example: ~/.claude/logs   or the directory your agent writes sessions to.");
    sep();

    let mut watch_dirs: Vec<String> = Vec::new();
    loop {
        let dir = prompt_text(
            "Watch directory path",
            "",
            "(leave blank to finish, ~ is expanded)",
        );
        if dir.is_empty() {
            break;
        }
        watch_dirs.push(dir.clone());
        result_line(&format!("Added: {dir}"));
    }

    if watch_dirs.is_empty() {
        warn_line("No watch directories added — daemon will not auto-start.");
        sep();
        return;
    }

    let auto_start = prompt_confirm(
        "Auto-start daemon when the MCP server launches? (recommended)",
        true,
    );

    let mut s = settings::load();
    s.daemon = Some(DaemonConfig {
        watch_dirs,
        auto_start,
    });

    match settings::save(&s) {
        Ok(()) => result_line(&format!(
            "Daemon config saved to {}",
            bold(&settings::settings_path().display().to_string())
        )),
        Err(e) => warn_line(&format!("Could not save daemon config: {e}")),
    }
    sep();
}