mirror of
https://github.com/openai/codex.git
synced 2026-04-28 02:11:08 +03:00
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""
|
|
Simple library for loading and saving task metadata embedded as TOML front-matter
|
|
in task Markdown files.
|
|
"""
|
|
import re
|
|
import subprocess
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import toml
|
|
from enum import Enum
|
|
from pydantic import BaseModel, Field
|
|
|
|
FRONTMATTER_RE = re.compile(r"^\+\+\+\s*(.*?)\s*\+\+\+", re.S | re.M)
|
|
|
|
def repo_root():
|
|
return Path(subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).decode().strip())
|
|
|
|
def task_dir():
|
|
return repo_root() / "agentydragon/tasks"
|
|
|
|
def worktree_dir():
|
|
return task_dir() / ".worktrees"
|
|
|
|
class TaskStatus(str, Enum):
|
|
NOT_STARTED = "Not started"
|
|
IN_PROGRESS = "In progress"
|
|
NEEDS_INPUT = "Needs input"
|
|
NEEDS_MANUAL_REVIEW = "Needs manual review"
|
|
DONE = "Done"
|
|
CANCELLED = "Cancelled"
|
|
MERGED = "Merged"
|
|
|
|
class TaskMeta(BaseModel):
|
|
id: str
|
|
title: str
|
|
status: TaskStatus
|
|
freeform_status: str = Field(default="")
|
|
dependencies: str = Field(default="")
|
|
last_updated: datetime = Field(default_factory=datetime.utcnow)
|
|
|
|
def load_task(path: Path) -> (TaskMeta, str):
|
|
text = path.read_text(encoding='utf-8')
|
|
m = FRONTMATTER_RE.match(text)
|
|
if not m:
|
|
raise ValueError(f"No TOML frontmatter in {path}")
|
|
meta = toml.loads(m.group(1))
|
|
tm = TaskMeta(**meta)
|
|
body = text[m.end():].lstrip('\n')
|
|
return tm, body
|
|
|
|
def save_task(path: Path, meta: TaskMeta, body: str) -> None:
|
|
tm = meta.dict()
|
|
# Serialize enum to its string value for front-matter
|
|
if isinstance(tm.get('status'), Enum):
|
|
tm['status'] = tm['status'].value
|
|
tm['last_updated'] = meta.last_updated.isoformat()
|
|
fm = toml.dumps(tm).strip()
|
|
content = f"+++\n{fm}\n+++\n\n{body.lstrip()}"
|
|
path.write_text(content, encoding='utf-8')
|