From 189b91ca36c591d1f009fa9add08d771ce8aa92b Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Mon, 16 Jun 2025 23:05:56 -0700 Subject: [PATCH] Fix: clineMessages not storing all checkpoints commitHashes (#4225) * Fixed issue where checkpoint commitHash was not being saved to every clineMessage in state, added handling in case checkpointTracker was not initialized (resumed tasks) * changeset * prettier --- .changeset/sour-peaches-float.md | 5 +++ src/core/task/index.ts | 63 ++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 .changeset/sour-peaches-float.md diff --git a/.changeset/sour-peaches-float.md b/.changeset/sour-peaches-float.md new file mode 100644 index 00000000..a638be1d --- /dev/null +++ b/.changeset/sour-peaches-float.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Fix where clineMessages were not saving checkpoint commitHash on some messages diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 419640bc..cc075b0f 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -429,6 +429,18 @@ export class Task { vscode.window.showErrorMessage("Failed to restore offsetcheckpoint: " + errorMessage) didWorkspaceRestoreFail = true } + } else if (!offset && lastMessageWithHash.lastCheckpointHash && this.checkpointTracker) { + // Fallback: restore to most recent checkpoint when target message has no checkpoint hash + console.warn(`Message ${messageTs} has no checkpoint hash, falling back to previous checkpoint`) + try { + await this.checkpointTracker.resetHead(lastMessageWithHash.lastCheckpointHash) + } catch (error) { + const errorMessage = error instanceof Error ? error.message : "Unknown error" + vscode.window.showErrorMessage("Failed to restore checkpoint: " + errorMessage) + didWorkspaceRestoreFail = true + } + } else { + vscode.window.showErrorMessage("Failed to restore checkpoint") } break } @@ -1262,23 +1274,43 @@ export class Task { return } - // For non-attempt completion we just say checkpoints - await this.say("checkpoint_created") - this.checkpointTracker?.commit().then(async (commitHash) => { - const lastCheckpointMessage = findLast(this.clineMessages, (m) => m.say === "checkpoint_created") - if (lastCheckpointMessage) { - lastCheckpointMessage.lastCheckpointHash = commitHash - await saveClineMessagesAndUpdateHistory( - this.getContext(), + // Initialize checkpoint tracker if it doesn't exist + if (!this.checkpointTracker && !this.checkpointTrackerErrorMessage) { + try { + this.checkpointTracker = await CheckpointTracker.create( this.taskId, - this.clineMessages, - this.taskIsFavorited ?? false, - this.conversationHistoryDeletedRange, - this.checkpointTracker, - this.updateTaskHistory, + this.context.globalStorageUri.fsPath, + this.enableCheckpoints, ) + } catch (error) { + const errorMessage = error instanceof Error ? error.message : "Unknown error" + console.error("Failed to initialize checkpoint tracker:", errorMessage) + this.checkpointTrackerErrorMessage = errorMessage + await this.postStateToWebview() + return } - }) // silently fails for now + } + + // Create a checkpoint commit and update clineMessages with a commitHash + if (this.checkpointTracker) { + const commitHash = await this.checkpointTracker.commit() + if (commitHash) { + await this.say("checkpoint_created") + const lastCheckpointMessage = findLast(this.clineMessages, (m) => m.say === "checkpoint_created") + if (lastCheckpointMessage) { + lastCheckpointMessage.lastCheckpointHash = commitHash + await saveClineMessagesAndUpdateHistory( + this.getContext(), + this.taskId, + this.clineMessages, + this.taskIsFavorited ?? false, + this.conversationHistoryDeletedRange, + this.checkpointTracker, + this.updateTaskHistory, + ) + } + } + } // silently fails for now // } else { @@ -1740,7 +1772,6 @@ export class Task { clineIgnoreInstructions, preferredLanguageInstructions, ) - console.log("[INSTRUCTIONS] User instructions:", userInstructions) systemPrompt += userInstructions } const contextManagementMetadata = await this.contextManager.getNewContextMessagesAndMetadata( @@ -4442,8 +4473,8 @@ export class Task { // Now, if it's the first request AND checkpoints are enabled AND tracker was successfully initialized, // then say "checkpoint_created" and perform the commit. if (isFirstRequest && this.enableCheckpoints && this.checkpointTracker) { - await this.say("checkpoint_created") // Now this is conditional const commitHash = await this.checkpointTracker.commit() // Actual commit + await this.say("checkpoint_created") // Now this is conditional const lastCheckpointMessage = findLast(this.clineMessages, (m) => m.say === "checkpoint_created") if (lastCheckpointMessage) { lastCheckpointMessage.lastCheckpointHash = commitHash