repopilot 0.18.0

Local-first CLI for reviewing Git changes, security boundaries, and blast radius before merge.
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
use std::path::{Path, PathBuf};

use crate::analysis::{ArchitectureContext, FileRole, LanguageFamily, ModuleKind};
use crate::findings::types::Confidence;
use crate::graph::ImportResolutionStats;
use crate::scan::config::{LayerSpec, ScanConfig};

use super::layers::LayerIndex;
use super::packages::PackageIndex;
use super::{NodeInfo, dead_module_finding, test_leak_finding};

fn complete_graph() -> ImportResolutionStats {
    ImportResolutionStats::default()
}

fn node(
    relative: &str,
    role: FileRole,
    is_entrypoint: bool,
    is_public_api: bool,
) -> NodeInfo<'static> {
    NodeInfo {
        relative: PathBuf::from(relative),
        context: ArchitectureContext {
            file_role: role,
            module_kind: ModuleKind::Unknown,
            language_family: LanguageFamily::CurlyBrace,
            is_entrypoint,
            is_public_api,
        },
        facts: None,
    }
}

fn prod(relative: &str) -> NodeInfo<'static> {
    node(relative, FileRole::Production, false, false)
}

#[test]
fn dead_module_flags_unreferenced_production_file() {
    let info = prod("src/orphan.ts");
    let finding = dead_module_finding(&info, Some(0), &complete_graph());
    let finding = finding.expect("unreferenced production file should be flagged");
    assert_eq!(finding.confidence, Confidence::High);
}

#[test]
fn dead_module_ignores_non_code_files() {
    // Docs / config / stylesheets are never "imported", so they always have
    // fan_in=0 and must not be reported as dead modules.
    let markup = NodeInfo {
        relative: PathBuf::from(".claude/agents/reviewer.md"),
        context: ArchitectureContext {
            file_role: FileRole::Production,
            module_kind: ModuleKind::Unknown,
            language_family: LanguageFamily::Markup,
            is_entrypoint: false,
            is_public_api: false,
        },
        facts: None,
    };
    assert!(dead_module_finding(&markup, Some(0), &complete_graph()).is_none());
}

#[test]
fn dead_module_exempts_entrypoints_public_api_and_imported_files() {
    let resolution = complete_graph();
    assert!(
        dead_module_finding(
            &node("src/main.rs", FileRole::Production, true, false),
            Some(0),
            &resolution
        )
        .is_none()
    );
    assert!(
        dead_module_finding(
            &node("src/lib.rs", FileRole::Production, false, true),
            Some(0),
            &resolution
        )
        .is_none()
    );
    assert!(dead_module_finding(&prod("src/used.ts"), Some(2), &resolution).is_none());
    assert!(
        dead_module_finding(
            &node("src/foo.test.ts", FileRole::Test, false, false),
            Some(0),
            &resolution
        )
        .is_none()
    );
}

#[test]
fn dead_module_is_demoted_to_low_when_graph_has_unresolved_imports() {
    let mut resolution = ImportResolutionStats::default();
    resolution.record(Path::new("src/other.ts"), "./missing-helper");

    let finding = dead_module_finding(&prod("src/orphan.ts"), Some(0), &resolution)
        .expect("dead module should still be reported when the graph is merely incomplete");

    // `Low` (not the `Medium` sentinel) so the registry keeps the demotion.
    assert_eq!(finding.confidence, Confidence::Low);
    assert!(
        finding.evidence[0]
            .snippet
            .contains("unresolved internal import"),
        "snippet should explain the demotion: {}",
        finding.evidence[0].snippet
    );
}

#[test]
fn dead_module_is_suppressed_when_unresolved_import_could_target_it() {
    let mut resolution = ImportResolutionStats::default();
    resolution.record(Path::new("src/other.ts"), "../legacy/orphan");

    assert!(
        dead_module_finding(&prod("src/orphan.ts"), Some(0), &resolution).is_none(),
        "an unresolved import matching the candidate's name is a plausible importer"
    );
}

