@supa-media/tex-contract 0.9.0 → 0.15.6

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.
@@ -76,6 +76,15 @@ const HISTORY_RESERVE = 16_000;
76
76
  */
77
77
  const BULK_FLOOR = 8_000;
78
78
 
79
+ /**
80
+ * The most the system prompt and the tool list are kept to once a clip reaches them, in estimated tokens: two steps
81
+ * under the budget, the share `HISTORY_RESERVE` leaves them, so the person's ask and this turn's lookups still fit
82
+ * beside them. The cut to it depends on their own words only, never on the step, so every round of a turn that is
83
+ * trimmed sends the same prefix and reads it from the cache (production, 2026-11-27, app 1:1 turn 447ed548: a per-step
84
+ * clip changed the stable block mid-turn, and that round read 0 and wrote 84,495 tokens in 6.4 s).
85
+ */
86
+ const PREFIX_CAP = HAIKU_INPUT_BUDGET - 2 * TRIM_STEP;
87
+
79
88
  /** A picture or a PDF page, counted once. The base64 is not tokens. */
80
89
  const IMAGE_TOKENS = 1_600;
81
90
 
@@ -168,11 +177,11 @@ function clipMessageTail(message, dropTokens) {
168
177
  * Clip the system prompt and tool descriptions by about `dropTokens`, longest first, keeping each
169
178
  * one's end. Messages are not touched. Returns the number of clips made.
170
179
  */
171
- function clipBulk(body, dropTokens) {
180
+ function clipBulk(body, dropTokens, once = null) {
172
181
  let remaining = Math.ceil(dropTokens * TEXT_CHARS_PER_TOKEN);
173
182
  let clips = 0;
174
183
  while (remaining > 0) {
175
- const best = largestText(body, 0);
184
+ const best = largestText(body, 0, once);
176
185
  if (!best || best.text.length <= EARLIER.length + 1) break;
177
186
  const cutChars = Math.min(remaining, best.text.length - EARLIER.length - 1);
178
187
  const tail = best.text.slice(cutChars);
@@ -235,8 +244,8 @@ function stubHistory(messages, where) {
235
244
  * Messages from `end` on are left whole. Returns false when nothing long is
236
245
  * left, so the loop stops.
237
246
  */
238
- function clipLargest(body, end = Infinity) {
239
- const best = largestText(body, end);
247
+ function clipLargest(body, end = Infinity, once = null) {
248
+ const best = largestText(body, end, once);
240
249
  if (!best || best.text.length <= EARLIER.length + 1) return false;
241
250
  const tail = Math.max(0, Math.floor(best.text.length / 2) - EARLIER.length - 1);
242
251
  const clipped = tail === 0 ? EARLIER : `${EARLIER}\n${best.text.slice(-tail)}`;
@@ -245,21 +254,18 @@ function clipLargest(body, end = Infinity) {
245
254
  return true;
246
255
  }
247
256
 
248
- /** The longest text `clipLargest` may clip, as { text, set }, or null. */
249
- function largestText(body, end = Infinity) {
257
+ /**
258
+ * The longest text `clipLargest` may clip, as { text, set, key }, or null. `key` names a system block or a tool
259
+ * description (null for a message's text); one named in `once` was already cut to `PREFIX_CAP` in this call and is not
260
+ * offered again.
261
+ */
262
+ function largestText(body, end = Infinity, once = null) {
250
263
  let best = null;
251
- const offer = (text, set) => {
252
- if (typeof text === "string" && text.length > (best?.text.length ?? EARLIER.length + 1)) best = { text, set };
264
+ const offer = (text, set, key = null) => {
265
+ if (key !== null && once?.has(key)) return;
266
+ if (typeof text === "string" && text.length > (best?.text.length ?? EARLIER.length + 1)) best = { text, set, key };
253
267
  };
254
- if (typeof body.system === "string") offer(body.system, (value) => { body.system = value; });
255
- else if (Array.isArray(body.system)) {
256
- body.system.forEach((block, index) => {
257
- if (typeof block?.text === "string") offer(block.text, (value) => { body.system[index] = { ...block, text: value }; });
258
- });
259
- }
260
- (body.tools ?? []).forEach((tool, index) => {
261
- if (typeof tool?.description === "string") offer(tool.description, (value) => { body.tools[index] = { ...tool, description: value }; });
262
- });
268
+ eachPrefixText(body, offer);
263
269
  (body.messages ?? []).forEach((message, index) => {
264
270
  if (index >= end) return; // the turn's own messages, when the cut keeps them
265
271
  if (typeof message?.content === "string") offer(message.content, (value) => { body.messages[index] = { ...message, content: value }; });
@@ -273,6 +279,42 @@ function largestText(body, end = Infinity) {
273
279
  return best;
274
280
  }
275
281
 
282
+ /** Each system block and tool description of `body`, as offer(text, set, key). */
283
+ function eachPrefixText(body, offer) {
284
+ if (typeof body.system === "string") offer(body.system, (value) => { body.system = value; }, "sys");
285
+ else if (Array.isArray(body.system)) {
286
+ body.system.forEach((block, index) => {
287
+ if (typeof block?.text === "string") offer(block.text, (value) => { body.system[index] = { ...block, text: value }; }, `sys/${index}`);
288
+ });
289
+ }
290
+ (body.tools ?? []).forEach((tool, index) => {
291
+ if (typeof tool?.description === "string") offer(tool.description, (value) => { body.tools[index] = { ...tool, description: value }; }, `tool/${index}`);
292
+ });
293
+ }
294
+
295
+ /**
296
+ * Halve the longest system block or tool description until the two are under `PREFIX_CAP`, keeping each text's start
297
+ * (where the setup says who Tex is and its rules) and ending it on a whole line, then mark every one of them in
298
+ * `once` so nothing in this call but the last-resort clip cuts them again. Their words alone decide the cut, so every
299
+ * step makes the same one. Returns how many clips it made.
300
+ */
301
+ function capPrefix(body, once) {
302
+ let shed = 0;
303
+ while (bulkTokens(body) > PREFIX_CAP) {
304
+ let best = null;
305
+ eachPrefixText(body, (text, set) => {
306
+ if (text.length > (best?.text.length ?? 1)) best = { text, set };
307
+ });
308
+ if (!best) break;
309
+ const half = Math.floor(best.text.length / 2);
310
+ const line = best.text.lastIndexOf("\n", half);
311
+ best.set(best.text.slice(0, line > half * 0.8 ? line : half));
312
+ shed += 1;
313
+ }
314
+ eachPrefixText(body, (_text, _set, key) => once.add(key));
315
+ return shed;
316
+ }
317
+
276
318
  /** The longest tool result that still holds more than the stub, as { message, block, length }, or null. */
277
319
  function largestResult(messages) {
278
320
  let best = null;
@@ -338,7 +380,7 @@ const stepOf = (tokens) => Math.max(0, Math.ceil((tokens - HAIKU_INPUT_BUDGET) /
338
380
  * reach the step does the frame's newest tool result go, and then a clip, and
339
381
  * only on the frame.
340
382
  */
341
- function fitHaikuBody(body, { turnFrom } = {}) {
383
+ function fitHaikuBody(body, { turnFrom, capPrefix: withCap = false } = {}) {
342
384
  const before = estimateBodyTokens(body);
343
385
  if (before <= HAIKU_INPUT_BUDGET) return { body, before, after: before, shed: 0 };
344
386
  const next = JSON.parse(JSON.stringify(body));
@@ -381,8 +423,14 @@ function fitHaikuBody(body, { turnFrom } = {}) {
381
423
  if (drop > 0 && clipMessageTail(fm[straddler], drop)) shed += 1;
382
424
  }
383
425
  // (d) The system prompt and tools yield to the reserved history, when they alone can bring the call to the step.
426
+ // With `capPrefix` (Tex's calls, opted in from Tex's own repository; Grok Bot, 2026-11-30: the contract change does
427
+ // nothing until Tex takes it, and Context's own client stays as it was), over `PREFIX_CAP` they are cut once, to the cap, from their ends, and not clipped again in this call: a clip sized
428
+ // to the step changed their bytes at every step a turn crossed, and each such round wrote the whole prompt to the
429
+ // cache again (2026-11-27). Under the cap they are not the overflow, and yield by what the step needs, as before.
430
+ const once = new Set();
384
431
  const need = estimateBodyTokens(frame) - low;
385
- if (need > 0 && bulkTokens(frame) - need >= BULK_FLOOR) shed += clipBulk(frame, need);
432
+ if (withCap && need > 0 && bulkTokens(frame) > PREFIX_CAP) shed += capPrefix(frame, once);
433
+ else if (need > 0 && bulkTokens(frame) - need >= BULK_FLOOR) shed += clipBulk(frame, need);
386
434
  // (e) Then everything as before: the reserved history, oldest first.
387
435
  shed += cut(frame, low, { newest: false, clip: false, keepFrom });
388
436
  const over = () => estimateBodyTokens(frame) + restTokens > HAIKU_INPUT_BUDGET;
@@ -394,13 +442,18 @@ function fitHaikuBody(body, { turnFrom } = {}) {
394
442
  // turn, whichever is longer, down to the step so the same clip holds next round; a tool result goes
395
443
  // only while the call is over the budget. The turn's words are never clipped here: a lookup of a
396
444
  // few thousand characters is no reason to drop it while the system prompt is the overflow.
445
+ // The first time this clip reaches a system block or tool description over `PREFIX_CAP`, they are cut to the cap
446
+ // as in (d) instead of halved.
397
447
  if (over()) {
398
448
  while (estimateBodyTokens(frame) > low) {
399
- const clip = largestText(frame, start);
449
+ const clip = largestText(frame, start, once);
400
450
  const result = over() ? largestResult(frame.messages) : null;
401
451
  if (result && (!clip || result.length >= clip.text.length)) {
402
452
  frame.messages[result.message].content[result.block] = stubResult(frame.messages[result.message].content[result.block]);
403
- } else if (!clip || !clipLargest(frame, start)) break;
453
+ } else if (withCap && clip?.key != null && once.size === 0 && bulkTokens(frame) > PREFIX_CAP) {
454
+ shed += capPrefix(frame, once);
455
+ continue;
456
+ } else if (!clip || !clipLargest(frame, start, once)) break;
404
457
  shed += 1;
405
458
  }
406
459
  }
@@ -413,4 +466,4 @@ function fitHaikuBody(body, { turnFrom } = {}) {
413
466
  return { body: fitted, before, after: estimateBodyTokens(fitted), shed };
414
467
  }
415
468
 
416
- module.exports = { HAIKU_INPUT_BUDGET, TRIM_STEP, HISTORY_RESERVE, LEFT_OUT, EARLIER, estimateBodyTokens, fitHaikuBody };
469
+ module.exports = { HAIKU_INPUT_BUDGET, TRIM_STEP, HISTORY_RESERVE, PREFIX_CAP, LEFT_OUT, EARLIER, estimateBodyTokens, fitHaikuBody };