@theoremjs/agents 0.3.4-next.550.gf20b9cd → 0.3.4-next.556.g19d4fbb

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.
@@ -102,6 +102,8 @@ function slotKey(run, slot) {
102
102
  /** A run may not start on the canary's place unless `leading`: a stand-in for it is not prompt text. */
103
103
  function addRuns(set, readings, lengths, leading) {
104
104
  for (const words of readings.plain) {
105
+ for (const word of words)
106
+ set.words.add(word);
105
107
  for (const length of lengths) {
106
108
  for (let from = 0; from + length <= words.length; from++) {
107
109
  set.plain.add(words.slice(from, from + length).join(' '));
@@ -142,7 +144,12 @@ function promptGrams(stretches, canary) {
142
144
  sameStretches(gramsCache.stretches, stretches)) {
143
145
  return gramsCache.grams;
144
146
  }
145
- const grams = { plain: new Set(), slotted: new Set(), slots: new Set() };
147
+ const grams = {
148
+ plain: new Set(),
149
+ slotted: new Set(),
150
+ slots: new Set(),
151
+ words: new Set(),
152
+ };
146
153
  addRuns(grams, promptReadings(stretches, canary), [PROMPT_ECHO_WORDS], true);
147
154
  gramsCache = { stretches, canary, grams };
148
155
  return grams;
@@ -156,7 +163,7 @@ function promptShape(stretches, canary) {
156
163
  }
157
164
  const readings = promptReadings(stretches, canary);
158
165
  const shape = {
159
- runs: { plain: new Set(), slotted: new Set(), slots: new Set() },
166
+ runs: { plain: new Set(), slotted: new Set(), slots: new Set(), words: new Set() },
160
167
  prefixes: new Set(),
161
168
  longest: 0,
162
169
  };
@@ -257,7 +264,16 @@ function echoRanges(text, stretches, canary) {
257
264
  }
258
265
  const words = echoWords(text);
259
266
  const ranges = [];
267
+ // why: A run holds no word the prompt lacks but the canary's stand-in, so a window with more is skipped unread.
268
+ const strange = grams.slots.size > 0 ? 1 : 0;
269
+ const foreign = words.map((entry) => (grams.words.has(entry.word) ? 0 : 1));
270
+ let outside = foreign.slice(0, PROMPT_ECHO_WORDS - 1).reduce((sum, n) => sum + n, 0);
260
271
  for (let from = 0; from + PROMPT_ECHO_WORDS <= words.length; from++) {
272
+ outside += foreign[from + PROMPT_ECHO_WORDS - 1];
273
+ if (from > 0)
274
+ outside -= foreign[from - 1];
275
+ if (outside > strange)
276
+ continue;
261
277
  const run = words.slice(from, from + PROMPT_ECHO_WORDS).map((entry) => entry.word);
262
278
  const slot = matchRun(grams, run);
263
279
  if (slot === undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.550.gf20b9cd",
3
+ "version": "0.3.4-next.556.g19d4fbb",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",