@@ -97,7 +97,12 @@ function inProgressTurnResult() {
9797 } ;
9898}
9999
100- function createClientFactory ( options : { mcpServers ?: unknown [ ] } = { } ) {
100+ function createClientFactory (
101+ options : {
102+ mcpServers ?: unknown [ ] ;
103+ errorBeforeCompletion ?: { message : string ; willRetry : boolean } ;
104+ } = { } ,
105+ ) {
101106 const methods : string [ ] = [ ] ;
102107 const notificationHandlers : Array < ( notification : CodexServerNotification ) => void > = [ ] ;
103108 const request = vi . fn ( async ( method : string , _params ?: unknown ) => {
@@ -126,6 +131,17 @@ function createClientFactory(options: { mcpServers?: unknown[] } = {}) {
126131 if ( method === "turn/start" ) {
127132 queueMicrotask ( ( ) => {
128133 for ( const handler of notificationHandlers ) {
134+ if ( options . errorBeforeCompletion ) {
135+ handler ( {
136+ method : "error" ,
137+ params : {
138+ threadId : "thread-finalizer" ,
139+ turnId : "turn-finalizer" ,
140+ error : { message : options . errorBeforeCompletion . message } ,
141+ willRetry : options . errorBeforeCompletion . willRetry ,
142+ } ,
143+ } ) ;
144+ }
129145 handler ( {
130146 method : "rawResponse/completed" ,
131147 params : {
@@ -175,6 +191,46 @@ function createClientFactory(options: { mcpServers?: unknown[] } = {}) {
175191}
176192
177193describe ( "runBoundedCodexAppServerTurn settled finalization isolation" , ( ) => {
194+ it ( "continues after a retryable error notification" , async ( ) => {
195+ const fake = createClientFactory ( {
196+ errorBeforeCompletion : { message : "temporary upstream disconnect" , willRetry : true } ,
197+ } ) ;
198+
199+ await expect (
200+ runBoundedCodexAppServerTurn ( {
201+ model : { mode : "required" , id : "gpt-5.4" } ,
202+ timeoutMs : 5_000 ,
203+ options : { clientFactory : fake . factory } ,
204+ taskLabel : "settled-turn finalization" ,
205+ developerInstructions : "Finalize only." ,
206+ input : [ { type : "text" , text : "Produce the final answer." , text_elements : [ ] } ] ,
207+ requiredModalities : [ "text" ] ,
208+ isolation : "private-stdio" ,
209+ requireNoExternalCapabilities : true ,
210+ } ) ,
211+ ) . resolves . toMatchObject ( { text : "The message was sent successfully." } ) ;
212+ } ) ;
213+
214+ it ( "still fails on a terminal error notification" , async ( ) => {
215+ const fake = createClientFactory ( {
216+ errorBeforeCompletion : { message : "terminal upstream failure" , willRetry : false } ,
217+ } ) ;
218+
219+ await expect (
220+ runBoundedCodexAppServerTurn ( {
221+ model : { mode : "required" , id : "gpt-5.4" } ,
222+ timeoutMs : 5_000 ,
223+ options : { clientFactory : fake . factory } ,
224+ taskLabel : "settled-turn finalization" ,
225+ developerInstructions : "Finalize only." ,
226+ input : [ { type : "text" , text : "Produce the final answer." , text_elements : [ ] } ] ,
227+ requiredModalities : [ "text" ] ,
228+ isolation : "private-stdio" ,
229+ requireNoExternalCapabilities : true ,
230+ } ) ,
231+ ) . rejects . toThrow ( "terminal upstream failure" ) ;
232+ } ) ;
233+
178234 it ( "attests ring-zero and injects frozen history before starting the final turn" , async ( ) => {
179235 const fake = createClientFactory ( ) ;
180236 const historyItems : JsonValue [ ] = [
0 commit comments