@trazum/cli 1.65.0 → 1.67.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/src/i18n/types.ts CHANGED
@@ -518,6 +518,29 @@ export interface CliMessages {
518
518
  caveat(code: string): string;
519
519
  };
520
520
 
521
+ position: {
522
+ /** `Where 2026-08 stands, measured` — the month is data, not prose. */
523
+ heading(month: string): string;
524
+ scopeMonth(): string;
525
+ scopeDay(): string;
526
+ scopeLabel(label: string): string;
527
+ within(scope: string, measured: string, limit: string, remaining: string, days: number, elapsed: number): string;
528
+ over(scope: string, measured: string, limit: string, overBy: string): string;
529
+ cannotTell(scope: string): string;
530
+ /**
531
+ * Division on the past, said as such — never a forecast. Absent under
532
+ * the floor, so this line only ever renders with its denominator.
533
+ */
534
+ distance(days: string, rate: string, overDays: number): string;
535
+ unmeasuredHeading(): string;
536
+ unmeasured(scope: string, why: string): string;
537
+ why(reason: 'no-clock' | 'no-labels' | 'nothing-recorded' | 'label-unseen'): string;
538
+ cannotSayHeading(): string;
539
+ unpriced(count: number): string;
540
+ /** The source, named: this log alone, never merged with the store. */
541
+ source(): string;
542
+ noLog(): string;
543
+ };
521
544
  pulse: {
522
545
  heading(): string;
523
546
  kind(kind: string): string;
@@ -810,6 +833,10 @@ export interface CliMessages {
810
833
  noBudget(): string;
811
834
  walkTruncated(): string;
812
835
  exactCountsCost(files: number): string;
836
+ /** How many of the listed files are checkable, and how many were dropped. */
837
+ filesFromSummary(checked: number, listed: number, dropped: number): string;
838
+ /** The baseline is whole-repository; a partial list checks budgets only. */
839
+ filesFromNoBaseline(): string;
813
840
  };
814
841
 
815
842
  /**
@@ -1506,6 +1533,14 @@ export interface CliMessages {
1506
1533
  listening(where: string): string;
1507
1534
  /** Why there is no --host, and why that is not an omission. */
1508
1535
  loopbackOnly(): string;
1536
+ /**
1537
+ * A `limits` block is configured and no `--log` was given, so every
1538
+ * ceiling will judge cannot-tell. Said at start rather than discovered
1539
+ * one refusal at a time. Shared by both HTTP doors.
1540
+ */
1541
+ limitsNoLog(): string;
1542
+ /** Unpriced records in the `--log` — money the measured side cannot see. */
1543
+ limitsUnpriced(count: number): string;
1509
1544
  measuredFrom(usd: string): string;
1510
1545
  nothingMeasured(dir: string): string;
1511
1546
  noBudget(): string;
package/src/index.ts CHANGED
@@ -92,6 +92,7 @@ import {
92
92
  listModels,
93
93
  LOCALES,
94
94
  MAX_BASELINE_BYTES,
95
+ matchGlob,
95
96
  MAX_INPUT_CHARS,
96
97
  moneyIsComparable,
97
98
  mostSpecificMatch,
@@ -106,7 +107,11 @@ import {
106
107
  detectTextLanguage,
107
108
  dictionaryStanding,
108
109
  languagesWithStanding,
110
+ indexUsage,
111
+ parseUsageLine,
109
112
  plannedCalls,
113
+ positionAt,
114
+ positionReport,
110
115
  PRICING_LAST_REVIEWED,
111
116
  profilePrompt,
112
117
  profileToCsv,
@@ -239,7 +244,7 @@ import {
239
244
  renderRankMarkdown,
240
245
  renderProfileMarkdown,
241
246
  } from './markdown.js';
242
- import { renderProfileHtml, renderRollupHtml } from './html.js';
247
+ import { renderPositionHtml, renderProfileHtml, renderRollupHtml } from './html.js';
243
248
  import type { CliMessages } from './i18n/index.js';
244
249
 
245
250
  // --------------------------------------------------------------------------
@@ -277,6 +282,8 @@ interface Args {
277
282
  const VALUE_FLAGS = new Set([
278
283
  'answers',
279
284
  'calls',
285
+ 'files-from',
286
+ 'log',
280
287
  'avg-output',
281
288
  'a',
282
289
  'at',
@@ -467,6 +474,28 @@ const LOG_EXTENSIONS = ['.jsonl', '.ndjson', '.log', '.json'];
467
474
  * would be a figure quietly missing a day, the failure this repository
468
475
  * refuses everywhere it can occur.
469
476
  */
477
+ /**
478
+ * The measured side of the `limits` policy, from `--log` — shared by the two
479
+ * HTTP doors. Returns null when no log was named: the doors then judge every
480
+ * ceiling `cannot-tell`, which is the honest answer to "what has this label
481
+ * spent" when nobody handed over the record of what labels spent.
482
+ */
483
+ async function usageIndexFrom(
484
+ args: Args,
485
+ pricing: PricingCatalogue,
486
+ t: CliMessages,
487
+ ): Promise<ReturnType<typeof indexUsage> | null> {
488
+ const logPath = stringFlag(args, 'log');
489
+ if (logPath === undefined) return null;
490
+ const text = await readUsageLog(logPath, t);
491
+ const records = text
492
+ .split('\n')
493
+ .filter((line) => line.trim() !== '')
494
+ .map((line) => parseUsageLine(line))
495
+ .filter((record): record is NonNullable<ReturnType<typeof parseUsageLine>> => record !== null);
496
+ return indexUsage(records, { catalogue: pricing });
497
+ }
498
+
470
499
  async function readUsageLog(file: string, t: CliMessages): Promise<string> {
471
500
  if (!file.endsWith('.gz')) return readFile(file, 'utf8');
472
501
  const compressed = await readFile(file);
@@ -618,7 +647,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
618
647
  'tokens-only', 'cost', 'prompt', 'suggest', 'apply-suggestions',
619
648
  'cache-suggestions', 'from-log', 'label', 'all-labels',
620
649
  ],
621
- check: ['max-tokens', 'level', 'exact-tokens', 'markdown-out', 'baseline'],
650
+ check: ['max-tokens', 'level', 'exact-tokens', 'markdown-out', 'baseline', 'files-from'],
622
651
  baseline: ['model', 'calls', 'output-tokens', 'cache-hit-rate', 'batch', 'exact-tokens', 'out', 'o'],
623
652
  profile: ['json', 'pricing', 'pricing-live', 'against', 'what-if', 'markdown-out', 'html-out', 'csv-out', 'csv-shape', 'max-usd', 'max-growth-usd', 'max-cache-loss-usd', 'max-day-usd', 'max-session-usd', 'label', 'since', 'until', 'dry-run', 'markdown-summary', 'by-source'],
624
653
  plan: ['json', 'out', 'markdown-out', 'min-usd', 'pricing', 'pricing-live'],
@@ -627,7 +656,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
627
656
  connect: ['since', 'until', 'payload', 'store', 'json', 'out', 'markdown-out', 'pricing', 'pricing-live', 'dry-run'],
628
657
  store: ['prune', 'keep', 'json', 'pricing', 'pricing-live', 'dry-run'],
629
658
  watch: ['once', 'interval', 'since', 'payload', 'webhook', 'json', 'pricing', 'pricing-live'],
630
- serve: ['port', 'socket', 'pricing', 'pricing-live'],
659
+ serve: ['port', 'socket', 'log', 'pricing', 'pricing-live'],
631
660
  route: ['prompt-file', 'cases', 'label', 'concurrency', 'json', 'yes', 'pricing', 'pricing-live'],
632
661
  eval: ['cases', 'level', 'concurrency', 'export', 'out', 'o', 'model'],
633
662
  prune: ['cases', 'concurrency', 'json', 'yes'],
@@ -638,11 +667,12 @@ const COMMAND_FLAGS: Record<string, string[]> = {
638
667
  conform: ['contract', 'json'],
639
668
  schema: [],
640
669
  rollup: ['json', 'html-out'],
670
+ position: ['json', 'html-out', 'pricing', 'pricing-live'],
641
671
  pulse: ['json', 'max-stale-hours'],
642
672
  bench: ['workload', 'json', 'record', 'against', 'max-ratio'],
643
673
  write: ['answers', 'json', 'out', 'o', 'calls', 'avg-output'],
644
674
  feedback: [],
645
- gateway: ['on-cannot-tell', 'port', 'socket', 'pricing', 'pricing-live'],
675
+ gateway: ['on-cannot-tell', 'port', 'socket', 'log', 'pricing', 'pricing-live'],
646
676
  ladder: ['pricing', 'pricing-live', 'since', 'until', 'label'],
647
677
  experiment: ['a', 'b', 'min-outcomes', 'pricing', 'pricing-live'],
648
678
  quality: ['label', 'at', 'gate', 'pricing', 'pricing-live'],
@@ -2519,6 +2549,130 @@ async function commandWrite(args: Args, t: CliMessages): Promise<void> {
2519
2549
  }
2520
2550
  }
2521
2551
 
2552
+ /**
2553
+ * `trazum position <usage.jsonl>` — where the month stands, measured.
2554
+ *
2555
+ * One answer where `profile`, `budgetPositions` and `watch` each held a
2556
+ * piece: every configured ceiling with its measurement, its window and its
2557
+ * denominators, from the named log alone. The distance line is division on
2558
+ * the past — `positionReport` withholds it under the floor, on an over and
2559
+ * on a zero rate, so if it prints, its denominator prints with it.
2560
+ */
2561
+ async function commandPosition(
2562
+ args: Args,
2563
+ config: TrazumConfig,
2564
+ pricing: PricingCatalogue,
2565
+ t: CliMessages,
2566
+ ): Promise<void> {
2567
+ const file = args.positional[0];
2568
+ if (file === undefined) throw new Error(t.position.noLog());
2569
+
2570
+ const text = await readUsageLog(file, t);
2571
+ const records = text
2572
+ .split('\n')
2573
+ .filter((line) => line.trim() !== '')
2574
+ .map((line) => parseUsageLine(line))
2575
+ .filter((record): record is NonNullable<ReturnType<typeof parseUsageLine>> => record !== null);
2576
+
2577
+ const document = positionReport(
2578
+ records,
2579
+ {
2580
+ ...(config.spend === undefined ? {} : { spend: config.spend }),
2581
+ ...(config.limits === undefined ? {} : { limits: config.limits }),
2582
+ },
2583
+ { catalogue: pricing },
2584
+ );
2585
+
2586
+ /**
2587
+ * The HTML door, written on both output paths — the 1.64 rule: the page a
2588
+ * person forwards must exist whether the run was for a human or a pipe.
2589
+ */
2590
+ const htmlOut = stringFlag(args, 'html-out');
2591
+ if (htmlOut !== undefined) {
2592
+ await writeFile(htmlOut, renderPositionHtml(document, t), 'utf8');
2593
+ if (!boolFlag(args, 'json')) console.error(c.dim(t.html.written(htmlOut)));
2594
+ }
2595
+
2596
+ if (boolFlag(args, 'json')) {
2597
+ console.log(JSON.stringify(document, null, 2));
2598
+ return;
2599
+ }
2600
+
2601
+ const scopeName = (position: { scope: string; label: string | null }): string =>
2602
+ position.scope === 'month'
2603
+ ? t.position.scopeMonth()
2604
+ : position.scope === 'day'
2605
+ ? t.position.scopeDay()
2606
+ : t.position.scopeLabel(position.label ?? '');
2607
+
2608
+ console.log();
2609
+ console.log(c.bold(t.position.heading(document.month.id)));
2610
+ for (const position of document.positions) {
2611
+ const name = scopeName(position);
2612
+ if (position.verdict === 'cannot-tell') {
2613
+ console.log(` ${c.yellow('?')} ${wrap(t.position.cannotTell(name), 72, ' ')}`);
2614
+ continue;
2615
+ }
2616
+ if (position.verdict === 'over') {
2617
+ console.log(
2618
+ ` ${c.red('✗')} ${wrap(
2619
+ t.position.over(name, formatUsd(position.measuredUsd), formatUsd(position.limitUsd), formatUsd(-position.remainingUsd)),
2620
+ 72,
2621
+ ' ',
2622
+ )}`,
2623
+ );
2624
+ continue;
2625
+ }
2626
+ console.log(
2627
+ ` ${c.green('✓')} ${wrap(
2628
+ t.position.within(
2629
+ name,
2630
+ formatUsd(position.measuredUsd),
2631
+ formatUsd(position.limitUsd),
2632
+ formatUsd(position.remainingUsd),
2633
+ position.daysMeasured,
2634
+ position.daysElapsed,
2635
+ ),
2636
+ 72,
2637
+ ' ',
2638
+ )}`,
2639
+ );
2640
+ if (position.distance !== null) {
2641
+ console.log(
2642
+ ` ${c.dim(wrap(
2643
+ t.position.distance(
2644
+ position.distance.daysAway.toFixed(1),
2645
+ formatUsd(position.distance.usdPerDay),
2646
+ position.distance.overDays,
2647
+ ),
2648
+ 70,
2649
+ ' ',
2650
+ ))}`,
2651
+ );
2652
+ }
2653
+ }
2654
+ if (document.unmeasured.length > 0) {
2655
+ console.log();
2656
+ console.log(` ${c.bold(t.position.unmeasuredHeading())}`);
2657
+ for (const entry of document.unmeasured) {
2658
+ console.log(` ${c.yellow(wrap(t.position.unmeasured(scopeName(entry), t.position.why(entry.why)), 70, ' '))}`);
2659
+ }
2660
+ }
2661
+ if (document.cannotSay.length > 0) {
2662
+ console.log();
2663
+ console.log(` ${c.bold(t.position.cannotSayHeading())}`);
2664
+ for (const line of document.cannotSay) {
2665
+ console.log(` ${c.dim(wrap(line, 70, ' '))}`);
2666
+ }
2667
+ }
2668
+ if (document.unpricedRecords > 0) {
2669
+ console.log();
2670
+ console.log(` ${c.yellow(wrap(t.position.unpriced(document.unpricedRecords), 72, ' '))}`);
2671
+ }
2672
+ console.log();
2673
+ console.log(` ${c.dim(wrap(t.position.source(), 74, ' '))}`);
2674
+ }
2675
+
2522
2676
  async function commandPulse(args: Args, t: CliMessages): Promise<void> {
2523
2677
  const root = process.cwd();
2524
2678
  const maxStaleHours = numberFlag(args, 'max-stale-hours', Number.NaN, t);
@@ -3172,6 +3326,9 @@ async function commandGateway(
3172
3326
  asOfMs: Date.now(),
3173
3327
  };
3174
3328
 
3329
+ /** Same measured side as `serve`'s, from `--log` — see `usageIndexFrom`. */
3330
+ const limitsIndex = await usageIndexFrom(args, pricing, t);
3331
+
3175
3332
  const measured: { calls: number; usd: number } = { calls: 0, usd: 0 };
3176
3333
  /**
3177
3334
  * Forwarded calls whose cost this session cannot see.
@@ -3188,6 +3345,9 @@ async function commandGateway(
3188
3345
  onCannotTell: policyFlag as FailurePolicy,
3189
3346
  ...(config.spend?.substitute === undefined ? {} : { substitute: config.spend.substitute }),
3190
3347
  },
3348
+ ...(config.limits === undefined ? {} : { limits: config.limits }),
3349
+ ...(limitsIndex === null ? {} : { position: (call: { label?: string; session?: string }) => positionAt(limitsIndex, call) }),
3350
+ ...(config.waive === undefined ? {} : { waivers: config.waive }),
3191
3351
  standing: () => standing,
3192
3352
  record: (call) => {
3193
3353
  measured.calls += 1;
@@ -3228,6 +3388,12 @@ async function commandGateway(
3228
3388
  ` ${c.dim(wrap(standing === null ? t.gateway.noStanding() : t.gateway.standing(formatUsd(standing.consumedUsd), formatUsd(standing.limitUsd)), 74, ' '))}`,
3229
3389
  );
3230
3390
  console.log(` ${c.dim(wrap(t.gateway.policy(policyFlag), 74, ' '))}`);
3391
+ if (config.limits !== undefined && limitsIndex === null) {
3392
+ console.log(` ${c.yellow(wrap(t.serve.limitsNoLog(), 74, ' '))}`);
3393
+ }
3394
+ if (limitsIndex !== null && limitsIndex.unpriced > 0) {
3395
+ console.log(` ${c.yellow(wrap(t.serve.limitsUnpriced(limitsIndex.unpriced), 74, ' '))}`);
3396
+ }
3231
3397
  console.log();
3232
3398
  }
3233
3399
 
@@ -4920,6 +5086,25 @@ async function commandCheck(
4920
5086
  // A flag beats the config, as everywhere else. -1 means "not given".
4921
5087
  const flagBudget = numberFlag(args, 'max-tokens', -1, t);
4922
5088
 
5089
+ /**
5090
+ * `--files-from -` reads a file list from stdin — the shape
5091
+ * `git diff --name-only` already produces — so a pre-commit hook is one
5092
+ * pipe with no shell loop. Chapter three of the 1.67 arc, and the
5093
+ * refusals and budgets are the directory mode's own: only which files are
5094
+ * looked at changes, and what was dropped from the list is counted out
5095
+ * loud rather than silently.
5096
+ */
5097
+ const filesFrom = stringFlag(args, 'files-from');
5098
+ if (filesFrom !== undefined) {
5099
+ const listText = await readInput(filesFrom, t, maxInputFlag(args, t));
5100
+ const listed = listText
5101
+ .split('\n')
5102
+ .map((line) => line.trim())
5103
+ .filter((line) => line !== '');
5104
+ await checkDirectory('.', args, flagBudget, config, counter, level, t, locale, pricing, listed);
5105
+ return;
5106
+ }
5107
+
4923
5108
  const asDirectory = target !== undefined && target !== '-' ? await isDirectory(target) : false;
4924
5109
 
4925
5110
  if (asDirectory) {
@@ -5279,6 +5464,16 @@ async function commandServe(
5279
5464
  const limitUsd = config.spend?.monthlyUsd;
5280
5465
  const measured = standing !== null && standing.coverage !== 'none';
5281
5466
 
5467
+ /**
5468
+ * The limits policy's measured side, from the usage log `--log` points at —
5469
+ * read once at start, same staleness posture as the store. The store cannot
5470
+ * serve this: its records are provider buckets with no label and no
5471
+ * session, so per-label and per-session spend exist only in a usage log.
5472
+ * Without `--log`, every ceiling judges `cannot-tell` rather than judging
5473
+ * against a measurement nobody took.
5474
+ */
5475
+ const limitsIndex = await usageIndexFrom(args, pricing, t);
5476
+
5282
5477
  const server = buildServer({
5283
5478
  catalogue: pricing,
5284
5479
  position: () => ({
@@ -5291,6 +5486,9 @@ async function commandServe(
5291
5486
  // know which month the figure is about.
5292
5487
  window: standing === null ? null : { fromMs: standing.period.fromMs, toMs: standing.period.toMs },
5293
5488
  }),
5489
+ ...(config.limits === undefined ? {} : { limits: config.limits }),
5490
+ ...(limitsIndex === null ? {} : { measured: (call: { label?: string; session?: string }) => positionAt(limitsIndex, call) }),
5491
+ ...(config.waive === undefined ? {} : { waivers: config.waive }),
5294
5492
  });
5295
5493
 
5296
5494
  const socket = stringFlag(args, 'socket');
@@ -5314,6 +5512,12 @@ async function commandServe(
5314
5512
  if (limitUsd === undefined) {
5315
5513
  console.log(` ${c.dim(wrap(t.serve.noBudget(), 74, ' '))}`);
5316
5514
  }
5515
+ if (config.limits !== undefined && limitsIndex === null) {
5516
+ console.log(` ${c.yellow(wrap(t.serve.limitsNoLog(), 74, ' '))}`);
5517
+ }
5518
+ if (limitsIndex !== null && limitsIndex.unpriced > 0) {
5519
+ console.log(` ${c.yellow(wrap(t.serve.limitsUnpriced(limitsIndex.unpriced), 74, ' '))}`);
5520
+ }
5317
5521
  }
5318
5522
 
5319
5523
  /**
@@ -9218,6 +9422,37 @@ interface PromptScan {
9218
9422
  * files the gate does not check. One walk, one answer, and the budget resolution
9219
9423
  * comes along for free so `check` still sees exactly what it always did.
9220
9424
  */
9425
+ /**
9426
+ * Filters an explicit file list to what the directory walk would have
9427
+ * accepted: the same extensions, the same ignore globs, and only files that
9428
+ * exist — a path listed by `git diff --name-only` may be a deletion. The
9429
+ * dropped count is returned so the caller can say it, because a hook that
9430
+ * silently skips half its input reads as having checked everything.
9431
+ */
9432
+ async function fromList(
9433
+ root: string,
9434
+ listed: readonly string[],
9435
+ config: TrazumConfig,
9436
+ ): Promise<{ checkable: string[]; dropped: number }> {
9437
+ const extensions = (config.extensions ?? [...DEFAULT_EXTENSIONS, ...SOURCE_EXTENSIONS]).map((ext) =>
9438
+ ext.toLowerCase(),
9439
+ );
9440
+ const ignore = config.ignore ?? [];
9441
+ const checkable: string[] = [];
9442
+ for (const raw of listed) {
9443
+ const relativePath = raw.replace(/^\.\//, '');
9444
+ if (!extensions.some((ext) => relativePath.toLowerCase().endsWith(ext))) continue;
9445
+ if (ignore.some((pattern) => matchGlob(pattern, relativePath) || matchGlob(pattern, `${relativePath}/`))) continue;
9446
+ try {
9447
+ if (!(await stat(join(root, relativePath))).isFile()) continue;
9448
+ } catch {
9449
+ continue; // listed and gone: a deletion in the diff, not an error
9450
+ }
9451
+ checkable.push(relativePath);
9452
+ }
9453
+ return { checkable, dropped: listed.length - checkable.length };
9454
+ }
9455
+
9221
9456
  async function scanPrompts(
9222
9457
  root: string,
9223
9458
  args: Args,
@@ -9228,13 +9463,16 @@ async function scanPrompts(
9228
9463
  t: CliMessages,
9229
9464
  locale: Locale,
9230
9465
  pricing: PricingCatalogue,
9466
+ only?: readonly string[],
9231
9467
  ): Promise<PromptScan> {
9232
9468
  // Source files are walked alongside prompt files rather than opted into.
9233
9469
  // Requiring config to discover a marker somebody just wrote is how `eval` came
9234
9470
  // to be fully implemented and completely undiscoverable; an unmarked source
9235
9471
  // file costs one `includes()` and is dropped.
9236
9472
  const extensions = config.extensions ?? [...DEFAULT_EXTENSIONS, ...SOURCE_EXTENSIONS];
9237
- const { files, truncated } = await walkPrompts(root, { extensions, ignore: config.ignore });
9473
+ const walked = only === undefined ? await walkPrompts(root, { extensions, ignore: config.ignore }) : null;
9474
+ const files = walked === null ? [...only!] : walked.files;
9475
+ const truncated = walked === null ? false : walked.truncated;
9238
9476
 
9239
9477
  if (files.length === 0) {
9240
9478
  throw new Error(t.errors.noPromptsFound(root, extensions.join(' ')));
@@ -9304,8 +9542,25 @@ async function checkDirectory(
9304
9542
  t: CliMessages,
9305
9543
  locale: Locale,
9306
9544
  pricing: PricingCatalogue,
9545
+ only?: readonly string[],
9307
9546
  ): Promise<void> {
9308
9547
  const n = (value: number): string => value.toLocaleString(t.numberLocale);
9548
+
9549
+ /**
9550
+ * An explicit list with nothing checkable is a pass, not an error: the
9551
+ * whole point of `--files-from` is a hook over `git diff --name-only`,
9552
+ * and a commit that touches no prompt files must go through — with the
9553
+ * drop counted out loud, never silently.
9554
+ */
9555
+ if (only !== undefined) {
9556
+ const { checkable, dropped } = await fromList(root, only, config);
9557
+ if (!boolFlag(args, 'json')) {
9558
+ console.log(c.dim(t.check.filesFromSummary(checkable.length, only.length, dropped)));
9559
+ }
9560
+ if (checkable.length === 0) return;
9561
+ only = checkable;
9562
+ }
9563
+
9309
9564
  const { verdicts, declined, truncated, extensions } = await scanPrompts(
9310
9565
  root,
9311
9566
  args,
@@ -9316,6 +9571,7 @@ async function checkDirectory(
9316
9571
  t,
9317
9572
  locale,
9318
9573
  pricing,
9574
+ only,
9319
9575
  );
9320
9576
 
9321
9577
  /**
@@ -9327,7 +9583,16 @@ async function checkDirectory(
9327
9583
  * asked for and could not run is not a pass: that is the whole reason
9328
9584
  * `parseBaseline` throws on everything.
9329
9585
  */
9330
- const wantsBaseline = config.baseline !== undefined && boolFlag(args, 'baseline', true);
9586
+ /**
9587
+ * The baseline is a whole-repository gate: it compares every measured file
9588
+ * against the committed record, and handing it two changed files would
9589
+ * report the other thirty-eight as removed. So `--files-from` checks
9590
+ * budgets only, and says so once rather than silently narrowing a gate.
9591
+ */
9592
+ const wantsBaseline = config.baseline !== undefined && boolFlag(args, 'baseline', true) && only === undefined;
9593
+ if (config.baseline !== undefined && only !== undefined && !boolFlag(args, 'json')) {
9594
+ console.log(c.dim(t.check.filesFromNoBaseline()));
9595
+ }
9331
9596
  let baselineOutcome: {
9332
9597
  comparison: BaselineComparison;
9333
9598
  breached: BaselineBreach[];
@@ -10672,6 +10937,9 @@ async function main(): Promise<void> {
10672
10937
  case 'rollup':
10673
10938
  await commandRollup(args, t);
10674
10939
  break;
10940
+ case 'position':
10941
+ await commandPosition(args, config, pricing, t);
10942
+ break;
10675
10943
  case 'pulse':
10676
10944
  await commandPulse(args, t);
10677
10945
  break;
package/src/serve.ts CHANGED
@@ -25,8 +25,8 @@
25
25
 
26
26
  import { createServer } from 'node:http';
27
27
  import type { IncomingMessage, Server, ServerResponse } from 'node:http';
28
- import { answerCost } from '@trazum/core';
29
- import type { CostAnswer, PricingCatalogue } from '@trazum/core';
28
+ import { answerCost, judgeLimits } from '@trazum/core';
29
+ import type { CostAnswer, LimitsConfig, MeasuredPosition, PricingCatalogue, WaiveEntry } from '@trazum/core';
30
30
 
31
31
  /** Compiled in. See the module note: this is the inbound SSRF posture. */
32
32
  export const BIND_HOST = '127.0.0.1';
@@ -54,6 +54,15 @@ export interface ServeContext {
54
54
  * rather than implying it is current to the second.
55
55
  */
56
56
  position: () => { consumedUsd?: number; limitUsd?: number; window?: { fromMs: number; toMs: number } | null };
57
+ /** The `limits` block, when the config carries one. */
58
+ limits?: LimitsConfig;
59
+ /**
60
+ * The measured position for one call's scopes — from an index built once
61
+ * at start, same staleness posture as `position`.
62
+ */
63
+ measured?: (call: { label?: string; session?: string }) => MeasuredPosition;
64
+ /** The config's `waive` list — a silenced limit answers within, on the record. */
65
+ waivers?: readonly WaiveEntry[];
57
66
  }
58
67
 
59
68
  async function readBody(request: IncomingMessage): Promise<string> {
@@ -112,17 +121,66 @@ export function buildServer(context: ServeContext): Server {
112
121
  }
113
122
 
114
123
  const position = context.position();
115
- const answer: CostAnswer = answerCost(
116
- {
117
- model: typeof payload.model === 'string' ? payload.model : undefined,
118
- inputTokens: typeof payload.inputTokens === 'number' ? payload.inputTokens : undefined,
119
- outputTokens: typeof payload.outputTokens === 'number' ? payload.outputTokens : undefined,
120
- basis: payload.basis === 'heuristic' ? 'heuristic' : 'token-count',
121
- ...position,
122
- },
123
- { catalogue: context.catalogue },
124
- );
125
- send(response, 200, answer);
124
+ const call = {
125
+ model: typeof payload.model === 'string' ? payload.model : undefined,
126
+ inputTokens: typeof payload.inputTokens === 'number' ? payload.inputTokens : undefined,
127
+ outputTokens: typeof payload.outputTokens === 'number' ? payload.outputTokens : undefined,
128
+ basis: (payload.basis === 'heuristic' ? 'heuristic' : 'token-count') as 'heuristic' | 'token-count',
129
+ /**
130
+ * `label` and `session` scope the limits judgement and nothing else.
131
+ * The session value is used to look up measured spend and is never
132
+ * echoed back — the answer's judgement names the scope, not the key.
133
+ */
134
+ label: typeof payload.label === 'string' && payload.label !== '' ? payload.label : undefined,
135
+ session: typeof payload.session === 'string' && payload.session !== '' ? payload.session : undefined,
136
+ };
137
+ let answer: CostAnswer;
138
+ let policy;
139
+ try {
140
+ answer = answerCost(
141
+ {
142
+ model: call.model,
143
+ inputTokens: call.inputTokens,
144
+ outputTokens: call.outputTokens,
145
+ basis: call.basis,
146
+ ...position,
147
+ },
148
+ { catalogue: context.catalogue },
149
+ );
150
+ /**
151
+ * The limits policy, judged by the same function the gateway and the
152
+ * spend guard call — one judge, three doors. Always present: absent
153
+ * config answers `no-policy` rather than omitting the field, because
154
+ * a missing field and a judged absence are different answers.
155
+ */
156
+ policy = judgeLimits(
157
+ context.limits,
158
+ context.measured?.({
159
+ ...(call.label === undefined ? {} : { label: call.label }),
160
+ ...(call.session === undefined ? {} : { session: call.session }),
161
+ }) ?? { dayUsd: null, sessionUsd: null, labelUsd: null },
162
+ call,
163
+ {
164
+ catalogue: context.catalogue,
165
+ ...(context.waivers === undefined ? {} : { waivers: context.waivers }),
166
+ },
167
+ );
168
+ } catch (error) {
169
+ /*
170
+ The core refuses a figure that cannot mean what it says — a
171
+ negative token count, most importantly, because a negative estimate
172
+ lowers the projected spend and buys an approval. Before this catch
173
+ existed the refusal was an uncaught throw inside the request
174
+ handler: `{"inputTokens": -5}` took the whole oracle down. A bad
175
+ figure is the caller's error, and it gets the caller's status code.
176
+ */
177
+ send(response, 400, {
178
+ error: 'bad-request',
179
+ detail: error instanceof Error ? error.message : 'unjudgeable figures',
180
+ });
181
+ return;
182
+ }
183
+ send(response, 200, { ...answer, policy });
126
184
  })();
127
185
  });
128
186
  }