aidaemon-memory 0.21.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
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
use std::sync::Arc;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use tokio::sync::RwLock;

use aidaemon_tools::OutcomeTool as Tool;
use aidaemon_tools::semantics::{
    ToolCallSemantics, ToolCapabilities, ToolMutationEffects, ToolRole,
};

use crate::project::{MemoryScope, NoteKind, NoteSource, NoteWrite, ProjectNotes};
use crate::records::{
    FactPrivacy, PersonalAliasCandidate, PersonalEntityCandidate, PersonalFactCandidate,
    PersonalMemoryWrite,
};
use crate::stores::FactStore;
use aidaemon_core::ProjectRef;

pub struct RememberFactTool {
    state: Arc<dyn FactStore>,
    /// Current channel_id set by the agent before tool execution.
    pub current_channel_id: Arc<RwLock<Option<String>>>,
    /// Project memory: on a turn that works in a project, what the turn
    /// learns about the project is kept here instead of in `state`.
    project_notes: Option<Arc<ProjectNotes>>,
    /// The project of the one turn this instance serves, for a host whose
    /// dispatcher does not set `ToolCallContext::project`.
    turn_project: Option<ProjectRef>,
}

impl RememberFactTool {
    pub fn new(state: Arc<dyn FactStore>) -> Self {
        Self {
            state,
            current_channel_id: Arc::new(RwLock::new(None)),
            project_notes: None,
            turn_project: None,
        }
    }

    /// Keep project facts in `notes` on turns that work in a project.
    pub fn with_project_notes(mut self, notes: Arc<ProjectNotes>) -> Self {
        self.project_notes = Some(notes);
        self
    }

    /// This instance serves one turn that works in `project`.
    pub fn for_project(mut self, project: Option<ProjectRef>) -> Self {
        self.turn_project = project;
        self
    }

    /// The project the call's turn works in, when project memory is on.
    fn project(
        &self,
        facts: &aidaemon_tools::semantics::ToolCallContext,
    ) -> Option<(Arc<ProjectNotes>, ProjectRef)> {
        let notes = self.project_notes.clone()?;
        let project = facts
            .project
            .clone()
            .or_else(|| self.turn_project.clone())?;
        Some((notes, project))
    }
}

#[derive(Deserialize)]
struct RememberArgs {
    category: Option<String>,
    key: Option<String>,
    value: Option<String>,
    /// Forget the single `category`/`key` fact instead of storing a value.
    #[serde(default)]
    delete: bool,
    #[serde(default)]
    facts: Option<Vec<FactEntry>>,
    /// People and relationships moved to `manage_people` `remember`; a call
    /// that still sends them is answered with that and stores nothing.
    #[serde(default)]
    personal_memory: Option<PersonalMemoryWrite>,
    /// `project` or `personal`, for the single fact and as the default of
    /// `facts` entries without their own.
    #[serde(default)]
    scope: Option<String>,
}

/// The `scope` parameter, shared by the single fact and `facts` entries.
static SCOPE_SCHEMA: std::sync::LazyLock<Value> = std::sync::LazyLock::new(|| {
    json!({
        "type": "string",
        "enum": ["project", "personal"],
        "description": "In a project conversation: project (default) keeps it in the project's notes, personal keeps it for the owner. Facts about the owner are always personal."
    })
});

/// The `evidence` parameter, shared by the single fact and `facts` entries.
/// The learner reads it after the run: a quote found in the owner's own
/// message marks the fact as stated by the owner, anything else as derived
/// (see `TaskLearner`'s provenance stamping).
static EVIDENCE_SCHEMA: std::sync::LazyLock<Value> = std::sync::LazyLock::new(|| {
    json!({
        "type": "string",
        "description": "The owner's own words stating this fact, copied verbatim from their message. Omit when they did not state it (inferred, asked, negated, or from a tool, file or page)."
    })
});

/// The `evidence` of a `facts` entry: as [`EVIDENCE_SCHEMA`], described once.
static ENTRY_EVIDENCE_SCHEMA: std::sync::LazyLock<Value> =
    std::sync::LazyLock::new(|| json!({"type": "string"}));

