burnoff 1.4.1 → 2.1.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/.claude-plugin/plugin.json +1 -1
- package/.github/workflows/burnoff-pr-gate.yml +5 -3
- package/.github/workflows/burnoff-startup-lint.yml +2 -2
- package/CHANGELOG.md +74 -0
- package/README.md +187 -213
- package/SECURITY.md +38 -0
- package/bin/burnoff.mjs +307 -32
- package/commands/burnoff.md +6 -4
- package/docs/CLAUDE_CODE_PLUGIN.md +29 -28
- package/docs/DESIGN_PARTNER_INTAKE.md +3 -3
- package/hooks/hook-io.mjs +216 -11
- package/hooks/hooks.json +44 -0
- package/hooks/post-tool-use.mjs +3 -1
- package/hooks/pre-tool-use.mjs +47 -8
- package/hooks/session-lifecycle.mjs +24 -3
- package/hooks/user-prompt-submit.mjs +98 -0
- package/package.json +6 -4
- package/scripts/bench-hooks.mjs +34 -3
- package/scripts/bench-savings.mjs +318 -0
- package/scripts/burnoff-statusline.mjs +138 -6
- package/src/ci/ci-gate.mjs +199 -13
- package/src/ci/pr-cost.mjs +417 -45
- package/src/ci/pr-summary.mjs +467 -0
- package/src/cli/audit.mjs +179 -28
- package/src/cli/doctor.mjs +32 -6
- package/src/cli/hook-health.mjs +9 -1
- package/src/cli/init.mjs +226 -39
- package/src/cli/preview.mjs +299 -0
- package/src/cli/startup-audit/formatter.mjs +248 -0
- package/src/cli/startup-audit/jsonc.mjs +601 -0
- package/src/cli/startup-audit/mcp-audit.mjs +735 -0
- package/src/cli/startup-audit/rules-hygiene.mjs +645 -0
- package/src/cli/startup-audit/telemetry-scan.mjs +975 -0
- package/src/cli/startup-audit.mjs +451 -2471
- package/src/clients/claude-code.mjs +41 -4
- package/src/config/catalog.json +295 -0
- package/src/config/effort.mjs +145 -0
- package/src/config/harness-quirks.mjs +340 -0
- package/src/config/loader.mjs +27 -2
- package/src/config/policy.mjs +81 -9
- package/src/config/pricing.mjs +247 -19
- package/src/config/semver.mjs +25 -0
- package/src/config/uncached-attribution.mjs +280 -41
- package/src/guards/compaction-baton.mjs +248 -0
- package/src/guards/loop-breaker.mjs +328 -47
- package/src/guards/model-switch-hook.mjs +126 -0
- package/src/guards/phase5-detectors.mjs +275 -21
- package/src/guards/post-tool-engine.mjs +333 -11
- package/src/guards/pre-tool-engine.mjs +607 -45
- package/src/guards/prefix-guard.mjs +609 -0
- package/src/guards/prompt-guard.mjs +309 -0
- package/src/guards/read-clamp.mjs +192 -8
- package/src/guards/session-hook.mjs +149 -7
- package/src/guards/shell-guard.mjs +56 -5
- package/src/guards/snooze.mjs +169 -5
- package/src/ipc/daemon.mjs +76 -0
- package/src/ipc/fast-client.mjs +165 -4
- package/src/ipc/protocol.mjs +16 -3
- package/src/storage/antigravity-parser.mjs +16 -2
- package/src/storage/file-lock.mjs +16 -3
- package/src/storage/jsonl-parser.mjs +133 -22
- package/src/storage/telemetry-metrics.mjs +254 -4
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/ci/pr-summary.mjs
|
|
3
|
+
* Zero-Secret PR Usage Summary Exporter & Ephemeral CI Runner Verifier (`burnoff-pr-summary/v1`).
|
|
4
|
+
*
|
|
5
|
+
* Exports sanitized, integrity-hashed branch/PR token & cost summaries locally so
|
|
6
|
+
* ephemeral CI runners without `~/.claude/projects/` transcripts can verify and gate PRs
|
|
7
|
+
* without leaking prompts, credentials, author PII, or local OS paths.
|
|
8
|
+
* Zero runtime dependencies, zero network calls.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { execFileSync } from 'node:child_process';
|
|
12
|
+
import crypto from 'node:crypto';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import os from 'node:os';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { redactSecretsPure } from '../guards/credential-scrubber.mjs';
|
|
17
|
+
import { atomicWriteFileSync } from '../storage/atomic-io.mjs';
|
|
18
|
+
|
|
19
|
+
export const PR_SUMMARY_FORMAT = 'burnoff-pr-summary/v1';
|
|
20
|
+
export const PR_ATTRIBUTION_FORMAT = 'burnoff-pr-attribution/v1';
|
|
21
|
+
export const DEFAULT_PR_SUMMARY_REL_PATH = '.burnoff-ci/pr-summary.json';
|
|
22
|
+
|
|
23
|
+
const SUMMARY_ALLOWED_KEYS = Object.freeze([
|
|
24
|
+
'sessionCount',
|
|
25
|
+
'totalCommits',
|
|
26
|
+
'totalFilesChanged',
|
|
27
|
+
'totalAdditions',
|
|
28
|
+
'totalDeletions',
|
|
29
|
+
'totalLoc',
|
|
30
|
+
'totalInputTokens',
|
|
31
|
+
'totalCacheWriteTokens',
|
|
32
|
+
'totalCacheReadTokens',
|
|
33
|
+
'totalOutputTokens',
|
|
34
|
+
'totalTokens',
|
|
35
|
+
'cacheHitPct',
|
|
36
|
+
'estimatedCostUsd',
|
|
37
|
+
'reconciledCostUsd',
|
|
38
|
+
'attributionVariancePct',
|
|
39
|
+
'estimatedSavedUsd',
|
|
40
|
+
'estimatedQuotaUnits',
|
|
41
|
+
'quotaUnitLabel',
|
|
42
|
+
'quotaWeightsDisclosure',
|
|
43
|
+
'quotaBreakdown',
|
|
44
|
+
'wasteRatioBasis',
|
|
45
|
+
'grossWasteTokens',
|
|
46
|
+
'grossWasteRatioPct',
|
|
47
|
+
'excludedUpstreamTokens',
|
|
48
|
+
'excludedUpstreamSharePct',
|
|
49
|
+
'appendFloorTokens',
|
|
50
|
+
'upstreamEvictionTokens',
|
|
51
|
+
'evictionByPattern',
|
|
52
|
+
'actionableWasteTokens',
|
|
53
|
+
'actionableWasteRatioPct',
|
|
54
|
+
'estimatedWasteTokens',
|
|
55
|
+
'wasteRatioPct',
|
|
56
|
+
'ring0Violations',
|
|
57
|
+
'usdPer100Loc'
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
function isExcludedLocalToolPath(relPath = '') {
|
|
61
|
+
const norm = String(relPath || '').replace(/\\/g, '/').replace(/^\.\//, '').toLowerCase();
|
|
62
|
+
return norm === '.claude/settings.local.json' || norm.endsWith('/.claude/settings.local.json');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function sanitizeRepoPath(rawPath = '', cwd = '') {
|
|
66
|
+
const input = String(rawPath || '').trim();
|
|
67
|
+
if (!input) return '';
|
|
68
|
+
let normalized = input.replace(/\\/g, '/');
|
|
69
|
+
if (cwd) {
|
|
70
|
+
const abs = path.isAbsolute(input) ? input : path.resolve(cwd, input);
|
|
71
|
+
const rel = path.relative(cwd, abs).replace(/\\/g, '/');
|
|
72
|
+
if (rel && !rel.startsWith('..') && !path.isAbsolute(rel)) {
|
|
73
|
+
normalized = rel;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const homeNorm = String(os.homedir() || '').replace(/\\/g, '/').replace(/\/+$/, '');
|
|
77
|
+
if (homeNorm && normalized.toLowerCase().startsWith(`${homeNorm.toLowerCase()}/`)) {
|
|
78
|
+
normalized = normalized.slice(homeNorm.length + 1);
|
|
79
|
+
}
|
|
80
|
+
normalized = normalized
|
|
81
|
+
.replace(/^[A-Za-z]:\/Users\/[^/]+\//i, '')
|
|
82
|
+
.replace(/^\/Users\/[^/]+\//i, '')
|
|
83
|
+
.replace(/^\/home\/[^/]+\//i, '')
|
|
84
|
+
.replace(/^file:\/\/\//i, '')
|
|
85
|
+
.replace(/^\.\//, '');
|
|
86
|
+
return normalized;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function escapeRegExp(str = '') {
|
|
90
|
+
return String(str).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function scrubTextField(rawText = '', maxLen = 200) {
|
|
94
|
+
let out = String(rawText ?? '');
|
|
95
|
+
|
|
96
|
+
const homeRaw = String(os.homedir() || '').replace(/[\\/]+$/, '');
|
|
97
|
+
if (homeRaw) {
|
|
98
|
+
const homeSlash = homeRaw.replace(/\\/g, '/');
|
|
99
|
+
const homeBack = homeRaw.replace(/\//g, '\\');
|
|
100
|
+
for (const prefix of new Set([homeRaw, homeSlash, homeBack])) {
|
|
101
|
+
if (!prefix) continue;
|
|
102
|
+
const prefixRe = new RegExp(`${escapeRegExp(prefix)}[\\\\/]?`, 'gi');
|
|
103
|
+
out = out.replace(prefixRe, '');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
out = out
|
|
108
|
+
.replace(/file:\/\/\/[A-Za-z]:[\\/][^ \r\n\t'"]+/gi, (m) => sanitizeRepoPath(m))
|
|
109
|
+
.replace(/\b[A-Za-z]:[\\/]Users[\\/][^\\/\s'"]+[\\/]/gi, '')
|
|
110
|
+
.replace(/(^|[\s'"])(?:\/Users\/[^/\s'"]+\/|\/home\/[^/\s'"]+\/)/g, '$1');
|
|
111
|
+
|
|
112
|
+
const secretRes = redactSecretsPure(out);
|
|
113
|
+
const collapsed = secretRes.sanitized.replace(/[\r\n]+/g, ' ').trim();
|
|
114
|
+
const capped = maxLen > 0 && collapsed.length > maxLen ? `${collapsed.slice(0, maxLen - 3)}...` : collapsed;
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
text: capped,
|
|
118
|
+
redactedCount: secretRes.redactedCount
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function canonicalizeValue(value) {
|
|
123
|
+
if (Array.isArray(value)) {
|
|
124
|
+
return value.map((item) => canonicalizeValue(item));
|
|
125
|
+
}
|
|
126
|
+
if (value && typeof value === 'object') {
|
|
127
|
+
const sorted = {};
|
|
128
|
+
for (const key of Object.keys(value).sort()) {
|
|
129
|
+
const v = value[key];
|
|
130
|
+
if (v !== undefined) {
|
|
131
|
+
sorted[key] = canonicalizeValue(v);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return sorted;
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Resolves the git commit SHA for `head` (defaults to `HEAD`), returning `null` if unavailable.
|
|
141
|
+
*/
|
|
142
|
+
export function resolveGitHeadSha(cwd, head = 'HEAD') {
|
|
143
|
+
try {
|
|
144
|
+
const out = execFileSync('git', ['rev-parse', '--verify', '--quiet', `${head}^{commit}`], {
|
|
145
|
+
cwd: path.resolve(cwd || process.cwd()),
|
|
146
|
+
encoding: 'utf8',
|
|
147
|
+
stdio: ['ignore', 'pipe', 'ignore']
|
|
148
|
+
});
|
|
149
|
+
const trimmed = String(out || '').trim();
|
|
150
|
+
return trimmed || null;
|
|
151
|
+
} catch {
|
|
152
|
+
return null; // ignore: non-git workspace or unresolvable revision yields null headSha
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Computes deterministic SHA-256 integrity digest over the signed PR summary payload.
|
|
158
|
+
*/
|
|
159
|
+
export function computePrSummaryDigest(doc = {}) {
|
|
160
|
+
const payload = canonicalizeValue({
|
|
161
|
+
format: doc.format,
|
|
162
|
+
headSha: doc.headSha ?? null,
|
|
163
|
+
base: doc.base ?? 'main',
|
|
164
|
+
head: doc.head ?? 'HEAD',
|
|
165
|
+
summary: doc.summary ?? {},
|
|
166
|
+
notices: Array.isArray(doc.notices) ? doc.notices : [],
|
|
167
|
+
commits: Array.isArray(doc.commits) ? doc.commits : [],
|
|
168
|
+
topCostFiles: Array.isArray(doc.topCostFiles) ? doc.topCostFiles : []
|
|
169
|
+
});
|
|
170
|
+
return crypto.createHash('sha256').update(JSON.stringify(payload), 'utf8').digest('hex');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function pickSummaryFields(rawSummary = {}) {
|
|
174
|
+
const out = {};
|
|
175
|
+
for (const key of SUMMARY_ALLOWED_KEYS) {
|
|
176
|
+
if (rawSummary[key] === undefined) continue;
|
|
177
|
+
const val = rawSummary[key];
|
|
178
|
+
if (val === null || typeof val === 'number' || typeof val === 'boolean') {
|
|
179
|
+
out[key] = val;
|
|
180
|
+
} else if (typeof val === 'string') {
|
|
181
|
+
out[key] = scrubTextField(val, 240).text;
|
|
182
|
+
} else if (key === 'quotaBreakdown' && val && typeof val === 'object') {
|
|
183
|
+
out[key] = {
|
|
184
|
+
unit: typeof val.unit === 'string' ? val.unit : 'Q-units',
|
|
185
|
+
estimated: Boolean(val.estimated),
|
|
186
|
+
totalQuotaUnits: Number(val.totalQuotaUnits || 0),
|
|
187
|
+
effectiveMultiplier: Number(val.effectiveMultiplier || 1),
|
|
188
|
+
elevatedModels: Array.isArray(val.elevatedModels) ? val.elevatedModels.map(String).sort() : [],
|
|
189
|
+
sdkCliCalls: Number(val.sdkCliCalls || 0),
|
|
190
|
+
acpCustomAgentCalls: Number(val.acpCustomAgentCalls || 0)
|
|
191
|
+
};
|
|
192
|
+
} else if (key === 'evictionByPattern' && val && typeof val === 'object') {
|
|
193
|
+
const cleanPatterns = {};
|
|
194
|
+
for (const [pk, pv] of Object.entries(val)) {
|
|
195
|
+
if (Number.isFinite(Number(pv))) {
|
|
196
|
+
cleanPatterns[String(pk)] = Number(pv);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
out[key] = cleanPatterns;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function sanitizeCommitRow(row = {}) {
|
|
206
|
+
const scrubbedSubject = scrubTextField(row.subject || '', 160);
|
|
207
|
+
const sha = String(row.sha || '');
|
|
208
|
+
return {
|
|
209
|
+
row: {
|
|
210
|
+
sha,
|
|
211
|
+
shortSha: String(row.shortSha || sha.slice(0, 7)),
|
|
212
|
+
timestampIso: String(row.timestampIso || ''),
|
|
213
|
+
subject: scrubbedSubject.text,
|
|
214
|
+
filesChanged: Number(row.filesChanged || 0),
|
|
215
|
+
additions: Number(row.additions || 0),
|
|
216
|
+
deletions: Number(row.deletions || 0),
|
|
217
|
+
inputTokens: Number(row.inputTokens || 0),
|
|
218
|
+
cacheWriteTokens: Number(row.cacheWriteTokens || 0),
|
|
219
|
+
cacheReadTokens: Number(row.cacheReadTokens || 0),
|
|
220
|
+
outputTokens: Number(row.outputTokens || 0),
|
|
221
|
+
attributedTokens: Number(row.attributedTokens || 0),
|
|
222
|
+
cacheHitPct: Number(row.cacheHitPct || 0),
|
|
223
|
+
estimatedCostUsd: Number(row.estimatedCostUsd || 0),
|
|
224
|
+
wasteTokens: Number(row.wasteTokens || 0),
|
|
225
|
+
ring0Violations: Number(row.ring0Violations || 0)
|
|
226
|
+
},
|
|
227
|
+
redactedCount: scrubbedSubject.redactedCount
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function sanitizeTopCostFiles(files = [], cwd = '') {
|
|
232
|
+
return (Array.isArray(files) ? files : [])
|
|
233
|
+
.map((f) => {
|
|
234
|
+
const cleanPath = sanitizeRepoPath(f?.filePath || '', cwd);
|
|
235
|
+
return {
|
|
236
|
+
filePath: cleanPath,
|
|
237
|
+
additions: Number(f?.additions || 0),
|
|
238
|
+
deletions: Number(f?.deletions || 0),
|
|
239
|
+
attributedTokens: Number(f?.attributedTokens || 0),
|
|
240
|
+
estimatedCostUsd: Number(f?.estimatedCostUsd || 0)
|
|
241
|
+
};
|
|
242
|
+
})
|
|
243
|
+
.filter((f) => f.filePath && !isExcludedLocalToolPath(f.filePath));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function sanitizeNotices(notices = []) {
|
|
247
|
+
const cleaned = [];
|
|
248
|
+
let redactedCount = 0;
|
|
249
|
+
for (const n of Array.isArray(notices) ? notices : []) {
|
|
250
|
+
if (!n || !n.code) continue;
|
|
251
|
+
const msgRes = scrubTextField(n.message || '', 300);
|
|
252
|
+
redactedCount += msgRes.redactedCount;
|
|
253
|
+
cleaned.push({
|
|
254
|
+
code: String(n.code),
|
|
255
|
+
message: msgRes.text
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return { notices: cleaned, redactedCount };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Builds a zero-secret, integrity-hashed `burnoff-pr-summary/v1` document from a `pr-cost` report.
|
|
263
|
+
*/
|
|
264
|
+
export function buildPrSummary(report, options = {}) {
|
|
265
|
+
if (!report || report.ok === false || !report.summary || typeof report.summary !== 'object') {
|
|
266
|
+
throw new Error('Cannot build PR summary: attribution report is missing or invalid.');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const cwd = path.resolve(options.cwd || process.cwd());
|
|
270
|
+
const base = String(report.base || options.base || 'main');
|
|
271
|
+
const head = String(report.head || options.head || 'HEAD');
|
|
272
|
+
|
|
273
|
+
let redactedCount = 0;
|
|
274
|
+
const commits = [];
|
|
275
|
+
for (const rawCommit of Array.isArray(report.commits) ? report.commits : []) {
|
|
276
|
+
const res = sanitizeCommitRow(rawCommit);
|
|
277
|
+
redactedCount += res.redactedCount;
|
|
278
|
+
commits.push(res.row);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const topCostFiles = sanitizeTopCostFiles(report.topCostFiles, cwd);
|
|
282
|
+
const noticeRes = sanitizeNotices(report.notices);
|
|
283
|
+
redactedCount += noticeRes.redactedCount;
|
|
284
|
+
|
|
285
|
+
const summary = pickSummaryFields(report.summary);
|
|
286
|
+
const headSha =
|
|
287
|
+
options.headSha !== undefined
|
|
288
|
+
? options.headSha
|
|
289
|
+
: report.headSha !== undefined
|
|
290
|
+
? report.headSha
|
|
291
|
+
: resolveGitHeadSha(cwd, head) ?? (commits.length ? commits[commits.length - 1].sha || null : null);
|
|
292
|
+
|
|
293
|
+
const doc = {
|
|
294
|
+
format: PR_SUMMARY_FORMAT,
|
|
295
|
+
generatedAt: report.generatedAt || new Date().toISOString(),
|
|
296
|
+
headSha,
|
|
297
|
+
base,
|
|
298
|
+
head,
|
|
299
|
+
fallbackUsed: Boolean(report.fallbackUsed),
|
|
300
|
+
privacy: {
|
|
301
|
+
zeroSecrets: true,
|
|
302
|
+
promptsIncluded: false,
|
|
303
|
+
localPathsStripped: true,
|
|
304
|
+
redactedCount
|
|
305
|
+
},
|
|
306
|
+
summary,
|
|
307
|
+
notices: noticeRes.notices,
|
|
308
|
+
commits,
|
|
309
|
+
topCostFiles
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
doc.integrity = {
|
|
313
|
+
algorithm: 'sha256',
|
|
314
|
+
digest: computePrSummaryDigest(doc)
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
return doc;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Exports and atomically writes a zero-secret PR summary file (defaults to `.burnoff-ci/pr-summary.json`).
|
|
322
|
+
*/
|
|
323
|
+
export function writePrSummary(report, options = {}) {
|
|
324
|
+
try {
|
|
325
|
+
const cwd = path.resolve(options.cwd || process.cwd());
|
|
326
|
+
const doc = buildPrSummary(report, { ...options, cwd });
|
|
327
|
+
const targetPath = options.outPath
|
|
328
|
+
? path.resolve(cwd, options.outPath)
|
|
329
|
+
: path.join(cwd, DEFAULT_PR_SUMMARY_REL_PATH);
|
|
330
|
+
|
|
331
|
+
atomicWriteFileSync(targetPath, JSON.stringify(doc, null, 2) + '\n', { encoding: 'utf8' });
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
ok: true,
|
|
335
|
+
format: PR_SUMMARY_FORMAT,
|
|
336
|
+
path: targetPath,
|
|
337
|
+
relativePath: sanitizeRepoPath(targetPath, cwd) || DEFAULT_PR_SUMMARY_REL_PATH,
|
|
338
|
+
headSha: doc.headSha,
|
|
339
|
+
digest: doc.integrity.digest,
|
|
340
|
+
redactedCount: doc.privacy.redactedCount,
|
|
341
|
+
summary: doc
|
|
342
|
+
};
|
|
343
|
+
} catch (err) {
|
|
344
|
+
return {
|
|
345
|
+
ok: false,
|
|
346
|
+
error: err?.message || String(err)
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Reads, integrity-verifies, and hydrates a `.burnoff-ci/pr-summary.json` into a `pr-cost` report.
|
|
353
|
+
*/
|
|
354
|
+
export function readPrSummary(summaryPath, options = {}) {
|
|
355
|
+
const cwd = path.resolve(options.cwd || process.cwd());
|
|
356
|
+
const absPath = path.resolve(cwd, summaryPath || DEFAULT_PR_SUMMARY_REL_PATH);
|
|
357
|
+
|
|
358
|
+
if (!fs.existsSync(absPath)) {
|
|
359
|
+
return {
|
|
360
|
+
ok: false,
|
|
361
|
+
code: 'SUMMARY_NOT_FOUND',
|
|
362
|
+
error: `PR summary file not found: ${absPath}`
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
let doc;
|
|
367
|
+
try {
|
|
368
|
+
doc = JSON.parse(fs.readFileSync(absPath, 'utf8'));
|
|
369
|
+
} catch (err) {
|
|
370
|
+
return {
|
|
371
|
+
ok: false,
|
|
372
|
+
code: 'SUMMARY_INVALID_JSON',
|
|
373
|
+
error: `Invalid PR summary JSON (${absPath}): ${err?.message || String(err)}`
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (!doc || typeof doc !== 'object' || doc.format !== PR_SUMMARY_FORMAT || !doc.summary || typeof doc.summary !== 'object') {
|
|
378
|
+
return {
|
|
379
|
+
ok: false,
|
|
380
|
+
code: 'SUMMARY_INVALID_FORMAT',
|
|
381
|
+
error: `Invalid PR summary format in ${absPath} (expected format "${PR_SUMMARY_FORMAT}").`
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (options.verifyIntegrity !== false) {
|
|
386
|
+
const expectedDigest = computePrSummaryDigest(doc);
|
|
387
|
+
if (
|
|
388
|
+
!doc.integrity ||
|
|
389
|
+
doc.integrity.algorithm !== 'sha256' ||
|
|
390
|
+
typeof doc.integrity.digest !== 'string' ||
|
|
391
|
+
doc.integrity.digest !== expectedDigest
|
|
392
|
+
) {
|
|
393
|
+
return {
|
|
394
|
+
ok: false,
|
|
395
|
+
code: 'SUMMARY_INTEGRITY_MISMATCH',
|
|
396
|
+
error: 'PR summary integrity digest mismatch (file may be corrupted or modified). Re-run `burnoff pr-cost --write-summary`.'
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const expectedHeadSha =
|
|
402
|
+
options.expectedHeadSha !== undefined
|
|
403
|
+
? options.expectedHeadSha
|
|
404
|
+
: resolveGitHeadSha(cwd, options.head || doc.head || 'HEAD');
|
|
405
|
+
|
|
406
|
+
const notices = sanitizeNotices(doc.notices).notices;
|
|
407
|
+
let stale = false;
|
|
408
|
+
if (expectedHeadSha && doc.headSha && expectedHeadSha !== doc.headSha) {
|
|
409
|
+
stale = true;
|
|
410
|
+
notices.push({
|
|
411
|
+
code: 'STALE_PR_SUMMARY',
|
|
412
|
+
message: `PR summary was exported at ${String(doc.headSha).slice(0, 7)}, current HEAD is ${String(expectedHeadSha).slice(0, 7)}; re-run 'burnoff pr-cost --write-summary' to refresh.`
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const summary = pickSummaryFields(doc.summary);
|
|
417
|
+
if (options.wasteRatioBasis !== undefined && options.wasteRatioBasis !== null) {
|
|
418
|
+
const basis = String(options.wasteRatioBasis).trim().toLowerCase() === 'gross' ? 'gross' : 'actionable';
|
|
419
|
+
summary.wasteRatioBasis = basis;
|
|
420
|
+
if (basis === 'gross') {
|
|
421
|
+
if (summary.grossWasteTokens !== undefined) summary.estimatedWasteTokens = summary.grossWasteTokens;
|
|
422
|
+
if (summary.grossWasteRatioPct !== undefined) summary.wasteRatioPct = summary.grossWasteRatioPct;
|
|
423
|
+
} else {
|
|
424
|
+
if (summary.actionableWasteTokens !== undefined) summary.estimatedWasteTokens = summary.actionableWasteTokens;
|
|
425
|
+
if (summary.actionableWasteRatioPct !== undefined) summary.wasteRatioPct = summary.actionableWasteRatioPct;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const commits = (Array.isArray(doc.commits) ? doc.commits : []).map((c) => sanitizeCommitRow(c).row);
|
|
430
|
+
const topCostFiles = sanitizeTopCostFiles(doc.topCostFiles, cwd);
|
|
431
|
+
const relativePath = sanitizeRepoPath(absPath, cwd) || DEFAULT_PR_SUMMARY_REL_PATH;
|
|
432
|
+
|
|
433
|
+
return {
|
|
434
|
+
ok: true,
|
|
435
|
+
stale,
|
|
436
|
+
summaryPath: absPath,
|
|
437
|
+
relativePath,
|
|
438
|
+
report: {
|
|
439
|
+
ok: true,
|
|
440
|
+
format: PR_ATTRIBUTION_FORMAT,
|
|
441
|
+
source: 'pr-summary',
|
|
442
|
+
summaryFormat: PR_SUMMARY_FORMAT,
|
|
443
|
+
summarySourcePath: relativePath,
|
|
444
|
+
summaryDigest: doc.integrity?.digest || null,
|
|
445
|
+
headSha: doc.headSha ?? null,
|
|
446
|
+
staleSummary: stale,
|
|
447
|
+
generatedAt: doc.generatedAt || null,
|
|
448
|
+
base: doc.base || 'main',
|
|
449
|
+
head: doc.head || 'HEAD',
|
|
450
|
+
fallbackUsed: Boolean(doc.fallbackUsed),
|
|
451
|
+
summary,
|
|
452
|
+
notices,
|
|
453
|
+
commits,
|
|
454
|
+
topCostFiles
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export default {
|
|
460
|
+
PR_SUMMARY_FORMAT,
|
|
461
|
+
DEFAULT_PR_SUMMARY_REL_PATH,
|
|
462
|
+
resolveGitHeadSha,
|
|
463
|
+
computePrSummaryDigest,
|
|
464
|
+
buildPrSummary,
|
|
465
|
+
writePrSummary,
|
|
466
|
+
readPrSummary
|
|
467
|
+
};
|