@@ -3382,3 +3382,86 @@ describe("loadChatHistory retry handling", () => {
33823382 expect ( state . chatThinkingLevel ) . toBeNull ( ) ;
33833383 } ) ;
33843384} ) ;
3385+
3386+ describe ( "loadChatHistory in-flight run adoption" , ( ) => {
3387+ it ( "restores a buffered in-flight assistant stream on reconnect" , async ( ) => {
3388+ // Reconnect after switching away: only the committed user turn is persisted,
3389+ // and the gateway reports the still-streaming assistant run as inFlightRun.
3390+ const request = vi . fn ( ) . mockResolvedValue ( {
3391+ messages : [ { role : "user" , content : [ { type : "text" , text : "Do the thing" } ] } ] ,
3392+ inFlightRun : { runId : "run-live" , text : "Partial assistant repl" } ,
3393+ } ) ;
3394+ const state = createState ( {
3395+ client : { request } as unknown as ChatState [ "client" ] ,
3396+ connected : true ,
3397+ chatRunId : null ,
3398+ chatStream : null ,
3399+ chatStreamStartedAt : null ,
3400+ } ) ;
3401+
3402+ await loadChatHistory ( state ) ;
3403+
3404+ expect ( state . chatRunId ) . toBe ( "run-live" ) ;
3405+ expect ( state . chatStream ) . toBe ( "Partial assistant repl" ) ;
3406+ expect ( state . chatStreamStartedAt ) . toEqual ( expect . any ( Number ) ) ;
3407+ expect ( state . chatMessages ) . toHaveLength ( 1 ) ;
3408+ } ) ;
3409+
3410+ it ( "adopts an empty-text in-flight run so the thread shows streaming, not idle" , async ( ) => {
3411+ // Runtimes that only deliver assistant text at completion (e.g. Codex) report
3412+ // an inFlightRun with no buffered text; the client must still adopt the run so
3413+ // the status is streaming and a reading indicator renders (chatStream === "").
3414+ const request = vi . fn ( ) . mockResolvedValue ( {
3415+ messages : [ { role : "user" , content : [ { type : "text" , text : "Run it" } ] } ] ,
3416+ inFlightRun : { runId : "run-codex" , text : "" } ,
3417+ } ) ;
3418+ const state = createState ( {
3419+ client : { request } as unknown as ChatState [ "client" ] ,
3420+ connected : true ,
3421+ } ) ;
3422+
3423+ await loadChatHistory ( state ) ;
3424+
3425+ expect ( state . chatRunId ) . toBe ( "run-codex" ) ;
3426+ expect ( state . chatStream ) . toBe ( "" ) ;
3427+ expect ( state . chatStreamStartedAt ) . toEqual ( expect . any ( Number ) ) ;
3428+ } ) ;
3429+
3430+ it ( "ignores inFlightRun without a run id" , async ( ) => {
3431+ const request = vi . fn ( ) . mockResolvedValue ( {
3432+ messages : [ { role : "user" , content : [ { type : "text" , text : "hi" } ] } ] ,
3433+ inFlightRun : { text : "orphan partial" } ,
3434+ } ) ;
3435+ const state = createState ( {
3436+ client : { request } as unknown as ChatState [ "client" ] ,
3437+ connected : true ,
3438+ } ) ;
3439+
3440+ await loadChatHistory ( state ) ;
3441+
3442+ expect ( state . chatRunId ) . toBeNull ( ) ;
3443+ expect ( state . chatStream ) . toBeNull ( ) ;
3444+ } ) ;
3445+
3446+ it ( "does not adopt inFlightRun when a fresher local run is already streaming" , async ( ) => {
3447+ // A live local run started/continued during the reload must win over a stale
3448+ // history snapshot's inFlightRun; reconciliation keeps the local run id, so
3449+ // adoption must be skipped.
3450+ const request = vi . fn ( ) . mockResolvedValue ( {
3451+ messages : [ { role : "user" , content : [ { type : "text" , text : "newer turn" } ] } ] ,
3452+ inFlightRun : { runId : "run-stale" , text : "stale partial" } ,
3453+ } ) ;
3454+ const state = createState ( {
3455+ client : { request } as unknown as ChatState [ "client" ] ,
3456+ connected : true ,
3457+ chatRunId : "run-fresh" ,
3458+ chatStream : "fresh live text" ,
3459+ chatStreamStartedAt : 999 ,
3460+ } ) ;
3461+
3462+ await loadChatHistory ( state ) ;
3463+
3464+ expect ( state . chatRunId ) . toBe ( "run-fresh" ) ;
3465+ expect ( state . chatStream ) . toBe ( "fresh live text" ) ;
3466+ } ) ;
3467+ } ) ;
0 commit comments