/// The `scope` of a `facts` entry: as [`SCOPE_SCHEMA`], described once.
static ENTRY_SCOPE_SCHEMA: std::sync::LazyLock<Value> =
    std::sync::LazyLock::new(|| json!({"type": "string", "enum": ["project", "personal"]}));

/// How a call that still carries people is answered.
pub const PEOPLE_MOVED: &str = "remember_fact no longer stores people; nothing was stored. Send the same personal_memory to manage_people with action \"remember\", and any owner facts here without it.";

#[derive(Deserialize)]
struct FactEntry {
    category: String,
    key: String,
    #[serde(default)]
    value: String,
    #[serde(default)]
    delete: bool,
    #[serde(default)]
    scope: Option<String>,
}

/// True when this exact call would delete at least one fact. Mirrors the
/// entry selection in `call`: a non-empty `facts` batch wins, otherwise the
/// single key/value pair. Deletion is the typed `delete` flag, never a value.
pub fn call_deletes_facts(arguments: &str) -> bool {
    let Ok(args) = serde_json::from_str::<RememberArgs>(arguments) else {
        return false;
    };
    match args.facts.filter(|facts| !facts.is_empty()) {
        Some(facts) => facts.iter().any(|entry| entry.delete),
        None => args.delete && args.key.is_some_and(|key| !key.trim().is_empty()),
    }
}

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

    fn description(&self) -> &str {
        "Store or forget stable facts about the owner and their environment, not task research, generated content, goals, or schedules. Other people, family and relationships: manage_people remember."
    }

    fn schema(&self) -> Value {
        json!({
            "name": "remember_fact",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {
                    "category": {
                        "type": "string"
                    },
                    "key": {
                        "type": "string"
                    },
                    "value": {
                        "type": "string",
                        "description": "Stored as written, including \"none\"."
                    },
                    "delete": {
                        "type": "boolean",
                        "description": "Forget category/key; value is ignored."
                    },
                    "scope": SCOPE_SCHEMA.clone(),
                    "evidence": EVIDENCE_SCHEMA.clone(),
                    "facts": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "category": {
                                    "type": "string"
                                },
                                "key": {
                                    "type": "string"
                                },
                                "value": {
                                    "type": "string",
                                    "description": "Stored as written, including \"none\"."
                                },
                                "delete": {
                                    "type": "boolean",
                                    "description": "Forget this fact; value is ignored."
                                },
                                "scope": ENTRY_SCOPE_SCHEMA.clone(),
                                "evidence": ENTRY_EVIDENCE_SCHEMA.clone()
                            },
                            "required": ["category", "key"]
                        }
                    }
                },
                "additionalProperties": false
            }
        })
    }

    async fn execute(
        &self,
        arguments: &str,
        _facts: &aidaemon_tools::semantics::ToolCallContext,
    ) -> anyhow::Result<aidaemon_tools::semantics::ToolCallOutcome> {
        let output: anyhow::Result<String> = async move {
            let args: RememberArgs = serde_json::from_str(arguments)?;
            let channel_id = self.current_channel_id.read().await.clone();

            let mut results = Vec::new();

            // An empty personal_memory block (models often send every schema
            // section) carries nothing; one with people is answered with
            // where people went, and nothing in the call is stored.
            if call_carries_people(arguments) {
                return Ok(PEOPLE_MOVED.to_string());
            }
            let facts = args.facts.filter(|facts| !facts.is_empty());
            let single_key = args.key.filter(|key| !key.trim().is_empty());

            // Build the list of facts to store (batch or single)
            let entries: Vec<FactEntry> = if let Some(facts) = facts {
                facts
            } else {
                // Single-fact mode: require all three fields
                let category = args.category.ok_or_else(|| {
                    anyhow::anyhow!("'category' is required (or use 'facts' array)")
                })?;
                let key = single_key
                    .ok_or_else(|| anyhow::anyhow!("'key' is required (or use 'facts' array)"))?;
                let value = args.value.unwrap_or_default();
                vec![FactEntry {
                    category,
                    key,
                    value,
                    delete: args.delete,
                    scope: None,
                }]
            };
            // Each entry's own scope, else the call's.
            let call_scope = args.scope.clone();
            let entries: Vec<FactEntry> = entries
                .into_iter()
                .map(|mut entry| {
                    if entry.scope.is_none() {
                        entry.scope = call_scope.clone();
                    }
                    entry
                })
                .collect();

            // On a turn in a project, what belongs to the project becomes
            // its notes and never reaches the fact store, so it cannot
            // replace or merge with any of the owner's facts.
            let project = self.project(_facts);
            let mut project_indices = std::collections::BTreeSet::new();
            for (index, entry) in entries.iter().enumerate() {
                let requested = match entry.scope.as_deref() {
                    None => None,
                    Some(raw) => match MemoryScope::parse(raw) {
                        Some(scope) => Some(scope),
                        None => {
                            results.push(format!(
                                "Not stored [{}] {}: scope must be \"project\" or \"personal\"",
                                entry.category, entry.key
                            ));
                            project_indices.insert(index);
                            continue;
                        }
                    },
                };
                let in_project = project.is_some();
                if requested == Some(MemoryScope::Project) && !in_project {
                    results.push(format!(
                        "Not stored [{}] {}: project memory is not available in this conversation; leave scope out to remember it for the owner",
                        entry.category, entry.key
                    ));
                    project_indices.insert(index);
                    continue;
                }
                if crate::project::route(&entry.category, requested, in_project)
                    != MemoryScope::Project
                {
                    continue;
                }
                project_indices.insert(index);
                let Some((notes, project)) = project.clone() else {
                    continue;
                };
                results.push(project_note(notes, project, entry).await);
            }

            let mut structured = PersonalMemoryWrite {
                direct_user_statement: true,
                ..Default::default()
            };
            let mut owner_added = false;
            let mut structured_indices = std::collections::BTreeSet::new();
            for (index, entry) in entries.iter().enumerate() {
                if project_indices.contains(&index)
                    || !matches!(
                        entry.category.trim().to_ascii_lowercase().as_str(),
                        "user" | "personal" | "profile"
                    )
                {
                    continue;
                }
                let key = entry.key.trim().to_ascii_lowercase();
                let supported = matches!(
                    key.as_str(),
                    "name"
                        | "full_name"
                        | "preferred_name"
                        | "nickname"
                        | "username"
                        | "online_nickname"
                        | "online_handle"
                        | "handle"
                        | "birthday"
                        | "birth_date"
                        | "date_of_birth"
                        | "residence"
                        | "current_residence"
                        | "birthplace"
                        | "place_of_birth"
                );
                // A forget takes the delete path below, which also retracts
                // the canonical record. An empty value is reported there.
                if !supported || entry.delete || entry.value.trim().is_empty() {
                    continue;
                }
                if !owner_added {
                    structured.entities.push(PersonalEntityCandidate {
                        local_id: "owner".to_string(),
                        entity_type: "person".to_string(),
                        canonical_name: if matches!(key.as_str(), "name" | "full_name") {
                            entry.value.clone()
                        } else {
                            "Owner".to_string()
                        },
                        is_reference: false,
                        canonical_name_confirmed: matches!(key.as_str(), "name" | "full_name"),
                    });
                    owner_added = true;
                } else if matches!(key.as_str(), "name" | "full_name")
                    && let Some(owner) = structured.entities.first_mut()
                {
                    owner.canonical_name = entry.value.clone();
                    owner.canonical_name_confirmed = true;
                }
                match key.as_str() {
                    "name" | "full_name" => structured.aliases.push(PersonalAliasCandidate {
                        entity_local_id: "owner".to_string(),
                        value: entry.value.clone(),
                        alias_type: "legal_name_variant".to_string(),
                    }),
                    "preferred_name" | "nickname" => {
                        structured.aliases.push(PersonalAliasCandidate {
                            entity_local_id: "owner".to_string(),
                            value: entry.value.clone(),
                            alias_type: key,
                        })
                    }
                    "username" | "online_nickname" | "online_handle" | "handle" => {
                        structured.aliases.push(PersonalAliasCandidate {
                            entity_local_id: "owner".to_string(),
                            value: entry.value.clone(),
                            alias_type: "online_handle".to_string(),
                        })
                    }
                    _ => structured.facts.push(PersonalFactCandidate {
                        subject_local_id: "owner".to_string(),
                        predicate: key,
                        value: entry.value.clone(),
                        display_value: None,
                        valid_from: None,
                        valid_to: None,
                        retract: false,
                    }),
                }
                structured_indices.insert(index);
            }

            if !structured.entities.is_empty() {
                let result = self
                    .state
                    .reconcile_personal_memory(
                        &structured,
                        "agent",
                        None,
                        channel_id.as_deref(),
                        FactPrivacy::Private,
                    )
                    .await?;
                results.push(format!("Personal memory: {}", result.concise_summary()));
                for index in &structured_indices {
                    let entry = &entries[*index];
                    results.push(format!(
                        "Remembered: [{}] {} = {} (canonical)",
                        entry.category, entry.key, entry.value
                    ));
                }
            }

            for (index, entry) in entries.iter().enumerate() {
                if structured_indices.contains(&index) || project_indices.contains(&index) {
                    continue;
                }
                if entry.delete {
                    let deleted = self
                        .state
                        .delete_fact_by_key(&entry.category, &entry.key)
                        .await?;
                    if deleted {
                        results.push(format!("Deleted: [{}] {}", entry.category, entry.key));
                    } else {
                        results.push(format!(
                            "Not found (nothing to delete): [{}] {}",
                            entry.category, entry.key
                        ));
                    }
                    continue;
                }
                if entry.value.trim().is_empty() {
                    results.push(format!(
                        "Not stored [{}] {}: value is empty (set delete: true to forget it)",
                        entry.category, entry.key
                    ));
                    continue;
                }

                self.state
                    .upsert_fact(
                        &entry.category,
                        &entry.key,
                        &entry.value,
                        "agent",
                        channel_id.as_deref(),
                        FactPrivacy::Global,
                    )
                    .await?;
                results.push(format!(
                    "Remembered: [{}] {} = {}",
                    entry.category, entry.key, entry.value
                ));
            }

            Ok(results.join("\n"))
        }
        .await;
        let people_moved = call_carries_people(arguments);
        output.map(|output| {
            let mut outcome = aidaemon_tools::semantics::ToolCallOutcome::from_output(output);
            if people_moved {
                outcome.metadata.outcome_status =
                    Some(aidaemon_tools::semantics::ToolOutcomeStatus::FailedPermanent);
            }
            outcome
        })
    }
}

