feat: configurable exec timeout per request (--timeout flag, default 30s)

This commit is contained in:
Helios Agent 2026-03-03 16:05:29 +01:00
parent 1c0af1693b
commit 537ed95a3c
No known key found for this signature in database
GPG key ID: C8259547CD8309B5
5 changed files with 44 additions and 17 deletions

View file

@ -60,6 +60,19 @@ async fn dispatch<F>(
op: &str,
make_msg: F,
) -> Result<ClientMessage, (StatusCode, Json<ErrorBody>)>
where
F: FnOnce(Uuid) -> ServerMessage,
{
dispatch_with_timeout(state, session_id, op, make_msg, REQUEST_TIMEOUT).await
}
async fn dispatch_with_timeout<F>(
state: &AppState,
session_id: &str,
op: &str,
make_msg: F,
timeout: Duration,
) -> Result<ClientMessage, (StatusCode, Json<ErrorBody>)>
where
F: FnOnce(Uuid) -> ServerMessage,
{
@ -86,7 +99,7 @@ where
send_error(session_id, op)
})?;
match tokio::time::timeout(REQUEST_TIMEOUT, rx).await {
match tokio::time::timeout(timeout, rx).await {
Ok(Ok(response)) => Ok(response),
Ok(Err(_)) => Err(send_error(session_id, op)),
Err(_) => Err(timeout_error(session_id, op)),
@ -143,6 +156,9 @@ pub async fn request_screenshot(
#[derive(Deserialize)]
pub struct ExecBody {
pub command: String,
/// Optional timeout in milliseconds (default: 30000). Use higher values for
/// long-running commands like downloads.
pub timeout_ms: Option<u64>,
}
pub async fn request_exec(
@ -150,10 +166,16 @@ pub async fn request_exec(
State(state): State<AppState>,
Json(body): Json<ExecBody>,
) -> impl IntoResponse {
match dispatch(&state, &session_id, "exec", |rid| ServerMessage::ExecRequest {
// Server-side wait must be at least as long as the client timeout + buffer
let server_timeout = body.timeout_ms
.map(|ms| std::time::Duration::from_millis(ms + 5_000))
.unwrap_or(REQUEST_TIMEOUT);
match dispatch_with_timeout(&state, &session_id, "exec", |rid| ServerMessage::ExecRequest {
request_id: rid,
command: body.command.clone(),
})
timeout_ms: body.timeout_ms,
}, server_timeout)
.await
{
Ok(ClientMessage::ExecResponse {