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

confirm-category.ts

53 lines · 2.0 KB · TypeScript
/**
 * Confirm or correct a work item's category. An agent calls this when
 * triaging to confirm Clef's guess or set a different category.
 *
 * - Confirming copies the guess into categoryConfirmed.
 * - Correcting sets categoryConfirmed to the agent's value.
 * - The original categoryGuess stays visible either way.
 *
 * @example
 * import confirmCategory from 'kody:@kody/kody-factory/confirm-category'
 * await confirmCategory({ workItemId: 'wi_abc', category: 'friction', agent: 'patch' })
 */
import { updateLedgerItem, cap } from '../lib/update-helpers.ts'
import { nowIso } from '../core/work-item.ts'

export default async function confirmCategory(input: {
	workItemId: string
	/** The confirmed category. Pass the guess value to confirm it, or a different value to correct it. */
	category: string
	/** Agent id that confirmed/corrected. */
	agent: string
	dryRun?: boolean
}) {
	if (!input?.workItemId) throw new Error('confirm-category: workItemId is required')
	if (!input.category?.trim()) throw new Error('confirm-category: category is required')
	if (!input.agent?.trim()) throw new Error('confirm-category: agent is required')

	const category = cap(input.category.trim(), 50)
	const agent = cap(input.agent.trim(), 100)
	const at = nowIso()

	if (input.dryRun) {
		return { confirmed: true, category, agent, persisted: false }
	}

	const item = await updateLedgerItem(input.workItemId, (cur) => ({
		...cur,
		categoryConfirmed: category,
		categoryConfirmedBy: agent,
		categoryConfirmedAt: at,
		updatedAt: at,
		events: [...cur.events, {
			at,
			from: cur.state,
			to: cur.state,
			reason: `category confirmed as ${category}${cur.categoryGuess && cur.categoryGuess !== category ? ` (was guessed ${cur.categoryGuess})` : ''}`,
			actor: 'agent' as const,
			meta: { categoryConfirmed: category, categoryGuess: cur.categoryGuess, agent },
		}],
	}))

	return { confirmed: true, category, agent, itemId: item.id, previousGuess: item.categoryGuess ?? null, persisted: true }
}