diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 09ec3034a9bc9..d63e04b65f3ca 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -4764,15 +4764,20 @@ describe("CodexAdapterV2 post-settle continuation", () => { }; }), ); + // Holding item/completed in its handler means the earlier delta was already handled + // and its flush timer started, so one clock step shows the streaming update. + let completedCount = 0; const harness = yield* makeCodexReplayHarness( transcript, undefined, undefined, undefined, - (entry) => { - const gate = streamed - ? gates.find((gate) => entry.label === `item/completed/${gate.id}`) - : undefined; + undefined, + undefined, + undefined, + (method) => { + const gate = + streamed && method === "item/completed" ? gates[completedCount++] : undefined; return gate === undefined ? Effect.void : Deferred.succeed(gate.ready, undefined).pipe( diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index b4c3ac7b7558b..b168861decb4e 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -464,6 +464,59 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { }), ); + it.effect("keeps reading input in order while a notification handler is still running", () => + Effect.gen(function* () { + const { stdio, input, output } = yield* makeInMemoryStdio(); + const handlerStarted = yield* Deferred.make(); + const releaseHandler = yield* Deferred.make(); + const laterChunkPulled = yield* Deferred.make(); + const laterNotificationHandled = yield* Deferred.make(); + const handled: Array = []; + // stdin.pipe(Stream.interruptWhen) already holds one chunk ahead of the reader. + const heldChunk = encodeJsonl({ id: 1, result: { threadId: "thread-1" } }); + const laterChunk = encodeJsonl({ method: "turn/started", params: {} }); + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio: Stdio.make({ + ...stdio, + stdin: stdio.stdin.pipe( + Stream.tap((chunk) => + chunk === laterChunk ? Deferred.succeed(laterChunkPulled, undefined) : Effect.void, + ), + ), + }), + onNotification: (notification) => + Effect.gen(function* () { + if (notification.method === "thread/started") { + yield* Deferred.succeed(handlerStarted, undefined); + yield* Deferred.await(releaseHandler); + } + handled.push(notification.method); + if (notification.method === "turn/started") { + yield* Deferred.succeed(laterNotificationHandled, undefined); + } + }), + }); + const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped); + yield* Queue.take(output); + + yield* Queue.offer(input, encodeJsonl({ method: "thread/started", params: {} })); + yield* Deferred.await(handlerStarted); + yield* Queue.offer(input, heldChunk); + yield* Queue.offer(input, laterChunk); + + yield* Deferred.await(laterChunkPulled); + assert.isUndefined( + pending.pollUnsafe(), + "Expected the response to wait for the earlier notification handler", + ); + + yield* Deferred.succeed(releaseHandler, undefined); + assert.deepEqual(yield* Fiber.join(pending), { threadId: "thread-1" }); + yield* Deferred.await(laterNotificationHandled); + assert.deepEqual(handled, ["thread/started", "turn/started"]); + }), + ); + it.effect("rejects incoming requests after the active handler limit is reached", () => Effect.gen(function* () { const { stdio, input, output } = yield* makeInMemoryStdio(); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index fdf0863af0a16..407a7e2783e14 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -164,6 +164,8 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa const protocolScope = yield* Scope.Scope; const requestHandlerScope = yield* Scope.fork(protocolScope, "parallel"); const outgoing = yield* Queue.unbounded>(); + // Unbounded so stdout keeps draining while a handler runs; Codex disconnects on a full pipe. + const incoming = yield* Queue.unbounded(); const incomingNotifications = yield* Queue.sliding(MAX_BUFFERED_RAW_MESSAGES); const incomingRequests = @@ -439,42 +441,40 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa const line = remainder.join("").replace(/\r$/, ""); remainder.length = 0; remainderBytes = 0; - yield* handleLine(line); + yield* Queue.offer(incoming, line); start = newline + 1; } }), ), Effect.matchEffect({ onFailure: (error) => - handleTermination(() => - Effect.succeed(normalizeIncomingError(error, "read-input-stream")), - ), + Queue.fail(incoming, normalizeIncomingError(error, "read-input-stream")), onSuccess: () => - Effect.sync(() => { - const line = remainder.join(""); - remainder.length = 0; - return line; - }).pipe( - Effect.flatMap(handleLine), - Effect.matchEffect({ - onFailure: (error) => handleTermination(() => Effect.succeed(error)), - onSuccess: () => - handleTermination(() => { - const ended = new CodexError.CodexAppServerInputStreamEndedError({}); - // Node ends stdout before it reports the exit of a crashed process, so - // wait briefly for the exit status. stdout can also close while the - // process keeps running, so the wait must not hang pending requests. - return options.terminationError === undefined - ? Effect.succeed(ended) - : options.terminationError.pipe( - Effect.timeoutOrElse({ - duration: EXIT_STATUS_GRACE, - orElse: () => Effect.succeed(ended), - }), - ); - }), - }), - ), + Queue.offer(incoming, remainder.join("")).pipe(Effect.andThen(Queue.end(incoming))), + }), + Effect.forkScoped, + ); + + yield* Stream.fromQueue(incoming).pipe( + Stream.interruptWhen(Deferred.await(terminationSignal)), + Stream.runForEach(handleLine), + Effect.matchEffect({ + onFailure: (error) => handleTermination(() => Effect.succeed(error)), + onSuccess: () => + handleTermination(() => { + const ended = new CodexError.CodexAppServerInputStreamEndedError({}); + // Node ends stdout before it reports the exit of a crashed process, so + // wait briefly for the exit status. stdout can also close while the + // process keeps running, so the wait must not hang pending requests. + return options.terminationError === undefined + ? Effect.succeed(ended) + : options.terminationError.pipe( + Effect.timeoutOrElse({ + duration: EXIT_STATUS_GRACE, + orElse: () => Effect.succeed(ended), + }), + ); + }), }), Effect.forkScoped, );