Repository navigation
Review base merge resolutions in the available delta #1259
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
5a8bb70
ce9a0be
0b6463e
7be5115
05fac16
930d5b8
7b9c73c
595b57f
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 |
|---|---|---|
|
|
@@ -829,11 +829,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, | ||
|
|
@@ -984,12 +985,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) | ||
| if patch is 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: Transient fetch/API failures produce advice to rewrite history
|
||
| 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( | ||
|
|
@@ -999,23 +1015,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) | ||
| 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,34 +96,51 @@ 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 | ||
| 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 {2, 3} or commits[0] != cursor: | ||
| return None | ||
| cursor = commits[1] | ||
| if len(commits) == 2: | ||
| if clean: | ||
| return None | ||
| 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=", | ||
|
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: Merge-delta patch drops
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: Diff hardening flags are now copied in three places The flag set |
||
| "--no-color", "--no-ext-diff", "--no-textconv", "--text", | ||
| "--ignore-submodules=none", "--submodule=short", | ||
| f"{reviewed}..{head}", preserve_newlines=True) | ||
| return patch.encode("utf-8", errors="surrogateescape") | ||
| except (candidate.CandidateError, github.GhError, KeyError, OSError, TypeError, ValueError): | ||
| return False | ||
| return None | ||
|
|
||
|
|
||
| def readiness_review(reviews: list[dict], head: str, publishers: tuple[str, ...], *, | ||
|
|
@@ -156,7 +174,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: Repost re-verifies merge history but discards the patch
On
--repost,_delta_patchruns only as a gate: it does a full isolated fetch and remerge, then throws the result away. It does not compare the patch with what the saved review actually saw, even though Codex evidence recordsdiff_sha256. Either compare the hash when it is available, or add a comment that this call is only an ancestry/base-provenance check. Otherwise a later reader may assume repost binds the saved review to the current material.