liminal_server/server/connection/services.rs
1use std::collections::HashMap;
2use std::path::Path;
3use std::sync::atomic::{AtomicU64, Ordering};
4use std::sync::{Arc, Mutex, mpsc};
5use std::time::Instant;
6
7use haematite::{Database, DatabaseConfig, EventStore};
8use liminal::channel::{ChannelConfig, ChannelHandle, ChannelMode, Schema};
9use liminal::conversation::{
10 ConversationSupervisor, CrashPolicy, EchoBehaviour, ParticipantBehaviour,
11};
12use liminal::durability::bridge::block_on;
13use liminal::durability::{
14 DedupCache, DedupDecision, DurabilityError, DurableStore, EphemeralHaematiteStore,
15 HaematiteStore, ProcessingReceipt, open_ephemeral,
16};
17use liminal::protocol::{MessageEnvelope, ProtocolError, SchemaId as ProtocolSchemaId};
18
19use super::conversation::{ConnectionConversation, LiminalConversationResource};
20use super::services_cluster::build_channel_cluster;
21use super::services_schema::resolve_channel_schema;
22use super::worker_front_door::WorkerFrontDoorServices;
23use crate::ServerError;
24use crate::config::types::{ClusterConfig, ServerConfig, ServiceProfile};
25use crate::server::participant::{InstalledParticipantService, ProductionParticipantHandler};
26
27pub use super::services_cluster::ChannelCluster;
28
29/// Registry of custom conversation responders, keyed by conversation subject.
30///
31/// A registered [`ParticipantBehaviour`] becomes the participant for any
32/// conversation opened on its subject; subjects with no entry fall back to the
33/// built-in [`EchoBehaviour`].
34type ResponderRegistry = HashMap<String, Arc<dyn ParticipantBehaviour>>;
35
36/// Marker for resources retained by a connection process until unsubscribe.
37pub trait SubscriptionResource: std::fmt::Debug + Send {
38 /// Releases the library subscription resource.
39 ///
40 /// # Errors
41 /// Returns [`ServerError`] when the liminal library reports an unsubscribe failure.
42 fn unsubscribe(self: Box<Self>) -> Result<(), ServerError>;
43
44 /// Attempts to pull the next delivered envelope from the wrapped library
45 /// subscription without blocking.
46 ///
47 /// Returns `None` when the subscriber inbox is empty (or momentarily
48 /// unavailable): the connection process is the delivery pump, so a transient
49 /// empty read is simply "nothing to deliver this slice", never an error.
50 fn try_next(&mut self) -> Option<liminal::envelope::Envelope>;
51
52 /// Non-consuming availability query for the post-arm race barrier.
53 fn has_pending(&self) -> bool;
54
55 /// Whether an overflow has marked this subscription for shedding (§5). The
56 /// delivery pump sheds an overflowed subscription with a typed error frame.
57 /// Defaulted to `false`: a resource with no bounded inbox never overflows.
58 fn is_overflowed(&self) -> bool {
59 false
60 }
61}
62
63/// Library subscription resource owned by a single connection process.
64#[derive(Debug)]
65pub struct ConnectionSubscription {
66 id: u64,
67 /// Client-chosen application stream the server delivers this subscription's
68 /// messages on (echoed on `SubscribeAck`, carried on every `Deliver`). Set by
69 /// the connection process from the `Subscribe` frame before the subscription is
70 /// stored; `0` only while momentarily unset during construction.
71 stream_id: u32,
72 selected_schema: ProtocolSchemaId,
73 resource: Box<dyn SubscriptionResource>,
74}
75
76impl ConnectionSubscription {
77 /// Creates an owned subscription resource for one connection process.
78 #[must_use]
79 pub fn new(
80 id: u64,
81 selected_schema: ProtocolSchemaId,
82 resource: Box<dyn SubscriptionResource>,
83 ) -> Self {
84 Self {
85 id,
86 stream_id: 0,
87 selected_schema,
88 resource,
89 }
90 }
91
92 /// Returns the protocol subscription id.
93 #[must_use]
94 pub const fn id(&self) -> u64 {
95 self.id
96 }
97
98 /// Records the client-chosen delivery stream id for this subscription.
99 pub(super) const fn set_stream_id(&mut self, stream_id: u32) {
100 self.stream_id = stream_id;
101 }
102
103 /// Returns the client-chosen application stream id deliveries ride on.
104 #[must_use]
105 pub(super) const fn stream_id(&self) -> u32 {
106 self.stream_id
107 }
108
109 /// Returns the schema selected for this subscription stream.
110 #[must_use]
111 pub const fn selected_schema(&self) -> ProtocolSchemaId {
112 self.selected_schema
113 }
114
115 /// Attempts to pull the next delivered envelope without blocking.
116 pub(super) fn try_next(&mut self) -> Option<liminal::envelope::Envelope> {
117 self.resource.try_next()
118 }
119
120 pub(super) fn has_pending(&self) -> bool {
121 self.resource.has_pending()
122 }
123
124 /// Whether this subscription has been shed by an inbox overflow (§5).
125 pub(super) fn is_overflowed(&self) -> bool {
126 self.resource.is_overflowed()
127 }
128
129 pub(super) fn unsubscribe(self) -> Result<(), ServerError> {
130 self.resource.unsubscribe()
131 }
132}
133
134/// Outcome of a server publish.
135///
136/// Carries the assigned message id plus a genuine delivery ack (`delivered` = the
137/// message was accepted by at least one live subscriber on this publish, after any
138/// dedup-on-delivery suppression).
139#[derive(Clone, Copy, Debug, PartialEq, Eq)]
140pub struct PublishOutcome {
141 /// Monotonic message id assigned to the accepted publish.
142 pub message_id: u64,
143 /// Whether the message was genuinely delivered to a subscriber. `false` means
144 /// the publish was accepted but reached no subscriber (empty channel) or was
145 /// a duplicate suppressed by dedup-on-delivery.
146 pub delivered: bool,
147}
148
149/// Operations that adapt wire frames to liminal library calls.
150pub trait ConnectionServices: std::fmt::Debug + Send + Sync {
151 /// Returns the complete participant service installed on this adapter.
152 ///
153 /// `None` keeps participant capability disabled even when the adapter owns a
154 /// durable store for unrelated channel traffic. The returned token is
155 /// server-sealed and atomically carries declared semantics plus durability,
156 /// making a handler-without-store activation impossible by construction.
157 fn participant_service(&self) -> Option<InstalledParticipantService> {
158 None
159 }
160
161 /// Delegates a publish request to the liminal library.
162 ///
163 /// `idempotency_key`, when `Some`, drives dedup-on-delivery: a re-publish with
164 /// the same key is delivered to subscribers at most once. The returned
165 /// [`PublishOutcome`] carries the genuine delivery ack.
166 ///
167 /// # Errors
168 /// Returns [`ServerError`] when the liminal publish operation fails.
169 fn publish(
170 &self,
171 channel: &str,
172 envelope: &MessageEnvelope,
173 idempotency_key: Option<&str>,
174 ) -> Result<PublishOutcome, ServerError>;
175
176 /// Delegates a subscribe request to the liminal library.
177 ///
178 /// `install`, when `Some`, carries the connection's §5 shared inbox byte
179 /// budget, per-inbox fairness cap, and R3 wake notifier. The implementation
180 /// MUST install it on the subscription's inbox BEFORE the registration is
181 /// published to the channel actor (i.e. before any envelope can be
182 /// delivered), so no envelope is ever admitted uncharged, past the depth
183 /// cap, or without a wake. Implementations with no real inbox (test
184 /// stand-ins, capability-scoped profiles that refuse subscribe) may ignore
185 /// it.
186 ///
187 /// # Errors
188 /// Returns [`ServerError`] when the liminal subscribe operation fails.
189 fn subscribe(
190 &self,
191 channel: &str,
192 accepted_schemas: &[ProtocolSchemaId],
193 install: Option<liminal::channel::InboxInstall>,
194 ) -> Result<ConnectionSubscription, ServerError>;
195
196 /// Delegates unsubscribe to the liminal library.
197 ///
198 /// # Errors
199 /// Returns [`ServerError`] when the liminal unsubscribe operation fails.
200 fn unsubscribe(&self, subscription: ConnectionSubscription) -> Result<(), ServerError>;
201
202 /// Delegates conversation open to the liminal library.
203 ///
204 /// # Errors
205 /// Returns [`ServerError`] when the liminal conversation open operation fails.
206 fn open_conversation(
207 &self,
208 conversation_id: u64,
209 subject: &str,
210 ) -> Result<ConnectionConversation, ServerError>;
211
212 /// Delegates a conversation message to the liminal library.
213 ///
214 /// # Errors
215 /// Returns [`ServerError`] when the liminal conversation message operation fails.
216 fn conversation_message(
217 &self,
218 conversation: &ConnectionConversation,
219 envelope: &MessageEnvelope,
220 ) -> Result<(), ServerError>;
221
222 /// Delegates conversation close to the liminal library.
223 ///
224 /// # Errors
225 /// Returns [`ServerError`] when the liminal conversation close operation fails.
226 fn close_conversation(&self, conversation: ConnectionConversation) -> Result<(), ServerError>;
227
228 /// Flushes durable channel state through the liminal library boundary.
229 ///
230 /// # Errors
231 /// Returns [`ServerError`] when the liminal channel flush operation fails.
232 fn flush_durable_state(&self) -> Result<(), ServerError>;
233
234 /// Whether this adapter backs ordinary channel and conversation operations.
235 ///
236 /// The default is `true` — the full-service adapter serves publish, subscribe,
237 /// and conversation frames, so full mode is byte-for-byte unchanged. The
238 /// capability-scoped worker front door overrides this to `false`, letting
239 /// [`super::apply`] reject the channel/conversation frames it short-circuits on
240 /// empty connection state (`Unsubscribe`, `ConversationMessage`,
241 /// `ConversationClose`) with a typed error frame instead of silently swallowing
242 /// an operation for a resource that could never have been created in this
243 /// profile. Frames that always reach a service method (`Publish`, `Subscribe`,
244 /// `ConversationOpen`) are rejected by the front door's own method bodies and do
245 /// not consult this flag.
246 fn supports_channel_operations(&self) -> bool {
247 true
248 }
249}
250
251/// Default adapter from server wire frames to liminal channel/conversation APIs.
252#[derive(Debug)]
253pub struct LiminalConnectionServices {
254 channels: HashMap<String, ConfiguredChannel>,
255 cluster: ChannelCluster,
256 durable_store: Arc<dyn DurableStore>,
257 /// Complete participant service, installed only when semantic lifecycle
258 /// handling and its durable aggregate store are both ready.
259 participant_service: Option<InstalledParticipantService>,
260 /// In-memory (haematite-backed) dedup cache for dedup-on-delivery. Keyed by
261 /// the per-message idempotency key carried on the publish frame; a duplicate
262 /// key is suppressed before fan-out so a subscriber receives it at most once.
263 /// Not persisted across restarts (13-L1 scope; durable dedup is deferred).
264 dedup: DedupCache,
265 conversation_supervisor: Arc<ConversationSupervisor>,
266 /// Registered custom conversation responders, keyed by conversation subject.
267 ///
268 /// When a conversation is opened (`open_conversation`), the subject is looked
269 /// up here: a registered [`ParticipantBehaviour`] becomes the conversation's
270 /// participant; with no registration the conversation falls back to the
271 /// built-in [`EchoBehaviour`], preserving the original echo semantics exactly.
272 /// This is the seam aion #13 plugs a remote worker responder into. Interior
273 /// mutability is required because the services are shared behind `&self`.
274 responders: Mutex<ResponderRegistry>,
275 next_message_id: AtomicU64,
276 next_subscription_id: AtomicU64,
277}
278
279impl LiminalConnectionServices {
280 /// Builds library-backed services from validated server configuration.
281 ///
282 /// Durable-mode channels are backed by a shared haematite event store so
283 /// their publishes are persisted and survive the graceful-shutdown flush;
284 /// ephemeral channels carry no store.
285 ///
286 /// Full-only: a config selecting the worker-front-door profile is rejected at
287 /// entry (before any store is built), so this constructor can never build full
288 /// services for a profile that forbids them. Profile-aware callers go through
289 /// [`build_connection_services`] instead.
290 ///
291 /// # Errors
292 /// Returns [`ServerError`] when the config selects a non-full profile or a
293 /// configured channel cannot be initialized.
294 pub fn from_config(config: &ServerConfig) -> Result<Self, ServerError> {
295 require_full_profile(config)?;
296 let store = ProductionSubsystems.durable_store(config.persistence_path.as_deref())?;
297 Self::from_config_with_store_via(config, store, &ProductionSubsystems)
298 }
299
300 /// Builds services over a caller-provided durable store.
301 ///
302 /// Used by tests that need to inspect persisted state through the same store
303 /// handle the durable channels write to.
304 ///
305 /// Full-only: rejects a worker-front-door profile at entry, exactly like
306 /// [`Self::from_config`].
307 ///
308 /// # Errors
309 /// Returns [`ServerError`] when the config selects a non-full profile or a
310 /// configured channel cannot be initialized.
311 pub fn from_config_with_store(
312 config: &ServerConfig,
313 durable_store: Arc<dyn DurableStore>,
314 ) -> Result<Self, ServerError> {
315 require_full_profile(config)?;
316 Self::from_config_with_store_via(config, durable_store, &ProductionSubsystems)
317 }
318
319 /// [`Self::from_config_with_store`] with the subsystem factory injected.
320 ///
321 /// The channel supervisor and conversation supervisor are constructed ONLY
322 /// through `subsystems` — there is no direct constructor call in this body —
323 /// so a factory that records as a side effect of constructing cannot have its
324 /// recording omitted (§9 D2 seam census, record-by-construction). No profile
325 /// check here: the caller (the public wrapper or the profile dispatch in
326 /// [`build_connection_services`]) has already established the full profile.
327 fn from_config_with_store_via(
328 config: &ServerConfig,
329 durable_store: Arc<dyn DurableStore>,
330 subsystems: &dyn SubsystemFactory,
331 ) -> Result<Self, ServerError> {
332 // Build ONE shared channel supervisor for the whole server. When a
333 // [cluster] section is present it is distribution-enabled, so every
334 // channel actor and subscriber shares the clustered scheduler the cluster
335 // attaches its process-group transport to (SRV-005, Constraint B).
336 let cluster = subsystems.channel_cluster(config.cluster.as_ref())?;
337 let mut channels = HashMap::new();
338 for channel in &config.channels {
339 // Resolve the channel's real JSON Schema (loaded from `schema_ref`
340 // during config validation) or the permissive empty schema when the
341 // channel declared none. The protocol schema id advertised at
342 // subscribe time is derived from the SAME schema bytes so an SDK
343 // deriving ids from schema bytes converges on it.
344 let resolved = resolve_channel_schema(channel);
345 let schema =
346 Schema::new(resolved.document).map_err(|error| ServerError::ConfigValidation {
347 message: format!("failed to initialize channel '{}': {error}", channel.name),
348 })?;
349 let channel_config = if channel.durable {
350 ChannelConfig::new(channel.name.clone(), schema, ChannelMode::Durable)
351 } else {
352 ChannelConfig::new(channel.name.clone(), schema, ChannelMode::Ephemeral)
353 };
354 let handle = if channel.durable {
355 ChannelHandle::new_durable_with_supervisor(
356 channel_config,
357 Arc::clone(&durable_store),
358 cluster.supervisor().clone(),
359 )
360 .map_err(|error| ServerError::ConfigValidation {
361 message: format!(
362 "failed to initialize durable channel '{}': {error}",
363 channel.name
364 ),
365 })?
366 } else {
367 ChannelHandle::with_supervisor(channel_config, cluster.supervisor().clone())
368 };
369 channels.insert(
370 channel.name.clone(),
371 ConfiguredChannel {
372 handle,
373 protocol_schema: resolved.protocol_id,
374 },
375 );
376 }
377 let conversation_supervisor = subsystems.conversation_supervisor()?;
378 let dedup = DedupCache::new(Arc::clone(&durable_store), DELIVERY_DEDUP_NAMESPACE);
379 // Production participant activation (LP gap closure, Part B): the
380 // deployment's [participant] section installs the ONE production
381 // semantic handler, sealed together with the same durable store the
382 // conversation logs live in, under the configured wire-frame limit.
383 // No section, no service — the capability bit stays off and the
384 // connection path is byte-identical to the pre-activation build.
385 let participant_service = config
386 .participant
387 .as_ref()
388 .map(|participant| {
389 let handler =
390 ProductionParticipantHandler::new(Arc::clone(&durable_store), *participant)
391 .map_err(|error| ServerError::ParticipantStartupRestore {
392 message: error.to_string(),
393 })?;
394 InstalledParticipantService::new(
395 Arc::new(handler),
396 Arc::clone(&durable_store),
397 participant.wire_frame_limit,
398 )
399 .map_err(|error| ServerError::ConfigValidation {
400 message: format!(
401 "participant.wire_frame_limit: {} is below the protocol's minimum \
402 complete participant frame ({error:?})",
403 participant.wire_frame_limit
404 ),
405 })
406 })
407 .transpose()?;
408 Ok(Self {
409 channels,
410 cluster,
411 durable_store,
412 participant_service,
413 dedup,
414 conversation_supervisor,
415 responders: Mutex::new(HashMap::new()),
416 next_message_id: AtomicU64::new(1),
417 next_subscription_id: AtomicU64::new(1),
418 })
419 }
420
421 /// Builds services with no configured channels.
422 ///
423 /// # Errors
424 /// Returns [`ServerError`] when the conversation supervisor scheduler cannot start.
425 pub fn empty() -> Result<Self, ServerError> {
426 let conversation_supervisor = ProductionSubsystems.conversation_supervisor()?;
427 let durable_store = build_durable_store(None)?;
428 let dedup = DedupCache::new(Arc::clone(&durable_store), DELIVERY_DEDUP_NAMESPACE);
429 Ok(Self {
430 channels: HashMap::new(),
431 cluster: build_channel_cluster(None)?,
432 durable_store,
433 participant_service: None,
434 dedup,
435 conversation_supervisor,
436 responders: Mutex::new(HashMap::new()),
437 next_message_id: AtomicU64::new(1),
438 next_subscription_id: AtomicU64::new(1),
439 })
440 }
441
442 /// The shared channel supervisor + cluster resolver backing this service.
443 ///
444 /// The server runtime uses this to attach the cluster to the channel
445 /// supervisor's clustered scheduler (SRV-005).
446 #[must_use]
447 pub const fn channel_cluster(&self) -> &ChannelCluster {
448 &self.cluster
449 }
450
451 /// Returns the shared durable store backing this service's durable channels.
452 #[must_use]
453 pub fn durable_store(&self) -> Arc<dyn DurableStore> {
454 Arc::clone(&self.durable_store)
455 }
456
457 /// Installs a complete participant bundle in full-service supervisor tests.
458 ///
459 /// Production full services intentionally stay disabled until a concrete
460 /// lifecycle handler exists. This consuming test builder exercises the real
461 /// supervisor activation path without allowing an already-shared adapter to
462 /// change capability posture.
463 #[cfg(test)]
464 #[must_use]
465 pub(crate) fn with_participant_service(
466 mut self,
467 participant_service: InstalledParticipantService,
468 ) -> Self {
469 self.participant_service = Some(participant_service);
470 self
471 }
472
473 /// Returns the conversation supervisor backing supervised conversations.
474 ///
475 /// Tests use this to reach the underlying beamr scheduler so they can spawn
476 /// or terminate participant processes and exercise crash detection.
477 #[must_use]
478 pub fn conversation_supervisor(&self) -> Arc<ConversationSupervisor> {
479 Arc::clone(&self.conversation_supervisor)
480 }
481
482 /// Registers a custom conversation responder for a routing `subject`.
483 ///
484 /// When a conversation is later opened with this exact `subject`, its
485 /// participant runs `behaviour` instead of the built-in [`EchoBehaviour`].
486 /// The responder is spawned and supervised identically to the echo
487 /// participant — a real linked beamr process with the same crash-detection
488 /// semantics — so this exposes the responder seam without changing how
489 /// participants run. Registering a subject that already has a responder
490 /// replaces it; the previous behaviour is returned.
491 ///
492 /// This is the liminal-side seam aion #13 plugs a remote worker into: it
493 /// registers a responder that forwards each request to the worker and routes
494 /// the worker's reply back through the conversation. Subjects with no
495 /// registration keep echoing, so existing callers are unaffected.
496 ///
497 /// # Errors
498 /// Returns [`ServerError`] when the responder registry lock is poisoned.
499 pub fn register_responder(
500 &self,
501 subject: impl Into<String>,
502 behaviour: Arc<dyn ParticipantBehaviour>,
503 ) -> Result<Option<Arc<dyn ParticipantBehaviour>>, ServerError> {
504 let mut responders = self.lock_responders()?;
505 Ok(responders.insert(subject.into(), behaviour))
506 }
507
508 /// Removes the custom responder registered for `subject`, if any.
509 ///
510 /// After removal the subject reverts to the built-in [`EchoBehaviour`] on the
511 /// next [`Self::open_conversation`]. Returns the removed behaviour when one
512 /// was registered.
513 ///
514 /// # Errors
515 /// Returns [`ServerError`] when the responder registry lock is poisoned.
516 pub fn unregister_responder(
517 &self,
518 subject: &str,
519 ) -> Result<Option<Arc<dyn ParticipantBehaviour>>, ServerError> {
520 let mut responders = self.lock_responders()?;
521 Ok(responders.remove(subject))
522 }
523
524 /// Resolves the responder behaviour for `subject`: the registered custom
525 /// responder when present, otherwise the built-in [`EchoBehaviour`].
526 ///
527 /// This is the single routing decision behind the seam — registered-or-echo —
528 /// so the fallback is identical to the original hard-wired echo path.
529 fn responder_for(&self, subject: &str) -> Result<Arc<dyn ParticipantBehaviour>, ServerError> {
530 let responders = self.lock_responders()?;
531 Ok(responders.get(subject).map_or_else(
532 || Arc::new(EchoBehaviour) as Arc<dyn ParticipantBehaviour>,
533 Arc::clone,
534 ))
535 }
536
537 /// Locks the responder registry, mapping a poisoned lock to a [`ServerError`]
538 /// rather than panicking (the workspace denies `unwrap`/`expect`/`panic`).
539 fn lock_responders(&self) -> Result<std::sync::MutexGuard<'_, ResponderRegistry>, ServerError> {
540 self.responders
541 .lock()
542 .map_err(|_poisoned| ServerError::ListenerAccept {
543 message: "responder registry lock poisoned".to_owned(),
544 })
545 }
546
547 /// Subscribes to a configured channel and returns the raw library
548 /// subscription handle so a test can drain the subscriber inbox directly and
549 /// observe exactly which messages reached a subscriber.
550 #[cfg(test)]
551 pub(crate) fn subscribe_handle_for_test(
552 &self,
553 channel: &str,
554 ) -> Result<liminal::channel::SubscriptionHandle, ServerError> {
555 let configured = self
556 .channels
557 .get(channel)
558 .ok_or_else(|| ServerError::ListenerAccept {
559 message: format!("channel '{channel}' is not configured"),
560 })?;
561 configured
562 .handle
563 .subscribe()
564 .map_err(|error| ServerError::ListenerAccept {
565 message: format!("liminal subscribe failed for channel '{channel}': {error}"),
566 })
567 }
568
569 /// Claims the delivery right for an idempotency key.
570 ///
571 /// Returns `Ok(true)` when this is the first publish for the key (the caller
572 /// may deliver), and `Ok(false)` when the key was already claimed/completed (a
573 /// duplicate the caller must suppress). The dedup cache is driven synchronously
574 /// over the in-memory haematite store via the durable bridge.
575 fn claim_delivery(&self, key: &str) -> Result<bool, ServerError> {
576 let decision = block_on(self.dedup.claim_or_get(key, dedup_timestamp_millis()))
577 .map_err(|error| ServerError::ListenerAccept {
578 message: format!("dedup bridge failed for key '{key}': {error}"),
579 })?
580 .map_err(|error| ServerError::ListenerAccept {
581 message: format!("dedup claim failed for key '{key}': {error}"),
582 })?;
583 Ok(matches!(decision, DedupDecision::Claimed))
584 }
585
586 /// Releases a dangling in-flight dedup claim after a failed delivery.
587 ///
588 /// Best-effort: a release failure cannot mask the original publish error, so
589 /// this returns nothing and logs at `error` level instead of surfacing. It is
590 /// never silent — the leak (a permanently suppressed key) must be observable.
591 /// `release_claim` itself never clobbers a stored receipt, so calling it on the
592 /// failure path is safe even if a concurrent completion raced ahead.
593 fn release_claim(&self, key: &str) {
594 match block_on(self.dedup.release_claim(key)) {
595 Ok(Ok(())) => {}
596 Ok(Err(error)) => {
597 tracing::error!(
598 idempotency_key = key,
599 %error,
600 "failed to release dedup claim after publish failure; key may stay suppressed"
601 );
602 }
603 Err(error) => {
604 tracing::error!(
605 idempotency_key = key,
606 %error,
607 "dedup release bridge failed after publish failure; key may stay suppressed"
608 );
609 }
610 }
611 }
612}
613
614/// Returns the current epoch-millis timestamp used as the dedup entry anchor.
615///
616/// A clock error before the Unix epoch yields `0`: the timestamp is only a TTL
617/// anchor for the in-memory cache and a zero anchor never breaks the at-most-once
618/// claim semantics, so this avoids surfacing a clock fault on the publish path.
619fn dedup_timestamp_millis() -> u64 {
620 use std::time::{SystemTime, UNIX_EPOCH};
621 SystemTime::now()
622 .duration_since(UNIX_EPOCH)
623 .ok()
624 .and_then(|duration| u64::try_from(duration.as_millis()).ok())
625 .unwrap_or(0)
626}
627
628/// Default shard count for an on-disk durable store.
629///
630/// Haematite routes keys across this many single-threaded shard actors; a small
631/// power of two gives parallelism across cursors/streams without spawning an
632/// actor per core. The value is fixed (haematite has no silent default) and not
633/// yet surfaced in server config.
634const DEFAULT_SHARD_COUNT: usize = 8;
635
636/// Namespace prefix for the dedup-on-delivery cache streams. Keeps delivery dedup
637/// keys from colliding with any other haematite streams in the shared store.
638const DELIVERY_DEDUP_NAMESPACE: &str = "liminal:delivery-dedup";
639
640/// Beamr-scheduler-owning subsystems the full-service construction path builds
641/// beyond the connection supervisor's own scheduler. The §9 D2 seam census counts
642/// these: the worker-front-door profile must construct NONE of them. Test-gated:
643/// this is the recording vocabulary of the gate's instrument, not production state.
644#[cfg(test)]
645#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
646pub(super) enum SchedulerSubsystem {
647 /// The shared channel supervisor (its own beamr scheduler).
648 ChannelSupervisor,
649 /// The conversation supervisor (its own beamr scheduler).
650 ConversationSupervisor,
651 /// The haematite store's database (its shard-actor scheduler).
652 HaematiteStore,
653}
654
655/// Constructor seam for every scheduler-owning subsystem (`SchedulerSubsystem`)
656/// the profile-aware construction path can create.
657///
658/// These methods are the ONLY route through which [`build_connection_services`]
659/// and the [`LiminalConnectionServices`] config constructors reach
660/// `build_channel_cluster`, `ConversationSupervisor::new`, and the durable-store
661/// constructors — no direct constructor call exists in those bodies. The §9 D2
662/// gate therefore injects a factory that records as a side effect of
663/// constructing (the D3 store-seam ownership move applied to schedulers): a
664/// recording cannot be omitted without also failing to construct the subsystem,
665/// which closes the "hand-placed census call beside the constructor" gap where a
666/// future subsystem could be built without its courtesy call.
667pub(super) trait SubsystemFactory {
668 /// Constructs the shared channel supervisor + cluster resolver (a beamr
669 /// scheduler).
670 ///
671 /// # Errors
672 /// Returns [`ServerError`] when the channel supervisor scheduler cannot start.
673 fn channel_cluster(
674 &self,
675 cluster_config: Option<&ClusterConfig>,
676 ) -> Result<ChannelCluster, ServerError>;
677
678 /// Constructs the conversation supervisor (a beamr scheduler).
679 ///
680 /// # Errors
681 /// Returns [`ServerError`] when the conversation supervisor scheduler cannot
682 /// start.
683 fn conversation_supervisor(&self) -> Result<Arc<ConversationSupervisor>, ServerError>;
684
685 /// Constructs the durable store (haematite's shard-actor scheduler):
686 /// persistent under `persistence_path`, self-owning ephemeral otherwise.
687 ///
688 /// # Errors
689 /// Returns [`ServerError`] when the store cannot be opened.
690 fn durable_store(
691 &self,
692 persistence_path: Option<&Path>,
693 ) -> Result<Arc<dyn DurableStore>, ServerError>;
694}
695
696/// The production factory: the real constructors, recording nothing.
697pub(super) struct ProductionSubsystems;
698
699impl SubsystemFactory for ProductionSubsystems {
700 fn channel_cluster(
701 &self,
702 cluster_config: Option<&ClusterConfig>,
703 ) -> Result<ChannelCluster, ServerError> {
704 build_channel_cluster(cluster_config)
705 }
706
707 fn conversation_supervisor(&self) -> Result<Arc<ConversationSupervisor>, ServerError> {
708 Ok(Arc::new(ConversationSupervisor::new().map_err(
709 |error| ServerError::ConfigValidation {
710 message: format!("failed to start conversation supervisor: {error}"),
711 },
712 )?))
713 }
714
715 fn durable_store(
716 &self,
717 persistence_path: Option<&Path>,
718 ) -> Result<Arc<dyn DurableStore>, ServerError> {
719 build_durable_store(persistence_path)
720 }
721}
722
723/// Test-only recording implementation of [`SubsystemFactory`] for the §9 D2
724/// construction gate. Lives here (not in a test module's private scope) so the
725/// supervisor-level gate test reuses the same instrument.
726#[cfg(test)]
727#[allow(clippy::expect_used)]
728pub(super) mod subsystem_census {
729 use std::path::{Path, PathBuf};
730 use std::sync::{Arc, Mutex};
731
732 use liminal::conversation::ConversationSupervisor;
733 use liminal::durability::{DurableStore, open_ephemeral_rooted};
734
735 use super::{
736 ChannelCluster, DEFAULT_SHARD_COUNT, ProductionSubsystems, SchedulerSubsystem,
737 SubsystemFactory, build_durable_store_with,
738 };
739 use crate::ServerError;
740 use crate::config::types::ClusterConfig;
741
742 /// Records each [`SchedulerSubsystem`] AS A SIDE EFFECT of constructing it,
743 /// then hands back the production-constructed subsystem (with ephemeral
744 /// stores rooted in an isolated directory, so the fs half of the gate is a
745 /// real negative assertion). Because [`SubsystemFactory`] is the only route
746 /// the profile-aware construction path has to these constructors, a
747 /// recording cannot be omitted without also failing to construct — the
748 /// record-by-construction guarantee.
749 pub struct RecordingSubsystems {
750 census: Mutex<Vec<SchedulerSubsystem>>,
751 ephemeral_root: PathBuf,
752 }
753
754 impl RecordingSubsystems {
755 /// A recording factory whose ephemeral stores live under `ephemeral_root`.
756 pub fn rooted(ephemeral_root: &Path) -> Self {
757 Self {
758 census: Mutex::new(Vec::new()),
759 ephemeral_root: ephemeral_root.to_path_buf(),
760 }
761 }
762
763 /// The recorded construction census, sorted for order-independent
764 /// comparison.
765 pub fn recorded(&self) -> Vec<SchedulerSubsystem> {
766 let mut recorded = self
767 .census
768 .lock()
769 .expect("subsystem census lock is never poisoned in tests")
770 .clone();
771 recorded.sort();
772 recorded
773 }
774
775 fn record(&self, subsystem: SchedulerSubsystem) {
776 self.census
777 .lock()
778 .expect("subsystem census lock is never poisoned in tests")
779 .push(subsystem);
780 }
781 }
782
783 impl SubsystemFactory for RecordingSubsystems {
784 fn channel_cluster(
785 &self,
786 cluster_config: Option<&ClusterConfig>,
787 ) -> Result<ChannelCluster, ServerError> {
788 let cluster = ProductionSubsystems.channel_cluster(cluster_config)?;
789 self.record(SchedulerSubsystem::ChannelSupervisor);
790 Ok(cluster)
791 }
792
793 fn conversation_supervisor(&self) -> Result<Arc<ConversationSupervisor>, ServerError> {
794 let supervisor = ProductionSubsystems.conversation_supervisor()?;
795 self.record(SchedulerSubsystem::ConversationSupervisor);
796 Ok(supervisor)
797 }
798
799 fn durable_store(
800 &self,
801 persistence_path: Option<&Path>,
802 ) -> Result<Arc<dyn DurableStore>, ServerError> {
803 let store = build_durable_store_with(persistence_path, || {
804 open_ephemeral_rooted(&self.ephemeral_root, DEFAULT_SHARD_COUNT)
805 })?;
806 self.record(SchedulerSubsystem::HaematiteStore);
807 Ok(store)
808 }
809 }
810}
811
812/// Full-only constructor guard: rejects a config whose profile is not `Full`.
813///
814/// [`LiminalConnectionServices`]' config-based constructors call this at entry so
815/// the full service stack can never be built for a worker-front-door config —
816/// profile enforcement holds on every public construction path, not only the
817/// file-loading pipeline.
818fn require_full_profile(config: &ServerConfig) -> Result<(), ServerError> {
819 match config.services.profile()? {
820 ServiceProfile::Full => Ok(()),
821 ServiceProfile::WorkerFrontDoor => Err(ServerError::ConfigValidation {
822 message: format!(
823 "services.profile: \"{}\" cannot construct the full LiminalConnectionServices; \
824 build profile-selected services via build_connection_services",
825 ServiceProfile::WORKER_FRONT_DOOR
826 ),
827 }),
828 }
829}
830
831/// Builds the connection-services adapter selected by `config`'s service profile.
832///
833/// `Full` builds [`LiminalConnectionServices`] (channels, conversations, durable
834/// store, dedup cache) exactly as today. `WorkerFrontDoor` builds
835/// [`WorkerFrontDoorServices`], which constructs none of that machinery. This is the
836/// single profile-dispatch authority: [`super::supervisor::ConnectionSupervisor`]'s
837/// config constructor and the standalone runtime's worker arm both route through it
838/// (the runtime's full arm stays on the explicit
839/// [`LiminalConnectionServices::from_config`] path because it also needs the shared
840/// channel cluster, which the trait object does not expose — that path is guarded
841/// full-only at entry).
842///
843/// # Errors
844/// Returns [`ServerError`] when the selected adapter cannot be constructed, the
845/// configured profile value is not recognised, or the worker-front-door profile is
846/// combined with full-only config fields.
847pub fn build_connection_services(
848 config: &ServerConfig,
849) -> Result<Arc<dyn ConnectionServices>, ServerError> {
850 build_connection_services_via(config, &ProductionSubsystems)
851}
852
853/// [`build_connection_services`] with the subsystem factory injected — the §9 D2
854/// gate seam, both halves at once.
855///
856/// Every scheduler-owning subsystem the `Full` branch creates is constructed
857/// through `subsystems` and nowhere else, so a recording factory observes exactly
858/// what was built (thread half, record-by-construction); the gate's factory also
859/// roots its ephemeral stores in an isolated directory, so "the root stays empty
860/// on the worker branch" is a real negative assertion (fs half, the D3 pattern).
861/// The `WorkerFrontDoor` branch never touches the factory — a regression that gave
862/// the front door any subsystem would both record in the census and land a store
863/// directory in the injected root.
864///
865/// The worker branch re-runs the cross-field checks here, not only in file-loading
866/// validation, so a directly-constructed config cannot smuggle full-only machinery
867/// past the profile.
868pub(super) fn build_connection_services_via(
869 config: &ServerConfig,
870 subsystems: &dyn SubsystemFactory,
871) -> Result<Arc<dyn ConnectionServices>, ServerError> {
872 match config.services.profile()? {
873 ServiceProfile::Full => {
874 let store = subsystems.durable_store(config.persistence_path.as_deref())?;
875 Ok(Arc::new(
876 LiminalConnectionServices::from_config_with_store_via(config, store, subsystems)?,
877 ))
878 }
879 ServiceProfile::WorkerFrontDoor => {
880 let errors = crate::config::validation::worker_front_door_field_errors(config);
881 if !errors.is_empty() {
882 return Err(ServerError::ConfigValidation {
883 message: errors.join("; "),
884 });
885 }
886 Ok(Arc::new(WorkerFrontDoorServices::new()))
887 }
888 }
889}
890
891/// Builds the haematite-backed durable store.
892///
893/// When `persistence_path` is `Some`, the database lives there and survives
894/// process restarts: an existing database directory is reopened, a fresh one is
895/// created. When it is `None` (no durable path configured, or the channel-free
896/// `empty()` services used by tests), a self-owning ephemeral store is opened
897/// instead: its temporary directory is created and removed by the store itself
898/// (D3), so it leaves no residue once the last store handle drops. The two paths
899/// return distinct concrete stores on purpose — only the ephemeral one carries a
900/// directory guard; the persistent path is untouched.
901fn build_durable_store(
902 persistence_path: Option<&Path>,
903) -> Result<Arc<dyn DurableStore>, ServerError> {
904 build_durable_store_with(persistence_path, || open_ephemeral(DEFAULT_SHARD_COUNT))
905}
906
907/// [`build_durable_store`] with the ephemeral factory injected.
908///
909/// The split exists for the D3 construction gates: the SAME branch logic runs
910/// in production and tests, and only the factory closure differs — tests root
911/// the ephemeral store in an isolated directory (via liminal's test-gated
912/// rooted factory) so "no ephemeral directory was created" is a real assertion
913/// rather than a scan of the shared system temp dir.
914fn build_durable_store_with(
915 persistence_path: Option<&Path>,
916 make_ephemeral: impl FnOnce() -> Result<EphemeralHaematiteStore, DurabilityError>,
917) -> Result<Arc<dyn DurableStore>, ServerError> {
918 let Some(path) = persistence_path else {
919 let store = make_ephemeral().map_err(|error| ServerError::ConfigValidation {
920 message: format!("failed to open ephemeral durable store: {error}"),
921 })?;
922 return Ok(Arc::new(store));
923 };
924 let data_dir = path.join("durability");
925 let database = open_or_create_database(&data_dir)?;
926 let event_store = EventStore::new(database);
927 Ok(Arc::new(HaematiteStore::new(Arc::new(event_store))))
928}
929
930/// Opens an existing haematite database at `data_dir`, or creates one.
931fn open_or_create_database(data_dir: &Path) -> Result<Database, ServerError> {
932 let config_file = data_dir.join("config.json");
933 let result = if config_file.exists() {
934 Database::open(data_dir)
935 } else {
936 Database::create(DatabaseConfig {
937 data_dir: data_dir.to_path_buf(),
938 shard_count: DEFAULT_SHARD_COUNT,
939 sweep_interval: None,
940 distributed: None,
941 })
942 };
943 result.map_err(|error| ServerError::ConfigValidation {
944 message: format!(
945 "failed to open durable store at {}: {error}",
946 data_dir.display()
947 ),
948 })
949}
950
951impl ConnectionServices for LiminalConnectionServices {
952 fn participant_service(&self) -> Option<InstalledParticipantService> {
953 self.participant_service.clone()
954 }
955
956 fn publish(
957 &self,
958 channel: &str,
959 envelope: &MessageEnvelope,
960 idempotency_key: Option<&str>,
961 ) -> Result<PublishOutcome, ServerError> {
962 let handle = self
963 .channels
964 .get(channel)
965 .map(|configured| configured.handle.clone())
966 .ok_or_else(|| ServerError::ListenerAccept {
967 message: format!("channel '{channel}' is not configured"),
968 })?;
969
970 // Dedup-on-delivery: a publish carrying an idempotency key is delivered to
971 // subscribers AT MOST ONCE across re-publishes of the same key. Only a
972 // fresh `Claimed` decision proceeds to fan-out; a `Completed`/`InFlight`
973 // decision is a duplicate and is suppressed (no second delivery), which is
974 // the at-most-once guarantee the aion outbox relies on.
975 if let Some(key) = idempotency_key {
976 if !self.claim_delivery(key)? {
977 // A dedup-suppressed re-publish is still an accepted publish (it is
978 // assigned a message id), but it reaches no subscriber, so it counts
979 // toward publishes and not deliveries.
980 crate::metrics::publish_accepted();
981 return Ok(PublishOutcome {
982 message_id: self.next_message_id.fetch_add(1, Ordering::Relaxed),
983 delivered: false,
984 });
985 }
986 }
987
988 let delivery = handle.publish_with_delivery(
989 &envelope.payload,
990 liminal::envelope::PublisherId::default(),
991 None,
992 );
993 let delivery = match delivery {
994 Ok(delivery) => delivery,
995 Err(error) => {
996 // The claim above appended an `InFlight` entry but the delivery
997 // failed before `complete_receipt` could run. Release the claim so
998 // the key is re-claimable; otherwise every re-publish would see
999 // `InFlight` and be suppressed forever. Best-effort: surface the
1000 // ORIGINAL publish error regardless, but never swallow a release
1001 // failure silently (it leaves the leak intact).
1002 if let Some(key) = idempotency_key {
1003 self.release_claim(key);
1004 }
1005 return Err(ServerError::ListenerAccept {
1006 message: format!("liminal publish failed for channel '{channel}': {error}"),
1007 });
1008 }
1009 };
1010
1011 // Record the dedup completion AFTER a successful claimed delivery so the
1012 // claim is not left dangling `InFlight` (which would wrongly defer every
1013 // future duplicate). The receipt body is empty: the dedup contract here
1014 // only needs presence, not a stored result.
1015 if let Some(key) = idempotency_key {
1016 block_on(
1017 self.dedup
1018 .complete_receipt(key, ProcessingReceipt::new(Vec::new())),
1019 )
1020 .map_err(|error| ServerError::ListenerAccept {
1021 message: format!("dedup receipt bridge failed for key '{key}': {error}"),
1022 })?
1023 .map_err(|error| ServerError::ListenerAccept {
1024 message: format!("dedup receipt write failed for key '{key}': {error}"),
1025 })?;
1026 }
1027
1028 // Record the accepted publish and its genuine subscriber deliveries. The
1029 // delivered count (0 for an empty channel) is the same signal the delivery
1030 // ack is derived from.
1031 crate::metrics::publish_accepted();
1032 let delivered_count = u64::try_from(delivery.delivered_count()).unwrap_or(u64::MAX);
1033 crate::metrics::deliveries_recorded(delivered_count);
1034
1035 Ok(PublishOutcome {
1036 message_id: self.next_message_id.fetch_add(1, Ordering::Relaxed),
1037 delivered: delivery.is_delivered(),
1038 })
1039 }
1040
1041 fn subscribe(
1042 &self,
1043 channel: &str,
1044 accepted_schemas: &[ProtocolSchemaId],
1045 install: Option<liminal::channel::InboxInstall>,
1046 ) -> Result<ConnectionSubscription, ServerError> {
1047 let configured = self
1048 .channels
1049 .get(channel)
1050 .ok_or_else(|| ServerError::ListenerAccept {
1051 message: format!("channel '{channel}' is not configured"),
1052 })?;
1053 let selected_schema = if accepted_schemas.is_empty() {
1054 configured.protocol_schema
1055 } else {
1056 liminal::protocol::negotiate_schema(configured.protocol_schema, accepted_schemas)
1057 .map_err(|error| server_error_from_protocol(&error))?
1058 };
1059 // `subscribe_with_install` installs the §5 budget/fairness cap and the R3
1060 // wake notifier on the inbox at construction — strictly before the
1061 // registration is published to the channel actor — so there is no window
1062 // in which a publish can land uncharged or without a wake.
1063 let subscription = install
1064 .map_or_else(
1065 || configured.handle.subscribe(),
1066 |install| configured.handle.subscribe_with_install(install),
1067 )
1068 .map_err(|error| ServerError::ListenerAccept {
1069 message: format!("liminal subscribe failed for channel '{channel}': {error}"),
1070 })?;
1071 let id = self.next_subscription_id.fetch_add(1, Ordering::Relaxed);
1072 Ok(ConnectionSubscription::new(
1073 id,
1074 selected_schema,
1075 Box::new(LiminalSubscriptionResource { subscription }),
1076 ))
1077 }
1078
1079 fn unsubscribe(&self, subscription: ConnectionSubscription) -> Result<(), ServerError> {
1080 subscription.unsubscribe()
1081 }
1082
1083 fn open_conversation(
1084 &self,
1085 conversation_id: u64,
1086 subject: &str,
1087 ) -> Result<ConnectionConversation, ServerError> {
1088 // Spawn a REAL participant process (a beamr `NativeHandler` running the
1089 // resolved responder behaviour) on the conversation supervisor's
1090 // scheduler, and a supervised conversation actor linked to it. The actor
1091 // FORWARDS each conversation message to the participant, which genuinely
1092 // processes it and delivers a reply back. The actor traps the
1093 // participant's EXIT (a beamr process link), so killing it fires a
1094 // structural, microsecond-scale crash signal.
1095 //
1096 // The responder is chosen by `subject`: a custom responder registered via
1097 // `register_responder` for this subject, or the built-in `EchoBehaviour`
1098 // when none is registered. Either way it runs as the SAME supervised,
1099 // linked participant process — the seam changes WHO responds, not HOW the
1100 // participant is spawned or supervised.
1101 let behaviour = self.responder_for(subject)?;
1102 let (actor, participant) = self
1103 .conversation_supervisor
1104 .spawn_with_participant(behaviour, None, ChannelMode::Ephemeral, CrashPolicy::Fail)
1105 .map_err(|error| ServerError::ListenerAccept {
1106 message: format!(
1107 "failed to spawn supervised conversation {conversation_id} ('{subject}'): {error}"
1108 ),
1109 })?;
1110
1111 // Drive boot to completion so the beamr link to the participant exists
1112 // before any message is forwarded (link-before-forward), mirroring the
1113 // ROUTING-004 dispatch pattern.
1114 actor.pid().map_err(|error| ServerError::ListenerAccept {
1115 message: format!(
1116 "failed to boot supervised conversation {conversation_id} ('{subject}'): {error}"
1117 ),
1118 })?;
1119
1120 // Register the structural EXIT notifier BEFORE returning, so a crash that
1121 // fires the instant a message reaches the participant is never missed.
1122 // The notifier is woken by the actor's trapped-EXIT handler (event
1123 // driven), and a crash that already landed is replayed immediately.
1124 let (exit_tx, exit_rx) = mpsc::sync_channel::<Instant>(1);
1125 actor
1126 .notify_on_participant_exit(participant, exit_tx)
1127 .map_err(|error| ServerError::ListenerAccept {
1128 message: format!(
1129 "failed to arm crash detection for conversation {conversation_id}: {error}"
1130 ),
1131 })?;
1132
1133 Ok(ConnectionConversation::new(Box::new(
1134 LiminalConversationResource::new(actor, participant, exit_rx),
1135 )))
1136 }
1137
1138 fn conversation_message(
1139 &self,
1140 conversation: &ConnectionConversation,
1141 envelope: &MessageEnvelope,
1142 ) -> Result<(), ServerError> {
1143 conversation.message(envelope)
1144 }
1145
1146 fn close_conversation(&self, conversation: ConnectionConversation) -> Result<(), ServerError> {
1147 conversation.close()
1148 }
1149
1150 fn flush_durable_state(&self) -> Result<(), ServerError> {
1151 for (channel_name, configured) in &self.channels {
1152 if configured.handle.config().mode == ChannelMode::Durable {
1153 configured
1154 .handle
1155 .flush()
1156 .map_err(|error| ServerError::ShutdownFlush {
1157 message: format!(
1158 "failed to flush durable channel '{channel_name}': {error}"
1159 ),
1160 })?;
1161 }
1162 }
1163 Ok(())
1164 }
1165}
1166
1167#[derive(Debug)]
1168struct ConfiguredChannel {
1169 handle: ChannelHandle,
1170 protocol_schema: ProtocolSchemaId,
1171}
1172
1173#[derive(Debug)]
1174struct LiminalSubscriptionResource {
1175 subscription: liminal::channel::SubscriptionHandle,
1176}
1177
1178impl SubscriptionResource for LiminalSubscriptionResource {
1179 fn unsubscribe(self: Box<Self>) -> Result<(), ServerError> {
1180 drop(self.subscription);
1181 Ok(())
1182 }
1183
1184 fn is_overflowed(&self) -> bool {
1185 self.subscription.is_overflowed()
1186 }
1187
1188 fn has_pending(&self) -> bool {
1189 self.subscription.has_pending()
1190 }
1191
1192 fn try_next(&mut self) -> Option<liminal::envelope::Envelope> {
1193 match self.subscription.try_next() {
1194 Ok(envelope) => envelope,
1195 Err(error) => {
1196 // A poisoned inbox lock is PERMANENT, not transient: once poisoned it
1197 // stays poisoned, so every future `try_next` also returns `Err` and this
1198 // subscription goes silent for the rest of its life — no further
1199 // deliveries, not "held for the next slice". Poisoning requires a panic
1200 // while the lock is held, which the workspace lints forbid
1201 // (no unwrap/expect/panic), so this is an accepted low-probability
1202 // failure rather than a recoverable one. We keep the connection alive (a
1203 // single permanently-silent subscription is less harmful than tearing
1204 // down every other subscription and stream the connection multiplexes)
1205 // but log loudly so the silence is diagnosable. The log cannot storm:
1206 // it can only fire after the one panic that poisoned the lock.
1207 tracing::error!(
1208 %error,
1209 "subscription inbox lock is poisoned; this subscription is now \
1210 permanently silent and will deliver no further messages"
1211 );
1212 None
1213 }
1214 }
1215 }
1216}
1217
1218pub(super) fn server_error_from_protocol(error: &ProtocolError) -> ServerError {
1219 ServerError::ListenerAccept {
1220 message: format!("protocol operation failed: {error}"),
1221 }
1222}
1223
1224#[cfg(test)]
1225#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
1226mod durable_store_tests {
1227 use liminal::durability::open_ephemeral_rooted;
1228
1229 use super::subsystem_census::RecordingSubsystems;
1230 use super::{
1231 ConnectionServices, DEFAULT_SHARD_COUNT, LiminalConnectionServices, SchedulerSubsystem,
1232 build_connection_services, build_connection_services_via, build_durable_store_with,
1233 };
1234 use crate::ServerError;
1235 use crate::config::types::{LimitsConfig, ServerConfig, ServicesConfig};
1236
1237 /// Counts directory entries under `root`, for the empty/one-dir assertions
1238 /// on an injected ephemeral root.
1239 fn entry_count(root: &std::path::Path) -> usize {
1240 std::fs::read_dir(root)
1241 .expect("ephemeral root is readable")
1242 .count()
1243 }
1244
1245 /// A minimal channel-free config with the given service `profile`. No channels,
1246 /// routing, persistence, or cluster — the shape both profiles accept (the full
1247 /// profile simply builds an empty channel set; the worker-front-door profile
1248 /// requires exactly this shape).
1249 fn config_with_profile(profile: &str) -> ServerConfig {
1250 ServerConfig {
1251 listen_address: "127.0.0.1:0".parse().expect("valid socket addr"),
1252 health_listen_address: "127.0.0.1:1".parse().expect("valid socket addr"),
1253 drain_timeout_ms: 30_000,
1254 channels: Vec::new(),
1255 routing_rules: Vec::new(),
1256 persistence_path: None,
1257 cluster: None,
1258 auth: None,
1259 services: ServicesConfig {
1260 profile: profile.to_owned(),
1261 },
1262 limits: LimitsConfig::default(),
1263 participant: None,
1264 websocket: None,
1265 }
1266 }
1267
1268 /// §9 D2 front-door construction gate (fs half): building the worker-front-door
1269 /// services creates NO haematite store and NO temp dir, while the full profile
1270 /// over an equally-rooted factory DOES create exactly one store directory.
1271 ///
1272 /// The injected root is the only place an ephemeral store directory can appear
1273 /// (the recording factory roots its stores there), so "the root stays empty on
1274 /// the front-door branch" is a real negative assertion: a regression that gave
1275 /// the front door a store would land its directory here and fail this test. The
1276 /// full-profile arm is the positive control proving the seam genuinely
1277 /// constructs a store when the profile asks for one.
1278 #[test]
1279 fn worker_front_door_builds_no_store_and_no_temp_dir() {
1280 let front_door_root = tempfile::tempdir().expect("test can create an ephemeral root");
1281 let full_root = tempfile::tempdir().expect("test can create an ephemeral root");
1282
1283 let front_door_subsystems = RecordingSubsystems::rooted(front_door_root.path());
1284 let front_door: std::sync::Arc<dyn ConnectionServices> = build_connection_services_via(
1285 &config_with_profile("worker-front-door"),
1286 &front_door_subsystems,
1287 )
1288 .expect("worker-front-door services build");
1289 assert!(
1290 !front_door.supports_channel_operations(),
1291 "the worker front door serves no channel operations"
1292 );
1293 assert_eq!(
1294 entry_count(front_door_root.path()),
1295 0,
1296 "the worker front door creates no ephemeral store directory (no haematite, no temp dir)"
1297 );
1298
1299 let full_subsystems = RecordingSubsystems::rooted(full_root.path());
1300 let full = build_connection_services_via(&config_with_profile("full"), &full_subsystems)
1301 .expect("full services build");
1302 assert!(
1303 full.supports_channel_operations(),
1304 "full mode serves channel operations"
1305 );
1306 assert_eq!(
1307 entry_count(full_root.path()),
1308 1,
1309 "full mode with no persistence path builds exactly one ephemeral store directory"
1310 );
1311
1312 drop(front_door);
1313 drop(full);
1314 }
1315
1316 /// §9 D2 front-door construction gate (thread half — record-by-construction
1317 /// census): the worker profile constructs NO channel-supervisor,
1318 /// conversation-supervisor, or haematite scheduler, while the SAME instrument
1319 /// over the full profile records all three — the positive control proving the
1320 /// census detects the extra schedulers, so an empty census on the worker branch
1321 /// is a real observation, not a decoration.
1322 ///
1323 /// The instrument's boundary: recording happens INSIDE the [`SubsystemFactory`]
1324 /// methods that are the profile-aware path's only route to these constructors,
1325 /// so a recording cannot be silently omitted — a future subsystem added to this
1326 /// path either goes through the factory (and is recorded by construction) or
1327 /// bypasses it, which is a code-review-visible structural violation of the
1328 /// factory seam, not a silently-missing side call. The connection supervisor's
1329 /// own scheduler is the shared baseline of both profiles and is asserted at the
1330 /// supervisor level (`supervisor::tests`); an OS-level thread census upgrades
1331 /// this when the beamr composition lane's scheduler-inventory API (currently on
1332 /// their branch, not yet consumable from liminal) lands.
1333 #[test]
1334 fn worker_profile_census_is_empty_and_full_profile_records_all_schedulers() {
1335 let worker_root = tempfile::tempdir().expect("test can create an ephemeral root");
1336 let full_root = tempfile::tempdir().expect("test can create an ephemeral root");
1337
1338 let worker_subsystems = RecordingSubsystems::rooted(worker_root.path());
1339 let front_door = build_connection_services_via(
1340 &config_with_profile("worker-front-door"),
1341 &worker_subsystems,
1342 )
1343 .expect("worker-front-door services build");
1344 assert_eq!(
1345 worker_subsystems.recorded(),
1346 Vec::<SchedulerSubsystem>::new(),
1347 "the worker front door constructs no scheduler-owning subsystem"
1348 );
1349
1350 let full_subsystems = RecordingSubsystems::rooted(full_root.path());
1351 let full = build_connection_services_via(&config_with_profile("full"), &full_subsystems)
1352 .expect("full services build");
1353 assert_eq!(
1354 full_subsystems.recorded(),
1355 vec![
1356 SchedulerSubsystem::ChannelSupervisor,
1357 SchedulerSubsystem::ConversationSupervisor,
1358 SchedulerSubsystem::HaematiteStore,
1359 ],
1360 "the full profile constructs every scheduler-owning subsystem, once each — \
1361 the positive control proving the census instrument detects them"
1362 );
1363
1364 drop(front_door);
1365 drop(full);
1366 }
1367
1368 /// MAJOR-1 regression: the full-only constructors reject a worker-front-door
1369 /// config with a typed `ConfigValidation` error AT ENTRY — no full service can
1370 /// be created through any public config-based constructor under that profile.
1371 #[test]
1372 fn full_only_constructors_reject_worker_profile() {
1373 let config = config_with_profile("worker-front-door");
1374
1375 let from_config = LiminalConnectionServices::from_config(&config);
1376 assert!(
1377 matches!(from_config, Err(ServerError::ConfigValidation { .. })),
1378 "from_config must reject a worker-front-door profile with ConfigValidation, got {from_config:?}"
1379 );
1380
1381 let root = tempfile::tempdir().expect("test can create an ephemeral root");
1382 let store = open_ephemeral_rooted(root.path(), DEFAULT_SHARD_COUNT)
1383 .expect("test store for the rejection check builds");
1384 let from_config_with_store =
1385 LiminalConnectionServices::from_config_with_store(&config, std::sync::Arc::new(store));
1386 assert!(
1387 matches!(
1388 from_config_with_store,
1389 Err(ServerError::ConfigValidation { .. })
1390 ),
1391 "from_config_with_store must reject a worker-front-door profile with ConfigValidation"
1392 );
1393 }
1394
1395 /// MAJOR-1 regression: the profile-aware factory itself re-runs the
1396 /// worker-front-door cross-field checks, so a directly-constructed config (one
1397 /// that never passed file-loading validation) combining the worker profile with
1398 /// full-only machinery is refused with the same typed `ConfigValidation` errors.
1399 #[test]
1400 fn build_connection_services_rejects_worker_profile_with_full_only_fields() {
1401 let mut config = config_with_profile("worker-front-door");
1402 config.channels = vec![crate::config::types::ChannelDef {
1403 name: "orders".to_owned(),
1404 schema_ref: None,
1405 durable: false,
1406 loaded_schema: None,
1407 }];
1408 config.persistence_path = Some(std::path::PathBuf::from("/tmp"));
1409
1410 let result = build_connection_services(&config);
1411 let Err(ServerError::ConfigValidation { message }) = result else {
1412 panic!("expected ConfigValidation for worker profile with full-only fields");
1413 };
1414 assert!(message.contains("builds no channels"), "got: {message}");
1415 assert!(
1416 message.contains("builds no durable store"),
1417 "got: {message}"
1418 );
1419 }
1420
1421 /// §9 D3 construction gate (persistent half): requesting a *persistent* store
1422 /// creates its database under the configured path and NO ephemeral directory.
1423 ///
1424 /// Exercises `build_durable_store_with` — the same branch logic production
1425 /// runs — with only the ephemeral factory swapped to root in an isolated
1426 /// directory. That root is where any ephemeral directory would have to
1427 /// appear, so "the root stays empty" is a real negative assertion — a
1428 /// regression that constructs an ephemeral store on the persistent branch
1429 /// lands its directory here and fails this test.
1430 #[test]
1431 fn persistent_store_uses_configured_path_and_creates_no_temp_dir() {
1432 let home = tempfile::tempdir().expect("test can create a temp dir");
1433 let ephemeral_root = tempfile::tempdir().expect("test can create an ephemeral root");
1434
1435 let store = build_durable_store_with(Some(home.path()), || {
1436 open_ephemeral_rooted(ephemeral_root.path(), DEFAULT_SHARD_COUNT)
1437 })
1438 .expect("persistent store builds");
1439
1440 assert!(
1441 home.path().join("durability").join("config.json").exists(),
1442 "the persistent database is created under the configured path"
1443 );
1444 assert_eq!(
1445 entry_count(ephemeral_root.path()),
1446 0,
1447 "the persistent branch creates no ephemeral guard directory"
1448 );
1449
1450 drop(store);
1451 }
1452
1453 /// Pins the wiring seam: the ephemeral (`None`) branch of the shared build
1454 /// logic goes through the guarded constructor — exactly one directory
1455 /// appears under the injected root while the store lives, and zero residue
1456 /// remains after the last handle drops.
1457 #[test]
1458 fn ephemeral_store_directory_is_owned_through_the_build_seam() {
1459 let ephemeral_root = tempfile::tempdir().expect("test can create an ephemeral root");
1460
1461 let store = build_durable_store_with(None, || {
1462 open_ephemeral_rooted(ephemeral_root.path(), DEFAULT_SHARD_COUNT)
1463 })
1464 .expect("ephemeral store builds");
1465
1466 assert_eq!(
1467 entry_count(ephemeral_root.path()),
1468 1,
1469 "the ephemeral branch creates exactly one guard directory"
1470 );
1471
1472 drop(store);
1473
1474 assert_eq!(
1475 entry_count(ephemeral_root.path()),
1476 0,
1477 "dropping the last store handle removes the guard directory — zero residue"
1478 );
1479 }
1480}