Skip to content

fix(repos): refuse bad link text, file links ending in /, and FIFOs - #1327

Open
vchamarthi wants to merge 2 commits into
intel:mainfrom
vchamarthi:issue-1272-repo-scan-mishandles-guidance-links
Open

vchamarthi wants to merge 2 commits into
intel:mainfrom
vchamarthi:issue-1272-repo-scan-mishandles-guidance-links

Conversation

@vchamarthi

Copy link
Copy Markdown
Contributor

Summary

repo scan, repo budget and repo inspect mishandled three kinds of guidance path: a committed link whose text is not UTF-8 (traceback), a link ending in / that names a file (counted as that file), and a FIFO in a plain directory (the command waited forever, or the file was called a link). This fixes all six scenarios of #1272. Related: #1273 (same links to nothing message for a FIFO, now is not a regular file) and #1271 (same FIFO listing and repo inspect read).

Changes

  • GitTree._follow reads link text through a new _link_text and raises SourceArgumentError when it is not UTF-8, so read_many and the scan record it as skipped.
  • links._walk_links and GitTree._follow remember that a link text ended in / and raise _NoSuchPath when what they reach is not a directory, so resolve refuses nul.md -> real.md/ in a tree and at a revision.
  • WorkTree.read_text and read_bytes check S_ISREG before opening; WorkTree.resolve says is not a regular file for an existing FIFO, socket or device instead of links to nothing.
  • A changelog fragment.
  • The two new errors trace to repo scan mishandles guidance links with invalid or non-UTF-8 targets #1272: links.py _require_dir (cat nul.md fails with Not a directory for nul.md -> real.md/) and source.py WorkTree._readable (opening a FIFO waits for a writer); the second names the issue in its message.
  • budget._unreadable_reason reports a not a regular file error as such, so repo budget no longer says a present FIFO is missing or a broken link.
  • links._inside keeps the trailing / of a rooted link text (and of the last link it walks outside the root), so the directory requirement survives a link chain that leaves and re-enters the root.

Risk

Small. Only paths that raised, hung or were miscounted change. A link text ending in / that reaches a directory still resolves. A directory is still passed to the open as before, so its error is unchanged. The FIFO guard is in WorkTree only; GitTree holds no FIFOs.

Testing

New tests in tests/test_repo_source_links.py cover the trailing slash (tree and revision, and a chain), non-UTF-8 link text for a file and for a directory link (including the repo scan <dir>@HEAD exit code), the WorkTree readers on a FIFO, and repo scan and repo budget on a FIFO AGENTS.md and CLAUDE.md plus the instructions-file message. I ran that file with tests/test_growth_ratchet.py, tests/test_repo_l0.py and tests/test_repo_budget.py (407 passed), ruff check and mypy on the two source files.

Open questions

None beyond the choices listed below.

  • I chose A non-UTF-8 link text raises SourceArgumentError (a refusal, exit 2 at worst), and read_many lists it as skipped over Treating the link as dangling and giving no error: The issue asks for guidance file not scanned: ... as for a link that escapes the repository. Is that the behaviour you want?
  • I chose A link ending in / that reaches a file raises _NoSuchPath (a SourceError), as the .. after a file case does over Converting it to SourceArgumentError with links to nothing: Smallest change; the issue asks for SourceError. Is that the behaviour you want?
  • I chose The FIFO check lives in WorkTree.read_text, read_bytes and resolve, so every reader gets it over Guarding each caller in budget.py and l0: One place; callers already catch SourceError. Is that the behaviour you want?

Revert

A plain revert restores the old behaviour; nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

Review points edit lines of recent commits: dokimi/dokimi/repos/links.py (7e52ceb fix(repo): a directory refuses a manifest link that leaves and re-ente). Each commit's own tests still pass; none of their assertions changes except those listed above for a scenario of the issue.

Review points also changed dokimi/dokimi/repos/budget.py; each change is for a point a test in this pull request reproduces.

Checklist evidence
  • Tests added/updated and pytest passes: 1 test file(s) changed; suite: 30405 passed; 1 failing test(s) fail on the base branch too: tests/test_repo_compare_interval.py::test_the_row_p_places_zero_where_large_deltas_round_p_at_zero_apart; 10 of 10 proof(s) fail before the change
  • ruff, mypy, and schema checks pass: passed: ci.yml:static / ruff check, ci.yml:typecheck / mypy, ci.yml:static / schema-check, ci.yml:static / schema_compat vs the PR base
  • Docs updated (or N/A): N/A: No doc describes how resolve treats a link ending in /, a non-UTF-8 link, or a FIFO; the changelog fragment records the change.
  • Self-reviewed per "Before opening a PR" in AGENTS.md: nothing left open; mutation of changed lines ran
  • Each new raise/exit and test names the doc, contract or issue it traces to: every new error path and test names a doc, contract or issue
  • No ad-hoc run artifacts committed: 1 added file(s), none a run's output
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml): no new source file

