Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/core/assistant-message/presentAssistantMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,11 @@ export async function presentAssistantMessage(cline: Task) {

switch (block.name) {
case "write_to_file":
await checkpointSaveAndMark(cline)
// Save checkpoint BEFORE file edit
await checkpointSaveAndMark(cline, "before")
await writeToFileTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
// Save checkpoint AFTER file edit
await checkpointSaveAndMark(cline, "after")
break
case "update_todo_list":
await updateTodoListTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
Expand All @@ -429,11 +432,12 @@ export async function presentAssistantMessage(cline: Task) {
)
}

// Save checkpoint BEFORE file edit
await checkpointSaveAndMark(cline, "before")

if (isMultiFileApplyDiffEnabled) {
await checkpointSaveAndMark(cline)
await applyDiffTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
} else {
await checkpointSaveAndMark(cline)
await applyDiffToolLegacy(
cline,
block,
Expand All @@ -443,15 +447,23 @@ export async function presentAssistantMessage(cline: Task) {
removeClosingTag,
)
}
// Save checkpoint AFTER file edit
await checkpointSaveAndMark(cline, "after")
break
}
case "insert_content":
await checkpointSaveAndMark(cline)
// Save checkpoint BEFORE file edit
await checkpointSaveAndMark(cline, "before")
await insertContentTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
// Save checkpoint AFTER file edit
await checkpointSaveAndMark(cline, "after")
break
case "search_and_replace":
await checkpointSaveAndMark(cline)
// Save checkpoint BEFORE file edit
await checkpointSaveAndMark(cline, "before")
await searchAndReplaceTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
// Save checkpoint AFTER file edit
await checkpointSaveAndMark(cline, "after")
break
case "read_file":
await readFileTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
Expand Down Expand Up @@ -583,16 +595,25 @@ export async function presentAssistantMessage(cline: Task) {
/**
* save checkpoint and mark done in the current streaming task.
* @param task The Task instance to checkpoint save and mark.
* @param timing Whether this is a "before" or "after" checkpoint for file edits
* @returns
*/
async function checkpointSaveAndMark(task: Task) {
if (task.currentStreamingDidCheckpoint) {
async function checkpointSaveAndMark(task: Task, timing?: "before" | "after") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice implementation of the dual checkpoint strategy! The optional timing parameter is a clean way to distinguish between "before" and "after" checkpoints while maintaining backward compatibility.

One suggestion for future improvement: Consider adding a brief comment here explaining the checkpoint strategy (before for recovery, after for progression) to help future maintainers understand the design decision.

// For "before" checkpoints, always save regardless of currentStreamingDidCheckpoint
// For "after" checkpoints or no timing specified, use the original logic
if (timing !== "before" && task.currentStreamingDidCheckpoint) {
return
}
try {
await task.checkpointSave(true)
task.currentStreamingDidCheckpoint = true
// Only mark as done for "after" checkpoints or when no timing is specified
if (timing !== "before") {
task.currentStreamingDidCheckpoint = true
}
} catch (error) {
console.error(`[Task#presentAssistantMessage] Error saving checkpoint: ${error.message}`, error)
console.error(
`[Task#presentAssistantMessage] Error saving checkpoint (${timing || "default"}): ${error.message}`,
error,
)
}
}
Loading
Loading