use std::{
io::{self, BufRead, Read, Write},
net::{Ipv4Addr, Shutdown, SocketAddr},
path::Path,
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
},
time::Duration,
};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use tokio::{
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
net::TcpListener,
sync::{Semaphore, mpsc, oneshot},
task::JoinHandle,
};
use crate::{
agent::error::Failure,
llm::stdio_mcp,
platform,
types::{RequestKey, SessionId},
};
use super::{
VerifyBridgeState,
wire::{LINE_BYTES, McpEnv, McpServer},
};
const PORT_ENV: &str = "ARTIFACTIZE_ACP_MCP_PORT";
const CAPABILITY_ENV: &str = "ARTIFACTIZE_ACP_MCP_CAPABILITY";
const CONNECT_TIMEOUT: Duration = Duration::from_secs(2);
const QUEUED_CALLS: usize = 1;
const MAX_BRIDGE_CONNECTIONS: usize = 4;
pub const NO_ACTIVE_TURN_CODE: i64 = -32000;
const INTERNAL_KEY_HEX_LEN: usize = 32;
pub const ALLOWLISTED_TOOLS: &[&str] = &[
"status",
"config_graph",
"request_logs",
"config_check",
"run_list",
"run_show",
"request_list",
"request_show",
"session_show",
"tools_check",
"doctor",
"models",
"remote_status",
"cache_show",
"plugin_list",
"verify_start",
"verify_status",
"verify_wait",
"verify_watch",
"verify_result",
"verify_cancel",
];
pub fn is_allowlisted(name: &str) -> bool {
ALLOWLISTED_TOOLS.contains(&name)
}
pub fn internal_chat_request_key(
session_id: &SessionId,
send: usize,
agent_request_key: &str,
) -> Result<RequestKey, String> {
let mut hasher = Sha256::new();
hasher.update(session_id.as_str().as_bytes());
hasher.update([0]);
hasher.update(send.to_string().as_bytes());
hasher.update([0]);
hasher.update(agent_request_key.as_bytes());
let digest = hex_prefix(hasher.finalize().as_slice(), INTERNAL_KEY_HEX_LEN);
RequestKey::internal_chat(format!("chat/{digest}"))
}
pub fn canonical_args_hash(arguments: &Value) -> String {
let canonical = canonicalize_json(arguments, true);
let encoded = serde_json::to_string(&canonical).unwrap_or_default();
let digest = Sha256::digest(encoded.as_bytes());
hex_prefix(digest.as_slice(), 64)
}
fn canonicalize_json(value: &Value, strip_request_key: bool) -> Value {
match value {
Value::Object(map) => {
let mut sorted = serde_json::Map::new();
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
for key in keys {
if strip_request_key && (key == "request_key" || key == "requestKey") {
continue;
}
sorted.insert(key.clone(), canonicalize_json(&map[key], false));
}
Value::Object(sorted)
}
Value::Array(items) => Value::Array(
items
.iter()
.map(|item| canonicalize_json(item, false))
.collect(),
),
other => other.clone(),
}
}
fn hex_prefix(bytes: &[u8], chars: usize) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(chars);
for &byte in bytes {
if out.len() >= chars {
break;
}
out.push(HEX[(byte >> 4) as usize] as char);
if out.len() >= chars {
break;
}
out.push(HEX[(byte & 0x0f) as usize] as char);
}
out
}
pub struct BridgeCall {
pub id: stdio_mcp::RpcId,
pub name: String,
pub arguments: Value,
pub reply: oneshot::Sender<Value>,
}
pub struct VerifyBridge {
port: String,
capability: String,
calls: mpsc::Receiver<BridgeCall>,
active_turn: Arc<AtomicBool>,
connected: Arc<AtomicBool>,
in_flight: Arc<AtomicUsize>,
task: Option<JoinHandle<()>>,
}
impl VerifyBridge {
pub fn start(tools: stdio_mcp::Server) -> Result<Self, Failure> {
let address = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));
let listener = platform::net::exclusive_listener(address).map_err(bridge_error)?;
let port = listener
.local_addr()
.map_err(bridge_error)?
.port()
.to_string();
let capability = crate::agent::uuid()?;
let (send, calls) = mpsc::channel(QUEUED_CALLS);
let active_turn = Arc::new(AtomicBool::new(false));
let connected = Arc::new(AtomicBool::new(false));
let in_flight = Arc::new(AtomicUsize::new(0));
let task = tokio::spawn(serve(
listener,
capability.clone(),
Arc::new(tools),
send,
active_turn.clone(),
connected.clone(),
in_flight.clone(),
));
Ok(Self {
port,
capability,
calls,
active_turn,
connected,
in_flight,
task: Some(task),
})
}
pub fn mcp_server(&self, executable: &Path) -> McpServer {
McpServer {
name: stdio_mcp::NAME.to_owned(),
command: executable.to_path_buf(),
args: vec!["__chat-mcp".to_owned()],
env: vec![
McpEnv {
name: PORT_ENV.to_owned(),
value: self.port.clone(),
},
McpEnv {
name: CAPABILITY_ENV.to_owned(),
value: self.capability.clone(),
},
],
}
}
pub fn arm_turn(&self) {
self.active_turn.store(true, Ordering::SeqCst);
}
pub fn disarm_turn(&self) {
self.active_turn.store(false, Ordering::SeqCst);
}
pub fn state(&self) -> VerifyBridgeState {
if self.connected.load(Ordering::Relaxed) {
VerifyBridgeState::Connected
} else {
VerifyBridgeState::NotConnected
}
}
pub fn in_flight_calls(&self) -> usize {
self.in_flight.load(Ordering::SeqCst)
}
pub async fn next(&mut self) -> Option<BridgeCall> {
self.calls.recv().await
}
pub async fn stop(mut self) {
self.disarm_turn();
if let Some(task) = self.task.take() {
task.abort();
let _ = crate::task::joined(task.await);
}
}
}
impl Drop for VerifyBridge {
fn drop(&mut self) {
self.disarm_turn();
if let Some(task) = self.task.take() {
task.abort();
}
}
}
fn bridge_error(error: io::Error) -> Failure {
Failure::from(format!("Cannot open the ACP Verify MCP bridge: {error}."))
}
async fn serve(
listener: TcpListener,
capability: String,
tools: Arc<stdio_mcp::Server>,
calls: mpsc::Sender<BridgeCall>,
active_turn: Arc<AtomicBool>,
connected: Arc<AtomicBool>,
in_flight: Arc<AtomicUsize>,
) {
let slots = Arc::new(Semaphore::new(MAX_BRIDGE_CONNECTIONS));
while let Ok((mut stream, _)) = listener.accept().await {
let Ok(permit) = slots.clone().try_acquire_owned() else {
continue;
};
let capability = capability.clone();
let tools = tools.clone();
let calls = calls.clone();
let active_turn = active_turn.clone();
let connected = connected.clone();
let in_flight = in_flight.clone();
tokio::spawn(async move {
let _permit = permit;
let mut presented = vec![0u8; capability.len()];
let read =
tokio::time::timeout(CONNECT_TIMEOUT, stream.read_exact(&mut presented)).await;
if matches!(read, Ok(Ok(_))) && presented == capability.as_bytes() {
let _ =
serve_stdio(stream, &tools, &calls, &active_turn, &connected, &in_flight).await;
}
});
}
}
async fn serve_stdio(
stream: tokio::net::TcpStream,
tools: &stdio_mcp::Server,
calls: &mpsc::Sender<BridgeCall>,
active_turn: &AtomicBool,
connected: &AtomicBool,
in_flight: &AtomicUsize,
) -> io::Result<()> {
let (input, mut output) = stream.into_split();
let mut input = BufReader::new(input);
loop {
let mut line = Vec::new();
if (&mut input)
.take(LINE_BYTES + 1)
.read_until(b'\n', &mut line)
.await?
== 0
{
return Ok(());
}
if line.len() as u64 > LINE_BYTES {
return Err(io::ErrorKind::InvalidData.into());
}
let request = serde_json::from_slice(&line);
line.clear();
let reply = match request {
Ok(stdio_mcp::Rpc::Unknown { id: None, .. }) => continue,
Ok(rpc) => match tools.answer(rpc) {
stdio_mcp::Answer::Reply(reply) => {
connected.store(true, Ordering::Relaxed);
reply
}
stdio_mcp::Answer::Call {
id,
name,
arguments,
} => {
if !active_turn.load(Ordering::SeqCst) {
no_active_turn_error(&id)
} else if !is_allowlisted(&name) {
stdio_mcp::call_error(
&id,
&format!("Tool {name:?} is not allowlisted on the ACP Verify bridge."),
)
} else {
let _guard = InFlightGuard::enter(in_flight);
let (reply_tx, receive) = oneshot::channel();
if calls
.send(BridgeCall {
id: id.clone(),
name,
arguments,
reply: reply_tx,
})
.await
.is_err()
{
no_active_turn_error(&id)
} else {
match receive.await {
Ok(reply) => reply,
Err(_) => no_active_turn_error(&id),
}
}
}
}
},
Err(_) => return Err(io::ErrorKind::InvalidData.into()),
};
serde_json::to_writer(&mut line, &reply)?;
line.push(b'\n');
output.write_all(&line).await?;
}
}
struct InFlightGuard<'a>(&'a AtomicUsize);
impl<'a> InFlightGuard<'a> {
fn enter(counter: &'a AtomicUsize) -> Self {
counter.fetch_add(1, Ordering::SeqCst);
Self(counter)
}
}
impl Drop for InFlightGuard<'_> {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::SeqCst);
}
}
fn no_active_turn_error(id: &stdio_mcp::RpcId) -> Value {
json!({
"jsonrpc": "2.0",
"id": id,
"error": {
"code": NO_ACTIVE_TURN_CODE,
"message": "No active Chat turn."
}
})
}
#[doc(hidden)]
pub fn stdio() -> Result<(), String> {
let unavailable = || "No ACP Chat Verify bridge is available.".to_owned();
let port = platform::environment::var_text(PORT_ENV)
.and_then(|port| port.parse::<u16>().ok())
.filter(|port| *port != 0)
.ok_or_else(unavailable)?;
let capability = platform::environment::var_text(CAPABILITY_ENV).ok_or_else(unavailable)?;
let mut stream =
platform::net::connect_loopback(port, CONNECT_TIMEOUT).map_err(|_| unavailable())?;
stream
.write_all(capability.as_bytes())
.map_err(|_| unavailable())?;
let mut send = stream.try_clone().map_err(|_| unavailable())?;
std::thread::Builder::new()
.name("chat-mcp-stdin".into())
.spawn(move || {
let _ = relay(io::stdin().lock(), &mut send);
let _ = send.shutdown(Shutdown::Write);
})
.map_err(|_| unavailable())?;
relay(io::BufReader::new(&mut stream), &mut io::stdout().lock()).map_err(|_| unavailable())?;
Ok(())
}
fn relay(mut read: impl BufRead, write: &mut impl Write) -> io::Result<()> {
let mut line = Vec::new();
while (&mut read)
.take(LINE_BYTES + 1)
.read_until(b'\n', &mut line)?
!= 0
{
if line.len() as u64 > LINE_BYTES {
return Err(io::ErrorKind::InvalidData.into());
}
write.write_all(&line)?;
write.flush()?;
line.clear();
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn internal_chat_key_uses_disjoint_slash_namespace_and_deterministic_args_hash() {
let session: SessionId = "chat-abc".parse().unwrap();
let key = internal_chat_request_key(&session, 2, "agent-key-1").unwrap();
assert!(key.as_str().starts_with("chat/"));
assert_eq!(key.as_str().len(), 37);
assert!(key.as_str().parse::<RequestKey>().is_err());
let hash_a = canonical_args_hash(&json!({
"request_key": "k1",
"scope": { "kind": "all" },
"speculate": true
}));
let hash_b = canonical_args_hash(&json!({
"speculate": true,
"scope": { "kind": "all" },
"request_key": "k2"
}));
assert_eq!(hash_a, hash_b);
let hash_c = canonical_args_hash(&json!({
"scope": { "kind": "eval", "evalId": "a/b" },
"speculate": true
}));
assert_ne!(hash_a, hash_c);
}
#[test]
fn allowlist_excludes_file_shell_and_plugin_mutation_tools() {
assert!(is_allowlisted("verify_start"));
assert!(is_allowlisted("status"));
assert!(is_allowlisted("plugin_list"));
for excluded in [
"read",
"list",
"glob",
"grep",
"write",
"edit",
"run_command",
"plugin_add",
"plugin_remove",
] {
assert!(!is_allowlisted(excluded), "{excluded} must be excluded");
}
}
}