@trazum/cli 1.50.9 → 1.50.11

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, 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, 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
@@ -35,6 +35,9 @@ const VALUE_FLAGS = new Set([
35
35
  'a',
36
36
  'at',
37
37
  'b',
38
+ 'floor',
39
+ 'discount',
40
+ 'months',
38
41
  'min-outcomes',
39
42
  'against',
40
43
  'contract',
@@ -327,6 +330,8 @@ const COMMAND_FLAGS = {
327
330
  experiment: ['a', 'b', 'min-outcomes', 'pricing', 'pricing-live'],
328
331
  quality: ['label', 'at', 'gate', 'pricing', 'pricing-live'],
329
332
  semantic: ['yes', 'model', 'pricing', 'pricing-live'],
333
+ owners: ['pricing', 'pricing-live', 'since', 'until'],
334
+ commitment: ['floor', 'discount', 'months', 'pricing', 'pricing-live'],
330
335
  where: [],
331
336
  rules: [],
332
337
  blame: ['limit', 'model', 'calls', 'output-tokens', 'batch', 'prompt', 'markdown-out'],
@@ -2056,6 +2061,236 @@ async function commandSemantic(args, config, pricing, t) {
2056
2061
  console.log(` ${c.dim(wrap(t.semantic.optIn(), 74, ' '))}`);
2057
2062
  console.log();
2058
2063
  }
2064
+ /**
2065
+ * `trazum owners <log>` — whose budget each workload lands on.
2066
+ *
2067
+ * The fleet answered *which service* in 1.37. This answers *whose money*,
2068
+ * which is the question that decides whether anything on the list gets done: a
2069
+ * report saying "the bill is $40,000 and here is $9,000 of savings" is read by
2070
+ * four people who each assume it is one of the other three's problem.
2071
+ *
2072
+ * **The unallocated is its own line and is never spread.** See `owners.ts` for
2073
+ * why that is worth breaking a module over.
2074
+ */
2075
+ async function commandOwners(args, config, pricing, t) {
2076
+ const path = args.positional[0];
2077
+ if (path === undefined)
2078
+ throw new Error(t.errors.missingInputFile());
2079
+ console.log();
2080
+ console.log(c.bold(t.owners.heading()));
2081
+ if (config.owners === undefined) {
2082
+ console.log(` ${c.dim(wrap(t.owners.noOwners(), 74, ' '))}`);
2083
+ console.log();
2084
+ return;
2085
+ }
2086
+ const report = profileUsage(await readUsageLog(path, t), { catalogue: pricing });
2087
+ const result = allocate(report.byLabel.map((entry) => ({
2088
+ label: entry.label,
2089
+ usd: entry.breakdown.totalUsd,
2090
+ calls: entry.breakdown.calls,
2091
+ })), config.owners);
2092
+ const n = (value) => value.toLocaleString(t.numberLocale);
2093
+ const pct = (value) => `${(value * 100).toFixed(1)}%`;
2094
+ /**
2095
+ * Problems first, before any figure.
2096
+ *
2097
+ * A split that does not sum to one sends a whole workload to unallocated,
2098
+ * and a reader who saw the table before the explanation would go looking for
2099
+ * a bug in their logs.
2100
+ */
2101
+ if (result.problems.length > 0) {
2102
+ console.log();
2103
+ console.log(` ${c.red('✗')} ${c.bold(t.owners.problemsHeading())}`);
2104
+ for (const problem of result.problems) {
2105
+ const detail = problem.kind === 'budget-for-unknown-owner'
2106
+ ? problem.owner
2107
+ : problem.kind === 'split-does-not-sum'
2108
+ ? `"${problem.label}" sums to ${problem.total}`
2109
+ : problem.kind === 'negative-share' || problem.kind === 'split-names-unknown-owner'
2110
+ ? `"${problem.label}" → ${problem.owner}`
2111
+ : `"${problem.label}"`;
2112
+ console.log(` ${wrap(t.owners.problem(problem.kind, detail), 70, ' ')}`);
2113
+ }
2114
+ process.exitCode = 1;
2115
+ }
2116
+ console.log();
2117
+ const col = t.owners.columns;
2118
+ const rows = result.owners.map((line) => ({
2119
+ owner: line.owner,
2120
+ spend: formatUsd(line.usd),
2121
+ budget: line.budgetUsd === null ? '—' : formatUsd(line.budgetUsd),
2122
+ calls: n(Math.round(line.calls)),
2123
+ verdict: t.owners.verdict(line.verdict),
2124
+ kind: line.verdict,
2125
+ }));
2126
+ const w = {
2127
+ owner: Math.max(...rows.map((r) => r.owner.length), col.owner.length),
2128
+ spend: Math.max(...rows.map((r) => r.spend.length), col.spend.length),
2129
+ budget: Math.max(...rows.map((r) => r.budget.length), col.budget.length),
2130
+ calls: Math.max(...rows.map((r) => r.calls.length), col.calls.length),
2131
+ };
2132
+ console.log(c.dim(` ${col.owner.padEnd(w.owner)} ${col.spend.padStart(w.spend)} ` +
2133
+ `${col.budget.padStart(w.budget)} ${col.calls.padStart(w.calls)}`));
2134
+ for (const row of rows) {
2135
+ const tint = row.kind === 'over' ? c.red : row.kind === 'not-measured' ? c.yellow : c.dim;
2136
+ console.log(` ${row.owner.padEnd(w.owner)} ${row.spend.padStart(w.spend)} ` +
2137
+ `${row.budget.padStart(w.budget)} ${row.calls.padStart(w.calls)} ${tint(row.verdict)}`);
2138
+ }
2139
+ // The 1.37 refusal, applied to people, said in full for each owner it hits.
2140
+ for (const line of result.owners) {
2141
+ if (line.verdict === 'not-measured') {
2142
+ console.log();
2143
+ console.log(` ${c.yellow('!')} ${wrap(t.owners.notMeasured(line.owner), 74, ' ')}`);
2144
+ }
2145
+ }
2146
+ console.log();
2147
+ if (result.unallocated.usd > 0) {
2148
+ console.log(` ${c.yellow('!')} ${wrap(t.owners.unallocated(formatUsd(result.unallocated.usd), report.total.totalUsd > 0 ? pct(result.unallocated.usd / report.total.totalUsd) : '—', result.unallocated.labels.slice(0, 6).join(', ')), 74, ' ')}`);
2149
+ console.log(` ${c.dim(wrap(t.owners.neverSpread(), 72, ' '))}`);
2150
+ }
2151
+ else {
2152
+ console.log(` ${c.dim(t.owners.nothingUnallocated())}`);
2153
+ }
2154
+ /**
2155
+ * The shared rules, printed with the report.
2156
+ *
2157
+ * The whole design: the argument then happens about the rule — "why is
2158
+ * search 60/40?" — rather than about the number, which is an argument nobody
2159
+ * can win because nobody can see where the number came from.
2160
+ */
2161
+ if (result.sharedApplied.length > 0) {
2162
+ console.log();
2163
+ console.log(` ${c.bold(t.owners.sharedHeading())}`);
2164
+ for (const { label, split } of result.sharedApplied) {
2165
+ const rule = Object.entries(split)
2166
+ .map(([owner, share]) => `${owner} ${pct(share)}`)
2167
+ .join(', ');
2168
+ console.log(` ${c.dim(t.owners.sharedRule(label, rule))}`);
2169
+ }
2170
+ }
2171
+ console.log();
2172
+ }
2173
+ /**
2174
+ * `trazum commitment <log> --floor <usd> --discount <pct> [--months 12]`
2175
+ *
2176
+ * What a committed-use deal would have been worth **on the traffic you
2177
+ * actually had**. Every team that signs one of these is doing arithmetic in a
2178
+ * spreadsheet against a number they guessed, and it is the highest-stakes
2179
+ * instance of exactly the failure this product exists to end — because the
2180
+ * guess is annual and signed.
2181
+ *
2182
+ * Nothing here projects. "On the traffic you actually had, this would have
2183
+ * saved $X" is a measurement; "you will save $X" is a claim about the future
2184
+ * this product has refused at every scale since 1.27.
2185
+ */
2186
+ async function commandCommitment(args, config, pricing, t) {
2187
+ const path = args.positional[0];
2188
+ if (path === undefined)
2189
+ throw new Error(t.errors.missingInputFile());
2190
+ const floorRaw = stringFlag(args, 'floor');
2191
+ const discountRaw = stringFlag(args, 'discount');
2192
+ const floor = floorRaw === undefined ? Number.NaN : Number(floorRaw);
2193
+ const discountInput = discountRaw === undefined ? Number.NaN : Number(discountRaw);
2194
+ if (!Number.isFinite(floor) || floor <= 0 || !Number.isFinite(discountInput) || discountInput <= 0) {
2195
+ throw new Error(t.commitment.needsTerms());
2196
+ }
2197
+ // Written either way in a contract, so read either way: 20 and 0.2 are the
2198
+ // same deal, and refusing one of them would be pedantry with a stack trace.
2199
+ const discount = discountInput > 1 ? discountInput / 100 : discountInput;
2200
+ const months = Number(stringFlag(args, 'months') ?? 12);
2201
+ const terms = { monthlyFloorUsd: floor, discount, months };
2202
+ const report = profileUsage(await readUsageLog(path, t), { catalogue: pricing });
2203
+ /**
2204
+ * Whole calendar months only.
2205
+ *
2206
+ * A partial month replayed against a monthly floor is a shortfall the
2207
+ * traffic never had — the deal would be judged against a fortnight of usage
2208
+ * and a full month of commitment. Dropping them costs an answer; keeping
2209
+ * them would manufacture one.
2210
+ */
2211
+ const byMonth = new Map();
2212
+ for (const day of report.spendByDay) {
2213
+ const key = day.day.slice(0, 7);
2214
+ byMonth.set(key, (byMonth.get(key) ?? 0) + day.usd);
2215
+ }
2216
+ const days = new Map();
2217
+ for (const day of report.spendByDay) {
2218
+ const key = day.day.slice(0, 7);
2219
+ const set = days.get(key) ?? new Set();
2220
+ set.add(day.day);
2221
+ days.set(key, set);
2222
+ }
2223
+ const whole = [...byMonth.entries()]
2224
+ .filter(([key]) => {
2225
+ const [y, m] = key.split('-').map(Number);
2226
+ const inMonth = new Date(Date.UTC(y ?? 2026, m ?? 1, 0)).getUTCDate();
2227
+ return (days.get(key)?.size ?? 0) >= inMonth - 2;
2228
+ })
2229
+ .map(([month, usd]) => ({ month, usd }))
2230
+ .sort((a, b) => a.month.localeCompare(b.month));
2231
+ const replay = replayCommitment(whole, terms);
2232
+ const coverage = coversTheTerm(replay, terms);
2233
+ const n = (value) => value.toLocaleString(t.numberLocale);
2234
+ const pct = (value) => `${(value * 100).toFixed(0)}%`;
2235
+ console.log();
2236
+ console.log(c.bold(t.commitment.heading(formatUsd(floor), pct(discount), n(months))));
2237
+ console.log(` ${c.dim(wrap(t.commitment.asIf(), 74, ' '))}`);
2238
+ console.log();
2239
+ if (replay.unknown !== null) {
2240
+ console.log(` ${c.yellow('?')} ${wrap(t.commitment.cannotTell(replay.unknown, n(replay.monthsNeeded ?? 0)), 74, ' ')}`);
2241
+ console.log();
2242
+ console.log(` ${c.dim(t.commitment.breakEven(formatUsd(replay.breakEvenMonthlyUsd)))}`);
2243
+ console.log();
2244
+ return;
2245
+ }
2246
+ const col = t.commitment.columns;
2247
+ const rows = replay.months.map((m) => ({
2248
+ month: m.month,
2249
+ list: formatUsd(m.listUsd),
2250
+ paid: formatUsd(m.paidUsd),
2251
+ // Signed: every month in this column can go either way, and formatUsd
2252
+ // renders a negative as `$-2,400`, which reads as a typo. The sign carries
2253
+ // the whole meaning here, so it goes where a reader expects it.
2254
+ saving: formatSignedUsd(m.savingUsd),
2255
+ shortfall: m.shortfall,
2256
+ }));
2257
+ const w = {
2258
+ month: Math.max(...rows.map((r) => r.month.length), col.month.length),
2259
+ list: Math.max(...rows.map((r) => r.list.length), col.list.length),
2260
+ paid: Math.max(...rows.map((r) => r.paid.length), col.paid.length),
2261
+ saving: Math.max(...rows.map((r) => r.saving.length), col.saving.length),
2262
+ };
2263
+ console.log(c.dim(` ${col.month.padEnd(w.month)} ${col.list.padStart(w.list)} ` +
2264
+ `${col.paid.padStart(w.paid)} ${col.saving.padStart(w.saving)}`));
2265
+ for (const row of rows) {
2266
+ const tint = row.shortfall ? c.red : c.dim;
2267
+ console.log(` ${row.month.padEnd(w.month)} ${row.list.padStart(w.list)} ` +
2268
+ `${row.paid.padStart(w.paid)} ${tint(row.saving.padStart(w.saving))}`);
2269
+ }
2270
+ console.log();
2271
+ console.log(` ${c.bold(t.commitment.net(formatUsd(replay.netUsd), n(replay.months.length)))}`);
2272
+ console.log(` ${c.dim(t.commitment.good(formatUsd(replay.savedInGoodMonthsUsd)))}`);
2273
+ if (replay.shortfallMonths > 0) {
2274
+ console.log(` ${c.yellow('!')} ${wrap(t.commitment.lost(formatUsd(replay.lostToUnusedFloorUsd), n(replay.shortfallMonths)), 74, ' ')}`);
2275
+ }
2276
+ else {
2277
+ console.log(` ${c.dim(t.commitment.noShortfall())}`);
2278
+ }
2279
+ console.log();
2280
+ console.log(` ${c.dim(wrap(t.commitment.breakEven(formatUsd(replay.breakEvenMonthlyUsd)), 74, ' '))}`);
2281
+ if (replay.spread !== null) {
2282
+ console.log(` ${c.dim(wrap(t.commitment.spread(formatUsd(replay.spread.lowUsd), formatUsd(replay.spread.highUsd), formatUsd(replay.spread.medianUsd)), 74, ' '))}`);
2283
+ }
2284
+ // Said, not enforced: a shorter history is a real answer about a shorter
2285
+ // period, and the gap must not go unmarked.
2286
+ if (coverage.short) {
2287
+ console.log();
2288
+ console.log(` ${c.yellow('!')} ${wrap(t.commitment.shortTerm(n(coverage.covered), n(coverage.ofMonths)), 74, ' ')}`);
2289
+ }
2290
+ console.log();
2291
+ console.log(` ${c.dim(wrap(t.commitment.neverAForecast(), 74, ' '))}`);
2292
+ console.log();
2293
+ }
2059
2294
  function commandModels(t, pricing) {
2060
2295
  const n = (value) => value.toLocaleString(t.numberLocale);
2061
2296
  const col = t.models.columns;
@@ -7103,6 +7338,12 @@ async function main() {
7103
7338
  case 'models':
7104
7339
  commandModels(t, pricing);
7105
7340
  break;
7341
+ case 'commitment':
7342
+ await commandCommitment(args, config, pricing, t);
7343
+ break;
7344
+ case 'owners':
7345
+ await commandOwners(args, config, pricing, t);
7346
+ break;
7106
7347
  case 'semantic':
7107
7348
  await commandSemantic(args, config, pricing, t);
7108
7349
  break;