zentinel-agent-protocol 0.6.11

Agent protocol and IPC for Zentinel reverse proxy external processors
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! Binary protocol for Unix Domain Socket transport.
//!
//! This module provides a binary framing format for efficient communication
//! over UDS, eliminating JSON overhead and base64 encoding for body data.
//!
//! # Wire Format
//!
//! ```text
//! +----------------+---------------+-------------------+
//! | Length (4 BE)  | Type (1 byte) | Payload (N bytes) |
//! +----------------+---------------+-------------------+
//! ```
//!
//! - **Length**: 4-byte big-endian u32, total length of type + payload
//! - **Type**: 1-byte message type discriminator
//! - **Payload**: Variable-length payload (format depends on type)
//!
//! # Performance Benefits
//!
//! - No JSON parsing overhead (~10x faster for small messages)
//! - No base64 encoding for body data (saves 33% bandwidth)
//! - Zero-copy with `bytes::Bytes` where possible

use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::collections::HashMap;
use std::io;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::{AgentProtocolError, Decision, HeaderOp};

/// Maximum binary message size (10 MB)
pub const MAX_BINARY_MESSAGE_SIZE: usize = 10 * 1024 * 1024;

/// Binary message types
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageType {
    /// Handshake request (proxy -> agent)
    HandshakeRequest = 0x01,
    /// Handshake response (agent -> proxy)
    HandshakeResponse = 0x02,
    /// Request headers event
    RequestHeaders = 0x10,
    /// Request body chunk (raw bytes, no base64)
    RequestBodyChunk = 0x11,
    /// Response headers event
    ResponseHeaders = 0x12,
    /// Response body chunk (raw bytes, no base64)
    ResponseBodyChunk = 0x13,
    /// Request complete event
    RequestComplete = 0x14,
    /// WebSocket frame event
    WebSocketFrame = 0x15,
    /// Agent response
    AgentResponse = 0x20,
    /// Ping
    Ping = 0x30,
    /// Pong
    Pong = 0x31,
    /// Cancel request
    Cancel = 0x40,
    /// Error
    Error = 0xFF,
}

impl TryFrom<u8> for MessageType {
    type Error = AgentProtocolError;

    fn try_from(value: u8) -> Result<Self, AgentProtocolError> {
        match value {
            0x01 => Ok(MessageType::HandshakeRequest),
            0x02 => Ok(MessageType::HandshakeResponse),
            0x10 => Ok(MessageType::RequestHeaders),
            0x11 => Ok(MessageType::RequestBodyChunk),
            0x12 => Ok(MessageType::ResponseHeaders),
            0x13 => Ok(MessageType::ResponseBodyChunk),
            0x14 => Ok(MessageType::RequestComplete),
            0x15 => Ok(MessageType::WebSocketFrame),
            0x20 => Ok(MessageType::AgentResponse),
            0x30 => Ok(MessageType::Ping),
            0x31 => Ok(MessageType::Pong),
            0x40 => Ok(MessageType::Cancel),
            0xFF => Ok(MessageType::Error),
            _ => Err(AgentProtocolError::InvalidMessage(format!(
                "Unknown message type: 0x{:02x}",
                value
            ))),
        }
    }
}

/// Binary frame with header and payload.
#[derive(Debug, Clone)]
pub struct BinaryFrame {
    pub msg_type: MessageType,
    pub payload: Bytes,
}

impl BinaryFrame {
    /// Create a new binary frame.
    pub fn new(msg_type: MessageType, payload: impl Into<Bytes>) -> Self {
        Self {
            msg_type,
            payload: payload.into(),
        }
    }

    /// Encode frame to bytes.
    pub fn encode(&self) -> Bytes {
        let payload_len = self.payload.len();
        let total_len = 1 + payload_len; // type byte + payload

        let mut buf = BytesMut::with_capacity(4 + total_len);
        buf.put_u32(total_len as u32);
        buf.put_u8(self.msg_type as u8);
        buf.put_slice(&self.payload);

        buf.freeze()
    }

