rho-coding-agent 3.5.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! HTTP/1.1 + WebSocket server for `rho daemon serve --http`.

use std::{
    convert::Infallible, future::Future, net::SocketAddr, pin::Pin, sync::Arc, time::Duration,
};

use anyhow::Context;
use bytes::Bytes;
use http_body_util::{combinators::BoxBody, BodyExt, Full};
use hyper::{body::Incoming, header, Method, Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use rho_protocol::ClientEnvironment;
use serde_json::{Map, Value};
use tokio::{
    io::{AsyncRead, AsyncWrite},
    net::TcpListener,
};
use tokio_tungstenite::{
    tungstenite::{handshake::derive_accept_key, protocol::Role},
    WebSocketStream,
};

use super::{
    bridge::{relay_websocket, websocket_config},
    tokens::DaemonTokenSet,
};
use crate::app::{
    http_guard::{
        bearer_token, check_host, check_origin, is_private, own_hosts, AllowedHost, AllowedOrigin,
        Refusal, LOOPBACK_HOSTS,
    },
    http_upgrade::{adopted_listener, Drain, Upgrade},
    mcp_server::token_store,
};

/// WebSocket endpoint path served by `rho daemon serve --http`.
pub(super) const ENDPOINT: &str = "/ws";

/// The longest the server waits, on a stop signal or an in-place upgrade,
/// for each WebSocket to receive the daemon's answers to requests it already
/// sent and its closing frame.
//
// A tripwire for a stalled client or daemon: daemon requests answer once
// started (a prompt or a login answers, then streams), and a closing daemon
// connection gives each write `rho_protocol::CLOSE_GRACE` (1 s).
const DRAIN_TIMEOUT: Duration = Duration::from_secs(5);

/// Options for `rho daemon serve --http`.
pub(crate) struct HttpOptions {
    pub(crate) address: SocketAddr,
    /// `Host` names to accept besides loopback and the bound private IP.
    pub(crate) allowed_hosts: Vec<String>,
    /// Browser `Origin` values to accept; requests with any other `Origin`
    /// are refused, and requests without one pass.
    pub(crate) allowed_origins: Vec<String>,
    /// The listening socket a re-executing server handed this one
    /// (`http_upgrade`), on `address`, to serve on in place of binding it.
    pub(crate) inherited_listener: Option<std::net::TcpListener>,
    /// How the server upgrades in place once its executable is replaced;
    /// without one it keeps its binary.
    pub(crate) upgrade: Option<Upgrade>,
}

type Body = BoxBody<Bytes, Infallible>;
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

/// How the HTTP server opens a fresh raw stream to the local daemon for each
/// authenticated WebSocket connection.
pub(super) trait DaemonConnector: Send + Sync + 'static {
    type Stream: AsyncRead + AsyncWrite + Unpin + Send + 'static;

    fn connect(&self) -> BoxFuture<anyhow::Result<Self::Stream>>;
}

/// How the HTTP server loads the active bearer token set on each upgrade.
pub(super) trait TokenLoader: Send + Sync + 'static {
    fn load(&self) -> BoxFuture<anyhow::Result<DaemonTokenSet>>;
}

/// Production loader: reads the credential store fresh on every request so
/// rotations and revocations apply immediately.
struct StoreTokenLoader;

impl TokenLoader for StoreTokenLoader {
    fn load(&self) -> BoxFuture<anyhow::Result<DaemonTokenSet>> {
        Box::pin(token_store::load_fresh::<DaemonTokenSet>())
    }
}

/// Production connector: connects raw to the local daemon, starting it first
/// if it is not yet running.
struct LocalDaemonConnector;

impl DaemonConnector for LocalDaemonConnector {
    type Stream = crate::daemon::RawStream;

    fn connect(&self) -> BoxFuture<anyhow::Result<Self::Stream>> {
        Box::pin(crate::app::daemon_cli::connect_raw_or_start())
    }
}

/// What every connection the server accepts shares.
pub(super) struct ServerState<C, T> {
    allowed_hosts: Vec<AllowedHost>,
    allowed_origins: Vec<AllowedOrigin>,
    /// The server's environment, the base of a remote client's.
    host_env: Map<String, Value>,
    connector: C,
    tokens: T,
    /// Counts each open WebSocket as a call under way, and tells each to
    /// close once the server stops or upgrades.
    pub(super) drain: Drain,
}

impl<C: DaemonConnector, T: TokenLoader> ServerState<C, T> {
    pub(super) fn new(
        allowed_hosts: Vec<AllowedHost>,
        allowed_origins: Vec<AllowedOrigin>,
        environment: ClientEnvironment,
        connector: C,
        tokens: T,
    ) -> Arc<Self> {
        Arc::new(Self {
            allowed_hosts,
            allowed_origins,
            host_env: environment
                .into_iter()
                .map(|(key, value)| (key, Value::String(value)))
                .collect(),
            connector,
            tokens,
            drain: Drain::new(),
        })
    }
}

