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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(tests): isolate Codex/Claude config homes so the suite can't wipe real auth (#624)
Qi Qi372c7feb80b3
diff --git a/AGENTS.md b/AGENTS.mdindex 989c0fb67..cd78d3923 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -82,13 +82,12 @@ Hard rule:- **Never restart the local `vibe` service for routine verification.**- The local `vibe` process may be the coding agent runtime itself; restarting it can interrupt the session.-- **Tests and probes must never mutate the current local environment or live user state.**- Do not run commands, setup flows, migrations, installers, config writes, or- agent detection/install tests against `~/.avibe`, legacy `~/.vibe_remote`, the user's shell- environment, or the running local service unless the user explicitly asks for- that exact local operation. Use an isolated `VIBE_REMOTE_HOME`, a temporary- fixture directory, the Incus regression environment, or the existing regression- environment instead.+- **Tests and probes must be hermetic by default.** Treat `$HOME`, XDG dirs,+ keychains, CLI config/token stores, running services, browser profiles, and+ cloud accounts as production data unless the user explicitly asks otherwise.+- Any test that reaches write-capable production paths must redirect the whole+ call path to test-owned state and prove a representative write cannot touch+ real local or external user state; `uses_real_paths` tests must remain read-only.- Unless the user explicitly asks otherwise, use the Incus regression environment for user-facing verification.### Regression Testing (Incus)diff --git a/pyproject.toml b/pyproject.tomlindex 56cab3a69..5b61d5241 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -124,7 +124,7 @@ extend-ignore = ["E501"]asyncio_mode = "auto"markers = ["integration: platform integration tests requiring Docker + platform tokens",- "uses_real_paths: opt out of the autouse VIBE_REMOTE_HOME isolation so the test can exercise the real config.paths resolver (must remain read-only)",+ "uses_real_paths: opt out of the autouse home/config isolation so the test can exercise real path resolvers (must remain read-only)",][dependency-groups]diff --git a/tests/conftest.py b/tests/conftest.pyindex e0c3c9ba8..f38657378 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -2,10 +2,9 @@Per AGENTS.md ("Tests and probes must never mutate the current localenvironment or live user state"), every test runs against an isolated-data directory by default, so config writes, state files, and runtime-markers can never leak into the developer's real ``~/.avibe/`` or-``~/.vibe_remote/``-directory.+data directory by default, so config writes, state files, runtime+markers, and backend credential files can never leak into the+developer's real home.Historically a handful of install / upgrade tests mocked``resolve_cli_path`` to return fixture paths like@@ -14,11 +13,25 @@ The post-install bookkeeping in ``vibe.api._run_install_command`` thencalled ``load_config()`` / ``cfg.save()`` against the real config.json andpersisted the fixture path, surfacing in the UI after the next restart.-Isolation mechanism: we set ``VIBE_REMOTE_HOME`` to a per-test tmp-directory. This means ``config.paths.get_vibe_remote_dir`` runs as-written — only its env-var-set branch is exercised under isolation, and-the function itself is never replaced, so the suite still catches-regressions in path-resolution logic.+Isolation mechanism: we set ``HOME``, XDG config/data/cache/state homes, and+``VIBE_REMOTE_HOME`` to a per-test tmp directory, and patch+``pathlib.Path.home`` to match. This means ``config.paths.get_vibe_remote_dir``+runs as written — only its env-var-set branch is exercised under isolation, and+the function itself is never replaced, so the suite still catches regressions in+path-resolution logic while Python helpers, subprocesses, and ``expanduser("~")``+do not see the developer's real home.++The same hazard applies to the agent backends' on-disk credential files:+Codex resolves its home from ``CODEX_HOME`` (falling back to ``~/.codex``)+and Claude Code from ``CLAUDE_CONFIG_DIR`` (falling back to ``~/.claude``).+Tests that drive ``apply_codex_auth`` / ``apply_claude_auth`` — directly or+through the auth-setup scenario harness — would otherwise rewrite the+developer's real ``~/.codex/auth.json`` (dropping ``OPENAI_API_KEY``) and+``~/.claude/settings.json`` (dropping ``ANTHROPIC_*`` env). OpenCode has+no dedicated config-home env var in our helper layer and resolves+``~/.local/share/opencode/auth.json`` from ``Path.home()``, so the+patched home is its isolation boundary. We pin all three to per-test tmp+dirs for the same reason.Path-resolution tests (e.g. ``tests/test_v2_paths.py::test_paths_are_under_home``)intentionally cover the env-var-unset branch where ``get_vibe_remote_dir``@@ -30,15 +43,32 @@ write to ``~/.vibe_remote/``).from __future__ import annotations+from pathlib import Path+import pytest+REAL_USER_HOME = Path.home()+@pytest.fixture(autouse=True)def _isolate_vibe_remote_home(request, tmp_path, monkeypatch):if request.node.get_closest_marker("uses_real_paths"):returnmonkeypatch.delenv("AVIBE_HOME", raising=False)- monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path / ".vibe_remote"))+ isolated_home = tmp_path / "home"+ monkeypatch.setattr(Path, "home", lambda: isolated_home)+ monkeypatch.setenv("HOME", str(isolated_home))+ monkeypatch.setenv("XDG_CONFIG_HOME", str(isolated_home / ".config"))+ monkeypatch.setenv("XDG_DATA_HOME", str(isolated_home / ".local" / "share"))+ monkeypatch.setenv("XDG_CACHE_HOME", str(isolated_home / ".cache"))+ monkeypatch.setenv("XDG_STATE_HOME", str(isolated_home / ".local" / "state"))+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(isolated_home / ".vibe_remote"))+ # Keep Codex / Claude Code credential writes off the developer's real+ # home. Tests that manage these env vars themselves (e.g. the+ # ``get_codex_home`` env-precedence tests) override these via their own+ # monkeypatch calls, which run after this fixture.+ monkeypatch.setenv("CODEX_HOME", str(isolated_home / ".codex"))+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(isolated_home / ".claude"))@pytest.fixture(autouse=True)diff --git a/tests/test_conftest_auth_home_isolation.py b/tests/test_conftest_auth_home_isolation.pynew file mode 100644index 000000000..3a4d7089d--- /dev/null+++ b/tests/test_conftest_auth_home_isolation.py@@ -0,0 +1,113 @@+"""Regression guard: the autouse isolation fixture must redirect backend+credential homes to per-test tmp dirs.++Without this, auth-setup tests that drive ``apply_codex_auth`` /+``apply_claude_auth`` (notably the scenarios under+``tests/scenarios/auth_setup/``) rewrite the developer's real+``~/.codex/auth.json`` and ``~/.claude/settings.json``, silently dropping+``OPENAI_API_KEY`` and the ``ANTHROPIC_*`` env block. OpenCode resolves+``~/.local/share/opencode/auth.json`` from ``Path.home()`` in our helper+layer, so it needs the same default isolation even though the current+auth-setup scenarios mock its installer. This violates the AGENTS.md rule+that tests must never mutate live user state.+"""++from __future__ import annotations++import os+import json+import subprocess+import sys+from pathlib import Path++from tests.conftest import REAL_USER_HOME+from vibe import claude_config, codex_config, opencode_config+++def test_backend_credential_homes_are_isolated_from_real_home() -> None:+ codex_home = codex_config.get_codex_home()+ claude_home = claude_config.get_claude_home()+ opencode_auth = opencode_config.get_opencode_auth_path()+ isolated_home = Path(os.environ["HOME"])++ # The autouse fixture must have pinned explicit backend homes where the+ # underlying tool supports them.+ assert os.environ.get("CODEX_HOME")+ assert os.environ.get("CLAUDE_CONFIG_DIR")+ assert os.environ.get("XDG_CONFIG_HOME") == str(isolated_home / ".config")+ assert os.environ.get("XDG_DATA_HOME") == str(isolated_home / ".local" / "share")+ assert os.environ.get("XDG_CACHE_HOME") == str(isolated_home / ".cache")+ assert os.environ.get("XDG_STATE_HOME") == str(isolated_home / ".local" / "state")++ # And the resolved homes must honour them rather than the real ~/.codex+ # / ~/.claude.+ assert codex_home == Path(os.environ["CODEX_HOME"]).expanduser()+ assert claude_home == Path(os.environ["CLAUDE_CONFIG_DIR"]).expanduser()+ assert codex_home != REAL_USER_HOME / ".codex"+ assert claude_home != REAL_USER_HOME / ".claude"++ # OpenCode has no env-var home override in our helpers, so the autouse+ # Path.home patch is its default isolation boundary.+ assert Path.home() != REAL_USER_HOME+ assert Path.home() == isolated_home+ assert os.path.expanduser("~") == str(isolated_home)+ assert opencode_auth == (+ Path.home() / ".local" / "share" / "opencode" / "auth.json"+ )+ assert opencode_auth != (+ REAL_USER_HOME / ".local" / "share" / "opencode" / "auth.json"+ )+++def test_subprocess_home_and_xdg_paths_are_isolated() -> None:+ code = (+ "import json, os, pathlib; "+ "print(json.dumps({"+ "'home_env': os.environ.get('HOME'), "+ "'path_home': str(pathlib.Path.home()), "+ "'expanduser': os.path.expanduser('~'), "+ "'xdg_config': os.environ.get('XDG_CONFIG_HOME'), "+ "'xdg_data': os.environ.get('XDG_DATA_HOME'), "+ "'xdg_cache': os.environ.get('XDG_CACHE_HOME'), "+ "'xdg_state': os.environ.get('XDG_STATE_HOME')"+ "}))"+ )+ payload = subprocess.check_output([sys.executable, "-c", code], text=True)+ data = json.loads(payload)+ isolated_home = Path(os.environ["HOME"])++ assert data["home_env"] == str(isolated_home)+ assert data["path_home"] == str(isolated_home)+ assert data["expanduser"] == str(isolated_home)+ assert data["xdg_config"] == str(isolated_home / ".config")+ assert data["xdg_data"] == str(isolated_home / ".local" / "share")+ assert data["xdg_cache"] == str(isolated_home / ".cache")+ assert data["xdg_state"] == str(isolated_home / ".local" / "state")+++def test_apply_auth_writes_land_under_isolated_home() -> None:+ """End-to-end: the no-``home`` arg write path used by the live service+ (and exercised by the auth-setup scenarios) must land under the+ isolated home, never the real one."""+ codex_config.apply_codex_auth(+ auth_mode="api_key", api_key="sk-isolation-test", base_url=None+ )+ isolated_auth = codex_config.get_codex_home() / "auth.json"+ assert isolated_auth.exists()+ assert isolated_auth != REAL_USER_HOME / ".codex" / "auth.json"++ claude_config.apply_claude_auth(auth_mode="oauth", api_key=None, base_url=None)+ isolated_settings = claude_config.get_claude_settings_path()+ assert isolated_settings.exists()+ assert isolated_settings != REAL_USER_HOME / ".claude" / "settings.json"++ opencode_auth = opencode_config.get_opencode_auth_path()+ opencode_auth.parent.mkdir(parents=True, exist_ok=True)+ opencode_auth.write_text(+ '{"opencode":{"type":"api","key":"sk-isolation-test"}}\n',+ encoding="utf-8",+ )+ assert opencode_auth.exists()+ assert opencode_auth != (+ REAL_USER_HOME / ".local" / "share" / "opencode" / "auth.json"+ )