@@ -360,7 +360,7 @@ describe("SessionManager", () => {
360360 const firstSession = createSession ( ) ;
361361 const firstSessionId = firstSession . sessionId ;
362362
363- vi . mocked ( uuidv4 ) . mockReturnValue ( "new-uuid-456" as any ) ;
363+ vi . mocked ( uuidv4 ) . mockReturnValue ( "new-uuid-456" ) ;
364364
365365 const secondSession = startNewSession ( ) ;
366366
@@ -381,7 +381,7 @@ describe("SessionManager", () => {
381381 } ,
382382 ] ;
383383
384- vi . mocked ( uuidv4 ) . mockReturnValue ( "new-uuid-789" as any ) ;
384+ vi . mocked ( uuidv4 ) . mockReturnValue ( "new-uuid-789" ) ;
385385
386386 const session = startNewSession ( history ) ;
387387
@@ -392,7 +392,7 @@ describe("SessionManager", () => {
392392 it ( "should set the new session as current" , ( ) => {
393393 const originalSession = createSession ( ) ;
394394
395- vi . mocked ( uuidv4 ) . mockReturnValue ( "new-session-id" as any ) ;
395+ vi . mocked ( uuidv4 ) . mockReturnValue ( "new-session-id" ) ;
396396
397397 const newSession = startNewSession ( ) ;
398398 const currentSession = getCurrentSession ( ) ;
@@ -401,4 +401,100 @@ describe("SessionManager", () => {
401401 expect ( currentSession ) . not . toBe ( originalSession ) ;
402402 } ) ;
403403 } ) ;
404+
405+ describe ( "session isolation" , ( ) => {
406+ it ( "should not pollute new sessions with previous session history" , ( ) => {
407+ // Simulate first CLI session
408+ vi . mocked ( uuidv4 ) . mockReturnValue ( "session-1" ) ;
409+ const session1 = createSession ( ) ;
410+ const history1 : ChatHistoryItem [ ] = [
411+ {
412+ message : {
413+ role : "user" ,
414+ content : "Tell me about dogs" ,
415+ } ,
416+ contextItems : [ ] ,
417+ } ,
418+ {
419+ message : {
420+ role : "assistant" ,
421+ content : "Dogs are loyal companions..." ,
422+ } ,
423+ contextItems : [ ] ,
424+ } ,
425+ ] ;
426+ updateSessionHistory ( history1 ) ;
427+
428+ // Simulate starting a new CLI session (without --resume)
429+ vi . mocked ( uuidv4 ) . mockReturnValue ( "session-2" ) ;
430+ const session2 = startNewSession ( [ ] ) ;
431+
432+ // New session should have clean state
433+ expect ( session2 . sessionId ) . toBe ( "session-2" ) ;
434+ expect ( session2 . sessionId ) . not . toBe ( session1 . sessionId ) ;
435+ expect ( session2 . history ) . toEqual ( [ ] ) ;
436+ expect ( session2 . history . length ) . toBe ( 0 ) ;
437+ } ) ;
438+
439+ it ( "should create independent sessions for concurrent operations" , ( ) => {
440+ // Create first session with some data
441+ vi . mocked ( uuidv4 ) . mockReturnValue ( "concurrent-1" ) ;
442+ const session1 = createSession ( ) ;
443+ updateSessionTitle ( "Session 1" ) ;
444+ updateSessionHistory ( [
445+ {
446+ message : {
447+ role : "user" ,
448+ content : "First session message" ,
449+ } ,
450+ contextItems : [ ] ,
451+ } ,
452+ ] ) ;
453+
454+ // Start a new session
455+ vi . mocked ( uuidv4 ) . mockReturnValue ( "concurrent-2" ) ;
456+ const session2 = startNewSession ( [ ] ) ;
457+
458+ // Verify session2 is clean
459+ expect ( session2 . title ) . toBe ( "Untitled Session" ) ;
460+ expect ( session2 . history ) . toEqual ( [ ] ) ;
461+ expect ( session2 . sessionId ) . not . toBe ( session1 . sessionId ) ;
462+ } ) ;
463+
464+ it ( "should properly clear session state when transitioning between sessions" , ( ) => {
465+ // First session with complex history
466+ vi . mocked ( uuidv4 ) . mockReturnValue ( "complex-session-1" ) ;
467+ const session1 = createSession ( ) ;
468+ updateSessionTitle ( "Complex Session" ) ;
469+ const complexHistory : ChatHistoryItem [ ] = [
470+ {
471+ message : {
472+ role : "user" ,
473+ content : "What were we discussing?" ,
474+ } ,
475+ contextItems : [ ] ,
476+ } ,
477+ {
478+ message : {
479+ role : "assistant" ,
480+ content : "We were discussing dogs earlier." ,
481+ } ,
482+ contextItems : [ ] ,
483+ } ,
484+ ] ;
485+ updateSessionHistory ( complexHistory ) ;
486+
487+ // Verify first session has data
488+ expect ( getCurrentSession ( ) . history . length ) . toBe ( 2 ) ;
489+
490+ // Start fresh session
491+ vi . mocked ( uuidv4 ) . mockReturnValue ( "fresh-session-2" ) ;
492+ const session2 = startNewSession ( [ ] ) ;
493+
494+ // Verify clean state
495+ expect ( session2 . history . length ) . toBe ( 0 ) ;
496+ expect ( session2 . title ) . toBe ( "Untitled Session" ) ;
497+ expect ( getCurrentSession ( ) . sessionId ) . toBe ( "fresh-session-2" ) ;
498+ } ) ;
499+ } ) ;
404500} ) ;
0 commit comments