diff --git a/plugins/stewards/fixtures.ts b/plugins/stewards/fixtures.ts index 55843152..fdabc8f4 100644 --- a/plugins/stewards/fixtures.ts +++ b/plugins/stewards/fixtures.ts @@ -49,7 +49,10 @@ export const pull = (over: Partial = {}, pullOver: Partial = {}, settings: Record { - w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: null, sectionId, providerId: "claude-code", model: "claude-opus-5-5", reasoningLevel: "high", environmentProviderId: "multi-repo-workspace", branchName: "retry" }); + w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: null, sectionId, providerId: "claude-code", model: "claude-opus-5-5", reasoningLevel: "high", environmentProviderId: "multi-repo-workspace", branchName: "bb/eng-12-retry" }); await harness.behavior.emitThreadEvent("thread.created", { thread: makeThreadResponse({ id, parentThreadId: null, lifecycleOwnerThreadId: owner, title, originPluginId: null, providerId: "claude-code", sectionId }) }); await flush(); }; @@ -1791,10 +1791,10 @@ describe("scheduling overlapping work", () => { expect(toProject()).toHaveLength(1); const wake = toProject()[0]!.text; expect(wake).toMatch(/^# Project steward: scheduling requested for ENG-13/u); - expect(wake).toMatch(/from ENG-13 \(schedule\): Schedule ENG-13 \(Add SSO\)\? Its ready gate has passed, and it overlaps at file level with what is already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/login; its pull request acme\/web#7 is open on retry\. The default is to stack it, not to wait for the merge: when the earlier pull request is open, steward_update \{ binding: "ENG-13", guidance: "Start now, branch from with that branch as PR base", resolve: "" \}; when it has no pull request yet, steward_update \{ binding: "ENG-13", hold: "", stack: \["ENG-12"\] \}/u); + expect(wake).toMatch(/from ENG-13 \(schedule\): Schedule ENG-13 \(Add SSO\)\? Its ready gate has passed, and it overlaps at file level with what is already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/login; its pull request acme\/web#7 is open on bb\/eng-12-retry\. The default is to stack it, not to wait for the merge: when the earlier pull request is open, steward_update \{ binding: "ENG-13", guidance: "Start now, branch from with that branch as PR base", resolve: "" \}; when it has no pull request yet, steward_update \{ binding: "ENG-13", hold: "", stack: \["ENG-12"\] \}/u); expect(wake).toMatch(/A schedule escalation is the plugin's/u); expect(wake).toMatch(/- ENG-12 \(ticket, In Progress\): .*\[touches web\/src\/login\]/u); - expect(wake).toMatch(/- ENG-13 \(ticket, Todo\): .*\[touches web\/src; file-level overlap: ENG-12 on web\/src\/login \(its pull request acme\/web#7 is open on retry\); 1 escalation\(s\): schedule→project\]/u); + expect(wake).toMatch(/- ENG-13 \(ticket, Todo\): .*\[touches web\/src; file-level overlap: ENG-12 on web\/src\/login \(its pull request acme\/web#7 is open on bb\/eng-12-retry\); 1 escalation\(s\): schedule→project\]/u); const id = /(esc_[\w-]+) from ENG-13 \(schedule\)/u.exec(wake)![1]!; // Asked once, however often the steward reports; its own wake carries the overlap and the wait. @@ -1855,7 +1855,7 @@ describe("scheduling overlapping work", () => { await t.tool("steward_report", { ...building, touches: ["web/src/audit"] }, third); await t.tool("steward_report", ready, second); expect(toProject()).toHaveLength(2); - expect(toProject()[1]!.text).toMatch(/Schedule ENG-13 \(Add SSO\)\? .* overlaps at file level with what is already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/login; its pull request acme\/web#7 is open on retry; ENG-14 \(In Progress; 0 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/audit\. The default is to stack it, not to wait for the merge: when the earlier pull request is open, steward_update \{ binding: "ENG-13", guidance: "Start now, branch from with that branch as PR base", resolve: "" \}; when it has no pull request yet, steward_update \{ binding: "ENG-13", hold: "", stack: \["ENG-12", "ENG-14"\] \}/u); + expect(toProject()[1]!.text).toMatch(/Schedule ENG-13 \(Add SSO\)\? .* overlaps at file level with what is already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/login; its pull request acme\/web#7 is open on bb\/eng-12-retry; ENG-14 \(In Progress; 0 open, 0 merged, no more pull requests: not reported\) on the overlapping files web\/src\/audit\. The default is to stack it, not to wait for the merge: when the earlier pull request is open, steward_update \{ binding: "ENG-13", guidance: "Start now, branch from with that branch as PR base", resolve: "" \}; when it has no pull request yet, steward_update \{ binding: "ENG-13", hold: "", stack: \["ENG-12", "ENG-14"\] \}/u); // Held behind both; ENG-14 is canceled, ENG-12 ends: the hold lifts when the last one goes. await t.tool("steward_update", { binding: "ENG-13", hold: "After the login and audit work", behind: ["ENG-12", "ENG-14"] }, project); @@ -3070,7 +3070,7 @@ describe("pull requests", () => { const before = t.sends.filter((s) => s.threadId === steward).length; const head = "aaaaaaa1"; const marker = comment({ id: "r8", author: "jane", did: "reviewed", body: ``, signedBy: "thr_reviewer", createdAt: "2026-10-14T16:00:00Z" }); - t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { checks: { state: "success", failing: [] } })); + t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); await t.tool("steward_report", { phase: "Review", summary: "Posted round 8.", nextAction: "Wait for the owner.", actor: "user" }, steward); expect(t.hooks.store.openNeeds(b.id).find((n) => n.kind === "approve-pr")).toBeDefined(); expect(t.sends.filter((s) => s.threadId === steward)).toHaveLength(before); @@ -3085,7 +3085,7 @@ describe("pull requests", () => { const b = t.hooks.store.find("ENG-12")!; const before = t.sends.filter((s) => s.threadId === steward).length; const marker = comment({ id: "r8", author: "jane", did: "reviewed", body: ``, signedBy: "thr_reviewer", createdAt: "2026-10-14T16:00:00Z" }); - t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { checks: { state: "success", failing: [] } })); + t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); await t.tool("steward_report", { phase: "Review", summary: "Posted round 8.", nextAction: "Wait for the worker.", actor: "other" }, steward); expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "approve-pr")).toBe(false); expect(t.sends.filter((s) => s.threadId === steward)).toHaveLength(before); @@ -3100,7 +3100,7 @@ describe("pull requests", () => { const b = t.hooks.store.find("ENG-12")!; const before = t.sends.filter((s) => s.threadId === steward).length; const plain = comment({ id: "c9", author: "sam", body: "Looks good to me.", createdAt: "2026-10-14T16:00:00Z" }); - t.entities.set("acme/web#7", pull({ author: "sam", comments: [plain] }, { checks: { state: "success", failing: [] } })); + t.entities.set("acme/web#7", pull({ author: "sam", comments: [plain] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); await t.tool("steward_report", { phase: "Review", summary: "A comment landed.", nextAction: "Wait.", actor: "other" }, steward); expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "approve-pr")).toBe(false); expect(t.sends.filter((s) => s.threadId === steward)).toHaveLength(before); @@ -3114,7 +3114,7 @@ describe("pull requests", () => { const steward = t.spawns[1]!.id; const b = t.hooks.store.find("ENG-12")!; const marker = comment({ id: "r8", author: "jane", did: "reviewed", body: ``, signedBy: "thr_reviewer", createdAt: "2026-10-14T16:00:00Z" }); - t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { checks: { state: "success", failing: [] } })); + t.entities.set("acme/web#7", pull({ author: "sam", comments: [marker] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); await t.tool("steward_report", { phase: "Review", summary: "Posted round 8.", nextAction: "Wait for the owner.", actor: "user" }, steward); expect(t.hooks.store.openNeeds(b.id).filter((n) => n.kind === "approve-pr")).toHaveLength(1); // The doorbell reconcile that follows syncs the same marker again. @@ -3129,11 +3129,11 @@ describe("pull requests", () => { await t.ring("linear", "uuid-12"); const steward = t.spawns[1]!.id; const b = t.hooks.store.find("ENG-12")!; - await t.created("thr_w1", steward, "ENG-12: Implement"); + await t.created("thr_w1", steward, "ENG-12: Implement", { branchName: "bb/eng-12-retry" }); await t.idle("thr_w1", "Waiting on the reviewer."); const before = t.sends.filter((s) => s.threadId === steward).length; const marker = comment({ id: "r8", author: "jane", did: "reviewed", body: ``, signedBy: "thr_reviewer", createdAt: "2026-10-14T16:00:00Z" }); - t.entities.set("acme/web#7", pull({ author: "jane", comments: [marker] }, { headAuthor: "jane", checks: { state: "success", failing: [] }, reviews: [{ author: "jane", state: "commented", submittedAt: "2026-10-14T16:00:00Z", commit: "aaaaaaa1" }] })); + t.entities.set("acme/web#7", pull({ author: "jane", comments: [marker] }, { head: "bb/eng-12-retry", headAuthor: "jane", checks: { state: "success", failing: [] }, reviews: [{ author: "jane", state: "commented", submittedAt: "2026-10-14T16:00:00Z", commit: "aaaaaaa1" }] })); await t.ring("github", "acme/web#7"); expect(t.hooks.store.openNeeds(b.id).find((n) => n.kind === "approve-pr")).toBeDefined(); expect(t.sends.filter((s) => s.threadId === steward)).toHaveLength(before); @@ -5948,7 +5948,9 @@ describe("needs you items", () => { async function withPull(comments: ReturnType[], pullOver: Record = {}, entityOver: Record = {}) { const t = await setup(); t.entities.set("uuid-12", issue({ linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] })); - t.entities.set("acme/web#7", pull({ comments, ...entityOver }, { checks: { state: "success", failing: [] }, ...pullOver })); + // The worker branch convention names the ticket, so the linked pull + // belongs to it rather than merely mentioning it (BB-239). + t.entities.set("acme/web#7", pull({ comments, ...entityOver }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] }, ...pullOver })); await t.ring("linear", "uuid-12"); const [project, steward] = t.spawns.map((x) => x.id) as [string, string]; return { ...t, project, steward, binding: () => t.hooks.store.find("ENG-12")! }; @@ -7349,6 +7351,114 @@ describe("needs you items", () => { expect(out.text).toMatch(new RegExp(`Approval of acme/web#7 is requested from the user, on the Stewards page \\(${need.id}\\)`)); expect(out.text).not.toMatch(/Approval of the spec is requested/u); }); + + // BB-239: a linked pull request that only mentions the ticket (a GitHub + // cross-reference) files no gate item on this binding; one PR carries one + // set of items, on the binding whose worker opened it. + it("BB-239: a mentioned-only linked pull request files no gate item, while the ticket's own does", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ linkedPulls: [ + { key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }, + { key: "acme/web#8", url: "https://github.com/acme/web/pull/8" }, + ] })); + // #7 only mentions ENG-12: neither its branch nor its title names the + // ticket, so its gate items belong to the binding that owns it. + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Related to the login work", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + // #8 follows the worker branch convention for ENG-12 and belongs to it. + t.entities.set("acme/web#8", pull({ id: "acme/web#8", key: "acme/web#8", url: "https://github.com/acme/web/pull/8", title: "Fix login", comments: [marker, owner] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + const b = t.hooks.store.find("ENG-12")!; + expect(t.hooks.store.openNeeds(b.id).filter((n) => n.kind === "merge").map((n) => n.url)).toEqual(["https://github.com/acme/web/pull/8"]); + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "approve-pr" && n.url === "https://github.com/acme/web/pull/7")).toBe(false); + expect(t.hooks.store.openEscalations(b.id).some((x) => x.kind === "approval" && x.url === "https://github.com/acme/web/pull/7")).toBe(false); + }); + + it("BB-239: closes items and an escalation already filed for a non-belonging pull request", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] })); + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Related to the login work", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + const b = t.hooks.store.find("ENG-12")!; + const url = "https://github.com/acme/web/pull/7"; + const merge = t.hooks.store.fileNeed({ bindingId: b.id, kind: "merge", url, what: "Merge acme/web#7", how: null, filedBy: "plugin" }); + const approve = t.hooks.store.fileNeed({ bindingId: b.id, kind: "approve-pr", url, what: "Approve acme/web#7 on GitHub", how: null, filedBy: "plugin" }); + const esc = t.hooks.store.escalate({ bindingId: b.id, kind: "approval", question: "Approve acme/web#7", meanwhile: "Wait.", route: "user", recommendation: null, resolutionKind: "approve-decline", url, headSha: "aaaaaaa1", projectId: "p1" }); + await t.ring("linear", "uuid-12"); + expect(t.hooks.store.need(merge.id)!.status).toBe("done"); + expect(t.hooks.store.need(merge.id)!.resolution).toBe("The pull request belongs to another ticket."); + expect(t.hooks.store.need(approve.id)!.status).toBe("done"); + expect(t.hooks.store.need(approve.id)!.resolution).toBe("The pull request belongs to another ticket."); + expect(t.hooks.store.escalation(esc.id)!.status).toBe("resolved"); + expect(t.hooks.store.escalation(esc.id)!.resolution).toBe("The pull request belongs to another ticket."); + }); + + it("BB-239: a pull request that names one ticket files its items only there, not on a ticket that cross-references it", async () => { + const t = await setup(); + const linked = { linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] }; + t.entities.set("uuid-12", issue({ ...linked })); + t.entities.set("uuid-13", issue({ id: "uuid-13", key: "ENG-13", title: "Add SSO", ...linked })); + // #7 belongs to ENG-12 by branch and title, and merely mentions ENG-13. + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Fix login (#12)", comments: [marker, owner] }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + await t.ring("linear", "uuid-13"); + const a = t.hooks.store.find("ENG-12")!; + const b = t.hooks.store.find("ENG-13")!; + expect(t.hooks.store.openNeeds(a.id).some((n) => n.kind === "merge" && n.url === "https://github.com/acme/web/pull/7")).toBe(true); + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "merge")).toBe(false); + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "approve-pr")).toBe(false); + expect(t.hooks.store.openEscalations(b.id).some((x) => x.kind === "approval")).toBe(false); + }); + + // Each belonging rule in isolation, so a future change that drops one is + // caught (BB-239 review round 1). + it("BB-239: a worker's branch owns a pull request on that head even when the branch does not name the ticket", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] })); + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Fix login", comments: [marker, owner] }, { head: "retry", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + const steward = t.spawns[1]!.id; + await t.created("thr_w1", steward, "ENG-12: Implement", { branchName: "retry" }); + t.hooks.store.setWorker("thr_w1", "stopped", null); + await t.ring("linear", "uuid-12"); + const b = t.hooks.store.find("ENG-12")!; + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "merge" && n.url === "https://github.com/acme/web/pull/7")).toBe(true); + }); + + it("BB-239: a worker's final message that names the pull request owns it on a foreign branch", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] })); + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Fix login", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + const steward = t.spawns[1]!.id; + await t.created("thr_w1", steward, "ENG-12: Implement", { branchName: "retry" }); + t.hooks.store.setWorker("thr_w1", "stopped", "Opened https://github.com/acme/web/pull/7 for review."); + await t.ring("linear", "uuid-12"); + const b = t.hooks.store.find("ENG-12")!; + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "merge" && n.url === "https://github.com/acme/web/pull/7")).toBe(true); + }); + + it("BB-239: a title that names the ticket owns the pull request on a foreign branch", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] })); + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", title: "Fix login (ENG-12)", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + await t.ring("linear", "uuid-12"); + const b = t.hooks.store.find("ENG-12")!; + expect(t.hooks.store.openNeeds(b.id).some((n) => n.kind === "merge" && n.url === "https://github.com/acme/web/pull/7")).toBe(true); + }); + + it("BB-239: a GitHub issue's title number owns only a pull request in the issue's repo", async () => { + const t = await setup(); + t.files[playbookPath("github-issues")] = GITHUB_ISSUES; + t.entities.set("acme/web#14", githubIssue({ id: "acme/web#14", key: "acme/web#14", linkedPulls: [ + { key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }, + { key: "acme/other#14", url: "https://github.com/acme/other/pull/14" }, + ] })); + t.entities.set("acme/web#7", pull({ id: "acme/web#7", key: "acme/web#7", url: "https://github.com/acme/web/pull/7", unit: "acme/web", title: "Fix login (#14)", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + t.entities.set("acme/other#14", pull({ id: "acme/other#14", key: "acme/other#14", url: "https://github.com/acme/other/pull/14", unit: "acme/other", title: "Fix login (#14)", comments: [marker, owner] }, { head: "feature/other", checks: { state: "success", failing: [] } })); + await t.ring("github", "acme/web#14"); + const b = t.hooks.store.find("acme/web#14", "p1")!; + expect(t.hooks.store.openNeeds(b.id).filter((n) => n.kind === "merge").map((n) => n.url)).toEqual(["https://github.com/acme/web/pull/7"]); + }); }); // BB-69: the CLI lists the same "waiting on you" set the Stewards page and the @@ -11802,7 +11912,9 @@ describe("the reviewer role and the gate and merge commands (BB-133)", () => { const t = await setup(); if (over.project !== undefined) t.files[PROJECT_FILE] = JSON.stringify(over.project); t.entities.set("uuid-12", issue({ linkedPulls: [{ key, url: `https://github.com/${repo}/pull/${number}` }] })); - t.entities.set(key, pull({ id: key, key, url: `https://github.com/${repo}/pull/${number}`, unit: repo, comments, ...over.entityOver }, { checks: { state: "success", failing: [] }, ...over.pullOver })); + // The worker branch convention names the ticket, so the linked pull + // belongs to it rather than merely mentioning it (BB-239). + t.entities.set(key, pull({ id: key, key, url: `https://github.com/${repo}/pull/${number}`, unit: repo, comments, ...over.entityOver }, { head: "bb/eng-12-retry", checks: { state: "success", failing: [] }, ...over.pullOver })); if (over.files !== undefined) t.githubFiles.set(key, over.files); await t.ring("linear", "uuid-12"); const [project, steward] = t.spawns.map((x) => x.id) as [string, string]; @@ -12315,7 +12427,7 @@ describe("mechanical status and quiet notes (BB-203)", () => { async function withPull(comments: ReturnType[] = [marker]) { const t = await setup(); t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", ...linked })); - t.entities.set("acme/web#7", pull({ comments }, { headAuthor: "jane", checks: { state: "success", failing: [] } })); + t.entities.set("acme/web#7", pull({ comments }, { head: "bb/eng-12-retry", headAuthor: "jane", checks: { state: "success", failing: [] } })); await t.ring("linear", "uuid-12"); const [project, steward] = t.spawns.map((x) => x.id) as [string, string]; return { ...t, project, steward, binding: () => t.hooks.store.find("ENG-12")!, stewardSends: () => t.sends.filter((s) => s.threadId === steward).length }; diff --git a/plugins/stewards/server.ts b/plugins/stewards/server.ts index 358009fe..a00591a5 100644 --- a/plugins/stewards/server.ts +++ b/plugins/stewards/server.ts @@ -5970,6 +5970,39 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P return b.kind === "ticket" ? [...own, ...observed.pulls] : own; } + /** + * Whether a linked pull request belongs to a ticket binding rather than + * merely mentioning it (BB-239). A pull request cross-referenced in the + * ticket's timeline but belonging to another ticket's binding files no + * `approve-pr`/`merge` item and raises no approval escalation here; it + * stays in the wake as context. It belongs when a worker of the binding + * reported or opened it (`ownPulls`), or its head branch or title names + * the ticket: the worker branch convention `bb/---…`, or + * the ticket named in the title (`(#)` for a GitHub issue, the key + * `ENG-12`/`BB-239` for a Linear ticket). The closing set is not in the + * snapshot contract, so a closing pull request with neither a worker nor a + * naming branch or title is not recognized in this first cut (BB-239 spec). + */ + function pullBelongsToTicket(b: Binding, pull: EntitySnapshot, ownPulls: ReadonlySet): boolean { + const escape = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); + if (ownPulls.has(lc(pull.key)) || (pull.url !== null && ownPulls.has(lc(pull.url)))) return true; + const key = b.entityKey.trim(); + const number = /(\d+)$/u.exec(key)?.[1] ?? null; + if (number === null) return false; + const slug = lc(key).replace(/[^a-z0-9]+/gu, "-").replace(/^-+|-+$/gu, ""); + const head = lc(pull.pull?.head ?? ""); + if (head !== "" && slug !== "" && new RegExp(`(?:^|[-/])${escape(slug)}(?:[-/]|$)`, "u").test(head)) return true; + const title = pull.title ?? ""; + // A GitHub issue's worker titles its pull request with the issue number, + // and only a pull request in the issue's own repo counts (a different + // repo's #12 is not this ticket). A Linear ticket's title names the key + // (e.g. "… (ENG-12)"); `(#)` there would be a GitHub number and could + // match another repo's pull request (BB-239 review round 1). + const repo = key.includes("#") ? key.slice(0, key.indexOf("#")) : ""; + if (b.source === "github") return title.includes(`(#${number})`) && lc(pull.unit) === lc(repo); + return new RegExp(`(?:^|[^a-z0-9])${escape(lc(key))}(?:[^a-z0-9]|$)`, "u").test(lc(title)); + } + /** * The schedule wake's pull request facts (BB-89): how many of a binding's * linked pull requests are open and how many merged, and whether its @@ -6141,8 +6174,21 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P const config = await loadConfig(b.projectId); const decisions = config?.file.bigDecisions ?? []; const ownerLogin = config?.githubLogin ?? null; + // BB-239: the pull requests a worker of this binding opened or reported. + // A ticket's other linked pull requests (GitHub cross-references) only + // mention it and must not file gate items here. + const ownPulls = b.kind === "ticket" ? await ownPullKeys(b, store.workers(b.id), observed) : null; for (const pull of observedPulls(b, observed)) { const url = pull.url ?? pull.key; + if (ownPulls !== null && !pullBelongsToTicket(b, pull, ownPulls)) { + // The pull request belongs to another ticket's binding; close any + // item filed before this rule and leave it in the wake as context. + store.closeNeedByKey(b.id, "approve-pr", url, "The pull request belongs to another ticket."); + store.closeNeedByKey(b.id, "merge", url, "The pull request belongs to another ticket."); + const esc = store.openEscalations(b.id).find((x) => x.kind === "approval" && x.url === url && x.resolutionKind === "approve-decline") ?? null; + if (esc !== null) store.resolve(esc.id, "The pull request belongs to another ticket."); + continue; + } const hadApprove = store.openNeeds(b.id).some((n) => n.kind === "approve-pr" && n.url === url); const { gate: g, closed, approvalRaised: raised } = await syncPullApproval(approvalsCtx, b, pull, decisions); if (raised) approvalRaised = true;