@relayburn/sdk 1.7.0 → 1.9.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 CHANGED
@@ -4,12 +4,28 @@ All notable changes to `@relayburn/sdk`.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.9.0] - 2026-05-03
8
+
9
+ ### Added
10
+
11
+ - `compare({ models, … })` returns the per-(model, activity) `CompareResult` shape (`analyzedTurns`, `models`, `categories`, `totals`, flat `cells[]`, `fidelity { minimum, excluded, summary }`) — the JSON object `burn compare --json` now emits. Mirrors the CLI's archive-vs-ledger branching: archive when `minFidelity === 'partial'` and no provider filter, ledger walk otherwise. Falls back transparently to the ledger walk when the archive read fails.
12
+ - `sessionCost({ session })` returns the compact per-session cost shape (`totalUSD`, `totalTokens`, `turnCount`, `models`) the MCP `burn__sessionCost` tool now wraps directly.
13
+ - `summary()` result now includes `turnCount`.
14
+ - `summary()` and `sessionCost()` read through the SQLite archive by default with transparent fallback to the JSONL ledger walk on archive failure. Pass `onLog` to capture the fallback reason.
15
+ - `overhead({ project, since?, kind? })` returns per-file + per-section overhead cost attribution (the JSON shape `burn overhead --json` now consumes).
16
+ - `overheadTrim({ project, since?, kind?, top?, includeDiff? })` returns trim recommendations with projected savings and (by default) embedded unified diffs (the JSON shape `burn overhead trim --json` now consumes). Pass `includeDiff: false` to skip the per-file disk reads.
17
+ - `summary({ since })` and `overhead({ since })` / `overheadTrim({ since })` now accept either an ISO timestamp or a relative range (`24h`, `7d`, `4w`, `2m`); the SDK normalizes both forms before querying the ledger so direct SDK callers get the same forgiving input shape CLI users have. Previously a raw relative string would silently filter out every turn.
18
+
7
19
  ## [1.7.0] - 2026-05-02
8
20
 
9
21
  ### Added
10
22
 
11
23
  - `hotspots({ patterns })` now also surfaces `tool-output-bloat`, `ghost-surface`, and `tool-call-pattern` findings (previously only the core `detectPatterns` set). Each side-channel detector loads its own inputs (Claude settings, tool-result events, on-disk surface) lazily based on the requested patterns.
12
24
 
25
+ ### Changed
26
+
27
+ - SDK no longer depends on `@relayburn/cli`. `ingest()` now imports from the new `@relayburn/ingest` package, and `buildGhostSurfaceInputs` lives in `@relayburn/analyze`. The SDK's public surface is unchanged.
28
+
13
29
  ## [1.5.0] - 2026-05-01
14
30
 
15
31
  ### Added
package/README.md CHANGED
@@ -1,12 +1,43 @@
1
1
  # @relayburn/sdk
2
2
 
3
- Embeddable Relayburn SDK for in-process ingestion and analysis.
3
+ Embeddable Relayburn SDK for in-process ingestion and analysis. This package is the **source of truth** for the in-process query/compute surface — `@relayburn/mcp` and `@relayburn/cli` consume the SDK rather than duplicating its logic.
4
4
 
5
5
  ```ts
6
- import { Ledger, ingest, summary, hotspots } from '@relayburn/sdk';
6
+ import {
7
+ Ledger,
8
+ ingest,
9
+ summary,
10
+ sessionCost,
11
+ compare,
12
+ overhead,
13
+ overheadTrim,
14
+ hotspots,
15
+ } from '@relayburn/sdk';
7
16
 
8
17
  await Ledger.open({ home: '/tmp/relayburn-home' });
9
18
  await ingest({ ledgerHome: '/tmp/relayburn-home' });
19
+
20
+ // Slice-wide rollup: turnCount + per-model + per-tool aggregates.
10
21
  const stats = await summary({ session: 'session-id', ledgerHome: '/tmp/relayburn-home' });
22
+
23
+ // Compact session-scoped cost shape (totalUSD/totalTokens/turnCount/models).
24
+ // Powers the MCP `burn__sessionCost` tool.
25
+ const cost = await sessionCost({ session: 'session-id' });
26
+
27
+ // Per-(model, activity) comparison shape — the JSON object `burn compare --json` emits.
28
+ const cmp = await compare({
29
+ models: ['claude-sonnet-4-6', 'claude-haiku-4-5'],
30
+ since: '30d',
31
+ minFidelity: 'usage-only',
32
+ });
33
+
34
+ // Overhead-file (CLAUDE.md / AGENTS.md / .claude/CLAUDE.md) cost attribution.
35
+ const oh = await overhead({ project: '/path/to/repo', since: '30d' });
36
+ const trim = await overheadTrim({ project: '/path/to/repo', top: 3 });
37
+
11
38
  const findings = await hotspots({ session: 'session-id', patterns: ['retry-loop'] });
12
39
  ```
