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
14 changes: 13 additions & 1 deletion 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 } from "./config.js";
import { comment, issue, pull } from "./fixtures.js";
import { approvalWanted, diffObserved, endingDeadline, followingThrough, gateState, isCommentNews, lifecycle, resolveChecklistItem, specHash, stuckDue, unheardWait } from "./reconcile.js";
import { approvalWanted, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, isCommentNews, lifecycle, resolveChecklistItem, specHash, stuckDue, unheardWait } from "./reconcile.js";
import { readyStateSchema } from "./store.js";
import { GITHUB_REVIEWS, LINEAR_TICKETS } from "./templates.js";

Expand Down Expand Up @@ -151,6 +151,18 @@ describe("the ready gate", () => {
expect(approvalWanted(gateState(tickets, ready({ checklist: allMet, substantial: true, approval }), "changed"))).toBe(true);
});

it("is known unmet on a missing or stale approval or an item reported unmet, not on an unreported item", () => {
const hash = specHash(issue(), "thr_s");
const approval = { at: 1, specHash: hash, by: "thr_p" };
expect(gateKnownUnmet(tickets, ready({}), hash)).toBe(true);
expect(gateKnownUnmet(tickets, ready({ approval }), hash)).toBe(false);
expect(gateKnownUnmet(tickets, ready({ approval, checklist: allMet.slice(1) }), hash)).toBe(false);
expect(gateKnownUnmet(tickets, ready({ approval }), "changed")).toBe(true);
expect(gateKnownUnmet(tickets, ready({ substantial: false }), hash)).toBe(false);
expect(gateKnownUnmet(tickets, ready({ approval, checklist: [{ ...allMet[0]!, met: false }] }), hash)).toBe(true);
expect(gateKnownUnmet(tickets, ready({ checklist: allMet, approval }), hash)).toBe(false);
});

it("knows a reported item by its number or the playbook's own words, never a paraphrase", () => {
const items = ["The problem, as observed: the current behaviour and where in the code it comes from", "What is out of scope"];
expect(resolveChecklistItem(items, 1)).toBe(items[0]);
Expand Down
14 changes: 14 additions & 0 deletions plugins/stewards/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ export function gateState(playbook: Playbook, ready: ReadyState, currentSpecHash
return { none: false, checklistMet, unmet, substantial: ready.substantial, approvalNeeded, approved, approvalStale, passed };
}

/**
* Whether the gate is shut on something known rather than merely not yet
* reported: approval is needed and the current spec has none (missing,
* voided or stale), or the steward reported a checklist item unmet. An item
* the steward has not reported does not count, so a ticket moved on right
* after its approval, before the steward reports again, is not flagged.
*/
export function gateKnownUnmet(playbook: Playbook, ready: ReadyState, currentSpecHash: string): boolean {
const gate = gateState(playbook, ready, currentSpecHash);
if (gate.passed) return false;
if (gate.approvalNeeded && (!gate.approved || gate.approvalStale)) return true;
return gate.unmet.some((item) => reportedItem(ready, item)?.met === false);
}

/**
* Whether the gate waits on its approver: the checklist is met, the ticket
* is (or may be) substantial, and the current spec has no approval. The
Expand Down
63 changes: 63 additions & 0 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,69 @@ describe("a Linear ticket steward", () => {
expect(wake).toContain(`Your thread id is ${steward}.`);
});

// BB-7: flagged on every Todo → In Progress, approval seconds old, and on
// every In Review → In Progress of a worker's normal loop.
describe("moving into build before the ready gate passed", () => {
const flagged = /before the ready gate passed/u;
const base = { phase: "Todo", summary: "Specified.", nextAction: "Move to In Progress", actor: "steward" };
const inProgress = (over: Parameters<typeof issue>[0] = {}) => issue({ state: "In Progress", stateType: "started", ...over });
/** A bound ticket in Todo, with its project steward and steward. */
const bound = async () => {
const t = await setup();
t.entities.set("uuid-12", issue());
await t.ring("linear", "uuid-12");
const [project, steward] = t.spawns.map((x) => x.id) as [string, string];
const wakes = () => t.sends.filter((s) => s.threadId === steward).map((s) => s.text);
/** Move the ticket, and return the wake it brought. */
const move = async (entity: EntitySnapshot) => {
const before = wakes().length;
t.entities.set("uuid-12", entity);
await t.ring("linear", "uuid-12");
expect(wakes().length).toBeGreaterThan(before);
return wakes().at(-1)!;
};
return { t, project, steward, move };
};

it("is raised entering build from spec with no approval", async () => {
const { move } = await bound();
expect(await move(inProgress())).toMatch(flagged);
});

it("is not raised with a current approval and no checklist report yet", async () => {
const { t, project, move } = await bound();
expect((await t.tool("steward_update", { binding: "ENG-12", approve: true }, project)).error).toBe(false);
expect(await move(inProgress())).not.toMatch(flagged);
});

it("is raised when the approval is stale for the current spec", async () => {
const { t, project, move } = await bound();
await t.tool("steward_update", { binding: "ENG-12", approve: true }, project);
expect(await move(inProgress({ description: "Users cannot log in. Also SSO." }))).toMatch(flagged);
});

it("is raised when the approval was voided", async () => {
const { t, project, steward, move } = await bound();
await t.tool("steward_update", { binding: "ENG-12", approve: true }, project);
expect((await t.tool("steward_report", { ...base, voidApproval: true }, steward)).error).toBe(false);
expect(await move(inProgress())).toMatch(flagged);
});

it("is raised when a reported checklist item is unmet, approval or not", async () => {
const { t, steward, move } = await bound();
const checklist = [1, 2, 3, 4].map((item) => ({ item, met: item !== 4 }));
expect((await t.tool("steward_report", { ...base, ready: { checklist, substantial: false } }, steward)).error).toBe(false);
expect(await move(inProgress())).toMatch(flagged);
});

it("is never raised moving back from review", async () => {
const { move } = await bound();
// No approval and nothing reported: a gate that is really unmet.
expect(await move(issue({ state: "In Review", stateType: "started" }))).not.toMatch(flagged);
expect(await move(inProgress())).not.toMatch(flagged);
});
});

