claudectl-core 0.75.0

Foundational types and IO primitives for claudectl. Shared across the TUI, the binary, and future crates.
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
//! Where in its project a session is working (#596).
//!
//! A session's *focus* is the deepest directory under its worktree root
//! that holds a strict majority of its recent edits. It is derived from the
//! edit tools' inputs alone (`transcript::EDIT_TOOLS`: `Edit`, `Write`,
//! `MultiEdit`, `NotebookEdit`): no LLM, no heuristics about names,
//! integers end to end. A strict majority makes the
//! answer unique by construction (two sibling directories cannot both hold
//! more than half), and the root always qualifies, so a session with any
//! in-tree edit has a focus and one with none has `None`.
//!
//! Edits outside the root are not the project's work and are left out of
//! the total, so a session that also touches `~/.claude/CLAUDE.md` is still
//! focused on the directory it edits inside the repo.
//!
//! The bus uses the focus to route work to whichever role is bound to the
//! enclosing subtree; see `bus::roles::owner_of_path` in the binary.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::paths::{canonical_spellings, canonicalize_deep, normalize_lexically};
use crate::transcript::{
    TranscriptBlock, TranscriptEvent, TranscriptRole, edited_path, is_edit_tool, parse_line,
    read_tail,
};

/// Bytes of transcript tail scanned for edits. Recent edits say what a
/// session is doing *now*; the bus process never runs the monitor, so this
/// is a bounded read rather than an incremental one.
pub const TRANSCRIPT_SCAN_BYTES: u64 = 2 * 1024 * 1024;

/// The directory a session's edits concentrate in, relative to the root.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Focus {
    /// Directory relative to the root, `/`-separated; `.` is the root itself.
    pub dir: String,
    /// Edits under `dir` (every edit counts once per enclosing directory).
    pub edits: u32,
    /// Edits under the root in total.
    pub total: u32,
}

impl Focus {
    /// The focus as an absolute path under `root`.
    pub fn path_under(&self, root: &Path) -> PathBuf {
        if self.dir == "." {
            root.to_path_buf()
        } else {
            root.join(&self.dir)
        }
    }
}

/// The deepest directory holding a strict majority of `edits` under `root`.
///
/// `edits` is `(file path, edit count)`; paths are matched against `root`
/// as given and against its canonical form, because tool inputs may say
/// `/tmp/…` where `git rev-parse` says `/private/tmp/…`.
pub fn focus_from_edits<'a>(
    edits: impl IntoIterator<Item = (&'a str, u32)>,
    root: &Path,
) -> Option<Focus> {
    let roots = root_forms(root);
    let mut per_dir: HashMap<String, u32> = HashMap::new();
    let mut total = 0u32;
    for (file, count) in edits {
        let Some(rel) = relative_to_any(file, &roots) else {
            continue;
        };
        let Some(dir) = parent_dir(&rel) else {
            continue;
        };
        total = total.saturating_add(count);
        for ancestor in ancestors(dir) {
            *per_dir.entry(ancestor).or_insert(0) += count;
        }
    }
    if total == 0 {
        return None;
    }
    per_dir
        .into_iter()
        .filter(|(_, n)| u64::from(*n) * 2 > u64::from(total))
        .max_by_key(|(dir, _)| depth(dir))
        .map(|(dir, edits)| Focus { dir, edits, total })
}

/// Edit counts per file from the tail of a transcript, keyed by the path
/// the tool was given. Reads at most `cap` bytes; an unreadable file is an
/// empty map, since a missing focus is the honest answer there.
pub fn edits_from_transcript(jsonl_path: &Path, cap: u64) -> HashMap<String, u32> {
    let text = read_tail(jsonl_path, cap).unwrap_or_default();
    let mut edits = HashMap::new();
    for line in text.lines() {
        // A line without a tool call cannot carry an edit; skip the JSON
        // parse for the tool results and text turns that make up most of a
        // transcript by bytes.
        if !line.contains("tool_use") {
            continue;
        }
        let Some(TranscriptEvent::Message(msg)) = parse_line(line) else {
            continue;
        };
        if msg.role != TranscriptRole::Assistant {
            continue;
        }
        for block in &msg.content {
            let TranscriptBlock::ToolUse { name, input } = block else {
                continue;
            };
            if !is_edit_tool(name) {
                continue;
            }
            if let Some(p) = edited_path(input) {
                *edits.entry(p.to_string()).or_insert(0) += 1;
            }
        }
    }
    edits
}

