axiom_engine 0.1.5

Axiom-TTT Inference Engine – Rust implementation using candle
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
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
//! Native Model Context Protocol (MCP) server over a JSON-RPC 2.0 stdio
//! transport.
//!
//! This exposes Axiom to a host LLM (e.g. Claude Code) as a first-class tool
//! provider, running as a dedicated process (`--mode mcp`). It is intentionally
//! **separate** from the HTTP proxy so that:
//!
//! * `stdout` carries **only** newline-delimited JSON-RPC frames (the MCP stdio
//!   contract). Every diagnostic goes to `stderr` via `eprintln!`.
//! * the long-running proxy is never destabilised by protocol traffic.
//!
//! Both transports share the same engine internals: the [`InferencePipeline`]
//! and the persistent [`MasterVibe`].
//!
//! ## Tools
//! * `axiom_compress_path` — absorb a directory through the local TTT engine and
//!   return the resulting `<axiom_context_fingerprint>` block. Committing the
//!   adapted session into the master vibe (and persisting it) is the **explicit**
//!   merge trigger.
//! * `axiom_evaluate_drift` — cross-entropy of supplied code against the current
//!   fast-weights; a loss spike past the baseline threshold returns
//!   `isError: true` to signal architectural deviation.

use std::error::Error;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::Instant;

use candle_core::{Device, Tensor};
use serde_json::{json, Value};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};

use crate::config::AxiomConfig;
use crate::context_compressor::{
    adapt_session_blocking, extract_memory_vector_blocking, MAX_ADAPT_CHUNK_TOKENS,
};
use crate::embedder::{embed_text, EmbeddingModel};
use crate::inference::InferencePipeline;
use crate::memory_recall::{recall, RecallParams};
use crate::memory_store::{now_secs, MemoryKind, MemoryRecord, MemoryStore};
use crate::session_awareness::AwarenessStore;
use crate::vibe_memory::MasterVibe;

/// MCP protocol revision we advertise in the `initialize` handshake.
const MCP_PROTOCOL_VERSION: &str = "2024-11-05";
const SERVER_NAME: &str = "axiom-ttt";
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Token window fed through online TTT adaptation at a time. Bounds per-call
/// memory and remains strictly below the Phase 3 512-token ceiling.
const ADAPT_WINDOW_TOKENS: usize = MAX_ADAPT_CHUNK_TOKENS;
/// Cap on tokens scored by `axiom_evaluate_drift` (bounds compute per call).
const DRIFT_MAX_TOKENS: usize = 512;

/// Shared engine context handed to every tool invocation.
#[derive(Clone)]
pub struct McpContext {
    pipeline: Arc<Mutex<InferencePipeline>>,
    vibe: Arc<Mutex<MasterVibe>>,
    /// Tier-2 lossless memory store (Phase 2.0): JSONL records per scope.
    memory: Arc<Mutex<MemoryStore>>,
    /// Trained contrastive embedder (Phase 2.0.1); None → pipeline TTT fallback.
    embedder: Option<Arc<EmbeddingModel>>,
    /// When true, new tool sessions start from the master vibe instead of
    /// identity (opt-in via `AXIOM_VIBE_PRIME=1`).
    prime: bool,
    /// Cross-entropy above which `axiom_evaluate_drift` reports drift.
    drift_threshold: f32,
    /// Top-k recall indices recorded in the compression fingerprint.
    top_k: usize,
    /// Max files / bytes ingested by `axiom_compress_path`.
    max_files: usize,
    max_bytes: usize,
    /// Base URL of the running Axiom HTTP proxy (for awareness endpoint calls).
    /// Override with `AXIOM_PROXY_URL` (default `http://127.0.0.1:3000`).
    pub proxy_url: String,
    /// Inter-agent task board. Loaded once from AXIOM_TASK_DIR.
    task_board: Arc<crate::task_board::TaskBoard>,
    /// Per-session token-awareness store (PR #72). Used to record tool response
    /// costs and conditionally annotate responses with a meta cost line.
    awareness: Arc<AwarenessStore>,
}

/// Assemble an [`McpContext`] (pipeline + vibe + memory + embedder) from config,
/// device, and a checkpoint path. Shared by the stdio server and the HTTP/SSE
/// transport ([`crate::server`]'s `/mcp` route) so both expose identical tools
/// against identical engine internals.
pub async fn build_context(
    config: AxiomConfig,
    device: Device,
    checkpoint_path: String,
) -> Result<McpContext, String> {
    // Build the pipeline on a blocking thread. It owns a `reqwest::blocking::Client`
    // which carries its own runtime; constructing/dropping that inside the async
    // context is unsafe, so we keep all of its lifecycle off the async runtime.
    let pipeline = {
        let cfg = config.clone();
        let dev = device.clone();
        let ckpt = checkpoint_path.clone();
        // BPE tokenizer via AXIOM_TOKENIZER (falls back to hash tokenizer when unset).
        let tokenizer_path = std::env::var("AXIOM_TOKENIZER")
            .ok()
            .filter(|p| !p.trim().is_empty());
        tokio::task::spawn_blocking(move || {
            let runtime = crate::inference::InferenceRuntimeOptions {
                tokenizer_path,
                ..Default::default()
            };
            InferencePipeline::with_checkpoint_and_options(cfg, dev, &ckpt, runtime)
        })
        .await
        .map_err(|e| format!("pipeline build join error: {e}"))?
        .map_err(|e| format!("failed to assemble inference pipeline: {e}"))?
    };

    let vibe = MasterVibe::from_env(config.n_layers, config.d_model, &device);
    let prime = std::env::var("AXIOM_VIBE_PRIME")
        .map(|v| v == "1")
        .unwrap_or(false);
    if prime && !vibe.is_initialized() {
        eprintln!("[mcp] AXIOM_VIBE_PRIME=1 set but no master vibe yet; sessions start from identity until first commit");
    }
    // Default to the BPE-calibrated deterministic gate (7.03); override via env.
    let drift_threshold = std::env::var("AXIOM_DRIFT_THRESHOLD")
        .ok()
        .and_then(|v| v.parse::<f32>().ok())
        .unwrap_or(7.03);
    let top_k = std::env::var("AXIOM_TTT_COMPRESS_TOP_K")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
        .unwrap_or(32);
    let max_files = std::env::var("AXIOM_MCP_MAX_FILES")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
        .unwrap_or(512);
    let max_bytes = std::env::var("AXIOM_MCP_MAX_BYTES")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
        .unwrap_or(1_048_576); // 1 MiB

    // Tier-2 memory store root (Phase 2.0). Override with AXIOM_MEMORY_DIR.
    let memory_dir =
        std::env::var("AXIOM_MEMORY_DIR").unwrap_or_else(|_| "checkpoints/memory".to_string());
    let memory = MemoryStore::open(&memory_dir)
        .map_err(|e| format!("failed to open memory store at {memory_dir}: {e}"))?;
    eprintln!("[mcp] memory store at {memory_dir}");

    // Phase 2.0.1 trained embedder (CPU for the MCP path). Absent → TTT fallback.
    let embedder_ckpt = std::env::var("AXIOM_EMB_CKPT")
        .unwrap_or_else(|_| "checkpoints/axiom_embedder.bin".to_string());
    let embedder = EmbeddingModel::load(&embedder_ckpt, Device::Cpu).map(Arc::new);
    eprintln!(
        "[mcp] contrastive embedder: {}",
        if embedder.is_some() {
            "loaded"
        } else {
            "absent (pipeline fallback)"
        }
    );

    Ok(McpContext {
        pipeline: Arc::new(Mutex::new(pipeline)),
        vibe: Arc::new(Mutex::new(vibe)),
        memory: Arc::new(Mutex::new(memory)),
        embedder,
        prime,
        drift_threshold,
        top_k,
        max_files,
        max_bytes,
        proxy_url: std::env::var("AXIOM_PROXY_URL")
            .unwrap_or_else(|_| "http://127.0.0.1:3000".to_string()),
        task_board: Arc::new(
            if std::env::var("AXIOM_TASK_DIR").is_ok() {
                crate::task_board::TaskBoard::from_env()
                    .map_err(|e| format!("AXIOM_TASK_DIR open failed: {e}"))?
            } else {
                crate::task_board::TaskBoard::open("checkpoints/tasks")
                    .map_err(|e| format!("task board init failed: {e}"))?
            }
        ),
        awareness: Arc::new(AwarenessStore::new()),
    })
}

