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
88 changes: 85 additions & 3 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ describe("a Linear ticket steward", () => {
const partly = await t.tool("steward_report", { ...base, ready: { checklist: [1, 2, 3].map((item) => ({ item, met: true })), substantial: false } }, steward);
expect(partly.text).toMatch(/Ready gate: not passed \(unmet: 4\)/u);
const met = await t.tool("steward_report", { ...base, ready: { checklist: [1, 2, 3, "Out of scope is stated"].map((item) => ({ item, met: true, note: "in the spec" })), substantial: false } }, steward);
expect(met).toMatchObject({ error: false, text: expect.stringMatching(/Ready gate: passed\./u) });
expect(met).toMatchObject({ error: false, text: expect.stringMatching(/Ready gate: passed and nothing schedules you\./u) });

// Moving to a build stage now is no conflict, and the wake says the gate passed.
t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" }));
Expand All @@ -489,6 +489,86 @@ describe("a Linear ticket steward", () => {
expect(wake).toContain(`Your thread id is ${steward}.`);
});

// BB-55: the stall after a report passes the ready gate. The reply must send
// the steward to work in the same turn, and the mechanical tier wakes it
// again at once if it still ends without a worker, unless a hold or an
// approval/schedule request is deciding instead.
describe("BB-55: the ready-gate start-now reply and the backstop wake", () => {
const base = { phase: "Todo", summary: "Specified.", nextAction: "Move to In Progress", actor: "steward" };
const met = { checklist: [1, 2, 3, 4].map((item) => ({ item, met: true })), substantial: false };
const backstop = "The ready gate passed on your last turn and nothing holds you: start the worker.";
const startWakes = (t: Awaited<ReturnType<typeof setup>>) => t.sends.filter((s) => s.text.includes(backstop));
const startReply = "Reported. Ready gate: passed and nothing schedules you. Move the ticket to In Progress and start the worker now, in this turn, with the task in the In Progress instructions; do not end the turn first.";

async function bound() {
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];
return { t, project, steward };
}

it("says start now on the report that passes the gate, and wakes it at once when the turn still ends with no worker", async () => {
const { t, steward } = await bound();
const reported = await t.tool("steward_report", { ...base, ready: met }, steward);
expect(reported.text).toBe(startReply);
await t.flush();
expect(startWakes(t)).toHaveLength(1);
});

it("drops the backstop wake when the steward obeys and starts the worker in the same turn", async () => {
const { t, steward } = await bound();
expect((await t.tool("steward_report", { ...base, ready: met }, steward)).text).toBe(startReply);
// The steward obeys: the ticket moves to In Progress and a worker is
// spawned before the turn ends. The queued note is re-checked at
// delivery and dropped, so no wake fires.
t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" }));
await t.created("thr_w1", steward);
await t.flush();
expect(startWakes(t)).toHaveLength(0);
});

it("drops the backstop wake when a hold is applied between the report and the wake", async () => {
const { t, project, steward } = await bound();
expect((await t.tool("steward_report", { ...base, ready: met }, steward)).text).toBe(startReply);
expect((await t.tool("steward_update", { binding: "ENG-12", hold: "Wait for the design" }, project)).error).toBe(false);
await t.flush();
expect(startWakes(t)).toHaveLength(0);
});

it("does not schedule the backstop wake when a hold is open", async () => {
const { t, project, steward } = await bound();
expect((await t.tool("steward_update", { binding: "ENG-12", hold: "Wait for the design" }, project)).error).toBe(false);
const reported = await t.tool("steward_report", { ...base, ready: met }, steward);
expect(reported.text).not.toContain("nothing schedules you");
await t.flush();
expect(startWakes(t)).toHaveLength(0);
});

it("does not schedule the backstop wake when an approval request is open", async () => {
const { t, steward } = await bound();
const reported = await t.tool("steward_report", { ...base, ready: { ...met, substantial: true } }, steward);
expect(reported.text).not.toContain("nothing schedules you");
await t.flush();
expect(startWakes(t)).toHaveLength(0);
});

it("does not schedule the backstop wake when a scheduling request is open", 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());
t.entities.set("uuid-13", issue({ id: "uuid-13", key: "ENG-13", title: "Add SSO" }));
await t.ring("linear", "uuid-12");
await t.ring("linear", "uuid-13");
const [, first, second] = t.spawns.map((x) => x.id);
await t.tool("steward_report", { phase: "In Progress", summary: "Building.", nextAction: "Open the PR", actor: "steward", touches: ["web/src/login"] }, first!);
const reported = await t.tool("steward_report", { ...base, ready: met, touches: ["web/src"] }, second!);
expect(reported.text).toMatch(/Scheduling is with the project steward/u);
await t.flush();
expect(startWakes(t)).toHaveLength(0);
});
});