40
+
41
+ `summary`, `sessionCost`, `compare`, `overhead`, and `overheadTrim` read through the SQLite archive when available, transparently falling back to the JSONL ledger walk if the archive can't be opened. Pass `onLog` to surface fallback messages in your host's log channel.
42
+
43
+ `overheadTrim` includes a unified-diff string per recommendation by default (matches `burn overhead trim --json`); pass `includeDiff: false` to skip the per-file disk reads when you only need the recommendation rows.
package/index.d.ts CHANGED
@@ -4,8 +4,141 @@ export declare class Ledger { static open(opts?: LedgerOpenOptions): Promise<Led
4
4
  export interface IngestOptions { sessionId?: string; harness?: 'claude-code'|'codex'|'opencode'; ledgerHome?: string }
5
5
  export declare function ingest(opts?: IngestOptions): Promise<unknown>
6
6
 
7
- export interface SummaryOptions { session?: string; project?: string; since?: string; ledgerHome?: string }
8
- export declare function summary(opts?: SummaryOptions): Promise<{ totalTokens: number; totalCost: number; byTool: Array<{tool:string;tokens:number;cost:number;count:number}>; byModel: Array<{model:string;tokens:number;cost:number}> }>
7
+ export interface SummaryOptions {
8
+ session?: string;
9
+ project?: string;
10
+ /** ISO timestamp (e.g. `2026-04-01T00:00:00Z`) or relative range (`24h`, `7d`, `4w`, `2m`). */
11
+ since?: string;
12
+ ledgerHome?: string;
13
+ /** Optional logger invoked when the SQLite archive read fails and the SDK falls back to a full ledger walk. */
14
+ onLog?: (msg: string) => void;
15
+ }
16
+ export declare function summary(opts?: SummaryOptions): Promise<{
17
+ totalTokens: number;
18
+ totalCost: number;
19
+ turnCount: number;
20
+ byTool: Array<{ tool: string; tokens: number; cost: number; count: number }>;
21
+ byModel: Array<{ model: string; tokens: number; cost: number }>;
22
+ }>
23
+
24
+ export interface SessionCostOptions {
25
+ /** Session id to total. Omit for `{ note: 'no session id provided' }`. */
26
+ session?: string;
27
+ ledgerHome?: string;
28
+ onLog?: (msg: string) => void;
29
+ }
30
+ export interface SessionCostResult {
31
+ sessionId: string | null;
32
+ totalUSD: number;
33
+ totalTokens: number;
34
+ turnCount: number;
35
+ models: string[];
36
+ note?: string;
37
+ }
38
+ /** Compact session-scoped cost shape; powers the MCP `burn__sessionCost` tool. */
39
+ export declare function sessionCost(opts?: SessionCostOptions): Promise<SessionCostResult>
40
+
41
+ export type OverheadFileKind = 'claude-md' | 'agents-md';
42
+ export type OverheadHarness = 'claude-code' | 'codex' | 'opencode';
43
+
44
+ export interface OverheadOptions {
45
+ /** Project path to inspect; defaults to process.cwd(). */
46
+ project?: string;
47
+ /** ISO timestamp or relative range (`24h`, `7d`, `4w`, `2m`); the SDK normalizes both forms before querying. */
48
+ since?: string;
49
+ /** Narrow to a single overhead file kind. */
50
+ kind?: OverheadFileKind;
51
+ ledgerHome?: string;
52
+ onLog?: (msg: string) => void;
53
+ }
54
+
55
+ export interface OverheadSection {
56
+ heading: string;
57
+ startLine: number;
58
+ endLine: number;
59
+ tokens: number;
60
+ }
61
+
62
+ export interface OverheadSectionCost {
63
+ filePath: string;
64
+ section: OverheadSection;
65
+ tokenShare: number;
66
+ costPerSession: number;
67
+ totalCost: number;
68
+ }
69
+
70
+ export interface OverheadAttributionDetail {
71
+ sessionCount: number;
72
+ perSessionAvg: number;
73
+ perSessionP95: number;
74
+ totalCost: number;
75
+ sectionCosts: OverheadSectionCost[];
76
+ }
77
+
78
+ export interface OverheadFileSummary {
79
+ kind: OverheadFileKind;
80
+ path: string;
81
+ appliesTo: OverheadHarness[];
82
+ totalLines: number;
83
+ bytes: number;
84
+ tokens: number;
85
+ sections: OverheadSection[];
86
+ groupingLevel: number;
87
+ }
88
+
89
+ export interface OverheadPerFileEntry {
90
+ path: string;
91
+ kind: OverheadFileKind;
92
+ appliesTo: OverheadHarness[];
93
+ attribution: OverheadAttributionDetail;
94
+ }
95
+
96
+ export interface OverheadResult {
97
+ project: string;
98
+ files: OverheadFileSummary[];
99
+ perFile: OverheadPerFileEntry[];
100
+ grandTotal: number;
101
+ }
102
+
103
+ /** Per-file + per-section overhead cost attribution. Powers `burn overhead`. */
104
+ export declare function overhead(opts?: OverheadOptions): Promise<OverheadResult>
105
+
106
+ export interface OverheadTrimOptions extends OverheadOptions {
107
+ /** Recommendations per file. Default 3. */
108
+ top?: number;
109
+ /** Include the unified-diff text per recommendation (requires a file read per recommended file). Default true; pass false to skip. */
110
+ includeDiff?: boolean;
111
+ }
112
+
113
+ export interface OverheadTrimRecommendation {
114
+ file: string;
115
+ kind: OverheadFileKind;
116
+ appliesTo: OverheadHarness[];
117
+ section: { heading: string; startLine: number; endLine: number; tokens: number };
118
+ projectedSavings: {
119
+ perSessionUsd: number;
120
+ acrossWindowUsd: number;
121
+ tokens: number;
122
+ tokenShare: number;
123
+ };
124
+ diff?: string;
125
+ }
126
+
127
+ export interface OverheadTrimResult {
128
+ project: string;
129
+ since: string;
130
+ recommendations: OverheadTrimRecommendation[];
131
+ summary: {
132
+ filesAnalyzed: number;
133
+ filesWithRecommendations: number;
134
+ totalRecommendations: number;
135
+ totalProjectedSavingsPerSession: number;
136
+ totalProjectedSavingsAcrossWindow: number;
137
+ };
138
+ }
139
+
140
+ /** Trim recommendations for high-cost overhead-file sections. Powers `burn overhead trim`. */
141
+ export declare function overheadTrim(opts?: OverheadTrimOptions): Promise<OverheadTrimResult>
9
142
 
10
143
  export interface HotspotsOptions {
11
144
  session?: string;
@@ -23,3 +156,79 @@ export interface HotspotsOptions {
23
156
  ledgerHome?: string;
24
157
  }
25
158
  export declare function hotspots(opts?: HotspotsOptions): Promise<unknown>
159
+
160
+ export type FidelityClass = 'full' | 'usage-only' | 'aggregate-only' | 'cost-only' | 'partial';
161
+
162
+ export interface FidelitySummaryShape {
163
+ total: number;
164
+ byClass: Record<FidelityClass, number>;
165
+ unknown: number;
166
+ missingCoverage: Record<string, number>;
167
+ }
168
+
169
+ export interface CompareExcludedBreakdown {
170
+ total: number;
171
+ aggregateOnly: number;
172
+ costOnly: number;
173
+ partial: number;
174
+ usageOnly: number;
175
+ }
176
+
177
+ export interface CompareCellResult {
178
+ model: string;
179
+ category: string;
180
+ turns: number;
181
+ editTurns: number;
182
+ oneShotTurns: number;
183
+ pricedTurns: number;
184
+ totalCost: number;
185
+ costPerTurn: number | null;
186
+ oneShotRate: number | null;
187
+ cacheHitRate: number | null;
188
+ medianRetries: number | null;
189
+ noData: boolean;
190
+ insufficientSample: boolean;
191
+ }
192
+
193
+ export interface CompareOptions {
194
+ /** Required: ≥2 model names to compare. */
195
+ models: string[];
196
+ session?: string;
197
+ project?: string;
198
+ /** ISO timestamp (e.g. `2026-04-01T00:00:00Z`) or relative range (`24h`, `7d`, `4w`, `2m`). */
199
+ since?: string;
200
+ workflow?: string;
201
+ agent?: string;
202
+ /** Resolved provider filter (e.g. `['anthropic', 'synthetic']`). */
203
+ provider?: string[];
204
+ /** Insufficient-sample threshold; cells below this get flagged. Default 5. */
205
+ minSample?: number;
206
+ /** Minimum fidelity class to include in the aggregate. Default `'usage-only'`. */
207
+ minFidelity?: FidelityClass;
208
+ ledgerHome?: string;
209
+ onLog?: (msg: string) => void;
210
+ }
211
+
212
+ export interface CompareResult {
213
+ analyzedTurns: number;
214
+ minSample: number;
215
+ models: string[];
216
+ categories: string[];
217
+ totals: Record<string, { turns: number; totalCost: number }>;
218
+ cells: CompareCellResult[];
219
+ fidelity: {
220
+ minimum: FidelityClass;
221
+ excluded: CompareExcludedBreakdown;
222
+ summary: FidelitySummaryShape;
223
+ };
224
+ }
225
+
226
+ /**
227
+ * Per-(model, activity) comparison shape. Powers `burn compare` and the
228
+ * future `burn__compare` MCP tool. Reads through the SQLite archive when
229
+ * `minFidelity === 'partial'` and no provider filter is set; otherwise
230
+ * walks the ledger so the fidelity gate / provider filter can be applied
231
+ * per-turn. Falls back transparently to the ledger walk when the archive
232
+ * read fails.
233
+ */
234
+ export declare function compare(opts: CompareOptions): Promise<CompareResult>
package/index.js CHANGED
@@ -1,21 +1,44 @@
1
- import { queryAll, queryUserTurns, queryToolResultEvents } from '@relayburn/ledger';
2
1
  import {
3
- loadPricing,
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,
4
15
  costForTurn,
5
- attributeHotspots,
16
+ DEFAULT_MIN_SAMPLE,
17
+ detectGhostSurface,
6
18
  detectPatterns,
7
- findingsFromPatterns,
19
+ detectToolCallPatterns,
8
20
  detectToolOutputBloat,
9
- toolOutputBloatToFinding,
10
- detectGhostSurface,
21
+ filterTurnsByProvider,
22
+ findingsFromPatterns,
23
+ findOverheadFiles,
11
24
  ghostSurfaceToFinding,
12
- detectToolCallPatterns,
13
- toolCallPatternToFinding,
25
+ hasMinimumFidelity,
14
26
  loadClaudeSettings,
15
- userClaudeSettingsPath,
27
+ loadOverheadFile,
28
+ loadPricing,
16
29
  projectClaudeSettingsPath,
30
+ renderUnifiedDiffForRecommendation,
31
+ summarizeFidelity,
32
+ sumCosts,
33
+ attributeHotspots,
34
+ toolCallPatternToFinding,
35
+ toolOutputBloatToFinding,
36
+ userClaudeSettingsPath,
17
37
  } from '@relayburn/analyze';
18
- import { ingestAll, buildGhostSurfaceInputs } from '@relayburn/cli';
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';
19
42
 
20
43
  function withHome(home, fn) {
21
44
  const prev = process.env.RELAYBURN_HOME;
@@ -28,6 +51,65 @@ function withHome(home, fn) {
28
51
  });
29
52
  }
30
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
+
31
113
  export class Ledger {
32
114
  static async open(opts = {}) {
33
115
  return new Ledger(opts.home);
@@ -44,8 +126,8 @@ export async function ingest(opts = {}) {
44
126
 
45
127
  export async function summary(opts = {}) {
46
128
  return withHome(opts.ledgerHome, async () => {
47
- const q = { sessionId: opts.session, project: opts.project, since: opts.since };
48
- const turns = await queryAll(q);
129
+ const q = { sessionId: opts.session, project: opts.project, since: normalizeSince(opts.since) };
130
+ const turns = await loadTurnsViaArchive(q, opts.onLog);
49
131
  const pricing = await loadPricing();
50
132
  const byTool = new Map();
51
133
  const byModel = new Map();
@@ -78,7 +160,71 @@ export async function summary(opts = {}) {
78
160
  }
79
161
  }
80
162
 
81
- return { totalTokens, totalCost, byTool: [...byTool.values()], byModel: [...byModel.values()] };
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
+ };
82
228
  });
83
229
  }
84
230
 
@@ -146,3 +292,339 @@ function bucketBySession(userTurns) {
146
292
  }
147
293
  return out;
148
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@relayburn/sdk",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Embeddable Relayburn SDK for in-process ingest, summary, and hotspots queries",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -16,9 +16,10 @@
16
16
  "node": ">=22"
17
17
  },
18
18
  "dependencies": {
19
- "@relayburn/analyze": "1.7.0",
20
- "@relayburn/cli": "1.7.0",
21
- "@relayburn/ledger": "1.7.0"
19
+ "@relayburn/analyze": "1.9.0",
20
+ "@relayburn/ingest": "1.9.0",
21
+ "@relayburn/ledger": "1.9.0",
22
+ "@relayburn/reader": "1.9.0"
22
23
  },
23
24
  "repository": {
24
25
  "type": "git",