@trazum/core 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.
Files changed (46) hide show
  1. package/dist/config-schema.d.ts +32 -1
  2. package/dist/config-schema.d.ts.map +1 -1
  3. package/dist/config-schema.js +55 -3
  4. package/dist/config-schema.js.map +1 -1
  5. package/dist/conform.d.ts +8 -1
  6. package/dist/conform.d.ts.map +1 -1
  7. package/dist/conform.js +82 -0
  8. package/dist/conform.js.map +1 -1
  9. package/dist/gateway.d.ts +32 -1
  10. package/dist/gateway.d.ts.map +1 -1
  11. package/dist/gateway.js +64 -3
  12. package/dist/gateway.js.map +1 -1
  13. package/dist/guard.d.ts +20 -0
  14. package/dist/guard.d.ts.map +1 -1
  15. package/dist/guard.js +41 -3
  16. package/dist/guard.js.map +1 -1
  17. package/dist/index.d.ts +8 -3
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +5 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/judgement.d.ts +159 -0
  22. package/dist/judgement.d.ts.map +1 -0
  23. package/dist/judgement.js +188 -0
  24. package/dist/judgement.js.map +1 -0
  25. package/dist/position.d.ts +74 -0
  26. package/dist/position.d.ts.map +1 -0
  27. package/dist/position.js +93 -0
  28. package/dist/position.js.map +1 -0
  29. package/dist/schemas.d.ts +13 -0
  30. package/dist/schemas.d.ts.map +1 -0
  31. package/dist/schemas.js +144 -0
  32. package/dist/schemas.js.map +1 -0
  33. package/dist/session-cost.d.ts +2 -0
  34. package/dist/session-cost.d.ts.map +1 -1
  35. package/dist/session-cost.js +1 -1
  36. package/dist/session-cost.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/config-schema.ts +89 -3
  39. package/src/conform.ts +79 -0
  40. package/src/gateway.ts +108 -4
  41. package/src/guard.ts +73 -3
  42. package/src/index.ts +18 -2
  43. package/src/judgement.ts +346 -0
  44. package/src/position.ts +126 -0
  45. package/src/schemas.ts +150 -0
  46. package/src/session-cost.ts +1 -1
