Skip to content

Commit 38f466f

Browse files
Add prek hook to enforce 500-line limit on AGENTS.md and SKILL.md files (#1849)
* Initial plan * Add check-agent-file-sizes prek hook to enforce 500-line limit Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1f0d7bd9-4f6e-4bd9-950a-a557a5082300 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
1 parent e7b63b6 commit 38f466f

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ repos:
6868
priority: 0
6969
- repo: local
7070
hooks:
71+
- id: check-agent-file-sizes
72+
name: check-agent-file-sizes
73+
entry: uv run --frozen --offline hooks/check-agent-file-sizes.py
74+
args:
75+
- "--agents-file=AGENTS.md"
76+
- "--skills-dir=.agents/skills"
77+
- "--max-lines=500"
78+
language: system
79+
always_run: true
80+
pass_filenames: false
81+
priority: 0
7182
- id: check-skills-documented
7283
name: check-skills-documented
7384
entry: uv run --frozen --offline hooks/check-skills-documented.py

hooks/check-agent-file-sizes.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Check that AGENTS.md and all SKILL.md files do not exceed the line limit.
2+
3+
Scans the AGENTS.md file and all SKILL.md files in the skills directory to
4+
ensure no file exceeds the maximum number of lines. When a violation is found,
5+
instructions are printed explaining how to reduce the file size.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import argparse
11+
from pathlib import Path
12+
13+
14+
def main() -> int:
15+
parser = argparse.ArgumentParser(
16+
description="Check that agent files do not exceed the line limit.",
17+
)
18+
parser.add_argument(
19+
"--agents-file",
20+
required=True,
21+
help="Path to the AGENTS.md file.",
22+
)
23+
parser.add_argument(
24+
"--skills-dir",
25+
required=True,
26+
help="Path to the skills directory containing subdirectories with SKILL.md files.",
27+
)
28+
parser.add_argument(
29+
"--max-lines",
30+
type=int,
31+
default=500,
32+
help="Maximum number of lines allowed per file (default: 500).",
33+
)
34+
args = parser.parse_args()
35+
36+
agents_file = Path(args.agents_file)
37+
skills_dir = Path(args.skills_dir)
38+
max_lines: int = args.max_lines
39+
40+
if not agents_file.is_file():
41+
print(f"ERROR: {agents_file} not found.")
42+
return 1
43+
44+
if not skills_dir.is_dir():
45+
print(f"ERROR: {skills_dir} is not a directory.")
46+
return 1
47+
48+
violations: list[tuple[Path, int]] = []
49+
50+
agents_line_count = _count_lines(agents_file)
51+
if agents_line_count > max_lines:
52+
violations.append((agents_file, agents_line_count))
53+
54+
for skill_file in sorted(skills_dir.glob("*/SKILL.md")):
55+
line_count = _count_lines(skill_file)
56+
if line_count > max_lines:
57+
violations.append((skill_file, line_count))
58+
59+
if violations:
60+
print(f"ERROR: The following agent files exceed the {max_lines}-line limit:")
61+
for path, count in violations:
62+
print(f" {path}: {count} lines (limit: {max_lines})")
63+
print()
64+
print("To fix these violations:")
65+
print(
66+
" - For SKILL.md files: split the skill into sub-skills, each in its own"
67+
" subdirectory under .agents/skills/."
68+
)
69+
print(
70+
" - For AGENTS.md: move general instructions into separate files referenced"
71+
" from AGENTS.md."
72+
)
73+
return 1
74+
75+
return 0
76+
77+
78+
def _count_lines(path: Path) -> int:
79+
return len(path.read_text(encoding="utf-8").splitlines())
80+
81+
82+
if __name__ == "__main__":
83+
raise SystemExit(main())

0 commit comments

Comments
 (0)