Base: codex-main originale (prima del fork aKode)

This commit is contained in:
Andrea
2026-11-09 10:37:42 +01:00
commit 7874033c81
10166 changed files with 2728937 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
//! Platform-specific pipe ownership beneath the shared MCP framing layer.
use std::io;
use codex_utils_pty::Child;
use codex_utils_pty::Command;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub(super) type Stdin = tokio::net::windows::named_pipe::NamedPipeServer;
#[cfg(windows)]
pub(super) type Stdout = tokio::net::windows::named_pipe::NamedPipeServer;
#[cfg(windows)]
pub(super) type Stderr = tokio::net::windows::named_pipe::NamedPipeServer;
#[cfg(not(windows))]
pub(super) type Stdin = tokio::process::ChildStdin;
#[cfg(not(windows))]
pub(super) type Stdout = tokio::process::ChildStdout;
#[cfg(not(windows))]
pub(super) type Stderr = tokio::process::ChildStderr;
pub(super) struct LocalStdio {
pub(super) child: Child,
pub(super) stdin: Stdin,
pub(super) stdout: Stdout,
pub(super) stderr: Option<Stderr>,
}
pub(super) fn spawn(command: Command) -> io::Result<LocalStdio> {
#[cfg(windows)]
{
let mut command = command;
let pipes = windows::Stdio::configure(&mut command)?;
Ok(LocalStdio {
child: command.spawn()?,
stdin: pipes.stdin,
stdout: pipes.stdout,
stderr: Some(pipes.stderr),
})
}
#[cfg(not(windows))]
{
let mut child = command.spawn()?;
let stdin = child
.stdin
.take()
.ok_or_else(|| io::Error::other("MCP server stdin was not piped"))?;
let stdout = child
.stdout
.take()
.ok_or_else(|| io::Error::other("MCP server stdout was not piped"))?;
let stderr = child.stderr.take();
Ok(LocalStdio {
child,
stdin,
stdout,
stderr,
})
}
}

View File

