From 7c10ae734f8a4d7d2af29aca4755c0fb53e4306d Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 21 Nov 2026 00:26:49 +0100 Subject: [PATCH] file_length: exclude dirs only inside the repo A repo under ~/.cache (agent worktrees) had every file exempt, so the local gate passed what CI rejects. Co-Authored-By: Claude Opus 5.5 --- file_length/check.py | 2 +- file_length/exemptions.py | 7 ++++++- file_length/tests/test_check.py | 15 +++++++++++++++ file_length/tests/test_exemptions.py | 7 +++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/file_length/check.py b/file_length/check.py index 38a96af..7756b66 100644 --- a/file_length/check.py +++ b/file_length/check.py @@ -67,7 +67,7 @@ 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(absolutize(path)): + if is_vendored(absolutize(path), Path.cwd()): return "vendored / third-party" repo_reason = repo_exempt_reason(absolutize(path), Path.cwd()) if repo_reason is not None: diff --git a/file_length/exemptions.py b/file_length/exemptions.py index be7ffa4..0f2a142 100644 --- a/file_length/exemptions.py +++ b/file_length/exemptions.py @@ -47,15 +47,20 @@ def is_data_text(path: Path, lines: int, size: int) -> bool: return (size / lines) < DATA_TXT_MAX_MEAN_LINE -def is_vendored(path: Path) -> bool: +def is_vendored(path: Path, root: Path | None = None) -> bool: """True if the path sits under a known vendored subtree or excluded dir. Callers must pass an absolute path (see `check.absolutize`). Every rule below needs the repo-name context that a repo-relative path has already thrown away: '/.claude/skills/' cannot match 'skills/x.md', so a relative path would quietly skip the exemption instead of applying it. + + EXCLUDED_DIRS match only the parts below ``root`` (the repo): a repo that + itself sits under '.cache' (agent worktrees) is not excluded wholesale. """ parts = path.parts + if root is not None and path.is_relative_to(root): + parts = path.relative_to(root).parts if any(part in EXCLUDED_DIRS for part in parts): return True posix = path.as_posix() diff --git a/file_length/tests/test_check.py b/file_length/tests/test_check.py index 97c7d0e..59ea835 100644 --- a/file_length/tests/test_check.py +++ b/file_length/tests/test_check.py @@ -149,3 +149,18 @@ def test_runs_as_a_plain_script(repo: Path, monkeypatch: pytest.MonkeyPatch) -> with pytest.raises(SystemExit) as exc: runpy.run_path(str(Path(check.__file__)), run_name="__main__") assert exc.value.code == 0 + + +def test_a_repo_under_an_excluded_dir_is_still_capped( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + # Agent worktrees live under ~/.cache/...: only the parts inside the repo + # may match EXCLUDED_DIRS, or every file there was silently exempt. + root = tmp_path / ".cache" / "wt" + subprocess.run(["git", "init", "-q", str(root)], check=True) + monkeypatch.chdir(root) + write(root, "big.py", lines(260)) + write(root, ".cache/inner.py", lines(260)) + assert run_main(monkeypatch, "big.py") == 1 + assert run_main(monkeypatch, "--all") == 1 + assert run_main(monkeypatch, ".cache/inner.py") == 0 diff --git a/file_length/tests/test_exemptions.py b/file_length/tests/test_exemptions.py index 6e11d4d..64aca29 100644 --- a/file_length/tests/test_exemptions.py +++ b/file_length/tests/test_exemptions.py @@ -58,6 +58,13 @@ def test_own_code_is_not_vendored() -> None: assert not is_vendored(Path("/home/u/mcp-servers/session-autopsy/x.py")) +def test_excluded_dirs_only_count_below_the_root() -> None: + root = Path("/home/u/.cache/wt") + assert not is_vendored(root / "src/a.py", root) + assert is_vendored(root / "build/a.py", root) + assert is_vendored(Path("/elsewhere/.cache/a.py"), root) + + def test_session_artifact() -> None: assert is_session_artifact(Path("/home/u/r/docs/superpowers/plans/a.md")) assert not is_session_artifact(Path("/home/u/r/docs/README.md"))