1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! # RelayRL Framework
//!
//! **Version:** 0.5.0-alpha
//! **Status:** Under active development, expect breaking changes
//!
//! RelayRL is a high-performance, multi-actor native reinforcement learning framework designed for
//! concurrent actor execution and efficient trajectory collection. This crate currently provides the core
//! client runtime infrastructure for distributed RL experiments.
//!
//! ## Architecture Overview
//!
//! The framework follows a layered architecture optimized for concurrent multi-actor execution:
//!
//! ```text
//! ┌─────────────────────────────────────────────────┐
//! │ Public API (RelayRLAgent, AgentBuilder) │
//! └─────────────────────────────────────────────────┘
//! │
//! ┌─────────────────────────────────────────────────┐
//! │ Runtime Coordination Layer │
//! │ - ClientCoordinator │
//! │ - ScaleManager (router scaling) │
//! │ - StateManager (actor state) │
//! │ - LifecycleManager (shutdown coordination) │
//! └─────────────────────────────────────────────────┘
//! │
//! ┌─────────────────────────────────────────────────┐
//! │ Message Routing Layer │
//! │ - RouterDispatcher │
//! │ - Router instances (scalable workers) │
//! └─────────────────────────────────────────────────┘
//! │
//! ┌─────────────────────────────────────────────────┐
//! │ Actor Execution Layer │
//! │ - Concurrent Actor instances │
//! │ - Local model inference │
//! │ - Trajectory building │
//! └─────────────────────────────────────────────────┘
//! │
//! ┌─────────────────────────────────────────────────┐
//! │ Data Collection Layer │
//! │ - TrajectoryBuffer (priority scheduling) │
//! │ - Arrow File Sink (available) │
//! │ - Transport Sink (under development) │
//! │ - Database Sink (under development) │
//! └─────────────────────────────────────────────────┘
//! ```
//!
//! ## Module Structure
//!
//! - **[`network::client`]**: Multi-actor client runtime (complete rewrite in v0.5.0)
//! - [`agent`](network::client::agent): Public API for agent construction and interaction
//! - `runtime`: Internal runtime components
//! - `actor`: Individual actor implementations with local inference
//! - `coordination`: Lifecycle, scaling, metrics, and state management
//! - `router`: Message routing between actors and data sinks
//! - `data`: Transport and database layers (under development)
//!
//! - **[`network::server`]**: Training and inference server implementations (optional features)
//!
//! - **[`templates`]**: Environment trait definitions for training and testing
//!
//! - **[`utilities`]**: Configuration loading, logging, metrics, and system utilities
//!
//! ## Current Status
//!
//! ### Available
//! - Multi-actor client runtime with concurrent execution
//! - Local Arrow file sink for trajectory data
//! - Builder pattern API for ergonomic agent construction
//! - Router-based message dispatching with scaling support
//! - Actor lifecycle management (create, remove, scale)
//!
//! ### Under Development
//! - Network transport layer (ZMQ)
//! - Database trajectory sinks (PostgreSQL/SQLite)
//! - Server-side inference mode
//! - Training server integration
//!
//! ### Not In This Crate
//! - **Python Bindings**: See `relayrl_python` crate
//! - **Algorithms**: See `relayrl_algorithms` crate
//! - **Type Definitions**: See `relayrl_types` crate
//!
//! ## Quick Example
//!
//! ```rust,no_run
//! use relayrl_framework::prelude::network::*;
//! use relayrl_types::data::tensor::DeviceType;
//! use burn_ndarray::NdArray;
//! use burn_tensor::{Tensor, Float};
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Build agent with 4 concurrent actors
//! let (agent, params) = AgentBuilder::<NdArray, 4, 2, Float, Float>::builder()
//! .actor_count(4)
//! .router_scale(2)
//! .default_device(DeviceType::Cpu)
//! .config_path(PathBuf::from("client_config.json"))
//! .build()
//! .await?;
//!
//! // Start runtime
//! agent.start(
//! params.actor_count,
//! params.router_scale,
//! params.default_device,
//! params.default_model,
//! params.config_path,
//! ).await?;
//!
//! // Request actions from actors
//! let observation = Tensor::<NdArray, 2, Float>::zeros([1, 4], &Default::default());
//! let actions = agent.request_action(
//! vec![/* actor IDs */],
//! observation,
//! None,
//! 0.0
//! ).await?;
//!
//! // Shutdown gracefully
//! agent.shutdown().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Feature Flags
//!
//! - `client` (default): Core client runtime
//! - `network`: Full network stack (client + servers)
//! - `transport_layer`: Network transport (ZMQ)
//! - `database_layer`: Database support (PostgreSQL, SQLite)
//! - `logging`: Log4rs logging
//! - `metrics`: Prometheus/OpenTelemetry metrics
//! - `profile`: Flamegraph and tokio-console profiling
/// Core networking functionality for RelayRL.
///
/// This module provides the multi-actor client runtime and optional server implementations.
///
/// ## Client Runtime
///
/// The [`client`](network::client) module contains the complete rewrite (v0.5.0) of the
/// multi-actor client runtime, including:
/// - Public [`agent`](network::client::agent) API for agent construction and control
/// - Internal runtime coordination (scaling, lifecycle, state management)
/// - Router-based message dispatching
/// - Actor execution with local inference
/// - Data collection via Arrow file sink (transport/database under development)
///
/// ## Server Components (Optional)
///
/// The [`server`](network::server) module provides training and inference server implementations,
/// available via feature flags (`training_server`, `inference_server`).
/// Environment trait definitions for RL training and testing.
///
/// This module provides:
/// - [`EnvironmentTrainingTrait`](templates::environment_traits::EnvironmentTrainingTrait):
/// Interface for training environments with performance metrics
/// - [`EnvironmentTestingTrait`](templates::environment_traits::EnvironmentTestingTrait):
/// Interface for inference/testing environments
/// Configuration, logging, metrics, and system utilities.
///
/// This module contains:
/// - `configuration`: JSON-based configuration loading and builders
/// - `observability`: Logging (log4rs) and metrics (Prometheus/OpenTelemetry) systems
/// - `tokio`: Tokio runtime utilities
/// Prelude module for convenient imports.
///
/// This module re-exports commonly used types and traits for easier access:
///
/// ```rust
/// use relayrl_framework::prelude::network::*; // Agent API
/// use relayrl_framework::prelude::config::*; // Configuration
/// use relayrl_framework::prelude::config::network_codec::*; // Codec types
/// use relayrl_framework::prelude::tensor::burn::*; // Burn tensor types
/// use relayrl_framework::prelude::tensor::relayrl::*; // RelayRL tensor types
/// use relayrl_framework::prelude::action::*; // Action types
/// use relayrl_framework::prelude::trajectory::*; // Trajectory types
/// use relayrl_framework::prelude::model::*; // Model types
/// use relayrl_framework::prelude::templates::*; // Environment types
/// ```