mindreader 0.6.0

Selective durable graph memory MCP server for AI agents, backed by Neo4j
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
//! Ranked fact retrieval and the graph-free `recall` input contract.
//!
//! Text and non-schema label queries rank current ordinary `ASSERTS` facts:
//! Spike category first, then subject+fact+object weight, then text score.
//! `validate_recall_args` enforces exactly one selector (`text`, `iris`,
//! `labels`, `around`, or `history`) without advertising schema unions.
//! MCP catalog labels (`Class`/`Property`) are diverted in `service` before
//! this rank path. Retrieval never changes weights.

use crate::domain::{normalize_rfc3339, DomainError};
use crate::error::{Error, Result};
use crate::graph::{endpoint_json, fact_envelope, fetch_all, node_json, rel_json, spike_rank};
use crate::iri::{is_iri, property_iri, split_camel_case};
use crate::layers::{normalize_scope, validate_layer_ids};
use neo4rs::{query, Graph, Node, Relation};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet};

/// Maximum unique terms admitted to the keyword fallback query.
const MAX_KEYWORD_TOKENS: usize = 16;
/// Short tokens are usually grammatical glue across supported natural languages.
const MIN_KEYWORD_TOKEN_CHARS: usize = 4;
/// Oversized terms are skipped rather than truncated into a different token.
const MAX_KEYWORD_TOKEN_BYTES: usize = 64;
/// Very long text skips the effectively-unmatchable exact phrase channel.
const MAX_EXACT_PHRASE_BYTES: usize = 512;
/// Lucene sentinel that matches no indexed token (empty or oversized keyword/phrase channel).
const NO_MATCH_QUERY: &str = "\"mindreadernokeywordmatch9f86d081884c\"";
/// Index-score multiplier for the exact-phrase channel.
const EXACT_TEXT_WEIGHT: f64 = 2.0;
/// Index-score multiplier for the bounded-keyword channel (weaker than exact).
const KEYWORD_TEXT_WEIGHT: f64 = 1.0;

/// In-process ranked search (`layers` here is the request visibility union).
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct SearchArgs {
    /// Request visibility union (named `layers` here; MCP wire field is `scope`).
    pub layers: Vec<String>,
    /// Lexical query: exact phrase, bounded keywords, and exact property-name match.
    #[serde(default)]
    pub text: Option<String>,
    /// Subject/object label filter for the rank path (not the Class/Property catalog).
    #[serde(default)]
    pub labels: Option<Vec<String>>,
    /// Maximum facts; default 20, clamped to 1..=100.
    #[serde(default)]
    pub limit: Option<u32>,
    /// Optional world-time instant; only explicitly qualified facts can match.
    #[serde(default, rename = "effectiveAt")]
    pub effective_at: Option<String>,
}

/// Internal direct-search evidence used by semantic rank fusion.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct TextMatch {
    /// Phrase or exact-property channel hit.
    pub exact: bool,
    /// Fraction of bounded query terms present in `factText` (`1.0` when exact).
    pub keyword_confidence: f64,
    /// May seed a semantic activation bundle (exact or full keyword coverage).
    pub bundle_eligible: bool,
}

/// Private graph identity used to diversify learned semantic bundles.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct FactGroup {
    pub subject_iri: String,
    pub property_iri: String,
}

/// Internal ranked search result plus channel evidence that must not leak onto the MCP wire.
pub(crate) struct SearchResult {
    pub facts: Vec<Value>,
    pub about: Vec<Value>,
    pub scope: Vec<String>,
    pub mode: &'static str,
    pub truncated: bool,
    /// Per-fact exact/keyword evidence; must not appear on the MCP wire.
    pub text_matches: HashMap<String, TextMatch>,
    /// Subject+property identity used to diversify semantic bundles.
    pub fact_groups: HashMap<String, FactGroup>,
}

impl SearchResult {
    /// Drop fusion-only maps and emit the closed-world envelope (`nodes`/`paths`/`lookups` empty).
    fn into_payload(self) -> Value {
        json!({
            "ok": true,
            "mode": self.mode,
            "facts": self.facts,
            "nodes": [],
            "paths": [],
            "about": self.about,
            "lookups": [],
            "scope": self.scope,
            "truncated": self.truncated,
        })
    }
}

