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.

test(conftest): refuse network access past loopback across the whole suite (#2542)

Call Whitea5c6cbf1737e
diff --git a/pyproject.toml b/pyproject.toml
index a6cd4bee2..a0f0bec4e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -177,6 +177,7 @@ markers = [
"reap_processes: kill every process the test started that is still running when it ends, and after a killed run (tests/process_reaper.py)",
"real_web_push_sender: opt out of the autouse stub that keeps the delayed Web Push sender from outliving a test, for tests that drive the sender directly",
"real_catalog_refresh: opt out of the autouse stubs that keep the remote model catalog and models.dev fetches off the network and off threads that outlive a test, for tests that drive a refresh or fetch directly",
+ "allow_network(reason): opt an integration test that is meant to be live out of the suite-wide refusal of network access past loopback (tests/network_guard.py; state the reason)",
]
[dependency-groups]
diff --git a/tests/conftest.py b/tests/conftest.py
index 4c0de68c3..de7c41900 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -72,7 +72,7 @@ import psutil
import pytest
from sqlalchemy.exc import SAWarning
-from tests import process_reaper
+from tests import network_guard, process_reaper
from tests.fake_pid_helpers import PID_LIMIT
REAL_USER_HOME = Path.home()
@@ -207,9 +207,25 @@ def _record_test_processes(request):
yield
[email protected](tryfirst=True)
+def pytest_runtest_setup(item):
+ """Open the test's network window before any fixture it needs is set up (tests/network_guard.py)."""
+
+ marker = item.get_closest_marker("allow_network")
+ if marker is not None and not (marker.kwargs.get("reason") or marker.args):
+ pytest.fail("@pytest.mark.allow_network needs a reason")
+ if marker is not None and item.get_closest_marker("integration") is None:
+ # The opt-out lifts the guard process-wide, so it may open only in a run the unit suite is not part of.
+ pytest.fail("@pytest.mark.allow_network is honoured only on an integration test")
+ network_guard.begin_test(item.nodeid, allowed=marker is not None)
+
+
@pytest.hookimpl(wrapper=True)
def pytest_runtest_teardown(item):
- """Reap after every finalizer of the test has run, so a process a fixture stops is gone first."""
+ """Reap after every finalizer of the test has run, so a process a fixture stops is gone first.
+
+ The network window closes at the same point, so a refusal in any finalizer is the test's too.
+ """
global _active_test_processes
try:
@@ -219,6 +235,9 @@ def pytest_runtest_teardown(item):
processes = item.stash.get(_TEST_PROCESSES, None)
if processes is not None:
_reaped_processes.extend(f"{item.nodeid}: {each}" for each in processes.reap())
+ refused = network_guard.end_test()
+ if refused:
+ pytest.fail("refused network access past loopback:\n" + "\n".join(refused), pytrace=False)
@pytest.fixture
@@ -1063,9 +1082,12 @@ def _real_home_tripwire(event: str, args: tuple) -> None:
sys.addaudithook(_real_home_tripwire)
+# Installed here, before any product module is imported, for the same reasons.
+sys.addaudithook(network_guard.audit)
_REPORTED_WRITES = pytest.StashKey[int]()
+_REPORTED_NETWORK_REFUSALS = pytest.StashKey[int]()
_running_session: pytest.Session | None = None
@@ -1094,6 +1116,12 @@ def pytest_terminal_summary(terminalreporter) -> None:
terminalreporter.section("writes under the real Avibe home were refused", red=True)
for message in written:
terminalreporter.line(message, red=True)
+ refused = list(network_guard.refused_outside_tests)
+ terminalreporter.config.stash[_REPORTED_NETWORK_REFUSALS] = len(refused)
+ if refused:
+ terminalreporter.section("network access past loopback was refused outside any test", red=True)
+ for message in refused:
+ terminalreporter.line(message, red=True)
@pytest.hookimpl(trylast=True)
@@ -1107,7 +1135,9 @@ def pytest_unconfigure(config: pytest.Config) -> None:
shutil.rmtree(_SESSION_HOME, ignore_errors=True)
for message in _real_home_writes[config.stash.get(_REPORTED_WRITES, 0) :]:
sys.stderr.write(f"refused a write under the real Avibe home: {message}\n")
- if _real_home_writes:
+ for message in network_guard.refused_outside_tests[config.stash.get(_REPORTED_NETWORK_REFUSALS, 0) :]:
+ sys.stderr.write(f"refused network access past loopback outside any test: {message}\n")
+ if _real_home_writes or network_guard.refused_outside_tests:
_fail_running_session()
diff --git a/tests/e2e/integration_cases.py b/tests/e2e/integration_cases.py
index 74b2198a1..5ff86166a 100644
--- a/tests/e2e/integration_cases.py
+++ b/tests/e2e/integration_cases.py
@@ -7,6 +7,7 @@ This avoids duplicating test logic across slack/discord/feishu.
import pytest
[email protected]_network(reason="drives the real Slack, Discord and Feishu APIs with platform tokens")
class PlatformIntegrationTests:
"""Mixin class with shared integration test cases.
diff --git a/tests/network_guard.py b/tests/network_guard.py
new file mode 100644
index 000000000..fc62c258d
--- /dev/null
+++ b/tests/network_guard.py
@@ -0,0 +1,202 @@
+"""No test reaches past this machine's loopback, however the request is made.
+
+Two leaks turned CI shards red in one day: an update-checker test downloaded
+askill from GitHub (#2531), and a Model Hub test that seeded Vibey with no
+cached models.dev copy fetched the catalog, where an SSL read hung until the
+file's watchdog killed it. Neither test meant to touch the network; each
+reached it through product code nothing had stubbed.
+
+``audit`` is a CPython audit hook (``tests/conftest.py`` installs it before
+any product module is imported). It sees the socket module's own events,
+raised in C, so it covers every client that ends in a Python socket --
+``urllib``, ``http.client``, ``requests``/``urllib3``, ``httpx`` and
+``aiohttp`` on the standard asyncio loop -- in every thread, whatever name the
+caller imported, and between tests. It refuses:
+
+- ``socket.connect`` (``connect`` and ``connect_ex``) and ``socket.sendto`` /
+ ``socket.sendmsg`` to an IPv4 or IPv6 address that is not loopback or
+ unspecified, or to a host name other than ``localhost``;
+- ``socket.getaddrinfo`` and ``socket.gethostbyname`` for a host name other
+ than ``localhost``, so a lookup fails before it queries DNS and the refusal
+ names the host rather than the address it would have resolved to. IP
+ literals resolve without a query and are left to the connect check;
+- ``socket.gethostbyaddr``, a reverse lookup, of anything but loopback, an
+ unspecified address included.
+
+Unix sockets and every other address family pass. A refusal raises
+``NetworkAccessRefused``, an ``OSError``, so the product takes its offline path
+instead of hanging, and is recorded: a refusal the product swallows still
+fails the test that made it (``take_refusals``), and one made outside any
+test fails the run.
+
+``allow_network(reason=...)`` opts out a test that is meant to be live. It is
+honoured only on an ``integration`` test, which the unit shards exclude, because
+the opt-out lifts the guard for the whole process while that test runs: every
+thread, including one an earlier test left behind. The opt-out spans the test's
+whole run protocol, including the session-scoped fixtures it sets up.
+
+Outside the hook: child processes (each loads its own interpreter), a native
+resolver or transport that bypasses CPython's socket module, such as uvloop's
+or c-ares's, and ``socket.getnameinfo``, whose audit event omits the flags
+that say whether it queries DNS.
+"""
+
+from __future__ import annotations
+
+import errno
+import ipaddress
+import socket
+import threading
+import traceback
+from dataclasses import dataclass, field
+from pathlib import Path
+
+_REPO_ROOT = str(Path(__file__).resolve().parents[1])
+_THIS_FILE = str(Path(__file__).resolve())
+_INET_FAMILIES = frozenset({socket.AF_INET, socket.AF_INET6})
+_LOOKUP_EVENTS = frozenset({"socket.getaddrinfo", "socket.gethostbyname", "socket.gethostbyaddr"})
+_SEND_EVENTS = frozenset({"socket.connect", "socket.sendto", "socket.sendmsg"})
+_EVENTS = _LOOKUP_EVENTS | _SEND_EVENTS
+_STACK_FRAMES = 6
+
+
+class NetworkAccessRefused(OSError):
+ """A test tried to reach past this machine's loopback."""
+
+
+@dataclass
+class _Window:
+ """One test's run protocol: whether it may reach the network, and what was refused."""
+
+ nodeid: str
+ allowed: bool
+ refusals: list[str] = field(default_factory=list)
+
+
+_current: _Window | None = None
+# Refusals made while no test was running, in order: collection, module import,
+# or a thread that outlived the test that started it.
+refused_outside_tests: list[str] = []
+# Recording a refusal and closing a window are atomic with respect to each other,
+# so a refusal lands either in the window its test still reports or outside any test.
+_lock = threading.Lock()
+
+
+def begin_test(nodeid: str, *, allowed: bool) -> None:
+ global _current
+ with _lock:
+ _current = _Window(nodeid, allowed)
+
+
+def end_test() -> list[str]:
+ """Close the running test's window; what it was refused."""
+
+ global _current
+ with _lock:
+ window, _current = _current, None
+ return window.refusals if window is not None else []
+
+
+def take_refusals() -> list[str]:
+ """Return and forget what the running test was refused, for a test that expects refusals."""
+
+ with _lock:
+ window = _current
+ if window is None:
+ return []
+ taken, window.refusals[:] = list(window.refusals), []
+ return taken
+
+
+def _is_local_name(host: str) -> bool:
+ return host.lower().rstrip(".") == "localhost"
+
+
+def _ip(host: str) -> ipaddress.IPv4Address | ipaddress.IPv6Address | None:
+ try:
+ address = ipaddress.ip_address(host.partition("%")[0]) # an IPv6 zone index names no host
+ except ValueError:
+ return None
+ if isinstance(address, ipaddress.IPv6Address) and address.ipv4_mapped is not None:
+ return address.ipv4_mapped
+ return address
+
+
+def _host_text(host) -> str | None:
+ if isinstance(host, bytes):
+ return host.decode("ascii", "replace")
+ return host if isinstance(host, str) else None
+
+
+def _is_remote(host: str) -> bool:
+ ip = _ip(host)
+ if ip is None:
+ return not _is_local_name(host)
+ return not (ip.is_loopback or ip.is_unspecified)
+
+
+def _refused_target(event: str, args: tuple) -> str | None:
+ """What ``event`` would reach past loopback, as the caller named it, or None."""
+
+ if event in _LOOKUP_EVENTS:
+ host = _host_text(args[0])
+ if not host:
+ return None
+ ip = _ip(host)
+ if event == "socket.gethostbyaddr":
+ # A reverse lookup asks DNS about any address but loopback, an unspecified one included.
+ local = ip.is_loopback if ip is not None else _is_local_name(host)
+ else:
+ # A forward lookup of an IP literal sends no query.
+ local = ip is not None or _is_local_name(host)
+ if local:
+ return None
+ return f"{host}:{args[1]}" if event == "socket.getaddrinfo" and args[1] is not None else host
+ sock, address = args[0], args[-1]
+ if sock.family not in _INET_FAMILIES or not isinstance(address, tuple) or not address:
+ return None
+ host = _host_text(address[0])
+ if host is None or not _is_remote(host):
+ return None
+ port = address[1] if len(address) > 1 else "?"
+ return f"[{host}]:{port}" if ":" in host else f"{host}:{port}"
+
+
+def _origin() -> str:
+ """The innermost repository frames that led here, so the report names the call site."""
+
+ frames = [
+ frame
+ for frame in traceback.extract_stack()
+ if frame.filename.startswith(_REPO_ROOT)
+ and frame.filename != _THIS_FILE
+ and "site-packages" not in frame.filename # a virtualenv inside the checkout
+ ][-_STACK_FRAMES:]
+ return " <- ".join(
+ f"{frame.filename[len(_REPO_ROOT) + 1 :]}:{frame.lineno} {frame.name}" for frame in reversed(frames)
+ )
+
+
+def audit(event: str, args: tuple) -> None:
+ if event not in _EVENTS:
+ return
+ window = _current
+ if window is not None and window.allowed:
+ return
+ target = _refused_target(event, args)
+ if target is None:
+ return
+ # A lookup the event loop hands to its executor carries no frame of the code that asked for it.
+ origin = _origin() or "no repository frame on this thread"
+ message = f"{event} to {target} (thread {threading.current_thread().name}) from {origin}"
+ with _lock:
+ # Read again: the window seen above may have closed while the stack was rendered.
+ window = _current
+ if window is not None and window.allowed:
+ return
+ (window.refusals if window is not None else refused_outside_tests).append(message)
+ raise NetworkAccessRefused(
+ errno.ENETUNREACH,
+ f"tests may not reach past loopback: {event} to {target}; stub the fetch at the boundary that owns it, "
+ "or mark a test that is meant to be live with allow_network(reason=...)",
+ )
diff --git a/tests/scenarios/permissions/test_deferred_authority_provenance.py b/tests/scenarios/permissions/test_deferred_authority_provenance.py
index 828cd5a4c..ac431df27 100644
--- a/tests/scenarios/permissions/test_deferred_authority_provenance.py
+++ b/tests/scenarios/permissions/test_deferred_authority_provenance.py
@@ -114,7 +114,7 @@ def _bind_project(conn, project_id: str, *, revision: int, emails: list[str]) ->
@pytest.fixture
-def turn_targets(tmp_path):
+def turn_targets(tmp_path, monkeypatch):
"""One real Agent on two real sessions: one this Editor may drive, one they may not.
Both rows are written the way the product writes them — through
@@ -125,13 +125,14 @@ def turn_targets(tmp_path):
from core.vibe_agents import VibeAgentStore
from storage import projects_service, resource_access_service
from storage.workbench_sessions_service import create_session
- from tests.ui_server_test_helpers import _save_config
+ from tests.ui_server_test_helpers import _save_config, permissions_backend_unreachable
# A stored snapshot is re-decided against the current pairing, so the
# execution guard only answers at all on a paired installation whose durable
# binding exists — the interactive path bootstraps it, exactly like this.
config = _save_config(tmp_path, paired=True, instance_kind="organization")
ensure_sqlite_state()
+ permissions_backend_unreachable(monkeypatch)
from vibe import remote_access
diff --git a/tests/scenarios/permissions/test_member_instance_operations.py b/tests/scenarios/permissions/test_member_instance_operations.py
index c89e03d0b..d52c930ff 100644
--- a/tests/scenarios/permissions/test_member_instance_operations.py
+++ b/tests/scenarios/permissions/test_member_instance_operations.py
@@ -23,7 +23,13 @@ from storage.db import create_sqlite_engine
from storage.importer import ensure_sqlite_state
from storage.models import agent_sessions, messages, resource_access_policies, resource_access_groups
from storage.workbench_sessions_service import create_session
-from tests.ui_server_test_helpers import _save_config, csrf_headers, remote_peer, remote_session_cookie
+from tests.ui_server_test_helpers import (
+ _save_config,
+ csrf_headers,
+ permissions_backend_unreachable,
+ remote_peer,
+ remote_session_cookie,
+)
from vibe import internal_client, remote_access, ui_server
from vibe.authorization import INSTANCE_SCOPED_REFETCH_EVENTS, AuthorizationContext
from vibe.sse_broker import broker
@@ -37,6 +43,7 @@ def operations(request, tmp_path, monkeypatch):
config = _save_config(tmp_path, paired=True, instance_kind=request.param)
ensure_sqlite_state()
monkeypatch.setattr(ui_server, "_ensure_remote_access_monitoring", lambda *args: None)
+ permissions_backend_unreachable(monkeypatch)
store = VibeAgentStore()
store.ensure_builtin_default_agent(backend="codex")
agent = store.require("codex")
diff --git a/tests/test_conftest_network_guard.py b/tests/test_conftest_network_guard.py
new file mode 100644
index 000000000..4183591c8
--- /dev/null
+++ b/tests/test_conftest_network_guard.py
@@ -0,0 +1,309 @@
+"""No test reaches past loopback, whichever client it uses and whether or not it swallows the refusal.
+
+Two tests downloaded from GitHub and models.dev through product code nothing
+had stubbed (#2531), and an SSL read that hung killed a CI shard. The guard in
+``tests/network_guard.py`` refuses such a request, names the host, and fails
+the test that made it even when the product catches the refusal.
+
+The refused requests here use reserved names (``.invalid``) and documentation
+addresses (203.0.113.0/24, 2001:db8::/32), and the in-process checks use UDP
+``connect``, which sends nothing: a regressed guard still reaches no real host.
+"""
+
+from __future__ import annotations
+
+import shutil
+import socket
+import tempfile
+from pathlib import Path
+
+import pytest
+
+from tests import network_guard
+
+pytest_plugins = ("pytester",)
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+
+# Each request from a handler that swallows whatever it raises, as product code
+# with an offline fallback does; one from a daemon thread; one made while the
+# module is collected, outside any test; and an opt-out with no reason.
+_REFUSED_REQUESTS = """
+import asyncio
+import socket
+import threading
+import urllib.request
+
+import aiohttp
+import httpx
+import pytest
+import requests
+
+
+def _swallow(call):
+ try:
+ call()
+ except BaseException:
+ pass
+
+
+_swallow(lambda: socket.getaddrinfo("refused-at-collection.invalid", 443))
+
+
+def _connect(family, kind, address, use_connect_ex=False):
+ sock = socket.socket(family, kind)
+ try:
+ (sock.connect_ex if use_connect_ex else sock.connect)(address)
+ finally:
+ sock.close()
+
+
+def test_connect():
+ _swallow(lambda: _connect(socket.AF_INET, socket.SOCK_STREAM, ("203.0.113.7", 443)))
+
+
+def test_connect_ex():
+ _swallow(lambda: _connect(socket.AF_INET, socket.SOCK_STREAM, ("203.0.113.8", 443), use_connect_ex=True))
+
+
+def test_ipv6_connect():
+ _swallow(lambda: _connect(socket.AF_INET6, socket.SOCK_STREAM, ("2001:db8::7", 443)))
+
+
+def test_udp_sendto():
+ def send():
+ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
+ sock.sendto(b"x", ("203.0.113.9", 53))
+
+ _swallow(send)
+
+
+def test_reverse_lookup():
+ _swallow(lambda: socket.gethostbyaddr("203.0.113.10"))
+
+
+def test_create_connection():
+ _swallow(lambda: socket.create_connection(("refused-create-connection.invalid", 443), timeout=5))
+
+
+def test_urllib():
+ _swallow(lambda: urllib.request.urlopen("https://refused-urllib.invalid/", timeout=5))
+
+
+def test_requests():
+ _swallow(lambda: requests.get("https://refused-requests.invalid/", timeout=5))
+
+
+def test_httpx():
+ _swallow(lambda: httpx.get("https://refused-httpx.invalid/", timeout=5))
+
+
+def test_httpx_async():
+ async def fetch():
+ async with httpx.AsyncClient(timeout=5) as client:
+ await client.get("https://refused-httpx-async.invalid/")
+
+ _swallow(lambda: asyncio.run(fetch()))
+
+
+def test_aiohttp():
+ async def fetch():
+ async with aiohttp.ClientSession() as session:
+ await session.get("https://refused-aiohttp.invalid/")
+
+ _swallow(lambda: asyncio.run(fetch()))
+
+
+def test_asyncio_ip_literal():
+ _swallow(lambda: asyncio.run(asyncio.open_connection("203.0.113.11", 443)))
+
+
+def test_daemon_thread():
+ thread = threading.Thread(
+ target=_swallow,
+ args=(lambda: urllib.request.urlopen("https://refused-thread.invalid/", timeout=5),),
+ daemon=True,
+ )
+ thread.start()
+ thread.join(30)
+
+
+def test_opt_out_without_a_reason():
+ pass
+"""
+
+_REFUSED_TARGETS = {
+ "test_connect": "socket.connect to 203.0.113.7:443",
+ "test_connect_ex": "socket.connect to 203.0.113.8:443",
+ "test_ipv6_connect": "socket.connect to [2001:db8::7]:443",
+ "test_udp_sendto": "socket.sendto to 203.0.113.9:53",
+ "test_reverse_lookup": "socket.gethostbyaddr to 203.0.113.10",
+ "test_create_connection": "socket.getaddrinfo to refused-create-connection.invalid:443",
+ "test_urllib": "socket.getaddrinfo to refused-urllib.invalid:443",
+ "test_requests": "socket.getaddrinfo to refused-requests.invalid:443",
+ "test_httpx": "socket.getaddrinfo to refused-httpx.invalid:443",
+ "test_httpx_async": "socket.getaddrinfo to refused-httpx-async.invalid:443",
+ "test_aiohttp": "socket.getaddrinfo to refused-aiohttp.invalid:443",
+ "test_asyncio_ip_literal": "socket.connect to 203.0.113.11:443",
+ "test_daemon_thread": "socket.getaddrinfo to refused-thread.invalid:443",
+}
+
+
+def test_a_swallowed_request_past_loopback_fails_its_test_naming_the_host(
+ pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setenv("PYTHONPATH", str(REPO_ROOT))
+ pytester.makepyfile(test_inner=_REFUSED_REQUESTS)
+ result = pytester.runpytest_subprocess("-p", "tests.conftest", "-p", "no:cacheprovider", timeout=120)
+
+ output = result.stdout.str()
+ # Each test body passes; the refusal it swallowed fails it at teardown.
+ result.assert_outcomes(passed=len(_REFUSED_TARGETS), errors=len(_REFUSED_TARGETS) + 1)
+ for test, target in _REFUSED_TARGETS.items():
+ section = output.partition(f" ERROR at teardown of {test} ")[2].partition(" ERROR at ")[0]
+ assert f"refused network access past loopback:\n{target} (thread " in section, (test, output)
+ section = output.partition(" ERROR at setup of test_opt_out_without_a_reason ")[2]
+ assert "Failed: @pytest.mark.allow_network needs a reason" in section, output
+ # Collection is outside any test: the run fails on its own account.
+ assert "network access past loopback was refused outside any test" in output
+ assert "socket.getaddrinfo to refused-at-collection.invalid:443" in output
+ assert result.ret == pytest.ExitCode.TESTS_FAILED
+
+
+ ("address", "refused"),
+ [
+ (("127.0.0.1", 9), False),
+ (("127.8.9.10", 9), False),
+ (("0.0.0.0", 9), False),
+ (("localhost", 9), False),
+ (("LOCALHOST", 9), False),
+ (("::1", 9), False),
+ (("::ffff:127.0.0.1", 9), False),
+ (("::", 9), False),
+ (("192.168.1.1", 9), True),
+ (("10.0.0.1", 9), True),
+ (("::ffff:203.0.113.12", 9), True),
+ (("2001:db8::12", 9), True),
+ ],
+)
+def test_only_loopback_and_unspecified_addresses_are_reachable(address: tuple[str, int], refused: bool) -> None:
+ family = socket.AF_INET6 if ":" in address[0] else socket.AF_INET
+ sock = socket.socket(family, socket.SOCK_DGRAM)
+ try:
+ if refused:
+ with pytest.raises(network_guard.NetworkAccessRefused, match="tests may not reach past loopback"):
+ sock.connect(address)
+ assert len(network_guard.take_refusals()) == 1
+ else:
+ sock.connect(address)
+ finally:
+ sock.close()
+
+
[email protected]("address", ["0.0.0.0", "::", "203.0.113.16", "refused-reverse.invalid"])
+def test_a_reverse_lookup_past_loopback_is_refused(address: str) -> None:
+ with pytest.raises(network_guard.NetworkAccessRefused, match="socket.gethostbyaddr"):
+ socket.gethostbyaddr(address)
+ assert len(network_guard.take_refusals()) == 1
+
+
+def test_a_forward_lookup_of_an_address_sends_no_query_and_passes() -> None:
+ assert socket.getaddrinfo("203.0.113.13", 443, type=socket.SOCK_STREAM)[0][4][:2] == ("203.0.113.13", 443)
+
+
+def test_loopback_and_unix_servers_are_reachable() -> None:
+ with socket.create_server(("127.0.0.1", 0)) as server:
+ port = server.getsockname()[1]
+ for host in ("127.0.0.1", "localhost"):
+ socket.create_connection((host, port), timeout=5).close()
+ if not hasattr(socket, "AF_UNIX"):
+ return
+ # AF_UNIX paths are limited to about 100 bytes, so not under tmp_path.
+ directory = tempfile.mkdtemp(prefix="ng-")
+ try:
+ path = f"{directory}/s"
+ with socket.socket(socket.AF_UNIX) as server, socket.socket(socket.AF_UNIX) as client:
+ server.bind(path)
+ server.listen()
+ client.connect(path)
+ finally:
+ shutil.rmtree(directory)
+
+
+# The opt-out, honoured only on an integration test; and a refusal whose report is
+# still being rendered when its test's window closes, which must not be lost.
+_WINDOW_EDGES = """
+import socket
+import threading
+
+import pytest
+
+from tests import network_guard
+
+
+def _udp_connect(host):
+ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
+ sock.connect((host, 9))
+
+
[email protected]_network(reason="proves the opt-out; a UDP connect sends nothing")
+def test_an_opted_out_integration_test():
+ _udp_connect("203.0.113.14")
+
+
[email protected]_network(reason="not an integration test")
+def test_an_opt_out_outside_integration():
+ pass
+
+
+_render = network_guard._origin
+_rendering = threading.Event()
+_finish_rendering = threading.Event()
+_threads = []
+
+
+def _slow_render():
+ _rendering.set()
+ _finish_rendering.wait(30)
+ return _render()
+
+
+def test_a_refusal_still_rendering_at_teardown():
+ network_guard._origin = _slow_render
+
+ def refuse():
+ try:
+ _udp_connect("203.0.113.15")
+ except OSError:
+ pass
+
+ _threads.append(threading.Thread(target=refuse, daemon=True))
+ _threads[0].start()
+ assert _rendering.wait(30)
+
+
+def test_the_next_test():
+ network_guard._origin = _render
+ _finish_rendering.set()
+ _threads[0].join(30)
+"""
+
+
+def test_the_opt_out_and_a_closing_window_lose_no_refusal(
+ pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setenv("PYTHONPATH", str(REPO_ROOT))
+ pytester.makepyfile(test_inner=_WINDOW_EDGES)
+ result = pytester.runpytest_subprocess("-p", "tests.conftest", "-p", "no:cacheprovider", timeout=120)
+
+ output = result.stdout.str()
+ result.assert_outcomes(passed=3, errors=2)
+ assert "203.0.113.14" not in output
+ section = output.partition(" ERROR at setup of test_an_opt_out_outside_integration ")[2]
+ assert "Failed: @pytest.mark.allow_network is honoured only on an integration test" in section, output
+ # Recorded once rendered, against the window open then, rather than into the closed one.
+ section = output.partition(" ERROR at teardown of test_the_next_test ")[2]
+ assert "socket.connect to 203.0.113.15:9 (thread " in section, output
diff --git a/tests/test_file_browser_backend.py b/tests/test_file_browser_backend.py
index c9d421ed5..09f3cfdc2 100644
--- a/tests/test_file_browser_backend.py
+++ b/tests/test_file_browser_backend.py
@@ -2505,6 +2505,7 @@ def test_startup_reconcile_skips_tmux_when_env_set(monkeypatch):
monkeypatch.setattr(api, "ensure_askill_installed", lambda force=False: {"ok": True, "installed": True})
monkeypatch.setattr(api, "ensure_avault_installed", lambda force=False: {"ok": True, "installed": True})
+ monkeypatch.setattr(api, "ensure_model_hub_engine_installed", lambda force=False: {"ok": True, "installed": True})
monkeypatch.setenv("VIBE_UI_ENABLE_TERMINAL", "1")
monkeypatch.setenv("VIBE_RIPGREP_OFFLINE", "1")
diff --git a/tests/test_local_deps.py b/tests/test_local_deps.py
index ad89d0427..20868f909 100644
--- a/tests/test_local_deps.py
+++ b/tests/test_local_deps.py
@@ -69,6 +69,14 @@ class _FakeHTTPResponse:
return self._body.read(size)
+def _stub_tmux_install(monkeypatch) -> None:
+ """Startup reconcile also ensures the managed tmux, which downloads it where none is installed."""
+
+ import core.tmux_runtime as tmux_runtime
+
+ monkeypatch.setattr(tmux_runtime, "ensure_tmux_installed", lambda force=False: {"ok": True, "installed": True})
+
+
def _serve_release_asset(monkeypatch, suffix: str, body: bytes) -> list[str]:
"""Serve ``body`` for any URL ending in ``suffix``; return the requested URLs."""
calls: list[str] = []
@@ -1740,6 +1748,7 @@ def test_dependencies_status_node_unsupported_not_ready(monkeypatch):
def test_reconcile_startup_dependencies_reports_runtime_install_failure_without_node(monkeypatch):
monkeypatch.setenv("VIBE_RIPGREP_OFFLINE", "1")
+ _stub_tmux_install(monkeypatch)
monkeypatch.setattr(api, "ensure_askill_installed", lambda force=False: {"ok": True, "installed": True})
monkeypatch.setattr(api, "ensure_avault_installed", lambda force=False: {"ok": True, "installed": True})
monkeypatch.setattr(
@@ -2302,6 +2311,7 @@ def test_reconcile_startup_dependencies_uses_automatic_runtime_admission(monkeyp
"""MH-RUNTIME-008: startup converges CPA without changing run intent."""
monkeypatch.setenv("VIBE_RIPGREP_OFFLINE", "1")
+ _stub_tmux_install(monkeypatch)
askill_calls = []
avault_calls = []
model_hub_calls = []
diff --git a/tests/test_remote_access_vibe_cloud.py b/tests/test_remote_access_vibe_cloud.py
index 006dc4e29..70de5464f 100644
--- a/tests/test_remote_access_vibe_cloud.py
+++ b/tests/test_remote_access_vibe_cloud.py
@@ -43,6 +43,17 @@ def _resolve_backend_test_to_public_address(monkeypatch):
monkeypatch.setattr(remote_access, "_resolve_pairing_backend_addresses", resolve)
[email protected](autouse=True)
+def _packaged_cloudflared_is_installed(monkeypatch):
+ """Starting the connector first installs a missing packaged pin, which downloads cloudflared.
+
+ The tests here resolve their connector binary themselves, as on a machine that
+ already holds the pin; ``tests/test_tunnel_recovery.py`` covers installing it.
+ """
+
+ monkeypatch.setattr(remote_access, "_cloudflared_pin_needed", lambda config=None: False)
+
+
def _config() -> V2Config:
config = V2Config(
mode="self_host",
@@ -264,8 +275,15 @@ def test_parse_session_cookie_returns_payload_for_fresh_token() -> None:
assert payload["vibe_instance_role"] == "owner"
-def test_parse_session_cookie_rejects_legacy_roleless_payload() -> None:
+def test_parse_session_cookie_rejects_legacy_roleless_payload(monkeypatch) -> None:
config = _config()
+
+ # A roleless cookie has no stored authorization, so only the control plane
+ # could vouch for it; here, as when it is unreachable, nothing does.
+ def control_plane_unreachable(*_args, **_kwargs):
+ raise RuntimeError("resource_acl_device_unavailable")
+
+ monkeypatch.setattr(remote_access, "_device_json_request", control_plane_unreachable)
issued_at = int(time.time())
payload = {
"email": "[email protected]",
diff --git a/tests/test_show_pages.py b/tests/test_show_pages.py
index bec305c80..c200e9929 100644
--- a/tests/test_show_pages.py
+++ b/tests/test_show_pages.py
@@ -46,7 +46,7 @@ def _stub_runtime_prepare_dependencies(
tmux_result=None,
git_result=None,
):
- calls = {"askill": [], "avault": [], "tmux": [], "git": []}
+ calls = {"askill": [], "avault": [], "tmux": [], "git": [], "model_hub_engine": []}
def fake_askill(offline=False, force=False):
calls["askill"].append({"offline": offline, "force": force})
@@ -64,11 +64,16 @@ def _stub_runtime_prepare_dependencies(
calls["git"].append({"offline": offline, "force": force})
return git_result or {"ok": True, "installed": True}
+ def fake_model_hub_engine(offline=False, force=False):
+ calls["model_hub_engine"].append({"offline": offline, "force": force})
+ return {"ok": True, "installed": True}
+
monkeypatch.setattr(cli, "_ensure_askill_during_prepare", fake_askill)
monkeypatch.setattr(cli, "_ensure_avault_during_prepare", fake_avault)
monkeypatch.setattr(cli, "_ensure_tmux_during_prepare", fake_tmux)
monkeypatch.setattr(cli, "_ensure_git_during_prepare", fake_git)
monkeypatch.setattr(cli, "_ensure_ripgrep_during_prepare", lambda **_kwargs: {"ok": True})
+ monkeypatch.setattr(cli, "_ensure_model_hub_engine_during_prepare", fake_model_hub_engine)
return calls
diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py
index 02f5f0f87..db606c560 100644
--- a/tests/test_telegram_bot.py
+++ b/tests/test_telegram_bot.py
@@ -8,6 +8,7 @@ from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
+import aiohttp
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
@@ -32,6 +33,21 @@ from config.v2_config import TelegramConfig
ROUTING_BACKENDS = list(AGENT_BACKENDS)
[email protected](autouse=True)
+def _telegram_api_unreachable(monkeypatch):
+ """Every Bot API call ends in ``telegram_api.call_api``, which posts to api.telegram.org.
+
+ A test that expects a call stubs that call; any other one fails the way it does
+ offline, such as the command-menu sync a started bot runs or the cleanup of an
+ interaction message.
+ """
+
+ async def unreachable(_bot_token, method, *_args, **_kwargs):
+ raise aiohttp.ClientConnectionError(f"tests do not reach api.telegram.org ({method})")
+
+ monkeypatch.setattr(telegram_api, "call_api", unreachable)
+
+
@pytest.fixture(autouse=True)
def _reset_chat_discovery_cache():
"""chat_discovery keeps a process-global debounce cache keyed on
diff --git a/tests/test_ui_show_pages.py b/tests/test_ui_show_pages.py
index 9f840295f..34c932f7f 100644
--- a/tests/test_ui_show_pages.py
+++ b/tests/test_ui_show_pages.py
@@ -75,7 +75,7 @@ from core.show_runtime_failures import (
show_runtime_recovery_action,
)
from storage import resource_access_service
-from tests.ui_server_test_helpers import _mock_interface, _remote_peer, _save_config
+from tests.ui_server_test_helpers import _mock_interface, _remote_peer, _save_config, permissions_backend_unreachable
from tests.ui_server_test_helpers import csrf_headers, remote_session_cookie
from vibe import remote_access, show_identity, ui_server
from vibe.ui_compat import TEST_REMOTE_ADDR_HEADER
@@ -7851,6 +7851,7 @@ def test_public_show_page_dispatch_records_the_visitor_that_admitted_it(monkeypa
monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
config = _save_config(tmp_path, paired=True)
+ permissions_backend_unreachable(monkeypatch)
_create_agent_session("ses123")
share_id = _create_show_page("ses123", "public")
published = []
@@ -7937,6 +7938,7 @@ def test_public_show_page_dispatch_fails_closed_without_a_resolved_visitor(monke
monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
config = _save_config(tmp_path, paired=True)
+ permissions_backend_unreachable(monkeypatch)
_create_agent_session("ses123")
share_id = _create_show_page("ses123", "public")
monkeypatch.setattr("vibe.ui_server._show_public_editor_context", lambda: None)
@@ -13951,6 +13953,7 @@ def test_show_runtime_manager_defaults_to_manifest_when_package_manifest_exists(
def test_remote_instance_manager_can_repair_accessible_show_runtime(monkeypatch, tmp_path, role):
monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
config = _save_config(tmp_path, paired=True, instance_kind="organization")
+ permissions_backend_unreachable(monkeypatch)
_create_show_page("ses-manager-repair", "private")
manager = _FakeShowRuntimeManager(fail=True, failure_reason="runtime_start_health_timeout")
set_show_runtime_manager_for_tests(manager)
diff --git a/tests/test_upgrade_flow.py b/tests/test_upgrade_flow.py
index 2a82761ed..91da3c96a 100644
--- a/tests/test_upgrade_flow.py
+++ b/tests/test_upgrade_flow.py
@@ -53,6 +53,18 @@ def _tree_is_not_an_installed_distribution(monkeypatch):
monkeypatch.setattr("vibe.upgrade._distributions_providing_this_package", lambda: [])
[email protected](autouse=True)
+def _no_latest_release_lookup(monkeypatch):
+ """``do_upgrade`` asks PyPI for the latest release before it builds its plan.
+
+ The tests here drive the upgrade, not that lookup, so they see what an offline
+ machine sees; a test that needs a target names one, and the lookup's own tests
+ call ``get_latest_version_info`` directly.
+ """
+
+ monkeypatch.setattr(api, "get_version_info", lambda: {"latest": None})
+
+
def test_build_upgrade_plan_stages_custom_legacy_uv_launcher(monkeypatch):
monkeypatch.setattr("vibe.upgrade.os.path.exists", lambda path: True)
monkeypatch.setattr("vibe.upgrade.os.access", lambda path, mode: True)
diff --git a/tests/ui_server_test_helpers.py b/tests/ui_server_test_helpers.py
index d41225bb1..aadfa3216 100644
--- a/tests/ui_server_test_helpers.py
+++ b/tests/ui_server_test_helpers.py
@@ -77,6 +77,21 @@ def save_config(tmp_path):
return _save_config(tmp_path)
+def permissions_backend_unreachable(monkeypatch) -> None:
+ """Answer every Permissions backend read the way an unreachable backend does.
+
+ A paired ``_save_config`` names ``https://backend.test``, which no DNS answers,
+ and resolving a Show Page's ownership on such an instance asks that backend.
+ """
+
+ from vibe import permissions
+
+ def unreachable(config=None):
+ raise permissions.PermissionsUnavailableError("permissions_backend_unavailable")
+
+ monkeypatch.setattr(permissions, "get_current_permissions", unreachable)
+
+
def _mock_interface(monkeypatch, ip: str, prefix: int, name: str = "en0") -> None:
address = ipaddress.ip_address(ip)
family = socket.AF_INET if address.version == 4 else socket.AF_INET6