|
1 | 1 | import { |
2 | 2 | EventId, |
3 | 3 | MessageId, |
| 4 | + ProjectId, |
| 5 | + ProviderInstanceId, |
| 6 | + type OrchestrationV2ThreadProjection, |
4 | 7 | ThreadId, |
5 | 8 | RunId, |
6 | 9 | TurnItemId, |
7 | 10 | type OrchestrationV2ThreadStreamItem, |
| 11 | + type OrchestrationV2TurnItem, |
8 | 12 | } from "@t3tools/contracts"; |
9 | 13 | import { it } from "@effect/vitest"; |
10 | 14 | import * as DateTime from "effect/DateTime"; |
@@ -13,11 +17,13 @@ import * as Stream from "effect/Stream"; |
13 | 17 | import * as TestClock from "effect/testing/TestClock"; |
14 | 18 | import { describe, expect } from "vite-plus/test"; |
15 | 19 | import { |
16 | | - coalesceLiveToolUpdatedEvents, |
| 20 | + coalesceLiveUpdatedEvents, |
17 | 21 | coalesceThreadLiveStream, |
18 | 22 | makeThreadLiveEventCoalescer, |
19 | 23 | } from "./ThreadLiveEventCoalescer.ts"; |
20 | 24 |
|
| 25 | +import { applyOrchestrationV2ProjectionEvent } from "../../../../packages/client-runtime/src/state/orchestrationV2Projection.ts"; |
| 26 | + |
21 | 27 | type ThreadEvent = Extract<OrchestrationV2ThreadStreamItem, { readonly kind: "event" }>; |
22 | 28 | const threadId = ThreadId.make("thread-coalescer-test"); |
23 | 29 | const runId = RunId.make("run-coalescer-test"); |
@@ -94,15 +100,69 @@ function makeMessage(sequence: number, text = "Still working"): ThreadEvent { |
94 | 100 | }; |
95 | 101 | } |
96 | 102 |
|
| 103 | +function makeStreamingMessage( |
| 104 | + sequence: number, |
| 105 | + options: { |
| 106 | + readonly id?: string; |
| 107 | + readonly text?: string; |
| 108 | + readonly streaming?: boolean; |
| 109 | + } = {}, |
| 110 | +): ThreadEvent { |
| 111 | + const message = makeMessage(sequence, options.text ?? `text-${sequence}`); |
| 112 | + if (message.event.type !== "message.updated") throw new Error("Expected message"); |
| 113 | + return { |
| 114 | + ...message, |
| 115 | + event: { |
| 116 | + ...message.event, |
| 117 | + payload: { |
| 118 | + ...message.event.payload, |
| 119 | + id: MessageId.make(options.id ?? "message"), |
| 120 | + streaming: options.streaming ?? true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +function makeTextItem( |
| 127 | + sequence: number, |
| 128 | + options: { |
| 129 | + readonly id?: string; |
| 130 | + readonly text?: string; |
| 131 | + readonly type?: "assistant_message" | "reasoning"; |
| 132 | + readonly status?: OrchestrationV2TurnItem["status"]; |
| 133 | + readonly streaming?: boolean; |
| 134 | + } = {}, |
| 135 | +): ThreadEvent { |
| 136 | + const tool = makeToolActivity(sequence); |
| 137 | + if (tool.event.type !== "turn-item.updated" || tool.event.payload.type !== "command_execution") |
| 138 | + throw new Error("Expected command item"); |
| 139 | + const { input: _input, output: _output, ...base } = tool.event.payload; |
| 140 | + return { |
| 141 | + ...tool, |
| 142 | + event: { |
| 143 | + ...tool.event, |
| 144 | + payload: { |
| 145 | + ...base, |
| 146 | + id: TurnItemId.make(options.id ?? "item"), |
| 147 | + type: options.type ?? "assistant_message", |
| 148 | + messageId: MessageId.make("message"), |
| 149 | + text: options.text ?? `text-${sequence}`, |
| 150 | + status: options.status ?? "running", |
| 151 | + streaming: options.streaming ?? true, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }; |
| 155 | +} |
| 156 | + |
97 | 157 | describe("ThreadLiveEventCoalescer", () => { |
98 | | - it("coalesces only calls with a stable toolCallId", () => { |
| 158 | + it("preserves tool-call identity switches as insertion-order boundaries", () => { |
99 | 159 | const events = [ |
100 | 160 | makeToolActivity(1, { toolCallId: "call-a" }), |
101 | 161 | makeToolActivity(2, { toolCallId: "call-b" }), |
102 | 162 | makeToolActivity(3, { toolCallId: "call-a" }), |
103 | 163 | ]; |
104 | 164 |
|
105 | | - expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3]); |
| 165 | + expect(coalesceLiveUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2, 3]); |
106 | 166 | }); |
107 | 167 |
|
108 | 168 | it("does not coalesce stable tool calls across turns", () => { |
@@ -111,7 +171,7 @@ describe("ThreadLiveEventCoalescer", () => { |
111 | 171 | makeToolActivity(2, { turnId: RunId.make("turn-new") }), |
112 | 172 | ]; |
113 | 173 |
|
114 | | - expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2]); |
| 174 | + expect(coalesceLiveUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2]); |
115 | 175 | }); |
116 | 176 |
|
117 | 177 | it("flushes a stable update run before a completion boundary", () => { |
@@ -122,9 +182,247 @@ describe("ThreadLiveEventCoalescer", () => { |
122 | 182 | makeToolActivity(4), |
123 | 183 | ]; |
124 | 184 |
|
125 | | - expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3, 4]); |
| 185 | + expect(coalesceLiveUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3, 4]); |
| 186 | + }); |
| 187 | + |
| 188 | + it("keeps the latest full text in sequence order and preserves state after every survivor cursor", () => { |
| 189 | + const events = [ |
| 190 | + makeStreamingMessage(1, { id: "shared", text: "H" }), |
| 191 | + makeTextItem(2, { id: "shared", text: "H" }), |
| 192 | + makeTextItem(3, { id: "reasoning", type: "reasoning", text: "Think" }), |
| 193 | + makeToolActivity(4), |
| 194 | + makeTextItem(5, { id: "unique", text: "Keep me" }), |
| 195 | + makeStreamingMessage(6, { id: "shared", text: "Hello" }), |
| 196 | + makeTextItem(7, { id: "shared", text: "Hello" }), |
| 197 | + makeTextItem(8, { id: "reasoning", type: "reasoning", text: "Thinking done" }), |
| 198 | + makeToolActivity(9), |
| 199 | + makeStreamingMessage(10, { id: "shared", streaming: false, text: "Hello!" }), |
| 200 | + makeTextItem(11, { id: "shared", streaming: false, status: "completed", text: "Hello!" }), |
| 201 | + ]; |
| 202 | + const survivors = coalesceLiveUpdatedEvents(events); |
| 203 | + expect(survivors).toEqual(events); |
| 204 | + |
| 205 | + const latestStates = (input: ReadonlyArray<ThreadEvent>) => |
| 206 | + Object.fromEntries( |
| 207 | + input.map(({ event }) => [ |
| 208 | + `${event.type}:${"id" in event.payload ? event.payload.id : ""}`, |
| 209 | + event.payload, |
| 210 | + ]), |
| 211 | + ); |
| 212 | + for (let index = 0; index < survivors.length; index += 1) { |
| 213 | + const received = survivors.slice(0, index + 1); |
| 214 | + const cursor = received.at(-1)!.sequence; |
| 215 | + const resumed = coalesceLiveUpdatedEvents(events.filter((event) => event.sequence > cursor)); |
| 216 | + expect(latestStates([...received, ...resumed])).toEqual(latestStates(events)); |
| 217 | + } |
126 | 218 | }); |
127 | 219 |
|
| 220 | + it("preserves assistant-message identity changes as insertion-order boundaries", () => { |
| 221 | + const events = [ |
| 222 | + makeStreamingMessage(1, { id: "message-a" }), |
| 223 | + makeTextItem(2, { id: "item-a" }), |
| 224 | + makeStreamingMessage(3, { id: "message-b" }), |
| 225 | + makeTextItem(4, { id: "item-b" }), |
| 226 | + makeStreamingMessage(5, { id: "message-a" }), |
| 227 | + makeTextItem(6, { id: "item-a" }), |
| 228 | + ]; |
| 229 | + expect(coalesceLiveUpdatedEvents(events)).toEqual(events); |
| 230 | + }); |
| 231 | + |
| 232 | + it.effect( |
| 233 | + "preserves turn-item insertion and partial timeline state after every survivor cursor", |
| 234 | + () => |
| 235 | + Effect.gen(function* () { |
| 236 | + const item = ( |
| 237 | + sequence: number, |
| 238 | + id: string, |
| 239 | + text: string, |
| 240 | + completed = false, |
| 241 | + ): ThreadEvent => { |
| 242 | + const input = makeTextItem(sequence, { |
| 243 | + id, |
| 244 | + text, |
| 245 | + type: "reasoning", |
| 246 | + status: completed ? "completed" : "running", |
| 247 | + streaming: !completed, |
| 248 | + }); |
| 249 | + if (input.event.type !== "turn-item.updated") throw new Error("Expected turn item"); |
| 250 | + return { |
| 251 | + ...input, |
| 252 | + event: { |
| 253 | + ...input.event, |
| 254 | + runId: undefined, |
| 255 | + payload: { |
| 256 | + ...input.event.payload, |
| 257 | + runId: null, |
| 258 | + ordinal: id === "a" ? 1 : 2, |
| 259 | + completedAt: completed ? now : null, |
| 260 | + }, |
| 261 | + }, |
| 262 | + }; |
| 263 | + }; |
| 264 | + const events = [ |
| 265 | + item(1, "a", "A"), |
| 266 | + item(2, "b", "B"), |
| 267 | + item(3, "a", "AA"), |
| 268 | + item(4, "a", "Final A", true), |
| 269 | + item(5, "b", "Final B", true), |
| 270 | + ]; |
| 271 | + const emptyProjection = { |
| 272 | + thread: { |
| 273 | + id: threadId, |
| 274 | + projectId: ProjectId.make("project-reducer"), |
| 275 | + title: "Reducer", |
| 276 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 277 | + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 278 | + runtimeMode: "full-access", |
| 279 | + interactionMode: "default", |
| 280 | + branch: null, |
| 281 | + worktreePath: null, |
| 282 | + activeProviderThreadId: null, |
| 283 | + lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null }, |
| 284 | + forkedFrom: null, |
| 285 | + createdBy: "user", |
| 286 | + creationSource: "web", |
| 287 | + createdAt: now, |
| 288 | + updatedAt: now, |
| 289 | + archivedAt: null, |
| 290 | + settledOverride: null, |
| 291 | + settledAt: null, |
| 292 | + lastVisitedAt: null, |
| 293 | + deletedAt: null, |
| 294 | + }, |
| 295 | + runs: [], |
| 296 | + attempts: [], |
| 297 | + nodes: [], |
| 298 | + subagents: [], |
| 299 | + providerSessions: [], |
| 300 | + providerThreads: [], |
| 301 | + providerTurns: [], |
| 302 | + runtimeRequests: [], |
| 303 | + messages: [], |
| 304 | + plans: [], |
| 305 | + turnItems: [], |
| 306 | + checkpointScopes: [], |
| 307 | + checkpoints: [], |
| 308 | + contextHandoffs: [], |
| 309 | + contextTransfers: [], |
| 310 | + visibleTurnItems: [], |
| 311 | + updatedAt: now, |
| 312 | + } satisfies OrchestrationV2ThreadProjection; |
| 313 | + const apply = (inputs: ReadonlyArray<ThreadEvent>, partialTimeline: boolean) => { |
| 314 | + let projection: OrchestrationV2ThreadProjection = emptyProjection; |
| 315 | + let latestLocalTurnOrdinal = 0; |
| 316 | + for (const { event } of inputs) { |
| 317 | + const next = applyOrchestrationV2ProjectionEvent(projection, event, { |
| 318 | + partialTimeline, |
| 319 | + latestLocalTurnOrdinal, |
| 320 | + }); |
| 321 | + if (next === null || next === projection) continue; |
| 322 | + projection = next; |
| 323 | + if (partialTimeline && event.type === "turn-item.updated") { |
| 324 | + latestLocalTurnOrdinal = Math.max(latestLocalTurnOrdinal, event.payload.ordinal); |
| 325 | + } |
| 326 | + } |
| 327 | + return projection; |
| 328 | + }; |
| 329 | + const survivors = yield* Stream.fromIterable(events).pipe( |
| 330 | + coalesceThreadLiveStream, |
| 331 | + Stream.filter((input): input is ThreadEvent => input.kind === "event"), |
| 332 | + Stream.runCollect, |
| 333 | + ); |
| 334 | + for (const partialTimeline of [true, false]) { |
| 335 | + const expected = apply(events, partialTimeline); |
| 336 | + expect( |
| 337 | + expected.turnItems.map((item) => [item.id, "text" in item ? item.text : null]), |
| 338 | + ).toEqual([ |
| 339 | + ["a", "Final A"], |
| 340 | + ["b", "Final B"], |
| 341 | + ]); |
| 342 | + expect(apply(survivors, partialTimeline)).toEqual(expected); |
| 343 | + for (const [index, survivor] of survivors.entries()) { |
| 344 | + const received = survivors.slice(0, index + 1); |
| 345 | + const replay = yield* Stream.fromIterable( |
| 346 | + events.filter((event) => event.sequence > survivor.sequence), |
| 347 | + ).pipe( |
| 348 | + coalesceThreadLiveStream, |
| 349 | + Stream.filter((input): input is ThreadEvent => input.kind === "event"), |
| 350 | + Stream.runCollect, |
| 351 | + ); |
| 352 | + expect(apply([...received, ...replay], partialTimeline)).toEqual(expected); |
| 353 | + } |
| 354 | + } |
| 355 | + }), |
| 356 | + ); |
| 357 | + |
| 358 | + it.effect("flushes text at synchronization and completion boundaries", () => |
| 359 | + Effect.scoped( |
| 360 | + Effect.gen(function* () { |
| 361 | + const coalescer = yield* makeThreadLiveEventCoalescer(); |
| 362 | + const events = [ |
| 363 | + makeStreamingMessage(1), |
| 364 | + makeTextItem(2, { type: "reasoning" }), |
| 365 | + makeStreamingMessage(3), |
| 366 | + makeTextItem(4, { type: "reasoning" }), |
| 367 | + { kind: "synchronized" } as const, |
| 368 | + makeStreamingMessage(5), |
| 369 | + makeTextItem(6, { type: "reasoning" }), |
| 370 | + makeStreamingMessage(7, { streaming: false, text: "Final answer" }), |
| 371 | + makeTextItem(8, { |
| 372 | + type: "reasoning", |
| 373 | + streaming: false, |
| 374 | + status: "completed", |
| 375 | + text: "Final answer", |
| 376 | + }), |
| 377 | + makeStreamingMessage(9), |
| 378 | + makeTextItem(10, { type: "reasoning" }), |
| 379 | + ]; |
| 380 | + yield* coalescer.offerAll(events); |
| 381 | + yield* coalescer.end; |
| 382 | + expect(yield* Stream.runCollect(coalescer.stream)).toEqual(events.slice(2)); |
| 383 | + expect(yield* coalescer.usage).toEqual({ retainedItems: 0, retainedSerializedBytes: 0 }); |
| 384 | + }), |
| 385 | + ), |
| 386 | + ); |
| 387 | + |
| 388 | + it.effect( |
| 389 | + "flushes growing text on the fixed window and charges only unacknowledged survivors", |
| 390 | + () => |
| 391 | + Effect.scoped( |
| 392 | + Effect.gen(function* () { |
| 393 | + const coalescer = yield* makeThreadLiveEventCoalescer(); |
| 394 | + const first = [makeStreamingMessage(1, { text: "é" }), makeTextItem(2, { text: "é" })]; |
| 395 | + const latest = [makeStreamingMessage(3, { text: "éé" }), makeTextItem(4, { text: "éé" })]; |
| 396 | + const serializedBytes = (events: ReadonlyArray<ThreadEvent>) => |
| 397 | + events.reduce((sum, event) => sum + Buffer.byteLength(encodeEvent(event)), 0); |
| 398 | + yield* coalescer.offerAll(first); |
| 399 | + yield* TestClock.adjust("40 millis"); |
| 400 | + yield* coalescer.offerAll(latest); |
| 401 | + expect(yield* coalescer.usage).toEqual({ |
| 402 | + retainedItems: 4, |
| 403 | + retainedSerializedBytes: serializedBytes([...first, ...latest]), |
| 404 | + }); |
| 405 | + yield* TestClock.adjust("10 millis"); |
| 406 | + const expectedUsage = { |
| 407 | + retainedItems: 2, |
| 408 | + retainedSerializedBytes: serializedBytes(latest), |
| 409 | + }; |
| 410 | + expect(yield* coalescer.usage).toEqual(expectedUsage); |
| 411 | + const pull = yield* Stream.toPull(coalescer.stream); |
| 412 | + expect(yield* pull).toEqual(latest); |
| 413 | + expect(yield* coalescer.usage).toEqual(expectedUsage); |
| 414 | + |
| 415 | + const next = [makeStreamingMessage(5), makeTextItem(6)]; |
| 416 | + yield* coalescer.offerAll(next); |
| 417 | + yield* TestClock.adjust("50 millis"); |
| 418 | + expect(yield* pull).toEqual(next); |
| 419 | + yield* coalescer.end; |
| 420 | + yield* pull.pipe(Effect.result); |
| 421 | + expect(yield* coalescer.usage).toEqual({ retainedItems: 0, retainedSerializedBytes: 0 }); |
| 422 | + }), |
| 423 | + ), |
| 424 | + ); |
| 425 | + |
128 | 426 | it.effect("keeps the flush timer alive when an offer's shorter scope closes", () => |
129 | 427 | Effect.scoped( |
130 | 428 | Effect.gen(function* () { |
|