import type { ExtensionContext } from "@mariozechner/pi-coding-agent"; import { EXTENSION_ID } from "./constants.js"; import type { ReviewRoundEntry, ReviewState } from "./types.js"; type WritableSessionManager = ExtensionContext["sessionManager"] & { appendCustomEntry(customType: string, data?: unknown): string; }; export function appendReviewRound(ctx: ExtensionContext, data: ReviewRoundEntry): string { return (ctx.sessionManager as WritableSessionManager).appendCustomEntry(EXTENSION_ID, data); } export function restoreReviewState(ctx: ExtensionContext): ReviewState { const rounds: ReviewRoundEntry[] = []; for (const entry of ctx.sessionManager.getBranch()) { if (entry.type !== "custom" || entry.customType !== EXTENSION_ID) continue; const data = entry.data as ReviewRoundEntry | undefined; if (data?.version === 1 && data.kind === "round") rounds.push(data); } return { round: Math.max(0, ...rounds.map((round) => round.round)), findings: rounds.flatMap((round) => round.findings), rounds, }; } export function openFindings(state: ReviewState) { return state.findings.filter((finding) => finding.status === "open"); }