fix: harden capture_patch and snapshot_untracked filename handling - #2488
Open
theSatvik wants to merge 1 commit into
Open
fix: harden capture_patch and snapshot_untracked filename handling#2488theSatvik wants to merge 1 commit into
theSatvik wants to merge 1 commit into
Conversation
Ignore entries now reach git as :(literal) pathspecs, so a pre-existing file named like a glob (or starting with ':') can no longer unstage the agent's unrelated changes out of the captured patch. snapshot_untracked rejects filenames that arrived lossily decoded (U+FFFD) instead of returning names that can never match the real file. Closes PrimeIntellect-ai#2196. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2196 — both filename-handling gaps from the post-merge review of #2144.
What changed
1. Ignore entries reach git as literal pathspecs.
capture_patchnow prefixes each ignore entry with:(literal), so a pre-existing file named like a glob can no longer unstage the agent's unrelated changes (and a name starting with:can't be parsed as pathspec magic). This one is nastier than it sounds — reproduced against real git:2. Lossily-decoded filenames fail loudly. Runtimes decode stdout with
errors="replace", so a non-UTF-8 filename reachessnapshot_untrackedwith U+FFFD substituted — it can never match the real file again, the file silently stays out of the ignore set, and the captured patch credits the agent with an image file.snapshot_untrackednow raises aValueErrornaming the problem instead of returning a name that guarantees a wrong patch. (Chose fail-explicitly over a bytes-level path because the runtimes'ProgramResult.stdoutcontract isstr; happy to go the bytes route instead if you'd rather change that contract. The error message notes that a filename legitimately containing U+FFFD also trips this — the price of the lossy text contract.)Tests
New
tests/v1/test_git_utils.py(3 tests, scripted duck-typed runtime): literal-pathspec argv including the*.py/weird[name].txt/ leading-:cases, normal NUL-split round-trip, and the U+FFFD rejection. I know AGENTS.md discourages new unit-test files, but the issue's "Done when" explicitly asks for focused coverage of glob metacharacters and non-UTF-8 bytes — these can't be exercised through the e2e suite without a sandbox image shipping hostile filenames (and macOS dev machines can't even create the non-UTF-8 case on APFS).uv run pytest tests/v1/test_git_utils.py— 3 passed;ruff check/ruff format/ pre-commit on touched files — clean.Note
Harden filename handling in
capture_patchandsnapshot_untrackedcapture_patch, ignore entries are prefixed with:(literal)to pass them to git as literal pathspecs, preventing globbing or pathspec magic.snapshot_untracked, the function raises aValueErrorifgit ls-files -zstdout contains the Unicode replacement character to detect non-UTF-8 filenames.snapshot_untrackednow raisesValueErroron non-UTF-8 filenames instead of returning lossy names;capture_patchchanges how ignore entries are matched (from glob/magic to literal).Macroscope summarized 4af3f02.