Open History to see the last commit
1import {
2 EnvironmentId,
3 EventId,
4 MessageId,
5 type OrchestrationV2DomainEvent,
6 type OrchestrationV2ThreadProjection,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import * as Option from "effect/Option";
10import { HttpClient, HttpClientResponse } from "effect/unstable/http";
11import { bench, describe } from "vite-plus/test";
12
13import { issueRemoteWebSocketTicket } from "./authorization/remote.ts";
14import { PrimaryConnectionTarget } from "./connection/model.ts";
15import { fetchRemoteEnvironmentDescriptor } from "./environment/descriptor.ts";
16import type { RemoteEnvironmentRequestError } from "./rpc/http.ts";
17import { fetchEnvironmentThreadSnapshot } from "./state/threadSnapshotHttp.ts";
18import { applyOrchestrationV2ProjectionEvent } from "./state/orchestrationV2Projection.ts";
19import { v2Projection, v2Now } from "./state/orchestrationV2TestFixtures.ts";
20
21const timestamp = "2026-09-01T00:00:00.000Z";
22const thread: OrchestrationV2ThreadProjection = {
23 ...v2Projection,
24 messages: Array.from({ length: 100 }, (_, index) => ({
25 id: MessageId.make(`message-${index}`),
26 threadId: v2Projection.thread.id,
27 runId: null,
28 nodeId: null,
29 role: "assistant",
30 text: "Message text. ".repeat(40),
31 attachments: [],
32 streaming: false,
33 createdBy: "agent",
34 creationSource: "provider",
35 createdAt: v2Now,
36 updatedAt: v2Now,
37 })),
38};
39const target = new PrimaryConnectionTarget({
40 environmentId: EnvironmentId.make("remote-1"),
41 label: "Remote",
42 httpBaseUrl: "https://remote.example.test",
43 wsBaseUrl: "wss://remote.example.test/ws",
44});
45const responses = {
46 "/.well-known/t3/environment": {
47 environmentId: target.environmentId,
48 label: target.label,
49 platform: { os: "linux", arch: "x64" },
50 serverVersion: "0.0.0-test",
51 capabilities: { repositoryIdentity: true },
52 },
53 "/api/auth/websocket-ticket": { ticket: "test-ticket", expiresAt: timestamp },
54 "/api/orchestration/threads/thread-v2": { snapshotSequence: 1, projection: thread },
55};
56const httpClient = HttpClient.make((request) =>
57 Effect.sync(() => {
58 const path = new URL(request.url).pathname as keyof typeof responses;
59 return HttpClientResponse.fromWeb(request, Response.json(responses[path]));
60 }),
61);
62const requests: Record<
63 string,
64 Effect.Effect<unknown, RemoteEnvironmentRequestError, HttpClient.HttpClient>
65> = {
66 "read remote connection descriptor": fetchRemoteEnvironmentDescriptor({
67 httpBaseUrl: target.httpBaseUrl,
68 }),
69 "issue remote WebSocket ticket": issueRemoteWebSocketTicket({
70 httpBaseUrl: target.httpBaseUrl,
71 bearerToken: "test-token",
72 }),
73 "load remote snapshot with 100 messages": fetchEnvironmentThreadSnapshot({
74 prepared: {
75 environmentId: target.environmentId,
76 label: target.label,
77 httpBaseUrl: target.httpBaseUrl,
78 socketUrl: target.wsBaseUrl,
79 httpAuthorization: null,
80 target,
81 },
82 threadId: thread.thread.id,
83 signer: Option.none(),
84 }),
85};
86
87describe("remote HTTP processing with an in-memory transport", () => {
88 for (const [name, request] of Object.entries(requests)) {
89 bench(
90 name,
91 async () => {
92 await Effect.runPromise(
93 request.pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
94 );
95 },
96 { warmupTime: 1_000, time: 1_500 },
97 );
98 }
99});
100
101const delta: Extract<OrchestrationV2DomainEvent, { type: "message.updated" }> = {
102 id: EventId.make("delta"),
103 type: "message.updated",
104 threadId: thread.thread.id,
105 occurredAt: v2Now,
106 payload: { ...thread.messages[99]!, text: " next", streaming: true },
107};
108
109describe("remote message replay", () => {
110 for (const count of [100, 1_000]) {
111 const loaded = {
112 ...thread,
113 messages: Array.from({ length: count }, (_, index) => ({
114 ...thread.messages[0]!,
115 id: MessageId.make(`message-${index}`),
116 })),
117 };
118 const event = {
119 ...delta,
120 payload: { ...delta.payload, id: loaded.messages.at(-1)!.id },
121 };
122 bench(
123 `apply 200 message updates to ${count} loaded messages`,
124 () => {
125 let current: OrchestrationV2ThreadProjection = loaded;
126 for (let index = 0; index < 200; index += 1) {
127 current =
128 applyOrchestrationV2ProjectionEvent(current, {
129 ...event,
130 payload: { ...event.payload, text: ` next ${index}` },
131 }) ?? current;
132 }
133 },
134 { warmupTime: 1_000, time: 1_500 },
135 );
136 }
137});