ai-memory 0.7.1

AI-agnostic persistent memory system — MCP server, HTTP API, and CLI for any AI platform
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
// Copyright 2026 AlphaOne LLC
// SPDX-License-Identifier: Apache-2.0

//! MCP `memory_recall` handler and namespace-chain helpers.

use crate::embeddings::Embed;
use crate::hnsw::VectorIndex;
use crate::mcp::param_names;
use crate::mcp::registry::McpTool;
use crate::models::{
    AttestLevel, CandidateCounts, ConfidenceTier, Memory, MemoryKind, RecallMeta, RecallTelemetry,
};
use crate::observations;
use crate::reranker::BatchedReranker;
use crate::{db, validate};
use serde_json::{Value, json};

// --- D1.3 (#984): per-tool McpTool impl for `memory_recall` ---

// #967 — `RecallRequest` and `KindsFilter` were promoted to canonical
// DTOs under `crate::models::recall_request`. They're re-exported here
// so the d1_3_984 parity test (which references the local `RecallRequest`
// symbol via `schemars::schema_for!`) keeps compiling unchanged, and so
// `RecallTool::input_schema()` continues to derive the schema from the
// same struct every surface marshals into. `KindsFilter` is part of the
// public re-export so legacy `mcp::tools::recall::KindsFilter` callers
// keep resolving even though only `RecallRequest` is touched in this
// module.
#[allow(unused_imports)]
pub use crate::models::recall_request::{KindsFilter, RecallRequest};

/// v0.7.0 #972 D1.3 (#984) — `McpTool` impl for `memory_recall`.
#[allow(dead_code)]
pub struct RecallTool;

impl McpTool for RecallTool {
    fn name() -> &'static str {
        crate::mcp::registry::tool_names::MEMORY_RECALL
    }
    fn description() -> &'static str {
        "Recall memories relevant to a context (ranked)."
    }
    fn docs() -> &'static str {
        "Fuzzy OR recall ranked by relevance + priority + access + tier. Optional: budget_tokens (cl100k cap), context_tokens (query-embed bias), session_id (+0.05 recency boost per #518), session_default (splice [agents.defaults.recall_scope]), include_archived, kinds filter. Default format toon_compact (~79% smaller)."
    }
    fn input_schema() -> Value {
        crate::mcp::registry::input_schema_for::<RecallRequest>()
    }
    fn family() -> &'static str {
        crate::profile::Family::Core.name()
    }
}

#[cfg(test)]
mod d1_3_984_tests {
    //! D1.3 (#984) — schema parity for the `memory_recall` tool.
    //! Reuses the allowed-diffs catalog documented in d1_2_983_tests.
    use super::*;

    fn legacy_props(tool_name: &str) -> serde_json::Map<String, Value> {
        let defs = crate::mcp::registry::tool_definitions();
        let tools = defs
            .get("tools")
            .and_then(Value::as_array)
            .expect("tool_definitions emits `tools` array");
        let entry = tools
            .iter()
            .find(|t| t.get("name").and_then(Value::as_str) == Some(tool_name))
            .unwrap_or_else(|| panic!("{tool_name} must be in legacy catalog"));
        entry
            .pointer("/inputSchema/properties")
            .and_then(Value::as_object)
            .unwrap_or_else(|| panic!("{tool_name}.inputSchema.properties must be object"))
            .clone()
    }

    fn derived_props_for<T: schemars::JsonSchema>() -> serde_json::Map<String, Value> {
        let schema = schemars::schema_for!(T);
        let v = serde_json::to_value(schema).expect("schema → value");
        v.get("properties")
            .and_then(Value::as_object)
            .or_else(|| {
                v.pointer(&format!(
                    "/definitions/{}/properties",
                    std::any::type_name::<T>().rsplit("::").next().unwrap_or("")
                ))
                .and_then(Value::as_object)
            })
            .cloned()
            .expect("schemars schema must have properties at a known path")
    }

    fn assert_property_set_parity(tool_name: &str, derived: &serde_json::Map<String, Value>) {
        let legacy = legacy_props(tool_name);
        let legacy_keys: std::collections::BTreeSet<&str> =
            legacy.keys().map(String::as_str).collect();
        let derived_keys: std::collections::BTreeSet<&str> =
            derived.keys().map(String::as_str).collect();
        assert_eq!(
            legacy_keys,
            derived_keys,
            "{tool_name}: property set drift; diff = {:?}",
            legacy_keys
                .symmetric_difference(&derived_keys)
                .collect::<Vec<_>>()
        );
    }

    fn assert_descriptions_match(tool_name: &str, derived: &serde_json::Map<String, Value>) {
        let legacy = legacy_props(tool_name);
        for (name, legacy_prop) in &legacy {
            if let Some(want) = legacy_prop.get("description").and_then(Value::as_str) {
                let got = derived
                    .get(name)
                    .and_then(|p| p.get("description"))
                    .and_then(Value::as_str);
                assert_eq!(
                    got,
                    Some(want),
                    "{tool_name}.{name}: description must match legacy byte-for-byte"
                );
            }
        }
    }

    #[test]
    fn recall_parity_984() {
        let derived = derived_props_for::<RecallRequest>();
        assert_property_set_parity("memory_recall", &derived);
        assert_descriptions_match("memory_recall", &derived);
    }

    #[test]
    fn recall_tool_metadata_984() {
        assert_eq!(RecallTool::name(), "memory_recall");
        assert_eq!(RecallTool::family(), "core");
    }
}

// #967 — `parse_kinds_filter(params: &Value)` is removed. The DTO's
// `kinds: Option<KindsFilter>` field carries the typed wire shape, and
// `KindsFilter::parse()` does the OR-of-kinds + COR-4 (#767) honoured
// resolution. See `src/models/recall_request.rs` for the canonical
// implementation + tests (`kinds_filter_typo_array_returns_empty_some_cor4`).

/// v0.7.x Form 6 — apply the parsed kinds filter to a recall result
/// set in-place. No-op when `kinds == None`. OR-of-kinds semantics:
/// a memory passes when `kinds.contains(&memory.memory_kind)`.
///
/// Cluster E audit COR-4 (issue #767): `Some(vec![])` (empty allow-
/// list, intentionally declared filter that matched zero known kinds)
/// returns zero rows rather than collapsing into "no filter".
fn apply_kinds_filter(
    results: Vec<(Memory, f64)>,
    kinds: Option<&[MemoryKind]>,
) -> Vec<(Memory, f64)> {
    match kinds {
        None => results,
        Some(allowed) => results
            .into_iter()
            .filter(|(m, _)| allowed.contains(&m.memory_kind))
            .collect(),
    }
}

/// Build the standards-inheritance chain for a namespace, most-general
/// first. Task 1.6 extends this from the historical 3-level scheme
/// (global → parent → namespace) to N levels by walking the `/`-derived
/// ancestors from [`crate::models::namespace_ancestors`] plus any
/// `namespace_meta` explicit-parent chain rooted at the top of the
/// hierarchical path (which keeps legacy flat-namespace setups working).
///
/// Returned vector is top-down: `[*, org, unit, team, agent]` for a
/// 4-level hierarchical namespace. Cycle-safe and bounded.
/// Display-side wrapper around [`db::build_namespace_chain`].
///
/// v0.6.3.1 (P4, audit G1): the chain walker moved into `db.rs` so the
/// governance enforcement gate could share a single canonical
/// implementation with the recall/standard injection paths. This thin
/// shim keeps existing call sites compiling without re-routing every
/// invocation through `db::`.