/// Writes or deletes one entry in `project`'s notes, as a result line.
async fn project_note(notes: Arc<ProjectNotes>, project: ProjectRef, entry: &FactEntry) -> String {
    let kind = crate::project::kind_for_category(&entry.category);
    let label = format!("[{}] {}", entry.category, entry.key);
    let name = project.name.clone();
    let category = entry.category.clone();
    let key = entry.key.clone();
    if entry.delete {
        let deleted = tokio::task::spawn_blocking(move || notes.delete(&project, Some(kind), &key))
            .await
            .map_err(|error| error.to_string())
            .and_then(|result| result.map_err(|error| error.to_string()));
        return match deleted {
            Ok(true) => format!("Deleted from project {name}: {label}"),
            Ok(false) => format!("Not found in project {name} (nothing to delete): {label}"),
            Err(error) => format!("Not deleted {label}: {error}"),
        };
    }
    if entry.value.trim().is_empty() {
        return format!("Not stored {label}: value is empty (set delete: true to forget it)");
    }
    let value = entry.value.clone();
    let write = NoteWrite {
        kind,
        key,
        category: Some(category),
        description: None,
        body: value.clone(),
        source: NoteSource::Agent,
    };
    let written = tokio::task::spawn_blocking(move || notes.write(&project, &write))
        .await
        .map_err(|error| error.to_string())
        .and_then(|result| result.map_err(|error| error.to_string()));
    match written {
        Ok(_) => {
            let kind = if kind == NoteKind::Fact {
                String::new()
            } else {
                format!(" ({})", kind.as_str())
            };
            format!("Remembered for project {name}{kind}: {label} = {value}")
        }
        Err(error) => format!("Not stored {label}: {error}"),
    }
}

