-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
121 lines (96 loc) · 3.76 KB
/
Copy pathtask.py
File metadata and controls
121 lines (96 loc) · 3.76 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
task.py — DevMind's Task System
Inspired by Claude Code's Task.ts.
Tracks the lifecycle of every operation performed by the agent.
"""
import secrets
import string
import time
from dataclasses import dataclass, field
from enum import Enum
# ============================================================
# Task Types — the kinds of work the agent can do
# ============================================================
class TaskType(Enum):
BASH = "bash" # Running a terminal command
FILE_READ = "file_read" # Reading a file
FILE_WRITE = "file_write" # Writing a file
WEB_SEARCH = "web_search" # Searching the internet
AGENT = "agent" # Querying Claude
# ============================================================
# Task Status — the current state of a task
# (Inspired by Task.ts: TaskStatus)
# ============================================================
class TaskStatus(Enum):
PENDING = "pending" # Not started yet
RUNNING = "running" # Currently in progress
COMPLETED = "completed" # Finished successfully
FAILED = "failed" # Failed with an error
KILLED = "killed" # Terminated mid-execution
def is_terminal_status(status: TaskStatus) -> bool:
"""Return whether the task can no longer change state.
Mirrors Task.ts: isTerminalTaskStatus.
"""
return status in (TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.KILLED)
# ============================================================
# Task ID generation — unique token per task
# e.g. "b4f7k2m9" for a bash task
# (Inspired by Task.ts: generateTaskId)
# ============================================================
TASK_PREFIXES = {
TaskType.BASH: "b",
TaskType.FILE_READ: "r",
TaskType.FILE_WRITE: "w",
TaskType.WEB_SEARCH: "s",
TaskType.AGENT: "a",
}
ALPHABET = string.digits + string.ascii_lowercase # 0-9 + a-z
def generate_task_id(task_type: TaskType) -> str:
"""Generate a secure random ID like 'b4f7k2m9'."""
prefix = TASK_PREFIXES.get(task_type, "x")
random_part = "".join(ALPHABET[b % len(ALPHABET)] for b in secrets.token_bytes(8))
return prefix + random_part
# ============================================================
# TaskState — full information about one task
# (Inspired by Task.ts: TaskStateBase)
# ============================================================
@dataclass
class TaskState:
id: str
type: TaskType
description: str
status: TaskStatus = TaskStatus.PENDING
start_time: float = field(default_factory=time.time)
end_time: float | None = None
result: str | None = None
error: str | None = None
def create_task(task_type: TaskType, description: str) -> TaskState:
"""Create a new task."""
return TaskState(
id=generate_task_id(task_type),
type=task_type,
description=description,
)
def complete_task(task: TaskState, result: str) -> TaskState:
"""Mark a task as successfully completed."""
task.status = TaskStatus.COMPLETED
task.end_time = time.time()
task.result = result
return task
def fail_task(task: TaskState, error: str) -> TaskState:
"""Mark a task as failed."""
task.status = TaskStatus.FAILED
task.end_time = time.time()
task.error = error
return task
def get_duration(task: TaskState) -> float | None:
"""Return how many seconds the task took, or None if still running."""
if task.end_time and task.start_time:
return round(task.end_time - task.start_time, 2)
return None
if __name__ == "__main__":
t = create_task(TaskType.BASH, "run git status")
print(f"Task created: {t.id} | Status: {t.status.value}")
t = complete_task(t, "On branch main, nothing to commit")
print(f"Done! Duration: {get_duration(t)}s")
print(f"Terminal status? {is_terminal_status(t.status)}")