pub async fn handle_recall_with_pre_recall_hook(
    conn: &rusqlite::Connection,
    params: &Value,
    embedder: Option<&dyn Embed>,
    vector_index: Option<&VectorIndex>,
    reranker: Option<&BatchedReranker>,
    archive_on_gc: bool,
    resolved_ttl: &crate::config::ResolvedTtl,
    resolved_scoring: &crate::config::ResolvedScoring,
    chain: &crate::hooks::HookChain,
    registry: &mut crate::hooks::ExecutorRegistry,
    // v0.7.0 (issue #518) — recall scope defaults; forwarded
    // unchanged to `handle_recall_caller`.
    recall_scope: Option<&crate::config::RecallScope>,
    // v0.7.0 #1468 — caller-scoped `scope=private` post-filter caller.
    // Threaded through to `handle_recall_caller` so the pre-recall-hook
    // entry point applies the SAME ownership gate as the plain dispatch
    // path. `None` keeps the single-tenant trust-all read posture. This
    // is wired now (rather than left as `None`) so wiring this surface
    // into MCP dispatch later cannot silently bypass #1468.
    caller: Option<&str>,
) -> Result<Value, String> {
    // Resolve the (query, namespace, k) triple once so the hook
    // sees exactly what the recall would see.
    let context = params["context"]
        .as_str()
        .ok_or(crate::errors::msg::CONTEXT_REQUIRED)?;
    let namespace = params["namespace"].as_str().unwrap_or("");
    let k = u32::try_from(params["limit"].as_u64().unwrap_or(10)).unwrap_or(u32::MAX);

    // Fire the hot-path chain. The chain runner enforces the 50ms
    // class deadline (G6); a hook that exceeds it converts to
    // fail-open Allow per the configured `FailMode`.
    let outcome =
        crate::hooks::apply_pre_recall_expand(context, namespace, k, chain, registry).await;

    if let crate::hooks::PreRecallOutcome::Denied { reason, code } = &outcome {
        // The recall is suppressed. Return the same envelope shape
        // a normal empty recall would produce, decorated with a
        // `meta.diagnostic.pre_recall_denied` block so the caller
        // can distinguish "no matches" from "blocked by hook".
        let mut resp = json!({
            "memories": [],
            "count": 0,
            "mode": "denied_by_hook",
        });
        let meta = resp
            .as_object_mut()
            .expect("recall response is always a JSON object")
            .entry("meta".to_string())
            .or_insert_with(|| json!({}));
        meta["diagnostic"] = json!({
            "pre_recall_denied": {
                "reason": reason,
                "code": code,
            }
        });
        return Ok(resp);
    }

    // Apply any Modify-side rewrites onto the params bag before
    // calling the sync recall path. We clone the input so the
    // caller's Value is left untouched.
    let mut effective = params.clone();
    if let crate::hooks::PreRecallOutcome::Modified {
        query: q,
        namespace: ns,
        k: nk,
    } = outcome
    {
        if let Some(obj) = effective.as_object_mut() {
            obj.insert("context".to_string(), json!(q));
            // Only inject `namespace` if the hook actually rewrote
            // it (vs leaving the original empty-string default).
            if !ns.is_empty() {
                obj.insert("namespace".to_string(), json!(ns));
            }
            obj.insert("limit".to_string(), json!(u64::from(nk)));
        }
    }

    handle_recall_caller(
        conn,
        &effective,
        embedder,
        vector_index,
        reranker,
        archive_on_gc,
        resolved_ttl,
        resolved_scoring,
        recall_scope,
        caller,
    )
}

/// v0.7.0 Gap 7 (#890) — Tier-3 recall-row decoration.
///
/// Serialise a `(Memory, score)` pair into the JSON shape the recall
/// response surfaces. When `verbose_provenance` is true (the default
/// since v0.7.0), the row carries the full provenance audit trail —
/// derived `confidence_tier`, derived `freshness_state`, and the
/// `latest_link_attest_level` lookup over `memory_links`. Plain serde
/// already round-trips the substrate-side columns (`confidence`,
/// `source`, `source_uri`, `access_count`, `last_accessed_at`), so
/// the additional decoration is the *derived* half.
///
/// Token-budget contract: the verbose decoration adds at most ~120
/// bytes per row (3 short snake_case keys + 3 short snake_case
/// values). The token-budget guards (`tests/token_budget_guard.rs`)
/// pin the catalog totals; the per-row decoration grows with `count`
/// not catalog size, so the guards remain accurate.
///
/// v0.7.x (#1155) — exposed as `pub(crate)` so the HTTP recall handler
/// at `src/handlers/recall.rs` can apply the same verbose-decoration
/// shape under operator opt-in via the `Accept-Provenance: verbose`
/// HTTP header. MCP wire default is `verbose_provenance=true`
/// (set inline at `handle_recall_dto`); HTTP default is
/// `verbose_provenance=false` for v0.6.x wire-shape backwards compat
/// (consumers opt in via header).
pub(crate) fn decorate_memory(
    mem: &Memory,
    score: f64,
    verbose_provenance: bool,
    conn: &rusqlite::Connection,
) -> Value {
    let mut val = serde_json::to_value(mem).unwrap_or_default();
    let Some(obj) = val.as_object_mut() else {
        return val;
    };
    obj.insert(
        "score".to_string(),
        json!(
            (score * crate::SCORE_DISPLAY_ROUND_FACTOR).round() / crate::SCORE_DISPLAY_ROUND_FACTOR
        ),
    );
    if !verbose_provenance {
        return val;
    }
    // Gap 7 (#890) — derived confidence tier (Gap 4 enum).
    obj.insert(
        "confidence_tier".to_string(),
        json!(mem.confidence_tier().as_str()),
    );
    // Gap 7 — derived freshness_state from expires_at + last_accessed_at.
    obj.insert("freshness_state".to_string(), json!(freshness_state(mem)));
    // Gap 7 — latest link attest level. Best-effort: a SQL error here
    // collapses to None so a corrupt links row doesn't break the
    // recall response.
    let latest_attest = latest_link_attest_level(conn, &mem.id);
    if let Some(level) = latest_attest {
        obj.insert("latest_link_attest_level".to_string(), json!(level));
    }
    val
}

/// v0.7.0 Gap 7 (#890) — derive a coarse freshness state from
/// substrate-side timestamps.
///
/// - `"expired"` — `expires_at` is set and lies in the past.
/// - `"stale"`   — no access recorded in the last 30 days (long-tier
///                 rows that haven't been touched for a month).
/// - `"warm"`    — has been accessed in the last 30 days.
/// - `"fresh"`   — newly created OR `last_accessed_at == created_at`
///                 (never touched, but young).
///
/// Conservative defaults: a row with unparseable timestamps lands in
/// `"warm"` (the substrate sees activity recently enough to surface
/// it via recall, so blocking it on a timestamp parse would be
/// hostile). Pure function; no DB queries.
pub(crate) fn freshness_state(mem: &Memory) -> &'static str {
    let now = chrono::Utc::now();
    if let Some(exp) = mem.expires_at.as_deref()
        && let Ok(dt) = chrono::DateTime::parse_from_rfc3339(exp)
        && dt < now
    {
        return "expired";
    }
    let last = mem.last_accessed_at.as_deref().unwrap_or(&mem.created_at);
    let Ok(last_dt) = chrono::DateTime::parse_from_rfc3339(last) else {
        return "warm";
    };
    let age_days = (now - last_dt.with_timezone(&chrono::Utc)).num_days();
    if age_days > 30 {
        "stale"
    } else if age_days < 1 && mem.access_count == 0 {
        "fresh"
    } else {
        "warm"
    }
}

