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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(scheduled-tasks): supervise watch_store coroutine and respawn on death
cyh4d108a0dabfa
diff --git a/core/scheduled_tasks.py b/core/scheduled_tasks.pyindex aa45f8298..c3823a473 100644--- a/core/scheduled_tasks.py+++ b/core/scheduled_tasks.py@@ -473,6 +473,7 @@ class ScheduledTaskService:self._reconcile_task: Optional[asyncio.Task] = Noneself._job_signatures: Dict[str, tuple[Any, ...]] = {}self._running = False+ self._watch_store_restart_count = 0self.request_store.recover_processing()def validate_platform(self, platform: str) -> None:@@ -484,12 +485,34 @@ class ScheduledTaskService:returnself.scheduler.start()self._running = True- self._reconcile_task = asyncio.create_task(self._watch_store())+ self._spawn_watch_store()try:self.reconcile_jobs()except Exception as exc:logger.error("Initial scheduled task reconcile failed: %s", exc, exc_info=True)+ def _spawn_watch_store(self) -> None:+ self._reconcile_task = asyncio.create_task(self._watch_store())+ self._reconcile_task.add_done_callback(self._on_watch_store_done)++ def _on_watch_store_done(self, task: "asyncio.Task[Any]") -> None:+ # Only respawn if the service is still meant to be running. During+ # stop() we deliberately cancel the task and clear _running first.+ if not self._running:+ return+ if task.cancelled():+ cause: Any = "CancelledError"+ else:+ cause = task.exception()+ self._watch_store_restart_count += 1+ logger.error(+ "Scheduled task watch store exited unexpectedly "+ "(restart_count=%d, cause=%r); respawning",+ self._watch_store_restart_count,+ cause,+ )+ self._spawn_watch_store()+async def stop(self) -> None:self._running = Falseif self._reconcile_task:@@ -507,11 +530,15 @@ class ScheduledTaskService:if self.store.maybe_reload():self.reconcile_jobs()await self._drain_requests()+ await asyncio.sleep(2)except asyncio.CancelledError:raiseexcept Exception as exc:logger.error("Scheduled task store watch failed: %s", exc, exc_info=True)- await asyncio.sleep(2)+ try:+ await asyncio.sleep(2)+ except asyncio.CancelledError:+ raisedef reconcile_jobs(self) -> None:desired_ids = set()diff --git a/tests/test_scheduled_tasks.py b/tests/test_scheduled_tasks.pyindex 714bc0737..ea0f78296 100644--- a/tests/test_scheduled_tasks.py+++ b/tests/test_scheduled_tasks.py@@ -580,5 +580,97 @@ def test_start_keeps_watcher_alive_after_initial_reconcile_failure(tmp_path: Patawait service._reconcile_taskexcept asyncio.CancelledError:pass+ await service.stop()++ asyncio.run(_exercise())+++def test_watch_store_respawns_after_unexpected_cancellation(tmp_path: Path) -> None:+ """A spurious CancelledError must not silently kill the drain loop."""+ store = ScheduledTaskStore(tmp_path / "scheduled_tasks.json")+ controller = SimpleNamespace(platform_settings_managers={})+ service = ScheduledTaskService(controller=controller, store=store)+ service.scheduler = _StubScheduler()++ started = asyncio.Event()++ async def _watch_store():+ started.set()+ await asyncio.Event().wait()++ service._watch_store = _watch_store # type: ignore[method-assign]++ async def _exercise():+ service.start()+ first_task = service._reconcile_task+ assert first_task is not None+ await asyncio.wait_for(started.wait(), timeout=1)++ started.clear()+ first_task.cancel()+ for _ in range(50):+ await asyncio.sleep(0)+ if service._reconcile_task is not None and service._reconcile_task is not first_task:+ break+ assert service._reconcile_task is not None+ assert service._reconcile_task is not first_task+ assert service._watch_store_restart_count == 1++ await asyncio.wait_for(started.wait(), timeout=1)+ await service.stop()++ asyncio.run(_exercise())+++def test_watch_store_respawns_after_unexpected_exception(tmp_path: Path) -> None:+ """If the watch coroutine crashes with a non-Cancelled exception it must respawn."""+ store = ScheduledTaskStore(tmp_path / "scheduled_tasks.json")+ controller = SimpleNamespace(platform_settings_managers={})+ service = ScheduledTaskService(controller=controller, store=store)+ service.scheduler = _StubScheduler()++ invocations: list[int] = []++ async def _watch_store():+ invocations.append(1)+ if len(invocations) == 1:+ raise RuntimeError("boom")+ await asyncio.Event().wait()++ service._watch_store = _watch_store # type: ignore[method-assign]++ async def _exercise():+ service.start()+ for _ in range(50):+ await asyncio.sleep(0)+ if len(invocations) >= 2:+ break+ assert len(invocations) >= 2+ assert service._watch_store_restart_count == 1+ await service.stop()++ asyncio.run(_exercise())+++def test_watch_store_does_not_respawn_after_stop(tmp_path: Path) -> None:+ """stop() cancels the task and must not trigger a respawn."""+ store = ScheduledTaskStore(tmp_path / "scheduled_tasks.json")+ controller = SimpleNamespace(platform_settings_managers={})+ service = ScheduledTaskService(controller=controller, store=store)+ service.scheduler = _StubScheduler()++ async def _watch_store():+ await asyncio.Event().wait()++ service._watch_store = _watch_store # type: ignore[method-assign]++ async def _exercise():+ service.start()+ first_task = service._reconcile_task+ assert first_task is not None+ await service.stop()+ assert service._reconcile_task is None+ assert service._watch_store_restart_count == 0+ assert first_task.cancelled() or first_task.done()asyncio.run(_exercise())