holographic-memory 0.6.4

Local-first search engine and agent memory: fast approximate nearest-neighbor and BM25 full-text search, encrypted stores, provable deletion, built on vector-symbolic architectures.
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
// Copyright 2024-2026 WritersLogic Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{ensure, Result};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::sync::OnceLock;

use crate::core::documents::terms;

pub type Source = (String, String, String, String, String);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Candidate {
    pub observations: Vec<Source>,
    pub ordinal: usize,
    pub rank: usize,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
    pub question: String,
    pub budget: usize,
    pub candidates: Vec<Candidate>,
}

#[derive(Debug, Serialize)]
pub struct Obligations {
    pub topics: BTreeSet<String>,
    pub identities: Vec<String>,
    pub literal_quotes: Vec<String>,
    pub date_literals: Vec<String>,
    pub inference_boundaries: Vec<String>,
    pub quantity: bool,
    pub chronology: bool,
    pub exact_quote: bool,
}

#[derive(Debug, Serialize)]
pub struct Retrieval {
    pub required: Obligations,
    pub selected: Vec<usize>,
    pub witnessed: BTreeMap<String, Vec<usize>>,
    pub packet: String,
}

#[derive(Serialize)]
struct Packet<'a> {
    v: u8,
    q: Vec<&'a Vec<Source>>,
    i: Vec<String>,
}

fn tokens(text: &str) -> BTreeSet<String> {
    terms(text, true).into_keys().collect()
}

fn valid_date(date: &str) -> bool {
    let Some(prefix) = date.get(..10) else {
        return false;
    };
    let bytes = prefix.as_bytes();
    if !b"/-".contains(&bytes[4])
        || bytes[7] != bytes[4]
        || bytes
            .iter()
            .enumerate()
            .any(|(i, b)| i != 4 && i != 7 && !b.is_ascii_digit())
    {
        return false;
    }
    let year = prefix[..4].parse::<u16>().unwrap_or(0);
    let month = prefix[5..7].parse::<u8>().unwrap_or(0);
    let day = prefix[8..10].parse::<u8>().unwrap_or(0);
    let leap = year.is_multiple_of(4) && (!year.is_multiple_of(100) || year.is_multiple_of(400));
    let days = match month {
        2 => 28 + u8::from(leap),
        4 | 6 | 9 | 11 => 30,
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        _ => 0,
    };
    year > 0
        && day > 0
        && day <= days
        && date
            .get(10..)
            .is_some_and(|suffix| suffix.is_empty() || suffix.starts_with(char::is_whitespace))
}

fn calendar_literals(text: &str) -> BTreeSet<String> {
    text.split_whitespace()
        .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric()))
        .filter(|word| valid_date(word))
        .map(|word| word[..10].replace('/', "-"))
        .collect()
}

fn contains_identity(text: &str, identity: &str) -> bool {
    text.match_indices(identity).any(|(start, _)| {
        text[..start]
            .chars()
            .next_back()
            .is_none_or(|c| !c.is_alphanumeric())
            && text[start + identity.len()..]
                .chars()
                .next()
                .is_none_or(|c| !c.is_alphanumeric())
    })
}

