loctree 0.13.0

Structural code intelligence for AI agents. Scan once, query everything.
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
//! Cut 11 — auto-scope discovery for `loct context` (zero-flag UX).
//!
//! When the operator types `loct context` with no `--file` / `--task` /
//! `--changed`, we still need to surface the most relevant slice. The
//! discovery algorithm is intentionally deterministic: same git state +
//! same worktree state = same scope output.
//!
//! Priority order:
//! 1. Dirty worktree → use changed-file scope (treat as `--changed`).
//! 2. Branch name carrying intent (e.g. `feat/cut11-context-pill`) → keep
//!    as a hint (recorded in `AutoScope::branch_hint`).
//! 3. Recent commits (last 5) → recorded in `AutoScope::commit_hints`.
//! 4. Fall back to top-N hubs by importer count (the highest-leverage
//!    files in the snapshot).
//!
//! The scope is consumed by the pill renderer, which uses the structured
//! data to pick which targets to compose slices for and which hints to
//! surface in TL;DR. The pill never invents scope: every target it shows
//! was either in the dirty worktree, a top-importer hub, or an explicit
//! flag from the operator.

use std::path::Path;
use std::process::Command;

use crate::metrics::top_hubs_by_importers_direct_filtered;
use crate::snapshot::Snapshot;

/// Auto-derived scope for a zero-flag `loct context` invocation.
#[derive(Debug, Clone, Default)]
pub struct AutoScope {
    /// Files derived from git status (dirty worktree only). May be empty.
    pub changed_files: Vec<String>,
    /// Top-N hub files by importer count.
    pub top_hubs: Vec<HubEntry>,
    /// Recent activity (top-N files modified in the last 24-48h, by commit).
    pub recent_files: Vec<String>,
    /// Branch name, if a git branch is checked out.
    pub branch: Option<String>,
    /// Branch-derived intent hint (parsed from `feat/<x>` / `fix/<x>` form).
    pub branch_hint: Option<String>,
    /// Last 5 commit subject lines (newest first).
    pub commit_hints: Vec<String>,
    /// `true` when the worktree had uncommitted changes at scope discovery.
    /// Canonical source for the worktree label rendered into the pill header
    /// (kept in sync with `pack.risk.dirty_worktree`).
    pub dirty: bool,
}

#[derive(Debug, Clone)]
pub struct HubEntry {
    pub file: String,
    pub importers: usize,
}

/// Default ceiling for the top-hubs list when no explicit flag was given.
pub const DEFAULT_TOP_HUBS: usize = 30;

/// Derive an `AutoScope` from snapshot + git state.
///
/// `root` is the directory the pill is being generated for (typically the
/// snapshot root). All git operations run with `current_dir(root)`.
pub fn discover(snapshot: &Snapshot, root: &Path) -> AutoScope {
    let branch = current_branch(root);
    let dirty = worktree_dirty(root).unwrap_or(false);
    let changed_files = if dirty {
        changed_files(root)
    } else {
        Vec::new()
    };
    let recent_files = recently_committed(root, 24);
    let commit_hints = recent_commit_subjects(root, 5);

    let branch_hint = branch.as_deref().and_then(parse_branch_hint);
    let top_hubs = top_hubs_by_importer(snapshot, DEFAULT_TOP_HUBS);

    AutoScope {
        changed_files,
        top_hubs,
        recent_files,
        branch,
        branch_hint,
        commit_hints,
        dirty,
    }
}

/// Produce a stable identifier for the auto-scope. The determinism contract
/// (same git state -> same identifier) is asserted by the unit tests; gated
/// to `cfg(test)` until a non-test caller adopts it (the obvious candidate is
/// a future snapshot-cache key, but no such consumer exists yet).
#[cfg(test)]
pub fn scope_identifier(scope: &AutoScope) -> String {
    let mut buf = String::new();
    if let Some(branch) = &scope.branch {
        buf.push_str(branch);
    }
    buf.push('|');
    buf.push_str(if scope.dirty { "dirty" } else { "clean" });
    buf.push('|');
    buf.push_str(&scope.changed_files.len().to_string());
    buf.push('|');
    buf.push_str(&scope.top_hubs.len().to_string());
    buf
}

fn current_branch(root: &Path) -> Option<String> {
    let output = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(root)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let trimmed = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if trimmed.is_empty() || trimmed == "HEAD" {
        return None;
    }
    Some(trimmed)
}

fn worktree_dirty(root: &Path) -> Option<bool> {
    let output = Command::new("git")
        .args(["status", "--porcelain"])
        .current_dir(root)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    Some(!String::from_utf8_lossy(&output.stdout).trim().is_empty())
}

