peppa/packages/runner/src/delay-actions.ts

60 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-209: the superadmin's buttons on a delay card (the dashboard's «Задержки» and the Telegram
// message about an exhausted delay): «Перезапустить», «Перевести на ревью» (only past the review
// gate), «Закрыть». Web and bot are clients of the runner service (PEP-21): the action runs there.
import { clearPlatformDelay, getPlatformDelay, listSessions, PeppaError, type DelayAction, type DelayReason } from "@peppa/core";
import type { Db } from "@peppa/db";
import { m } from "@peppa/shared/i18n";
import { moveTask } from "./flow.ts";
import { platformToReview } from "./review-wanted.ts";
import { isRunnerClient, runnerCall } from "./runner-client.ts";
import { resolveTaskById } from "./sessions.ts";
import { clearStuckHeal, diagnose, restartAgent } from "./stuck-tasks.ts";
export interface DelayTarget {
projectId: number;
taskId: number | null;
}
/** Runs one button; returns the toast/flash text. Refusals are `PeppaError` with the reason. */
export async function delayAction(db: Db, target: DelayTarget, reason: DelayReason, action: DelayAction): Promise<string> {
if (isRunnerClient()) return (await runnerCall<{ text: string }>(db, "delay", { target, reason, action }, { key: target.taskId != null ? `task:${target.taskId}` : null })).text;
const scope = { projectId: target.projectId, taskId: target.taskId };
if (action === "close") {
await clearPlatformDelay(db, scope, reason);
if (target.taskId != null) await clearStuckHeal(db, target.taskId);
return m("Карточка закрыта");
}
if (target.taskId == null) {
if (action === "review") throw new PeppaError(m("Карточка не про задачу: переводить на ревью нечего"));
await clearPlatformDelay(db, scope, reason);
return m("Карточка закрыта: Peppa FACTORY попробует снова сама");
}
const ref = await resolveTaskById(db, target.taskId);
if (action === "review") {
if (ref.task.status === "review") {
await clearPlatformDelay(db, scope);
return m("{key} уже на ревью", { key: ref.key });
}
if (!["proposing", "in_progress"].includes(ref.task.status)) throw new PeppaError(m("{key} не в работе", { key: ref.key }));
const d = await diagnose(db, ref);
if (d.kind !== "review") {
const why = d.kind === "agent" ? d.reason : d.kind === "merged" ? m("изменения уже приняты в код") : m("проверки ещё идут");
throw new PeppaError(m("Перевести на ревью нельзя: {reason}", { reason: why }));
}
await platformToReview(db, ref, d.pull.head.sha, "review_by_admin");
await clearPlatformDelay(db, scope);
await clearStuckHeal(db, ref.task.id);
return m("{key} на ревью", { key: ref.key });
}
// restart
const existing = await getPlatformDelay(db, scope, reason);
if (ref.task.status === "backlog") await moveTask(db, ref, "in_progress");
else {
const [s] = (await listSessions(db, { taskId: ref.task.id })).sort((a, b) => b.id - a.id);
await restartAgent(db, ref, s ?? null, "Администратор Пеппы перезапустил тебя. Сверься с peppa.get_task и доведи стори до ревью.");
}
await clearPlatformDelay(db, scope, existing ? reason : undefined);
await clearStuckHeal(db, ref.task.id);
return m("{key}: агент перезапущен", { key: ref.key });
}