@@ -745,6 +745,17 @@ export async function runTui(opts: RunTuiOptions): Promise<TuiResult> {
745745 return await runTuiUnlocked ( opts ) ;
746746}
747747
748+ class TuiSessionIdentityState {
749+ sessionKey = "" ;
750+ sessionId : string | null = null ;
751+ readonly generations = new Map < string , number > ( ) ;
752+ readonly sessionIds = new Map < string , string > ( ) ;
753+ constructor ( public agentId : string ) { }
754+ generationKey ( ) {
755+ return JSON . stringify ( [ this . agentId , this . sessionKey ] ) ;
756+ }
757+ }
758+
748759async function runTuiUnlocked ( opts : RunTuiOptions ) : Promise < TuiResult > {
749760 const isLocalMode = opts . local === true || opts . backend !== undefined ;
750761 const config = opts . config ?? getRuntimeConfig ( { skipPluginValidation : ! isLocalMode } ) ;
@@ -755,19 +766,15 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
755766 const sessionScope = ( config . session ?. scope ?? "per-sender" ) as SessionScope ;
756767 const sessionMainKey = normalizeMainKey ( config . session ?. mainKey ) ;
757768 const configuredDefaultAgentId = tryResolveDefaultAgentId ( config ) ;
758- let currentAgentId = resolveInitialTuiAgentId ( {
769+ const initialAgentId = resolveInitialTuiAgentId ( {
759770 cfg : config ,
760771 fallbackAgentId : configuredDefaultAgentId ,
761772 initialSessionInput,
762773 agentId : opts . agentId ,
763774 } ) ;
764- const agentDefaultId = configuredDefaultAgentId ?? currentAgentId ;
775+ const agentDefaultId = configuredDefaultAgentId ?? initialAgentId ;
765776 const agentNames = new Map < string , string > ( ) ;
766- let currentSessionKey = "" ;
767777 let rememberedSessionApplied = false ;
768- let currentSessionId : string | null = null ;
769- const sessionGenerations = new Map < string , number > ( ) ;
770- const sessionIds = new Map < string , string > ( ) ;
771778 let connectionGeneration = 0 ;
772779 const connectionLineage = createTuiConnectionLineage ( ) ;
773780 let remediationShown = false ;
@@ -789,61 +796,58 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
789796 let statusStartedAt : number | null = null ;
790797 let lastActivityStatus = "idle" ;
791798 let invalidateSessionRunOwnership : ( ) => void = ( ) => undefined ;
799+ let notifySessionChanged : ( ) => void = ( ) => undefined ;
792800 let retireHistoryAbsentRun : ( _runId : string ) => void = ( ) => undefined ;
793801
794- const currentSessionGenerationKey = ( ) : string =>
795- JSON . stringify ( [ currentAgentId , currentSessionKey ] ) ;
796- const readCurrentSessionGeneration = ( ) =>
797- sessionGenerations . get ( currentSessionGenerationKey ( ) ) ?? 0 ;
798- const writeCurrentSessionGeneration = ( value : number ) => {
799- sessionGenerations . set ( currentSessionGenerationKey ( ) , value ) ;
800- } ;
801-
802- const state : TuiStateAccess = {
802+ const state : TuiStateAccess & {
803+ sessionGeneration : number ;
804+ sessionIdentity : TuiSessionIdentityState ;
805+ } = {
806+ sessionIdentity : new TuiSessionIdentityState ( initialAgentId ) ,
803807 agentDefaultId,
804808 sessionMainKey,
805809 sessionScope,
806810 agents : [ ] ,
807811 get currentAgentId ( ) {
808- return currentAgentId ;
812+ return this . sessionIdentity . agentId ;
809813 } ,
810- set currentAgentId ( value ) {
811- if ( currentAgentId === value ) {
814+ set currentAgentId ( value : string ) {
815+ if ( this . sessionIdentity . agentId === value ) {
812816 return ;
813817 }
814- currentAgentId = value ;
818+ this . sessionIdentity . agentId = value ;
815819 invalidateSessionRunOwnership ( ) ;
816- pluginApprovals ?. sessionChanged ( ) ;
817- taskSuggestions ?. sessionChanged ( ) ;
820+ notifySessionChanged ( ) ;
818821 } ,
819822 get currentSessionKey ( ) {
820- return currentSessionKey ;
823+ return this . sessionIdentity . sessionKey ;
821824 } ,
822- set currentSessionKey ( value ) {
823- currentSessionKey = value ;
824- pluginApprovals ?. sessionChanged ( ) ;
825- taskSuggestions ?. sessionChanged ( ) ;
825+ set currentSessionKey ( value : string ) {
826+ this . sessionIdentity . sessionKey = value ;
827+ notifySessionChanged ( ) ;
826828 } ,
827829 get currentSessionId ( ) {
828- return currentSessionId ;
830+ return this . sessionIdentity . sessionId ;
829831 } ,
830- set currentSessionId ( value ) {
832+ set currentSessionId ( value : string | null ) {
831833 if ( value ) {
832- const generationKey = currentSessionGenerationKey ( ) ;
833- const previousSessionId = sessionIds . get ( generationKey ) ;
834+ const generationKey = this . sessionIdentity . generationKey ( ) ;
835+ const previousSessionId = this . sessionIdentity . sessionIds . get ( generationKey ) ;
834836 // The first ID binds an unresolved selection; reset/replacement owners bump explicitly.
835837 if ( previousSessionId && previousSessionId !== value ) {
836- writeCurrentSessionGeneration ( readCurrentSessionGeneration ( ) + 1 ) ;
838+ this . sessionGeneration += 1 ;
837839 }
838- sessionIds . set ( generationKey , value ) ;
840+ this . sessionIdentity . sessionIds . set ( generationKey , value ) ;
839841 }
840- currentSessionId = value ;
842+ this . sessionIdentity . sessionId = value ;
841843 } ,
842844 get sessionGeneration ( ) {
843- return readCurrentSessionGeneration ( ) ;
845+ const generationKey = this . sessionIdentity . generationKey ( ) ;
846+ return this . sessionIdentity . generations . get ( generationKey ) ?? 0 ;
844847 } ,
845- set sessionGeneration ( value ) {
846- writeCurrentSessionGeneration ( Math . max ( readCurrentSessionGeneration ( ) , value ) ) ;
848+ set sessionGeneration ( value : number ) {
849+ const generationKey = this . sessionIdentity . generationKey ( ) ;
850+ this . sessionIdentity . generations . set ( generationKey , Math . max ( this . sessionGeneration , value ) ) ;
847851 } ,
848852 activeChatRunId : null ,
849853 pendingSubmit : null ,
@@ -1033,9 +1037,10 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
10331037 } ) ;
10341038 } ;
10351039
1036- currentSessionKey = resolveSessionSelection ( initialSessionInput ) . key ;
1040+ // Initial selection predates controller construction, so it intentionally does not notify.
1041+ state . sessionIdentity . sessionKey = resolveSessionSelection ( initialSessionInput ) . key ;
10371042
1038- const buildLastSessionScopeKeyFor = ( sessionKey = currentSessionKey ) => {
1043+ const buildLastSessionScopeKeyFor = ( sessionKey = state . currentSessionKey ) => {
10391044 const parsed = parseAgentSessionKey ( sessionKey ) ;
10401045 return buildTuiLastSessionScopeKey ( {
10411046 connectionUrl : client . connection . url ,
@@ -1069,7 +1074,7 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
10691074 }
10701075 const rememberedSelection = remembered ? resolveSessionSelection ( remembered ) : null ;
10711076 const rememberedKey = rememberedSelection ?. key ?? null ;
1072- if ( ! rememberedKey || rememberedKey === currentSessionKey ) {
1077+ if ( ! rememberedKey || rememberedKey === state . currentSessionKey ) {
10731078 rememberedSessionApplied = true ;
10741079 return ;
10751080 }
@@ -1102,16 +1107,16 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
11021107 currentAgentId : state . currentAgentId ,
11031108 sessions : sessions . sessions ,
11041109 } ) ;
1105- if ( ! restored || restored === currentSessionKey ) {
1110+ if ( ! restored || restored === state . currentSessionKey ) {
11061111 return ;
11071112 }
1108- currentSessionKey = restored ;
1113+ state . currentSessionKey = restored ;
11091114 updateHeader ( ) ;
11101115 updateFooter ( ) ;
11111116 } ;
11121117
11131118 const updateHeader = ( ) => {
1114- const sessionLabel = formatSessionKey ( currentSessionKey ) ;
1119+ const sessionLabel = formatSessionKey ( state . currentSessionKey ) ;
11151120 const agentLabel = formatAgentLabel ( state . currentAgentId ) ;
11161121 const title = opts . title ?? "openclaw tui" ;
11171122 const text = `${ title } - ${ client . connection . url } - agent ${ agentLabel } - session ${ sessionLabel } ` ;
@@ -1369,7 +1374,7 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
13691374 : undefined ;
13701375
13711376 const updateFooter = ( ) => {
1372- const sessionKeyLabel = formatSessionKey ( currentSessionKey ) ;
1377+ const sessionKeyLabel = formatSessionKey ( state . currentSessionKey ) ;
13731378 const sessionLabel = state . sessionInfo . displayName
13741379 ? `${ sessionKeyLabel } (${ state . sessionInfo . displayName } )`
13751380 : sessionKeyLabel ;
@@ -1393,7 +1398,7 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
13931398 client,
13941399 chatLog,
13951400 getAgentId : ( ) => state . currentAgentId ,
1396- getSessionKey : ( ) => currentSessionKey ,
1401+ getSessionKey : ( ) => state . currentSessionKey ,
13971402 openOverlay,
13981403 closeOverlay,
13991404 requestRender : ( ) => tui . requestRender ( ) ,
@@ -1407,12 +1412,7 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
14071412 } ,
14081413 } ;
14091414
1410- const initialSessionAgentId = ( ( ) => {
1411- if ( ! initialSessionInput ) {
1412- return null ;
1413- }
1414- return currentAgentId ;
1415- } ) ( ) ;
1415+ const initialSessionAgentId = initialSessionInput ? state . currentAgentId : null ;
14161416 const sessionActions = createSessionActions ( {
14171417 client,
14181418 chatLog,
@@ -1461,13 +1461,17 @@ async function runTuiUnlocked(opts: RunTuiOptions): Promise<TuiResult> {
14611461 const taskSuggestions = createTuiTaskSuggestionController ( {
14621462 client,
14631463 chatLog,
1464- getAgentId : ( ) => currentAgentId ,
1465- getSessionKey : ( ) => currentSessionKey ,
1464+ getAgentId : ( ) => state . currentAgentId ,
1465+ getSessionKey : ( ) => state . currentSessionKey ,
14661466 openOverlay,
14671467 closeOverlay,
14681468 requestRender : ( ) => tui . requestRender ( ) ,
14691469 onAccepted : setSession ,
14701470 } ) ;
1471+ notifySessionChanged = ( ) => {
1472+ pluginApprovals . sessionChanged ( ) ;
1473+ taskSuggestions . sessionChanged ( ) ;
1474+ } ;
14711475
14721476 const {
14731477 handleChatEvent,
0 commit comments