← 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
/**
* Triage one card: set its priority or needs-kent, add or remove tags.
* Emits `@kody/factory.card.triaged`; a raise to the push threshold pushes
* (once per card) and a move onto a different card route wakes that role.
*
* @example
* import triage from 'kody:@kody/kody-factory/cards/triage'
* await triage({ cardId: 'wi_123', priority: 1, addTags: ['area:billing'] })
*/
import { loadJson, saveJson, storageKeys } from '../lib/storage.ts'
import { emptyLedger, nowIso, type Ledger, type WorkItem, type WorkItemPriority } from '../core/work-item.ts'
import { rankOf } from '../core/priority-queue.ts'
import { afterCardTriaged } from '../cards/lifecycle.ts'
import { withTags } from '../cards/tags.ts'
export default async function triage(input: { cardId: string; priority?: WorkItemPriority; needsKent?: boolean; addTags?: string[]; removeTags?: string[]; reason?: string; dryRun?: boolean }) {
const id = String(input?.cardId ?? '')
const ledger = await loadJson<Ledger>(storageKeys.LEDGER_KEY, emptyLedger())
const item = ledger.items[id]
if (!item) throw new Error(`cards/triage: no card ${id}`)
if (input.priority !== undefined && input.priority !== null && ![0, 1, 2, 3].includes(input.priority)) throw new Error('cards/triage: priority must be 0-3 or null')
const tagOk = (t: unknown) => typeof t === 'string' && t.length > 0 && t.length <= 120 && !/\s/.test(t)
const add = (input.addTags ?? []).filter(tagOk)
const remove = new Set((input.removeTags ?? []).filter(tagOk).map((t) => t.toLowerCase()))
const priority = input.priority === undefined ? item.priority : input.priority
const needsKent = typeof input.needsKent === 'boolean' ? input.needsKent : item.needsKent
const labels = withTags(item.labels.filter((l) => !remove.has(l.toLowerCase())), add)
const at = nowIso()
const reason = String(input.reason ?? 'triaged').slice(0, 300)
const next: WorkItem = {
...item,
priority,
needsKent,
rank: rankOf(priority, needsKent),
labels,
updatedAt: at,
events: [...item.events, { at, from: item.state, to: item.state, reason: `triage: ${reason}`, actor: 'agent', meta: { ...(priority !== item.priority ? { priority } : {}), ...(needsKent !== item.needsKent ? { needsKent } : {}), ...(add.length ? { addTags: add } : {}), ...(remove.size ? { removeTags: [...remove] } : {}) } }],
}
if (input.dryRun) return { ok: true, dryRun: true, card: next }
ledger.items[id] = next
await saveJson(storageKeys.LEDGER_KEY, ledger)
const [hook] = await afterCardTriaged([{ before: { state: item.state, priority: item.priority, needsKent: item.needsKent, labels: item.labels }, after: next, reason }])
return { ok: true, dryRun: false, card: next, ...(hook ? { push: hook.push, route: hook.route } : {}) }
}