π OpenHands: AI-Driven Development
Remove dead MonitoringListener code (#14157)
Tim O'Farrell28d26f817854
diff --git a/enterprise/server/config.py b/enterprise/server/config.pyindex ea24a70981..52b9cd6ea1 100644--- a/enterprise/server/config.py+++ b/enterprise/server/config.py@@ -72,9 +72,6 @@ class SaaSServerConfig(ServerConfig):auth_url: str | None = os.environ.get('AUTH_URL')settings_store_class: str = 'storage.saas_settings_store.SaasSettingsStore'secret_store_class: str = 'storage.saas_secrets_store.SaasSecretsStore'- monitoring_listener_class: str = (- 'server.saas_monitoring_listener.SaaSMonitoringListener'- )user_auth_class: str = 'server.auth.saas_user_auth.SaasUserAuth'# Maintenance window configurationmaintenance_start_time: str = os.environ.get(diff --git a/enterprise/server/saas_monitoring_listener.py b/enterprise/server/saas_monitoring_listener.pydeleted file mode 100644index 83d3a3657a..0000000000--- a/enterprise/server/saas_monitoring_listener.py+++ /dev/null@@ -1,55 +0,0 @@-from server.logger import logger--from openhands.core.config.openhands_config import OpenHandsConfig-from openhands.core.schema.agent import AgentState-from openhands.events.event import Event-from openhands.events.observation import (- AgentStateChangedObservation,-)-from openhands.server.monitoring import MonitoringListener---class SaaSMonitoringListener(MonitoringListener):- """Forward app signals to structured logging for GCP native monitoring."""-- def on_session_event(self, event: Event) -> None:- """Track metrics about events being added to a Session's EventStream."""- if (- isinstance(event, AgentStateChangedObservation)- and event.agent_state == AgentState.ERROR- ):- logger.info(- 'Tracking agent status error',- extra={'signal': 'saas_agent_status_errors'},- )-- def on_agent_session_start(self, success: bool, duration: float) -> None:- """Track an agent session start.-- Success is true if startup completed without error.- Duration is start time in seconds observed by AgentSession.- """- logger.info(- 'Tracking agent session start',- extra={- 'signal': 'saas_agent_session_start',- 'success': success,- 'duration': duration,- },- )-- def on_create_conversation(self) -> None:- """Track the beginning of conversation creation.-- Does not currently capture whether it succeed.- """- logger.info(- 'Tracking create conversation', extra={'signal': 'saas_create_conversation'}- )-- @classmethod- def get_instance(- cls,- config: OpenHandsConfig,- ) -> 'SaaSMonitoringListener':- return cls()diff --git a/enterprise/server/utils/conversation_callback_utils.py b/enterprise/server/utils/conversation_callback_utils.pydeleted file mode 100644index 62282e74ac..0000000000--- a/enterprise/server/utils/conversation_callback_utils.py+++ /dev/null@@ -1,279 +0,0 @@-import json-from datetime import datetime--from server.logger import logger-from sqlalchemy import and_, select-from storage.conversation_callback import (- CallbackStatus,- ConversationCallback,- ConversationCallbackProcessor,-)-from storage.conversation_work import ConversationWork-from storage.database import a_session_maker, session_maker-from storage.stored_conversation_metadata import StoredConversationMetadata--from openhands.core.config import load_openhands_config-from openhands.core.schema.agent import AgentState-from openhands.events.event_store import EventStore-from openhands.events.observation.agent import AgentStateChangedObservation-from openhands.events.serialization.event import event_from_dict-from openhands.storage import get_file_store-from openhands.storage.files import FileStore-from openhands.storage.locations import (- get_conversation_agent_state_filename,- get_conversation_dir,-)-from openhands.utils.async_utils import call_sync_from_async--config = load_openhands_config()-file_store = get_file_store(config.file_store, config.file_store_path)---async def process_event(- user_id: str, conversation_id: str, subpath: str, content: dict-):- """- Process a conversation event and invoke any registered callbacks.-- Args:- user_id: The user ID associated with the conversation- conversation_id: The conversation ID- subpath: The event subpath- content: The event content- """- logger.debug(- 'process_event',- extra={- 'user_id': user_id,- 'conversation_id': conversation_id,- 'content': content,- },- )- write_path = get_conversation_dir(conversation_id, user_id) + subpath-- # This writes to the google cloud storage, so we do this in a background thread to not block the main runloop...- await call_sync_from_async(file_store.write, write_path, json.dumps(content))-- event = event_from_dict(content)- if isinstance(event, AgentStateChangedObservation):- # Load and invoke all active callbacks for this conversation- await invoke_conversation_callbacks(conversation_id, event)-- # Update active working seconds if agent state is not Running- if event.agent_state != AgentState.RUNNING:- event_store = EventStore(conversation_id, file_store, user_id)- update_active_working_seconds(- event_store, conversation_id, user_id, file_store- )---async def invoke_conversation_callbacks(- conversation_id: str, observation: AgentStateChangedObservation-):- """- Load and invoke all active callbacks for a conversation.-- Args:- conversation_id: The conversation ID to process callbacks for- observation: The AgentStateChangedObservation that triggered the callback- """- async with a_session_maker() as session:- result = await session.execute(- select(ConversationCallback).filter(- and_(- ConversationCallback.conversation_id == conversation_id,- ConversationCallback.status == CallbackStatus.ACTIVE,- )- )- )- callbacks = result.scalars().all()-- for callback in callbacks:- try:- processor = callback.get_processor()- await processor.__call__(callback, observation)- logger.info(- 'callback_invoked_successfully',- extra={- 'conversation_id': conversation_id,- 'callback_id': callback.id,- 'processor_type': callback.processor_type,- },- )- except Exception as e:- logger.error(- 'callback_invocation_failed',- extra={- 'conversation_id': conversation_id,- 'callback_id': callback.id,- 'processor_type': callback.processor_type,- 'error': str(e),- },- )- # Mark callback as error status- callback.status = CallbackStatus.ERROR- callback.updated_at = datetime.now()-- await session.commit()---def update_conversation_metadata(conversation_id: str, content: dict):- """- Update conversation metadata with new content.-- Args:- conversation_id: The conversation ID to update- content: The metadata content to update- """- logger.debug(- 'update_conversation_metadata',- extra={- 'conversation_id': conversation_id,- 'content': content,- },- )- with session_maker() as session:- conversation = (- session.query(StoredConversationMetadata)- .filter(StoredConversationMetadata.conversation_id == conversation_id)- .first()- )- conversation.title = content.get('title') or conversation.title- conversation.last_updated_at = datetime.now()- conversation.accumulated_cost = (- content.get('accumulated_cost') or conversation.accumulated_cost- )- conversation.prompt_tokens = (- content.get('prompt_tokens') or conversation.prompt_tokens- )- conversation.completion_tokens = (- content.get('completion_tokens') or conversation.completion_tokens- )- conversation.total_tokens = (- content.get('total_tokens') or conversation.total_tokens- )- session.commit()---def register_callback_processor(- conversation_id: str, processor: ConversationCallbackProcessor-) -> int:- """- Register a callback processor for a conversation.-- Args:- conversation_id: The conversation ID to register the callback for- processor: The ConversationCallbackProcessor instance to register-- Returns:- int: The ID of the created callback- """- with session_maker() as session:- callback = ConversationCallback(- conversation_id=conversation_id, status=CallbackStatus.ACTIVE- )- callback.set_processor(processor)- session.add(callback)- session.commit()- return callback.id---def update_active_working_seconds(- event_store: EventStore, conversation_id: str, user_id: str, file_store: FileStore-):- """- Calculate and update the total active working seconds for a conversation.-- This function reads all events for the conversation, looks for AgentStateChanged- observations, and calculates the total time spent in a running state.-- Args:- event_store: The EventStore instance for reading events- conversation_id: The conversation ID to process- user_id: The user ID associated with the conversation- file_store: The FileStore instance for accessing conversation data- """- try:- # Track agent state changes and calculate running time- running_start_time = None- total_running_seconds = 0.0-- for event in event_store.search_events():- if isinstance(event, AgentStateChangedObservation) and event.timestamp:- event_timestamp = datetime.fromisoformat(event.timestamp).timestamp()-- if event.agent_state == AgentState.RUNNING:- # Agent started running- if running_start_time is None:- running_start_time = event_timestamp- elif running_start_time is not None:- # Agent stopped running, calculate duration- duration = event_timestamp - running_start_time- total_running_seconds += duration- running_start_time = None-- # If agent is still running at the end, don't count that time yet- # (it will be counted when the agent stops)-- # Create or update the conversation_work record- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- if conversation_work:- # Update existing record- conversation_work.seconds = total_running_seconds- conversation_work.updated_at = datetime.now().isoformat()- else:- # Create new record- conversation_work = ConversationWork(- conversation_id=conversation_id,- user_id=user_id,- seconds=total_running_seconds,- )- session.add(conversation_work)-- session.commit()-- logger.info(- 'updated_active_working_seconds',- extra={- 'conversation_id': conversation_id,- 'user_id': user_id,- 'total_seconds': total_running_seconds,- },- )-- except Exception as e:- logger.error(- 'failed_to_update_active_working_seconds',- extra={- 'conversation_id': conversation_id,- 'user_id': user_id,- 'error': str(e),- },- )---def update_agent_state(user_id: str, conversation_id: str, content: bytes):- """- Update agent state file for a conversation.-- Args:- user_id: The user ID associated with the conversation- conversation_id: The conversation ID- content: The agent state content as bytes- """- logger.debug(- 'update_agent_state',- extra={- 'user_id': user_id,- 'conversation_id': conversation_id,- 'content_size': len(content),- },- )- write_path = get_conversation_agent_state_filename(conversation_id, user_id)- file_store.write(write_path, content)diff --git a/enterprise/tests/unit/server/test_conversation_callback_utils.py b/enterprise/tests/unit/server/test_conversation_callback_utils.pydeleted file mode 100644index 2445b6a7da..0000000000--- a/enterprise/tests/unit/server/test_conversation_callback_utils.py+++ /dev/null@@ -1,533 +0,0 @@-"""-Tests for conversation_callback_utils.py-"""--from unittest.mock import Mock, patch--import pytest-from server.utils.conversation_callback_utils import update_active_working_seconds-from storage.conversation_work import ConversationWork--from openhands.core.schema.agent import AgentState-from openhands.events.observation.agent import AgentStateChangedObservation-from openhands.storage.files import FileStore---class TestUpdateActiveWorkingSeconds:- """Test the update_active_working_seconds function."""-- @pytest.fixture- def mock_file_store(self):- """Create a mock FileStore."""- return Mock(spec=FileStore)-- @pytest.fixture- def mock_event_store(self):- """Create a mock EventStore."""- return Mock()-- def test_update_active_working_seconds_multiple_state_changes(- self, session_maker, mock_event_store, mock_file_store- ):- """Test calculating active working seconds with multiple state changes between running and ready."""- conversation_id = 'test_conversation_123'- user_id = 'test_user_456'-- # Create a sequence of events with state changes between RUNNING and other states- # Timeline:- # t=0: RUNNING (start)- # t=10: AWAITING_USER_INPUT (10 seconds of running)- # t=15: RUNNING (start again)- # t=25: FINISHED (10 more seconds of running)- # t=30: RUNNING (start again)- # t=40: PAUSED (10 more seconds of running)- # Total: 30 seconds of running time-- # Create mock events with ISO-formatted timestamps for testing- events = []-- # First running period: 10 seconds- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.RUNNING- event1.timestamp = '1970-01-01T00:00:00.000000'- events.append(event1)-- event2 = Mock(spec=AgentStateChangedObservation)- event2.agent_state = AgentState.AWAITING_USER_INPUT- event2.timestamp = '1970-01-01T00:00:10.000000'- events.append(event2)-- # Second running period: 10 seconds- event3 = Mock(spec=AgentStateChangedObservation)- event3.agent_state = AgentState.RUNNING- event3.timestamp = '1970-01-01T00:00:15.000000'- events.append(event3)-- event4 = Mock(spec=AgentStateChangedObservation)- event4.agent_state = AgentState.FINISHED- event4.timestamp = '1970-01-01T00:00:25.000000'- events.append(event4)-- # Third running period: 10 seconds- event5 = Mock(spec=AgentStateChangedObservation)- event5.agent_state = AgentState.RUNNING- event5.timestamp = '1970-01-01T00:00:30.000000'- events.append(event5)-- event6 = Mock(spec=AgentStateChangedObservation)- event6.agent_state = AgentState.PAUSED- event6.timestamp = '1970-01-01T00:00:40.000000'- events.append(event6)-- # Configure the mock event store to return our test events- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify the ConversationWork record was created with correct total seconds- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.conversation_id == conversation_id- assert conversation_work.user_id == user_id- assert conversation_work.seconds == 30.0 # Total running time- assert conversation_work.created_at is not None- assert conversation_work.updated_at is not None-- def test_update_active_working_seconds_updates_existing_record(- self, session_maker, mock_event_store, mock_file_store- ):- """Test that the function updates an existing ConversationWork record."""- conversation_id = 'test_conversation_456'- user_id = 'test_user_789'-- # Create an existing ConversationWork record- with session_maker() as session:- existing_work = ConversationWork(- conversation_id=conversation_id,- user_id=user_id,- seconds=15.0, # Previous value- )- session.add(existing_work)- session.commit()-- # Create events with new running time- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.RUNNING- event1.timestamp = '1970-01-01T00:00:00.000000'-- event2 = Mock(spec=AgentStateChangedObservation)- event2.agent_state = AgentState.STOPPED- event2.timestamp = '1970-01-01T00:00:20.000000'-- events = [event1, event2]-- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify the existing record was updated- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.seconds == 20.0 # Updated value- assert conversation_work.user_id == user_id-- def test_update_active_working_seconds_agent_still_running(- self, session_maker, mock_event_store, mock_file_store- ):- """Test that time is not counted if agent is still running at the end."""- conversation_id = 'test_conversation_789'- user_id = 'test_user_012'-- # Create events where agent starts running but never stops- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.RUNNING- event1.timestamp = '1970-01-01T00:00:00.000000'-- event2 = Mock(spec=AgentStateChangedObservation)- event2.agent_state = AgentState.AWAITING_USER_INPUT- event2.timestamp = '1970-01-01T00:00:10.000000'-- event3 = Mock(spec=AgentStateChangedObservation)- event3.agent_state = AgentState.RUNNING- event3.timestamp = '1970-01-01T00:00:15.000000'-- events = [event1, event2, event3]- # No final state change - agent still running-- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify only the completed running period is counted- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.seconds == 10.0 # Only the first completed period-- def test_update_active_working_seconds_no_running_states(- self, session_maker, mock_event_store, mock_file_store- ):- """Test that zero seconds are recorded when there are no running states."""- conversation_id = 'test_conversation_000'- user_id = 'test_user_000'-- # Create events with no RUNNING states- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.LOADING- event1.timestamp = '1970-01-01T00:00:00.000000'-- event2 = Mock(spec=AgentStateChangedObservation)- event2.agent_state = AgentState.AWAITING_USER_INPUT- event2.timestamp = '1970-01-01T00:00:05.000000'-- event3 = Mock(spec=AgentStateChangedObservation)- event3.agent_state = AgentState.FINISHED- event3.timestamp = '1970-01-01T00:00:10.000000'-- events = [event1, event2, event3]-- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify zero seconds are recorded- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.seconds == 0.0-- def test_update_active_working_seconds_mixed_event_types(- self, session_maker, mock_event_store, mock_file_store- ):- """Test that only AgentStateChangedObservation events are processed."""- conversation_id = 'test_conversation_mixed'- user_id = 'test_user_mixed'-- # Create a mix of event types, only AgentStateChangedObservation should be processed- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.RUNNING- event1.timestamp = '1970-01-01T00:00:00.000000'-- # Mock other event types that should be ignored- event2 = Mock() # Not an AgentStateChangedObservation- event2.timestamp = '1970-01-01T00:00:05.000000'-- event3 = Mock() # Not an AgentStateChangedObservation- event3.timestamp = '1970-01-01T00:00:08.000000'-- event4 = Mock(spec=AgentStateChangedObservation)- event4.agent_state = AgentState.STOPPED- event4.timestamp = '1970-01-01T00:00:10.000000'-- events = [event1, event2, event3, event4]-- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify only the AgentStateChangedObservation events were processed- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.seconds == 10.0 # Only the valid state changes-- @patch('server.utils.conversation_callback_utils.logger')- def test_update_active_working_seconds_handles_exceptions(- self, mock_logger, session_maker, mock_event_store, mock_file_store- ):- """Test that exceptions are properly handled and logged."""- conversation_id = 'test_conversation_error'- user_id = 'test_user_error'-- # Configure the mock to raise an exception- mock_event_store.search_events.side_effect = Exception('Test error')-- # Call the function under test- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify the error was logged- mock_logger.error.assert_called_once()- error_call = mock_logger.error.call_args- assert error_call[0][0] == 'failed_to_update_active_working_seconds'- assert error_call[1]['extra']['conversation_id'] == conversation_id- assert error_call[1]['extra']['user_id'] == user_id- assert 'Test error' in error_call[1]['extra']['error']-- def test_update_active_working_seconds_complex_state_transitions(- self, session_maker, mock_event_store, mock_file_store- ):- """Test complex state transitions including error and rate limited states."""- conversation_id = 'test_conversation_complex'- user_id = 'test_user_complex'-- # Create a complex sequence of state changes- events = []-- # First running period: 5 seconds- event1 = Mock(spec=AgentStateChangedObservation)- event1.agent_state = AgentState.LOADING- event1.timestamp = '1970-01-01T00:00:00.000000'- events.append(event1)-- event2 = Mock(spec=AgentStateChangedObservation)- event2.agent_state = AgentState.RUNNING- event2.timestamp = '1970-01-01T00:00:02.000000'- events.append(event2)-- event3 = Mock(spec=AgentStateChangedObservation)- event3.agent_state = AgentState.ERROR- event3.timestamp = '1970-01-01T00:00:07.000000'- events.append(event3)-- # Second running period: 8 seconds- event4 = Mock(spec=AgentStateChangedObservation)- event4.agent_state = AgentState.RUNNING- event4.timestamp = '1970-01-01T00:00:10.000000'- events.append(event4)-- event5 = Mock(spec=AgentStateChangedObservation)- event5.agent_state = AgentState.RATE_LIMITED- event5.timestamp = '1970-01-01T00:00:18.000000'- events.append(event5)-- # Third running period: 3 seconds- event6 = Mock(spec=AgentStateChangedObservation)- event6.agent_state = AgentState.RUNNING- event6.timestamp = '1970-01-01T00:00:20.000000'- events.append(event6)-- event7 = Mock(spec=AgentStateChangedObservation)- event7.agent_state = AgentState.AWAITING_USER_CONFIRMATION- event7.timestamp = '1970-01-01T00:00:23.000000'- events.append(event7)-- event8 = Mock(spec=AgentStateChangedObservation)- event8.agent_state = AgentState.USER_CONFIRMED- event8.timestamp = '1970-01-01T00:00:25.000000'- events.append(event8)-- # Fourth running period: 7 seconds- event9 = Mock(spec=AgentStateChangedObservation)- event9.agent_state = AgentState.RUNNING- event9.timestamp = '1970-01-01T00:00:30.000000'- events.append(event9)-- event10 = Mock(spec=AgentStateChangedObservation)- event10.agent_state = AgentState.FINISHED- event10.timestamp = '1970-01-01T00:00:37.000000'- events.append(event10)-- mock_event_store.search_events.return_value = events-- # Call the function under test with mocked session_maker- with patch(- 'server.utils.conversation_callback_utils.session_maker', session_maker- ):- update_active_working_seconds(- mock_event_store, conversation_id, user_id, mock_file_store- )-- # Verify the total running time is calculated correctly- # Running periods: 5 + 8 + 3 + 7 = 23 seconds- with session_maker() as session:- conversation_work = (- session.query(ConversationWork)- .filter(ConversationWork.conversation_id == conversation_id)- .first()- )-- assert conversation_work is not None- assert conversation_work.seconds == 23.0- assert conversation_work.conversation_id == conversation_id- assert conversation_work.user_id == user_id---class TestInvokeConversationCallbacks:- """Tests for invoke_conversation_callbacks function.-- This function uses async database sessions (a_session_maker) to query- and invoke callbacks for a conversation.- """-- @pytest.fixture- def mock_observation(self):- """Create a mock AgentStateChangedObservation."""-- observation = Mock(spec=AgentStateChangedObservation)- observation.agent_state = AgentState.FINISHED- return observation-- @pytest.fixture- def create_mock_async_session(self):- """Factory to create properly mocked async session context manager."""- from contextlib import asynccontextmanager- from unittest.mock import AsyncMock-- def _create(callbacks_list):- mock_session = Mock()- mock_result = Mock()- mock_result.scalars.return_value.all.return_value = callbacks_list- mock_session.execute = AsyncMock(return_value=mock_result)- mock_session.commit = AsyncMock(return_value=None)-- @asynccontextmanager- async def mock_context_manager():- yield mock_session-- return mock_context_manager, mock_session-- return _create-- @pytest.mark.asyncio- async def test_invoke_callbacks_with_active_callbacks(- self, mock_observation, create_mock_async_session- ):- """Test that active callbacks are invoked successfully."""- from unittest.mock import AsyncMock-- # Arrange- conversation_id = 'test_conversation_callbacks'- mock_processor = AsyncMock(return_value=None)-- # Create a mock callback- mock_callback = Mock()- mock_callback.id = 1- mock_callback.processor_type = 'test_processor'- mock_callback.get_processor.return_value = mock_processor-- mock_context_manager, mock_session = create_mock_async_session([mock_callback])-- # Act- with patch(- 'server.utils.conversation_callback_utils.a_session_maker',- mock_context_manager,- ):- from server.utils.conversation_callback_utils import (- invoke_conversation_callbacks,- )-- await invoke_conversation_callbacks(conversation_id, mock_observation)-- # Assert- mock_callback.get_processor.assert_called_once()- mock_processor.assert_called_once_with(mock_callback, mock_observation)-- @pytest.mark.asyncio- async def test_invoke_callbacks_with_no_active_callbacks(- self, mock_observation, create_mock_async_session- ):- """Test behavior when no active callbacks exist."""- # Arrange- conversation_id = 'test_no_callbacks'-- mock_context_manager, mock_session = create_mock_async_session([])-- # Act- with patch(- 'server.utils.conversation_callback_utils.a_session_maker',- mock_context_manager,- ):- from server.utils.conversation_callback_utils import (- invoke_conversation_callbacks,- )-- await invoke_conversation_callbacks(conversation_id, mock_observation)-- # Assert - should complete without errors- mock_session.commit.assert_called_once()-- @pytest.mark.asyncio- async def test_invoke_callbacks_handles_processor_exception(- self, mock_observation, create_mock_async_session- ):- """Test that processor exceptions are caught and callback status is updated."""- from unittest.mock import AsyncMock-- # Arrange- conversation_id = 'test_callback_error'- mock_processor = AsyncMock(side_effect=Exception('Processor error'))-- mock_callback = Mock()- mock_callback.id = 1- mock_callback.processor_type = 'failing_processor'- mock_callback.get_processor.return_value = mock_processor- mock_callback.status = 'active'-- mock_context_manager, mock_session = create_mock_async_session([mock_callback])-- # Act- with patch(- 'server.utils.conversation_callback_utils.a_session_maker',- mock_context_manager,- ), patch('server.utils.conversation_callback_utils.logger') as mock_logger:- from server.utils.conversation_callback_utils import (- invoke_conversation_callbacks,- )- from storage.conversation_callback import CallbackStatus-- await invoke_conversation_callbacks(conversation_id, mock_observation)-- # Assert - callback status should be set to ERROR- assert mock_callback.status == CallbackStatus.ERROR- mock_logger.error.assert_called_once()- error_call = mock_logger.error.call_args- assert error_call[0][0] == 'callback_invocation_failed'diff --git a/enterprise/tests/unit/test_saas_monitoring_listener.py b/enterprise/tests/unit/test_saas_monitoring_listener.pydeleted file mode 100644index fe84f2fd63..0000000000--- a/enterprise/tests/unit/test_saas_monitoring_listener.py+++ /dev/null@@ -1,42 +0,0 @@-import pytest-from server.saas_monitoring_listener import SaaSMonitoringListener--from openhands.core.config.openhands_config import OpenHandsConfig-from openhands.core.schema.agent import AgentState-from openhands.events.event import Event-from openhands.events.observation import (- AgentStateChangedObservation,-)---def listener():- return SaaSMonitoringListener.get_instance(OpenHandsConfig())---def test_on_session_event_with_agent_state_changed_non_error(listener):- event = AgentStateChangedObservation('', AgentState.STOPPED)-- listener.on_session_event(event)---def test_on_session_event_with_agent_state_changed_error(listener):- event = AgentStateChangedObservation('', AgentState.ERROR)-- listener.on_session_event(event)---def test_on_session_event_with_other_event(listener):- listener.on_session_event(Event())---def test_on_agent_session_start_success(listener):- listener.on_agent_session_start(success=True, duration=1.5)---def test_on_agent_session_start_failure(listener):- listener.on_agent_session_start(success=False, duration=0.5)---def test_on_create_conversation(listener):- listener.on_create_conversation()diff --git a/openhands/server/config/server_config.py b/openhands/server/config/server_config.pyindex 2c1d53df90..25caa8d5ee 100644--- a/openhands/server/config/server_config.py+++ b/openhands/server/config/server_config.py@@ -27,7 +27,6 @@ class ServerConfig(ServerConfigInterface):secret_store_class: str = ('openhands.storage.secrets.file_secrets_store.FileSecretsStore')- monitoring_listener_class: str = 'openhands.server.monitoring.MonitoringListener'user_auth_class: str = ('openhands.server.user_auth.default_user_auth.DefaultUserAuth')diff --git a/openhands/server/monitoring.py b/openhands/server/monitoring.pydeleted file mode 100644index cecd1199aa..0000000000--- a/openhands/server/monitoring.py+++ /dev/null@@ -1,49 +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-# This module belongs to the old V0 web server. The V1 application server lives under openhands/app_server/.-from openhands.core.config.openhands_config import OpenHandsConfig-from openhands.events.event import Event---class MonitoringListener:- """Abstract base class for monitoring application activity.-- This is an extension point in OpenHands that allows applications to customize how- application activity is monitored. Applications can substitute their own implementation by:- 1. Creating a class that inherits from MonitoringListener- 2. Implementing desired methods (all methods have default no-op implementations)- 3. Setting server_config.monitoring_listener_class to the fully qualified name of the class-- The class is instantiated via get_impl() in openhands.server.shared.py.-- Implementations should be non-disruptive, do not raise or block to perform I/O.- """-- def on_session_event(self, event: Event) -> None:- """Track metrics about events being added to a Session's EventStream."""- pass-- def on_agent_session_start(self, success: bool, duration: float) -> None:- """Track an agent session start.- Success is true if startup completed without error.- Duration is start time in seconds observed by AgentSession.- """- pass-- def on_create_conversation(self) -> None:- """Track the beginning of conversation creation.- Does not currently capture whether it succeed.- """- pass-- @classmethod- def get_instance(- cls,- config: OpenHandsConfig,- ) -> 'MonitoringListener':- return cls()diff --git a/openhands/server/shared.py b/openhands/server/shared.pyindex 378cba26ce..994c4685f4 100644--- a/openhands/server/shared.py+++ b/openhands/server/shared.py@@ -14,7 +14,6 @@ from dotenv import load_dotenvfrom openhands.core.config import load_openhands_configfrom openhands.core.config.openhands_config import OpenHandsConfigfrom openhands.server.config.server_config import ServerConfig, load_server_config-from openhands.server.monitoring import MonitoringListenerfrom openhands.server.types import ServerConfigInterfacefrom openhands.storage import get_file_storefrom openhands.storage.files import FileStore@@ -55,13 +54,6 @@ sio = socketio.AsyncServer(max_http_buffer_size=4 * 1024 * 1024,)-MonitoringListenerImpl = get_impl(- MonitoringListener,- server_config.monitoring_listener_class,-)--monitoring_listener = MonitoringListenerImpl.get_instance(config)-SettingsStoreImpl = get_impl(SettingsStore, server_config.settings_store_class)SecretsStoreImpl = get_impl(SecretsStore, server_config.secret_store_class)diff --git a/openhands/utils/import_utils.py b/openhands/utils/import_utils.pyindex 0f263d6cb5..7dc25b918f 100644--- a/openhands/utils/import_utils.py+++ b/openhands/utils/import_utils.py@@ -64,7 +64,7 @@ def get_impl(cls: type[T], impl_name: str | None) -> type[T]:>>> CustomStore = get_impl(SettingsStore, 'myapp.CustomSettingsStore')Common Use Cases:- - Server components (UserAuth, MonitoringListener, etc.)+ - Server components (UserAuth, etc.)- Storage implementations (SettingsStore, SecretsStore, etc.)- Service integrations (GitHub, GitLab, Bitbucket, Azure DevOps services)