Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Gate host integration behind `MARIMO_ENABLE_HOST` (#11302)

Trevor Manzc6226418c358
diff --git a/marimo/_config/settings.py b/marimo/_config/settings.py
index 4e6c1a2374..2711ff194d 100644
--- a/marimo/_config/settings.py
+++ b/marimo/_config/settings.py
@@ -15,6 +15,8 @@ class GlobalSettings:
YES: bool = False
CHECK_STATUS_UPDATE: bool = False
TRACING: bool = is_env_true("MARIMO_TRACING")
+ # Temporary opt-in while the host protocol is experimental.
+ ENABLE_HOST: bool = is_env_true("MARIMO_ENABLE_HOST")
PROFILE_DIR: str | None = None
LOG_LEVEL: int = logging.WARNING
MANAGE_SCRIPT_METADATA: bool = is_env_true("MARIMO_MANAGE_SCRIPT_METADATA")
diff --git a/marimo/_server/host/lifespan.py b/marimo/_server/host/lifespan.py
index 93ad3cf930..8af1b900ef 100644
--- a/marimo/_server/host/lifespan.py
+++ b/marimo/_server/host/lifespan.py
@@ -9,6 +9,7 @@ import secrets
from typing import TYPE_CHECKING
from marimo import _loggers
+from marimo._config.settings import GLOBAL_SETTINGS
from marimo._server.api.deps import AppStateBase
from marimo._server.host.bridge import SessionBridge
from marimo._server.host.context import HostContext
@@ -27,7 +28,7 @@ LOGGER = _loggers.marimo_logger()
async def host(app: Starlette) -> AsyncIterator[None]:
"""Initialize the host API and discovery record in edit mode."""
state = AppStateBase.from_app(app)
- if state.mode is not SessionMode.EDIT:
+ if not GLOBAL_SETTINGS.ENABLE_HOST or state.mode is not SessionMode.EDIT:
yield
return
diff --git a/marimo/_server/main.py b/marimo/_server/main.py
index ac98876be0..c3e8b99d1c 100644
--- a/marimo/_server/main.py
+++ b/marimo/_server/main.py
@@ -60,10 +60,12 @@ def create_starlette_app(
skew_protection: bool = True,
timeout: float | None = None,
) -> Starlette:
- # Handle host API requests first to bypass the editor's middleware.
- final_middlewares: list[Middleware] = [
- Middleware(HostApiMiddleware, base_url=base_url)
- ]
+ final_middlewares: list[Middleware] = []
+ if GLOBAL_SETTINGS.ENABLE_HOST:
+ # Handle host API requests first to bypass the editor's middleware.
+ final_middlewares.append(
+ Middleware(HostApiMiddleware, base_url=base_url)
+ )
if allow_origins is None:
allow_origins = ("localhost", "127.0.0.1") + (
diff --git a/protocols/host/conformance/README.md b/protocols/host/conformance/README.md
index b6798253e8..0bdb90efdd 100644
--- a/protocols/host/conformance/README.md
+++ b/protocols/host/conformance/README.md
@@ -3,7 +3,8 @@
Python tests for hosts implementing the [host protocol](../spec.md). The suite
uses HTTP and SSE and does not import marimo.
-Start a host in a disposable workspace, then run this from the repository root:
+Start a host in a disposable workspace (for marimo, use
+`MARIMO_ENABLE_HOST=1 marimo edit`), then run this from the repository root:
```sh
uv run --locked --project protocols/host/conformance \
diff --git a/tests/_server/host/conftest.py b/tests/_server/host/conftest.py
index 5efe780541..946498d4d3 100644
--- a/tests/_server/host/conftest.py
+++ b/tests/_server/host/conftest.py
@@ -9,6 +9,7 @@ from unittest.mock import MagicMock
import pytest
+from marimo._config.settings import GLOBAL_SETTINGS
from marimo._core import backend as be
from marimo._messaging.notebook import NotebookDocument
from marimo._messaging.notification import NotificationMessage
@@ -20,6 +21,11 @@ from marimo._session.types import KernelState
from marimo._types.ids import ConsumerId
+def enable_host(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(GLOBAL_SETTINGS, "ENABLE_HOST", True)
+
+
class FakeSession:
"""A session fake with a real scratchpad lock and event bus."""
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 0a069e1f06..dd438a7399 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -48,6 +48,7 @@ if TYPE_CHECKING:
TOKEN = "record-token"
AUTH = {"Authorization": f"Bearer {TOKEN}"}
+pytestmark = pytest.mark.usefixtures("enable_host")
async def analyzed(notebook: Notebook) -> protocol.Analysis:
diff --git a/tests/_server/host/test_host_execution.py b/tests/_server/host/test_host_execution.py
index 1160412cd3..67186c913a 100644
--- a/tests/_server/host/test_host_execution.py
+++ b/tests/_server/host/test_host_execution.py
@@ -32,6 +32,7 @@ if TYPE_CHECKING:
from tests._server.host.drive import Response
AUTH = {"Authorization": "Bearer execution-test"}
+pytestmark = pytest.mark.usefixtures("enable_host")
def key(value: str) -> dict[str, str]:
diff --git a/tests/_server/host/test_host_record.py b/tests/_server/host/test_host_record.py
index 42e039b85b..bdc892a768 100644
--- a/tests/_server/host/test_host_record.py
+++ b/tests/_server/host/test_host_record.py
@@ -13,11 +13,13 @@ import msgspec
import pytest
import uvicorn
+from marimo._config.settings import GLOBAL_SETTINGS
from marimo._core import backend as be, protocol
from marimo._server.host import HOST_API_PATH, record as record_module
from marimo._server.host.lifespan import host as host_lifespan
from marimo._server.host.record import host_url
from marimo._server.main import create_starlette_app
+from marimo._session.model import SessionMode
from marimo._utils.lifespans import Lifespans
from tests._core.conftest import (
ScriptedBackend,
@@ -27,11 +29,16 @@ from tests._core.conftest import (
runtime_id_of,
)
from tests._server.host.drive import finished
-from tests._server.mocks import get_starlette_server_state_init
+from tests._server.mocks import (
+ get_mock_session_manager,
+ get_starlette_server_state_init,
+)
if TYPE_CHECKING:
from pathlib import Path
+pytestmark = pytest.mark.usefixtures("enable_host")
+
@pytest.mark.parametrize(
("host", "loopback"),
@@ -79,6 +86,32 @@ async def test_an_unreachable_bind_writes_no_record(
assert app.state.host_context is not None
+ ("enabled", "mode"), [(False, SessionMode.EDIT), (True, SessionMode.RUN)]
+)
+async def test_inactive_host_writes_no_record_and_serves_no_api(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ enabled: bool,
+ mode: SessionMode,
+) -> None:
+ monkeypatch.setattr(GLOBAL_SETTINGS, "ENABLE_HOST", enabled)
+ monkeypatch.setattr(record_module, "marimo_state_dir", lambda: tmp_path)
+ app = create_starlette_app(
+ base_url="", enable_auth=False, lifespan=Lifespans([host_lifespan])
+ )
+ get_starlette_server_state_init(
+ session_manager=get_mock_session_manager(mode=mode)
+ ).apply(app.state)
+ app.state.server = uvicorn.Server(
+ uvicorn.Config(app, host="127.0.0.1", port=1234)
+ )
+
+ async with app.router.lifespan_context(app):
+ assert not (tmp_path / "hosts-v1").exists()
+ assert (await finished(app, "GET", "/notebooks")).status == 404
+
+
@pytest.mark.parametrize("external_port", [1234, 8443])
async def test_the_lifespan_serves_the_host_while_the_server_runs(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, external_port: int