mirror of
https://github.com/openai/codex.git
synced 2026-09-08 15:50:34 +00:00
## Why The process-owned code mode implementation needs an explicit, bounded wire contract before either side depends on it. Keeping framing and message semantics in `codex-code-mode-protocol` gives the client and sidecar one shared source of truth and makes compatibility failures detectable during connection setup. ## What changed - adds a versioned client/host handshake with required and optional capabilities - defines operation requests and responses for session lifecycle and cell control - defines reverse delegate request, response, cancellation, and cell-closure messages - adds a four-byte little-endian length-prefixed JSON codec with a hard frame cap - rejects malformed frames, unknown fields, invalid identifiers, and unsupported protocol states - locks the wire representation down with explicit JSON round-trip tests ## Testing - `just test -p codex-code-mode-protocol` ## Stack Part 1 of 6. Followed by [#29805](https://github.com/openai/codex/pull/29805).
102 lines
2.9 KiB
Rust
102 lines
2.9 KiB
Rust
use std::io;
|
|
use std::mem::size_of;
|
|
|
|
use serde::Serialize;
|
|
use serde::de::DeserializeOwned;
|
|
use tokio::io::AsyncRead;
|
|
use tokio::io::AsyncReadExt;
|
|
use tokio::io::AsyncWrite;
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
/// Maximum JSON payload size accepted for one IPC frame.
|
|
pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024;
|
|
|
|
/// Decodes JSON messages prefixed by a four-byte little-endian payload length.
|
|
pub struct FramedReader<R> {
|
|
reader: R,
|
|
}
|
|
|
|
impl<R> FramedReader<R>
|
|
where
|
|
R: AsyncRead + Unpin,
|
|
{
|
|
pub fn new(reader: R) -> Self {
|
|
Self { reader }
|
|
}
|
|
|
|
/// Reads the next frame, returning `None` only for EOF at a frame boundary.
|
|
pub async fn read<T>(&mut self) -> io::Result<Option<T>>
|
|
where
|
|
T: DeserializeOwned,
|
|
{
|
|
let mut length_bytes = [0_u8; size_of::<u32>()];
|
|
if self.reader.read(&mut length_bytes[..1]).await? == 0 {
|
|
return Ok(None);
|
|
}
|
|
self.reader.read_exact(&mut length_bytes[1..]).await?;
|
|
|
|
let length = u32::from_le_bytes(length_bytes) as usize;
|
|
if length > MAX_FRAME_BYTES {
|
|
return Err(io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("code-mode IPC frame length {length} exceeds {MAX_FRAME_BYTES} bytes"),
|
|
));
|
|
}
|
|
|
|
let mut payload = vec![0; length];
|
|
self.reader.read_exact(&mut payload).await?;
|
|
serde_json::from_slice(&payload).map(Some).map_err(|err| {
|
|
io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("failed to decode code-mode IPC frame: {err}"),
|
|
)
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Encodes JSON messages with a four-byte little-endian payload length.
|
|
pub struct FramedWriter<W> {
|
|
writer: W,
|
|
}
|
|
|
|
impl<W> FramedWriter<W>
|
|
where
|
|
W: AsyncWrite + Unpin,
|
|
{
|
|
pub fn new(writer: W) -> Self {
|
|
Self { writer }
|
|
}
|
|
|
|
/// Writes and flushes one complete frame.
|
|
pub async fn write<T>(&mut self, message: &T) -> io::Result<()>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
let payload = serde_json::to_vec(message).map_err(|err| {
|
|
io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("failed to encode code-mode IPC frame: {err}"),
|
|
)
|
|
})?;
|
|
if payload.len() > MAX_FRAME_BYTES {
|
|
return Err(io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!(
|
|
"code-mode IPC frame length {} exceeds {MAX_FRAME_BYTES} bytes",
|
|
payload.len()
|
|
),
|
|
));
|
|
}
|
|
let length = u32::try_from(payload.len()).map_err(|_| {
|
|
io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
"code-mode IPC frame length exceeds u32",
|
|
)
|
|
})?;
|
|
|
|
self.writer.write_all(&length.to_le_bytes()).await?;
|
|
self.writer.write_all(&payload).await?;
|
|
self.writer.flush().await
|
|
}
|
|
}
|