@@ -22,6 +22,52 @@ import { Logger } from '@shared/utils/logger'
2222import { PanelProvider } from '@/views/panel/backend/panel-provider'
2323import { dictionary } from '@shared/constants/dictionary'
2424
25+ const remove_old_checkpoints = async (
26+ checkpoints : Checkpoint [ ]
27+ ) : Promise < Checkpoint [ ] > => {
28+ const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
29+ const checkpoint_lifespan_hours =
30+ config . get < number > ( 'checkpointLifespan' ) || 48
31+ const checkpoint_lifespan_ms = checkpoint_lifespan_hours * 60 * 60 * 1000
32+
33+ const now = Date . now ( )
34+ const cutoff_time = now - checkpoint_lifespan_ms
35+
36+ const checkpoints_to_keep : Checkpoint [ ] = [ ]
37+ const checkpoints_to_remove : Checkpoint [ ] = [ ]
38+
39+ for ( const checkpoint of checkpoints ) {
40+ if ( checkpoint . timestamp < cutoff_time ) {
41+ checkpoints_to_remove . push ( checkpoint )
42+ } else {
43+ checkpoints_to_keep . push ( checkpoint )
44+ }
45+ }
46+
47+ // Delete old checkpoint files
48+ for ( const checkpoint of checkpoints_to_remove ) {
49+ try {
50+ const checkpoint_path = get_checkpoint_path ( checkpoint . timestamp )
51+ await vscode . workspace . fs . delete ( vscode . Uri . file ( checkpoint_path ) , {
52+ recursive : true
53+ } )
54+ Logger . info ( {
55+ function_name : 'remove_old_checkpoints' ,
56+ message : `Removed checkpoint older than 48 hours: ${ checkpoint . title } ` ,
57+ data : { timestamp : checkpoint . timestamp }
58+ } )
59+ } catch ( error ) {
60+ Logger . warn ( {
61+ function_name : 'remove_old_checkpoints' ,
62+ message : 'Could not delete old checkpoint file' ,
63+ data : error
64+ } )
65+ }
66+ }
67+
68+ return checkpoints_to_keep
69+ }
70+
2571export const create_checkpoint = async (
2672 workspace_provider : WorkspaceProvider ,
2773 context : vscode . ExtensionContext ,
@@ -159,7 +205,10 @@ export const create_checkpoint = async (
159205 }
160206 }
161207
162- const checkpoints = await get_checkpoints ( context )
208+ let checkpoints = await get_checkpoints ( context )
209+
210+ // Remove checkpoints older than 48 hours
211+ checkpoints = await remove_old_checkpoints ( checkpoints )
163212
164213 const final_description = get_incremented_description (
165214 title ,
0 commit comments