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
17 changes: 17 additions & 0 deletions plugins/stewards/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,23 @@ Plugin code. No model turn.
changed is kept as a quiet note and shown with the next turn something
else makes due.

Holds that wait behind other bindings (`holdBehind`) lift by state, not
only by the event that finishes the work. On every reconcile of a waiter,
when every binding it waits behind satisfies `finished` — every linked pull
request merged and the binding ended, terminal, or its steward reported
`noMorePulls` — the plugin releases the hold through the same path as a
merge-time release: the waiter is woken, the binding note and the project
note are written, and a lift that frees two waiters overlapping each other
re-holds the second behind the first. So a hold placed after its target
merged, or a `noMorePulls` report that arrives after the merge, lifts on
the next reconcile instead of waiting for the deploy. A hold with an empty
`holdBehind` is untouched. The `schedule` escalation the plugin files names
each conflicting binding's phase and its pull request facts — how many
linked pull requests are open and merged, and whether `noMorePulls` was
reported (with a note when a linked pull request could not be read) — so
the project steward can see merged-but-open work without a second look,
with no recommendation beyond holding behind it or starting now.

### Judgment tier

The steward is woken with one message in a fixed form:
Expand Down
66 changes: 63 additions & 3 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ 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 touches what is already being changed by ENG-12 \(In Progress\) on web\/src\/login\. Hold it behind that work with steward_update \{ binding: "ENG-13", hold: "<why>", behind: \["ENG-12"\] \}/u);
expect(wake).toMatch(/from ENG-13 \(schedule\): Schedule ENG-13 \(Add SSO\)\? Its ready gate has passed, and it touches what is already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on web\/src\/login\. Hold it behind that work with steward_update \{ binding: "ENG-13", hold: "<why>", behind: \["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; overlaps ENG-12 on web\/src\/login; 1 escalation\(s\): schedule→project\]/u);
Expand Down Expand Up @@ -1378,7 +1378,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\)\? .* already being changed by ENG-12 \(In Progress\) on web\/src\/login; ENG-14 \(In Progress\) on web\/src\/audit\. Hold it behind that work with steward_update \{ binding: "ENG-13", hold: "<why>", behind: \["ENG-12", "ENG-14"\] \}, and it starts by itself once their pull requests merge or they end/u);
expect(toProject()[1]!.text).toMatch(/Schedule ENG-13 \(Add SSO\)\? .* already being changed by ENG-12 \(In Progress; 1 open, 0 merged, no more pull requests: not reported\) on web\/src\/login; ENG-14 \(In Progress; 0 open, 0 merged, no more pull requests: not reported\) on web\/src\/audit\. Hold it behind that work with steward_update \{ binding: "ENG-13", hold: "<why>", behind: \["ENG-12", "ENG-14"\] \}, and it starts by itself once their pull requests merge or they end/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);
Expand Down Expand Up @@ -1466,7 +1466,7 @@ describe("scheduling overlapping work", () => {
expect(reported.text).toMatch(/Scheduling is with the project steward \(esc_[\w-]+\): ENG-13 touches what ENG-12 is already changing/u);
expect(t.hooks.store.find("ENG-13")!.clearedToStart).toBe(false);
expect(toProject()).toHaveLength(1);
expect(toProject()[0]!.text).toMatch(/already being changed by ENG-12 \(Todo\) on web\/src\/login/u);
expect(toProject()[0]!.text).toMatch(/already being changed by ENG-12 \(Todo; 0 open, 0 merged, no more pull requests: not reported\) on web\/src\/login/u);
// Held, it stops counting as in flight; told to start, it counts again.
const id = /(esc_[\w-]+) from ENG-13 \(schedule\)/u.exec(toProject()[0]!.text)![1]!;
await t.tool("steward_update", { binding: "ENG-13", guidance: "Start now: different files.", resolve: id }, project!);
Expand Down Expand Up @@ -1570,6 +1570,66 @@ describe("scheduling overlapping work", () => {
expect(t.hooks.store.project("p1").pendingNotes.join("\n")).toMatch(/Lifted the hold on ENG-13: ENG-12, which it waited behind, merged its pull requests, and its work is finished/u);
});

// BB-89: a hold placed after its target already merged and reported no
// more coming lifts on the waiter's next reconcile, not at an edge that has
// already passed.
it("lifts a hold placed after its target merged and reported noMorePulls on the next reconcile", async () => {
const { t, project, first, second, toProject } = await two();
t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true }));
await t.tool("steward_report", { ...building, noMorePulls: true }, first);
await t.ring("github", "acme/web#7");
expect(t.hooks.store.find("ENG-12")!.noMorePulls).toBe(true);
// The target is finished before the hold exists: the edge-triggered release
// has already run and found no waiter.
const wakes = toProject().length;
await t.tool("steward_update", { binding: "ENG-13", hold: "After ENG-12", behind: ["ENG-12"] }, project);
await t.flush();
// The hold placement's own reconcile of ENG-13 runs the level-triggered
// path: the target was already finished, so no edge fires for it.
expect(t.hooks.store.find("ENG-13")).toMatchObject({ hold: null, holdBehind: [] });
await t.ring("linear", "uuid-13");
expect(t.hooks.store.find("ENG-13")).toMatchObject({ hold: null, holdBehind: [] });
// Finished work lifts silently: a note on the digest, no project steward turn.
expect(toProject().length).toBe(wakes);
expect(t.hooks.store.project("p1").pendingNotes.join("\n")).toMatch(/Lifted the hold on ENG-13: ENG-12, which it waited behind, merged its pull requests, and its work is finished/u);
});

