x0x 0.19.49

Agent-to-agent gossip network for AI systems — no winners, no losers, just cooperation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! WebSocket deep integration tests.
//!
//! Tests WebSocket connection lifecycle, pub/sub via WS, direct messaging,
//! session tracking, message ordering, and concurrent clients.
//!
//! All tests require a running x0xd daemon cluster.
//! Run with: cargo nextest run --test ws_integration -- --ignored
//!
//! Prerequisites: cargo build --release --bin x0xd

use futures::{SinkExt, StreamExt};
use serde_json::{json, Value};
use std::time::Duration;
use tokio_tungstenite::tungstenite::Message;

#[path = "harness/src/daemon.rs"]
mod daemon;

use daemon::DaemonFixture;

async fn daemon() -> DaemonFixture {
    DaemonFixture::start("ws-test").await
}

fn client_with_auth(d: &DaemonFixture) -> reqwest::Client {
    d.authed_client(Duration::from_secs(10))
}

// ---------------------------------------------------------------------------
// WebSocket helper
// ---------------------------------------------------------------------------

async fn ws_connect(
    d: &DaemonFixture,
    path: &str,
) -> tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>> {
    let (ws, _) = tokio_tungstenite::connect_async(&d.ws_url(path))
        .await
        .expect("WS connect failed");
    ws
}

async fn ws_recv_text(
    ws: &mut tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    timeout_secs: u64,
) -> Option<String> {
    let deadline = Duration::from_secs(timeout_secs);
    match tokio::time::timeout(deadline, ws.next()).await {
        Ok(Some(Ok(Message::Text(t)))) => Some(t.to_string()),
        _ => None,
    }
}

async fn ws_send(
    ws: &mut tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    msg: &str,
) {
    ws.send(Message::Text(msg.into()))
        .await
        .expect("WS send failed");
}

