mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-15 02:50:57 +01:00
Files that cannot be read in one piece force re-reads and partial edits, which is the largest avoidable cost in an LLM-assisted workflow. This adds the enforcement side of a 250-line cap covering code AND prose. - file_length/config.py: single source of truth for the cap and its exemptions (generated, markup, vendored, data-ish txt), shared by both the gate and the survey so the two cannot silently disagree. - file_length/check.py: the check itself. Exits 1 and lists violations; adjudication is an exit code, not a judgement call. - scripts/check_file_length.sh: thin wrapper pre-commit calls with staged files. Verified against six fixtures: over-cap code fails, under-cap passes, generated l10n (1417 lines) exempt, vendored pdfjs exempt, the 174800-line wordlist exempt as data, and a 1101-line CLAUDE.md correctly fails since prose is capped too. Baseline at time of writing: 657 files over 250 lines across 29 repos. Each repo carries a refactor_claude_todo.md prompt with its own list. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C7yZz45ezmzCB89Cv5gAtZ
129 lines
3.7 KiB
Python
129 lines
3.7 KiB
Python
"""Gate: fail if any given file exceeds the 250-line cap.
|
|
|
|
Invoked by `scripts/check_file_length.sh`, which pre-commit calls with the
|
|
staged files. Exits 1 and lists every violation; exits 0 silently otherwise.
|
|
|
|
The check is deliberately deterministic code rather than a judgement call --
|
|
adjudication belongs in an exit code, not in a model.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
if __package__ in (None, ""): # invoked as a plain script
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from file_length.config import ( # noqa: E402
|
|
CAPPED_EXTENSIONS,
|
|
EXCLUDED_DIRS,
|
|
MAX_LINES,
|
|
is_data_text,
|
|
is_generated,
|
|
is_vendored,
|
|
)
|
|
|
|
|
|
def count_lines(path: Path) -> int | None:
|
|
"""Number of lines, or None if the file is binary or unreadable."""
|
|
try:
|
|
with path.open("rb") as handle:
|
|
first = handle.read(8192)
|
|
if b"\x00" in first:
|
|
return None
|
|
count = first.count(b"\n")
|
|
while chunk := handle.read(1 << 20):
|
|
count += chunk.count(b"\n")
|
|
if first and not first.endswith(b"\n") and count == 0:
|
|
count = 1
|
|
return count
|
|
except OSError:
|
|
return None
|
|
|
|
|
|
def exempt_reason(path: Path, lines: int) -> str | None:
|
|
"""Why this file is not subject to the cap, or None if it is subject."""
|
|
if path.suffix.lower() not in CAPPED_EXTENSIONS:
|
|
return "not a capped extension"
|
|
if is_vendored(path):
|
|
return "vendored / third-party"
|
|
if is_generated(path):
|
|
return "generated"
|
|
try:
|
|
size = path.stat().st_size
|
|
except OSError:
|
|
return "unreadable"
|
|
if is_data_text(path, lines, size):
|
|
return "data-ish text (wordlist)"
|
|
return None
|
|
|
|
|
|
def iter_all(root: Path):
|
|
"""Every file under root, skipping excluded directories."""
|
|
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
|
|
dirnames[:] = [d for d in dirnames if d not in EXCLUDED_DIRS]
|
|
for name in filenames:
|
|
candidate = Path(dirpath) / name
|
|
if not candidate.is_symlink():
|
|
yield candidate
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description=f"Fail if any file exceeds {MAX_LINES} lines."
|
|
)
|
|
parser.add_argument("paths", nargs="*", type=Path)
|
|
parser.add_argument(
|
|
"--all",
|
|
action="store_true",
|
|
help="check the whole tree under the current directory",
|
|
)
|
|
parser.add_argument(
|
|
"--explain", action="store_true", help="also report why files were skipped"
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.all:
|
|
targets = list(iter_all(Path.cwd()))
|
|
elif args.paths:
|
|
targets = args.paths
|
|
else:
|
|
parser.error("give file paths or --all")
|
|
|
|
violations: list[tuple[Path, int]] = []
|
|
for path in targets:
|
|
if not path.is_file():
|
|
continue
|
|
lines = count_lines(path)
|
|
if lines is None:
|
|
continue
|
|
reason = exempt_reason(path, lines)
|
|
if reason is not None:
|
|
if args.explain:
|
|
print(f" skip {path}: {reason}")
|
|
continue
|
|
if lines > MAX_LINES:
|
|
violations.append((path, lines))
|
|
|
|
if not violations:
|
|
return 0
|
|
|
|
print(
|
|
f"File-length gate FAILED: {len(violations)} file(s) over {MAX_LINES} lines\n",
|
|
file=sys.stderr,
|
|
)
|
|
for path, lines in sorted(violations, key=lambda item: -item[1]):
|
|
over = lines - MAX_LINES
|
|
print(f" {path}: {lines} lines (over by {over})", file=sys.stderr)
|
|
print(
|
|
"\nSplit them, or see refactor_claude_todo.md in this repo.", file=sys.stderr
|
|
)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|