    /// Decode frame from reader.
    pub async fn decode<R: AsyncRead + Unpin>(reader: &mut R) -> Result<Self, AgentProtocolError> {
        // Read length (4 bytes)
        let mut len_buf = [0u8; 4];
        reader.read_exact(&mut len_buf).await.map_err(|e| {
            if e.kind() == io::ErrorKind::UnexpectedEof {
                AgentProtocolError::ConnectionFailed("Connection closed".to_string())
            } else {
                AgentProtocolError::Io(e)
            }
        })?;
        let total_len = u32::from_be_bytes(len_buf) as usize;

        // Validate length
        if total_len == 0 {
            return Err(AgentProtocolError::InvalidMessage(
                "Empty message".to_string(),
            ));
        }
        if total_len > MAX_BINARY_MESSAGE_SIZE {
            return Err(AgentProtocolError::MessageTooLarge {
                size: total_len,
                max: MAX_BINARY_MESSAGE_SIZE,
            });
        }

        // Read type byte
        let mut type_buf = [0u8; 1];
        reader.read_exact(&mut type_buf).await?;
        let msg_type = MessageType::try_from(type_buf[0])?;

        // Read payload
        let payload_len = total_len - 1;
        let mut payload = BytesMut::with_capacity(payload_len);
        payload.resize(payload_len, 0);
        reader.read_exact(&mut payload).await?;

        Ok(Self {
            msg_type,
            payload: payload.freeze(),
        })
    }

    /// Write frame to writer.
    pub async fn write<W: AsyncWrite + Unpin>(
        &self,
        writer: &mut W,
    ) -> Result<(), AgentProtocolError> {
        let encoded = self.encode();
        writer.write_all(&encoded).await?;
        writer.flush().await?;
        Ok(())
    }
}

/// Binary request headers event.
///
/// Wire format:
/// - correlation_id: length-prefixed string
/// - method: length-prefixed string
/// - uri: length-prefixed string
/// - headers: count (u16) + [(name_len, name, value_len, value), ...]
/// - client_ip: length-prefixed string
/// - client_port: u16
#[derive(Debug, Clone)]
pub struct BinaryRequestHeaders {
    pub correlation_id: String,
    pub method: String,
    pub uri: String,
    pub headers: HashMap<String, Vec<String>>,
    pub client_ip: String,
    pub client_port: u16,
}

impl BinaryRequestHeaders {
    /// Encode to bytes.
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::with_capacity(256);

        // Correlation ID
        put_string(&mut buf, &self.correlation_id);
        // Method
        put_string(&mut buf, &self.method);
        // URI
        put_string(&mut buf, &self.uri);

        // Headers count
        let header_count: usize = self.headers.values().map(|v| v.len()).sum();
        buf.put_u16(header_count as u16);

        // Headers (flattened: each value gets its own entry)
        for (name, values) in &self.headers {
            for value in values {
                put_string(&mut buf, name);
                put_string(&mut buf, value);
            }
        }

        // Client IP
        put_string(&mut buf, &self.client_ip);
        // Client port
        buf.put_u16(self.client_port);

        buf.freeze()
    }

    /// Decode from bytes.
    pub fn decode(mut data: Bytes) -> Result<Self, AgentProtocolError> {
        let correlation_id = get_string(&mut data)?;
        let method = get_string(&mut data)?;
        let uri = get_string(&mut data)?;

        // Headers
        if data.remaining() < 2 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing header count".to_string(),
            ));
        }
        let header_count = data.get_u16() as usize;

        let mut headers: HashMap<String, Vec<String>> = HashMap::new();
        for _ in 0..header_count {
            let name = get_string(&mut data)?;
            let value = get_string(&mut data)?;
            headers.entry(name).or_default().push(value);
        }

        let client_ip = get_string(&mut data)?;

        if data.remaining() < 2 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing client port".to_string(),
            ));
        }
        let client_port = data.get_u16();

        Ok(Self {
            correlation_id,
            method,
            uri,
            headers,
            client_ip,
            client_port,
        })
    }
}

/// Binary body chunk event (zero-copy).
///
/// Wire format:
/// - correlation_id: length-prefixed string
/// - chunk_index: u32
/// - is_last: u8 (0 or 1)
/// - data_len: u32
/// - data: raw bytes (no base64!)
#[derive(Debug, Clone)]
pub struct BinaryBodyChunk {
    pub correlation_id: String,
    pub chunk_index: u32,
    pub is_last: bool,
    pub data: Bytes,
}

impl BinaryBodyChunk {
    /// Encode to bytes.
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::with_capacity(32 + self.data.len());

        put_string(&mut buf, &self.correlation_id);
        buf.put_u32(self.chunk_index);
        buf.put_u8(if self.is_last { 1 } else { 0 });
        buf.put_u32(self.data.len() as u32);
        buf.put_slice(&self.data);

