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

schema.ts

123 lines · 3.8 KB · TypeScript
/**
 * Config document schema: the shape of an exported factory configuration.
 * No secrets ever appear in a config document; credential needs are listed
 * by name only. Import validates against this schema before applying.
 */

export const CONFIG_VERSION = 1

export type ConfigDocument = {
	version: typeof CONFIG_VERSION
	exportedAt: string
	/** Projects and their repo mappings, signal sources, and runner assignments. */
	projects: ConfigProject[]
	/** Non-secret factory settings. No credential values or account ids. */
	settings: ConfigSettings
	/** Which jobs are enabled. */
	jobs: ConfigJob[]
	/** Credential needs (names only, never values). */
	credentialNeeds: string[]
	/** Template id this config was derived from, if any. */
	templateId?: string
}

export type ConfigProject = {
	id: string
	displayName: string
	enabled: boolean
	defaultRunnerId: string
	repos: Array<{
		repoRef: string
		host: string
		enabled: boolean
	}>
	signalSources: string[]
	sources?: {
		githubRelease?: { enabled: boolean; repos?: string[]; expectWithinHours?: number }
	}
	paused?: boolean
}

export type ConfigSettings = {
	shadowMode: boolean
	maxConcurrentRuns: number
	agentMayMerge: string
	sweepStartLimit: number
	agentDryRunDefault: boolean
	killSwitch: boolean
	postMergeFixTimeoutMinutes: number
	postMergePendingTimeoutMinutes: number
	stallHealthEnabled: boolean
	columnTimeLimits?: Record<string, number>
	bugbotCursorLogin?: string
	shipPrSummaryChannelId?: string
	discordNotificationsEnabled?: boolean
	discordNotificationsChannelId?: string
	notificationRules?: import('../notifications/types.ts').NotificationRules
}

export type ConfigJob = {
	name: string
	enabled: boolean
	schedule: string
	description: string
}

export type ConfigDiff = {
	settings: Array<{ key: string; from: unknown; to: unknown }>
	projects: {
		added: string[]
		removed: string[]
		changed: Array<{ id: string; changes: Array<{ key: string; from: unknown; to: unknown }> }>
	}
	jobs: Array<{ name: string; from: boolean; to: boolean }>
}

export type ValidationResult =
	| { valid: true; doc: ConfigDocument }
	| { valid: false; errors: string[] }

export function validateConfigDocument(raw: unknown): ValidationResult {
	if (!raw || typeof raw !== 'object') {
		return { valid: false, errors: ['Config document must be a JSON object'] }
	}
	const doc = raw as Record<string, unknown>
	const errors: string[] = []

	if (doc.version !== CONFIG_VERSION) {
		errors.push(`Unsupported version ${String(doc.version)}; expected ${CONFIG_VERSION}`)
	}
	if (typeof doc.exportedAt !== 'string') {
		errors.push('Missing exportedAt timestamp')
	}
	if (!Array.isArray(doc.projects)) {
		errors.push('projects must be an array')
	} else {
		for (const [i, p] of (doc.projects as unknown[]).entries()) {
			if (!p || typeof p !== 'object') {
				errors.push(`projects[${i}] must be an object`)
				continue
			}
			const proj = p as Record<string, unknown>
			if (typeof proj.id !== 'string' || !proj.id) errors.push(`projects[${i}].id is required`)
			if (!Array.isArray(proj.repos)) errors.push(`projects[${i}].repos must be an array`)
		}
	}
	if (!doc.settings || typeof doc.settings !== 'object') {
		errors.push('settings must be an object')
	} else {
		const s = doc.settings as Record<string, unknown>
		if (typeof s.shadowMode !== 'boolean') errors.push('settings.shadowMode must be a boolean')
		if (typeof s.maxConcurrentRuns !== 'number') errors.push('settings.maxConcurrentRuns must be a number')
		if (typeof s.killSwitch !== 'boolean') errors.push('settings.killSwitch must be a boolean')
	}
	if (!Array.isArray(doc.jobs)) {
		errors.push('jobs must be an array')
	}
	if (!Array.isArray(doc.credentialNeeds)) {
		errors.push('credentialNeeds must be an array')
	}

	if (errors.length > 0) return { valid: false, errors }
	return { valid: true, doc: doc as unknown as ConfigDocument }
}