/// Whether the call still sends people (a non-empty `personal_memory`).
fn call_carries_people(arguments: &str) -> bool {
    serde_json::from_str::<RememberArgs>(arguments)
        .ok()
        .and_then(|args| args.personal_memory)
        .is_some_and(|personal| {
            !(personal.entities.is_empty()
                && personal.aliases.is_empty()
                && personal.facts.is_empty()
                && personal.relationships.is_empty())
        })
}

impl aidaemon_tools::semantics::ToolSemantics for RememberFactTool {
    fn dispatch_facts(&self) -> aidaemon_tools::semantics::ToolDispatchFacts {
        aidaemon_tools::semantics::ToolDispatchFacts {
            offering: aidaemon_tools::semantics::Offering::Core,
            activity: aidaemon_tools::semantics::ToolActivity::Fixed(
                aidaemon_core::card::ActivityKind::Remember,
            ),
            channel_reach: aidaemon_tools::semantics::ChannelReach::Public,
            status_label: Some("updating memory"),
            ..Default::default()
        }
    }

    fn tool_role(&self) -> ToolRole {
        ToolRole::Universal
    }

    /// Memory is persistent agent state: a write is typed `configuration`
    /// and a deletion is also `destructive`, so the receipt can prove a
    /// checklist step declared with either label. An untyped receipt could
    /// not, which made the model report a deletion it had just performed as
    /// "already deleted".
    fn call_semantics(&self, arguments: &str) -> ToolCallSemantics {
        if call_deletes_facts(arguments) {
            ToolCallSemantics::mutation_with(
                ToolMutationEffects::CONFIGURATION
                    .union(ToolMutationEffects::MEMORY_WRITE)
                    .union(ToolMutationEffects::DESTRUCTIVE),
            )
        } else {
            ToolCallSemantics::mutation_with(
                ToolMutationEffects::CONFIGURATION.union(ToolMutationEffects::MEMORY_WRITE),
            )
        }
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities {
            read_only: false,
            external_side_effect: false,
            needs_approval: false,
            idempotent: true,
            high_impact_write: false,
        }
    }

