@silverprotocol/core 0.11.0 → 0.12.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/src/reduce.ts CHANGED
@@ -52,7 +52,7 @@ function mergeProviderMeta(
52
52
  * result. A bare `block.providerMetadata = mergeProviderMeta(...)` left an
53
53
  * explicit `providerMetadata: undefined` key on blocks that never had any:
54
54
  * invisible in JSON, but visible to `in` / `toHaveProperty` / Object.keys
55
- * consumers (sp-openai's PH-2 finding, 2026-09-23).
55
+ * consumers (found 2026-09-23).
56
56
  */
57
57
  function setProviderMeta(block: { providerMetadata?: AgProviderMeta }, incoming: AgProviderMeta | undefined): void {
58
58
  const merged = mergeProviderMeta(block.providerMetadata, incoming);
@@ -99,7 +99,7 @@ function isClosedEvent(ev: AgEvent): ev is AgClosedEventType {
99
99
  }
100
100
 
101
101
  /**
102
- * Implementation bound on held landings (CB-7; cto's note, not normative). No
102
+ * Implementation bound on held landings (not normative). No
103
103
  * reference producer emits a record event before its turn's opener, so a
104
104
  * conformant stream holds nothing; the bound only stops a hostile or broken
105
105
  * stream from growing state result() never shows. Past either cap the landing
@@ -132,7 +132,7 @@ export class Reducer {
132
132
  // knows by name, so the tool.done adoption CREATE path degrades loudly
133
133
  // (resync) instead of creating a message for a turn that was never opened.
134
134
  #openedTurns: Set<string> = new Set();
135
- // D9 (draft.4 §5.0 INV-OWNER; bar wf_08cf78ac-c30, A tightened): every
135
+ // draft.4 §5.0 INV-OWNER: every
136
136
  // turnId the fold has seen opened — by turn.start, by subagent.start, or by a
137
137
  // folded messages.snapshot (its `turns`, or a message's turnId) — mapped to
138
138
  // the threadId it was opened on (undefined when the opener named none). A
@@ -142,7 +142,7 @@ export class Reducer {
142
142
  // #openedTurns (tool.done adoption) and never rebuilt from #turns, whose
143
143
  // records non-terminal arms may have minted as stubs.
144
144
  #seenOpened: Map<string, string | undefined> = new Map();
145
- // CB-7 (draft.5 §5.0 INV-OWNER, "same-turn thread, hold"): the landings of
145
+ // draft.5 §5.0 INV-OWNER (a held record lands on its own turn's thread): the landings of
146
146
  // the six record events (source, handoff, prompt.blocked, guardrail.result,
147
147
  // display.required, agent.capabilities) for a turn whose thread the fold does
148
148
  // not know yet. Each is a turn record result() never shows; it moves into
@@ -156,7 +156,7 @@ export class Reducer {
156
156
  // takes a turn.done{paused}; every terminal that folds removes its id. A
157
157
  // messages.snapshot folds no terminal, so the set survives it (a refresh
158
158
  // split by a reconnect still folds); whether the turn is closed paused is
159
- // read from the records the snapshot leaves (CB-8).
159
+ // read from the records the snapshot leaves.
160
160
  #restartedSinceClose: Set<string> = new Set();
161
161
  // block/tool-call id → position in its owning message's content[], for REPLACE.
162
162
  #blockPos: Map<string, { messageId: string; index: number }> = new Map();
@@ -171,7 +171,7 @@ export class Reducer {
171
171
  #lastSeq: number = -1;
172
172
  #resync: boolean = false;
173
173
 
174
- // ── rd-14 INV-BLOCK (invoke-scoped) ─────────────────────────────────────────
174
+ // ── INV-BLOCK (invoke-scoped) ─────────────────────────────────────────
175
175
  // Block ids created in the current invoke, and toolCallIds whose FINAL
176
176
  // (more-less) tool.done already landed. Both reset at the seq-0 restart, so a
177
177
  // new invoke may reuse per-invoke ids.
@@ -199,7 +199,7 @@ export class Reducer {
199
199
  if (this.#lastSeq >= 0 && ev.seq > this.#lastSeq + 1) {
200
200
  this.#resync = true;
201
201
  }
202
- // rd-14 (founder: "Stall it, like a gap"): a repeated or backward seq ABOVE 0
202
+ // INV-SEQ (draft.4): a repeated or backward seq ABOVE 0
203
203
  // parks exactly like a forward gap. Only seq 0 (a new invoke's restart) may go
204
204
  // backward (SPEC INV-SEQ), so a re-delivery can never fold twice.
205
205
  if (this.#lastSeq >= 0 && ev.seq > 0 && ev.seq <= this.#lastSeq) {
@@ -257,7 +257,7 @@ export class Reducer {
257
257
  this.#openedTurns.add(ev.turnId);
258
258
  this.#seenOpened.set(ev.turnId, ev.threadId);
259
259
  // Idempotent: if the turn already exists, merge defined fields only. A
260
- // record held for this turn (CB-7) is adopted onto this thread first.
260
+ // record held for this turn is adopted onto this thread first.
261
261
  const existing = this.#turns.get(ev.turnId) ?? this.#adoptHeld(ev.turnId, ev.threadId);
262
262
  if (existing === undefined) {
263
263
  this.#turns.set(ev.turnId, {
@@ -278,9 +278,9 @@ export class Reducer {
278
278
 
279
279
  // ── MESSAGE lifecycle ──────────────────────────────────────────────────
280
280
  case "message.start": {
281
- // rd-14 INV-MSG: a message.start for a turn that already closed parks.
281
+ // INV-MSG: a message.start for a turn that already closed parks.
282
282
  if (this.#isClosedTurn(this.#resolveTurnId(ev.turnId) ?? ev.turnId)) { this.#resync = true; break; }
283
- // CB-7 (draft.5 INV-OWNER): a live message.start makes its turn's thread
283
+ // draft.5 INV-OWNER: a live message.start makes its turn's thread
284
284
  // known and joins D9's seen-opened set (a turn.start's thread wins; else
285
285
  // the first one folded), and a record held for the turn is adopted onto it.
286
286
  if (this.#seenOpened.get(ev.turnId) === undefined) this.#seenOpened.set(ev.turnId, ev.threadId);
@@ -349,12 +349,12 @@ export class Reducer {
349
349
  // adopting the unresolvable label as this subagent's own threadId.
350
350
  // A "real" turn is identified by threadId !== turnId: through draft.4
351
351
  // a defensive stub used its own turnId as a placeholder thread. draft.5
352
- // retires the stubs (CB-7), and the check stays as a guard.
352
+ // retires the stubs, and the check stays as a guard.
353
353
  // `ev.parentTurnId` remains the last resort when no real turn exists yet.
354
354
  const realTurn = [...this.#turns.values()].find((t) => t.threadId !== t.turnId);
355
355
  threadId = realTurn?.threadId ?? ev.parentTurnId;
356
356
  }
357
- // A record held for this nested turn (CB-7) takes the thread and the
357
+ // A record held for this nested turn takes the thread and the
358
358
  // parentTurnId here; otherwise a new record opens.
359
359
  if (this.#adoptHeld(ev.turnId, threadId, ev.parentTurnId) === undefined) {
360
360
  this.#turns.set(ev.turnId, {
@@ -387,7 +387,7 @@ export class Reducer {
387
387
 
388
388
  // ── TEXT blocks ───────────────────────────────────────────────────────────
389
389
  case "text.start": {
390
- if (this.#dupBlock(ev.id)) break; // rd-14 INV-BLOCK
390
+ if (this.#dupBlock(ev.id)) break; // INV-BLOCK
391
391
  const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
392
392
  if (msg === undefined) { this.#resync = true; break; }
393
393
  const block: AgBlock = {
@@ -409,7 +409,7 @@ export class Reducer {
409
409
  const msg = this.#messages.get(pos.messageId);
410
410
  if (msg === undefined) break;
411
411
  // INV-MSG (SPEC.md:745): a straggler delta into a sealed message, or into
412
- // any message of a closed turn (rd-14), parks.
412
+ // any message of a closed turn, parks.
413
413
  if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG delta guard
414
414
  const block = msg.content[pos.index];
415
415
  if (block === undefined || block.type !== "text") break;
@@ -441,7 +441,7 @@ export class Reducer {
441
441
 
442
442
  // ── REASONING blocks ──────────────────────────────────────────────────────
443
443
  case "reasoning.start": {
444
- if (this.#dupBlock(ev.id)) break; // rd-14 INV-BLOCK
444
+ if (this.#dupBlock(ev.id)) break; // INV-BLOCK
445
445
  const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
446
446
  if (msg === undefined) { this.#resync = true; break; }
447
447
  const block: AgBlock = {
@@ -464,7 +464,7 @@ export class Reducer {
464
464
  const msg = this.#messages.get(pos.messageId);
465
465
  if (msg === undefined) break;
466
466
  // INV-MSG (SPEC.md:745): a straggler delta into a sealed message, or into
467
- // any message of a closed turn (rd-14), parks.
467
+ // any message of a closed turn, parks.
468
468
  if (this.#isClosedTarget(msg.id)) { this.#resync = true; break; } // INV-MSG delta guard
469
469
  const block = msg.content[pos.index];
470
470
  if (block === undefined || block.type !== "reasoning") break;
@@ -533,7 +533,7 @@ export class Reducer {
533
533
 
534
534
  // ── TOOL-CALL blocks ──────────────────────────────────────────────────────
535
535
  case "tool.start": {
536
- if (this.#dupBlock(ev.toolCallId)) break; // rd-14 INV-BLOCK
536
+ if (this.#dupBlock(ev.toolCallId)) break; // INV-BLOCK
537
537
  const msg = this.openMessage(ev.turnId, ev.candidateIndex ?? 0);
538
538
  if (msg === undefined) { this.#resync = true; break; }
539
539
  const block: AgBlock = {
@@ -595,7 +595,7 @@ export class Reducer {
595
595
 
596
596
  // ── TOOL-RESULT blocks ────────────────────────────────────────────────────
597
597
  case "tool.done": {
598
- // rd-14 INV-BLOCK: a second FINAL (more-less) tool.done for one call parks.
598
+ // INV-BLOCK: a second FINAL (more-less) tool.done for one call parks.
599
599
  if (ev.more !== true) {
600
600
  if (this.#finalToolDone.has(ev.toolCallId)) { this.#resync = true; break; }
601
601
  this.#finalToolDone.add(ev.toolCallId);
@@ -621,15 +621,15 @@ export class Reducer {
621
621
  this.#resync = true;
622
622
  break;
623
623
  }
624
- // Snapshot fold (draft.4 §5; fold/flush P1, bar wf_2231e194-e31): while a
624
+ // Snapshot fold (draft.4 §5): while a
625
625
  // result is kept open, every tool.done carries the FULL current result, so
626
626
  // a later one REPLACES the payload as a unit. content, outcome, isError,
627
627
  // structuredContent, uiData, sideData, errorText, errorCode and
628
628
  // pendingInput take the later event's values, and a field it omits is
629
629
  // CLEARED: an ok final no longer keeps a kept-open error's
630
630
  // isError/errorText, and an error final no longer keeps a kept-open
631
- // structuredContent. uiData is payload (founder ruling, bar
632
- // wf_93a30c7b-cd0): a carried `uiData: null` is STORED as a value ("no
631
+ // structuredContent. uiData is payload (draft.4):
632
+ // a carried `uiData: null` is STORED as a value ("no
633
633
  // view data"), never a retraction; a view's identity that must outlive a
634
634
  // snapshot lives on `_meta.ui.resourceUri`, a descriptor below.
635
635
  block.content = ev.content;
@@ -835,7 +835,7 @@ export class Reducer {
835
835
  // Non-folding into content; sets outcome={type:"error",...} on the turn record.
836
836
  // D9, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
837
837
  // the sole OPEN turn, never a closed one; with none or several open its
838
- // owner is unresolvable and the fold parks (INV-OWNER, draft.5 CB-7).
838
+ // owner is unresolvable and the fold parks (INV-OWNER, draft.5).
839
839
  if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
840
840
  const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.#soleOpenRecord();
841
841
  if (turn === undefined) {
@@ -867,7 +867,7 @@ export class Reducer {
867
867
  // reducer-invented (audit M29) — it stays whatever a prior turn.done left it as.
868
868
  // D9, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
869
869
  // the sole OPEN turn, never a closed one; with none or several open its
870
- // owner is unresolvable and the fold parks (INV-OWNER, draft.5 CB-7).
870
+ // owner is unresolvable and the fold parks (INV-OWNER, draft.5).
871
871
  if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
872
872
  const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.#soleOpenRecord();
873
873
  if (turn === undefined) {
@@ -1046,7 +1046,7 @@ export class Reducer {
1046
1046
  // Apply patch to #state using a tightened discriminator:
1047
1047
  //
1048
1048
  // Array.isArray(patch) → RFC-6902 via applyPatch (R6)
1049
- // typeof patch === "object" && != null → key-replace: each top-level key set whole (draft.4, pkg-21)
1049
+ // typeof patch === "object" && != null → key-replace: each top-level key set whole (draft.4)
1050
1050
  // else (scalar / null) → explicit no-op
1051
1051
  // (documented: a future third source may extend this discriminator;
1052
1052
  // scalars are NOT an error and must NOT set #resync)
@@ -1067,14 +1067,14 @@ export class Reducer {
1067
1067
  }
1068
1068
  this.#state = result.value;
1069
1069
  } else if (typeof patch === "object" && patch !== null) {
1070
- // Key-replace (draft.4 §5, pkg-21, founder-ruled): each top-level key
1070
+ // Key-replace (draft.4 §5): each top-level key
1071
1071
  // of the patch REPLACES #state[key] whole, which is how ADK applies
1072
1072
  // a stateDelta (sessions/state.js State.set). draft.3 merged one
1073
1073
  // level deep: a write that rewrote part of an object-valued key kept
1074
1074
  // the stale members (state-fold-gemini38: {cfg:{a:1,b:2}} then
1075
1075
  // {cfg:{a:5}} folded to {cfg:{a:5,b:2}} where ADK holds {cfg:{a:5}}),
1076
1076
  // and two partial writes to one key could assemble an object neither
1077
- // write held (DC-6). null is stored as a value. #state starts as {}
1077
+ // write held. null is stored as a value. #state starts as {}
1078
1078
  // when it is absent or not an object.
1079
1079
  // Keys are DEFINED, never assigned, so an own `__proto__` key of a
1080
1080
  // hand-built patch never selects the state's prototype.
@@ -1218,7 +1218,7 @@ export class Reducer {
1218
1218
  this.#openedTurns = ev.turns !== undefined
1219
1219
  ? new Set(ev.turns.map((t) => t.turnId))
1220
1220
  : new Set(this.#turns.keys());
1221
- // D9 (bar wf_08cf78ac-c30): a snapshot that carries `turns` is
1221
+ // draft.4: a snapshot that carries `turns` is
1222
1222
  // authoritative, so seen becomes exactly its turns plus its messages'
1223
1223
  // turns (a turn seen before it and absent from it is no longer seen);
1224
1224
  // one that omits `turns` keeps what was seen and adds its messages'
@@ -1234,7 +1234,7 @@ export class Reducer {
1234
1234
  if (messageThread.get(m.turnId) === undefined) messageThread.set(m.turnId, m.threadId);
1235
1235
  }
1236
1236
  for (const [turnId, threadId] of messageThread) this.#seenOpened.set(turnId, threadId);
1237
- // CB-7: a held record whose thread this snapshot taught is adopted onto
1237
+ // draft.5 INV-OWNER: a held record whose thread this snapshot taught is adopted onto
1238
1238
  // it. #openedTurns was seeded above from records with a known thread
1239
1239
  // only, so an adopted record is not an adoption target by this snapshot.
1240
1240
  for (const turnId of [...this.#held.keys()]) {
@@ -1398,14 +1398,14 @@ export class Reducer {
1398
1398
  * If omitted and exactly one turn is open, return that turn's id.
1399
1399
  * Otherwise return `undefined` (ambiguous / no turns open).
1400
1400
  */
1401
- /** INV-MSG: true when the message is sealed or its turn has closed (rd-14 closed-turn half). */
1401
+ /** INV-MSG: true when the message is sealed or its turn has closed (the closed-turn half). */
1402
1402
  #isClosedTarget(messageId: string): boolean {
1403
1403
  if (this.#sealed.has(messageId)) return true;
1404
1404
  return this.#isClosedTurn(this.#messages.get(messageId)?.turnId);
1405
1405
  }
1406
1406
 
1407
1407
  /**
1408
- * INV-MSG (draft.5, CB-8 A′: closure follows the records): a turn is closed
1408
+ * INV-MSG (draft.5: closure follows the records): a turn is closed
1409
1409
  * while the fold holds a turn record for it that carries an `outcome`. Only a
1410
1410
  * folded turn.done / turn.error / turn.abort sets one, so a terminal that
1411
1411
  * folds onto no record closes nothing; a messages.snapshot changes closure
@@ -1442,7 +1442,7 @@ export class Reducer {
1442
1442
  return true;
1443
1443
  }
1444
1444
 
1445
- /** rd-14 INV-BLOCK: a block-creating id seen twice in one invoke parks; else records it. */
1445
+ /** INV-BLOCK: a block-creating id seen twice in one invoke parks; else records it. */
1446
1446
  #dupBlock(id: string): boolean {
1447
1447
  if (this.#invokeBlockIds.has(id)) {
1448
1448
  this.#resync = true;
@@ -1454,7 +1454,7 @@ export class Reducer {
1454
1454
 
1455
1455
  #resolveTurnId(turnId: string | undefined): string | undefined {
1456
1456
  if (turnId !== undefined) return turnId;
1457
- // §4 per-turn routing / INV-OWNER (draft.5, CB-7): an omitted turnId
1457
+ // §4 per-turn routing / INV-OWNER (draft.5): an omitted turnId
1458
1458
  // resolves to the sole OPEN turn, never to a closed one (through draft.4
1459
1459
  // this counted records, so a lone closed turn or a stub could be picked).
1460
1460
  return this.#soleOpenRecord()?.turnId;
@@ -1472,7 +1472,7 @@ export class Reducer {
1472
1472
  }
1473
1473
 
1474
1474
  /**
1475
- * Where a record event lands (CB-7, draft.5 INV-OWNER). A named turn with a
1475
+ * Where a record event lands (draft.5 INV-OWNER). A named turn with a
1476
1476
  * record lands on it; one whose thread is known gets a new record on that
1477
1477
  * thread; one whose thread is not known yet is held (a record result() omits),
1478
1478
  * or, past the implementation bound, parks the fold. A turnId-less record
@@ -1505,7 +1505,7 @@ export class Reducer {
1505
1505
  }
1506
1506
 
1507
1507
  /**
1508
- * Move the record held for `turnId` (CB-7) into #turns on the thread that is
1508
+ * Move the record held for `turnId` into #turns on the thread that is
1509
1509
  * now known, with `parentTurnId` when a subagent.start opened it. Returns the
1510
1510
  * adopted record, or undefined when nothing was held for the turn.
1511
1511
  */