Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/ultra/result.ts

Raw
// ultra result formatting: eager because restored session messages render at startup.

import {
	formatStructuredText,
	formatUsage,
	formatValueMarkdown,
} from "./display.ts";
import type { PhaseSlotResult, WorkflowResult } from "./engine.ts";

// ---------------------------------------------------------------------------
// Result message formatting
// ---------------------------------------------------------------------------

function record(value: unknown): Record<string, unknown> | undefined {
	return typeof value === "object" && value !== null && !Array.isArray(value)
		? (value as Record<string, unknown>)
		: undefined;
}

function text(value: unknown): string | undefined {
	return typeof value === "string" && value.length > 0 ? value : undefined;
}

function itemLine(value: unknown): string {
	const r = record(value);
	if (!r) return formatValueMarkdown(value);
	const titleKey = ["title", "summary", "group", "name", "file", "status"].find(
		(key) => text(r[key]),
	);
	if (!titleKey) return formatValueMarkdown(value);
	const fields = Object.fromEntries(
		Object.entries(r).filter(([key]) => key !== titleKey),
	);
	return [
		`- **${r[titleKey]}**`,
		...(Object.keys(fields).length
			? formatValueMarkdown(fields)
					.split("\n")
					.map((line) => `  ${line}`)
			: []),
	].join("\n");
}

function resultItems(value: unknown): unknown[] {
	if (Array.isArray(value)) return value;
	return value === undefined || value === null ? [] : [value];
}

interface ReviewReport {
	analysis: string;
	findings: unknown[];
	recommendations: string[];
	unresolved: unknown[];
	blockers: string[];
}

interface ResearchReport {
	answerStatus: string;
	executiveSummary: string;
	analysis: string;
	findings: unknown[];
	contradictions: string[];
	gaps: string[];
	recommendations: string[];
	sources: unknown[];
	blockers: string[];
}

function stringArray(value: unknown): string[] {
	return Array.isArray(value)
		? value.filter(
				(item): item is string => typeof item === "string" && item.length > 0,
			)
		: [];
}

function reviewReport(result: WorkflowResult): ReviewReport | undefined {
	if (result.workflow !== "review") return undefined;
	const items = resultItems(result.result);
	if (items.length !== 1) return undefined;
	const value = record(items[0]);
	const analysis = text(value?.analysis);
	if (!analysis || !Array.isArray(value?.findings)) return undefined;
	const recommendations = Array.isArray(value.recommendations)
		? value.recommendations.filter(
				(recommendation): recommendation is string =>
					typeof recommendation === "string" && recommendation.length > 0,
			)
		: [];
	return {
		analysis,
		findings: value.findings,
		recommendations,
		unresolved: Array.isArray(value.unresolved) ? value.unresolved : [],
		blockers: stringArray(value.blockers),
	};
}

function researchReport(result: WorkflowResult): ResearchReport | undefined {
	if (result.workflow !== "research") return undefined;
	const items = resultItems(result.result);
	if (items.length !== 1) return undefined;
	const value = record(items[0]);
	const answerStatus = text(value?.answer_status);
	const executiveSummary = text(value?.executive_summary);
	const analysis = text(value?.analysis);
	if (
		!answerStatus ||
		!executiveSummary ||
		!analysis ||
		!Array.isArray(value?.findings) ||
		!Array.isArray(value.sources)
	)
		return undefined;
	return {
		answerStatus,
		executiveSummary,
		analysis,
		findings: value.findings,
		contradictions: stringArray(value.contradictions),
		gaps: stringArray(value.gaps),
		recommendations: stringArray(value.recommendations),
		sources: value.sources,
		blockers: stringArray(value.blockers),
	};
}

function researchFindingLine(value: unknown): string {
	const finding = record(value);
	if (!finding) return itemLine(value);
	const claim = text(finding.claim);
	if (!claim) return itemLine(value);
	const evidence = text(finding.evidence);
	const confidence = text(finding.confidence);
	const citations = stringArray(finding.citations);
	return [
		`- **${claim}**`,
		...(confidence ? [`  Confidence: ${confidence}`] : []),
		...(evidence ? [`  ${evidence}`] : []),
		...(citations.length > 0 ? [`  Sources: ${citations.join(", ")}`] : []),
	].join("  \n");
}

