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

friction-cluster.ts

92 lines · 3.2 KB · TypeScript
/**
 * Daily friction clustering: group new friction signals into clusters
 * using the routing/clef layer (or deterministic keyword fallback),
 * create one board card per cluster, and assign one agent per cluster.
 *
 * This module is the pure logic; the job entry calls it.
 */
import { heuristicAssign, buildRepoGroups, orderIssues, type AreaAssignment, type PlannedGroup } from '../friction/grouping.ts'
import type { FrictionIssue, SweepArea } from '../friction/shared.ts'

export type FrictionSignal = {
	id: string
	title: string
	body: string
	labels: string[]
	number: number
	repoRef: string
	createdAt: string
	htmlUrl: string
}

export type ClusterResult = {
	clusters: Array<{
		slug: string
		name: string
		areas: string[]
		signals: FrictionSignal[]
	}>
	deferred: FrictionSignal[]
	method: 'heuristic'
}

const DEFAULT_AREAS: SweepArea[] = [
	{ id: 'ui', description: 'User interface and frontend', keywords: ['ui', 'css', 'react', 'component', 'button', 'page', 'layout', 'style', 'display', 'render'], labels: ['ui', 'frontend'] },
	{ id: 'api', description: 'API and backend logic', keywords: ['api', 'endpoint', 'route', 'server', 'handler', 'middleware', 'request', 'response', 'rest', 'graphql'], labels: ['api', 'backend'] },
	{ id: 'data', description: 'Database, storage, and data', keywords: ['database', 'db', 'migration', 'query', 'schema', 'storage', 'cache', 'redis', 'postgres', 'sql'], labels: ['data', 'database'] },
	{ id: 'infra', description: 'Infrastructure and deployment', keywords: ['deploy', 'ci', 'cd', 'docker', 'kubernetes', 'config', 'env', 'build', 'pipeline', 'infra'], labels: ['infra', 'devops'] },
	{ id: 'docs', description: 'Documentation and developer experience', keywords: ['docs', 'readme', 'documentation', 'guide', 'tutorial', 'example', 'dx'], labels: ['docs', 'documentation'] },
	{ id: 'general', description: 'General or uncategorized', keywords: [], labels: [] },
]

function toFrictionIssue(signal: FrictionSignal): FrictionIssue {
	return {
		number: signal.number,
		title: signal.title,
		body: signal.body,
		labels: signal.labels,
		createdAt: signal.createdAt,
		htmlUrl: signal.htmlUrl,
		userLogin: '',
		updatedAt: signal.createdAt,
		comments: [],
	}
}

export function clusterFrictionSignals(
	signals: FrictionSignal[],
	opts?: { areas?: SweepArea[]; maxGroups?: number; maxPerGroup?: number },
): ClusterResult {
	if (signals.length === 0) {
		return { clusters: [], deferred: [], method: 'heuristic' }
	}

	const areas = opts?.areas ?? DEFAULT_AREAS
	const issues = signals.map(toFrictionIssue)
	const assignments = new Map<number, AreaAssignment>()

	for (const issue of issues) {
		assignments.set(issue.number, heuristicAssign(issue, areas))
	}

	const { groups, deferred } = buildRepoGroups({
		issues,
		assignments,
		areas,
		maxGroups: opts?.maxGroups ?? 5,
		maxPerGroup: opts?.maxPerGroup ?? 8,
	})

	const signalMap = new Map(signals.map((s) => [s.number, s]))

	return {
		clusters: groups.map((g) => ({
			slug: g.slug,
			name: g.name,
			areas: g.areas,
			signals: g.issues.map((i) => signalMap.get(i.number)!).filter(Boolean),
		})),
		deferred: deferred.map((i) => signalMap.get(i.number)!).filter(Boolean),
		method: 'heuristic',
	}
}