@cotal-ai/connector-core 0.78.1 → 0.81.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"agui-emitter.d.ts","sourceRoot":"","sources":["../src/agui-emitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,EAA4C,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAEL,mBAAmB,EASnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,SAAS,EAEf,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,SAAS,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACrD,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;IACnC,4FAA4F;IAC5F,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,oGAAoG;IACpG,0BAA0B,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,WAAW,CAAC,CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,IAAI,EAAE,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,sBAAsB,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC;IACvG,kBAAkB,CAAC,CAAC,EAAE;QACpB,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,IAAI,EAAE,CAAC;QACd,EAAE,EAAE,MAAM,CAAC;QACX,sBAAsB,EAAE,MAAM,CAAC;KAChC,G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KAAK,CAAC,EAAE;YAAE,OAAO,EAAE,MAAM,CAAC;YAAC,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;KAC5C,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IA4D1B;;;;;;;;;;;;;;;OAeG;IACH,aAAa,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI;IAQ5C,2FAA2F;IAC3F,IAAI,gBAAgB,IAAI,MAAM,CAE7B;IAED,mFAAmF;IACnF,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAMrD;;;;;OAKG;IACH,SAAS,IAAI;QAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;KAAE;IAInF;oGACgG;IAChG,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI;IAKnD;;;;;;;;;;OAUG;IACH,OAAO,CAAC,gBAAgB;IAgBxB,qGAAqG;IACrG,OAAO,CAAC,OAAO;IASf;uGACmG;YACrF,OAAO;IA2BrB;;;;;;;;;;;;;;;;OAgBG;YACW,OAAO;IA0HrB;;;;;;;;;;;;;;;;;;OAkBG;IACH,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,IAAI;CAIb"}
@@ -13,7 +13,7 @@
13
13
  import { randomUUID } from "node:crypto";
14
14
  import { dirname } from "node:path";
15
15
  import { isAguiFramePart, isCasLoss, principalKey } from "@cotal-ai/core";
16
- import { AguiBrackets, AguiVocabularyError, aguiFrame, applyAguiEgressPolicy, extraPropertyPath, frozenBodyEgressVerdict, RUN_ERROR_EGRESS_MESSAGE, runError, runFinished, takeCodePoints, } from "./agui.js";
16
+ import { AguiBrackets, AguiVocabularyError, aguiFrame, applyAguiEgressPolicy, extraPropertyPath, frozenBodyEgressVerdict, RUN_ERROR_EGRESS_MESSAGE, AGUI_EVENT_TYPE, runError, runFinished, } from "./agui.js";
17
17
  import { eventChannelForSession } from "./launch.js";
