peppa/packages/core/src/launch-smoke.ts
peppa-bot b6d3921429
All checks were successful
peppa/lint lint: success за 2с
peppa/typecheck typecheck: success за 47с
peppa/build build: success за 33с
peppa/test test: success за 1455с
peppa-ci / ci (pull_request) Successful in 26m17s
PEP-266: CLI peppa smoke, раздел «Запуск агентов» на Observability, тест, архив change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 15:19:52 +00:00

68 lines
2.7 KiB
TypeScript

// 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<SmokeProvider, string> = { 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<SmokeRun[]> {
return (await getSetting<SmokeRun[]>(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<SmokeRun>(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<void> {
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<void> {
await deleteSetting(db, SMOKE_REQUEST_KEY);
}