Skip to content

Add InstaVM environment support#156

Open
mkagenius wants to merge 1 commit into
alexzhang13:mainfrom
mkagenius:feature/instavm-environment-v2
Open

Add InstaVM environment support#156
mkagenius wants to merge 1 commit into
alexzhang13:mainfrom
mkagenius:feature/instavm-environment-v2

Conversation

@mkagenius

Copy link
Copy Markdown

Adds an InstaVMREPL environment that runs Python code in InstaVM Firecracker microVMs (sub-second cold starts, stateful Python).

Usage

rlm = RLM(
    environment="instavm",
    environment_kwargs={"api_key": "..."},
)

Or via the env var INSTAVM_API_KEY. Install with pip install -e ".[instavm]".

Implementation

Follows the same isolated-env pattern as E2BREPL / ModalREPL / DaytonaREPL:

  • HTTP broker runs inside the VM and is exposed via vm.shares.create(port=..., is_public=True).
  • A poller thread on the host forwards /pending requests to the local LMHandler and posts responses back to /respond.
  • Inside-VM llm_query / llm_query_batched use httpx (preinstalled in InstaVM images).
  • Broker uses Python stdlib only (http.server, pickle) — no pip install step at session setup.
  • persistent=True raises NotImplementedError, mirroring E2BREPL / ModalREPL.
  • Optional instavm extra with lazy import; tests/test_imports.py continues to pass without the extra installed.

Verification

  • uv run ruff check --fix . — passes
  • uv run ruff format . — passes
  • uv run pre-commit run --all-files — passes (ruff + ruff format + ty)
  • uv run pytest — 277 passed (only failure is a pre-existing test_async_completion config issue in tests/clients/test_gemini.py unrelated to this change)
  • Live end-to-end smoke tested against a real InstaVM with a real OpenAI backend: code execution, state persistence across blocks, single llm_query, batched llm_query_batched, and FINAL_VAR cross-block lookup all work.

History

This is a fresh redo of an unsubmitted draft branch (feature/instavm-environment on the same fork) from ~4 months ago. The original branch never made it to a PR; in the meantime upstream rewrote the env base classes and InstaVM's SDK moved 0.8.0 → 0.21.0, so this is a clean reimplementation against current upstream conventions rather than a rebase.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Copilot AI review requested due to automatic review settings May 2, 2026 06:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new instavm isolated REPL backend so RLM can run generated Python inside InstaVM microVMs, extending the existing family of cloud sandbox environments alongside Modal, E2B, Daytona, and Prime.

Changes:

  • Adds a new InstaVMREPL environment implementation with VM setup, in-VM execution, and host-side LM request brokering.
  • Registers instavm as a selectable environment and adds the optional package extra/dependency lock entries.
  • Documents installation/usage and adds a runnable example for the new environment.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
uv.lock Locks the new instavm optional dependency and extra metadata.
rlm/environments/instavm_repl.py Implements the new InstaVM-backed isolated REPL environment.
rlm/environments/__init__.py Wires instavm into environment lookup and exports.
pyproject.toml Adds the instavm optional dependency extra.
examples/instavm_repl_example.py Demonstrates configuring RLM with the new environment.
README.md Documents InstaVM support and installation steps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

__all__ = [
"BaseEnv",
"IPythonREPL",
"InstaVMREPL",
Comment on lines +373 to +375
share = self.vm.shares.create(port=self.BROKER_PORT, is_public=True)
self.broker_url = share["url"].rstrip("/")
self.share_id = share.get("share_id")
Comment on lines +431 to +434
if req_type == "single":
prompt = req_data.get("prompt")
request = LMRequest(prompt=prompt, model=model)
response = send_lm_request(self.lm_handler_address, request)
Comment thread rlm/environments/instavm_repl.py Outdated
Comment on lines +441 to +443
if req_type == "batched":
prompts = req_data.get("prompts", [])
responses = send_lm_request_batched(self.lm_handler_address, prompts, model=model)
Comment on lines +231 to +235
"__name__": "__main__",
"llm_query": llm_query,
"llm_query_batched": llm_query_batched,
"FINAL_VAR": FINAL_VAR,
}}
Comment thread rlm/environments/instavm_repl.py Outdated
Comment on lines +184 to +204
if os.path.exists(STATE_FILE):
try:
with open(STATE_FILE, "rb") as fh:
return pickle.load(fh)
except Exception:
return {{}}
return {{}}


def save_state(state):
clean = {{}}
for key, value in state.items():
if key.startswith("_"):
continue
try:
pickle.dumps(value)
clean[key] = value
except Exception:
continue
with open(STATE_FILE, "wb") as fh:
pickle.dump(clean, fh)
Comment on lines 50 to +53
def get_environment(
environment: Literal["local", "ipython", "modal", "docker", "daytona", "prime", "e2b"],
environment: Literal[
"local", "ipython", "modal", "docker", "daytona", "prime", "e2b", "instavm"
],
InstaVM (https://instavm.io) provides Firecracker microVMs with sub-second
cold starts and stateful Python execution. This adds a new InstaVMREPL
environment class following the existing isolated-env pattern (e2b/modal/
daytona), so it plugs into RLM via:

    rlm = RLM(
        environment="instavm",
        environment_kwargs={"api_key": ...},
    )

Implementation notes:
- Uses the same HTTP broker pattern as ModalREPL/E2BREPL for forwarding
  in-VM llm_query / llm_query_batched calls back to the host LMHandler.
- Broker runs entirely on Python stdlib (http.server, pickle) inside the
  VM; in-VM client uses httpx (preinstalled in InstaVM images) so the
  setup phase needs zero pip-install steps.
- Public broker URL is obtained via vm.shares.create(port=..., is_public=True).
- persistent=True raises NotImplementedError (matches E2BREPL/ModalREPL).
- Optional dep declared as the 'instavm' extra; lazy-imported only when
  the environment is actually used so test_imports.py stays happy.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mkagenius mkagenius force-pushed the feature/instavm-environment-v2 branch from 0fab680 to 811b914 Compare May 2, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants