refactor(expand/all): Implement additive expansion and complexity report integration

Refactors the `expandTask` and `expandAllTasks` features to complete subtask 61.38 and enhance functionality based on subtask 61.37's refactor.

Key Changes:

- **Additive Expansion (`expandTask`, `expandAllTasks`):**

    - Modified `expandTask` default behavior to append newly generated subtasks to any existing ones.

    - Added a `force` flag (passed down from CLI/MCP via `--force` option/parameter) to `expandTask` and `expandAllTasks`. When `force` is true, existing subtasks are cleared before generating new ones.

    - Updated relevant CLI command (`expand`), MCP tool (`expand_task`, `expand_all`), and direct function wrappers (`expandTaskDirect`, `expandAllTasksDirect`) to handle and pass the `force` flag.

- **Complexity Report Integration (`expandTask`):**

    - `expandTask` now reads `scripts/task-complexity-report.json`.

    - If an analysis entry exists for the target task:

        - `recommendedSubtasks` is used to determine the number of subtasks to generate (unless `--num` is explicitly provided).

        - `expansionPrompt` is used as the primary prompt content for the AI.

        - `reasoning` is appended to any additional context provided.

    - If no report entry exists or the report is missing, it falls back to default subtask count (from config) and standard prompt generation.

- **`expandAllTasks` Orchestration:**

    - Refactored `expandAllTasks` to primarily iterate through eligible tasks (pending/in-progress, considering `force` flag and existing subtasks) and call the updated `expandTask` function for each.

    - Removed redundant logic (like complexity reading or explicit subtask clearing) now handled within `expandTask`.

    - Ensures correct context (`session`, `mcpLog`) and flags (`useResearch`, `force`) are passed down.

- **Configuration & Cleanup:**

    - Updated `.cursor/mcp.json` with new Perplexity/Anthropic API keys (old ones invalidated).

    - Completed refactoring of `expandTask` started in 61.37, confirming usage of `generateTextService` and appropriate prompts.

- **Task Management:**

    - Marked subtask 61.37 as complete.

    - Updated `.changeset/cuddly-zebras-matter.md` to reflect user-facing changes.

These changes finalize the refactoring of the task expansion features, making them more robust, configurable via complexity analysis, and aligned with the unified AI service architecture.
This commit is contained in:
Eyal Toledano
2025-04-25 02:57:08 -04:00
parent f6c5a3b23b
commit 443824a35e
12 changed files with 1068 additions and 542 deletions

View File

@@ -676,18 +676,32 @@ function registerCommands(programInstance) {
if (options.all) {
// --- Handle expand --all ---
// This currently calls expandAllTasks. If expandAllTasks internally calls
// the refactored expandTask, it needs to be updated to pass the empty context {}.
// For now, we assume expandAllTasks needs its own refactor (Subtask 61.38).
// We'll add a placeholder log here.
console.log(
chalk.blue(
'Expanding all pending tasks... (Requires expand-all-tasks.js refactor)'
)
);
// Placeholder: await expandAllTasks(tasksPath, options.num, options.research, options.prompt, options.force, {});
console.log(chalk.blue('Expanding all pending tasks...'));
// Updated call to the refactored expandAllTasks
try {
const result = await expandAllTasks(
tasksPath,
options.num, // Pass num
options.research, // Pass research flag
options.prompt, // Pass additional context
options.force, // Pass force flag
{} // Pass empty context for CLI calls
// outputFormat defaults to 'text' in expandAllTasks for CLI
);
// Optional: Display summary from result
console.log(chalk.green(`Expansion Summary:`));
console.log(chalk.green(` - Attempted: ${result.tasksToExpand}`));
console.log(chalk.green(` - Expanded: ${result.expandedCount}`));
console.log(chalk.yellow(` - Skipped: ${result.skippedCount}`));
console.log(chalk.red(` - Failed: ${result.failedCount}`));
} catch (error) {
console.error(
chalk.red(`Error expanding all tasks: ${error.message}`)
);
process.exit(1);
}
} else if (options.id) {
// --- Handle expand --id <id> ---
// --- Handle expand --id <id> (Should be correct from previous refactor) ---
if (!options.id) {
console.error(
chalk.red('Error: Task ID is required unless using --all.')
@@ -696,19 +710,24 @@ function registerCommands(programInstance) {
}
console.log(chalk.blue(`Expanding task ${options.id}...`));
// Call the refactored expandTask function
await expandTask(
tasksPath,
options.id,
options.num, // Pass num (core function handles default)
options.research,
options.prompt,
// Pass empty context for CLI calls
{}
// Note: The 'force' flag is now primarily handled by the Direct Function Wrapper
// based on pre-checks, but the core function no longer explicitly needs it.
);
try {
// Call the refactored expandTask function
await expandTask(
tasksPath,
options.id,
options.num,
options.research,
options.prompt,
{}, // Pass empty context for CLI calls
options.force // Pass the force flag down
);
// expandTask logs its own success/failure for single task
} catch (error) {
console.error(
chalk.red(`Error expanding task ${options.id}: ${error.message}`)
);
process.exit(1);
}
} else {
console.error(
chalk.red('Error: You must specify either a task ID (--id) or --all.')