@@ -7,6 +7,7 @@ import * as Schema from "effect/Schema"; |
7 | 7 | import * as Sink from "effect/Sink"; |
8 | 8 | import * as Stdio from "effect/Stdio"; |
9 | 9 | import * as Stream from "effect/Stream"; |
| 10 | +import * as Tracer from "effect/Tracer"; |
10 | 11 | import * as TestClock from "effect/testing/TestClock"; |
11 | 12 |
|
12 | 13 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
@@ -186,6 +187,95 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { |
186 | 187 | }), |
187 | 188 | ); |
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 | + |
189 | 279 | it.effect.each([1, 7, 1024])( |
190 | 280 | "preserves JSONL framing and UTF-8 across %i-byte input chunks", |
191 | 281 | (chunkSize) => |
|