function researchSourceLine(value: unknown): string {
	const source = record(value);
	if (!source) return itemLine(value);
	const title = text(source.title) ?? "Untitled source";
	const url = text(source.url);
	const sourceType = text(source.source_type);
	const publishedAt = text(source.published_at);
	const metadata = [sourceType, publishedAt, url].filter(Boolean).join(" · ");
	return `- **${title}**${metadata ? `  \n  ${metadata}` : ""}`;
}

function verdictSlots(
	result: WorkflowResult,
	status: string,
): PhaseSlotResult[] {
	if (result.workflow !== "review") return [];
	return Object.values(result.phaseResults)
		.flat()
		.filter((slot) => {
			const v = record(slot.value);
			// Historical real=false did not distinguish uncertainty from refutation.
			return (
				slot.ok &&
				(v?.status === status ||
					(status === "unresolved" &&
						v?.status === undefined &&
						v?.real === false))
			);
		});
}

function compactFailureMessage(message: string): string {
	return message.length > 160 ? `${message.slice(0, 159)}…` : message;
}

function dropLines(result: WorkflowResult, expanded: boolean): string[] {
	const lines: string[] = [];
	for (const failures of Object.values(result.phaseFailures)) {
		for (const failure of failures) {
			const message = expanded
				? failure.message
				: compactFailureMessage(failure.message);
			lines.push(`- ${message}  \n  ${failure.agentId} [${failure.code}]`);
			if (!expanded) continue;
			lines.push(
				`  retryable: ${failure.retryable ? "yes" : "no"}`,
				`  attempts: ${failure.attempts}`,
			);
			if (failure.model) lines.push(`  model: ${failure.model}`);
			if (failure.thinkingLevel)
				lines.push(`  thinking: ${failure.thinkingLevel}`);
			if (failure.item !== undefined)
				lines.push(
					"",
					"  **Item**",
					...formatValueMarkdown(failure.item)
						.split("\n")
						.map((line) => `  ${line}`),
				);
			if (failure.transcriptTail)
				lines.push(
					"",
					"  **Transcript tail**",
					...formatStructuredText(failure.transcriptTail)
						.split("\n")
						.map((line) => `  ${line}`),
				);
		}
	}
	return lines;
}

function plural(count: number, singular: string): string {
	return `${count} ${singular}${count === 1 ? "" : "s"}`;
}

function summaryLine(
	result: WorkflowResult,
	finalCount: number,
	refutedCount: number,
	droppedCount: number,
): string {
	const incomplete =
		droppedCount > 0
			? ` Review incomplete: ${plural(droppedCount, "step")} dropped.`
			: "";
	if (result.workflow === "review") {
		const findings =
			finalCount === 0
				? "No verified findings."
				: `${plural(finalCount, "verified finding")}.`;
		const refuted =
			refutedCount > 0
				? ` ${plural(refutedCount, "candidate finding")} refuted.`
				: "";
		return `${findings}${refuted}${incomplete}`;
	}
	return `${plural(finalCount, "final result")}.${incomplete}`;
}

function reportMarkdown(value: unknown): string {
	if (typeof value === "string") return value;
	if (
		Array.isArray(value) &&
		value.length > 0 &&
		value.every((item) => typeof item === "string")
	)
		return value.join("\n\n");
	return formatValueMarkdown(value);
}

function recommendationLines(
	result: WorkflowResult,
	finalCount: number,
	droppedCount: number,
): string[] {
	if (result.workflow === "review") {
		if (droppedCount > 0)
			return [
				"- Inspect dropped steps and rerun review before relying on the result.",
			];
		if (finalCount > 0)
			return ["- Address verified findings, then rerun review."];
		return [
			"- No code changes recommended from this review.",
			"- Continue normal project validation.",
		];
	}
	if (droppedCount > 0)
		return ["- Inspect dropped steps and rerun the workflow."];
	if (finalCount > 0) return ["- Continue with the final results above."];
	return ["- No follow-up action indicated."];
}

