Skip to content

Quickly sandbox your AI agent.

Python

fix: re-exec CLI after auto-install to clear stale module state

Tyler Payne85a2ca54f1af
diff --git a/packages/quicksand-core/quicksand_core/_auto_install.py b/packages/quicksand-core/quicksand_core/_auto_install.py
index 88b1537..9ef5c4a 100644
--- a/packages/quicksand-core/quicksand_core/_auto_install.py
+++ b/packages/quicksand-core/quicksand_core/_auto_install.py
@@ -25,6 +25,16 @@ logger = logging.getLogger("quicksand.auto_install")
_TRUTHY = {"1", "true", "yes", "on"}
+class ImagesInstalled(Exception):
+ """Raised after :func:`auto_install_images` successfully installs a wheel.
+
+ Signals the caller that images were written to disk but the running
+ Python process has stale module / entry-point state from before the
+ install. The operation should be retried in a fresh process; the CLI
+ entry point catches this and ``os.execv``s back into the same command.
+ """
+
+
def auto_install_images(package_name: str, images_dir: Path) -> bool:
"""Re-install *package_name* against the quicksand simple index.
@@ -41,7 +51,13 @@ def auto_install_images(package_name: str, images_dir: Path) -> bool:
the reinstall actually placed images.
Returns:
- True if ``manifest.json`` is present in *images_dir* afterwards.
+ ``False`` if auto-install is disabled or fails.
+
+ Raises:
+ ImagesInstalled: When pip successfully installs the new wheel.
+ Pip subprocess runs cleanly, but the parent process still holds
+ references to the previous (pure-stub) module, so continuing
+ in-process is unreliable — bubble up to the CLI for a re-exec.
"""
val = os.environ.get("QUICKSAND_AUTO_INSTALL", "1").strip().lower()
if val not in _TRUTHY:
@@ -77,4 +93,9 @@ def auto_install_images(package_name: str, images_dir: Path) -> bool:
logger.warning("pip install failed (exit %s)", result.returncode)
return False
- return (images_dir / "manifest.json").exists()
+ if not (images_dir / "manifest.json").exists():
+ # Pip succeeded but the new wheel didn't drop a manifest. Treat as
+ # a soft failure so callers can fall back to their own error path.
+ return False
+
+ raise ImagesInstalled(package_name)
diff --git a/packages/quicksand-core/quicksand_core/qemu/image_resolver.py b/packages/quicksand-core/quicksand_core/qemu/image_resolver.py
index 04a0587..5f3f04a 100644
--- a/packages/quicksand-core/quicksand_core/qemu/image_resolver.py
+++ b/packages/quicksand-core/quicksand_core/qemu/image_resolver.py
@@ -15,6 +15,7 @@ import logging
from importlib.metadata import entry_points
from pathlib import Path
+from .._auto_install import ImagesInstalled
from .._types import FilePatterns, ResolvedImage, SaveManifest
from ..host.arch import Architecture, _detect_architecture
@@ -200,6 +201,10 @@ class ImageResolver:
)
logger.info("Resolved base image via entry point: %s", name)
return result
+ except ImagesInstalled:
+ # Auto-install fired during this lookup; the in-process state
+ # is now stale. Bubble up to the CLI for a clean re-exec.
+ raise
except Exception:
logger.debug("Failed to load entry point %s", ep.name, exc_info=True)
continue
diff --git a/packages/quicksand/quicksand/cli/__init__.py b/packages/quicksand/quicksand/cli/__init__.py
index 5587f89..f7d0ea0 100644
--- a/packages/quicksand/quicksand/cli/__init__.py
+++ b/packages/quicksand/quicksand/cli/__init__.py
@@ -3,13 +3,27 @@
from __future__ import annotations
import argparse
+import os
import sys
+from quicksand_core._auto_install import ImagesInstalled
+
from . import clean, dev, install, list_extras, qemu, run, uninstall
def main() -> int:
"""Main entry point for the quicksand CLI."""
+ try:
+ return _dispatch()
+ except ImagesInstalled as e:
+ # A provider auto-installed its fat wheel mid-resolve. Pip ran in a
+ # subprocess so the parent's in-memory module state is stale —
+ # restart the process so subsequent resolution sees a clean slate.
+ print(f"\nInstalled images for {e}; restarting...\n", flush=True)
+ os.execv(sys.executable, [sys.executable, *sys.argv])
+
+
+def _dispatch() -> int:
parser = argparse.ArgumentParser(
prog="quicksand",
description="A VM harness for AI agents.",