claudectl 0.49.2

Mission control for Claude Code — supervise, orchestrate, and connect coding agents with a local LLM brain and hive mind
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
#![allow(dead_code)]

use std::collections::HashMap;

use super::decisions::{DecisionRecord, DistilledPreferences};
use super::insights::{Insight, InsightCategory, InsightSeverity, epoch_now};

// ────────────────────────────────────────────────────────────────────────────
// Detection algorithms
// ────────────────────────────────────────────────────────────────────────────

/// Extract a command keyword for grouping (first two tokens).
/// Duplicated from decisions.rs because that function is private.
pub(crate) fn extract_command_keyword(command: Option<&str>) -> Option<String> {
    let cmd = command?.trim();
    if cmd.is_empty() {
        return None;
    }
    let tokens: Vec<&str> = cmd.split_whitespace().take(2).collect();
    Some(tokens.join(" "))
}

/// Detect tools/commands that are repeatedly rejected by the user.
pub(crate) fn detect_friction_patterns(decisions: &[DecisionRecord]) -> Vec<Insight> {
    let mut groups: HashMap<(String, Option<String>), (u32, u32)> = HashMap::new();

    for d in decisions {
        let tool = d.tool.clone().unwrap_or_else(|| "*".to_string());
        let cmd = extract_command_keyword(d.command.as_deref());
        let key = (tool, cmd);
        let entry = groups.entry(key).or_insert((0, 0));
        entry.0 += 1; // total
        if d.is_negative() {
            entry.1 += 1; // rejected
        }
    }

    let now = epoch_now();
    let mut insights = Vec::new();

    for ((tool, cmd), (total, rejected)) in &groups {
        if *rejected < 3 || *total < 3 {
            continue;
        }
        let rejection_rate = *rejected as f64 / *total as f64;
        if rejection_rate < 0.6 {
            continue;
        }

        let cmd_part = cmd
            .as_ref()
            .map(|c| format!(" \"{c}\""))
            .unwrap_or_default();

        let severity = if rejection_rate >= 0.9 {
            InsightSeverity::Warning
        } else {
            InsightSeverity::Suggestion
        };

        insights.push(Insight {
            fingerprint: format!("friction:{}:{}", tool, cmd.as_deref().unwrap_or("*")),
            generated_at: now,
            category: InsightCategory::FrictionPattern,
            severity,
            summary: format!(
                "[{tool}]{cmd_part} rejected {rejected}/{total} times ({:.0}%)",
                rejection_rate * 100.0
            ),
            suggestion: Some(format!("consider adding deny rule for [{tool}]{cmd_part}")),
            evidence_count: *total,
        });
    }

    insights
}

/// Detect repeated errors from the same tool across sessions.
pub(crate) fn detect_error_loops(decisions: &[DecisionRecord]) -> Vec<Insight> {
    // Group by PID, then find consecutive errors for same tool
    let mut pid_groups: HashMap<u32, Vec<&DecisionRecord>> = HashMap::new();
    for d in decisions {
        pid_groups.entry(d.pid).or_default().push(d);
    }

    // Count how many sessions had error loops for each (tool, cmd) combo
    let mut loop_counts: HashMap<(String, Option<String>), u32> = HashMap::new();

    for session_decisions in pid_groups.values() {
        let mut streak_tool: Option<String> = None;
        let mut streak_cmd: Option<String> = None;
        let mut streak_count: u32 = 0;

        for d in session_decisions {
            let has_error = d
                .context
                .as_ref()
                .map(|c| c.last_tool_error)
                .unwrap_or(false);
            let tool = d.tool.clone().unwrap_or_default();
            let cmd = extract_command_keyword(d.command.as_deref());

            if has_error && Some(&tool) == streak_tool.as_ref() {
                streak_count += 1;
            } else if has_error {
                // New error streak
                streak_tool = Some(tool.clone());
                streak_cmd = cmd.clone();
                streak_count = 1;
            } else {
                // No error — check if previous streak was long enough
                if streak_count >= 3 {
                    if let Some(ref t) = streak_tool {
                        *loop_counts
                            .entry((t.clone(), streak_cmd.clone()))
                            .or_insert(0) += 1;
                    }
                }
                streak_tool = None;
                streak_cmd = None;
                streak_count = 0;
            }
        }
        // Check trailing streak
        if streak_count >= 3 {
            if let Some(ref t) = streak_tool {
                *loop_counts
                    .entry((t.clone(), streak_cmd.clone()))
                    .or_insert(0) += 1;
            }
        }
    }

    let now = epoch_now();
    loop_counts
        .into_iter()
        .filter(|(_, count)| *count >= 1)
        .map(|((tool, cmd), count)| {
            let cmd_part = cmd
                .as_ref()
                .map(|c| format!(" \"{c}\""))
                .unwrap_or_default();
            Insight {
                fingerprint: format!("error_loop:{}:{}", tool, cmd.as_deref().unwrap_or("*")),
                generated_at: now,
                category: InsightCategory::ErrorLoop,
                severity: if count >= 3 {
                    InsightSeverity::Warning
                } else {
                    InsightSeverity::Suggestion
                },
                summary: format!(
                    "[{tool}]{cmd_part} hit 3+ consecutive errors in {count} session(s)"
                ),
                suggestion: Some(format!("investigate why [{tool}]{cmd_part} keeps failing")),
                evidence_count: count,
            }
        })
        .collect()
}

