Skip to content
← Public packages

@kentcdodds/codebase-cleanup

Cursor cloud agents for legacy cleanup, docs freshness, tight tests, and prod CI fixes, plus a weekly Devin general audit.

FollowFork

Files

config.ts

208 lines · 6.0 KB · TypeScript
import {
	devinModes,
	isAutomationId,
	isDevinMode,
	unknownAutomationMessage,
	type AutomationConfig,
	type AutomationId,
} from './shared.ts'
import {
	addRepo as storageAddRepo,
	getAutomation,
	getConfig,
	listRecentLaunches,
	removeRepo as storageRemoveRepo,
	setAutomation,
	setConfig,
} from './storage.ts'

function requireAutomationId(value: unknown): AutomationId {
	if (!isAutomationId(value)) {
		throw new Error(unknownAutomationMessage())
	}
	return value
}

/**
 * List all automations and their stored config (repos, enabled, model, schedule).
 *
 * @example
 * import { listConfig } from 'kody:@kentcdodds/codebase-cleanup/config'
 * const config = await listConfig()
 */
export async function listConfig() {
	return await getConfig()
}

/**
 * Get one automation's config.
 *
 * @param input.automation - Automation id
 * @example
 * import { get } from 'kody:@kentcdodds/codebase-cleanup/config'
 * const item = await get({ automation: 'legacy-reaper' })
 */
export async function get(input: { automation: AutomationId }) {
	const id = requireAutomationId(input?.automation)
	return { automation: id, ...(await getAutomation(id)) }
}

/**
 * Patch one automation's config (enabled, repos, branch, model, schedule, skipIfNoCommits24h, notify,
 * for prod-ci-failure: ciWorkflows, recheckDelaySeconds; for general-audit (Devin): devinMode, maxAcuLimit;
 * for docs-freshness / keep-tests-tight: maxAgentsPerDay, chunkMin, chunkMax, triageThreshold, fullSweepWeekday).
 * `notify` is a Kody export spec agents call to reach you, or null to report in the PR.
 * Unknown keys are rejected.
 *
 * @example
 * import { set } from 'kody:@kentcdodds/codebase-cleanup/config'
 * await set({ automation: 'docs-freshness', enabled: true })
 */
export async function set(input: {
	automation: AutomationId
	enabled?: boolean
	repos?: string[]
	branch?: string
	model?: AutomationConfig['model']
	schedule?: string | null
	skipIfNoCommits24h?: boolean
	notify?: string | null
	ciWorkflows?: string[]
	recheckDelaySeconds?: number
	devinMode?: AutomationConfig['devinMode']
	maxAcuLimit?: number | null
	maxAgentsPerDay?: number
	chunkMin?: number
	chunkMax?: number
	triageThreshold?: number
	fullSweepWeekday?: number | null
}) {
	const id = requireAutomationId(input?.automation)
	const allowed = new Set([
		'automation',
		'enabled',
		'repos',
		'branch',
		'model',
		'schedule',
		'skipIfNoCommits24h',
		'notify',
		'ciWorkflows',
		'recheckDelaySeconds',
		'devinMode',
		'maxAcuLimit',
		'maxAgentsPerDay',
		'chunkMin',
		'chunkMax',
		'triageThreshold',
		'fullSweepWeekday',
	])
	for (const key of Object.keys(input || {})) {
		if (!allowed.has(key)) {
			throw new Error(`Unknown config key: ${key}`)
		}
	}
	const patch: Partial<AutomationConfig> = {}
	if (typeof input.enabled === 'boolean') patch.enabled = input.enabled
	if (Array.isArray(input.repos)) patch.repos = input.repos
	if (typeof input.branch === 'string') patch.branch = input.branch
	if (input.model) patch.model = input.model
	if (input.schedule === null || typeof input.schedule === 'string') {
		patch.schedule = input.schedule
	}
	if (typeof input.skipIfNoCommits24h === 'boolean') {
		patch.skipIfNoCommits24h = input.skipIfNoCommits24h
	}
	if (input.notify === null || typeof input.notify === 'string') {
		patch.notify = input.notify
	}
	if (Array.isArray(input.ciWorkflows)) patch.ciWorkflows = input.ciWorkflows
	if (typeof input.recheckDelaySeconds === 'number') {
		patch.recheckDelaySeconds = input.recheckDelaySeconds
	}
	if (input.devinMode !== undefined) {
		if (!isDevinMode(input.devinMode)) {
			throw new Error(`Unknown devinMode. Expected one of: ${devinModes.join(', ')}`)
		}
		patch.devinMode = input.devinMode
	}
	if (input.maxAcuLimit === null || typeof input.maxAcuLimit === 'number') {
		patch.maxAcuLimit = input.maxAcuLimit
	}
	for (const key of ['maxAgentsPerDay', 'chunkMin', 'chunkMax', 'triageThreshold'] as const) {
		if (typeof input[key] === 'number') patch[key] = input[key]
	}
	if (input.fullSweepWeekday === null || typeof input.fullSweepWeekday === 'number') {
		patch.fullSweepWeekday = input.fullSweepWeekday
	}
	const next = await setAutomation(id, patch)
	return { automation: id, ...next }
}

/**
 * Replace the full automations config object.
 *
 * @example
 * import { replaceAll } from 'kody:@kentcdodds/codebase-cleanup/config'
 * await replaceAll({ automations: { ... } })
 */
export async function replaceAll(input: {
	automations: Record<AutomationId, AutomationConfig>
}) {
	if (!input?.automations || typeof input.automations !== 'object') {
		throw new Error('automations object is required')
	}
	return await setConfig({ automations: input.automations })
}

/**
 * Add a repository (owner/repo) to an automation.
 *
 * @example
 * import { addRepo } from 'kody:@kentcdodds/codebase-cleanup/config'
 * await addRepo({ automation: 'legacy-reaper', repo: 'acme/other' })
 */
export async function addRepo(input: { automation: AutomationId; repo: string }) {
	const id = requireAutomationId(input?.automation)
	if (!input?.repo) throw new Error('repo is required')
	const next = await storageAddRepo(id, input.repo)
	return { automation: id, ...next }
}

/**
 * Remove a repository from an automation.
 *
 * @example
 * import { removeRepo } from 'kody:@kentcdodds/codebase-cleanup/config'
 * await removeRepo({ automation: 'legacy-reaper', repo: 'acme/other' })
 */
export async function removeRepo(input: {
	automation: AutomationId
	repo: string
}) {
	const id = requireAutomationId(input?.automation)
	if (!input?.repo) throw new Error('repo is required')
	const next = await storageRemoveRepo(id, input.repo)
	return { automation: id, ...next }
}

/**
 * Recent launch / dryRun records for debugging.
 *
 * @example
 * import { recentLaunches } from 'kody:@kentcdodds/codebase-cleanup/config'
 * await recentLaunches({ limit: 10 })
 */
export async function recentLaunches(input: { limit?: number } = {}) {
	return await listRecentLaunches(input.limit ?? 20)
}

export default {
	listConfig,
	get,
	set,
	replaceAll,
	addRepo,
	removeRepo,
	recentLaunches,
}