11import { assert , describe , it } from "@effect/vitest" ;
22import * as Effect from "effect/Effect" ;
3+ import * as Deferred from "effect/Deferred" ;
4+ import * as Fiber from "effect/Fiber" ;
5+ import * as Queue from "effect/Queue" ;
6+ import * as Stream from "effect/Stream" ;
7+ import * as TestClock from "effect/testing/TestClock" ;
38import * as PlatformError from "effect/PlatformError" ;
49import { ChildProcessSpawner } from "effect/process" ;
510
611import * as CodexError from "../errors.ts" ;
7- import { makeTerminationError } from "./stdio.ts" ;
12+ import * as CodexProtocol from "../protocol.ts" ;
13+ import {
14+ appendStderrTail ,
15+ makeTerminationError ,
16+ stderrExcerpt ,
17+ makeInMemoryStdio ,
18+ makeStderrTail ,
19+ makeChildStdio ,
20+ } from "./stdio.ts" ;
821
922describe ( "Codex App Server child process termination" , ( ) => {
23+ it . effect ( "retains exit diagnostics when stderr stays open after stdout ends" , ( ) =>
24+ Effect . gen ( function * ( ) {
25+ const { stdio, input, output } = yield * makeInMemoryStdio ( ) ;
26+ const captured = yield * Deferred . make < void > ( ) ;
27+ const stderr = Stream . make ( new TextEncoder ( ) . encode ( "Error: invalid config\n" ) ) . pipe (
28+ Stream . concat ( Stream . fromEffect ( Deferred . succeed ( captured , undefined ) ) . pipe ( Stream . drain ) ) ,
29+ Stream . concat ( Stream . never ) ,
30+ ) ;
31+ const tail = yield * makeStderrTail ( stderr ) ;
32+ const exited = yield * Deferred . make < ChildProcessSpawner . ExitCode > ( ) ;
33+ const transport = yield * CodexProtocol . makeCodexAppServerPatchedProtocol ( {
34+ stdio : makeChildStdio ( { stdout : stdio . stdin , stdin : stdio . stdout ( ) } , tail . awaitDrained ) ,
35+ terminationError : makeTerminationError (
36+ {
37+ pid : ChildProcessSpawner . ProcessId ( 55 ) ,
38+ exitCode : Deferred . await ( exited ) ,
39+ } ,
40+ tail . read ,
41+ ) ,
42+ } ) ;
43+ const pending = yield * transport . request ( "thread/read" , { } ) . pipe (
44+ Effect . match ( {
45+ onFailure : ( error ) => error ,
46+ onSuccess : ( ) => assert . fail ( "Expected the pending request to fail" ) ,
47+ } ) ,
48+ Effect . forkScoped ,
49+ ) ;
50+ yield * Queue . take ( output ) ;
51+ yield * Deferred . await ( captured ) ;
52+ yield * Queue . end ( input ) ;
53+ yield * TestClock . adjust ( "100 millis" ) ;
54+ yield * Deferred . succeed ( exited , ChildProcessSpawner . ExitCode ( 1 ) ) ;
55+ yield * TestClock . adjust ( "250 millis" ) ;
56+ const error = yield * Fiber . join ( pending ) ;
57+ assert . instanceOf ( error , CodexError . CodexAppServerProcessExitedError ) ;
58+ assert . equal (
59+ error . message ,
60+ "Codex App Server process exited with code 1: Error: invalid config" ,
61+ ) ;
62+ } ) ,
63+ ) ;
64+
1065 it . effect ( "retains the process identifier and exact exit-status cause" , ( ) =>
1166 Effect . gen ( function * ( ) {
1267 const rootCause = new Error ( "private process diagnostics" ) ;
@@ -16,10 +71,13 @@ describe("Codex App Server child process termination", () => {
1671 method : "exitCode" ,
1772 cause : rootCause ,
1873 } ) ;
19- const error = yield * makeTerminationError ( {
20- pid : ChildProcessSpawner . ProcessId ( 52 ) ,
21- exitCode : Effect . fail ( cause ) ,
22- } ) ;
74+ const error = yield * makeTerminationError (
75+ {
76+ pid : ChildProcessSpawner . ProcessId ( 52 ) ,
77+ exitCode : Effect . fail ( cause ) ,
78+ } ,
79+ Effect . succeed ( "" ) ,
80+ ) ;
2381
2482 assert . instanceOf ( error , CodexError . CodexAppServerTransportError ) ;
2583 assert . equal ( error . pid , 52 ) ;
@@ -31,4 +89,112 @@ describe("Codex App Server child process termination", () => {
3189 assert . notInclude ( error . message , rootCause . message ) ;
3290 } ) ,
3391 ) ;
92+
93+ it . effect ( "names the stderr error line when the process exits" , ( ) =>
94+ Effect . gen ( function * ( ) {
95+ const error = yield * makeTerminationError (
96+ {
97+ pid : ChildProcessSpawner . ProcessId ( 53 ) ,
98+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 1 ) ) ,
99+ } ,
100+ Effect . succeed ( "Error: Missing optional dependency @openai/codex-darwin-arm64.\n" ) ,
101+ ) ;
102+
103+ assert . instanceOf ( error , CodexError . CodexAppServerProcessExitedError ) ;
104+ assert . equal ( error . stderr , "Error: Missing optional dependency @openai/codex-darwin-arm64." ) ;
105+ assert . equal (
106+ error . message ,
107+ "Codex App Server process exited with code 1: Error: Missing optional dependency @openai/codex-darwin-arm64." ,
108+ ) ;
109+ } ) ,
110+ ) ;
111+
112+ it . effect ( "keeps the bare exit message when stderr is empty" , ( ) =>
113+ Effect . gen ( function * ( ) {
114+ const error = yield * makeTerminationError (
115+ {
116+ pid : ChildProcessSpawner . ProcessId ( 54 ) ,
117+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 1 ) ) ,
118+ } ,
119+ Effect . succeed ( " \n\n" ) ,
120+ ) ;
121+
122+ assert . instanceOf ( error , CodexError . CodexAppServerProcessExitedError ) ;
123+ assert . isUndefined ( error . stderr ) ;
124+ assert . equal ( error . message , "Codex App Server process exited with code 1" ) ;
125+ } ) ,
126+ ) ;
127+ } ) ;
128+
129+ describe ( "Codex App Server stderr excerpt" , ( ) => {
130+ it . each ( [
131+ {
132+ name : "Node launcher missing its platform package" ,
133+ stderr : [
134+ "file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:107" ,
135+ " throw new Error(" ,
136+ " ^" ,
137+ "" ,
138+ "Error: Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest" ,
139+ " at findCodexExecutable (file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:107:9)" ,
140+ " at file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:112:20" ,
141+ "" ,
142+ "Node.js v26.8.2" ,
143+ "" ,
144+ ] . join ( "\n" ) ,
145+ excerpt :
146+ "Error: Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest" ,
147+ } ,
148+ {
149+ name : "Node error with a code" ,
150+ stderr :
151+ "node:internal/modules/esm/resolve:873\n throw new ERR_MODULE_NOT_FOUND(\n\nError [ERR_MODULE_NOT_FOUND]: Cannot find package 'x'\n at packageResolve (node:internal/modules/esm/resolve:873:9)\n" ,
152+ excerpt : "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'x'" ,
153+ } ,
154+ {
155+ name : "Rust CLI error" ,
156+ stderr : "error: unexpected argument 'app-server' found\n\nUsage: codex [OPTIONS]\n" ,
157+ excerpt : "error: unexpected argument 'app-server' found" ,
158+ } ,
159+ {
160+ name : "uppercase error label" ,
161+ stderr : "ERROR: Missing dependency\nUsage: codex [OPTIONS]\n" ,
162+ excerpt : "ERROR: Missing dependency" ,
163+ } ,
164+ {
165+ name : "unlabelled message" ,
166+ stderr : "WARNING: proceeding without PATH aliases\nMissing OpenAI API key.\n" ,
167+ excerpt : "Missing OpenAI API key." ,
168+ } ,
169+ {
170+ name : "ANSI color, carriage returns and repeated whitespace" ,
171+ stderr : "\u001b[31m Error:\t config.toml is invalid \u001b[0m\r\n" ,
172+ excerpt : "Error: config.toml is invalid" ,
173+ } ,
174+ {
175+ name : "C1 terminal controls" ,
176+ stderr : "\u009b31mError: invalid config\u009b0m\nUsage: codex [OPTIONS]\n" ,
177+ excerpt : "Error: invalid config" ,
178+ } ,
179+ {
180+ name : "oversized line" ,
181+ stderr : `Error: ${ "x" . repeat ( 1_000 ) } ` ,
182+ excerpt : `Error: ${ "x" . repeat ( 504 ) } …` ,
183+ } ,
184+ ] ) ( "$name" , ( { stderr, excerpt } ) => {
185+ assert . equal ( stderrExcerpt ( stderr ) , excerpt ) ;
186+ assert . isAtMost ( excerpt . length , 512 ) ;
187+ } ) ;
188+
189+ it ( "returns nothing for blank stderr" , ( ) => {
190+ assert . isUndefined ( stderrExcerpt ( "\n \t\r\n\u001b[0m\n" ) ) ;
191+ } ) ;
192+
193+ it ( "keeps only the newest stderr" , ( ) => {
194+ const tail = appendStderrTail ( "a" . repeat ( 8_000 ) , `${ "b" . repeat ( 400 ) } \nError: last` ) ;
195+
196+ assert . equal ( tail . length , 8_192 ) ;
197+ assert . isTrue ( tail . endsWith ( "Error: last" ) ) ;
198+ assert . equal ( stderrExcerpt ( tail ) , "Error: last" ) ;
199+ } ) ;
34200} ) ;
0 commit comments