instruction-files 0.3.0

Discovery, auditing, and sync for AGENTS.md/CLAUDE.md instruction files
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Audit checks for instruction files.
//!
//! Cross-cutting checks (check_context_invariant, check_staleness, check_line_budget)
//! are re-exported from `agent-kit::audit_common`. Domain-specific checks
//! (check_actionable, check_tree_paths) are re-exported from `agent-rules`.

use crate::types::Issue;
use once_cell::sync::Lazy;
use regex::Regex;
use std::path::Path;

pub use agent_kit::audit_common::{check_context_invariant, check_line_budget, check_staleness};
pub use agent_rules::{check_actionable, check_tree_paths};

/// Regex matching markdown links whose target ends with `LIBRARY_CONTEXT_POLICY.md`.
static POLICY_LINK_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"\[([^\]]*)\]\(([^)]*LIBRARY_CONTEXT_POLICY\.md[^)]*)\)|(?m)^((?:https?://|\.\.?/)[^\s]*LIBRARY_CONTEXT_POLICY\.md)\s*$").unwrap()
});

/// Check that a library AGENTS.md contains a `## Library Context Policy` section
/// with a resolvable link to `LIBRARY_CONTEXT_POLICY.md`.
///
/// Only applies to AGENTS.md files in subdirectories (not the project root).
pub fn check_library_context_policy(rel: &str, content: &str, root: &Path) -> Vec<Issue> {
    // Only check AGENTS.md files in subdirectories
    let path = std::path::Path::new(rel);
    let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
    if file_name != "AGENTS.md" {
        return vec![];
    }
    // Skip root-level AGENTS.md
    let parent = path.parent().and_then(|p| p.to_str()).unwrap_or("");
    if parent.is_empty() {
        return vec![];
    }

    let mut issues = Vec::new();

    // Find the ## Library Context Policy section
    let mut section_line = 0;
    let mut in_section = false;
    let mut section_content = String::new();

    for (i, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed == "## Library Context Policy" {
            section_line = i + 1;
            in_section = true;
            continue;
        }
        if in_section {
            // Stop at next ## heading
            if trimmed.starts_with("## ") {
                break;
            }
            section_content.push_str(line);
            section_content.push('\n');
        }
    }

    if section_line == 0 {
        issues.push(Issue {
            file: rel.to_string(),
            line: 0,
            end_line: 0,
            message: "Missing required '## Library Context Policy' section".to_string(),
            warning: false,
        });
        return issues;
    }

    // Check for a link to LIBRARY_CONTEXT_POLICY.md
    if let Some(caps) = POLICY_LINK_RE.captures(&section_content) {
        // Extract the URL (group 2 for markdown links, group 3 for bare URLs)
        let url = caps.get(2).or_else(|| caps.get(3)).map(|m| m.as_str()).unwrap_or("");

        // Validate link resolves
        if url.starts_with("http://") || url.starts_with("https://") {
            // Accept any URL containing LIBRARY_CONTEXT_POLICY.md
        } else {
            // Relative path — resolve from the AGENTS.md file's directory
            let agents_dir = root.join(parent);
            let resolved = agents_dir.join(url);
            if !resolved.exists() {
                issues.push(Issue {
                    file: rel.to_string(),
                    line: section_line,
                    end_line: 0,
                    message: format!(
                        "Library Context Policy link '{}' does not resolve (expected at {})",
                        url,
                        resolved.display()
                    ),
                    warning: false,
                });
            }
        }
    } else {
        issues.push(Issue {
            file: rel.to_string(),
            line: section_line,
            end_line: 0,
            message: "Library Context Policy section has no link to LIBRARY_CONTEXT_POLICY.md"
                .to_string(),
            warning: false,
        });
    }

    issues
}

#[cfg(test)]
mod tests {
    use super::*;
    use agent_rules::extract_tree_paths;
    use crate::types::AuditConfig;
    use std::fs;
    use tempfile::TempDir;

    // --- extract_tree_paths ---

