Skip to main content

aidaemon_memory/
policy.rs

1//! The automatic-memory policy of one task, compiled once from typed facts.
2//! Prompt retrieval, message-span projection and post-task learning all read
3//! this one value; none of them reinterprets the user's wording.
4
5/// Who wrote the turn's text.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum TurnAuthor {
8    /// A person wrote it.
9    Person,
10    /// No person wrote it (background continuation, runtime resume, mandate
11    /// cycle, control command).
12    Synthetic,
13}
14
15/// What automatic memory may do on one task.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum MemoryPipelinePolicy {
18    Allowed,
19    /// The request the task continues forbade user memory.
20    SuppressedByCurrentContract,
21    /// No person wrote this turn's text. Recall stays on so the agent still
22    /// knows who it works for; persistence stays off so it cannot learn from
23    /// its own synthetic text.
24    RecallOnlyNonUserTurn,
25}
26
27impl MemoryPipelinePolicy {
28    /// Compile the policy: a request that forbade user memory suppresses it;
29    /// otherwise a person's turn keeps full memory and a turn no person wrote
30    /// may only recall.
31    pub const fn compile(author: TurnAuthor, user_memory_forbidden: bool) -> Self {
32        if user_memory_forbidden {
33            Self::SuppressedByCurrentContract
34        } else if matches!(author, TurnAuthor::Person) {
35            Self::Allowed
36        } else {
37            Self::RecallOnlyNonUserTurn
38        }
39    }
40
41    /// Whether automatic memory may be written or learned from this task.
42    pub const fn allows_memory(self) -> bool {
43        matches!(self, Self::Allowed)
44    }
45
46    /// Whether stored memory may be recalled into this task's prompt.
47    pub const fn allows_recall(self) -> bool {
48        matches!(self, Self::Allowed | Self::RecallOnlyNonUserTurn)
49    }
50
51    pub const fn reason_code(self) -> &'static str {
52        match self {
53            Self::Allowed => "allowed_by_compiled_contract",
54            Self::SuppressedByCurrentContract => "inherited_contract_denies_user_memory",
55            Self::RecallOnlyNonUserTurn => "non_user_turn_recall_only",
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn a_forbidding_request_wins_and_synthetic_turns_only_recall() {
66        let person = MemoryPipelinePolicy::compile(TurnAuthor::Person, false);
67        assert!(person.allows_memory() && person.allows_recall());
68        let synthetic = MemoryPipelinePolicy::compile(TurnAuthor::Synthetic, false);
69        assert!(synthetic.allows_recall() && !synthetic.allows_memory());
70        for author in [TurnAuthor::Person, TurnAuthor::Synthetic] {
71            let denied = MemoryPipelinePolicy::compile(author, true);
72            assert!(!denied.allows_recall() && !denied.allows_memory());
73        }
74    }
75}