@@ -0,0 +1,346 @@
1
+ /**
2
+ * One policy, judged once — the function every door calls.
3
+ *
4
+ * The `limits` block states the policy: per-day, per-session and per-label
5
+ * USD ceilings. This module answers whether a proposed call fits it, and the
6
+ * design constraint is the 1.62 arc's lesson: **two doors to the same value
7
+ * agreeing by coincidence is a defect waiting for its input.** So the doors
8
+ * — the gateway's 402, `serve`'s cost answer, `spend_guard` over MCP — do
9
+ * not each read a slice of config and do their own arithmetic. They hand
10
+ * this function the policy, the measured position and the call, and forward
11
+ * its answer.
12
+ *
13
+ * Nothing here re-derives the single-ceiling semantics either. Each ceiling
14
+ * is judged by `answerCost`, the function that has answered "does this call
15
+ * fit this budget" for every door since 1.44 — including its refusals: a
16
+ * negative token count is rejected before it can become money, an unpriced
17
+ * model is a cannot-tell rather than a guess, and a verdict always names
18
+ * whether it rests on a measurement alone or needed the estimate. A policy
19
+ * judgement is those answers, one per applicable ceiling, under one verdict.
20
+ *
21
+ * Two refusals are this module's own, and both close the same loophole: a
22
+ * policy with per-label ceilings judging a call that names no label, or a
23
+ * per-session ceiling judging a call that names no session, is `cannot-tell`
24
+ * — not "no ceiling applies". A call that omits its label does not slip past
25
+ * the label's ceiling; it becomes unjudgeable, and the answer says why.
26
+ */
27
+
28
+ import { answerCost } from './answer.js';
29
+ import type { CostAnswer } from './answer.js';
30
+ import type { PricingCatalogue } from './pricing.js';
31
+ import type { LimitsConfig, WaiveEntry } from './config-schema.js';
32
+ import { formatUsd } from './savings.js';
33
+
34
+ /** Which ceiling a judgement is about. */
35
+ export type LimitScope = 'day' | 'session' | 'label';
36
+
37
+ export type LimitVerdict = 'within' | 'over' | 'cannot-tell';
38
+
39
+ /** Why one ceiling could not be judged, when it could not. */
40
+ export type CannotJudgeReason =
41
+ /** The scope's spend has not been measured, so the consumed half is unknown. */
42
+ | 'nothing-measured'
43
+ /** The model is not in the catalogue, so the call cannot be priced. */
44
+ | 'model-unpriced'
45
+ /** Per-label ceilings exist and the call names no label. */
46
+ | 'no-label-on-call'
47
+ /** A per-session ceiling exists and the call names no session. */
48
+ | 'no-session-on-call';
49
+
50
+ /**
51
+ * One ceiling, judged. The three figures a refusal must be auditable from
52
+ * are all here: the limit, the measured spend, and the window the measured
53
+ * figure covers — the denominator, not just the number.
54
+ */
55
+ export interface LimitJudgement {
56
+ scope: LimitScope;
57
+ /** Which label, when the scope is `label`. Null otherwise. */
58
+ label: string | null;
59
+ limitUsd: number;
60
+ verdict: LimitVerdict;
61
+ /**
62
+ * `measured` when the ceiling is already crossed without the estimate;
63
+ * `measured+estimated` when it takes the described call to cross — or to
64
+ * fit. Null when the verdict is `cannot-tell`.
65
+ */
66
+ restsOn: 'measured' | 'measured+estimated' | null;
67
+ reason: CannotJudgeReason | null;
68
+ /** Measured spend inside this scope's period. Null when not measured. */
69
+ measuredUsd: number | null;
70
+ /** The period the measured figure covers. Null when unknown. */
71
+ window: { fromMs: number; toMs: number } | null;
72
+ /** Where the scope would stand after this call. Null when unjudgeable. */
73
+ afterCallUsd: number | null;
74
+ /**
75
+ * The unexpired waiver silencing this ceiling, when the config carries one.
76
+ *
77
+ * The `check` mechanism, applied here unchanged: the verdict stays `over`
78
+ * — the measurement is the measurement — but a waived `over` does not make
79
+ * the policy refuse, and the waiver rides in the judgement so every answer
80
+ * from every door is the record of the silence. Null when nothing is
81
+ * waived, which is almost always.
82
+ */
83
+ waived: { reason: string; until: string } | null;
84
+ }
85
+
86
+ export interface PolicyJudgement {
87
+ schemaVersion: 1;
88
+ /**
89
+ * The strictest verdict wins: one `over` makes the policy `over`, else one
90
+ * `cannot-tell` makes it `cannot-tell`, else everything judged is `within`.
91
+ * The order is the only safe one — a door that read "within" off a policy
92
+ * with an unjudgeable ceiling would be approving a call nobody judged.
93
+ */
94
+ verdict: LimitVerdict;
95
+ /** Set when the verdict is `cannot-tell` and no ceiling produced it. */
96
+ reason: 'no-policy' | null;
97
+ /** One entry per applicable ceiling, in policy order: day, session, label. */
98
+ judgements: LimitJudgement[];
99
+ }
100
+
101
+ /**
102
+ * The measured side: what the store has billed inside each scope's period.
103
+ *
104
+ * Every field is a measurement or `null`, never zero-for-absence — the rule
105
+ * every document in this product holds. A scope the caller could not measure
106
+ * (no clock on the log, a session nothing recorded) is `null`, and the
107
+ * judgement for that ceiling is `cannot-tell` rather than "under".
108
+ */
109
+ export interface MeasuredPosition {
110
+ /** Measured spend in the current UTC day. */
111
+ dayUsd: number | null;
112
+ dayWindow?: { fromMs: number; toMs: number } | null;
113
+ /** Measured spend in the call's session. */
114
+ sessionUsd: number | null;
115
+ sessionWindow?: { fromMs: number; toMs: number } | null;
116
+ /** Measured spend under the call's label. */
117
+ labelUsd: number | null;
118
+ labelWindow?: { fromMs: number; toMs: number } | null;
119
+ }
120
+
121
+ /** The call being asked about — the same fields every door already takes. */
122
+ export interface ProposedCall {
123
+ model?: string;
124
+ inputTokens?: number;
125
+ outputTokens?: number;
126
+ basis?: 'token-count' | 'heuristic';
127
+ label?: string;
128
+ session?: string;
129
+ }
130
+
131
+ const judgementOf = (
132
+ scope: LimitScope,
133
+ label: string | null,
134
+ limitUsd: number,
135
+ answer: CostAnswer,
136
+ ): LimitJudgement => ({
137
+ scope,
138
+ label,
139
+ limitUsd,
140
+ verdict: answer.verdict,
141
+ restsOn: answer.restsOn,
142
+ // The single-ceiling reasons map one-to-one; the two call-shape refusals
143
+ // (`no-label-on-call`, `no-session-on-call`) never reach answerCost.
144
+ reason:
145
+ answer.reason === null ? null : answer.reason === 'model-unpriced' ? 'model-unpriced' : 'nothing-measured',
146
+ measuredUsd: answer.budget?.consumedUsd ?? null,
147
+ window: answer.budget?.window ?? null,
148
+ afterCallUsd: answer.afterCall?.usd ?? null,
149
+ waived: null,
150
+ });
151
+
152
+ const unjudgeable = (
153
+ scope: LimitScope,
154
+ label: string | null,
155
+ limitUsd: number,
156
+ reason: CannotJudgeReason,
157
+ ): LimitJudgement => ({
158
+ scope,
159
+ label,
160
+ limitUsd,
161
+ verdict: 'cannot-tell',
162
+ restsOn: null,
163
+ reason,
164
+ measuredUsd: null,
165
+ window: null,
166
+ afterCallUsd: null,
167
+ waived: null,
168
+ });
169
+
170
+ /** The waiver gate that names one judgement's ceiling. */
171
+ const gateOf = (judgement: LimitJudgement): string =>
172
+ judgement.scope === 'day'
173
+ ? 'limits.dayUsd'
174
+ : judgement.scope === 'session'
175
+ ? 'limits.sessionUsd'
176
+ : `limits.byLabel:${judgement.label ?? ''}`;
177
+
178
+ /**
179
+ * The unexpired waiver for one judgement, if the config carries one.
180
+ *
181
+ * Judged in UTC at run time, the same rule `check` applies: the day the
182
+ * waiver expires, the ceiling refuses again — nobody has to remember.
183
+ */
184
+ const waiverFor = (
185
+ judgement: LimitJudgement,
186
+ waivers: readonly WaiveEntry[],
187
+ on: Date,
188
+ ): { reason: string; until: string } | null => {
189
+ const today = on.toISOString().slice(0, 10);
190
+ const match = waivers.find((entry) => entry.gate === gateOf(judgement) && entry.until >= today);
191
+ return match === undefined ? null : { reason: match.reason, until: match.until };
192
+ };
193
+
194
+ /**
195
+ * Judges a proposed call against the whole `limits` policy.
196
+ *
197
+ * Pure and synchronous for the same reason `answerCost` is: this runs inside
198
+ * a request that has not been sent yet, and a function that reads a file
199
+ * cannot promise single-digit milliseconds. The caller measures; this judges.
200
+ */
201
+ export function judgeLimits(
202
+ policy: LimitsConfig | undefined,
203
+ position: MeasuredPosition,
204
+ call: ProposedCall,
205
+ options: { catalogue: PricingCatalogue; on?: Date; waivers?: readonly WaiveEntry[] },
206
+ ): PolicyJudgement {
207
+ const judgements: LimitJudgement[] = [];
208
+
209
+ const judge = (consumedUsd: number | null, limitUsd: number, window: { fromMs: number; toMs: number } | null) =>
210
+ answerCost(
211
+ {
212
+ model: call.model,
213
+ inputTokens: call.inputTokens,
214
+ outputTokens: call.outputTokens,
215
+ basis: call.basis,
216
+ limitUsd,
217
+ consumedUsd: consumedUsd ?? undefined,
218
+ window,
219
+ },
220
+ options,
221
+ );
222
+
223
+ if (policy?.dayUsd !== undefined) {
224
+ judgements.push(
225
+ judgementOf('day', null, policy.dayUsd, judge(position.dayUsd, policy.dayUsd, position.dayWindow ?? null)),
226
+ );
227
+ }
228
+
229
+ if (policy?.sessionUsd !== undefined) {
230
+ judgements.push(
231
+ call.session === undefined
232
+ ? unjudgeable('session', null, policy.sessionUsd, 'no-session-on-call')
233
+ : judgementOf(
234
+ 'session',
235
+ null,
236
+ policy.sessionUsd,
237
+ judge(position.sessionUsd, policy.sessionUsd, position.sessionWindow ?? null),
238
+ ),
239
+ );
240
+ }
241
+
242
+ if (policy?.byLabel !== undefined && Object.keys(policy.byLabel).length > 0) {
243
+ if (call.label === undefined) {
244
+ /*
245
+ The loophole this closes: per-label ceilings with an unlabelled call.
246
+ Skipping the scope would mean the ceiling only binds calls polite
247
+ enough to name themselves — the same defect as the negative token
248
+ count, worn as an omission. One judgement, no label, cannot-tell.
249
+ The ceiling reported is the smallest, because that is the one the
250
+ call might be dodging.
251
+ */
252
+ const smallest = Math.min(...Object.values(policy.byLabel));
253
+ judgements.push(unjudgeable('label', null, smallest, 'no-label-on-call'));
254
+ } else if (policy.byLabel[call.label] !== undefined) {
255
+ const limitUsd = policy.byLabel[call.label]!;
256
+ judgements.push(
257
+ judgementOf(
258
+ 'label',
259
+ call.label,
260
+ limitUsd,
261
+ judge(position.labelUsd, limitUsd, position.labelWindow ?? null),
262
+ ),
263
+ );
264
+ }
265
+ // A labelled call whose label has no ceiling is judged by the other
266
+ // scopes alone: the policy said nothing about this label, and inventing
267
+ // a ceiling for it would be enforcing config nobody wrote.
268
+ }
269
+
270
+ if (judgements.length === 0) {
271
+ return { schemaVersion: 1, verdict: 'cannot-tell', reason: 'no-policy', judgements: [] };
272
+ }
273
+
274
+ /**
275
+ * Waivers, applied after every ceiling is judged: the measurement stays —
276
+ * a waived judgement still says `over` — but the silence is attached, on
277
+ * the record, and the verdict below treats a waived `over` as not-refusing.
278
+ */
279
+ const { on = new Date(), waivers = [] } = options;
280
+ if (waivers.length > 0) {
281
+ for (const judgement of judgements) {
282
+ if (judgement.verdict === 'over') judgement.waived = waiverFor(judgement, waivers, on);
283
+ }
284
+ }
285
+
286
+ const verdict: LimitVerdict = judgements.some((entry) => entry.verdict === 'over' && entry.waived === null)
287
+ ? 'over'
288
+ : judgements.some((entry) => entry.verdict === 'cannot-tell')
289
+ ? 'cannot-tell'
290
+ : 'within';
291
+
292
+ return { schemaVersion: 1, verdict, reason: null, judgements };
293
+ }
294
+
295
+ /** The scope, as a refusal names it. Labels print; session identifiers never do. */
296
+ export function limitScopeName(judgement: LimitJudgement): string {
297
+ return judgement.scope === 'day'
298
+ ? "the day's ceiling"
299
+ : judgement.scope === 'session'
300
+ ? "this session's ceiling"
301
+ : judgement.label === null
302
+ ? 'the per-label ceiling'
303
+ : `the "${judgement.label}" label's ceiling`;
304
+ }
305
+
306
+ /** The period a measured figure covers, when the judgement carries one. */
307
+ function periodOf(judgement: LimitJudgement): string {
308
+ if (judgement.window === null) return '';
309
+ const from = new Date(judgement.window.fromMs).toISOString();
310
+ const to = new Date(judgement.window.toMs).toISOString();
311
+ return ` between ${from} and ${to}`;
312
+ }
313
+
314
+ /**
315
+ * The refusal, legible — chapter four's rule, written once.
316
+ *
317
+ * Every over-limit sentence names the limit, the measured position and the
318
+ * period, so an agent can log it and a person can audit it without
319
+ * re-running anything. Every door uses this builder rather than composing
320
+ * its own, for the same reason every door uses the same judge.
321
+ */
322
+ export function limitSentence(judgement: LimitJudgement): string {
323
+ const limit = formatUsd(judgement.limitUsd);
324
+ const measured =
325
+ judgement.measuredUsd === null ? 'nothing measured' : `${formatUsd(judgement.measuredUsd)} measured`;
326
+ return judgement.restsOn === 'measured'
327
+ ? `${capital(limitScopeName(judgement))} of ${limit} is already spent: ${measured}${periodOf(judgement)}.`
328
+ : `This call would take ${limitScopeName(judgement)} of ${limit} past its limit: ${measured}${periodOf(judgement)}, plus this call's estimate.`;
329
+ }
330
+
331
+ /** Why the policy could not be judged, in one auditable sentence. */
332
+ export function unjudgedSentence(judged: PolicyJudgement): string {
333
+ const first = judged.judgements.find((entry) => entry.verdict === 'cannot-tell');
334
+ const which = first === undefined ? 'a ceiling' : limitScopeName(first);
335
+ const why =
336
+ first?.reason === 'no-label-on-call'
337
+ ? 'the call names no label'
338
+ : first?.reason === 'no-session-on-call'
339
+ ? 'the call names no session'
340
+ : first?.reason === 'model-unpriced'
341
+ ? 'the model is not in the price catalogue'
342
+ : 'its spend has not been measured';
343
+ return `${capital(which)} in the limits policy cannot be judged: ${why}.`;
344
+ }
345
+
346
+ const capital = (text: string): string => `${text[0]!.toUpperCase()}${text.slice(1)}`;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The measured side of the limits policy: what a usage log says each scope
3
+ * has already spent.
4
+ *
5
+ * `judgeLimits` judges; this measures. The split is the product's oldest
6
+ * rule — the judging function must be pure and instant, so whatever reads
7
+ * and prices records happens once, here, and the doors keep the result.
8
+ *
9
+ * **Absence is `null`, never zero.** A log with no clock cannot say what
10
+ * today has cost; a log that records no sessions cannot say what a session
11
+ * has spent. Both come back `null`, and `judgeLimits` turns a `null` into
12
+ * `cannot-tell` rather than an approval. The one deliberate asymmetry: a log
13
+ * that *does* record sessions and has never seen this one answers `0` — the
14
+ * history of a conversation that has not started is complete and empty,
15
+ * which is a measurement, not an absence.
16
+ *
17
+ * **Session keys never leave this module.** The index groups by them — that
18
+ * is the whole job — but the map lives in door memory, `positionAt` returns
19
+ * only dollar figures, and nothing here is serialised. The guarantee the
20
+ * usage module states (a session key is somebody's conversation and is never
21
+ * printed) holds through this door too.
22
+ */
23
+
24
+ import { costOf } from './session-cost.js';
25
+ import type { UsageRecord } from './usage.js';
26
+ import type { PricingCatalogue } from './pricing.js';
27
+ import type { MeasuredPosition } from './judgement.js';
28
+
29
+ const DAY_MS = 24 * 60 * 60 * 1000;
30
+
31
+ /**
32
+ * Everything a door needs to answer `positionAt` in constant time, built
33
+ * once from the log it was pointed at.
34
+ */
35
+ export interface MeasuredIndex {
36
+ /** Spend inside the current UTC day. Null when no record carries a clock. */
37
+ dayUsd: number | null;
38
+ /** The UTC day the figure covers. Null exactly when `dayUsd` is. */
39
+ dayWindow: { fromMs: number; toMs: number } | null;
40
+ /** Whether any record carried a label / a session at all. */
41
+ labelsSeen: boolean;
42
+ sessionsSeen: boolean;
43
+ /** Priced spend per label. Keys are labels, which do print. */
44
+ labels: ReadonlyMap<string, number>;
45
+ /** Priced spend per session. Keys are session identifiers — never printed. */
46
+ sessions: ReadonlyMap<string, number>;
47
+ /** Whether the log held any records at all. */
48
+ any: boolean;
49
+ /** Records whose model the catalogue cannot price — money nobody can see. */
50
+ unpriced: number;
51
+ }
52
+
53
+ /**
54
+ * Prices and groups a parsed usage log, once.
55
+ *
56
+ * An unpriced record contributes nothing to any figure and is **counted**
57
+ * instead of dropped silently: a door that reports "measured $3" over a log
58
+ * with a thousand unpriced records is wrong by an amount nobody can see, in
59
+ * the flattering direction, and `unpriced` is how the door says so.
60
+ */
61
+ export function indexUsage(
62
+ records: readonly UsageRecord[],
63
+ options: { catalogue: PricingCatalogue; on?: Date },
64
+ ): MeasuredIndex {
65
+ const { catalogue, on = new Date() } = options;
66
+ const dayFromMs = Math.floor(on.getTime() / DAY_MS) * DAY_MS;
67
+ const dayToMs = dayFromMs + DAY_MS;
68
+
69
+ const labels = new Map<string, number>();
70
+ const sessions = new Map<string, number>();
71
+ let labelsSeen = false;
72
+ let sessionsSeen = false;
73
+ let clocksSeen = false;
74
+ let dayUsd = 0;
75
+ let unpriced = 0;
76
+
77
+ for (const record of records) {
78
+ if (record.label !== null) labelsSeen = true;
79
+ if (record.session !== null) sessionsSeen = true;
80
+ if (record.ts !== null) clocksSeen = true;
81
+
82
+ const usd = costOf(record, catalogue, on);
83
+ if (usd === null) {
84
+ unpriced += 1;
85
+ continue;
86
+ }
87
+ if (record.label !== null) labels.set(record.label, (labels.get(record.label) ?? 0) + usd);
88
+ if (record.session !== null) sessions.set(record.session, (sessions.get(record.session) ?? 0) + usd);
89
+ if (record.ts !== null && record.ts >= dayFromMs && record.ts < dayToMs) dayUsd += usd;
90
+ }
91
+
92
+ return {
93
+ dayUsd: clocksSeen ? dayUsd : null,
94
+ dayWindow: clocksSeen ? { fromMs: dayFromMs, toMs: dayToMs } : null,
95
+ labelsSeen,
96
+ sessionsSeen,
97
+ labels,
98
+ sessions,
99
+ any: records.length > 0,
100
+ unpriced,
101
+ };
102
+ }
103
+
104
+ /**
105
+ * The measured position for one proposed call, from an index built once.
106
+ *
107
+ * A scope the log cannot see is `null`; a scope it can see and has nothing
108
+ * recorded for is `0`. The difference is the difference between "unknown"
109
+ * and "a conversation that has not started yet", and collapsing them is how
110
+ * a dead log approves a live spend.
111
+ */
112
+ export function positionAt(
113
+ index: MeasuredIndex,
114
+ call: { label?: string; session?: string },
115
+ ): MeasuredPosition {
116
+ if (!index.any) {
117
+ return { dayUsd: null, sessionUsd: null, labelUsd: null };
118
+ }
119
+ return {
120
+ dayUsd: index.dayUsd,
121
+ dayWindow: index.dayWindow,
122
+ sessionUsd:
123
+ call.session !== undefined && index.sessionsSeen ? (index.sessions.get(call.session) ?? 0) : null,
124
+ labelUsd: call.label !== undefined && index.labelsSeen ? (index.labels.get(call.label) ?? 0) : null,
125
+ };
126
+ }
package/src/schemas.ts ADDED
@@ -0,0 +1,150 @@
1
+ import type { ContractName } from './conform.js';
2
+
3
+ /**
4
+ * A JSON Schema for every contract, so a document can be checked with **no
5
+ * Trazum at all** — any off-the-shelf draft 2020-12 validator will do.
6
+ *
7
+ * Authored, not generated: `conform`'s rules live as functions and a schema
8
+ * generated from closures would be a guess wearing a standard. What keeps the
9
+ * two doors honest instead is the guard in the suite: every fixture `conform`
10
+ * accepts must validate against its schema, and every fixture `conform`
11
+ * rejects for a structural reason must fail it — a schema that drifts from
12
+ * `conform` is the two-doors defect wearing a file format.
13
+ *
14
+ * The schemas state **required fields and their types, and stop there** —
15
+ * the same line `conform` holds. `additionalProperties` is never `false`:
16
+ * these documents gain fields without a version bump, and a schema that
17
+ * rejected tomorrow's field would be a schema nobody upgrades. Field prose
18
+ * lives in `docs/json-output.md`; the schema carries structure.
19
+ */
20
+
21
+ const DRAFT = 'https://json-schema.org/draft/2020-12/schema';
22
+
23
+ /** Shorthand: an object schema over required properties. */
24
+ function doc(name: ContractName, properties: Record<string, unknown>): Record<string, unknown> {
25
+ return {
26
+ $schema: DRAFT,
27
+ $id: `https://github.com/Davmunrey/Trazum/schema/${name}/v1.json`,
28
+ title: `trazum ${name} document, schemaVersion 1`,
29
+ type: 'object',
30
+ required: ['schemaVersion', ...Object.keys(properties)],
31
+ properties: {
32
+ schemaVersion: { const: 1 },
33
+ ...properties,
34
+ },
35
+ };
36
+ }
37
+
38
+ const array = { type: 'array' } as const;
39
+ const object = { type: 'object' } as const;
40
+ const number = { type: 'number' } as const;
41
+ const string = { type: 'string' } as const;
42
+ const boolean = { type: 'boolean' } as const;
43
+ const objectOrNull = { type: ['object', 'null'] } as const;
44
+ const stringOrNull = { type: ['string', 'null'] } as const;
45
+
46
+ /**
47
+ * One schema per contract, keyed by the exact names `--contract` accepts.
48
+ *
49
+ * The usage log is the one non-document contract: its schema describes **one
50
+ * record** — one line of the log — because a `.jsonl` file is not itself a
51
+ * JSON value and no JSON Schema can describe a file of lines. Validators run
52
+ * per line, which is how the log is read everywhere else too.
53
+ */
54
+ export const CONTRACT_SCHEMAS: Record<ContractName, Record<string, unknown>> = {
55
+ 'usage-log': {
56
+ $schema: DRAFT,
57
+ $id: 'https://github.com/Davmunrey/Trazum/schema/usage-log/v1.json',
58
+ title: 'trazum usage-log record — one line of the log',
59
+ type: 'object',
60
+ required: ['model'],
61
+ properties: {
62
+ model: string,
63
+ usage: object,
64
+ timestamp: string,
65
+ label: string,
66
+ session: string,
67
+ outcome: string,
68
+ stop_reason: string,
69
+ },
70
+ },
71
+ profile: doc('profile', {
72
+ total: object,
73
+ byLabel: array,
74
+ byModel: array,
75
+ byLabelAndModel: array,
76
+ unpricedModels: array,
77
+ skippedLines: array,
78
+ span: objectOrNull,
79
+ }),
80
+ fleet: doc('fleet', { bySource: array, rollup: object }),
81
+ plan: doc('plan', {
82
+ actions: array,
83
+ projectedSavingUsd: number,
84
+ measuredStakeUsd: number,
85
+ totalUsd: number,
86
+ }),
87
+ verification: doc('verification', {
88
+ actions: array,
89
+ arrived: number,
90
+ notArrived: number,
91
+ cannotTell: number,
92
+ }),
93
+ history: doc('history', { periods: array, runs: array }),
94
+ connected: doc('connected', { provider: string, total: object, unavailable: array }),
95
+ 'cost-answer': doc('cost-answer', {
96
+ verdict: string,
97
+ call: objectOrNull,
98
+ budget: objectOrNull,
99
+ }),
100
+ 'spend-guard': doc('spend-guard', {
101
+ verdict: string,
102
+ cost: object,
103
+ alternatives: array,
104
+ because: string,
105
+ }),
106
+ 'outcome-report': doc('outcome-report', {
107
+ slices: array,
108
+ undeclared: array,
109
+ coverage: object,
110
+ successShareOfRecordedUsd: { type: ['number', 'null'] },
111
+ noRate: stringOrNull,
112
+ }),
113
+ 'annual-record': doc('annual-record', {
114
+ months: array,
115
+ missingMonths: array,
116
+ promises: object,
117
+ outcomes: objectOrNull,
118
+ cannotSay: array,
119
+ }),
120
+ 'roll-up': doc('roll-up', {
121
+ contributors: array,
122
+ rejected: array,
123
+ total: object,
124
+ notMerged: array,
125
+ cannotSay: array,
126
+ }),
127
+ 'first-run': doc('first-run', { config: object, justified: array, declined: array }),
128
+ pulse: doc('pulse', { beats: array, nowMs: number, stale: boolean }),
129
+ 'rule-yield': doc('rule-yield', {
130
+ rules: array,
131
+ tokensBefore: number,
132
+ tokensSaved: number,
133
+ floor: number,
134
+ }),
135
+ 'gateway-refusal': doc('gateway-refusal', { error: object, reason: string, alternatives: array }),
136
+ bench: doc('bench', { workloads: array, node: string, cpus: number }),
137
+ 'prompt-draft': doc('prompt-draft', {
138
+ prompt: stringOrNull,
139
+ sections: array,
140
+ answered: array,
141
+ declined: array,
142
+ missing: array,
143
+ measured: objectOrNull,
144
+ }),
145
+ };
146
+
147
+ /** The schema for one named contract — the object `trazum schema` prints. */
148
+ export function contractSchema(name: ContractName): Record<string, unknown> {
149
+ return CONTRACT_SCHEMAS[name];
150
+ }
@@ -72,7 +72,7 @@ export interface SessionCostTracker {
72
72
  }
73
73
 
74
74
  /** Every billed dollar of one call, at its own model's rates. */
75
- function costOf(record: UsageRecord, catalogue: PricingCatalogue, on: Date): number | null {
75
+ export function costOf(record: UsageRecord, catalogue: PricingCatalogue, on: Date): number | null {
76
76
  const model = catalogue.byId.get(record.model);
77
77
  if (!model) return null;
78
78
  const { inputPerMTok, outputPerMTok } = effectivePricing(model, on);