skls 0.3.2

TUI for listing and managing agent skills across Cursor, Claude Code, and Codex
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
//! Parse agent transcripts to count skill activations.

use crate::model::{Agent, SkillRecord};
use anyhow::Result;
use chrono::{DateTime, Duration, Utc};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

#[derive(Debug, Clone)]
pub struct LogPaths {
    pub cursor_transcripts: PathBuf,
    pub claude_projects: PathBuf,
    pub codex_sessions: PathBuf,
}

impl LogPaths {
    pub fn from_home(home: &Path) -> Self {
        Self {
            cursor_transcripts: home.join(".cursor/projects"),
            claude_projects: home.join(".claude/projects"),
            codex_sessions: home.join(".codex/sessions"),
        }
    }
}

/// Limits that keep activation analysis interactive even with huge transcript trees.
#[derive(Debug, Clone, Copy)]
pub struct AnalyzeLimits {
    /// Max sessions (jsonl files) per agent, newest first.
    pub max_files_per_agent: usize,
    /// Bytes read from each jsonl (head only).
    pub max_bytes_per_file: u64,
}

impl Default for AnalyzeLimits {
    fn default() -> Self {
        Self {
            max_files_per_agent: 80,
            max_bytes_per_file: 256 * 1024,
        }
    }
}

impl AnalyzeLimits {
    /// No caps — may take tens of seconds on large transcript trees.
    pub fn unlimited() -> Self {
        Self {
            max_files_per_agent: usize::MAX,
            max_bytes_per_file: u64::MAX,
        }
    }

    pub fn is_unlimited(&self) -> bool {
        self.max_files_per_agent == usize::MAX && self.max_bytes_per_file == u64::MAX
    }
}

#[derive(Debug, Clone, Default)]
pub struct ActivationIndex {
    /// skill_id (lowercase) -> set of session ids that hit
    pub hits_by_skill: HashMap<String, HashSet<String>>,
    /// skill_id -> last hit timestamp
    pub last_hit: HashMap<String, DateTime<Utc>>,
    /// agent -> total sessions scanned in window
    pub sessions_by_agent: HashMap<Agent, u64>,
    /// How many candidate files were skipped due to the per-agent cap.
    pub truncated_files: u64,
}

pub fn analyze_logs(
    paths: &LogPaths,
    window_days: i64,
    now: DateTime<Utc>,
) -> Result<ActivationIndex> {
    analyze_logs_with_limits(paths, window_days, now, AnalyzeLimits::default())
}

pub fn analyze_logs_with_limits(
    paths: &LogPaths,
    window_days: i64,
    now: DateTime<Utc>,
    limits: AnalyzeLimits,
) -> Result<ActivationIndex> {
    let cutoff = now - Duration::days(window_days);
    let cutoff_sys = SystemTime::UNIX_EPOCH
        + std::time::Duration::from_secs(cutoff.timestamp().max(0) as u64);
    let mut index = ActivationIndex::default();

    scan_agent_files(
        Agent::Cursor,
        collect_cursor_jsonl(&paths.cursor_transcripts),
        cutoff_sys,
        limits,
        &mut index,
    )?;
    scan_agent_files(
        Agent::ClaudeCode,
        collect_jsonl_tree(&paths.claude_projects, 8),
        cutoff_sys,
        limits,
        &mut index,
    )?;
    scan_agent_files(
        Agent::Codex,
        collect_jsonl_tree(&paths.codex_sessions, 8),
        cutoff_sys,
        limits,
        &mut index,
    )?;

    Ok(index)
}

