avocado-core 2.2.0

Core engine for AvocadoDB - deterministic context compilation for AI agents
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
//! High-level session management API
//!
//! This module provides session management APIs for conversation handling:
//!
//! - `SessionManager` - Uses `Database` directly (existing API, backward compatible)
//! - `SessionManagerGeneric<B>` - Uses `StorageBackend` trait (works with any backend)
//!
//! # Features
//!
//! - Create and manage conversation sessions
//! - Add user and assistant messages
//! - Automatic context compilation for user queries
//! - Format conversation history for LLM consumption
//! - Debug and replay conversations

use crate::compiler;
use crate::db::Database;
use crate::index::VectorIndex;
use crate::storage::StorageBackend;
use crate::types::{
    CompilerConfig, Message, MessageRole, Result, Session, WorkingSet,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// High-level session management
pub struct SessionManager {
    db: Database,
}

impl SessionManager {
    /// Create a new SessionManager
    ///
    /// # Arguments
    ///
    /// * `db` - Database instance
    ///
    /// # Returns
    ///
    /// A new SessionManager instance
    pub fn new(db: Database) -> Self {
        Self { db }
    }

    /// Start a new session
    ///
    /// # Arguments
    ///
    /// * `user_id` - Optional user identifier
    ///
    /// # Returns
    ///
    /// The newly created session
    pub fn start_session(&self, user_id: Option<&str>) -> Result<Session> {
        self.db.create_session(user_id, None)
    }

    /// Add a user message and compile context
    ///
    /// This method:
    /// 1. Adds the user message to the database
    /// 2. Calls the compiler to generate a WorkingSet from the query
    /// 3. Associates the WorkingSet with the session
    /// 4. Returns both the Message and WorkingSet
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `query` - The user's query
    /// * `config` - Compiler configuration
    /// * `index` - Vector index for search
    /// * `api_key` - Optional OpenAI API key (for embeddings)
    ///
    /// # Returns
    ///
    /// Tuple of (Message, WorkingSet)
    pub async fn add_user_message(
        &self,
        session_id: &str,
        query: &str,
        config: CompilerConfig,
        index: &VectorIndex,
        api_key: Option<&str>,
    ) -> Result<(Message, WorkingSet)> {
        // Add the message to the database
        let message = self
            .db
            .add_message(session_id, MessageRole::User, query, None)?;

        // Compile the context
        let working_set = compiler::compile(query, config.clone(), &self.db, index, api_key).await?;

        // Associate the working set with the session
        self.db.associate_working_set(
            session_id,
            Some(&message.id),
            &working_set,
            query,
            &config,
        )?;

        Ok((message, working_set))
    }

    /// Add an assistant response
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `content` - The assistant's response
    /// * `metadata` - Optional metadata (e.g., model info, citations)
    ///
    /// # Returns
    ///
    /// The newly created message
    pub fn add_assistant_message(
        &self,
        session_id: &str,
        content: &str,
        metadata: Option<&serde_json::Value>,
    ) -> Result<Message> {
        self.db
            .add_message(session_id, MessageRole::Assistant, content, metadata)
    }

    /// Get conversation history formatted for LLM consumption
    ///
    /// Formats messages as:
    /// ```text
    /// User: <message>
    ///
    /// Assistant: <message>
    ///
    /// User: <message>
    /// ...
    /// ```
    ///
    /// If `max_tokens` is specified, older messages are truncated to stay within
    /// the token budget. Most recent messages are always kept (they're most relevant).
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `max_tokens` - Optional token limit
    ///
    /// # Returns
    ///
    /// Formatted conversation history as a string
    pub fn get_conversation_history(
        &self,
        session_id: &str,
        max_tokens: Option<usize>,
    ) -> Result<String> {
        let messages = self.db.get_messages(session_id, None)?;

        if messages.is_empty() {
            return Ok(String::new());
        }

        // Format all messages first
        let formatted_messages: Vec<String> = messages
            .iter()
            .map(|msg| {
                let role = match msg.role {
                    MessageRole::User => "User",
                    MessageRole::Assistant => "Assistant",
                    MessageRole::System => "System",
                    MessageRole::Tool => "Tool",
                };
                format!("{}: {}", role, msg.content)
            })
            .collect();

        // If no token limit, return all messages
        if max_tokens.is_none() {
            return Ok(formatted_messages.join("\n\n"));
        }

        let max_tokens = max_tokens.unwrap();

        // Apply token limiting - keep most recent messages
        // Token counting: simple approximation (chars / 4)
        let mut selected_messages = Vec::new();
        let mut total_tokens = 0;

        // Iterate from most recent to oldest
        for msg in formatted_messages.iter().rev() {
            let msg_tokens = estimate_tokens(msg);

            if total_tokens + msg_tokens <= max_tokens {
                selected_messages.push(msg.clone());
                total_tokens += msg_tokens;
            } else {
                // Can't fit any more messages
                break;
            }
        }

        // Reverse to restore chronological order
        selected_messages.reverse();

        Ok(selected_messages.join("\n\n"))
    }

    /// Replay a session for debugging
    ///
    /// Groups messages into conversation turns (user + assistant pairs)
    /// and includes associated working sets for analysis.
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    ///
    /// # Returns
    ///
    /// SessionReplay with structured debug data
    pub fn replay_session(&self, session_id: &str) -> Result<SessionReplay> {
        let session_data = self.db.get_session_full(session_id)?;

        if session_data.is_none() {
            return Err(crate::types::Error::NotFound(format!(
                "Session not found: {}",
                session_id
            )));
        }

        let session_data = session_data.unwrap();
        let session = session_data.session;
        let messages = session_data.messages;
        let working_sets = session_data.working_sets;

        // Build a map of message_id -> working_set for quick lookup
        let mut working_set_map = std::collections::HashMap::new();
        for ws in working_sets {
            if let Some(msg_id) = &ws.message_id {
                working_set_map.insert(msg_id.clone(), ws.working_set);
            }
        }

        // Group messages into turns
        let mut turns = Vec::new();
        let mut i = 0;

        while i < messages.len() {
            let msg = &messages[i];

            // Only create turns for user messages
            if matches!(msg.role, MessageRole::User) {
                let user_message = msg.clone();
                let working_set = working_set_map.get(&user_message.id).cloned();

                // Look for the next assistant message (if any)
                let assistant_message = if i + 1 < messages.len()
                    && matches!(messages[i + 1].role, MessageRole::Assistant)
                {
                    i += 1; // Skip the assistant message in the next iteration
                    Some(messages[i].clone())
                } else {
                    None
                };

                turns.push(SessionTurn {
                    user_message,
                    working_set,
                    assistant_message,
                });
            }

            i += 1;
        }

        Ok(SessionReplay { session, turns })
    }
}

// ============================================================================
// Generic Session Manager (backend-agnostic)
// ============================================================================

/// Backend-agnostic session manager
///
/// This is the generic version of `SessionManager` that works with any
/// `StorageBackend` implementation (SQLite, PostgreSQL, etc.)
///
/// # Example
///
/// ```ignore
/// use avocado_core::storage::SqliteBackend;
/// use avocado_core::session::SessionManagerGeneric;
///
/// let backend = SqliteBackend::new("db.sqlite").await?;
/// let manager = SessionManagerGeneric::new(backend);
/// let session = manager.start_session(None).await?;
/// ```
pub struct SessionManagerGeneric<B: StorageBackend> {
    backend: Arc<B>,
}

impl<B: StorageBackend> SessionManagerGeneric<B> {
    /// Create a new SessionManagerGeneric
    ///
    /// # Arguments
    ///
    /// * `backend` - Storage backend implementation
    ///
    /// # Returns
    ///
    /// A new SessionManagerGeneric instance
    pub fn new(backend: B) -> Self {
        Self {
            backend: Arc::new(backend),
        }
    }

    /// Create from an Arc'd backend (for sharing)
    pub fn from_arc(backend: Arc<B>) -> Self {
        Self { backend }
    }

    /// Get a reference to the backend
    pub fn backend(&self) -> &B {
        &self.backend
    }

    /// Start a new session
    ///
    /// # Arguments
    ///
    /// * `user_id` - Optional user identifier
    ///
    /// # Returns
    ///
    /// The newly created session
    pub async fn start_session(&self, user_id: Option<&str>) -> Result<Session> {
        self.backend.create_session(user_id, None).await
    }

    /// Add a user message and compile context
    ///
    /// This method:
    /// 1. Adds the user message to the database
    /// 2. Calls the compiler to generate a WorkingSet from the query
    /// 3. Associates the WorkingSet with the session
    /// 4. Returns both the Message and WorkingSet
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `query` - The user's query
    /// * `config` - Compiler configuration
    /// * `api_key` - Optional OpenAI API key (for embeddings)
    ///
    /// # Returns
    ///
    /// Tuple of (Message, WorkingSet)
    pub async fn add_user_message(
        &self,
        session_id: &str,
        query: &str,
        config: CompilerConfig,
        api_key: Option<&str>,
    ) -> Result<(Message, WorkingSet)> {
        // Add the message to the database
        let message = self
            .backend
            .add_message(session_id, MessageRole::User, query, None)
            .await?;

        // Compile the context using the backend
        let working_set = compiler::compile_with_backend(
            query,
            config.clone(),
            self.backend.as_ref(),
            api_key,
        )
        .await?;

        // Associate the working set with the session
        self.backend
            .associate_working_set(session_id, Some(&message.id), &working_set, query, &config)
            .await?;

        Ok((message, working_set))
    }

    /// Add a user message with explain option
    pub async fn add_user_message_with_explain(
        &self,
        session_id: &str,
        query: &str,
        config: CompilerConfig,
        api_key: Option<&str>,
        explain: bool,
    ) -> Result<(Message, WorkingSet)> {
        let message = self
            .backend
            .add_message(session_id, MessageRole::User, query, None)
            .await?;

        let working_set = compiler::compile_with_backend_options(
            query,
            config.clone(),
            self.backend.as_ref(),
            api_key,
            explain,
        )
        .await?;

        self.backend
            .associate_working_set(session_id, Some(&message.id), &working_set, query, &config)
            .await?;

        Ok((message, working_set))
    }

    /// Add an assistant response
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `content` - The assistant's response
    /// * `metadata` - Optional metadata (e.g., model info, citations)
    ///
    /// # Returns
    ///
    /// The newly created message
    pub async fn add_assistant_message(
        &self,
        session_id: &str,
        content: &str,
        metadata: Option<&serde_json::Value>,
    ) -> Result<Message> {
        self.backend
            .add_message(session_id, MessageRole::Assistant, content, metadata)
            .await
    }

    /// Get conversation history formatted for LLM consumption
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    /// * `max_tokens` - Optional token limit
    ///
    /// # Returns
    ///
    /// Formatted conversation history as a string
    pub async fn get_conversation_history(
        &self,
        session_id: &str,
        max_tokens: Option<usize>,
    ) -> Result<String> {
        let messages = self.backend.get_messages(session_id, None).await?;

        if messages.is_empty() {
            return Ok(String::new());
        }

        // Format all messages
        let formatted_messages: Vec<String> = messages
            .iter()
            .map(|msg| {
                let role = match msg.role {
                    MessageRole::User => "User",
                    MessageRole::Assistant => "Assistant",
                    MessageRole::System => "System",
                    MessageRole::Tool => "Tool",
                };
                format!("{}: {}", role, msg.content)
            })
            .collect();

        // If no token limit, return all messages
        if max_tokens.is_none() {
            return Ok(formatted_messages.join("\n\n"));
        }

        let max_tokens = max_tokens.unwrap();

        // Apply token limiting - keep most recent messages
        let mut selected_messages = Vec::new();
        let mut total_tokens = 0;

        for msg in formatted_messages.iter().rev() {
            let msg_tokens = estimate_tokens(msg);

            if total_tokens + msg_tokens <= max_tokens {
                selected_messages.push(msg.clone());
                total_tokens += msg_tokens;
            } else {
                break;
            }
        }

        selected_messages.reverse();
        Ok(selected_messages.join("\n\n"))
    }

    /// Replay a session for debugging
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session ID
    ///
    /// # Returns
    ///
    /// SessionReplay with structured debug data
    pub async fn replay_session(&self, session_id: &str) -> Result<SessionReplay> {
        let session_data = self.backend.get_session_full(session_id).await?;

        if session_data.is_none() {
            return Err(crate::types::Error::NotFound(format!(
                "Session not found: {}",
                session_id
            )));
        }

        let session_data = session_data.unwrap();
        let session = session_data.session;
        let messages = session_data.messages;
        let working_sets = session_data.working_sets;

        // Build a map of message_id -> working_set for quick lookup
        let mut working_set_map = std::collections::HashMap::new();
        for ws in working_sets {
            if let Some(msg_id) = &ws.message_id {
                working_set_map.insert(msg_id.clone(), ws.working_set);
            }
        }

        // Group messages into turns
        let mut turns = Vec::new();
        let mut i = 0;

        while i < messages.len() {
            let msg = &messages[i];

            if matches!(msg.role, MessageRole::User) {
                let user_message = msg.clone();
                let working_set = working_set_map.get(&user_message.id).cloned();

                let assistant_message = if i + 1 < messages.len()
                    && matches!(messages[i + 1].role, MessageRole::Assistant)
                {
                    i += 1;
                    Some(messages[i].clone())
                } else {
                    None
                };

                turns.push(SessionTurn {
                    user_message,
                    working_set,
                    assistant_message,
                });
            }

            i += 1;
        }

        Ok(SessionReplay { session, turns })
    }

    /// Get session by ID
    pub async fn get_session(&self, session_id: &str) -> Result<Option<Session>> {
        self.backend.get_session(session_id).await
    }

    /// List sessions
    pub async fn list_sessions(
        &self,
        user_id: Option<&str>,
        limit: Option<usize>,
    ) -> Result<Vec<Session>> {
        self.backend.list_sessions(user_id, limit).await
    }

    /// Delete a session
    pub async fn delete_session(&self, session_id: &str) -> Result<()> {
        self.backend.delete_session(session_id).await
    }
}

/// Replay data for debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionReplay {
    /// The session
    pub session: Session,
    /// Conversation turns (user + assistant pairs)
    pub turns: Vec<SessionTurn>,
}

