From e74dd7f025e306f05a18f4be537e39a98dbc1b9b Mon Sep 17 00:00:00 2001 From: Doomguy <307901355+Doomguy-BFG9000@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:34:01 +0300 Subject: [PATCH] fix(task): honor retry state and synchronize assignees --- .../task-create-assignment-parity.test.ts | 63 ++++++++++++++++++- v3/@claude-flow/cli/src/commands/task.ts | 2 +- .../cli/src/mcp-tools/task-tools.ts | 12 +++- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/v3/@claude-flow/cli/__tests__/task-create-assignment-parity.test.ts b/v3/@claude-flow/cli/__tests__/task-create-assignment-parity.test.ts index e78e749d02..f952eae4a8 100644 --- a/v3/@claude-flow/cli/__tests__/task-create-assignment-parity.test.ts +++ b/v3/@claude-flow/cli/__tests__/task-create-assignment-parity.test.ts @@ -1,7 +1,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; +import { resolveTargetCommand } from '../src/command-resolution.js'; +import { CommandParser } from '../src/parser.js'; const state = vi.hoisted(() => ({ cwd: '' })); vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd })); vi.mock('../src/mcp-client.js', async () => { @@ -22,6 +24,65 @@ describe('task create CLI/MCP assignment parity', () => { }); import { taskTools } from '../src/mcp-tools/task-tools.js'; + +describe('task retry CLI/MCP state parity (#4723)', () => { + it.each([ + { label: 'explicit retain', options: ['--no-reset-state'], reset: false }, + { label: 'CLI default', options: [], reset: false }, + { label: 'explicit reset', options: ['--reset-state'], reset: true }, + { label: 'MCP default', options: null, reset: true }, + ])('persists the new task and synchronizes assignees with $label', async ({ options, reset }) => { + state.cwd = mkdtempSync(join(tmpdir(), 'ruflo-task-retry-')); + const flowDir = join(state.cwd, '.claude-flow'); + mkdirSync(join(flowDir, 'agents'), { recursive: true }); + const agentPaths = [join(flowDir, 'agents', 'store.json'), join(flowDir, 'agents.json')]; + for (const path of agentPaths) { + writeFileSync(path, JSON.stringify({ agents: { coder: { status: 'idle', currentTask: null } } })); + } + const readAgent = (path: string) => JSON.parse(readFileSync(path, 'utf8')).agents.coder; + const readTasks = () => JSON.parse(readFileSync(join(flowDir, 'tasks', 'store.json'), 'utf8')).tasks; + const call = async (name: string, input: Record) => { + const tool = taskTools.find(candidate => candidate.name === name)!; + return await tool.handler(input) as { taskId: string; newTaskId: string }; + }; + const { taskId } = await call('task_create', { + type: 'test', description: 'Persist retry state', assignTo: ['coder'], tags: ['fixture'], + }); + const savedResult = { partial: 'fixture result' }; + await call('task_update', { taskId, status: 'failed', progress: 42, result: savedResult }); + const original = readTasks()[taskId]; + for (const path of agentPaths) expect(readAgent(path)).toMatchObject({ status: 'idle', currentTask: null }); + + let newTaskId: string; + if (options === null) { + ({ newTaskId } = await call('task_retry', { taskId })); + } else { + const parser = new CommandParser(); + parser.registerCommand(taskCommand); + const { command, flags, positional } = parser.parse(['task', 'retry', taskId, ...options]); + const { target, args } = resolveTargetCommand(taskCommand, command, positional); + expect(parser.validateFlags(flags, target)).toEqual([]); + const result = await target.action!({ args, flags, cwd: state.cwd, interactive: false }); + expect(result).toMatchObject({ success: true }); + ({ newTaskId } = result.data as { newTaskId: string }); + } + + const tasks = readTasks(); + expect(newTaskId).not.toBe(taskId); + expect(tasks[taskId]).toEqual(original); + expect(tasks[newTaskId]).toMatchObject({ + status: 'pending', progress: reset ? 0 : 42, startedAt: null, completedAt: null, + assignedTo: ['coder'], tags: ['fixture', 'retry-of:' + taskId], + }); + expect(tasks[newTaskId].result).toEqual(reset ? undefined : savedResult); + for (const path of agentPaths) { + expect(readAgent(path)).toMatchObject({ status: 'busy', currentTask: newTaskId }); + } + await call('task_cancel', { taskId: newTaskId }); + for (const path of agentPaths) expect(readAgent(path)).toMatchObject({ status: 'idle', currentTask: null }); + }); +}); + it('lists all, running and agent-filtered tasks using the persisted handler contract', async () => { state.cwd = mkdtempSync(join(tmpdir(), 'ruflo-task-list-')); const call = (name: string, input: any): Promise => taskTools.find(t=>t.name===name)!.handler(input) as Promise; diff --git a/v3/@claude-flow/cli/src/commands/task.ts b/v3/@claude-flow/cli/src/commands/task.ts index 0a8857e2c5..0203c8515a 100644 --- a/v3/@claude-flow/cli/src/commands/task.ts +++ b/v3/@claude-flow/cli/src/commands/task.ts @@ -743,7 +743,7 @@ const retryCommand: Command = { ], action: async (ctx: CommandContext): Promise => { const taskId = ctx.args[0]; - const resetState = ctx.flags['reset-state'] as boolean; + const resetState = (ctx.flags.resetState ?? ctx.flags['reset-state']) as boolean | undefined; if (!taskId) { output.printError('Task ID is required'); 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 ab9fc0a354..53de700d08 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts @@ -623,22 +623,30 @@ export const taskTools: MCPTool[] = [ const original = store.tasks[taskId]; if (!original) return { success: false, taskId, error: 'Task not found' }; + const retainState = input.resetState === false; const newTaskId = `task-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; - store.tasks[newTaskId] = { + const retry: TaskRecord = { taskId: newTaskId, type: original.type, description: original.description, priority: original.priority, status: 'pending', - progress: 0, + progress: retainState ? original.progress ?? 0 : 0, assignedTo: [...original.assignedTo], tags: [...original.tags, 'retry-of:' + taskId], createdAt: new Date().toISOString(), startedAt: null, completedAt: null, }; + if (retainState && original.result !== undefined) retry.result = original.result; + store.tasks[newTaskId] = retry; saveTaskStore(store); + updateAgents(retry.assignedTo, (agent) => { + agent.status = 'busy'; + agent.currentTask = newTaskId; + }); + return { taskId, newTaskId,