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(cli): preserve source restart import path

cyh80dcecadf1a8
diff --git a/tests/test_upgrade_flow.py b/tests/test_upgrade_flow.py
index 6c3b48d88..5b9e7f1c6 100644
--- a/tests/test_upgrade_flow.py
+++ b/tests/test_upgrade_flow.py
@@ -16,6 +16,7 @@ from vibe.upgrade import (
get_current_vibe_bin_dir,
get_latest_version_info,
get_restart_command,
+ get_restart_environment,
get_running_vibe_path,
get_safe_cwd,
)
@@ -267,6 +268,17 @@ def test_get_restart_command_falls_back_to_python_module(monkeypatch):
assert command == ["/usr/bin/python3", "-c", "from vibe.cli import main; main()"]
+def test_get_restart_environment_adds_source_root_for_python_fallback(monkeypatch):
+ monkeypatch.delenv("VIBE_CURRENT_EXECUTABLE", raising=False)
+ monkeypatch.setattr("vibe.upgrade.shutil.which", lambda *args, **kwargs: None)
+
+ env = get_restart_environment(argv0="python", base_env={"PYTHONPATH": "/existing/path"})
+
+ source_root = str(Path(__file__).resolve().parents[1])
+ assert env is not None
+ assert env["PYTHONPATH"] == f"{source_root}{os.pathsep}/existing/path"
+
+
def test_do_upgrade_uses_upgrade_plan_env_and_restarts(monkeypatch):
plan = UpgradePlan(
command=["/usr/local/bin/uv", "tool", "install", "vibe-remote", "--upgrade"],
@@ -278,6 +290,7 @@ def test_do_upgrade_uses_upgrade_plan_env_and_restarts(monkeypatch):
monkeypatch.setattr(api, "build_upgrade_plan", lambda **kwargs: plan)
monkeypatch.setattr(api, "get_running_vibe_path", lambda: "/custom/bin/vibe")
monkeypatch.setattr(api, "get_restart_command", lambda **kwargs: ["/custom/bin/vibe"])
+ monkeypatch.setattr(api, "get_restart_environment", lambda **kwargs: None)
monkeypatch.setattr(api, "_delayed_restart_helper_command", lambda: ["/usr/bin/python3"])
def fake_run(cmd, **kwargs):
diff --git a/tests/test_vibe_cli.py b/tests/test_vibe_cli.py
index 587ca5500..ed72d8de5 100644
--- a/tests/test_vibe_cli.py
+++ b/tests/test_vibe_cli.py
@@ -99,12 +99,13 @@ def test_cmd_restart_schedules_delayed_restart(monkeypatch, capsys):
monkeypatch.setattr(cli, "cache_running_vibe_path", lambda: "/usr/local/bin/vibe")
monkeypatch.setattr(cli, "get_restart_command", lambda vibe_path=None: [vibe_path or "vibe"])
+ monkeypatch.setattr(cli, "get_restart_environment", lambda vibe_path=None: None)
monkeypatch.setattr(cli, "get_safe_cwd", lambda: "/tmp")
monkeypatch.setattr(
cli.api,
"_spawn_delayed_restart",
- lambda command, cwd, delay_seconds=2.0: scheduled.update(
- {"command": command, "cwd": cwd, "delay_seconds": delay_seconds}
+ lambda command, cwd, delay_seconds=2.0, env=None: scheduled.update(
+ {"command": command, "cwd": cwd, "delay_seconds": delay_seconds, "env": env}
),
)
monkeypatch.setattr(cli, "cmd_stop", lambda: stop_called.append(True))
@@ -115,6 +116,7 @@ def test_cmd_restart_schedules_delayed_restart(monkeypatch, capsys):
"command": ["/usr/local/bin/vibe", "restart"],
"cwd": "/tmp",
"delay_seconds": 60,
+ "env": None,
}
assert stop_called == []
assert start_called == []
@@ -124,6 +126,34 @@ def test_cmd_restart_schedules_delayed_restart(monkeypatch, capsys):
assert "delayed restart will run in the background" in output
+def test_cmd_restart_schedules_delayed_restart_with_import_env(monkeypatch):
+ scheduled = {}
+
+ monkeypatch.setattr(cli, "cache_running_vibe_path", lambda: None)
+ monkeypatch.setattr(
+ cli,
+ "get_restart_command",
+ lambda vibe_path=None: [sys.executable, "-c", "from vibe.cli import main; main()"],
+ )
+ monkeypatch.setattr(cli, "get_restart_environment", lambda vibe_path=None: {"PYTHONPATH": "/repo"})
+ monkeypatch.setattr(cli, "get_safe_cwd", lambda: "/tmp")
+ monkeypatch.setattr(
+ cli.api,
+ "_spawn_delayed_restart",
+ lambda command, cwd, delay_seconds=2.0, env=None: scheduled.update(
+ {"command": command, "cwd": cwd, "delay_seconds": delay_seconds, "env": env}
+ ),
+ )
+
+ assert cli._cmd_restart_with_delay(5) == 0
+ assert scheduled == {
+ "command": [sys.executable, "-c", "from vibe.cli import main; main()", "restart"],
+ "cwd": "/tmp",
+ "delay_seconds": 5,
+ "env": {"PYTHONPATH": "/repo"},
+ }
+
+
def test_cmd_restart_runs_synchronously_by_default(monkeypatch):
calls = []
diff --git a/vibe/api.py b/vibe/api.py
index ffc261f28..0dd110e08 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -33,6 +33,7 @@ from vibe.upgrade import (
build_upgrade_plan,
get_latest_version_info,
get_restart_command,
+ get_restart_environment,
get_running_vibe_path,
get_safe_cwd,
)
@@ -71,11 +72,16 @@ def _delayed_restart_helper_command() -> list[str]:
raise FileNotFoundError("No stable Python launcher available for delayed restart helper")
-def _spawn_delayed_restart(command: list[str], cwd: str, delay_seconds: float = 2.0) -> None:
+def _spawn_delayed_restart(
+ command: list[str],
+ cwd: str,
+ delay_seconds: float = 2.0,
+ env: dict[str, str] | None = None,
+) -> None:
helper_code = (
"import subprocess, time\n"
f"time.sleep({delay_seconds!r})\n"
- f"subprocess.Popen({command!r}, cwd={cwd!r}, stdout=subprocess.DEVNULL, "
+ f"subprocess.Popen({command!r}, cwd={cwd!r}, env={env!r}, stdout=subprocess.DEVNULL, "
"stderr=subprocess.DEVNULL, close_fds=True)\n"
)
helper_cmd = [*_delayed_restart_helper_command(), "-c", helper_code]
@@ -779,6 +785,7 @@ def do_upgrade(auto_restart: bool = True) -> dict:
_spawn_delayed_restart(
get_restart_command(vibe_path=current_vibe_path),
safe_cwd,
+ env=get_restart_environment(vibe_path=current_vibe_path),
)
restarting = True
diff --git a/vibe/cli.py b/vibe/cli.py
index 47a0bd77c..9cb28a4ff 100644
--- a/vibe/cli.py
+++ b/vibe/cli.py
@@ -41,6 +41,7 @@ from vibe.upgrade import (
cache_running_vibe_path,
get_latest_version_info,
get_restart_command,
+ get_restart_environment,
get_safe_cwd,
)
@@ -1990,7 +1991,12 @@ def _format_restart_delay(delay_seconds: float) -> str:
def _schedule_delayed_restart(delay_seconds: float) -> int:
current_vibe_path = cache_running_vibe_path()
restart_command = [*get_restart_command(vibe_path=current_vibe_path), "restart"]
- api._spawn_delayed_restart(restart_command, get_safe_cwd(), delay_seconds=delay_seconds)
+ api._spawn_delayed_restart(
+ restart_command,
+ get_safe_cwd(),
+ delay_seconds=delay_seconds,
+ env=get_restart_environment(vibe_path=current_vibe_path),
+ )
print(f"Restart scheduled in {_format_restart_delay(delay_seconds)}.")
print("This command exits immediately; the delayed restart will run in the background.")
return 0
diff --git a/vibe/upgrade.py b/vibe/upgrade.py
index fbab6d619..1dc1ee380 100644
--- a/vibe/upgrade.py
+++ b/vibe/upgrade.py
@@ -148,6 +148,50 @@ def get_restart_command(
return [python_executable or sys.executable, "-c", "from vibe.cli import main; main()"]
+def _get_source_checkout_root() -> str | None:
+ source_root = Path(__file__).resolve().parent.parent
+ if not source_root.is_dir():
+ return None
+ if not (source_root / "pyproject.toml").is_file():
+ return None
+ if not (source_root / "vibe" / "__init__.py").is_file():
+ return None
+ return str(source_root)
+
+
+def get_restart_environment(
+ *,
+ vibe_path: str | None = None,
+ argv0: str | None = None,
+ search_path: str | None = None,
+ base_env: Mapping[str, str] | None = None,
+) -> dict[str, str] | None:
+ resolved = get_running_vibe_path(vibe_path=vibe_path, argv0=argv0, search_path=search_path)
+ if resolved:
+ return None
+
+ source_root = _get_source_checkout_root()
+ if not source_root:
+ return None
+
+ env = dict(base_env or os.environ)
+ pythonpath = env.get("PYTHONPATH")
+ if pythonpath:
+ normalized_root = os.path.abspath(source_root)
+ normalized_entries = {
+ os.path.abspath(os.path.expanduser(entry))
+ for entry in pythonpath.split(os.pathsep)
+ if entry
+ }
+ if normalized_root in normalized_entries:
+ return env
+ env["PYTHONPATH"] = os.pathsep.join((source_root, pythonpath))
+ return env
+
+ env["PYTHONPATH"] = source_root
+ return env
+
+
def get_restart_shell_command(
*,
vibe_path: str | None = None,