codeburn/src/yield.ts
iamtoruk b95cb95ce4 fix(yield): group commit attribution by canonical repo, not launch path
Sessions launched from monorepo subdirectories or separate worktrees
of one repository formed independent attribution groups, each awarding
the same commit (double-counted productive sessions; the duplicate
owner was not even flagged ambiguous). Groups now key on the realpath
of git-common-dir, so all subdirs and worktrees of a repo collapse
into one group while distinct repos stay separate; ambiguity semantics
unchanged, now correctly spanning the whole repo. rev-parse results
cached per directory.

Fixes #713. Regression tests fail on the unfixed code.
2026-07-16 18:58:49 -07:00

445 lines
15 KiB
TypeScript

import { execFileSync } from 'child_process'
import { realpathSync } from 'fs'
import { resolve } from 'path'
import { parseAllSessions } from './parser.js'
import type { DateRange, SessionSummary } from './types.js'
export type YieldCategory = 'productive' | 'reverted' | 'abandoned' | 'ambiguous'
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 }
ambiguous: { cost: number; sessions: number }
total: { cost: number; sessions: number }
details: SessionYield[]
}
export type YieldJsonReport = {
period: {
label: string
start: string
end: string
}
summary: {
productive: YieldBucketJson
reverted: YieldBucketJson
abandoned: YieldBucketJson
ambiguous: YieldBucketJson
total: { costUSD: number; sessions: number }
productiveToRevertedCostRatio: number | null
}
methodology: 'timestamp-window'
details: SessionYieldJson[]
}
type YieldBucketJson = {
costUSD: number
sessions: number
costPercent: number
sessionPercent: number
}
type SessionYieldJson = Omit<SessionYield, 'cost'> & {
costUSD: number
}
const SAFE_REF_PATTERN = /^[A-Za-z0-9._/\-]+$/
function runGit(args: string[], cwd: string): string | null {
try {
return execFileSync('git', args, { cwd, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim()
} catch {
return null
}
}
type RepoIdentity = {
/** Canonical group key: the absolute git-common-dir (shared object store). */
readonly key: string
/** A member directory to run `git log` from; --all spans the whole store. */
readonly gitDir: string
}
function canonicalPath(path: string): string {
try {
return realpathSync(path)
} catch {
return path
}
}
/**
* Resolve a directory to its canonical repository identity, or null when it is
* not inside a git work tree.
*
* The key is the absolute `git-common-dir` (the shared object store), NOT the
* raw path: `git rev-parse --is-inside-work-tree` is true in every subdirectory
* and `git log --all` returns the same repo-wide list from any of them, so
* keying groups on the raw path would let two monorepo subdirectories — or two
* worktrees of one repo — each award the same commit. All subdirectories AND
* all worktrees of one repo share this common-dir, so they collapse to a single
* group; distinct repositories keep distinct keys.
*
* Cached per directory: resolution runs once per unique path (many sessions
* share a path), never once per session.
*/
function resolveRepoIdentity(
dir: string,
cache: Map<string, RepoIdentity | null>,
): RepoIdentity | null {
const cached = cache.get(dir)
if (cached !== undefined) return cached
let identity: RepoIdentity | null = null
// One fork answers both questions; line 1 is --is-inside-work-tree, line 2 the
// --git-common-dir (relative to `dir` for a subdir, absolute for a worktree).
const out = runGit(['rev-parse', '--is-inside-work-tree', '--git-common-dir'], dir)
if (out) {
const [insideWorkTree, commonDir] = out.split('\n')
if (insideWorkTree === 'true' && commonDir) {
// realpath canonicalizes symlinks: git reports a linked worktree's
// common-dir as a realpath but the main worktree's as ".git" relative to
// its (possibly symlinked) path, so both must be canonicalized to collapse
// to one key.
identity = { key: canonicalPath(resolve(dir, commonDir)), gitDir: dir }
}
}
cache.set(dir, identity)
return identity
}
function getMainBranch(cwd: string): string {
const result = runGit(['symbolic-ref', 'refs/remotes/origin/HEAD'], cwd)
if (result) {
const branch = result.replace('refs/remotes/origin/', '')
if (SAFE_REF_PATTERN.test(branch)) return branch
}
const branches = runGit(['branch', '-a'], cwd) ?? ''
if (branches.includes('main')) return 'main'
if (branches.includes('master')) return 'master'
return 'main'
}
type CommitInfo = {
sha: string
timestamp: Date
inMain: boolean
/** Set when a LATER commit's body says "This reverts commit <sha>" — i.e. the work in this commit was reverted out of main. */
wasReverted: boolean
}
type SessionWindow = {
readonly start: Date
readonly end: Date
readonly sessionId: string
}
type SessionAttribution = {
readonly window: SessionWindow | null
readonly commits: CommitInfo[]
lostCandidacy: boolean
}
/**
* Find SHAs that were the target of a `git revert` ANYWHERE in the repo's
* history (not just the time window). The standard `git revert` body
* format is "This reverts commit <SHA>." which we grep out.
*
* The previous implementation flagged a commit as `isRevert` based on the
* substring "revert" appearing in its OWN subject. Two bugs there:
* 1. Subjects like "Add revert button" matched.
* 2. The session that PERFORMED the revert was tagged "reverted", not the
* session whose work was being reverted — so the original session always
* looked productive even after its work was thrown away.
*/
function getRevertedShas(cwd: string): Set<string> {
const bodies = runGit(
['log', '--all', '--grep=^This reverts commit', '--format=%B%x1e'],
cwd,
) ?? ''
const set = new Set<string>()
const re = /This reverts commit ([0-9a-f]{7,40})/g
let m: RegExpExecArray | null
while ((m = re.exec(bodies)) !== null) {
set.add(m[1].toLowerCase())
}
return set
}
function getCommitsInRange(cwd: string, since: Date, until: Date, mainBranch: string): CommitInfo[] {
const sinceStr = since.toISOString()
const untilStr = until.toISOString()
const log = runGit(
['log', '--all', `--since=${sinceStr}`, `--until=${untilStr}`, '--format=%H|%aI|%s'],
cwd
)
if (!log) return []
const mainCommits = new Set(
(runGit(['log', mainBranch, '--format=%H'], cwd) ?? '').split('\n').filter(Boolean)
)
const revertedShas = getRevertedShas(cwd)
return log.split('\n').filter(Boolean).map(line => {
const [sha] = line.split('|')
const timestamp = line.split('|')[1] ?? ''
return {
sha,
timestamp: new Date(timestamp),
inMain: mainCommits.has(sha),
// wasReverted: matches when ANY later commit's body says
// "This reverts commit <sha>". Compare against the full SHA AND its
// 7-char short prefix to be safe; git revert sometimes records the
// short form.
wasReverted: revertedShas.has(sha.toLowerCase()) ||
revertedShas.has(sha.toLowerCase().slice(0, 7)),
}
})
}
function sessionWindow(session: SessionSummary): SessionWindow | null {
if (!session.firstTimestamp) return null
const start = new Date(session.firstTimestamp)
const lastTs = session.lastTimestamp ?? session.firstTimestamp
const end = new Date(new Date(lastTs).getTime() + 60 * 60 * 1000)
return { start, end, sessionId: session.sessionId }
}
function attributeCommits(
sessions: SessionSummary[],
commits: CommitInfo[],
): SessionAttribution[] {
const attributions: SessionAttribution[] = sessions.map(session => ({
window: sessionWindow(session),
commits: [],
lostCandidacy: false,
}))
for (const commit of commits) {
const candidates = attributions.filter((attribution): attribution is SessionAttribution & { window: SessionWindow } =>
attribution.window !== null &&
commit.timestamp >= attribution.window.start &&
commit.timestamp <= attribution.window.end,
)
const owner = candidates.reduce<SessionAttribution & { window: SessionWindow } | null>((current, candidate) => {
if (current === null) return candidate
const currentSpan = current.window.end.getTime() - current.window.start.getTime()
const candidateSpan = candidate.window.end.getTime() - candidate.window.start.getTime()
if (candidateSpan !== currentSpan) return candidateSpan < currentSpan ? candidate : current
if (candidate.window.start.getTime() !== current.window.start.getTime()) {
return candidate.window.start < current.window.start ? candidate : current
}
// sessionId, not array position: session order is cache-state dependent.
return candidate.window.sessionId < current.window.sessionId ? candidate : current
}, null)
for (const candidate of candidates) {
if (candidate !== owner) candidate.lostCandidacy = true
}
owner?.commits.push(commit)
}
return attributions
}
function categorizeSession(
session: SessionSummary,
commits: CommitInfo[],
lostCandidacy: boolean,
): { category: YieldCategory; commitCount: number } {
if (!session.firstTimestamp) {
return { category: 'abandoned', commitCount: 0 }
}
if (commits.length === 0) {
// Ambiguous only when this session's window actually contained a commit
// that a tighter window won — mere overlap with a credited session is not
// enough to withhold the abandoned classification.
return {
category: lostCandidacy ? 'ambiguous' : 'abandoned',
commitCount: 0,
}
}
const inMainCount = commits.filter(c => c.inMain).length
// A session is "reverted" when at least half of its in-main commits were
// later reverted out (revert detected via "This reverts commit <sha>"
// anywhere later in history, not in the same time window).
const revertedCount = commits.filter(c => c.inMain && c.wasReverted).length
if (revertedCount > 0 && revertedCount >= inMainCount / 2) {
return { category: 'reverted', commitCount: commits.length }
}
if (inMainCount > 0) {
return { category: 'productive', commitCount: inMainCount }
}
return { category: 'abandoned', commitCount: commits.length }
}
export async function computeYield(range: DateRange, cwd: string, provider: string = 'all'): Promise<YieldSummary> {
const projects = await parseAllSessions(range, provider)
const summary: YieldSummary = {
productive: { cost: 0, sessions: 0 },
reverted: { cost: 0, sessions: 0 },
abandoned: { cost: 0, sessions: 0 },
ambiguous: { cost: 0, sessions: 0 },
total: { cost: 0, sessions: 0 },
details: [],
}
const repoIdentityCache = new Map<string, RepoIdentity | null>()
// Get all commits in the date range for correlation
const cwdIdentity = resolveRepoIdentity(cwd, repoIdentityCache)
const cwdCommits = cwdIdentity
? getCommitsInRange(cwd, range.start, range.end, getMainBranch(cwd))
: []
// Group sessions by canonical repository identity before attributing so that
// each commit is awarded at most once across the whole repo. Two monorepo
// subdirectory sessions, or two worktrees of one repo, resolve to the same
// git-common-dir and share ONE group; keying on the raw path would double
// count. A project whose path is missing or not a git repo falls back to the
// cwd repo (or, when cwd is not a repo either, an empty commit list) exactly
// as before.
type RepoGroup = { commits: CommitInfo[]; sessions: SessionSummary[]; projectNames: string[] }
const repoGroups = new Map<string, RepoGroup>()
for (const project of projects) {
const projectIdentity = project.projectPath
? resolveRepoIdentity(project.projectPath, repoIdentityCache)
: null
const identity = projectIdentity ?? cwdIdentity
const groupKey = identity ? identity.key : cwd
let group = repoGroups.get(groupKey)
if (!group) {
group = {
commits: !identity
? []
: cwdIdentity && identity.key === cwdIdentity.key
? cwdCommits
: getCommitsInRange(identity.gitDir, range.start, range.end, getMainBranch(identity.gitDir)),
sessions: [],
projectNames: [],
}
repoGroups.set(groupKey, group)
}
for (const session of project.sessions) {
group.sessions.push(session)
group.projectNames.push(project.project)
}
}
for (const group of repoGroups.values()) {
const attributions = attributeCommits(group.sessions, group.commits)
for (const [index, session] of group.sessions.entries()) {
const attribution = attributions[index]
const { category, commitCount } = categorizeSession(
session,
attribution?.commits ?? [],
attribution?.lostCandidacy ?? false,
)
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: group.projectNames[index] ?? session.project,
cost: session.totalCostUSD,
category,
commitCount,
})
}
}
return summary
}
export function formatYieldSummary(summary: YieldSummary): string {
const { productive, reverted, abandoned, ambiguous, 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`,
`Ambiguous: ${fmt(ambiguous.cost).padStart(8)} (${pct(ambiguous.cost)}%) - ${ambiguous.sessions} sessions lost commits to concurrent sessions`,
'',
'Attribution: timestamp-window based (heuristic)',
'',
`Total: ${fmt(total.cost).padStart(8)} - ${total.sessions} sessions`,
'',
]
return lines.join('\n')
}
export function buildYieldJsonReport(
summary: YieldSummary,
periodLabel: string,
range: DateRange,
): YieldJsonReport {
const bucket = (value: { cost: number; sessions: number }): YieldBucketJson => ({
costUSD: value.cost,
sessions: value.sessions,
costPercent: summary.total.cost > 0
? Math.round((value.cost / summary.total.cost) * 1000) / 10
: 0,
sessionPercent: summary.total.sessions > 0
? Math.round((value.sessions / summary.total.sessions) * 1000) / 10
: 0,
})
return {
period: {
label: periodLabel,
start: range.start.toISOString(),
end: range.end.toISOString(),
},
summary: {
productive: bucket(summary.productive),
reverted: bucket(summary.reverted),
abandoned: bucket(summary.abandoned),
ambiguous: bucket(summary.ambiguous),
total: {
costUSD: summary.total.cost,
sessions: summary.total.sessions,
},
productiveToRevertedCostRatio: summary.reverted.cost > 0
? Math.round((summary.productive.cost / summary.reverted.cost) * 100) / 100
: null,
},
methodology: 'timestamp-window',
details: summary.details.map(detail => ({
sessionId: detail.sessionId,
project: detail.project,
costUSD: detail.cost,
category: detail.category,
commitCount: detail.commitCount,
})),
}
}