terraphim-session-analyzer 1.16.34

Analyze AI coding assistant session logs to identify agent usage patterns
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
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
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
use crate::models::{
    AgentAttribution, AgentInvocation, AgentStatistics, AgentToolCorrelation, AnalyzerConfig,
    CollaborationPattern, FileOperation, SessionAnalysis, ToolCategory, ToolInvocation,
    ToolStatistics,
};
use crate::parser::SessionParser;
use anyhow::Result;
use indexmap::IndexMap;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use tracing::{debug, info};

pub struct Analyzer {
    parsers: Vec<SessionParser>,
    config: AnalyzerConfig,
}

impl Analyzer {
    /// Create analyzer from a specific path (file or directory)
    ///
    /// # Errors
    ///
    /// Returns an error if the path doesn't exist or cannot be read
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let parsers = if path.is_file() {
            vec![SessionParser::from_file(path)?]
        } else if path.is_dir() {
            SessionParser::from_directory(path)?
        } else {
            return Err(anyhow::anyhow!("Path does not exist: {}", path.display()));
        };

        Ok(Self {
            parsers,
            config: AnalyzerConfig::default(),
        })
    }

    /// Create analyzer from default Claude session location
    ///
    /// # Errors
    ///
    /// Returns an error if the default Claude directory doesn't exist
    pub fn from_default_location() -> Result<Self> {
        let parsers = SessionParser::from_default_location()?;
        Ok(Self {
            parsers,
            config: AnalyzerConfig::default(),
        })
    }

    /// Set custom configuration
    /// Used in integration tests
    #[allow(dead_code)]
    #[must_use]
    pub fn with_config(mut self, config: AnalyzerConfig) -> Self {
        self.config = config;
        self
    }

    /// Analyze all sessions or filter by target file
    ///
    /// # Errors
    ///
    /// Returns an error if session parsing or analysis fails
    pub fn analyze(&self, target_file: Option<&str>) -> Result<Vec<SessionAnalysis>> {
        info!("Analyzing {} session(s)", self.parsers.len());

        let analyses: Result<Vec<_>> = self
            .parsers
            .par_iter()
            .filter_map(|parser| {
                match self.analyze_session(parser, target_file) {
                    Ok(analysis) => {
                        // If target file specified, only include sessions with relevant operations
                        if let Some(_target) = target_file {
                            if analysis.file_operations.is_empty() {
                                return None; // Skip sessions without target file operations
                            }
                        }
                        Some(Ok(analysis))
                    }
                    Err(e) => Some(Err(e)),
                }
            })
            .collect();

        analyses
    }

    /// Analyze a single session
    fn analyze_session(
        &self,
        parser: &SessionParser,
        target_file: Option<&str>,
    ) -> Result<SessionAnalysis> {
        let (session_id, project_path, start_time, end_time) = parser.get_session_info();

        debug!("Analyzing session: {}", session_id);

        // Extract raw data
        let mut agents = parser.extract_agent_invocations();
        let mut file_operations = parser.extract_file_operations();

        // Sort agents by timestamp for efficient binary search in set_agent_context
        agents.sort_by_key(|a| a.timestamp);

        // Set agent context for file operations
        self.set_agent_context(&mut file_operations, &agents, parser);

        // Calculate agent durations
        Self::calculate_agent_durations(&mut agents);

        // Filter by target file if specified
        if let Some(target) = target_file {
            file_operations.retain(|op| op.file_path.contains(target));

            // Also filter agents to only those that worked on the target file
            let relevant_agent_contexts: HashSet<&str> = file_operations
                .iter()
                .filter_map(|op| op.agent_context.as_deref())
                .collect();

            agents.retain(|agent| relevant_agent_contexts.contains(agent.agent_type.as_str()));
        }

        // Build file-to-agent attributions
        let file_to_agents = self.build_file_attributions(&file_operations, &agents);

        // Calculate agent statistics
        let agent_stats = Self::calculate_agent_statistics(&agents, &file_operations);

        // Detect collaboration patterns
        let collaboration_patterns = self.detect_collaboration_patterns(&agents);

        let duration_ms = if let (Some(start), Some(end)) = (start_time, end_time) {
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            {
                (end - start).total(jiff::Unit::Millisecond)? as u64
            }
        } else {
            0
        };

        Ok(SessionAnalysis {
            session_id,
            project_path,
            start_time: start_time.unwrap_or_else(jiff::Timestamp::now),
            end_time: end_time.unwrap_or_else(jiff::Timestamp::now),
            duration_ms,
            agents,
            file_operations,
            file_to_agents,
            agent_stats,
            collaboration_patterns,
        })
    }

    /// Set agent context for file operations based on temporal proximity
    /// Uses binary search for O(log n) lookup instead of O(n) linear search
    fn set_agent_context(
        &self,
        file_operations: &mut [FileOperation],
        agents: &[AgentInvocation],
        parser: &SessionParser,
    ) {
        // Ensure agents are sorted by timestamp for binary search
        // (they should already be sorted from extraction, but let's verify)
        debug_assert!(agents.windows(2).all(|w| w[0].timestamp <= w[1].timestamp));

        for file_op in file_operations.iter_mut() {
            // First try to find agent from parser's context lookup
            if let Some(agent) = parser.find_active_agent(&file_op.message_id) {
                file_op.agent_context = Some(agent);
                continue;
            }

            // Use binary search to find the most recent agent before this file operation
            // We're looking for the rightmost agent with timestamp <= file_op.timestamp
            let agent_idx = match agents.binary_search_by_key(&file_op.timestamp, |a| a.timestamp) {
                Ok(idx) => Some(idx), // Exact match
                Err(idx) => {
                    if idx > 0 {
                        Some(idx - 1) // Previous agent is the most recent before this operation
                    } else {
                        None // No agent before this operation
                    }
                }
            };

            if let Some(idx) = agent_idx {
                let agent = &agents[idx];
                let time_diff = file_op.timestamp - agent.timestamp;
                let time_diff_ms = time_diff.total(jiff::Unit::Millisecond).unwrap_or(0.0);
                let window_ms = self.config.file_attribution_window_ms;

                #[allow(clippy::cast_precision_loss)]
                if time_diff_ms <= (window_ms as f64) {
                    file_op.agent_context = Some(agent.agent_type.clone());
                }
            }
        }
    }

    /// Calculate durations for agent invocations
    fn calculate_agent_durations(agents: &mut [AgentInvocation]) {
        // Sort by timestamp for duration calculation
        agents.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));

        for i in 0..agents.len() {
            if i + 1 < agents.len() {
                let duration = agents[i + 1].timestamp - agents[i].timestamp;
                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                {
                    agents[i].duration_ms =
                        Some(duration.total(jiff::Unit::Millisecond).unwrap_or(0.0) as u64);
                }
            }
        }
    }

    /// Build file-to-agent attribution mapping
    fn build_file_attributions(
        &self,
        file_operations: &[FileOperation],
        _agents: &[AgentInvocation],
    ) -> IndexMap<String, Vec<AgentAttribution>> {
        let mut file_to_agents: IndexMap<String, Vec<AgentAttribution>> = IndexMap::new();

        // Group file operations by file path
        let mut file_groups: HashMap<String, Vec<&FileOperation>> = HashMap::new();
        for op in file_operations {
            // Skip files matching exclude patterns
            if self.should_exclude_file(&op.file_path) {
                continue;
            }

            file_groups
                .entry(op.file_path.clone())
                .or_default()
                .push(op);
        }

        // Calculate attributions for each file
        for (file_path, ops) in file_groups {
            let mut agent_contributions: HashMap<String, AgentContribution> = HashMap::new();

            for op in ops {
                if let Some(agent_type) = &op.agent_context {
                    let contribution = agent_contributions
                        .entry(agent_type.clone())
                        .or_insert_with(|| AgentContribution {
                            operations: Vec::new(),
                            first_interaction: op.timestamp,
                            last_interaction: op.timestamp,
                        });

                    contribution.operations.push(format!("{:?}", op.operation));
                    if op.timestamp < contribution.first_interaction {
                        contribution.first_interaction = op.timestamp;
                    }
                    if op.timestamp > contribution.last_interaction {
                        contribution.last_interaction = op.timestamp;
                    }
                }
            }

            // Convert to attributions with percentages
            #[allow(clippy::cast_precision_loss)]
            let total_ops = agent_contributions
                .values()
                .map(|c| c.operations.len())
                .sum::<usize>() as f32;

            if total_ops > 0.0 {
                let attributions: Vec<AgentAttribution> = agent_contributions
                    .into_iter()
                    .map(|(agent_type, contribution)| {
                        #[allow(clippy::cast_precision_loss)]
                        let contribution_percent =
                            (contribution.operations.len() as f32 / total_ops) * 100.0;
                        let confidence_score = Self::calculate_confidence_score(
                            &contribution.operations,
                            contribution_percent,
                        );

                        AgentAttribution {
                            agent_type,
                            contribution_percent,
                            confidence_score,
                            operations: contribution.operations,
                            first_interaction: contribution.first_interaction,
                            last_interaction: contribution.last_interaction,
                        }
                    })
                    .collect();

                file_to_agents.insert(file_path, attributions);
            }
        }

        file_to_agents
    }

    /// Calculate agent statistics
    fn calculate_agent_statistics(
        agents: &[AgentInvocation],
        file_operations: &[FileOperation],
    ) -> IndexMap<String, AgentStatistics> {
        let mut stats: IndexMap<String, AgentStatistics> = IndexMap::new();

        // Group agents by type
        let mut agent_groups: HashMap<String, Vec<&AgentInvocation>> = HashMap::new();
        for agent in agents {
            agent_groups
                .entry(agent.agent_type.clone())
                .or_default()
                .push(agent);
        }

        // Calculate statistics for each agent type
        for (agent_type, agent_list) in agent_groups {
            #[allow(clippy::cast_possible_truncation)]
            let total_invocations = agent_list.len() as u32;
            let total_duration_ms = agent_list.iter().filter_map(|a| a.duration_ms).sum::<u64>();

            #[allow(clippy::cast_possible_truncation)]
            let files_touched = file_operations
                .iter()
                .filter(|op| op.agent_context.as_ref() == Some(&agent_type))
                .map(|op| &op.file_path)
                .collect::<HashSet<_>>()
                .len() as u32;

            let tools_used = file_operations
                .iter()
                .filter(|op| op.agent_context.as_ref() == Some(&agent_type))
                .map(|op| format!("{:?}", op.operation))
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();

            let first_seen = agent_list
                .iter()
                .map(|a| a.timestamp)
                .min()
                .unwrap_or_else(jiff::Timestamp::now);
            let last_seen = agent_list
                .iter()
                .map(|a| a.timestamp)
                .max()
                .unwrap_or_else(jiff::Timestamp::now);

            stats.insert(
                agent_type.clone(),
                AgentStatistics {
                    agent_type,
                    total_invocations,
                    total_duration_ms,
                    files_touched,
                    tools_used,
                    first_seen,
                    last_seen,
                },
            );
        }

        stats
    }

    /// Detect collaboration patterns between agents
    fn detect_collaboration_patterns(
        &self,
        agents: &[AgentInvocation],
    ) -> Vec<CollaborationPattern> {
        let mut patterns = Vec::new();

        // Sequential pattern: architect -> developer -> test-writer
        let sequential_pattern = Self::detect_sequential_pattern(agents);
        if let Some(pattern) = sequential_pattern {
            patterns.push(pattern);
        }

        // Parallel pattern: multiple agents working simultaneously
        let parallel_pattern = Self::detect_parallel_pattern(agents);
        if let Some(pattern) = parallel_pattern {
            patterns.push(pattern);
        }

        patterns
    }

    /// Detect sequential collaboration patterns
    fn detect_sequential_pattern(agents: &[AgentInvocation]) -> Option<CollaborationPattern> {
        let common_sequences = vec![
            vec!["architect", "developer", "test-writer-fixer"],
            vec!["architect", "backend-architect", "developer"],
            vec!["rapid-prototyper", "developer", "technical-writer"],
        ];

        for sequence in common_sequences {
            if Self::matches_sequence(agents, &sequence) {
                return Some(CollaborationPattern {
                    pattern_type: "Sequential".to_string(),
                    agents: sequence.iter().map(|s| (*s).to_string()).collect(),
                    description: format!("Sequential workflow: {}", sequence.join(" → ")),
                    frequency: 1,
                    confidence: 0.8,
                });
            }
        }

        None
    }

    /// Detect parallel collaboration patterns
    fn detect_parallel_pattern(agents: &[AgentInvocation]) -> Option<CollaborationPattern> {
        // Group agents by time windows
        let window_ms = 300_000; // 5 minutes
        let mut time_groups: Vec<Vec<&AgentInvocation>> = Vec::new();

        for agent in agents {
            let mut found_group = false;
            for group in &mut time_groups {
                if let Some(first) = group.first() {
                    let time_diff = (agent.timestamp - first.timestamp)
                        .total(jiff::Unit::Millisecond)
                        .unwrap_or(0.0)
                        .abs();
                    if time_diff <= f64::from(window_ms) {
                        group.push(agent);
                        found_group = true;
                        break;
                    }
                }
            }
            if !found_group {
                time_groups.push(vec![agent]);
            }
        }

        // Find groups with multiple different agents
        for group in time_groups {
            let unique_agents: HashSet<&str> =
                group.iter().map(|a| a.agent_type.as_str()).collect();

            if unique_agents.len() >= 2 {
                return Some(CollaborationPattern {
                    pattern_type: "Parallel".to_string(),
                    agents: unique_agents.iter().map(|s| (*s).to_string()).collect(),
                    description: format!(
                        "Parallel collaboration: {}",
                        unique_agents
                            .iter()
                            .map(|s| (*s).to_string())
                            .collect::<Vec<_>>()
                            .join(" + ")
                    ),
                    frequency: 1,
                    confidence: 0.7,
                });
            }
        }

        None
    }

    /// Check if agents match a specific sequence
    fn matches_sequence(agents: &[AgentInvocation], sequence: &[&str]) -> bool {
        let agent_types: Vec<&str> = agents.iter().map(|a| a.agent_type.as_str()).collect();

        // Simple substring matching for now
        for window in agent_types.windows(sequence.len()) {
            if window == sequence {
                return true;
            }
        }

        false
    }

    /// Calculate confidence score for agent attribution
    fn calculate_confidence_score(operations: &[String], contribution_percent: f32) -> f32 {
        let mut confidence = 0.5; // Base confidence

        // Higher confidence for more operations
        #[allow(clippy::cast_precision_loss)]
        {
            confidence += (operations.len() as f32 * 0.1).min(0.3);
        }

        // Higher confidence for higher contribution percentage
        confidence += (contribution_percent / 100.0) * 0.4;

        // Boost confidence for write operations vs read operations
        #[allow(clippy::cast_precision_loss)]
        let write_ops = operations
            .iter()
            .filter(|op| matches!(op.as_str(), "Write" | "Edit" | "MultiEdit"))
            .count() as f32;
        #[allow(clippy::cast_precision_loss)]
        let total_ops = operations.len() as f32;

        if total_ops > 0.0 {
            let write_ratio = write_ops / total_ops;
            confidence += write_ratio * 0.2;
        }

        confidence.min(1.0)
    }

    /// Check if file should be excluded based on patterns
    fn should_exclude_file(&self, file_path: &str) -> bool {
        for pattern in &self.config.exclude_patterns {
            if file_path.contains(pattern) {
                return true;
            }
        }
        false
    }

    /// Get summary statistics across all sessions
    ///
    /// # Errors
    ///
    /// Returns an error if analysis fails
    pub fn get_summary_stats(&self) -> Result<SummaryStats> {
        let analyses = self.analyze(None)?;

        let total_sessions = analyses.len();
        let total_agents = analyses.iter().map(|a| a.agents.len()).sum::<usize>();
        let total_files = analyses
            .iter()
            .map(|a| a.file_to_agents.len())
            .sum::<usize>();

        let agent_types: HashSet<String> = analyses
            .iter()
            .flat_map(|a| a.agents.iter().map(|ag| ag.agent_type.clone()))
            .collect();

        Ok(SummaryStats {
            total_sessions,
            total_agents,
            total_files,
            unique_agent_types: agent_types.len(),
            most_active_agents: Self::get_most_active_agents(&analyses),
        })
    }

    /// Get most active agent types across all sessions
    fn get_most_active_agents(analyses: &[SessionAnalysis]) -> Vec<(String, u32)> {
        let mut agent_counts: HashMap<String, u32> = HashMap::new();

        for analysis in analyses {
            for agent in &analysis.agents {
                *agent_counts.entry(agent.agent_type.clone()).or_insert(0) += 1;
            }
        }

        let mut sorted: Vec<_> = agent_counts.into_iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(&a.1));
        sorted.into_iter().take(10).collect()
    }

    /// Calculate which agents use which tools
    /// Returns correlations sorted by usage count (descending)
    #[must_use]
    pub fn calculate_agent_tool_correlations(
        &self,
        tool_invocations: &[ToolInvocation],
    ) -> Vec<AgentToolCorrelation> {
        // Group tools by agent and tool name
        let mut agent_tool_map: HashMap<(String, String), AgentToolData> = HashMap::new();

        for invocation in tool_invocations {
            if let Some(agent) = &invocation.agent_context {
                let key = (agent.clone(), invocation.tool_name.clone());
                let data = agent_tool_map.entry(key).or_insert_with(|| AgentToolData {
                    usage_count: 0,
                    success_count: 0,
                    failure_count: 0,
                    session_count: HashSet::new(),
                });

                data.usage_count += 1;
                data.session_count.insert(invocation.session_id.clone());

                if let Some(exit_code) = invocation.exit_code {
                    if exit_code == 0 {
                        data.success_count += 1;
                    } else {
                        data.failure_count += 1;
                    }
                }
            }
        }

        // Calculate total tool usage per agent
        let mut agent_totals: HashMap<String, u32> = HashMap::new();
        for ((agent, _), data) in &agent_tool_map {
            *agent_totals.entry(agent.clone()).or_insert(0) += data.usage_count;
        }

        // Convert to correlations and calculate percentages
        let mut correlations: Vec<AgentToolCorrelation> = agent_tool_map
            .into_iter()
            .map(|((agent_type, tool_name), data)| {
                let total_attempts = data.success_count + data.failure_count;
                #[allow(clippy::cast_precision_loss)]
                let success_rate = if total_attempts > 0 {
                    (data.success_count as f32) / (total_attempts as f32)
                } else {
                    0.0
                };

                #[allow(clippy::cast_precision_loss)]
                let average_invocations_per_session = if !data.session_count.is_empty() {
                    (data.usage_count as f32) / (data.session_count.len() as f32)
                } else {
                    0.0
                };

                AgentToolCorrelation {
                    agent_type,
                    tool_name,
                    usage_count: data.usage_count,
                    success_rate,
                    average_invocations_per_session,
                }
            })
            .collect();

        // Sort by usage count descending
        correlations.sort_by(|a, b| b.usage_count.cmp(&a.usage_count));

        correlations
    }

    /// Calculate comprehensive tool statistics
    #[must_use]
    pub fn calculate_tool_statistics(
        &self,
        tool_invocations: &[ToolInvocation],
    ) -> IndexMap<String, ToolStatistics> {
        let mut tool_map: HashMap<String, ToolStatsData> = HashMap::new();

        for invocation in tool_invocations {
            let data = tool_map
                .entry(invocation.tool_name.clone())
                .or_insert_with(|| ToolStatsData {
                    category: invocation.tool_category.clone(),
                    total_invocations: 0,
                    agents_using: HashSet::new(),
                    success_count: 0,
                    failure_count: 0,
                    first_seen: invocation.timestamp,
                    last_seen: invocation.timestamp,
                    command_patterns: HashSet::new(),
                    sessions: HashSet::new(),
                });

            data.total_invocations += 1;
            data.sessions.insert(invocation.session_id.clone());

            if let Some(agent) = &invocation.agent_context {
                data.agents_using.insert(agent.clone());
            }

            if let Some(exit_code) = invocation.exit_code {
                if exit_code == 0 {
                    data.success_count += 1;
                } else {
                    data.failure_count += 1;
                }
            }

            if invocation.timestamp < data.first_seen {
                data.first_seen = invocation.timestamp;
            }
            if invocation.timestamp > data.last_seen {
                data.last_seen = invocation.timestamp;
            }

            // Extract common command patterns (first 100 chars of command)
            let pattern = if invocation.command_line.len() > 100 {
                invocation.command_line[..100].to_string()
            } else {
                invocation.command_line.clone()
            };
            data.command_patterns.insert(pattern);
        }

        // Convert to IndexMap for ordered results
        let mut stats: IndexMap<String, ToolStatistics> = tool_map
            .into_iter()
            .map(|(tool_name, data)| {
                #[allow(clippy::cast_possible_truncation)]
                let stats = ToolStatistics {
                    tool_name: tool_name.clone(),
                    category: data.category,
                    total_invocations: data.total_invocations,
                    agents_using: data.agents_using.into_iter().collect(),
                    success_count: data.success_count,
                    failure_count: data.failure_count,
                    first_seen: data.first_seen,
                    last_seen: data.last_seen,
                    command_patterns: data.command_patterns.into_iter().take(10).collect(),
                    sessions: data.sessions.into_iter().collect(),
                };
                (tool_name, stats)
            })
            .collect();

        // Sort by total invocations descending
        stats.sort_by(|_, v1, _, v2| v2.total_invocations.cmp(&v1.total_invocations));

        stats
    }

    /// Calculate category breakdown
    #[must_use]
    pub fn calculate_category_breakdown(
        &self,
        tool_invocations: &[ToolInvocation],
    ) -> IndexMap<ToolCategory, u32> {
        let mut category_counts: HashMap<ToolCategory, u32> = HashMap::new();

        for invocation in tool_invocations {
            *category_counts
                .entry(invocation.tool_category.clone())
                .or_insert(0) += 1;
        }

        // Convert to IndexMap and sort by count descending
        let mut breakdown: IndexMap<ToolCategory, u32> = category_counts.into_iter().collect();
        breakdown.sort_by(|_, v1, _, v2| v2.cmp(v1));

        breakdown
    }

    /// Detect sequential tool usage patterns (tool chains)
    ///
    /// # Arguments
    /// * `tool_invocations` - List of tool invocations to analyze
    ///
    /// # Returns
    /// A vector of `ToolChain` instances representing detected patterns
    ///
    /// # Algorithm
    /// 1. Group tools by session
    /// 2. Sort by timestamp within each session
    /// 3. Use sliding windows (2-5 tools) to find sequences
    /// 4. Group identical sequences across sessions
    /// 5. Calculate frequency, timing, and success rate
    /// 6. Filter chains that appear at least twice
    #[must_use]
    #[allow(dead_code)] // Will be used when tool chain analysis is exposed in CLI
    pub fn detect_tool_chains(
        &self,
        tool_invocations: &[ToolInvocation],
    ) -> Vec<crate::models::ToolChain> {
        use crate::models::ToolChain;

        // Group tools by session
        let mut session_tools: HashMap<String, Vec<&ToolInvocation>> = HashMap::new();
        for invocation in tool_invocations {
            session_tools
                .entry(invocation.session_id.clone())
                .or_default()
                .push(invocation);
        }

        // Track sequences: (tool_names) -> SequenceData
        let mut sequence_map: HashMap<Vec<String>, SequenceData> = HashMap::new();

        // Maximum time window between tools in a chain (1 hour = 3,600,000 ms)
        const MAX_TIME_BETWEEN_TOOLS_MS: u64 = 3_600_000;

        // Process each session
        for (_session_id, mut tools) in session_tools {
            // Sort by timestamp
            tools.sort_by_key(|t| t.timestamp);

            // Try different window sizes (2 to 5 tools)
            for window_size in 2..=5.min(tools.len()) {
                // Extract all consecutive sequences of this size
                for window in tools.windows(window_size) {
                    // Check if tools are within time window
                    let first_time = window[0].timestamp;
                    let last_time = window[window_size - 1].timestamp;

                    let time_diff = last_time - first_time;
                    #[allow(clippy::cast_sign_loss)]
                    let time_diff_ms = time_diff
                        .total(jiff::Unit::Millisecond)
                        .unwrap_or(0.0)
                        .abs() as u64;

                    if time_diff_ms > MAX_TIME_BETWEEN_TOOLS_MS {
                        continue; // Tools too far apart, skip
                    }

                    // Extract tool names
                    let tool_names: Vec<String> =
                        window.iter().map(|t| t.tool_name.clone()).collect();

                    // Get agent context (use first tool's agent)
                    let agent = window[0].agent_context.clone();

                    // Calculate time between tools
                    let mut time_diffs = Vec::new();
                    for i in 0..window.len() - 1 {
                        let diff = window[i + 1].timestamp - window[i].timestamp;
                        #[allow(clippy::cast_sign_loss)]
                        let diff_ms =
                            diff.total(jiff::Unit::Millisecond).unwrap_or(0.0).abs() as u64;
                        time_diffs.push(diff_ms);
                    }

                    // Calculate success rate
                    let total_with_exit_code =
                        window.iter().filter(|t| t.exit_code.is_some()).count();
                    let successful = window.iter().filter(|t| t.exit_code == Some(0)).count();

                    let data = sequence_map
                        .entry(tool_names)
                        .or_insert_with(SequenceData::new);
                    data.frequency += 1;
                    data.time_diffs.extend(time_diffs);
                    data.total_with_exit_code += total_with_exit_code;
                    data.successful += successful;

                    if let Some(agent) = agent {
                        *data.agent_counts.entry(agent).or_insert(0) += 1;
                    }
                }
            }
        }

        // Convert to ToolChain instances
        let mut chains: Vec<ToolChain> = sequence_map
            .into_iter()
            .filter(|(_, data)| data.frequency >= 2) // Must appear at least twice
            .map(|(tools, data)| {
                #[allow(clippy::cast_precision_loss)]
                let average_time_between_ms = if data.time_diffs.is_empty() {
                    0
                } else {
                    data.time_diffs.iter().sum::<u64>() / (data.time_diffs.len() as u64)
                };

                // Find most common agent
                let typical_agent = data
                    .agent_counts
                    .into_iter()
                    .max_by_key(|(_, count)| *count)
                    .map(|(agent, _)| agent);

                #[allow(clippy::cast_precision_loss)]
                let success_rate = if data.total_with_exit_code > 0 {
                    (data.successful as f32) / (data.total_with_exit_code as f32)
                } else {
                    0.0
                };

                ToolChain {
                    tools,
                    frequency: data.frequency,
                    average_time_between_ms,
                    typical_agent,
                    success_rate,
                }
            })
            .collect();

        // Sort by frequency descending
        chains.sort_by(|a, b| b.frequency.cmp(&a.frequency));

        chains
    }
}