export function formatUltraResultMarkdown(
	result: WorkflowResult,
	expanded = false,
): string {
	if (Object.hasOwn(result, "report")) return reportMarkdown(result.report);
	const report = reviewReport(result);
	const research = researchReport(result);
	const selected = resultItems(result.result);
	const finalItems =
		report?.findings ??
		research?.findings ??
		(result.workflow === "review"
			? selected.filter((item) => {
					const value = record(item);
					return (
						value?.status !== "refuted" &&
						value?.status !== "unresolved" &&
						value?.real !== false
					);
				})
			: selected);
	const lines = [
		`# ultra ${result.workflow}${result.steered ? " (steered)" : ""}`,
	];
	lines.push("");
	if (result.tokenUsage) {
		const usage = formatUsage(result.tokenUsage);
		lines.push(
			`Usage: ${usage.summary}${expanded ? `  \n${usage.detail}` : ""}`,
			"",
		);
	}
	lines.push(
		result.phases
			.map((p) => `${p.id}: ${p.ok}/${p.ran} ok, ${p.dropped} dropped`)
			.join(" · ") || "No phases.",
		"",
	);

	if (result.fullOutputPath)
		lines.push(`Full evidence: \`${result.fullOutputPath}\``, "");
	const selectedReport =
		selected.length === 1 ? record(selected[0]) : undefined;
	if (text(selectedReport?.status))
		lines.push(`Status: ${selectedReport?.status}`, "");
	const refuted = verdictSlots(result, "refuted");
	const unresolved = report?.unresolved.length
		? report.unresolved
		: verdictSlots(result, "unresolved").map((slot) => slot.value);
	const blockers = report?.blockers ?? research?.blockers ?? [];
	const droppedCount = result.phases.reduce(
		(total, phase) => total + phase.dropped,
		0,
	);
	if (report) lines.push("## Analysis", report.analysis, "");
	else if (research)
		lines.push(
			"## Answer",
			`${research.executiveSummary}  `,
			`Status: ${research.answerStatus}`,
			"",
			"## Analysis",
			research.analysis,
			"",
		);
	else
		lines.push(
			"## Summary",
			summaryLine(result, finalItems.length, refuted.length, droppedCount),
			"",
		);

	if (finalItems.length > 0) {
		lines.push(
			result.workflow === "review"
				? "## Verified findings"
				: research
					? "## Findings"
					: "## Final results",
			...(research
				? finalItems.map(researchFindingLine)
				: finalItems.map(itemLine)),
			"",
		);
	}

	if (refuted.length > 0) {
		lines.push(
			"## Refuted findings",
			...refuted.map((s) => itemLine(s.value)),
			"",
		);
	}

	if (unresolved.length > 0)
		lines.push("## Unresolved findings", ...unresolved.map(itemLine), "");
	if (blockers.length > 0)
		lines.push("## Blockers", ...blockers.map((item) => `- ${item}`), "");

	if (research?.contradictions.length)
		lines.push(
			"## Contradictions",
			...research.contradictions.map((item) => `- ${item}`),
			"",
		);
	if (research?.gaps.length)
		lines.push(
			"## Research gaps",
			...research.gaps.map((item) => `- ${item}`),
			"",
		);

	const drops = dropLines(result, expanded);
	if (drops.length > 0) lines.push("## Drops", ...drops, "");
	const dynamicExtensions = result.dynamicExtensions ?? [];
	if (dynamicExtensions.length > 0)
		lines.push(
			"## Dynamic extensions",
			...dynamicExtensions.map(
				(extension) =>
					`- **${extension.name}**: ${extension.description}  \n  Source: \`${extension.sourcePath}\``,
			),
			"",
		);
	lines.push(
		"## Recommendations",
		...(report?.recommendations.length
			? report.recommendations.map((recommendation) => `- ${recommendation}`)
			: research?.recommendations.length
				? research.recommendations.map(
						(recommendation) => `- ${recommendation}`,
					)
				: unresolved.length > 0 || blockers.length > 0
					? [
							"- Resolve the reported blockers and verification gaps before relying on this result.",
						]
					: recommendationLines(result, finalItems.length, droppedCount)),
		"",
	);
	if (research?.sources.length)
		lines.push("## Sources", ...research.sources.map(researchSourceLine), "");

	if (expanded) lines.push("## Workflow details", formatValueMarkdown(result));
	return lines.join("\n").trimEnd();
}