/// Boot the MCP stdio server. Runs until stdin reaches EOF (host disconnect).
pub async fn run_stdio_server(
    config: AxiomConfig,
    device: Device,
    checkpoint_path: String,
) -> Result<(), Box<dyn Error>> {
    // All status output MUST go to stderr; stdout is reserved for JSON-RPC.
    eprintln!("[mcp] booting Axiom MCP stdio server (protocol {MCP_PROTOCOL_VERSION})");

    let ctx = build_context(config, device, checkpoint_path).await?;

    eprintln!(
        "[mcp] ready — tools: axiom_compress_path, axiom_evaluate_drift, axiom_expand, \
         axiom_remember, axiom_recall, axiom_forget (prime={}, drift_threshold={})",
        ctx.prime, ctx.drift_threshold
    );

    let mut lines = BufReader::new(tokio::io::stdin()).lines();
    let mut stdout = tokio::io::stdout();

    while let Some(line) = lines.next_line().await? {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        if let Some(response) = handle_message(trimmed, &ctx).await {
            let mut payload = serde_json::to_string(&response)
                .unwrap_or_else(|e| format!(r#"{{"jsonrpc":"2.0","id":null,"error":{{"code":-32603,"message":"serialize failed: {e}"}}}}"#));
            payload.push('\n');
            stdout.write_all(payload.as_bytes()).await?;
            stdout.flush().await?;
        }
    }

    eprintln!("[mcp] stdin closed; shutting down");

    // Drop the pipeline on a blocking thread so the reqwest blocking client's
    // internal runtime is not dropped from within this async context.
    let McpContext { pipeline, .. } = ctx;
    let _ = tokio::task::spawn_blocking(move || drop(pipeline)).await;
    Ok(())
}

/// Dispatch one JSON-RPC line against an [`McpContext`] — shared by the stdio
/// loop and the HTTP/SSE transport ([`crate::server`]'s `/mcp` route). Returns
/// `Some(response)` for requests, `None` for notifications.
pub async fn dispatch(line: &str, ctx: &McpContext) -> Option<Value> {
    handle_message(line, ctx).await
}

/// Parse and route one JSON-RPC line. Returns `Some(response)` for requests,
/// `None` for notifications (no `id`) which must not be answered.
async fn handle_message(line: &str, ctx: &McpContext) -> Option<Value> {
    let req: Value = match serde_json::from_str(line) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("[mcp] parse error: {e}");
            return Some(error_response(
                Value::Null,
                -32700,
                &format!("parse error: {e}"),
            ));
        }
    };

    let id = req.get("id").cloned();
    let method = req.get("method").and_then(Value::as_str).unwrap_or("");

    // Notifications carry no id and must receive no response.
    let is_notification = id.is_none();

    match method {
        "initialize" => Some(success_response(
            id.unwrap_or(Value::Null),
            json!({
                "protocolVersion": MCP_PROTOCOL_VERSION,
                "capabilities": { "tools": {} },
                "serverInfo": { "name": SERVER_NAME, "version": SERVER_VERSION }
            }),
        )),
        "notifications/initialized" | "initialized" => {
            eprintln!("[mcp] client initialized");
            None
        }
        "ping" => Some(success_response(id.unwrap_or(Value::Null), json!({}))),
        "tools/list" => Some(success_response(id.unwrap_or(Value::Null), tools_list())),
        "tools/call" => {
            if is_notification {
                return None;
            }
            let id = id.unwrap_or(Value::Null);
            Some(handle_tools_call(id, req.get("params"), ctx).await)
        }
        other => {
            if is_notification {
                eprintln!("[mcp] ignoring unknown notification '{other}'");
                None
            } else {
                Some(error_response(
                    id.unwrap_or(Value::Null),
                    -32601,
                    &format!("method not found: {other}"),
                ))
            }
        }
    }
}