#[derive(Debug)]
struct AgentContribution {
    operations: Vec<String>,
    first_interaction: jiff::Timestamp,
    last_interaction: jiff::Timestamp,
}

/// Helper struct for tracking agent-tool usage data
struct AgentToolData {
    usage_count: u32,
    success_count: u32,
    failure_count: u32,
    session_count: HashSet<String>,
}

/// Helper struct for tracking tool statistics data
struct ToolStatsData {
    category: ToolCategory,
    total_invocations: u32,
    agents_using: HashSet<String>,
    success_count: u32,
    failure_count: u32,
    first_seen: jiff::Timestamp,
    last_seen: jiff::Timestamp,
    command_patterns: HashSet<String>,
    sessions: HashSet<String>,
}

/// Helper struct for tracking tool chain sequence data
#[allow(dead_code)] // Used in tool chain detection
struct SequenceData {
    frequency: u32,
    time_diffs: Vec<u64>,
    agent_counts: HashMap<String, u32>,
    total_with_exit_code: usize,
    successful: usize,
}

#[allow(dead_code)] // Used in tool chain detection
impl SequenceData {
    fn new() -> Self {
        Self {
            frequency: 0,
            time_diffs: Vec::new(),
            agent_counts: HashMap::new(),
            total_with_exit_code: 0,
            successful: 0,
        }
    }
}

