// PEP-174: a project on pause — nothing happens in it. Agents are stopped and not started, the // AI roles (analyst, product agent, reviewer, triage) are not called, nobody gets notifications, // auto-merge and auto-close stand. Every such place asks `isProjectPaused` / `assertProjectActive`; // pausing and resuming themselves live in runner/pause.ts (they stop and resume the agents). import { settings, type Db, keyPrefix } from "@peppa/db"; import { m } from "@peppa/shared/i18n"; import { like } from "drizzle-orm"; import { PeppaError } from "./errors.ts"; import { deleteSetting, getSetting, setSetting } from "./settings.ts"; export interface ProjectPause { /** When the pause began (ms). */ since: number; /** Who paused (user id); null — Peppa itself. */ by: number | null; /** Runners stopped by the pause: «Продолжить» resumes them. */ sessions: number[]; } /** The last finished pause: what happened inside it is not sent afterwards. */ export interface PauseWindow { from: number; to: number; } const PREFIX = "project.paused:"; const key = (projectId: number) => `${PREFIX}${projectId}`; const lastKey = (projectId: number) => `project.pause.last:${projectId}`; /** What a refused action says (web, bot, MCP, CLI). */ export const PROJECT_PAUSED_TEXT = m("Проект на паузе — агенты не работают и уведомлений нет. Нажмите «Продолжить» на странице проекта"); export const getProjectPause = (db: Db, projectId: number) => getSetting(db, key(projectId)); export const setProjectPause = (db: Db, projectId: number, p: ProjectPause) => setSetting(db, key(projectId), p); export async function isProjectPaused(db: Db, projectId: number | null | undefined): Promise { if (projectId == null) return false; return (await getProjectPause(db, projectId)) != null; } /** Throws the plain «project on pause» refusal. */ export async function assertProjectActive(db: Db, projectId: number | null | undefined): Promise { if (await isProjectPaused(db, projectId)) throw new PeppaError(PROJECT_PAUSED_TEXT); } /** Ids of the paused projects (one query — for ticks and lists). */ export async function pausedProjectIds(db: Db): Promise> { const rows = await db.select({ key: settings.key }).from(settings).where(keyPrefix(settings.key, `${PREFIX}`)); return new Set(rows.map((r) => Number(r.key.slice(PREFIX.length)))); } /** Ends the pause and remembers its window; returns the pause that ended (null — was not paused). */ export async function clearProjectPause(db: Db, projectId: number, now = new Date()): Promise { const p = await getProjectPause(db, projectId); if (!p) return null; await deleteSetting(db, key(projectId)); await setSetting(db, lastKey(projectId), { from: p.since, to: now.getTime() } satisfies PauseWindow); return p; } export const lastPauseWindow = (db: Db, projectId: number) => getSetting(db, lastKey(projectId)); /** True when `at` fell into the project's pause (now or the last one): such messages are not sent later. */ export async function duringPause(db: Db, projectId: number | null | undefined, at: Date): Promise { if (projectId == null) return false; const p = await getProjectPause(db, projectId); if (p) return true; const w = await lastPauseWindow(db, projectId); const t = at.getTime(); return !!w && t >= w.from && t < w.to; }