@@ -31,6 +31,7 @@ import { |
31 | 31 | import { assert, describe, it } from "@effect/vitest"; |
32 | 32 | import * as CodexClient from "effect-codex-app-server/client"; |
33 | 33 | import * as CodexReplay from "effect-codex-app-server/replay"; |
| 34 | +import * as CodexError from "effect-codex-app-server/errors"; |
34 | 35 | import type * as CodexSchema from "effect-codex-app-server/schema"; |
35 | 36 | import * as DateTime from "effect/DateTime"; |
36 | 37 | import * as Deferred from "effect/Deferred"; |
@@ -47,6 +48,11 @@ import * as Queue from "effect/Queue"; |
47 | 48 | import * as Ref from "effect/Ref"; |
48 | 49 | import * as Schema from "effect/Schema"; |
49 | 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"; |
50 | 56 | import { TestClock } from "effect/testing"; |
51 | 57 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
52 | 58 |
|
@@ -67,6 +73,9 @@ const replayTranscriptJson = Schema.fromJsonString(CodexReplay.CodexAppServerRep |
67 | 73 | const encodeReplayTranscriptJson = Schema.encodeEffect(replayTranscriptJson); |
68 | 74 | const decodeReplayTranscriptJson = Schema.decodeUnknownEffect(replayTranscriptJson); |
69 | 75 | const encodeStringJson = Schema.encodeEffect(Schema.fromJsonString(Schema.String)); |
| 76 | +const isProviderAdapterEventStreamError = Schema.is( |
| 77 | + ProviderAdapter.ProviderAdapterEventStreamError, |
| 78 | +); |
70 | 79 |
|
71 | 80 | describe("CodexAdapterV2 context usage", () => { |
72 | 81 | it("uses the current context rather than cumulative processed tokens", () => { |
@@ -609,6 +618,7 @@ function codexReplayPreamble(input: { |
609 | 618 | thread: { |
610 | 619 | id: input.nativeThreadId, |
611 | 620 | sessionId: input.nativeThreadId, |
| 621 | + projectId: null, |
612 | 622 | forkedFromId: null, |
613 | 623 | preview: "", |
614 | 624 | ephemeral: false, |
@@ -702,6 +712,154 @@ function withReplayRequestId( |
702 | 712 | : entry; |
703 | 713 | } |
704 | 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 | + |
705 | 863 | describe("CodexAdapterV2 session initialize", () => { |
706 | 864 | const openReplaySession = ( |
707 | 865 | transcript: CodexReplay.CodexAppServerReplayTranscript, |
|