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(watch): handle spawn failures and wall clock runtime

cyh2be0c92b403b
diff --git a/core/watches.py b/core/watches.py
index b15110d1f..820a199f3 100644
--- a/core/watches.py
+++ b/core/watches.py
@@ -294,7 +294,7 @@ class ManagedWatchService:
self._reconcile_task: Optional[asyncio.Task] = None
self._active_tasks: dict[str, asyncio.Task] = {}
self._active_pids: dict[str, int] = {}
- self._watch_started_at: dict[str, float] = {}
+ self._watch_started_at: dict[str, str] = {}
def start(self) -> None:
if self._running:
@@ -362,18 +362,14 @@ class ManagedWatchService:
payload["watches"][watch_id] = {
"running": not task.done(),
"pid": self._active_pids.get(watch_id),
- "started_at": datetime.fromtimestamp(
- self._watch_started_at.get(watch_id, 0.0), timezone.utc
- ).isoformat()
- if watch_id in self._watch_started_at
- else None,
+ "started_at": self._watch_started_at.get(watch_id),
"updated_at": now,
}
self.runtime_store.write(payload)
async def _run_watch(self, watch_id: str) -> None:
lifetime_started = asyncio.get_running_loop().time()
- self._watch_started_at[watch_id] = lifetime_started
+ self._watch_started_at[watch_id] = _utc_now_iso()
self._write_runtime_state()
while self._running:
@@ -410,7 +406,17 @@ class ManagedWatchService:
cycle_timeout = watch.timeout_seconds
self.store.mark_cycle_start(watch.id)
- result = await self._run_cycle(watch, timeout_seconds=cycle_timeout)
+ try:
+ result = await self._run_cycle(watch, timeout_seconds=cycle_timeout)
+ except asyncio.CancelledError:
+ raise
+ except Exception as exc:
+ result = _CycleResult(
+ exit_code=1,
+ stdout="",
+ stderr=str(exc),
+ timed_out=False,
+ )
if result.exit_code == 0:
prompt = _build_prompt(watch.prefix, result.stdout)
diff --git a/tests/test_watches.py b/tests/test_watches.py
index 0c2efb136..f81f4c4d7 100644
--- a/tests/test_watches.py
+++ b/tests/test_watches.py
@@ -4,6 +4,7 @@ import asyncio
import json
import os
import sys
+from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
@@ -203,3 +204,88 @@ def test_managed_watch_service_stop_terminates_running_waiter(tmp_path: Path) ->
with pytest.raises(ProcessLookupError):
os.kill(pid, 0)
+
+
+def test_managed_watch_service_records_wall_clock_started_at(tmp_path: Path) -> None:
+ store = ManagedWatchStore(tmp_path / "watches.json")
+ request_store = TaskExecutionStore(tmp_path / "task_requests")
+ runtime_store = WatchRuntimeStateStore(tmp_path / "watch_runtime.json")
+ watch = store.add_watch(
+ name="Wait forever",
+ session_key="slack::channel::C123",
+ command=["python3", "-c", "import time; time.sleep(30)"],
+ shell_command=None,
+ prefix=None,
+ cwd=None,
+ mode="forever",
+ timeout_seconds=0,
+ lifetime_timeout_seconds=0,
+ retry_exit_codes=[1],
+ retry_delay_seconds=0.01,
+ post_to=None,
+ deliver_key=None,
+ )
+ service = ManagedWatchService(
+ controller=SimpleNamespace(),
+ store=store,
+ request_store=request_store,
+ runtime_store=runtime_store,
+ )
+
+ async def _run() -> str:
+ service.start()
+ for _ in range(100):
+ started_at = runtime_store.load().get("watches", {}).get(watch.id, {}).get("started_at")
+ if started_at:
+ await service.stop()
+ return started_at
+ await asyncio.sleep(0.02)
+ await service.stop()
+ raise AssertionError("started_at was never written")
+
+ started_at = asyncio.run(_run())
+ assert datetime.fromisoformat(started_at).year >= 2024
+
+
+def test_managed_watch_service_turns_spawn_error_into_failed_cycle(tmp_path: Path) -> None:
+ store = ManagedWatchStore(tmp_path / "watches.json")
+ request_store = TaskExecutionStore(tmp_path / "task_requests")
+ runtime_store = WatchRuntimeStateStore(tmp_path / "watch_runtime.json")
+ watch = store.add_watch(
+ name="Broken waiter",
+ session_key="slack::channel::C123",
+ command=["/definitely/missing/waiter"],
+ shell_command=None,
+ prefix=None,
+ cwd=None,
+ mode="once",
+ timeout_seconds=5,
+ lifetime_timeout_seconds=0,
+ retry_exit_codes=[1],
+ retry_delay_seconds=0.01,
+ post_to=None,
+ deliver_key=None,
+ )
+ service = ManagedWatchService(
+ controller=SimpleNamespace(),
+ store=store,
+ request_store=request_store,
+ runtime_store=runtime_store,
+ )
+
+ async def _run() -> None:
+ service.start()
+ for _ in range(100):
+ if watch.id not in service._active_tasks:
+ break
+ await asyncio.sleep(0.02)
+ await service.stop()
+
+ asyncio.run(_run())
+
+ saved = store.get_watch(watch.id)
+ assert saved is not None
+ assert saved.enabled is False
+ assert saved.last_exit_code == 1
+ assert saved.last_error
+ assert request_store.list_pending() == []