@trazum/core 1.39.0 → 1.41.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/README.md +28 -0
- package/dist/connector.d.ts +227 -0
- package/dist/connector.d.ts.map +1 -0
- package/dist/connector.js +431 -0
- package/dist/connector.js.map +1 -0
- package/dist/history.d.ts +110 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +191 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/connector.ts +616 -0
- package/src/history.ts +284 -0
- package/src/index.ts +20 -0
package/src/history.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The long run: many reports over many periods, as one series.
|
|
3
|
+
*
|
|
4
|
+
* Every comparison in Trazum is between two logs, and a product's cost
|
|
5
|
+
* problem is rarely visible in two — it is visible in twenty. This module
|
|
6
|
+
* takes *stored reports* (the `--json` documents a team already keeps) and
|
|
7
|
+
* builds the series no pairwise comparison can see: the workload that grew a
|
|
8
|
+
* little every week, the model share that has been climbing since a date,
|
|
9
|
+
* the cache hit rate decaying slowly enough that no single week's report
|
|
10
|
+
* called it a finding.
|
|
11
|
+
*
|
|
12
|
+
* **Still no forecasts.** Twenty points make a trend visible; they do not
|
|
13
|
+
* make next month knowable. The series is stated, the shape is named as
|
|
14
|
+
* consecutive movement — never a line fitted through the points — and where
|
|
15
|
+
* it goes next remains the reader's to judge, the same refusal
|
|
16
|
+
* `modelMixDrift` has carried since 1.27.
|
|
17
|
+
*
|
|
18
|
+
* **Derived from stored reports, not re-parsed logs**, so a year of `--json`
|
|
19
|
+
* output is enough and the raw logs can be thrown away — which is what the
|
|
20
|
+
* privacy story requires anyway. Browser-safe: documents in, series out.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { UNLABELLED } from './usage.js';
|
|
24
|
+
import type { PlanActionKind, PlanDocument } from './plan.js';
|
|
25
|
+
|
|
26
|
+
/** The slice of a stored profile document this module actually reads. */
|
|
27
|
+
export interface StoredReport {
|
|
28
|
+
/** Where it came from — a file name, shown so a finding can be traced. */
|
|
29
|
+
name: string;
|
|
30
|
+
span: { fromMs: number; toMs: number } | null;
|
|
31
|
+
totalUsd: number;
|
|
32
|
+
calls: number;
|
|
33
|
+
/** Label → dollars this period. */
|
|
34
|
+
byLabel: Map<string, number>;
|
|
35
|
+
/** Model → dollars this period. */
|
|
36
|
+
byModel: Map<string, number>;
|
|
37
|
+
/** Share of input tokens served from cache, or null when unknowable. */
|
|
38
|
+
cacheReadShare: number | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A run of consecutive movement, named — never extrapolated.
|
|
43
|
+
*
|
|
44
|
+
* `periods` counts the *rises* (or falls), so a run of 3 spans 4 reports.
|
|
45
|
+
* The floor is 3: two rises is what `--against` already shows, and one is
|
|
46
|
+
* noise wearing a trend's clothes.
|
|
47
|
+
*/
|
|
48
|
+
export interface HistoryRun {
|
|
49
|
+
kind: 'label-spend-climbing' | 'model-share-climbing' | 'cache-share-decaying';
|
|
50
|
+
subject: string;
|
|
51
|
+
/** Consecutive rises (falls, for decay). */
|
|
52
|
+
periods: number;
|
|
53
|
+
/** The report the run started in, by name — "climbing since <this one>". */
|
|
54
|
+
sinceName: string;
|
|
55
|
+
/** First and last values of the run, so the reader judges the size. */
|
|
56
|
+
from: number;
|
|
57
|
+
to: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The same action planned again and again: a decision nobody is executing. */
|
|
61
|
+
export interface RepeatedPlanAction {
|
|
62
|
+
kind: PlanActionKind;
|
|
63
|
+
label: string;
|
|
64
|
+
model: string;
|
|
65
|
+
appearances: number;
|
|
66
|
+
firstPlanned: string | null;
|
|
67
|
+
lastPlanned: string | null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface HistoryDocument {
|
|
71
|
+
schemaVersion: 1;
|
|
72
|
+
/** Ordered oldest first by span start. */
|
|
73
|
+
periods: { name: string; fromMs: number; toMs: number; totalUsd: number; calls: number }[];
|
|
74
|
+
/** Per label, dollars per period — null where the label had no traffic. */
|
|
75
|
+
labelSeries: { label: string; points: (number | null)[] }[];
|
|
76
|
+
/** Per model, share of that period's total — null where absent. */
|
|
77
|
+
modelShareSeries: { model: string; points: (number | null)[] }[];
|
|
78
|
+
/** Cache read share per period, null where unknowable. */
|
|
79
|
+
cacheShareSeries: (number | null)[];
|
|
80
|
+
/** The findings only a series can make. Shapes, never forecasts. */
|
|
81
|
+
runs: HistoryRun[];
|
|
82
|
+
/** Plans in the same directory, held against each other. */
|
|
83
|
+
repeatedPlanActions: RepeatedPlanAction[];
|
|
84
|
+
/**
|
|
85
|
+
* Reports that carry no span cannot be placed on a timeline; they are
|
|
86
|
+
* named here and in no series above, never silently absorbed.
|
|
87
|
+
*/
|
|
88
|
+
undatedReports: string[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const MIN_RUN = 3;
|
|
92
|
+
|
|
93
|
+
/** The longest run of strictly consecutive movement ending anywhere in the series. */
|
|
94
|
+
function longestRun(
|
|
95
|
+
points: (number | null)[],
|
|
96
|
+
direction: 1 | -1,
|
|
97
|
+
): { start: number; length: number } | null {
|
|
98
|
+
let best: { start: number; length: number } | null = null;
|
|
99
|
+
let start = -1;
|
|
100
|
+
let length = 0;
|
|
101
|
+
for (let i = 1; i < points.length; i++) {
|
|
102
|
+
const prev = points[i - 1] ?? null;
|
|
103
|
+
const here = points[i] ?? null;
|
|
104
|
+
if (prev !== null && here !== null && Math.sign(here - prev) === direction && here !== prev) {
|
|
105
|
+
if (length === 0) start = i - 1;
|
|
106
|
+
length += 1;
|
|
107
|
+
if (best === null || length > best.length) best = { start, length };
|
|
108
|
+
} else {
|
|
109
|
+
length = 0;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return best !== null && best.length >= MIN_RUN ? best : null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function buildHistory(
|
|
116
|
+
reports: StoredReport[],
|
|
117
|
+
plans: (PlanDocument & { createdAt?: string; name?: string })[] = [],
|
|
118
|
+
): HistoryDocument {
|
|
119
|
+
const undatedReports = reports.filter((r) => r.span === null).map((r) => r.name);
|
|
120
|
+
const dated = reports
|
|
121
|
+
.filter((r) => r.span !== null)
|
|
122
|
+
.sort((a, b) => a.span!.fromMs - b.span!.fromMs);
|
|
123
|
+
|
|
124
|
+
const periods = dated.map((r) => ({
|
|
125
|
+
name: r.name,
|
|
126
|
+
fromMs: r.span!.fromMs,
|
|
127
|
+
toMs: r.span!.toMs,
|
|
128
|
+
totalUsd: r.totalUsd,
|
|
129
|
+
calls: r.calls,
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
const labels = [...new Set(dated.flatMap((r) => [...r.byLabel.keys()]))].sort();
|
|
133
|
+
const labelSeries = labels.map((label) => ({
|
|
134
|
+
label,
|
|
135
|
+
points: dated.map((r) => r.byLabel.get(label) ?? null),
|
|
136
|
+
}));
|
|
137
|
+
|
|
138
|
+
const models = [...new Set(dated.flatMap((r) => [...r.byModel.keys()]))].sort();
|
|
139
|
+
const modelShareSeries = models.map((model) => ({
|
|
140
|
+
model,
|
|
141
|
+
points: dated.map((r) => {
|
|
142
|
+
const usd = r.byModel.get(model);
|
|
143
|
+
if (usd === undefined || r.totalUsd <= 0) return null;
|
|
144
|
+
return usd / r.totalUsd;
|
|
145
|
+
}),
|
|
146
|
+
}));
|
|
147
|
+
|
|
148
|
+
const cacheShareSeries = dated.map((r) => r.cacheReadShare);
|
|
149
|
+
|
|
150
|
+
const runs: HistoryRun[] = [];
|
|
151
|
+
for (const series of labelSeries) {
|
|
152
|
+
const run = longestRun(series.points, 1);
|
|
153
|
+
if (run === null) continue;
|
|
154
|
+
runs.push({
|
|
155
|
+
kind: 'label-spend-climbing',
|
|
156
|
+
subject: series.label,
|
|
157
|
+
periods: run.length,
|
|
158
|
+
sinceName: periods[run.start]!.name,
|
|
159
|
+
from: series.points[run.start]!,
|
|
160
|
+
to: series.points[run.start + run.length]!,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
for (const series of modelShareSeries) {
|
|
164
|
+
const run = longestRun(series.points, 1);
|
|
165
|
+
if (run === null) continue;
|
|
166
|
+
runs.push({
|
|
167
|
+
kind: 'model-share-climbing',
|
|
168
|
+
subject: series.model,
|
|
169
|
+
periods: run.length,
|
|
170
|
+
sinceName: periods[run.start]!.name,
|
|
171
|
+
from: series.points[run.start]!,
|
|
172
|
+
to: series.points[run.start + run.length]!,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
{
|
|
176
|
+
const run = longestRun(cacheShareSeries, -1);
|
|
177
|
+
if (run !== null) {
|
|
178
|
+
runs.push({
|
|
179
|
+
kind: 'cache-share-decaying',
|
|
180
|
+
subject: 'cache',
|
|
181
|
+
periods: run.length,
|
|
182
|
+
sinceName: periods[run.start]!.name,
|
|
183
|
+
from: cacheShareSeries[run.start]!,
|
|
184
|
+
to: cacheShareSeries[run.start + run.length]!,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
runs.sort((a, b) => b.periods - a.periods);
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Plans held against each other: the same action (kind, label, model) in
|
|
192
|
+
* two or more plans is a decision nobody is executing, and the dates make
|
|
193
|
+
* the sentence sayable — "planned first on <date>, still planned on
|
|
194
|
+
* <date>".
|
|
195
|
+
*/
|
|
196
|
+
const seen = new Map<string, RepeatedPlanAction>();
|
|
197
|
+
const ordered = [...plans].sort((a, b) => (a.createdAt ?? '').localeCompare(b.createdAt ?? ''));
|
|
198
|
+
for (const plan of ordered) {
|
|
199
|
+
for (const action of plan.actions) {
|
|
200
|
+
const key = `${action.kind}\n${action.label}\n${action.model}`;
|
|
201
|
+
const entry = seen.get(key);
|
|
202
|
+
if (entry === undefined) {
|
|
203
|
+
seen.set(key, {
|
|
204
|
+
kind: action.kind,
|
|
205
|
+
label: action.label,
|
|
206
|
+
model: action.model,
|
|
207
|
+
appearances: 1,
|
|
208
|
+
firstPlanned: plan.createdAt ?? null,
|
|
209
|
+
lastPlanned: plan.createdAt ?? null,
|
|
210
|
+
});
|
|
211
|
+
} else {
|
|
212
|
+
entry.appearances += 1;
|
|
213
|
+
entry.lastPlanned = plan.createdAt ?? entry.lastPlanned;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const repeatedPlanActions = [...seen.values()]
|
|
218
|
+
.filter((entry) => entry.appearances >= 2)
|
|
219
|
+
.sort((a, b) => b.appearances - a.appearances);
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
schemaVersion: 1,
|
|
223
|
+
periods,
|
|
224
|
+
labelSeries,
|
|
225
|
+
modelShareSeries,
|
|
226
|
+
cacheShareSeries,
|
|
227
|
+
runs,
|
|
228
|
+
repeatedPlanActions,
|
|
229
|
+
undatedReports,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Reads one stored `profile --json` document into the slice history needs.
|
|
235
|
+
* Returns null when the JSON is not a profile document — the caller names
|
|
236
|
+
* the file rather than absorbing it.
|
|
237
|
+
*/
|
|
238
|
+
export function storedReportFrom(name: string, parsed: unknown): StoredReport | null {
|
|
239
|
+
const doc = parsed as {
|
|
240
|
+
schemaVersion?: number;
|
|
241
|
+
span?: { fromMs: number; toMs: number } | null;
|
|
242
|
+
total?: {
|
|
243
|
+
totalUsd?: number;
|
|
244
|
+
calls?: number;
|
|
245
|
+
inputTokens?: number;
|
|
246
|
+
cacheReadTokens?: number;
|
|
247
|
+
cacheWriteTokens?: number;
|
|
248
|
+
};
|
|
249
|
+
byLabelAndModel?: {
|
|
250
|
+
label?: string;
|
|
251
|
+
model?: string;
|
|
252
|
+
breakdown?: { totalUsd?: number };
|
|
253
|
+
}[];
|
|
254
|
+
};
|
|
255
|
+
if (doc === null || typeof doc !== 'object') return null;
|
|
256
|
+
if (doc.schemaVersion !== 1 || doc.total === undefined || !Array.isArray(doc.byLabelAndModel)) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const byLabel = new Map<string, number>();
|
|
261
|
+
const byModel = new Map<string, number>();
|
|
262
|
+
for (const slice of doc.byLabelAndModel) {
|
|
263
|
+
const usd = slice.breakdown?.totalUsd ?? 0;
|
|
264
|
+
const label = slice.label ?? UNLABELLED;
|
|
265
|
+
const model = slice.model ?? 'unknown';
|
|
266
|
+
byLabel.set(label, (byLabel.get(label) ?? 0) + usd);
|
|
267
|
+
byModel.set(model, (byModel.get(model) ?? 0) + usd);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const input = doc.total.inputTokens ?? 0;
|
|
271
|
+
const cacheRead = doc.total.cacheReadTokens ?? 0;
|
|
272
|
+
const cacheWrite = doc.total.cacheWriteTokens ?? 0;
|
|
273
|
+
const denominator = input + cacheRead + cacheWrite;
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
name,
|
|
277
|
+
span: doc.span ?? null,
|
|
278
|
+
totalUsd: doc.total.totalUsd ?? 0,
|
|
279
|
+
calls: doc.total.calls ?? 0,
|
|
280
|
+
byLabel,
|
|
281
|
+
byModel,
|
|
282
|
+
cacheReadShare: denominator > 0 ? cacheRead / denominator : null,
|
|
283
|
+
};
|
|
284
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,26 @@ export { assignSources, fleetRollup } from './fleet.js';
|
|
|
47
47
|
export { buildPlan, planLabelName } from './plan.js';
|
|
48
48
|
export type { PlanAction, PlanActionKind, PlanAssumption, PlanDocument } from './plan.js';
|
|
49
49
|
export { verifyPlan } from './verify.js';
|
|
50
|
+
export { buildHistory, storedReportFrom, MIN_RUN } from './history.js';
|
|
51
|
+
export {
|
|
52
|
+
CONNECTORS,
|
|
53
|
+
connectorFor,
|
|
54
|
+
normalizeAnthropicUsage,
|
|
55
|
+
normalizeOpenAIUsage,
|
|
56
|
+
bucketedProfile,
|
|
57
|
+
bucketedCacheEconomics,
|
|
58
|
+
} from './connector.js';
|
|
59
|
+
export type {
|
|
60
|
+
BucketedReport,
|
|
61
|
+
BucketedSlice,
|
|
62
|
+
ConnectorDescriptor,
|
|
63
|
+
ConnectorGranularity,
|
|
64
|
+
ConnectorPull,
|
|
65
|
+
PullGap,
|
|
66
|
+
UnavailableFinding,
|
|
67
|
+
UsageBucket,
|
|
68
|
+
} from './connector.js';
|
|
69
|
+
export type { HistoryDocument, HistoryRun, RepeatedPlanAction, StoredReport } from './history.js';
|
|
50
70
|
export type { CannotTellReason, PlanVerification, VerifiedAction, VerifyOutcome } from './verify.js';
|
|
51
71
|
export type { FleetSource, FleetRollup } from './fleet.js';
|
|
52
72
|
export type { MeasuredUsage, LabelCoverage } from './measured-profile.js';
|