Repository navigation
Review conflict resolutions in the available delta round #1731
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
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 |
|---|---|---|
|
|
@@ -848,11 +848,12 @@ def cmd_review(args: argparse.Namespace, wt: Path, config: Config) -> int: | |
| candidates = [family for family in config.reviewers if family not in authors] | ||
| if result.fallback and not _published_fallback(result.fallback, candidates, excluded): | ||
| raise CliError("saved fallback note is not in the published form; run without --repost") | ||
| allowed, _ = _decide_kind( | ||
| allowed, reviewed = _decide_kind( | ||
| _pr_markers(reviews, head, config.review_publishers, repo=config.repo, number=pr.number), head | ||
| ) | ||
| if allowed != kind: | ||
| raise CliError("saved review kind differs from the remaining review budget") | ||
| _delta_patch(wt, config, reviewed, head, pr.number) | ||
| else: | ||
| try: | ||
| result = _run_review(wt, config, head, kind, reviewed, args.model, args.effort, | ||
|
|
@@ -1003,12 +1004,27 @@ def _confirm_review(wt: Path, config: Config, pr: github.PR, result: ReviewResul | |
| return True | ||
|
|
||
|
|
||
| def _delta_merge_recovery(wt: Path, reviewed: str, merge: str) -> str: | ||
| pre_merge = _git(wt, "rev-parse", f"{merge}^1") | ||
| if not _is_ancestor(wt, reviewed, pre_merge): | ||
| return f"replay the fixes on the reviewed head {reviewed[:12]}" | ||
| return (f"save the current tip on a backup branch, use `git reset --hard {pre_merge}`, " | ||
| "and cherry-pick any later fixes from the backup") | ||
| def _delta_patch(wt: Path, config: Config, since: str | None, head: str, number: int | None) -> bytes: | ||
| if since is None: | ||
| return b"" | ||
| if not _is_ancestor(wt, since, head): | ||
| raise CliError( | ||
| f"reviewed head {since[:12]} is not an ancestor of this branch. Find the original tip with `git reflog`. " | ||
| "Back up your branch, reset to that tip and replay later fixes. To publish the restored head, " | ||
| f"run `loopzero pr`, then `loopzero review` before merging {config.base_branch}, or open a new PR" | ||
| ) | ||
| if not worktree.merges_since(wt, since): | ||
| return worktree.diff_since(wt, since) | ||
| patch = (eligibility.base_merge_material(config.repo, number, since, head, config.base_branch, clean=False) | ||
| if number is not None else None) | ||
|
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: Bound or fetch failures produce history-rewrite advice
|
||
| if patch is None: | ||
| raise CliError( | ||
| f"could not verify merge history: {since[:12]} must be on the first-parent history and " | ||
| f"merged parents must come from {config.base_branch}. Save your work, then replay the fixes " | ||
| f"on the reviewed head {since[:12]}, including resolution edits. Push with `loopzero pr` " | ||
| "and run `loopzero review`, or open a new PR" | ||
| ) | ||
| return patch | ||
|
|
||
|
|
||
| def _run_review( | ||
|
|
@@ -1018,23 +1034,8 @@ def _run_review( | |
| ) -> ReviewResult: | ||
| lineage_base = _primary_base(wt, config) | ||
| since = reviewed if kind == "delta" else lineage_base | ||
| if kind == "delta" and not _is_ancestor(wt, since, head): | ||
| raise CliError( | ||
| f"reviewed head {since[:12]} is not an ancestor of this branch. Find the original " | ||
| "branch tip with `git reflog`. Save the current tip on a backup branch, then use " | ||
| "`git reset --hard <original-tip>` and replay any later fixes. To publish the restored " | ||
| f"head, run `loopzero pr`, then `loopzero review` before merging {config.base_branch}, or open a new PR " | ||
| "from the current branch" | ||
| ) | ||
| if kind == "delta" and (merges := worktree.merges_since(wt, since)): | ||
| recovery = _delta_merge_recovery(wt, since, merges[0]) | ||
| raise CliError( | ||
| f"merge commit {merges[0][:12]} since the reviewed head {since[:12]} would put the " | ||
| f"merged-in changes in the delta material. To recover, {recovery}. " | ||
| f"To publish the recovered head, run `loopzero pr`, then `loopzero review` " | ||
| f"before merging {config.base_branch} again" | ||
| ) | ||
| patch = worktree.diff_since(wt, since) | ||
| patch = (_delta_patch(wt, config, since, head, publication[1] if publication else None) | ||
|
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 only caller of |
||
| if kind == "delta" else worktree.diff_since(wt, since)) | ||
| diff = runners.render_patch(patch) | ||
| task = worktree.task_text(wt) | ||
| authors = runners.author_families(wt, head, lineage_base) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import json | ||
| import re | ||
| import sys | ||
| from collections.abc import Callable | ||
| from pathlib import Path | ||
|
|
||
| from loopzero import candidate, config, github, runners | ||
|
|
@@ -95,36 +96,52 @@ def eligible_review(reviews: list[dict], head: str, publishers: tuple[str, ...], | |
| return latest if verdict else None | ||
|
|
||
|
|
||
| def _clean_base_merges(repo: str, number: int, reviewed: str, head: str, base_branch: str) -> bool: | ||
| """Prove each intervening first-parent commit is an unmodified merge from the base.""" | ||
| def _verified_merge(git: Callable[..., str], merge: str, first: str, second: str, | ||
| base: str, *, clean: bool) -> bool: | ||
| git("merge-base", "--is-ancestor", second, base) | ||
| merge_bases = git("merge-base", "--all", first, second).splitlines() | ||
| # A shallow root outside the known merge bases could hide another base. | ||
| if git("rev-list", "--max-parents=0", first, second, "--not", *merge_bases): | ||
| return False | ||
| return not clean or git("merge-tree", "--write-tree", first, second) == git("rev-parse", f"{merge}^{{tree}}") | ||
|
|
||
|
|
||
| def base_merge_material( | ||
| repo: str, number: int, reviewed: str, head: str, base_branch: str, *, clean: bool, | ||
| ) -> bytes | None: | ||
| """Return a verified delta patch, or empty bytes for proven clean inheritance.""" | ||
| try: | ||
| pr = github.api_get(f"repos/{repo}/pulls/{number}") | ||
| base = pr["base"]["sha"] | ||
| if (pr["head"]["sha"] != head or pr["base"]["ref"] != base_branch | ||
| or not re.fullmatch(r"[0-9a-f]{40}", base)): | ||
| return False | ||
| return None | ||
| with candidate.fetched_git(repo, [reviewed, head, base]) as git: | ||
| history = git("rev-list", "--first-parent", "--parents", f"{reviewed}..{head}") | ||
| cursor = head | ||
| row_lengths = {3} if clean else {2, 3} | ||
| for row in history.splitlines(): | ||
| commits = row.split() | ||
| if len(commits) != 3 or commits[0] != cursor: | ||
| return False | ||
| merge, first, second = commits | ||
| git("merge-base", "--is-ancestor", second, base) | ||
| merge_bases = git("merge-base", "--all", first, second).splitlines() | ||
| # A shallow root outside the known merge bases could hide another base. | ||
| if git("rev-list", "--max-parents=0", first, second, "--not", *merge_bases): | ||
| return False | ||
| tree = git("merge-tree", "--write-tree", first, second) | ||
| if tree != git("rev-parse", f"{merge}^{{tree}}"): | ||
| return False | ||
| cursor = first | ||
| return cursor == reviewed | ||
| if len(commits) not in row_lengths or commits[0] != cursor: | ||
| return None | ||
| cursor = commits[1] | ||
| if len(commits) == 2: | ||
| continue | ||
| if not _verified_merge(git, *commits, base, clean=clean): | ||
| return None | ||
| if cursor != reviewed: | ||
| return None | ||
| if clean: | ||
| return b"" | ||
| patch = git("log", "--first-parent", "--reverse", "--remerge-diff", "-p", "--format=", | ||
| "--no-color", "--no-ext-diff", "--no-textconv", "--text", | ||
|
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: Delta material relies on
|
||
| "--ignore-submodules=none", "--submodule=short", | ||
| f"{reviewed}..{head}", preserve_newlines=True) | ||
| return patch.encode("utf-8", errors="surrogateescape") | ||
| except github.GhUnavailable: | ||
| raise | ||
| except (candidate.CandidateError, github.GhError, KeyError, OSError, TypeError, ValueError): | ||
| return False | ||
| return None | ||
|
|
||
|
|
||
| def readiness_review(reviews: list[dict], head: str, publishers: tuple[str, ...], *, | ||
|
|
@@ -158,7 +175,7 @@ def readiness_review(reviews: list[dict], head: str, publishers: tuple[str, ...] | |
| return None | ||
| elif not (verdict.startswith("approve** (") and state in {"APPROVED", "COMMENTED"}): | ||
| return None | ||
| if not _clean_base_merges(repo, number, reviewed, head, base_branch): | ||
| if base_merge_material(repo, number, reviewed, head, base_branch, clean=True) is None: | ||
| return None | ||
| return latest | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Shortened wording drops what a remerge edit is measured against
The old text said the delta gets the changes from each automatic remerge to its recorded merge tree. The new text says only "edits to each automatic remerge", so it no longer says what those edits are compared with. A reader could take it to mean the whole merge diff against the first parent, which would pull in unrelated base changes. The task's acceptance criteria rule that out. If the size budget allows, a short fix is "edits from each automatic remerge to its recorded merge". The rewrapped lines are also about 115 characters wide, while the rest of this list wraps near 80. That passes the existing checks (
tests/test_size_budget.pyonly limitscore/CONTRACT.md's size), so it is only a style point.