oxi-cli 0.6.12

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
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
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
//! Session tree navigation for branched sessions
//!
//! Handles navigation between different points in the session tree,
//! including the creation of branch summaries when leaving a path.

use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use uuid::Uuid;

// ============================================================================
// Types
// ============================================================================

/// Extended session entry types for tree navigation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SessionEntryType {
    /// A regular message (user/assistant/system/tool)
    Message(MessageEntry),
    /// Summary of a branch when navigating away
    BranchSummary(BranchSummaryEntry),
    /// Summary after context compaction
    Compaction(CompactionEntry),
    /// A label/marker on an entry
    Label(LabelEntry),
    /// User-defined metadata for the session
    SessionInfo(SessionInfoEntry),
    /// Extension-specific data (not sent to LLM)
    Custom(CustomEntry),
    /// Custom message from extension (sent to LLM)
    CustomMessage(CustomMessageEntry),
}

/// Message entry content
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageEntry {
    /// Unique identifier for this message.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Role of the message sender.
    pub role: MessageRole,
    /// Text content of the message.
    pub content: String,
}

/// Message role types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MessageRole {
    /// User-authored message.
    User,
    /// Assistant-authored message.
    Assistant,
    /// System prompt message.
    System,
    /// Tool call message.
    Tool,
    /// Tool result message.
    ToolResult,
    /// Custom role message.
    Custom,
}

impl MessageRole {
    /// Check if this is a user message
    pub fn is_user(&self) -> bool {
        matches!(self, MessageRole::User)
    }

    /// Check if this is an assistant message
    pub fn is_assistant(&self) -> bool {
        matches!(self, MessageRole::Assistant)
    }
}

/// Branch summary entry - captures context when navigating away from a branch
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchSummaryEntry {
    /// Unique identifier for this summary entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// ID of the entry this summary is attached to
    pub from_id: Uuid,
    /// The summary text
    pub summary: String,
    /// Optional details (e.g., file lists)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<BranchSummaryDetails>,
    /// True if generated by an extension
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_hook: Option<bool>,
}

/// Details stored in BranchSummaryEntry for file tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchSummaryDetails {
    /// Files that were read during the branch.
    pub read_files: Vec<String>,
    /// Files that were modified during the branch.
    pub modified_files: Vec<String>,
}

/// Compaction entry - summary after context window compaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionEntry {
    /// Unique identifier for this compaction entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Compacted summary text.
    pub summary: String,
    /// ID of first entry kept after compaction
    pub first_kept_entry_id: Uuid,
    /// Estimated tokens before compaction
    pub tokens_before: usize,
    /// Extension-specific data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
    /// True if generated by an extension
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_hook: Option<bool>,
}

/// Label entry - user-defined bookmark/marker on entries
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LabelEntry {
    /// Unique identifier for this label entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Entry ID this label is attached to.
    pub target_id: Uuid,
    /// Optional label text.
    pub label: Option<String>,
}

/// Session info entry - metadata like display name
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfoEntry {
    /// Unique identifier for this session info entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Optional display name for the session.
    pub name: Option<String>,
}

/// Custom entry for extensions to store extension-specific data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomEntry {
    /// Unique identifier for this custom entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Extension-defined type discriminator.
    pub custom_type: String,
    /// Optional extension-specific data payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
}

/// Custom message entry for extensions to inject messages into LLM context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomMessageEntry {
    /// Unique identifier for this custom message entry.
    pub id: Uuid,
    /// Parent entry ID in the session tree.
    pub parent_id: Option<Uuid>,
    /// Unix timestamp in milliseconds.
    pub timestamp: i64,
    /// Extension-defined type discriminator.
    pub custom_type: String,
    /// Message content injected into LLM context.
    pub content: String,
    /// Optional extension-specific details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
    /// Controls TUI rendering
    pub display: bool,
}

/// Result of collecting entries for branch summarization
#[derive(Debug, Clone)]
pub struct CollectEntriesResult {
    /// Entries to summarize, in chronological order
    pub entries: Vec<SessionEntryType>,
    /// Common ancestor between old and new position
    pub common_ancestor_id: Option<Uuid>,
}

/// Navigation target options
#[derive(Debug, Clone)]
pub struct NavigationOptions {
    /// If true, generate a summary of the branch being left
    pub summarize: bool,
    /// Custom instructions for summarization
    pub custom_instructions: Option<String>,
    /// If true, custom_instructions replaces the default prompt instead of being appended
    pub replace_instructions: bool,
    /// Label to attach to the branch summary entry
    pub label: Option<String>,
}

impl Default for NavigationOptions {
    fn default() -> Self {
        Self {
            summarize: false,
            custom_instructions: None,
            replace_instructions: false,
            label: None,
        }
    }
}

