Skip to main content

myko_server/
lib.rs

1//! Myko server runtime — WebSocket, durable event backends, peer federation.
2//!
3//! This crate contains the tokio-dependent parts of the Myko server:
4//! - `CellServer` — server lifecycle (durable catch-up init, WS accept loop)
5//! - `postgres` — PostgreSQL producer/consumer (event-table + LISTEN/NOTIFY)
6//! - `ws_handler` — WebSocket connection handling
7//! - `peer_registry` — federation with other servers
8//! - `mcp` — Model Context Protocol server
9//!
10//! Tokio-free server types (CellServerCtx, HandlerRegistry, etc.) live in `myko::server`.
11
12pub mod mcp;
13pub mod peer_persister;
14pub mod peer_registry;
15pub mod postgres;
16pub mod router;
17pub mod server_ownership;
18pub mod telemetry;
19pub mod ws_handler;
20pub mod ws_timing;
21
22// Re-export all tokio-free server types from myko
23use std::{
24    collections::HashMap,
25    net::SocketAddr,
26    sync::{
27        Arc, RwLock,
28        atomic::{AtomicBool, Ordering},
29    },
30    time::Duration,
31};
32
33use futures_util::StreamExt;
34pub use myko::server::*;
35use myko::{
36    client::MykoClient, command::CommandContext, request::RequestContext, saga::SagaRegistration,
37    search::SearchIndex, store::StoreRegistry, wire::MEvent,
38};
39pub use peer_persister::PeerPersister;
40pub use server_ownership::ServerOwnershipManager;
41use uuid::Uuid;
42
43use crate::postgres::{
44    CellPostgresConsumer, CellPostgresProducer, PostgresConfig, PostgresHistoryReplayProvider,
45    PostgresHistoryStore, PostgresProducerHandle,
46};
47
48/// Cell-based Myko server configuration.
49#[derive(Clone)]
50pub struct CellServerConfig {
51    /// Address to bind the WebSocket server
52    pub bind_addr: SocketAddr,
53    /// Disable Nagle's algorithm (set `TCP_NODELAY`) on accepted connections.
54    /// Myko's traffic is small, frequent, latency-sensitive messages (e.g.
55    /// ~60Hz pulses); with Nagle on, TCP coalesces successive small writes
56    /// into fewer segments that arrive together, so an even send cadence is
57    /// delivered as bursts. Defaults to `true`.
58    pub tcp_nodelay: bool,
59    /// Optional Postgres configuration for event persistence/distribution
60    pub postgres: Option<PostgresConfig>,
61    /// Server host ID (auto-generated if not provided)
62    pub host_id: Option<Uuid>,
63    /// Optional peer registry configuration for federation
64    pub peer_registry: Option<peer_registry::PeerRegistryConfig>,
65    /// Default persister override
66    pub default_persister: Option<Arc<dyn Persister>>,
67    /// Per-entity persister overrides keyed by entity type name
68    pub persister_overrides: HashMap<String, Arc<dyn Persister>>,
69    /// Optional pre-constructed peer-client map. When provided, it will be
70    /// used as-is (so any `PeerPersister` built against the same `Arc`
71    /// shares the live map). If `None`, the server creates its own.
72    pub peer_clients: Option<Arc<dashmap::DashMap<Arc<str>, Arc<MykoClient>>>>,
73}
74
75/// Builder for creating a CellServer.
76#[derive(Default)]
77pub struct CellServerBuilder {
78    bind_addr: Option<SocketAddr>,
79    tcp_nodelay: Option<bool>,
80    host_id: Option<Uuid>,
81    postgres: Option<PostgresConfig>,
82    peer_registry: Option<peer_registry::PeerRegistryConfig>,
83    default_persister: Option<Arc<dyn Persister>>,
84    persister_overrides: HashMap<String, Arc<dyn Persister>>,
85    /// Optional pre-constructed peer-client map — useful when a
86    /// `PeerPersister` must reference the same map the server will use.
87    /// Defaults to a fresh empty map if not provided.
88    peer_clients: Option<Arc<dashmap::DashMap<Arc<str>, Arc<MykoClient>>>>,
89    after_init: Option<AfterInitCallback>,
90    /// Optional MCP `ServerInfo`. Defaults to `ServerInfo::default()` if not
91    /// set; binaries override this to advertise their own name / version /
92    /// instructions on the `/myko/mcp` endpoint.
93    server_info: Option<mcp::dispatch::ServerInfo>,
94}
95
96type AfterInitCallback = Box<dyn FnOnce(&CellServer) + Send>;
97
98impl CellServerBuilder {
99    /// Create a new server builder.
100    pub fn new() -> Self {
101        Self::default()
102    }
103
104    /// Set the WebSocket bind address.
105    pub fn with_bind_addr(mut self, addr: SocketAddr) -> Self {
106        self.bind_addr = Some(addr);
107        self
108    }
109
110    /// Set whether to disable Nagle's algorithm (`TCP_NODELAY`) on accepted
111    /// connections. Defaults to `true` (Nagle off) — recommended for myko's
112    /// small, frequent, latency-sensitive messages so an even send cadence
113    /// isn't delivered as coalesced bursts.
114    pub fn with_tcp_nodelay(mut self, enabled: bool) -> Self {
115        self.tcp_nodelay = Some(enabled);
116        self
117    }
118
119    /// Set the server host ID (auto-generated if not set).
120    pub fn with_host_id(mut self, id: Uuid) -> Self {
121        self.host_id = Some(id);
122        self
123    }
124
125    /// Configure Postgres for event persistence/distribution.
126    pub fn with_postgres(mut self, config: PostgresConfig) -> Self {
127        self.postgres = Some(config);
128        self
129    }
130
131    /// Configure peer registry for federation.
132    pub fn with_peer_registry(mut self, config: peer_registry::PeerRegistryConfig) -> Self {
133        self.peer_registry = Some(config);
134        self
135    }
136
137    /// Set the default persister used for all entity types without explicit overrides.
138    pub fn with_default_persister(mut self, persister: Arc<dyn Persister>) -> Self {
139        self.default_persister = Some(persister);
140        self
141    }
142
143    /// Override persister for a specific entity type (e.g. "Pulse").
144    pub fn with_persister_override(
145        mut self,
146        entity_type: impl Into<String>,
147        persister: Arc<dyn Persister>,
148    ) -> Self {
149        self.persister_overrides
150            .insert(entity_type.into(), persister);
151        self
152    }
153
154    /// Provide a pre-constructed peer-client map. The server's peer
155    /// registry will populate it as peers connect. Pass the same `Arc`
156    /// into `PeerPersister::new(...)` when you register a
157    /// `with_persister_override(..., PeerPersister)` so the persister
158    /// shares the live map.
159    pub fn with_peer_clients(
160        mut self,
161        peer_clients: Arc<dashmap::DashMap<Arc<str>, Arc<MykoClient>>>,
162    ) -> Self {
163        self.peer_clients = Some(peer_clients);
164        self
165    }
166
167    /// Register a callback to run after initialization and relation establishment,
168    /// but before the WebSocket accept loop starts. Use this for starting subsystems
169    /// that need entity data (e.g., scene engine).
170    pub fn after_init(mut self, f: impl FnOnce(&CellServer) + Send + 'static) -> Self {
171        self.after_init = Some(Box::new(f));
172        self
173    }
174
175    /// Set the MCP `ServerInfo` advertised on the `/myko/mcp` `initialize`
176    /// response. Defaults to `ServerInfo::default()` (`myko-mcp` /
177    /// `CARGO_PKG_VERSION` / no instructions).
178    pub fn with_server_info(mut self, info: mcp::dispatch::ServerInfo) -> Self {
179        self.server_info = Some(info);
180        self
181    }
182
183    /// Build the server.
184    pub fn build(self) -> CellServer {
185        let bind_addr = self
186            .bind_addr
187            .unwrap_or_else(|| "127.0.0.1:5155".parse().unwrap());
188
189        let server_info = Arc::new(self.server_info.unwrap_or_default());
190
191        let mut server = CellServer::new(CellServerConfig {
192            bind_addr,
193            tcp_nodelay: self.tcp_nodelay.unwrap_or(true),
194            postgres: self.postgres,
195            host_id: self.host_id,
196            peer_registry: self.peer_registry,
197            default_persister: self.default_persister,
198            persister_overrides: self.persister_overrides,
199            peer_clients: self.peer_clients,
200        });
201        server.after_init = std::sync::Mutex::new(self.after_init);
202        server.server_info = server_info;
203        server
204    }
205}
206
207/// Cell-based Myko server.
208///
209/// Uses hyphae cells for reactive queries and reports instead of actors.
210pub struct CellServer {
211    /// Central entity store registry
212    pub registry: Arc<StoreRegistry>,
213    /// Handler registry for items, queries, and reports
214    pub handler_registry: Arc<HandlerRegistry>,
215    /// Relationship manager for cascade operations
216    pub relationship_manager: Arc<RelationshipManager>,
217    /// Optional Postgres producer handle
218    pub postgres_producer: Option<PostgresProducerHandle>,
219    /// Full-text search index
220    pub search_index: Arc<SearchIndex>,
221    /// Persister routing (default + per-entity overrides)
222    pub persisters: Arc<PersisterRouter>,
223    /// Server host ID
224    pub host_id: Uuid,
225    /// Server configuration
226    config: CellServerConfig,
227    /// Postgres producer (kept alive)
228    _postgres_producer_owner: Option<CellPostgresProducer>,
229    /// Postgres consumer (kept alive)
230    postgres_consumer: Option<CellPostgresConsumer>,
231    /// Whether the server is ready to accept connections
232    ready: Arc<AtomicBool>,
233    /// Peer registry for federation (initialized after catch-up)
234    peer_registry_instance: RwLock<Option<peer_registry::PeerRegistry>>,
235    /// Live peer clients shared with report context.
236    peer_clients: Arc<dashmap::DashMap<Arc<str>, Arc<MykoClient>>>,
237    /// Callback to run after init (catch-up + relations) but before WS loop
238    after_init: std::sync::Mutex<Option<AfterInitCallback>>,
239    /// MCP `ServerInfo` advertised on the `/myko/mcp` `initialize` response.
240    /// Set via [`CellServerBuilder::with_server_info`]; defaults to
241    /// `ServerInfo::default()`.
242    server_info: Arc<mcp::dispatch::ServerInfo>,
243    /// Sender for local+replicated event fan-out to saga runtime.
244    saga_event_tx: flume::Sender<MEvent>,
245    /// Receiver consumed when saga runtime starts.
246    saga_event_rx: std::sync::Mutex<Option<flume::Receiver<MEvent>>>,
247    /// Saga tasks kept alive for server lifetime.
248    saga_tasks: std::sync::Mutex<Vec<tokio::task::JoinHandle<()>>>,
249    /// Server ownership death-watch guard (kept alive for server lifetime).
250    _server_ownership_guard: std::sync::Mutex<Option<hyphae::SubscriptionGuard>>,
251}
252
253impl CellServer {
254    /// Create a new server builder.
255    pub fn builder() -> CellServerBuilder {
256        CellServerBuilder::new()
257    }
258
259    /// Create a new cell-based server.
260    pub fn new(config: CellServerConfig) -> Self {
261        let host_id = config.host_id.unwrap_or_else(Uuid::new_v4);
262        let registry = Arc::new(StoreRegistry::new());
263        let handler_registry = Arc::new(HandlerRegistry::new());
264        let relationship_manager = Arc::new(RelationshipManager::new());
265
266        // Initialize the client registry for WebSocket client message dispatch
267        init_client_registry();
268
269        let (saga_event_tx, saga_event_rx) = flume::unbounded::<MEvent>();
270        let (postgres_producer_owner, postgres_producer, postgres_consumer) =
271            if let Some(ref postgres_config) = config.postgres {
272                match CellPostgresProducer::new(postgres_config, host_id) {
273                    Ok(producer) => {
274                        let handle = producer.handle();
275                        let consumer = match CellPostgresConsumer::start(
276                            postgres_config,
277                            host_id,
278                            handler_registry.clone(),
279                            registry.clone(),
280                        ) {
281                            Ok(c) => Some(c),
282                            Err(e) => {
283                                tracing::error!("Failed to start Postgres consumer: {}", e);
284                                None
285                            }
286                        };
287                        (Some(producer), Some(handle), consumer)
288                    }
289                    Err(e) => {
290                        tracing::error!("Failed to create Postgres producer: {}", e);
291                        (None, None, None)
292                    }
293                }
294            } else {
295                (None, None, None)
296            };
297
298        // If no durable consumer, server is immediately ready
299        let ready = Arc::new(AtomicBool::new(postgres_consumer.is_none()));
300
301        // Initialize full-text search index
302        let search_index = Arc::new(SearchIndex::new());
303
304        // Build persister routing:
305        // - explicit default from config if provided
306        // - otherwise Postgres producer handle when available
307        // - explicit per-entity overrides always win
308        let mut persister_router = PersisterRouter::default();
309        if let Some(default_persister) = config.default_persister.clone() {
310            persister_router.set_default(Some(default_persister));
311        } else if let Some(handle) = postgres_producer.clone() {
312            persister_router.set_default(Some(Arc::new(handle) as Arc<dyn Persister>));
313        }
314        for (entity_type, persister) in &config.persister_overrides {
315            persister_router.set_override(entity_type.clone(), persister.clone());
316        }
317        let persisters = Arc::new(persister_router);
318
319        let peer_clients = config
320            .peer_clients
321            .clone()
322            .unwrap_or_else(|| Arc::new(dashmap::DashMap::new()));
323
324        Self {
325            registry,
326            handler_registry,
327            relationship_manager,
328            postgres_producer,
329            search_index,
330            persisters,
331            host_id,
332            config,
333            _postgres_producer_owner: postgres_producer_owner,
334            postgres_consumer,
335            ready,
336            peer_registry_instance: RwLock::new(None),
337            peer_clients,
338            after_init: std::sync::Mutex::new(None),
339            server_info: Arc::new(mcp::dispatch::ServerInfo::default()),
340            saga_event_tx,
341            saga_event_rx: std::sync::Mutex::new(Some(saga_event_rx)),
342            saga_tasks: std::sync::Mutex::new(Vec::new()),
343            _server_ownership_guard: std::sync::Mutex::new(None),
344        }
345    }
346
347    /// Start the peer registry for federation.
348    pub fn start_peer_registry(&self, config: Option<peer_registry::PeerRegistryConfig>) {
349        let peer_config = config.or_else(|| self.config.peer_registry.clone());
350
351        if let Some(peer_config) = peer_config {
352            tracing::info!("Starting peer registry");
353            let pr = peer_registry::PeerRegistry::new(self.ctx(), peer_config);
354            *self.peer_registry_instance.write().unwrap() = Some(pr);
355        }
356    }
357
358    /// Check if peer registry is running.
359    pub fn has_peer_registry(&self) -> bool {
360        self.peer_registry_instance.read().unwrap().is_some()
361    }
362
363    /// Get the store registry.
364    pub fn registry(&self) -> Arc<StoreRegistry> {
365        self.registry.clone()
366    }
367
368    /// Get the handler registry.
369    pub fn handler_registry(&self) -> Arc<HandlerRegistry> {
370        self.handler_registry.clone()
371    }
372
373    /// Get the MCP `ServerInfo` advertised on the `/myko/mcp` `initialize`
374    /// response.
375    pub fn server_info(&self) -> Arc<mcp::dispatch::ServerInfo> {
376        self.server_info.clone()
377    }
378
379    /// Get a server context for module use.
380    pub fn ctx(&self) -> CellServerCtx {
381        let history_replay: Option<Arc<dyn myko::server::HistoryReplayProvider>> =
382            self.config.postgres.as_ref().map(|pg| {
383                Arc::new(PostgresHistoryReplayProvider::new(pg.clone()))
384                    as Arc<dyn myko::server::HistoryReplayProvider>
385            });
386        CellServerCtx::new(
387            self.host_id,
388            self.registry.clone(),
389            self.handler_registry.clone(),
390            self.relationship_manager.clone(),
391            self.persisters.clone(),
392            self.search_index.clone(),
393            self.peer_clients.clone(),
394            Some(self.saga_event_tx.clone()),
395            history_replay,
396        )
397    }
398
399    fn start_saga_runtime(&self) {
400        let registrations: Vec<_> = inventory::iter::<SagaRegistration>().collect();
401        if registrations.is_empty() {
402            return;
403        }
404        let Some(rx) = self
405            .saga_event_rx
406            .lock()
407            .expect("saga_event_rx mutex poisoned")
408            .take()
409        else {
410            return;
411        };
412
413        tracing::info!("Starting saga runtime with {} saga(s)", registrations.len());
414
415        // NOTE(ts): One unbounded flume channel per saga, with dispatch-side filtering
416        // so sagas only receive events matching their entity type and change type.
417        struct SagaChannel {
418            tx: flume::Sender<MEvent>,
419            entity_type: &'static str,
420            change_type: myko::event::MEventType,
421        }
422        let mut saga_channels: Vec<SagaChannel> = Vec::new();
423
424        for registration in registrations {
425            let saga = (registration.create)();
426            let saga_name = saga.name().to_string();
427            let (saga_tx, saga_rx) = flume::unbounded::<MEvent>();
428            saga_channels.push(SagaChannel {
429                tx: saga_tx,
430                entity_type: registration.event_entity_type,
431                change_type: registration.event_change_type,
432            });
433            let events: myko::saga::EventStream = Box::pin(futures_util::stream::unfold(
434                saga_rx,
435                move |saga_rx| async move {
436                    saga_rx
437                        .recv_async()
438                        .await
439                        .ok()
440                        .map(|event| (event, saga_rx))
441                },
442            ));
443
444            let saga_ctx = Arc::new(myko::saga::SagaContext::with_event_sink(
445                self.host_id,
446                self.registry.clone(),
447                self.saga_event_tx.clone(),
448            ));
449            let mut command_stream = saga.build_boxed(events, saga_ctx);
450
451            let host_id = self.host_id;
452            let registry = self.registry.clone();
453            let handler_registry = self.handler_registry.clone();
454            let relationship_manager = self.relationship_manager.clone();
455            let persisters = self.persisters.clone();
456            let search_index = self.search_index.clone();
457            let peer_clients = self.peer_clients.clone();
458            let saga_event_tx = self.saga_event_tx.clone();
459
460            let handle = tokio::spawn(async move {
461                while let Some(command) = command_stream.next().await {
462                    let command_name = command.command_name();
463                    tracing::debug!("Saga {} executing command {}", saga_name, command_name);
464                    let req = Arc::new(RequestContext::internal(
465                        Arc::from(Uuid::new_v4().to_string()),
466                        host_id,
467                        &format!("saga:{saga_name}"),
468                    ));
469
470                    let cmd_ctx = CommandContext::new(
471                        Arc::from(command_name),
472                        req,
473                        Arc::new(CellServerCtx::new(
474                            host_id,
475                            registry.clone(),
476                            handler_registry.clone(),
477                            relationship_manager.clone(),
478                            persisters.clone(),
479                            search_index.clone(),
480                            peer_clients.clone(),
481                            Some(saga_event_tx.clone()),
482                            None,
483                        )),
484                    );
485
486                    if let Err(err) = command.execute_boxed(cmd_ctx) {
487                        tracing::error!(
488                            "Saga {} command {} failed: {}",
489                            saga_name,
490                            command_name,
491                            err.message
492                        );
493                    }
494                }
495            });
496
497            self.saga_tasks
498                .lock()
499                .expect("saga_tasks mutex poisoned")
500                .push(handle);
501        }
502
503        // NOTE(ts): Dispatcher fans out events to saga channels, filtering by
504        // entity type and change type so each saga only receives relevant events.
505        let dispatcher = tokio::spawn(async move {
506            while let Ok(event) = rx.recv_async().await {
507                for ch in &saga_channels {
508                    if event.item_type == ch.entity_type && event.change_type == ch.change_type {
509                        let _ = ch.tx.send(event.clone());
510                    }
511                }
512            }
513        });
514        self.saga_tasks
515            .lock()
516            .expect("saga_tasks mutex poisoned")
517            .push(dispatcher);
518    }
519
520    /// Create a Postgres-backed history store for replay/windback operations.
521    pub fn postgres_history_store(&self) -> Result<Option<PostgresHistoryStore>, String> {
522        self.config
523            .postgres
524            .clone()
525            .map(PostgresHistoryStore::new)
526            .transpose()
527    }
528
529    /// Initialize Postgres replay/listener and wait for catch-up.
530    pub fn init_postgres_and_wait(&self, timeout: Duration) -> Result<(), String> {
531        if self.config.postgres.is_some() && self.postgres_consumer.is_none() {
532            return Err(
533                "Postgres is configured but the Postgres consumer is not running".to_string(),
534            );
535        }
536
537        if let Some(ref consumer) = self.postgres_consumer {
538            consumer.wait_until_caught_up(timeout)?;
539            self.ready.store(true, Ordering::SeqCst);
540        }
541        Ok(())
542    }
543
544    /// Establish relationship invariants.
545    pub fn establish_relations(&self) {
546        if let Err(e) = self.relationship_manager.establish_relations(&self.ctx()) {
547            tracing::error!("Failed to establish relations: {e}");
548        }
549    }
550
551    /// Check if the server is ready to accept connections.
552    pub fn is_ready(&self) -> bool {
553        if let Some(ref consumer) = self.postgres_consumer {
554            if consumer.is_caught_up() {
555                self.ready.store(true, Ordering::SeqCst);
556                return true;
557            }
558            return false;
559        }
560        true
561    }
562
563    /// Run the server with full initialization.
564    pub async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
565        use tokio::net::TcpListener;
566
567        // Persisters can veto startup via startup healthchecks.
568        let entity_types: Vec<&str> = self
569            .handler_registry
570            .entity_types()
571            .map(|t| t.as_ref())
572            .collect();
573        self.persisters
574            .startup_healthcheck(&entity_types)
575            .map_err(|reason| format!("Persister startup healthcheck failed: {reason}"))?;
576
577        if self.config.postgres.is_some() && self.postgres_consumer.is_none() {
578            return Err("Postgres is configured but the Postgres consumer failed to start".into());
579        }
580
581        // Wait for Postgres catch-up if configured
582        if self.postgres_consumer.is_some() {
583            tracing::info!("Waiting for Postgres event consumer to catch up...");
584            let timeout = std::time::Duration::from_secs(300);
585            self.init_postgres_and_wait(timeout)
586                .map_err(|reason| format!("Postgres startup catch-up failed: {reason}"))?;
587            tracing::info!("Postgres caught up, ready to accept connections");
588        }
589
590        // Build search index from store data (after catch-up)
591        tracing::info!("Building search index...");
592        self.search_index.build_from_registry(&self.registry);
593
594        // Establish relations (cleanup orphans, ensure required entities)
595        tracing::info!("Establishing relations...");
596        self.establish_relations();
597
598        // Claim orphaned server-owned items and start death watch
599        tracing::info!("Checking server-owned item ownership...");
600        if let Err(e) = ServerOwnershipManager::claim_orphaned(&self.ctx()) {
601            tracing::error!("Failed to claim orphaned server-owned items: {}", e);
602        }
603        let ownership_guard = ServerOwnershipManager::watch_peer_deaths(&self.ctx());
604        *self
605            ._server_ownership_guard
606            .lock()
607            .expect("server_ownership_guard mutex poisoned") = Some(ownership_guard);
608
609        // Run after_init hook (e.g., scene engine startup) BEFORE binding the
610        // listener. This hook runs synchronously and can be slow (e.g.
611        // rship's scene-editor-view warmup, which materializes a view per
612        // scene) — binding first and accepting later left a window where the
613        // OS would complete TCP handshakes and queue them in the accept
614        // backlog while nothing in the process was reading them yet. A
615        // client connecting in that window would see an established TCP
616        // connection that never got a WebSocket-upgrade response: not
617        // rejected (so no clean retry trigger), not served (so no response
618        // ever arrives) — stuck rather than cleanly failing. Binding late
619        // means a connection attempt during startup gets a prompt
620        // ECONNREFUSED instead, which every client/proxy already retries.
621        if let Some(hook) = self
622            .after_init
623            .lock()
624            .expect("after_init mutex poisoned")
625            .take()
626        {
627            hook(self);
628        }
629
630        self.start_saga_runtime();
631
632        // WS message-throughput summary thread. Emits a single log line every
633        // 250ms with inbound/outbound counts per message kind. Used for
634        // diagnosing server-vs-client pacing during slow loads.
635        crate::ws_timing::start_periodic_logger();
636
637        // Report-cache hit/miss summary thread. Replaces the per-call debug
638        // log spam that was dominating I/O during loads.
639        myko::server::report_cache_stats::start_periodic_logger();
640
641        // Entity-SET summary thread. Replaces the per-`set` "[entity] SET ..."
642        // debug spam (Pulse SETs dominate under pulse-heavy workloads).
643        myko::server::entity_set_stats::start_periodic_logger();
644
645        // Per-search summary thread. One log line per window listing each
646        // search that completed (entity_type, result count, elapsed).
647        myko::search::search_stats::start_periodic_logger();
648
649        // Live per-entity-type item-count gauge, sampled by the OTLP metrics
650        // exporter's own periodic reader (see telemetry::init_from_env) —
651        // no-op when telemetry isn't configured.
652        crate::telemetry::register_item_count_gauge(self.registry.clone());
653
654        // Bind WebSocket listener last, once all synchronous startup work
655        // (including after_init) is done, so peer publication only happens
656        // once the gateway is actually available to serve requests, not just
657        // listening.
658        let listener = TcpListener::bind(&self.config.bind_addr).await?;
659        tracing::info!("CellServer listening on {}", self.config.bind_addr);
660        tracing::info!(
661            "Myko gateway: ws://{}/myko | MCP: /myko/mcp (POST + WS + SSE)",
662            self.config.bind_addr
663        );
664
665        // Start peer registry if configured
666        if self.config.peer_registry.is_some() {
667            self.start_peer_registry(None);
668        }
669
670        tracing::info!("Server started");
671        self.run_ws_accept_loop(listener).await
672    }
673
674    /// Run just the accept loop (no Postgres / relations / saga startup).
675    pub async fn run_ws_loop(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
676        use tokio::net::TcpListener;
677
678        let listener = TcpListener::bind(&self.config.bind_addr).await?;
679        tracing::info!("CellServer listening on {}", self.config.bind_addr);
680        tracing::info!(
681            "Myko gateway: ws://{}/myko | MCP: /myko/mcp (POST + WS + SSE)",
682            self.config.bind_addr
683        );
684        self.run_ws_accept_loop(listener).await
685    }
686
687    async fn run_ws_accept_loop(
688        &self,
689        listener: tokio::net::TcpListener,
690    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
691        let ready = self.ready.clone();
692
693        loop {
694            let (stream, addr) = listener.accept().await?;
695
696            // Disable Nagle (unless configured off): our writes are small,
697            // frequent, latency-sensitive messages (e.g. ~60Hz pulses). With
698            // Nagle on (tokio's default), TCP coalesces successive small writes
699            // into fewer segments that land together, so an even 60Hz send
700            // arrives at the client as ~15-20Hz bursts of 3-4 — which downstream
701            // latest-wins consumers (e.g. the pulse-unreal transform apply) then
702            // collapse to one update per burst, producing visibly choppy motion.
703            // Ship each write promptly instead.
704            if self.config.tcp_nodelay
705                && let Err(e) = stream.set_nodelay(true)
706            {
707                tracing::warn!("failed to set TCP_NODELAY on connection from {addr}: {e}");
708            }
709
710            // Check if server is ready (durable backend caught up)
711            if !ready.load(Ordering::SeqCst) {
712                if self.is_ready() {
713                    tracing::info!("Server is now ready to accept connections");
714                } else {
715                    tracing::warn!(
716                        "Rejecting connection from {} - server not ready (durable backend catching up)",
717                        addr
718                    );
719                    drop(stream);
720                    continue;
721                }
722            }
723
724            tracing::debug!("New connection from {}", addr);
725
726            let ctx = Arc::new(self.ctx());
727            let server_info = self.server_info.clone();
728
729            tokio::spawn(async move {
730                if let Err(e) = router::route_connection(stream, addr, ctx, server_info).await {
731                    tracing::error!("Connection error from {}: {}", addr, e);
732                }
733            });
734        }
735    }
736}
737
738#[cfg(test)]
739mod tests {
740    use super::*;
741
742    #[test]
743    fn test_server_creation() {
744        let config = CellServerConfig {
745            bind_addr: "127.0.0.1:0".parse().unwrap(),
746            tcp_nodelay: true,
747            postgres: None,
748            host_id: None,
749            peer_registry: None,
750            default_persister: None,
751            persister_overrides: HashMap::new(),
752            peer_clients: None,
753        };
754        let server = CellServer::new(config);
755        assert!(Arc::strong_count(&server.registry) >= 1);
756    }
757
758    #[test]
759    fn test_server_with_host_id() {
760        let host_id = Uuid::new_v4();
761        let config = CellServerConfig {
762            bind_addr: "127.0.0.1:0".parse().unwrap(),
763            tcp_nodelay: true,
764            postgres: None,
765            host_id: Some(host_id),
766            peer_registry: None,
767            default_persister: None,
768            persister_overrides: HashMap::new(),
769            peer_clients: None,
770        };
771        let server = CellServer::new(config);
772        assert_eq!(server.host_id, host_id);
773    }
774}