#![cfg(unix)]
pub mod actions;
use anyhow::{Context, Result};
use bytes::Bytes;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixStream;
use tokio::sync::{mpsc, Mutex};
use tracing::{debug, info};
use super::connection::ConnectionId;
use crate::llm::action_helper::call_llm;
use crate::llm::ollama_client::OllamaClient;
use crate::llm::ActionResult;
use crate::logging::emit::Log;
use crate::protocol::Event;
use crate::server::SocketFileProtocol;
use crate::state::app_state::AppState;
use actions::{SOCKET_FILE_CONNECTION_OPENED_EVENT, SOCKET_FILE_DATA_RECEIVED_EVENT};
const FIRST_BYTE_READ_TIMEOUT: Duration = Duration::from_secs(300);
const IDLE_BETWEEN_MESSAGES_TIMEOUT: Duration = Duration::from_secs(900);
const MAX_CONNECTIONS: usize = crate::server::accept_bounded::DEFAULT_MAX_CONNECTIONS;
const CONNECTION_CAP_REFUSAL: &[u8] = b"";
async fn read_bounded<R>(
reader: &mut R,
buffer: &mut [u8],
activity: &crate::server::accept_bounded::ConnectionActivity,
bound: Duration,
) -> Option<std::io::Result<usize>>
where
R: tokio::io::AsyncRead + Unpin,
{
loop {
match tokio::time::timeout(bound, reader.read(buffer)).await {
Ok(result) => return Some(result),
Err(_) => match activity.idle_for() {
None => continue,
Some(idle) if idle < bound => continue,
Some(_) => return None,
},
}
}
}
#[derive(Debug, Clone, PartialEq)]
enum ConnectionState {
Idle,
Processing,
Accumulating,
}
struct ConnectionData {
state: ConnectionState,
queued_data: Vec<u8>,
write_half: Arc<Mutex<tokio::io::WriteHalf<UnixStream>>>,
}
fn describe_file_type(ft: &std::fs::FileType) -> &'static str {
use std::os::unix::fs::FileTypeExt;
if ft.is_dir() {
"directory"
} else if ft.is_symlink() {
"symlink"
} else if ft.is_fifo() {
"FIFO"
} else if ft.is_char_device() {
"character device"
} else if ft.is_block_device() {
"block device"
} else {
"regular file"
}
}
struct SocketCleanup(PathBuf);
impl Drop for SocketCleanup {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
pub struct SocketFileServer;
impl SocketFileServer {
#[allow(clippy::too_many_arguments)]
pub async fn spawn_with_llm_actions(
socket_path: PathBuf,
llm_client: OllamaClient,
app_state: Arc<AppState>,
status_tx: mpsc::UnboundedSender<String>,
send_first: bool,
server_id: crate::state::ServerId,
first_byte_timeout_secs: Option<u64>,
idle_timeout_secs: Option<u64>,
) -> Result<PathBuf> {
let first_byte_timeout = first_byte_timeout_secs
.map(Duration::from_secs)
.unwrap_or(FIRST_BYTE_READ_TIMEOUT);
let idle_timeout = idle_timeout_secs
.map(Duration::from_secs)
.unwrap_or(IDLE_BETWEEN_MESSAGES_TIMEOUT);
match std::fs::symlink_metadata(&socket_path) {
Ok(meta) => {
use std::os::unix::fs::FileTypeExt;
if !meta.file_type().is_socket() {
anyhow::bail!(
"Refusing to remove {:?}: it exists but is not a Unix domain socket \
(it is a {}). Delete it yourself if that is really what you want, or \
pass a different socket_path.",
socket_path,
describe_file_type(&meta.file_type())
);
}
std::fs::remove_file(&socket_path).with_context(|| {
format!("Failed to remove existing socket file: {:?}", socket_path)
})?;
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
}
Err(e) => {
return Err(e).with_context(|| format!("Failed to stat {:?}", socket_path));
}
}
let listener = tokio::net::UnixListener::bind(&socket_path)
.with_context(|| format!("Failed to bind to socket path: {:?}", socket_path))?;
std::fs::set_permissions(
&socket_path,
<std::fs::Permissions as std::os::unix::fs::PermissionsExt>::from_mode(0o600),
)
.with_context(|| {
format!(
"Failed to restrict permissions on socket file {:?} to owner-only",
socket_path
)
})?;
Log::new(Some(&status_tx))
.info(format!("Socket file server listening on {:?}", socket_path));
let connections = Arc::new(Mutex::new(HashMap::new()));
let protocol = Arc::new(SocketFileProtocol::new());
let socket_path_clone = socket_path.clone();
let task_registrar = app_state.clone();
let cleanup = SocketCleanup(socket_path.clone());
let limiter = crate::server::accept_bounded::ConnectionLimiter::new(MAX_CONNECTIONS);
let accept_handle = tokio::spawn(async move {
let _cleanup = cleanup;
loop {
match crate::server::accept_bounded::accept_bounded_unix(
&listener,
&limiter,
CONNECTION_CAP_REFUSAL,
"Socket file",
Some(&status_tx),
)
.await
{
Ok((stream, permit)) => {
let connection_id =
ConnectionId::new(app_state.get_next_unified_id().await);
info!("Accepted socket file connection {}", connection_id);
let (read_half, write_half) = tokio::io::split(stream);
let write_half_arc = Arc::new(Mutex::new(write_half));
let activity =
Arc::new(crate::server::accept_bounded::ConnectionActivity::new());
use crate::state::server::{
ConnectionState as ServerConnectionState, ConnectionStatus,
ProtocolConnectionInfo,
};
let now = crate::utils::clock::Instant::now();
let dummy_addr = "127.0.0.1:0".parse().unwrap();
let conn_state = ServerConnectionState {
id: connection_id,
remote_addr: dummy_addr,
local_addr: dummy_addr,
bytes_sent: 0,
bytes_received: 0,
packets_sent: 0,
packets_received: 0,
last_activity: now,
status: ConnectionStatus::Active,
status_changed_at: now,
protocol_info: ProtocolConnectionInfo::new(serde_json::json!({
"state": "Idle",
"socket_path": socket_path_clone.to_string_lossy()
})),
};
app_state
.add_connection_to_server(server_id, conn_state)
.await;
let _ = status_tx.send("__UPDATE_UI__".to_string());
connections.lock().await.insert(
connection_id,
ConnectionData {
state: ConnectionState::Idle,
queued_data: Vec::new(),
write_half: write_half_arc.clone(),
},
);
if send_first {
let llm_client_clone = llm_client.clone();
let app_state_clone = app_state.clone();
let status_tx_clone = status_tx.clone();
let connections_clone = connections.clone();
let write_half_for_conn = write_half_arc.clone();
let protocol_clone = protocol.clone();
let banner_busy = activity.busy();
let task_owner = app_state.clone();
task_owner
.spawn_server_task(server_id, async move {
let _banner_busy = banner_busy;
Self::send_banner(
connection_id,
server_id,
llm_client_clone,
app_state_clone,
status_tx_clone,
connections_clone,
write_half_for_conn,
protocol_clone,
)
.await;
})
.await;
}
let llm_client_clone = llm_client.clone();
let app_state_clone = app_state.clone();
let status_tx_clone = status_tx.clone();
let connections_clone = connections.clone();
let protocol_clone = protocol.clone();
let activity_clone = activity.clone();
let task_owner = app_state.clone();
task_owner
.spawn_server_task(server_id, async move {
let _permit = permit;
let mut buffer = vec![0u8; 8192];
let mut read_half = read_half;
let mut seen_bytes = false;
loop {
let bound = if seen_bytes {
idle_timeout
} else {
first_byte_timeout
};
let read = match read_bounded(
&mut read_half,
&mut buffer,
&activity_clone,
bound,
)
.await
{
Some(result) => result,
None => {
connections_clone.lock().await.remove(&connection_id);
app_state_clone
.close_connection_on_server(
server_id,
connection_id,
)
.await;
Log::new(Some(&status_tx_clone)).info(format!(
"Socket file connection {connection_id} sent \
nothing for {}s; closing idle connection",
bound.as_secs()
));
let _ =
status_tx_clone.send("__UPDATE_UI__".to_string());
break;
}
};
match read {
Ok(0) => {
connections_clone.lock().await.remove(&connection_id);
app_state_clone
.close_connection_on_server(
server_id,
connection_id,
)
.await;
Log::new(Some(&status_tx_clone)).info(format!(
"Socket file connection {connection_id} closed"
));
let _ =
status_tx_clone.send("__UPDATE_UI__".to_string());
break;
}
Ok(n) => {
seen_bytes = true;
activity_clone.touch();
let data = Bytes::copy_from_slice(&buffer[..n]);
app_state_clone
.update_connection_stats(
server_id,
connection_id,
Some(n as u64),
None,
Some(1),
None,
)
.await;
let log = Log::new(Some(&status_tx_clone));
if data.iter().all(|&b| {
b.is_ascii_graphic() || b.is_ascii_whitespace()
}) {
let data_str = String::from_utf8_lossy(&data);
let preview =
crate::utils::truncate_for_log(&data_str, 100);
log.debug(format!(
"Socket file received {} bytes on {}: {}",
n, connection_id, preview
));
log.trace(format!(
"Socket file data (text): {:?}",
data_str
));
} else {
log.debug(format!(
"Socket file received {} bytes on {} (binary data)",
n, connection_id
));
log.trace(format!(
"Socket file data (hex): {}",
hex::encode(&data)
));
}
let llm_clone = llm_client_clone.clone();
let state_clone = app_state_clone.clone();
let status_clone = status_tx_clone.clone();
let conns_clone = connections_clone.clone();
let protocol_clone = protocol_clone.clone();
let busy = activity_clone.busy();
let task_owner = app_state_clone.clone();
task_owner
.spawn_server_task(server_id, async move {
let _busy = busy;
Self::handle_data_with_actions(
connection_id,
server_id,
data,
llm_clone,
state_clone,
status_clone,
conns_clone,
protocol_clone,
)
.await;
})
.await;
}
Err(e) => {
Log::new(Some(&status_tx_clone)).error(format!(
"Read error on socket file connection {}: {}",
connection_id, e
));
connections_clone.lock().await.remove(&connection_id);
break;
}
}
}
})
.await;
}
Err(e) => {
Log::new(Some(&status_tx))
.error(format!("Accept error on socket file: {}", e));
break;
}
}
}
});
task_registrar
.register_server_task(server_id, accept_handle)
.await;
Ok(socket_path)
}
#[allow(clippy::too_many_arguments)]
async fn send_banner(
connection_id: ConnectionId,
server_id: crate::state::ServerId,
llm_client: OllamaClient,
app_state: Arc<AppState>,
status_tx: mpsc::UnboundedSender<String>,
connections: Arc<Mutex<HashMap<ConnectionId, ConnectionData>>>,
write_half: Arc<Mutex<tokio::io::WriteHalf<UnixStream>>>,
protocol: Arc<SocketFileProtocol>,
) {
{
let log = Log::new(Some(&status_tx));
let event = Event::new(&SOCKET_FILE_CONNECTION_OPENED_EVENT, serde_json::json!({}));
match call_llm(
&llm_client,
&app_state,
server_id,
Some(connection_id),
&event,
protocol.as_ref(),
)
.await
{
Ok(execution_result) => {
debug!("LLM socket file banner response received");
for msg in execution_result.messages {
let _ = status_tx.send(msg);
}
let mut wrote_banner = false;
for protocol_result in execution_result.protocol_results {
match protocol_result {
ActionResult::Output(output_data) => {
let mut write = write_half.lock().await;
if let Err(e) = write.write_all(&output_data).await {
log.error(format!("Failed to send socket file banner: {}", e));
} else {
app_state
.update_connection_stats(
server_id,
connection_id,
None,
Some(output_data.len() as u64),
None,
Some(1),
)
.await;
if output_data
.iter()
.all(|&b| b.is_ascii_graphic() || b.is_ascii_whitespace())
{
let data_str = String::from_utf8_lossy(&output_data);
let preview =
crate::utils::truncate_for_log(&data_str, 100);
log.debug(format!(
"Socket file sent {} bytes to {}: {}",
output_data.len(),
connection_id,
preview
));
log.trace(format!(
"Socket file sent (text): {:?}",
data_str
));
} else {
log.debug(format!(
"Socket file sent {} bytes to {} (binary data)",
output_data.len(),
connection_id
));
log.trace(format!(
"Socket file sent (hex): {}",
hex::encode(&output_data)
));
}
log.debug(format!(
"Sent banner to socket file connection {connection_id}"
));
wrote_banner = true;
}
}
ActionResult::CloseConnection => {
connections.lock().await.remove(&connection_id);
log.info(format!(
"Closed socket file connection {connection_id} after banner: decision=model_close"
));
}
_ => {}
}
}
if !wrote_banner {
log.debug(format!(
"No banner bytes for socket file connection {connection_id}: decision=model_no_actions"
));
}
}
Err(e) => {
let failure = crate::utils::WireFailure::classify(&e);
let class = if failure.is_overloaded() {
"overloaded"
} else {
"unavailable"
};
log.warn(format!(
"Socket file banner failed for {connection_id}: decision=fail_closed_llm_error class={class} error={e}"
));
{
let mut write = write_half.lock().await;
let _ = write.shutdown().await;
}
connections.lock().await.remove(&connection_id);
app_state
.close_connection_on_server(server_id, connection_id)
.await;
log.info(format!(
"Closed socket file connection {connection_id} after banner LLM error"
));
}
}
}
}
async fn handle_data_with_actions(
connection_id: ConnectionId,
server_id: crate::state::ServerId,
data: Bytes,
llm_client: OllamaClient,
app_state: Arc<AppState>,
status_tx: mpsc::UnboundedSender<String>,
connections: Arc<Mutex<HashMap<ConnectionId, ConnectionData>>>,
protocol: Arc<SocketFileProtocol>,
) {
let log = Log::new(Some(&status_tx));
let current_state = {
let conns = connections.lock().await;
if let Some(conn_data) = conns.get(&connection_id) {
conn_data.state.clone()
} else {
debug!(
"socket-file connection {} is no longer registered; dropping {} received bytes",
connection_id,
data.len()
);
return;
}
};
if current_state == ConnectionState::Processing {
connections
.lock()
.await
.entry(connection_id)
.and_modify(|conn| {
conn.queued_data.extend_from_slice(&data);
});
log.debug(format!(
"Queued {} bytes for socket file connection {}",
data.len(),
connection_id
));
return;
}
let mut all_data = {
let mut conns = connections.lock().await;
let Some(conn_data) = conns.get_mut(&connection_id) else {
debug!(
"socket-file connection {} went away before its {} received bytes could be \
processed",
connection_id,
data.len()
);
return;
};
conn_data.state = ConnectionState::Processing;
let mut merged = conn_data.queued_data.clone();
merged.extend_from_slice(&data);
conn_data.queued_data.clear();
Bytes::from(merged)
};
loop {
let write_half = {
let conns = connections.lock().await;
conns.get(&connection_id).map(|c| c.write_half.clone())
};
let Some(write_half) = write_half else {
debug!(
"socket-file connection {} went away before its response could be written",
connection_id
);
return;
};
let (data_str, data_encoding) = if all_data
.iter()
.all(|&b| b.is_ascii_graphic() || b.is_ascii_whitespace())
{
(String::from_utf8_lossy(&all_data).to_string(), "utf8")
} else {
(hex::encode(&all_data), "hex")
};
let event = Event::new(
&SOCKET_FILE_DATA_RECEIVED_EVENT,
serde_json::json!({
"data": data_str,
"encoding": data_encoding
}),
);
match call_llm(
&llm_client,
&app_state,
server_id,
Some(connection_id),
&event,
protocol.as_ref(),
)
.await
{
Ok(execution_result) => {
debug!("LLM socket file response received");
for msg in execution_result.messages {
let _ = status_tx.send(msg);
}
let mut should_close = false;
let mut should_wait = false;
let mut wrote_output = false;
for protocol_result in execution_result.protocol_results {
match protocol_result {
ActionResult::Output(output_data) => {
let mut write = write_half.lock().await;
if let Err(e) = write.write_all(&output_data).await {
log.error(format!(
"Failed to send socket file response: {}",
e
));
} else {
app_state
.update_connection_stats(
server_id,
connection_id,
None,
Some(output_data.len() as u64),
None,
Some(1),
)
.await;
if output_data
.iter()
.all(|&b| b.is_ascii_graphic() || b.is_ascii_whitespace())
{
let data_str = String::from_utf8_lossy(&output_data);
let preview =
crate::utils::truncate_for_log(&data_str, 100);
log.debug(format!(
"Socket file sent {} bytes to {}: {}",
output_data.len(),
connection_id,
preview
));
log.trace(format!(
"Socket file sent (text): {:?}",
data_str
));
} else {
log.debug(format!(
"Socket file sent {} bytes to {} (binary data)",
output_data.len(),
connection_id
));
log.trace(format!(
"Socket file sent (hex): {}",
hex::encode(&output_data)
));
}
log.debug(format!(
"Sent {} bytes to socket file connection {}",
output_data.len(),
connection_id
));
wrote_output = true;
}
}
ActionResult::CloseConnection => {
should_close = true;
}
ActionResult::WaitForMore => {
should_wait = true;
}
_ => {}
}
}
if should_wait {
connections
.lock()
.await
.entry(connection_id)
.and_modify(|conn| conn.state = ConnectionState::Accumulating);
log.debug(format!(
"Waiting for more data from socket file connection {connection_id}"
));
return;
}
if should_close {
connections.lock().await.remove(&connection_id);
log.info(format!(
"Closed socket file connection {connection_id}: decision=model_close"
));
return;
}
if !wrote_output {
log.debug(format!(
"No response bytes for socket file connection {connection_id}: decision=model_no_actions"
));
}
let has_queued = {
let conns = connections.lock().await;
conns
.get(&connection_id)
.map(|c| !c.queued_data.is_empty())
.unwrap_or(false)
};
if has_queued {
let queued = {
let mut conns = connections.lock().await;
match conns.get_mut(&connection_id) {
Some(conn) => std::mem::take(&mut conn.queued_data),
None => return,
}
};
if queued.is_empty() {
connections
.lock()
.await
.entry(connection_id)
.and_modify(|conn| conn.state = ConnectionState::Idle);
return;
}
all_data = Bytes::from(queued);
} else {
connections
.lock()
.await
.entry(connection_id)
.and_modify(|conn| conn.state = ConnectionState::Idle);
return;
}
}
Err(e) => {
let failure = crate::utils::WireFailure::classify(&e);
let class = if failure.is_overloaded() {
"overloaded"
} else {
"unavailable"
};
log.warn(format!(
"LLM error for socket file data on {connection_id}: decision=fail_closed_llm_error class={class} error={e}"
));
if failure.is_overloaded() {
log.warn(format!(
"Socket file connection {connection_id} closed: LLM capacity exhausted"
));
}
{
let mut write = write_half.lock().await;
let _ = write.shutdown().await;
}
connections.lock().await.remove(&connection_id);
app_state
.close_connection_on_server(server_id, connection_id)
.await;
log.info(format!(
"Closed socket file connection {connection_id} after LLM error"
));
return;
}
}
}
}
}