// 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", () => {
Expand Down Expand Up @@ -1251,8 +1331,10 @@ describe("scheduling overlapping work", () => {
const toProject = () => t.sends.filter((s) => s.threadId === project);
// ENG-13 is ready while ENG-12 has not said what it touches: nothing to schedule against.
const reported = await t.tool("steward_report", ready, second!);
expect(reported.text).toBe("Reported. Ready gate: passed. You will be woken when something actionable changes, or in 2d. End the turn now.");
expect(reported.text).toBe("Reported. Ready gate: passed and nothing schedules you. Move the ticket to In Progress and start the worker now, in this turn, with the task in the In Progress instructions; do not end the turn first.");
expect(toProject()).toHaveLength(0);
// BB-55: the passing report brings the one backstop wake, and nothing else wakes ENG-13.
await t.flush();
// ENG-12 reports its paths; ENG-13's next look (a label touch, nothing actionable) files the request and wakes nobody else.
await t.tool("steward_report", { ...building, touches: ["web/src/login"] }, first!);
const sentToSecond = t.sends.filter((s) => s.threadId === second).length;
Expand Down Expand Up @@ -1358,7 +1440,7 @@ describe("scheduling overlapping work", () => {
t.hooks.store.patch(t.hooks.store.find("ENG-12")!.id, { noMorePulls: true });
expect((await t.harness.behavior.runCli(["show", "ENG-12"])).stdout).toContain("no more pull requests");
const finished = await t.tool("steward_report", ready, second);
expect(finished.text).toMatch(/Ready gate: passed\. You will be woken/u);
expect(finished.text).toMatch(/Ready gate: passed and nothing schedules you/u);
expect(toProject()).toHaveLength(0);
// The same merged PR, but no "no more coming": still at work, so it asks.
t.hooks.store.patch(t.hooks.store.find("ENG-12")!.id, { noMorePulls: null });
Expand Down
55 changes: 54 additions & 1 deletion plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export const DEFAULT_TIMING: Timing = {
approvalLookbackMs: 24 * 60 * 60_000,
};

/** BB-55: the backstop wake when a report passes the ready gate and the turn ends with no worker started. */
export const START_WORKER_WAKE = "The ready gate passed on your last turn and nothing holds you: start the worker.";

export interface StewardsDeps {
now: () => number;
timing?: Partial<Timing>;
Expand Down Expand Up @@ -1899,7 +1902,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// and a queued count is stale; anything else is kept once, and is
// actionable if either says so.
const queued = b.pending;
const loudQueued = queued.filter((n) => n.quiet !== true).length;
let loudQueued = queued.filter((n) => n.quiet !== true).length;
for (const n of queued) {
if (isCommentNews(n.text)) continue;
const same = changes.find((c) => c.text === n.text);
Expand Down Expand Up @@ -1927,6 +1930,26 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// The work this binding was holding others behind has landed: lift them.
if (allPullsMerged(b, observed) && (prev === null || !allPullsMerged(b, prev))) await releaseBehind(b, "merged its pull requests");
const scheduling = await syncScheduleRequest(b, playbook);
// BB-55: the start-now backstop is queued by the report that passed the
// gate, but by the time the turn ends the steward may have obeyed: the
// ticket is in its build stage, a worker is running, or something now
// holds it. Re-check here, with the fresh snapshot and the requests
// filed, and drop the note rather than wake a steward that started.
if (b.pending.some((n) => n.text === START_WORKER_WAKE)) {
const startStillDue =
b.stage === "spec" &&
gateState(playbook, b.ready, currentSpec).passed &&
!anyWorkerInFlight(store.workers(b.id)) &&
b.hold === null &&
!store.openEscalations(b.id).some((x) => x.kind === "approval" || x.kind === "schedule");
if (!startStillDue) {
b = store.patch(b.id, { pending: b.pending.filter((n) => n.text !== START_WORKER_WAKE) });
for (let i = changes.length - 1; i >= 0; i--) if (changes[i]!.text === START_WORKER_WAKE) changes.splice(i, 1);
}
// The drop changed what is queued; `deferred` and the wake reason read
// this, so it must not stay stale.
loudQueued = b.pending.filter((n) => n.quiet !== true).length;
}
/**
* Keep news for the next turn: what is actionable as a reason for it, the
* rest as quiet notes. A quiet comment count is not kept: the next wake
Expand Down Expand Up @@ -4733,6 +4756,10 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
async execute(input, { threadId }) {
try {
let b = ownBinding(threadId);
// The gate as it stood before this report, so the reply can tell a
// report that just passed it from one that finds it already passed.
const priorReady = b.ready;
const priorSnapshot = b.lastSnapshot;
const opsTickets = input.opsTickets ?? [];
if (opsTickets.length > 0) {
if (b.kind !== "ops") return toolError(new Error("opsTickets is for an ops steward's report. Report without it."));
Expand All @@ -4752,6 +4779,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
}
const config = await loadConfig(b.projectId);
const playbook = config === null ? null : bindingPlaybook(config, b);
const priorGate = playbook !== null && playbook.ready !== null ? gateState(playbook, priorReady, priorSnapshot === null ? (priorReady.approval?.specHash ?? "") : specHashFor(b, priorSnapshot.entity)) : null;
// Follow-through has one deadline from when it was first declared; a
// requeue past it is a requeue at it.
const followThroughUntil = b.ending !== "terminal" ? null : (b.followThroughUntil ?? (input.followThrough === undefined ? null : now() + (playbook?.followThroughMs ?? DEFAULT_FOLLOW_THROUGH_MS)));
Expand Down Expand Up @@ -4921,8 +4949,10 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
const escalated = raised === null ? "" : ` Escalation ${raised.id} is with ${raised.route === "parent" ? "your parent steward" : "the project steward"}; do what you said meanwhile.`;
// Where the gate stands after this report, so a miss shows now and not at the next wake.
let gateNote = "";
let gateJustPassed = false;
if (input.ready !== undefined && playbook !== null && playbook.ready !== null) {
const gate = gateState(playbook, ready, b.lastSnapshot === null ? "" : specHashFor(b, b.lastSnapshot.entity));
gateJustPassed = gate.passed && priorGate !== null && !priorGate.passed;
const unmet = gate.unmet.map((item) => playbook.ready!.checklist.indexOf(item) + 1);
const why = !gate.checklistMet ? `unmet: ${unmet.join(", ")}` : gate.approvalStale ? "the approval is stale" : "approval is needed";
gateNote = gate.passed ? " Ready gate: passed." : ` Ready gate: not passed (${why}).`;
Expand All @@ -4941,12 +4971,35 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
const schedule = store.openEscalations(b.id).find((x) => x.kind === "schedule");
const askedAbout = store.binding(b.id)?.scheduleAsked ?? b.scheduleAsked;
if (schedule !== undefined) gateNote += ` Scheduling is with the project steward (${schedule.id}): ${b.entityKey} touches what ${askedAbout.join(", ")} ${askedAbout.length === 1 ? "is" : "are"} already changing. Start no workers and do not move the ticket into a build state until it decides; wait for its guidance or hold.`;
// BB-55: the gate just passed and nothing else is deciding, so the
// turn must not end here. The backstop wake fires only when the
// ticket is still in its spec stage with no worker running: a hold
// or an open approval/schedule request means someone else decides,
// and no wake is due. The wake is scheduled at once (a loud note on
// the next reconcile), not left to the playbook's requeue.
b = store.binding(b.id) ?? b;
const nothingPending = request === undefined && specNeed === undefined && schedule === undefined && b.hold === null;
const startDue =
gateJustPassed &&
b.kind === "ticket" &&
b.endedAt === null &&
b.ending === null &&
b.stage === "spec" &&
!store.workers(b.id).some((w) => w.status === "running") &&
nothingPending;
if (startDue) {
store.note(b.id, START_WORKER_WAKE, false);
scheduleReconcile(b.id);
}
if (b.ending === "terminal") {
if (b.followThrough === null) return `Reported.${refusalNote}${escalated}${deploymentNote} The binding ends when this turn does: running workers are stopped and the thread is archived. End the turn now.`;
return `Reported.${refusalNote}${escalated}${deploymentNote} The binding stays open to follow through until ${iso(followThroughUntil!)}; running workers keep going. You will be woken when a worker finishes or something actionable changes, or in ${formatDuration(requeueMs)}. End the turn now.`;
}
const hidden = archiveWhenIdle && !needsUser(b) ? " Your thread is hidden when the turn ends, and comes back on that wake." : "";
const recorded = opsTickets.length === 0 ? "" : ` Recorded ${opsTickets.map((t) => `${t.ticket} (${t.did})`).join(", ")} against ${opsTickets.length === 1 ? "its fingerprint" : "their fingerprints"}.`;
if (gateJustPassed && nothingPending) {
return `Reported.${recorded}${refusalNote}${escalated} Ready gate: passed and nothing schedules you. Move the ticket to In Progress and start the worker now, in this turn, with the task in the In Progress instructions; do not end the turn first.`;
}
return `Reported.${recorded}${refusalNote}${gateNote}${escalated} You will be woken when something actionable changes, or in ${formatDuration(requeueMs)}.${hidden} End the turn now.`;
} catch (error) {
return toolError(error);
Expand Down
9 changes: 9 additions & 0 deletions plugins/stewards/wake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe("a steward's wake", () => {
expect(wake).toContain("not yours to choose");
});

// BB-55: the Todo wake must say that an unjudged substantiality is settled
// by a `substantial: false` report, which passes the gate at once.
it("says an unjudged substantiality is passed by a substantial: false report in the same turn", () => {
const wake = formatWake(wakeInput());
expect(wake).toContain("- Substantial: not judged yet (treated as yes)");
expect(wake).toContain("with every checklist item met");
expect(wake).toMatch(/report `substantial: false`.*the gate passes immediately on that report.*start the worker in the same turn/u);
});

it("lists an open escalation and approval request as on the Stewards page, not to be asked again", () => {
const { s, bind } = world();
const binding = bind("ENG-12");
Expand Down
5 changes: 5 additions & 0 deletions plugins/stewards/wake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ function gateLines(input: Pick<WakeInput, "gate" | "binding" | "playbook" | "esc
lines.push(" Report each item in steward_report's ready.checklist by its number: { item: 1, met: true, note }.");
lines.push(`- Substantial: ${gate.substantial === null ? "not judged yet (treated as yes)" : gate.substantial ? "yes" : "no"}${binding.ready.why ? ` — ${binding.ready.why}` : ""}`);
lines.push(` Substantial when any of: ${playbook.ready!.substantial.join("; ") || "(none listed)"}`);
// BB-55: the stall the ticket names: a steward that has not judged
// substantiality waits for an approval it does not need. Saying the
// `substantial: false` report passes the gate at once is what makes it
// start the worker in that same turn.
if (gate.substantial === null && !gate.passed) lines.push(" If none apply, report `substantial: false`: with every checklist item met, the gate passes immediately on that report, so move the ticket to In Progress and start the worker in the same turn.");
const approval = playbook.ready!.approval;
const how = approval === "me" ? "the user" : approval === "project" ? "the project steward" : `the ticket reaching ${approval["linear-state"]}`;
const request = input.escalations.find((x) => x.status === "open" && x.kind === "approval");
Expand Down
Loading