#[derive(Debug, Clone)]
pub struct SummaryStats {
    pub total_sessions: usize,
    pub total_agents: usize,
    pub total_files: usize,
    pub unique_agent_types: usize,
    pub most_active_agents: Vec<(String, u32)>,
}

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

    #[test]
    fn test_calculate_confidence_score() {
        let _analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let operations = vec!["Write".to_string(), "Edit".to_string()];
        let confidence = Analyzer::calculate_confidence_score(&operations, 75.0);

        assert!(confidence > 0.5);
        assert!(confidence <= 1.0);
    }

    #[test]
    fn test_should_exclude_file() {
        let config = AnalyzerConfig {
            exclude_patterns: vec!["node_modules/".to_string(), "target/".to_string()],
            ..Default::default()
        };

        let analyzer = Analyzer {
            parsers: vec![],
            config,
        };

        assert!(analyzer.should_exclude_file("node_modules/package.json"));
        assert!(analyzer.should_exclude_file("target/debug/main"));
        assert!(!analyzer.should_exclude_file("src/main.rs"));
    }

    #[test]
    fn test_calculate_agent_tool_correlations() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let tool_invocations = vec![
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec!["install".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("rust-expert".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-3".to_string(),
            },
        ];

        let correlations = analyzer.calculate_agent_tool_correlations(&tool_invocations);

        assert_eq!(correlations.len(), 2); // 2 unique agent-tool pairs
        assert_eq!(correlations[0].usage_count, 2); // developer-npm should be first (highest usage)
        assert_eq!(correlations[0].agent_type, "developer");
        assert_eq!(correlations[0].tool_name, "npm");
        assert_eq!(correlations[0].success_rate, 1.0); // Both npm calls succeeded
        assert_eq!(correlations[1].usage_count, 1);
        assert_eq!(correlations[1].agent_type, "rust-expert");
    }

    #[test]
    fn test_calculate_tool_statistics() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let tool_invocations = vec![
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec!["install".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(1),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
        ];

        let stats = analyzer.calculate_tool_statistics(&tool_invocations);

        assert_eq!(stats.len(), 1); // Only npm
        let npm_stats = stats.get("npm").unwrap();
        assert_eq!(npm_stats.total_invocations, 2);
        assert_eq!(npm_stats.success_count, 1);
        assert_eq!(npm_stats.failure_count, 1);
        assert!(npm_stats.agents_using.contains(&"developer".to_string()));
        assert!(matches!(npm_stats.category, ToolCategory::PackageManager));
    }

    #[test]
    fn test_calculate_category_breakdown() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let tool_invocations = vec![
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec![],
                flags: HashMap::new(),
                exit_code: None,
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec![],
                flags: HashMap::new(),
                exit_code: None,
                agent_context: Some("rust-expert".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo test".to_string(),
                arguments: vec![],
                flags: HashMap::new(),
                exit_code: None,
                agent_context: Some("rust-expert".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-3".to_string(),
            },
        ];

        let breakdown = analyzer.calculate_category_breakdown(&tool_invocations);

        assert_eq!(breakdown.len(), 2);
        // BuildTool should be first (2 invocations)
        let categories: Vec<_> = breakdown.keys().collect();
        assert!(matches!(categories[0], ToolCategory::BuildTool));
        assert_eq!(breakdown[categories[0]], 2);
        assert_eq!(breakdown[categories[1]], 1);
    }

    #[test]
    fn test_detect_tool_chains_basic() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let one_sec = jiff::Span::new().seconds(1);

        // Create a chain that appears twice: cargo build -> cargo test
        let tool_invocations = vec![
            // First occurrence
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(one_sec).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "cargo test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
            // Second occurrence
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().seconds(10)).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-3".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().seconds(11)).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "cargo test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-4".to_string(),
            },
        ];

        let chains = analyzer.detect_tool_chains(&tool_invocations);

        assert!(!chains.is_empty(), "Should detect at least one chain");

        // Find the chain with the exact pattern we're looking for
        let cargo_chain = chains
            .iter()
            .find(|c| c.tools == vec!["cargo".to_string(), "cargo".to_string()]);
        assert!(cargo_chain.is_some(), "Should find cargo->cargo chain");

        let chain = cargo_chain.unwrap();
        // With 4 tools, we get 3 overlapping windows: [0,1], [1,2], [2,3]
        assert!(chain.frequency >= 2, "Frequency should be at least 2");
        assert_eq!(chain.typical_agent, Some("developer".to_string()));
        assert_eq!(chain.success_rate, 1.0);
    }

    #[test]
    fn test_detect_tool_chains_deployment_pipeline() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let one_sec = jiff::Span::new().seconds(1);

        // Create deployment pipeline: npm install -> npm build -> wrangler deploy
        // Appears twice across different sessions
        let tool_invocations = vec![
            // Session 1 - First occurrence
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec!["install".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(one_sec).unwrap(),
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "npm build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().seconds(2)).unwrap(),
                tool_name: "wrangler".to_string(),
                tool_category: ToolCategory::CloudDeploy,
                command_line: "wrangler deploy".to_string(),
                arguments: vec!["deploy".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-3".to_string(),
            },
            // Session 2 - Second occurrence
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().minutes(10)).unwrap(),
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec!["install".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-2".to_string(),
                message_id: "msg-4".to_string(),
            },
            ToolInvocation {
                timestamp: now
                    .checked_add(jiff::Span::new().minutes(10).seconds(1))
                    .unwrap(),
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "npm build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-2".to_string(),
                message_id: "msg-5".to_string(),
            },
            ToolInvocation {
                timestamp: now
                    .checked_add(jiff::Span::new().minutes(10).seconds(2))
                    .unwrap(),
                tool_name: "wrangler".to_string(),
                tool_category: ToolCategory::CloudDeploy,
                command_line: "wrangler deploy".to_string(),
                arguments: vec!["deploy".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("devops".to_string()),
                session_id: "session-2".to_string(),
                message_id: "msg-6".to_string(),
            },
        ];

        let chains = analyzer.detect_tool_chains(&tool_invocations);

        assert!(!chains.is_empty(), "Should detect deployment chain");

        // Find the 3-tool chain (npm -> npm -> wrangler)
        let three_tool_chain = chains.iter().find(|c| c.tools.len() == 3);
        assert!(three_tool_chain.is_some(), "Should find 3-tool chain");

        let chain = three_tool_chain.unwrap();
        assert_eq!(
            chain.tools,
            vec!["npm".to_string(), "npm".to_string(), "wrangler".to_string()]
        );
        assert_eq!(chain.frequency, 2);
        assert_eq!(chain.typical_agent, Some("devops".to_string()));
        assert_eq!(chain.success_rate, 1.0);
    }

    #[test]
    fn test_detect_tool_chains_ignores_single_occurrence() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let one_sec = jiff::Span::new().seconds(1);

        // Create a chain that appears only once
        let tool_invocations = vec![
            ToolInvocation {
                timestamp: now,
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::PackageManager,
                command_line: "npm install".to_string(),
                arguments: vec!["install".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(one_sec).unwrap(),
                tool_name: "npm".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "npm test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
        ];

        let chains = analyzer.detect_tool_chains(&tool_invocations);

        // Should be empty because chain appears only once
        assert!(
            chains.is_empty(),
            "Should not detect chains that appear only once"
        );
    }

    #[test]
    fn test_detect_tool_chains_time_window() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();

        // Create tools that are too far apart (> 1 hour)
        let tool_invocations = vec![
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().hours(2)).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "cargo test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
        ];

        let chains = analyzer.detect_tool_chains(&tool_invocations);

        // Should be empty because tools are too far apart
        assert!(
            chains.is_empty(),
            "Should not detect chains with tools too far apart"
        );
    }

    #[test]
    fn test_detect_tool_chains_success_rate() {
        use crate::models::{ToolCategory, ToolInvocation};
        use jiff::Timestamp;

        let analyzer = Analyzer {
            parsers: vec![],
            config: AnalyzerConfig::default(),
        };

        let now = Timestamp::now();
        let one_sec = jiff::Span::new().seconds(1);

        // Create a chain with mixed success: 2 occurrences, 1 success, 1 failure
        let tool_invocations = vec![
            // First occurrence - success
            ToolInvocation {
                timestamp: now,
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-1".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(one_sec).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "cargo test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-2".to_string(),
            },
            // Second occurrence - failure on test
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().seconds(10)).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::BuildTool,
                command_line: "cargo build".to_string(),
                arguments: vec!["build".to_string()],
                flags: HashMap::new(),
                exit_code: Some(0),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-3".to_string(),
            },
            ToolInvocation {
                timestamp: now.checked_add(jiff::Span::new().seconds(11)).unwrap(),
                tool_name: "cargo".to_string(),
                tool_category: ToolCategory::Testing,
                command_line: "cargo test".to_string(),
                arguments: vec!["test".to_string()],
                flags: HashMap::new(),
                exit_code: Some(1),
                agent_context: Some("developer".to_string()),
                session_id: "session-1".to_string(),
                message_id: "msg-4".to_string(),
            },
        ];

        let chains = analyzer.detect_tool_chains(&tool_invocations);

        assert!(!chains.is_empty(), "Should detect chain");

        // Find the cargo->cargo chain
        let cargo_chain = chains
            .iter()
            .find(|c| c.tools == vec!["cargo".to_string(), "cargo".to_string()]);
        assert!(cargo_chain.is_some(), "Should find cargo->cargo chain");

        let chain = cargo_chain.unwrap();
        assert!(chain.frequency >= 2, "Frequency should be at least 2");
        // With overlapping windows, we have:
        // Window [0,1]: cargo(exit 0) + cargo(exit 0) = 2 success, 0 failure
        // Window [1,2]: cargo(exit 0) + cargo(exit 0) = 2 success, 0 failure
        // Window [2,3]: cargo(exit 0) + cargo(exit 1) = 1 success, 1 failure
        // Total: 5 successes out of 6 tools = 0.833...
        assert!(
            chain.success_rate >= 0.82 && chain.success_rate <= 0.84,
            "Success rate should be around 0.83, got {}",
            chain.success_rate
        );
    }
}