Skip to main content

reddb_wire/
knowledge.rs

1//! Agent-facing connection-model knowledge reference.
2//!
3//! Supported URI schemes are generated from the connection layer's
4//! [`crate::conn_string::SUPPORTED_SCHEMES`] catalog. The deployment topology
5//! guidance is hand-authored narrative per ADR 0061.
6
7use crate::conn_string::{ConnectionScheme, SUPPORTED_SCHEMES};
8
9/// Canonical URI for the connection-model knowledge resource served over MCP.
10pub const RESOURCE_URI: &str = "reddb://knowledge/connections";
11
12/// Short human title for the connection knowledge resource.
13pub const RESOURCE_TITLE: &str = "RedDB Connection Model";
14
15/// One-line description of the connection knowledge resource.
16pub const RESOURCE_DESCRIPTION: &str =
17    "Generated URI scheme/transport catalog plus the RedDB deployment topology guide.";
18
19/// Markers delimiting the generated connection block inside `docs/llms.txt`.
20pub const LLMS_BEGIN_MARKER: &str = "<!-- BEGIN GENERATED: connections -->";
21/// Closing marker for the generated connection block in `docs/llms.txt`.
22pub const LLMS_END_MARKER: &str = "<!-- END GENERATED: connections -->";
23
24struct Topology {
25    name: &'static str,
26    summary: &'static str,
27}
28
29const TOPOLOGIES: &[Topology] = &[
30    Topology {
31        name: "Embedded / standalone",
32        summary: "Embedded connections use RAM (`memory://`), a temporary database file \
33(`tmp://`), or a durable file (`file://`). A standalone server exposes the engine over \
34RedWire, gRPC, and HTTP.",
35    },
36    Topology {
37        name: "Serverless",
38        summary: "Serverless callers usually keep no local state and connect to a managed \
39endpoint. Prefer `reds://` when RedWire is reachable; use HTTPS/gRPC only when the platform \
40requires those transports.",
41    },
42    Topology {
43        name: "Primary-replica",
44        summary: "A primary accepts writes while replicas serve read traffic after replication. \
45Multi-host remote URIs express primary-first routing, with `?route=primary` available when reads \
46must stay on the primary.",
47    },
48    Topology {
49        name: "Cluster",
50        summary: "Cluster deployments coordinate multiple RedDB nodes behind the same logical \
51database. RedWire (`red://` / `reds://`) is the principal data-plane transport; gRPC and HTTP are \
52compatibility/admin surfaces, not the only way to connect.",
53    },
54];
55
56/// Full Markdown body served by the MCP resource.
57pub fn connection_reference_markdown() -> String {
58    let mut out = String::new();
59    push_intro(&mut out);
60    push_supported_schemes(&mut out);
61    push_topologies(&mut out);
62    out
63}
64
65/// The connection block as embedded in `docs/llms.txt`.
66pub fn connection_llms_section() -> String {
67    format!(
68        "{LLMS_BEGIN_MARKER}\n{}\n{LLMS_END_MARKER}",
69        connection_reference_markdown().trim_end()
70    )
71}
72
73fn push_intro(out: &mut String) {
74    out.push_str("# RedDB Connection Model\n\n");
75    out.push_str(
76        "RedWire (`red://` / `reds://`) is RedDB's principal transport. \
77gRPC, HTTP(S), browser WebSocket variants, `memory://`, `tmp://`, and `file://` are supported connection \
78surfaces, but agents should not collapse the model to \"only gRPC\" or \"only WebSocket\".\n\n",
79    );
80}
81
82fn push_supported_schemes(out: &mut String) {
83    out.push_str("## Supported URI Schemes and Transports\n\n");
84    out.push_str("| URI scheme | Transport | Mode | Example | Notes |\n");
85    out.push_str("|---|---|---|---|---|\n");
86    for scheme in SUPPORTED_SCHEMES {
87        push_scheme_row(out, *scheme);
88    }
89    out.push('\n');
90}
91
92fn push_scheme_row(out: &mut String, scheme: ConnectionScheme) {
93    out.push_str("| `");
94    out.push_str(scheme.uri_prefix());
95    out.push_str("` | ");
96    out.push_str(scheme.transport());
97    out.push_str(" | ");
98    out.push_str(scheme.mode());
99    out.push_str(" | `");
100    out.push_str(scheme.example());
101    out.push_str("` | ");
102    out.push_str(scheme.notes());
103    out.push_str(" |\n");
104}
105
106fn push_topologies(out: &mut String) {
107    out.push_str("## Deployment Topologies\n\n");
108    for topology in TOPOLOGIES {
109        out.push_str("### ");
110        out.push_str(topology.name);
111        out.push_str("\n\n");
112        out.push_str(topology.summary);
113        out.push_str("\n\n");
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn supported_scheme_examples_parse() {
123        for scheme in SUPPORTED_SCHEMES {
124            crate::conn_string::parse(scheme.example()).unwrap_or_else(|err| {
125                panic!("{} example did not parse: {err}", scheme.uri_prefix())
126            });
127        }
128    }
129
130    #[test]
131    fn reference_lists_supported_schemes_from_connection_layer() {
132        let markdown = connection_reference_markdown();
133        for scheme in SUPPORTED_SCHEMES {
134            let needle = format!("`{}`", scheme.uri_prefix());
135            assert!(markdown.contains(&needle), "missing {needle}");
136        }
137    }
138
139    #[test]
140    fn reference_covers_principal_transport_and_topologies() {
141        let markdown = connection_reference_markdown();
142        assert!(markdown.contains("principal transport"));
143        assert!(markdown.contains("RedWire (`red://` / `reds://`)"));
144        for topology in TOPOLOGIES {
145            assert!(
146                markdown.contains(topology.name),
147                "missing {}",
148                topology.name
149            );
150        }
151    }
152
153    #[test]
154    fn llms_section_wraps_reference() {
155        let section = connection_llms_section();
156        assert!(section.starts_with(LLMS_BEGIN_MARKER));
157        assert!(section.ends_with(LLMS_END_MARKER));
158        let reference = connection_reference_markdown();
159        assert!(section.contains(reference.trim_end()));
160    }
161}