/// MCP `recall` arguments. Runtime accepts exactly one selector.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RecallArgs {
    /// Request visibility union; empty is global-only.
    pub scope: Vec<String>,
    /// Lexical selector; mutually exclusive with the other four selectors.
    #[serde(default)]
    pub text: Option<String>,
    /// 1–20 node IRIs; hops applies only here.
    #[serde(default)]
    pub iris: Option<Vec<String>>,
    /// Catalog (`Class`/`Property`) or ranked non-schema label filter.
    #[serde(default)]
    pub labels: Option<Vec<String>>,
    /// Starting node IRI for bounded graph walk; `p` and `depth` apply only here.
    #[serde(default)]
    pub around: Option<String>,
    /// Iris only: `0` (default) keeps incident facts on `lookups[i].facts`;
    /// `1` also copies them to top-level `facts[]` when `detail` is `detailed`.
    /// Concise iris recall clears top-level `facts[]` so the answer stays in lookups.
    #[serde(default)]
    pub hops: Option<u32>,
    /// Around mode only: predicate names or IRIs applied before the fact limit.
    #[serde(default)]
    pub p: Option<Vec<String>>,
    /// Around mode only: traversal depth 1..=3.
    #[serde(default)]
    pub depth: Option<u32>,
    /// Around only: `both`, `outgoing`, or `incoming` for the whole path from the start node.
    #[serde(default)]
    pub direction: Option<String>,
    /// Node or fact IRI whose current and `validTo` facts are returned.
    #[serde(default)]
    pub history: Option<String>,
    /// `concise` or `detailed`; omitted defaults to concise.
    #[serde(default)]
    pub detail: Option<String>,
    /// Maximum facts; default 20, at most 100.
    #[serde(default)]
    pub limit: Option<u32>,
    /// Optional world-time instant for text, non-catalog labels, IRI, or around recall.
    #[serde(default, rename = "effectiveAt")]
    pub effective_at: Option<String>,
}

/// Graph-free recall input contract (runtime XOR, no schema union).
pub fn validate_recall_args(args: &RecallArgs) -> Result<()> {
    validate_layer_ids(args.scope.clone())?;
    let text = args
        .text
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty());
    let iris = args.iris.as_ref().map(Vec::len).unwrap_or(0);
    let labels = args.labels.as_ref().map(Vec::len).unwrap_or(0);
    let around = args
        .around
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty());
    let history = args
        .history
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty());
    let selectors = [
        ("text", text.is_some()),
        ("iris", iris > 0),
        ("labels", labels > 0),
        ("around", around.is_some()),
        ("history", history.is_some()),
    ]
    .into_iter()
    .filter_map(|(name, selected)| selected.then_some(name))
    .collect::<Vec<_>>();
    if selectors.is_empty() {
        return Err(DomainError::InvalidInput(
            "recall requires one selector: text, iris, labels, around, or history".into(),
        )
        .into());
    }
    if selectors.len() > 1 {
        return Err(DomainError::InvalidInput(format!(
            "recall selectors are mutually exclusive; received {}; keep only one of text, iris, labels, around, or history",
            selectors.join(", ")
        ))
        .into());
    }
    let selector = selectors[0];
    crate::payload::Detail::parse(args.detail.as_deref())?;
    if selector != "iris" && args.hops.is_some() {
        return Err(DomainError::InvalidInput(format!(
            "recall hops applies only to the iris selector, not {selector}"
        ))
        .into());
    }
    if selector != "around" && args.p.is_some() {
        return Err(DomainError::InvalidInput(format!(
            "recall p applies only to the around selector, not {selector}"
        ))
        .into());
    }
    if selector != "around" && args.depth.is_some() {
        return Err(DomainError::InvalidInput(format!(
            "recall depth applies only to the around selector, not {selector}"
        ))
        .into());
    }
    if selector != "around" && args.direction.is_some() {
        return Err(DomainError::InvalidInput(format!(
            "recall direction applies only to the around selector, not {selector}"
        ))
        .into());
    }
    if selector != "history" && args.history.is_some() {
        return Err(DomainError::InvalidInput(format!(
            "recall history applies only to the history selector, not {selector}"
        ))
        .into());
    }
    if let Some(effective_at) = args.effective_at.as_deref() {
        normalize_rfc3339(effective_at, "recall effectiveAt")?;
        if selector == "history" {
            return Err(DomainError::InvalidInput(
                "recall effectiveAt does not apply to history; history exposes both clocks".into(),
            )
            .into());
        }
        if selector == "labels" && args.labels.as_deref().is_some_and(is_schema_catalog_labels) {
            return Err(DomainError::InvalidInput(
                "recall effectiveAt does not apply to the Class/Property catalog".into(),
            )
            .into());
        }
    }
    if let Some(values) = &args.iris {
        if !(1..=20).contains(&values.len()) {
            return Err(DomainError::InvalidInput(
                "recall iris must contain 1..=20 node IRIs".into(),
            )
            .into());
        }
        let mut seen = HashSet::new();
        for value in values {
            let iri = value.trim();
            if !is_iri(iri) || iri.starts_with("mindreader:relationship/") {
                return Err(DomainError::InvalidInput(format!(
                    "recall iris accepts node IRIs, not {value:?}"
                ))
                .into());
            }
            if !seen.insert(iri) {
                return Err(DomainError::InvalidInput(format!(
                    "recall iris contains duplicate IRI {iri:?}"
                ))
                .into());
            }
        }
    }
    if let Some(values) = &args.labels {
        if values.is_empty() || values.iter().any(|value| value.trim().is_empty()) {
            return Err(DomainError::InvalidInput(
                "recall labels must contain non-empty labels".into(),
            )
            .into());
        }
    }
    if let Some(values) = &args.p {
        if values.is_empty() || values.iter().any(|value| value.trim().is_empty()) {
            return Err(DomainError::InvalidInput(
                "recall p must contain non-empty predicates".into(),
            )
            .into());
        }
    }
    if let Some(iri) = around {
        if !is_iri(iri) || iri.starts_with("mindreader:relationship/") {
            return Err(DomainError::InvalidInput(format!(
                "recall around requires a node IRI, not {iri:?}"
            ))
            .into());
        }
    }
    if let Some(iri) = history {
        if !is_iri(iri) {
            return Err(DomainError::InvalidInput(format!(
                "recall history requires a node or fact IRI, not {iri:?}"
            ))
            .into());
        }
    }
    if let Some(hops) = args.hops {
        if hops != 0 && hops != 1 {
            return Err(DomainError::InvalidInput("recall hops must be 0 or 1".into()).into());
        }
    }
    if let Some(depth) = args.depth {
        if !(1..=3).contains(&depth) {
            return Err(DomainError::InvalidInput("recall depth must be 1..=3".into()).into());
        }
    }
    if let Some(direction) = args.direction.as_deref() {
        if !matches!(direction, "both" | "outgoing" | "incoming") {
            return Err(DomainError::InvalidInput(
                "recall direction must be both, outgoing, or incoming".into(),
            )
            .into());
        }
    }
    if let Some(limit) = args.limit {
        if limit == 0 || limit > 100 {
            return Err(Error::from(DomainError::InvalidInput(
                "recall limit must be 1..=100".into(),
            )));
        }
    }
    Ok(())
}

