@relayburn/sdk 1.9.0 → 2.0.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/CHANGELOG.md +27 -29
- package/README.md +27 -34
- package/package.json +46 -17
- package/src/binding.cjs +91 -0
- package/src/binding.d.ts +21 -0
- package/src/index.cjs +68 -0
- package/src/index.d.ts +422 -0
- package/src/index.js +144 -0
- package/index.d.ts +0 -234
- package/index.js +0 -630
package/index.js
DELETED
|
@@ -1,630 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildArchive,
|
|
3
|
-
queryAll,
|
|
4
|
-
queryAllFromArchive,
|
|
5
|
-
queryTurnsFromArchive,
|
|
6
|
-
queryUserTurns,
|
|
7
|
-
queryToolResultEvents,
|
|
8
|
-
} from '@relayburn/ledger';
|
|
9
|
-
import {
|
|
10
|
-
attributeOverhead,
|
|
11
|
-
buildCompareTable,
|
|
12
|
-
buildGhostSurfaceInputs,
|
|
13
|
-
buildTrimRecommendations,
|
|
14
|
-
compareFromArchive,
|
|
15
|
-
costForTurn,
|
|
16
|
-
DEFAULT_MIN_SAMPLE,
|
|
17
|
-
detectGhostSurface,
|
|
18
|
-
detectPatterns,
|
|
19
|
-
detectToolCallPatterns,
|
|
20
|
-
detectToolOutputBloat,
|
|
21
|
-
filterTurnsByProvider,
|
|
22
|
-
findingsFromPatterns,
|
|
23
|
-
findOverheadFiles,
|
|
24
|
-
ghostSurfaceToFinding,
|
|
25
|
-
hasMinimumFidelity,
|
|
26
|
-
loadClaudeSettings,
|
|
27
|
-
loadOverheadFile,
|
|
28
|
-
loadPricing,
|
|
29
|
-
projectClaudeSettingsPath,
|
|
30
|
-
renderUnifiedDiffForRecommendation,
|
|
31
|
-
summarizeFidelity,
|
|
32
|
-
sumCosts,
|
|
33
|
-
attributeHotspots,
|
|
34
|
-
toolCallPatternToFinding,
|
|
35
|
-
toolOutputBloatToFinding,
|
|
36
|
-
userClaudeSettingsPath,
|
|
37
|
-
} from '@relayburn/analyze';
|
|
38
|
-
import { ingestAll } from '@relayburn/ingest';
|
|
39
|
-
import { resolveProject } from '@relayburn/reader';
|
|
40
|
-
import { readFile } from 'node:fs/promises';
|
|
41
|
-
import * as path from 'node:path';
|
|
42
|
-
|
|
43
|
-
function withHome(home, fn) {
|
|
44
|
-
const prev = process.env.RELAYBURN_HOME;
|
|
45
|
-
if (home) process.env.RELAYBURN_HOME = home;
|
|
46
|
-
return Promise.resolve(fn()).finally(() => {
|
|
47
|
-
if (home) {
|
|
48
|
-
if (prev === undefined) delete process.env.RELAYBURN_HOME;
|
|
49
|
-
else process.env.RELAYBURN_HOME = prev;
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Bring the SQLite archive current and query against it, falling back to a
|
|
55
|
-
// full ledger walk if the archive can't be built or read. Mirrors the strategy
|
|
56
|
-
// the CLI's loadTurns() uses so SDK consumers (and the MCP server, which now
|
|
57
|
-
// calls through here) get the same hot-path performance without re-implementing
|
|
58
|
-
// the fallback logic in every caller. `onLog` lets callers surface the
|
|
59
|
-
// fallback reason; defaults to a no-op so library use stays quiet.
|
|
60
|
-
async function loadTurnsViaArchive(q, onLog) {
|
|
61
|
-
try {
|
|
62
|
-
await buildArchive();
|
|
63
|
-
return await queryAllFromArchive(q);
|
|
64
|
-
} catch (err) {
|
|
65
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
66
|
-
onLog?.(`archive query failed, falling back to ledger walk: ${msg}`);
|
|
67
|
-
return queryAll(q);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function loadSessionTurnsViaArchive(sessionId, onLog) {
|
|
72
|
-
try {
|
|
73
|
-
await buildArchive();
|
|
74
|
-
return await queryTurnsFromArchive({ sessionId });
|
|
75
|
-
} catch (err) {
|
|
76
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
77
|
-
onLog?.(`archive query failed, falling back to ledger walk: ${msg}`);
|
|
78
|
-
return queryAll({ sessionId });
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Accept either a CLI-style relative range (`24h`, `7d`, `4w`, `2m`) or an
|
|
83
|
-
// ISO timestamp and return an ISO string the ledger query can compare. The
|
|
84
|
-
// ledger filter does lexical string comparison on `turn.ts`, so passing a raw
|
|
85
|
-
// `7d` would silently filter every turn out (since `'7'` > `'2'` lexically).
|
|
86
|
-
// Lifted from `packages/cli/src/format.ts` so direct SDK callers (and future
|
|
87
|
-
// MCP tools) get the same forgiving input shape the CLI users see, without
|
|
88
|
-
// the silent-drop trap.
|
|
89
|
-
function normalizeSince(since) {
|
|
90
|
-
if (since === undefined) return undefined;
|
|
91
|
-
if (typeof since !== 'string' || since.length === 0) return undefined;
|
|
92
|
-
const m = /^(\d+)([hdwm])$/.exec(since);
|
|
93
|
-
if (!m) {
|
|
94
|
-
const d = new Date(since);
|
|
95
|
-
if (Number.isNaN(d.getTime())) {
|
|
96
|
-
throw new Error(`invalid since: ${since} (expected ISO timestamp or relative range like 7d)`);
|
|
97
|
-
}
|
|
98
|
-
return d.toISOString();
|
|
99
|
-
}
|
|
100
|
-
const n = parseInt(m[1], 10);
|
|
101
|
-
const unit = m[2];
|
|
102
|
-
const ms =
|
|
103
|
-
unit === 'h'
|
|
104
|
-
? n * 3600_000
|
|
105
|
-
: unit === 'd'
|
|
106
|
-
? n * 86400_000
|
|
107
|
-
: unit === 'w'
|
|
108
|
-
? n * 7 * 86400_000
|
|
109
|
-
: /* m */ n * 30 * 86400_000;
|
|
110
|
-
return new Date(Date.now() - ms).toISOString();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export class Ledger {
|
|
114
|
-
static async open(opts = {}) {
|
|
115
|
-
return new Ledger(opts.home);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
constructor(home) {
|
|
119
|
-
this.home = home;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export async function ingest(opts = {}) {
|
|
124
|
-
return withHome(opts.ledgerHome, async () => ingestAll());
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export async function summary(opts = {}) {
|
|
128
|
-
return withHome(opts.ledgerHome, async () => {
|
|
129
|
-
const q = { sessionId: opts.session, project: opts.project, since: normalizeSince(opts.since) };
|
|
130
|
-
const turns = await loadTurnsViaArchive(q, opts.onLog);
|
|
131
|
-
const pricing = await loadPricing();
|
|
132
|
-
const byTool = new Map();
|
|
133
|
-
const byModel = new Map();
|
|
134
|
-
let totalTokens = 0;
|
|
135
|
-
let totalCost = 0;
|
|
136
|
-
|
|
137
|
-
for (const t of turns) {
|
|
138
|
-
const c = costForTurn(t, pricing)?.total ?? 0;
|
|
139
|
-
const usage =
|
|
140
|
-
t.usage.input +
|
|
141
|
-
t.usage.output +
|
|
142
|
-
t.usage.reasoning +
|
|
143
|
-
t.usage.cacheRead +
|
|
144
|
-
t.usage.cacheCreate5m +
|
|
145
|
-
t.usage.cacheCreate1h;
|
|
146
|
-
totalTokens += usage;
|
|
147
|
-
totalCost += c;
|
|
148
|
-
|
|
149
|
-
const model = byModel.get(t.model) ?? { model: t.model, tokens: 0, cost: 0 };
|
|
150
|
-
model.tokens += usage;
|
|
151
|
-
model.cost += c;
|
|
152
|
-
byModel.set(t.model, model);
|
|
153
|
-
|
|
154
|
-
for (const call of t.toolCalls) {
|
|
155
|
-
const tool = byTool.get(call.name) ?? { tool: call.name, tokens: 0, cost: 0, count: 0 };
|
|
156
|
-
tool.tokens += usage;
|
|
157
|
-
tool.cost += c;
|
|
158
|
-
tool.count += 1;
|
|
159
|
-
byTool.set(call.name, tool);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
totalTokens,
|
|
165
|
-
totalCost,
|
|
166
|
-
turnCount: turns.length,
|
|
167
|
-
byTool: [...byTool.values()],
|
|
168
|
-
byModel: [...byModel.values()],
|
|
169
|
-
};
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Compact session-scoped cost summary. Same numbers as `summary({ session })`
|
|
174
|
-
// but shaped for callers that just want the headline: totalUSD, totalTokens,
|
|
175
|
-
// turnCount, distinct models. The MCP `burn__sessionCost` tool wraps this
|
|
176
|
-
// directly so the cost shape lives in one place. `note` is set when the
|
|
177
|
-
// session is empty or when no session id was provided so MCP clients can
|
|
178
|
-
// surface a human-readable reason without re-deriving it.
|
|
179
|
-
export async function sessionCost(opts = {}) {
|
|
180
|
-
return withHome(opts.ledgerHome, async () => {
|
|
181
|
-
const sessionId = opts.session;
|
|
182
|
-
if (!sessionId) {
|
|
183
|
-
return {
|
|
184
|
-
sessionId: null,
|
|
185
|
-
totalUSD: 0,
|
|
186
|
-
totalTokens: 0,
|
|
187
|
-
turnCount: 0,
|
|
188
|
-
models: [],
|
|
189
|
-
note: 'no session id provided',
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
const turns = await loadSessionTurnsViaArchive(sessionId, opts.onLog);
|
|
193
|
-
if (turns.length === 0) {
|
|
194
|
-
return {
|
|
195
|
-
sessionId,
|
|
196
|
-
totalUSD: 0,
|
|
197
|
-
totalTokens: 0,
|
|
198
|
-
turnCount: 0,
|
|
199
|
-
models: [],
|
|
200
|
-
note: 'no turns recorded for this session yet',
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
const pricing = await loadPricing();
|
|
204
|
-
const models = new Set();
|
|
205
|
-
let totalTokens = 0;
|
|
206
|
-
const costs = [];
|
|
207
|
-
for (const t of turns) {
|
|
208
|
-
models.add(t.model);
|
|
209
|
-
const u = t.usage;
|
|
210
|
-
totalTokens +=
|
|
211
|
-
(u.input ?? 0) +
|
|
212
|
-
(u.output ?? 0) +
|
|
213
|
-
(u.reasoning ?? 0) +
|
|
214
|
-
(u.cacheRead ?? 0) +
|
|
215
|
-
(u.cacheCreate5m ?? 0) +
|
|
216
|
-
(u.cacheCreate1h ?? 0);
|
|
217
|
-
const c = costForTurn(t, pricing);
|
|
218
|
-
if (c) costs.push(c);
|
|
219
|
-
}
|
|
220
|
-
const total = sumCosts(costs);
|
|
221
|
-
return {
|
|
222
|
-
sessionId,
|
|
223
|
-
totalUSD: Math.round(total.total * 1_000_000) / 1_000_000,
|
|
224
|
-
totalTokens,
|
|
225
|
-
turnCount: turns.length,
|
|
226
|
-
models: [...models].sort(),
|
|
227
|
-
};
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export async function hotspots(opts = {}) {
|
|
232
|
-
return withHome(opts.ledgerHome, async () => {
|
|
233
|
-
const turns = await queryAll({ sessionId: opts.session });
|
|
234
|
-
const userTurns = await queryUserTurns({ sessionId: opts.session });
|
|
235
|
-
const pricing = await loadPricing();
|
|
236
|
-
const userTurnsBySession = bucketBySession(userTurns);
|
|
237
|
-
const attribution = attributeHotspots(turns, { pricing, userTurnsBySession });
|
|
238
|
-
|
|
239
|
-
if (!opts.patterns || opts.patterns.length === 0) return attribution;
|
|
240
|
-
|
|
241
|
-
const wanted = new Set(opts.patterns);
|
|
242
|
-
const findings = [];
|
|
243
|
-
|
|
244
|
-
// Core patterns (retries, failures, edit-heavy, etc.) flow through
|
|
245
|
-
// detectPatterns + findingsFromPatterns; non-matching kinds are filtered.
|
|
246
|
-
const detected = detectPatterns(turns, { pricing, userTurnsBySession });
|
|
247
|
-
for (const f of findingsFromPatterns(detected)) {
|
|
248
|
-
if (wanted.has(f.kind)) findings.push(f);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Side-channel detectors live outside detectPatterns. Each one reads its
|
|
252
|
-
// own slice of state, so we run them lazily based on `wanted`.
|
|
253
|
-
|
|
254
|
-
if (wanted.has('tool-output-bloat')) {
|
|
255
|
-
const settings = [];
|
|
256
|
-
const userLoaded = await loadClaudeSettings(userClaudeSettingsPath());
|
|
257
|
-
if (userLoaded) settings.push(userLoaded);
|
|
258
|
-
const projectLoaded = await loadClaudeSettings(projectClaudeSettingsPath());
|
|
259
|
-
if (projectLoaded) settings.push(projectLoaded);
|
|
260
|
-
const toolResultEvents = await queryToolResultEvents({ sessionId: opts.session });
|
|
261
|
-
const bloats = detectToolOutputBloat({
|
|
262
|
-
settings,
|
|
263
|
-
toolResultEvents,
|
|
264
|
-
userTurns,
|
|
265
|
-
turns,
|
|
266
|
-
pricing,
|
|
267
|
-
});
|
|
268
|
-
for (const b of bloats) findings.push(toolOutputBloatToFinding(b));
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (wanted.has('ghost-surface')) {
|
|
272
|
-
const ghostInputs = await buildGhostSurfaceInputs(turns, pricing);
|
|
273
|
-
const ghosts = await detectGhostSurface(ghostInputs);
|
|
274
|
-
for (const g of ghosts) findings.push(ghostSurfaceToFinding(g));
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (wanted.has('tool-call-pattern')) {
|
|
278
|
-
const patterns = detectToolCallPatterns(turns, { pricing });
|
|
279
|
-
for (const p of patterns) findings.push(toolCallPatternToFinding(p));
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return findings;
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
function bucketBySession(userTurns) {
|
|
287
|
-
const out = new Map();
|
|
288
|
-
for (const ut of userTurns) {
|
|
289
|
-
const list = out.get(ut.sessionId);
|
|
290
|
-
if (list) list.push(ut);
|
|
291
|
-
else out.set(ut.sessionId, [ut]);
|
|
292
|
-
}
|
|
293
|
-
return out;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const VALID_OVERHEAD_KINDS = ['claude-md', 'agents-md'];
|
|
297
|
-
|
|
298
|
-
// Discover and parse overhead files for a project, returning the parsed files
|
|
299
|
-
// alongside the cost attribution (per-file and per-section). Shared by
|
|
300
|
-
// `overhead()` (report mode) and `overheadTrim()` (recommendations mode) so the
|
|
301
|
-
// discovery + ingest + query + attribution pipeline lives in one place.
|
|
302
|
-
async function gatherOverhead(opts = {}) {
|
|
303
|
-
const projectPath = opts.project ? path.resolve(opts.project) : process.cwd();
|
|
304
|
-
const kind = opts.kind;
|
|
305
|
-
if (kind !== undefined && !VALID_OVERHEAD_KINDS.includes(kind)) {
|
|
306
|
-
throw new Error(
|
|
307
|
-
`invalid overhead kind: ${JSON.stringify(kind)} (expected one of: ${VALID_OVERHEAD_KINDS.join(', ')})`,
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
let found = await findOverheadFiles(projectPath);
|
|
312
|
-
if (kind) found = found.filter((f) => f.kind === kind);
|
|
313
|
-
if (found.length === 0) {
|
|
314
|
-
return { projectPath, files: [], attribution: null };
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const files = [];
|
|
318
|
-
for (const f of found) files.push(await loadOverheadFile(f));
|
|
319
|
-
|
|
320
|
-
const resolved = resolveProject(projectPath);
|
|
321
|
-
const q = { project: resolved.projectKey ?? projectPath };
|
|
322
|
-
const normalizedSince = normalizeSince(opts.since);
|
|
323
|
-
if (normalizedSince) q.since = normalizedSince;
|
|
324
|
-
|
|
325
|
-
const turns = await loadTurnsViaArchive(q, opts.onLog);
|
|
326
|
-
const pricing = await loadPricing();
|
|
327
|
-
const attribution = attributeOverhead({ files, turns, pricing });
|
|
328
|
-
return { projectPath, files, attribution };
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
export async function overhead(opts = {}) {
|
|
332
|
-
return withHome(opts.ledgerHome, async () => {
|
|
333
|
-
const data = await gatherOverhead(opts);
|
|
334
|
-
if (!data.attribution) {
|
|
335
|
-
return { project: data.projectPath, files: [], perFile: [], grandTotal: 0 };
|
|
336
|
-
}
|
|
337
|
-
return {
|
|
338
|
-
project: data.projectPath,
|
|
339
|
-
files: data.files.map(({ file, parsed }) => ({
|
|
340
|
-
kind: file.kind,
|
|
341
|
-
path: file.path,
|
|
342
|
-
appliesTo: file.appliesTo,
|
|
343
|
-
totalLines: parsed.totalLines,
|
|
344
|
-
bytes: parsed.bytes,
|
|
345
|
-
tokens: parsed.tokens,
|
|
346
|
-
sections: parsed.sections,
|
|
347
|
-
groupingLevel: parsed.groupingLevel,
|
|
348
|
-
})),
|
|
349
|
-
perFile: data.attribution.perFile.map((p) => ({
|
|
350
|
-
path: p.file.path,
|
|
351
|
-
kind: p.file.kind,
|
|
352
|
-
appliesTo: p.file.appliesTo,
|
|
353
|
-
attribution: p.attribution,
|
|
354
|
-
})),
|
|
355
|
-
grandTotal: data.attribution.grandTotal,
|
|
356
|
-
};
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export async function overheadTrim(opts = {}) {
|
|
361
|
-
return withHome(opts.ledgerHome, async () => {
|
|
362
|
-
const data = await gatherOverhead(opts);
|
|
363
|
-
const topPerFile = parseTopN(opts.top);
|
|
364
|
-
const sinceLabel = opts.since ?? 'all time';
|
|
365
|
-
if (!data.attribution) {
|
|
366
|
-
return {
|
|
367
|
-
project: data.projectPath,
|
|
368
|
-
since: sinceLabel,
|
|
369
|
-
recommendations: [],
|
|
370
|
-
summary: {
|
|
371
|
-
filesAnalyzed: 0,
|
|
372
|
-
filesWithRecommendations: 0,
|
|
373
|
-
totalRecommendations: 0,
|
|
374
|
-
totalProjectedSavingsPerSession: 0,
|
|
375
|
-
totalProjectedSavingsAcrossWindow: 0,
|
|
376
|
-
},
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// The diff field is the unified-diff text the trim recommendation would
|
|
381
|
-
// produce — heavy enough to opt out of but useful enough that the CLI's
|
|
382
|
-
// --json mode always emits it. Keep that default; allow opts.includeDiff
|
|
383
|
-
// === false to skip the file reads when a caller (e.g. a future MCP tool)
|
|
384
|
-
// only wants the recommendation rows.
|
|
385
|
-
const includeDiff = opts.includeDiff !== false;
|
|
386
|
-
const textCache = new Map();
|
|
387
|
-
const recommendations = [];
|
|
388
|
-
let filesWithRecommendations = 0;
|
|
389
|
-
|
|
390
|
-
for (const fileAttr of data.attribution.perFile) {
|
|
391
|
-
const recs = buildTrimRecommendations(fileAttr.attribution, topPerFile);
|
|
392
|
-
if (recs.length === 0) continue;
|
|
393
|
-
filesWithRecommendations++;
|
|
394
|
-
let text;
|
|
395
|
-
if (includeDiff) {
|
|
396
|
-
text = textCache.get(fileAttr.file.path);
|
|
397
|
-
if (text === undefined) {
|
|
398
|
-
text = await readFile(fileAttr.file.path, 'utf8');
|
|
399
|
-
textCache.set(fileAttr.file.path, text);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
for (const rec of recs) {
|
|
403
|
-
const entry = {
|
|
404
|
-
file: toProjectRelativePath(fileAttr.file.path, data.projectPath),
|
|
405
|
-
kind: fileAttr.file.kind,
|
|
406
|
-
appliesTo: fileAttr.file.appliesTo,
|
|
407
|
-
section: {
|
|
408
|
-
heading: rec.section.heading,
|
|
409
|
-
startLine: rec.section.startLine,
|
|
410
|
-
endLine: rec.section.endLine,
|
|
411
|
-
tokens: rec.section.tokens,
|
|
412
|
-
},
|
|
413
|
-
projectedSavings: {
|
|
414
|
-
perSessionUsd: rec.projectedSavingsPerSession,
|
|
415
|
-
acrossWindowUsd: rec.projectedSavingsAcrossWindow,
|
|
416
|
-
tokens: rec.section.tokens,
|
|
417
|
-
tokenShare: rec.tokenShare,
|
|
418
|
-
},
|
|
419
|
-
};
|
|
420
|
-
if (includeDiff) {
|
|
421
|
-
entry.diff = renderUnifiedDiffForRecommendation(
|
|
422
|
-
fileAttr.file.path,
|
|
423
|
-
text,
|
|
424
|
-
rec,
|
|
425
|
-
data.projectPath,
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
recommendations.push(entry);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
return {
|
|
433
|
-
project: data.projectPath,
|
|
434
|
-
since: sinceLabel,
|
|
435
|
-
recommendations,
|
|
436
|
-
summary: {
|
|
437
|
-
filesAnalyzed: data.files.length,
|
|
438
|
-
filesWithRecommendations,
|
|
439
|
-
totalRecommendations: recommendations.length,
|
|
440
|
-
totalProjectedSavingsPerSession: recommendations.reduce(
|
|
441
|
-
(sum, r) => sum + r.projectedSavings.perSessionUsd,
|
|
442
|
-
0,
|
|
443
|
-
),
|
|
444
|
-
totalProjectedSavingsAcrossWindow: recommendations.reduce(
|
|
445
|
-
(sum, r) => sum + r.projectedSavings.acrossWindowUsd,
|
|
446
|
-
0,
|
|
447
|
-
),
|
|
448
|
-
},
|
|
449
|
-
};
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function parseTopN(v) {
|
|
454
|
-
if (typeof v !== 'number' || !Number.isFinite(v) || v <= 0) return 3;
|
|
455
|
-
return Math.floor(v);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function toProjectRelativePath(filePath, projectPath) {
|
|
459
|
-
const rel = path.relative(projectPath, filePath);
|
|
460
|
-
const display = rel && !rel.startsWith('..') ? rel : filePath;
|
|
461
|
-
return display.split(path.sep).join('/');
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const FIDELITY_CHOICES = ['full', 'usage-only', 'aggregate-only', 'cost-only', 'partial'];
|
|
465
|
-
|
|
466
|
-
// Per-(model, activity) comparison shape. Mirrors the archive-vs-ledger
|
|
467
|
-
// branching `runCompare` ships in the CLI: archive when nothing forces a
|
|
468
|
-
// per-turn walk (no fidelity gate, no provider filter), ledger walk
|
|
469
|
-
// otherwise. Returns the same JSON object the CLI's `--json` mode emits so
|
|
470
|
-
// the CLI becomes a thin presenter and a future `burn__compare` MCP tool
|
|
471
|
-
// can wrap this directly.
|
|
472
|
-
export async function compare(opts) {
|
|
473
|
-
if (!opts || !Array.isArray(opts.models) || opts.models.length < 2) {
|
|
474
|
-
throw new Error('compare: needs at least 2 models');
|
|
475
|
-
}
|
|
476
|
-
if (opts.minFidelity !== undefined && !FIDELITY_CHOICES.includes(opts.minFidelity)) {
|
|
477
|
-
throw new Error(
|
|
478
|
-
`compare: invalid minFidelity: ${opts.minFidelity} (expected one of ${FIDELITY_CHOICES.join(', ')})`,
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
return withHome(opts.ledgerHome, async () => {
|
|
482
|
-
const minFidelity = opts.minFidelity ?? 'usage-only';
|
|
483
|
-
const minSample = opts.minSample ?? DEFAULT_MIN_SAMPLE;
|
|
484
|
-
const providerFilter = normalizeProviderFilter(opts.provider);
|
|
485
|
-
|
|
486
|
-
const q = {};
|
|
487
|
-
const since = normalizeSince(opts.since);
|
|
488
|
-
if (since !== undefined) q.since = since;
|
|
489
|
-
if (opts.session !== undefined) q.sessionId = opts.session;
|
|
490
|
-
if (opts.project !== undefined) q.project = opts.project;
|
|
491
|
-
if (opts.workflow !== undefined || opts.agent !== undefined) {
|
|
492
|
-
q.enrichment = {};
|
|
493
|
-
if (opts.workflow !== undefined) q.enrichment.workflowId = opts.workflow;
|
|
494
|
-
if (opts.agent !== undefined) q.enrichment.agentId = opts.agent;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const pricing = await loadPricing();
|
|
498
|
-
const tableOpts = { pricing, minSample, models: opts.models };
|
|
499
|
-
|
|
500
|
-
// `RELAYBURN_ARCHIVE=0` (also `false`/`no`) is the documented escape
|
|
501
|
-
// hatch from the archive path — used by `burn compare --no-archive` for
|
|
502
|
-
// parity/debug workflows. Honor it before deciding whether to query the
|
|
503
|
-
// archive at all so the CLI flag actually forces the ledger walk even
|
|
504
|
-
// when the archive on disk is healthy.
|
|
505
|
-
const archiveEnabled = !envDisablesArchive();
|
|
506
|
-
|
|
507
|
-
// Archive path is additionally restricted to slices where nothing forces
|
|
508
|
-
// a per-turn walk: no fidelity gate (`partial` lets everything through)
|
|
509
|
-
// and no provider filter (provider is derived per turn from (model,
|
|
510
|
-
// source) at query time and the archive's grouped SQL doesn't expose
|
|
511
|
-
// that classifier).
|
|
512
|
-
const useArchive = archiveEnabled && minFidelity === 'partial' && !providerFilter;
|
|
513
|
-
|
|
514
|
-
let table;
|
|
515
|
-
let analyzedTurns;
|
|
516
|
-
let summary;
|
|
517
|
-
if (useArchive) {
|
|
518
|
-
try {
|
|
519
|
-
await buildArchive();
|
|
520
|
-
const archived = await compareFromArchive(q, tableOpts);
|
|
521
|
-
table = archived.table;
|
|
522
|
-
// For the fidelity-permissive mode we still emit a zero-excluded
|
|
523
|
-
// summary so the JSON schema stays stable. summarizeFidelity needs
|
|
524
|
-
// turn rows; pull them via the same archive-aware loader.
|
|
525
|
-
const turnsForSummary = await loadTurnsViaArchive(q, opts.onLog);
|
|
526
|
-
summary = summarizeFidelity(turnsForSummary);
|
|
527
|
-
analyzedTurns = turnsForSummary.length;
|
|
528
|
-
return shapeCompareResult(table, analyzedTurns, minFidelity, summary);
|
|
529
|
-
} catch (err) {
|
|
530
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
531
|
-
opts.onLog?.(`archive compare failed, falling back to ledger walk: ${msg}`);
|
|
532
|
-
// Fall through to ledger path.
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
// Ledger-walk path. When the archive is disabled we go straight to
|
|
537
|
-
// `queryAll` (no `buildArchive` side effect); otherwise the
|
|
538
|
-
// archive-aware loader still wins on the hot path even when the gate
|
|
539
|
-
// forces post-load filtering.
|
|
540
|
-
const queriedTurns = archiveEnabled
|
|
541
|
-
? await loadTurnsViaArchive(q, opts.onLog)
|
|
542
|
-
: await queryAll(q);
|
|
543
|
-
const turns = providerFilter ? filterTurnsByProvider(queriedTurns, providerFilter) : queriedTurns;
|
|
544
|
-
summary = summarizeFidelity(turns);
|
|
545
|
-
const filteredTurns = minFidelity === 'partial'
|
|
546
|
-
? turns
|
|
547
|
-
: turns.filter((t) => hasMinimumFidelity(t.fidelity, minFidelity));
|
|
548
|
-
table = buildCompareTable(filteredTurns, tableOpts);
|
|
549
|
-
analyzedTurns = filteredTurns.length;
|
|
550
|
-
return shapeCompareResult(table, analyzedTurns, minFidelity, summary);
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
function envDisablesArchive() {
|
|
555
|
-
const v = process.env.RELAYBURN_ARCHIVE;
|
|
556
|
-
return v === '0' || v === 'false' || v === 'no';
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
function normalizeProviderFilter(provider) {
|
|
560
|
-
if (!provider) return undefined;
|
|
561
|
-
if (!Array.isArray(provider)) {
|
|
562
|
-
throw new Error('compare: provider must be an array of strings');
|
|
563
|
-
}
|
|
564
|
-
const normalized = provider
|
|
565
|
-
.map((p) => (typeof p === 'string' ? p.trim().toLowerCase() : ''))
|
|
566
|
-
.filter(Boolean);
|
|
567
|
-
if (normalized.length === 0) return undefined;
|
|
568
|
-
return new Set(normalized);
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
// Sum the byClass buckets that fall below the minimum fidelity. We never
|
|
572
|
-
// exclude `unknown` (records without a fidelity field — `hasMinimumFidelity`
|
|
573
|
-
// passes them for backward compat), so they don't get counted here.
|
|
574
|
-
// `partial` is the "include everything" escape hatch; it always reports zero
|
|
575
|
-
// excluded.
|
|
576
|
-
export function computeCompareExcluded(summary, minimum) {
|
|
577
|
-
const out = { total: 0, aggregateOnly: 0, costOnly: 0, partial: 0, usageOnly: 0 };
|
|
578
|
-
if (minimum === 'partial') return out;
|
|
579
|
-
const order = ['cost-only', 'aggregate-only', 'partial', 'usage-only', 'full'];
|
|
580
|
-
const need = order.indexOf(minimum);
|
|
581
|
-
for (const cls of order) {
|
|
582
|
-
if (order.indexOf(cls) >= need) continue;
|
|
583
|
-
const n = summary.byClass[cls];
|
|
584
|
-
if (!n) continue;
|
|
585
|
-
out.total += n;
|
|
586
|
-
if (cls === 'aggregate-only') out.aggregateOnly += n;
|
|
587
|
-
else if (cls === 'cost-only') out.costOnly += n;
|
|
588
|
-
else if (cls === 'partial') out.partial += n;
|
|
589
|
-
else if (cls === 'usage-only') out.usageOnly += n;
|
|
590
|
-
}
|
|
591
|
-
return out;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
function shapeCompareResult(table, analyzedTurns, minimum, summary) {
|
|
595
|
-
const excluded = computeCompareExcluded(summary, minimum);
|
|
596
|
-
const cells = [];
|
|
597
|
-
for (const m of table.models) {
|
|
598
|
-
for (const cat of table.categories) {
|
|
599
|
-
const c = table.cells[m][cat];
|
|
600
|
-
cells.push({
|
|
601
|
-
model: m,
|
|
602
|
-
category: cat,
|
|
603
|
-
turns: c.turns,
|
|
604
|
-
editTurns: c.editTurns,
|
|
605
|
-
oneShotTurns: c.oneShotTurns,
|
|
606
|
-
pricedTurns: c.pricedTurns,
|
|
607
|
-
totalCost: round(c.totalCost, 6),
|
|
608
|
-
costPerTurn: c.costPerTurn !== null ? round(c.costPerTurn, 6) : null,
|
|
609
|
-
oneShotRate: c.oneShotRate !== null ? round(c.oneShotRate, 4) : null,
|
|
610
|
-
cacheHitRate: c.cacheHitRate !== null ? round(c.cacheHitRate, 4) : null,
|
|
611
|
-
medianRetries: c.medianRetries,
|
|
612
|
-
noData: c.noData,
|
|
613
|
-
insufficientSample: c.insufficientSample,
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return {
|
|
618
|
-
analyzedTurns,
|
|
619
|
-
minSample: table.minSample,
|
|
620
|
-
models: table.models,
|
|
621
|
-
categories: table.categories,
|
|
622
|
-
totals: table.totals,
|
|
623
|
-
cells,
|
|
624
|
-
fidelity: { minimum, excluded, summary },
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function round(n, digits) {
|
|
629
|
-
return Number(n.toFixed(digits));
|
|
630
|
-
}
|