Skip to content

Commit fb218ab

Browse files
committed
wait for checkpoint to finish saving before running any tools
1 parent 6d8ddcf commit fb218ab

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

npm-app/src/cli-handlers/checkpoint.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ export function handleClearCheckpoints(): void {
237237
console.log('Cleared all checkpoints.')
238238
}
239239

240+
export async function waitForPreviousCheckpoint(): Promise<void> {
241+
try {
242+
// Make sure the previous checkpoint is done
243+
await checkpointManager.getLatestCheckpoint().fileStateIdPromise
244+
} catch (error) {
245+
// No latest checkpoint available, previous checkpoint is guaranteed to be done.
246+
}
247+
}
248+
240249
export async function saveCheckpoint(
241250
userInput: string,
242251
client: Client,
@@ -251,12 +260,7 @@ export async function saveCheckpoint(
251260
await readyPromise
252261
Spinner.get().stop()
253262

254-
try {
255-
// Make sure the previous checkpoint is done
256-
await checkpointManager.getLatestCheckpoint().fileStateIdPromise
257-
} catch (error) {
258-
// No latest checkpoint available, previous checkpoint is guaranteed to be done.
259-
}
263+
await waitForPreviousCheckpoint()
260264

261265
// Save the current agent state
262266
try {

npm-app/src/client.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import { activeBrowserRunner } from './browser-runner'
6262
import { setMessages } from './chat-storage'
6363
import { checkpointManager } from './checkpoints/checkpoint-manager'
6464
import { CLI } from './cli'
65+
import { waitForPreviousCheckpoint } from './cli-handlers/checkpoint'
6566
import { backendUrl, websiteUrl } from './config'
6667
import { CREDENTIALS_PATH, userFromJson } from './credentials'
6768
import { calculateFingerprint } from './fingerprint'
@@ -1085,26 +1086,26 @@ export class Client {
10851086
toolCall.name === 'str_replace' ||
10861087
toolCall.name === 'create_plan'
10871088
) {
1089+
await waitForPreviousCheckpoint()
10881090
// Save lastChanges for `diff` command
10891091
this.lastChanges.push(FileChangeSchema.parse(toolCall.parameters))
10901092
this.hadFileChanges = true
10911093
// Track the changed file path
10921094
this.filesChangedForHook.push(toolCall.parameters.path)
10931095
}
1094-
if (
1095-
toolCall.name === 'run_terminal_command' &&
1096-
toolCall.parameters.mode === 'user'
1097-
) {
1098-
// Special case: when terminal command is run as a user command, then no need to reprompt assistant.
1099-
this.responseComplete = true
1100-
isComplete = true
1101-
}
1102-
if (
1103-
toolCall.name === 'run_terminal_command' &&
1104-
toolCall.parameters.mode === 'assistant' &&
1105-
toolCall.parameters.process_type === 'BACKGROUND'
1106-
) {
1107-
this.oneTimeFlags[SHOULD_ASK_CONFIG] = true
1096+
if (toolCall.name === 'run_terminal_command') {
1097+
await waitForPreviousCheckpoint()
1098+
if (toolCall.parameters.mode === 'user') {
1099+
// Special case: when terminal command is run as a user command, then no need to reprompt assistant.
1100+
this.responseComplete = true
1101+
isComplete = true
1102+
}
1103+
if (
1104+
toolCall.parameters.mode === 'assistant' &&
1105+
toolCall.parameters.process_type === 'BACKGROUND'
1106+
) {
1107+
this.oneTimeFlags[SHOULD_ASK_CONFIG] = true
1108+
}
11081109
}
11091110
const toolResult = await handleToolCall(toolCall)
11101111
toolResults.push(toolResult)

0 commit comments

Comments
 (0)