|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + EventId, |
| 4 | + MessageId, |
| 5 | + ProjectId, |
| 6 | + ProviderInstanceId, |
| 7 | + ThreadId, |
| 8 | + type OrchestrationEvent, |
| 9 | + type OrchestrationThread, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as Option from "effect/Option"; |
| 13 | +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; |
| 14 | +import { bench, describe } from "vite-plus/test"; |
| 15 | + |
| 16 | +import { issueRemoteWebSocketTicket } from "./authorization/remote.ts"; |
| 17 | +import { PrimaryConnectionTarget } from "./connection/model.ts"; |
| 18 | +import { fetchRemoteEnvironmentDescriptor } from "./environment/descriptor.ts"; |
| 19 | +import type { RemoteEnvironmentRequestError } from "./rpc/http.ts"; |
| 20 | +import { fetchEnvironmentThreadSnapshot } from "./state/threadSnapshotHttp.ts"; |
| 21 | +import { applyThreadDetailEvent } from "./state/threadReducer.ts"; |
| 22 | + |
| 23 | +const timestamp = "2026-09-01T00:00:00.000Z"; |
| 24 | +const thread: OrchestrationThread = { |
| 25 | + id: ThreadId.make("thread-1"), |
| 26 | + projectId: ProjectId.make("project-1"), |
| 27 | + title: "Remote thread", |
| 28 | + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 29 | + runtimeMode: "full-access", |
| 30 | + interactionMode: "default", |
| 31 | + branch: null, |
| 32 | + worktreePath: null, |
| 33 | + latestTurn: null, |
| 34 | + createdAt: timestamp, |
| 35 | + updatedAt: timestamp, |
| 36 | + archivedAt: null, |
| 37 | + settledOverride: null, |
| 38 | + settledAt: null, |
| 39 | + deletedAt: null, |
| 40 | + pullRequests: [], |
| 41 | + messages: Array.from({ length: 100 }, (_, index) => ({ |
| 42 | + id: MessageId.make(`message-${index}`), |
| 43 | + role: "assistant", |
| 44 | + text: "Message text. ".repeat(40), |
| 45 | + turnId: null, |
| 46 | + streaming: false, |
| 47 | + createdAt: timestamp, |
| 48 | + updatedAt: timestamp, |
| 49 | + })), |
| 50 | + proposedPlans: [], |
| 51 | + activities: [], |
| 52 | + checkpoints: [], |
| 53 | + session: null, |
| 54 | +}; |
| 55 | +const target = new PrimaryConnectionTarget({ |
| 56 | + environmentId: EnvironmentId.make("remote-1"), |
| 57 | + label: "Remote", |
| 58 | + httpBaseUrl: "https://remote.example.test", |
| 59 | + wsBaseUrl: "wss://remote.example.test/ws", |
| 60 | +}); |
| 61 | +const responses = { |
| 62 | + "/.well-known/t3/environment": { |
| 63 | + environmentId: target.environmentId, |
| 64 | + label: target.label, |
| 65 | + platform: { os: "linux", arch: "x64" }, |
| 66 | + serverVersion: "0.0.0-test", |
| 67 | + capabilities: { repositoryIdentity: true }, |
| 68 | + }, |
| 69 | + "/api/auth/websocket-ticket": { ticket: "test-ticket", expiresAt: timestamp }, |
| 70 | + "/api/orchestration/threads/thread-1": { snapshotSequence: 1, thread }, |
| 71 | +}; |
| 72 | +const httpClient = HttpClient.make((request) => |
| 73 | + Effect.sync(() => { |
| 74 | + const path = new URL(request.url).pathname as keyof typeof responses; |
| 75 | + return HttpClientResponse.fromWeb(request, Response.json(responses[path])); |
| 76 | + }), |
| 77 | +); |
| 78 | +const requests: Record< |
| 79 | + string, |
| 80 | + Effect.Effect<unknown, RemoteEnvironmentRequestError, HttpClient.HttpClient> |
| 81 | +> = { |
| 82 | + "read remote connection descriptor": fetchRemoteEnvironmentDescriptor({ |
| 83 | + httpBaseUrl: target.httpBaseUrl, |
| 84 | + }), |
| 85 | + "issue remote WebSocket ticket": issueRemoteWebSocketTicket({ |
| 86 | + httpBaseUrl: target.httpBaseUrl, |
| 87 | + bearerToken: "test-token", |
| 88 | + }), |
| 89 | + "load remote snapshot with 100 messages": fetchEnvironmentThreadSnapshot({ |
| 90 | + prepared: { |
| 91 | + environmentId: target.environmentId, |
| 92 | + label: target.label, |
| 93 | + httpBaseUrl: target.httpBaseUrl, |
| 94 | + socketUrl: target.wsBaseUrl, |
| 95 | + httpAuthorization: null, |
| 96 | + target, |
| 97 | + }, |
| 98 | + threadId: thread.id, |
| 99 | + signer: Option.none(), |
| 100 | + }), |
| 101 | +}; |
| 102 | + |
| 103 | +describe("remote HTTP processing with an in-memory transport", () => { |
| 104 | + for (const [name, request] of Object.entries(requests)) { |
| 105 | + bench( |
| 106 | + name, |
| 107 | + async () => { |
| 108 | + await Effect.runPromise( |
| 109 | + request.pipe(Effect.provideService(HttpClient.HttpClient, httpClient)), |
| 110 | + ); |
| 111 | + }, |
| 112 | + { warmupTime: 1_000, time: 1_500 }, |
| 113 | + ); |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +const delta: OrchestrationEvent = { |
| 118 | + eventId: EventId.make("delta"), |
| 119 | + sequence: 2, |
| 120 | + aggregateKind: "thread", |
| 121 | + aggregateId: thread.id, |
| 122 | + occurredAt: timestamp, |
| 123 | + commandId: null, |
| 124 | + causationEventId: null, |
| 125 | + correlationId: null, |
| 126 | + metadata: {}, |
| 127 | + type: "thread.message-sent", |
| 128 | + payload: { |
| 129 | + threadId: thread.id, |
| 130 | + messageId: MessageId.make("message-99"), |
| 131 | + role: "assistant", |
| 132 | + text: " next", |
| 133 | + turnId: null, |
| 134 | + streaming: true, |
| 135 | + createdAt: timestamp, |
| 136 | + updatedAt: timestamp, |
| 137 | + }, |
| 138 | +}; |
| 139 | + |
| 140 | +describe("remote message replay", () => { |
| 141 | + for (const count of [100, 1_000]) { |
| 142 | + const loaded = { |
| 143 | + ...thread, |
| 144 | + messages: Array.from({ length: count }, (_, index) => ({ |
| 145 | + ...thread.messages[0]!, |
| 146 | + id: MessageId.make(`message-${index}`), |
| 147 | + })), |
| 148 | + }; |
| 149 | + const event = { |
| 150 | + ...delta, |
| 151 | + payload: { ...delta.payload, messageId: loaded.messages.at(-1)!.id }, |
| 152 | + }; |
| 153 | + bench( |
| 154 | + `apply 200 text deltas to ${count} loaded messages`, |
| 155 | + () => { |
| 156 | + let current: OrchestrationThread = loaded; |
| 157 | + for (let index = 0; index < 200; index += 1) { |
| 158 | + const result = applyThreadDetailEvent(current, event); |
| 159 | + if (result.kind === "updated") current = result.thread; |
| 160 | + } |
| 161 | + }, |
| 162 | + { warmupTime: 1_000, time: 1_500 }, |
| 163 | + ); |
| 164 | + } |
| 165 | +}); |
0 commit comments