diff --git a/changelog.d/added-19071-tracker-autoclose.md b/changelog.d/added-19071-tracker-autoclose.md new file mode 100644 index 0000000000..0edbf13dcb --- /dev/null +++ b/changelog.d/added-19071-tracker-autoclose.md @@ -0,0 +1 @@ +- New opt-in `repos..parent_autoclose: trackers_too` (also a fourth choice, "Also close trackers whose work is done", in Settings, Repos tab) closes tracker and umbrella issues that have no sub-issue children once everything they reference is finished. "Done" is computed from the state of the issues and PRs the tracker lists in its task list or `close_when` checklist, plus the PRs that reference it with `Refs`, never from close keywords, since v5 merges do not auto-close. Only issues that announce themselves as trackers are read, `hive/tracking` and `hold` issues never close, a person's reopen is final, and the close waits for the same finished evidence on two reads 15 minutes apart and leaves an evidence comment listing every reference and its state. Default behaviour is unchanged ([#19071](https://github.com/hivecommons/hive/issues/19071)). diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 1d119fb3b1..f91aba9894 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -8320,7 +8320,7 @@ "System" ], "summary": "Parent auto-close policy per repo", - "description": "Returns repos..parent_autoclose for every monitored repo (effective value and whether the repo carries its own), the default, and the three options the Settings editor offers. Owner only.", + "description": "Returns repos..parent_autoclose for every monitored repo (effective value and whether the repo carries its own), the default, and the four options the Settings editor offers. Owner only.", "responses": { "200": { "description": "Current policy per monitored repo", @@ -8356,7 +8356,7 @@ "System" ], "summary": "Set a repo's parent auto-close policy", - "description": "Sets repos..parent_autoclose for one monitored repo: true (close finished parents, the default and clears the override), epics_too (also close epic and hold parents) or false (never auto-close parents). Applies without a restart. Parents a person reopened and tracking issues are never auto-closed under any value. Audited as config_repo_parent_autoclose. Owner only.", + "description": "Sets repos..parent_autoclose for one monitored repo: true (close finished parents, the default and clears the override), epics_too (also close epic and hold parents), trackers_too (as epics_too, and also close tracker or umbrella issues with no sub-issues once every PR and issue they reference is finished) or false (never auto-close parents). Applies without a restart. Parents a person reopened and tracking issues are never auto-closed under any value. Audited as config_repo_parent_autoclose. Owner only.", "requestBody": { "required": true, "content": { @@ -8376,6 +8376,7 @@ "enum": [ "true", "epics_too", + "trackers_too", "false" ] } diff --git a/src/docs/api-reference/configuration.md b/src/docs/api-reference/configuration.md index f4f836d3bf..1052e983e1 100644 --- a/src/docs/api-reference/configuration.md +++ b/src/docs/api-reference/configuration.md @@ -104,8 +104,8 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `GET` | `/api/config/reference-repos` | Owner only | `project.reference_repos` ([#18136](https://github.com/hivecommons/hive/issues/18136)): `reference_repos` (`{repo, write, write_token_file}` plus `derived_from` on a derived entry and, on every write entry, `credential_state`, `credential_reason` and `credential_login`, the GitHub account the writes appear under ([#18197](https://github.com/hivecommons/hive/issues/18197)), plus `credential_identity` (`bot`, `owner` or `token_file`) and `credential_note`, the one-line why ([#18516](https://github.com/hivecommons/hive/issues/18516)); always an array), `write_kinds`, `write_identity` (`project.remote_write_identity` with the default applied), `write_identities` and `bot_login` (the App bot login, when known) | `pkg/dashboard/api.go:557` | | `PUT` | `/api/config/reference-repos` | Owner only | Replace `project.reference_repos`; `{"reference_repos": []}` clears it. Refused with a 400 and nothing changed when the config loader would reject the list (bare name, repo also in `project.repos`, duplicate, unknown write kind, token file without write) or when a write entry has no credential boot would accept — the same message boot exits 22 with — or when an entry carries a `write_token_file` the config file does not already hold for that repo (the path is operator-only; a dashboard write opt-in writes as the hive owner's connected GitHub account). Entries with `derived_from` ([#18139](https://github.com/hivecommons/hive/issues/18139)) are the request issue's grant: echo them back as served; changing one's write set, leaving one out, sending one the hive did not derive or adding a hand-written entry for the same repo is a 409 naming the issue to change instead ([#18161](https://github.com/hivecommons/hive/issues/18161)). They are never stored: the saved list holds the hand-written entries and the derived set is merged back in memory after the save. 500 when the file could not be written (proxy restored, nothing changed). On success the GitHub proxy's reference policies are replaced without a restart; audited as `config_reference_repos` | `pkg/dashboard/api.go:558` | | `PUT` | `/api/config/reference-repos/write-identity` | Owner only | Set `project.remote_write_identity` ([#18516](https://github.com/hivecommons/hive/issues/18516)): body `{"write_identity": "bot"}`, one of `bot`, `bot_only`, `owner`. 400 on an unknown value, or when a hand-written write entry would have no credential under the new value (the message boot exits 22 with); nothing is stored then. 500 when the file could not be written. On success the reference repos are re-resolved before the response, which is the same body as `GET /api/config/reference-repos`; audited as `config_remote_write_identity` with `previous` and `write_identity` | `pkg/dashboard/api.go:563` | -| `GET` | `/api/config/repos/parent-autoclose` | Owner only | `repos..parent_autoclose` for every monitored repo: the effective value (`true`, `epics_too` or `false`), whether the repo carries its own, the default and the three options the Settings editor offers ([#19070](https://github.com/hivecommons/hive/issues/19070)) | `pkg/dashboard/api.go:560` | -| `PUT` | `/api/config/repos/parent-autoclose` | Owner only | Set one monitored repo's parent auto-close policy: `{"repo": "owner/name", "value": "true" \| "epics_too" \| "false"}`. `true` is the default and clears the override. 400 for a missing repo or an invalid value, 404 for a repo that is not monitored, 500 when the file could not be written (previous value restored). Applies without a restart; audited as `config_repo_parent_autoclose`. Parents a person reopened and tracking issues are never auto-closed under any value | `pkg/dashboard/api.go:561` | +| `GET` | `/api/config/repos/parent-autoclose` | Owner only | `repos..parent_autoclose` for every monitored repo: the effective value (`true`, `epics_too`, `trackers_too` or `false`), whether the repo carries its own, the default and the four options the Settings editor offers ([#19070](https://github.com/hivecommons/hive/issues/19070)) | `pkg/dashboard/api.go:560` | +| `PUT` | `/api/config/repos/parent-autoclose` | Owner only | Set one monitored repo's parent auto-close policy: `{"repo": "owner/name", "value": "true" \| "epics_too" \| "trackers_too" \| "false"}`. `true` is the default and clears the override. 400 for a missing repo or an invalid value, 404 for a repo that is not monitored, 500 when the file could not be written (previous value restored). Applies without a restart; audited as `config_repo_parent_autoclose`. Parents a person reopened and tracking issues are never auto-closed under any value | `pkg/dashboard/api.go:561` | | `GET` | `/api/config/prompt-sources` | Owner only | Repo-sourced prompt and definition gate ([#18490](https://github.com/hivecommons/hive/issues/18490)): `repos` (one row per managed repo, then any owner approval whose repo is no longer managed, each `{repo, managed, owner_approved, seed_allowed, allowed, approved_by, approved_at}`), `seed_enabled`, `seed_allowlist`, `trust_notice`, `seed_only_note` | `pkg/dashboard/api.go:572` | | `PUT` | `/api/config/prompt-sources/owner-repos` | Owner only | `{"repo": "org/name", "allowed": true, "acknowledge_trust": true}` approves one of the hive's managed repos as a `prompt_source` / `definition_source` repo; `"allowed": false` revokes. 400 with nothing changed for a repo outside `project.repos` (still needs the seed edit), a malformed slug, or an approval without `acknowledge_trust: true`. Persisted to `variables.security.owner_github_prompt_repos` in the running config; audited as `config_prompt_source_repo`. Responds with the GET body | `pkg/dashboard/api.go:573` | | `GET` | `/api/config/review` | Dashboard auth/session | Review Config Get | `pkg/dashboard/api.go:218` | diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index e8f8df3a3a..d3280d51ec 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -714,6 +714,7 @@ Per repo, `repos..parent_autoclose` chooses the policy: |---|---| | `true` (default) | close a decomposed parent when all children are closed, except `epic`/`hold`-labelled issues | | `epics_too` | also close `epic`/`hold`-labelled parents | +| `trackers_too` | `epics_too`, and also close tracker/umbrella issues that have no sub-issue children once everything they reference is finished (below) | | `false` | never auto-close a parent in this repo | ```yaml @@ -724,7 +725,7 @@ repos: You can also set it without editing YAML: Settings, Repos tab, "Parent auto-close" (owner only). Each monitored repo has a choice of "Close finished parents -(default)", "Also close epics and holds" and "Never auto-close parents"; saving +(default)", "Also close epics and holds", "Also close trackers whose work is done" and "Never auto-close parents"; saving applies it without a restart and is recorded in the audit log. Issues marked as tracking issues (`hive/tracking`, the `` marker, or "Keep this issue open") are never auto-closed under any value. @@ -732,6 +733,41 @@ this issue open") are never auto-closed under any value. The dashboard does not yet show a "kept open: reopened by " pill; the kept-open decision is logged (`decomposed parent kept open`). +**Trackers and umbrellas with no sub-issues (`trackers_too`).** Many umbrella and +audit trackers are not decomposed parents: they have no sub-issue children and +are referenced by `Refs`-only PRs. Under v5 a merge never closes an issue, so +with `repos..parent_autoclose: trackers_too` the sweep computes "done" +from the state of what the tracker references, never from close keywords. It is +off unless you choose it, and nothing changes for any other value. + +- *Which issues are read.* Only an issue that announces itself as a tracker: one + the shared umbrella classifier recognises (`umbrella`, `tracker`, `epic` or + `meta-tracker` label, the `` marker, a `[Tracker]`-style + title prefix, or a task list of two or more issue references; see + `hub.contribute_umbrella`), an `audit` or `roadmap` label, or a `close_when` + checklist. A design or feature issue that merely has `Refs` PRs is never + closed this way, because a `Refs` PR is partial work by definition. +- *The reference set.* With a `close_when` heading (or `close_when:` line) in the + body, the checklist under it is the whole set: the tracker is done when every + item is ticked or, for an item naming an issue or PR, closed. Otherwise the set + is every task-list item that names an issue or PR plus every PR that + references the tracker with `Refs`, a closing keyword or `Part of`. The tracker + is done when that set is non-empty, every PR in it is merged or closed and + every issue in it is closed. +- *Safety.* `hive/tracking` issues (label, `` marker, "Keep + this issue open") and `hold`-labelled issues are never closed, even though + `trackers_too` implies `epics_too`. A tracker a person reopened after a hive + auto-close is never closed again. An unreadable reference or timeline, or more + than 50 references, means no close. Reads are cached per tracker, and the close + needs the same finished evidence on two fresh reads 15 minutes apart, so a + tracker is not closed between two PRs landing. +- *Audit.* Before closing, the hive posts one comment (carrying + ``) listing every referenced issue and PR with + its state, then closes the tracker as completed. Reopening it is final. + +Closing done-but-open issues that sit in `stage/040-covered-by-pr` when the +covering PR merges is not part of this rule. + ### `stage/stalled` — "nobody is working this" On a PR, `stage/stalled` means **no activity by anyone but the hive for the diff --git a/src/docs/notifications.md b/src/docs/notifications.md index 83c849f980..6b99c3bebc 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -490,6 +490,7 @@ without a class. Review submissions and review requests are always counted as | `github.(*Client).PostIssueNote` | `status` | passthrough wrapper (issue status card, `attention_pilot`); attributed to its caller | | `github.(*Client).PostInformationalIssueNote` | `status` | passthrough wrapper (issue status card in quiet mode or under `attention_pilot`, for notes nobody acts on); attributed to its caller | | `github.(*Client).closeDecomposedParent` | `status` | | +| `github.(*Client).closeDoneTracker` | `status` | | | `github.(*Client).closeIssueForMergedPRClaimWithIssue` | `status` | | | `github.(*Client).closeSupersededPRDecision` | `status` | | | `github.(*Client).fileReviewBacklog` | `status` | | diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index d3bf028607..af28a9d2d9 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -301,7 +301,7 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `governor.trajectory.enabled` | Defaults to enabled. | The lane no-ops until a reviewer endpoint and model resolve from `governor.trajectory` or `governor.litellm`. | | `governor.kick_limits.max_issues` / `max_prs` | `100` / `50` | Caps on every issue list and every PR list (actionable, stale drafts, merge-eligible, CI-failing) rendered into a kick prompt; a cut list ends with an explicit "… and N more" line. An absent key or a negative value means the default; an explicit `0` opts out of the cap entirely (`KickListUnlimited` — every item is listed), and anything above `500` is pinned to `500`. Editable without touching this file under **Settings → Repos → Kick prompt list caps**. Truncation drops the *newest, lowest-priority* items: issues are listed oldest first, and PRs in review-priority order (fixes, then refactors/docs, then tests, oldest first within each class), so lowering a cap never hides the most urgent work. Raise `max_prs` only if your agents actually work more than 50 PRs per turn; the tail of a long list costs tokens and delivery time on every kick without changing behaviour (#7368). | | `project.agents_md_max_bytes` | `32768` (or env `HIVE_AGENTS_MD_MAX_BYTES`); clamped to `2048`..`262144`. `repos..agents_md_max_bytes` overrides it per repo. | Caps the repo `AGENTS.md` block prepended to a kick. Whole sections (split at `#`/`##` headings; each resolved skill is its own unit) are kept or dropped, never clipped mid-rule; when any are dropped the kick carries a one-line `⚠️ AGENTS.md exceeded N bytes; … — read AGENTS.md …` warning naming them and the scheduler logs it. See [agents-md.md](../agents-md.md#size-cap-whole-sections-visible-warning). | -| `repos..parent_autoclose` | `true` (default), `epics_too` or `false` | Decomposed-parent auto-close policy for the repo: `true` closes a parent when every child is closed except `epic`/`hold`-labelled issues; `epics_too` also closes those; `false` never closes a parent. A parent a person reopened after a hive auto-close is never re-closed under any value. Editable in Settings, Repos tab, Parent auto-close (owner only; applies without a restart). See [stage-labels.md](../labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled). | +| `repos..parent_autoclose` | `true` (default), `epics_too`, `trackers_too` or `false` | Decomposed-parent auto-close policy for the repo: `true` closes a parent when every child is closed except `epic`/`hold`-labelled issues; `epics_too` also closes those; `trackers_too` does that and also closes tracker/umbrella issues with no sub-issues once everything they reference is finished; `false` never closes a parent. A parent a person reopened after a hive auto-close is never re-closed under any value. Editable in Settings, Repos tab, Parent auto-close (owner only; applies without a restart). See [stage-labels.md](../labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled). | | `project.style` | unset (built-in text applies) | Hive-wide **house style**: a short how-to-change rule list prepended to every kick on every backend, directly under the header. A managed repo's root `HIVE_STYLE.md` (read from `project.checkouts_dir`) overrides it; precedence repo > hive > built-in. Inline text wins over `project.style_file`. See [house-style.md](../house-style.md). | | `project.style_file` | unset | Path on the hive host holding the hive-wide house style; used when `project.style` is empty. | | `project.style_max_bytes` | `4096`; values under `512` are raised to `512`. | Caps each house-style candidate. An oversize candidate is skipped whole (not clipped) and the next source applies; the skip is audited as `house_style_skip`. The repo file is also run through ioscan at the issue-body policy and refused whole on a block. | diff --git a/src/docs/settings.md b/src/docs/settings.md index 796f0c3264..22f529e0fb 100644 --- a/src/docs/settings.md +++ b/src/docs/settings.md @@ -44,7 +44,7 @@ Which labels hold, require, or route work, the label the merger queue applies to The repos the hive manages and the caps on how many issues and PRs are listed in one kick (`governor.kick_limits`). See the `governor.kick_limits` row of the [notable fields](operator-reference.md#notable-fields). -The same tab has a **Parent auto-close** card (owner only, `repos..parent_autoclose`): per monitored repo, choose "Close finished parents (default)", "Also close epics and holds" or "Never auto-close parents". It shows the effective value for each repo, applies on save without a restart, and is audited. A parent a person reopened is never re-closed, and issues marked as tracking issues are never auto-closed. See [stage-labels](labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled). +The same tab has a **Parent auto-close** card (owner only, `repos..parent_autoclose`): per monitored repo, choose "Close finished parents (default)", "Also close epics and holds", "Also close trackers whose work is done" or "Never auto-close parents". It shows the effective value for each repo, applies on save without a restart, and is audited. A parent a person reopened is never re-closed, and issues marked as tracking issues are never auto-closed. See [stage-labels](labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled). ## Budget diff --git a/src/pkg/config/repo_settings.go b/src/pkg/config/repo_settings.go index 0a93b98b1f..3b5141a24f 100644 --- a/src/pkg/config/repo_settings.go +++ b/src/pkg/config/repo_settings.go @@ -26,8 +26,11 @@ type RepoSettings struct { // repo (hivecommons/hive#18869): "true" (the default when empty) closes a // decomposed parent once every child is closed, except an issue labelled // epic or hold; "false" never auto-closes a parent; "epics_too" also - // closes epic/hold-labelled parents. A parent a person reopened after a - // hive auto-close is never re-closed under any value. YAML booleans + // closes epic/hold-labelled parents; "trackers_too" does that and also + // closes tracker or umbrella issues that have no sub-issue children once + // every PR and issue they reference is finished (hivecommons/hive#19071). + // A parent a person reopened after a hive auto-close is never re-closed + // under any value. YAML booleans // true/false are accepted and read as the strings. ParentAutoClose string `yaml:"parent_autoclose,omitempty" json:"parent_autoclose,omitempty"` // ReviewPreflight overrides review.preflight for this repo @@ -40,18 +43,21 @@ const ( ParentAutoCloseOn = "true" ParentAutoCloseOff = "false" ParentAutoCloseEpicsToo = "epics_too" + // ParentAutoCloseTrackersToo implies ParentAutoCloseEpicsToo and also + // closes tracker/umbrella issues whose referenced work is all finished. + ParentAutoCloseTrackersToo = "trackers_too" ) // ParentAutoCloseFor is the normalised parent auto-close policy for repo: -// one of ParentAutoCloseOn (default), ParentAutoCloseOff or -// ParentAutoCloseEpicsToo. An unrecognised value falls back to the default. +// one of ParentAutoCloseOn (default), ParentAutoCloseOff, +// ParentAutoCloseEpicsToo or ParentAutoCloseTrackersToo. An unrecognised value falls back to the default. func (c *Config) ParentAutoCloseFor(repo string) string { rs, ok := c.RepoSettingsFor(repo) if !ok { return ParentAutoCloseOn } switch v := strings.ToLower(strings.TrimSpace(rs.ParentAutoClose)); v { - case ParentAutoCloseOff, ParentAutoCloseEpicsToo: + case ParentAutoCloseOff, ParentAutoCloseEpicsToo, ParentAutoCloseTrackersToo: return v } return ParentAutoCloseOn @@ -93,7 +99,7 @@ func (c *Config) repoSettingsKeyLocked(repo string) (string, bool) { // included: the dashboard always sends an explicit choice). func NormalizeParentAutoClose(v string) (string, bool) { switch n := strings.ToLower(strings.TrimSpace(v)); n { - case ParentAutoCloseOn, ParentAutoCloseOff, ParentAutoCloseEpicsToo: + case ParentAutoCloseOn, ParentAutoCloseOff, ParentAutoCloseEpicsToo, ParentAutoCloseTrackersToo: return n, true } return "", false @@ -126,7 +132,7 @@ func (c *Config) SetParentAutoClose(repo, value string) error { } norm, ok := NormalizeParentAutoClose(value) if !ok { - return fmt.Errorf("parent_autoclose must be %q, %q or %q", ParentAutoCloseOn, ParentAutoCloseEpicsToo, ParentAutoCloseOff) + return fmt.Errorf("parent_autoclose must be %q, %q, %q or %q", ParentAutoCloseOn, ParentAutoCloseEpicsToo, ParentAutoCloseTrackersToo, ParentAutoCloseOff) } name, ok := c.monitoredRepoName(repo) if !ok { diff --git a/src/pkg/config/repo_settings_test.go b/src/pkg/config/repo_settings_test.go index 6a70274831..54f9b7f18f 100644 --- a/src/pkg/config/repo_settings_test.go +++ b/src/pkg/config/repo_settings_test.go @@ -54,11 +54,11 @@ func TestReposAgentsMdMaxBytesYAML(t *testing.T) { func TestParentAutoCloseFor(t *testing.T) { var cfg Config - if err := yaml.Unmarshal([]byte("project: {org: acme}\nrepos:\n off: {parent_autoclose: false}\n epics: {parent_autoclose: epics_too}\n on: {parent_autoclose: true}\n junk: {parent_autoclose: maybe}\n other: {}\n"), &cfg); err != nil { + if err := yaml.Unmarshal([]byte("project: {org: acme}\nrepos:\n off: {parent_autoclose: false}\n epics: {parent_autoclose: epics_too}\n trackers: {parent_autoclose: Trackers_Too}\n on: {parent_autoclose: true}\n junk: {parent_autoclose: maybe}\n other: {}\n"), &cfg); err != nil { t.Fatal(err) } cases := map[string]string{ - "off": ParentAutoCloseOff, "acme/epics": ParentAutoCloseEpicsToo, "on": ParentAutoCloseOn, + "off": ParentAutoCloseOff, "acme/epics": ParentAutoCloseEpicsToo, "trackers": ParentAutoCloseTrackersToo, "on": ParentAutoCloseOn, "junk": ParentAutoCloseOn, "other": ParentAutoCloseOn, "unlisted": ParentAutoCloseOn, } for repo, want := range cases { diff --git a/src/pkg/dashboard/api_config_parent_autoclose.go b/src/pkg/dashboard/api_config_parent_autoclose.go index d17197d8c1..05ece9c8f0 100644 --- a/src/pkg/dashboard/api_config_parent_autoclose.go +++ b/src/pkg/dashboard/api_config_parent_autoclose.go @@ -33,7 +33,7 @@ type parentAutoCloseOption struct { // parentAutoCloseRepo is one monitored repo and its policy. type parentAutoCloseRepo struct { Repo string `json:"repo"` - // Value is the effective policy: "true", "false" or "epics_too". + // Value is the effective policy: "true", "false", "epics_too" or "trackers_too". Value string `json:"value"` // Explicit is true when the repo carries its own value, false when it // inherits the default. @@ -56,6 +56,8 @@ var parentAutoCloseOptions = []parentAutoCloseOption{ "Close a split-up parent issue once all of its children are closed, except parents labelled epic or hold."}, {config.ParentAutoCloseEpicsToo, "Also close epics and holds", "As above, and also close parents labelled epic or hold when all of their children are closed."}, + {config.ParentAutoCloseTrackersToo, "Also close trackers whose work is done", + "As above, and also close tracker or umbrella issues with no sub-issues once every PR and issue they reference is finished, after a short settle delay and with an evidence comment. Issues marked as tracking issues are never closed."}, {config.ParentAutoCloseOff, "Never auto-close parents", "The hive never closes a parent issue itself; a person closes it."}, } @@ -93,7 +95,7 @@ func (s *Server) handleParentAutoCloseGet(w http.ResponseWriter, r *http.Request } // handleParentAutoClosePut sets one monitored repo's policy. Body: -// {"repo": "owner/name", "value": "true" | "epics_too" | "false"}. Choosing +// {"repo": "owner/name", "value": "true" | "epics_too" | "trackers_too" | "false"}. Choosing // "true" (the default) clears the repo's override. func (s *Server) handleParentAutoClosePut(w http.ResponseWriter, r *http.Request) { if !requireOwnerRole(w, r) { @@ -120,7 +122,7 @@ func (s *Server) handleParentAutoClosePut(w http.ResponseWriter, r *http.Request // --- validate before mutating anything --- if _, ok := config.NormalizeParentAutoClose(body.Value); !ok { - jsonError(w, `value must be "true", "epics_too" or "false"`, http.StatusBadRequest) + jsonError(w, `value must be "true", "epics_too", "trackers_too" or "false"`, http.StatusBadRequest) return } previous := cfg.ParentAutoCloseFor(body.Repo) diff --git a/src/pkg/dashboard/api_config_parent_autoclose_test.go b/src/pkg/dashboard/api_config_parent_autoclose_test.go index d6dc932af2..d1e373c1ed 100644 --- a/src/pkg/dashboard/api_config_parent_autoclose_test.go +++ b/src/pkg/dashboard/api_config_parent_autoclose_test.go @@ -43,7 +43,7 @@ func TestParentAutoClose_GetListsMonitoredReposAndOptions(t *testing.T) { t.Fatalf("GET: %d %s", rec.Code, rec.Body.String()) } got := decodeParentAutoClose(t, rec.Body.Bytes()) - if got.Default != config.ParentAutoCloseOn || len(got.Options) != 3 { + if got.Default != config.ParentAutoCloseOn || len(got.Options) != 4 { t.Fatalf("default=%q options=%v", got.Default, got.Options) } want := []parentAutoCloseRepo{ diff --git a/src/pkg/github/decomposed_parents.go b/src/pkg/github/decomposed_parents.go index 5a63abb560..cb13592064 100644 --- a/src/pkg/github/decomposed_parents.go +++ b/src/pkg/github/decomposed_parents.go @@ -423,13 +423,14 @@ func (c *Client) parentAutoClosePolicy(repo string) string { // parentAutoCloseExempt returns why a parent must not be auto-closed from // its policy and labels alone, or "" when it may be (hivecommons/hive#18869). -// "false" exempts every parent; unless the policy is "epics_too", an issue +// "false" exempts every parent; unless the policy is "epics_too" or +// "trackers_too" (which implies it), an issue // labelled epic (any case) or hold is left for a person to close. func parentAutoCloseExempt(policy string, labels []string) string { if policy == config.ParentAutoCloseOff { return "repo parent_autoclose is false" } - if policy == config.ParentAutoCloseEpicsToo { + if policy == config.ParentAutoCloseEpicsToo || policy == config.ParentAutoCloseTrackersToo { return "" } for _, l := range labels { diff --git a/src/pkg/github/decomposed_parents_test.go b/src/pkg/github/decomposed_parents_test.go index 3089a4b08f..2b83047c17 100644 --- a/src/pkg/github/decomposed_parents_test.go +++ b/src/pkg/github/decomposed_parents_test.go @@ -419,6 +419,7 @@ func TestParentAutoCloseExempt(t *testing.T) { {"true", []string{"Epic"}, true}, {"true", []string{"hold"}, true}, {"epics_too", []string{"epic", "hold"}, false}, + {"trackers_too", []string{"epic", "hold"}, false}, {"false", nil, true}, } for _, tc := range cases { diff --git a/src/pkg/github/issue_stage_labels.go b/src/pkg/github/issue_stage_labels.go index a9c1178190..591f673818 100644 --- a/src/pkg/github/issue_stage_labels.go +++ b/src/pkg/github/issue_stage_labels.go @@ -144,6 +144,10 @@ type IssueStageResult struct { // (hivecommons/hive#12857). ParentsClosed int ParentsAwaitingChildren int + // TrackersClosed counts tracker/umbrella issues closed because every + // issue and PR they reference is finished (trackers_too, + // hivecommons/hive#19071). + TrackersClosed int } // issueStageRESTCaller tags the writes for /api/gh-rate-limits. @@ -191,6 +195,10 @@ type IssueStageInput struct { // DelegatesWork mirrors the watcher's unchecked issue-task predicate. // Unlike Children, it includes body-only delegation without ledger entries. DelegatesWork bool + // TrackerSignal is set when the issue announces itself as a tracker or + // umbrella (label, title, task list or close_when checklist), the only + // issues the trackers_too auto-close policy reads (hivecommons/hive#19071). + TrackerSignal bool // SpekUmbrella is set when the body carries the Spek publish marker with // no `task=`: the issue is a Spek-published RFC (spektacular#114), an // umbrella like `epic`, so a claim on it is never staged. @@ -435,7 +443,7 @@ func forgeIssueLabels(labels []string, claim *IssueClaimContext) []string { // issueStageInputFrom is the stage input for one actionable issue. func issueStageInputFrom(issue Issue) IssueStageInput { - return IssueStageInput{Repo: issue.Repo, Number: issue.Number, Labels: forgeIssueLabels(issue.Labels, issue.ClaimContext), Title: issue.Title, ClaimKind: issue.ClaimKind, ClaimedBy: issue.ClaimedBy, PRStarted: issue.ClaimPRStarted, Claim: issue.ClaimContext, Children: issue.Children, AuthorKind: issueAuthorKind(issue), DelegatesWork: hasUnfinishedDelegatedWork(issue.Body), SpekUmbrella: spekmarker.IsUmbrella(issue.Body), UpdatedAt: issue.UpdatedAt, Assignees: issue.Assignees} + return IssueStageInput{Repo: issue.Repo, Number: issue.Number, Labels: forgeIssueLabels(issue.Labels, issue.ClaimContext), Title: issue.Title, ClaimKind: issue.ClaimKind, ClaimedBy: issue.ClaimedBy, PRStarted: issue.ClaimPRStarted, Claim: issue.ClaimContext, Children: issue.Children, AuthorKind: issueAuthorKind(issue), DelegatesWork: hasUnfinishedDelegatedWork(issue.Body), TrackerSignal: trackerSignal(issue.Title, issue.Labels, issue.Body), SpekUmbrella: spekmarker.IsUmbrella(issue.Body), UpdatedAt: issue.UpdatedAt, Assignees: issue.Assignees} } // IssueStageInputs flattens an enumeration result into one row per open @@ -589,6 +597,11 @@ func SyncIssueStageLabelsWith(ctx context.Context, client *Client, result *Actio } awaitingChildren = true res.ParentsAwaitingChildren++ + } else if in.Children == nil && client.closeDoneTracker(ctx, in, store, key, now, logger) { + res.TrackersClosed++ + res.Writes++ + res.Comments++ + continue } if closed, commented := handleMergedPRIssueStage(ctx, client, in, logger); closed { res.MergedPRIssuesClosed++ diff --git a/src/pkg/github/issue_timeline.go b/src/pkg/github/issue_timeline.go index 320b446cb9..df8c211999 100644 --- a/src/pkg/github/issue_timeline.go +++ b/src/pkg/github/issue_timeline.go @@ -22,6 +22,9 @@ type IssueEvent struct { // SourcePR is the referencing pull request's number on a cross-referenced // event whose source is a pull request; 0 otherwise. SourcePR int + // SourceRepo is the referencing pull request's lowercased "owner/name" + // when GitHub reported it; empty otherwise. + SourceRepo string // CommitID is the referencing commit on a referenced event. CommitID string // Actor is the login that caused the event (the commenter on a @@ -72,6 +75,7 @@ func issueEventFrom(t *gh.Timeline, since time.Time) (IssueEvent, bool) { } if src := t.GetSource(); src != nil && src.Issue != nil && src.Issue.IsPullRequest() { ev.SourcePR = src.Issue.GetNumber() + ev.SourceRepo = strings.ToLower(src.Issue.GetRepository().GetFullName()) } actor := t.GetActor() if actor == nil { diff --git a/src/pkg/github/notification_write_sites.go b/src/pkg/github/notification_write_sites.go index 126d3bb89b..31789a8122 100644 --- a/src/pkg/github/notification_write_sites.go +++ b/src/pkg/github/notification_write_sites.go @@ -56,6 +56,7 @@ var notificationWriteSites = map[string]NotificationWriteSite{ "github.(*Client).SweepLegacyFleetReportIssues": {Class: NotificationClassStatus}, "github.(*Client).closeDecomposedParent": {Class: NotificationClassStatus}, "github.(*Client).closeIssueForMergedPRClaimWithIssue": {Class: NotificationClassStatus}, + "github.(*Client).closeDoneTracker": {Class: NotificationClassStatus}, "github.(*Client).closeSupersededPRDecision": {Class: NotificationClassStatus}, "github.(*Client).fileReviewBacklog": {Class: NotificationClassStatus}, "github.(*Client).foldSentinelNotice": {Class: NotificationClassStatus}, diff --git a/src/pkg/github/tracker_autoclose.go b/src/pkg/github/tracker_autoclose.go new file mode 100644 index 0000000000..3a42536ab6 --- /dev/null +++ b/src/pkg/github/tracker_autoclose.go @@ -0,0 +1,482 @@ +package github + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "fmt" + "log/slog" + "regexp" + "sort" + "strconv" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/gfi" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Tracker and umbrella auto-close (hivecommons/hive#19071). +// +// parent_autoclose closes decomposed parents: issues whose sub-issue children +// are all closed. Many umbrella and audit trackers are not decomposed parents. +// They have no sub-issue children and are referenced by `Refs`-only PRs, so +// nothing closed them when the work was finished. Under v5 a merge never +// closes an issue, so "done" is computed here from the state of what the +// tracker references, never from close keywords. +// +// The rule is opt-in per repo (repos..parent_autoclose: trackers_too, +// which implies epics_too) and off by default. An issue is a candidate only +// when it announces itself as a tracker: an umbrella by the shared classifier +// (label, marker, title prefix, task list of issue references), an audit or +// roadmap label, or a close_when checklist. +// A design or feature issue that merely has `Refs` PRs is not a candidate, so +// partial work never closes it (the failure that closed #18596). +// +// The set of references is, in order of authority: +// +// 1. a `close_when` checklist in the body, when present: the author lists +// exactly what must be finished, and the tracker is done when every item +// is checked or (for an item that names an issue or PR) closed; +// 2. otherwise every task-list item that names an issue or PR, plus every +// PR that references the tracker with Refs (or a closing keyword). +// +// The tracker is done when that set is non-empty and every PR in it is closed +// or merged and every issue in it is closed. Safety: hive/tracking issues and +// hold-labelled issues are never closed, a person's reopen is final, an +// unreadable reference or timeline means no close that cycle, and the close +// needs the same evidence on two reads trackerSettleInterval apart. + +const ( + // trackerSettleInterval is both the per-tracker read cache lifetime and + // the settle delay: a tracker closes only when a fresh read, at least + // this long after the first one, sees the same finished evidence, so it + // is not closed between two PRs landing. + trackerSettleInterval = 15 * time.Minute + // trackerMaxReferences bounds the reads one tracker can cost. A tracker + // that references more is never auto-closed. + trackerMaxReferences = 50 + // trackerEvidenceMarker identifies the evidence a close comment was + // written for, so a close that failed after its comment is not repeated. + trackerEvidenceMarker = "\n") + b.WriteString("Everything this tracker references is finished:\n\n") + for _, it := range items { + b.WriteString("- " + it.String() + "\n") + } + if explicit { + b.WriteString("\nThe list above is this issue's `close_when` checklist, so the hive is closing it as completed.") + } else { + b.WriteString("\nThe list above is every issue and PR this tracker references (task-list items and PRs that reference it), so the hive is closing it as completed.") + } + b.WriteString(" Reopen it if something is still missing; a reopen by a person is never overridden.") + return b.String() +} + +// trackerReadRecord is the per-tracker cache: the last read's evidence and +// when it was taken, plus the fingerprint of the previous finished read. +type trackerReadRecord struct { + at time.Time + hash string + doneHash string + doneAt time.Time +} + +var ( + trackerMu sync.Mutex + trackerReads = map[string]trackerReadRecord{} +) + +// trackerReadDue reports whether the tracker has to be read again. +func trackerReadDue(key string, now time.Time) bool { + trackerMu.Lock() + defer trackerMu.Unlock() + rec, ok := trackerReads[key] + return !ok || now.Sub(rec.at) >= trackerSettleInterval +} + +// trackerSettled records a fresh read and reports whether the same finished +// evidence was already seen by an earlier read at least trackerSettleInterval +// ago. Unfinished evidence clears the settle state. +func trackerSettled(key string, now time.Time, hash string, done bool) bool { + trackerMu.Lock() + defer trackerMu.Unlock() + rec := trackerReads[key] + settled := done && rec.doneHash == hash && !rec.doneAt.IsZero() && now.Sub(rec.doneAt) >= trackerSettleInterval + next := trackerReadRecord{at: now, hash: hash} + switch { + case !done: + case rec.doneHash == hash && !rec.doneAt.IsZero(): + next.doneHash, next.doneAt = rec.doneHash, rec.doneAt + default: + next.doneHash, next.doneAt = hash, now + } + trackerReads[key] = next + return settled +} + +// readTrackerItem reads one referenced issue or PR from its own repo. +func (c *Client) readTrackerItem(ctx context.Context, owner, name string, ref ChildRef) (trackerItem, *gh.Issue, error) { + itemOwner, itemName := owner, name + if ref.Repo != "" { + itemOwner, itemName = c.splitRepo(ref.Repo) + } + issue, _, err := c.client.Issues.Get(ctx, itemOwner, itemName, ref.Number) + if err != nil { + return trackerItem{}, nil, fmt.Errorf("reading %s/%s#%d: %w", itemOwner, itemName, ref.Number, err) + } + state := strings.ToLower(issue.GetState()) + return trackerItem{Ref: ref, PR: issue.IsPullRequest(), State: state, Done: state == "closed"}, issue, nil +} + +// trackerEvidence resolves what the tracker references and the state of each +// reference. explicit is true when the set came from a close_when checklist. +// Any unreadable reference is an error: the tracker is not closed that cycle. +func (c *Client) trackerEvidence(ctx context.Context, owner, name string, number int, body string) (items []trackerItem, explicit bool, err error) { + repoFull := strings.ToLower(owner + "/" + name) + seen := map[ChildRef]bool{{Number: number}: true} + add := func(t trackerTask) (bool, error) { + ref := t.Ref + if strings.EqualFold(ref.Repo, repoFull) { + ref.Repo = "" + } + if seen[ref] { + return true, nil + } + seen[ref] = true + it, _, rerr := c.readTrackerItem(ctx, owner, name, ref) + if rerr != nil { + return false, rerr + } + it.Done = it.Done || t.Checked + items = append(items, it) + return true, nil + } + + if closeWhen, ok := parseCloseWhen(body); ok { + explicit = true + for _, t := range closeWhen { + if len(items) >= trackerMaxReferences { + return nil, explicit, fmt.Errorf("close_when lists more than %d items", trackerMaxReferences) + } + if !t.HasRef { + // A checklist line with no issue or PR is done only when ticked. + it := trackerItem{Text: t.Text, State: "unchecked", Done: t.Checked} + if t.Checked { + it.State = "checked" + } + items = append(items, it) + continue + } + if _, aerr := add(t); aerr != nil { + return nil, explicit, aerr + } + } + sortTrackerItems(items) + return items, explicit, nil + } + + for _, t := range parseTrackerTasks(body) { + if !t.HasRef { + continue + } + if len(items) >= trackerMaxReferences { + return nil, false, fmt.Errorf("tracker references more than %d items", trackerMaxReferences) + } + // Inferred items are done only when closed, never merely ticked. + t.Checked = false + if _, aerr := add(t); aerr != nil { + return nil, false, aerr + } + } + + events, terr := c.IssueTimelineSince(ctx, owner+"/"+name, number, time.Time{}) + if terr != nil { + return nil, false, fmt.Errorf("reading timeline of %s/%s#%d: %w", owner, name, number, terr) + } + for _, ev := range events { + if ev.Event != "cross-referenced" || ev.SourcePR <= 0 { + continue + } + if ev.SourceRepo != "" && !strings.EqualFold(ev.SourceRepo, repoFull) { + continue + } + ref := ChildRef{Number: ev.SourcePR} + if seen[ref] { + continue + } + it, pr, rerr := c.readTrackerItem(ctx, owner, name, ref) + if rerr != nil { + return nil, false, rerr + } + if !trackerRefersTo(pr.GetBody(), repoFull, number) { + continue + } + seen[ref] = true + if len(items) >= trackerMaxReferences { + return nil, false, fmt.Errorf("tracker references more than %d items", trackerMaxReferences) + } + items = append(items, it) + } + sortTrackerItems(items) + return items, false, nil +} + +// sortTrackerItems orders evidence by reference so the fingerprint and the +// comment do not depend on enumeration order. +func sortTrackerItems(items []trackerItem) { + sort.SliceStable(items, func(i, j int) bool { + a, b := items[i], items[j] + if a.Ref.Repo != b.Ref.Repo { + return a.Ref.Repo < b.Ref.Repo + } + if a.Ref.Number != b.Ref.Number { + return a.Ref.Number < b.Ref.Number + } + return a.Text < b.Text + }) +} + +// closeDoneTracker closes an open tracker/umbrella issue with no sub-issue +// children once everything it references is finished (hivecommons/hive#19071). +// It returns true when the issue was closed. It costs nothing unless the repo +// opted in with trackers_too and the issue announces itself as a tracker; the +// reads are cached per tracker, and the close needs a second fresh read with +// the same finished evidence. The evidence comment is posted before the +// close; a failed or refused comment means no close. +func (c *Client) closeDoneTracker(ctx context.Context, in IssueStageInput, store *stagelabels.Store, key string, now time.Time, logger *slog.Logger) bool { + if in.Held || !in.TrackerSignal || c.parentAutoClosePolicy(in.Repo) != config.ParentAutoCloseTrackersToo { + return false + } + // Label/title exemptions cost no API call. Hold is respected here even + // though trackers_too implies epics_too: a hold is an owner's pause. + if HasHoldLabel(in.Labels) || gfi.IsTracking(in.Title, in.Labels, "") { + return false + } + if !trackerReadDue(key, now) { + return false + } + owner, name := c.splitRepo(in.Repo) + issue, _, err := c.client.Issues.Get(ctx, owner, name, in.Number) + if err != nil { + if logger != nil { + logger.Warn("issue stage labels: tracker unreadable, not closing", "repo", in.Repo, "issue", in.Number, "error", err) + } + return false + } + if !strings.EqualFold(issue.GetState(), "open") || issue.IsPullRequest() { + return false + } + if why, tracking := isTrackingIssue(issue); tracking { + if logger != nil { + logger.Debug("issue stage labels: tracker not auto-closed, marked as a tracking issue", "repo", in.Repo, "issue", in.Number, "why", why) + } + return false + } + items, explicit, err := c.trackerEvidence(ctx, owner, name, in.Number, issue.GetBody()) + if err != nil { + if logger != nil { + logger.Warn("issue stage labels: tracker references unreadable, not closing", "repo", in.Repo, "issue", in.Number, "error", err) + } + // Forget the settle state: the evidence is unknown. + trackerSettled(key, now, "", false) + return false + } + hash := trackerEvidenceHash(items) + done := trackerAllDone(items) + if !trackerSettled(key, now, hash, done) { + return false + } + if by, kept := c.parentKeptOpenBy(ctx, owner, name, in.Number, logger); kept || by != "" { + if logger != nil { + logger.Info("issue stage labels: tracker kept open, reopened by a person after a hive auto-close", "repo", in.Repo, "issue", in.Number, "reopened_by", by) + } + return false + } + note := trackerEvidenceComment(items, explicit, hash) + needle := trackerEvidenceMarker + hash + " -->" + posted, cerr := c.IssueCommentsContain(ctx, in.Repo, in.Number, needle) + if cerr != nil { + if logger != nil { + logger.Warn("issue stage labels: tracker comments unreadable, not closing", "repo", in.Repo, "issue", in.Number, "error", cerr) + } + return false + } + if !posted { + if err := c.CreateIssueCommentBudgeted(ctx, in.Repo, in.Number, note); err != nil { + if logger != nil { + logger.Warn("issue stage labels: tracker evidence comment failed, not closing", "repo", in.Repo, "issue", in.Number, "error", err) + } + return false + } + } + if _, err := stagelabels.Enforce(ctx, c.StageLabeler(), stagelabels.IssueItem, owner, name, in.Number, in.Labels, stagelabels.Desired{}); err != nil && logger != nil { + logger.Warn("issue stage labels: clearing stage labels on tracker failed", "repo", in.Repo, "issue", in.Number, "error", err) + } + if err := c.CloseIssue(ctx, in.Repo, in.Number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}); err != nil { + if logger != nil { + logger.Warn("issue stage labels: closing tracker failed", "repo", in.Repo, "issue", in.Number, "error", err) + } + return false + } + if store != nil { + store.Forget(key) + } + if logger != nil { + logger.Info("issue stage labels: tracker closed, all referenced work finished", "repo", in.Repo, "issue", in.Number, "evidence", hash, "references", len(items)) + } + return true +} diff --git a/src/pkg/github/tracker_autoclose_test.go b/src/pkg/github/tracker_autoclose_test.go new file mode 100644 index 0000000000..0599bf51e4 --- /dev/null +++ b/src/pkg/github/tracker_autoclose_test.go @@ -0,0 +1,303 @@ +package github + +import ( + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +func TestParseTrackerTasks(t *testing.T) { + body := "Intro #99\n- [ ] #11 first\n* [x] acme/other#12 done\n- [ ] no reference here\n- [ ] see issue#5 not a ref\n - [ ] nested #13\n" + got := parseTrackerTasks(body) + if len(got) != 5 { + t.Fatalf("items = %+v", got) + } + if !got[0].HasRef || got[0].Ref != (ChildRef{Number: 11}) || got[0].Checked { + t.Errorf("item0 = %+v", got[0]) + } + if !got[1].HasRef || got[1].Ref != (ChildRef{Repo: "acme/other", Number: 12}) || !got[1].Checked { + t.Errorf("item1 = %+v", got[1]) + } + if got[2].HasRef || got[3].HasRef { + t.Errorf("lines without a bare or repo-qualified ref must not carry one: %+v %+v", got[2], got[3]) + } + if !got[4].HasRef || got[4].Ref.Number != 13 { + t.Errorf("item4 = %+v", got[4]) + } +} + +func TestParseCloseWhen(t *testing.T) { + body := "- [ ] #1 outside\n\n## close_when\n\n- [x] ship docs\n- [ ] #2\n\nMore prose\n- [ ] #3 after prose\n" + items, ok := parseCloseWhen(body) + if !ok || len(items) != 2 { + t.Fatalf("ok=%v items=%+v", ok, items) + } + if !items[0].Checked || items[0].HasRef || items[1].Ref.Number != 2 { + t.Errorf("items = %+v", items) + } + if _, ok := parseCloseWhen("- [ ] #1\n"); ok { + t.Error("a body without a close_when marker must not report one") + } + if items, ok := parseCloseWhen("close_when:\n- [ ] #4"); !ok || len(items) != 1 { + t.Errorf("close_when: line form = %+v %v", items, ok) + } +} + +func TestTrackerSignal(t *testing.T) { + cases := []struct { + name string + title string + labels []string + body string + want bool + }{ + {"plain issue with Refs PRs only", "Betty on Zoom without a Zoom app", []string{"help wanted"}, "design text", false}, + {"tracker label", "x", []string{"Tracker"}, "", true}, + {"audit label", "x", []string{"audit"}, "", true}, + {"umbrella title prefix", "[Umbrella] fail-closed defaults", nil, "", true}, + {"task list of two references", "x", nil, "- [ ] #4\n- [ ] #5", true}, + {"a single reference is a blocked-on note", "x", nil, "- [ ] #4", false}, + {"task list without a reference", "x", nil, "- [ ] write docs", false}, + {"close_when", "x", nil, "close_when:\n- [x] done", true}, + } + for _, tc := range cases { + if got := trackerSignal(tc.title, tc.labels, tc.body); got != tc.want { + t.Errorf("%s: signal = %v, want %v", tc.name, got, tc.want) + } + } +} + +func TestTrackerRefersTo(t *testing.T) { + cases := []struct { + body string + want bool + }{ + {"Refs #7", true}, + {"refs: #7", true}, + {"Part of #7", true}, + {"Fixes acme/widgets#7", true}, + {"Refs #70", false}, + {"Refs other/repo#7", false}, + {"mentions #7 in passing", false}, + } + for _, tc := range cases { + if got := trackerRefersTo(tc.body, "acme/widgets", 7); got != tc.want { + t.Errorf("%q: refers = %v, want %v", tc.body, got, tc.want) + } + } +} + +func resetTrackerReadsForTest(t *testing.T) { + t.Helper() + clear := func() { + trackerMu.Lock() + trackerReads = map[string]trackerReadRecord{} + trackerMu.Unlock() + } + clear() + t.Cleanup(clear) + resetParentReopenCacheForTest(t) +} + +type trackerCase struct { + title string + labels []string + body string + policy string + // closedRefs are referenced issues/PRs that are closed. + closedRefs []int + // prBodies are referencing PR bodies; every key also gets a + // cross-referenced timeline event on the tracker. + prBodies map[int]string + extra []map[string]any +} + +// runTrackerSweeps sweeps tracker #7 at stageNow() and again one settle +// interval later, and returns whether it closed after each sweep and the +// evidence comments posted. +func runTrackerSweeps(t *testing.T, tc trackerCase) (afterFirst, afterSecond bool, comments []string) { + t.Helper() + resetTrackerReadsForTest(t) + fake := newIssueStageServer(t) + c := NewClientForTest(fake.srv.URL, "acme", []string{"widgets"}, testLogger()) + policy := tc.policy + c.SetIssueStageLabels(func() IssueStagePolicy { + return IssueStagePolicy{Enabled: true, ParentAutoClose: func(string) string { return policy }} + }) + store := stagelabels.NewStore("", stageNow) + store.Observe(stagelabels.Key("acme/widgets", 7), stagelabels.Actionable, "actionable") + fake.mu.Lock() + fake.bodies[7] = tc.body + for _, n := range tc.closedRefs { + fake.closed[n] = "completed" + } + var events []map[string]any + for pr, body := range tc.prBodies { + fake.bodies[pr] = body + events = append(events, map[string]any{ + "event": "cross-referenced", + "created_at": "2026-10-01T00:00:00Z", + "source": map[string]any{"type": "issue", "issue": map[string]any{ + "number": pr, "pull_request": map[string]any{"url": "https://api.example/pulls"}, + "repository": map[string]any{"full_name": "acme/widgets"}, + }}, + }) + } + fake.timelines = map[int][]map[string]any{7: append(events, tc.extra...)} + fake.mu.Unlock() + + build := func() *ActionableResult { + result := &ActionableResult{} + result.Issues = IssueResultFromItems([]Issue{{Repo: "acme/widgets", Number: 7, Title: tc.title, Body: tc.body, + Labels: append([]string{stagelabels.Actionable.Label()}, tc.labels...)}}) + return result + } + syncSeeded(t, fake, c, build(), store, stageNow()) + fake.mu.Lock() + _, afterFirst = fake.closed[7] + fake.mu.Unlock() + syncSeeded(t, fake, c, build(), store, stageNow().Add(trackerSettleInterval+time.Minute)) + fake.mu.Lock() + defer fake.mu.Unlock() + _, afterSecond = fake.closed[7] + return afterFirst, afterSecond, append([]string(nil), fake.commentBodies[7]...) +} + +func TestTrackerAutoClose_AllReferencesDoneClosesAfterSettleWithEvidence(t *testing.T) { + first, second, comments := runTrackerSweeps(t, trackerCase{ + title: "x", labels: []string{"umbrella"}, body: "- [ ] #11\n- [ ] #12\n", policy: "trackers_too", + closedRefs: []int{11, 12}, + }) + if first { + t.Fatal("the first read must only record the evidence, not close") + } + if !second { + t.Fatal("the same finished evidence on a second read must close the tracker") + } + if len(comments) != 1 || !strings.Contains(comments[0], parentAutoCloseMarker) || + !strings.Contains(comments[0], "#11") || !strings.Contains(comments[0], "#12") || !strings.Contains(comments[0], "closed") { + t.Fatalf("evidence comment = %q", comments) + } +} + +func TestTrackerAutoClose_OneOpenReferenceKeepsItOpen(t *testing.T) { + _, second, comments := runTrackerSweeps(t, trackerCase{ + title: "x", labels: []string{"umbrella"}, body: "- [ ] #11\n- [ ] #12\n", policy: "trackers_too", + closedRefs: []int{11}, + }) + if second || len(comments) != 0 { + t.Fatalf("an open reference must keep the tracker open: closed=%v comments=%q", second, comments) + } +} + +func TestTrackerAutoClose_DefaultAndEpicsPoliciesNeverCloseTrackers(t *testing.T) { + for _, policy := range []string{"true", "epics_too", "false", ""} { + _, second, _ := runTrackerSweeps(t, trackerCase{ + title: "[Umbrella] x", body: "- [ ] #11\n", policy: policy, closedRefs: []int{11}, + }) + if second { + t.Fatalf("policy %q must not close a tracker", policy) + } + } +} + +func TestTrackerAutoClose_TrackingIssueNeverCloses(t *testing.T) { + for name, tc := range map[string]trackerCase{ + "label": {title: "[Umbrella] x", labels: []string{"hive/tracking"}, body: "- [ ] #11\n"}, + "marker": {title: "[Umbrella] x", body: "\n- [ ] #11\n"}, + "keep": {title: "[Umbrella] x", body: "Keep this issue open.\n- [ ] #11\n"}, + "hold": {title: "[Umbrella] x", labels: []string{"hold"}, body: "- [ ] #11\n"}, + "on hold": {title: "[Umbrella] x", labels: []string{"on-hold"}, body: "- [ ] #11\n"}, + } { + tc.policy = "trackers_too" + tc.closedRefs = []int{11} + if _, second, _ := runTrackerSweeps(t, tc); second { + t.Fatalf("%s: a tracking or held issue must never be closed", name) + } + } +} + +func TestTrackerAutoClose_ReopenedByPersonNeverRecloses(t *testing.T) { + _, second, _ := runTrackerSweeps(t, trackerCase{ + title: "[Umbrella] x", body: "- [ ] #11\n", policy: "trackers_too", closedRefs: []int{11}, + extra: []map[string]any{ + timelineEvent("commented", "hive[bot]", "Bot", parentAutoCloseMarker+"\nclosed", "2026-10-02T00:00:00Z"), + timelineEvent("closed", "hive[bot]", "Bot", "", "2026-10-02T00:00:01Z"), + timelineEvent("reopened", "alice", "User", "", "2026-10-03T00:00:00Z"), + }, + }) + if second { + t.Fatal("a tracker a person reopened after an auto-close must stay open") + } +} + +func TestTrackerAutoClose_EmptyReferenceSetNeverCloses(t *testing.T) { + for name, tc := range map[string]trackerCase{ + "tracker label, nothing referenced": {title: "x", labels: []string{"tracker"}, body: "free text"}, + "close_when with no items": {title: "x", body: "close_when:\n\nprose"}, + } { + tc.policy = "trackers_too" + if _, second, _ := runTrackerSweeps(t, tc); second { + t.Fatalf("%s: an empty reference set must never close", name) + } + } +} + +func TestTrackerAutoClose_PlainIssueWithRefsPRsIsNotACandidate(t *testing.T) { + _, second, _ := runTrackerSweeps(t, trackerCase{ + title: "Betty on Zoom without a Zoom app", body: "design text", policy: "trackers_too", + closedRefs: []int{21}, prBodies: map[int]string{21: "Refs #7"}, + }) + if second { + t.Fatal("an issue that does not announce itself as a tracker must not close from Refs PRs alone") + } +} + +func TestTrackerAutoClose_InfersFromRefsPRs(t *testing.T) { + _, second, comments := runTrackerSweeps(t, trackerCase{ + title: "x", labels: []string{"tracker"}, body: "free text", policy: "trackers_too", + closedRefs: []int{21, 22}, + prBodies: map[int]string{21: "Refs #7", 22: "Part of #7", 23: "only mentions #7 in passing"}, + }) + if !second { + t.Fatal("every referencing PR closed must close the tracker; a PR that merely mentions it is not a reference") + } + if len(comments) != 1 || strings.Contains(comments[0], "#23") { + t.Fatalf("comment = %q", comments) + } +} + +func TestTrackerAutoClose_OpenRefsPRKeepsItOpen(t *testing.T) { + _, second, _ := runTrackerSweeps(t, trackerCase{ + title: "x", labels: []string{"tracker"}, body: "free text", policy: "trackers_too", + closedRefs: []int{21}, prBodies: map[int]string{21: "Refs #7", 22: "Refs #7"}, + }) + if second { + t.Fatal("an open PR that references the tracker must keep it open") + } +} + +func TestTrackerAutoClose_CloseWhenReplacesInferredSet(t *testing.T) { + // #12 is an open task-list item and PR 22 is an open Refs PR, but the + // explicit close_when checklist names only #11 and a ticked step. + _, second, comments := runTrackerSweeps(t, trackerCase{ + title: "x", policy: "trackers_too", closedRefs: []int{11}, + body: "- [ ] #12 not part of close_when\n\n## close_when\n- [x] docs shipped\n- [ ] #11\n", + prBodies: map[int]string{22: "Refs #7"}, + }) + if !second { + t.Fatal("close_when must replace the inferred set") + } + if len(comments) != 1 || !strings.Contains(comments[0], "close_when") || strings.Contains(comments[0], "#12") { + t.Fatalf("comment = %q", comments) + } + _, second, _ = runTrackerSweeps(t, trackerCase{ + title: "x", policy: "trackers_too", closedRefs: []int{11}, + body: "close_when:\n- [ ] docs shipped\n- [ ] #11\n", + }) + if second { + t.Fatal("an unticked close_when step without a reference keeps the tracker open") + } +} diff --git a/src/pkg/writeback/testdata/direct-writes-baseline.txt b/src/pkg/writeback/testdata/direct-writes-baseline.txt index b02d1e37b7..0f57645eac 100644 --- a/src/pkg/writeback/testdata/direct-writes-baseline.txt +++ b/src/pkg/writeback/testdata/direct-writes-baseline.txt @@ -120,6 +120,7 @@ wrap pkg/github/review_backlog_routing.go 1 wrap pkg/github/review_backlog_selfcheck.go 1 wrap pkg/github/review_request_watcher.go 2 wrap pkg/github/stage_labeler.go 2 +wrap pkg/github/tracker_autoclose.go 1 wrap pkg/github/tracking_issue.go 1 wrap pkg/prfollowup/prfollowup.go 2 wrap pkg/questionclose/questionclose.go 2