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
//! Database list and retrieval operations.
use rusqlite::Result as SqliteResult;
use super::{Error, Result, map_row_to_memory};
impl super::Database {
/// List memories for a project, ordered by creation time (newest first).
///
/// # Arguments
///
/// * `project_id` - Project identifier
/// * `limit` - Maximum number of results to return
/// * `memory_types` - Optional filter for memory types (None = no filter by type)
/// * `statuses` - Optional filter for lifecycle statuses (None = default to 'active')
///
/// # Errors
///
/// Returns error if the limit is invalid or the query fails.
pub fn list(
&self,
project_id: &str,
limit: usize,
memory_types: Option<&[&str]>,
statuses: Option<&[&str]>,
) -> Result<Vec<super::Memory>> {
let mut where_clauses = vec!["project_id = ?1".to_string()];
let mut params: Vec<&dyn rusqlite::ToSql> = vec![&project_id];
let param_index = super::build_filters(
&mut where_clauses,
&mut params,
2,
statuses,
memory_types,
"",
);
let limit_param = limit as i64;
params.push(&limit_param);
let where_clause = where_clauses.join(" AND ");
let query = format!(
"SELECT id, project_id, content, metadata, embedding, created_at, updated_at, type, status, superseded_by, retrieval_count, last_retrieved_at, importance
FROM memories WHERE {} ORDER BY created_at DESC LIMIT ?{}",
where_clause, param_index
);
let mut stmt = self.conn.prepare(&query)?;
let memories: SqliteResult<Vec<super::Memory>> = stmt
.query_map(params.as_slice(), map_row_to_memory)?
.collect();
Ok(memories?)
}
/// List memories for a project created since a given timestamp.
///
/// Returns memories with `created_at > since_timestamp`, ordered by creation time (newest first).
/// The timestamp comparison is exclusive (does not include memories created exactly at the timestamp).
///
/// # Arguments
///
/// * `project_id` - Project identifier
/// * `since_timestamp` - RFC3339-formatted timestamp (exclusive lower bound)
/// * `limit` - Maximum number of results to return
/// * `memory_types` - Optional filter for memory types (None = no filter by type)
/// * `statuses` - Optional filter for lifecycle statuses (None = default to 'active')
///
/// # Errors
///
/// Returns error if:
/// - The timestamp is not valid RFC3339
/// - The limit is invalid
/// - The database query fails
///
/// # Examples
///
/// ```ignore
/// use chrono::Utc;
/// let one_hour_ago = (Utc::now() - chrono::Duration::hours(1)).to_rfc3339();
/// let recent = db.list_since("project", &one_hour_ago, 10, None, None)?;
/// ```
#[allow(dead_code)] // Public API for library consumers (e.g., kide)
pub fn list_since(
&self,
project_id: &str,
since_timestamp: &str,
limit: usize,
memory_types: Option<&[&str]>,
statuses: Option<&[&str]>,
) -> Result<Vec<super::Memory>> {
// Validate timestamp format by parsing it
let _parsed = chrono::DateTime::parse_from_rfc3339(since_timestamp)
.map_err(|e| Error::Sqlite(format!("Invalid RFC3339 timestamp: {}", e)))?;
let mut where_clauses = vec!["project_id = ?1".to_string(), "created_at > ?2".to_string()];
let mut params: Vec<&dyn rusqlite::ToSql> = vec![&project_id, &since_timestamp];
let param_index = super::build_filters(
&mut where_clauses,
&mut params,
3,
statuses,
memory_types,
"",
);
let limit_param = limit as i64;
params.push(&limit_param);
let where_clause = where_clauses.join(" AND ");
let query = format!(
"SELECT id, project_id, content, metadata, embedding, created_at, updated_at, type, status, superseded_by, retrieval_count, last_retrieved_at, importance
FROM memories WHERE {} ORDER BY created_at DESC LIMIT ?{}",
where_clause, param_index
);
let mut stmt = self.conn.prepare(&query)?;
let memories: SqliteResult<Vec<super::Memory>> = stmt
.query_map(params.as_slice(), map_row_to_memory)?
.collect();
Ok(memories?)
}
/// Get multiple memories by their IDs.
///
/// Returns results in the same order as the input IDs. Missing IDs are represented as `None`.
///
/// # Arguments
///
/// * `ids` - Slice of memory IDs to retrieve
///
/// # Returns
///
/// Vector of `Option<Memory>` with the same length as `ids`. Each position corresponds
/// to the ID at the same index in the input. `Some(memory)` if found, `None` if not found.
///
/// # Errors
///
/// Returns error if any database query fails (individual not-found cases are handled via `None`).
///
/// # Examples
///
/// ```ignore
/// let results = db.get_many(&["id1", "id2", "missing-id"])?;
/// assert_eq!(results.len(), 3);
/// assert!(results[0].is_some()); // Found id1
/// assert!(results[1].is_some()); // Found id2
/// assert!(results[2].is_none()); // Missing ID
/// ```
#[allow(dead_code)] // Public API for library consumers (e.g., kide)
pub fn get_many(&self, ids: &[&str]) -> Result<Vec<Option<super::Memory>>> {
if ids.is_empty() {
return Ok(Vec::new());
}
let placeholders = ids
.iter()
.enumerate()
.map(|(i, _)| format!("?{}", i + 1))
.collect::<Vec<_>>()
.join(", ");
let query = format!(
r#"
SELECT id, project_id, content, metadata, embedding, created_at, updated_at, type, status, superseded_by, retrieval_count, last_retrieved_at, importance
FROM memories
WHERE id IN ({})
"#,
placeholders
);
let mut stmt = self.conn.prepare(&query)?;
let params: Vec<&dyn rusqlite::ToSql> =
ids.iter().map(|id| id as &dyn rusqlite::ToSql).collect();
let rows: SqliteResult<Vec<(String, super::Memory)>> = stmt
.query_map(params.as_slice(), |row| {
let id: String = row.get(0)?;
let memory = map_row_to_memory(row)?;
Ok((id, memory))
})?
.collect();
let found_memories: std::collections::HashMap<String, super::Memory> =
rows?.into_iter().collect();
// Preserve input ordering
let results: Vec<Option<super::Memory>> = ids
.iter()
.map(|id| found_memories.get(*id).cloned())
.collect();
Ok(results)
}
/// Increment retrieval_count and set last_retrieved_at for given memory IDs.
///
/// Called by CLI handlers and MCP handlers after retrieving memories via search/get.
/// This method is NOT called internally by supersede or other operations that don't
/// represent user-initiated retrieval.
#[allow(dead_code)] // Library API: unused when MCP feature is disabled
pub fn touch_memories(&self, ids: &[&str]) -> Result<()> {
if ids.is_empty() {
return Ok(());
}
let now = chrono::Utc::now().to_rfc3339();
let placeholders: Vec<String> = (0..ids.len()).map(|i| format!("?{}", i + 2)).collect();
let sql = format!(
"UPDATE memories SET retrieval_count = retrieval_count + 1, last_retrieved_at = ?1 WHERE id IN ({})",
placeholders.join(", ")
);
let params: Vec<&dyn rusqlite::types::ToSql> =
std::iter::once(&now as &dyn rusqlite::types::ToSql)
.chain(ids.iter().map(|id| id as &dyn rusqlite::types::ToSql))
.collect();
self.conn.execute(&sql, params.as_slice())?;
Ok(())
}
}