@jjanczur/tyran 0.1.17 → 0.1.19

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.
@@ -1279,6 +1279,10 @@ export const OVERNIGHT_RUNTIME_FILES = Object.freeze([
1279
1279
  'resume.json',
1280
1280
  'resume.log',
1281
1281
  'usage.json',
1282
+ // The spend cache. Machine-local like the rest of this list and for the same
1283
+ // reason: it is derived from transcripts under the operator's home
1284
+ // directory, so another clone reads different bytes from the same journal.
1285
+ 'cost.json',
1282
1286
  ]);
1283
1287
 
1284
1288
  /**
@@ -595,6 +595,11 @@ export const STATE_GITIGNORE_LINES = Object.freeze([
595
595
  'state/resume.json',
596
596
  'state/resume.log',
597
597
  'state/usage.json',
598
+ // Spend, read back from the platform's own transcripts under the operator's
599
+ // home directory. Committing it would put one machine's vendor spend into
600
+ // shared history — a one-way door in an append-only repo — and make two
601
+ // clones of the same journal disagree.
602
+ 'state/cost.json',
598
603
  // The ask queue's two runtime files: a session id and a pid that mean
599
604
  // nothing in another clone, and a sheet whose answers already live in the
600
605
  // journal — committing either makes every parallel merge conflict on state
@@ -68,7 +68,7 @@ export function validateConfig(doc) {
68
68
  const errors = [];
69
69
  if (!isPlainObject(doc)) return ['config must be a mapping'];
70
70
 
71
- const known = ['profile', 'autonomy', 'tiers', 'validation', 'shared_zones', 'budget', 'main_writable_paths', 'limits'];
71
+ const known = ['profile', 'autonomy', 'tiers', 'validation', 'shared_zones', 'budget', 'main_writable_paths', 'limits', 'pricing'];
72
72
  for (const key of Object.keys(doc)) {
73
73
  if (!known.includes(key)) errors.push(`${key}: unknown top-level key`);
74
74
  }
@@ -184,6 +184,51 @@ export function validateConfig(doc) {
184
184
  }
185
185
  }
186
186
 
187
+ // What a model costs per million tokens. Operator-written from a vendor's
188
+ // price list — never scanner-inferred, so no provenance wrapper, exactly
189
+ // like `limits:`. Absent means the cost report shows tokens and no money,
190
+ // which is the honest default: Tyran does not know what anyone pays.
191
+ if ('pricing' in doc) {
192
+ if (!isPlainObject(doc.pricing)) errors.push('pricing: must be a mapping');
193
+ else {
194
+ const pricing = doc.pricing;
195
+ // The rate card's NAME travels with every amount it produces. Two people
196
+ // quoting different cards produce different money from one set of
197
+ // tokens, and a figure that cannot say which card it came from is not a
198
+ // measurement.
199
+ if ('rate_card' in pricing && !isNonEmptyString(pricing.rate_card)) {
200
+ errors.push('pricing.rate_card: must be a non-empty label');
201
+ }
202
+ if ('models' in pricing) {
203
+ if (!isPlainObject(pricing.models)) errors.push('pricing.models: must be a mapping of model id to rates');
204
+ else {
205
+ for (const [model, rates] of Object.entries(pricing.models)) {
206
+ if (!isPlainObject(rates)) {
207
+ errors.push(`pricing.models.${model}: must be a mapping of rate name to price per million tokens`);
208
+ continue;
209
+ }
210
+ for (const key of Object.keys(rates)) {
211
+ if (!PRICING_RATE_KEYS.includes(key)) {
212
+ errors.push(`pricing.models.${model}.${key}: unknown rate, one of ${PRICING_RATE_KEYS.join(' | ')}`);
213
+ }
214
+ }
215
+ for (const key of PRICING_RATE_KEYS) {
216
+ if (!(key in rates)) {
217
+ errors.push(`pricing.models.${model}.${key}: required`);
218
+ } else if (typeof rates[key] !== 'number' || !Number.isFinite(rates[key]) || rates[key] < 0) {
219
+ errors.push(`pricing.models.${model}.${key}: must be a non-negative number (price per million tokens)`);
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ const knownPricing = ['rate_card', 'models'];
226
+ for (const key of Object.keys(pricing)) {
227
+ if (!knownPricing.includes(key)) errors.push(`pricing.${key}: unknown key`);
228
+ }
229
+ }
230
+ }
231
+
187
232
  return errors;
188
233
  }
189
234
 
@@ -218,6 +263,49 @@ export function limitsOf(doc) {
218
263
  };
219
264
  }
220
265
 
266
+ /**
267
+ * The four things a request is billed for. All four are REQUIRED on any model
268
+ * an operator prices: a table carrying three of them would price the fourth at
269
+ * zero, silently, and cache reads alone were measured at three quarters of a
270
+ * real session's cost. A partial rate card is a wrong number, not a partial
271
+ * one.
272
+ */
273
+ export const PRICING_RATE_KEYS = Object.freeze(['input', 'cache_write', 'cache_read', 'output']);
274
+
275
+ /**
276
+ * The pricing block, normalized — the ONE place the shape lives. Returns
277
+ * `{ rate_card, models }` where `models` is a null-prototype table keyed by
278
+ * the model id the platform reports, and `rate_card` is the label that must
279
+ * travel with every amount derived from it (null when unlabelled).
280
+ *
281
+ * A model missing from the table gets NO entry here rather than a zero: the
282
+ * caller must be able to tell "priced at nothing" from "not priced", because
283
+ * the first is a number and the second is a gap that belongs on screen.
284
+ * Rates that fail validateConfig's shape are dropped for the same reason the
285
+ * usage gate drops out-of-range limits — enforcing a value the schema rejects
286
+ * is how a bad config becomes a confident wrong answer.
287
+ */
288
+ export function pricingOf(doc) {
289
+ const pricing = isPlainObject(doc) && isPlainObject(doc.pricing) ? doc.pricing : {};
290
+ const models = Object.create(null);
291
+ if (isPlainObject(pricing.models)) {
292
+ for (const [model, rates] of Object.entries(pricing.models)) {
293
+ if (!isPlainObject(rates)) continue;
294
+ const usable = PRICING_RATE_KEYS.every(
295
+ (key) => typeof rates[key] === 'number' && Number.isFinite(rates[key]) && rates[key] >= 0
296
+ );
297
+ if (!usable) continue;
298
+ const row = Object.create(null);
299
+ for (const key of PRICING_RATE_KEYS) row[key] = rates[key];
300
+ models[model] = row;
301
+ }
302
+ }
303
+ return {
304
+ rate_card: isNonEmptyString(pricing.rate_card) ? pricing.rate_card : null,
305
+ models,
306
+ };
307
+ }
308
+
221
309
  /**
222
310
  * Validate a knowledge file: `entries:` list of learned facts.
223
311
  * Schema adapted from the best pattern found in the wild (metaswarm):
@@ -67,3 +67,24 @@ limits:
67
67
  long_wait: hold
68
68
  resume_margin_minutes: 5
69
69
  keep_awake: false # a real boolean, so the bare-off trap above cannot apply
70
+
71
+ # What a model costs, per MILLION tokens. Optional; absent means the spend
72
+ # report counts tokens and shows no money, which is the honest default —
73
+ # Tyran does not know what you pay. Operator-written from a vendor's price
74
+ # list, never inferred, so there is no provenance wrapper here.
75
+ #
76
+ # All four rates are required on any model you price: a table carrying three
77
+ # of them would price the fourth at zero, silently, and cache reads alone
78
+ # measure at roughly three quarters of a real session's cost. A model you
79
+ # leave out is reported as unpriced rather than as free.
80
+ #
81
+ # See docs/cost.md. Uncomment and fill in the ids your platform reports —
82
+ # `node scripts/cost.mjs --dir .tyran` lists them under "By model".
83
+ # pricing:
84
+ # rate_card: 'list-2026-08' # the label that travels with every amount
85
+ # models:
86
+ # some-model-id:
87
+ # input: 15
88
+ # cache_write: 18.75
89
+ # cache_read: 1.5
90
+ # output: 75