diff --git a/packages/runner/src/autonomy.ts b/packages/runner/src/autonomy.ts index 503323f3..41c2c958 100644 --- a/packages/runner/src/autonomy.ts +++ b/packages/runner/src/autonomy.ts @@ -238,6 +238,19 @@ export async function notifyPm(db: Db, projectId: number, dedupeKey: string, tex // ── PEP-24: dispatcher ───────────────────────────────────────────────────────── +/** PEP-185: how many ready stories the «auto» dispatcher takes right now (0 while it can't start any: + * no subscription, limits, a recent failure, the WIP limit reached). The rest wait for a person. */ +export async function autoPickSlots(db: Db, project: Project, a: AutonomySettings, now = new Date()): Promise { + if (a.pick !== "auto") return 0; + const sessions = (await listSessions(db, { active: true })).filter((s) => s.projectId === project.id); + if (await projectAgentWarning(db, project.id, now)) return 0; + if (sessions.some((s) => s.status === "rate_limited") || (await projectPoolExhausted(db, project.id, now))) return 0; + if (((await getIdle(db, project.id)).failUntil ?? 0) > now.getTime()) return 0; + const working = await workingSessions(db, sessions); + const queued = (await queuedItems(db, { sessionIds: sessions.map((s) => s.id) })).length; + return Math.max(0, a.wipLimit - working.length - queued); +} + async function dispatch(db: Db, project: Project, a: AutonomySettings, now: Date, log: (m: string) => void) { const sessions = (await listSessions(db, { active: true })).filter((s) => s.projectId === project.id); const working = await workingSessions(db, sessions); diff --git a/packages/runner/src/pick-question.ts b/packages/runner/src/pick-question.ts index 203ab286..ed516dd1 100644 --- a/packages/runner/src/pick-question.ts +++ b/packages/runner/src/pick-question.ts @@ -19,7 +19,7 @@ import { } from "@peppa/core"; import type { Db } from "@peppa/db"; import { formatTaskKey } from "@peppa/shared"; -import { readyBacklog } from "./autonomy.ts"; +import { autoPickSlots, readyBacklog } from "./autonomy.ts"; import { moveTask } from "./flow.ts"; import { onAnswer, resolveTaskById } from "./sessions.ts"; @@ -81,10 +81,11 @@ async function pickForProject(db: Db, project: Project, log: (m: string) => void changed = true; } - // The dispatcher of an «auto» project takes stories itself — nobody is asked. - const auto = (await getAutonomy(db, project.id)).pick === "auto"; - if (!auto) { - const ready = await readyBacklog(db, project.id, backlog); + // The dispatcher of an «auto» project takes the first stories itself; the ones it can't take now + // (WIP limit reached, limits) wait for a person — they are asked about. + { + const autonomy = await getAutonomy(db, project.id); + const ready = (await readyBacklog(db, project.id, backlog)).slice(await autoPickSlots(db, project, autonomy)); let slots = maxOpen() - Object.keys(state.open).length; for (const task of ready) { if (slots <= 0) break; diff --git a/packages/testkit/test/pick-question.test.ts b/packages/testkit/test/pick-question.test.ts index 448d2867..bc68eaac 100644 --- a/packages/testkit/test/pick-question.test.ts +++ b/packages/testkit/test/pick-question.test.ts @@ -61,7 +61,7 @@ describe("PEP-185: «Взять в работу?»", () => { assert.equal((await open(p.id)).length, 3); }); - test("paused / blocked / auto projects are not asked", async () => { + test("paused / blocked projects are not asked; auto — only what the dispatcher can't take", async () => { const paused = await proj(); await story(paused.id, "A"); await core.setSetting(db, `project.paused:${paused.id}`, { since: Date.now(), by: "x", sessions: [] }); @@ -80,8 +80,15 @@ describe("PEP-185: «Взять в работу?»", () => { const auto = await proj(); await core.setAutonomy(db, auto.id, { level: "auto" }); + await core.setAutonomy(db, auto.id, { wipLimit: 1 }); await story(auto.id, "B"); await runner.pickQuestionTick(db); - assert.equal((await open(auto.id)).length, 0); + assert.equal((await open(auto.id)).length, 0, "the dispatcher takes the first story itself"); + // The WIP limit is reached (one place, one story for it) — the next story waits for a person. + const c = await story(auto.id, "C"); + await runner.pickQuestionTick(db); + const aq = await open(auto.id); + assert.equal(aq.length, 1); + assert.equal(aq[0]!.taskId, c.task.id); }); });