aimdb_core/remote/mod.rs
1//! Remote access subsystem for AimDB (AimX protocol)
2//!
3//! Provides introspection and management APIs over Unix domain sockets,
4//! enabling external tools (CLI, dashboards, MCP adapters) to interact
5//! with running AimDB instances.
6//!
7//! # Protocol
8//!
9//! AimX uses NDJSON (newline-delimited JSON) tagged frames over a session
10//! transport (Unix domain sockets via `aimdb-uds-connector`, serial via
11//! `aimdb-serial-connector`). The envelope codec lives in `session::aimx`
12//! (feature `connector-session`). Compatibility is by major version — see
13//! [`PROTOCOL_VERSION`] and [`version_compatible`].
14//!
15//! # Security
16//!
17//! - **Read-only by default**: No writes unless explicitly enabled
18//! - **UDS permissions**: Primary security mechanism (file permissions)
19//! - **Optional auth tokens**: Additional authentication layer
20//! - **Per-record write permissions**: Explicit opt-in required
21//!
22//! # Usage
23//!
24//! Remote access is registered like any other connector — via `with_connector`
25//! using `aimdb_uds_connector::UdsServer` (this replaced the former
26//! `AimDbBuilder::with_remote_access(config)`). Illustrative (not compiled:
27//! the connector lives in a downstream crate `aimdb-core` cannot depend on):
28//!
29//! ```rust,ignore
30//! use aimdb_core::remote::{AimxConfig, SecurityPolicy};
31//! use aimdb_uds_connector::UdsServer;
32//!
33//! let config = AimxConfig::uds_default()
34//! .socket_path("/var/run/aimdb/aimdb.sock")
35//! .security_policy(SecurityPolicy::ReadOnly)
36//! .max_connections(16)
37//! .max_subs_per_connection(32);
38//!
39//! let (db, runner) = AimDbBuilder::new()
40//! .runtime(tokio_adapter)
41//! .with_connector(UdsServer::from_config(config))
42//! .build()
43//! .await?;
44//! ```
45
46mod config;
47mod error;
48mod instance;
49mod metadata;
50mod protocol;
51mod query;
52
53pub use config::{AimxConfig, SecurityPolicy};
54pub use error::{RemoteError, RemoteResult};
55pub use instance::{instance_info_reply, post_link_chunk_reply, PostLinkError};
56pub use metadata::{InboundKeysInfo, RecordMetadata};
57pub use protocol::{
58 version_compatible, ws_url_with_version, BuildReply, ErrorObject, Event, HelloMessage,
59 InstanceIdentityReply, InstanceInfoReply, PostLinkChunk, PostLinkReply, Request, Response,
60 WelcomeMessage, INSTANCE_INFO_REPLY_MAX, POST_LINK_CHUNK_MAX, PROTOCOL_VERSION, VERSION_PARAM,
61};
62pub use query::{QueryHandlerFn, QueryHandlerParams, QueryRecord, QUERY_ALL_PATTERN};
63
64// Internal exports for implementation
65#[cfg(feature = "connector-session")]
66pub(crate) mod stream;
67
68/// The leaf (entity) segment of a record key — the trailing component after the
69/// last separator. Both `.` and `/` are treated as delimiters, since keys use
70/// either convention (`temp.vienna` or `sensors/temp/vienna`), and both yield
71/// `vienna`. Servers report this as a record's `entity`, which identifies the
72/// node only where the leaf *is* the node: `station.17.temperature` yields
73/// `temperature`, not `17`.
74pub fn topic_leaf(key: &str) -> &str {
75 key.rsplit(['.', '/']).next().unwrap_or(key)
76}
77
78#[cfg(test)]
79mod topic_leaf_tests {
80 use super::topic_leaf;
81
82 #[test]
83 fn leaf_handles_both_separators() {
84 // Dot convention.
85 assert_eq!(topic_leaf("temp.vienna"), "vienna");
86 // Slash convention (the bug: `rsplit('.')` returned the whole key).
87 assert_eq!(topic_leaf("sensors/temp/vienna"), "vienna");
88 // Mixed — the last separator of either kind wins.
89 assert_eq!(topic_leaf("sensors/temp.vienna"), "vienna");
90 assert_eq!(topic_leaf("a.b/c"), "c");
91 // No separator: the whole key is its own leaf.
92 assert_eq!(topic_leaf("vienna"), "vienna");
93 }
94}