Skip to content

Commit e31c503

Browse files
committed
Refactor remove_old_checkpoints into its own file get-checkpoints.ts and ensure starred checkpoints are preserved during cleanup
1 parent cbd1d9a commit e31c503

3 files changed

Lines changed: 61 additions & 46 deletions

File tree

‎packages/vscode/src/commands/checkpoints-command/actions/create-checkpoint.ts‎

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,7 @@ import { PanelProvider } from '@/views/panel/backend/panel-provider'
2323
import { dictionary } from '@shared/constants/dictionary'
2424
import { WebsitesProvider } from '@/context/providers/websites-provider'
2525
import { response_preview_promise_resolve } from '../../apply-chat-response-command/utils/preview'
26-
27-
const remove_old_checkpoints = async (
28-
checkpoints: Checkpoint[]
29-
): Promise<Checkpoint[]> => {
30-
const config = vscode.workspace.getConfiguration('codeWebChat')
31-
const checkpoint_lifespan_hours =
32-
config.get<number>('checkpointLifespan') || 48
33-
const checkpoint_lifespan_ms = checkpoint_lifespan_hours * 60 * 60 * 1000
34-
35-
const now = Date.now()
36-
const cutoff_time = now - checkpoint_lifespan_ms
37-
38-
const checkpoints_to_keep: Checkpoint[] = []
39-
const checkpoints_to_remove: Checkpoint[] = []
40-
41-
for (const checkpoint of checkpoints) {
42-
if (checkpoint.timestamp < cutoff_time) {
43-
checkpoints_to_remove.push(checkpoint)
44-
} else {
45-
checkpoints_to_keep.push(checkpoint)
46-
}
47-
}
48-
49-
// Delete old checkpoint files
50-
for (const checkpoint of checkpoints_to_remove) {
51-
try {
52-
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
53-
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
54-
recursive: true
55-
})
56-
Logger.info({
57-
function_name: 'remove_old_checkpoints',
58-
message: `Removed checkpoint older than 48 hours: ${checkpoint.title}`,
59-
data: { timestamp: checkpoint.timestamp }
60-
})
61-
} catch (error) {
62-
Logger.warn({
63-
function_name: 'remove_old_checkpoints',
64-
message: 'Could not delete old checkpoint file',
65-
data: error
66-
})
67-
}
68-
}
69-
70-
return checkpoints_to_keep
71-
}
26+
import { remove_old_checkpoints } from './get-checkpoints'
7227

7328
export const create_checkpoint = async (
7429
workspace_provider: WorkspaceProvider,

‎packages/vscode/src/commands/checkpoints-command/actions/get-checkpoints.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,53 @@ import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
33
import type { Checkpoint } from '../types'
44
import { get_checkpoint_path } from '../utils'
55

6+
import { Logger } from '@shared/utils/logger'
7+
8+
export const remove_old_checkpoints = async (
9+
checkpoints: Checkpoint[]
10+
): Promise<Checkpoint[]> => {
11+
const config = vscode.workspace.getConfiguration('codeWebChat')
12+
const checkpoint_lifespan_hours =
13+
config.get<number>('checkpointLifespan') || 48
14+
const checkpoint_lifespan_ms = checkpoint_lifespan_hours * 60 * 60 * 1000
15+
16+
const now = Date.now()
17+
const cutoff_time = now - checkpoint_lifespan_ms
18+
19+
const checkpoints_to_keep: Checkpoint[] = []
20+
const checkpoints_to_remove: Checkpoint[] = []
21+
22+
for (const checkpoint of checkpoints) {
23+
if (checkpoint.timestamp < cutoff_time && !checkpoint.is_starred) {
24+
checkpoints_to_remove.push(checkpoint)
25+
} else {
26+
checkpoints_to_keep.push(checkpoint)
27+
}
28+
}
29+
30+
// Delete old checkpoint files
31+
for (const checkpoint of checkpoints_to_remove) {
32+
try {
33+
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
34+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
35+
recursive: true
36+
})
37+
Logger.info({
38+
function_name: 'remove_old_checkpoints',
39+
message: `Removed old checkpoint: ${checkpoint.title}`,
40+
data: { timestamp: checkpoint.timestamp }
41+
})
42+
} catch (error) {
43+
Logger.warn({
44+
function_name: 'remove_old_checkpoints',
45+
message: 'Could not delete old checkpoint file',
46+
data: error
47+
})
48+
}
49+
}
50+
51+
return checkpoints_to_keep
52+
}
653
export const get_checkpoints = async (
754
context: vscode.ExtensionContext
855
): Promise<Checkpoint[]> => {

‎packages/vscode/src/extension.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import {
2626
open_url_command,
2727
generate_commit_message_command
2828
} from './commands'
29+
import {
30+
get_checkpoints,
31+
remove_old_checkpoints
32+
} from './commands/checkpoints-command/actions'
33+
import { CHECKPOINTS_STATE_KEY } from './constants/state-keys'
2934
import { SettingsProvider } from './views/settings/backend/settings-provider'
3035

3136
// Store WebSocketServer instance at module level
@@ -48,6 +53,14 @@ export async function activate(context: vscode.ExtensionContext) {
4853

4954
await migrations()
5055

56+
const startup_tasks = async () => {
57+
const valid_checkpoints = await get_checkpoints(context)
58+
const kept_checkpoints = await remove_old_checkpoints(valid_checkpoints)
59+
await context.workspaceState.update(CHECKPOINTS_STATE_KEY, kept_checkpoints)
60+
}
61+
// Run startup tasks without blocking extension activation
62+
startup_tasks()
63+
5164
const panel_provider = new PanelProvider(
5265
context.extensionUri,
5366
workspace_provider,

0 commit comments

Comments
 (0)