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.

Validate UI pid ownership before stop

cyh015cd9839629
diff --git a/tests/test_vibe_cli.py b/tests/test_vibe_cli.py
index c09448e74..969bbb6cc 100644
--- a/tests/test_vibe_cli.py
+++ b/tests/test_vibe_cli.py
@@ -435,6 +435,16 @@ def test_start_ui_replaces_stale_live_pid_when_health_check_fails(tmp_path, monk
monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 12345)
monkeypatch.setattr(runtime, "ui_server_healthy", lambda host, port: False)
+ monkeypatch.setattr(
+ runtime,
+ "get_process_command",
+ lambda pid: (
+ f"{sys.executable} -c "
+ "\"from vibe.ui_server import run_ui_server; run_ui_server('127.0.0.1', 5123)\""
+ if pid == 12345
+ else None
+ ),
+ )
monkeypatch.setattr(runtime, "stop_pid", lambda pid: stopped.append(pid) or True)
monkeypatch.setattr(runtime, "wait_for_ui_server", lambda host, port: True)
@@ -450,6 +460,30 @@ def test_start_ui_replaces_stale_live_pid_when_health_check_fails(tmp_path, monk
assert paths.get_runtime_ui_pid_path().read_text(encoding="utf-8") == "67890"
+def test_start_ui_does_not_stop_unrelated_reused_pid(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)
+ monkeypatch.setattr(runtime, "get_process_command", lambda pid: "/usr/bin/unrelated --work" if pid == 12345 else None)
+ monkeypatch.setattr(runtime, "wait_for_ui_server", lambda host, port: True)
+
+ def fail_stop(pid, timeout=5):
+ raise AssertionError(f"unrelated pid should not be stopped: {pid}")
+
+ def fake_spawn(args, pid_path, stdout_name, stderr_name, env=None):
+ pid_path.write_text("67890", encoding="utf-8")
+ return 67890
+
+ monkeypatch.setattr(runtime, "stop_pid", fail_stop)
+ 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_start_ui_waits_for_replacement_health(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 d3a10fcc3..e0b205bab 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -498,6 +498,13 @@ def wait_for_ui_server(host: str, port: int, timeout: float = 5.0) -> bool:
return ui_server_healthy(host, port)
+def _pid_matches_ui_server(pid: int) -> bool:
+ command = get_process_command(pid)
+ if not command:
+ return False
+ return "vibe.ui_server" in command and "run_ui_server" in command
+
+
def resolve_localhost_family() -> str:
"""Return the loopback family ``localhost`` actually maps to on this host.
@@ -569,12 +576,18 @@ def start_ui(host, port):
if existing_pid and pid_alive(existing_pid) and ui_server_healthy(host, port):
return existing_pid
if existing_pid and pid_alive(existing_pid):
- logger.warning(
- "Stopping stale UI process pid=%s because health check failed for %s",
- existing_pid,
- _ui_health_url(host, port),
- )
- stop_pid(existing_pid)
+ if _pid_matches_ui_server(existing_pid):
+ logger.warning(
+ "Stopping stale UI process pid=%s because health check failed for %s",
+ existing_pid,
+ _ui_health_url(host, port),
+ )
+ stop_pid(existing_pid)
+ else:
+ logger.warning(
+ "Ignoring stale UI pid file pid=%s because it does not match the Vibe UI server",
+ existing_pid,
+ )
pid_path.unlink(missing_ok=True)
command = "from vibe.ui_server import run_ui_server; run_ui_server('{}', {})".format(host, port)