Skip to content

Commit 630c8bc

Browse files
FIX + feat: add MessageManager layer for centralized history coordination (#9842)
1 parent a1d392f commit 630c8bc

File tree

10 files changed

+1024
-118
lines changed

10 files changed

+1024
-118
lines changed

src/core/checkpoints/__tests__/checkpoint.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach, Mock } from "vitest"
22
import { Task } from "../../task/Task"
33
import { ClineProvider } from "../../webview/ClineProvider"
44
import { checkpointSave, checkpointRestore, checkpointDiff, getCheckpointService } from "../index"
5+
import { MessageManager } from "../../message-manager"
56
import * as vscode from "vscode"
67

78
// Mock vscode
@@ -102,6 +103,7 @@ describe("Checkpoint functionality", () => {
102103
overwriteApiConversationHistory: vi.fn(),
103104
combineMessages: vi.fn().mockReturnValue([]),
104105
}
106+
mockTask.messageManager = new MessageManager(mockTask)
105107

106108
// Update the mock to return our mockCheckpointService
107109
const checkpointsModule = await import("../../../services/checkpoints")

src/core/checkpoints/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,20 @@ export async function checkpointRestore(
258258
await provider?.postMessageToWebview({ type: "currentCheckpointUpdated", text: commitHash })
259259

260260
if (mode === "restore") {
261-
await task.overwriteApiConversationHistory(task.apiConversationHistory.filter((m) => !m.ts || m.ts < ts))
262-
261+
// Calculate metrics from messages that will be deleted (must be done before rewind)
263262
const deletedMessages = task.clineMessages.slice(index + 1)
264263

265264
const { totalTokensIn, totalTokensOut, totalCacheWrites, totalCacheReads, totalCost } = getApiMetrics(
266265
task.combineMessages(deletedMessages),
267266
)
268267

269-
// For delete operations, exclude the checkpoint message itself
270-
// For edit operations, include the checkpoint message (to be edited)
271-
const endIndex = operation === "edit" ? index + 1 : index
272-
await task.overwriteClineMessages(task.clineMessages.slice(0, endIndex))
268+
// Use MessageManager to properly handle context-management events
269+
// This ensures orphaned Summary messages and truncation markers are cleaned up
270+
await task.messageManager.rewindToTimestamp(ts, {
271+
includeTargetMessage: operation === "edit",
272+
})
273273

274-
// TODO: Verify that this is working as expected.
274+
// Report the deleted API request metrics
275275
await task.say(
276276
"api_req_deleted",
277277
JSON.stringify({

0 commit comments

Comments
 (0)