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 idempotent start health checks

cyhfa9895d5504f
diff --git a/tests/test_ui_server_logs.py b/tests/test_ui_server_logs.py
index 3eb9d5d8b..624e13bd6 100644
--- a/tests/test_ui_server_logs.py
+++ b/tests/test_ui_server_logs.py
@@ -228,3 +228,22 @@ def test_status_endpoint_degrades_when_pid_probe_raises(monkeypatch, tmp_path):
assert payload["running"] is False
assert payload["pid"] is None
assert payload["state"] == "stopped"
+
+
+def test_control_start_reuses_running_service_without_stop(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ paths.ensure_data_dirs()
+ runtime.write_status("running", detail="already running", service_pid=12345, ui_pid=67890)
+ calls = []
+
+ monkeypatch.setattr(runtime, "ensure_config", lambda: calls.append("ensure_config"))
+ monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop_service"))
+ monkeypatch.setattr(runtime, "start_service", lambda: calls.append("start_service") or 12345)
+
+ client = app.test_client()
+ response = client.post("/control", json={"action": "start"}, headers=csrf_headers(client))
+
+ assert response.status_code == 200
+ assert calls == ["ensure_config", "start_service"]
+ payload = response.get_json()
+ assert payload["status"]["service_pid"] == 12345
diff --git a/tests/test_vibe_cli.py b/tests/test_vibe_cli.py
index 8e550c6da..25b70714a 100644
--- a/tests/test_vibe_cli.py
+++ b/tests/test_vibe_cli.py
@@ -417,15 +417,40 @@ def test_start_ui_reuses_existing_live_pid(tmp_path, monkeypatch):
paths.get_runtime_ui_pid_path().write_text("12345", encoding="utf-8")
monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 12345)
+ monkeypatch.setattr(runtime, "ui_server_healthy", lambda host, port: host == "127.0.0.1" and port == 5123)
def fail_spawn(*_args, **_kwargs):
- raise AssertionError("start_ui should not spawn when an existing UI process is alive")
+ raise AssertionError("start_ui should not spawn when an existing UI process is healthy")
monkeypatch.setattr(runtime, "spawn_background", fail_spawn)
assert runtime.start_ui("127.0.0.1", 5123) == 12345
+def test_start_ui_replaces_stale_live_pid_when_health_check_fails(tmp_path, monkeypatch):
+ monkeypatch.setattr(paths, "get_vibe_remote_dir", lambda: tmp_path / ".vibe_remote")
+ runtime.ensure_dirs()
+ paths.get_runtime_ui_pid_path().write_text("12345", encoding="utf-8")
+
+ monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 12345)
+ monkeypatch.setattr(runtime, "ui_server_healthy", lambda host, port: False)
+
+ def fake_spawn(args, pid_path, stdout_name, stderr_name, env=None):
+ assert args[-1] == "from vibe.ui_server import run_ui_server; run_ui_server('127.0.0.1', 5123)"
+ pid_path.write_text("67890", encoding="utf-8")
+ return 67890
+
+ monkeypatch.setattr(runtime, "spawn_background", fake_spawn)
+
+ assert runtime.start_ui("127.0.0.1", 5123) == 67890
+ assert paths.get_runtime_ui_pid_path().read_text(encoding="utf-8") == "67890"
+
+
+def test_ui_health_url_uses_loopback_for_wildcard_bind():
+ assert runtime._ui_health_url("0.0.0.0", 5100) == "http://127.0.0.1:5100/health"
+ assert runtime._ui_health_url("::", 5100) == "http://[::1]:5100/health"
+
+
def test_shutdown_intent_round_trip(tmp_path, monkeypatch):
monkeypatch.setattr(paths, "get_vibe_remote_dir", lambda: tmp_path / ".vibe_remote")
runtime.ensure_dirs()
diff --git a/vibe/runtime.py b/vibe/runtime.py
index fa0e303d9..77a325609 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -8,6 +8,8 @@ import subprocess
import sys
import threading
import time
+import urllib.error
+import urllib.request
from pathlib import Path
from config import paths
@@ -466,6 +468,27 @@ def start_service():
)
+def _ui_health_url(host: str, port: int) -> str:
+ health_host = (host or "127.0.0.1").strip()
+ if health_host in {"0.0.0.0", ""}:
+ health_host = "127.0.0.1"
+ elif health_host in {"::", "::0"}:
+ health_host = "[::1]"
+ elif health_host.startswith("[") and health_host.endswith("]"):
+ pass
+ elif ":" in health_host:
+ health_host = f"[{health_host}]"
+ return f"http://{health_host}:{port}/health"
+
+
+def ui_server_healthy(host: str, port: int, timeout: float = 0.5) -> bool:
+ try:
+ with urllib.request.urlopen(_ui_health_url(host, port), timeout=timeout) as response:
+ return response.status == 200
+ except (OSError, urllib.error.URLError, TimeoutError, ValueError):
+ return False
+
+
def resolve_localhost_family() -> str:
"""Return the loopback family ``localhost`` actually maps to on this host.
@@ -534,7 +557,7 @@ def start_ui(host, port):
existing_pid = int(pid_path.read_text(encoding="utf-8").strip())
except Exception:
existing_pid = 0
- if existing_pid and pid_alive(existing_pid):
+ if existing_pid and pid_alive(existing_pid) and ui_server_healthy(host, port):
return existing_pid
pid_path.unlink(missing_ok=True)
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index c3d181ab2..60214e0c6 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -1209,7 +1209,6 @@ def control():
status["last_action"] = action
if action == "start":
runtime.ensure_config()
- runtime.stop_service()
service_pid = runtime.start_service()
runtime.write_status("running", "started", service_pid, status.get("ui_pid"))
elif action == "stop":