@@ -183,10 +183,12 @@ function createGatewayTimeoutError() {
183183}
184184
185185function createGatewayClosedError ( ) {
186- const err = new Error ( "gateway closed before response " ) ;
186+ const err = new Error ( "gateway closed (1006 abnormal closure): no close reason " ) ;
187187 err . name = "GatewayTransportError" ;
188188 return Object . assign ( err , {
189189 kind : "closed" ,
190+ code : 1006 ,
191+ reason : "no close reason" ,
190192 connectionDetails : {
191193 url : "ws://127.0.0.1:18789" ,
192194 urlSource : "local loopback" ,
@@ -1606,9 +1608,12 @@ describe("agentCliCommand", () => {
16061608 ) ;
16071609 expect ( resultMetaOverrides . transport ) . toBe ( "embedded" ) ;
16081610 expect ( resultMetaOverrides . fallbackFrom ) . toBe ( "gateway" ) ;
1611+ expect ( resultMetaOverrides . fallbackReason ) . toBe ( "gateway_closed" ) ;
16091612 expect (
16101613 mockMessages ( runtime . error ) . some ( ( message ) =>
1611- message . includes ( "EMBEDDED FALLBACK: Gateway agent failed" ) ,
1614+ message . includes (
1615+ "Gateway agent connection closed; running embedded agent with fresh session" ,
1616+ ) ,
16121617 ) ,
16131618 ) . toBe ( true ) ;
16141619 expect ( runtime . log ) . toHaveBeenCalledWith ( "local" ) ;
@@ -1654,9 +1659,18 @@ describe("agentCliCommand", () => {
16541659 }
16551660 } ) ;
16561661
1657- it ( "preserves explicit session keys for embedded fallback when the gateway closes" , async ( ) => {
1662+ it ( "uses a fresh embedded session when an accepted gateway turn closes abnormally " , async ( ) => {
16581663 await withTempStore ( async ( ) => {
1659- callGateway . mockRejectedValue ( createGatewayClosedError ( ) ) ;
1664+ callGateway . mockImplementationOnce ( async ( requestValue : unknown ) => {
1665+ const request = requireRecord ( requestValue , "gateway request" ) ;
1666+ const onAccepted = request . onAccepted as ( ( payload : unknown ) => void ) | undefined ;
1667+ onAccepted ?.( {
1668+ status : "accepted" ,
1669+ runId : "accepted-run" ,
1670+ sessionKey : "agent:main:incident-42" ,
1671+ } ) ;
1672+ throw createGatewayClosedError ( ) ;
1673+ } ) ;
16601674 mockLocalAgentReply ( ) ;
16611675
16621676 await agentCliCommand ( { message : "hi" , sessionKey : "agent:main:incident-42" } , runtime ) ;
@@ -1667,29 +1681,39 @@ describe("agentCliCommand", () => {
16671681 requireFirstCallArg ( agentCommand , "embedded agent" ) ,
16681682 "embedded agent options" ,
16691683 ) ;
1670- expect ( fallbackOpts . sessionKey ) . toBe ( "agent:main:incident-42" ) ;
1684+ const fallbackSessionId = String ( fallbackOpts . sessionId ) ;
1685+ const fallbackSessionKey = String ( fallbackOpts . sessionKey ) ;
1686+ expect ( fallbackSessionId ) . toMatch ( / ^ g a t e w a y - f a l l b a c k - / ) ;
1687+ expect ( fallbackSessionKey ) . toBe ( `agent:main:explicit:${ fallbackSessionId } ` ) ;
1688+ expect ( fallbackSessionKey ) . not . toBe ( "agent:main:incident-42" ) ;
1689+ expect ( fallbackOpts . runId ) . toBe ( fallbackSessionId ) ;
16711690 expect ( fallbackOpts . resultMetaOverrides ) . toMatchObject ( {
16721691 transport : "embedded" ,
16731692 fallbackFrom : "gateway" ,
1693+ fallbackReason : "gateway_closed" ,
1694+ fallbackSessionId,
1695+ fallbackSessionKey,
16741696 } ) ;
16751697 } ) ;
16761698 } ) ;
16771699
1678- it ( "propagates harness-owned session rejection from embedded gateway fallback" , async ( ) => {
1700+ it ( "does not pass a harness-owned session key into closed gateway fallback" , async ( ) => {
16791701 await withTempStore ( async ( ) => {
16801702 const sessionKey = "agent:main:harness:codex:supervision:missing-fallback" ;
16811703 callGateway . mockRejectedValue ( createGatewayClosedError ( ) ) ;
1682- agentCommand . mockRejectedValueOnce ( new Error ( AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE ) ) ;
1704+ mockLocalAgentReply ( ) ;
16831705
1684- await expect ( agentCliCommand ( { message : "hi" , sessionKey } , runtime ) ) . rejects . toThrow (
1685- AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE ,
1686- ) ;
1706+ await agentCliCommand ( { message : "hi" , sessionKey } , runtime ) ;
16871707
16881708 expect ( callGateway ) . toHaveBeenCalledOnce ( ) ;
16891709 expect ( agentCommand ) . toHaveBeenCalledOnce ( ) ;
1690- expect (
1691- requireRecord ( requireFirstCallArg ( agentCommand , "embedded agent" ) , "options" ) ,
1692- ) . toMatchObject ( { sessionKey } ) ;
1710+ const fallbackOpts = requireRecord (
1711+ requireFirstCallArg ( agentCommand , "embedded agent" ) ,
1712+ "options" ,
1713+ ) ;
1714+ const fallbackSessionId = String ( fallbackOpts . sessionId ) ;
1715+ expect ( fallbackOpts . sessionKey ) . toBe ( `agent:main:explicit:${ fallbackSessionId } ` ) ;
1716+ expect ( fallbackOpts . sessionKey ) . not . toBe ( sessionKey ) ;
16931717 } ) ;
16941718 } ) ;
16951719
@@ -1887,7 +1911,7 @@ describe("agentCliCommand", () => {
18871911 expect ( resultMetaOverrides . fallbackFrom ) . toBe ( "gateway" ) ;
18881912 expect (
18891913 mockMessages ( jsonRuntime . error ) . some ( ( message ) =>
1890- message . includes ( "EMBEDDED FALLBACK: Gateway agent failed " ) ,
1914+ message . includes ( "EMBEDDED FALLBACK: Gateway agent connection closed " ) ,
18911915 ) ,
18921916 ) . toBe ( true ) ;
18931917 expect ( loggingState . forceConsoleToStderr ) . toBe ( true ) ;
0 commit comments