agentlint-cli 0.1.0

A fast, local-first linter for AI coding-agent configuration
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::Path;
use std::sync::LazyLock;

use regex::Regex;

use crate::analysis::{line_column, push};
use crate::scanner::SourceFile;
use crate::{Diagnostic, Severity};

static TOKEN_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"[A-Za-z0-9_]+|[^\sA-Za-z0-9_]").expect("valid token regex"));
static LINK_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[[^\]]+\]\(([^)#?]+)(?:#[^)]+)?\)").expect("valid link regex"));
static CODE_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"`([^`\n]+)`").expect("valid inline code regex"));
static PATH_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?x)(?:^|[\s(\[\{\"'])( (?:\.?\.?/)?(?:src|docs|scripts|tests?|config|packages|apps|crates|lib|bin|database|migrations|skills)/[A-Za-z0-9_./@+~-]+ | [A-Za-z0-9_.@+-]+/[A-Za-z0-9_./@+~-]+\.(?:md|mdc|rs|ts|tsx|js|jsx|py|go|toml|json|ya?ml|sh|fish|prisma|sql) )"#).expect("valid path regex")
});
static SECRET_ASSIGNMENT_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?i)\b(?:OPENAI_API_KEY|ANTHROPIC_API_KEY|GITHUB_TOKEN|GITLAB_TOKEN|AWS_SECRET_ACCESS_KEY|API_KEY|ACCESS_TOKEN|CLIENT_SECRET)\b\s*[:=]\s*[\"']?([^\s\"',}\]]+)"#).expect("valid secret regex")
});
static BEARER_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)\b(?:authorization\s*:\s*)?bearer\s+([A-Za-z0-9._~+/=-]{16,})")
        .expect("valid bearer regex")
});
static PREFIX_SECRET_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\b((?:sk-(?:proj-)?|gh[pousr]_|github_pat_)[A-Za-z0-9_-]{12,})\b")
        .expect("valid credential prefix regex")
});

#[derive(Debug, Clone)]
struct Sentence<'a> {
    file: &'a SourceFile,
    line: usize,
    text: String,
    normalized: Vec<String>,
}

pub fn approximate_tokens(text: &str) -> usize {
    TOKEN_RE.find_iter(text).count()
}

pub fn check_file(root: &Path, file: &SourceFile, diagnostics: &mut Vec<Diagnostic>) {
    check_references(root, file, diagnostics);
    check_secrets(file, diagnostics);
    check_context(file, diagnostics);
}

fn check_context(file: &SourceFile, diagnostics: &mut Vec<Diagnostic>) {
    let tokens = approximate_tokens(&file.content);
    if tokens >= 6_000 {
        push(
            diagnostics,
            Diagnostic::new(
                "AGL301",
                Severity::Warning,
                &file.relative_path,
                format!("This file is approximately {tokens} tokens and may consume significant agent context."),
            )
            .suggest("Move detailed reference material to on-demand documentation and keep durable instructions concise."),
        );
    }
    let body = strip_frontmatter(&file.content);
    let mut offset = file.content.len().saturating_sub(body.len());
    for block in body.split("\n\n") {
        if block.chars().count() >= 1_200 && !block.trim_start().starts_with("```") {
            let (line, column) = line_column(&file.content, offset);
            push(
                diagnostics,
                Diagnostic::new(
                    "AGL303",
                    Severity::Info,
                    &file.relative_path,
                    format!(
                        "This instruction block is {} characters long.",
                        block.chars().count()
                    ),
                )
                .at(line, column)
                .suggest("Split this block into short, actionable instructions."),
            );
        }
        offset += block.len() + 2;
    }
}