    /// Only the agent's own memory store changes, so a decline earlier in
    /// the run never puts a fact to the owner.
    fn call_effects(&self, _arguments: &str) -> aidaemon_core::CallEffects {
        aidaemon_core::CallEffects::none()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::full::tools::test_support::{ToolCalls as _, memory};
    use crate::sqlite::SqliteMemory;
    use aidaemon_tools::semantics::ToolSemantics;

    async fn setup() -> (RememberFactTool, Arc<SqliteMemory>, ()) {
        let state = memory().await;
        (RememberFactTool::new(state.clone()), state, ())
    }

    #[tokio::test]
    async fn empty_personal_memory_block_does_not_drop_batch_facts() {
        let (tool, state, _db) = setup().await;
        // Shape a live model sent: every schema section filled in, with the
        // real content only in the top-level `facts` array.
        let args = json!({
            "category": "preference",
            "key": "",
            "value": "",
            "facts": [
                {"category": "personal", "key": "qa_codename", "value": "SYNTH-CODE-7"},
                {"category": "preference", "key": "favorite_test_number", "value": "1729"}
            ],
            "personal_memory": {
                "aliases": [], "entities": [], "facts": [], "relationships": [],
                "correction": false, "direct_user_statement": true
            }
        });
        let out = tool.call(&args.to_string()).await.unwrap();
        assert!(!out.contains("already current"), "{out}");
        assert!(out.contains("qa_codename = SYNTH-CODE-7"), "{out}");

        let facts = state.get_facts(None).await.unwrap();
        assert!(
            facts
                .iter()
                .any(|f| f.key == "qa_codename" && f.value == "SYNTH-CODE-7")
        );
        assert!(
            facts
                .iter()
                .any(|f| f.key == "favorite_test_number" && f.value == "1729")
        );
    }

    #[tokio::test]
    async fn empty_personal_memory_and_no_facts_is_an_error_not_a_noop() {
        let (tool, _state, _db) = setup().await;
        let args = json!({
            "key": "",
            "value": "",
            "personal_memory": {"entities": [], "aliases": [], "facts": [], "relationships": []}
        });
        assert!(tool.call(&args.to_string()).await.is_err());
    }

    /// People moved to manage_people: a call that still sends them stores
    /// nothing (not even the facts beside them) and says where they went.
    #[tokio::test]
    async fn a_call_with_people_names_manage_people_and_stores_nothing() {
        let (tool, state, _db) = setup().await;
        let args = json!({
            "facts": [{"category": "preference", "key": "editor", "value": "synthetic-editor"}],
            "personal_memory": {
                "entities": [{"local_id": "zoe", "entity_type": "person", "canonical_name": "Zoe"}]
            }
        });
        let outcome = tool
            .execute(&args.to_string(), &Default::default())
            .await
            .unwrap();
        assert!(
            outcome.output.contains("manage_people"),
            "{}",
            outcome.output
        );
        assert_eq!(
            outcome.metadata.outcome_status,
            Some(aidaemon_tools::semantics::ToolOutcomeStatus::FailedPermanent)
        );
        assert!(state.get_facts(None).await.unwrap().is_empty());
        assert!(state.get_canonical_memory_facts().await.unwrap().is_empty());
    }

    /// The owner's own profile keys still land in structured memory.
    #[tokio::test]
    async fn owner_profile_keys_still_reach_structured_memory() {
        let (tool, state, _db) = setup().await;
        let out = tool
            .call(
                &json!({"category": "user", "key": "birthday", "value": "1990-01-02"}).to_string(),
            )
            .await
            .unwrap();
        assert!(out.contains("(canonical)"), "{out}");
        assert!(
            state
                .get_canonical_memory_facts()
                .await
                .unwrap()
                .iter()
                .any(|fact| fact.category == "user" && fact.value == "1990-01-02")
        );
    }

    /// A value is stored as written: "none" is an answer, and wording such
    /// as "from now on" is not an attack. Forgetting is the typed flag.
    #[tokio::test]
    async fn values_are_stored_as_written_and_delete_is_typed() {
        let (tool, state, _db) = setup().await;
        let stored = |key: &'static str| {
            let state = state.clone();
            async move {
                state
                    .get_facts(None)
                    .await
                    .unwrap()
                    .into_iter()
                    .find(|f| f.key == key)
                    .map(|f| f.value)
            }
        };
        for (key, value) in [
            ("allergies", "none"),
            ("middle_name", "N/A"),
            ("work_schedule", "From now on I work remote Fridays"),
            ("quarter_goal", "my goal is to ship the synthetic widget"),
        ] {
            let out = tool
                .call(&json!({"category": "user", "key": key, "value": value}).to_string())
                .await
                .unwrap();
            assert!(out.contains("Remembered"), "{out}");
            assert_eq!(stored(key).await.as_deref(), Some(value));
        }

        let out = tool
            .call(&json!({"category": "user", "key": "allergies", "delete": true}).to_string())
            .await
            .unwrap();
        assert!(out.contains("Deleted"), "{out}");
        assert_eq!(stored("allergies").await, None);

        let out = tool
            .call(&json!({"category": "user", "key": "middle_name", "value": ""}).to_string())
            .await
            .unwrap();
        assert!(out.contains("Not stored"), "{out}");
        assert_eq!(stored("middle_name").await.as_deref(), Some("N/A"));
    }

