repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/diff-review/format.ts

Raw
File outline
import type { CollectedDiff, Finding, ReviewRoundEntry } from "./types.js";

export function formatReviewMarkdown(round: ReviewRoundEntry): string {
	const lines: string[] = [`# Diff Review Round ${round.round}`, ""];
	if (round.diffSummary.skippedFiles.length > 0) lines.push("## Skipped Files", ...round.diffSummary.skippedFiles.map((file) => `- ${file}`), "");
	if (round.findings.length === 0) {
		lines.push("No findings recorded.", "", "Use this review result to decide whether any follow-up is needed. Do not edit files yet.");
		return lines.join("\n");
	}

	lines.push("## Findings");
	for (const [index, finding] of round.findings.entries()) lines.push(formatFinding(finding, index + 1));
	if (round.notes?.trim()) lines.push("", "## Notes", round.notes.trim());
	lines.push("", "Use this review to propose and discuss a fix plan first. Do not edit files yet.");
	return lines.join("\n");
}

export function makeReviewRound(round: number, diff: CollectedDiff, findings: Finding[], notes?: string): ReviewRoundEntry {
	return {
		version: 1,
		kind: "round",
		round,
		source: diff.source,
		findings,
		...(notes?.trim() && { notes: notes.trim() }),
		diffSummary: {
			filesChanged: diff.files.length,
			additions: diff.additions,
			deletions: diff.deletions,
			skippedFiles: diff.skippedFiles,
		},
		createdAt: new Date().toISOString(),
	};
}

function formatFinding(finding: Finding, index: number): string {
	const range = finding.startLine === finding.endLine ? `${finding.startLine}` : `${finding.startLine}-${finding.endLine}`;
	const location = `${finding.file}:${range}`;
	const lineSemantics =
		finding.side === "additions"
			? "file line number on new/additions side (current file)"
			: "file line number on old/deletions side (preimage; may not exist in working tree)";
	const context = [finding.anchor.before, finding.anchor.selected, finding.anchor.after].filter(Boolean).join("\n");
	const lines = [
		`### Finding ${index}: [${finding.severity}][${finding.category}] ${location} ${finding.side}`,
		"",
		`- Location: \`${location}\``,
		`- Side: \`${finding.side}\``,
		`- Line semantics: ${lineSemantics}`,
		`- Comment: ${finding.comment}`,
	];
	if (finding.notes?.trim()) lines.push(`- Notes: ${finding.notes.trim()}`);
	lines.push("", "Selected code:", fencedText(finding.anchor.selected));
	if (context.trim()) lines.push("", "Nearby context:", fencedText(context));
	return lines.join("\n");
}

function fencedText(text: string): string {
	const fence = longestBacktickRun(text) >= 3 ? "`".repeat(longestBacktickRun(text) + 1) : "```";
	return `${fence}text\n${text}\n${fence}`;
}

function longestBacktickRun(text: string): number {
	let longest = 0;
	let current = 0;
	for (const char of text) {
		if (char === "`") current++;
		else {
			longest = Math.max(longest, current);
			current = 0;
		}
	}
	return Math.max(longest, current);
}