// BB-89: the same hold, but with no "no more coming" report, is not
// finished, so the level-triggered path leaves it alone.
it("does not lift a hold placed after its target merged while noMorePulls is unreported", async () => {
const { t, project, second } = await two();
t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true }));
await t.ring("github", "acme/web#7");
expect(t.hooks.store.find("ENG-12")!.noMorePulls).toBeNull();
await t.tool("steward_update", { binding: "ENG-13", hold: "After ENG-12", behind: ["ENG-12"] }, project);
await t.flush();
await t.ring("linear", "uuid-13");
expect(t.hooks.store.find("ENG-13")!.hold).toBe("After ENG-12");
expect(t.hooks.store.find("ENG-13")!.holdBehind).toEqual(["ENG-12"]);
});

// BB-89: the level-triggered path only touches holds that wait behind
// something; a plain hold is the project steward's to lift.
it("never lifts a hold with an empty holdBehind by state", async () => {
const { t, project } = await two();
await t.tool("steward_update", { binding: "ENG-13", hold: "Wait for the design" }, project);
await t.flush();
t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true }));
await t.ring("github", "acme/web#7");
await t.ring("linear", "uuid-13");
expect(t.hooks.store.find("ENG-13")).toMatchObject({ hold: "Wait for the design", holdBehind: [] });
});

// BB-89: the schedule wake names the target's pull request state so the
// project steward can see a merged-but-open conflict without a second look.
it("names the open and merged pull request counts in the schedule escalation", async () => {
const { t, second, toProject } = await two();
t.entities.set("acme/web#7", pull({ state: "merged" }, { merged: true }));
const reported = await t.tool("steward_report", ready, second);
expect(reported.text).toMatch(/Scheduling is with the project steward/u);
expect(toProject()[0]!.text).toMatch(/already being changed by ENG-12 \(In Progress; 0 open, 1 merged, no more pull requests: not reported\) on web\/src\/login/u);
});