#[test]
fn test_leak_flags_production_importing_test_or_fixture() {
    let source = prod("src/app.ts");
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    assert!(
        test_leak_finding(
            &source,
            &node("src/app.test.ts", FileRole::Test, false, false),
            root,
            &known
        )
        .is_some()
    );
    assert!(
        test_leak_finding(
            &source,
            &node("fixtures/data.ts", FileRole::Fixture, false, false),
            root,
            &known
        )
        .is_some()
    );
    // Production importing production, and tests importing tests, are fine.
    assert!(test_leak_finding(&source, &prod("src/util.ts"), root, &known).is_none());
    assert!(
        test_leak_finding(
            &node("src/app.test.ts", FileRole::Test, false, false),
            &node("src/helper.test.ts", FileRole::Test, false, false),
            root,
            &known
        )
        .is_none()
    );
}

fn layered_config() -> ScanConfig {
    ScanConfig {
        architecture_layers: vec![
            LayerSpec {
                name: "ui".into(),
                paths: vec!["src/ui/**".into()],
            },
            LayerSpec {
                name: "core".into(),
                paths: vec!["src/core/**".into()],
            },
        ],
        ..ScanConfig::default()
    }
}

#[test]
fn layer_violation_flags_lower_layer_importing_higher_layer() {
    let index = LayerIndex::from_config(&layered_config());
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    // core (index 1) importing ui (index 0) reverses the declared order.
    let finding = index.violation_finding(
        &prod("src/core/service.ts"),
        &prod("src/ui/widget.ts"),
        root,
        &known,
    );
    assert!(finding.is_some());
}

#[test]
fn layer_violation_allows_declared_direction_and_unlayered_files() {
    let index = LayerIndex::from_config(&layered_config());
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    // ui importing core follows the declared order.
    assert!(
        index
            .violation_finding(
                &prod("src/ui/page.ts"),
                &prod("src/core/service.ts"),
                root,
                &known
            )
            .is_none()
    );
    // A file outside every layer is ignored.
    assert!(
        index
            .violation_finding(
                &prod("src/util/log.ts"),
                &prod("src/ui/widget.ts"),
                root,
                &known
            )
            .is_none()
    );
}

#[test]
fn layer_index_is_empty_without_config() {
    let index = LayerIndex::from_config(&ScanConfig::default());
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    assert!(
        index
            .violation_finding(&prod("src/core/a.ts"), &prod("src/ui/b.ts"), root, &known)
            .is_none()
    );
}

fn packaged_config() -> ScanConfig {
    ScanConfig {
        package_roots: vec!["packages/*".into()],
        ..ScanConfig::default()
    }
}

#[test]
fn package_boundary_flags_cross_package_internal_import() {
    let index = PackageIndex::from_config(&packaged_config());
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    let finding = index.violation_finding(
        &prod("packages/web/src/use.ts"),
        &prod("packages/auth/src/internal.ts"),
        root,
        &known,
    );
    assert!(finding.is_some());
}

#[test]
fn architecture_findings_use_import_line_as_evidence() {
    let mut known = std::collections::HashSet::new();
    known.insert(PathBuf::from("packages/auth/src/internal.ts"));

    let content = "\
const x = 1;
import { something } from \"../../auth/src/internal\";
export {};
";
    let facts = crate::scan::facts::FileFacts {
        path: PathBuf::from("packages/web/src/use.ts"),
        language: Some("TypeScript".to_string()),
        non_empty_lines: 3,
        branch_count: 0,
        imports: vec!["../../auth/src/internal".to_string()],
        content: Some(content.to_string()),
        has_inline_tests: false,
    };

    let source = NodeInfo {
        relative: PathBuf::from("packages/web/src/use.ts"),
        context: ArchitectureContext {
            file_role: FileRole::Production,
            module_kind: ModuleKind::Unknown,
            language_family: LanguageFamily::CurlyBrace,
            is_entrypoint: false,
            is_public_api: false,
        },
        facts: Some(&facts),
    };

    let target = prod("packages/auth/src/internal.ts");

    let index = PackageIndex::from_config(&packaged_config());
    // Use an absolute-ish root just to prove resolve_import handles the relative setup properly
    let root = Path::new("/var/repo");
    let finding = index
        .violation_finding(&source, &target, root, &known)
        .expect("should find violation");

    assert_eq!(
        finding.evidence[0].line_start, 2,
        "evidence should point to line 2"
    );
    assert_eq!(
        finding.evidence[0].line_end, None,
        "single line import has no line_end"
    );
}

