fix: reliably dispatch and return background tasks
Rebuild & Deploy / rebuild (push) Successful in 47s

This commit is contained in:
snowspeeder committed 2026-08-29 07:29:38 -04:00
1 parent 746ca8dca9
commit 321421e37f
7 files changed
+310 -27

No files matched your search

+15 -9
View File
@@ -52,12 +52,18 @@ class Task:
class TaskRegistry:
"""File-backed registry. Writers (MCP process) append to events.jsonl.
Readers (main agent) tail the file and dispatch to subscribers."""
Readers (main agent) tail the file and dispatch to subscribers.
def __init__(self):
TASKS_DIR.mkdir(parents=True, exist_ok=True)
if not EVENTS_FILE.exists():
EVENTS_FILE.touch()
A registry can be scoped to a task directory. Voice sessions use a unique
directory so one room can never consume another room's task results.
"""
def __init__(self, tasks_dir: str | Path | None = None):
self.tasks_dir = Path(tasks_dir) if tasks_dir is not None else TASKS_DIR
self.events_file = self.tasks_dir / "events.jsonl"
self.tasks_dir.mkdir(parents=True, exist_ok=True)
if not self.events_file.exists():
self.events_file.touch()
self._tasks: dict[str, Task] = {}
self._listeners: list[Callable[[str, dict], Any]] = []
self._tail_pos = 0
@@ -109,7 +115,7 @@ class TaskRegistry:
"""Read new events from the file. Call periodically from the agent."""
events = []
try:
with open(EVENTS_FILE, "r") as f:
with open(self.events_file, "r", encoding="utf-8") as f:
f.seek(self._tail_pos)
for line in f:
line = line.strip()
@@ -136,12 +142,12 @@ class TaskRegistry:
def _write_event(self, task_id: str, event: dict):
payload = json.dumps({"task_id": task_id, **event})
with open(EVENTS_FILE, "a") as f:
with open(self.events_file, "a", encoding="utf-8") as f:
f.write(payload + "\n")
def _save_task(self, task: Task):
path = TASKS_DIR / f"{task.id}.json"
path.write_text(json.dumps(self._task_dict(task)))
path = self.tasks_dir / f"{task.id}.json"
path.write_text(json.dumps(self._task_dict(task)), encoding="utf-8")
@staticmethod
def _task_dict(task: Task) -> dict: