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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
refactor: break down session.py into smaller bits (#7532)
Myles Scolnick128ac621a46a
diff --git a/marimo/_server/api/deps.py b/marimo/_server/api/deps.pyindex 01492b904a..3547467ae2 100644--- a/marimo/_server/api/deps.py+++ b/marimo/_server/api/deps.py@@ -50,10 +50,6 @@ class AppStateBase:def quiet(self) -> bool:return self.session_manager.quiet- @property- def development_mode(self) -> bool:- return self.session_manager.development_mode-@propertydef host(self) -> str:host: str = self.state.host@@ -164,9 +160,9 @@ class AppState(AppStateBase):LOGGER.warning("Valid consumers ids: %s",[- list(session.room.consumers.values())+ list(session.consumers.values())for session in self.session_manager.sessions.values()- if session.room.consumers+ if session.consumers],)raise ValueError(f"Invalid session id: {session_id}")diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.pyindex c8a65c9387..2818771f1c 100644--- a/marimo/_server/api/lifespans.py+++ b/marimo/_server/api/lifespans.py@@ -156,12 +156,13 @@ async def open_browser(app: Starlette) -> AsyncIterator[None]:async def logging(app: Starlette) -> AsyncIterator[None]:state = AppState.from_app(app)manager: SessionManager = state.session_manager+ quiet = state.quietfile_router = manager.file_routermcp_server_enabled = state.mcp_server_enabledskew_protection_enabled = state.skew_protection# Startup message- if not manager.quiet:+ if not quiet:file = file_router.maybe_get_single_file()print_startup(file_name=file.name if file else None,@@ -183,7 +184,7 @@ async def logging(app: Starlette) -> AsyncIterator[None]:yield# Shutdown message- if not manager.quiet:+ if not quiet:print_shutdown()@@ -199,7 +200,7 @@ async def signal_handler(app: Starlette) -> AsyncIterator[None]:close_uvicorn(state.server)InterruptHandler(- quiet=manager.quiet,+ quiet=state.quiet,shutdown=shutdown,).register()yielddiff --git a/marimo/_server/asgi.py b/marimo/_server/asgi.pyindex c92f170d89..9431d97997 100644--- a/marimo/_server/asgi.py+++ b/marimo/_server/asgi.py@@ -456,7 +456,6 @@ def create_asgi_app(session_manager = SessionManager(file_router=AppFileRouter.from_filename(MarimoPath(file_path)),mode=SessionMode.RUN,- development_mode=False,quiet=quiet,include_code=include_code,# Currently we only support run mode,diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 4a3932e95a..8c1fa5a33a 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -237,7 +237,7 @@ async def run_app_until_completion(cli_args: SerializedCLIArgs,argv: list[str] | None,) -> tuple[SessionView, bool]:- from marimo._server.sessions import Session+ from marimo._server.sessions.session import SessionImplinstantiated_event = asyncio.Event()@@ -317,7 +317,7 @@ async def run_app_until_completion(# Create a sessionsession_consumer = DefaultSessionConsumer()- session = Session.create(+ session = SessionImpl.create(# Any initialization ID will doinitialization_id="_any_",session_consumer=session_consumer,diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.pydeleted file mode 100644index 046e8f4de7..0000000000--- a/marimo/_server/sessions.py+++ /dev/null@@ -1,1271 +0,0 @@-# Copyright 2024 Marimo. All rights reserved.-"""Client session management--This module encapsulates session management: each client gets a unique session,-and each session wraps a Python kernel and a websocket connection through which-the kernel can send messages to the frontend. Sessions do not share kernels or-websockets.--In run mode, in which we may have many clients connected to the server, a-session is closed as soon as its websocket connection is severed. In edit mode,-in which we have at most one connected client, a session may be kept around-even if its socket is closed.-"""--from __future__ import annotations--import asyncio-import multiprocessing as mp-import os-import queue-import signal-import sys-import threading-import time-from multiprocessing import connection-from multiprocessing.queues import Queue as MPQueue-from pathlib import Path-from typing import Any, Optional, Union-from uuid import uuid4--from marimo import _loggers-from marimo._ast.cell import CellConfig-from marimo._cli.print import red-from marimo._config.manager import (- MarimoConfigManager,- MarimoConfigReader,- ScriptConfigManager,-)-from marimo._config.settings import GLOBAL_SETTINGS-from marimo._messaging.ops import (- FocusCell,- MessageOperation,- Reload,- UpdateCellCodes,- UpdateCellIdsRequest,-)-from marimo._messaging.types import KernelMessage-from marimo._output.formatters.formatters import register_formatters-from marimo._runtime import requests, runtime-from marimo._runtime.requests import (- AppMetadata,- CreationRequest,- DeleteCellRequest,- ExecuteMultipleRequest,- ExecutionRequest,- HTTPRequest,- SerializedCLIArgs,- SerializedQueryParams,- SetUIElementValueRequest,- SyncGraphRequest,-)-from marimo._server.exceptions import InvalidSessionException-from marimo._server.file_router import AppFileRouter, MarimoFileKey-from marimo._server.lsp import LspServer-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode-from marimo._server.models.models import InstantiateRequest-from marimo._server.notebook import AppFileManager-from marimo._server.recents import RecentFilesManager-from marimo._server.session.serialize import (- SessionCacheKey,- SessionCacheManager,-)-from marimo._server.session.session_view import SessionView-from marimo._server.tokens import AuthToken, SkewProtectionToken-from marimo._server.types import ProcessLike-from marimo._server.utils import print_, print_tabbed-from marimo._types.ids import CellId_t, ConsumerId, SessionId-from marimo._utils import async_path-from marimo._utils.disposable import Disposable-from marimo._utils.distributor import (- ConnectionDistributor,- QueueDistributor,-)-from marimo._utils.file_watcher import FileWatcherManager-from marimo._utils.repr import format_repr-from marimo._utils.typed_connection import TypedConnection--LOGGER = _loggers.marimo_logger()---class QueueManager:- """Manages queues for a session."""-- def __init__(self, use_multiprocessing: bool):- context = mp.get_context("spawn") if use_multiprocessing else None-- # Control messages for the kernel (run, set UI element, set config, etc- # ) are sent through the control queue- self.control_queue: Union[- mp.Queue[requests.ControlRequest],- queue.Queue[requests.ControlRequest],- ] = context.Queue() if context is not None else queue.Queue()-- # Set UI element queues are stored in both the control queue and- # this queue, so that the backend can merge/batch set-ui-element- # requests.- self.set_ui_element_queue: Union[- mp.Queue[requests.SetUIElementValueRequest],- queue.Queue[requests.SetUIElementValueRequest],- ] = context.Queue() if context is not None else queue.Queue()-- # Code completion requests are sent through a separate queue- self.completion_queue: Union[- mp.Queue[requests.CodeCompletionRequest],- queue.Queue[requests.CodeCompletionRequest],- ] = context.Queue() if context is not None else queue.Queue()-- self.win32_interrupt_queue: (- Union[mp.Queue[bool], queue.Queue[bool]] | None- )- if sys.platform == "win32":- self.win32_interrupt_queue = (- context.Queue() if context is not None else queue.Queue()- )- else:- self.win32_interrupt_queue = None-- # Input messages for the user's Python code are sent through the- # input queue- self.input_queue: Union[mp.Queue[str], queue.Queue[str]] = (- context.Queue(maxsize=1)- if context is not None- else queue.Queue(maxsize=1)- )- self.stream_queue: Optional[- queue.Queue[Union[KernelMessage, None]]- ] = None- if not use_multiprocessing:- self.stream_queue = queue.Queue()-- def close_queues(self) -> None:- if isinstance(self.control_queue, MPQueue):- # cancel join thread because we don't care if the queues still have- # things in it: don't want to make the child process wait for the- # queues to empty- self.control_queue.cancel_join_thread()- self.control_queue.close()- else:- # kernel thread cleans up read/write conn and IOloop handler on- # exit; we don't join the thread because we don't want to block- self.control_queue.put(requests.StopRequest())-- if isinstance(self.set_ui_element_queue, MPQueue):- self.set_ui_element_queue.cancel_join_thread()- self.set_ui_element_queue.close()-- if isinstance(self.input_queue, MPQueue):- # again, don't make the child process wait for the queues to empty- self.input_queue.cancel_join_thread()- self.input_queue.close()-- if isinstance(self.completion_queue, MPQueue):- self.completion_queue.cancel_join_thread()- self.completion_queue.close()-- if isinstance(self.win32_interrupt_queue, MPQueue):- self.win32_interrupt_queue.cancel_join_thread()- self.win32_interrupt_queue.close()---class KernelManager:- def __init__(- self,- queue_manager: QueueManager,- mode: SessionMode,- configs: dict[CellId_t, CellConfig],- app_metadata: AppMetadata,- config_manager: MarimoConfigReader,- virtual_files_supported: bool,- redirect_console_to_browser: bool,- ) -> None:- self.kernel_task: Optional[ProcessLike | threading.Thread] = None- self.queue_manager = queue_manager- self.mode = mode- self.configs = configs- self.app_metadata = app_metadata- self.config_manager = config_manager- self.redirect_console_to_browser = redirect_console_to_browser-- # Only used in edit mode- self._read_conn: Optional[TypedConnection[KernelMessage]] = None- self._virtual_files_supported = virtual_files_supported-- def start_kernel(self) -> None:- # We use a process in edit mode so that we can interrupt the app- # with a SIGINT; we don't mind the additional memory consumption,- # since there's only one client session- is_edit_mode = self.mode == SessionMode.EDIT- listener = None- if is_edit_mode:- # Need to use a socket for windows compatibility- listener = connection.Listener(family="AF_INET")- self.kernel_task = mp.Process(- target=runtime.launch_kernel,- args=(- self.queue_manager.control_queue,- self.queue_manager.set_ui_element_queue,- self.queue_manager.completion_queue,- self.queue_manager.input_queue,- # stream queue unused- None,- listener.address,- is_edit_mode,- self.configs,- self.app_metadata,- self.config_manager.get_config(hide_secrets=False),- self._virtual_files_supported,- self.redirect_console_to_browser,- self.queue_manager.win32_interrupt_queue,- self.profile_path,- GLOBAL_SETTINGS.LOG_LEVEL,- ),- # The process can't be a daemon, because daemonic processes- # can't create children- # https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon # noqa: E501- daemon=False,- )- else:- # We use threads in run mode to minimize memory consumption;- # launching a process would copy the entire program state,- # which (as of writing) is around 150MB-- # We can't terminate threads, so we have to wait until they- # naturally exit before cleaning up resources- def launch_kernel_with_cleanup(*args: Any) -> None:- runtime.launch_kernel(*args)-- # install formatter import hooks, which will be shared by all- # threads (in edit mode, the single kernel process installs- # formatters ...)- register_formatters(theme=self.config_manager.theme)-- assert self.queue_manager.stream_queue is not None- # Make threads daemons so killing the server immediately brings- # down all client sessions- self.kernel_task = threading.Thread(- target=launch_kernel_with_cleanup,- args=(- self.queue_manager.control_queue,- self.queue_manager.set_ui_element_queue,- self.queue_manager.completion_queue,- self.queue_manager.input_queue,- self.queue_manager.stream_queue,- # IPC not used in run mode- None,- is_edit_mode,- self.configs,- self.app_metadata,- self.config_manager.get_config(hide_secrets=False),- self._virtual_files_supported,- self.redirect_console_to_browser,- # win32 interrupt queue- None,- # profile path- None,- # log level- GLOBAL_SETTINGS.LOG_LEVEL,- ),- # daemon threads can create child processes, unlike- # daemon processes- daemon=True,- )-- self.kernel_task.start() # type: ignore- if listener is not None:- # First thing kernel does is connect to the socket, so it's safe to- # call accept- self._read_conn = TypedConnection[KernelMessage].of(- listener.accept()- )-- @property- def profile_path(self) -> str | None:- self._profile_path: str | None-- if hasattr(self, "_profile_path"):- return self._profile_path-- profile_dir = GLOBAL_SETTINGS.PROFILE_DIR- if profile_dir is not None:- self._profile_path = os.path.join(- profile_dir,- (- os.path.basename(self.app_metadata.filename) + str(uuid4())- if self.app_metadata.filename is not None- else str(uuid4())- ),- )- else:- self._profile_path = None- return self._profile_path-- def is_alive(self) -> bool:- return self.kernel_task is not None and self.kernel_task.is_alive()-- def interrupt_kernel(self) -> None:- if self.kernel_task is None:- return-- if isinstance(self.kernel_task, threading.Thread):- # no interruptions in run mode- return-- if self.kernel_task.pid is not None:- q = self.queue_manager.win32_interrupt_queue- if sys.platform == "win32" and q is not None:- LOGGER.debug("Queueing interrupt request for kernel.")- q.put_nowait(True)- else:- LOGGER.debug("Sending SIGINT to kernel")- os.kill(self.kernel_task.pid, signal.SIGINT)-- def close_kernel(self) -> None:- assert self.kernel_task is not None, "kernel not started"-- if isinstance(self.kernel_task, threading.Thread):- # in run mode- if self.kernel_task.is_alive():- # We don't join the kernel thread because we don't want to server- # to block on it finishing- self.queue_manager.control_queue.put(requests.StopRequest())- else:- # otherwise we have something that is `ProcessLike`- if self.profile_path is not None and self.kernel_task.is_alive():- self.queue_manager.control_queue.put(requests.StopRequest())- # Hack: Wait for kernel to exit and write out profile;- # joining the process hangs, but not sure why.- print_(- "\tWriting profile statistics to",- self.profile_path,- " ...",- )- while not os.path.exists(self.profile_path):- time.sleep(0.1)- time.sleep(1)-- self.queue_manager.close_queues()- if self.kernel_task.is_alive():- self.kernel_task.terminate()- if self._read_conn is not None:- self._read_conn.close()-- @property- def kernel_connection(self) -> TypedConnection[KernelMessage]:- assert self._read_conn is not None, "connection not started"- return self._read_conn---class Room:- """- A room is a collection of SessionConsumers- that can be used to broadcast messages to all- of them.- """-- def __init__(self) -> None:- self.main_consumer: Optional[SessionConsumer] = None- self.consumers: dict[SessionConsumer, ConsumerId] = {}- self.disposables: dict[SessionConsumer, Disposable] = {}-- @property- def size(self) -> int:- return len(self.consumers)-- def add_consumer(- self,- consumer: SessionConsumer,- dispose: Disposable,- consumer_id: ConsumerId,- # Whether the consumer is the main session consumer- # We only allow one main consumer, the rest are kiosk consumers- main: bool,- ) -> None:- self.consumers[consumer] = consumer_id- self.disposables[consumer] = dispose- if main:- assert self.main_consumer is None, (- "Main session consumer already exists"- )- self.main_consumer = consumer-- def remove_consumer(self, consumer: SessionConsumer) -> None:- if consumer not in self.consumers:- LOGGER.debug(- "Attempted to remove a consumer that was not in room."- )- return-- if consumer == self.main_consumer:- self.main_consumer = None- self.consumers.pop(consumer)- disposable = self.disposables.pop(consumer)- try:- consumer.on_stop()- finally:- disposable.dispose()-- def broadcast(- self,- operation: MessageOperation,- except_consumer: Optional[ConsumerId],- ) -> None:- for consumer in self.consumers:- if consumer.consumer_id == except_consumer:- continue- if consumer.connection_state() == ConnectionState.OPEN:- consumer.write_operation(operation)-- def close(self) -> None:- for consumer in self.consumers:- disposable = self.disposables.pop(consumer)- consumer.on_stop()- disposable.dispose()- self.consumers = {}- self.main_consumer = None---_DEFAULT_TTL_SECONDS = 120---class Session:- """A client session.-- Each session has its own Python kernel, for editing and running the app,- and its own websocket, for sending messages to the client.- """-- SESSION_CACHE_INTERVAL_SECONDS = 2-- @classmethod- def create(- cls,- *,- initialization_id: str,- session_consumer: SessionConsumer,- mode: SessionMode,- app_metadata: AppMetadata,- app_file_manager: AppFileManager,- config_manager: MarimoConfigManager,- virtual_files_supported: bool,- redirect_console_to_browser: bool,- ttl_seconds: Optional[int],- ) -> Session:- """- Create a new session.- """- # Inherit config from the session manager- # and override with any script-level config- config_manager = config_manager.with_overrides(- ScriptConfigManager(app_file_manager.path).get_config()- )-- configs = app_file_manager.app.cell_manager.config_map()- use_multiprocessing = mode == SessionMode.EDIT- queue_manager = QueueManager(use_multiprocessing)- kernel_manager = KernelManager(- queue_manager,- mode,- configs,- app_metadata,- config_manager,- virtual_files_supported=virtual_files_supported,- redirect_console_to_browser=redirect_console_to_browser,- )-- return cls(- initialization_id=initialization_id,- session_consumer=session_consumer,- queue_manager=queue_manager,- kernel_manager=kernel_manager,- app_file_manager=app_file_manager,- config_manager=config_manager,- ttl_seconds=ttl_seconds,- )-- def __init__(- self,- initialization_id: str,- session_consumer: SessionConsumer,- queue_manager: QueueManager,- kernel_manager: KernelManager,- app_file_manager: AppFileManager,- config_manager: MarimoConfigManager,- ttl_seconds: Optional[int],- ) -> None:- """Initialize kernel and client connection to it."""- # This is some unique ID that we can use to identify the session- # in edit mode. We don't use the session_id because this can change if- # the session is resumed- self.initialization_id = initialization_id- self.app_file_manager = app_file_manager- self.room = Room()- self._queue_manager = queue_manager- self.kernel_manager = kernel_manager- self.ttl_seconds = (- ttl_seconds if ttl_seconds is not None else _DEFAULT_TTL_SECONDS- )- self.session_view = SessionView()- self.session_cache_manager: SessionCacheManager | None = None- self.config_manager = config_manager- self.kernel_manager.start_kernel()- # Reads from the kernel connection and distributes the- # messages to each subscriber.- self.message_distributor: (- ConnectionDistributor[KernelMessage]- | QueueDistributor[KernelMessage]- )- if self.kernel_manager.mode == SessionMode.EDIT:- self.message_distributor = ConnectionDistributor[KernelMessage](- self.kernel_manager.kernel_connection- )- else:- q = self._queue_manager.stream_queue- assert q is not None- self.message_distributor = QueueDistributor[KernelMessage](queue=q)-- self.message_distributor.add_consumer(- lambda msg: self.session_view.add_raw_operation(msg)- )- self.connect_consumer(session_consumer, main=True)- self.message_distributor.start()-- self.heartbeat_task: Optional[asyncio.Task[Any]] = None- self._start_heartbeat()- self._closed = False-- def _start_heartbeat(self) -> None:- def _check_alive() -> None:- if not self.kernel_manager.is_alive():- LOGGER.debug(- "Closing session %s because kernel died",- self.initialization_id,- )- self.close()- print_()- print_tabbed(- red(- "The Python kernel for file "- f"{self.app_file_manager.filename} died unexpectedly."- )- )- print_()- self.close()-- # Start a heartbeat task, which checks if the kernel is alive- # every second-- async def _heartbeat() -> None:- while True:- await asyncio.sleep(1)- _check_alive()-- try:- loop = asyncio.get_event_loop()- self.heartbeat_task = loop.create_task(_heartbeat())- except RuntimeError:- # This can happen if there is no event loop running- self.heartbeat_task = None-- def try_interrupt(self) -> None:- """Try to interrupt the kernel."""- self.kernel_manager.interrupt_kernel()-- def put_control_request(- self,- request: requests.ControlRequest,- from_consumer_id: Optional[ConsumerId],- ) -> None:- """Put a control request in the control queue."""- self._queue_manager.control_queue.put(request)- if isinstance(request, SetUIElementValueRequest):- self._queue_manager.set_ui_element_queue.put(request)- # Propagate the control request to the room- if isinstance(request, (ExecuteMultipleRequest, SyncGraphRequest)):- if isinstance(request, ExecuteMultipleRequest):- cell_ids = request.cell_ids- codes = request.codes- else:- cell_ids = request.run_ids- codes = [request.cells[cell_id] for cell_id in cell_ids]- self.room.broadcast(- UpdateCellCodes(- cell_ids=cell_ids,- codes=codes,- # Not stale because we just ran the code- code_is_stale=False,- ),- except_consumer=from_consumer_id,- )- if len(cell_ids) == 1:- self.room.broadcast(- FocusCell(cell_id=cell_ids[0]),- except_consumer=from_consumer_id,- )- self.session_view.add_control_request(request)-- def put_completion_request(- self, request: requests.CodeCompletionRequest- ) -> None:- """Put a code completion request in the completion queue."""- self._queue_manager.completion_queue.put(request)-- def put_input(self, text: str) -> None:- """Put an input() request in the input queue."""- self._queue_manager.input_queue.put(text)- self.session_view.add_stdin(text)-- def disconnect_consumer(self, session_consumer: SessionConsumer) -> None:- """- Stop the session consumer but keep the kernel running.-- This will disconnect the main session consumer,- or a kiosk consumer.- """- self.room.remove_consumer(session_consumer)-- def maybe_disconnect_consumer(self) -> None:- """- Disconnect the main session consumer if it connected.- """- if self.room.main_consumer is not None:- self.disconnect_consumer(self.room.main_consumer)-- def connect_consumer(- self, session_consumer: SessionConsumer, *, main: bool- ) -> None:- """- Connect or resume the session with a new consumer.-- If its the main consumer and one already exists,- an exception is raised.- """- subscribe = session_consumer.on_start()- unsubscribe_consumer = self.message_distributor.add_consumer(subscribe)- self.room.add_consumer(- session_consumer,- unsubscribe_consumer,- session_consumer.consumer_id,- main=main,- )-- def get_current_state(self) -> SessionView:- """Return the current state of the session."""- return self.session_view-- def connection_state(self) -> ConnectionState:- """Return the connection state of the session."""- if self._closed:- return ConnectionState.CLOSED- if self.room.main_consumer is None:- return ConnectionState.ORPHANED- return self.room.main_consumer.connection_state()-- def write_operation(- self,- operation: MessageOperation,- from_consumer_id: Optional[ConsumerId],- ) -> None:- """Write an operation to the session consumer and the session view."""- self.session_view.add_operation(operation)- self.room.broadcast(operation, except_consumer=from_consumer_id)-- def close(self) -> None:- """- Close the session.-- This will close the session consumer, kernel, and all kiosk consumers.- """- if self._closed:- return-- self._closed = True- # Close the room- self.room.close()- # Close the kernel- self.message_distributor.stop()- if self.heartbeat_task:- self.heartbeat_task.cancel()- if self.session_cache_manager:- self.session_cache_manager.stop()- self.kernel_manager.close_kernel()-- def instantiate(- self,- request: InstantiateRequest,- *,- http_request: Optional[HTTPRequest],- ) -> None:- """Instantiate the app."""- execution_requests = tuple(- ExecutionRequest(- cell_id=cell_data.cell_id,- code=cell_data.code,- request=http_request,- )- for cell_data in self.app_file_manager.app.cell_manager.cell_data()- )-- self.put_control_request(- CreationRequest(- execution_requests=execution_requests,- set_ui_element_value_request=SetUIElementValueRequest(- object_ids=request.object_ids,- values=request.values,- token=str(uuid4()),- request=http_request,- ),- auto_run=request.auto_run,- request=http_request,- ),- from_consumer_id=None,- )-- def sync_session_view_from_cache(self) -> None:- """Sync the session view from a file.-- Overwrites the existing session view.- Mutates the existing session.- """- from marimo._version import __version__-- LOGGER.debug("Syncing session view from cache")- self.session_cache_manager = SessionCacheManager(- session_view=self.session_view,- path=self.app_file_manager.path,- interval=self.SESSION_CACHE_INTERVAL_SECONDS,- )-- app = self.app_file_manager.app- codes = tuple(- cell_data.code for cell_data in app.cell_manager.cell_data()- )- cell_ids = tuple(app.cell_manager.cell_ids())- key = SessionCacheKey(- codes=codes, marimo_version=__version__, cell_ids=cell_ids- )- self.session_view = self.session_cache_manager.read_session_view(key)- self.session_cache_manager.start()-- def __repr__(self) -> str:- return format_repr(- self,- {- "connection_state": self.connection_state(),- "room": self.room,- },- )---class SessionManager:- """Mapping from client session IDs to sessions.-- Maintains a mapping from client session IDs to client sessions;- there is exactly one session per client.-- The SessionManager also encapsulates state common to all sessions:- - the app filename- - the app mode (edit or run)- - the auth token- - the skew-protection token- """-- def __init__(- self,- *,- file_router: AppFileRouter,- mode: SessionMode,- development_mode: bool,- quiet: bool,- include_code: bool,- lsp_server: LspServer,- config_manager: MarimoConfigManager,- cli_args: SerializedCLIArgs,- argv: list[str] | None,- auth_token: Optional[AuthToken],- redirect_console_to_browser: bool,- ttl_seconds: Optional[int],- watch: bool = False,- ) -> None:- self.file_router = file_router- self.mode = mode- self.development_mode = development_mode- self.quiet = quiet- self.sessions: dict[SessionId, Session] = {}- self.include_code = include_code- self.ttl_seconds = ttl_seconds- self.lsp_server = lsp_server- self.file_change_handler = SessionFileChangeHandler(- self, config_manager- )- self.watcher_manager = FileWatcherManager()- self.watch = watch- self.recents = RecentFilesManager()- self.cli_args = cli_args- self.argv = argv- self.redirect_console_to_browser = redirect_console_to_browser-- # We should access the config_manager from the session if possible- # since this will contain config-level overrides- self._config_manager = config_manager-- def _get_code() -> str:- app = file_router.get_single_app_file_manager(- default_width=self._config_manager.default_width,- default_auto_download=self._config_manager.default_auto_download,- default_sql_output=self._config_manager.default_sql_output,- ).app- return "".join(code for code in app.cell_manager.codes())-- # Auth token and Skew-protection token- if mode == SessionMode.EDIT:- # In edit mode, if no auth token is provided,- # generate a random token- self.auth_token = (- AuthToken.random() if auth_token is None else auth_token- )- self.skew_protection_token = SkewProtectionToken.random()- else:- source_code = _get_code()- # Because run-mode is read-only and we could have multiple- # servers for the same app (going to sleep or autoscaling),- # we default to a token based on the app's code- self.auth_token = (- AuthToken.from_code(source_code)- if auth_token is None- else auth_token- )- self.skew_protection_token = SkewProtectionToken.from_code(- source_code- )-- def app_manager(self, key: MarimoFileKey) -> AppFileManager:- """- Get the app manager for the given key.- """- return self.file_router.get_file_manager(- key,- default_width=self._config_manager.default_width,- default_auto_download=self._config_manager.default_auto_download,- default_sql_output=self._config_manager.default_sql_output,- )-- def create_session(- self,- session_id: SessionId,- session_consumer: SessionConsumer,- query_params: SerializedQueryParams,- file_key: MarimoFileKey,- ) -> Session:- """Create a new session"""- LOGGER.debug("Creating new session for id %s", session_id)- if session_id not in self.sessions:- app_file_manager = self.file_router.get_file_manager(- file_key,- default_width=self._config_manager.default_width,- default_auto_download=self._config_manager.default_auto_download,- default_sql_output=self._config_manager.default_sql_output,- )-- if app_file_manager.path:- self.recents.touch(app_file_manager.path)-- session = Session.create(- initialization_id=file_key,- session_consumer=session_consumer,- mode=self.mode,- app_metadata=AppMetadata(- query_params=query_params,- filename=app_file_manager.path,- cli_args=self.cli_args,- argv=self.argv,- app_config=app_file_manager.app.config,- ),- app_file_manager=app_file_manager,- config_manager=self._config_manager,- virtual_files_supported=True,- redirect_console_to_browser=self.redirect_console_to_browser,- ttl_seconds=self.ttl_seconds,- )- self.sessions[session_id] = session-- # Start file watcher if enabled- if self.watch and app_file_manager.path:- self._start_file_watcher_for_session(session)-- return self.sessions[session_id]-- def _start_file_watcher_for_session(self, session: Session) -> None:- """Start a file watcher for a session."""- if not session.app_file_manager.path:- return-- async def on_file_changed(path: Path) -> None:- # Skip if the session does not relate to the file- if session.app_file_manager.path != await async_path.abspath(path):- return-- # Use the centralized file change handler- await self.file_change_handler.handle_file_change(- str(path), [session]- )-- session._unsubscribe_file_watcher_ = on_file_changed # type: ignore-- self.watcher_manager.add_callback(- Path(session.app_file_manager.path), on_file_changed- )-- def handle_file_rename_for_watch(- self, session_id: SessionId, prev_path: Optional[str], new_path: str- ) -> tuple[bool, Optional[str]]:- """Handle renaming a file for a session.-- Returns:- tuple[bool, Optional[str]]: (success, error_message)- """- session = self.get_session(session_id)- if not session:- return False, "Session not found"-- if not os.path.exists(new_path):- return False, f"File {new_path} does not exist"-- if not session.app_file_manager.path:- return False, "Session has no associated file"-- # Handle rename for session cache- if session.session_cache_manager:- session.session_cache_manager.rename_path(new_path)-- try:- if self.watch:- # Remove the old file watcher if it exists- if prev_path:- self.watcher_manager.remove_callback(- Path(prev_path),- session._unsubscribe_file_watcher_, # type: ignore- )-- # Add a watcher for the new path if needed- self._start_file_watcher_for_session(session)-- return True, None-- except Exception as e:- LOGGER.error(f"Error handling file rename: {e}")-- if self.watch:- self._start_file_watcher_for_session(session)- return False, str(e)-- async def handle_file_change(self, path: str) -> None:- """Handle a file change."""- await self.file_change_handler.handle_file_change(- path, list(self.sessions.values())- )-- def get_session(self, session_id: SessionId) -> Optional[Session]:- session = self.sessions.get(session_id)- if session:- return session-- # Search for kiosk sessions- for session in self.sessions.values():- if ConsumerId(session_id) in session.room.consumers.values():- return session-- return None-- def get_session_by_file_key(- self, file_key: MarimoFileKey- ) -> Optional[Session]:- for session in self.sessions.values():- if (- session.initialization_id == file_key- or session.app_file_manager.path == os.path.abspath(file_key)- ):- return session- return None-- def maybe_resume_session(- self, new_session_id: SessionId, file_key: MarimoFileKey- ) -> Optional[Session]:- """- Try to resume a session if one is resumable.- If it is resumable, return the session and update the session id.- """-- # If in run mode, only resume the session if it is orphaned and has- # the same session id, otherwise we want to create a new session- if self.mode == SessionMode.RUN:- maybe_session = self.get_session(new_session_id)- if (- maybe_session- and maybe_session.connection_state()- == ConnectionState.ORPHANED- ):- LOGGER.debug(- "Found a resumable RUN session: prev_id=%s",- new_session_id,- )- return maybe_session- return None-- # Cleanup sessions with dead kernels; materializing as a list because- # close_sessions mutates self.sessions- for session_id, session in list(self.sessions.items()):- task = session.kernel_manager.kernel_task- if task is not None and not task.is_alive():- self.close_session(session_id)-- # Should only return an orphaned session- sessions_with_the_same_file: dict[SessionId, Session] = {- session_id: session- for session_id, session in self.sessions.items()- if session.app_file_manager.path == os.path.abspath(file_key)- }-- if len(sessions_with_the_same_file) == 0:- return None- if len(sessions_with_the_same_file) > 1:- raise InvalidSessionException(- "Only one session should exist while editing"- )-- (session_id, session) = next(iter(sessions_with_the_same_file.items()))- connection_state = session.connection_state()- if connection_state == ConnectionState.ORPHANED:- LOGGER.debug(- f"Found a resumable EDIT session: prev_id={session_id}"- )- # Set new session and remove old session- self.sessions[new_session_id] = session- # If the ID is the same, we don't need to delete the old session- if new_session_id != session_id and session_id in self.sessions:- del self.sessions[session_id]- return session-- LOGGER.debug(- "Session is not resumable, current state: %s",- connection_state,- )- return None-- def any_clients_connected(self, key: MarimoFileKey) -> bool:- """Returns True if at least one client has an open socket."""- if key.startswith(AppFileRouter.NEW_FILE):- return False-- for session in self.sessions.values():- if session.connection_state() == ConnectionState.OPEN and (- session.app_file_manager.path == os.path.abspath(key)- ):- return True- return False-- async def start_lsp_server(self) -> None:- """Starts the lsp server if it is not already started.-- Doesn't start in run mode.- """- if self.mode == SessionMode.RUN:- LOGGER.warning("Cannot start LSP server in run mode")- return-- LOGGER.info("Starting LSP server...")- alert = await self.lsp_server.start()-- if alert is not None:- LOGGER.error(- f"LSP server startup failed: {alert.title} - {alert.description}"- )- for session in self.sessions.values():- session.write_operation(alert, from_consumer_id=None)- return- else:- LOGGER.info("LSP server started successfully")-- def close_session(self, session_id: SessionId) -> bool:- """Close a session and remove its file watcher if it has one."""- LOGGER.debug("Closing session %s", session_id)- session = self.get_session(session_id)- if session is None:- return False-- # Remove the file watcher callback for this session- if session.app_file_manager.path and self.watch:- self.watcher_manager.remove_callback(- Path(session.app_file_manager.path),- session._unsubscribe_file_watcher_, # type: ignore- )-- session.close()- if session_id in self.sessions:- del self.sessions[session_id]- return True-- def close_all_sessions(self) -> None:- LOGGER.debug("Closing all sessions (sessions: %s)", self.sessions)- for session in self.sessions.values():- session.close()- LOGGER.debug("Closed all sessions.")- self.sessions = {}-- def shutdown(self) -> None:- """Shutdown the session manager and stop all file watchers."""- LOGGER.debug("Shutting down")- self.close_all_sessions()- self.lsp_server.stop()- self.watcher_manager.stop_all()-- def should_send_code_to_frontend(self) -> bool:- """Returns True if the server can send messages to the frontend."""- return self.mode == SessionMode.EDIT or self.include_code-- def get_active_connection_count(self) -> int:- return len(- [- session- for session in self.sessions.values()- if session.connection_state() == ConnectionState.OPEN- ]- )---class SessionFileChangeHandler:- def __init__(- self,- session_manager: SessionManager,- config_manager: MarimoConfigManager,- ) -> None:- self.session_manager = session_manager- self.config_manager = config_manager- # Track ongoing file change operations to prevent duplicates- self._file_change_locks: dict[str, asyncio.Lock] = {}-- async def handle_file_change(- self, file_path: str, sessions: list[Session]- ) -> None:- """Handle file changes for marimo notebooks.-- This method reloads the notebook and sends appropriate operations- to the frontend when a marimo file is modified.- """- abs_file_path = await async_path.abspath(file_path)-- # Use a lock to prevent concurrent processing of the same file- if abs_file_path not in self._file_change_locks:- self._file_change_locks[abs_file_path] = asyncio.Lock()-- async with self._file_change_locks[abs_file_path]:- # Find all sessions associated with this file- sessions_for_file: list[Session] = []- for s in sessions:- if s.app_file_manager.path == abs_file_path:- sessions_for_file.append(s)-- if not sessions_for_file:- # No active session for this file- return-- if len(sessions_for_file) > 1:- LOGGER.error(- f"Only one session should exist for a file: {abs_file_path}"- )-- self._handle_file_change(- abs_file_path,- sessions_for_file[0],- )-- def _handle_file_change(- self,- file_path: str,- session: Session,- ) -> None:- LOGGER.debug(f"{file_path} was modified, handling {session}")-- # Check if the file content matches the last save- # to avoid reloading our own writes- if session.app_file_manager.file_content_matches_last_save():- LOGGER.debug(- f"File {file_path} content matches last save, skipping reload"- )- return-- # Reload the file manager to get the latest code- try:- changed_cell_ids = session.app_file_manager.reload()- except Exception as e:- # If there are syntax errors, we just skip- # and don't send the changes- LOGGER.error(f"Error loading file: {e}")- return-- # In run mode, we just call Reload()- if self.session_manager.mode == SessionMode.RUN:- session.write_operation(Reload(), from_consumer_id=None)- return-- # Get the latest codes- codes = list(session.app_file_manager.app.cell_manager.codes())- cell_ids = list(session.app_file_manager.app.cell_manager.cell_ids())-- LOGGER.info(- f"File changed: {file_path}. num_cell_ids: {len(cell_ids)}, num_codes: {len(codes)}, changed_cell_ids: {changed_cell_ids}"- )-- # Send the updated cell ids and codes to the frontend- session.write_operation(- UpdateCellIdsRequest(cell_ids=cell_ids),- from_consumer_id=None,- )-- # Check if we should auto-run cells based on config- watcher_on_save = self.config_manager.get_config()["runtime"][- "watcher_on_save"- ]- should_autorun = watcher_on_save == "autorun"- deleted = {- cell_id for cell_id in changed_cell_ids if cell_id not in cell_ids- }-- # Auto-run cells if configured- if should_autorun:- changed_cell_ids_list = list(changed_cell_ids - deleted)- cells = dict(zip(cell_ids, codes))-- session.put_control_request(- SyncGraphRequest(- cells=cells,- run_ids=changed_cell_ids_list,- delete_ids=list(deleted),- ),- from_consumer_id=None,- )- else:- for to_delete in deleted:- session.put_control_request(- DeleteCellRequest(cell_id=to_delete),- from_consumer_id=None,- )- session.write_operation(- UpdateCellCodes(- cell_ids=cell_ids,- codes=codes,- code_is_stale=True,- ),- from_consumer_id=None,- )---def send_message_to_consumer(- session: Session,- operation: MessageOperation,- consumer_id: Optional[ConsumerId],-) -> None:- if session.connection_state() == ConnectionState.OPEN:- for consumer, c_id in session.room.consumers.items():- if c_id == consumer_id:- consumer.write_operation(operation)diff --git a/marimo/_server/sessions/__init__.py b/marimo/_server/sessions/__init__.pynew file mode 100644index 0000000000..b5a7b414a0--- /dev/null+++ b/marimo/_server/sessions/__init__.py@@ -0,0 +1,30 @@+# Copyright 2024 Marimo. All rights reserved.+"""Session management for marimo server.++This module provides session management functionality including:+- Session lifecycle (creation, resumption, closure)+- Kernel management (process/thread management, interruption)+- Queue management (control, completion, input queues)+- Room management (broadcasting to multiple consumers)+- File change handling++All public APIs are re-exported from this module for backward compatibility.+"""++from __future__ import annotations++from marimo._server.sessions.session_manager import SessionManager+from marimo._server.sessions.types import (+ KernelManager,+ QueueManager,+ Session,+)+from marimo._server.sessions.utils import send_message_to_consumer++__all__ = [+ "Session",+ "SessionManager",+ "KernelManager",+ "QueueManager",+ "send_message_to_consumer",+]diff --git a/marimo/_server/sessions/managers.py b/marimo/_server/sessions/managers.pynew file mode 100644index 0000000000..33eec9aa2d--- /dev/null+++ b/marimo/_server/sessions/managers.py@@ -0,0 +1,305 @@+# Copyright 2024 Marimo. All rights reserved.+"""Queue and Kernel managers for session management.++This module contains the infrastructure components for managing+kernel processes/threads and their associated communication queues.+"""++from __future__ import annotations++import os+import queue+import signal+import sys+import threading+import time+from multiprocessing import Process, connection, get_context+from multiprocessing.queues import Queue as MPQueue+from typing import Any, Optional, Union+from uuid import uuid4++from marimo import _loggers+from marimo._ast.cell import CellConfig+from marimo._config.manager import MarimoConfigReader+from marimo._config.settings import GLOBAL_SETTINGS+from marimo._messaging.types import KernelMessage+from marimo._output.formatters.formatters import register_formatters+from marimo._runtime import requests, runtime+from marimo._runtime.requests import AppMetadata+from marimo._server.model import SessionMode+from marimo._server.sessions.types import KernelManager, QueueManager+from marimo._server.types import ProcessLike+from marimo._server.utils import print_+from marimo._types.ids import CellId_t+from marimo._utils.typed_connection import TypedConnection++LOGGER = _loggers.marimo_logger()+++class QueueManagerImpl(QueueManager):+ """Manages queues for a session."""++ def __init__(self, *, use_multiprocessing: bool):+ context = get_context("spawn") if use_multiprocessing else None++ # Control messages for the kernel (run, set UI element, set config, etc+ # ) are sent through the control queue+ self.control_queue: Union[+ MPQueue[requests.ControlRequest],+ queue.Queue[requests.ControlRequest],+ ] = context.Queue() if context is not None else queue.Queue()++ # Set UI element queues are stored in both the control queue and+ # this queue, so that the backend can merge/batch set-ui-element+ # requests.+ self.set_ui_element_queue: Union[+ MPQueue[requests.SetUIElementValueRequest],+ queue.Queue[requests.SetUIElementValueRequest],+ ] = context.Queue() if context is not None else queue.Queue()++ # Code completion requests are sent through a separate queue+ self.completion_queue: Union[+ MPQueue[requests.CodeCompletionRequest],+ queue.Queue[requests.CodeCompletionRequest],+ ] = context.Queue() if context is not None else queue.Queue()++ self.win32_interrupt_queue: (+ Union[MPQueue[bool], queue.Queue[bool]] | None+ )+ if sys.platform == "win32":+ self.win32_interrupt_queue = (+ context.Queue() if context is not None else queue.Queue()+ )+ else:+ self.win32_interrupt_queue = None++ # Input messages for the user's Python code are sent through the+ # input queue+ self.input_queue: Union[MPQueue[str], queue.Queue[str]] = (+ context.Queue(maxsize=1)+ if context is not None+ else queue.Queue(maxsize=1)+ )+ self.stream_queue: Optional[+ queue.Queue[Union[KernelMessage, None]]+ ] = None+ if not use_multiprocessing:+ self.stream_queue = queue.Queue()++ def close_queues(self) -> None:+ if isinstance(self.control_queue, MPQueue):+ # cancel join thread because we don't care if the queues still have+ # things in it: don't want to make the child process wait for the+ # queues to empty+ self.control_queue.cancel_join_thread()+ self.control_queue.close()+ else:+ # kernel thread cleans up read/write conn and IOloop handler on+ # exit; we don't join the thread because we don't want to block+ self.control_queue.put(requests.StopRequest())++ if isinstance(self.set_ui_element_queue, MPQueue):+ self.set_ui_element_queue.cancel_join_thread()+ self.set_ui_element_queue.close()++ if isinstance(self.input_queue, MPQueue):+ # again, don't make the child process wait for the queues to empty+ self.input_queue.cancel_join_thread()+ self.input_queue.close()++ if isinstance(self.completion_queue, MPQueue):+ self.completion_queue.cancel_join_thread()+ self.completion_queue.close()++ if isinstance(self.win32_interrupt_queue, MPQueue):+ self.win32_interrupt_queue.cancel_join_thread()+ self.win32_interrupt_queue.close()+++class KernelManagerImpl(KernelManager):+ def __init__(+ self,+ *,+ queue_manager: QueueManager,+ mode: SessionMode,+ configs: dict[CellId_t, CellConfig],+ app_metadata: AppMetadata,+ config_manager: MarimoConfigReader,+ virtual_files_supported: bool,+ redirect_console_to_browser: bool,+ ) -> None:+ self.kernel_task: Optional[ProcessLike | threading.Thread] = None+ self.queue_manager = queue_manager+ self.mode = mode+ self.configs = configs+ self.app_metadata = app_metadata+ self.config_manager = config_manager+ self.redirect_console_to_browser = redirect_console_to_browser++ # Only used in edit mode+ self._read_conn: Optional[TypedConnection[KernelMessage]] = None+ self._virtual_files_supported = virtual_files_supported++ def start_kernel(self) -> None:+ # We use a process in edit mode so that we can interrupt the app+ # with a SIGINT; we don't mind the additional memory consumption,+ # since there's only one client session+ is_edit_mode = self.mode == SessionMode.EDIT+ listener = None+ if is_edit_mode:+ # Need to use a socket for windows compatibility+ listener = connection.Listener(family="AF_INET")+ self.kernel_task = Process(+ target=runtime.launch_kernel,+ args=(+ self.queue_manager.control_queue,+ self.queue_manager.set_ui_element_queue,+ self.queue_manager.completion_queue,+ self.queue_manager.input_queue,+ # stream queue unused+ None,+ listener.address,+ is_edit_mode,+ self.configs,+ self.app_metadata,+ self.config_manager.get_config(hide_secrets=False),+ self._virtual_files_supported,+ self.redirect_console_to_browser,+ self.queue_manager.win32_interrupt_queue,+ self.profile_path,+ GLOBAL_SETTINGS.LOG_LEVEL,+ ),+ # The process can't be a daemon, because daemonic processes+ # can't create children+ # https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon # noqa: E501+ daemon=False,+ )+ else:+ # We use threads in run mode to minimize memory consumption;+ # launching a process would copy the entire program state,+ # which (as of writing) is around 150MB++ # We can't terminate threads, so we have to wait until they+ # naturally exit before cleaning up resources+ def launch_kernel_with_cleanup(*args: Any) -> None:+ runtime.launch_kernel(*args)++ # install formatter import hooks, which will be shared by all+ # threads (in edit mode, the single kernel process installs+ # formatters ...)+ register_formatters(theme=self.config_manager.theme)++ assert self.queue_manager.stream_queue is not None+ # Make threads daemons so killing the server immediately brings+ # down all client sessions+ self.kernel_task = threading.Thread(+ target=launch_kernel_with_cleanup,+ args=(+ self.queue_manager.control_queue,+ self.queue_manager.set_ui_element_queue,+ self.queue_manager.completion_queue,+ self.queue_manager.input_queue,+ self.queue_manager.stream_queue,+ # IPC not used in run mode+ None,+ is_edit_mode,+ self.configs,+ self.app_metadata,+ self.config_manager.get_config(hide_secrets=False),+ self._virtual_files_supported,+ self.redirect_console_to_browser,+ # win32 interrupt queue+ None,+ # profile path+ None,+ # log level+ GLOBAL_SETTINGS.LOG_LEVEL,+ ),+ # daemon threads can create child processes, unlike+ # daemon processes+ daemon=True,+ )++ self.kernel_task.start() # type: ignore+ if listener is not None:+ # First thing kernel does is connect to the socket, so it's safe to+ # call accept+ self._read_conn = TypedConnection[KernelMessage].of(+ listener.accept()+ )++ @property+ def profile_path(self) -> str | None:+ self._profile_path: str | None++ if hasattr(self, "_profile_path"):+ return self._profile_path++ profile_dir = GLOBAL_SETTINGS.PROFILE_DIR+ if profile_dir is not None:+ self._profile_path = os.path.join(+ profile_dir,+ (+ os.path.basename(self.app_metadata.filename) + str(uuid4())+ if self.app_metadata.filename is not None+ else str(uuid4())+ ),+ )+ else:+ self._profile_path = None+ return self._profile_path++ def is_alive(self) -> bool:+ return self.kernel_task is not None and self.kernel_task.is_alive()++ def interrupt_kernel(self) -> None:+ if self.kernel_task is None:+ return++ if isinstance(self.kernel_task, threading.Thread):+ # no interruptions in run mode+ return++ if self.kernel_task.pid is not None:+ q = self.queue_manager.win32_interrupt_queue+ if sys.platform == "win32" and q is not None:+ LOGGER.debug("Queueing interrupt request for kernel.")+ q.put_nowait(True)+ else:+ LOGGER.debug("Sending SIGINT to kernel")+ os.kill(self.kernel_task.pid, signal.SIGINT)++ def close_kernel(self) -> None:+ assert self.kernel_task is not None, "kernel not started"++ if isinstance(self.kernel_task, threading.Thread):+ # in run mode+ if self.kernel_task.is_alive():+ # We don't join the kernel thread because we don't want to server+ # to block on it finishing+ self.queue_manager.control_queue.put(requests.StopRequest())+ else:+ # otherwise we have something that is `ProcessLike`+ if self.profile_path is not None and self.kernel_task.is_alive():+ self.queue_manager.control_queue.put(requests.StopRequest())+ # Hack: Wait for kernel to exit and write out profile;+ # joining the process hangs, but not sure why.+ print_(+ "\tWriting profile statistics to",+ self.profile_path,+ " ...",+ )+ while not os.path.exists(self.profile_path):+ time.sleep(0.1)+ time.sleep(1)++ self.queue_manager.close_queues()+ if self.kernel_task.is_alive():+ self.kernel_task.terminate()+ if self._read_conn is not None:+ self._read_conn.close()++ @property+ def kernel_connection(self) -> TypedConnection[KernelMessage]:+ assert self._read_conn is not None, "connection not started"+ return self._read_conndiff --git a/marimo/_server/sessions/room.py b/marimo/_server/sessions/room.pynew file mode 100644index 0000000000..5aa13fda48--- /dev/null+++ b/marimo/_server/sessions/room.py@@ -0,0 +1,88 @@+# Copyright 2024 Marimo. All rights reserved.+"""Room management for broadcasting messages to multiple consumers.++A Room is a collection of SessionConsumers that can be used to broadcast+messages to all of them. Each room has one main consumer (the primary+session) and can have multiple kiosk consumers.+"""++from __future__ import annotations++from typing import Optional++from marimo import _loggers+from marimo._messaging.ops import MessageOperation+from marimo._server.model import ConnectionState, SessionConsumer+from marimo._types.ids import ConsumerId+from marimo._utils.disposable import Disposable++LOGGER = _loggers.marimo_logger()+++class Room:+ """+ A room is a collection of SessionConsumers+ that can be used to broadcast messages to all+ of them.+ """++ def __init__(self) -> None:+ self.main_consumer: Optional[SessionConsumer] = None+ self.consumers: dict[SessionConsumer, ConsumerId] = {}+ self.disposables: dict[SessionConsumer, Disposable] = {}++ @property+ def size(self) -> int:+ return len(self.consumers)++ def add_consumer(+ self,+ consumer: SessionConsumer,+ dispose: Disposable,+ consumer_id: ConsumerId,+ # Whether the consumer is the main session consumer+ # We only allow one main consumer, the rest are kiosk consumers+ main: bool,+ ) -> None:+ self.consumers[consumer] = consumer_id+ self.disposables[consumer] = dispose+ if main:+ assert self.main_consumer is None, (+ "Main session consumer already exists"+ )+ self.main_consumer = consumer++ def remove_consumer(self, consumer: SessionConsumer) -> None:+ if consumer not in self.consumers:+ LOGGER.debug(+ "Attempted to remove a consumer that was not in room."+ )+ return++ if consumer == self.main_consumer:+ self.main_consumer = None+ self.consumers.pop(consumer)+ disposable = self.disposables.pop(consumer)+ try:+ consumer.on_stop()+ finally:+ disposable.dispose()++ def broadcast(+ self,+ operation: MessageOperation,+ except_consumer: Optional[ConsumerId],+ ) -> None:+ for consumer in self.consumers:+ if consumer.consumer_id == except_consumer:+ continue+ if consumer.connection_state() == ConnectionState.OPEN:+ consumer.write_operation(operation)++ def close(self) -> None:+ for consumer in self.consumers:+ disposable = self.disposables.pop(consumer)+ consumer.on_stop()+ disposable.dispose()+ self.consumers = {}+ self.main_consumer = Nonediff --git a/marimo/_server/sessions/session.py b/marimo/_server/sessions/session.pynew file mode 100644index 0000000000..83ee8b8603--- /dev/null+++ b/marimo/_server/sessions/session.py@@ -0,0 +1,398 @@+# Copyright 2024 Marimo. All rights reserved.+"""Core Session class for managing client sessions.++Each session represents a single client connection with its own Python kernel+and websocket for bidirectional communication.+"""++from __future__ import annotations++import asyncio+from typing import TYPE_CHECKING, Any, Optional+from uuid import uuid4++from marimo import _loggers+from marimo._cli.print import red+from marimo._config.manager import MarimoConfigManager, ScriptConfigManager+from marimo._messaging.ops import (+ FocusCell,+ MessageOperation,+ UpdateCellCodes,+)+from marimo._messaging.types import KernelMessage+from marimo._runtime import requests+from marimo._runtime.requests import (+ AppMetadata,+ CreationRequest,+ ExecuteMultipleRequest,+ ExecutionRequest,+ HTTPRequest,+ SetUIElementValueRequest,+ SyncGraphRequest,+)+from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.models.models import InstantiateRequest+from marimo._server.notebook import AppFileManager+from marimo._server.session.serialize import (+ SessionCacheKey,+ SessionCacheManager,+)+from marimo._server.session.session_view import SessionView+from marimo._server.sessions.managers import (+ KernelManagerImpl,+ QueueManagerImpl,+)+from marimo._server.sessions.room import Room+from marimo._server.sessions.types import KernelManager, QueueManager, Session+from marimo._server.utils import print_, print_tabbed+from marimo._types.ids import ConsumerId+from marimo._utils.distributor import (+ ConnectionDistributor,+ QueueDistributor,+)+from marimo._utils.repr import format_repr++if TYPE_CHECKING:+ from collections.abc import Mapping++LOGGER = _loggers.marimo_logger()++_DEFAULT_TTL_SECONDS = 120++__all__ = ["Session", "SessionImpl"]+++class SessionImpl(Session):+ """A client session.++ Each session has its own Python kernel, for editing and running the app,+ and its own websocket, for sending messages to the client.+ """++ SESSION_CACHE_INTERVAL_SECONDS = 2++ @classmethod+ def create(+ cls,+ *,+ initialization_id: str,+ session_consumer: SessionConsumer,+ mode: SessionMode,+ app_metadata: AppMetadata,+ app_file_manager: AppFileManager,+ config_manager: MarimoConfigManager,+ virtual_files_supported: bool,+ redirect_console_to_browser: bool,+ ttl_seconds: Optional[int],+ ) -> Session:+ """+ Create a new session.+ """+ # Inherit config from the session manager+ # and override with any script-level config+ config_manager = config_manager.with_overrides(+ ScriptConfigManager(app_file_manager.path).get_config()+ )++ configs = app_file_manager.app.cell_manager.config_map()+ use_multiprocessing = mode == SessionMode.EDIT+ queue_manager = QueueManagerImpl(+ use_multiprocessing=use_multiprocessing+ )+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=mode,+ configs=configs,+ app_metadata=app_metadata,+ config_manager=config_manager,+ virtual_files_supported=virtual_files_supported,+ redirect_console_to_browser=redirect_console_to_browser,+ )++ return cls(+ initialization_id=initialization_id,+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=app_file_manager,+ config_manager=config_manager,+ ttl_seconds=ttl_seconds,+ )++ def __init__(+ self,+ initialization_id: str,+ session_consumer: SessionConsumer,+ queue_manager: QueueManager,+ kernel_manager: KernelManager,+ app_file_manager: AppFileManager,+ config_manager: MarimoConfigManager,+ ttl_seconds: Optional[int],+ ) -> None:+ """Initialize kernel and client connection to it."""+ # This is some unique ID that we can use to identify the session+ # in edit mode. We don't use the session_id because this can change if+ # the session is resumed+ self.initialization_id = initialization_id+ self.app_file_manager = app_file_manager+ self.room = Room()+ self._queue_manager = queue_manager+ self.kernel_manager = kernel_manager+ self.ttl_seconds = (+ ttl_seconds if ttl_seconds is not None else _DEFAULT_TTL_SECONDS+ )+ self.session_view = SessionView()+ self.session_cache_manager: SessionCacheManager | None = None+ self.config_manager = config_manager+ self.kernel_manager.start_kernel()+ # Reads from the kernel connection and distributes the+ # messages to each subscriber.+ self.message_distributor: (+ ConnectionDistributor[KernelMessage]+ | QueueDistributor[KernelMessage]+ )+ if self.kernel_manager.mode == SessionMode.EDIT:+ self.message_distributor = ConnectionDistributor[KernelMessage](+ self.kernel_manager.kernel_connection+ )+ else:+ q = self._queue_manager.stream_queue+ assert q is not None+ self.message_distributor = QueueDistributor[KernelMessage](queue=q)++ self.message_distributor.add_consumer(+ lambda msg: self.session_view.add_raw_operation(msg)+ )+ self.connect_consumer(session_consumer, main=True)+ self.message_distributor.start()++ self.heartbeat_task: Optional[asyncio.Task[Any]] = None+ self._start_heartbeat()+ self._closed = False++ @property+ def consumers(self) -> Mapping[SessionConsumer, ConsumerId]:+ """Get the consumers in the session."""+ return self.room.consumers++ def _start_heartbeat(self) -> None:+ def _check_alive() -> None:+ if not self.kernel_manager.is_alive():+ LOGGER.debug(+ "Closing session %s because kernel died",+ self.initialization_id,+ )+ self.close()+ print_()+ print_tabbed(+ red(+ "The Python kernel for file "+ f"{self.app_file_manager.filename} died unexpectedly."+ )+ )+ print_()+ self.close()++ # Start a heartbeat task, which checks if the kernel is alive+ # every second++ async def _heartbeat() -> None:+ while True:+ await asyncio.sleep(1)+ _check_alive()++ try:+ loop = asyncio.get_event_loop()+ self.heartbeat_task = loop.create_task(_heartbeat())+ except RuntimeError:+ # This can happen if there is no event loop running+ self.heartbeat_task = None++ def try_interrupt(self) -> None:+ """Try to interrupt the kernel."""+ self.kernel_manager.interrupt_kernel()++ def put_control_request(+ self,+ request: requests.ControlRequest,+ from_consumer_id: Optional[ConsumerId],+ ) -> None:+ """Put a control request in the control queue."""+ self._queue_manager.control_queue.put(request)+ if isinstance(request, SetUIElementValueRequest):+ self._queue_manager.set_ui_element_queue.put(request)+ # Propagate the control request to the room+ if isinstance(request, (ExecuteMultipleRequest, SyncGraphRequest)):+ if isinstance(request, ExecuteMultipleRequest):+ cell_ids = request.cell_ids+ codes = request.codes+ else:+ cell_ids = request.run_ids+ codes = [request.cells[cell_id] for cell_id in cell_ids]+ self.room.broadcast(+ UpdateCellCodes(+ cell_ids=cell_ids,+ codes=codes,+ # Not stale because we just ran the code+ code_is_stale=False,+ ),+ except_consumer=from_consumer_id,+ )+ if len(cell_ids) == 1:+ self.room.broadcast(+ FocusCell(cell_id=cell_ids[0]),+ except_consumer=from_consumer_id,+ )+ self.session_view.add_control_request(request)++ def put_completion_request(+ self, request: requests.CodeCompletionRequest+ ) -> None:+ """Put a code completion request in the completion queue."""+ self._queue_manager.completion_queue.put(request)++ def put_input(self, text: str) -> None:+ """Put an input() request in the input queue."""+ self._queue_manager.input_queue.put(text)+ self.session_view.add_stdin(text)++ def disconnect_consumer(self, session_consumer: SessionConsumer) -> None:+ """+ Stop the session consumer but keep the kernel running.++ This will disconnect the main session consumer,+ or a kiosk consumer.+ """+ self.room.remove_consumer(session_consumer)++ def maybe_disconnect_consumer(self) -> None:+ """+ Disconnect the main session consumer if it connected.+ """+ if self.room.main_consumer is not None:+ self.disconnect_consumer(self.room.main_consumer)++ def connect_consumer(+ self, session_consumer: SessionConsumer, *, main: bool+ ) -> None:+ """+ Connect or resume the session with a new consumer.++ If its the main consumer and one already exists,+ an exception is raised.+ """+ subscribe = session_consumer.on_start()+ unsubscribe_consumer = self.message_distributor.add_consumer(subscribe)+ self.room.add_consumer(+ session_consumer,+ unsubscribe_consumer,+ session_consumer.consumer_id,+ main=main,+ )++ def get_current_state(self) -> SessionView:+ """Return the current state of the session."""+ return self.session_view++ def connection_state(self) -> ConnectionState:+ """Return the connection state of the session."""+ if self._closed:+ return ConnectionState.CLOSED+ if self.room.main_consumer is None:+ return ConnectionState.ORPHANED+ return self.room.main_consumer.connection_state()++ def write_operation(+ self,+ operation: MessageOperation,+ from_consumer_id: Optional[ConsumerId],+ ) -> None:+ """Write an operation to the session consumer and the session view."""+ self.session_view.add_operation(operation)+ self.room.broadcast(operation, except_consumer=from_consumer_id)++ def close(self) -> None:+ """+ Close the session.++ This will close the session consumer, kernel, and all kiosk consumers.+ """+ if self._closed:+ return++ self._closed = True+ # Close the room+ self.room.close()+ # Close the kernel+ self.message_distributor.stop()+ if self.heartbeat_task:+ self.heartbeat_task.cancel()+ if self.session_cache_manager:+ self.session_cache_manager.stop()+ self.kernel_manager.close_kernel()++ def instantiate(+ self,+ request: InstantiateRequest,+ *,+ http_request: Optional[HTTPRequest],+ ) -> None:+ """Instantiate the app."""+ execution_requests = tuple(+ ExecutionRequest(+ cell_id=cell_data.cell_id,+ code=cell_data.code,+ request=http_request,+ )+ for cell_data in self.app_file_manager.app.cell_manager.cell_data()+ )++ self.put_control_request(+ CreationRequest(+ execution_requests=execution_requests,+ set_ui_element_value_request=SetUIElementValueRequest(+ object_ids=request.object_ids,+ values=request.values,+ token=str(uuid4()),+ request=http_request,+ ),+ auto_run=request.auto_run,+ request=http_request,+ ),+ from_consumer_id=None,+ )++ def sync_session_view_from_cache(self) -> None:+ """Sync the session view from a file.++ Overwrites the existing session view.+ Mutates the existing session.+ """+ from marimo._version import __version__++ LOGGER.debug("Syncing session view from cache")+ self.session_cache_manager = SessionCacheManager(+ session_view=self.session_view,+ path=self.app_file_manager.path,+ interval=self.SESSION_CACHE_INTERVAL_SECONDS,+ )++ app = self.app_file_manager.app+ codes = tuple(+ cell_data.code for cell_data in app.cell_manager.cell_data()+ )+ cell_ids = tuple(app.cell_manager.cell_ids())+ key = SessionCacheKey(+ codes=codes, marimo_version=__version__, cell_ids=cell_ids+ )+ self.session_view = self.session_cache_manager.read_session_view(key)+ self.session_cache_manager.start()++ def __repr__(self) -> str:+ return format_repr(+ self,+ {+ "connection_state": self.connection_state(),+ "room": self.room,+ },+ )diff --git a/marimo/_server/sessions/session_manager.py b/marimo/_server/sessions/session_manager.pynew file mode 100644index 0000000000..bba5bd37e6--- /dev/null+++ b/marimo/_server/sessions/session_manager.py@@ -0,0 +1,544 @@+# Copyright 2024 Marimo. All rights reserved.+"""Session manager for coordinating multiple sessions.++The SessionManager maintains a mapping from client session IDs to sessions+and encapsulates state common to all sessions including auth tokens,+file watching, and LSP server management.+"""++from __future__ import annotations++import asyncio+import os+from pathlib import Path+from typing import Optional++from marimo import _loggers+from marimo._config.manager import MarimoConfigManager+from marimo._messaging.ops import (+ Reload,+ UpdateCellCodes,+ UpdateCellIdsRequest,+)+from marimo._runtime.requests import (+ DeleteCellRequest,+ SerializedCLIArgs,+ SerializedQueryParams,+ SyncGraphRequest,+)+from marimo._server.exceptions import InvalidSessionException+from marimo._server.file_router import AppFileRouter, MarimoFileKey+from marimo._server.lsp import LspServer+from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.notebook import AppFileManager+from marimo._server.recents import RecentFilesManager+from marimo._server.sessions.session import Session, SessionImpl+from marimo._server.tokens import AuthToken, SkewProtectionToken+from marimo._types.ids import ConsumerId, SessionId+from marimo._utils import async_path+from marimo._utils.file_watcher import FileWatcherManager++LOGGER = _loggers.marimo_logger()+++class SessionManager:+ """Mapping from client session IDs to sessions.++ Maintains a mapping from client session IDs to client sessions;+ there is exactly one session per client.++ The SessionManager also encapsulates state common to all sessions:+ - the app filename+ - the app mode (edit or run)+ - the auth token+ - the skew-protection token+ """++ def __init__(+ self,+ *,+ file_router: AppFileRouter,+ mode: SessionMode,+ quiet: bool,+ include_code: bool,+ lsp_server: LspServer,+ config_manager: MarimoConfigManager,+ cli_args: SerializedCLIArgs,+ argv: list[str] | None,+ auth_token: Optional[AuthToken],+ redirect_console_to_browser: bool,+ ttl_seconds: Optional[int],+ watch: bool = False,+ ) -> None:+ self.file_router = file_router+ self.mode = mode+ self.quiet = quiet+ self.sessions: dict[SessionId, Session] = {}+ self.include_code = include_code+ self.ttl_seconds = ttl_seconds+ self.lsp_server = lsp_server+ self.file_change_handler = SessionFileChangeHandler(+ self, config_manager+ )+ self.watcher_manager = FileWatcherManager()+ self.watch = watch+ self.recents = RecentFilesManager()+ self.cli_args = cli_args+ self.argv = argv+ self.redirect_console_to_browser = redirect_console_to_browser++ # We should access the config_manager from the session if possible+ # since this will contain config-level overrides+ self._config_manager = config_manager++ def _get_code() -> str:+ app = file_router.get_single_app_file_manager(+ default_width=self._config_manager.default_width,+ default_auto_download=self._config_manager.default_auto_download,+ default_sql_output=self._config_manager.default_sql_output,+ ).app+ return "".join(code for code in app.cell_manager.codes())++ # Auth token and Skew-protection token+ if mode == SessionMode.EDIT:+ # In edit mode, if no auth token is provided,+ # generate a random token+ self.auth_token = (+ AuthToken.random() if auth_token is None else auth_token+ )+ self.skew_protection_token = SkewProtectionToken.random()+ else:+ source_code = _get_code()+ # Because run-mode is read-only and we could have multiple+ # servers for the same app (going to sleep or autoscaling),+ # we default to a token based on the app's code+ self.auth_token = (+ AuthToken.from_code(source_code)+ if auth_token is None+ else auth_token+ )+ self.skew_protection_token = SkewProtectionToken.from_code(+ source_code+ )++ def app_manager(self, key: MarimoFileKey) -> AppFileManager:+ """+ Get the app manager for the given key.+ """+ return self.file_router.get_file_manager(+ key,+ default_width=self._config_manager.default_width,+ default_auto_download=self._config_manager.default_auto_download,+ default_sql_output=self._config_manager.default_sql_output,+ )++ def create_session(+ self,+ session_id: SessionId,+ session_consumer: SessionConsumer,+ query_params: SerializedQueryParams,+ file_key: MarimoFileKey,+ ) -> Session:+ """Create a new session"""+ LOGGER.debug("Creating new session for id %s", session_id)+ if session_id not in self.sessions:+ app_file_manager = self.file_router.get_file_manager(+ file_key,+ default_width=self._config_manager.default_width,+ default_auto_download=self._config_manager.default_auto_download,+ default_sql_output=self._config_manager.default_sql_output,+ )++ if app_file_manager.path:+ self.recents.touch(app_file_manager.path)++ from marimo._runtime.requests import AppMetadata++ session = SessionImpl.create(+ initialization_id=file_key,+ session_consumer=session_consumer,+ mode=self.mode,+ app_metadata=AppMetadata(+ query_params=query_params,+ filename=app_file_manager.path,+ cli_args=self.cli_args,+ argv=self.argv,+ app_config=app_file_manager.app.config,+ ),+ app_file_manager=app_file_manager,+ config_manager=self._config_manager,+ virtual_files_supported=True,+ redirect_console_to_browser=self.redirect_console_to_browser,+ ttl_seconds=self.ttl_seconds,+ )+ self.sessions[session_id] = session++ # Start file watcher if enabled+ if self.watch and app_file_manager.path:+ self._start_file_watcher_for_session(session)++ return self.sessions[session_id]++ def _start_file_watcher_for_session(self, session: Session) -> None:+ """Start a file watcher for a session."""+ if not session.app_file_manager.path:+ return++ async def on_file_changed(path: Path) -> None:+ # Skip if the session does not relate to the file+ if session.app_file_manager.path != await async_path.abspath(path):+ return++ # Use the centralized file change handler+ await self.file_change_handler.handle_file_change(+ str(path), [session]+ )++ session._unsubscribe_file_watcher_ = on_file_changed # type: ignore++ self.watcher_manager.add_callback(+ Path(session.app_file_manager.path), on_file_changed+ )++ def handle_file_rename_for_watch(+ self, session_id: SessionId, prev_path: Optional[str], new_path: str+ ) -> tuple[bool, Optional[str]]:+ """Handle renaming a file for a session.++ Returns:+ tuple[bool, Optional[str]]: (success, error_message)+ """+ session = self.get_session(session_id)+ if not session:+ return False, "Session not found"++ if not os.path.exists(new_path):+ return False, f"File {new_path} does not exist"++ if not session.app_file_manager.path:+ return False, "Session has no associated file"++ # Handle rename for session cache+ if session.session_cache_manager:+ session.session_cache_manager.rename_path(new_path)++ try:+ if self.watch:+ # Remove the old file watcher if it exists+ if prev_path:+ self.watcher_manager.remove_callback(+ Path(prev_path),+ session._unsubscribe_file_watcher_, # type: ignore+ )++ # Add a watcher for the new path if needed+ self._start_file_watcher_for_session(session)++ return True, None++ except Exception as e:+ LOGGER.error(f"Error handling file rename: {e}")++ if self.watch:+ self._start_file_watcher_for_session(session)+ return False, str(e)++ async def handle_file_change(self, path: str) -> None:+ """Handle a file change."""+ await self.file_change_handler.handle_file_change(+ path, list(self.sessions.values())+ )++ def get_session(self, session_id: SessionId) -> Optional[Session]:+ session = self.sessions.get(session_id)+ if session:+ return session++ # Search for kiosk sessions+ for session in self.sessions.values():+ if ConsumerId(session_id) in session.consumers.values():+ return session++ return None++ def get_session_by_file_key(+ self, file_key: MarimoFileKey+ ) -> Optional[Session]:+ for session in self.sessions.values():+ if (+ session.initialization_id == file_key+ or session.app_file_manager.path == os.path.abspath(file_key)+ ):+ return session+ return None++ def maybe_resume_session(+ self, new_session_id: SessionId, file_key: MarimoFileKey+ ) -> Optional[Session]:+ """+ Try to resume a session if one is resumable.+ If it is resumable, return the session and update the session id.+ """++ # If in run mode, only resume the session if it is orphaned and has+ # the same session id, otherwise we want to create a new session+ if self.mode == SessionMode.RUN:+ maybe_session = self.get_session(new_session_id)+ if (+ maybe_session+ and maybe_session.connection_state()+ == ConnectionState.ORPHANED+ ):+ LOGGER.debug(+ "Found a resumable RUN session: prev_id=%s",+ new_session_id,+ )+ return maybe_session+ return None++ # Cleanup sessions with dead kernels; materializing as a list because+ # close_sessions mutates self.sessions+ for session_id, session in list(self.sessions.items()):+ task = session.kernel_manager.kernel_task+ if task is not None and not task.is_alive():+ self.close_session(session_id)++ # Should only return an orphaned session+ sessions_with_the_same_file: dict[SessionId, Session] = {+ session_id: session+ for session_id, session in self.sessions.items()+ if session.app_file_manager.path == os.path.abspath(file_key)+ }++ if len(sessions_with_the_same_file) == 0:+ return None+ if len(sessions_with_the_same_file) > 1:+ raise InvalidSessionException(+ "Only one session should exist while editing"+ )++ (session_id, session) = next(iter(sessions_with_the_same_file.items()))+ connection_state = session.connection_state()+ if connection_state == ConnectionState.ORPHANED:+ LOGGER.debug(+ f"Found a resumable EDIT session: prev_id={session_id}"+ )+ # Set new session and remove old session+ self.sessions[new_session_id] = session+ # If the ID is the same, we don't need to delete the old session+ if new_session_id != session_id and session_id in self.sessions:+ del self.sessions[session_id]+ return session++ LOGGER.debug(+ "Session is not resumable, current state: %s",+ connection_state,+ )+ return None++ def any_clients_connected(self, key: MarimoFileKey) -> bool:+ """Returns True if at least one client has an open socket."""+ if key.startswith(AppFileRouter.NEW_FILE):+ return False++ for session in self.sessions.values():+ if session.connection_state() == ConnectionState.OPEN and (+ session.app_file_manager.path == os.path.abspath(key)+ ):+ return True+ return False++ async def start_lsp_server(self) -> None:+ """Starts the lsp server if it is not already started.++ Doesn't start in run mode.+ """+ if self.mode == SessionMode.RUN:+ LOGGER.warning("Cannot start LSP server in run mode")+ return++ LOGGER.info("Starting LSP server...")+ alert = await self.lsp_server.start()++ if alert is not None:+ LOGGER.error(+ f"LSP server startup failed: {alert.title} - {alert.description}"+ )+ for session in self.sessions.values():+ session.write_operation(alert, from_consumer_id=None)+ return+ else:+ LOGGER.info("LSP server started successfully")++ def close_session(self, session_id: SessionId) -> bool:+ """Close a session and remove its file watcher if it has one."""+ LOGGER.debug("Closing session %s", session_id)+ session = self.get_session(session_id)+ if session is None:+ return False++ # Remove the file watcher callback for this session+ if session.app_file_manager.path and self.watch:+ self.watcher_manager.remove_callback(+ Path(session.app_file_manager.path),+ session._unsubscribe_file_watcher_, # type: ignore+ )++ session.close()+ if session_id in self.sessions:+ del self.sessions[session_id]+ return True++ def close_all_sessions(self) -> None:+ LOGGER.debug("Closing all sessions (sessions: %s)", self.sessions)+ for session in self.sessions.values():+ session.close()+ LOGGER.debug("Closed all sessions.")+ self.sessions = {}++ def shutdown(self) -> None:+ """Shutdown the session manager and stop all file watchers."""+ LOGGER.debug("Shutting down")+ self.close_all_sessions()+ self.lsp_server.stop()+ self.watcher_manager.stop_all()++ def should_send_code_to_frontend(self) -> bool:+ """Returns True if the server can send messages to the frontend."""+ return self.mode == SessionMode.EDIT or self.include_code++ def get_active_connection_count(self) -> int:+ return len(+ [+ session+ for session in self.sessions.values()+ if session.connection_state() == ConnectionState.OPEN+ ]+ )+++class SessionFileChangeHandler:+ def __init__(+ self,+ session_manager: SessionManager,+ config_manager: MarimoConfigManager,+ ) -> None:+ self.session_manager = session_manager+ self.config_manager = config_manager+ # Track ongoing file change operations to prevent duplicates+ self._file_change_locks: dict[str, asyncio.Lock] = {}++ async def handle_file_change(+ self, file_path: str, sessions: list[Session]+ ) -> None:+ """Handle file changes for marimo notebooks.++ This method reloads the notebook and sends appropriate operations+ to the frontend when a marimo file is modified.+ """+ abs_file_path = await async_path.abspath(file_path)++ # Use a lock to prevent concurrent processing of the same file+ if abs_file_path not in self._file_change_locks:+ self._file_change_locks[abs_file_path] = asyncio.Lock()++ async with self._file_change_locks[abs_file_path]:+ # Find all sessions associated with this file+ sessions_for_file: list[Session] = []+ for s in sessions:+ if s.app_file_manager.path == abs_file_path:+ sessions_for_file.append(s)++ if not sessions_for_file:+ # No active session for this file+ return++ if len(sessions_for_file) > 1:+ LOGGER.error(+ f"Only one session should exist for a file: {abs_file_path}"+ )++ self._handle_file_change(+ abs_file_path,+ sessions_for_file[0],+ )++ def _handle_file_change(+ self,+ file_path: str,+ session: Session,+ ) -> None:+ LOGGER.debug(f"{file_path} was modified, handling {session}")++ # Check if the file content matches the last save+ # to avoid reloading our own writes+ if session.app_file_manager.file_content_matches_last_save():+ LOGGER.debug(+ f"File {file_path} content matches last save, skipping reload"+ )+ return++ # Reload the file manager to get the latest code+ try:+ changed_cell_ids = session.app_file_manager.reload()+ except Exception as e:+ # If there are syntax errors, we just skip+ # and don't send the changes+ LOGGER.error(f"Error loading file: {e}")+ return++ # In run mode, we just call Reload()+ if self.session_manager.mode == SessionMode.RUN:+ session.write_operation(Reload(), from_consumer_id=None)+ return++ # Get the latest codes+ codes = list(session.app_file_manager.app.cell_manager.codes())+ cell_ids = list(session.app_file_manager.app.cell_manager.cell_ids())++ LOGGER.info(+ f"File changed: {file_path}. num_cell_ids: {len(cell_ids)}, num_codes: {len(codes)}, changed_cell_ids: {changed_cell_ids}"+ )++ # Send the updated cell ids and codes to the frontend+ session.write_operation(+ UpdateCellIdsRequest(cell_ids=cell_ids),+ from_consumer_id=None,+ )++ # Check if we should auto-run cells based on config+ watcher_on_save = self.config_manager.get_config()["runtime"][+ "watcher_on_save"+ ]+ should_autorun = watcher_on_save == "autorun"+ deleted = {+ cell_id for cell_id in changed_cell_ids if cell_id not in cell_ids+ }++ # Auto-run cells if configured+ if should_autorun:+ changed_cell_ids_list = list(changed_cell_ids - deleted)+ cells = dict(zip(cell_ids, codes))++ session.put_control_request(+ SyncGraphRequest(+ cells=cells,+ run_ids=changed_cell_ids_list,+ delete_ids=list(deleted),+ ),+ from_consumer_id=None,+ )+ else:+ for to_delete in deleted:+ session.put_control_request(+ DeleteCellRequest(cell_id=to_delete),+ from_consumer_id=None,+ )+ session.write_operation(+ UpdateCellCodes(+ cell_ids=cell_ids,+ codes=codes,+ code_is_stale=True,+ ),+ from_consumer_id=None,+ )diff --git a/marimo/_server/sessions/types.py b/marimo/_server/sessions/types.pynew file mode 100644index 0000000000..6840e562ba--- /dev/null+++ b/marimo/_server/sessions/types.py@@ -0,0 +1,172 @@+# Copyright 2024 Marimo. All rights reserved.+"""Protocol definitions for session management components.++These protocols define the public interfaces for all major session management+classes, enabling type checking, easier testing, and potential alternative+implementations.+"""++from __future__ import annotations++from typing import TYPE_CHECKING, Any, Optional, Protocol, Union++from marimo._messaging.ops import MessageOperation+from marimo._messaging.types import KernelMessage+from marimo._runtime import requests+from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.notebook.file_manager import AppFileManager+from marimo._server.session.session_view import SessionView+from marimo._server.types import ProcessLike, QueueType+from marimo._types.ids import ConsumerId+from marimo._utils.typed_connection import TypedConnection++if TYPE_CHECKING:+ import threading+ from collections.abc import Mapping++ from marimo._config.manager import MarimoConfigManager+ from marimo._server.session.serialize import SessionCacheManager+ from marimo._utils.distributor import (+ ConnectionDistributor,+ QueueDistributor,+ )+++class QueueManager(Protocol):+ """Protocol for queue management."""++ control_queue: QueueType[requests.ControlRequest]+ set_ui_element_queue: QueueType[requests.SetUIElementValueRequest]+ completion_queue: QueueType[requests.CodeCompletionRequest]+ input_queue: QueueType[str]+ stream_queue: Optional[QueueType[Union[KernelMessage, None]]]+ win32_interrupt_queue: QueueType[bool] | None++ def close_queues(self) -> None:+ """Close all queues."""+ ...+++class KernelManager(Protocol):+ """Protocol for kernel management."""++ kernel_task: Optional[Union[ProcessLike, threading.Thread]]+ mode: SessionMode++ def start_kernel(self) -> None:+ """Start the kernel process or thread."""+ ...++ def is_alive(self) -> bool:+ """Check if the kernel is still running."""+ ...++ def interrupt_kernel(self) -> None:+ """Interrupt the running kernel."""+ ...++ def close_kernel(self) -> None:+ """Close the kernel and clean up resources."""+ ...++ @property+ def kernel_connection(self) -> TypedConnection[KernelMessage]:+ """Get the kernel connection for reading messages."""+ ...++ @property+ def profile_path(self) -> str | None:+ """Get the profile path for the kernel."""+ ...+++class Session(Protocol):+ """Protocol for session management."""++ initialization_id: str+ session_view: SessionView+ app_file_manager: AppFileManager+ config_manager: MarimoConfigManager++ # TODO: don't expose these internals+ session_cache_manager: Optional[SessionCacheManager]+ message_distributor: Union[+ ConnectionDistributor[KernelMessage],+ QueueDistributor[KernelMessage],+ ]+ kernel_manager: KernelManager+ ttl_seconds: int++ @property+ def consumers(self) -> Mapping[SessionConsumer, ConsumerId]:+ """Get the consumers in the session."""+ ...++ def try_interrupt(self) -> None:+ """Try to interrupt the kernel."""+ ...++ def put_control_request(+ self,+ request: requests.ControlRequest,+ from_consumer_id: Optional[ConsumerId],+ ) -> None:+ """Put a control request in the control queue."""+ ...++ def put_completion_request(+ self, request: requests.CodeCompletionRequest+ ) -> None:+ """Put a code completion request in the completion queue."""+ ...++ def put_input(self, text: str) -> None:+ """Put an input() request in the input queue."""+ ...++ def disconnect_consumer(self, session_consumer: SessionConsumer) -> None:+ """Stop the session consumer but keep the kernel running."""+ ...++ def maybe_disconnect_consumer(self) -> None:+ """Disconnect the main session consumer if it connected."""+ ...++ def connect_consumer(+ self, session_consumer: SessionConsumer, *, main: bool+ ) -> None:+ """Connect or resume the session with a new consumer."""+ ...++ def get_current_state(self) -> SessionView:+ """Return the current state of the session."""+ ...++ def connection_state(self) -> ConnectionState:+ """Return the connection state of the session."""+ ...++ def write_operation(+ self,+ operation: MessageOperation,+ from_consumer_id: Optional[ConsumerId],+ ) -> None:+ """Write an operation to the session consumer and the session view."""+ ...++ def instantiate(+ self,+ request: Any,+ *,+ http_request: Optional[requests.HTTPRequest],+ ) -> None:+ """Instantiate the app."""+ ...++ def sync_session_view_from_cache(self) -> None:+ """Sync the session view from cache."""+ ...++ def close(self) -> None:+ """Close the session."""+ ...diff --git a/marimo/_server/sessions/utils.py b/marimo/_server/sessions/utils.pynew file mode 100644index 0000000000..0bc3e2b5f1--- /dev/null+++ b/marimo/_server/sessions/utils.py@@ -0,0 +1,25 @@+# Copyright 2024 Marimo. All rights reserved.+"""Utility functions for session management."""++from __future__ import annotations++from typing import TYPE_CHECKING, Optional++from marimo._server.model import ConnectionState+from marimo._types.ids import ConsumerId++if TYPE_CHECKING:+ from marimo._messaging.ops import MessageOperation+ from marimo._server.sessions.session import Session+++def send_message_to_consumer(+ session: Session,+ operation: MessageOperation,+ consumer_id: Optional[ConsumerId],+) -> None:+ """Send a message operation to a specific consumer in a session."""+ if session.connection_state() == ConnectionState.OPEN:+ for consumer, c_id in session.consumers.items():+ if c_id == consumer_id:+ consumer.write_operation(operation)diff --git a/marimo/_server/start.py b/marimo/_server/start.pyindex 21a912a2a7..71c0271344 100644--- a/marimo/_server/start.py+++ b/marimo/_server/start.py@@ -226,7 +226,6 @@ def start(session_manager = SessionManager(file_router=file_router,mode=mode,- development_mode=development_mode,quiet=quiet,include_code=include_code,ttl_seconds=ttl_seconds,diff --git a/marimo/_server/types.py b/marimo/_server/types.pyindex f2b570ccab..5b2b636ee6 100644--- a/marimo/_server/types.py+++ b/marimo/_server/types.py@@ -1,7 +1,7 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations-from typing import Protocol, TypeVar, Union+from typing import Optional, Protocol, TypeVar, UnionT = TypeVar("T")@@ -31,9 +31,13 @@ class QueueType(Protocol[T]):class ProcessLike(Protocol):+ """Protocol for process-like objects."""+@propertydef pid(self) -> int | None: ...def is_alive(self) -> bool: ...def terminate(self) -> None: ...++ def join(self, timeout: Optional[float] = None) -> None: ...diff --git a/marimo/_utils/distributor.py b/marimo/_utils/distributor.pyindex a1471c3330..e4824c5633 100644--- a/marimo/_utils/distributor.py+++ b/marimo/_utils/distributor.py@@ -4,15 +4,13 @@ from __future__ import annotationsimport asyncioimport threadingimport time-from typing import TYPE_CHECKING, Callable, Generic, TypeVar, Union+from typing import Callable, Generic, TypeVar, Unionfrom marimo import _loggers+from marimo._server.types import QueueTypefrom marimo._utils.disposable import Disposablefrom marimo._utils.typed_connection import TypedConnection-if TYPE_CHECKING:- import queue-LOGGER = _loggers.marimo_logger()T = TypeVar("T")@@ -95,7 +93,7 @@ class ConnectionDistributor(Generic[T]):class QueueDistributor(Generic[T]):- def __init__(self, queue: queue.Queue[Union[T, None]]) -> None:+ def __init__(self, queue: QueueType[Union[T, None]]) -> None:self.consumers: list[Consumer[T]] = []# distributor uses None as a signal to stopself.queue = queuediff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.pyindex c528ced046..f5966ec28e 100644--- a/tests/_server/api/endpoints/test_ws.py+++ b/tests/_server/api/endpoints/test_ws.py@@ -331,7 +331,7 @@ async def test_connects_to_existing_session_with_same_file(# Check in the same roomsession_manager = get_session_manager(client)assert len(session_manager.sessions) == 1- assert session_manager.sessions["123"].room.size == 2+ assert len(session_manager.sessions["123"].consumers) == 2data2 = websocket2.receive_json()assert_parse_ready_response(data2)diff --git a/tests/_server/mocks.py b/tests/_server/mocks.pyindex 4d7496856b..b883cf266a 100644--- a/tests/_server/mocks.py+++ b/tests/_server/mocks.py@@ -9,7 +9,7 @@ from marimo._config.manager import get_default_config_managerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.lsp import NoopLspServerfrom marimo._server.model import SessionMode-from marimo._server.sessions import SessionManager+from marimo._server.sessions.session_manager import SessionManagerfrom marimo._server.tokens import AuthToken, SkewProtectionTokenfrom marimo._utils.marimo_path import MarimoPath@@ -50,7 +50,6 @@ if __name__ == "__main__":sm = SessionManager(file_router=AppFileRouter.from_filename(MarimoPath(temp_file.name)),mode=SessionMode.EDIT,- development_mode=False,quiet=False,include_code=True,lsp_server=lsp_server,diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.pyindex d2053d8c6a..49800365dd 100644--- a/tests/_server/test_session_manager.py+++ b/tests/_server/test_session_manager.py@@ -44,7 +44,6 @@ def session_manager():return SessionManager(file_router=AppFileRouter.new_file(),mode=SessionMode.EDIT,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),@@ -263,7 +262,6 @@ def test_session_manager_auth_token_edit_mode_with_provided_token():session_manager = SessionManager(file_router=AppFileRouter.new_file(),mode=SessionMode.EDIT,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),@@ -285,7 +283,6 @@ def test_session_manager_auth_token_edit_mode_without_provided_token():session_manager = SessionManager(file_router=AppFileRouter.new_file(),mode=SessionMode.EDIT,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),@@ -311,7 +308,6 @@ def test_session_manager_auth_token_run_mode_with_provided_token():session_manager = SessionManager(file_router=AppFileRouter.new_file(),mode=SessionMode.RUN,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),@@ -354,7 +350,6 @@ def test_session_manager_auth_token_run_mode_without_provided_token(session_manager = SessionManager(file_router=AppFileRouter.infer(str(file_path)),mode=SessionMode.RUN,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),@@ -375,7 +370,6 @@ def test_session_manager_auth_token_run_mode_without_provided_token(session_manager2 = SessionManager(file_router=AppFileRouter.infer(str(file_path)),mode=SessionMode.RUN,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(spec=LspServer),diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.pyindex 8f4f6ea7f0..100582ead7 100644--- a/tests/_server/test_sessions.py+++ b/tests/_server/test_sessions.py@@ -7,6 +7,7 @@ import inspectimport osimport queueimport sys+import threadingimport timefrom multiprocessing.queues import Queue as MPQueuefrom pathlib import Path@@ -34,12 +35,14 @@ from marimo._server.file_router import AppFileRouterfrom marimo._server.model import ConnectionState, SessionModefrom marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionView-from marimo._server.sessions import (- KernelManager,- QueueManager,- Session,- SessionManager,+from marimo._server.sessions.managers import (+ KernelManagerImpl,+ QueueManagerImpl,)+from marimo._server.sessions.session import (+ SessionImpl,+)+from marimo._server.sessions.session_manager import SessionManagerfrom marimo._server.utils import initialize_asynciofrom marimo._types.ids import SessionIdfrom marimo._utils.marimo_path import MarimoPath@@ -82,13 +85,13 @@ session_id = SessionId("test")@save_and_restore_maindef test_queue_manager() -> None:# Test with multiprocessing queues- queue_manager_mp = QueueManager(use_multiprocessing=True)+ queue_manager_mp = QueueManagerImpl(use_multiprocessing=True)assert isinstance(queue_manager_mp.control_queue, MPQueue)assert isinstance(queue_manager_mp.completion_queue, MPQueue)assert isinstance(queue_manager_mp.input_queue, MPQueue)# Test with threading queues- queue_manager_thread = QueueManager(use_multiprocessing=False)+ queue_manager_thread = QueueManagerImpl(use_multiprocessing=False)assert isinstance(queue_manager_thread.control_queue, queue.Queue)assert isinstance(queue_manager_thread.completion_queue, queue.Queue)assert isinstance(queue_manager_thread.input_queue, queue.Queue)@@ -97,16 +100,16 @@ def test_queue_manager() -> None:@save_and_restore_maindef test_kernel_manager_run_mode() -> None:# Mock objects and data for testing- queue_manager = QueueManager(use_multiprocessing=False)+ queue_manager = QueueManagerImpl(use_multiprocessing=False)mode = SessionMode.RUN# Instantiate a KernelManager- kernel_manager = KernelManager(- queue_manager,- mode,- {},- app_metadata,- get_default_config_manager(current_path=None),+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=mode,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)@@ -121,6 +124,7 @@ def test_kernel_manager_run_mode() -> None:kernel_manager.close_kernel()# Assert shutdown+ assert isinstance(kernel_manager.kernel_task, threading.Thread)kernel_manager.kernel_task.join()assert not kernel_manager.is_alive()assert queue_manager.input_queue.empty()@@ -130,16 +134,16 @@ def test_kernel_manager_run_mode() -> None:@save_and_restore_maindef test_kernel_manager_edit_mode() -> None:# Mock objects and data for testing- queue_manager = QueueManager(use_multiprocessing=True)+ queue_manager = QueueManagerImpl(use_multiprocessing=True)mode = SessionMode.EDIT# Instantiate a KernelManager- kernel_manager = KernelManager(- queue_manager,- mode,- {},- app_metadata,- get_default_config_manager(current_path=None),+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=mode,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)@@ -163,15 +167,15 @@ def test_kernel_manager_edit_mode() -> None:@save_and_restore_maindef test_kernel_manager_interrupt(tmp_path: Path) -> None:- queue_manager = QueueManager(use_multiprocessing=True)+ queue_manager = QueueManagerImpl(use_multiprocessing=True)mode = SessionMode.EDIT- kernel_manager = KernelManager(- queue_manager,- mode,- {},- app_metadata,- get_default_config_manager(current_path=None),+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=mode,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)@@ -270,19 +274,19 @@ session_id = SessionId("test_session_id")async def test_session() -> None:session_consumer: Any = MagicMock()session_consumer.connection_state.return_value = ConnectionState.OPEN- queue_manager = QueueManager(use_multiprocessing=False)- kernel_manager = KernelManager(- queue_manager,- SessionMode.RUN,- {},- app_metadata,- get_default_config_manager(current_path=None),+ queue_manager = QueueManagerImpl(use_multiprocessing=False)+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=SessionMode.RUN,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)# Instantiate a Session- session = Session(+ session = SessionImpl(session_id,session_consumer,queue_manager,@@ -317,27 +321,25 @@ async def test_session() -> None:def test_session_disconnect_reconnect() -> None:session_consumer: Any = MagicMock()session_consumer.connection_state.return_value = ConnectionState.OPEN- queue_manager = QueueManager(use_multiprocessing=False)- kernel_manager = KernelManager(- queue_manager,- SessionMode.RUN,- {},- AppMetadata(- query_params={}, cli_args={}, argv=None, app_config=_AppConfig()- ),- get_default_config_manager(current_path=None),+ queue_manager = QueueManagerImpl(use_multiprocessing=False)+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=SessionMode.RUN,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)# Instantiate a Session- session = Session(- session_id,- session_consumer,- queue_manager,- kernel_manager,- AppFileManager.from_app(InternalApp(App())),- get_default_config_manager(current_path=None),+ session = SessionImpl(+ initialization_id=session_id,+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=AppFileManager.from_app(InternalApp(App())),+ config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,)@@ -377,25 +379,25 @@ def test_session_disconnect_reconnect() -> None:def test_session_with_kiosk_consumers() -> None:session_consumer: Any = MagicMock()session_consumer.connection_state.return_value = ConnectionState.OPEN- queue_manager = QueueManager(use_multiprocessing=False)- kernel_manager = KernelManager(- queue_manager,- SessionMode.RUN,- {},- app_metadata,- get_default_config_manager(current_path=None),+ queue_manager = QueueManagerImpl(use_multiprocessing=False)+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=SessionMode.RUN,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)# Instantiate a Session- session = Session(- session_id,- session_consumer,- queue_manager,- kernel_manager,- AppFileManager.from_app(InternalApp(App())),- get_default_config_manager(current_path=None),+ session = SessionImpl(+ initialization_id=session_id,+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=AppFileManager.from_app(InternalApp(App())),+ config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,)@@ -461,7 +463,6 @@ def __():session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=True,include_code=True,lsp_server=MagicMock(),@@ -615,7 +616,6 @@ def test_watch_mode_does_not_override_config(tmp_path: Path) -> None:session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=True,include_code=True,lsp_server=MagicMock(),@@ -631,7 +631,6 @@ def test_watch_mode_does_not_override_config(tmp_path: Path) -> None:session_manager_no_watch = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=True,include_code=True,lsp_server=MagicMock(),@@ -689,7 +688,6 @@ def __():session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=True,include_code=True,lsp_server=MagicMock(),@@ -834,7 +832,6 @@ def __():session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=True,include_code=True,lsp_server=MagicMock(),@@ -940,13 +937,13 @@ def __():# Create session with the temporary filesession_consumer = MagicMock()session_consumer.connection_state.return_value = ConnectionState.OPEN- queue_manager = QueueManager(use_multiprocessing=False)- kernel_manager = KernelManager(- queue_manager,- SessionMode.RUN,- {},- app_metadata,- get_default_config_manager(current_path=None),+ queue_manager = QueueManagerImpl(use_multiprocessing=False)+ kernel_manager = KernelManagerImpl(+ queue_manager=queue_manager,+ mode=SessionMode.RUN,+ configs={},+ app_metadata=app_metadata,+ config_manager=get_default_config_manager(current_path=None),virtual_files_supported=True,redirect_console_to_browser=False,)@@ -956,13 +953,13 @@ def __():cell_data.codefor cell_data in app_file_manager.app.cell_manager.cell_data())- session = Session(- session_id,- session_consumer,- queue_manager,- kernel_manager,- app_file_manager,- get_default_config_manager(current_path=None),+ session = SessionImpl(+ initialization_id=session_id,+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=app_file_manager,+ config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,)@@ -974,7 +971,7 @@ def __():assert session.session_cache_manager.path == str(notebook_path)assert (session.session_cache_manager.interval- == Session.SESSION_CACHE_INTERVAL_SECONDS+ == SessionImpl.SESSION_CACHE_INTERVAL_SECONDS)# Add some operations to the session view@@ -988,13 +985,13 @@ def __():)# Create a new session and sync from cache- session2 = Session(- "test2",- session_consumer,- queue_manager,- kernel_manager,- app_file_manager,- get_default_config_manager(current_path=None),+ session2 = SessionImpl(+ initialization_id="test2",+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=app_file_manager,+ config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,)session2.sync_session_view_from_cache()@@ -1009,13 +1006,13 @@ def __():# Test syncing with no file pathapp_file_manager_no_path = AppFileManager.from_app(InternalApp(App()))- session3 = Session(- "test3",- session_consumer,- queue_manager,- kernel_manager,- app_file_manager_no_path,- get_default_config_manager(current_path=None),+ session3 = SessionImpl(+ initialization_id="test3",+ session_consumer=session_consumer,+ queue_manager=queue_manager,+ kernel_manager=kernel_manager,+ app_file_manager=app_file_manager_no_path,+ config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,)session3.sync_session_view_from_cache()@@ -1056,7 +1053,7 @@ def test_session_with_script_config_overrides(app_file_manager = AppFileManager(filename=str(tmp_file))# Create session with the file that has script config- session = Session.create(+ session = SessionImpl.create(initialization_id="test_id",session_consumer=session_consumer,mode=SessionMode.RUN,@@ -1114,7 +1111,7 @@ async def test_file_change_handler_skips_own_writes(tmp_path: Path) -> None:session_consumer.connection_state.return_value = ConnectionState.OPENapp_file_manager = AppFileManager(filename=str(temp_file))- session = Session.create(+ session = SessionImpl.create(initialization_id="test_id",session_consumer=session_consumer,mode=SessionMode.EDIT,@@ -1143,7 +1140,6 @@ async def test_file_change_handler_skips_own_writes(tmp_path: Path) -> None:session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,- development_mode=False,quiet=False,include_code=True,lsp_server=MagicMock(),