aidaemon-memory 0.26.0

Swappable long-term memory for aidaemon agents: the MemoryStore traits plus SQLite and embedding implementations
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
//! `manage_memories`: read, search, forget and re-scope what the memory
//! holds. A host that manages more than memory through the same tool (the
//! daemon's goals) presents its own combined tool and runs these actions
//! through [`ManageMemoriesTool::run`].

use std::sync::Arc;

use aidaemon_core::ChannelVisibility;
use aidaemon_tools::OutcomeTool as Tool;
use aidaemon_tools::semantics::{
    ApprovalCall, OwnerApprovalNeed, ToolCallContext, ToolCallMetadata, ToolCallOutcome,
    ToolCallSemantics, ToolCapabilities, ToolMutationEffects, ToolOutcomeStatus,
    semantics_for_exact_read_actions,
};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};

use crate::records::{Fact, FactPrivacy};
use crate::stores::MemoryStores;

/// The actions this tool runs.
pub const MEMORY_ACTIONS: &[&str] = &[
    "list",
    "health",
    "repair",
    "forget",
    "set_privacy",
    "search",
    "search_episodes",
];

/// The actions that only read.
const READ_ACTIONS: &[&str] = &["list", "health", "search", "search_episodes"];

fn compact_provenance_excerpt(text: &str) -> String {
    let single_line = text.split_whitespace().collect::<Vec<_>>().join(" ");
    crate::text::truncate_chars(&single_line, 180)
}

/// The stored facts and episodes, managed by the model: list, search,
/// forget, change who may see a fact, and check or repair the store.
pub struct ManageMemoriesTool {
    store: Arc<dyn MemoryStores>,
}

impl ManageMemoriesTool {
    pub fn new(store: Arc<dyn MemoryStores>) -> Self {
        Self { store }
    }

    /// The active fact a `set_privacy` call names, if any.
    async fn find_fact(&self, category: &str, key: &str) -> anyhow::Result<Option<Fact>> {
        Ok(self
            .store
            .get_facts(Some(category))
            .await?
            .into_iter()
            .find(|f| f.key == key && f.superseded_at.is_none()))
    }

    /// The owner card for a `set_privacy` call that widens who may see a
    /// fact; narrowing, a call naming no active fact, and every other
    /// action need none. The subject is the exact arguments approved.
    pub async fn privacy_approval(&self, arguments: &Value) -> OwnerApprovalNeed {
        let Ok(args) = serde_json::from_value::<ManageArgs>(arguments.clone()) else {
            return OwnerApprovalNeed::NotNeeded;
        };
        let (Some(category), Some(key), Some(privacy)) = (
            args.category.as_deref(),
            args.key.as_deref(),
            args.privacy.as_deref(),
        ) else {
            return OwnerApprovalNeed::NotNeeded;
        };
        if args.action != "set_privacy" {
            return OwnerApprovalNeed::NotNeeded;
        }
        let to = FactPrivacy::from_str_lossy(privacy);
        let value = match self.find_fact(category, key).await {
            Ok(Some(fact)) if fact.privacy.widens_to(to) => {
                format!(" = \"{}\"", crate::text::truncate_chars(&fact.value, 83))
            }
            Ok(_) => return OwnerApprovalNeed::NotNeeded,
            // The lookup failed: ask with what the call names rather than
            // waving it through (the run reads the store again).
            Err(_) => String::new(),
        };
        OwnerApprovalNeed::Ask(aidaemon_core::OwnerApprovalRequest::plain(format!("Make memory {to}: [{category}] {key}{value}"), json!({"action": "set_privacy", "category": category, "key": key, "privacy": to.to_string()})
                .to_string(), aidaemon_core::ApprovalRisk::High, vec![match to {
                FactPrivacy::Global => "This will make this memory visible across all channels",
                _ => "This will make this memory visible in its channel, not only in DMs",
            }
            .to_string()]))
    }
}

#[derive(Deserialize)]
struct ManageArgs {
    action: String,
    limit: Option<usize>,
    category: Option<String>,
    key: Option<String>,
    privacy: Option<String>,
    query: Option<String>,
}

