skls 0.6.0

TUI for listing and managing agent skills across Cursor, Claude Code, and Codex
Documentation
//! Delete-recommendation scoring.

use crate::model::{InstallSource, SkillRecord};
use chrono::{Duration, Utc};

/// Higher score => stronger delete candidate.
pub fn compute_delete_score(skill: &SkillRecord, now: chrono::DateTime<Utc>) -> f64 {
    let mut score = 0.0;

    match skill.stats.activation_rate {
        None => score += 25.0, // unknown / no sessions
        Some(rate) if rate <= 0.0 => score += 40.0,
        Some(rate) if rate < 0.05 => score += 30.0,
        Some(rate) if rate < 0.15 => score += 15.0,
        Some(rate) if rate < 0.30 => score += 5.0,
        Some(_) => score += 0.0,
    }

    match skill.stats.last_hit_at {
        None => score += 20.0,
        Some(ts) => {
            let age = now.signed_duration_since(ts);
            if age > Duration::days(60) {
                score += 25.0;
            } else if age > Duration::days(30) {
                score += 15.0;
            } else if age > Duration::days(14) {
                score += 8.0;
            }
        }
    }

    // Many host installs of an unused skill = more cleanup value / clutter
    if skill.agents.len() >= 3 {
        score += 10.0;
    } else if skill.agents.len() == 2 {
        score += 5.0;
    }

    if skill.source == InstallSource::Manual && skill.source_url.is_none() {
        score += 5.0;
    }

    if skill.stats.hits == 0 {
        score += 10.0;
    }

    // Pinned skills are never suggested for deletion.
    if skill.pinned {
        score = 0.0;
    }

    score
}

pub fn apply_scores(records: &mut [SkillRecord], now: chrono::DateTime<Utc>) {
    for rec in records.iter_mut() {
        rec.stats.delete_score = compute_delete_score(rec, now);
    }
}

/// Delete-score thresholds behind the detail-pane advice label.
pub const DELETE_SCORE_REVIEW: f64 = 35.0;
pub const DELETE_SCORE_CONSIDER: f64 = 60.0;

/// Recommendation bucket for a delete score (drives the detail-pane advice).
pub fn delete_advice(score: f64) -> &'static str {
    if score >= DELETE_SCORE_CONSIDER {
        "consider delete"
    } else if score >= DELETE_SCORE_REVIEW {
        "review"
    } else {
        "keep"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{InstallKind, Scope, SkillStats};

    fn base() -> SkillRecord {
        SkillRecord {
            id: "x".into(),
            name: "x".into(),
            description: String::new(),
            scope: Scope::User,
            project: None,
            agents: vec![],
            locations: vec![],
            install_kind: InstallKind::Copy,
            source: InstallSource::Manual,
            source_url: None,
            author: None,
            version: None,
            pinned: false,
            stats: SkillStats {
                hits: 0,
                sessions_total: 100,
                last_hit_at: None,
                activation_rate: Some(0.0),
                delete_score: 0.0,
            },
        }
    }

    #[test]
    fn unused_skills_score_high() {
        let now = Utc::now();
        let unused = base();
        let mut used = base();
        used.stats.hits = 20;
        used.stats.activation_rate = Some(0.5);
        used.stats.last_hit_at = Some(now);
        used.source = InstallSource::Gh;
        used.source_url = Some("https://x".into());

        let s_unused = compute_delete_score(&unused, now);
        let s_used = compute_delete_score(&used, now);
        assert!(s_unused > s_used);
        assert!(s_unused >= 60.0);
    }

    #[test]
    fn pinned_skills_score_zero() {
        let now = Utc::now();
        let mut pinned = base();
        pinned.pinned = true;
        assert_eq!(compute_delete_score(&pinned, now), 0.0);
    }

    #[test]
    fn delete_advice_buckets_thresholds() {
        assert_eq!(delete_advice(60.0), "consider delete");
        assert_eq!(delete_advice(59.9), "review");
        assert_eq!(delete_advice(35.0), "review");
        assert_eq!(delete_advice(34.9), "keep");
        assert_eq!(delete_advice(0.0), "keep");
    }
}