Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
6
akode-rs/stdio-to-uds/BUILD.bazel
Normal file
6
akode-rs/stdio-to-uds/BUILD.bazel
Normal file
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "stdio-to-uds",
|
||||
crate_name = "akode_stdio_to_uds",
|
||||
)
|
||||
33
akode-rs/stdio-to-uds/Cargo.toml
Normal file
33
akode-rs/stdio-to-uds/Cargo.toml
Normal file
@@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "akode-stdio-to-uds"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "akode-stdio-to-uds"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "akode_stdio_to_uds"
|
||||
path = "src/lib.rs"
|
||||
test = false
|
||||
doctest = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
akode-uds = { workspace = true }
|
||||
tokio = { workspace = true, features = [
|
||||
"io-std",
|
||||
"io-util",
|
||||
"macros",
|
||||
"rt-multi-thread",
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
||||
akode-utils-cargo-bin = { workspace = true }
|
||||
pretty_assertions = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
20
akode-rs/stdio-to-uds/README.md
Normal file
20
akode-rs/stdio-to-uds/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# akode-stdio-to-uds
|
||||
|
||||
Traditionally, there are two transport mechanisms for an MCP server: stdio and HTTP.
|
||||
|
||||
This crate helps enable a third, which is UNIX domain socket, because it has the advantages that:
|
||||
|
||||
- The UDS can be attached to long-running process, like an HTTP server.
|
||||
- The UDS can leverage UNIX file permissions to restrict access.
|
||||
|
||||
To that end, this crate provides an adapter between a UDS and stdio. The idea is that someone could start an MCP server that communicates over `/tmp/mcp.sock`. Then the user could specify this on the fly like so:
|
||||
|
||||
```
|
||||
codex --config mcp_servers.example={command="akode-stdio-to-uds",args=["/tmp/mcp.sock"]}
|
||||
```
|
||||
|
||||
Unfortunately, the Rust standard library does not provide support for UNIX domain sockets on Windows today even though support was added in October 2018 in Windows 10:
|
||||
|
||||
https://github.com/rust-lang/rust/issues/56533
|
||||
|
||||
As a workaround, this crate uses `akode-uds`, which provides a cross-platform async UDS API backed by https://crates.io/crates/uds_windows on Windows.
|
||||
46
akode-rs/stdio-to-uds/src/lib.rs
Normal file
46
akode-rs/stdio-to-uds/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
#![deny(clippy::print_stdout)]
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Context;
|
||||
use akode_uds::UnixStream;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
/// Connects to the Unix Domain Socket at `socket_path` and relays data between
|
||||
/// standard input/output and the socket.
|
||||
pub async fn run(socket_path: &Path) -> anyhow::Result<()> {
|
||||
let stream = UnixStream::connect(socket_path)
|
||||
.await
|
||||
.with_context(|| format!("failed to connect to socket at {}", socket_path.display()))?;
|
||||
let (mut socket_reader, mut socket_writer) = tokio::io::split(stream);
|
||||
|
||||
let copy_socket_to_stdout = async {
|
||||
let mut stdout = tokio::io::stdout();
|
||||
tokio::io::copy(&mut socket_reader, &mut stdout).await?;
|
||||
stdout.flush().await?;
|
||||
Ok(())
|
||||
};
|
||||
let copy_stdin_to_socket = async {
|
||||
let mut stdin = tokio::io::stdin();
|
||||
tokio::io::copy(&mut stdin, &mut socket_writer)
|
||||
.await
|
||||
.context("failed to copy data from stdin to socket")?;
|
||||
|
||||
// The peer can close immediately after sending its response; in that
|
||||
// race, half-closing our write side can report NotConnected on some
|
||||
// platforms.
|
||||
if let Err(err) = socket_writer.shutdown().await
|
||||
&& err.kind() != io::ErrorKind::NotConnected
|
||||
{
|
||||
return Err(err).context("failed to shutdown socket writer");
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
};
|
||||
|
||||
tokio::try_join!(copy_stdin_to_socket, copy_socket_to_stdout)
|
||||
.context("failed to relay data between stdio and socket")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
20
akode-rs/stdio-to-uds/src/main.rs
Normal file
20
akode-rs/stdio-to-uds/src/main.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let mut args = env::args_os().skip(1);
|
||||
let Some(socket_path) = args.next() else {
|
||||
eprintln!("Usage: akode-stdio-to-uds <socket-path>");
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
if args.next().is_some() {
|
||||
eprintln!("Expected exactly one argument: <socket-path>");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let socket_path = PathBuf::from(socket_path);
|
||||
akode_stdio_to_uds::run(&socket_path).await
|
||||
}
|
||||
157
akode-rs/stdio-to-uds/tests/stdio_to_uds.rs
Normal file
157
akode-rs/stdio-to-uds/tests/stdio_to_uds.rs
Normal file
@@ -0,0 +1,157 @@
|
||||
use std::io::ErrorKind;
|
||||
use std::io::Read;
|
||||
use std::process::Command;
|
||||
use std::process::ExitStatus;
|
||||
use std::process::Stdio;
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Context;
|
||||
use akode_uds::UnixListener;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipes_stdin_and_stdout_through_socket() -> anyhow::Result<()> {
|
||||
// This test intentionally avoids `read_to_end()` on the server side because
|
||||
// waiting for EOF can race with socket half-close behavior on slower runners.
|
||||
// Reading the exact request length keeps the test deterministic.
|
||||
//
|
||||
// We also use `std::process::Command` (instead of `assert_cmd`) so we can
|
||||
// poll/kill on timeout and include incremental server events + stderr in
|
||||
// failure output, which makes flaky failures actionable to debug.
|
||||
let dir = tempfile::TempDir::new().context("failed to create temp dir")?;
|
||||
let socket_path = dir.path().join("socket");
|
||||
let request = b"request";
|
||||
let request_path = dir.path().join("request.txt");
|
||||
std::fs::write(&request_path, request).context("failed to write child stdin fixture")?;
|
||||
let listener = match UnixListener::bind(&socket_path).await {
|
||||
Ok(listener) => listener,
|
||||
Err(err) if err.kind() == ErrorKind::PermissionDenied => {
|
||||
eprintln!("skipping test: failed to bind unix socket: {err}");
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err).context("failed to bind test unix socket");
|
||||
}
|
||||
};
|
||||
|
||||
let (event_tx, event_rx) = mpsc::channel();
|
||||
let server_task = tokio::spawn(async move {
|
||||
let mut listener = listener;
|
||||
let _ = event_tx.send("waiting for accept".to_string());
|
||||
let mut connection = listener
|
||||
.accept()
|
||||
.await
|
||||
.context("failed to accept test connection")?;
|
||||
let _ = event_tx.send("accepted connection".to_string());
|
||||
let mut received = vec![0; request.len()];
|
||||
connection
|
||||
.read_exact(&mut received)
|
||||
.await
|
||||
.context("failed to read data from client")?;
|
||||
let _ = event_tx.send(format!("read {} bytes", received.len()));
|
||||
connection
|
||||
.write_all(b"response")
|
||||
.await
|
||||
.context("failed to write response to client")?;
|
||||
let _ = event_tx.send("wrote response".to_string());
|
||||
anyhow::Ok(received)
|
||||
});
|
||||
|
||||
struct ChildOutput {
|
||||
status: ExitStatus,
|
||||
stdout: Vec<u8>,
|
||||
stderr: Vec<u8>,
|
||||
server_events: Vec<String>,
|
||||
}
|
||||
|
||||
let child_task = tokio::task::spawn_blocking(move || -> anyhow::Result<ChildOutput> {
|
||||
let stdin =
|
||||
std::fs::File::open(&request_path).context("failed to open child stdin fixture")?;
|
||||
let mut child = Command::new(akode_utils_cargo_bin::cargo_bin("akode-stdio-to-uds")?)
|
||||
.arg(&socket_path)
|
||||
.stdin(Stdio::from(stdin))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.context("failed to spawn akode-stdio-to-uds")?;
|
||||
|
||||
let mut child_stdout = child.stdout.take().context("missing child stdout")?;
|
||||
let mut child_stderr = child.stderr.take().context("missing child stderr")?;
|
||||
let (stdout_tx, stdout_rx) = mpsc::channel();
|
||||
let (stderr_tx, stderr_rx) = mpsc::channel();
|
||||
thread::spawn(move || {
|
||||
let mut stdout = Vec::new();
|
||||
let result = child_stdout.read_to_end(&mut stdout).map(|_| stdout);
|
||||
let _ = stdout_tx.send(result);
|
||||
});
|
||||
thread::spawn(move || {
|
||||
let mut stderr = Vec::new();
|
||||
let result = child_stderr.read_to_end(&mut stderr).map(|_| stderr);
|
||||
let _ = stderr_tx.send(result);
|
||||
});
|
||||
|
||||
let mut server_events = Vec::new();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let status = loop {
|
||||
while let Ok(event) = event_rx.try_recv() {
|
||||
server_events.push(event);
|
||||
}
|
||||
|
||||
if let Some(status) = child.try_wait().context("failed to poll child status")? {
|
||||
break status;
|
||||
}
|
||||
|
||||
if Instant::now() >= deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
let stderr = stderr_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stderr after kill")?
|
||||
.context("failed to read child stderr")?;
|
||||
anyhow::bail!(
|
||||
"akode-stdio-to-uds did not exit in time; server events: {:?}; stderr: {}",
|
||||
server_events,
|
||||
String::from_utf8_lossy(&stderr).trim_end()
|
||||
);
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_millis(25));
|
||||
};
|
||||
|
||||
let stdout = stdout_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stdout")?
|
||||
.context("failed to read child stdout")?;
|
||||
let stderr = stderr_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stderr")?
|
||||
.context("failed to read child stderr")?;
|
||||
|
||||
Ok(ChildOutput {
|
||||
status,
|
||||
stdout,
|
||||
stderr,
|
||||
server_events,
|
||||
})
|
||||
});
|
||||
|
||||
let child_output = child_task.await.context("child task panicked")??;
|
||||
assert!(
|
||||
child_output.status.success(),
|
||||
"akode-stdio-to-uds exited with {status}; server events: {:?}; stderr: {}",
|
||||
child_output.server_events,
|
||||
String::from_utf8_lossy(&child_output.stderr).trim_end(),
|
||||
status = child_output.status
|
||||
);
|
||||
assert_eq!(child_output.stdout, b"response");
|
||||
|
||||
let received = server_task.await.context("server task panicked")??;
|
||||
assert_eq!(received, request);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user