pub fn apply_stats(records: &mut [SkillRecord], index: &ActivationIndex) {
    let total_sessions: u64 = index.sessions_by_agent.values().sum();
    for rec in records.iter_mut() {
        let key = rec.id.to_lowercase();
        let name_key = rec.name.to_lowercase();
        let mut hits = index
            .hits_by_skill
            .get(&key)
            .map(|s| s.len() as u64)
            .unwrap_or(0);
        if hits == 0 {
            hits = index
                .hits_by_skill
                .get(&name_key)
                .map(|s| s.len() as u64)
                .unwrap_or(0);
        }
        let last = index
            .last_hit
            .get(&key)
            .copied()
            .or_else(|| index.last_hit.get(&name_key).copied());
        let sessions_total = if total_sessions == 0 {
            0
        } else {
            let mut sum = 0u64;
            for agent in &rec.agents {
                sum += index.sessions_by_agent.get(agent).copied().unwrap_or(0);
            }
            if sum == 0 {
                total_sessions
            } else {
                sum
            }
        };
        rec.stats.hits = hits;
        rec.stats.sessions_total = sessions_total;
        rec.stats.last_hit_at = last;
        rec.stats.activation_rate = if sessions_total == 0 {
            None
        } else {
            Some(hits as f64 / sessions_total as f64)
        };
    }
}

fn scan_agent_files(
    agent: Agent,
    files: Vec<PathBuf>,
    cutoff: SystemTime,
    limits: AnalyzeLimits,
    index: &mut ActivationIndex,
) -> Result<()> {
    let mut dated: Vec<(SystemTime, PathBuf)> = Vec::new();
    for path in files {
        let Ok(meta) = fs::metadata(&path) else {
            continue;
        };
        let Ok(modified) = meta.modified() else {
            continue;
        };
        if modified < cutoff {
            continue;
        }
        dated.push((modified, path));
    }
    dated.sort_by(|a, b| b.0.cmp(&a.0));
    if dated.len() > limits.max_files_per_agent {
        index.truncated_files += (dated.len() - limits.max_files_per_agent) as u64;
        dated.truncate(limits.max_files_per_agent);
    }

    let mut sessions = 0u64;
    for (modified, path) in dated {
        if process_file(&path, modified, limits.max_bytes_per_file, index)? {
            sessions += 1;
        }
    }
    *index.sessions_by_agent.entry(agent).or_insert(0) += sessions;
    Ok(())
}

fn collect_cursor_jsonl(root: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    if !root.is_dir() {
        return out;
    }
    let Ok(projects) = fs::read_dir(root) else {
        return out;
    };
    for project in projects.flatten() {
        let transcripts = project.path().join("agent-transcripts");
        if !transcripts.is_dir() {
            continue;
        }
        let Ok(entries) = fs::read_dir(&transcripts) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                out.push(path);
                continue;
            }
            if path.is_dir() {
                if let Ok(nested) = fs::read_dir(&path) {
                    for n in nested.flatten() {
                        let npath = n.path();
                        if npath.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                            out.push(npath);
                        }
                    }
                }
            }
        }
    }
    out
}

fn collect_jsonl_tree(root: &Path, max_depth: usize) -> Vec<PathBuf> {
    let mut out = Vec::new();
    fn rec(path: &Path, depth: usize, max_depth: usize, out: &mut Vec<PathBuf>) {
        if depth > max_depth {
            return;
        }
        let Ok(rd) = fs::read_dir(path) else {
            return;
        };
        for e in rd.flatten() {
            let p = e.path();
            if p.is_dir() {
                // Skip bulky caches that never hold session transcripts.
                let name = p.file_name().and_then(|s| s.to_str()).unwrap_or("");
                if matches!(name, "node_modules" | ".git" | "vendor" | "cache") {
                    continue;
                }
                rec(&p, depth + 1, max_depth, out);
            } else if p.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                out.push(p);
            }
        }
    }
    if root.is_dir() {
        rec(root, 0, max_depth, &mut out);
    }
    out
}

fn process_file(
    path: &Path,
    modified: SystemTime,
    max_bytes: u64,
    index: &mut ActivationIndex,
) -> Result<bool> {
    let mut file = match fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return Ok(false),
    };
    let mut buf = Vec::new();
    let mut limited = file.by_ref().take(max_bytes);
    limited.read_to_end(&mut buf)?;
    let lower = String::from_utf8_lossy(&buf).to_lowercase();
    // Cheap reject: no skill-ish path markers.
    if !lower.contains("skills/") && !lower.contains("skill.md") {
        let when = DateTime::<Utc>::from(modified);
        let session_id = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");
        // Still counts as a scanned session for denominator.
        let _ = (when, session_id);
        return Ok(true);
    }

    let when = DateTime::<Utc>::from(modified);
    let session_id = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string();
    record_hits_from_text(&lower, &session_id, when, index);
    Ok(true)
}

fn record_hits_from_text(
    lower: &str,
    session_id: &str,
    when: DateTime<Utc>,
    index: &mut ActivationIndex,
) {
    for segment in
        lower.split(|c: char| !c.is_ascii_alphanumeric() && c != '-' && c != '_' && c != '/')
    {
        if let Some(name) = extract_skill_name(segment) {
            note_hit(index, &name, session_id, when);
        }
    }
}

fn extract_skill_name(segment: &str) -> Option<String> {
    let s = segment.trim_matches('/');
    if let Some(rest) = s.strip_prefix("skills/") {
        let name = rest.split('/').next().unwrap_or("");
        if is_plausible_skill_id(name) {
            return Some(name.to_string());
        }
    }
    if let Some(idx) = s.rfind("/skills/") {
        let rest = &s[idx + "/skills/".len()..];
        let name = rest.split('/').next().unwrap_or("");
        if is_plausible_skill_id(name) {
            return Some(name.to_string());
        }
    }
    if s.ends_with("/skill.md") {
        let parent = s.trim_end_matches("/skill.md");
        let name = parent.rsplit('/').next().unwrap_or("");
        if is_plausible_skill_id(name) {
            return Some(name.to_string());
        }
    }
    None
}

fn is_plausible_skill_id(name: &str) -> bool {
    !name.is_empty()
        && name.len() < 80
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
        && name != "skills"
        && name != "agents"
}

fn note_hit(index: &mut ActivationIndex, skill: &str, session_id: &str, when: DateTime<Utc>) {
    let key = skill.to_lowercase();
    index
        .hits_by_skill
        .entry(key.clone())
        .or_default()
        .insert(session_id.to_string());
    let entry = index.last_hit.entry(key).or_insert(when);
    if when > *entry {
        *entry = when;
    }
}

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

    #[test]
    fn extracts_skill_from_path_mentions() {
        let mut index = ActivationIndex::default();
        let text = r#"read /users/me/.claude/skills/brainstorming/skill.md and also skills/tdd/SKILL.md"#
            .to_lowercase();
        record_hits_from_text(&text, "sess1", Utc::now(), &mut index);
        assert!(index.hits_by_skill.contains_key("brainstorming"));
        assert!(index.hits_by_skill.contains_key("tdd"));
    }

    #[test]
    fn counts_session_once_per_skill() {
        let tmp = tempfile::tempdir().unwrap();
        let transcripts = tmp.path().join("proj/agent-transcripts");
        fs::create_dir_all(&transcripts).unwrap();
        let mut f = fs::File::create(transcripts.join("abc.jsonl")).unwrap();
        writeln!(
            f,
            r#"{{"text":"using ~/.cursor/skills/find-skills/SKILL.md and again skills/find-skills/x"}}"#
        )
        .unwrap();

        let paths = LogPaths {
            cursor_transcripts: tmp.path().to_path_buf(),
            claude_projects: tmp.path().join("none"),
            codex_sessions: tmp.path().join("none"),
        };
        let index = analyze_logs(&paths, 30, Utc::now()).unwrap();
        let hits = index.hits_by_skill.get("find-skills").unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(index.sessions_by_agent.get(&Agent::Cursor), Some(&1));
    }

    #[test]
    fn respects_file_cap() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("projects/p1");
        fs::create_dir_all(&root).unwrap();
        for i in 0..20 {
            let p = root.join(format!("s{i}.jsonl"));
            fs::write(&p, format!(r#"{{"t":"skills/foo/SKILL.md {i}"}}"#)).unwrap();
        }
        let paths = LogPaths {
            cursor_transcripts: tmp.path().join("none"),
            claude_projects: tmp.path().join("projects"),
            codex_sessions: tmp.path().join("none"),
        };
        let index = analyze_logs_with_limits(
            &paths,
            30,
            Utc::now(),
            AnalyzeLimits {
                max_files_per_agent: 5,
                max_bytes_per_file: 64 * 1024,
            },
        )
        .unwrap();
        assert_eq!(index.sessions_by_agent.get(&Agent::ClaudeCode), Some(&5));
        assert!(index.truncated_files >= 15);
    }
}