Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-preparation-refresh-proof.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 7 additions & 17 deletions src/pkg/github/automerge/base_preparation_observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
67 changes: 59 additions & 8 deletions src/pkg/github/automerge/base_preparation_observation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -104,21 +147,29 @@ 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)
}
after, err := os.ReadFile(path)
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")
}
})
Expand Down
Loading