/// v0.7.0 Gap 7 (#890) — return the strongest attestation level
/// across every link incident on `memory_id`. `peer_attested >
/// self_signed > unsigned`. Returns `None` when no links exist.
/// Best-effort: a SQL error returns `None` so the recall row keeps
/// its remaining decoration.
pub(crate) fn latest_link_attest_level(
    conn: &rusqlite::Connection,
    memory_id: &str,
) -> Option<String> {
    let links = db::get_links(conn, memory_id).ok()?;
    let mut best: Option<AttestLevel> = None;
    for link in &links {
        let Some(level_str) = link.attest_level.as_deref() else {
            continue;
        };
        let Some(level) = AttestLevel::from_str(level_str) else {
            continue;
        };
        let candidate_rank = attest_rank(level);
        match best {
            None => best = Some(level),
            Some(curr) if candidate_rank > attest_rank(curr) => best = Some(level),
            _ => {}
        }
    }
    best.map(|l| l.as_str().to_string())
}

const fn attest_rank(level: AttestLevel) -> u8 {
    // v0.7.0 #1430 fix: new SignedByPeer (L4 capture_turn) + DaemonSigned
    // (governance audit) variants ranked alongside the original 3.
    // Ranking semantics:
    //   - Unsigned     (0) — no signature, lowest trust
    //   - SelfSigned   (1) — writer-local signature
    //   - DaemonSigned (1) — substrate-self signature on its own
    //                        audit emission (semantically equivalent
    //                        rank to SelfSigned — daemon writing about
    //                        its own actions)
    //   - SignedByPeer (2) — host-supplied signature, allowlist-verified
    //                        (equivalent rank to PeerAttested: both
    //                        require an external pubkey enrollment +
    //                        signature verification)
    //   - PeerAttested (2) — federation H3 inbound, allowlist-verified
    match level {
        AttestLevel::Unsigned => 0,
        AttestLevel::SelfSigned | AttestLevel::DaemonSigned => 1,
        AttestLevel::PeerAttested | AttestLevel::SignedByPeer => 2,
    }
}

/// FX-4 / PERF-2 (2026-05-26) — batched lookup of the strongest
/// attestation level across every link incident on each `memory_id`
/// in `ids`. Replaces the per-row [`latest_link_attest_level`] call
/// that the HTTP recall handler used to issue under the DB mutex
/// (one round-trip per row × N rows = N round-trips under the lock).
/// One `IN(...)` SQL emit covers the batch; the map is keyed by
/// `memory_id` and only entries with a non-`None` level land in it.
/// Best-effort: a SQL error returns an empty map so the recall
/// response keeps its remaining decoration.
pub(crate) fn latest_link_attest_level_many(
    conn: &rusqlite::Connection,
    ids: &[&str],
) -> std::collections::HashMap<String, String> {
    let mut out: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    if ids.is_empty() {
        return out;
    }
    // Chunk to keep the SQL parameter count well below sqlite's
    // default `SQLITE_LIMIT_VARIABLE_NUMBER` (999 on the standard
    // build); each row contributes 2 placeholders (source_id +
    // target_id) so 250 ids per chunk = 500 params, comfortable
    // headroom. The recall handler caps `limit` at 50 today so the
    // typical batch is one chunk; the cap is defensive only.
    const CHUNK: usize = 250;
    // Track best attestation per id across both the `source_id` and
    // `target_id` columns. A link with `target_id = id` still
    // contributes to `id`'s attestation rank because `get_links`
    // surfaces incident edges in either direction.
    let mut best_by_id: std::collections::HashMap<String, AttestLevel> =
        std::collections::HashMap::new();
    for chunk in ids.chunks(CHUNK) {
        let placeholders = std::iter::repeat("?")
            .take(chunk.len())
            .collect::<Vec<_>>()
            .join(",");
        let sql = format!(
            "SELECT source_id, target_id, attest_level \
             FROM memory_links \
             WHERE source_id IN ({placeholders}) OR target_id IN ({placeholders})"
        );
        // Bind ids twice — once for the `source_id IN (...)` clause
        // and once for the `target_id IN (...)` clause. Allocation
        // is a single `Vec<&str>` of length 2 × chunk.len().
        let mut params: Vec<&str> = Vec::with_capacity(chunk.len() * 2);
        params.extend_from_slice(chunk);
        params.extend_from_slice(chunk);
        let Ok(mut stmt) = conn.prepare(&sql) else {
            // Prepare error — return what we have. The decorator
            // already treats `None` as a degraded-best-effort signal.
            return out;
        };
        let Ok(rows) = stmt.query_map(rusqlite::params_from_iter(params.iter()), |row| {
            let source_id: String = row.get(0)?;
            let target_id: String = row.get(1)?;
            let level: Option<String> = row.get(2)?;
            Ok((source_id, target_id, level))
        }) else {
            return out;
        };
        // `chunk` is &[&str] — convert to a HashSet<&str> for O(1)
        // membership tests across both columns.
        let in_batch: std::collections::HashSet<&str> = chunk.iter().copied().collect();
        for r in rows {
            let Ok((source_id, target_id, level_opt)) = r else {
                continue;
            };
            let Some(level_str) = level_opt else { continue };
            let Some(level) = AttestLevel::from_str(&level_str) else {
                continue;
            };
            let rank = attest_rank(level);
            // Apply to whichever endpoint(s) of the link are in our
            // batch — both directions count as "incident" per the
            // per-row implementation above.
            for endpoint in [&source_id, &target_id] {
                if !in_batch.contains(endpoint.as_str()) {
                    continue;
                }
                match best_by_id.get(endpoint) {
                    None => {
                        best_by_id.insert(endpoint.clone(), level);
                    }
                    Some(curr) if rank > attest_rank(*curr) => {
                        best_by_id.insert(endpoint.clone(), level);
                    }
                    _ => {}
                }
            }
        }
    }
    for (id, level) in best_by_id {
        out.insert(id, level.as_str().to_string());
    }
    out
}

/// FX-4 / PERF-2 (2026-05-26) — batched front-end for
/// [`decorate_memory`] used by the HTTP recall handler. Resolves
/// the verbose-decoration link-attestation lookup for every memory
/// in one SQL round-trip via [`latest_link_attest_level_many`]
/// instead of N round-trips. Returns one `Value` per `(mem, score)`
/// in input order so the caller can splice it straight into the
/// response payload.
///
/// Per-row pure fields (`confidence_tier`, `freshness_state`, the
/// serialised `Memory` body, and the rounded `score`) match the
/// shape that [`decorate_memory`] produces — the only structural
/// difference is that the attestation lookup is amortised across
/// the batch. The verbose-OFF path is identical to the legacy
/// per-row shape (no DB queries) and is short-circuited here.
pub fn decorate_memory_many(
    rows: &[(Memory, f64)],
    verbose_provenance: bool,
    conn: &rusqlite::Connection,
) -> Vec<Value> {
    if !verbose_provenance {
        return rows
            .iter()
            .map(|(mem, score)| {
                let mut val = serde_json::to_value(mem).unwrap_or_default();
                if let Some(obj) = val.as_object_mut() {
                    obj.insert(
                        "score".to_string(),
                        json!(
                            (score * crate::SCORE_DISPLAY_ROUND_FACTOR).round()
                                / crate::SCORE_DISPLAY_ROUND_FACTOR
                        ),
                    );
                }
                val
            })
            .collect();
    }
    let ids: Vec<&str> = rows.iter().map(|(m, _)| m.id.as_str()).collect();
    let attest_map = latest_link_attest_level_many(conn, &ids);
    rows.iter()
        .map(|(mem, score)| {
            let mut val = serde_json::to_value(mem).unwrap_or_default();
            let Some(obj) = val.as_object_mut() else {
                return val;
            };
            obj.insert(
                "score".to_string(),
                json!(
                    (score * crate::SCORE_DISPLAY_ROUND_FACTOR).round()
                        / crate::SCORE_DISPLAY_ROUND_FACTOR
                ),
            );
            obj.insert(
                "confidence_tier".to_string(),
                json!(mem.confidence_tier().as_str()),
            );
            obj.insert("freshness_state".to_string(), json!(freshness_state(mem)));
            if let Some(level) = attest_map.get(&mem.id) {
                obj.insert("latest_link_attest_level".to_string(), json!(level));
            }
            val
        })
        .collect()
}

