@@ -216,6 +216,10 @@ describe("tui session actions", () => {
216216 agentNames,
217217 updateHeader,
218218 updateFooter,
219+ resolveSessionSelection : vi . fn ( ( _raw ?: string , agentId = state . currentAgentId ) => ( {
220+ key : `agent:${ agentId } :${ state . sessionMainKey } ` ,
221+ agentId,
222+ } ) ) ,
219223 } ) ;
220224
221225 await expect ( refreshAgents ( ) ) . resolves . toEqual ( { ok : true , value : undefined } ) ;
@@ -227,6 +231,7 @@ describe("tui session actions", () => {
227231 { id : "system-agent" , kind : "system" , name : "System Agent" } ,
228232 ] ) ;
229233 expect ( state . currentAgentId ) . toBe ( "team-lead" ) ;
234+ expect ( state . currentSessionKey ) . toBe ( "agent:team-lead:primary" ) ;
230235 expect ( [ ...agentNames ] ) . toEqual ( [
231236 [ "team-lead" , "Lead Agent" ] ,
232237 [ "system-agent" , "System Agent" ] ,
@@ -235,6 +240,129 @@ describe("tui session actions", () => {
235240 expect ( updateFooter ) . toHaveBeenCalledTimes ( 1 ) ;
236241 } ) ;
237242
243+ it . each ( [
244+ {
245+ scope : "per-sender" as const ,
246+ previousKey : "agent:research:main" ,
247+ nextKey : "agent:ops:main" ,
248+ } ,
249+ {
250+ scope : "global" as const ,
251+ previousKey : "global" ,
252+ nextKey : "global" ,
253+ } ,
254+ ] ) (
255+ "retires the complete $scope session when its selected agent disappears" ,
256+ async ( { scope, previousKey, nextKey } ) => {
257+ const state = createBaseState ( {
258+ agents : [ { id : "research" } ] ,
259+ currentAgentId : "research" ,
260+ currentSessionKey : previousKey ,
261+ currentSessionId : "old-session" ,
262+ sessionMainKey : "main" ,
263+ sessionScope : scope ,
264+ activeChatRunId : "old-run" ,
265+ pendingSubmit : acceptedSubmit ( "pending-run" ) ,
266+ historyLoaded : true ,
267+ sessionInfo : { updatedAt : 100 , thinkingLevel : "high" , verboseLevel : "full" } ,
268+ } ) ;
269+ sendPendingUser ( state , "pending-run" , "stale prompt" ) ;
270+ const loadHistory = vi . fn ( ) ;
271+ const invalidateRunOwnership = vi . fn ( ) ;
272+ const clearLocalRunIds = vi . fn ( ) ;
273+ const clearAll = vi . fn ( ) ;
274+ const clearPendingUsers = vi . fn ( ) ;
275+ const btw = createBtwPresenter ( ) ;
276+ const { refreshAgents } = createTestSessionActions ( {
277+ client : makeTuiBackend ( {
278+ loadHistory,
279+ listAgents : vi . fn ( ) . mockResolvedValue ( {
280+ defaultId : "ops" ,
281+ mainKey : "main" ,
282+ scope,
283+ agents : [ { id : "ops" } ] ,
284+ } ) ,
285+ } ) ,
286+ chatLog : makeChatLog ( { clearAll, clearPendingUsers } ) ,
287+ btw,
288+ state,
289+ invalidateRunOwnership,
290+ clearLocalRunIds,
291+ resolveSessionSelection : vi . fn ( ( _raw ?: string , agentId = state . currentAgentId ) => ( {
292+ key : scope === "global" ? "global" : `agent:${ agentId } :main` ,
293+ agentId,
294+ } ) ) ,
295+ } ) ;
296+
297+ await expect ( refreshAgents ( ) ) . resolves . toEqual ( { ok : true , value : undefined } ) ;
298+
299+ expect ( state ) . toMatchObject ( {
300+ currentAgentId : "ops" ,
301+ currentSessionKey : nextKey ,
302+ currentSessionId : null ,
303+ activeChatRunId : null ,
304+ pendingSubmit : null ,
305+ historyLoaded : false ,
306+ sessionInfo : { updatedAt : null } ,
307+ } ) ;
308+ expect ( state . sessionInfo . thinkingLevel ) . toBe ( "high" ) ;
309+ expect ( state . sessionInfo . verboseLevel ) . toBeUndefined ( ) ;
310+ expect ( state . sessionProjection ?. entries ) . toEqual ( [ ] ) ;
311+ expect ( invalidateRunOwnership ) . toHaveBeenCalledOnce ( ) ;
312+ expect ( clearLocalRunIds ) . toHaveBeenCalledOnce ( ) ;
313+ expect ( clearAll ) . toHaveBeenCalledOnce ( ) ;
314+ expect ( clearPendingUsers ) . toHaveBeenCalledOnce ( ) ;
315+ expect ( btw . clear ) . toHaveBeenCalledOnce ( ) ;
316+ expect ( loadHistory ) . not . toHaveBeenCalled ( ) ;
317+ } ,
318+ ) ;
319+
320+ it ( "preserves the complete selected session when its agent remains in the roster" , async ( ) => {
321+ const state = createBaseState ( {
322+ agents : [ { id : "research" } ] ,
323+ currentAgentId : "research" ,
324+ currentSessionKey : "agent:research:incident" ,
325+ currentSessionId : "current-session" ,
326+ sessionScope : "per-sender" ,
327+ activeChatRunId : "current-run" ,
328+ pendingSubmit : acceptedSubmit ( "pending-run" ) ,
329+ historyLoaded : true ,
330+ sessionInfo : { updatedAt : 100 , thinkingLevel : "high" } ,
331+ } ) ;
332+ sendPendingUser ( state , "pending-run" , "current prompt" ) ;
333+ const previousProjection = state . sessionProjection ;
334+ const invalidateRunOwnership = vi . fn ( ) ;
335+ const resolveSessionSelection = vi . fn ( ) ;
336+ const { refreshAgents } = createTestSessionActions ( {
337+ client : makeTuiBackend ( {
338+ listAgents : vi . fn ( ) . mockResolvedValue ( {
339+ defaultId : "ops" ,
340+ mainKey : "main" ,
341+ scope : "per-sender" ,
342+ agents : [ { id : "ops" } , { id : "research" } ] ,
343+ } ) ,
344+ } ) ,
345+ state,
346+ invalidateRunOwnership,
347+ resolveSessionSelection,
348+ } ) ;
349+
350+ await expect ( refreshAgents ( ) ) . resolves . toEqual ( { ok : true , value : undefined } ) ;
351+
352+ expect ( state ) . toMatchObject ( {
353+ currentAgentId : "research" ,
354+ currentSessionKey : "agent:research:incident" ,
355+ currentSessionId : "current-session" ,
356+ activeChatRunId : "current-run" ,
357+ historyLoaded : true ,
358+ sessionInfo : { updatedAt : 100 , thinkingLevel : "high" } ,
359+ } ) ;
360+ expect ( state . pendingSubmit ) . toEqual ( acceptedSubmit ( "pending-run" ) ) ;
361+ expect ( state . sessionProjection ) . toBe ( previousProjection ) ;
362+ expect ( invalidateRunOwnership ) . not . toHaveBeenCalled ( ) ;
363+ expect ( resolveSessionSelection ) . not . toHaveBeenCalled ( ) ;
364+ } ) ;
365+
238366 it ( "queues session refreshes and applies the latest result" , async ( ) => {
239367 let resolveFirst : ( ( value : unknown ) => void ) | undefined ;
240368 let resolveSecond : ( ( value : unknown ) => void ) | undefined ;
0 commit comments