/// Result of tree navigation
#[derive(Debug, Clone)]
pub struct NavigationResult {
    /// Text content for the editor (user message text when navigating to user message)
    pub editor_text: Option<String>,
    /// True if navigation was cancelled
    pub cancelled: bool,
    /// True if summarization was aborted
    pub aborted: bool,
    /// ID of the summary entry created, if any
    pub summary_entry_id: Option<Uuid>,
}

/// Preparation data for tree navigation (passed to extension hooks)
#[derive(Debug, Clone)]
pub struct TreePreparation {
    /// The navigation target entry ID.
    pub target_id: Uuid,
    /// The leaf ID before navigation.
    pub old_leaf_id: Option<Uuid>,
    /// Common ancestor between old and new positions.
    pub common_ancestor_id: Option<Uuid>,
    /// Entries collected for summarization.
    pub entries_to_summarize: Vec<SessionEntryType>,
    /// Whether the user requested a summary.
    pub user_wants_summary: bool,
    /// Custom instructions for summarization.
    pub custom_instructions: Option<String>,
    /// Whether custom instructions replace the default prompt.
    pub replace_instructions: bool,
    /// Label to attach to the summary.
    pub label: Option<String>,
}

/// Trait for LLM-based summarization
/// Implement this to provide actual summarization, or use a mock for testing
pub trait Summarizer: Send + Sync {
    /// Generate a summary of the given entries
    fn summarize(
        &self,
        entries: &[SessionEntryType],
        custom_instructions: Option<&str>,
        replace_instructions: bool,
    ) -> impl std::future::Future<Output = Result<BranchSummaryResult, SummarizationError>>
           + Send
           + 'static;
}

/// Result of branch summarization
#[derive(Debug, Clone)]
pub struct BranchSummaryResult {
    /// The generated summary text
    pub summary: Option<String>,
    /// Files that were read during this branch
    pub read_files: Vec<String>,
    /// Files that were modified during this branch
    pub modified_files: Vec<String>,
    /// True if the operation was aborted
    pub aborted: bool,
    /// Error message if summarization failed
    pub error: Option<String>,
}

/// Error during summarization
#[derive(Debug, Clone)]
pub enum SummarizationError {
    /// No model available for summarization.
    NoModel,
    /// Summarization was aborted by the user.
    Aborted,
    /// Summarization failed with an error message.
    Failed(String),
}

/// Extension hook result for session_before_tree event
#[derive(Debug, Clone)]
pub struct BeforeTreeHookResult {
    /// If true, cancel the navigation
    pub cancel: bool,
    /// Summary from extension (if user wants summary)
    pub summary: Option<ExtensionSummary>,
    /// Override custom instructions
    pub custom_instructions: Option<String>,
    /// Override replace instructions flag
    pub replace_instructions: Option<bool>,
    /// Override label
    pub label: Option<String>,
}

/// Summary generated by an extension
#[derive(Debug, Clone)]
pub struct ExtensionSummary {
    /// Summary text generated by the extension.
    pub summary: String,
    /// Optional extension-specific details.
    pub details: Option<serde_json::Value>,
}

// ============================================================================
// Tree Navigation Implementation
// ============================================================================

/// Session navigator for tree-based session management
pub struct SessionNavigator {
    /// All entries indexed by ID
    entries_by_id: HashMap<Uuid, SessionEntryType>,
    /// Labels by entry ID
    labels_by_id: HashMap<Uuid, String>,
    /// Label timestamps by entry ID
    label_timestamps_by_id: HashMap<Uuid, i64>,
    /// Current leaf ID (where new entries are appended)
    leaf_id: Option<Uuid>,
}

impl SessionNavigator {
    /// Create a new session navigator
    pub fn new() -> Self {
        Self {
            entries_by_id: HashMap::new(),
            labels_by_id: HashMap::new(),
            label_timestamps_by_id: HashMap::new(),
            leaf_id: None,
        }
    }

    /// Create from existing entries
    pub fn from_entries(entries: Vec<SessionEntryType>, leaf_id: Option<Uuid>) -> Self {
        let mut entries_by_id = HashMap::new();
        for entry in &entries {
            let id = Self::entry_id(entry);
            entries_by_id.insert(id, entry.clone());
        }

        Self {
            entries_by_id,
            labels_by_id: HashMap::new(),
            label_timestamps_by_id: HashMap::new(),
            leaf_id,
        }
    }

    /// Get the current leaf ID
    pub fn get_leaf_id(&self) -> Option<Uuid> {
        self.leaf_id
    }

    /// Get an entry by ID
    pub fn get_entry(&self, id: Uuid) -> Option<&SessionEntryType> {
        self.entries_by_id.get(&id)
    }

    /// Get the label for an entry, if any
    pub fn get_label(&self, id: Uuid) -> Option<&str> {
        self.labels_by_id.get(&id).map(|s| s.as_str())
    }

    /// Get all entries
    pub fn get_entries(&self) -> Vec<&SessionEntryType> {
        self.entries_by_id.values().collect()
    }