/// Static tool catalogue with strict input schemas.
fn tools_list() -> Value {
    json!({
        "tools": [
            {
                "name": "axiom_compress_path",
                "description": "Absorb a directory of source code through Axiom's local Test-Time Training engine (mutating the fast-weights), then return a dense <axiom_context_fingerprint> block summarising the compressed context. Also commits the adapted session into the persistent master vibe.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "Absolute or relative path to a directory (or single file) to compress."
                        }
                    },
                    "required": ["path"]
                }
            },
            {
                "name": "axiom_evaluate_drift",
                "description": "Compute the cross-entropy loss of the provided code against Axiom's current fast-weights. A loss spike past the baseline threshold returns isError:true, signalling architectural drift from the absorbed codebase patterns.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "code_content": {
                            "type": "string",
                            "description": "Raw source code to evaluate for architectural drift."
                        }
                    },
                    "required": ["code_content"]
                }
            },
            {
                "name": "axiom_expand",
                "description": "Retrieve the full source body of a symbol that Axiom's context compression dropped from a session digest. When you see an <axiom_context_digest> skeleton (signatures kept, bodies elided) and need a specific implementation, call this with the symbol name and the digest's session id to get the actual code back.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "symbol": {
                            "type": "string",
                            "description": "Name of the function / struct / class / type to expand."
                        },
                        "session_id": {
                            "type": "string",
                            "description": "The session id from the digest's session=\"...\" attribute. Defaults to the standard dev session if omitted."
                        }
                    },
                    "required": ["symbol"]
                }
            },
            {
                "name": "axiom_remember",
                "description": "Persist a memory (a decision, a fix, a convention, a snippet) into Axiom's long-term store so it can be recalled in future sessions. Use for things worth keeping: 'we chose X over Y because Z', a hard-won bug fix, a project convention.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "text": { "type": "string", "description": "The memory content to store, verbatim." },
                        "kind": { "type": "string", "enum": ["decision","code","conversation","fix"], "description": "Optional category (default: conversation)." },
                        "scope": { "type": "string", "description": "Optional scope: 'personal' (default) for conventions that follow you, or 'project:<name>' for repo-specific memory." }
                    },
                    "required": ["text"]
                }
            },
            {
                "name": "axiom_recall",
                "description": "Search Axiom's long-term memory for content relevant to a query and return the exact stored text (never a paraphrase). Use to answer 'what did we decide about X?', 'have I solved this before?', 'what's my convention for Y?'.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "query": { "type": "string", "description": "What to recall." },
                        "scope": { "type": "string", "description": "Optional project scope to include alongside 'personal'." }
                    },
                    "required": ["query"]
                }
            },
            {
                "name": "axiom_forget",
                "description": "Remove a memory by its id (from an axiom_recall result) so it is no longer recalled. Tombstones the record; the original line stays in the append-only log but is excluded from all future recall.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "id": { "type": "string", "description": "The memory id to forget." },
                        "scope": { "type": "string", "description": "Scope the id belongs to (default 'personal')." }
                    },
                    "required": ["id"]
                }
            },
            {
                "name": "axiom_verify",
                "description": "Grounding verification: given your draft `response` and the `evidence` it should be grounded in, flag factual claims NOT supported by that evidence (likely hallucinations relative to the context). Returns per-claim verdicts (SUPPORTED/UNSUPPORTED/UNVERIFIED), the grounded fraction, and the flagged claims. Use before asserting facts from a document/codebase. Note: this checks support against the supplied evidence; it is not universal fact-checking and the lexical tier does not catch contradictions that reuse the evidence's wording.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "response": { "type": "string", "description": "The answer/claims to verify." },
                        "evidence": { "type": "string", "description": "The source text the response must be grounded in." }
                    },
                    "required": ["response", "evidence"]
                }
            },
            {
                "name": "axiom_immunity",
                "description": "Report what Axiom has learned about program failures from supervised `axiom run` executions: the heals it now applies prophylactically (e.g. directories it pre-creates) and each program's failure-tension history. Use when debugging a command that fails in the user's environment — Axiom may already know the fix.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "command": { "type": "string", "description": "Optional case-insensitive command substring to filter by (e.g. 'cargo build'). Omit to list everything Axiom has learned." }
                    }
                }
            },
            {
                "name": "search",
                "description": "Search Axiom's long-term memory and return a ranked list of matching records as {id, title, url}. This is the standard ChatGPT-connector `search` tool (paired with `fetch`): call `search` to find relevant memories, then `fetch` with a result id to read the full text. Internally an alias over Axiom's semantic memory recall.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "query": { "type": "string", "description": "What to search Axiom's memory for." },
                        "scope": { "type": "string", "description": "Optional project scope to include alongside 'personal'." }
                    },
                    "required": ["query"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "results": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "id": { "type": "string" },
                                    "title": { "type": "string" },
                                    "url": { "type": "string" }
                                },
                                "required": ["id", "title", "url"]
                            }
                        }
                    },
                    "required": ["results"]
                }
            },
            {
                "name": "fetch",
                "description": "Fetch the full text of a single Axiom memory record by its id (an id returned by `search`). Returns {id, title, text, url, metadata}. This is the standard ChatGPT-connector `fetch` tool that pairs with `search`.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "id": { "type": "string", "description": "The record id from a `search` result." }
                    },
                    "required": ["id"]
                },
                "outputSchema": {
                    "type": "object",
                    "properties": {
                        "id": { "type": "string" },
                        "title": { "type": "string" },
                        "text": { "type": "string" },
                        "url": { "type": "string" },
                        "metadata": { "type": ["object", "null"] }
                    },
                    "required": ["id", "title", "text", "url", "metadata"]
                }
            },
            {
                "name": "axiom_status",
                "description": "Report the current session awareness state: token budget, tokens spent on Axiom responses, compression ratio, expansion-miss count, and a recommendation. Call this to understand how much context budget remains and whether Axiom's compression settings need tuning.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "session_id": {
                            "type": "string",
                            "description": "Session ID to query (default: 'mcp')."
                        }
                    }
                }
            },
            {
                "name": "axiom_post_task",
                "description": "Post a task to a named channel so another agent (Claude, Codex, or a script) can claim and execute it. Attach an Axiom context digest so the claimer can reconstruct your context without re-reading it.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "channel": { "type": "string", "description": "Work-queue channel name (e.g. 'code-review', 'codex-tasks')." },
                        "description": { "type": "string", "description": "What needs to be done." },
                        "context_digest": { "type": "string", "description": "Optional Axiom fingerprint from axiom_compress_path. The claimer can call axiom_expand to retrieve dropped symbols." },
                        "budget_snapshot": { "type": "integer", "description": "Optional remaining token count to help the claimer size its work." },
                        "priority": { "type": "integer", "description": "0 = normal, 1 = high. High-priority tasks are claimed first." },
                        "posted_by": { "type": "string", "description": "Optional agent identifier for the poster." }
                    },
                    "required": ["channel", "description"]
                }
            },
            {
                "name": "axiom_claim_task",
                "description": "Claim the next available task from a channel. Returns the highest-priority oldest pending task and marks it as in-progress. Call axiom_task_result when done.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "channel": { "type": "string", "description": "Channel to claim from." },
                        "agent_id": { "type": "string", "description": "Optional identifier of the claiming agent." }
                    },
                    "required": ["channel"]
                }
            },
            {
                "name": "axiom_task_result",
                "description": "Report the result of a claimed task. Marks it done or failed.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "task_id": { "type": "string", "description": "Task ID returned by axiom_claim_task." },
                        "result": { "type": "string", "description": "Result text, summary, or error message." },
                        "success": { "type": "boolean", "description": "true = Done, false = Failed." }
                    },
                    "required": ["task_id", "result", "success"]
                }
            },
            {
                "name": "axiom_list_tasks",
                "description": "List tasks in a channel, optionally filtered by status (pending, claimed, done, failed). Returns all tasks if no filter given.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "channel": { "type": "string", "description": "Channel to inspect." },
                        "status": { "type": "string", "description": "Optional filter: pending | claimed | done | failed." }
                    },
                    "required": ["channel"]
                }
            },
            {
                "name": "axiom_channels",
                "description": "List all task-board channels that have at least one task.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "session_id": { "type": "string", "description": "Optional session identifier." }
                    }
                }
            }
        ]
    })
}

