@trazum/cli 1.50.10 → 1.51.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/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { open, readdir, readFile, stat, writeFile } from 'node:fs/promises';
4
4
  import { dirname, join, resolve as resolvePath } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import { gunzipSync } from 'node:zlib';
7
- import { applyRewrites, BASELINE_FILENAME, BASELINE_VERSION, breaches, cacheableMinimum, analyzeCachePrefix, billLevers, bucketedCacheEconomics, bucketedProfile, buildHistory, buildPlan, connectorFor, CONNECTORS, normalizeAnthropicUsage, normalizeOpenAIUsage, bucketsFromRecords, evaluateWatch, firedKey, pruneRecords, recordsFromBuckets, storeInventory, storedReportFrom, verifyPlan, cacheEconomics, cacheHitRate, contextPressure, comparePrompts, compareToBaseline, computeSavings, countTokensAnthropic, DEFAULT_USAGE, budgetPositions, conform, BREAK_EVEN_BAND, allocate, runExperiment, qualityGate, semanticPassCost, verifySemanticProposals, SEMANTIC_SYSTEM_PROMPT, ladderPosition, validateLadder, outcomeReport, rankPerOutcome, FAILURE_POLICIES, detectFromSource, matchLocale, parsePlanDocument, waiverDay, waiverHistory, proposeInit, MIN_RATE_DAYS, parseConfig, coverageDrift, driversBetween, explainGateFailure, assignSources, fleetRollup, labelCoverage, measuredUsage, gateMargin, GATE_MARGIN_TIGHT, estimateTokens, evaluate, extractPrompts, findExamples, formatBaseline, formatSignedUsd, formatUsd, getMessages, getModel, hasMarker, LOCALES, MAX_BASELINE_BYTES, moneyIsComparable, mostSpecificMatch, nearestName, optimize, parseBaseline, PHRASE_LANGUAGES, plannedCalls, profilePrompt, profileToCsv, profileUsage, promptId, providerFromEnv, pruneExamples, refineWithLlm, rejectionText, reorderForCache, repriceProfile, reviewAgeDays, reviewExamples, RULES, sharedPrefixes, sharesOf, SOURCE_EXTENSIONS, suggestRewrites, toOtlpMetrics, toPromptfoo, TTL_1H_MS, UNLABELLED, withExactTokenCounts, } from '@trazum/core';
7
+ import { applyRewrites, BASELINE_FILENAME, BASELINE_VERSION, breaches, cacheableMinimum, analyzeCachePrefix, billLevers, bucketedCacheEconomics, bucketedProfile, buildHistory, buildPlan, connectorFor, CONNECTORS, normalizeAnthropicUsage, normalizeOpenAIUsage, bucketsFromRecords, evaluateWatch, firedKey, pruneRecords, recordsFromBuckets, storeInventory, storedReportFrom, verifyPlan, cacheEconomics, cacheHitRate, contextPressure, comparePrompts, compareToBaseline, computeSavings, countTokensAnthropic, DEFAULT_USAGE, budgetPositions, conform, BREAK_EVEN_BAND, allocate, annualRecord, replayCommitment, coversTheTerm, runExperiment, qualityGate, semanticPassCost, verifySemanticProposals, SEMANTIC_SYSTEM_PROMPT, ladderPosition, validateLadder, outcomeReport, rankPerOutcome, FAILURE_POLICIES, detectFromSource, matchLocale, parsePlanDocument, waiverDay, waiverHistory, proposeInit, MIN_RATE_DAYS, parseConfig, coverageDrift, driversBetween, explainGateFailure, assignSources, fleetRollup, labelCoverage, measuredUsage, gateMargin, GATE_MARGIN_TIGHT, estimateTokens, evaluate, extractPrompts, findExamples, formatBaseline, formatSignedUsd, formatUsd, getMessages, getModel, hasMarker, LOCALES, MAX_BASELINE_BYTES, moneyIsComparable, mostSpecificMatch, nearestName, optimize, parseBaseline, PHRASE_LANGUAGES, plannedCalls, profilePrompt, profileToCsv, profileUsage, promptId, providerFromEnv, pruneExamples, refineWithLlm, rejectionText, reorderForCache, repriceProfile, reviewAgeDays, reviewExamples, RULES, sharedPrefixes, sharesOf, SOURCE_EXTENSIONS, suggestRewrites, toOtlpMetrics, toPromptfoo, TTL_1H_MS, UNLABELLED, withExactTokenCounts, } from '@trazum/core';
8
8
  import { cacheDir, cacheStats, cachingProvider, clearCache } from './suggest-cache.js';