/// v0.7.0 Gap 3 (#886) — record one `recall_observations` row per
/// returned candidate under `recall_id`. The `retriever` label is
/// stamped uniformly across the batch ("hybrid+rerank", "hybrid",
/// "keyword") to match the corresponding response `mode`. Best-
/// effort: a SQL error logs at warn level and continues, since the
/// recall response is already minted by the time this runs.
fn record_recall_observations(
    conn: &rusqlite::Connection,
    recall_id: &str,
    memories_json: &[Value],
    retriever: &str,
) {
    if !observations::table_exists(conn) {
        return;
    }
    let mut candidates: Vec<observations::Candidate<'_>> = Vec::with_capacity(memories_json.len());
    let mut id_holders: Vec<&str> = Vec::with_capacity(memories_json.len());
    for (idx, m) in memories_json.iter().enumerate() {
        if let Some(id) = m.get(param_names::ID).and_then(Value::as_str) {
            id_holders.push(id);
            let score = m.get("score").and_then(Value::as_f64).unwrap_or(0.0);
            #[allow(clippy::cast_possible_wrap)]
            let rank = (idx + 1) as i64;
            candidates.push(observations::Candidate {
                // QUAL-4 (med/low review batch) — load-bearing `.expect()`
                // with a reason string. The push at line 572 above is the
                // immediate predecessor; `id_holders.last()` cannot be
                // `None` here. The annotation pins the local invariant so
                // a future refactor that breaks the push-then-read pairing
                // surfaces a named panic instead of a bare unwrap.
                memory_id: id_holders
                    .last()
                    .copied()
                    .expect("just pushed id_holders above"),
                retriever,
                rank,
                score,
            });
        }
    }
    if let Err(e) = observations::record_recall(conn, recall_id, &candidates) {
        tracing::warn!(
            target: "observations",
            recall_id = %recall_id,
            "record_recall failed (non-fatal): {e}"
        );
    }
}

/// #967 — JSON-bag entry kept as a thin wrapper around
/// [`handle_recall_dto`]. The pre-#967 surface continues to accept the
/// `&Value` params bag so existing call sites (tests + the MCP
/// dispatcher) compile unchanged; field extraction is delegated to
/// [`RecallRequest::from_mcp_params`].
#[allow(clippy::too_many_arguments)]
pub fn handle_recall(
    conn: &rusqlite::Connection,
    params: &Value,
    embedder: Option<&dyn Embed>,
    vector_index: Option<&VectorIndex>,
    reranker: Option<&BatchedReranker>,
    archive_on_gc: bool,
    resolved_ttl: &crate::config::ResolvedTtl,
    resolved_scoring: &crate::config::ResolvedScoring,
    recall_scope: Option<&crate::config::RecallScope>,
) -> Result<Value, String> {
    handle_recall_caller(
        conn,
        params,
        embedder,
        vector_index,
        reranker,
        archive_on_gc,
        resolved_ttl,
        resolved_scoring,
        recall_scope,
        None,
    )
}

/// v0.7.0 #1468 — caller-scoped MCP recall entry. Identical to
/// [`handle_recall`] but threads a visibility `caller` (resolved by the
/// dispatch layer via
/// [`crate::identity::resolve_read_visibility_caller`]) into
/// [`handle_recall_dto`], which post-filters every retrieval branch by
/// the canonical [`crate::visibility::is_visible_to_caller`] predicate.
/// `None` preserves the single-tenant trust-all read posture.
#[allow(clippy::too_many_arguments)]
pub fn handle_recall_caller(
    conn: &rusqlite::Connection,
    params: &Value,
    embedder: Option<&dyn Embed>,
    vector_index: Option<&VectorIndex>,
    reranker: Option<&BatchedReranker>,
    archive_on_gc: bool,
    resolved_ttl: &crate::config::ResolvedTtl,
    resolved_scoring: &crate::config::ResolvedScoring,
    recall_scope: Option<&crate::config::RecallScope>,
    caller: Option<&str>,
) -> Result<Value, String> {
    let req = RecallRequest::from_mcp_params(params)?;
    handle_recall_dto(
        conn,
        &req,
        embedder,
        vector_index,
        reranker,
        archive_on_gc,
        resolved_ttl,
        resolved_scoring,
        recall_scope,
        caller,
    )
}

