From 39e58da6fedffa76822f7bf3942f119b0fc5e82b Mon Sep 17 00:00:00 2001 From: peppa-bot Date: Tue, 17 Nov 2026 01:48:56 +0500 Subject: [PATCH] =?UTF-8?q?PEP-185:=20=C2=AB=D0=92=D0=B7=D1=8F=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B2=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=3F=C2=BB=20als?= =?UTF-8?q?o=20in=20auto=20projects=20=E2=80=94=20for=20stories=20the=20di?= =?UTF-8?q?spatcher=20can't=20take=20now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BRM (pick=auto, WIP 1, 8 in work): ready stories waited forever and nobody was asked. Co-Authored-By: Claude Opus 5.5 --- packages/runner/src/autonomy.ts | 13 +++++++++++++ packages/runner/src/pick-question.ts | 11 ++++++----- packages/testkit/test/pick-question.test.ts | 11 +++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) 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); }); });