it("tells the steward its own thread id and that the linear tools sign for it", async () => {
const t = await setup();
t.entities.set("uuid-12", issue());
Expand Down
7 changes: 4 additions & 3 deletions plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
type ProjectFile,
} from "./config.js";
import { nightlyDue, stillNight } from "./nightly.js";
import { advanced, approvalWanted, diffObserved, endingDeadline, followingThrough, gateState, hasLabel, isCommentNews, lifecycle, newestCommentAt, phaseAndStage, resolveChecklistItem, specHash, stuckDue, unheardWait, type Change } from "./reconcile.js";
import { advanced, approvalWanted, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, hasLabel, isCommentNews, lifecycle, newestCommentAt, phaseAndStage, resolveChecklistItem, specHash, stuckDue, unheardWait, type Change } from "./reconcile.js";
import {
SOURCE_PLUGIN_IDS,
STEWARDS_PLUGIN_ID,
Expand Down Expand Up @@ -942,8 +942,9 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
const workerRunning = store.workers(b.id).some((w) => w.status === "running");
const fresh = prev === null ? [] : diffObserved(prev, observed, { own: ownThreads(b), stewardThreadId: b.threadId, approved: ready.approval !== null, cursor: b.commentCursor, workerRunning });
changes.push(...fresh);
const gate = gateState(playbook, ready, currentSpec);
if (stage === "build" && b.stage !== "build" && !gate.passed && prev !== null) {
// Only an entry into build from spec: review → build is the worker's
// normal loop, and an unreported checklist is not an unmet one.
if (stage === "build" && b.stage === "spec" && prev !== null && gateKnownUnmet(playbook, ready, currentSpec)) {
changes.push({ text: `Moved into a build stage (${entity.state}) before the ready gate passed`, actionable: true });
}
if (b.requeueAt !== null && b.requeueAt <= now()) changes.push({ text: "Requeue time came", actionable: true });
Expand Down
Loading