fn check_references(root: &Path, file: &SourceFile, diagnostics: &mut Vec<Diagnostic>) {
    let mut references = BTreeSet::new();
    for capture in LINK_RE.captures_iter(&file.content) {
        if let Some(value) = capture.get(1) {
            references.insert((value.start(), clean_path(value.as_str())));
        }
    }
    for capture in CODE_RE.captures_iter(&file.content) {
        let Some(value) = capture.get(1) else {
            continue;
        };
        if plausible_path(value.as_str()) {
            references.insert((value.start(), clean_path(value.as_str())));
        }
    }
    for capture in PATH_RE.captures_iter(&file.content) {
        if let Some(value) = capture.get(1) {
            references.insert((value.start(), clean_path(value.as_str())));
        }
    }
    for (offset, reference) in references {
        if reference.is_empty()
            || reference.contains('*')
            || reference.starts_with("http://")
            || reference.starts_with("https://")
            || reference.starts_with('#')
            || reference.contains("${")
            || reference == "/"
        {
            continue;
        }
        let candidate = Path::new(&reference);
        let resolved = if candidate.is_absolute() {
            candidate.to_path_buf()
        } else {
            root.join(candidate)
        };
        if resolved.exists() {
            continue;
        }
        let (line, column) = line_column(&file.content, offset);
        let rule = if matches!(
            candidate.extension().and_then(|value| value.to_str()),
            Some("sh" | "fish" | "py")
        ) && reference.starts_with("scripts/")
        {
            "AGL203"
        } else {
            "AGL202"
        };
        let severity = if rule == "AGL203" {
            Severity::Error
        } else {
            Severity::Warning
        };
        let display = reference.trim_end_matches(['.', ',', ':', ';']);
        let mut diagnostic = Diagnostic::new(
            rule,
            severity,
            &file.relative_path,
            format!("Referenced path `{display}` does not exist in this repository."),
        )
        .at(line, column)
        .reference(display);
        if let Some(close) = unambiguous_path_match(root, display) {
            diagnostic = diagnostic.suggest(format!("Did you mean `{close}`?"));
        }
        push(diagnostics, diagnostic);
    }
}

fn plausible_path(value: &str) -> bool {
    let value = value.trim();
    !value.contains(' ')
        && !value.starts_with('-')
        && !value.contains("://")
        && (value.starts_with("./")
            || value.starts_with("../")
            || value.contains('/') && Path::new(value).extension().is_some()
            || [
                "src/",
                "docs/",
                "scripts/",
                "tests/",
                "database/",
                "skills/",
            ]
            .iter()
            .any(|prefix| value.starts_with(prefix)))
}

fn clean_path(value: &str) -> String {
    value
        .trim()
        .trim_matches(['`', '"', '\'', '(', ')', '[', ']'])
        .trim_end_matches(['.', ',', ':', ';'])
        .split('#')
        .next()
        .unwrap_or_default()
        .to_owned()
}

fn unambiguous_path_match(root: &Path, missing: &str) -> Option<String> {
    let missing_path = Path::new(missing);
    let parent = missing_path.parent()?;
    let name = missing_path.file_name()?.to_str()?;
    let directory = root.join(parent);
    let entries = std::fs::read_dir(directory).ok()?;
    let mut matches = entries
        .filter_map(Result::ok)
        .filter_map(|entry| entry.file_name().into_string().ok())
        .filter(|candidate| strsim::levenshtein(candidate, name) <= 2);
    let first = matches.next()?;
    matches
        .next()
        .is_none()
        .then(|| parent.join(first).to_string_lossy().into_owned())
}

fn check_secrets(file: &SourceFile, diagnostics: &mut Vec<Diagnostic>) {
    let mut matches = Vec::new();
    for regex in [&*SECRET_ASSIGNMENT_RE, &*BEARER_RE, &*PREFIX_SECRET_RE] {
        for capture in regex.captures_iter(&file.content) {
            if let Some(value) = capture.get(1).filter(|value| likely_secret(value.as_str())) {
                matches.push((value.start(), redact(value.as_str())));
            }
        }
    }
    matches.sort();
    matches.dedup();
    for (offset, masked) in matches {
        let (line, column) = line_column(&file.content, offset);
        push(
            diagnostics,
            Diagnostic::new(
                "AGL401",
                Severity::Error,
                &file.relative_path,
                format!("Potential secret committed inside agent configuration (`{masked}`)."),
            )
            .at(line, column)
            .suggest("Remove the value, rotate the credential, and reference an environment variable instead."),
        );
    }
}

fn likely_secret(value: &str) -> bool {
    let upper = value.to_ascii_uppercase();
    if value.len() < 12
        || value.contains("${")
        || value.contains("process.env")
        || value.starts_with('<')
        || upper.contains("YOUR_")
        || upper.contains("EXAMPLE")
        || upper.contains("PLACEHOLDER")
        || upper.contains("REDACTED")
        || upper == "API_KEY"
        || value
            .chars()
            .all(|character| character == 'x' || character == '*')
    {
        return false;
    }
    value
        .chars()
        .any(|character| character.is_ascii_alphabetic())
        && value.chars().any(|character| character.is_ascii_digit())
}

