peppa/packages/testkit/test/autonomy.test.ts
Асель 00c8f79a36 PEP-7: autonomous mode — board moves start agents, PM only for stoppers
- web board (Backlog / In progress / Review / Done) with drag-and-drop and
  buttons; moveTask starts an agent on In progress, stops on Backlog/Done
- routine proposals go straight to apply; request_approval/ask_pm only for
  architectural stoppers (RULES.md, MCP hints, template pushed to Gitea)
- Stop hook keeps the agent going (decision: block, max 2 in a row,
  sessions.auto_nudges reset by a human answer) instead of pinging the PM
- review: ready-for-review message with Accept (acceptTask → done, session
  stopped) in Telegram and web; a text reply goes back to the agent
- mock-claude honours Stop-hook block; 6 new e2e tests (48 total, green)
- OpenSpec: pep-7 archived (task-autonomy, web-workspace board)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:21:11 +05:00

104 lines
5.2 KiB
TypeScript

// 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, /готова к ревью/);
assert.match(review.text, /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 PM is called; a human answer 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();
const q = await p.waitQuestion(sid, "attention");
assert.match(q.text, /застрял на шаге 3/);
assert.equal(await nudges(sid), runner.MAX_AUTO_NUDGES);
await runner.answerAndDeliver(db, q.id, "делай вариант Б", "telegram");
assert.equal((await p.session(sid)).autoNudges, 0);
await p.waitLog("answer delivered", (e) => e.ev === "prompt" && String(e.prompt).includes("вариант Б"));
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: "Стоппер?" } }]] });
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);
});
});