@silverprotocol/core 0.10.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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * input-check.ts — the reference AgInput receiver check (draft.4 §0.2,
3
- * workspace#20 decision 6: option 1 "omit the element, reject the input";
4
- * input classes per the founder's ALT-1 ruling, bar wf_a8a31902-fb5).
3
+ * §10 item 29: an input that fails is rejected whole, classed unknown-value,
4
+ * malformed or major-mismatch).
5
5
  *
6
6
  * A receiver passes unknown object fields through untouched, and REJECTS THE
7
7
  * WHOLE INPUT, before acting on any part of it, when the input fails this
@@ -11,8 +11,8 @@
11
11
  * - `major-mismatch`, at `["version"]`: `version` differs in MAJOR (§12);
12
12
  * - `unknown-value`: the value at the path is a STRING that the closed set
13
13
  * there does not define (an `AgInput.kind`, an `AgHitlAnswer.status`, a
14
- * reasoning `effort`, an `AgBlock` `type`, …). zod's codes alone cannot
15
- * tell this from malformed, so the rule reads the raw value (bar CB-6);
14
+ * reasoning `mode`, an `AgBlock` `type`, …). zod's codes alone cannot
15
+ * tell this from malformed, so the rule reads the raw value;
16
16
  * - `malformed`: every other failure — a missing value, a value of the wrong
17
17
  * JSON type, a value that breaks a stated constraint, a non-JSON input, or
