@supa-media/tex-contract 0.4.0 → 0.9.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 (53) hide show
  1. package/README.md +16 -0
  2. package/package.json +1 -1
  3. package/texContract/aboutNoteLimits.cjs +1 -1
  4. package/texContract/asks.cjs +1 -1
  5. package/texContract/capabilities.cjs +1 -1
  6. package/texContract/computerTool.cjs +1 -1
  7. package/texContract/contextGateway.cjs +1 -1
  8. package/texContract/deskEligible.cjs +1 -1
  9. package/texContract/doorBatch.cjs +27 -0
  10. package/texContract/doorBatch.d.cts +6 -0
  11. package/texContract/eventWords.cjs +2 -2
  12. package/texContract/graphWires.cjs +1 -1
  13. package/texContract/guestNames.cjs +1 -1
  14. package/texContract/heard.cjs +1 -1
  15. package/texContract/identity.cjs +2 -1
  16. package/texContract/judgedActions.cjs +1 -1
  17. package/texContract/labLedger.cjs +37 -0
  18. package/texContract/labLedger.d.cts +5 -0
  19. package/texContract/mailCheck.cjs +1 -1
  20. package/texContract/membersSeal.cjs +202 -0
  21. package/texContract/membersSeal.d.cts +13 -0
  22. package/texContract/noteRunFacts.cjs +1 -1
  23. package/texContract/pageJobs.cjs +3 -3
  24. package/texContract/quietHoursWindow.cjs +1 -1
  25. package/texContract/reactEmoji.cjs +1 -1
  26. package/texContract/settingHabits.cjs +1 -1
  27. package/texContract/settingsNotes.cjs +1 -1
  28. package/texContract/shownEmail.cjs +1 -1
  29. package/texContract/texClients.cjs +2 -2
  30. package/texContract/texGraph.cjs +1 -1
  31. package/texContract/texNumber.cjs +1 -1
  32. package/texContract/texSettings.cjs +1 -1
  33. package/texContract/texStateFiles.cjs +1 -1
  34. package/texContract/texStoreWire.cjs +1 -1
  35. package/texContract/threadsFold.cjs +1 -1
  36. package/texContract/toolCatalog.generated.json +5 -5
  37. package/texContract/transcription.cjs +1 -1
  38. package/texContract/uploadImages.cjs +1 -1
  39. package/texContract/v2Translate.cjs +1 -1
  40. package/texContract/yesWords.cjs +1 -1
  41. package/texShared/githubOidc.ts +3 -3
  42. package/texShared/groupRunQuestions.cjs +2 -2
  43. package/texShared/jobPartEnd.cjs +4 -4
  44. package/texShared/jobResume.cjs +3 -3
  45. package/texShared/jobSplit.cjs +2 -2
  46. package/texShared/jobStart.cjs +2 -2
  47. package/texShared/mail.cjs +7 -1
  48. package/texShared/models/creditRest.cjs +1 -1
  49. package/texShared/models/promptBudget.cjs +132 -11
  50. package/texShared/noteAppLink.cjs +1 -1
  51. package/texShared/pages/blocks.cjs +1 -1
  52. package/texShared/pages/draw.cjs +1 -1
  53. package/texShared/searchConfig.cjs +1 -1
@@ -24,6 +24,16 @@
24
24
  * so a newer message leaves it exactly as it was until the body has grown by
25
25
  * about a step more. See docs/decisions/texting-assistant/week-history.md.
26
26
  *
27
+ * The newest history before the turn has a reserved share (`HISTORY_RESERVE`).
28
+ * Older tool results go first, then history older than the reserve, oldest
29
+ * first. The message across the reserve's edge keeps its newest part. Then the
30
+ * system prompt and the tool list are clipped, keeping their end, when that
31
+ * alone brings the call to its step. Only then does the reserved history go.
32
+ * On 2026-11-30 Tex's front door's system prompt and tools (about 90,000
33
+ * estimated tokens) were over the step-under cut on their own, so every
34
+ * earlier turn was stubbed and the front door saw no history (Tex Quality,
35
+ * merge-gate finding).
36
+ *
27
37
  * Shared by Tex (`aiGateway.js`) and Context's own model client
28
38
  * (`apps/mcp/src/models/client.js`): every Haiku call either side makes is fitted the same way.
29
39
  */
@@ -51,6 +61,21 @@ const TEXT_CHARS_PER_TOKEN = 2.68;
51
61
  */
52
62
  const TRIM_STEP = 16_000;
53
63
 