    /// Get the branch path from root to a given entry
    pub fn get_branch(&self, from_id: Option<Uuid>) -> Vec<&SessionEntryType> {
        let mut path = Vec::new();
        let start_id = from_id.or(self.leaf_id);

        let mut current_id = start_id;
        while let Some(id) = current_id {
            if let Some(entry) = self.entries_by_id.get(&id) {
                path.insert(0, entry);
                current_id = Self::entry_parent_id(entry);
            } else {
                break;
            }
        }

        path
    }

    /// Get all direct children of an entry
    pub fn get_children(&self, parent_id: Uuid) -> Vec<&SessionEntryType> {
        self.entries_by_id
            .values()
            .filter(|entry| Self::entry_parent_id(entry) == Some(parent_id))
            .collect()
    }

    /// Collect entries that should be summarized when navigating from one position to another.
    pub fn collect_entries_for_branch_summary(
        &self,
        old_leaf_id: Option<Uuid>,
        target_id: Uuid,
    ) -> CollectEntriesResult {
        let old_leaf_id = match old_leaf_id {
            Some(id) => id,
            None => {
                return CollectEntriesResult {
                    entries: Vec::new(),
                    common_ancestor_id: None,
                };
            }
        };

        let old_path_ids: HashSet<Uuid> = self
            .get_branch(Some(old_leaf_id))
            .iter()
            .map(|e| Self::entry_id(e))
            .collect();

        let target_path = self.get_branch(Some(target_id));

        let mut common_ancestor_id: Option<Uuid> = None;
        for entry in target_path.iter().rev() {
            let id = Self::entry_id(entry);
            if old_path_ids.contains(&id) {
                common_ancestor_id = Some(id);
                break;
            }
        }

        let mut entries: Vec<&SessionEntryType> = Vec::new();
        let mut current_id: Option<Uuid> = Some(old_leaf_id);

        while let Some(id) = current_id {
            if current_id == common_ancestor_id {
                break;
            }
            if let Some(entry) = self.entries_by_id.get(&id) {
                entries.push(entry);
                current_id = Self::entry_parent_id(entry);
            } else {
                break;
            }
        }

        entries.reverse();

        CollectEntriesResult {
            entries: entries.into_iter().cloned().collect(),
            common_ancestor_id,
        }
    }

    /// Navigate to a target entry in the session tree.
    pub fn navigate_tree<N: Summarizer + ?Sized>(
        &mut self,
        target_id: Uuid,
        options: NavigationOptions,
        summarizer: Option<&N>,
        extension_hook: Option<&dyn Fn(TreePreparation) -> BeforeTreeHookResult>,
    ) -> NavigationResult {
        let old_leaf_id = self.leaf_id;

        if Some(target_id) == old_leaf_id {
            return NavigationResult {
                editor_text: None,
                cancelled: false,
                aborted: false,
                summary_entry_id: None,
            };
        }

        let target_entry = match self.entries_by_id.get(&target_id) {
            Some(e) => e,
            None => {
                return NavigationResult {
                    editor_text: None,
                    cancelled: true,
                    aborted: false,
                    summary_entry_id: None,
                };
            }
        };

        let collection = self.collect_entries_for_branch_summary(old_leaf_id, target_id);

        let mut custom_instructions = options.custom_instructions.clone();
        let mut replace_instructions = options.replace_instructions;
        let mut label = options.label.clone();

        let preparation = TreePreparation {
            target_id,
            old_leaf_id,
            common_ancestor_id: collection.common_ancestor_id,
            entries_to_summarize: collection.entries.clone(),
            user_wants_summary: options.summarize,
            custom_instructions: custom_instructions.clone(),
            replace_instructions,
            label: label.clone(),
        };

        let mut extension_summary: Option<ExtensionSummary> = None;
        let mut from_extension = false;

        if let Some(hook) = extension_hook {
            let result = hook(preparation);
            if result.cancel {
                return NavigationResult {
                    editor_text: None,
                    cancelled: true,
                    aborted: false,
                    summary_entry_id: None,
                };
            }

            if let Some(ext_sum) = result.summary {
                extension_summary = Some(ext_sum);
                from_extension = true;
            }

            if let Some(ci) = result.custom_instructions {
                custom_instructions = Some(ci);
            }
            if let Some(ri) = result.replace_instructions {
                replace_instructions = ri;
            }
            if let Some(l) = result.label {
                label = Some(l);
            }
        }

        let mut summary_text: Option<String> = None;
        let mut summary_details: Option<BranchSummaryDetails> = None;

        if options.summarize && !collection.entries.is_empty() && extension_summary.is_none() {
            if let Some(summarizer) = summarizer {
                let rt = summarizer.summarize(
                    &collection.entries,
                    custom_instructions.as_deref(),
                    replace_instructions,
                );
                let runtime = tokio::runtime::Handle::current();
                let result = runtime.block_on(rt);

                match result {
                    Ok(summary_result) => {
                        if summary_result.aborted {
                            return NavigationResult {
                                editor_text: None,
                                cancelled: true,
                                aborted: true,
                                summary_entry_id: None,
                            };
                        }
                        if let Some(err) = summary_result.error {
                            tracing::warn!("Summarization failed: {}", err);
                        }
                        summary_text = summary_result.summary;
                        if !summary_result.read_files.is_empty()
                            || !summary_result.modified_files.is_empty()
                        {
                            summary_details = Some(BranchSummaryDetails {
                                read_files: summary_result.read_files,
                                modified_files: summary_result.modified_files,
                            });
                        }
                    }
                    Err(e) => {
                        tracing::warn!("Summarization error: {:?}", e);
                    }
                }
            }
        } else if let Some(ext_sum) = extension_summary {
            summary_text = Some(ext_sum.summary);
            summary_details = ext_sum.details.and_then(|d| serde_json::from_value(d).ok());
        }

        let (new_leaf_id, editor_text) = Self::determine_leaf_and_editor(target_entry);

        let has_summary = summary_text.is_some();
        let summary_entry_id = if let Some(text) = summary_text {
            let summary_id = self.branch_with_summary(new_leaf_id, text, summary_details, from_extension);

            if let Some(l) = &label {
                self.append_label_change(summary_id, Some(l.clone()));
            }

            Some(summary_id)
        } else if new_leaf_id.is_none() {
            self.reset_leaf();
            None
        } else {
            if let Some(id) = new_leaf_id {
                self.branch(id);
            } else {
                self.reset_leaf();
            }
            None
        };

        let has_label = label.is_some();
        if has_label && !has_summary {
            self.append_label_change(target_id, label);
        }

        NavigationResult {
            editor_text,
            cancelled: false,
            aborted: false,
            summary_entry_id,
        }
    }

