repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/tune/jsonl.ts

Raw
File outline
/**
 * JSONL session log — parse and reconstruct tune state.
 *
 * One JSON line per entry. First entry is a `config` header; subsequent
 * entries are run results. Segments are delimited by additional config
 * headers (re-init).
 */

// ── Types ─────────────────────────────────────────────────────────────────

export type JsonlEntry = Record<string, unknown>;

export interface TuneConfigEntry extends JsonlEntry {
	type: "config";
	name?: string;
	metricName?: string;
	metricUnit?: string;
	bestDirection?: "lower" | "higher";
}

export interface TuneRunEntry extends JsonlEntry {
	run: number;
}

export interface ReconstructedMetricDef {
	name: string;
	unit: string;
}

export interface ReconstructedRun {
	run: number;
	commit: string;
	metric: number;
	metrics: Record<string, number>;
	status: "keep" | "discard" | "crash" | "checks_failed";
	description: string;
	timestamp: number;
	segment: number;
	confidence: number | null;
	asi?: Record<string, unknown>;
}

export interface ReconstructedJsonlState {
	name: string | null;
	metricName: string;
	metricUnit: string;
	bestDirection: "lower" | "higher";
	currentSegment: number;
	results: ReconstructedRun[];
	secondaryMetrics: ReconstructedMetricDef[];
}

// ── Constants ─────────────────────────────────────────────────────────────

const DEFAULT_METRIC_NAME = "metric";
const DEFAULT_METRIC_UNIT = "";
const DEFAULT_DIRECTION = "lower" as const;

// ── Helpers ───────────────────────────────────────────────────────────────

function isObjectRecord(value: unknown): value is JsonlEntry {
	return value !== null && typeof value === "object" && !Array.isArray(value);
}

function nonEmptyLines(text: string): string[] {
	return text.split("\n").filter(Boolean);
}

function inferMetricUnit(name: string): string {
	if (name.endsWith("µs")) return "µs";
	if (name.endsWith("_ms")) return "ms";
	if (name.endsWith("_s") || name.endsWith("_sec")) return "s";
	if (name.endsWith("_kb")) return "kb";
	if (name.endsWith("_mb")) return "mb";
	return "";
}

function metricMapFrom(value: unknown): Record<string, number> {
	if (!isObjectRecord(value)) return {};
	const metrics: Record<string, number> = {};
	for (const [name, metric] of Object.entries(value)) {
		if (typeof metric === "number") metrics[name] = metric;
	}
	return metrics;
}

function statusFrom(value: unknown): ReconstructedRun["status"] {
	if (value === "discard") return "discard";
	if (value === "crash") return "crash";
	if (value === "checks_failed") return "checks_failed";
	return "keep";
}

function directionFrom(
	value: unknown,
): ReconstructedJsonlState["bestDirection"] {
	return value === "higher" ? "higher" : DEFAULT_DIRECTION;
}

function asiFrom(value: unknown): Record<string, unknown> | undefined {
	return isObjectRecord(value) ? value : undefined;
}

function reconstructedState(): ReconstructedJsonlState {
	return {
		name: null,
		metricName: DEFAULT_METRIC_NAME,
		metricUnit: DEFAULT_METRIC_UNIT,
		bestDirection: DEFAULT_DIRECTION,
		currentSegment: 0,
		results: [],
		secondaryMetrics: [],
	};
}

function updateConfig(
	state: ReconstructedJsonlState,
	entry: TuneConfigEntry,
): void {
	if (typeof entry.name === "string") state.name = entry.name;
	if (typeof entry.metricName === "string") state.metricName = entry.metricName;
	if (typeof entry.metricUnit === "string") state.metricUnit = entry.metricUnit;
	state.bestDirection = directionFrom(entry.bestDirection);
}

function nextSegment(state: ReconstructedJsonlState, segment: number): number {
	if (state.results.length === 0) return segment;
	state.secondaryMetrics = [];
	return segment + 1;
}

function runFrom(entry: TuneRunEntry, segment: number): ReconstructedRun {
	return {
		run: typeof entry.run === "number" ? entry.run : 0,
		commit: typeof entry.commit === "string" ? entry.commit : "",
		metric: typeof entry.metric === "number" ? entry.metric : 0,
		metrics: metricMapFrom(entry.metrics),
		status: statusFrom(entry.status),
		description: typeof entry.description === "string" ? entry.description : "",
		timestamp: typeof entry.timestamp === "number" ? entry.timestamp : 0,
		segment,
		confidence: typeof entry.confidence === "number" ? entry.confidence : null,
		asi: asiFrom(entry.asi),
	};
}

function registerSecondaryMetrics(
	state: ReconstructedJsonlState,
	metrics: Record<string, number>,
): void {
	for (const name of Object.keys(metrics)) {
		if (state.secondaryMetrics.find((metric) => metric.name === name)) continue;
		state.secondaryMetrics.push({ name, unit: inferMetricUnit(name) });
	}
}

// ── Public API ────────────────────────────────────────────────────────────

export function parseJsonlEntry(line: string): JsonlEntry | null {
	try {
		const parsed = JSON.parse(line);
		return isObjectRecord(parsed) ? parsed : null;
	} catch {
		return null;
	}
}

export function isTuneConfigEntry(entry: unknown): entry is TuneConfigEntry {
	return isObjectRecord(entry) && entry.type === "config";
}

export function isTuneRunEntry(entry: unknown): entry is TuneRunEntry {
	return isObjectRecord(entry) && typeof entry.run === "number";
}

function firstConfigEntry(jsonlContent: string): TuneConfigEntry | null {
	for (const line of nonEmptyLines(jsonlContent)) {
		const entry = parseJsonlEntry(line);
		if (isTuneConfigEntry(entry)) return entry;
	}
	return null;
}

export function hasTuneConfigHeader(jsonlContent: string): boolean {
	return firstConfigEntry(jsonlContent) !== null;
}

export function extractTuneSessionName(jsonlContent: string): string {
	return firstConfigEntry(jsonlContent)?.name || "Tune";
}

export function reconstructJsonlState(
	jsonlContent: string,
): ReconstructedJsonlState {
	const state = reconstructedState();
	let segment = 0;

	for (const line of nonEmptyLines(jsonlContent)) {
		const entry = parseJsonlEntry(line);
		if (!entry) continue;

		if (isTuneConfigEntry(entry)) {
			updateConfig(state, entry);
			segment = nextSegment(state, segment);
			state.currentSegment = segment;
			continue;
		}

		if (!isTuneRunEntry(entry)) continue;

		const run = runFrom(entry, segment);
		state.results.push(run);
		registerSecondaryMetrics(state, run.metrics);
	}

	return state;
}