brainwires-relay 0.1.0

MCP server framework, relay client, and agent communication backbone for Brainwires
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Unix Socket IPC Utilities
//!
//! Provides async read/write helpers for Unix domain socket communication
//! between the TUI viewer and Agent process.
//!
//! # Encryption
//!
//! This module provides both plaintext and encrypted IPC:
//! - `IpcReader`/`IpcWriter` - Plaintext JSON over newlines (legacy, for handshake)
//! - `EncryptedIpcReader`/`EncryptedIpcWriter` - ChaCha20-Poly1305 encrypted messages
//!
//! The encrypted variants use the session token to derive the encryption key,
//! providing confidentiality and integrity for all messages after handshake.

use anyhow::{bail, Context, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter};
use tokio::net::unix::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::UnixStream;

use super::crypto::IpcCipher;

/// Maximum message size (16 MB)
const MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;

/// Reader for receiving IPC messages
pub struct IpcReader {
    reader: BufReader<OwnedReadHalf>,
}

impl IpcReader {
    /// Create a new IPC reader from a Unix stream read half
    pub fn new(read_half: OwnedReadHalf) -> Self {
        Self {
            reader: BufReader::new(read_half),
        }
    }

    /// Read a message from the socket
    ///
    /// Messages are newline-delimited JSON.
    /// Returns None on EOF.
    pub async fn read<T: DeserializeOwned>(&mut self) -> Result<Option<T>> {
        let mut line = String::new();
        let bytes_read = self.reader.read_line(&mut line).await?;

        if bytes_read == 0 {
            return Ok(None); // EOF
        }

        if line.len() > MAX_MESSAGE_SIZE {
            bail!("Message exceeds maximum size of {} bytes", MAX_MESSAGE_SIZE);
        }

        let message: T = serde_json::from_str(line.trim())
            .with_context(|| format!("Failed to parse IPC message: {}", line.trim()))?;

        Ok(Some(message))
    }
}

/// Writer for sending IPC messages
pub struct IpcWriter {
    writer: BufWriter<OwnedWriteHalf>,
}

impl IpcWriter {
    /// Create a new IPC writer from a Unix stream write half
    pub fn new(write_half: OwnedWriteHalf) -> Self {
        Self {
            writer: BufWriter::new(write_half),
        }
    }

    /// Write a message to the socket
    ///
    /// Messages are serialized as newline-delimited JSON.
    pub async fn write<T: Serialize>(&mut self, message: &T) -> Result<()> {
        let json = serde_json::to_string(message)
            .context("Failed to serialize IPC message")?;

        if json.len() > MAX_MESSAGE_SIZE {
            bail!("Message exceeds maximum size of {} bytes", MAX_MESSAGE_SIZE);
        }

        self.writer.write_all(json.as_bytes()).await?;
        self.writer.write_all(b"\n").await?;
        self.writer.flush().await?;

        Ok(())
    }
}

/// IPC connection handle (combines reader and writer)
pub struct IpcConnection {
    /// The reader half of the connection.
    pub reader: IpcReader,
    /// The writer half of the connection.
    pub writer: IpcWriter,
}

impl IpcConnection {
    /// Create an IPC connection from a Unix stream
    pub fn from_stream(stream: UnixStream) -> Self {
        let (read_half, write_half) = stream.into_split();
        Self {
            reader: IpcReader::new(read_half),
            writer: IpcWriter::new(write_half),
        }
    }

    /// Connect to an agent socket by path
    pub async fn connect(socket_path: &Path) -> Result<Self> {
        if !socket_path.exists() {
            bail!("Agent socket not found: {}", socket_path.display());
        }

        let stream = UnixStream::connect(socket_path)
            .await
            .with_context(|| format!("Failed to connect to agent socket: {}", socket_path.display()))?;

        Ok(Self::from_stream(stream))
    }

