fix(move-task): Fix critical bugs in task move functionality

- Fixed parent-to-parent task moves where original task would remain as duplicate
- Fixed moving tasks to become subtasks of empty parents (validation errors)
- Fixed moving subtasks between different parent tasks
- Improved comma-separated batch moves with proper error handling
- Updated MCP tool to use core logic instead of custom implementation
- Resolves task duplication issues and enables proper task hierarchy reorganization
This commit is contained in:
Eyal Toledano
2025-05-25 18:03:43 -04:00
parent f2c5911e58
commit d391f3b5b3
2 changed files with 458 additions and 1374 deletions

File diff suppressed because one or more lines are too long

View File

@@ -3,129 +3,129 @@
* Tool for moving tasks or subtasks to a new position * Tool for moving tasks or subtasks to a new position
*/ */
import { z } from 'zod'; import { z } from "zod";
import { import {
handleApiResult, handleApiResult,
createErrorResponse, createErrorResponse,
withNormalizedProjectRoot withNormalizedProjectRoot,
} from './utils.js'; } from "./utils.js";
import { moveTaskDirect } from '../core/task-master-core.js'; import { moveTaskDirect } from "../core/task-master-core.js";
import { findTasksPath } from '../core/utils/path-utils.js'; import { findTasksPath } from "../core/utils/path-utils.js";
/** /**
* Register the moveTask tool with the MCP server * Register the moveTask tool with the MCP server
* @param {Object} server - FastMCP server instance * @param {Object} server - FastMCP server instance
*/ */
export function registerMoveTaskTool(server) { export function registerMoveTaskTool(server) {
server.addTool({ server.addTool({
name: 'move_task', name: "move_task",
description: 'Move a task or subtask to a new position', description: "Move a task or subtask to a new position",
parameters: z.object({ parameters: z.object({
from: z from: z
.string() .string()
.describe( .describe(
'ID of the task/subtask to move (e.g., "5" or "5.2"). Can be comma-separated to move multiple tasks (e.g., "5,6,7")' 'ID of the task/subtask to move (e.g., "5" or "5.2"). Can be comma-separated to move multiple tasks (e.g., "5,6,7")'
), ),
to: z to: z
.string() .string()
.describe( .describe(
'ID of the destination (e.g., "7" or "7.3"). Must match the number of source IDs if comma-separated' 'ID of the destination (e.g., "7" or "7.3"). Must match the number of source IDs if comma-separated'
), ),
file: z.string().optional().describe('Custom path to tasks.json file'), file: z.string().optional().describe("Custom path to tasks.json file"),
projectRoot: z projectRoot: z
.string() .string()
.describe( .describe(
'Root directory of the project (typically derived from session)' "Root directory of the project (typically derived from session)"
) ),
}), }),
execute: withNormalizedProjectRoot(async (args, { log, session }) => { execute: withNormalizedProjectRoot(async (args, { log, session }) => {
try { try {
// Find tasks.json path if not provided // Find tasks.json path if not provided
let tasksJsonPath = args.file; let tasksJsonPath = args.file;
if (!tasksJsonPath) { if (!tasksJsonPath) {
tasksJsonPath = findTasksPath(args, log); tasksJsonPath = findTasksPath(args, log);
} }
// Parse comma-separated IDs // Parse comma-separated IDs
const fromIds = args.from.split(',').map((id) => id.trim()); const fromIds = args.from.split(",").map((id) => id.trim());
const toIds = args.to.split(',').map((id) => id.trim()); const toIds = args.to.split(",").map((id) => id.trim());
// Validate matching IDs count // Validate matching IDs count
if (fromIds.length !== toIds.length) { if (fromIds.length !== toIds.length) {
return createErrorResponse( return createErrorResponse(
'The number of source and destination IDs must match', "The number of source and destination IDs must match",
'MISMATCHED_ID_COUNT' "MISMATCHED_ID_COUNT"
); );
} }
// If moving multiple tasks // If moving multiple tasks
if (fromIds.length > 1) { if (fromIds.length > 1) {
const results = []; const results = [];
// Move tasks one by one, only generate files on the last move // Move tasks one by one, only generate files on the last move
for (let i = 0; i < fromIds.length; i++) { for (let i = 0; i < fromIds.length; i++) {
const fromId = fromIds[i]; const fromId = fromIds[i];
const toId = toIds[i]; const toId = toIds[i];
// Skip if source and destination are the same // Skip if source and destination are the same
if (fromId === toId) { if (fromId === toId) {
log.info(`Skipping ${fromId} -> ${toId} (same ID)`); log.info(`Skipping ${fromId} -> ${toId} (same ID)`);
continue; continue;
} }
const shouldGenerateFiles = i === fromIds.length - 1; const shouldGenerateFiles = i === fromIds.length - 1;
const result = await moveTaskDirect( const result = await moveTaskDirect(
{ {
sourceId: fromId, sourceId: fromId,
destinationId: toId, destinationId: toId,
tasksJsonPath, tasksJsonPath,
projectRoot: args.projectRoot projectRoot: args.projectRoot,
}, },
log, log,
{ session } { session }
); );
if (!result.success) { if (!result.success) {
log.error( log.error(
`Failed to move ${fromId} to ${toId}: ${result.error.message}` `Failed to move ${fromId} to ${toId}: ${result.error.message}`
); );
} else { } else {
results.push(result.data); results.push(result.data);
} }
} }
return handleApiResult( return handleApiResult(
{ {
success: true, success: true,
data: { data: {
moves: results, moves: results,
message: `Successfully moved ${results.length} tasks` message: `Successfully moved ${results.length} tasks`,
} },
}, },
log log
); );
} else { } else {
// Moving a single task // Moving a single task
return handleApiResult( return handleApiResult(
await moveTaskDirect( await moveTaskDirect(
{ {
sourceId: args.from, sourceId: args.from,
destinationId: args.to, destinationId: args.to,
tasksJsonPath, tasksJsonPath,
projectRoot: args.projectRoot projectRoot: args.projectRoot,
}, },
log, log,
{ session } { session }
), ),
log log
); );
} }
} catch (error) { } catch (error) {
return createErrorResponse( return createErrorResponse(
`Failed to move task: ${error.message}`, `Failed to move task: ${error.message}`,
'MOVE_TASK_ERROR' "MOVE_TASK_ERROR"
); );
} }
}) }),
}); });
} }