From efa394feea72c9c7db19d4fc047c607e95089a67 Mon Sep 17 00:00:00 2001 From: Dylan Trotter Date: Mon, 19 Oct 2026 02:01:18 +0000 Subject: [PATCH] stewards: lease only the linked pull requests still open (BB-4) A ticket binding leased every pull request in its linkedPulls until the ticket ended, so closed and merged slices stayed watched. The github interest set now takes a ticket's linked pull requests from the stored snapshots in lastSnapshot.pulls and leaves out the closed and merged ones; a linked key with no snapshot yet is kept. A reconcile that changes which linked pull requests are leased pushes the interest set again. source-contract.ts is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- plugins/stewards/server.test.ts | 29 ++++++++++++++++++++++++++++ plugins/stewards/server.ts | 34 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/plugins/stewards/server.test.ts b/plugins/stewards/server.test.ts index 50f02827..b4132372 100644 --- a/plugins/stewards/server.test.ts +++ b/plugins/stewards/server.test.ts @@ -551,6 +551,35 @@ describe("a Linear ticket steward", () => { expect(t.hooks.store.open("p1")[0]!.pending).toEqual([]); }); + it("leases only the linked pull requests still open, and one not read yet", async () => { + const t = await setup(); + const link = (n: number) => ({ key: `acme/web#${n}`, url: `https://github.com/acme/web/pull/${n}` }); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started", linkedPulls: [link(7), link(8), link(9), link(10)] })); + t.entities.set("acme/web#7", pull()); + t.entities.set("acme/web#8", pull({ id: "acme/web#8", key: "acme/web#8", state: "closed" })); + t.entities.set("acme/web#9", pull({ id: "acme/web#9", key: "acme/web#9", state: "merged" }, { merged: true })); + // acme/web#10 has no snapshot: it cannot be read yet. + await t.ring("linear", "uuid-12"); + const github = t.rpcCalls.filter((c) => c.method === "sourceSetInterest" && c.pluginId === "github-watcher").at(-1)?.input; + expect(github).toEqual({ units: ["acme/web"], entities: ["acme/web#10", "acme/web#7"] }); + }); + + it("stops leasing a linked pull request once it merges", 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({ state: "In Progress", stateType: "started", ...linked })); + t.entities.set("acme/web#7", pull()); + await t.ring("linear", "uuid-12"); + const github = () => t.rpcCalls.filter((c) => c.method === "sourceSetInterest" && c.pluginId === "github-watcher"); + expect(github().at(-1)?.input).toEqual({ units: ["acme/web"], entities: ["acme/web#7"] }); + const sent = github().length; + + t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true })); + await t.ring("github", "acme/web#7"); + expect(github()).toHaveLength(sent + 1); + expect(github().at(-1)?.input).toEqual({ units: ["acme/web"], entities: [] }); + }); + it("counts a worker woken again (by a watch, a reviewer) as running", async () => { const t = await setup(); t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" })); diff --git a/plugins/stewards/server.ts b/plugins/stewards/server.ts index da094c56..a1c8d4e9 100644 --- a/plugins/stewards/server.ts +++ b/plugins/stewards/server.ts @@ -467,6 +467,23 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P return pulls; } + /** + * The pull requests linked to a ticket that are leased for it: those + * still open. One with no snapshot yet (not read, or the read failed) is + * kept, so a new pull request is watched before its first read. + */ + function leasedPulls(b: Pick): string[] { + if (b.kind !== "ticket" || b.lastSnapshot === null) return []; + const pulls = b.lastSnapshot.pulls; + return b.lastSnapshot.entity.linkedPulls + .map((l) => l.key) + .filter((k): k is string => k !== null) + .filter((k) => { + const seen = pulls.find((p) => lc(p.key) === lc(k)); + return seen === undefined || !["closed", "merged"].includes(lc(seen.state)); + }); + } + // The interest set: what linear and github should lease and ring us // about. Sent whole, only when it changed, a moment after the change. const lastInterest = new Map(); @@ -486,7 +503,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P entities: [ ...new Set([ ...open.filter((b) => b.source === "github").map((b) => b.entityId), - ...open.flatMap((b) => (b.kind === "ticket" ? (b.lastSnapshot?.entity.linkedPulls ?? []).map((l) => l.key).filter((k): k is string => k !== null) : [])), + ...open.flatMap((b) => leasedPulls(b)), ]), ].sort(), }, @@ -875,9 +892,22 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P /** * One reconcile of one binding: snapshot, lifecycle, compare, and wake * when something actionable changed (or `force` says this is a turn - * regardless: adoption, the nightly pass). + * regardless: adoption, the nightly pass). A linked pull request that + * appeared, closed or merged changes what is leased, so the interest set + * is pushed again. */ async function reconcileBinding(bindingId: string, force: WakeReason | null): Promise { + const before = store.binding(bindingId); + const leased = before === null ? "" : leasedPulls(before).join(" "); + try { + await reconcileOnce(bindingId, force); + } finally { + const after = store.binding(bindingId); + if (after !== null && leasedPulls(after).join(" ") !== leased) pushInterest(); + } + } + + async function reconcileOnce(bindingId: string, force: WakeReason | null): Promise { let b = store.binding(bindingId); if (b === null) return; const config = await loadConfig(b.projectId);