#[test]
fn package_boundary_allows_public_api_same_package_and_no_config() {
    let configured = PackageIndex::from_config(&packaged_config());
    let root = Path::new("");
    let known = std::collections::HashSet::new();
    // Importing another package's public API is allowed.
    assert!(
        configured
            .violation_finding(
                &prod("packages/web/src/use.ts"),
                &node("packages/auth/index.ts", FileRole::Production, false, true),
                root,
                &known
            )
            .is_none()
    );
    // Same package is allowed.
    assert!(
        configured
            .violation_finding(
                &prod("packages/auth/src/a.ts"),
                &prod("packages/auth/src/b.ts"),
                root,
                &known
            )
            .is_none()
    );
    // Without config the rule is silent.
    let unconfigured = PackageIndex::from_config(&ScanConfig::default());
    assert!(
        unconfigured
            .violation_finding(
                &prod("packages/web/src/use.ts"),
                &prod("packages/auth/src/internal.ts"),
                root,
                &known
            )
            .is_none()
    );
}

fn detected(repo_root: &Path, rel_roots: &[&str]) -> Vec<crate::scan::workspace::WorkspacePackage> {
    rel_roots
        .iter()
        .map(|rel| crate::scan::workspace::WorkspacePackage {
            name: rel.to_string(),
            root: repo_root.join(rel),
        })
        .collect()
}

#[test]
fn detected_workspace_auto_enables_package_boundary_at_high_confidence() {
    let repo_root = Path::new("/repo");
    let packages = detected(repo_root, &["packages/web", "packages/auth"]);
    // No `package_roots` config: detection drives the rule.
    let index = PackageIndex::new(&ScanConfig::default(), &packages, repo_root);
    let known = std::collections::HashSet::new();

    let finding = index
        .violation_finding(
            &prod("packages/web/src/use.ts"),
            &prod("packages/auth/src/internal.ts"),
            repo_root,
            &known,
        )
        .expect("cross-package internal import on a workspace should be flagged");
    // Manifest-declared boundaries are reported at the High ceiling.
    assert_eq!(finding.confidence, Confidence::High);
}

#[test]
fn detected_workspace_respects_public_api_and_same_package() {
    let repo_root = Path::new("/repo");
    let packages = detected(repo_root, &["packages/web", "packages/auth"]);
    let index = PackageIndex::new(&ScanConfig::default(), &packages, repo_root);
    let known = std::collections::HashSet::new();

    // Public entry of another package is allowed.
    assert!(
        index
            .violation_finding(
                &prod("packages/web/src/use.ts"),
                &node("packages/auth/index.ts", FileRole::Production, false, true),
                repo_root,
                &known,
            )
            .is_none()
    );
    // Same package is allowed.
    assert!(
        index
            .violation_finding(
                &prod("packages/auth/src/a.ts"),
                &prod("packages/auth/src/b.ts"),
                repo_root,
                &known,
            )
            .is_none()
    );
}

#[test]
fn configured_package_roots_win_over_detection() {
    // When `package_roots` is set, detection is ignored and findings keep the
    // registry-default (Medium) confidence rather than the manifest High.
    let repo_root = Path::new("/repo");
    let index = PackageIndex::new(
        &packaged_config(),
        &detected(repo_root, &["unrelated/pkg"]),
        repo_root,
    );
    let known = std::collections::HashSet::new();

    let finding = index
        .violation_finding(
            &prod("packages/web/src/use.ts"),
            &prod("packages/auth/src/internal.ts"),
            repo_root,
            &known,
        )
        .expect("glob-configured boundary should still flag");
    assert_eq!(finding.confidence, Confidence::Medium);
}

#[test]
fn no_workspace_and_no_config_is_silent() {
    let index = PackageIndex::new(&ScanConfig::default(), &[], Path::new("/repo"));
    let known = std::collections::HashSet::new();
    assert!(
        index
            .violation_finding(
                &prod("packages/web/src/use.ts"),
                &prod("packages/auth/src/internal.ts"),
                Path::new("/repo"),
                &known,
            )
            .is_none()
    );
}