@@ -326,6 +326,14 @@ describe("ClineProvider - Sticky Mode", () => {
326326 } )
327327 } )
328328
329+ afterEach ( async ( ) => {
330+ // The beforeEach above hands every test a fresh provider and the file had no hook that
331+ // released it: dispose() removes the instance from the static list that the claimed-id
332+ // check in setViewStateId and the fan-out methods iterate. It returns early once
333+ // _disposed is set, so a test that already disposed its provider is unaffected.
334+ await provider . dispose ( )
335+ } )
336+
329337 describe ( "handleModeSwitch" , ( ) => {
330338 beforeEach ( async ( ) => {
331339 await provider . resolveWebviewView ( mockWebviewView )
@@ -552,6 +560,77 @@ describe("ClineProvider - Sticky Mode", () => {
552560 expect ( persisted . mode ) . toBeUndefined ( )
553561 } )
554562
563+ // The listener-failure case above rethrows before handleModeSwitchUnlocked reaches
564+ // setValue("mode", newMode), so it cannot reach the rollback branch. This case fails
565+ // the durable mode write inside that call, from a view that already had a pin, so the
566+ // rollback has something to undo on every surface it touched.
567+ it ( "reverts the pin, the persisted entry and the task mode when the durable mode write fails" , async ( ) => {
568+ const mockTask = Object . assign (
569+ { } as Task ,
570+ {
571+ taskId : "test-task-id" ,
572+ taskMode : "architect" ,
573+ _taskMode : "architect" ,
574+ emit : vi . fn ( ) ,
575+ saveClineMessages : vi . fn ( ) ,
576+ clineMessages : [ ] ,
577+ apiConversationHistory : [ ] ,
578+ updateApiConfiguration : vi . fn ( ) ,
579+ } as Partial < Task > ,
580+ )
581+ const historyItem : HistoryItem = {
582+ id : "test-task-id" ,
583+ ts : Date . now ( ) ,
584+ task : "Test task" ,
585+ mode : "architect" ,
586+ number : 1 ,
587+ tokensIn : 0 ,
588+ tokensOut : 0 ,
589+ cacheWrites : 0 ,
590+ cacheReads : 0 ,
591+ totalCost : 0 ,
592+ }
593+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
594+ const historyWrites : HistoryItem [ ] = [ ]
595+ vi . spyOn ( provider , "updateTaskHistory" ) . mockImplementation ( ( item ) => {
596+ historyWrites . push ( item )
597+ return Promise . resolve ( [ ] )
598+ } )
599+ await provider . addClineToStack ( mockTask )
600+ await provider . contextProxy . setValue ( "mode" , "architect" )
601+ // A view that already pins the mode it is switching away from: the rollback must
602+ // restore that pin instead of leaving the forward one behind.
603+ await provider . saveViewState ( "mode" , "architect" )
604+
605+ const originalUpdate = mockContext . globalState . update
606+ mockContext . globalState . update = vi . fn ( ) . mockImplementation ( ( key : string , value : unknown ) => {
607+ if ( key === "mode" ) {
608+ return Promise . reject ( new Error ( "storage unavailable" ) )
609+ }
610+ return originalUpdate ( key , value )
611+ } )
612+
613+ try {
614+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "ask" , mockTask ) ) . rejects . toThrow (
615+ "storage unavailable" ,
616+ )
617+ } finally {
618+ mockContext . globalState . update = originalUpdate
619+ }
620+
621+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
622+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "architect" )
623+ const persisted = provider [ "getPersistedViewStates" ] ( ) [ provider [ "viewStateId" ] ]
624+ expect ( persisted . mode ) . toBe ( "architect" )
625+ expect ( mockTask . taskMode ) . toBe ( "architect" )
626+ // The compensation covers the shared selection, this view's pin and the task's mode.
627+ // The history row is rewritten only by a switch that completes, so the write the
628+ // failed switch attempted carries the new mode while the stored row still names the
629+ // mode the task was on before it.
630+ expect ( historyWrites . at ( - 1 ) ?. mode ) . toBe ( "ask" )
631+ expect ( historyItem . mode ) . toBe ( "architect" )
632+ } )
633+
555634 it ( "proceeds normally when no cancellation state exists" , async ( ) => {
556635 // A minimal typed double keeps the test focused on the mode-switch contract.
557636 // The literal is asserted as Partial<Task> so its private members (_taskMode,
@@ -1354,71 +1433,98 @@ describe("ClineProvider - Sticky Mode", () => {
13541433 } )
13551434
13561435 it ( "does not load a temporary-id entry that an earlier session wrote" , async ( ) => {
1357- // Temporary names restart from 0 on every host start, so storage can still hold
1358- // an entry under this instance's name from a previous session. Seed the entry
1359- // through the ContextProxy before the provider is constructed so the ordinary
1360- // loadViewState read (which goes through the proxy cache) actually sees it.
1361- const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1362- const proxy = new ContextProxy ( mockContext )
1363- await proxy . setValue ( "viewStates" , {
1364- [ tempId ] : { mode : "architect" , currentApiConfigName : "stale-profile" } ,
1365- } )
1366-
1367- const fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1368- expect ( fresh . viewId ) . toBe ( tempId )
1369-
1370- // Await the load itself instead of a fixed timer: the negative
1371- // assertion must run after the load has definitely finished.
1372- await fresh [ "loadViewState" ] ( )
1373-
1374- // The unowned temporary-id entry must not be adopted...
1375- expect ( fresh [ "viewLocalState" ] ) . toStrictEqual ( { } )
1376-
1377- // ...and the shared values still win, so the stale entry cannot reach getState().
1378- const state = await fresh . getState ( )
1379- expect ( state . mode ) . toBe ( "code" )
1380- expect ( state . currentApiConfigName ) . not . toBe ( "stale-profile" )
1436+ // Declared outside the try so the finally can still reach the instance.
1437+ let fresh : ClineProvider | undefined
1438+ try {
1439+ // Temporary names restart from 0 on every host start, so storage can still hold
1440+ // an entry under this instance's name from a previous session. Seed the entry
1441+ // through the ContextProxy before the provider is constructed so the ordinary
1442+ // loadViewState read (which goes through the proxy cache) actually sees it.
1443+ const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1444+ const proxy = new ContextProxy ( mockContext )
1445+ await proxy . setValue ( "viewStates" , {
1446+ [ tempId ] : { mode : "architect" , currentApiConfigName : "stale-profile" } ,
1447+ } )
1448+
1449+ fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1450+ expect ( fresh . viewId ) . toBe ( tempId )
1451+
1452+ // Await the load itself instead of a fixed timer: the negative
1453+ // assertion must run after the load has definitely finished.
1454+ await fresh [ "loadViewState" ] ( )
1455+
1456+ // The unowned temporary-id entry must not be adopted...
1457+ expect ( fresh [ "viewLocalState" ] ) . toStrictEqual ( { } )
1458+
1459+ // ...and the shared values still win, so the stale entry cannot reach getState().
1460+ const state = await fresh . getState ( )
1461+ expect ( state . mode ) . toBe ( "code" )
1462+ expect ( state . currentApiConfigName ) . not . toBe ( "stale-profile" )
1463+ } finally {
1464+ // dispose() drops the instance from the static list that the claimed-id check and
1465+ // the fan-out methods iterate; without it a leaked provider can affect a later
1466+ // test in this file.
1467+ await fresh ?. dispose ( )
1468+ }
13811469 } )
13821470
13831471 it ( "does not merge a stale temporary-id entry into this instance's first durable write" , async ( ) => {
1384- // A first write under an unowned temporary id claims the slot: fields left by an
1385- // earlier session under the same name must not be adopted (and later re-keyed to
1386- // the stable id) by merging into the colliding entry.
1387- const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1388- const proxy = new ContextProxy ( mockContext )
1389- await proxy . setValue ( "viewStates" , { [ tempId ] : { currentApiConfigName : "old-profile" , updatedAt : 1 } } )
1472+ // Declared outside the try so the finally can still reach the instance.
1473+ let fresh : ClineProvider | undefined
1474+ try {
1475+ // A first write under an unowned temporary id claims the slot: fields left by an
1476+ // earlier session under the same name must not be adopted (and later re-keyed to
1477+ // the stable id) by merging into the colliding entry.
1478+ const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1479+ const proxy = new ContextProxy ( mockContext )
1480+ await proxy . setValue ( "viewStates" , { [ tempId ] : { currentApiConfigName : "old-profile" , updatedAt : 1 } } )
13901481
1391- const fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1392- expect ( fresh . viewId ) . toBe ( tempId )
1482+ fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1483+ expect ( fresh . viewId ) . toBe ( tempId )
13931484
1394- await fresh . saveViewState ( "mode" , "code" )
1485+ await fresh . saveViewState ( "mode" , "code" )
13951486
1396- const states = fresh [ "getPersistedViewStates" ] ( { fresh : true } )
1397- expect ( states [ tempId ] ) . toEqual ( { mode : "code" , updatedAt : expect . any ( Number ) } )
1398- expect ( fresh [ "viewLocalState" ] . currentApiConfigName ) . toBeUndefined ( )
1487+ const states = fresh [ "getPersistedViewStates" ] ( { fresh : true } )
1488+ expect ( states [ tempId ] ) . toEqual ( { mode : "code" , updatedAt : expect . any ( Number ) } )
1489+ expect ( fresh [ "viewLocalState" ] . currentApiConfigName ) . toBeUndefined ( )
1490+ } finally {
1491+ // dispose() drops the instance from the static list that the claimed-id check and
1492+ // the fan-out methods iterate; without it a leaked provider can affect a later
1493+ // test in this file.
1494+ await fresh ?. dispose ( )
1495+ }
13991496 } )
14001497
14011498 it ( "does not mark the temporary id as authored when the durable write fails" , async ( ) => {
1402- // Authorship must be recorded only after the write lands: a failed persistence
1403- // that still marked the id owned would let a later load adopt an entry that was
1404- // never actually stored.
1405- const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1406- const proxy = new ContextProxy ( mockContext )
1407- const fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1408- expect ( fresh . viewId ) . toBe ( tempId )
1409-
1410- const originalUpdate = mockContext . globalState . update
1411- mockContext . globalState . update = vi . fn ( ) . mockImplementation ( ( key : string , _value : unknown ) => {
1412- if ( key === "viewStates" ) {
1413- return Promise . reject ( new Error ( "storage unavailable" ) )
1414- }
1415- return Promise . resolve ( )
1416- } )
1499+ // Declared outside the try so the finally can still reach the instance.
1500+ let fresh : ClineProvider | undefined
1501+ try {
1502+ // Authorship must be recorded only after the write lands: a failed persistence
1503+ // that still marked the id owned would let a later load adopt an entry that was
1504+ // never actually stored.
1505+ const tempId = `sidebar-${ ClineProvider [ "nextViewId" ] } `
1506+ const proxy = new ContextProxy ( mockContext )
1507+ fresh = new ClineProvider ( mockContext , mockOutputChannel , "sidebar" , proxy , new WebviewFocusTracker ( ) )
1508+ expect ( fresh . viewId ) . toBe ( tempId )
1509+
1510+ const originalUpdate = mockContext . globalState . update
1511+ mockContext . globalState . update = vi . fn ( ) . mockImplementation ( ( key : string , _value : unknown ) => {
1512+ if ( key === "viewStates" ) {
1513+ return Promise . reject ( new Error ( "storage unavailable" ) )
1514+ }
1515+ return Promise . resolve ( )
1516+ } )
14171517
1418- await expect ( fresh . saveViewState ( "mode" , "code" ) ) . rejects . toThrow ( "storage unavailable" )
1419- expect ( fresh [ "wroteUnderTemporaryViewStateId" ] ) . toBe ( false )
1518+ await expect ( fresh . saveViewState ( "mode" , "code" ) ) . rejects . toThrow ( "storage unavailable" )
1519+ expect ( fresh [ "wroteUnderTemporaryViewStateId" ] ) . toBe ( false )
14201520
1421- mockContext . globalState . update = originalUpdate
1521+ mockContext . globalState . update = originalUpdate
1522+ } finally {
1523+ // dispose() drops the instance from the static list that the claimed-id check and
1524+ // the fan-out methods iterate; without it a leaked provider can affect a later
1525+ // test in this file.
1526+ await fresh ?. dispose ( )
1527+ }
14221528 } )
14231529
14241530 it ( "does not adopt an entry under the temporary name that this instance never wrote" , async ( ) => {
0 commit comments