/// Required fact IRI from the canonical pasteable target envelope.
pub fn fact_handle_iri(fact: &Value) -> Result<&str> {
    fact.pointer("/target/iri")
        .and_then(Value::as_str)
        .filter(|iri| !iri.is_empty())
        .ok_or_else(|| crate::graph_error!("fact result is missing target.iri"))
}

/// True when every label is `Class` or `Property` (catalog path, not ranked search).
pub fn is_schema_catalog_labels(labels: &[String]) -> bool {
    !labels.is_empty()
        && labels.iter().all(|label| {
            let trimmed = label.trim();
            trimmed == "Class" || trimmed == "Property"
        })
}

/// Quote a Lucene phrase and escape operators so user text cannot change query shape.
fn lucene_query(text: &str) -> String {
    let escaped = lucene_escape(text);
    let split = split_camel_case(text);
    if split != text {
        format!("({escaped} OR {})", lucene_escape(&split))
    } else {
        escaped
    }
}

/// Extract unique Unicode-alphanumeric terms with the search channel's length bounds.
fn keyword_tokens(text: &str, limit: usize) -> Vec<String> {
    let split = split_camel_case(text);
    let mut tokens = Vec::new();
    let mut seen = HashSet::new();
    let mut current = String::new();
    let push_token =
        |current: &mut String, tokens: &mut Vec<String>, seen: &mut HashSet<String>| {
            if current.is_empty() || tokens.len() >= limit {
                current.clear();
                return;
            }
            let token = current.to_lowercase();
            current.clear();
            if token.chars().count() >= MIN_KEYWORD_TOKEN_CHARS
                && token.len() <= MAX_KEYWORD_TOKEN_BYTES
                && seen.insert(token.clone())
            {
                tokens.push(token);
            }
        };
    for character in split.chars() {
        if character.is_alphanumeric() {
            current.push(character);
        } else {
            push_token(&mut current, &mut tokens, &mut seen);
        }
    }
    push_token(&mut current, &mut tokens, &mut seen);
    tokens
}

/// Build a bounded OR query from unique Unicode-alphanumeric terms.
fn lucene_keyword_query(text: &str) -> String {
    let tokens = keyword_tokens(text, MAX_KEYWORD_TOKENS);
    if tokens.is_empty() {
        return NO_MATCH_QUERY.into();
    }
    tokens
        .iter()
        .map(|token| lucene_escape(token))
        .collect::<Vec<_>>()
        .join(" OR ")
}

/// Fraction of bounded query terms present anywhere in a relationship's indexed text.
fn keyword_coverage(query_tokens: &[String], fact_text: &str) -> f64 {
    if query_tokens.is_empty() {
        return 0.0;
    }
    let fact_tokens = keyword_tokens(fact_text, usize::MAX)
        .into_iter()
        .collect::<HashSet<_>>();
    let matched = query_tokens
        .iter()
        .filter(|token| fact_tokens.contains(*token))
        .count();
    matched as f64 / query_tokens.len() as f64
}

