/** * Apply a nightly card run's per-card outcomes to the board. * * - fixed: note plus the PR link; the merge ships the card (`Fixes `). * - already-fixed, stale, invalid: the card is dropped with the reason. * - needs-kent: the card moves to needs-kent with the recommendation. * - waiting, not-reached, skipped: a note; a later run picks it up. */ import { loadJson, saveJson, storageKeys } from '../lib/storage.ts' import { emptyLedger, nowIso, type Ledger, type WorkItem } from '../core/work-item.ts' import { rankOf } from '../core/priority-queue.ts' import { afterCardTriaged, type TriageChange } from '../cards/lifecycle.ts' import { withTags } from '../cards/tags.ts' export type CardOutcome = 'fixed' | 'already-fixed' | 'invalid' | 'skipped' | 'not-reached' | 'stale' | 'waiting' | 'needs-kent' export async function applyCardOutcomes( cards: Array<{ cardId: string; outcome: CardOutcome; note?: string }>, ctx: { groupKey: string; prUrl: string | null; summary: string }, ): Promise> { const ledger = await loadJson(storageKeys.LEDGER_KEY, emptyLedger()) const out: Array<{ cardId: string; outcome: CardOutcome; state?: string; error?: string }> = [] const triaged: TriageChange[] = [] const at = nowIso() for (const c of cards) { const item = ledger.items[c.cardId] if (!item) { out.push({ ...c, error: 'no such card' }) continue } if (item.state === 'shipped' || item.state === 'dropped') { out.push({ cardId: c.cardId, outcome: c.outcome, state: item.state }) continue } const meta = { groupKey: ctx.groupKey, outcome: c.outcome, ...(ctx.prUrl ? { prUrl: ctx.prUrl } : {}), ...(c.note ? { note: c.note } : {}) } let next: WorkItem = { ...item, updatedAt: at } const reason = `nightly run: ${c.outcome}${c.note ? `: ${c.note.slice(0, 200)}` : ''}` if (c.outcome === 'already-fixed' || c.outcome === 'stale' || c.outcome === 'invalid') { next = { ...next, state: 'dropped', events: [...item.events, { at, from: item.state, to: 'dropped', reason, actor: 'agent', meta }] } } else if (c.outcome === 'needs-kent') { next = { ...next, state: 'needs-kent', needsKent: true, rank: rankOf(item.priority, true), labels: withTags(item.labels, ['needs-kent']), events: [...item.events, { at, from: item.state, to: 'needs-kent', reason, actor: 'agent', meta }] } } else { next = { ...next, events: [...item.events, { at, from: item.state, to: item.state, reason, actor: 'agent', meta }] } } if (ctx.prUrl && c.outcome === 'fixed' && !(next.links ?? []).some((l) => l.url === ctx.prUrl)) { next = { ...next, links: [...(next.links ?? []), { kind: 'pr', url: ctx.prUrl, label: 'Nightly run PR', reason: 'nightly-run', at }] } } ledger.items[c.cardId] = next triaged.push({ before: { state: item.state, priority: item.priority, needsKent: item.needsKent, labels: item.labels }, after: next, reason }) out.push({ cardId: c.cardId, outcome: c.outcome, state: next.state }) } await saveJson(storageKeys.LEDGER_KEY, ledger) await afterCardTriaged(triaged) return out }