@trazum/core 2.7.11 → 3.0.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 (115) hide show
  1. package/README.md +1 -1
  2. package/dist/advisories.d.ts +14 -7
  3. package/dist/advisories.d.ts.map +1 -1
  4. package/dist/advisories.js +27 -20
  5. package/dist/advisories.js.map +1 -1
  6. package/dist/budget.d.ts.map +1 -1
  7. package/dist/budget.js +2 -1
  8. package/dist/budget.js.map +1 -1
  9. package/dist/connector.js +1 -1
  10. package/dist/connector.js.map +1 -1
  11. package/dist/conversation.d.ts.map +1 -1
  12. package/dist/conversation.js +4 -2
  13. package/dist/conversation.js.map +1 -1
  14. package/dist/i18n/en.js +2 -2
  15. package/dist/i18n/en.js.map +1 -1
  16. package/dist/i18n/es.js +2 -2
  17. package/dist/i18n/es.js.map +1 -1
  18. package/dist/i18n/types.d.ts +2 -1
  19. package/dist/i18n/types.d.ts.map +1 -1
  20. package/dist/index.d.ts +3 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +2 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/input-shape.d.ts.map +1 -1
  25. package/dist/input-shape.js +4 -2
  26. package/dist/input-shape.js.map +1 -1
  27. package/dist/openrouter.d.ts +1 -1
  28. package/dist/openrouter.d.ts.map +1 -1
  29. package/dist/openrouter.js +1 -2
  30. package/dist/openrouter.js.map +1 -1
  31. package/dist/phrases.d.ts +39 -0
  32. package/dist/phrases.d.ts.map +1 -1
  33. package/dist/phrases.js +130 -0
  34. package/dist/phrases.js.map +1 -1
  35. package/dist/pricing-overlay.d.ts +1 -1
  36. package/dist/pricing-overlay.d.ts.map +1 -1
  37. package/dist/pricing-overlay.js +21 -11
  38. package/dist/pricing-overlay.js.map +1 -1
  39. package/dist/pricing.d.ts +33 -6
  40. package/dist/pricing.d.ts.map +1 -1
  41. package/dist/pricing.js +77 -115
  42. package/dist/pricing.js.map +1 -1
  43. package/dist/repeats.d.ts.map +1 -1
  44. package/dist/repeats.js +4 -2
  45. package/dist/repeats.js.map +1 -1
  46. package/dist/reprice.js +1 -1
  47. package/dist/reprice.js.map +1 -1
  48. package/dist/review.js +2 -1
  49. package/dist/review.js.map +1 -1
  50. package/dist/rules.d.ts.map +1 -1
  51. package/dist/rules.js +119 -3
  52. package/dist/rules.js.map +1 -1
  53. package/dist/segment.d.ts +31 -1
  54. package/dist/segment.d.ts.map +1 -1
  55. package/dist/segment.js +128 -2
  56. package/dist/segment.js.map +1 -1
  57. package/dist/session-cost.d.ts.map +1 -1
  58. package/dist/session-cost.js +4 -2
  59. package/dist/session-cost.js.map +1 -1
  60. package/dist/session-ledger.d.ts.map +1 -1
  61. package/dist/session-ledger.js +4 -2
  62. package/dist/session-ledger.js.map +1 -1
  63. package/dist/structure.d.ts +34 -0
  64. package/dist/structure.d.ts.map +1 -1
  65. package/dist/structure.js +143 -12
  66. package/dist/structure.js.map +1 -1
  67. package/dist/truncation-retry.d.ts.map +1 -1
  68. package/dist/truncation-retry.js +4 -2
  69. package/dist/truncation-retry.js.map +1 -1
  70. package/dist/ttl-fit.d.ts.map +1 -1
  71. package/dist/ttl-fit.js +4 -2
  72. package/dist/ttl-fit.js.map +1 -1
  73. package/dist/types.d.ts +17 -7
  74. package/dist/types.d.ts.map +1 -1
  75. package/dist/usage.d.ts.map +1 -1
  76. package/dist/usage.js +13 -12
  77. package/dist/usage.js.map +1 -1
  78. package/dist/watch.d.ts +11 -1
  79. package/dist/watch.d.ts.map +1 -1
  80. package/dist/watch.js.map +1 -1
  81. package/dist/weekly.d.ts +140 -0
  82. package/dist/weekly.d.ts.map +1 -0
  83. package/dist/weekly.js +176 -0
  84. package/dist/weekly.js.map +1 -0
  85. package/dist/write.js +2 -2
  86. package/dist/write.js.map +1 -1
  87. package/package.json +1 -1
  88. package/src/advisories.ts +41 -25
  89. package/src/budget.ts +2 -1
  90. package/src/connector.ts +1 -1
  91. package/src/conversation.ts +4 -2
  92. package/src/i18n/en.ts +2 -2
  93. package/src/i18n/es.ts +2 -2
  94. package/src/i18n/types.ts +2 -1
  95. package/src/index.ts +4 -2
  96. package/src/input-shape.ts +4 -2
  97. package/src/openrouter.ts +1 -2
  98. package/src/phrases.ts +134 -0
  99. package/src/pricing-overlay.ts +23 -14
  100. package/src/pricing.ts +82 -120
  101. package/src/repeats.ts +4 -2
  102. package/src/reprice.ts +1 -1
  103. package/src/review.ts +1 -1
  104. package/src/rules.ts +135 -2
  105. package/src/segment.ts +131 -3
  106. package/src/session-cost.ts +4 -2
  107. package/src/session-ledger.ts +4 -2
  108. package/src/structure.ts +152 -13
  109. package/src/truncation-retry.ts +4 -2
  110. package/src/ttl-fit.ts +4 -2
  111. package/src/types.ts +18 -7
  112. package/src/usage.ts +13 -12
  113. package/src/watch.ts +12 -1
  114. package/src/weekly.ts +281 -0
  115. package/src/write.ts +2 -2