pub fn parse_question(question: &str) -> Obligations {
    let lower = question.to_lowercase();
    static STOP: OnceLock<BTreeSet<String>> = OnceLock::new();
    let stop = STOP.get_or_init(|| tokens("a an the i me my we our you your have has had do did does was were is are be been being what which who how when where why can could would should will this that these those of in on at to for from with and or not it its some any all both each many much total type most last first latest recently since ago past during month week year day time earlier earliest later order passed more before after about bit helpful tips anxious mentioned getting around spent spend take took went visit visited attend attended participated purchase purchased friend family people event m s ve re ll d one two three four five six seven eight nine ten eleven twelve"));
    let mut topics: BTreeSet<_> = tokens(question).difference(stop).cloned().collect();
    for (cue, expansion) in [
        ("aquarium", "tank fish"),
        ("game", "play gaming"),
        ("jog", "run workout"),
        ("cook", "bake recipe kitchen meal"),
        ("sport", "soccer tournament triathlon running bike racing"),
        ("getting around", "transport card tour meeting nervous"),
        ("money", "paid cost price free"),
        ("pay", "paid cost price quote corrected"),
    ] {
        if lower.contains(cue) {
            topics.extend(tokens(expansion));
        }
    }
    let identities = question
        .split_whitespace()
        .filter(|word| {
            word.chars().next().is_some_and(char::is_uppercase)
                && !word.to_lowercase().starts_with("i'")
                && !word.to_lowercase().starts_with("i’")
                && ![
                    "I", "What", "Which", "How", "Who", "Where", "When", "Can", "Do", "Did", "Is",
                ]
                .contains(word)
        })
        .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric()).to_owned())
        .collect();
    let mut literal_quotes = Vec::new();
    let mut opening = None;
    for (i, ch) in question.char_indices() {
        if let Some((start, close)) = opening {
            if ch == close {
                if start < i {
                    literal_quotes.push(question[start..i].to_owned());
                }
                opening = None;
            }
        } else if ch == '"'
            || ch == '“'
            || ch == '‘'
            || (ch == '\''
                && question[..i]
                    .chars()
                    .next_back()
                    .is_none_or(|c| !c.is_alphanumeric()))
        {
            opening = Some((
                i + ch.len_utf8(),
                match ch {
                    '“' => '”',
                    '‘' => '’',
                    _ => ch,
                },
            ));
        }
    }
    let date_literals = calendar_literals(question).into_iter().collect();
    Obligations {
        topics,
        identities,
        literal_quotes,
        date_literals,
        inference_boundaries: vec![
            "Quoted turns are observations; arithmetic, temporal resolution, absence and answer entailment require inference.".into(),
            "Source dates label observations; an event date requires support within the quoted text or explicit inference.".into(),
        ],
        quantity: lower.contains("how many")
            || lower.contains("how much")
            || lower.contains("total"),
        chronology: [
            "first", "order", "since", "ago", "most recent", "latest", "earliest", "before", "after",
            "between",
        ]
        .iter()
        .any(|word| lower.contains(word)),
        exact_quote: lower.contains("quote")
            || lower.contains("exact")
            || lower.contains("verbatim"),
    }
}

fn validate(request: &Request) -> Result<()> {
    ensure!(
        !request.question.is_empty() && request.question.len() <= 8192,
        "invalid question length"
    );
    ensure!(
        (1..=1_048_576).contains(&request.budget),
        "invalid packet budget"
    );
    ensure!(
        !request.candidates.is_empty() && request.candidates.len() <= 64,
        "expected 1..=64 candidates"
    );
    let mut ids = BTreeSet::new();
    let mut bytes = 0usize;
    for candidate in &request.candidates {
        ensure!(candidate.rank <= 64, "invalid cached rank");
        ensure!(
            (1..=4096).contains(&candidate.ordinal),
            "invalid source ordinal"
        );
        ensure!(
            !candidate.observations.is_empty() && candidate.observations.len() <= 16,
            "invalid alias count"
        );
        let first = &candidate.observations[0];
        ensure!(ids.insert(&first.0), "duplicate candidate handle");
        for (turn, session, date, role, text) in &candidate.observations {
            ensure!(
                turn.len() == 64
                    && session.len() == 64
                    && turn
                        .bytes()
                        .chain(session.bytes())
                        .all(|c| c.is_ascii_hexdigit()),
                "invalid opaque identity"
            );
            ensure!(
                turn == &first.0 && text == &first.4,
                "aliases must retain the same identity and text"
            );
            ensure!(
                role == "user" && date.len() <= 64 && valid_date(date),
                "invalid source witness"
            );
            ensure!(text.len() <= 1_048_576, "source turn exceeds limit");
            bytes = bytes
                .checked_add(text.len())
                .ok_or_else(|| anyhow::anyhow!("source size overflow"))?;
        }
    }
    ensure!(bytes <= 4_194_304, "source allocation exceeds limit");
    Ok(())
}

