Skip to content

repo inspect misreports unreadable sources and unusable files #1271

In review

Description

@vchamarthi

repo inspect misreports unreadable sources and unusable files

Summary

Repository inspection can crash when it encounters inaccessible paths or report guidance and plugin inventory that misses unreadable or unusable inputs. Users need the report to distinguish what the command could inspect from what it could not read.

Scenarios (9)

1. repo inspect ends in a PermissionError traceback when AGENTS.md in a plain directory links into a chmod 000 directory

Input: a directory outside git with a.py, locked/agents.md, AGENTS.md as a symbolic link to locked/agents.md, then chmod 000 locked.

python cli.py repo inspect <dir> exits 5 with PermissionError: [Errno 13] Permission denied: '.../repo/locked/agents.md'. The traceback ends at dokimi/repos/source.py:170 in WorkTree.resolve, at if target.is_dir() and dirs:. The caller _read_sizes (dokimi/repos/environment/__init__.py:120) catches only SourceError, and resolve calls is_dir() outside its try.

Expected: exit 0 and 1 instruction files (1 root), as for a dangling link, with resolve raising SourceArgumentError.

d=$(mktemp -d)/repo; mkdir -p "$d/locked"
printf '# Guide\n' > "$d/locked/agents.md"; printf 'x = 1\n' > "$d/a.py"
ln -s locked/agents.md "$d/AGENTS.md"
chmod 000 "$d/locked"
python cli.py repo inspect "$d"; echo "exit=$?"
chmod 755 "$d/locked"

A test is attached as tests/test_repo_inspect_link_into_unreadable_dir.py; it fails on aa7e8053. Catching OSError around target.is_dir() and target.is_file() in resolve and raising SourceArgumentError makes it pass, and tests/test_repo_inspect_cli.py stays green.

tests/test_repo_inspect_link_into_unreadable_dir.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""``repo inspect`` on a plain directory whose instruction file links into a directory that cannot be searched."""

from __future__ import annotations

import os
import subprocess
import sys
from pathlib import Path

import pytest

PROJECT = Path(__file__).resolve().parents[1]


@pytest.mark.skipif(os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),
                    reason="needs a directory mode the user cannot bypass")
def test_inspect_survives_an_instruction_link_into_an_unreadable_directory(tmp_path):
    root = tmp_path / "repo"
    (root / "locked").mkdir(parents=True)
    (root / "locked" / "agents.md").write_text("# Guide\n", encoding="utf-8")
    (root / "a.py").write_text("x = 1\n", encoding="utf-8")
    (root / "AGENTS.md").symlink_to("locked/agents.md")
    (root / "locked").chmod(0o000)
    try:
        done = subprocess.run([sys.executable, "cli.py", "repo", "inspect", str(root)], cwd=PROJECT,
                              capture_output=True, text=True, timeout=120)
    finally:
        (root / "locked").chmod(0o755)
    assert "Traceback" not in done.stderr, done.stderr[-600:]
    assert done.returncode == 0, done.stderr[-600:]
    assert "1 instruction files (1 root)" in done.stdout

2. repo inspect exits 0 and reports an empty tree with instructions.missing for a source directory of mode 000

Input: a directory that holds AGENTS.md and a.py, then chmod 000 on the directory itself.

python cli.py repo inspect <dir> prints 0 instruction files (0 root), 0 check surfaces, risk low and WARNING instructions.missing -- no AGENTS.md, CLAUDE.md, GEMINI.md, .github/copilot-instructions.md or other root instruction file, and exits 0. The directory has an AGENTS.md; the command could not list it. WorkTree.files (dokimi/repos/source.py) falls back to os.walk, which drops the EACCES and yields nothing.

Expected: a refusal that names the unreadable directory, exit 2 (or 1 for a failed read). A missing path or a regular file as the source is refused with Error: local source is not a directory.

d=$(mktemp -d)/repo; mkdir -p "$d"
printf '# Guide\n' > "$d/AGENTS.md"; printf 'x = 1\n' > "$d/a.py"
chmod 000 "$d"
python cli.py repo inspect "$d"; echo "exit=$?"
chmod 755 "$d"

A test is attached as tests/test_repo_inspect_unreadable_root.py; it fails on aa7e8053 at assert "0 instruction files" not in done.stdout.

tests/test_repo_inspect_unreadable_root.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""``repo inspect`` on a source directory that cannot be read is refused, not reported as an empty tree."""

from __future__ import annotations

import os
import subprocess
import sys
from pathlib import Path

import pytest

PROJECT = Path(__file__).resolve().parents[1]


@pytest.mark.skipif(os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),
                    reason="needs a directory mode the user cannot bypass")
