// PEP-205: the agent that finished its work does not wait for the checks (every poll spends the // subscription's limit). `update_task(status: review)` while the checks still run is remembered // (`review.wanted:`); the agent ends its turn. When the checks of the head are green the // platform itself moves the story to review (through the reviewer agent if the project has one) and // sends the «ready for review» card — with no turn of the agent. Red checks drop the mark: the CI // feedback brings the log to the agent, who asks for review again after the fix. import { buildReviewCard, deleteSetting, getSetting, listSessions, prGate, sendReviewCard, setSetting, updateTask, logEvent, isProjectPaused, type TaskRef } from "@peppa/core"; import { settings, type Db, keyPrefix } from "@peppa/db"; import { like } from "drizzle-orm"; import { REVIEW_WANTED_PREFIX, type ReviewWanted } from "./review-wanted-state.ts"; import { requestReview } from "./reviewer.ts"; import { resolveTaskById } from "./sessions.ts"; /** One pass: stories whose agent asked for review while the checks ran. Returns how many went to review. */ export async function reviewWantedTick(db: Db, o: { log?: (m: string) => void } = {}): Promise { const log = o.log ?? (() => {}); let moved = 0; for (const row of await db.select().from(settings).where(keyPrefix(settings.key, `${REVIEW_WANTED_PREFIX}`))) { const taskId = Number(row.key.slice(REVIEW_WANTED_PREFIX.length)); const want = row.value as ReviewWanted; try { const ref = await resolveTaskById(db, taskId).catch(() => null); if (!ref || !["proposing", "in_progress"].includes(ref.task.status)) { await deleteSetting(db, row.key); continue; } if (await isProjectPaused(db, ref.project.id)) continue; const withPr = { ...ref, task: { ...ref.task, prUrl: want.prUrl ?? ref.task.prUrl } }; const gate = await prGate(withPr).catch(() => null); if (!gate || !gate.pull || gate.pull.state !== "open") continue; // Red checks and a conflict reach the agent through the CI feedback; it asks for review again. if (gate.summary?.state === "failure" || gate.pull.mergeable === false) { await deleteSetting(db, row.key); continue; } if (!(gate.ok || gate.behind)) continue; await deleteSetting(db, row.key); const r = await platformToReview(db, withPr, gate.pull.head.sha); log(r === "reviewer" ? `${ref.key}: checks green — the reviewer agent takes the PR` : `${ref.key}: checks green — moved to review by the platform (no turn of the agent)`); moved++; } catch (e) { log(`review wanted ${taskId}: ${(e as Error).message}`); } } return moved; } /** * The platform moves a story with a green, conflict-free result to review itself — no turn of the * agent: through the reviewer agent when the project has one, else status review and the «готово» * card (PEP-205; PEP-209 heals stuck stories with it too). `ref.task.prUrl` must be the result. */ export async function platformToReview(db: Db, ref: TaskRef, sha: string, via: string = "review_by_platform"): Promise<"reviewer" | "review"> { const r = await requestReview(db, ref).catch(() => ({ action: "review" as const })); if (r.action === "queued") { await updateTask(db, ref, { prUrl: ref.task.prUrl ?? undefined }); return "reviewer"; } await updateTask(db, ref, { status: "review", prUrl: ref.task.prUrl ?? undefined }); const ready = { ...ref, task: { ...ref.task, status: "review" as const } }; const s = (await listSessions(db, { active: true, taskId: ref.task.id }))[0] ?? null; await sendReviewCard(db, ready, await buildReviewCard(db, ready, {}), s); await logEvent(db, { projectId: ref.project.id, taskId: ref.task.id, sessionId: s?.id ?? null, type: via, payload: { sha } }); return "review"; }