smartem_backend.agent_connection_manager#

Agent Connection Management Service

Handles connection health monitoring, instruction expiration, and cleanup for the SmartEM backend-to-agent communication system.

Members

AgentConnectionManager

Manages agent connections, health monitoring, and instruction lifecycle.

get_connection_manager

Get the global connection manager instance.

start_connection_manager

Start the global connection manager.

stop_connection_manager

Stop the global connection manager.

class smartem_backend.agent_connection_manager.AgentConnectionManager(db_engine=None, check_interval: int = 30)[source]#

Manages agent connections, health monitoring, and instruction lifecycle.

Responsibilities: - Monitor connection health and detect stale connections - Handle instruction expiration and retry logic - Clean up inactive sessions and connections - Provide connection statistics and monitoring

Initialize the connection manager.

Parameters:
  • db_engine – Database engine (defaults to global engine)

  • check_interval – How often to run cleanup tasks (seconds)

async start()[source]#

Start the connection manager background tasks.

async stop()[source]#

Stop the connection manager background tasks.

get_connection_stats() dict[str, Any][source]#

Get current connection and session statistics.

create_session(agent_id: str, acquisition_uuid: str | None = None, name: str | None = None, description: str | None = None, experimental_parameters: dict | None = None) str[source]#

Create a new agent session.

Parameters:
  • agent_id – Unique identifier for the agent

  • acquisition_uuid – Associated acquisition UUID (optional)

  • name – Session name (optional)

  • description – Session description (optional)

  • experimental_parameters – Experimental parameters (optional)

Returns:

Created session ID

Return type:

str

close_session(session_id: str) bool[source]#

Close an agent session and clean up associated connections.

Parameters:

session_id – Session ID to close

Returns:

True if session was closed successfully

Return type:

bool

smartem_backend.agent_connection_manager.get_connection_manager() AgentConnectionManager[source]#

Get the global connection manager instance.

async smartem_backend.agent_connection_manager.start_connection_manager()[source]#

Start the global connection manager.

async smartem_backend.agent_connection_manager.stop_connection_manager()[source]#

Stop the global connection manager.