peppa/packages/testkit/src/mock-planner.ts
2026-11-14 06:28:53 +05:00

45 lines
2.7 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.

// mock-planner (PEP-42): stands in for `claude -p --output-format json` as the epic planner.
// Reads the prompt on stdin, answers with the CLI's JSON envelope whose `result` is the plan
// line. Behaviour comes from MOCK_PLANNER_SCENARIO (a JSON file):
// { "stories": [{ "title": "…", "description": "…", "depends_on": [1] }],
// "garbage": false, "fail": false, "hang": false, "prose": false }
// Without a scenario: three stories derived from the epic title, each depending on the previous.
// Every call is appended to <scenario>.log.jsonl (epic, wishes, existing stories, token, args).
import { appendFileSync, existsSync, readFileSync } from "node:fs";
interface Scenario {
stories?: Array<{ title: string; description?: string; depends_on?: Array<number | string> }>;
garbage?: boolean;
fail?: boolean;
hang?: boolean;
/** Wrap the JSON in prose and a code fence, like a chatty model. */
prose?: boolean;
}
const file = process.env.MOCK_PLANNER_SCENARIO;
const sc: Scenario = file && existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {};
const prompt = readFileSync(0, "utf8");
const epic = /^Эпик: (.*)$/m.exec(prompt)?.[1] ?? "";
const wishes = /Пожелания PM:\n([\s\S]*?)\n\n/.exec(prompt)?.[1] ?? null;
const existing = /Уже есть стори эпика:\n([\s\S]*?)\n\n/.exec(prompt)?.[1] ?? "";
if (file) {
appendFileSync(`${file}.log.jsonl`, JSON.stringify({ args: process.argv.slice(2), epic, wishes, existing, token: process.env.CLAUDE_CODE_OAUTH_TOKEN ? "set" : null }) + "\n");
}
if (!process.argv.includes("-p")) {
console.error("mock-planner: expected -p (non-interactive print mode)");
process.exit(2);
}
if (sc.hang) await new Promise(() => setInterval(() => {}, 1000));
if (sc.fail) {
console.error("mock-planner: API Error: 529 overloaded");
process.exit(1);
}
const stories = sc.stories ?? [
{ title: `${epic}: модель данных`, description: "Схема и миграция.\n\nКритерии готовности:\n- миграция применяется", depends_on: [] },
{ title: `${epic}: API`, description: "Эндпоинты.\n\nКритерии готовности:\n- тесты зелёные", depends_on: [1] },
{ title: `${epic}: экран`, description: "UI.\n\nКритерии готовности:\n- экран открывается", depends_on: [2] },
];
const json = JSON.stringify({ stories });
const result = sc.garbage ? "Извините, я не могу составить план." : sc.prose ? `Вот план:\n\`\`\`json\n${json}\n\`\`\`\nУдачи!` : json;
process.stdout.write(JSON.stringify({ type: "result", subtype: "success", is_error: false, num_turns: 1, result, session_id: "mock-planner" }) + "\n");