// PEP-7: autonomous cycle — no routine approvals, keep-going nudges, review and accept, // board moves that start/stop agents. import assert from "node:assert/strict"; import { after, describe, test } from "node:test"; import { core, db, project, runner, teardown, waitFor } from "./helpers.ts"; import { events, questions } from "@peppa/db"; import { and, eq } from "drizzle-orm"; after(teardown); const nudges = async (sid: number) => (await db.select().from(events).where(and(eq(events.sessionId, sid), eq(events.type, "auto_continue")))).length; describe("autonomous cycle", () => { test("proposal → apply → review without asking the PM; one nudge keeps the agent going; accept closes the task", async () => { const p = await project({ turns: [ [ { mcp: "update_task", args: { task: "1", status: "proposing", openspec_change: "auto-1" } }, { mcp: "update_task", args: { task: "1", status: "in_progress" } }, { say: "Proposal готов, начал apply." }, ], [ { mcp: "report_progress", args: { task: "1", text: "всё сделано" } }, { mcp: "update_task", args: { task: "1", status: "review", pr_url: "https://git.example/pr/7" } }, { say: "PR открыт." }, ], ], }); const sid = await p.start(); const review = await p.waitQuestion(sid, "attention"); assert.match(review.text, /готова к ревью/); // PEP-152: the address of the result is a technical detail — under «Подробнее». assert.doesNotMatch(review.text, /pr\/7/); assert.match((await core.messageDetails(db, [review.id])).get(review.id) ?? "", /pr\/7/); assert.equal(await nudges(sid), 1, "the agent was nudged once instead of pinging the PM"); assert.ok(p.log().some((e) => e.ev === "continue"), "mock continued via the Stop hook"); const qs = await db.select().from(questions).where(eq(questions.sessionId, sid)); assert.deepEqual(qs.map((q) => q.kind), ["attention"], "no approval/question to the PM along the way"); assert.equal((await p.task()).status, "review"); await runner.acceptTask(db, review.id, "web"); assert.equal((await p.task()).status, "done"); await p.waitStatus(sid, "done"); assert.equal((await core.getQuestion(db, review.id)).answer, "✅ Принято"); }); test("after two nudges in a row the turn ends as progress — the PM is not called (PEP-149); a human message resets the counter", async () => { const p = await project({ turns: [[{ mcp: "update_task", args: { task: "1", status: "in_progress" } }, { say: "шаг 1" }], [{ say: "шаг 2" }], [{ say: "застрял на шаге 3" }], [{ say: "понял" }]], }); const sid = await p.start(); await p.waitStatus(sid, "idle"); assert.equal(await nudges(sid), runner.MAX_AUTO_NUDGES); assert.deepEqual(await p.pending(sid), [], "nobody is asked"); await runner.sendToSession(db, sid, "делай вариант Б"); await p.waitLog("message delivered", (e) => e.ev === "prompt" && String(e.prompt).includes("вариант Б")); await waitFor("the counter is reset", async () => (await p.session(sid)).autoNudges === 0); await runner.stopSession(db, sid); }); test("a comment on the review message goes back to the agent", async () => { const p = await project({ turns: [[{ mcp: "update_task", args: { task: "1", status: "review" } }, { say: "Готово." }], [{ say: "Доделал." }]], }); const sid = await p.start(); const q = await p.waitQuestion(sid, "attention"); assert.ok(await runner.isReviewQuestion(db, q)); await runner.answerAndDeliver(db, q.id, "добавь README", "web"); await p.waitLog("comment delivered", (e) => e.ev === "prompt" && String(e.prompt).includes("добавь README")); await runner.stopSession(db, sid); }); }); describe("task board", () => { test("moving a card to In progress starts an agent; back to Backlog stops it", async () => { const p = await project({ turns: [[{ mcp: "ask_pm", args: { task: "1", question: "Стоппер?", options: ["Да", "Нет"], default: "Да" } }]] }); let ref = await core.resolveTask(db, p.taskKey); assert.equal(runner.columnOf(ref.task.status), "backlog"); const { started } = await runner.moveTask(db, ref, "in_progress"); assert.ok(started, "session started"); assert.equal((await p.task()).status, "in_progress"); const q = await p.waitQuestion(started!, "question"); // Moving again into In progress doesn't start a second agent. ref = await core.resolveTask(db, p.taskKey); assert.equal((await runner.moveTask(db, ref, "in_progress")).started, undefined); ref = await core.resolveTask(db, p.taskKey); const { stopped } = await runner.moveTask(db, ref, "backlog"); assert.equal(stopped, 1); assert.equal((await p.task()).status, "backlog"); assert.equal((await p.session(started!)).status, "done"); assert.equal((await core.getQuestion(db, q.id)).answeredVia, "system", "open question closed as no longer relevant"); }); test("Review → Done by the PM", async () => { const p = await project({ turns: [] }); let ref = await core.resolveTask(db, p.taskKey); await core.updateTask(db, ref, { status: "review" }); ref = await core.resolveTask(db, p.taskKey); await runner.moveTask(db, ref, "done"); assert.equal((await p.task()).status, "done"); await waitFor("no sessions", async () => (await core.listSessions(db, { taskId: ref.task.id })).length === 0); }); });