@@ -1662,7 +1662,9 @@ describe("runAgentTurnWithFallback", () => {
16621662
16631663 expect ( result . kind ) . toBe ( "final" ) ;
16641664 if ( result . kind === "final" ) {
1665- expect ( result . payload . text ) . toContain ( "Something went wrong while processing your request" ) ;
1665+ expect ( result . payload . text ) . toContain ( "Agent failed before reply" ) ;
1666+ expect ( result . payload . text ) . toContain ( "All models failed" ) ;
1667+ expect ( result . payload . text ) . toContain ( "402 (billing)" ) ;
16661668 expect ( result . payload . text ) . not . toContain ( "Rate-limited" ) ;
16671669 }
16681670 } ) ;
@@ -1877,7 +1879,7 @@ describe("runAgentTurnWithFallback", () => {
18771879 expect ( failMock ) . not . toHaveBeenCalled ( ) ;
18781880 } ) ;
18791881
1880- it ( "returns a friendly generic error on external chat channels" , async ( ) => {
1882+ it ( "forwards sanitized generic errors on external chat channels" , async ( ) => {
18811883 state . runEmbeddedPiAgentMock . mockRejectedValueOnce (
18821884 new Error ( "INVALID_ARGUMENT: some other failure" ) ,
18831885 ) ;
@@ -1910,7 +1912,47 @@ describe("runAgentTurnWithFallback", () => {
19101912 expect ( result . kind ) . toBe ( "final" ) ;
19111913 if ( result . kind === "final" ) {
19121914 expect ( result . payload . text ) . toBe (
1913- "⚠️ Something went wrong while processing your request. Please try again, or use /new to start a fresh session." ,
1915+ "⚠️ Agent failed before reply: INVALID_ARGUMENT: some other failure. Please try again, or use /new to start a fresh session." ,
1916+ ) ;
1917+ }
1918+ } ) ;
1919+
1920+ it ( "formats raw Codex API payloads before forwarding external errors" , async ( ) => {
1921+ state . runEmbeddedPiAgentMock . mockRejectedValueOnce (
1922+ new Error (
1923+ 'Codex error: {"type":"error","error":{"type":"server_error","message":"Something exploded"},"sequence_number":2}' ,
1924+ ) ,
1925+ ) ;
1926+
1927+ const runAgentTurnWithFallback = await getRunAgentTurnWithFallback ( ) ;
1928+ const result = await runAgentTurnWithFallback ( {
1929+ commandBody : "hello" ,
1930+ followupRun : createFollowupRun ( ) ,
1931+ sessionCtx : {
1932+ Provider : "whatsapp" ,
1933+ MessageSid : "msg" ,
1934+ } as unknown as TemplateContext ,
1935+ opts : { } ,
1936+ typingSignals : createMockTypingSignaler ( ) ,
1937+ blockReplyPipeline : null ,
1938+ blockStreamingEnabled : false ,
1939+ resolvedBlockStreamingBreak : "message_end" ,
1940+ applyReplyToMode : ( payload ) => payload ,
1941+ shouldEmitToolResult : ( ) => true ,
1942+ shouldEmitToolOutput : ( ) => false ,
1943+ pendingToolTasks : new Set ( ) ,
1944+ resetSessionAfterCompactionFailure : async ( ) => false ,
1945+ resetSessionAfterRoleOrderingConflict : async ( ) => false ,
1946+ isHeartbeat : false ,
1947+ sessionKey : "main" ,
1948+ getActiveSessionEntry : ( ) => undefined ,
1949+ resolvedVerboseLevel : "off" ,
1950+ } ) ;
1951+
1952+ expect ( result . kind ) . toBe ( "final" ) ;
1953+ if ( result . kind === "final" ) {
1954+ expect ( result . payload . text ) . toBe (
1955+ "⚠️ Agent failed before reply: LLM error server_error: Something exploded. Please try again, or use /new to start a fresh session." ,
19141956 ) ;
19151957 }
19161958 } ) ;
0 commit comments