    #[test]
    fn extract_tree_paths_basic() {
        let content = "\
## Project Structure

```
src/
  main.rs
  lib.rs
```
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths.len(), 2);
        assert_eq!(paths[0].1, "src/main.rs");
        assert_eq!(paths[1].1, "src/lib.rs");
    }

    #[test]
    fn extract_tree_paths_nested() {
        let content = "\
## Project Structure

```
src/
  agent/
    mod.rs
    claude.rs
  main.rs
```
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths.len(), 3);
        assert_eq!(paths[0].1, "src/agent/mod.rs");
        assert_eq!(paths[1].1, "src/agent/claude.rs");
        assert_eq!(paths[2].1, "src/main.rs");
    }

    #[test]
    fn extract_tree_paths_symlink() {
        let content = "\
## Project Structure

```
mail -> ../data/mail
src/
  main.rs
```
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].1, "src/main.rs");
    }

    #[test]
    fn extract_tree_paths_with_comments() {
        let content = "\
## Project Structure

```
src/
  main.rs  # entry point
  lib.rs   # library
```
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths.len(), 2);
        assert_eq!(paths[0].1, "src/main.rs");
        assert_eq!(paths[1].1, "src/lib.rs");
    }

    #[test]
    fn extract_tree_paths_no_section() {
        let content = "# Just a heading\n\nSome text.\n";
        let paths = extract_tree_paths(content);
        assert!(paths.is_empty());
    }

    #[test]
    fn extract_tree_paths_empty_block() {
        let content = "\
## Project Structure

```
```
";
        let paths = extract_tree_paths(content);
        assert!(paths.is_empty());
    }

    #[test]
    fn extract_tree_paths_stops_at_next_section() {
        let content = "\
## Project Structure

```
src/
  main.rs
```

## Other Section

Some text.
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].1, "src/main.rs");
    }

    #[test]
    fn extract_tree_paths_line_numbers() {
        let content = "\
## Project Structure

```
Cargo.toml
src/
  main.rs
```
";
        let paths = extract_tree_paths(content);
        assert_eq!(paths[0], (4, "Cargo.toml".to_string()));
        assert_eq!(paths[1], (6, "src/main.rs".to_string()));
    }

    // --- check_tree_paths ---

    #[test]
    fn check_tree_paths_existing() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::create_dir_all(root.join("src")).unwrap();
        fs::write(root.join("src/main.rs"), "fn main() {}").unwrap();

        let content = "\
## Project Structure

```
src/
  main.rs
```
";
        let issues = check_tree_paths("CLAUDE.md", content, root);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_tree_paths_missing() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        let content = "\
## Project Structure

```
src/
  missing.rs
```
";
        let issues = check_tree_paths("CLAUDE.md", content, root);
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("missing.rs"));
        assert!(!issues[0].warning);
    }

    #[test]
    fn check_tree_paths_skips_brackets() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        let content = "\
## Project Structure

```
src/
  [generated files]
```
";
        let issues = check_tree_paths("CLAUDE.md", content, root);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_tree_paths_skips_env() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        let content = "\
## Project Structure

