etha.tensor_bus.bootstrap
=========================

.. py:module:: etha.tensor_bus.bootstrap

.. autoapi-nested-parse::

   Bootstrap utilities.



Attributes
----------

.. autoapisummary::

   etha.tensor_bus.bootstrap.logger


Classes
-------

.. autoapisummary::

   etha.tensor_bus.bootstrap.BootstrapInfo


Functions
---------

.. autoapisummary::

   etha.tensor_bus.bootstrap.bootstrap_client
   etha.tensor_bus.bootstrap.setup_ptrace


Module Contents
---------------

.. py:class:: BootstrapInfo

   Information about the bootstrap process.

   :ivar agent_rank: The agent rank this worker is connected to
   :ivar global_rank: Global rank within the worker process group (from torchrun)
   :ivar rank_offset: Offset used for calculation (if applicable)
   :ivar device: CUDA device string (e.g., "cuda:0")
   :ivar command_queue_path: Path to Agent's CommandQueue LMDB
   :ivar state_path: Path to Agent's State LMDB
   :ivar method: How agent_rank was determined ("direct" or "offset")



   .. py:attribute:: agent_rank
      :type:  int


   .. py:attribute:: command_queue_path
      :type:  str


   .. py:attribute:: global_rank
      :type:  int


   .. py:attribute:: method
      :type:  str


   .. py:attribute:: rank_offset
      :type:  int | None


   .. py:attribute:: state_path
      :type:  str


.. py:function:: bootstrap_client(path_naming_fn: collections.abc.Callable[[int], tuple[str, str]] | None = None, connection_timeout: float = 30.0) -> tuple[etha.tensor_bus.client.TensorBusClient, BootstrapInfo]

   Bootstrap TensorBusClient with automatic agent rank resolution.

   This function encapsulates the entire Worker-side bootstrap process:
   1. Determines agent_rank from environment variables (AGENT_RANK or LOCAL_RANK + OFFSET)
   2. Resolves LMDB paths using naming convention
   3. Creates and returns TensorBusClient

   Environment Variables (priority order):
   1. AGENT_RANK: Direct specification (highest priority)
   2. LOCAL_RANK + AGENT_RANK_OFFSET: Offset-based calculation

   :param path_naming_fn: Optional custom function to get (cmd_queue_path, state_path) from rank.
                          If None, uses default convention:
                          - /tmp/agent_rank{N}_command.lmdb
                          - /tmp/agent_rank{N}_state.lmdb
   :param connection_timeout: Max time to wait for Agent connection (seconds, default 30.0)

   :returns: Tuple of TensorBusClient and BootstrapInfo
   :rtype: (client, info)

   :raises ValueError: If required environment variables are missing
   :raises ConnectionError: If Agent is not found or not responding


.. py:function:: setup_ptrace()

   Setup ptrace authorization for agent process.

   For simplicity, we use PR_SET_PTRACER_ANY in this prototype.
   In production, you'd authorize specific agent PIDs.


.. py:data:: logger

