peppa/packages/testkit/test/lost-conversation.test.ts

104 lines
5.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-227: Claude cannot resume a conversation it no longer has («No conversation found with session
// ID») — the watchdog starts a new conversation instead of resuming the same id in a loop; and the
// «<model> now uses usage credits» dialog does not hold the agent.
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { after, afterEach, describe, test } from "node:test";
import { and, eq } from "drizzle-orm";
process.env.PEPPA_SECRET ??= "mock-secret-for-tests";
import { core, db, project, runner, superadmin, teardown, waitFor } from "./helpers.ts";
const { events } = await import("@peppa/db");
const { claudeCreditsDialog } = await import("../../runner/src/claude-screen.ts");
after(teardown);
afterEach(() => runner.setWatchdogSettings(db, ["reset"]));
const MIN = 60_000;
const sess = (id: number) => core.getSession(db, id);
const tickAt = (now: Date) => runner.tick(db, { intervalMs: 0, stallMs: 3_600_000, now });
const eventsOf = async (sid: number, type: string) => db.select().from(events).where(and(eq(events.sessionId, sid), eq(events.type, type)));
async function storyAtWork(scenario: Parameters<typeof project>[0]) {
const p = await project(scenario);
await core.updateTask(db, await core.resolveTask(db, p.taskKey), { status: "in_progress" });
return p;
}
const DIALOG = "Fable 5.2 now uses usage credits\n\n❯ 1. Use usage credits\n 2. Switch to Opus 5.5\n 3. Exit";
describe("Потерянный разговор — новый запуск, а не resume по кругу", () => {
test("Разговор потерян", async () => {
const p = await storyAtWork({ forgetResume: true, turns: [[{ say: "Жду." }]] });
const sid = await p.start();
await p.waitLog("turn 0", (e) => e.ev === "prompt");
const conversationId = await waitFor("conversation saved", async () => (await sess(sid)).conversationId);
// The agent's tmux is gone: a planned restart resumes the saved conversation…
execFileSync("tmux", ["-L", process.env.PEPPA_TMUX_SOCKET!, "kill-session", "-t", `=${(await sess(sid)).tmuxName}`]);
const t0 = new Date();
await tickAt(t0);
await tickAt(new Date(t0.getTime() + MIN + 1000));
const resumed = await p.waitLog("resume refused", (e) => e.ev === "exit" && e.reason === "no_conversation");
assert.equal(resumed.gen, "2");
assert.equal((await sess(sid)).watchState?.restarts?.length, 1);
// …Claude no longer has it: the conversation is forgotten, a new one starts at once.
const t1 = new Date(t0.getTime() + MIN + 5000);
await waitFor("conversation lost", async () => (await tickAt(t1), (await eventsOf(sid, "conversation_lost")).length));
const [lost] = await eventsOf(sid, "conversation_lost");
assert.equal((lost!.payload as any).conversationId, conversationId);
assert.equal((await sess(sid)).conversationId, null, "the saved conversation is forgotten");
await tickAt(new Date(t1.getTime() + 1000));
const start = await p.waitLog("fresh start", (e) => e.ev === "start" && e.gen === "3");
assert.equal(start.resume, null, "no --resume");
assert.ok((start.argv as string[]).some((a) => a.includes("сверься с peppa.get_task и tasks.md") || a.includes("Сверься с peppa.get_task и tasks.md")), "the prompt sends the agent to get_task and tasks.md");
assert.equal((await sess(sid)).watchState?.restarts?.length, 1, "no automatic restart spent");
assert.equal((await eventsOf(sid, "conversation_restarted")).length, 1);
await runner.stopSession(db, sid);
});
test("Падение без сохранённого разговора", async () => {
const p = await storyAtWork({ turns: [[{ say: "No conversation found with session ID: x" }, { crash: 1 }]] });
const sid = await p.start();
await p.waitLog("crashed", (e) => e.ev === "exit");
await core.updateSession(db, sid, { conversationId: null });
const t0 = new Date();
await waitFor("restart planned", async () => (await tickAt(t0), (await sess(sid)).watchState?.restartAt));
assert.equal((await sess(sid)).watchState?.restartReason, "crash", "the usual automatic restart");
assert.equal((await eventsOf(sid, "conversation_lost")).length, 0);
await runner.stopSession(db, sid);
});
});
describe("Диалог кредитов модели не держит агента", () => {
test("разбор экрана", () => {
assert.deepEqual(claudeCreditsDialog(`${DIALOG}\n\n\n`), { digit: "2" });
assert.deepEqual(claudeCreditsDialog("Fable 5.2 now uses usage credits\n 1. Continue\n 2. Exit"), { digit: null });
assert.equal(claudeCreditsDialog("⏺ Готово.\n> "), null);
});
test("Есть пункт перехода на Opus", async () => {
const p = await storyAtWork({ turns: [[{ say: DIALOG }]] });
const sid = await p.start();
await p.waitLog("turn 0", (e) => e.ev === "prompt");
const [ev] = await waitFor("dismissed", async () => (await tickAt(new Date()), (await eventsOf(sid, "model_dialog_dismissed")).length ? await eventsOf(sid, "model_dialog_dismissed") : null));
assert.equal((ev!.payload as any).choice, "2");
await tickAt(new Date());
assert.equal((await eventsOf(sid, "model_dialog_dismissed")).length, 1, "not pressed again at once");
await runner.stopSession(db, sid);
});
test("Пункта нет", async () => {
const sent = await superadmin();
const p = await storyAtWork({ turns: [[{ say: "Fable 5.2 now uses usage credits\n 1. Continue\n 2. Exit" }]] });
const sid = await p.start();
await p.waitLog("turn 0", (e) => e.ev === "prompt");
const text = await waitFor("superadmin told", async () => (await tickAt(new Date()), (await sent())[0]));
assert.match(text, /просит отдельные кредиты/);
await tickAt(new Date());
assert.equal((await sent()).length, 1, "one message");
await runner.stopSession(db, sid);
});
});