/// Exact phrase query, bounded so long natural-language input uses keywords only.
fn lucene_exact_query(text: &str) -> String {
    if text.len() > MAX_EXACT_PHRASE_BYTES {
        NO_MATCH_QUERY.into()
    } else {
        lucene_query(text)
    }
}

/// Build exact and fallback queries without executing an identical keyword channel twice.
fn lucene_search_queries(text: &str) -> (String, String) {
    let exact = lucene_exact_query(text);
    let keyword = lucene_keyword_query(text);
    if keyword == exact {
        (exact, NO_MATCH_QUERY.into())
    } else {
        (exact, keyword)
    }
}

/// Phrase-quote user text and escape Lucene operators so they stay literals.
fn lucene_escape(text: &str) -> String {
    let mut out = String::from("\"");
    for ch in text.chars() {
        if "+-&|!(){}[]^\"~*?:\\/".contains(ch) {
            out.push('\\');
        }
        out.push(ch);
    }
    out.push('"');
    out
}

/// Full-text node, fact, and exact-property candidates for ranked text recall.
const TEXT_CANDIDATES: &str = r#"
CALL {
  CALL db.index.fulltext.queryNodes('wakeup_nodes', $qExact) YIELD node, score
  MATCH (node)-[relationship]-(other:Entity)
  RETURN startNode(relationship) AS s, relationship AS r,
         endNode(relationship) AS o,
         0.0 AS factScore, 0 AS factExact,
         score * $exactTextWeight AS endpointScore
  UNION ALL
  CALL db.index.fulltext.queryRelationships('wakeup_facts', $qExact)
  YIELD relationship, score
  RETURN startNode(relationship) AS s, relationship AS r,
         endNode(relationship) AS o,
         score * $exactTextWeight AS factScore, 1 AS factExact,
         0.0 AS endpointScore
  UNION ALL
  CALL db.index.fulltext.queryNodes('wakeup_nodes', $qKeywords) YIELD node, score
  MATCH (node)-[relationship]-(other:Entity)
  RETURN startNode(relationship) AS s, relationship AS r,
         endNode(relationship) AS o,
         0.0 AS factScore, 0 AS factExact,
         score * $keywordTextWeight AS endpointScore
  UNION ALL
  CALL db.index.fulltext.queryRelationships('wakeup_facts', $qKeywords)
  YIELD relationship, score
  RETURN startNode(relationship) AS s, relationship AS r,
         endNode(relationship) AS o,
         score * $keywordTextWeight AS factScore, 0 AS factExact,
         0.0 AS endpointScore
  UNION ALL
  MATCH (p:Entity:Property)
  WHERE toLower(coalesce(p.name, '')) = toLower($predicateName)
     OR p.iri = $predicateIri
     OR p.iri ENDS WITH ('/' + $predicateName)
  MATCH (s:Entity)-[r:ASSERTS]->(o:Entity)
  WHERE r.validTo IS NULL AND r.propertyIri = p.iri
  RETURN s, r, o,
         1.0 AS factScore, 1 AS factExact,
         0.0 AS endpointScore
}
WITH s, r, o,
     max(factScore) AS factScore, max(factExact) AS factExact,
     max(endpointScore) AS endpointScore
WITH s, r, o,
     CASE WHEN factScore > 0.0 THEN factScore ELSE endpointScore END AS indexScore,
     factExact AS relationshipExact,
     CASE WHEN factScore > 0.0 THEN 1 ELSE 0 END AS relationshipMatched,
     coalesce(r.factText, '') AS factText
"#;

/// Unfiltered current-edge scan used when ranking by labels only.
const LABEL_CANDIDATES: &str = r#"
MATCH (s:Entity)-[r]->(o:Entity)
WITH s, r, o, 1.0 AS indexScore,
     0 AS relationshipExact, 0 AS relationshipMatched, '' AS factText
"#;

/// Shared rank tail: current ordinary assertions, endpoint closure, Spike then weight then score.
const RANK_AND_LIMIT: &str = r#"
WHERE r.validTo IS NULL
  AND type(r) = 'ASSERTS'
  AND ($effectiveAt IS NULL
       OR (coalesce(r.effectiveQualified, false)
           AND (r.effectiveFrom IS NULL OR r.effectiveFrom <= datetime($effectiveAt))
           AND (r.effectiveTo IS NULL OR datetime($effectiveAt) < r.effectiveTo)))
  AND (size(s.layers) = 0
       OR any(layer IN s.layers WHERE layer IN $layers))
  AND (size(r.layers) = 0
       OR any(layer IN r.layers WHERE layer IN $layers))
  AND (size(o.layers) = 0
       OR any(layer IN o.layers WHERE layer IN $layers))
  AND ($labelCount = 0
       OR any(label IN $labels WHERE label IN labels(s) OR label IN labels(o)))
WITH s, r, o, indexScore, relationshipExact, relationshipMatched, factText,
     CASE
       WHEN r.spike = 'Knowledge' THEN 4
       WHEN r.spike = 'Insight' THEN 3
       WHEN r.spike = 'Pattern' THEN 2
       WHEN r.spike = 'Signal' THEN 1
       ELSE 0
     END AS ownSpikeRank
