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(remote-access): preserve and refresh connector state

cyhee4aa0ab35d9
diff --git a/tests/test_remote_access_vibe_cloud.py b/tests/test_remote_access_vibe_cloud.py
index 2cec4782d..f1b995f39 100644
--- a/tests/test_remote_access_vibe_cloud.py
+++ b/tests/test_remote_access_vibe_cloud.py
@@ -104,3 +104,70 @@ def test_cloudflared_pid_detection_handles_quoted_paths_with_spaces(monkeypatch)
)
assert remote_access._is_cloudflared_pid(123) is True
+
+
+def test_stop_preserves_pid_file_when_process_stop_fails(monkeypatch, tmp_path) -> None:
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ pid = 123
+ remote_access._pid_path().parent.mkdir(parents=True, exist_ok=True)
+ remote_access._pid_path().write_text(str(pid), encoding="utf-8")
+ remote_access._state_path().write_text('{"pid": 123}', encoding="utf-8")
+
+ monkeypatch.setattr(remote_access, "_is_cloudflared_pid", lambda candidate: candidate == pid)
+ monkeypatch.setattr(runtime, "stop_pid", lambda candidate, timeout=8: False)
+
+ result = remote_access.stop()
+
+ assert result["ok"] is False
+ assert result["error"] == "cloudflared_stop_failed"
+ assert remote_access._pid_path().read_text(encoding="utf-8") == str(pid)
+ assert remote_access._state_path().exists()
+
+
+def test_start_restarts_when_runtime_signature_changes(monkeypatch, tmp_path) -> None:
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ config = _config()
+ config.remote_access.vibe_cloud.tunnel_token = "new-token"
+ binary = "/usr/local/bin/cloudflared"
+ old_pid = 111
+ new_pid = 222
+ remote_access._pid_path().parent.mkdir(parents=True, exist_ok=True)
+ remote_access._pid_path().write_text(str(old_pid), encoding="utf-8")
+ remote_access._state_path().write_text(
+ json.dumps(
+ {
+ "pid": old_pid,
+ "provider": "vibe_cloud",
+ "binary_path": binary,
+ "public_url": "https://alex.vibe.io",
+ "tunnel_token_sha256": "old-token-hash",
+ }
+ ),
+ encoding="utf-8",
+ )
+ alive = {old_pid, new_pid}
+
+ monkeypatch.setattr(remote_access, "_resolve_binary", lambda cfg: binary)
+ monkeypatch.setattr(remote_access, "_version", lambda path: "cloudflared test")
+ monkeypatch.setattr(runtime, "pid_alive", lambda pid: pid in alive)
+ monkeypatch.setattr(runtime, "get_process_command", lambda pid: f"{binary} tunnel run")
+
+ def stop_pid(pid, timeout=8):
+ alive.discard(pid)
+ return True
+
+ monkeypatch.setattr(runtime, "stop_pid", stop_pid)
+ def spawn_background(args, pid_path, stdout_name, stderr_name, env=None):
+ pid_path.write_text(str(new_pid), encoding="utf-8")
+ return new_pid
+
+ monkeypatch.setattr(runtime, "spawn_background", spawn_background)
+
+ result = remote_access.start(config)
+ state = json.loads(remote_access._state_path().read_text(encoding="utf-8"))
+
+ assert result["ok"] is True
+ assert result["started"] is True
+ assert result["pid"] == new_pid
+ assert old_pid not in alive
+ assert state["tunnel_token_sha256"] == "348e9df2a42bd6e3c6356ca9c95c5f1fe9a6b3e5cd25f4ae58df0f09049c3209"
diff --git a/vibe/remote_access.py b/vibe/remote_access.py
index 7f89da2b3..821a13b36 100644
--- a/vibe/remote_access.py
+++ b/vibe/remote_access.py
@@ -168,20 +168,39 @@ def _is_cloudflared_pid(pid: int | None) -> bool:
def _write_state(pid: int, config: V2Config, binary: str) -> None:
+ _state_path().write_text(json.dumps({"pid": pid, **_runtime_signature(config, binary)}, indent=2), encoding="utf-8")
+
+
+def _runtime_signature(config: V2Config, binary: str) -> dict[str, str]:
cloud = config.remote_access.vibe_cloud
- _state_path().write_text(
- json.dumps(
- {
- "pid": pid,
- "provider": "vibe_cloud",
- "binary_path": binary,
- "public_url": cloud.public_url,
- "tunnel_token_sha256": hashlib.sha256((cloud.tunnel_token or "").encode("utf-8")).hexdigest(),
- },
- indent=2,
- ),
- encoding="utf-8",
- )
+ return {
+ "provider": "vibe_cloud",
+ "binary_path": binary,
+ "public_url": cloud.public_url,
+ "tunnel_token_sha256": hashlib.sha256((cloud.tunnel_token or "").encode("utf-8")).hexdigest(),
+ }
+
+
+def _read_state() -> dict[str, Any] | None:
+ try:
+ payload = json.loads(_state_path().read_text(encoding="utf-8"))
+ except Exception:
+ return None
+ return payload if isinstance(payload, dict) else None
+
+
+def _running_signature(pid: int | None) -> dict[str, str] | None:
+ if not _is_cloudflared_pid(pid):
+ return None
+ state = _read_state()
+ if state is None or state.get("pid") != pid:
+ return None
+ return {
+ "provider": str(state.get("provider") or ""),
+ "binary_path": str(state.get("binary_path") or ""),
+ "public_url": str(state.get("public_url") or ""),
+ "tunnel_token_sha256": str(state.get("tunnel_token_sha256") or ""),
+ }
def status(config: V2Config | None = None) -> dict[str, Any]:
@@ -217,8 +236,9 @@ def stop() -> dict[str, Any]:
_pid_path().unlink(missing_ok=True)
_state_path().unlink(missing_ok=True)
return {**status(), "ok": True, "stopped": False, "stale_pid": True}
- stopped = runtime.stop_process(_pid_path(), timeout=8)
+ stopped = runtime.stop_pid(pid, timeout=8) if pid is not None else False
if stopped:
+ _pid_path().unlink(missing_ok=True)
_state_path().unlink(missing_ok=True)
if pid is not None and not stopped and _is_cloudflared_pid(pid):
return {**status(), "ok": False, "error": "cloudflared_stop_failed", "stopped": False}
@@ -242,7 +262,18 @@ def start(config: V2Config | None = None) -> dict[str, Any]:
binary = str(install_result["path"])
current = status(config)
if current.get("running"):
- return {**current, "ok": True, "started": False}
+ running_sig = _running_signature(current.get("pid"))
+ desired_sig = _runtime_signature(config, binary)
+ if running_sig == desired_sig:
+ return {**current, "ok": True, "started": False}
+ stop_result = stop()
+ if stop_result.get("ok") is False or stop_result.get("running"):
+ return {
+ **stop_result,
+ "ok": False,
+ "error": stop_result.get("error") or "cloudflared_stop_failed",
+ "restarted": False,
+ }
env = {**os.environ, "TUNNEL_TOKEN": cloud.tunnel_token}
try:
pid = runtime.spawn_background(