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
//! Integration tests for the PatternLearner knowledge graph
//!
//! Tests the complete learning lifecycle: observation -> voting -> promotion -> caching

use anyhow::Result;
use tempfile::TempDir;
use terraphim_session_analyzer::models::ToolCategory;
use terraphim_session_analyzer::patterns::knowledge_graph::{
    PatternLearner, infer_category_from_contexts,
};

#[test]
fn test_complete_learning_lifecycle() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Phase 1: Observe a tool multiple times
    learner.observe(
        "pytest".to_string(),
        "pytest tests/".to_string(),
        ToolCategory::Testing,
    );
    learner.observe(
        "pytest".to_string(),
        "pytest tests/ --verbose".to_string(),
        ToolCategory::Testing,
    );
    learner.observe(
        "pytest".to_string(),
        "pytest tests/ --cov".to_string(),
        ToolCategory::Testing,
    );

    assert_eq!(learner.candidate_count(), 1);

    // Phase 2: Promote candidates (threshold = 3)
    let promoted = learner.promote_candidates();

    assert_eq!(promoted.len(), 1);
    assert_eq!(promoted[0].tool_name, "pytest");
    assert!(matches!(promoted[0].category, ToolCategory::Testing));
    assert_eq!(promoted[0].observations, 3);
    assert!(promoted[0].confidence > 0.9); // All votes for Testing

    // After promotion, candidate should be removed
    assert_eq!(learner.candidate_count(), 0);

    Ok(())
}

#[test]
fn test_learning_with_conflicting_votes() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Observe tool with conflicting categorizations
    learner.observe(
        "custom-tool".to_string(),
        "custom-tool build".to_string(),
        ToolCategory::BuildTool,
    );
    learner.observe(
        "custom-tool".to_string(),
        "custom-tool test".to_string(),
        ToolCategory::Testing,
    );
    learner.observe(
        "custom-tool".to_string(),
        "custom-tool deploy".to_string(),
        ToolCategory::BuildTool,
    );

    let promoted = learner.promote_candidates();

    assert_eq!(promoted.len(), 1);
    // BuildTool should win (2 votes vs 1)
    assert!(matches!(promoted[0].category, ToolCategory::BuildTool));
    // Confidence should be 2/3 ≈ 0.67
    assert!((promoted[0].confidence - 0.67).abs() < 0.01);

    Ok(())
}

#[test]
fn test_multiple_tools_learning() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Observe multiple different tools
    for i in 0..3 {
        learner.observe(
            "pytest".to_string(),
            format!("pytest test_{i}.py"),
            ToolCategory::Testing,
        );
        learner.observe(
            "webpack".to_string(),
            format!("webpack build --mode production{i}"),
            ToolCategory::BuildTool,
        );
        learner.observe(
            "eslint".to_string(),
            format!("eslint src/{i}"),
            ToolCategory::Linting,
        );
    }

    assert_eq!(learner.candidate_count(), 3);

    let promoted = learner.promote_candidates();

    assert_eq!(promoted.len(), 3);
    assert_eq!(learner.candidate_count(), 0);

    // Verify each tool was categorized correctly
    let tool_names: Vec<&str> = promoted.iter().map(|p| p.tool_name.as_str()).collect();
    assert!(tool_names.contains(&"pytest"));
    assert!(tool_names.contains(&"webpack"));
    assert!(tool_names.contains(&"eslint"));

    Ok(())
}

#[test]
fn test_custom_threshold() -> Result<()> {
    let mut learner = PatternLearner::with_threshold(5);

    // Observe 4 times (below custom threshold of 5)
    for i in 0..4 {
        learner.observe(
            "custom".to_string(),
            format!("custom command {i}"),
            ToolCategory::Other("unknown".to_string()),
        );
    }

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 0); // Not enough observations

    // One more observation to meet threshold
    learner.observe(
        "custom".to_string(),
        "custom command 5".to_string(),
        ToolCategory::Other("unknown".to_string()),
    );

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 1);
    assert_eq!(promoted[0].observations, 5);

    Ok(())
}