OPTIONAL MATCH (sp:Entity)-[a:ABOUT]->(s)
WHERE a.validTo IS NULL
  AND a.spike IS NOT NULL
  AND (size(sp.layers) = 0
       OR any(layer IN sp.layers WHERE layer IN $layers))
  AND (size(a.layers) = 0
       OR any(layer IN a.layers WHERE layer IN $layers))
  AND (size(s.layers) = 0
       OR any(layer IN s.layers WHERE layer IN $layers))
WITH s, r, o, indexScore, relationshipExact, relationshipMatched, factText,
     ownSpikeRank,
     max(CASE
       WHEN a.spike = 'Knowledge' THEN 4
       WHEN a.spike = 'Insight' THEN 3
       WHEN a.spike = 'Pattern' THEN 2
       WHEN a.spike = 'Signal' THEN 1
       ELSE 0
     END) AS attachedSpikeRank,
     s.weight AS subjectWeight,
     r.weight AS relationshipWeight,
     o.weight AS objectWeight
WITH s, r, o, indexScore, relationshipExact, relationshipMatched, factText,
     CASE WHEN ownSpikeRank > 0 THEN ownSpikeRank ELSE attachedSpikeRank END AS spikeRank,
     CASE
       WHEN subjectWeight > 0 AND relationshipWeight > $maxWeight - subjectWeight THEN $maxWeight
       WHEN subjectWeight < 0 AND relationshipWeight < $minWeight - subjectWeight THEN $minWeight
       ELSE subjectWeight + relationshipWeight
     END AS subjectRelationshipWeight,
     objectWeight
WITH s, r, o, relationshipExact, relationshipMatched, factText, spikeRank,
     CASE
       WHEN subjectRelationshipWeight > 0 AND objectWeight > $maxWeight - subjectRelationshipWeight THEN $maxWeight
       WHEN subjectRelationshipWeight < 0 AND objectWeight < $minWeight - subjectRelationshipWeight THEN $minWeight
       ELSE subjectRelationshipWeight + objectWeight
     END AS effectiveWeight,
     indexScore AS score,
     r.propertyIri AS property
ORDER BY spikeRank DESC, effectiveWeight DESC, score DESC,
         s.iri ASC, property ASC, o.iri ASC, r.iri ASC
LIMIT $limit
RETURN s, r, o, property, spikeRank, effectiveWeight, score,
       relationshipExact, relationshipMatched, factText
"#;

/// Assemble the closed-world rank query: text index or label filter, then Spike/weight/score.
fn ranked_query(text_mode: bool) -> String {
    let candidates = if text_mode {
        TEXT_CANDIDATES
    } else {
        LABEL_CANDIDATES
    };
    format!("{candidates}{RANK_AND_LIMIT}")
}