    /// Connect to an agent by session ID, looking up the socket in sessions_dir
    pub async fn connect_to_agent(sessions_dir: &Path, session_id: &str) -> Result<Self> {
        let socket_path = get_agent_socket_path(sessions_dir, session_id);
        Self::connect(&socket_path).await
    }

    /// Split into reader and writer
    pub fn split(self) -> (IpcReader, IpcWriter) {
        (self.reader, self.writer)
    }

    /// Upgrade to encrypted connection using the session token
    ///
    /// This should be called after the handshake is complete and both
    /// sides have agreed on the session token.
    pub fn upgrade_to_encrypted(self, session_token: &str) -> EncryptedIpcConnection {
        let cipher = Arc::new(IpcCipher::from_session_token(session_token));
        let (read_half, write_half) = (self.reader, self.writer);
        EncryptedIpcConnection {
            reader: EncryptedIpcReader::new(read_half, Arc::clone(&cipher)),
            writer: EncryptedIpcWriter::new(write_half, cipher),
        }
    }
}

// ============================================================================
// Encrypted IPC (ChaCha20-Poly1305)
// ============================================================================

/// Encrypted reader for receiving IPC messages
///
/// Uses ChaCha20-Poly1305 authenticated encryption.
/// Message format: [4-byte length][encrypted data]
pub struct EncryptedIpcReader {
    inner: IpcReader,
    cipher: Arc<IpcCipher>,
}

impl EncryptedIpcReader {
    /// Create a new encrypted IPC reader
    pub fn new(reader: IpcReader, cipher: Arc<IpcCipher>) -> Self {
        Self {
            inner: reader,
            cipher,
        }
    }

    /// Read an encrypted message from the socket
    ///
    /// Messages are length-prefixed encrypted blobs.
    /// Returns None on EOF.
    pub async fn read<T: DeserializeOwned>(&mut self) -> Result<Option<T>> {
        // Read length prefix (4 bytes, big-endian)
        let mut len_buf = [0u8; 4];
        match self.inner.reader.read_exact(&mut len_buf).await {
            Ok(_) => {}
            Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
            Err(e) => return Err(e.into()),
        }

        let msg_len = u32::from_be_bytes(len_buf) as usize;

        if msg_len > MAX_MESSAGE_SIZE {
            bail!("Encrypted message exceeds maximum size of {} bytes", MAX_MESSAGE_SIZE);
        }

        // Read encrypted message
        let mut encrypted = vec![0u8; msg_len];
        self.inner.reader.read_exact(&mut encrypted).await?;

        // Decrypt
        let plaintext = self.cipher.decrypt(&encrypted)
            .context("Failed to decrypt IPC message")?;

        // Deserialize
        let message: T = serde_json::from_slice(&plaintext)
            .context("Failed to parse decrypted IPC message")?;

        Ok(Some(message))
    }
}

/// Encrypted writer for sending IPC messages
///
/// Uses ChaCha20-Poly1305 authenticated encryption.
/// Message format: [4-byte length][encrypted data]
pub struct EncryptedIpcWriter {
    inner: IpcWriter,
    cipher: Arc<IpcCipher>,
}

impl EncryptedIpcWriter {
    /// Create a new encrypted IPC writer
    pub fn new(writer: IpcWriter, cipher: Arc<IpcCipher>) -> Self {
        Self {
            inner: writer,
            cipher,
        }
    }

    /// Write an encrypted message to the socket
    ///
    /// Messages are serialized, encrypted, then length-prefixed.
    pub async fn write<T: Serialize>(&mut self, message: &T) -> Result<()> {
        // Serialize
        let json = serde_json::to_vec(message)
            .context("Failed to serialize IPC message")?;

        if json.len() > MAX_MESSAGE_SIZE {
            bail!("Message exceeds maximum size of {} bytes", MAX_MESSAGE_SIZE);
        }

        // Encrypt
        let encrypted = self.cipher.encrypt(&json)
            .context("Failed to encrypt IPC message")?;

        // Write length prefix (4 bytes, big-endian)
        let len_buf = (encrypted.len() as u32).to_be_bytes();
        self.inner.writer.write_all(&len_buf).await?;

        // Write encrypted message
        self.inner.writer.write_all(&encrypted).await?;
        self.inner.writer.flush().await?;

        Ok(())
    }
}