18
18
  * a `protocol` other than `"agjson"` (it names the protocol; it is not a
package/src/record.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  /**
2
- * record.ts — lenient readers for STORED records (draft.4 §0.2, workspace#20
3
- * decision 6, founder ruling 2026-09-23: option 1, "omit the element, reject
4
- * the input").
2
+ * record.ts — lenient readers for STORED records (draft.4 §0.2: a stored
3
+ * record omits an unreadable element and reports it; an input is rejected).
5
4
  *
6
5
  * §0.2's consumer posture also binds a reader of a stored `AgMessage` or
7
6
  * `AgMemoryRecord` it did not receive inside an event. These readers
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) {
@@ -938,6 +938,7 @@ export class Reducer {
938
938
  }
939
939
  turn.promptBlocked = {
940
940
  reason: ev.reason,
941
+ ...(ev.reasonRaw !== undefined ? { reasonRaw: ev.reasonRaw } : {}),
941
942
  ...(ev.safety !== undefined ? { safety: ev.safety } : {}),
942
943
  };
943
944
  break;
@@ -1045,7 +1046,7 @@ export class Reducer {
1045
1046
  // Apply patch to #state using a tightened discriminator:
1046
1047
  //
1047
1048
  // Array.isArray(patch) → RFC-6902 via applyPatch (R6)
1048
- // 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)
1049
1050
  // else (scalar / null) → explicit no-op
1050
1051
  // (documented: a future third source may extend this discriminator;
1051
1052
  // scalars are NOT an error and must NOT set #resync)
@@ -1066,14 +1067,14 @@ export class Reducer {
1066
1067
  }
1067
1068
  this.#state = result.value;
1068
1069
  } else if (typeof patch === "object" && patch !== null) {
1069
- // Key-replace (draft.4 §5, pkg-21, founder-ruled): each top-level key
1070
+ // Key-replace (draft.4 §5): each top-level key
1070
1071
  // of the patch REPLACES #state[key] whole, which is how ADK applies
1071
1072
  // a stateDelta (sessions/state.js State.set). draft.3 merged one
1072
1073
  // level deep: a write that rewrote part of an object-valued key kept
1073
1074
  // the stale members (state-fold-gemini38: {cfg:{a:1,b:2}} then
1074
1075
  // {cfg:{a:5}} folded to {cfg:{a:5,b:2}} where ADK holds {cfg:{a:5}}),
1075
1076
  // and two partial writes to one key could assemble an object neither
1076
- // 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 {}
1077
1078
  // when it is absent or not an object.
1078
1079
  // Keys are DEFINED, never assigned, so an own `__proto__` key of a
1079
1080
  // hand-built patch never selects the state's prototype.
@@ -1109,6 +1110,7 @@ export class Reducer {
1109
1110
  ...(ev.reason !== undefined ? { reason: ev.reason } : {}),
1110
1111
  ...(ev.durable !== undefined ? { durable: ev.durable } : {}),
1111
1112
  ...(ev.turnId !== undefined ? { turnId: ev.turnId } : {}),
1113
+ ...(ev._meta !== undefined ? { _meta: ev._meta } : {}),
1112
1114
  };
1113
1115
  this.#memory.set(memKey, record);
1114
1116
  } else if (ev.patch !== undefined) {
@@ -1129,6 +1131,7 @@ export class Reducer {
1129
1131
  existing.value = result.value;
1130
1132
  if (ev.reason !== undefined) existing.reason = ev.reason;
1131
1133
  if (ev.durable !== undefined) existing.durable = ev.durable;
1134
+ if (ev._meta !== undefined) existing._meta = ev._meta;
1132
1135
  }
1133
1136
  break;
1134
1137
  }
@@ -1215,7 +1218,7 @@ export class Reducer {
1215
1218
  this.#openedTurns = ev.turns !== undefined
1216
1219
  ? new Set(ev.turns.map((t) => t.turnId))
1217
1220
  : new Set(this.#turns.keys());
1218
- // D9 (bar wf_08cf78ac-c30): a snapshot that carries `turns` is
1221
+ // draft.4: a snapshot that carries `turns` is
1219
1222
  // authoritative, so seen becomes exactly its turns plus its messages'
1220
1223
  // turns (a turn seen before it and absent from it is no longer seen);
1221
1224
  // one that omits `turns` keeps what was seen and adds its messages'
@@ -1231,7 +1234,7 @@ export class Reducer {
1231
1234
  if (messageThread.get(m.turnId) === undefined) messageThread.set(m.turnId, m.threadId);
1232
1235
  }
1233
1236
  for (const [turnId, threadId] of messageThread) this.#seenOpened.set(turnId, threadId);
1234
- // 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
1235
1238
  // it. #openedTurns was seeded above from records with a known thread
1236
1239
  // only, so an adopted record is not an adoption target by this snapshot.
1237
1240
  for (const turnId of [...this.#held.keys()]) {
@@ -1395,14 +1398,14 @@ export class Reducer {
1395
1398
  * If omitted and exactly one turn is open, return that turn's id.
1396
1399
  * Otherwise return `undefined` (ambiguous / no turns open).
1397
1400
  */
1398
- /** 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). */
1399
1402
  #isClosedTarget(messageId: string): boolean {
1400
1403
  if (this.#sealed.has(messageId)) return true;
1401
1404
  return this.#isClosedTurn(this.#messages.get(messageId)?.turnId);
1402
1405
  }
1403
1406
 
1404
1407
  /**
1405
- * 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
1406
1409
  * while the fold holds a turn record for it that carries an `outcome`. Only a
1407
1410
  * folded turn.done / turn.error / turn.abort sets one, so a terminal that
1408
1411
  * folds onto no record closes nothing; a messages.snapshot changes closure
@@ -1439,7 +1442,7 @@ export class Reducer {
1439
1442
  return true;
1440
1443
  }
1441
1444
 
1442
- /** 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. */
1443
1446
  #dupBlock(id: string): boolean {
1444
1447
  if (this.#invokeBlockIds.has(id)) {
1445
1448
  this.#resync = true;
@@ -1451,7 +1454,7 @@ export class Reducer {
1451
1454
 
1452
1455
  #resolveTurnId(turnId: string | undefined): string | undefined {
1453
1456
  if (turnId !== undefined) return turnId;
1454
- // §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
1455
1458
  // resolves to the sole OPEN turn, never to a closed one (through draft.4
1456
1459
  // this counted records, so a lone closed turn or a stub could be picked).
1457
1460
  return this.#soleOpenRecord()?.turnId;
@@ -1469,7 +1472,7 @@ export class Reducer {
1469
1472
  }
1470
1473
 
1471
1474
  /**
1472
- * 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
1473
1476
  * record lands on it; one whose thread is known gets a new record on that
1474
1477
  * thread; one whose thread is not known yet is held (a record result() omits),
1475
1478
  * or, past the implementation bound, parks the fold. A turnId-less record
@@ -1502,7 +1505,7 @@ export class Reducer {
1502
1505
  }
1503
1506
 
1504
1507
  /**
1505
- * 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
1506
1509
  * now known, with `parentTurnId` when a subagent.start opened it. Returns the
1507
1510
  * adopted record, or undefined when nothing was held for the turn.
1508
1511
  */