/// Route `tools/call` to the named tool, returning a JSON-RPC response whose
/// result is an MCP tool-result payload (`{ content: [...], isError: bool }`).
async fn handle_tools_call(id: Value, params: Option<&Value>, ctx: &McpContext) -> Value {
    let Some(params) = params else {
        return error_response(id, -32602, "missing params");
    };
    let name = params.get("name").and_then(Value::as_str).unwrap_or("");
    let args = params
        .get("arguments")
        .cloned()
        .unwrap_or_else(|| json!({}));

    match name {
        "axiom_compress_path" => {
            let Some(path) = args.get("path").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_compress_path requires string 'path'");
            };
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let path = path.to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome = tokio::task::spawn_blocking(move || compress_path_blocking(&path, &ctx))
                .await
                .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(block) => {
                    let text = record_and_annotate(&awareness, &session_id, "axiom_compress_path", &block);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    eprintln!("[mcp] axiom_compress_path failed: {e}");
                    let msg = format!("compression failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_compress_path", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_evaluate_drift" => {
            let Some(code) = args.get("code_content").and_then(Value::as_str) else {
                return error_response(
                    id,
                    -32602,
                    "axiom_evaluate_drift requires string 'code_content'",
                );
            };
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let code = code.to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome = tokio::task::spawn_blocking(move || evaluate_drift_blocking(&code, &ctx))
                .await
                .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok((report, is_drift)) => {
                    let text = record_and_annotate(&awareness, &session_id, "axiom_evaluate_drift", &report);
                    success_response(id, tool_text_result(&text, is_drift))
                }
                Err(e) => {
                    eprintln!("[mcp] axiom_evaluate_drift failed: {e}");
                    let msg = format!("evaluation failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_evaluate_drift", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_expand" => {
            let Some(symbol) = args.get("symbol").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_expand requires string 'symbol'");
            };
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("axiom-dev-session")
                .to_string();
            let awareness_session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let symbol = symbol.to_string();
            let awareness = ctx.awareness.clone();
            let outcome =
                tokio::task::spawn_blocking(move || expand_symbol_blocking(&session_id, &symbol))
                    .await
                    .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok((text, is_err)) => {
                    let text = record_and_annotate(&awareness, &awareness_session_id, "axiom_expand", &text);
                    success_response(id, tool_text_result(&text, is_err))
                }
                Err(e) => {
                    eprintln!("[mcp] axiom_expand failed: {e}");
                    let msg = format!("expand failed: {e}");
                    let text = record_and_annotate(&awareness, &awareness_session_id, "axiom_expand", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_remember" => {
            let Some(text) = args.get("text").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_remember requires string 'text'");
            };
            let text = text.to_string();
            let kind = args
                .get("kind")
                .and_then(Value::as_str)
                .unwrap_or("conversation")
                .to_string();
            let scope = args
                .get("scope")
                .and_then(Value::as_str)
                .unwrap_or("personal")
                .to_string();
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome =
                tokio::task::spawn_blocking(move || remember_blocking(&text, &kind, &scope, &ctx))
                    .await
                    .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(msg) => {
                    let text = record_and_annotate(&awareness, &session_id, "axiom_remember", &msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("remember failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_remember", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_recall" => {
            let Some(query) = args.get("query").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_recall requires string 'query'");
            };
            let query = query.to_string();
            let scope = args
                .get("scope")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                recall_blocking(&query, scope.as_deref(), &ctx)
            })
            .await
            .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(report) => {
                    let text = record_and_annotate(&awareness, &session_id, "axiom_recall", &report);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("recall failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_recall", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_forget" => {
            let Some(mem_id) = args.get("id").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_forget requires string 'id'");
            };
            let mem_id = mem_id.to_string();
            let scope = args
                .get("scope")
                .and_then(Value::as_str)
                .unwrap_or("personal")
                .to_string();
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome =
                tokio::task::spawn_blocking(move || forget_blocking(&mem_id, &scope, &ctx))
                    .await
                    .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(msg) => {
                    let text = record_and_annotate(&awareness, &session_id, "axiom_forget", &msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("forget failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_forget", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_verify" => {
            let response = args.get("response").and_then(Value::as_str).unwrap_or("");
            let evidence = args.get("evidence").and_then(Value::as_str).unwrap_or("");
            if response.trim().is_empty() {
                return error_response(id, -32602, "axiom_verify requires non-empty 'response'");
            }
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let report = crate::hallucination::verify(response, evidence);
            let mut text = format!(
                "Grounding: {}/{} claims supported ({:.0}% grounded).",
                report.supported,
                report.claims.len(),
                report.grounded_fraction * 100.0
            );
            let flagged = report.flagged();
            if flagged.is_empty() {
                text.push_str(" No unsupported claims.");
            } else {
                text.push_str("\nUNSUPPORTED (not grounded in the evidence):");
                for c in flagged {
                    text.push_str(&format!("\n  - {}", c.claim));
                }
            }
            let is_error = report.unsupported > 0;
            // isError=true when anything is unsupported, so the agent notices.
            let text = record_and_annotate(&ctx.awareness, &session_id, "axiom_verify", &text);
            success_response(id, tool_text_result(&text, is_error))
        }
        "axiom_immunity" => {
            let query = args
                .get("command")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let outcome = tokio::task::spawn_blocking(move || match crate::heal_memory::HealMemory::default_path() {
                Some(path) => crate::heal_memory::HealMemory::load(&path).report_text(query.as_deref()),
                None => "Axiom heal memory is disabled (AXIOM_HEAL_MEMORY=0).".to_string(),
            })
            .await
            .unwrap_or_else(|e| format!("worker join error: {e}"));
            let outcome = record_and_annotate(&awareness, &session_id, "axiom_immunity", &outcome);
            success_response(id, tool_text_result(&outcome, false))
        }
        "search" => {
            let Some(query) = args.get("query").and_then(Value::as_str) else {
                return error_response(id, -32602, "search requires string 'query'");
            };
            let query = query.to_string();
            let scope = args
                .get("scope")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                search_blocking(&query, scope.as_deref(), &ctx)
            })
            .await
            .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(value) => {
                    // Record cost based on the JSON text representation.
                    let text_repr = serde_json::to_string(&value).unwrap_or_default();
                    record_and_annotate(&awareness, &session_id, "search", &text_repr);
                    success_response(id, tool_structured_result(value, false))
                }
                Err(e) => {
                    let msg = format!("search failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "search", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "fetch" => {
            let Some(doc_id) = args.get("id").and_then(Value::as_str) else {
                return error_response(id, -32602, "fetch requires string 'id'");
            };
            let doc_id = doc_id.to_string();
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let ctx = ctx.clone();
            let outcome = tokio::task::spawn_blocking(move || fetch_blocking(&doc_id, &ctx))
                .await
                .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(value) => {
                    let text_repr = serde_json::to_string(&value).unwrap_or_default();
                    record_and_annotate(&awareness, &session_id, "fetch", &text_repr);
                    success_response(id, tool_structured_result(value, false))
                }
                Err(e) => {
                    let msg = format!("fetch failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "fetch", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_status" => {
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("mcp")
                .to_string();
            let proxy_url = ctx.proxy_url.clone();
            let awareness = ctx.awareness.clone();
            let awareness_session_id = session_id.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                status_blocking(&session_id, &proxy_url)
            })
            .await
            .unwrap_or_else(|e| Err(format!("worker join error: {e}")));
            match outcome {
                Ok(report) => {
                    let text = record_and_annotate(&awareness, &awareness_session_id, "axiom_status", &report);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("status unavailable: {e}");
                    let text = record_and_annotate(&awareness, &awareness_session_id, "axiom_status", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_post_task" => {
            let Some(channel) = args.get("channel").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_post_task requires string 'channel'");
            };
            let Some(description) = args.get("description").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_post_task requires string 'description'");
            };
            let channel = channel.to_string();
            let description = description.to_string();
            let context_digest = args.get("context_digest").and_then(Value::as_str).map(str::to_string);
            let budget_snapshot = args.get("budget_snapshot").and_then(Value::as_u64).map(|n| n as usize);
            let priority = args.get("priority").and_then(Value::as_u64).unwrap_or(0) as u8;
            let posted_by = args.get("posted_by").and_then(Value::as_str).map(str::to_string);
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let board = ctx.task_board.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                board.post_task(&channel, description, context_digest, budget_snapshot, priority, posted_by)
            })
            .await
            .unwrap_or_else(|e| Err(std::io::Error::other(format!("join error: {e}"))));
            match outcome {
                Ok(task) => {
                    let msg = format!(
                        "Task posted.\ntask_id : {}\nchannel : {}\nstatus  : pending\ndescription: {}",
                        task.task_id, task.channel, task.description
                    );
                    let text = record_and_annotate(&awareness, &session_id, "axiom_post_task", &msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("post_task failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_post_task", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_claim_task" => {
            let Some(channel) = args.get("channel").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_claim_task requires string 'channel'");
            };
            let channel = channel.to_string();
            let agent_id = args.get("agent_id").and_then(Value::as_str).map(str::to_string);
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let board = ctx.task_board.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                board.claim_task(&channel, agent_id)
            })
            .await
            .unwrap_or_else(|e| Err(std::io::Error::other(format!("join error: {e}"))));
            match outcome {
                Ok(None) => {
                    let msg = "No pending tasks in this channel.";
                    let text = record_and_annotate(&awareness, &session_id, "axiom_claim_task", msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Ok(Some(task)) => {
                    let digest_line = task.context_digest
                        .as_deref()
                        .map(|d| format!("\ncontext_digest:\n{d}"))
                        .unwrap_or_default();
                    let budget_line = task.budget_snapshot
                        .map(|b| format!("\nbudget_snapshot: {b} tokens"))
                        .unwrap_or_default();
                    let msg = format!(
                        "Task claimed.\ntask_id    : {}\nchannel    : {}\ndescription: {}\nposted_by  : {}\npriority   : {}{}{}",
                        task.task_id, task.channel, task.description,
                        task.posted_by.as_deref().unwrap_or("unknown"),
                        task.priority, budget_line, digest_line
                    );
                    let text = record_and_annotate(&awareness, &session_id, "axiom_claim_task", &msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Err(e) => {
                    let msg = format!("claim_task failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_claim_task", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_task_result" => {
            let Some(task_id) = args.get("task_id").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_task_result requires string 'task_id'");
            };
            let Some(result) = args.get("result").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_task_result requires string 'result'");
            };
            let success = args.get("success").and_then(Value::as_bool).unwrap_or(true);
            let task_id = task_id.to_string();
            let result = result.to_string();
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let board = ctx.task_board.clone();
            let outcome = tokio::task::spawn_blocking(move || {
                board.task_result(&task_id, result, success)
            })
            .await
            .unwrap_or_else(|e| Err(std::io::Error::other(format!("join error: {e}"))));
            match outcome {
                Ok(true) => {
                    let msg = format!("Task marked {}.", if success { "done" } else { "failed" });
                    let text = record_and_annotate(&awareness, &session_id, "axiom_task_result", &msg);
                    success_response(id, tool_text_result(&text, false))
                }
                Ok(false) => {
                    let msg = "task_id not found.";
                    let text = record_and_annotate(&awareness, &session_id, "axiom_task_result", msg);
                    success_response(id, tool_text_result(&text, true))
                }
                Err(e) => {
                    let msg = format!("task_result failed: {e}");
                    let text = record_and_annotate(&awareness, &session_id, "axiom_task_result", &msg);
                    success_response(id, tool_text_result(&text, true))
                }
            }
        }
        "axiom_list_tasks" => {
            let Some(channel) = args.get("channel").and_then(Value::as_str) else {
                return error_response(id, -32602, "axiom_list_tasks requires string 'channel'");
            };
            let channel = channel.to_string();
            let status_filter = args.get("status").and_then(Value::as_str).map(str::to_string);
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let awareness = ctx.awareness.clone();
            let board = ctx.task_board.clone();
            let tasks = tokio::task::spawn_blocking(move || {
                board.list_tasks(&channel, status_filter.as_deref())
            })
            .await
            .unwrap_or_default();
            if tasks.is_empty() {
                let text = record_and_annotate(&awareness, &session_id, "axiom_list_tasks", "No tasks found.");
                return success_response(id, tool_text_result(&text, false));
            }
            let lines: Vec<String> = tasks.iter().map(|t| {
                format!(
                    "[{}] {} | {} | {}{}",
                    t.status,
                    t.task_id.get(..8).unwrap_or(&t.task_id),
                    t.description,
                    t.posted_by.as_deref().unwrap_or("?"),
                    t.result.as_deref().map(|r| format!(" → {}", r.lines().next().unwrap_or(r))).unwrap_or_default()
                )
            }).collect();
            let msg = lines.join("\n");
            let text = record_and_annotate(&awareness, &session_id, "axiom_list_tasks", &msg);
            success_response(id, tool_text_result(&text, false))
        }
        "axiom_channels" => {
            let session_id = args
                .get("session_id")
                .and_then(Value::as_str)
                .unwrap_or("default")
                .to_string();
            let board = ctx.task_board.clone();
            let channels = tokio::task::spawn_blocking(move || board.channels())
                .await
                .unwrap_or_default();
            let text = if channels.is_empty() {
                "No task channels found.".to_string()
            } else {
                format!("Channels ({}): {}", channels.len(), channels.join(", "))
            };
            let text = record_and_annotate(&ctx.awareness, &session_id, "axiom_channels", &text);
            success_response(id, tool_text_result(&text, false))
        }
        other => error_response(id, -32602, &format!("unknown tool: {other}")),
    }
}

/// `axiom_status` worker — calls `GET /v1/awareness/{session_id}` on the running
/// proxy and formats the JSON as a human-readable status block.
fn status_blocking(session_id: &str, proxy_url: &str) -> Result<String, String> {
    let url = format!(
        "{}/v1/awareness/{}",
        proxy_url.trim_end_matches('/'),
        urlencoding::encode(session_id)
    );
    let client = reqwest::blocking::Client::new();
    let resp = client
        .get(&url)
        .send()
        .map_err(|e| format!("could not reach Axiom proxy at {url}: {e}"))?;
    let status = resp.status();
    if status.as_u16() == 404 {
        return Ok(format!(
            "No awareness state for session '{session_id}' yet.              Call POST /v1/budget to report your token budget."
        ));
    }
    if !status.is_success() {
        return Err(format!("proxy returned HTTP {status}"));
    }
    let v: serde_json::Value = resp
        .json()
        .map_err(|e| format!("bad JSON from proxy: {e}"))?;
    // Format as a readable block.
    let budget = v["budget_remaining"]
        .as_u64()
        .map(|n| format!("{n} tokens"))
        .unwrap_or_else(|| "not set".to_string());
    let spent = v["tokens_spent_on_axiom"].as_u64().unwrap_or(0);
    let tool_calls = v["tool_calls_total"].as_u64().unwrap_or(0);
    let expansions = v["expansion_calls"].as_u64().unwrap_or(0);
    let ratio = v["compression_ratio"]
        .as_f64()
        .map(|r| format!("{:.1}%", r * 100.0))
        .unwrap_or_else(|| "no data".to_string());
    let tight = v["is_tight"].as_bool().unwrap_or(false);
    let model = v["target_model"].as_str().unwrap_or("unknown");
    let rec = v["recommendation"].as_str().unwrap_or("—");
    Ok(format!(
        "=== Axiom Session Awareness: {session_id} ===
         model            : {model}
         budget remaining : {budget}{tight_flag}
         spent on Axiom   : {spent} tokens across {tool_calls} tool calls
         compression ratio: {ratio} (expand misses: {expansions})
         recommendation   : {rec}",
        tight_flag = if tight { " ⚠ TIGHT" } else { "" }
    ))
}

/// `axiom_expand` worker — HTTP-calls the running proxy's `POST /v1/expand` to
/// retrieve a dropped symbol body. The MCP server is a separate process from the
/// proxy, so it reaches it over the loopback API. Override the proxy location
/// with `AXIOM_PROXY_URL` (default `http://127.0.0.1:3000`).
/// Returns `(text, is_error)`.
fn expand_symbol_blocking(session_id: &str, symbol: &str) -> Result<(String, bool), String> {
    let base =
        std::env::var("AXIOM_PROXY_URL").unwrap_or_else(|_| "http://127.0.0.1:3000".to_string());
    let url = format!("{}/v1/expand", base.trim_end_matches('/'));
    let client = reqwest::blocking::Client::new();
    let resp = client
        .post(&url)
        .json(&json!({ "session_id": session_id, "symbol": symbol }))
        .send()
        .map_err(|e| format!("could not reach Axiom proxy at {url}: {e}"))?;
    let status = resp.status();
    let body: Value = resp
        .json()
        .map_err(|e| format!("bad response from proxy ({status}): {e}"))?;
    if body.get("found").and_then(Value::as_bool).unwrap_or(false) {
        let code = body
            .get("body")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();
        Ok((code, false))
    } else {
        let err = body
            .get("error")
            .and_then(Value::as_str)
            .unwrap_or("symbol not found")
            .to_string();
        Ok((
            format!("could not expand '{symbol}' (HTTP {status}): {err}"),
            true,
        ))
    }
}

/// Allocate the starting W̃ states for a tool session: primed from the master
/// vibe when opt-in priming is on and a master exists, else identity.
fn start_states(ctx: &McpContext, pipeline: &InferencePipeline) -> Result<Vec<Tensor>, String> {
    if ctx.prime {
        if let Ok(vibe) = ctx.vibe.lock() {
            if let Some(primed) = vibe.prime_states() {
                return Ok(primed);
            }
        }
    }
    pipeline.init_session_states().map_err(|e| e.to_string())
}

/// `axiom_compress_path` worker. Reads the directory, streams it through the
/// TTT engine, extracts the fingerprint, then commits + persists the master.
fn compress_path_blocking(path: &str, ctx: &McpContext) -> Result<String, String> {
    let started = Instant::now();
    let p = Path::new(path);
    if !p.exists() {
        return Err(format!("path does not exist: {path}"));
    }

    // Gather text content (bounded by max_files / max_bytes).
    let mut total_bytes = 0usize;
    let mut file_count = 0usize;
    let mut corpus = String::new();
    if p.is_file() {
        if let Ok(text) = std::fs::read_to_string(p) {
            corpus.push_str(&text);
            file_count = 1;
        }
    } else {
        for entry in walkdir::WalkDir::new(p)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            if file_count >= ctx.max_files || total_bytes >= ctx.max_bytes {
                break;
            }
            if let Ok(text) = std::fs::read_to_string(entry.path()) {
                let remaining = ctx.max_bytes.saturating_sub(total_bytes);
                let slice = if text.len() > remaining {
                    &text[..remaining]
                } else {
                    &text[..]
                };
                corpus.push_str(slice);
                corpus.push('\n');
                total_bytes += slice.len();
                file_count += 1;
            }
        }
    }
    if corpus.trim().is_empty() {
        return Err(format!("no readable text content under {path}"));
    }

    let pipeline = ctx
        .pipeline
        .lock()
        .map_err(|_| "pipeline lock poisoned".to_string())?;
    let token_ids = pipeline.encode_text(&corpus);
    let tokens_processed = token_ids.len();

    let mut states = start_states(ctx, &pipeline)?;

    // Adapt in bounded windows to cap per-call memory.
    for window in token_ids.chunks(ADAPT_WINDOW_TOKENS) {
        adapt_session_blocking(&pipeline, &mut states, window).map_err(|e| e.to_string())?;
    }

    // Recall pass + fingerprint. Use the tail of the corpus as the query.
    let query: Vec<u32> = token_ids.iter().rev().take(32).rev().copied().collect();
    let session_id = format!("mcp-compress-{}", short_hash(path));
    let fingerprint = extract_memory_vector_blocking(
        &pipeline,
        &mut states,
        &query,
        &session_id,
        tokens_processed,
        started,
        ctx.top_k,
    )
    .map_err(|e| e.to_string())?;

    // Explicit merge trigger: fold this session into the persistent master vibe.
    match ctx.vibe.lock() {
        Ok(mut vibe) => {
            if let Err(e) = vibe.commit_and_save(&states) {
                eprintln!("[mcp] vibe commit skipped: {e}");
            }
        }
        Err(_) => eprintln!("[mcp] vibe lock poisoned; commit skipped"),
    }

    eprintln!(
        "[mcp] compressed {file_count} file(s) / {tokens_processed} tokens from {path} \
         (recall_norm={:.3})",
        fingerprint.recall_norm
    );
    Ok(fingerprint.to_prompt_block())
}

/// `axiom_evaluate_drift` worker. Returns `(report_text, is_drift)`.
fn evaluate_drift_blocking(code: &str, ctx: &McpContext) -> Result<(String, bool), String> {
    let pipeline = ctx
        .pipeline
        .lock()
        .map_err(|_| "pipeline lock poisoned".to_string())?;
    let mut ids = pipeline.encode_text(code);
    if ids.len() < 2 {
        return Ok((
            "input too short to evaluate drift (need >= 2 tokens)".to_string(),
            false,
        ));
    }
    ids.truncate(DRIFT_MAX_TOKENS);
    let n = ids.len();
    let device = pipeline.device();

    let mut states = start_states(ctx, &pipeline)?;

    // Next-token prediction: predict ids[1..] from ids[..n-1].
    let input =
        Tensor::from_vec(ids[..n - 1].to_vec(), (1, n - 1), device).map_err(|e| e.to_string())?;
    let logits = pipeline
        .model()
        .forward_lm(&input, &mut states)
        .map_err(|e| e.to_string())?; // [1, n-1, vocab]
    let vocab = pipeline.model().config.vocab_size;
    let logits_2d = logits
        .squeeze(0)
        .and_then(|t| t.reshape((n - 1, vocab)))
        .map_err(|e| e.to_string())?;
    let targets =
        Tensor::from_vec(ids[1..].to_vec(), (n - 1,), device).map_err(|e| e.to_string())?;
    let loss = candle_nn::loss::cross_entropy(&logits_2d, &targets).map_err(|e| e.to_string())?;
    let loss_val = loss.to_scalar::<f32>().map_err(|e| e.to_string())?;

    let is_drift = loss_val > ctx.drift_threshold;
    let report = format!(
        "cross_entropy_loss={loss_val:.4} baseline_threshold={:.4} tokens_scored={} drift={}",
        ctx.drift_threshold,
        n,
        if is_drift { "YES" } else { "no" }
    );
    eprintln!("[mcp] evaluate_drift -> {report}");
    Ok((report, is_drift))
}

/// Embed via the trained contrastive encoder when present, else the pipeline
/// (TTT pooling). The single embedding entry point for remember + recall.
fn embed_query(text: &str, ctx: &McpContext) -> Result<Vec<f32>, String> {
    if let Some(e) = &ctx.embedder {
        return e.embed(text).map_err(|err| err.to_string());
    }
    let pipeline = ctx
        .pipeline
        .lock()
        .map_err(|_| "pipeline lock poisoned".to_string())?;
    embed_text(&pipeline, text).map_err(|err| err.to_string())
}

/// `axiom_remember` worker. Embeds the text, scores its drift (salience), and
/// appends a record to the chosen scope. Returns the new memory id.
fn remember_blocking(
    text: &str,
    kind: &str,
    scope: &str,
    ctx: &McpContext,
) -> Result<String, String> {
    let embedding = embed_query(text, ctx)?;
    // Salience = drift cross-entropy (reuses the evaluate path's signal cheaply).
    let drift = {
        let pipeline = ctx
            .pipeline
            .lock()
            .map_err(|_| "pipeline lock poisoned".to_string())?;
        drift_score(&pipeline, text).unwrap_or(0.0)
    };

    let kind = match kind {
        "decision" => MemoryKind::Decision,
        "code" => MemoryKind::Code,
        "fix" => MemoryKind::Fix,
        _ => MemoryKind::Conversation,
    };
    let id = short_hash(&format!("{scope}|{}|{text}", now_secs()));
    let rec = MemoryRecord {
        id: id.clone(),
        scope: scope.to_string(),
        ts: now_secs(),
        kind,
        body: text.to_string(),
        embedding,
        drift_at_ingest: drift,
        supersedes: None,
        tombstone: false,
    };
    let store = ctx
        .memory
        .lock()
        .map_err(|_| "memory lock poisoned".to_string())?;
    store.append(&rec).map_err(|e| e.to_string())?;
    Ok(format!(
        "remembered id={id} scope={scope} (salience drift={drift:.2})"
    ))
}

/// `axiom_recall` worker. Embeds the query and searches `personal` ∪ optional
/// project scope, returning a readable list of exact stored bodies + ids.
fn recall_blocking(query: &str, scope: Option<&str>, ctx: &McpContext) -> Result<String, String> {
    let q_emb = embed_query(query, ctx)?;

    let mut scopes = vec!["personal".to_string()];
    if let Some(s) = scope {
        if s != "personal" {
            scopes.push(s.to_string());
        }
    }
    let store = ctx
        .memory
        .lock()
        .map_err(|_| "memory lock poisoned".to_string())?;
    let hits = recall(&store, &scopes, &q_emb, &RecallParams::default());
    if hits.is_empty() {
        return Ok("no relevant memories found".to_string());
    }
    let mut out = String::from("<axiom_recall>\n");
    for h in &hits {
        out.push_str(&format!(
            "• [{:.2}] id={} scope={} ts={}\n  {}\n",
            h.score, h.record.id, h.record.scope, h.record.ts, h.record.body
        ));
    }
    out.push_str("</axiom_recall>");
    Ok(out)
}

/// Percent-encode a URI segment: RFC 3986 unreserved characters pass through,
/// everything else becomes `%XX`. Keeps memory locators/ids valid for scopes
/// like `project:acme` (a raw `:` would otherwise be misread).
fn pct_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char)
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

/// Inverse of [`pct_encode`]. Invalid escapes are passed through literally so it
/// never panics on malformed input.
fn pct_decode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' && i + 3 <= bytes.len() {
            if let Ok(b) = u8::from_str_radix(&s[i + 1..i + 3], 16) {
                out.push(b);
                i += 3;
                continue;
            }
        }
        out.push(bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// Connector-safe, stable opaque locator for a memory record (shown as `url` in
/// search/fetch results so ChatGPT can display/cite a source without exposing
/// the filesystem). Scope and id live in the percent-encoded *path*, never the
/// authority, so values like `project:acme` cannot be misparsed as `host:port`.
fn memory_url(scope: &str, id: &str) -> String {
    format!(
        "axiom-memory://memory/{}/{}",
        pct_encode(scope),
        pct_encode(id)
    )
}

/// The scope-qualified id surfaced by `search` and consumed by `fetch`, so the
/// round-trip resolves to exactly the record `search` returned regardless of how
/// raw ids are allocated. Format `<pct-encoded-scope>:<raw-id>` — raw ids are
/// hex, so the first `:` unambiguously separates the two parts.
fn qualified_id(scope: &str, id: &str) -> String {
    format!("{}:{}", pct_encode(scope), id)
}

/// Parse a [`qualified_id`] back into `(scope, raw_id)`. Returns `None` for a
/// bare id (no `:`), letting `fetch` fall back to a scope-scan for legacy ids.
fn parse_qualified_id(doc_id: &str) -> Option<(String, String)> {
    doc_id
        .split_once(':')
        .map(|(s, i)| (pct_decode(s), i.to_string()))
}

/// A short single-line title from a record body (first non-empty line, truncated).
fn title_for(body: &str) -> String {
    let line = body.lines().find(|l| !l.trim().is_empty()).unwrap_or("").trim();
    const MAX: usize = 80;
    if line.chars().count() > MAX {
        let truncated: String = line.chars().take(MAX - 1).collect();
        format!("{truncated}…")
    } else if line.is_empty() {
        "(untitled memory)".to_string()
    } else {
        line.to_string()
    }
}

/// `search` worker (ChatGPT-connector alias over recall). Returns the JSON value
/// `{"results":[{"id","title","url"}]}` — the shape the standard ChatGPT search
/// tool expects. Empty `results` when nothing matches (not an error).
fn search_blocking(query: &str, scope: Option<&str>, ctx: &McpContext) -> Result<Value, String> {
    let q_emb = embed_query(query, ctx)?;
    let mut scopes = vec!["personal".to_string()];
    if let Some(s) = scope {
        if s != "personal" {
            scopes.push(s.to_string());
        }
    }
    let store = ctx
        .memory
        .lock()
        .map_err(|_| "memory lock poisoned".to_string())?;
    let hits = recall(&store, &scopes, &q_emb, &RecallParams::default());
    let results: Vec<Value> = hits
        .iter()
        .map(|h| {
            json!({
                "id": qualified_id(&h.record.scope, &h.record.id),
                "title": title_for(&h.record.body),
                "url": memory_url(&h.record.scope, &h.record.id),
            })
        })
        .collect();
    Ok(json!({ "results": results }))
}

/// `fetch` worker (ChatGPT-connector alias). Resolves a `search` result id to its
/// full stored text, returning JSON `{"id","title","text","url","metadata"}`.
/// A scope-qualified id (the form `search` emits) resolves the exact scope/record
/// deterministically; a bare legacy id falls back to a cross-scope lookup.
fn fetch_blocking(doc_id: &str, ctx: &McpContext) -> Result<Value, String> {
    let store = ctx
        .memory
        .lock()
        .map_err(|_| "memory lock poisoned".to_string())?;
    let rec = match parse_qualified_id(doc_id) {
        Some((scope, raw_id)) => store
            .load_scope(&scope)
            .into_iter()
            .find(|r| r.id == raw_id),
        None => store.get(doc_id),
    }
    .ok_or_else(|| format!("no memory record with id={doc_id}"))?;
    Ok(json!({
        // Echo the qualified id so a follow-up fetch is still unambiguous.
        "id": qualified_id(&rec.scope, &rec.id),
        "title": title_for(&rec.body),
        "text": rec.body,
        "url": memory_url(&rec.scope, &rec.id),
        "metadata": { "scope": rec.scope, "ts": rec.ts },
    }))
}

/// `axiom_forget` worker. Tombstones an id in its scope.
fn forget_blocking(mem_id: &str, scope: &str, ctx: &McpContext) -> Result<String, String> {
    let store = ctx
        .memory
        .lock()
        .map_err(|_| "memory lock poisoned".to_string())?;
    store.tombstone(scope, mem_id).map_err(|e| e.to_string())?;
    Ok(format!("forgot id={mem_id} scope={scope}"))
}

/// Cross-entropy of `text` against current fast-weights — used as a salience
/// score at ingest. Mirrors `evaluate_drift_blocking` but returns the raw loss.
fn drift_score(pipeline: &InferencePipeline, text: &str) -> Result<f32, String> {
    let mut ids = pipeline.encode_text(text);
    if ids.len() < 2 {
        return Ok(0.0);
    }
    ids.truncate(DRIFT_MAX_TOKENS);
    let n = ids.len();
    let device = pipeline.device();
    let mut states = pipeline.init_session_states().map_err(|e| e.to_string())?;
    let input =
        Tensor::from_vec(ids[..n - 1].to_vec(), (1, n - 1), device).map_err(|e| e.to_string())?;
    let logits = pipeline
        .model()
        .forward_lm(&input, &mut states)
        .map_err(|e| e.to_string())?;
    let vocab = pipeline.model().config.vocab_size;
    let logits_2d = logits
        .squeeze(0)
        .and_then(|t| t.reshape((n - 1, vocab)))
        .map_err(|e| e.to_string())?;
    let targets =
        Tensor::from_vec(ids[1..].to_vec(), (n - 1,), device).map_err(|e| e.to_string())?;
    let loss = candle_nn::loss::cross_entropy(&logits_2d, &targets).map_err(|e| e.to_string())?;
    loss.to_scalar::<f32>().map_err(|e| e.to_string())
}

// ---------------------------------------------------------------------------
// Per-response token-cost tracking
// ---------------------------------------------------------------------------

/// Estimate token cost, record it against the awareness store, and optionally
/// append a compact `_meta: token_cost=N` line to the response text.
///
/// The meta line is only appended when a budget has already been set for the
/// session (i.e. `awareness.get(session_id).is_some()`), keeping responses
/// clean until the agent opts in via `POST /v1/budget`.
fn record_and_annotate(
    awareness: &AwarenessStore,
    session_id: &str,
    tool_name: &str,
    response_text: &str,
) -> String {
    let token_cost = (response_text.len() / 4).max(1);
    // Always record in the store (creates the entry if it already exists).
    // We only record if the session has been opted in (budget set).
    let annotate = if let Some(state) = awareness.get(session_id) {
        state.record_tool_response(tool_name, token_cost);
        true
    } else {
        false
    };
    if annotate {
        format!("{response_text}\n\n_meta: token_cost={token_cost}")
    } else {
        response_text.to_string()
    }
}

// ---------------------------------------------------------------------------
// JSON-RPC helpers
// ---------------------------------------------------------------------------

fn success_response(id: Value, result: Value) -> Value {
    json!({ "jsonrpc": "2.0", "id": id, "result": result })
}

fn error_response(id: Value, code: i64, message: &str) -> Value {
    json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": message } })
}

/// Build an MCP tool-result payload.
fn tool_text_result(text: &str, is_error: bool) -> Value {
    json!({
        "content": [ { "type": "text", "text": text } ],
        "isError": is_error
    })
}

/// Build an MCP tool-result payload that carries a structured object in
/// `structuredContent` *and* a JSON-encoded text mirror in `content`. The MCP
/// spec lets a tool return both; ChatGPT standard/company-knowledge connectors
/// read `structuredContent` (validated against the tool's `outputSchema`), while
/// the text mirror keeps plain text-only clients working.
fn tool_structured_result(structured: Value, is_error: bool) -> Value {
    let text = serde_json::to_string(&structured).unwrap_or_else(|_| "{}".to_string());
    json!({
        "content": [ { "type": "text", "text": text } ],
        "structuredContent": structured,
        "isError": is_error
    })
}

/// Short stable id derived from a path, for session labelling.
fn short_hash(s: &str) -> String {
    use sha2::{Digest, Sha256};
    let digest = Sha256::digest(s.as_bytes());
    format!(
        "{:x}{:x}{:x}{:x}",
        digest[0], digest[1], digest[2], digest[3]
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn tools_list_exposes_tools_with_schemas() {
        let list = tools_list();
        let tools = list["tools"].as_array().unwrap();
        assert_eq!(tools.len(), 16);
        let names: Vec<&str> = tools.iter().map(|t| t["name"].as_str().unwrap()).collect();
        assert!(names.contains(&"axiom_compress_path"));
        assert!(names.contains(&"axiom_evaluate_drift"));
        assert!(names.contains(&"axiom_expand"));
        assert!(names.contains(&"axiom_status"));
        assert!(names.contains(&"axiom_post_task"));
        assert!(names.contains(&"axiom_claim_task"));
        assert!(names.contains(&"axiom_task_result"));
        assert!(names.contains(&"axiom_list_tasks"));
        assert!(names.contains(&"axiom_channels"));
        for t in tools {
            assert_eq!(t["inputSchema"]["type"], "object");
            // `required` is optional in MCP (axiom_immunity has only optional
            // params); when present it must be an array.
            if let Some(req) = t["inputSchema"].get("required") {
                assert!(req.is_array());
            }
        }
    }

    #[test]
    fn tools_list_includes_memory_tools() {
        let list = tools_list();
        let tools = list["tools"].as_array().unwrap();
        let names: Vec<&str> = tools.iter().map(|t| t["name"].as_str().unwrap()).collect();
        assert!(names.contains(&"axiom_remember"));
        assert!(names.contains(&"axiom_recall"));
        assert!(names.contains(&"axiom_forget"));
    }

    #[test]
    fn tools_list_includes_immunity_tool() {
        let list = tools_list();
        let names: Vec<&str> = list["tools"]
            .as_array()
            .unwrap()
            .iter()
            .map(|t| t["name"].as_str().unwrap())
            .collect();
        assert!(names.contains(&"axiom_immunity"));
    }

    #[test]
    fn tools_list_includes_chatgpt_search_fetch_aliases() {
        // The standard ChatGPT connector requires tools literally named
        // `search` and `fetch`; assert both are present with the right required
        // params so a standard-mode connector can attach.
        let list = tools_list();
        let tools = list["tools"].as_array().unwrap();
        let search = tools.iter().find(|t| t["name"] == "search").unwrap();
        let fetch = tools.iter().find(|t| t["name"] == "fetch").unwrap();
        assert_eq!(search["inputSchema"]["required"][0], "query");
        assert_eq!(fetch["inputSchema"]["required"][0], "id");
    }

    #[test]
    fn title_for_takes_first_nonempty_line_and_truncates() {
        assert_eq!(title_for("\n\nhello world\nmore"), "hello world");
        assert_eq!(title_for("   "), "(untitled memory)");
        let long = "x".repeat(200);
        let t = title_for(&long);
        assert!(t.chars().count() <= 80, "title too long: {}", t.chars().count());
        assert!(t.ends_with('…'));
    }

    #[test]
    fn memory_url_is_stable_and_encodes_special_scopes() {
        assert_eq!(
            memory_url("personal", "abc123"),
            "axiom-memory://memory/personal/abc123"
        );
        // A scope with a colon must not land raw in the authority.
        assert_eq!(
            memory_url("project:acme", "abc123"),
            "axiom-memory://memory/project%3Aacme/abc123"
        );
    }

    #[test]
    fn qualified_id_roundtrips_including_special_scopes() {
        for (scope, id) in [("personal", "abc123"), ("project:acme", "deadbeef")] {
            let q = qualified_id(scope, id);
            let (s, i) = parse_qualified_id(&q).expect("qualified id should parse");
            assert_eq!(s, scope);
            assert_eq!(i, id);
        }
        // A bare (legacy) id has no ':' and parses as None → fetch falls back.
        assert!(parse_qualified_id("abc123").is_none());
    }

    #[test]
    fn pct_encode_decode_roundtrip() {
        for s in ["personal", "project:acme", "a/b c", "weird%20"] {
            assert_eq!(pct_decode(&pct_encode(s)), s);
        }
    }

    #[test]
    fn initialize_advertises_tools_capability() {
        // Build the same result initialize returns and assert its shape.
        let result = json!({
            "protocolVersion": MCP_PROTOCOL_VERSION,
            "capabilities": { "tools": {} },
            "serverInfo": { "name": SERVER_NAME, "version": SERVER_VERSION }
        });
        assert!(result["capabilities"]["tools"].is_object());
        assert_eq!(result["protocolVersion"], MCP_PROTOCOL_VERSION);
    }

    #[test]
    fn error_and_success_envelopes_are_wellformed() {
        let ok = success_response(json!(1), json!({"x":1}));
        assert_eq!(ok["jsonrpc"], "2.0");
        assert_eq!(ok["id"], 1);
        assert!(ok["result"].is_object());

        let err = error_response(json!("abc"), -32601, "nope");
        assert_eq!(err["error"]["code"], -32601);
        assert_eq!(err["id"], "abc");
    }

    #[test]
    fn tool_result_marks_errors() {
        let r = tool_text_result("boom", true);
        assert_eq!(r["isError"], true);
        assert_eq!(r["content"][0]["type"], "text");
        assert_eq!(r["content"][0]["text"], "boom");
    }

    #[test]
    fn structured_result_carries_object_and_text_mirror() {
        let payload = json!({ "results": [ { "id": "a", "title": "t", "url": "u" } ] });
        let r = tool_structured_result(payload.clone(), false);
        assert_eq!(r["isError"], false);
        // structuredContent holds the object (what ChatGPT validates/reads).
        assert_eq!(r["structuredContent"], payload);
        // content holds a JSON-encoded text mirror (text-only clients).
        let text = r["content"][0]["text"].as_str().unwrap();
        let reparsed: Value = serde_json::from_str(text).unwrap();
        assert_eq!(reparsed, payload);
    }

    #[test]
    fn search_and_fetch_advertise_output_schemas() {
        let list = tools_list();
        let tools = list["tools"].as_array().unwrap();
        let search = tools.iter().find(|t| t["name"] == "search").unwrap();
        let fetch = tools.iter().find(|t| t["name"] == "fetch").unwrap();
        assert_eq!(search["outputSchema"]["required"][0], "results");
        let fetch_req = fetch["outputSchema"]["required"].as_array().unwrap();
        for k in ["id", "title", "text", "url"] {
            assert!(
                fetch_req.iter().any(|v| v == k),
                "fetch outputSchema must require {k}"
            );
        }
    }
}