/// Detect sessions frequently hitting high context usage.
pub(crate) fn detect_context_blowouts(decisions: &[DecisionRecord]) -> Vec<Insight> {
    // Group by PID, check if any decision in session had context > 80%
    let mut pid_max_context: HashMap<u32, u8> = HashMap::new();
    for d in decisions {
        if let Some(ref ctx) = d.context {
            let entry = pid_max_context.entry(d.pid).or_insert(0);
            if ctx.context_pct > *entry {
                *entry = ctx.context_pct;
            }
        }
    }

    if pid_max_context.is_empty() {
        return Vec::new();
    }

    // Only look at recent sessions (last 20 PIDs by insertion order)
    let recent: Vec<u8> = pid_max_context
        .values()
        .copied()
        .collect::<Vec<_>>()
        .into_iter()
        .rev()
        .take(20)
        .collect();

    let blowout_count = recent.iter().filter(|&&pct| pct > 80).count();
    let total = recent.len();
    let blowout_rate = blowout_count as f64 / total as f64;

    if blowout_rate < 0.4 || blowout_count < 2 {
        return Vec::new();
    }

    vec![Insight {
        fingerprint: "context_blowout:global".to_string(),
        generated_at: epoch_now(),
        category: InsightCategory::ContextBlowout,
        severity: if blowout_rate >= 0.7 {
            InsightSeverity::Warning
        } else {
            InsightSeverity::Suggestion
        },
        summary: format!(
            "Context >80% in {blowout_count}/{total} recent sessions ({:.0}%)",
            blowout_rate * 100.0
        ),
        suggestion: Some("consider earlier /compact when context approaches 70%".to_string()),
        evidence_count: blowout_count as u32,
    }]
}

/// Detect high-confidence patterns that could become AutoRules.
pub(crate) fn detect_missing_rules(
    _decisions: &[DecisionRecord],
    prefs: &DistilledPreferences,
) -> Vec<Insight> {
    let now = epoch_now();
    let mut insights = Vec::new();

    for p in &prefs.patterns {
        if p.sample_count < 5 || p.confidence < 0.8 {
            continue;
        }

        // High-confidence approve patterns
        if p.accept_rate >= 0.9 {
            let cmd_part = p
                .command_pattern
                .as_ref()
                .map(|c| format!(" \"{c}\""))
                .unwrap_or_default();

            insights.push(Insight {
                fingerprint: format!(
                    "missing_rule:approve:{}:{}",
                    p.tool,
                    p.command_pattern.as_deref().unwrap_or("*")
                ),
                generated_at: now,
                category: InsightCategory::MissingRule,
                severity: InsightSeverity::Suggestion,
                summary: format!(
                    "approve [{}]{cmd_part} (accepted {:.0}%, n={})",
                    p.tool,
                    p.accept_rate * 100.0,
                    p.sample_count,
                ),
                suggestion: Some(format!(
                    "add to .claudectl.toml: [[rules]] match_tool=\"{}\" match_command=\"{}\" action=\"approve\"",
                    p.tool,
                    p.command_pattern.as_deref().unwrap_or("*"),
                )),
                evidence_count: p.sample_count,
            });
        }

        // High-confidence deny patterns
        if p.accept_rate <= 0.1 {
            let cmd_part = p
                .command_pattern
                .as_ref()
                .map(|c| format!(" \"{c}\""))
                .unwrap_or_default();

            insights.push(Insight {
                fingerprint: format!(
                    "missing_rule:deny:{}:{}",
                    p.tool,
                    p.command_pattern.as_deref().unwrap_or("*")
                ),
                generated_at: now,
                category: InsightCategory::MissingRule,
                severity: InsightSeverity::Suggestion,
                summary: format!(
                    "deny [{}]{cmd_part} (rejected {:.0}%, n={})",
                    p.tool,
                    (1.0 - p.accept_rate) * 100.0,
                    p.sample_count,
                ),
                suggestion: Some(format!(
                    "add to .claudectl.toml: [[rules]] match_tool=\"{}\" match_command=\"{}\" action=\"deny\"",
                    p.tool,
                    p.command_pattern.as_deref().unwrap_or("*"),
                )),
                evidence_count: p.sample_count,
            });
        }
    }

    insights
}