18
18
  /**
19
19
  * A bracket violation that is OURS, not the writer's: the machine that tracks open runs and messages
@@ -93,21 +93,24 @@ export function packUnits(opts) {
93
93
  const { threadId, epoch, measure, limit } = opts;
94
94
  if (!Number.isSafeInteger(limit) || limit <= 0)
95
95
  throw new AguiVocabularyError(`pack limit must be a positive safe integer, got ${JSON.stringify(limit)}`);
96
+ // `unitFrom` is the index of the first unit a frame carries, so a caller that must re-pack the
97
+ // unpublished tail of a batch can say which units that tail is.
96
98
  const out = [];
97
99
  let seq = opts.firstSeq;
98
100
  let batch = [];
99
101
  let batchRun;
100
102
  let batchCursor;
103
+ let batchFrom = 0;
101
104
  const flush = () => {
102
105
  if (batch.length === 0)
103
106
  return;
104
- out.push({ frame: aguiFrame({ threadId, runId: batchRun, epoch, seq, events: batch }), cursor: batchCursor });
107
+ out.push({ frame: aguiFrame({ threadId, runId: batchRun, epoch, seq, events: batch }), cursor: batchCursor, unitFrom: batchFrom });
105
108
  seq += 1;
106
109
  batch = [];
107
110
  batchRun = undefined;
108
111
  batchCursor = undefined;
109
112
  };
110
- for (const unit of opts.units) {
113
+ for (const [index, unit] of opts.units.entries()) {
111
114
  if (unit.events.length === 0)
112
115
  throw new AguiVocabularyError("packUnits was handed an empty unit; a record that maps to nothing advances the cursor and never becomes a frame");
113
116
  // A frame names ONE run. A unit from a different run cannot join the open batch even if it
@@ -117,6 +120,8 @@ export function packUnits(opts) {
117
120
  const candidate = [...batch, ...unit.events];
118
121
  const fits = measure(aguiFrame({ threadId, runId: batchRun ?? unit.runId, epoch, seq, events: candidate })) <= limit;
119
122
  if (fits) {
123
+ if (batch.length === 0)
124
+ batchFrom = index;
120
125
  batch = candidate;
121
126
  batchRun = batchRun ?? unit.runId;
122
127
  batchCursor = unit.cursor;
@@ -137,70 +142,11 @@ export function packUnits(opts) {
137
142
  batch = [...unit.events];
138
143
  batchRun = unit.runId;
139
144
  batchCursor = unit.cursor;
145
+ batchFrom = index;
140
146
  }
141
147
  flush();
142
148
  return out;
143
149
  }
144
- /**
145
- * The notice a bounded `RUN_ERROR` carries so a reader cannot mistake a shortened or omitted
146
- * upstream detail for the original. The close path is the one place this string is composed.
147
- */
148
- const RUN_ERROR_DETAIL_BOUND_NOTICE = "original detail omitted or shortened because it exceeded the frame bound";
149
- /**
150
- * Rebuild a `RUN_ERROR` so the closing frame fits the live payload ceiling.
151
- *
152
- * **This exists because the close is a terminal a reader waits on, and the message is untrusted
153
- * upstream free text.** `packUnits` is right to refuse an oversized source observation — a unit
154
- * that does not fit has no honest cursor. A close unit is not a source observation: we author it,
155
- * it consumes no record, and refusing it leaves the run with no terminal, no pending WAL recovery,
156
- * and a dead holder. So the close rebuilds the one event until the SAME `measure` `packUnits` will
157
- * use says it fits, and says in the message that the original detail was omitted or shortened. A
158
- * short message that already fits is returned unchanged. Since #1431 the close passes only
159
- * {@link RUN_ERROR_EGRESS_MESSAGE}, which is shorter than the notice, so this either returns it
160
- * unchanged or throws.
161
- *
162
- * It does not live in `packUnits` and it does not call `splitFrames`. Those are a different
163
- * contract (source-record packing, and the preview plane). Putting the bound on this close is the
164
- * one place every connector already goes through.
165
- */
166
- function boundRunErrorForFrame(opts) {
167
- const build = (message) => runError({
168
- message,
169
- timestamp: opts.timestamp,
170
- ...(opts.code ? { code: opts.code } : {}),
171
- ...(opts.cotal ? { cotal: opts.cotal } : {}),
172
- });
173
- const frameOf = (event) => aguiFrame({
174
- threadId: opts.threadId,
175
- runId: opts.runId,
176
- epoch: opts.epoch,
177
- seq: opts.seq,
178
- events: [event],
179
- });
180
- const original = build(opts.message);
181
- if (opts.measure(frameOf(original)) <= opts.limit)
182
- return original;
183
- const labelled = (kept) => kept.length === 0 ? RUN_ERROR_DETAIL_BOUND_NOTICE : `${RUN_ERROR_DETAIL_BOUND_NOTICE}: ${kept}`;
184
- const at = (codePoints) => build(labelled(takeCodePoints(opts.message, codePoints)));
185
- const emptied = at(0);
186
- if (opts.measure(frameOf(emptied)) > opts.limit)
187
- throw new AguiVocabularyError(`a RUN_ERROR close does not fit in ${opts.limit} bytes even with the failure detail emptied ` +
188
- `and replaced by a bound notice — the envelope, the code and the headers alone are ` +
189
- `${opts.measure(frameOf(emptied))} bytes. No bound on the detail can help, so this fails ` +
190
- `loudly rather than leaving the run without a terminal.`);
191
- // Binary-search the largest well-formed prefix that still fits, re-measuring the whole frame
192
- // each step: JSON escaping and UTF-8 length are nonlinear, so cutting by the overage is wrong.
193
- let lo = 0;
194
- let hi = Array.from(opts.message).length + 1;
195
- while (hi - lo > 1) {
196
- const mid = Math.floor((lo + hi) / 2);
197
- if (opts.measure(frameOf(at(mid))) <= opts.limit)
198
- lo = mid;
199
- else
200
- hi = mid;
201
- }
202
- return at(lo);
203
- }
204
150
  /**
205
151
  * The event emitter: one per principal, one thread at a time.
206
152
  *
@@ -231,6 +177,7 @@ export class AguiEmitter {
231
177
  map;
232
178
  channel;
233
179
  threadId;
180
+ onUnreadable;
234
181
  /**
235
182
  * The bracket machine AT THE FOLDED POSITION — deliberately not "wherever validation got to".
236
183
  *
@@ -243,21 +190,39 @@ export class AguiEmitter {
243
190
  /** True once THIS process has fed an event through the bracket machine. It is the half of the
244
191
  * restart diagnosis that keeps a genuine mid-stream violation from being blamed on a restart. */
