diff --git a/changelog.d/fixed-claim-ledger-scan-drops-webhook-claim.md b/changelog.d/fixed-claim-ledger-scan-drops-webhook-claim.md new file mode 100644 index 0000000000..dbebe788c9 --- /dev/null +++ b/changelog.d/fixed-claim-ledger-scan-drops-webhook-claim.md @@ -0,0 +1 @@ +- The duplicate-PR guard no longer drops a PR claim the webhook recorded after the claim scan's listing began; the issue stayed covered until the next scan re-listed the PR instead of being re-offered and getting a second PR ([#15559](https://github.com/hivecommons/hive/issues/15559) saw #15561 and #15567). diff --git a/src/pkg/github/prchurn.go b/src/pkg/github/prchurn.go index dbd10f04ed..7c5dbd159c 100644 --- a/src/pkg/github/prchurn.go +++ b/src/pkg/github/prchurn.go @@ -111,6 +111,11 @@ func (r IssuePRRecord) terminal() bool { type ClaimScan struct { Claims []IssueClaim History []IssuePRRecord + // StartedAt is when the scan began listing. A claim the webhook path + // recorded at or after this instant describes a PR the listing may not + // have contained yet, so an authoritative reconcile must not treat its + // absence from Claims as evidence the PR is gone (hivecommons/hive#15559). + StartedAt time.Time } // PRTerminalObservation reports a terminal PR state seen by the regular PR diff --git a/src/pkg/github/prclaims.go b/src/pkg/github/prclaims.go index 361a6759ef..8059cb1dc2 100644 --- a/src/pkg/github/prclaims.go +++ b/src/pkg/github/prclaims.go @@ -1112,7 +1112,7 @@ func (c *Client) FetchClaimScan(ctx context.Context, identity HiveIdentity) (Cla } } - return ClaimScan{Claims: claims, History: history}, firstErr + return ClaimScan{Claims: claims, History: history, StartedAt: now}, firstErr } func (c *Client) prTerminalMergedBy(ctx context.Context, owner, repo string, pr *gh.PullRequest) string { @@ -1648,6 +1648,22 @@ func OpenIssuesOf(result *ActionableResult) OpenIssueSet { // set — is what starts the settledClaimRetention countdown, not the merge. // Unsettled claims ignore the set; their lifetime is the scan's and the TTL's. func (l *ClaimLedger) ReconcileOpen(live []IssueClaim, authoritative bool, open OpenIssueSet) { + l.reconcileOpen(live, authoritative, open, time.Time{}) +} + +// ReconcileScan is ReconcileOpen for a complete scan: it additionally carries +// forward every unsettled claim recorded at or after scan.StartedAt that the +// scan did not re-list (hivecommons/hive#15559). The webhook path records a +// PR's claims the moment it opens; a scan whose listing began before that +// moment never saw the PR, so an authoritative map replacement that dropped +// the claim re-offered the issue while its PR was open — the hive then opened +// a second PR for it. The next scan lists the PR itself and confirms or drops +// the carried claim, so the carry-forward is bounded by one scan interval. +func (l *ClaimLedger) ReconcileScan(scan ClaimScan, authoritative bool, open OpenIssueSet) { + l.reconcileOpen(scan.Claims, authoritative, open, scan.StartedAt) +} + +func (l *ClaimLedger) reconcileOpen(live []IssueClaim, authoritative bool, open OpenIssueSet, listedAt time.Time) { if l == nil { return } @@ -1664,6 +1680,17 @@ func (l *ClaimLedger) ReconcileOpen(live []IssueClaim, authoritative bool, open } l.insertLocked(l.anchorFirstObservedLocked(prev, c)) } + if !listedAt.IsZero() { + // A claim recorded after the listing began is newer evidence than + // the listing; insertLocked's rank rule still lets a stronger + // live claim for the same issue win. + for _, c := range prev { + if c.Settled() || c.ObservedAt.Before(listedAt) { + continue + } + l.insertLocked(c) + } + } // Settled claims are carried forward (#7871, #8003). A verdict- // recovered claim describes a settling PR (or commit) that never // referenced the issue, so the scan that just ran did not and will not @@ -2270,7 +2297,7 @@ func ApplyDuplicatePRGuard( "cached_claims", ledger.Len(), ) } - ledger.ReconcileOpen(scan.Claims, authoritative, OpenIssuesOf(result)) + ledger.ReconcileScan(scan, authoritative, OpenIssuesOf(result)) // Churn history accumulates from whatever the scan DID return, partial or // not (#7995): a record is an observation that a pull request existed in a // state, which a later failure cannot falsify. diff --git a/src/pkg/github/prclaims_scan_carry_test.go b/src/pkg/github/prclaims_scan_carry_test.go new file mode 100644 index 0000000000..bfc3ee9982 --- /dev/null +++ b/src/pkg/github/prclaims_scan_carry_test.go @@ -0,0 +1,105 @@ +package github + +import ( + "testing" + "time" +) + +// hivecommons/hive#15559: Karibhou's PR #15561 ("Refs #15559") opened at +// 23:15:41Z and the webhook recorded its claim at 23:15:44Z. The eval cycle's +// claim scan had begun listing before the PR existed and reconciled +// authoritatively at 23:17:09Z, replacing the map without the claim. The issue +// went back to actionable and the scanner opened #15567 for it 14 minutes +// later while #15561 sat in review. + +func webhookOpenClaim(observed time.Time) IssueClaim { + return IssueClaim{ + Repo: "hive", Issue: 15559, + PRNumber: 15561, PRRepo: "hive", + PRURL: "https://github.com/hivecommons/hive/pull/15561", + PRAuthor: "Karibhou", PRState: PRStateOpen, + ExternalAuthor: true, Reference: true, + ObservedAt: observed, FirstObservedAt: observed, + } +} + +// TestReconcileScan_CarriesClaimRecordedAfterListingBegan pins the fix: a +// claim the webhook recorded after the scan started survives an authoritative +// reconcile that did not list its PR. +func TestReconcileScan_CarriesClaimRecordedAfterListingBegan(t *testing.T) { + now := time.Date(2026, 10, 29, 23, 17, 9, 0, time.UTC) + l := settledLedger(t, &now) + scanStart := now.Add(-3 * time.Minute) + claim := webhookOpenClaim(scanStart.Add(2 * time.Minute)) + if err := l.Record(claim); err != nil { + t.Fatal(err) + } + + l.ReconcileScan(ClaimScan{StartedAt: scanStart}, true, nil) + + got, ok := l.Lookup(claim.Repo, claim.Issue) + if !ok { + t.Fatal("claim recorded after the scan began must survive the authoritative reconcile") + } + if got.PRNumber != claim.PRNumber || !got.ObservedAt.Equal(claim.ObservedAt) { + t.Fatalf("carried claim changed: %+v", got) + } +} + +// TestReconcileScan_DropsClaimOlderThanListing: a claim the listing should +// have contained and did not is still dropped — the PR closed. +func TestReconcileScan_DropsClaimOlderThanListing(t *testing.T) { + now := time.Date(2026, 10, 29, 23, 17, 9, 0, time.UTC) + l := settledLedger(t, &now) + scanStart := now.Add(-3 * time.Minute) + if err := l.Record(webhookOpenClaim(scanStart.Add(-time.Minute))); err != nil { + t.Fatal(err) + } + + l.ReconcileScan(ClaimScan{StartedAt: scanStart}, true, nil) + + if _, ok := l.Lookup("hive", 15559); ok { + t.Fatal("claim observed before the listing began must be dropped when the scan no longer lists its PR") + } +} + +// TestReconcileScan_LiveStrongerClaimStillWins: the carry-forward goes through +// the rank rule, so a hive-authored closing claim the scan DID list for the +// same issue is not displaced by the carried weak one. +func TestReconcileScan_LiveStrongerClaimStillWins(t *testing.T) { + now := time.Date(2026, 10, 29, 23, 17, 9, 0, time.UTC) + l := settledLedger(t, &now) + scanStart := now.Add(-3 * time.Minute) + if err := l.Record(webhookOpenClaim(scanStart.Add(time.Minute))); err != nil { + t.Fatal(err) + } + strong := IssueClaim{ + Repo: "hive", Issue: 15559, PRNumber: 15567, PRRepo: "hive", + PRAuthor: "hivecommons-hive[bot]", PRState: PRStateOpen, Closing: true, + ObservedAt: scanStart, FirstObservedAt: scanStart, + } + + l.ReconcileScan(ClaimScan{Claims: []IssueClaim{strong}, StartedAt: scanStart}, true, nil) + + got, ok := l.Lookup("hive", 15559) + if !ok || got.PRNumber != strong.PRNumber { + t.Fatalf("live stronger claim must win, got %+v", got) + } +} + +// TestReconcileScan_ZeroStartCarriesNothing: a scan with no StartedAt (the +// fail-closed path reuses the persisted ledger; legacy callers) behaves as +// ReconcileOpen always did. +func TestReconcileScan_ZeroStartCarriesNothing(t *testing.T) { + now := time.Date(2026, 10, 29, 23, 17, 9, 0, time.UTC) + l := settledLedger(t, &now) + if err := l.Record(webhookOpenClaim(now)); err != nil { + t.Fatal(err) + } + + l.ReconcileScan(ClaimScan{}, true, nil) + + if _, ok := l.Lookup("hive", 15559); ok { + t.Fatal("without a listing start the authoritative reconcile must replace the map as before") + } +}