|
| 1 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { createInterface } from "node:readline/promises"; |
| 4 | +import { stdin as input, stdout as output } from "node:process"; |
| 5 | + |
| 6 | +interface PendingEdit { |
| 7 | + section: string; |
| 8 | + edit: string; |
| 9 | + confidence: number; |
| 10 | +} |
| 11 | + |
| 12 | +function parsePending(raw: string): PendingEdit[] { |
| 13 | + const out: PendingEdit[] = []; |
| 14 | + for (const line of raw.split("\n")) { |
| 15 | + const trimmed = line.trim(); |
| 16 | + if (!trimmed) continue; |
| 17 | + try { |
| 18 | + const obj = JSON.parse(trimmed) as Partial<PendingEdit>; |
| 19 | + if (typeof obj.section === "string" && typeof obj.edit === "string") { |
| 20 | + out.push({ |
| 21 | + section: obj.section, |
| 22 | + edit: obj.edit, |
| 23 | + confidence: |
| 24 | + typeof obj.confidence === "number" ? obj.confidence : 0, |
| 25 | + }); |
| 26 | + } |
| 27 | + } catch { |
| 28 | + // skip malformed line |
| 29 | + } |
| 30 | + } |
| 31 | + return out; |
| 32 | +} |
| 33 | + |
| 34 | +function appendToDecisionLog(tracePath: string, edit: string): void { |
| 35 | + let doc = readFileSync(tracePath, "utf8"); |
| 36 | + if (!doc.endsWith("\n")) doc += "\n"; |
| 37 | + const bullet = `- ${edit}\n`; |
| 38 | + |
| 39 | + const heading = /^##\s+Decision Log\b.*$/im.exec(doc); |
| 40 | + if (!heading) { |
| 41 | + writeFileSync(tracePath, `${doc}\n## Decision Log\n\n${bullet}`, "utf8"); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const headingLineEnd = doc.indexOf("\n", heading.index) + 1; |
| 46 | + const rest = doc.slice(headingLineEnd); |
| 47 | + const nextHeading = /^##\s+/m.exec(rest); |
| 48 | + const sectionEnd = nextHeading |
| 49 | + ? headingLineEnd + nextHeading.index |
| 50 | + : doc.length; |
| 51 | + |
| 52 | + const before = doc.slice(0, headingLineEnd); |
| 53 | + const body = doc.slice(headingLineEnd, sectionEnd).replace(/\s*$/, ""); |
| 54 | + const after = doc.slice(sectionEnd); |
| 55 | + |
| 56 | + const newBody = body === "" ? `\n${bullet}` : `${body}\n${bullet}`; |
| 57 | + const separator = after ? "\n" : ""; |
| 58 | + writeFileSync(tracePath, before + newBody + separator + after, "utf8"); |
| 59 | +} |
| 60 | + |
| 61 | +export async function review(cwd: string): Promise<void> { |
| 62 | + const pendingPath = resolve(cwd, ".trace/pending.jsonl"); |
| 63 | + const tracePath = resolve(cwd, "TRACE.md"); |
| 64 | + |
| 65 | + if (!existsSync(tracePath)) { |
| 66 | + console.error( |
| 67 | + "trace: no TRACE.md in current directory. Run `trace init` first.", |
| 68 | + ); |
| 69 | + process.exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + if (!existsSync(pendingPath)) { |
| 73 | + console.log("trace: no pending edits."); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const edits = parsePending(readFileSync(pendingPath, "utf8")); |
| 78 | + if (edits.length === 0) { |
| 79 | + console.log("trace: no pending edits."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const rl = createInterface({ input, output }); |
| 84 | + let accepted = 0; |
| 85 | + let skipped = 0; |
| 86 | + let i = 0; |
| 87 | + let aborted = false; |
| 88 | + |
| 89 | + try { |
| 90 | + for (; i < edits.length; i++) { |
| 91 | + const { section, edit, confidence } = edits[i]; |
| 92 | + console.log(""); |
| 93 | + console.log(`── proposal ${i + 1} of ${edits.length} ──`); |
| 94 | + console.log(`section: ${section}`); |
| 95 | + console.log(`confidence: ${confidence.toFixed(2)}`); |
| 96 | + console.log(`edit: ${edit}`); |
| 97 | + console.log(""); |
| 98 | + |
| 99 | + let answer: string; |
| 100 | + try { |
| 101 | + answer = (await rl.question("[a]ccept / [s]kip? ")) |
| 102 | + .trim() |
| 103 | + .toLowerCase(); |
| 104 | + } catch { |
| 105 | + console.log("\n→ input closed. Remaining proposals kept in queue."); |
| 106 | + aborted = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + if (answer === "a" || answer === "accept") { |
| 111 | + appendToDecisionLog(tracePath, edit); |
| 112 | + accepted++; |
| 113 | + console.log("→ accepted, appended to Decision Log."); |
| 114 | + } else { |
| 115 | + skipped++; |
| 116 | + console.log("→ skipped."); |
| 117 | + } |
| 118 | + } |
| 119 | + } finally { |
| 120 | + rl.close(); |
| 121 | + } |
| 122 | + |
| 123 | + if (aborted) { |
| 124 | + const remaining = edits |
| 125 | + .slice(i) |
| 126 | + .map((e) => JSON.stringify(e)) |
| 127 | + .join("\n"); |
| 128 | + writeFileSync( |
| 129 | + pendingPath, |
| 130 | + remaining.length ? remaining + "\n" : "", |
| 131 | + "utf8", |
| 132 | + ); |
| 133 | + console.log(""); |
| 134 | + console.log( |
| 135 | + `trace: reviewed ${i} of ${edits.length} — ${accepted} accepted, ${skipped} skipped. ${edits.length - i} kept in queue.`, |
| 136 | + ); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + writeFileSync(pendingPath, "", "utf8"); |
| 141 | + console.log(""); |
| 142 | + console.log( |
| 143 | + `trace: reviewed ${edits.length} — ${accepted} accepted, ${skipped} skipped. Queue cleared.`, |
| 144 | + ); |
| 145 | +} |
0 commit comments