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(windows): check a managed runtime where it stays, and end the engine a stopped service left (#2475)

Call Whitee6299002dea7
diff --git a/core/managed_runtime.py b/core/managed_runtime.py
index 77a356089..c13460c92 100644
--- a/core/managed_runtime.py
+++ b/core/managed_runtime.py
@@ -373,7 +373,16 @@ class ManagedRuntimeManager:
manifest=manifest,
archive=archive,
)
- if not self._binary_matches_manifest(staged_binary, manifest):
+ replace_target = install_dir.exists() and (
+ (force and self.spec.replace_target_on_force)
+ or (canonical_target_was_rejected and self.spec.replace_invalid_target_on_repair)
+ )
+ # Run the binary where it stays: Windows may hold a file it just
+ # executed (Defender, the Arm64 x64-emulation cache) for a moment,
+ # and a directory holding one cannot be renamed. A target replaced
+ # in place is removed first, so that binary is checked here, while
+ # the install it replaces is still there to keep.
+ if replace_target and not self._binary_matches_manifest(staged_binary, manifest):
return self._failure(
self._reason("binary_not_runnable"),
manifest=manifest,
@@ -382,12 +391,6 @@ class ManagedRuntimeManager:
install_dir.parent.mkdir(parents=True, exist_ok=True)
if install_dir.exists():
- replace_target = (
- force and self.spec.replace_target_on_force
- ) or (
- canonical_target_was_rejected
- and self.spec.replace_invalid_target_on_repair
- )
if replace_target:
if not self._remove_install_target_for_replacement(install_dir):
return self._failure(
@@ -404,6 +407,12 @@ class ManagedRuntimeManager:
shutil.move(str(staging_dir), str(install_dir))
candidate_install_dir = install_dir
installed_binary = (install_dir / archive.bin_path).resolve(strict=True)
+ if not replace_target and not self._binary_matches_manifest(installed_binary, manifest):
+ return self._failure(
+ self._reason("binary_not_runnable"),
+ manifest=manifest,
+ archive=archive,
+ )
self._write_manifest_install_metadata(
install_dir,
manifest,
diff --git a/tests/test_managed_runtime.py b/tests/test_managed_runtime.py
index 96eb80fb5..ade10d35d 100644
--- a/tests/test_managed_runtime.py
+++ b/tests/test_managed_runtime.py
@@ -190,6 +190,37 @@ def _fixture_runtime_manager(
)
[email protected]("runs", [True, False])
+def test_install_runs_the_binary_only_from_its_install_directory(tmp_path: Path, runs: bool) -> None:
+ """Windows may hold a file it just ran, and a directory holding one cannot be renamed.
+
+ So the version check runs where the binary stays, never in staging; a
+ binary that fails it leaves neither a published install nor its directory.
+ """
+ manifest_path = tmp_path / "manifest.json"
+ _write_fixture_runtime_release(tmp_path, manifest_path, label="ran-in-place", version="1.0.0")
+ manager = _fixture_runtime_manager(tmp_path / "runtime", manifest_path=manifest_path)
+ probed: list[Path] = []
+
+ def binary_version(binary: Path | None) -> str | None:
+ probed.append(Path(binary))
+ return "1.0.0" if runs else None
+
+ manager._binary_version = binary_version
+
+ result = manager.ensure()
+
+ assert len(probed) == 1
+ assert not any(part.startswith("install-") for part in probed[0].relative_to(tmp_path / "runtime").parts)
+ if runs:
+ assert result["ok"] is True
+ assert probed[0] == Path(result["path"])
+ else:
+ assert result["ok"] is False
+ assert result["reason"].endswith("binary_not_runnable")
+ assert not probed[0].exists()
+
+
def test_force_install_uses_sibling_target_by_default(
tmp_path: Path,
) -> None:
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index ca99b5c48..69c35c57a 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -3419,6 +3419,45 @@ def test_supervisor_stop_without_a_handle_reaps_the_recorded_engine(tmp_path: Pa
assert not (store.root / "engine-process.json").exists()
[email protected]("stop", ["full", "desktop"])
+def test_vibe_stop_ends_the_engine_a_terminated_service_left_running(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, stop: str,
+) -> None:
+ """On Windows a stop ends the service with TerminateProcess, so its engine outlives it.
+
+ The engine then kept its port, and its executable locked, until the next
+ start. Both stops end it while no service holds the lock.
+ """
+ from vibe import cli
+ from vibe.model_hub_runtime.supervisor import set_engine_supervisor_for_tests
+
+ orphan = _orphan_engine(tmp_path)
+ supervisor, store = _fixture_supervisor(tmp_path)
+ set_engine_supervisor_for_tests(supervisor)
+ try:
+ if stop == "full":
+ monkeypatch.setattr(cli, "_pid_file_points_to_live_process", lambda path: False)
+ monkeypatch.setattr(cli.runtime, "stop_service", lambda **kwargs: False)
+ monkeypatch.setattr(cli.runtime, "stop_ui", lambda **kwargs: False)
+ monkeypatch.setattr(cli, "_stop_opencode_server", lambda *args: False)
+ monkeypatch.setattr(cli, "_stop_jobs", lambda: None)
+ monkeypatch.setattr(cli, "_write_status", lambda state, detail=None: None)
+ assert cli.cmd_stop() == 0
+ else:
+ stopped = SimpleNamespace(
+ outcome=cli.runtime.DesktopRuntimeStopOutcome.STOPPED, opencode_stopped=False, remaining=(),
+ )
+ monkeypatch.setattr(cli.runtime, "stop_desktop_runtime", lambda runtime_id: stopped)
+ monkeypatch.setattr(cli, "_stop_the_home_s_connector", lambda: 0)
+ monkeypatch.setattr(cli, "_print_left_running", lambda result: None)
+ assert cli.cmd_stop(expect_runtime_id="desktop-fixture") == 0
+
+ _wait_for(lambda: orphan.poll() is not None)
+ assert not (store.root / "engine-process.json").exists()
+ finally:
+ set_engine_supervisor_for_tests(None)
+
+
def test_supervisor_refuses_to_start_beside_an_unconfirmed_engine(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
diff --git a/vibe/cli.py b/vibe/cli.py
index 802e33c15..966ac2396 100644
--- a/vibe/cli.py
+++ b/vibe/cli.py
@@ -14138,6 +14138,7 @@ def _stop_expected_desktop_runtime(runtime_id: str) -> int:
if result.outcome is runtime.DesktopRuntimeStopOutcome.NOT_OURS:
_print_stop_json({"reason": result.refusal})
return 3
+ runtime.desktop_service_lock_presence(while_absent=_stop_model_hub_engine)
if result.opencode_stopped:
_report_opencode_stopped()
# Unlike a full stop, an OpenCode server of this Runtime that survives
@@ -14216,6 +14217,30 @@ def _stop_jobs() -> None:
logger.warning("Could not stop the commands jobs run", exc_info=True)
+def _stop_model_hub_engine() -> None:
+ """End a Model Hub engine its service left running.
+
+ A service stops its own engine, but a stop that ends the service outright --
+ always on Windows, where it is TerminateProcess -- leaves the engine running
+ and holding its port until the next start reaps it. The supervisor's stop
+ reaps by the record and identity marker that service left, so it ends
+ exactly that engine. Called only while holding the free service lock; a
+ failure is logged and left to the next start.
+ """
+
+ try:
+ from vibe.model_hub_runtime.supervisor import get_engine_supervisor
+
+ get_engine_supervisor().stop()
+ except Exception:
+ logger.warning("Could not stop a Model Hub engine its service left running", exc_info=True)
+
+
+def _stop_what_the_service_left() -> None:
+ _stop_jobs()
+ _stop_model_hub_engine()
+
+
def cmd_stop(*, expect_runtime_id: str | None = None):
from vibe.desktop_runtime import desktop_caller_provenance
@@ -14239,7 +14264,7 @@ def cmd_stop(*, expect_runtime_id: str | None = None):
return 3
# Holding the free service lock: neither a service that failed to stop nor one
# started since owns these commands while they are stopped.
- runtime.desktop_service_lock_presence(while_absent=_stop_jobs)
+ runtime.desktop_service_lock_presence(while_absent=_stop_what_the_service_left)
from vibe.desktop_backends import reap_abandoned_desktop_backend_installs
# Each tree's owner decides whether it is abandoned, so the reap runs