zeptoclaw 0.9.0

Ultra-lightweight personal AI assistant
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! r8r bridge integration for ZeptoClaw.
//!
//! This module handles the bidirectional event stream between ZeptoClaw and
//! [r8r](https://github.com/qhkm/r8r), the workflow-automation engine.
//!
//! # Submodules
//!
//! * [`events`] — Mirrored event types matching r8r's wire format.
//! * [`dedup`]  — Deduplicator for at-least-once delivery.
//! * [`approval`] — Approval routing and response parsing (Task 8).
//! * [`health`] — Health ping loop and CLI status (Task 9).

pub mod approval;
pub mod dedup;
pub mod events;
pub mod health;

pub use dedup::Deduplicator;
pub use events::{Ack, BridgeEvent, BridgeEventEnvelope};

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use futures::{SinkExt, StreamExt};
use tokio::sync::{mpsc, Mutex};
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::{HeaderValue, Request};
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
use tokio_tungstenite::tungstenite::Message as WsMessage;
use tracing::{info, warn};
use url::Url;

/// Maximum length for event IDs to prevent memory abuse.
const MAX_EVENT_ID_LEN: usize = 256;

/// Maximum WebSocket message size (1 MiB).
const MAX_WS_MESSAGE_SIZE: usize = 1024 * 1024;

// ---------------------------------------------------------------------------
// R8rBridge — WebSocket client for r8r event stream
// ---------------------------------------------------------------------------

/// WebSocket client that connects to r8r's `/api/ws/events` endpoint.
///
/// Provides:
/// - Automatic reconnection with exponential backoff.
/// - At-least-once deduplication via [`Deduplicator`].
/// - Event dispatch by type (health, approval, execution).
/// - Outbound message sending via an internal mpsc channel.
pub struct R8rBridge {
    /// WebSocket endpoint URL (e.g. `ws://localhost:8080/api/ws/events`).
    endpoint: String,
    /// Optional bearer token for authentication.
    token: Option<String>,
    /// Sender half of the outbound message channel (feeds the WS writer task).
    sender: Arc<Mutex<Option<mpsc::Sender<String>>>>,
    /// Deduplicator for at-least-once delivery.
    dedup: Arc<Mutex<Deduplicator>>,
    /// Last received health status event.
    health_status: Arc<Mutex<Option<BridgeEvent>>>,
    /// Whether the bridge is currently connected.
    connected: Arc<AtomicBool>,
}

fn build_ws_request(endpoint: &str, token: Option<&str>) -> Result<Request<()>, String> {
    let parsed = Url::parse(endpoint).map_err(|e| format!("Invalid endpoint URL: {e}"))?;
    let host = parsed
        .host_str()
        .ok_or_else(|| "Endpoint URL has no host".to_string())?;
    let host_with_port = if let Some(port) = parsed.port() {
        format!("{host}:{port}")
    } else {
        host.to_string()
    };

    let mut request = endpoint
        .into_client_request()
        .map_err(|e| format!("Failed to build WS request: {e}"))?;
    let host_header =
        HeaderValue::from_str(&host_with_port).map_err(|e| format!("Invalid Host header: {e}"))?;
    request.headers_mut().insert("Host", host_header);

    if let Some(token) = token {
        let auth_header = HeaderValue::from_str(&format!("Bearer {token}"))
            .map_err(|e| format!("Invalid Authorization header: {e}"))?;
        request.headers_mut().insert("Authorization", auth_header);
    }

    Ok(request)
}

/// Sanitize an endpoint URL for logging by stripping any embedded credentials.
fn sanitize_endpoint(endpoint: &str) -> String {
    Url::parse(endpoint)
        .ok()
        .map(|mut u| {
            let _ = u.set_password(None);
            let _ = u.set_username("");
            u.to_string()
        })
        .unwrap_or_else(|| "[invalid url]".to_string())
}

