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

work-item.ts

117 lines · 3.4 KB · TypeScript
/**
 * Work-item ledger model. Every item carries projectId.
 * States: intake → triaged → planned → running → verifying → shipped
 * Exits: needs-kent | dropped
 */

export const WORK_ITEM_STATES = [
	'intake',
	'triaged',
	'planned',
	'running',
	'verifying',
	'shipped',
	'needs-kent',
	'dropped',
] as const

export type WorkItemState = (typeof WORK_ITEM_STATES)[number]

export type WorkItemPriority = 0 | 1 | 2 | 3 | null

export type WorkItemEvent = {
	at: string
	from: WorkItemState | null
	to: WorkItemState
	reason: string
	actor: 'system' | 'agent' | 'human' | 'sweep'
	meta?: Record<string, unknown>
}

export type WorkItem = {
	id: string
	projectId: string
	title: string
	body: string
	state: WorkItemState
	/** P0–P3; null = unprioritized. */
	priority: WorkItemPriority
	needsKent: boolean
	/** Global rank; lower is more urgent. */
	rank: number
	source: string
	externalId: string | null
	fingerprint: string
	signalIds: string[]
	labels: string[]
	repoRef: string | null
	planCardId: string | null
	runId: string | null
	createdAt: string
	updatedAt: string
	events: WorkItemEvent[]
	shadow: boolean
	/** Related artifacts: source issue, overlapping/open PRs, agent sessions. */
	links?: WorkItemLink[]
	/** Pull request lifecycle for this item (one PR per item in the pilot). */
	pr?: WorkItemPr
	/** After merge: watch main CI, ask the implementer to fix red, then fall back. */
	postMerge?: import('./post-merge.ts').PostMergeWatch
	/** Clef/routing first-pass category guess (unconfirmed). */
	categoryGuess?: string | null
	/** Agent-confirmed category. Takes precedence over the guess and the derived category. */
	categoryConfirmed?: string | null
	/** Who confirmed the category (agent id). Null means unconfirmed. */
	categoryConfirmedBy?: string | null
	/** When the category was confirmed. */
	categoryConfirmedAt?: string | null
	/** Set when a factory report filed this card: the report id, its source, and the poster's stable key. */
	reportRef?: { id: string; source: string; key: string }
}

export type WorkItemLink = {
	kind: 'issue' | 'pr' | 'session' | 'review' | 'other'
	url: string
	label?: string
	/** Why it was linked, e.g. `overlap:open-pr`, `overlap:active-session`, `outcome`. */
	reason?: string
	at: string
}

export type PrReviewState = 'none' | 'pending' | 'approved' | 'changes_requested'

export type WorkItemPr = {
	url: string
	number: number | null
	reviewState: PrReviewState
	ready: boolean
	/** Set only by an explicit merge attestation (human or a named pipeline). */
	merged?: { at: string; by: 'human' | 'pipeline'; attestedBy: string; mergeCommit?: string }
	updatedAt: string
	/** Last read from GitHub by refresh-pr (observation, not attestation). */
	github?: { state: string; merged: boolean; mergedAt: string | null; headSha: string | null; checks: 'success' | 'failure' | 'pending' | 'none'; readAt: string }
}

/** Add a link once (dedupe by kind+url). Returns a new array. */
export function withLink(links: WorkItemLink[] | undefined, link: WorkItemLink): WorkItemLink[] {
	const list = links ? links.slice() : []
	if (!list.some((l) => l.kind === link.kind && l.url === link.url)) list.push(link)
	return list
}

export type Ledger = {
	version: 1
	items: Record<string, WorkItem>
}

export function emptyLedger(): Ledger {
	return { version: 1, items: {} }
}

export function newWorkItemId(): string {
	return `wi_${crypto.randomUUID().replace(/-/g, '').slice(0, 16)}`
}

export function nowIso(): string {
	return new Date().toISOString()
}