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

config.ts

106 lines · 4.0 KB · TypeScript
/**
 * Switches for the factory health error feed. Everything defaults to off, so a
 * publish changes nothing until the owner turns a switch on. Kept under its own
 * storage key: saving a switch never rewrites factory settings (shadow mode,
 * pilot scope).
 */
import { packageContext } from 'kody:runtime'
import { loadJson, saveJson } from '../lib/storage.ts'
import { DEFAULT_ORG, normalizeOrg } from './fingerprint.ts'

const CONFIG_KEY = 'factory:health:config:v1'
const ORG_KEY = 'factory:health:org:v1'

/** Patch, the Grok Bot that owns package and run-error triage. */
export const PATCH_BOT_ID = 'a2600d4e-d242-4709-8a9f-f756ad2d7e82'
export const PATCH_BOT_NAME = 'patch'

export type HealthConfig = {
	/** Record run.error.recorded events as fingerprints (and soft-triage known noise). */
	intakeEnabled: boolean
	/** Let the sweep scan open error runs and drain recorded outcomes into Activity. */
	reconcileEnabled: boolean
	/** Let the sweep wake Patch. Off never sends a wake, whatever else is on. */
	wakesEnabled: boolean
	/** Org slug override for fingerprints and Activity links; null learns it from events. */
	org: string | null
	wakeBotId: string
	wakeBotName: string
}

export const SWITCHES = ['intakeEnabled', 'reconcileEnabled', 'wakesEnabled'] as const
export type HealthSwitch = (typeof SWITCHES)[number]

export function defaultHealthConfig(): HealthConfig {
	return {
		intakeEnabled: false,
		reconcileEnabled: false,
		wakesEnabled: false,
		org: null,
		wakeBotId: PATCH_BOT_ID,
		wakeBotName: PATCH_BOT_NAME,
	}
}

export function normalizeHealthConfig(raw: unknown): HealthConfig {
	const r = (raw && typeof raw === 'object' ? raw : {}) as Partial<Record<keyof HealthConfig, unknown>>
	const base = defaultHealthConfig()
	const org = typeof r.org === 'string' && /^[a-z0-9][a-z0-9-]{0,62}$/.test(r.org) ? r.org : null
	const botId = typeof r.wakeBotId === 'string' && /^[A-Za-z0-9][A-Za-z0-9-]{0,99}$/.test(r.wakeBotId) ? r.wakeBotId : base.wakeBotId
	const botName = typeof r.wakeBotName === 'string' && /^[a-z0-9][a-z0-9-]{0,39}$/.test(r.wakeBotName) ? r.wakeBotName : base.wakeBotName
	return {
		intakeEnabled: r.intakeEnabled === true,
		reconcileEnabled: r.reconcileEnabled === true,
		wakesEnabled: r.wakesEnabled === true,
		org,
		wakeBotId: botId,
		wakeBotName: botName,
	}
}

export async function loadHealthConfig(): Promise<HealthConfig> {
	return normalizeHealthConfig(await loadJson<unknown>(CONFIG_KEY, null))
}

export async function patchHealthConfig(patch: Partial<HealthConfig>): Promise<HealthConfig> {
	const next = normalizeHealthConfig({ ...(await loadHealthConfig()), ...patch })
	await saveJson(CONFIG_KEY, next)
	return next
}

/**
 * The owning org from a hosted app host. Two forms: `<org>.kody.run` (the
 * package is in the path) and `<pkg>--<org>.kody.run` (the org follows the
 * last double hyphen).
 */
export function orgFromHost(host: string): string | null {
	const match = /^([a-z0-9][a-z0-9-]*)\.kody\.run$/.exec(host.toLowerCase())
	if (!match) return null
	const label = match[1]!
	const split = label.lastIndexOf('--')
	const org = split === -1 ? label : label.slice(split + 2)
	return /^[a-z0-9][a-z0-9-]*$/.test(org) ? org : null
}

function orgFromHostedUrl(): string | null {
	try {
		return orgFromHost(new URL(String(packageContext?.hostedUrl ?? '')).hostname)
	} catch {
		return null
	}
}

/** Remember the org a trusted event named, so the sweep (which gets no event) can use it. */
export async function rememberOrg(org: string): Promise<void> {
	const current = await loadJson<{ org?: string } | null>(ORG_KEY, null)
	if (current?.org !== org) await saveJson(ORG_KEY, { org })
}

export async function resolveOrg(config: HealthConfig): Promise<string> {
	if (config.org) return config.org
	const learned = await loadJson<{ org?: string } | null>(ORG_KEY, null)
	return normalizeOrg(learned?.org || orgFromHostedUrl() || DEFAULT_ORG)
}

/** Origin for Activity links. Activity lives on the main site, not the app host. */
export const ACTIVITY_ORIGIN = 'https://kody.codes'