peppa/packages/runner/src/flow.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

70 lines
3.2 KiB
TypeScript

// PM-side moves on the task board and review acceptance (web and Telegram use these).
import { answerQuestion, getQuestion, listSessions, PeppaError, resolveTask, updateTask, type Question, type TaskRef } from "@peppa/core";
import { tasks, projects, type Db } from "@peppa/db";
import { formatTaskKey } from "@peppa/shared";
import { eq } from "drizzle-orm";
import { startSession, stopSession, type StartOptions } from "./sessions.ts";
export type BoardColumn = "backlog" | "in_progress" | "review" | "done";
/** Which board column a task status belongs to. */
export function columnOf(status: string): BoardColumn {
if (status === "backlog") return "backlog";
if (status === "review") return "review";
if (status === "done") return "done";
return "in_progress"; // proposing, awaiting_approval, in_progress, blocked
}
async function activeSessions(db: Db, taskId: number) {
return (await listSessions(db, { active: true, taskId }));
}
/**
* Moves a task card. Into "in progress": status in_progress and an agent session is
* started unless one is running. Into "backlog"/"done": active sessions are stopped.
*/
export async function moveTask(db: Db, ref: TaskRef, to: BoardColumn, start: StartOptions = {}): Promise<{ started?: number; stopped: number }> {
const from = columnOf(ref.task.status);
let stopped = 0;
if (to === "in_progress") {
if (from !== "in_progress") await updateTask(db, ref, { status: "in_progress" });
if ((await activeSessions(db, ref.task.id)).length === 0) {
const s = await startSession(db, ref.key, start);
return { started: s.id, stopped };
}
return { stopped };
}
if (to === "backlog" || to === "done") {
for (const s of await activeSessions(db, ref.task.id)) {
await stopSession(db, s.id);
stopped++;
}
}
if (from !== to || ref.task.status !== to) await updateTask(db, ref, { status: to });
return { stopped };
}
/** "Accept" on a ready-for-review message: task done, session stopped, question closed. */
export async function acceptTask(db: Db, questionId: number, via: NonNullable<Question["answeredVia"]>): Promise<TaskRef> {
const q = await getQuestion(db, questionId);
if (!q.taskId) throw new PeppaError("Вопрос не привязан к задаче");
if (q.answeredAt) throw new PeppaError(`Уже отвечено (${q.answeredVia}): ${q.answer}`);
const [t] = await db
.select({ key: projects.key, number: tasks.number })
.from(tasks)
.innerJoin(projects, eq(tasks.projectId, projects.id))
.where(eq(tasks.id, q.taskId));
const ref = await resolveTask(db, formatTaskKey(t!.key, t!.number));
if (ref.task.status !== "review") throw new PeppaError(`${ref.key} не в ревью (сейчас ${ref.task.status})`);
await answerQuestion(db, q.id, "✅ Принято", via);
await moveTask(db, ref, "done");
return ref;
}
/** True when an "attention" question is the ready-for-review message of its task. */
export async function isReviewQuestion(db: Db, q: Question): Promise<boolean> {
if (q.kind !== "attention" || !q.taskId || q.answeredAt) return false;
const [t] = await db.select({ status: tasks.status }).from(tasks).where(eq(tasks.id, q.taskId));
return t?.status === "review";
}