    fn determine_leaf_and_editor(entry: &SessionEntryType) -> (Option<Uuid>, Option<String>) {
        match entry {
            SessionEntryType::Message(msg) if msg.role.is_user() => {
                let editor_text = if msg.content.is_empty() {
                    None
                } else {
                    Some(msg.content.clone())
                };
                (msg.parent_id, editor_text)
            }
            SessionEntryType::CustomMessage(custom) => {
                let editor_text = if custom.content.is_empty() {
                    None
                } else {
                    Some(custom.content.clone())
                };
                (custom.parent_id, editor_text)
            }
            _ => {
                let leaf_id = Self::entry_id(entry);
                (Some(leaf_id), None)
            }
        }
    }

    /// Switch to a different entry (start a new branch).
    pub fn branch(&mut self, branch_from_id: Uuid) {
        if !self.entries_by_id.contains_key(&branch_from_id) {
            return;
        }
        self.leaf_id = Some(branch_from_id);
    }

    /// Reset the leaf pointer to null (before any entries).
    pub fn reset_leaf(&mut self) {
        self.leaf_id = None;
    }

    /// Start a new branch with a summary of the abandoned path.
    pub fn branch_with_summary(
        &mut self,
        branch_from_id: Option<Uuid>,
        summary: String,
        details: Option<BranchSummaryDetails>,
        from_hook: bool,
    ) -> Uuid {
        self.leaf_id = branch_from_id;

        let summary_id = Uuid::new_v4();
        let entry = SessionEntryType::BranchSummary(BranchSummaryEntry {
            id: summary_id,
            parent_id: branch_from_id,
            timestamp: Utc::now().timestamp_millis(),
            from_id: branch_from_id.unwrap_or(Uuid::nil()),
            summary,
            details,
            from_hook: Some(from_hook),
        });

        self.entries_by_id.insert(summary_id, entry.clone());
        summary_id
    }

    /// Set or clear a label on an entry.
    pub fn append_label_change(&mut self, target_id: Uuid, label: Option<String>) -> Uuid {
        if !self.entries_by_id.contains_key(&target_id) {
            return Uuid::nil();
        }

        let label_id = Uuid::new_v4();
        let entry = SessionEntryType::Label(LabelEntry {
            id: label_id,
            parent_id: self.leaf_id,
            timestamp: Utc::now().timestamp_millis(),
            target_id,
            label: label.clone(),
        });

        self.entries_by_id.insert(label_id, entry);

        if let Some(l) = label {
            self.labels_by_id.insert(target_id, l);
            self.label_timestamps_by_id
                .insert(target_id, Utc::now().timestamp_millis());
        } else {
            self.labels_by_id.remove(&target_id);
            self.label_timestamps_by_id.remove(&target_id);
        }

        label_id
    }

    /// Add an entry to the session
    pub fn add_entry(&mut self, entry: SessionEntryType) {
        let id = Self::entry_id(&entry);
        self.entries_by_id.insert(id, entry);
    }

    /// Get the label timestamp for an entry, if any
    pub fn get_label_timestamp(&self, id: Uuid) -> Option<i64> {
        self.label_timestamps_by_id.get(&id).copied()
    }

