@trazum/cli 1.65.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.65.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.65.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
@@ -212,6 +212,10 @@ ${bold('OPTIONS FOR gateway')}
212
212
  What happens when the gateway cannot judge a call
213
213
  — no budget, nothing measured, an unpriced model.
214
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.
215
219
 
216
220
  Stands between your SDK and the provider, speaking their wire format, so no
217
221
  code changes. Usage is measured from the provider's own response as it comes
@@ -569,6 +573,11 @@ ${bold('OPTIONS FOR plan')}
569
573
  ${bold('OPTIONS FOR serve')}
570
574
  --port <n> Port on 127.0.0.1. Default: 7317.
571
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.
572
581
 
573
582
  Answers the two questions that matter at call time — what will this cost,
574
583
  and is there budget left — in single-digit milliseconds, so an agent or a
@@ -778,6 +787,13 @@ ${bold('CONFIG FILE')}
778
787
  spend { "maxUsd": 200, "byLabel": { "chat": 40 } } — money budgets for
779
788
  "trazum profile", in dollars. A budgeted label with no calls in
780
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
781
797
  owners { "patterns": { "payments": ["billing-*"] },
782
798
  "shared": { "search": { "payments": 0.6, "support": 0.4 } },
783
799
  "budgets": { "payments": 400 } } — whose budget each workload
@@ -2460,6 +2476,10 @@ ${bold('EXAMPLES')}
2460
2476
 
