diff --git a/changelog.d/fixed-verified-open-supersedes-done.md b/changelog.d/fixed-verified-open-supersedes-done.md new file mode 100644 index 0000000000..46d5c316a5 --- /dev/null +++ b/changelog.d/fixed-verified-open-supersedes-done.md @@ -0,0 +1 @@ +- `hive/verified-open` and `hive/already-done` no longer stand together. projectbluefin/testsuite#1023 got already-done, then verified-open two hours later, and the inbox offered it as "verified done but could not close". The label relay's verified-open now removes the done labels, a verified already-done removes verified-open, and the issue stage sweep settles issues already wearing both from their label events (the later verdict stands). diff --git a/src/docs/dashboard.md b/src/docs/dashboard.md index 2d9fee6f13..8b9b325c76 100644 --- a/src/docs/dashboard.md +++ b/src/docs/dashboard.md @@ -639,6 +639,8 @@ in exactly one band, while non-winning states remain as badges on the pill: labels still apply. When the only done evidence is `stage/040-covered-by-pr` (or a later mirrored PR stage), the ✓ signal reads `covered by an open PR (stage/040) — check it merged before closing`, not `already done`. + `hive/verified-open` and `hive/already-done` are not worn together for + long: the issue stage sweep keeps the later verdict and removes the other. Precedence is confirm & close → parked → claimed → needs triage → unclaimed, so a label that parks an issue beats an assignment and done beats all other display states. @@ -837,6 +839,8 @@ hand-merge rule. `hive/verified-open` (the agent found the work unfinished) cancels `hive/likely-done` for rules (6) and (7) and for the safety net, so an issue carrying both is neither awaiting the reporter nor verified done; `hive/already-done` and `hive/covered-by-pr` are unaffected. +An issue does not keep both `hive/verified-open` and `hive/already-done`: the +issue stage sweep keeps the later verdict and removes the other. **Runs and the approval desk.** Two waits live outside GitHub, so no label records them. An active run the Runs tab shows as `waiting_on: human` makes the diff --git a/src/docs/github-write-surface.md b/src/docs/github-write-surface.md index cf47fbdc3a..13af6c5f3d 100644 --- a/src/docs/github-write-surface.md +++ b/src/docs/github-write-surface.md @@ -101,7 +101,9 @@ One reserved label refuses the whole request — nothing in it is applied — so batch can never partially land. When the only refused part is removing `hive/likely-done` or `hive/covered-by-pr` alongside an add of `hive/verified-open`, the refusal tells the agent to resend the add on its own, -because the hive removes those labels itself. `hive/verified-open`, the one +because the hive removes those labels itself. The same holds for +`hive/already-done`: when the add of `hive/verified-open` lands, the relay +removes `hive/already-done` and `hive/likely-done` from the issue. `hive/verified-open`, the one `hive/` label an agent may add, is created on the repository first when it does not exist. The refusal is a policy denial like any other: `.denied`, a result file naming the labels, and an audit entry. A lane diff --git a/src/docs/labels-and-control-signals/reference.md b/src/docs/labels-and-control-signals/reference.md index 69d94c2f75..9118fe9fde 100644 --- a/src/docs/labels-and-control-signals/reference.md +++ b/src/docs/labels-and-control-signals/reference.md @@ -131,8 +131,8 @@ Some hive-authored labels look like provenance but are read as signals, so they | `🐝 covered by PR` (formerly `hive/covered-by-pr`; **retired in favour of `stage/040-covered-by-pr`**, see [Stage labels](stage-labels.md#stage-labels-stagennn--the-pipeline-state-machine); the stage reconciler removes it from open items) | Issues | PR-claim label sync and dashboard | Open PR is verified as related; issue remains actionable. For a closing or branch-name claim Hive also links the PR to the issue via GitHub's `addCloseIssueReferences`, so GitHub's own issue list shows the PR icon ([#11888](https://github.com/hivecommons/hive/issues/11888), [#12091](https://github.com/hivecommons/hive/issues/12091)). A reference claim (`Refs #N`, `Part of #N` — including a branch named after the issue when the body says `Refs #N`) is never linked, because a closing link makes GitHub close the issue when the partial PR merges; if the hive App itself linked such a pair earlier, the next sweep retracts the link with `removeCloseIssueReferences`, while links made by a person are left in place and logged ([#12144](https://github.com/hivecommons/hive/issues/12144)). The dashboard's own `🔗` pill does not depend on the GitHub link. Labels are synced only for actionable issues, so stale labels can remain on held/exempt/filtered issues. | Display-only | Hive | Claim sync after enumeration | Hive removes when actionable issue no longer has open PR evidence | Fixed label (`src/pkg/github/prclaims.go:1380-1435`) | | `hive/likely-done` | Issues | PR-claim label sync and dashboard | Merged PR is verified as related while issue remains open; issue remains actionable until GitHub/operator closes or confirms. Requires a closing keyword or branch-name claim: a merged `Refs #N` / `Part of #N` applies `hive/partially-addressed` instead ([#12383](https://github.com/hivecommons/hive/issues/12383)). | Display-only | Hive | Claim sync after enumeration | Hive removes when actionable evidence no longer says likely done | Fixed label (`src/pkg/github/prclaims.go`) | | `hive/partially-addressed` | Issues | Close-on-merge sweep, PR-claim label sync and task-list sweep | A merged PR covered only part of the issue ([#11931](https://github.com/hivecommons/hive/issues/11931)): applied instead of `hive/likely-done` when the PR has no closing keyword (`Fixes`/`Closes`/`Resolves #N`) for the issue (a `Refs #N`-only PR leaves the issue open on merge; a body that adds `Closes #N` to a partial `Refs #N` note is refused by every merge lane with `closing-contradiction:#N` until one of the two is dropped, [#15025](https://github.com/hivecommons/hive/issues/15025)), its body carries a remainder/out-of-scope paragraph, or none of the backtick-quoted paths cited in the issue body is among its changed files. No `/fixed` prompt is posted, `hive/likely-done` is suppressed, and the issue stays in the actionable pool; the `` comment lists the cited paths no merged PR changed as `Not yet touched: ...`. | Display; suppresses likely-done | Hive | Close-on-merge sweep for issues behind a confirmation gate | Hive removes it when a later merged PR fully covers the issue and `hive/likely-done` is applied; humans may remove it | Fixed label (`src/pkg/github/close_on_merge.go`, `src/pkg/github/task_list_sweep.go`) | -| `hive/verified-open` | Issues | Verify-once kick path, PR-claim label sync and close-on-merge | A `hive/likely-done` / already-done claim from a merged PR was checked and the issue is NOT finished ([#11931](https://github.com/hivecommons/hive/issues/11931)): the agent that verified it labels the issue (`src/pkg/scheduler/kickmessage.go` "verify once" instruction) so later kicks say "already verified — implement the rest" and the claim sync never re-applies `hive/likely-done` from the same stale claim (`src/pkg/github/prclaims.go`). Merged-PR evidence, orthogonal to the stage: the issue keeps moving `010 → 020 → 030 → 040` as new work starts. | Suppresses likely-done; kick wording | Agent (verify-once kick) / Hive | Verify-once kick, claim sync | Human removes when the issue is really done, or close-on-merge closes the issue | Fixed label (`src/pkg/github/prclaims.go` `VerifiedOpenLabel`, `src/cmd/hive/verifyonce.go`) | -| `hive/already-done` | Issues | Contributor queue, already-done verdict close path, and dashboard | Contributor already-done verdict/confirmation; default contributor skip label and done band. When the verdict cites a PR that the API verifies as merged on the default branch, Hive labels the issue and runs the normal completed close path; human-filed bugs close too unless `hive: needs-confirmation` or `issues.reporter_confirmation` opted into the reporter gate. A verdict that cites no PR/commit, or whose citation fails verification, is never labelled: Hive comments that the contributor reports it may be resolved and holds it from offers for `hub.contribute_already_done_hold_days`. | Contributor hard skip; dashboard display; conditional close | Hive/contributor flow | Contributor admission, verified verdict settlement, issue close gate | Human removes/reopens or confirms reporter-gated bugs | `hub.contribute_already_done_label`, fixed label (`src/pkg/config/hub_config.go:196-208`, `src/pkg/config/defaults.go:275-289`, `src/pkg/dashboard/contribute_verdict_settle.go`, `src/pkg/github/issue_close.go`) | +| `hive/verified-open` | Issues | Verify-once kick path, PR-claim label sync and close-on-merge, label relay and issue stage sweep | A `hive/likely-done` / already-done claim from a merged PR was checked and the issue is NOT finished ([#11931](https://github.com/hivecommons/hive/issues/11931)): the agent that verified it labels the issue (`src/pkg/scheduler/kickmessage.go` "verify once" instruction) so later kicks say "already verified — implement the rest" and the claim sync never re-applies `hive/likely-done` from the same stale claim (`src/pkg/github/prclaims.go`). Merged-PR evidence, orthogonal to the stage: the issue keeps moving `010 → 020 → 030 → 040` as new work starts. | Suppresses likely-done; kick wording | Agent (verify-once kick) / Hive | Verify-once kick, claim sync, label relay add, issue stage sweep | Human removes when the issue is really done, or close-on-merge closes the issue; a later verified `hive/already-done` removes it, and the issue stage sweep keeps the later of the two verdicts | Fixed label (`src/pkg/github/prclaims.go` `VerifiedOpenLabel`, `src/cmd/hive/verifyonce.go`) | +| `hive/already-done` | Issues | Contributor queue, already-done verdict close path, and dashboard | Contributor already-done verdict/confirmation; default contributor skip label and done band. When the verdict cites a PR that the API verifies as merged on the default branch, Hive labels the issue and runs the normal completed close path; human-filed bugs close too unless `hive: needs-confirmation` or `issues.reporter_confirmation` opted into the reporter gate. A verdict that cites no PR/commit, or whose citation fails verification, is never labelled: Hive comments that the contributor reports it may be resolved and holds it from offers for `hub.contribute_already_done_hold_days`. | Contributor hard skip; dashboard display; conditional close | Hive/contributor flow | Contributor admission, verified verdict settlement, issue close gate | Human removes/reopens or confirms reporter-gated bugs; a later `hive/verified-open` (label relay) removes it, and the issue stage sweep keeps the later of the two verdicts | `hub.contribute_already_done_label`, fixed label (`src/pkg/config/hub_config.go:196-208`, `src/pkg/config/defaults.go:275-289`, `src/pkg/dashboard/contribute_verdict_settle.go`, `src/pkg/github/issue_close.go`) | | `blocked` | Issues | Contributor queue, dashboard issue bands, un-park sweep | Excluded from agent enumeration, automated claims and contributor offers; shown in the waiting band. A contributor verdict naming an open blocker records a native issue dependency or `` marker ([#15023](https://github.com/hivecommons/hive/issues/15023)); a verdict requiring a person uses `needs-human` instead. | Hard suppress | Human, or Hive App on a contributor blocked verdict | Contributor admission/dashboard render | Follow the [blocked reference](#blocked): human-applied or re-applied blocks are not automatically lifted. Marker-less review-backlog cleanup does not lift contributor-verdict blocks. | `hub.contribute_skip_labels`, dashboard bands (`src/pkg/config/hub_config.go:136-155`, `src/pkg/config/hub_config.go:469-492`, `src/docs/dashboard.md:105-140`) | | `needs-direction` (retired) | Issues | Issue stage reconciler (migration only) | Retired ([#12683](https://github.com/hivecommons/hive/issues/12683)): nothing writes it. `stage/045-needs-decision` is the single "issue parked for a maintainer ruling" signal (owner decision: `stage/085-needs-decision` is PR-only). An open issue still carrying it is read as parked (same hard suppress, same un-park), the issue stage reconciler adds `stage/045-needs-decision` and then removes the retired label, and the label definition is deleted from the repo once no open item carries it. The scanner's "needs-direction (scanner): …" comment may remain as the explanation text accompanying the 045 transition — no label. | Hard suppress until migrated | — (legacy) | Issue stage reconciler | Migrated to `stage/045-needs-decision` (`src/pkg/github/legacy_decision_labels.go`) | Fixed legacy spelling (`src/pkg/config/issue_filter.go`) | | `needs-decision` (retired) | Issues, PRs | Issue stage reconciler (migration only); PR lanes read it as a hold modifier until gone | Retired ([#12683](https://github.com/hivecommons/hive/issues/12683)): nothing writes it. The relay's `hive-open-issue --needs-decision` and the configured `project.issue_filter.hard_suppress_labels.needs_decision` / `hub.contribute_needs_decision_label` now default to `stage/045-needs-decision`; a configured legacy spelling (or `stage/085-needs-decision`) is upgraded. Migration as for `needs-direction`. | Hard suppress until migrated | — (legacy) | Issue stage reconciler | Migrated to `stage/045-needs-decision` (`src/pkg/github/legacy_decision_labels.go`) | `hub.contribute_needs_decision_label` (`src/pkg/config/hub_config.go`, `src/pkg/config/issue_filter.go`) | @@ -650,11 +650,13 @@ A merged PR covered only part of the issue ([#11931](https://github.com/hivecomm A `hive/likely-done` / already-done claim from a merged PR was checked and the issue is NOT finished ([#11931](https://github.com/hivecommons/hive/issues/11931)): the agent that verified it labels the issue (`src/pkg/scheduler/kickmessage.go` "verify once" instruction) so later kicks say "already verified — implement the rest" and the claim sync never re-applies `hive/likely-done` from the same stale claim (`src/pkg/github/prclaims.go`). Merged-PR evidence, orthogonal to the stage: the issue keeps moving `010 → 020 → 030 → 040` as new work starts. +The two verdicts never stand together. When the label relay adds `hive/verified-open` it removes `hive/already-done` and `hive/likely-done`, and a contributor's verified already-done removes `hive/verified-open`. An issue that already wears both (projectbluefin/testsuite#1023: already-done at 01:47Z, verified-open at 03:13Z) is settled by the issue stage sweep from the issue's label events: the later add stands and the other label comes off, through the write-back gate (`src/pkg/github/done_verdict_labels.go`). Only the default `hive/already-done` spelling is reconciled, not a renamed `hub.contribute_already_done_label`. + **Who applies it:** Agent (verify-once kick) / Hive. -**Checked by:** Verify-once kick path, PR-claim label sync and close-on-merge. **When:** Verify-once kick, claim sync. +**Checked by:** Verify-once kick path, PR-claim label sync, close-on-merge, the label relay and the issue stage sweep. **When:** Verify-once kick, claim sync, the relay's add, every issue stage sweep. -**Release or override.** Human removes when the issue is really done, or close-on-merge closes the issue +**Release or override.** Human removes when the issue is really done, or close-on-merge closes the issue. A later verified `hive/already-done` removes it, and the issue stage sweep keeps the later of the two verdicts when both are worn **Configuration and implementation.** Fixed label (`src/pkg/github/prclaims.go` `VerifiedOpenLabel`, `src/cmd/hive/verifyonce.go`) @@ -668,7 +670,7 @@ Contributor already-done verdict/confirmation; default contributor skip label an **Checked by:** Contributor queue, already-done verdict close path, and dashboard. **When:** Contributor admission, verified verdict settlement, issue close gate. -**Release or override.** Human removes/reopens or confirms reporter-gated bugs +**Release or override.** Human removes/reopens or confirms reporter-gated bugs. A later `hive/verified-open` (label relay) removes it, and the issue stage sweep keeps the later of the two verdicts when both are worn **Configuration and implementation.** `hub.contribute_already_done_label`, fixed label (`src/pkg/config/hub_config.go:253-259,422-430`, `src/pkg/dashboard/contribute_verdict_settle.go`, `src/pkg/github/issue_close.go`) diff --git a/src/pkg/dashboard/contribute_verdict_settle.go b/src/pkg/dashboard/contribute_verdict_settle.go index 4085b2b8a1..a9d639c407 100644 --- a/src/pkg/dashboard/contribute_verdict_settle.go +++ b/src/pkg/dashboard/contribute_verdict_settle.go @@ -298,6 +298,10 @@ func (h *ContributeWSHub) markAlreadyDoneIssue(ctx context.Context, repo string, if err := h.server.deps.GHClient.AddLabels(ctx, repo, number, []string{label}); err != nil { return err } + // A verified already-done supersedes an earlier hive/verified-open, so + // the issue never wears both verdicts. + // Best-effort: the stage sweep settles a pair left behind. + _ = h.server.deps.GHClient.RemoveLabelsFresh(ctx, repo, number, ghpkg.VerifiedOpenLabel) // writeback:solicited the contributor's verified already-done verdict if !closeIssue { return nil } diff --git a/src/pkg/dashboard/contribute_verdict_settle_ghclient_test.go b/src/pkg/dashboard/contribute_verdict_settle_ghclient_test.go index b16f7db79f..0d0820fc9b 100644 --- a/src/pkg/dashboard/contribute_verdict_settle_ghclient_test.go +++ b/src/pkg/dashboard/contribute_verdict_settle_ghclient_test.go @@ -35,6 +35,7 @@ type settleGHFixture struct { existingComments []string commentsListErr bool issueLabels []string + labelsRemoved []string } func (f *settleGHFixture) server(t *testing.T) *httptest.Server { @@ -104,6 +105,15 @@ func (f *settleGHFixture) server(t *testing.T) *httptest.Server { } _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "closed"}) case r.Method == http.MethodDelete && strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/7/labels/"): + name := strings.TrimPrefix(r.URL.Path, "/repos/o/r/issues/7/labels/") + for i, l := range f.issueLabels { + if strings.EqualFold(l, name) { + f.issueLabels = append(f.issueLabels[:i], f.issueLabels[i+1:]...) + f.labelsRemoved = append(f.labelsRemoved, name) + _ = json.NewEncoder(w).Encode([]map[string]any{}) + return + } + } w.WriteHeader(http.StatusNotFound) _ = json.NewEncoder(w).Encode(map[string]any{"message": "Not Found"}) default: @@ -521,3 +531,19 @@ func TestVerdictSettleGH_ContextCancelledDuringVerifyIsUnverified(t *testing.T) t.Fatalf("non-already-done disposition = %q, want empty", got) } } + +// projectbluefin/testsuite#1023: a verified already-done takes an earlier +// hive/verified-open off, so the issue never wears both verdicts. +func TestVerdictSettleGH_MarkAlreadyDone_RemovesVerifiedOpen(t *testing.T) { + fx := &settleGHFixture{issueLabels: []string{"kind/bug", ghpkg.VerifiedOpenLabel}} + hub, _ := settleGHHub(t, fx) + claim := ghpkg.IssueClaim{PRNumber: 41, PRURL: "https://github.com/o/r/pull/41", MergedPR: true} + if err := hub.markAlreadyDoneIssue(context.Background(), "o/r", 7, claim, "danathar", false); err != nil { + t.Fatalf("markAlreadyDoneIssue: %v", err) + } + fx.mu.Lock() + defer fx.mu.Unlock() + if len(fx.labelsRemoved) != 1 || fx.labelsRemoved[0] != ghpkg.VerifiedOpenLabel { + t.Fatalf("labels removed = %v, want hive/verified-open", fx.labelsRemoved) + } +} diff --git a/src/pkg/github/done_verdict_labels.go b/src/pkg/github/done_verdict_labels.go new file mode 100644 index 0000000000..df3442c299 --- /dev/null +++ b/src/pkg/github/done_verdict_labels.go @@ -0,0 +1,84 @@ +package github + +import ( + "context" + "log/slog" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/writeback" +) + +// reconcileDoneVerdictLabels settles an issue that wears both +// hive/already-done and hive/verified-open: the later verdict stands and the +// other label comes off. projectbluefin/testsuite#1023 got already-done at +// 11-04 01:47Z and verified-open at 03:13Z ("Nothing more can be implemented +// here until #706"), and the inbox offered it as "verified done but could +// not close". New verdicts remove the other label as they land (the label +// relay and the contribute verdict); this heals issues labelled before that. +// It reads the issue's events only for an issue wearing both, and writes +// through the write-back gate. Returns whether a label was removed. +func reconcileDoneVerdictLabels(ctx context.Context, client *Client, in IssueStageInput, logger *slog.Logger) bool { + if client == nil || client.client == nil || !issueHasLabel(in.Labels, AlreadyDoneLabel) || !issueHasLabel(in.Labels, VerifiedOpenLabel) { + return false + } + owner, name := client.splitRepo(in.Repo) + done, open, err := client.latestLabeledAt(ctx, owner, name, in.Number, AlreadyDoneLabel, VerifiedOpenLabel) + if err != nil || done.IsZero() || open.IsZero() || done.Equal(open) { + if err != nil && logger != nil { + logger.Warn("done verdict labels: reading issue events failed; retrying next sweep", "repo", in.Repo, "issue", in.Number, "error", err) + } + return false + } + stale := AlreadyDoneLabel + if done.After(open) { + stale = VerifiedOpenLabel + } + item, err := client.WritebackItemForNumber(ctx, in.Repo, in.Number) + if err != nil { + return false + } + if d := client.WritebackAllowed(owner+"/"+name, item, writeback.KindStageLabel, stale); !d.Allowed { + return false + } + if err := client.RemoveLabelsFresh(ctx, in.Repo, in.Number, stale); err != nil { // writeback:gated + if logger != nil { + logger.Warn("done verdict labels: removing the older verdict failed", "repo", in.Repo, "issue", in.Number, "label", stale, "error", err) + } + return false + } + if logger != nil { + logger.Info("done verdict labels: the later verdict stands", "repo", in.Repo, "issue", in.Number, "removed", stale) + } + return true +} + +// latestLabeledAt returns when each of a and b was last added to the issue +// (zero when never seen in its events). +func (c *Client) latestLabeledAt(ctx context.Context, owner, name string, number int, a, b string) (atA, atB time.Time, err error) { + opts := &gh.ListOptions{PerPage: 100} + for { + events, resp, err := c.client.Issues.ListIssueEvents(ctx, owner, name, number, opts) + if err != nil { + return atA, atB, err + } + for _, ev := range events { + if ev.GetEvent() != "labeled" { + continue + } + at := ev.GetCreatedAt().Time + switch label := ev.GetLabel().GetName(); { + case strings.EqualFold(label, a) && at.After(atA): + atA = at + case strings.EqualFold(label, b) && at.After(atB): + atB = at + } + } + if resp == nil || resp.NextPage == 0 { + return atA, atB, nil + } + opts.Page = resp.NextPage + } +} diff --git a/src/pkg/github/done_verdict_labels_test.go b/src/pkg/github/done_verdict_labels_test.go new file mode 100644 index 0000000000..bd8f8f296a --- /dev/null +++ b/src/pkg/github/done_verdict_labels_test.go @@ -0,0 +1,131 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// doneVerdictServer serves one issue o/r#N with labels and a label-event +// history, and records label removals. +type doneVerdictServer struct { + mu sync.Mutex + labels []string + events []map[string]any + removed []string + added []string +} + +func (d *doneVerdictServer) start(t *testing.T) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + d.mu.Lock() + defer d.mu.Unlock() + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/events"): + _ = json.NewEncoder(w).Encode(d.events) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/labels/"): + _ = json.NewEncoder(w).Encode(map[string]any{"name": "x"}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/labels"): + _ = json.NewEncoder(w).Encode(wireLabelsOf(d.labels)) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/issues/"): + _ = json.NewEncoder(w).Encode(map[string]any{"number": 1023, "state": "open", "labels": wireLabelsOf(d.labels), "user": map[string]any{"login": "castrojo"}}) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/labels"): + var body []string + _ = json.NewDecoder(r.Body).Decode(&body) + d.added = append(d.added, body...) + d.labels = append(d.labels, body...) + _ = json.NewEncoder(w).Encode(wireLabelsOf(d.labels)) + case r.Method == http.MethodDelete && strings.Contains(r.URL.Path, "/labels/"): + name := r.URL.Path[strings.LastIndex(r.URL.Path, "/labels/")+len("/labels/"):] + d.removed = append(d.removed, name) + d.labels = removeIssueLabel(d.labels, name) + _ = json.NewEncoder(w).Encode([]any{}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/comments"): + _ = json.NewEncoder(w).Encode([]any{}) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + return srv +} + +func labeledEvent(label, at string) map[string]any { + return map[string]any{"event": "labeled", "created_at": at, "label": map[string]any{"name": label}} +} + +// projectbluefin/testsuite#1023: hive/already-done (11-04 01:47Z), then +// hive/verified-open (03:13Z). The later verdict stands. +func TestReconcileDoneVerdictLabelsKeepsTheLaterVerdict(t *testing.T) { + both := []string{"kind/bug", AlreadyDoneLabel, VerifiedOpenLabel, "triage/accepted"} + for _, tc := range []struct { + name string + events []map[string]any + labels []string + removed string + }{ + {name: "verified-open later", labels: both, events: []map[string]any{labeledEvent(AlreadyDoneLabel, "2026-11-04T01:47:00Z"), labeledEvent(VerifiedOpenLabel, "2026-11-04T03:13:00Z")}, removed: AlreadyDoneLabel}, + {name: "already-done later", labels: both, events: []map[string]any{labeledEvent(VerifiedOpenLabel, "2026-11-04T01:47:00Z"), labeledEvent(AlreadyDoneLabel, "2026-11-04T03:13:00Z")}, removed: VerifiedOpenLabel}, + {name: "re-added verified-open counts its latest add", labels: both, events: []map[string]any{labeledEvent(VerifiedOpenLabel, "2026-11-01T00:00:00Z"), labeledEvent(AlreadyDoneLabel, "2026-11-02T00:00:00Z"), labeledEvent(VerifiedOpenLabel, "2026-11-03T00:00:00Z")}, removed: AlreadyDoneLabel}, + {name: "re-added already-done counts its latest add", labels: both, events: []map[string]any{labeledEvent(AlreadyDoneLabel, "2026-11-01T00:00:00Z"), labeledEvent(VerifiedOpenLabel, "2026-11-02T00:00:00Z"), labeledEvent(AlreadyDoneLabel, "2026-11-03T00:00:00Z")}, removed: VerifiedOpenLabel}, + {name: "an add missing from the events changes nothing", labels: both, events: []map[string]any{labeledEvent(VerifiedOpenLabel, "2026-11-04T03:13:00Z")}}, + {name: "only one verdict worn: no read, no write", labels: []string{AlreadyDoneLabel}, events: []map[string]any{labeledEvent(AlreadyDoneLabel, "2026-11-04T01:47:00Z")}}, + } { + t.Run(tc.name, func(t *testing.T) { + d := &doneVerdictServer{labels: append([]string(nil), tc.labels...), events: tc.events} + c := testClient(t, d.start(t).URL) + changed := reconcileDoneVerdictLabels(context.Background(), c, IssueStageInput{Repo: "o/r", Number: 1023, Labels: tc.labels}, nil) + d.mu.Lock() + defer d.mu.Unlock() + if got := strings.Join(d.removed, ","); got != tc.removed || changed != (tc.removed != "") { + t.Fatalf("removed = %q (changed=%v), want %q", got, changed, tc.removed) + } + }) + } +} + +// The agent's verified-open verdict takes the done labels off as it lands. +func TestLabelRelayVerifiedOpenRemovesDoneLabels(t *testing.T) { + d := &doneVerdictServer{labels: []string{"kind/bug", AlreadyDoneLabel, LikelyDoneLabel}} + c := issueTestClient(t, d.start(t).URL) + dir := withIssueDir(t) + reqPath, err := WriteIssueRequest(dir, IssueRequest{Kind: "label", Repo: "o/r", Number: 1023, Agent: "scanner", Labels: []string{VerifiedOpenLabel}}) + if err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + if resp := readIssueResultFile(t, reqPath); !resp.OK { + t.Fatalf("result = %+v, want ok", resp) + } + d.mu.Lock() + defer d.mu.Unlock() + if strings.Join(d.added, ",") != VerifiedOpenLabel { + t.Fatalf("added = %v", d.added) + } + if got := strings.Join(d.removed, ","); got != AlreadyDoneLabel+","+LikelyDoneLabel { + t.Fatalf("removed = %q, want already-done and likely-done", got) + } +} + +// The stage sweep runs the reconcile on every issue it walks. +func TestSyncIssueStageLabelsSettlesDoneVerdictPair(t *testing.T) { + fake := newIssueStageServer(t) + c := issueStageTestClient(t, fake.srv, true) + fake.events = map[int][]map[string]any{1023: {labeledEvent(AlreadyDoneLabel, "2026-11-04T01:47:00Z"), labeledEvent(VerifiedOpenLabel, "2026-11-04T03:13:00Z")}} + result := &ActionableResult{} + appendRepoWorkIssueDetail(result, Issue{Repo: "acme/widgets", Number: 1023, Labels: []string{AlreadyDoneLabel, VerifiedOpenLabel, "triage/accepted"}}, workBucketFiltered, "already done") + syncSeeded(t, fake, c, result, stagelabels.NewStore("", stageNow), stageNow()) + fake.mu.Lock() + defer fake.mu.Unlock() + if !issueHasLabel(fake.removed[1023], AlreadyDoneLabel) || issueHasLabel(fake.removed[1023], VerifiedOpenLabel) { + t.Fatalf("removed = %v, want hive/already-done only", fake.removed[1023]) + } +} diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index bd9b34fdcc..576bff55ef 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -866,6 +866,17 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f if err == nil && len(plainLabels) > 0 { err = c.AddLabels(ctx, req.Repo, req.Number, plainLabels) } + if err == nil && issueHasLabel(plainLabels, VerifiedOpenLabel) { + // The agent's verdict that the work is not finished supersedes + // the done labels: projectbluefin/testsuite#1023 wore + // hive/already-done next to a later hive/verified-open and the + // inbox offered to close it. Best-effort: the verdict landed, and + // the stage sweep settles a pair left behind + // (reconcileDoneVerdictLabels). + if rerr := c.RemoveLabelsFresh(ctx, req.Repo, req.Number, AlreadyDoneLabel, LikelyDoneLabel); rerr != nil && c.logger != nil { // writeback:solicited the agent's verified-open verdict + c.logger.Warn("issue-request watcher: removing done labels after verified-open failed; the stage sweep retries", "repo", req.Repo, "number", req.Number, "error", rerr) + } + } if err == nil && unpark { err = c.StageFacts().Unparked(ctx, c.IssueStageRef(req.Repo, req.Number), req.Agent) } diff --git a/src/pkg/github/issue_stage_labels.go b/src/pkg/github/issue_stage_labels.go index f24861c650..bf81558ef8 100644 --- a/src/pkg/github/issue_stage_labels.go +++ b/src/pkg/github/issue_stage_labels.go @@ -442,6 +442,9 @@ func SyncIssueStageLabelsWith(ctx context.Context, client *Client, result *Actio legacyCarriers[in.Repo]++ } res.ClaimedByHealed += healBackendClaimedByLabels(ctx, client, in, logger) + if reconcileDoneVerdictLabels(ctx, client, in, logger) { + res.Writes++ + } // A decomposed parent is waiting on its children, not on itself // (hivecommons/hive#12857): all closed → close it; any open → it // keeps its stage but is never marked stalled. diff --git a/src/pkg/github/issue_stage_labels_test.go b/src/pkg/github/issue_stage_labels_test.go index e5e70fd009..62a7616aff 100644 --- a/src/pkg/github/issue_stage_labels_test.go +++ b/src/pkg/github/issue_stage_labels_test.go @@ -26,8 +26,10 @@ import ( // issueStageServer is a fake GitHub that records label writes and comments // and answers the label-family provisioning list with everything present. type issueStageServer struct { - srv *httptest.Server - mu sync.Mutex + srv *httptest.Server + mu sync.Mutex + // events answers GET /issues/N/events (nil = none). + events map[int][]map[string]any added map[int][]string removed map[int][]string comments map[int]int @@ -72,6 +74,18 @@ func newIssueStageServer(t *testing.T) *issueStageServer { s.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") path, _ := url.PathUnescape(r.URL.Path) + if r.Method == http.MethodGet && strings.HasSuffix(path, "/events") { + parts := strings.Split(strings.TrimSuffix(path, "/events"), "/") + n, _ := strconv.Atoi(parts[len(parts)-1]) + s.mu.Lock() + evs := s.events[n] + s.mu.Unlock() + if evs == nil { + evs = []map[string]any{} + } + _ = json.NewEncoder(w).Encode(evs) + return + } if r.Method == http.MethodPost && path == "/graphql" { var q struct { Query string `json:"query"`