245
192
  fedAnyEvent = false;
193
+ /** True once a pump has read the source: the last turn can be seeded only before that. */
194
+ pumped = false;
195
+ /** Terminals owed to runs, by run: a terminal placed on a run the wire has open or a pump is
196
+ * about to open, published in place of the finish the source synthesizes for that run, or taken
197
+ * back by {@link takeOwed} for a close the holder makes. Settled at the freeze of the frame that
198
+ * closes the run. */
199
+ owed = new Map();
200
+ /** Terminals reported at a position no pump has read up to yet, in the order they arrived. */
201
+ queued = [];
202
+ /**
203
+ * The turn the frozen frames most recently began: the run it opened, `null` for one that opened
204
+ * none, `undefined` while the log shows no turn. A terminal reported before any record a pump
205
+ * reads is this turn's. Starts as the run the log has open, which is the last turn the log can
206
+ * name; a holder that drops an emitter seeds the next one with what that emitter knew.
207
+ */
208
+ lastTurn;
246
209
  constructor(ep, wal, source, map,
247
210
  /** Derived from the endpoint's OWN principal, never from a config name or the launch env. */
248
- channel, threadId) {
211
+ channel, threadId, onUnreadable) {
249
212
  this.ep = ep;
250
213
  this.wal = wal;
251
214
  this.source = source;
252
215
  this.map = map;
253
216
  this.channel = channel;
254
217
  this.threadId = threadId;
218
+ this.onUnreadable = onUnreadable;
255
219
  // RESTORED FROM THE WAL, which is the whole point of the v2 migration: a process that died
256
220
  // mid-run comes back knowing which runs and messages are open, instead of refusing the first
257
221
  // event it re-reads. `null` means the document cannot say (migrated from v1) — an empty machine
258
222
  // is the honest starting point there, and `diagnoseBracket` is what keeps the resulting refusal
259
223
  // from being blamed on the writer.
260
224
  this.brackets = wal.brackets ? AguiBrackets.restore(wal.brackets) : new AguiBrackets();
225
+ this.lastTurn = this.brackets.runId;
261
226
  }
262
227
  /**
263
228
  * Start an emitter: resolve the channel, run the single-replica preflight, and settle any pending
@@ -294,8 +259,11 @@ export class AguiEmitter {
294
259
  // recovery can republish a frozen frame, and a WAL whose expectation still came from its own
295
260
  // thread would republish against the wrong tip.
296
261
  await wal.bindSubjectFrontier(opts.subjectFrontier);
297
- const em = new AguiEmitter(endpoint, wal, opts.source, opts.map, channel, wal.threadId);
262
+ const em = new AguiEmitter(endpoint, wal, opts.source, opts.map, channel, wal.threadId, opts.onUnreadable);
298
263
  await em.recover();
264
+ // The run the wire has open once the pending frame is settled, which is the last turn the log
265
+ // can name: a terminal reported before any record the first pump reads is that run's.
266
+ em.lastTurn = em.brackets.runId;
299
267
  return em;
300
268
  }
301
269
  /** True once the emitter has stopped for good. */