/// Explicit `ABOUT` facts that give Spike context around ranked subjects.
async fn spike_context(
    graph: &Graph,
    layers: &[String],
    about_iris: Vec<String>,
    limit: i64,
) -> Result<Vec<Value>> {
    if about_iris.is_empty() {
        return Ok(Vec::new());
    }
    let mut spike_list = Vec::new();
    let mut seen_spike = HashSet::new();
    for row in fetch_all(
        graph,
        query(
            r#"
            MATCH (sp:Entity)-[a:ABOUT]->(el:Entity)
            WHERE a.validTo IS NULL AND el.iri IN $iris
              AND a.spike IS NOT NULL
              AND (size(sp.layers) = 0
                   OR any(layer IN sp.layers WHERE layer IN $layers))
              AND (size(a.layers) = 0
                   OR any(layer IN a.layers WHERE layer IN $layers))
              AND (size(el.layers) = 0
                   OR any(layer IN el.layers WHERE layer IN $layers))
            WITH sp, a, el,
                 CASE
                   WHEN a.spike = 'Knowledge' THEN 4
                   WHEN a.spike = 'Insight' THEN 3
                   WHEN a.spike = 'Pattern' THEN 2
                   WHEN a.spike = 'Signal' THEN 1
                   ELSE 0
                 END AS spikeRank,
                 sp.weight AS spikeWeight,
                 a.weight AS relationshipWeight,
                 el.weight AS elementWeight
            WITH sp, a, el, spikeRank,
                 CASE
                   WHEN spikeWeight > 0 AND relationshipWeight > $maxWeight - spikeWeight THEN $maxWeight
                   WHEN spikeWeight < 0 AND relationshipWeight < $minWeight - spikeWeight THEN $minWeight
                   ELSE spikeWeight + relationshipWeight
                 END AS spikeRelationshipWeight,
                 elementWeight
            WITH sp, a, el, spikeRank,
                 CASE
                   WHEN spikeRelationshipWeight > 0 AND elementWeight > $maxWeight - spikeRelationshipWeight THEN $maxWeight
                   WHEN spikeRelationshipWeight < 0 AND elementWeight < $minWeight - spikeRelationshipWeight THEN $minWeight
                   ELSE spikeRelationshipWeight + elementWeight
                 END AS effectiveWeight
            WITH sp, a, el, spikeRank, effectiveWeight
            ORDER BY spikeRank DESC, effectiveWeight DESC, el.iri ASC, sp.iri ASC
            LIMIT $limit
            RETURN sp, a, el, effectiveWeight
            "#,
        )
        .param("layers", layers.to_vec())
        .param("iris", about_iris)
        .param("minWeight", i64::MIN)
        .param("maxWeight", i64::MAX)
        .param("limit", limit),
    )
    .await?
    {
        let sp = row.get::<Node>("sp")?;
        let about_rel = row.get::<Relation>("a")?;
        let element = row.get::<Node>("el")?;
        let Some(rank) = about_rel.get::<String>("spike").ok() else {
            continue;
        };
        let about = element.get::<String>("iri")?;
        let sp_iri = sp.get::<String>("iri")?;
        let relationship = rel_json(&about_rel, &sp_iri, &about)?;
        let combined = row.get::<i64>("effectiveWeight")?;
        let rel_iri = about_rel.get::<String>("iri")?;
        if seen_spike.insert(rel_iri) {
            spike_list.push(json!({
                "node": node_json(&sp)?,
                "about": about,
                "rank": rank,
                "relationship": relationship,
                "effectiveWeight": combined,
            }));
        }
    }
    spike_list.sort_by(|left, right| {
        spike_rank(right.get("rank").and_then(Value::as_str))
            .cmp(&spike_rank(left.get("rank").and_then(Value::as_str)))
            .then_with(|| {
                right
                    .get("effectiveWeight")
                    .and_then(Value::as_i64)
                    .cmp(&left.get("effectiveWeight").and_then(Value::as_i64))
            })
            .then_with(|| {
                left.get("about")
                    .and_then(Value::as_str)
                    .cmp(&right.get("about").and_then(Value::as_str))
            })
            .then_with(|| {
                left.pointer("/node/iri")
                    .and_then(Value::as_str)
                    .cmp(&right.pointer("/node/iri").and_then(Value::as_str))
            })
    });
    Ok(spike_list)
}

/// Rank current visible ordinary facts for text and/or non-schema labels.
pub async fn memory_search(graph: &Graph, args: SearchArgs) -> Result<Value> {
    Ok(memory_search_with_matches(graph, args)
        .await?
        .into_payload())
}

