← 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
/**
* Global priority ranking across projects.
* Absorbed ranking rules from @kentcdodds/priority-queue (thin port):
* security-first (when marked), then tier (P0 < P1 < P2 < P3 < unprioritized),
* needs-kent before plain within a priority, then oldest createdAt, then id.
*/
import type { WorkItem, WorkItemPriority } from './work-item.ts'
const UNPRIORITIZED = 9
/** Lower is more urgent. P0+nk=0, P0=1, P1+nk=2, ... unprioritized=19. */
export function rankOf(priority: WorkItemPriority, needsKent: boolean): number {
return (priority ?? UNPRIORITIZED) * 2 + (needsKent ? 0 : 1)
}
export function parsePriorityLabel(labels: string[]): {
priority: WorkItemPriority
needsKent: boolean
} {
const PRIORITY_RE = /^p([0-3])\b/i
const NEEDS_KENT_RE = /^needs[\s_-]?kent$/i
let priority: WorkItemPriority = null
for (const l of labels) {
const m = PRIORITY_RE.exec(l.trim())
if (m) {
const p = Number(m[1]) as 0 | 1 | 2 | 3
if (priority === null || p < priority) priority = p
}
}
const needsKent = labels.some((l) => NEEDS_KENT_RE.test(l.trim()))
return { priority, needsKent }
}
export type RankedItem = WorkItem & { why: string }
function why(item: WorkItem): string {
const parts = [
item.priority === null
? 'no P0–P3 (ranked after prioritized work)'
: `P${item.priority}`,
item.needsKent ? 'needs Kent' : 'not waiting on Kent',
`project ${item.projectId}`,
`created ${item.createdAt.slice(0, 10)}`,
]
return parts.join('; ')
}
export function compareWorkItems(a: WorkItem, b: WorkItem): number {
if (a.rank !== b.rank) return a.rank - b.rank
const ta = Date.parse(a.createdAt) || 0
const tb = Date.parse(b.createdAt) || 0
if (ta !== tb) return ta - tb
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0
}
/** Rank every open (non-terminal) item across all projects. */
export function rankGlobalQueue(
items: WorkItem[],
opts?: { projectId?: string; includeTerminal?: boolean },
): RankedItem[] {
const terminal = new Set(['shipped', 'dropped'])
let list = items.slice()
if (!opts?.includeTerminal) {
list = list.filter((i) => !terminal.has(i.state))
}
if (opts?.projectId) {
list = list.filter((i) => i.projectId === opts.projectId)
}
const ranked = list.map((item) => {
const nextRank = rankOf(item.priority, item.needsKent)
const updated = nextRank === item.rank ? item : { ...item, rank: nextRank }
return { ...updated, why: why(updated) }
})
ranked.sort(compareWorkItems)
return ranked
}
export function nextItem(
items: WorkItem[],
opts?: { projectId?: string },
): RankedItem | null {
const ranked = rankGlobalQueue(items, opts)
return ranked[0] ?? null
}