Skip to content

Commit 6f6f5fb

Browse files
committed
Implement checkpoint operation locking to prevent concurrent checkpoint creation or restoration attempts
1 parent a83060f commit 6f6f5fb

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export const dictionary = {
8080
TYPE_SOMETHING_TO_USE_GROUP: 'Type something to use this group',
8181
GROUP_HAS_NO_SELECTED_PRESETS:
8282
'The chosen group has no selected presets to run',
83+
CHECKPOINT_OPERATION_IN_PROGRESS:
84+
'Another checkpoint operation is currently in progress. Please wait for it to complete.',
8385
URL_OVERRIDE_DIFFERENT_DOMAIN: (preset_name: string) =>
8486
`URL override for preset "${preset_name}" was discarded because it uses a different domain`,
8587
CONTEXT_SIZE_WARNING: (threshold: string, percentage: number) =>

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as vscode from 'vscode'
22
import {
33
CHECKPOINTS_STATE_KEY,
4-
TEMPORARY_CHECKPOINT_STATE_KEY
4+
TEMPORARY_CHECKPOINT_STATE_KEY,
5+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY
56
} from '../../../constants/state-keys'
67
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
78
import type { Checkpoint } from '../types'
@@ -19,6 +20,7 @@ import {
1920
import * as path from 'path'
2021
import { Logger } from '@shared/utils/logger'
2122
import { PanelProvider } from '@/views/panel/backend/panel-provider'
23+
import { dictionary } from '@shared/constants/dictionary'
2224

2325
export const create_checkpoint = async (
2426
workspace_provider: WorkspaceProvider,
@@ -28,6 +30,16 @@ export const create_checkpoint = async (
2830
description?: string
2931
): Promise<Checkpoint | undefined> => {
3032
try {
33+
const operation_in_progress = context.workspaceState.get<boolean>(
34+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY
35+
)
36+
if (operation_in_progress) {
37+
vscode.window.showWarningMessage(
38+
dictionary.warning_message.CHECKPOINT_OPERATION_IN_PROGRESS
39+
)
40+
return undefined
41+
}
42+
3143
const old_temp_checkpoint = context.workspaceState.get<Checkpoint>(
3244
TEMPORARY_CHECKPOINT_STATE_KEY
3345
)
@@ -62,6 +74,10 @@ export const create_checkpoint = async (
6274
let new_checkpoint: Checkpoint | undefined
6375

6476
const create_checkpoint_task = async () => {
77+
await context.workspaceState.update(
78+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
79+
true
80+
)
6581
await vscode.workspace.saveAll()
6682
const checkpoint_dir_path = get_checkpoint_path(timestamp)
6783
const checkpoint_dir_uri = vscode.Uri.file(checkpoint_dir_path)
@@ -159,6 +175,10 @@ export const create_checkpoint = async (
159175
checkpoints.unshift(checkpoint_object)
160176
await context.workspaceState.update(CHECKPOINTS_STATE_KEY, checkpoints)
161177
await panel_provider.send_checkpoints()
178+
await context.workspaceState.update(
179+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
180+
false
181+
)
162182
new_checkpoint = checkpoint_object
163183
}
164184

@@ -179,6 +199,10 @@ export const create_checkpoint = async (
179199
vscode.window.showErrorMessage(
180200
`Failed to create checkpoint: ${err.message}`
181201
)
202+
await context.workspaceState.update(
203+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
204+
false
205+
)
182206
return undefined
183207
}
184208
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode'
22
import * as path from 'path'
33
import * as os from 'os'
44
import { execSync } from 'child_process'
5-
import { TEMPORARY_CHECKPOINT_STATE_KEY } from '../../../constants/state-keys'
5+
import { TEMPORARY_CHECKPOINT_STATE_KEY, CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY } from '../../../constants/state-keys'
66
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
77
import type { Checkpoint } from '../types'
88
import { create_checkpoint } from './create-checkpoint'
@@ -15,6 +15,7 @@ import { Logger } from '@shared/utils/logger'
1515
import { PanelProvider } from '@/views/panel/backend/panel-provider'
1616
import { response_preview_promise_resolve } from '../../apply-chat-response-command/utils/preview'
1717
import { ongoing_review_cleanup_promise } from '../../apply-chat-response-command/utils/preview-handler'
18+
import { dictionary } from '@shared/constants/dictionary'
1819

1920
export const restore_checkpoint = async (params: {
2021
checkpoint: Checkpoint
@@ -23,6 +24,16 @@ export const restore_checkpoint = async (params: {
2324
panel_provider: PanelProvider
2425
options?: { skip_confirmation?: boolean }
2526
}) => {
27+
const operation_in_progress = params.context.workspaceState.get<boolean>(
28+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY
29+
)
30+
if (operation_in_progress) {
31+
vscode.window.showWarningMessage(
32+
dictionary.warning_message.CHECKPOINT_OPERATION_IN_PROGRESS
33+
)
34+
return
35+
}
36+
2637
const tab_groups = vscode.window.tabGroups.all
2738
const open_files: {
2839
uri: vscode.Uri
@@ -103,6 +114,10 @@ export const restore_checkpoint = async (params: {
103114
}
104115

105116
try {
117+
await params.context.workspaceState.update(
118+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
119+
true
120+
)
106121
await vscode.window.withProgress(
107122
{
108123
location: vscode.ProgressLocation.Notification,
@@ -358,6 +373,10 @@ export const restore_checkpoint = async (params: {
358373
vscode.window.showInformationMessage(message)
359374
}
360375
} catch (err: any) {
376+
await params.context.workspaceState.update(
377+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
378+
false
379+
)
361380
vscode.window.showErrorMessage(
362381
`Failed to restore checkpoint: ${err.message}`
363382
)
@@ -372,5 +391,10 @@ export const restore_checkpoint = async (params: {
372391
undefined
373392
)
374393
}
394+
} finally {
395+
await params.context.workspaceState.update(
396+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
397+
false
398+
)
375399
}
376400
}

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const LAST_REFACTOR_INSTRUCTION_SOURCE_STATE_KEY =
3030
export const LAST_REFACTOR_INSTRUCTION_STATE_KEY = 'last-refactor-instruction'
3131
export const CHECKPOINTS_STATE_KEY = 'checkpoints'
3232
export const TEMPORARY_CHECKPOINT_STATE_KEY = 'temporary-checkpoint'
33+
export const CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY = 'checkpoint-operation-in-progress'
3334

3435
export const DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY =
3536
'duplicate-workspace-context'

0 commit comments

Comments
 (0)