Skip to main content

talos_memory/
types.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5/// The kind of memory item stored in the semantic/procedural layer.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum MemoryKind {
9    /// Consolidated facts, entities, claims, preferences, project knowledge.
10    Semantic,
11    /// Learned operational procedures, skills, patterns, playbooks.
12    Procedural,
13}
14
15impl std::fmt::Display for MemoryKind {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        match self {
18            MemoryKind::Semantic => write!(f, "Semantic"),
19            MemoryKind::Procedural => write!(f, "Procedural"),
20        }
21    }
22}
23
24/// The kind of entity extracted from memory content.
25#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum EntityKind {
28    File,
29    Url,
30    Code,
31    Concept,
32}
33
34/// An entity extracted from memory content.
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct Entity {
37    pub id: String,
38    pub name: String,
39    pub kind: EntityKind,
40    pub created_at: DateTime<Utc>,
41}
42
43/// A single memory item in the semantic or procedural layer.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct MemoryItem {
46    /// Unique identifier for this memory item.
47    pub id: String,
48    /// The kind of memory (semantic or procedural).
49    pub kind: MemoryKind,
50    /// A key identifying the concept or topic this memory relates to.
51    pub key: String,
52    /// The content of the memory.
53    pub content: String,
54    /// Confidence score for this memory (0.0 to 1.0).
55    pub confidence: f64,
56    /// When this memory was first created.
57    pub created_at: DateTime<Utc>,
58    /// When this memory was last reinforced (re-inserted with supporting evidence).
59    pub last_reinforced: DateTime<Utc>,
60    /// When this memory was last accessed via retrieval.
61    pub last_accessed: Option<DateTime<Utc>>,
62    /// Reference to a contradiction record if this item conflicts with another.
63    pub contradiction_ref: Option<String>,
64}
65
66/// A link from a memory item to its evidence source.
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct EvidenceLink {
69    /// Unique identifier for this evidence link.
70    pub id: String,
71    /// The memory item this evidence supports.
72    pub memory_id: String,
73    /// The type of evidence source (e.g., "session", "tool_call", "user_feedback").
74    pub source_type: String,
75    /// Reference to the specific evidence source.
76    pub source_ref: String,
77    /// When this evidence link was created.
78    pub created_at: DateTime<Utc>,
79}
80
81/// A retrieval result with scoring and provenance.
82#[derive(Debug, Clone)]
83pub struct RetrievalResult {
84    /// The retrieved memory item.
85    pub item: MemoryItem,
86    /// Evidence links supporting this memory.
87    pub evidence: Vec<EvidenceLink>,
88    /// Combined relevance score (higher is more relevant).
89    pub score: f64,
90    /// Human-readable breakdown of how the score was computed.
91    pub score_breakdown: String,
92}
93
94/// Errors that can occur during memory store operations.
95#[derive(Debug, Error)]
96pub enum MemoryStoreError {
97    /// A database operation failed.
98    #[error("database operation failed: {0}")]
99    Database(#[from] rusqlite::Error),
100    /// Failed to parse a timestamp.
101    #[error("invalid timestamp: {0}")]
102    InvalidTimestamp(String),
103    /// I/O error.
104    #[error("I/O error: {0}")]
105    Io(#[from] std::io::Error),
106    /// SQLite maintenance failed.
107    #[error("maintenance failed: {0}")]
108    Maintenance(String),
109}
110
111/// Memory store status summary (no content exposed).
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct MemoryStatus {
114    pub total_items: usize,
115    pub semantic_count: usize,
116    pub procedural_count: usize,
117    pub evidence_count: usize,
118    pub entity_count: usize,
119    pub db_path: Option<String>,
120    pub db_size_bytes: u64,
121}
122
123/// Policy for selecting memory retention candidates (dry-run only).
124#[derive(Debug, Clone, Default)]
125pub struct RetentionPolicy {
126    pub min_confidence: Option<f64>,
127    pub max_age_days: Option<i64>,
128    pub unreinforced_only: bool,
129}
130
131/// A memory item selected as a retention candidate (dry-run, no deletion).
132#[derive(Debug, Clone)]
133pub struct RetentionCandidate {
134    pub id: String,
135    pub kind: String,
136    pub key_preview: String,
137    pub confidence: f64,
138    pub last_reinforced: DateTime<Utc>,
139    pub age_days: i64,
140    pub evidence_count: usize,
141    pub reason: String,
142}