        buf.freeze()
    }

    /// Decode from bytes.
    pub fn decode(mut data: Bytes) -> Result<Self, AgentProtocolError> {
        let correlation_id = get_string(&mut data)?;

        if data.remaining() < 9 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing body chunk fields".to_string(),
            ));
        }

        let chunk_index = data.get_u32();
        let is_last = data.get_u8() != 0;
        let data_len = data.get_u32() as usize;

        if data.remaining() < data_len {
            return Err(AgentProtocolError::InvalidMessage(
                "Body data truncated".to_string(),
            ));
        }

        let body_data = data.copy_to_bytes(data_len);

        Ok(Self {
            correlation_id,
            chunk_index,
            is_last,
            data: body_data,
        })
    }
}

/// Binary agent response.
///
/// Wire format:
/// - correlation_id: length-prefixed string
/// - decision_type: u8 (0=Allow, 1=Block, 2=Redirect, 3=Challenge)
/// - decision_data: varies by type
/// - request_headers_ops: count (u16) + ops
/// - response_headers_ops: count (u16) + ops
/// - needs_more: u8
#[derive(Debug, Clone)]
pub struct BinaryAgentResponse {
    pub correlation_id: String,
    pub decision: Decision,
    pub request_headers: Vec<HeaderOp>,
    pub response_headers: Vec<HeaderOp>,
    pub needs_more: bool,
}

impl BinaryAgentResponse {
    /// Encode to bytes.
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::with_capacity(128);

        put_string(&mut buf, &self.correlation_id);

        // Decision
        match &self.decision {
            Decision::Allow => {
                buf.put_u8(0);
            }
            Decision::Block {
                status,
                body,
                headers,
            } => {
                buf.put_u8(1);
                buf.put_u16(*status);
                put_optional_string(&mut buf, body.as_deref());
                // Block headers
                let h_count = headers.as_ref().map(|h| h.len()).unwrap_or(0);
                buf.put_u16(h_count as u16);
                if let Some(headers) = headers {
                    for (k, v) in headers {
                        put_string(&mut buf, k);
                        put_string(&mut buf, v);
                    }
                }
            }
            Decision::Redirect { url, status } => {
                buf.put_u8(2);
                put_string(&mut buf, url);
                buf.put_u16(*status);
            }
            Decision::Challenge {
                challenge_type,
                params,
            } => {
                buf.put_u8(3);
                put_string(&mut buf, challenge_type);
                buf.put_u16(params.len() as u16);
                for (k, v) in params {
                    put_string(&mut buf, k);
                    put_string(&mut buf, v);
                }
            }
        }

        // Request header ops
        buf.put_u16(self.request_headers.len() as u16);
        for op in &self.request_headers {
            encode_header_op(&mut buf, op);
        }

        // Response header ops
        buf.put_u16(self.response_headers.len() as u16);
        for op in &self.response_headers {
            encode_header_op(&mut buf, op);
        }

        // Needs more
        buf.put_u8(if self.needs_more { 1 } else { 0 });

