Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

eval.ts

72 lines · 2.3 KB · TypeScript
/**
 * Signals routing eval: build fixtures from real factory data, run the
 * two-tier routing model, and report accuracy.
 *
 * Run in the kody org where the credentials are mapped:
 *
 * @example
 * import signalsEval from 'kody:@kody/kody-factory/signals/eval'
 * // Build fixtures only (no model calls):
 * await signalsEval({ action: 'build' })
 * // Build fixtures and run routing model against them:
 * await signalsEval({ action: 'run' })
 * // Build fixtures and run, with custom GitHub repo and Sentry org:
 * await signalsEval({ action: 'run', githubRepo: 'kody-hq/kody' })
 */
import { buildAllFixtures, fixturesFromGithub, fixturesFromLedger, fixturesFromSentry, fixturesFromFriction } from '../../signals/eval/build-fixtures.ts'
import { runEval, type EvalReport } from '../../signals/eval/run.ts'

type EvalInput =
	| { action: 'build'; githubRepo?: string }
	| { action: 'run'; githubRepo?: string }

export default async function signalsEval(input: EvalInput) {
	if (input.action === 'build') {
		const { fixtures, sources } = await buildAllFixtures()
		return {
			ok: true,
			action: 'build',
			fixtureCount: fixtures.length,
			sources,
			fixtures: fixtures.map((f) => ({
				id: f.id,
				title: f.title.slice(0, 100),
				source: f.source,
				expectedLabel: f.expectedLabel,
				origin: f.origin,
			})),
		}
	}

	if (input.action === 'run') {
		const { fixtures, sources } = await buildAllFixtures()
		if (fixtures.length === 0) {
			return {
				ok: false,
				action: 'run',
				error: 'No fixtures were built. Check that the GitHub credential is mapped and the ledger has items with outcomes.',
				sources,
			}
		}
		const report = await runEval(fixtures)
		return {
			ok: true,
			action: 'run',
			fixtureCount: report.total,
			sources,
			accuracy: report.accuracy,
			escalationRate: report.escalationRate,
			avgLatencyMs: report.avgLatencyMs,
			modelUsed: report.modelUsed,
			perLabel: report.perLabel,
			confusionMatrix: report.confusionMatrix,
			incorrect: report.incorrect,
			summary: `${report.correct}/${report.total} correct (${(report.accuracy * 100).toFixed(1)}%), ` +
				`escalation rate ${(report.escalationRate * 100).toFixed(1)}%, ` +
				`avg latency ${report.avgLatencyMs}ms, ` +
				`model: ${report.modelUsed ?? 'heuristic fallback'}`,
		}
	}

	throw new Error(`Unknown action: ${(input as { action: string }).action}`)
}