// PEP-127: the PM gets the «готова к ревью» message (with «Принять») only when the story can // be merged right now — green checks of the PR head and no conflict. Otherwise the message is // held (`review.hold:`) and the watchdog sends it once the gate opens // (runner/review-card.ts). import { settings, type Db } from "@peppa/db"; import { m } from "@peppa/shared/i18n"; import { like } from "drizzle-orm"; import { logEvent } from "./audit.ts"; import { prGate } from "./ci.ts"; import type { GiteaClient } from "./gitea.ts"; import { raiseSessionQuestion, type Session } from "./sessions.ts"; import { deleteSetting, getSetting, setSetting } from "./settings.ts"; import { askPm, type Question, type TaskRef } from "./tasks.ts"; export interface ReviewHold { /** The whole message the PM gets when the gate opens. */ text: string; /** When it was held (ms). */ at: number; /** Head SHA the agent was asked to rebase (once per head commit). */ rebaseSha?: string; /** Head SHA for which Peppa asked Gitea to bring the base branch into the PR branch. */ updateSha?: string; } const PREFIX = "review.hold:"; const key = (taskId: number) => `${PREFIX}${taskId}`; /** Answer that takes a ready-for-review message back when its PR got into a conflict. */ export const REVIEW_REVOKED_ANSWER = m("🔄 Агент обновляет изменения — пришлём задачу снова, когда её можно будет принять."); /** Gate reason when Gitea could not be asked: closed, never "ok". */ export const GATE_UNKNOWN_REASON = m("не удалось проверить состояние проверок — повторите позже"); export const getReviewHold = (db: Db, taskId: number) => getSetting(db, key(taskId)); export const setReviewHold = (db: Db, taskId: number, h: ReviewHold) => setSetting(db, key(taskId), h); export const clearReviewHold = (db: Db, taskId: number) => deleteSetting(db, key(taskId)); export async function listReviewHolds(db: Db): Promise> { const rows = await db.select().from(settings).where(like(settings.key, `${PREFIX}%`)); return new Map(rows.map((r) => [Number(r.key.slice(PREFIX.length)), r.value as ReviewHold])); } /** * Sends «готова к ревью» to the PM if the story's PR can be merged now; otherwise holds the * message (event `awaiting_checks` with the reason) and returns null. A Gitea error keeps the * gate closed. */ export async function sendReviewCard(db: Db, ref: TaskRef, text: string, session?: Session | null, gitea?: GiteaClient): Promise { const gate = await prGate(ref, gitea).catch(() => null); if (gate?.ok) { await clearReviewHold(db, ref.task.id); return session ? raiseSessionQuestion(db, session, "attention", text) : askPm(db, ref, { kind: "attention", text }); } const prev = await getReviewHold(db, ref.task.id); await setReviewHold(db, ref.task.id, { ...prev, text, at: Date.now() }); await logEvent(db, { projectId: ref.project.id, taskId: ref.task.id, sessionId: session?.id ?? null, type: "awaiting_checks", payload: { reason: gate?.reason ?? GATE_UNKNOWN_REASON, sha: gate?.pull?.head.sha ?? null } }); return null; }