// PEP-41: feedback to a live agent with escalation — once a story reaches its failure // threshold, the runner is relaunched (same conversation) on the stronger model instead of // getting the message typed into the weaker one. import { checkEscalation, getSession, launchModelFor, logSessionEvent, resolveAgentSettings, sessionLaunchModel } from "@peppa/core"; import type { Db } from "@peppa/db"; import { resumeSession, sendToSession } from "./sessions.ts"; import * as tmux from "./tmux.ts"; import { isRunnerClient, runnerCall } from "./runner-client.ts"; export async function sendWithEscalation(db: Db, sessionId: number, text: string): Promise<"sent" | "escalated"> { // PEP-21: clients (web, bot) hand it to the runner service as one command. if (isRunnerClient()) return (await runnerCall<{ result: "sent" | "escalated" }>(db, "sendEscalated", { sessionId, text }, { key: `session:${sessionId}` })).result; const s = await getSession(db, sessionId); if (s.taskId) { const { escalation } = await checkEscalation(db, s.projectId, s.taskId); if (escalation) { const want = launchModelFor(await resolveAgentSettings(db, s.projectId, s.taskId), s.provider, true).model; const cur = await sessionLaunchModel(db, s.id); if (want && want !== cur) { if (await tmux.hasSession(s.tmuxName)) await tmux.killSession(s.tmuxName); await logSessionEvent(db, s, "model_escalated", { from: cur, to: want, failures: escalation.failures }); const prompt = `У стори уже ${escalation.failures} неудач(и) (красные проверки и возвраты ревьюера) — FORGE перезапустила тебя на более сильной модели (${want}).\n\n${text}`; await resumeSession(db, s.id, { prompt }); return "escalated"; } } } await sendToSession(db, sessionId, text); return "sent"; }