This commit is contained in:
toot
2026-09-16 18:11:48 +08:00
parent 9e01cbbec0
commit ada97a7497
27 changed files with 661 additions and 45 deletions
@@ -71,6 +71,14 @@ class AgentDatabase:
row = connection.execute("SELECT status FROM task_runs WHERE task_id = ?", (task_id,)).fetchone()
return str(row[0]) if row is not None else None
def active_task_ids(self) -> list[str]:
"""Return only work which can still be stopped by this Agent."""
with sqlite3.connect(self._database_path) as connection:
rows = connection.execute(
"SELECT task_id FROM task_runs WHERE status IN ('accepted', 'running')"
).fetchall()
return [str(row[0]) for row in rows]
def create_task(self, task_id: str, instruction: str, received_at: str) -> None:
with sqlite3.connect(self._database_path) as connection:
connection.execute(
@@ -79,6 +87,15 @@ class AgentDatabase:
)
self._record_event(connection, task_id, "accepted", "任务已接收。", received_at)
def cancel_unreceived_task(self, task_id: str, reason: str, occurred_at: str) -> None:
"""Store a cancellation tombstone when cancel overtakes dispatch."""
with sqlite3.connect(self._database_path) as connection:
connection.execute(
"INSERT OR IGNORE INTO task_runs(task_id, instruction, status, received_at, finished_at, error) VALUES (?, '', 'cancelled', ?, ?, ?)",
(task_id, occurred_at, occurred_at, reason),
)
self._record_event(connection, task_id, "cancelled", reason, occurred_at)
def get_meta(self, key: str) -> str:
with sqlite3.connect(self._database_path) as connection:
row = connection.execute("SELECT value FROM application_meta WHERE key = ?", (key,)).fetchone()