/// The focus of a session: its transcript tail against its worktree root.
///
/// The window starts at [`TRANSCRIPT_SCAN_BYTES`] and grows four-fold, up
/// to [`TRANSCRIPT_SCAN_MAX_BYTES`], while it holds no edit at all and the
/// file has more to give. A single multi-MB tool result (a build log) can
/// otherwise fill the whole window and report `None` for a session that
/// edited ten files just before it.
pub fn session_focus(jsonl_path: Option<&Path>, root: &Path) -> Option<Focus> {
    session_focus_within(
        jsonl_path?,
        root,
        TRANSCRIPT_SCAN_BYTES,
        TRANSCRIPT_SCAN_MAX_BYTES,
    )
}

/// Largest transcript tail `session_focus` will read when smaller windows
/// hold no edit.
pub const TRANSCRIPT_SCAN_MAX_BYTES: u64 = 32 * 1024 * 1024;

/// `session_focus` with the window bounds given, so a test can make the
/// first window miss.
pub fn session_focus_within(jsonl_path: &Path, root: &Path, cap: u64, max: u64) -> Option<Focus> {
    let len = std::fs::metadata(jsonl_path).map(|m| m.len()).unwrap_or(0);
    let mut cap = cap.max(1);
    loop {
        let edits = edits_from_transcript(jsonl_path, cap);
        // Stop on an *in-tree* edit, which is what a focus is made of: an
        // out-of-tree edit in the window (a note in `~/.claude`) is not a
        // reason to stop looking for the project's own.
        let focus = focus_from_edits(edits.iter().map(|(p, n)| (p.as_str(), *n)), root);
        if focus.is_some() || cap >= len || cap >= max {
            return focus;
        }
        cap = cap.saturating_mul(4).min(max);
    }
}

/// The root as given and as it resolves on disk, deduplicated, with no
/// trailing slash so the prefix test below is one shape.
fn root_forms(root: &Path) -> Vec<String> {
    let mut forms = vec![trim_slash(&root.to_string_lossy())];
    let c = trim_slash(&canonicalize_deep(root).to_string_lossy());
    if !forms.contains(&c) {
        forms.push(c);
    }
    forms
}

fn trim_slash(s: &str) -> String {
    let t = s.trim_end_matches('/');
    if t.is_empty() {
        "/".to_string()
    } else {
        t.to_string()
    }
}

/// `file` relative to the first root it sits under, or `None` when it is
/// outside every form of the root. The file is tried as spelled and in its
/// canonical form, because `git rev-parse` reports a canonical root while a
/// tool input follows the session's cwd, which may run through a symlink.
fn relative_to_any(file: &str, roots: &[String]) -> Option<String> {
    // Lexically normalized first (`.` and `..` folded, so a path that
    // escapes the root cannot pass a string prefix test): that is the common
    // case and costs no syscall. The on-disk forms are computed only when no
    // root matched.
    let normalized = normalize_lexically(Path::new(file));
    let literal = normalized.to_string_lossy();
    if let Some(rel) = roots.iter().find_map(|root| strip_root(&literal, root)) {
        return Some(rel.to_string());
    }
    canonical_spellings(&normalized)
        .iter()
        .find_map(|f| roots.iter().find_map(|root| strip_root(f, root)))
        .map(str::to_string)
}

fn strip_root<'a>(file: &'a str, root: &str) -> Option<&'a str> {
    if root == "/" {
        return file.strip_prefix('/');
    }
    file.strip_prefix(root)
        .and_then(|rest| rest.strip_prefix('/'))
}

/// The directory part of a relative file path, `.` for a top-level file.
fn parent_dir(rel: &str) -> Option<&str> {
    let rel = rel.trim_end_matches('/');
    if rel.is_empty() {
        return None;
    }
    Some(match rel.rfind('/') {
        Some(i) => &rel[..i],
        None => ".",
    })
}

/// Every directory enclosing `dir`, from the root (`.`) down to `dir`.
fn ancestors(dir: &str) -> Vec<String> {
    let mut out = vec![".".to_string()];
    if dir == "." {
        return out;
    }
    let mut acc = String::new();
    for seg in dir.split('/').filter(|s| !s.is_empty()) {
        if !acc.is_empty() {
            acc.push('/');
        }
        acc.push_str(seg);
        out.push(acc.clone());
    }
    out
}

