|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +from collections import defaultdict, deque |
| 7 | + |
| 8 | +class Task: |
| 9 | + def __init__(self, id=None, label=None, description=None, category=None, complexity=0, dependencies=None, status="todo", plan_path=None): |
| 10 | + self.id = id |
| 11 | + self.label = label |
| 12 | + self.description = description |
| 13 | + self.category = category |
| 14 | + self.complexity = complexity |
| 15 | + self.dependencies = dependencies if dependencies is not None else [] |
| 16 | + self.status = status # e.g., "todo", "in_progress", "done" |
| 17 | + self.plan_path = plan_path |
| 18 | + |
| 19 | + def __repr__(self): |
| 20 | + return (f"Task(id={self.id!r}, label={self.label!r}, description={self.description!r}, category={self.category!r}, " |
| 21 | + f"complexity={self.complexity!r}, dependencies={self.dependencies!r}, status={self.status!r}, plan_path={self.plan_path!r})") |
| 22 | + |
| 23 | + def __eq__(self, other): |
| 24 | + if not isinstance(other, Task): |
| 25 | + return NotImplemented |
| 26 | + return (self.id == other.id and |
| 27 | + self.label == other.label and |
| 28 | + self.description == other.description and |
| 29 | + self.category == other.category and |
| 30 | + self.complexity == other.complexity and |
| 31 | + self.dependencies == other.dependencies and |
| 32 | + self.status == other.status and |
| 33 | + self.plan_path == other.plan_path) |
| 34 | + |
| 35 | +# --- Regex Patterns --- |
| 36 | +# New format: - [Status] **[ID]** Label: Description (Complexity: X) [Deps: Y] (See plan: Z) |
| 37 | +NEW_TASK_REGEX = re.compile( |
| 38 | + r'^- \[(?P<status>[^\]]+)\] \*\*(?P<id>[^ ]+)\*\* (?P<label>[^:]+): (?P<description>.*?)(?: \(Complexity: (?P<complexity>\d+)\))?(?: \[Deps: (?P<dependencies>.*?)\])?(?: \(See plan: (?P<plan_path>.*?)\))?$' |
| 39 | +) |
| 40 | +# Old format: - [ ] Description (See plan: ...) |
| 41 | +OLD_TASK_REGEX = re.compile( |
| 42 | + r'^- \[(?P<status>[^\]]*)\] (?P<description>.*?)(?: \(See plan: (?P<plan_path>.*?)\))?$' |
| 43 | +) |
| 44 | +HEADER_WARNING = """# Tasks |
| 45 | +
|
| 46 | +> **WARNING: NEVER MODIFY THIS FILE BY HAND. USE THE SCRIPT INSTEAD.** |
| 47 | +> Run `python .gemini/scripts/task.py --help` for usage. |
| 48 | +""" |
| 49 | + |
| 50 | +# --- Parser Functions --- |
| 51 | + |
| 52 | +def parse_task_line(line): |
| 53 | + line = line.strip() |
| 54 | + if not line or line.startswith('#') or line.startswith('>'): |
| 55 | + return None |
| 56 | + |
| 57 | + new_match = NEW_TASK_REGEX.match(line) |
| 58 | + if new_match: |
| 59 | + data = new_match.groupdict() |
| 60 | + deps_str = data.get('dependencies') |
| 61 | + dependencies = [] |
| 62 | + if deps_str: |
| 63 | + dependencies = [dep.strip() for dep in deps_str.split(',') if dep.strip()] |
| 64 | + |
| 65 | + complexity = int(data.get('complexity')) if data.get('complexity') else 0 |
| 66 | + return Task( |
| 67 | + id=data.get('id'), |
| 68 | + label=data.get('label'), |
| 69 | + description=data.get('description'), |
| 70 | + category=None, |
| 71 | + complexity=complexity, |
| 72 | + dependencies=dependencies, |
| 73 | + status=data.get('status'), |
| 74 | + plan_path=data.get('plan_path') |
| 75 | + ) |
| 76 | + |
| 77 | + old_match = OLD_TASK_REGEX.match(line) |
| 78 | + if old_match: |
| 79 | + data = old_match.groupdict() |
| 80 | + status = data.get('status').strip() |
| 81 | + if not status: status = "todo" |
| 82 | + return Task( |
| 83 | + id=None, |
| 84 | + label=None, |
| 85 | + description=data.get('description'), |
| 86 | + category=None, |
| 87 | + complexity=0, |
| 88 | + dependencies=[], |
| 89 | + status=status, |
| 90 | + plan_path=data.get('plan_path') |
| 91 | + ) |
| 92 | + return None |
| 93 | + |
| 94 | +def parse_tasks_file(file_content): |
| 95 | + tasks = [] |
| 96 | + current_category = None |
| 97 | + lines = file_content.splitlines() |
| 98 | + |
| 99 | + for line in lines: |
| 100 | + stripped_line = line.strip() |
| 101 | + if stripped_line.startswith("## Active Tasks"): |
| 102 | + current_category = None |
| 103 | + continue |
| 104 | + elif stripped_line.startswith("## Archive"): |
| 105 | + current_category = None |
| 106 | + continue |
| 107 | + elif stripped_line.startswith("### "): |
| 108 | + current_category = stripped_line[4:].strip() |
| 109 | + continue |
| 110 | + |
| 111 | + task = parse_task_line(line) |
| 112 | + if task: |
| 113 | + task.category = current_category |
| 114 | + tasks.append(task) |
| 115 | + return tasks |
| 116 | + |
| 117 | +# --- Formatter Functions --- |
| 118 | + |
| 119 | +def format_task_to_line(task): |
| 120 | + if task.id is not None: |
| 121 | + complexity_str = f" (Complexity: {task.complexity})" if task.complexity != 0 else "" |
| 122 | + deps_str = f" [Deps: {', '.join(task.dependencies)}]" |
| 123 | + plan_path_str = f" (See plan: {task.plan_path})" if task.plan_path else "" |
| 124 | + return f"- [{task.status}] **{task.id}** {task.label}: {task.description}{complexity_str}{deps_str}{plan_path_str}" |
| 125 | + else: |
| 126 | + plan_path_str = f" (See plan: {task.plan_path})" if task.plan_path else "" |
| 127 | + return f"- [{task.status}] {task.description}{plan_path_str}" |
| 128 | + |
| 129 | +def topological_sort(tasks): |
| 130 | + adj = defaultdict(list) |
| 131 | + in_degree = defaultdict(int) |
| 132 | + task_map = {task.id: task for task in tasks if task.id} |
| 133 | + |
| 134 | + for tid in task_map: |
| 135 | + in_degree[tid] = 0 |
| 136 | + |
| 137 | + for task in tasks: |
| 138 | + if task.id: |
| 139 | + for dep_id in task.dependencies: |
| 140 | + if dep_id in task_map: |
| 141 | + adj[dep_id].append(task.id) |
| 142 | + in_degree[task.id] += 1 |
| 143 | + |
| 144 | + queue = deque(sorted([tid for tid in in_degree if in_degree[tid] == 0])) |
| 145 | + sorted_tasks_ids = [] |
| 146 | + |
| 147 | + while queue: |
| 148 | + u = queue.popleft() |
| 149 | + sorted_tasks_ids.append(u) |
| 150 | + for v in sorted(adj[u]): |
| 151 | + in_degree[v] -= 1 |
| 152 | + if in_degree[v] == 0: |
| 153 | + queue.append(v) |
| 154 | + |
| 155 | + sorted_tasks = [task_map[tid] for tid in sorted_tasks_ids] |
| 156 | + # Add tasks with IDs that were not in the sort (cycles) |
| 157 | + remaining_with_ids = sorted([t for t in tasks if t.id and t.id not in sorted_tasks_ids], key=lambda t: (t.complexity, t.id)) |
| 158 | + # Add tasks without IDs |
| 159 | + without_ids = sorted([t for t in tasks if not t.id], key=lambda t: (t.complexity, t.description)) |
| 160 | + |
| 161 | + return sorted_tasks + remaining_with_ids + without_ids |
| 162 | + |
| 163 | +def format_tasks_to_markdown(tasks): |
| 164 | + active_tasks = [t for t in tasks if t.status != "done"] |
| 165 | + archive_tasks = [t for t in tasks if t.status == "done"] |
| 166 | + |
| 167 | + def group_by_category(task_list): |
| 168 | + grouped = defaultdict(list) |
| 169 | + for t in task_list: |
| 170 | + cat = t.category if t.category else "Uncategorized" |
| 171 | + grouped[cat].append(t) |
| 172 | + return grouped |
| 173 | + |
| 174 | + active_grouped = group_by_category(active_tasks) |
| 175 | + archive_grouped = group_by_category(archive_tasks) |
| 176 | + |
| 177 | + lines = [HEADER_WARNING.strip(), ""] |
| 178 | + |
| 179 | + lines.append("## Active Tasks") |
| 180 | + if not active_tasks: |
| 181 | + lines.append("No active tasks.") |
| 182 | + else: |
| 183 | + for cat in sorted(active_grouped.keys()): |
| 184 | + lines.append(f"### {cat}") |
| 185 | + for t in topological_sort(active_grouped[cat]): |
| 186 | + lines.append(format_task_to_line(t)) |
| 187 | + |
| 188 | + lines.append("## Archive") |
| 189 | + if not archive_tasks: |
| 190 | + lines.append("No archived tasks.") |
| 191 | + else: |
| 192 | + for cat in sorted(archive_grouped.keys()): |
| 193 | + lines.append(f"### {cat}") |
| 194 | + # Archive sorted by complexity then id/description |
| 195 | + cat_tasks = sorted(archive_grouped[cat], key=lambda t: (t.complexity, t.id if t.id else t.description)) |
| 196 | + for t in cat_tasks: |
| 197 | + lines.append(format_task_to_line(t)) |
| 198 | + |
| 199 | + return "\n".join(lines) + "\n" |
| 200 | + |
| 201 | +def main(): |
| 202 | + parser = argparse.ArgumentParser(description="Task management script for TASKS.md.") |
| 203 | + # For now we just implement the reformat on call |
| 204 | + tasks_file_path = "TASKS.md" |
| 205 | + try: |
| 206 | + with open(tasks_file_path, 'r') as f: |
| 207 | + content = f.read() |
| 208 | + except FileNotFoundError: |
| 209 | + sys.exit(1) |
| 210 | + |
| 211 | + tasks = parse_tasks_file(content) |
| 212 | + formatted = format_tasks_to_markdown(tasks) |
| 213 | + with open(tasks_file_path, 'w') as f: |
| 214 | + f.write(formatted) |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + main() |
0 commit comments