// PEP-266: results of the launch smoke test (the runner service runs it, runner/launch-smoke.ts): // what the superadmin reads on Observability and in `peppa smoke status`, and «Проверить сейчас». import type { Db } from "@peppa/db"; import { deleteSetting, getSetting, setSetting } from "./settings.ts"; export type SmokeProvider = "claude" | "codex" | "opencode"; export const SMOKE_RUN_KEY = "smoke.run"; export const SMOKE_HISTORY_KEY = "smoke.history"; export const SMOKE_REQUEST_KEY = "smoke.request"; export const SMOKE_HISTORY_MAX = 20; /** Minutes an agent has to commit the file (PEPPA_SMOKE_TIMEOUT_MIN, default 5). */ export function smokeTimeoutMin(): number { const v = process.env.PEPPA_SMOKE_TIMEOUT_MIN; const n = v == null || v.trim() === "" ? NaN : Number(v); return Number.isFinite(n) && n > 0 ? n : 5; } export const PROVIDER_LABEL: Record = { claude: "Claude", codex: "Codex", opencode: "OpenCode" }; export type SmokeStatus = "running" | "ok" | "failed" | "skipped"; export type SmokeTrigger = "deploy" | "hourly" | "manual"; export interface SmokeItem { provider: SmokeProvider; status: SmokeStatus; sessionId?: number; subscriptionId?: number | null; cwd?: string | null; startedAt?: string; /** Seconds from the start to the commit (ok) or to giving up (failed). */ sec?: number; /** Why it failed or was not checked (a phrase of the factory, translatable). */ reason?: string; /** The last lines of the agent's screen on a failure (engineer's detail). */ screen?: string; } export interface SmokeRun { id: number; at: string; commit: string | null; trigger: SmokeTrigger; by?: string | null; items: SmokeItem[]; finishedAt?: string; } /** Finished runs, newest first. */ export async function smokeHistory(db: Db): Promise { return (await getSetting(db, SMOKE_HISTORY_KEY)) ?? []; } /** What the superadmin sees: the run going on now (if any) and the finished ones. */ export async function smokeStatus(db: Db): Promise<{ current: SmokeRun | null; history: SmokeRun[]; requested: boolean }> { return { current: (await getSetting(db, SMOKE_RUN_KEY)) ?? null, history: await smokeHistory(db), requested: !!(await getSetting(db, SMOKE_REQUEST_KEY)) }; } /** «Проверить сейчас» (web, CLI): the next tick of the runner service starts a run. */ export async function requestSmoke(db: Db, by: string | null = null, now = new Date()): Promise { await setSetting(db, SMOKE_REQUEST_KEY, { at: now.toISOString(), by }); } /** Drops the request (the run it asked for has started). */ export async function clearSmokeRequest(db: Db): Promise { await deleteSetting(db, SMOKE_REQUEST_KEY); }