-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_system.py
More file actions
102 lines (78 loc) · 2.82 KB
/
Copy pathtask_system.py
File metadata and controls
102 lines (78 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
import time
import random
from pathlib import Path
from dataclasses import dataclass, asdict
from config import get_logger
from wcode_errors import ToolError
logger = get_logger(__name__)
WORKDIR = Path.cwd()
TASKS_DIR = WORKDIR / ".tasks"
TASKS_DIR.mkdir(exist_ok=True)
@dataclass
class Task:
id: str
subject: str
description: str
status: str # pending | in_progress | completed
owner: str | None # Agent name (multi-agent scenarios)
blockedBy: list[str] # Dependency task IDs
def _task_path(task_id: str) -> Path:
return TASKS_DIR / f"{task_id}.json"
def save_task(task: Task):
_task_path(task.id).write_text(json.dumps(asdict(task), indent=2))
def create_task(subject: str, description: str = "",
blockedBy: list[str] | None = None) -> Task:
task = Task(
id=f"task_{int(time.time())}_{random.randint(0, 9999):04d}",
subject=subject,
description=description,
status="pending",
owner=None,
blockedBy=blockedBy or [],
)
save_task(task)
return task
def load_task(task_id: str) -> Task:
path = _task_path(task_id)
return Task(**json.loads(path.read_text()))
def list_tasks() -> list[Task]:
return [load_task(f.stem) for f in sorted(TASKS_DIR.glob("*.json"))]
def get_task(task_id: str) -> str:
task = load_task(task_id)
return json.dumps(asdict(task), indent=2)
def can_start(task_id: str) -> bool:
task = load_task(task_id)
for dep_id in task.blockedBy:
if not _task_path(dep_id).exists():
return False
if load_task(dep_id).status != "completed":
return False
return True
def claim_task(task_id: str, owner: str = "agent") -> str:
task = load_task(task_id)
if task.status != "pending":
raise ToolError(f"Task {task_id} is {task.status}, cannot claim.")
if not can_start(task_id):
deps = [d for d in task.blockedBy
if not _task_path(d).exists() or load_task(d).status != "completed"]
raise ToolError(f"Blocked by: {deps}")
task.owner = owner
task.status = "in_progress"
save_task(task)
logger.info(f"[claim] {task.subject} → in_progress (owner: {owner})")
return f"Claimed {task.id} ({task.subject})"
def complete_task(task_id: str) -> str:
task = load_task(task_id)
if task.status != "in_progress":
raise ToolError(f"Task {task_id} is {task.status}, cannot complete.")
task.status = "completed"
save_task(task)
return f"Completed {task.id} ({task.subject})"
def scan_unclaimed_tasks() -> list[dict]:
unclaimed = []
for f in sorted(TASKS_DIR.glob("*.json")):
task = json.loads(f.read_text())
if task.get("status") == "pending" and not task.get("owner") and can_start(task["id"]):
unclaimed.append(task)
return unclaimed