feat(sidecar): WIT al sidecar — módulos conscientes vivos
Cierra el ciclo brahman-card-wit ↔ runtime: un módulo que tenga su .wit lo parsea, lo manda en Hello, y aparece como "consciente" en el broker y en brahman-status. Cambios coordinados (un solo commit por la cadena de tipos): - brahman-card::WitInterface deriva Serialize/Deserialize/Eq. - brahman-handshake::Hello lleva wit: Option<WitInterface> (#[serde(default)] para tolerar Hellos antiguos en formato JSON aunque postcard exige presencia explícita). - Server's register_session enruta a ResolvedCard::from_conscious cuando viene wit; from_agnostic cuando no. - Client::connect queda como wrapper de connect_with(path, card, wit: Option<WitInterface>) — backward-compatible. - Broker::register acepta Option<WitInterface> como tercer arg; BrokeredCard guarda el wit. 25 sitios de tests actualizados con `, None` (vía perl). - brahman-sidecar::SidecarConfig.wit + helpers SidecarConfig::with_wit y spawn_conscious(card, wit). Log attached reporta conscious=true|false. - brahman-status pretty-print con 🧠 + sección wit (package/world + imports + exports) por sesión consciente. - Example nuevo presence-conscious: parsea protocol.wit y se presenta consciente. Validación end-to-end manual: $ ente-zero & $ presence-conscious demo.conscious shared_wit/protocol.wit & $ brahman-status Sessions (1): 01K... demo.conscious 🧠 lifecycle=Daemon wit: brahman:protocol@0.1.0 / module imports: types, handshake, lifecycle exports: run Tests: 32/32 (broker 11 + card 8 + handshake codec+transport 2 + integ 7 + admin 0 + card-wit 4). Workspace: 0 errores. CHANGELOG.md actualizado. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ use std::collections::VecDeque;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use brahman_card::{Card, CARD_SCHEMA_VERSION};
|
||||
use brahman_card::{Card, WitInterface, CARD_SCHEMA_VERSION};
|
||||
use thiserror::Error;
|
||||
use tokio::net::UnixStream;
|
||||
|
||||
@@ -43,9 +43,20 @@ pub struct Client {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Conecta al socket, envía Hello con la Card dada y procesa la respuesta.
|
||||
/// Conecta como módulo agnóstico (sin WIT). Equivalente a
|
||||
/// `connect_with(path, card, None)`.
|
||||
pub async fn connect(path: impl AsRef<Path>, card: Card) -> Result<Self, ClientError> {
|
||||
// Pre-validamos para fallar local antes de hablar con el servidor.
|
||||
Self::connect_with(path, card, None).await
|
||||
}
|
||||
|
||||
/// Conecta al socket enviando Hello con la Card dada y opcionalmente
|
||||
/// una `WitInterface` ya extraída. Si `wit` es `Some`, el server
|
||||
/// registra el módulo como "consciente".
|
||||
pub async fn connect_with(
|
||||
path: impl AsRef<Path>,
|
||||
card: Card,
|
||||
wit: Option<WitInterface>,
|
||||
) -> Result<Self, ClientError> {
|
||||
card.validate()
|
||||
.map_err(|e| ClientError::InvalidCard(e.to_string()))?;
|
||||
|
||||
@@ -54,6 +65,7 @@ impl Client {
|
||||
schema_version: CARD_SCHEMA_VERSION,
|
||||
protocol_version: brahman_card::PROTOCOL_VERSION.to_string(),
|
||||
card,
|
||||
wit,
|
||||
};
|
||||
write_frame(&mut stream, &Frame::Hello(hello)).await?;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! Todos los mensajes que cruzan el wire son variantes de [`Frame`].
|
||||
|
||||
use brahman_broker::MatchStrategy;
|
||||
use brahman_card::TypeRef;
|
||||
use brahman_card::{TypeRef, WitInterface};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
|
||||
@@ -11,7 +11,9 @@ use ulid::Ulid;
|
||||
pub type SessionId = Ulid;
|
||||
|
||||
/// Saludo inicial del módulo. Lleva la Card completa para que el servidor
|
||||
/// la valide e indexe.
|
||||
/// la valide e indexe. Opcionalmente, una `WitInterface` ya extraída — si
|
||||
/// está presente, el módulo es "consciente" y el server lo registra como
|
||||
/// `ResolvedCard::from_conscious`; si no, como `from_agnostic`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Hello {
|
||||
/// Versión del schema de Card que el cliente sigue.
|
||||
@@ -20,6 +22,10 @@ pub struct Hello {
|
||||
pub protocol_version: String,
|
||||
/// Tarjeta de Presentación.
|
||||
pub card: brahman_card::Card,
|
||||
/// Interfaz WIT extraída por el cliente (típicamente con
|
||||
/// `brahman-card-wit`). `None` si el módulo es agnóstico.
|
||||
#[serde(default)]
|
||||
pub wit: Option<WitInterface>,
|
||||
}
|
||||
|
||||
/// Respuesta del servidor a un `Hello` aceptado.
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::sync::Arc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use brahman_broker::{Broker, Endpoint};
|
||||
use brahman_card::{Card, ResolvedCard, CARD_SCHEMA_VERSION};
|
||||
use brahman_card::{Card, ResolvedCard, WitInterface, CARD_SCHEMA_VERSION};
|
||||
use tokio::net::{UnixListener, UnixStream};
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use tracing::{debug, warn};
|
||||
@@ -367,7 +367,7 @@ impl Session {
|
||||
}
|
||||
|
||||
let session_id = Ulid::new();
|
||||
self.register_session(session_id, hello.card).await;
|
||||
self.register_session(session_id, hello.card, hello.wit).await;
|
||||
|
||||
let ack = HelloAck {
|
||||
server_version: crate::HANDSHAKE_VERSION.to_string(),
|
||||
@@ -381,11 +381,23 @@ impl Session {
|
||||
}
|
||||
|
||||
/// Indexa la sesión: ResolvedCard en sessions + Card en broker (si hay).
|
||||
async fn register_session(&self, session_id: SessionId, card: Card) {
|
||||
/// Si `wit` está presente, el módulo se registra como "consciente".
|
||||
async fn register_session(
|
||||
&self,
|
||||
session_id: SessionId,
|
||||
card: Card,
|
||||
wit: Option<WitInterface>,
|
||||
) {
|
||||
if let Some(broker) = &self.config.broker {
|
||||
broker.lock().await.register(session_id, &card);
|
||||
broker
|
||||
.lock()
|
||||
.await
|
||||
.register(session_id, &card, wit.clone());
|
||||
}
|
||||
let resolved = ResolvedCard::from_agnostic(card);
|
||||
let resolved = match wit {
|
||||
Some(w) => ResolvedCard::from_conscious(card, w),
|
||||
None => ResolvedCard::from_agnostic(card),
|
||||
};
|
||||
self.sessions.lock().await.insert(session_id, resolved);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user