/// #967 canonical-DTO entry. The `&RecallRequest` carries every
/// caller-supplied scalar (18 fields pre-#967 extracted one-by-one
/// from the `params: &Value` bag). The remaining args are the
/// substrate-side context that doesn't belong on the wire DTO:
/// connection handle, embedder, vector index, reranker, gc-archive
/// flag, resolved TTL / scoring configs, and the operator's
/// `[agents.defaults.recall_scope]` defaults.
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
pub fn handle_recall_dto(
    conn: &rusqlite::Connection,
    req: &RecallRequest,
    embedder: Option<&dyn Embed>,
    vector_index: Option<&VectorIndex>,
    reranker: Option<&BatchedReranker>,
    archive_on_gc: bool,
    resolved_ttl: &crate::config::ResolvedTtl,
    resolved_scoring: &crate::config::ResolvedScoring,
    // v0.7.0 (issue #518) — operator-configured recall defaults.
    // When `session_default=true` is set on the request AND a given
    // filter axis is absent, the corresponding `recall_scope` field
    // is spliced into the request before the storage call. `None`
    // keeps v0.6.x recall semantics exactly.
    recall_scope: Option<&crate::config::RecallScope>,
    // v0.7.0 #1468 — read-path visibility caller. The `db::recall*`
    // family applies the #151 namespace-scope (`as_agent`) gate but NOT
    // the per-row `scope=private` ownership predicate, so a cross-agent
    // private row could otherwise reach the MCP wire. When `Some`, every
    // retrieval branch drops rows the caller does not own via
    // `crate::visibility::is_visible_to_caller`. `None` (single-tenant /
    // no stable env identity) keeps the trust-all read posture.
    caller: Option<&str>,
) -> Result<Value, String> {
    // v0.7.0 Gap 7 (#890) — `verbose_provenance` defaults to true.
    // Pre-Gap-7 recall responses dropped per-row provenance scaffolding
    // (confidence_tier / source_uri / freshness_state / access_count /
    // latest_link_attest_level) to keep the wire small; v0.7.0
    // reverses the default so MCP callers see the full audit trail
    // by default. Clients that want the trimmed shape can pass
    // `verbose_provenance=false`.
    let verbose_provenance = req.verbose_provenance.unwrap_or(true);

    // v0.7.0 Gap 3 (#886) — fresh per-call recall_id stamped into
    // every observation row (and echoed back in the response so the
    // caller can cite it on a later memory_store / memory_link).
    let recall_id = uuid::Uuid::new_v4().to_string();

    // v0.7.0 Gap 4 (#887) — derived-tier filter (`"confirmed"` /
    // `"likely"` / `"ambiguous"`). When set, keeps only the matching
    // tier. Unknown / empty values fall through to "no filter" so a
    // typo on the client side doesn't silently inverter the filter.
    let confidence_tier_filter: Option<ConfidenceTier> = req
        .confidence_tier
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .and_then(ConfidenceTier::parse);

    // Helper: serialize scored memories with score field (#95) and,
    // when `verbose_provenance` is set, the Gap 7 (#890) decoration
    // block (`confidence_tier`, `freshness_state`, `latest_link_attest_level`).
    // Plain serde already emits `confidence`, `source`, `source_uri`,
    // `access_count`, `last_accessed_at`; the Gap 7 contract just adds
    // the derived fields the substrate computes here.
    let scored_memories =
        |results: Vec<(Memory, f64)>, conn: &rusqlite::Connection| -> Vec<Value> {
            results
                .into_iter()
                .map(|(mem, score)| decorate_memory(&mem, score, verbose_provenance, conn))
                .collect()
        };

    // v0.7.0 Gap 4 (#887) — filter `(Memory, f64)` candidates by the
    // derived confidence tier. No-op when `confidence_tier_filter` is
    // None.
    let apply_confidence_tier_filter = |results: Vec<(Memory, f64)>| -> Vec<(Memory, f64)> {
        match confidence_tier_filter {
            None => results,
            Some(target) => results
                .into_iter()
                .filter(|(m, _)| m.confidence_tier() == target)
                .collect(),
        }
    };

    // v0.7.0 #1468 — per-row ownership visibility filter. Applied at every
    // retrieval branch immediately before serialization so a cross-agent
    // `scope=private` row never reaches the wire. No-op when `caller` is
    // `None` (single-tenant trust-all read posture).
    let apply_visibility_filter = |results: Vec<(Memory, f64)>| -> Vec<(Memory, f64)> {
        match caller {
            None => results,
            Some(c) => results
                .into_iter()
                .filter(|(m, _)| crate::visibility::is_visible_to_caller(m, c))
                .collect(),
        }
    };

    let _ = db::gc_if_needed(conn, archive_on_gc);
    let context = req.context.as_str();
    if context.is_empty() {
        return Err(crate::errors::msg::CONTEXT_REQUIRED.to_string());
    }
    // v0.7.0 (issue #518) — when the caller passed
    // `session_default=true` AND a given filter axis is absent,
    // splice in the corresponding `[agents.defaults.recall_scope]`
    // value. Explicit args always win. Sqlite recall does not
    // expose a `tier` filter on the legacy `db::recall` /
    // `db::recall_hybrid` paths, so the `tier` axis is plumbed but
    // not consumed on this branch (the postgres SAL handler in
    // `handlers/recall.rs::recall_response` applies it via
    // `Filter.tier`).
    let session_default = req.session_default.unwrap_or(false);
    let scope = if session_default { recall_scope } else { None };
    // Compute owned defaults so they outlive the parse step.
    let scope_namespace: Option<String> = scope
        .and_then(|s| s.namespaces.as_ref())
        .and_then(|v| v.first())
        .cloned();
    let scope_since: Option<String> = scope.and_then(|s| {
        s.since.as_deref().and_then(|d| {
            crate::config::parse_duration_string(d).map(|dur| {
                let cutoff = chrono::Utc::now() - dur;
                cutoff.to_rfc3339()
            })
        })
    });
    let explicit_namespace = req.namespace.as_deref();
    let explicit_since = req.since.as_deref();
    let namespace: Option<&str> = explicit_namespace.or(scope_namespace.as_deref());
    let limit = if let Some(v) = req.limit
        && v > 0
    {
        usize::try_from(v).unwrap_or(usize::MAX)
    } else if let Some(v) = scope.and_then(|s| s.limit) {
        usize::try_from(v).unwrap_or(usize::MAX)
    } else {
        10
    };
    let tags = req.tags.as_deref();
    let since: Option<&str> = explicit_since.or(scope_since.as_deref());
    let until = req.until.as_deref();
    // #151 visibility
    let as_agent = req.as_agent.as_deref();
    if let Some(a) = as_agent {
        validate::validate_namespace(a).map_err(|e| e.to_string())?;
    }
    // Task 1.11 / Phase P6 (R1): optional token budget. R1 semantics
    // permit `0` ("give me nothing") and return an empty result with
    // `meta.budget_overflow = false` — see the comment on
    // `db::apply_token_budget`. This supersedes the v0.6.3 Ultrareview
    // #348 hard-reject of 0; the meta block now disambiguates "user
    // asked for zero" from "buggy uninitialized counter" by always
    // round-tripping the requested budget.
    let budget_tokens = req.resolved_budget_tokens();

    // v0.7.x Form 6 — Batman-taxonomy `kinds` filter. Parsed once
    // and applied to every result vector below (keyword, hybrid,
    // hybrid+rerank). OR-of-kinds within the param, AND with the
    // other filters (namespace, tags, time window, visibility).
    let kinds_filter = req.kinds.as_ref().and_then(KindsFilter::parse);

    // v0.7.0 WT-1-E — atom-preference recall semantics.
    //
    // By default recall surfaces atoms in place of archived sources
    // (the WT-1-B atomiser sets `atomised_into > 0` AND
    // `metadata.atomisation_archived_at` on the parent row when atoms
    // exist). Auditors and the forensic-export path opt in via
    // `include_archived=true` to see both atoms AND the archived
    // source for the same query — the substrate read is the same;
    // only the WHERE clause changes.
    //
    // Composes with namespace, memory_kind (via storage filter),
    // time-window, tier, and the existing visibility predicate.
    let include_archived = req.include_archived.unwrap_or(false);

    // v0.7.0 Form 4 (issue #757) — fact-provenance post-filters.
    // `has_citations` keeps only memories with a non-empty citations
    // array; `source_uri_prefix` keeps only memories whose
    // `source_uri` column begins with the supplied string. Both
    // compose with the existing SQL-side filters; we run them in
    // Rust after the recall returns so the substrate signature
    // doesn't grow another two positional args. Tool-count baseline
    // preserved (no new MCP tool).
    let has_citations_filter = req.has_citations.unwrap_or(false);
    let source_uri_prefix: Option<String> = req.source_uri_prefix.clone();

    // v0.7.0 (issue #518) — per-session "recently accessed" boost.
    // When the caller passes a non-empty `session_id`, the rerank
    // post-step adds `SESSION_RECENCY_BOOST` to every candidate
    // already in the session's ring buffer and records the post-
    // boost hit set back into the buffer so the next recall in the
    // same session reuses the new context. `None` / empty preserves
    // pre-#518 recall semantics exactly.
    let session_id: Option<String> = req
        .session_id
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(std::string::ToString::to_string);
    let session_tracker = crate::reranker::global_session_recall_tracker();

    // v0.6.0.0 contextual recall — caller-supplied recent conversation tokens.
    let context_tokens: Vec<String> = req
        .context_tokens
        .as_ref()
        .map(|arr| arr.iter().filter(|s| !s.is_empty()).cloned().collect())
        .unwrap_or_default();

    // Helper: tack tokens_used / budget_tokens onto the response, plus
    // — when a budget was supplied — the Phase P6 RecallMeta-style
    // sub-block (`meta.budget_tokens_used`, `budget_tokens_remaining`,
    // `memories_dropped`, `budget_overflow`). The legacy top-level
    // `tokens_used` / `budget_tokens` fields are preserved verbatim so
    // pre-P6 callers continue to work byte-for-byte.
    //
    // NOTE on RecallMeta: Phase P3 introduces a top-level `meta` block
    // (recall_mode, reranker_used, candidate_counts, blend_weight). This
    // P6 worktree pre-dates the P3 merge, so we define the budget-mode
    // sub-block directly under `meta.budget` and let P3's rebase fold
    // its fields in alongside ours. See REMEDIATIONv0631.md L488-489.
    let decorate_budget = |resp: &mut Value, outcome: &db::BudgetOutcome| {
        resp["tokens_used"] = json!(outcome.tokens_used);
        if let Some(b) = budget_tokens {
            resp["budget_tokens"] = json!(b);
            // Phase P6 R1 meta block. Always emitted when a budget is
            // supplied so callers can rely on the field set. Kept under
            // a dedicated `meta` key so the top-level shape stays
            // backward-compatible — pre-P6 callers ignore unknown keys.
            let meta = resp
                .as_object_mut()
                .expect("recall response is always a JSON object")
                .entry("meta".to_string())
                .or_insert_with(|| json!({}));
            meta["budget_tokens_used"] = json!(outcome.tokens_used);
            meta["budget_tokens_remaining"] = json!(outcome.tokens_remaining.unwrap_or(0));
            meta["memories_dropped"] = json!(outcome.memories_dropped);
            meta["budget_overflow"] = json!(outcome.budget_overflow);
        }
    };

    // v0.6.3.1 (P3): build the per-request meta block from retrieval-stage
    // telemetry + the runtime reranker variant. The block is always
    // present in the response — clients that don't read it ignore unknown
    // fields per JSON-RPC convention. Closes audit gaps G2/G8/G11 by
    // making silent-degrade paths visible at request time.
    // v0.7.0 R3-S2 — distinguish *originally lexical* from
    // *degraded lexical* so the recall response surfaces an in-band
    // signal when the operator's configured neural cross-encoder
    // failed to load and fell back. Pre-R3 this was a tracing-event-
    // only signal; the G8 closure claim required a per-call field
    // and now has one. Wire shape:
    //   - "neural"          — configured + loaded
    //   - "lexical"         — operator chose lexical or never asked
    //                         for a neural cross-encoder
    //   - "degraded_lexical"— configured neural, runtime fell back
    //   - "none"            — no reranker plumbed at all
    let reranker_used = match reranker {
        Some(ce) if ce.is_neural() => "neural",
        Some(ce) if ce.is_degraded_lexical() => "degraded_lexical",
        Some(_) => "lexical",
        None => "none",
    };
    let attach_meta = |resp: &mut Value, recall_mode: &str, telemetry: &RecallTelemetry| {
        // Round blend_weight to 3 decimals — matches the score field
        // precision and keeps the wire shape stable regardless of f64
        // representation jitter.
        let blend_weight = (telemetry.blend_weight_avg * crate::SCORE_DISPLAY_ROUND_FACTOR).round()
            / crate::SCORE_DISPLAY_ROUND_FACTOR;
        let meta = RecallMeta {
            recall_mode: recall_mode.to_string(),
            reranker_used: reranker_used.to_string(),
            candidate_counts: CandidateCounts {
                fts: telemetry.fts_candidates,
                hnsw: telemetry.hnsw_candidates,
            },
            blend_weight,
        };
        // Merge into existing meta object rather than replacing — P6's
        // decorate_budget may have already populated budget_* keys here.
        if let Ok(Value::Object(p3_fields)) = serde_json::to_value(&meta) {
            let meta_obj = resp
                .as_object_mut()
                .expect("recall response is always a JSON object")
                .entry("meta".to_string())
                .or_insert_with(|| json!({}));
            if let Some(existing) = meta_obj.as_object_mut() {
                for (k, v) in p3_fields {
                    existing.insert(k, v);
                }
            }
        }
    };

    // Use hybrid recall if embedder is available
    if let Some(emb) = embedder {
        match emb.embed_query(context) {
            Ok(primary_emb) => {
                // v0.6.0.0: fuse primary query with context-token embedding
                // at 70/30 when caller supplied conversation tokens.
                let query_emb = if context_tokens.is_empty() {
                    primary_emb
                } else {
                    let joined = context_tokens.join(" ");
                    match emb.embed_query(&joined) {
                        Ok(ctx_emb) => crate::embeddings::Embedder::fuse(
                            &primary_emb,
                            &ctx_emb,
                            crate::RECALL_PRIMARY_CTX_BLEND,
                        ),
                        Err(e) => {
                            tracing::warn!("context_tokens embed failed, using primary only: {e}");
                            primary_emb
                        }
                    }
                };
                let (results, outcome, telemetry) = db::recall_hybrid_with_telemetry(
                    conn,
                    context,
                    &query_emb,
                    namespace,
                    limit.min(50),
                    tags,
                    since,
                    until,
                    vector_index,
                    resolved_ttl.short_extend_secs,
                    resolved_ttl.mid_extend_secs,
                    as_agent,
                    budget_tokens,
                    resolved_scoring,
                    include_archived,
                    // v0.7.0 Cluster-A PERF-3 — push source-URI prefix
                    // into SQL WHERE so the partial
                    // `idx_memories_source_uri` index covers the lookup.
                    // The post-filter call below is a no-op when the
                    // SQL push-down already constrained the set; we
                    // keep it for the `has_citations` axis only.
                    source_uri_prefix.as_deref(),
                )
                .map_err(|e| e.to_string())?;
                let results = crate::cli::recall::apply_form4_recall_filters(
                    results,
                    has_citations_filter,
                    source_uri_prefix.as_deref(),
                );

                // Apply cross-encoder reranking if available
                if let Some(ce) = reranker {
                    let ce_reranked = ce.rerank(context, results);
                    let ce_reranked = apply_kinds_filter(ce_reranked, kinds_filter.as_deref());
                    let ce_reranked = apply_confidence_tier_filter(ce_reranked);
                    let ce_reranked = apply_visibility_filter(ce_reranked);
                    // v0.7.0 (issue #518) — session recency boost.
                    let ce_reranked = crate::reranker::apply_session_recency_boost(
                        ce_reranked,
                        session_id.as_deref(),
                        session_tracker,
                    );
                    let memories = scored_memories(ce_reranked, conn);
                    record_recall_observations(
                        conn,
                        &recall_id,
                        &memories,
                        crate::models::RECALL_MODE_HYBRID_RERANK,
                    );
                    let mut resp = json!({
                        "recall_id": recall_id,
                        "memories": memories,
                        "count": memories.len(),
                        "mode": crate::models::RECALL_MODE_HYBRID_RERANK,
                    });
                    decorate_budget(&mut resp, &outcome);
                    attach_meta(&mut resp, "hybrid", &telemetry);
                    super::inject_namespace_standard(conn, namespace, &mut resp);
                    return Ok(resp);
                }

                let results = apply_kinds_filter(results, kinds_filter.as_deref());
                let results = apply_confidence_tier_filter(results);
                let results = apply_visibility_filter(results);
                // v0.7.0 (issue #518) — session recency boost (no
                // cross-encoder branch).
                let results = crate::reranker::apply_session_recency_boost(
                    results,
                    session_id.as_deref(),
                    session_tracker,
                );
                let memories = scored_memories(results, conn);
                record_recall_observations(conn, &recall_id, &memories, "hybrid");
                let mut resp = json!({
                    "recall_id": recall_id,
                    "memories": memories,
                    "count": memories.len(),
                    "mode": "hybrid",
                });
                decorate_budget(&mut resp, &outcome);
                attach_meta(&mut resp, "hybrid", &telemetry);
                super::inject_namespace_standard(conn, namespace, &mut resp);
                return Ok(resp);
            }
            Err(e) => {
                // v0.6.3.1 (P3, G11): the embedder being present but the
                // per-query embed failing is a different silent-degrade
                // path than "embedder unavailable at startup" — preserve
                // the existing tracing event and fall through to
                // keyword_only mode below, which is what the meta block
                // will report.
                tracing::warn!("embedding failed, falling back to FTS: {}", e);
            }
        }
    }

    // Fallback to keyword-only recall
    let (results, outcome, telemetry) = db::recall_with_telemetry(
        conn,
        context,
        namespace,
        limit.min(50),
        tags,
        since,
        until,
        resolved_ttl.short_extend_secs,
        resolved_ttl.mid_extend_secs,
        as_agent,
        budget_tokens,
        include_archived,
        // v0.7.0 Cluster-A PERF-3 — see hybrid branch above.
        source_uri_prefix.as_deref(),
    )
    .map_err(|e| e.to_string())?;
    let results = crate::cli::recall::apply_form4_recall_filters(
        results,
        has_citations_filter,
        source_uri_prefix.as_deref(),
    );
    let results = apply_kinds_filter(results, kinds_filter.as_deref());
    let results = apply_confidence_tier_filter(results);
    let results = apply_visibility_filter(results);
    // v0.7.0 (issue #518) — session recency boost on the keyword-only
    // fallback branch as well, so the contract is uniform regardless
    // of which retrieval mode produced the candidate set.
    let results = crate::reranker::apply_session_recency_boost(
        results,
        session_id.as_deref(),
        session_tracker,
    );
    let memories = scored_memories(results, conn);
    record_recall_observations(conn, &recall_id, &memories, "keyword");
    let mut resp = json!({
        "recall_id": recall_id,
        "memories": memories,
        "count": memories.len(),
        "mode": "keyword",
    });
    decorate_budget(&mut resp, &outcome);
    attach_meta(&mut resp, "keyword_only", &telemetry);
    super::inject_namespace_standard(conn, namespace, &mut resp);
    Ok(resp)
}

