mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 02:39:13 +00:00
Add experimental yield command to track productive vs wasted spend
New command: codeburn yield --period <period> Correlates AI sessions with git commits to categorize spend: - Productive: sessions with nearby commits that made it to main - Reverted: sessions with commits that were reverted - Abandoned: sessions with no commits or not in main Uses timestamp proximity heuristic (session time + 1 hour window). Works across branches, squash merges, and rebases by checking if commits are in main branch ancestry. Closes #152
This commit is contained in:
parent
155b4a0708
commit
1c5ca45bd8
2 changed files with 200 additions and 0 deletions
13
src/cli.ts
13
src/cli.ts
|
|
@ -889,4 +889,17 @@ program
|
|||
await renderCompare(range, opts.provider)
|
||||
})
|
||||
|
||||
program
|
||||
.command('yield')
|
||||
.description('Track which AI spend shipped to main vs reverted/abandoned (experimental)')
|
||||
.option('-p, --period <period>', 'Analysis period: today, week, 30days, month, all', 'week')
|
||||
.action(async (opts) => {
|
||||
const { computeYield, formatYieldSummary } = await import('./yield.js')
|
||||
await loadPricing()
|
||||
const { range, label } = getDateRange(opts.period)
|
||||
console.log(`\n Analyzing yield for ${label}...\n`)
|
||||
const summary = await computeYield(range, process.cwd())
|
||||
console.log(formatYieldSummary(summary))
|
||||
})
|
||||
|
||||
program.parse()
|
||||
|
|
|
|||
187
src/yield.ts
Normal file
187
src/yield.ts
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
import { execSync } from 'child_process'
|
||||
import { parseAllSessions } from './parser.js'
|
||||
import type { DateRange, SessionSummary } from './types.js'
|
||||
|
||||
export type YieldCategory = 'productive' | 'reverted' | 'abandoned'
|
||||
|
||||
export type SessionYield = {
|
||||
sessionId: string
|
||||
project: string
|
||||
cost: number
|
||||
category: YieldCategory
|
||||
commitCount: number
|
||||
}
|
||||
|
||||
export type YieldSummary = {
|
||||
productive: { cost: number; sessions: number }
|
||||
reverted: { cost: number; sessions: number }
|
||||
abandoned: { cost: number; sessions: number }
|
||||
total: { cost: number; sessions: number }
|
||||
details: SessionYield[]
|
||||
}
|
||||
|
||||
function runGit(cmd: string, cwd: string): string | null {
|
||||
try {
|
||||
return execSync(cmd, { cwd, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function isGitRepo(dir: string): boolean {
|
||||
return runGit('git rev-parse --is-inside-work-tree', dir) === 'true'
|
||||
}
|
||||
|
||||
function getMainBranch(cwd: string): string {
|
||||
// Try to get default branch from remote
|
||||
const result = runGit('git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null', cwd)
|
||||
if (result) {
|
||||
return result.replace('refs/remotes/origin/', '')
|
||||
}
|
||||
|
||||
// Fallback: check common names
|
||||
const branches = runGit('git branch -a', cwd) ?? ''
|
||||
if (branches.includes('main')) return 'main'
|
||||
if (branches.includes('master')) return 'master'
|
||||
return 'main'
|
||||
}
|
||||
|
||||
type CommitInfo = {
|
||||
sha: string
|
||||
timestamp: Date
|
||||
isRevert: boolean
|
||||
inMain: boolean
|
||||
}
|
||||
|
||||
function getCommitsInRange(cwd: string, since: Date, until: Date, mainBranch: string): CommitInfo[] {
|
||||
const sinceStr = since.toISOString()
|
||||
const untilStr = until.toISOString()
|
||||
|
||||
// Get all commits in range with their info
|
||||
const log = runGit(
|
||||
`git log --all --since="${sinceStr}" --until="${untilStr}" --format="%H|%aI|%s"`,
|
||||
cwd
|
||||
)
|
||||
|
||||
if (!log) return []
|
||||
|
||||
const mainCommits = new Set(
|
||||
(runGit(`git log ${mainBranch} --format="%H"`, cwd) ?? '').split('\n').filter(Boolean)
|
||||
)
|
||||
|
||||
return log.split('\n').filter(Boolean).map(line => {
|
||||
const [sha, timestamp, subject] = line.split('|')
|
||||
return {
|
||||
sha,
|
||||
timestamp: new Date(timestamp),
|
||||
isRevert: subject.toLowerCase().includes('revert'),
|
||||
inMain: mainCommits.has(sha),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function categorizeSession(
|
||||
session: SessionSummary,
|
||||
commits: CommitInfo[]
|
||||
): { category: YieldCategory; commitCount: number } {
|
||||
if (!session.firstTimestamp) {
|
||||
return { category: 'abandoned', commitCount: 0 }
|
||||
}
|
||||
|
||||
// Find commits within a window around the session (session time + 1 hour buffer)
|
||||
const sessionStart = new Date(session.firstTimestamp)
|
||||
const lastTs = session.lastTimestamp ?? session.firstTimestamp
|
||||
const sessionEnd = new Date(new Date(lastTs).getTime() + 60 * 60 * 1000) // +1 hour
|
||||
|
||||
const relevantCommits = commits.filter(c =>
|
||||
c.timestamp >= sessionStart && c.timestamp <= sessionEnd
|
||||
)
|
||||
|
||||
if (relevantCommits.length === 0) {
|
||||
// No commits near this session - check if session had edits
|
||||
if (session.toolBreakdown['Edit'] || session.toolBreakdown['Write']) {
|
||||
return { category: 'abandoned', commitCount: 0 }
|
||||
}
|
||||
// No edits, not applicable
|
||||
return { category: 'abandoned', commitCount: 0 }
|
||||
}
|
||||
|
||||
const inMainCount = relevantCommits.filter(c => c.inMain).length
|
||||
const revertedCount = relevantCommits.filter(c => c.isRevert && c.inMain).length
|
||||
|
||||
if (revertedCount > 0 && revertedCount >= inMainCount / 2) {
|
||||
return { category: 'reverted', commitCount: relevantCommits.length }
|
||||
}
|
||||
|
||||
if (inMainCount > 0) {
|
||||
return { category: 'productive', commitCount: inMainCount }
|
||||
}
|
||||
|
||||
return { category: 'abandoned', commitCount: relevantCommits.length }
|
||||
}
|
||||
|
||||
export async function computeYield(range: DateRange, cwd: string): Promise<YieldSummary> {
|
||||
const projects = await parseAllSessions(range, 'all')
|
||||
|
||||
const summary: YieldSummary = {
|
||||
productive: { cost: 0, sessions: 0 },
|
||||
reverted: { cost: 0, sessions: 0 },
|
||||
abandoned: { cost: 0, sessions: 0 },
|
||||
total: { cost: 0, sessions: 0 },
|
||||
details: [],
|
||||
}
|
||||
|
||||
// Get all commits in the date range for correlation
|
||||
const commits = isGitRepo(cwd)
|
||||
? getCommitsInRange(cwd, range.start, range.end, getMainBranch(cwd))
|
||||
: []
|
||||
|
||||
for (const project of projects) {
|
||||
// Try project-specific git repo first, fall back to cwd
|
||||
const projectCwd = project.projectPath && isGitRepo(project.projectPath)
|
||||
? project.projectPath
|
||||
: cwd
|
||||
|
||||
const projectCommits = projectCwd !== cwd && isGitRepo(projectCwd)
|
||||
? getCommitsInRange(projectCwd, range.start, range.end, getMainBranch(projectCwd))
|
||||
: commits
|
||||
|
||||
for (const session of project.sessions) {
|
||||
const { category, commitCount } = categorizeSession(session, projectCommits)
|
||||
|
||||
summary[category].cost += session.totalCostUSD
|
||||
summary[category].sessions += 1
|
||||
summary.total.cost += session.totalCostUSD
|
||||
summary.total.sessions += 1
|
||||
|
||||
summary.details.push({
|
||||
sessionId: session.sessionId,
|
||||
project: project.project,
|
||||
cost: session.totalCostUSD,
|
||||
category,
|
||||
commitCount,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return summary
|
||||
}
|
||||
|
||||
export function formatYieldSummary(summary: YieldSummary): string {
|
||||
const { productive, reverted, abandoned, total } = summary
|
||||
|
||||
const pct = (n: number) => total.cost > 0 ? Math.round((n / total.cost) * 100) : 0
|
||||
const fmt = (n: number) => `$${n.toFixed(2)}`
|
||||
|
||||
const lines = [
|
||||
'',
|
||||
`Productive: ${fmt(productive.cost).padStart(8)} (${pct(productive.cost)}%) - ${productive.sessions} sessions shipped to main`,
|
||||
`Reverted: ${fmt(reverted.cost).padStart(8)} (${pct(reverted.cost)}%) - ${reverted.sessions} sessions were reverted`,
|
||||
`Abandoned: ${fmt(abandoned.cost).padStart(8)} (${pct(abandoned.cost)}%) - ${abandoned.sessions} sessions never committed`,
|
||||
'',
|
||||
`Total: ${fmt(total.cost).padStart(8)} - ${total.sessions} sessions`,
|
||||
'',
|
||||
]
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue