From 13862a61d512e68b48746f382191996052cc3eed Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:03:21 +0900 Subject: [PATCH] fix(tasks): clear completedAt when task_update reopens a finished task Moving a completed, failed or cancelled task back to pending or in_progress kept the old completedAt, so task_status and the store reported a finish time for a task that is unfinished again. --- ...-update-reopen-clears-completed-at.test.ts | 43 +++++++++++++++++++ .../cli/src/mcp-tools/task-tools.ts | 5 +++ 2 files changed, 48 insertions(+) create mode 100644 v3/@claude-flow/cli/__tests__/task-update-reopen-clears-completed-at.test.ts diff --git a/v3/@claude-flow/cli/__tests__/task-update-reopen-clears-completed-at.test.ts b/v3/@claude-flow/cli/__tests__/task-update-reopen-clears-completed-at.test.ts new file mode 100644 index 0000000000..c34da96eee --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/task-update-reopen-clears-completed-at.test.ts @@ -0,0 +1,43 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest'; +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +const state = vi.hoisted(() => ({ cwd: '' })); +vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd })); +import { taskTools } from '../src/mcp-tools/task-tools.js'; +const call = (name: string, input: any): Promise => taskTools.find(t => t.name === name)!.handler(input) as Promise; +const stored = (id: string) => JSON.parse(readFileSync(join(state.cwd, '.claude-flow/tasks/store.json'), 'utf8')).tasks[id]; +beforeEach(() => { state.cwd = mkdtempSync(join(tmpdir(), 'ruflo-task-reopen-')); }); +afterEach(() => rmSync(state.cwd, { recursive: true, force: true })); + +it.each([ + ['completed', 'pending'], + ['completed', 'in_progress'], + ['failed', 'in_progress'], + ['cancelled', 'pending'], +])('reopening a %s task as %s clears completedAt', async (finished, reopened) => { + const { taskId } = await call('task_create', { type: 'test', description: 'task' }); + await call('task_update', { taskId, status: finished }); + expect(stored(taskId).completedAt).toEqual(expect.any(String)); + + await call('task_update', { taskId, status: reopened }); + expect(stored(taskId)).toMatchObject({ status: reopened, completedAt: null }); + expect((await call('task_status', { taskId })).completedAt).toBeNull(); +}); + +it('finishing the reopened task records a new completedAt', async () => { + const { taskId } = await call('task_create', { type: 'test', description: 'task' }); + await call('task_update', { taskId, status: 'completed' }); + await call('task_update', { taskId, status: 'pending' }); + await call('task_update', { taskId, status: 'completed' }); + expect(stored(taskId)).toMatchObject({ status: 'completed', progress: 100 }); + expect(stored(taskId).completedAt).toEqual(expect.any(String)); +}); + +it('a progress-only update leaves a finished task completedAt alone', async () => { + const { taskId } = await call('task_create', { type: 'test', description: 'task' }); + await call('task_update', { taskId, status: 'failed' }); + const { completedAt } = stored(taskId); + await call('task_update', { taskId, progress: 30 }); + expect(stored(taskId).completedAt).toBe(completedAt); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts index 50cdb35192..de47488744 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts @@ -443,6 +443,11 @@ export const taskTools: MCPTool[] = [ if (newStatus === 'in_progress' && !task.startedAt) { task.startedAt = new Date().toISOString(); } + // Reopening a finished task (completed/failed/cancelled -> pending/in_progress) makes it unfinished + // again, so it must not keep reporting the completedAt of the run that was undone. + if (newStatus === 'pending' || newStatus === 'in_progress') { + task.completedAt = null; + } // A finished task frees its workers, as task_complete and task_cancel do; otherwise a worker // whose task was finished through task_update stays `busy` forever, and a later task_complete // returns early because the status already reads completed.