// mock-triage (PEP-58): stands in for `claude -p --output-format json` as the incident // triager. Reads the prompt on stdin, answers with the CLI's JSON envelope whose `result` // is the decision line. Behaviour comes from MOCK_TRIAGE_SCENARIO (a JSON file): // { "rules": [{ "match": "PaymentError", "action": "start", "confidence": 0.9, "reason": "…" }], // "default": { "action": "ask", "confidence": 0.3, "reason": "…" }, // "fail": false, "hang": false } // Every call is appended to .log.jsonl (args, whether the PM prompt and token came through). import { appendFileSync, existsSync, readFileSync } from "node:fs"; interface Rule { match: string; action: string; confidence?: number; reason?: string; } interface Scenario { rules?: Rule[]; default?: Omit; fail?: boolean; hang?: boolean; } const file = process.env.MOCK_TRIAGE_SCENARIO; const sc: Scenario = file && existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {}; const prompt = readFileSync(0, "utf8"); const pmRules = /Правила PM:\n([\s\S]*?)\n\nПроект:/.exec(prompt)?.[1] ?? ""; const title = /^Ошибка: (.*)$/m.exec(prompt)?.[1] ?? ""; if (file) { appendFileSync(`${file}.log.jsonl`, JSON.stringify({ args: process.argv.slice(2), title, pmRules, token: process.env.CLAUDE_CODE_OAUTH_TOKEN ? "set" : null, cwd: process.cwd() }) + "\n"); } if (!process.argv.includes("-p")) { console.error("mock-triage: expected -p (non-interactive print mode)"); process.exit(2); } if (sc.hang) await new Promise(() => setInterval(() => {}, 1000)); // never answers if (sc.fail) { console.error("mock-triage: API Error: 529 overloaded"); process.exit(1); } const rule = sc.rules?.find((r) => new RegExp(r.match, "i").test(title)) ?? { match: "", ...(sc.default ?? { action: "ask", confidence: 0.2, reason: "правила PM не покрывают этот случай" }) }; const decision = { action: rule.action, confidence: rule.confidence ?? 0.9, reason: rule.reason ?? `mock: правило «${rule.match}»` }; process.stdout.write( JSON.stringify({ type: "result", subtype: "success", is_error: false, num_turns: 1, result: JSON.stringify(decision), session_id: "mock-triage" }) + "\n", );