peppa/packages/runner/src/escalation.ts
Асель 621aa0b5b3 PEP-41: escalation goes through the runner service in client processes (PEP-21)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 07:46:10 +05:00

31 lines
1.9 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-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} неудач(и) (красные проверки и возвраты ревьюера) — Пеппа перезапустила тебя на более сильной модели (${want}).\n\n${text}`;
await resumeSession(db, s.id, { prompt });
return "escalated";
}
}
}
await sendToSession(db, sessionId, text);
return "sent";
}