#[cfg(test)]
mod tests {
    //! L0.7-3 Tier B chunk-A — coverage tests for `handle_recall`
    //! and `handle_recall_with_pre_recall_hook`.
    //!
    //! Six-category template:
    //! A. happy path — keyword + hybrid + reranker
    //! B. validation — missing context
    //! D. state-dependent — empty result, namespace filter miss
    //! Embedder-bound: BOTH None and Some(&dyn Embed) paths.

    use super::*;
    use crate::config::{RecallScope, ResolvedScoring, ResolvedTtl};
    use crate::embeddings::test_support::MockEmbedder;
    use crate::hnsw::VectorIndex;
    use crate::models::{Memory, Tier};
    use crate::reranker::{BatchedReranker, CrossEncoder};
    use crate::storage as db;

    fn fresh_conn() -> rusqlite::Connection {
        db::open(std::path::Path::new(":memory:")).expect("open in-memory db")
    }

    fn make_mem(title: &str, content: &str, ns: &str) -> Memory {
        let now = chrono::Utc::now().to_rfc3339();
        Memory {
            id: uuid::Uuid::new_v4().to_string(),
            tier: Tier::Long,
            namespace: ns.to_string(),
            title: title.to_string(),
            content: content.to_string(),
            tags: vec![],
            priority: 5,
            confidence: 1.0,
            source: "test".to_string(),
            access_count: 0,
            created_at: now.clone(),
            updated_at: now,
            last_accessed_at: None,
            expires_at: None,
            metadata: json!({"agent_id": "ai:test"}),
            reflection_depth: 0,
            memory_kind: crate::models::MemoryKind::Observation,
            entity_id: None,
            persona_version: None,
            citations: Vec::new(),
            source_uri: None,
            source_span: None,
            confidence_source: crate::models::ConfidenceSource::CallerProvided,
            confidence_signals: None,
            confidence_decayed_at: None,
            version: 1,
        }
    }

