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
11 changes: 11 additions & 0 deletions plugins/stewards/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,17 @@ A **lesson** makes the answer a record and, for most of them, lands it.
steward is woken with the answer and asked whether it is a general
judgement: the answer is a lesson candidate for its next turn. A plain
question with no escalation behind it wakes nobody.
- **Counts (BB-35 step 2).** A `steward_update { resolve }` carries
`cites: [lesson ids]` — the lessons the decision rested on — and the wake
says a decision must cite what it was decided from. The plugin records
`hits` (one per distinct binding), `corrections` (the owner's answer or
unsigned comment on the entity differing from the steward's recommendation
on a decision that cited the lesson), and `lastCitedAt`, keeping the cited
bindings and the escalation's cited lessons. This step only records:
nothing is promoted, retired or written to `.bb` from these counts yet.
Only the owner's own words, or this project's steward or worker threads,
may create, cite or correct a lesson; a signed or non-owner comment never
does.
- **Procedure lessons self-merge.** Playbook procedure, sequencing and
conventions are what the project steward may already decide. It applies
the text to the file in its `.bb` checkout, commits, and publishes with
Expand Down
5 changes: 4 additions & 1 deletion plugins/stewards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ bb. [DESIGN.md](DESIGN.md) is the design; this is how it is built and run.
escalation on the Stewards page (or from a thread) wakes the project
steward with that answer as a lesson candidate: if it is a general
judgement the playbook should carry, the project steward records it as a
lesson. `bb stewards lessons [--all]`
lesson. A resolve carries `cites: [lesson ids]` — the lessons it rested on
— and the plugin counts `hits` (once per distinct binding), `corrections`
and `lastCitedAt`. Counts only for now: nothing is promoted, retired or
written to `.bb` from them yet. `bb stewards lessons [--all]`
lists them, `bb stewards drop-lesson <id>` drops one. DESIGN.md §20.
- **The nightly pass** (01:00 in the project's time zone by default, done by
02:45 so bb-gate's 03:00 restarts do not cut a turn) gives every open
Expand Down
31 changes: 29 additions & 2 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 } from "./config.js";
import { comment, issue, pull } from "./fixtures.js";
import { approvalWanted, anyWorkerInFlight, asksForOpenApproval, classifyApproval, compactDue, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, isCommentNews, lifecycle, markerLaneChange, namesUserAsBlocker, ownerApproval, prGate, resolveChecklistItem, reviewMarker, specHash, stuckDue, unheardWait, workerInFlight } from "./reconcile.js";
import { approvalWanted, answerDiffers, anyWorkerInFlight, asksForOpenApproval, classifyApproval, commentDiffersFromRecommendation, compactDue, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, gateState, isCommentNews, lifecycle, markerLaneChange, namesUserAsBlocker, ownerApproval, prGate, resolveChecklistItem, reviewMarker, specHash, stuckDue, unheardWait, workerInFlight } from "./reconcile.js";
import { readyStateSchema } from "./store.js";
import { GITHUB_REVIEWS, LINEAR_TICKETS } from "./templates.js";

