@trazum/cli 1.64.0 → 1.66.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/serve.d.ts CHANGED
@@ -23,7 +23,7 @@
23
23
  * into a hot path.
24
24
  */
25
25
  import type { Server } from 'node:http';
26
- import type { PricingCatalogue } from '@trazum/core';
26
+ import type { LimitsConfig, MeasuredPosition, PricingCatalogue, WaiveEntry } from '@trazum/core';
27
27
  /** Compiled in. See the module note: this is the inbound SSRF posture. */
28
28
  export declare const BIND_HOST = "127.0.0.1";
29
29
  export declare const DEFAULT_PORT = 7317;
@@ -54,6 +54,18 @@ export interface ServeContext {
54
54
  toMs: number;
55
55
  } | null;
56
56
  };
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: {
64
+ label?: string;
65
+ session?: string;
66
+ }) => MeasuredPosition;
67
+ /** The config's `waive` list — a silenced limit answers within, on the record. */
68
+ waivers?: readonly WaiveEntry[];
57
69
  }
58
70
  export declare function buildServer(context: ServeContext): Server;
59
71
  export interface ListenTarget {
@@ -1 +1 @@
1
- {"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../src/serve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,KAAK,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAC;AAEzE,OAAO,KAAK,EAAc,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEjE,0EAA0E;AAC1E,eAAO,MAAM,SAAS,cAAc,CAAC;AAErC,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,UAAY,CAAC;AAExC,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,gBAAgB,CAAC;IAC5B;;;;;;;;OAQG;IACH,QAAQ,EAAE,MA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,YAAY,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,gBAAgB,CAAC;IAC5E,kFAAkF;IAClF,OAAO,CAAC,EAAE,SAAS,UAAU,EAAE,CAAC;CACjC;AAyBD,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,MAAM,CA+FzD;AAED,MAAM,WAAW,YAAY;IAC3B,uEAAuE;IACvE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAa5E"}
package/dist/serve.js CHANGED
@@ -23,7 +23,7 @@
23
23
  * into a hot path.
24
24
  */
25
25
  import { createServer } from 'node:http';
26
- import { answerCost } from '@trazum/core';
26
+ import { answerCost, judgeLimits } from '@trazum/core';
27
27
  /** Compiled in. See the module note: this is the inbound SSRF posture. */
28
28
  export const BIND_HOST = '127.0.0.1';
29
29
  export const DEFAULT_PORT = 7317;
@@ -87,14 +87,59 @@ export function buildServer(context) {
87
87
  return;
88
88
  }
89
89
  const position = context.position();
90
- const answer = answerCost({
90
+ const call = {
91
91
  model: typeof payload.model === 'string' ? payload.model : undefined,
92
92
  inputTokens: typeof payload.inputTokens === 'number' ? payload.inputTokens : undefined,
93
93
  outputTokens: typeof payload.outputTokens === 'number' ? payload.outputTokens : undefined,
94
- basis: payload.basis === 'heuristic' ? 'heuristic' : 'token-count',
95
- ...position,
96
- }, { catalogue: context.catalogue });
97
- send(response, 200, answer);
94
+ basis: (payload.basis === 'heuristic' ? 'heuristic' : 'token-count'),
95
+ /**
96
+ * `label` and `session` scope the limits judgement and nothing else.
97
+ * The session value is used to look up measured spend and is never
98
+ * echoed back — the answer's judgement names the scope, not the key.
99
+ */
100
+ label: typeof payload.label === 'string' && payload.label !== '' ? payload.label : undefined,
101
+ session: typeof payload.session === 'string' && payload.session !== '' ? payload.session : undefined,
102
+ };
103
+ let answer;
104
+ let policy;
105
+ try {
106
+ answer = answerCost({
107
+ model: call.model,
108
+ inputTokens: call.inputTokens,
109
+ outputTokens: call.outputTokens,
110
+ basis: call.basis,
111
+ ...position,
112
+ }, { catalogue: context.catalogue });
113
+ /**
114
+ * The limits policy, judged by the same function the gateway and the
115
+ * spend guard call — one judge, three doors. Always present: absent
116
+ * config answers `no-policy` rather than omitting the field, because
117
+ * a missing field and a judged absence are different answers.
118
+ */
119
+ policy = judgeLimits(context.limits, context.measured?.({
120
+ ...(call.label === undefined ? {} : { label: call.label }),
121
+ ...(call.session === undefined ? {} : { session: call.session }),
122
+ }) ?? { dayUsd: null, sessionUsd: null, labelUsd: null }, call, {
123
+ catalogue: context.catalogue,
124
+ ...(context.waivers === undefined ? {} : { waivers: context.waivers }),
125
+ });
126
+ }
127
+ catch (error) {
128
+ /*
129
+ The core refuses a figure that cannot mean what it says — a
130
+ negative token count, most importantly, because a negative estimate
131
+ lowers the projected spend and buys an approval. Before this catch
132
+ existed the refusal was an uncaught throw inside the request
133
+ handler: `{"inputTokens": -5}` took the whole oracle down. A bad
134
+ figure is the caller's error, and it gets the caller's status code.
135
+ */
136
+ send(response, 400, {
137
+ error: 'bad-request',
138
+ detail: error instanceof Error ? error.message : 'unjudgeable figures',
139
+ });
140
+ return;
141
+ }
142
+ send(response, 200, { ...answer, policy });
98
143
  })();
