Skip to content

repo scan mishandles guidance links with invalid or non-UTF-8 targets #1272

In review

Description

@vchamarthi

repo scan mishandles guidance links with invalid or non-UTF-8 targets

Summary

Guidance scanning can count links that cannot be opened or stop with a traceback while decoding committed link text. The inconsistent results make it hard to tell which guidance the command actually read.

Scenarios (6)

1. repo scan counts a guidance link whose target ends in a slash and names a file, in a tree and at @Head

Input: a git repo with real.md, a symlink nul.md -> real.md/, and manifest guidance globs ["nul.md"]. cat nul.md fails with Not a directory.

repo scan <dir>@HEAD prints t: 1 guidance files in <dir>@86a403ebb2f1 and claims: 0, with no guidance file not scanned: nul.md line. The working tree gives the same. WorkTree.resolve('nul.md') and GitTree.resolve('nul.md') both return real.md instead of raising SourceError. Expected: guidance file not scanned: nul.md, as for any other link that cannot be read.

sh make.sh && python cli.py repo scan repo@HEAD

make.sh builds the repo above. The test is attached as tests/test_guidance_link_trailing_slash.py; both of its cases fail on d4023b9e.

tests/test_guidance_link_trailing_slash.py
import os
import subprocess
from pathlib import Path

import pytest

from dokimi.repos.source import GitTree, SourceError, WorkTree


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


def _build(root: Path) -> None:
    _git(root, "init", "-q")
    (root / "real.md").write_text("Use pytest.\n", encoding="utf-8")
    os.symlink("real.md/", root / "nul.md")
    _git(root, "add", "-A")
    _git(root, "commit", "-qm", "i")


def test_worktree_rejects_link_with_trailing_slash(tmp_path: Path) -> None:
    _build(tmp_path)
    with pytest.raises(SourceError):
        WorkTree(tmp_path).resolve("nul.md")


def test_gittree_rejects_link_with_trailing_slash(tmp_path: Path) -> None:
    _build(tmp_path)
    tree = GitTree(tmp_path / ".git", _git(tmp_path, "rev-parse", "HEAD"))
    with pytest.raises(SourceError):
        tree.resolve("nul.md")

2. repo scan and repo inspect @Head raise UnicodeDecodeError when a committed AGENTS.md is a link with non-UTF-8 text

Input: a repository whose committed AGENTS.md is a mode-120000 blob holding the bytes ff fe 78, read as <dir>@HEAD.

repo scan and repo inspect end in a traceback and exit 5. It runs through repos/source.py:301 GitTree._follow into repos/source.py:643 _git, which decodes the link text as strict UTF-8: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte. repo inspect reaches it from repos/environment/__init__.py:120 _read_sizes, repo scan from repos/source.py:338 read_many. repo budget on the same input exits 0 with not readable (non-UTF-8 or binary).

Expected: exit 0 with warning: guidance file not scanned: AGENTS.md: ..., as for a link that escapes the repository. A fix that reads the link text as bytes and raises SourceArgumentError on invalid UTF-8 leaves the strict read_text test in tests/test_repo_l0.py green.

git init -q r && cd r && printf 'hi\n' > README.md && h=$(printf '\377\376x' | git hash-object -w --stdin) && git update-index --add --cacheinfo 120000,$h,AGENTS.md && git add README.md && git -c user.name=n -c user.email=a@b commit -qm a && cd .. && python cli.py repo scan r@HEAD

A test is attached as tests/test_scan_link_text_not_utf8.py; it fails on 360b8a15. The same input fails repo inspect in tests/test_inspect_link_text_not_utf8.py, also attached.

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

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


def _git(repo: Path, *args: str, data: bytes | None = None) -> str:
    done = subprocess.run(
        ["git", "-c", "user.name=n", "-c", "user.email=a@b", *args],
        cwd=repo, input=data, capture_output=True, check=True,
    )
    return done.stdout.decode().strip()