@@ -0,0 +1,137 @@
//! IOCP-backed MCP pipes with private, preconnected child endpoints.
use std::fs::File;
use std::fs::OpenOptions;
use std::io;
use std::os::windows::fs::OpenOptionsExt;
use std::ptr;
use codex_utils_pty::ChildStdin;
use codex_utils_pty::Command;
use tokio::net::windows::named_pipe::NamedPipeServer;
use tokio::net::windows::named_pipe::PipeMode;
use tokio::net::windows::named_pipe::ServerOptions;
use uuid::Uuid;
use windows_sys::Win32::Foundation::GENERIC_WRITE;
use windows_sys::Win32::Foundation::LocalFree;
use windows_sys::Win32::Security::Authorization::ConvertStringSecurityDescriptorToSecurityDescriptorW;
use windows_sys::Win32::Security::Authorization::SDDL_REVISION_1;
use windows_sys::Win32::Security::PSECURITY_DESCRIPTOR;
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::FILE_READ_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::SECURITY_IDENTIFICATION;
use windows_sys::Win32::Storage::FileSystem::SECURITY_SQOS_PRESENT;
pub(super) struct Stdio {
pub(super) stdin: NamedPipeServer,
pub(super) stdout: NamedPipeServer,
pub(super) stderr: NamedPipeServer,
}
impl Stdio {
pub(super) fn configure(command: &mut Command) -> io::Result<Self> {
let security = PipeSecurity::new()?;
let (stdin, child_stdin) = connected_pipe(Direction::ChildReads, &security)?;
let (stdout, child_stdout) = connected_pipe(Direction::ChildWrites, &security)?;
let (stderr, child_stderr) = connected_pipe(Direction::ChildWrites, &security)?;
command
.stdin(ChildStdin::File(child_stdin.into()))
.stdout_handle(child_stdout.into())
.stderr_handle(child_stderr.into());
Ok(Self {
stdin,
stdout,
stderr,
})
}
}
enum Direction {
ChildReads,
ChildWrites,
}
fn connected_pipe(
direction: Direction,
security: &PipeSecurity,
) -> io::Result<(NamedPipeServer, File)> {
let name = format!(r"\\.\pipe\codex-mcp-{}", Uuid::new_v4().simple());
let mut options = ServerOptions::new();
options
.pipe_mode(PipeMode::Byte)
.first_pipe_instance(true)
.max_instances(1)
.reject_remote_clients(true);
let mut client = OpenOptions::new();
client.security_qos_flags(SECURITY_IDENTIFICATION | SECURITY_SQOS_PRESENT);
match direction {
Direction::ChildReads => {
options.access_inbound(false).access_outbound(true);
client.read(true);
}
Direction::ChildWrites => {
options.access_inbound(true).access_outbound(false);
// Helpers can query the blocking mode of their stdout and stderr pipes.
client.access_mode(GENERIC_WRITE | FILE_READ_ATTRIBUTES);
}
}
let mut attributes = SECURITY_ATTRIBUTES {
nLength: u32::try_from(size_of::<SECURITY_ATTRIBUTES>()).map_err(io::Error::other)?,
lpSecurityDescriptor: security.0,
bInheritHandle: 0,
};
// SAFETY: attributes and its owned descriptor stay live through CreateNamedPipeW.
// The kernel copies the descriptor; the returned pipe owns its separate handle.
let server = unsafe {
options.create_with_security_attributes_raw(&name, ptr::from_mut(&mut attributes).cast())
}?;
let child = client.open(&name)?;
// The child endpoint is already connected. Complete registration on the
// launcher's blocking thread before the child is allowed to execute.
tokio::runtime::Handle::current().block_on(server.connect())?;
Ok((server, child))
}
struct PipeSecurity(PSECURITY_DESCRIPTOR);
impl PipeSecurity {
fn new() -> io::Result<Self> {
// Only the pipe owner and SYSTEM can open the single local instance.
let sddl: Vec<u16> = "D:P(A;;GA;;;SY)(A;;GA;;;OW)"
.encode_utf16()
.chain(std::iter::once(0))
.collect();
let mut descriptor = ptr::null_mut();
// SAFETY: sddl is NUL-terminated and descriptor is a valid writable out
// pointer. On success Windows transfers a LocalFree-owned allocation.
if unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
sddl.as_ptr(),
SDDL_REVISION_1,
&mut descriptor,
ptr::null_mut(),
)
} == 0
{
return Err(io::Error::last_os_error());
}
Ok(Self(descriptor))
}
}
impl Drop for PipeSecurity {
fn drop(&mut self) {
// SAFETY: this is the sole owner of the successful SDDL conversion's
// allocation; no pipe creation call retains a pointer to it.
if !unsafe { LocalFree(self.0) }.is_null() {
tracing::warn!(
"Failed to release MCP pipe security descriptor: {}",
io::Error::last_os_error()
);
}
}
}
#[cfg(test)]
#[path = "windows_tests.rs"]
mod tests;

View File

@@ -0,0 +1,37 @@
//! Checks the access rights of the synchronous pipe endpoint inherited by a helper.
#![cfg(test)]
use std::io;
use std::os::windows::io::AsRawHandle;
use std::ptr;
use windows_sys::Win32::System::Pipes::GetNamedPipeHandleStateW;
use super::Direction;
use super::PipeSecurity;
use super::connected_pipe;
#[test]
fn child_can_read_output_pipe_mode() -> io::Result<()> {
let runtime = tokio::runtime::Runtime::new()?;
let _guard = runtime.enter();
let (_server, child) = connected_pipe(Direction::ChildWrites, &PipeSecurity::new()?)?;
let mut mode = 0;
// SAFETY: the file owns a live pipe handle and mode is writable for the call.
if unsafe {
GetNamedPipeHandleStateW(
child.as_raw_handle(),
&mut mode,
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
/*nmaxusernamesize*/ 0,
)
} == 0
{
return Err(io::Error::last_os_error());
}
Ok(())
}