/** * Cost tracking per project. Records what is actually available: * run counts, durations, and session counts from the ledger. * Devin ACU data is not available from the API (confirmed by project-detail.ts). * Honest empty when data is unknown. */ import type { WorkItem } from './work-item.ts' export type ProjectCostSummary = { projectId: string /** Total agent runs started for this project. */ runsStarted: number /** Runs that completed successfully. */ runsCompleted: number /** Runs that failed or were cancelled. */ runsFailed: number /** Total time agents spent running (ms), computed from event timestamps. Zero when no timing data. */ totalRunDurationMs: number /** Average run duration (ms). Null when no completed runs with timing data. */ avgRunDurationMs: number | null /** Items that shipped. */ itemsShipped: number /** Items dropped. */ itemsDropped: number /** Whether Devin ACU data is available. Currently always false. */ acuAvailable: false /** Human-readable note about cost data availability. */ costNote: string /** Period the data covers. */ period: { from: string | null; to: string } } export function computeProjectCosts(items: WorkItem[], projectId: string): ProjectCostSummary { const projectItems = items.filter((i) => i.projectId === projectId) const now = new Date().toISOString() let runsStarted = 0 let runsCompleted = 0 let runsFailed = 0 let totalRunDurationMs = 0 let runsWithDuration = 0 let itemsShipped = 0 let itemsDropped = 0 let earliestEvent: string | null = null for (const item of projectItems) { if (item.state === 'shipped') itemsShipped++ if (item.state === 'dropped') itemsDropped++ let startedAt: string | null = null let endedAt: string | null = null for (const event of item.events) { if (!earliestEvent || event.at < earliestEvent) earliestEvent = event.at if (event.to === 'running' && event.from !== 'running') { runsStarted++ startedAt = event.at } if (event.from === 'running' && event.to === 'verifying') { runsCompleted++ endedAt = event.at } if (event.from === 'running' && event.to === 'needs-kent') { const reason = event.reason ?? '' if (/failed|cancelled|timed.out/.test(reason)) { runsFailed++ } endedAt = event.at } } if (startedAt && endedAt) { const duration = Date.parse(endedAt) - Date.parse(startedAt) if (duration > 0) { totalRunDurationMs += duration runsWithDuration++ } } } return { projectId, runsStarted, runsCompleted, runsFailed, totalRunDurationMs, avgRunDurationMs: runsWithDuration > 0 ? Math.round(totalRunDurationMs / runsWithDuration) : null, itemsShipped, itemsDropped, acuAvailable: false, costNote: 'Devin ACU data is not available from the API. Showing run counts and durations from the factory ledger.', period: { from: earliestEvent, to: now }, } }