    fn seed(conn: &rusqlite::Connection) {
        db::insert(
            conn,
            &make_mem(
                "Rust ownership",
                "Rust ownership rules prevent data races",
                "test",
            ),
        )
        .unwrap();
        db::insert(
            conn,
            &make_mem(
                "Python typing",
                "Python typing is dynamic with hints",
                "test",
            ),
        )
        .unwrap();
        db::insert(conn, &make_mem("Other topic", "Unrelated content", "other")).unwrap();
    }

    // B. validation — missing context
    #[test]
    fn missing_context_errors() {
        let conn = fresh_conn();
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let err = handle_recall(
            &conn,
            &json!({}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .unwrap_err();
        assert!(err.contains("context"));
    }

    // A. happy path — keyword-only (embedder=None)
    #[test]
    fn keyword_only_path() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("keyword"));
        assert_eq!(resp["meta"]["recall_mode"].as_str(), Some("keyword_only"));
    }

    // --- v0.7.0 #1468 — caller-scoped visibility on the recall path -------

    fn owned_mem(title: &str, agent: &str, scope: Option<&str>) -> Memory {
        let mut m = make_mem(title, "shared ownership keyword content", "vis");
        m.metadata = match scope {
            Some(s) => json!({crate::META_KEY_AGENT_ID: agent, crate::META_KEY_SCOPE: s}),
            None => json!({crate::META_KEY_AGENT_ID: agent}),
        };
        m
    }

    fn seed_vis(conn: &rusqlite::Connection) {
        use crate::models::namespace::MemoryScope;
        db::insert(conn, &owned_mem("priv", "ai:alice", None)).expect("ins");
        db::insert(
            conn,
            &owned_mem("shared", "ai:bob", Some(MemoryScope::Collective.as_str())),
        )
        .expect("ins");
    }

    fn recall_titles(resp: &Value) -> Vec<String> {
        resp["memories"]
            .as_array()
            .map(|a| {
                a.iter()
                    .filter_map(|m| m["title"].as_str().map(str::to_string))
                    .collect()
            })
            .unwrap_or_default()
    }