fn depth(dir: &str) -> usize {
    if dir == "." {
        0
    } else {
        dir.split('/').count()
    }
}

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

    fn focus(edits: &[(&str, u32)], root: &str) -> Option<Focus> {
        focus_from_edits(edits.iter().copied(), Path::new(root))
    }

    #[test]
    fn the_deepest_majority_directory_is_the_focus() {
        let f = focus(
            &[
                ("/repo/src/relay/peer.rs", 3),
                ("/repo/src/relay/mesh.rs", 2),
                ("/repo/src/bus/mcp.rs", 1),
            ],
            "/repo",
        )
        .unwrap();
        assert_eq!(f.dir, "src/relay");
        assert_eq!((f.edits, f.total), (5, 6));
    }

    #[test]
    fn an_even_split_stops_at_the_common_parent() {
        let f = focus(
            &[("/repo/src/relay/peer.rs", 2), ("/repo/src/bus/mcp.rs", 2)],
            "/repo",
        )
        .unwrap();
        assert_eq!(f.dir, "src", "neither sibling holds a strict majority");
    }

    #[test]
    fn work_spread_across_the_tree_is_the_root() {
        let f = focus(
            &[
                ("/repo/README.md", 1),
                ("/repo/src/a.rs", 1),
                ("/repo/docs/b.md", 1),
            ],
            "/repo",
        )
        .unwrap();
        assert_eq!(f.dir, ".");
        assert_eq!(f.path_under(Path::new("/repo")), PathBuf::from("/repo"));
    }

    #[test]
    fn edits_outside_the_root_do_not_count() {
        let f = focus(
            &[
                ("/home/me/.claude/CLAUDE.md", 10),
                ("/repo-other/src/x.rs", 10),
                ("/repo/src/relay/peer.rs", 1),
            ],
            "/repo",
        )
        .unwrap();
        assert_eq!(f.dir, "src/relay");
        assert_eq!(f.total, 1, "a sibling repo with the same prefix is outside");
    }

    #[test]
    fn no_in_tree_edits_is_no_focus() {
        assert_eq!(focus(&[], "/repo"), None);
        assert_eq!(focus(&[("/elsewhere/x.rs", 4)], "/repo"), None);
    }

    #[test]
    fn a_trailing_slash_on_the_root_is_harmless() {
        let f = focus(&[("/repo/src/a.rs", 1)], "/repo/").unwrap();
        assert_eq!(f.dir, "src");
        assert_eq!(f.path_under(Path::new("/repo")), PathBuf::from("/repo/src"));
    }

    #[test]
    fn the_canonical_root_matches_too() {
        // Tool inputs may name the resolved form of a root that is given
        // through a symlink (macOS `/tmp` → `/private/tmp`).
        let tmp = std::env::temp_dir().join(format!("focus-{}", std::process::id()));
        std::fs::create_dir_all(tmp.join("src")).unwrap();
        let canon = std::fs::canonicalize(&tmp).unwrap();
        let file = format!("{}/src/lib.rs", canon.display());
        let f = focus(&[(file.as_str(), 2)], &tmp.to_string_lossy()).unwrap();
        assert_eq!(f.dir, "src");
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn a_canonical_root_matches_a_file_spelled_through_the_symlink() {
        // The shape `list_agents` meets: `git rev-parse` gives the canonical
        // root, the transcript's tool inputs follow the session cwd (macOS
        // `/var/folders/…` → `/private/var/folders/…`), and the file may not
        // exist yet. On a platform with no symlink here both spellings are
        // equal and the test still holds.
        let tmp = std::env::temp_dir().join(format!("focus-rev-{}", std::process::id()));
        std::fs::create_dir_all(tmp.join("src")).unwrap();
        let canon_root = std::fs::canonicalize(&tmp).unwrap();
        let file = format!("{}/src/new.rs", tmp.display());
        let f = focus(&[(file.as_str(), 1)], &canon_root.to_string_lossy()).unwrap();
        assert_eq!(f.dir, "src");
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn transcript_edits_count_only_editing_tools_by_the_assistant() {
        let tmp = std::env::temp_dir().join(format!("focus-jsonl-{}", std::process::id()));
        let lines = [
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/repo/src/relay/peer.rs"}}]}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","input":{"file_path":"/repo/src/bus/mcp.rs"}}]}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"NotebookEdit","input":{"notebook_path":"/repo/src/relay/nb.ipynb"}}]}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Write","input":{"file_path":"/repo/src/relay/peer.rs"}}]}}"#,
            r#"{"type":"user","message":{"role":"user","content":"please edit /repo/src/bus/x.rs"}}"#,
        ];
        std::fs::write(&tmp, lines.join("\n")).unwrap();
        let edits = edits_from_transcript(&tmp, TRANSCRIPT_SCAN_BYTES);
        assert_eq!(edits.get("/repo/src/relay/peer.rs"), Some(&2));
        assert_eq!(edits.get("/repo/src/relay/nb.ipynb"), Some(&1));
        assert_eq!(
            edits.get("/repo/src/bus/mcp.rs"),
            None,
            "reads are not edits"
        );
        assert_eq!(edits.len(), 2);
        let f = session_focus(Some(&tmp), Path::new("/repo")).unwrap();
        assert_eq!(f.dir, "src/relay");
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn a_capped_read_keeps_the_tail() {
        let tmp = std::env::temp_dir().join(format!("focus-tail-{}", std::process::id()));
        let old = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/repo/old/a.rs"}}]}}"#;
        let new = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/repo/new/b.rs"}}]}}"#;
        std::fs::write(&tmp, format!("{old}\n{new}\n")).unwrap();
        let edits = edits_from_transcript(&tmp, (new.len() + 1) as u64);
        assert_eq!(edits.get("/repo/new/b.rs"), Some(&1));
        assert_eq!(
            edits.get("/repo/old/a.rs"),
            None,
            "the head fell outside the cap"
        );
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn a_path_that_escapes_the_root_is_outside_and_dots_do_not_double_count() {
        assert_eq!(
            focus(&[("/repo/../sibling/x.rs", 3)], "/repo"),
            None,
            "`..` out of the root is not in the tree"
        );
        let f = focus(
            &[("/repo/./src/./x.rs", 1), ("/repo/src/../src/y.rs", 1)],
            "/repo",
        )
        .unwrap();
        assert_eq!(f.dir, "src");
        assert_eq!((f.edits, f.total), (2, 2));
    }

    #[test]
    fn an_out_of_tree_edit_in_the_window_does_not_stop_the_search() {
        let tmp = std::env::temp_dir().join(format!("focus-grow2-{}", std::process::id()));
        let in_tree = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/repo/src/relay/peer.rs"}}]}}"#;
        let log = "x".repeat(4096);
        let result = format!(
            r#"{{"type":"user","message":{{"role":"user","content":[{{"type":"tool_result","content":"{log}"}}]}}}}"#
        );
        let out_of_tree = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/home/me/.claude/CLAUDE.md"}}]}}"#;
        std::fs::write(&tmp, format!("{in_tree}\n{result}\n{out_of_tree}\n")).unwrap();
        let small = (result.len() + out_of_tree.len() + 2) as u64;
        assert_eq!(
            edits_from_transcript(&tmp, small).len(),
            1,
            "only the out-of-tree edit fits"
        );
        let f = session_focus_within(&tmp, Path::new("/repo"), small, small * 16).unwrap();
        assert_eq!(f.dir, "src/relay");
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn a_window_full_of_one_huge_tool_result_grows_until_it_finds_the_edits() {
        let tmp = std::env::temp_dir().join(format!("focus-grow-{}", std::process::id()));
        let edit = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/repo/src/relay/peer.rs"}}]}}"#;
        let log = "x".repeat(4096);
        let result = format!(
            r#"{{"type":"user","message":{{"role":"user","content":[{{"type":"tool_result","content":"{log}"}}]}}}}"#
        );
        std::fs::write(&tmp, format!("{edit}\n{result}\n")).unwrap();
        // A window the size of the result alone sees no edit...
        let small = (result.len() + 1) as u64;
        assert_eq!(edits_from_transcript(&tmp, small).len(), 0);
        // ...so the focus read grows and finds it, within the bound.
        let f = session_focus_within(&tmp, Path::new("/repo"), small, small * 16).unwrap();
        assert_eq!(f.dir, "src/relay");
        // With the bound at the small window it honestly reports None.
        assert_eq!(
            session_focus_within(&tmp, Path::new("/repo"), small, small),
            None
        );
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn a_missing_transcript_is_no_focus() {
        assert_eq!(session_focus(None, Path::new("/repo")), None);
        assert_eq!(
            session_focus(Some(Path::new("/nonexistent/x.jsonl")), Path::new("/repo")),
            None
        );
    }
}