mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
## Why Pipe output chunks can split one protocol frame or contain several frames. A timed-out read must also retain any partial frame it already consumed so the next exchange can continue decoding it. ## What changed Add a stateful `MessageReader` that buffers partial input, preserves unread bytes from coalesced chunks, and validates frame bounds before reading the payload. Use it for voice helper handshake and shutdown responses. ## Testing Add async tests for fragmented and coalesced frames, cancelled reads, oversized headers, truncated and malformed input, and invalid pipe chunk sizes. GitOrigin-RevId: 274d9698e05f2de2147235b6e4e1bd38dbb7c23b
62 lines
2.0 KiB
Rust
62 lines
2.0 KiB
Rust
//! Read bounded helper frames independently of pipe chunks; cancellation retains partial input.
|
|
|
|
use std::io;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use crate::Message;
|
|
use crate::decode_frame;
|
|
|
|
// Matches the maximum read in codex_utils_pty's pipe output reader. Retain at most
|
|
// one such chunk and one bounded partial frame, never a queue of decoded messages.
|
|
const MAX_CHUNK_BYTES: usize = 8_192;
|
|
|
|
pub(crate) struct MessageReader {
|
|
output: mpsc::Receiver<Vec<u8>>,
|
|
pending: std::vec::IntoIter<u8>,
|
|
frame: Vec<u8>,
|
|
}
|
|
|
|
impl MessageReader {
|
|
pub(crate) fn new(output: mpsc::Receiver<Vec<u8>>) -> Self {
|
|
Self {
|
|
output,
|
|
pending: Vec::new().into_iter(),
|
|
frame: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn next(&mut self) -> io::Result<Message> {
|
|
loop {
|
|
let target = if let [a, b, c, d, ..] = self.frame.as_slice() {
|
|
// Validate the header before consuming or allocating its payload.
|
|
decode_frame(&self.frame)?;
|
|
u32::from_be_bytes([*a, *b, *c, *d]) as usize + 4
|
|
} else {
|
|
4
|
|
};
|
|
while self.frame.len() < target {
|
|
if self.pending.len() == 0 {
|
|
let chunk = self.output.recv().await.ok_or_else(|| {
|
|
io::Error::new(io::ErrorKind::UnexpectedEof, "voice helper output closed")
|
|
})?;
|
|
if chunk.is_empty() || chunk.len() > MAX_CHUNK_BYTES {
|
|
return Err(io::Error::other("invalid voice helper output chunk"));
|
|
}
|
|
self.pending = chunk.into_iter();
|
|
}
|
|
self.frame
|
|
.extend(self.pending.by_ref().take(target - self.frame.len()));
|
|
}
|
|
if let Some(message) = decode_frame(&self.frame)? {
|
|
self.frame.clear();
|
|
return Ok(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "message_reader_tests.rs"]
|
|
mod tests;
|