/** * Factory cards as sweep entries for the nightly card run. Nightly cards * (friction, and imported issues tagged `sweep:nightly`) for one repo become * `FrictionIssue`-shaped entries so grouping and prompts are shared with the * issue sweep. An entry's number is the card's GitHub issue number (`gh:#n`) * when it has one, else a stable number derived from the card id. */ import { loadJson, storageKeys } from '../lib/storage.ts' import { loadSettings } from '../lib/settings.ts' import { appBaseUrl, cardLink } from '../lib/links.ts' import { emptyLedger, type Ledger, type WorkItem } from '../core/work-item.ts' import { inPilotScope } from '../core/scope.ts' import { hasTag, isNightlyCard } from '../cards/tags.ts' import type { PromptCardRef } from './devin-prompt.ts' import type { FrictionIssue } from './shared.ts' const OPEN = new Set(['intake', 'triaged', 'planned']) /** Stable entry number for a card without a GitHub issue (above real issue numbers). */ export function cardEntryNumber(cardId: string): number { let h = 0x811c9dc5 for (let i = 0; i < cardId.length; i++) { h ^= cardId.charCodeAt(i) h = Math.imul(h, 0x01000193) >>> 0 } return 100_000_000 + (h % 800_000_000) } export function ghNumberOf(item: Pick): number | null { const tag = item.labels.find((l) => /^gh:#\d+$/.test(l)) if (tag) return Number(tag.slice(4)) if (item.source === 'github-issues' && /^\d{1,9}$/.test(String(item.externalId ?? ''))) return Number(item.externalId) return null } export type CardEntries = { issues: FrictionIssue[] refs: Map /** Entry number to card id. */ cardIds: Map } export function nightlyCardsFor(items: WorkItem[], repoSlug: string): WorkItem[] { const slug = repoSlug.toLowerCase() return items.filter((i) => isNightlyCard(i) && OPEN.has(i.state) && !i.needsKent && !hasTag(i.labels, 'needs-kent') && (i.repoRef ?? '').toLowerCase() === slug) } export function toEntries(cards: WorkItem[], base: string): CardEntries { const issues: FrictionIssue[] = [] const refs = new Map() const cardIds = new Map() for (const card of cards) { const gh = ghNumberOf(card) const number = gh ?? cardEntryNumber(card.id) if (cardIds.has(number)) continue const cardUrl = cardLink(base, card.id) const ghUrl = gh && card.repoRef ? `https://github.com/${card.repoRef}/issues/${gh}` : null const seen = card.events.filter((e) => e.reason === 'friction seen again') issues.push({ number, title: card.title, body: card.body, htmlUrl: ghUrl ?? cardUrl, userLogin: 'factory', createdAt: card.createdAt, updatedAt: card.updatedAt, labels: [...card.labels, ...(typeof card.priority === 'number' ? [`P${card.priority}`] : [])], comments: seen .filter((e) => typeof e.meta?.note === 'string') .map((e) => ({ id: 0, userLogin: 'friction', createdAt: e.at, body: String(e.meta!.note) })), }) refs.set(number, { cardId: card.id, cardUrl, ghIssue: gh, ghUrl, seenAgain: seen.length }) cardIds.set(number, card.id) } return { issues, refs, cardIds } } /** Open nightly cards for a repo, in the pilot scope. */ export async function listCardEntries(repoSlug: string): Promise { const [ledger, settings, base] = await Promise.all([loadJson(storageKeys.LEDGER_KEY, emptyLedger()), loadSettings(), appBaseUrl()]) const cards = nightlyCardsFor(Object.values(ledger.items), repoSlug).filter((i) => inPilotScope(settings.pilotScope, i.projectId, i.repoRef)) return toEntries(cards, base) } /** Open nightly package cards (`pkg:*`, no repo): one Patch wake lists them. */ export function nightlyPackageCards(items: WorkItem[]): WorkItem[] { return items.filter((i) => isNightlyCard(i) && OPEN.has(i.state) && !i.needsKent && !i.repoRef && hasTag(i.labels, 'pkg:*')) }