fix(shell): handle undecodable git output in @ file mention#2016
Open
pi-dal wants to merge 2 commits intoMoonshotAI:mainfrom
Open
fix(shell): handle undecodable git output in @ file mention#2016pi-dal wants to merge 2 commits intoMoonshotAI:mainfrom
pi-dal wants to merge 2 commits intoMoonshotAI:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Windows-only crash in @ file mention completion by making git-backed file discovery resilient to undecodable git ls-files -z output.
Changes:
- Switch
git ls-filessubprocess calls to read rawbytesoutput (no locale-dependent text decoding). - Add
_decode_git_output(..., errors="surrogateescape")and make thels-filesparser toleratestdout=None. - Add a regression test intended to cover the Windows decode-failure scenario.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/kimi_cli/utils/file_filter.py |
Reads git output as bytes and decodes in a controlled way to avoid Windows locale decode crashes. |
tests/utils/test_file_filter.py |
Adds a regression test for the decode-failure/missing-stdout scenario in git-backed file listing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+363
to
+391
| """Undecodable git output must not crash file discovery on Windows.""" | ||
|
|
||
| def fake_run(cmd, **kwargs): | ||
| text = kwargs.get("text", False) | ||
| if "--deleted" in cmd or "--others" in cmd: | ||
| return subprocess.CompletedProcess( | ||
| cmd, | ||
| 0, | ||
| stdout="" if text else b"", | ||
| stderr="" if text else b"", | ||
| ) | ||
| if text: | ||
| # Simulate Windows text-mode subprocess decoding failure, where | ||
| # stdout ends up as None after the reader thread dies. | ||
| return subprocess.CompletedProcess(cmd, 0, stdout=None, stderr="") | ||
| return subprocess.CompletedProcess( | ||
| cmd, | ||
| 0, | ||
| stdout=b"src/\xadbad.py\0", | ||
| stderr=b"", | ||
| ) | ||
|
|
||
| monkeypatch.setattr(subprocess, "run", fake_run) | ||
|
|
||
| result = list_files_git(tmp_path) | ||
|
|
||
| assert result is not None | ||
| assert "src/" in result | ||
| assert any(path.endswith("bad.py") for path in result) |
Author
There was a problem hiding this comment.
Fixed. I split the coverage instead of keeping the dead text=True branch: one test still exercises undecodable bytes from the main git ls-files call, and another explicitly asserts _parse_ls_files_output(None) == [] so the defensive stdout=None path is covered directly.
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.
Related Issue
Resolve #2015
Description
This fixes a Windows-only crash in
@file mention completion.Root cause:
git ls-files -zoutput was read with locale-dependentsubprocesstext decodingUnicodeDecodeError, then cascade into aNoneType.splitcrashChanges:
git ls-filesoutput as raw bytes instead of locale-decoded textfile_filter.pywithutf-8andsurrogateescapeValidation:
uv run pytest tests/utils/test_file_filter.py tests/ui_and_conv/test_file_completer.pyChecklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.