/// Execute ranked search while retaining private exact-vs-keyword evidence for semantic fusion.
pub(crate) async fn memory_search_with_matches(
    graph: &Graph,
    args: SearchArgs,
) -> Result<SearchResult> {
    let layers = normalize_scope(args.layers)?;
    let limit = args.limit.unwrap_or(20).clamp(1, 100) as i64;
    let labels = args.labels.unwrap_or_default();
    let trimmed = args.text.unwrap_or_default().trim().to_string();
    let effective_at = args
        .effective_at
        .as_deref()
        .map(|value| normalize_rfc3339(value, "recall effectiveAt"))
        .transpose()?;
    // In-process SearchArgs may be empty; MCP recall never reaches here without a selector.
    if trimmed.is_empty() && labels.is_empty() {
        return Ok(SearchResult {
            facts: Vec::new(),
            about: Vec::new(),
            scope: layers,
            mode: "labels",
            truncated: false,
            text_matches: HashMap::new(),
            fact_groups: HashMap::new(),
        });
    }

    let text_mode = !trimmed.is_empty();
    let query_tokens = keyword_tokens(&trimmed, MAX_KEYWORD_TOKENS);
    let query_limit = limit.saturating_add(1);
    let mut ranked = query(&ranked_query(text_mode))
        .param("layers", layers.clone())
        .param("labels", labels.clone())
        .param("labelCount", labels.len() as i64)
        .param("limit", query_limit)
        .param("minWeight", i64::MIN)
        .param("maxWeight", i64::MAX)
        .param("effectiveAt", effective_at);
    if text_mode {
        let (exact_query, keyword_query) = lucene_search_queries(&trimmed);
        ranked = ranked
            .param("qExact", exact_query)
            .param("qKeywords", keyword_query)
            .param("exactTextWeight", EXACT_TEXT_WEIGHT)
            .param("keywordTextWeight", KEYWORD_TEXT_WEIGHT)
            .param("predicateName", trimmed.clone())
            .param("predicateIri", property_iri(&trimmed));
    }

    let mut facts = Vec::new();
    let mut text_matches = HashMap::new();
    let mut fact_groups = HashMap::new();
    for row in fetch_all(graph, ranked).await? {
        let subject = row.get::<Node>("s")?;
        let relationship = row.get::<Relation>("r")?;
        let object = row.get::<Node>("o")?;
        let subject_iri = subject.get::<String>("iri")?;
        let object_iri = object.get::<String>("iri")?;
        let fact_iri = relationship.get::<String>("iri")?;
        let effective_weight = row.get::<i64>("effectiveWeight")?;
        let property = row.get::<String>("property")?;
        let scope_vec = relationship.get::<Vec<String>>("layers")?;
        let relationship = rel_json(&relationship, &subject_iri, &object_iri)?;
        let mut fact = fact_envelope(
            endpoint_json(&subject)?,
            &property,
            endpoint_json(&object)?,
            &relationship,
            &scope_vec,
        )?;
        fact["score"] = json!(row.get::<f64>("score")?);
        fact["effectiveWeight"] = json!(effective_weight);
        // Endpoint-only hits get a fact group but no fusion evidence.
        if row.get::<i64>("relationshipMatched")? != 0 {
            let exact = row.get::<i64>("relationshipExact")? != 0;
            let keyword_confidence = if exact {
                1.0
            } else {
                keyword_coverage(&query_tokens, &row.get::<String>("factText")?)
            };
            text_matches.insert(
                fact_iri.clone(),
                TextMatch {
                    exact,
                    keyword_confidence,
                    bundle_eligible: exact || keyword_confidence == 1.0,
                },
            );
        }
        fact_groups.insert(
            fact_iri,
            FactGroup {
                subject_iri,
                property_iri: property,
            },
        );
        facts.push(fact);
    }
    let truncated = facts.len() > limit as usize;
    facts.truncate(limit as usize);
    let mut element_iris = HashSet::new();
    for fact in &facts {
        if let Some(iri) = fact.pointer("/s/iri").and_then(Value::as_str) {
            element_iris.insert(iri.to_string());
        }
        let object_is_element = fact
            .pointer("/o/labels")
            .and_then(Value::as_array)
            .is_some_and(|labels| labels.iter().any(|label| label == "Element"));
        if object_is_element {
            if let Some(iri) = fact.pointer("/o/iri").and_then(Value::as_str) {
                element_iris.insert(iri.to_string());
            }
        }
    }
    let spike = spike_context(
        graph,
        &layers,
        element_iris.into_iter().collect::<Vec<_>>(),
        limit,
    )
    .await?;
    Ok(SearchResult {
        facts,
        about: spike,
        scope: layers,
        mode: if text_mode { "text" } else { "labels" },
        truncated,
        text_matches,
        fact_groups,
    })
}

#[cfg(test)]
mod tests {
    use super::{
        fact_handle_iri, keyword_coverage, keyword_tokens, lucene_escape, lucene_exact_query,
        lucene_keyword_query, lucene_query, lucene_search_queries, ranked_query,
        validate_recall_args, RecallArgs, MAX_EXACT_PHRASE_BYTES, MAX_KEYWORD_TOKENS,
    };
    use serde_json::json;

    #[test]
    fn search_helpers_preserve_contract_values() {
        assert_eq!(lucene_escape("C++"), "\"C\\+\\+\"");
        assert_eq!(
            lucene_query("graphModel"),
            "(\"graphModel\" OR \"graph Model\")"
        );
        assert_eq!(
            lucene_keyword_query("Graph graphModel / C++ 2026"),
            "\"graph\" OR \"model\" OR \"2026\""
        );
        assert_eq!(
            lucene_keyword_query("the und per con"),
            super::NO_MATCH_QUERY
        );
        assert_eq!(lucene_keyword_query("!!!"), super::NO_MATCH_QUERY);
        assert_eq!(
            lucene_exact_query(&"x".repeat(MAX_EXACT_PHRASE_BYTES + 1)),
            super::NO_MATCH_QUERY
        );
        assert_eq!(
            lucene_search_queries("common"),
            ("\"common\"".into(), super::NO_MATCH_QUERY.into())
        );
        assert_eq!(
            lucene_search_queries("common benchmark"),
            (
                "\"common benchmark\"".into(),
                "\"common\" OR \"benchmark\"".into()
            )
        );
        assert!(ranked_query(true).contains("r.propertyIri = p.iri"));
        assert!(ranked_query(true).contains("$qExact"));
        assert!(ranked_query(true).contains("$qKeywords"));
        assert!(ranked_query(true).contains(
            "CASE WHEN factScore > 0.0 THEN factScore ELSE endpointScore END AS indexScore"
        ));
        let query = ranked_query(true);
        assert!(query.contains("AND type(r) = 'ASSERTS'"));
        assert!(query.contains("coalesce(r.effectiveQualified, false)"));
        assert!(query.contains("datetime($effectiveAt) < r.effectiveTo"));
        assert!(query.contains("WHEN r.spike = 'Knowledge'"));
        assert!(query.contains("WHEN a.spike = 'Knowledge'"));
        assert!(!query.contains("WHEN s:Knowledge"));
        assert!(query.contains("ORDER BY spikeRank DESC, effectiveWeight DESC, score DESC"));
        assert!(query.contains("LIMIT $limit"));
        assert!(!query.contains("collect("));
    }