/// Binds `options.address` (or adopts `options.inherited_listener`) and serves
/// `rho-protocol` over WebSocket (`GET /ws`) until `shutdown` resolves or the
/// executable is replaced and upgraded in place.
pub(crate) async fn serve_http(
    options: HttpOptions,
    environment: ClientEnvironment,
    shutdown: impl Future<Output = ()>,
) -> anyhow::Result<()> {
    let ip = options.address.ip();
    if !is_private(ip) {
        anyhow::bail!(
            "rho daemon serve --http binds a loopback address, such as 127.0.0.1:8766, or this machine's address on a private network, such as 192.168.1.20:8766 or a NetBird or Tailscale peer's 100.x.y.z:8766, not {}; reach it from elsewhere through a tunnel",
            options.address
        );
    }
    let own = own_hosts(ip);
    let allowed_hosts = LOOPBACK_HOSTS
        .iter()
        .map(|host| (*host).to_owned())
        .chain(own.iter().cloned())
        .chain(options.allowed_hosts)
        .map(|host| AllowedHost::parse(&host))
        .collect::<anyhow::Result<Vec<_>>>()?;
    let allowed_origins = options
        .allowed_origins
        .iter()
        .map(|origin| AllowedOrigin::parse(origin))
        .collect::<anyhow::Result<Vec<_>>>()?;
    let listener = match options.inherited_listener {
        Some(socket) => adopted_listener(socket, options.address)?,
        None => TcpListener::bind(options.address)
            .await
            .with_context(|| format!("could not listen on {}", options.address))?,
    };
    let local = listener.local_addr()?;
    eprintln!("rho daemon serve listening on ws://{local}{ENDPOINT}");
    if !own.is_empty() {
        eprintln!(
            "rho daemon serve: plain HTTP/WS on a network address: anyone who can read traffic on that network can read requests, bearer tokens included"
        );
    }
    let state = ServerState::new(
        allowed_hosts,
        allowed_origins,
        environment,
        LocalDaemonConnector,
        StoreTokenLoader,
    );
    let Some(upgrade) = options.upgrade else {
        accept_until(&listener, &state, shutdown).await?;
        stop(&state.drain).await;
        return Ok(());
    };
    let replaced = accept_until(&listener, &state, async {
        tokio::select! {
            () = shutdown => None,
            version = upgrade.replaced() => Some(version),
        }
    })
    .await?;
    match replaced {
        Some(version) => Err(state
            .drain
            .finish(upgrade, &version, listener, DRAIN_TIMEOUT)
            .await),
        None => {
            stop(&state.drain).await;
            Ok(())
        }
    }
}

/// Closes every open WebSocket once the daemon has answered what it sent,
/// waiting at most [`DRAIN_TIMEOUT`].
async fn stop(drain: &Drain) {
    let left = drain.settle(DRAIN_TIMEOUT).await;
    if left > 0 {
        eprintln!(
            "rho daemon serve: {left} WebSockets still open after DRAIN_TIMEOUT ({DRAIN_TIMEOUT:?}); closing them"
        );
    }
}

/// Serves each connection `listener` accepts until `until` resolves, and
/// returns its output. Connections close once `state.drain` begins.
pub(super) async fn accept_until<C, T, U>(
    listener: &TcpListener,
    state: &Arc<ServerState<C, T>>,
    until: impl Future<Output = U>,
) -> std::io::Result<U>
where
    C: DaemonConnector,
    T: TokenLoader,
{
    tokio::pin!(until);
    loop {
        let stream = tokio::select! {
            ended = &mut until => return Ok(ended),
            accepted = listener.accept() => match accepted {
                Ok((stream, _)) => stream,
                Err(error) if is_transient_accept_error(&error) => {
                    tracing::debug!(%error, "daemon HTTP accept failed");
                    continue;
                }
                Err(error) => return Err(error),
            }
        };
        let state = Arc::clone(state);
        let began = state.drain.began();
        tokio::spawn(async move {
            let handler = hyper::service::service_fn(move |request| {
                let state = Arc::clone(&state);
                async move { Ok::<_, Infallible>(handle(state, request).await) }
            });
            let connection = hyper::server::conn::http1::Builder::new()
                .serve_connection(TokioIo::new(stream), handler)
                .with_upgrades();
            tokio::pin!(connection);
            let ended = tokio::select! {
                ended = connection.as_mut() => ended,
                () = began => {
                    connection.as_mut().graceful_shutdown();
                    connection.await
                }
            };
            if let Err(error) = ended {
                tracing::debug!(%error, "daemon HTTP connection ended");
            }
        });
    }
}