// BB-54 item 5: reporting more pull requests after "none coming" clears the
// report and re-runs the overlap check, through the tool.
it("re-files a schedule request when a steward reports more pull requests after saying none were coming", async () => {
Expand Down
68 changes: 59 additions & 9 deletions plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,24 +1077,35 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
return { observed: { entity, pulls }, complete: pulls.length >= keys.length };
}

/** Whether `b` is at work, judged from a fresh GitHub read and its running workers (BB-54). */
async function atWorkFresh(b: Binding): Promise<boolean> {
/**
* `b`'s at-work judgement from a fresh GitHub read, with the read itself
* (BB-54, BB-89): the schedule wake names the pull request facts from the
* same read the judgement used.
*/
async function atWorkWithRead(b: Binding): Promise<{ atWork: boolean; observed: Observed; complete: boolean } | null> {
const running = store.workers(b.id).some((w) => w.status === "running");
const fresh = await freshObserved(b);
if (fresh === null) return atWork(b, { runningWorker: running });
if (fresh === null) return null;
// An incomplete read cannot prove the work is finished (a linked pull
// request may be open and unread), so drop a "no more coming" report for
// this judgement: the project steward is woken rather than a request
// skipped.
const base = { ...b, lastSnapshot: fresh.observed, ...(fresh.complete ? {} : { noMorePulls: null }) };
return atWork(base, { runningWorker: running });
return { atWork: atWork(base, { runningWorker: running }), observed: fresh.observed, complete: fresh.complete };
}

/** The overlapping open bindings at work on `b`'s paths, from a fresh GitHub read (BB-54). */
async function conflictsFresh(b: Binding): Promise<Conflict[]> {
const conflicts: Conflict[] = [];
/**
* The overlapping open bindings at work on `b`'s paths, from a fresh
* GitHub read (BB-54), each with the read the schedule wake reports from
* (BB-89).
*/
async function conflictsFresh(b: Binding): Promise<Array<Conflict & { observed: Observed | null; complete: boolean }>> {
const conflicts: Array<Conflict & { observed: Observed | null; complete: boolean }> = [];
for (const c of overlapping(b, store.open(b.projectId))) {
if (await atWorkFresh(c.with)) conflicts.push(c);
const read = await atWorkWithRead(c.with);
if (read === null ? atWork(c.with, { runningWorker: store.workers(c.with.id).some((w) => w.status === "running") }) : read.atWork) {
conflicts.push({ ...c, observed: read?.observed ?? null, complete: read?.complete ?? false });
}
}
return conflicts;
}
Expand Down Expand Up @@ -2144,6 +2155,20 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
await syncNeeds(b, observed, playbook, workerInFlight);
// 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");
// BB-89: level-triggered, so a hold placed after its targets finished, or
// a noMorePulls report that arrived after the merge, lifts on this
// reconcile rather than waiting for an edge that has already passed.
// Every target must satisfy `finished`; the release goes through
// releaseBehind, so the wake, the notes and the re-hold of overlapping
// waiters are the same as any other lift.
if (b.holdBehind.length > 0) {
const projectId = b.projectId;
const targets = b.holdBehind.map((k) => store.find(k, projectId)).filter((t): t is Binding => t !== null);
if (targets.length === b.holdBehind.length && targets.every((t) => t.lastSnapshot !== null && finished(t, t.lastSnapshot))) {
for (const t of targets) await releaseBehind(t, howFinished(t));
b = store.binding(b.id) ?? b;
}
}
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
Expand Down Expand Up @@ -3676,7 +3701,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
}
cleared(false);
store.patch(b.id, { scheduleAsked: keys });
const overlap = conflicts.map((c) => `${c.with.entityKey} (${c.with.phase ?? "?"}) on ${c.on.join(", ")}`).join("; ");
const overlap = conflicts.map((c) => `${c.with.entityKey} (${c.with.phase ?? "?"}; ${pullFacts(c.with, c.observed, c.complete)}) on ${c.on.join(", ")}`).join("; ");
const x = store.escalate({
bindingId: b.id,
kind: "schedule",
Expand All @@ -3691,6 +3716,16 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
return x;
}

/**
* How a finished target's hold release reads (BB-89): the ending it
* reached, or the merge that finished it.
*/
function howFinished(b: Binding): string {
if (b.endedAt !== null) return `ended (${b.endReason ?? "ended"})`;
if (b.ending === "terminal") return `reached ${b.lastSnapshot?.entity.state ?? "a terminal state"}`;
return "merged its pull requests";
}

/**
* The work `b` was holding others behind has landed (`how`: its pull
* requests merged, or it ended): take it off every hold that waits behind
Expand Down Expand Up @@ -3756,6 +3791,21 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
return b.kind === "ticket" ? [...own, ...observed.pulls] : own;
}

/**
* 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
* steward has reported that no more are coming. Facts only; the wake draws
* no conclusion from them.
*/
function pullFacts(b: Binding, observed: Observed | null, complete: boolean): string {
if (observed === null) return "pull requests not read, no more pull requests: not reported";
const pulls = observedPulls(b, observed);
const merged = pulls.filter((p) => p.pull?.merged === true).length;
const open = pulls.filter((p) => p.pull?.merged !== true && lc(p.state) !== "closed").length;
const facts = `${open} open, ${merged} merged, no more pull requests: ${b.noMorePulls === true ? "reported" : "not reported"}`;
return complete ? facts : `${facts} (read incomplete)`;
}

/**
* File or refresh the user's `approve-spec` need for a binding. The plugin
* files it when the gate waits and the approver is the user (`approval: me`),
Expand Down
18 changes: 13 additions & 5 deletions plugins/stewards/skills/stewards/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ Two tools are yours; everything else you do with the tools every thread has.
You may put your own binding on hold (`steward_update { hold }`), for
instance when the user says to halt, but only your parent or the project
steward lifts it. A hold placed `behind` other bindings lifts itself, and
wakes you, once each has merged its pull requests or ended.
wakes you, once each is finished — its pull requests merged and it ended,
terminal, or reported `noMorePulls` — checked on every look, so a hold
placed after a merge still lifts on the next one.
- **Report what you touch, and let the project steward schedule you.** As
soon as the spec names the repos and paths the work changes, report them
with `steward_report { touches: ["<repo>/<path>", …] }` (repo directory
Expand Down Expand Up @@ -273,10 +275,16 @@ ticket that touches what another is building or reviewing reaches you as a
`steward_update { binding, hold: "<why>", behind: [<keys>] }` holds it
behind that work and closes the request, and the hold lifts itself, waking
the steward, once every binding named has merged its pull requests or
ended. The lift wakes you only when that work is not finished; when its
ticket is terminal with its pull requests merged, or its steward reported
`noMorePulls`, it is a note on the held binding and your next digest
instead. If one lift frees two waiters that overlap each other, the first
ended. The lift is by state: it is re-checked on every look, so a hold
placed after a merge, or a `noMorePulls` report that arrives after it,
lifts on the next look rather than waiting for the deploy. The lift wakes
you only when that work is not finished; when its ticket is terminal with
its pull requests merged, or its steward reported `noMorePulls`, it is a
note on the held binding and your next digest instead. The `schedule`
escalation names each conflicting binding's phase and its pull request
facts — how many linked pull requests are open and merged, and whether
`noMorePulls` was reported — so you can see merged-but-open work before
deciding. If one lift frees two waiters that overlap each other, the first
by priority then age is released and the rest wait behind it. When the
overlap is harmless, or the new ticket matters more (then
hold the other one instead), let it start with `guidance` ("Start now: …")
Expand Down
Loading