Open History to see the last commit
1import {
2 EventId,
3 MessageId,
4 ThreadId,
5 TurnId,
6 type OrchestrationEvent,
7 type OrchestrationThreadActivity,
8} from "@t3tools/contracts";
9import { it } from "@effect/vitest";
10import * as Clock from "effect/Clock";
11import * as Effect from "effect/Effect";
12import * as TestClock from "effect/testing/TestClock";
13import { describe, expect } from "vite-plus/test";
14
15import {
16 coalesceLiveToolUpdatedEvents,
17 makeThreadLiveEventCoalescer,
18} from "./ThreadLiveEventCoalescer.ts";
19
20const threadId = ThreadId.make("thread-coalescer-test");
21const turnId = TurnId.make("turn-coalescer-test");
22
23function makeToolActivity(
24 sequence: number,
25 options: {
26 readonly kind?: "tool.updated" | "tool.completed";
27 readonly toolCallId?: string;
28 readonly turnId?: TurnId;
29 } = {},
30): OrchestrationEvent {
31 const {
32 kind = "tool.updated",
33 toolCallId = "call-edit",
34 turnId: activityTurnId = turnId,
35 } = options;
36 const activity: OrchestrationThreadActivity = {
37 id: EventId.make(`activity-${sequence}`),
38 tone: "tool",
39 kind,
40 summary: "Editing app.ts",
41 payload: {
42 itemType: "file_change",
43 title: "Editing app.ts",
44 data: toolCallId ? { toolCallId } : {},
45 },
46 turnId: activityTurnId,
47 createdAt: "2026-01-01T00:00:01.000Z",
48 };
49 return {
50 sequence,
51 eventId: EventId.make(`event-${sequence}`),
52 aggregateKind: "thread",
53 aggregateId: threadId,
54 occurredAt: "2026-01-01T00:00:01.000Z",
55 commandId: null,
56 causationEventId: null,
57 correlationId: null,
58 metadata: {},
59 type: "thread.activity-appended",
60 payload: { threadId, activity },
61 };
62}
63
64function makeMessage(sequence: number): OrchestrationEvent {
65 return {
66 sequence,
67 eventId: EventId.make(`event-${sequence}`),
68 aggregateKind: "thread",
69 aggregateId: threadId,
70 occurredAt: "2026-01-01T00:00:02.000Z",
71 commandId: null,
72 causationEventId: null,
73 correlationId: null,
74 metadata: {},
75 type: "thread.message-sent",
76 payload: {
77 threadId,
78 messageId: MessageId.make(`message-${sequence}`),
79 role: "assistant",
80 text: "Still working",
81 turnId,
82 streaming: false,
83 createdAt: "2026-01-01T00:00:02.000Z",
84 updatedAt: "2026-01-01T00:00:02.000Z",
85 },
86 };
87}
88
89describe("ThreadLiveEventCoalescer", () => {
90 it("coalesces only calls with a stable toolCallId", () => {
91 const events = [
92 makeToolActivity(1, { toolCallId: "call-a" }),
93 makeToolActivity(2, { toolCallId: "call-b" }),
94 makeToolActivity(3, { toolCallId: "call-a" }),
95 ];
96
97 expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3]);
98 });
99
100 it("preserves parallel same-label calls without a stable toolCallId", () => {
101 const events = [
102 makeToolActivity(1, { toolCallId: "" }),
103 makeToolActivity(2, { toolCallId: "" }),
104 makeToolActivity(3, { kind: "tool.completed", toolCallId: "" }),
105 ];
106
107 expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2, 3]);
108 });
109
110 it("does not coalesce stable tool calls across turns", () => {
111 const events = [
112 makeToolActivity(1, { turnId: TurnId.make("turn-old") }),
113 makeToolActivity(2, { turnId: TurnId.make("turn-new") }),
114 ];
115
116 expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2]);
117 });
118
119 it("flushes a stable update run before a completion boundary", () => {
120 const events = [
121 makeToolActivity(1),
122 makeToolActivity(2),
123 makeToolActivity(3, { kind: "tool.completed" }),
124 makeToolActivity(4),
125 ];
126
127 expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3, 4]);
128 });
129
130 it.effect("flushes pending tool updates as soon as an unrelated event arrives", () =>
131 Effect.scoped(
132 Effect.gen(function* () {
133 const coalescer = yield* makeThreadLiveEventCoalescer({ coalesceWindow: "500 millis" });
134 const startedAt = yield* Clock.currentTimeMillis;
135 yield* Effect.forEach(
136 Array.from({ length: 10 }, (_, index) => index + 2),
137 (sequence) =>
138 coalescer.offerAndWait({ kind: "event", event: makeToolActivity(sequence) }),
139 { discard: true },
140 );
141 yield* coalescer.offerAndWait({ kind: "event", event: makeMessage(12) });
142
143 expect(yield* Clock.currentTimeMillis).toBe(startedAt);
144 expect(
145 Array.from(yield* coalescer.takeAll).map((item) =>
146 item.kind === "event" ? item.event.sequence : item.kind,
147 ),
148 ).toEqual([11, 12]);
149 }),
150 ).pipe(Effect.provide(TestClock.layer())),
151 );
152
153 it.effect("flushes pending tool updates as soon as a synchronization marker arrives", () =>
154 Effect.scoped(
155 Effect.gen(function* () {
156 const coalescer = yield* makeThreadLiveEventCoalescer({ coalesceWindow: "500 millis" });
157 const startedAt = yield* Clock.currentTimeMillis;
158 yield* coalescer.offerAndWait({ kind: "event", event: makeToolActivity(2) });
159 yield* coalescer.offerAndWait({ kind: "event", event: makeToolActivity(3) });
160 yield* coalescer.offerAndWait({ kind: "synchronized" });
161
162 expect(yield* Clock.currentTimeMillis).toBe(startedAt);
163 expect(
164 Array.from(yield* coalescer.takeAll).map((item) =>
165 item.kind === "event" ? item.event.sequence : item.kind,
166 ),
167 ).toEqual([3, "synchronized"]);
168 }),
169 ).pipe(Effect.provide(TestClock.layer())),
170 );
171});