From cc6847d32093f394bdefd286d62d2b825a77d816 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 17 Nov 2026 11:09:26 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Cross-repo=20decomposition=20chi?= =?UTF-8?q?ldren=20and=20cross-repo=20PR=20issue=20links?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scanner decomposition filed every child in the tracker's repo with a bare "Part of #N" / hive:parent N link, and a PR in another managed repo that said "Refs owner/repo#N" never covered its issue: the claim was keyed owner/repo#N while enumeration keys issues by the configured spelling. - hive-open-issue --parent owner/repo#N; the relay files the child in the managed repo where its work lives (falls back to the parent's repo when that repo is unmanaged; refuses an unmanaged parent repo), links it as a cross-repo sub-issue, rewrites the parent ref and marker to full form and records the parent repo in the split-parent ledger. - Tracker progress and decomposed-parent close count children in any repo. - Claims are keyed by the configured repo spelling, so a cross-repo PR covers its issue and the issue mirrors the PR stage; the stage-pr-link comment is posted for a PR in another repo; an agent claim is renewed while a hive PR covers the issue and released once it closes. Fixes #18676 Fixes #18680 Signed-off-by: Andy Anderson --- bin/hive-open-issue.sh | 30 +- bin/test_hive_open_issue.sh | 41 +++ ...ed-18676-cross-repo-pr-issue-stage-link.md | 1 + ...18680-cross-repo-decomposition-children.md | 1 + src/docs/hive-open-issue.md | 17 +- src/docs/scale-envelope.md | 2 +- .../dashboard/claims_agent_pr_hold_test.go | 77 +++++ src/pkg/dashboard/claims_api.go | 52 +++- src/pkg/github/client.go | 10 +- src/pkg/github/cross_repo_children.go | 251 +++++++++++++++++ src/pkg/github/cross_repo_children_test.go | 266 ++++++++++++++++++ src/pkg/github/cross_repo_pr_link_test.go | 114 ++++++++ src/pkg/github/decomposed_parents.go | 210 ++++++++++---- src/pkg/github/decomposed_parents_test.go | 18 +- src/pkg/github/issue_request_watcher.go | 51 +++- src/pkg/github/pr_issue_link.go | 57 +++- src/pkg/github/prclaims.go | 3 + src/pkg/github/split_parents.go | 24 +- .../policies/defaults/scanner-automerge.md | 2 +- src/policies/scanner-automerge.md | 2 +- 20 files changed, 1138 insertions(+), 91 deletions(-) create mode 100644 changelog.d/fixed-18676-cross-repo-pr-issue-stage-link.md create mode 100644 changelog.d/fixed-18680-cross-repo-decomposition-children.md create mode 100644 src/pkg/dashboard/claims_agent_pr_hold_test.go create mode 100644 src/pkg/github/cross_repo_children.go create mode 100644 src/pkg/github/cross_repo_children_test.go create mode 100644 src/pkg/github/cross_repo_pr_link_test.go diff --git a/bin/hive-open-issue.sh b/bin/hive-open-issue.sh index d59e91f814..235c88c775 100755 --- a/bin/hive-open-issue.sh +++ b/bin/hive-open-issue.sh @@ -16,7 +16,7 @@ # forge-resistance as the gh wrapper; this shim adds no privilege. # # Usage (drop-in for the common gh shapes): -# hive-open-issue --repo --title "" [--body ""|--body-file f] [--label a,b] [--parent ] [--blocked-by ] [--close-on-merge] [--needs-decision] [--no-consolidate ] [--claim] +# hive-open-issue --repo --title "" [--body ""|--body-file f] [--label a,b] [--parent ] [--blocked-by ] [--close-on-merge] [--needs-decision] [--no-consolidate ] [--claim] # hive-open-issue comment --repo --body "" [--blocked-on-pr ] [--not-agent-work ] # hive-open-issue claim --repo # hive-open-issue close --repo [--override-reason "..."] @@ -30,6 +30,14 @@ # so the link still reads in plain text. A failed link (parent missing, at # GitHub's sub-issue cap, …) does not stop the child issue from being created. # +# --parent (or the parent's issue URL) names a parent in +# ANOTHER repo (hivecommons/hive#18680). File each child in the managed repo +# where its work lives (--repo) and point --parent at the tracker: the +# watcher links it across repos, writes `Part of owner/repo#n` and a +# cross-repo `hive:parent` marker, and counts it in the tracker's progress. +# When --repo is not a repo this hive manages, the child is filed in the +# parent's repo instead (the result's repo_fallback_from says so). +# # --blocked-by (repeatable) records each issue in the same repo as # a GitHub "blocked by" dependency of the new issue (hivecommons/hive#9839). # Use it when a split has an order: the hive keeps a blocked child out of the @@ -405,7 +413,7 @@ REVIEWERS_JSON="$(printf '%s\n' "${REVIEWERS[@]:-}" | python3 -c 'import json,sy TEAM_REVIEWERS_JSON="$(printf '%s\n' "${TEAM_REVIEWERS[@]:-}" | python3 -c 'import json,sys; print(json.dumps([x.rstrip("\n") for x in sys.stdin if x.rstrip("\n")]))')" BLOCKED_BY_JSON="$(printf '%s\n' "${BLOCKED_BY[@]:-}" | python3 -c 'import json,sys; print(json.dumps([x.rstrip("\n") for x in sys.stdin if x.rstrip("\n")]))')" python3 - "$TEMP_FILE" "$REQ_FILE" "$KIND" "$REPO" "$TITLE" "${OVERRIDE_REASON:-$BODY}" "$AGENT" "$LABELS_JSON" "${NUMBER:-0}" "$DRY_RUN" "${PARENT:-0}" "$REMOVE_LABELS_JSON" "$REVIEWERS_JSON" "$TEAM_REVIEWERS_JSON" "$BLOCKED_BY_JSON" "$NEEDS_DECISION" "${BLOCKED_ON_PR:-0}" "$NO_CONSOLIDATE" "$CLAIM_ON_CREATE" "${NOT_AGENT_WORK:-}" <<'PY' -import json, os, sys +import json, os, re, sys temporary, path, kind, repo, title, body, agent, labels, number, dry_run, parent, remove_labels, reviewers, team_reviewers, blocked_by, needs_decision, blocked_on_pr, no_consolidate, claim_on_create, not_agent_work = sys.argv[1:21] labels = [part.strip() for value in json.loads(labels) for part in value.split(",") if part.strip()] @@ -450,8 +458,21 @@ else: req["labels"] = labels # --parent: link the new issue as a GitHub sub-issue of this number # (hivecommons/hive#9435). Only meaningful for a create. + # (hivecommons/hive#18680) Also accepts "owner/repo#N" or an issue URL + # for a parent in another repo. + parent = parent.strip() + parent_repo = "" + m = re.match(r'^(?:https?://github\.com/)?([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)(?:#|/issues/)(\d+)/?$', parent) + if m: + parent_repo = m.group(1) + "/" + m.group(2) + parent = m.group(3) + parent = parent.lstrip("#") + if not parent.isdigit(): + sys.exit("hive-open-issue: --parent must be N, #N, owner/repo#N or an issue URL") if int(parent) > 0: req["parent"] = int(parent) + if parent_repo and parent_repo.lower() != repo.strip().lower(): + req["parent_repo"] = parent_repo # --blocked-by: record GitHub "blocked by" dependencies on the new issue # (hivecommons/hive#9839). Accepts "12", "#12", "12,13", repeated flags. blockers = [] @@ -529,7 +550,10 @@ elif [ "$KIND" = "request_review" ]; then else echo "hive-open-issue: requested issue on $REPO as the App bot: $TITLE" if [ -n "$PARENT" ] && [ "$PARENT" != "0" ]; then - echo "hive-open-issue: will link as a GitHub sub-issue of $REPO#$PARENT" + case "$PARENT" in + */*) echo "hive-open-issue: will link as a GitHub sub-issue of $PARENT" ;; + *) echo "hive-open-issue: will link as a GitHub sub-issue of $REPO#${PARENT#\#}" ;; + esac fi if [ "$NEEDS_DECISION" -eq 1 ]; then echo "hive-open-issue: will park the issue on a maintainer decision (needs-decision label)" diff --git a/bin/test_hive_open_issue.sh b/bin/test_hive_open_issue.sh index e40beadd35..fafa2373c3 100755 --- a/bin/test_hive_open_issue.sh +++ b/bin/test_hive_open_issue.sh @@ -177,6 +177,47 @@ fi rm -rf "$REQ_DIR" mkdir -p "$REQ_DIR" +# A cross-repo parent (hivecommons/hive#18680): owner/repo#N and an issue URL +# both produce parent + parent_repo; a same-repo owner/repo#N omits parent_repo. +run_script "xparentbot" --repo "hiveorg/companion" --title "Cross child" --body "body" \ + --parent "hiveorg/widgets#100" +REQ_FILE="$(find_req xparentbot)" +if [ -n "$REQ_FILE" ]; then + GOT="$(python3 -c "import json,sys; d=json.load(open(sys.argv[1])); print(d.get('parent', 0), d.get('parent_repo', ''))" "$REQ_FILE")" + check "--parent owner/repo#N produces parent and parent_repo" "100 hiveorg/widgets" "$GOT" +else + echo " FAIL: request file not created for cross-repo --parent test" + FAIL=$((FAIL + 1)) +fi +rm -rf "$REQ_DIR" +mkdir -p "$REQ_DIR" + +run_script "urlparentbot" --repo "hiveorg/companion" --title "URL child" --body "body" \ + --parent "https://github.com/hiveorg/widgets/issues/101" +REQ_FILE="$(find_req urlparentbot)" +if [ -n "$REQ_FILE" ]; then + GOT="$(python3 -c "import json,sys; d=json.load(open(sys.argv[1])); print(d.get('parent', 0), d.get('parent_repo', ''))" "$REQ_FILE")" + check "--parent issue URL produces parent and parent_repo" "101 hiveorg/widgets" "$GOT" +else + echo " FAIL: request file not created for URL --parent test" + FAIL=$((FAIL + 1)) +fi +rm -rf "$REQ_DIR" +mkdir -p "$REQ_DIR" + +run_script "sameparentbot" --repo "hiveorg/widgets" --title "Same child" --body "body" \ + --parent "hiveorg/widgets#102" +REQ_FILE="$(find_req sameparentbot)" +if [ -n "$REQ_FILE" ]; then + GOT="$(python3 -c "import json,sys; d=json.load(open(sys.argv[1])); print(d.get('parent', 0), 'parent_repo' in d)" "$REQ_FILE")" + check "--parent naming the same repo omits parent_repo" "102 False" "$GOT" +else + echo " FAIL: request file not created for same-repo --parent test" + FAIL=$((FAIL + 1)) +fi +rm -rf "$REQ_DIR" +mkdir -p "$REQ_DIR" + # Omitting --parent must not add the field at all (optional, not zero-valued). run_script "noparentbot" --repo "hiveorg/widgets" --title "No parent" --body "body" diff --git a/changelog.d/fixed-18676-cross-repo-pr-issue-stage-link.md b/changelog.d/fixed-18676-cross-repo-pr-issue-stage-link.md new file mode 100644 index 0000000000..0e071b6c8c --- /dev/null +++ b/changelog.d/fixed-18676-cross-repo-pr-issue-stage-link.md @@ -0,0 +1 @@ +- A PR in one managed repo that references an issue in another (`Refs owner/repo#N`, `Fixes owner/repo#N`) now covers that issue ([#18676](https://github.com/hivecommons/hive/issues/18676)). The claim was keyed `owner/repo#N` while enumeration keys issues by the configured (often bare) repo name, so the issue stayed Actionable with no PR. The claim is now keyed by the configured spelling, the issue is staged covered-by-PR and follows the PR's stage (for example needs-decision), it gets a `hive:issue-stage-pr-link` comment naming the PR as `owner/repo#N`, and the hive agent's claim on the issue is renewed while that PR is open and released once it merges or closes. diff --git a/changelog.d/fixed-18680-cross-repo-decomposition-children.md b/changelog.d/fixed-18680-cross-repo-decomposition-children.md new file mode 100644 index 0000000000..61e899258d --- /dev/null +++ b/changelog.d/fixed-18680-cross-repo-decomposition-children.md @@ -0,0 +1 @@ +- Scanner decomposition now files each child issue in the managed repo where its work lives, not always in the tracker's repo ([#18680](https://github.com/hivecommons/hive/issues/18680)). `hive-open-issue --parent owner/repo#N` (or the parent's issue URL) names a parent in another repo: the relay links the child as a cross-repo sub-issue, writes `Part of owner/repo#N` and a `` marker (a bare `#N` only resolves inside the child's own repo), and the split-parent ledger records the parent's repo. A tracker's progress and its auto-close count children in any managed repo. A parent repo the hive does not manage is refused, and a child repo it does not manage falls back to the parent's repo (`repo_fallback_from` in the result), so children are never filed in unmanaged repos. diff --git a/src/docs/hive-open-issue.md b/src/docs/hive-open-issue.md index 9f64c1df7e..9eb8ba1286 100644 --- a/src/docs/hive-open-issue.md +++ b/src/docs/hive-open-issue.md @@ -42,7 +42,7 @@ Shapes are selected by an optional leading positional keyword (`comment`, `issue`): ```sh -hive-open-issue --repo --title "" [--body ""|--body-file f] [--label a,b] [--parent ] [--blocked-by ] [--needs-decision] [--no-consolidate ] [--claim] +hive-open-issue --repo --title "" [--body ""|--body-file f] [--label a,b] [--parent ] [--blocked-by ] [--needs-decision] [--no-consolidate ] [--claim] hive-open-issue comment --repo --body "" [--blocked-on-pr ] [--not-agent-work ] hive-open-issue claim --repo hive-open-issue label --repo [--label a,b] [--remove-label c] @@ -59,7 +59,7 @@ hive-open-issue request-review --repo [--reviewer a,b] | `--remove-label` | — | label | repeatable; labels to take off the item | | `--reviewer` | `--add-reviewer` | request-review | repeatable; comma-separated GitHub logins to ask for a review | | `--team-reviewer` | — | request-review | repeatable; team slugs (an `org/` prefix is accepted) to ask for a review | -| `--parent` | — | issue | issue number in the same repo to link the new issue to as a GitHub sub-issue. Declaring it also exempts the request from the file-set, component and canonical-subject folds (see [DAG children never take a fold](#dag-children-never-take-a-fold)), so `--no-consolidate` is not needed for a decomposition child | +| `--parent` | — | issue | issue number in the same repo (or `owner/repo#n` for a parent in another managed repo) to link the new issue to as a GitHub sub-issue. Declaring it also exempts the request from the file-set, component and canonical-subject folds (see [DAG children never take a fold](#dag-children-never-take-a-fold)), so `--no-consolidate` is not needed for a decomposition child | | `--blocked-by` | — | issue | repeatable; issue numbers in the same repo the new issue is blocked by, recorded as GitHub "blocked by" dependencies (`#` prefix tolerated). Like `--parent`, declaring it exempts the request from the shared-path folds (see [DAG children never take a fold](#dag-children-never-take-a-fold)) | | `--needs-decision` | — | issue | park the new issue on a maintainer decision: the watcher applies the hive's configured needs-decision label itself (see [Parking an issue that needs a decision](#parking-an-issue-that-needs-a-decision)) | | `--claim` | — | issue | file the new issue **and** claim it for this agent in the same request (see [Filing your own next step](#filing-your-own-next-step)). The request carries `"claim": true`; the result reports `claimed`, or `claim_error` when the claim did not land | @@ -140,6 +140,19 @@ is never marked `stage/stalled` ([#12857](https://github.com/hivecommons/hive/issues/12857); see [labels-and-control-signals.md](labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled)). +`--parent ` (or the parent's issue URL) names a parent in +another managed repo, for a tracker whose child work lives elsewhere +([#18680](https://github.com/hivecommons/hive/issues/18680)). File the child in +the repo where its work lives (`--repo`); the request carries `"parent"` and +`"parent_repo"`. The watcher links the child as a cross-repo sub-issue of +`owner/repo#n`, rewrites bare `#n` mentions of the parent (including the +"Part of" line, which it adds when missing) to `owner/repo#n`, and writes the +marker as ``, because a bare `#n` resolves +only inside the child's own repo. The parent's tracker progress counts such +children too. A parent repo this hive does not manage is refused; a child +repo it does not manage falls back to the parent's repo, and the result's +`repo_fallback_from` names the repo that was asked for. + `--blocked-by ` records that the new issue must wait for issue(s) `n` in the same repo, as GitHub "blocked by" dependencies (`POST /repos/{owner}/{repo}/issues/{new}/dependencies/blocked_by`), after the diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 3ace5f8388..e734801b85 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5343-5353`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5351-5361`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/dashboard/claims_agent_pr_hold_test.go b/src/pkg/dashboard/claims_agent_pr_hold_test.go new file mode 100644 index 0000000000..82e61017a3 --- /dev/null +++ b/src/pkg/dashboard/claims_agent_pr_hold_test.go @@ -0,0 +1,77 @@ +package dashboard + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/claims" + ghpkg "github.com/hivecommons/hive/pkg/github" +) + +// An agent claim stays live while the hive's open PR in ANOTHER managed repo +// references the issue, and is released once that PR stops covering it +// (hivecommons/hive#18676: hivecommons/quorum-companion#3 `Refs +// hivecommons/hive#18660`, whose claim lapsed while the PR was open). +func TestClaims_AgentClaimHeldByCrossRepoPR(t *testing.T) { + hub, s, l := claimsHub(t) + covering := ghpkg.IssueClaim{Repo: "repo1", Issue: 1, PRRepo: "companion", PRNumber: 3, + PRURL: "https://github.com/myorg/companion/pull/3", PRAuthor: "hive[bot]", PRState: ghpkg.PRStateOpen, Reference: true} + open := true + s.deps.IssueClaimed = func(repo string, number int) (ghpkg.IssueClaim, bool) { + // The duplicate-PR ledger keys the claim by the configured (bare) + // spelling; the sweep must find it from the claim's owner/repo key. + if repo == "repo1" && number == 1 && open { + return covering, true + } + return ghpkg.IssueClaim{}, false + } + res, err := l.Claim(claims.Request{Repo: "myorg/repo1", Issue: 1, Holder: "scanner", HolderID: "scanner", Kind: claims.KindAgent}) + if err != nil || !res.Outcome.Changed() { + t.Fatalf("claim: %+v %v", res, err) + } + before := res.Claim.ExpiresAt + + if n := hub.sweepPRHeldClaims(); n != 0 { + t.Fatalf("sweep released %d claims while the cross-repo PR is open", n) + } + c, ok := l.Lookup("myorg/repo1", 1) + if !ok || c.Holder != "scanner" || !c.PRStarted || c.PRRepo != "companion" || c.PRNumber != 3 { + t.Fatalf("claim after sweep = %+v ok=%v, want held for companion#3", c, ok) + } + if !c.ExpiresAt.After(before) { + t.Fatalf("claim expiry %v not renewed past %v", c.ExpiresAt, before) + } + + // External PRs and merged PRs never hold an agent's claim. + for _, pr := range []ghpkg.IssueClaim{ + {Repo: "repo1", Issue: 2, PRRepo: "companion", PRNumber: 4, ExternalAuthor: true}, + {Repo: "repo1", Issue: 2, PRRepo: "companion", PRNumber: 4, MergedPR: true}, + } { + if _, ok := (&ContributeWSHub{server: &Server{deps: &Dependencies{IssueClaimed: func(string, int) (ghpkg.IssueClaim, bool) { return pr, true }}}}).agentCoveringPR(claims.Claim{Repo: "myorg/repo1", Issue: 2}); ok { + t.Fatalf("%+v must not hold an agent claim", pr) + } + } + + open = false + if n := hub.sweepPRHeldClaims(); n != 1 { + t.Fatalf("sweep released %d claims once the PR closed, want 1", n) + } + if _, ok := l.Lookup("myorg/repo1", 1); ok { + t.Fatal("PR-held agent claim survived its PR closing") + } +} + +// An agent claim with no covering PR is left to its own TTL: the sweep never +// releases a claim it did not hold. +func TestClaims_AgentClaimWithoutPRLeftAlone(t *testing.T) { + hub, s, l := claimsHub(t) + s.deps.IssueClaimed = func(string, int) (ghpkg.IssueClaim, bool) { return ghpkg.IssueClaim{}, false } + if _, err := l.Claim(claims.Request{Repo: "myorg/repo1", Issue: 5, Holder: "scanner", HolderID: "scanner", Kind: claims.KindAgent}); err != nil { + t.Fatal(err) + } + if n := hub.sweepPRHeldClaims(); n != 0 { + t.Fatalf("released %d", n) + } + if c, ok := l.Lookup("myorg/repo1", 5); !ok || c.PRStarted { + t.Fatalf("claim = %+v ok=%v, want untouched", c, ok) + } +} diff --git a/src/pkg/dashboard/claims_api.go b/src/pkg/dashboard/claims_api.go index 6c86eab4d1..2ee418bffc 100644 --- a/src/pkg/dashboard/claims_api.go +++ b/src/pkg/dashboard/claims_api.go @@ -269,7 +269,9 @@ func (h *ContributeWSHub) recordCompletionPRClaim(task *WSTaskAssign, prURL, use // covers the issue the hold is renewed; once it merged, closed, or stopped // covering the issue the claim is released. Without the duplicate-PR ledger // wired there is nothing to re-check against, so the holds are left to their -// own TTL. Returns how many claims were released. +// own TTL. A hive agent's claim is renewed the same way while an open hive +// PR covers its issue, in any managed repo (sweepAgentPRHeldClaim, +// hivecommons/hive#18676). Returns how many claims were released. func (h *ContributeWSHub) sweepPRHeldClaims() int { l := h.claimsLedger() if l == nil || h.server == nil || h.server.deps == nil || h.server.deps.IssueClaimed == nil { @@ -277,6 +279,10 @@ func (h *ContributeWSHub) sweepPRHeldClaims() int { } released := 0 for _, c := range l.List() { + if c.Kind == claims.KindAgent && c.HolderID != "" { + released += h.sweepAgentPRHeldClaim(l, c) + continue + } if c.Kind != claims.KindContributor || c.PRNumber <= 0 || c.HolderID == "" { continue } @@ -294,6 +300,50 @@ func (h *ContributeWSHub) sweepPRHeldClaims() int { return released } +// sweepAgentPRHeldClaim keeps a hive agent's claim alive while an open PR +// the hive authored covers the issue, in whatever managed repo that PR lives +// (hivecommons/hive#18676): the scanner claimed hivecommons/hive#18660 and +// opened hivecommons/quorum-companion#3 with `Refs hivecommons/hive#18660`; +// nothing renewed the claim once the agent's session ended, so it lapsed +// two hours later while the PR waited on a decision and the issue read as +// abandoned. A claim held this way (Claim.PRNumber set) is released once no +// open hive PR covers the issue any more. A claim the sweep never held is +// left to its own TTL. Returns 1 when it released the claim. +func (h *ContributeWSHub) sweepAgentPRHeldClaim(l *claims.Ledger, c claims.Claim) int { + if pr, ok := h.agentCoveringPR(c); ok { + l.HoldForPR(c.HolderID, c.Key(), coveringPRRepo(pr), pr.PRNumber) + return 0 + } + if c.PRNumber <= 0 { + return 0 + } + released := 0 + for _, r := range l.ReleaseByHolderID(c.HolderID, claims.ReasonCoveringPRDone, c.Key()) { + released++ + h.logger.Info("[claims] PR-held agent claim released", "issue", r.Key(), "holder", r.Holder, "pr", r.PRNumber, "pr_repo", r.PRRepo) + } + return released +} + +// agentCoveringPR reports the open, hive-authored pull request the +// duplicate-PR claim ledger records against an agent claim's issue — +// closing or a non-closing `Refs`, same repo or another managed repo (the +// ledger keys a cross-repo `Refs owner/repo#N` under the issue's repo). +func (h *ContributeWSHub) agentCoveringPR(c claims.Claim) (ghpkg.IssueClaim, bool) { + name := c.Repo + if i := strings.LastIndex(name, "/"); i >= 0 { + name = name[i+1:] + } + pr, ok := h.issueClaimedByOpenPR(c.Repo, name, c.Issue) + if !ok || pr.PRNumber <= 0 || pr.MergedPR || pr.ExternalAuthor { + return ghpkg.IssueClaim{}, false + } + if state := strings.TrimSpace(pr.PRState); state != "" && state != ghpkg.PRStateOpen { + return ghpkg.IssueClaim{}, false + } + return pr, true +} + // removeLegacyClaimLabelForLease tells the stage engine the contributor // claim that travelled with a lease is gone (a ClaimReleased fact, ADR-0025) // so the issue's stage/020-claimed comes off; the engine re-reads the issue diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 2d13c119e1..ebab4f1b2a 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -57,6 +57,11 @@ type Client struct { // transiently refused fetch does not publish the repo as empty // (enumerate_lastgood.go). Zero value is ready to use. lastGoodEnum lastGoodEnumeration + // openSnapshots keeps each repo's newest set of open issue numbers so a + // parent's tracker progress counts children filed in another managed + // repo (cross_repo_children.go, hivecommons/hive#18680). Zero value is + // ready to use. + openSnapshots openIssueSnapshots // forge is the per-repo state cache EnumerateActionable reads listings // from when attached (forgestate_reader.go, hivecommons/hive#13375). // Nil lists GitHub directly. @@ -1899,11 +1904,14 @@ func (c *Client) fetchIssues(ctx context.Context, repo string, now time.Time, re splitParents = nil } openByNumber := make(map[int]*gh.Issue, len(allIssues)) + openNumbers := make(map[int]struct{}, len(allIssues)) for _, issue := range allIssues { if !issue.IsPullRequest() { openByNumber[issue.GetNumber()] = issue + openNumbers[issue.GetNumber()] = struct{}{} } } + c.openSnapshots.store(fullRepoName(owner, repoName), openNumbers) for _, issue := range allIssues { if issue.IsPullRequest() { @@ -2079,7 +2087,7 @@ func (c *Client) fetchIssues(ctx context.Context, repo string, now time.Time, re }) } dropMutualBlocks(actionable, c.logger) - annotateIssueChildren(repo, subIssueSummaries, splitParents, openByNumber, actionable, details) + annotateIssueChildren(fullRepoName(owner, repoName), subIssueSummaries, splitParents, openByNumber, c.openSnapshots.isOpen, actionable, details) // Keep an honest fallback if a future exclusion path is added without a // dedicated category. This is the same enumeration snapshot, not a second diff --git a/src/pkg/github/cross_repo_children.go b/src/pkg/github/cross_repo_children.go new file mode 100644 index 0000000000..0ee10a237c --- /dev/null +++ b/src/pkg/github/cross_repo_children.go @@ -0,0 +1,251 @@ +package github + +import ( + "regexp" + "sort" + "strconv" + "strings" + "sync" +) + +// Cross-repo decomposition (hivecommons/hive#18680). +// +// A tracker in one managed repo can be split into children whose work lives +// in another managed repo — hivecommons/hive#18596 (the companion app +// roadmap) has children whose code is all in hivecommons/quorum-companion. +// The relay files such a child in the repo where its work lives and links it +// back to the parent in full cross-repo form: `Part of owner/repo#N` in the +// body and `` as the machine-readable +// marker. A bare `#N` resolves only inside the child's own repo, so a bare +// link on a cross-repo child names the wrong issue. + +// ChildRef names one child of a decomposed parent. Repo is the child's +// "owner/name" when it lives in a different repo from the parent, and "" +// when it shares the parent's repo. +type ChildRef struct { + Repo string + Number int +} + +// String renders the ref the way GitHub resolves it from the parent: a bare +// `#N` for a same-repo child, `owner/repo#N` otherwise. +func (r ChildRef) String() string { + if r.Repo == "" { + return "#" + strconv.Itoa(r.Number) + } + return r.Repo + "#" + strconv.Itoa(r.Number) +} + +// sortChildRefs orders same-repo children first, then by repo and number. +func sortChildRefs(refs []ChildRef) { + sort.Slice(refs, func(i, j int) bool { + a, b := refs[i], refs[j] + if a.Repo != b.Repo { + return a.Repo < b.Repo + } + return a.Number < b.Number + }) +} + +// fullRepoName is the lowercased "owner/name" spelling repo resolves to, +// qualifying a bare name with org. +func fullRepoName(org, repo string) string { + repo = strings.TrimSpace(repo) + if repo == "" { + return "" + } + if !strings.Contains(repo, "/") && org != "" { + repo = org + "/" + repo + } + return strings.ToLower(repo) +} + +// ParentMarkerFor returns the body marker for parent issue n in parentRepo. +// An empty parentRepo means the child's own repo and yields the bare form +// ParentMarker writes; any other repo yields ``. +func ParentMarkerFor(parentRepo string, n int) string { + parentRepo = strings.TrimSpace(parentRepo) + if parentRepo == "" { + return ParentMarker(n) + } + return parentMarkerPrefix + parentRepo + "#" + strconv.Itoa(n) + " -->" +} + +// ParseParentMarkerRef returns the parent a body's marker names: its repo +// ("" for the bare same-repo form) and number, or ("", 0) when there is no +// marker. +func ParseParentMarkerRef(body string) (repo string, n int) { + m := parentMarkerRe.FindStringSubmatch(body) + if m == nil { + return "", 0 + } + n, _ = strconv.Atoi(m[2]) + return m[1], n +} + +// markerNamesParent reports whether body's parent marker names parent issue +// number in repoFull (lowercased "owner/name"): a bare marker always means +// the child's own repo, so it matches only when the child is enumerated +// from repoFull itself. +func markerNamesParent(body, repoFull string, parent int) bool { + repo, n := ParseParentMarkerRef(body) + if n != parent || n <= 0 { + return false + } + return repo == "" || strings.EqualFold(repo, repoFull) +} + +// bareParentRefRe matches a bare `#N` that is not already qualified with a +// repo (no word character or `/` before the `#`) and is not the prefix of a +// longer number. +func bareParentRefRe(n int) *regexp.Regexp { + return regexp.MustCompile(`(^|[^\w/#])#` + strconv.Itoa(n) + `\b`) +} + +// withCrossRepoParentReference rewrites a cross-repo child's body so its +// link to the parent resolves from the child's repo (hivecommons/hive#18680): +// every bare `#N` naming the parent becomes `owner/repo#N`, and when the +// body has no `Part of owner/repo#N` line one is put first. A same-repo +// child (parentRepo "") is returned unchanged. +func withCrossRepoParentReference(body, parentRepo string, parent int) string { + parentRepo = strings.TrimSpace(parentRepo) + if parentRepo == "" || parent <= 0 { + return body + } + full := parentRepo + "#" + strconv.Itoa(parent) + // The marker is rewritten by withParentMarker; leave it alone here so a + // bare marker is replaced rather than turned into a second spelling. + marker := "" + if m := parentMarkerRe.FindString(body); m != "" { + marker = m + body = strings.Replace(body, m, "\x00hive-parent-marker\x00", 1) + } + body = bareParentRefRe(parent).ReplaceAllString(body, "${1}"+full) + if marker != "" { + body = strings.Replace(body, "\x00hive-parent-marker\x00", marker, 1) + } + partOf := regexp.MustCompile(`(?i)\bpart\s+of\s+` + regexp.QuoteMeta(full) + `\b`) + if !partOf.MatchString(body) { + trimmed := strings.TrimLeft(body, "\n") + if trimmed == "" { + return "Part of " + full + } + body = "Part of " + full + "\n\n" + trimmed + } + return body +} + +// openIssueSnapshots keeps the newest enumerated set of open issue numbers +// per repo (lowercased "owner/name"), so a parent's tracker progress can +// count a child that lives in another managed repo (hivecommons/hive#18680). +// Repos are enumerated one after another, so a parent may read another +// repo's set from the previous pass; that lag only affects the display +// count — closing a parent always re-reads its children live. Zero value is +// ready to use. +type openIssueSnapshots struct { + mu sync.RWMutex + sets map[string]map[int]struct{} +} + +func (s *openIssueSnapshots) store(repoFull string, open map[int]struct{}) { + if s == nil || repoFull == "" { + return + } + s.mu.Lock() + defer s.mu.Unlock() + if s.sets == nil { + s.sets = map[string]map[int]struct{}{} + } + s.sets[strings.ToLower(repoFull)] = open +} + +// isOpen reports whether repoFull#n was open in the newest snapshot of that +// repo; known is false when the repo has not been enumerated yet. +func (s *openIssueSnapshots) isOpen(repoFull string, n int) (open, known bool) { + if s == nil { + return false, false + } + s.mu.RLock() + defer s.mu.RUnlock() + set, ok := s.sets[strings.ToLower(repoFull)] + if !ok { + return false, false + } + _, open = set[n] + return open, true +} + +// crossRepoOpenFunc resolves whether a child in another repo is open; known +// is false when nothing is known about that repo. +type crossRepoOpenFunc func(repoFull string, n int) (open, known bool) + +// crossRepoParent returns the parent's "owner/name" when req names a parent +// in a different repo from the child, or "" for a same-repo parent. +func (c *Client) crossRepoParent(req IssueRequest) string { + parentRepo := strings.TrimSpace(req.ParentRepo) + if req.Parent <= 0 || parentRepo == "" { + return "" + } + pOwner, pName := c.splitRepo(parentRepo) + cOwner, cName := c.splitRepo(strings.TrimSpace(req.Repo)) + if strings.EqualFold(pOwner+"/"+pName, cOwner+"/"+cName) { + return "" + } + return pOwner + "/" + pName +} + +// resolveChildIssueRepo settles which repo a child with a cross-repo parent +// is filed in (hivecommons/hive#18680). The child goes where its work lives +// (req.Repo) when this hive manages that repo; otherwise it falls back to the +// parent's repo, so the relay never files in an unmanaged repo. A ParentRepo +// that names the child's own repo is cleared. Returns the requested repo when +// it fell back, "" otherwise. The caller has already refused an unmanaged +// parent repo. +func (c *Client) resolveChildIssueRepo(req *IssueRequest) string { + if req == nil || req.Parent <= 0 || strings.TrimSpace(req.ParentRepo) == "" { + return "" + } + if c.crossRepoParent(*req) == "" { + req.ParentRepo = "" + return "" + } + if in, configured := c.repoInConfiguredSet(req.Repo); configured && !in { + from := req.Repo + req.Repo = strings.TrimSpace(req.ParentRepo) + req.ParentRepo = "" + return from + } + return "" +} + +// configuredRepoSpelling returns the configured project.repos entry repo +// names (compared as full owner/name, case-insensitively), or repo +// unchanged when it is not configured. +func (c *Client) configuredRepoSpelling(repo string) string { + repo = strings.TrimSpace(repo) + if c == nil || repo == "" { + return repo + } + owner, name := c.splitRepo(repo) + want := strings.ToLower(owner + "/" + name) + for _, r := range c.getRepos() { + o, n := c.splitRepo(strings.TrimSpace(r)) + if strings.ToLower(o+"/"+n) == want { + return r + } + } + return repo +} + +// canonicalizeClaimRepos keys every claim by the configured spelling of +// its issue's repo (hivecommons/hive#18676). A PR names an issue in another +// repo in full `owner/repo#N` form, so its claim carried "owner/repo" while +// enumeration keys issues by the configured spelling — the bare repo name +// on a default config. The ledger lookup then missed the claim entirely: a +// quorum-companion PR with `Refs hivecommons/hive#18660` left #18660 +// Actionable with no PR link while the PR sat at needs-decision. +func (c *Client) canonicalizeClaimRepos(claims []IssueClaim) { + for i := range claims { + claims[i].Repo = c.configuredRepoSpelling(claims[i].Repo) + } +} diff --git a/src/pkg/github/cross_repo_children_test.go b/src/pkg/github/cross_repo_children_test.go new file mode 100644 index 0000000000..ee41494513 --- /dev/null +++ b/src/pkg/github/cross_repo_children_test.go @@ -0,0 +1,266 @@ +package github + +import ( + "context" + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + gh "github.com/google/go-github/v72/github" +) + +// crossRepoIssueServer records which repo each issue create and sub-issue +// link went to, and the created body (hivecommons/hive#18680). +type crossRepoIssueServer struct { + mu sync.Mutex + creates []string // request paths of POST .../issues + bodies []string + subIssues []string // request paths of POST .../sub_issues +} + +func newCrossRepoIssueServer(t *testing.T) (*crossRepoIssueServer, *httptest.Server) { + t.Helper() + s := &crossRepoIssueServer{} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/sub_issues"): + s.mu.Lock() + s.subIssues = append(s.subIssues, r.URL.Path) + s.mu.Unlock() + _, _ = io.WriteString(w, `{"id":1}`) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/labels/"): + _, _ = io.WriteString(w, `{"name":"x"}`) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/labels"): + _, _ = io.WriteString(w, `{"name":"x"}`) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/issues"): + _, _ = io.WriteString(w, `[]`) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/issues"): + var payload struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&payload) + s.mu.Lock() + s.creates = append(s.creates, r.URL.Path) + s.bodies = append(s.bodies, payload.Body) + s.mu.Unlock() + _, _ = io.WriteString(w, `{"id":990099,"number":99,"html_url":"https://github.example/o/x/issues/99"}`) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + return s, srv +} + +// A plan whose work lives in another managed repo files the child THERE, +// links it to the parent in full cross-repo form, and records the +// cross-repo relation in the split-parent ledger (hivecommons/hive#18680). +func TestIssueRequestWatcher_CrossRepoChildFiledInManagedRepo(t *testing.T) { + fake, srv := newCrossRepoIssueServer(t) + c := issueTestClient(t, srv.URL) + c.SetRepos([]string{"r", "companion"}) + dir := withIssueDir(t) + + reqPath, err := WriteIssueRequest(dir, IssueRequest{ + Repo: "o/companion", Title: "[scanner] Windows capture", Agent: "scanner", + Body: "Part of #18596\n\nAdd the Windows capture module; keeps the #18596 checklist actionable.", + Parent: 18596, ParentRepo: "o/r", + }) + if err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + + fake.mu.Lock() + defer fake.mu.Unlock() + if len(fake.creates) != 1 || fake.creates[0] != "/repos/o/companion/issues" { + t.Fatalf("child must be created in the managed repo its work lives in: %v", fake.creates) + } + body := fake.bodies[0] + if !strings.Contains(body, "Part of o/r#18596") || strings.Contains(body, "Part of #18596") { + t.Fatalf("parent link must be the full cross-repo form: %q", body) + } + if !strings.Contains(body, "the o/r#18596 checklist") { + t.Fatalf("bare mentions of the parent must be qualified: %q", body) + } + if !strings.Contains(body, ParentMarkerFor("o/r", 18596)) || strings.Contains(body, ParentMarker(18596)) { + t.Fatalf("marker must carry owner/repo#N: %q", body) + } + if repo, n := ParseParentMarkerRef(body); repo != "o/r" || n != 18596 { + t.Fatalf("ParseParentMarkerRef = %q %d", repo, n) + } + if ParseParentMarker(body) != 0 { + t.Fatal("a cross-repo marker must not read as a same-repo parent") + } + if len(fake.subIssues) != 1 || fake.subIssues[0] != "/repos/o/r/issues/18596/sub_issues" { + t.Fatalf("sub-issue link must target the parent's repo: %v", fake.subIssues) + } + resp := readIssueResult(t, reqPath) + if !resp.OK || !resp.ParentLinked || resp.RepoFallbackFrom != "" { + t.Fatalf("result = %+v", resp) + } + ledger, err := LoadSplitParents(SplitParentsPath) + if err != nil { + t.Fatal(err) + } + if got := splitChildrenOf(ledger, "o/r", 18596); len(got) != 1 || got[0] != (ChildRef{Repo: "o/companion", Number: 99}) { + t.Fatalf("ledger children of o/r#18596 = %+v (%+v)", got, ledger) + } + if splitParentOf(ledger, "o/companion", 99) != 0 { + t.Fatal("a cross-repo parent must not be resolved inside the child's repo") + } +} + +// A child whose requested repo the hive does not manage falls back to the +// parent's repo — never filed in an unmanaged repo — with the plain +// same-repo link. +func TestIssueRequestWatcher_CrossRepoChildUnmanagedRepoFallsBack(t *testing.T) { + fake, srv := newCrossRepoIssueServer(t) + c := issueTestClient(t, srv.URL) + c.SetRepos([]string{"r"}) + dir := withIssueDir(t) + + reqPath, err := WriteIssueRequest(dir, IssueRequest{ + Repo: "elsewhere/unmanaged", Title: "[scanner] Linux capture", Agent: "scanner", + Body: "Part of #18596\n\nLinux capture.", Parent: 18596, ParentRepo: "o/r", + }) + if err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + + fake.mu.Lock() + defer fake.mu.Unlock() + if len(fake.creates) != 1 || fake.creates[0] != "/repos/o/r/issues" { + t.Fatalf("child must fall back to the parent's repo: %v", fake.creates) + } + body := fake.bodies[0] + if !strings.Contains(body, "Part of #18596") || !strings.Contains(body, ParentMarker(18596)) { + t.Fatalf("same-repo child keeps the bare link: %q", body) + } + if len(fake.subIssues) != 1 || fake.subIssues[0] != "/repos/o/r/issues/18596/sub_issues" { + t.Fatalf("sub-issue link = %v", fake.subIssues) + } + resp := readIssueResult(t, reqPath) + if !resp.OK || resp.RepoFallbackFrom != "elsewhere/unmanaged" { + t.Fatalf("result must report the fallback: %+v", resp) + } +} + +// A parent in an unmanaged repo is refused outright: the relay never links +// a child under an issue the hive does not manage. +func TestIssueRequestWatcher_CrossRepoChildUnmanagedParentRefused(t *testing.T) { + fake, srv := newCrossRepoIssueServer(t) + c := issueTestClient(t, srv.URL) + c.SetRepos([]string{"r"}) + dir := withIssueDir(t) + + if _, err := WriteIssueRequest(dir, IssueRequest{ + Repo: "o/r", Title: "[scanner] child", Agent: "scanner", Body: "work", + Parent: 5, ParentRepo: "elsewhere/unmanaged", + }); err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + fake.mu.Lock() + defer fake.mu.Unlock() + if len(fake.creates) != 0 || len(fake.subIssues) != 0 { + t.Fatalf("nothing may be written for an unmanaged parent: creates=%v links=%v", fake.creates, fake.subIssues) + } +} + +func TestWithCrossRepoParentReference(t *testing.T) { + if got := withCrossRepoParentReference("Part of #7", "", 7); got != "Part of #7" { + t.Fatalf("same-repo body must be unchanged: %q", got) + } + got := withCrossRepoParentReference("Work.\n\nSee #70 and #7, also o/r#7.", "o/r", 7) + if !strings.HasPrefix(got, "Part of o/r#7\n\n") || !strings.Contains(got, "See #70 and o/r#7, also o/r#7.") { + t.Fatalf("got %q", got) + } + if again := withCrossRepoParentReference(got, "o/r", 7); again != got { + t.Fatalf("must be idempotent: %q", again) + } + // A bare marker survives untouched for withParentMarker to replace. + body := withCrossRepoParentReference("x\n\n"+ParentMarker(7), "o/r", 7) + if !strings.Contains(body, ParentMarker(7)) { + t.Fatalf("marker rewritten here: %q", body) + } + if out := withParentMarker(body, "o/r", 7); strings.Contains(out, ParentMarker(7)) || !strings.Contains(out, ParentMarkerFor("o/r", 7)) { + t.Fatalf("bare marker must become the full form: %q", out) + } +} + +// The parent's tracker progress counts children in any managed repo: a +// closed cross-repo child is Completed, an open one is not, and one in a +// repo not yet enumerated counts as open (hivecommons/hive#18680). +func TestChildrenOf_CountsCrossRepoChildren(t *testing.T) { + ledger := map[string]SplitParent{ + splitParentKey("o/companion", 3): {Parent: 1, ParentRepo: "o/r"}, + splitParentKey("o/companion", 4): {Parent: 1, ParentRepo: "o/r"}, + splitParentKey("o/r", 5): {Parent: 1}, + splitParentKey("o/other", 6): {Parent: 1, ParentRepo: "o/r"}, + } + open := map[int]*gh.Issue{1: {Number: gh.Ptr(1)}, 5: {Number: gh.Ptr(5)}} + var snaps openIssueSnapshots + snaps.store("o/companion", map[int]struct{}{4: {}}) + + got := childrenOf("o/r", 1, nil, ledger, open, snaps.isOpen) + // #3 closed (absent from companion's snapshot), #4 open, #5 open in the + // parent's repo, #6 in an unenumerated repo counts as open. + if got == nil || *got != (IssueChildren{Total: 4, Completed: 1}) { + t.Fatalf("children = %+v", got) + } + snaps.store("O/Other", map[int]struct{}{}) + if got := childrenOf("o/r", 1, nil, ledger, open, snaps.isOpen); got == nil || got.Completed != 2 { + t.Fatalf("children after o/other enumerated = %+v", got) + } + // The child's repo never mistakes the cross-repo parent number for one + // of its own issues. + if childrenOf("o/companion", 1, nil, ledger, map[int]*gh.Issue{}, snaps.isOpen) != nil { + t.Fatal("o/companion#1 has no children") + } + // A cross-repo marker in an open issue of the parent's repo that names + // another repo is not a child of this parent. + marked := map[int]*gh.Issue{ + 1: {Number: gh.Ptr(1)}, + 8: {Number: gh.Ptr(8), Body: gh.Ptr(ParentMarkerFor("o/elsewhere", 1))}, + 9: {Number: gh.Ptr(9), Body: gh.Ptr(ParentMarkerFor("O/R", 1))}, + } + if got := childrenOf("o/r", 1, nil, nil, marked, nil); got == nil || got.Total != 1 { + t.Fatalf("marker children = %+v", got) + } +} + +// Closing a decomposed parent re-reads cross-repo sub-issues from their own +// repo and names them, and the PR that closed them, in full form. +func TestDecomposedChildren_CrossRepoSubIssues(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/7/sub_issues" { + _, _ = io.WriteString(w, `[{"number":3,"state":"closed","repository_url":"https://api.github.com/repos/o/r"},`+ + `{"number":3,"state":"closed","repository_url":"https://api.github.com/repos/o/companion"}]`) + return + } + w.WriteHeader(http.StatusNotFound) + })) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"r", "companion"}, slog.New(slog.NewTextHandler(io.Discard, nil))) + children, open, ok, err := c.decomposedChildren(context.Background(), "o", "r", 7) + if err != nil || !ok || open != 0 { + t.Fatalf("children=%v open=%d ok=%v err=%v", children, open, ok, err) + } + want := []ChildRef{{Number: 3}, {Repo: "o/companion", Number: 3}} + if len(children) != 2 || children[0] != want[0] || children[1] != want[1] { + t.Fatalf("children = %+v, want %+v", children, want) + } + note := decomposedParentCloseComment(children, map[ChildRef]int{want[1]: 12}) + if !strings.Contains(note, "All children delivered: #3, o/companion#3 → PR o/companion#12.") { + t.Fatalf("note = %q", note) + } +} diff --git a/src/pkg/github/cross_repo_pr_link_test.go b/src/pkg/github/cross_repo_pr_link_test.go new file mode 100644 index 0000000000..ae35d63bd6 --- /dev/null +++ b/src/pkg/github/cross_repo_pr_link_test.go @@ -0,0 +1,114 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// A PR in another managed repo that says `Refs owner/repo#N` claims the +// issue under the issue's configured spelling, so the issue is staged as +// covered and mirrors the PR's stage (hivecommons/hive#18676: +// quorum-companion#3 `Refs hivecommons/hive#18660` at needs-decision while +// #18660 still read Actionable with no PR). +func TestCrossRepoRefsPRCoversIssueAndMirrorsPRStage(t *testing.T) { + c := NewClientForTest("http://127.0.0.1:1", "o", []string{"r", "companion"}, testLogger()) + ledger := NewClaimLedger(filepath.Join(t.TempDir(), "claims.json"), testLogger()) + got := c.RecordOpenedPRClaims(ledger, PROpenedDetail{ + Repo: "companion", Number: 3, URL: "https://github.com/o/companion/pull/3", + Title: "feat: Windows capture", Body: "Refs o/r#18660", Author: "hive[bot]", Head: "scanner/windows-capture", + }, HiveIdentity{AppLogin: "hive[bot]"}, time.Now()) + if len(got) != 1 || got[0].Repo != "r" || got[0].Issue != 18660 || got[0].PRRepo != "companion" || !got[0].Reference { + t.Fatalf("claims = %+v, want one reference claim keyed by the configured spelling r#18660", got) + } + if _, ok := ledger.Lookup("r", 18660); !ok { + t.Fatal("ledger lookup by the enumerated spelling must find the cross-repo claim") + } + + result := &ActionableResult{} + result.Issues = IssueResultFromItems([]Issue{{Repo: "r", Number: 18660, Title: "Windows capture", Labels: []string{stagelabels.Actionable.Label()}}}) + FilterClaimedIssues(result, ledger, nil, testLogger()) + ins := IssueStageInputs(result) + if len(ins) != 1 || ins[0].Claim == nil || ins[0].Claim.PRRepo != "companion" || ins[0].Claim.Decision != ClaimDecisionCoveredByOpenPR { + t.Fatalf("stage input = %+v", ins) + } + stage, _, reason := IssueStageFor(ins[0], func(repo string, n int) stagelabels.Stage { + if repo == "companion" && n == 3 { + return stagelabels.NeedsDecision + } + return stagelabels.None + }) + if stage != stagelabels.CoveredByPR || !strings.Contains(reason, stagelabels.NeedsDecision.String()) { + t.Fatalf("stage = %v (%s), want covered-by-pr mirroring the PR's needs-decision", stage, reason) + } +} + +// Same-repo and unconfigured repos keep their spelling. +func TestCanonicalizeClaimRepos(t *testing.T) { + c := NewClientForTest("http://127.0.0.1:1", "o", []string{"r", "x/full"}, testLogger()) + claims := []IssueClaim{{Repo: "O/R"}, {Repo: "r"}, {Repo: "x/full"}, {Repo: "other/r"}, {Repo: "full"}} + c.canonicalizeClaimRepos(claims) + want := []string{"r", "r", "x/full", "other/r", "full"} + for i, w := range want { + if claims[i].Repo != w { + t.Fatalf("claim %d repo = %q, want %q", i, claims[i].Repo, w) + } + } +} + +// The issue gets the stage-pr-link comment for a PR in another managed +// repo, naming it in full owner/repo#N form; the PR body is never touched. +func TestEnsureIssueStagePRLinkCrossRepoPR(t *testing.T) { + var lookups []string + state := "OPEN" + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Method != http.MethodPost || !strings.HasSuffix(r.URL.Path, "/graphql") { + t.Errorf("unexpected request %s %s (cross-repo PR bodies must never be edited)", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + return + } + var req struct { + Variables map[string]any `json:"variables"` + } + _ = json.NewDecoder(r.Body).Decode(&req) + lookups = append(lookups, req.Variables["owner"].(string)+"/"+req.Variables["name"].(string)) + _ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{ + "repository": map[string]any{"pullRequest": map[string]any{ + "state": state, "baseRefName": "main", "url": "https://github.com/o/companion/pull/3", + }}, + }}) + })) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"r", "companion"}, testLogger()) + c.SetAppBotLogin("hive[bot]") + claim := &IssueClaimContext{PRNumber: 3, PRRepo: "companion", PRURL: "https://github.com/o/companion/pull/3", PRAuthor: "hive[bot]", Reference: true} + + got, err := c.EnsureIssueStagePRLink(context.Background(), "r", 18660, claim) + if err != nil { + t.Fatalf("EnsureIssueStagePRLink: %v", err) + } + if len(lookups) != 1 || lookups[0] != "o/companion" { + t.Fatalf("PR must be looked up in its own repo: %v", lookups) + } + if got.EditedBody || got.Skipped != "cross-repo" { + t.Fatalf("result = %+v", got) + } + if !strings.HasPrefix(got.StageComment, "") || + !strings.Contains(got.StageComment, "PR o/companion#3 covers this issue: https://github.com/o/companion/pull/3") { + t.Fatalf("stage comment = %q", got.StageComment) + } + + state = "MERGED" + got, err = c.EnsureIssueStagePRLink(context.Background(), "r", 18660, claim) + if err != nil || got.StageComment != "" || got.Skipped != "pr-not-open" { + t.Fatalf("closed cross-repo PR: %+v %v", got, err) + } +} diff --git a/src/pkg/github/decomposed_parents.go b/src/pkg/github/decomposed_parents.go index 22d2edab94..0a3bba42b1 100644 --- a/src/pkg/github/decomposed_parents.go +++ b/src/pkg/github/decomposed_parents.go @@ -6,7 +6,6 @@ import ( "log/slog" "net/http" "regexp" - "sort" "strconv" "strings" @@ -73,82 +72,134 @@ func ParentMarker(n int) string { return parentMarkerPrefix + strconv.Itoa(n) + " -->" } -var parentMarkerRe = regexp.MustCompile(``) +// parentMarkerRe matches both marker forms: the bare same-repo +// `` and the cross-repo `` +// (hivecommons/hive#18680). Group 1 is the repo ("" for the bare form), +// group 2 the number. +var parentMarkerRe = regexp.MustCompile(``) -// ParseParentMarker returns the parent issue number a body's marker names, -// or 0 when there is none. +// ParseParentMarker returns the parent issue number a body's bare +// (same-repo) marker names, or 0 when there is none. A cross-repo marker +// names an issue in another repo, so it is not reported here; read it with +// ParseParentMarkerRef. func ParseParentMarker(body string) int { - m := parentMarkerRe.FindStringSubmatch(body) - if m == nil { + repo, n := ParseParentMarkerRef(body) + if repo != "" { return 0 } - n, _ := strconv.Atoi(m[1]) return n } // withParentMarker appends the parent marker to body unless one for the same -// parent is already present. -func withParentMarker(body string, parent int) string { - if parent <= 0 || ParseParentMarker(body) == parent { +// parent is already present. parentRepo is "" for a parent in the child's own +// repo, or the parent's "owner/name" for a cross-repo parent; a bare marker +// for the same number on a cross-repo child is replaced by the full form. +func withParentMarker(body, parentRepo string, parent int) string { + if parent <= 0 { return body } + parentRepo = strings.TrimSpace(parentRepo) + want := ParentMarkerFor(parentRepo, parent) + if repo, n := ParseParentMarkerRef(body); n == parent { + if strings.EqualFold(repo, parentRepo) { + return body + } + if repo == "" && parentRepo != "" { + return strings.Replace(body, parentMarkerRe.FindString(body), want, 1) + } + } body = strings.TrimRight(body, "\n") if body == "" { - return ParentMarker(parent) + return want } - return body + "\n\n" + ParentMarker(parent) + return body + "\n\n" + want } -// splitChildrenOf inverts the ledger: the child numbers recorded under -// repo#parent, sorted. -func splitChildrenOf(children map[string]SplitParent, repo string, parent int) []int { +// splitChildrenOf inverts the ledger: the children recorded under +// parentRepo#parent, sorted. parentRepo is matched as a full "owner/name" +// (case-insensitive); a child filed in another repo (SplitParent.ParentRepo +// set, hivecommons/hive#18680) is returned with its own repo, a same-repo +// child with Repo "". +func splitChildrenOf(children map[string]SplitParent, parentRepo string, parent int) []ChildRef { if len(children) == 0 || parent <= 0 { return nil } - prefix := strings.ToLower(strings.TrimSpace(repo)) + "#" - var out []int + want := strings.ToLower(strings.TrimSpace(parentRepo)) + var out []ChildRef for key, sp := range children { - if sp.Parent != parent || !strings.HasPrefix(key, prefix) { + if sp.Parent != parent { continue } - n, err := strconv.Atoi(strings.TrimPrefix(key, prefix)) - if err != nil || n <= 0 || n == parent { + i := strings.LastIndex(key, "#") + if i <= 0 { continue } - out = append(out, n) + childRepo := strings.ToLower(key[:i]) + n, err := strconv.Atoi(key[i+1:]) + if err != nil || n <= 0 { + continue + } + ownerRepo := childRepo + if sp.ParentRepo != "" { + ownerRepo = strings.ToLower(strings.TrimSpace(sp.ParentRepo)) + } + if ownerRepo != want { + continue + } + ref := ChildRef{Number: n} + if childRepo != want { + ref.Repo = childRepo + } else if n == parent { + continue + } + out = append(out, ref) } - sort.Ints(out) + sortChildRefs(out) return out } // childrenOf resolves a parent's tracked children from what enumeration -// already has in hand: GitHub's sub-issue summary first; failing that, the -// ledger and body markers inverted against the open snapshot. In the -// fallback a child absent from the open snapshot is counted as closed (the -// snapshot is every open issue in the repo), and a body-marked child that -// is open counts as open — the marker cannot name closed children, so a -// marker-only parent is exempt from stalling but is never auto-closed on -// that evidence alone (closeDecomposedParent re-reads the children). -func childrenOf(repo string, parent int, summaries map[int]IssueChildren, ledger map[string]SplitParent, open map[int]*gh.Issue) *IssueChildren { +// already has in hand: GitHub's sub-issue summary first (it counts +// sub-issues in any repo); failing that, the ledger and body markers +// inverted against the open snapshot. repo is the parent's repo as +// "owner/name". In the fallback a same-repo child absent from the open +// snapshot is counted as closed (the snapshot is every open issue in the +// repo), and a body-marked child that is open counts as open — the marker +// cannot name closed children, so a marker-only parent is exempt from +// stalling but is never auto-closed on that evidence alone +// (closeDecomposedParent re-reads the children). A child in another repo +// (hivecommons/hive#18680) is resolved through crossOpen, that repo's +// newest open snapshot; while nothing is known about that repo it counts as +// open, so a parent is never shown done on missing evidence. +func childrenOf(repo string, parent int, summaries map[int]IssueChildren, ledger map[string]SplitParent, open map[int]*gh.Issue, crossOpen crossRepoOpenFunc) *IssueChildren { if s, ok := summaries[parent]; ok && s.Total > 0 { s := s return &s } - seen := map[int]bool{} - for _, n := range splitChildrenOf(ledger, repo, parent) { - seen[n] = true + seen := map[ChildRef]bool{} + for _, ref := range splitChildrenOf(ledger, repo, parent) { + seen[ref] = true } for n, issue := range open { - if n != parent && issue != nil && ParseParentMarker(issue.GetBody()) == parent { - seen[n] = true + if n != parent && issue != nil && markerNamesParent(issue.GetBody(), repo, parent) { + seen[ChildRef{Number: n}] = true } } if len(seen) == 0 { return nil } out := &IssueChildren{Total: len(seen)} - for n := range seen { - if _, isOpen := open[n]; !isOpen { + for ref := range seen { + if ref.Repo == "" { + if _, isOpen := open[ref.Number]; !isOpen { + out.Completed++ + } + continue + } + if crossOpen == nil { + continue + } + if isOpen, known := crossOpen(ref.Repo, ref.Number); known && !isOpen { out.Completed++ } } @@ -157,21 +208,40 @@ func childrenOf(repo string, parent int, summaries map[int]IssueChildren, ledger // annotateIssueChildren sets Children on every enumerated row that is a // decomposed parent. -func annotateIssueChildren(repo string, summaries map[int]IssueChildren, ledger map[string]SplitParent, open map[int]*gh.Issue, actionable []Issue, details []RepoWorkIssue) { +func annotateIssueChildren(repo string, summaries map[int]IssueChildren, ledger map[string]SplitParent, open map[int]*gh.Issue, crossOpen crossRepoOpenFunc, actionable []Issue, details []RepoWorkIssue) { for i := range actionable { - actionable[i].Children = childrenOf(repo, actionable[i].Number, summaries, ledger, open) + actionable[i].Children = childrenOf(repo, actionable[i].Number, summaries, ledger, open, crossOpen) } for i := range details { if details[i].Number > 0 { - details[i].Children = childrenOf(repo, details[i].Number, summaries, ledger, open) + details[i].Children = childrenOf(repo, details[i].Number, summaries, ledger, open, crossOpen) } } } // subIssueRef is one child as GET .../issues/{parent}/sub_issues reports it. +// RepositoryURL ("https://api.github.com/repos/owner/name") names the +// child's repo, which differs from the parent's for a cross-repo sub-issue +// (hivecommons/hive#18680); empty means the parent's repo. type subIssueRef struct { - Number int `json:"number"` - State string `json:"state"` + Number int `json:"number"` + State string `json:"state"` + RepositoryURL string `json:"repository_url,omitempty"` +} + +// repo returns the child's lowercased "owner/name" from RepositoryURL, or +// "" when the payload did not carry one. +func (r subIssueRef) repo() string { + u := strings.TrimRight(strings.TrimSpace(r.RepositoryURL), "/") + i := strings.LastIndex(u, "/repos/") + if i < 0 { + return "" + } + full := u[i+len("/repos/"):] + if strings.Count(full, "/") != 1 { + return "" + } + return strings.ToLower(full) } // listSubIssues reads a parent's native sub-issue list. go-github v72 has no @@ -205,36 +275,46 @@ func (c *Client) listSubIssues(ctx context.Context, owner, repoName string, pare // decomposedChildren re-reads a parent's children live, right before a // close: the native sub-issue list when it has entries, otherwise each -// ledger-recorded child by number. Returns the sorted child numbers and -// how many are open; ok is false when nothing could be read. -func (c *Client) decomposedChildren(ctx context.Context, owner, repoName string, parent int) (children []int, open int, ok bool, err error) { +// ledger-recorded child, read from its own repo. Returns the sorted +// children and how many are open; ok is false when nothing could be read. +// A child in another repo (hivecommons/hive#18680) carries that repo. +func (c *Client) decomposedChildren(ctx context.Context, owner, repoName string, parent int) (children []ChildRef, open int, ok bool, err error) { + parentFull := strings.ToLower(owner + "/" + repoName) refs, err := c.listSubIssues(ctx, owner, repoName, parent) if err != nil { return nil, 0, false, fmt.Errorf("listing sub-issues of %s/%s#%d: %w", owner, repoName, parent, err) } if len(refs) > 0 { for _, r := range refs { - if r.Number <= 0 || r.Number == parent { + ref := ChildRef{Number: r.Number} + if childRepo := r.repo(); childRepo != "" && childRepo != parentFull { + ref.Repo = childRepo + } + if r.Number <= 0 || (ref.Repo == "" && r.Number == parent) { continue } - children = append(children, r.Number) + children = append(children, ref) if !strings.EqualFold(r.State, "closed") { open++ } } - sort.Ints(children) + sortChildRefs(children) return children, open, len(children) > 0, nil } ledger, lerr := LoadSplitParents("") if lerr != nil { return nil, 0, false, nil } - for _, n := range splitChildrenOf(ledger, owner+"/"+repoName, parent) { - child, _, gerr := c.client.Issues.Get(ctx, owner, repoName, n) + for _, ref := range splitChildrenOf(ledger, parentFull, parent) { + childOwner, childName := owner, repoName + if ref.Repo != "" { + childOwner, childName = c.splitRepo(ref.Repo) + } + child, _, gerr := c.client.Issues.Get(ctx, childOwner, childName, ref.Number) if gerr != nil { - return nil, 0, false, fmt.Errorf("reading child %s/%s#%d: %w", owner, repoName, n, gerr) + return nil, 0, false, fmt.Errorf("reading child %s/%s#%d: %w", childOwner, childName, ref.Number, gerr) } - children = append(children, n) + children = append(children, ref) if !strings.EqualFold(child.GetState(), "closed") { open++ } @@ -247,14 +327,16 @@ func (c *Client) decomposedChildren(ctx context.Context, owner, repoName string, const parentAutoCloseMarker = "" // decomposedParentCloseComment is the one comment the sweep leaves on a -// parent it closes. closers maps child → merged PR number (0 = unknown). -func decomposedParentCloseComment(children []int, closers map[int]int) string { +// parent it closes. closers maps child → merged PR number (0 = unknown); a +// cross-repo child's PR lives in the child's repo, so both are written in +// full owner/repo#N form. +func decomposedParentCloseComment(children []ChildRef, closers map[ChildRef]int) string { parts := make([]string, 0, len(children)) - for _, n := range children { - if pr := closers[n]; pr > 0 { - parts = append(parts, fmt.Sprintf("#%d → PR #%d", n, pr)) + for _, ref := range children { + if pr := closers[ref]; pr > 0 { + parts = append(parts, fmt.Sprintf("%s → PR %s", ref, ChildRef{Repo: ref.Repo, Number: pr})) } else { - parts = append(parts, fmt.Sprintf("#%d", n)) + parts = append(parts, ref.String()) } } return parentAutoCloseMarker + "\nAll children delivered: " + strings.Join(parts, ", ") + ".\n\nEvery tracked child of this issue is closed, so the hive is closing it as completed. Reopen it if something it asked for is still missing." @@ -279,10 +361,14 @@ func (c *Client) closeDecomposedParent(ctx context.Context, in IssueStageInput, if !ok || open > 0 { return false } - closers := map[int]int{} - for _, n := range children { - if closer, found := c.closedByPullRequestReference(ctx, owner, name, n, 0); found { - closers[n] = closer.Number + closers := map[ChildRef]int{} + for _, ref := range children { + childOwner, childName := owner, name + if ref.Repo != "" { + childOwner, childName = c.splitRepo(ref.Repo) + } + if closer, found := c.closedByPullRequestReference(ctx, childOwner, childName, ref.Number, 0); found { + closers[ref] = closer.Number } } if _, err := stagelabels.Enforce(ctx, c.StageLabeler(), stagelabels.IssueItem, owner, name, in.Number, in.Labels, stagelabels.Desired{}); err != nil && logger != nil { diff --git a/src/pkg/github/decomposed_parents_test.go b/src/pkg/github/decomposed_parents_test.go index a4b2e34f43..f1e02a8842 100644 --- a/src/pkg/github/decomposed_parents_test.go +++ b/src/pkg/github/decomposed_parents_test.go @@ -13,17 +13,17 @@ import ( ) func TestParentMarker_RoundTrip(t *testing.T) { - body := withParentMarker("Child body.\n", 12361) + body := withParentMarker("Child body.\n", "", 12361) if !strings.HasSuffix(body, ParentMarker(12361)) || !strings.Contains(body, "Child body.") { t.Fatalf("body = %q", body) } if ParseParentMarker(body) != 12361 { t.Fatalf("parse = %d", ParseParentMarker(body)) } - if again := withParentMarker(body, 12361); again != body { + if again := withParentMarker(body, "", 12361); again != body { t.Fatalf("marker must not repeat: %q", again) } - if withParentMarker("x", 0) != "x" || ParseParentMarker("no marker") != 0 { + if withParentMarker("x", "", 0) != "x" || ParseParentMarker("no marker") != 0 { t.Fatal("zero parent / absent marker must be no-ops") } if ParseParentMarker("") != 42 { @@ -45,28 +45,28 @@ func TestChildrenOf_SummaryThenLedgerAndMarkerFallback(t *testing.T) { } summaries := map[int]IssueChildren{1: {Total: 4, Completed: 4}} - if got := childrenOf("acme/widgets", 1, summaries, ledger, open); got == nil || *got != (IssueChildren{Total: 4, Completed: 4}) { + if got := childrenOf("acme/widgets", 1, summaries, ledger, open, nil); got == nil || *got != (IssueChildren{Total: 4, Completed: 4}) { t.Fatalf("summary must win: %+v", got) } // No summary: ledger children 3 and 4 (closed: absent from the snapshot) // plus body-marked open child 2; the other repo's #6 is ignored. - got := childrenOf("acme/widgets", 1, nil, ledger, open) + got := childrenOf("acme/widgets", 1, nil, ledger, open, nil) if got == nil || *got != (IssueChildren{Total: 3, Completed: 2}) { t.Fatalf("fallback = %+v", got) } if got.Open() != 1 || got.AllClosed() { t.Fatalf("open=%d allClosed=%v", got.Open(), got.AllClosed()) } - if childrenOf("acme/widgets", 2, nil, ledger, open) != nil { + if childrenOf("acme/widgets", 2, nil, ledger, open, nil) != nil { t.Fatal("a leaf has no children") } - if got := splitChildrenOf(ledger, "ACME/Widgets", 1); len(got) != 2 || got[0] != 3 || got[1] != 4 { + if got := splitChildrenOf(ledger, "ACME/Widgets", 1); len(got) != 2 || got[0] != (ChildRef{Number: 3}) || got[1] != (ChildRef{Number: 4}) { t.Fatalf("splitChildrenOf = %v", got) } } func TestDecomposedParentCloseComment(t *testing.T) { - got := decomposedParentCloseComment([]int{3, 4, 5}, map[int]int{3: 30, 5: 50}) + got := decomposedParentCloseComment([]ChildRef{{Number: 3}, {Number: 4}, {Number: 5}}, map[ChildRef]int{{Number: 3}: 30, {Number: 5}: 50}) if !strings.HasPrefix(got, parentAutoCloseMarker) { t.Fatalf("missing marker: %q", got) } @@ -234,7 +234,7 @@ func TestAnnotateIssueChildren_SetsParentsOnly(t *testing.T) { actionable := []Issue{{Repo: "acme/widgets", Number: 1}, {Repo: "acme/widgets", Number: 2}} details := []RepoWorkIssue{{Repo: "acme/widgets", Number: 3}, {Repo: "acme/widgets"}} summaries := map[int]IssueChildren{1: {Total: 2, Completed: 1}, 3: {Total: 1, Completed: 1}} - annotateIssueChildren("acme/widgets", summaries, nil, nil, actionable, details) + annotateIssueChildren("acme/widgets", summaries, nil, nil, nil, actionable, details) if actionable[0].Children == nil || actionable[0].Children.Total != 2 || actionable[1].Children != nil { t.Fatalf("actionable = %+v", actionable) } diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 30d57b242a..6740e21e7c 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -123,6 +123,15 @@ type IssueRequest struct { // progress bar instead of only a "Part of #N" line in the body // (hivecommons/hive#9435). A failure to link does not fail the create. Parent int `json:"parent,omitempty"` // issue only + // ParentRepo is the repo Parent lives in when it is not Repo + // (hivecommons/hive#18680): a tracker in one managed repo whose child's + // work lives in another. The child is filed in Repo — where its work + // lives — and linked back in full cross-repo form (`Part of + // owner/repo#N`, ``, and a native + // cross-repo sub-issue link). Both repos must be managed by this hive; + // when Repo is not, the child falls back to ParentRepo, so a child is + // never filed in an unmanaged repo. Empty means Repo. + ParentRepo string `json:"parent_repo,omitempty"` // issue only // BlockedBy lists issue numbers in the same repo that must close before // this one is ready. On an "issue" request the watcher records each as a // GitHub "blocked by" dependency of the new issue after creating it @@ -214,6 +223,10 @@ type IssueResponse struct { // when a requested link did not succeed; the issue is still created OK. ParentLinked bool `json:"parent_linked,omitempty"` ParentLinkError string `json:"parent_link_error,omitempty"` + // RepoFallbackFrom names the repo a cross-repo child asked to be filed + // in when the hive does not manage it (hivecommons/hive#18680): the + // child was filed in the parent's repo instead, which Number/URL name. + RepoFallbackFrom string `json:"repo_fallback_from,omitempty"` // BlockedByLinked lists the req.BlockedBy numbers that were recorded as // GitHub "blocked by" dependencies of the created issue; BlockedByErrors // names each one that could not be, with the reason (#9839). @@ -515,6 +528,23 @@ func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src slog.String("path", path), slog.String("reason", shapeErr)) return } + // Cross-repo decomposition (hivecommons/hive#18680): resolve which + // managed repo the child is filed in before the repository gate, so an + // unmanaged child repo falls back to the parent's instead of being + // refused, and an unmanaged parent repo is refused like any other. + var repoFallbackFrom string + if kind == "issue" && req.Parent > 0 && strings.TrimSpace(req.ParentRepo) != "" { + if err := c.validateIssueRequestRepo(req.ParentRepo); err != nil { + c.quarantineFailedIssueRequest(path, req, kind, "parent repository refused", IssueResponse{Error: err.Error()}, nowFn) + return + } + repoFallbackFrom = c.resolveChildIssueRepo(&req) + if repoFallbackFrom != "" { + c.logger.Info("issue-request watcher: child repo is not managed by this hive; filing in the parent's repo", + slog.String("requested_repo", repoFallbackFrom), slog.String("repo", req.Repo), + slog.Int("parent", req.Parent), slog.String("agent", req.Agent)) + } + } // The repository gate: an impossible, placeholder or out-of-scope repo // can never be written to, so it is quarantined now rather than retried // — still before authorization and before any GitHub call. @@ -603,8 +633,12 @@ func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src if kind == "issue" && req.Parent > 0 { // Machine-readable child → parent marker (hivecommons/hive#12857): // the sub-issue link below is best-effort and the ledger is - // spoke-local, so the body carries the relation too. - body = withParentMarker(body, req.Parent) + // spoke-local, so the body carries the relation too. A cross-repo + // child names its parent in full owner/repo#N form, in the marker + // and in the "Part of" line (hivecommons/hive#18680). + parentRepo := c.crossRepoParent(req) + body = withCrossRepoParentReference(body, parentRepo, req.Parent) + body = withParentMarker(body, parentRepo, req.Parent) } if kind == "issue" { // An empty dedupe checklist is not an issue (hivecommons/hive#15381): @@ -648,7 +682,7 @@ func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src body = AppendTrailer(body, meta) } - resp := IssueResponse{At: nowFn().UTC().Format(time.RFC3339)} + resp := IssueResponse{At: nowFn().UTC().Format(time.RFC3339), RepoFallbackFrom: repoFallbackFrom} // #17298: a request that starts work on an issue (a claim, or a comment // announcing a start — the kick path for epic-driven work) is checked // against the same claim-target gate as hive-open-pr before any write, @@ -941,7 +975,12 @@ func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src // error, …) is logged and surfaced in the result but never turns // a successful create into a failure or a retry. if req.Parent > 0 && !res.Consolidated && res.ID > 0 { - if linkErr := c.AddSubIssue(ctx, req.Repo, req.Parent, res.ID); linkErr != nil { + parentRepo := c.crossRepoParent(req) + linkRepo := req.Repo + if parentRepo != "" { + linkRepo = parentRepo + } + if linkErr := c.AddSubIssue(ctx, linkRepo, req.Parent, res.ID); linkErr != nil { resp.ParentLinkError = linkErr.Error() c.logger.Warn("issue-request watcher: sub-issue link failed, issue still created", slog.String("repo", req.Repo), slog.Int("number", res.Number), @@ -952,8 +991,8 @@ func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src // child may inherit the parent's acknowledgement. Only // relay-made links are recorded; a sub-issue link anyone // adds later in the GitHub UI confers nothing. - linkOwner, linkRepo := c.splitRepo(req.Repo) - if recErr := RecordSplitParent("", linkOwner+"/"+linkRepo, res.Number, req.Parent, req.Agent); recErr != nil { + childOwner, childRepo := c.splitRepo(req.Repo) + if recErr := RecordSplitParentRef("", childOwner+"/"+childRepo, res.Number, parentRepo, req.Parent, req.Agent); recErr != nil { c.logger.Warn("issue-request watcher: sub-issue linked but split-parent ledger write failed; child will not inherit acknowledgement", slog.String("repo", req.Repo), slog.Int("number", res.Number), slog.Int("parent", req.Parent), slog.String("error", recErr.Error())) diff --git a/src/pkg/github/pr_issue_link.go b/src/pkg/github/pr_issue_link.go index 4c87edfb97..f613da2963 100644 --- a/src/pkg/github/pr_issue_link.go +++ b/src/pkg/github/pr_issue_link.go @@ -258,9 +258,12 @@ func (c *Client) EnsureIssueStagePRLink(ctx context.Context, repo string, issueN prRepo = owner + "/" + name } prOwner, prName := c.splitRepo(prRepo) - if prOwner == "" || prName == "" || !strings.EqualFold(prOwner+"/"+prName, owner+"/"+name) { + if prOwner == "" || prName == "" { return IssueStagePRLinkResult{Skipped: "cross-repo"}, nil } + if !strings.EqualFold(prOwner+"/"+prName, owner+"/"+name) { + return c.crossRepoIssueStagePRLink(ctx, prOwner, prName, owner+"/"+name, issueNumber, claim) + } var out struct { Repository struct { @@ -396,6 +399,58 @@ func rewriteRefsIssueLinkToClosing(body, repo string, issue int) (string, bool) return out, true } +// crossRepoIssueStagePRLink is EnsureIssueStagePRLink for a PR that lives in +// another managed repo from the issue it references (hivecommons/hive#18676: +// hivecommons/quorum-companion#3 with `Refs hivecommons/hive#18660`). The +// PR body is never edited — GitHub draws no issue-list PR icon across repos +// for a non-closing reference, and a closing keyword in another repo is the +// author's call — so while the PR is open the result always asks the caller +// for the stage-entry comment, which names the PR in full owner/repo#N form. +func (c *Client) crossRepoIssueStagePRLink(ctx context.Context, prOwner, prName, issueRepo string, issueNumber int, claim *IssueClaimContext) (IssueStagePRLinkResult, error) { + var out struct { + Repository struct { + PullRequest struct { + State string `json:"state"` + BaseRefName string `json:"baseRefName"` + URL string `json:"url"` + } `json:"pullRequest"` + } `json:"repository"` + } + query := `query($owner:String!, $name:String!, $pr:Int!) { + repository(owner:$owner, name:$name) { + pullRequest(number:$pr) { state baseRefName url } + } +}` + vars := map[string]any{"owner": prOwner, "name": prName, "pr": claim.PRNumber} + if err := c.graphQL(WithRESTCaller(ctx, "hive:issue_stage_pr_link"), query, vars, &out); err != nil { + return IssueStagePRLinkResult{}, fmt.Errorf("EnsureIssueStagePRLink %s#%d->%s/%s#%d lookup: %w", issueRepo, issueNumber, prOwner, prName, claim.PRNumber, err) + } + pr := out.Repository.PullRequest + res := IssueStagePRLinkResult{Base: strings.TrimSpace(pr.BaseRefName)} + if !strings.EqualFold(pr.State, "OPEN") { + res.Skipped = "pr-not-open" + return res, nil + } + prURL := strings.TrimSpace(pr.URL) + if prURL == "" { + prURL = strings.TrimSpace(claim.PRURL) + } + if prURL == "" { + prURL = fmt.Sprintf("https://github.com/%s/%s/pull/%d", prOwner, prName, claim.PRNumber) + } + res.Skipped = "cross-repo" + res.StageComment = crossRepoIssueStagePRLinkComment(prOwner+"/"+prName, prURL, claim.PRNumber) + return res, nil +} + +// crossRepoIssueStagePRLinkComment is the stage-entry comment for a PR in +// another repo. The marker carries repo= so a reader can tell +// owner/repo#N from a same-repo PR of the same number. +func crossRepoIssueStagePRLinkComment(prRepo, prURL string, prNumber int) string { + ref := fmt.Sprintf("%s#%d", prRepo, prNumber) + return fmt.Sprintf("\nPR %s covers this issue: %s\n\nThe PR lives in another repo this hive manages, so GitHub shows no PR icon for it in this repo's issue list; this comment is the link. The issue follows the PR's stage on the board while it is open.", prNumber, prRepo, ref, prURL) +} + func issueStagePRLinkComment(prURL string, prNumber int, explanation string) string { return fmt.Sprintf("\nPR #%d covers this issue: %s\n\n%s", prNumber, prNumber, prURL, explanation) } diff --git a/src/pkg/github/prclaims.go b/src/pkg/github/prclaims.go index abe95667a7..8d4d03df39 100644 --- a/src/pkg/github/prclaims.go +++ b/src/pkg/github/prclaims.go @@ -934,6 +934,7 @@ func (c *Client) RecordOpenedPRClaims(ledger *ClaimLedger, d PROpenedDetail, ide return nil } claims := ClaimsFromPROpened(d, c.getRepos(), identity, now) + c.canonicalizeClaimRepos(claims) for _, claim := range claims { if err := ledger.Record(claim); err != nil && c.logger != nil { c.logger.Warn("duplicate-PR guard: could not persist claim from just-opened PR", @@ -1027,6 +1028,7 @@ func (c *Client) FetchClaimScan(ctx context.Context, identity HiveIdentity) (Cla continue } prClaims := claimsFromPR(pr, repo, identity, now) + c.canonicalizeClaimRepos(prClaims) for i := range prClaims { prClaims[i].PRState = PRStateOpen } @@ -1121,6 +1123,7 @@ func (c *Client) FetchClaimScan(ctx context.Context, identity HiveIdentity) (Cla // #16116: other people's reviews reach the throughput card's // Reviewed row the way their merges reach Merged·Closed. prClaims := claimsFromPR(pr, repo, identity, now) + c.canonicalizeClaimRepos(prClaims) history = append(history, prHistoryFromClaims(prClaims, PRStateMerged, now)...) for _, claim := range prClaims { claim.MergedPR = true diff --git a/src/pkg/github/split_parents.go b/src/pkg/github/split_parents.go index 4a512121dc..f600b242f7 100644 --- a/src/pkg/github/split_parents.go +++ b/src/pkg/github/split_parents.go @@ -51,6 +51,11 @@ type SplitParent struct { Parent int `json:"parent"` Agent string `json:"agent,omitempty"` At time.Time `json:"at"` + // ParentRepo is the parent's lowercased "owner/name" when it lives in a + // different repo from the child (hivecommons/hive#18680); empty means the + // child's own repo, which is every entry written before cross-repo + // decomposition existed. + ParentRepo string `json:"parent_repo,omitempty"` } type splitParentLedger struct { @@ -94,10 +99,21 @@ func LoadSplitParents(path string) (map[string]SplitParent, error) { // (full "owner/repo"). Called only after AddSubIssue succeeded, so the ledger // never claims a link GitHub does not show. func RecordSplitParent(path, repo string, child, parent int, agent string) error { + return RecordSplitParentRef(path, repo, child, "", parent, agent) +} + +// RecordSplitParentRef is RecordSplitParent for a parent that may live in +// another repo (hivecommons/hive#18680): parentRepo is the parent's full +// "owner/repo", or "" (or the child's own repo) for a same-repo parent. +func RecordSplitParentRef(path, repo string, child int, parentRepo string, parent int, agent string) error { if path == "" { path = SplitParentsPath } - if child <= 0 || parent <= 0 || child == parent { + parentRepo = strings.ToLower(strings.TrimSpace(parentRepo)) + if parentRepo == strings.ToLower(strings.TrimSpace(repo)) { + parentRepo = "" + } + if child <= 0 || parent <= 0 || (child == parent && parentRepo == "") { return nil } splitParentsMu.Lock() @@ -109,7 +125,7 @@ func RecordSplitParent(path, repo string, child, parent int, agent string) error // GitHub; start a fresh one. children = map[string]SplitParent{} } - children[splitParentKey(repo, child)] = SplitParent{Parent: parent, Agent: strings.TrimSpace(agent), At: time.Now().UTC()} + children[splitParentKey(repo, child)] = SplitParent{Parent: parent, ParentRepo: parentRepo, Agent: strings.TrimSpace(agent), At: time.Now().UTC()} pruneSplitParents(children) if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { @@ -147,11 +163,13 @@ func pruneSplitParents(children map[string]SplitParent) { } // splitParentOf returns the relay-recorded parent number of repo#number, or 0. +// A parent in another repo (SplitParent.ParentRepo) is not reported: callers +// resolve the number inside repo, where it would name a different issue. func splitParentOf(children map[string]SplitParent, repo string, number int) int { if len(children) == 0 { return 0 } - if sp, ok := children[splitParentKey(repo, number)]; ok && sp.Parent > 0 && sp.Parent != number { + if sp, ok := children[splitParentKey(repo, number)]; ok && sp.Parent > 0 && sp.Parent != number && (sp.ParentRepo == "" || strings.EqualFold(sp.ParentRepo, strings.TrimSpace(repo))) { return sp.Parent } return 0 diff --git a/src/pkg/policies/defaults/scanner-automerge.md b/src/pkg/policies/defaults/scanner-automerge.md index 20f1fabfb2..d87e96893b 100644 --- a/src/pkg/policies/defaults/scanner-automerge.md +++ b/src/pkg/policies/defaults/scanner-automerge.md @@ -93,7 +93,7 @@ On the Copilot CLI backend, do NOT pass the bare `haiku`/`sonnet`/`opus` aliases Set the model parameter explicitly on every agent call. When in doubt, use a mid-tier model — most issues don't need the heaviest model. -**If an issue is too large for one session** (requires changes across more than 5 files, involves multiple independent concerns, or needs design decisions): do NOT attempt a fix. Instead, create focused child issues (`hive-open-issue --parent ...` to link each child to the parent as a real GitHub sub-issue, and keep "Part of #N" in the body too so the link still reads in plain text; when the children have an order, pass `--blocked-by ` on each later one so the order is recorded as a GitHub "blocked by" link and the hive holds the later child back until the earlier one closes), add a comment on the parent explaining the decomposition, and move on. The next kick cycle picks up the children. +**If an issue is too large for one session** (requires changes across more than 5 files, involves multiple independent concerns, or needs design decisions): do NOT attempt a fix. Instead, create focused child issues (`hive-open-issue --parent ...` to link each child to the parent as a real GitHub sub-issue, and keep "Part of #N" in the body too so the link still reads in plain text; when the children have an order, pass `--blocked-by ` on each later one so the order is recorded as a GitHub "blocked by" link and the hive holds the later child back until the earlier one closes), add a comment on the parent explaining the decomposition, and move on. File each child in the managed repo where its work lives (`--repo` from the target paths and repo the plan names, which may differ from the parent's); when that is another repo, pass `--parent #` and write `Part of #` in full, because a bare `#N` resolves only inside the child's own repo. Never file a child in a repo this hive does not manage; the relay falls back to the parent's repo. The next kick cycle picks up the children. When using `--needs-decision`, include `Risk: ` in the body; the Owner rule above (decide engineering questions yourself, park only for risk categories) applies. diff --git a/src/policies/scanner-automerge.md b/src/policies/scanner-automerge.md index 20f1fabfb2..d87e96893b 100644 --- a/src/policies/scanner-automerge.md +++ b/src/policies/scanner-automerge.md @@ -93,7 +93,7 @@ On the Copilot CLI backend, do NOT pass the bare `haiku`/`sonnet`/`opus` aliases Set the model parameter explicitly on every agent call. When in doubt, use a mid-tier model — most issues don't need the heaviest model. -**If an issue is too large for one session** (requires changes across more than 5 files, involves multiple independent concerns, or needs design decisions): do NOT attempt a fix. Instead, create focused child issues (`hive-open-issue --parent ...` to link each child to the parent as a real GitHub sub-issue, and keep "Part of #N" in the body too so the link still reads in plain text; when the children have an order, pass `--blocked-by ` on each later one so the order is recorded as a GitHub "blocked by" link and the hive holds the later child back until the earlier one closes), add a comment on the parent explaining the decomposition, and move on. The next kick cycle picks up the children. +**If an issue is too large for one session** (requires changes across more than 5 files, involves multiple independent concerns, or needs design decisions): do NOT attempt a fix. Instead, create focused child issues (`hive-open-issue --parent ...` to link each child to the parent as a real GitHub sub-issue, and keep "Part of #N" in the body too so the link still reads in plain text; when the children have an order, pass `--blocked-by ` on each later one so the order is recorded as a GitHub "blocked by" link and the hive holds the later child back until the earlier one closes), add a comment on the parent explaining the decomposition, and move on. File each child in the managed repo where its work lives (`--repo` from the target paths and repo the plan names, which may differ from the parent's); when that is another repo, pass `--parent #` and write `Part of #` in full, because a bare `#N` resolves only inside the child's own repo. Never file a child in a repo this hive does not manage; the relay falls back to the parent's repo. The next kick cycle picks up the children. When using `--needs-decision`, include `Risk: ` in the body; the Owner rule above (decide engineering questions yourself, park only for risk categories) applies.