99
144
  });
100
145
  }
package/dist/serve.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"serve.js","sourceRoot":"","sources":["../src/serve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C,0EAA0E;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AQD,MAAM,UAAU,MAAM,CAAC,MAAc,EAAE,MAAoB;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC7B,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAO,CAAC,CAAC,CAAC;YAC5D,OAAO;QACT,CAAC;QACD,oDAAoD;QACpD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,IAAI,YAAY,EAAE,SAAS,EAAE,GAAG,EAAE;YACzD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YACjC,OAAO,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QAC9G,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trazum/cli",
3
- "version": "1.64.0",
3
+ "version": "1.66.0",
4
4
  "description": "Trazum CLI: find where your LLM bill goes, price every finding per month, and enforce token budgets in CI.",
5
5
  "license": "MIT",
6
6
  "author": "David Mu\u00f1oz Rey",
@@ -37,7 +37,7 @@
37
37
  "prepublishOnly": "npm run build && npm test"
38
38
  },
39
39
  "dependencies": {
40
- "@trazum/core": "1.64.0"
40
+ "@trazum/core": "1.66.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^26.2.0",
@@ -35,7 +35,15 @@ import { once } from 'node:events';
35
35
  import { createServer } from 'node:http';
36
36
  import type { IncomingMessage, Server, ServerResponse } from 'node:http';
37
37
  import { estimateTokens, gatewayDecision, streamingUsageReader, usageFromResponse } from '@trazum/core';
38
- import type { GatewayDecision, GatewayPolicy, GatewayStanding, PricingCatalogue } from '@trazum/core';
38
+ import type {
39
+ GatewayDecision,
40
+ GatewayPolicy,
41
+ GatewayStanding,
42
+ LimitsConfig,
43
+ MeasuredPosition,
44
+ PricingCatalogue,
45
+ WaiveEntry,
46
+ } from '@trazum/core';
39
47
 
40
48
  /** Compiled in. See the module note. */
41
49
  export const BIND_HOST = '127.0.0.1';
@@ -179,6 +187,15 @@ export interface GatewayContext {
179
187
  policy: GatewayPolicy;
180
188
  /** Where the budget stands, refreshed by the caller — never read per request. */
181
189
  standing: () => GatewayStanding | null;
190
+ /** The `limits` block, when the config carries one. */
191
+ limits?: LimitsConfig;
192
+ /**
193
+ * The measured position for one call's scopes — from an index built once
194
+ * at start, like `standing`. Never a file read in the request path.
195
+ */
196
+ position?: (call: { label?: string; session?: string }) => MeasuredPosition;
197
+ /** The config's `waive` list — a silenced limit forwards, on the record. */
198
+ waivers?: readonly WaiveEntry[];
182
199
  /**
183
200
  * Called after a forwarded call returns, with the provider's own counts.
184
201
  *
@@ -250,6 +267,7 @@ function describe(body: string, provider: string, modelFromPath: string | null):
250
267
  inputTokens: number | null;
251
268
  maxOutputTokens: number | null;
252
269
  label: string | null;
270
+ session: string | null;
253
271
  } | null {
254
272
  let parsed: unknown;
255
273
  try {
@@ -307,14 +325,26 @@ function describe(body: string, provider: string, modelFromPath: string | null):
307
325
  * workload they never named.
308
326
  */
309
327
  label: labelOf(request),
328
+ /**
329
+ * `metadata.trazum_session` — the same seam as the label, for the same
330
+ * reason: a per-session ceiling can only bind conversations that name
331
+ * themselves, and inferring one would attribute spend to a conversation
332
+ * nobody declared. The value is used to judge and never forwarded,
333
+ * recorded, or printed.
334
+ */
335
+ session: metadataField(request, 'trazum_session'),
310
336
  };
311
337
  }
