"""QUEUE.md parser and task state tracking.""" import re from dataclasses import dataclass, field from enum import Enum from pathlib import Path class TaskStatus(Enum): PENDING = "pending" RUNNING = "running" DONE = "done" FAILED = "failed" SKIPPED = "skipped" class TaskAction(Enum): EDIT = "EDIT" CREATE = "CREATE" DELETE = "DELETE" TEST = "TEST" @dataclass class Task: action: TaskAction filepath: str target: str # function name, or "-" for whole file instruction: str status: TaskStatus = TaskStatus.PENDING attempts: int = 0 error: str = "" line_number: int = 0 # line in QUEUE.md for status updates # QUEUE.md line format: # EDIT auth.py validate_login "reject empty password strings" # CREATE config.py - "define AUTH_SECRET from env" _LINE_RE = re.compile( r'^(EDIT|CREATE|DELETE|TEST)\s+(\S+)\s+(\S+)\s+"([^"]+)"', re.IGNORECASE, ) # Status markers embedded in QUEUE.md # [done] EDIT ... # [failed:3] EDIT ... # [skipped] EDIT ... _STATUS_RE = re.compile(r'^\[(done|failed:\d+|skipped)\]\s+', re.IGNORECASE) def parse_queue(path: str) -> list[Task]: """Parse QUEUE.md and return tasks in order. Lines prefixed with [done], [failed:N], or [skipped] are included but their status is set accordingly. Lines starting with # or blank are ignored. """ tasks = [] text = Path(path).read_text() for lineno, raw_line in enumerate(text.splitlines(), start=1): line = raw_line.strip() if not line or line.startswith("#"): continue status = TaskStatus.PENDING attempts = 0 # Strip status marker if present m_status = _STATUS_RE.match(line) if m_status: marker = m_status.group(1).lower() if marker == "done": status = TaskStatus.DONE elif marker == "skipped": status = TaskStatus.SKIPPED elif marker.startswith("failed:"): status = TaskStatus.FAILED attempts = int(marker.split(":")[1]) line = line[m_status.end():] m = _LINE_RE.match(line) if not m: continue action_str, filepath, target, instruction = m.groups() try: action = TaskAction[action_str.upper()] except KeyError: continue tasks.append(Task( action=action, filepath=filepath, target=target, instruction=instruction, status=status, attempts=attempts, line_number=lineno, )) return tasks def write_queue(path: str, tasks: list[Task]) -> None: """Write tasks back to QUEUE.md with updated status markers.""" lines = [] for t in tasks: marker = "" if t.status == TaskStatus.DONE: marker = "[done] " elif t.status == TaskStatus.FAILED: marker = f"[failed:{t.attempts}] " elif t.status == TaskStatus.SKIPPED: marker = "[skipped] " target = t.target if t.target else "-" lines.append(f'{marker}{t.action.value} {t.filepath} {target} "{t.instruction}"') Path(path).write_text("\n".join(lines) + "\n") def queue_summary(tasks: list[Task]) -> str: """One-line summary of queue state.""" counts = {s: 0 for s in TaskStatus} for t in tasks: counts[t.status] += 1 total = len(tasks) done = counts[TaskStatus.DONE] failed = counts[TaskStatus.FAILED] skipped = counts[TaskStatus.SKIPPED] pending = counts[TaskStatus.PENDING] return ( f"{total} tasks: {done} done, {pending} pending, " f"{failed} failed, {skipped} skipped" )