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
//! CLI arguments for `rho daemon`.
use std::{net::SocketAddr, time::Duration};
use clap::Subcommand;
use crate::app::automation_protocol::parse_duration;
/// The shortest `rho daemon run --unload-idle-after`. Shorter, a session
/// could unload between one client's consecutive requests, as a reopen
/// takes up to 41 ms measured (`UNLOAD_IDLE_AFTER` has the receipt), and
/// the sweep, which runs once per period, would spin.
const MIN_UNLOAD_IDLE_AFTER: Duration = Duration::from_millis(100);
fn parse_unload_idle_after(value: &str) -> Result<Duration, String> {
let duration = parse_duration(value)?;
if duration < MIN_UNLOAD_IDLE_AFTER {
return Err(format!(
"invalid duration '{value}': must be at least {}",
humantime::format_duration(MIN_UNLOAD_IDLE_AFTER)
));
}
Ok(duration)
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonCommand {
/// Run the daemon in the foreground until stopped. The default.
Run {
/// Unload a session once it has sat idle this long: no turn under
/// way and no client attached. Unloading closes its MCP servers and
/// lets other rho processes open it; the next prompt or attach
/// reopens it. A session that saved nothing is discarded instead.
/// Defaults to 60s; at least 100ms.
#[arg(long, value_name = "DURATION", value_parser = parse_unload_idle_after)]
unload_idle_after: Option<Duration>,
/// The lock and socket descriptors a daemon that re-executes itself
/// in place hands the new binary. Only the daemon passes it.
#[arg(long, value_name = "LOCK:SOCKET", hide = true)]
exec_handoff: Option<String>,
},
/// Start the daemon in the background unless it is already running,
/// and wait until it answers.
///
/// Exits 0 once a daemon runs, including one that already did. Waits
/// for a daemon that is stopping to exit before starting a new one.
Start,
/// Show the running daemon's pid, version, uptime, socket, and any
/// upgrade it waits to run.
///
/// Never starts a daemon. Exits with status 1 when none is running.
Status {
/// Print one JSON document instead of text.
#[arg(long)]
json: bool,
},
/// Stop the running daemon and wait for it to exit.
///
/// Exits with status 1 when none is running.
Stop,
/// Restart the running daemon in place and wait until it answers again.
///
/// The daemon keeps its pid, socket, and lock, so clients reconnect to
/// it rather than start another, and re-executes its executable, which
/// runs an installed upgrade at once. Turns under way are interrupted;
/// a TUI session's turn with no time limit continues once the TUI
/// attaches to its session again. Starts the daemon when none is
/// running.
Restart,
/// Expose the local daemon's `rho-protocol` over HTTP WebSockets (`GET /ws`)
/// so clients on hosts that cannot use SSH can reach it.
///
/// Binds a loopback or private/overlay network address; every upgrade
/// requires a bearer token from `rho daemon token rotate`.
Serve(DaemonServeArgs),
/// Manage the bearer tokens `rho daemon serve --http` accepts.
Token {
#[command(subcommand)]
command: DaemonTokenCommand,
},
}
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct DaemonServeArgs {
/// Serve `rho-protocol` over HTTP WebSocket (`GET /ws`) on this address:
/// a loopback address, such as 127.0.0.1:8766, or this machine's address
/// on a private network, such as a NetBird or Tailscale peer's
/// 100.x.y.z:8766, where traffic travels as plain HTTP/WS.
#[arg(long, value_name = "ADDR")]
pub http: SocketAddr,
/// Also accept this `Host` header, such as a tunnel's public hostname,
/// on any port unless it names one (rho.example.com:443). May be
/// repeated.
#[arg(long, value_name = "HOST")]
pub allow_host: Vec<String>,
/// Accept WebSocket upgrades from this browser origin, such as
/// https://app.example.com. Any other `Origin` is refused. Browsers
/// cannot send the bearer token themselves, so they need a proxy that
/// adds `Authorization`. May be repeated.
#[arg(long, value_name = "ORIGIN")]
pub allow_origin: Vec<String>,
/// Internal: the listening socket a server that upgraded in place
/// handed this one.
#[arg(long, value_name = "FD", hide = true)]
pub listener_fd: Option<i32>,
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonTokenCommand {
/// Create token NAME, or give it a new secret, and print the secret once.
/// The old secret stops working at once.
///
/// A daemon HTTP token grants full user-level daemon protocol access
/// (opening sessions in any workspace, answering approvals, and editing
/// config). Treat it like SSH access.
Rotate {
/// The token's name, which `list` and logs show.
#[arg(value_name = "NAME")]
name: String,
},
/// List daemon HTTP tokens by name.
List {
/// Print one JSON document instead of text rows.
#[arg(long)]
json: bool,
},
/// Delete token NAME; new connections presenting it are refused at once.
/// Open WebSockets stay connected until they close or the bridge
/// restarts.
Revoke {
#[arg(value_name = "NAME")]
name: String,
},
}