The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 14:30 UTC
fix(server): Codex works again after its app-server is killed (#26188)
Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 27, 2026 at 16:27 UTC
b88da91397c247b98c892831d7e759f72ea4db43
4 files changed
+192
-2
apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+10
-1
@@ -99,7 +99,16 @@ const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTran
99
const text = yield* fs.readFileString(
100
decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
101
);
102
- return yield* decodeProviderReplayNdjson(text.replaceAll(queuedFollowupResult, followupResult));
102
+ const transcript = yield* decodeProviderReplayNdjson(
103
+ text.replaceAll(queuedFollowupResult, followupResult),
104
+ );
105
+ // The recording ends when the recorder closes the app-server while the parent
106
+ // turn is still open. This test keeps acting as that parent afterwards, and
107
+ // replaying the exit would kill the shared process and fail the parent run.
108
+ return {
109
+ ...transcript,
110
+ entries: transcript.entries.filter((entry) => entry.type !== "runtime_exit"),
111
+ };
112
}, Effect.provide(NodeServices.layer));
113
114
function makeProviderSnapshot(input: {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+158
@@ -31,6 +31,7 @@ import {
31
import { assert, describe, it } from "@effect/vitest";
32
import * as CodexClient from "effect-codex-app-server/client";
33
import * as CodexReplay from "effect-codex-app-server/replay";
34
+import * as CodexError from "effect-codex-app-server/errors";
35
import type * as CodexSchema from "effect-codex-app-server/schema";
36
import * as DateTime from "effect/DateTime";
37
import * as Deferred from "effect/Deferred";
@@ -47,6 +48,11 @@ import * as Queue from "effect/Queue";
48
import * as Ref from "effect/Ref";
49
import * as Schema from "effect/Schema";
50
import * as Stream from "effect/Stream";
51
+import * as Sink from "effect/Sink";
52
+import * as Stdio from "effect/Stdio";
53
+import * as Exit from "effect/Exit";
54
+import * as Cause from "effect/Cause";
55
+import * as PlatformError from "effect/PlatformError";
56
import { TestClock } from "effect/testing";
57
import { ChildProcess, ChildProcessSpawner } from "effect/process";
58
@@ -67,6 +73,9 @@ const replayTranscriptJson = Schema.fromJsonString(CodexReplay.CodexAppServerRep
73
const encodeReplayTranscriptJson = Schema.encodeEffect(replayTranscriptJson);
74
const decodeReplayTranscriptJson = Schema.decodeUnknownEffect(replayTranscriptJson);
75
const encodeStringJson = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
76
+const isProviderAdapterEventStreamError = Schema.is(
77
+ ProviderAdapter.ProviderAdapterEventStreamError,
78
+);
79
80
describe("CodexAdapterV2 context usage", () => {
81
it("uses the current context rather than cumulative processed tokens", () => {
@@ -609,6 +618,7 @@ function codexReplayPreamble(input: {
618
thread: {
619
id: input.nativeThreadId,
620
sessionId: input.nativeThreadId,
621
+ projectId: null,
622
forkedFromId: null,
623
preview: "",
624
ephemeral: false,
@@ -702,6 +712,154 @@ function withReplayRequestId(
712
: entry;
713
}
714
715
+describe("CodexAdapterV2 process exit", () => {
716
+ it.effect.each([
717
+ { exit: "numeric", active: false },
718
+ { exit: "SIGKILL", active: false },
719
+ { exit: "numeric", active: true },
720
+ { exit: "SIGKILL", active: true },
721
+ ] as const)("retires the shared session after $exit (active turn: $active)", ({ exit, active }) =>
722
+ Effect.gen(function* () {
723
+ const scenario = `process-exit-${exit}-${active}`;
724
+ const nativeThreadId = `native-${scenario}`;
725
+ const nativeTurnId = `turn-${scenario}`;
726
+ const input = yield* Queue.unbounded<Uint8Array, Cause.Done>();
727
+ const emit = (frame: unknown) =>
728
+ Queue.offer(input, new TextEncoder().encode(JSON.stringify(frame) + "\n"));
729
+ const outputEnded = yield* Deferred.make<void>();
730
+ const turnStarted = yield* Deferred.make<void>();
731
+ const turnCompleted = yield* Deferred.make<void>();
732
+ const events: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
733
+ const preamble = codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "work" });
734
+ const terminationError =
735
+ exit === "numeric"
736
+ ? new CodexError.CodexAppServerProcessExitedError({ code: 17, pid: 52 })
737
+ : new CodexError.CodexAppServerTransportError({
738
+ operation: "read-process-exit-status",
739
+ pid: 52,
740
+ cause: PlatformError.systemError({
741
+ _tag: "Unknown",
742
+ module: "ChildProcess",
743
+ method: "exitCode",
744
+ cause: new Error("Process terminated by SIGKILL"),
745
+ }),
746
+ });
747
+ const client = yield* CodexClient.make(
748
+ Stdio.make({
749
+ args: Effect.succeed([]),
750
+ stdin: Stream.fromQueue(input),
751
+ stdout: () =>
752
+ Sink.forEach((chunk: string | Uint8Array) =>
753
+ Effect.gen(function* () {
754
+ const frame = JSON.parse(
755
+ typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk),
756
+ ) as { readonly id?: number; readonly method: string };
757
+ const response = preamble.find(
758
+ (entry) =>
759
+ entry.type === "emit_inbound" &&
760
+ entry.label === frame.method &&
761
+ Predicate.isObject(entry.frame) &&
762
+ "id" in entry.frame,
763
+ );
764
+ if (response?.type === "emit_inbound") {
765
+ yield* emit(response.frame);
766
+ }
767
+ }),
768
+ ).pipe(Sink.ensuring(Deferred.succeed(outputEnded, undefined))),
769
+ stderr: () => Sink.drain,
770
+ }),
771
+ {},
772
+ Effect.succeed(terminationError),
773
+ );
774
+ const adapter = yield* CodexAdapterV2.makeCodexAdapterV2({
775
+ instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID,
776
+ settings: DEFAULT_CODEX_SETTINGS,
777
+ environment: {},
778
+ clientFactory: { open: () => Effect.succeed(client) },
779
+ crypto: yield* Crypto.Crypto,
780
+ fileSystem: yield* FileSystem.FileSystem,
781
+ idAllocator: yield* IdAllocator.IdAllocatorV2,
782
+ serverConfig: yield* makeReplayServerConfig(scenario).pipe(Effect.orDie),
783
+ });
784
+ const threadId = ThreadId.make(`thread-${scenario}`);
785
+ const runtime = yield* adapter.openSession({
786
+ threadId,
787
+ providerSessionId: ProviderSessionId.make(`session-${scenario}`),
788
+ modelSelection: CODEX_TEST_MODEL_SELECTION,
789
+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
790
+ });
791
+ const eventReader = yield* runtime.events.pipe(
792
+ Stream.runForEach((event) =>
793
+ Effect.sync(() => events.push(event)).pipe(
794
+ Effect.andThen(
795
+ event.type === "provider_turn.updated" && event.providerTurn.status === "running"
796
+ ? Deferred.succeed(turnStarted, undefined)
797
+ : event.type === "turn.terminal"
798
+ ? Deferred.succeed(turnCompleted, undefined)
799
+ : Effect.void,
800
+ ),
801
+ ),
802
+ ),
803
+ Effect.forkScoped,
804
+ );
805
+ const providerThread = yield* runtime.ensureThread({
806
+ threadId,
807
+ modelSelection: CODEX_TEST_MODEL_SELECTION,
808
+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
809
+ });
810
+ yield* runtime.startTurn(
811
+ makeCodexTestTurnInput({
812
+ threadId,
813
+ providerThread,
814
+ now: yield* DateTime.now,
815
+ attemptId: RunAttemptId.make(`attempt-${scenario}`),
816
+ text: "work",
817
+ }),
818
+ );
819
+ yield* emit({
820
+ method: "turn/started",
821
+ params: {
822
+ threadId: nativeThreadId,
823
+ turn: makeCodexReplayTurn({ id: nativeTurnId, status: "inProgress" }),
824
+ },
825
+ });
826
+ yield* Deferred.await(turnStarted);
827
+ if (!active) {
828
+ yield* emit({
829
+ method: "turn/completed",
830
+ params: {
831
+ threadId: nativeThreadId,
832
+ turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
833
+ },
834
+ });
835
+ yield* Deferred.await(turnCompleted);
836
+ assert.isTrue(
837
+ events.some((event) => event.type === "turn.terminal" && event.status === "completed"),
838
+ );
839
+ }
840
+
841
+ yield* Queue.end(input);
842
+ // Output closes only after the protocol records the process termination.
843
+ yield* Deferred.await(outputEnded);
844
+ const rpcError = yield* client.request("thread/list", {}).pipe(Effect.flip);
845
+ assert.strictEqual(rpcError, terminationError);
846
+ // Without the fix the stream never ends, so this waits until the project timeout.
847
+ const readerExit = yield* Fiber.await(eventReader);
848
+ // A failure, not a clean end, carries the Codex error into the session's lastError.
849
+ assert.isTrue(
850
+ Exit.isFailure(readerExit) &&
851
+ isProviderAdapterEventStreamError(Cause.squash(readerExit.cause)),
852
+ "the session manager must receive process death so it can retire the shared process",
853
+ );
854
+ }).pipe(
855
+ Effect.scoped,
856
+ Effect.provide(
857
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
858
+ ),
859
+ ),
860
+ );
861
+});
862
+
863
describe("CodexAdapterV2 session initialize", () => {
864
const openReplaySession = (
865
transcript: CodexReplay.CodexAppServerReplayTranscript,
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+19
-1
@@ -1746,7 +1746,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1746
model: input.modelSelection.model,
1747
now,
1748
});
1749
- const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
1749
+ const events = yield* Queue.unbounded<
1750
+ ProviderAdapter.ProviderAdapterV2Event,
1751
+ ProviderAdapter.ProviderAdapterEventStreamError
1752
+ >();
1753
+ // Drain final notifications before the session manager retires the dead process.
1754
+ // Queue.fail lets readers take items already buffered before they see the failure.
1755
+ yield* client.awaitTermination.pipe(
1756
+ Effect.catch((cause) =>
1757
+ Queue.fail(
1758
+ events,
1759
+ new ProviderAdapter.ProviderAdapterEventStreamError({
1760
+ driver: CODEX_PROVIDER,
1761
+ providerSessionId: input.providerSessionId,
1762
+ cause,
1763
+ }),
1764
+ ),
1765
+ ),
1766
+ Effect.forkScoped,
1767
+ );
1768
const rateLimitSnapshot = yield* Ref.make<CodexRateLimitSnapshot | undefined>(undefined);
1769
const limitedTurnItems = yield* Ref.make(
1770
new Map<ProviderThreadId, Extract<OrchestrationV2TurnItem, { type: "error" }>>(),
packages/effect-codex-app-server/src/client.ts
+5
@@ -1,4 +1,5 @@
1
import * as Context from "effect/Context";
2
+import * as Deferred from "effect/Deferred";
3
import * as Effect from "effect/Effect";
4
import * as Layer from "effect/Layer";
5
import * as Schema from "effect/Schema";
@@ -40,6 +41,7 @@ export class CodexAppServerClient extends Context.Service<
41
CodexAppServerClient,
42
{
43
readonly raw: CodexAppServerClientRaw;
44
+ readonly awaitTermination: Effect.Effect<never, CodexError.CodexAppServerError>;
45
readonly request: <M extends CodexRpc.ClientRequestMethod>(
46
method: M,
47
payload: CodexRpc.ClientRequestParamsByMethod[M],
@@ -100,6 +102,7 @@ export const make = Effect.fn("effect-codex-app-server/CodexAppServerClient.make
102
terminationError?: Effect.Effect<CodexError.CodexAppServerError>,
103
): Effect.fn.Return<CodexAppServerClient["Service"], never, Scope.Scope> {
104
const requestHandlers = new Map<string, ServerRequestHandler>();
105
+ const termination = yield* Deferred.make<never, CodexError.CodexAppServerError>();
106
const notificationHandlers = new Map<string, Array<ServerNotificationHandler>>();
107
let unknownRequestHandler:
108
| ((method: string, params: unknown) => Effect.Effect<unknown, CodexError.CodexAppServerError>)
@@ -203,6 +206,7 @@ export const make = Effect.fn("effect-codex-app-server/CodexAppServerClient.make
206
...(options.logger ? { logger: options.logger } : {}),
207
onNotification: dispatchNotification,
208
onRequest: dispatchRequest,
209
+ onTermination: (error) => Deferred.fail(termination, error).pipe(Effect.asVoid),
210
});
211
212
const request = <M extends CodexRpc.ClientRequestMethod>(
@@ -230,6 +234,7 @@ export const make = Effect.fn("effect-codex-app-server/CodexAppServerClient.make
234
);
235
236
return CodexAppServerClient.of({
237
+ awaitTermination: Deferred.await(termination),
238
raw: {
239
notifications: transport.incomingNotifications,
240
requests: transport.incomingRequests,