@runtypelabs/persona 3.14.0 → 3.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,256 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { SequenceReorderBuffer } from "./sequence-buffer";
3
+
4
+ describe("SequenceReorderBuffer", () => {
5
+ let emitted: Array<{ payloadType: string; payload: any }>;
6
+ let emitter: (payloadType: string, payload: any) => void;
7
+
8
+ beforeEach(() => {
9
+ vi.useFakeTimers();
10
+ emitted = [];
11
+ emitter = (payloadType, payload) => {
12
+ emitted.push({ payloadType, payload });
13
+ };
14
+ });
15
+
16
+ afterEach(() => {
17
+ vi.useRealTimers();
18
+ });
19
+
20
+ it("passes in-order events through immediately", () => {
21
+ const buf = new SequenceReorderBuffer(emitter);
22
+ buf.push("step_delta", { seq: 1, text: "a" });
23
+ buf.push("step_delta", { seq: 2, text: "b" });
24
+ buf.push("step_delta", { seq: 3, text: "c" });
25
+
26
+ expect(emitted).toHaveLength(3);
27
+ expect(emitted[0].payload.text).toBe("a");
28
+ expect(emitted[1].payload.text).toBe("b");
29
+ expect(emitted[2].payload.text).toBe("c");
30
+ buf.destroy();
31
+ });
32
+
33
+ it("reorders leading out-of-order events (3, 1, 2 → 1, 2, 3)", () => {
34
+ const buf = new SequenceReorderBuffer(emitter);
35
+ // seq=3 arrives first — should be buffered (3 > nextExpected=1)
36
+ buf.push("step_delta", { seq: 3, text: "c" });
37
+ expect(emitted).toHaveLength(0);
38
+
39
+ // seq=1 arrives — matches nextExpected, emits, then drains seq=2 (not present), stops
40
+ buf.push("step_delta", { seq: 1, text: "a" });
41
+ expect(emitted).toHaveLength(1);
42
+ expect(emitted[0].payload.text).toBe("a");
43
+
44
+ // seq=2 arrives — matches nextExpected=2, emits, drains seq=3 from buffer
45
+ buf.push("step_delta", { seq: 2, text: "b" });
46
+ expect(emitted).toHaveLength(3);
47
+ expect(emitted[1].payload.text).toBe("b");
48
+ expect(emitted[2].payload.text).toBe("c");
49
+ buf.destroy();
50
+ });
51
+
52
+ it("reorders mid-stream out-of-order events", () => {
53
+ const buf = new SequenceReorderBuffer(emitter);
54
+ buf.push("step_delta", { seq: 1, text: "a" });
55
+ buf.push("step_delta", { seq: 3, text: "c" }); // buffered
56
+ buf.push("step_delta", { seq: 2, text: "b" }); // emits, drains 3
57
+
58
+ expect(emitted).toHaveLength(3);
59
+ expect(emitted[0].payload.text).toBe("a");
60
+ expect(emitted[1].payload.text).toBe("b");
61
+ expect(emitted[2].payload.text).toBe("c");
62
+ buf.destroy();
63
+ });
64
+
65
+ it("flushes buffered events after gap timeout when a seq is missing", () => {
66
+ const buf = new SequenceReorderBuffer(emitter, 50);
67
+ buf.push("step_delta", { seq: 1, text: "a" }); // emits
68
+ buf.push("step_delta", { seq: 3, text: "c" }); // buffered (waiting for seq 2)
69
+
70
+ expect(emitted).toHaveLength(1);
71
+
72
+ // Advance past gap timeout — seq=2 never arrives, flush seq=3 anyway
73
+ vi.advanceTimersByTime(60);
74
+
75
+ expect(emitted).toHaveLength(2);
76
+ expect(emitted[1].payload.text).toBe("c");
77
+ buf.destroy();
78
+ });
79
+
80
+ it("passes no-seq events through immediately (backward compat)", () => {
81
+ const buf = new SequenceReorderBuffer(emitter);
82
+ buf.push("flow_start", { flowId: "abc" });
83
+ buf.push("step_start", { name: "test" });
84
+
85
+ expect(emitted).toHaveLength(2);
86
+ expect(emitted[0].payload.flowId).toBe("abc");
87
+ expect(emitted[1].payload.name).toBe("test");
88
+ buf.destroy();
89
+ });
90
+
91
+ it("emits late/duplicate events (seq < nextExpected)", () => {
92
+ const buf = new SequenceReorderBuffer(emitter);
93
+ // Process seq 1-3 normally to advance nextExpected to 4
94
+ buf.push("step_delta", { seq: 1, text: "a" });
95
+ buf.push("step_delta", { seq: 2, text: "b" });
96
+ buf.push("step_delta", { seq: 3, text: "c" });
97
+ expect(emitted).toHaveLength(3);
98
+
99
+ // Now seq=1 arrives again — it's a duplicate (1 < nextExpected=4), still emitted
100
+ buf.push("step_delta", { seq: 1, text: "a-dup" });
101
+ expect(emitted).toHaveLength(4);
102
+ expect(emitted[3].payload.text).toBe("a-dup");
103
+ buf.destroy();
104
+ });
105
+
106
+ it("handles mixed seq and no-seq events", () => {
107
+ const buf = new SequenceReorderBuffer(emitter);
108
+ buf.push("step_delta", { seq: 1, text: "a" });
109
+ buf.push("status", { status: "streaming" }); // no seq
110
+ buf.push("step_delta", { seq: 2, text: "b" });
111
+ buf.push("error", { error: "oops" }); // no seq
112
+
113
+ expect(emitted).toHaveLength(4);
114
+ expect(emitted[0].payload.text).toBe("a");
115
+ expect(emitted[1].payload.status).toBe("streaming");
116
+ expect(emitted[2].payload.text).toBe("b");
117
+ expect(emitted[3].payload.error).toBe("oops");
118
+ buf.destroy();
119
+ });
120
+
121
+ it("handles large burst of out-of-order events correctly", () => {
122
+ const buf = new SequenceReorderBuffer(emitter);
123
+ // Send events in reverse order: 10, 9, 8, ..., 1
124
+ // All are buffered until seq=1 arrives (last), then everything drains
125
+ const seqs = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
126
+ for (const seq of seqs) {
127
+ buf.push("step_delta", { seq, text: `chunk-${seq}` });
128
+ }
129
+
130
+ expect(emitted).toHaveLength(10);
131
+ const emittedTexts = emitted.map(e => e.payload.text);
132
+ expect(emittedTexts).toEqual([
133
+ "chunk-1", "chunk-2", "chunk-3", "chunk-4", "chunk-5",
134
+ "chunk-6", "chunk-7", "chunk-8", "chunk-9", "chunk-10"
135
+ ]);
136
+ buf.destroy();
137
+ });
138
+
139
+ it("handles scrambled arrival order", () => {
140
+ const buf = new SequenceReorderBuffer(emitter);
141
+ const scrambled = [1, 5, 3, 2, 4, 8, 6, 7, 10, 9];
142
+ for (const seq of scrambled) {
143
+ buf.push("step_delta", { seq, text: `chunk-${seq}` });
144
+ }
145
+
146
+ expect(emitted).toHaveLength(10);
147
+ const emittedTexts = emitted.map(e => e.payload.text);
148
+ expect(emittedTexts).toEqual([
149
+ "chunk-1", "chunk-2", "chunk-3", "chunk-4", "chunk-5",
150
+ "chunk-6", "chunk-7", "chunk-8", "chunk-9", "chunk-10"
151
+ ]);
152
+ buf.destroy();
153
+ });
154
+
155
+ it("no-seq event flushes pending buffer and cancels the gap timer", () => {
156
+ const buf = new SequenceReorderBuffer(emitter, 50);
157
+ buf.push("step_delta", { seq: 1, text: "a" });
158
+ buf.push("step_delta", { seq: 3, text: "c" }); // buffered, starts gap timer
159
+ expect(emitted).toHaveLength(1);
160
+
161
+ // A no-seq event triggers flushAll, which drains the buffer in seq order
162
+ // and cancels the gap timer.
163
+ buf.push("flow_complete", { flowId: "done" });
164
+ expect(emitted).toHaveLength(3); // a, c, flow_complete
165
+
166
+ // The gap timer must no longer fire after the flushAll.
167
+ vi.advanceTimersByTime(100);
168
+ expect(emitted).toHaveLength(3);
169
+ buf.destroy();
170
+ });
171
+
172
+ it("supports sequenceIndex as an alternative to seq", () => {
173
+ const buf = new SequenceReorderBuffer(emitter);
174
+ buf.push("reason_delta", { sequenceIndex: 1, text: "a" });
175
+ buf.push("reason_delta", { sequenceIndex: 3, text: "c" });
176
+ buf.push("reason_delta", { sequenceIndex: 2, text: "b" });
177
+
178
+ expect(emitted).toHaveLength(3);
179
+ expect(emitted[0].payload.text).toBe("a");
180
+ expect(emitted[1].payload.text).toBe("b");
181
+ expect(emitted[2].payload.text).toBe("c");
182
+ buf.destroy();
183
+ });
184
+
185
+ it("leading gap flushes via timeout when seq=1 never arrives", () => {
186
+ const buf = new SequenceReorderBuffer(emitter, 50);
187
+ // Only seq=2 and seq=3 arrive — seq=1 is missing
188
+ buf.push("step_delta", { seq: 2, text: "b" });
189
+ buf.push("step_delta", { seq: 3, text: "c" });
190
+ expect(emitted).toHaveLength(0); // both buffered
191
+
192
+ vi.advanceTimersByTime(50);
193
+ // Gap timer flushes in seq order
194
+ expect(emitted).toHaveLength(2);
195
+ expect(emitted[0].payload.text).toBe("b");
196
+ expect(emitted[1].payload.text).toBe("c");
197
+ buf.destroy();
198
+ });
199
+
200
+ it("handles a stream whose first seq is > 1 via the gap timeout (no loss)", () => {
201
+ // Defensive: if the server's counter ever starts above 1 (e.g. a resumed
202
+ // stream), the hardcoded nextExpectedSeq=1 would buffer the first event.
203
+ // The gap timer must still flush it so nothing is lost.
204
+ const buf = new SequenceReorderBuffer(emitter, 50);
205
+ buf.push("step_delta", { seq: 5, text: "first" });
206
+ buf.push("step_delta", { seq: 6, text: "second" });
207
+ expect(emitted).toHaveLength(0);
208
+
209
+ vi.advanceTimersByTime(50);
210
+
211
+ expect(emitted).toHaveLength(2);
212
+ expect(emitted[0].payload.text).toBe("first");
213
+ expect(emitted[1].payload.text).toBe("second");
214
+
215
+ // Subsequent in-order events should pass through immediately.
216
+ buf.push("step_delta", { seq: 7, text: "third" });
217
+ expect(emitted).toHaveLength(3);
218
+ expect(emitted[2].payload.text).toBe("third");
219
+ buf.destroy();
220
+ });
221
+
222
+ it("warns and emits both events on seq collision (does not silently drop)", () => {
223
+ // Server invariant: seq is unique per stream. If it's ever violated
224
+ // (bug, replay, mixed counters), Map.set would silently overwrite. The
225
+ // buffer must detect this, warn, and emit the prior event so nothing is
226
+ // dropped.
227
+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
228
+ const buf = new SequenceReorderBuffer(emitter, 50);
229
+
230
+ buf.push("step_delta", { seq: 1, text: "a" });
231
+ // seq=3 buffered, waiting for seq=2
232
+ buf.push("step_delta", { seq: 3, text: "first-at-3" });
233
+ expect(emitted).toHaveLength(1);
234
+
235
+ // Second event with same seq=3 — prior one should be emitted out-of-order
236
+ buf.push("reason_delta", { seq: 3, text: "second-at-3" });
237
+
238
+ expect(warnSpy).toHaveBeenCalledTimes(1);
239
+ expect(warnSpy.mock.calls[0][0]).toContain("duplicate seq=3");
240
+ expect(warnSpy.mock.calls[0][0]).toContain("step_delta");
241
+ expect(warnSpy.mock.calls[0][0]).toContain("reason_delta");
242
+
243
+ // Prior event flushed immediately (out of seq order), nothing lost
244
+ expect(emitted).toHaveLength(2);
245
+ expect(emitted[1].payload.text).toBe("first-at-3");
246
+
247
+ // seq=2 arrives — advances nextExpected through the buffered second-at-3
248
+ buf.push("step_delta", { seq: 2, text: "b" });
249
+ expect(emitted).toHaveLength(4);
250
+ expect(emitted[2].payload.text).toBe("b");
251
+ expect(emitted[3].payload.text).toBe("second-at-3");
252
+
253
+ warnSpy.mockRestore();
254
+ buf.destroy();
255
+ });
256
+ });
@@ -0,0 +1,130 @@
1
+ type BufferedEvent = { payloadType: string; payload: any; seq: number };
2
+
3
+ export class SequenceReorderBuffer {
4
+ private nextExpectedSeq: number | null = null;
5
+ private buffer: Map<number, BufferedEvent> = new Map();
6
+ private flushTimer: ReturnType<typeof setTimeout> | null = null;
7
+ private emitter: (payloadType: string, payload: any) => void;
8
+ private gapTimeoutMs: number;
9
+
10
+ constructor(emitter: (payloadType: string, payload: any) => void, gapTimeoutMs = 50) {
11
+ this.emitter = emitter;
12
+ this.gapTimeoutMs = gapTimeoutMs;
13
+ }
14
+
15
+ push(payloadType: string, payload: any): void {
16
+ // All three fields are sourced from the same FlowExecutionEngine.sequenceCounter:
17
+ // - `seq`: step_delta, text_start, text_end, agent_* events (top-level)
18
+ // - `sequenceIndex`: reason_start, reason_delta, reason_complete, source
19
+ // - `agentContext.seq`: tool_start, tool_delta, tool_complete (agent loop)
20
+ const seq = payload?.seq ?? payload?.sequenceIndex ?? payload?.agentContext?.seq;
21
+
22
+ // No seq field — emit immediately (backward compat).
23
+ // If there are buffered events waiting for a gap to fill, flush them
24
+ // first: the server sending an unsequenced event means it has moved on
25
+ // and the missing seq numbers are not coming.
26
+ if (seq === undefined || seq === null) {
27
+ if (this.buffer.size > 0) {
28
+ this.flushAll();
29
+ }
30
+ this.emitter(payloadType, payload);
31
+ return;
32
+ }
33
+
34
+ // Server's sequenceCounter resets to 0 on each execution and pre-increments,
35
+ // so the first sequenced event in any stream is expected to have seq=1.
36
+ // If a server ever starts at a different number (e.g. a resumed stream),
37
+ // the 50ms gap timer below is the safety net: the first event gets
38
+ // buffered, then flushed after the gap elapses. Correctness is preserved;
39
+ // the only cost is a one-time latency on the leading event.
40
+ if (this.nextExpectedSeq === null) {
41
+ this.nextExpectedSeq = 1;
42
+ }
43
+
44
+ // If this is the expected event, emit it and drain consecutive buffered events
45
+ if (seq === this.nextExpectedSeq) {
46
+ this.emitter(payloadType, payload);
47
+ this.nextExpectedSeq = (seq as number) + 1;
48
+ this.drainConsecutive();
49
+ return;
50
+ }
51
+
52
+ // If seq < nextExpected, it's a duplicate or late arrival — emit anyway (don't drop)
53
+ if (seq < this.nextExpectedSeq!) {
54
+ this.emitter(payloadType, payload);
55
+ return;
56
+ }
57
+
58
+ // seq > nextExpected — buffer it and start gap timer.
59
+ // If another event with the same seq is already buffered, the server
60
+ // broke its "seq is unique per stream" invariant. Rather than silently
61
+ // overwrite (losing one event) or swallow the new one, emit the prior
62
+ // event immediately — out of order, but better than dropping it — and
63
+ // warn so the issue is visible.
64
+ const existing = this.buffer.get(seq);
65
+ if (existing !== undefined) {
66
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
67
+ console.warn(
68
+ `[persona] SequenceReorderBuffer: duplicate seq=${seq} ` +
69
+ `(${existing.payloadType} vs ${payloadType}); ` +
70
+ `emitting earlier event out-of-order to avoid loss`
71
+ );
72
+ }
73
+ this.emitter(existing.payloadType, existing.payload);
74
+ }
75
+ this.buffer.set(seq, { payloadType, payload, seq });
76
+ this.startGapTimer();
77
+ }
78
+
79
+ private drainConsecutive(): void {
80
+ while (this.buffer.has(this.nextExpectedSeq!)) {
81
+ const event = this.buffer.get(this.nextExpectedSeq!)!;
82
+ this.buffer.delete(this.nextExpectedSeq!);
83
+ this.emitter(event.payloadType, event.payload);
84
+ this.nextExpectedSeq!++;
85
+ }
86
+ // If buffer is empty, clear the gap timer
87
+ if (this.buffer.size === 0) {
88
+ this.clearGapTimer();
89
+ }
90
+ }
91
+
92
+ private startGapTimer(): void {
93
+ if (this.flushTimer !== null) return;
94
+ this.flushTimer = setTimeout(() => {
95
+ this.flushAll();
96
+ }, this.gapTimeoutMs);
97
+ }
98
+
99
+ private clearGapTimer(): void {
100
+ if (this.flushTimer !== null) {
101
+ clearTimeout(this.flushTimer);
102
+ this.flushTimer = null;
103
+ }
104
+ }
105
+
106
+ private flushAll(): void {
107
+ this.clearGapTimer();
108
+ if (this.buffer.size === 0) return;
109
+
110
+ // Flush all buffered events in seq order
111
+ const sorted = [...this.buffer.entries()].sort((a, b) => a[0] - b[0]);
112
+ for (const [seq, event] of sorted) {
113
+ this.buffer.delete(seq);
114
+ this.emitter(event.payloadType, event.payload);
115
+ }
116
+ // Update nextExpectedSeq to after the last flushed
117
+ if (sorted.length > 0) {
118
+ this.nextExpectedSeq = sorted[sorted.length - 1][0] + 1;
119
+ }
120
+ }
121
+
122
+ destroy(): void {
123
+ this.clearGapTimer();
124
+ this.buffer.clear();
125
+ }
126
+
127
+ flushPending(): void {
128
+ this.flushAll();
129
+ }
130
+ }