const TOOL_DESCRIPTION: &str = "Memories. search=stored facts; search_episodes=generated summaries of past chats (exact messages: search_history). Store via remember_fact.";

/// The memory actions' parameters, each described. A host that offers more
/// actions on the same tool (personal goals) extends this map.
pub fn memory_parameters() -> serde_json::Map<String, Value> {
    let Value::Object(map) = json!({
        "action": { "type": "string", "enum": MEMORY_ACTIONS, "description": "forget and set_privacy name one fact by category and key." },
        "limit": { "type": "integer", "description": "Max results." },
        "category": { "type": "string", "description": "forget/set_privacy: the fact's category; list: filter." },
        "key": { "type": "string", "description": "forget/set_privacy: the fact's key." },
        "privacy": { "type": "string", "enum": ["global", "channel", "private"], "description": "set_privacy: who may see it." },
        "query": { "type": "string", "description": "search: what to find. search_episodes: a topic, or omit for the latest chats." }
    }) else {
        unreachable!("a JSON object literal")
    };
    map
}

/// The answer to an action the tool does not have, naming the ones it has.
pub fn unknown_action_message(action: &str, actions: &[&str]) -> String {
    format!("Unknown action: '{action}'. Use {}.", actions.join(", "))
}

fn manage_memories_schema() -> Value {
    json!({
        "name": "manage_memories",
        "description": TOOL_DESCRIPTION,
        "parameters": {
            "type": "object",
            "properties": memory_parameters(),
            "required": ["action"],
            "additionalProperties": false
        }
    })
}

/// Memory is persistent agent state, so writes are `configuration` and a
/// forget is also `destructive`.
fn memory_action_effects(arguments: &str) -> ToolMutationEffects {
    match action_of(arguments).as_deref() {
        Some("forget") => {
            ToolMutationEffects::CONFIGURATION.union(ToolMutationEffects::DESTRUCTIVE)
        }
        Some("set_privacy") => ToolMutationEffects::CONFIGURATION,
        _ => ToolMutationEffects::NONE,
    }
}

/// The `action` argument, if any.
fn action_of(arguments: &str) -> Option<String> {
    serde_json::from_str::<Value>(arguments)
        .ok()?
        .get("action")?
        .as_str()
        .map(str::to_string)
}

#[async_trait]
impl Tool for ManageMemoriesTool {
    fn name(&self) -> &str {
        "manage_memories"
    }

    fn description(&self) -> &str {
        TOOL_DESCRIPTION
    }

    fn schema(&self) -> Value {
        manage_memories_schema()
    }

    async fn execute(
        &self,
        arguments: &str,
        facts: &ToolCallContext,
    ) -> anyhow::Result<ToolCallOutcome> {
        let output = self.run(arguments, facts).await?;
        let trimmed = output.trim_start();
        let outcome_status = if trimmed.starts_with("Unknown action:") {
            ToolOutcomeStatus::FailedPermanent
        } else if trimmed.starts_with("No matching") {
            ToolOutcomeStatus::CompletedWithNegativeResult
        } else {
            ToolOutcomeStatus::Succeeded
        };
        Ok(ToolCallOutcome {
            output,
            metadata: ToolCallMetadata {
                outcome_status: Some(outcome_status),
                ..ToolCallMetadata::default()
            },
            finish: None,
        })
    }
}

#[async_trait]
impl aidaemon_tools::semantics::ToolSemantics for ManageMemoriesTool {
    fn dispatch_facts(&self) -> aidaemon_tools::semantics::ToolDispatchFacts {
        aidaemon_tools::semantics::ToolDispatchFacts {
            offering: aidaemon_tools::semantics::Offering::Core,
            status_from_arguments: true,
            activity: aidaemon_tools::semantics::ToolActivity::Memory,
            ..Default::default()
        }
    }

    fn call_semantics(&self, arguments: &str) -> ToolCallSemantics {
        semantics_for_exact_read_actions(arguments, READ_ACTIONS, memory_action_effects(arguments))
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities::APPROVED_HIGH_IMPACT_LOCAL
    }