async fn ws_subscribe_topic(
    ws: &mut tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    topic: &str,
) {
    ws_send(
        ws,
        &format!(r#"{{"type":"subscribe","topics":["{topic}"]}}"#),
    )
    .await;

    for _ in 0..5 {
        let msg = ws_recv_text(ws, 5)
            .await
            .expect("should receive subscription confirmation");
        let frame: Value = serde_json::from_str(&msg).expect("parse subscription confirmation");
        match frame["type"].as_str() {
            Some("subscribed") => return,
            Some("pong") | Some("connected") => continue,
            _ => panic!("unexpected subscription frame: {msg}"),
        }
    }

    panic!("did not receive subscribed frame after subscribe command");
}

async fn ws_unsubscribe_topic(
    ws: &mut tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    topic: &str,
) {
    ws_send(
        ws,
        &format!(r#"{{"type":"unsubscribe","topics":["{topic}"]}}"#),
    )
    .await;

    let mut confirmed = false;
    for _ in 0..5 {
        let Some(msg) = ws_recv_text(ws, 5).await else {
            break;
        };
        let Ok(frame) = serde_json::from_str::<Value>(&msg) else {
            continue;
        };
        match frame["type"].as_str() {
            Some("unsubscribed") => {
                confirmed = true;
                break;
            }
            Some("pong") | Some("connected") => continue,
            _ => {}
        }
    }

    assert!(confirmed, "did not receive unsubscribed frame");
}

async fn ws_recv_topic_message(
    ws: &mut tokio_tungstenite::WebSocketStream<
        tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
    >,
    topic: &str,
    timeout_secs: u64,
) -> Option<Value> {
    tokio::time::timeout(Duration::from_secs(timeout_secs), async {
        loop {
            match ws.next().await {
                Some(Ok(Message::Text(text))) => {
                    let Ok(frame) = serde_json::from_str::<Value>(&text) else {
                        continue;
                    };
                    if frame["type"].as_str() == Some("message")
                        && frame["topic"].as_str() == Some(topic)
                    {
                        return Some(frame);
                    }
                }
                Some(Ok(_)) => {}
                _ => return None,
            }
        }
    })
    .await
    .unwrap_or(None)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// WebSocket connection returns a "connected" message with session_id.
#[tokio::test]
#[ignore]
async fn ws_connection_lifecycle() {
    let d = daemon().await;
    let mut ws = ws_connect(&d, "/ws").await;

    let msg = ws_recv_text(&mut ws, 5)
        .await
        .expect("should receive connected msg");
    let frame: Value = serde_json::from_str(&msg).expect("parse JSON");
    assert_eq!(
        frame["type"], "connected",
        "first message should be 'connected'"
    );
    assert!(frame["session_id"].is_string(), "should have session_id");

    // Clean close
    ws.close(None).await.expect("close");
}

/// Ping-pong works.
#[tokio::test]
#[ignore]
async fn ws_ping_pong() {
    let d = daemon().await;
    let mut ws = ws_connect(&d, "/ws").await;

    // Consume connected message
    let _ = ws_recv_text(&mut ws, 5).await;

    // Send ping
    ws_send(&mut ws, r#"{"type":"ping"}"#).await;

    let msg = ws_recv_text(&mut ws, 5).await.expect("should receive pong");
    let frame: Value = serde_json::from_str(&msg).expect("parse JSON");
    assert_eq!(frame["type"], "pong");

    ws.close(None).await.expect("close");
}

/// Subscribe to a topic via WS, publish via REST, receive via WS.
#[tokio::test]
#[ignore]
async fn ws_subscribe_publish_receive() {
    let d = daemon().await;
    let client = client_with_auth(&d);
    let mut ws = ws_connect(&d, "/ws").await;

    // Consume connected message
    let _ = ws_recv_text(&mut ws, 5).await;

    // Subscribe via WS
    let topic = format!("ws-test-{}", rand::random::<u32>());
    ws_subscribe_topic(&mut ws, &topic).await;

    // Publish via REST
    let payload =
        base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b"hello-ws-test");
    let resp = client
        .post(d.url("/publish"))
        .json(&json!({"topic": topic, "payload": payload}))
        .send()
        .await
        .expect("publish");
    assert_eq!(resp.status(), 200);

    // Receive via WS
    let recv_msg = ws_recv_text(&mut ws, 10).await;
    assert!(
        recv_msg.is_some(),
        "should receive published message via WS"
    );

    ws.close(None).await.expect("close");
}

/// Unsubscribe removes only that session's topic forwarder; duplicate subscribe is idempotent.
#[tokio::test]
#[ignore]
async fn ws_unsubscribe_stops_forwarder_and_repeat_subscribe_is_idempotent() {
    let d = daemon().await;
    let client = client_with_auth(&d);
    let topic = format!("unsubscribe-test-{}", rand::random::<u32>());

    let mut unsubscribed = ws_connect(&d, "/ws").await;
    let _ = ws_recv_text(&mut unsubscribed, 5).await;
    ws_subscribe_topic(&mut unsubscribed, &topic).await;
    ws_subscribe_topic(&mut unsubscribed, &topic).await;

    let mut subscribed = ws_connect(&d, "/ws").await;
    let _ = ws_recv_text(&mut subscribed, 5).await;
    ws_subscribe_topic(&mut subscribed, &topic).await;

    tokio::time::sleep(Duration::from_millis(500)).await;

    let first_payload =
        base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b"first");
    let first_status = client
        .post(d.url("/publish"))
        .json(&json!({"topic": &topic, "payload": &first_payload}))
        .send()
        .await
        .ok()
        .map(|resp| resp.status().as_u16());
    assert_eq!(first_status, Some(200));

    let first = ws_recv_topic_message(&mut unsubscribed, &topic, 10).await;
    assert_eq!(
        first.as_ref().and_then(|frame| frame["payload"].as_str()),
        Some(first_payload.as_str())
    );
    assert!(
        ws_recv_topic_message(&mut unsubscribed, &topic, 1)
            .await
            .is_none(),
        "repeat subscribe delivered a duplicate message"
    );
    let _ = ws_recv_topic_message(&mut subscribed, &topic, 10).await;

    ws_unsubscribe_topic(&mut unsubscribed, &topic).await;
    tokio::time::sleep(Duration::from_millis(200)).await;

    let second_payload =
        base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b"second");
    let second_status = client
        .post(d.url("/publish"))
        .json(&json!({"topic": &topic, "payload": &second_payload}))
        .send()
        .await
        .ok()
        .map(|resp| resp.status().as_u16());
    assert_eq!(second_status, Some(200));

    let second = ws_recv_topic_message(&mut subscribed, &topic, 10).await;
    assert_eq!(
        second.as_ref().and_then(|frame| frame["payload"].as_str()),
        Some(second_payload.as_str())
    );
    assert!(
        ws_recv_topic_message(&mut unsubscribed, &topic, 1)
            .await
            .is_none(),
        "unsubscribed session received a topic message"
    );

    unsubscribed.close(None).await.ok();
    subscribed.close(None).await.ok();
}

/// Session shows up in /ws/sessions while connected.
#[tokio::test]
#[ignore]
async fn ws_session_tracking() {
    let d = daemon().await;
    let client = client_with_auth(&d);

    // Get initial session count
    let initial: Value = client
        .get(d.url("/ws/sessions"))
        .send()
        .await
        .expect("sessions")
        .json()
        .await
        .expect("parse");

    let initial_count = initial
        .as_array()
        .or_else(|| initial["sessions"].as_array())
        .map_or(0, |a| a.len());

    // Connect WS
    let mut ws = ws_connect(&d, "/ws").await;
    let _ = ws_recv_text(&mut ws, 5).await;

    // Small delay for session registration
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Check session count increased
    let after: Value = client
        .get(d.url("/ws/sessions"))
        .send()
        .await
        .expect("sessions")
        .json()
        .await
        .expect("parse");

    let after_count = after
        .as_array()
        .or_else(|| after["sessions"].as_array())
        .map_or(0, |a| a.len());

    assert!(
        after_count > initial_count,
        "session count should increase: before={initial_count}, after={after_count}"
    );

    // Close and verify cleanup
    ws.close(None).await.expect("close");
    tokio::time::sleep(Duration::from_millis(1000)).await;

    let final_resp: Value = client
        .get(d.url("/ws/sessions"))
        .send()
        .await
        .expect("sessions")
        .json()
        .await
        .expect("parse");

    let final_count = final_resp
        .as_array()
        .or_else(|| final_resp["sessions"].as_array())
        .map_or(0, |a| a.len());

    assert!(
        final_count <= initial_count,
        "session count should decrease after close: initial={initial_count}, final={final_count}"
    );
}

/// WS without auth token is rejected.
#[tokio::test]
#[ignore]
async fn ws_requires_auth() {
    let d = daemon().await;
    // Connect without token
    let url = format!("ws://{}/ws", d.api_addr());
    let result = tokio_tungstenite::connect_async(&url).await;

    // Should either fail to connect, close immediately, or get an auth error frame.
    match result {
        Err(_) => {} // Expected — connection rejected
        Ok((mut ws, _)) => {
            let (rejected, observed) =
                match tokio::time::timeout(Duration::from_secs(5), ws.next()).await {
                    Err(_) => (false, "no frame before timeout".to_string()),
                    Ok(None) => (true, "connection closed".to_string()),
                    Ok(Some(Err(err))) => (true, format!("read error: {err}")),
                    Ok(Some(Ok(Message::Close(_)))) => (true, "close frame".to_string()),
                    Ok(Some(Ok(Message::Text(text)))) => {
                        let frame = serde_json::from_str::<Value>(&text);
                        let is_error = matches!(
                            frame.as_ref().ok().and_then(|frame| frame["type"].as_str()),
                            Some("error")
                        );
                        (is_error, format!("text frame: {text}"))
                    }
                    Ok(Some(Ok(frame))) => (false, format!("non-error frame: {frame:?}")),
                };
            ws.close(None).await.ok();
            assert!(
                rejected,
                "unauthenticated websocket accepted connection without closing or sending an auth error: {observed}"
            );
        }
    }
}

/// Direct WebSocket endpoint receives direct messages.
#[tokio::test]
#[ignore]
async fn ws_direct_endpoint() {
    let d = daemon().await;
    let mut ws = ws_connect(&d, "/ws/direct").await;

    let msg = ws_recv_text(&mut ws, 5)
        .await
        .expect("should receive connected msg");
    let frame: Value = serde_json::from_str(&msg).expect("parse JSON");
    assert_eq!(
        frame["type"], "connected",
        "/ws/direct should send connected message"
    );

    ws.close(None).await.expect("close");
}

/// Multiple WS clients on same topic all receive messages.
#[tokio::test]
#[ignore]
async fn ws_concurrent_subscribers() {
    let d = daemon().await;
    let client = client_with_auth(&d);
    let topic = format!("concurrent-test-{}", rand::random::<u32>());
    let n_clients = 5;
    let n_messages = 3;

    // Connect N clients and subscribe to same topic
    let mut clients = Vec::new();
    for _ in 0..n_clients {
        let mut ws = ws_connect(&d, "/ws").await;
        let _ = ws_recv_text(&mut ws, 5).await; // consume connected
        ws_subscribe_topic(&mut ws, &topic).await;
        clients.push(ws);
    }

    // Small delay for subscriptions to propagate
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Publish N messages via REST
    let mut expected_payloads = Vec::new();
    for i in 0..n_messages {
        let payload = base64::Engine::encode(
            &base64::engine::general_purpose::STANDARD,
            format!("msg-{i}").as_bytes(),
        );
        expected_payloads.push(payload.clone());
        client
            .post(d.url("/publish"))
            .json(&json!({"topic": &topic, "payload": payload}))
            .send()
            .await
            .expect("publish");
    }

    // Each client should receive all messages
    expected_payloads.sort();
    for (idx, ws) in clients.iter_mut().enumerate() {
        let mut received_payloads = Vec::new();
        for _ in 0..n_messages {
            let Some(frame) = ws_recv_topic_message(ws, &topic, 5).await else {
                break;
            };
            let payload = frame["payload"].as_str().map(str::to_owned);
            assert!(
                payload.is_some(),
                "Client {idx} received topic message without payload: {frame}"
            );
            if let Some(payload) = payload {
                received_payloads.push(payload);
            }
        }
        received_payloads.sort();
        assert_eq!(
            received_payloads, expected_payloads,
            "Client {idx} should receive all published payloads exactly once"
        );
        assert!(
            ws_recv_topic_message(ws, &topic, 1).await.is_none(),
            "Client {idx} received more than {n_messages} topic messages"
        );
    }

    // Clean up
    for mut ws in clients {
        ws.close(None).await.ok();
    }
}

/// Messages published in order arrive in order (FIFO per topic).
#[tokio::test]
#[ignore]
async fn ws_message_ordering() {
    let d = daemon().await;
    let client = client_with_auth(&d);
    let topic = format!("order-test-{}", rand::random::<u32>());

    let mut ws = ws_connect(&d, "/ws").await;
    let _ = ws_recv_text(&mut ws, 5).await; // connected
    ws_subscribe_topic(&mut ws, &topic).await;

    tokio::time::sleep(Duration::from_millis(300)).await;

    // Publish messages with sequential payloads
    let n = 10;
    for i in 0..n {
        let payload = base64::Engine::encode(
            &base64::engine::general_purpose::STANDARD,
            format!("seq-{i:04}").as_bytes(),
        );
        client
            .post(d.url("/publish"))
            .json(&json!({"topic": &topic, "payload": payload}))
            .send()
            .await
            .expect("publish");
    }

    let expected_payloads = (0..n).map(|i| format!("seq-{i:04}")).collect::<Vec<_>>();
    let mut received_payloads = Vec::with_capacity(n);
    for idx in 0..n {
        let Some(frame) = ws_recv_topic_message(&mut ws, &topic, 10).await else {
            break;
        };
        let decoded = frame["payload"]
            .as_str()
            .and_then(|payload| {
                base64::Engine::decode(&base64::engine::general_purpose::STANDARD, payload).ok()
            })
            .and_then(|bytes| String::from_utf8(bytes).ok());
        assert!(
            decoded.is_some(),
            "ordered message {idx} should contain a base64 UTF-8 payload: {frame}"
        );
        if let Some(decoded) = decoded {
            received_payloads.push(decoded);
        }
    }

    assert_eq!(
        received_payloads, expected_payloads,
        "received payloads should match the published FIFO sequence"
    );
    assert!(
        ws_recv_topic_message(&mut ws, &topic, 1).await.is_none(),
        "received more than {n} ordered topic messages"
    );

    ws.close(None).await.expect("close");
}