    #[tokio::test]
    async fn memory_receipts_are_typed_for_checklist_credit() {
        let (tool, _state, _db) = setup().await;
        let effects = |args: Value| tool.call_semantics(&args.to_string()).mutation_effects;

        let batch_delete = effects(json!({
            "facts": [{"category": "user", "key": "qa_codename", "delete": true}],
            "key": "", "value": ""
        }));
        assert!(batch_delete.satisfies(ToolMutationEffects::DESTRUCTIVE));
        assert!(batch_delete.satisfies(ToolMutationEffects::CONFIGURATION));
        assert!(
            effects(json!({"category": "user", "key": "qa_codename", "delete": true}))
                .satisfies(ToolMutationEffects::DESTRUCTIVE)
        );
        // A value that reads like a forget is a value.
        assert!(
            !effects(json!({"category": "user", "key": "qa_codename", "value": "delete"}))
                .satisfies(ToolMutationEffects::DESTRUCTIVE)
        );

        let store = effects(json!({
            "facts": [{"category": "user", "key": "qa_codename", "value": "SYNTH-CODE-7"}],
            "key": "", "value": ""
        }));
        assert!(store.satisfies(ToolMutationEffects::CONFIGURATION));
        assert!(!store.satisfies(ToolMutationEffects::DESTRUCTIVE));
        // A plan that calls saving a fact a derived write can close, but a
        // saved fact is never a workspace file, and a config change alone is
        // not a saved fact.
        assert!(store.satisfies(ToolMutationEffects::LOCAL_DERIVED_WRITE));
        assert!(!store.satisfies(ToolMutationEffects::LOCAL_WORKSPACE_WRITE));
        assert!(
            !ToolMutationEffects::CONFIGURATION.satisfies(ToolMutationEffects::LOCAL_DERIVED_WRITE)
        );
        // An empty key names nothing to delete.
        assert!(
            !effects(json!({"key": "", "delete": true}))
                .satisfies(ToolMutationEffects::DESTRUCTIVE)
        );
    }

