The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
perf(codex): trace app server request latency (#28666)
Yash Singh committed
Nov 1, 2026 at 07:06 UTC
2b1021ce411db0f0aade4c513ab2bb4b0cdcaf94
2 files changed
+113
-10
packages/effect-codex-app-server/src/protocol.test.ts
+90
@@ -7,6 +7,7 @@ import * as Schema from "effect/Schema";
7
import * as Sink from "effect/Sink";
8
import * as Stdio from "effect/Stdio";
9
import * as Stream from "effect/Stream";
10
+import * as Tracer from "effect/Tracer";
11
import * as TestClock from "effect/testing/TestClock";
12
13
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -186,6 +187,95 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
187
}),
188
);
189
190
+ it.effect(
191
+ "correlates concurrent request spans through responses, failures and interruption",
192
+ () => {
193
+ const spans: Array<Tracer.NativeSpan> = [];
194
+ const tracer = Tracer.make({
195
+ span: (options) => {
196
+ const span = new Tracer.NativeSpan(options);
197
+ spans.push(span);
198
+ return span;
199
+ },
200
+ });
201
+ return Effect.gen(function* () {
202
+ const { stdio, input, output } = yield* makeInMemoryStdio();
203
+ const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio });
204
+ const methods = ["model/list", "account/read", "skills/list"];
205
+ const requests = [];
206
+ for (const method of methods) {
207
+ requests.push(
208
+ yield* transport.request(method, { secret: "private-payload" }).pipe(Effect.forkScoped),
209
+ );
210
+ yield* Queue.take(output);
211
+ }
212
+ yield* Queue.offer(
213
+ input,
214
+ encodeJsonl({ id: 2, error: { code: -32602, message: "Invalid params" } }),
215
+ );
216
+ yield* Fiber.await(requests[1]!);
217
+ yield* Queue.offer(input, encodeJsonl({ id: 1, result: { secret: "private-result" } }));
218
+ yield* Fiber.join(requests[0]!);
219
+ yield* Fiber.interrupt(requests[2]!);
220
+ // A late response for an interrupted request must not affect the next request.
221
+ yield* Queue.offer(input, encodeJsonl({ id: 3, result: {} }));
222
+ const next = yield* transport.request("config/read").pipe(Effect.forkScoped);
223
+ yield* Queue.take(output);
224
+ yield* Queue.offer(input, encodeJsonl({ id: 4, result: {} }));
225
+ yield* Fiber.join(next);
226
+ for (const [index, method] of [...methods, "config/read"].entries()) {
227
+ const request = spans.find(
228
+ (span) =>
229
+ span.name === "CodexAppServerProtocol.request" &&
230
+ span.attributes.get("rpc.request_id") === index + 1,
231
+ );
232
+ assert.isDefined(request);
233
+ assert.equal(request!.attributes.get("rpc.method"), method);
234
+ assert.equal(request!.attributes.get("rpc.system"), "jsonrpc");
235
+ assert.equal(request!.status._tag, "Ended");
236
+ if (request!.status._tag === "Ended") {
237
+ assert.equal(
238
+ request!.status.exit._tag,
239
+ index === 1 || index === 2 ? "Failure" : "Success",
240
+ );
241
+ }
242
+ for (const phase of ["enqueue", "awaitResponse"]) {
243
+ const child: Tracer.NativeSpan | undefined = spans.find(
244
+ (span) =>
245
+ span.name === `CodexAppServerProtocol.request.${phase}` &&
246
+ span.parent?._tag === "Some" &&
247
+ span.parent.value.spanId === request!.spanId,
248
+ );
249
+ assert.isDefined(child);
250
+ assert.equal(child!.traceId, request!.traceId);
251
+ assert.equal(child!.attributes.get("rpc.method"), method);
252
+ assert.equal(child!.attributes.get("rpc.request_id"), index + 1);
253
+ assert.equal(child!.status._tag, "Ended");
254
+ }
255
+ }
256
+ const failedSend = yield* transport
257
+ .request("thread/start", { value: 1n })
258
+ .pipe(Effect.exit);
259
+ assert.equal(failedSend._tag, "Failure");
260
+ const sendSpans = spans.filter((span) => span.attributes.get("rpc.request_id") === 5);
261
+ assert.deepEqual(
262
+ sendSpans.map((span) => span.name),
263
+ ["CodexAppServerProtocol.request", "CodexAppServerProtocol.request.enqueue"],
264
+ );
265
+ for (const span of sendSpans) {
266
+ assert.equal(span.attributes.get("rpc.method"), "thread/start");
267
+ assert.equal(span.status._tag, "Ended");
268
+ if (span.status._tag === "Ended") assert.equal(span.status.exit._tag, "Failure");
269
+ }
270
+ for (const span of spans) {
271
+ const attrs = JSON.stringify([...span.attributes]);
272
+ assert.notInclude(attrs, "private-payload");
273
+ assert.notInclude(attrs, "private-result");
274
+ }
275
+ }).pipe(Effect.withTracer(tracer));
276
+ },
277
+ );
278
+
279
it.effect.each([1, 7, 1024])(
280
"preserves JSONL framing and UTF-8 across %i-byte input chunks",
281
(chunkSize) =>
packages/effect-codex-app-server/src/protocol.ts
+23
-10
@@ -502,17 +502,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
502
nextRequestId,
503
(current) => [current, current + 1] as const,
504
);
505
+ const attributes = {
506
+ "rpc.system": "jsonrpc",
507
+ "rpc.method": method,
508
+ "rpc.request_id": requestId,
509
+ };
510
+ yield* Effect.annotateCurrentSpan(attributes);
511
const deferred = yield* Deferred.make<unknown, CodexError.CodexAppServerError>();
506
- yield* Ref.update(pending, (current) =>
507
- new Map(current).set(String(requestId), { deferred, method }),
508
- );
509
- yield* offerOutgoing({
510
- id: requestId,
511
- method,
512
- ...(payload !== undefined ? { params: payload } : {}),
513
- }).pipe(Effect.tapError(() => removePending(String(requestId))));
514
- return yield* Deferred.await(deferred).pipe(
515
- Effect.onInterrupt(() => removePending(String(requestId))),
512
+ return yield* Effect.acquireUseRelease(
513
+ Ref.update(pending, (current) =>
514
+ new Map(current).set(String(requestId), { deferred, method }),
515
+ ),
516
+ () =>
517
+ Effect.gen(function* () {
518
+ yield* offerOutgoing({
519
+ id: requestId,
520
+ method,
521
+ ...(payload !== undefined ? { params: payload } : {}),
522
+ }).pipe(Effect.withSpan("CodexAppServerProtocol.request.enqueue", { attributes }));
523
+ // Includes queue/stdin delivery, Codex work and response routing, not just T3 CPU.
524
+ return yield* Deferred.await(deferred).pipe(
525
+ Effect.withSpan("CodexAppServerProtocol.request.awaitResponse", { attributes }),
526
+ );
527
+ }),
528
+ () => removePending(String(requestId)),
529
);
530
});
531