impl R8rBridge {
    /// Create a new bridge client.
    ///
    /// * `endpoint` — WebSocket URL (e.g. `ws://localhost:8080/api/ws/events`).
    /// * `token` — Optional bearer token for the `Authorization` header.
    pub fn new(endpoint: String, token: Option<String>) -> Self {
        Self {
            endpoint,
            token,
            sender: Arc::new(Mutex::new(None)),
            dedup: Arc::new(Mutex::new(Deduplicator::default())),
            health_status: Arc::new(Mutex::new(None)),
            connected: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Returns `true` if the bridge is currently connected.
    pub fn is_connected(&self) -> bool {
        self.connected.load(Ordering::Relaxed)
    }

    /// Establish a one-shot WebSocket connection.
    ///
    /// 1. Builds an HTTP upgrade request with `Authorization` and `Host` headers.
    /// 2. Connects via `tokio-tungstenite`.
    /// 3. Splits the stream into sender/receiver halves.
    /// 4. Spawns a writer task that reads from an internal mpsc channel.
    /// 5. Spawns a reader task that:
    ///    - Parses incoming text messages as [`BridgeEventEnvelope`].
    ///    - Deduplicates events via [`Deduplicator`].
    ///    - Sends acknowledgments back.
    ///    - Dispatches events by type.
    pub async fn connect(&self) -> Result<(), String> {
        let request = build_ws_request(&self.endpoint, self.token.as_deref())?;

        // Connect with message size limits to prevent memory exhaustion.
        let mut ws_config = WebSocketConfig::default();
        ws_config.max_message_size = Some(MAX_WS_MESSAGE_SIZE);
        ws_config.max_frame_size = Some(MAX_WS_MESSAGE_SIZE);
        let (ws_stream, _response) =
            tokio_tungstenite::connect_async_with_config(request, Some(ws_config), false)
                .await
                .map_err(|e| format!("WebSocket connection failed: {e}"))?;

        let (ws_write, ws_read) = ws_stream.split();

        // Create internal mpsc channel for outbound messages.
        let (tx, mut rx) = mpsc::channel::<String>(256);
        {
            let mut sender_guard = self.sender.lock().await;
            *sender_guard = Some(tx);
        }

        self.connected.store(true, Ordering::Relaxed);
        info!(
            "r8r bridge connected to {}",
            sanitize_endpoint(&self.endpoint)
        );

        // Spawn writer task: reads from mpsc, writes to WS.
        let ws_write = Arc::new(Mutex::new(ws_write));
        let ws_write_clone = Arc::clone(&ws_write);
        tokio::spawn(async move {
            while let Some(msg) = rx.recv().await {
                let mut writer = ws_write_clone.lock().await;
                if let Err(e) = writer.send(WsMessage::Text(msg.into())).await {
                    warn!("r8r bridge send error: {e}");
                    break;
                }
            }
        });

        // Spawn reader task: reads from WS, dispatches events.
        let dedup = Arc::clone(&self.dedup);
        let health_status = Arc::clone(&self.health_status);
        let connected = Arc::clone(&self.connected);
        let sender = Arc::clone(&self.sender);

        tokio::spawn(async move {
            let mut ws_read = ws_read;

            while let Some(msg_result) = ws_read.next().await {
                let msg = match msg_result {
                    Ok(m) => m,
                    Err(e) => {
                        warn!("r8r bridge receive error: {e}");
                        break;
                    }
                };

                let text = match msg {
                    WsMessage::Text(t) => t.to_string(),
                    WsMessage::Close(_) => {
                        info!("r8r bridge received close frame");
                        break;
                    }
                    _ => continue,
                };

                // Parse envelope.
                let envelope: BridgeEventEnvelope = match serde_json::from_str(&text) {
                    Ok(e) => e,
                    Err(e) => {
                        warn!("r8r bridge: failed to parse envelope: {e}");
                        continue;
                    }
                };

                // Reject oversized event IDs to prevent memory abuse.
                if envelope.id.len() > MAX_EVENT_ID_LEN {
                    warn!(
                        "r8r bridge: event ID exceeds {} chars, dropping",
                        MAX_EVENT_ID_LEN
                    );
                    continue;
                }

                // Ack every well-formed envelope, including duplicates, so replayed
                // deliveries are drained cleanly on reconnect.
                let ack = Ack {
                    event_id: envelope.id.clone(),
                };
                if let Ok(ack_json) = serde_json::to_string(&ack) {
                    let mut writer = ws_write.lock().await;
                    if let Err(e) = writer.send(WsMessage::Text(ack_json.into())).await {
                        warn!("r8r bridge: failed to send ack: {e}");
                    }
                }

                // Dedup check.
                {
                    let mut dd = dedup.lock().await;
                    if !dd.is_new(&envelope.id) {
                        info!("r8r bridge: acknowledged duplicate event {}", envelope.id);
                        continue;
                    }
                }

                // Dispatch by event type.
                match BridgeEvent::from_type_and_data(&envelope.event_type, &envelope.data) {
                    Ok(event) => match &event {
                        BridgeEvent::HealthStatus { .. } => {
                            let mut hs = health_status.lock().await;
                            *hs = Some(event);
                        }
                        BridgeEvent::ApprovalRequested {
                            workflow,
                            execution_id,
                            ..
                        } => {
                            info!(
                                "r8r bridge: approval requested for {} ({})",
                                workflow, execution_id
                            );
                        }
                        BridgeEvent::ApprovalTimeout {
                            workflow,
                            execution_id,
                            ..
                        } => {
                            info!(
                                "r8r bridge: approval timeout for {} ({})",
                                workflow, execution_id
                            );
                        }
                        BridgeEvent::ExecutionCompleted {
                            workflow,
                            execution_id,
                            ..
                        } => {
                            info!(
                                "r8r bridge: execution completed for {} ({})",
                                workflow, execution_id
                            );
                        }
                        BridgeEvent::ExecutionFailed {
                            workflow,
                            execution_id,
                            ..
                        } => {
                            info!(
                                "r8r bridge: execution failed for {} ({})",
                                workflow, execution_id
                            );
                        }
                        _ => {
                            info!("r8r bridge: received event type {}", envelope.event_type);
                        }
                    },
                    Err(e) => {
                        warn!("r8r bridge: failed to parse event: {e}");
                    }
                }
            }

            // Disconnected — clean up.
            connected.store(false, Ordering::Relaxed);
            let mut sender_guard = sender.lock().await;
            *sender_guard = None;
            info!("r8r bridge disconnected");
        });

        Ok(())
    }

    /// Disconnect from the bridge, clearing the sender and setting connected to false.
    pub async fn disconnect(&self) {
        let mut sender_guard = self.sender.lock().await;
        *sender_guard = None;
        self.connected.store(false, Ordering::Relaxed);
        info!("r8r bridge disconnected (manual)");
    }

    /// Send a serialized envelope through the WebSocket.
    ///
    /// Returns an error if not connected or the channel is full.
    pub async fn send(&self, envelope: BridgeEventEnvelope) -> Result<(), String> {
        let json = serde_json::to_string(&envelope)
            .map_err(|e| format!("Failed to serialize envelope: {e}"))?;

        let sender_guard = self.sender.lock().await;
        match sender_guard.as_ref() {
            Some(tx) => tx
                .send(json)
                .await
                .map_err(|e| format!("Failed to send message: {e}")),
            None => Err("Not connected".to_string()),
        }
    }

    /// Send a health ping to the r8r server.
    pub async fn send_health_ping(&self) -> Result<(), String> {
        let envelope = BridgeEventEnvelope::new(BridgeEvent::HealthPing, None);
        self.send(envelope).await
    }

    /// Return the last received health status event, if any.
    pub async fn last_health_status(&self) -> Option<BridgeEvent> {
        let hs = self.health_status.lock().await;
        hs.clone()
    }

    /// Run a reconnection loop with exponential backoff.
    ///
    /// * `max_interval_secs` — Maximum backoff interval in seconds.
    ///
    /// This method runs forever, reconnecting on disconnection.
    pub async fn run(&self, max_interval_secs: u64) {
        let mut backoff_secs: u64 = 1;

        loop {
            match self.connect().await {
                Ok(()) => {
                    backoff_secs = 1;
                    // Wait until disconnected.
                    while self.is_connected() {
                        tokio::time::sleep(Duration::from_millis(250)).await;
                    }
                }
                Err(e) => {
                    warn!("r8r bridge connection failed: {e}");
                }
            }

            tokio::time::sleep(Duration::from_secs(backoff_secs)).await;
            backoff_secs = (backoff_secs * 2).min(max_interval_secs);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_ws_request_sets_host_and_authorization_headers() {
        let request =
            build_ws_request("ws://localhost:8080/api/ws/events", Some("secret-token")).unwrap();

        assert_eq!(request.headers()["Host"], "localhost:8080");
        assert_eq!(request.headers()["Authorization"], "Bearer secret-token");
    }

    #[test]
    fn test_build_ws_request_rejects_invalid_authorization_header() {
        let err = build_ws_request("ws://localhost:8080/api/ws/events", Some("bad\nvalue"))
            .expect_err("invalid header should return an error");
        assert!(err.contains("Authorization"));
    }

    #[test]
    fn test_sanitize_endpoint_strips_credentials() {
        let sanitized = sanitize_endpoint("ws://user:secret@host:8080/path");
        assert!(!sanitized.contains("secret"), "password should be stripped");
        assert!(!sanitized.contains("user"), "username should be stripped");
        assert!(sanitized.contains("host:8080/path"));
    }

    #[test]
    fn test_sanitize_endpoint_passes_clean_url() {
        let sanitized = sanitize_endpoint("ws://localhost:8080/api/ws/events");
        assert_eq!(sanitized, "ws://localhost:8080/api/ws/events");
    }

    #[test]
    fn test_sanitize_endpoint_handles_invalid_url() {
        assert_eq!(sanitize_endpoint("not a url"), "[invalid url]");
    }

    #[test]
    fn test_max_event_id_len_constant() {
        const { assert!(MAX_EVENT_ID_LEN >= 64, "must allow standard evt_<uuid> IDs") };
        const { assert!(MAX_EVENT_ID_LEN <= 1024, "must not allow absurdly long IDs") };
    }
}