pub fn packet(candidates: &[Candidate], selected: &[usize]) -> Result<String> {
    let evidence = selected
        .iter()
        .map(|&i| {
            candidates
                .get(i)
                .map(|c| &c.observations)
                .ok_or_else(|| anyhow::anyhow!("source index out of range"))
        })
        .collect::<Result<Vec<_>>>()?;
    Ok(serde_json::to_string(&Packet {
        v: 2,
        q: evidence,
        i: Vec::new(),
    })?)
}

fn source_names(text: &str) -> BTreeSet<String> {
    text.split_whitespace()
        .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric()))
        .filter(|word| {
            word.chars().next().is_some_and(char::is_uppercase)
                && word.len() > 2
                && !word.contains('\'')
                && !word.contains('’')
                && ![
                    "The", "That", "This", "What", "Which", "How", "Can", "Thanks", "Speaking",
                    "With", "Anyway", "Actually",
                ]
                .contains(word)
        })
        .map(str::to_owned)
        .collect()
}

struct Facts {
    words: BTreeSet<String>,
    numbers: BTreeSet<String>,
    companion: bool,
    names: BTreeSet<String>,
    temporal: BTreeSet<String>,
    dates: BTreeSet<String>,
    factual: bool,
    completion_words: BTreeSet<String>,
    friend: bool,
}

pub struct Prepared<'a> {
    request: &'a Request,
    facts: Vec<Facts>,
    costs: Vec<usize>,
}