fn redact(value: &str) -> String {
    let characters: Vec<char> = value.chars().collect();
    if characters.len() <= 8 {
        return "[redacted]".into();
    }
    let start: String = characters.iter().take(6).collect();
    let end: String = characters.iter().rev().take(3).rev().collect();
    format!("{start}...{end}")
}

pub fn check_cross_file(files: &[SourceFile], diagnostics: &mut Vec<Diagnostic>) -> usize {
    let sentences: Vec<Sentence<'_>> = files.iter().flat_map(sentences).collect();
    let mut duplicated_tokens = 0;
    for left_index in 0..sentences.len() {
        for right in &sentences[left_index + 1..] {
            let left = &sentences[left_index];
            if left.file.relative_path == right.file.relative_path
                || left.normalized.len() < 5
                || right.normalized.len() < 5
            {
                continue;
            }
            let score = similarity(&left.normalized, &right.normalized);
            if score >= 0.78 {
                duplicated_tokens += left.normalized.len().min(right.normalized.len());
                push(
                    diagnostics,
                    Diagnostic::new(
                        "AGL302",
                        Severity::Warning,
                        &right.file.relative_path,
                        format!(
                            "Instruction duplicates {}:{}.",
                            left.file.relative_path.display(),
                            left.line
                        ),
                    )
                    .at(right.line, 1)
                    .reference(format!(
                        "{}:{}",
                        left.file.relative_path.display(),
                        left.line
                    ))
                    .suggest("Keep shared guidance in one canonical instruction file."),
                );
            }
        }
    }
    check_contradictions(&sentences, diagnostics);
    duplicated_tokens
}

fn sentences(file: &SourceFile) -> Vec<Sentence<'_>> {
    let mut result = Vec::new();
    let body = strip_frontmatter(&file.content);
    let mut offset = file.content.len().saturating_sub(body.len());
    for line in body.lines() {
        for part in line.split(['.', '!', '?', ';']) {
            let text = part.trim().trim_start_matches(|character: char| {
                matches!(character, '-' | '*' | '#' | '0'..='9' | '.' | ' ')
            });
            let normalized = normalize_words(text);
            if normalized.len() >= 3 && looks_instructional(text) {
                result.push(Sentence {
                    file,
                    line: line_column(&file.content, offset).0,
                    text: text.to_owned(),
                    normalized,
                });
            }
        }
        offset += line.len() + 1;
    }
    result
}

fn looks_instructional(text: &str) -> bool {
    let lower = text.to_ascii_lowercase();
    [
        "always ",
        "never ",
        "must ",
        "do not ",
        "don't ",
        "use ",
        "avoid ",
        "prefer ",
        "only ",
        "required",
        "forbidden",
        "run ",
        "before ",
        "may freely ",
    ]
    .iter()
    .any(|marker| lower.contains(marker))
}

fn normalize_words(text: &str) -> Vec<String> {
    static WORDS: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"[a-z0-9_./-]+").expect("valid words regex"));
    static STOP: &[&str] = &[
        "a", "an", "the", "to", "please", "you", "we", "should", "changes", "change",
    ];
    WORDS
        .find_iter(&text.to_ascii_lowercase())
        .map(|item| item.as_str().to_owned())
        .filter(|word| !STOP.contains(&word.as_str()))
        .collect()
}

fn similarity(left: &[String], right: &[String]) -> f64 {
    let left_set: HashSet<&str> = left.iter().map(String::as_str).collect();
    let right_set: HashSet<&str> = right.iter().map(String::as_str).collect();
    let intersection =
        f64::from(u32::try_from(left_set.intersection(&right_set).count()).unwrap_or(u32::MAX));
    let union = f64::from(u32::try_from(left_set.union(&right_set).count()).unwrap_or(u32::MAX));
    let jaccard = if union == 0.0 {
        0.0
    } else {
        intersection / union
    };
    let left_joined = left.join(" ");
    let right_joined = right.join(" ");
    let edit = strsim::normalized_levenshtein(&left_joined, &right_joined);
    jaccard.max(edit)
}