    #[test]
    fn keyword_coverage_measures_unique_query_term_overlap() {
        let query = keyword_tokens("marlin circles beneath the boat", MAX_KEYWORD_TOKENS);
        assert_eq!(query, ["marlin", "circles", "beneath", "boat"]);
        assert_eq!(
            keyword_coverage(&query, "The marlin circles beneath Santiago's skiff"),
            0.75
        );
        assert_eq!(keyword_coverage(&query, "The fisherman returns"), 0.0);
    }

    #[test]
    fn fact_handle_accepts_only_the_canonical_target() {
        assert_eq!(
            fact_handle_iri(&json!({"target": {"kind": "fact", "iri": "fact:1"}})).unwrap(),
            "fact:1"
        );
        assert!(fact_handle_iri(&json!({"relationship": {"iri": "fact:legacy"}})).is_err());
    }

    #[test]
    fn recall_args_require_exactly_one_selector() {
        let mut args = RecallArgs {
            scope: vec!["project:x".into()],
            text: None,
            iris: None,
            labels: None,
            around: None,
            hops: None,
            p: None,
            depth: None,
            direction: None,
            history: None,
            detail: None,
            limit: None,
            effective_at: None,
        };
        assert_eq!(
            validate_recall_args(&args).unwrap_err().to_string(),
            "recall requires one selector: text, iris, labels, around, or history"
        );
        args.text = Some("Alice".into());
        assert!(validate_recall_args(&args).is_ok());
        args.around = Some("mindreader:element/alice".into());
        assert_eq!(
            validate_recall_args(&args).unwrap_err().to_string(),
            "recall selectors are mutually exclusive; received text, around; keep only one of text, iris, labels, around, or history"
        );
        args.around = None;
        args.hops = Some(2);
        assert!(validate_recall_args(&args).is_err());
        args.text = None;
        args.iris = Some(vec!["mindreader:element/alice".into()]);
        args.hops = Some(1);
        assert!(validate_recall_args(&args).is_ok());
        args.iris = Some(vec!["mindreader:relationship/fact".into()]);
        assert!(validate_recall_args(&args).is_err());
        args.iris = None;
        args.hops = None;
        args.history = Some("mindreader:relationship/fact".into());
        assert!(validate_recall_args(&args).is_ok());
        args.detail = Some("verbose".into());
        assert!(validate_recall_args(&args).is_err());
    }

    #[test]
    fn recall_args_reject_selector_inapplicable_fields_and_bad_bounds() {
        let base = RecallArgs {
            scope: vec![],
            text: Some("Alice".into()),
            iris: None,
            labels: None,
            around: None,
            hops: None,
            p: None,
            depth: None,
            direction: None,
            history: None,
            detail: None,
            limit: Some(20),
            effective_at: None,
        };
        assert!(validate_recall_args(&base).is_ok());
        assert!(validate_recall_args(&RecallArgs {
            hops: Some(1),
            ..base.clone()
        })
        .is_err());
        assert!(validate_recall_args(&RecallArgs {
            limit: Some(101),
            ..base.clone()
        })
        .is_err());
        assert!(validate_recall_args(&RecallArgs {
            text: None,
            around: Some("mindreader:element/alice".into()),
            direction: Some("outgoing".into()),
            ..base.clone()
        })
        .is_ok());
        assert!(validate_recall_args(&RecallArgs {
            text: None,
            around: Some("mindreader:element/alice".into()),
            direction: Some("sideways".into()),
            ..base.clone()
        })
        .is_err());
        assert!(validate_recall_args(&RecallArgs {
            text: None,
            iris: Some(vec![
                "mindreader:element/alice".into(),
                " mindreader:element/alice ".into(),
            ]),
            ..base
        })
        .is_err());
    }

    #[test]
    fn recall_effective_at_is_strict_and_selector_aware() {
        let base = RecallArgs {
            scope: vec![],
            text: Some("Alice".into()),
            iris: None,
            labels: None,
            around: None,
            hops: None,
            p: None,
            depth: None,
            direction: None,
            history: None,
            detail: None,
            limit: Some(20),
            effective_at: Some("2024-01-01T01:00:00+01:00".into()),
        };
        assert!(validate_recall_args(&base).is_ok());
        assert!(validate_recall_args(&RecallArgs {
            effective_at: Some("2024-01-01".into()),
            ..base.clone()
        })
        .is_err());
        assert!(validate_recall_args(&RecallArgs {
            text: None,
            history: Some("mindreader:relationship/fact".into()),
            ..base.clone()
        })
        .is_err());
        assert!(validate_recall_args(&RecallArgs {
            text: None,
            labels: Some(vec!["Property".into()]),
            ..base
        })
        .is_err());
    }
}