impl<'a> Prepared<'a> {
    pub fn new(request: &'a Request) -> Result<Self> {
        validate(request)?;
        let facts = request
            .candidates
            .iter()
            .map(|candidate| {
                let text = &candidate.observations[0].4;
                let words = tokens(text);
                let declarations: Vec<_> = text
                    .split_inclusive(['.', '!', '?', '\n'])
                    .filter_map(|sentence| {
                        let lower = sentence.to_lowercase();
                        (!sentence.ends_with('?')
                            && ["i ", "i'", "my ", "we ", "our "]
                                .iter()
                                .any(|s| lower.contains(s)))
                        .then(|| tokens(sentence))
                    })
                    .collect();
                let numbers: BTreeSet<String> = declarations
                    .iter()
                    .flat_map(|w| {
                        w.iter()
                            .filter(|s| s.chars().any(|c| c.is_ascii_digit()) || ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"].contains(&s.as_str()))
                            .cloned()
                    })
                    .collect();
                let lower = text.to_lowercase();
                let companion = [
                    "with my ",
                    "with a friend",
                    "who's a",
                    "with her",
                    "with him",
                ]
                .iter()
                .any(|s| lower.contains(s));
                let names = source_names(text);
                let temporal: BTreeSet<String> = declarations.iter().flat_map(|w| w.iter().filter(|s| ["today", "yesterday", "ago", "last", "recent", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"].contains(&s.as_str())).cloned()).collect();
                static PREDICATES: OnceLock<BTreeSet<String>> = OnceLock::new();
                let predicates = PREDICATES.get_or_init(|| tokens("attended participated completed finished went visited learned paid spent got bought purchased upgraded received own has using read made baked cooked led took quoted corrected getting earned sold booked using lived traveled spent"));
                let factual = !numbers.is_empty() || !temporal.is_empty() || declarations.iter().any(|words| !words.is_disjoint(predicates));
                let completion_words = declarations.iter().filter(|words| words.contains("complet") || words.contains("finish")).flat_map(|words| words.iter().cloned()).collect();
                let friend = lower.contains("my friend") || lower.contains("friend's") || lower.contains("friend’s");
                Facts {
                    completion_words, friend,
                    names, temporal, factual,
                    dates: calendar_literals(text),
                    words,
                    numbers,
                    companion,
                }
            })
            .collect();
        let costs = request
            .candidates
            .iter()
            .map(|c| serde_json::to_vec(&c.observations).map(|v| v.len() + 1))
            .collect::<std::result::Result<_, _>>()?;
        Ok(Self {
            request,
            facts,
            costs,
        })
    }
}

pub fn retrieve(request: &Request) -> Result<Retrieval> {
    retrieve_prepared(&Prepared::new(request)?)
}

pub fn retrieve_prepared(prepared: &Prepared<'_>) -> Result<Retrieval> {
    let request = prepared.request;
    let required = parse_question(&request.question);
    let lower = request.question.to_lowercase();
    let transport = lower.contains("getting around");
    let duration = lower.contains("hours");
    let money = lower.contains("money") || lower.contains("pay");
    let binary = required.chronology && lower.contains("first") && lower.contains(" or ");
    let active = required.quantity || required.chronology || transport;
    let acquisition = lower.contains("purchase") || lower.contains("bought");
    let completion = lower.contains("completed") || lower.contains("finished");
    let completion_subject: BTreeSet<_> = required
        .topics
        .iter()
        .filter(|s| !["complet", "finish", "read", "onlin"].contains(&s.as_str()))
        .cloned()
        .collect();

    let named: BTreeSet<_> = required
        .identities
        .iter()
        .filter(|name| name.len() >= 4 && name.as_str() != "City")
        .flat_map(|name| tokens(name))
        .collect();
    let relevant = |i: usize| {
        let facts = &prepared.facts[i];
        facts.factual
            && (!completion
                || (!facts.numbers.is_empty()
                    && !facts.completion_words.is_disjoint(&completion_subject)))
            && !facts.words.is_disjoint(&required.topics)
            && (named.is_empty() || !facts.words.is_disjoint(&named))
            && (!duration || facts.words.contains("hour") || facts.words.contains("minut"))
            && (!money
                || request.candidates[i].observations[0].4.contains('$')
                || facts.words.contains("free"))
    };
    let mut roots: BTreeMap<&str, usize> = BTreeMap::new();
    for (i, c) in request.candidates.iter().enumerate() {
        let sid = c.observations[0].1.as_str();
        let root = roots.entry(sid).or_insert(i);
        if (relevant(i) && !relevant(*root))
            || (relevant(i) == relevant(*root)
                && (c.ordinal, i) < (request.candidates[*root].ordinal, *root))
        {
            *root = i;
        }
    }
    let mut coverage = vec![BTreeSet::new(); request.candidates.len()];
    if active {
        for (&sid, &root) in &roots {
            if relevant(root) {
                coverage[root].insert(format!("premise:session:{sid}"));
                if required.chronology {
                    coverage[root].insert(format!(
                        "date:{sid}:{}",
                        request.candidates[root].observations[0].2
                    ));
                }
                if binary {
                    let tail = request
                        .candidates
                        .iter()
                        .enumerate()
                        .filter(|(_, c)| c.observations[0].1 == sid)
                        .max_by_key(|(i, c)| (c.ordinal, *i))
                        .map(|(i, _)| i)
                        .unwrap_or(root);
                    coverage[tail].insert(format!("boundary:session:{sid}"));
                }
            }
        }
        if completion {
            for (&sid, &root) in &roots {
                if relevant(root) {
                    if let Some((tail, _)) = request
                        .candidates
                        .iter()
                        .enumerate()
                        .filter(|(i, c)| c.observations[0].1 == sid && relevant(*i))
                        .max_by_key(|(i, c)| (c.ordinal, *i))
                    {
                        coverage[tail].insert(format!("confirmation:session:{sid}"));
                    }
                }
            }
        }
        for (i, witness) in coverage.iter_mut().enumerate() {
            let facts = &prepared.facts[i];
            let sid = &request.candidates[i].observations[0].1;
            let root = roots[sid.as_str()];
            if i < 5
                && (relevant(i)
                    || (relevant(root)
                        && (!facts.numbers.is_empty() || !facts.temporal.is_empty())))
            {
                if !facts.numbers.is_empty() {
                    witness.insert(format!(
                        "quantity:{}:session:{sid}",
                        facts
                            .numbers
                            .iter()
                            .map(String::as_str)
                            .collect::<Vec<_>>()
                            .join("/")
                    ));
                }
                if (facts.companion || facts.friend)
                    && (lower.contains("friend") || lower.contains("family"))
                {
                    witness.insert(format!("companion:session:{sid}"));
                    if relevant(i)
                        && (!acquisition
                            || facts.words.contains("new")
                            || facts.words.contains("bought")
                            || facts.words.contains("purchas"))
                    {
                        witness.insert(format!("premise:session:{sid}"));
                        if required.chronology {
                            witness.insert(format!(
                                "date:{sid}:{}",
                                request.candidates[i].observations[0].2
                            ));
                        }
                    }
                }
                if required.chronology && !facts.temporal.is_empty() {
                    witness.insert(format!(
                        "time:{}:session:{sid}",
                        facts
                            .temporal
                            .iter()
                            .map(String::as_str)
                            .collect::<Vec<_>>()
                            .join("/")
                    ));
                    if relevant(i)
                        && (!acquisition
                            || facts.words.contains("new")
                            || facts.words.contains("bought")
                            || facts.words.contains("purchas"))
                    {
                        witness.insert(format!("premise:session:{sid}"));
                        if required.chronology {
                            witness.insert(format!(
                                "date:{sid}:{}",
                                request.candidates[i].observations[0].2
                            ));
                        }
                    }
                }
                if lower.contains("where") || lower.contains("held") {
                    for name in &facts.names {
                        witness.insert(format!("identity:{name}:session:{sid}"));
                    }
                    if relevant(i)
                        && (!acquisition
                            || facts.words.contains("new")
                            || facts.words.contains("bought")
                            || facts.words.contains("purchas"))
                    {
                        witness.insert(format!("premise:session:{sid}"));
                        if required.chronology {
                            witness.insert(format!(
                                "date:{sid}:{}",
                                request.candidates[i].observations[0].2
                            ));
                        }
                    }
                }
            }
            if transport && (facts.words.contains("card") || facts.words.contains("nervous")) {
                witness.insert(format!("transport:session:{sid}"));
                for name in &facts.names {
                    witness.insert(format!("identity:{name}:session:{sid}"));
                }
                if facts.words.contains("nervous") {
                    witness.insert(format!("anxiety:session:{sid}"));
                }
            }
        }
    }
    for (i, witness) in coverage.iter_mut().enumerate() {
        let candidate = &request.candidates[i];
        let sid = &candidate.observations[0].1;
        let text = &candidate.observations[0].4;
        for literal in &required.literal_quotes {
            if text.contains(literal) {
                witness.insert(format!("quotation:{literal}:session:{sid}"));
            }
        }
        for identity in &required.identities {
            if !identity.is_empty() && contains_identity(text, identity) {
                witness.insert(format!("identity:{identity}:session:{sid}"));
            }
        }
        for date in &required.date_literals {
            if prepared.facts[i].dates.contains(date) {
                witness.insert(format!("literal-date:{date}:session:{sid}"));
            }
            for source in &candidate.observations {
                if source.2[..10].replace('/', "-") == *date {
                    witness.insert(format!("observation-date:{date}:session:{}", source.1));
                }
            }
        }
    }
    let mut selected = vec![0];
    let mut used = 20 + prepared.costs[0];
    if used > request.budget {
        let packet = packet(&request.candidates, &[])?;
        ensure!(
            packet.len() <= request.budget,
            "packet overhead exceeds budget"
        );
        return Ok(Retrieval {
            required,
            selected: Vec::new(),
            witnessed: BTreeMap::new(),
            packet,
        });
    }
    let mut covered = coverage[0].clone();
    loop {
        let mut best = None;
        for i in 0..coverage.len() {
            if selected.contains(&i) {
                continue;
            }
            let sid = request.candidates[i].observations[0].1.as_str();
            let root = roots[sid];
            let bundle = if active
                && root != i
                && !coverage[root].is_empty()
                && !selected.contains(&root)
                && !coverage[i].contains(&format!("premise:session:{sid}"))
            {
                vec![root, i]
            } else {
                vec![i]
            };
            let extra: BTreeSet<_> = bundle
                .iter()
                .flat_map(|&j| coverage[j].iter())
                .filter(|key| !covered.contains(*key))
                .cloned()
                .collect();
            let gain: usize = extra
                .iter()
                .map(|key| if key.starts_with("premise:") { 4 } else { 1 })
                .sum();
            if gain == 0
                || used + bundle.iter().map(|&j| prepared.costs[j]).sum::<usize>() > request.budget
            {
                continue;
            }
            let key = (
                gain,
                usize::MAX - request.candidates[i].ordinal,
                usize::MAX - i,
            );
            if best.as_ref().is_none_or(|(_, old)| key > *old) {
                best = Some((bundle, key));
            }
        }
        let Some((bundle, _)) = best else {
            break;
        };
        for i in bundle {
            selected.push(i);
            used += prepared.costs[i];
            covered.extend(coverage[i].iter().cloned());
        }
    }
    for i in 0..request.candidates.len() {
        if !selected.contains(&i) && used + prepared.costs[i] <= request.budget {
            selected.push(i);
            used += prepared.costs[i];
        }
    }
    selected.sort_unstable();
    let mut witnessed: BTreeMap<String, Vec<usize>> = BTreeMap::new();
    for &i in &selected {
        for operand in &coverage[i] {
            witnessed.entry(operand.clone()).or_default().push(i);
        }
    }
    let packet = packet(&request.candidates, &selected)?;
    ensure!(
        packet.len() <= request.budget,
        "packet overhead exceeds budget"
    );
    Ok(Retrieval {
        required,
        selected,
        witnessed,
        packet,
    })
}

pub fn knapsack_control(request: &Request) -> Result<Vec<usize>> {
    knapsack_prepared(&Prepared::new(request)?)
}

pub fn knapsack_prepared(prepared: &Prepared<'_>) -> Result<Vec<usize>> {
    let request = prepared.request;
    let costs = &prepared.costs;
    let limit = request
        .budget
        .checked_sub(b"{\"v\":1,\"evidence\":[]}".len() - 1)
        .ok_or_else(|| anyhow::anyhow!("control budget too small"))?;
    ensure!(costs[0] <= limit, "control anchor exceeds budget");
    fn gcd(mut a: u128, mut b: u128) -> u128 {
        while b != 0 {
            (a, b) = (b, a % b);
        }
        a
    }
    let mut denominator = 1u128;
    for c in &request.candidates {
        let n = c.rank as u128 + 1;
        denominator = (denominator / gcd(denominator, n))
            .checked_mul(n)
            .ok_or_else(|| anyhow::anyhow!("control rational overflow"))?;
    }
    let mut states = BTreeMap::from([(
        costs[0],
        (
            denominator / (request.candidates[0].rank as u128 + 1),
            vec![0],
        ),
    )]);
    for (i, &cost) in costs.iter().enumerate().skip(1) {
        let mut expanded = states.clone();
        for (&used, (value, chosen)) in &states {
            if used + cost > limit {
                continue;
            }
            let mut choice = chosen.clone();
            choice.push(i);
            let candidate = (
                *value + denominator / (request.candidates[i].rank as u128 + 1),
                choice,
            );
            let entry = expanded
                .entry(used + cost)
                .or_insert_with(|| candidate.clone());
            if candidate.0 > entry.0 || (candidate.0 == entry.0 && candidate.1 < entry.1) {
                *entry = candidate;
            }
        }
        let mut best = 0;
        expanded.retain(|_, (value, _)| {
            if *value > best {
                best = *value;
                true
            } else {
                false
            }
        });
        states = expanded;
    }
    let selected = states
        .into_iter()
        .min_by(|(ca, (va, sa)), (cb, (vb, sb))| vb.cmp(va).then(ca.cmp(cb)).then(sa.cmp(sb)))
        .ok_or_else(|| anyhow::anyhow!("empty control frontier"))?
        .1
         .1;
    let evidence: Vec<_> = selected
        .iter()
        .map(|&i| &request.candidates[i].observations)
        .collect();
    let encoded = serde_json::to_vec(&serde_json::json!({"v":1,"evidence":evidence}))?;
    ensure!(
        encoded.len() <= request.budget,
        "control packet exceeds budget"
    );
    Ok(selected)
}

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

    fn candidate(id: char, text: &str, ordinal: usize) -> Candidate {
        Candidate {
            observations: vec![(
                id.to_string().repeat(64),
                id.to_string().repeat(64),
                "2023/05/20".into(),
                "user".into(),
                text.into(),
            )],
            ordinal,
            rank: ordinal - 1,
        }
    }

    #[test]
    fn parses_named_literals_and_keeps_inference_explicit() {
        let obligations = parse_question(
            "What exact quote did Alice give about ‘Book Lovers Unite’ before 2024/02/29?",
        );
        assert_eq!(obligations.literal_quotes, ["Book Lovers Unite"]);
        assert_eq!(obligations.date_literals, ["2024-02-29"]);
        assert!(obligations.identities.contains(&"Alice".to_owned()));
        assert!(obligations.exact_quote && obligations.chronology);
        assert!(!obligations.inference_boundaries.is_empty());
        assert!(valid_date("2024/02/29"));
        assert!(!valid_date("2023/02/29"));
        assert!(!valid_date("2024/02/30"));
        assert!(!valid_date("2024/13/01"));
        assert!(!valid_date("2024/02/29garbage"));
        assert!(calendar_literals("2023/02/29?").is_empty());
    }

    #[test]
    fn resolves_a_quantity_through_its_source_premise() {
        let root = candidate('a', "I went to Hawaii with my family.", 1);
        let mut quantity = candidate('b', "With my family, we had to plan the 10-day trip.", 3);
        quantity.observations[0].1 = root.observations[0].1.clone();
        let candidates = vec![
            root,
            candidate('c', "I would like some trip ideas.", 1),
            quantity,
        ];
        let budget = packet(&candidates, &[0, 2]).unwrap().len();
        let result = retrieve(&Request {
            question: "How many days did I spend in Hawaii?".into(),
            budget,
            candidates,
        })
        .unwrap();
        assert_eq!(result.selected, [0, 2]);
    }

    #[test]
    fn keeps_named_event_comparisons_within_the_requested_identities() {
        let candidates = vec![
            candidate('a', "I went to Europe.", 1),
            candidate('b', "I went to Japan.", 1),
            candidate('c', "I went to Thailand.", 1),
        ];
        let budget = packet(&candidates, &[0, 2]).unwrap().len();
        let result = retrieve(&Request {
            question: "Which trip came first, Europe or Thailand?".into(),
            budget,
            candidates,
        })
        .unwrap();
        assert_eq!(result.selected, [0, 2]);
        assert!(!result.required.topics.contains("one"));
    }

    #[test]
    fn binds_a_cooking_question_to_a_dated_baking_witness() {
        let candidates = vec![
            candidate('a', "I reviewed a recipe.", 1),
            candidate('b', "I finished a cooking course last week.", 1),
            candidate(
                'c',
                "I just baked a chocolate cake for my friend's birthday today.",
                3,
            ),
        ];
        let budget = packet(&candidates, &[0, 2]).unwrap().len();
        let result = retrieve(&Request {
            question: "What did I cook for my friend two days ago?".into(),
            budget,
            candidates,
        })
        .unwrap();
        assert_eq!(result.selected, [0, 2]);
    }

    #[test]
    fn preserves_complete_quotes_at_exact_byte_boundary() {
        let candidates = vec![candidate('a', "I played a game for 5 hours. α", 1)];
        let budget = packet(&candidates, &[0]).unwrap().len();
        for delta in [-1, 0, 1] {
            let request = Request {
                question: "How many hours did I play games?".into(),
                budget: budget.checked_add_signed(delta).unwrap(),
                candidates: candidates.clone(),
            };
            let result = retrieve(&request).unwrap();
            assert_eq!(result.selected, if delta < 0 { vec![] } else { vec![0] });
            let decoded: serde_json::Value = serde_json::from_str(&result.packet).unwrap();
            assert_eq!(decoded["i"], serde_json::json!([]));
            if delta >= 0 {
                assert_eq!(decoded["q"][0][0][4], candidates[0].observations[0].4);
            }
        }
    }

    #[test]
    fn rejects_invalid_and_inconsistent_source_handles() {
        let mut request = Request {
            question: "When did I play?".into(),
            budget: 1024,
            candidates: vec![candidate('g', "I played.", 1)],
        };
        assert!(retrieve(&request).is_err());
        request.candidates[0] = candidate('a', "I played.", 1);
        let mut alias = request.candidates[0].observations[0].clone();
        alias.4 = "changed".into();
        request.candidates[0].observations.push(alias);
        assert!(retrieve(&request).is_err());
    }

    #[test]
    fn covers_complementary_quantities_instead_of_repeated_turns() {
        let candidates = vec![
            candidate('a', "I played games for 30 hours.", 1),
            candidate('b', "I played games for 30 hours.", 3),
            candidate('c', "I played games for 5 hours.", 1),
        ];
        let budget = packet(&candidates, &[0, 2]).unwrap().len();
        let result = retrieve(&Request {
            question: "How many hours have I played games in total?".into(),
            budget,
            candidates,
        })
        .unwrap();
        assert_eq!(result.selected, [0, 2]);
    }

    #[test]
    fn retrieves_literal_operands_beyond_the_cached_prefix() {
        for (question, distractor, witness) in [
            (
                "What is the exact quote “Beta release is ready.”?",
                "My note says “Beta release is wrong.”",
                "My note says “Beta release is ready.”",
            ),
            (
                "What did Alice say?",
                "My colleague Aliceann said ready.",
                "My colleague Alice said it was ready.",
            ),
            (
                "What did I say on (2024/02/29)?",
                "I wrote that the release was late.",
                "I wrote that the release was ready.",
            ),
        ] {
            let mut candidates = vec![
                candidate('a', "I kept an unrelated note.", 1),
                candidate('b', distractor, 1),
                candidate('c', witness, 3),
            ];
            candidates[2].observations[0].2 = "2024/02/29".into();
            let budget = packet(&candidates, &[0, 2]).unwrap().len();
            let result = retrieve(&Request {
                question: question.into(),
                budget,
                candidates,
            })
            .unwrap();
            assert_eq!(result.selected, [0, 2], "{question}");
            assert!(result.witnessed.values().any(|indices| indices == &[2]));
        }
    }

    #[test]
    fn separates_quoted_event_dates_from_observation_alias_dates() {
        let mut source = candidate('a', "I attended the launch on 2024/02/28.", 1);
        let mut alias = source.observations[0].clone();
        alias.1 = "b".repeat(64);
        alias.2 = "2024-02-29".into();
        source.observations.push(alias);
        let candidates = vec![source];
        let budget = packet(&candidates, &[0]).unwrap().len();
        for (date, kind, session) in [
            ("2024-02-28", "literal-date", 'a'),
            ("2024/02/29", "observation-date", 'b'),
        ] {
            let result = retrieve(&Request {
                question: format!("What did I say on {date}?"),
                budget,
                candidates: candidates.clone(),
            })
            .unwrap();
            assert_eq!(result.selected, [0]);
            assert_eq!(result.witnessed.len(), 1);
            assert!(result.witnessed.contains_key(&format!(
                "{kind}:{}:session:{}",
                date.replace('/', "-"),
                session.to_string().repeat(64),
            )));
            let decoded: serde_json::Value = serde_json::from_str(&result.packet).unwrap();
            assert_eq!(
                decoded["q"][0],
                serde_json::json!(candidates[0].observations)
            );
            assert_eq!(decoded["i"], serde_json::json!([]));
        }
    }
}