diff --git a/plugins/stewards/DESIGN.md b/plugins/stewards/DESIGN.md index db4523cf..265fc034 100644 --- a/plugins/stewards/DESIGN.md +++ b/plugins/stewards/DESIGN.md @@ -628,7 +628,8 @@ tuning it against the BB and bithub stewards is a follow-up, not this design. A worker is recognized by the `thread.created` handler by its lifecycle owner or its parent — whichever the skill that spawned it set — and never as -a reviewer, which is a child of a worker. Before +a reviewer, which follows a worker as its lifecycle owner (BB-323) or its +parent. Before the runaway guard, the handler compares the worker's provider, model, reasoning level and environment provider with the playbook's `models.worker` (or the binding's override) and the project's environment. On a mismatch it @@ -1059,7 +1060,10 @@ deployment: # where a merge is confirmed live (BB-53); omi --parent-self --parent-notifications off --lifecycle-owner-thread "$BB_THREAD_ID" …`; the plugin recognizes them by that owner or their parent, and a worker an older steward left with only the - owner is still recognized. `parentNotifications: off` keeps a worker's + owner is still recognized. A reviewer is started from its worker with that + worker as lifecycle owner and no parent (BB-323), because bb core refuses a + child for an active Pi parent; the plugin recognizes it by that owner or its + parent. `parentNotifications: off` keeps a worker's finish from waking the steward: its final message arrives in the next wake, which lists finished workers. The worker nests under its steward's row, because bb's list trees by parent thread. A finished worker's thread is archived once the reconcile that diff --git a/plugins/stewards/README.md b/plugins/stewards/README.md index 320a5782..0b698075 100644 --- a/plugins/stewards/README.md +++ b/plugins/stewards/README.md @@ -232,10 +232,12 @@ bb. [DESIGN.md](DESIGN.md) is the design; this is how it is built and run. you is left unread. - **Workers** are children of their steward with their own checkout, one per bounded task, spawned with `--parent-self --parent-notifications off - --lifecycle-owner-thread`. A reviewer is a child of the worker that started - it, one per review round. A worker nests under its steward's row and a - reviewer under its worker's, because bb's list trees by parent thread; the - parent is not woken on a child's turn. The plugin only ever archives a + --lifecycle-owner-thread`. A reviewer is started from the worker that + started it with that worker as its lifecycle owner and no parent (BB-323), + one per review round. A worker nests under its steward's row and a reviewer + under its worker's, because the sidebar trees a thread under its parent, or + under its lifecycle owner when it has none; the parent is not woken on a + child's turn. The plugin only ever archives a steward, worker or reviewer, never deletes one: deleting a parent thread deletes every descendant, archived ones included (get-bb/bb#6129), so deleting the project steward row by hand takes every steward, worker and @@ -316,8 +318,9 @@ bb. [DESIGN.md](DESIGN.md) is the design; this is how it is built and run. wins, then the playbook, then nothing. `bb stewards spawn-flags --role reviewer []` prints the full `bb thread spawn` command for a reviewer — hidden, full permissions, the reviewer model, started from the - worker thread that owns the pull request so `--parent-self` makes it that - worker's child — and fails clearly when no reviewer model is configured. + worker thread that owns the pull request with that worker as its lifecycle + owner and no parent (BB-323) — and fails clearly when no reviewer model is + configured. The default role is `worker`, whose output is unchanged. `bb stewards gate [--json]` reads the pull request fresh and prints the same verdict the page and the wake use (`prGate`): the lane, the latest diff --git a/plugins/stewards/read-counts.test.ts b/plugins/stewards/read-counts.test.ts index 28dd02ef..e060bfdf 100644 --- a/plugins/stewards/read-counts.test.ts +++ b/plugins/stewards/read-counts.test.ts @@ -165,7 +165,11 @@ describe("a reconcile pass over a fixed binding set (BB-297)", () => { // second gate) because the reconcile hands it the one assessment it just // built. The wake fell 16 → 15 for the same reason: `wakeInner` reads the // assessment the reconcile passed in, not `projectApprovals` again. - expect(reconcile).toEqual({ storeReads: 183, sourceCalls: 12, reconciles: 4, bindings: 4 }); + // BB-323 raised the reconcile reads 183 → 185: `archiveWorkers` asks + // `reviewersOf` for a finished worker's live reviewers as well as its + // descendants, so a parentless reviewer keeps the worker unarchived. That + // is the worker row and its binding, two store reads. + expect(reconcile).toEqual({ storeReads: 185, sourceCalls: 12, reconciles: 4, bindings: 4 }); expect(wake).toEqual({ storeReads: 15, sourceCalls: 0, wakes: 1 }); }); diff --git a/plugins/stewards/server.test.ts b/plugins/stewards/server.test.ts index 52946f46..ed069740 100644 --- a/plugins/stewards/server.test.ts +++ b/plugins/stewards/server.test.ts @@ -947,6 +947,53 @@ describe("a Linear ticket steward", () => { expect(result.bindings[0]!.reviewers).toBeUndefined(); }); + // BB-323: the printed reviewer command drops `--parent-self`, so a reviewer + // has no parent and names only its worker as lifecycle owner. It is + // recognized from that owner, tagged, superseded and never validated as a + // worker; a steward-owned or steward-parented thread is still validated. + it("recognizes a parentless reviewer by its lifecycle owner, never validates it, and supersedes it", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" })); + await t.ring("linear", "uuid-12"); + const steward = t.spawns[1]!.id; + const binding = t.hooks.store.open("p1")[0]!.id; + const stopped = () => t.harness.sdk.callsTo("threads.stop").map(([args]) => (args as { threadId: string }).threadId); + + await t.created("thr_w1", steward); + // The reviewer names only its lifecycle owner, with a model that + // validation would stop, so a missed recognition is visible. + await t.createdReviewer("thr_r1", "thr_w1", "Review round 1", { model: "claude-sonnet-5-5" }); + expect(t.metadata.get("thr_r1")).toEqual({ threadBriefs: false }); + expect(t.hooks.store.worker("thr_r1")).toBeNull(); + expect(stopped()).not.toContain("thr_r1"); + + // A newer round supersedes the older parentless reviewer. + await t.createdReviewer("thr_r2", "thr_w1", "Review round 2"); + expect(t.threads.get("thr_r1")!.archivedAt).not.toBeNull(); + expect(t.threads.get("thr_r2")!.archivedAt).toBeNull(); + + // The parented form is unchanged: a newer child supersedes the parentless + // one, and is itself never validated. + await t.created("thr_r3", "thr_w1", "Review round 3", { model: "claude-sonnet-5-5" }); + expect(t.threads.get("thr_r2")!.archivedAt).not.toBeNull(); + expect(t.threads.get("thr_r3")!.archivedAt).toBeNull(); + expect(t.hooks.store.worker("thr_r3")).toBeNull(); + + // A parentless thread whose lifecycle owner is a steward is still a worker, + // and a steward-parented thread on the wrong model is still stopped. + await t.createdOwned("thr_w2", steward); + expect(t.hooks.store.worker("thr_w2")).toMatchObject({ bindingId: binding, status: "running" }); + await t.created("thr_bad", steward, "ENG-12: Implement", { model: "claude-sonnet-5-5" }); + expect(t.hooks.store.worker("thr_bad")!.status).toBe("stopped"); + expect(stopped()).toContain("thr_bad"); + + // A thread whose parent is a worker but whose lifecycle owner is the + // steward is still validated as a worker, not skipped as a reviewer. + await t.created("thr_weird", "thr_w1", "ENG-12: Implement", { lifecycleOwnerThreadId: steward, model: "claude-sonnet-5-5" }); + expect(t.hooks.store.worker("thr_weird")!.status).toBe("stopped"); + expect(stopped()).toContain("thr_weird"); + }); + it("keeps a running worker's own pull request activity for the next turn instead of waking for it", async () => { const t = await setup(); const linked = { linkedPulls: [{ key: "acme/web#7", url: "https://github.com/acme/web/pull/7" }] }; @@ -4386,6 +4433,29 @@ describe("archiving finished workers and reviewers (BB-47)", () => { expect(t.threads.get("thr_r2")!.archivedAt).toBeNull(); }); + it("keeps a worker whose parentless reviewer is still unarchived, and archives it once the reviewer is (BB-323)", async () => { + const t = await setup(); + t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" })); + await t.ring("linear", "uuid-12"); + const steward = t.spawns[1]!.id; + await t.created("thr_w1", steward); + await t.createdReviewer("thr_r1", "thr_w1", "Review round 1"); + + // The worker ends its turn without a waiting phrase while its parentless + // reviewer still runs: the reviewer is not a descendant, but it keeps the + // worker unarchived (bb would archive its lifecycle tree with it). + t.threads.get("thr_r1")!.status = "active"; + await t.idle("thr_w1", "Spawned the reviewer."); + expect(t.threads.get("thr_r1")!.archivedAt).toBeNull(); + expect(t.threads.get("thr_w1")!.archivedAt).toBeNull(); + + // The reviewer is consumed, and the worker's next turn ends: both leave. + t.threads.get("thr_r1")!.status = "idle"; + await t.idle("thr_w1", "Addressed the review and pushed."); + expect(t.threads.get("thr_r1")!.archivedAt).not.toBeNull(); + expect(t.threads.get("thr_w1")!.archivedAt).not.toBeNull(); + }); + it("archives a finished worker after the reconcile that consumed its report, unless it needs the user", async () => { const t = await setup(); t.entities.set("uuid-12", issue({ state: "In Progress", stateType: "started" })); @@ -13549,11 +13619,12 @@ describe("the reviewer role and the gate and merge commands (BB-133)", () => { expect(missing.exitCode).toBe(1); expect(missing.stderr).toMatch(/No reviewer model/u); - // The worker default is unchanged. + // The worker default is unchanged, and keeps `--parent-self`. const worker = await t.harness.behavior.runCli(["spawn-flags", "ENG-12", "--project", "p1"]); expect(worker.stdout).toContain("--model claude-opus-5-5"); expect(worker.stdout).not.toContain("--visibility hidden"); expect(worker.stdout).not.toContain("--permission-mode"); + expect(worker.stdout).toContain("--parent-self"); // A playbook reviewer model, then a binding override, both read back. t.files[playbookPath("linear-tickets")] = LINEAR_TICKETS.replace(" worker: claude/claude-opus-5-5/high", " worker: claude/claude-opus-5-5/high\n reviewer: pi/fireworks/accounts/fireworks/models/glm-5p3"); @@ -13563,6 +13634,10 @@ describe("the reviewer role and the gate and merge commands (BB-133)", () => { expect(reviewer.stdout).toContain("--visibility hidden --permission-mode full"); expect(reviewer.stdout).toContain("--provider pi --model fireworks/accounts/fireworks/models/glm-5p3"); expect(reviewer.stdout).toContain('--title "Review ENG-12: "'); + // BB-323: the reviewer keeps the worker as lifecycle owner and has no + // parent, so it can run from an active worker. + expect(reviewer.stdout).toContain('--lifecycle-owner-thread "$BB_THREAD_ID"'); + expect(reviewer.stdout).not.toContain("--parent-self"); const update = await t.tool("steward_update", { binding: "ENG-12", reviewerModel: "pi/fireworks/accounts/fireworks/models/other-reviewer" }, t.spawns[0]!.id); expect(update.error, update.text).toBe(false); @@ -13594,6 +13669,7 @@ describe("the reviewer role and the gate and merge commands (BB-133)", () => { expect(out.exitCode).toBe(0); expect(out.stdout).toContain('--title "Review ENG-12: "'); expect(out.stdout).toContain("--visibility hidden --permission-mode full"); + expect(out.stdout).not.toContain("--parent-self"); }); it("prints the gate verdict and exits 0 when met, 1 when not, with --json", async () => { diff --git a/plugins/stewards/server.ts b/plugins/stewards/server.ts index c62aa1f0..1330ec31 100644 --- a/plugins/stewards/server.ts +++ b/plugins/stewards/server.ts @@ -3563,7 +3563,9 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P /** * The binding a newly created thread belongs to: a worker's lifecycle owner * or parent when that is a steward, or the steward of the worker a reviewer - * is a child of. Null for a thread this plugin does not own. Local store + * follows. A reviewer names its worker as lifecycle owner (BB-323, no + * parent) or, from an older worker, as parent, so the same walk finds it + * either way. Null for a thread this plugin does not own. Local store * lookups only, so an unrelated thread costs nothing. */ function spawnedOwner(thread: { lifecycleOwnerThreadId: string | null; parentThreadId: string | null }): Binding | null { @@ -3696,26 +3698,53 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P } /** - * The worker a thread is a child of, if any. A reviewer is spawned with - * its worker as parent (`--parent-self`, what build-tickets uses). Null - * for a worker itself and for anything else. + * The worker a reviewer belongs to, if any. A reviewer is started from the + * worker thread it reviews for, which is its lifecycle owner (BB-323, the + * printed command, no parent) or, from an older worker, its parent + * (`--parent-self`). Recognized only from the store's own record: a + * parentless thread counts as a reviewer only when its lifecycle owner is a + * worker of an open binding, never from the missing parent alone. Null for + * a worker itself and for anything else. */ - function reviewerOwner(thread: { parentThreadId: string | null }): Worker | null { - return thread.parentThreadId === null ? null : store.worker(thread.parentThreadId); + function reviewerOwner(thread: { lifecycleOwnerThreadId: string | null; parentThreadId: string | null }): Worker | null { + if (thread.parentThreadId !== null) { + const parent = store.worker(thread.parentThreadId); + if (parent !== null) return parent; + } + if (thread.parentThreadId !== null || thread.lifecycleOwnerThreadId === null) return null; + const owner = store.worker(thread.lifecycleOwnerThreadId); + if (owner === null) return null; + const binding = store.binding(owner.bindingId); + return binding !== null && binding.endedAt === null ? owner : null; } /** - * A worker's reviewer threads, as bb lists them: its children, hidden or + * A worker's reviewer threads, as bb lists them: its children and the + * parentless threads that name it as lifecycle owner (BB-323), hidden or * not, not archived. Null when bb could not be asked, so a caller that must * not guess — archiving a worker that might still have a reviewer — can * tell "none" from "unknown". Kept per worker for the two reconcile paths - * that need a reviewer's own status or its direct-children relationship; - * the batch paths use `descendantsOfMany` (BB-100). + * that need a reviewer's own status or its direct relationship; the batch + * paths use `descendantsOfMany` (BB-100). */ async function reviewersOf(workerThreadId: string): Promise | null> { try { const children = await bb.sdk.threads.list({ parentThreadId: workerThreadId, includeHidden: true }); - return children.filter((c) => c.archivedAt === null).map((c) => ({ id: c.id, status: String(c.status) })); + // bb has no lifecycle-owner filter, so a parentless reviewer is found + // by reading the worker's project's root threads. A worker the store + // does not know owns no such reviewer and costs no second read. + const owner = store.worker(workerThreadId); + const binding = owner === null ? null : store.binding(owner.bindingId); + const roots = binding === null ? [] : await bb.sdk.threads.list({ projectId: binding.projectId, hasParent: false, includeHidden: true }); + const seen = new Set(); + const out: Array<{ id: string; status: string }> = []; + for (const t of [...children, ...roots]) { + if (t.archivedAt !== null || seen.has(t.id)) continue; + if (t.parentThreadId !== workerThreadId && !(t.parentThreadId === null && t.lifecycleOwnerThreadId === workerThreadId)) continue; + seen.add(t.id); + out.push({ id: t.id, status: String(t.status) }); + } + return out; } catch (error) { bb.log.warn(`stewards: could not list the reviewers of worker ${workerThreadId}: ${errorMessage(error)}`); return null; @@ -3793,13 +3822,16 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P /** * Archive the given finished workers unless one still has an unarchived - * child. A worker whose reviewer is still unarchived must stay: bb core - * releases a thread's unarchived children from it when it is archived, - * so archiving the worker would clear the reviewer's parent: it could - * no longer wake the worker, and no later pass could find it to archive - * it. One batched descendant lookup covers every candidate (BB-100); a - * listing that failed is "unknown", so all the workers wait rather than - * risking an orphan (BB-47). + * reviewer. A worker whose reviewer is still unarchived must stay: bb core + * archives a thread's whole lifecycle tree with it, and releases its + * unarchived children, so archiving the worker would take a parentless + * reviewer down with it and clear a parented reviewer's parent: neither + * could wake the worker, and no later pass could find it to archive it. + * One batched descendant lookup covers every candidate (BB-100), and a + * parentless reviewer is not a descendant, so the worker's live reviewers + * are read through `reviewersOf` as well; a listing that failed is + * "unknown", so all the workers wait rather than risking an orphan + * (BB-47). */ async function archiveWorkers(candidates: readonly Worker[]): Promise { if (candidates.length === 0) return; @@ -3807,6 +3839,10 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P if (descendants === null) return; for (const w of candidates) { if ((descendants.get(w.threadId) ?? []).length > 0) continue; + // A reviewer with only a lifecycle owner is not a descendant (BB-323); + // a failed lookup is "unknown", so the worker waits too. + const reviewers = await reviewersOf(w.threadId); + if (reviewers === null || reviewers.length > 0) continue; const info = await threadInfo(w.threadId).catch(() => null); if (info === null || info.deletedAt !== null || info.archivedAt !== null || busy(info.status)) continue; await quietly(`archive finished worker ${w.threadId}`, () => bb.sdk.threads.archive({ threadId: w.threadId })); @@ -8444,7 +8480,8 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P // recognized here through their lifecycle owner or their parent (the // steward) rather than a tool of their own. A worker an older steward left // with only the lifecycle owner is still recognized, and a nested worker - // is never taken for a reviewer: a reviewer's parent is a worker. + // is never taken for a reviewer: a reviewer follows a worker, as its + // lifecycle owner (BB-323) or its parent. /** * The post-spawn safety net: a worker whose provider, model, reasoning @@ -8529,14 +8566,21 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P // Stewards this plugin spawned (a promoted child under its parent) are not workers. if (thread.originPluginId === bb.pluginId) return; // A worker (its lifecycle owner or parent is a steward) or a reviewer (a - // child of a worker) is tagged for thread-briefs here, whichever way the - // skill that spawned it built the command. + // worker as lifecycle owner or parent) is tagged for thread-briefs here, + // whichever way the skill that spawned it built the command. if (spawnedOwner(thread) !== null) await tagThread(thread.id, THREAD_BRIEFS_OFF); - // A reviewer is a child of a worker. The newest round supersedes the - // older ones, so they are archived as it is created (BB-47). - const reviewerOf = reviewerOwner(thread); - if (reviewerOf !== null) await supersedeOlderReviewers(reviewerOf.threadId, thread.id); + // A reviewer follows a worker, as its lifecycle owner (BB-323) or its + // parent. The newest round supersedes the older ones, so they are + // archived as it is created (BB-47). const owner = [thread.lifecycleOwnerThreadId, thread.parentThreadId].map((t) => (t === null ? null : store.byThread(t))).find((b) => b !== null && b !== undefined && b.endedAt === null); + const reviewerOf = reviewerOwner(thread); + if (reviewerOf !== null) { + await supersedeOlderReviewers(reviewerOf.threadId, thread.id); + // A reviewer is never a worker, unless its lifecycle owner or parent is + // an open binding's steward: then it is still filed and validated as a + // worker. + if (owner === undefined || owner === null) return; + } if (owner === undefined || owner === null || store.worker(thread.id) !== null) return; const limit = (await settings.get()).workerSpawnsPerHour; const runaway = store.spawnsSince(owner.id, now() - 3_600_000) >= limit; @@ -13288,17 +13332,18 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P "spawn-flags": cliCommand({ summary: "The full runnable `bb thread spawn` command for a steward's workers or a worker's reviewer, from the playbook's model and the project environment", - description: "Use these flags as they are: they are the playbook's, not the steward's to choose. Defaults to the binding of the invoking thread. `--role reviewer` is run from the worker thread that owns the pull request, so `--parent-self` makes the reviewer that worker's child; it needs `models.reviewer` in the playbook or a binding override.", + description: "Use these flags as they are: they are the playbook's, not the steward's to choose. Defaults to the binding of the invoking thread. `--role reviewer` is run from the worker thread that owns the pull request, with that worker as the reviewer's lifecycle owner and no parent (BB-323); it needs `models.reviewer` in the playbook or a binding override.", positionals: [{ name: "binding", description: "The entity key (ENG-12, owner/repo#3) or binding id; the invoking thread's binding when left out", required: false }], options: { project: PROJECT_OPTION, role: { type: "string", description: "worker (the default) or reviewer" } }, async run(input, ctx) { const role = input.options.role ?? "worker"; if (role !== "worker" && role !== "reviewer") throw new PluginCliError(`Unknown role ${JSON.stringify(role)}: worker or reviewer.`, { code: "bad_role" }); - // A reviewer is spawned with `--parent-self` from the worker it - // reviews for, so the command only makes sense from that worker's - // thread; from a steward it would parent the reviewer to the - // steward and the plugin would stop it as a mis-spawned worker. - if (role === "reviewer" && ctx.threadId !== undefined && store.worker(ctx.threadId) === null) throw new PluginCliError("`--role reviewer` is run from the worker thread that owns the pull request, so the reviewer is that worker's child; run it there.", { code: "reviewer_not_from_worker" }); + // A reviewer is started from the worker it reviews for and takes + // that worker as its lifecycle owner, so the command only makes + // sense from that worker's thread; from a steward the reviewer + // would follow the steward and the plugin would stop it as a + // mis-spawned worker. + if (role === "reviewer" && ctx.threadId !== undefined && store.worker(ctx.threadId) === null) throw new PluginCliError("`--role reviewer` is run from the worker thread that owns the pull request, so the reviewer follows that worker; run it there.", { code: "reviewer_not_from_worker" }); const b = bindingFromCli(input.positionals.binding, input.options.project, ctx, { worker: true }); const config = await loadConfig(b.projectId); const playbook = config === null ? null : bindingPlaybook(config, b); diff --git a/plugins/stewards/skills/build-tickets/SKILL.md b/plugins/stewards/skills/build-tickets/SKILL.md index a7968c9f..a1d8cf21 100644 --- a/plugins/stewards/skills/build-tickets/SKILL.md +++ b/plugins/stewards/skills/build-tickets/SKILL.md @@ -178,17 +178,20 @@ wins and you correct the PR body. There is exactly one reviewer per PR. First check that there is none yet: no "Independent review running" comment on the PR (`gh pr view --repo --json comments --jq '.comments[].body' | grep -F 'Independent review running'`) -and no child thread -(`bb thread list --parent-thread "$BB_THREAD_ID" --include-hidden --json`; -children are hidden, so a list without `--include-hidden` is always -empty). If either shows one, use it and skip to The review loop. +and no reviewer thread of this worker +(`bb thread list --project "$BB_PROJECT_ID" --include-hidden --json | jq '[.[] | select(.parentThreadId == null and .lifecycleOwnerThreadId == env.BB_THREAD_ID)]'`; +the printed command starts the reviewer from this worker with this thread as +its lifecycle owner and no parent, and a reviewer is hidden, so the list +needs `--include-hidden`). If either shows one, use it and skip to The review +loop. Otherwise write the review prompt (below). The reviewer follows the `pr-reviewer` skill and needs the project's environment so it can run tests. Do not hand-roll its flags: `bb stewards spawn-flags --role reviewer` prints the full reviewer spawn command — hidden, full permissions, the project's reviewer model and environment, started from -this worker thread so `--parent-self` makes it your child. Its output ends +this worker thread with this thread as the reviewer's lifecycle owner and no +parent. Its output ends in a heredoc whose body is the placeholder ``; run the command with your review prompt in place of that line, and the `` in its title replaced by a short label. Add `--json` and keep the whole @@ -208,9 +211,9 @@ Author thread: @thread:$BB_THREAD_ID. After you post your review comment, end yo ``` Run the spawn once. If you cannot read an id, look again with the -`--include-hidden` list rather than spawning again: a second spawn is a -second reviewer posting its own rounds. A new child may sit in `pending` -behind the concurrency limit; that is normal. Comment on the PR once, +lifecycle-owner query above (`--include-hidden`) rather than spawning again: +a second spawn is a second reviewer posting its own rounds. A new reviewer +may sit in `pending` behind the concurrency limit; that is normal. Comment on the PR once, right away: "Independent review running in bb thread @thread:.", signed. diff --git a/plugins/stewards/test-world.ts b/plugins/stewards/test-world.ts index fa7279b2..c07d6b53 100644 --- a/plugins/stewards/test-world.ts +++ b/plugins/stewards/test-world.ts @@ -63,7 +63,7 @@ export function world() { /** A plain thrown failure for `sourceEnsureLabel`, to tell it from unavailable (BB-236). */ let labelEnsureThrow: string | null = null; const rpcCalls: Call[] = []; - const threads = new Map(); + const threads = new Map(); /** Every `threads.delete` the plugin asked for; the plugin must only ever archive. */ const deletes: string[] = []; const writes: Array<{ path: string; hostId?: string }> = []; @@ -491,15 +491,17 @@ export function world() { if (t.model === undefined) return { model: "claude-fable-5-1", reasoningLevel: "medium", serviceTier: "default", permissionMode: "auto", source: "client/turn/requested" }; return { model: t.model, reasoningLevel: t.reasoningLevel ?? "none", serviceTier: "default", permissionMode: "auto", source: "client/turn/requested" }; }, - list: async ({ sectionId, archived, parentThreadId }: { sectionId?: string; archived?: boolean; parentThreadId?: string }) => + list: async ({ sectionId, archived, parentThreadId, projectId, hasParent }: { sectionId?: string; archived?: boolean; parentThreadId?: string; projectId?: string; hasParent?: boolean }) => [...threads.entries()] .filter( ([, t]) => (sectionId === undefined || t.sectionId === sectionId) && (archived !== false || t.archivedAt === null) && - (parentThreadId === undefined || (t.parentThreadId ?? null) === parentThreadId), + (parentThreadId === undefined || (t.parentThreadId ?? null) === parentThreadId) && + (projectId === undefined || (t.projectId ?? "p1") === projectId) && + (hasParent === undefined || ((t.parentThreadId ?? null) !== null) === hasParent), ) - .map(([id, t]) => makeThreadResponse({ id, status: t.status as never, archivedAt: t.archivedAt, parentThreadId: t.parentThreadId ?? null, sectionId: t.sectionId ?? null })), + .map(([id, t]) => makeThreadResponse({ id, status: t.status as never, archivedAt: t.archivedAt, parentThreadId: t.parentThreadId ?? null, lifecycleOwnerThreadId: t.lifecycleOwnerThreadId ?? null, sectionId: t.sectionId ?? null })), experimental_listDescendants: async ({ threadIds, includeArchived, includeHidden }: { threadIds: string[]; includeArchived?: boolean; includeHidden?: boolean }) => { const descendants = (id: string): string[] => { const out: string[] = []; @@ -693,7 +695,7 @@ export async function setup(extra: Partial = {}, settings: Record< id: string, parent: string, title = "Implement", - over: { providerId?: string; model?: string | null; reasoningLevel?: string | null; environmentProviderId?: string | null; sectionId?: string | null; branchName?: string | null } = {}, + over: { providerId?: string; model?: string | null; reasoningLevel?: string | null; environmentProviderId?: string | null; sectionId?: string | null; branchName?: string | null; lifecycleOwnerThreadId?: string | null } = {}, ) => { const providerId = over.providerId ?? "claude-code"; const model = over.model === undefined ? "claude-opus-5-5" : over.model; @@ -702,16 +704,32 @@ export async function setup(extra: Partial = {}, settings: Record< // The worker's workspace branch; the pull() fixture's head names ENG-12 // (BB-239), so a worker owns it unless a test says otherwise (BB-52 rule 0). const branchName = over.branchName === undefined ? "bb/eng-12-retry" : over.branchName; - w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: parent, sectionId: over.sectionId ?? null, providerId, ...(model === null ? {} : { model }), ...(reasoningLevel === null ? {} : { reasoningLevel }), environmentProviderId, branchName }); - await harness.behavior.emitThreadEvent("thread.created", { thread: makeThreadResponse({ id, parentThreadId: parent, lifecycleOwnerThreadId: parent, title, originPluginId: null, providerId, sectionId: over.sectionId ?? null }) }); + const lifecycleOwner = over.lifecycleOwnerThreadId === undefined ? parent : over.lifecycleOwnerThreadId; + w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: parent, lifecycleOwnerThreadId: lifecycleOwner, sectionId: over.sectionId ?? null, providerId, ...(model === null ? {} : { model }), ...(reasoningLevel === null ? {} : { reasoningLevel }), environmentProviderId, branchName }); + await harness.behavior.emitThreadEvent("thread.created", { thread: makeThreadResponse({ id, parentThreadId: parent, lifecycleOwnerThreadId: lifecycleOwner, title, originPluginId: null, providerId, sectionId: over.sectionId ?? null }) }); await flush(); }; /** A top-level worker a steward started with only `--lifecycle-owner-thread`: no parent. */ const createdOwned = async (id: string, owner: string, title = "Implement", sectionId: string | null = null) => { - w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: null, sectionId, providerId: "claude-code", model: "claude-opus-5-5", reasoningLevel: "high", environmentProviderId: "multi-repo-workspace", branchName: "bb/eng-12-retry" }); + w.threads.set(id, { status: "active", archivedAt: null, visibility: "visible", parentThreadId: null, lifecycleOwnerThreadId: owner, sectionId, providerId: "claude-code", model: "claude-opus-5-5", reasoningLevel: "high", environmentProviderId: "multi-repo-workspace", branchName: "bb/eng-12-retry" }); await harness.behavior.emitThreadEvent("thread.created", { thread: makeThreadResponse({ id, parentThreadId: null, lifecycleOwnerThreadId: owner, title, originPluginId: null, providerId: "claude-code", sectionId }) }); await flush(); }; + /** A reviewer a worker started without `--parent-self` (BB-323): no parent, the worker as lifecycle owner. */ + const createdReviewer = async ( + id: string, + worker: string, + title = "Review", + over: { providerId?: string; model?: string | null; reasoningLevel?: string | null; environmentProviderId?: string | null } = {}, + ) => { + const providerId = over.providerId ?? "claude-code"; + const model = over.model === undefined ? "claude-opus-5-5" : over.model; + const reasoningLevel = over.reasoningLevel === undefined ? "high" : over.reasoningLevel; + const environmentProviderId = over.environmentProviderId === undefined ? "multi-repo-workspace" : over.environmentProviderId; + w.threads.set(id, { status: "active", archivedAt: null, visibility: "hidden", parentThreadId: null, lifecycleOwnerThreadId: worker, providerId, ...(model === null ? {} : { model }), ...(reasoningLevel === null ? {} : { reasoningLevel }), environmentProviderId, branchName: "bb/eng-12-retry" }); + await harness.behavior.emitThreadEvent("thread.created", { thread: makeThreadResponse({ id, parentThreadId: null, lifecycleOwnerThreadId: worker, title, originPluginId: null, providerId, sectionId: null }) }); + await flush(); + }; /** A steward-filed escalation the project steward has forwarded to the user, for tests. */ const escalationNeed = (fields: { key?: string; kind: "decide" | "configure" | "provide-secret"; url?: string; what: string; how?: string; variable?: string; purpose?: string; worker?: string; writeEnv?: string; setting?: string; page?: string; choices?: string[] }) => { const bindingId = hooks.store.find(fields.key ?? "ENG-12", "p1")!.id; @@ -738,5 +756,5 @@ export async function setup(extra: Partial = {}, settings: Record< }, }); }; - return { ...w, bb, harness, hooks, flush, ring, tool, list, idle, created, createdOwned, escalationNeed }; + return { ...w, bb, harness, hooks, flush, ring, tool, list, idle, created, createdOwned, createdReviewer, escalationNeed }; } diff --git a/plugins/stewards/wake.test.ts b/plugins/stewards/wake.test.ts index dd07d218..bfd9af62 100644 --- a/plugins/stewards/wake.test.ts +++ b/plugins/stewards/wake.test.ts @@ -657,18 +657,17 @@ describe("the re-approval diff in the project wake (BB-249)", () => { describe("the spawn command (BB-133)", () => { const flags = "--environment-provider multi-repo-workspace --provider claude-code --model claude-opus-5-5"; - it("is unchanged for the worker role, and adds the reviewer's hidden full-permission flags", () => { + it("is unchanged for the worker role, and drops --parent-self for the reviewer (BB-323)", () => { const worker = spawnCommand({ projectId: "p1", entityKey: "ENG-12", workerFlags: flags }); expect(worker).toContain('bb thread spawn --project p1 --parent-self --parent-notifications off --lifecycle-owner-thread "$BB_THREAD_ID" --environment-provider multi-repo-workspace --provider claude-code --model claude-opus-5-5 --title "ENG-12: " --prompt-file -'); expect(worker).not.toContain("--visibility hidden"); expect(worker).not.toContain("--permission-mode"); const reviewer = spawnCommand({ projectId: "p1", entityKey: "ENG-12", workerFlags: flags, role: "reviewer" }); - expect(reviewer).toContain("--visibility hidden --permission-mode full"); - expect(reviewer).toContain('--title "Review ENG-12: "'); - // The reviewer is started from the worker thread, so `--parent-self` makes - // it that worker's child, and the lifecycle owner stays the worker. - expect(reviewer).toContain("--parent-self --parent-notifications off --lifecycle-owner-thread \"$BB_THREAD_ID\""); + // BB-323: no `--parent-self` (bb core refuses a child for an active Pi + // parent); the worker stays the lifecycle owner. + expect(reviewer).toContain('bb thread spawn --project p1 --parent-notifications off --lifecycle-owner-thread "$BB_THREAD_ID" --visibility hidden --permission-mode full --environment-provider multi-repo-workspace --provider claude-code --model claude-opus-5-5 --title "Review ENG-12: " --prompt-file -'); + expect(reviewer).not.toContain("--parent-self"); }); }); diff --git a/plugins/stewards/wake.ts b/plugins/stewards/wake.ts index 0b76b1ff..fb51a12b 100644 --- a/plugins/stewards/wake.ts +++ b/plugins/stewards/wake.ts @@ -66,15 +66,17 @@ export const BRIEF_PREFIX = * The spawn command, shared by the wake, `bb stewards spawn-flags` and the * brief. A worker is the default and its output is unchanged; a reviewer * (BB-133) is hidden, runs with full permissions (Pi accepts only `full`) and - * is started from the worker thread it reviews for, so `--parent-self` makes - * it that worker's child and the lifecycle owner stays the worker. + * is started from the worker thread it reviews for, with that worker as its + * lifecycle owner and no parent (BB-323): bb core refuses a child for an + * active Pi parent, and a worker is active when it runs the command. */ export function spawnCommand(input: { projectId: string; entityKey: string; workerFlags: string; role?: "worker" | "reviewer" }): string { const reviewer = input.role === "reviewer"; + const parent = reviewer ? "" : " --parent-self"; const extra = reviewer ? " --visibility hidden --permission-mode full" : ""; const title = reviewer ? `Review ${input.entityKey}: ` : `${input.entityKey}: `; return [ - `bb thread spawn --project ${input.projectId} --parent-self --parent-notifications off --lifecycle-owner-thread "$BB_THREAD_ID"${extra}${input.workerFlags === "" ? "" : ` ${input.workerFlags}`} --title "${title}" --prompt-file - <<'EOF'`, + `bb thread spawn --project ${input.projectId}${parent} --parent-notifications off --lifecycle-owner-thread "$BB_THREAD_ID"${extra}${input.workerFlags === "" ? "" : ` ${input.workerFlags}`} --title "${title}" --prompt-file - <<'EOF'`, "", "EOF", ].join("\n");