@theoremjs/agents 0.3.4-next.783.g4851c77 → 0.3.4-next.787.g385fa7e

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.
@@ -94,88 +94,78 @@ function viewReadings(stretches, canary) {
94
94
  }
95
95
  return readings;
96
96
  }
97
- /** A run with a slot, keyed by where the slot is and the words around it. */
98
- function slotKey(run, slot) {
99
- // why: Bare numbers are never words here, so the slot's index leads unambiguously.
100
- return [slot, ...run.filter((_, k) => k !== slot)].join(' ');
101
- }
102
- /** A run may not start on the canary's place unless `leading`: a stand-in for it is not prompt text. */
103
- function addRuns(set, readings, lengths, leading) {
104
- for (const words of readings.plain) {
105
- for (const word of words)
106
- set.words.add(word);
107
- for (const length of lengths) {
108
- for (let from = 0; from + length <= words.length; from++) {
109
- set.plain.add(words.slice(from, from + length).join(' '));
110
- }
97
+ /** Between two readings in `PromptWords`: no run reaches across it. */
98
+ const BREAK = '\u0001';
99
+ function readPrompt(stretches, canary) {
100
+ const readings = promptReadings(stretches, canary);
101
+ const read = {
102
+ plain: readings.plain.flatMap((words) => [...words, BREAK]),
103
+ places: new Map(),
104
+ slotted: readings.slotted.flatMap((words) => [...words, BREAK]),
105
+ slots: [],
106
+ prefixes: new Set(),
107
+ longest: 0,
108
+ };
109
+ for (const [at, word] of read.plain.entries()) {
110
+ if (word === BREAK)
111
+ continue;
112
+ const places = read.places.get(word);
113
+ if (places) {
114
+ places.push(at);
115
+ continue;
111
116
  }
112
- }
113
- for (const slotted of readings.slotted) {
114
- for (const length of lengths) {
115
- for (let from = 0; from + length <= slotted.length; from++) {
116
- const run = slotted.slice(from, from + length);
117
- const slot = run.indexOf(SLOT);
118
- if (slot === -1 || (slot === 0 && !leading))
119
- continue;
120
- set.slotted.add(slotKey(run, slot));
121
- set.slots.add(slot);
122
- }
117
+ read.places.set(word, [at]);
118
+ read.longest = Math.max(read.longest, word.length);
119
+ for (let end = 1; end <= word.length && ASCII.test(word.charAt(end - 1)); end++) {
120
+ read.prefixes.add(word.slice(0, end));
123
121
  }
124
122
  }
125
- }
126
- /** Where in `run` the canary's place fell when `set` has it: -1 for nowhere, undefined when not a run. */
127
- function matchRun(set, run) {
128
- if (set.plain.has(run.join(' ')))
129
- return -1;
130
- for (const slot of set.slots) {
131
- if (slot < run.length && set.slotted.has(slotKey(run, slot)))
132
- return slot;
123
+ for (const [at, word] of read.slotted.entries()) {
124
+ if (word === SLOT)
125
+ read.slots.push(at);
133
126
  }
134
- return undefined;
135
- }
136
- function sameStretches(a, b) {
137
- return a === b || (a.length === b.length && a.every((stretch, k) => stretch === b[k]));
127
+ return read;
138
128
  }
139
- let gramsCache;
140
- /** Every run of `PROMPT_ECHO_WORDS` words in a private stretch. */
141
- function promptGrams(stretches, canary) {
142
- if (gramsCache &&
143
- gramsCache.canary === canary &&
144
- sameStretches(gramsCache.stretches, stretches)) {
145
- return gramsCache.grams;
129
+ /** Whether a reading has `run` word for word, looked for from its word the prompt has least. */
130
+ function hasRun(read, run) {
131
+ let fewest;
132
+ let anchor = 0;
133
+ for (const [k, word] of run.entries()) {
134
+ const places = read.places.get(word);
135
+ if (!places)
136
+ return false;
137
+ if (!fewest || places.length < fewest.length) {
138
+ fewest = places;
139
+ anchor = k;
140
+ }
146
141
  }
147
- const grams = {
148
- plain: new Set(),
149
- slotted: new Set(),
150
- slots: new Set(),
151
- words: new Set(),
152
- };
153
- addRuns(grams, promptReadings(stretches, canary), [PROMPT_ECHO_WORDS], true);
154
- gramsCache = { stretches, canary, grams };
155
- return grams;
142
+ return (fewest ?? []).some((at) => run.every((word, k) => read.plain[at - anchor + k] === word));
156
143
  }
157
- let shapeCache;
158
- function promptShape(stretches, canary) {
159
- if (shapeCache &&
160
- shapeCache.canary === canary &&
161
- sameStretches(shapeCache.stretches, stretches)) {
162
- return shapeCache.shape;
163
- }
164
- const readings = promptReadings(stretches, canary);
165
- const shape = {
166
- runs: { plain: new Set(), slotted: new Set(), slots: new Set(), words: new Set() },
167
- prefixes: new Set(),
168
- longest: 0,
169
- };
170
- for (const word of readings.plain.flat()) {
171
- shape.longest = Math.max(shape.longest, word.length);
172
- for (let end = 1; end <= word.length && ASCII.test(word.charAt(end - 1)); end++) {
173
- shape.prefixes.add(word.slice(0, end));
144
+ /**
145
+ * Where in `run` the canary's place fell when the prompt has it: -1 for
146
+ * nowhere, undefined when not a run. A run may not start on the canary's place
147
+ * unless `leading`: a stand-in for it is not prompt text.
148
+ */
149
+ function matchRun(read, run, leading) {
150
+ if (hasRun(read, run))
151
+ return -1;
152
+ for (const at of read.slots) {
153
+ for (let slot = run.length - 1; slot >= (leading ? 0 : 1); slot--) {
154
+ const from = at - slot;
155
+ if (run.every((word, k) => k === slot || read.slotted[from + k] === word))
156
+ return slot;
174
157
  }
175
158
  }
176
- addRuns(shape.runs, readings, Array.from({ length: PROMPT_ECHO_WORDS - 1 }, (_, k) => k + 1), false);
177
- shapeCache = { stretches, canary, shape };
178
- return shape;
159
+ return undefined;
160
+ }
161
+ const readings = new WeakMap();
162
+ /** `stretches` read once for as long as they live: each turn holds its own, so overlapping turns never rebuild each other's. */
163
+ function promptWords(stretches, canary) {
164
+ const byCanary = readings.get(stretches) ?? new Map();
165
+ readings.set(stretches, byCanary);
166
+ const read = byCanary.get(canary) ?? readPrompt(stretches, canary);
167
+ byCanary.set(canary, read);
168
+ return read;
179
169
  }
180
170
  /**
181
171
  * Whether a word still being written could end as a word of the system
@@ -183,11 +173,11 @@ function promptShape(stretches, canary) {
183
173
  * scripts can fold several letters into one (Hangul jamo, up to three), so
184
174
  * only a word too long to fold down to any of them is ruled out.
185
175
  */
186
- function mayBecomePromptWord(partial, shape) {
176
+ function mayBecomePromptWord(partial, read) {
187
177
  if (ASCII.test(partial)) {
188
- return shape.prefixes.has(partial.toLowerCase());
178
+ return read.prefixes.has(partial.toLowerCase());
189
179
  }
190
- return partial.length <= MAX_FOLD * shape.longest;
180
+ return partial.length <= MAX_FOLD * read.longest;
191
181
  }
192
182
  /**
193
183
  * Where an echo still being written could start: the first word of the
@@ -215,11 +205,11 @@ function holdIn(text, tail, stretches, canary) {
215
205
  while (writing > 0 && WORD_CHAR.test(text.charAt(writing - 1)))
216
206
  writing--;
217
207
  const complete = words.filter((entry) => tail + entry.to < text.length || writing === text.length);
218
- const shape = promptShape(stretches, canary);
219
- const hold = mayBecomePromptWord(text.slice(writing), shape) ? writing : text.length;
208
+ const read = promptWords(stretches, canary);
209
+ const hold = mayBecomePromptWord(text.slice(writing), read) ? writing : text.length;
220
210
  for (let from = Math.max(0, complete.length - (PROMPT_ECHO_WORDS - 1)); from < complete.length; from++) {
221
211
  const run = complete.slice(from).map((entry) => entry.word);
222
- if (matchRun(shape.runs, run) !== undefined) {
212
+ if (matchRun(read, run, false) !== undefined) {
223
213
  return Math.min(hold, tail + complete[from].at);
224
214
  }
225
215
  }
@@ -258,15 +248,12 @@ function promptEchoRanges(text, stretches, canary) {
258
248
  .sort((a, b) => a[0] - b[0]);
259
249
  }
260
250
  function echoRanges(text, stretches, canary) {
261
- const grams = promptGrams(stretches, canary);
262
- if (!text || grams.plain.size === 0) {
263
- return [];
264
- }
251
+ const read = promptWords(stretches, canary);
265
252
  const words = echoWords(text);
266
253
  const ranges = [];
267
254
  // 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));
255
+ const strange = read.slots.length > 0 ? 1 : 0;
256
+ const foreign = words.map((entry) => (read.places.has(entry.word) ? 0 : 1));
270
257
  let outside = foreign.slice(0, PROMPT_ECHO_WORDS - 1).reduce((sum, n) => sum + n, 0);
271
258
  for (let from = 0; from + PROMPT_ECHO_WORDS <= words.length; from++) {
272
259
  outside += foreign[from + PROMPT_ECHO_WORDS - 1];
@@ -275,7 +262,7 @@ function echoRanges(text, stretches, canary) {
275
262
  if (outside > strange)
276
263
  continue;
277
264
  const run = words.slice(from, from + PROMPT_ECHO_WORDS).map((entry) => entry.word);
278
- const slot = matchRun(grams, run);
265
+ const slot = matchRun(read, run, true);
279
266
  if (slot === undefined)
280
267
  continue;
281
268
  // why: A stand-in for the canary at either end is the model's, not the prompt's.
@@ -176,9 +176,9 @@ async function applyOutbound(gate, events, turnPhase, onWithhold) {
176
176
  const out = batch.action === 'emit' ? batch.events : [];
177
177
  if (turnPhase !== 'complete')
178
178
  return out;
179
- // why: The response is whole only once its verdict is in: `turn_complete` follows what the
180
- // verdict released, and a withheld response ends on its error.
181
- const said = out.filter((event) => !isTurnComplete(event));
179
+ // why: The response is whole only once its verdict is in: `turn_complete` and `done` follow what
180
+ // the verdict released, and a withheld response ends on its error.
181
+ const said = out.filter((event) => !isTurnComplete(event) && event.type !== 'done');
182
182
  const finalized = await finalizeLiveOutboundTurn(gate);
183
183
  if (finalized.action === 'withhold') {
184
184
  onWithhold();
@@ -192,7 +192,7 @@ async function applyOutbound(gate, events, turnPhase, onWithhold) {
192
192
  ...said,
193
193
  ...(finalized.action === 'emit' ? finalized.events : []),
194
194
  ...out.filter(isTurnComplete),
195
- { type: 'done', stop: { kind: 'completed' } },
195
+ ...out.filter((event) => event.type === 'done'),
196
196
  ];
197
197
  }
198
198
  function isTurnComplete(event) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.783.g4851c77",
3
+ "version": "0.3.4-next.787.g385fa7e",
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",