fix(analyze-complexity): pass projectRoot through analyze-complexity flow

Modified analyze-task-complexity.js core function, direct function, and analyze.js tool to correctly pass projectRoot. Fixed import error in tools/index.js. Added debug logging to _resolveApiKey in ai-services-unified.js. This enables the .env API key fallback for analyze_project_complexity.
This commit is contained in:
Eyal Toledano
2025-05-01 14:18:44 -04:00
parent 8f49b0198a
commit b382ef2b8d
5 changed files with 401 additions and 344 deletions

View File

@@ -18,15 +18,17 @@ import { createLogWrapper } from '../../tools/utils.js'; // Import the new utili
* @param {string} args.outputPath - Explicit absolute path to save the report.
* @param {string|number} [args.threshold] - Minimum complexity score to recommend expansion (1-10)
* @param {boolean} [args.research] - Use Perplexity AI for research-backed complexity analysis
* @param {string} [args.projectRoot] - Project root path.
* @param {Object} log - Logger object
* @param {Object} [context={}] - Context object containing session data
* @param {Object} [context.session] - MCP session object
* @returns {Promise<{success: boolean, data?: Object, error?: {code: string, message: string}}>}
*/
export async function analyzeTaskComplexityDirect(args, log, context = {}) {
const { session } = context; // Extract session
// Destructure expected args
const { tasksJsonPath, outputPath, model, threshold, research } = args; // Model is ignored by core function now
const { session } = context;
const { tasksJsonPath, outputPath, threshold, research, projectRoot } = args;
const logWrapper = createLogWrapper(log);
// --- Initial Checks (remain the same) ---
try {
@@ -60,35 +62,34 @@ export async function analyzeTaskComplexityDirect(args, log, context = {}) {
log.info('Using research role for complexity analysis');
}
// Prepare options for the core function
const options = {
file: tasksPath,
output: resolvedOutputPath,
// model: model, // No longer needed
// Prepare options for the core function - REMOVED mcpLog and session here
const coreOptions = {
file: tasksJsonPath,
output: outputPath,
threshold: threshold,
research: research === true // Ensure boolean
research: research === true, // Ensure boolean
projectRoot: projectRoot // Pass projectRoot here
};
// --- End Initial Checks ---
// --- Silent Mode and Logger Wrapper (remain the same) ---
// --- Silent Mode and Logger Wrapper ---
const wasSilent = isSilentMode();
if (!wasSilent) {
enableSilentMode();
enableSilentMode(); // Still enable silent mode as a backup
}
// Create logger wrapper using the utility
const mcpLog = createLogWrapper(log);
let report; // To store the result from the core function
let report;
try {
// --- Call Core Function (Updated Context Passing) ---
// Call the core function, passing options and the context object { session, mcpLog }
report = await analyzeTaskComplexity(options, {
session, // Pass the session object
mcpLog // Pass the logger wrapper
});
// --- End Core Function Call ---
// --- Call Core Function (Pass context separately) ---
// Pass coreOptions as the first argument
// Pass context object { session, mcpLog } as the second argument
report = await analyzeTaskComplexity(
coreOptions, // Pass options object
{ session, mcpLog: logWrapper } // Pass context object
// Removed the explicit 'json' format argument, assuming context handling is sufficient
// If issues persist, we might need to add an explicit format param to analyzeTaskComplexity
);
} catch (error) {
log.error(
`Error in analyzeTaskComplexity core function: ${error.message}`
@@ -100,7 +101,7 @@ export async function analyzeTaskComplexityDirect(args, log, context = {}) {
return {
success: false,
error: {
code: 'ANALYZE_CORE_ERROR', // More specific error code
code: 'ANALYZE_CORE_ERROR',
message: `Error running core complexity analysis: ${error.message}`
}
};
@@ -124,10 +125,10 @@ export async function analyzeTaskComplexityDirect(args, log, context = {}) {
};
}
// The core function now returns the report object directly
if (!report || !report.complexityAnalysis) {
// Added a check to ensure report is defined before accessing its properties
if (!report || typeof report !== 'object') {
log.error(
'Core analyzeTaskComplexity function did not return a valid report object.'
'Core analysis function returned an invalid or undefined response.'
);
return {
success: false,
@@ -139,7 +140,10 @@ export async function analyzeTaskComplexityDirect(args, log, context = {}) {
}
try {
const analysisArray = report.complexityAnalysis; // Already an array
// Ensure complexityAnalysis exists and is an array
const analysisArray = Array.isArray(report.complexityAnalysis)
? report.complexityAnalysis
: [];
// Count tasks by complexity (remains the same)
const highComplexityTasks = analysisArray.filter(
@@ -155,16 +159,15 @@ export async function analyzeTaskComplexityDirect(args, log, context = {}) {
return {
success: true,
data: {
message: `Task complexity analysis complete. Report saved to ${resolvedOutputPath}`,
reportPath: resolvedOutputPath,
message: `Task complexity analysis complete. Report saved to ${outputPath}`, // Use outputPath from args
reportPath: outputPath, // Use outputPath from args
reportSummary: {
taskCount: analysisArray.length,
highComplexityTasks,
mediumComplexityTasks,
lowComplexityTasks
}
// Include the full report data if needed by the client
// fullReport: report
},
fullReport: report // Now includes the full report
}
};
} catch (parseError) {

View File

@@ -4,120 +4,142 @@
*/
import { z } from 'zod';
import { handleApiResult, createErrorResponse } from './utils.js';
import { analyzeTaskComplexityDirect } from '../core/direct-functions/analyze-task-complexity.js';
import { findTasksJsonPath } from '../core/utils/path-utils.js';
import path from 'path';
import fs from 'fs';
import fs from 'fs'; // Import fs for directory check/creation
import {
handleApiResult,
createErrorResponse,
getProjectRootFromSession // Assuming this is in './utils.js' relative to this file
} from './utils.js';
import { analyzeTaskComplexityDirect } from '../core/task-master-core.js'; // Assuming core functions are exported via task-master-core.js
import { findTasksJsonPath } from '../core/utils/path-utils.js';
/**
* Register the analyze tool with the MCP server
* Register the analyze_project_complexity tool
* @param {Object} server - FastMCP server instance
*/
export function registerAnalyzeTool(server) {
export function registerAnalyzeProjectComplexityTool(server) {
server.addTool({
name: 'analyze_project_complexity',
description:
'Analyze task complexity and generate expansion recommendations',
'Analyze task complexity and generate expansion recommendations.',
parameters: z.object({
threshold: z.coerce // Use coerce for number conversion from string if needed
.number()
.int()
.min(1)
.max(10)
.optional()
.default(5) // Default threshold
.describe('Complexity score threshold (1-10) to recommend expansion.'),
research: z
.boolean()
.optional()
.default(false)
.describe('Use Perplexity AI for research-backed analysis.'),
output: z
.string()
.optional()
.describe(
'Output file path relative to project root (default: scripts/task-complexity-report.json)'
),
threshold: z.coerce
.number()
.min(1)
.max(10)
.optional()
.describe(
'Minimum complexity score to recommend expansion (1-10) (default: 5)'
'Output file path relative to project root (default: scripts/task-complexity-report.json).'
),
file: z
.string()
.optional()
.describe(
'Absolute path to the tasks file in the /tasks folder inside the project root (default: tasks/tasks.json)'
'Path to the tasks file relative to project root (default: tasks/tasks.json).'
),
research: z
.boolean()
.optional()
.default(false)
.describe('Use research role for complexity analysis'),
projectRoot: z
.string()
.describe('The directory of the project. Must be an absolute path.')
}),
execute: async (args, { log, session }) => {
const toolName = 'analyze_project_complexity'; // Define tool name for logging
try {
log.info(
`Executing analyze_project_complexity tool with args: ${JSON.stringify(args)}`
`Executing ${toolName} tool with args: ${JSON.stringify(args)}`
);
// 1. Get Project Root (Mandatory for this tool)
const rootFolder = args.projectRoot;
if (!rootFolder) {
return createErrorResponse('projectRoot is required.');
}
if (!path.isAbsolute(rootFolder)) {
return createErrorResponse('projectRoot must be an absolute path.');
if (!rootFolder || !path.isAbsolute(rootFolder)) {
log.error(
`${toolName}: projectRoot is required and must be absolute.`
);
return createErrorResponse(
'projectRoot is required and must be absolute.'
);
}
log.info(`${toolName}: Project root: ${rootFolder}`);
// 2. Resolve Paths relative to projectRoot
let tasksJsonPath;
try {
// Note: findTasksJsonPath expects 'file' relative to root, or absolute
tasksJsonPath = findTasksJsonPath(
{ projectRoot: rootFolder, file: args.file },
{ projectRoot: rootFolder, file: args.file }, // Pass root and optional relative file path
log
);
log.info(`${toolName}: Resolved tasks path: ${tasksJsonPath}`);
} catch (error) {
log.error(`Error finding tasks.json: ${error.message}`);
log.error(`${toolName}: Error finding tasks.json: ${error.message}`);
return createErrorResponse(
`Failed to find tasks.json within project root '${rootFolder}': ${error.message}`
);
}
const outputPath = args.output
? path.resolve(rootFolder, args.output)
: path.resolve(rootFolder, 'scripts', 'task-complexity-report.json');
? path.resolve(rootFolder, args.output) // Resolve relative output path
: path.resolve(rootFolder, 'scripts', 'task-complexity-report.json'); // Default location resolved relative to root
log.info(`${toolName}: Report output path: ${outputPath}`);
// Ensure output directory exists
const outputDir = path.dirname(outputPath);
try {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
log.info(`Created output directory: ${outputDir}`);
log.info(`${toolName}: Created output directory: ${outputDir}`);
}
} catch (dirError) {
log.error(
`Failed to create output directory ${outputDir}: ${dirError.message}`
`${toolName}: Failed to create output directory ${outputDir}: ${dirError.message}`
);
return createErrorResponse(
`Failed to create output directory: ${dirError.message}`
);
}
// 3. Call Direct Function - Pass projectRoot in first arg object
const result = await analyzeTaskComplexityDirect(
{
// Pass resolved absolute paths and other args
tasksJsonPath: tasksJsonPath,
outputPath: outputPath,
outputPath: outputPath, // Pass resolved absolute path
threshold: args.threshold,
research: args.research
research: args.research,
projectRoot: rootFolder // <<< Pass projectRoot HERE
},
log,
{ session }
{ session } // Pass context object with session
);
if (result.success) {
log.info(`Tool analyze_project_complexity finished successfully.`);
} else {
log.error(
`Tool analyze_project_complexity failed: ${result.error?.message || 'Unknown error'}`
);
}
return handleApiResult(result, log, 'Error analyzing task complexity');
// 4. Handle Result
log.info(
`${toolName}: Direct function result: success=${result.success}`
);
return handleApiResult(
result,
log,
'Error analyzing task complexity' // Consistent error prefix
);
} catch (error) {
log.error(`Critical error in analyze tool execute: ${error.message}`);
return createErrorResponse(`Internal tool error: ${error.message}`);
log.error(
`Critical error in ${toolName} tool execute: ${error.message}`
);
return createErrorResponse(
`Internal tool error (${toolName}): ${error.message}`
);
}
}
});

View File

@@ -17,7 +17,7 @@ import { registerExpandTaskTool } from './expand-task.js';
import { registerAddTaskTool } from './add-task.js';
import { registerAddSubtaskTool } from './add-subtask.js';
import { registerRemoveSubtaskTool } from './remove-subtask.js';
import { registerAnalyzeTool } from './analyze.js';
import { registerAnalyzeProjectComplexityTool } from './analyze.js';
import { registerClearSubtasksTool } from './clear-subtasks.js';
import { registerExpandAllTool } from './expand-all.js';
import { registerRemoveDependencyTool } from './remove-dependency.js';
@@ -63,7 +63,7 @@ export function registerTaskMasterTools(server) {
registerClearSubtasksTool(server);
// Group 5: Task Analysis & Expansion
registerAnalyzeTool(server);
registerAnalyzeProjectComplexityTool(server);
registerExpandTaskTool(server);
registerExpandAllTool(server);