Expand Down Expand Up @@ -177,7 +177,7 @@ describe("a worker in flight between its turns (BB-11)", () => {

describe("waits nobody hears", () => {
const status = (blockedOn: string | null) => ({ phase: "Todo", summary: "s", nextAction: "n", actor: "other" as const, blockedOn, requeueAfterMs: null, conditions: [], resources: { workers: [], pullRequests: [], children: [] }, at: 1 });
const open = [{ id: "esc_1", bindingId: "b", kind: "decision" as const, question: "q", meanwhile: "m", route: "project" as const, recommendation: null, choices: [], status: "open" as const, resolution: null, createdAt: 1, resolvedAt: null, deliveredAt: null, projectId: null, origin: { threadId: null, bindingId: null, chain: [] }, resolutionKind: null, url: null, data: {}, answeredBy: null, forwardedFrom: null, headSha: null, isQuestion: false }];
const open = [{ id: "esc_1", bindingId: "b", kind: "decision" as const, question: "q", meanwhile: "m", route: "project" as const, recommendation: null, choices: [], status: "open" as const, resolution: null, createdAt: 1, resolvedAt: null, deliveredAt: null, projectId: null, origin: { threadId: null, bindingId: null, chain: [] }, resolutionKind: null, url: null, data: {}, answeredBy: null, forwardedFrom: null, headSha: null, isQuestion: false, citedLessons: [], correctionsCounted: false }];

it("flags a status blocked on the project steward or the user with no escalation open", () => {
expect(unheardWait(status("project steward's approval of the new spec"), [])).toMatch(/project steward/u);
Expand Down Expand Up @@ -422,3 +422,30 @@ describe("a blockedOn naming the user", () => {
expect(namesUserAsBlocker("sam's review")).toBe(false);
});
});

// BB-35 step 2: the citation counters. A correction is the owner's own words
// differing from the steward's recommendation on a decision that cited a
// lesson; only the owner's own unsigned comment counts.
describe("a correction from a comment", () => {
it("is the connected owner's own unsigned words differing from the recommendation", () => {
const mine = comment({ author: "jane", body: "Do it the other way.", signedBy: null });
expect(commentDiffersFromRecommendation(mine, "jane", "Do it the recommended way.")).toBe(true);
expect(commentDiffersFromRecommendation(mine, "jane", "Do it the other way.")).toBe(false);
expect(commentDiffersFromRecommendation(mine, "jane", null)).toBe(false);
expect(commentDiffersFromRecommendation(mine, null, "Do it the recommended way.")).toBe(false);
});

it("never counts someone else's comment, or a signed one", () => {
// A non-owner's words are information, not an instruction.
expect(commentDiffersFromRecommendation(comment({ author: "Sam", body: "No, do it the other way." }), "jane", "Do it the recommended way.")).toBe(false);
// A signed comment belongs to a thread, however it reads.
expect(commentDiffersFromRecommendation(comment({ author: "jane", body: "No, do it the other way.", signedBy: "thr_w" }), "jane", "Do it the recommended way.")).toBe(false);
});

it("treats an empty side as no difference", () => {
expect(answerDiffers("", "Do it.")).toBe(false);
expect(answerDiffers("Do it.", "")).toBe(false);
expect(answerDiffers(" do it. ", "Do it.")).toBe(false);
expect(answerDiffers("Don't do it.", "Do it.")).toBe(true);
});
});
34 changes: 34 additions & 0 deletions plugins/stewards/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@ export function newHumanComments(entity: EntitySnapshot, cursor: string, own: Re
return entity.comments.filter((c) => c.createdAt > cursor && (c.signedBy === null || !own.has(c.signedBy)));
}

/**
* Whether an answer says something other than the recommendation, whitespace-
* and case-insensitively. An empty side never differs: there is nothing to
* reverse. A leading agreement adds nothing, and a negation on only one side
* flips the meaning. Used by the citation counters, where "differs" is what
* makes a correction (BB-35 step 2).
*/
const NEGATION = /(?:\b(?:no|not|never|without|avoid|reject|decline|stop|keep)\b|n't\b)/iu;
const AGREEMENT = /^(?:yes|yep|yeah|agreed?|ok(?:ay)?|sure|correct|confirmed|approved|lgtm|sounds good|do it|please do|go ahead)[\s,.:;—-]+/iu;
const words = (s: string): Set<string> => new Set(s.split(/[^a-z0-9']+/u).filter((w) => w !== ""));
const subset = (x: Set<string>, y: Set<string>): boolean => [...x].every((w) => y.has(w));
export function answerDiffers(answer: string, recommendation: string): boolean {
const a = answer.replace(/\s+/gu, " ").trim().toLowerCase();
const r = recommendation.replace(/\s+/gu, " ").trim().toLowerCase();
if (a === "" || r === "") return false;
if (a === r) return false;
if (NEGATION.test(a) !== NEGATION.test(r)) return true;
const aw = words(a.replace(AGREEMENT, ""));
const rw = words(r);
return !subset(rw, aw) && !subset(aw, rw);
}

/**
* Whether a comment is the connected owner's own unsigned words that differ
* from the recommendation. Only the owner's own unsigned comment can correct a
* lesson: a signed comment belongs to a thread, and anyone else's words — on
* Linear or GitHub — never do (BB-35 step 2).
*/
export function commentDiffersFromRecommendation(c: SnapshotComment, viewer: string | null, recommendation: string | null): boolean {
if (viewer === null || recommendation === null || c.signedBy !== null) return false;
if (c.author.toLowerCase() !== viewer.toLowerCase()) return false;
return answerDiffers(c.body, recommendation);
}

export const newestCommentAt = (observed: Observed): string =>
[observed.entity, ...observed.pulls].flatMap((e) => e.comments.map((c) => c.createdAt)).reduce((a, b) => (a > b ? a : b), "");

Expand Down
62 changes: 62 additions & 0 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,68 @@ describe("questions to the user", () => {
});
});

// BB-35 step 2: a resolve cites the lessons it rested on, and the plugin
// counts hits, corrections and the last citation. Counts only: no tier and
// no transition, and nothing is written to `.bb`.
describe("citations and counts (BB-35 step 2)", () => {
/** A project with a recorded lesson and an escalation the project steward forwarded. */
async function cited() {
const t = await adopted();
const recorded = await t.tool("steward_update", { lesson: { text: "Auth is the plugin's.", file: "AGENTS.md", scope: "procedure" } }, t.project);
const lesson = /(lsn_[\w-]+)/u.exec(recorded.text)![1]!;
await t.tool("steward_report", { phase: "Todo", summary: "Scope unclear.", nextAction: "Wait", actor: "other", escalate: { kind: "decision", question: "Include SSO?", meanwhile: "Wait." } }, t.steward);
const esc = /(esc_[\w-]+) from ENG-12/u.exec(t.sends.filter((s) => s.threadId === t.project).at(-1)!.text)![1]!;
const forwarded = await t.tool("steward_update", { binding: "ENG-12", forward: esc, recommendation: "Leave SSO out.", cites: [lesson] }, t.project);
const qst = /(qst_[\w-]+)/u.exec(forwarded.text)![1]!;
return { ...t, lesson, esc, qst };
}

it("counts a citation once per distinct binding and keeps it on the escalation", async () => {
const t = await cited();
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ hits: 1, corrections: 0, lastCitedAt: expect.any(Number), citedBindings: [t.hooks.store.find("ENG-12")!.id] });
expect(t.hooks.store.escalation(t.esc)).toMatchObject({ citedLessons: [t.lesson] });
// A second citation from the same binding refreshes the time but is not a new hit.
await t.tool("steward_update", { binding: "ENG-12", guidance: "Leave SSO out.", resolve: t.esc, cites: [t.lesson] }, t.project);
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ hits: 1 });
});

it("ignores an unknown lesson id and still resolves", async () => {
const t = await cited();
const closed = await t.tool("steward_update", { binding: "ENG-12", guidance: "Leave SSO out.", resolve: t.esc, cites: ["lsn_nope", t.lesson] }, t.project);
expect(closed.error).toBe(false);
expect(closed.text).toMatch(/not counted/u);
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ hits: 1 });
});

it("counts a correction when the owner's page answer differs from the recommendation", async () => {
const t = await cited();
expect(await t.harness.callRpc("stewardsAnswerQuestion", { questionId: t.qst, answer: "No, keep SSO." }, client)).toMatchObject({ ok: true });
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ corrections: 1 });
expect(t.hooks.store.escalation(t.esc)).toMatchObject({ correctionsCounted: true });
});

it("does not count a correction when the owner agrees with the recommendation", async () => {
const t = await cited();
expect(await t.harness.callRpc("stewardsAnswerQuestion", { questionId: t.qst, answer: "Agreed, leave SSO out." }, client)).toMatchObject({ ok: true });
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ corrections: 0 });
});

it("counts the owner's own unsigned comment as a correction", async () => {
const t = await cited();
t.entities.set("uuid-12", issue({ comments: [comment({ author: "Jane", body: "Actually, keep SSO.", signedBy: null })] }));
await t.ring("linear", "uuid-12");
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ corrections: 1 });
});

it("never counts a non-owner's comment as a correction", async () => {
const t = await cited();
t.entities.set("uuid-12", issue({ comments: [comment({ author: "Sam", body: "Actually, keep SSO.", signedBy: null })] }));
await t.ring("linear", "uuid-12");
expect(t.hooks.store.lesson(t.lesson)).toMatchObject({ corrections: 0 });
expect(t.hooks.store.escalation(t.esc)).toMatchObject({ correctionsCounted: false });
});
});

// BB-13: one spec approval, from any surface. The page answer, the project
// steward's words, an unsigned comment on the entity, and the user's own
// amendment all approve the same gate.
Expand Down
Loading
Loading