@silverprotocol/core 0.12.4 → 1.0.0-rc.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.
- package/README.md +17 -9
- package/dist/agjson.d.ts +594 -594
- package/dist/agjson.d.ts.map +1 -1
- package/dist/agjson.js +132 -17
- package/dist/agjson.js.map +1 -1
- package/dist/atomic-push.js.map +1 -1
- package/dist/copy-json.d.ts +31 -4
- package/dist/copy-json.d.ts.map +1 -1
- package/dist/copy-json.js +147 -15
- package/dist/copy-json.js.map +1 -1
- package/dist/depth.d.ts +34 -0
- package/dist/depth.d.ts.map +1 -0
- package/dist/depth.js +141 -0
- package/dist/depth.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +7 -2
- package/dist/ingest.d.ts.map +1 -1
- package/dist/ingest.js +36 -4
- package/dist/ingest.js.map +1 -1
- package/dist/input-check.d.ts.map +1 -1
- package/dist/input-check.js +17 -3
- package/dist/input-check.js.map +1 -1
- package/dist/json-patch.d.ts.map +1 -1
- package/dist/json-patch.js +17 -9
- package/dist/json-patch.js.map +1 -1
- package/dist/persist.js.map +1 -1
- package/dist/record.d.ts.map +1 -1
- package/dist/record.js +25 -6
- package/dist/record.js.map +1 -1
- package/dist/reduce.d.ts +1 -1
- package/dist/reduce.d.ts.map +1 -1
- package/dist/reduce.js +293 -112
- package/dist/reduce.js.map +1 -1
- package/dist/stream-assembler.d.ts +6 -7
- package/dist/stream-assembler.d.ts.map +1 -1
- package/dist/stream-assembler.js +21 -18
- package/dist/stream-assembler.js.map +1 -1
- package/dist/wire.d.ts +21 -3
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +34 -5
- package/dist/wire.js.map +1 -1
- package/package.json +1 -1
- package/src/agjson.ts +128 -28
- package/src/atomic-push.ts +2 -2
- package/src/copy-json.ts +143 -17
- package/src/depth.ts +138 -0
- package/src/index.ts +1 -0
- package/src/ingest.ts +35 -4
- package/src/input-check.ts +15 -3
- package/src/json-patch.ts +21 -8
- package/src/record.ts +25 -6
- package/src/reduce.ts +273 -111
- package/src/stream-assembler.ts +21 -18
- package/src/wire.ts +36 -6
package/src/reduce.ts
CHANGED
|
@@ -14,6 +14,9 @@ import type {
|
|
|
14
14
|
} from "./agjson.js";
|
|
15
15
|
import { REMOVE_ALL } from "./agjson.js";
|
|
16
16
|
import { applyPatch } from "./json-patch.js";
|
|
17
|
+
import { deleteOwnProtoMember, withoutOwnProtoMember } from "./copy-json.js";
|
|
18
|
+
import { exceedsDepth, scanDepth } from "./depth.js";
|
|
19
|
+
import { AG_EVENT_MAX_DEPTH } from "./wire.js";
|
|
17
20
|
|
|
18
21
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
19
22
|
// Reducer — the normative event→state fold (spec §5).
|
|
@@ -108,6 +111,13 @@ function isClosedEvent(ev: AgEvent): ev is AgClosedEventType {
|
|
|
108
111
|
const HELD_TURNS_CAP = 64;
|
|
109
112
|
const HELD_LANDINGS_CAP = 1024;
|
|
110
113
|
|
|
114
|
+
/**
|
|
115
|
+
* The deepest node of a state or memory value a patch may leave, counted from
|
|
116
|
+
* the value's root: the depth at which a state.snapshot's `snapshot` or a
|
|
117
|
+
* memory.write's `value` sits below its event root is 1.
|
|
118
|
+
*/
|
|
119
|
+
const STORED_VALUE_MAX_DEPTH = AG_EVENT_MAX_DEPTH - 1;
|
|
120
|
+
|
|
111
121
|
export class Reducer {
|
|
112
122
|
// ── keyed accumulators ──────────────────────────────────────────────────────
|
|
113
123
|
// Messages, keyed by message id (insertion-ordered).
|
|
@@ -127,10 +137,10 @@ export class Reducer {
|
|
|
127
137
|
// Message ids sealed by message.end.
|
|
128
138
|
#sealed: Set<string> = new Set();
|
|
129
139
|
// turnIds legitimately opened via turn.start / subagent.start (or restored
|
|
130
|
-
// by a messages.snapshot's turns?)
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
//
|
|
140
|
+
// by a messages.snapshot's turns?). Distinguishes "a real turn genuinely
|
|
141
|
+
// exists" from a turn the fold only knows by name, so the tool.done adoption
|
|
142
|
+
// CREATE path degrades loudly (resync) instead of creating a message for a
|
|
143
|
+
// turn that was never opened.
|
|
134
144
|
#openedTurns: Set<string> = new Set();
|
|
135
145
|
// draft.4 §5.0 INV-OWNER: every
|
|
136
146
|
// turnId the fold has seen opened — by turn.start, by subagent.start, or by a
|
|
@@ -142,15 +152,28 @@ export class Reducer {
|
|
|
142
152
|
// #openedTurns (tool.done adoption) and never rebuilt from #turns, whose
|
|
143
153
|
// records non-terminal arms may have minted as stubs.
|
|
144
154
|
#seenOpened: Map<string, string | undefined> = new Map();
|
|
155
|
+
// §5 `subagent.start` row (rc.1): the fold's root thread — the first threadId a
|
|
156
|
+
// folded turn.start, message.start, artifact.start or messages.snapshot carried.
|
|
157
|
+
// A nested turn whose parent's thread is not known takes it.
|
|
158
|
+
#rootThread: string | undefined;
|
|
159
|
+
// Nested turns a `subagent.start` opened while neither the parent's thread nor
|
|
160
|
+
// the root thread was known: turnId → parentTurnId. Each one's record is held in
|
|
161
|
+
// #held (result() omits it) until one of the two becomes known (#releaseHeldNested).
|
|
162
|
+
#heldNested: Map<string, string> = new Map();
|
|
145
163
|
// draft.5 §5.0 INV-OWNER (a held record lands on its own turn's thread): the landings of
|
|
146
164
|
// the six record events (source, handoff, prompt.blocked, guardrail.result,
|
|
147
165
|
// display.required, agent.capabilities) for a turn whose thread the fold does
|
|
148
166
|
// not know yet. Each is a turn record result() never shows; it moves into
|
|
149
167
|
// #turns on the turn's thread once a turn.start, subagent.start, message.start
|
|
150
168
|
// or messages.snapshot makes that thread known (#adoptHeld). `landings` counts
|
|
151
|
-
// the events held on it
|
|
152
|
-
|
|
169
|
+
// the record events held on it other than display.required, `dr` the
|
|
170
|
+
// display.required ones, each against its own implementation bound below: a
|
|
171
|
+
// display.required is never discarded, so it survives a turns-carrying
|
|
172
|
+
// snapshot, and counting it apart keeps it from filling the bound the other
|
|
173
|
+
// five landings recover through that snapshot.
|
|
174
|
+
#held: Map<string, { record: AgTurnRecord; landings: number; dr: number }> = new Map();
|
|
153
175
|
#heldLandings = 0;
|
|
176
|
+
#heldDrLandings = 0;
|
|
154
177
|
// draft.5 §5.0 INV-MSG (the paused refresh): closed turns a turn.start has
|
|
155
178
|
// named since their last terminal folded. Only such a turn, closed paused,
|
|
156
179
|
// takes a turn.done{paused}; every terminal that folds removes its id. A
|
|
@@ -185,6 +208,14 @@ export class Reducer {
|
|
|
185
208
|
* R3–R10 fill remaining content types.
|
|
186
209
|
*/
|
|
187
210
|
push(ev: AgEvent): void {
|
|
211
|
+
// One pass before anything reads the event: whether a node sits deeper
|
|
212
|
+
// than AG_EVENT_MAX_DEPTH, and whether an own `__proto__` member is held
|
|
213
|
+
// (the delete walk below runs only on an event that holds one). A
|
|
214
|
+
// too-deep event occupies its seq slot like any other and is folded no
|
|
215
|
+
// further: copying it, or patching the state with it, would overflow the
|
|
216
|
+
// stack. A consuming ingest never yields one; it stubs such a frame.
|
|
217
|
+
const scan = scanDepth(ev, AG_EVENT_MAX_DEPTH);
|
|
218
|
+
|
|
188
219
|
// ── Seq-gap detection (R9) ────────────────────────────────────────────────
|
|
189
220
|
// Seq accounting is UNIVERSAL (INV-SEQ: "gap-free ordinal" over the whole
|
|
190
221
|
// per-invoke stream) — EVERY well-formed event with a numeric seq advances
|
|
@@ -192,7 +223,7 @@ export class Reducer {
|
|
|
192
223
|
// other live-only events). This must run BEFORE the isClosedEvent skip
|
|
193
224
|
// below: an ext/live-only event between two closed events still occupies a
|
|
194
225
|
// seq slot, so skipping it here would let the NEXT closed event look like
|
|
195
|
-
// a forward gap that never happened (found
|
|
226
|
+
// a forward gap that never happened (found in review — the false-park
|
|
196
227
|
// this fixes voided every fold with an ext emission between closed events).
|
|
197
228
|
// After the first event (#lastSeq >= 0), a forward gap sets #resync (park)
|
|
198
229
|
// — including when an ext/live-only event is the one revealing the gap.
|
|
@@ -226,6 +257,7 @@ export class Reducer {
|
|
|
226
257
|
this.#invokeBlockIds.clear();
|
|
227
258
|
this.#finalToolDone.clear();
|
|
228
259
|
}
|
|
260
|
+
if (scan.tooDeep) return;
|
|
229
261
|
|
|
230
262
|
// Store-time isolation: the handlers below keep references into the event
|
|
231
263
|
// (a block, a providerMetadata or _meta bag, a tool result, an outcome, a
|
|
@@ -235,10 +267,21 @@ export class Reducer {
|
|
|
235
267
|
// It runs before the ext guard below, which then narrows the copy. An event
|
|
236
268
|
// structuredClone cannot copy (a function-valued field) folds as pushed, as
|
|
237
269
|
// it always did, rather than throwing out of push().
|
|
270
|
+
//
|
|
271
|
+
// §13.7: the fold omits an own `__proto__` member at every depth of every
|
|
272
|
+
// event it folds. On the clone the walk deletes in place, when the scan
|
|
273
|
+
// above found one; an event that cannot be cloned gets a rebuilt copy, so
|
|
274
|
+
// the host's object is never mutated. Neither walk throws out of push().
|
|
238
275
|
try {
|
|
239
276
|
ev = structuredClone(ev);
|
|
277
|
+
if (scan.hasOwnProto) deleteOwnProtoMember(ev);
|
|
240
278
|
} catch {
|
|
241
|
-
// not cloneable: fold the event as pushed
|
|
279
|
+
// not cloneable: fold the event as pushed, less the member
|
|
280
|
+
try {
|
|
281
|
+
ev = withoutOwnProtoMember(ev);
|
|
282
|
+
} catch {
|
|
283
|
+
// a host getter that throws: fold the event as pushed
|
|
284
|
+
}
|
|
242
285
|
}
|
|
243
286
|
|
|
244
287
|
// Ext events (`ext.<vendor>.<key>`) are live-only / non-folding (§4/§12).
|
|
@@ -253,7 +296,7 @@ export class Reducer {
|
|
|
253
296
|
case "turn.start": {
|
|
254
297
|
// draft.5 INV-MSG: a turn.start naming a closed turn opens its paused refresh.
|
|
255
298
|
if (this.#isClosedTurn(ev.turnId)) this.#restartedSinceClose.add(ev.turnId);
|
|
256
|
-
//
|
|
299
|
+
// A turn.start always counts as a legitimately opened turn.
|
|
257
300
|
this.#openedTurns.add(ev.turnId);
|
|
258
301
|
this.#seenOpened.set(ev.turnId, ev.threadId);
|
|
259
302
|
// Idempotent: if the turn already exists, merge defined fields only. A
|
|
@@ -273,15 +316,24 @@ export class Reducer {
|
|
|
273
316
|
existing.trigger = ev.trigger;
|
|
274
317
|
}
|
|
275
318
|
}
|
|
319
|
+
this.#rootThread ??= ev.threadId;
|
|
320
|
+
this.#releaseHeldNested();
|
|
276
321
|
break;
|
|
277
322
|
}
|
|
278
323
|
|
|
279
324
|
// ── MESSAGE lifecycle ──────────────────────────────────────────────────
|
|
280
325
|
case "message.start": {
|
|
326
|
+
// INV-MSG (rc.1): a message.start naming an id the fold already holds as a
|
|
327
|
+
// message — open or sealed, carried by a folded messages.snapshot, or
|
|
328
|
+
// adopted by a tool.done — parks and the fold is unchanged. The message
|
|
329
|
+
// set is fold-wide, so a later invoke reusing a held id parks too; a
|
|
330
|
+
// folded message.remove, or a snapshot that omits the id, releases it.
|
|
331
|
+
// Checked before any write below, so a parked repeat registers nothing.
|
|
332
|
+
if (this.#messages.has(ev.id)) { this.#resync = true; break; }
|
|
281
333
|
// INV-MSG: a message.start for a turn that already closed parks.
|
|
282
334
|
if (this.#isClosedTurn(this.#resolveTurnId(ev.turnId) ?? ev.turnId)) { this.#resync = true; break; }
|
|
283
335
|
// draft.5 INV-OWNER: a live message.start makes its turn's thread
|
|
284
|
-
// known and joins
|
|
336
|
+
// known and joins the seen-opened set (§5.0 INV-OWNER) (a turn.start's thread wins; else
|
|
285
337
|
// the first one folded), and a record held for the turn is adopted onto it.
|
|
286
338
|
if (this.#seenOpened.get(ev.turnId) === undefined) this.#seenOpened.set(ev.turnId, ev.threadId);
|
|
287
339
|
const knownThread = this.#seenOpened.get(ev.turnId);
|
|
@@ -303,6 +355,8 @@ export class Reducer {
|
|
|
303
355
|
this.#messages.set(ev.id, msg);
|
|
304
356
|
// Register partition pointer: (turnId, candidateIndex ?? 0) → message id.
|
|
305
357
|
this.#openMsg.set(partKey(ev.turnId, ev.candidateIndex ?? 0), ev.id);
|
|
358
|
+
this.#rootThread ??= ev.threadId;
|
|
359
|
+
this.#releaseHeldNested();
|
|
306
360
|
break;
|
|
307
361
|
}
|
|
308
362
|
|
|
@@ -324,35 +378,38 @@ export class Reducer {
|
|
|
324
378
|
|
|
325
379
|
// ── SUBAGENT lifecycle ─────────────────────────────────────────────────
|
|
326
380
|
case "subagent.start": {
|
|
327
|
-
//
|
|
381
|
+
// A subagent.start always counts as a legitimately
|
|
328
382
|
// opened turn, even on the idempotent-duplicate early return below.
|
|
329
383
|
this.#openedTurns.add(ev.turnId);
|
|
330
|
-
// Idempotent:
|
|
384
|
+
// Idempotent: a second open of a turn the fold already holds merges fields and never duplicates it (§5
|
|
385
|
+
// turn-open idempotency): a record with no parentTurnId takes this one; a record that has one keeps it, and
|
|
386
|
+
// a known thread stays.
|
|
331
387
|
const existingNested = this.#turns.get(ev.turnId);
|
|
332
388
|
if (existingNested !== undefined) {
|
|
389
|
+
if (existingNested.parentTurnId === undefined) existingNested.parentTurnId = ev.parentTurnId;
|
|
333
390
|
this.#seenOpened.set(ev.turnId, existingNested.threadId);
|
|
334
391
|
break;
|
|
335
392
|
}
|
|
336
|
-
//
|
|
337
|
-
//
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
//
|
|
354
|
-
|
|
355
|
-
|
|
393
|
+
// §5 `subagent.start` row (rc.1): the nested turn's thread is the parent
|
|
394
|
+
// turn's thread when that thread is known (INV-OWNER), else the fold's root
|
|
395
|
+
// thread; until one of the two is known the record is held, result() omits
|
|
396
|
+
// it, and the first that becomes known gives its thread (#releaseHeldNested).
|
|
397
|
+
const threadId = this.#threadOfTurn(ev.parentTurnId) ?? this.#rootThread;
|
|
398
|
+
if (threadId === undefined) {
|
|
399
|
+
if (!this.#heldNested.has(ev.turnId) && this.#heldNested.size >= HELD_TURNS_CAP) {
|
|
400
|
+
this.#resync = true; // degrade loudly past the implementation bound
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
let held = this.#held.get(ev.turnId);
|
|
404
|
+
if (held === undefined) {
|
|
405
|
+
held = { record: { turnId: ev.turnId, threadId: "" }, landings: 0, dr: 0 };
|
|
406
|
+
this.#held.set(ev.turnId, held);
|
|
407
|
+
}
|
|
408
|
+
held.record.parentTurnId ??= ev.parentTurnId;
|
|
409
|
+
this.#heldNested.set(ev.turnId, held.record.parentTurnId ?? ev.parentTurnId);
|
|
410
|
+
// Seen opened, its thread not known yet: a terminal for it lands on the held record.
|
|
411
|
+
this.#seenOpened.set(ev.turnId, undefined);
|
|
412
|
+
break;
|
|
356
413
|
}
|
|
357
414
|
// A record held for this nested turn takes the thread and the
|
|
358
415
|
// parentTurnId here; otherwise a new record opens.
|
|
@@ -364,6 +421,7 @@ export class Reducer {
|
|
|
364
421
|
});
|
|
365
422
|
}
|
|
366
423
|
this.#seenOpened.set(ev.turnId, threadId);
|
|
424
|
+
this.#releaseHeldNested();
|
|
367
425
|
break;
|
|
368
426
|
}
|
|
369
427
|
|
|
@@ -423,16 +481,16 @@ export class Reducer {
|
|
|
423
481
|
if (pos === undefined) break;
|
|
424
482
|
const msg = this.#messages.get(pos.messageId);
|
|
425
483
|
if (msg === undefined) break;
|
|
426
|
-
// INV-MSG (SPEC.md:745, draft.4
|
|
484
|
+
// INV-MSG (SPEC.md:745, draft.4): a block-finalizing event into a sealed
|
|
427
485
|
// message, or into any message of a closed turn, parks like a delta.
|
|
428
|
-
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
486
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
429
487
|
const block = msg.content[pos.index];
|
|
430
488
|
if (block === undefined || block.type !== "text") break;
|
|
431
489
|
// draft.4 phase: a value on the end REPLACES the start one; absent keeps
|
|
432
490
|
// it; never filled once the owning message is sealed.
|
|
433
491
|
if (ev.phase !== undefined && !this.#sealed.has(msg.id)) block.phase = ev.phase;
|
|
434
492
|
setProviderMeta(block, ev.providerMetadata);
|
|
435
|
-
// STREAMED-text citations carrier
|
|
493
|
+
// STREAMED-text citations carrier: attach citations to the
|
|
436
494
|
// sealed block named by `ev.id` — never re-emitted as a duplicate
|
|
437
495
|
// supplement block.
|
|
438
496
|
if (ev.citations !== undefined) block.citations = ev.citations;
|
|
@@ -479,9 +537,9 @@ export class Reducer {
|
|
|
479
537
|
if (pos === undefined) break;
|
|
480
538
|
const msg = this.#messages.get(pos.messageId);
|
|
481
539
|
if (msg === undefined) break;
|
|
482
|
-
// INV-MSG (SPEC.md:745, draft.4
|
|
540
|
+
// INV-MSG (SPEC.md:745, draft.4): a block-finalizing event into a sealed
|
|
483
541
|
// message, or into any message of a closed turn, parks like a delta.
|
|
484
|
-
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
542
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
485
543
|
const block = msg.content[pos.index];
|
|
486
544
|
if (block === undefined || block.type !== "reasoning") break;
|
|
487
545
|
// draft.4 phase: same rule as text.end (end REPLACES, absent keeps, no post-seal fill).
|
|
@@ -511,9 +569,9 @@ export class Reducer {
|
|
|
511
569
|
if (pos === undefined) break;
|
|
512
570
|
const msg = this.#messages.get(pos.messageId);
|
|
513
571
|
if (msg === undefined) break;
|
|
514
|
-
// INV-MSG (SPEC.md:745, draft.4
|
|
572
|
+
// INV-MSG (SPEC.md:745, draft.4): a block-finalizing event into a sealed
|
|
515
573
|
// message, or into any message of a closed turn, parks like a delta.
|
|
516
|
-
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
574
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
517
575
|
const block = msg.content[pos.index];
|
|
518
576
|
if (block === undefined || block.type !== "reasoning") break;
|
|
519
577
|
// Use accumulated opaque scratch if present; otherwise use ev.value directly.
|
|
@@ -572,9 +630,9 @@ export class Reducer {
|
|
|
572
630
|
if (pos === undefined) break;
|
|
573
631
|
const msg = this.#messages.get(pos.messageId);
|
|
574
632
|
if (msg === undefined) break;
|
|
575
|
-
// INV-MSG (SPEC.md:745, draft.4
|
|
633
|
+
// INV-MSG (SPEC.md:745, draft.4): a block-finalizing event into a sealed
|
|
576
634
|
// message, or into any message of a closed turn, parks like a delta.
|
|
577
|
-
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
635
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard
|
|
578
636
|
const block = msg.content[pos.index];
|
|
579
637
|
if (block === undefined || block.type !== "tool-call") break;
|
|
580
638
|
block.input = ev.input;
|
|
@@ -596,10 +654,10 @@ export class Reducer {
|
|
|
596
654
|
// ── TOOL-RESULT blocks ────────────────────────────────────────────────────
|
|
597
655
|
case "tool.done": {
|
|
598
656
|
// INV-BLOCK: a second FINAL (more-less) tool.done for one call parks.
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
}
|
|
657
|
+
// INV-BLOCK (rc.1): a more:true after the call's final (more-less) result
|
|
658
|
+
// landed in this invoke parks; a final result is never re-opened within its
|
|
659
|
+
// invoke. #finalToolDone is invoke-scoped (reset at seq 0).
|
|
660
|
+
if (this.#finalToolDone.has(ev.toolCallId)) { this.#resync = true; break; }
|
|
603
661
|
const resultKey = `result:${ev.toolCallId}`;
|
|
604
662
|
const existingPos = this.#blockPos.get(resultKey);
|
|
605
663
|
|
|
@@ -616,8 +674,7 @@ export class Reducer {
|
|
|
616
674
|
// published content. Resolve the block's owning turn via the
|
|
617
675
|
// #blockPos → messageId → message.turnId chain and park instead of mutating
|
|
618
676
|
// (mirrors the adoption path's closed-turn/sealed guard above).
|
|
619
|
-
|
|
620
|
-
if (this.#sealed.has(msg.id) || this.#isClosedTurn(ownerTurnKey)) {
|
|
677
|
+
if (this.#sealed.has(msg.id) || this.#isClosedTurn(msg.turnId)) {
|
|
621
678
|
this.#resync = true;
|
|
622
679
|
break;
|
|
623
680
|
}
|
|
@@ -650,7 +707,7 @@ export class Reducer {
|
|
|
650
707
|
if (ev.dynamic !== undefined) block.dynamic = ev.dynamic;
|
|
651
708
|
if (ev._meta !== undefined) block._meta = ev._meta;
|
|
652
709
|
setProviderMeta(block, ev.providerMetadata);
|
|
653
|
-
// Typed preliminary flag mirrors the block's `more` state
|
|
710
|
+
// Typed preliminary flag mirrors the block's `more` state:
|
|
654
711
|
// more:true keeps it set (kept open); the final more-less tool.done
|
|
655
712
|
// clears it and closes the result (removed from open tracking).
|
|
656
713
|
if (ev.more) {
|
|
@@ -658,13 +715,14 @@ export class Reducer {
|
|
|
658
715
|
} else {
|
|
659
716
|
delete block.preliminary;
|
|
660
717
|
this.#blockPos.delete(resultKey);
|
|
718
|
+
this.#finalToolDone.add(ev.toolCallId);
|
|
661
719
|
}
|
|
662
720
|
} else {
|
|
663
721
|
// CREATE path: first tool.done for this toolCallId.
|
|
664
722
|
let msg: AgMessage | undefined;
|
|
665
723
|
if (ev.messageId !== undefined) {
|
|
666
724
|
// SPEC §5 tool.done row: the landed tool-result message ADOPTS
|
|
667
|
-
// ev.messageId as its own id (stable ToolMessage identity
|
|
725
|
+
// ev.messageId as its own id (stable ToolMessage identity).
|
|
668
726
|
const existing = this.#messages.get(ev.messageId);
|
|
669
727
|
// INV-MSG enforcement: a sealed message, or ANY message of a closed
|
|
670
728
|
// turn — including one that would need to be freshly created — is
|
|
@@ -672,17 +730,19 @@ export class Reducer {
|
|
|
672
730
|
// would land in (the existing message's own turnId, or the turn a
|
|
673
731
|
// new message would take) BEFORE either sub-path runs, and park
|
|
674
732
|
// instead of silently attaching/creating past the seal or the
|
|
675
|
-
// turn's binding window.
|
|
676
|
-
//
|
|
733
|
+
// turn's binding window. A message with no turnId, or a new one whose
|
|
734
|
+
// turn resolves to none, has no turn open to adoption and parks below,
|
|
735
|
+
// whatever the fold's turns are named.
|
|
677
736
|
const targetTurnKey =
|
|
678
737
|
existing !== undefined
|
|
679
|
-
?
|
|
680
|
-
: (this.#resolveTurnId(ev.turnId) ?? ev.turnId
|
|
681
|
-
//
|
|
682
|
-
//
|
|
683
|
-
//
|
|
684
|
-
//
|
|
738
|
+
? existing.turnId
|
|
739
|
+
: (this.#resolveTurnId(ev.turnId) ?? ev.turnId);
|
|
740
|
+
// A turn that was never legitimately opened (turn.start /
|
|
741
|
+
// subagent.start / a snapshot's turns?) is just as invalid an
|
|
742
|
+
// adoption target as a sealed message or a closed turn: park rather
|
|
743
|
+
// than create a message for it.
|
|
685
744
|
if (
|
|
745
|
+
targetTurnKey === undefined ||
|
|
686
746
|
this.#sealed.has(ev.messageId) ||
|
|
687
747
|
this.#isClosedTurn(targetTurnKey) ||
|
|
688
748
|
!this.#openedTurns.has(targetTurnKey)
|
|
@@ -739,6 +799,8 @@ export class Reducer {
|
|
|
739
799
|
// If more:true, keep the block open for subsequent merge.
|
|
740
800
|
if (ev.more) {
|
|
741
801
|
this.#blockPos.set(resultKey, { messageId: msg.id, index });
|
|
802
|
+
} else {
|
|
803
|
+
this.#finalToolDone.add(ev.toolCallId);
|
|
742
804
|
}
|
|
743
805
|
}
|
|
744
806
|
break;
|
|
@@ -792,7 +854,7 @@ export class Reducer {
|
|
|
792
854
|
// ── TURN-RECORD events (R5) ───────────────────────────────────────────────
|
|
793
855
|
|
|
794
856
|
case "turn.done": {
|
|
795
|
-
//
|
|
857
|
+
// INV-OWNER (draft.4 §5.0): a terminal for a turn this fold never saw
|
|
796
858
|
// opened is not an unresolvable owner: it folds onto no turn record and
|
|
797
859
|
// does not park. A CORE client that ignores subagent.*, or a fold whose
|
|
798
860
|
// opener was dropped, sees exactly this; a producer's lone terminal fails
|
|
@@ -817,7 +879,7 @@ export class Reducer {
|
|
|
817
879
|
}
|
|
818
880
|
// messageMetadata: REPLACE the whole messageMetadata bag of the message named
|
|
819
881
|
// by turn.done.messageId when present, else of the open message of this turn
|
|
820
|
-
// (
|
|
882
|
+
// (§5 turn.done row, §13.10). No per-key merge.
|
|
821
883
|
// (Read happens BEFORE the binding window closes below.)
|
|
822
884
|
if (ev.messageMetadata !== undefined) {
|
|
823
885
|
const msg = ev.messageId !== undefined ? this.#messages.get(ev.messageId) : this.openMessage(ev.turnId, 0);
|
|
@@ -833,15 +895,14 @@ export class Reducer {
|
|
|
833
895
|
|
|
834
896
|
case "turn.error": {
|
|
835
897
|
// Non-folding into content; sets outcome={type:"error",...} on the turn record.
|
|
836
|
-
//
|
|
898
|
+
// INV-OWNER, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
|
|
837
899
|
// the sole OPEN turn, never a closed one; with none or several open its
|
|
838
900
|
// owner is unresolvable and the fold parks (INV-OWNER, draft.5).
|
|
839
901
|
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
840
|
-
const
|
|
841
|
-
if (
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
902
|
+
const owner = ev.turnId ?? this.#soleOpenTurn();
|
|
903
|
+
if (owner === undefined) { this.#resync = true; break; }
|
|
904
|
+
const turn = this.#seenTurnRecord(owner);
|
|
905
|
+
if (turn === undefined) break;
|
|
845
906
|
// draft.5 INV-MSG: a turn.error for a closed turn parks, writing nothing.
|
|
846
907
|
if (!this.#terminalMayFold(turn, false)) break;
|
|
847
908
|
// draft.5: `code` and `retriable` are recorded when turn.error carries them.
|
|
@@ -864,16 +925,15 @@ export class Reducer {
|
|
|
864
925
|
case "turn.abort": {
|
|
865
926
|
// Non-folding into content; sets a dedicated aborted outcome on the turn record
|
|
866
927
|
// (symmetric with turn.error). taskState is verbatim-A2A only and is NEVER
|
|
867
|
-
// reducer-invented
|
|
868
|
-
//
|
|
928
|
+
// reducer-invented — it stays whatever a prior turn.done left it as.
|
|
929
|
+
// INV-OWNER, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
|
|
869
930
|
// the sole OPEN turn, never a closed one; with none or several open its
|
|
870
931
|
// owner is unresolvable and the fold parks (INV-OWNER, draft.5).
|
|
871
932
|
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
872
|
-
const
|
|
873
|
-
if (
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
}
|
|
933
|
+
const owner = ev.turnId ?? this.#soleOpenTurn();
|
|
934
|
+
if (owner === undefined) { this.#resync = true; break; }
|
|
935
|
+
const turn = this.#seenTurnRecord(owner);
|
|
936
|
+
if (turn === undefined) break;
|
|
877
937
|
// draft.5 INV-MSG: a turn.abort for a closed turn parks, writing nothing.
|
|
878
938
|
if (!this.#terminalMayFold(turn, false)) break;
|
|
879
939
|
turn.outcome = { type: "aborted", ...(ev.reason !== undefined ? { reason: ev.reason } : {}) };
|
|
@@ -886,7 +946,7 @@ export class Reducer {
|
|
|
886
946
|
case "source": {
|
|
887
947
|
// Append sourceId to the turn's sourceIds[] in order (preserve groundingChunks order),
|
|
888
948
|
// AND land the FULL record (payload/chunkIndex/providerMetadata) on sources[] so
|
|
889
|
-
// citations can resolve post-fold
|
|
949
|
+
// citations can resolve post-fold — sourceIds[] stays as the derived index.
|
|
890
950
|
const turn = this.#landingRecord(ev.turnId);
|
|
891
951
|
if (turn === undefined) break;
|
|
892
952
|
if (turn.sourceIds === undefined) {
|
|
@@ -926,7 +986,7 @@ export class Reducer {
|
|
|
926
986
|
// Record safety[] on the turn (merge — append to existing safety, or create) AND
|
|
927
987
|
// land the dedicated promptBlocked record {reason, safety?} so the REQUIRED reason
|
|
928
988
|
// and the blockedness itself survive the fold — a bare-reason block used to fold to
|
|
929
|
-
// ZERO trace
|
|
989
|
+
// ZERO trace.
|
|
930
990
|
const turn = this.#landingRecord(ev.turnId);
|
|
931
991
|
if (turn === undefined) break;
|
|
932
992
|
if (ev.safety !== undefined) {
|
|
@@ -964,7 +1024,7 @@ export class Reducer {
|
|
|
964
1024
|
|
|
965
1025
|
case "display.required": {
|
|
966
1026
|
// MUST NOT drop (ToS). Push {provider, html} onto turn's displayRequired[].
|
|
967
|
-
const turn = this.#landingRecord(ev.turnId);
|
|
1027
|
+
const turn = this.#landingRecord(ev.turnId, true);
|
|
968
1028
|
if (turn === undefined) break;
|
|
969
1029
|
if (turn.displayRequired === undefined) {
|
|
970
1030
|
turn.displayRequired = [];
|
|
@@ -996,6 +1056,8 @@ export class Reducer {
|
|
|
996
1056
|
...(ev.extensions !== undefined ? { extensions: ev.extensions } : {}),
|
|
997
1057
|
};
|
|
998
1058
|
this.#artifacts.set(ev.artifactId, artifact);
|
|
1059
|
+
this.#rootThread ??= ev.threadId;
|
|
1060
|
+
this.#releaseHeldNested();
|
|
999
1061
|
break;
|
|
1000
1062
|
}
|
|
1001
1063
|
|
|
@@ -1054,14 +1116,18 @@ export class Reducer {
|
|
|
1054
1116
|
if (Array.isArray(patch)) {
|
|
1055
1117
|
// RFC-6902 path: requires an existing document to patch against.
|
|
1056
1118
|
if (this.#state === undefined) {
|
|
1119
|
+
// An empty patch applies no operation: nothing to resync, and the
|
|
1120
|
+
// state stays absent.
|
|
1121
|
+
if (patch.length === 0) break;
|
|
1057
1122
|
// No document to apply the patch to — signal resync (same semantics
|
|
1058
1123
|
// as memory.write patch against a never-seeded record).
|
|
1059
1124
|
this.#resync = true;
|
|
1060
1125
|
break;
|
|
1061
1126
|
}
|
|
1062
1127
|
const result = applyPatch(this.#state, patch);
|
|
1063
|
-
if (!result.ok) {
|
|
1064
|
-
// applyPatch failed
|
|
1128
|
+
if (!result.ok || exceedsDepth(result.value, STORED_VALUE_MAX_DEPTH)) {
|
|
1129
|
+
// applyPatch failed, or its result nests deeper than a
|
|
1130
|
+
// state.snapshot could carry — signal resync; leave #state unchanged.
|
|
1065
1131
|
this.#resync = true;
|
|
1066
1132
|
break;
|
|
1067
1133
|
}
|
|
@@ -1117,13 +1183,16 @@ export class Reducer {
|
|
|
1117
1183
|
// PATCH path: mutate an existing record via R6 applyPatch.
|
|
1118
1184
|
const existing = this.#memory.get(memKey);
|
|
1119
1185
|
if (existing === undefined) {
|
|
1186
|
+
// An empty patch applies no operation: no record, no resync.
|
|
1187
|
+
if (Array.isArray(ev.patch) && ev.patch.length === 0) break;
|
|
1120
1188
|
// NEVER seed from {} — set resync flag and bail.
|
|
1121
1189
|
this.#resync = true;
|
|
1122
1190
|
break;
|
|
1123
1191
|
}
|
|
1124
1192
|
const result = applyPatch(existing.value, ev.patch);
|
|
1125
|
-
if (!result.ok) {
|
|
1126
|
-
// applyPatch failed
|
|
1193
|
+
if (!result.ok || exceedsDepth(result.value, STORED_VALUE_MAX_DEPTH)) {
|
|
1194
|
+
// applyPatch failed, or its result nests deeper than a memory.write
|
|
1195
|
+
// value could — set resync flag and leave record unchanged.
|
|
1127
1196
|
this.#resync = true;
|
|
1128
1197
|
break;
|
|
1129
1198
|
}
|
|
@@ -1172,9 +1241,24 @@ export class Reducer {
|
|
|
1172
1241
|
// CONDITIONALLY replace #turns (only if turns? present in event).
|
|
1173
1242
|
if (ev.turns !== undefined) {
|
|
1174
1243
|
this.#turns = new Map(structuredClone(ev.turns).map((t) => [t.turnId, t]));
|
|
1175
|
-
// The snapshot's turns are authoritative
|
|
1176
|
-
|
|
1244
|
+
// The snapshot's turns are authoritative for a landing held before it,
|
|
1245
|
+
// except a display.required: it MUST NOT be dropped by a reducer and is
|
|
1246
|
+
// held, never discarded, until its turn's thread is known (§5, §13.3).
|
|
1247
|
+
// Only those entries stay held; the other landings go.
|
|
1248
|
+
const kept: Map<string, { record: AgTurnRecord; landings: number; dr: number }> = new Map();
|
|
1249
|
+
let keptLandings = 0;
|
|
1250
|
+
for (const [turnId, held] of this.#held) {
|
|
1251
|
+
const entries = held.record.displayRequired;
|
|
1252
|
+
if (entries === undefined || entries.length === 0) continue;
|
|
1253
|
+
kept.set(turnId, { record: { turnId, threadId: "", displayRequired: entries }, landings: 0, dr: entries.length });
|
|
1254
|
+
keptLandings += entries.length;
|
|
1255
|
+
}
|
|
1256
|
+
this.#held = kept;
|
|
1177
1257
|
this.#heldLandings = 0;
|
|
1258
|
+
this.#heldDrLandings = keptLandings;
|
|
1259
|
+
// ...and a held nested record goes with them, keeping only its
|
|
1260
|
+
// display.required landings, placed below.
|
|
1261
|
+
this.#heldNested.clear();
|
|
1178
1262
|
}
|
|
1179
1263
|
|
|
1180
1264
|
// CONDITIONALLY replace #artifacts (only if artifacts? present in event).
|
|
@@ -1211,10 +1295,9 @@ export class Reducer {
|
|
|
1211
1295
|
this.#sealed = new Set();
|
|
1212
1296
|
this.#blockPos = new Map();
|
|
1213
1297
|
// #restartedSinceClose is NOT cleared: a snapshot folds no terminal (draft.5 INV-MSG).
|
|
1214
|
-
//
|
|
1215
|
-
//
|
|
1216
|
-
//
|
|
1217
|
-
// the records it keeps.
|
|
1298
|
+
// A snapshot-restored turn counts as opened: reseed #openedTurns from
|
|
1299
|
+
// the replaced turns in lockstep with #turns above; a turns-omitting
|
|
1300
|
+
// snapshot re-seeds it from the records it keeps.
|
|
1218
1301
|
this.#openedTurns = ev.turns !== undefined
|
|
1219
1302
|
? new Set(ev.turns.map((t) => t.turnId))
|
|
1220
1303
|
: new Set(this.#turns.keys());
|
|
@@ -1237,11 +1320,37 @@ export class Reducer {
|
|
|
1237
1320
|
// draft.5 INV-OWNER: a held record whose thread this snapshot taught is adopted onto
|
|
1238
1321
|
// it. #openedTurns was seeded above from records with a known thread
|
|
1239
1322
|
// only, so an adopted record is not an adoption target by this snapshot.
|
|
1323
|
+
// A held record for a turn the snapshot itself carries joins that record
|
|
1324
|
+
// instead of replacing it: only display.required entries survive a
|
|
1325
|
+
// turns-carrying snapshot. Each held entry matches at most one entry the
|
|
1326
|
+
// snapshot's record already carried (the same landing, folded by the
|
|
1327
|
+
// snapshot's producer); an unmatched one is appended, so repeated entries
|
|
1328
|
+
// keep their count.
|
|
1240
1329
|
for (const turnId of [...this.#held.keys()]) {
|
|
1241
1330
|
const threadId = this.#seenOpened.get(turnId);
|
|
1242
|
-
if (threadId
|
|
1331
|
+
if (threadId === undefined || this.#heldNested.has(turnId)) continue;
|
|
1332
|
+
const present = this.#turns.get(turnId);
|
|
1333
|
+
if (present === undefined) {
|
|
1334
|
+
this.#adoptHeld(turnId, threadId);
|
|
1335
|
+
continue;
|
|
1336
|
+
}
|
|
1337
|
+
const held = this.#held.get(turnId)!;
|
|
1338
|
+
this.#held.delete(turnId);
|
|
1339
|
+
this.#heldLandings -= held.landings;
|
|
1340
|
+
this.#heldDrLandings -= held.dr;
|
|
1341
|
+
const list = (present.displayRequired ??= []);
|
|
1342
|
+
const unmatched = [...list];
|
|
1343
|
+
for (const entry of held.record.displayRequired ?? []) {
|
|
1344
|
+
const i = unmatched.findIndex((e) => e.provider === entry.provider && e.html === entry.html);
|
|
1345
|
+
if (i >= 0) unmatched.splice(i, 1);
|
|
1346
|
+
else list.push(entry);
|
|
1347
|
+
}
|
|
1243
1348
|
}
|
|
1244
1349
|
|
|
1350
|
+
// The root thread: the first threadId the snapshot carried, in its turns, else its messages.
|
|
1351
|
+
this.#rootThread ??= ev.turns?.find((t) => t.threadId !== undefined)?.threadId ?? ev.messages.find((m) => m.threadId !== undefined)?.threadId;
|
|
1352
|
+
this.#releaseHeldNested();
|
|
1353
|
+
|
|
1245
1354
|
// Un-park.
|
|
1246
1355
|
this.#resync = false;
|
|
1247
1356
|
break;
|
|
@@ -1292,7 +1401,7 @@ export class Reducer {
|
|
|
1292
1401
|
// Clear the per-block scratch buffers too (same block-id / toolCallId key),
|
|
1293
1402
|
// else a stale opaque-signature / arg buffer leaks into a LATER message that
|
|
1294
1403
|
// reuses the same id (cross-task scratch leak — byte-identity can't catch it
|
|
1295
|
-
// because it corrupts batch AND incremental identically).
|
|
1404
|
+
// because it corrupts batch AND incremental identically).
|
|
1296
1405
|
this.#toolArgs.delete(blockId);
|
|
1297
1406
|
this.#opaque.delete(blockId);
|
|
1298
1407
|
// INV-BLOCK is about ids PRESENT in the fold (SPEC INV-BLOCK): a removed
|
|
@@ -1374,8 +1483,8 @@ export class Reducer {
|
|
|
1374
1483
|
}
|
|
1375
1484
|
|
|
1376
1485
|
/**
|
|
1377
|
-
* Evict dangling per-block streaming scratch owned by a closed turn
|
|
1378
|
-
*
|
|
1486
|
+
* Evict dangling per-block streaming scratch owned by a closed turn:
|
|
1487
|
+
* #toolArgs/#opaque entries are otherwise reclaimed only on
|
|
1379
1488
|
* assembled/seal/snapshot — an errored/aborted turn leaks them and a reused
|
|
1380
1489
|
* per-invoke block id in a LATER turn would concatenate stale scratch.
|
|
1381
1490
|
*/
|
|
@@ -1411,11 +1520,11 @@ export class Reducer {
|
|
|
1411
1520
|
* folds onto no record closes nothing; a messages.snapshot changes closure
|
|
1412
1521
|
* only through the records its §5 per-container rule leaves in the fold. No
|
|
1413
1522
|
* message of a closed turn (sealed or not, existing or not yet created) is a
|
|
1414
|
-
* valid attach or adoption target. `key` is a resolved turnId, or
|
|
1415
|
-
*
|
|
1523
|
+
* valid attach or adoption target. `key` is a resolved turnId, or undefined
|
|
1524
|
+
* for a message with no turnId, which names no record.
|
|
1416
1525
|
*/
|
|
1417
1526
|
#isClosedTurn(key: string | undefined): boolean {
|
|
1418
|
-
return key !== undefined && this.#turns.get(key)?.outcome !== undefined;
|
|
1527
|
+
return key !== undefined && (this.#turns.get(key) ?? this.#held.get(key)?.record)?.outcome !== undefined;
|
|
1419
1528
|
}
|
|
1420
1529
|
|
|
1421
1530
|
/**
|
|
@@ -1457,20 +1566,60 @@ export class Reducer {
|
|
|
1457
1566
|
// §4 per-turn routing / INV-OWNER (draft.5): an omitted turnId
|
|
1458
1567
|
// resolves to the sole OPEN turn, never to a closed one (through draft.4
|
|
1459
1568
|
// this counted records, so a lone closed turn or a stub could be picked).
|
|
1460
|
-
return this.#
|
|
1569
|
+
return this.#soleOpenTurn();
|
|
1461
1570
|
}
|
|
1462
1571
|
|
|
1463
|
-
/**
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1572
|
+
/**
|
|
1573
|
+
* The sole OPEN turn (§4 per-turn routing, INV-OWNER): one the reducer has seen
|
|
1574
|
+
* opened (or holds a record for) that is not closed. Undefined when none or
|
|
1575
|
+
* several are open.
|
|
1576
|
+
*/
|
|
1577
|
+
#soleOpenTurn(): string | undefined {
|
|
1578
|
+
let found: string | undefined;
|
|
1579
|
+
for (const turnId of new Set([...this.#seenOpened.keys(), ...this.#turns.keys()])) {
|
|
1580
|
+
if (this.#isClosedTurn(turnId)) continue;
|
|
1468
1581
|
if (found !== undefined) return undefined;
|
|
1469
|
-
found =
|
|
1582
|
+
found = turnId;
|
|
1470
1583
|
}
|
|
1471
1584
|
return found;
|
|
1472
1585
|
}
|
|
1473
1586
|
|
|
1587
|
+
/** A turn's known thread: its record's, else the one it was seen opened on. */
|
|
1588
|
+
#threadOfTurn(turnId: string): string | undefined {
|
|
1589
|
+
if (this.#heldNested.has(turnId)) return undefined;
|
|
1590
|
+
return this.#turns.get(turnId)?.threadId ?? this.#seenOpened.get(turnId);
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
/**
|
|
1594
|
+
* Release every held nested record whose parent's thread or the root thread is
|
|
1595
|
+
* now known onto that thread (the parent's first). Repeats until none moves, so
|
|
1596
|
+
* a chain of held nested turns releases in one pass.
|
|
1597
|
+
*/
|
|
1598
|
+
#releaseHeldNested(): void {
|
|
1599
|
+
const release = (turnId: string, parentTurnId: string, threadId: string) => {
|
|
1600
|
+
this.#heldNested.delete(turnId);
|
|
1601
|
+
this.#adoptHeld(turnId, threadId, parentTurnId);
|
|
1602
|
+
this.#seenOpened.set(turnId, threadId);
|
|
1603
|
+
};
|
|
1604
|
+
let moved = true;
|
|
1605
|
+
while (moved && this.#heldNested.size > 0) {
|
|
1606
|
+
moved = false;
|
|
1607
|
+
for (const [turnId, parentTurnId] of [...this.#heldNested]) {
|
|
1608
|
+
const threadId = this.#threadOfTurn(parentTurnId);
|
|
1609
|
+
if (threadId === undefined) continue;
|
|
1610
|
+
release(turnId, parentTurnId, threadId);
|
|
1611
|
+
moved = true;
|
|
1612
|
+
}
|
|
1613
|
+
if (moved || this.#rootThread === undefined) continue;
|
|
1614
|
+
let heads = [...this.#heldNested].filter(([, p]) => !this.#heldNested.has(p));
|
|
1615
|
+
if (heads.length === 0) heads = [...this.#heldNested].slice(0, 1);
|
|
1616
|
+
for (const [turnId, parentTurnId] of heads) {
|
|
1617
|
+
release(turnId, parentTurnId, this.#rootThread);
|
|
1618
|
+
moved = true;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1474
1623
|
/**
|
|
1475
1624
|
* Where a record event lands (draft.5 INV-OWNER). A named turn with a
|
|
1476
1625
|
* record lands on it; one whose thread is known gets a new record on that
|
|
@@ -1479,28 +1628,36 @@ export class Reducer {
|
|
|
1479
1628
|
* event lands on the sole open turn's record, else its owner is unresolvable
|
|
1480
1629
|
* and the fold parks.
|
|
1481
1630
|
*/
|
|
1482
|
-
#landingRecord(turnId: string | undefined): AgTurnRecord | undefined {
|
|
1631
|
+
#landingRecord(turnId: string | undefined, displayRequired = false): AgTurnRecord | undefined {
|
|
1483
1632
|
if (turnId === undefined) {
|
|
1484
|
-
|
|
1485
|
-
if (
|
|
1486
|
-
return sole;
|
|
1633
|
+
turnId = this.#soleOpenTurn();
|
|
1634
|
+
if (turnId === undefined) { this.#resync = true; return undefined; }
|
|
1487
1635
|
}
|
|
1488
1636
|
const existing = this.#turns.get(turnId);
|
|
1489
1637
|
if (existing !== undefined) return existing;
|
|
1490
1638
|
const threadId = this.#seenOpened.get(turnId);
|
|
1491
1639
|
if (threadId !== undefined) return this.#seenTurnRecord(turnId);
|
|
1492
1640
|
let held = this.#held.get(turnId);
|
|
1493
|
-
|
|
1641
|
+
// The bound counts display.required landings and the other five apart (#held).
|
|
1642
|
+
const newTurn = held === undefined || (displayRequired ? held.dr === 0 : held.landings === 0);
|
|
1643
|
+
const heldTurns = newTurn ? [...this.#held.values()].filter((h) => (displayRequired ? h.dr : h.landings) > 0).length : 0;
|
|
1644
|
+
const heldLandings = displayRequired ? this.#heldDrLandings : this.#heldLandings;
|
|
1645
|
+
if ((newTurn && heldTurns >= HELD_TURNS_CAP) || heldLandings >= HELD_LANDINGS_CAP) {
|
|
1494
1646
|
this.#resync = true; // degrade loudly past the bound
|
|
1495
1647
|
return undefined;
|
|
1496
1648
|
}
|
|
1497
1649
|
if (held === undefined) {
|
|
1498
1650
|
// threadId is set on adoption; a held record is never emitted.
|
|
1499
|
-
held = { record: { turnId, threadId: "" }, landings: 0 };
|
|
1651
|
+
held = { record: { turnId, threadId: "" }, landings: 0, dr: 0 };
|
|
1500
1652
|
this.#held.set(turnId, held);
|
|
1501
1653
|
}
|
|
1502
|
-
|
|
1503
|
-
|
|
1654
|
+
if (displayRequired) {
|
|
1655
|
+
held.dr++;
|
|
1656
|
+
this.#heldDrLandings++;
|
|
1657
|
+
} else {
|
|
1658
|
+
held.landings++;
|
|
1659
|
+
this.#heldLandings++;
|
|
1660
|
+
}
|
|
1504
1661
|
return held.record;
|
|
1505
1662
|
}
|
|
1506
1663
|
|
|
@@ -1513,7 +1670,9 @@ export class Reducer {
|
|
|
1513
1670
|
const held = this.#held.get(turnId);
|
|
1514
1671
|
if (held === undefined) return undefined;
|
|
1515
1672
|
this.#held.delete(turnId);
|
|
1673
|
+
this.#heldNested.delete(turnId);
|
|
1516
1674
|
this.#heldLandings -= held.landings;
|
|
1675
|
+
this.#heldDrLandings -= held.dr;
|
|
1517
1676
|
const { turnId: _turnId, threadId: _placeholder, ...landings } = held.record;
|
|
1518
1677
|
const record: AgTurnRecord = {
|
|
1519
1678
|
turnId,
|
|
@@ -1526,18 +1685,21 @@ export class Reducer {
|
|
|
1526
1685
|
}
|
|
1527
1686
|
|
|
1528
1687
|
/**
|
|
1529
|
-
*
|
|
1688
|
+
* INV-OWNER: the record a terminal for a SEEN turn folds onto — the existing one,
|
|
1530
1689
|
* else one created on the thread the fold saw the turn opened on. With no
|
|
1531
1690
|
* thread known it returns undefined and the terminal folds onto no record.
|
|
1532
1691
|
* No other turn's thread is borrowed (that could adopt a subagent's parent
|
|
1533
|
-
* label as a threadId), and
|
|
1692
|
+
* label as a threadId), and an INV-OWNER path never writes a threadId equal to its
|
|
1534
1693
|
* turnId.
|
|
1535
1694
|
*/
|
|
1536
1695
|
#seenTurnRecord(turnId: string): AgTurnRecord | undefined {
|
|
1537
1696
|
const existing = this.#turns.get(turnId);
|
|
1538
1697
|
if (existing !== undefined) return existing;
|
|
1539
1698
|
const threadId = this.#seenOpened.get(turnId);
|
|
1540
|
-
if (threadId === undefined)
|
|
1699
|
+
if (threadId === undefined) {
|
|
1700
|
+
// A nested turn whose record is held: a terminal lands on that record (INV-OWNER).
|
|
1701
|
+
return this.#heldNested.has(turnId) ? this.#held.get(turnId)?.record : undefined;
|
|
1702
|
+
}
|
|
1541
1703
|
const adopted = this.#adoptHeld(turnId, threadId);
|
|
1542
1704
|
if (adopted !== undefined) return adopted;
|
|
1543
1705
|
const record: AgTurnRecord = { turnId, threadId };
|
|
@@ -1581,7 +1743,7 @@ export class Reducer {
|
|
|
1581
1743
|
|
|
1582
1744
|
/**
|
|
1583
1745
|
* Batch fold. Returns the folded result AND the resync signal — a parked fold
|
|
1584
|
-
* is never a silent truncation (
|
|
1746
|
+
* is never a silent truncation (INV-SEQ: "a conformant reducer MUST
|
|
1585
1747
|
* expose its resync condition to the caller").
|
|
1586
1748
|
*/
|
|
1587
1749
|
export function reduce(events: AgEvent[]): { result: AgReduceResult; needsResync: boolean } {
|