@silverprotocol/core 0.7.1 → 0.8.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 +20 -2
- package/dist/agjson.d.ts +39 -1
- package/dist/agjson.d.ts.map +1 -1
- package/dist/agjson.js +23 -3
- package/dist/agjson.js.map +1 -1
- 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/persist.d.ts +18 -0
- package/dist/persist.d.ts.map +1 -0
- package/dist/persist.js +12 -0
- package/dist/persist.js.map +1 -0
- package/dist/reduce.d.ts +1 -2
- package/dist/reduce.d.ts.map +1 -1
- package/dist/reduce.js +265 -88
- package/dist/reduce.js.map +1 -1
- package/dist/stream-assembler.d.ts +1 -0
- package/dist/stream-assembler.d.ts.map +1 -1
- package/dist/stream-assembler.js +24 -3
- package/dist/stream-assembler.js.map +1 -1
- package/package.json +1 -1
- package/src/agjson.ts +37 -4
- package/src/index.ts +1 -0
- package/src/persist.ts +22 -0
- package/src/reduce.ts +251 -88
- package/src/stream-assembler.ts +25 -3
package/src/reduce.ts
CHANGED
|
@@ -98,6 +98,16 @@ function isClosedEvent(ev: AgEvent): ev is AgClosedEventType {
|
|
|
98
98
|
return !ev.type.startsWith("ext.");
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Implementation bound on held landings (CB-7; cto's note, not normative). No
|
|
103
|
+
* reference producer emits a record event before its turn's opener, so a
|
|
104
|
+
* conformant stream holds nothing; the bound only stops a hostile or broken
|
|
105
|
+
* stream from growing state result() never shows. Past either cap the landing
|
|
106
|
+
* is not held and the fold parks (needsResync): the loud degradation.
|
|
107
|
+
*/
|
|
108
|
+
const HELD_TURNS_CAP = 64;
|
|
109
|
+
const HELD_LANDINGS_CAP = 1024;
|
|
110
|
+
|
|
101
111
|
export class Reducer {
|
|
102
112
|
// ── keyed accumulators ──────────────────────────────────────────────────────
|
|
103
113
|
// Messages, keyed by message id (insertion-ordered).
|
|
@@ -116,16 +126,11 @@ export class Reducer {
|
|
|
116
126
|
#openMsg: Map<string, string> = new Map();
|
|
117
127
|
// Message ids sealed by message.end.
|
|
118
128
|
#sealed: Set<string> = new Set();
|
|
119
|
-
// Resolved turnIds closed by turn.done/turn.error/turn.abort (INV-MSG: no
|
|
120
|
-
// message of a closed turn — sealed or not, existing or not-yet-created —
|
|
121
|
-
// is ever a valid attach/adoption target).
|
|
122
|
-
#closedTurns: Set<string> = new Set();
|
|
123
129
|
// turnIds legitimately opened via turn.start / subagent.start (or restored
|
|
124
130
|
// by a messages.snapshot's turns?) — Task 8c leg 3 (guuey capstone finding
|
|
125
|
-
// B). Distinguishes "a real turn genuinely exists" from
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
// turn for a key that was never actually opened.
|
|
131
|
+
// B). Distinguishes "a real turn genuinely exists" from a turn the fold only
|
|
132
|
+
// knows by name, so the tool.done adoption CREATE path degrades loudly
|
|
133
|
+
// (resync) instead of creating a message for a turn that was never opened.
|
|
129
134
|
#openedTurns: Set<string> = new Set();
|
|
130
135
|
// D9 (draft.4 §5.0 INV-OWNER; bar wf_08cf78ac-c30, A tightened): every
|
|
131
136
|
// turnId the fold has seen opened — by turn.start, by subagent.start, or by a
|
|
@@ -137,6 +142,22 @@ export class Reducer {
|
|
|
137
142
|
// #openedTurns (tool.done adoption) and never rebuilt from #turns, whose
|
|
138
143
|
// records non-terminal arms may have minted as stubs.
|
|
139
144
|
#seenOpened: Map<string, string | undefined> = new Map();
|
|
145
|
+
// CB-7 (draft.5 §5.0 INV-OWNER, "same-turn thread, hold"): the landings of
|
|
146
|
+
// the six record events (source, handoff, prompt.blocked, guardrail.result,
|
|
147
|
+
// display.required, agent.capabilities) for a turn whose thread the fold does
|
|
148
|
+
// not know yet. Each is a turn record result() never shows; it moves into
|
|
149
|
+
// #turns on the turn's thread once a turn.start, subagent.start, message.start
|
|
150
|
+
// or messages.snapshot makes that thread known (#adoptHeld). `landings` counts
|
|
151
|
+
// the events held on it, for the implementation bound below.
|
|
152
|
+
#held: Map<string, { record: AgTurnRecord; landings: number }> = new Map();
|
|
153
|
+
#heldLandings = 0;
|
|
154
|
+
// draft.5 §5.0 INV-MSG (the paused refresh): closed turns a turn.start has
|
|
155
|
+
// named since their last terminal folded. Only such a turn, closed paused,
|
|
156
|
+
// takes a turn.done{paused}; every terminal that folds removes its id. A
|
|
157
|
+
// messages.snapshot folds no terminal, so the set survives it (a refresh
|
|
158
|
+
// split by a reconnect still folds); whether the turn is closed paused is
|
|
159
|
+
// read from the records the snapshot leaves (CB-8).
|
|
160
|
+
#restartedSinceClose: Set<string> = new Set();
|
|
140
161
|
// block/tool-call id → position in its owning message's content[], for REPLACE.
|
|
141
162
|
#blockPos: Map<string, { messageId: string; index: number }> = new Map();
|
|
142
163
|
|
|
@@ -230,11 +251,14 @@ export class Reducer {
|
|
|
230
251
|
switch (ev.type) {
|
|
231
252
|
// ── TURN lifecycle ─────────────────────────────────────────────────────
|
|
232
253
|
case "turn.start": {
|
|
254
|
+
// draft.5 INV-MSG: a turn.start naming a closed turn opens its paused refresh.
|
|
255
|
+
if (this.#isClosedTurn(ev.turnId)) this.#restartedSinceClose.add(ev.turnId);
|
|
233
256
|
// Task 8c leg 3: a turn.start always counts as a legitimately opened turn.
|
|
234
257
|
this.#openedTurns.add(ev.turnId);
|
|
235
258
|
this.#seenOpened.set(ev.turnId, ev.threadId);
|
|
236
|
-
// Idempotent: if the turn already exists, merge defined fields only.
|
|
237
|
-
|
|
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.
|
|
261
|
+
const existing = this.#turns.get(ev.turnId) ?? this.#adoptHeld(ev.turnId, ev.threadId);
|
|
238
262
|
if (existing === undefined) {
|
|
239
263
|
this.#turns.set(ev.turnId, {
|
|
240
264
|
turnId: ev.turnId,
|
|
@@ -242,7 +266,9 @@ export class Reducer {
|
|
|
242
266
|
...(ev.trigger !== undefined ? { trigger: ev.trigger } : {}),
|
|
243
267
|
});
|
|
244
268
|
} else {
|
|
245
|
-
// Merge —
|
|
269
|
+
// Merge (INV-TURN) — a turn.start gives the thread; other fields only
|
|
270
|
+
// overwrite with defined values (idempotent re-delivery).
|
|
271
|
+
existing.threadId = ev.threadId;
|
|
246
272
|
if (ev.trigger !== undefined) {
|
|
247
273
|
existing.trigger = ev.trigger;
|
|
248
274
|
}
|
|
@@ -253,7 +279,13 @@ export class Reducer {
|
|
|
253
279
|
// ── MESSAGE lifecycle ──────────────────────────────────────────────────
|
|
254
280
|
case "message.start": {
|
|
255
281
|
// rd-14 INV-MSG: a message.start for a turn that already closed parks.
|
|
256
|
-
if (this.#
|
|
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
|
|
284
|
+
// known and joins D9's seen-opened set (a turn.start's thread wins; else
|
|
285
|
+
// the first one folded), and a record held for the turn is adopted onto it.
|
|
286
|
+
if (this.#seenOpened.get(ev.turnId) === undefined) this.#seenOpened.set(ev.turnId, ev.threadId);
|
|
287
|
+
const knownThread = this.#seenOpened.get(ev.turnId);
|
|
288
|
+
if (knownThread !== undefined) this.#adoptHeld(ev.turnId, knownThread);
|
|
257
289
|
const msg: AgMessage = {
|
|
258
290
|
id: ev.id,
|
|
259
291
|
role: ev.role,
|
|
@@ -315,18 +347,22 @@ export class Reducer {
|
|
|
315
347
|
// Every entity in a fold shares one root threadId (SPEC §1.2), so
|
|
316
348
|
// fall back to any ALREADY-OPENED real turn's threadId instead of
|
|
317
349
|
// adopting the unresolvable label as this subagent's own threadId.
|
|
318
|
-
// A "real" turn is identified by threadId !== turnId
|
|
319
|
-
//
|
|
320
|
-
//
|
|
321
|
-
// resort when no real turn exists yet.
|
|
350
|
+
// A "real" turn is identified by threadId !== turnId: through draft.4
|
|
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.
|
|
353
|
+
// `ev.parentTurnId` remains the last resort when no real turn exists yet.
|
|
322
354
|
const realTurn = [...this.#turns.values()].find((t) => t.threadId !== t.turnId);
|
|
323
355
|
threadId = realTurn?.threadId ?? ev.parentTurnId;
|
|
324
356
|
}
|
|
325
|
-
this
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
357
|
+
// A record held for this nested turn (CB-7) takes the thread and the
|
|
358
|
+
// parentTurnId here; otherwise a new record opens.
|
|
359
|
+
if (this.#adoptHeld(ev.turnId, threadId, ev.parentTurnId) === undefined) {
|
|
360
|
+
this.#turns.set(ev.turnId, {
|
|
361
|
+
turnId: ev.turnId,
|
|
362
|
+
parentTurnId: ev.parentTurnId,
|
|
363
|
+
threadId,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
330
366
|
this.#seenOpened.set(ev.turnId, threadId);
|
|
331
367
|
break;
|
|
332
368
|
}
|
|
@@ -581,7 +617,7 @@ export class Reducer {
|
|
|
581
617
|
// #blockPos → messageId → message.turnId chain and park instead of mutating
|
|
582
618
|
// (mirrors the adoption path's closed-turn/sealed guard above).
|
|
583
619
|
const ownerTurnKey = msg.turnId ?? "unknown-turn";
|
|
584
|
-
if (this.#sealed.has(msg.id) || this.#
|
|
620
|
+
if (this.#sealed.has(msg.id) || this.#isClosedTurn(ownerTurnKey)) {
|
|
585
621
|
this.#resync = true;
|
|
586
622
|
break;
|
|
587
623
|
}
|
|
@@ -633,10 +669,11 @@ export class Reducer {
|
|
|
633
669
|
// INV-MSG enforcement: a sealed message, or ANY message of a closed
|
|
634
670
|
// turn — including one that would need to be freshly created — is
|
|
635
671
|
// never a valid adoption target. Resolve the turn this adoption
|
|
636
|
-
// would land in (the existing message's own turnId, or the
|
|
637
|
-
//
|
|
638
|
-
//
|
|
639
|
-
//
|
|
672
|
+
// would land in (the existing message's own turnId, or the turn a
|
|
673
|
+
// new message would take) BEFORE either sub-path runs, and park
|
|
674
|
+
// instead of silently attaching/creating past the seal or the
|
|
675
|
+
// turn's binding window. "unknown-turn" is only a key that names no
|
|
676
|
+
// record, so an unresolvable owner parks below.
|
|
640
677
|
const targetTurnKey =
|
|
641
678
|
existing !== undefined
|
|
642
679
|
? (existing.turnId ?? "unknown-turn")
|
|
@@ -644,11 +681,10 @@ export class Reducer {
|
|
|
644
681
|
// Task 8c leg 3 (guuey capstone finding B): a turn that was never
|
|
645
682
|
// legitimately opened (turn.start / subagent.start / a snapshot's
|
|
646
683
|
// turns?) is just as invalid an adoption target as a sealed
|
|
647
|
-
// message or a closed turn
|
|
648
|
-
// would happily fabricate a phantom turn stub instead of parking.
|
|
684
|
+
// message or a closed turn: park rather than create a message for it.
|
|
649
685
|
if (
|
|
650
686
|
this.#sealed.has(ev.messageId) ||
|
|
651
|
-
this.#
|
|
687
|
+
this.#isClosedTurn(targetTurnKey) ||
|
|
652
688
|
!this.#openedTurns.has(targetTurnKey)
|
|
653
689
|
) {
|
|
654
690
|
this.#resync = true;
|
|
@@ -657,7 +693,13 @@ export class Reducer {
|
|
|
657
693
|
if (existing !== undefined) {
|
|
658
694
|
msg = existing;
|
|
659
695
|
} else {
|
|
660
|
-
|
|
696
|
+
// An opened turn always has its record (turn.start / subagent.start /
|
|
697
|
+
// a snapshot's turns); a held record is never an adoption target.
|
|
698
|
+
const turn = this.#turns.get(targetTurnKey);
|
|
699
|
+
if (turn === undefined) {
|
|
700
|
+
this.#resync = true;
|
|
701
|
+
break;
|
|
702
|
+
}
|
|
661
703
|
// Create new tool-result message with the adopted messageId.
|
|
662
704
|
msg = {
|
|
663
705
|
id: ev.messageId,
|
|
@@ -758,6 +800,9 @@ export class Reducer {
|
|
|
758
800
|
if (!this.#seenOpened.has(ev.turnId)) break;
|
|
759
801
|
const turn = this.#seenTurnRecord(ev.turnId);
|
|
760
802
|
if (turn === undefined) break;
|
|
803
|
+
// draft.5 INV-MSG: a turn.done for a closed turn parks, writing nothing,
|
|
804
|
+
// unless it is the paused refresh.
|
|
805
|
+
if (!this.#terminalMayFold(turn, ev.outcome.type === "paused")) break;
|
|
761
806
|
turn.finishReason = ev.finishReason;
|
|
762
807
|
// draft.4: the native finish value, verbatim, beside its mapped reason.
|
|
763
808
|
if (ev.finishReasonRaw !== undefined) turn.finishReasonRaw = ev.finishReasonRaw;
|
|
@@ -770,8 +815,9 @@ export class Reducer {
|
|
|
770
815
|
if (ev.outcome.type === "paused") {
|
|
771
816
|
turn.asks = ev.outcome.asks;
|
|
772
817
|
}
|
|
773
|
-
// messageMetadata: REPLACE
|
|
774
|
-
// when present, else the open message of this turn
|
|
818
|
+
// messageMetadata: REPLACE the whole messageMetadata bag of the message named
|
|
819
|
+
// by turn.done.messageId when present, else of the open message of this turn
|
|
820
|
+
// (audit M20; §5 turn.done row, §13.10). No per-key merge.
|
|
775
821
|
// (Read happens BEFORE the binding window closes below.)
|
|
776
822
|
if (ev.messageMetadata !== undefined) {
|
|
777
823
|
const msg = ev.messageId !== undefined ? this.#messages.get(ev.messageId) : this.openMessage(ev.turnId, 0);
|
|
@@ -787,21 +833,31 @@ export class Reducer {
|
|
|
787
833
|
|
|
788
834
|
case "turn.error": {
|
|
789
835
|
// Non-folding into content; sets outcome={type:"error",...} on the turn record.
|
|
790
|
-
// D9, as turn.done
|
|
791
|
-
// the
|
|
836
|
+
// D9, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
|
|
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).
|
|
792
839
|
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
793
|
-
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this
|
|
794
|
-
if (turn === undefined)
|
|
840
|
+
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.#soleOpenRecord();
|
|
841
|
+
if (turn === undefined) {
|
|
842
|
+
if (ev.turnId === undefined) this.#resync = true;
|
|
843
|
+
break;
|
|
844
|
+
}
|
|
845
|
+
// draft.5 INV-MSG: a turn.error for a closed turn parks, writing nothing.
|
|
846
|
+
if (!this.#terminalMayFold(turn, false)) break;
|
|
847
|
+
// draft.5: `code` and `retriable` are recorded when turn.error carries them.
|
|
848
|
+
// `retriable` is the producer's judgement; an absent one stays absent
|
|
849
|
+
// (unstated, never coerced to false). A non-terminal `error` never folds it.
|
|
795
850
|
turn.outcome = {
|
|
796
851
|
type: "error",
|
|
797
852
|
message: ev.message,
|
|
798
853
|
...(ev.code !== undefined ? { code: ev.code } : {}),
|
|
854
|
+
...(ev.retriable !== undefined ? { retriable: ev.retriable } : {}),
|
|
799
855
|
};
|
|
800
856
|
// Usage is recorded VERBATIM — NO de-cumulation (mirrors turn.done; normalizer duty).
|
|
801
857
|
if (ev.usage !== undefined) turn.usage = ev.usage;
|
|
802
858
|
// INV-MSG binding window: no blocks attach to a closed turn's messages.
|
|
803
|
-
this.#closeTurnWindow(
|
|
804
|
-
this.#evictTurnScratch(
|
|
859
|
+
this.#closeTurnWindow(turn.turnId);
|
|
860
|
+
this.#evictTurnScratch(turn.turnId);
|
|
805
861
|
break;
|
|
806
862
|
}
|
|
807
863
|
|
|
@@ -809,15 +865,21 @@ export class Reducer {
|
|
|
809
865
|
// Non-folding into content; sets a dedicated aborted outcome on the turn record
|
|
810
866
|
// (symmetric with turn.error). taskState is verbatim-A2A only and is NEVER
|
|
811
867
|
// reducer-invented (audit M29) — it stays whatever a prior turn.done left it as.
|
|
812
|
-
// D9, as turn.done
|
|
813
|
-
// the
|
|
868
|
+
// D9, as turn.done, for a NAMED turnId. A turnId-less terminal resolves to
|
|
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).
|
|
814
871
|
if (ev.turnId !== undefined && !this.#seenOpened.has(ev.turnId)) break;
|
|
815
|
-
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this
|
|
816
|
-
if (turn === undefined)
|
|
872
|
+
const turn = ev.turnId !== undefined ? this.#seenTurnRecord(ev.turnId) : this.#soleOpenRecord();
|
|
873
|
+
if (turn === undefined) {
|
|
874
|
+
if (ev.turnId === undefined) this.#resync = true;
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
// draft.5 INV-MSG: a turn.abort for a closed turn parks, writing nothing.
|
|
878
|
+
if (!this.#terminalMayFold(turn, false)) break;
|
|
817
879
|
turn.outcome = { type: "aborted", ...(ev.reason !== undefined ? { reason: ev.reason } : {}) };
|
|
818
880
|
// INV-MSG binding window: no blocks attach to a closed turn's messages.
|
|
819
|
-
this.#closeTurnWindow(
|
|
820
|
-
this.#evictTurnScratch(
|
|
881
|
+
this.#closeTurnWindow(turn.turnId);
|
|
882
|
+
this.#evictTurnScratch(turn.turnId);
|
|
821
883
|
break;
|
|
822
884
|
}
|
|
823
885
|
|
|
@@ -825,7 +887,8 @@ export class Reducer {
|
|
|
825
887
|
// Append sourceId to the turn's sourceIds[] in order (preserve groundingChunks order),
|
|
826
888
|
// AND land the FULL record (payload/chunkIndex/providerMetadata) on sources[] so
|
|
827
889
|
// citations can resolve post-fold (audit M23) — sourceIds[] stays as the derived index.
|
|
828
|
-
const turn = this
|
|
890
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
891
|
+
if (turn === undefined) break;
|
|
829
892
|
if (turn.sourceIds === undefined) {
|
|
830
893
|
turn.sourceIds = [];
|
|
831
894
|
}
|
|
@@ -844,7 +907,8 @@ export class Reducer {
|
|
|
844
907
|
|
|
845
908
|
case "handoff": {
|
|
846
909
|
// Push an AgHandoffRecord onto the turn's handoffs[].
|
|
847
|
-
const turn = this
|
|
910
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
911
|
+
if (turn === undefined) break;
|
|
848
912
|
if (turn.handoffs === undefined) {
|
|
849
913
|
turn.handoffs = [];
|
|
850
914
|
}
|
|
@@ -863,7 +927,8 @@ export class Reducer {
|
|
|
863
927
|
// land the dedicated promptBlocked record {reason, safety?} so the REQUIRED reason
|
|
864
928
|
// and the blockedness itself survive the fold — a bare-reason block used to fold to
|
|
865
929
|
// ZERO trace (audit M28).
|
|
866
|
-
const turn = this
|
|
930
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
931
|
+
if (turn === undefined) break;
|
|
867
932
|
if (ev.safety !== undefined) {
|
|
868
933
|
if (turn.safety === undefined) {
|
|
869
934
|
turn.safety = [...ev.safety];
|
|
@@ -880,7 +945,8 @@ export class Reducer {
|
|
|
880
945
|
|
|
881
946
|
case "guardrail.result": {
|
|
882
947
|
// Push a guardrail evaluation record onto the turn's guardrails[].
|
|
883
|
-
const turn = this
|
|
948
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
949
|
+
if (turn === undefined) break;
|
|
884
950
|
if (turn.guardrails === undefined) {
|
|
885
951
|
turn.guardrails = [];
|
|
886
952
|
}
|
|
@@ -897,7 +963,8 @@ export class Reducer {
|
|
|
897
963
|
|
|
898
964
|
case "display.required": {
|
|
899
965
|
// MUST NOT drop (ToS). Push {provider, html} onto turn's displayRequired[].
|
|
900
|
-
const turn = this
|
|
966
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
967
|
+
if (turn === undefined) break;
|
|
901
968
|
if (turn.displayRequired === undefined) {
|
|
902
969
|
turn.displayRequired = [];
|
|
903
970
|
}
|
|
@@ -909,7 +976,8 @@ export class Reducer {
|
|
|
909
976
|
case "agent.capabilities": {
|
|
910
977
|
// SPEC §5: "Record the agent's AgCapabilities on the turn (first-turn negotiation)."
|
|
911
978
|
// Fold ev.capabilities onto the AgTurnRecord for the owning turn.
|
|
912
|
-
const turn = this
|
|
979
|
+
const turn = this.#landingRecord(ev.turnId);
|
|
980
|
+
if (turn === undefined) break;
|
|
913
981
|
turn.capabilities = ev.capabilities;
|
|
914
982
|
break;
|
|
915
983
|
}
|
|
@@ -1101,6 +1169,9 @@ export class Reducer {
|
|
|
1101
1169
|
// CONDITIONALLY replace #turns (only if turns? present in event).
|
|
1102
1170
|
if (ev.turns !== undefined) {
|
|
1103
1171
|
this.#turns = new Map(structuredClone(ev.turns).map((t) => [t.turnId, t]));
|
|
1172
|
+
// The snapshot's turns are authoritative: a landing held before it goes too.
|
|
1173
|
+
this.#held = new Map();
|
|
1174
|
+
this.#heldLandings = 0;
|
|
1104
1175
|
}
|
|
1105
1176
|
|
|
1106
1177
|
// CONDITIONALLY replace #artifacts (only if artifacts? present in event).
|
|
@@ -1123,17 +1194,24 @@ export class Reducer {
|
|
|
1123
1194
|
this.#memory = new Map([...nonThread, ...snapshotThread]);
|
|
1124
1195
|
}
|
|
1125
1196
|
|
|
1126
|
-
// Clear
|
|
1197
|
+
// Clear the transient scratch (draft.5 §5 messages.snapshot row): per-
|
|
1198
|
+
// toolCallId arg scratch, reasoning signature scratch, open-message
|
|
1199
|
+
// pointers, message seals and block positions; open/un-sealed blocks go
|
|
1200
|
+
// with the #messages REPLACE above. INV-BLOCK's invoke-scoped id sets are
|
|
1201
|
+
// NOT cleared (only a 0-restart resets them). Turn closure is not
|
|
1202
|
+
// scratch: it follows the turn records this arm leaves in the fold
|
|
1203
|
+
// (INV-MSG, #isClosedTurn), so carried turns bring their outcomes and a
|
|
1204
|
+
// turns-omitting snapshot keeps every record, and with it every closure.
|
|
1127
1205
|
this.#toolArgs = new Map();
|
|
1128
1206
|
this.#opaque = new Map();
|
|
1129
1207
|
this.#openMsg = new Map();
|
|
1130
1208
|
this.#sealed = new Set();
|
|
1131
|
-
this.#closedTurns = new Set();
|
|
1132
1209
|
this.#blockPos = new Map();
|
|
1210
|
+
// #restartedSinceClose is NOT cleared: a snapshot folds no terminal (draft.5 INV-MSG).
|
|
1133
1211
|
// Task 8c leg 3: a snapshot-restored turn counts as opened (guuey
|
|
1134
1212
|
// capstone finding B) — reseed #openedTurns from the replaced turns in
|
|
1135
|
-
// lockstep with #turns above;
|
|
1136
|
-
//
|
|
1213
|
+
// lockstep with #turns above; a turns-omitting snapshot re-seeds it from
|
|
1214
|
+
// the records it keeps.
|
|
1137
1215
|
this.#openedTurns = ev.turns !== undefined
|
|
1138
1216
|
? new Set(ev.turns.map((t) => t.turnId))
|
|
1139
1217
|
: new Set(this.#turns.keys());
|
|
@@ -1153,6 +1231,13 @@ export class Reducer {
|
|
|
1153
1231
|
if (messageThread.get(m.turnId) === undefined) messageThread.set(m.turnId, m.threadId);
|
|
1154
1232
|
}
|
|
1155
1233
|
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
|
|
1235
|
+
// it. #openedTurns was seeded above from records with a known thread
|
|
1236
|
+
// only, so an adopted record is not an adoption target by this snapshot.
|
|
1237
|
+
for (const turnId of [...this.#held.keys()]) {
|
|
1238
|
+
const threadId = this.#seenOpened.get(turnId);
|
|
1239
|
+
if (threadId !== undefined) this.#adoptHeld(turnId, threadId);
|
|
1240
|
+
}
|
|
1156
1241
|
|
|
1157
1242
|
// Un-park.
|
|
1158
1243
|
this.#resync = false;
|
|
@@ -1272,11 +1357,14 @@ export class Reducer {
|
|
|
1272
1357
|
return this.#messages.get(msgId);
|
|
1273
1358
|
}
|
|
1274
1359
|
|
|
1275
|
-
/**
|
|
1360
|
+
/**
|
|
1361
|
+
* Delete every open-message pointer belonging to a closed turn (INV-MSG).
|
|
1362
|
+
* Closure itself is the record's `outcome`, set by the terminal arm before
|
|
1363
|
+
* this runs (#isClosedTurn); this only evicts the turn's open-message pointers.
|
|
1364
|
+
*/
|
|
1276
1365
|
#closeTurnWindow(turnId: string | undefined): void {
|
|
1277
1366
|
const resolved = this.#resolveTurnId(turnId);
|
|
1278
1367
|
if (resolved === undefined) return;
|
|
1279
|
-
this.#closedTurns.add(resolved);
|
|
1280
1368
|
for (const [pk] of this.#openMsg) {
|
|
1281
1369
|
if (pk.startsWith(`${resolved} `)) this.#openMsg.delete(pk);
|
|
1282
1370
|
}
|
|
@@ -1310,8 +1398,45 @@ export class Reducer {
|
|
|
1310
1398
|
/** INV-MSG: true when the message is sealed or its turn has closed (rd-14 closed-turn half). */
|
|
1311
1399
|
#isClosedTarget(messageId: string): boolean {
|
|
1312
1400
|
if (this.#sealed.has(messageId)) return true;
|
|
1313
|
-
|
|
1314
|
-
|
|
1401
|
+
return this.#isClosedTurn(this.#messages.get(messageId)?.turnId);
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
/**
|
|
1405
|
+
* INV-MSG (draft.5, CB-8 A′: closure follows the records): a turn is closed
|
|
1406
|
+
* while the fold holds a turn record for it that carries an `outcome`. Only a
|
|
1407
|
+
* folded turn.done / turn.error / turn.abort sets one, so a terminal that
|
|
1408
|
+
* folds onto no record closes nothing; a messages.snapshot changes closure
|
|
1409
|
+
* only through the records its §5 per-container rule leaves in the fold. No
|
|
1410
|
+
* message of a closed turn (sealed or not, existing or not yet created) is a
|
|
1411
|
+
* valid attach or adoption target. `key` is a resolved turnId, or the
|
|
1412
|
+
* "unknown-turn" key of a message with no turnId, which names no record.
|
|
1413
|
+
*/
|
|
1414
|
+
#isClosedTurn(key: string | undefined): boolean {
|
|
1415
|
+
return key !== undefined && this.#turns.get(key)?.outcome !== undefined;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* draft.5 §5.0 INV-MSG / INV-TURN: may a terminal fold onto `turn`? A turn
|
|
1420
|
+
* whose record carries an outcome is closed (#isClosedTurn), and a
|
|
1421
|
+
* turn.done, turn.error or turn.abort for it is a reduce()-error: the fold
|
|
1422
|
+
* parks and nothing is written, whatever the outcome and whether or not it
|
|
1423
|
+
* repeats the recorded one. The one exception is the paused refresh: a
|
|
1424
|
+
* turn.done{paused} (`pausedDone`) for a turn whose recorded outcome is
|
|
1425
|
+
* paused and which a turn.start has named since its last terminal folded. It
|
|
1426
|
+
* folds through the arm's ordinary writes (outcome and asks replace; a field
|
|
1427
|
+
* it omits keeps its recorded value), and the turn stays closed. A terminal
|
|
1428
|
+
* that folds consumes that turn.start.
|
|
1429
|
+
*/
|
|
1430
|
+
#terminalMayFold(turn: AgTurnRecord, pausedDone: boolean): boolean {
|
|
1431
|
+
if (turn.outcome !== undefined) {
|
|
1432
|
+
const refresh = pausedDone && turn.outcome.type === "paused" && this.#restartedSinceClose.has(turn.turnId);
|
|
1433
|
+
if (!refresh) {
|
|
1434
|
+
this.#resync = true;
|
|
1435
|
+
return false;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
this.#restartedSinceClose.delete(turn.turnId);
|
|
1439
|
+
return true;
|
|
1315
1440
|
}
|
|
1316
1441
|
|
|
1317
1442
|
/** rd-14 INV-BLOCK: a block-creating id seen twice in one invoke parks; else records it. */
|
|
@@ -1326,30 +1451,77 @@ export class Reducer {
|
|
|
1326
1451
|
|
|
1327
1452
|
#resolveTurnId(turnId: string | undefined): string | undefined {
|
|
1328
1453
|
if (turnId !== undefined) return turnId;
|
|
1329
|
-
//
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1454
|
+
// §4 per-turn routing / INV-OWNER (draft.5, CB-7): an omitted turnId
|
|
1455
|
+
// resolves to the sole OPEN turn, never to a closed one (through draft.4
|
|
1456
|
+
// this counted records, so a lone closed turn or a stub could be picked).
|
|
1457
|
+
return this.#soleOpenRecord()?.turnId;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/** The sole OPEN turn record (no `outcome` yet), or undefined when none or several are open. */
|
|
1461
|
+
#soleOpenRecord(): AgTurnRecord | undefined {
|
|
1462
|
+
let found: AgTurnRecord | undefined;
|
|
1463
|
+
for (const t of this.#turns.values()) {
|
|
1464
|
+
if (t.outcome !== undefined) continue;
|
|
1465
|
+
if (found !== undefined) return undefined;
|
|
1466
|
+
found = t;
|
|
1335
1467
|
}
|
|
1336
|
-
return
|
|
1468
|
+
return found;
|
|
1337
1469
|
}
|
|
1338
1470
|
|
|
1339
1471
|
/**
|
|
1340
|
-
*
|
|
1341
|
-
*
|
|
1342
|
-
*
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
1345
|
-
*
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1472
|
+
* Where a record event lands (CB-7, draft.5 INV-OWNER). A named turn with a
|
|
1473
|
+
* record lands on it; one whose thread is known gets a new record on that
|
|
1474
|
+
* thread; one whose thread is not known yet is held (a record result() omits),
|
|
1475
|
+
* or, past the implementation bound, parks the fold. A turnId-less record
|
|
1476
|
+
* event lands on the sole open turn's record, else its owner is unresolvable
|
|
1477
|
+
* and the fold parks.
|
|
1478
|
+
*/
|
|
1479
|
+
#landingRecord(turnId: string | undefined): AgTurnRecord | undefined {
|
|
1480
|
+
if (turnId === undefined) {
|
|
1481
|
+
const sole = this.#soleOpenRecord();
|
|
1482
|
+
if (sole === undefined) this.#resync = true;
|
|
1483
|
+
return sole;
|
|
1484
|
+
}
|
|
1485
|
+
const existing = this.#turns.get(turnId);
|
|
1486
|
+
if (existing !== undefined) return existing;
|
|
1487
|
+
const threadId = this.#seenOpened.get(turnId);
|
|
1488
|
+
if (threadId !== undefined) return this.#seenTurnRecord(turnId);
|
|
1489
|
+
let held = this.#held.get(turnId);
|
|
1490
|
+
if ((held === undefined && this.#held.size >= HELD_TURNS_CAP) || this.#heldLandings >= HELD_LANDINGS_CAP) {
|
|
1491
|
+
this.#resync = true; // degrade loudly past the bound
|
|
1492
|
+
return undefined;
|
|
1493
|
+
}
|
|
1494
|
+
if (held === undefined) {
|
|
1495
|
+
// threadId is set on adoption; a held record is never emitted.
|
|
1496
|
+
held = { record: { turnId, threadId: "" }, landings: 0 };
|
|
1497
|
+
this.#held.set(turnId, held);
|
|
1498
|
+
}
|
|
1499
|
+
held.landings++;
|
|
1500
|
+
this.#heldLandings++;
|
|
1501
|
+
return held.record;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
* Move the record held for `turnId` (CB-7) into #turns on the thread that is
|
|
1506
|
+
* now known, with `parentTurnId` when a subagent.start opened it. Returns the
|
|
1507
|
+
* adopted record, or undefined when nothing was held for the turn.
|
|
1352
1508
|
*/
|
|
1509
|
+
#adoptHeld(turnId: string, threadId: string, parentTurnId?: string): AgTurnRecord | undefined {
|
|
1510
|
+
const held = this.#held.get(turnId);
|
|
1511
|
+
if (held === undefined) return undefined;
|
|
1512
|
+
this.#held.delete(turnId);
|
|
1513
|
+
this.#heldLandings -= held.landings;
|
|
1514
|
+
const { turnId: _turnId, threadId: _placeholder, ...landings } = held.record;
|
|
1515
|
+
const record: AgTurnRecord = {
|
|
1516
|
+
turnId,
|
|
1517
|
+
...(parentTurnId !== undefined ? { parentTurnId } : {}),
|
|
1518
|
+
threadId,
|
|
1519
|
+
...landings,
|
|
1520
|
+
};
|
|
1521
|
+
this.#turns.set(turnId, record);
|
|
1522
|
+
return record;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1353
1525
|
/**
|
|
1354
1526
|
* D9: the record a terminal for a SEEN turn folds onto — the existing one,
|
|
1355
1527
|
* else one created on the thread the fold saw the turn opened on. With no
|
|
@@ -1363,22 +1535,13 @@ export class Reducer {
|
|
|
1363
1535
|
if (existing !== undefined) return existing;
|
|
1364
1536
|
const threadId = this.#seenOpened.get(turnId);
|
|
1365
1537
|
if (threadId === undefined) return undefined;
|
|
1538
|
+
const adopted = this.#adoptHeld(turnId, threadId);
|
|
1539
|
+
if (adopted !== undefined) return adopted;
|
|
1366
1540
|
const record: AgTurnRecord = { turnId, threadId };
|
|
1367
1541
|
this.#turns.set(turnId, record);
|
|
1368
1542
|
return record;
|
|
1369
1543
|
}
|
|
1370
1544
|
|
|
1371
|
-
ensureTurn(turnId: string | undefined): AgTurnRecord {
|
|
1372
|
-
const resolved = this.#resolveTurnId(turnId) ?? turnId;
|
|
1373
|
-
const key = resolved ?? "unknown-turn";
|
|
1374
|
-
const existing = this.#turns.get(key);
|
|
1375
|
-
if (existing !== undefined) return existing;
|
|
1376
|
-
// Defensive stub: threadId is required on AgTurnRecord; use key as placeholder.
|
|
1377
|
-
const stub: AgTurnRecord = { turnId: key, threadId: key };
|
|
1378
|
-
this.#turns.set(key, stub);
|
|
1379
|
-
return stub;
|
|
1380
|
-
}
|
|
1381
|
-
|
|
1382
1545
|
/**
|
|
1383
1546
|
* Materialize the current fold state as a DEFENSIVE DEEP COPY.
|
|
1384
1547
|
* Each call returns fresh arrays and objects — holding a snapshot
|
package/src/stream-assembler.ts
CHANGED
|
@@ -109,6 +109,7 @@ export interface AssemblerCheckpoint {
|
|
|
109
109
|
readonly seq: number;
|
|
110
110
|
readonly seenTurns: ReadonlySet<string>;
|
|
111
111
|
readonly openTurns: ReadonlySet<string>;
|
|
112
|
+
readonly closedTurns: ReadonlySet<string>;
|
|
112
113
|
readonly openMessages: ReadonlyMap<string, { turnId: string }>;
|
|
113
114
|
readonly msgTurn: ReadonlyMap<string, string>;
|
|
114
115
|
readonly lastTurn: string | undefined;
|
|
@@ -131,6 +132,13 @@ export class StreamAssembler {
|
|
|
131
132
|
// Drives `flush()`'s INV-FLUSH turn.abort closure (audit M21).
|
|
132
133
|
#openTurns = new Set<string>();
|
|
133
134
|
|
|
135
|
+
// turnIds whose terminal (turn.done / turn.error / turn.abort, or a nested
|
|
136
|
+
// turn's subagent.done) has been emitted. A closed turn stays closed: a later
|
|
137
|
+
// merging turn.start or subagent.start naming it never puts it back in
|
|
138
|
+
// #openTurns (the reducer's INV-MSG rule: such a start merges fields and does
|
|
139
|
+
// not reopen the turn), so flush() can never emit a second terminal for it.
|
|
140
|
+
#closedTurns = new Set<string>();
|
|
141
|
+
|
|
134
142
|
// Insertion-ordered map of open messageId → its owning turnId.
|
|
135
143
|
// Used by flush() to close dangling messages in the order they were opened.
|
|
136
144
|
#openMessages = new Map<string, { turnId: string }>();
|
|
@@ -180,17 +188,21 @@ export class StreamAssembler {
|
|
|
180
188
|
switch (ev.type) {
|
|
181
189
|
case "turn.start":
|
|
182
190
|
case "subagent.start":
|
|
183
|
-
this.#openTurns.add(ev.turnId);
|
|
191
|
+
if (!this.#closedTurns.has(ev.turnId)) this.#openTurns.add(ev.turnId);
|
|
184
192
|
break;
|
|
185
193
|
case "turn.done":
|
|
186
194
|
case "turn.error":
|
|
187
195
|
case "turn.abort": {
|
|
188
196
|
const closed = ev.turnId ?? this.#lastTurn;
|
|
189
|
-
if (closed !== undefined)
|
|
197
|
+
if (closed !== undefined) {
|
|
198
|
+
this.#openTurns.delete(closed);
|
|
199
|
+
this.#closedTurns.add(closed);
|
|
200
|
+
}
|
|
190
201
|
break;
|
|
191
202
|
}
|
|
192
203
|
case "subagent.done":
|
|
193
204
|
this.#openTurns.delete(ev.turnId);
|
|
205
|
+
this.#closedTurns.add(ev.turnId);
|
|
194
206
|
break;
|
|
195
207
|
default:
|
|
196
208
|
break;
|
|
@@ -252,7 +264,15 @@ export class StreamAssembler {
|
|
|
252
264
|
* Delegates to `#ensureTurn` so the synthesis path lives in one place.
|
|
253
265
|
*/
|
|
254
266
|
openTurn(turnId: string, threadId: string, opts?: { trigger?: AgTrigger }): void {
|
|
255
|
-
this.#
|
|
267
|
+
if (this.#seenTurns.has(turnId) && opts?.trigger !== undefined) {
|
|
268
|
+
// A trigger for a turn already introduced is not dropped: a later
|
|
269
|
+
// turn.start naming a seen turn merges its fields (INV-TURN), so the
|
|
270
|
+
// trigger reaches the turn record. A closed turn stays closed (#emit).
|
|
271
|
+
const ev: TurnStartEvent = { type: "turn.start", seq: this.#nextSeq(), turnId, threadId, trigger: opts.trigger };
|
|
272
|
+
this.#emit(ev);
|
|
273
|
+
} else {
|
|
274
|
+
this.#ensureTurn(turnId, threadId, opts);
|
|
275
|
+
}
|
|
256
276
|
this.#lastTurn = turnId;
|
|
257
277
|
}
|
|
258
278
|
|
|
@@ -682,6 +702,7 @@ export class StreamAssembler {
|
|
|
682
702
|
seq: this.#seq,
|
|
683
703
|
seenTurns: new Set(this.#seenTurns),
|
|
684
704
|
openTurns: new Set(this.#openTurns),
|
|
705
|
+
closedTurns: new Set(this.#closedTurns),
|
|
685
706
|
openMessages: new Map([...this.#openMessages].map(([k, v]) => [k, { ...v }])),
|
|
686
707
|
msgTurn: new Map(this.#msgTurn),
|
|
687
708
|
lastTurn: this.#lastTurn,
|
|
@@ -700,6 +721,7 @@ export class StreamAssembler {
|
|
|
700
721
|
this.#seq = cp.seq;
|
|
701
722
|
this.#seenTurns = new Set(cp.seenTurns);
|
|
702
723
|
this.#openTurns = new Set(cp.openTurns);
|
|
724
|
+
this.#closedTurns = new Set(cp.closedTurns);
|
|
703
725
|
this.#openMessages = new Map([...cp.openMessages].map(([k, v]) => [k, { ...v }]));
|
|
704
726
|
this.#msgTurn = new Map(cp.msgTurn);
|
|
705
727
|
this.#lastTurn = cp.lastTurn;
|