def test_inspect_refuses_a_source_directory_it_cannot_read(tmp_path):
    root = tmp_path / "repo"
    root.mkdir()
    (root / "AGENTS.md").write_text("# Guide\n", encoding="utf-8")
    (root / "a.py").write_text("x = 1\n", encoding="utf-8")
    root.chmod(0o000)
    try:
        done = subprocess.run([sys.executable, "cli.py", "repo", "inspect", str(root)], cwd=PROJECT,
                              capture_output=True, text=True, timeout=120)
    finally:
        root.chmod(0o755)
    assert "Traceback" not in done.stderr, done.stderr[-600:]
    assert "0 instruction files" not in done.stdout
    assert done.returncode in (1, 2), (done.returncode, done.stdout[-300:])

3. repo inspect on a plain directory of mode 444 ends in a PermissionError traceback with exit 5

Input: a plain directory repo of mode 444 (read bit, no search bit) holding one file, AGENTS.md.

repo inspect ends in PermissionError: [Errno 13] Permission denied: '.../repo/AGENTS.md' and exits 5. The traceback runs repos/environment/__init__.py:120 (_read_sizes) -> tree.resolve(path) -> repos/source.py:178 (target.is_dir()). Expected: exit 2 with a message naming the unreadable directory, no traceback.

mkdir -p repo && echo hi > repo/AGENTS.md && chmod 444 repo && python cli.py repo inspect repo; echo rc=$?; chmod 755 repo

The test tests/test_repo_inspect_mode444.py is attached; it fails on d4023b9e.

tests/test_repo_inspect_mode444.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import os
import subprocess
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]


@pytest.mark.skipif(os.geteuid() == 0, reason="root ignores directory permission bits")
def test_repo_inspect_directory_without_search_bit(tmp_path: Path) -> None:
    repo = tmp_path / "repo"
    repo.mkdir()
    (repo / "AGENTS.md").write_text("hi\n", encoding="utf-8")
    os.chmod(repo, 0o444)
    try:
        run = subprocess.run([sys.executable, "cli.py", "repo", "inspect", str(repo)],
                             capture_output=True, text=True, cwd=ROOT)
    finally:
        os.chmod(repo, 0o755)
    out = run.stdout + run.stderr
    assert "Traceback" not in run.stderr, run.stderr
    assert run.returncode == 2, out
    assert out.strip() and "repo" in out, out

4. repo inspect counts a dangling AGENTS.md symlink as a root instruction file: no finding and no not_observed line

Input: a directory with a.py and AGENTS.md as a symbolic link to nowhere.md, which does not exist.

python cli.py repo inspect <dir> --json exits 0 and lists AGENTS.md under instructions. There is no instructions.missing finding, no finding with path AGENTS.md, no not_observed line that names it, and the orient stage is no_known_blocker. _read_sizes (dokimi/repos/environment/__init__.py:119-122) drops the SourceError from tree.resolve, and detect_instructions goes by the file name alone.

Expected: one of the three says the file cannot be loaded. A dangling package.json is already named in not_observed (tests/test_repo_environment.py::test_a_dangling_package_json_is_listed_as_not_observed).

d=$(mktemp -d)/repo; mkdir -p "$d"
printf 'x = 1\n' > "$d/a.py"
ln -s nowhere.md "$d/AGENTS.md"
python cli.py repo inspect "$d" --json | grep -n -A12 '"instructions"\|instructions.missing\|"not_observed"'

A test is attached as tests/test_repo_inspect_dangling_instruction_link.py; it fails on aa7e8053 with assert (False or False or False).