    /// `set_privacy` asks the owner when it widens who may see a fact.
    async fn owner_approval_request(
        &self,
        call: &ApprovalCall<'_>,
        _facts: &ToolCallContext,
    ) -> OwnerApprovalNeed {
        self.privacy_approval(call.arguments).await
    }

    /// Reads, `repair` and `forget` touch only the agent's own memory
    /// store. `set_privacy` widens who may see a fact, so it stays
    /// undeclared.
    fn call_effects(&self, arguments: &str) -> aidaemon_core::CallEffects {
        match action_of(arguments).as_deref() {
            Some("list" | "health" | "search" | "search_episodes" | "repair" | "forget") => {
                aidaemon_core::CallEffects::none()
            }
            _ => aidaemon_core::CallEffects::unknown(),
        }
    }
}

impl ManageMemoriesTool {
    /// Run one call with the dispatcher's `facts` (session, role, channel,
    /// trust), never facts from the arguments. An action outside
    /// [`MEMORY_ACTIONS`] is answered as unknown.
    pub async fn run(&self, arguments: &str, facts: &ToolCallContext) -> anyhow::Result<String> {
        let args: ManageArgs = serde_json::from_str(arguments)?;
        let trusted_session = facts.trusted_session;
        let audience = call_audience(facts);

        match args.action.as_str() {
            "health" => {
                let report = self.store.memory_health_report().await?;
                Ok(serde_json::to_string_pretty(&report)?)
            }
            "repair" => {
                let report = self.store.repair_memory_projections().await?;
                Ok(serde_json::to_string_pretty(&report)?)
            }
            "list" => {
                let mut facts = self.store.get_canonical_memory_facts().await?;
                let canonical_owner_keys: std::collections::HashSet<String> = facts
                    .iter()
                    .filter(|fact| fact.category == "user")
                    .map(|fact| crate::text::owner_projection_family(&fact.key))
                    .collect();
                facts.extend(
                    self.store
                        .get_all_facts_with_provenance()
                        .await?
                        .into_iter()
                        .filter(|fact| {
                            let cat_lower = fact.category.trim().to_ascii_lowercase();
                            let is_owner_cat =
                                matches!(cat_lower.as_str(), "user" | "personal" | "profile")
                                    || (cat_lower == "preference"
                                        && crate::text::is_owner_profile_key(&fact.key));
                            !is_owner_cat
                                || !canonical_owner_keys
                                    .contains(&crate::text::owner_projection_family(&fact.key))
                        }),
                );
                // The same visibility rule recall applies: what this turn's
                // audience may not see is never listed into its reply.
                facts.retain(|fact| audience.may_see(Some(&fact_access(fact))));
                if facts.is_empty() {
                    return Ok("No memories stored.".to_string());
                }

                let filtered = if let Some(ref cat) = args.category {
                    facts
                        .into_iter()
                        .filter(|f| f.category == *cat)
                        .collect::<Vec<_>>()
                } else {
                    facts
                };

                let limit = args.limit.unwrap_or(100).clamp(1, 200);
                let total = filtered.len();
                let shown = filtered.into_iter().take(limit).collect::<Vec<_>>();

                let mut output = format!(
                    "**Stored Memories** (showing {} of {} facts)\n\n",
                    shown.len(),
                    total
                );
                let mut current_cat = String::new();
                for f in &shown {
                    if f.category != current_cat {
                        current_cat = f.category.clone();
                        output.push_str(&format!("### {}\n", current_cat));
                    }
                    let privacy_label = f.privacy.to_string();
                    let channel_label = f.channel_id.as_deref().unwrap_or("global");
                    let age = chrono::Utc::now().signed_duration_since(f.updated_at);
                    let age_str = if age.num_days() > 0 {
                        format!("{}d ago", age.num_days())
                    } else if age.num_hours() > 0 {
                        format!("{}h ago", age.num_hours())
                    } else {
                        "just now".to_string()
                    };
                    output.push_str(&format!(
                        "- **{}**: {} (privacy: {}, from: {}, updated: {})\n",
                        f.key, f.value, privacy_label, channel_label, age_str
                    ));
                }
                if total > shown.len() {
                    output.push_str(&format!(
                        "\n(Results truncated to {}. Provide a higher `limit` to see more.)",
                        shown.len()
                    ));
                }
                Ok(output)
            }
            "forget" => {
                let key_raw = args
                    .key
                    .as_deref()
                    .ok_or_else(|| anyhow::anyhow!("'key' is required for forget action"))?;
                let category = args
                    .category
                    .as_deref()
                    .ok_or_else(|| anyhow::anyhow!("'category' is required for forget action"))?;

                // Reject empty / whitespace-only / too-short keys. A blank key would
                // make partial key matching true for every
                // fact, wiping the store. A single character is also too broad to be
                // a safe substring match across all facts.
                let key = key_raw.trim();
                if key.is_empty() {
                    anyhow::bail!("'key' must not be empty or whitespace for forget action");
                }
                if key.chars().count() < 2 {
                    anyhow::bail!(
                        "'key' must be at least 2 characters for forget action (got {:?}); use manage_memories(action='list') to see exact keys",
                        key
                    );
                }

                // Canonicalize the requested key for fuzzy matching
                let canon = |k: &str| -> String {
                    let mut out = String::with_capacity(k.len());
                    let mut last_sep = false;
                    for ch in k.trim().chars() {
                        if ch.is_ascii_alphanumeric() {
                            out.push(ch.to_ascii_lowercase());
                            last_sep = false;
                        } else if !last_sep {
                            out.push('_');
                            last_sep = true;
                        }
                    }
                    out.trim_matches('_').to_string()
                };

                let key_canonical = canon(key);

                // Within the explicitly-requested category an exact or canonical
                // key match is authoritative. Only when none exists may a partial
                // key select facts, and then only as whole `_`-separated key
                // segments ("color" -> "favorite_color"). A fact key contained
                // in the requested key is never a match: forgetting
                // `qa_pet_name` used to also delete the owner's `name` fact.
                let exact_in_category = |f: &crate::records::Fact| -> bool {
                    f.superseded_at.is_none() && (f.key == key || canon(&f.key) == key_canonical)
                };
                let key_segments: Vec<&str> = key_canonical.split('_').collect();
                let partial_in_category = |f: &crate::records::Fact| -> bool {
                    if f.superseded_at.is_some() || key_canonical.is_empty() {
                        return false;
                    }
                    let fk_canon = canon(&f.key);
                    let fact_segments: Vec<&str> = fk_canon.split('_').collect();
                    fact_segments
                        .windows(key_segments.len())
                        .any(|window| window == key_segments.as_slice())
                };

                // Across OTHER categories (the "duplicate sweep") only accept exact
                // or canonical matches. Substring matching outside the requested
                // category is too broad and can delete unrelated facts.
                let matches_key_cross_category = |f: &crate::records::Fact| -> bool {
                    if f.superseded_at.is_some() {
                        return false;
                    }
                    f.key == key || canon(&f.key) == key_canonical
                };

                let mut to_delete: Vec<(i64, String, String)> = Vec::new();
                let mut seen_ids = std::collections::HashSet::new();

                let facts = self.store.get_facts(Some(category)).await?;
                let has_exact = facts.iter().any(&exact_in_category);
                for f in &facts {
                    let selected = if has_exact {
                        exact_in_category(f)
                    } else {
                        partial_in_category(f)
                    };
                    if selected && seen_ids.insert(f.id) {
                        to_delete.push((f.id, f.category.clone(), f.key.clone()));
                    }
                }

                // Also check OTHER categories for exact/canonical duplicates only.
                let all_facts = self.store.get_facts(None).await?;
                for f in &all_facts {
                    if f.category == category {
                        continue;
                    }
                    if matches_key_cross_category(f) && seen_ids.insert(f.id) {
                        to_delete.push((f.id, f.category.clone(), f.key.clone()));
                    }
                }

                // Also check canonical personal memory (owner profile facts,
                // aliases, relationships) so canonical-only records can be
                // forgotten via `manage_memories(action="forget")`.
                let canonical_facts = self.store.get_canonical_memory_facts().await?;
                let req_personal_key = crate::text::canonical_personal_key(key);
                let req_family = crate::text::owner_projection_family(key);
                let cat_lower = category.trim().to_ascii_lowercase();
                for f in &canonical_facts {
                    let same_cat = f.category.eq_ignore_ascii_case(&cat_lower);
                    let owner_alias_cat = f.category == "user"
                        && (matches!(cat_lower.as_str(), "user" | "personal" | "profile")
                            || (cat_lower == "preference"
                                && crate::text::is_owner_profile_key(&req_personal_key)));
                    let personal_match = (same_cat || owner_alias_cat)
                        && (f.key == key
                            || canon(&f.key) == key_canonical
                            || crate::text::canonical_personal_key(&f.key) == req_personal_key
                            || crate::text::owner_projection_family(&f.key) == req_family);
                    if personal_match && seen_ids.insert(f.id) {
                        to_delete.push((f.id, f.category.clone(), f.key.clone()));
                    }
                }

                if to_delete.is_empty() {
                    Ok(format!(
                        "No active fact found matching key '{}' in any category. Use manage_memories(action='list') to see stored facts and their exact keys.",
                        key
                    ))
                } else {
                    let mut forgotten = Vec::new();
                    for (id, cat, k) in &to_delete {
                        self.store.delete_fact(*id).await?;
                        forgotten.push(format!("[{}] {}", cat, k));
                    }
                    Ok(format!(
                        "Forgotten {} fact(s): {}",
                        forgotten.len(),
                        forgotten.join(", ")
                    ))
                }
            }
            "set_privacy" => {
                let key = args
                    .key
                    .as_deref()
                    .ok_or_else(|| anyhow::anyhow!("'key' is required for set_privacy action"))?;
                let category = args.category.as_deref().ok_or_else(|| {
                    anyhow::anyhow!("'category' is required for set_privacy action")
                })?;
                let privacy_str = args.privacy.as_deref().ok_or_else(|| {
                    anyhow::anyhow!("'privacy' is required for set_privacy action")
                })?;
                let privacy = FactPrivacy::from_str_lossy(privacy_str);

                let fact = self.find_fact(category, key).await?;

                match fact {
                    Some(f) => {
                        // The kernel asked the owner before a widening
                        // runs; one that reaches here without its
                        // judgment is refused.
                        if f.privacy.widens_to(privacy) && facts.kernel_approved.is_none() {
                            anyhow::bail!(
                                "widening a memory's privacy requires approval through the agent \
                                 kernel; it was called outside an approved agent call"
                            );
                        }
                        self.store.update_fact_privacy(f.id, privacy).await?;
                        Ok(format!(
                            "Updated privacy: [{}] {} → {}",
                            category, key, privacy
                        ))
                    }
                    None => Ok(format!("No active fact found: [{}] {}", category, key)),
                }
            }
            "search" => {
                let query = args.query.as_deref().unwrap_or("");
                if query.is_empty() {
                    return Ok("Please provide a search query.".to_string());
                }

                let limit = args.limit.unwrap_or(10).clamp(1, 200);
                let mut report = self
                    .store
                    .search_facts(query, limit, &crate::search::FactSearchConfig::default())
                    .await?;
                report
                    .hits
                    .retain(|hit| audience.may_see(Some(&fact_access(&hit.fact))));
                if report.hits.is_empty() {
                    return Ok(format!("No memories matching '{}'.", query));
                }

                let counts = format!(
                    "{} shown of {} considered",
                    report.hits.len(),
                    report.considered
                );
                let mut output = format!(
                    "══ Stored facts matching '{}' ({}) ══\n\
                     IMPORTANT: Use these EXACT values when answering — do not substitute or infer.\n\n",
                    query, counts
                );
                for hit in &report.hits {
                    let f = &hit.fact;
                    let privacy_label = f.privacy.to_string();
                    let channel_label = f.channel_id.as_deref().unwrap_or("global");
                    let marker = if hit.kind == crate::search::MatchKind::Semantic {
                        " (semantic match)"
                    } else {
                        ""
                    };
                    // Surface WHEN the fact was learned so the model can answer
                    // temporal questions ("when did I tell you X?") directly from
                    // search results instead of reaching for the wrong tool.
                    let learned_ts = f.first_seen_at.unwrap_or(f.created_at).to_rfc3339();
                    let learned = aidaemon_tools::duration_format::format_age(&learned_ts);
                    let evidence = f
                        .source_excerpt
                        .as_deref()
                        .map(compact_provenance_excerpt)
                        .filter(|excerpt| !excerpt.is_empty())
                        .map(|excerpt| {
                            format!(
                                "; evidence: {}",
                                serde_json::to_string(&excerpt)
                                    .unwrap_or_else(|_| "\"[unavailable]\"".to_string())
                            )
                        })
                        .unwrap_or_default();
                    output.push_str(&format!(
                        "• [{}] {} → \"{}\" (fact_id: {}, source: {}, privacy: {}, from: {}, learned: {}{}){}\n",
                        f.category,
                        f.key,
                        f.value,
                        f.id,
                        f.source,
                        privacy_label,
                        channel_label,
                        learned,
                        evidence,
                        marker
                    ));
                }
                if !report.related.is_empty() {
                    output.push_str("\n── Related context (connected facts) ──\n");
                    for hit in &report.related {
                        output.push_str(&format!(
                            "• [{}] {} → \"{}\"\n",
                            hit.fact.category, hit.fact.key, hit.fact.value
                        ));
                    }
                }

                Ok(output)
            }
            "search_episodes" => {
                // Active recall over episodic memory (past-conversation summaries).
                // Facts answer "what is X"; episodes answer "what did we discuss /
                // do, and when". With a query → semantic search; without one →
                // most-recent conversations (browse). This tool is owner-only (the
                // session tool roster is empty for non-owners), with explicit
                // channel/session scoping for non-private origins.
                let query = args.query.as_deref().unwrap_or("").trim();
                let limit = args.limit.unwrap_or(8).clamp(1, 50);
                let visibility = facts
                    .channel_visibility
                    .unwrap_or(ChannelVisibility::Internal);
                let episodes = match visibility {
                    ChannelVisibility::Private => {
                        self.store.get_relevant_episodes(query, limit).await?
                    }
                    ChannelVisibility::PrivateGroup | ChannelVisibility::Public => {
                        self.store
                            .get_relevant_episodes_for_channel(
                                query,
                                limit,
                                facts.channel_id.as_deref(),
                            )
                            .await?
                    }
                    ChannelVisibility::PublicExternal => {
                        return Ok(
                            "Past-conversation recall is disabled in public-external channels."
                                .to_string(),
                        );
                    }
                    ChannelVisibility::Internal if trusted_session => {
                        self.store.get_relevant_episodes(query, limit).await?
                    }
                    ChannelVisibility::Internal => {
                        let session_id = facts.session().unwrap_or("");
                        self.store
                            .get_relevant_episodes_for_session(query, limit, session_id)
                            .await?
                    }
                };
                let recalled: Vec<i64> = episodes.iter().map(|episode| episode.id).collect();
                let _ = self.store.record_memory_recall(&[], &recalled).await;
                if episodes.is_empty() {
                    return Ok(if query.is_empty() {
                        "No past conversations recorded yet.".to_string()
                    } else {
                        format!("No past conversations found matching '{}'.", query)
                    });
                }

                let header = if query.is_empty() {
                    format!(
                        "══ Generated episode summaries: recent conversations ({}) ══\n\n",
                        episodes.len()
                    )
                } else {
                    format!(
                        "══ Generated episode summaries matching '{}' ({}) ══\n\n",
                        query,
                        episodes.len()
                    )
                };
                let mut output = header;
                for e in &episodes {
                    let when =
                        aidaemon_tools::duration_format::format_age(&e.created_at.to_rfc3339());
                    output.push_str(&format!("• [{}] {}", when, e.summary.trim()));
                    if let Some(topics) = e.topics.as_ref().filter(|t| !t.is_empty()) {
                        output.push_str(&format!(" (topics: {})", topics.join(", ")));
                    }
                    if let Some(outcome) = e.outcome.as_deref().filter(|o| !o.is_empty()) {
                        output.push_str(&format!(" [outcome: {}]", outcome));
                    }
                    output.push('\n');
                }
                output.push_str(
                    "\nThese are semantic, generated summaries. Use search_history for exact \
                     retained user/assistant wording, anchored context, and message paging.\n",
                );
                Ok(output)
            }
            other => Ok(unknown_action_message(other, MEMORY_ACTIONS)),
        }
    }
}

