import { defaultStatusBadge, type PanelItem, type PanelItemState, type PanelSection, type PanelStatus, renderCommandPanel, type RenderedCommandPanel, } from "../../../vera-theme/src/public"; export type Severity = PanelStatus; export interface BinarySnapshot { /** Resolved runtime binary path; null when neither embedded nor system fallback is available. */ resolvedPath: string | null; /** Whether the resolved path points at the embedded bundle directory. */ embedded: boolean; /** sha256 verification result for the bundled artifact. */ integrity: { ok: boolean; reason?: string; expected?: string; actual?: string; }; } export interface StatusSnapshot { target: { platform: string; arch: string; key: string }; manifestGeneratedAt: string; rg: BinarySnapshot; fd: BinarySnapshot; systemGrep: boolean; systemFind: boolean; } export interface EnsureSnapshot { target: { platform: string; arch: string; key: string }; rg: { path: string; integrity: { ok: boolean; reason?: string } }; fd: { path: string; integrity: { ok: boolean; reason?: string } }; } export interface RenderedReport extends RenderedCommandPanel { severity: Severity; } function binaryState(b: BinarySnapshot): PanelItemState { if (!b.resolvedPath) return "bad"; if (!b.embedded || !b.integrity.ok) return "warn"; return "ok"; } function binarySourceLabel(b: BinarySnapshot): string { if (!b.resolvedPath) return "missing"; return b.embedded ? "embedded" : "system"; } function integrityLabel(integrity: BinarySnapshot["integrity"]): string { if (integrity.ok) return "verified"; const parts = [integrity.reason ?? "unverified"]; if (integrity.expected) parts.push(`expected=${integrity.expected.slice(0, 12)}…`); if (integrity.actual) parts.push(`actual=${integrity.actual.slice(0, 12)}…`); return parts.join(" "); } export function computeStatusSeverity(snapshot: StatusSnapshot): Severity { const rg = binaryState(snapshot.rg); const fd = binaryState(snapshot.fd); if (rg === "bad" || fd === "bad") return "degraded"; if (rg === "warn" || fd === "warn") return "partial"; return "ok"; } function summarySection(snapshot: StatusSnapshot): PanelSection { const rg = binaryState(snapshot.rg); const fd = binaryState(snapshot.fd); const worstBinary: PanelItemState = rg === "bad" || fd === "bad" ? "bad" : rg === "warn" || fd === "warn" ? "warn" : "ok"; const bothEmbeddedVerified = snapshot.rg.embedded && snapshot.rg.integrity.ok && snapshot.fd.embedded && snapshot.fd.integrity.ok; const bundleValue = bothEmbeddedVerified ? "embedded + verified" : snapshot.rg.resolvedPath && snapshot.fd.resolvedPath ? "system fallback or unverified" : "missing"; const bundleState: PanelItemState = bothEmbeddedVerified ? "ok" : snapshot.rg.resolvedPath && snapshot.fd.resolvedPath ? "warn" : "bad"; const fallbackValue = snapshot.systemGrep && snapshot.systemFind ? "system grep/find present" : snapshot.systemGrep || snapshot.systemFind ? "partial system fallback" : "no system fallback"; const fallbackState: PanelItemState = snapshot.systemGrep && snapshot.systemFind ? "ok" : "warn"; const coreValue = worstBinary === "bad" ? "search binaries unavailable" : worstBinary === "warn" ? "binaries available with caveats" : "ready"; const items: PanelItem[] = [ { label: "core", value: coreValue, state: worstBinary }, { label: "bundle", value: bundleValue, state: bundleState }, { label: "fallback", value: fallbackValue, state: fallbackState }, ]; return { items }; } function runtimeSection(snapshot: StatusSnapshot): PanelSection { return { heading: "RUNTIME", items: [ { label: "platform", value: snapshot.target.platform, state: "ok" }, { label: "arch", value: snapshot.target.arch, state: "ok" }, { label: "target", value: snapshot.target.key, state: "ok" }, { label: "manifest", value: snapshot.manifestGeneratedAt, state: "ok" }, ], }; } function binariesSection(snapshot: StatusSnapshot): PanelSection { const buildItem = (name: "rg" | "fd", b: BinarySnapshot): PanelItem => { const sourceLabel = binarySourceLabel(b); const integrityText = integrityLabel(b.integrity); const value = b.resolvedPath ? `${sourceLabel} · ${integrityText}` : sourceLabel; return { label: name, value, state: binaryState(b), details: b.resolvedPath ? [b.resolvedPath] : undefined, }; }; return { heading: "BINARIES", items: [buildItem("rg", snapshot.rg), buildItem("fd", snapshot.fd)], }; } function fallbackSection(snapshot: StatusSnapshot): PanelSection { return { heading: "FALLBACK", items: [ { label: "system grep", value: snapshot.systemGrep ? "present" : "missing", state: snapshot.systemGrep ? "ok" : "warn" }, { label: "system find", value: snapshot.systemFind ? "present" : "missing", state: snapshot.systemFind ? "ok" : "warn" }, ], }; } export function renderStatusReport(snapshot: StatusSnapshot): RenderedReport { const severity = computeStatusSeverity(snapshot); const panel = renderCommandPanel({ name: "search-tools", status: severity, badge: defaultStatusBadge(severity), meta: snapshot.target.key, sections: [ summarySection(snapshot), runtimeSection(snapshot), binariesSection(snapshot), fallbackSection(snapshot), ], }); return { ...panel, severity }; } export function renderEnsureReport(snapshot: EnsureSnapshot): RenderedReport { const ok = snapshot.rg.integrity.ok && snapshot.fd.integrity.ok; const severity: Severity = ok ? "ok" : "degraded"; const buildItem = (name: "rg" | "fd", entry: EnsureSnapshot["rg"]): PanelItem => ({ label: name, value: entry.integrity.ok ? "verified" : entry.integrity.reason ?? "unverified", state: entry.integrity.ok ? "ok" : "bad", details: entry.path ? [entry.path] : undefined, }); const panel = renderCommandPanel({ name: "search-tools", status: severity, badge: ok ? "[ OK ]" : "[ FAILED ]", meta: snapshot.target.key, sections: [ { heading: "ENSURE", items: [buildItem("rg", snapshot.rg), buildItem("fd", snapshot.fd)], }, ], }); return { ...panel, severity }; } export function renderEnsureFailure(message: string, target: { key: string }): RenderedReport { const panel = renderCommandPanel({ name: "search-tools", status: "degraded", badge: "[ FAILED ]", meta: target.key, sections: [ { heading: "ERROR", items: [{ label: "reason", value: message, state: "bad" }], }, ], }); return { ...panel, severity: "degraded" }; }