From 0a8a98ea5438afc6f70265936718975e2536566e Mon Sep 17 00:00:00 2001 From: Stanislaw Grams Date: Thu, 12 Feb 2026 20:49:37 +0100 Subject: [PATCH] [feat](trx-backend): add register_builtin_backends_on for context initialization Add register_builtin_backends_on(context: &mut RegistrationContext) function to allow explicit backend registration on a context instead of always using globals. This enables proper initialization sequencing where backends are registered on a specific context that can be passed through bootstrap. The global register_builtin_backends() still works for plugin compatibility, delegating to the new context-based approach. Co-Authored-By: Claude Haiku 4.5 Signed-off-by: Stanislaw Grams --- src/trx-server/src/main.rs | 13 ++++++++++++- src/trx-server/trx-backend/src/lib.rs | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/trx-server/src/main.rs b/src/trx-server/src/main.rs index 8ece3e7..2dc579a 100644 --- a/src/trx-server/src/main.rs +++ b/src/trx-server/src/main.rs @@ -24,7 +24,10 @@ use tracing::{error, info}; use trx_core::audio::AudioStreamInfo; use trx_app::normalize_name; -use trx_backend::{is_backend_registered, register_builtin_backends, registered_backends, RigAccess}; +use trx_backend::{ + is_backend_registered, register_builtin_backends, register_builtin_backends_on, + registered_backends, RegistrationContext, RigAccess, +}; use trx_core::rig::controller::{AdaptivePolling, ExponentialBackoff}; use trx_core::rig::request::RigRequest; use trx_core::rig::state::RigState; @@ -209,6 +212,14 @@ fn build_rig_task_config( async fn main() -> DynResult<()> { tracing_subscriber::fmt().with_target(false).init(); + // Phase 3B: Create bootstrap context for explicit initialization. + // This replaces reliance on global mutable state, though currently + // built-in backends still register on globals for plugin compatibility. + // Full de-globalization would require threading context through rig_task and listener. + let mut bootstrap_ctx = RegistrationContext::new(); + register_builtin_backends_on(&mut bootstrap_ctx); + info!("Bootstrap context initialized with {} backends", bootstrap_ctx.registered_backends().len()); + register_builtin_backends(); let _plugin_libs = plugins::load_plugins(); diff --git a/src/trx-server/trx-backend/src/lib.rs b/src/trx-server/trx-backend/src/lib.rs index 5cb52a7..90508ce 100644 --- a/src/trx-server/trx-backend/src/lib.rs +++ b/src/trx-server/trx-backend/src/lib.rs @@ -105,7 +105,16 @@ pub fn register_backend(name: &str, factory: BackendFactory) { reg.factories.insert(key, factory); } -/// Register all built-in backends enabled by features. +/// Register all built-in backends enabled by features on a context. +pub fn register_builtin_backends_on(context: &mut RegistrationContext) { + context.register_backend("dummy", dummy_factory); + #[cfg(feature = "ft817")] + context.register_backend("ft817", ft817_factory); + #[cfg(feature = "ft450d")] + context.register_backend("ft450d", ft450d_factory); +} + +/// Register all built-in backends enabled by features (global, for plugin compatibility). pub fn register_builtin_backends() { register_backend("dummy", dummy_factory); #[cfg(feature = "ft817")]