/// Encrypted IPC connection handle
pub struct EncryptedIpcConnection {
    /// The encrypted reader half of the connection.
    pub reader: EncryptedIpcReader,
    /// The encrypted writer half of the connection.
    pub writer: EncryptedIpcWriter,
}

impl EncryptedIpcConnection {
    /// Create an encrypted IPC connection from a Unix stream and session token
    pub fn from_stream(stream: UnixStream, session_token: &str) -> Self {
        let cipher = Arc::new(IpcCipher::from_session_token(session_token));
        let (read_half, write_half) = stream.into_split();
        Self {
            reader: EncryptedIpcReader::new(
                IpcReader::new(read_half),
                Arc::clone(&cipher),
            ),
            writer: EncryptedIpcWriter::new(
                IpcWriter::new(write_half),
                cipher,
            ),
        }
    }

    /// Split into encrypted reader and writer
    pub fn split(self) -> (EncryptedIpcReader, EncryptedIpcWriter) {
        (self.reader, self.writer)
    }
}

// ============================================================================
// Path Helpers
// ============================================================================

/// Get the socket path for an agent session
pub fn get_agent_socket_path(sessions_dir: &Path, session_id: &str) -> PathBuf {
    sessions_dir.join(format!("{}.sock", session_id))
}

/// Get the token file path for an agent session
pub fn get_session_token_path(sessions_dir: &Path, session_id: &str) -> PathBuf {
    sessions_dir.join(format!("{}.token", session_id))
}

// ============================================================================
// Session Token Management (for secure IPC authentication)
// ============================================================================

/// Generate a cryptographically secure session token (64 hex characters = 256 bits)
pub fn generate_session_token() -> String {
    use rand::RngCore;
    let mut bytes = [0u8; 32];
    rand::thread_rng().fill_bytes(&mut bytes);
    hex::encode(bytes)
}

/// Write session token to disk with secure permissions (0600)
/// This should only be called by the agent process that owns the session
pub fn write_session_token(sessions_dir: &Path, session_id: &str, token: &str) -> Result<()> {
    let token_path = get_session_token_path(sessions_dir, session_id);

    // Ensure parent directory exists
    if let Some(parent) = token_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Write token
    std::fs::write(&token_path, token)?;

    // Set secure permissions (0600 = owner read/write only)
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&token_path, std::fs::Permissions::from_mode(0o600))?;
    }

    tracing::debug!("Wrote session token: {} (0600 permissions)", token_path.display());
    Ok(())
}

/// Read session token from disk
/// This is used by clients that need to reattach to a session
pub fn read_session_token(sessions_dir: &Path, session_id: &str) -> Result<Option<String>> {
    let token_path = get_session_token_path(sessions_dir, session_id);

    if !token_path.exists() {
        return Ok(None);
    }

    let token = std::fs::read_to_string(&token_path)
        .with_context(|| format!("Failed to read session token from {}", token_path.display()))?;

    Ok(Some(token.trim().to_string()))
}

/// Delete session token file
pub fn delete_session_token(sessions_dir: &Path, session_id: &str) -> Result<()> {
    let token_path = get_session_token_path(sessions_dir, session_id);

    if token_path.exists() {
        std::fs::remove_file(&token_path)
            .with_context(|| format!("Failed to delete session token: {}", token_path.display()))?;
        tracing::debug!("Deleted session token: {}", token_path.display());
    }

    Ok(())
}

