@@ -1572,6 +1572,8 @@ const make = Effect.gen(function* () {
15721572 event . type === "turn.started" ||
15731573 event . type === "turn.completed"
15741574 ) {
1575+ const unexpectedSessionExit =
1576+ event . type === "session.exited" && event . payload . exitKind === "error" ;
15751577 const status = ( ( ) => {
15761578 switch ( event . type ) {
15771579 case "session.state.changed" : {
@@ -1593,6 +1595,7 @@ const make = Effect.gen(function* () {
15931595 return activeTurnId !== null ? "running" : hasPendingTurnStart ? "starting" : "ready" ;
15941596 }
15951597 } ) ( ) ;
1598+ const lifecycleStatus = unexpectedSessionExit ? "error" : status ;
15961599 const nextActiveTurnId =
15971600 event . type === "turn.started"
15981601 ? ( eventTurnId ?? null )
@@ -1605,14 +1608,18 @@ const make = Effect.gen(function* () {
16051608 ? null
16061609 : activeTurnId ;
16071610 const lastError =
1608- event . type === "session.state.changed" && event . payload . state === "error"
1609- ? ( event . payload . reason ?? thread . session ?. lastError ?? "Provider session error" )
1610- : event . type === "turn.completed" &&
1611- normalizeRuntimeTurnState ( event . payload . state ) === "failed"
1612- ? ( event . payload . errorMessage ?? thread . session ?. lastError ?? "Turn failed" )
1613- : status === "ready"
1614- ? null
1615- : ( thread . session ?. lastError ?? null ) ;
1611+ unexpectedSessionExit
1612+ ? ( event . payload . reason ??
1613+ thread . session ?. lastError ??
1614+ "Provider session exited unexpectedly" )
1615+ : event . type === "session.state.changed" && event . payload . state === "error"
1616+ ? ( event . payload . reason ?? thread . session ?. lastError ?? "Provider session error" )
1617+ : event . type === "turn.completed" &&
1618+ normalizeRuntimeTurnState ( event . payload . state ) === "failed"
1619+ ? ( event . payload . errorMessage ?? thread . session ?. lastError ?? "Turn failed" )
1620+ : lifecycleStatus === "ready"
1621+ ? null
1622+ : ( thread . session ?. lastError ?? null ) ;
16161623
16171624 if ( shouldApplyThreadLifecycle ) {
16181625 if ( event . type === "turn.started" && acceptedTurnStartedSourcePlan !== null ) {
@@ -1641,7 +1648,7 @@ const make = Effect.gen(function* () {
16411648 threadId : thread . id ,
16421649 session : {
16431650 threadId : thread . id ,
1644- status,
1651+ status : lifecycleStatus ,
16451652 providerName : event . provider ,
16461653 ...( event . providerInstanceId !== undefined
16471654 ? { providerInstanceId : event . providerInstanceId }
0 commit comments