    fn entry_id(entry: &SessionEntryType) -> Uuid {
        match entry {
            SessionEntryType::Message(e) => e.id,
            SessionEntryType::BranchSummary(e) => e.id,
            SessionEntryType::Compaction(e) => e.id,
            SessionEntryType::Label(e) => e.id,
            SessionEntryType::SessionInfo(e) => e.id,
            SessionEntryType::Custom(e) => e.id,
            SessionEntryType::CustomMessage(e) => e.id,
        }
    }

    fn entry_parent_id(entry: &SessionEntryType) -> Option<Uuid> {
        match entry {
            SessionEntryType::Message(e) => e.parent_id,
            SessionEntryType::BranchSummary(e) => e.parent_id,
            SessionEntryType::Compaction(e) => e.parent_id,
            SessionEntryType::Label(e) => e.parent_id,
            SessionEntryType::SessionInfo(e) => e.parent_id,
            SessionEntryType::Custom(e) => e.parent_id,
            SessionEntryType::CustomMessage(e) => e.parent_id,
        }
    }
}

impl Default for SessionNavigator {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Utility Functions
// ============================================================================

/// Extract text content from user message
pub fn extract_user_message_text(content: &str) -> String {
    content.to_string()
}

/// Extract text content from custom message
pub fn extract_custom_message_text(content: &str) -> String {
    content.to_string()
}

/// Check if an entry is a user message
pub fn is_user_message(entry: &SessionEntryType) -> bool {
    matches!(
        entry,
        SessionEntryType::Message(msg) if msg.role.is_user()
    )
}

/// Check if an entry is a custom message
pub fn is_custom_message(entry: &SessionEntryType) -> bool {
    matches!(entry, SessionEntryType::CustomMessage(_))
}

/// Check if an entry is an assistant message
pub fn is_assistant_message(entry: &SessionEntryType) -> bool {
    matches!(
        entry,
        SessionEntryType::Message(msg) if msg.role.is_assistant()
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    /// No-op summarizer for tests that don't need summarization.
    struct NoOpSummarizer;
    impl Summarizer for NoOpSummarizer {
        fn summarize(
            &self,
            _entries: &[SessionEntryType],
            _custom_instructions: Option<&str>,
            _replace_instructions: bool,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<BranchSummaryResult, SummarizationError>> + Send + 'static>> {
            Box::pin(async {
                Ok(BranchSummaryResult {
                    summary: None,
                    read_files: vec![],
                    modified_files: vec![],
                    aborted: false,
                    error: None,
                })
            })
        }
    }

    fn create_message(id: Uuid, parent_id: Option<Uuid>, role: MessageRole, content: &str) -> SessionEntryType {
        SessionEntryType::Message(MessageEntry {
            id,
            parent_id,
            timestamp: 0,
            role,
            content: content.to_string(),
        })
    }

    fn entry_id(entry: &SessionEntryType) -> Uuid {
        match entry {
            SessionEntryType::Message(e) => e.id,
            SessionEntryType::BranchSummary(e) => e.id,
            SessionEntryType::Compaction(e) => e.id,
            SessionEntryType::Label(e) => e.id,
            SessionEntryType::SessionInfo(e) => e.id,
            SessionEntryType::Custom(e) => e.id,
            SessionEntryType::CustomMessage(e) => e.id,
        }
    }

    // ===================================================================
    // Original tests
    // ===================================================================

    #[test]
    fn test_navigate_to_user_message() {
        let mut nav = SessionNavigator::new();

        let root_id = Uuid::new_v4();
        let user_id = Uuid::new_v4();
        let assistant_id = Uuid::new_v4();

        nav.add_entry(create_message(root_id, None, MessageRole::User, "Hello"));
        nav.add_entry(create_message(user_id, Some(root_id), MessageRole::User, "How are you?"));
        nav.add_entry(create_message(assistant_id, Some(user_id), MessageRole::Assistant, "I'm fine"));

        nav.branch(assistant_id);

        let result = nav.navigate_tree(user_id, NavigationOptions::default(), None as Option<&NoOpSummarizer>, None);

        assert!(!result.cancelled);
        assert!(!result.aborted);
        assert_eq!(result.editor_text, Some("How are you?".to_string()));
        assert_eq!(nav.get_leaf_id(), Some(root_id));
    }

    #[test]
    fn test_navigate_to_assistant_message() {
        let mut nav = SessionNavigator::new();

        let root_id = Uuid::new_v4();
        let user_id = Uuid::new_v4();
        let assistant_id = Uuid::new_v4();

        nav.add_entry(create_message(root_id, None, MessageRole::User, "Hello"));
        nav.add_entry(create_message(user_id, Some(root_id), MessageRole::User, "How are you?"));
        nav.add_entry(create_message(assistant_id, Some(user_id), MessageRole::Assistant, "I'm fine"));

        nav.branch(assistant_id);

        let result = nav.navigate_tree(assistant_id, NavigationOptions::default(), None as Option<&NoOpSummarizer>, None);

        assert!(!result.cancelled);
        assert_eq!(nav.get_leaf_id(), Some(assistant_id));
    }

    #[test]
    fn test_noop_navigation() {
        let mut nav = SessionNavigator::new();

        let entry_id = Uuid::new_v4();
        nav.add_entry(create_message(entry_id, None, MessageRole::User, "Test"));
        nav.branch(entry_id);

        let result = nav.navigate_tree(entry_id, NavigationOptions::default(), None as Option<&NoOpSummarizer>, None);

        assert!(!result.cancelled);
        assert_eq!(result.editor_text, None);
    }

    #[test]
    fn test_collect_entries_for_branch_summary() {
        let root_id = Uuid::new_v4();
        let user_id = Uuid::new_v4();
        let assistant_id = Uuid::new_v4();
        let branch_user_id = Uuid::new_v4();
        let branch_assistant_id = Uuid::new_v4();

        let entries = vec![
            create_message(root_id, None, MessageRole::User, "Root"),
            create_message(user_id, Some(root_id), MessageRole::User, "User"),
            create_message(assistant_id, Some(user_id), MessageRole::Assistant, "Assistant"),
            create_message(branch_user_id, Some(user_id), MessageRole::User, "Branch User"),
            create_message(branch_assistant_id, Some(branch_user_id), MessageRole::Assistant, "Branch Assistant"),
        ];

        let nav = SessionNavigator::from_entries(entries, Some(branch_assistant_id));

        let result = nav.collect_entries_for_branch_summary(Some(branch_assistant_id), root_id);

        assert_eq!(result.common_ancestor_id, Some(root_id));
        assert_eq!(result.entries.len(), 3);
    }

    #[test]
    fn test_label_attachment() {
        let mut nav = SessionNavigator::new();

        let entry_id = Uuid::new_v4();
        nav.add_entry(create_message(entry_id, None, MessageRole::User, "Test"));

        let label_id = nav.append_label_change(entry_id, Some("Important".to_string()));

        assert_eq!(nav.get_label(entry_id), Some("Important"));
        assert!(nav.get_entry(label_id).is_some());

        nav.append_label_change(entry_id, None);

        assert_eq!(nav.get_label(entry_id), None);
    }

    #[test]
    fn test_branch_with_summary() {
        let mut nav = SessionNavigator::new();

        let entry_id = Uuid::new_v4();
        nav.add_entry(create_message(entry_id, None, MessageRole::User, "Test"));

        nav.branch(entry_id);

        let summary_id = nav.branch_with_summary(
            Some(entry_id),
            "This is a summary".to_string(),
            None,
            false,
        );

        assert!(nav.get_entry(summary_id).is_some());
        assert_eq!(nav.get_leaf_id(), Some(entry_id));

        match nav.get_entry(summary_id) {
            Some(SessionEntryType::BranchSummary(e)) => {
                assert_eq!(e.summary, "This is a summary");
            }
            _ => panic!("Expected branch summary entry"),
        }
    }

    // ===================================================================
    // Additional comprehensive tests
    // ===================================================================

    #[test]
    fn test_new_navigator_has_no_leaf() {
        let nav = SessionNavigator::new();
        assert!(nav.get_leaf_id().is_none());
    }

    #[test]
    fn test_default_navigator_has_no_leaf() {
        let nav = SessionNavigator::default();
        assert!(nav.get_leaf_id().is_none());
    }

    #[test]
    fn test_get_entry_returns_none_for_unknown() {
        let nav = SessionNavigator::new();
        assert!(nav.get_entry(Uuid::new_v4()).is_none());
    }

    #[test]
    fn test_get_branch_returns_empty_when_no_entries() {
        let nav = SessionNavigator::new();
        assert!(nav.get_branch(None).is_empty());
    }

    #[test]
    fn test_get_branch_returns_full_path() {
        let mut nav = SessionNavigator::new();
        let root_id = Uuid::new_v4();
        let mid_id = Uuid::new_v4();
        let leaf_id = Uuid::new_v4();

        nav.add_entry(create_message(root_id, None, MessageRole::User, "Root"));
        nav.add_entry(create_message(mid_id, Some(root_id), MessageRole::Assistant, "Mid"));
        nav.add_entry(create_message(leaf_id, Some(mid_id), MessageRole::User, "Leaf"));
        nav.branch(leaf_id);

        let branch = nav.get_branch(None);
        assert_eq!(branch.len(), 3);
        assert_eq!(entry_id(branch[0]), root_id);
        assert_eq!(entry_id(branch[1]), mid_id);
        assert_eq!(entry_id(branch[2]), leaf_id);
    }

    #[test]
    fn test_get_children() {
        let mut nav = SessionNavigator::new();
        let parent_id = Uuid::new_v4();
        let child_a = Uuid::new_v4();
        let child_b = Uuid::new_v4();

        nav.add_entry(create_message(parent_id, None, MessageRole::User, "Parent"));
        nav.add_entry(create_message(child_a, Some(parent_id), MessageRole::Assistant, "A"));
        nav.add_entry(create_message(child_b, Some(parent_id), MessageRole::Assistant, "B"));

        let children = nav.get_children(parent_id);
        assert_eq!(children.len(), 2);
    }

    #[test]
    fn test_get_children_of_leaf() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Solo"));

        let children = nav.get_children(id);
        assert!(children.is_empty());
    }

    #[test]
    fn test_branch_switches_leaf() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));

        nav.branch(id);
        assert_eq!(nav.get_leaf_id(), Some(id));

        nav.reset_leaf();
        assert_eq!(nav.get_leaf_id(), None);
    }

    #[test]
    fn test_reset_leaf() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));
        nav.branch(id);
        assert_eq!(nav.get_leaf_id(), Some(id));

        nav.reset_leaf();
        assert!(nav.get_leaf_id().is_none());
    }

    #[test]
    fn test_from_entries_preserves_leaf() {
        let id1 = Uuid::new_v4();
        let id2 = Uuid::new_v4();
        let entries = vec![
            create_message(id1, None, MessageRole::User, "A"),
            create_message(id2, Some(id1), MessageRole::Assistant, "B"),
        ];
        let nav = SessionNavigator::from_entries(entries, Some(id2));
        assert_eq!(nav.get_leaf_id(), Some(id2));
        assert!(nav.get_entry(id1).is_some());
        assert!(nav.get_entry(id2).is_some());
    }

    #[test]
    fn test_navigate_to_nonexistent_returns_cancelled() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));
        nav.branch(id);

        let result = nav.navigate_tree(
            Uuid::new_v4(),
            NavigationOptions::default(),
            None as Option<&NoOpSummarizer>,
            None,
        );
        assert!(result.cancelled);
    }

    #[test]
    fn test_navigate_to_root_resets_leaf() {
        let mut nav = SessionNavigator::new();
        let root_id = Uuid::new_v4();
        let child_id = Uuid::new_v4();

        nav.add_entry(create_message(root_id, None, MessageRole::User, "Root"));
        nav.add_entry(create_message(child_id, Some(root_id), MessageRole::Assistant, "Child"));
        nav.branch(child_id);

        let result = nav.navigate_tree(
            root_id,
            NavigationOptions::default(),
            None as Option<&NoOpSummarizer>,
            None,
        );
        assert!(!result.cancelled);
        assert_eq!(result.editor_text, Some("Root".to_string()));
        assert_eq!(nav.get_leaf_id(), None);
    }

    #[test]
    fn test_collect_entries_no_old_leaf() {
        let target_id = Uuid::new_v4();
        let mut nav = SessionNavigator::new();
        nav.add_entry(create_message(target_id, None, MessageRole::User, "T"));

        let result = nav.collect_entries_for_branch_summary(None, target_id);
        assert!(result.entries.is_empty());
        assert_eq!(result.common_ancestor_id, None);
    }

    #[test]
    fn test_collect_entries_same_branch_common_ancestor() {
        let root_id = Uuid::new_v4();
        let user_id = Uuid::new_v4();
        let assistant_id = Uuid::new_v4();

        let entries = vec![
            create_message(root_id, None, MessageRole::User, "Root"),
            create_message(user_id, Some(root_id), MessageRole::User, "User"),
            create_message(assistant_id, Some(user_id), MessageRole::Assistant, "Asst"),
        ];

        let nav = SessionNavigator::from_entries(entries, Some(assistant_id));

        let result = nav.collect_entries_for_branch_summary(Some(assistant_id), user_id);
        assert_eq!(result.common_ancestor_id, Some(user_id));
        assert_eq!(result.entries.len(), 1);
    }

    #[test]
    fn test_label_timestamp() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));

        assert!(nav.get_label_timestamp(id).is_none());
        nav.append_label_change(id, Some("marker".to_string()));
        assert!(nav.get_label_timestamp(id).is_some());
    }

    #[test]
    fn test_label_replace() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));

        nav.append_label_change(id, Some("first".to_string()));
        assert_eq!(nav.get_label(id), Some("first"));

        nav.append_label_change(id, Some("second".to_string()));
        assert_eq!(nav.get_label(id), Some("second"));
    }

    #[test]
    fn test_label_nonexistent_entry_returns_nil() {
        let mut nav = SessionNavigator::new();
        let id = nav.append_label_change(Uuid::new_v4(), Some("ghost".to_string()));
        assert_eq!(id, Uuid::nil());
    }

    #[test]
    fn test_branch_with_summary_details() {
        let mut nav = SessionNavigator::new();
        let id = Uuid::new_v4();
        nav.add_entry(create_message(id, None, MessageRole::User, "Test"));
        nav.branch(id);

        let details = BranchSummaryDetails {
            read_files: vec!["a.rs".into()],
            modified_files: vec!["b.rs".into()],
        };
        let summary_id = nav.branch_with_summary(Some(id), "Summary".into(), Some(details), true);

        match nav.get_entry(summary_id) {
            Some(SessionEntryType::BranchSummary(e)) => {
                assert_eq!(e.summary, "Summary");
                assert!(e.from_hook.unwrap_or(false));
                assert!(e.details.is_some());
                let d = e.details.as_ref().unwrap();
                assert_eq!(d.read_files, vec!["a.rs"]);
                assert_eq!(d.modified_files, vec!["b.rs"]);
            }
            _ => panic!("Expected branch summary"),
        }
    }

    #[test]
    fn test_navigate_with_extension_cancel() {
        let mut nav = SessionNavigator::new();
        let root_id = Uuid::new_v4();
        let child_id = Uuid::new_v4();
        nav.add_entry(create_message(root_id, None, MessageRole::User, "R"));
        nav.add_entry(create_message(child_id, Some(root_id), MessageRole::Assistant, "C"));
        nav.branch(child_id);

        let hook = |_: TreePreparation| -> BeforeTreeHookResult {
            BeforeTreeHookResult {
                cancel: true,
                summary: None,
                custom_instructions: None,
                replace_instructions: None,
                label: None,
            }
        };

        let result = nav.navigate_tree(
            root_id,
            NavigationOptions::default(),
            None as Option<&NoOpSummarizer>,
            Some(&hook),
        );
        assert!(result.cancelled);
    }

    #[test]
    fn test_navigate_with_extension_summary() {
        let mut nav = SessionNavigator::new();
        let root_id = Uuid::new_v4();
        let child_id = Uuid::new_v4();
        nav.add_entry(create_message(root_id, None, MessageRole::User, "R"));
        nav.add_entry(create_message(child_id, Some(root_id), MessageRole::Assistant, "C"));
        nav.branch(child_id);

        let hook = |_: TreePreparation| -> BeforeTreeHookResult {
            BeforeTreeHookResult {
                cancel: false,
                summary: Some(ExtensionSummary {
                    summary: "Ext summary".into(),
                    details: None,
                }),
                custom_instructions: None,
                replace_instructions: None,
                label: None,
            }
        };

        let result = nav.navigate_tree(
            root_id,
            NavigationOptions {
                summarize: true,
                ..Default::default()
            },
            None as Option<&NoOpSummarizer>,
            Some(&hook),
        );
        assert!(!result.cancelled);
        assert!(result.summary_entry_id.is_some());

        let sid = result.summary_entry_id.unwrap();
        match nav.get_entry(sid) {
            Some(SessionEntryType::BranchSummary(e)) => {
                assert_eq!(e.summary, "Ext summary");
            }
            _ => panic!("Expected branch summary from extension"),
        }
    }

    #[test]
    fn test_message_role_checks() {
        assert!(MessageRole::User.is_user());
        assert!(!MessageRole::User.is_assistant());
        assert!(MessageRole::Assistant.is_assistant());
        assert!(!MessageRole::Assistant.is_user());
        assert!(!MessageRole::System.is_user());
        assert!(!MessageRole::Tool.is_user());
    }

    #[test]
    fn test_utility_functions() {
        let user_entry = create_message(Uuid::new_v4(), None, MessageRole::User, "hi");
        let asst_entry = create_message(Uuid::new_v4(), None, MessageRole::Assistant, "yo");
        let sys_entry = create_message(Uuid::new_v4(), None, MessageRole::System, "sys");

        assert!(is_user_message(&user_entry));
        assert!(!is_user_message(&asst_entry));
        assert!(is_assistant_message(&asst_entry));
        assert!(!is_assistant_message(&user_entry));
        assert!(!is_user_message(&sys_entry));
        assert!(!is_assistant_message(&sys_entry));
    }

    #[test]
    fn test_session_entry_type_accessors() {
        let id = Uuid::new_v4();
        let msg = SessionEntryType::Message(MessageEntry {
            id,
            parent_id: None,
            timestamp: 42,
            role: MessageRole::User,
            content: "test".into(),
        });

        match &msg {
            SessionEntryType::Message(e) => {
                assert_eq!(e.id, id);
                assert_eq!(e.parent_id, None);
                assert_eq!(e.timestamp, 42);
                assert_eq!(e.role, MessageRole::User);
                assert_eq!(e.content, "test");
            }
            _ => panic!("Expected Message"),
        }
    }

    #[test]
    fn test_get_all_entries() {
        let mut nav = SessionNavigator::new();
        let id1 = Uuid::new_v4();
        let id2 = Uuid::new_v4();
        nav.add_entry(create_message(id1, None, MessageRole::User, "A"));
        nav.add_entry(create_message(id2, Some(id1), MessageRole::Assistant, "B"));

        let all = nav.get_entries();
        assert_eq!(all.len(), 2);
    }
}