burnoff 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/burnoff-pr-gate.yml +75 -0
- package/README.md +94 -20
- package/bin/burnoff.mjs +129 -30
- package/hooks/hook-io.mjs +27 -18
- package/hooks/post-tool-use.mjs +33 -26
- package/hooks/pre-tool-use.mjs +43 -41
- package/hooks/session-lifecycle.mjs +40 -0
- package/package.json +8 -5
- package/src/ci/ci-gate.mjs +84 -9
- package/src/ci/pr-cost.mjs +207 -22
- package/src/ci/team-digest.mjs +6 -4
- package/src/cli/audit.mjs +177 -23
- package/src/cli/doctor.mjs +230 -25
- package/src/cli/init.mjs +415 -75
- package/src/cli/startup-audit.mjs +2824 -0
- package/src/clients/antigravity.mjs +285 -0
- package/src/clients/claude-code.mjs +180 -0
- package/src/clients/detect.mjs +64 -0
- package/src/config/catalog.json +188 -45
- package/src/config/loader.mjs +96 -13
- package/src/config/paths.mjs +92 -2
- package/src/config/policy.mjs +103 -17
- package/src/config/pricing.mjs +1033 -0
- package/src/config/uncached-attribution.mjs +719 -0
- package/src/guards/loop-breaker.mjs +267 -24
- package/src/guards/post-tool-engine.mjs +316 -35
- package/src/guards/pre-tool-engine.mjs +389 -130
- package/src/guards/read-clamp.mjs +149 -75
- package/src/guards/session-hook.mjs +207 -0
- package/src/guards/shell-guard.mjs +610 -74
- package/src/ipc/daemon.mjs +50 -8
- package/src/ipc/fast-client.mjs +62 -5
- package/src/ipc/protocol.mjs +1 -1
- package/src/storage/antigravity-parser.mjs +258 -0
- package/src/storage/atomic-io.mjs +2 -2
- package/src/storage/file-lock.mjs +66 -13
- package/src/storage/fts5-archive.mjs +31 -16
- package/src/storage/jsonl-parser.mjs +209 -20
- package/src/storage/ledger.mjs +15 -10
- package/src/storage/sqlite-client.mjs +35 -11
- package/hooks/hooks.json +0 -12
package/src/ci/pr-cost.mjs
CHANGED
|
@@ -12,11 +12,17 @@ import fs from 'node:fs';
|
|
|
12
12
|
import os from 'node:os';
|
|
13
13
|
import path from 'node:path';
|
|
14
14
|
import { getHomeDir } from '../config/paths.mjs';
|
|
15
|
+
import { DEFAULT_UNMAPPED_CACHED_WEIGHT, resolveModelRateCard, valuateCall } from '../config/pricing.mjs';
|
|
16
|
+
import { attributeUncachedTokens } from '../config/uncached-attribution.mjs';
|
|
15
17
|
import { readLedgerEntries } from '../storage/ledger.mjs';
|
|
16
18
|
import {
|
|
17
19
|
discoverClaudeCodeSessions,
|
|
18
20
|
parseTranscriptIncremental
|
|
19
21
|
} from '../storage/jsonl-parser.mjs';
|
|
22
|
+
import {
|
|
23
|
+
discoverAntigravityTranscripts,
|
|
24
|
+
parseAntigravityTranscript
|
|
25
|
+
} from '../storage/antigravity-parser.mjs';
|
|
20
26
|
|
|
21
27
|
export const PR_ATTRIBUTION_FORMAT = 'burnoff-pr-attribution/v1';
|
|
22
28
|
|
|
@@ -199,15 +205,52 @@ export function collectGitBranchCommits(
|
|
|
199
205
|
|
|
200
206
|
function extractStepFields(step = {}, cwd = '') {
|
|
201
207
|
const usage = step.tokenUsage || {};
|
|
202
|
-
const inTok =
|
|
203
|
-
|
|
204
|
-
|
|
208
|
+
const inTok = Number(
|
|
209
|
+
step.uncachedTokens ??
|
|
210
|
+
step.uncachedInputTokens ??
|
|
211
|
+
step.freshInputTokens ??
|
|
212
|
+
step.inputTokens ??
|
|
213
|
+
step.input_tokens ??
|
|
214
|
+
usage.freshInputTokens ??
|
|
215
|
+
usage.inputTokens ??
|
|
216
|
+
0
|
|
217
|
+
);
|
|
218
|
+
const cacheWrite5mTok = Number(
|
|
219
|
+
step.cacheWrite5mTokens ??
|
|
220
|
+
step.write5mTokens ??
|
|
221
|
+
step.cache_write_5m_tokens ??
|
|
222
|
+
step.cacheCreationEphemeral5mInputTokens ??
|
|
223
|
+
usage.cacheWrite5mTokens ??
|
|
224
|
+
usage.cacheCreationEphemeral5mInputTokens ??
|
|
225
|
+
0
|
|
226
|
+
);
|
|
227
|
+
const cacheWrite1hTok = Number(
|
|
228
|
+
step.cacheWrite1hTokens ??
|
|
229
|
+
step.write1hTokens ??
|
|
230
|
+
step.cache_write_1h_tokens ??
|
|
231
|
+
step.cacheCreationEphemeral1hInputTokens ??
|
|
232
|
+
usage.cacheWrite1hTokens ??
|
|
233
|
+
usage.cacheCreationEphemeral1hInputTokens ??
|
|
234
|
+
0
|
|
235
|
+
);
|
|
236
|
+
const cacheCreationTok = Number(
|
|
237
|
+
step.cacheCreationTokens ?? step.cache_creation_input_tokens ?? usage.cacheCreationTokens ?? 0
|
|
238
|
+
);
|
|
239
|
+
const cacheWriteTok = Math.max(cacheCreationTok, cacheWrite5mTok + cacheWrite1hTok);
|
|
205
240
|
const cacheReadTok = Number(
|
|
206
241
|
step.cacheReadTokens ?? step.cache_read_input_tokens ?? usage.cacheReadTokens ?? 0
|
|
207
242
|
);
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
243
|
+
const rawOutTok = Number(step.outputTokens ?? step.output_tokens ?? usage.outputTokens ?? 0);
|
|
244
|
+
const thinkTok = Number(step.thinkingTokens ?? step.thinking_tokens ?? usage.thinkingTokens ?? 0);
|
|
245
|
+
const stepModel = step.model || step.modelId || usage.model || null;
|
|
246
|
+
const rateCard = stepModel ? resolveModelRateCard({ model: stepModel, label: step.label || '' }) : null;
|
|
247
|
+
const includesThinking =
|
|
248
|
+
step.outputIncludesThinking !== undefined
|
|
249
|
+
? Boolean(step.outputIncludesThinking)
|
|
250
|
+
: (rateCard ? rateCard.outputIncludesThinking !== false : true);
|
|
251
|
+
const outTok = includesThinking ? rawOutTok : rawOutTok + thinkTok;
|
|
252
|
+
const hasExplicitWaste =
|
|
253
|
+
step.wasteTokens !== undefined || step.wastedTokens !== undefined;
|
|
211
254
|
const wasteTok = Number(step.wasteTokens ?? step.wastedTokens ?? 0);
|
|
212
255
|
const rawTs = step.timestampMs ?? step.createdAt ?? step.created_at ?? step.timestamp ?? null;
|
|
213
256
|
const tsMs = typeof rawTs === 'number' ? rawTs : rawTs ? Date.parse(String(rawTs)) : NaN;
|
|
@@ -239,19 +282,57 @@ function extractStepFields(step = {}, cwd = '') {
|
|
|
239
282
|
(step.decision === 'deny' && RING0_DETECTORS.has(detector));
|
|
240
283
|
|
|
241
284
|
return {
|
|
285
|
+
model: step.model || step.modelId || usage.model || null,
|
|
286
|
+
label: step.label || null,
|
|
287
|
+
sessionId: step.sessionId || step.conv || 'default',
|
|
288
|
+
stepIndex: step.stepIndex ?? null,
|
|
242
289
|
inTok,
|
|
290
|
+
cacheWrite5mTok,
|
|
291
|
+
cacheWrite1hTok,
|
|
292
|
+
cacheCreationTok,
|
|
293
|
+
cacheWriteTok,
|
|
243
294
|
cacheReadTok,
|
|
244
295
|
outTok,
|
|
296
|
+
thinkingTok: thinkTok,
|
|
245
297
|
wasteTok,
|
|
246
|
-
|
|
298
|
+
hasExplicitWaste,
|
|
299
|
+
totalTok: inTok + cacheWriteTok + cacheReadTok + outTok,
|
|
247
300
|
tsMs: Number.isFinite(tsMs) ? tsMs : null,
|
|
301
|
+
hadCompactionBefore: Boolean(step.hadCompactionBefore),
|
|
302
|
+
isCompactSummary: Boolean(step.isCompactSummary),
|
|
303
|
+
isCompactionBoundary: Boolean(step.isCompactionBoundary),
|
|
304
|
+
isSidechain: Boolean(step.isSidechain),
|
|
305
|
+
entrypoint: step.entrypoint || null,
|
|
306
|
+
shapeHash: step.shapeHash || null,
|
|
307
|
+
cacheTtlTier: step.cacheTtlTier || (cacheWrite1hTok > 0 ? '1h' : '5m'),
|
|
248
308
|
filesTouched: Array.from(filesTouched),
|
|
249
309
|
isRing0
|
|
250
310
|
};
|
|
251
311
|
}
|
|
252
312
|
|
|
253
|
-
function computeStepCostUsd(inTok, cacheReadTok, outTok, pricePerMillionUsd = 3.0) {
|
|
254
|
-
return (
|
|
313
|
+
function computeStepCostUsd(inTok, cacheReadTok, outTok, pricePerMillionUsd = 3.0, cacheWriteTok = 0) {
|
|
314
|
+
return (
|
|
315
|
+
(inTok * 1.0 + cacheWriteTok * 1.25 + cacheReadTok * DEFAULT_UNMAPPED_CACHED_WEIGHT + outTok * 5.0) /
|
|
316
|
+
1_000_000
|
|
317
|
+
) * pricePerMillionUsd;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function computeStepUsdWithModel(st, pricePerMillionUsd, hasExplicitPriceOverride) {
|
|
321
|
+
if (!hasExplicitPriceOverride && st?.model) {
|
|
322
|
+
const val = valuateCall({
|
|
323
|
+
model: st.model,
|
|
324
|
+
label: st.label,
|
|
325
|
+
timestampMs: st.tsMs,
|
|
326
|
+
uncachedTokens: st.inTok,
|
|
327
|
+
cachedTokens: st.cacheReadTok,
|
|
328
|
+
cacheWrite5mTokens: st.cacheWrite5mTok,
|
|
329
|
+
cacheWrite1hTokens: st.cacheWrite1hTok,
|
|
330
|
+
cacheCreationTokens: st.cacheCreationTok,
|
|
331
|
+
outputTokens: st.outTok
|
|
332
|
+
});
|
|
333
|
+
if (val.totalEstimatedUsd !== null) return val.totalEstimatedUsd;
|
|
334
|
+
}
|
|
335
|
+
return computeStepCostUsd(st.inTok, st.cacheReadTok, st.outTok, pricePerMillionUsd, st.cacheWriteTok);
|
|
255
336
|
}
|
|
256
337
|
|
|
257
338
|
/**
|
|
@@ -259,9 +340,13 @@ function computeStepCostUsd(inTok, cacheReadTok, outTok, pricePerMillionUsd = 3.
|
|
|
259
340
|
*/
|
|
260
341
|
export function buildPrCostAttribution(options = {}) {
|
|
261
342
|
const cwd = path.resolve(options.cwd || process.cwd());
|
|
262
|
-
const
|
|
343
|
+
const explicitHomeDir = typeof options.homeDir === 'string' && options.homeDir.trim()
|
|
344
|
+
? options.homeDir.trim()
|
|
345
|
+
: null;
|
|
346
|
+
const homeDir = explicitHomeDir || getHomeDir();
|
|
263
347
|
const base = String(options.base || 'main');
|
|
264
348
|
const head = String(options.head || 'HEAD');
|
|
349
|
+
const hasExplicitPriceOverride = options.pricePerMillionUsd !== undefined && options.pricePerMillionUsd !== null;
|
|
265
350
|
const pricePerMillionUsd = Number(options.pricePerMillionUsd || 3.0);
|
|
266
351
|
|
|
267
352
|
let commits = Array.isArray(options.commits) ? options.commits : null;
|
|
@@ -280,21 +365,48 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
280
365
|
}
|
|
281
366
|
|
|
282
367
|
const rawSteps = [];
|
|
368
|
+
let sessionCount = 0;
|
|
283
369
|
if (Array.isArray(options.steps)) {
|
|
284
370
|
rawSteps.push(...options.steps);
|
|
371
|
+
sessionCount = Math.max(sessionCount, options.steps.length > 0 ? 1 : 0);
|
|
285
372
|
}
|
|
286
373
|
if (Array.isArray(options.sessions)) {
|
|
374
|
+
sessionCount = Math.max(sessionCount, options.sessions.length);
|
|
287
375
|
for (const sess of options.sessions) {
|
|
288
376
|
if (Array.isArray(sess?.steps)) rawSteps.push(...sess.steps);
|
|
289
377
|
}
|
|
290
378
|
}
|
|
291
379
|
if (!Array.isArray(options.steps) && !Array.isArray(options.sessions) && options.readSessions !== false) {
|
|
292
380
|
const maxSessions = Math.max(1, Number(options.maxSessions ?? 20));
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
381
|
+
const clientOpt = String(options.client || 'auto').trim().toLowerCase();
|
|
382
|
+
const includeClaude = clientOpt !== 'antigravity' && clientOpt !== 'ag';
|
|
383
|
+
const includeAg = clientOpt === 'antigravity' || clientOpt === 'ag' || clientOpt === 'both' || clientOpt === 'auto';
|
|
384
|
+
|
|
385
|
+
const claudeDiscovered = includeClaude ? discoverClaudeCodeSessions(homeDir, { cwd }).slice(0, maxSessions) : [];
|
|
386
|
+
const agDiscovered = includeAg ? discoverAntigravityTranscripts({ homeDir, cwd }).slice(0, maxSessions) : [];
|
|
387
|
+
sessionCount = claudeDiscovered.length + agDiscovered.length;
|
|
388
|
+
|
|
389
|
+
for (const meta of claudeDiscovered) {
|
|
390
|
+
const parsed = parseTranscriptIncremental(meta.filePath, { homeDir: explicitHomeDir });
|
|
391
|
+
if (Array.isArray(parsed?.steps)) {
|
|
392
|
+
rawSteps.push(
|
|
393
|
+
...parsed.steps.map((st) => ({
|
|
394
|
+
...st,
|
|
395
|
+
sessionId: st.sessionId || meta.sessionId || 'default'
|
|
396
|
+
}))
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
for (const meta of agDiscovered) {
|
|
402
|
+
const parsed = parseAntigravityTranscript(meta.filePath, { sessionId: meta.sessionId });
|
|
296
403
|
if (Array.isArray(parsed?.steps)) {
|
|
297
|
-
rawSteps.push(
|
|
404
|
+
rawSteps.push(
|
|
405
|
+
...parsed.steps.map((st) => ({
|
|
406
|
+
...st,
|
|
407
|
+
sessionId: st.sessionId || meta.sessionId || 'default'
|
|
408
|
+
}))
|
|
409
|
+
);
|
|
298
410
|
}
|
|
299
411
|
}
|
|
300
412
|
}
|
|
@@ -303,7 +415,7 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
303
415
|
const rawLedger = Array.isArray(options.ledgerEntries)
|
|
304
416
|
? options.ledgerEntries
|
|
305
417
|
: options.readLedger !== false
|
|
306
|
-
? readLedgerEntries({ homeDir, cwd })
|
|
418
|
+
? readLedgerEntries({ homeDir: explicitHomeDir, cwd })
|
|
307
419
|
: [];
|
|
308
420
|
|
|
309
421
|
const validTimestamps = commits.map((c) => c.timestampMs).filter((t) => Number.isFinite(t) && t > 0);
|
|
@@ -333,6 +445,7 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
333
445
|
}
|
|
334
446
|
|
|
335
447
|
const matchingLedger = rawLedger.filter((entry) => {
|
|
448
|
+
if (entry?.cwd && path.resolve(String(entry.cwd)) !== cwd) return false;
|
|
336
449
|
const rawTs = entry.timestamp ?? entry.ts ?? null;
|
|
337
450
|
const tsMs = typeof rawTs === 'number' ? rawTs : rawTs ? Date.parse(String(rawTs)) : NaN;
|
|
338
451
|
if (!Number.isFinite(tsMs) || !validTimestamps.length) return true;
|
|
@@ -340,15 +453,24 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
340
453
|
});
|
|
341
454
|
|
|
342
455
|
const ledgerSavedUsd = matchingLedger.reduce((s, e) => s + Number(e.savedUSD ?? e.savedUsd ?? 0), 0);
|
|
456
|
+
const ledgerRing0Count = matchingLedger.filter(
|
|
457
|
+
(e) =>
|
|
458
|
+
Boolean(e?.ring0Violation) ||
|
|
459
|
+
RING0_DETECTORS.has(String(e?.detectorId || e?.detector || '').toUpperCase()) ||
|
|
460
|
+
String(e?.category || '').toUpperCase() === 'RING0_VIOLATION_BLOCKED'
|
|
461
|
+
).length;
|
|
343
462
|
|
|
344
463
|
const commitBuckets = new Map(sortedCommits.map((_, idx) => [idx, []]));
|
|
345
464
|
const totalBranchLoc = sortedCommits.reduce((s, c) => s + Math.max(1, c.totalLoc || 0), 0);
|
|
346
465
|
|
|
347
466
|
let unmatchedIn = 0;
|
|
467
|
+
let unmatchedWrite = 0;
|
|
348
468
|
let unmatchedCache = 0;
|
|
349
469
|
let unmatchedOut = 0;
|
|
470
|
+
let unmatchedUsd = 0;
|
|
350
471
|
let unmatchedWaste = 0;
|
|
351
472
|
let unmatchedRing0 = 0;
|
|
473
|
+
const matchedAttrSteps = [];
|
|
352
474
|
|
|
353
475
|
for (const st of normalizedSteps) {
|
|
354
476
|
let assignedIdx = -1;
|
|
@@ -380,39 +502,80 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
380
502
|
|
|
381
503
|
if (assignedIdx >= 0) {
|
|
382
504
|
commitBuckets.get(assignedIdx).push(st);
|
|
505
|
+
if (!st.hasExplicitWaste) matchedAttrSteps.push(st);
|
|
383
506
|
} else if (sortedCommits.length > 0 && st.tsMs === null) {
|
|
384
507
|
unmatchedIn += st.inTok;
|
|
508
|
+
unmatchedWrite += st.cacheWriteTok;
|
|
385
509
|
unmatchedCache += st.cacheReadTok;
|
|
386
510
|
unmatchedOut += st.outTok;
|
|
511
|
+
unmatchedUsd += computeStepUsdWithModel(st, pricePerMillionUsd, hasExplicitPriceOverride);
|
|
387
512
|
unmatchedWaste += st.wasteTok;
|
|
388
513
|
if (st.isRing0) unmatchedRing0 += 1;
|
|
514
|
+
if (!st.hasExplicitWaste) matchedAttrSteps.push(st);
|
|
389
515
|
}
|
|
390
516
|
}
|
|
391
517
|
|
|
518
|
+
let inferredPreventableWaste = 0;
|
|
519
|
+
if (matchedAttrSteps.length >= 2) {
|
|
520
|
+
const attrCalls = matchedAttrSteps.map((st, idx) => ({
|
|
521
|
+
conv: st.sessionId || 'default',
|
|
522
|
+
idx: st.stepIndex ?? idx,
|
|
523
|
+
model: st.model || 'claude-sonnet-5-5',
|
|
524
|
+
freshInputTokens: st.inTok,
|
|
525
|
+
cacheCreationTokens: st.cacheWriteTok,
|
|
526
|
+
cacheCreationEphemeral5mInputTokens: st.cacheWrite5mTok,
|
|
527
|
+
cacheCreationEphemeral1hInputTokens: st.cacheWrite1hTok,
|
|
528
|
+
cacheReadTokens: st.cacheReadTok,
|
|
529
|
+
outputTokens: st.outTok,
|
|
530
|
+
createdAt: st.tsMs,
|
|
531
|
+
hadCompactionBefore: st.hadCompactionBefore,
|
|
532
|
+
isCompactSummary: st.isCompactSummary,
|
|
533
|
+
isCompactionBoundary: st.isCompactionBoundary,
|
|
534
|
+
isSidechain: st.isSidechain,
|
|
535
|
+
entrypoint: st.entrypoint,
|
|
536
|
+
shapeHash: st.shapeHash,
|
|
537
|
+
cacheTtlTier: st.cacheTtlTier
|
|
538
|
+
}));
|
|
539
|
+
const wasteAttr = attributeUncachedTokens(attrCalls);
|
|
540
|
+
inferredPreventableWaste =
|
|
541
|
+
Number(wasteAttr?.buckets?.idle_ttl_miss?.tokens || 0) +
|
|
542
|
+
Number(wasteAttr?.buckets?.model_or_effort_switch?.tokens || 0) +
|
|
543
|
+
Number(wasteAttr?.buckets?.unexplained_prefix_reset?.subBuckets?.actionable_prefix_reset?.tokens || 0) +
|
|
544
|
+
Number(wasteAttr?.buckets?.compaction_rebuild?.claude55CompactCacheMiss?.tokens || 0);
|
|
545
|
+
}
|
|
546
|
+
|
|
392
547
|
const fileAggMap = new Map();
|
|
393
548
|
let sumIn = 0;
|
|
549
|
+
let sumWrite = 0;
|
|
394
550
|
let sumCache = 0;
|
|
395
551
|
let sumOut = 0;
|
|
552
|
+
let sumCostUsd = 0;
|
|
396
553
|
let sumWaste = 0;
|
|
397
|
-
let sumRing0 = unmatchedRing0;
|
|
554
|
+
let sumRing0 = unmatchedRing0 + ledgerRing0Count;
|
|
398
555
|
|
|
399
556
|
const commitRows = sortedCommits.map((c, idx) => {
|
|
400
557
|
const bucket = commitBuckets.get(idx) || [];
|
|
401
558
|
const locShare = totalBranchLoc > 0 ? Math.max(1, c.totalLoc || 0) / totalBranchLoc : 0;
|
|
402
559
|
|
|
403
560
|
const cIn = bucket.reduce((s, st) => s + st.inTok, 0) + Math.round(unmatchedIn * locShare);
|
|
561
|
+
const cWrite = bucket.reduce((s, st) => s + st.cacheWriteTok, 0) + Math.round(unmatchedWrite * locShare);
|
|
404
562
|
const cCache = bucket.reduce((s, st) => s + st.cacheReadTok, 0) + Math.round(unmatchedCache * locShare);
|
|
405
563
|
const cOut = bucket.reduce((s, st) => s + st.outTok, 0) + Math.round(unmatchedOut * locShare);
|
|
406
564
|
const cWaste = bucket.reduce((s, st) => s + st.wasteTok, 0) + Math.round(unmatchedWaste * locShare);
|
|
407
565
|
const cRing0 = bucket.filter((st) => st.isRing0).length;
|
|
408
|
-
const cTotal = cIn + cCache + cOut;
|
|
409
|
-
const cDenom = cIn + cCache;
|
|
566
|
+
const cTotal = cIn + cWrite + cCache + cOut;
|
|
567
|
+
const cDenom = cIn + cWrite + cCache;
|
|
410
568
|
const cHitPct = cDenom > 0 ? Math.round((cCache / cDenom) * 100) : 0;
|
|
411
|
-
const
|
|
569
|
+
const rawBucketUsd =
|
|
570
|
+
bucket.reduce((s, st) => s + computeStepUsdWithModel(st, pricePerMillionUsd, hasExplicitPriceOverride), 0) +
|
|
571
|
+
unmatchedUsd * locShare;
|
|
572
|
+
const cCostUsd = Number(rawBucketUsd.toFixed(4));
|
|
412
573
|
|
|
413
574
|
sumIn += cIn;
|
|
575
|
+
sumWrite += cWrite;
|
|
414
576
|
sumCache += cCache;
|
|
415
577
|
sumOut += cOut;
|
|
578
|
+
sumCostUsd += rawBucketUsd;
|
|
416
579
|
sumWaste += cWaste;
|
|
417
580
|
sumRing0 += cRing0;
|
|
418
581
|
|
|
@@ -446,6 +609,7 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
446
609
|
deletions: c.totalDeletions,
|
|
447
610
|
attributedTokens: cTotal,
|
|
448
611
|
inputTokens: cIn,
|
|
612
|
+
cacheWriteTokens: cWrite,
|
|
449
613
|
cacheReadTokens: cCache,
|
|
450
614
|
outputTokens: cOut,
|
|
451
615
|
wasteTokens: cWaste,
|
|
@@ -455,10 +619,29 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
455
619
|
};
|
|
456
620
|
});
|
|
457
621
|
|
|
458
|
-
|
|
459
|
-
|
|
622
|
+
if (inferredPreventableWaste > 0 && commitRows.length > 0) {
|
|
623
|
+
const totalInputForShare = commitRows.reduce((s, r) => s + Math.max(1, r.inputTokens + r.cacheWriteTokens), 0);
|
|
624
|
+
let distributed = 0;
|
|
625
|
+
for (let i = 0; i < commitRows.length; i++) {
|
|
626
|
+
const shareTok =
|
|
627
|
+
i === commitRows.length - 1
|
|
628
|
+
? Math.max(0, inferredPreventableWaste - distributed)
|
|
629
|
+
: Math.round(
|
|
630
|
+
(inferredPreventableWaste * Math.max(1, commitRows[i].inputTokens + commitRows[i].cacheWriteTokens)) /
|
|
631
|
+
totalInputForShare
|
|
632
|
+
);
|
|
633
|
+
commitRows[i].wasteTokens += shareTok;
|
|
634
|
+
distributed += shareTok;
|
|
635
|
+
}
|
|
636
|
+
sumWaste += inferredPreventableWaste;
|
|
637
|
+
} else if (inferredPreventableWaste > 0) {
|
|
638
|
+
sumWaste += inferredPreventableWaste;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
const totalTokens = sumIn + sumWrite + sumCache + sumOut;
|
|
642
|
+
const cacheDenom = sumIn + sumWrite + sumCache;
|
|
460
643
|
const cacheHitPct = cacheDenom > 0 ? Math.round((sumCache / cacheDenom) * 100) : 0;
|
|
461
|
-
const estimatedCostUsd = Number(
|
|
644
|
+
const estimatedCostUsd = Number(sumCostUsd.toFixed(4));
|
|
462
645
|
const wasteRatioPct = totalTokens > 0 ? Math.round((sumWaste / totalTokens) * 100) : 0;
|
|
463
646
|
const uniqueFiles = new Set(commitRows.flatMap((_, idx) => sortedCommits[idx].files.map((f) => f.filePath)));
|
|
464
647
|
const totalAdditions = sortedCommits.reduce((s, c) => s + c.totalAdditions, 0);
|
|
@@ -501,12 +684,14 @@ export function buildPrCostAttribution(options = {}) {
|
|
|
501
684
|
head,
|
|
502
685
|
fallbackUsed,
|
|
503
686
|
summary: {
|
|
687
|
+
sessionCount,
|
|
504
688
|
totalCommits: commitRows.length,
|
|
505
689
|
totalFilesChanged: uniqueFiles.size,
|
|
506
690
|
totalAdditions,
|
|
507
691
|
totalDeletions,
|
|
508
692
|
totalLoc,
|
|
509
693
|
totalInputTokens: sumIn,
|
|
694
|
+
totalCacheWriteTokens: sumWrite,
|
|
510
695
|
totalCacheReadTokens: sumCache,
|
|
511
696
|
totalOutputTokens: sumOut,
|
|
512
697
|
totalTokens,
|
package/src/ci/team-digest.mjs
CHANGED
|
@@ -15,19 +15,21 @@ import { loadCiBudgetPolicy } from './ci-gate.mjs';
|
|
|
15
15
|
*/
|
|
16
16
|
export function buildTeamDigest(options = {}) {
|
|
17
17
|
const cwd = path.resolve(options.cwd || process.cwd());
|
|
18
|
-
const
|
|
18
|
+
const explicitHomeDir = typeof options.homeDir === 'string' && options.homeDir.trim()
|
|
19
|
+
? options.homeDir.trim()
|
|
20
|
+
: null;
|
|
19
21
|
const days = Math.max(1, Number(options.days ?? 7));
|
|
20
22
|
const nowMs = Number(options.nowMs || Date.now());
|
|
21
23
|
const cutoffMs = nowMs - days * 24 * 60 * 60 * 1000;
|
|
22
24
|
|
|
23
25
|
let sessions = Array.isArray(options.sessions) ? options.sessions : null;
|
|
24
26
|
if (!sessions) {
|
|
25
|
-
const audited = auditClaudeSessions({ cwd, homeDir, limit: options.limit ?? 50 });
|
|
27
|
+
const audited = auditClaudeSessions({ cwd, homeDir: explicitHomeDir, limit: options.limit ?? 50 });
|
|
26
28
|
sessions = audited.sessions.filter((s) => !s.mtimeMs || s.mtimeMs >= cutoffMs);
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
const prReports = Array.isArray(options.prReports) ? options.prReports : [];
|
|
30
|
-
const ledgerSummary = options.ledgerSummary || getSavedQuotaReceiptSummary({ homeDir, nowMs });
|
|
32
|
+
const ledgerSummary = options.ledgerSummary || getSavedQuotaReceiptSummary({ homeDir: explicitHomeDir, nowMs });
|
|
31
33
|
const policy = loadCiBudgetPolicy(cwd, options.policyOverrides || {});
|
|
32
34
|
|
|
33
35
|
let totalSpendUsd = 0;
|
|
@@ -112,7 +114,7 @@ export function formatTeamDigest(digest = {}) {
|
|
|
112
114
|
`2. Cache Efficiency: ${s.cacheHitPct ?? 0}% prompt cache hit rate (${((s.totalTokens || 0) / 1000).toFixed(1)}k total tokens).`,
|
|
113
115
|
`3. Top Cost Driver: ${td.label} ($${Number(td.spendUsd || 0).toFixed(2)}, ${((td.tokens || 0) / 1000).toFixed(1)}k tok).`,
|
|
114
116
|
`4. Guard Savings: $${Number(s.avoidedUsd || 0).toFixed(2)} avoided (${((s.avoidedTokens || 0) / 1000).toFixed(1)}k tok across ${s.interventions ?? 0} interventions).`,
|
|
115
|
-
`5. Policy Gate Status: ${s.ring0Violations ?? 0} Ring-0 violations | PR cap <= $${Number(s.maxPrCostUsd
|
|
117
|
+
`5. Policy Gate Status: ${s.ring0Violations ?? 0} Ring-0 violations | PR cap <= $${Number(s.maxPrCostUsd ?? 15).toFixed(2)}.`
|
|
116
118
|
];
|
|
117
119
|
return lines.join('\n') + '\n';
|
|
118
120
|
}
|