π OpenHands: AI-Driven Development
Removed V0 controller (#14060)
Tim O'Farrell180a35f013f7
diff --git a/openhands/controller/__init__.py b/openhands/controller/__init__.pydeleted file mode 100644index c050e6e4dd..0000000000--- a/openhands/controller/__init__.py+++ /dev/null@@ -1,5 +0,0 @@-from openhands.controller.agent_controller import AgentController--__all__ = [- 'AgentController',-]diff --git a/openhands/controller/agent.py b/openhands/controller/agent.pydeleted file mode 100644index 6aaaaf9b0b..0000000000--- a/openhands/controller/agent.py+++ /dev/null@@ -1,191 +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-# V1 replacement for this module lives in the Software Agent SDK.-from __future__ import annotations--from abc import ABC, abstractmethod-from typing import TYPE_CHECKING--from openhands.llm.llm_registry import LLMRegistry--if TYPE_CHECKING:- from openhands.controller.state.state import State- from openhands.events.action import Action- from openhands.events.action.message import SystemMessageAction- from openhands.utils.prompt import PromptManager-from litellm import ChatCompletionToolParam--from openhands.core.config import AgentConfig-from openhands.core.exceptions import (- AgentAlreadyRegisteredError,- AgentNotRegisteredError,-)-from openhands.core.logger import openhands_logger as logger-from openhands.events.event import EventSource-from openhands.runtime.plugins import PluginRequirement---class Agent(ABC):- DEPRECATED = False- """- This abstract base class is an general interface for an agent dedicated to- executing a specific instruction and allowing human interaction with the- agent during execution.- It tracks the execution status and maintains a history of interactions.- """-- _registry: dict[str, type['Agent']] = {}- sandbox_plugins: list[PluginRequirement] = []-- config_model: type[AgentConfig] = AgentConfig- """Class field that specifies the config model to use for the agent. Subclasses may override with a derived config model if needed."""-- def __init__(- self,- config: AgentConfig,- llm_registry: LLMRegistry,- ):- self.llm = llm_registry.get_llm_from_agent_config('agent', config)- self.llm_registry = llm_registry- self.config = config- self._complete = False- self._prompt_manager: 'PromptManager' | None = None- self.mcp_tools: dict[str, ChatCompletionToolParam] = {}- self.tools: list = []-- @property- def prompt_manager(self) -> 'PromptManager':- if self._prompt_manager is None:- raise ValueError(f'Prompt manager not initialized for agent {self.name}')- return self._prompt_manager-- def get_system_message(self) -> 'SystemMessageAction | None':- """Returns a SystemMessageAction containing the system message and tools.- This will be added to the event stream as the first message.-- Returns:- SystemMessageAction: The system message action with content and tools- None: If there was an error generating the system message- """- # Import here to avoid circular imports- from openhands.events.action.message import SystemMessageAction-- try:- if not self.prompt_manager:- logger.warning(- f'[{self.name}] Prompt manager not initialized before getting system message'- )- return None-- system_message = self.prompt_manager.get_system_message(- cli_mode=self.config.cli_mode- )-- # Get tools if available- tools = getattr(self, 'tools', None)-- system_message_action = SystemMessageAction(- content=system_message, tools=tools, agent_class=self.name- )- # Set the source attribute- system_message_action._source = EventSource.AGENT # type: ignore-- return system_message_action- except Exception as e:- logger.warning(f'[{self.name}] Failed to generate system message: {e}')- return None-- @property- def complete(self) -> bool:- """Indicates whether the current instruction execution is complete.-- Returns:- - complete (bool): True if execution is complete; False otherwise.- """- return self._complete-- @abstractmethod- def step(self, state: 'State') -> 'Action':- """Starts the execution of the assigned instruction. This method should- be implemented by subclasses to define the specific execution logic.- """- pass-- def reset(self) -> None:- """Resets the agent's execution status."""- # Only reset the completion status, not the LLM metrics- self._complete = False-- @property- def name(self) -> str:- return self.__class__.__name__-- @classmethod- def register(cls, name: str, agent_cls: type['Agent']) -> None:- """Registers an agent class in the registry.-- Parameters:- - name (str): The name to register the class under.- - agent_cls (Type['Agent']): The class to register.-- Raises:- - AgentAlreadyRegisteredError: If name already registered- """- if name in cls._registry:- raise AgentAlreadyRegisteredError(name)- cls._registry[name] = agent_cls-- @classmethod- def get_cls(cls, name: str) -> type['Agent']:- """Retrieves an agent class from the registry.-- Parameters:- - name (str): The name of the class to retrieve-- Returns:- - agent_cls (Type['Agent']): The class registered under the specified name.-- Raises:- - AgentNotRegisteredError: If name not registered- """- if name not in cls._registry:- raise AgentNotRegisteredError(name)- return cls._registry[name]-- @classmethod- def list_agents(cls) -> list[str]:- """Retrieves the list of all agent names from the registry.-- Raises:- - AgentNotRegisteredError: If no agent is registered- """- if not bool(cls._registry):- raise AgentNotRegisteredError()- return list(cls._registry.keys())-- def set_mcp_tools(self, mcp_tools: list[dict]) -> None:- """Sets the list of MCP tools for the agent.-- Args:- - mcp_tools (list[dict]): The list of MCP tools.- """- logger.info(- f'Setting {len(mcp_tools)} MCP tools for agent {self.name}: {[tool["function"]["name"] for tool in mcp_tools]}'- )- for tool in mcp_tools:- _tool = ChatCompletionToolParam(**tool)- if _tool['function']['name'] in self.mcp_tools:- logger.warning(- f'Tool {_tool["function"]["name"]} already exists, skipping'- )- continue- self.mcp_tools[_tool['function']['name']] = _tool- self.tools.append(_tool)- logger.info(- f'Tools updated for agent {self.name}, total {len(self.tools)}: {[tool["function"]["name"] for tool in self.tools]}'- )diff --git a/openhands/controller/agent_controller.py b/openhands/controller/agent_controller.pydeleted file mode 100644index f2a84a1a3a..0000000000--- a/openhands/controller/agent_controller.py+++ /dev/null@@ -1,1338 +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-# V1 replacement for this module lives in the Software Agent SDK.-from __future__ import annotations--import asyncio-import copy-import os-import time-from typing import Callable--from litellm.exceptions import ( # noqa- APIConnectionError,- APIError,- AuthenticationError,- BadRequestError,- ContentPolicyViolationError,- ContextWindowExceededError,- InternalServerError,- NotFoundError,- OpenAIError,- RateLimitError,- ServiceUnavailableError,- Timeout,-)--from openhands.controller.agent import Agent-from openhands.controller.replay import ReplayManager-from openhands.controller.state.state import State-from openhands.controller.state.state_tracker import StateTracker-from openhands.controller.stuck import StuckDetector-from openhands.core.config import AgentConfig, LLMConfig-from openhands.core.exceptions import (- AgentStuckInLoopError,- FunctionCallNotExistsError,- FunctionCallValidationError,- LLMContextWindowExceedError,- LLMMalformedActionError,- LLMNoActionError,- LLMResponseError,-)-from openhands.core.logger import LOG_ALL_EVENTS-from openhands.core.logger import openhands_logger as logger-from openhands.core.schema import AgentState-from openhands.events import (- EventSource,- EventStream,- EventStreamSubscriber,- RecallType,-)-from openhands.events.action import (- Action,- ActionConfirmationStatus,- ActionSecurityRisk,- AgentDelegateAction,- AgentFinishAction,- AgentRejectAction,- BrowseInteractiveAction,- BrowseURLAction,- ChangeAgentStateAction,- CmdRunAction,- FileEditAction,- FileReadAction,- FileWriteAction,- IPythonRunCellAction,- LoopRecoveryAction,- MCPAction,- MessageAction,- NullAction,- SystemMessageAction,-)-from openhands.events.action.agent import (- CondensationAction,- CondensationRequestAction,- RecallAction,-)-from openhands.events.event import Event-from openhands.events.observation import (- AgentDelegateObservation,- AgentStateChangedObservation,- ErrorObservation,- LoopDetectionObservation,- NullObservation,- Observation,-)-from openhands.events.serialization.event import truncate_content-from openhands.llm.metrics import Metrics-from openhands.runtime.runtime_status import RuntimeStatus-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.files import FileStore--# note: RESUME is only available on web GUI-TRAFFIC_CONTROL_REMINDER = (- "Please click on resume button if you'd like to continue, or start a new task."-)-ERROR_ACTION_NOT_EXECUTED_STOPPED_ID = 'AGENT_ERROR$ERROR_ACTION_NOT_EXECUTED_STOPPED'-ERROR_ACTION_NOT_EXECUTED_ERROR_ID = 'AGENT_ERROR$ERROR_ACTION_NOT_EXECUTED_ERROR'-ERROR_ACTION_NOT_EXECUTED_STOPPED = (- 'Stop button pressed. The action has not been executed.'-)-ERROR_ACTION_NOT_EXECUTED_ERROR = 'The action has not been executed due to a runtime error. The runtime system may have crashed and restarted due to resource constraints. Any previously established system state, dependencies, or environment variables may have been lost.'---class AgentController:- id: str- agent: Agent- max_iterations: int- event_stream: EventStream- state: State- confirmation_mode: bool- agent_to_llm_config: dict[str, LLMConfig]- agent_configs: dict[str, AgentConfig]- parent: 'AgentController | None' = None- delegate: 'AgentController | None' = None- _pending_action_info: tuple[Action, float] | None = None # (action, timestamp)- _closed: bool = False- _cached_first_user_message: MessageAction | None = None-- def __init__(- self,- agent: Agent,- event_stream: EventStream,- conversation_stats: ConversationStats,- iteration_delta: int,- budget_per_task_delta: float | None = None,- agent_to_llm_config: dict[str, LLMConfig] | None = None,- agent_configs: dict[str, AgentConfig] | None = None,- sid: str | None = None,- file_store: FileStore | None = None,- user_id: str | None = None,- confirmation_mode: bool = False,- initial_state: State | None = None,- is_delegate: bool = False,- headless_mode: bool = True,- status_callback: Callable | None = None,- replay_events: list[Event] | None = None,- ):- """Initializes a new instance of the AgentController class.-- Args:- agent: The agent instance to control.- event_stream: The event stream to publish events to.- max_iterations: The maximum number of iterations the agent can run.- max_budget_per_task: The maximum budget (in USD) allowed per task, beyond which the agent will stop.- agent_to_llm_config: A dictionary mapping agent names to LLM configurations in the case that- we delegate to a different agent.- agent_configs: A dictionary mapping agent names to agent configurations in the case that- we delegate to a different agent.- sid: The session ID of the agent.- confirmation_mode: Whether to enable confirmation mode for agent actions.- initial_state: The initial state of the controller.- is_delegate: Whether this controller is a delegate.- headless_mode: Whether the agent is run in headless mode.- status_callback: Optional callback function to handle status updates.- replay_events: A list of logs to replay.- """- self.id = sid or event_stream.sid- self.user_id = user_id- self.file_store = file_store- self.agent = agent- self.headless_mode = headless_mode- self.is_delegate = is_delegate- self.conversation_stats = conversation_stats-- # the event stream must be set before maybe subscribing to it- self.event_stream = event_stream-- # subscribe to the event stream if this is not a delegate- if not self.is_delegate:- self.event_stream.subscribe(- EventStreamSubscriber.AGENT_CONTROLLER, self.on_event, self.id- )-- self.state_tracker = StateTracker(sid, file_store, user_id)-- # state from the previous session, state from a parent agent, or a fresh state- self.set_initial_state(- state=initial_state,- conversation_stats=conversation_stats,- max_iterations=iteration_delta,- max_budget_per_task=budget_per_task_delta,- confirmation_mode=confirmation_mode,- )-- self.state = self.state_tracker.state # TODO: share between manager and controller for backward compatability; we should ideally move all state related logic to the state manager-- self.agent_to_llm_config = agent_to_llm_config if agent_to_llm_config else {}- self.agent_configs = agent_configs if agent_configs else {}- self._initial_max_iterations = iteration_delta- self._initial_max_budget_per_task = budget_per_task_delta-- # stuck helper- self._stuck_detector = StuckDetector(self.state)- self.status_callback = status_callback-- # replay-related- self._replay_manager = ReplayManager(replay_events)-- self.confirmation_mode = confirmation_mode-- # Add the system message to the event stream- self._add_system_message()-- def _add_system_message(self):- for event in self.event_stream.search_events(start_id=self.state.start_id):- if isinstance(event, MessageAction) and event.source == EventSource.USER:- # FIXME: Remove this after 6/1/2025- # Do not try to add a system message if we first run into- # a user message -- this means the eventstream exits before- # SystemMessageAction is introduced.- # We expect *agent* to handle this case gracefully.- return-- if isinstance(event, SystemMessageAction):- # Do not try to add the system message if it already exists- return-- # Add the system message to the event stream- # This should be done for all agents, including delegates- system_message = self.agent.get_system_message()- if system_message and system_message.content:- preview = (- system_message.content[:50] + '...'- if len(system_message.content) > 50- else system_message.content- )- logger.debug(f'System message: {preview}')- self.event_stream.add_event(system_message, EventSource.AGENT)-- async def close(self, set_stop_state: bool = True) -> None:- """Closes the agent controller, canceling any ongoing tasks and unsubscribing from the event stream.-- Note that it's fairly important that this closes properly, otherwise the state is incomplete.- """- if set_stop_state:- await self.set_agent_state_to(AgentState.STOPPED)-- self.state_tracker.close(self.event_stream)-- # unsubscribe from the event stream- # only the root parent controller subscribes to the event stream- if not self.is_delegate:- self.event_stream.unsubscribe(- EventStreamSubscriber.AGENT_CONTROLLER, self.id- )- self._closed = True-- def log(- self,- level: str,- message: str,- extra: dict | None = None,- exc_info: bool = False,- ) -> None:- """Logs a message to the agent controller's logger.-- Args:- level (str): The logging level to use (e.g., 'info', 'debug', 'error').- message (str): The message to log.- extra (dict | None, optional): Additional fields to log. Includes session_id by default.- exc_info (bool, optional): Whether to include exception info. Defaults to False.- """- message = f'[Agent Controller {self.id}] {message}'- if extra is None:- extra = {}- extra_merged = {'session_id': self.id, **extra}- getattr(logger, level)(- message, extra=extra_merged, exc_info=exc_info, stacklevel=2- )-- async def _react_to_exception(- self,- e: Exception,- ) -> None:- """React to an exception by setting the agent state to error and sending a status message."""- # Store the error reason before setting the agent state- self.state.last_error = f'{type(e).__name__}: {str(e)}'-- if self.status_callback is not None:- runtime_status = RuntimeStatus.ERROR- if isinstance(e, AuthenticationError):- runtime_status = RuntimeStatus.ERROR_LLM_AUTHENTICATION- self.state.last_error = runtime_status.value- elif isinstance(- e,- (- ServiceUnavailableError,- APIConnectionError,- APIError,- ),- ):- runtime_status = RuntimeStatus.ERROR_LLM_SERVICE_UNAVAILABLE- self.state.last_error = runtime_status.value- elif isinstance(e, InternalServerError):- runtime_status = RuntimeStatus.ERROR_LLM_INTERNAL_SERVER_ERROR- self.state.last_error = runtime_status.value- elif isinstance(e, BadRequestError) and 'ExceededBudget' in str(e):- runtime_status = RuntimeStatus.ERROR_LLM_OUT_OF_CREDITS- self.state.last_error = runtime_status.value- elif isinstance(e, ContentPolicyViolationError) or (- isinstance(e, BadRequestError)- and 'ContentPolicyViolationError' in str(e)- ):- runtime_status = RuntimeStatus.ERROR_LLM_CONTENT_POLICY_VIOLATION- self.state.last_error = runtime_status.value- elif isinstance(e, RateLimitError):- # Check if this is the final retry attempt- if (- hasattr(e, 'retry_attempt')- and hasattr(e, 'max_retries')- and e.retry_attempt >= e.max_retries- ):- # All retries exhausted, set to ERROR state with a special message- self.state.last_error = (- RuntimeStatus.AGENT_RATE_LIMITED_STOPPED_MESSAGE.value- )- await self.set_agent_state_to(AgentState.ERROR)- else:- # Still retrying, set to RATE_LIMITED state- await self.set_agent_state_to(AgentState.RATE_LIMITED)- return- self.status_callback('error', runtime_status, self.state.last_error)-- # Set the agent state to ERROR after storing the reason- await self.set_agent_state_to(AgentState.ERROR)-- def step(self) -> None:- asyncio.create_task(self._step_with_exception_handling())-- async def _step_with_exception_handling(self) -> None:- try:- await self._step()- except Exception as e:- self.log(- 'error',- f'Error while running the agent (session ID: {self.id}): {e}',- exc_info=True,- )- reported = RuntimeError(- f'There was an unexpected error while running the agent: {e.__class__.__name__}. You can refresh the page or ask the agent to try again.'- )- if (- isinstance(e, Timeout)- or isinstance(e, APIError)- or isinstance(e, BadRequestError)- or isinstance(e, NotFoundError)- or isinstance(e, InternalServerError)- or isinstance(e, AuthenticationError)- or isinstance(e, RateLimitError)- or isinstance(e, ContentPolicyViolationError)- or isinstance(e, LLMContextWindowExceedError)- ):- reported = e- else:- self.log(- 'warning',- f'Unknown exception type while running the agent: {type(e).__name__}.',- )- await self._react_to_exception(reported)-- def should_step(self, event: Event) -> bool:- """Whether the agent should take a step based on an event.-- In general, the agent should take a step if it receives a message from the user,- or observes something in the environment (after acting).- """- # it might be the delegate's day in the sun- if self.delegate is not None:- return False-- if isinstance(event, Action):- if isinstance(event, MessageAction) and event.source == EventSource.USER:- return True- if (- isinstance(event, MessageAction)- and self.get_agent_state() != AgentState.AWAITING_USER_INPUT- ):- # TODO: this is fragile, but how else to check if eligible?- return True- if isinstance(event, AgentDelegateAction):- return True- if isinstance(event, CondensationAction):- return True- if isinstance(event, CondensationRequestAction):- return True- return False- if isinstance(event, Observation):- if (- isinstance(event, NullObservation)- and event.cause is not None- and event.cause- > 0 # NullObservation has cause > 0 (RecallAction), not 0 (user message)- ):- return True- if isinstance(event, AgentStateChangedObservation) or isinstance(- event, NullObservation- ):- return False- return True- return False-- def on_event(self, event: Event) -> None:- """Callback from the event stream. Notifies the controller of incoming events.-- Args:- event (Event): The incoming event to process.- """- # If we have a delegate that is not finished or errored, forward events to it- if self.delegate is not None:- delegate_state = self.delegate.get_agent_state()- if (- delegate_state- not in (- AgentState.FINISHED,- AgentState.ERROR,- AgentState.REJECTED,- )- or 'RuntimeError: Agent reached maximum iteration.'- in self.delegate.state.last_error- or 'RuntimeError:Agent reached maximum budget for conversation'- in self.delegate.state.last_error- ):- # Forward the event to delegate and skip parent processing- asyncio.get_event_loop().run_until_complete(- self.delegate._on_event(event)- )- return- else:- # delegate is done or errored, so end it- self.end_delegate()- return-- # continue parent processing only if there's no active delegate- asyncio.get_event_loop().run_until_complete(self._on_event(event))-- async def _on_event(self, event: Event) -> None:- if hasattr(event, 'hidden') and event.hidden:- return-- self.state_tracker.add_history(event)-- if isinstance(event, Action):- await self._handle_action(event)- elif isinstance(event, Observation):- await self._handle_observation(event)-- should_step = self.should_step(event)- if should_step:- self.log(- 'debug',- f'Stepping agent after event: {type(event).__name__}',- extra={'msg_type': 'STEPPING_AGENT'},- )- await self._step_with_exception_handling()- elif isinstance(event, MessageAction) and event.source == EventSource.USER:- # If we received a user message but aren't stepping, log why- self.log(- 'warning',- f'Not stepping agent after user message. Current state: {self.get_agent_state()}',- extra={'msg_type': 'NOT_STEPPING_AFTER_USER_MESSAGE'},- )-- async def _handle_action(self, action: Action) -> None:- """Handles an Action from the agent or delegate."""- if isinstance(action, ChangeAgentStateAction):- await self.set_agent_state_to(action.agent_state) # type: ignore- elif isinstance(action, MessageAction):- await self._handle_message_action(action)- elif isinstance(action, AgentDelegateAction):- await self.start_delegate(action)- assert self.delegate is not None- # Post a MessageAction with the task for the delegate- if 'task' in action.inputs:- self.event_stream.add_event(- MessageAction(content='TASK: ' + action.inputs['task']),- EventSource.USER,- )- await self.delegate.set_agent_state_to(AgentState.RUNNING)- return-- elif isinstance(action, AgentFinishAction):- self.state.outputs = action.outputs- await self.set_agent_state_to(AgentState.FINISHED)- elif isinstance(action, AgentRejectAction):- self.state.outputs = action.outputs- await self.set_agent_state_to(AgentState.REJECTED)- elif isinstance(action, LoopRecoveryAction):- await self._handle_loop_recovery_action(action)-- async def _handle_observation(self, observation: Observation) -> None:- """Handles observation from the event stream.-- Args:- observation (observation): The observation to handle.- """- observation_to_print = copy.deepcopy(observation)- if len(observation_to_print.content) > self.agent.llm.config.max_message_chars:- observation_to_print.content = truncate_content(- observation_to_print.content, self.agent.llm.config.max_message_chars- )- # Use info level if LOG_ALL_EVENTS is set- log_level = 'info' if os.getenv('LOG_ALL_EVENTS') in ('true', '1') else 'debug'- self.log(- log_level, str(observation_to_print), extra={'msg_type': 'OBSERVATION'}- )-- # this happens for runnable actions and microagent actions- if self._pending_action and self._pending_action.id == observation.cause:- if self.state.agent_state == AgentState.AWAITING_USER_CONFIRMATION:- return-- self._pending_action = None-- if self.state.agent_state == AgentState.USER_CONFIRMED:- await self.set_agent_state_to(AgentState.RUNNING)- if self.state.agent_state == AgentState.USER_REJECTED:- await self.set_agent_state_to(AgentState.AWAITING_USER_INPUT)- return-- async def _handle_message_action(self, action: MessageAction) -> None:- """Handles message actions from the event stream.-- Args:- action (MessageAction): The message action to handle.- """- if action.source == EventSource.USER:- # Use info level if LOG_ALL_EVENTS is set- log_level = (- 'info' if os.getenv('LOG_ALL_EVENTS') in ('true', '1') else 'debug'- )- self.log(- log_level,- str(action),- extra={'msg_type': 'ACTION', 'event_source': EventSource.USER},- )-- # if this is the first user message for this agent, matters for the microagent info type- first_user_message = self._first_user_message()- is_first_user_message = (- action.id == first_user_message.id if first_user_message else False- )- recall_type = (- RecallType.WORKSPACE_CONTEXT- if is_first_user_message- else RecallType.KNOWLEDGE- )-- recall_action = RecallAction(query=action.content, recall_type=recall_type)- self._pending_action = recall_action- # this is source=USER because the user message is the trigger for the microagent retrieval- self.event_stream.add_event(recall_action, EventSource.USER)-- if self.get_agent_state() != AgentState.RUNNING:- await self.set_agent_state_to(AgentState.RUNNING)-- elif action.source == EventSource.AGENT:- # If the agent is waiting for a response, set the appropriate state- if action.wait_for_response:- await self.set_agent_state_to(AgentState.AWAITING_USER_INPUT)-- async def _handle_loop_recovery_action(self, action: LoopRecoveryAction) -> None:- # Check if this is a loop recovery option- if self._stuck_detector.stuck_analysis:- option = action.option-- # Handle the loop recovery option- if option == 1:- # Option 1: Restart from before loop- await self._perform_loop_recovery(self._stuck_detector.stuck_analysis)- elif option == 2:- # Option 2: Restart with last user message- await self._restart_with_last_user_message(- self._stuck_detector.stuck_analysis- )- elif option == 3:- # Option 3: Stop agent completely- await self.set_agent_state_to(AgentState.STOPPED)- return-- def _reset(self) -> None:- """Resets the agent controller."""- # Runnable actions need an Observation- # make sure there is an Observation with the tool call metadata to be recognized by the agent- # otherwise the pending action is found in history, but it's incomplete without an obs with tool result- if self._pending_action and hasattr(self._pending_action, 'tool_call_metadata'):- # find out if there already is an observation with the same tool call metadata- found_observation = False- for event in self.state.history:- if (- isinstance(event, Observation)- and event.tool_call_metadata- == self._pending_action.tool_call_metadata- ):- found_observation = True- break-- # make a new ErrorObservation with the tool call metadata- if not found_observation:- # Use different messages and IDs based on whether the agent was stopped by user or due to error- if self.state.agent_state == AgentState.STOPPED:- error_content = ERROR_ACTION_NOT_EXECUTED_STOPPED- error_id = ERROR_ACTION_NOT_EXECUTED_STOPPED_ID- else: # AgentState.ERROR- error_content = ERROR_ACTION_NOT_EXECUTED_ERROR- error_id = ERROR_ACTION_NOT_EXECUTED_ERROR_ID-- obs = ErrorObservation(- content=error_content,- error_id=error_id,- )- obs.tool_call_metadata = self._pending_action.tool_call_metadata- obs._cause = self._pending_action.id # type: ignore[attr-defined]- self.event_stream.add_event(obs, EventSource.AGENT)-- # NOTE: RecallActions don't need an ErrorObservation upon reset, as long as they have no tool calls-- # reset the pending action, this will be called when the agent is STOPPED or ERROR- self._pending_action = None- self.agent.reset()-- async def set_agent_state_to(self, new_state: AgentState) -> None:- """Updates the agent's state and handles side effects. Can emit events to the event stream.-- Args:- new_state (AgentState): The new state to set for the agent.- """- self.log(- 'info',- f'Setting agent({self.agent.name}) state from {self.state.agent_state} to {new_state}',- )-- if new_state == self.state.agent_state:- return-- # Store old state for control limits check- old_state = self.state.agent_state-- # Update agent state BEFORE calling _reset() so _reset() sees the correct state- self.state.agent_state = new_state-- if new_state in (AgentState.STOPPED, AgentState.ERROR):- self._reset()-- # User is allowing to check control limits and expand them if applicable- if old_state == AgentState.ERROR and new_state == AgentState.RUNNING:- self.state_tracker.maybe_increase_control_flags_limits(self.headless_mode)-- if self._pending_action is not None and (- new_state in (AgentState.USER_CONFIRMED, AgentState.USER_REJECTED)- ):- if hasattr(self._pending_action, 'thought'):- self._pending_action.thought = '' # type: ignore[union-attr]- if new_state == AgentState.USER_CONFIRMED:- confirmation_state = ActionConfirmationStatus.CONFIRMED- else:- confirmation_state = ActionConfirmationStatus.REJECTED- self._pending_action.confirmation_state = confirmation_state # type: ignore[attr-defined]- self._pending_action._id = None # type: ignore[attr-defined]- self.event_stream.add_event(self._pending_action, EventSource.AGENT)-- # Create observation with reason field if it's an error state- reason = ''- if new_state == AgentState.ERROR:- reason = self.state.last_error-- self.event_stream.add_event(- AgentStateChangedObservation('', self.state.agent_state, reason),- EventSource.ENVIRONMENT,- )-- # Save state whenever agent state changes to ensure we don't lose state- # in case of crashes or unexpected circumstances- self.save_state()-- def get_agent_state(self) -> AgentState:- """Returns the current state of the agent.-- Returns:- AgentState: The current state of the agent.- """- return self.state.agent_state-- async def start_delegate(self, action: AgentDelegateAction) -> None:- """Start a delegate agent to handle a subtask.-- OpenHands is a multi-agentic system. A `task` is a conversation between- OpenHands (the whole system) and the user, which might involve one or more inputs- from the user. It starts with an initial input (typically a task statement) from- the user, and ends with either an `AgentFinishAction` initiated by the agent, a- stop initiated by the user, or an error.-- A `subtask` is a conversation between an agent and the user, or another agent. If a `task`- is conducted by a single agent, then it's also a `subtask`. Otherwise, a `task` consists of- multiple `subtasks`, each executed by one agent.-- Args:- action (AgentDelegateAction): The action containing information about the delegate agent to start.- """- agent_cls: type[Agent] = Agent.get_cls(action.agent)- agent_config = self.agent_configs.get(action.agent, self.agent.config)- # Make sure metrics are shared between parent and child for global accumulation- delegate_agent = agent_cls(- config=agent_config, llm_registry=self.agent.llm_registry- )-- # Take a snapshot of the current metrics before starting the delegate- state = State(- session_id=self.id.removesuffix('-delegate'),- user_id=self.user_id,- inputs=action.inputs or {},- iteration_flag=self.state.iteration_flag,- budget_flag=self.state.budget_flag,- delegate_level=self.state.delegate_level + 1,- # global metrics should be shared between parent and child- metrics=self.state.metrics,- # start on top of the stream- start_id=self.event_stream.get_latest_event_id() + 1,- parent_metrics_snapshot=self.state_tracker.get_metrics_snapshot(),- parent_iteration=self.state.iteration_flag.current_value,- )- self.log(- 'debug',- f'start delegate, creating agent {delegate_agent.name}',- )-- # Create the delegate with is_delegate=True so it does NOT subscribe directly- self.delegate = AgentController(- sid=self.id + '-delegate',- file_store=self.file_store,- user_id=self.user_id,- agent=delegate_agent,- event_stream=self.event_stream,- conversation_stats=self.conversation_stats,- iteration_delta=self._initial_max_iterations,- budget_per_task_delta=self._initial_max_budget_per_task,- agent_to_llm_config=self.agent_to_llm_config,- agent_configs=self.agent_configs,- initial_state=state,- is_delegate=True,- headless_mode=self.headless_mode,- )-- def end_delegate(self) -> None:- """Ends the currently active delegate (e.g., if it is finished or errored).-- so that this controller can resume normal operation.- """- if self.delegate is None:- return-- delegate_state = self.delegate.get_agent_state()-- # update iteration that is shared across agents- self.state.iteration_flag.current_value = (- self.delegate.state.iteration_flag.current_value- )-- # Calculate delegate-specific metrics before closing the delegate- delegate_metrics = self.state.get_local_metrics()- logger.info(f'Local metrics for delegate: {delegate_metrics}')-- # close the delegate controller before adding new events- asyncio.get_event_loop().run_until_complete(self.delegate.close())-- if delegate_state in (AgentState.FINISHED, AgentState.REJECTED):- # retrieve delegate result- delegate_outputs = (- self.delegate.state.outputs if self.delegate.state else {}- )-- # prepare delegate result observation- # TODO: replace this with AI-generated summary (#2395)- # Filter out metrics from the formatted output to avoid clutter- display_outputs = {- k: v for k, v in delegate_outputs.items() if k != 'metrics'- }- formatted_output = ', '.join(- f'{key}: {value}' for key, value in display_outputs.items()- )- content = (- f'{self.delegate.agent.name} finishes task with {formatted_output}'- )- else:- # delegate state is ERROR- # emit AgentDelegateObservation with error content- delegate_outputs = (- self.delegate.state.outputs if self.delegate.state else {}- )- content = (- f'{self.delegate.agent.name} encountered an error during execution.'- )-- content = f'Delegated agent finished with result:\n\n{content}'-- # emit the delegate result observation- obs = AgentDelegateObservation(outputs=delegate_outputs, content=content)-- # associate the delegate action with the initiating tool call- for event in reversed(self.state.history):- if isinstance(event, AgentDelegateAction):- delegate_action = event- obs.tool_call_metadata = delegate_action.tool_call_metadata- break-- self.event_stream.add_event(obs, EventSource.AGENT)-- # unset delegate so parent can resume normal handling- self.delegate = None-- async def _step(self) -> None:- """Executes a single step of the parent or delegate agent. Detects stuck agents and limits on the number of iterations and the task budget."""- if self.get_agent_state() != AgentState.RUNNING:- self.log(- 'debug',- f'Agent not stepping because state is {self.get_agent_state()} (not RUNNING)',- extra={'msg_type': 'STEP_BLOCKED_STATE'},- )- return-- if self._pending_action:- action_id = getattr(self._pending_action, 'id', 'unknown')- action_type = type(self._pending_action).__name__- self.log(- 'debug',- f'Agent not stepping because of pending action: {action_type} (id={action_id})',- extra={'msg_type': 'STEP_BLOCKED_PENDING_ACTION'},- )- return-- self.log(- 'debug',- f'LEVEL {self.state.delegate_level} LOCAL STEP {self.state.get_local_step()} GLOBAL STEP {self.state.iteration_flag.current_value}',- extra={'msg_type': 'STEP'},- )-- # Synchronize spend across all llm services with the budget flag- self.state_tracker.sync_budget_flag_with_metrics()- if self.agent.config.enable_stuck_detection and self._is_stuck():- await self._react_to_exception(- AgentStuckInLoopError('Agent got stuck in a loop')- )- return-- try:- self.state_tracker.run_control_flags()- except Exception as e:- logger.warning('Control flag limits hit')- await self._react_to_exception(e)- return-- action: Action = NullAction()-- if self._replay_manager.should_replay():- # in replay mode, we don't let the agent to proceed- # instead, we replay the action from the replay trajectory- action = self._replay_manager.step()- else:- try:- action = self.agent.step(self.state)- if action is None:- raise LLMNoActionError('No action was returned')- action._source = EventSource.AGENT # type: ignore [attr-defined]- except (- LLMMalformedActionError,- LLMNoActionError,- LLMResponseError,- FunctionCallValidationError,- FunctionCallNotExistsError,- ) as e:- self.event_stream.add_event(- ErrorObservation(- content=str(e),- ),- EventSource.AGENT,- )- return- except (ContextWindowExceededError, BadRequestError, OpenAIError) as e:- # FIXME: this is a hack until a litellm fix is confirmed- # Check if this is a nested context window error- # We have to rely on string-matching because LiteLLM doesn't consistently- # wrap the failure in a ContextWindowExceededError- error_str = str(e).lower()- if (- 'contextwindowexceedederror' in error_str- or 'prompt is too long' in error_str- or 'input length and `max_tokens` exceed context limit' in error_str- or 'please reduce the length of' in error_str- or 'the request exceeds the available context size' in error_str- or 'context length exceeded' in error_str- # For OpenRouter context window errors- or (- 'sambanovaexception' in error_str- and 'maximum context length' in error_str- )- # For SambaNova context window errors - only match when both patterns are present- or isinstance(e, ContextWindowExceededError)- ):- if self.agent.config.enable_history_truncation:- self.event_stream.add_event(- CondensationRequestAction(), EventSource.AGENT- )- return- else:- raise LLMContextWindowExceedError()- # Check if this is a tool call validation error that should be recoverable- elif (- isinstance(e, BadRequestError)- and 'tool call validation failed' in error_str- and (- 'missing properties' in error_str- or 'missing required' in error_str- )- ):- # Handle tool call validation errors from Groq as recoverable errors- self.event_stream.add_event(- ErrorObservation(- content=f'Tool call validation failed: {str(e)}. Please check the tool parameters and try again.',- ),- EventSource.AGENT,- )- return- else:- raise e-- if action.runnable:- if self.state.confirmation_mode and (- type(action) is CmdRunAction- or type(action) is IPythonRunCellAction- or type(action) is BrowseInteractiveAction- or type(action) is BrowseURLAction- or type(action) is FileEditAction- or type(action) is FileReadAction- or type(action) is FileWriteAction- or type(action) is MCPAction- ):- # Check if the action has a security_risk attribute set by the LLM or security analyzer- security_risk = getattr(- action, 'security_risk', ActionSecurityRisk.UNKNOWN- )-- is_high_security_risk = security_risk == ActionSecurityRisk.HIGH- is_ask_for_every_action = security_risk == ActionSecurityRisk.UNKNOWN-- # If security_risk is HIGH, requires confirmation- # UNLESS it is CLI which will handle action risks it itself- if self.agent.config.cli_mode:- # TODO(refactor): this is not ideal to have CLI been an exception- # We should refactor agent controller to consider this in the future- # See issue: https://github.com/OpenHands/OpenHands/issues/10464- action.confirmation_state = ( # type: ignore[union-attr]- ActionConfirmationStatus.AWAITING_CONFIRMATION- )- # Only HIGH security risk actions require confirmation- elif (- is_high_security_risk or is_ask_for_every_action- ) and self.confirmation_mode:- logger.debug(- f'[non-CLI mode] Detected HIGH security risk in action: {action}. Ask for confirmation'- )- action.confirmation_state = ( # type: ignore[union-attr]- ActionConfirmationStatus.AWAITING_CONFIRMATION- )- self._pending_action = action-- if not isinstance(action, NullAction):- if (- hasattr(action, 'confirmation_state')- and action.confirmation_state- == ActionConfirmationStatus.AWAITING_CONFIRMATION- ):- await self.set_agent_state_to(AgentState.AWAITING_USER_CONFIRMATION)-- # Create and log metrics for frontend display- self._prepare_metrics_for_frontend(action)-- self.event_stream.add_event(action, action._source) # type: ignore [attr-defined]-- log_level = 'info' if LOG_ALL_EVENTS else 'debug'- self.log(log_level, str(action), extra={'msg_type': 'ACTION'})-- @property- def _pending_action(self) -> Action | None:- """Get the current pending action with time tracking.-- Returns:- Action | None: The current pending action, or None if there isn't one.- """- if self._pending_action_info is None:- return None-- action, timestamp = self._pending_action_info- current_time = time.time()- elapsed_time = current_time - timestamp-- # Log if the pending action has been active for a long time (but don't clear it)- if elapsed_time > 60.0: # 1 minute - just for logging purposes- action_id = getattr(action, 'id', 'unknown')- action_type = type(action).__name__- self.log(- 'info',- f'Pending action active for {elapsed_time:.2f}s: {action_type} (id={action_id})',- extra={'msg_type': 'PENDING_ACTION_TIMEOUT'},- )-- return action-- @_pending_action.setter- def _pending_action(self, action: Action | None) -> None:- """Set or clear the pending action with timestamp and logging.-- Args:- action: The action to set as pending, or None to clear.- """- if action is None:- if self._pending_action_info is not None:- prev_action, timestamp = self._pending_action_info- action_id = getattr(prev_action, 'id', 'unknown')- action_type = type(prev_action).__name__- elapsed_time = time.time() - timestamp- self.log(- 'debug',- f'Cleared pending action after {elapsed_time:.2f}s: {action_type} (id={action_id})',- extra={'msg_type': 'PENDING_ACTION_CLEARED'},- )- self._pending_action_info = None- else:- action_id = getattr(action, 'id', 'unknown')- action_type = type(action).__name__- self.log(- 'debug',- f'Set pending action: {action_type} (id={action_id})',- extra={'msg_type': 'PENDING_ACTION_SET'},- )- self._pending_action_info = (action, time.time())-- def get_state(self) -> State:- """Returns the current running state object.-- Returns:- State: The current state object.- """- return self.state-- def set_initial_state(- self,- state: State | None,- conversation_stats: ConversationStats,- max_iterations: int,- max_budget_per_task: float | None,- confirmation_mode: bool = False,- ):- self.state_tracker.set_initial_state(- self.id,- state,- conversation_stats,- max_iterations,- max_budget_per_task,- confirmation_mode,- )- # Always load from the event stream to avoid losing history- self.state_tracker._init_history(- self.event_stream,- )-- def get_trajectory(self, include_screenshots: bool = False) -> list[dict]:- # state history could be partially hidden/truncated before controller is closed- assert self._closed- return self.state_tracker.get_trajectory(include_screenshots)-- def _is_stuck(self) -> bool:- """Checks if the agent or its delegate is stuck in a loop.-- Returns:- bool: True if the agent is stuck, False otherwise.- """- # check if delegate stuck- if self.delegate and self.delegate._is_stuck():- return True-- return self._stuck_detector.is_stuck(self.headless_mode)-- def attempt_loop_recovery(self) -> bool:- """Attempts loop recovery when agent is stuck in a loop.- Only supports CLI for now.-- Returns:- bool: True if recovery was successful and agent should continue,- False if recovery failed or was not attempted.- """- # Check if we're in a loop- if not self._stuck_detector.stuck_analysis:- return False-- """Handle loop recovery in CLI mode by pausing the agent and presenting recovery options."""- recovery_point = self._stuck_detector.stuck_analysis.loop_start_idx-- # Present loop detection message- self.event_stream.add_event(- LoopDetectionObservation(- content=f"""β οΈ Agent detected in a loop!-Loop type: {self._stuck_detector.stuck_analysis.loop_type}-Loop detected at iteration {self.state.iteration_flag.current_value}-\nRecovery options:-/resume 1. Restart from before loop (preserves {recovery_point} events)-/resume 2. Restart with last user message (reuses your most recent instruction)-/exit. Quit directly-\nThe agent has been paused. Type '/resume 1', '/resume 2', or '/exit' to choose an option.-"""- ),- source=EventSource.ENVIRONMENT,- )-- # Pause the agent using the same mechanism as Ctrl+P- # This ensures consistent behavior and avoids event loop conflicts- self.event_stream.add_event(- ChangeAgentStateAction(AgentState.PAUSED),- EventSource.ENVIRONMENT, # Use ENVIRONMENT source to distinguish from user pause- )- return True-- def _prepare_metrics_for_frontend(self, action: Action) -> None:- """Create a minimal metrics object for frontend display and log it.-- To avoid performance issues with long conversations, we only keep:- - accumulated_cost: The current total cost- - accumulated_token_usage: Accumulated token statistics across all API calls- - max_budget_per_task: The maximum budget allowed for the task-- This includes metrics from both the agent's LLM and the condenser's LLM if it exists.-- Args:- action: The action to attach metrics to- """- # Get metrics from agent LLM- metrics = self.conversation_stats.get_combined_metrics()-- # Create a clean copy with only the fields we want to keep- clean_metrics = Metrics()- clean_metrics.accumulated_cost = metrics.accumulated_cost- clean_metrics._accumulated_token_usage = copy.deepcopy(- metrics.accumulated_token_usage- )-- # Add max_budget_per_task to metrics- if self.state.budget_flag:- clean_metrics.max_budget_per_task = self.state.budget_flag.max_value-- action.llm_metrics = clean_metrics-- # Log the metrics information for debugging- # Get the latest usage directly from the agent's metrics- latest_usage = None- if self.state.metrics.token_usages:- latest_usage = self.state.metrics.token_usages[-1]-- accumulated_usage = self.state.metrics.accumulated_token_usage- self.log(- 'debug',- f'Action metrics - accumulated_cost: {metrics.accumulated_cost}, max_budget: {metrics.max_budget_per_task}, '- f'latest tokens (prompt/completion/cache_read/cache_write): '- f'{latest_usage.prompt_tokens if latest_usage else 0}/'- f'{latest_usage.completion_tokens if latest_usage else 0}/'- f'{latest_usage.cache_read_tokens if latest_usage else 0}/'- f'{latest_usage.cache_write_tokens if latest_usage else 0}, '- f'accumulated tokens (prompt/completion): '- f'{accumulated_usage.prompt_tokens}/'- f'{accumulated_usage.completion_tokens}',- extra={'msg_type': 'METRICS'},- )-- def __repr__(self) -> str:- pending_action_info = '<none>'- if (- hasattr(self, '_pending_action_info')- and self._pending_action_info is not None- ):- action, timestamp = self._pending_action_info- action_id = getattr(action, 'id', 'unknown')- action_type = type(action).__name__- elapsed_time = time.time() - timestamp- pending_action_info = (- f'{action_type}(id={action_id}, elapsed={elapsed_time:.2f}s)'- )-- return (- f'AgentController(id={getattr(self, "id", "<uninitialized>")}, '- f'agent={getattr(self, "agent", "<uninitialized>")!r}, '- f'event_stream={getattr(self, "event_stream", "<uninitialized>")!r}, '- f'state={getattr(self, "state", "<uninitialized>")!r}, '- f'delegate={getattr(self, "delegate", "<uninitialized>")!r}, '- f'_pending_action={pending_action_info})'- )-- def _is_awaiting_observation(self) -> bool:- events = self.event_stream.search_events(reverse=True)- for event in events:- if isinstance(event, AgentStateChangedObservation):- result = event.agent_state == AgentState.RUNNING- return result- return False-- def _first_user_message(- self, events: list[Event] | None = None- ) -> MessageAction | None:- """Get the first user message for this agent.-- For regular agents, this is the first user message from the beginning (start_id=0).- For delegate agents, this is the first user message after the delegate's start_id.-- Args:- events: Optional list of events to search through. If None, uses the event stream.-- Returns:- MessageAction | None: The first user message, or None if no user message found- """- # If events list is provided, search through it- if events is not None:- return next(- (- e- for e in events- if isinstance(e, MessageAction) and e.source == EventSource.USER- ),- None,- )-- # Otherwise, use the original event stream logic with caching- # Return cached message if any- if self._cached_first_user_message is not None:- return self._cached_first_user_message-- # Find the first user message- self._cached_first_user_message = next(- (- e- for e in self.event_stream.search_events(- start_id=self.state.start_id,- )- if isinstance(e, MessageAction) and e.source == EventSource.USER- ),- None,- )- return self._cached_first_user_message-- async def _perform_loop_recovery(- self, stuck_analysis: StuckDetector.StuckAnalysis- ) -> None:- """Perform loop recovery by truncating memory and restarting from before the loop."""- recovery_point = stuck_analysis.loop_start_idx-- # Truncate memory to the recovery point- await self._truncate_memory_to_point(recovery_point)-- # Set agent state to AWAITING_USER_INPUT to allow user to provide new instructions- await self.set_agent_state_to(AgentState.AWAITING_USER_INPUT)-- self.event_stream.add_event(- LoopDetectionObservation(- content="""β Loop recovery completed. Agent has been reset to before the loop.-You can now provide new instructions to continue.-"""- ),- source=EventSource.ENVIRONMENT,- )-- async def _truncate_memory_to_point(self, recovery_point: int) -> None:- """Truncate memory to the specified recovery point."""- # Get all events from state history- all_events = self.state.history-- if recovery_point >= len(all_events):- return-- # Keep only events up to the recovery point- events_to_keep = all_events[:recovery_point]-- # Update state history- self.state.history = events_to_keep-- # Update end_id to reflect the truncation- if events_to_keep:- self.state.end_id = events_to_keep[-1].id- else:- self.state.end_id = -1-- # Clear any cached messages- self._cached_first_user_message = None-- async def _restart_with_last_user_message(- self, stuck_analysis: StuckDetector.StuckAnalysis- ) -> None:- """Restart the agent using the last user message as the new instruction."""-- # Find the last user message in the history- last_user_message = None- for event in reversed(self.state.history):- if isinstance(event, MessageAction) and event.source == EventSource.USER:- last_user_message = event- break-- if last_user_message:- # Truncate memory to just before the loop started- recovery_point = stuck_analysis.loop_start_idx- await self._truncate_memory_to_point(recovery_point)-- # Set agent state to RUNNING and re-use the last user message- await self.set_agent_state_to(AgentState.RUNNING)-- # Re-use the last user message as the new instruction- self.event_stream.add_event(- LoopDetectionObservation(- content=f"""\nβ Restarting with your last instruction: {last_user_message.content}-Agent is now continuing with the same task...-"""- ),- source=EventSource.ENVIRONMENT,- )-- # Create a new action with the last user message- new_action = MessageAction(- content=last_user_message.content, wait_for_response=False- )- new_action._source = EventSource.USER # type: ignore [attr-defined]-- # Process the action to restart the agent- await self._handle_action(new_action)- else:- # If no user message found, fall back to regular recovery- print('\nβ οΈ No previous user message found. Using standard recovery.')- await self._perform_loop_recovery(stuck_analysis)-- def save_state(self):- self.state_tracker.save_state()diff --git a/openhands/controller/replay.py b/openhands/controller/replay.pydeleted file mode 100644index 8294369c6a..0000000000--- a/openhands/controller/replay.py+++ /dev/null@@ -1,105 +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-from __future__ import annotations--from openhands.core.logger import openhands_logger as logger-from openhands.events.action.action import Action-from openhands.events.action.message import MessageAction-from openhands.events.event import Event, EventSource-from openhands.events.observation.empty import NullObservation-from openhands.events.serialization.event import event_from_dict---class ReplayManager:- """ReplayManager manages the lifecycle of a replay session of a given trajectory.-- Replay manager keeps track of a list of events, replays actions, and ignore- messages and observations.-- Note that unexpected or even errorneous results could happen if- 1) any action is non-deterministic, OR- 2) if the initial state before the replay session is different from the- initial state of the trajectory.- """-- def __init__(self, events: list[Event] | None):- replay_events = []- for event in events or []:- if event.source == EventSource.ENVIRONMENT:- # ignore ENVIRONMENT events as they are not issued by- # the user or agent, and should not be replayed- continue- if isinstance(event, NullObservation):- # ignore NullObservation- continue- replay_events.append(event)-- if replay_events:- logger.info(f'Replay events loaded, events length = {len(replay_events)}')- for index in range(len(replay_events) - 1):- event = replay_events[index]- if isinstance(event, MessageAction) and event.wait_for_response:- # For any message waiting for response that is not the last- # event, we override wait_for_response to False, as a response- # would have been included in the next event, and we don't- # want the user to interfere with the replay process- logger.info(- 'Replay events contains wait_for_response message action, ignoring wait_for_response'- )- event.wait_for_response = False- self.replay_events = replay_events- self.replay_mode = bool(replay_events)- self.replay_index = 0-- def _replayable(self) -> bool:- return (- self.replay_events is not None- and self.replay_index < len(self.replay_events)- and isinstance(self.replay_events[self.replay_index], Action)- )-- def should_replay(self) -> bool:- """Whether the controller is in trajectory replay mode, and the replay- hasn't finished. Note: after the replay is finished, the user and- the agent could continue to message/act.-- This method also moves "replay_index" to the next action, if applicable.- """- if not self.replay_mode:- return False-- assert self.replay_events is not None- while self.replay_index < len(self.replay_events) and not self._replayable():- self.replay_index += 1-- return self._replayable()-- def step(self) -> Action:- assert self.replay_events is not None- event = self.replay_events[self.replay_index]- assert isinstance(event, Action)- self.replay_index += 1- return event-- @staticmethod- def get_replay_events(trajectory: list[dict]) -> list[Event]:- if not isinstance(trajectory, list):- raise ValueError(- f'Expected a list in {trajectory}, got {type(trajectory).__name__}'- )- replay_events = []- for item in trajectory:- event = event_from_dict(item)- if event.source == EventSource.ENVIRONMENT:- # ignore ENVIRONMENT events as they are not issued by- # the user or agent, and should not be replayed- continue- # cannot add an event with _id to event stream- event._id = None # type: ignore[attr-defined]- replay_events.append(event)- return replay_eventsdiff --git a/openhands/controller/state/control_flags.py b/openhands/controller/state/control_flags.pydeleted file mode 100644index 2973b78a44..0000000000--- a/openhands/controller/state/control_flags.py+++ /dev/null@@ -1,102 +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-from __future__ import annotations--from dataclasses import dataclass-from typing import Generic, TypeVar--T = TypeVar(- 'T', int, float-) # Type for the value (int for iterations, float for budget)---@dataclass-class ControlFlag(Generic[T]):- """Base class for control flags that manage limits and state transitions."""-- limit_increase_amount: T- current_value: T- max_value: T- headless_mode: bool = False- _hit_limit: bool = False-- def reached_limit(self) -> bool:- """Check if the limit has been reached.-- Returns:- bool: True if the limit has been reached, False otherwise.- """- raise NotImplementedError-- def increase_limit(self, headless_mode: bool) -> None:- """Expand the limit when needed."""- raise NotImplementedError-- def step(self):- """Determine the next state based on the current state and mode.-- Returns:- ControlFlagState: The next state.- """- raise NotImplementedError---@dataclass-class IterationControlFlag(ControlFlag[int]):- """Control flag for managing iteration limits."""-- def reached_limit(self) -> bool:- """Check if the iteration limit has been reached."""- self._hit_limit = self.current_value >= self.max_value- return self._hit_limit-- def increase_limit(self, headless_mode: bool) -> None:- """Expand the iteration limit by adding the initial value."""- if not headless_mode and self._hit_limit:- self.max_value += self.limit_increase_amount- self._hit_limit = False-- def step(self):- if self.reached_limit():- raise RuntimeError(- f'Agent reached maximum iteration. '- f'Current iteration: {self.current_value}, max iteration: {self.max_value}'- )-- # Increment the current value- self.current_value += 1---@dataclass-class BudgetControlFlag(ControlFlag[float]):- """Control flag for managing budget limits."""-- def reached_limit(self) -> bool:- """Check if the budget limit has been reached."""- self._hit_limit = self.current_value >= self.max_value- return self._hit_limit-- def increase_limit(self, headless_mode) -> None:- """Expand the budget limit by adding the initial value to the current value."""- if self._hit_limit:- self.max_value = self.current_value + self.limit_increase_amount- self._hit_limit = False-- def step(self):- """Check if we've reached the limit and update state accordingly.-- Note: Unlike IterationControlFlag, this doesn't increment the value- as the budget is updated externally.- """- if self.reached_limit():- current_str = f'{self.current_value:.2f}'- max_str = f'{self.max_value:.2f}'- raise RuntimeError(- f'Agent reached maximum budget for conversation.'- f'Current budget: {current_str}, max budget: {max_str}'- )diff --git a/openhands/controller/state/state.py b/openhands/controller/state/state.pydeleted file mode 100644index 23299d72fb..0000000000--- a/openhands/controller/state/state.py+++ /dev/null@@ -1,318 +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-from __future__ import annotations--import base64-import os-import pickle-from dataclasses import dataclass, field-from enum import Enum-from typing import Any--import openhands-from openhands.controller.state.control_flags import (- BudgetControlFlag,- IterationControlFlag,-)-from openhands.controller.state.view import View-from openhands.core.logger import openhands_logger as logger-from openhands.core.schema import AgentState-from openhands.events.action import (- MessageAction,-)-from openhands.events.action.agent import AgentFinishAction-from openhands.events.event import Event, EventSource-from openhands.llm.metrics import Metrics-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.files import FileStore-from openhands.storage.locations import get_conversation_agent_state_filename--RESUMABLE_STATES = [- AgentState.RUNNING,- AgentState.PAUSED,- AgentState.AWAITING_USER_INPUT,- AgentState.FINISHED,-]---# NOTE: this is deprecated-class TrafficControlState(str, Enum):- # default state, no rate limiting- NORMAL = 'normal'-- # task paused due to traffic control- THROTTLING = 'throttling'-- # traffic control is temporarily paused- PAUSED = 'paused'---@dataclass-class State:- """Represents the running state of an agent in the OpenHands system, saving data of its operation and memory.-- - Multi-agent/delegate state:- - store the task (conversation between the agent and the user)- - the subtask (conversation between an agent and the user or another agent)- - global and local iterations- - delegate levels for multi-agent interactions- - almost stuck state-- - Running state of an agent:- - current agent state (e.g., LOADING, RUNNING, PAUSED)- - traffic control state for rate limiting- - confirmation mode- - the last error encountered-- - Data for saving and restoring the agent:- - save to and restore from a session- - serialize with pickle and base64-- - Save / restore data about message history- - start and end IDs for events in agent's history- - summaries and delegate summaries-- - Metrics:- - global metrics for the current task- - local metrics for the current subtask-- - Extra data:- - additional task-specific data- """-- session_id: str = ''- user_id: str | None = None- iteration_flag: IterationControlFlag = field(- default_factory=lambda: IterationControlFlag(- limit_increase_amount=100, current_value=0, max_value=100- )- )- conversation_stats: ConversationStats | None = None- budget_flag: BudgetControlFlag | None = None- confirmation_mode: bool = False- history: list[Event] = field(default_factory=list)- inputs: dict = field(default_factory=dict)- outputs: dict = field(default_factory=dict)- agent_state: AgentState = AgentState.LOADING- resume_state: AgentState | None = None-- # root agent has level 0, and every delegate increases the level by one- delegate_level: int = 0- # start_id and end_id track the range of events in history- start_id: int = -1- end_id: int = -1-- parent_metrics_snapshot: Metrics | None = None- parent_iteration: int = 100-- # NOTE: this is used by the controller to track parent's metrics snapshot before delegation- # evaluation tasks to store extra data needed to track the progress/state of the task.- extra_data: dict[str, Any] = field(default_factory=dict)- last_error: str = ''-- # NOTE: deprecated args, kept here temporarily for backwards compatability- # Will be remove in 30 days- iteration: int | None = None- local_iteration: int | None = None- max_iterations: int | None = None- traffic_control_state: TrafficControlState | None = None- local_metrics: Metrics | None = None- delegates: dict[tuple[int, int], tuple[str, str]] | None = None-- metrics: Metrics = field(default_factory=Metrics)-- def save_to_session(- self, sid: str, file_store: FileStore, user_id: str | None- ) -> None:- conversation_stats = self.conversation_stats- self.conversation_stats = None # Don't save conversation stats, handles itself-- pickled = pickle.dumps(self)- logger.debug(f'Saving state to session {sid}:{self.agent_state}')- encoded = base64.b64encode(pickled).decode('utf-8')- try:- file_store.write(- get_conversation_agent_state_filename(sid, user_id), encoded- )-- # see if state is in the old directory on saas/remote use cases and delete it.- if user_id:- filename = get_conversation_agent_state_filename(sid)- try:- file_store.delete(filename)- except Exception:- pass- except Exception as e:- logger.error(f'Failed to save state to session: {e}')- raise e-- self.conversation_stats = conversation_stats # restore reference-- @staticmethod- def restore_from_session(- sid: str, file_store: FileStore, user_id: str | None = None- ) -> 'State':- """Restores the state from the previously saved session."""- state: State- try:- encoded = file_store.read(- get_conversation_agent_state_filename(sid, user_id)- )- pickled = base64.b64decode(encoded)- state = pickle.loads(pickled)- except FileNotFoundError:- # if user_id is provided, we are in a saas/remote use case- # and we need to check if the state is in the old directory.- if user_id:- filename = get_conversation_agent_state_filename(sid)- encoded = file_store.read(filename)- pickled = base64.b64decode(encoded)- state = pickle.loads(pickled)- else:- raise FileNotFoundError(- f'Could not restore state from session file for sid: {sid}'- )- except Exception as e:- logger.debug(f'Could not restore state from session: {e}')- raise e-- # update state- if state.agent_state in RESUMABLE_STATES:- state.resume_state = state.agent_state- else:- state.resume_state = None-- # first state after restore- state.agent_state = AgentState.LOADING-- # We don't need to clean up deprecated fields here- # They will be handled by __getstate__ when the state is saved again-- return state-- def __getstate__(self) -> dict:- # don't pickle history, it will be restored from the event stream- state = self.__dict__.copy()- state['history'] = []-- # Remove any view caching attributes. They'll be rebuilt frmo the- # history after that gets reloaded.- state.pop('_history_checksum', None)- state.pop('_view', None)-- # Remove deprecated fields before pickling- state.pop('iteration', None)- state.pop('local_iteration', None)- state.pop('max_iterations', None)- state.pop('traffic_control_state', None)- state.pop('local_metrics', None)- state.pop('delegates', None)-- return state-- def __setstate__(self, state: dict) -> None:- # Check if we're restoring from an older version (before control flags)- is_old_version = 'iteration' in state-- # Convert old iteration tracking to new iteration_flag if needed- if is_old_version:- # Create iteration_flag from old values- max_iterations = state.get('max_iterations', 100)- current_iteration = state.get('iteration', 0)-- # Add the iteration_flag to the state- state['iteration_flag'] = IterationControlFlag(- limit_increase_amount=max_iterations,- current_value=current_iteration,- max_value=max_iterations,- )-- # Update the state- self.__dict__.update(state)-- # We keep the deprecated fields for backward compatibility- # They will be removed by __getstate__ when the state is saved again-- # make sure we always have the attribute history- if not hasattr(self, 'history'):- self.history = []-- # Ensure we have default values for new fields if they're missing- if not hasattr(self, 'iteration_flag'):- self.iteration_flag = IterationControlFlag(- limit_increase_amount=100, current_value=0, max_value=100- )-- if not hasattr(self, 'budget_flag'):- self.budget_flag = None-- def get_current_user_intent(self) -> tuple[str | None, list[str] | None]:- """Returns the latest user message and image(if provided) that appears after a FinishAction, or the first (the task) if nothing was finished yet."""- last_user_message = None- last_user_message_image_urls: list[str] | None = []- for event in reversed(self.view):- if isinstance(event, MessageAction) and event.source == 'user':- last_user_message = event.content- last_user_message_image_urls = event.image_urls- elif isinstance(event, AgentFinishAction):- if last_user_message is not None:- return last_user_message, None-- return last_user_message, last_user_message_image_urls-- def get_last_agent_message(self) -> MessageAction | None:- for event in reversed(self.view):- if isinstance(event, MessageAction) and event.source == EventSource.AGENT:- return event- return None-- def get_last_user_message(self) -> MessageAction | None:- for event in reversed(self.view):- if isinstance(event, MessageAction) and event.source == EventSource.USER:- return event- return None-- def to_llm_metadata(self, model_name: str, agent_name: str) -> dict:- metadata = {- 'session_id': self.session_id,- 'trace_version': openhands.__version__,- 'trace_user_id': self.user_id,- 'tags': [- f'model:{model_name}',- f'agent:{agent_name}',- f'web_host:{os.environ.get("WEB_HOST", "unspecified")}',- f'openhands_version:{openhands.__version__}',- ],- }- return metadata-- def get_local_step(self):- if not self.parent_iteration:- return self.iteration_flag.current_value-- return self.iteration_flag.current_value - self.parent_iteration-- def get_local_metrics(self):- if not self.parent_metrics_snapshot:- return self.metrics- return self.metrics.diff(self.parent_metrics_snapshot)-- @property- def view(self) -> View:- # Compute a simple checksum from the history to see if we can re-use any- # cached view.- history_checksum = len(self.history)- old_history_checksum = getattr(self, '_history_checksum', -1)-- # If the history has changed, we need to re-create the view and update- # the caching.- if history_checksum != old_history_checksum:- self._history_checksum = history_checksum- self._view = View.from_events(self.history)-- return self._viewdiff --git a/openhands/controller/state/state_tracker.py b/openhands/controller/state/state_tracker.pydeleted file mode 100644index b314f64255..0000000000--- a/openhands/controller/state/state_tracker.py+++ /dev/null@@ -1,275 +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-from openhands.controller.state.control_flags import (- BudgetControlFlag,- IterationControlFlag,-)-from openhands.controller.state.state import State-from openhands.core.logger import openhands_logger as logger-from openhands.events.action.agent import AgentDelegateAction, ChangeAgentStateAction-from openhands.events.action.empty import NullAction-from openhands.events.event import Event-from openhands.events.event_filter import EventFilter-from openhands.events.observation.agent import AgentStateChangedObservation-from openhands.events.observation.delegate import AgentDelegateObservation-from openhands.events.observation.empty import NullObservation-from openhands.events.serialization.event import event_to_trajectory-from openhands.events.stream import EventStream-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.files import FileStore---class StateTracker:- """Manages and synchronizes the state of an agent throughout its lifecycle.-- It is responsible for:- 1. Maintaining agent state persistence across sessions- 2. Managing agent history by filtering and tracking relevant events (previously done in the agent controller)- 3. Synchronizing metrics between the controller and LLM components- 4. Updating control flags for budget and iteration limits-- """-- def __init__(- self, sid: str | None, file_store: FileStore | None, user_id: str | None- ):- self.sid = sid- self.file_store = file_store- self.user_id = user_id-- # filter out events that are not relevant to the agent- # so they will not be included in the agent history- self.agent_history_filter = EventFilter(- exclude_types=(- NullAction,- NullObservation,- ChangeAgentStateAction,- AgentStateChangedObservation,- ),- exclude_hidden=True,- )-- def set_initial_state(- self,- id: str,- state: State | None,- conversation_stats: ConversationStats,- max_iterations: int,- max_budget_per_task: float | None,- confirmation_mode: bool = False,- ) -> None:- """Sets the initial state for the agent, either from the previous session, or from a parent agent, or by creating a new one.-- Args:- state: The state to initialize with, or None to create a new state.- max_iterations: The maximum number of iterations allowed for the task.- confirmation_mode: Whether to enable confirmation mode.- """- # state can come from:- # - the previous session, in which case it has history- # - from a parent agent, in which case it has no history- # - None / a new state-- # If state is None, we create a brand new state and still load the event stream so we can restore the history- if state is None:- self.state = State(- session_id=id.removesuffix('-delegate'),- user_id=self.user_id,- inputs={},- conversation_stats=conversation_stats,- iteration_flag=IterationControlFlag(- limit_increase_amount=max_iterations,- current_value=0,- max_value=max_iterations,- ),- budget_flag=None- if not max_budget_per_task- else BudgetControlFlag(- limit_increase_amount=max_budget_per_task,- current_value=0,- max_value=max_budget_per_task,- ),- confirmation_mode=confirmation_mode,- )- self.state.start_id = 0-- logger.info(- f'AgentController {id} - created new state. start_id: {self.state.start_id}'- )- else:- self.state = state- if self.state.start_id <= -1:- self.state.start_id = 0-- state.conversation_stats = conversation_stats-- def _init_history(self, event_stream: EventStream) -> None:- """Initializes the agent's history from the event stream.-- The history is a list of events that:- - Excludes events of types listed in self.filter_out- - Excludes events with hidden=True attribute- - For delegate events (between AgentDelegateAction and AgentDelegateObservation):- - Excludes all events between the action and observation- - Includes the delegate action and observation themselves- """- # define range of events to fetch- # delegates start with a start_id and initially won't find any events- # otherwise we're restoring a previous session- start_id = self.state.start_id if self.state.start_id >= 0 else 0- end_id = (- self.state.end_id- if self.state.end_id >= 0- else event_stream.get_latest_event_id()- )-- # sanity check- if start_id > end_id + 1:- logger.warning(- f'start_id {start_id} is greater than end_id + 1 ({end_id + 1}). History will be empty.',- )- self.state.history = []- return-- events: list[Event] = []-- # Get rest of history- events_to_add = list(- event_stream.search_events(- start_id=start_id,- end_id=end_id,- reverse=False,- filter=self.agent_history_filter,- )- )- events.extend(events_to_add)-- # Find all delegate action/observation pairs- delegate_ranges: list[tuple[int, int]] = []- delegate_action_ids: list[int] = [] # stack of unmatched delegate action IDs-- for event in events:- if isinstance(event, AgentDelegateAction):- delegate_action_ids.append(event.id)- # Note: we can get agent=event.agent and task=event.inputs.get('task','')- # if we need to track these in the future-- elif isinstance(event, AgentDelegateObservation):- # Match with most recent unmatched delegate action- if not delegate_action_ids:- logger.warning(- f'Found AgentDelegateObservation without matching action at id={event.id}',- )- continue-- action_id = delegate_action_ids.pop()- delegate_ranges.append((action_id, event.id))-- # Filter out events between delegate action/observation pairs- if delegate_ranges:- filtered_events: list[Event] = []- current_idx = 0-- for start_id, end_id in sorted(delegate_ranges):- # Add events before delegate range- filtered_events.extend(- event for event in events[current_idx:] if event.id < start_id- )-- # Add delegate action and observation- filtered_events.extend(- event for event in events if event.id in (start_id, end_id)- )-- # Update index to after delegate range- current_idx = next(- (i for i, e in enumerate(events) if e.id > end_id), len(events)- )-- # Add any remaining events after last delegate range- filtered_events.extend(events[current_idx:])-- self.state.history = filtered_events- else:- self.state.history = events-- # make sure history is in sync- self.state.start_id = start_id-- def close(self, event_stream: EventStream):- # we made history, now is the time to rewrite it!- # the final state.history will be used by external scripts like evals, tests, etc.- # history will need to be complete WITH delegates events- # like the regular agent history, it does not include:- # - 'hidden' events, events with hidden=True- # - backend events (the default 'filtered out' types, types in self.filter_out)- start_id = self.state.start_id if self.state.start_id >= 0 else 0- end_id = (- self.state.end_id- if self.state.end_id >= 0- else event_stream.get_latest_event_id()- )-- self.state.history = list(- event_stream.search_events(- start_id=start_id,- end_id=end_id,- reverse=False,- filter=self.agent_history_filter,- )- )-- def add_history(self, event: Event):- # if the event is not filtered out, add it to the history- if self.agent_history_filter.include(event):- self.state.history.append(event)-- def get_trajectory(self, include_screenshots: bool = False) -> list[dict]:- return [- event_to_trajectory(event, include_screenshots)- for event in self.state.history- ]-- def maybe_increase_control_flags_limits(self, headless_mode: bool):- # Iteration and budget extensions are independent of each other- # An error will be thrown if any one of the control flags have reached or exceeded its limit- self.state.iteration_flag.increase_limit(headless_mode)- if self.state.budget_flag:- self.state.budget_flag.increase_limit(headless_mode)-- def get_metrics_snapshot(self):- """Deep copy of metrics- This serves as a snapshot for the parent's metrics at the time a delegate is created- It will be stored and used to compute local metrics for the delegate- (since delegates now accumulate metrics from where its parent left off)- """- return self.state.metrics.copy()-- def save_state(self):- """Save's current state to persistent store"""- if self.sid and self.file_store:- self.state.save_to_session(self.sid, self.file_store, self.user_id)-- if self.state.conversation_stats:- self.state.conversation_stats.save_metrics()-- def run_control_flags(self):- """Performs one step of the control flags"""- self.state.iteration_flag.step()- if self.state.budget_flag:- self.state.budget_flag.step()-- def sync_budget_flag_with_metrics(self):- """Ensures that budget flag is up to date with accumulated costs from llm completions- Budget flag will monitor for when budget is exceeded- """- # Sync cost across all llm services from llm registry- if self.state.budget_flag and self.state.conversation_stats:- self.state.budget_flag.current_value = (- self.state.conversation_stats.get_combined_metrics().accumulated_cost- )diff --git a/openhands/controller/state/view.py b/openhands/controller/state/view.pydeleted file mode 100644index 81dd8bab5d..0000000000--- a/openhands/controller/state/view.py+++ /dev/null@@ -1,96 +0,0 @@-from __future__ import annotations--from typing import overload--from pydantic import BaseModel--from openhands.core.logger import openhands_logger as logger-from openhands.events.action.agent import CondensationAction, CondensationRequestAction-from openhands.events.event import Event-from openhands.events.observation.agent import AgentCondensationObservation---class View(BaseModel):- """Linearly ordered view of events.-- Produced by a condenser to indicate the included events are ready to process as LLM input.- """-- events: list[Event]- unhandled_condensation_request: bool = False- # Set of event IDs that have been forgotten/condensed- forgotten_event_ids: set[int] = set()-- def __len__(self) -> int:- return len(self.events)-- def __iter__(self):- return iter(self.events)-- # To preserve list-like indexing, we ideally support slicing and position-based indexing.- # The only challenge with that is switching the return type based on the input type -- we- # can mark the different signatures for MyPy with `@overload` decorators.-- @overload- def __getitem__(self, key: slice) -> list[Event]: ...-- @overload- def __getitem__(self, key: int) -> Event: ...-- def __getitem__(self, key: int | slice) -> Event | list[Event]:- if isinstance(key, slice):- start, stop, step = key.indices(len(self))- return [self[i] for i in range(start, stop, step)]- elif isinstance(key, int):- return self.events[key]- else:- raise ValueError(f'Invalid key type: {type(key)}')-- @staticmethod- def from_events(events: list[Event]) -> View:- """Create a view from a list of events, respecting the semantics of any condensation events."""- forgotten_event_ids: set[int] = set()- for event in events:- if isinstance(event, CondensationAction):- forgotten_event_ids.update(event.forgotten)- # Make sure we also forget the condensation action itself- forgotten_event_ids.add(event.id)- if isinstance(event, CondensationRequestAction):- forgotten_event_ids.add(event.id)-- kept_events = [event for event in events if event.id not in forgotten_event_ids]-- # If we have a summary, insert it at the specified offset.- summary: str | None = None- summary_offset: int | None = None-- # The relevant summary is always in the last condensation event (i.e., the most recent one).- for event in reversed(events):- if isinstance(event, CondensationAction):- if event.summary is not None and event.summary_offset is not None:- summary = event.summary- summary_offset = event.summary_offset- break-- if summary is not None and summary_offset is not None:- logger.info(f'Inserting summary at offset {summary_offset}')-- kept_events.insert(- summary_offset, AgentCondensationObservation(content=summary)- )-- # Check for an unhandled condensation request -- these are events closer to the- # end of the list than any condensation action.- unhandled_condensation_request = False- for event in reversed(events):- if isinstance(event, CondensationAction):- break- if isinstance(event, CondensationRequestAction):- unhandled_condensation_request = True- break-- return View(- events=kept_events,- unhandled_condensation_request=unhandled_condensation_request,- forgotten_event_ids=forgotten_event_ids,- )diff --git a/openhands/controller/stuck.py b/openhands/controller/stuck.pydeleted file mode 100644index c951d89540..0000000000--- a/openhands/controller/stuck.py+++ /dev/null@@ -1,488 +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-from dataclasses import dataclass-from typing import Optional--from openhands.controller.state.state import State-from openhands.core.logger import openhands_logger as logger-from openhands.events import Event, EventSource-from openhands.events.action.action import Action-from openhands.events.action.commands import IPythonRunCellAction-from openhands.events.action.empty import NullAction-from openhands.events.action.message import MessageAction-from openhands.events.observation import (- CmdOutputObservation,- IPythonRunCellObservation,-)-from openhands.events.observation.agent import AgentCondensationObservation-from openhands.events.observation.empty import NullObservation-from openhands.events.observation.error import ErrorObservation-from openhands.events.observation.observation import Observation---class StuckDetector:- SYNTAX_ERROR_MESSAGES = [- 'SyntaxError: unterminated string literal (detected at line',- 'SyntaxError: invalid syntax. Perhaps you forgot a comma?',- 'SyntaxError: incomplete input',- ]-- @dataclass- class StuckAnalysis:- loop_type: str- loop_repeat_times: int- loop_start_idx: int # in filtered_history-- def __init__(self, state: State):- self.state = state- self.stuck_analysis: Optional[StuckDetector.StuckAnalysis] = None-- def is_stuck(self, headless_mode: bool = True) -> bool:- """Checks if the agent is stuck in a loop.-- Args:- headless_mode: Matches AgentController's headless_mode.- If True: Consider all history (automated/testing)- If False: Consider only history after last user message (interactive)-- Returns:- bool: True if the agent is stuck in a loop, False otherwise.- """- filtered_history_offset = 0- if not headless_mode:- # In interactive mode, only look at history after the last user message- last_user_msg_idx = -1- for i, event in enumerate(reversed(self.state.history)):- if (- isinstance(event, MessageAction)- and event.source == EventSource.USER- ):- last_user_msg_idx = len(self.state.history) - i - 1- break- filtered_history_offset = last_user_msg_idx + 1- history_to_check = self.state.history[last_user_msg_idx + 1 :]- else:- # In headless mode, look at all history- history_to_check = self.state.history-- # Filter out user messages and null events- filtered_history = [- event- for event in history_to_check- if not (- # Filter works elegantly in both modes:- # - In headless: actively filters out user messages from full history- # - In non-headless: no-op since we already sliced after last user message- (isinstance(event, MessageAction) and event.source == EventSource.USER)- # there might be some NullAction or NullObservation in the history at least for now- or isinstance(event, (NullAction, NullObservation))- )- ]-- # it takes 3 actions minimum to detect a loop, otherwise nothing to do here- if len(filtered_history) < 3:- return False-- # the first few scenarios detect 3 or 4 repeated steps- # prepare the last 4 actions and observations, to check them out- last_actions: list[Event] = []- last_observations: list[Event] = []-- # retrieve the last four actions and observations starting from the end of history, wherever they are- for event in reversed(filtered_history):- if isinstance(event, Action) and len(last_actions) < 4:- last_actions.append(event)- elif isinstance(event, Observation) and len(last_observations) < 4:- last_observations.append(event)-- if len(last_actions) == 4 and len(last_observations) == 4:- break-- # scenario 1: same action, same observation- if self._is_stuck_repeating_action_observation(- last_actions, last_observations, filtered_history, filtered_history_offset- ):- return True-- # scenario 2: same action, errors- if self._is_stuck_repeating_action_error(- last_actions, last_observations, filtered_history, filtered_history_offset- ):- return True-- # scenario 3: monologue- if self._is_stuck_monologue(filtered_history, filtered_history_offset):- return True-- # scenario 4: action, observation pattern on the last six steps- if len(filtered_history) >= 6:- if self._is_stuck_action_observation_pattern(- filtered_history, filtered_history_offset- ):- return True-- # scenario 5: context window error loop- if len(filtered_history) >= 10:- if self._is_stuck_context_window_error(- filtered_history, filtered_history_offset- ):- return True-- # Empty stuck_analysis when not stuck- self.stuck_analysis = None- return False-- def _is_stuck_repeating_action_observation(- self,- last_actions: list[Event],- last_observations: list[Event],- filtered_history: list[Event],- filtered_history_offset: int = 0,- ) -> bool:- # scenario 1: same action, same observation- # it takes 4 actions and 4 observations to detect a loop- # assert len(last_actions) == 4 and len(last_observations) == 4-- # Check for a loop of 4 identical action-observation pairs- if len(last_actions) == 4 and len(last_observations) == 4:- actions_equal = all(- self._eq_no_pid(last_actions[0], action) for action in last_actions- )- observations_equal = all(- self._eq_no_pid(last_observations[0], observation)- for observation in last_observations- )-- if actions_equal and observations_equal:- logger.warning('Action, Observation loop detected')- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='repeating_action_observation',- loop_repeat_times=4,- loop_start_idx=filtered_history.index(last_actions[-1])- + filtered_history_offset,- )- return True-- return False-- def _is_stuck_repeating_action_error(- self,- last_actions: list[Event],- last_observations: list[Event],- filtered_history: list[Event],- filtered_history_offset: int = 0,- ) -> bool:- # scenario 2: same action, errors- # it takes 3 actions and 3 observations to detect a loop- # check if the last three actions are the same and result in errors-- if len(last_actions) < 3 or len(last_observations) < 3:- return False-- # are the last three actions the "same"?- if all(self._eq_no_pid(last_actions[0], action) for action in last_actions[:3]):- # and the last three observations are all errors?- if all(isinstance(obs, ErrorObservation) for obs in last_observations[:3]):- logger.warning('Action, ErrorObservation loop detected')- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='repeating_action_error',- loop_repeat_times=3,- loop_start_idx=filtered_history.index(last_actions[-1])- + filtered_history_offset,- )- return True- # or, are the last three observations all IPythonRunCellObservation with SyntaxError?- elif all(- isinstance(obs, IPythonRunCellObservation)- for obs in last_observations[:3]- ):- warning = 'Action, IPythonRunCellObservation loop detected'- for error_message in self.SYNTAX_ERROR_MESSAGES:- if error_message.startswith(- 'SyntaxError: unterminated string literal (detected at line'- ):- if self._check_for_consistent_line_error(- [- obs- for obs in last_observations[:3]- if isinstance(obs, IPythonRunCellObservation)- ],- error_message,- ):- logger.warning(warning)- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='repeating_action_error',- loop_repeat_times=3,- loop_start_idx=filtered_history.index(last_actions[-1])- + filtered_history_offset,- )- return True- elif error_message in (- 'SyntaxError: invalid syntax. Perhaps you forgot a comma?',- 'SyntaxError: incomplete input',- ) and self._check_for_consistent_invalid_syntax(- [- obs- for obs in last_observations[:3]- if isinstance(obs, IPythonRunCellObservation)- ],- error_message,- ):- logger.warning(warning)- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='repeating_action_error',- loop_repeat_times=3,- loop_start_idx=filtered_history.index(last_actions[-1])- + filtered_history_offset,- )- return True- return False-- def _check_for_consistent_invalid_syntax(- self, observations: list[IPythonRunCellObservation], error_message: str- ) -> bool:- first_lines = []- valid_observations = []-- for obs in observations:- content = obs.content- lines = content.strip().split('\n')-- if len(lines) < 6: # 6 because a real syntax error has at least 6 lines- return False-- line1 = lines[0].strip()- if not line1.startswith('Cell In[1], line'):- return False-- first_lines.append(line1) # Store the first line of each observation-- # Check last three lines- if (- lines[-1].startswith('[Jupyter Python interpreter:')- and lines[-2].startswith('[Jupyter current working directory:')- and error_message in lines[-3]- ):- valid_observations.append(obs)-- # Check if:- # 1. All first lines are identical- # 2. We have exactly 3 valid observations- # 3. The error message line is identical in all valid observations- return (- len(set(first_lines)) == 1- and len(valid_observations) == 3- and len(- set(- obs.content.strip().split('\n')[:-2][-1]- for obs in valid_observations- )- )- == 1- )-- def _check_for_consistent_line_error(- self, observations: list[IPythonRunCellObservation], error_message: str- ) -> bool:- error_lines = []-- for obs in observations:- content = obs.content- lines = content.strip().split('\n')-- if len(lines) < 3:- return False-- last_lines = lines[-3:]-- # Check if the last two lines are our own- if not (- last_lines[-2].startswith('[Jupyter current working directory:')- and last_lines[-1].startswith('[Jupyter Python interpreter:')- ):- return False-- # Check for the error message in the 3rd-to-last line- if error_message in last_lines[-3]:- error_lines.append(last_lines[-3])-- # Check if we found the error message in all 3 observations- # and the 3rd-to-last line is identical across all occurrences- return len(error_lines) == 3 and len(set(error_lines)) == 1-- def _is_stuck_monologue(- self, filtered_history: list[Event], filtered_history_offset: int = 0- ) -> bool:- # scenario 3: monologue- # check for repeated MessageActions with source=AGENT- # see if the agent is engaged in a good old monologue, telling itself the same thing over and over- agent_message_actions = [- (i, event)- for i, event in enumerate(filtered_history)- if isinstance(event, MessageAction) and event.source == EventSource.AGENT- ]-- # last three message actions will do for this check- if len(agent_message_actions) >= 3:- last_agent_message_actions = agent_message_actions[-3:]-- if all(- (last_agent_message_actions[0][1] == action[1])- for action in last_agent_message_actions- ):- # check if there are any observations between the repeated MessageActions- # then it's not yet a loop, maybe it can recover- start_index = last_agent_message_actions[0][0]- end_index = last_agent_message_actions[-1][0]-- has_observation_between = False- for event in filtered_history[start_index + 1 : end_index]:- if isinstance(event, Observation):- has_observation_between = True- break-- if not has_observation_between:- logger.warning('Repeated MessageAction with source=AGENT detected')- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='monologue',- loop_repeat_times=3,- loop_start_idx=start_index + filtered_history_offset,- )- return True- return False-- def _is_stuck_action_observation_pattern(- self, filtered_history: list[Event], filtered_history_offset: int = 0- ) -> bool:- # scenario 4: action, observation pattern on the last six steps- # check if the agent repeats the same (Action, Observation)- # every other step in the last six steps- last_six_actions: list[Event] = []- last_six_observations: list[Event] = []-- # the end of history is most interesting- for event in reversed(filtered_history):- if isinstance(event, Action) and len(last_six_actions) < 6:- last_six_actions.append(event)- elif isinstance(event, Observation) and len(last_six_observations) < 6:- last_six_observations.append(event)-- if len(last_six_actions) == 6 and len(last_six_observations) == 6:- break-- # this pattern is every other step, like:- # (action_1, obs_1), (action_2, obs_2), (action_1, obs_1), (action_2, obs_2),...- if len(last_six_actions) == 6 and len(last_six_observations) == 6:- actions_equal = (- # action_0 == action_2 == action_4- self._eq_no_pid(last_six_actions[0], last_six_actions[2])- and self._eq_no_pid(last_six_actions[0], last_six_actions[4])- # action_1 == action_3 == action_5- and self._eq_no_pid(last_six_actions[1], last_six_actions[3])- and self._eq_no_pid(last_six_actions[1], last_six_actions[5])- )- observations_equal = (- # obs_0 == obs_2 == obs_4- self._eq_no_pid(last_six_observations[0], last_six_observations[2])- and self._eq_no_pid(last_six_observations[0], last_six_observations[4])- # obs_1 == obs_3 == obs_5- and self._eq_no_pid(last_six_observations[1], last_six_observations[3])- and self._eq_no_pid(last_six_observations[1], last_six_observations[5])- )-- if actions_equal and observations_equal:- logger.warning('Action, Observation pattern detected')- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='repeating_action_observation_pattern',- loop_repeat_times=3,- loop_start_idx=filtered_history.index(last_six_actions[-1])- + filtered_history_offset,- )- return True- return False-- def _is_stuck_context_window_error(- self, filtered_history: list[Event], filtered_history_offset: int = 0- ) -> bool:- """Detects if we're stuck in a loop of context window errors.-- This happens when we repeatedly get context window errors and try to trim,- but the trimming doesn't work, causing us to get more context window errors.- The pattern is repeated AgentCondensationObservation events without any other- events between them.-- Args:- filtered_history: List of filtered events to check-- Returns:- bool: True if we detect a context window error loop- """- # Look for AgentCondensationObservation events- condensation_events = [- (i, event)- for i, event in enumerate(filtered_history)- if isinstance(event, AgentCondensationObservation)- ]-- # Need at least 10 condensation events to detect a loop- if len(condensation_events) < 10:- return False-- # Get the last 10 condensation events- last_condensation_events = condensation_events[-10:]-- # Check if there are any non-condensation events between them- for i in range(len(last_condensation_events) - 1):- start_idx = last_condensation_events[i][0]- end_idx = last_condensation_events[i + 1][0]-- # Look for any non-condensation events between these two- has_other_events = False- for event in filtered_history[start_idx + 1 : end_idx]:- if not isinstance(event, AgentCondensationObservation):- has_other_events = True- break-- if not has_other_events:- logger.warning(- 'Context window error loop detected - repeated condensation events'- )- self.stuck_analysis = StuckDetector.StuckAnalysis(- loop_type='context_window_error',- loop_repeat_times=2,- loop_start_idx=start_idx + filtered_history_offset,- )- return True-- return False-- def _eq_no_pid(self, obj1: Event, obj2: Event) -> bool:- if isinstance(obj1, IPythonRunCellAction) and isinstance(- obj2, IPythonRunCellAction- ):- # for loop detection on edit actions, ignore the thought, compare some code- # the code should have at least 3 lines, to avoid simple one-liners- if (- 'edit_file_by_replace(' in obj1.code- and 'edit_file_by_replace(' in obj2.code- ):- return (- len(obj1.code.split('\n')) > 2- and obj1.code.split('\n')[:3] == obj2.code.split('\n')[:3]- )- else:- # default comparison- return obj1 == obj2- elif isinstance(obj1, CmdOutputObservation) and isinstance(- obj2, CmdOutputObservation- ):- # for loop detection, ignore command_id, which is the pid- return obj1.command == obj2.command and obj1.exit_code == obj2.exit_code- else:- # this is the default comparison- return obj1 == obj2diff --git a/openhands/core/config/agent_config.py b/openhands/core/config/agent_config.pyindex 8f994ccfa1..09034a6a76 100644--- a/openhands/core/config/agent_config.py+++ b/openhands/core/config/agent_config.py@@ -16,7 +16,6 @@ from openhands.core.config.condenser_config import (from openhands.core.config.extended_config import ExtendedConfigfrom openhands.core.config.model_routing_config import ModelRoutingConfigfrom openhands.core.logger import openhands_logger as logger-from openhands.utils.import_utils import get_implclass AgentConfig(BaseModel):@@ -130,38 +129,4 @@ class AgentConfig(BaseModel):# Still add it to the mappingagent_mapping['agent'] = base_config- # Process each custom section independently- for name, overrides in custom_sections.items():- try:- # Merge base config with overrides- merged = {**base_config.model_dump(), **overrides}- if merged.get('classpath'):- # if an explicit classpath is given, try to load it and look up its config model class- from openhands.controller.agent import Agent-- try:- agent_cls = get_impl(Agent, merged.get('classpath'))- custom_config = agent_cls.config_model.model_validate(merged)- except Exception as e:- logger.warning(- f'Failed to load custom agent class [{merged.get("classpath")}]: {e}. Using default config model.'- )- custom_config = cls.model_validate(merged)- else:- # otherwise, try to look up the agent class by name (i.e. if it's a built-in)- # if that fails, just use the default AgentConfig class.- try:- agent_cls = Agent.get_cls(name)- custom_config = agent_cls.config_model.model_validate(merged)- except Exception:- # otherwise, just fall back to the default config model- custom_config = cls.model_validate(merged)- agent_mapping[name] = custom_config- except ValidationError as e:- logger.warning(- f'Invalid agent configuration for [{name}]: {e}. This section will be skipped.'- )- # Skip this custom section but continue with others- continue-return agent_mappingdiff --git a/openhands/core/config/utils.py b/openhands/core/config/utils.pyindex 3072b1ac6c..73ed9c6570 100644--- a/openhands/core/config/utils.py+++ b/openhands/core/config/utils.py@@ -35,7 +35,6 @@ from openhands.core.config.sandbox_config import SandboxConfigfrom openhands.core.config.security_config import SecurityConfigfrom openhands.storage import get_file_storefrom openhands.storage.files import FileStore-from openhands.utils.import_utils import get_implJWT_SECRET = '.jwt_secret'load_dotenv()@@ -683,29 +682,6 @@ def parse_arguments() -> argparse.Namespace:return args-def register_custom_agents(config: OpenHandsConfig) -> None:- """Register custom agents from configuration.-- This function is called after configuration is loaded to ensure all custom agents- specified in the config are properly imported and registered.- """- # Import here to avoid circular dependency- from openhands.controller.agent import Agent-- for agent_name, agent_config in config.agents.items():- if agent_config.classpath:- try:- agent_cls = get_impl(Agent, agent_config.classpath)- Agent.register(agent_name, agent_cls)- logger.openhands_logger.info(- f"Registered custom agent '{agent_name}' from {agent_config.classpath}"- )- except Exception as e:- logger.openhands_logger.error(- f"Failed to register agent '{agent_name}': {e}"- )--def load_openhands_config(set_logging_levels: bool = True, config_file: str = 'config.toml') -> OpenHandsConfig:@@ -719,7 +695,6 @@ def load_openhands_config(load_from_toml(config, config_file)load_from_env(config, os.environ)finalize_config(config)- register_custom_agents(config)if set_logging_levels:logger.DEBUG = config.debuglogger.DISABLE_COLOR_PRINTING = config.disable_colordiff --git a/openhands/runtime/impl/local/local_runtime.py b/openhands/runtime/impl/local/local_runtime.pyindex 0a11763433..b73ca4cd7a 100644--- a/openhands/runtime/impl/local/local_runtime.py+++ b/openhands/runtime/impl/local/local_runtime.py@@ -431,7 +431,7 @@ class LocalRuntime(ActionExecutionClient):initial_num_warm_servers = int(os.getenv('INITIAL_NUM_WARM_SERVERS', '0'))# Initialize warm servers if neededif initial_num_warm_servers > 0 and len(_WARM_SERVERS) == 0:- plugins = _get_plugins(config)+ plugins: list[PluginRequirement] = []# Copy the logic from Runtime where we add a VSCodePlugin on init if missingif not headless_mode and not DISABLE_VSCODE_PLUGIN:@@ -841,10 +841,3 @@ def _create_warm_server_in_background(target=_create_warm_server, daemon=True, args=(config, plugins))thread.start()---def _get_plugins(config: OpenHandsConfig) -> list[PluginRequirement]:- from openhands.controller.agent import Agent-- plugins = Agent.get_cls(config.default_agent).sandbox_plugins- return pluginsdiff --git a/openhands/utils/prompt.py b/openhands/utils/prompt.pydeleted file mode 100644index f74d94255c..0000000000--- a/openhands/utils/prompt.py+++ /dev/null@@ -1,149 +0,0 @@-import os-from dataclasses import dataclass, field-from itertools import islice--from jinja2 import Environment, FileSystemLoader, Template--from openhands.controller.state.state import State-from openhands.core.message import Message, TextContent-from openhands.events.observation.agent import MicroagentKnowledge---@dataclass-class RuntimeInfo:- date: str- available_hosts: dict[str, int] = field(default_factory=dict)- additional_agent_instructions: str = ''- custom_secrets_descriptions: dict[str, str] = field(default_factory=dict)- working_dir: str = ''---@dataclass-class RepositoryInfo:- """Information about a GitHub repository that has been cloned."""-- repo_name: str | None = None- repo_directory: str | None = None- branch_name: str | None = None---@dataclass-class ConversationInstructions:- """Optional instructions the agent must follow throughout the conversation while addressing the user's initial task-- Examples include-- 1. Resolver instructions: you're responding to GitHub issue #1234, make sure to open a PR when you are done- 2. Slack instructions: make sure to check whether any of the context attached is relevant to the task <context_messages>- """-- content: str = ''---class PromptManager:- """Manages prompt templates and includes information from the user's workspace micro-agents and global micro-agents.-- This class is dedicated to loading and rendering prompts (system prompt, user prompt).-- Attributes:- prompt_dir: Directory containing prompt templates.- """-- def __init__(- self,- prompt_dir: str,- system_prompt_filename: str = 'system_prompt.j2',- ):- if prompt_dir is None:- raise ValueError('Prompt directory is not set')-- self.prompt_dir: str = prompt_dir- self.env = Environment(loader=FileSystemLoader(prompt_dir))- self.system_template: Template = self._load_template(system_prompt_filename)- self.user_template: Template = self._load_template('user_prompt.j2')- self.additional_info_template: Template = self._load_template(- 'additional_info.j2'- )- self.microagent_info_template: Template = self._load_template(- 'microagent_info.j2'- )-- def _load_template(self, template_name: str) -> Template:- """Load a template from the prompt directory.-- Args:- template_name: Full filename of the template to load, including the .j2 extension.-- Returns:- The loaded Jinja2 template.-- Raises:- FileNotFoundError: If the template file is not found.- """- try:- return self.env.get_template(template_name)- except Exception:- template_path = os.path.join(self.prompt_dir, template_name)- raise FileNotFoundError(f'Prompt file {template_path} not found')-- def get_system_message(self, **context) -> str:- system_message = self.system_template.render(**context).strip()- return system_message-- def get_example_user_message(self) -> str:- """This is an initial user message that can be provided to the agent- before *actual* user instructions are provided.-- It can be used to provide a demonstration of how the agent- should behave in order to solve the user's task. And it may- optionally contain some additional context about the user's task.- These additional context will convert the current generic agent- into a more specialized agent that is tailored to the user's task.- """- return self.user_template.render().strip()-- def build_workspace_context(- self,- repository_info: RepositoryInfo | None,- runtime_info: RuntimeInfo | None,- conversation_instructions: ConversationInstructions | None,- repo_instructions: str = '',- ) -> str:- """Renders the additional info template with the stored repository/runtime info."""- return self.additional_info_template.render(- repository_info=repository_info,- repository_instructions=repo_instructions,- runtime_info=runtime_info,- conversation_instructions=conversation_instructions,- ).strip()-- def build_microagent_info(- self,- triggered_agents: list[MicroagentKnowledge],- ) -> str:- """Renders the microagent info template with the triggered agents.-- Args:- triggered_agents: A list of MicroagentKnowledge objects containing information- about triggered microagents.- """- return self.microagent_info_template.render(- triggered_agents=triggered_agents- ).strip()-- def add_turns_left_reminder(self, messages: list[Message], state: State) -> None:- latest_user_message = next(- islice(- (- m- for m in reversed(messages)- if m.role == 'user'- and any(isinstance(c, TextContent) for c in m.content)- ),- 1,- ),- None,- )- if latest_user_message:- reminder_text = f'\n\nENVIRONMENT REMINDER: You have {state.iteration_flag.max_value - state.iteration_flag.current_value} turns left to complete the task. When finished reply with <finish></finish>.'- latest_user_message.content.append(TextContent(text=reminder_text))diff --git a/tests/unit/controller/state/test_control_flags.py b/tests/unit/controller/state/test_control_flags.pydeleted file mode 100644index f5edaec758..0000000000--- a/tests/unit/controller/state/test_control_flags.py+++ /dev/null@@ -1,139 +0,0 @@-import pytest--from openhands.controller.state.control_flags import (- BudgetControlFlag,- IterationControlFlag,-)---def test_iteration_control_flag_reaches_limit_and_increases():- flag = IterationControlFlag(limit_increase_amount=5, current_value=5, max_value=5)-- # Should be at limit- assert flag.reached_limit() is True- assert flag._hit_limit is True-- # Increase limit in non-headless mode- flag.increase_limit(headless_mode=False)- assert flag.max_value == 10 # increased by limit_increase_amount-- # After increase, we should no longer be at limit- flag._hit_limit = False # simulate reset- assert flag.reached_limit() is False---def test_iteration_control_flag_does_not_increase_in_headless():- flag = IterationControlFlag(limit_increase_amount=5, current_value=5, max_value=5)-- assert flag.reached_limit() is True- assert flag._hit_limit is True-- # Should NOT increase max_value in headless mode- flag.increase_limit(headless_mode=True)- assert flag.max_value == 5---def test_iteration_control_flag_step_behavior():- flag = IterationControlFlag(limit_increase_amount=2, current_value=0, max_value=2)-- # First step- flag.step()- assert flag.current_value == 1- assert not flag.reached_limit()-- # Second step- flag.step()- assert flag.current_value == 2- assert flag.reached_limit()-- # Stepping again should raise error- with pytest.raises(RuntimeError, match='Agent reached maximum iteration'):- flag.step()---# ----- BudgetControlFlag Tests --------def test_budget_control_flag_reaches_limit_and_increases():- flag = BudgetControlFlag(- limit_increase_amount=10.0, current_value=50.0, max_value=50.0- )-- # Should be at limit- assert flag.reached_limit() is True- assert flag._hit_limit is True-- # Increase budget β allowed only if _hit_limit == True- flag.increase_limit(headless_mode=False)- assert flag.max_value == 60.0 # current_value + limit_increase_amount-- # After increasing, _hit_limit should be reset manually in your logic- flag._hit_limit = False- flag.current_value = 55.0- assert flag.reached_limit() is False---def test_budget_control_flag_does_not_increase_if_not_hit_limit():- flag = BudgetControlFlag(- limit_increase_amount=10.0, current_value=40.0, max_value=50.0- )-- # Not at limit yet- assert flag.reached_limit() is False- assert flag._hit_limit is False-- # Try to increase β should do nothing- old_max_value = flag.max_value- flag.increase_limit(headless_mode=False)- assert flag.max_value == old_max_value---def test_budget_control_flag_does_not_increase_in_headless():- flag = BudgetControlFlag(- limit_increase_amount=10.0, current_value=50.0, max_value=50.0- )-- assert flag.reached_limit() is True- assert flag._hit_limit is True-- # Increase limit in headless mode β should still increase since BudgetControlFlag ignores headless param- flag.increase_limit(headless_mode=True)- assert flag.max_value == 60.0---def test_budget_control_flag_step_raises_on_limit():- flag = BudgetControlFlag(- limit_increase_amount=5.0, current_value=55.0, max_value=50.0- )-- # Should raise RuntimeError- with pytest.raises(RuntimeError, match='Agent reached maximum budget'):- flag.step()-- # After increasing limit, step should not raise- flag.max_value = 60.0- flag._hit_limit = False- flag.step() # Should not raise---def test_budget_control_flag_hit_limit_resets_after_increase():- flag = BudgetControlFlag(- limit_increase_amount=10.0, current_value=50.0, max_value=50.0- )-- # Initially should hit limit- assert flag.reached_limit() is True- assert flag._hit_limit is True-- # Increase limit- flag.increase_limit(headless_mode=False)-- # After increasing, _hit_limit should be reset- assert flag._hit_limit is False-- # Should no longer report reaching limit unless value exceeds new max- assert flag.reached_limit() is False-- # If we push current_value over new max_value:- flag.current_value = flag.max_value + 1.0- assert flag.reached_limit() is Truediff --git a/tests/unit/controller/state/test_state.py b/tests/unit/controller/state/test_state.pydeleted file mode 100644index 29c4315231..0000000000--- a/tests/unit/controller/state/test_state.py+++ /dev/null@@ -1,125 +0,0 @@-from unittest.mock import patch--from openhands.controller.state.state import State, TrafficControlState-from openhands.core.schema import AgentState-from openhands.events.event import Event-from openhands.llm.metrics import Metrics-from openhands.storage.memory import InMemoryFileStore---def example_event(index: int) -> Event:- event = Event()- event._message = f'Test message {index}'- event._id = index- return event---def test_state_view_caching_avoids_unnecessary_rebuilding():- """Test that the state view caching avoids unnecessarily rebuilding the view when the history hasn't changed."""- state = State()- state.history = [example_event(i) for i in range(5)]-- # Build the view once.- view = state.view-- # Easy way to check that the cache works -- `view` and future calls of- # `state.view` should be the same object. We'll check that by using the `id`- # of the view.- assert id(view) == id(state.view)-- # Add an event to the history. This should produce a different view.- state.history.append(example_event(100))-- new_view = state.view- assert id(new_view) != id(view)-- # But once we have the new view once, it should be cached.- assert id(new_view) == id(state.view)---def test_state_view_cache_not_serialized():- """Test that the fields used to cache view construction are not serialized when state is saved."""- state = State()- state.history = [example_event(i) for i in range(5)]-- # Build the view once.- view = state.view-- # Serialize the state.- store = InMemoryFileStore()- state.save_to_session('test_sid', store, None)- restored_state = State.restore_from_session('test_sid', store, None)-- # The state usually has the history rebuilt from the event stream -- we'll- # simulate this by manually setting the state history to the same events.- restored_state.history = state.history-- restored_view = restored_state.view-- # Since serialization doesn't include the view cache, the restored view will- # be structurally identical but _not_ the same object.- assert id(restored_view) != id(view)- assert restored_view.events == view.events---def test_restore_older_state_version():- """Test that we can restore from an older state version (before control flags)."""- # Create a dictionary that mimics the old state format (before control flags)- state = State(- session_id='test_old_session',- iteration=42,- local_iteration=42,- max_iterations=100,- agent_state=AgentState.RUNNING,- traffic_control_state=TrafficControlState.NORMAL,- metrics=Metrics(),- confirmation_mode=False,- )-- def no_op_getstate(self):- return self.__dict__-- store = InMemoryFileStore()-- with patch.object(State, '__getstate__', no_op_getstate):- state.save_to_session('test_old_session', store, None)-- # Now restore it- restored_state = State.restore_from_session('test_old_session', store, None)-- # Verify that when we store the active fields are populated with the values from the deprecated fields- assert restored_state.session_id == 'test_old_session'- assert restored_state.agent_state == AgentState.LOADING- assert restored_state.resume_state == AgentState.RUNNING- assert restored_state.iteration_flag.current_value == 42- assert restored_state.iteration_flag.max_value == 100---def test_save_without_deprecated_fields():- """Test that we can save state without deprecated fields"""- # Create a dictionary that mimics the old state format (before control flags)- state = State(- session_id='test_old_session',- iteration=42,- local_iteration=42,- max_iterations=100,- agent_state=AgentState.RUNNING,- traffic_control_state=TrafficControlState.NORMAL,- metrics=Metrics(),- confirmation_mode=False,- )-- store = InMemoryFileStore()-- state.save_to_session('test_state', store, None)- restored_state = State.restore_from_session('test_state', store, None)-- # Verify that when we save and restore, the deprecated fields are removed- # but the new fields maintain the correct values- assert restored_state.session_id == 'test_old_session'- assert restored_state.agent_state == AgentState.LOADING- assert restored_state.resume_state == AgentState.RUNNING- assert (- restored_state.iteration_flag.current_value == 0- ) # The depreciated attrib was not stored, so it did not override existing values on restore- assert restored_state.iteration_flag.max_value == 100diff --git a/tests/unit/controller/test_agent_controller_loop_recovery.py b/tests/unit/controller/test_agent_controller_loop_recovery.pydeleted file mode 100644index 562825cc6b..0000000000--- a/tests/unit/controller/test_agent_controller_loop_recovery.py+++ /dev/null@@ -1,410 +0,0 @@-"""Tests for agent controller loop recovery functionality."""--from unittest.mock import AsyncMock, MagicMock--import pytest--from openhands.controller.agent_controller import AgentController-from openhands.controller.stuck import StuckDetector-from openhands.core.schema import AgentState-from openhands.events import EventStream-from openhands.events.action import LoopRecoveryAction, MessageAction-from openhands.events.observation import LoopDetectionObservation-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.memory import InMemoryFileStore---class TestAgentControllerLoopRecovery:- """Tests for agent controller loop recovery functionality."""-- @pytest.fixture- def mock_controller(self):- """Create a mock agent controller for testing."""- # Create mock dependencies- mock_event_stream = MagicMock(- spec=EventStream,- event_stream=EventStream(- sid='test-session-id', file_store=InMemoryFileStore({})- ),- )- mock_event_stream.sid = 'test-session-id'- mock_event_stream.get_latest_event_id.return_value = 0-- mock_conversation_stats = MagicMock(spec=ConversationStats)-- mock_agent = MagicMock()- mock_agent.act = AsyncMock()-- # Create controller with correct parameters- controller = AgentController(- agent=mock_agent,- event_stream=mock_event_stream,- conversation_stats=mock_conversation_stats,- iteration_delta=100,- headless_mode=True,- )-- # Mock state properties- controller.state.history = []- controller.state.agent_state = AgentState.RUNNING- controller.state.iteration_flag = MagicMock()- controller.state.iteration_flag.current_value = 10-- # Mock stuck detector- controller._stuck_detector = MagicMock(spec=StuckDetector)- controller._stuck_detector.stuck_analysis = None- controller._stuck_detector.is_stuck = MagicMock(return_value=False)-- return controller-- @pytest.mark.asyncio- async def test_controller_detects_loop_and_produces_observation(- self, mock_controller- ):- """Test that controller detects loops and produces LoopDetectionObservation."""- # Setup stuck detector to detect a loop- mock_controller._stuck_detector.is_stuck.return_value = True- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_type = (- 'repeating_action_observation'- )- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 5-- # Call attempt_loop_recovery- result = mock_controller.attempt_loop_recovery()-- # Verify that loop recovery was attempted- assert result is True-- # Verify that LoopDetectionObservation was added to event stream- mock_controller.event_stream.add_event.assert_called()-- # Check that LoopDetectionObservation was created- calls = mock_controller.event_stream.add_event.call_args_list- loop_detection_found = False- pause_action_found = False-- for call in calls:- args, _ = call- # add_event only takes one argument (the event)- event = args[0]-- if isinstance(event, LoopDetectionObservation):- loop_detection_found = True- assert 'Agent detected in a loop!' in event.content- assert 'repeating_action_observation' in event.content- assert 'Loop detected at iteration 10' in event.content- elif (- hasattr(event, 'agent_state') and event.agent_state == AgentState.PAUSED- ):- pause_action_found = True-- assert loop_detection_found, 'LoopDetectionObservation should be created'- assert pause_action_found, 'Agent should be paused'-- @pytest.mark.asyncio- async def test_controller_handles_loop_recovery_action_option_1(- self, mock_controller- ):- """Test that controller handles LoopRecoveryAction with option 1."""- # Setup stuck analysis- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 5-- # Mock the _perform_loop_recovery method for this test- mock_controller._perform_loop_recovery = AsyncMock()-- # Create LoopRecoveryAction with option 1- action = LoopRecoveryAction(option=1)-- # Call _handle_loop_recovery_action- await mock_controller._handle_loop_recovery_action(action)-- # Verify that _perform_loop_recovery was called- mock_controller._perform_loop_recovery.assert_called_once_with(- mock_controller._stuck_detector.stuck_analysis- )-- @pytest.mark.asyncio- async def test_controller_handles_loop_recovery_action_option_2(- self, mock_controller- ):- """Test that controller handles LoopRecoveryAction with option 2."""- # Setup stuck analysis- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 5-- # Mock the _restart_with_last_user_message method for this test- mock_controller._restart_with_last_user_message = AsyncMock()-- # Create LoopRecoveryAction with option 2- action = LoopRecoveryAction(option=2)-- # Call _handle_loop_recovery_action- await mock_controller._handle_loop_recovery_action(action)-- # Verify that _restart_with_last_user_message was called- mock_controller._restart_with_last_user_message.assert_called_once_with(- mock_controller._stuck_detector.stuck_analysis- )-- @pytest.mark.asyncio- async def test_controller_handles_loop_recovery_action_option_3(- self, mock_controller- ):- """Test that controller handles LoopRecoveryAction with option 3 (stop)."""- # Setup stuck analysis- mock_controller._stuck_detector.stuck_analysis = MagicMock()-- # Mock the set_agent_state_to method for this test- mock_controller.set_agent_state_to = AsyncMock()-- # Create LoopRecoveryAction with option 3- action = LoopRecoveryAction(option=3)-- # Call _handle_loop_recovery_action- await mock_controller._handle_loop_recovery_action(action)-- # Verify that set_agent_state_to was called with STOPPED- mock_controller.set_agent_state_to.assert_called_once_with(AgentState.STOPPED)-- @pytest.mark.asyncio- async def test_controller_ignores_loop_recovery_without_stuck_analysis(- self, mock_controller- ):- """Test that controller ignores LoopRecoveryAction when no stuck analysis exists."""- # Ensure no stuck analysis- mock_controller._stuck_detector.stuck_analysis = None-- # Mock all recovery methods for this test- mock_controller._perform_loop_recovery = AsyncMock()- mock_controller._restart_with_last_user_message = AsyncMock()- mock_controller.set_agent_state_to = AsyncMock()-- # Create LoopRecoveryAction- action = LoopRecoveryAction(option=1)-- # Call _handle_loop_recovery_action- await mock_controller._handle_loop_recovery_action(action)-- # Verify that no recovery methods were called- mock_controller._perform_loop_recovery.assert_not_called()- mock_controller._restart_with_last_user_message.assert_not_called()- mock_controller.set_agent_state_to.assert_not_called()-- @pytest.mark.asyncio- async def test_controller_no_loop_recovery_when_not_stuck(self, mock_controller):- """Test that controller doesn't attempt recovery when not stuck."""- # Setup no stuck analysis- mock_controller._stuck_detector.stuck_analysis = None-- # Reset the mock to ignore any previous calls (like system message)- mock_controller.event_stream.add_event.reset_mock()-- # Call attempt_loop_recovery- result = mock_controller.attempt_loop_recovery()-- # Verify that no recovery was attempted- assert result is False-- # Verify that no loop recovery events were added to the stream- # (Note: there might be other events, but no loop recovery specific ones)- calls = mock_controller.event_stream.add_event.call_args_list- loop_recovery_events = [- call- for call in calls- if len(call[0]) > 0- and (- isinstance(call[0][0], LoopDetectionObservation)- or (- hasattr(call[0][0], 'agent_state')- and call[0][0].agent_state == AgentState.PAUSED- )- )- ]- assert len(loop_recovery_events) == 0, (- 'No loop recovery events should be added when not stuck'- )-- @pytest.mark.asyncio- async def test_controller_state_transition_after_loop_recovery(- self, mock_controller- ):- """Test that controller state transitions correctly after loop recovery."""- # Setup initial state- mock_controller.state.agent_state = AgentState.RUNNING-- # Setup stuck detector to detect a loop- mock_controller._stuck_detector.is_stuck.return_value = True- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_type = 'monologue'- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 3-- # Call attempt_loop_recovery- result = mock_controller.attempt_loop_recovery()-- # Verify that recovery was attempted- assert result is True-- # Verify that agent was paused- calls = mock_controller.event_stream.add_event.call_args_list- pause_found = False- for call in calls:- args, _ = call- # add_event only takes one argument (the event)- event = args[0]- if hasattr(event, 'agent_state') and event.agent_state == AgentState.PAUSED:- pause_found = True- break-- assert pause_found, 'Agent should be paused after loop detection'-- @pytest.mark.asyncio- async def test_controller_resumes_after_loop_recovery(self, mock_controller):- """Test that controller can resume normal operation after loop recovery."""- # Setup stuck analysis- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 5-- # Mock the _perform_loop_recovery method for this test- mock_controller._perform_loop_recovery = AsyncMock()-- # Create LoopRecoveryAction with option 1- action = LoopRecoveryAction(option=1)-- # Call _handle_loop_recovery_action- await mock_controller._handle_loop_recovery_action(action)-- # Verify that recovery was performed- mock_controller._perform_loop_recovery.assert_called_once()-- # Verify that agent can continue normal operation- # (This would be tested in integration tests with actual agent execution)-- @pytest.mark.asyncio- async def test_controller_truncates_history_during_loop_recovery(- self, mock_controller- ):- """Test that controller correctly truncates history during loop recovery."""- # Setup mock history with events- from openhands.events.action import CmdRunAction- from openhands.events.observation import CmdOutputObservation, NullObservation-- # Create a realistic history with 10 events- mock_history = []-- # Add initial user message- user_msg = MessageAction(- content='Hello, help me with this task', wait_for_response=False- )- user_msg._source = 'user'- user_msg._id = 1- mock_history.append(user_msg)-- # Add agent response- agent_obs = NullObservation(content='')- agent_obs._id = 2- mock_history.append(agent_obs)-- # Add some commands and observations (simulating a loop)- for i in range(3, 11):- if i % 2 == 1: # Action- cmd = CmdRunAction(command='ls -la')- cmd._id = i- mock_history.append(cmd)- else: # Observation- obs = CmdOutputObservation(- content='file1.txt file2.txt', command='ls -la'- )- obs._id = i- obs._cause = i - 1- mock_history.append(obs)-- # Set the mock history- mock_controller.state.history = mock_history- mock_controller.state.end_id = 10-- # Setup stuck analysis to indicate loop starts at index 5- mock_controller._stuck_detector.stuck_analysis = MagicMock()- mock_controller._stuck_detector.stuck_analysis.loop_start_idx = 5-- # Create LoopRecoveryAction with option 1 (truncate memory)- LoopRecoveryAction(option=1)-- # Test actual truncation by calling the _perform_loop_recovery method directly- # Reset history for actual truncation test- mock_controller.state.history = mock_history.copy()- mock_controller.state.end_id = 10-- # Call the actual _perform_loop_recovery method directly- print(- f'Before truncation: {len(mock_controller.state.history)} events, recovery_point={mock_controller._stuck_detector.stuck_analysis.loop_start_idx}'- )- print(- f'_perform_loop_recovery method: {mock_controller._perform_loop_recovery}'- )- print(- f'_truncate_memory_to_point method: {mock_controller._truncate_memory_to_point}'- )- await mock_controller._perform_loop_recovery(- mock_controller._stuck_detector.stuck_analysis- )-- # Debug: print the actual history after truncation- print(f'History after truncation: {len(mock_controller.state.history)} events')- for i, event in enumerate(mock_controller.state.history):- print(f' Event {i}: id={event.id}, type={type(event).__name__}')-- # Verify that history was truncated to the recovery point- # The recovery point is index 5, so we should keep events 0-4 (5 events)- assert len(mock_controller.state.history) == 5, (- f'Expected 5 events after truncation, got {len(mock_controller.state.history)}'- )-- # Verify the specific events that remain- expected_ids = [1, 2, 3, 4, 5]- for i, event in enumerate(mock_controller.state.history):- assert event.id == expected_ids[i], (- f'Event at index {i} should have id {expected_ids[i]}, got {event.id}'- )-- # Verify end_id was updated- assert mock_controller.state.end_id == 5, (- f'Expected end_id to be 5, got {mock_controller.state.end_id}'- )-- def test_stuck_detection_config_option_exists(self):- """Test that the enable_stuck_detection config option exists and defaults to True."""- from openhands.core.config.agent_config import AgentConfig-- # Create a default config- config = AgentConfig()-- # Verify the attribute exists and defaults to True- assert hasattr(config, 'enable_stuck_detection')- assert config.enable_stuck_detection is True-- # Verify we can create a config with it disabled- config_disabled = AgentConfig(enable_stuck_detection=False)- assert config_disabled.enable_stuck_detection is False-- def test_stuck_detection_config_from_env(self):- """Test that enable_stuck_detection can be set via environment variable."""- import os-- from openhands.core.config.agent_config import AgentConfig-- # Test with enabled (default)- os.environ.pop('AGENT_ENABLE_STUCK_DETECTION', None)- config = AgentConfig()- assert config.enable_stuck_detection is True-- # Test with explicitly disabled- os.environ['AGENT_ENABLE_STUCK_DETECTION'] = 'false'- # Need to reload for env var to take effect in real usage- # For this test, we just verify the config accepts the parameter- config_disabled = AgentConfig(enable_stuck_detection=False)- assert config_disabled.enable_stuck_detection is False-- # Cleanup- os.environ.pop('AGENT_ENABLE_STUCK_DETECTION', None)diff --git a/tests/unit/controller/test_agent_delegation.py b/tests/unit/controller/test_agent_delegation.pydeleted file mode 100644index 013b298611..0000000000--- a/tests/unit/controller/test_agent_delegation.py+++ /dev/null@@ -1,190 +0,0 @@-import asyncio-from concurrent.futures import ThreadPoolExecutor-from unittest.mock import AsyncMock, MagicMock, Mock-from uuid import uuid4--import pytest--from openhands.controller.agent import Agent-from openhands.controller.agent_controller import AgentController-from openhands.controller.state.state import State-from openhands.core.config import OpenHandsConfig-from openhands.core.config.agent_config import AgentConfig-from openhands.core.config.llm_config import LLMConfig-from openhands.core.schema import AgentState-from openhands.events import EventSource, EventStream-from openhands.events.action import (- MessageAction,-)-from openhands.events.action.message import SystemMessageAction-from openhands.llm.llm import LLM-from openhands.llm.llm_registry import LLMRegistry-from openhands.llm.metrics import Metrics-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.memory import InMemoryFileStore---def llm_registry():- config = OpenHandsConfig()- return LLMRegistry(config=config)---def conversation_stats():- import uuid-- file_store = InMemoryFileStore({})- # Use a unique conversation ID for each test to avoid conflicts- conversation_id = f'test-conversation-{uuid.uuid4()}'- return ConversationStats(- file_store=file_store, conversation_id=conversation_id, user_id='test-user'- )---def connected_registry_and_stats(llm_registry, conversation_stats):- """Connect the LLMRegistry and ConversationStats properly"""- # Subscribe to LLM registry events to track metrics- llm_registry.subscribe(conversation_stats.register_llm)- return llm_registry, conversation_stats---def mock_event_stream():- """Creates an event stream in memory."""- sid = f'test-{uuid4()}'- file_store = InMemoryFileStore({})- return EventStream(sid=sid, file_store=file_store)---def mock_parent_agent(llm_registry):- """Creates a mock parent agent for testing delegation."""- agent = MagicMock(spec=Agent)- agent.name = 'ParentAgent'- agent.llm = MagicMock(spec=LLM)- agent.llm.service_id = 'main_agent'- agent.llm.metrics = Metrics()- agent.llm.config = LLMConfig()- agent.llm.retry_listener = None # Add retry_listener attribute- agent.config = AgentConfig()- agent.llm_registry = llm_registry # Add the missing llm_registry attribute-- # Add a proper system message mock- system_message = SystemMessageAction(content='Test system message')- system_message._source = EventSource.AGENT- system_message._id = -1 # Set invalid ID to avoid the ID check- agent.get_system_message.return_value = system_message- return agent---def mock_child_agent(llm_registry):- """Creates a mock child agent for testing delegation."""- agent = MagicMock(spec=Agent)- agent.name = 'ChildAgent'- agent.llm = MagicMock(spec=LLM)- agent.llm.service_id = 'main_agent'- agent.llm.metrics = Metrics()- agent.llm.config = LLMConfig()- agent.llm.retry_listener = None # Add retry_listener attribute- agent.config = AgentConfig()- agent.llm_registry = llm_registry # Add the missing llm_registry attribute-- system_message = SystemMessageAction(content='Test system message')- system_message._source = EventSource.AGENT- system_message._id = -1 # Set invalid ID to avoid the ID check- agent.get_system_message.return_value = system_message- return agent---def create_mock_agent_factory(mock_child_agent, llm_registry):- """Helper function to create a mock agent factory with proper LLM registration."""-- def create_mock_agent(config, llm_registry=None):- # Register the mock agent's LLM in the registry so get_combined_metrics() can find it- if llm_registry:- mock_child_agent.llm = llm_registry.get_llm('agent_llm', LLMConfig())- mock_child_agent.llm_registry = (- llm_registry # Set the llm_registry attribute- )- return mock_child_agent-- return create_mock_agent--- 'delegate_state',- [- AgentState.RUNNING,- AgentState.FINISHED,- AgentState.ERROR,- AgentState.REJECTED,- ],-)-async def test_delegate_step_different_states(- mock_parent_agent, mock_event_stream, delegate_state, connected_registry_and_stats-):- """Ensure that delegate is closed or remains open based on the delegate's state."""- llm_registry, conversation_stats = connected_registry_and_stats-- # Create a state with iteration_flag.max_value set to 10- state = State(inputs={})- state.iteration_flag.max_value = 10- controller = AgentController(- agent=mock_parent_agent,- event_stream=mock_event_stream,- conversation_stats=conversation_stats,- iteration_delta=1, # Add the required iteration_delta parameter- sid='test',- confirmation_mode=False,- headless_mode=True,- initial_state=state,- )-- mock_delegate = AsyncMock()- controller.delegate = mock_delegate-- mock_delegate.state.iteration_flag = MagicMock()- mock_delegate.state.iteration_flag.current_value = 5- mock_delegate.state.outputs = {'result': 'test'}- mock_delegate.agent.name = 'TestDelegate'-- mock_delegate.get_agent_state = Mock(return_value=delegate_state)- mock_delegate._step = AsyncMock()- mock_delegate.close = AsyncMock()-- async def call_on_event_with_new_loop():- """In this thread, create and set a fresh event loop, so that the run_until_complete()- calls inside controller.on_event(...) find a valid loop.- """- loop_in_thread = asyncio.new_event_loop()- try:- asyncio.set_event_loop(loop_in_thread)- msg_action = MessageAction(content='Test message')- msg_action._source = EventSource.USER- controller.on_event(msg_action)- finally:- loop_in_thread.close()-- loop = asyncio.get_running_loop()- with ThreadPoolExecutor() as executor:- future = loop.run_in_executor(executor, call_on_event_with_new_loop)- await future-- # Give time for the event loop to process events- await asyncio.sleep(0.5)-- if delegate_state == AgentState.RUNNING:- assert controller.delegate is not None- assert controller.state.iteration_flag.current_value == 0- mock_delegate.close.assert_not_called()- else:- assert controller.delegate is None- assert controller.state.iteration_flag.current_value == 5- # The close method is called once in end_delegate- assert mock_delegate.close.call_count == 1-- await controller.close()diff --git a/tests/unit/controller/test_is_stuck.py b/tests/unit/controller/test_is_stuck.pydeleted file mode 100644index 838ba28651..0000000000--- a/tests/unit/controller/test_is_stuck.py+++ /dev/null@@ -1,1029 +0,0 @@-import logging-from unittest.mock import Mock, patch--import pytest-from pytest import TempPathFactory--from openhands.controller.agent_controller import AgentController-from openhands.controller.state.state import State-from openhands.controller.stuck import StuckDetector-from openhands.events.action import (- CmdRunAction,- FileReadAction,- MessageAction,-)-from openhands.events.action.commands import IPythonRunCellAction-from openhands.events.event import Event-from openhands.events.observation import (- CmdOutputObservation,- FileReadObservation,-)-from openhands.events.observation.agent import AgentCondensationObservation-from openhands.events.observation.commands import IPythonRunCellObservation-from openhands.events.observation.empty import NullObservation-from openhands.events.observation.error import ErrorObservation-from openhands.events.stream import EventSource, EventStream-from openhands.storage import get_file_store--# Reusable action/observation mocks for stuck-pattern tests-cmd_ls_action = CmdRunAction(command='ls')-cmd_ls_observation = CmdOutputObservation(command='ls', content='file1.txt')--read_file1_action = FileReadAction(path='file1.txt')-read_file1_observation = FileReadObservation(content='File content', path='file1.txt')--pwd_action = CmdRunAction(command='pwd')-read_file2_action = FileReadAction(path='file2.txt')--cmd_ls_different_observation = CmdOutputObservation(- command='ls_DIFFERENT', content='file1.txt'-)-read_file2_observation = FileReadObservation(content='File content', path='file2.txt')---class MockOtherEvent(Event):- """Event type used to verify non-action/observation events are ignored."""---other_event = MockOtherEvent()---def collect_events(stream):- return [event for event in stream.get_events()]---logging.basicConfig(level=logging.DEBUG)--jupyter_line_1 = '\n[Jupyter current working directory:'-jupyter_line_2 = '\n[Jupyter Python interpreter:'-code_snippet = """-edit_file_by_replace(- 'book_store.py',- to_replace=\"""def total(basket):- if not basket:- return 0-"""---def temp_dir(tmp_path_factory: TempPathFactory) -> str:- return str(tmp_path_factory.mktemp('test_is_stuck'))---def event_stream(temp_dir):- file_store = get_file_store('local', temp_dir)- event_stream = EventStream('asdf', file_store)- yield event_stream- # clear after each test- event_stream.clear()---class TestStuckDetector:- @pytest.fixture- def stuck_detector(self):- state = State(inputs={})- # Set the iteration flag's max_value to 50 (equivalent to the old max_iterations)- state.iteration_flag.max_value = 50- state.history = [] # Initialize history as an empty list- return StuckDetector(state)-- def _impl_syntax_error_events(- self,- state: State,- error_message: str,- random_line: bool,- incidents: int = 4,- ):- for i in range(incidents):- ipython_action = IPythonRunCellAction(code=code_snippet)- state.history.append(ipython_action)- extra_number = (i + 1) * 10 if random_line else '42'- extra_line = '\n' * (i + 1) if random_line else ''- ipython_observation = IPythonRunCellObservation(- content=f' Cell In[1], line {extra_number}\n'- 'to_replace="""def largest(min_factor, max_factor):\n ^\n'- f'{error_message}{extra_line}' + jupyter_line_1 + jupyter_line_2,- code=code_snippet,- )- # ipython_observation._cause = ipython_action._id- state.history.append(ipython_observation)-- def _impl_unterminated_string_error_events(- self, state: State, random_line: bool, incidents: int = 4- ):- for i in range(incidents):- ipython_action = IPythonRunCellAction(code=code_snippet)- state.history.append(ipython_action)- line_number = (i + 1) * 10 if random_line else '1'- ipython_observation = IPythonRunCellObservation(- content=f'print(" Cell In[1], line {line_number}\nhello\n ^\nSyntaxError: unterminated string literal (detected at line {line_number})'- + jupyter_line_1- + jupyter_line_2,- code=code_snippet,- )- # ipython_observation._cause = ipython_action._- state.history.append(ipython_observation)-- def test_history_too_short(self, stuck_detector: StuckDetector):- state = stuck_detector.state- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- observation = NullObservation(content='')- # observation._cause = message_action.id- state.history.append(message_action)- state.history.append(observation)-- cmd_action = CmdRunAction(command='ls')- state.history.append(cmd_action)- cmd_observation = CmdOutputObservation(- command='ls', content='file1.txt\nfile2.txt'- )- # cmd_observation._cause = cmd_action._id- state.history.append(cmd_observation)-- assert stuck_detector.is_stuck(headless_mode=True) is False- assert stuck_detector.stuck_analysis is None-- def test_interactive_mode_resets_after_user_message(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state-- # First add some actions that would be stuck in non-UI mode- for i in range(4):- cmd_action = CmdRunAction(command='ls')- cmd_action._id = i- state.history.append(cmd_action)- cmd_observation = CmdOutputObservation(- content='', command='ls', command_id=i- )- cmd_observation._cause = cmd_action._id- state.history.append(cmd_observation)-- # In headless mode, this should be stuck- assert stuck_detector.is_stuck(headless_mode=True) is True-- # with the UI, it will ALSO be stuck initially- assert stuck_detector.is_stuck(headless_mode=False) is True-- # Add a user message- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)-- # In not-headless mode, this should not be stuck because we ignore history before user message- assert stuck_detector.is_stuck(headless_mode=False) is False-- # But in headless mode, this should be still stuck because user messages do not count- assert stuck_detector.is_stuck(headless_mode=True) is True-- # Add two more identical actions - still not stuck because we need at least 3- for i in range(2):- cmd_action = CmdRunAction(command='ls')- cmd_action._id = i + 4- state.history.append(cmd_action)- cmd_observation = CmdOutputObservation(- content='', command='ls', command_id=i + 4- )- cmd_observation._cause = cmd_action._id- state.history.append(cmd_observation)-- assert stuck_detector.is_stuck(headless_mode=False) is False-- # Add two more identical actions - now it should be stuck- for i in range(2):- cmd_action = CmdRunAction(command='ls')- cmd_action._id = i + 6- state.history.append(cmd_action)- cmd_observation = CmdOutputObservation(- content='', command='ls', command_id=i + 6- )- cmd_observation._cause = cmd_action._id- state.history.append(cmd_observation)-- assert stuck_detector.is_stuck(headless_mode=False) is True-- def test_is_stuck_repeating_action_observation(self, stuck_detector: StuckDetector):- state = stuck_detector.state- message_action = MessageAction(content='Done', wait_for_response=False)- message_action._source = EventSource.USER-- hello_action = MessageAction(content='Hello', wait_for_response=False)- hello_observation = NullObservation('')-- # 2 events- state.history.append(hello_action)- state.history.append(hello_observation)-- cmd_action_1 = CmdRunAction(command='ls')- cmd_action_1._id = 1- state.history.append(cmd_action_1)- cmd_observation_1 = CmdOutputObservation(content='', command='ls')- cmd_observation_1._cause = cmd_action_1._id- state.history.append(cmd_observation_1)- # 4 events-- cmd_action_2 = CmdRunAction(command='ls')- cmd_action_2._id = 2- state.history.append(cmd_action_2)- cmd_observation_2 = CmdOutputObservation(content='', command='ls')- cmd_observation_2._cause = cmd_action_2._id- state.history.append(cmd_observation_2)- # 6 events-- # random user message just because we can- message_null_observation = NullObservation(content='')- state.history.append(message_action)- state.history.append(message_null_observation)- # 8 events-- assert stuck_detector.is_stuck(headless_mode=True) is False-- cmd_action_3 = CmdRunAction(command='ls')- cmd_action_3._id = 3- state.history.append(cmd_action_3)- cmd_observation_3 = CmdOutputObservation(content='', command='ls')- cmd_observation_3._cause = cmd_action_3._id- state.history.append(cmd_observation_3)- # 10 events-- assert len(state.history) == 10- assert stuck_detector.is_stuck(headless_mode=True) is False-- cmd_action_4 = CmdRunAction(command='ls')- cmd_action_4._id = 4- state.history.append(cmd_action_4)- cmd_observation_4 = CmdOutputObservation(content='', command='ls')- cmd_observation_4._cause = cmd_action_4._id- state.history.append(cmd_observation_4)- # 12 events-- assert len(state.history) == 12-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with('Action, Observation loop detected')-- # recover to before first loop pattern- assert stuck_detector.stuck_analysis.loop_type == 'repeating_action_observation'- assert stuck_detector.stuck_analysis.loop_repeat_times == 4- assert stuck_detector.stuck_analysis.loop_start_idx == 1-- def test_is_stuck_repeating_action_error(self, stuck_detector: StuckDetector):- state = stuck_detector.state- # (action, error_observation), not necessarily the same error- message_action = MessageAction(content='Done', wait_for_response=False)- message_action._source = EventSource.USER-- hello_action = MessageAction(content='Hello', wait_for_response=False)- hello_observation = NullObservation(content='')- state.history.append(hello_action)- # hello_observation._cause = hello_action._id- state.history.append(hello_observation)- # 2 events-- cmd_action_1 = CmdRunAction(command='invalid_command')- state.history.append(cmd_action_1)- error_observation_1 = ErrorObservation(content='Command not found')- # error_observation_1._cause = cmd_action_1._id- state.history.append(error_observation_1)- # 4 events-- cmd_action_2 = CmdRunAction(command='invalid_command')- state.history.append(cmd_action_2)- error_observation_2 = ErrorObservation(- content='Command still not found or another error'- )- # error_observation_2._cause = cmd_action_2._id- state.history.append(error_observation_2)- # 6 events-- message_null_observation = NullObservation(content='')- state.history.append(message_action)- state.history.append(message_null_observation)- # 8 events-- cmd_action_3 = CmdRunAction(command='invalid_command')- state.history.append(cmd_action_3)- error_observation_3 = ErrorObservation(content='Different error')- # error_observation_3._cause = cmd_action_3._id- state.history.append(error_observation_3)- # 10 events-- cmd_action_4 = CmdRunAction(command='invalid_command')- state.history.append(cmd_action_4)- error_observation_4 = ErrorObservation(content='Command not found')- # error_observation_4._cause = cmd_action_4._id- state.history.append(error_observation_4)- # 12 events-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with(- 'Action, ErrorObservation loop detected'- )- assert stuck_detector.stuck_analysis.loop_type == 'repeating_action_error'- assert stuck_detector.stuck_analysis.loop_repeat_times == 3- assert stuck_detector.stuck_analysis.loop_start_idx == 1-- def test_is_stuck_invalid_syntax_error(self, stuck_detector: StuckDetector):- state = stuck_detector.state- self._impl_syntax_error_events(- state,- error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',- random_line=False,- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is True-- def test_is_not_stuck_invalid_syntax_error_random_lines(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- self._impl_syntax_error_events(- state,- error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',- random_line=True,- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_not_stuck_invalid_syntax_error_only_three_incidents(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- self._impl_syntax_error_events(- state,- error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',- random_line=True,- incidents=3,- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_stuck_incomplete_input_error(self, stuck_detector: StuckDetector):- state = stuck_detector.state- self._impl_syntax_error_events(- state,- error_message='SyntaxError: incomplete input',- random_line=False,- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is True-- def test_is_not_stuck_incomplete_input_error(self, stuck_detector: StuckDetector):- state = stuck_detector.state- self._impl_syntax_error_events(- state,- error_message='SyntaxError: incomplete input',- random_line=True,- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_not_stuck_ipython_unterminated_string_error_random_lines(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- self._impl_unterminated_string_error_events(state, random_line=True)-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_not_stuck_ipython_unterminated_string_error_only_two_incidents(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- self._impl_unterminated_string_error_events(- state, random_line=False, incidents=2- )-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_stuck_ipython_unterminated_string_error(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- self._impl_unterminated_string_error_events(state, random_line=False)-- with patch('logging.Logger.warning'):- assert stuck_detector.is_stuck(headless_mode=True) is True-- def test_is_not_stuck_ipython_syntax_error_not_at_end(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- # this test is to make sure we don't get false positives- # since the "at line x" is changing in between!- ipython_action_1 = IPythonRunCellAction(code='print("hello')- state.history.append(ipython_action_1)- ipython_observation_1 = IPythonRunCellObservation(- content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 1)\nThis is some additional output',- code='print("hello',- )- # ipython_observation_1._cause = ipython_action_1._id- state.history.append(ipython_observation_1)-- ipython_action_2 = IPythonRunCellAction(code='print("hello')- state.history.append(ipython_action_2)- ipython_observation_2 = IPythonRunCellObservation(- content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 1)\nToo much output here on and on',- code='print("hello',- )- # ipython_observation_2._cause = ipython_action_2._id- state.history.append(ipython_observation_2)-- ipython_action_3 = IPythonRunCellAction(code='print("hello')- state.history.append(ipython_action_3)- ipython_observation_3 = IPythonRunCellObservation(- content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 3)\nEnough',- code='print("hello',- )- # ipython_observation_3._cause = ipython_action_3._id- state.history.append(ipython_observation_3)-- ipython_action_4 = IPythonRunCellAction(code='print("hello')- state.history.append(ipython_action_4)- ipython_observation_4 = IPythonRunCellObservation(- content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 2)\nLast line of output',- code='print("hello',- )- # ipython_observation_4._cause = ipython_action_4._id- state.history.append(ipython_observation_4)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is False- mock_warning.assert_not_called()-- def test_is_stuck_repeating_action_observation_pattern(- self, stuck_detector: StuckDetector- ):- state = stuck_detector.state- message_action = MessageAction(content='Come on', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)- message_observation = NullObservation(content='')- state.history.append(message_observation)-- cmd_action_1 = CmdRunAction(command='ls')- state.history.append(cmd_action_1)- cmd_observation_1 = CmdOutputObservation(- command='ls', content='file1.txt\nfile2.txt'- )- # cmd_observation_1._cause = cmd_action_1._id- state.history.append(cmd_observation_1)-- read_action_1 = FileReadAction(path='file1.txt')- state.history.append(read_action_1)- read_observation_1 = FileReadObservation(- content='File content', path='file1.txt'- )- # read_observation_1._cause = read_action_1._id- state.history.append(read_observation_1)-- cmd_action_2 = CmdRunAction(command='ls')- state.history.append(cmd_action_2)- cmd_observation_2 = CmdOutputObservation(- command='ls', content='file1.txt\nfile2.txt'- )- # cmd_observation_2._cause = cmd_action_2._id- state.history.append(cmd_observation_2)-- read_action_2 = FileReadAction(path='file1.txt')- state.history.append(read_action_2)- read_observation_2 = FileReadObservation(- content='File content', path='file1.txt'- )- # read_observation_2._cause = read_action_2._id- state.history.append(read_observation_2)-- message_action = MessageAction(content='Come on', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)-- message_null_observation = NullObservation(content='')- state.history.append(message_null_observation)-- cmd_action_3 = CmdRunAction(command='ls')- state.history.append(cmd_action_3)- cmd_observation_3 = CmdOutputObservation(- command='ls', content='file1.txt\nfile2.txt'- )- # cmd_observation_3._cause = cmd_action_3._id- state.history.append(cmd_observation_3)-- read_action_3 = FileReadAction(path='file1.txt')- state.history.append(read_action_3)- read_observation_3 = FileReadObservation(- content='File content', path='file1.txt'- )- # read_observation_3._cause = read_action_3._id- state.history.append(read_observation_3)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with('Action, Observation pattern detected')- assert (- stuck_detector.stuck_analysis.loop_type- == 'repeating_action_observation_pattern'- )- assert stuck_detector.stuck_analysis.loop_repeat_times == 3- assert stuck_detector.stuck_analysis.loop_start_idx == 0 # null ignored-- def test_is_stuck_not_stuck(self, stuck_detector: StuckDetector):- state = stuck_detector.state- message_action = MessageAction(content='Done', wait_for_response=False)- message_action._source = EventSource.USER-- hello_action = MessageAction(content='Hello', wait_for_response=False)- state.history.append(hello_action)- hello_observation = NullObservation(content='')- # hello_observation._cause = hello_action._id- state.history.append(hello_observation)-- cmd_action_1 = CmdRunAction(command='ls')- state.history.append(cmd_action_1)- cmd_observation_1 = CmdOutputObservation(- command='ls', content='file1.txt\nfile2.txt'- )- # cmd_observation_1._cause = cmd_action_1._id- state.history.append(cmd_observation_1)-- read_action_1 = FileReadAction(path='file1.txt')- state.history.append(read_action_1)- read_observation_1 = FileReadObservation(- content='File content', path='file1.txt'- )- # read_observation_1._cause = read_action_1._id- state.history.append(read_observation_1)-- cmd_action_2 = CmdRunAction(command='pwd')- state.history.append(cmd_action_2)- cmd_observation_2 = CmdOutputObservation(command='pwd', content='/home/user')- # cmd_observation_2._cause = cmd_action_2._id- state.history.append(cmd_observation_2)-- read_action_2 = FileReadAction(path='file2.txt')- state.history.append(read_action_2)- read_observation_2 = FileReadObservation(- content='Another file content', path='file2.txt'- )- # read_observation_2._cause = read_action_2._id- state.history.append(read_observation_2)-- message_null_observation = NullObservation(content='')- state.history.append(message_action)- state.history.append(message_null_observation)-- cmd_action_3 = CmdRunAction(command='pwd')- state.history.append(cmd_action_3)- cmd_observation_3 = CmdOutputObservation(command='pwd', content='/home/user')- # cmd_observation_3._cause = cmd_action_3._id- state.history.append(cmd_observation_3)-- read_action_3 = FileReadAction(path='file2.txt')- state.history.append(read_action_3)- read_observation_3 = FileReadObservation(- content='Another file content', path='file2.txt'- )- # read_observation_3._cause = read_action_3._id- state.history.append(read_observation_3)-- assert stuck_detector.is_stuck(headless_mode=True) is False-- def test_is_stuck_monologue(self, stuck_detector):- state = stuck_detector.state- # Add events to the history list directly- message_action_1 = MessageAction(content='Hi there!')- message_action_1._source = EventSource.USER- state.history.append(message_action_1)- message_action_2 = MessageAction(content='Hi there!')- message_action_2._source = EventSource.AGENT- state.history.append(message_action_2)- message_action_3 = MessageAction(content='How are you?')- message_action_3._source = EventSource.USER- state.history.append(message_action_3)-- cmd_kill_action = CmdRunAction(- command='echo 42', thought="I'm not stuck, he's stuck"- )- state.history.append(cmd_kill_action)-- message_action_4 = MessageAction(content="I'm doing well, thanks for asking.")- message_action_4._source = EventSource.AGENT- state.history.append(message_action_4)- message_action_5 = MessageAction(content="I'm doing well, thanks for asking.")- message_action_5._source = EventSource.AGENT- state.history.append(message_action_5)- message_action_6 = MessageAction(content="I'm doing well, thanks for asking.")- message_action_6._source = EventSource.AGENT- state.history.append(message_action_6)-- assert stuck_detector.is_stuck(headless_mode=True)- assert stuck_detector.stuck_analysis.loop_type == 'monologue'- assert stuck_detector.stuck_analysis.loop_repeat_times == 3- assert stuck_detector.stuck_analysis.loop_start_idx == 2 # null ignored-- # Add an observation event between the repeated message actions- cmd_output_observation = CmdOutputObservation(- content='OK, I was stuck, but no more.',- command='storybook',- exit_code=0,- )- # cmd_output_observation._cause = cmd_kill_action._id- state.history.append(cmd_output_observation)-- message_action_7 = MessageAction(content="I'm doing well, thanks for asking.")- message_action_7._source = EventSource.AGENT- state.history.append(message_action_7)- message_action_8 = MessageAction(content="I'm doing well, thanks for asking.")- message_action_8._source = EventSource.AGENT- state.history.append(message_action_8)-- with patch('logging.Logger.warning'):- assert not stuck_detector.is_stuck(headless_mode=True)-- def test_is_stuck_context_window_error_loop(self, stuck_detector):- """Test that we detect when we're stuck in a loop of context window errors."""- state = stuck_detector.state-- # Add some initial events- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)- message_observation = NullObservation(content='')- state.history.append(message_observation)-- # Add ten consecutive condensation events (should detect as stuck)- for _ in range(10):- condensation = AgentCondensationObservation(- content='Trimming prompt to meet context window limitations'- )- state.history.append(condensation)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with(- 'Context window error loop detected - repeated condensation events'- )- assert stuck_detector.stuck_analysis.loop_type == 'context_window_error'- assert stuck_detector.stuck_analysis.loop_repeat_times == 2- assert stuck_detector.stuck_analysis.loop_start_idx == 0-- def test_is_not_stuck_context_window_error_with_other_events(self, stuck_detector):- """Test that we don't detect a loop when there are other events between condensation events."""- state = stuck_detector.state-- # Add some initial events- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)- message_observation = NullObservation(content='')- state.history.append(message_observation)-- # Add 10 condensation events with other events between them- for i in range(10):- # Add a condensation event- condensation = AgentCondensationObservation(- content='Trimming prompt to meet context window limitations'- )- state.history.append(condensation)-- # Add some other events between condensation events (except after the last one)- if i < 9:- # Add a command action and observation- cmd_action = CmdRunAction(command=f'ls {i}')- state.history.append(cmd_action)- cmd_observation = CmdOutputObservation(- command=f'ls {i}', content='file1.txt\nfile2.txt'- )- state.history.append(cmd_observation)-- # Add a file read action and observation for even iterations- if i % 2 == 0:- read_action = FileReadAction(path=f'file{i}.txt')- state.history.append(read_action)- read_observation = FileReadObservation(- content=f'File content {i}', path=f'file{i}.txt'- )- state.history.append(read_observation)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is False- mock_warning.assert_not_called()-- def test_is_not_stuck_context_window_error_less_than_ten(self, stuck_detector):- """Test that we don't detect a loop with less than ten condensation events."""- state = stuck_detector.state-- # Add some initial events- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)- message_observation = NullObservation(content='')- state.history.append(message_observation)-- # Add only nine condensation events (should not detect as stuck)- for _ in range(9):- condensation = AgentCondensationObservation(- content='Trimming prompt to meet context window limitations'- )- state.history.append(condensation)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is False- mock_warning.assert_not_called()-- def test_is_stuck_context_window_error_with_user_messages(self, stuck_detector):- """Test that we still detect a loop even with user messages between condensation events in headless mode.-- User messages are filtered out in the stuck detection logic, so they shouldn't- prevent us from detecting a loop of condensation events.- """- state = stuck_detector.state-- # Add some initial events- message_action = MessageAction(content='Hello', wait_for_response=False)- message_action._source = EventSource.USER- state.history.append(message_action)- message_observation = NullObservation(content='')- state.history.append(message_observation)-- # Add condensation events with user messages between them (total of 10)- for i in range(10):- # Add a condensation event- condensation = AgentCondensationObservation(- content='Trimming prompt to meet context window limitations'- )- state.history.append(condensation)-- # Add user message between condensation events (except after the last one)- if i < 9:- user_message = MessageAction(- content=f'Please continue {i}', wait_for_response=False- )- user_message._source = EventSource.USER- state.history.append(user_message)- user_observation = NullObservation(content='')- state.history.append(user_observation)-- with patch('logging.Logger.warning') as mock_warning:- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with(- 'Context window error loop detected - repeated condensation events'- )- assert stuck_detector.stuck_analysis.loop_type == 'context_window_error'- assert stuck_detector.stuck_analysis.loop_repeat_times == 2- assert stuck_detector.stuck_analysis.loop_start_idx == 0-- def test_is_not_stuck_context_window_error_in_non_headless(self, stuck_detector):- """Test non-headless mode with condensation events before last user message.-- In non-headless mode, we don't detect a loop if the condensation events- are before the last user message. We only look at events after the last- user message.- """- state = stuck_detector.state-- # Add condensation events first- for _ in range(10):- condensation = AgentCondensationObservation(- content='Trimming prompt to meet context window limitations'- )- state.history.append(condensation)-- # Add a user message at the end- user_message = MessageAction(content='Please continue', wait_for_response=False)- user_message._source = EventSource.USER- state.history.append(user_message)- user_observation = NullObservation(content='')- state.history.append(user_observation)-- with patch('logging.Logger.warning') as mock_warning:- # In headless mode, we should detect the loop- assert stuck_detector.is_stuck(headless_mode=True) is True- mock_warning.assert_called_once_with(- 'Context window error loop detected - repeated condensation events'- )-- # Reset mock for next assertion- mock_warning.reset_mock()-- # In non-headless mode, we should NOT detect the loop since we only look- # at events after the last user message- assert stuck_detector.is_stuck(headless_mode=False) is False- mock_warning.assert_not_called()-- @pytest.fixture- def stuck_detector_mcdc(self):- return StuckDetector(state=None)-- def test_fail_guard_five_actions(self, stuck_detector_mcdc: StuckDetector):- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1, obs_1, action_2, obs_2- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_3, obs_3, action_4, obs_4- read_file1_action,- read_file1_observation, # action_5, obs_5- ] # Total 5 actions, 5 observations- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_fail_guard_five_obs(self, stuck_detector_mcdc: StuckDetector):- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1, obs_1, action_2, obs_2- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_3, obs_3, action_4, obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action, # action_5, obs_5, action_6 (missing obs_6)- ] # Total 6 actions, 5 observations- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_fail_actions_break_A6_A4(self, stuck_detector_mcdc: StuckDetector):- # action_6(cmd_ls_action) != action_4(pwd_action)- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1,obs_1, action_2,obs_2- read_file1_action,- read_file1_observation,- pwd_action,- cmd_ls_observation, # action_3,obs_3, action_4(pwd_action),obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5,obs_5, action_6(cmd_ls_action),obs_6- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_fail_actions_break_A5_A3(self, stuck_detector_mcdc: StuckDetector):- # action_5(read_file1_action) != action_3(read_file2_action)- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1,obs_1, action_2,obs_2- read_file2_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_3(read_file2_action),obs_3, action_4,obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5(read_file1_action),obs_5, action_6,obs_6- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_fail_obs_break_O6_O4(self, stuck_detector_mcdc: StuckDetector):- # obs_6(cmd_ls_observation) != obs_4(cmd_ls_different_observation)- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1,obs_1, action_2,obs_2- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_different_observation, # action_3,obs_3, action_4,obs_4(cmd_ls_different_observation)- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5,obs_5, action_6,obs_6(cmd_ls_observation)- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_fail_obs_break_O5_O3(self, stuck_detector_mcdc: StuckDetector):- # obs_5(read_file1_observation) != obs_3(read_file2_observation)- history: list[Event] = [- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1,obs_1, action_2,obs_2- read_file1_action,- read_file2_observation,- cmd_ls_action,- cmd_ls_observation, # action_3,obs_3(read_file2_observation), action_4,obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5(read_file1_observation),obs_5, action_6,obs_6- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0)- is False- )-- def test_loop_ignores_other_events(self, stuck_detector_mcdc: StuckDetector):- history: list[Event] = [- other_event,- read_file1_action,- read_file1_observation,- other_event, # action_1, obs_1- cmd_ls_action,- cmd_ls_observation,- other_event,- read_file1_action,- read_file1_observation, # action_2, obs_2, action_3, obs_3- cmd_ls_action,- cmd_ls_observation,- other_event,- read_file1_action,- read_file1_observation, # action_4, obs_4, action_5, obs_5- cmd_ls_action,- cmd_ls_observation,- other_event, # action_6, obs_6- ]-- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0) is True- )-- def test_loop_ignores_extra_actions(self, stuck_detector_mcdc: StuckDetector):- history: list[Event] = [- cmd_ls_action, # Extra action (action_0), will be ignored- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1, obs_1, action_2, obs_2- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_3, obs_3, action_4, obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5, obs_5, action_6, obs_6- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0) is True- )-- def test_loop_ignores_extra_observations(self, stuck_detector_mcdc: StuckDetector):- history: list[Event] = [- read_file1_observation, # Extra observation (obs_0), will be ignored- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_1, obs_1, action_2, obs_2- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_3, obs_3, action_4, obs_4- read_file1_action,- read_file1_observation,- cmd_ls_action,- cmd_ls_observation, # action_5, obs_5, action_6, obs_6- ]- assert (- stuck_detector_mcdc._is_stuck_action_observation_pattern(history, 0) is True- )---class TestAgentController:- @pytest.fixture- def controller(self):- controller = Mock(spec=AgentController)- controller._is_stuck = AgentController._is_stuck.__get__(- controller, AgentController- )- controller.delegate = None- controller.state = Mock()- return controller-- def test_is_stuck_delegate_stuck(self, controller: AgentController):- controller.delegate = Mock()- controller.delegate._is_stuck.return_value = True- assert controller._is_stuck() is Truediff --git a/tests/unit/core/config/test_config.py b/tests/unit/core/config/test_config.pyindex 3446163045..eb025f7e4e 100644--- a/tests/unit/core/config/test_config.py+++ b/tests/unit/core/config/test_config.py@@ -9,11 +9,9 @@ from openhands.core.config import (LLMConfig,OpenHandsConfig,finalize_config,- get_agent_config_arg,get_llm_config_arg,load_from_env,load_from_toml,- load_openhands_config,)from openhands.core.config.condenser_config import (ConversationWindowCondenserConfig,@@ -139,22 +137,6 @@ default_agent = "TestAgent"default_config.get_llm_config_from_agent('CodeActAgent')== default_config.get_llm_config())- assert (- default_config.get_agent_config('CodeActAgent').enable_prompt_extensions is True- )-- # defined agent config overrides default ones- assert default_config.get_llm_config_from_agent(- 'BrowsingAgent'- ) == default_config.get_llm_config('cheap')- assert (- default_config.get_llm_config_from_agent('BrowsingAgent').model- == 'some-cheap-model'- )- assert (- default_config.get_agent_config('BrowsingAgent').enable_prompt_extensions- is False- )assert default_config.sandbox.volumes == '/opt/files2/workspace:/workspace:rw'assert default_config.sandbox.timeout == 1@@ -1059,180 +1041,3 @@ api_key="redacted"llm_config = get_llm_config_arg('gpt3', temp_toml_file)assert llm_config.model == 'gpt-3.5-turbo'---def test_get_agent_configs(default_config, temp_toml_file):- temp_toml = """-[core]-max_iterations = 100-max_budget_per_task = 4.0--[agent.CodeActAgent]-enable_prompt_extensions = true--[agent.BrowsingAgent]-enable_jupyter = false-"""-- with open(temp_toml_file, 'w') as f:- f.write(temp_toml)-- load_from_toml(default_config, temp_toml_file)-- codeact_config = default_config.get_agent_configs().get('CodeActAgent')- assert codeact_config.enable_prompt_extensions is True- browsing_config = default_config.get_agent_configs().get('BrowsingAgent')- assert browsing_config.enable_jupyter is False---def test_get_agent_config_arg(temp_toml_file):- temp_toml = """-[core]-max_iterations = 100-max_budget_per_task = 4.0--[agent.CodeActAgent]-enable_prompt_extensions = false-enable_browsing = false--[agent.BrowsingAgent]-enable_prompt_extensions = true-enable_jupyter = false-"""-- with open(temp_toml_file, 'w') as f:- f.write(temp_toml)-- agent_config = get_agent_config_arg('CodeActAgent', temp_toml_file)- assert not agent_config.enable_prompt_extensions- assert not agent_config.enable_browsing-- agent_config2 = get_agent_config_arg('BrowsingAgent', temp_toml_file)- assert agent_config2.enable_prompt_extensions- assert not agent_config2.enable_jupyter---def test_agent_config_custom_group_name(temp_toml_file):- temp_toml = """-[core]-max_iterations = 99--[agent.group1]-enable_prompt_extensions = true--[agent.group2]-enable_prompt_extensions = false-"""- with open(temp_toml_file, 'w') as f:- f.write(temp_toml)-- # just a sanity check that load app config wouldn't fail- app_config = load_openhands_config(config_file=temp_toml_file)- assert app_config.max_iterations == 99-- # run_infer in evaluation can use `get_agent_config_arg` to load custom- # agent configs with any group name (not just agent name)- agent_config1 = get_agent_config_arg('group1', temp_toml_file)- assert agent_config1.enable_prompt_extensions- agent_config2 = get_agent_config_arg('group2', temp_toml_file)- assert not agent_config2.enable_prompt_extensions---def test_agent_config_from_toml_section():- """Test that AgentConfig.from_toml_section correctly parses agent configurations from TOML."""- from openhands.core.config.agent_config import AgentConfig-- # Test with base config and custom configs- agent_section = {- 'enable_prompt_extensions': True,- 'enable_browsing': True,- 'CustomAgent1': {'enable_browsing': False},- 'CustomAgent2': {'enable_prompt_extensions': False},- 'InvalidAgent': {- 'invalid_field': 'some_value' # This should be skipped but not affect others- },- }-- # Parse the section- result = AgentConfig.from_toml_section(agent_section)-- # Verify the base config was correctly parsed- assert 'agent' in result- assert result['agent'].enable_prompt_extensions is True- assert result['agent'].enable_browsing is True-- # Verify custom configs were correctly parsed and inherit from base- assert 'CustomAgent1' in result- assert result['CustomAgent1'].enable_browsing is False # Overridden- assert result['CustomAgent1'].enable_prompt_extensions is True # Inherited-- assert 'CustomAgent2' in result- assert result['CustomAgent2'].enable_browsing is True # Inherited- assert result['CustomAgent2'].enable_prompt_extensions is False # Overridden-- # Verify the invalid config was skipped- assert 'InvalidAgent' not in result---def test_agent_config_from_toml_section_with_invalid_base():- """Test that AgentConfig.from_toml_section handles invalid base configurations gracefully."""- from openhands.core.config.agent_config import AgentConfig-- # Test with invalid base config but valid custom configs- agent_section = {- 'invalid_field': 'some_value', # This should be ignored in base config- 'enable_jupyter': 'not_a_bool', # This should cause validation error- 'CustomAgent': {- 'enable_browsing': False,- 'enable_jupyter': True,- },- }-- # Parse the section- result = AgentConfig.from_toml_section(agent_section)-- # Verify a default base config was created despite the invalid fields- assert 'agent' in result- assert result['agent'].enable_browsing is True # Default value- assert result['agent'].enable_jupyter is True # Default value-- # Verify custom config was still processed correctly- assert 'CustomAgent' in result- assert result['CustomAgent'].enable_browsing is False- assert result['CustomAgent'].enable_jupyter is True---def test_agent_config_system_prompt_filename_default():- """Test that AgentConfig defaults to 'system_prompt.j2' for system_prompt_filename."""- config = AgentConfig()- assert config.system_prompt_filename == 'system_prompt.j2'---def test_agent_config_system_prompt_filename_toml_integration(- default_config, temp_toml_file-):- """Test that system_prompt_filename is correctly loaded from TOML configuration."""- with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:- toml_file.write(- """-[agent]-enable_browsing = true-system_prompt_filename = "custom_prompt.j2"--[agent.CodeReviewAgent]-system_prompt_filename = "code_review_prompt.j2"-enable_browsing = false-"""- )-- load_from_toml(default_config, temp_toml_file)-- # Check default agent config- default_agent_config = default_config.get_agent_config()- assert default_agent_config.system_prompt_filename == 'custom_prompt.j2'- assert default_agent_config.enable_browsing is True-- # Check custom agent config- custom_agent_config = default_config.get_agent_config('CodeReviewAgent')- assert custom_agent_config.system_prompt_filename == 'code_review_prompt.j2'- assert custom_agent_config.enable_browsing is Falsediff --git a/tests/unit/integrations/bitbucket/test_bitbucket.py b/tests/unit/integrations/bitbucket/test_bitbucket.pyindex 47391c6925..1c2e6c1b60 100644--- a/tests/unit/integrations/bitbucket/test_bitbucket.py+++ b/tests/unit/integrations/bitbucket/test_bitbucket.py@@ -1,6 +1,5 @@"""Tests for Bitbucket integration."""-import osimport unittestfrom unittest.mock import AsyncMock, MagicMock, patch@@ -624,160 +623,3 @@ async def test_get_user_handles_user_emails_api_failure():assert user.email is Noneassert user.login == 'testuser'assert user.name == 'Test User'---# Setup.py Bitbucket Token Tests-@patch('openhands.core.setup.call_async_from_sync')-def test_initialize_repository_for_runtime_with_bitbucket_token(- mock_call_async_from_sync,-):- """Test that initialize_repository_for_runtime properly handles BITBUCKET_TOKEN."""- from openhands.core.setup import initialize_repository_for_runtime- from openhands.integrations.provider import ProviderType-- # Mock runtime- mock_runtime = MagicMock()- mock_runtime.clone_or_init_repo = AsyncMock(return_value='test-repo')- mock_runtime.maybe_run_setup_script = MagicMock()- mock_runtime.maybe_setup_git_hooks = MagicMock()-- # Mock call_async_from_sync to return the expected result- mock_call_async_from_sync.return_value = 'test-repo'-- # Set up environment with BITBUCKET_TOKEN- with patch.dict(os.environ, {'BITBUCKET_TOKEN': 'username:app_password'}):- result = initialize_repository_for_runtime(- runtime=mock_runtime, selected_repository='openhands/test-repo'- )-- # Verify the result- assert result == 'test-repo'-- # Verify that call_async_from_sync was called with the correct arguments- mock_call_async_from_sync.assert_called_once()- args, kwargs = mock_call_async_from_sync.call_args-- # Check that the function called was clone_or_init_repo- assert args[0] == mock_runtime.clone_or_init_repo-- # Check that provider tokens were passed correctly- provider_tokens = args[2] # Third argument is immutable_provider_tokens- assert provider_tokens is not None- assert ProviderType.BITBUCKET in provider_tokens- assert (- provider_tokens[ProviderType.BITBUCKET].token.get_secret_value()- == 'username:app_password'- )-- # Check that the repository was passed correctly- assert args[3] == 'openhands/test-repo' # selected_repository- assert args[4] is None # selected_branch---@patch('openhands.core.setup.call_async_from_sync')-def test_initialize_repository_for_runtime_with_multiple_tokens(- mock_call_async_from_sync,-):- """Test that initialize_repository_for_runtime handles multiple provider tokens including Bitbucket."""- from openhands.core.setup import initialize_repository_for_runtime- from openhands.integrations.provider import ProviderType-- # Mock runtime- mock_runtime = MagicMock()- mock_runtime.clone_or_init_repo = AsyncMock(return_value='test-repo')- mock_runtime.maybe_run_setup_script = MagicMock()- mock_runtime.maybe_setup_git_hooks = MagicMock()-- # Mock call_async_from_sync to return the expected result- mock_call_async_from_sync.return_value = 'test-repo'-- # Set up environment with multiple tokens- with patch.dict(- os.environ,- {- 'GITHUB_TOKEN': 'github_token_123',- 'GITLAB_TOKEN': 'gitlab_token_456',- 'BITBUCKET_TOKEN': 'username:bitbucket_app_password',- },- ):- result = initialize_repository_for_runtime(- runtime=mock_runtime, selected_repository='openhands/test-repo'- )-- # Verify the result- assert result == 'test-repo'-- # Verify that call_async_from_sync was called- mock_call_async_from_sync.assert_called_once()- args, kwargs = mock_call_async_from_sync.call_args-- # Check that provider tokens were passed correctly- provider_tokens = args[2] # Third argument is immutable_provider_tokens- assert provider_tokens is not None-- # Verify all three provider types are present- assert ProviderType.GITHUB in provider_tokens- assert ProviderType.GITLAB in provider_tokens- assert ProviderType.BITBUCKET in provider_tokens-- # Verify token values- assert (- provider_tokens[ProviderType.GITHUB].token.get_secret_value()- == 'github_token_123'- )- assert (- provider_tokens[ProviderType.GITLAB].token.get_secret_value()- == 'gitlab_token_456'- )- assert (- provider_tokens[ProviderType.BITBUCKET].token.get_secret_value()- == 'username:bitbucket_app_password'- )---@patch('openhands.core.setup.call_async_from_sync')-def test_initialize_repository_for_runtime_without_bitbucket_token(- mock_call_async_from_sync,-):- """Test that initialize_repository_for_runtime works without BITBUCKET_TOKEN."""- from openhands.core.setup import initialize_repository_for_runtime- from openhands.integrations.provider import ProviderType-- # Mock runtime- mock_runtime = MagicMock()- mock_runtime.clone_or_init_repo = AsyncMock(return_value='test-repo')- mock_runtime.maybe_run_setup_script = MagicMock()- mock_runtime.maybe_setup_git_hooks = MagicMock()-- # Mock call_async_from_sync to return the expected result- mock_call_async_from_sync.return_value = 'test-repo'-- # Set up environment without BITBUCKET_TOKEN but with other tokens- with patch.dict(- os.environ,- {'GITHUB_TOKEN': 'github_token_123', 'GITLAB_TOKEN': 'gitlab_token_456'},- clear=False,- ):- # Ensure BITBUCKET_TOKEN is not in environment- if 'BITBUCKET_TOKEN' in os.environ:- del os.environ['BITBUCKET_TOKEN']-- result = initialize_repository_for_runtime(- runtime=mock_runtime, selected_repository='openhands/test-repo'- )-- # Verify the result- assert result == 'test-repo'-- # Verify that call_async_from_sync was called- mock_call_async_from_sync.assert_called_once()- args, kwargs = mock_call_async_from_sync.call_args-- # Check that provider tokens were passed correctly- provider_tokens = args[2] # Third argument is immutable_provider_tokens- assert provider_tokens is not None-- # Verify only GitHub and GitLab are present, not Bitbucket- assert ProviderType.GITHUB in provider_tokens- assert ProviderType.GITLAB in provider_tokens- assert ProviderType.BITBUCKET not in provider_tokensdiff --git a/tests/unit/mcp/test_mcp_tool_timeout_stall.py b/tests/unit/mcp/test_mcp_tool_timeout_stall.pydeleted file mode 100644index c2ad58f90f..0000000000--- a/tests/unit/mcp/test_mcp_tool_timeout_stall.py+++ /dev/null@@ -1,267 +0,0 @@-"""Test for MCP tool timeout causing agent to stall indefinitely."""--import asyncio-import json-from unittest import mock--import pytest-from mcp import McpError--from openhands.controller.agent import Agent-from openhands.controller.agent_controller import AgentController-from openhands.core.schema import AgentState-from openhands.events.action.mcp import MCPAction-from openhands.events.action.message import SystemMessageAction-from openhands.events.event import EventSource-from openhands.events.observation.mcp import MCPObservation-from openhands.events.stream import EventStream-from openhands.mcp.client import MCPClient-from openhands.mcp.tool import MCPClientTool-from openhands.mcp.utils import call_tool_mcp-from openhands.server.services.conversation_stats import ConversationStats-from openhands.storage.memory import InMemoryFileStore---class MockConfig:- """Mock config for testing."""-- def __init__(self):- self.max_message_chars = 10000---class MockLLM:- """Mock LLM for testing."""-- def __init__(self):- self.metrics = None- self.config = MockConfig()---def conversation_stats():- return ConversationStats(None, 'convo-id', None)---class MockAgent(Agent):- """Mock agent for testing."""-- def __init__(self):- self.step_called = False- self.next_action = None- self.llm = MockLLM()-- def step(self, *args, **kwargs):- """Mock step method."""- self.step_called = True- return self.next_action-- def get_system_message(self):- """Mock get_system_message method."""- return SystemMessageAction(content='System message')---async def test_mcp_tool_timeout_error_handling(conversation_stats):- """Test that verifies MCP tool timeout errors are properly handled and returned as observations."""- # Create a mock MCPClient- mock_client = mock.MagicMock(spec=MCPClient)-- # Configure the mock to raise a McpError when call_tool is called- async def mock_call_tool(*args, **kwargs):- # Simulate a timeout- await asyncio.sleep(0.1)- # Create a mock error object with the message attribute- error = mock.MagicMock()- error.message = 'Timed out while waiting for response to ClientRequest. Waited 30.0 seconds.'- raise McpError(error)-- mock_client.call_tool.side_effect = mock_call_tool-- # Create a mock tool- mock_tool = MCPClientTool(- name='test_tool',- description='Test tool',- inputSchema={'type': 'object', 'properties': {}},- session=None,- )- mock_client.tools = [mock_tool]- mock_client.tool_map = {'test_tool': mock_tool}-- # Create a mock file store- mock_file_store = InMemoryFileStore({})-- # Create a mock event stream- event_stream = EventStream(sid='test-session', file_store=mock_file_store)-- # Create a mock agent- agent = MockAgent()-- # Create a mock agent controller- controller = AgentController(- agent=agent,- event_stream=event_stream,- conversation_stats=conversation_stats,- iteration_delta=10,- budget_per_task_delta=None,- sid='test-session',- )-- # Set up the agent state- await controller.set_agent_state_to(AgentState.RUNNING)-- # Create an MCP action- mcp_action = MCPAction(- name='test_tool',- arguments={'param': 'value'},- thought='Testing MCP timeout handling',- )-- # Add the action to the event stream- event_stream.add_event(mcp_action, EventSource.AGENT)-- # Set the pending action- controller._pending_action = mcp_action-- # Before the fix, this would raise an exception and not return an observation- # Now with the fix, it should return an error observation- result = await call_tool_mcp([mock_client], mcp_action)-- # Verify that the function returns an error observation- assert isinstance(result, MCPObservation)- content = json.loads(result.content)- assert content['isError'] is True- assert 'timed out' in content['error'].lower()-- # The agent controller would now be able to continue processing- # because it received an error observation instead of an exception-- # Verify that the agent is still in the RUNNING state- assert controller.get_agent_state() == AgentState.RUNNING-- # Verify that the agent can continue processing- agent.next_action = MCPAction(- name='another_tool',- arguments={'param': 'value'},- thought='Another action after timeout',- )-- # The agent controller would be able to step because it received an observation- # This demonstrates that the fix is working--[email protected](reason='2025-10-07 : This test is flaky')-async def test_mcp_tool_timeout_agent_continuation(conversation_stats):- """Test that verifies the agent can continue processing after an MCP tool timeout."""- # Create a mock MCPClient- mock_client = mock.MagicMock(spec=MCPClient)-- # Configure the mock to raise a McpError when call_tool is called- async def mock_call_tool(*args, **kwargs):- # Simulate a timeout- await asyncio.sleep(0.1)- # Create a mock error object with the message attribute- error = mock.MagicMock()- error.message = 'Timed out while waiting for response to ClientRequest. Waited 30.0 seconds.'- raise McpError(error)-- mock_client.call_tool.side_effect = mock_call_tool-- # Create a mock tool- mock_tool = MCPClientTool(- name='test_tool',- description='Test tool',- inputSchema={'type': 'object', 'properties': {}},- session=None,- )- mock_client.tools = [mock_tool]- mock_client.tool_map = {'test_tool': mock_tool}-- # Create a mock file store- mock_file_store = InMemoryFileStore({})-- # Create a mock event stream- event_stream = EventStream(sid='test-session', file_store=mock_file_store)-- # Create a mock agent- agent = MockAgent()-- # Create a mock agent controller- controller = AgentController(- agent=agent,- event_stream=event_stream,- conversation_stats=conversation_stats,- iteration_delta=10,- budget_per_task_delta=None,- sid='test-session',- )-- # Set up the agent state- await controller.set_agent_state_to(AgentState.RUNNING)-- # Create an MCP action- mcp_action = MCPAction(- name='test_tool',- arguments={'param': 'value'},- thought='Testing MCP timeout handling',- )-- # Add the action to the event stream- event_stream.add_event(mcp_action, EventSource.AGENT)-- # Set the pending action- controller._pending_action = mcp_action-- # Now implement the fix in call_tool_mcp- async def fixed_call_tool_mcp(clients, action):- try:- # This will raise the McpError- await mock_client.call_tool(action.name, action.arguments)- except McpError as e:- # Create an error observation- error_content = json.dumps(- {'isError': True, 'error': str(e), 'content': []}- )- observation = MCPObservation(- content=error_content,- name=action.name,- arguments=action.arguments,- )- # Set the cause- setattr(observation, '_cause', action.id)- return observation-- # Use our fixed function- with mock.patch(- 'openhands.mcp.utils.call_tool_mcp', side_effect=fixed_call_tool_mcp- ):- # Call the function that would normally be called by the agent controller- result = await call_tool_mcp([mock_client], mcp_action)-- # Verify that the function returns an error observation- assert isinstance(result, MCPObservation)- content = json.loads(result.content)- assert content['isError'] is True- assert 'timed out' in content['error'].lower()-- # Now simulate the agent controller's handling of the observation- event_stream.add_event(result, EventSource.ENVIRONMENT)-- # Verify that the pending action is cleared- controller._pending_action = None-- # Verify that the agent is still in the RUNNING state- assert controller.get_agent_state() == AgentState.RUNNING-- # Verify that the agent can continue processing- agent.next_action = MCPAction(- name='another_tool',- arguments={'param': 'value'},- thought='Another action after timeout',- )-- # Simulate a step- await controller._step()-- # Verify that the agent was asked to step- assert agent.step_called