async fn handle<C: DaemonConnector, T: TokenLoader>(
    state: Arc<ServerState<C, T>>,
    request: Request<Incoming>,
) -> Response<Body> {
    if request.uri().path() != ENDPOINT {
        return refusal(StatusCode::NOT_FOUND, "not found");
    }
    if let Err(Refusal { status, reason }) = check_host(&request, &state.allowed_hosts)
        .and_then(|()| check_origin(&request, &state.allowed_origins))
    {
        return refusal(status, reason);
    }
    let Some(presented) = bearer_token(&request) else {
        return unauthorized("missing bearer token");
    };
    let tokens = match state.tokens.load().await {
        Ok(tokens) => tokens,
        Err(error) => {
            eprintln!("rho daemon serve: could not read the daemon HTTP tokens: {error:#}");
            return refusal(
                StatusCode::INTERNAL_SERVER_ERROR,
                "could not read the server's tokens",
            );
        }
    };
    let Some(token) = tokens.authenticate(&presented) else {
        return unauthorized("invalid bearer token");
    };
    if request.method() != Method::GET {
        let mut response = refusal(StatusCode::METHOD_NOT_ALLOWED, "method not allowed");
        response
            .headers_mut()
            .insert(header::ALLOW, header::HeaderValue::from_static("GET"));
        return response;
    }
    let Some(ws_key) = validate_websocket_upgrade(&request) else {
        let mut response = refusal(
            StatusCode::UPGRADE_REQUIRED,
            "WebSocket upgrade required on /ws",
        );
        response.headers_mut().insert(
            header::UPGRADE,
            header::HeaderValue::from_static("websocket"),
        );
        response.headers_mut().insert(
            header::SEC_WEBSOCKET_VERSION,
            header::HeaderValue::from_static("13"),
        );
        return response;
    };
    let daemon_stream = match state.connector.connect().await {
        Ok(stream) => stream,
        Err(error) => {
            eprintln!("rho daemon serve: could not reach the rho daemon: {error:#}");
            return refusal(
                StatusCode::SERVICE_UNAVAILABLE,
                &format!("could not reach the rho daemon: {error:#}"),
            );
        }
    };

    let accept = derive_accept_key(ws_key.as_bytes());
    let token_name = token.name.clone();
    let upgrade = hyper::upgrade::on(request);
    // Counted from the response on, so a drain that begins meanwhile waits
    // for this WebSocket too.
    let call = state.drain.calls().enter();
    let draining = state.drain.began();
    tokio::spawn(async move {
        let _call = call;
        match upgrade.await {
            Ok(upgraded) => {
                tracing::debug!(token = %token_name, "accepted daemon WebSocket client");
                let ws = WebSocketStream::from_raw_socket(
                    TokioIo::new(upgraded),
                    Role::Server,
                    Some(websocket_config()),
                )
                .await;
                relay_websocket(ws, daemon_stream, &state.host_env, draining).await;
                tracing::debug!(token = %token_name, "daemon WebSocket client disconnected");
            }
            Err(error) => {
                tracing::debug!(token = %token_name, %error, "WebSocket upgrade failed");
            }
        }
    });

    let mut response = Response::new(Full::new(Bytes::new()).boxed());
    *response.status_mut() = StatusCode::SWITCHING_PROTOCOLS;
    let headers = response.headers_mut();
    headers.insert(
        header::UPGRADE,
        header::HeaderValue::from_static("websocket"),
    );
    headers.insert(
        header::CONNECTION,
        header::HeaderValue::from_static("Upgrade"),
    );
    headers.insert(
        header::SEC_WEBSOCKET_ACCEPT,
        header::HeaderValue::from_str(&accept).expect("a base64 accept key is a valid header"),
    );
    response
}

/// Validates RFC 6455 client upgrade headers and returns `Sec-WebSocket-Key`.
fn validate_websocket_upgrade<B>(request: &Request<B>) -> Option<String> {
    let has_upgrade_ws = request
        .headers()
        .get_all(header::UPGRADE)
        .iter()
        .filter_map(|value| value.to_str().ok())
        .flat_map(|value| value.split(','))
        .any(|token| token.trim().eq_ignore_ascii_case("websocket"));
    let has_connection_upgrade = request
        .headers()
        .get_all(header::CONNECTION)
        .iter()
        .filter_map(|value| value.to_str().ok())
        .flat_map(|value| value.split(','))
        .any(|token| token.trim().eq_ignore_ascii_case("upgrade"));
    let version_ok = request
        .headers()
        .get(header::SEC_WEBSOCKET_VERSION)
        .and_then(|value| value.to_str().ok())
        .is_some_and(|version| version.trim() == "13");
    let key = request
        .headers()
        .get(header::SEC_WEBSOCKET_KEY)
        .and_then(|value| value.to_str().ok())
        .map(str::trim)
        .filter(|key| !key.is_empty())?;
    (has_upgrade_ws && has_connection_upgrade && version_ok).then(|| key.to_owned())
}

fn unauthorized(message: &str) -> Response<Body> {
    let mut response = refusal(StatusCode::UNAUTHORIZED, message);
    response.headers_mut().insert(
        header::WWW_AUTHENTICATE,
        header::HeaderValue::from_static("Bearer"),
    );
    response
}

fn refusal(status: StatusCode, message: &str) -> Response<Body> {
    let mut response = Response::new(Full::new(Bytes::from(message.to_owned())).boxed());
    *response.status_mut() = status;
    response
}

fn is_transient_accept_error(error: &std::io::Error) -> bool {
    matches!(
        error.kind(),
        std::io::ErrorKind::ConnectionAborted
            | std::io::ErrorKind::ConnectionReset
            | std::io::ErrorKind::Interrupted
            | std::io::ErrorKind::WouldBlock
    )
}

#[cfg(test)]
#[path = "server_tests.rs"]
mod tests;