/// Detect tools where brain accuracy is low.
pub(crate) fn detect_accuracy_gaps(prefs: &DistilledPreferences) -> Vec<Insight> {
    let now = epoch_now();
    prefs
        .tool_accuracy
        .iter()
        .filter(|ta| ta.total >= 5 && ta.confidence_threshold > 0.7)
        .map(|ta| {
            let accuracy = if ta.total > 0 {
                (ta.correct as f64 / ta.total as f64) * 100.0
            } else {
                0.0
            };
            Insight {
                fingerprint: format!("accuracy_gap:{}", ta.tool),
                generated_at: now,
                category: InsightCategory::AccuracyGap,
                severity: if accuracy < 50.0 {
                    InsightSeverity::Warning
                } else {
                    InsightSeverity::Suggestion
                },
                summary: format!(
                    "Brain accuracy for [{}] is {:.0}% (threshold raised to {:.2})",
                    ta.tool, accuracy, ta.confidence_threshold,
                ),
                suggestion: Some(format!(
                    "more training data needed for [{}] — brain defers these to manual review",
                    ta.tool,
                )),
                evidence_count: ta.total,
            }
        })
        .collect()
}

/// Convert temporal patterns from distillation into insights.
pub(crate) fn detect_temporal_friction(prefs: &DistilledPreferences) -> Vec<Insight> {
    let now = epoch_now();
    prefs
        .temporal
        .iter()
        .filter(|tp| tp.strength > 0.3 && tp.sample_count >= 3)
        .map(|tp| {
            // Use first 40 chars of description as fingerprint suffix
            let fp_suffix: String = tp
                .description
                .chars()
                .filter(|c| c.is_alphanumeric() || *c == ' ')
                .take(40)
                .collect::<String>()
                .replace(' ', "_");
            Insight {
                fingerprint: format!("temporal:{fp_suffix}"),
                generated_at: now,
                category: InsightCategory::TemporalFriction,
                severity: InsightSeverity::Info,
                summary: tp.description.clone(),
                suggestion: None,
                evidence_count: tp.sample_count,
            }
        })
        .collect()
}

