Skip to main content

qualia_core_db/poet_host/
catalog.rs

1//! Vibe 0.1 binding table over Qualia's existing capability registry.
2//!
3//! Humans and apps reach the engine through these IDs. MCP is a separate
4//! bot door; it must not be the only listing.
5
6use crate::{vibe_host::bridge, CapabilityDescriptor, CAPABILITY_DESCRIPTORS};
7use std::collections::BTreeMap;
8use vibe::Value;
9
10/// A 0.1 Vibe binding and whether the live host actually implements it.
11#[derive(Debug, Clone, Copy)]
12pub struct VibeBinding {
13    pub id: &'static str,
14    pub family: &'static str,
15    pub required: bool,
16    pub honesty: &'static str,
17}
18
19/// Required ยง11 bindings plus honesty. Unbound engine families stay MCP/CLI-only until wired.
20pub const VIBE_0_1: &[VibeBinding] = &[
21    VibeBinding {
22        id: "math.abs",
23        family: "CapabilityDiscovery",
24        required: true,
25        honesty: "live",
26    },
27    VibeBinding {
28        id: "math.min",
29        family: "CapabilityDiscovery",
30        required: true,
31        honesty: "live",
32    },
33    VibeBinding {
34        id: "math.max",
35        family: "CapabilityDiscovery",
36        required: true,
37        honesty: "live",
38    },
39    VibeBinding {
40        id: "rdf.triple",
41        family: "GraphDatabase",
42        required: true,
43        honesty: "live",
44    },
45    VibeBinding {
46        id: "rdf.reify",
47        family: "GraphDatabase",
48        required: true,
49        honesty: "live",
50    },
51    VibeBinding {
52        id: "quin.statement",
53        family: "GraphDatabase",
54        required: true,
55        honesty: "live",
56    },
57    VibeBinding {
58        id: "graph.read",
59        family: "GraphDatabase",
60        required: true,
61        honesty: "partial",
62    },
63    VibeBinding {
64        id: "graph.write",
65        family: "GraphDatabase",
66        required: true,
67        honesty: "partial",
68    },
69    VibeBinding {
70        id: "aura.validate",
71        family: "SHACL",
72        required: true,
73        honesty: "partial",
74    },
75    VibeBinding {
76        id: "pulse.publish",
77        family: "CapabilityDiscovery",
78        required: true,
79        honesty: "partial",
80    },
81    VibeBinding {
82        id: "capability.resolve",
83        family: "CapabilityDiscovery",
84        required: true,
85        honesty: "live",
86    },
87    VibeBinding {
88        id: "capability.invoke",
89        family: "CapabilityDiscovery",
90        required: false,
91        honesty: "partial",
92    },
93    VibeBinding {
94        id: "time.unix",
95        family: "CapabilityDiscovery",
96        required: false,
97        honesty: "partial",
98    },
99];
100
101/// Bindings whose honesty flips to "live" when the host snapshot is attached
102/// to the live daemon graph. On WASM / detached, they stay "partial".
103const DYNAMIC_LIVE_WHEN_ATTACHED: &[&str] = &[
104    "graph.read",
105    "graph.write",
106    "aura.validate",
107    "pulse.publish",
108];
109
110/// Return the effective honesty label for a binding, given attachment state.
111///
112/// `graph.read`, `graph.write`, `aura.validate`, and `pulse.publish` are
113/// "partial" on detached/WASM hosts but "live" when attached to the daemon
114/// graph (queries refresh from the live graph, commits extend it, SHACL
115/// validates against it, and pulse publishes through the transport channel).
116/// All other bindings keep their static label.
117pub fn dynamic_honesty(id: &str, attached: bool) -> &'static str {
118    if attached && DYNAMIC_LIVE_WHEN_ATTACHED.contains(&id) {
119        return "live";
120    }
121    VIBE_0_1
122        .iter()
123        .find(|b| b.id == id)
124        .map(|b| b.honesty)
125        .unwrap_or("unbound")
126}
127
128/// Resolves a catalog `id` to a descriptor `Value`.
129pub fn resolve_id(id: &str) -> Value {
130    resolve_id_with(id, false)
131}
132
133/// Resolves `id` with `attached` flag.
134pub fn resolve_id_with(id: &str, attached: bool) -> Value {
135    let binding = VIBE_0_1.iter().find(|b| b.id == id);
136    let family = binding.map(|b| b.family);
137    let desc = family.and_then(|name| CAPABILITY_DESCRIPTORS.iter().find(|d| d.name == name));
138    let honesty = dynamic_honesty(id, attached);
139    let mut rec = BTreeMap::new();
140    rec.insert("id".into(), Value::String(id.into()));
141    rec.insert("vibe_bound".into(), Value::Bool(binding.is_some()));
142    rec.insert("honesty".into(), Value::String(honesty.to_string()));
143    rec.insert("human_surface".into(), Value::String("poet".into()));
144    rec.insert("execution_host".into(), Value::String("vibe-host".into()));
145    let bridge_metadata = bridge::capability_json(id, attached);
146    if let Some(mode) = bridge_metadata.get("mode").and_then(|value| value.as_str()) {
147        rec.insert("execution_mode".into(), Value::String(mode.into()));
148    }
149    if let Some(semantics) = bridge_metadata
150        .get("semantics")
151        .and_then(|value| value.as_str())
152    {
153        rec.insert("semantics".into(), Value::String(semantics.into()));
154    }
155    if let Some(requires_native) = bridge_metadata
156        .get("requires_native")
157        .and_then(|value| value.as_bool())
158    {
159        rec.insert("requires_native".into(), Value::Bool(requires_native));
160    }
161    if let Some(b) = binding {
162        rec.insert("required".into(), Value::Bool(b.required));
163        rec.insert("family".into(), Value::String(b.family.into()));
164    }
165    if let Some(d) = desc {
166        rec.insert("maturity".into(), Value::String(d.maturity.into()));
167        rec.insert(
168            "mcp_tools".into(),
169            Value::List(
170                d.mcp_tools
171                    .iter()
172                    .map(|t| Value::String((*t).into()))
173                    .collect(),
174            ),
175        );
176    }
177    Value::Record(rec)
178}
179
180/// Capability descriptors exposed only via MCP.
181pub fn engine_families_mcp_only() -> Vec<&'static CapabilityDescriptor> {
182    CAPABILITY_DESCRIPTORS
183        .iter()
184        .filter(|d| !VIBE_0_1.iter().any(|b| b.family == d.name) && !d.mcp_tools.is_empty())
185        .collect()
186}
187
188#[cfg(test)]
189mod tests {
190    use super::*;
191
192    #[test]
193    fn graph_read_resolves_as_partial_vibe_when_detached() {
194        match resolve_id("graph.read") {
195            Value::Record(r) => {
196                assert_eq!(r.get("vibe_bound"), Some(&Value::Bool(true)));
197                assert_eq!(r.get("honesty"), Some(&Value::String("partial".into())));
198            }
199            other => panic!("{other:?}"),
200        }
201    }
202
203    #[test]
204    fn graph_read_is_live_when_attached() {
205        match resolve_id_with("graph.read", true) {
206            Value::Record(r) => {
207                assert_eq!(r.get("honesty"), Some(&Value::String("live".into())));
208            }
209            other => panic!("{other:?}"),
210        }
211    }
212
213    #[test]
214    fn pulse_publish_is_live_when_attached() {
215        match resolve_id_with("pulse.publish", true) {
216            Value::Record(r) => {
217                assert_eq!(r.get("honesty"), Some(&Value::String("live".into())));
218            }
219            other => panic!("{other:?}"),
220        }
221    }
222
223    #[test]
224    fn capability_invoke_stays_partial_even_when_attached() {
225        match resolve_id_with("capability.invoke", true) {
226            Value::Record(r) => {
227                assert_eq!(r.get("honesty"), Some(&Value::String("partial".into())));
228            }
229            other => panic!("{other:?}"),
230        }
231    }
232
233    #[test]
234    fn deontic_is_not_a_vibe_0_1_keyword() {
235        match resolve_id("logic.deontic.evaluate") {
236            Value::Record(r) => {
237                assert_eq!(r.get("vibe_bound"), Some(&Value::Bool(false)));
238            }
239            other => panic!("{other:?}"),
240        }
241    }
242
243    #[test]
244    fn detached_graph_write_is_marked_as_an_isolated_snapshot() {
245        match resolve_id("graph.write") {
246            Value::Record(r) => {
247                assert_eq!(
248                    r.get("execution_mode"),
249                    Some(&Value::String("standalone-snapshot".into()))
250                );
251                assert_eq!(
252                    r.get("semantics"),
253                    Some(&Value::String("isolated-snapshot".into()))
254                );
255            }
256            other => panic!("{other:?}"),
257        }
258    }
259}