fn changed_files(root: &Path) -> Vec<String> {
    let output = match Command::new("git")
        .args(["status", "--porcelain", "--untracked-files=no"])
        .current_dir(root)
        .output()
    {
        Ok(o) if o.status.success() => o,
        _ => return Vec::new(),
    };
    let body = String::from_utf8_lossy(&output.stdout);
    let mut files = Vec::new();
    for line in body.lines() {
        // Porcelain format: "XY path" or "XY path -> renamed".
        if line.len() < 3 {
            continue;
        }
        let path_part = line[3..].trim();
        let normalized = path_part
            .rsplit(" -> ")
            .next()
            .unwrap_or(path_part)
            .trim_matches('"')
            .to_string();
        if !normalized.is_empty() {
            files.push(normalized);
        }
    }
    files.sort();
    files.dedup();
    files
}

fn recently_committed(root: &Path, hours: u64) -> Vec<String> {
    let since = format!("--since={hours} hours ago");
    let output = match Command::new("git")
        .args(["log", "--name-only", "--pretty=format:", &since])
        .current_dir(root)
        .output()
    {
        Ok(o) if o.status.success() => o,
        _ => return Vec::new(),
    };
    let body = String::from_utf8_lossy(&output.stdout);
    let mut files = Vec::new();
    for line in body.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        files.push(trimmed.to_string());
    }
    files.sort();
    files.dedup();
    // Keep recent slice tight — pill budget cares about ranking, not volume.
    files.truncate(20);
    files
}

fn recent_commit_subjects(root: &Path, count: usize) -> Vec<String> {
    let output = match Command::new("git")
        .args(["log", &format!("-{count}"), "--pretty=format:%h %s"])
        .current_dir(root)
        .output()
    {
        Ok(o) if o.status.success() => o,
        _ => return Vec::new(),
    };
    let body = String::from_utf8_lossy(&output.stdout);
    body.lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect()
}

/// Branch hints encode the operator's current intent. Convert
/// `feat/cut11-context-pill` → `Cut 11 context pill` for human consumption.
fn parse_branch_hint(branch: &str) -> Option<String> {
    let payload = branch
        .split_once('/')
        .map(|(_, suffix)| suffix)
        .unwrap_or(branch);
    if payload.is_empty() {
        return None;
    }
    let humanized = payload
        .replace(['-', '_'], " ")
        .split_whitespace()
        .map(capitalize_word)
        .collect::<Vec<_>>()
        .join(" ");
    if humanized.is_empty() {
        None
    } else {
        Some(humanized)
    }
}

fn capitalize_word(word: &str) -> String {
    let mut chars = word.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
        None => String::new(),
    }
}

fn top_hubs_by_importer(snapshot: &Snapshot, limit: usize) -> Vec<HubEntry> {
    // loctree-fail hak 2026-05-23 #1: test fixtures must not be
    // promoted to repo-root hub status. A fixture file with internal
    // edges (`tests/fixtures/tauri_app/src/App.tsx` ↔
    // `tests/fixtures/tauri_app/src/components/...`) was being
    // surfaced as `Path: App.tsx` + Authority: RepoVerified + Role:
    // Target, which is structurally false: the file lives under
    // `tests/fixtures/`, not at the canonical repo root, and its
    // authority is *FixtureSource*, not *RepoVerified*. We drop fixture
    // targets from auto-scope while keeping the importer metric itself
    // canonical in `crate::metrics`.
    top_hubs_by_importers_direct_filtered(snapshot, limit, |metric| !is_fixture_path(&metric.file))
        .into_iter()
        .map(|metric| HubEntry {
            file: metric.file,
            importers: metric.importers_direct,
        })
        .collect()
}

