From 57e9ab8cde335335d4e1a3a8f7c9fd9ba1a0299d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 23:31:37 +0000 Subject: [PATCH] Move agent-server service exceptions into an exceptions module ConversationService and EventService declared their exceptions inline, so conversation_service.py imported ServiceInactive from event_service.py and routers imported exceptions from the services. They now live in openhands/agent_server/exceptions.py: ServiceInactive, ConversationRunLimitExceeded, CondensationUnavailable, InvalidParentConversation, UnavailableAgentTool, CredentialBindingActivationRequired and CredentialBindingActivationTooLate. Names, bases and messages are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013ArwwLYPA4zfbsUTU75ee4 --- .../agent_server/conversation_router.py | 12 ++--- .../agent_server/conversation_service.py | 23 ++------- .../agent_server/credential_binding.py | 2 +- .../openhands/agent_server/event_router.py | 3 +- .../openhands/agent_server/event_service.py | 30 +++-------- .../openhands/agent_server/exceptions.py | 50 +++++++++++++++++++ .../openhands/agent_server/session_socket.py | 6 +-- .../openhands/agent_server/sockets.py | 6 +-- tests/agent_server/test_ask_user_router.py | 2 +- .../test_conversation_response.py | 3 +- .../agent_server/test_conversation_router.py | 9 ++-- .../agent_server/test_conversation_service.py | 4 +- tests/agent_server/test_credential_binding.py | 10 ++-- tests/agent_server/test_domain_errors.py | 8 +-- tests/agent_server/test_event_service.py | 3 +- tests/agent_server/test_event_socket_races.py | 3 +- .../test_openai_gateway_docker.py | 2 +- .../test_parent_child_conversations.py | 11 ++-- tests/agent_server/test_reactions_router.py | 2 +- tests/agent_server/test_run_admission.py | 2 +- .../test_unhandled_exception_error_id.py | 4 +- 21 files changed, 101 insertions(+), 94 deletions(-) create mode 100644 openhands-agent-server/openhands/agent_server/exceptions.py diff --git a/openhands-agent-server/openhands/agent_server/conversation_router.py b/openhands-agent-server/openhands/agent_server/conversation_router.py index 2289e4b66a7..400268b422d 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_router.py +++ b/openhands-agent-server/openhands/agent_server/conversation_router.py @@ -19,11 +19,7 @@ decrypt_incoming_llm_secrets, ) from openhands.agent_server.conversation_registry import ConversationRegistry -from openhands.agent_server.conversation_service import ( - ConversationService, - InvalidParentConversation, - UnavailableAgentTool, -) +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( get_cipher, get_conversation_registry, @@ -31,7 +27,11 @@ get_llm_profile_store, ) from openhands.agent_server.errors import error_responses -from openhands.agent_server.event_service import CondensationUnavailable +from openhands.agent_server.exceptions import ( + CondensationUnavailable, + InvalidParentConversation, + UnavailableAgentTool, +) from openhands.agent_server.launch import launch_error from openhands.agent_server.models import ( INCLUDE_SKILLS_PARAM_TITLE, diff --git a/openhands-agent-server/openhands/agent_server/conversation_service.py b/openhands-agent-server/openhands/agent_server/conversation_service.py index 6942f67ac2c..7c167a453f0 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_service.py +++ b/openhands-agent-server/openhands/agent_server/conversation_service.py @@ -24,7 +24,12 @@ LEASE_RENEW_INTERVAL_SECONDS, EventService, RunSlot, +) +from openhands.agent_server.exceptions import ( + CredentialBindingActivationRequired, + InvalidParentConversation, ServiceInactive, + UnavailableAgentTool, ) from openhands.agent_server.experimental_features import ( experimental_feature_tools, @@ -91,7 +96,6 @@ CredentialBindingError, VersionedCredentialBinding, ) -from openhands.sdk.errors import Conflict from openhands.sdk.event import MessageEvent from openhands.sdk.event.conversation_state import ConversationStateUpdateEvent from openhands.sdk.git.exceptions import GitCommandError, GitRepositoryError @@ -120,13 +124,6 @@ SERVER_SHUTDOWN_WS_CLOSE = (1012, "Server shutting down") -class CredentialBindingActivationRequired(Conflict, RuntimeError): - """A runtime credential binding must be activated first; retry after it.""" - - def __init__(self, detail: str) -> None: - super().__init__(detail, extra={"retryable": True}) - - def _build_worktree_guidance( *, source_workspace: Path, @@ -335,16 +332,6 @@ def _create_conversation_worktree(plan: _WorktreePlan) -> LocalWorkspace: logger = logging.getLogger(__name__) -class InvalidParentConversation(ValueError): - """``parent_conversation_id`` is unknown, self-referential, or in - a different workspace.""" - - -class UnavailableAgentTool(ValueError): - """The agent names a tool whose ``tool_module_qualnames`` module the server - cannot import, so the agent could never be built.""" - - def _same_workspace(a: LocalWorkspace, b: LocalWorkspace) -> bool: return Path(a.working_dir).resolve() == Path(b.working_dir).resolve() diff --git a/openhands-agent-server/openhands/agent_server/credential_binding.py b/openhands-agent-server/openhands/agent_server/credential_binding.py index abcd189f850..bae7e1d1653 100644 --- a/openhands-agent-server/openhands/agent_server/credential_binding.py +++ b/openhands-agent-server/openhands/agent_server/credential_binding.py @@ -10,7 +10,7 @@ from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_conversation_service -from openhands.agent_server.event_service import CredentialBindingActivationTooLate +from openhands.agent_server.exceptions import CredentialBindingActivationTooLate from openhands.agent_server.persistence import FileSecretsStore, run_store from openhands.sdk.agent.acp_file_credentials import supports_file_credential_binding from openhands.sdk.credential import ( diff --git a/openhands-agent-server/openhands/agent_server/event_router.py b/openhands-agent-server/openhands/agent_server/event_router.py index b14f639e9c4..d1841122af0 100644 --- a/openhands-agent-server/openhands/agent_server/event_router.py +++ b/openhands-agent-server/openhands/agent_server/event_router.py @@ -25,7 +25,8 @@ get_settings_store, ) from openhands.agent_server.errors import error_responses -from openhands.agent_server.event_service import EventService, ServiceInactive +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import ServiceInactive from openhands.agent_server.experimental_features import ( host_experimental_features, is_enabled, diff --git a/openhands-agent-server/openhands/agent_server/event_service.py b/openhands-agent-server/openhands/agent_server/event_service.py index f1a6ef3c0fb..45cc5c62844 100644 --- a/openhands-agent-server/openhands/agent_server/event_service.py +++ b/openhands-agent-server/openhands/agent_server/event_service.py @@ -20,6 +20,12 @@ ConversationLease, ConversationOwnershipLostError, ) +from openhands.agent_server.exceptions import ( + CondensationUnavailable, + ConversationRunLimitExceeded, + CredentialBindingActivationTooLate, + ServiceInactive, +) from openhands.agent_server.managed_llm_key import register_managed_llm_key_refresh from openhands.agent_server.models import ( ConfirmationResponseRequest, @@ -74,7 +80,6 @@ HttpVersionedCredentialBinding, VersionedCredentialBinding, ) -from openhands.sdk.errors import RateLimited from openhands.sdk.event import ( AgentErrorEvent, ObservationBaseEvent, @@ -134,25 +139,6 @@ def _without_skill_bodies( return event -class ConversationRunLimitExceeded(RateLimited, RuntimeError): - """The server has no capacity to start another conversation run.""" - - -class CondensationUnavailable(ValueError): - """The conversation's agent has no condenser that handles requests.""" - - -class ServiceInactive(ValueError): - """The service is not running: not started yet, or closing or closed. - - It stays a ``ValueError`` with the message ``"inactive_service"``, so - routes and clients see the same error as before; callers catch the type. - """ - - def __init__(self) -> None: - super().__init__("inactive_service") - - class _RunPermit: """Ref-counted permit shared by the chained runs of one admission session.""" @@ -232,10 +218,6 @@ def __exit__(self, *_exc) -> None: self.release() -class CredentialBindingActivationTooLate(RuntimeError): - pass - - def _log_emit_failure(event_kind: str, future: "asyncio.Future[None]") -> None: """Log an event emission that failed inside the executor thread.""" if future.cancelled(): diff --git a/openhands-agent-server/openhands/agent_server/exceptions.py b/openhands-agent-server/openhands/agent_server/exceptions.py new file mode 100644 index 00000000000..be60fc6bf0c --- /dev/null +++ b/openhands-agent-server/openhands/agent_server/exceptions.py @@ -0,0 +1,50 @@ +"""Exceptions the agent server's conversation and event services raise. + +Each one names a refusal or a state that callers handle by type. The ones that +subclass an ``openhands.sdk.errors`` category get their HTTP status from +``errors.py``; the ``ValueError`` ones keep the messages they always had, and +routes map them where they are caught. +""" + +from openhands.sdk.errors import Conflict, RateLimited + + +class ServiceInactive(ValueError): + """The service is not running: not started yet, or closing or closed. + + It stays a ``ValueError`` with the message ``"inactive_service"``, so + routes and clients see the same error as before; callers catch the type. + """ + + def __init__(self) -> None: + super().__init__("inactive_service") + + +class ConversationRunLimitExceeded(RateLimited, RuntimeError): + """The server has no capacity to start another conversation run.""" + + +class CondensationUnavailable(ValueError): + """The conversation's agent has no condenser that handles requests.""" + + +class InvalidParentConversation(ValueError): + """``parent_conversation_id`` is unknown, self-referential, or in + a different workspace.""" + + +class UnavailableAgentTool(ValueError): + """The agent names a tool whose ``tool_module_qualnames`` module the server + cannot import, so the agent could never be built.""" + + +class CredentialBindingActivationRequired(Conflict, RuntimeError): + """A runtime credential binding must be activated first; retry after it.""" + + def __init__(self, detail: str) -> None: + super().__init__(detail, extra={"retryable": True}) + + +class CredentialBindingActivationTooLate(RuntimeError): + """The conversation can no longer take this credential binding in place: + it already has a different one, is not running, or is not an ACP agent.""" diff --git a/openhands-agent-server/openhands/agent_server/session_socket.py b/openhands-agent-server/openhands/agent_server/session_socket.py index 88b38600d6a..11c43092b44 100644 --- a/openhands-agent-server/openhands/agent_server/session_socket.py +++ b/openhands-agent-server/openhands/agent_server/session_socket.py @@ -20,11 +20,9 @@ from openhands.agent_server import sockets from openhands.agent_server.config import Config -from openhands.agent_server.conversation_service import ( - ConversationService, - CredentialBindingActivationRequired, -) +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_config, get_conversation_service +from openhands.agent_server.exceptions import CredentialBindingActivationRequired from openhands.agent_server.pub_sub import MaxSubscribersError, Subscriber from openhands.agent_server.session_protocol import ( MAX_FRAME_BYTES, diff --git a/openhands-agent-server/openhands/agent_server/sockets.py b/openhands-agent-server/openhands/agent_server/sockets.py index 62818fe3ccc..4f21c83dbe0 100644 --- a/openhands-agent-server/openhands/agent_server/sockets.py +++ b/openhands-agent-server/openhands/agent_server/sockets.py @@ -31,16 +31,14 @@ from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import Config -from openhands.agent_server.conversation_service import ( - ConversationService, - CredentialBindingActivationRequired, -) +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( get_config, get_conversation_service, get_server_bash_event_service, ) from openhands.agent_server.event_router import normalize_datetime_to_server_timezone +from openhands.agent_server.exceptions import CredentialBindingActivationRequired from openhands.agent_server.models import ( BashError, BashEventBase, diff --git a/tests/agent_server/test_ask_user_router.py b/tests/agent_server/test_ask_user_router.py index cb3d90d821b..0c0f30208ac 100644 --- a/tests/agent_server/test_ask_user_router.py +++ b/tests/agent_server/test_ask_user_router.py @@ -21,7 +21,7 @@ get_settings_store, ) from openhands.agent_server.event_router import event_router -from openhands.agent_server.event_service import ServiceInactive +from openhands.agent_server.exceptions import ServiceInactive from openhands.agent_server.models import StartConversationRequest from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import LLM, Agent diff --git a/tests/agent_server/test_conversation_response.py b/tests/agent_server/test_conversation_response.py index 47a94f436ac..d0ce41c159b 100644 --- a/tests/agent_server/test_conversation_response.py +++ b/tests/agent_server/test_conversation_response.py @@ -14,7 +14,8 @@ from openhands.agent_server.conversation_router import conversation_router from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_conversation_service -from openhands.agent_server.event_service import EventService, ServiceInactive +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import ServiceInactive from openhands.sdk import LLM, Agent, Conversation, LocalConversation, Message from openhands.sdk.event import ActionEvent, MessageEvent from openhands.sdk.llm import MessageToolCall, TextContent diff --git a/tests/agent_server/test_conversation_router.py b/tests/agent_server/test_conversation_router.py index 9823cec7462..cc5fd7a5ccc 100644 --- a/tests/agent_server/test_conversation_router.py +++ b/tests/agent_server/test_conversation_router.py @@ -15,11 +15,8 @@ from openhands.agent_server.conversation_router import conversation_router from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_conversation_service -from openhands.agent_server.event_service import ( - CondensationUnavailable, - EventService, - ServiceInactive, -) +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import CondensationUnavailable, ServiceInactive from openhands.agent_server.launch import launch_source from openhands.agent_server.models import ( ConversationInfo, @@ -3317,7 +3314,7 @@ def test_start_conversation_unavailable_agent_tool_returns_422( client, mock_conversation_service ): """An agent tool whose module the server cannot import yields 422, not 500.""" - from openhands.agent_server.conversation_service import UnavailableAgentTool + from openhands.agent_server.exceptions import UnavailableAgentTool mock_conversation_service.start_conversation.side_effect = UnavailableAgentTool( "The agent uses tools whose modules the agent server cannot import: " diff --git a/tests/agent_server/test_conversation_service.py b/tests/agent_server/test_conversation_service.py index 38daad6532a..db04a35b54c 100644 --- a/tests/agent_server/test_conversation_service.py +++ b/tests/agent_server/test_conversation_service.py @@ -21,14 +21,14 @@ from openhands.agent_server.conversation_service import ( AutoTitleSubscriber, ConversationService, - UnavailableAgentTool, _compose_conversation_info, _ConversationRecord, _create_conversation_worktree, _get_worktree_start_point, _plan_conversation_worktree, ) -from openhands.agent_server.event_service import EventService, ServiceInactive +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import ServiceInactive, UnavailableAgentTool from openhands.agent_server.models import ( ConversationInfo, ConversationPage, diff --git a/tests/agent_server/test_credential_binding.py b/tests/agent_server/test_credential_binding.py index f3b0e4618ec..d7bc87de659 100644 --- a/tests/agent_server/test_credential_binding.py +++ b/tests/agent_server/test_credential_binding.py @@ -15,17 +15,15 @@ import openhands.agent_server.event_service as event_service_module from openhands.agent_server.api import add_domain_error_handler -from openhands.agent_server.conversation_service import ( - ConversationService, - CredentialBindingActivationRequired, -) +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.credential_binding import ( LocalVersionedCredentialBinding, router, ) -from openhands.agent_server.event_service import ( +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import ( + CredentialBindingActivationRequired, CredentialBindingActivationTooLate, - EventService, ) from openhands.agent_server.models import StartConversationRequest, StoredConversation from openhands.agent_server.persistence import ( diff --git a/tests/agent_server/test_domain_errors.py b/tests/agent_server/test_domain_errors.py index 56ebcec92aa..7f5a3031629 100644 --- a/tests/agent_server/test_domain_errors.py +++ b/tests/agent_server/test_domain_errors.py @@ -9,9 +9,6 @@ from openhands.agent_server.api import _add_exception_handlers from openhands.agent_server.conversation_lease import ConversationOwnershipLostError -from openhands.agent_server.conversation_service import ( - CredentialBindingActivationRequired, -) from openhands.agent_server.errors import ( ERROR_STATUS, ForwardedStatus, @@ -19,7 +16,10 @@ error_responses, status_for, ) -from openhands.agent_server.event_service import ConversationRunLimitExceeded +from openhands.agent_server.exceptions import ( + ConversationRunLimitExceeded, + CredentialBindingActivationRequired, +) from openhands.sdk.errors import ( Busy, Conflict, diff --git a/tests/agent_server/test_event_service.py b/tests/agent_server/test_event_service.py index b01fab373b0..d3be686b640 100644 --- a/tests/agent_server/test_event_service.py +++ b/tests/agent_server/test_event_service.py @@ -24,12 +24,11 @@ from openhands.agent_server.errors import status_for from openhands.agent_server.event_router import run_gateway_message from openhands.agent_server.event_service import ( - CondensationUnavailable, EventService, RunSlot, - ServiceInactive, _log_emit_failure, ) +from openhands.agent_server.exceptions import CondensationUnavailable, ServiceInactive from openhands.agent_server.file_router import _create_zip_from_directory from openhands.agent_server.models import ( ConfirmationResponseRequest, diff --git a/tests/agent_server/test_event_socket_races.py b/tests/agent_server/test_event_socket_races.py index 43e29703f80..2231e9ff80d 100644 --- a/tests/agent_server/test_event_socket_races.py +++ b/tests/agent_server/test_event_socket_races.py @@ -13,7 +13,8 @@ from openhands.agent_server import session_socket, sockets from openhands.agent_server.config import Config -from openhands.agent_server.event_service import EventService, ServiceInactive +from openhands.agent_server.event_service import EventService +from openhands.agent_server.exceptions import ServiceInactive from openhands.agent_server.models import StoredConversation from openhands.agent_server.pub_sub import PubSub, Subscriber from openhands.agent_server.server_context import build_server_context diff --git a/tests/agent_server/test_openai_gateway_docker.py b/tests/agent_server/test_openai_gateway_docker.py index ad3ee21474e..8403a3e520c 100644 --- a/tests/agent_server/test_openai_gateway_docker.py +++ b/tests/agent_server/test_openai_gateway_docker.py @@ -18,7 +18,7 @@ from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry from openhands.agent_server.errors import status_for from openhands.agent_server.event_router import run_gateway_message -from openhands.agent_server.event_service import ConversationRunLimitExceeded +from openhands.agent_server.exceptions import ConversationRunLimitExceeded from openhands.agent_server.openai import service as gateway from openhands.agent_server.persistence import ServerStores from openhands.sdk.conversation.conversation_stats import ConversationStats diff --git a/tests/agent_server/test_parent_child_conversations.py b/tests/agent_server/test_parent_child_conversations.py index a856e31a2da..3af4f26040e 100644 --- a/tests/agent_server/test_parent_child_conversations.py +++ b/tests/agent_server/test_parent_child_conversations.py @@ -13,15 +13,12 @@ from openhands.agent_server.api import add_domain_error_handler from openhands.agent_server.conversation_router import conversation_router -from openhands.agent_server.conversation_service import ( - ConversationService, - InvalidParentConversation, -) +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_conversation_service -from openhands.agent_server.event_service import ( +from openhands.agent_server.event_service import EventService, RunSlot +from openhands.agent_server.exceptions import ( ConversationRunLimitExceeded, - EventService, - RunSlot, + InvalidParentConversation, ) from openhands.agent_server.models import ( ConversationInfo, diff --git a/tests/agent_server/test_reactions_router.py b/tests/agent_server/test_reactions_router.py index 4db9e63e340..44d65291fd9 100644 --- a/tests/agent_server/test_reactions_router.py +++ b/tests/agent_server/test_reactions_router.py @@ -15,7 +15,7 @@ get_settings_store, ) from openhands.agent_server.event_router import event_router -from openhands.agent_server.event_service import ServiceInactive +from openhands.agent_server.exceptions import ServiceInactive from openhands.agent_server.experimental_features import ( RUNTIME_EXPERIMENTAL_FEATURES_ENV, ) diff --git a/tests/agent_server/test_run_admission.py b/tests/agent_server/test_run_admission.py index ce06c01e233..90bef7fe58e 100644 --- a/tests/agent_server/test_run_admission.py +++ b/tests/agent_server/test_run_admission.py @@ -11,7 +11,7 @@ from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService -from openhands.agent_server.event_service import ConversationRunLimitExceeded +from openhands.agent_server.exceptions import ConversationRunLimitExceeded from openhands.agent_server.openai.service import _run_agent from openhands.sdk import LLM, Agent, Message, TextContent from openhands.sdk.conversation.request import StartConversationRequest diff --git a/tests/agent_server/test_unhandled_exception_error_id.py b/tests/agent_server/test_unhandled_exception_error_id.py index 01ef327ec3a..c8157dbae81 100644 --- a/tests/agent_server/test_unhandled_exception_error_id.py +++ b/tests/agent_server/test_unhandled_exception_error_id.py @@ -11,9 +11,7 @@ from openhands.agent_server.api import _add_exception_handlers from openhands.agent_server.conversation_lease import ConversationOwnershipLostError -from openhands.agent_server.conversation_service import ( - CredentialBindingActivationRequired, -) +from openhands.agent_server.exceptions import CredentialBindingActivationRequired @pytest.fixture