Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
53 changes: 53 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
const releaseHandler = yield* Deferred.make<void>();
const laterChunkPulled = yield* Deferred.make<void>();
const laterNotificationHandled = yield* Deferred.make<void>();
const handled: Array<string> = [];
// 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();
Expand Down
58 changes: 29 additions & 29 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Cause.Done<void>>();
// Unbounded so stdout keeps draining while a handler runs; Codex disconnects on a full pipe.
const incoming = yield* Queue.unbounded<string, CodexError.CodexAppServerError | Cause.Done>();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const incomingNotifications =
yield* Queue.sliding<CodexAppServerIncomingNotification>(MAX_BUFFERED_RAW_MESSAGES);
const incomingRequests =
Expand Down Expand Up @@ -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,
);
Expand Down
Loading