/// Validate that a provided token matches the stored token for a session
/// Returns true if tokens match, false if they don't match or no token exists
pub fn validate_session_token(sessions_dir: &Path, session_id: &str, provided_token: &str) -> bool {
    match read_session_token(sessions_dir, session_id) {
        Ok(Some(stored_token)) => {
            // Use constant-time comparison to prevent timing attacks
            use subtle::ConstantTimeEq;
            provided_token.as_bytes().ct_eq(stored_token.as_bytes()).into()
        }
        Ok(None) => {
            tracing::warn!("No session token found for session {}", session_id);
            false
        }
        Err(e) => {
            tracing::error!("Failed to read session token for {}: {}", session_id, e);
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::protocol::{AgentMessage, ViewerMessage};
    use tokio::net::UnixListener;

    #[tokio::test]
    async fn test_ipc_roundtrip() {
        // Create a temporary socket
        let temp_dir = tempfile::tempdir().unwrap();
        let socket_path = temp_dir.path().join("test.sock");

        // Start listener
        let listener = UnixListener::bind(&socket_path).unwrap();

        // Spawn server task
        let server_task = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();
            let mut conn = IpcConnection::from_stream(stream);

            // Read message from client
            let msg: ViewerMessage = conn.reader.read().await.unwrap().unwrap();
            match msg {
                ViewerMessage::UserInput { content, .. } => {
                    assert_eq!(content, "Hello");
                }
                _ => panic!("Unexpected message type"),
            }

            // Send response
            let response = AgentMessage::Ack {
                command: "user_input".to_string(),
            };
            conn.writer.write(&response).await.unwrap();
        });

        // Client connects and sends message
        let stream = UnixStream::connect(&socket_path).await.unwrap();
        let mut conn = IpcConnection::from_stream(stream);

        let msg = ViewerMessage::UserInput {
            content: "Hello".to_string(),
            context_files: vec![],
        };
        conn.writer.write(&msg).await.unwrap();

        // Read response
        let response: AgentMessage = conn.reader.read().await.unwrap().unwrap();
        match response {
            AgentMessage::Ack { command } => {
                assert_eq!(command, "user_input");
            }
            _ => panic!("Unexpected response type"),
        }

        server_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_encrypted_ipc_roundtrip() {
        // Create a temporary socket
        let temp_dir = tempfile::tempdir().unwrap();
        let socket_path = temp_dir.path().join("encrypted_test.sock");

        // Start listener
        let listener = UnixListener::bind(&socket_path).unwrap();

        // Shared session token for encryption
        let session_token = "test-session-token-for-encrypted-ipc";

        let server_token = session_token.to_string();
        // Spawn server task
        let server_task = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();

            // Upgrade to encrypted connection
            let conn = IpcConnection::from_stream(stream);
            let mut encrypted_conn = conn.upgrade_to_encrypted(&server_token);

            // Read encrypted message from client
            let msg: ViewerMessage = encrypted_conn.reader.read().await.unwrap().unwrap();
            match msg {
                ViewerMessage::UserInput { content, .. } => {
                    assert_eq!(content, "Encrypted Hello!");
                }
                _ => panic!("Unexpected message type"),
            }

            // Send encrypted response
            let response = AgentMessage::Ack {
                command: "encrypted_user_input".to_string(),
            };
            encrypted_conn.writer.write(&response).await.unwrap();
        });

        // Client connects and sends encrypted message
        let stream = UnixStream::connect(&socket_path).await.unwrap();
        let conn = IpcConnection::from_stream(stream);
        let mut encrypted_conn = conn.upgrade_to_encrypted(session_token);

        let msg = ViewerMessage::UserInput {
            content: "Encrypted Hello!".to_string(),
            context_files: vec![],
        };
        encrypted_conn.writer.write(&msg).await.unwrap();

        // Read encrypted response
        let response: AgentMessage = encrypted_conn.reader.read().await.unwrap().unwrap();
        match response {
            AgentMessage::Ack { command } => {
                assert_eq!(command, "encrypted_user_input");
            }
            _ => panic!("Unexpected response type"),
        }

        server_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_encrypted_ipc_wrong_key_fails() {
        // Create a temporary socket
        let temp_dir = tempfile::tempdir().unwrap();
        let socket_path = temp_dir.path().join("wrong_key_test.sock");

        // Start listener
        let listener = UnixListener::bind(&socket_path).unwrap();

        // Spawn server task with DIFFERENT token
        let server_task = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();

            // Server uses different token
            let conn = IpcConnection::from_stream(stream);
            let mut encrypted_conn = conn.upgrade_to_encrypted("server-token-different");

            // Read should fail due to wrong key
            let result: Result<Option<ViewerMessage>> = encrypted_conn.reader.read().await;
            assert!(result.is_err(), "Should fail to decrypt with wrong key");
        });

        // Client connects with different token
        let stream = UnixStream::connect(&socket_path).await.unwrap();
        let conn = IpcConnection::from_stream(stream);
        let mut encrypted_conn = conn.upgrade_to_encrypted("client-token-different");

        let msg = ViewerMessage::UserInput {
            content: "This will fail".to_string(),
            context_files: vec![],
        };
        encrypted_conn.writer.write(&msg).await.unwrap();

        server_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_encrypted_multiple_messages() {
        // Create a temporary socket
        let temp_dir = tempfile::tempdir().unwrap();
        let socket_path = temp_dir.path().join("multi_msg_test.sock");

        // Start listener
        let listener = UnixListener::bind(&socket_path).unwrap();
        let session_token = "multi-message-token";

        let server_token = session_token.to_string();
        let server_task = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();
            let conn = IpcConnection::from_stream(stream);
            let mut encrypted_conn = conn.upgrade_to_encrypted(&server_token);

            // Read and respond to multiple messages
            for i in 0..5 {
                let msg: ViewerMessage = encrypted_conn.reader.read().await.unwrap().unwrap();
                match msg {
                    ViewerMessage::UserInput { content, .. } => {
                        assert_eq!(content, format!("Message {}", i));
                    }
                    _ => panic!("Unexpected message type"),
                }

                let response = AgentMessage::Ack {
                    command: format!("ack_{}", i),
                };
                encrypted_conn.writer.write(&response).await.unwrap();
            }
        });

        // Client sends multiple encrypted messages
        let stream = UnixStream::connect(&socket_path).await.unwrap();
        let conn = IpcConnection::from_stream(stream);
        let mut encrypted_conn = conn.upgrade_to_encrypted(session_token);

        for i in 0..5 {
            let msg = ViewerMessage::UserInput {
                content: format!("Message {}", i),
                context_files: vec![],
            };
            encrypted_conn.writer.write(&msg).await.unwrap();

            let response: AgentMessage = encrypted_conn.reader.read().await.unwrap().unwrap();
            match response {
                AgentMessage::Ack { command } => {
                    assert_eq!(command, format!("ack_{}", i));
                }
                _ => panic!("Unexpected response type"),
            }
        }

        server_task.await.unwrap();
    }

    #[test]
    fn test_session_token_roundtrip() {
        let temp_dir = tempfile::tempdir().unwrap();
        let sessions_dir = temp_dir.path();

        let token = generate_session_token();
        assert_eq!(token.len(), 64); // 32 bytes = 64 hex chars

        write_session_token(sessions_dir, "test-session", &token).unwrap();
        let read_token = read_session_token(sessions_dir, "test-session").unwrap();
        assert_eq!(read_token, Some(token.clone()));

        assert!(validate_session_token(sessions_dir, "test-session", &token));
        assert!(!validate_session_token(sessions_dir, "test-session", "wrong-token"));

        delete_session_token(sessions_dir, "test-session").unwrap();
        let read_after_delete = read_session_token(sessions_dir, "test-session").unwrap();
        assert_eq!(read_after_delete, None);
    }
}