def test_scan_skips_guidance_link_whose_text_is_not_utf8(tmp_path: Path) -> None:
    repo = tmp_path / "repo"
    repo.mkdir()
    _git(repo, "init", "-q")
    (repo / "README.md").write_text("hi\n", encoding="utf-8")
    oid = _git(repo, "hash-object", "-w", "--stdin", data=b"\xff\xfex")
    _git(repo, "update-index", "--add", "--cacheinfo", f"120000,{oid},AGENTS.md")
    _git(repo, "add", "README.md")
    _git(repo, "commit", "-qm", "a")
    run = subprocess.run([sys.executable, "cli.py", "repo", "scan", f"{repo}@HEAD"],
                         capture_output=True, text=True, cwd=ROOT)
    out = run.stderr + run.stdout
    assert "Traceback" not in out, out
    assert run.returncode in (0, 2), out

3. repo scan @Head ends in UnicodeDecodeError when a guidance link passes through a non-UTF-8 directory link

Input: a committed repo where .github/instructions is a symlink whose text is the bytes r\xe9al (not UTF-8), and AGENTS.md holds See [x](.github/instructions/a.instructions.md).

repo scan <dir>@HEAD exits 5 with a traceback: check_link -> exists -> _through_link -> GitTree.resolve -> GitTree._follow (repos/source.py:301) -> _git (repos/source.py:643), ending in UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: invalid continuation byte. The same repo scanned as a working tree prints a report.

Expected: a report or a refusal with exit 2, never a traceback. _follow reads the link text with the strict decode in _git.

python cli.py repo scan <dir>@HEAD

A test is attached as tests/test_git_tree_non_utf8_dir_link.py; it fails on d4023b9e.

tests/test_git_tree_non_utf8_dir_link.py
import os
import subprocess
from pathlib import Path

from dokimi.repos.source import GitTree


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


def test_non_utf8_directory_link_does_not_raise_unicode_error(tmp_path: Path) -> None:
    repo = tmp_path / "repo"
    (repo / "real").mkdir(parents=True)
    (repo / ".github").mkdir()
    (repo / "real" / "a.instructions.md").write_text("hi\n", encoding="utf-8")
    os.symlink(b"r\xe9al", os.fsencode(repo / ".github" / "instructions"))
    (repo / "AGENTS.md").write_text("# a\nSee [x](.github/instructions/a.instructions.md)\n", encoding="utf-8")
    _git(repo.parent, "init", "-q", str(repo))
    _git(repo, "add", "-A")
    _git(repo, "commit", "-qm", "init")
    sha = _git(repo, "rev-parse", "HEAD")
    tree = GitTree(repo, sha)
    try:
        tree.is_file(".github/instructions/a.instructions.md")
    except UnicodeDecodeError as exc:
        raise AssertionError(f"raised {exc!r}") from exc
    except Exception as exc:  # a refusal other than a decode error is acceptable
        assert not isinstance(exc, UnicodeError)

4. repo scan and repo budget never return on a plain directory whose AGENTS.md is a FIFO

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

repo scan prints the no .agent-eval/repo-eval.yaml note and then blocks until killed; under timeout 15 it ends with rc=124. repo budget on the same directory prints nothing and ends with rc=124 too. Expected: the non-regular file is skipped or reported as not readable, and the command exits promptly with 0, 1 or 2, as repo budget already does for non-UTF-8 link text.

mkdir -p /tmp/fifo-dir && mkfifo /tmp/fifo-dir/AGENTS.md
timeout 15 python cli.py repo scan /tmp/fifo-dir; echo rc=$?
timeout 15 python cli.py repo budget /tmp/fifo-dir; echo rc=$?

Tests are attached as tests/test_repo_scan_fifo_guidance.py and tests/test_repo_budget_fifo_guidance.py; both fail with a 15 s timeout on 360b8a15.

tests/test_repo_scan_fifo_guidance.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(not hasattr(os, "mkfifo"), reason="needs os.mkfifo")
def test_repo_scan_with_fifo_guidance_returns(tmp_path: Path) -> None:
    os.mkfifo(tmp_path / "AGENTS.md")
    try:
        run = subprocess.run([sys.executable, "cli.py", "repo", "scan", str(tmp_path)],
                             capture_output=True, text=True, cwd=ROOT, timeout=15)
    except subprocess.TimeoutExpired:
        pytest.fail("repo scan did not return when AGENTS.md is a named pipe")
    out = run.stdout + run.stderr
    assert "Traceback" not in out, out
    assert run.returncode in (0, 1, 2), out

