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(runtime): complete restart supervisor stop handling

cyhb24dc4f2ca9a
diff --git a/tests/test_restart_supervisor.py b/tests/test_restart_supervisor.py
index 5b4e60b8d..98c3254fd 100644
--- a/tests/test_restart_supervisor.py
+++ b/tests/test_restart_supervisor.py
@@ -47,7 +47,8 @@ def test_restart_job_stops_and_starts_service(monkeypatch, tmp_path):
paths.get_runtime_pid_path().write_text("111", encoding="utf-8")
calls = []
- monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop") or True)
+ monkeypatch.setattr(runtime, "stop_ui", lambda: calls.append("stop_ui") or True)
+ monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop_service") or True)
monkeypatch.setattr(restart_supervisor, "get_safe_cwd", lambda: str(tmp_path))
monkeypatch.setattr(restart_supervisor, "get_restart_invocation_command", lambda vibe_path=None: ["/bin/vibe", "restart"])
monkeypatch.setattr(restart_supervisor, "get_restart_environment", lambda vibe_path=None: None)
@@ -63,7 +64,7 @@ def test_restart_job_stops_and_starts_service(monkeypatch, tmp_path):
rc = restart_supervisor._run_restart_job(job_id="jobabc", delay_seconds=0, vibe_path="/bin/vibe", trigger="test")
assert rc == 0
- assert calls == ["stop", ("run", ["/bin/vibe", "start"])]
+ assert calls == ["stop_ui", "stop_service", ("run", ["/bin/vibe", "start"])]
status = runtime.read_json(runtime.get_restart_status_path())
assert status["ok"] is True
assert status["state"] == "succeeded"
@@ -77,15 +78,69 @@ def test_restart_job_aborts_when_stop_fails(monkeypatch, tmp_path):
paths.get_runtime_pid_path().write_text("111", encoding="utf-8")
calls = []
- monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop") or False)
+ monkeypatch.setattr(runtime, "stop_ui", lambda: calls.append("stop_ui") or True)
+ monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop_service") or False)
+ monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 111)
monkeypatch.setattr(restart_supervisor, "get_safe_cwd", lambda: str(tmp_path))
monkeypatch.setattr(restart_supervisor.subprocess, "run", lambda *args, **kwargs: calls.append("run"))
rc = restart_supervisor._run_restart_job(job_id="jobdef", delay_seconds=0, vibe_path="/bin/vibe", trigger="test")
assert rc == 2
- assert calls == ["stop"]
+ assert calls == ["stop_ui", "stop_service"]
status = runtime.read_json(runtime.get_restart_status_path())
assert status["ok"] is False
assert status["state"] == "failed"
assert "did not stop" in status["error"]
+
+
+def test_restart_job_continues_when_old_pid_already_exited(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ paths.ensure_data_dirs()
+ paths.get_runtime_pid_path().write_text("111", encoding="utf-8")
+ calls = []
+
+ monkeypatch.setattr(runtime, "stop_ui", lambda: calls.append("stop_ui") or True)
+ monkeypatch.setattr(runtime, "stop_service", lambda: calls.append("stop_service") or False)
+ monkeypatch.setattr(restart_supervisor, "get_safe_cwd", lambda: str(tmp_path))
+ monkeypatch.setattr(restart_supervisor, "get_restart_invocation_command", lambda vibe_path=None: ["/bin/vibe", "restart"])
+ monkeypatch.setattr(restart_supervisor, "get_restart_environment", lambda vibe_path=None: None)
+
+ def fake_run(command, **kwargs):
+ calls.append(("run", command))
+ paths.get_runtime_pid_path().write_text("222", encoding="utf-8")
+ return subprocess.CompletedProcess(command, 0)
+
+ monkeypatch.setattr(restart_supervisor.subprocess, "run", fake_run)
+ monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 222)
+
+ rc = restart_supervisor._run_restart_job(job_id="joboldgone", delay_seconds=0, vibe_path="/bin/vibe", trigger="test")
+
+ assert rc == 0
+ assert calls == ["stop_ui", "stop_service", ("run", ["/bin/vibe", "start"])]
+ assert runtime.read_json(runtime.get_restart_status_path())["state"] == "succeeded"
+
+
+def test_restart_job_marks_start_timeout_failed(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ paths.ensure_data_dirs()
+ paths.get_runtime_pid_path().write_text("111", encoding="utf-8")
+
+ monkeypatch.setattr(runtime, "stop_ui", lambda: True)
+ monkeypatch.setattr(runtime, "stop_service", lambda: True)
+ monkeypatch.setattr(restart_supervisor, "get_safe_cwd", lambda: str(tmp_path))
+ monkeypatch.setattr(restart_supervisor, "get_restart_invocation_command", lambda vibe_path=None: ["/bin/vibe", "restart"])
+ monkeypatch.setattr(restart_supervisor, "get_restart_environment", lambda vibe_path=None: None)
+ monkeypatch.setattr(
+ restart_supervisor.subprocess,
+ "run",
+ lambda command, **kwargs: (_ for _ in ()).throw(subprocess.TimeoutExpired(command, 30)),
+ )
+
+ rc = restart_supervisor._run_restart_job(job_id="jobtimeout", delay_seconds=0, vibe_path="/bin/vibe", trigger="test")
+
+ assert rc == 4
+ status = runtime.read_json(runtime.get_restart_status_path())
+ assert status["ok"] is False
+ assert status["state"] == "failed"
+ assert "timed out" in status["error"]
diff --git a/tests/test_vibe_cli.py b/tests/test_vibe_cli.py
index 4cc19846c..165ad072c 100644
--- a/tests/test_vibe_cli.py
+++ b/tests/test_vibe_cli.py
@@ -214,14 +214,14 @@ def test_cmd_stop_fails_when_live_service_survives(monkeypatch, capsys):
assert "Vibe service did not stop" in capsys.readouterr().err
-def test_cmd_vibe_uses_restart_compatibility_default(monkeypatch):
+def test_cmd_vibe_uses_start_compatibility_default(monkeypatch):
calls = []
- monkeypatch.setattr(cli, "_cmd_restart_with_delay", lambda delay: calls.append(("restart", delay)) or 0)
+ monkeypatch.setattr(cli, "cmd_start", lambda: calls.append("start") or 0)
assert cli.cmd_vibe() == 0
- assert calls == [("restart", 0.0)]
+ assert calls == ["start"]
def test_runtime_architecture_items_warn_for_x86_uv_on_apple_silicon(monkeypatch):
diff --git a/vibe/cli.py b/vibe/cli.py
index e045dc93d..6e94cf1c1 100644
--- a/vibe/cli.py
+++ b/vibe/cli.py
@@ -3367,8 +3367,8 @@ def cmd_start():
def cmd_vibe():
- """Compatibility default: bare `vibe` restarts during the migration window."""
- return _cmd_restart_with_delay(0.0)
+ """Compatibility default: bare `vibe` starts services and opens the Web UI."""
+ return cmd_start()
def _stop_opencode_server():
diff --git a/vibe/restart_supervisor.py b/vibe/restart_supervisor.py
index 3a3edcd79..313f822cd 100644
--- a/vibe/restart_supervisor.py
+++ b/vibe/restart_supervisor.py
@@ -62,6 +62,14 @@ def _read_recorded_pid() -> int | None:
return pid if pid > 0 else None
+def _fail(payload: dict, error: str, log, return_code: int) -> int:
+ payload.update(ok=False, state="failed", error=error)
+ _write_status(payload)
+ log.write(f"{_now_iso()} {error}\n")
+ log.flush()
+ return return_code
+
+
def _run_restart_job(
*,
job_id: str,
@@ -100,40 +108,46 @@ def _run_restart_job(
_write_status(payload)
write("restart job started after delay")
+ write("stopping UI")
+ ui_stopped = runtime.stop_ui()
+ ui_pid = None
+ try:
+ ui_pid = int(paths.get_runtime_ui_pid_path().read_text(encoding="utf-8").strip())
+ except (OSError, ValueError):
+ pass
+ if ui_pid and ui_stopped is False and runtime.pid_alive(ui_pid):
+ return _fail(payload, f"UI pid {ui_pid} did not stop", log, 2)
+
write("stopping service")
stopped = runtime.stop_service()
- if old_pid and stopped is False:
- payload.update(ok=False, state="failed", error=f"service pid {old_pid} did not stop")
- _write_status(payload)
- write(payload["error"])
- return 2
+ if old_pid and stopped is False and runtime.pid_alive(old_pid):
+ return _fail(payload, f"service pid {old_pid} did not stop", log, 2)
write("starting service")
command = get_restart_invocation_command(vibe_path=vibe_path)
env = get_restart_environment(vibe_path=vibe_path)
start_command = [*command[:-1], "start"] if command and command[-1] == "restart" else [*(command or ["vibe"]), "start"]
- result = subprocess.run(
- start_command,
- cwd=safe_cwd,
- env=env,
- stdout=log,
- stderr=subprocess.STDOUT,
- text=True,
- check=False,
- timeout=30,
- )
+ try:
+ result = subprocess.run(
+ start_command,
+ cwd=safe_cwd,
+ env=env,
+ stdout=log,
+ stderr=subprocess.STDOUT,
+ text=True,
+ check=False,
+ timeout=30,
+ )
+ except subprocess.TimeoutExpired:
+ return _fail(payload, "start command timed out after 30 seconds", log, 4)
+ except Exception as exc:
+ return _fail(payload, f"start command failed: {exc}", log, 1)
if result.returncode != 0:
- payload.update(ok=False, state="failed", error=f"start command failed with exit code {result.returncode}")
- _write_status(payload)
- write(payload["error"])
- return result.returncode or 1
+ return _fail(payload, f"start command failed with exit code {result.returncode}", log, result.returncode or 1)
new_pid = _read_recorded_pid()
if not new_pid or not runtime.pid_alive(new_pid):
- payload.update(ok=False, state="failed", error="start command completed but service pid is not alive")
- _write_status(payload)
- write(payload["error"])
- return 3
+ return _fail(payload, "start command completed but service pid is not alive", log, 3)
payload.update(ok=True, state="succeeded", new_pid=new_pid, error=None)
_write_status(payload)