    mod project_scope {
        use super::*;
        use crate::project::{NoteKind, ProjectNotes};
        use aidaemon_tools::semantics::ToolCallContext;

        struct Fixture {
            tool: RememberFactTool,
            state: Arc<SqliteMemory>,
            notes: Arc<ProjectNotes>,
            project: ProjectRef,
            _dir: tempfile::TempDir,
        }

        async fn fixture() -> Fixture {
            let state = memory().await;
            let dir = tempfile::tempdir().unwrap();
            let notes = Arc::new(ProjectNotes::new(dir.path()));
            let tool = RememberFactTool::new(state.clone()).with_project_notes(notes.clone());
            let project = ProjectRef::new(
                "project-00000000-0000-4000-8000-000000000001",
                "Acme Site",
                None,
            )
            .unwrap();
            Fixture {
                tool,
                state,
                notes,
                project,
                _dir: dir,
            }
        }

        async fn call_in(
            tool: &RememberFactTool,
            project: Option<&ProjectRef>,
            args: Value,
        ) -> String {
            let facts = ToolCallContext {
                project: project.cloned(),
                ..Default::default()
            };
            tool.execute(&args.to_string(), &facts)
                .await
                .unwrap()
                .output
        }

        #[tokio::test]
        async fn a_project_fact_never_touches_personal_memory() {
            let f = fixture().await;
            for (category, key, value) in [
                ("user", "name", "Alice Rivera"),
                ("preference", "editor", "vim"),
                ("technical", "test_command", "pytest -q"),
            ] {
                f.state
                    .upsert_fact(category, key, value, "user", None, FactPrivacy::Global)
                    .await
                    .unwrap();
            }
            let before = f.state.get_facts(None).await.unwrap();
            // Same key as a personal fact, and a near-synonym of another.
            let out = call_in(
                &f.tool,
                Some(&f.project),
                json!({"facts": [
                    {"category": "technical", "key": "test_command", "value": "make test"},
                    {"category": "technical", "key": "editor_config", "value": "vim modelines are off here"}
                ]}),
            )
            .await;
            assert!(out.contains("Remembered for project Acme Site"), "{out}");
            let after = f.state.get_facts(None).await.unwrap();
            let ids = |facts: &[crate::records::Fact]| {
                let mut v: Vec<_> = facts.iter().map(|x| (x.id, x.value.clone())).collect();
                v.sort();
                v
            };
            assert_eq!(ids(&before), ids(&after));
            assert!(
                f.notes
                    .read(&f.project, NoteKind::Fact, "test_command")
                    .unwrap()
                    .contains("make test")
            );
            assert_eq!(f.notes.entries(&f.project).len(), 2);
        }

