@silverprotocol/core 0.6.6 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -6
- package/dist/agjson.d.ts +29 -8
- package/dist/agjson.d.ts.map +1 -1
- package/dist/agjson.js +28 -7
- package/dist/agjson.js.map +1 -1
- package/dist/atomic-push.d.ts +51 -0
- package/dist/atomic-push.d.ts.map +1 -0
- package/dist/atomic-push.js +188 -0
- package/dist/atomic-push.js.map +1 -0
- package/dist/copy-json.d.ts +12 -0
- package/dist/copy-json.d.ts.map +1 -0
- package/dist/copy-json.js +21 -0
- package/dist/copy-json.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +41 -5
- package/dist/ingest.d.ts.map +1 -1
- package/dist/ingest.js +53 -30
- package/dist/ingest.js.map +1 -1
- package/dist/input-check.d.ts +51 -0
- package/dist/input-check.d.ts.map +1 -0
- package/dist/input-check.js +113 -0
- package/dist/input-check.js.map +1 -0
- package/dist/record.d.ts +68 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +112 -0
- package/dist/record.js.map +1 -0
- package/dist/reduce.d.ts +0 -14
- package/dist/reduce.d.ts.map +1 -1
- package/dist/reduce.js +270 -68
- package/dist/reduce.js.map +1 -1
- package/dist/stream-assembler.d.ts +47 -4
- package/dist/stream-assembler.d.ts.map +1 -1
- package/dist/stream-assembler.js +55 -4
- package/dist/stream-assembler.js.map +1 -1
- package/dist/wire.d.ts +96 -0
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +309 -0
- package/dist/wire.js.map +1 -1
- package/package.json +1 -1
- package/src/agjson.ts +33 -10
- package/src/atomic-push.ts +217 -0
- package/src/copy-json.ts +27 -0
- package/src/index.ts +3 -0
- package/src/ingest.ts +71 -26
- package/src/input-check.ts +124 -0
- package/src/record.ts +132 -0
- package/src/reduce.ts +234 -56
- package/src/stream-assembler.ts +74 -8
- package/src/wire.ts +303 -0
package/src/reduce.ts
CHANGED
|
@@ -47,6 +47,28 @@ function mergeProviderMeta(
|
|
|
47
47
|
return { ...existing, ...incoming } as AgProviderMeta;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Merge `incoming` into a block's providerMetadata, assigning only a defined
|
|
52
|
+
* result. A bare `block.providerMetadata = mergeProviderMeta(...)` left an
|
|
53
|
+
* explicit `providerMetadata: undefined` key on blocks that never had any:
|
|
54
|
+
* invisible in JSON, but visible to `in` / `toHaveProperty` / Object.keys
|
|
55
|
+
* consumers (sp-openai's PH-2 finding, 2026-09-23).
|
|
56
|
+
*/
|
|
57
|
+
function setProviderMeta(block: { providerMetadata?: AgProviderMeta }, incoming: AgProviderMeta | undefined): void {
|
|
58
|
+
const merged = mergeProviderMeta(block.providerMetadata, incoming);
|
|
59
|
+
if (merged !== undefined) block.providerMetadata = merged;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Snapshot-fold one optional result field (draft.4 §5 tool.done): the later
|
|
65
|
+
* event's value, or no key at all when the later event omits it.
|
|
66
|
+
*/
|
|
67
|
+
function replaceOrClear<B extends object, K extends keyof B>(block: B, key: K, value: B[K] | undefined): void {
|
|
68
|
+
if (value === undefined) delete block[key];
|
|
69
|
+
else block[key] = value;
|
|
70
|
+
}
|
|
71
|
+
|
|
50
72
|
|
|
51
73
|
/**
|
|
52
74
|
* Build the partition key for the open-message map.
|
|
@@ -105,6 +127,16 @@ export class Reducer {
|
|
|
105
127
|
// path can degrade loudly (resync) instead of silently minting a phantom
|
|
106
128
|
// turn for a key that was never actually opened.
|
|
107
129
|
#openedTurns: Set<string> = new Set();
|
|
130
|
+
// D9 (draft.4 §5.0 INV-OWNER; bar wf_08cf78ac-c30, A tightened): every
|
|
131
|
+
// turnId the fold has seen opened — by turn.start, by subagent.start, or by a
|
|
132
|
+
// folded messages.snapshot (its `turns`, or a message's turnId) — mapped to
|
|
133
|
+
// the threadId it was opened on (undefined when the opener named none). A
|
|
134
|
+
// snapshot that carries `turns` resets it (see the messages.snapshot arm).
|
|
135
|
+
// Only a terminal for one of these folds onto a turn record, and a record it
|
|
136
|
+
// has to create takes that thread, never the turnId. Kept apart from
|
|
137
|
+
// #openedTurns (tool.done adoption) and never rebuilt from #turns, whose
|
|
138
|
+
// records non-terminal arms may have minted as stubs.
|
|
139
|
+
#seenOpened: Map<string, string | undefined> = new Map();
|
|
108
140
|
// block/tool-call id → position in its owning message's content[], for REPLACE.
|
|
109
141
|
#blockPos: Map<string, { messageId: string; index: number }> = new Map();
|
|
110
142
|
|
|
@@ -118,6 +150,13 @@ export class Reducer {
|
|
|
118
150
|
#lastSeq: number = -1;
|
|
119
151
|
#resync: boolean = false;
|
|
120
152
|
|
|
153
|
+
// ── rd-14 INV-BLOCK (invoke-scoped) ─────────────────────────────────────────
|
|
154
|
+
// Block ids created in the current invoke, and toolCallIds whose FINAL
|
|
155
|
+
// (more-less) tool.done already landed. Both reset at the seq-0 restart, so a
|
|
156
|
+
// new invoke may reuse per-invoke ids.
|
|
157
|
+
#invokeBlockIds: Set<string> = new Set();
|
|
158
|
+
#finalToolDone: Set<string> = new Set();
|
|
159
|
+
|
|
121
160
|
/**
|
|
122
161
|
* Feed a single normalized AgEvent into the fold.
|
|
123
162
|
* R1: lifecycle handlers (turn/message/subagent/step).
|
|
@@ -139,6 +178,12 @@ export class Reducer {
|
|
|
139
178
|
if (this.#lastSeq >= 0 && ev.seq > this.#lastSeq + 1) {
|
|
140
179
|
this.#resync = true;
|
|
141
180
|
}
|
|
181
|
+
// rd-14 (founder: "Stall it, like a gap"): a repeated or backward seq ABOVE 0
|
|
182
|
+
// parks exactly like a forward gap. Only seq 0 (a new invoke's restart) may go
|
|
183
|
+
// backward (SPEC INV-SEQ), so a re-delivery can never fold twice.
|
|
184
|
+
if (this.#lastSeq >= 0 && ev.seq > 0 && ev.seq <= this.#lastSeq) {
|
|
185
|
+
this.#resync = true;
|
|
186
|
+
}
|
|
142
187
|
|
|
143
188
|
// ── Park-gate (R9) ───────────────────────────────────────────────────────
|
|
144
189
|
// While parked, process ONLY the two snapshot kinds; all else is ignored.
|
|
@@ -155,6 +200,25 @@ export class Reducer {
|
|
|
155
200
|
// Update #lastSeq for every processed event (including snapshots and
|
|
156
201
|
// non-folding ext/live-only events — seq accounting is universal, see above).
|
|
157
202
|
this.#lastSeq = ev.seq;
|
|
203
|
+
// A seq-0 restart opens a new invoke: the invoke-scoped INV-BLOCK sets reset.
|
|
204
|
+
if (ev.seq === 0) {
|
|
205
|
+
this.#invokeBlockIds.clear();
|
|
206
|
+
this.#finalToolDone.clear();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Store-time isolation: the handlers below keep references into the event
|
|
210
|
+
// (a block, a providerMetadata or _meta bag, a tool result, an outcome, a
|
|
211
|
+
// state snapshot, a patch value, a memory value), so the fold works on its
|
|
212
|
+
// own copy. A host that reuses or mutates its event object after push() can
|
|
213
|
+
// never move the fold; result() clones on the way out, this on the way in.
|
|
214
|
+
// It runs before the ext guard below, which then narrows the copy. An event
|
|
215
|
+
// structuredClone cannot copy (a function-valued field) folds as pushed, as
|
|
216
|
+
// it always did, rather than throwing out of push().
|
|
217
|
+
try {
|
|
218
|
+
ev = structuredClone(ev);
|
|
219
|
+
} catch {
|
|
220
|
+
// not cloneable: fold the event as pushed
|
|
221
|
+
}
|
|
158
222
|
|
|
159
223
|
// Ext events (`ext.<vendor>.<key>`) are live-only / non-folding (§4/§12).
|
|
160
224
|
// Rule them out HERE — after seq accounting above, so an ext event still
|
|
@@ -168,6 +232,7 @@ export class Reducer {
|
|
|
168
232
|
case "turn.start": {
|
|
169
233
|
// Task 8c leg 3: a turn.start always counts as a legitimately opened turn.
|
|
170
234
|
this.#openedTurns.add(ev.turnId);
|
|
235
|
+
this.#seenOpened.set(ev.turnId, ev.threadId);
|
|
171
236
|
// Idempotent: if the turn already exists, merge defined fields only.
|
|
172
237
|
const existing = this.#turns.get(ev.turnId);
|
|
173
238
|
if (existing === undefined) {
|
|
@@ -187,6 +252,8 @@ export class Reducer {
|
|
|
187
252
|
|
|
188
253
|
// ── MESSAGE lifecycle ──────────────────────────────────────────────────
|
|
189
254
|
case "message.start": {
|
|
255
|
+
// rd-14 INV-MSG: a message.start for a turn that already closed parks.
|
|
256
|
+
if (this.#closedTurns.has(this.#resolveTurnId(ev.turnId) ?? ev.turnId)) { this.#resync = true; break; }
|
|
190
257
|
const msg: AgMessage = {
|
|
191
258
|
id: ev.id,
|
|
192
259
|
role: ev.role,
|
|
@@ -229,7 +296,11 @@ export class Reducer {
|
|
|
229
296
|
// opened turn, even on the idempotent-duplicate early return below.
|
|
230
297
|
this.#openedTurns.add(ev.turnId);
|
|
231
298
|
// Idempotent: if the nested turn already exists, skip (never duplicate).
|
|
232
|
-
|
|
299
|
+
const existingNested = this.#turns.get(ev.turnId);
|
|
300
|
+
if (existingNested !== undefined) {
|
|
301
|
+
this.#seenOpened.set(ev.turnId, existingNested.threadId);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
233
304
|
// threadId is required on AgTurnRecord; inherit from the parent turn.
|
|
234
305
|
// subagent.start does not carry threadId on the wire, so we look it up.
|
|
235
306
|
const parentTurn = this.#turns.get(ev.parentTurnId);
|
|
@@ -256,6 +327,7 @@ export class Reducer {
|
|
|
256
327
|
parentTurnId: ev.parentTurnId,
|
|
257
328
|
threadId,
|
|
258
329
|
});
|
|
330
|
+
this.#seenOpened.set(ev.turnId, threadId);
|
|
259
331
|
break;
|
|
260
332
|
}
|
|
261
333
|
|
|
@@ -279,11 +351,13 @@ export class Reducer {
|
|
|
279
351
|
|
|
280
352
|
// ── TEXT blocks ───────────────────────────────────────────────────────────
|
|
281
353
|
case "text.start": {
|
|
354
|
+
if (this.#dupBlock(ev.id)) break; // rd-14 INV-BLOCK
|
|
282
355
|
const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
|
|
283
356
|
if (msg === undefined) { this.#resync = true; break; }
|
|
284
357
|
const block: AgBlock = {
|
|
285
358
|
type: "text",
|
|
286
359
|
text: "", // REQUIRED field — mid-stream result() before any delta must parse
|
|
360
|
+
...(ev.phase !== undefined ? { phase: ev.phase } : {}), // draft.4 open-string label
|
|
287
361
|
...(ev.providerMetadata !== undefined ? { providerMetadata: ev.providerMetadata } : {}),
|
|
288
362
|
...(ev._meta !== undefined ? { _meta: ev._meta } : {}),
|
|
289
363
|
};
|
|
@@ -298,12 +372,13 @@ export class Reducer {
|
|
|
298
372
|
if (pos === undefined) break;
|
|
299
373
|
const msg = this.#messages.get(pos.messageId);
|
|
300
374
|
if (msg === undefined) break;
|
|
301
|
-
// INV-MSG (SPEC.md:745): a straggler delta into a sealed message
|
|
302
|
-
|
|
375
|
+
// INV-MSG (SPEC.md:745): a straggler delta into a sealed message, or into
|
|
376
|
+
// any message of a closed turn (rd-14), parks.
|
|
377
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG delta guard
|
|
303
378
|
const block = msg.content[pos.index];
|
|
304
379
|
if (block === undefined || block.type !== "text") break;
|
|
305
380
|
block.text += ev.delta;
|
|
306
|
-
|
|
381
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
307
382
|
break;
|
|
308
383
|
}
|
|
309
384
|
|
|
@@ -312,9 +387,15 @@ export class Reducer {
|
|
|
312
387
|
if (pos === undefined) break;
|
|
313
388
|
const msg = this.#messages.get(pos.messageId);
|
|
314
389
|
if (msg === undefined) break;
|
|
390
|
+
// INV-MSG (SPEC.md:745, draft.4 E5): a block-finalizing event into a sealed
|
|
391
|
+
// message, or into any message of a closed turn, parks like a delta.
|
|
392
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard (E5)
|
|
315
393
|
const block = msg.content[pos.index];
|
|
316
394
|
if (block === undefined || block.type !== "text") break;
|
|
317
|
-
|
|
395
|
+
// draft.4 phase: a value on the end REPLACES the start one; absent keeps
|
|
396
|
+
// it; never filled once the owning message is sealed.
|
|
397
|
+
if (ev.phase !== undefined && !this.#sealed.has(msg.id)) block.phase = ev.phase;
|
|
398
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
318
399
|
// STREAMED-text citations carrier (audit M22): attach citations to the
|
|
319
400
|
// sealed block named by `ev.id` — never re-emitted as a duplicate
|
|
320
401
|
// supplement block.
|
|
@@ -324,11 +405,13 @@ export class Reducer {
|
|
|
324
405
|
|
|
325
406
|
// ── REASONING blocks ──────────────────────────────────────────────────────
|
|
326
407
|
case "reasoning.start": {
|
|
408
|
+
if (this.#dupBlock(ev.id)) break; // rd-14 INV-BLOCK
|
|
327
409
|
const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
|
|
328
410
|
if (msg === undefined) { this.#resync = true; break; }
|
|
329
411
|
const block: AgBlock = {
|
|
330
412
|
type: "reasoning",
|
|
331
413
|
text: "", // seeded — mid-stream result() before any delta must parse
|
|
414
|
+
...(ev.phase !== undefined ? { phase: ev.phase } : {}), // draft.4 open-string label
|
|
332
415
|
...(ev.providerMetadata !== undefined ? { providerMetadata: ev.providerMetadata } : {}),
|
|
333
416
|
...(ev._meta !== undefined ? { _meta: ev._meta } : {}),
|
|
334
417
|
...(ev.itemId !== undefined ? { itemId: ev.itemId } : {}),
|
|
@@ -344,13 +427,14 @@ export class Reducer {
|
|
|
344
427
|
if (pos === undefined) break;
|
|
345
428
|
const msg = this.#messages.get(pos.messageId);
|
|
346
429
|
if (msg === undefined) break;
|
|
347
|
-
// INV-MSG (SPEC.md:745): a straggler delta into a sealed message
|
|
348
|
-
|
|
430
|
+
// INV-MSG (SPEC.md:745): a straggler delta into a sealed message, or into
|
|
431
|
+
// any message of a closed turn (rd-14), parks.
|
|
432
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG delta guard
|
|
349
433
|
const block = msg.content[pos.index];
|
|
350
434
|
if (block === undefined || block.type !== "reasoning") break;
|
|
351
435
|
// APPEND delta to text (in-order concat of parts)
|
|
352
436
|
block.text = (block.text ?? "") + ev.delta;
|
|
353
|
-
|
|
437
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
354
438
|
break;
|
|
355
439
|
}
|
|
356
440
|
|
|
@@ -359,9 +443,14 @@ export class Reducer {
|
|
|
359
443
|
if (pos === undefined) break;
|
|
360
444
|
const msg = this.#messages.get(pos.messageId);
|
|
361
445
|
if (msg === undefined) break;
|
|
446
|
+
// INV-MSG (SPEC.md:745, draft.4 E5): a block-finalizing event into a sealed
|
|
447
|
+
// message, or into any message of a closed turn, parks like a delta.
|
|
448
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard (E5)
|
|
362
449
|
const block = msg.content[pos.index];
|
|
363
450
|
if (block === undefined || block.type !== "reasoning") break;
|
|
364
|
-
|
|
451
|
+
// draft.4 phase: same rule as text.end (end REPLACES, absent keeps, no post-seal fill).
|
|
452
|
+
if (ev.phase !== undefined && !this.#sealed.has(msg.id)) block.phase = ev.phase;
|
|
453
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
365
454
|
if (ev.provider !== undefined) {
|
|
366
455
|
block.provider = ev.provider;
|
|
367
456
|
}
|
|
@@ -374,7 +463,7 @@ export class Reducer {
|
|
|
374
463
|
// parks. The block is resolved by id; with no known block the delta
|
|
375
464
|
// stays scratch-only, as before.
|
|
376
465
|
const opaquePos = this.#blockPos.get(ev.id);
|
|
377
|
-
if (opaquePos !== undefined && this.#
|
|
466
|
+
if (opaquePos !== undefined && this.#isClosedTarget(opaquePos.messageId)) { this.#resync = true; break; } // INV-MSG delta guard
|
|
378
467
|
const existing = this.#opaque.get(ev.id) ?? "";
|
|
379
468
|
this.#opaque.set(ev.id, existing + ev.delta);
|
|
380
469
|
break;
|
|
@@ -386,6 +475,9 @@ export class Reducer {
|
|
|
386
475
|
if (pos === undefined) break;
|
|
387
476
|
const msg = this.#messages.get(pos.messageId);
|
|
388
477
|
if (msg === undefined) break;
|
|
478
|
+
// INV-MSG (SPEC.md:745, draft.4 E5): a block-finalizing event into a sealed
|
|
479
|
+
// message, or into any message of a closed turn, parks like a delta.
|
|
480
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard (E5)
|
|
389
481
|
const block = msg.content[pos.index];
|
|
390
482
|
if (block === undefined || block.type !== "reasoning") break;
|
|
391
483
|
// Use accumulated opaque scratch if present; otherwise use ev.value directly.
|
|
@@ -405,6 +497,7 @@ export class Reducer {
|
|
|
405
497
|
|
|
406
498
|
// ── TOOL-CALL blocks ──────────────────────────────────────────────────────
|
|
407
499
|
case "tool.start": {
|
|
500
|
+
if (this.#dupBlock(ev.toolCallId)) break; // rd-14 INV-BLOCK
|
|
408
501
|
const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
|
|
409
502
|
if (msg === undefined) { this.#resync = true; break; }
|
|
410
503
|
const block: AgBlock = {
|
|
@@ -431,7 +524,7 @@ export class Reducer {
|
|
|
431
524
|
// APPEND raw partial-JSON delta to scratch (NEVER authoritative input).
|
|
432
525
|
// INV-MSG (SPEC.md:745): a straggler into a sealed message's tool call parks.
|
|
433
526
|
const argsPos = this.#blockPos.get(ev.toolCallId);
|
|
434
|
-
if (argsPos !== undefined && this.#
|
|
527
|
+
if (argsPos !== undefined && this.#isClosedTarget(argsPos.messageId)) { this.#resync = true; break; } // INV-MSG delta guard
|
|
435
528
|
const existing = this.#toolArgs.get(ev.toolCallId) ?? "";
|
|
436
529
|
this.#toolArgs.set(ev.toolCallId, existing + ev.delta);
|
|
437
530
|
break;
|
|
@@ -443,6 +536,9 @@ export class Reducer {
|
|
|
443
536
|
if (pos === undefined) break;
|
|
444
537
|
const msg = this.#messages.get(pos.messageId);
|
|
445
538
|
if (msg === undefined) break;
|
|
539
|
+
// INV-MSG (SPEC.md:745, draft.4 E5): a block-finalizing event into a sealed
|
|
540
|
+
// message, or into any message of a closed turn, parks like a delta.
|
|
541
|
+
if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG finalizer guard (E5)
|
|
446
542
|
const block = msg.content[pos.index];
|
|
447
543
|
if (block === undefined || block.type !== "tool-call") break;
|
|
448
544
|
block.input = ev.input;
|
|
@@ -455,7 +551,7 @@ export class Reducer {
|
|
|
455
551
|
if (ev.toolMetadata !== undefined) {
|
|
456
552
|
block.toolMetadata = ev.toolMetadata;
|
|
457
553
|
}
|
|
458
|
-
|
|
554
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
459
555
|
// Clear scratch now that it's been superseded by the authoritative input.
|
|
460
556
|
this.#toolArgs.delete(ev.toolCallId);
|
|
461
557
|
break;
|
|
@@ -463,13 +559,18 @@ export class Reducer {
|
|
|
463
559
|
|
|
464
560
|
// ── TOOL-RESULT blocks ────────────────────────────────────────────────────
|
|
465
561
|
case "tool.done": {
|
|
562
|
+
// rd-14 INV-BLOCK: a second FINAL (more-less) tool.done for one call parks.
|
|
563
|
+
if (ev.more !== true) {
|
|
564
|
+
if (this.#finalToolDone.has(ev.toolCallId)) { this.#resync = true; break; }
|
|
565
|
+
this.#finalToolDone.add(ev.toolCallId);
|
|
566
|
+
}
|
|
466
567
|
const resultKey = `result:${ev.toolCallId}`;
|
|
467
568
|
const existingPos = this.#blockPos.get(resultKey);
|
|
468
569
|
|
|
469
570
|
if (existingPos !== undefined) {
|
|
470
|
-
// MERGE path: a
|
|
471
|
-
// toolCallId
|
|
472
|
-
//
|
|
571
|
+
// MERGE path: a kept-open (more:true) tool-result block exists for this
|
|
572
|
+
// toolCallId; the incoming tool.done (final or next kept-open) folds onto it
|
|
573
|
+
// as a snapshot (draft.4 §5, below).
|
|
473
574
|
const msg = this.#messages.get(existingPos.messageId);
|
|
474
575
|
if (msg === undefined) break;
|
|
475
576
|
const block = msg.content[existingPos.index];
|
|
@@ -484,29 +585,38 @@ export class Reducer {
|
|
|
484
585
|
this.#resync = true;
|
|
485
586
|
break;
|
|
486
587
|
}
|
|
588
|
+
// Snapshot fold (draft.4 §5; fold/flush P1, bar wf_2231e194-e31): while a
|
|
589
|
+
// result is kept open, every tool.done carries the FULL current result, so
|
|
590
|
+
// a later one REPLACES the payload as a unit. content, outcome, isError,
|
|
591
|
+
// structuredContent, uiData, sideData, errorText, errorCode and
|
|
592
|
+
// pendingInput take the later event's values, and a field it omits is
|
|
593
|
+
// CLEARED: an ok final no longer keeps a kept-open error's
|
|
594
|
+
// isError/errorText, and an error final no longer keeps a kept-open
|
|
595
|
+
// structuredContent. uiData is payload (founder ruling, bar
|
|
596
|
+
// wf_93a30c7b-cd0): a carried `uiData: null` is STORED as a value ("no
|
|
597
|
+
// view data"), never a retraction; a view's identity that must outlive a
|
|
598
|
+
// snapshot lives on `_meta.ui.resourceUri`, a descriptor below.
|
|
487
599
|
block.content = ev.content;
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
//
|
|
600
|
+
replaceOrClear(block, "outcome", ev.outcome);
|
|
601
|
+
replaceOrClear(block, "isError", ev.isError);
|
|
602
|
+
replaceOrClear(block, "structuredContent", ev.structuredContent);
|
|
603
|
+
replaceOrClear(block, "uiData", ev.uiData);
|
|
604
|
+
replaceOrClear(block, "sideData", ev.sideData);
|
|
605
|
+
replaceOrClear(block, "errorText", ev.errorText);
|
|
606
|
+
replaceOrClear(block, "errorCode", ev.errorCode);
|
|
607
|
+
replaceOrClear(block, "pendingInput", ev.pendingInput);
|
|
608
|
+
// Descriptors are replaced only when the later event carries them:
|
|
609
|
+
// toolMetadata and dynamic (SPEC.md:799; as tool.args.assembled treats
|
|
610
|
+
// toolMetadata), and `_meta`, the §0.4 host side-channel, so a kept-open
|
|
611
|
+
// result's MCP-Apps/A2UI card bootstrap survives a final that omits its
|
|
612
|
+
// own (workspace#9). providerMetadata merges by key.
|
|
497
613
|
if (ev.toolMetadata !== undefined) block.toolMetadata = ev.toolMetadata;
|
|
498
614
|
if (ev.dynamic !== undefined) block.dynamic = ev.dynamic;
|
|
499
|
-
if (ev.pendingInput !== undefined) block.pendingInput = ev.pendingInput;
|
|
500
|
-
// workspace#9: the §0.4 host side-channel joins the merge like every
|
|
501
|
-
// other result field — guarded, so a preliminary's `_meta` (the
|
|
502
|
-
// MCP-Apps/A2UI card bootstrap) survives a final REPLACE that omits
|
|
503
|
-
// its own. Mirrors the text.start/reasoning.start carriage.
|
|
504
615
|
if (ev._meta !== undefined) block._meta = ev._meta;
|
|
505
|
-
|
|
616
|
+
setProviderMeta(block, ev.providerMetadata);
|
|
506
617
|
// Typed preliminary flag mirrors the block's `more` state (audit M20):
|
|
507
|
-
// more:true keeps it set (
|
|
508
|
-
//
|
|
509
|
-
// clears it, closing (removing from open tracking).
|
|
618
|
+
// more:true keeps it set (kept open); the final more-less tool.done
|
|
619
|
+
// clears it and closes the result (removed from open tracking).
|
|
510
620
|
if (ev.more) {
|
|
511
621
|
block.preliminary = true;
|
|
512
622
|
} else {
|
|
@@ -640,8 +750,17 @@ export class Reducer {
|
|
|
640
750
|
// ── TURN-RECORD events (R5) ───────────────────────────────────────────────
|
|
641
751
|
|
|
642
752
|
case "turn.done": {
|
|
643
|
-
|
|
753
|
+
// D9 (draft.4 §5.0 INV-OWNER): a terminal for a turn this fold never saw
|
|
754
|
+
// opened is not an unresolvable owner: it folds onto no turn record and
|
|
755
|
+
// does not park. A CORE client that ignores subagent.*, or a fold whose
|
|
756
|
+
// opener was dropped, sees exactly this; a producer's lone terminal fails
|
|
757
|
+
// the §10 producer turn-open leg instead of minting a stub record here.
|
|
758
|
+
if (!this.#seenOpened.has(ev.turnId)) break;
|
|
759
|
+
const turn = this.#seenTurnRecord(ev.turnId);
|
|
760
|
+
if (turn === undefined) break;
|
|
644
761
|
turn.finishReason = ev.finishReason;
|
|
762
|
+
// draft.4: the native finish value, verbatim, beside its mapped reason.
|
|
763
|
+
if (ev.finishReasonRaw !== undefined) turn.finishReasonRaw = ev.finishReasonRaw;
|
|
645
764
|
// Usage is recorded VERBATIM — NO de-cumulation (spec §8.4; normalizer duty).
|
|
646
765
|
if (ev.usage !== undefined) turn.usage = ev.usage;
|
|
647
766
|
if (ev.safety !== undefined) turn.safety = ev.safety;
|
|
@@ -668,7 +787,11 @@ export class Reducer {
|
|
|
668
787
|
|
|
669
788
|
case "turn.error": {
|
|
670
789
|
// Non-folding into content; sets outcome={type:"error",...} on the turn record.
|
|
671
|
-
|
|
790
|
+
// D9, as turn.done; only for a NAMED turnId (a turnId-less terminal keeps
|
|
791
|
+
// the INV-OWNER backfill below).
|
|
792
|
+
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
793
|
+
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.ensureTurn(ev.turnId);
|
|
794
|
+
if (turn === undefined) break;
|
|
672
795
|
turn.outcome = {
|
|
673
796
|
type: "error",
|
|
674
797
|
message: ev.message,
|
|
@@ -686,7 +809,11 @@ export class Reducer {
|
|
|
686
809
|
// Non-folding into content; sets a dedicated aborted outcome on the turn record
|
|
687
810
|
// (symmetric with turn.error). taskState is verbatim-A2A only and is NEVER
|
|
688
811
|
// reducer-invented (audit M29) — it stays whatever a prior turn.done left it as.
|
|
689
|
-
|
|
812
|
+
// D9, as turn.done; only for a NAMED turnId (a turnId-less terminal keeps
|
|
813
|
+
// the INV-OWNER backfill below).
|
|
814
|
+
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
815
|
+
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.ensureTurn(ev.turnId);
|
|
816
|
+
if (turn === undefined) break;
|
|
690
817
|
turn.outcome = { type: "aborted", ...(ev.reason !== undefined ? { reason: ev.reason } : {}) };
|
|
691
818
|
// INV-MSG binding window: no blocks attach to a closed turn's messages.
|
|
692
819
|
this.#closeTurnWindow(ev.turnId);
|
|
@@ -850,7 +977,7 @@ export class Reducer {
|
|
|
850
977
|
// Apply patch to #state using a tightened discriminator:
|
|
851
978
|
//
|
|
852
979
|
// Array.isArray(patch) → RFC-6902 via applyPatch (R6)
|
|
853
|
-
// typeof patch === "object" && != null →
|
|
980
|
+
// typeof patch === "object" && != null → key-replace: each top-level key set whole (draft.4, pkg-21)
|
|
854
981
|
// else (scalar / null) → explicit no-op
|
|
855
982
|
// (documented: a future third source may extend this discriminator;
|
|
856
983
|
// scalars are NOT an error and must NOT set #resync)
|
|
@@ -871,29 +998,26 @@ export class Reducer {
|
|
|
871
998
|
}
|
|
872
999
|
this.#state = result.value;
|
|
873
1000
|
} else if (typeof patch === "object" && patch !== null) {
|
|
874
|
-
//
|
|
875
|
-
//
|
|
876
|
-
//
|
|
877
|
-
//
|
|
1001
|
+
// Key-replace (draft.4 §5, pkg-21, founder-ruled): each top-level key
|
|
1002
|
+
// of the patch REPLACES #state[key] whole, which is how ADK applies
|
|
1003
|
+
// a stateDelta (sessions/state.js State.set). draft.3 merged one
|
|
1004
|
+
// level deep: a write that rewrote part of an object-valued key kept
|
|
1005
|
+
// the stale members (state-fold-gemini38: {cfg:{a:1,b:2}} then
|
|
1006
|
+
// {cfg:{a:5}} folded to {cfg:{a:5,b:2}} where ADK holds {cfg:{a:5}}),
|
|
1007
|
+
// and two partial writes to one key could assemble an object neither
|
|
1008
|
+
// write held (DC-6). null is stored as a value. #state starts as {}
|
|
1009
|
+
// when it is absent or not an object.
|
|
1010
|
+
// Keys are DEFINED, never assigned, so an own `__proto__` key of a
|
|
1011
|
+
// hand-built patch never selects the state's prototype.
|
|
878
1012
|
const base: { [k: string]: JsonValue } =
|
|
879
1013
|
this.#state !== undefined && typeof this.#state === "object" && !Array.isArray(this.#state)
|
|
880
1014
|
? { ...this.#state }
|
|
881
1015
|
: {};
|
|
882
|
-
for (const
|
|
883
|
-
const
|
|
884
|
-
if (
|
|
885
|
-
//
|
|
886
|
-
|
|
887
|
-
const existingObj: { [k: string]: JsonValue } =
|
|
888
|
-
typeof existing === "object" && existing !== null && !Array.isArray(existing)
|
|
889
|
-
? { ...(existing as { [k: string]: JsonValue }) }
|
|
890
|
-
: {};
|
|
891
|
-
base[nodeKey] = { ...existingObj, ...(nodeUpdate as { [k: string]: JsonValue }) };
|
|
892
|
-
} else {
|
|
893
|
-
// Non-object node value: replace the node entry wholesale.
|
|
894
|
-
if (nodeUpdate !== undefined) {
|
|
895
|
-
base[nodeKey] = nodeUpdate;
|
|
896
|
-
}
|
|
1016
|
+
for (const key of Object.keys(patch)) {
|
|
1017
|
+
const value = patch[key];
|
|
1018
|
+
if (value !== undefined) {
|
|
1019
|
+
// Copy-isolated from the event, as the capstone C1/C2 fixes require.
|
|
1020
|
+
Object.defineProperty(base, key, { value: structuredClone(value), enumerable: true, writable: true, configurable: true });
|
|
897
1021
|
}
|
|
898
1022
|
}
|
|
899
1023
|
this.#state = base;
|
|
@@ -1013,6 +1137,22 @@ export class Reducer {
|
|
|
1013
1137
|
this.#openedTurns = ev.turns !== undefined
|
|
1014
1138
|
? new Set(ev.turns.map((t) => t.turnId))
|
|
1015
1139
|
: new Set(this.#turns.keys());
|
|
1140
|
+
// D9 (bar wf_08cf78ac-c30): a snapshot that carries `turns` is
|
|
1141
|
+
// authoritative, so seen becomes exactly its turns plus its messages'
|
|
1142
|
+
// turns (a turn seen before it and absent from it is no longer seen);
|
|
1143
|
+
// one that omits `turns` keeps what was seen and adds its messages'
|
|
1144
|
+
// turns. Never rebuilt from #turns (a source/handoff/guardrail event
|
|
1145
|
+
// can have minted a stub record for a turn nobody opened). A
|
|
1146
|
+
// message-carried turn takes the thread of this snapshot's first
|
|
1147
|
+
// message with that turnId and a threadId, else none.
|
|
1148
|
+
if (ev.turns !== undefined) this.#seenOpened = new Map(ev.turns.map((t) => [t.turnId, t.threadId]));
|
|
1149
|
+
const snapshotTurnIds = new Set((ev.turns ?? []).map((t) => t.turnId));
|
|
1150
|
+
const messageThread = new Map<string, string | undefined>();
|
|
1151
|
+
for (const m of ev.messages) {
|
|
1152
|
+
if (m.turnId === undefined || snapshotTurnIds.has(m.turnId)) continue;
|
|
1153
|
+
if (messageThread.get(m.turnId) === undefined) messageThread.set(m.turnId, m.threadId);
|
|
1154
|
+
}
|
|
1155
|
+
for (const [turnId, threadId] of messageThread) this.#seenOpened.set(turnId, threadId);
|
|
1016
1156
|
|
|
1017
1157
|
// Un-park.
|
|
1018
1158
|
this.#resync = false;
|
|
@@ -1067,6 +1207,9 @@ export class Reducer {
|
|
|
1067
1207
|
// because it corrupts batch AND incremental identically). (final-review M1)
|
|
1068
1208
|
this.#toolArgs.delete(blockId);
|
|
1069
1209
|
this.#opaque.delete(blockId);
|
|
1210
|
+
// INV-BLOCK is about ids PRESENT in the fold (SPEC INV-BLOCK): a removed
|
|
1211
|
+
// block's id is no longer present, so a later *.start may reuse it.
|
|
1212
|
+
this.#invokeBlockIds.delete(blockId);
|
|
1070
1213
|
}
|
|
1071
1214
|
}
|
|
1072
1215
|
|
|
@@ -1164,6 +1307,23 @@ export class Reducer {
|
|
|
1164
1307
|
* If omitted and exactly one turn is open, return that turn's id.
|
|
1165
1308
|
* Otherwise return `undefined` (ambiguous / no turns open).
|
|
1166
1309
|
*/
|
|
1310
|
+
/** INV-MSG: true when the message is sealed or its turn has closed (rd-14 closed-turn half). */
|
|
1311
|
+
#isClosedTarget(messageId: string): boolean {
|
|
1312
|
+
if (this.#sealed.has(messageId)) return true;
|
|
1313
|
+
const turnId = this.#messages.get(messageId)?.turnId;
|
|
1314
|
+
return turnId !== undefined && this.#closedTurns.has(turnId);
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
/** rd-14 INV-BLOCK: a block-creating id seen twice in one invoke parks; else records it. */
|
|
1318
|
+
#dupBlock(id: string): boolean {
|
|
1319
|
+
if (this.#invokeBlockIds.has(id)) {
|
|
1320
|
+
this.#resync = true;
|
|
1321
|
+
return true;
|
|
1322
|
+
}
|
|
1323
|
+
this.#invokeBlockIds.add(id);
|
|
1324
|
+
return false;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1167
1327
|
#resolveTurnId(turnId: string | undefined): string | undefined {
|
|
1168
1328
|
if (turnId !== undefined) return turnId;
|
|
1169
1329
|
// Single-turn default: if exactly one turn exists, use it.
|
|
@@ -1190,6 +1350,24 @@ export class Reducer {
|
|
|
1190
1350
|
* AgTurnRecord requires `threadId`; when creating a defensive stub we use the
|
|
1191
1351
|
* turnId itself as a placeholder (no threadId is available without turn.start).
|
|
1192
1352
|
*/
|
|
1353
|
+
/**
|
|
1354
|
+
* D9: the record a terminal for a SEEN turn folds onto — the existing one,
|
|
1355
|
+
* else one created on the thread the fold saw the turn opened on. With no
|
|
1356
|
+
* thread known it returns undefined and the terminal folds onto no record.
|
|
1357
|
+
* No other turn's thread is borrowed (that could adopt a subagent's parent
|
|
1358
|
+
* label as a threadId), and a D9 path never writes a threadId equal to its
|
|
1359
|
+
* turnId.
|
|
1360
|
+
*/
|
|
1361
|
+
#seenTurnRecord(turnId: string): AgTurnRecord | undefined {
|
|
1362
|
+
const existing = this.#turns.get(turnId);
|
|
1363
|
+
if (existing !== undefined) return existing;
|
|
1364
|
+
const threadId = this.#seenOpened.get(turnId);
|
|
1365
|
+
if (threadId === undefined) return undefined;
|
|
1366
|
+
const record: AgTurnRecord = { turnId, threadId };
|
|
1367
|
+
this.#turns.set(turnId, record);
|
|
1368
|
+
return record;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1193
1371
|
ensureTurn(turnId: string | undefined): AgTurnRecord {
|
|
1194
1372
|
const resolved = this.#resolveTurnId(turnId) ?? turnId;
|
|
1195
1373
|
const key = resolved ?? "unknown-turn";
|