/// Returns true when `path` lives under a directory the project clearly
/// marks as test fixture / mock / snapshot territory. Used by scope
/// discovery to keep fixtures out of hub ranking and by the pill
/// renderer to demote their authority.
///
/// Matched fragments (case-sensitive, slash-bounded):
///   - `tests/fixtures/`
///   - `test/fixtures/`
///   - `tests/data/`
///   - `test_fixtures/`
///   - `__fixtures__/`
///   - `__snapshots__/`
///   - `testdata/`
///   - `fixtures/`
///   - `mocks/`
///
/// Backslash variants (Windows paths) are normalized to forward slashes
/// before matching.
pub fn is_fixture_path(path: &str) -> bool {
    if path.is_empty() {
        return false;
    }
    let normalized = path.replace('\\', "/");
    const FRAGMENTS: &[&str] = &[
        "tests/fixtures/",
        "test/fixtures/",
        "tests/data/",
        "test_fixtures/",
        "__fixtures__/",
        "__snapshots__/",
        "testdata/",
        "/fixtures/",
        "/mocks/",
    ];
    for frag in FRAGMENTS {
        if normalized.contains(frag) {
            return true;
        }
    }
    // Bare top-level `fixtures/` or `mocks/` (no leading slash) also count.
    if normalized.starts_with("fixtures/") || normalized.starts_with("mocks/") {
        return true;
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::snapshot::GraphEdge;
    use crate::types::FileAnalysis;

    fn fixture_snapshot() -> Snapshot {
        let mut snapshot = Snapshot::new(vec![".".to_string()]);
        for name in ["hub.rs", "consumer_a.rs", "consumer_b.rs", "isolated.rs"] {
            snapshot.files.push(FileAnalysis::new(name.to_string()));
        }
        for from in ["consumer_a.rs", "consumer_b.rs"] {
            snapshot.edges.push(GraphEdge {
                from: from.to_string(),
                to: "hub.rs".to_string(),
                label: "import".to_string(),
            });
        }
        snapshot
    }

    #[test]
    fn top_hubs_ranks_by_importer_count() {
        let snapshot = fixture_snapshot();
        let hubs = top_hubs_by_importer(&snapshot, 5);
        assert!(!hubs.is_empty());
        assert_eq!(hubs[0].file, "hub.rs");
        assert_eq!(hubs[0].importers, 2);
    }

    #[test]
    fn top_hubs_respects_limit() {
        let snapshot = fixture_snapshot();
        let hubs = top_hubs_by_importer(&snapshot, 1);
        assert_eq!(hubs.len(), 1);
    }

    #[test]
    fn parse_branch_hint_handles_feat_prefix() {
        assert_eq!(
            parse_branch_hint("feat/cut11-context-pill").as_deref(),
            Some("Cut11 Context Pill")
        );
    }

    #[test]
    fn parse_branch_hint_handles_no_prefix() {
        assert_eq!(parse_branch_hint("main").as_deref(), Some("Main"));
    }

    #[test]
    fn parse_branch_hint_returns_none_for_empty() {
        assert!(parse_branch_hint("").is_none());
        assert!(parse_branch_hint("feat/").is_none());
    }

    /// loctree-fail hak 2026-05-23 #1 regression: fixture files (under
    /// `tests/fixtures/`, `__snapshots__/`, etc.) must NOT enter the hub
    /// ranking that drives `loct context` scope selection. A pure-fixture
    /// edge graph must produce an empty top-hubs list.
    #[test]
    fn fixture_paths_are_excluded_from_top_hubs() {
        let mut snapshot = Snapshot::new(vec![".".to_string()]);
        for name in [
            "loctree-rs/tests/fixtures/tauri_app/src/App.tsx",
            "loctree-rs/tests/fixtures/tauri_app/src/components/Hero.tsx",
            "loctree-rs/tests/fixtures/tauri_app/src/components/Footer.tsx",
        ] {
            snapshot.files.push(FileAnalysis::new(name.to_string()));
        }
        snapshot.edges.push(GraphEdge {
            from: "loctree-rs/tests/fixtures/tauri_app/src/components/Hero.tsx".to_string(),
            to: "loctree-rs/tests/fixtures/tauri_app/src/App.tsx".to_string(),
            label: "import".to_string(),
        });
        snapshot.edges.push(GraphEdge {
            from: "loctree-rs/tests/fixtures/tauri_app/src/components/Footer.tsx".to_string(),
            to: "loctree-rs/tests/fixtures/tauri_app/src/App.tsx".to_string(),
            label: "import".to_string(),
        });
        let hubs = top_hubs_by_importer(&snapshot, 10);
        assert!(
            hubs.is_empty(),
            "fixtures must not show up as repo hubs, got: {hubs:?}"
        );
    }

    #[test]
    fn is_fixture_path_matches_canonical_layouts() {
        for path in [
            "loctree-rs/tests/fixtures/tauri_app/src/App.tsx",
            "tests/fixtures/sample.json",
            "tests/data/repo.toml",
            "src/__fixtures__/users.json",
            "components/__snapshots__/Button.snap",
            "test_fixtures/payload.yaml",
            "pkg/testdata/golden.txt",
            "fixtures/scenario_a/input.json",
            "mocks/api_response.json",
        ] {
            assert!(
                is_fixture_path(path),
                "expected fixture-path match for {path}"
            );
        }
    }

    #[test]
    fn is_fixture_path_keeps_real_source_clean() {
        for path in [
            "src/main.rs",
            "loctree-rs/src/cli/dispatch/handlers/context/pill.rs",
            "components/App.tsx",
            "test_utils/helpers.rs",
            "tests/integration.rs",
        ] {
            assert!(
                !is_fixture_path(path),
                "expected real-source pass for {path}"
            );
        }
    }

    #[test]
    fn scope_identifier_is_deterministic_for_identical_state() {
        let scope = AutoScope {
            branch: Some("main".to_string()),
            dirty: false,
            changed_files: vec![],
            top_hubs: vec![HubEntry {
                file: "x".to_string(),
                importers: 1,
            }],
            ..AutoScope::default()
        };
        let a = scope_identifier(&scope);
        let b = scope_identifier(&scope);
        assert_eq!(a, b);
        assert!(a.contains("main"));
        assert!(a.contains("clean"));
    }
}