Repository navigation
Preserve carriage returns in review diffs #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0469418
4525544
0a0d2e2
73d5449
ddb222a
8038969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,6 +158,14 @@ def _review_policy(kind: str, *, split: bool) -> str: | |
| ) | ||
|
|
||
|
|
||
| def render_patch(patch: bytes) -> str: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The escaped rendering is applied to the whole patch and nothing tells the reviewer If any single byte in the patch is not valid UTF-8, |
||
| """Keep UTF-8 patches exact. Escape invalid bytes and literal backslashes otherwise.""" | ||
| try: | ||
| return patch.decode("utf-8") | ||
| except UnicodeDecodeError: | ||
| return patch.replace(b"\\", b"\\\\").decode("utf-8", errors="backslashreplace") | ||
|
|
||
|
|
||
| def build_prompt(*, kind: str, head: str, task_text: str, diff: str, | ||
| chunk: tuple[int, int] | None = None) -> str: | ||
| """Frame exact review material without putting fixed reviewer policy on stdin.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,11 +51,17 @@ def __init__(self, command: tuple[str, ...], tail: str) -> None: | |
| super().__init__(f"{' '.join(command)} failed: {tail.strip()[-400:]}") | ||
|
|
||
|
|
||
| def _git(cwd: Path, *args: str, timeout: float = 120, preserve_whitespace: bool = False) -> str: | ||
| def _git( | ||
| cwd: Path, *args: str, timeout: float = 120, | ||
| preserve_whitespace: bool = False, preserve_newlines: bool = False, | ||
| ) -> str: | ||
| argv = ("git", *args) | ||
| done = run(list(argv), cwd=cwd, env_allowlist=GIT_ENV, timeout=timeout) | ||
| done = run(list(argv), cwd=cwd, env_allowlist=GIT_ENV, timeout=timeout, | ||
| preserve_newlines=preserve_newlines) | ||
| if done.exit_code != 0: | ||
| raise GitError(argv, done.stderr or done.stdout) | ||
| if preserve_newlines: | ||
| return done.stdout | ||
| return done.stdout.removesuffix("\n") if preserve_whitespace else done.stdout.strip() | ||
|
|
||
|
|
||
|
|
@@ -243,8 +249,9 @@ def is_dirty(wt: Path) -> bool: | |
| return bool(_git(Path(wt), "status", "--porcelain", "--untracked-files=all")) | ||
|
|
||
|
|
||
| def diff_since(wt: Path, sha: str) -> str: | ||
| return _git(Path(wt), "diff", "--no-color", sha, "HEAD") | ||
| def diff_since(wt: Path, sha: str) -> bytes: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The diff is decoded to text and then re-encoded to bytes
|
||
| diff = _git(Path(wt), "diff", "--no-color", sha, "HEAD", preserve_newlines=True) | ||
| return diff.encode("utf-8", errors="surrogateescape") | ||
|
|
||
|
|
||
| def merges_since(wt: Path, sha: str) -> list[str]: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,52 @@ def repo(tmp_path): | |
| # ----------------------------------------------------------------- prompt | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("legacy_encoding", [False, True]) | ||
| def test_committed_diff_preserves_git_bytes_for_native_evidence(git_repo, legacy_encoding): | ||
| import subprocess | ||
|
|
||
| from loopzero import runners, worktree | ||
|
|
||
| from .conftest import git | ||
|
|
||
| git(git_repo, "config", "core.autocrlf", "false") | ||
| path = git_repo / "README.md" | ||
| if legacy_encoding: | ||
| path.write_bytes(b"caf\xe9\n") | ||
| git(git_repo, "commit", "-am", "add Latin-1 text") | ||
| base = worktree.head(git_repo) | ||
| path.write_bytes(b"caf\xe8\r\n" if legacy_encoding else b"hello\r\n") | ||
| git(git_repo, "commit", "-am", "change line endings") | ||
| head = worktree.head(git_repo) | ||
| native = subprocess.check_output(["git", "diff", "--no-color", base, head], cwd=git_repo) | ||
| patch = worktree.diff_since(git_repo, base) | ||
| assert patch == native | ||
| diff = runners.render_patch(patch) | ||
| prompt = runners.build_prompt(kind="primary", head=head, task_text="", diff=diff) | ||
| if legacy_encoding: | ||
| assert "-caf\\xe9\n+caf\\xe8\r\n" in prompt | ||
| else: | ||
| assert b"-hello\n+hello\r\n" in native | ||
| assert prompt.split("\n", 1)[1].encode() == native | ||
| context = { | ||
| "cwd": str(git_repo), "head": head, "base": base, | ||
| "diff_sha256": hashlib.sha256(patch).hexdigest(), "diff_bytes": len(patch), | ||
| } | ||
| command = f"git -C {git_repo} rev-parse HEAD && git -C {git_repo} diff --no-color {base} {head}" | ||
| item = {"type": "command_execution", "id": "read-1", "command": command} | ||
| output = head + "\n" + native.decode(errors="replace") | ||
| events = [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The test builds a fake Codex output instead of exercising real Codex behavior The test creates |
||
| {"type": "item.started", "item": item}, | ||
| {"type": "item.completed", | ||
| "item": dict(item, status="completed", exit_code=0, aggregated_output=output)}, | ||
| ] | ||
| inspection = runners._native_inspection({"events": events}, context) | ||
| if legacy_encoding: | ||
| assert not inspection | ||
| else: | ||
| assert inspection["diff_event"]["end"] == len(output) | ||
|
|
||
|
|
||
| def test_prompt_frames_exact_task_and_raw_diff() -> None: | ||
| prompt = build_prompt(kind="delta", head="deadbeef", task_text=" Objective ", diff="+line\n") | ||
| header, diff = prompt.split("\n", 1) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.