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
71 changes: 71 additions & 0 deletions plugins/stewards/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,77 @@ describe("a Linear ticket steward", () => {
expect(interest.find((c) => c.pluginId === "github-watcher")?.input).toEqual({ units: ["acme/web"], entities: [], dependabot: [], deployment: [] });
});

// BB-111: `ensureProjectSteward` spawns the project steward with an
// environment that attaches asynchronously, so a read taken at once is null
// and the first stewards of a new project got their own environment. The
// first steward now waits for the project steward's environment.
describe("BB-111: waiting for the project steward's environment", () => {
const fastWait = { debounceMs: 1, nightlyStaggerMs: 0, environmentPollMs: 1, environmentWaitMs: 500 };
const projectThread = (t: Awaited<ReturnType<typeof setup>>) => t.spawns[0]!.id;
const stewardSpawn = (t: Awaited<ReturnType<typeof setup>>) => t.spawns.find((s) => s.parentThreadId === projectThread(t));

it("reuses the environment once the project steward's provider attaches it", async () => {
const t = await setup({ timing: fastWait });
const originalGet = t.sdk.threads.get;
let reads = 0;
t.harness.sdk.stub("threads.get", async (args: { threadId: string }) => {
const base = await originalGet(args);
if (args.threadId !== "thr_1") return base;
reads += 1;
// The provider attaches the environment after the thread exists: the
// first read has no id, the next has it ready.
return (reads === 1 ? { ...base, environmentId: null, environment: null } : base) as unknown;
});
t.entities.set("uuid-12", issue());
await t.ring("linear", "uuid-12");
expect(reads).toBe(2);
expect(stewardSpawn(t)).toMatchObject({ environment: { type: "reuse", environmentId: "env1" } });
});

it("falls back to a fresh environment when the wait times out, and logs it", async () => {
const t = await setup({ timing: { ...fastWait, environmentWaitMs: 20 } });
const originalGet = t.sdk.threads.get;
t.harness.sdk.stub("threads.get", async (args: { threadId: string }) => {
const base = await originalGet(args);
if (args.threadId !== "thr_1") return base;
return { ...base, environmentId: null, environment: null } as unknown;
});
t.entities.set("uuid-12", issue());
await t.ring("linear", "uuid-12");
expect(stewardSpawn(t)).toMatchObject({ environment: { type: "provider", environmentProviderId: "multi-repo-workspace" } });
expect(t.harness.logEntries.some((e) => e.level === "info" && e.message.includes("timed out"))).toBe(true);
});

it("falls back to a fresh environment when the project steward's environment is destroyed, and logs it", async () => {
const t = await setup({ timing: fastWait });
const originalGet = t.sdk.threads.get;
t.harness.sdk.stub("threads.get", async (args: { threadId: string }) => {
const base = await originalGet(args);
if (args.threadId !== "thr_1") return base;
return { ...base, environment: { ...((base as { environment?: Record<string, unknown> }).environment ?? {}), status: "destroyed" } } as unknown;
});
t.entities.set("uuid-12", issue());
await t.ring("linear", "uuid-12");
expect(stewardSpawn(t)).toMatchObject({ environment: { type: "provider", environmentProviderId: "multi-repo-workspace" } });
expect(t.harness.logEntries.some((e) => e.level === "info" && e.message.includes("destroyed"))).toBe(true);
});

it("reuses an already-ready environment without waiting", async () => {
// A long bound: a bug that waits would hold the test open until it times out.
const t = await setup({ timing: { ...fastWait, environmentWaitMs: 60_000 } });
const originalGet = t.sdk.threads.get;
let reads = 0;
t.harness.sdk.stub("threads.get", async (args: { threadId: string }) => {
if (args.threadId === "thr_1") reads += 1;
return originalGet(args);
});
t.entities.set("uuid-12", issue());
await t.ring("linear", "uuid-12");
expect(reads).toBe(1);
expect(stewardSpawn(t)).toMatchObject({ environment: { type: "reuse", environmentId: "env1" } });
});
});

it("retries a lost adoption wake: a binding left with no thread is woken as adopted on the next reconcile (BB-92)", async () => {
const t = await setup({ timing: { debounceMs: 1, nightlyStaggerMs: 0, configTtlMs: 0 } });
// Let the startup sweep finish before counting config reads, so the only
Expand Down
37 changes: 36 additions & 1 deletion plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export interface Timing {
missedReportsForStuck: number;
/** How far back a user's words are looked for when an approval is recorded. */
approvalLookbackMs: number;
/** How often `spawnSteward` re-checks the project steward's environment while it attaches (BB-111). */
environmentPollMs: number;
/** How long `spawnSteward` waits for that environment before giving the steward its own (BB-111). */
environmentWaitMs: number;
}

export const DEFAULT_TIMING: Timing = {
Expand All @@ -141,6 +145,8 @@ export const DEFAULT_TIMING: Timing = {
endingTimeoutMs: 60 * 60_000,
missedReportsForStuck: 3,
approvalLookbackMs: 24 * 60 * 60_000,
environmentPollMs: 1000,
environmentWaitMs: 60_000,
};

/** BB-55: the backstop wake when a report passes the ready gate and the turn ends with no worker started. */
Expand Down Expand Up @@ -1830,11 +1836,40 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
kept.add(threadId);
}

/**
* The project steward's environment, for a new steward to share, waiting
* for it to attach. `ensureProjectSteward` returns before the provider has
* created it, so a read taken at once is null and the first stewards of a
* new project would each get their own environment (BB-111). Polls until
* the environment has an id and is ready; an already-ready one returns on
* the first poll. Returns null on timeout, or when the environment is
* destroyed, so the caller gives the steward a fresh one.
*/
async function sharedEnvironment(projectThread: string, entityKey: string): Promise<string | null> {
// A bounded number of polls, so the wait is deterministic and injectable
// in tests; the last poll is not followed by a sleep.
const polls = Math.max(1, Math.ceil(timing.environmentWaitMs / timing.environmentPollMs));
for (let poll = 0; poll < polls; poll += 1) {
const info = await threadInfo(projectThread).catch(() => null);
if (info !== null) {
if (environmentGone(info)) {
bb.log.info(`stewards: the project steward's environment is destroyed; giving ${entityKey}'s steward its own`);
return null;
}
// An environment id with no readiness field counts as ready.
if (info.environmentId !== null && (info.environmentStatus === null || info.environmentStatus === "ready")) return info.environmentId;
}
if (poll + 1 < polls) await new Promise((resolve) => setTimeout(resolve, timing.environmentPollMs));
}
bb.log.info(`stewards: timed out waiting up to ${timing.environmentWaitMs}ms for the project steward's environment; giving ${entityKey}'s steward its own`);
return null;
}

async function spawnSteward(b: Binding, config: ProjectConfig, playbook: Playbook, prompt: string): Promise<string | null> {
const projectThread = await ensureProjectSteward(config);
// Stewards decide and delegate; they need no checkout of their own. They
// share the project steward's environment, and workers get fresh ones.
const env = projectThread === null ? null : (await threadInfo(projectThread))?.environmentId ?? null;
const env = projectThread === null ? null : await sharedEnvironment(projectThread, b.entityKey);
const sectionId = await projectSection(b.projectId);
const base = {
projectId: b.projectId,
Expand Down
Loading