312
338
 
313
339
  function labelOf(request: Record<string, unknown>): string | null {
340
+ return metadataField(request, 'trazum_label');
341
+ }
342
+
343
+ function metadataField(request: Record<string, unknown>, key: string): string | null {
314
344
  const metadata = request.metadata;
315
345
  if (typeof metadata !== 'object' || metadata === null || Array.isArray(metadata)) return null;
316
- const label = (metadata as Record<string, unknown>).trazum_label;
317
- return typeof label === 'string' && label.trim() !== '' ? label : null;
346
+ const value = (metadata as Record<string, unknown>)[key];
347
+ return typeof value === 'string' && value.trim() !== '' ? value : null;
318
348
  }
319
349
 
320
350
  /** The refusal, as the caller's SDK will receive it. */
@@ -329,6 +359,9 @@ function refusalBody(decision: Extract<GatewayDecision, { kind: 'refuse' }>): st
329
359
  standing: decision.standing,
330
360
  estimatedUsd: decision.estimatedUsd,
331
361
  alternatives: decision.alternatives,
362
+ // The limits policy, judged by the same function every door calls —
363
+ // the 402 body carries the judgement, not a paraphrase of it.
364
+ policy: decision.policy,
332
365
  },
333
366
  null,
334
367
  2,
@@ -379,7 +412,20 @@ export function buildGateway(context: GatewayContext): Server {
379
412
  const decision = gatewayDecision(
380
413
  { provider: context.provider, ...described },
381
414
  context.standing(),
382
- { catalogue: context.catalogue, policy: context.policy },
415
+ {
416
+ catalogue: context.catalogue,
417
+ policy: context.policy,
418
+ ...(context.limits === undefined ? {} : { limits: context.limits }),
419
+ ...(context.waivers === undefined ? {} : { waivers: context.waivers }),
420
+ ...(context.position === undefined
421
+ ? {}
422
+ : {
423
+ position: context.position({
424
+ ...(described.label === null ? {} : { label: described.label }),
425
+ ...(described.session === null ? {} : { session: described.session }),
426
+ }),
427
+ }),
428
+ },
383
429
  );
384
430
 
385
431
  if (decision.kind === 'refuse') {
package/src/i18n/en.ts CHANGED
@@ -56,6 +56,7 @@ ${bold('USAGE')}
56
56
  trazum prune <file> --cases <file> --yes
57
57
  trazum where [file]
58
58
  trazum conform <file|-> [--contract <name>]
59
+ trazum schema <contract>
59
60
  trazum rollup <document...|dir> [--json] [--html-out <file>]
60
61
  trazum pulse [--max-stale-hours <n>]
61
62
  trazum bench [--workload <id>] [--record <file>] [--against <file> --max-ratio <n>] [--json]
@@ -211,6 +212,10 @@ ${bold('OPTIONS FOR gateway')}
211
212
  What happens when the gateway cannot judge a call
212
213
  — no budget, nothing measured, an unpriced model.
213
214
  --port <n> | --socket <p> Where to listen. Loopback only, always.
215
+ --log <usage.jsonl> The usage log the limits policy is measured
216
+ against, read once at start — same flag as
217
+ serve's, same reason: label and session spend
218
+ live in a usage log, not in the store.
214
219
 
215
220
  Stands between your SDK and the provider, speaking their wire format, so no
216
221
  code changes. Usage is measured from the provider's own response as it comes
@@ -568,6 +573,11 @@ ${bold('OPTIONS FOR plan')}
568
573
  ${bold('OPTIONS FOR serve')}
569
574
  --port <n> Port on 127.0.0.1. Default: 7317.
570
575
  --socket <path> Listen on a Unix socket instead of a port.
576
+ --log <usage.jsonl> The usage log the limits policy is measured
577
+ against, read once at start. Per-label and
578
+ per-session spend live in a usage log, not in
579
+ the store; without this, every ceiling in
580
+ "limits" answers cannot-tell.
571
581
 
572
582
  Answers the two questions that matter at call time — what will this cost,
573
583
  and is there budget left — in single-digit milliseconds, so an agent or a
@@ -777,6 +787,13 @@ ${bold('CONFIG FILE')}
777
787
  spend { "maxUsd": 200, "byLabel": { "chat": 40 } } — money budgets for
778
788
  "trazum profile", in dollars. A budgeted label with no calls in
779
789
  the log is reported as not measured, never as a pass
790
+ limits { "dayUsd": 25, "sessionUsd": 1.5, "byLabel": { "chat": 5 } } —
791
+ the enforcement policy: USD ceilings judged before a call is
792
+ made, at whichever door it arrives (the gateway's 402, "serve",
793
+ spend_guard over MCP). Separate from "spend" because a report
794
+ gate and a call refusal are read at different moments; every
795
+ ceiling must be a positive number, because a ceiling of 0 is an
796
+ outage written as a policy
780
797
  owners { "patterns": { "payments": ["billing-*"] },
781
798
  "shared": { "search": { "payments": 0.6, "support": 0.4 } },
782
799
  "budgets": { "payments": 400 } } — whose budget each workload
@@ -1608,6 +1625,12 @@ ${bold('EXAMPLES')}
1608
1625
  written: (path) => `HTML report written to ${path}.`,
1609
1626
  },
1610
1627
 
1628
+ schema: {
1629
+ noTarget: (known) =>
1630
+ `Name a contract to print its JSON Schema. Known: ${known}. The schema validates with any draft 2020-12 validator — no Trazum required.`,
1631
+ unknown: (name, known) => `"${name}" is not a contract. Known: ${known}.`,
1632
+ },
1633
+
1611
1634
  bench: {
1612
1635
  heading: () => 'This machine, measured',
1613
1636
  machine: (node, platform, cpuCount, cpuModel) =>
@@ -2453,6 +2476,10 @@ ${bold('EXAMPLES')}
2453
2476
 
2454
2477
  serve: {
2455
2478
  listening: (where) => `Answering on ${where}`,
2479
+ limitsNoLog: () =>
2480
+ 'A limits policy is configured and no --log was given, so every ceiling will answer cannot-tell: per-label and per-session spend live in a usage log, not in the store. Point --log at your usage log to make the ceilings judgeable.',
2481
+ limitsUnpriced: (count) =>
2482
+ `${count} record(s) in the --log name a model the catalogue cannot price. They contribute nothing to any measured figure — money the limits policy cannot see, said here rather than hidden.`,
2456
2483
  loopbackOnly: () =>
2457
2484
  'Loopback only, and there is no flag to change that: this holds your spend, your model mix and your budgets, and would answer whoever asked. There is no auth for the same reason — a token checked over loopback is theatre.',
2458
2485
  measuredFrom: (usd) =>
package/src/i18n/es.ts CHANGED
@@ -43,6 +43,7 @@ ${bold('USO')}
43
43
  trazum prune <fichero> --cases <fichero> --yes
44
44
  trazum where [fichero]
45
45
  trazum conform <fichero|-> [--contract <nombre>]
46
+ trazum schema <contrato>
46
47
  trazum rollup <documento...|dir> [--json] [--html-out <fichero>]
47
48
  trazum pulse [--max-stale-hours <n>]
48
49
  trazum bench [--workload <id>] [--record <fichero>] [--against <fichero> --max-ratio <n>] [--json]
@@ -203,6 +204,11 @@ ${bold('OPCIONES DE gateway')}
203
204
  juzgar una llamada — sin presupuesto, sin nada
204
205
  medido, un modelo sin precio.
205
206
  --port <n> | --socket <p> Dónde escuchar. Solo loopback, siempre.
207
+ --log <uso.jsonl> El log de uso contra el que se mide la política
208
+ de limits, leído una vez al arrancar — el mismo
209
+ flag que en serve, por la misma razón: el gasto
210
+ por etiqueta y sesión vive en un log de uso, no
211
+ en el almacén.
206
212
 
207
213
  Se pone entre tu SDK y el proveedor, hablando su formato, así que no hay
208
214
  cambios de código. El consumo se mide desde la propia respuesta del proveedor
@@ -582,6 +588,11 @@ ${bold('OPCIONES DE plan')}
582
588
  ${bold('OPCIONES DE serve')}
583
589
  --port <n> Puerto en 127.0.0.1. Por defecto: 7317.
584
590
  --socket <ruta> Escucha en un socket Unix en vez de un puerto.
591
+ --log <uso.jsonl> El log de uso contra el que se mide la política
592
+ de limits, leído una vez al arrancar. El gasto
593
+ por etiqueta y por sesión vive en un log de uso,
594
+ no en el almacén; sin esto, cada techo de
595
+ "limits" responde cannot-tell.
585
596
 
586
597
  Responde las dos preguntas que importan en el momento de la llamada — cuánto
587
598
  va a costar esto y si queda presupuesto — en milisegundos de un solo dígito,
@@ -801,6 +812,13 @@ ${bold('FICHERO DE CONFIGURACIÓN')}
801
812
  spend { "maxUsd": 200, "byLabel": { "chat": 40 } } — presupuestos en
802
813
  dólares para "trazum profile". Una etiqueta con presupuesto y sin
803
814
  llamadas se informa como no medida, nunca como aprobada
815
+ limits { "dayUsd": 25, "sessionUsd": 1.5, "byLabel": { "chat": 5 } } —
816
+ la política de aplicación: techos en USD juzgados antes de hacer
817
+ una llamada, en la puerta a la que llegue (el 402 del gateway,
818
+ "serve", spend_guard por MCP). Separado de "spend" porque una
819
+ puerta de informe y un rechazo de llamada se leen en momentos
820
+ distintos; cada techo debe ser un número positivo, porque un
821
+ techo de 0 es una caída escrita como política
804
822
  owners { "patterns": { "payments": ["billing-*"] },
805
823
  "shared": { "search": { "payments": 0.6, "support": 0.4 } },
806
824
  "budgets": { "payments": 400 } } — de quién es el presupuesto de
@@ -1620,6 +1638,12 @@ ${bold('EJEMPLOS')}
1620
1638
  written: (path) => `Informe HTML escrito en ${path}.`,
1621
1639
  },
1622
1640
 
1641
+ schema: {
1642
+ noTarget: (known) =>
1643
+ `Nombra un contrato para imprimir su JSON Schema. Conocidos: ${known}. El esquema valida con cualquier validador draft 2020-12 — sin Trazum.`,
1644
+ unknown: (name, known) => `"${name}" no es un contrato. Conocidos: ${known}.`,
1645
+ },
1646
+
1623
1647
  bench: {
1624
1648
  heading: () => 'Esta máquina, medida',
1625
1649
  machine: (node, platform, cpuCount, cpuModel) =>
@@ -2462,6 +2486,10 @@ ${bold('EJEMPLOS')}
2462
2486
 
2463
2487
  serve: {
2464
2488
  listening: (where) => `Respondiendo en ${where}`,
2489
+ limitsNoLog: () =>
2490
+ 'Hay una política de limits configurada y no se dio --log, así que cada techo responderá cannot-tell: el gasto por etiqueta y por sesión vive en un log de uso, no en el almacén. Apunta --log a tu log de uso para que los techos sean juzgables.',
2491
+ limitsUnpriced: (count) =>
2492
+ `${count} registro(s) del --log nombran un modelo que el catálogo no puede tasar. No aportan nada a ninguna cifra medida — dinero que la política de limits no puede ver, dicho aquí en vez de escondido.`,
2465
2493
  loopbackOnly: () =>
2466
2494
  'Solo loopback, y no hay flag para cambiarlo: esto guarda tu gasto, tu mezcla de modelos y tus presupuestos, y le respondería a quien preguntara. No hay autenticación por la misma razón — un token comprobado sobre loopback es teatro.',
2467
2495
  measuredFrom: (usd) =>
package/src/i18n/types.ts CHANGED
@@ -551,6 +551,12 @@ export interface CliMessages {
551
551
  written(path: string): string;
552
552
  };
553
553
 
554
+ /** The portable format: a JSON Schema per contract, for validators that are not Trazum. */
555
+ schema: {
556
+ noTarget(known: string): string;
557
+ unknown(name: string, known: string): string;
558
+ };
559
+
554
560
  bench: {
555
561
  heading(): string;
556
562
  machine(node: string, platform: string, cpuCount: number, cpuModel: string | null): string;
@@ -1500,6 +1506,14 @@ export interface CliMessages {
1500
1506
  listening(where: string): string;
1501
1507
  /** Why there is no --host, and why that is not an omission. */
1502
1508
  loopbackOnly(): string;
1509
+ /**
1510
+ * A `limits` block is configured and no `--log` was given, so every
1511
+ * ceiling will judge cannot-tell. Said at start rather than discovered
1512
+ * one refusal at a time. Shared by both HTTP doors.
1513
+ */
1514
+ limitsNoLog(): string;
1515
+ /** Unpriced records in the `--log` — money the measured side cannot see. */
1516
+ limitsUnpriced(count: number): string;
1503
1517
  measuredFrom(usd: string): string;
1504
1518
  nothingMeasured(dir: string): string;
1505
1519
  noBudget(): string;
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ import { gunzipSync } from 'node:zlib';
9
9
 
10
10
  import {
11
11
  CONTRACT_NAMES,
12
+ contractSchema,
12
13
  applyRewrites,
13
14
  BASELINE_FILENAME,
14
15
  BASELINE_VERSION,
@@ -105,7 +106,10 @@ import {
105
106
  detectTextLanguage,
106
107
  dictionaryStanding,
107
108
  languagesWithStanding,
109
+ indexUsage,
110
+ parseUsageLine,
108
111
  plannedCalls,
112
+ positionAt,
109
113
  PRICING_LAST_REVIEWED,
110
114
  profilePrompt,
111
115
  profileToCsv,
@@ -276,6 +280,7 @@ interface Args {
276
280
  const VALUE_FLAGS = new Set([
277
281
  'answers',
278
282
  'calls',
283
+ 'log',
279
284
  'avg-output',
280
285
  'a',
281
286
  'at',
@@ -466,6 +471,28 @@ const LOG_EXTENSIONS = ['.jsonl', '.ndjson', '.log', '.json'];
466
471
  * would be a figure quietly missing a day, the failure this repository
467
472
  * refuses everywhere it can occur.
468
473
  */
474
+ /**
475
+ * The measured side of the `limits` policy, from `--log` — shared by the two
476
+ * HTTP doors. Returns null when no log was named: the doors then judge every
477
+ * ceiling `cannot-tell`, which is the honest answer to "what has this label
478
+ * spent" when nobody handed over the record of what labels spent.
479
+ */
480
+ async function usageIndexFrom(
481
+ args: Args,
482
+ pricing: PricingCatalogue,
483
+ t: CliMessages,
484
+ ): Promise<ReturnType<typeof indexUsage> | null> {
485
+ const logPath = stringFlag(args, 'log');
486
+ if (logPath === undefined) return null;
487
+ const text = await readUsageLog(logPath, t);
488
+ const records = text
489
+ .split('\n')
490
+ .filter((line) => line.trim() !== '')
491
+ .map((line) => parseUsageLine(line))
492
+ .filter((record): record is NonNullable<ReturnType<typeof parseUsageLine>> => record !== null);
493
+ return indexUsage(records, { catalogue: pricing });
494
+ }
495
+
469
496
  async function readUsageLog(file: string, t: CliMessages): Promise<string> {
470
497
  if (!file.endsWith('.gz')) return readFile(file, 'utf8');
471
498
  const compressed = await readFile(file);
@@ -626,7 +653,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
626
653
  connect: ['since', 'until', 'payload', 'store', 'json', 'out', 'markdown-out', 'pricing', 'pricing-live', 'dry-run'],
627
654
  store: ['prune', 'keep', 'json', 'pricing', 'pricing-live', 'dry-run'],
628
655
  watch: ['once', 'interval', 'since', 'payload', 'webhook', 'json', 'pricing', 'pricing-live'],
629
- serve: ['port', 'socket', 'pricing', 'pricing-live'],
656
+ serve: ['port', 'socket', 'log', 'pricing', 'pricing-live'],
630
657
  route: ['prompt-file', 'cases', 'label', 'concurrency', 'json', 'yes', 'pricing', 'pricing-live'],
631
658
  eval: ['cases', 'level', 'concurrency', 'export', 'out', 'o', 'model'],
632
659
  prune: ['cases', 'concurrency', 'json', 'yes'],
@@ -635,12 +662,13 @@ const COMMAND_FLAGS: Record<string, string[]> = {
635
662
  rank: ['level', 'model', 'calls', 'output-tokens', 'batch', 'disable', 'prompt', 'markdown-out'],
636
663
  init: ['dry-run', 'yes', 'json', 'pricing', 'pricing-live'],
637
664
  conform: ['contract', 'json'],
665
+ schema: [],
638
666
  rollup: ['json', 'html-out'],
639
667
  pulse: ['json', 'max-stale-hours'],
640
668
  bench: ['workload', 'json', 'record', 'against', 'max-ratio'],
641
669
  write: ['answers', 'json', 'out', 'o', 'calls', 'avg-output'],
642
670
  feedback: [],
643
- gateway: ['on-cannot-tell', 'port', 'socket', 'pricing', 'pricing-live'],
671
+ gateway: ['on-cannot-tell', 'port', 'socket', 'log', 'pricing', 'pricing-live'],
644
672
  ladder: ['pricing', 'pricing-live', 'since', 'until', 'label'],
645
673
  experiment: ['a', 'b', 'min-outcomes', 'pricing', 'pricing-live'],
646
674
  quality: ['label', 'at', 'gate', 'pricing', 'pricing-live'],
@@ -2889,6 +2917,26 @@ async function readBenchBaseline(path: string, t: CliMessages): Promise<BenchBas
2889
2917
  return candidate;
2890
2918
  }
2891
2919
 
2920
+
2921
+ /**
2922
+ * `trazum schema <contract>` — the format, portable.
2923
+ *
2924
+ * Prints the JSON Schema for a named contract, so a document can be checked
2925
+ * by any off-the-shelf validator with no Trazum installed. The output is the
2926
+ * schema and nothing else — pipeable by construction, like `--json`
2927
+ * everywhere — and the refusal with no or an unknown name lists every
2928
+ * contract, derived from the same list `--contract` accepts, the way the
2929
+ * gateway names its providers.
2930
+ */
2931
+ function commandSchema(args: Args, t: CliMessages): void {
2932
+ const name = args.positional[0];
2933
+ if (name === undefined) throw new Error(t.schema.noTarget(CONTRACT_NAMES.join(', ')));
2934
+ if (!(CONTRACT_NAMES as readonly string[]).includes(name)) {
2935
+ throw new Error(t.schema.unknown(name, CONTRACT_NAMES.join(', ')));
2936
+ }
2937
+ console.log(JSON.stringify(contractSchema(name as (typeof CONTRACT_NAMES)[number]), null, 2));
2938
+ }
2939
+
2892
2940
  async function commandBench(args: Args, t: CliMessages): Promise<void> {
2893
2941
  const asJson = boolFlag(args, 'json');
2894
2942
  const chosen = stringFlag(args, 'workload');
@@ -3150,6 +3198,9 @@ async function commandGateway(
3150
3198
  asOfMs: Date.now(),
3151
3199
  };
3152
3200
 
3201
+ /** Same measured side as `serve`'s, from `--log` — see `usageIndexFrom`. */
3202
+ const limitsIndex = await usageIndexFrom(args, pricing, t);
3203
+
3153
3204
  const measured: { calls: number; usd: number } = { calls: 0, usd: 0 };
3154
3205
  /**
3155
3206
  * Forwarded calls whose cost this session cannot see.
@@ -3166,6 +3217,9 @@ async function commandGateway(
3166
3217
  onCannotTell: policyFlag as FailurePolicy,
3167
3218
  ...(config.spend?.substitute === undefined ? {} : { substitute: config.spend.substitute }),
3168
3219
  },
3220
+ ...(config.limits === undefined ? {} : { limits: config.limits }),
3221
+ ...(limitsIndex === null ? {} : { position: (call: { label?: string; session?: string }) => positionAt(limitsIndex, call) }),
3222
+ ...(config.waive === undefined ? {} : { waivers: config.waive }),
3169
3223
  standing: () => standing,
3170
3224
  record: (call) => {
3171
3225
  measured.calls += 1;
@@ -3206,6 +3260,12 @@ async function commandGateway(
3206
3260
  ` ${c.dim(wrap(standing === null ? t.gateway.noStanding() : t.gateway.standing(formatUsd(standing.consumedUsd), formatUsd(standing.limitUsd)), 74, ' '))}`,
3207
3261
  );
3208
3262
  console.log(` ${c.dim(wrap(t.gateway.policy(policyFlag), 74, ' '))}`);
3263
+ if (config.limits !== undefined && limitsIndex === null) {
3264
+ console.log(` ${c.yellow(wrap(t.serve.limitsNoLog(), 74, ' '))}`);
3265
+ }
3266
+ if (limitsIndex !== null && limitsIndex.unpriced > 0) {
3267
+ console.log(` ${c.yellow(wrap(t.serve.limitsUnpriced(limitsIndex.unpriced), 74, ' '))}`);
3268
+ }
3209
3269
  console.log();
3210
3270
  }
3211
3271
 
@@ -5257,6 +5317,16 @@ async function commandServe(
5257
5317
  const limitUsd = config.spend?.monthlyUsd;
5258
5318
  const measured = standing !== null && standing.coverage !== 'none';
5259
5319
 
5320
+ /**
5321
+ * The limits policy's measured side, from the usage log `--log` points at —
5322
+ * read once at start, same staleness posture as the store. The store cannot
5323
+ * serve this: its records are provider buckets with no label and no
5324
+ * session, so per-label and per-session spend exist only in a usage log.
5325
+ * Without `--log`, every ceiling judges `cannot-tell` rather than judging
5326
+ * against a measurement nobody took.
5327
+ */
5328
+ const limitsIndex = await usageIndexFrom(args, pricing, t);
5329
+
5260
5330
  const server = buildServer({
5261
5331
  catalogue: pricing,
5262
5332
  position: () => ({
@@ -5269,6 +5339,9 @@ async function commandServe(
5269
5339
  // know which month the figure is about.
5270
5340
  window: standing === null ? null : { fromMs: standing.period.fromMs, toMs: standing.period.toMs },
5271
5341
  }),
5342
+ ...(config.limits === undefined ? {} : { limits: config.limits }),
5343
+ ...(limitsIndex === null ? {} : { measured: (call: { label?: string; session?: string }) => positionAt(limitsIndex, call) }),
5344
+ ...(config.waive === undefined ? {} : { waivers: config.waive }),
5272
5345
  });
5273
5346
 
5274
5347
  const socket = stringFlag(args, 'socket');
@@ -5292,6 +5365,12 @@ async function commandServe(
5292
5365
  if (limitUsd === undefined) {
5293
5366
  console.log(` ${c.dim(wrap(t.serve.noBudget(), 74, ' '))}`);
5294
5367
  }
5368
+ if (config.limits !== undefined && limitsIndex === null) {
5369
+ console.log(` ${c.yellow(wrap(t.serve.limitsNoLog(), 74, ' '))}`);
5370
+ }
5371
+ if (limitsIndex !== null && limitsIndex.unpriced > 0) {
5372
+ console.log(` ${c.yellow(wrap(t.serve.limitsUnpriced(limitsIndex.unpriced), 74, ' '))}`);
5373
+ }
5295
5374
  }
5296
5375
 
5297
5376
  /**
@@ -10644,6 +10723,9 @@ async function main(): Promise<void> {
10644
10723
  case 'conform':
10645
10724
  await commandConform(args, t);
10646
10725
  break;
10726
+ case 'schema':
10727
+ commandSchema(args, t);
10728
+ break;
10647
10729
  case 'rollup':
10648
10730
  await commandRollup(args, t);
10649
10731
  break;