/// Detect increasing burn rate trends and cost outliers.
pub(crate) fn detect_cost_patterns(decisions: &[DecisionRecord]) -> Vec<Insight> {
    let burn_rates: Vec<f64> = decisions
        .iter()
        .filter_map(|d| d.context.as_ref().map(|c| c.burn_rate_per_hr))
        .filter(|r| *r > 0.0)
        .collect();

    if burn_rates.len() < 10 {
        return Vec::new();
    }

    let mut insights = Vec::new();
    let now = epoch_now();

    // Compare first half vs second half burn rates
    let mid = burn_rates.len() / 2;
    let first_avg: f64 = burn_rates[..mid].iter().sum::<f64>() / mid as f64;
    let second_avg: f64 = burn_rates[mid..].iter().sum::<f64>() / (burn_rates.len() - mid) as f64;

    if first_avg > 0.0 {
        let increase = (second_avg - first_avg) / first_avg;
        if increase > 0.5 {
            insights.push(Insight {
                fingerprint: "cost_trend:increasing".to_string(),
                generated_at: now,
                category: InsightCategory::CostPattern,
                severity: if increase > 1.0 {
                    InsightSeverity::Warning
                } else {
                    InsightSeverity::Suggestion
                },
                summary: format!(
                    "Burn rate trending up: ${:.2}/hr -> ${:.2}/hr ({:+.0}%)",
                    first_avg,
                    second_avg,
                    increase * 100.0,
                ),
                suggestion: Some(
                    "consider setting a budget with --budget or reviewing costly operations"
                        .to_string(),
                ),
                evidence_count: burn_rates.len() as u32,
            });
        }
    }

    // Detect cost outlier sessions
    let mut per_session_cost: HashMap<u32, f64> = HashMap::new();
    for d in decisions {
        if let Some(ref ctx) = d.context {
            let entry = per_session_cost.entry(d.pid).or_insert(0.0);
            if ctx.cost_usd > *entry {
                *entry = ctx.cost_usd;
            }
        }
    }

    if per_session_cost.len() >= 3 {
        let costs: Vec<f64> = per_session_cost.values().copied().collect();
        let avg: f64 = costs.iter().sum::<f64>() / costs.len() as f64;
        let outlier_count = costs.iter().filter(|&&c| c > avg * 2.0 && c > 1.0).count();

        if outlier_count >= 2 {
            insights.push(Insight {
                fingerprint: "cost_trend:outliers".to_string(),
                generated_at: now,
                category: InsightCategory::CostPattern,
                severity: InsightSeverity::Info,
                summary: format!(
                    "{outlier_count} sessions cost >2x average (avg ${avg:.2})"
                ),
                suggestion: Some(
                    "review high-cost sessions — consider budget limits or earlier session restarts"
                        .to_string(),
                ),
                evidence_count: outlier_count as u32,
            });
        }
    }

    insights
}