#[derive(Debug)]
struct Directive {
    negative: bool,
    subject: String,
}

fn check_contradictions(sentences: &[Sentence<'_>], diagnostics: &mut Vec<Diagnostic>) {
    let mut directives: HashMap<String, Vec<(&Sentence<'_>, bool)>> = HashMap::new();
    for sentence in sentences {
        if let Some(directive) = directive(&sentence.text) {
            directives
                .entry(directive.subject)
                .or_default()
                .push((sentence, directive.negative));
        }
    }
    for entries in directives.values() {
        for (index, (left, left_negative)) in entries.iter().enumerate() {
            for (right, right_negative) in &entries[index + 1..] {
                if left_negative == right_negative {
                    continue;
                }
                push(
                    diagnostics,
                    Diagnostic::new(
                        "AGL602",
                        Severity::Warning,
                        &right.file.relative_path,
                        format!(
                            "Instruction contradicts {}:{} (`{}`).",
                            left.file.relative_path.display(),
                            left.line,
                            concise(&left.text)
                        ),
                    )
                    .at(right.line, 1)
                    .reference(format!(
                        "{}:{}",
                        left.file.relative_path.display(),
                        left.line
                    ))
                    .suggest("Choose one policy and remove or scope the conflicting instruction."),
                );
            }
        }
    }
}

fn directive(text: &str) -> Option<Directive> {
    let lower = text.to_ascii_lowercase();
    let negative_markers = [
        "never ",
        "must not ",
        "do not ",
        "don't ",
        "avoid ",
        "forbidden",
        "skip ",
    ];
    let positive_markers = [
        "always ",
        "must ",
        "required",
        "use ",
        "do ",
        "only ",
        "may freely ",
        "run ",
    ];
    let negative = negative_markers.iter().any(|marker| lower.contains(marker));
    if !negative && !positive_markers.iter().any(|marker| lower.contains(marker)) {
        return None;
    }
    let mut words = normalize_words(&lower);
    for word in &mut words {
        if word == "modify" || word == "editing" || word == "modifying" {
            *word = "edit".into();
        }
        if word == "tests" {
            *word = "test".into();
        }
    }
    words.retain(|word| {
        ![
            "always",
            "never",
            "must",
            "not",
            "do",
            "don't",
            "avoid",
            "forbidden",
            "required",
            "use",
            "only",
            "may",
            "freely",
            "skip",
            "run",
            "instead",
            "before",
            "committing",
            "files",
            "under",
        ]
        .contains(&word.as_str())
    });
    if words.is_empty() {
        return None;
    }
    words.sort();
    words.dedup();
    Some(Directive {
        negative,
        subject: words.join(" "),
    })
}

fn concise(text: &str) -> String {
    const LIMIT: usize = 80;
    if text.chars().count() <= LIMIT {
        text.to_owned()
    } else {
        format!("{}…", text.chars().take(LIMIT).collect::<String>())
    }
}

fn strip_frontmatter(text: &str) -> &str {
    if let Some(stripped) = text.strip_prefix("---\n") {
        if let Some(end) = stripped.find("\n---") {
            let after = 4 + end + 4;
            return text.get(after..).unwrap_or_default();
        }
    } else if let Some(stripped) = text.strip_prefix("---\r\n") {
        if let Some(end) = stripped.find("\r\n---") {
            let after = 5 + end + 6;
            return text.get(after..).unwrap_or_default();
        }
    }
    text
}

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

    #[test]
    fn secret_redaction_never_reveals_full_value() {
        let value = "sk-proj-abc123456789xyz";
        let masked = redact(value);
        assert!(!masked.contains(value));
        assert_eq!(masked, "sk-pro...xyz");
    }

    #[test]
    fn placeholders_are_not_secrets() {
        assert!(!likely_secret("${OPENAI_API_KEY}"));
        assert!(!likely_secret("YOUR_API_KEY"));
        assert!(!likely_secret("example-token-12345"));
    }

    #[test]
    fn reordered_instruction_is_similar() {
        let left = normalize_words("Always run npm test before committing changes");
        let right = normalize_words("Before committing, always run npm test");
        assert!(similarity(&left, &right) >= 0.78);
    }
}