        #[tokio::test]
        async fn without_a_project_nothing_changes() {
            let f = fixture().await;
            let out = call_in(
                &f.tool,
                None,
                json!({"category": "technical", "key": "test_command", "value": "make test"}),
            )
            .await;
            assert_eq!(out, "Remembered: [technical] test_command = make test");
            assert!(f.notes.entries(&f.project).is_empty());
            let out = call_in(
                &f.tool,
                None,
                json!({"category": "technical", "key": "db", "value": "Postgres", "scope": "project"}),
            )
            .await;
            assert!(out.contains("project memory is not available"), "{out}");
            assert!(
                f.state
                    .get_facts(None)
                    .await
                    .unwrap()
                    .iter()
                    .all(|x| x.key != "db")
            );
        }

        #[tokio::test]
        async fn the_owner_stays_personal_in_a_project() {
            let f = fixture().await;
            let out = call_in(
                &f.tool,
                Some(&f.project),
                json!({"facts": [
                    {"category": "user", "key": "residence", "value": "Springfield", "scope": "project"},
                    {"category": "preference", "key": "indentation", "value": "tabs"},
                    {"category": "technical", "key": "laptop", "value": "a synthetic laptop", "scope": "personal"}
                ]}),
            )
            .await;
            assert!(out.contains("canonical"), "{out}");
            let facts = f.state.get_facts(None).await.unwrap();
            assert!(facts.iter().any(|x| x.key == "indentation"), "{facts:?}");
            assert!(facts.iter().any(|x| x.key == "laptop"), "{facts:?}");
            assert!(f.notes.entries(&f.project).is_empty());
        }

        #[tokio::test]
        async fn project_notes_are_deleted_by_key_and_bad_scopes_store_nothing() {
            let f = fixture().await;
            call_in(
                &f.tool,
                Some(&f.project),
                json!({"category": "pitfall", "key": "migrations", "value": "Run migrations before seeding."}),
            )
            .await;
            assert!(
                f.notes
                    .read(&f.project, NoteKind::Pitfall, "migrations")
                    .is_some()
            );
            let out = call_in(
                &f.tool,
                Some(&f.project),
                json!({"category": "pitfall", "key": "migrations", "delete": true}),
            )
            .await;
            assert!(out.starts_with("Deleted from project"), "{out}");
            assert!(f.notes.entries(&f.project).is_empty());
            let out = call_in(
                &f.tool,
                Some(&f.project),
                json!({"category": "technical", "key": "db", "value": "Postgres", "scope": "team"}),
            )
            .await;
            assert!(out.contains("scope must be"), "{out}");
            assert!(f.notes.entries(&f.project).is_empty());
            assert!(f.state.get_facts(None).await.unwrap().is_empty());
        }

        #[tokio::test]
        async fn a_tool_without_notes_keeps_todays_behaviour_in_a_project() {
            let state = memory().await;
            let tool = RememberFactTool::new(state.clone());
            let project = ProjectRef::new("project-x", "X", None).unwrap();
            let out = call_in(
                &tool,
                Some(&project),
                json!({"category": "technical", "key": "db", "value": "Postgres"}),
            )
            .await;
            assert_eq!(out, "Remembered: [technical] db = Postgres");
        }
    }
}