fix(tasks): Ensure new task IDs are sequential within the target tag

Modified the ID generation logic in 'add-task.js' to calculate the next task ID based on the highest ID within the specified tag, rather than globally across all tags.

This fixes a critical bug where creating a task in a new tag would result in a high, non-sequential ID, such as ID 105 for the first task in a tag.
This commit is contained in:
Eyal Toledano
2025-06-13 00:31:08 -04:00
parent b205e52d08
commit cac8c234d6
4 changed files with 45 additions and 18 deletions

View File

@@ -265,20 +265,27 @@ async function addTask(
throw new Error(`Tag "${targetTag}" not found.`);
}
// Ensure the target tag has a metadata object
// Ensure the target tag has a tasks array and metadata object
if (!rawData[targetTag].tasks) {
rawData[targetTag].tasks = [];
}
if (!rawData[targetTag].metadata) {
rawData[targetTag].metadata = {
created: new Date().toISOString(),
updated: new Date().toISOString(),
description: ``
};
}
// Get a flat list of ALL tasks across ALL tags to calculate new ID and validate dependencies
// Get a flat list of ALL tasks across ALL tags to validate dependencies
const allTasks = getAllTasks(rawData);
// Find the highest task ID across all tags to determine the next ID
// Find the highest task ID *within the target tag* to determine the next ID
const tasksInTargetTag = rawData[targetTag].tasks;
const highestId =
allTasks.length > 0 ? Math.max(...allTasks.map((t) => t.id)) : 0;
tasksInTargetTag.length > 0
? Math.max(...tasksInTargetTag.map((t) => t.id))
: 0;
const newTaskId = highestId + 1;
// Only show UI box for CLI mode