peppa/packages/runner/src/decisions.ts
2026-11-17 21:17:44 +05:00

82 lines
4.6 KiB
TypeScript
Raw Permalink 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-198: what the owner's answer to «Фабрика решила за вас» sets in motion (core/task-decisions.ts).
// «Подходит» only marks the decision. «Нужно иначе» — the story is not started yet: the analyst
// rebuilds it (the comment goes to the refine of PEP-115); in work: the agent gets «было — …,
// нужно — …»; in review: it is «Поправить». «Не делить» archives the parts of a split and puts
// them back into the first story's description.
import { m } from "@peppa/shared/i18n";
import {
addStoryComment,
changeDecision,
clearSplitPart,
confirmDecision,
getDecisions,
listSessions,
PeppaError,
updateTask,
withAssumptions,
type Decision,
type TaskRef,
} from "@peppa/core";
import { tasks, type Db } from "@peppa/db";
import { inArray } from "drizzle-orm";
import { archiveTask, fixStory } from "./flow.ts";
import { resolveTaskById, sendToSession } from "./sessions.ts";
export type DecisionOutcome = "confirmed" | "refine" | "agent" | "fix" | "noted" | "unsplit";
export async function confirmTaskDecision(db: Db, ref: TaskRef, id: number, by: { login: string }): Promise<DecisionOutcome> {
await confirmDecision(db, ref.task, id, by.login);
return "confirmed";
}
/** «Нужно иначе» → «Как нужно?». */
export async function changeTaskDecision(db: Db, ref: TaskRef, id: number, how: string, by: { id: number; login: string }): Promise<DecisionOutcome> {
const before = (await getDecisions(db, ref.task.id)).find((d) => d.id === id);
if (before?.kind === "split") throw new PeppaError(m("Разбивку можно принять или отменить кнопкой «Не делить»."));
const d = await changeDecision(db, ref.task, id, how, by.login);
const remark = m("Решение нужно иначе: было — {was}, нужно — {how}.", { was: d.was ?? "—", how: d.text });
// The developer's «Допущения» follow the list at once.
if (ref.task.description) await updateTask(db, ref, { description: withAssumptions(ref.task.description, await getDecisions(db, ref.task.id)) });
const fresh = (await resolveTaskById(db, ref.task.id).catch(() => null)) ?? ref;
const status = fresh.task.status;
if (status === "review") {
await fixStory(db, fresh, by, remark);
return "fix";
}
await addStoryComment(db, fresh, by, remark);
if (status === "backlog") return "refine"; // the analyst rebuilds the story (PEP-115 refine)
if (status === "done") return "noted";
const [live] = await listSessions(db, { active: true, taskId: fresh.task.id });
// i18n-ignore — text for the agent
if (live) await sendToSession(db, live.id, `Владелец изменил решение по ${fresh.key}: было — ${d.was ?? "—"}, нужно — ${d.text}. Учти это в работе и в design.md.`, { person: true }).catch(() => {});
return "agent";
}
/** «Не делить»: the parts go to the archive, their titles and what was postponed — back into this story. */
export async function undoSplit(db: Db, ref: TaskRef, id: number, by: { id: number; login: string }): Promise<DecisionOutcome> {
const d: Decision | undefined = (await getDecisions(db, ref.task.id)).find((x) => x.id === id);
if (!d || d.kind !== "split") throw new PeppaError(m("Этого решения уже нет. Обновите страницу."));
const parts = d.parts?.length ? await db.select().from(tasks).where(inArray(tasks.id, d.parts)) : [];
const back: string[] = [];
for (const t of parts) {
const part = await resolveTaskById(db, t.id).catch(() => null);
await clearSplitPart(db, t.id);
if (!part || t.archivedAt) continue;
// A part already taken to work stays: someone decided on it.
if (t.status !== "backlog") continue;
await archiveTask(db, part, true);
const about = (t.description ?? "").replace(/\n*_[^_]*_\s*$/, "").trim();
back.push(`- ${t.title}${about ? `: ${about.split("\n")[0]}` : ""}`);
}
for (const x of d.deferred ?? []) back.push(`- ${x}`);
await changeDecision(db, ref.task, id, m("Не делить: всё в одной задаче"), by.login);
const fresh = (await resolveTaskById(db, ref.task.id).catch(() => null)) ?? ref;
if (back.length) {
const desc = (fresh.task.description ?? "").trimEnd();
const section = `## Тоже входит в задачу\n${back.join("\n")}`;
const next = /^## Исходный запрос$/m.test(desc) ? desc.replace(/^## Исходный запрос$/m, `${section}\n\n## Исходный запрос`) : `${desc}\n\n${section}`;
await updateTask(db, fresh, { description: next });
}
return "unsplit";
}