@@ -387,6 +387,94 @@ describe("ClineProvider Task History Synchronization", () => {
387387 return calls . filter ( ( call ) => call [ 0 ] ?. type === type )
388388 }
389389
390+ describe ( "legacy history cleanup" , ( ) => {
391+ let legacyHistory : HistoryItem [ ]
392+
393+ beforeEach ( async ( ) => {
394+ legacyHistory = [ createHistoryItem ( { id : "legacy-task" , task : "Legacy task" } ) ]
395+ await mockContext . globalState . update ( "taskHistory" , legacyHistory )
396+ await mockContext . globalState . update ( "taskHistoryMigratedToFiles" , undefined )
397+ vi . mocked ( mockContext . globalState . update ) . mockClear ( )
398+ provider [ "taskHistoryStoreInitialized" ] = false
399+ } )
400+
401+ it . each ( [ false , true ] ) (
402+ "clears legacy history after migration (already migrated: %s)" ,
403+ async ( alreadyMigrated ) => {
404+ await mockContext . globalState . update ( "taskHistoryMigratedToFiles" , alreadyMigrated )
405+ vi . mocked ( mockContext . globalState . update ) . mockClear ( )
406+ const migrate = vi
407+ . spyOn ( provider . taskHistoryStore , "migrateFromGlobalState" )
408+ . mockImplementation ( async ( ) => {
409+ expect ( mockContext . globalState . get ( "taskHistory" ) ) . toEqual ( legacyHistory )
410+ expect ( mockContext . globalState . get ( "taskHistoryMigratedToFiles" ) ) . toBe ( false )
411+ } )
412+
413+ await provider [ "initializeTaskHistoryStore" ] ( )
414+
415+ expect ( migrate ) . toHaveBeenCalledTimes ( alreadyMigrated ? 0 : 1 )
416+ if ( ! alreadyMigrated ) {
417+ expect ( migrate ) . toHaveBeenCalledWith ( legacyHistory )
418+ }
419+ expect ( vi . mocked ( mockContext . globalState . update ) . mock . calls ) . toEqual (
420+ alreadyMigrated
421+ ? [ [ "taskHistory" , undefined ] ]
422+ : [
423+ [ "taskHistoryMigratedToFiles" , true ] ,
424+ [ "taskHistory" , undefined ] ,
425+ ] ,
426+ )
427+ expect ( mockContext . globalState . get ( "taskHistory" ) ) . toBeUndefined ( )
428+ expect ( provider [ "taskHistoryStoreInitialized" ] ) . toBe ( true )
429+ } ,
430+ )
431+
432+ it . each ( [ "initialization" , "migration" , "marker" ] ) ( "preserves legacy history when %s fails" , async ( stage ) => {
433+ const error = new Error ( `${ stage } failed` )
434+ const migrate = vi . spyOn ( provider . taskHistoryStore , "migrateFromGlobalState" ) . mockResolvedValue ( undefined )
435+ if ( stage === "initialization" ) {
436+ vi . spyOn ( provider . taskHistoryStore , "initialize" ) . mockRejectedValueOnce ( error )
437+ } else if ( stage === "migration" ) {
438+ migrate . mockRejectedValueOnce ( error )
439+ } else {
440+ vi . mocked ( mockContext . globalState . update ) . mockRejectedValueOnce ( error )
441+ }
442+
443+ await provider [ "initializeTaskHistoryStore" ] ( )
444+
445+ expect ( mockContext . globalState . update ) . not . toHaveBeenCalledWith ( "taskHistory" , undefined )
446+ expect ( mockContext . globalState . get ( "taskHistory" ) ) . toEqual ( legacyHistory )
447+ expect ( mockContext . globalState . get ( "taskHistoryMigratedToFiles" ) ) . toBeUndefined ( )
448+ expect ( provider [ "taskHistoryStoreInitialized" ] ) . toBe ( false )
449+ } )
450+
451+ it ( "keeps the store authoritative if cleanup fails and retries cleanup on initialization" , async ( ) => {
452+ await mockContext . globalState . update ( "taskHistoryMigratedToFiles" , true )
453+ vi . mocked ( mockContext . globalState . update ) . mockRejectedValueOnce ( new Error ( "cleanup failed" ) )
454+
455+ await provider [ "initializeTaskHistoryStore" ] ( )
456+
457+ expect ( mockContext . globalState . get ( "taskHistory" ) ) . toEqual ( legacyHistory )
458+ expect ( provider [ "taskHistoryStoreInitialized" ] ) . toBe ( true )
459+ await expect ( provider . getTaskWithId ( "legacy-task" ) ) . rejects . toThrow ( "Task not found" )
460+
461+ await provider [ "initializeTaskHistoryStore" ] ( )
462+
463+ expect ( mockContext . globalState . get ( "taskHistory" ) ) . toBeUndefined ( )
464+ } )
465+
466+ it ( "does not write globalState again after cleanup" , async ( ) => {
467+ await mockContext . globalState . update ( "taskHistoryMigratedToFiles" , true )
468+ await mockContext . globalState . update ( "taskHistory" , undefined )
469+ vi . mocked ( mockContext . globalState . update ) . mockClear ( )
470+
471+ await provider [ "initializeTaskHistoryStore" ] ( )
472+
473+ expect ( mockContext . globalState . update ) . not . toHaveBeenCalled ( )
474+ expect ( provider [ "taskHistoryStoreInitialized" ] ) . toBe ( true )
475+ } )
476+ } )
477+
390478 it ( "uses per-task files without registering a globalState write-through callback" , ( ) => {
391479 expect ( provider . taskHistoryStore [ "onWrite" ] ) . toBeUndefined ( )
392480 } )
0 commit comments