π OpenHands: AI-Driven Development
APP-1363 : Remove V0 io Package (#14094)
Tim O'Farrell478c998f04d1
diff --git a/openhands/core/loop.py b/openhands/core/loop.pydeleted file mode 100644index 1b30d9a3b9..0000000000--- a/openhands/core/loop.py+++ /dev/null@@ -1,54 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import asyncio--from openhands.controller import AgentController-from openhands.core.logger import openhands_logger as logger-from openhands.core.schema import AgentState-from openhands.memory.memory import Memory-from openhands.runtime.base import Runtime-from openhands.runtime.runtime_status import RuntimeStatus---async def run_agent_until_done(- controller: AgentController,- runtime: Runtime,- memory: Memory,- end_states: list[AgentState],- skip_set_callback: bool = False,-) -> None:- """run_agent_until_done takes a controller and a runtime, and will run- the agent until it reaches a terminal state.- Note that runtime must be connected before being passed in here.- """-- def status_callback(msg_type: str, runtime_status: RuntimeStatus, msg: str) -> None:- if msg_type == 'error':- logger.error(msg)- if controller:- controller.state.last_error = msg- asyncio.create_task(controller.set_agent_state_to(AgentState.ERROR))- else:- logger.info(msg)-- if not skip_set_callback:- if hasattr(runtime, 'status_callback') and runtime.status_callback:- raise ValueError(- 'Runtime status_callback was set, but run_agent_until_done will override it'- )- if hasattr(controller, 'status_callback') and controller.status_callback:- raise ValueError(- 'Controller status_callback was set, but run_agent_until_done will override it'- )-- runtime.status_callback = status_callback- controller.status_callback = status_callback- memory.status_callback = status_callback-- while controller.state.agent_state not in end_states:- await asyncio.sleep(1)diff --git a/openhands/core/main.py b/openhands/core/main.pydeleted file mode 100644index 567154034c..0000000000--- a/openhands/core/main.py+++ /dev/null@@ -1,393 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import asyncio-import json-import os-import signal-import sys-from pathlib import Path-from typing import Callable, Protocol--from openhands.controller.replay import ReplayManager-from openhands.controller.state.state import State-from openhands.core.config import (- OpenHandsConfig,- parse_arguments,- setup_config_from_args,-)-from openhands.core.config.mcp_config import MCPConfig, OpenHandsMCPConfigImpl-from openhands.core.logger import openhands_logger as logger-from openhands.core.loop import run_agent_until_done-from openhands.core.schema import AgentState-from openhands.core.setup import (- create_agent,- create_controller,- create_memory,- create_runtime,- generate_sid,- get_provider_tokens,- initialize_repository_for_runtime,-)-from openhands.events import EventSource, EventStreamSubscriber-from openhands.events.action import MessageAction, NullAction-from openhands.events.action.action import Action-from openhands.events.event import Event-from openhands.events.observation import AgentStateChangedObservation-from openhands.io import read_input, read_task-from openhands.mcp import add_mcp_tools_to_agent-from openhands.memory.memory import Memory-from openhands.runtime.base import Runtime-from openhands.utils.async_utils import call_async_from_sync-from openhands.utils.utils import create_registry_and_conversation_stats---class FakeUserResponseFunc(Protocol):- def __call__(- self,- state: State,- encapsulate_solution: bool = False,- try_parse: Callable[[Action | None], str] | None = None,- ) -> str: ...---async def run_controller(- config: OpenHandsConfig,- initial_user_action: Action,- sid: str | None = None,- runtime: Runtime | None = None,- exit_on_message: bool = False,- fake_user_response_fn: FakeUserResponseFunc | None = None,- headless_mode: bool = True,- memory: Memory | None = None,- conversation_instructions: str | None = None,-) -> State | None:- """Main coroutine to run the agent controller with task input flexibility.-- It's only used when you launch openhands backend directly via cmdline.-- Args:- config: The app config.- initial_user_action: An Action object containing initial user input- sid: (optional) The session id. IMPORTANT: please don't set this unless you know what you're doing.- Set it to incompatible value will cause unexpected behavior on RemoteRuntime.- runtime: (optional) A runtime for the agent to run on.- exit_on_message: quit if agent asks for a message from user (optional)- fake_user_response_fn: An optional function that receives the current state- (could be None) and returns a fake user response.- headless_mode: Whether the agent is run in headless mode.-- Returns:- The final state of the agent, or None if an error occurred.-- Raises:- AssertionError: If initial_user_action is not an Action instance.- Exception: Various exceptions may be raised during execution and will be logged.-- Notes:- - State persistence: If config.file_store is set, the agent's state will be- saved between sessions.- - Trajectories: If config.trajectories_path is set, execution history will be- saved as JSON for analysis.- - Budget control: Execution is limited by config.max_iterations and- config.max_budget_per_task.-- Example:- >>> config = load_openhands_config()- >>> action = MessageAction(content="Write a hello world program")- >>> state = await run_controller(config=config, initial_user_action=action)- """- sid = sid or generate_sid(config)-- llm_registry, conversation_stats, config = create_registry_and_conversation_stats(- config,- sid,- None,- )-- agent = create_agent(config, llm_registry)-- # when the runtime is created, it will be connected and clone the selected repository- repo_directory = None- if runtime is None:- # In itialize repository if needed- repo_tokens = get_provider_tokens()- runtime = create_runtime(- config,- llm_registry,- sid=sid,- headless_mode=headless_mode,- agent=agent,- git_provider_tokens=repo_tokens,- )- # Connect to the runtime- call_async_from_sync(runtime.connect)-- # Initialize repository if needed- if config.sandbox.selected_repo:- repo_directory = initialize_repository_for_runtime(- runtime,- immutable_provider_tokens=repo_tokens,- selected_repository=config.sandbox.selected_repo,- )-- event_stream = runtime.event_stream-- # when memory is created, it will load the microagents from the selected repository- if memory is None:- memory = create_memory(- runtime=runtime,- event_stream=event_stream,- sid=sid,- selected_repository=config.sandbox.selected_repo,- repo_directory=repo_directory,- conversation_instructions=conversation_instructions,- working_dir=str(runtime.workspace_root),- )-- # Add MCP tools to the agent- if agent.config.enable_mcp:- # Add OpenHands' MCP server by default- default_servers = await OpenHandsMCPConfigImpl.create_default_mcp_server_config(- config.mcp_host, config, None- )- runtime.config.mcp = MCPConfig(- mcpServers={**runtime.config.mcp.mcpServers, **default_servers}- )-- await add_mcp_tools_to_agent(agent, runtime, memory)-- replay_events: list[Event] | None = None- if config.replay_trajectory_path:- logger.info('Trajectory replay is enabled')- assert isinstance(initial_user_action, NullAction)- replay_events, initial_user_action = load_replay_log(- config.replay_trajectory_path- )-- controller, initial_state = create_controller(- agent, runtime, config, conversation_stats, replay_events=replay_events- )-- assert isinstance(initial_user_action, Action), (- f'initial user actions must be an Action, got {type(initial_user_action)}'- )- logger.debug(- f'Agent Controller Initialized: Running agent {agent.name}, model '- f'{agent.llm.config.model}, with actions: {initial_user_action}'- )-- # Set up asyncio-safe signal handler for graceful shutdown- sigint_count = 0- shutdown_event = asyncio.Event()-- def signal_handler():- """Handle SIGINT signals for graceful shutdown."""- nonlocal sigint_count- sigint_count += 1-- if sigint_count == 1:- logger.info('Received SIGINT (Ctrl+C). Initiating graceful shutdown...')- logger.info('Press Ctrl+C again to force immediate exit.')- shutdown_event.set()- else:- logger.info('Received second SIGINT. Forcing immediate exit...')- sys.exit(1)-- # Register the asyncio signal handler (safer for async contexts)- loop = asyncio.get_running_loop()- loop.add_signal_handler(signal.SIGINT, signal_handler)-- # start event is a MessageAction with the task, either resumed or new- if initial_state is not None and initial_state.last_error:- # we're resuming the previous session- event_stream.add_event(- MessageAction(- content=(- "Let's get back on track. If you experienced errors before, do "- 'NOT resume your task. Ask me about it.'- ),- ),- EventSource.USER,- )- else:- # init with the provided actions- event_stream.add_event(initial_user_action, EventSource.USER)-- def on_event(event: Event) -> None:- if isinstance(event, AgentStateChangedObservation):- if event.agent_state == AgentState.AWAITING_USER_INPUT:- if exit_on_message:- message = '/exit'- elif fake_user_response_fn is None:- message = read_input(config.cli_multiline_input)- else:- message = fake_user_response_fn(controller.get_state())- action = MessageAction(content=message)- event_stream.add_event(action, EventSource.USER)-- event_stream.subscribe(EventStreamSubscriber.MAIN, on_event, sid)-- end_states = [- AgentState.FINISHED,- AgentState.REJECTED,- AgentState.ERROR,- AgentState.PAUSED,- AgentState.STOPPED,- ]-- try:- # Create a task for the main agent loop- agent_task = asyncio.create_task(- run_agent_until_done(controller, runtime, memory, end_states)- )-- # Wait for either the agent to complete or shutdown signal- done, pending = await asyncio.wait(- [agent_task, asyncio.create_task(shutdown_event.wait())],- return_when=asyncio.FIRST_COMPLETED,- )-- # Cancel any pending tasks- for task in pending:- task.cancel()-- # Wait for all cancelled tasks to complete in parallel- await asyncio.gather(*pending, return_exceptions=True)-- # Check if shutdown was requested- if shutdown_event.is_set():- logger.info('Graceful shutdown requested.')-- # Perform graceful cleanup sequence- try:- # 1. Stop the agent controller first to prevent new LLM calls- logger.debug('Stopping agent controller...')- await controller.close()-- # 2. Stop the EventStream to prevent new events from being processed- logger.debug('Stopping EventStream...')- event_stream.close()-- # 3. Give time for in-flight operations to complete before closing runtime- logger.debug('Waiting for in-flight operations to complete...')- await asyncio.sleep(0.3)-- # 4. Close the runtime to avoid bash session interruption errors- logger.debug('Closing runtime...')- runtime.close()-- # 5. Give a brief moment for final cleanup to complete- await asyncio.sleep(0.1)-- except Exception as e:- logger.warning(f'Error during graceful cleanup: {e}')-- except Exception as e:- logger.error(f'Exception in main loop: {e}')-- # save session when we're about to close- if config.file_store is not None and config.file_store != 'memory':- end_state = controller.get_state()- # NOTE: the saved state does not include delegates events- end_state.save_to_session(- event_stream.sid, event_stream.file_store, event_stream.user_id- )-- await controller.close(set_stop_state=False)-- state = controller.get_state()-- # save trajectories if applicable- if config.save_trajectory_path is not None:- # if save_trajectory_path is a folder, use session id as file name- if os.path.isdir(config.save_trajectory_path):- file_path = os.path.join(config.save_trajectory_path, sid + '.json')- else:- file_path = config.save_trajectory_path- os.makedirs(os.path.dirname(file_path), exist_ok=True)- histories = controller.get_trajectory(config.save_screenshots_in_trajectory)- with open(file_path, 'w') as f:- json.dump(histories, f, indent=4)-- return state---def auto_continue_response(- state: State,- encapsulate_solution: bool = False,- try_parse: Callable[[Action | None], str] | None = None,-) -> str:- """Default function to generate user responses.- Tell the agent to proceed without asking for more input, or finish the interaction.- """- message = (- 'Please continue on whatever approach you think is suitable.\n'- 'If you think you have solved the task, please finish the interaction.\n'- 'IMPORTANT: YOU SHOULD NEVER ASK FOR HUMAN RESPONSE.\n'- )- return message---def load_replay_log(trajectory_path: str) -> tuple[list[Event] | None, Action]:- """Load trajectory from given path, serialize it to a list of events, and return- two things:- 1) A list of events except the first action- 2) First action (user message, a.k.a. initial task)- """- try:- path = Path(trajectory_path).resolve()-- if not path.exists():- raise ValueError(f'Trajectory file not found: {path}')-- if not path.is_file():- raise ValueError(f'Trajectory path is a directory, not a file: {path}')-- with open(path, 'r', encoding='utf-8') as file:- events = ReplayManager.get_replay_events(json.load(file))- assert isinstance(events[0], MessageAction)- return events[1:], events[0]- except json.JSONDecodeError as e:- raise ValueError(f'Invalid JSON format in {trajectory_path}: {e}')---if __name__ == '__main__':- args = parse_arguments()-- config: OpenHandsConfig = setup_config_from_args(args)-- # Read task from file, CLI args, or stdin- task_str = read_task(args, config.cli_multiline_input)-- initial_user_action: Action = NullAction()- if config.replay_trajectory_path:- if task_str:- raise ValueError(- 'User-specified task is not supported under trajectory replay mode'- )- else:- if not task_str:- raise ValueError('No task provided. Please specify a task through -t, -f.')-- # Create actual initial user action- initial_user_action = MessageAction(content=task_str)-- # Set session name- session_name = args.name- sid = generate_sid(config, session_name)-- asyncio.run(- run_controller(- config=config,- initial_user_action=initial_user_action,- sid=sid,- fake_user_response_fn=None- if args.no_auto_continue- else auto_continue_response,- )- )diff --git a/openhands/events/stream.py b/openhands/events/stream.pyindex 9d9b2c186c..d464527a48 100644--- a/openhands/events/stream.py+++ b/openhands/events/stream.py@@ -1,4 +1,5 @@import asyncio+import jsonimport queueimport threadingfrom concurrent.futures import ThreadPoolExecutor@@ -11,7 +12,6 @@ from openhands.core.logger import openhands_logger as loggerfrom openhands.events.event import Event, EventSourcefrom openhands.events.event_store import EventStorefrom openhands.events.serialization.event import event_from_dict, event_to_dict-from openhands.io import jsonfrom openhands.storage import FileStorefrom openhands.storage.locations import (get_conversation_dir,diff --git a/openhands/io/__init__.py b/openhands/io/__init__.pydeleted file mode 100644index bf1a054356..0000000000--- a/openhands/io/__init__.py+++ /dev/null@@ -1,10 +0,0 @@-from openhands.io.io import read_input, read_task, read_task_from_file-from openhands.io.json import dumps, loads--__all__ = [- 'read_input',- 'read_task_from_file',- 'read_task',- 'dumps',- 'loads',-]diff --git a/openhands/io/io.py b/openhands/io/io.pydeleted file mode 100644index e2991ec479..0000000000--- a/openhands/io/io.py+++ /dev/null@@ -1,37 +0,0 @@-import argparse-import sys---def read_input(cli_multiline_input: bool = False) -> str:- """Read input from user based on config settings."""- if cli_multiline_input:- print('Enter your message (enter "/exit" on a new line to finish):')- lines = []- while True:- line = input('>> ').rstrip()- if line == '/exit': # finish input- break- lines.append(line)- return '\n'.join(lines)- else:- return input('>> ').rstrip()---def read_task_from_file(file_path: str) -> str:- """Read task from the specified file."""- with open(file_path, 'r', encoding='utf-8') as file:- return file.read()---def read_task(args: argparse.Namespace, cli_multiline_input: bool) -> str:- """Read the task from the CLI args, file, or stdin."""- # Determine the task- task_str = ''- if args.file:- task_str = read_task_from_file(args.file)- elif args.task:- task_str = args.task- elif not sys.stdin.isatty():- task_str = read_input(cli_multiline_input)-- return task_strdiff --git a/openhands/io/json.py b/openhands/io/json.pydeleted file mode 100644index 0a9df78d8d..0000000000--- a/openhands/io/json.py+++ /dev/null@@ -1,75 +0,0 @@-import json-from datetime import datetime-from typing import Any--from json_repair import repair_json-from litellm.types.utils import ModelResponse--from openhands.core.exceptions import LLMResponseError-from openhands.events.event import Event-from openhands.events.observation import CmdOutputMetadata-from openhands.events.serialization import event_to_dict-from openhands.llm.metrics import Metrics---class OpenHandsJSONEncoder(json.JSONEncoder):- """Custom JSON encoder that handles datetime and event objects"""-- def default(self, obj):- if isinstance(obj, datetime):- return obj.isoformat()- if isinstance(obj, Event):- return event_to_dict(obj)- if isinstance(obj, Metrics):- return obj.get()- if isinstance(obj, ModelResponse):- return obj.model_dump()- if isinstance(obj, CmdOutputMetadata):- return obj.model_dump()- return super().default(obj)---# Create a single reusable encoder instance-_json_encoder = OpenHandsJSONEncoder()---def dumps(obj, **kwargs) -> str:- """Serialize an object to str format"""- if not kwargs:- return _json_encoder.encode(obj)-- # Create a copy of the kwargs to avoid modifying the original- encoder_kwargs = kwargs.copy()-- # If cls is specified, use it; otherwise use our custom encoder- if 'cls' not in encoder_kwargs:- encoder_kwargs['cls'] = OpenHandsJSONEncoder-- return json.dumps(obj, **encoder_kwargs)---def loads(json_str: str, **kwargs) -> Any:- """Create a JSON object from str"""- try:- return json.loads(json_str, **kwargs)- except json.JSONDecodeError:- pass- depth = 0- start = -1- for i, char in enumerate(json_str):- if char == '{':- if depth == 0:- start = i- depth += 1- elif char == '}':- depth -= 1- if depth == 0 and start != -1:- response = json_str[start : i + 1]- try:- json_str = repair_json(response)- return json.loads(json_str, **kwargs)- except (json.JSONDecodeError, ValueError, TypeError) as e:- raise LLMResponseError(- 'Invalid JSON in response. Please make sure the response is a valid JSON object.'- ) from e- raise LLMResponseError('No valid JSON object found in response.')diff --git a/openhands/llm/llm.py b/openhands/llm/llm.pyindex e15a18c491..b782cc6f6d 100644--- a/openhands/llm/llm.py+++ b/openhands/llm/llm.py@@ -7,6 +7,7 @@# Tag: Legacy-V0# V1 replacement for this module lives in the Software Agent SDK.import copy+import jsonimport osimport timeimport warnings@@ -245,8 +246,6 @@ class LLM(RetryMixin, DebugMixin):)def wrapper(*args: Any, **kwargs: Any) -> Any:"""Wrapper for the litellm completion function. Logs the input and output of the completion function."""- from openhands.io import json-messages_kwarg: (dict[str, Any] | Message | list[dict[str, Any]] | list[Message]) = []@@ -505,7 +504,6 @@ class LLM(RetryMixin, DebugMixin):# noinspection PyBroadExceptionexcept Exception:pass- from openhands.io import jsonlogger.debug(f'Model info: {json.dumps({"model": self.config.model, "base_url": self.config.base_url}, indent=2)}'diff --git a/tests/unit/io/test_io.py b/tests/unit/io/test_io.pydeleted file mode 100644index 1449df8517..0000000000--- a/tests/unit/io/test_io.py+++ /dev/null@@ -1,27 +0,0 @@-from unittest.mock import patch--from openhands.core.config import OpenHandsConfig-from openhands.io import read_input---def test_single_line_input():- """Test that single line input works when cli_multiline_input is False"""- config = OpenHandsConfig()- config.cli_multiline_input = False-- with patch('builtins.input', return_value='hello world'):- result = read_input(config.cli_multiline_input)- assert result == 'hello world'---def test_multiline_input():- """Test that multiline input works when cli_multiline_input is True"""- config = OpenHandsConfig()- config.cli_multiline_input = True-- # Simulate multiple lines of input followed by /exit- mock_inputs = ['line 1', 'line 2', 'line 3', '/exit']-- with patch('builtins.input', side_effect=mock_inputs):- result = read_input(config.cli_multiline_input)- assert result == 'line 1\nline 2\nline 3'diff --git a/tests/unit/io/test_json.py b/tests/unit/io/test_json.pydeleted file mode 100644index b93e46765c..0000000000--- a/tests/unit/io/test_json.py+++ /dev/null@@ -1,50 +0,0 @@-from datetime import datetime--from openhands.events.action import MessageAction-from openhands.io import json---def test_event_serialization_deserialization():- message = MessageAction(content='This is a test.', wait_for_response=False)- message._id = 42- message._timestamp = datetime(2020, 1, 1, 23, 59, 58)- serialized = json.dumps(message)- deserialized = json.loads(serialized)- expected = {- 'id': 42,- 'timestamp': '2020-01-01T23:59:58',- 'action': 'message',- 'message': 'This is a test.',- 'args': {- 'content': 'This is a test.',- 'image_urls': None,- 'file_urls': None,- 'wait_for_response': False,- 'security_risk': -1,- },- }- assert deserialized == expected---def test_array_serialization_deserialization():- message = MessageAction(content='This is a test.', wait_for_response=False)- message._id = 42- message._timestamp = datetime(2020, 1, 1, 0, 0, 0)- serialized = json.dumps([message])- deserialized = json.loads(serialized)- expected = [- {- 'id': 42,- 'timestamp': '2020-01-01T00:00:00',- 'action': 'message',- 'message': 'This is a test.',- 'args': {- 'content': 'This is a test.',- 'image_urls': None,- 'file_urls': None,- 'wait_for_response': False,- 'security_risk': -1,- },- }- ]- assert deserialized == expecteddiff --git a/tests/unit/io/test_json_encoder.py b/tests/unit/io/test_json_encoder.pydeleted file mode 100644index e610c2e988..0000000000--- a/tests/unit/io/test_json_encoder.py+++ /dev/null@@ -1,57 +0,0 @@-import gc-from datetime import datetime--import psutil--from openhands.io.json import dumps---def get_memory_usage():- """Get current memory usage of the process"""- process = psutil.Process()- return process.memory_info().rss---def test_json_encoder_memory_leak():- # Force garbage collection before test- gc.collect()- initial_memory = get_memory_usage()-- # Create a large dataset that will need encoding- large_data = {- 'datetime': datetime.now(),- 'nested': [{'timestamp': datetime.now()} for _ in range(1000)],- }-- # Track memory usage over multiple iterations- memory_samples = []- for i in range(10):- # Perform multiple serializations in each iteration- for _ in range(100):- dumps(large_data)- dumps(large_data, indent=2) # Test with kwargs too-- # Force garbage collection- gc.collect()- memory_samples.append(get_memory_usage())-- # Check if memory usage is stable (not continuously growing)- # We expect some fluctuation but not a steady increase- max_memory = max(memory_samples)- min_memory = min(memory_samples)- memory_variation = max_memory - min_memory-- # Allow for more memory variation (2MB) due to Python's memory management- # The standard library's json module may use more memory than expected- assert memory_variation < 2 * 1024 * 1024, (- f'Memory usage unstable: {memory_variation} bytes variation'- )-- # Also check total memory increase from start- final_memory = memory_samples[-1]- memory_increase = final_memory - initial_memory-- # Allow for some memory increase (2MB) as some objects may be cached- assert memory_increase < 2 * 1024 * 1024, (- f'Memory leak detected: {memory_increase} bytes increase'- )diff --git a/tests/unit/llm/test_litellm_proxy_model_parsing.py b/tests/unit/llm/test_litellm_proxy_model_parsing.pydeleted file mode 100644index c108570131..0000000000--- a/tests/unit/llm/test_litellm_proxy_model_parsing.py+++ /dev/null@@ -1,236 +0,0 @@-import sys-import types-from unittest.mock import patch--# Provide lightweight stubs for optional dependencies that are imported at module import time-# elsewhere in the codebase, to avoid installing heavy packages for this focused unit test.-if 'pythonjsonlogger' not in sys.modules:- pythonjsonlogger = types.ModuleType('pythonjsonlogger')- pythonjsonlogger.json = types.ModuleType('pythonjsonlogger.json')-- class _DummyJsonFormatter: # minimal stub- def __init__(self, *args, **kwargs):- pass-- pythonjsonlogger.json.JsonFormatter = _DummyJsonFormatter- sys.modules['pythonjsonlogger'] = pythonjsonlogger- sys.modules['pythonjsonlogger.json'] = pythonjsonlogger.json--if 'google' not in sys.modules:- google = types.ModuleType('google')- # make it package-like- google.__path__ = [] # type: ignore[attr-defined]- sys.modules['google'] = google-if 'google.api_core' not in sys.modules:- api_core = types.ModuleType('google.api_core')- api_core.__path__ = [] # type: ignore[attr-defined]- sys.modules['google.api_core'] = api_core-if 'google.api_core.exceptions' not in sys.modules:- exceptions_mod = types.ModuleType('google.api_core.exceptions')-- # Provide a NotFound exception type used by storage backends- class _NotFound(Exception):- pass-- exceptions_mod.NotFound = _NotFound- sys.modules['google.api_core.exceptions'] = exceptions_mod--# Also stub google.cloud and google.cloud.storage used by storage backends-if 'google.cloud' not in sys.modules:- google_cloud_pkg = types.ModuleType('google.cloud')- google_cloud_pkg.__path__ = [] # type: ignore[attr-defined]- sys.modules['google.cloud'] = google_cloud_pkg-if 'google.cloud.storage' not in sys.modules:- storage_pkg = types.ModuleType('google.cloud.storage')- storage_pkg.__path__ = [] # type: ignore[attr-defined]-- class _DummyClient:- def __init__(self, *args, **kwargs):- pass-- storage_pkg.Client = _DummyClient- sys.modules['google.cloud.storage'] = storage_pkg--# Submodules used by storage backend-if 'google.cloud.storage.blob' not in sys.modules:- blob_mod = types.ModuleType('google.cloud.storage.blob')-- class _DummyBlob:- def __init__(self, *args, **kwargs):- pass-- blob_mod.Blob = _DummyBlob- sys.modules['google.cloud.storage.blob'] = blob_mod-if 'google.cloud.storage.bucket' not in sys.modules:- bucket_mod = types.ModuleType('google.cloud.storage.bucket')-- class _DummyBucket:- def __init__(self, *args, **kwargs):- pass-- bucket_mod.Bucket = _DummyBucket- sys.modules['google.cloud.storage.bucket'] = bucket_mod--# Also provide google.cloud.storage.client module referencing the Client stub-if 'google.cloud.storage.client' not in sys.modules:- client_mod = types.ModuleType('google.cloud.storage.client')- try:- client_mod.Client = sys.modules['google.cloud.storage'].Client # type: ignore[attr-defined]- except Exception:-- class _DummyClient2:- def __init__(self, *args, **kwargs):- pass-- client_mod.Client = _DummyClient2- sys.modules['google.cloud.storage.client'] = client_mod--# Stub boto3 used by S3 backend-if 'boto3' not in sys.modules:- boto3_mod = types.ModuleType('boto3')-- def _noop(*args, **kwargs):- class _Dummy:- def __getattr__(self, _):- return _noop-- def __call__(self, *a, **k):- return None-- return _Dummy()-- boto3_mod.client = _noop- boto3_mod.resource = _noop-- class _DummySession:- def client(self, *a, **k):- return _noop()-- def resource(self, *a, **k):- return _noop()-- boto3_mod.session = types.SimpleNamespace(Session=_DummySession)- sys.modules['boto3'] = boto3_mod--if 'botocore' not in sys.modules:- botocore_mod = types.ModuleType('botocore')- botocore_mod.__path__ = [] # type: ignore[attr-defined]- sys.modules['botocore'] = botocore_mod-if 'botocore.exceptions' not in sys.modules:- botocore_exc = types.ModuleType('botocore.exceptions')-- class _BotoCoreError(Exception):- pass-- botocore_exc.BotoCoreError = _BotoCoreError- sys.modules['botocore.exceptions'] = botocore_exc--# Stub uvicorn server constants used by shutdown listener-if 'uvicorn' not in sys.modules:- uvicorn_mod = types.ModuleType('uvicorn')- uvicorn_mod.__path__ = [] # type: ignore[attr-defined]- sys.modules['uvicorn'] = uvicorn_mod-if 'uvicorn.server' not in sys.modules:- uvicorn_server = types.ModuleType('uvicorn.server')- # minimal placeholder; value isn't used in this test- uvicorn_server.HANDLED_SIGNALS = set()- sys.modules['uvicorn.server'] = uvicorn_server--# Stub json_repair used by openhands.io.json-if 'json_repair' not in sys.modules:- json_repair_mod = types.ModuleType('json_repair')-- def repair_json(s: str) -> str:- return s-- json_repair_mod.repair_json = repair_json- sys.modules['json_repair'] = json_repair_mod--# Stub deprecated.deprecated decorator-if 'deprecated' not in sys.modules:- deprecated_mod = types.ModuleType('deprecated')-- def deprecated(*dargs, **dkwargs): # decorator shim- def _wrap(func):- return func-- # Support both @deprecated and @deprecated(reason="...") usages- if dargs and callable(dargs[0]) and not dkwargs:- return dargs[0]- return _wrap-- deprecated_mod.deprecated = deprecated- sys.modules['deprecated'] = deprecated_mod--# Import OpenHands after stubbing optional deps-from openhands.core.config.llm_config import LLMConfig-from openhands.llm.llm import LLM-from openhands.llm.metrics import Metrics---class DummyResponse:- def __init__(self, json_data):- self._json = json_data-- def json(self):- return self._json---@patch('httpx.get')-def test_litellm_proxy_model_with_nested_slashes_is_accepted(mock_get):- # Arrange: simulate LiteLLM proxy /v1/model/info returning our model- model_tail = 'copilot/gpt-4.1'- mock_get.return_value = DummyResponse(- {- 'data': [- {- 'model_name': model_tail,- 'model_info': {- 'max_input_tokens': 128000,- 'supports_vision': False,- },- }- ]- }- )-- cfg = LLMConfig(- model=f'litellm_proxy/{model_tail}',- api_key=None,- base_url='http://localhost:4000', # any string; we mock httpx.get anyway- )-- # Act: construct LLM; should not raise ValidationError- llm = LLM(config=cfg, service_id='test', metrics=Metrics(model_name=cfg.model))-- # Assert: model remains intact and model_info was set from proxy data- assert llm.config.model == f'litellm_proxy/{model_tail}'- assert llm.model_info is None or isinstance(- llm.model_info, (dict, types.MappingProxyType)- )---@patch('httpx.get')-def test_litellm_proxy_model_info_lookup_uses_full_tail(mock_get):- # Ensure we match exactly the entire tail after prefix when selecting model info- model_tail = 'nested/provider/path/model-x'- mock_get.return_value = DummyResponse(- {- 'data': [- {'model_name': model_tail, 'model_info': {'max_input_tokens': 32000}},- {'model_name': 'other', 'model_info': {'max_input_tokens': 1}},- ]- }- )-- cfg = LLMConfig(- model=f'litellm_proxy/{model_tail}',- api_key=None,- base_url='http://localhost:4000',- )-- llm = LLM(config=cfg, service_id='test', metrics=Metrics(model_name=cfg.model))-- # If proxy data was set, prefer that exact match; otherwise at least the construction should succeed- if llm.model_info is not None:- assert llm.model_info.get('max_input_tokens') == 32000