        buf.freeze()
    }

    /// Decode from bytes.
    pub fn decode(mut data: Bytes) -> Result<Self, AgentProtocolError> {
        let correlation_id = get_string(&mut data)?;

        if data.remaining() < 1 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing decision type".to_string(),
            ));
        }

        let decision_type = data.get_u8();
        let decision = match decision_type {
            0 => Decision::Allow,
            1 => {
                if data.remaining() < 2 {
                    return Err(AgentProtocolError::InvalidMessage(
                        "Missing block status".to_string(),
                    ));
                }
                let status = data.get_u16();
                let body = get_optional_string(&mut data)?;
                if data.remaining() < 2 {
                    return Err(AgentProtocolError::InvalidMessage(
                        "Missing block headers count".to_string(),
                    ));
                }
                let h_count = data.get_u16() as usize;
                let headers = if h_count > 0 {
                    let mut h = HashMap::new();
                    for _ in 0..h_count {
                        let k = get_string(&mut data)?;
                        let v = get_string(&mut data)?;
                        h.insert(k, v);
                    }
                    Some(h)
                } else {
                    None
                };
                Decision::Block {
                    status,
                    body,
                    headers,
                }
            }
            2 => {
                let url = get_string(&mut data)?;
                if data.remaining() < 2 {
                    return Err(AgentProtocolError::InvalidMessage(
                        "Missing redirect status".to_string(),
                    ));
                }
                let status = data.get_u16();
                Decision::Redirect { url, status }
            }
            3 => {
                let challenge_type = get_string(&mut data)?;
                if data.remaining() < 2 {
                    return Err(AgentProtocolError::InvalidMessage(
                        "Missing challenge params count".to_string(),
                    ));
                }
                let p_count = data.get_u16() as usize;
                let mut params = HashMap::new();
                for _ in 0..p_count {
                    let k = get_string(&mut data)?;
                    let v = get_string(&mut data)?;
                    params.insert(k, v);
                }
                Decision::Challenge {
                    challenge_type,
                    params,
                }
            }
            _ => {
                return Err(AgentProtocolError::InvalidMessage(format!(
                    "Unknown decision type: {}",
                    decision_type
                )));
            }
        };

        // Request header ops
        if data.remaining() < 2 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing request headers count".to_string(),
            ));
        }
        let req_h_count = data.get_u16() as usize;
        let mut request_headers = Vec::with_capacity(req_h_count);
        for _ in 0..req_h_count {
            request_headers.push(decode_header_op(&mut data)?);
        }

        // Response header ops
        if data.remaining() < 2 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing response headers count".to_string(),
            ));
        }
        let resp_h_count = data.get_u16() as usize;
        let mut response_headers = Vec::with_capacity(resp_h_count);
        for _ in 0..resp_h_count {
            response_headers.push(decode_header_op(&mut data)?);
        }

        // Needs more
        if data.remaining() < 1 {
            return Err(AgentProtocolError::InvalidMessage(
                "Missing needs_more".to_string(),
            ));
        }
        let needs_more = data.get_u8() != 0;

        Ok(Self {
            correlation_id,
            decision,
            request_headers,
            response_headers,
            needs_more,
        })
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

fn put_string(buf: &mut BytesMut, s: &str) {
    let bytes = s.as_bytes();
    buf.put_u16(bytes.len() as u16);
    buf.put_slice(bytes);
}

fn get_string(data: &mut Bytes) -> Result<String, AgentProtocolError> {
    if data.remaining() < 2 {
        return Err(AgentProtocolError::InvalidMessage(
            "Missing string length".to_string(),
        ));
    }
    let len = data.get_u16() as usize;
    if data.remaining() < len {
        return Err(AgentProtocolError::InvalidMessage(
            "String data truncated".to_string(),
        ));
    }
    let bytes = data.copy_to_bytes(len);
    String::from_utf8(bytes.to_vec())
        .map_err(|e| AgentProtocolError::InvalidMessage(format!("Invalid UTF-8: {}", e)))
}

fn put_optional_string(buf: &mut BytesMut, s: Option<&str>) {
    match s {
        Some(s) => {
            buf.put_u8(1);
            put_string(buf, s);
        }
        None => {
            buf.put_u8(0);
        }
    }
}

fn get_optional_string(data: &mut Bytes) -> Result<Option<String>, AgentProtocolError> {
    if data.remaining() < 1 {
        return Err(AgentProtocolError::InvalidMessage(
            "Missing optional string flag".to_string(),
        ));
    }
    let present = data.get_u8() != 0;
    if present {
        get_string(data).map(Some)
    } else {
        Ok(None)
    }
}

fn encode_header_op(buf: &mut BytesMut, op: &HeaderOp) {
    match op {
        HeaderOp::Set { name, value } => {
            buf.put_u8(0);
            put_string(buf, name);
            put_string(buf, value);
        }
        HeaderOp::Add { name, value } => {
            buf.put_u8(1);
            put_string(buf, name);
            put_string(buf, value);
        }
        HeaderOp::Remove { name } => {
            buf.put_u8(2);
            put_string(buf, name);
        }
    }
}