2461
2477
  serve: {
2462
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.`,
2463
2483
  loopbackOnly: () =>
2464
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.',
2465
2485
  measuredFrom: (usd) =>
package/src/i18n/es.ts CHANGED
@@ -204,6 +204,11 @@ ${bold('OPCIONES DE gateway')}
204
204
  juzgar una llamada — sin presupuesto, sin nada
205
205
  medido, un modelo sin precio.
206
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.
207
212
 
208
213
  Se pone entre tu SDK y el proveedor, hablando su formato, así que no hay
209
214
  cambios de código. El consumo se mide desde la propia respuesta del proveedor
@@ -583,6 +588,11 @@ ${bold('OPCIONES DE plan')}
583
588
  ${bold('OPCIONES DE serve')}
584
589
  --port <n> Puerto en 127.0.0.1. Por defecto: 7317.
585
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.
586
596
 
587
597
  Responde las dos preguntas que importan en el momento de la llamada — cuánto
588
598
  va a costar esto y si queda presupuesto — en milisegundos de un solo dígito,
@@ -802,6 +812,13 @@ ${bold('FICHERO DE CONFIGURACIÓN')}
802
812
  spend { "maxUsd": 200, "byLabel": { "chat": 40 } } — presupuestos en
803
813
  dólares para "trazum profile". Una etiqueta con presupuesto y sin
804
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
805
822
  owners { "patterns": { "payments": ["billing-*"] },
806
823
  "shared": { "search": { "payments": 0.6, "support": 0.4 } },
807
824
  "budgets": { "payments": 400 } } — de quién es el presupuesto de
@@ -2469,6 +2486,10 @@ ${bold('EJEMPLOS')}
2469
2486
 
2470
2487
  serve: {
2471
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.`,
2472
2493
  loopbackOnly: () =>
2473
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.',
2474
2495
  measuredFrom: (usd) =>
package/src/i18n/types.ts CHANGED
@@ -1506,6 +1506,14 @@ export interface CliMessages {
1506
1506
  listening(where: string): string;
1507
1507
  /** Why there is no --host, and why that is not an omission. */
1508
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;
1509
1517
  measuredFrom(usd: string): string;
1510
1518
  nothingMeasured(dir: string): string;
1511
1519
  noBudget(): string;
package/src/index.ts CHANGED
@@ -106,7 +106,10 @@ import {
106
106
  detectTextLanguage,
107
107
  dictionaryStanding,
108
108
  languagesWithStanding,
109
+ indexUsage,
110
+ parseUsageLine,
109
111
  plannedCalls,
112
+ positionAt,
110
113
  PRICING_LAST_REVIEWED,
111
114
  profilePrompt,
112
115
  profileToCsv,
@@ -277,6 +280,7 @@ interface Args {
277
280
  const VALUE_FLAGS = new Set([
278
281
  'answers',
279
282
  'calls',
283
+ 'log',
280
284
  'avg-output',
281
285
  'a',
282
286
  'at',
@@ -467,6 +471,28 @@ const LOG_EXTENSIONS = ['.jsonl', '.ndjson', '.log', '.json'];
467
471
  * would be a figure quietly missing a day, the failure this repository
468
472
  * refuses everywhere it can occur.
469
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
+
470
496
  async function readUsageLog(file: string, t: CliMessages): Promise<string> {
471
497
  if (!file.endsWith('.gz')) return readFile(file, 'utf8');
472
498
  const compressed = await readFile(file);
@@ -627,7 +653,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
627
653
  connect: ['since', 'until', 'payload', 'store', 'json', 'out', 'markdown-out', 'pricing', 'pricing-live', 'dry-run'],
628
654
  store: ['prune', 'keep', 'json', 'pricing', 'pricing-live', 'dry-run'],
629
655
  watch: ['once', 'interval', 'since', 'payload', 'webhook', 'json', 'pricing', 'pricing-live'],
630
- serve: ['port', 'socket', 'pricing', 'pricing-live'],
656
+ serve: ['port', 'socket', 'log', 'pricing', 'pricing-live'],
631
657
  route: ['prompt-file', 'cases', 'label', 'concurrency', 'json', 'yes', 'pricing', 'pricing-live'],
632
658
  eval: ['cases', 'level', 'concurrency', 'export', 'out', 'o', 'model'],
633
659
  prune: ['cases', 'concurrency', 'json', 'yes'],
@@ -642,7 +668,7 @@ const COMMAND_FLAGS: Record<string, string[]> = {
642
668
  bench: ['workload', 'json', 'record', 'against', 'max-ratio'],
643
669
  write: ['answers', 'json', 'out', 'o', 'calls', 'avg-output'],
644
670
  feedback: [],
645
- gateway: ['on-cannot-tell', 'port', 'socket', 'pricing', 'pricing-live'],
671
+ gateway: ['on-cannot-tell', 'port', 'socket', 'log', 'pricing', 'pricing-live'],
646
672
  ladder: ['pricing', 'pricing-live', 'since', 'until', 'label'],
647
673
  experiment: ['a', 'b', 'min-outcomes', 'pricing', 'pricing-live'],
648
674
  quality: ['label', 'at', 'gate', 'pricing', 'pricing-live'],
@@ -3172,6 +3198,9 @@ async function commandGateway(
3172
3198
  asOfMs: Date.now(),
3173
3199
  };
3174
3200
 
3201
+ /** Same measured side as `serve`'s, from `--log` — see `usageIndexFrom`. */
3202
+ const limitsIndex = await usageIndexFrom(args, pricing, t);
3203
+
3175
3204
  const measured: { calls: number; usd: number } = { calls: 0, usd: 0 };
3176
3205
  /**
3177
3206
  * Forwarded calls whose cost this session cannot see.
@@ -3188,6 +3217,9 @@ async function commandGateway(
3188
3217
  onCannotTell: policyFlag as FailurePolicy,
3189
3218
  ...(config.spend?.substitute === undefined ? {} : { substitute: config.spend.substitute }),
3190
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 }),
3191
3223
  standing: () => standing,
3192
3224
  record: (call) => {
3193
3225
  measured.calls += 1;
@@ -3228,6 +3260,12 @@ async function commandGateway(
3228
3260
  ` ${c.dim(wrap(standing === null ? t.gateway.noStanding() : t.gateway.standing(formatUsd(standing.consumedUsd), formatUsd(standing.limitUsd)), 74, ' '))}`,
3229
3261
  );
3230
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
+ }
3231
3269
  console.log();
3232
3270
  }
3233
3271
 
@@ -5279,6 +5317,16 @@ async function commandServe(
5279
5317
  const limitUsd = config.spend?.monthlyUsd;
5280
5318
  const measured = standing !== null && standing.coverage !== 'none';
5281
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
+
5282
5330
  const server = buildServer({
5283
5331
  catalogue: pricing,
5284
5332
  position: () => ({
@@ -5291,6 +5339,9 @@ async function commandServe(
5291
5339
  // know which month the figure is about.
5292
5340
  window: standing === null ? null : { fromMs: standing.period.fromMs, toMs: standing.period.toMs },
5293
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 }),
5294
5345
  });
5295
5346
 
5296
5347
  const socket = stringFlag(args, 'socket');
@@ -5314,6 +5365,12 @@ async function commandServe(
5314
5365
  if (limitUsd === undefined) {
5315
5366
  console.log(` ${c.dim(wrap(t.serve.noBudget(), 74, ' '))}`);
5316
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
+ }
5317
5374
  }
5318
5375
 
5319
5376
  /**
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
  }