@@ -4,7 +4,7 @@ import * as PlatformError from "effect/PlatformError";
44import { ChildProcessSpawner } from "effect/process" ;
55
66import * as CodexError from "../errors.ts" ;
7- import { makeTerminationError } from "./stdio.ts" ;
7+ import { appendStderrTail , makeTerminationError , stderrExcerpt } from "./stdio.ts" ;
88
99describe ( "Codex App Server child process termination" , ( ) => {
1010 it . effect ( "retains the process identifier and exact exit-status cause" , ( ) =>
@@ -16,10 +16,13 @@ describe("Codex App Server child process termination", () => {
1616 method : "exitCode" ,
1717 cause : rootCause ,
1818 } ) ;
19- const error = yield * makeTerminationError ( {
20- pid : ChildProcessSpawner . ProcessId ( 52 ) ,
21- exitCode : Effect . fail ( cause ) ,
22- } ) ;
19+ const error = yield * makeTerminationError (
20+ {
21+ pid : ChildProcessSpawner . ProcessId ( 52 ) ,
22+ exitCode : Effect . fail ( cause ) ,
23+ } ,
24+ Effect . succeed ( "" ) ,
25+ ) ;
2326
2427 assert . instanceOf ( error , CodexError . CodexAppServerTransportError ) ;
2528 assert . equal ( error . pid , 52 ) ;
@@ -31,4 +34,102 @@ describe("Codex App Server child process termination", () => {
3134 assert . notInclude ( error . message , rootCause . message ) ;
3235 } ) ,
3336 ) ;
37+
38+ it . effect ( "names the stderr error line when the process exits" , ( ) =>
39+ Effect . gen ( function * ( ) {
40+ const error = yield * makeTerminationError (
41+ {
42+ pid : ChildProcessSpawner . ProcessId ( 53 ) ,
43+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 1 ) ) ,
44+ } ,
45+ Effect . succeed ( "Error: Missing optional dependency @openai/codex-darwin-arm64.\n" ) ,
46+ ) ;
47+
48+ assert . instanceOf ( error , CodexError . CodexAppServerProcessExitedError ) ;
49+ assert . equal ( error . stderr , "Error: Missing optional dependency @openai/codex-darwin-arm64." ) ;
50+ assert . equal (
51+ error . message ,
52+ "Codex App Server process exited with code 1: Error: Missing optional dependency @openai/codex-darwin-arm64." ,
53+ ) ;
54+ } ) ,
55+ ) ;
56+
57+ it . effect ( "keeps the bare exit message when stderr is empty" , ( ) =>
58+ Effect . gen ( function * ( ) {
59+ const error = yield * makeTerminationError (
60+ {
61+ pid : ChildProcessSpawner . ProcessId ( 54 ) ,
62+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 1 ) ) ,
63+ } ,
64+ Effect . succeed ( " \n\n" ) ,
65+ ) ;
66+
67+ assert . instanceOf ( error , CodexError . CodexAppServerProcessExitedError ) ;
68+ assert . isUndefined ( error . stderr ) ;
69+ assert . equal ( error . message , "Codex App Server process exited with code 1" ) ;
70+ } ) ,
71+ ) ;
72+ } ) ;
73+
74+ describe ( "Codex App Server stderr excerpt" , ( ) => {
75+ it . each ( [
76+ {
77+ name : "Node launcher missing its platform package" ,
78+ stderr : [
79+ "file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:107" ,
80+ " throw new Error(" ,
81+ " ^" ,
82+ "" ,
83+ "Error: Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest" ,
84+ " at findCodexExecutable (file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:107:9)" ,
85+ " at file:///usr/local/lib/node_modules/@openai/codex/bin/codex.js:112:20" ,
86+ "" ,
87+ "Node.js v26.8.2" ,
88+ "" ,
89+ ] . join ( "\n" ) ,
90+ excerpt :
91+ "Error: Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest" ,
92+ } ,
93+ {
94+ name : "Node error with a code" ,
95+ stderr :
96+ "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" ,
97+ excerpt : "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'x'" ,
98+ } ,
99+ {
100+ name : "Rust CLI error" ,
101+ stderr : "error: unexpected argument 'app-server' found\n\nUsage: codex [OPTIONS]\n" ,
102+ excerpt : "error: unexpected argument 'app-server' found" ,
103+ } ,
104+ {
105+ name : "unlabelled message" ,
106+ stderr : "WARNING: proceeding without PATH aliases\nMissing OpenAI API key.\n" ,
107+ excerpt : "Missing OpenAI API key." ,
108+ } ,
109+ {
110+ name : "ANSI color, carriage returns and repeated whitespace" ,
111+ stderr : "\u001b[31m Error:\t config.toml is invalid \u001b[0m\r\n" ,
112+ excerpt : "Error: config.toml is invalid" ,
113+ } ,
114+ {
115+ name : "oversized line" ,
116+ stderr : `Error: ${ "x" . repeat ( 1_000 ) } ` ,
117+ excerpt : `Error: ${ "x" . repeat ( 504 ) } …` ,
118+ } ,
119+ ] ) ( "$name" , ( { stderr, excerpt } ) => {
120+ assert . equal ( stderrExcerpt ( stderr ) , excerpt ) ;
121+ assert . isAtMost ( excerpt . length , 512 ) ;
122+ } ) ;
123+
124+ it ( "returns nothing for blank stderr" , ( ) => {
125+ assert . isUndefined ( stderrExcerpt ( "\n \t\r\n\u001b[0m\n" ) ) ;
126+ } ) ;
127+
128+ it ( "keeps only the newest stderr" , ( ) => {
129+ const tail = appendStderrTail ( "a" . repeat ( 8_000 ) , `${ "b" . repeat ( 400 ) } \nError: last` ) ;
130+
131+ assert . equal ( tail . length , 8_192 ) ;
132+ assert . isTrue ( tail . endsWith ( "Error: last" ) ) ;
133+ assert . equal ( stderrExcerpt ( tail ) , "Error: last" ) ;
134+ } ) ;
34135} ) ;
0 commit comments