/// Who the call's reply reaches, from the dispatcher's typed facts: the
/// owner (or a trusted scheduled session) in its own conversation sees all of
/// its memory; anyone else sees only what was learned in their channel.
fn call_audience(facts: &ToolCallContext) -> crate::types::RecallAudience {
    let visibility = facts
        .channel_visibility
        .unwrap_or(ChannelVisibility::Internal);
    let owner =
        matches!(facts.user_role, Some(aidaemon_core::UserRole::Owner)) || facts.trusted_session;
    crate::types::RecallAudience::new(visibility, facts.channel_id.clone(), owner)
}

fn fact_access(fact: &Fact) -> crate::types::FactAccess {
    crate::types::FactAccess {
        privacy: fact.privacy,
        channel_id: fact.channel_id.clone(),
    }
}

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

    #[test]
    fn provenance_excerpt_is_single_line_utf8_safe_and_bounded() {
        let input = format!("User said:\n\t{}", "🦀".repeat(250));
        let compact = compact_provenance_excerpt(&input);
        assert!(!compact.contains('\n'));
        assert!(!compact.contains('\t'));
        assert!(compact.chars().count() <= 180);
        assert!(compact.ends_with("..."));
    }

    async fn tool_with_channel_fact() -> (Arc<crate::sqlite::SqliteMemory>, ManageMemoriesTool) {
        use crate::stores::FactStore as _;
        let store = crate::full::tools::test_support::memory().await;
        store
            .upsert_fact(
                "preference",
                "report_format",
                "short summaries for Alice Rivera",
                "owner",
                Some("telegram:synthetic-channel-1"),
                FactPrivacy::Channel,
            )
            .await
            .unwrap();
        (store.clone(), ManageMemoriesTool::new(store))
    }

    #[tokio::test]
    async fn list_and_search_hide_what_the_audience_may_not_see() {
        use crate::stores::FactStore as _;
        let (store, tool) = tool_with_channel_fact().await;
        store
            .upsert_fact(
                "preference",
                "home_city",
                "Lisbon",
                "owner",
                None,
                FactPrivacy::Global,
            )
            .await
            .unwrap();
        let guest = ToolCallContext {
            channel_visibility: Some(ChannelVisibility::PrivateGroup),
            channel_id: Some("telegram:synthetic-channel-2".to_string()),
            user_role: Some(aidaemon_core::UserRole::Guest),
            ..Default::default()
        };
        let listed = tool.run(r#"{"action":"list"}"#, &guest).await.unwrap();
        assert!(!listed.contains("Lisbon"), "{listed}");
        assert!(!listed.contains("Alice Rivera"), "{listed}");
        let found = tool
            .run(r#"{"action":"search","query":"Lisbon"}"#, &guest)
            .await
            .unwrap();
        assert!(!found.contains("home_city"), "{found}");

        let owner = ToolCallContext {
            channel_visibility: Some(ChannelVisibility::Private),
            user_role: Some(aidaemon_core::UserRole::Owner),
            ..Default::default()
        };
        let listed = tool.run(r#"{"action":"list"}"#, &owner).await.unwrap();
        assert!(
            listed.contains("Lisbon") && listed.contains("Alice Rivera"),
            "{listed}"
        );
    }

    async fn privacy_of(store: &crate::sqlite::SqliteMemory) -> FactPrivacy {
        use crate::stores::FactStore as _;
        store.get_facts(Some("preference")).await.unwrap()[0].privacy
    }

    #[tokio::test]
    async fn widening_privacy_asks_with_its_exact_arguments() {
        let (_, tool) = tool_with_channel_fact().await;
        let args = json!({"action": "set_privacy", "category": "preference", "key": "report_format", "privacy": "global"});
        let OwnerApprovalNeed::Ask(request) = tool.privacy_approval(&args).await else {
            panic!("widening a fact must ask");
        };
        assert_eq!(
            request.summary,
            "Make memory global: [preference] report_format = \"short summaries for Alice Rivera\""
        );
        assert_eq!(request.risk, aidaemon_core::ApprovalRisk::High);
        let subject: Value = serde_json::from_str(&request.subject).unwrap();
        assert_eq!(subject, args);
        for narrower in [
            json!({"action": "set_privacy", "category": "preference", "key": "report_format", "privacy": "private"}),
            json!({"action": "set_privacy", "category": "preference", "key": "absent", "privacy": "global"}),
            json!({"action": "list"}),
        ] {
            assert!(matches!(
                tool.privacy_approval(&narrower).await,
                OwnerApprovalNeed::NotNeeded
            ));
        }
    }

    #[tokio::test]
    async fn widening_without_the_kernel_is_refused_and_narrowing_is_not() {
        let (store, tool) = tool_with_channel_fact().await;
        let widen = r#"{"action":"set_privacy","category":"preference","key":"report_format","privacy":"global"}"#;
        let err = tool
            .run(widen, &ToolCallContext::default())
            .await
            .unwrap_err();
        assert!(
            err.to_string()
                .contains("requires approval through the agent kernel"),
            "{err}"
        );
        assert_eq!(privacy_of(&store).await, FactPrivacy::Channel);
        tool.run(
            widen,
            &ToolCallContext {
                kernel_approved: Some(aidaemon_tools::semantics::ApprovalScope::Call),
                ..Default::default()
            },
        )
        .await
        .unwrap();
        assert_eq!(privacy_of(&store).await, FactPrivacy::Global);
        let narrow = r#"{"action":"set_privacy","category":"preference","key":"report_format","privacy":"private"}"#;
        tool.run(narrow, &ToolCallContext::default()).await.unwrap();
        assert_eq!(privacy_of(&store).await, FactPrivacy::Private);
    }

    #[tokio::test]
    async fn forget_retracts_canonical_only_owner_fact() {
        use crate::records::{
            PersonalAliasCandidate, PersonalEntityCandidate, PersonalMemoryWrite,
        };
        use crate::stores::FactStore as _;
        let store = crate::full::tools::test_support::memory().await;
        let update = PersonalMemoryWrite {
            direct_user_statement: true,
            entities: vec![PersonalEntityCandidate {
                local_id: "owner".to_string(),
                entity_type: "person".to_string(),
                canonical_name: "Owner".to_string(),
                is_reference: false,
                canonical_name_confirmed: false,
            }],
            aliases: vec![PersonalAliasCandidate {
                entity_local_id: "owner".to_string(),
                value: "Alice".to_string(),
                alias_type: "preferred_name".to_string(),
            }],
            ..Default::default()
        };
        store
            .reconcile_personal_memory(
                &update,
                "user_stated",
                Some("Call me Alice"),
                None,
                FactPrivacy::Global,
            )
            .await
            .unwrap();
        assert!(
            store
                .get_canonical_memory_facts()
                .await
                .unwrap()
                .iter()
                .any(|f| f.key == "preferred_name" && f.value == "Alice")
        );
        let tool = ManageMemoriesTool::new(store.clone());
        let owner = ToolCallContext {
            channel_visibility: Some(ChannelVisibility::Private),
            user_role: Some(aidaemon_core::UserRole::Owner),
            ..Default::default()
        };
        let out = tool
            .run(
                r#"{"action":"forget","category":"user","key":"preferred_name"}"#,
                &owner,
            )
            .await
            .unwrap();
        assert!(out.contains("Forgot"), "{out}");
        assert!(
            !store
                .get_canonical_memory_facts()
                .await
                .unwrap()
                .iter()
                .any(|f| f.key == "preferred_name" && f.value == "Alice")
        );
    }
}