@@ -510,16 +510,128 @@ describe("Checkpoint functionality", () => {
510510 expect ( mockProvider . cancelTask ) . toHaveBeenCalled ( )
511511 } )
512512
513- it ( "should handle missing message gracefully" , async ( ) => {
514- await checkpointRestore ( mockTask , {
515- ts : 999 , // Non-existent timestamp
516- commitHash : RESTORE_HASH ,
517- mode : "restore" ,
518- } )
513+ it ( "reports a restore to a message that is no longer there, and restores nothing" , async ( ) => {
514+ await expect (
515+ checkpointRestore ( mockTask , {
516+ ts : 999 , // Non-existent timestamp
517+ commitHash : RESTORE_HASH ,
518+ mode : "restore" ,
519+ } ) ,
520+ ) . rejects . toThrow ( "no longer in the conversation" )
519521
520522 expect ( mockCheckpointService . restoreCheckpoint ) . not . toHaveBeenCalled ( )
521523 } )
522524
525+ it ( "has nothing to report for a preview of a message that is no longer there" , async ( ) => {
526+ await expect (
527+ checkpointRestore ( mockTask , { ts : 999 , commitHash : RESTORE_HASH , mode : "preview" } ) ,
528+ ) . resolves . toBeUndefined ( )
529+
530+ expect ( mockCheckpointService . restoreCheckpoint ) . not . toHaveBeenCalled ( )
531+ } )
532+
533+ it . each ( [ "delete" , "edit" ] as const ) (
534+ "reports a restore when checkpoints were disabled after the checkpoint was recorded, instead of returning as if it worked (%s)" ,
535+ async ( operation ) => {
536+ mockTask . enableCheckpoints = false
537+
538+ await expect (
539+ checkpointRestore ( mockTask , { ts : 2 , commitHash : RESTORE_HASH , mode : "restore" , operation } ) ,
540+ ) . rejects . toThrow ( "Checkpoints are not available for this task" )
541+
542+ // Nothing was restored or rewound, and nothing was started: the caller is told, and can offer to start the task.
543+ expect ( mockCheckpointService . restoreCheckpoint ) . not . toHaveBeenCalled ( )
544+ expect ( mockTask . overwriteClineMessages ) . not . toHaveBeenCalled ( )
545+ expect ( mockProvider . cancelTask ) . not . toHaveBeenCalled ( )
546+ } ,
547+ )
548+
549+ it ( "has nothing to report for a preview when checkpoints are disabled" , async ( ) => {
550+ mockTask . enableCheckpoints = false
551+
552+ await expect (
553+ checkpointRestore ( mockTask , { ts : 2 , commitHash : RESTORE_HASH , mode : "preview" } ) ,
554+ ) . resolves . toBeUndefined ( )
555+ } )
556+
557+ describe ( "when the API history cannot follow the cut of the messages" , ( ) => {
558+ const original = ( ) => [
559+ { ts : 1 , say : "user" , text : "Message 1" } ,
560+ { ts : 2 , say : "assistant" , text : "Message 2" } ,
561+ { ts : 3 , say : "user" , text : "Message 3" } ,
562+ { ts : 4 , say : "checkpoint_saved" , text : RESTORE_HASH } ,
563+ ]
564+ // The task keeps what it is given, as the real one does.
565+ const keepWhatIsWritten = ( ) =>
566+ mockTask . overwriteClineMessages . mockImplementation ( async ( messages : unknown [ ] ) => {
567+ mockTask . clineMessages = messages
568+ } )
569+
570+ it ( "puts the messages back, so the task is started again with both histories as they were" , async ( ) => {
571+ keepWhatIsWritten ( )
572+ mockTask . ensureApiConversationHistoryStored . mockResolvedValueOnce ( false )
573+
574+ await expect (
575+ checkpointRestore ( mockTask , {
576+ ts : 2 ,
577+ commitHash : RESTORE_HASH ,
578+ mode : "restore" ,
579+ operation : "delete" ,
580+ } ) ,
581+ ) . rejects . toMatchObject ( {
582+ name : "ConversationRewindError" ,
583+ message : "The API conversation history could not be saved" ,
584+ restarted : true ,
585+ } )
586+
587+ // The cut was written, and then the messages that were there before it.
588+ expect ( mockTask . overwriteClineMessages ) . toHaveBeenCalledTimes ( 2 )
589+ expect ( mockTask . overwriteClineMessages . mock . calls [ 0 ] [ 0 ] ) . toHaveLength ( 1 )
590+ expect ( mockTask . overwriteClineMessages . mock . calls [ 1 ] [ 0 ] ) . toEqual ( original ( ) )
591+ expect ( mockProvider . cancelTask ) . toHaveBeenCalledTimes ( 1 ) // restarted, from what is on disk
592+ } )
593+
594+ it ( "says that the conversation is only partly rewound when the messages cannot be put back either" , async ( ) => {
595+ keepWhatIsWritten ( )
596+ mockTask . ensureApiConversationHistoryStored . mockResolvedValueOnce ( false )
597+ mockTask . overwriteClineMessages
598+ . mockImplementationOnce ( async ( messages : unknown [ ] ) => {
599+ mockTask . clineMessages = messages
600+ } )
601+ . mockRejectedValueOnce ( new Error ( "disk full" ) )
602+
603+ const restoring = checkpointRestore ( mockTask , {
604+ ts : 2 ,
605+ commitHash : RESTORE_HASH ,
606+ mode : "restore" ,
607+ operation : "delete" ,
608+ } )
609+
610+ await expect ( restoring ) . rejects . toMatchObject ( { name : "ConversationRewindError" } )
611+ await expect ( restoring ) . rejects . toThrow (
612+ / c o u l d n o t b e s a v e d , a n d t h e m e s s a g e s c o u l d n o t b e p u t b a c k \( d i s k f u l l \) .* o n l y p a r t l y r e w o u n d / ,
613+ )
614+ expect ( mockProvider . cancelTask ) . toHaveBeenCalledTimes ( 1 )
615+ } )
616+
617+ it ( "puts nothing back when the messages were not cut" , async ( ) => {
618+ keepWhatIsWritten ( )
619+ mockTask . overwriteClineMessages . mockReset ( )
620+ mockTask . overwriteClineMessages . mockRejectedValueOnce ( new Error ( "Failed to save the messages" ) )
621+
622+ await expect (
623+ checkpointRestore ( mockTask , {
624+ ts : 2 ,
625+ commitHash : RESTORE_HASH ,
626+ mode : "restore" ,
627+ operation : "delete" ,
628+ } ) ,
629+ ) . rejects . toMatchObject ( { name : "ConversationRewindError" } )
630+
631+ expect ( mockTask . overwriteClineMessages ) . toHaveBeenCalledTimes ( 1 ) // the one that failed, and no put-back
632+ } )
633+ } )
634+
523635 it . each ( [ "delete" , "edit" ] as const ) (
524636 "propagates restore failures for %s without rewinding" ,
525637 async ( operation ) => {
0 commit comments