From d00628d51b2849c945824a47688e4df6cf20dfcf Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Fri, 30 Oct 2026 20:13:53 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Recognize=20own=20preparation=20?= =?UTF-8?q?refreshes=20across=20destination=20advancement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- .../fixed-preparation-refresh-proof.md | 1 + src/docs/hive-merge.md | 10 ++- .../automerge/base_preparation_observation.go | 24 ++----- .../base_preparation_observation_test.go | 67 ++++++++++++++++--- 4 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 changelog.d/fixed-preparation-refresh-proof.md diff --git a/changelog.d/fixed-preparation-refresh-proof.md b/changelog.d/fixed-preparation-refresh-proof.md new file mode 100644 index 0000000000..5388f8562b --- /dev/null +++ b/changelog.d/fixed-preparation-refresh-proof.md @@ -0,0 +1 @@ +- Recognize an active preparation owner's own base refresh when the destination advances beyond its recorded base snapshot, including after restart, using the existing App-authored unchanged-content proof while preserving CI, review, ownership, and fairness gates. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index bceb339669..bc08ec7db8 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -416,9 +416,11 @@ reviewer dispatch scheduling are separate policies. The review planner observes the durable preparation owner once per destination per pass. Only its exact live, CI-green, mergeable head with a verdict still owed receives `base-preparation` review priority; it does not need a ready train -seat. A refreshed head must be the merge of the recorded attempted head and -base and pass the existing same-patch proof, so an author's content push or -amended merge cannot inherit that priority. +seat. A refreshed head must pass the existing App-authored base-refresh proof: +the attempted head is preserved, the merged base lies on the destination branch, +and the PR's content is unchanged. The actual merged base can be newer than the +attempt's recorded base SHA; this remains valid after a restart without rewriting +ownership. An author's content push or amended merge cannot inherit priority. Closed, held, retargeted, released and expired owners receive no promotion. Observation is read-only and performs GitHub reads outside preparation locks. Busy valid reviews are never interrupted. After a confirmed preparation review @@ -431,6 +433,8 @@ the existing review order is unchanged. Priority is not review approval or merge eligibility: normal label, model, current-head verdict and merge gates still run. `review swarm kick planned` exposes `priority=base-preparation`; the normal waiting reasons continue to expose reviewer/parallel-slot exhaustion. +`automerge base preparation observation skipped` with +`reason=unverified-app-base-refresh` records a failed refreshed-head proof. Human pushes, direct merges and external automation can still move a base. Such a move is coalesced until the preparation owner qualifies again; it must diff --git a/src/pkg/github/automerge/base_preparation_observation.go b/src/pkg/github/automerge/base_preparation_observation.go index d0b0529afc..f90e1cb614 100644 --- a/src/pkg/github/automerge/base_preparation_observation.go +++ b/src/pkg/github/automerge/base_preparation_observation.go @@ -54,26 +54,16 @@ func (c *Engine) ActiveBasePreparation(ctx context.Context, owner, repo, base st return BasePreparationObservation{}, false, nil } if head != snapshot.Owner.Head { - // An accepted refresh changes the head, but an author's subsequent - // content push must not inherit its priority. Prove the exact merge - // of the attempted head and base, rather than trusting any new SHA. + // UpdateBranch merges the live destination tip, which can differ + // from the attempt's PR snapshot. Reuse the App-authored refresh + // proof against the destination ref, not that stale base SHA. if snapshot.Attempt.Head == "" || snapshot.Attempt.Base == "" { return BasePreparationObservation{}, false, nil } - commit, _, err := c.gh.Repositories.GetCommit(ctx, owner, repo, head, nil) - if err != nil { - return BasePreparationObservation{}, false, err - } - if commit.GetSHA() != head || len(commit.Parents) != 2 || - commit.Parents[0].GetSHA() != snapshot.Attempt.Head || commit.Parents[1].GetSHA() != snapshot.Attempt.Base { - return BasePreparationObservation{}, false, nil - } - // An author can amend a merge's tree while preserving both parents. - // Reuse the review planner's fail-closed same-patch proof as well. - patches, ok := c.transport.(interface { - ReviewHeadsSamePatch(context.Context, string, string, string, string) bool - }) - if !ok || !patches.ReviewHeadsSamePatch(ctx, owner+"/"+repo, snapshot.Attempt.Base, snapshot.Attempt.Head, head) { + if !c.baseRefresh.HeadIsBaseRefreshOf(ctx, c.gh.Repositories, owner, repo, snapshot.Owner.Number, + base, snapshot.Attempt.Head, head, c.transport.AppBotLogin()) { + c.info("automerge base preparation observation skipped", "repo", owner+"/"+repo, + "base", base, "pr", snapshot.Owner.Number, "head", head, "reason", "unverified-app-base-refresh") return BasePreparationObservation{}, false, nil } } diff --git a/src/pkg/github/automerge/base_preparation_observation_test.go b/src/pkg/github/automerge/base_preparation_observation_test.go index 1879d24389..6dae9fafab 100644 --- a/src/pkg/github/automerge/base_preparation_observation_test.go +++ b/src/pkg/github/automerge/base_preparation_observation_test.go @@ -16,7 +16,12 @@ import ( func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing.T) { now := time.Date(2026, 10, 30, 20, 40, 0, 0, time.UTC) - for _, mode := range []string{"admitted", "refreshed", "author-push", "amended-merge", "wrong-base-parent", "held", "closed", "retargeted", "expired", "released", "policy-off", "released-during-read", "unreadable"} { + for _, mode := range []string{ + "admitted", "refreshed", "advanced-base", "advanced-base-amended", "advanced-base-wrong-branch", "non-app", + "author-push", "amended-merge", "wrong-base-parent", "blob-changed-same-patch", + "missing-attempt", "proof-unavailable", "held", "closed", "retargeted", "expired", + "released", "policy-off", "released-during-read", "released-during-proof", "unreadable", + } { t.Run(mode, func(t *testing.T) { dir := filepath.Join(t.TempDir(), "preparation") store := NewBasePreparationStore(dir) @@ -31,6 +36,9 @@ func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing. if mode == "released" { st.Owner = preparationCandidate{} } + if mode == "missing-attempt" { + st.Attempts = nil + } return nil }); err != nil { t.Fatal(err) @@ -73,22 +81,57 @@ func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing. } _ = json.NewEncoder(w).Encode(pr) case strings.HasSuffix(r.URL.Path, "/commits/refreshed"): + if mode == "proof-unavailable" { + w.WriteHeader(http.StatusForbidden) + return + } parents := []map[string]string{{"sha": "old"}, {"sha": "base1"}} + if strings.HasPrefix(mode, "advanced-base") { + parents[1]["sha"] = "base2" + } if mode == "author-push" { parents = parents[:1] } if mode == "wrong-base-parent" { parents[1]["sha"] = "other-base" } - _ = json.NewEncoder(w).Encode(map[string]any{"sha": "refreshed", "parents": parents}) + author := testHiveAppBotLogin + if mode == "non-app" { + author = "external-author" + } + _ = json.NewEncoder(w).Encode(map[string]any{"sha": "refreshed", "parents": parents, + "author": map[string]string{"login": author}}) case strings.Contains(r.URL.Path, "/compare/"): + if !strings.Contains(r.URL.Path, "/compare/main...") { + t.Errorf("refresh proof used stale recorded base instead of destination: %s", r.URL.Path) + } + if mode == "released-during-proof" { + if err := store.change(key, func(st *basePreparation) error { + st.Owner = preparationCandidate{} + return nil + }); err != nil { + t.Error(err) + } + } patch := "+unchanged PR content" - if mode == "amended-merge" && strings.HasSuffix(r.URL.Path, "...refreshed") { + blob := "pr-blob" + if (mode == "amended-merge" || mode == "advanced-base-amended") && strings.HasSuffix(r.URL.Path, "...refreshed") { patch = "+author changed content in amended merge" } - _ = json.NewEncoder(w).Encode(map[string]any{"files": []map[string]string{{ - "filename": "widget.go", "status": "modified", "patch": patch, - }}}) + if mode == "blob-changed-same-patch" && strings.HasSuffix(r.URL.Path, "...refreshed") { + blob = "changed-pr-blob" + } + mergedBase := "base1" + if strings.HasPrefix(mode, "advanced-base") { + mergedBase = "base2" + } + if mode == "advanced-base-wrong-branch" { + mergedBase = "destination-ancestor" + } + _ = json.NewEncoder(w).Encode(map[string]any{ + "merge_base_commit": map[string]string{"sha": mergedBase}, + "files": []map[string]string{{"filename": "widget.go", "status": "modified", "patch": patch, "sha": blob}}, + }) default: t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) w.WriteHeader(http.StatusNotFound) @@ -104,13 +147,21 @@ func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing. if (err != nil) != (mode == "unreadable") { t.Fatalf("error=%v", err) } - want := mode == "admitted" || mode == "refreshed" + want := mode == "admitted" || mode == "refreshed" || mode == "advanced-base" if active != want { t.Fatalf("active=%v want=%v observation=%+v", active, want, observation) } if active && (observation.Number != 7 || observation.HeadSHA != pr.GetHead().GetSHA() || observation.Waiting != 1 || observation.Reason != basePreparationReady) { t.Fatalf("observation=%+v", observation) } + if mode == "advanced-base" { + restarted := preparationEngine(srv.URL, NewBasePreparationStore(dir)) + restarted.now = func() time.Time { return now } + got, active, err := restarted.ActiveBasePreparation(context.Background(), "acme", "widget", "main") + if err != nil || !active || got.HeadSHA != "refreshed" { + t.Fatalf("fresh engine lost advanced-base owner: observation=%+v active=%v error=%v", got, active, err) + } + } if (mode == "released" || mode == "expired" || mode == "policy-off" || mode == "unreadable") && reads != 0 { t.Fatalf("inactive owner made %d remote reads", reads) } @@ -118,7 +169,7 @@ func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing. if err != nil { t.Fatal(err) } - if mode != "released-during-read" && string(before) != string(after) { + if mode != "released-during-read" && mode != "released-during-proof" && string(before) != string(after) { t.Fatal("read-only observation rewrote durable state") } })