Skip to content

Commit 1e0e968

Browse files
committed
fix: retain resume recovery history for aborted standalone tasks
1 parent 58d9f0e commit 1e0e968

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,17 +3458,21 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
34583458
throw error
34593459
}
34603460

3461-
if ((this.abort || this.abandoned) && this.parentTaskId) {
3462-
// The commit activated a child whose runner was aborted or
3461+
if (this.abort || this.abandoned) {
3462+
// The commit activated a task whose runner was aborted or
34633463
// abandoned while the commit was in flight, so the cancel or
3464-
// eviction skipped its own interruption marking. Re-interrupt
3465-
// first, then restore the pre-resume history while the record
3466-
// status still matches this resume's starting status. The
3467-
// restore removes the recovery copy only when it succeeds.
3468-
await provider.reinterruptAbandonedChild({
3469-
childTaskId: this.taskId,
3470-
parentTaskId: this.parentTaskId,
3471-
})
3464+
// eviction skipped its own interruption marking. A linked child
3465+
// is re-interrupted under its parent transition lock. A
3466+
// standalone record has no parent transition, so it stays
3467+
// active and the restore's committed_elsewhere gate keeps the
3468+
// recovery copy available. Either way the restore runs, and it
3469+
// removes the recovery copy only when it succeeds.
3470+
if (this.parentTaskId) {
3471+
await provider.reinterruptAbandonedChild({
3472+
childTaskId: this.taskId,
3473+
parentTaskId: this.parentTaskId,
3474+
})
3475+
}
34723476
await this.restorePreparedResumeHistory(
34733477
recoveryCopyPath,
34743478
existingApiConversationHistory,

‎src/core/task/__tests__/Task.resumeApiHistoryPersistence.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,41 @@ describe("Task resume api-history persistence on real files", () => {
374374
expect(await findRecoveryCopyName(taskDir)).toBeUndefined()
375375
})
376376

377+
it("keeps a standalone aborted task active and retains the recovery copy after the commit lands", async () => {
378+
// A standalone record has no parent transition, so the committed
379+
// active status stands. The restore must still run so its
380+
// committed_elsewhere gate retains the recovery copy, and the task
381+
// loop must not start on the aborted runner.
382+
const standaloneRecord = makeChildRecord({ parentTaskId: undefined, rootTaskId: undefined })
383+
await provider.taskHistoryStore.upsert(standaloneRecord)
384+
const taskDir = await seedTaskFiles(storagePath, childId, authoritativeApiHistory)
385+
386+
const { task, loop, resumeActivation } = prepareResumedTask(standaloneRecord)
387+
const reinterrupt = vi.spyOn(provider, "reinterruptAbandonedChild")
388+
// A cancel racing the commit flips the abort flag while the
389+
// activation is in flight.
390+
resumeActivation.mockImplementation(async (taskId: string, parentTaskId?: string) => {
391+
await ClineProvider.prototype.resumeInterruptedTask.call(provider, taskId, parentTaskId)
392+
task.abort = true
393+
})
394+
395+
await task["resumeTaskFromHistory"]()
396+
397+
expect(reinterrupt).not.toHaveBeenCalled()
398+
expect(loop).not.toHaveBeenCalled()
399+
const persisted = await readPersistedApiHistory(storagePath, childId)
400+
expect(persisted).toEqual(preparedApiHistory)
401+
const persistedRecord = await readPersistedRecord(storagePath, childId)
402+
expect(persistedRecord.status).toBe("active")
403+
expect(provider.taskHistoryStore.get(childId)?.status).toBe("active")
404+
// The record is active, so no restore write ran. The recovery copy
405+
// stays on disk with the pre-resume history for recovery.
406+
const recoveryCopyName = await findRecoveryCopyName(taskDir)
407+
expect(recoveryCopyName).toBeDefined()
408+
const recoveryCopy = JSON.parse(await fs.readFile(path.join(taskDir, recoveryCopyName as string), "utf8"))
409+
expect(recoveryCopy).toEqual(authoritativeApiHistory)
410+
})
411+
377412
it("stops before activation and keeps the persisted api history when the preparation write fails", async () => {
378413
await provider.taskHistoryStore.upsert(makeChildRecord({ parentTaskId: undefined, rootTaskId: undefined }))
379414
const taskDir = await seedTaskFiles(storagePath, childId, authoritativeApiHistory)

0 commit comments

Comments
 (0)