64
+ /**
65
+ * The newest history before the turn, in estimated tokens, kept whole where it can be. About 43,000
66
+ * characters at `TEXT_CHARS_PER_TOKEN`: it holds Tex's front door's whole chat window on main
67
+ * (`chat:2:16000`, 16,000 characters, about 6,000 tokens) plus about 27,000 characters of its newest
68
+ * day summaries or tool rounds. It is one `TRIM_STEP`, so at a cut a step under the budget the system
69
+ * prompt and tools keep a share of about 64,000 tokens.
70
+ */
71
+ const HISTORY_RESERVE = 16_000;
72
+
73
+ /**
74
+ * The system prompt and tools never yield below this, in estimated tokens, to keep history. Below it,
75
+ * history yields as before.
76
+ */
77
+ const BULK_FLOOR = 8_000;
78
+
54
79
  /** A picture or a PDF page, counted once. The base64 is not tokens. */
55
80
  const IMAGE_TOKENS = 1_600;
56
81
 
@@ -82,12 +107,82 @@ function messageTokens(message) {
82
107
  return 0;
83
108
  }
84
109
 
85
- /** Tokens in one gateway body: system, tools, and messages. */
86
- function estimateBodyTokens(body) {
110
+ /** Tokens in a gateway body's system prompt and tool list (the "bulk"). */
111
+ function bulkTokens(body) {
87
112
  const system = Array.isArray(body?.system) ? body.system.reduce((sum, block) => sum + blockTokens(block), 0) : textTokens(body?.system);
88
113
  const tools = textTokens(JSON.stringify(body?.tools ?? []));
114
+ return system + tools;
115
+ }
116
+
117
+ /** Tokens in one gateway body: system, tools, and messages. */
118
+ function estimateBodyTokens(body) {
89
119
  const messages = (body?.messages ?? []).reduce((sum, message) => sum + messageTokens(message), 0);
90
- return system + tools + messages;
120
+ return bulkTokens(body) + messages;
121
+ }
122
+
123
+ /**
124
+ * The newest messages before `end` that fit in `reserve`, walking back from `end - 1`. `from` is the
125
+ * first wholly reserved index (`end` if none), `used` the reserved tokens. The message at `from - 1`,
126
+ * if there is one, is the straddler: it crosses the reserve's edge.
127
+ */
128
+ function reservedFrom(messages, end, reserve) {
129
+ let from = end;
130
+ let used = 0;
131
+ for (let i = end - 1; i >= 0; i -= 1) {
132
+ const tokens = messageTokens(messages[i]);
133
+ if (used + tokens > reserve) break;
134
+ used += tokens;
135
+ from = i;
136
+ }
137
+ return { from, used };
138
+ }
139
+
140
+ /** The longest plain text of a message (its string content, or its longest `text` block), as { text, set }, or null. */
141
+ function messagePlainText(message) {
142
+ if (typeof message?.content === "string") return { text: message.content, set: (value) => { message.content = value; } };
143
+ if (!Array.isArray(message?.content)) return null;
144
+ let index = -1;
145
+ message.content.forEach((block, i) => {
146
+ if (block?.type === "text" && typeof block.text === "string" && (index < 0 || block.text.length > message.content[index].text.length)) index = i;
147
+ });
148
+ if (index < 0) return null;
149
+ return { text: message.content[index].text, set: (value) => { message.content[index] = { ...message.content[index], text: value }; } };
150
+ }
151
+
152
+ /**
153
+ * Drop about `dropTokens` of a message's longest text from its start, keeping its end. Tool calls,
154
+ * tool results and pictures are never clipped. Mutates `message`. Returns false when there is no text
155
+ * to clip or the result would not be shorter.
156
+ */
157
+ function clipMessageTail(message, dropTokens) {
158
+ const found = messagePlainText(message);
159
+ if (!found || found.text.length <= EARLIER.length + 1) return false;
160
+ const kept = found.text.slice(Math.ceil(dropTokens * TEXT_CHARS_PER_TOKEN));
161
+ const clipped = kept === "" ? EARLIER : `${EARLIER}\n${kept}`;
162
+ if (clipped.length >= found.text.length) return false;
163
+ found.set(clipped);
164
+ return true;
165
+ }
166
+
167
+ /**
168
+ * Clip the system prompt and tool descriptions by about `dropTokens`, longest first, keeping each
169
+ * one's end. Messages are not touched. Returns the number of clips made.
170
+ */
171
+ function clipBulk(body, dropTokens) {
172
+ let remaining = Math.ceil(dropTokens * TEXT_CHARS_PER_TOKEN);
173
+ let clips = 0;
174
+ while (remaining > 0) {
175
+ const best = largestText(body, 0);
176
+ if (!best || best.text.length <= EARLIER.length + 1) break;
177
+ const cutChars = Math.min(remaining, best.text.length - EARLIER.length - 1);
178
+ const tail = best.text.slice(cutChars);
179
+ const clipped = tail === "" ? EARLIER : `${EARLIER}\n${tail}`;
180
+ if (clipped.length >= best.text.length) break;
181
+ remaining -= best.text.length - clipped.length;
182
+ best.set(clipped);
183
+ clips += 1;
184
+ }
185
+ return clips;
91
186
  }
92
187
 
93
188
  function resultText(block) {
@@ -197,15 +292,17 @@ const messageList = (body) => (Array.isArray(body?.messages) ? body.messages : [
197
292
  * Stubs `body` in the fixed order until its estimate is at or under `target`. `older`: tool results
198
293
  * before message `keepFrom`, then history text before it. `newest`: a tool result anywhere, oldest
199
294
  * first. `clip`: the longest text, last of all, outside messages from `clipFrom` on. `keepFrom`
200
- * defaults to the body's last message. Returns how many cuts it made.
295
+ * defaults to the body's last message. `historyEnd` bounds the history stubbed (default `end`); `history: false`
296
+ * skips history altogether. Returns how many cuts it made.
201
297
  */
202
- function cut(body, target, { older = true, newest = true, clip = true, keepFrom, clipFrom = Infinity } = {}) {
298
+ function cut(body, target, { older = true, newest = true, clip = true, keepFrom, clipFrom = Infinity, historyEnd, history: withHistory = true } = {}) {
203
299
  const messages = messageList(body);
204
300
  const end = Math.max(0, Math.min(keepFrom ?? messages.length - 1, messages.length));
301
+ const historyLimit = historyEnd ?? end;
205
302
  let shed = 0;
206
303
  while (estimateBodyTokens(body) > target) {
207
304
  const result = older ? oldestResult(messages, end) : null;
208
- const history = older && !result ? oldestHistory(messages, end) : null;
305
+ const history = older && withHistory && !result ? oldestHistory(messages, historyLimit) : null;
209
306
  const any = newest && !result && !history ? oldestResult(messages, messages.length) : null;
210
307
  const where = result ?? any;
211
308
  if (where) messages[where.message].content[where.block] = stubResult(messages[where.message].content[where.block]);
@@ -232,9 +329,14 @@ const stepOf = (tokens) => Math.max(0, Math.ceil((tokens - HAIKU_INPUT_BUDGET) /
232
329
  * made on the shortest leading run of messages already in that step (the
233
330
  * "frame"), the newest of them standing in for the newest message; the
234
331
  * messages after it go as they came. A newer text or tool round in the same
235
- * step finds the same frame and makes the same cut, byte for byte. Only when
236
- * stubbing whole older units cannot reach the step does the frame's newest
237
- * tool result go, and then a clip, and only on the frame.
332
+ * step finds the same frame and makes the same cut, byte for byte. The newest
333
+ * history before the turn has a reserved share (`HISTORY_RESERVE`): older tool
334
+ * results go first, then history older than the reserve, then the message across
335
+ * the reserve's edge keeps its newest part, then the system prompt and tools are
336
+ * clipped (keeping their end) when that alone brings the call to the step, and
337
+ * only then the reserved history. Only when stubbing whole older units cannot
338
+ * reach the step does the frame's newest tool result go, and then a clip, and
339
+ * only on the frame.
238
340
  */
239
341
  function fitHaikuBody(body, { turnFrom } = {}) {
240
342
  const before = estimateBodyTokens(body);
@@ -263,7 +365,26 @@ function fitHaikuBody(body, { turnFrom } = {}) {
263
365
  // Whole older units, down to a step under: the cut that holds while the body grows. The frame's
264
366
  // last message is kept as if it were the newest, whatever follows it, so a newer message cannot
265
367
  // change this cut.
266
- let shed = cut(frame, low, { newest: false, clip: false, keepFrom });
368
+ // (a) Older tool results first, as before: the model can call them again.
369
+ let shed = cut(frame, low, { newest: false, clip: false, keepFrom, history: false });
370
+ // (b) The newest history before the turn is reserved (HISTORY_RESERVE); older history goes first, oldest first.
371
+ const fm = frame.messages;
372
+ const { from: reserveFrom, used } = reservedFrom(fm, keepFrom, HISTORY_RESERVE);
373
+ const straddler = reserveFrom - 1;
374
+ shed += cut(frame, low, { newest: false, clip: false, keepFrom, historyEnd: Math.max(0, straddler) });
375
+ // (c) The message across the reserve's edge keeps its newest part, up to what is left of the reserve.
376
+ if (straddler >= 0 && estimateBodyTokens(frame) > low) {
377
+ const room = HISTORY_RESERVE - used;
378
+ const excess = messageTokens(fm[straddler]) - room;
379
+ const need = estimateBodyTokens(frame) - low;
380
+ const drop = Math.min(excess, need);
381
+ if (drop > 0 && clipMessageTail(fm[straddler], drop)) shed += 1;
382
+ }
383
+ // (d) The system prompt and tools yield to the reserved history, when they alone can bring the call to the step.
384
+ const need = estimateBodyTokens(frame) - low;
385
+ if (need > 0 && bulkTokens(frame) - need >= BULK_FLOOR) shed += clipBulk(frame, need);
386
+ // (e) Then everything as before: the reserved history, oldest first.
387
+ shed += cut(frame, low, { newest: false, clip: false, keepFrom });
267
388
  const over = () => estimateBodyTokens(frame) + restTokens > HAIKU_INPUT_BUDGET;
268
389
  // Still over, and newer messages follow the frame with its last message before the ask: that
269
390
  // message is older history to them, down to the step.
@@ -292,4 +413,4 @@ function fitHaikuBody(body, { turnFrom } = {}) {
292
413
  return { body: fitted, before, after: estimateBodyTokens(fitted), shed };
293
414
  }
294
415
 
295
- module.exports = { HAIKU_INPUT_BUDGET, TRIM_STEP, LEFT_OUT, EARLIER, estimateBodyTokens, fitHaikuBody };
416
+ module.exports = { HAIKU_INPUT_BUDGET, TRIM_STEP, HISTORY_RESERVE, LEFT_OUT, EARLIER, estimateBodyTokens, fitHaikuBody };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The one builder of a note's app link, `<APP_ORIGIN>/note/@<slug>/<path>`, for
3
3
  * the gateway's MCP answers (`apps/mcp/src/notes/appLink.js`) and Tex's texted
4
- * ones (`packages/tex/gateway/agent/noteLinks.js`): two builders would drift.
4
+ * ones (in Tex's repository): two builders would drift.
5
5
  */
6
6
  /** The deployment's https origin, without a trailing slash, or null. */
7
7
  function appOriginOf(env) {
@@ -14,7 +14,7 @@
14
14
  * An item is `|`-separated parts, and its first part may start with a
15
15
  * `Label: `. What the label and each part mean is the kind's (`about` below,
16
16
  * which is also what Tex is told). These are Tex's card templates
17
- * (`packages/tex/gateway/agent/visuals/`) as page blocks: the same set of things a
17
+ * (in Tex's repository) as page blocks: the same set of things a
18
18
  * person is shown, written as data the page draws, so every word in them stays
19
19
  * a word in the note.
20
20
  *
@@ -81,7 +81,7 @@ const DRAW = {
81
81
  .map((item) => {
82
82
  const name = itemName(item);
83
83
  const on = chosen !== "" && name.toLowerCase() === chosen;
84
- // An option's photo (the gateway hunts one for each, `packages/tex/gateway/agent/visuals/pagePhotos.js`) leads its row.
84
+ // An option's photo (the gateway hunts one for each, Tex's code) leads its row.
85
85
  const pic = photosOf(item)[0];
86
86
  return `<div class="pg-option${on ? " on" : ""}${pic ? " pictured" : ""}" role="radio" aria-checked="${on}" tabindex="0" data-choose="${esc(name)}">${pic ? photo(pic, ctx, { name }) : ""}<div>${item.label ? part(item, -1, open, "b") : ""}${part(item, 0, open, item.label ? "span" : "b")}${part(item, 1, open, "span")}${sourceLink(item)}</div><span class="pg-tick">${on ? "Chosen" : "Choose"}</span></div>`;
87
87
  })
@@ -3,7 +3,7 @@
3
3
  * config, and then search config because not only text users search").
4
4
  *
5
5
  * `searchConfig.json` holds every step a search runs (a node), its settings, and the wires between them, in the
6
- * Lab's graph shape (the one Tex's `texConfig.json` uses). The gateway's search (`config/nodes.js`, run by the shared
6
+ * Lab's graph shape (the one Tex's own config uses). The gateway's search (`config/nodes.js`, run by the shared
7
7
  * graph runner) and the defaults every other caller of the search code ranks by (`../ranking.js`, `../settings.js`)
8
8
  * read it through this file only, so a config that does not match the schema below fails loudly at start instead of
9
9
  * being misread. The Lab draws it on its Search page and a Promote changes it in a pull request.