Skip to content

Commit d02b544

Browse files
fix(checkpoints): stop the task that is still running after a partly rewound conversation
1 parent 48075a9 commit d02b544

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe("Checkpoint functionality", () => {
9999
apiConversationHistory: [],
100100
pendingUserMessageCheckpoint: undefined,
101101
say: vi.fn().mockResolvedValue(undefined),
102+
abortTask: vi.fn().mockResolvedValue(undefined),
102103
ensureApiConversationHistoryStored: vi.fn().mockResolvedValue(true),
103104
overwriteClineMessages: vi.fn(),
104105
overwriteApiConversationHistory: vi.fn(),
@@ -619,6 +620,47 @@ describe("Checkpoint functionality", () => {
619620
)
620621
// Not started again from two sides that are not the same conversation.
621622
expect(mockProvider.cancelTask).not.toHaveBeenCalled()
623+
expect(mockTask.abortTask).toHaveBeenCalledTimes(1) // and the task that is there does not go on
624+
})
625+
626+
it.each(["delete", "edit"] as const)(
627+
"stops the task that is still running after a partly rewound conversation, for %s, and says so when it cannot",
628+
async (operation) => {
629+
keepWhatIsWritten()
630+
mockTask.ensureApiConversationHistoryStored.mockResolvedValueOnce(false)
631+
mockTask.overwriteClineMessages
632+
.mockImplementationOnce(async (messages: unknown[]) => {
633+
mockTask.clineMessages = messages
634+
})
635+
.mockRejectedValueOnce(new Error("disk full"))
636+
mockTask.abortTask.mockRejectedValueOnce(new Error("cannot stop"))
637+
638+
await expect(
639+
checkpointRestore(mockTask, { ts: 2, commitHash: RESTORE_HASH, mode: "restore", operation }),
640+
).rejects.toMatchObject({ name: "ConversationRewindError", restarted: false })
641+
642+
expect(mockTask.abortTask).toHaveBeenCalledTimes(1)
643+
expect(mockProvider.log).toHaveBeenCalledWith(
644+
expect.stringContaining("the task could not be stopped: cannot stop"),
645+
)
646+
expect(mockProvider.cancelTask).not.toHaveBeenCalled()
647+
},
648+
)
649+
650+
it("does not stop the task when the messages were put back, since it is restarted from a consistent conversation", async () => {
651+
keepWhatIsWritten()
652+
mockTask.ensureApiConversationHistoryStored.mockResolvedValueOnce(false)
653+
654+
await expect(
655+
checkpointRestore(mockTask, {
656+
ts: 2,
657+
commitHash: RESTORE_HASH,
658+
mode: "restore",
659+
operation: "edit",
660+
}),
661+
).rejects.toMatchObject({ restarted: true })
662+
663+
expect(mockTask.abortTask).not.toHaveBeenCalled()
622664
})
623665

624666
it("tells a failure that is not an Error as text, in the message about a conversation that is only partly rewound", async () => {

‎src/core/checkpoints/index.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ export async function checkpointRestore(
278278
// A conversation whose two sides are not the same is not started again: the model would be given turns that
279279
// the user cannot see. It is reported as a restart that did not happen, so the edit is handed back.
280280
if (error instanceof ConversationPartlyRewoundError) {
281+
// The task must not go on either: an edit is not aborted by its caller (a delete is), and a task that
282+
// keeps running would give the model turns that the user cannot see.
283+
try {
284+
await task.abortTask()
285+
} catch (abortError) {
286+
provider?.log(
287+
`[checkpointRestore] the task could not be stopped: ${
288+
abortError instanceof Error ? abortError.message : String(abortError)
289+
}`,
290+
)
291+
}
281292
throw new ConversationRewindError(error, {
282293
error: new Error(
283294
"the task was not started again, because its conversation is only partly rewound",

0 commit comments

Comments
 (0)