1use std::collections::{HashMap, HashSet};
11use std::sync::{Arc, Mutex};
12
13use async_trait::async_trait;
14use rap_protocol::{ChoiceId, ThreadId, ToolCallId};
15use serde::{Deserialize, Serialize};
16
17use crate::message::InfinityMessage;
18use crate::system::UserChoice;
19use crate::traits::{ConversationStore, SpawnContext, StateStore};
20
21#[derive(Debug)]
24pub struct InMemoryStoreError(pub String);
25
26impl std::fmt::Display for InMemoryStoreError {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 write!(f, "{}", self.0)
29 }
30}
31
32impl std::error::Error for InMemoryStoreError {}
33
34#[derive(Clone, Debug, Serialize, Deserialize)]
42pub struct ThreadInfo {
43 pub parent_thread_id: Option<ThreadId>,
44 pub root_thread_id: ThreadId,
45 pub spawn_message_order: Option<i64>,
47 pub spawn_tool_call_id: Option<ToolCallId>,
48 pub closed: bool,
49 pub is_subscription_event: bool,
50 #[serde(default)]
51 pub is_compaction: bool,
52 #[serde(default)]
54 pub fresh_context: bool,
55}
56
57#[derive(Clone, Debug, Serialize, Deserialize)]
60pub struct CompactionSummary {
61 pub summary: String,
62 pub up_to_order: i64,
63}
64
65#[derive(Clone, Default)]
68pub struct InMemoryConversationStore {
69 #[expect(clippy::type_complexity, reason = "shared state")]
70 messages: Arc<Mutex<HashMap<ThreadId, Vec<(InfinityMessage, String)>>>>,
71 threads: Arc<Mutex<HashMap<ThreadId, ThreadInfo>>>,
72 compaction_summaries: Arc<Mutex<HashMap<ThreadId, Vec<CompactionSummary>>>>,
73}
74
75impl InMemoryConversationStore {
76 pub fn new() -> Self {
77 Self::default()
78 }
79
80 pub fn thread_info(&self, thread_id: &ThreadId<str>) -> Option<ThreadInfo> {
90 self.threads
91 .lock()
92 .expect("bug: mutex poisoned")
93 .get(thread_id)
94 .cloned()
95 }
96
97 pub fn set_thread_info(&self, thread_id: &ThreadId<str>, info: ThreadInfo) {
99 self.threads
100 .lock()
101 .expect("bug: mutex poisoned")
102 .insert(thread_id.to_owned(), info);
103 }
104
105 pub fn thread_messages(
108 &self,
109 thread_id: &ThreadId<str>,
110 ) -> Option<Vec<(InfinityMessage, String)>> {
111 self.messages
112 .lock()
113 .expect("bug: mutex poisoned")
114 .get(thread_id)
115 .cloned()
116 }
117
118 pub fn set_thread_messages(
121 &self,
122 thread_id: &ThreadId<str>,
123 messages: Vec<(InfinityMessage, String)>,
124 ) -> bool {
125 self.messages
126 .lock()
127 .expect("bug: mutex poisoned")
128 .insert(thread_id.to_owned(), messages)
129 .is_some()
130 }
131
132 pub fn thread_compaction_summaries(&self, thread_id: &ThreadId<str>) -> Vec<CompactionSummary> {
134 self.compaction_summaries
135 .lock()
136 .expect("bug: mutex poisoned")
137 .get(thread_id)
138 .cloned()
139 .unwrap_or_default()
140 }
141
142 pub fn set_thread_compaction_summaries(
145 &self,
146 thread_id: &ThreadId<str>,
147 summaries: Vec<CompactionSummary>,
148 ) -> bool {
149 self.compaction_summaries
150 .lock()
151 .expect("bug: mutex poisoned")
152 .insert(thread_id.to_owned(), summaries)
153 .is_some()
154 }
155
156 pub fn spawn_thread_with_id(
164 &self,
165 new_thread_id: &ThreadId<str>,
166 parent_thread_id: &ThreadId<str>,
167 spawn_tool_call_id: &ToolCallId<str>,
168 is_for_subscription_event: bool,
169 context: SpawnContext,
170 ) {
171 let (spawn_message_order, fresh_context) = match context {
172 SpawnContext::InheritUpTo(order) => (order as i64, false),
173 SpawnContext::Fresh => (0, true),
174 };
175
176 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
177 let root = threads
178 .get(parent_thread_id)
179 .map(|t| t.root_thread_id.clone())
180 .unwrap_or_else(|| parent_thread_id.to_owned());
181 threads.insert(
182 new_thread_id.to_owned(),
183 ThreadInfo {
184 parent_thread_id: Some(parent_thread_id.to_owned()),
185 root_thread_id: root,
186 spawn_message_order: Some(spawn_message_order),
187 spawn_tool_call_id: Some(spawn_tool_call_id.to_owned()),
188 closed: false,
189 is_subscription_event: is_for_subscription_event,
190 is_compaction: false,
191 fresh_context,
192 },
193 );
194 }
195}
196
197#[async_trait]
198impl ConversationStore for InMemoryConversationStore {
199 type Error = InMemoryStoreError;
200
201 async fn ensure_root_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error> {
202 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
203 threads
204 .entry(thread_id.to_owned())
205 .or_insert_with(|| ThreadInfo {
206 parent_thread_id: None,
207 root_thread_id: thread_id.to_owned(),
208 spawn_message_order: None,
209 spawn_tool_call_id: None,
210 closed: false,
211 is_subscription_event: false,
212 is_compaction: false,
213 fresh_context: false,
214 });
215 Ok(())
216 }
217
218 async fn thread_exists(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
219 Ok(self
220 .threads
221 .lock()
222 .expect("bug: mutex poisoned")
223 .contains_key(thread_id))
224 }
225
226 async fn load_history_up_to(
227 &self,
228 session_id: &ThreadId<str>,
229 start_from: Option<i64>,
230 up_to: Option<i64>,
231 ) -> Result<Vec<InfinityMessage>, Self::Error> {
232 let msgs = self.messages.lock().expect("bug: mutex poisoned");
233 Ok(msgs
234 .get(session_id)
235 .map(|v| {
236 let start = start_from.unwrap_or(0) as usize;
237 let end = up_to.map(|u| u as usize).unwrap_or(v.len()).min(v.len());
238 let start = start.min(end);
239 v[start..end].iter().map(|(m, _)| m.clone()).collect()
240 })
241 .unwrap_or_default())
242 }
243
244 async fn append_messages(
245 &self,
246 session_id: &ThreadId<str>,
247 messages: Vec<(InfinityMessage, String)>,
248 ) -> Result<(), Self::Error> {
249 let mut store = self.messages.lock().expect("bug: mutex poisoned");
250 store
251 .entry(session_id.to_owned())
252 .or_default()
253 .extend(messages);
254 Ok(())
255 }
256
257 async fn spawn_thread(
258 &self,
259 parent_thread_id: &ThreadId<str>,
260 spawn_tool_call_id: &ToolCallId<str>,
261 is_for_subscription_event: bool,
262 context: SpawnContext,
263 ) -> Result<ThreadId, Self::Error> {
264 let new_id = ThreadId::from(uuid::Uuid::new_v4().to_string());
265 self.spawn_thread_with_id(
266 &new_id,
267 parent_thread_id,
268 spawn_tool_call_id,
269 is_for_subscription_event,
270 context,
271 );
272 Ok(new_id)
273 }
274
275 async fn is_thread_closed(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
276 let threads = self.threads.lock().expect("bug: mutex poisoned");
277 Ok(threads.get(thread_id).map(|t| t.closed).unwrap_or(false))
278 }
279
280 async fn close_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error> {
281 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
282 if let Some(t) = threads.get_mut(thread_id) {
283 t.closed = true;
284 }
285 Ok(())
286 }
287
288 async fn is_subscription_event_thread(
289 &self,
290 thread_id: &ThreadId<str>,
291 ) -> Result<bool, Self::Error> {
292 let threads = self.threads.lock().expect("bug: mutex poisoned");
293 Ok(threads
294 .get(thread_id)
295 .map(|t| t.is_subscription_event)
296 .unwrap_or(false))
297 }
298
299 async fn is_fresh_context_thread(
300 &self,
301 thread_id: &ThreadId<str>,
302 ) -> Result<bool, Self::Error> {
303 let threads = self.threads.lock().expect("bug: mutex poisoned");
304 Ok(threads
305 .get(thread_id)
306 .map(|t| t.fresh_context)
307 .unwrap_or(false))
308 }
309
310 async fn get_thread_parent_info(
311 &self,
312 thread_id: &ThreadId<str>,
313 ) -> Result<Option<(ThreadId, ToolCallId)>, Self::Error> {
314 let threads = self.threads.lock().expect("bug: mutex poisoned");
315 Ok(threads.get(thread_id).and_then(|t| {
316 match (&t.parent_thread_id, &t.spawn_tool_call_id) {
317 (Some(p), Some(tc)) => Some((p.clone(), tc.clone())),
318 _ => None,
319 }
320 }))
321 }
322
323 async fn get_ancestor_chain(
324 &self,
325 thread_id: &ThreadId<str>,
326 ) -> Result<Vec<(ThreadId, i64)>, Self::Error> {
327 let threads = self.threads.lock().expect("bug: mutex poisoned");
328 let mut result = Vec::new();
329 let mut current = thread_id.to_owned();
330 while let Some(info) = threads.get(¤t) {
331 let Some(parent) = info.parent_thread_id.clone() else {
332 break;
333 };
334 let order = info.spawn_message_order.unwrap_or(0);
335 result.push((parent.clone(), order));
336 current = parent;
337 }
338 result.reverse();
339 Ok(result)
340 }
341
342 async fn mark_thread_as_compaction(
343 &self,
344 thread_id: &ThreadId<str>,
345 ) -> Result<(), Self::Error> {
346 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
347 if let Some(t) = threads.get_mut(thread_id) {
348 t.is_compaction = true;
349 }
350 Ok(())
351 }
352
353 async fn is_compaction_thread(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
354 let threads = self.threads.lock().expect("bug: mutex poisoned");
355 Ok(threads
356 .get(thread_id)
357 .map(|t| t.is_compaction)
358 .unwrap_or(false))
359 }
360
361 async fn get_thread_spawn_order(
362 &self,
363 thread_id: &ThreadId<str>,
364 ) -> Result<Option<i64>, Self::Error> {
365 let threads = self.threads.lock().expect("bug: mutex poisoned");
366 Ok(threads.get(thread_id).and_then(|t| t.spawn_message_order))
367 }
368
369 async fn save_compaction_summary(
370 &self,
371 thread_id: &ThreadId<str>,
372 summary: &str,
373 up_to_order: i64,
374 ) -> Result<(), Self::Error> {
375 let mut cs = self
376 .compaction_summaries
377 .lock()
378 .expect("bug: mutex poisoned");
379 cs.entry(thread_id.to_owned())
380 .or_default()
381 .push(CompactionSummary {
382 summary: summary.to_owned(),
383 up_to_order,
384 });
385 Ok(())
386 }
387
388 async fn load_latest_compaction_summary_up_to(
389 &self,
390 thread_id: &ThreadId<str>,
391 up_to_order: Option<i64>,
392 ) -> Result<Option<(String, i64)>, Self::Error> {
393 let cs = self
394 .compaction_summaries
395 .lock()
396 .expect("bug: mutex poisoned");
397 Ok(cs.get(thread_id).and_then(|v| {
398 v.iter()
399 .rev()
400 .find(|s| up_to_order.is_none_or(|n| s.up_to_order <= n))
401 .map(|s| (s.summary.clone(), s.up_to_order))
402 }))
403 }
404}
405
406#[derive(Debug, Default, Clone, Serialize, Deserialize)]
409pub struct ThreadState {
410 #[serde(default)]
411 pub processed_message_ids: HashSet<String>,
412 #[serde(default)]
413 pub metadata: Option<serde_json::Value>,
414 #[serde(default)]
415 pub subscriptions: HashSet<ToolCallId>,
416 #[serde(default)]
417 pub pending_user_choices: Vec<UserChoice>,
418}
419
420#[derive(Clone, Default)]
423pub struct InMemoryStateStore {
424 processed_ids: Arc<Mutex<HashMap<ThreadId, HashSet<String>>>>,
425 metadata: Arc<Mutex<HashMap<ThreadId, serde_json::Value>>>,
426 subscriptions: Arc<Mutex<HashMap<ThreadId, HashSet<ToolCallId>>>>,
427 pending_user_choices: Arc<Mutex<HashMap<ThreadId, Vec<UserChoice>>>>,
428}
429
430impl InMemoryStateStore {
431 pub fn new() -> Self {
432 Self::default()
433 }
434
435 pub fn thread_state(&self, thread_id: &ThreadId<str>) -> ThreadState {
437 let processed = self.processed_ids.lock().expect("bug: mutex poisoned");
438 let metadata = self.metadata.lock().expect("bug: mutex poisoned");
439 let subscriptions = self.subscriptions.lock().expect("bug: mutex poisoned");
440 let pending_user_choices = self
441 .pending_user_choices
442 .lock()
443 .expect("bug: mutex poisoned");
444 ThreadState {
445 processed_message_ids: processed.get(thread_id).cloned().unwrap_or_default(),
446 metadata: metadata.get(thread_id).cloned(),
447 subscriptions: subscriptions.get(thread_id).cloned().unwrap_or_default(),
448 pending_user_choices: pending_user_choices
449 .get(thread_id)
450 .cloned()
451 .unwrap_or_default(),
452 }
453 }
454
455 pub fn set_thread_state(&self, thread_id: &ThreadId<str>, state: ThreadState) {
457 self.processed_ids
458 .lock()
459 .expect("bug: mutex poisoned")
460 .insert(thread_id.to_owned(), state.processed_message_ids);
461 if let Some(meta) = state.metadata {
462 self.metadata
463 .lock()
464 .expect("bug: mutex poisoned")
465 .insert(thread_id.to_owned(), meta);
466 }
467 if !state.subscriptions.is_empty() {
468 self.subscriptions
469 .lock()
470 .expect("bug: mutex poisoned")
471 .insert(thread_id.to_owned(), state.subscriptions);
472 }
473 if !state.pending_user_choices.is_empty() {
474 self.pending_user_choices
475 .lock()
476 .expect("bug: mutex poisoned")
477 .insert(thread_id.to_owned(), state.pending_user_choices);
478 }
479 }
480}
481
482#[async_trait]
483impl StateStore for InMemoryStateStore {
484 type Error = InMemoryStoreError;
485
486 async fn get_processed_ids(
487 &self,
488 thread_id: &ThreadId<str>,
489 ) -> Result<HashSet<String>, Self::Error> {
490 let store = self.processed_ids.lock().expect("bug: mutex poisoned");
491 Ok(store.get(thread_id).cloned().unwrap_or_default())
492 }
493
494 async fn add_processed_message_ids(
495 &self,
496 thread_id: &ThreadId<str>,
497 message_ids: Vec<String>,
498 ) -> Result<(), Self::Error> {
499 let mut store = self.processed_ids.lock().expect("bug: mutex poisoned");
500 store
501 .entry(thread_id.to_owned())
502 .or_default()
503 .extend(message_ids);
504 Ok(())
505 }
506
507 async fn get_metadata(
508 &self,
509 root_thread_id: &ThreadId<str>,
510 ) -> Result<Option<serde_json::Value>, Self::Error> {
511 let store = self.metadata.lock().expect("bug: mutex poisoned");
512 Ok(store.get(root_thread_id).cloned())
513 }
514
515 async fn set_metadata(
516 &self,
517 root_thread_id: &ThreadId<str>,
518 metadata: serde_json::Value,
519 ) -> Result<(), Self::Error> {
520 let mut store = self.metadata.lock().expect("bug: mutex poisoned");
521 store.insert(root_thread_id.to_owned(), metadata);
522 Ok(())
523 }
524
525 async fn get_active_subscriptions(
526 &self,
527 thread_id: &ThreadId<str>,
528 ) -> Result<Vec<ToolCallId>, Self::Error> {
529 let store = self.subscriptions.lock().expect("bug: mutex poisoned");
530 Ok(store
531 .get(thread_id)
532 .map(|s| s.iter().cloned().collect())
533 .unwrap_or_default())
534 }
535
536 async fn add_active_subscription(
537 &self,
538 thread_id: &ThreadId<str>,
539 tool_call_id: &ToolCallId<str>,
540 ) -> Result<(), Self::Error> {
541 let mut store = self.subscriptions.lock().expect("bug: mutex poisoned");
542 store
543 .entry(thread_id.to_owned())
544 .or_default()
545 .insert(tool_call_id.to_owned());
546 Ok(())
547 }
548
549 async fn remove_active_subscription(
550 &self,
551 thread_id: &ThreadId<str>,
552 tool_call_id: &ToolCallId<str>,
553 ) -> Result<(), Self::Error> {
554 let mut store = self.subscriptions.lock().expect("bug: mutex poisoned");
555 if let Some(s) = store.get_mut(thread_id) {
556 s.remove(tool_call_id);
557 }
558 Ok(())
559 }
560
561 async fn add_pending_user_choice(
562 &self,
563 thread_id: &ThreadId<str>,
564 choice: UserChoice,
565 ) -> Result<(), Self::Error> {
566 let mut store = self
567 .pending_user_choices
568 .lock()
569 .expect("bug: mutex poisoned");
570 let choices = store.entry(thread_id.to_owned()).or_default();
571 if let Some(existing) = choices.iter_mut().find(|existing| existing.id == choice.id) {
572 *existing = choice;
573 } else {
574 choices.push(choice);
575 }
576 Ok(())
577 }
578
579 async fn remove_pending_user_choice(
580 &self,
581 thread_id: &ThreadId<str>,
582 choice_id: &ChoiceId<str>,
583 ) -> Result<(), Self::Error> {
584 if let Some(choices) = self
585 .pending_user_choices
586 .lock()
587 .expect("bug: mutex poisoned")
588 .get_mut(thread_id)
589 {
590 choices.retain(|choice| choice.id != *choice_id);
591 }
592 Ok(())
593 }
594
595 async fn get_pending_user_choices(
596 &self,
597 thread_id: &ThreadId<str>,
598 ) -> Result<Vec<UserChoice>, Self::Error> {
599 Ok(self
600 .pending_user_choices
601 .lock()
602 .expect("bug: mutex poisoned")
603 .get(thread_id)
604 .cloned()
605 .unwrap_or_default())
606 }
607}
608
609#[cfg(test)]
610mod tests {
611 use super::*;
612 use crate::traits::ConversationStore;
613
614 fn user_msg(text: &str) -> InfinityMessage {
615 InfinityMessage::User {
616 content: infinity_provider_protocol::message::UserContent::Text(
617 infinity_provider_protocol::message::Text {
618 text: text.to_owned(),
619 },
620 ),
621 }
622 }
623
624 fn texts(history: &[InfinityMessage]) -> Vec<String> {
625 history
626 .iter()
627 .filter_map(|m| match m {
628 InfinityMessage::User {
629 content: infinity_provider_protocol::message::UserContent::Text(t),
630 } => Some(t.text.clone()),
631 _ => None,
632 })
633 .collect()
634 }
635
636 #[tokio::test]
637 async fn fresh_spawn_inherits_no_history() {
638 let store = InMemoryConversationStore::new();
639 store
640 .ensure_root_thread(ThreadId::from_ref("root"))
641 .await
642 .expect("root");
643 store
644 .append_messages(
645 ThreadId::from_ref("root"),
646 vec![
647 (user_msg("root one"), "m1".to_owned()),
648 (user_msg("root two"), "m2".to_owned()),
649 ],
650 )
651 .await
652 .expect("append");
653
654 let child = store
655 .spawn_thread(
656 ThreadId::from_ref("root"),
657 ToolCallId::from_ref("tc-fresh"),
658 false,
659 SpawnContext::Fresh,
660 )
661 .await
662 .expect("spawn fresh child");
663 store
664 .append_messages(&child, vec![(user_msg("child own"), "c1".to_owned())])
665 .await
666 .expect("append child");
667
668 let (history, _, ancestor_prefix_len) = store
669 .load_history_with_ancestors(&child)
670 .await
671 .expect("load history");
672 assert_eq!(ancestor_prefix_len, 0, "fresh child has no ancestor prefix");
673 assert_eq!(texts(&history), vec!["child own"]);
674 }
675
676 #[tokio::test]
677 async fn fresh_boundary_blocks_grandparent_context_for_descendants() {
678 let store = InMemoryConversationStore::new();
679 store
680 .ensure_root_thread(ThreadId::from_ref("root"))
681 .await
682 .expect("root");
683 store
684 .append_messages(
685 ThreadId::from_ref("root"),
686 vec![(user_msg("root secret"), "m1".to_owned())],
687 )
688 .await
689 .expect("append");
690
691 let fresh = store
693 .spawn_thread(
694 ThreadId::from_ref("root"),
695 ToolCallId::from_ref("tc-fresh"),
696 false,
697 SpawnContext::Fresh,
698 )
699 .await
700 .expect("spawn fresh child");
701 store
702 .append_messages(&fresh, vec![(user_msg("fresh work"), "f1".to_owned())])
703 .await
704 .expect("append fresh");
705
706 let grandchild = store
707 .spawn_thread(
708 &fresh,
709 ToolCallId::from_ref("tc-inherit"),
710 false,
711 SpawnContext::InheritUpTo(1),
712 )
713 .await
714 .expect("spawn grandchild");
715 store
716 .append_messages(&grandchild, vec![(user_msg("gc work"), "g1".to_owned())])
717 .await
718 .expect("append grandchild");
719
720 let (history, _, ancestor_prefix_len) = store
721 .load_history_with_ancestors(&grandchild)
722 .await
723 .expect("load history");
724 assert_eq!(
725 texts(&history),
726 vec!["fresh work", "gc work"],
727 "grandchild sees the fresh thread's context but nothing above the fresh boundary"
728 );
729 assert_eq!(ancestor_prefix_len, 1);
730 }
731
732 #[tokio::test]
733 async fn inherit_spawn_still_sees_full_ancestor_history() {
734 let store = InMemoryConversationStore::new();
735 store
736 .ensure_root_thread(ThreadId::from_ref("root"))
737 .await
738 .expect("root");
739 store
740 .append_messages(
741 ThreadId::from_ref("root"),
742 vec![(user_msg("root one"), "m1".to_owned())],
743 )
744 .await
745 .expect("append");
746
747 let child = store
748 .spawn_thread(
749 ThreadId::from_ref("root"),
750 ToolCallId::from_ref("tc-1"),
751 false,
752 SpawnContext::InheritUpTo(1),
753 )
754 .await
755 .expect("spawn child");
756 store
757 .append_messages(&child, vec![(user_msg("child own"), "c1".to_owned())])
758 .await
759 .expect("append child");
760
761 let (history, _, ancestor_prefix_len) = store
762 .load_history_with_ancestors(&child)
763 .await
764 .expect("load history");
765 assert_eq!(texts(&history), vec!["root one", "child own"]);
766 assert_eq!(ancestor_prefix_len, 1);
767 }
768}