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): align localhost bind/origin to actual loopback family

cyh18a6f215cb08
diff --git a/tests/test_remote_access_vibe_cloud.py b/tests/test_remote_access_vibe_cloud.py
index ffd900792..7de4a7760 100644
--- a/tests/test_remote_access_vibe_cloud.py
+++ b/tests/test_remote_access_vibe_cloud.py
@@ -118,8 +118,11 @@ def test_pair_origin_service_ignores_configured_ui_host(monkeypatch, tmp_path) -
assert remote_access.origin_service_for_pairing() == "http://127.0.0.1:15130"
-def test_pair_origin_service_normalizes_localhost_to_loopback_ipv4(monkeypatch, tmp_path) -> None:
+def test_pair_origin_service_uses_ipv4_loopback_when_localhost_resolves_dual_stack(
+ monkeypatch, tmp_path
+) -> None:
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ monkeypatch.setattr(runtime, "resolve_localhost_family", lambda: "inet")
config = _config()
config.ui.setup_host = "localhost"
config.ui.setup_port = 15130
@@ -131,6 +134,22 @@ def test_pair_origin_service_normalizes_localhost_to_loopback_ipv4(monkeypatch,
assert remote_access.origin_service_for_pairing() == "http://127.0.0.1:15130"
+def test_pair_origin_service_uses_ipv6_loopback_when_localhost_resolves_v6_only(
+ monkeypatch, tmp_path
+) -> None:
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ monkeypatch.setattr(runtime, "resolve_localhost_family", lambda: "inet6")
+ config = _config()
+ config.ui.setup_host = "localhost"
+ config.ui.setup_port = 15130
+ config.save()
+
+ # On IPv6-only hosts where ``localhost`` only resolves to ::1, the
+ # cloudflared origin must follow into v6 so it can reach the v6
+ # wildcard bind. Otherwise the tunnel dials an unreachable v4 socket.
+ assert remote_access.origin_service_for_pairing() == "http://[::1]:15130"
+
+
def test_pair_origin_service_preserves_ipv6_loopback(monkeypatch, tmp_path) -> None:
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
config = _config()
@@ -927,11 +946,61 @@ def test_effective_ui_bind_host_requested_host_yields_to_tunnel_override() -> No
assert runtime.effective_ui_bind_host(config, requested_host="100.97.103.112") == "0.0.0.0"
-def test_effective_ui_bind_host_overrides_to_ipv4_wildcard_when_setup_host_is_localhost() -> None:
+def test_effective_ui_bind_host_overrides_to_ipv4_wildcard_when_localhost_resolves_dual_stack(
+ monkeypatch,
+) -> None:
# Pairs with _origin_host_for_pairing returning 127.0.0.1 for "localhost":
# the bind family must be IPv4 so cloudflared can reach the UI.
+ monkeypatch.setattr(runtime, "resolve_localhost_family", lambda: "inet")
config = _config()
assert config.remote_access.vibe_cloud.enabled is True
config.ui.setup_host = "localhost"
assert runtime.effective_ui_bind_host(config) == "0.0.0.0"
+
+
+def test_effective_ui_bind_host_overrides_to_ipv6_wildcard_when_localhost_resolves_v6_only(
+ monkeypatch,
+) -> None:
+ # On IPv6-only hosts where "localhost" only resolves to ::1, forcing
+ # IPv4 would unbind the UI from the only loopback the OS exposes;
+ # follow the same family the cloudflared origin will use.
+ monkeypatch.setattr(runtime, "resolve_localhost_family", lambda: "inet6")
+ config = _config()
+ assert config.remote_access.vibe_cloud.enabled is True
+ config.ui.setup_host = "localhost"
+
+ assert runtime.effective_ui_bind_host(config) == "::"
+
+
+def test_resolve_localhost_family_prefers_ipv4_when_both_resolve(monkeypatch) -> None:
+ import socket as _socket
+
+ def fake_getaddrinfo(host, port, *, type=None): # noqa: A002 - shadowing matches stdlib
+ return [
+ (_socket.AF_INET6, _socket.SOCK_STREAM, 0, "", ("::1", 0, 0, 0)),
+ (_socket.AF_INET, _socket.SOCK_STREAM, 0, "", ("127.0.0.1", 0)),
+ ]
+
+ monkeypatch.setattr("vibe.runtime.socket.getaddrinfo", fake_getaddrinfo)
+ assert runtime.resolve_localhost_family() == "inet"
+
+
+def test_resolve_localhost_family_returns_inet6_when_only_v6_resolves(monkeypatch) -> None:
+ import socket as _socket
+
+ def fake_getaddrinfo(host, port, *, type=None): # noqa: A002
+ return [(_socket.AF_INET6, _socket.SOCK_STREAM, 0, "", ("::1", 0, 0, 0))]
+
+ monkeypatch.setattr("vibe.runtime.socket.getaddrinfo", fake_getaddrinfo)
+ assert runtime.resolve_localhost_family() == "inet6"
+
+
+def test_resolve_localhost_family_falls_back_to_inet_on_resolution_failure(monkeypatch) -> None:
+ import socket as _socket
+
+ def fake_getaddrinfo(host, port, *, type=None): # noqa: A002
+ raise _socket.gaierror("simulated")
+
+ monkeypatch.setattr("vibe.runtime.socket.getaddrinfo", fake_getaddrinfo)
+ assert runtime.resolve_localhost_family() == "inet"
diff --git a/vibe/remote_access.py b/vibe/remote_access.py
index 4d6d20bb9..1fe842eba 100644
--- a/vibe/remote_access.py
+++ b/vibe/remote_access.py
@@ -600,12 +600,14 @@ def _origin_host_for_pairing(config: V2Config) -> str:
host = (config.ui.setup_host or "").strip()
if host.startswith("[") and host.endswith("]"):
host = host[1:-1].strip()
- # cloudflared and werkzeug each resolve "localhost" independently; on a
- # dual-stack host they can land on different families (e.g. ::1 vs
- # 127.0.0.1) and surface as a 502. Hand cloudflared a literal IPv4
- # loopback so the origin family always matches the bind family.
+ # "localhost" is ambiguous: cloudflared and werkzeug resolve it
+ # independently, so on a dual-stack host they can land on different
+ # families (::1 vs 127.0.0.1) and surface as a 502. Hand cloudflared
+ # a literal loopback IP whose family matches the wildcard
+ # ``effective_ui_bind_host`` chooses, so the two sides never disagree
+ # and IPv6-only hosts still work.
if host.lower() == "localhost":
- return "127.0.0.1"
+ return "[::1]" if runtime.resolve_localhost_family() == "inet6" else "127.0.0.1"
try:
address = ipaddress.ip_address(host)
diff --git a/vibe/runtime.py b/vibe/runtime.py
index 86c7801a2..c482b48d8 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -3,6 +3,7 @@ import logging
import os
import signal
import shlex
+import socket
import subprocess
import sys
import threading
@@ -398,6 +399,29 @@ def start_service():
)
+def resolve_localhost_family() -> str:
+ """Return the loopback family ``localhost`` actually maps to on this host.
+
+ ``"inet"`` when IPv4 loopback resolves (the common dual-stack case),
+ ``"inet6"`` only when ``localhost`` is exclusively IPv6. Used by
+ ``effective_ui_bind_host`` and ``_origin_host_for_pairing`` so the
+ bind family and the cloudflared origin family stay aligned: forcing
+ IPv4 unconditionally would regress IPv6-only hosts, while leaving
+ resolution to werkzeug + cloudflared independently re-creates the
+ ::1 vs 127.0.0.1 race that surfaces as 502.
+ """
+ try:
+ infos = socket.getaddrinfo("localhost", None, type=socket.SOCK_STREAM)
+ except socket.gaierror:
+ return "inet"
+ families = {info[0] for info in infos}
+ if socket.AF_INET in families:
+ return "inet"
+ if socket.AF_INET6 in families:
+ return "inet6"
+ return "inet"
+
+
def effective_ui_bind_host(config: V2Config, requested_host: str | None = None) -> str:
"""Resolve the host the UI server should bind to.
@@ -424,6 +448,12 @@ def effective_ui_bind_host(config: V2Config, requested_host: str | None = None)
normalized = setup_host.strip()
if normalized.startswith("[") and normalized.endswith("]"):
normalized = normalized[1:-1]
+ # "localhost" is ambiguous on dual-stack hosts and may even be
+ # exclusively IPv6. Resolve once and pick the wildcard that
+ # matches the family _origin_host_for_pairing will hand
+ # cloudflared, so the two sides cannot disagree.
+ if normalized.lower() == "localhost":
+ return "::" if resolve_localhost_family() == "inet6" else "0.0.0.0"
if normalized in {"::", "::0"} or ":" in normalized:
return "::"
return "0.0.0.0"