← 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.
Files
/**
* Card routes: which bot role (if any) a new or re-triaged card wakes, and
* when. Generic and caller-configured (`settings.cardRoutes`); the factory
* emits `@kody/factory.wake.requested` with the role and `@kody/wake-relay`
* maps the role to a bot. First matching rule wins.
*
* `when`:
* - `now`: wake the role as soon as the card exists (or is raised to match).
* - `nightly`: no wake; the nightly friction run works it.
* - `none`: no wake (push rules still apply).
*/
import { hasTag } from './tags.ts'
import { WAKE_ROLES, type WakeRole } from '../events/wake-request.ts'
import type { WorkItem } from '../core/work-item.ts'
export type CardRouteWhen = 'now' | 'nightly' | 'none'
export type CardRoute = {
id: string
match: {
/** Any of these tags (trailing `*` matches a prefix). */
tagsAny?: string[]
/** Card priority at or above this (0 is highest). */
maxPriority?: 0 | 1 | 2 | 3
needsKent?: boolean
}
when: CardRouteWhen
role?: WakeRole
/** A specific bot (skips the relay's role route). */
bot?: string
}
export function defaultCardRoutes(): CardRoute[] {
return [
{ id: 'p0', match: { maxPriority: 0 }, when: 'now', role: 'platform' },
{ id: 'security', match: { tagsAny: ['area:security', 'kind:security', 'security', 'extra-care:security', 'extra-care:secrets', 'extra-care:auth', 'extra-care:isolation', 'extra-care:production-token-split'] }, when: 'now', role: 'platform' },
{ id: 'friction', match: { tagsAny: ['source:friction', 'kind:friction', 'kind:friction:wasteful-context', 'friction'] }, when: 'nightly' },
{ id: 'needs-kent', match: { needsKent: true }, when: 'none' },
{ id: 'package', match: { tagsAny: ['pkg:*'] }, when: 'now', role: 'package-triage' },
{ id: 'platform', match: { tagsAny: ['area:platform', 'area:billing'] }, when: 'now', role: 'platform' },
{ id: 'github', match: { tagsAny: ['source:github'] }, when: 'now', role: 'platform' },
]
}
const ID = /^[a-z0-9][a-z0-9_-]{0,40}$/
const BOT = /^[a-z][a-z0-9_-]{0,40}$/
export function normalizeCardRoutes(raw: unknown): CardRoute[] {
if (!Array.isArray(raw)) return defaultCardRoutes()
const out: CardRoute[] = []
for (const r of raw.slice(0, 50)) {
if (!r || typeof r !== 'object') continue
const v = r as Partial<CardRoute>
if (typeof v.id !== 'string' || !ID.test(v.id)) continue
const when: CardRouteWhen = v.when === 'now' || v.when === 'nightly' ? v.when : 'none'
const m = (v.match && typeof v.match === 'object' ? v.match : {}) as CardRoute['match']
const match: CardRoute['match'] = {}
if (Array.isArray(m.tagsAny)) match.tagsAny = m.tagsAny.filter((t) => typeof t === 'string' && t.length > 0 && t.length <= 120).slice(0, 30)
if (typeof m.maxPriority === 'number' && [0, 1, 2, 3].includes(m.maxPriority)) match.maxPriority = m.maxPriority
if (typeof m.needsKent === 'boolean') match.needsKent = m.needsKent
const role = typeof v.role === 'string' && (WAKE_ROLES as readonly string[]).includes(v.role) ? (v.role as WakeRole) : undefined
const bot = typeof v.bot === 'string' && BOT.test(v.bot) ? v.bot : undefined
if (when === 'now' && !role && !bot) continue
out.push({ id: v.id, match, when, ...(role ? { role } : {}), ...(bot ? { bot } : {}) })
}
return out
}
function matches(route: CardRoute, card: Pick<WorkItem, 'labels' | 'priority' | 'needsKent'>): boolean {
const m = route.match
const keys = Object.keys(m).length
if (keys === 0) return true
if (m.tagsAny && !m.tagsAny.some((t) => hasTag(card.labels, t))) return false
if (typeof m.maxPriority === 'number' && (card.priority === null || card.priority > m.maxPriority)) return false
if (typeof m.needsKent === 'boolean' && card.needsKent !== m.needsKent) return false
return true
}
/** First matching route, or null. */
export function matchCardRoute(routes: ReadonlyArray<CardRoute>, card: Pick<WorkItem, 'labels' | 'priority' | 'needsKent'>): CardRoute | null {
return routes.find((r) => matches(r, card)) ?? null
}