9
9
  import { dayOf, formatGap, median, spanDays } from './time.js';
10
10
  // Everything that reads the filesystem, on its own entry point so the web
@@ -34,7 +34,11 @@ const c = {
34
34
  const VALUE_FLAGS = new Set([
35
35
  'a',
36
36
  'at',
37
+ 'year',
37
38
  'b',
39
+ 'floor',
40
+ 'discount',
41
+ 'months',
38
42
  'min-outcomes',
39
43
  'against',
40
44
  'contract',
@@ -328,6 +332,8 @@ const COMMAND_FLAGS = {
328
332
  quality: ['label', 'at', 'gate', 'pricing', 'pricing-live'],
329
333
  semantic: ['yes', 'model', 'pricing', 'pricing-live'],
330
334
  owners: ['pricing', 'pricing-live', 'since', 'until'],
335
+ commitment: ['floor', 'discount', 'months', 'pricing', 'pricing-live'],
336
+ report: ['year', 'json', 'pricing', 'pricing-live'],
331
337
  where: [],
332
338
  rules: [],
333
339
  blame: ['limit', 'model', 'calls', 'output-tokens', 'batch', 'prompt', 'markdown-out'],
@@ -2166,6 +2172,229 @@ async function commandOwners(args, config, pricing, t) {
2166
2172
  }
2167
2173
  console.log();
2168
2174
  }
2175
+ /**
2176
+ * `trazum commitment <log> --floor <usd> --discount <pct> [--months 12]`
2177
+ *
2178
+ * What a committed-use deal would have been worth **on the traffic you
2179
+ * actually had**. Every team that signs one of these is doing arithmetic in a
2180
+ * spreadsheet against a number they guessed, and it is the highest-stakes
2181
+ * instance of exactly the failure this product exists to end — because the
2182
+ * guess is annual and signed.
2183
+ *
2184
+ * Nothing here projects. "On the traffic you actually had, this would have
2185
+ * saved $X" is a measurement; "you will save $X" is a claim about the future
2186
+ * this product has refused at every scale since 1.27.
2187
+ */
2188
+ async function commandCommitment(args, config, pricing, t) {
2189
+ const path = args.positional[0];
2190
+ if (path === undefined)
2191
+ throw new Error(t.errors.missingInputFile());
2192
+ const floorRaw = stringFlag(args, 'floor');
2193
+ const discountRaw = stringFlag(args, 'discount');
2194
+ const floor = floorRaw === undefined ? Number.NaN : Number(floorRaw);
2195
+ const discountInput = discountRaw === undefined ? Number.NaN : Number(discountRaw);
2196
+ if (!Number.isFinite(floor) || floor <= 0 || !Number.isFinite(discountInput) || discountInput <= 0) {
2197
+ throw new Error(t.commitment.needsTerms());
2198
+ }
2199
+ // Written either way in a contract, so read either way: 20 and 0.2 are the
2200
+ // same deal, and refusing one of them would be pedantry with a stack trace.
2201
+ const discount = discountInput > 1 ? discountInput / 100 : discountInput;
2202
+ const months = Number(stringFlag(args, 'months') ?? 12);
2203
+ const terms = { monthlyFloorUsd: floor, discount, months };
2204
+ const report = profileUsage(await readUsageLog(path, t), { catalogue: pricing });
2205
+ /**
2206
+ * Whole calendar months only.
2207
+ *
2208
+ * A partial month replayed against a monthly floor is a shortfall the
2209
+ * traffic never had — the deal would be judged against a fortnight of usage
2210
+ * and a full month of commitment. Dropping them costs an answer; keeping
2211
+ * them would manufacture one.
2212
+ */
2213
+ const byMonth = new Map();
2214
+ for (const day of report.spendByDay) {
2215
+ const key = day.day.slice(0, 7);
2216
+ byMonth.set(key, (byMonth.get(key) ?? 0) + day.usd);
2217
+ }
2218
+ const days = new Map();
2219
+ for (const day of report.spendByDay) {
2220
+ const key = day.day.slice(0, 7);
2221
+ const set = days.get(key) ?? new Set();
2222
+ set.add(day.day);
2223
+ days.set(key, set);
2224
+ }
2225
+ const whole = [...byMonth.entries()]
2226
+ .filter(([key]) => {
2227
+ const [y, m] = key.split('-').map(Number);
2228
+ const inMonth = new Date(Date.UTC(y ?? 2026, m ?? 1, 0)).getUTCDate();
2229
+ return (days.get(key)?.size ?? 0) >= inMonth - 2;
2230
+ })
2231
+ .map(([month, usd]) => ({ month, usd }))
2232
+ .sort((a, b) => a.month.localeCompare(b.month));
2233
+ const replay = replayCommitment(whole, terms);
2234
+ const coverage = coversTheTerm(replay, terms);
2235
+ const n = (value) => value.toLocaleString(t.numberLocale);
2236
+ const pct = (value) => `${(value * 100).toFixed(0)}%`;
2237
+ console.log();
2238
+ console.log(c.bold(t.commitment.heading(formatUsd(floor), pct(discount), n(months))));
2239
+ console.log(` ${c.dim(wrap(t.commitment.asIf(), 74, ' '))}`);
2240
+ console.log();
2241
+ if (replay.unknown !== null) {
2242
+ console.log(` ${c.yellow('?')} ${wrap(t.commitment.cannotTell(replay.unknown, n(replay.monthsNeeded ?? 0)), 74, ' ')}`);
2243
+ console.log();
2244
+ console.log(` ${c.dim(t.commitment.breakEven(formatUsd(replay.breakEvenMonthlyUsd)))}`);
2245
+ console.log();
2246
+ return;
2247
+ }
2248
+ const col = t.commitment.columns;
2249
+ const rows = replay.months.map((m) => ({
2250
+ month: m.month,
2251
+ list: formatUsd(m.listUsd),
2252
+ paid: formatUsd(m.paidUsd),
2253
+ // Signed: every month in this column can go either way, and formatUsd
2254
+ // renders a negative as `$-2,400`, which reads as a typo. The sign carries
2255
+ // the whole meaning here, so it goes where a reader expects it.
2256
+ saving: formatSignedUsd(m.savingUsd),
2257
+ shortfall: m.shortfall,
2258
+ }));
2259
+ const w = {
2260
+ month: Math.max(...rows.map((r) => r.month.length), col.month.length),
2261
+ list: Math.max(...rows.map((r) => r.list.length), col.list.length),
2262
+ paid: Math.max(...rows.map((r) => r.paid.length), col.paid.length),
2263
+ saving: Math.max(...rows.map((r) => r.saving.length), col.saving.length),
2264
+ };
2265
+ console.log(c.dim(` ${col.month.padEnd(w.month)} ${col.list.padStart(w.list)} ` +
2266
+ `${col.paid.padStart(w.paid)} ${col.saving.padStart(w.saving)}`));
2267
+ for (const row of rows) {
2268
+ const tint = row.shortfall ? c.red : c.dim;
2269
+ console.log(` ${row.month.padEnd(w.month)} ${row.list.padStart(w.list)} ` +
2270
+ `${row.paid.padStart(w.paid)} ${tint(row.saving.padStart(w.saving))}`);
2271
+ }
2272
+ console.log();
2273
+ console.log(` ${c.bold(t.commitment.net(formatUsd(replay.netUsd), n(replay.months.length)))}`);
2274
+ console.log(` ${c.dim(t.commitment.good(formatUsd(replay.savedInGoodMonthsUsd)))}`);
2275
+ if (replay.shortfallMonths > 0) {
2276
+ console.log(` ${c.yellow('!')} ${wrap(t.commitment.lost(formatUsd(replay.lostToUnusedFloorUsd), n(replay.shortfallMonths)), 74, ' ')}`);
2277
+ }
2278
+ else {
2279
+ console.log(` ${c.dim(t.commitment.noShortfall())}`);
2280
+ }
2281
+ console.log();
2282
+ console.log(` ${c.dim(wrap(t.commitment.breakEven(formatUsd(replay.breakEvenMonthlyUsd)), 74, ' '))}`);
2283
+ if (replay.spread !== null) {
2284
+ console.log(` ${c.dim(wrap(t.commitment.spread(formatUsd(replay.spread.lowUsd), formatUsd(replay.spread.highUsd), formatUsd(replay.spread.medianUsd)), 74, ' '))}`);
2285
+ }
2286
+ // Said, not enforced: a shorter history is a real answer about a shorter
2287
+ // period, and the gap must not go unmarked.
2288
+ if (coverage.short) {
2289
+ console.log();
2290
+ console.log(` ${c.yellow('!')} ${wrap(t.commitment.shortTerm(n(coverage.covered), n(coverage.ofMonths)), 74, ' ')}`);
2291
+ }
2292
+ console.log();
2293
+ console.log(` ${c.dim(wrap(t.commitment.neverAForecast(), 74, ' '))}`);
2294
+ console.log();
2295
+ }
2296
+ /**
2297
+ * `trazum report <log> --year <yyyy>` — the year, from what was already
2298
+ * written down.
2299
+ *
2300
+ * The last chapter of the arc, and the one that turns this product's argument
2301
+ * into something a stranger can audit. **No new data**: everything here comes
2302
+ * from the store and the plans a team already keeps, and nothing is computed
2303
+ * that could not be checked against a document that already exists.
2304
+ *
2305
+ * That constraint is the whole design. An annual report is the document most
2306
+ * likely to be quoted out of the room it was written in, and the one nobody
2307
+ * goes back to verify.
2308
+ */
2309
+ async function commandReport(args, config, pricing, t) {
2310
+ const path = args.positional[0];
2311
+ if (path === undefined)
2312
+ throw new Error(t.errors.missingInputFile());
2313
+ const year = stringFlag(args, 'year');
2314
+ if (year === undefined || !/^\d{4}$/.test(year))
2315
+ throw new Error(t.annual.needsYear());
2316
+ const report = profileUsage(await readUsageLog(path, t), { catalogue: pricing });
2317
+ /**
2318
+ * One period per calendar month, built from the day series.
2319
+ *
2320
+ * Partial months are kept here, unlike in `commitment`: a year report is
2321
+ * about what happened, and a month that only ran for a week still happened.
2322
+ * What it must not do is imply the month was whole, which is why the count
2323
+ * of recorded months is stated beside the total.
2324
+ */
2325
+ const byMonth = new Map();
2326
+ for (const day of report.spendByDay) {
2327
+ const key = day.day.slice(0, 7);
2328
+ const entry = byMonth.get(key) ?? { usd: 0, calls: 0 };
2329
+ entry.usd += day.usd;
2330
+ entry.calls += day.calls;
2331
+ byMonth.set(key, entry);
2332
+ }
2333
+ const outcomes = outcomeReport(report.outcomeTally, config.outcomes ?? null);
2334
+ const periods = [...byMonth.entries()]
2335
+ .map(([month, entry], index) => ({
2336
+ month,
2337
+ usd: entry.usd,
2338
+ calls: entry.calls,
2339
+ /**
2340
+ * Attached only when something was **recorded**, not merely parsed.
2341
+ *
2342
+ * The first version keyed on `parsed > 0`, which is true of any log with
2343
+ * calls in it — so a year that recorded no outcome at all still got an
2344
+ * outcomes object saying "0 of 120", and the honest sentence ("no
2345
+ * outcome was recorded this year, so nothing here says what the money
2346
+ * bought") was unreachable. A zero dressed as a measurement, which is
2347
+ * the exact failure the field's own contract forbids.
2348
+ *
2349
+ * Attached to the first month rather than divided across them: dividing
2350
+ * would invent a monthly figure nobody measured, and the year's total is
2351
+ * what this document reports.
2352
+ */
2353
+ ...(index === 0 && outcomes.coverage.recorded > 0 ? { outcomes } : {}),
2354
+ }))
2355
+ .sort((a, b) => a.month.localeCompare(b.month));
2356
+ const record = annualRecord(year, periods);
2357
+ const n = (value) => value.toLocaleString(t.numberLocale);
2358
+ console.log();
2359
+ console.log(c.bold(t.annual.heading(year)));
2360
+ console.log();
2361
+ console.log(` ${t.annual.spent(formatUsd(record.totalUsd), n(record.totalCalls), n(record.months.length))}`);
2362
+ if (record.missingMonths.length > 0) {
2363
+ console.log(` ${c.yellow('!')} ${wrap(t.annual.missing(record.missingMonths.join(', ')), 74, ' ')}`);
2364
+ }
2365
+ console.log();
2366
+ const p = record.promises;
2367
+ console.log(` ${wrap(t.annual.promises(n(p.planned), n(p.arrived), n(p.notArrived), n(p.cannotTell)), 74, ' ')}`);
2368
+ if (p.projectedUsd > 0) {
2369
+ console.log(` ${t.annual.projected(formatUsd(p.projectedUsd))}`);
2370
+ console.log(` ${c.dim(wrap(t.annual.noArrivedFigure(), 74, ' '))}`);
2371
+ }
2372
+ console.log();
2373
+ if (record.outcomes === null) {
2374
+ console.log(` ${c.dim(wrap(t.annual.noOutcomes(), 74, ' '))}`);
2375
+ }
2376
+ else {
2377
+ console.log(` ${wrap(t.annual.outcomes(n(record.outcomes.recorded), n(record.outcomes.parsed), formatUsd(record.outcomes.unrecordedUsd)), 74, ' ')}`);
2378
+ }
2379
+ /**
2380
+ * The section an annual report is usually missing, and the reason this one
2381
+ * is worth trusting: a document that lists its own blind spots is a document
2382
+ * somebody can act on the rest of.
2383
+ */
2384
+ if (record.cannotSay.length > 0) {
2385
+ console.log();
2386
+ console.log(` ${c.bold(t.annual.cannotSayHeading())}`);
2387
+ for (const kind of record.cannotSay) {
2388
+ console.log(` ${c.dim('·')} ${wrap(t.annual.cannotSay(kind), 70, ' ')}`);
2389
+ }
2390
+ }
2391
+ console.log();
2392
+ console.log(` ${c.dim(wrap(t.annual.noNewData(), 74, ' '))}`);
2393
+ console.log();
2394
+ if (boolFlag(args, 'json')) {
2395
+ console.log(JSON.stringify(record, null, 2));
2396
+ }
2397
+ }
2169
2398
  function commandModels(t, pricing) {
2170
2399
  const n = (value) => value.toLocaleString(t.numberLocale);
2171
2400
  const col = t.models.columns;
@@ -7213,6 +7442,12 @@ async function main() {
7213
7442
  case 'models':
7214
7443
  commandModels(t, pricing);
7215
7444
  break;
7445
+ case 'report':
7446
+ await commandReport(args, config, pricing, t);
7447
+ break;
7448
+ case 'commitment':
7449
+ await commandCommitment(args, config, pricing, t);
7450
+ break;
7216
7451
  case 'owners':
7217
7452
  await commandOwners(args, config, pricing, t);
7218
7453
  break;