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
use super::*;
#[async_trait]
impl crate::traits::MessageStore for SqliteStateStore {
async fn append_message(&self, msg: &Message) -> anyhow::Result<()> {
// Canonical persistence is event-sourced (events table). Keep only an
// in-memory hot window here for low-latency context assembly.
{
let mut wm = tokio::time::timeout(
std::time::Duration::from_secs(2),
self.working_memory.write(),
)
.await
.map_err(|_| anyhow::anyhow!("append_message: working_memory write lock timed out"))?;
let deque = wm
.entry(msg.session_id.clone())
.or_insert_with(VecDeque::new);
deque.push_back(msg.clone());
// Evict old messages but ALWAYS preserve the first user message (anchor)
// This is critical for Gemini which requires tool_calls to follow user/tool messages
let mut evicted = 0;
while deque.len() > self.cap {
// Find the first user message index
let anchor_idx = deque.iter().position(|m| m.role == "user");
if anchor_idx == Some(0) && deque.len() > 1 {
// Anchor is at front - evict the second message instead
deque.remove(1);
} else {
// Safe to evict from front
deque.pop_front();
}
evicted += 1;
}
tracing::debug!(
session_id = %msg.session_id,
role = %msg.role,
msg_id = %msg.id,
deque_len = deque.len(),
cap = self.cap,
evicted,
"append_message: added to working memory"
);
}
Ok(())
}
async fn get_history(&self, session_id: &str, limit: usize) -> anyhow::Result<Vec<Message>> {
// Check working memory first
{
let wm = match tokio::time::timeout(
std::time::Duration::from_secs(2),
self.working_memory.read(),
)
.await
{
Ok(guard) => guard,
Err(_) => {
tracing::warn!(
session_id,
"get_history: working_memory read lock timed out, falling back to DB hydrate"
);
// Fall through to DB hydrate path.
return self.hydrate(session_id).await.map(|deque| {
let msgs: Vec<_> = deque.iter().cloned().collect();
crate::conversation::truncate_with_anchor(msgs, limit)
});
}
};
tracing::debug!(
session_id,
wm_sessions = wm.len(),
has_session = wm.contains_key(session_id),
"get_history: checking working memory"
);
if let Some(deque) = wm.get(session_id) {
let roles: Vec<&str> = deque.iter().map(|m| m.role.as_str()).collect();
tracing::debug!(
session_id,
deque_len = deque.len(),
roles = ?roles,
"get_history: found session in working memory"
);
if !deque.is_empty() {
let msgs: Vec<_> = deque.iter().cloned().collect();
let before_len = msgs.len();
let result = crate::conversation::truncate_with_anchor(msgs, limit);
tracing::debug!(
session_id,
before_truncate = before_len,
after_truncate = result.len(),
"get_history: returning from working memory"
);
return Ok(result);
}
}
}
// Cold start: hydrate from DB
tracing::debug!(session_id, "get_history: cold start, hydrating from DB");
let deque = self.hydrate(session_id).await?;
let msgs: Vec<_> = deque.iter().cloned().collect();
let result = crate::conversation::truncate_with_anchor(msgs, limit);
tracing::debug!(
session_id,
hydrated_count = deque.len(),
result_count = result.len(),
"get_history: hydrated from DB"
);
// Cache in working memory
match tokio::time::timeout(
std::time::Duration::from_secs(2),
self.working_memory.write(),
)
.await
{
Ok(mut wm) => {
wm.insert(session_id.to_string(), deque);
}
Err(_) => {
tracing::warn!(
session_id,
"get_history: working_memory write lock timed out, skipping cache insert"
);
}
}
Ok(result)
}
async fn get_context(
&self,
session_id: &str,
_query: &str,
limit: usize,
) -> anyhow::Result<Vec<Message>> {
// Canonical context retrieval is event-backed. The in-memory working
// window is hydrated from events on cold start by get_history().
self.get_history(session_id, limit).await
}
async fn clear_session(&self, session_id: &str) -> anyhow::Result<()> {
// Clear working memory
{
match tokio::time::timeout(
std::time::Duration::from_secs(2),
self.working_memory.write(),
)
.await
{
Ok(mut wm) => {
wm.remove(session_id);
}
Err(_) => {
tracing::warn!(
session_id,
"clear_session: working_memory write lock timed out"
);
}
}
}
// Delete session rows across canonical tables.
// Some test DBs may not have all tables yet; treat missing tables as best-effort.
for table in ["events", "conversation_summaries"] {
let query = format!("DELETE FROM {table} WHERE session_id = ?");
if let Err(e) = sqlx::query(&query)
.bind(session_id)
.execute(&self.pool)
.await
{
let missing_table = format!("no such table: {table}");
if !e.to_string().contains(&missing_table) {
return Err(e.into());
}
}
}
Ok(())
}
}