// ────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::brain::decisions::{
        DecisionContext, DecisionType, DistilledPreferences, PreferencePattern, TemporalPattern,
        ToolAccuracy,
    };

    fn make_decision(tool: &str, command: &str, user_action: &str, pid: u32) -> DecisionRecord {
        DecisionRecord {
            timestamp: "0".to_string(),
            pid,
            project: "test".to_string(),
            tool: Some(tool.to_string()),
            command: Some(command.to_string()),
            brain_action: "approve".to_string(),
            brain_confidence: 0.8,
            brain_reasoning: String::new(),
            user_action: user_action.to_string(),
            context: None,
            outcome: None,
            decision_type: DecisionType::Session,
            suggested_at: None,
            resolved_at: None,
            override_reason: None,
            decision_id: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn make_decision_with_context(
        tool: &str,
        command: &str,
        user_action: &str,
        pid: u32,
        context_pct: u8,
        last_error: bool,
        burn_rate: f64,
        cost: f64,
    ) -> DecisionRecord {
        let mut d = make_decision(tool, command, user_action, pid);
        d.context = Some(DecisionContext {
            cost_usd: cost,
            context_pct,
            last_tool_error: last_error,
            error_message: None,
            model: "test".to_string(),
            elapsed_secs: 100,
            files_modified_count: 0,
            total_tool_calls: 10,
            has_file_conflict: false,
            status: "Processing".to_string(),
            burn_rate_per_hr: burn_rate,
            recent_error_count: 0,
            subagent_count: 0,
            hour: Some(10),
        });
        d
    }

    #[test]
    fn test_friction_patterns_detected() {
        let decisions: Vec<DecisionRecord> = (0..10)
            .map(|i| make_decision("Bash", "npm install", "reject", i))
            .collect();

        let insights = detect_friction_patterns(&decisions);
        assert_eq!(insights.len(), 1);
        assert_eq!(insights[0].category, InsightCategory::FrictionPattern);
        assert!(insights[0].summary.contains("npm install"));
        assert!(insights[0].summary.contains("10/10"));
    }

    #[test]
    fn test_friction_below_threshold_not_detected() {
        // 2 rejections out of 5 = 40%, below 60% threshold
        let mut decisions = Vec::new();
        for i in 0..3 {
            decisions.push(make_decision("Bash", "cargo test", "accept", i));
        }
        for i in 3..5 {
            decisions.push(make_decision("Bash", "cargo test", "reject", i));
        }

        let insights = detect_friction_patterns(&decisions);
        assert!(insights.is_empty());
    }

    #[test]
    fn test_error_loops_detected() {
        // 4 consecutive errors for same tool in one session
        let decisions: Vec<DecisionRecord> = (0..4)
            .map(|_| {
                make_decision_with_context(
                    "Write",
                    "src/main.rs",
                    "accept",
                    100,
                    50,
                    true,
                    1.0,
                    0.5,
                )
            })
            .collect();

        let insights = detect_error_loops(&decisions);
        assert_eq!(insights.len(), 1);
        assert_eq!(insights[0].category, InsightCategory::ErrorLoop);
    }

    #[test]
    fn test_context_blowouts_detected() {
        // 5 sessions all hitting >80% context
        let decisions: Vec<DecisionRecord> = (0..5)
            .map(|pid| {
                make_decision_with_context("Read", "file.rs", "accept", pid, 85, false, 1.0, 0.5)
            })
            .collect();

        let insights = detect_context_blowouts(&decisions);
        assert_eq!(insights.len(), 1);
        assert_eq!(insights[0].category, InsightCategory::ContextBlowout);
    }

    #[test]
    fn test_missing_rules_detected() {
        let prefs = DistilledPreferences {
            patterns: vec![
                PreferencePattern {
                    tool: "Bash".to_string(),
                    command_pattern: Some("cargo test".to_string()),
                    preferred_action: "approve".to_string(),
                    sample_count: 15,
                    accept_rate: 1.0,
                    conditions: Vec::new(),
                    confidence: 1.0,
                },
                PreferencePattern {
                    tool: "Bash".to_string(),
                    command_pattern: Some("rm -rf".to_string()),
                    preferred_action: "deny".to_string(),
                    sample_count: 6,
                    accept_rate: 0.0,
                    conditions: Vec::new(),
                    confidence: 1.0,
                },
            ],
            tool_accuracy: Vec::new(),
            total_decisions: 21,
            overall_accuracy: 0.8,
            temporal: Vec::new(),
        };

        let insights = detect_missing_rules(&[], &prefs);
        assert_eq!(insights.len(), 2);
        assert!(insights.iter().any(|i| i.summary.contains("cargo test")));
        assert!(insights.iter().any(|i| i.summary.contains("rm -rf")));
    }

    #[test]
    fn test_accuracy_gaps_detected() {
        let prefs = DistilledPreferences {
            patterns: Vec::new(),
            tool_accuracy: vec![ToolAccuracy {
                tool: "Edit".to_string(),
                total: 10,
                correct: 4,
                confidence_threshold: 0.85,
            }],
            total_decisions: 10,
            overall_accuracy: 0.4,
            temporal: Vec::new(),
        };

        let insights = detect_accuracy_gaps(&prefs);
        assert_eq!(insights.len(), 1);
        assert!(insights[0].summary.contains("Edit"));
        assert!(insights[0].summary.contains("40%"));
    }

    #[test]
    fn test_cost_trend_detected() {
        let mut decisions = Vec::new();
        // First 10: low burn rate
        for i in 0..10 {
            decisions.push(make_decision_with_context(
                "Bash", "cmd", "accept", i, 50, false, 1.0, 0.5,
            ));
        }
        // Next 10: high burn rate (3x increase)
        for i in 10..20 {
            decisions.push(make_decision_with_context(
                "Bash", "cmd", "accept", i, 50, false, 3.0, 1.5,
            ));
        }

        let insights = detect_cost_patterns(&decisions);
        assert!(
            insights
                .iter()
                .any(|i| i.fingerprint == "cost_trend:increasing")
        );
    }

    #[test]
    fn test_temporal_friction_detected() {
        let prefs = DistilledPreferences {
            patterns: Vec::new(),
            tool_accuracy: Vec::new(),
            total_decisions: 50,
            overall_accuracy: 0.8,
            temporal: vec![TemporalPattern {
                description: "After 3+ errors: user usually denies (n=5)".to_string(),
                sample_count: 5,
                strength: 0.6,
            }],
        };

        let insights = detect_temporal_friction(&prefs);
        assert_eq!(insights.len(), 1);
        assert_eq!(insights[0].category, InsightCategory::TemporalFriction);
    }
}