The best way to control Claude Code, Codex and more from any computer or phone

main
ts 290 lines 11.7 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as ConfigProvider from "effect/ConfigProvider";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import * as Schema from "effect/Schema";
10 import {
11 startDelayedOtlpCollector,
12 startOtlpCollector,
13 type OtlpCollector,
14 } from "../Utils/OtlpCollector.ts";
15 import OtelEventFlushWorker from "./fixtures/otel-event-flush-worker.ts";
16
17 const { test } = Test.make({ providers: Cloudflare.providers(), dev: true });
18
19 /** The spans an OTLP/JSON trace export carries; other fields are stripped. */
20 const TraceExport = Schema.fromJsonString(
21 Schema.Struct({
22 resourceSpans: Schema.Array(
23 Schema.Struct({
24 scopeSpans: Schema.Array(
25 Schema.Struct({
26 spans: Schema.Array(
27 Schema.Struct({
28 traceId: Schema.String,
29 spanId: Schema.String,
30 parentSpanId: Schema.optional(Schema.String),
31 name: Schema.String,
32 }),
33 ),
34 }),
35 ),
36 }),
37 ),
38 }),
39 );
40
41 /**
42 * The batches carrying the test's own spans, with those spans' names. Local
43 * tools probe every port they see listening (T3 Code sends `GET /` to the
44 * Worker), and the Worker exports those requests too. The test's spans are
45 * its client trace, which the DO RPC events continue; anything else is a
46 * probe's.
47 */
48 const ownBatches = (batches: OtlpCollector["requests"], traceId: string) =>
49 Effect.forEach(batches, (batch) =>
50 Schema.decodeUnknownEffect(TraceExport)(batch.body).pipe(
51 Effect.map(({ resourceSpans }) => {
52 const records = resourceSpans
53 .flatMap((resource) => resource.scopeSpans.flatMap((scope) => scope.spans))
54 .filter((span) => span.traceId === traceId);
55 return { batch, records, spans: records.map((span) => span.name) };
56 }),
57 ),
58 ).pipe(Effect.map((decoded) => decoded.filter(({ spans }) => spans.length > 0)));
59
60 test.provider(
61 "delivers Worker and Durable Object OTLP batches without delaying the Worker response",
62 (stack) =>
63 Effect.gen(function* () {
64 // Hold only Worker exports: the DO may flush in either foreground or background.
65 const collector = yield* startOtlpCollector({
66 holdResponse: (body) => body.includes('"name":"otel-event-flush.worker"'),
67 });
68 const currentConfig = yield* ConfigProvider.ConfigProvider;
69 yield* stack.destroy();
70 const deployment = yield* stack
71 .deploy(
72 Effect.gen(function* () {
73 const worker = yield* OtelEventFlushWorker;
74 return { url: worker.url };
75 }),
76 )
77 .pipe(
78 Effect.provideService(
79 ConfigProvider.ConfigProvider,
80 ConfigProvider.orElse(
81 ConfigProvider.fromUnknown({
82 OTLP_EVENT_FLUSH_URL: `${collector.url}/v1/traces`,
83 }),
84 currentConfig,
85 ),
86 ),
87 );
88
89 if (deployment.url === undefined) {
90 return yield* Effect.die("OTLP event flush test Worker URL unavailable");
91 }
92 // The test's requests share one fresh trace, told apart from probes.
93 const trace = yield* Effect.makeSpanScoped("telemetry-local.client", { root: true });
94 const own = Effect.suspend(() => ownBatches(collector.requests, trace.traceId));
95 const completed = own.pipe(
96 Effect.map((batches) => batches.filter(({ batch }) => batch.completed).length),
97 );
98 const client = yield* HttpClient.HttpClient;
99 const response = yield* client.get(deployment.url).pipe(Effect.withParentSpan(trace));
100 expect(response.status).toBe(200);
101 expect(yield* response.text).toBe("worker-saw:durable-object-ok");
102
103 // Release only after the response, not on a timer that can outlive
104 // the exporter's shutdown deadline under runner load.
105 expect(yield* completed).toBeLessThanOrEqual(1);
106 yield* Effect.sync(collector.releaseResponses);
107
108 // Both batches must be acknowledged before the next event.
109 yield* completed.pipe(
110 Effect.repeat({
111 schedule: Schedule.spaced("100 millis"),
112 until: (count) => count >= 2,
113 times: 30,
114 }),
115 );
116 expect(yield* completed).toBe(2);
117
118 // Same contract for the Durable Object RPC event path: the Worker's
119 // own batch (the 4th) must not be complete at response time.
120 yield* Effect.sync(collector.holdResponses);
121 const rpcResponse = yield* client
122 .get(`${deployment.url}/rpc`)
123 .pipe(Effect.withParentSpan(trace));
124 expect(rpcResponse.status).toBe(200);
125 expect(yield* rpcResponse.text).toBe("worker-saw:durable-object-rpc-ok");
126 expect(yield* completed).toBeLessThanOrEqual(3);
127 yield* Effect.sync(collector.releaseResponses);
128
129 yield* completed.pipe(
130 Effect.repeat({
131 schedule: Schedule.spaced("100 millis"),
132 until: (count) => count >= 4,
133 times: 30,
134 }),
135 );
136 const batches = yield* own;
137 // The DO RPC event continues the caller's trace, parented on the
138 // Worker span that made the call, as the DO fetch event does.
139 const spans = batches.flatMap(({ records }) => records);
140 const rpcSpan = spans.find((span) => span.name === "otel-event-flush.rpc");
141 expect(rpcSpan?.traceId).toBe(trace.traceId);
142 expect(spans.find((span) => span.spanId === rpcSpan?.parentSpanId)?.name).toBe(
143 "otel-event-flush.worker",
144 );
145 expect(batches).toHaveLength(4);
146 expect(batches.every(({ batch }) => batch.completed && !batch.aborted)).toBe(true);
147 for (const [name, count] of [
148 ["otel-event-flush.worker", 2],
149 ["otel-event-flush.child", 1],
150 ["otel-event-flush.rpc", 1],
151 ["GET", 3],
152 ] as const) {
153 expect(batches.filter(({ spans }) => spans.includes(name))).toHaveLength(count);
154 }
155
156 yield* stack.destroy();
157 expect(yield* completed).toBe(4);
158 }),
159 {
160 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
161 timeout: 120_000,
162 },
163 );
164
165 for (const scenario of [
166 { name: "bound default", shutdownTimeout: 0, delivered: true },
167 { name: "custom 3 seconds", shutdownTimeout: 3_000, delivered: false },
168 { name: "custom 10 seconds", shutdownTimeout: 10_000, delivered: true },
169 ]) {
170 test.provider(
171 `DO RPC exporter deadline with a four-second acknowledgement (${scenario.name})`,
172 (stack) =>
173 Effect.gen(function* () {
174 yield* stack.destroy();
175 const collector = yield* startDelayedOtlpCollector();
176 const currentConfig = yield* ConfigProvider.ConfigProvider;
177 const deployment = yield* stack
178 .deploy(
179 Effect.gen(function* () {
180 const worker = yield* OtelEventFlushWorker;
181 return { url: worker.url };
182 }),
183 )
184 .pipe(
185 Effect.provideService(
186 ConfigProvider.ConfigProvider,
187 ConfigProvider.orElse(
188 ConfigProvider.fromUnknown({
189 OTLP_EVENT_FLUSH_URL: `${collector.url}/v1/traces`,
190 OTLP_EVENT_FLUSH_SHUTDOWN_TIMEOUT: `${scenario.shutdownTimeout} millis`,
191 }),
192 currentConfig,
193 ),
194 ),
195 );
196 if (deployment.url === undefined) {
197 return yield* Effect.fail(new Error("OTLP deadline Worker URL unavailable"));
198 }
199 const trace = yield* Effect.makeSpanScoped("telemetry-local.client", { root: true });
200 const own = Effect.suspend(() => ownBatches(collector.requests, trace.traceId));
201 const client = yield* HttpClient.HttpClient;
202 const startedAt = yield* Effect.sync(() => Date.now());
203 const responseFiber = yield* Effect.gen(function* () {
204 const response = yield* client.get(`${deployment.url}/rpc`);
205 const body = yield* response.text;
206 const receivedAt = yield* Effect.sync(() => Date.now());
207 return { status: response.status, body, receivedAt };
208 }).pipe(Effect.withParentSpan(trace), Effect.forkChild());
209
210 const rpcBatch = yield* own.pipe(
211 Effect.map(
212 (batches) => batches.find(({ spans }) => spans.includes("otel-event-flush.rpc"))?.batch,
213 ),
214 Effect.repeat({
215 schedule: Schedule.spaced("500 millis"),
216 until: (batch) => batch !== undefined,
217 times: 10,
218 }),
219 );
220 if (rpcBatch === undefined) {
221 const response = yield* Fiber.join(responseFiber).pipe(Effect.timeout("10 seconds"));
222 yield* Effect.logError("Missing DO RPC export", {
223 scenario,
224 response,
225 batches: collector.requests,
226 });
227 return yield* Effect.fail(new Error("The collector never received the DO RPC batch"));
228 }
229 const response = yield* Fiber.join(responseFiber).pipe(Effect.timeout("10 seconds"));
230 expect(response.status).toBe(200);
231 expect(response.body).toBe("worker-saw:durable-object-rpc-ok");
232 yield* own.pipe(
233 Effect.map(
234 (batches) =>
235 batches.length === 2 && batches.every(({ batch }) => batch.closedAt !== undefined),
236 ),
237 Effect.repeat({
238 schedule: Schedule.spaced("1 second"),
239 until: (settled) => settled,
240 times: 10,
241 }),
242 );
243 const batches = yield* own;
244 yield* Effect.log("Native DO RPC exporter deadline", {
245 scenario: scenario.name,
246 workerResponseMillis: response.receivedAt - startedAt,
247 workerResponseAfterRpcReceiptMillis: response.receivedAt - rpcBatch.receivedAt,
248 rpcSocketLifetimeMillis:
249 rpcBatch.closedAt === undefined ? undefined : rpcBatch.closedAt - rpcBatch.receivedAt,
250 batches: batches.map(({ batch, spans }) => ({
251 spans,
252 receivedAt: batch.receivedAt,
253 responseStartedAt: batch.responseStartedAt,
254 closedAt: batch.closedAt,
255 completed: batch.completed,
256 aborted: batch.aborted,
257 })),
258 });
259 expect(batches).toHaveLength(2);
260 const names = batches.flatMap(({ spans }) => spans).sort();
261 expect(names).toEqual(["GET", "otel-event-flush.rpc", "otel-event-flush.worker"]);
262 expect(rpcBatch.closedAt).toBeTypeOf("number");
263 expect(response.receivedAt).toBeLessThan(rpcBatch.closedAt!);
264 expect(rpcBatch.completed).toBe(scenario.delivered);
265 expect(rpcBatch.aborted).toBe(!scenario.delivered);
266 expect(batches.filter(({ batch }) => batch.completed)).toHaveLength(
267 scenario.delivered ? 2 : 1,
268 );
269 const socketLifetime = rpcBatch.closedAt! - rpcBatch.receivedAt;
270 if (scenario.delivered) {
271 expect(rpcBatch.responseStartedAt).toBeTypeOf("number");
272 expect(response.receivedAt).toBeLessThan(rpcBatch.responseStartedAt!);
273 expect(socketLifetime).toBeGreaterThanOrEqual(3_900);
274 expect(socketLifetime).toBeLessThan(10_000);
275 } else {
276 expect(rpcBatch.responseStartedAt).toBeUndefined();
277 expect(socketLifetime).toBeGreaterThanOrEqual(2_500);
278 expect(socketLifetime).toBeLessThan(4_000);
279 }
280 const workerBatch = batches.find(({ batch }) => batch !== rpcBatch)!.batch;
281 expect(workerBatch.completed).toBe(true);
282 expect(workerBatch.aborted).toBe(false);
283 yield* stack.destroy();
284 }).pipe(Effect.scoped),
285 {
286 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
287 timeout: 120_000,
288 },
289 );
290 }