#[test]
fn test_context_limit() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Observe tool with many different contexts (should limit to 10)
    for i in 0..20 {
        learner.observe(
            "tool".to_string(),
            format!("tool command variant {i}"),
            ToolCategory::Testing,
        );
    }

    let candidates = learner.get_candidates();
    assert_eq!(candidates.len(), 1);
    // Context list should be limited to 10
    assert!(candidates[0].contexts.len() <= 10);
    assert_eq!(candidates[0].observations, 20); // All observations counted

    Ok(())
}

#[test]
fn test_save_and_load_cache() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let _cache_path = temp_dir.path().join("learned_patterns.json");

    let mut learner = PatternLearner::new();

    // Create some learned patterns
    for i in 0..3 {
        learner.observe(
            "pytest".to_string(),
            format!("pytest test_{i}.py"),
            ToolCategory::Testing,
        );
    }

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 1);

    // Save to custom location (modify the save_to_cache to accept path for testing)
    // For now, just verify the patterns are created correctly
    assert_eq!(promoted[0].tool_name, "pytest");
    assert!(matches!(promoted[0].category, ToolCategory::Testing));

    Ok(())
}

#[test]
fn test_infer_category_testing_keywords() {
    let contexts = vec![
        "pytest tests/unit/".to_string(),
        "pytest tests/integration/".to_string(),
        "pytest --verbose --cov".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Testing));
}

#[test]
fn test_infer_category_build_tool_keywords() {
    let contexts = vec![
        "webpack build --mode production".to_string(),
        "vite build".to_string(),
        "rollup -c".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::BuildTool));
}

#[test]
fn test_infer_category_linting_keywords() {
    let contexts = vec![
        "eslint src/ --fix".to_string(),
        "cargo clippy".to_string(),
        "pylint mymodule".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Linting));
}

#[test]
fn test_infer_category_git_keywords() {
    let contexts = vec![
        "git commit -m 'message'".to_string(),
        "git push origin main".to_string(),
        "git pull --rebase".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Git));
}

#[test]
fn test_infer_category_package_manager_keywords() {
    let contexts = vec![
        "npm install express".to_string(),
        "yarn add lodash".to_string(),
        "cargo install ripgrep".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::PackageManager));
}

#[test]
fn test_infer_category_cloud_deploy_keywords() {
    let contexts = vec![
        "wrangler deploy --env production".to_string(),
        "vercel deploy".to_string(),
        "netlify deploy --prod".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::CloudDeploy));
}

#[test]
fn test_infer_category_database_keywords() {
    let contexts = vec![
        "migrate up".to_string(),
        "psql -d mydb".to_string(),
        "mysql -u root".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Database));
}

#[test]
fn test_infer_category_unknown_defaults_to_other() {
    let contexts = vec![
        "unknown-tool --flag".to_string(),
        "mystery command".to_string(),
    ];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Other(_)));
}

#[test]
fn test_repeated_context_not_duplicated() {
    let mut learner = PatternLearner::new();

    // Observe same command multiple times
    for _ in 0..5 {
        learner.observe(
            "tool".to_string(),
            "tool test".to_string(), // Same command
            ToolCategory::Testing,
        );
    }

    let candidates = learner.get_candidates();
    assert_eq!(candidates.len(), 1);
    // Should only store unique contexts
    assert_eq!(candidates[0].contexts.len(), 1);
    assert_eq!(candidates[0].observations, 5);
}

#[test]
fn test_confidence_all_same_votes() {
    let mut learner = PatternLearner::new();

    // All observations vote for the same category
    for i in 0..10 {
        learner.observe(
            "tool".to_string(),
            format!("tool test {i}"),
            ToolCategory::Testing,
        );
    }

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 1);
    // Perfect confidence: all votes for same category
    assert_eq!(promoted[0].confidence, 1.0);
}