Fixes #1272.

`GitTree._follow` decoded link text strictly, so a committed link with
non-UTF-8 text ended `repo scan` and `repo inspect` in a traceback; it now
raises `SourceArgumentError`, which the scan reports as not scanned.
`resolve` accepted a link such as `nul.md -> real.md/`, which is ENOTDIR in a
checkout; both trees now refuse it. `WorkTree` opened a FIFO, so `repo scan`
and `repo budget` waited for a writer; it now refuses a non-regular file, and
names it as such instead of "links to nothing".

Fixes intel#1272

Signed-off-by: vchamarthi <123653452+vchamarthi@users.noreply.github.com>
@vchamarthi
vchamarthi requested a review from a team as a code owner November 27, 2026 01:28
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:29
Signed-off-by: vchamarthi <123653452+vchamarthi@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

6 open issues
What changed in this PR

Fixes incorrect/hanging handling of certain guidance paths across repo scan, repo budget, and repo inspect, specifically non‑UTF‑8 symlink text, symlink targets ending with / that actually resolve to files, and FIFOs in worktrees.

Changes:

  • Add directory-requirement tracking for symlink texts ending in / and raise _NoSuchPath (ENOTDIR semantics) when they resolve to a file.
  • Refuse committed symlinks whose blob text is not UTF‑8 by raising SourceArgumentError (avoids UnicodeDecodeError tracebacks).
  • Prevent hangs/misclassification by treating FIFOs/devices/sockets as “not a regular file” in WorkTree read/resolve paths and reporting this in repo budget.
File Description
dokimi/​tests/​test_repo_source_links.py Adds regression tests for trailing-slash symlinks, non‑UTF‑8 link blobs, and FIFO behavior in worktree + CLI commands
dokimi/​dokimi/​repos/​source.py Implements UTF‑8 refusal for Git symlink blobs and adds non-regular-file guarding for WorkTree reads/resolve
dokimi/​dokimi/​repos/​links.py Preserves trailing / semantics through link walking and enforces “must be directory” when required
dokimi/​dokimi/​repos/​budget.py Improves unreadable-reason reporting to surface “not a regular file”
dokimi/​changelog.d/​20261126_120000_source-link-text-and-fifo.md Changelog entry describing the fixed behaviors

Review effort: Lite

Comment on lines 426 to 431
def _unreadable_reason(err: Exception) -> str:
if isinstance(err, UnicodeDecodeError):
return "non-UTF-8 or binary"
if "is not a regular file" in str(err): # a FIFO, socket or device (#1272)
return "not a regular file"
return "not found, broken link, or outside tree" # dangling symlink, loop, escape, source error
Comment on lines +195 to +204
def _readable(self, path: str) -> Path:
"""The file ``path`` names, which is opened only if it is a regular file: opening a FIFO waits for a writer (#1272)."""
target = self._resolve(path)
try:
mode = os.stat(target).st_mode
except OSError:
return target # the open reports it
if not stat.S_ISREG(mode) and not stat.S_ISDIR(mode):
raise SourceArgumentError(f"{path} is not a regular file (#1272: opening a FIFO waits for a writer)")
return target
raise SourceArgumentError(f"{path} links to nothing: {target.relative_to(self.root).as_posix()}")
rel = target.relative_to(self.root).as_posix()
if target.exists(): # a FIFO, socket or device: present, but not a file to read (#1272)
raise SourceArgumentError(f"{rel} is not a regular file")
Comment on lines +308 to +313
root = tmp_path / "repo"
(root / ".github").mkdir(parents=True)
os.symlink(b"r\xe9al", os.fsencode(root / ".github" / "instructions"))
(root / "AGENTS.md").write_text("See [x](.github/instructions/a.instructions.md)\n", encoding="utf-8")
git = lambda *a: subprocess.run(["git", "-c", "user.name=n", "-c", "user.email=a@b", *a], cwd=root, # noqa: E731
check=True, capture_output=True).stdout.decode().strip()
@@ -0,0 +1,3 @@
### Fixed

- **`repo scan`, `repo budget` and `repo inspect` handle unreadable guidance links and FIFOs** — a committed link whose text is not UTF-8 is now reported as `guidance file not scanned` instead of ending in a `UnicodeDecodeError` traceback; a link whose target is a file followed by `/` (`nul.md -> real.md/`) is no longer counted as that file, as in a checkout; and a FIFO guidance file in a plain directory no longer blocks the command: it is reported as `not a regular file` rather than `links to nothing` (#1272, #PR_NUMBER).


def test_a_link_to_a_file_with_a_trailing_slash_names_no_file_in_both_trees(tmp_path):
"""#1272: ``nul.md -> real.md/`` is ENOTDIR in a checkout, so neither tree resolves it."""

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants