Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commands.performance.test.ts

275 lines9.4 KB

1import {
2 CommandId,
3 EnvironmentId,
4 MessageId,
5 ORCHESTRATION_V2_WS_METHODS,
6 OrchestrationV2RpcSchemas,
7 TurnItemId,
8 type OrchestrationV2Command,
9 type OrchestrationV2ProjectedTurnItem,
10 type OrchestrationV2ThreadProjection,
11 type OrchestrationV2TurnItem,
12} from "@t3tools/contracts";
13import { describe, expect, it } from "@effect/vitest";
14import * as SecureRandom from "effect/crypto/SecureRandom";
15import * as Effect from "effect/Effect";
16import * as Layer from "effect/Layer";
17import * as Option from "effect/Option";
18import * as Schema from "effect/Schema";
19import * as SubscriptionRef from "effect/SubscriptionRef";
20import type * as RpcMessage from "effect/rpc/RpcMessage";
21​
22import {
23 AVAILABLE_CONNECTION_STATE,
24 PrimaryConnectionTarget,
25 type PreparedConnection,
26} from "../connection/model.ts";
27import * as EnvironmentSupervisor from "../connection/supervisor.ts";
28import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
29import * as RpcSession from "../rpc/session.ts";
30import { v2Now, v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts";
31import { startThreadTurn } from "./commands.ts";
32​
33const ROW_COUNT = 600;
34const OUTPUT_BYTES_PER_ROW = 8_192;
35​
36const HISTORICAL_FULL_PROJECTION_APPLICATION_BYTES = 10_371_419;
37const HISTORICAL_FORMER_SEND_APPLICATION_BYTES = 10_371_678;
38const FORMER_SEND_RPC_JSON_BYTES = 10_372_184;
39const MAX_CURRENT_SEND_RPC_JSON_BYTES = 1_024;
40​
41const RPC_TRACE_CONTEXT = {
42 traceId: "0".repeat(32),
43 spanId: "0".repeat(16),
44 sampled: true,
45} as const;
46​
47const encodeGetThreadProjectionInput = Schema.encodeSync(
48 Schema.toCodecJson(OrchestrationV2RpcSchemas.getThreadProjection.input),
49);
50const encodeGetThreadProjectionOutput = Schema.encodeSync(
51 Schema.toCodecJson(OrchestrationV2RpcSchemas.getThreadProjection.output),
52);
53const encodeDispatchCommandInput = Schema.encodeSync(
54 Schema.toCodecJson(OrchestrationV2RpcSchemas.dispatchCommand.input),
55);
56const encodeDispatchCommandOutput = Schema.encodeSync(
57 Schema.toCodecJson(OrchestrationV2RpcSchemas.dispatchCommand.output),
58);
59​
60function encodedBytes(value: unknown): number {
61 return new TextEncoder().encode(JSON.stringify(value)).byteLength;
63​
64function encodedRpcRequestBytes(input: {
65 readonly requestId: number;
66 readonly tag: string;
67 readonly payload: unknown;
68}): number {
69 const request = {
70 _tag: "Request",
71 id: input.requestId,
72 tag: input.tag,
73 payload: input.payload,
74 ...RPC_TRACE_CONTEXT,
75 headers: [],
76 } satisfies RpcMessage.RequestEncoded;
77 return encodedBytes(request);
79​
80function encodedRpcSuccessBytes(input: {
81 readonly requestId: number;
82 readonly value: unknown;
83}): number {
84 const response = {
85 _tag: "Exit",
86 requestId: input.requestId,
87 exit: { _tag: "Success", value: input.value },
88 } satisfies RpcMessage.ResponseExitEncoded;
89 return encodedBytes(response);
91​
92function makeLargeProjection(): OrchestrationV2ThreadProjection {
93 const visibleTurnItems: OrchestrationV2ProjectedTurnItem[] = Array.from(
94 { length: ROW_COUNT },
95 (_, index) => {
96 const item: OrchestrationV2TurnItem = {
97 id: TurnItemId.make(`item-${index}`),
98 type: "command_execution",
99 threadId: v2ThreadId,
100 runId: null,
101 nodeId: null,
102 providerThreadId: null,
103 providerTurnId: null,
104 nativeItemRef: null,
105 parentItemId: null,
106 ordinal: index + 1,
107 status: "completed",
108 title: `Command ${index}`,
109 input: `cmd-${index}`,
110 output: "x".repeat(OUTPUT_BYTES_PER_ROW),
111 exitCode: 0,
112 startedAt: v2Now,
113 completedAt: v2Now,
114 updatedAt: v2Now,
115 };
116 return {
117 position: index,
118 visibility: "local",
119 sourceThreadId: v2ThreadId,
120 sourceItemId: item.id,
121 item,
122 };
123 },
124 );
125​
126 return {
127 ...v2Projection,
128 turnItems: visibleTurnItems.map((row) => row.item),
129 visibleTurnItems,
130 };
132​
133const TARGET = new PrimaryConnectionTarget({
134 environmentId: EnvironmentId.make("environment-perf"),
135 label: "Performance fixture",
136 httpBaseUrl: "https://environment.example.test",
137 wsBaseUrl: "wss://environment.example.test",
138});
139​
140const layerTestRandom = Layer.succeed(
141 SecureRandom.SecureRandom,
142 SecureRandom.make((size) => new Uint8Array(size)),
143);
144​
145const makeSupervisor = Effect.fn("CommandPerformance.makeSupervisor")(function* (input: {
146 readonly projection: OrchestrationV2ThreadProjection;
147 readonly projectionRequests: Array<unknown>;
148 readonly projectionResponseBytes: { value: number };
149 readonly commands: OrchestrationV2Command[];
150}) {
151 const client = {
152 [ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: (requestInput: unknown) =>
153 Effect.sync(() => {
154 input.projectionRequests.push(requestInput);
155 input.projectionResponseBytes.value += encodedBytes(input.projection);
156 return input.projection;
157 }),
158 [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
159 Effect.sync(() => {
160 input.commands.push(command);
161 return { sequence: input.commands.length };
162 }),
163 } as unknown as WsRpcProtocolClient;
164 const session: RpcSession.RpcSession = {
165 client,
166 initialConfig: Effect.succeed({
167 environment: {
168 capabilities: {
169 repositoryIdentity: true,
170 serverResolvedCommandContext: true,
171 },
172 },
173 } as never),
174 subscribeServerConfig: (requestInput = {}) => client.subscribeServerConfig(requestInput),
175 ready: Effect.void,
176 probe: Effect.void,
177 inboundFrames: Effect.succeed(0),
178 awaitingReply: Effect.succeed(false),
179 closed: Effect.never,
180 };
181 return EnvironmentSupervisor.EnvironmentSupervisor.of({
182 target: TARGET,
183 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
184 session: yield* SubscriptionRef.make(Option.some(session)),
185 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
186 connect: Effect.void,
187 disconnect: Effect.void,
188 retryNow: Effect.void,
189 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
190});
191​
192describe("routine command transport budget", () => {
193 it.effect("dispatches an implicit-auto message without a full projection round trip", () =>
194 Effect.gen(function* () {
195 const projection = makeLargeProjection();
196 const projectionRequests: unknown[] = [];
197 const projectionResponseBytes = { value: 0 };
198 const commands: OrchestrationV2Command[] = [];
199 const supervisor = yield* makeSupervisor({
200 projection,
201 projectionRequests,
202 projectionResponseBytes,
203 commands,
204 });
205​
206 const receipt = yield* startThreadTurn({
207 commandId: CommandId.make("perf-send"),
208 threadId: v2ThreadId,
209 message: {
210 messageId: MessageId.make("message-perf"),
211 role: "user",
212 text: "continue",
213 attachments: [],
214 },
215 runtimeMode: "full-access",
216 interactionMode: "default",
217 titleSeed: "continue",
218 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
219​
220 const command = commands[0]!;
221 if (command.type !== "message.dispatch") {
222 throw new Error(`Expected message.dispatch, received ${command.type}`);
223 }
224 if ("projection" in receipt) {
225 throw new Error("Expected a dispatch receipt");
226 }
227 const { deliveryIntent: _deliveryIntent, ...formerCommand } = command;
228​
229 const historicalApplicationBytes =
230 encodedBytes(projection) + encodedBytes(formerCommand) + encodedBytes(receipt);
231 const projectionRequestRpcJsonBytes = encodedRpcRequestBytes({
232 requestId: 0,
233 tag: ORCHESTRATION_V2_WS_METHODS.getThreadProjection,
234 payload: encodeGetThreadProjectionInput({ threadId: v2ThreadId }),
235 });
236 const projectionResponseRpcJsonBytes = encodedRpcSuccessBytes({
237 requestId: 0,
238 value: encodeGetThreadProjectionOutput(projection),
239 });
240 const formerDispatchRequestRpcJsonBytes = encodedRpcRequestBytes({
241 requestId: 1,
242 tag: ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
243 payload: encodeDispatchCommandInput(formerCommand),
244 });
245 const currentDispatchRequestRpcJsonBytes = encodedRpcRequestBytes({
246 requestId: 1,
247 tag: ORCHESTRATION_V2_WS_METHODS.dispatchCommand,
248 payload: encodeDispatchCommandInput(command),
249 });
250 const receiptRpcJsonBytes = encodedRpcSuccessBytes({
251 requestId: 1,
252 value: encodeDispatchCommandOutput(receipt),
253 });
254 const formerRpcJsonBytes =
255 projectionRequestRpcJsonBytes +
256 projectionResponseRpcJsonBytes +
257 formerDispatchRequestRpcJsonBytes +
258 receiptRpcJsonBytes;
259 const currentRpcJsonBytes = currentDispatchRequestRpcJsonBytes + receiptRpcJsonBytes;
260​
261 expect(encodedBytes(projection)).toBe(HISTORICAL_FULL_PROJECTION_APPLICATION_BYTES);
262 expect(historicalApplicationBytes).toBe(HISTORICAL_FORMER_SEND_APPLICATION_BYTES);
263 expect(formerRpcJsonBytes).toBe(FORMER_SEND_RPC_JSON_BYTES);
264 expect(projectionRequests).toEqual([]);
265 expect(projectionResponseBytes.value).toBe(0);
266 expect(command).toMatchObject({
267 type: "message.dispatch",
268 deliveryIntent: "auto",
269 dispatchMode: { type: "start_immediately" },
270 });
271 expect(currentRpcJsonBytes).toBeLessThanOrEqual(MAX_CURRENT_SEND_RPC_JSON_BYTES);
272 expect(currentRpcJsonBytes / formerRpcJsonBytes).toBeLessThanOrEqual(0.000_06);
273 }).pipe(Effect.provide(layerTestRandom)),
274 );
275});