package/src/repeats.ts CHANGED
@@ -121,8 +121,10 @@ export function createRepeatsTracker(options: RepeatsOptions): RepeatsTracker {
121
121
  if (before.tokens !== tokens) return;
122
122
 
123
123
  const at = billedAt(record, on);
124
- const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, promptSizeOf(record));
125
- const rates = multipliersFor(model, at);
124
+ const how = promptSizeOf(record);
125
+ const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, how);
126
+ // The same call, so a batch call's cache read is the Batch table's.
127
+ const rates = multipliersFor(model, at, how);
126
128
  const per = (count: number, rate: number): number => (count / 1_000_000) * rate;
127
129
  slice.repeats += 1;
128
130
  slice.usd +=
package/src/reprice.ts CHANGED
@@ -206,7 +206,7 @@ function priceTokensRanged(
206
206
  size: PromptSize,
207
207
  ): { usd: number; floorUsd: number | null } {
208
208
  const priced = effectivePricing(model, on, size);
209
- const rates = multipliersFor(model, on);
209
+ const rates = multipliersFor(model, on, priced);
210
210
  const per = (tokens: number, rate: number): number => (tokens / 1_000_000) * rate;
211
211
  const at = (inputPerMTok: number, outputPerMTok: number): number =>
212
212
  per(breakdown.inputTokens, inputPerMTok) +
package/src/review.ts CHANGED
@@ -163,7 +163,7 @@ export async function reviewExamples(
163
163
 
164
164
  const fresh = group.redundant.filter((index) => !claimed.has(index) && index !== group.keep);
165
165
  if (fresh.length === 0) continue;
166
- fresh.forEach((index) => claimed.add(index));
166
+ for (const index of fresh) claimed.add(index);
167
167
 
168
168
  groups.push({
169
169
  ...group,
package/src/rules.ts CHANGED
@@ -6,6 +6,10 @@ import {
6
6
  POLITENESS,
7
7
  SELF_CHECK,
8
8
  SHOUTED_WORDS,
9
+ THANKS,
10
+ THANKS_DEGREE,
11
+ THANKS_FOR,
12
+ THANKS_NOT_A_THANKS,
9
13
  VERBOSE_PHRASES,
10
14
  } from './phrases.js';
11
15
  import { jaccard, normalizeForCompare } from './similarity.js';
@@ -70,11 +74,41 @@ function dropRegex(phrase: string): RegExp {
70
74
  // The boundary check goes AFTER the optional comma: in front of it, the "y"
71
75
  // of "y, si no te importa," would fail the match and the comma would stay.
72
76
  return new RegExp(
73
- `(?:,[^\\S\\n]*)?(?<![\\p{L}\\p{N}_])${body}[^\\S\\n]*,?(?![\\p{L}\\p{N}_])`,
77
+ `(?:,[^\\S\\n]*)?(?<![\\p{L}\\p{N}_])${body}${WHERE_ONLY[phrase]?.(body) ?? ''}[^\\S\\n]*,?(?![\\p{L}\\p{N}_])`,
74
78
  'giu',
75
79
  );
76
80
  }
77
81
 
82
+ /**
83
+ * Three courtesy words that are also ordinary words, and where each one is
84
+ * the courtesy.
85
+ *
86
+ * Deleting them wherever they stood changed instructions at the safe level,
87
+ * measured on 2026-11-17:
88
+ *
89
+ * Aim to please the customer. -> Aim to the customer.
90
+ * Speak kindly to the customer. -> Speak to the customer.
91
+ * Die Bitte des Kunden steht ... -> Die des Kunden steht ...
92
+ *
93
+ * "please" after "to" is the verb. "kindly" is a courtesy only in front of
94
+ * what is being asked, which puts it at the start of a clause or right after
95
+ * "you" or "and"; anywhere else it says how to treat somebody. German "bitte"
96
+ * after a determiner is the noun, a request.
97
+ *
98
+ * **Each check is a lookbehind placed after the word, looking back across
99
+ * it.** In front of the word it would run at every position in the text, and
100
+ * each run walks back over the whitespace before it: quadratic on a long run
101
+ * of spaces, 47 seconds on 200,000 characters, which `security.test.js` caught
102
+ * on the first try. Behind the word it runs only where the word is.
103
+ */
104
+ const WS = '[^\\S\\n]';
105
+ const WHERE_ONLY: Readonly<Record<string, (body: string) => string>> = {
106
+ please: (body) => `(?<!\\bto${WS}+${body})`,
107
+ kindly: (body) => `(?<=(?:^|[\\n.!?;:,(]|\\byou|\\band)${WS}*${body})`,
108
+ bitte: (body) =>
109
+ `(?<!\\b(?:die|der|eine|einer|diese|dieser|ihre|ihrer|seine|seiner|meine|unsere|jede|keine)${WS}+${body})`,
110
+ };
111
+
78
112
  /** A rule that deletes a set of phrases. */
79
113
  function dropRule(id: RuleId, level: Rule['level'], phrases: readonly string[]): Rule {
80
114
  // Longest first, so "thank you very much" wins over "thanks".
@@ -185,7 +219,106 @@ const verbosePhrasesRule: Rule = {
185
219
  },
186
220
  };
187
221
 
188
- const politenessRule = dropRule('politeness', 'safe', POLITENESS);
222
+ const NOT_SPACE_OR_BREAK = '[^\\S\\n]';
223
+ /** Longest first, spaces loosened to any run that stays on the line. */
224
+ const anyOf = (phrases: readonly string[]): string =>
225
+ [...phrases]
226
+ .sort((a, b) => b.length - a.length)
227
+ .map((phrase) => escapeRe(phrase).replace(/\s+/g, `${NOT_SPACE_OR_BREAK}+`))
228
+ .join('|');
229
+
230
+ const WORD_END = '(?![\\p{L}\\p{N}_])';
231
+
232
+ /**
233
+ * A sentence that does nothing but thank.
234
+ *
235
+ * 1 what comes before it: the start of a line, or the space after the
236
+ * punctuation that ended the previous sentence
237
+ * the thanks, then how much ("so much", "de antemano"), any number of times
238
+ * 2 optionally what it is for: "for" and up to eighty characters holding no
239
+ * punctuation, no quotation mark and no protected span
240
+ * then the end of the sentence: its punctuation, or the end of the line
241
+ *
242
+ * A thanks followed by anything else does not match, and that is the point:
243
+ * "thanks to the new policy", "thank you messages should be short" and
244
+ * "thanks, John" are all left for a reader to deal with.
245
+ */
246
+ const THANKS_SENTENCE = new RegExp(
247
+ `(^${NOT_SPACE_OR_BREAK}*|(?<=[.!?…])${NOT_SPACE_OR_BREAK}+)`
248
+ + `[¡¿]*(?:${anyOf(THANKS)})${WORD_END}`
249
+ + `(?:${NOT_SPACE_OR_BREAK}+(?:${anyOf(THANKS_DEGREE)})${WORD_END})*`
250
+ + `(?:${NOT_SPACE_OR_BREAK}+(?:${anyOf(THANKS_FOR)})${NOT_SPACE_OR_BREAK}+`
251
+ + `([^.!?…\\n,;:"'“”‘’«»„()\\uE000\\uE001]{1,80}))?`
252
+ + `${NOT_SPACE_OR_BREAK}*(?:[.!?…]+|$)`,
253
+ 'gimu',
254
+ );
255
+
256
+ const NOT_A_THANKS = new RegExp(`(?<![\\p{L}\\p{N}_])(?:${anyOf(THANKS_NOT_A_THANKS)})${WORD_END}`, 'iu');
257
+ /** A list marker with nothing after it yet: "1.", "a)", "-". */
258
+ const LIST_MARKER = /^[^\S\n]*(?:\p{N}+[.)]|\p{L}[.)]|[-*•])[^\S\n]*$/u;
259
+
260
+ /**
261
+ * Removes the sentences that only thank, and leaves every other thanks alone.
262
+ *
263
+ * The regex finds a candidate; four checks then decide whether it is a
264
+ * courtesy the author added or text the prompt is about. Each is a case that
265
+ * was measured going wrong, and each resolves the doubt the same way: the text
266
+ * stays.
267
+ */
268
+ function removeThanks(text: string): { text: string; hits: number } {
269
+ let hits = 0;
270
+ const out = text.replace(THANKS_SENTENCE, (whole: string, lead: string, what: string | undefined, offset: number) => {
271
+ const lineStart = text.lastIndexOf('\n', offset - 1) + 1;
272
+ const before = text.slice(lineStart, offset);
273
+ const sentence = whole.slice(lead.length);
274
+
275
+ if (before.trim() === '') {
276
+ // On a line of its own, under a line that ends in a colon, it is what
277
+ // that line introduced: a template, an example, the text to send.
278
+ const above = text.slice(0, lineStart).trimEnd();
279
+ if (above.endsWith(':')) return whole;
280
+ } else {
281
+ // After a full stop in the middle of a line, a sentence begins with a
282
+ // capital. A lower-case "thanks" there follows an abbreviation: "e.g.
283
+ // thanks for your time."
284
+ const first = sentence.replace(/^[¡¿]+/, '')[0] ?? '';
285
+ if (first === first.toLowerCase()) return whole;
286
+ // "1. Thank you for your order." is an item in a list, not a closing.
287
+ if (LIST_MARKER.test(before)) return whole;
288
+ }
289
+ // "Thank you for your order is the subject line" is about the phrase.
290
+ if (what !== undefined && NOT_A_THANKS.test(what)) return whole;
291
+
292
+ hits++;
293
+ return '';
294
+ });
295
+ return { text: out, hits };
296
+ }
297
+
298
+ const THANKING = new Set(THANKS.map((phrase) => phrase.toLowerCase()));
299
+ const softeners = dropRule(
300
+ 'politeness',
301
+ 'safe',
302
+ POLITENESS.filter((phrase) => !THANKING.has(phrase.toLowerCase())),
303
+ );
304
+
305
+ /**
306
+ * Courtesy, in two halves that used to be one.
307
+ *
308
+ * A softener ("please", "por favor", "if you don't mind") is deleted where it
309
+ * stands, as before. A thanks is deleted only as a whole sentence; see
310
+ * `THANKS` in `phrases.ts` for what deleting the phrase alone did.
311
+ */
312
+ const politenessRule: Rule = {
313
+ id: 'politeness',
314
+ level: 'safe',
315
+ apply(text) {
316
+ const thanks = removeThanks(text);
317
+ const rest = softeners.apply(thanks.text);
318
+ const hits = thanks.hits + rest.hits;
319
+ return { text: hits > 0 ? tidyAfterRemoval(rest.text) : text, hits };
320
+ },
321
+ };
189
322
  const fillerRule = dropRule('filler', 'safe', FILLER);
190
323
  const intensifiersRule = dropRule('intensifiers', 'aggressive', INTENSIFIERS);
191
324
  const hedgesRule = dropRule('hedges', 'aggressive', HEDGES);
package/src/segment.ts CHANGED
@@ -137,6 +137,127 @@ interface Match {
137
137
  kind: ProtectionKind;
138
138
  }
139
139
 
140
+ /**
141
+ * Text inside quotation marks, which a prompt quotes because the words matter.
142
+ *
143
+ * `Start every reply with "Thank you for contacting us".` is an instruction
144
+ * about a string, and the string is the one part of it that must not change.
145
+ * Before this the rules read it as prose. Measured on 2026-11-17, at the safe
146
+ * level:
147
+ *
148
+ * Reply "Please hold on" when the line is busy. -> Reply " hold on" when ...
149
+ * If the customer writes "basically nothing works", escalate. -> ... " nothing works" ...
150
+ * Say «por favor espere» while loading. -> Say « espere» while loading.
151
+ *
152
+ * The model was being told to output, or to match, a different string, and
153
+ * the report counted the difference as tokens saved.
154
+ *
155
+ * **One line, and bounded.** A quotation mark with no partner on its line is
156
+ * an inch sign, a typo or a paragraph-long quotation, and none of those is a
157
+ * string somebody will compare against; the bound also keeps every pattern
158
+ * linear, which this file has learnt twice to check.
159
+ *
160
+ * **Apostrophes are not quotation marks.** A straight single quote opens a
161
+ * quotation only when no letter or digit comes before it, and closes one only
162
+ * when none comes after, so "don't" and "the customers' orders" open nothing.
163
+ */
164
+ const QUOTED = new RegExp(
165
+ [
166
+ '"[^"\\n]{1,400}"',
167
+ '“[^”\\n]{1,400}”',
168
+ '„[^“”\\n]{1,400}[“”]',
169
+ '«[^»\\n]{1,400}»',
170
+ "(?<![\\p{L}\\p{N}])'[^'\\n]{1,400}'(?![\\p{L}\\p{N}])",
171
+ '(?<![\\p{L}\\p{N}])‘[^’\\n]{1,400}’(?![\\p{L}\\p{N}])',
172
+ ].join('|'),
173
+ 'gu',
174
+ );
175
+
176
+ /** Stands in for a character somebody else already protects. Not a quotation mark, not a line end. */
177
+ const TAKEN = '\uE002';
178
+
179
+ /**
180
+ * Quoted spans, found with the earlier protections already in place.
181
+ *
182
+ * Run as an ordinary pattern it would lose either way round. After the
183
+ * others, a quotation holding a placeholder overlaps a reservation and is
184
+ * skipped, so `"Hello {{name}}, please hold"` stays editable. Before them, a
185
+ * quotation mark inside a code fence or a tag's attribute reserves a slice of
186
+ * it and the fence or the tag is the one skipped.
187
+ *
188
+ * So the text is scanned with every reserved character replaced by one that
189
+ * is neither a quotation mark nor a line end. A span on one line is swallowed
190
+ * whole by the quotation around it, a reservation that spans lines cannot be
191
+ * crossed, and a quotation mark inside a reservation is not seen at all.
192
+ */
193
+ function quotedSpans(text: string, reserved: readonly Match[]): Match[] {
194
+ if (!/["“„«'‘]/.test(text)) return [];
195
+ let skeleton = text;
196
+ if (reserved.length > 0) {
197
+ // By UTF-16 unit, the unit every offset in this file is in.
198
+ const units = text.split('');
199
+ for (const { start, end } of reserved) {
200
+ for (let index = start; index < end; index++) {
201
+ if (units[index] !== '\n') units[index] = TAKEN;
202
+ }
203
+ }
204
+ skeleton = units.join('');
205
+ }
206
+ const found: Match[] = [];
207
+ for (const match of skeleton.matchAll(QUOTED)) {
208
+ found.push({ start: match.index, end: match.index + match[0].length, kind: 'quoted' });
209
+ }
210
+ return found;
211
+ }
212
+
213
+ /**
214
+ * The first of `ranges` that overlaps `[start, end)`, if one does.
215
+ *
216
+ * `ranges` is in order of where each begins and no two of them overlap, which
217
+ * makes it in order of where each ends as well. So the first range that ends
218
+ * after `start` is the only one that can be the first to overlap, and it is
219
+ * found by halving, not by looking at each.
220
+ *
221
+ * It was looked for one by one, with `find`, once for every match. A text of
222
+ * nothing but protected spans has as many reservations as matches, and the
223
+ * work grew with the square of them: 70 kB of placeholders, tags and code
224
+ * spans took half a second, and the 400 kB the HTTP API accepts took 26 to 30
225
+ * seconds, measured on 2026-11-18 (#716). A test with a five-second budget
226
+ * was the only thing that had noticed, and only on a slow machine.
227
+ */
228
+ export function firstOverlap(ranges: readonly Match[], start: number, end: number): Match | undefined {
229
+ let low = 0;
230
+ let high = ranges.length;
231
+ while (low < high) {
232
+ const middle = (low + high) >>> 1;
233
+ const range = ranges[middle];
234
+ if (range !== undefined && range.end > start) high = middle;
235
+ else low = middle + 1;
236
+ }
237
+ const candidate = ranges[low];
238
+ return candidate !== undefined && candidate.start < end ? candidate : undefined;
239
+ }
240
+
241
+ /**
242
+ * Whether one of `quotes` holds the whole of `[start, end)`.
243
+ *
244
+ * `quotes` is in order and no two overlap, so the only one that can hold the
245
+ * span is the last to begin at or before `start`. Asked of every reservation,
246
+ * and answered by looking at every quotation, it was the same square again.
247
+ */
248
+ export function heldBy(quotes: readonly Match[], start: number, end: number): boolean {
249
+ let low = 0;
250
+ let high = quotes.length;
251
+ while (low < high) {
252
+ const middle = (low + high) >>> 1;
253
+ const quote = quotes[middle];
254
+ if (quote !== undefined && quote.start <= start) low = middle + 1;
255
+ else high = middle;
256
+ }
257
+ const candidate = quotes[low - 1];
258
+ return candidate !== undefined && end <= candidate.end;
259
+ }
260
+
140
261
  /** Splits the text into segments, marking what must not be modified. */
141
262
  export function segment(text: string): Segment[] {
142
263
  /*
@@ -158,8 +279,6 @@ export function segment(text: string): Segment[] {
158
279
 
159
280
  for (const { kind, regex, trimTrailing } of PATTERNS) {
160
281
  const reserved = [...kept].sort((a, b) => a.start - b.start);
161
- const overlapping = (start: number, end: number): Match | undefined =>
162
- reserved.find((range) => start < range.end && end > range.start);
163
282
 
164
283
  // Copy the regex so lastIndex is not shared between calls.
165
284
  const re = new RegExp(regex.source, regex.flags);
@@ -174,7 +293,7 @@ export function segment(text: string): Segment[] {
174
293
  if (matched.length === 0) continue;
175
294
  const start = m.index;
176
295
  const end = start + matched.length;
177
- const hit = overlapping(start, end);
296
+ const hit = firstOverlap(reserved, start, end);
178
297
  if (hit !== undefined) {
179
298
  // Not simply skipped: the scan resumes where the reservation ends, so
180
299
  // a legitimate match sitting just past it is still found.
@@ -185,6 +304,15 @@ export function segment(text: string): Segment[] {
185
304
  }
186
305
  }
187
306
 
307
+ // Quotations last, around whatever is already reserved inside them: a
308
+ // reservation a quotation swallows is dropped in favour of the quotation.
309
+ const quoted = quotedSpans(text, kept);
310
+ if (quoted.length > 0) {
311
+ const outside = kept.filter((range) => !heldBy(quoted, range.start, range.end));
312
+ kept.length = 0;
313
+ kept.push(...outside, ...quoted);
314
+ }
315
+
188
316
  // Order for emission; within one pattern matches never overlap, and across
189
317
  // patterns the reservation check above already guaranteed it.
190
318
  kept.sort((a, b) => a.start - b.start);
@@ -78,8 +78,10 @@ export function costOf(record: UsageRecord, catalogue: PricingCatalogue, on: Dat
78
78
  if (!model) return null;
79
79
  // At the call's own moment and on its own prompt, as the bill prices it.
80
80
  const at = billedAt(record, on);
81
- const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, promptSizeOf(record));
82
- const rates = multipliersFor(model, at);
81
+ const how = promptSizeOf(record);
82
+ const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, how);
83
+ // The same call, so a batch call's cache read is the Batch table's.
84
+ const rates = multipliersFor(model, at, how);
83
85
  const per = (tokens: number, rate: number): number => (tokens / 1_000_000) * rate;
84
86
  return (
85
87
  per(record.inputTokens, inputPerMTok) +
@@ -120,8 +120,10 @@ export function createSessionLedgerTracker(options: SessionLedgerOptions): Sessi
120
120
  tally.turns += 1;
121
121
  const model = catalogue.byId.get(record.model)!;
122
122
  const at = billedAt(record, on);
123
- const { inputPerMTok } = effectivePricing(model, at, promptSizeOf(record));
124
- const rates = multipliersFor(model, at);
123
+ const how = promptSizeOf(record);
124
+ const { inputPerMTok } = effectivePricing(model, at, how);
125
+ // The same call, so a batch call's cache read is the Batch table's.
126
+ const rates = multipliersFor(model, at, how);
125
127
  // The bill's own convention: unstated TTLs sit in the 5m bucket at the cheaper rate.
126
128
  tally.writeUsd +=
127
129
  ((record.cacheWrite5mTokens * rates.cacheWrite5m + record.cacheWrite1hTokens * rates.cacheWrite1h) / 1_000_000)
package/src/structure.ts CHANGED
@@ -782,19 +782,7 @@ export function findRestatedFormat(prompt: string, count: TokenCounter): Restate
782
782
  const prose = analysableText(prompt);
783
783
  const proseSentences = sentences(prose);
784
784
 
785
- const restatedKeys = new Set<string>();
786
- const guiltySentences = new Set<number>();
787
-
788
- proseSentences.forEach((sentence, index) => {
789
- for (const key of keys) {
790
- // Word boundary that also survives snake_case and kebab-case names.
791
- const pattern = new RegExp(`(?<![\\p{L}\\p{N}_-])${escapeRegExp(key)}(?![\\p{L}\\p{N}_-])`, 'iu');
792
- if (pattern.test(sentence)) {
793
- restatedKeys.add(key);
794
- guiltySentences.add(index);
795
- }
796
- }
797
- });
785
+ const { restatedKeys, guiltySentences } = keysInSentences(keys, proseSentences);
798
786
 
799
787
  if (restatedKeys.size < RESTATED_KEY_MINIMUM) return null;
800
788
 
@@ -811,6 +799,157 @@ function escapeRegExp(text: string): string {
811
799
  return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
812
800
  }
813
801
 
802
+ /**
803
+ * Which keys the prose names, and in which sentences: a key counts where it
804
+ * stands with no letter, digit, `_` or `-` on either side, compared without
805
+ * case. The pattern that says so exactly is `keyPattern`.
806
+ *
807
+ * **It used to ask that pattern of every sentence for every key, building it
808
+ * anew each time**, and that is the square of the text: a prompt of 40,000
809
+ * characters, half a schema of 1,778 keys and half prose, made `optimize`
810
+ * take 6.8 seconds, and one of 80,000 characters took 38, measured on
811
+ * 2026-11-18 (#772). The HTTP API accepts 400,000.
812
+ *
813
+ * So the prose is read once. It is cut where a key can start or end: a run of
814
+ * the characters the boundary looks at, or one other character. A key is cut
815
+ * the same way, and both are folded so that what the pattern calls equal is
816
+ * equal here. Every key is then looked for in one pass over the pieces, the
817
+ * way a dictionary of words is looked for in a text (Aho and Corasick, 1975),
818
+ * and a place where one is found is confirmed by the key's own pattern,
819
+ * anchored there. The pattern still decides; this only finds where to ask it.
820
+ *
821
+ * The answer is the one the old loop gave, in its order: `restated-format-scaling.test.js`
822
+ * holds the two side by side, and holds the fold to the engine's own
823
+ * case-insensitive comparison over every cased character.
824
+ */
825
+ export function keysInSentences(
826
+ keys: readonly string[],
827
+ sentences: readonly string[],
828
+ ): { restatedKeys: Set<string>; guiltySentences: Set<number> } {
829
+ const restatedKeys = new Set<string>();
830
+ const guiltySentences = new Set<number>();
831
+ if (keys.length === 0 || sentences.length === 0) return { restatedKeys, guiltySentences };
832
+
833
+ const patterns = keys.map((key) => keyPattern(key, 'iuy'));
834
+ const seen = new Map<string, { fold: string; run: boolean }>();
835
+ const lengths: number[] = [];
836
+ const root: KeyNode = { next: new Map(), fail: null, ends: [], link: null };
837
+ keys.forEach((key, index) => {
838
+ const pieces = keyPieces(key, seen);
839
+ lengths.push(pieces.length);
840
+ let node = root;
841
+ for (const { piece } of pieces) {
842
+ let child = node.next.get(piece);
843
+ if (child === undefined) {
844
+ child = { next: new Map(), fail: root, ends: [], link: null };
845
+ node.next.set(piece, child);
846
+ }
847
+ node = child;
848
+ }
849
+ node.ends.push(index);
850
+ });
851
+ const queue = [...root.next.values()];
852
+ for (let head = 0; head < queue.length; head++) {
853
+ const node = queue[head]!;
854
+ for (const [piece, child] of node.next) {
855
+ let fail = node.fail!;
856
+ while (fail !== root && !fail.next.has(piece)) fail = fail.fail!;
857
+ child.fail = node === root ? root : (fail.next.get(piece) ?? root);
858
+ child.link = child.fail.ends.length > 0 ? child.fail : child.fail.link;
859
+ queue.push(child);
860
+ }
861
+ }
862
+
863
+ sentences.forEach((sentence, index) => {
864
+ const pieces = keyPieces(sentence, seen);
865
+ const found = new Set<number>();
866
+ let node = root;
867
+ pieces.forEach(({ piece }, at) => {
868
+ while (node !== root && !node.next.has(piece)) node = node.fail!;
869
+ node = node.next.get(piece) ?? root;
870
+ for (let end: KeyNode | null = node.ends.length > 0 ? node : node.link; end !== null; end = end.link) {
871
+ for (const key of end.ends) {
872
+ if (found.has(key)) continue;
873
+ const first = at - lengths[key]! + 1;
874
+ // The pattern's own boundary, read off the pieces: a run of key
875
+ // characters is never cut, so one on either side is a letter there.
876
+ if (pieces[first - 1]?.run === true || pieces[at + 1]?.run === true) continue;
877
+ const pattern = patterns[key]!;
878
+ pattern.lastIndex = pieces[first]!.at;
879
+ if (pattern.test(sentence)) found.add(key);
880
+ }
881
+ }
882
+ });
883
+ if (found.size === 0) return;
884
+ guiltySentences.add(index);
885
+ for (const key of [...found].sort((a, b) => a - b)) restatedKeys.add(keys[key]!);
886
+ });
887
+ return { restatedKeys, guiltySentences };
888
+ }
889
+
890
+ /** A key, named in prose: not inside a longer name, and in any case. */
891
+ export function keyPattern(key: string, flags = 'iu'): RegExp {
892
+ // Word boundary that also survives snake_case and kebab-case names.
893
+ return new RegExp(`(?<![\\p{L}\\p{N}_-])${escapeRegExp(key)}(?![\\p{L}\\p{N}_-])`, flags);
894
+ }
895
+
896
+ interface KeyNode {
897
+ next: Map<string, KeyNode>;
898
+ fail: KeyNode | null;
899
+ /** The keys that end here, by index. */
900
+ ends: number[];
901
+ /** The nearest node down the fail chain where a key ends. */
902
+ link: KeyNode | null;
903
+ }
904
+
905
+ /** What `keyPattern`'s boundary reads as part of a name, under the same flags. */
906
+ const KEY_CHAR = /^[\p{L}\p{N}_-]$/iu;
907
+
908
+ /**
909
+ * Code points the engine compares as equal without case and that a lower- or
910
+ * upper-casing does not bring together. Found by comparing `foldKeyChar` with
911
+ * the engine over every code point on Node 22; `restated-format-scaling.test.js` repeats
912
+ * that comparison over the cased ones on whatever runs the tests. A pair the
913
+ * engine does not join costs nothing: the key's own pattern confirms a match.
914
+ */
915
+ const FOLD_TOGETHER: Readonly<Record<string, string>> = {
916
+ 'ΐ': 'ΐ',
917
+ 'ΰ': 'ΰ',
918
+ 'st': 'ſt',
919
+ };
920
+
921
+ /**
922
+ * One code point as `keyPieces` compares it: lowered, raised and lowered
923
+ * again, each step taken only when it gives one code point back.
924
+ */
925
+ export function foldKeyChar(char: string): string {
926
+ let fold = char;
927
+ for (const change of [(text: string) => text.toLowerCase(), (text: string) => text.toUpperCase(), (text: string) => text.toLowerCase()]) {
928
+ const next = change(fold);
929
+ if ([...next].length === 1) fold = next;
930
+ }
931
+ return FOLD_TOGETHER[fold] ?? fold;
932
+ }
933
+
934
+ /** A text cut where a key can start or end: each run of name characters, and each other character, folded, with where it starts. */
935
+ function keyPieces(text: string, seen: Map<string, { fold: string; run: boolean }>): Array<{ piece: string; at: number; run: boolean }> {
936
+ const pieces: Array<{ piece: string; at: number; run: boolean }> = [];
937
+ let at = 0;
938
+ for (const char of text) {
939
+ let known = seen.get(char);
940
+ if (known === undefined) {
941
+ known = { fold: foldKeyChar(char), run: KEY_CHAR.test(char) };
942
+ seen.set(char, known);
943
+ }
944
+ const { fold, run } = known;
945
+ const last = pieces[pieces.length - 1];
946
+ if (run && last !== undefined && last.run) last.piece += fold;
947
+ else pieces.push({ piece: fold, at, run });
948
+ at += char.length;
949
+ }
950
+ return pieces;
951
+ }
952
+
814
953
  // --------------------------------------------------------------------------
815
954
  // An output schema the API could carry instead of the prompt
816
955
  // --------------------------------------------------------------------------
@@ -92,8 +92,10 @@ export function createTruncationRetryTracker(options: TruncationRetryOptions): T
92
92
  const model = catalogue.byId.get(record.model);
93
93
  if (!model) return null;
94
94
  const at = billedAt(record, on);
95
- const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, promptSizeOf(record));
96
- const rates = multipliersFor(model, at);
95
+ const how = promptSizeOf(record);
96
+ const { inputPerMTok, outputPerMTok } = effectivePricing(model, at, how);
97
+ // The same call, so a batch call's cache read is the Batch table's.
98
+ const rates = multipliersFor(model, at, how);
97
99
  const per = (count: number, rate: number): number => (count / 1_000_000) * rate;
98
100
  return (
99
101
  per(record.inputTokens, inputPerMTok) +
package/src/ttl-fit.ts CHANGED
@@ -163,8 +163,10 @@ export function createTtlFitTracker(options: TtlFitOptions): TtlFitTracker {
163
163
  if (record.cacheWrite1hTokens > 0) {
164
164
  const model = catalogue.byId.get(record.model)!;
165
165
  const at = billedAt(record, on);
166
- const { inputPerMTok } = effectivePricing(model, at, promptSizeOf(record));
167
- const rates = multipliersFor(model, at);
166
+ const how = promptSizeOf(record);
167
+ const { inputPerMTok } = effectivePricing(model, at, how);
168
+ // The same call, so a batch call's cache read is the Batch table's.
169
+ const rates = multipliersFor(model, at, how);
168
170
  slice.overlongUsd +=
169
171
  (record.cacheWrite1hTokens / 1_000_000) * inputPerMTok * (rates.cacheWrite1h - rates.cacheWrite5m);
170
172
  }
package/src/types.ts CHANGED
@@ -106,6 +106,16 @@ export interface CostMultipliers {
106
106
  * "nobody has said", and only the first should stop the advisory firing.
107
107
  */
108
108
  batch?: number | null;
109
+ /**
110
+ * A batch call's cache read, as a fraction of the **batch** input price,
111
+ * where the provider's Batch table prints a cache price that is not the
112
+ * standard ratio of it. Google's Batch tab for Gemini 3.1 Pro Preview reads
113
+ * "Context caching price: Same as Standard", $0.20 against a batch input of
114
+ * $1.00, so 0.2 where a standard call reads at 0.1. Absent means a batch
115
+ * call reads the cache at `cacheRead`, the same share of a halved input,
116
+ * which is what every other Batch table this catalogue was read from prints.
117
+ */
118
+ batchCacheRead?: number;
109
119
  }
110
120
 
111
121
  /**
@@ -322,14 +332,14 @@ export interface ModelPricing {
322
332
  /** What the provider answered, in the provider's words. */
323
333
  because: string;
324
334
  };
325
- /** Capability, on a vendor-neutral scale. */
326
- capability: Capability;
327
335
  /**
328
- * @deprecated Use `capability`. Anthropic's ladder used as a generic axis
329
- * stopped making sense once other providers were priced. Kept in step with
330
- * `capability` for the whole of 1.x and removed in 2.0 — see VERSIONING.md.
336
+ * Capability, on a vendor-neutral scale.
337
+ *
338
+ * The only one a model carries since 3.0.0, which removed `tier`, deprecated
339
+ * since 1.8.0. A pricing overlay that still writes `tier` is refused, naming
340
+ * the key, rather than read or ignored.
331
341
  */
332
- tier: 'haiku' | 'sonnet' | 'opus' | 'frontier' | 'unknown';
342
+ capability: Capability;
333
343
  /** Promotional pricing, when one is running. */
334
344
  promo?: {
335
345
  inputPerMTok: number;
@@ -509,7 +519,8 @@ export type ProtectionKind =
509
519
  | 'url'
510
520
  | 'email'
511
521
  | 'placeholder'
512
- | 'xml-tag';
522
+ | 'xml-tag'
523
+ | 'quoted';
513
524
 
514
525
  /** Optimisation options. */
515
526
  export interface OptimizeOptions {