diff --git a/openhands-agent-server/openhands/agent_server/conversation_router.py b/openhands-agent-server/openhands/agent_server/conversation_router.py index 7bb8082684..117d526ccc 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.message_sender import SenderError from openhands.agent_server.models import ( diff --git a/openhands-agent-server/openhands/agent_server/conversation_service.py b/openhands-agent-server/openhands/agent_server/conversation_service.py index 7f629c08cf..46fb674f86 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_service.py +++ b/openhands-agent-server/openhands/agent_server/conversation_service.py @@ -26,7 +26,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, @@ -95,7 +100,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 @@ -124,13 +128,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, @@ -339,16 +336,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 abcd189f85..bae7e1d165 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 11c8c90f77..c6098be383 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 GatewayTimeout, 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 b650a6704f..ae68647082 100644 --- a/openhands-agent-server/openhands/agent_server/event_service.py +++ b/openhands-agent-server/openhands/agent_server/event_service.py @@ -21,6 +21,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, @@ -75,7 +81,6 @@ HttpVersionedCredentialBinding, VersionedCredentialBinding, ) -from openhands.sdk.errors import RateLimited from openhands.sdk.event import ( AgentErrorEvent, ObservationBaseEvent, @@ -135,25 +140,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.""" @@ -233,10 +219,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 0000000000..be60fc6bf0 --- /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 88b38600d6..11c43092b4 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 62818fe3cc..4f21c83dbe 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 cb3d90d821..0c0f30208a 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 47a94f436a..d0ce41c159 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 041b0a285c..7e8aec1611 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, @@ -3343,7 +3340,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 b2a7a45f28..809a46313f 100644 --- a/tests/agent_server/test_conversation_service.py +++ b/tests/agent_server/test_conversation_service.py @@ -23,14 +23,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 f3b0e4618e..d7bc87de65 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 56ebcec92a..7f5a303162 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 e5302caced..06a2b496f1 100644 --- a/tests/agent_server/test_event_service.py +++ b/tests/agent_server/test_event_service.py @@ -25,12 +25,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 43e29703f8..2231e9ff80 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 6bea100051..404c9ee27f 100644 --- a/tests/agent_server/test_openai_gateway_docker.py +++ b/tests/agent_server/test_openai_gateway_docker.py @@ -19,7 +19,7 @@ from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry from openhands.agent_server.errors import GatewayTimeout, 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 9fb044cc66..b733c4a106 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.message_sender import SenderError from openhands.agent_server.models import ( diff --git a/tests/agent_server/test_reactions_router.py b/tests/agent_server/test_reactions_router.py index 4db9e63e34..44d65291fd 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 ce06c01e23..90bef7fe58 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 01ef327ec3..c8157dbae8 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