#[test]
fn test_confidence_evenly_split_votes() {
    let mut learner = PatternLearner::new();

    // Split votes between two categories
    learner.observe(
        "tool".to_string(),
        "tool build 1".to_string(),
        ToolCategory::BuildTool,
    );
    learner.observe(
        "tool".to_string(),
        "tool build 2".to_string(),
        ToolCategory::BuildTool,
    );
    learner.observe(
        "tool".to_string(),
        "tool test".to_string(),
        ToolCategory::Testing,
    );
    learner.observe(
        "tool".to_string(),
        "tool test2".to_string(),
        ToolCategory::Testing,
    );

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 1);
    // 50% confidence (2 out of 4 votes for winner)
    assert_eq!(promoted[0].confidence, 0.5);
}

#[test]
fn test_learned_pattern_timestamp() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Observe and promote
    for i in 0..3 {
        learner.observe(
            "tool".to_string(),
            format!("tool {i}"),
            ToolCategory::Testing,
        );
    }

    let promoted = learner.promote_candidates();
    assert_eq!(promoted.len(), 1);

    // Verify learned_at timestamp is set and reasonable (not default/zero)
    let learned_at = &promoted[0].learned_at;
    assert!(learned_at.to_string().starts_with("20")); // Year starts with 20xx

    Ok(())
}

#[test]
fn test_multiple_promotion_rounds() -> Result<()> {
    let mut learner = PatternLearner::new();

    // Round 1: Add tool1 (meets threshold)
    for i in 0..3 {
        learner.observe(
            "tool1".to_string(),
            format!("tool1 {i}"),
            ToolCategory::Testing,
        );
    }

    let round1 = learner.promote_candidates();
    assert_eq!(round1.len(), 1);
    assert_eq!(round1[0].tool_name, "tool1");

    // Round 2: Add tool2 and tool3
    for i in 0..3 {
        learner.observe(
            "tool2".to_string(),
            format!("tool2 {i}"),
            ToolCategory::BuildTool,
        );
        learner.observe(
            "tool3".to_string(),
            format!("tool3 {i}"),
            ToolCategory::Linting,
        );
    }

    let round2 = learner.promote_candidates();
    assert_eq!(round2.len(), 2);

    Ok(())
}

#[test]
fn test_empty_contexts_infers_other() {
    let contexts: Vec<String> = Vec::new();
    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Other(_)));
}

#[test]
fn test_case_insensitive_context_inference() {
    let contexts = vec!["PYTEST TESTS/".to_string(), "PyTest --VERBOSE".to_string()];

    let category = infer_category_from_contexts(&contexts);
    assert!(matches!(category, ToolCategory::Testing));
}

#[test]
fn test_mixed_keywords_first_match_wins() {
    // When contexts contain multiple category keywords, first match in priority wins
    let contexts = vec![
        "npm install && webpack build".to_string(),
        "yarn add something".to_string(),
    ];

    // BuildTool (build, webpack) appears before PackageManager in priority
    let category = infer_category_from_contexts(&contexts);
    // This should match BuildTool because "build" keyword has higher priority
    assert!(matches!(category, ToolCategory::BuildTool));
}

#[test]
fn test_observation_updates_timestamps() -> Result<()> {
    let mut learner = PatternLearner::new();

    learner.observe(
        "tool".to_string(),
        "tool cmd".to_string(),
        ToolCategory::Testing,
    );

    let candidates1 = learner.get_candidates();
    let first_seen1 = candidates1[0].first_seen;
    let last_seen1 = candidates1[0].last_seen;

    // Timestamps should be equal on first observation
    assert_eq!(first_seen1, last_seen1);

    // Sleep briefly to ensure timestamp difference
    std::thread::sleep(std::time::Duration::from_millis(10));

    learner.observe(
        "tool".to_string(),
        "tool cmd2".to_string(),
        ToolCategory::Testing,
    );

    let candidates2 = learner.get_candidates();
    let first_seen2 = candidates2[0].first_seen;
    let last_seen2 = candidates2[0].last_seen;

    // First seen should not change
    assert_eq!(first_seen1, first_seen2);
    // Last seen should be updated
    assert!(last_seen2 > last_seen1);

    Ok(())
}