5. repo scan and repo budget block forever on a plain directory whose CLAUDE.md is a FIFO

Input: a plain directory (no git) holding CLAUDE.md made with mkfifo, with or without an AGENTS.md beside it.

repo scan prints the no .agent-eval/repo-eval.yaml note and then blocks; timeout 15 ends it with rc=124. repo budget on the same directory prints nothing and also ends with rc=124. Expected: the FIFO is skipped or refused with a message naming a non-regular file, and the command exits at once. repo inspect on the same directory exits 0, so only the readers behind scan and budget open the FIFO. The AGENTS.md FIFO case is already known; this is the CLAUDE.md name.

mkdir -p d && mkfifo d/CLAUDE.md && (timeout 15 python cli.py repo scan d; echo rc=$?)
(timeout 15 python cli.py repo budget d; echo rc=$?)

Tests tests/test_repo_scan_fifo_claude.py and tests/test_repo_budget_fifo_claude.py are attached; both fail on 07c4fbcd with subprocess.TimeoutExpired after 15 seconds. The blocking read is probably read_bytes() in DirTree.read_many in repos/source.py; not confirmed.

tests/test_repo_scan_fifo_claude.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(not hasattr(os, "mkfifo"), reason="needs os.mkfifo")
def test_repo_scan_does_not_block_on_fifo_claude_md(tmp_path: Path) -> None:
    repo = tmp_path / "repo"
    repo.mkdir()
    os.mkfifo(repo / "CLAUDE.md")
    (repo / "AGENTS.md").write_text("hi\n", encoding="utf-8")
    try:
        run = subprocess.run([sys.executable, "cli.py", "repo", "scan", str(repo)],
                             capture_output=True, text=True, cwd=ROOT, timeout=15)
    except subprocess.TimeoutExpired:
        pytest.fail("repo scan blocked on a FIFO CLAUDE.md")
    assert "Traceback" not in run.stdout + run.stderr

6. repo scan says a FIFO .github/instructions file 'links to nothing' though it is not a link

Input: a plain directory whose .github/instructions/a.instructions.md is a FIFO (mkfifo), with no other guidance file.

repo scan does not hang and exits 0. It prints warning: guidance file not scanned: .github/instructions/a.instructions.md: .github/instructions/a.instructions.md links to nothing: .github/instructions/a.instructions.md and an incomplete: line with the same text. The message comes from repos/source.py:183 and repos/source.py:320. Expected: a message that says the file is not a regular file, since a FIFO is not a link.

mkdir -p fifo-gh/.github/instructions && mkfifo fifo-gh/.github/instructions/a.instructions.md
timeout 15 python cli.py repo scan fifo-gh

A test is attached as tests/test_repo_scan_fifo_instructions.py; it fails on 07c4fbcd. repo budget and repo inspect exit 0 on the same directory under timeout 15.

tests/test_repo_scan_fifo_instructions.py
import os
import subprocess
import sys
from pathlib import Path

import pytest

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


@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="needs os.mkfifo")
def test_fifo_instructions_file_is_not_called_a_link(tmp_path: Path) -> None:
    folder = tmp_path / ".github" / "instructions"
    folder.mkdir(parents=True)
    os.mkfifo(folder / "a.instructions.md")
    run = subprocess.run(
        [sys.executable, "cli.py", "repo", "scan", str(tmp_path)],
        cwd=ROOT, timeout=15, capture_output=True, text=True,
    )
    out = run.stdout + run.stderr
    assert "links to nothing" not in out, out

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: #1271 and #1273. Scenario 6 has the same cause as #1271 scenarios 1 and 3 and #1273 scenario 1, and scenarios 4 and 5 share the FIFO listing with #1271 scenario 9 and #1273 scenario 2 (details on #1271).

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