Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([cos-task-id-uniqueness-validation]): dedupe duplicate task ids at parse so reorderTasks can't drop tasks

[._.]/ Adam Eivye393de9a2c0c
diff --git a/server/lib/taskParser.js b/server/lib/taskParser.js
index f6e3bc33d8..8b82536129 100644
--- a/server/lib/taskParser.js
+++ b/server/lib/taskParser.js
@@ -169,9 +169,31 @@ function parseMetadataLine(line) {
export function parseTasksMarkdown(content) {
const lines = content.split('\n');
const tasks = [];
+ const seenIds = new Set();
let currentTask = null;
let currentSection = null;
+ // Push a fully-parsed task, guaranteeing its id is unique across the file.
+ // Duplicate ids corrupt downstream consumers that key on id — most notably
+ // reorderTasks' `new Map(tasks.map(t => [t.id, t]))`, which silently collapses
+ // collisions so only the last duplicate survives the reorder write-back. We
+ // warn and suffix the colliding id (`-dup2`, `-dup3`, …) rather than throw:
+ // throwing would make a single hand-edited or corrupted TASKS.md crash every
+ // read of the CoS task system, whereas suffixing keeps every task alive with a
+ // distinct id. Called once per task, after its metadata lines are attached.
+ const pushTask = (task) => {
+ if (!task) return;
+ if (seenIds.has(task.id)) {
+ const originalId = task.id;
+ let suffix = 2;
+ while (seenIds.has(`${originalId}-dup${suffix}`)) suffix++;
+ task.id = `${originalId}-dup${suffix}`;
+ console.warn(`⚠️ Duplicate task id "${originalId}" in tasks markdown — renamed to "${task.id}"`);
+ }
+ seenIds.add(task.id);
+ tasks.push(task);
+ };
+
for (const line of lines) {
// Section headers
if (line.startsWith('## ')) {
@@ -186,9 +208,7 @@ export function parseTasksMarkdown(content) {
// Task line
if (line.startsWith('- [')) {
- if (currentTask) {
- tasks.push(currentTask);
- }
+ pushTask(currentTask);
currentTask = parseTaskLine(line);
if (currentTask) {
currentTask.section = currentSection;
@@ -206,9 +226,7 @@ export function parseTasksMarkdown(content) {
}
// Don't forget last task
- if (currentTask) {
- tasks.push(currentTask);
- }
+ pushTask(currentTask);
return tasks;
}
diff --git a/server/lib/taskParser.test.js b/server/lib/taskParser.test.js
index 8995e660e1..b2006907c7 100644
--- a/server/lib/taskParser.test.js
+++ b/server/lib/taskParser.test.js
@@ -178,6 +178,58 @@ describe('Task Parser', () => {
const tasks = parseTasksMarkdown(markdown);
expect(tasks[0].id).toBe('sys-001');
});
+
+ it('should preserve every task when ids collide, suffixing duplicates', () => {
+ const markdown = `# Tasks
+
+## Pending
+- [ ] #task-001 | HIGH | First task
+- [ ] #task-001 | LOW | Second task with same id
+- [ ] #task-001 | MEDIUM | Third task with same id`;
+
+ const tasks = parseTasksMarkdown(markdown);
+
+ // All three survive (the bug being fixed silently lost 2 of 3 on reorder)
+ expect(tasks).toHaveLength(3);
+ expect(tasks.map(t => t.id)).toEqual(['task-001', 'task-001-dup2', 'task-001-dup3']);
+ // Descriptions stay paired with their suffixed ids — no data loss
+ expect(tasks[1].description).toBe('Second task with same id');
+ expect(tasks[2].description).toBe('Third task with same id');
+ });
+
+ it('should produce ids unique enough for a keyed Map (the reorderTasks contract)', () => {
+ const markdown = `# Tasks
+
+## Pending
+- [ ] #task-001 | HIGH | A
+- [ ] #task-001 | LOW | B
+- [ ] #task-002 | MEDIUM | C`;
+
+ const tasks = parseTasksMarkdown(markdown);
+ const byId = new Map(tasks.map(t => [t.id, t]));
+
+ // Map size must equal task count — no silent collapse
+ expect(byId.size).toBe(tasks.length);
+ expect(byId.size).toBe(3);
+ });
+
+ it('should not collide a suffixed id with a real later task carrying that suffix', () => {
+ const markdown = `# Tasks
+
+## Pending
+- [ ] #task-001 | HIGH | A
+- [ ] #task-001 | LOW | B
+- [ ] #task-001-dup2 | MEDIUM | C already named like a suffix`;
+
+ const tasks = parseTasksMarkdown(markdown);
+ const ids = tasks.map(t => t.id);
+
+ // The renamed first duplicate takes `task-001-dup2`; the real `task-001-dup2`
+ // then collides and suffixes off its own id (`task-001-dup2-dup2`) — the key
+ // point is every id stays distinct, never a re-collision.
+ expect(new Set(ids).size).toBe(ids.length);
+ expect(ids).toEqual(['task-001', 'task-001-dup2', 'task-001-dup2-dup2']);
+ });
});
describe('groupTasksByStatus', () => {
diff --git a/server/services/cos.js b/server/services/cos.js
index 9049169ad6..7d4b6a1535 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -2813,7 +2813,9 @@ export async function reorderTasks(taskIds) {
const content = await readFile(filePath, 'utf-8');
const tasks = parseTasksMarkdown(content);
- // Create a map of tasks by ID for quick lookup
+ // Create a map of tasks by ID for quick lookup. parseTasksMarkdown guarantees
+ // unique ids (it suffixes any duplicate it encounters), so this Map can't
+ // silently collapse colliding tasks and drop them on write-back.
const taskMap = new Map(tasks.map(t => [t.id, t]));
// Reorder based on the provided order