@trazum/cli 1.62.1 → 1.64.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/index.ts CHANGED
@@ -91,6 +91,7 @@ import {
91
91
  listModels,
92
92
  LOCALES,
93
93
  MAX_BASELINE_BYTES,
94
+ MAX_INPUT_CHARS,
94
95
  moneyIsComparable,
95
96
  mostSpecificMatch,
96
97
  assemble,
@@ -237,6 +238,7 @@ import {
237
238
  renderRankMarkdown,
238
239
  renderProfileMarkdown,
239
240
  } from './markdown.js';
241
+ import { renderProfileHtml, renderRollupHtml } from './html.js';
240
242
  import type { CliMessages } from './i18n/index.js';
241
243
 
242
244
  // --------------------------------------------------------------------------
@@ -317,6 +319,7 @@ const VALUE_FLAGS = new Set([
317
319
  'workload',
318
320
  'record',
319
321
  'max-ratio',
322
+ 'max-input',
320
323
  'max-day-usd',
321
324
  'max-session-usd',
322
325
  'csv-out',
@@ -329,6 +332,7 @@ const VALUE_FLAGS = new Set([
329
332
  'locale',
330
333
  'config',
331
334
  'markdown-out',
335
+ 'html-out',
332
336
  'otlp-out',
333
337
  'pricing',
334
338
  'prompt',
@@ -605,7 +609,7 @@ function disabledRules(args: Args, config: TrazumConfig): RuleId[] | undefined {
605
609
  * a threshold is set — `--max-growh 5` would have been ignored and the build
606
610
  * gone green. Silence is the wrong answer for a typo.
607
611
  */
608
- const GLOBAL_FLAGS = ['help', 'h', 'version', 'v', 'locale', 'json', 'config', 'pricing', 'pricing-live'];
612
+ const GLOBAL_FLAGS = ['help', 'h', 'version', 'v', 'locale', 'json', 'config', 'pricing', 'pricing-live', 'max-input'];
609
613
  const COMMAND_FLAGS: Record<string, string[]> = {
610
614
  optimize: [
611
615
  'level', 'model', 'calls', 'output-tokens', 'cache-hit-rate', 'batch',
@@ -615,7 +619,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
615
619
  ],
616
620
  check: ['max-tokens', 'level', 'exact-tokens', 'markdown-out', 'baseline'],
617
621
  baseline: ['model', 'calls', 'output-tokens', 'cache-hit-rate', 'batch', 'exact-tokens', 'out', 'o'],
618
- profile: ['json', 'pricing', 'pricing-live', 'against', 'what-if', 'markdown-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'],
622
+ 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'],
619
623
  plan: ['json', 'out', 'markdown-out', 'min-usd', 'pricing', 'pricing-live'],
620
624
  verify: ['against', 'gate', 'json', 'markdown-out', 'pricing', 'pricing-live'],
621
625
  history: ['store', 'json', 'markdown-out'],
@@ -631,7 +635,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
631
635
  rank: ['level', 'model', 'calls', 'output-tokens', 'batch', 'disable', 'prompt', 'markdown-out'],
632
636
  init: ['dry-run', 'yes', 'json', 'pricing', 'pricing-live'],
633
637
  conform: ['contract', 'json'],
634
- rollup: ['json'],
638
+ rollup: ['json', 'html-out'],
635
639
  pulse: ['json', 'max-stale-hours'],
636
640
  bench: ['workload', 'json', 'record', 'against', 'max-ratio'],
637
641
  write: ['answers', 'json', 'out', 'o', 'calls', 'avg-output'],
@@ -2060,7 +2064,8 @@ async function commandConform(args: Args, t: CliMessages): Promise<void> {
2060
2064
  throw new Error(t.conform.badContract(named, CONTRACT_NAMES.join(', ')));
2061
2065
  }
2062
2066
 
2063
- const text = target === '-' ? await readInput('-', t) : await readUsageLog(target, t);
2067
+ // A document or a log, not a prompt: deliberately uncapped (limit null).
2068
+ const text = target === '-' ? await readInput('-', t, null) : await readUsageLog(target, t);
2064
2069
  const report = conform(text, named === undefined ? {} : { contract: named });
2065
2070
 
2066
2071
  if (boolFlag(args, 'json')) {
@@ -2190,6 +2195,14 @@ async function commandRollup(args: Args, t: CliMessages): Promise<void> {
2190
2195
 
2191
2196
  const document = rollUp(inputs);
2192
2197
 
2198
+ // The HTML door, on both output paths — a side file that vanished under
2199
+ // --json is the fault the profile's --csv-out already taught this file.
2200
+ const htmlOut = stringFlag(args, 'html-out');
2201
+ if (htmlOut !== undefined) {
2202
+ await writeFile(htmlOut, renderRollupHtml(document, t), 'utf8');
2203
+ console.error(c.dim(t.html.written(htmlOut)));
2204
+ }
2205
+
2193
2206
  if (boolFlag(args, 'json')) {
2194
2207
  console.log(JSON.stringify(document, null, 2));
2195
2208
  // A rejected contribution is a machine missing from the total, so it gates
@@ -3618,7 +3631,7 @@ async function commandSemantic(
3618
3631
  pricing: PricingCatalogue,
3619
3632
  t: CliMessages,
3620
3633
  ): Promise<void> {
3621
- const prompt = await readInput(args.positional[0], t);
3634
+ const prompt = await readInput(args.positional[0], t, maxInputFlag(args, t));
3622
3635
  const modelId = stringFlag(args, 'model') ?? config.usage?.model ?? DEFAULT_USAGE.model;
3623
3636
  const model = pricing.byId.get(modelId) ?? getModel(DEFAULT_USAGE.model);
3624
3637
  const rates = { inputPerMTok: model.inputPerMTok, outputPerMTok: model.outputPerMTok };
@@ -4279,7 +4292,7 @@ async function commandRulesMeasure(
4279
4292
  // `walkPrompts` returns names relative to the root it was given, so the
4280
4293
  // root has to go back on. Reading them bare only worked from a run whose
4281
4294
  // cwd happened to be the root, which is the one case a first probe uses.
4282
- const raw = await readFile(join(root, file), 'utf8');
4295
+ const raw = capInput(await readFile(join(root, file), 'utf8'), file, maxInputFlag(args, t), t);
4283
4296
  // A marked source file contributes its marked prompts; anything else
4284
4297
  // contributes itself. The same rule `check` and `doctor` already follow,
4285
4298
  // so the three commands measure the same text.
@@ -4331,16 +4344,57 @@ async function commandRulesMeasure(
4331
4344
  console.log();
4332
4345
  }
4333
4346
 
4334
- async function readInput(source: string | undefined, t: CliMessages): Promise<string> {
4347
+ /**
4348
+ * The refusal ceiling for a prompt door: the flag, or the number written once
4349
+ * in `@trazum/core`.
4350
+ *
4351
+ * Raising it is deliberate — a flag typed by the person paying the wall time —
4352
+ * and nothing raises it by accident. Lowering it is equally legitimate: a CI
4353
+ * job that knows its prompts are small can make anything bigger a red build.
4354
+ */
4355
+ function maxInputFlag(args: Args, t: CliMessages): number {
4356
+ const raw = args.flags.get('max-input');
4357
+ if (raw === undefined || typeof raw === 'boolean') return MAX_INPUT_CHARS;
4358
+ const value = Number(raw);
4359
+ if (!Number.isFinite(value) || value <= 0) {
4360
+ throw new Error(t.errors.badMaxInput(String(raw)));
4361
+ }
4362
+ return Math.floor(value);
4363
+ }
4364
+
4365
+ /** Refuses a prompt past the ceiling — the size and the limit named, never a grind. */
4366
+ function capInput(text: string, source: string, limit: number, t: CliMessages): string {
4367
+ if (text.length > limit) {
4368
+ throw new Error(t.errors.inputTooLarge(source, text.length, limit));
4369
+ }
4370
+ return text;
4371
+ }
4372
+
4373
+ /**
4374
+ * Reads a prompt-sized input, held to the ceiling.
4375
+ *
4376
+ * `limit: null` is for the callers whose input is a *document or a log* — a
4377
+ * 200,000-line export is ordinary and its size is the product's subject, so
4378
+ * holding it to a prompt's ceiling would refuse the exact input `profile`
4379
+ * exists to read. The null is written at the call site, so a reader of the
4380
+ * caller sees the decision.
4381
+ */
4382
+ async function readInput(
4383
+ source: string | undefined,
4384
+ t: CliMessages,
4385
+ limit: number | null = null,
4386
+ ): Promise<string> {
4335
4387
  if (!source) {
4336
4388
  throw new Error(t.errors.missingInputFile());
4337
4389
  }
4338
4390
  if (source === '-') {
4339
4391
  const chunks: Buffer[] = [];
4340
4392
  for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
4341
- return Buffer.concat(chunks).toString('utf8');
4393
+ const text = Buffer.concat(chunks).toString('utf8');
4394
+ return limit === null ? text : capInput(text, 'stdin', limit, t);
4342
4395
  }
4343
- return readFile(source, 'utf8');
4396
+ const text = await readFile(source, 'utf8');
4397
+ return limit === null ? text : capInput(text, source, limit, t);
4344
4398
  }
4345
4399
 
4346
4400
  async function commandOptimize(
@@ -4392,6 +4446,7 @@ async function commandOptimize(
4392
4446
  unreadable.push({ label, path: promptPath });
4393
4447
  continue;
4394
4448
  }
4449
+ text = capInput(text, promptPath, maxInputFlag(args, t), t);
4395
4450
  const r = optimize(text, { level, usage: m.profile, locale, pricing });
4396
4451
  rows.push({
4397
4452
  label,
@@ -4442,7 +4497,7 @@ async function commandOptimize(
4442
4497
  }
4443
4498
 
4444
4499
  const target = args.positional[0];
4445
- const raw = await readInput(target, t);
4500
+ const raw = await readInput(target, t, maxInputFlag(args, t));
4446
4501
  const level = levelFlag(args, config, t);
4447
4502
 
4448
4503
  // A source file is not a prompt.
@@ -4850,7 +4905,7 @@ async function commandCheck(
4850
4905
  return;
4851
4906
  }
4852
4907
 
4853
- const prompt = await readInput(target, t);
4908
+ const prompt = await readInput(target, t, maxInputFlag(args, t));
4854
4909
  const n = (value: number): string => value.toLocaleString(t.numberLocale);
4855
4910
 
4856
4911
  // A single file falls back to the config budget for its own path, so
@@ -7284,11 +7339,26 @@ async function commandProfile(
7284
7339
  * comment. Written from the same message catalogue the terminal used, because
7285
7340
  * two renderings of one finding drift the moment they are worded twice.
7286
7341
  */
7287
- const markdownOut = stringFlag(args, 'markdown-out');
7288
- if (markdownOut !== undefined) {
7289
- await writeFile(
7290
- markdownOut,
7291
- renderProfileMarkdown({
7342
+ const markdownOut = stringFlag(args, 'markdown-out');
7343
+ const htmlOut = stringFlag(args, 'html-out');
7344
+ if (markdownOut !== undefined || htmlOut !== undefined) {
7345
+ /**
7346
+ * Derived here, not borrowed from the terminal path. Under `--json`
7347
+ * that path never runs, and the outer `levers` this block used to
7348
+ * reach for was an uninitialised binding — `profile --json
7349
+ * --markdown-out` crashed with a ReferenceError from 1.59 until the
7350
+ * HTML door's test drove both flags together and found it. The flag
7351
+ * did not silently do nothing; it loudly did nothing, and no test had
7352
+ * ever asked.
7353
+ */
7354
+ const levers = billLevers(report, { catalogue: pricing });
7355
+ const cache = cacheEconomics(report.total);
7356
+ /**
7357
+ * One input object for both doors, built once: the HTML file and the
7358
+ * Markdown file are two projections of the same figures, and building
7359
+ * the input twice is how two renderings of one bill start disagreeing.
7360
+ */
7361
+ const renderInput = ({
7292
7362
  report,
7293
7363
  levers,
7294
7364
  cache,
@@ -7330,10 +7400,15 @@ async function commandProfile(
7330
7400
  },
7331
7401
  }
7332
7402
  : {}),
7333
- }),
7334
- 'utf8',
7335
- );
7336
- notice(c.dim(t.report.wroteTo(markdownOut)));
7403
+ });
7404
+ if (markdownOut !== undefined) {
7405
+ await writeFile(markdownOut, renderProfileMarkdown(renderInput), 'utf8');
7406
+ notice(c.dim(t.report.wroteTo(markdownOut)));
7407
+ }
7408
+ if (htmlOut !== undefined) {
7409
+ await writeFile(htmlOut, renderProfileHtml(renderInput), 'utf8');
7410
+ notice(c.dim(t.html.written(htmlOut)));
7411
+ }
7337
7412
  }
7338
7413
 
7339
7414
  /**
@@ -8862,7 +8937,7 @@ async function commandRoute(args: Args, pricing: PricingCatalogue, t: CliMessage
8862
8937
  return;
8863
8938
  }
8864
8939
 
8865
- const prompt = await readFile(promptPath, 'utf8');
8940
+ const prompt = capInput(await readFile(promptPath, 'utf8'), promptPath, maxInputFlag(args, t), t);
8866
8941
  const inputs = parseCases(await readFile(casesPath, 'utf8'));
8867
8942
  if (inputs.length === 0) throw new Error(t.errors.evalNoCases(casesPath));
8868
8943
 
@@ -9155,7 +9230,12 @@ async function scanPrompts(
9155
9230
  const declined: Array<{ path: string; line: number; detail: string }> = [];
9156
9231
 
9157
9232
  for (const relativePath of files) {
9158
- const text = await readFile(join(root, relativePath), 'utf8');
9233
+ const text = capInput(
9234
+ await readFile(join(root, relativePath), 'utf8'),
9235
+ relativePath,
9236
+ maxInputFlag(args, t),
9237
+ t,
9238
+ );
9159
9239
  // Budgets are keyed on paths as written in the repository, so a pattern like
9160
9240
  // `prompts/**` has to be matched against the path including the root the
9161
9241
  // user passed — not against the name relative to it.
@@ -9452,7 +9532,7 @@ function joinPosix(root: string, relativePath: string): string {
9452
9532
  * never "delete this", and nothing here edits the prompt.
9453
9533
  */
9454
9534
  async function commandPrune(args: Args, t: CliMessages): Promise<void> {
9455
- const prompt = await readInput(args.positional[0], t);
9535
+ const prompt = await readInput(args.positional[0], t, maxInputFlag(args, t));
9456
9536
 
9457
9537
  const casesPath = stringFlag(args, 'cases');
9458
9538
  if (!casesPath) throw new Error(t.errors.evalNeedsCases());
@@ -9540,7 +9620,7 @@ async function commandEval(
9540
9620
  t: CliMessages,
9541
9621
  locale: Locale,
9542
9622
  ): Promise<void> {
9543
- const prompt = await readInput(args.positional[0], t);
9623
+ const prompt = await readInput(args.positional[0], t, maxInputFlag(args, t));
9544
9624
  const level = levelFlag(args, config, t);
9545
9625
 
9546
9626
  const casesPath = stringFlag(args, 'cases');
@@ -9727,8 +9807,8 @@ async function commandDiff(
9727
9807
  }
9728
9808
 
9729
9809
  const [before, after] = await Promise.all([
9730
- readInput(beforePath, t),
9731
- readInput(afterPath, t),
9810
+ readInput(beforePath, t, maxInputFlag(args, t)),
9811
+ readInput(afterPath, t, maxInputFlag(args, t)),
9732
9812
  ]);
9733
9813
 
9734
9814
  const comparison = comparePrompts(before, after, {
@@ -10203,7 +10283,7 @@ async function commandRank(
10203
10283
  let skipped = 0;
10204
10284
 
10205
10285
  for (const file of files) {
10206
- const raw = await readFile(join(root, file), 'utf8');
10286
+ const raw = capInput(await readFile(join(root, file), 'utf8'), file, maxInputFlag(args, t), t);
10207
10287
 
10208
10288
  // A source file contributes its marked prompt, or nothing. Ranking
10209
10289
  // `src/prompts.ts` by the size of its imports would put the wrong file at
@@ -10684,7 +10764,7 @@ async function commandDoctor(
10684
10764
  let skipped = 0;
10685
10765
 
10686
10766
  for (const file of files) {
10687
- const raw = await readFile(join(root, file), 'utf8');
10767
+ const raw = capInput(await readFile(join(root, file), 'utf8'), file, maxInputFlag(args, t), t);
10688
10768
 
10689
10769
  // Same contract as `rank`: a source file contributes its marked prompt or
10690
10770
  // nothing, and an unmarked one is counted rather than allowed to abort a
@@ -10986,7 +11066,7 @@ async function diffDirectories(
10986
11066
 
10987
11067
  /** The prompt at a path, or null when the file holds no marked prompt. */
10988
11068
  const textAt = async (root: string, file: string): Promise<string | null> => {
10989
- const raw = await readFile(join(root, file), 'utf8');
11069
+ const raw = capInput(await readFile(join(root, file), 'utf8'), file, maxInputFlag(args, t), t);
10990
11070
  let source: { text: string; model?: string } | null;
10991
11071
  try {
10992
11072
  source = sourceFileOf(file, raw, pricing, stringFlag(args, 'prompt'));