fn decode_header_op(data: &mut Bytes) -> Result<HeaderOp, AgentProtocolError> {
    if data.remaining() < 1 {
        return Err(AgentProtocolError::InvalidMessage(
            "Missing header op type".to_string(),
        ));
    }
    let op_type = data.get_u8();
    match op_type {
        0 => {
            let name = get_string(data)?;
            let value = get_string(data)?;
            Ok(HeaderOp::Set { name, value })
        }
        1 => {
            let name = get_string(data)?;
            let value = get_string(data)?;
            Ok(HeaderOp::Add { name, value })
        }
        2 => {
            let name = get_string(data)?;
            Ok(HeaderOp::Remove { name })
        }
        _ => Err(AgentProtocolError::InvalidMessage(format!(
            "Unknown header op type: {}",
            op_type
        ))),
    }
}

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

    #[test]
    fn test_message_type_roundtrip() {
        for t in [
            MessageType::HandshakeRequest,
            MessageType::HandshakeResponse,
            MessageType::RequestHeaders,
            MessageType::RequestBodyChunk,
            MessageType::AgentResponse,
            MessageType::Ping,
            MessageType::Pong,
            MessageType::Cancel,
            MessageType::Error,
        ] {
            let byte = t as u8;
            let decoded = MessageType::try_from(byte).unwrap();
            assert_eq!(t, decoded);
        }
    }

    #[test]
    fn test_binary_frame_encode_decode() {
        let frame = BinaryFrame::new(MessageType::Ping, Bytes::from_static(b"hello"));
        let encoded = frame.encode();

        // Verify structure
        assert_eq!(encoded.len(), 4 + 1 + 5); // len + type + payload
        assert_eq!(&encoded[0..4], &[0, 0, 0, 6]); // length = 6 (type + payload)
        assert_eq!(encoded[4], MessageType::Ping as u8);
        assert_eq!(&encoded[5..], b"hello");
    }

    #[test]
    fn test_binary_request_headers_roundtrip() {
        let headers = BinaryRequestHeaders {
            correlation_id: "req-123".to_string(),
            method: "POST".to_string(),
            uri: "/api/test".to_string(),
            headers: {
                let mut h = HashMap::new();
                h.insert(
                    "content-type".to_string(),
                    vec!["application/json".to_string()],
                );
                h.insert(
                    "x-custom".to_string(),
                    vec!["value1".to_string(), "value2".to_string()],
                );
                h
            },
            client_ip: "192.168.1.1".to_string(),
            client_port: 12345,
        };

        let encoded = headers.encode();
        let decoded = BinaryRequestHeaders::decode(encoded).unwrap();

        assert_eq!(decoded.correlation_id, "req-123");
        assert_eq!(decoded.method, "POST");
        assert_eq!(decoded.uri, "/api/test");
        assert_eq!(decoded.client_ip, "192.168.1.1");
        assert_eq!(decoded.client_port, 12345);
        assert_eq!(
            decoded.headers.get("content-type").unwrap(),
            &vec!["application/json".to_string()]
        );
    }

    #[test]
    fn test_binary_body_chunk_roundtrip() {
        let chunk = BinaryBodyChunk {
            correlation_id: "req-456".to_string(),
            chunk_index: 2,
            is_last: true,
            data: Bytes::from_static(b"binary data here"),
        };

        let encoded = chunk.encode();
        let decoded = BinaryBodyChunk::decode(encoded).unwrap();

        assert_eq!(decoded.correlation_id, "req-456");
        assert_eq!(decoded.chunk_index, 2);
        assert!(decoded.is_last);
        assert_eq!(&decoded.data[..], b"binary data here");
    }

    #[test]
    fn test_binary_agent_response_allow() {
        let response = BinaryAgentResponse {
            correlation_id: "req-789".to_string(),
            decision: Decision::Allow,
            request_headers: vec![HeaderOp::Set {
                name: "X-Added".to_string(),
                value: "true".to_string(),
            }],
            response_headers: vec![],
            needs_more: false,
        };

        let encoded = response.encode();
        let decoded = BinaryAgentResponse::decode(encoded).unwrap();

        assert_eq!(decoded.correlation_id, "req-789");
        assert!(matches!(decoded.decision, Decision::Allow));
        assert_eq!(decoded.request_headers.len(), 1);
        assert!(!decoded.needs_more);
    }

    #[test]
    fn test_binary_agent_response_block() {
        let response = BinaryAgentResponse {
            correlation_id: "req-block".to_string(),
            decision: Decision::Block {
                status: 403,
                body: Some("Forbidden".to_string()),
                headers: None,
            },
            request_headers: vec![],
            response_headers: vec![],
            needs_more: false,
        };

        let encoded = response.encode();
        let decoded = BinaryAgentResponse::decode(encoded).unwrap();

        assert_eq!(decoded.correlation_id, "req-block");
        match decoded.decision {
            Decision::Block {
                status,
                body,
                headers,
            } => {
                assert_eq!(status, 403);
                assert_eq!(body, Some("Forbidden".to_string()));
                assert!(headers.is_none());
            }
            _ => panic!("Expected Block decision"),
        }
    }
}