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(ui): queue restart fallback after in-flight restart

cyhc734c5045379
diff --git a/tests/test_restart_supervisor.py b/tests/test_restart_supervisor.py
index 51c400c53..b77cf4858 100644
--- a/tests/test_restart_supervisor.py
+++ b/tests/test_restart_supervisor.py
@@ -188,6 +188,56 @@ def test_restart_job_prepares_show_runtime_after_service_start(monkeypatch, tmp_
assert runtime.read_json(runtime.get_restart_status_path())["state"] == "succeeded"
+def test_restart_job_schedules_pending_followup_after_success(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 = []
+ scheduled: list[dict] = []
+
+ monkeypatch.setattr(restart_supervisor, "_stop_runtime_for_restart", lambda stop_ui=True: _fake_stop_runtime(calls))
+ monkeypatch.setattr(restart_supervisor, "_start_runtime_processes", lambda start_ui=True: _fake_start_runtime(calls))
+ monkeypatch.setattr(restart_supervisor, "_wait_for_service_lock_release", lambda: True)
+ monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid == 222)
+ monkeypatch.setattr(runtime, "service_pid_recorded", lambda pid: pid == 222)
+
+ original_schedule_restart = restart_supervisor.schedule_restart
+
+ def _schedule_restart(**kwargs):
+ scheduled.append(kwargs)
+ return {"job_id": "followup"}
+
+ restart_supervisor.mark_pending_restart(
+ trigger="web-ui-config-pending",
+ scope="service",
+ reason="restart_in_progress",
+ restart_job_id="jobpending",
+ )
+ monkeypatch.setattr(restart_supervisor, "schedule_restart", _schedule_restart)
+
+ try:
+ rc = restart_supervisor._run_restart_job(
+ job_id="jobpending",
+ delay_seconds=0,
+ vibe_path="/bin/vibe",
+ trigger="web-ui",
+ scope="service",
+ )
+ finally:
+ monkeypatch.setattr(restart_supervisor, "schedule_restart", original_schedule_restart)
+
+ assert rc == 0
+ assert scheduled == [
+ {
+ "delay_seconds": 0.0,
+ "vibe_path": "/bin/vibe",
+ "trigger": "web-ui-config-pending",
+ "scope": "service",
+ }
+ ]
+ assert runtime.read_json(restart_supervisor._pending_restart_path()) is None
+
+
def test_restart_job_aborts_when_stop_fails(monkeypatch, tmp_path):
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
paths.ensure_data_dirs()
diff --git a/tests/test_ui_server_fastapi.py b/tests/test_ui_server_fastapi.py
index 3347591b1..66b72bd34 100644
--- a/tests/test_ui_server_fastapi.py
+++ b/tests/test_ui_server_fastapi.py
@@ -494,6 +494,37 @@ def test_config_post_schedules_service_restart_when_hot_reconcile_fails(monkeypa
assert restart_calls == [True]
+def test_config_restart_fallback_marks_pending_restart_when_restart_in_flight(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ from vibe import restart_supervisor
+ from vibe import runtime
+
+ runtime.get_restart_status_path().parent.mkdir(parents=True, exist_ok=True)
+ restart_status = {
+ "ok": None,
+ "state": "running",
+ "job_id": "job-in-flight",
+ "supervisor_pid": 4242,
+ }
+ runtime.write_json(runtime.get_restart_status_path(), restart_status)
+ monkeypatch.setattr(ui_server, "_restart_in_flight", lambda: True)
+ monkeypatch.setattr(
+ restart_supervisor,
+ "schedule_restart",
+ lambda **kwargs: (_ for _ in ()).throw(AssertionError("must not overlap restart jobs")),
+ )
+
+ result = ui_server._schedule_service_restart_for_config_fallback()
+
+ assert result["ok"] is True
+ assert result["code"] == "restart_pending_after_in_progress"
+ assert result["restart"] == restart_status
+ pending = runtime.read_json(restart_supervisor._pending_restart_path())
+ assert pending["restart_job_id"] == "job-in-flight"
+ assert pending["trigger"] == "web-ui-config-pending"
+ assert pending["scope"] == "service"
+
+
def test_static_ui_assets_use_cache_headers(monkeypatch, tmp_path):
ui_dist = tmp_path / "dist"
assets_dir = ui_dist / "assets"
diff --git a/vibe/restart_supervisor.py b/vibe/restart_supervisor.py
index 93c5e0ced..371300b08 100644
--- a/vibe/restart_supervisor.py
+++ b/vibe/restart_supervisor.py
@@ -30,6 +30,46 @@ def _restart_log_path(job_id: str) -> Path:
return paths.get_logs_dir() / f"restart-{timestamp}-{job_id}.log"
+def _pending_restart_path() -> Path:
+ return paths.get_runtime_dir() / "pending_restart.json"
+
+
+def mark_pending_restart(
+ *,
+ trigger: str,
+ scope: str = "service",
+ reason: str = "restart_in_progress",
+ restart_job_id: str | None = None,
+) -> dict:
+ payload = {
+ "trigger": trigger,
+ "scope": scope,
+ "reason": reason,
+ "restart_job_id": restart_job_id,
+ "created_at": _now_iso(),
+ "created_at_epoch": time.time(),
+ }
+ path = _pending_restart_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ runtime.write_json(path, payload)
+ return payload
+
+
+def _consume_pending_restart_for_job(job_id: str) -> dict | None:
+ path = _pending_restart_path()
+ payload = runtime.read_json(path)
+ if not isinstance(payload, dict):
+ return None
+ restart_job_id = payload.get("restart_job_id")
+ if restart_job_id and restart_job_id != job_id:
+ return None
+ try:
+ path.unlink()
+ except OSError:
+ logger.debug("Failed to remove pending restart marker", exc_info=True)
+ return payload
+
+
def _prune_restart_logs(limit: int = _RESTART_LOG_RETENTION) -> None:
try:
logs = sorted(
@@ -340,6 +380,24 @@ def _run_restart_job(
finally:
mark_duration("prepare_show_runtime_seconds", prepare_started_at)
+ pending_restart = _consume_pending_restart_for_job(job_id)
+ if pending_restart is not None:
+ write(
+ "scheduling pending follow-up restart "
+ f"trigger={pending_restart.get('trigger')!r} scope={pending_restart.get('scope')!r}"
+ )
+ try:
+ schedule_restart(
+ delay_seconds=0.0,
+ vibe_path=vibe_path,
+ trigger=str(pending_restart.get("trigger") or "pending-restart"),
+ scope=str(pending_restart.get("scope") or "service"),
+ )
+ except Exception as exc:
+ payload["pending_restart"] = {"scheduled": False, "error": str(exc)}
+ _write_status(payload)
+ write(f"failed to schedule pending follow-up restart: {exc}")
+
return 0
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index 4696e80a5..9368da55a 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -2231,11 +2231,23 @@ def _restart_in_flight() -> bool:
def _schedule_service_restart_for_config_fallback() -> dict[str, Any]:
from vibe import runtime
- from vibe.restart_supervisor import schedule_restart
+ from vibe.restart_supervisor import mark_pending_restart, schedule_restart
with _RESTART_CONTROL_LOCK:
+ restart_status = runtime.read_json(runtime.get_restart_status_path()) or {}
if _restart_in_flight():
- return {"ok": False, "code": "restart_in_progress", "error": "a restart is already in progress"}
+ pending = mark_pending_restart(
+ trigger="web-ui-config-pending",
+ scope="service",
+ reason="restart_in_progress",
+ restart_job_id=restart_status.get("job_id"),
+ )
+ return {
+ "ok": True,
+ "pending_restart": pending,
+ "restart": restart_status,
+ "code": "restart_pending_after_in_progress",
+ }
status = runtime.read_status()
runtime.write_status("restarting", "restarting", status.get("service_pid"), status.get("ui_pid"))
restart = schedule_restart(delay_seconds=0.0, trigger="web-ui-config", scope="service")