/// A conversation turn (user query + optional assistant response)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionTurn {
    /// User message
    pub user_message: Message,
    /// Working set compiled for this user message (if any)
    pub working_set: Option<WorkingSet>,
    /// Assistant response (if any)
    pub assistant_message: Option<Message>,
}

/// Estimate token count using simple approximation
///
/// Simple heuristic: chars / 4 (roughly matches GPT tokenization)
///
/// For production, consider using tiktoken-rs for accurate counting.
fn estimate_tokens(text: &str) -> usize {
    (text.len() + 3) / 4
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Artifact;
    use crate::types::Span;
    use uuid::Uuid;

    #[test]
    fn test_session_manager_new() {
        let db = Database::new(":memory:").unwrap();
        let _manager = SessionManager::new(db);
    }

    #[test]
    fn test_start_session() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db);

        let session = manager.start_session(Some("test_user")).unwrap();

        assert!(!session.id.is_empty());
        assert_eq!(session.user_id, Some("test_user".to_string()));
    }

    #[tokio::test]
    async fn test_add_user_message() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        // Create a session
        let session = manager.start_session(Some("user1")).unwrap();

        // Add some test data
        let artifact = Artifact {
            id: Uuid::new_v4().to_string(),
            path: "test.txt".to_string(),
            content: "This is a test document about Rust programming.".to_string(),
            content_hash: "hash123".to_string(),
            metadata: None,
            created_at: chrono::Utc::now(),
        };

        db.insert_artifact(&artifact).unwrap();

        let span = Span {
            id: Uuid::new_v4().to_string(),
            artifact_id: artifact.id.clone(),
            start_line: 1,
            end_line: 1,
            text: "This is a test document about Rust programming.".to_string(),
            embedding: Some(vec![0.1; 384]), // Fake embedding
            embedding_model: Some("test".to_string()),
            token_count: 10,
            metadata: None,
        };

        db.insert_spans(&[span]).unwrap();

        // Build index
        let index = db.get_vector_index().unwrap();

        // Add user message
        let config = CompilerConfig::default();
        let (message, working_set) = manager
            .add_user_message(&session.id, "What is Rust?", config, &index, None)
            .await
            .unwrap();

        assert_eq!(message.content, "What is Rust?");
        assert_eq!(message.role.as_str(), "user");
        assert!(!working_set.text.is_empty());
    }

    #[test]
    fn test_add_assistant_message() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db);

        let session = manager.start_session(Some("user1")).unwrap();

        let message = manager
            .add_assistant_message(&session.id, "Rust is a systems programming language.", None)
            .unwrap();

        assert_eq!(message.content, "Rust is a systems programming language.");
        assert_eq!(message.role.as_str(), "assistant");
    }

    #[test]
    fn test_get_conversation_history() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        let session = manager.start_session(Some("user1")).unwrap();

        // Add messages
        db.add_message(&session.id, MessageRole::User, "Hello", None)
            .unwrap();
        db.add_message(&session.id, MessageRole::Assistant, "Hi there!", None)
            .unwrap();
        db.add_message(&session.id, MessageRole::User, "How are you?", None)
            .unwrap();

        let history = manager
            .get_conversation_history(&session.id, None)
            .unwrap();

        assert!(history.contains("User: Hello"));
        assert!(history.contains("Assistant: Hi there!"));
        assert!(history.contains("User: How are you?"));

        // Verify formatting
        let lines: Vec<&str> = history.split("\n\n").collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "User: Hello");
        assert_eq!(lines[1], "Assistant: Hi there!");
        assert_eq!(lines[2], "User: How are you?");
    }

    #[test]
    fn test_get_conversation_history_with_token_limit() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        let session = manager.start_session(Some("user1")).unwrap();

        // Add messages
        db.add_message(&session.id, MessageRole::User, "Message 1", None)
            .unwrap();
        db.add_message(&session.id, MessageRole::Assistant, "Response 1", None)
            .unwrap();
        db.add_message(&session.id, MessageRole::User, "Message 2", None)
            .unwrap();
        db.add_message(&session.id, MessageRole::Assistant, "Response 2", None)
            .unwrap();

        // Set a tight token limit that should only allow the last 2 messages
        // Each message is about 5-7 tokens, so limit to 20 tokens
        let history = manager
            .get_conversation_history(&session.id, Some(20))
            .unwrap();

        // Should only contain the most recent messages
        assert!(history.contains("Message 2"));
        assert!(history.contains("Response 2"));

        // Should NOT contain older messages (if limit is tight enough)
        // Note: This is approximate due to simple token counting
        let message_count = history.split("\n\n").count();
        assert!(message_count <= 4); // All 4 messages fit in 20 tokens with our simple counting
    }

    #[test]
    fn test_get_conversation_history_empty() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db);

        let session = manager.start_session(Some("user1")).unwrap();

        let history = manager
            .get_conversation_history(&session.id, None)
            .unwrap();

        assert_eq!(history, "");
    }

    #[tokio::test]
    async fn test_replay_session() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        // Create session
        let session = manager.start_session(Some("user1")).unwrap();

        // Add test data for compilation
        let artifact = Artifact {
            id: Uuid::new_v4().to_string(),
            path: "test.txt".to_string(),
            content: "Test content for replay.".to_string(),
            content_hash: "hash123".to_string(),
            metadata: None,
            created_at: chrono::Utc::now(),
        };

        db.insert_artifact(&artifact).unwrap();

        let span = Span {
            id: Uuid::new_v4().to_string(),
            artifact_id: artifact.id.clone(),
            start_line: 1,
            end_line: 1,
            text: "Test content for replay.".to_string(),
            embedding: Some(vec![0.1; 384]),
            embedding_model: Some("test".to_string()),
            token_count: 5,
            metadata: None,
        };

        db.insert_spans(&[span]).unwrap();

        let index = db.get_vector_index().unwrap();

        // Add conversation
        let config = CompilerConfig::default();
        manager
            .add_user_message(&session.id, "First query", config.clone(), &index, None)
            .await
            .unwrap();
        manager
            .add_assistant_message(&session.id, "First response", None)
            .unwrap();
        manager
            .add_user_message(&session.id, "Second query", config, &index, None)
            .await
            .unwrap();
        manager
            .add_assistant_message(&session.id, "Second response", None)
            .unwrap();

        // Replay session
        let replay = manager.replay_session(&session.id).unwrap();

        assert_eq!(replay.session.id, session.id);
        assert_eq!(replay.turns.len(), 2);

        // Verify first turn
        let turn1 = &replay.turns[0];
        assert_eq!(turn1.user_message.content, "First query");
        assert!(turn1.working_set.is_some());
        assert!(turn1.assistant_message.is_some());
        assert_eq!(
            turn1.assistant_message.as_ref().unwrap().content,
            "First response"
        );

        // Verify second turn
        let turn2 = &replay.turns[1];
        assert_eq!(turn2.user_message.content, "Second query");
        assert!(turn2.working_set.is_some());
        assert!(turn2.assistant_message.is_some());
        assert_eq!(
            turn2.assistant_message.as_ref().unwrap().content,
            "Second response"
        );
    }

    #[test]
    fn test_replay_session_not_found() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db);

        let result = manager.replay_session("nonexistent-id");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_replay_session_incomplete_turns() {
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        let session = manager.start_session(Some("user1")).unwrap();

        // Add test data
        let artifact = Artifact {
            id: Uuid::new_v4().to_string(),
            path: "test.txt".to_string(),
            content: "Test content.".to_string(),
            content_hash: "hash123".to_string(),
            metadata: None,
            created_at: chrono::Utc::now(),
        };

        db.insert_artifact(&artifact).unwrap();

        let span = Span {
            id: Uuid::new_v4().to_string(),
            artifact_id: artifact.id.clone(),
            start_line: 1,
            end_line: 1,
            text: "Test content.".to_string(),
            embedding: Some(vec![0.1; 384]),
            embedding_model: Some("test".to_string()),
            token_count: 3,
            metadata: None,
        };

        db.insert_spans(&[span]).unwrap();

        let index = db.get_vector_index().unwrap();

        // Add user message without assistant response
        let config = CompilerConfig::default();
        manager
            .add_user_message(&session.id, "Query without response", config, &index, None)
            .await
            .unwrap();

        // Replay should still work
        let replay = manager.replay_session(&session.id).unwrap();

        assert_eq!(replay.turns.len(), 1);
        let turn = &replay.turns[0];
        assert_eq!(turn.user_message.content, "Query without response");
        assert!(turn.working_set.is_some());
        assert!(turn.assistant_message.is_none());
    }

    #[test]
    fn test_estimate_tokens() {
        // Test simple token estimation
        let text = "Hello world";
        let tokens = estimate_tokens(text);
        // "Hello world" = 11 chars, so (11 + 3) / 4 = 3 tokens
        assert_eq!(tokens, 3);

        let longer_text = "This is a longer piece of text for testing token estimation.";
        let tokens = estimate_tokens(longer_text);
        // Should be roughly chars/4
        assert!(tokens > 10);
        assert!(tokens < 20);
    }

    /// Integration test demonstrating the full SessionManager workflow
    #[tokio::test]
    async fn test_full_session_workflow() {
        // Setup database and manager
        let db = Database::new(":memory:").unwrap();
        let manager = SessionManager::new(db.clone());

        // Ingest some test documents
        let docs = vec![
            ("rust_basics.md", "Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety."),
            ("rust_ownership.md", "Ownership is Rust's most unique feature. It enables Rust to make memory safety guarantees without needing a garbage collector."),
            ("rust_concurrency.md", "Rust's type system and ownership model guarantee thread safety. You can't have data races in safe Rust code."),
        ];

        for (path, content) in &docs {
            let artifact = Artifact {
                id: Uuid::new_v4().to_string(),
                path: path.to_string(),
                content: content.to_string(),
                content_hash: format!("hash_{}", path),
                metadata: None,
                created_at: chrono::Utc::now(),
            };

            db.insert_artifact(&artifact).unwrap();

            // Create span for the document
            let span = Span {
                id: Uuid::new_v4().to_string(),
                artifact_id: artifact.id.clone(),
                start_line: 1,
                end_line: 1,
                text: content.to_string(),
                embedding: Some(vec![0.1; 384]), // Fake embedding
                embedding_model: Some("test".to_string()),
                token_count: content.split_whitespace().count(),
                metadata: None,
            };

            db.insert_spans(&[span]).unwrap();
        }

        // Build index
        let index = db.get_vector_index().unwrap();

        // Start a new session
        let session = manager.start_session(Some("alice")).unwrap();
        assert_eq!(session.user_id, Some("alice".to_string()));

        // First turn: User asks about Rust
        let config = CompilerConfig::default();
        let (msg1, ws1) = manager
            .add_user_message(&session.id, "What is Rust?", config.clone(), &index, None)
            .await
            .unwrap();

        assert_eq!(msg1.content, "What is Rust?");
        assert!(!ws1.text.is_empty());
        assert!(!ws1.citations.is_empty());

        // Assistant responds
        let resp1 = manager
            .add_assistant_message(
                &session.id,
                "Rust is a systems programming language known for memory safety.",
                None,
            )
            .unwrap();

        assert!(resp1.content.contains("memory safety"));

        // Second turn: User asks follow-up
        let (msg2, ws2) = manager
            .add_user_message(
                &session.id,
                "Tell me about ownership",
                config.clone(),
                &index,
                None,
            )
            .await
            .unwrap();

        assert_eq!(msg2.content, "Tell me about ownership");
        assert!(!ws2.text.is_empty());

        // Assistant responds
        let resp2 = manager
            .add_assistant_message(
                &session.id,
                "Ownership is Rust's unique feature for memory management.",
                None,
            )
            .unwrap();

        assert!(resp2.content.contains("Ownership"));

        // Get conversation history
        let history = manager
            .get_conversation_history(&session.id, None)
            .unwrap();

        // Verify all messages are in history
        assert!(history.contains("What is Rust?"));
        assert!(history.contains("memory safety"));
        assert!(history.contains("Tell me about ownership"));
        assert!(history.contains("Ownership is Rust's unique feature"));

        // Test token limiting - limit to about 2 messages worth
        let limited_history = manager
            .get_conversation_history(&session.id, Some(100))
            .unwrap();

        // Should work without errors and contain at least some messages
        assert!(!limited_history.is_empty());
        // Most recent messages should be present
        assert!(limited_history.contains("Ownership"));

        // Replay the session
        let replay = manager.replay_session(&session.id).unwrap();

        assert_eq!(replay.session.id, session.id);
        assert_eq!(replay.turns.len(), 2);

        // Verify first turn
        let turn1 = &replay.turns[0];
        assert_eq!(turn1.user_message.content, "What is Rust?");
        assert!(turn1.working_set.is_some());
        assert!(turn1.assistant_message.is_some());

        // Note: Working sets from replay are placeholders in Phase 1
        // Phase 2 database doesn't store full WorkingSet data yet
        // This is expected and documented in db.rs

        // Verify second turn
        let turn2 = &replay.turns[1];
        assert_eq!(turn2.user_message.content, "Tell me about ownership");
        assert!(turn2.working_set.is_some());
        assert!(turn2.assistant_message.is_some());

        // Verify messages are in order
        assert!(turn1.user_message.sequence_number < turn2.user_message.sequence_number);
    }
}