From 047eb4e29a504171aa80d76cbc815a8b0a660c0b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 17 Apr 2026 19:32:18 -0700 Subject: [PATCH] Normalize Codex IDs and preserve streamed stdout decoding - Use strict TurnId and ProviderItemId parsing in Codex session routing - Decode in-memory stdio chunks in streaming mode to avoid split UTF-8 corruption --- .../src/provider/codex/CodexSessionRuntime.ts | 34 +++++++------------ .../src/_internal/stdio.ts | 5 ++- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/apps/server/src/provider/codex/CodexSessionRuntime.ts b/apps/server/src/provider/codex/CodexSessionRuntime.ts index 5a355d9615..4a35e8f7ff 100644 --- a/apps/server/src/provider/codex/CodexSessionRuntime.ts +++ b/apps/server/src/provider/codex/CodexSessionRuntime.ts @@ -379,14 +379,6 @@ function classifyCodexStderrLine(rawLine: string): { readonly message: string } return { message: line }; } -function toTurnId(value: string | null | undefined): TurnId | undefined { - return value?.trim() ? TurnId.make(value) : undefined; -} - -function toItemId(value: string | null | undefined): ProviderItemId | undefined { - return value?.trim() ? ProviderItemId.make(value) : undefined; -} - function readNotificationThreadId(notification: CodexServerNotification): string | undefined { switch (notification.method) { case "thread/started": @@ -446,18 +438,18 @@ function readRouteFields(notification: CodexServerNotification): { case "turn/started": case "turn/completed": return { - turnId: toTurnId(notification.params.turn.id), + turnId: TurnId.make(notification.params.turn.id), itemId: undefined, }; case "error": return { - turnId: toTurnId(notification.params.turnId), + turnId: TurnId.make(notification.params.turnId), itemId: undefined, }; case "turn/diff/updated": case "turn/plan/updated": return { - turnId: toTurnId(notification.params.turnId), + turnId: TurnId.make(notification.params.turnId), itemId: undefined, }; case "serverRequest/resolved": @@ -468,8 +460,8 @@ function readRouteFields(notification: CodexServerNotification): { case "item/started": case "item/completed": return { - turnId: toTurnId(notification.params.turnId), - itemId: toItemId(notification.params.item.id), + turnId: TurnId.make(notification.params.turnId), + itemId: ProviderItemId.make(notification.params.item.id), }; case "item/agentMessage/delta": case "item/plan/delta": @@ -480,8 +472,8 @@ function readRouteFields(notification: CodexServerNotification): { case "item/reasoning/summaryPartAdded": case "item/reasoning/textDelta": return { - turnId: toTurnId(notification.params.turnId), - itemId: toItemId(notification.params.itemId), + turnId: TurnId.make(notification.params.turnId), + itemId: ProviderItemId.make(notification.params.itemId), }; default: return { @@ -838,8 +830,8 @@ export const makeCodexSessionRuntime = ( yield* client.handleServerRequest("item/commandExecution/requestApproval", (payload) => Effect.gen(function* () { const requestId = ApprovalRequestId.make(randomUUID()); - const turnId = toTurnId(payload.turnId); - const itemId = toItemId(payload.itemId); + const turnId = TurnId.make(payload.turnId); + const itemId = ProviderItemId.make(payload.itemId); const decision = yield* Deferred.make(); yield* Ref.update(pendingApprovalsRef, (current) => { @@ -894,8 +886,8 @@ export const makeCodexSessionRuntime = ( yield* client.handleServerRequest("item/fileChange/requestApproval", (payload) => Effect.gen(function* () { const requestId = ApprovalRequestId.make(randomUUID()); - const turnId = toTurnId(payload.turnId); - const itemId = toItemId(payload.itemId); + const turnId = TurnId.make(payload.turnId); + const itemId = ProviderItemId.make(payload.itemId); const decision = yield* Deferred.make(); yield* Ref.update(pendingApprovalsRef, (current) => { @@ -950,8 +942,8 @@ export const makeCodexSessionRuntime = ( yield* client.handleServerRequest("item/tool/requestUserInput", (payload) => Effect.gen(function* () { const requestId = ApprovalRequestId.make(randomUUID()); - const turnId = toTurnId(payload.turnId); - const itemId = toItemId(payload.itemId); + const turnId = TurnId.make(payload.turnId); + const itemId = ProviderItemId.make(payload.itemId); const answers = yield* Deferred.make(); yield* Ref.update(pendingUserInputsRef, (current) => { diff --git a/packages/effect-codex-app-server/src/_internal/stdio.ts b/packages/effect-codex-app-server/src/_internal/stdio.ts index 84a03e3a74..7698198dca 100644 --- a/packages/effect-codex-app-server/src/_internal/stdio.ts +++ b/packages/effect-codex-app-server/src/_internal/stdio.ts @@ -32,7 +32,10 @@ export const makeInMemoryStdio = Effect.fn("makeInMemoryStdio")(function* () { stdin: Stream.fromQueue(input), stdout: () => Sink.forEach((chunk: string | Uint8Array) => - Queue.offer(output, typeof chunk === "string" ? chunk : decoder.decode(chunk)), + Queue.offer( + output, + typeof chunk === "string" ? chunk : decoder.decode(chunk, { stream: true }), + ), ), stderr: () => Sink.drain, }),