```
.env
```
";
        let issues = check_tree_paths("CLAUDE.md", content, root);
        assert!(issues.is_empty());
    }

    // --- check_actionable ---

    #[test]
    fn check_actionable_skips_non_agent_files() {
        let config = AuditConfig::agent_doc();
        let issues = check_actionable("README.md", "## Overview\n\nSome overview.\n", &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_informational_heading() {
        let config = AuditConfig::agent_doc();
        let content = "# Doc\n\n## Overview\n\nSome overview text.\n\n## Rules\n\nDo this.\n";
        let issues = check_actionable("CLAUDE.md", content, &config);
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("Informational section"));
        assert!(issues[0].message.contains("Overview"));
        assert!(issues[0].warning);
    }

    #[test]
    fn check_actionable_no_informational_heading() {
        let config = AuditConfig::agent_doc();
        let content = "# Doc\n\n## Conventions\n\nUse serde.\n";
        let issues = check_actionable("AGENTS.md", content, &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_large_code_block_without_context() {
        let config = AuditConfig::agent_doc();
        let mut lines = vec!["# Doc".to_string(), "".to_string()];
        lines.push("```rust".to_string());
        for i in 0..10 {
            lines.push(format!("let x{} = {};", i, i));
        }
        lines.push("```".to_string());
        let content = lines.join("\n");

        let issues = check_actionable("CLAUDE.md", &content, &config);
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("Large code block"));
        assert!(issues[0].warning);
    }

    #[test]
    fn check_actionable_large_code_block_with_imperative() {
        let config = AuditConfig::agent_doc();
        let mut lines = vec![
            "# Doc".to_string(),
            "".to_string(),
            "Use the following pattern:".to_string(),
        ];
        lines.push("```rust".to_string());
        for i in 0..10 {
            lines.push(format!("let x{} = {};", i, i));
        }
        lines.push("```".to_string());
        let content = lines.join("\n");

        let issues = check_actionable("CLAUDE.md", &content, &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_small_code_block_ok() {
        let config = AuditConfig::agent_doc();
        let content = "# Doc\n\n```\nfoo\nbar\n```\n";
        let issues = check_actionable("AGENTS.md", content, &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_large_table() {
        let config = AuditConfig::agent_doc();
        let mut lines = vec!["# Doc".to_string(), "".to_string()];
        lines.push("| Col A | Col B |".to_string());
        lines.push("|-------|-------|".to_string());
        for i in 0..6 {
            lines.push(format!("| row{} | val{} |", i, i));
        }
        let content = lines.join("\n");

        let issues = check_actionable("CLAUDE.md", &content, &config);
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("Large table"));
        assert!(issues[0].warning);
    }

    #[test]
    fn check_actionable_small_table_ok() {
        let config = AuditConfig::agent_doc();
        let content = "\
# Doc

| A | B |
|---|---|
| 1 | 2 |
| 3 | 4 |
";
        let issues = check_actionable("SKILL.md", content, &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_link_heavy_list() {
        let config = AuditConfig::agent_doc();
        let mut lines = vec!["# Doc".to_string(), "".to_string()];
        for i in 0..12 {
            lines.push(format!("- [link{}](https://example.com/{})", i, i));
        }
        let content = lines.join("\n");

        let issues = check_actionable("CLAUDE.md", &content, &config);
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("Link-heavy list"));
        assert!(issues[0].warning);
    }

    // --- check_library_context_policy ---

    #[test]
    fn library_context_policy_missing_section() {
        let tmp = TempDir::new().unwrap();
        let content = "# My Library\n\nSome content.\n";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, tmp.path());
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("Missing required"));
        assert!(!issues[0].warning);
    }

    #[test]
    fn library_context_policy_missing_link() {
        let tmp = TempDir::new().unwrap();
        let content = "# My Library\n\n## Library Context Policy\n\nSome text without a link.\n";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, tmp.path());
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("no link"));
    }

    #[test]
    fn library_context_policy_valid_relative_link() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::create_dir_all(root.join("src/mylib")).unwrap();
        fs::create_dir_all(root.join("src/instruction-files")).unwrap();
        fs::write(
            root.join("src/instruction-files/LIBRARY_CONTEXT_POLICY.md"),
            "policy",
        )
        .unwrap();

        let content = "\
# My Library

## Library Context Policy

This library follows the agent-loop library-context policy. Contributors
authoring `AGENTS.md`, `SKILL.md`, or runbooks in this repo must read:

[Library Context Policy](../instruction-files/LIBRARY_CONTEXT_POLICY.md)

before making changes.
";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, root);
        assert!(issues.is_empty());
    }

    #[test]
    fn library_context_policy_valid_url() {
        let tmp = TempDir::new().unwrap();
        let content = "\
# My Library

## Library Context Policy

This library follows the agent-loop library-context policy.

[Policy](https://github.com/btakita/agent-loop/blob/main/src/instruction-files/LIBRARY_CONTEXT_POLICY.md)
";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, tmp.path());
        assert!(issues.is_empty());
    }

    #[test]
    fn library_context_policy_broken_relative_link() {
        let tmp = TempDir::new().unwrap();
        fs::create_dir_all(tmp.path().join("src/mylib")).unwrap();

        let content = "\
# My Library

## Library Context Policy

[Policy](../instruction-files/LIBRARY_CONTEXT_POLICY.md)
";
        let issues =
            check_library_context_policy("src/mylib/AGENTS.md", content, tmp.path());
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("does not resolve"));
    }

    #[test]
    fn library_context_policy_skips_root_agents() {
        let tmp = TempDir::new().unwrap();
        let content = "# Root\n\nNo policy section needed.\n";
        let issues = check_library_context_policy("AGENTS.md", content, tmp.path());
        assert!(issues.is_empty());
    }

    #[test]
    fn library_context_policy_skips_non_agents() {
        let tmp = TempDir::new().unwrap();
        let content = "# Readme\n";
        let issues = check_library_context_policy("src/mylib/CLAUDE.md", content, tmp.path());
        assert!(issues.is_empty());
    }

    #[test]
    fn library_context_policy_bare_url() {
        let tmp = TempDir::new().unwrap();
        let content = "\
# My Library

## Library Context Policy

This library follows the agent-loop library-context policy.

https://github.com/btakita/agent-loop/blob/main/src/instruction-files/LIBRARY_CONTEXT_POLICY.md
";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, tmp.path());
        assert!(issues.is_empty());
    }

    #[test]
    fn library_context_policy_bare_relative_path() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        fs::create_dir_all(root.join("src/mylib")).unwrap();
        fs::create_dir_all(root.join("src/instruction-files")).unwrap();
        fs::write(
            root.join("src/instruction-files/LIBRARY_CONTEXT_POLICY.md"),
            "policy",
        )
        .unwrap();

        let content = "\
# My Library

## Library Context Policy

This library follows the agent-loop library-context policy.

../instruction-files/LIBRARY_CONTEXT_POLICY.md
";
        let issues = check_library_context_policy("src/mylib/AGENTS.md", content, root);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_short_link_list_ok() {
        let config = AuditConfig::agent_doc();
        let mut lines = vec!["# Doc".to_string(), "".to_string()];
        for i in 0..5 {
            lines.push(format!("- [link{}](https://example.com/{})", i, i));
        }
        let content = lines.join("\n");

        let issues = check_actionable("AGENTS.md", &content, &config);
        assert!(issues.is_empty());
    }

    #[test]
    fn check_actionable_claude_md_skipped_in_corky_config() {
        let config = AuditConfig::corky();
        let content = "# Doc\n\n## Overview\n\nSome overview.\n";
        let issues = check_actionable("CLAUDE.md", content, &config);
        assert!(issues.is_empty()); // CLAUDE.md is not an agent file in corky config
    }
}