tests/test_repo_inspect_dangling_instruction_link.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""``repo inspect``: an instruction file that is a link to nothing is not a file a harness can load."""

from __future__ import annotations

import pytest

from dokimi.repos.environment import inspect_repo
from dokimi.repos.source import WorkTree


def test_a_dangling_agents_md_is_not_counted_as_a_readable_root_instruction_file(tmp_path):
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    try:
        (tmp_path / "AGENTS.md").symlink_to("nowhere.md")
    except OSError:
        pytest.skip("symlinks unavailable")
    rep = inspect_repo(WorkTree(tmp_path))
    missing = any(f["rule"] == "instructions.missing" for f in rep["findings"])
    flagged = any(f["path"] == "AGENTS.md" for f in rep["findings"])
    not_observed = any("AGENTS.md" in n for n in rep["not_observed"])
    assert missing or flagged or not_observed, "a dangling AGENTS.md is reported as a root instruction file, nothing says it cannot be read"

5. repo inspect counts a 0-byte root AGENTS.md as an instruction file and raises no finding

Input: a directory with no git repository whose only file is an empty AGENTS.md.

repo inspect --json exits 0 with one AGENTS.md instruction entry. The findings are only checks.test.missing and checks.lint.missing; no finding or not_observed line says the file is empty. Expected: a finding that the guidance file is empty, or the file not counted as guidance.

mkdir -p /tmp/empty-dir && : > /tmp/empty-dir/AGENTS.md
python cli.py repo inspect /tmp/empty-dir --json

A test is attached as tests/test_repo_inspect_empty_agents.py; it fails on 360b8a15.

tests/test_repo_inspect_empty_agents.py
import json
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def test_empty_agents_md_is_flagged(tmp_path: Path) -> None:
    (tmp_path / "AGENTS.md").write_text("", encoding="utf-8")
    run = subprocess.run([sys.executable, str(ROOT / "cli.py"), "repo", "inspect", str(tmp_path), "--json"],
                         capture_output=True, text=True, cwd=ROOT)
    assert run.returncode == 0, run.stdout + run.stderr
    data = json.loads(run.stdout)["data"]
    if any(i.get("path") == "AGENTS.md" for i in data["instructions"]):
        mentions = [json.dumps(x) for x in data["findings"] + data["not_observed"]]
        assert any("AGENTS.md" in m or "empty" in m for m in mentions), data["findings"]

6. repo inspect lists a root AGENTS.md that links to itself, a directory or a link loop as an instruction file with no finding

Input: a git tree whose root AGENTS.md is a symlink to itself (ln -s AGENTS.md AGENTS.md). The same happens when it links to a directory (ln -s sub AGENTS.md) and, at <dir>@HEAD, when AGENTS.md and B link to each other.

repo inspect exits 0 and lists AGENTS.md under data.instructions with scope root and size_bytes equal to the link text length (9 for the self-link, 3 for sub). The only findings are checks.test.missing and checks.lint.missing; not_observed does not mention the file.

Expected: a finding or note that AGENTS.md cannot be read (loop or directory), or exit 2 with a message. repo scan on the same trees already reports guidance file not scanned: AGENTS.md and an incomplete run, so the two commands disagree about whether the file exists as guidance.

cd "$(mktemp -d)" && git init -q && echo hi > README.md && ln -s AGENTS.md AGENTS.md && git add -A && git -c user.email=a@b -c user.name=n commit -qm x && python /path/to/dokimi/cli.py repo inspect . --json

No traceback in any of the three cases. A test is attached as tests/test_inspect-self-loop.py; the directory-link and @HEAD loop variants are tests/test_inspect-dir-link.py and tests/test_inspect-chain-head.py. All three fail on d4023b9e.

tests/test_inspect-self-loop.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import json
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def _git(repo: Path, *args: str) -> None:
    subprocess.run(["git", "-c", "user.email=a@b", "-c", "user.name=n", *args], cwd=repo, check=True, capture_output=True)


def test_inspect_self_linked_agents_md(tmp_path: Path) -> None:
    repo = tmp_path / "repo"
    repo.mkdir()
    _git(repo, "init", "-q")
    (repo / "README.md").write_text("hi\n", encoding="utf-8")
    (repo / "AGENTS.md").symlink_to("AGENTS.md")
    _git(repo, "add", "-A")
    _git(repo, "commit", "-qm", "x")
    run = subprocess.run([sys.executable, "cli.py", "repo", "inspect", str(repo), "--json"],
                         capture_output=True, text=True, cwd=ROOT)
    out = run.stdout + run.stderr
    assert "Traceback" not in out, out
    if run.returncode == 2:
        return
    data = json.loads(run.stdout)["data"]
    listed = [i for i in data["instructions"] if i["path"] == "AGENTS.md"]
    if listed:
        text = json.dumps(data["findings"]) + json.dumps(data["not_observed"]) + json.dumps(listed)
        assert "AGENTS.md" in json.dumps(data["findings"]) or "AGENTS.md" in json.dumps(data["not_observed"]), text

7. repo inspect lists a CLAUDE.md link to /dev/zero as an instruction file of 9 bytes with no note

Input: a directory whose only file is CLAUDE.md, a symlink to /dev/zero.

repo inspect --json exits 0 and lists CLAUDE.md as a root instruction file with "size_bytes": 9, the length of the link text, and "notes": []. No finding mentions that the file is a character device. Expected: a note or finding that CLAUDE.md is not a regular file, or the file left out of the instruction list.

mkdir -p d && ln -sfn /dev/zero d/CLAUDE.md && python cli.py repo inspect d --json

The test tests/test_inspect_device_instruction.py is attached; it fails on 07c4fbcd on the assertion that the device file is not listed silently.

tests/test_inspect_device_instruction.py
import json
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def test_device_instruction_file_is_not_listed_silently(tmp_path: Path) -> None:
    (tmp_path / "CLAUDE.md").symlink_to("/dev/zero")
    run = subprocess.run([sys.executable, str(ROOT / "cli.py"), "repo", "inspect", str(tmp_path), "--json"],
                         capture_output=True, text=True, cwd=ROOT, timeout=60)
    out = run.stdout + run.stderr
    assert "Traceback" not in out, out
    data = json.loads(run.stdout)["data"]
    listed = [i for i in data["instructions"] if i["path"] == "CLAUDE.md"]
    assert not listed or listed[0]["notes"] or data["checks"], out

8. repo inspect reports nothing for a .claude-plugin/plugin.json that is a directory, though a dangling link is reported

Input: a plain directory with an AGENTS.md and .claude-plugin/plugin.json as a directory.

python cli.py repo inspect <dir> --json exits 0 with no capability for .claude-plugin/plugin.json and no not_observed line for it. A dangling link, a FIFO, a non-UTF-8 file and a chmod 000 file at the same path each give a high-risk plugin capability with the note not read: invalid, large, a link or not an object, and a not_observed line. Expected: the directory is reported the same way, since the hooks and MCP servers it would declare cannot be read. .claude-plugin/marketplace.json shares PLUGIN_MANIFESTS in repos/environment/risks.py and was not run.

mkdir -p dirmanifest/.claude-plugin/plugin.json && echo x > dirmanifest/AGENTS.md && python cli.py repo inspect dirmanifest --json | grep -c 'plugin.json'

On main this prints 0; with a dangling link in place of the directory it prints 4. The attached tests/test_plugin_manifest_directory.py fails on 52160e9c for the directory case, and its dangling link control passes.

tests/test_plugin_manifest_directory.py
import os
from pathlib import Path

from dokimi.repos.environment import inspect_repo
from dokimi.repos.source import WorkTree

MANIFEST = ".claude-plugin/plugin.json"


def _reported(root: Path) -> bool:
    payload = inspect_repo(WorkTree(root))
    return any(c["path"] == MANIFEST for c in payload["capabilities"]) or any(
        MANIFEST in n for n in payload["not_observed"]
    )


def test_dangling_link_manifest_is_reported(tmp_path: Path) -> None:
    (tmp_path / "AGENTS.md").write_text("x")
    (tmp_path / ".claude-plugin").mkdir()
    os.symlink("nowhere", tmp_path / ".claude-plugin" / "plugin.json")
    assert _reported(tmp_path)


def test_directory_manifest_is_reported(tmp_path: Path) -> None:
    (tmp_path / "AGENTS.md").write_text("x")
    (tmp_path / ".claude-plugin" / "plugin.json").mkdir(parents=True)
    assert _reported(tmp_path)

9. repo inspect lists a FIFO AGENTS.md as an instruction file with no finding

Input: a directory with no git repository whose only file is AGENTS.md, made with mkfifo.

repo inspect --json exits 0. data.instructions holds one AGENTS.md entry with size_bytes 0. data.findings holds only checks.test.missing and checks.lint.missing, and data.not_observed never mentions AGENTS.md. Expected: the pipe is left out of the instruction files, or a finding or not_observed line says it is not a regular file and was not read.

mkdir -p /tmp/fifo-dir && mkfifo /tmp/fifo-dir/AGENTS.md
timeout 15 python cli.py repo inspect /tmp/fifo-dir --json

A test is attached as tests/test_repo_inspect_fifo_guidance.py; it fails on 360b8a15.

tests/test_repo_inspect_fifo_guidance.py
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import json
import os
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def test_fifo_agents_md_is_not_an_instruction_file_without_a_finding(tmp_path: Path) -> None:
    repo = tmp_path / "fifo-inspect"
    repo.mkdir()
    os.mkfifo(repo / "AGENTS.md")
    run = subprocess.run(
        [sys.executable, str(ROOT / "cli.py"), "repo", "inspect", str(repo), "--json"],
        capture_output=True, text=True, cwd=ROOT, timeout=15,
    )
    data = json.loads(run.stdout)["data"]
    listed = any(i["path"] == "AGENTS.md" for i in data["instructions"])
    mentioned = "AGENTS.md" in json.dumps(data["findings"] + data["not_observed"])
    assert not listed or mentioned, run.stdout

Related

Activity

  1. self-assigned this
    on Nov 26, 2026
  2. added
    bug
    on Nov 26, 2026
  3. vchamarthi commented on Nov 26, 2026

    @vchamarthi
    ContributorAuthor

    Related: #959 (its fix, #1028, added the walk error handling here), #1272 and #1273.

    Shared causes:

    One change to each could fix all of them together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions