Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(tasks): claim sqlite runs atomically

cyh053ae9200e9d
diff --git a/core/scheduled_tasks.py b/core/scheduled_tasks.py
index 541c51bd6..bf99f7d6e 100644
--- a/core/scheduled_tasks.py
+++ b/core/scheduled_tasks.py
@@ -546,13 +546,10 @@ class TaskExecutionStore:
def claim(self, request_id: str) -> Optional[TaskExecutionRequest]:
if self._sqlite is not None:
- payload = self._sqlite.get_run(request_id)
- if not payload or payload.get("status") != "pending":
- return None
now = _utc_now_iso()
- self._sqlite.update_run_status(request_id, status="processing", started_at=now, updated_at=now)
- payload["status"] = "processing"
- payload["started_at"] = now
+ payload = self._sqlite.claim_pending_run(request_id, started_at=now)
+ if payload is None:
+ return None
return TaskExecutionRequest.from_dict(payload)
pending_path = self._request_path(request_id, state="pending")
processing_path = self._request_path(request_id, state="processing")
diff --git a/storage/background.py b/storage/background.py
index 3325e3a87..ad0870c45 100644
--- a/storage/background.py
+++ b/storage/background.py
@@ -151,6 +151,19 @@ class SQLiteBackgroundTaskStore:
row = conn.execute(select(background_runs).where(background_runs.c.id == run_id).limit(1)).mappings().first()
return self._run_from_row(row) if row else None
+ def claim_pending_run(self, run_id: str, *, started_at: str) -> Optional[dict[str, Any]]:
+ with self.engine.begin() as conn:
+ result = conn.execute(
+ update(background_runs)
+ .where(background_runs.c.id == run_id)
+ .where(background_runs.c.status == "pending")
+ .values(status="processing", started_at=started_at, updated_at=started_at)
+ )
+ if not result.rowcount:
+ return None
+ row = conn.execute(select(background_runs).where(background_runs.c.id == run_id).limit(1)).mappings().first()
+ return self._run_from_row(row) if row else None
+
def update_run_status(
self,
run_id: str,
diff --git a/tests/test_scheduled_tasks.py b/tests/test_scheduled_tasks.py
index 902aefc54..0d07b9d47 100644
--- a/tests/test_scheduled_tasks.py
+++ b/tests/test_scheduled_tasks.py
@@ -200,6 +200,26 @@ def test_sqlite_complete_persists_resolved_run_target(tmp_path: Path) -> None:
assert saved["session_id"] == "sesk8m4q2p7x"
+def test_sqlite_claim_only_claims_pending_runs_once(tmp_path: Path) -> None:
+ sqlite = SQLiteBackgroundTaskStore(tmp_path / "state" / "vibe.sqlite")
+ first_store = TaskExecutionStore(tmp_path / "task_requests")
+ second_store = TaskExecutionStore(tmp_path / "task_requests-other")
+ first_store._sqlite = sqlite
+ second_store._sqlite = sqlite
+ request = first_store.enqueue_hook_send(
+ session_key="slack::channel::C123",
+ prompt="hello",
+ )
+
+ first_claim = first_store.claim(request.id)
+ second_claim = second_store.claim(request.id)
+
+ assert first_claim is not None
+ assert first_claim.request_type == "hook_send"
+ assert second_claim is None
+ assert sqlite.get_run(request.id)["status"] == "processing"
+
+
def test_store_round_trip_persists_task(tmp_path: Path) -> None:
store = ScheduledTaskStore(tmp_path / "scheduled_tasks.json")
task = store.add_task(