@@ -338,30 +306,88 @@ export class AguiEmitter {
338
306
  throw this.halted;
339
307
  if (this.wal.pending)
340
308
  throw new Error(`event emitter for ${this.channel}: a frame is still pending; recovery must settle it before a new read`);
309
+ this.pumped = true;
341
310
  const read = await this.source.read(this.wal.frontier.sourceCursor);
311
+ // A gap is a line the source stepped over; the cursor it returned is past it. It is handed to
312
+ // the connector's reporter BEFORE anything below advances the frontier, so a connector that gave
313
+ // this emitter nowhere to report keeps the old answer: the pump refuses, the cursor stays, and
314
+ // the gap is named in the refusal. Silence is the one answer not on offer.
315
+ for (const gap of read.unreadable ?? []) {
316
+ if (!this.onUnreadable)
317
+ throw new Error(`event emitter for ${this.channel}: the source skipped an unreadable record at offset ${gap.offset} ` +
318
+ `(${gap.bytes} bytes): ${gap.reason}; no gap reporter was given, so refusing to advance over it silently`);
319
+ this.onUnreadable(gap);
320
+ }
321
+ // Every record is mapped before any is packed, so a terminal reported among them can be placed
322
+ // on the turn the source had begun by then (see {@link queueTerminal}) before the record that
323
+ // synthesizes that turn's finish is turned into a unit.
324
+ const mapped = read.records.map((rec) => this.map(rec.value));
325
+ const begun = new Map();
326
+ mapped.forEach((m, i) => {
327
+ if (m === null)
328
+ return;
329
+ if ("turn" in m)
330
+ begun.set(i, null);
331
+ else
332
+ for (const e of m.events)
333
+ if (e.type === AGUI_EVENT_TYPE.RUN_STARTED)
334
+ begun.set(i, e.runId);
335
+ });
336
+ await this.place(read, begun);
342
337
  // Units the mapper dropped are not errors and not frames. Their cursor folds FORWARD into the
343
338
  // preceding unit, so consuming that frame consumes them too and they are never re-read. A drop
344
339
  // stays apart from a mapper error, which reaches the caller with the cursor unmoved.
345
340
  const units = [];
346
- for (const rec of read.records) {
347
- const mapped = this.map(rec.value);
348
- if (mapped === null || mapped.events.length === 0) {
349
- const last = units[units.length - 1];
350
- if (last)
351
- last.cursor = rec.cursor;
341
+ // Turns begun by records since the last unit, carried on the next unit so they are committed
342
+ // when its frame is frozen and never before: a turn a batch that failed had begun is read
343
+ // again by the next pump. A dropped record folds its cursor onto the unit before it, and the
344
+ // turns begun since fold onto that unit with it: they are committed by the freeze that
345
+ // consumes the record, as its cursor is.
346
+ let turns = [];
347
+ const fold = (rec) => {
348
+ const last = units[units.length - 1];
349
+ if (!last)
350
+ return;
351
+ last.cursor = rec.cursor;
352
+ if (turns.length > 0) {
353
+ (last.turns ??= []).push(...turns);
354
+ turns = [];
355
+ }
356
+ };
357
+ for (const [i, rec] of read.records.entries()) {
358
+ const m = mapped[i];
359
+ if (m !== null && "turn" in m) {
360
+ turns.push(null);
361
+ fold(rec);
362
+ continue;
363
+ }
364
+ if (m === null || m.events.length === 0) {
365
+ fold(rec);
352
366
  continue;
353
367
  }
368
+ // An owed terminal becomes a unit OF ITS OWN RUN, ahead of this record's. `RUN_ERROR` names no
369
+ // run, so the frame's envelope is its only attribution, and a frame names one run: left
370
+ // inside this record's unit it would be published under the NEXT run (Sol, round 6, through
371
+ // the built MCP entry point). Its cursor is the position BEFORE this record: the owed frame
372
+ // can be acknowledged while this record's frame is not, and a re-read must then map this
373
+ // record again, which the mapper rebuilt from the log does without the finish, the run being
374
+ // closed by then.
375
+ const owedUnit = this.takeOwedTerminal(m.events, units[units.length - 1]?.cursor ?? this.wal.frontier.sourceCursor);
376
+ if (owedUnit)
377
+ units.push(owedUnit.unit);
354
378
  // WRITE-PATH POLICY, before packing and therefore before beginSend. Disk and wire then agree.
355
379
  // A record that mapped only to forbidden kinds becomes a drop: its cursor folds forward like
356
380
  // any other drop, and packUnits never sees an empty unit.
357
- const events = applyAguiEgressPolicy(mapped.events);
381
+ const events = applyAguiEgressPolicy(owedUnit ? owedUnit.rest : m.events);
382
+ for (const e of m.events)
383
+ if (e.type === AGUI_EVENT_TYPE.RUN_STARTED)
384
+ turns.push(e.runId);
358
385
  if (events.length === 0) {
359
- const last = units[units.length - 1];
360
- if (last)
361
- last.cursor = rec.cursor;
386
+ fold(rec);
362
387
  continue;
363
388
  }
364
- units.push({ runId: mapped.runId, events, cursor: rec.cursor });
389
+ units.push({ runId: m.runId, events, cursor: rec.cursor, ...(turns.length > 0 ? { turns } : {}) });
390
+ turns = [];
365
391
  }
366
392
  // A bounded range that mapped to nothing advances the cursor atomically and ALONE.
367
393
  //
@@ -378,6 +404,7 @@ export class AguiEmitter {
378
404
  if (units.length === 0) {
379
405
  if (read.cursor !== this.wal.frontier.sourceCursor)
380
406
  await this.wal.advanceCursorOnly(read.cursor);
407
+ this.committed([], turns);
381
408
  return { frames: 0, events: 0 };
382
409
  }
383
410
  // Validate the WHOLE batch before publishing any of it. A vocabulary violation discovered
@@ -409,16 +436,123 @@ export class AguiEmitter {
409
436
  limit: this.ep.maxPayload,
410
437
  });
411
438
  let events = 0;
412
- for (const { frame, cursor } of frames) {
413
- await this.publish(frame, cursor);
439
+ for (let k = 0; k < frames.length; k++) {
440
+ const { frame, cursor, unitFrom } = frames[k];
441
+ const to = k + 1 < frames.length ? frames[k + 1].unitFrom : units.length;
442
+ await this.publish(frame, cursor, () => this.committed(units.slice(unitFrom, to), []));
414
443
  events += frame.events.length;
415
444
  }
416
- // Records the mapper dropped AFTER the last unit have no frame to ride on, so their cursor is
417
- // advanced on its own: the same cursor-only rule, applied to the tail of the batch.
445
+ // Records the mapper dropped AFTER the last unit rode on its frame: their cursor and the turns
446
+ // they began were committed with it, so nothing is left to advance or commit here.
418
447
  if (read.cursor !== this.wal.frontier.sourceCursor)
419
448
  await this.wal.advanceCursorOnly(read.cursor);
449
+ this.committed([], turns);
420
450
  return { frames: frames.length, events };
421
451
  }
452
+ /**
453
+ * Place every queued terminal the read has reached on the turn the source had begun when the
454
+ * terminal was reported: the last of the turns begun by the records before its position, or
455
+ * {@link lastTurn} when none of them is. A terminal reported past this read stays queued for the
456
+ * next one. Placed on a run, the terminal becomes that run's debt; on a turn that opened no run,
457
+ * it is consumed; on a run that already has a terminal, or one the wire has closed, it is
458
+ * dropped as the connector repeating itself.
459
+ */
460
+ async place(read, begun) {
461
+ for (const entry of [...this.queued]) {
462
+ const before = await this.boundary(entry, read);
463
+ if (before === undefined)
464
+ continue;
465
+ this.queued = this.queued.filter((q) => q !== entry);
466
+ let turn = this.lastTurn;
467
+ for (const [i, t] of begun)
468
+ if (i < before)
469
+ turn = t;
470
+ if (turn === undefined || turn === null || this.owed.has(turn))
471
+ continue;
472
+ if (turn !== this.brackets.runId && ![...begun.values()].includes(turn))
473
+ continue;
474
+ this.owed.set(turn, entry);
475
+ }
476
+ }
477
+ /**
478
+ * How many of the read's records the source held when the terminal was reported, or `undefined`
479
+ * when the terminal was reported past the read. The position is compared through the source
480
+ * itself, never by reading the cursor: the first record after the position is the one a read
481
+ * from it answers, and the cursor it carries is the same string this read gave that record. A
482
+ * read from the position that answers no record and ends where this read ends covers only lines
483
+ * the source could not read, so the terminal is after every record this read holds. A
484
+ * position whose read could not be taken, or that this emitter had already read past (a terminal
485
+ * is reported before anything after its position is read, so that position is never behind a
486
+ * read), drops the terminal and fails this pump, once: the plane reports it and the next pump
487
+ * goes on without it. Waiting for a pump that reads up to such a position would wait forever.
488
+ */
489
+ async boundary(entry, read) {
490
+ let at;
491
+ try {
492
+ at = await entry.at;
493
+ }
494
+ catch (e) {
495
+ this.queued = this.queued.filter((q) => q !== entry);
496
+ throw new Error(`event emitter for ${this.channel}: dropping the turn terminal reported at ${entry.terminal.timestamp}: ` +
497
+ `the source position it was reported at could not be read (${e.message})`);
498
+ }
499
+ if (at === read.cursor)
500
+ return read.records.length;
501
+ let ahead;
502
+ try {
503
+ ahead = await this.source.read(at);
504
+ }
505
+ catch (e) {
506
+ this.queued = this.queued.filter((q) => q !== entry);
507
+ throw new Error(`event emitter for ${this.channel}: dropping the turn terminal reported at ${entry.terminal.timestamp}: ` +
508
+ `the source could not be read from the position it was reported at (${e.message})`);
509
+ }
510
+ const first = ahead.records[0]?.cursor;
511
+ // No record after the position: every record this read holds is before it. What lies between
512
+ // this read's end and the position is either lines the source stepped over, when the read
513
+ // from the position ends where this read ends, or a record no read answers yet: a part still
514
+ // filling when the turn died, which the source's end counts and its reads stop before. Either
515
+ // way a read from this read's end answers nothing, and the terminal is after every record this
516
+ // read holds; held for a record that never comes, it would wait forever, and a settle that
517
+ // pumps for it would read the source without end (Sol, round 15 of 2026-10-13: 934 reads of
518
+ // an OpenCode session in 500 ms). A read that does answer is before the position, so the
519
+ // terminal waits for the pump that consumes it.
520
+ if (first === undefined) {
521
+ if (ahead.cursor === read.cursor)
522
+ return read.records.length;
523
+ return (await this.source.read(read.cursor)).records.length === 0 ? read.records.length : undefined;
524
+ }
525
+ const i = read.records.findIndex((r) => r.cursor === first);
526
+ if (i >= 0)
527
+ return i;
528
+ if ((await this.source.read(read.cursor)).records.some((r) => r.cursor === first))
529
+ return undefined;
530
+ this.queued = this.queued.filter((q) => q !== entry);
531
+ throw new Error(`event emitter for ${this.channel}: dropping the turn terminal reported at ${entry.terminal.timestamp}: ` +
532
+ `it was reported at a position this emitter had already read past`);
533
+ }
534
+ /**
535
+ * The commit of frozen units and of records advanced past without a frame: the last turn they
536
+ * began is now the turn a terminal reported before any later record ends, and a run they closed
537
+ * owes nothing, its terminal being in the frozen frame.
538
+ */
539
+ committed(units, turns) {
540
+ for (const u of units) {
541
+ for (const t of u.turns ?? [])
542
+ this.lastTurn = t;
543
+ // The debt settled is the run the terminal CLOSES, not the run the frame is labelled with: the
544
+ // finish the next prompt synthesizes for its predecessor travels in the successor's frame, and
545
+ // a debt the successor already carries must survive that freeze.
546
+ for (const e of u.events) {
547
+ if (e.type === AGUI_EVENT_TYPE.RUN_FINISHED)
548
+ this.owed.delete(e.runId);
549
+ else if (e.type === AGUI_EVENT_TYPE.RUN_ERROR)
550
+ this.owed.delete(u.runId);
551
+ }
552
+ }
553
+ for (const t of turns)
554
+ this.lastTurn = t;
555
+ }
422
556
  /**
423
557
  * Close the run this stream currently has open, at a boundary the RECORD STREAM CANNOT SEE.
424
558
  *
@@ -451,8 +585,8 @@ export class AguiEmitter {
451
585
  *
452
586
  * **`error.message` AND `error.code` NEVER REACH THE WIRE.** Both are upstream values, so the close
453
587
  * publishes {@link RUN_ERROR_EGRESS_MESSAGE} with no code, the same shape the write path's
454
- * `egressRunError` in `agui.ts` produces (#1431). The frame bound still runs after that, so a close
455
- * whose envelope cannot fit fails loud rather than leaving the run without a terminal.
588
+ * `egressRunError` in `agui.ts` produces (#1431). `packUnits` bounds the closing frame like any
589
+ * other, so a close whose envelope cannot fit fails loud.
456
590
  *
457
591
  * @returns the run that was closed, or `null` when the stream was already at a stopping point.
458
592
  */
@@ -464,6 +598,9 @@ export class AguiEmitter {
464
598
  const runId = this.brackets.runId;
465
599
  if (runId === undefined)
466
600
  return null;
601
+ // Either terminal settles a debt owed to this run: the run is closed, and nothing can be owed
602
+ // to a run that is no longer open.
603
+ this.owed.delete(runId);
467
604
  const cursor = this.wal.frontier.sourceCursor;
468
605
  if (cursor === undefined)
469
606
  throw new Error(`event emitter for ${this.channel}: run "${runId}" is open on a frontier that carries no source ` +
@@ -472,19 +609,11 @@ export class AguiEmitter {
472
609
  `cursor for the closing frame.`);
473
610
  // `RUN_ERROR` carries no `runId` and no `threadId` of its own — its schema is `message` plus an
474
611
  // optional `code` — so the run it closes is named by the frame's unit below, not by the event.
475
- // The bound is measured with the same function and ceiling `packUnits` will use, at the `seq`
476
- // the closing frame will actually carry.
477
612
  const event = o.error
478
- ? boundRunErrorForFrame({
613
+ ? runError({
479
614
  message: RUN_ERROR_EGRESS_MESSAGE,
480
615
  timestamp: o.timestamp,
481
616
  ...(o.cotal ? { cotal: o.cotal } : {}),
482
- threadId: this.threadId,
483
- runId,
484
- epoch: this.wal.epoch,
485
- seq: this.wal.frontier.seq + 1,
486
- measure: (f) => this.measure(f),
487
- limit: this.ep.maxPayload,
488
617
  })
489
618
  : runFinished({
490
619
  threadId: this.threadId,
@@ -512,9 +641,86 @@ export class AguiEmitter {
512
641
  limit: this.ep.maxPayload,
513
642
  });
514
643
  for (const { frame, cursor: c } of frames)
515
- await this.publish(frame, c);
644
+ await this.publish(frame, c, () => { });
516
645
  return runId;
517
646
  }
647
+ /**
648
+ * Queue a terminal the connector reported at a boundary the record stream cannot see, for the
649
+ * turn the source had begun when it was reported: a terminal that arrived while the plane was
650
+ * down, or while a re-establish was in flight. The connector reports a turn's terminal once the
651
+ * turn's records are in the source and before the next turn's are, so the position the source
652
+ * had reached then names the turn: the last one begun before that position. The next pump reads
653
+ * the records up to that position and places the terminal, see {@link place}. Placed on a run,
654
+ * an error is published in place of the first finish the mapper produces for that run, which is
655
+ * the finish a later prompt synthesizes when it finds the run still open, so a failed turn whose
656
+ * next prompt was already written when the plane came back is still closed as a failure, after
657
+ * its own records and before the next run opens, in a frame of its own run; a finish is that
658
+ * synthesized finish. A terminal the pump places on the run it leaves open is handed back by
659
+ * {@link takeOwed} for the close the holder makes. A turn that opened no run takes its terminal
660
+ * and does nothing with it, as a close with no run open does live; a second terminal for one
661
+ * turn is the connector repeating itself, and replaces nothing.
662
+ */
663
+ queueTerminal(entry) {
664
+ if (this.halted)
665
+ throw this.halted;
666
+ // A position that could not be read is reported by the pump that meets it, not as an
667
+ // unhandled rejection before any pump has.
668
+ entry.at.catch(() => undefined);
669
+ this.queued.push(entry);
670
+ }
671
+ /** How many terminals no pump has placed yet. A pump places every one the read reaches. */
672
+ get waitingTerminals() {
673
+ return this.queued.length;
674
+ }
675
+ /** Take the terminal owed to `runId` back, for a close the caller makes itself. */
676
+ takeOwed(runId) {
677
+ const entry = this.owed.get(runId);
678
+ this.owed.delete(runId);
679
+ return entry;
680
+ }
681
+ /**
682
+ * Every terminal this emitter still holds, for a holder that is dropping it: the terminals owed
683
+ * to runs the wire has not closed, then the ones no pump has placed, each with the position it
684
+ * was reported at, so the next emitter places them again from the log it starts from. The last
685
+ * turn the frozen frames began rides along, so the holder can seed the next emitter with it.
686
+ */
687
+ unsettled() {
688
+ return { terminals: [...this.owed.values(), ...this.queued], lastTurn: this.lastTurn };
689
+ }
690
+ /** Seed the last turn the wire began, from the emitter this one replaces: a terminal reported
691
+ * before any record the first pump reads is that turn's. Allowed before anything is pumped. */
692
+ seedLastTurn(turn) {
693
+ if (this.pumped)
694
+ throw new Error(`event emitter for ${this.channel}: the last turn can only be seeded before the first pump`);
695
+ this.lastTurn = turn;
696
+ }
697
+ /**
698
+ * Take the first finish of an owed run out of a record's events and, for a debt that is an
699
+ * error, answer it with a unit of that run carrying the owed error terminal instead. The error
700
+ * carries the fixed egress message, as a close does. `before` is the cursor the owed unit
701
+ * stores: the position ahead of the record the finish came from. A debt that is a finish is
702
+ * answered by the finish itself and changes nothing. The debt is settled when the frame carrying
703
+ * the terminal is frozen, not here: a frame whose write fails before the freeze is read again,
704
+ * and the terminal must still be owed then (Sol, round 11 of 2026-10-13, through the built MCP
705
+ * entry point: a log write refused once while the owed error was being written closed the failed
706
+ * run as a finish).
707
+ */
708
+ takeOwedTerminal(events, before) {
709
+ if (this.owed.size === 0)
710
+ return undefined;
711
+ const at = events.findIndex((e) => e.type === AGUI_EVENT_TYPE.RUN_FINISHED && this.owed.has(e.runId));
712
+ if (at < 0)
713
+ return undefined;
714
+ const finish = events[at];
715
+ const runId = finish.runId;
716
+ const { terminal } = this.owed.get(runId);
717
+ if (!terminal.error)
718
+ return undefined;
719
+ if (before === undefined)
720
+ throw new Error(`event emitter for ${this.channel}: a terminal is owed to run "${runId}" on a frontier that carries no source cursor; a run can only be open because a frame published it`);
721
+ const error = runError({ message: RUN_ERROR_EGRESS_MESSAGE, timestamp: typeof finish.timestamp === "number" ? finish.timestamp : Date.now(), ...(finish.cotal ? { cotal: finish.cotal } : {}) });
722
+ return { unit: { runId, events: [error], cursor: before }, rest: events.filter((_, i) => i !== at) };
723
+ }
518
724
  /** Measure a candidate frame EXACTLY as the wire will, at an upper bound over id and expectation. */
519
725
  measure(frame) {
520
726
  return this.ep.encodedSize({
@@ -524,15 +730,21 @@ export class AguiEmitter {
524
730
  expectedLastSubjectSeq: SIZING_EXPECTATION,
525
731
  });
526
732
  }
527
- /** Transition 1 then the first network attempt. */
528
- async publish(frame, cursor) {
529
- // Advance the real machine by exactly this frame. It cannot throw: the identical sequence was
530
- // already accepted by a clone starting from this same state, in this same order. It is not
733
+ /** Transition 1 then the first network attempt. `frozen` runs once the frame is frozen on disk,
734
+ * before the wire is tried: the commit of what the frame carries, for state the frame settles. */
735
+ async publish(frame, cursor, frozen) {
736
+ // Advance a copy of the machine by exactly this frame. It cannot throw: the identical sequence
737
+ // was already accepted by a clone starting from this same state, in this same order. It is not
531
738
  // wrapped in a diagnosis for that reason — a throw here would be a bug in this file, not a
532
- // stream problem, and dressing it as one would hide it.
739
+ // stream problem, and dressing it as one would hide it. The real machine moves at the FREEZE:
740
+ // a write that fails before it leaves the log without this frame, and the run this frame opens
741
+ // or closes is then neither open nor closed on the wire (Sol, round 11 of 2026-10-13, through
742
+ // the built MCP entry point: a first turn whose frame never froze was reported open, so its
743
+ // terminal was held for a run the log never had, and dropped).
744
+ const next = this.brackets.clone();
533
745
  for (const e of frame.events)
534
- this.brackets.accept(e);
535
- const brackets = this.brackets.snapshot();
746
+ next.accept(e);
747
+ const brackets = next.snapshot();
536
748
  const id = randomUUID();
537
749
  // THE SUBJECT'S TIP, NOT THIS THREAD'S LAST ACK. The two were the same number until a second
538
750
  // session of the same principal existed, and then they were not: the subject is per principal
@@ -543,6 +755,8 @@ export class AguiEmitter {
543
755
  // Durable BEFORE the wire. The order is the whole state machine: a crash between this line and
544
756
  // the next is recoverable precisely because the id and `E` are already frozen on disk.
545
757
  await this.wal.beginSend({ id, E, seq: frame.seq, sourceCursor: cursor, body, brackets });
758
+ this.brackets = next;
759
+ frozen();
546
760
  await this.attempt({ id, E, body, retry: false });
547
761
  }
548
762
  /**