Skip to content
TypeScript

perf(codex): trace app server request latency (#28666)

Yash Singh2b1021ce411d
diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts
index 351f0d2b877..b4c3ac7b755 100644
--- a/packages/effect-codex-app-server/src/protocol.test.ts
+++ b/packages/effect-codex-app-server/src/protocol.test.ts
@@ -7,6 +7,7 @@ import * as Schema from "effect/Schema";
import * as Sink from "effect/Sink";
import * as Stdio from "effect/Stdio";
import * as Stream from "effect/Stream";
+import * as Tracer from "effect/Tracer";
import * as TestClock from "effect/testing/TestClock";
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -186,6 +187,95 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
}),
);
+ it.effect(
+ "correlates concurrent request spans through responses, failures and interruption",
+ () => {
+ const spans: Array<Tracer.NativeSpan> = [];
+ const tracer = Tracer.make({
+ span: (options) => {
+ const span = new Tracer.NativeSpan(options);
+ spans.push(span);
+ return span;
+ },
+ });
+ return Effect.gen(function* () {
+ const { stdio, input, output } = yield* makeInMemoryStdio();
+ const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio });
+ const methods = ["model/list", "account/read", "skills/list"];
+ const requests = [];
+ for (const method of methods) {
+ requests.push(
+ yield* transport.request(method, { secret: "private-payload" }).pipe(Effect.forkScoped),
+ );
+ yield* Queue.take(output);
+ }
+ yield* Queue.offer(
+ input,
+ encodeJsonl({ id: 2, error: { code: -32602, message: "Invalid params" } }),
+ );
+ yield* Fiber.await(requests[1]!);
+ yield* Queue.offer(input, encodeJsonl({ id: 1, result: { secret: "private-result" } }));
+ yield* Fiber.join(requests[0]!);
+ yield* Fiber.interrupt(requests[2]!);
+ // A late response for an interrupted request must not affect the next request.
+ yield* Queue.offer(input, encodeJsonl({ id: 3, result: {} }));
+ const next = yield* transport.request("config/read").pipe(Effect.forkScoped);
+ yield* Queue.take(output);
+ yield* Queue.offer(input, encodeJsonl({ id: 4, result: {} }));
+ yield* Fiber.join(next);
+ for (const [index, method] of [...methods, "config/read"].entries()) {
+ const request = spans.find(
+ (span) =>
+ span.name === "CodexAppServerProtocol.request" &&
+ span.attributes.get("rpc.request_id") === index + 1,
+ );
+ assert.isDefined(request);
+ assert.equal(request!.attributes.get("rpc.method"), method);
+ assert.equal(request!.attributes.get("rpc.system"), "jsonrpc");
+ assert.equal(request!.status._tag, "Ended");
+ if (request!.status._tag === "Ended") {
+ assert.equal(
+ request!.status.exit._tag,
+ index === 1 || index === 2 ? "Failure" : "Success",
+ );
+ }
+ for (const phase of ["enqueue", "awaitResponse"]) {
+ const child: Tracer.NativeSpan | undefined = spans.find(
+ (span) =>
+ span.name === `CodexAppServerProtocol.request.${phase}` &&
+ span.parent?._tag === "Some" &&
+ span.parent.value.spanId === request!.spanId,
+ );
+ assert.isDefined(child);
+ assert.equal(child!.traceId, request!.traceId);
+ assert.equal(child!.attributes.get("rpc.method"), method);
+ assert.equal(child!.attributes.get("rpc.request_id"), index + 1);
+ assert.equal(child!.status._tag, "Ended");
+ }
+ }
+ const failedSend = yield* transport
+ .request("thread/start", { value: 1n })
+ .pipe(Effect.exit);
+ assert.equal(failedSend._tag, "Failure");
+ const sendSpans = spans.filter((span) => span.attributes.get("rpc.request_id") === 5);
+ assert.deepEqual(
+ sendSpans.map((span) => span.name),
+ ["CodexAppServerProtocol.request", "CodexAppServerProtocol.request.enqueue"],
+ );
+ for (const span of sendSpans) {
+ assert.equal(span.attributes.get("rpc.method"), "thread/start");
+ assert.equal(span.status._tag, "Ended");
+ if (span.status._tag === "Ended") assert.equal(span.status.exit._tag, "Failure");
+ }
+ for (const span of spans) {
+ const attrs = JSON.stringify([...span.attributes]);
+ assert.notInclude(attrs, "private-payload");
+ assert.notInclude(attrs, "private-result");
+ }
+ }).pipe(Effect.withTracer(tracer));
+ },
+ );
+
it.effect.each([1, 7, 1024])(
"preserves JSONL framing and UTF-8 across %i-byte input chunks",
(chunkSize) =>
diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts
index c628079d0b4..fdf0863af0a 100644
--- a/packages/effect-codex-app-server/src/protocol.ts
+++ b/packages/effect-codex-app-server/src/protocol.ts
@@ -502,17 +502,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
nextRequestId,
(current) => [current, current + 1] as const,
);
+ const attributes = {
+ "rpc.system": "jsonrpc",
+ "rpc.method": method,
+ "rpc.request_id": requestId,
+ };
+ yield* Effect.annotateCurrentSpan(attributes);
const deferred = yield* Deferred.make<unknown, CodexError.CodexAppServerError>();
- yield* Ref.update(pending, (current) =>
- new Map(current).set(String(requestId), { deferred, method }),
- );
- yield* offerOutgoing({
- id: requestId,
- method,
- ...(payload !== undefined ? { params: payload } : {}),
- }).pipe(Effect.tapError(() => removePending(String(requestId))));
- return yield* Deferred.await(deferred).pipe(
- Effect.onInterrupt(() => removePending(String(requestId))),
+ return yield* Effect.acquireUseRelease(
+ Ref.update(pending, (current) =>
+ new Map(current).set(String(requestId), { deferred, method }),
+ ),
+ () =>
+ Effect.gen(function* () {
+ yield* offerOutgoing({
+ id: requestId,
+ method,
+ ...(payload !== undefined ? { params: payload } : {}),
+ }).pipe(Effect.withSpan("CodexAppServerProtocol.request.enqueue", { attributes }));
+ // Includes queue/stdin delivery, Codex work and response routing, not just T3 CPU.
+ return yield* Deferred.await(deferred).pipe(
+ Effect.withSpan("CodexAppServerProtocol.request.awaitResponse", { attributes }),
+ );
+ }),
+ () => removePending(String(requestId)),
);
});