Skip to content

Commit

Permalink
fix runtime_manager plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren committed Dec 24, 2024
1 parent 4273001 commit 4b497c8
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 21 deletions.
3 changes: 2 additions & 1 deletion openhands/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
LocalhostCORSMiddleware,
NoCacheMiddleware,
RateLimitMiddleware,
session_manager,
)
from openhands.server.routes.conversation import app as conversation_api_router
from openhands.server.routes.feedback import app as feedback_api_router
Expand All @@ -24,7 +25,7 @@
from openhands.server.routes.public import app as public_api_router
from openhands.server.routes.security import app as security_api_router
from openhands.server.routes.settings import app as settings_router
from openhands.server.shared import openhands_config, session_manager
from openhands.server.shared import openhands_config
from openhands.utils.import_utils import get_impl


Expand Down
3 changes: 2 additions & 1 deletion openhands/server/listen_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from openhands.events.observation.agent import AgentStateChangedObservation
from openhands.events.serialization import event_to_dict
from openhands.events.stream import AsyncEventStreamWrapper
from openhands.server.middleware import session_manager
from openhands.server.session.manager import ConversationDoesNotExistError
from openhands.server.shared import config, openhands_config, session_manager, sio
from openhands.server.shared import config, openhands_config, sio
from openhands.server.types import AppMode
from openhands.storage.conversation.conversation_store import (
ConversationStore,
Expand Down
5 changes: 4 additions & 1 deletion openhands/server/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp

from openhands.server.shared import session_manager
from openhands.server.session import SessionManager
from openhands.server.shared import config, file_store, sio
from openhands.server.types import SessionMiddlewareInterface

session_manager = SessionManager(sio, config, file_store)


class LocalhostCORSMiddleware(CORSMiddleware):
"""
Expand Down
3 changes: 2 additions & 1 deletion openhands/server/routes/new_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from pydantic import BaseModel

from openhands.core.logger import openhands_logger as logger
from openhands.server.middleware import session_manager
from openhands.server.routes.settings import SettingsStoreImpl
from openhands.server.session.conversation_init_data import ConversationInitData
from openhands.server.shared import config, session_manager
from openhands.server.shared import config
from openhands.storage.conversation.conversation_store import (
ConversationMetadata,
ConversationStore,
Expand Down
13 changes: 5 additions & 8 deletions openhands/server/session/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from openhands.events.event import EventSource
from openhands.events.stream import EventStream
from openhands.runtime.base import Runtime
from openhands.runtime.runtime_manager import RuntimeManager
from openhands.security import SecurityAnalyzer, options
from openhands.server.shared import runtime_manager
from openhands.storage.files import FileStore
from openhands.utils.async_utils import call_async_from_sync, call_sync_from_async
from openhands.utils.shutdown_listener import should_continue
Expand All @@ -32,7 +32,6 @@ class AgentSession:
sid: str
event_stream: EventStream
file_store: FileStore
runtime_manager: RuntimeManager
controller: AgentController | None = None
runtime: Runtime | None = None
security_analyzer: SecurityAnalyzer | None = None
Expand All @@ -44,7 +43,6 @@ def __init__(
self,
sid: str,
file_store: FileStore,
runtime_manager: RuntimeManager,
status_callback: Optional[Callable] = None,
):
"""Initializes a new instance of the Session class
Expand All @@ -57,7 +55,6 @@ def __init__(
self.sid = sid
self.event_stream = EventStream(sid, file_store)
self.file_store = file_store
self.runtime_manager = runtime_manager
self._status_callback = status_callback

async def start(
Expand Down Expand Up @@ -117,7 +114,7 @@ async def _start(
return
self._initializing = True
self._create_security_analyzer(
self.runtime_manager.config.security.security_analyzer
runtime_manager.config.security.security_analyzer
)
await self._create_runtime(
agent=agent,
Expand All @@ -127,7 +124,7 @@ async def _start(

self.controller = self._create_controller(
agent,
self.runtime_manager.config.security.confirmation_mode,
runtime_manager.config.security.confirmation_mode,
max_iterations,
max_budget_per_task=max_budget_per_task,
agent_to_llm_config=agent_to_llm_config,
Expand Down Expand Up @@ -165,7 +162,7 @@ async def _close(self):
end_state.save_to_session(self.sid, self.file_store)
await self.controller.close()
if self.runtime is not None:
self.runtime_manager.destroy_runtime(self.sid)
runtime_manager.destroy_runtime(self.sid)
if self.security_analyzer is not None:
await self.security_analyzer.close()

Expand Down Expand Up @@ -208,7 +205,7 @@ async def _create_runtime(
await asyncio.sleep(1)

try:
self.runtime = await self.runtime_manager.create_runtime(
self.runtime = await runtime_manager.create_runtime(
event_stream=self.event_stream,
sid=self.sid,
plugins=agent.sandbox_plugins,
Expand Down
3 changes: 0 additions & 3 deletions openhands/server/session/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from openhands.core.exceptions import AgentRuntimeUnavailableError
from openhands.core.logger import openhands_logger as logger
from openhands.events.stream import EventStream, session_exists
from openhands.runtime.runtime_manager import RuntimeManager
from openhands.server.session.conversation import Conversation
from openhands.server.session.conversation_init_data import ConversationInitData
from openhands.server.session.session import ROOM_KEY, Session
Expand All @@ -33,7 +32,6 @@ class SessionManager:
sio: socketio.AsyncServer
config: AppConfig
file_store: FileStore
runtime_manager: RuntimeManager
_local_agent_loops_by_sid: dict[str, Session] = field(default_factory=dict)
local_connection_id_to_session_id: dict[str, str] = field(default_factory=dict)
_last_alive_timestamps: dict[str, float] = field(default_factory=dict)
Expand Down Expand Up @@ -316,7 +314,6 @@ async def maybe_start_agent_loop(
file_store=self.file_store,
config=self.config,
sio=self.sio,
runtime_manager=self.runtime_manager,
)
self._local_agent_loops_by_sid[sid] = session
await session.initialize_agent(conversation_init_data)
Expand Down
4 changes: 0 additions & 4 deletions openhands/server/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from openhands.events.serialization import event_from_dict, event_to_dict
from openhands.events.stream import EventStreamSubscriber
from openhands.llm.llm import LLM
from openhands.runtime.runtime_manager import RuntimeManager
from openhands.server.session.agent_session import AgentSession
from openhands.server.session.conversation_init_data import ConversationInitData
from openhands.storage.files import FileStore
Expand All @@ -40,14 +39,12 @@ class Session:
loop: asyncio.AbstractEventLoop
config: AppConfig
file_store: FileStore
runtime_manager: RuntimeManager

def __init__(
self,
sid: str,
config: AppConfig,
file_store: FileStore,
runtime_manager: RuntimeManager,
sio: socketio.AsyncServer | None,
):
self.sid = sid
Expand All @@ -58,7 +55,6 @@ def __init__(
sid,
file_store,
status_callback=self.queue_status_message,
runtime_manager=runtime_manager,
)
self.agent_session.event_stream.subscribe(
EventStreamSubscriber.SERVER, self.on_event, self.sid
Expand Down
2 changes: 0 additions & 2 deletions openhands/server/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from openhands.core.config import load_app_config
from openhands.runtime.runtime_manager import RuntimeManager
from openhands.server.config.openhands_config import load_openhands_config
from openhands.server.session import SessionManager
from openhands.storage import get_file_store

load_dotenv()
Expand All @@ -29,4 +28,3 @@
)

runtime_manager = RuntimeManager(config)
session_manager = SessionManager(sio, config, file_store, runtime_manager)

0 comments on commit 4b497c8

Please sign in to comment.