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): require stable startup before success

cyh6e51f50ff983
diff --git a/tests/test_cli_watch_command.py b/tests/test_cli_watch_command.py
index ca3e82fc8..428b003dc 100644
--- a/tests/test_cli_watch_command.py
+++ b/tests/test_cli_watch_command.py
@@ -4,6 +4,7 @@ import io
import json
import sys
from contextlib import redirect_stderr
+from datetime import datetime, timedelta, timezone
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
@@ -282,6 +283,113 @@ def test_watch_add_returns_structured_error_when_startup_fails(tmp_path: Path) -
assert payload["help_command"] == "vibe watch show abc"
+def test_wait_for_watch_startup_accepts_stably_running_watch(tmp_path: Path) -> None:
+ store = ManagedWatchStore(tmp_path / "watches.json")
+ runtime_store = WatchRuntimeStateStore(tmp_path / "watch_runtime.json")
+ watch = store.add_watch(
+ name="Stable watch",
+ session_key="slack::channel::C123",
+ command=["python3", "wait.py"],
+ shell_command=None,
+ prefix=None,
+ cwd=None,
+ mode="forever",
+ timeout_seconds=600,
+ lifetime_timeout_seconds=0,
+ retry_exit_codes=[75],
+ retry_delay_seconds=30,
+ post_to=None,
+ deliver_key=None,
+ )
+ watch.last_started_at = (datetime.now(timezone.utc) - timedelta(seconds=2)).isoformat()
+ store.upsert_watch(watch)
+ runtime_store.write(
+ {
+ "watches": {
+ watch.id: {
+ "running": True,
+ "pid": 1234,
+ "started_at": (datetime.now(timezone.utc) - timedelta(seconds=2)).isoformat(),
+ "updated_at": datetime.now(timezone.utc).isoformat(),
+ }
+ }
+ }
+ )
+
+ resolved_watch, runtime_entry = cli._wait_for_watch_startup(
+ store,
+ runtime_store,
+ watch.id,
+ timeout_seconds=0.2,
+ poll_interval_seconds=0.01,
+ stable_running_seconds=1.5,
+ )
+
+ assert resolved_watch.id == watch.id
+ assert runtime_entry["running"] is True
+
+
+def test_wait_for_watch_startup_rejects_watch_that_fails_before_stable_window(tmp_path: Path) -> None:
+ store = ManagedWatchStore(tmp_path / "watches.json")
+ runtime_store = WatchRuntimeStateStore(tmp_path / "watch_runtime.json")
+ watch = store.add_watch(
+ name="Flaky watch",
+ session_key="slack::channel::C123",
+ command=["python3", "wait.py"],
+ shell_command=None,
+ prefix=None,
+ cwd=None,
+ mode="forever",
+ timeout_seconds=600,
+ lifetime_timeout_seconds=0,
+ retry_exit_codes=[75],
+ retry_delay_seconds=30,
+ post_to=None,
+ deliver_key=None,
+ )
+ watch.last_started_at = datetime.now(timezone.utc).isoformat()
+ store.upsert_watch(watch)
+ runtime_store.write(
+ {
+ "watches": {
+ watch.id: {
+ "running": True,
+ "pid": 1234,
+ "started_at": datetime.now(timezone.utc).isoformat(),
+ "updated_at": datetime.now(timezone.utc).isoformat(),
+ }
+ }
+ }
+ )
+
+ monotonic_values = iter([0.0, 0.05, 0.1, 0.15, 0.2])
+
+ def _fail_watch(_seconds: float) -> None:
+ failed = store.get_watch(watch.id)
+ assert failed is not None
+ failed.enabled = False
+ failed.last_error = "waiter crashed"
+ failed.last_exit_code = 1
+ store.upsert_watch(failed)
+ runtime_store.write({"watches": {}})
+
+ with (
+ patch("vibe.cli.time.monotonic", side_effect=lambda: next(monotonic_values)),
+ patch("vibe.cli.time.sleep", side_effect=_fail_watch),
+ ):
+ with pytest.raises(cli.TaskCliError) as exc:
+ cli._wait_for_watch_startup(
+ store,
+ runtime_store,
+ watch.id,
+ timeout_seconds=0.2,
+ poll_interval_seconds=0.01,
+ stable_running_seconds=1.5,
+ )
+
+ assert exc.value.code == "watch_startup_failed"
+
+
def test_watch_list_brief_includes_runtime_state(tmp_path: Path, capsys) -> None:
store = ManagedWatchStore(tmp_path / "watches.json")
runtime_store = WatchRuntimeStateStore(tmp_path / "watch_runtime.json")
diff --git a/vibe/cli.py b/vibe/cli.py
index b15ddda52..83ad105ce 100644
--- a/vibe/cli.py
+++ b/vibe/cli.py
@@ -814,6 +814,18 @@ def _watch_payload(watch, runtime_entry: Optional[dict[str, object]], *, brief:
return payload
+def _seconds_since_iso(timestamp: object) -> float | None:
+ if not isinstance(timestamp, str) or not timestamp.strip():
+ return None
+ try:
+ started_at = datetime.fromisoformat(timestamp)
+ except ValueError:
+ return None
+ if started_at.tzinfo is None:
+ started_at = started_at.replace(tzinfo=timezone.utc)
+ return max(0.0, (datetime.now(timezone.utc) - started_at).total_seconds())
+
+
def _wait_for_watch_startup(
store: ManagedWatchStore,
runtime_store: WatchRuntimeStateStore,
@@ -821,6 +833,7 @@ def _wait_for_watch_startup(
*,
timeout_seconds: float = 4.0,
poll_interval_seconds: float = 0.1,
+ stable_running_seconds: float = 1.5,
):
inspect_command = f"vibe watch show {watch_id}"
deadline = time.monotonic() + timeout_seconds
@@ -846,12 +859,12 @@ def _wait_for_watch_startup(
help_command=inspect_command,
details={"watch": _watch_payload(watch, runtime_entry)},
)
- if runtime_entry and runtime_entry.get("running"):
- return watch, runtime_entry
- if watch.last_started_at:
- return watch, runtime_entry
- if watch.mode == "once" and watch.last_finished_at:
+ if watch.mode == "once" and watch.last_finished_at and not watch.last_error and watch.last_exit_code == 0:
return watch, runtime_entry
+ if runtime_entry and runtime_entry.get("running"):
+ stable_for = _seconds_since_iso(runtime_entry.get("started_at")) or _seconds_since_iso(watch.last_started_at)
+ if stable_for is not None and stable_for >= stable_running_seconds:
+ return watch, runtime_entry
time.sleep(poll_interval_seconds)
store.maybe_reload()