Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions plugins/stewards/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,13 @@ entry marked `decide: project | user`. The plugin raises an
marker puts a pull request in the owner-reviewed lane. The project
steward classifies it into an entry and either records a project-steward
approval for a `project` entry (`steward_update { binding, approve,
entry }`, bound to the pull request's head sha so a new push voids it and
kept only in the store, never as a GitHub comment or review) or forwards
it to you for a `user` one. A missing entry, an unknown mark, a `user`
entry, or a change to the decide list, `bigDecisions`, `prGate` or the
entry }`), which posts a signed `bb-approval` marker on the pull request,
keeps its comment id in the store and wakes the worker, or forwards it to
you for a `user` one. Only a marker whose comment id the store recorded
counts, so a hand-typed lookalike never does, and an approval is of the
pull request, so a later head is covered unless the worker added scope.
A missing entry, an unknown mark, a `user` entry, or a change to the
decide list, `bigDecisions`, `prGate` or the
approval code is always yours, whatever the list says; a signed comment
never satisfies either. `prGate` accepts the project-steward approval for
a `project` entry, and the user's unsigned GitHub approval is unchanged.
Expand Down Expand Up @@ -1166,9 +1169,10 @@ The rules that hold:
gate: the reviewer's latest marker has verdict `approved`,
every check has passed, and, in the owner-reviewed lane, the owner has
approved — an unsigned approving comment or an APPROVED review by the
connected user, or, for an entry the decide list marks `project`, a
project-steward approval recorded in the store and bound to the head sha
(BB-30); a signed comment is a thread's, never the owner's. An unknown
connected user, or, for an entry the decide list marks `project`, the
`bb-approval` marker the project steward posted, whose comment id the
store records (BB-30, BB-77); a signed comment is a thread's, never the
owner's. An unknown
lane is owner-reviewed, not no-review. Any comment whose first line opens
a `bb-review` marker is a round, however it is spelled: a marker missing
`verdict=` reads its verdict from a bare word (`changes-requested`) or as
Expand Down
8 changes: 8 additions & 0 deletions plugins/stewards/decisions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ describe("the decide list fails closed", () => {
expect(decideForPull([], decisions)).toBe("user");
expect(decideForPull(null, decisions)).toBe("user");
expect(decideForPull(["bb-plugins/plugins/stewards/store.ts"], decisions)).toBe("user");
// An identity path no project entry names is already the user's, whether or
// not a `user` entry lists it (BB-77).
const identity: BigDecision[] = [
{ text: "Identity and access", decide: "user", paths: ["bb-gate/router/**"] },
{ text: "A cross-repo contract", decide: "project", paths: ["bb-gate/docs/hooks.md"] },
];
expect(decideForPull(["bb-gate/router/routes.go"], identity)).toBe("user");
expect(decideForPull(["bb-gate/docs/hooks.md"], identity)).toBe("project");
});

it("gives the failed files fetch its own refusal text, not the self-widening one (BB-51)", () => {
Expand Down
14 changes: 11 additions & 3 deletions plugins/stewards/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ export function createGhTokenReader(): TokenReader {
}

export interface GithubWriter {
/** Post an unsigned comment as the connected user. */
comment(repo: string, number: number, body: string): Promise<void>;
/**
* Post a comment as the connected user and return GitHub's node id for it.
* The id ties the plugin's own approval record to the marker it posted, so a
* hand-typed lookalike never satisfies the lane gate.
*/
comment(repo: string, number: number, body: string): Promise<string>;
/** Squash-merge the pull request, with the given commit title and message. */
squashMerge(repo: string, number: number, title: string, body: string): Promise<void>;
/**
Expand Down Expand Up @@ -89,7 +93,11 @@ export function createGhWriter(token: TokenReader = createGhTokenReader(), doFet
};
return {
async comment(repo, number, body) {
await request("POST", `/repos/${repo}/issues/${number}/comments`, { body });
const response = await request("POST", `/repos/${repo}/issues/${number}/comments`, { body });
const created = (await response.json().catch(() => null)) as { node_id?: unknown; id?: unknown } | null;
const nodeId = created?.node_id ?? created?.id;
if (nodeId === undefined || nodeId === null || String(nodeId) === "") throw new GithubApiError("GitHub created the comment but returned no id, so the approval marker cannot be recorded.");
return String(nodeId);
},
async squashMerge(repo, number, title, body) {
await request("PUT", `/repos/${repo}/pulls/${number}/merge`, { merge_method: "squash", commit_title: title, commit_message: body });
Expand Down
85 changes: 70 additions & 15 deletions plugins/stewards/reconcile.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { parsePlaybook, phaseOf } from "./config.js";
import { comment, issue, pull } from "./fixtures.js";
import { approvalWanted, answerDiffers, anyWorkerInFlight, asksForOpenApproval, classifyApproval, commentDiffersFromRecommendation, compactDue, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, idleInActiveStage, isCommentNews, isOwnStageMove, lifecycle, markerLaneChange, namesUserAsBlocker, ownPullClosed, ownerApproval, prGate, resolveChecklistItem, reviewMarker, specHash, stuckDue, UNREADABLE_VERDICT, unheardWait, workerInFlight, workerReportedTerminal, type IdleBinding } from "./reconcile.js";
import { approvalMarkerLine, approvalWanted, answerDiffers, anyWorkerInFlight, asksForOpenApproval, classifyApproval, commentDiffersFromRecommendation, compactDue, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, idleInActiveStage, isCommentNews, isOwnStageMove, lifecycle, markerLaneChange, namesUserAsBlocker, ownPullClosed, ownerApproval, prGate, projectApprovalMarker, resolveChecklistItem, reviewMarker, reviewWorkerThread, specHash, stuckDue, UNREADABLE_VERDICT, unheardWait, workerInFlight, workerReportedTerminal, type IdleBinding, type ProjectApprovalRecord } from "./reconcile.js";
import { readyStateSchema } from "./store.js";
import { GITHUB_REVIEWS, LINEAR_TICKETS } from "./templates.js";

Expand Down Expand Up @@ -408,6 +408,9 @@ describe("the pull request gate (build-tickets)", () => {
const marker = (sha: string, verdict: string, lane: string) =>
comment({ id: `m-${sha}-${verdict}-${lane}`, author: "trotterdylan", body: `<!-- bb-review round=1 sha=${sha} verdict=${verdict} lane=${lane} -->`, signedBy: "thr_reviewer", createdAt: "2026-10-14T16:00:00Z" });
const owner = (body: string) => comment({ id: `oa-${body}`, author: "jane", body, signedBy: null, createdAt: "2026-10-14T17:00:00Z" });
const approval = (sha: string, entry: string | null, verdict: "approved" | "declined" = "approved", id = `ap-${sha}-${verdict}`, createdAt = "2026-10-14T16:10:00Z") =>
comment({ id, author: "trotterdylan", body: approvalMarkerLine({ sha, entry, verdict }), signedBy: "thr_project", createdAt });
const record = (commentId: string, verdict: "approved" | "declined" = "approved"): ProjectApprovalRecord => ({ commentId, verdict });
const green = { checks: { state: "success", failing: [] } };

it("is met with the reviewer's approval on the head and green checks in the no-review lane", () => {
Expand Down Expand Up @@ -460,39 +463,91 @@ describe("the pull request gate (build-tickets)", () => {
});

it("accepts a project-steward approval for a project entry, and only the user's for a user entry (BB-30)", () => {
const project = pull({ comments: [marker(head, "approved", "owner-reviewed")] }, green);
expect(prGate(project, { decision: "project", projectApproved: true })).toMatchObject({ decision: "project", projectApproved: true, gateMet: true, needsOwnerApproval: false });
const ap = approval(head, "A cross-repo contract");
const project = pull({ comments: [marker(head, "approved", "owner-reviewed"), ap] }, green);
expect(prGate(project, { decision: "project", projectApprovals: [record(ap.id)] })).toMatchObject({ decision: "project", projectApproved: true, gateMet: true, needsOwnerApproval: false });
// The same approval on a `user` entry is ignored: the user's unsigned GitHub approval is required.
expect(prGate(project, { decision: "user", projectApproved: true })).toMatchObject({ decision: "user", projectApproved: false, gateMet: false, needsOwnerApproval: true, missing: "owner approval needed" });
expect(prGate(project, { decision: "user", projectApprovals: [record(ap.id)] })).toMatchObject({ decision: "user", projectApproved: false, gateMet: false, needsOwnerApproval: true, missing: "owner approval needed" });
// The user's unsigned approval satisfies it either way.
const ownerApproved = pull({ comments: [marker(head, "approved", "owner-reviewed"), owner("LGTM")] }, green);
expect(prGate(ownerApproved, { decision: "user", projectApproved: false }).gateMet).toBe(true);
expect(prGate(ownerApproved, { decision: "user" }).gateMet).toBe(true);
// No project approval means the owner's.
expect(prGate(project, { projectApproved: false }).needsOwnerApproval).toBe(true);
expect(prGate(pull({ comments: [marker(head, "approved", "owner-reviewed")] }, green), { projectApprovals: [] }).needsOwnerApproval).toBe(true);
});

it("makes the decide mark the whole gate: a project entry the project steward, everything else the owner (BB-51)", () => {
const project = pull({ comments: [marker(head, "approved", "owner-reviewed")] }, green);
const projectWaiting = prGate(project, { decision: "project", projectApproved: false });
const ap = approval(head, "A cross-repo contract");
const project = pull({ comments: [marker(head, "approved", "owner-reviewed"), ap] }, green);
const projectWaiting = prGate(project, { decision: "project", projectApprovals: [] });
expect(projectWaiting).toMatchObject({ decision: "project", needsProjectApproval: true, needsOwnerApproval: false, gateMet: false, missing: "project steward approval needed" });
expect(prGate(project, { decision: "project", projectApproved: true })).toMatchObject({ gateMet: true, needsProjectApproval: false });
expect(prGate(project, { decision: "project", projectApprovals: [record(ap.id)] })).toMatchObject({ gateMet: true, needsProjectApproval: false });
// A user entry needs the owner, and a project-steward approval is not the gate.
const userWaiting = prGate(project, { decision: "user", projectApproved: false });
const userWaiting = prGate(project, { decision: "user", projectApprovals: [] });
expect(userWaiting).toMatchObject({ decision: "user", needsOwnerApproval: true, needsProjectApproval: false, gateMet: false, missing: "owner approval needed" });
expect(prGate(project, { decision: "user", projectApproved: true })).toMatchObject({ projectApproved: false, gateMet: false, needsOwnerApproval: true });
expect(prGate(project, { decision: "user", projectApprovals: [record(ap.id)] })).toMatchObject({ projectApproved: false, gateMet: false, needsOwnerApproval: true });
// An owner approval satisfies the user lane, not the project lane.
const ownerApproved = pull({ comments: [marker(head, "approved", "owner-reviewed"), owner("LGTM")] }, green);
expect(prGate(ownerApproved, { decision: "user" }).gateMet).toBe(true);
expect(prGate(ownerApproved, { decision: "project", projectApproved: false }).gateMet).toBe(false);
expect(prGate(ownerApproved, { decision: "project", projectApprovals: [] }).gateMet).toBe(false);
});

it("honours a project-steward marker only when the plugin recorded its comment id (BB-77)", () => {
// A hand-typed lookalike: the marker text is right, the comment id is not in the store.
const fake = approval(head, "A cross-repo contract", "approved", "typed-by-hand");
const project = pull({ comments: [marker(head, "approved", "owner-reviewed"), fake] }, green);
expect(prGate(project, { decision: "project", projectApprovals: [record("IC_1")] })).toMatchObject({ projectApproved: false, gateMet: false, missing: "project steward approval needed" });
// The record alone, with the marker gone from the snapshot, is not an approval either.
expect(prGate(pull({ comments: [marker(head, "approved", "owner-reviewed")] }, green), { decision: "project", projectApprovals: [record(fake.id)] })).toMatchObject({ projectApproved: false, gateMet: false });
// A declined marker is not an approval even when its record matches.
const declined = approval(head, "A cross-repo contract", "declined", "IC_declined");
expect(prGate(pull({ comments: [marker(head, "approved", "owner-reviewed"), declined] }, green), { decision: "project", projectApprovals: [record(declined.id, "declined")] })).toMatchObject({ projectApproved: false, gateMet: false, missing: "project steward approval needed" });
});

it("lets a decline at a later sha revoke an earlier approval, and a later approval revoke the decline (BB-77)", () => {
const sha1 = "aaaaaaa1";
const sha2 = "bbbbbbb2";
const sha3 = "ccccccc3";
const approved = approval(sha1, "A cross-repo contract", "approved", "IC_approved", "2026-10-14T16:10:00Z");
const declined = approval(sha2, "A cross-repo contract", "declined", "IC_declined", "2026-10-14T16:20:00Z");
const records: ProjectApprovalRecord[] = [record(approved.id), record(declined.id, "declined")];
const declinedPull = pull({ comments: [marker(sha1, "approved", "owner-reviewed"), approved, declined] }, { ...green, headSha: sha3 });
expect(prGate(declinedPull, { decision: "project", projectApprovals: records })).toMatchObject({ projectApproved: false, gateMet: false, missing: "project steward approval needed" });
const approvedAgain = approval(sha3, "A cross-repo contract", "approved", "IC_approved_again", "2026-10-14T16:30:00Z");
const reapproved = pull({ comments: [marker(sha1, "approved", "owner-reviewed"), approved, declined, approvedAgain] }, { ...green, headSha: sha3 });
expect(prGate(reapproved, { decision: "project", projectApprovals: [...records, record(approvedAgain.id)] })).toMatchObject({ projectApproved: true, gateMet: true });
});

it("covers a later head with an approval recorded on an earlier one (BB-77)", () => {
const earlier = "bbbbbbb2";
const ap = approval(earlier, "A cross-repo contract");
const moved = pull({ comments: [marker(earlier, "approved", "owner-reviewed"), ap] }, { ...green, headSha: head });
expect(moved.pull?.headSha).toBe(head);
expect(prGate(moved, { decision: "project", projectApprovals: [record(ap.id)] })).toMatchObject({ projectApproved: true, gateMet: true, headSha: head });
});

it("renders and parses the approval marker, quoting an entry with spaces (BB-77)", () => {
const line = approvalMarkerLine({ sha: head, entry: "A cross-repo contract other repos implement", verdict: "approved" });
expect(line).toBe(`<!-- bb-approval by=project-steward sha=${head} entry="A cross-repo contract other repos implement" -->`);
expect(projectApprovalMarker(line)).toEqual({ by: "project-steward", sha: head, entry: "A cross-repo contract other repos implement", verdict: null });
expect(projectApprovalMarker(approvalMarkerLine({ sha: head, entry: null, verdict: "declined" }))).toEqual({ by: "project-steward", sha: head, entry: null, verdict: "declined" });
expect(projectApprovalMarker("<!-- bb-review round=1 -->")).toBeNull();
});

it("finds the worker that posted build-tickets' review comment (BB-77)", () => {
const c = comment({ id: "w", body: "Independent review running in bb thread thr_w.", signedBy: "thr_w" });
expect(reviewWorkerThread([c])).toBe("thr_w");
expect(reviewWorkerThread([comment({ id: "u", body: "Independent review running.", signedBy: null })])).toBeNull();
});

it("fails closed on a lane the plugin cannot read (BB-30)", () => {
// An unknown lane is treated as owner-reviewed, not no-review.
const unknown = prGate(pull({ comments: [marker(head, "approved", "something-else")] }, green));
const unknown = prGate(pull({ comments: [marker(head, "approved", "something-else")] }, green), { projectApprovals: [] });
expect(unknown).toMatchObject({ lane: "something-else", gateMet: false, needsOwnerApproval: true });
// With a project approval it is satisfied as owner-reviewed, not as no-review.
expect(prGate(pull({ comments: [marker(head, "approved", "something-else")] }, green), { decision: "project", projectApproved: true }).gateMet).toBe(true);
expect(prGate(pull({ comments: [marker(head, "approved", "something-else")] }, green), { projectApproved: false }).gateMet).toBe(false);
const ap = approval(head, "A cross-repo contract");
const withAp = pull({ comments: [marker(head, "approved", "something-else"), ap] }, green);
expect(prGate(withAp, { decision: "project", projectApprovals: [record(ap.id)] }).gateMet).toBe(true);
expect(prGate(withAp, { projectApprovals: [] }).gateMet).toBe(false);
});

it("is not met when the verdict is not approved, checks are not green, or there is no round", () => {
Expand Down
Loading
Loading