    // #1468 — caller=None preserves trust-all recall (single-tenant).
    #[test]
    fn recall_caller_none_returns_all() {
        let conn = fresh_conn();
        seed_vis(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall_caller(
            &conn,
            &json!({"context": "ownership", "namespace": "vis"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
            None,
        )
        .expect("ok");
        assert_eq!(resp["count"].as_u64(), Some(2));
    }

    // #1468 — a non-owner caller never recalls another agent's private row.
    #[test]
    fn recall_non_owner_excludes_cross_agent_private() {
        let conn = fresh_conn();
        seed_vis(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall_caller(
            &conn,
            &json!({"context": "ownership", "namespace": "vis"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
            Some("ai:carol"),
        )
        .expect("ok");
        assert_eq!(resp["count"].as_u64(), Some(1));
        assert_eq!(recall_titles(&resp), vec!["shared".to_string()]);
    }

    // #1468 — the owning caller recalls its OWN private row plus shared.
    #[test]
    fn recall_owner_sees_own_private_and_shared() {
        let conn = fresh_conn();
        seed_vis(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall_caller(
            &conn,
            &json!({"context": "ownership", "namespace": "vis"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
            Some("ai:alice"),
        )
        .expect("ok");
        assert_eq!(resp["count"].as_u64(), Some(2));
    }

    // A. happy path — hybrid (embedder=Some)
    #[test]
    fn hybrid_path_with_embedder() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let mock = MockEmbedder::new_local().expect("mock");
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership rules", "namespace": "test"}),
            Some(&mock as &dyn crate::embeddings::Embed),
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("hybrid"));
        assert_eq!(resp["meta"]["recall_mode"].as_str(), Some("hybrid"));
    }

    // A. happy path — hybrid + reranker
    #[test]
    fn hybrid_with_reranker_path() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let mock = MockEmbedder::new_local().expect("mock");
        let lex = CrossEncoder::new();
        let batched = BatchedReranker::new(lex);
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership rules", "namespace": "test"}),
            Some(&mock as &dyn crate::embeddings::Embed),
            None,
            Some(&batched),
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("hybrid+rerank"));
        assert_eq!(resp["meta"]["reranker_used"].as_str(), Some("lexical"));
    }

    // hybrid with vector_index Some-path
    #[test]
    fn hybrid_with_vector_index() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let mock = MockEmbedder::new_local().expect("mock");
        let idx = VectorIndex::empty();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test"}),
            Some(&mock as &dyn crate::embeddings::Embed),
            Some(&idx),
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("hybrid"));
    }

    // budget_tokens path
    #[test]
    fn budget_tokens_meta_emitted() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test", "budget_tokens": 100u64}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["meta"]["budget_tokens_used"].is_number());
        assert_eq!(resp["budget_tokens"].as_u64(), Some(100));
    }

    // budget_tokens=0 (R1 semantic: allow zero)
    #[test]
    fn budget_tokens_zero_returns_empty() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test", "budget_tokens": 0u64}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["meta"]["budget_overflow"].is_boolean());
    }

    // session_default + recall_scope splice
    #[test]
    fn session_default_recall_scope_splices_defaults() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let scope = RecallScope {
            namespaces: Some(vec!["test".to_string()]),
            since: Some("24h".to_string()),
            tier: None,
            limit: Some(2),
        };
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "session_default": true}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            Some(&scope),
        )
        .expect("ok");
        // Should match the spliced namespace ("test")
        assert!(resp["count"].as_u64().unwrap() <= 2);
    }

    // context_tokens fusion path (with embedder)
    #[test]
    fn context_tokens_fusion_path() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let mock = MockEmbedder::new_local().expect("mock");
        let resp = handle_recall(
            &conn,
            &json!({
                "context": "ownership",
                "namespace": "test",
                "context_tokens": ["rust", "memory"]
            }),
            Some(&mock as &dyn crate::embeddings::Embed),
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("hybrid"));
    }

    // as_agent path (visibility filter)
    #[test]
    fn as_agent_validated() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test", "as_agent": "ai:viewer"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["count"].is_number());
    }

    // as_agent invalid
    #[test]
    fn as_agent_invalid_errors() {
        let conn = fresh_conn();
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let err = handle_recall(
            &conn,
            &json!({"context": "ownership", "as_agent": "has space"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .unwrap_err();
        assert!(!err.is_empty());
    }

    // archive_on_gc=true exercises gc_if_needed branch
    #[test]
    fn archive_on_gc_true_runs_gc() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test"}),
            None,
            None,
            None,
            true,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["memories"].is_array());
    }

    // until + since explicit filters
    #[test]
    fn since_until_filters_applied() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({
                "context": "ownership",
                "namespace": "test",
                "since": "2000-01-01T00:00:00Z",
                "until": "2100-01-01T00:00:00Z",
                "tags": "rust",
            }),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["memories"].is_array());
    }

    // limit huge → saturate
    #[test]
    fn limit_overflow_saturates() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test", "limit": u64::MAX}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert!(resp["memories"].is_array());
    }

    // Failing embedder — drives the per-query embed-error fallback
    // (lines 357/364) and the context_tokens embed-error fallback
    // (lines 314-316).
    struct FailEmbedder {
        fail_first: bool,
        fail_second: bool,
        calls: std::sync::atomic::AtomicUsize,
    }
    impl FailEmbedder {
        fn primary_fail() -> Self {
            Self {
                fail_first: true,
                fail_second: false,
                calls: std::sync::atomic::AtomicUsize::new(0),
            }
        }
        fn secondary_fail() -> Self {
            Self {
                fail_first: false,
                fail_second: true,
                calls: std::sync::atomic::AtomicUsize::new(0),
            }
        }
    }
    impl crate::embeddings::Embed for FailEmbedder {
        fn embed(&self, _: &str) -> anyhow::Result<Vec<f32>> {
            let n = self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            if (n == 0 && self.fail_first) || (n >= 1 && self.fail_second) {
                anyhow::bail!("FailEmbedder: synthetic failure on call {n}");
            }
            Ok(vec![0.1_f32; 384])
        }
    }

    #[test]
    fn primary_embedder_error_falls_back_to_keyword() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let fe = FailEmbedder::primary_fail();
        let resp = handle_recall(
            &conn,
            &json!({"context": "ownership", "namespace": "test"}),
            Some(&fe as &dyn crate::embeddings::Embed),
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("keyword"));
        assert_eq!(resp["meta"]["recall_mode"].as_str(), Some("keyword_only"));
    }

    #[test]
    fn context_tokens_embedder_error_uses_primary_only() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let fe = FailEmbedder::secondary_fail();
        let resp = handle_recall(
            &conn,
            &json!({
                "context": "ownership",
                "namespace": "test",
                "context_tokens": ["rust", "memory"]
            }),
            Some(&fe as &dyn crate::embeddings::Embed),
            None,
            None,
            false,
            &ttl,
            &scoring,
            None,
        )
        .expect("ok");
        // hybrid mode still — primary succeeded, context_tokens failed
        assert_eq!(resp["mode"].as_str(), Some("hybrid"));
    }

    // Pre-recall hook variant: empty chain → falls through
    #[tokio::test]
    async fn pre_recall_hook_empty_chain_passes_through() {
        let conn = fresh_conn();
        seed(&conn);
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let chain = crate::hooks::HookChain::new(vec![]);
        let mut registry = crate::hooks::ExecutorRegistry::default();
        let resp = handle_recall_with_pre_recall_hook(
            &conn,
            &json!({"context": "ownership", "namespace": "test"}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            &chain,
            &mut registry,
            None,
            None,
        )
        .await
        .expect("ok");
        assert_eq!(resp["mode"].as_str(), Some("keyword"));
    }

    // Pre-recall hook variant: context missing
    #[tokio::test]
    async fn pre_recall_hook_missing_context_errors() {
        let conn = fresh_conn();
        let ttl = ResolvedTtl::default();
        let scoring = ResolvedScoring::default();
        let chain = crate::hooks::HookChain::new(vec![]);
        let mut registry = crate::hooks::ExecutorRegistry::default();
        let err = handle_recall_with_pre_recall_hook(
            &conn,
            &json!({}),
            None,
            None,
            None,
            false,
            &ttl,
            &scoring,
            &chain,
            &mut registry,
            None,
            None,
        )
        .await
        .unwrap_err();
        assert!(err.contains("context"));
    }
}