Skip to main content

mj_controller/
server.rs

1//! Daemon-owned, phone-oriented control surface for Hel.
2//!
3//! The server deliberately owns no controller business logic. It publishes a
4//! redacted projection of controller state and forwards validated, typed
5//! actions through a channel supplied by the controller.
6
7use std::collections::BTreeMap;
8use std::convert::Infallible;
9use std::net::SocketAddr;
10use std::path::{Component, PathBuf};
11use std::sync::{Arc, Mutex};
12use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
13
14use anyhow::{Context, Result as AnyResult};
15use axum::body::{Body, Bytes, to_bytes};
16use axum::extract::{DefaultBodyLimit, Path, Query, Request, State};
17use axum::http::header::{
18    CACHE_CONTROL, CONTENT_SECURITY_POLICY as CONTENT_SECURITY_POLICY_HEADER, CONTENT_TYPE, COOKIE,
19    HeaderValue, LOCATION, REFERRER_POLICY, SET_COOKIE, X_CONTENT_TYPE_OPTIONS,
20};
21use axum::http::{HeaderMap, Response, StatusCode};
22use axum::middleware::Next;
23use axum::response::IntoResponse;
24use axum::response::sse::{Event, KeepAlive, Sse};
25use axum::routing::{get, post, put};
26use axum::{Json, Router};
27use base64::Engine as _;
28use hmac::{Hmac, KeyInit, Mac};
29use serde::{Deserialize, Serialize};
30use sha2::Sha256;
31use tokio::sync::Semaphore;
32use tokio::sync::{mpsc, watch};
33use tokio_stream::wrappers::ReceiverStream;
34use tokio_util::sync::CancellationToken;
35
36use mj_core::attachment::{AttachmentRef, AttachmentStore, MAX_IMAGE_BYTES, MAX_IMAGES};
37use mj_core::config::{Config, TargetTemplate, project_history_host, validate_id};
38use mj_core::elicitation::{ElicitationRequest, ElicitationResponse, MAX_ELICITATION_BYTES};
39use mj_core::path_completion::{CompletionHost, CompletionKind, PathCompletion};
40use mj_core::refusal::{Refusal, RefusalKind};
41use mj_core::state::{
42    MoveOperation, MovePhase, MovePreparation, MoveSelection, MoveSessionRequest,
43    ProjectSourceIdentity, SessionResourceAllocation, SessionState, SessionTransitionKind,
44    State as AppState,
45};
46
47use crate::targets::AdditionalMount;
48
49use crate::dictation::{
50    DictationError, DictationOperation, DictationRequest, DictationResponse, MAX_AUDIO_BYTES,
51    validate_wav,
52};
53use crate::image::optimize_image;
54
55pub mod api;
56
57pub use api::{
58    ApiFailure, ApiSession, PromptRequest, PromptResponse, SessionListResponse,
59    StartSessionRequest, StartSessionResponse, SubagentBackend, WaitOutcome, WaitRequest,
60    WaitResponse, api_token_path, load_or_create_api_token, map_stop_reason, resolve_wait,
61};
62
63pub use mj_client::web::{
64    BrowserDiffStat, BrowserTranscript, BrowserTranscriptEntry, WebListenerProcess,
65    WebViewerAccess, WebViewerRecovery,
66};
67
68// Keep all control surfaces on the same queue vocabulary. The resume flow
69// used to define a private copy here, which made a move request impossible to
70// pass through the web and daemon boundaries without lossy conversion.
71pub use mj_core::state::ResumeQueueDisposition;
72
73/// Select the process-wide rustls provider before any TLS configuration is built.
74///
75/// Dependency feature unification can enable both rustls providers. Rustls
76/// deliberately refuses to guess in that case, so each executable that links
77/// the controller installs the ring provider at process startup. A provider
78/// installed even earlier is already sufficient and remains in place.
79pub fn install_rustls_crypto_provider() {
80    let _ = rustls::crypto::ring::default_provider().install_default();
81}
82
83pub const COOKIE_NAME: &str = "hel_viewer_session";
84const DEFAULT_SESSION_TTL: Duration = Duration::from_secs(30 * 24 * 60 * 60);
85const EPHEMERAL_SESSION_TTL: Duration = Duration::from_secs(24 * 60 * 60);
86const MAX_BODY_BYTES: usize = 128 * 1024;
87const MAX_CODE_FAILURES: u32 = 5;
88const CODE_LOCKOUT_BASE: Duration = Duration::from_secs(30);
89const CODE_LOCKOUT_CAP: Duration = Duration::from_secs(60 * 60);
90const MAX_TITLE_CHARS: usize = 120;
91const MAX_PROMPT_CHARS: usize = 64 * 1024;
92/// How many repositories one dirty-worktree acknowledgement may name. A bundle
93/// with more repositories than this than has bigger problems than the phone.
94const MAX_DIRTY_ACKNOWLEDGEMENTS: usize = 32;
95/// The largest draft a phone may store. A composer is for a prompt, and a
96/// prompt this size has other problems; the bound exists so one viewer cannot
97/// fill the daemon's database with text it never sent.
98const MAX_DRAFT_BYTES: usize = 64 * 1024;
99/// How many prompt-history matches one search returns. Public because the
100/// controller loop performs the search and must use the same bound the phone
101/// was promised.
102pub const MAX_HISTORY_MATCHES: usize = 40;
103/// Image prompts need far more room than any other phone request. Browser
104/// uploads are base64-encoded, so two ordinary photographs already exceed the
105/// general body limit even when each one fits it. The larger bound therefore
106/// stays scoped to the action route that carries prompts.
107const MAX_PROMPT_BODY_BYTES: usize = 32 * 1024 * 1024;
108/// A browser uploads one source image at a time. The image optimizer has its
109/// own decoded-allocation bound; this is the HTTP envelope bound before that
110/// work starts.
111const MAX_ATTACHMENT_UPLOAD_BYTES: usize = 64 * 1024 * 1024;
112/// Keep two browser uploads/transcriptions in flight. The permit is acquired
113/// before reading the request body, so an overloaded client is rejected
114/// without accepting megabytes that cannot be processed yet.
115const MAX_CONCURRENT_DICTATIONS: usize = 2;
116/// Keep this in sync with the prompt admission bound and the browser composer.
117pub const MAX_PROMPT_IMAGES: usize = MAX_IMAGES;
118const COOKIE_KEY_BYTES: usize = 32;
119const COOKIE_KEY_FILE: &str = "phone-cookie-key";
120
121/// How long stored viewer state outlives its last use.
122///
123/// It matches the session cookie's own lifetime: state keyed to an identity
124/// that can no longer authenticate has nothing left to belong to.
125pub const fn default_session_ttl() -> Duration {
126    DEFAULT_SESSION_TTL
127}
128
129pub fn cookie_key_path() -> PathBuf {
130    mj_core::config::data_dir().join(COOKIE_KEY_FILE)
131}
132
133/// Load the phone cookie signing key, creating it on first use.
134///
135/// This signing key keeps a signed-in phone signed in across daemon restarts.
136/// Logged-out identities are tracked separately. Deleting the key is
137/// therefore the explicit sign-everyone-out gesture: the next start writes a
138/// new key and every outstanding cookie stops validating. A missing file is
139/// ordinary first use; an unreadable or too-short one is replaced loudly,
140/// because refusing to start would be a worse answer than asking phones to
141/// enter the viewer code again.
142pub fn load_or_create_cookie_key(path: &std::path::Path) -> AnyResult<Vec<u8>> {
143    match std::fs::read(path) {
144        Ok(key) if key.len() >= COOKIE_KEY_BYTES => return Ok(key),
145        Ok(key) => tracing::warn!(
146            path = %path.display(),
147            bytes = key.len(),
148            "phone cookie key is shorter than {COOKIE_KEY_BYTES} bytes; generating a new key signs every phone out"
149        ),
150        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
151        Err(error) => tracing::warn!(
152            path = %path.display(),
153            "could not read the phone cookie key ({error}); generating a new key signs every phone out"
154        ),
155    }
156    let key = generate_cookie_key()?;
157    mj_core::config::atomic_write(path, &key)
158        .with_context(|| format!("persist Mjolnir phone cookie key {}", path.display()))?;
159    Ok(key.to_vec())
160}
161
162/// Options for the daemon's phone service.
163///
164/// `ServerOptions::new` generates both the six-digit viewer code and an
165/// ephemeral cookie key. A caller that wants cookies to survive server
166/// restarts loads its key and logout records with `load_cookie_credentials`.
167/// `set_cookie_key` installs a key for callers managing storage themselves. The
168/// key and viewer code are intentionally omitted from `Debug` output.
169#[derive(Clone)]
170pub struct ServerOptions {
171    pub bind: SocketAddr,
172    pub snapshot_rx: watch::Receiver<ViewerSnapshot>,
173    pub conversation_rx: watch::Receiver<BTreeMap<String, BrowserTranscript>>,
174    pub action_tx: mpsc::Sender<ControllerRequest>,
175    pub bundle_tx: mpsc::Sender<BundleRequest>,
176    pub receipt_tx: mpsc::Sender<ReadReceiptRequest>,
177    pub preflight_tx: mpsc::Sender<PreflightRequest>,
178    pub move_preparation_tx: mpsc::Sender<MovePreparationRequest>,
179    pub client_state_tx: mpsc::Sender<ClientStateRequest>,
180    pub dictation_tx: mpsc::Sender<DictationRequest>,
181    /// Dedicated bounded path for stopping one live background task. This is
182    /// deliberately separate from [`ControllerAction`]: stopping a provider
183    /// task does not occupy the controller's action admission slot.
184    background_task_stop_tx: mpsc::Sender<BackgroundTaskStopRequest>,
185    pub shutdown: CancellationToken,
186    pub session_ttl: Duration,
187    /// Keep this enabled for direct HTTPS or an HTTPS reverse proxy. It may be
188    /// disabled only for an explicitly trusted HTTP development endpoint.
189    pub secure_cookie: bool,
190    tls_config: Option<axum_server::tls_rustls::RustlsConfig>,
191    viewer_code: String,
192    login_token: String,
193    cookie_key: Vec<u8>,
194    viewer_revocations: Arc<ViewerRevocations>,
195    api_token: String,
196    subagent: Option<Arc<dyn api::SubagentBackend>>,
197    /// Where the remembered fast-start preferences live. Injected rather than
198    /// resolved from the process environment inside a handler, so the
199    /// documented API answers from its own state and tests never read the
200    /// developer's real configuration.
201    preferences_path: PathBuf,
202    /// Checks the engine behind each local container target for the launch
203    /// options route. Tests replace it so the answer does not depend on the
204    /// engines the test host has.
205    engine_checks: Arc<api::LocalEngineChecks>,
206    worker_source_check: Option<WorkerSourceCheck>,
207    /// The daemon's upgrade admission. A closed gate refuses new requests, and
208    /// tells the long requests a shutdown ends that the daemon is being
209    /// replaced, so their clients follow it. Tests give the server its own.
210    upgrade_gate: Arc<crate::upgrade::Gate>,
211}
212
213/// The header, valued `pending`, that marks a 503 as "this daemon is being
214/// replaced by an automatic upgrade; ask the next one". The request was not
215/// accepted, or was a read the shutdown ended, so asking again repeats nothing.
216pub const UPGRADE_HEADER: &str = "x-mj-upgrade";
217
218/// Answers, for a target id, why no worker binary could serve a session there.
219/// It blocks (a bare SSH host is asked its platform), so callers run it off
220/// the async runtime.
221pub type WorkerSourceCheck = Arc<dyn Fn(&str) -> Option<String> + Send + Sync>;
222
223/// Typed request channels served by the authenticated HTTP surface.
224pub struct ServerRequests {
225    pub action_tx: mpsc::Sender<ControllerRequest>,
226    pub bundle_tx: mpsc::Sender<BundleRequest>,
227    pub receipt_tx: mpsc::Sender<ReadReceiptRequest>,
228    pub preflight_tx: mpsc::Sender<PreflightRequest>,
229    pub move_preparation_tx: mpsc::Sender<MovePreparationRequest>,
230    pub client_state_tx: mpsc::Sender<ClientStateRequest>,
231    pub dictation_tx: mpsc::Sender<DictationRequest>,
232}
233
234impl ServerOptions {
235    pub fn new(
236        bind: SocketAddr,
237        snapshot_rx: watch::Receiver<ViewerSnapshot>,
238        conversation_rx: watch::Receiver<BTreeMap<String, BrowserTranscript>>,
239        requests: ServerRequests,
240    ) -> AnyResult<Self> {
241        let cookie_key = generate_cookie_key()?.to_vec();
242        Ok(Self {
243            bind,
244            snapshot_rx,
245            conversation_rx,
246            action_tx: requests.action_tx,
247            bundle_tx: requests.bundle_tx,
248            receipt_tx: requests.receipt_tx,
249            preflight_tx: requests.preflight_tx,
250            move_preparation_tx: requests.move_preparation_tx,
251            client_state_tx: requests.client_state_tx,
252            dictation_tx: requests.dictation_tx,
253            background_task_stop_tx: mpsc::channel(1).0,
254            shutdown: CancellationToken::new(),
255            session_ttl: DEFAULT_SESSION_TTL,
256            secure_cookie: true,
257            tls_config: None,
258            viewer_code: generate_viewer_code()?,
259            login_token: derive_login_token(&cookie_key),
260            cookie_key,
261            viewer_revocations: Arc::new(ViewerRevocations::default()),
262            // An empty token authenticates nothing: the daemon installs the
263            // persisted one, and a server without it serves the viewer only.
264            api_token: String::new(),
265            subagent: None,
266            preferences_path: mj_core::go::GoPreferences::path(),
267            engine_checks: Arc::new(api::LocalEngineChecks::on_this_host()),
268            worker_source_check: None,
269            upgrade_gate: crate::upgrade::gate().clone(),
270        })
271    }
272
273    #[cfg(test)]
274    pub(crate) fn set_upgrade_gate(&mut self, gate: Arc<crate::upgrade::Gate>) {
275        self.upgrade_gate = gate;
276    }
277
278    pub fn viewer_code(&self) -> &str {
279        &self.viewer_code
280    }
281
282    pub fn login_token(&self) -> &str {
283        &self.login_token
284    }
285
286    /// Serve HTTPS directly using the supplied Rustls configuration. Hel's
287    /// CLI can load its persisted certificate (including a Tailscale-issued
288    /// certificate) and pass it here without coupling this module to disk.
289    pub fn set_tls_config(&mut self, config: axum_server::tls_rustls::RustlsConfig) {
290        self.tls_config = Some(config);
291        self.secure_cookie = true;
292    }
293
294    /// Install a persisted signing key. Rotating this value signs every phone
295    /// out and revokes QR login URLs. This does not load durable logout records;
296    /// use `load_cookie_credentials` for the daemon's persisted credentials.
297    pub fn set_cookie_key(&mut self, key: Vec<u8>) -> AnyResult<()> {
298        anyhow::ensure!(
299            key.len() >= COOKIE_KEY_BYTES,
300            "cookie signing key must be at least {COOKIE_KEY_BYTES} bytes"
301        );
302        self.login_token = derive_login_token(&key);
303        self.cookie_key = key;
304        Ok(())
305    }
306
307    /// Load the signing key and durable logout records off the async runtime.
308    pub async fn load_cookie_credentials(&mut self, path: PathBuf) -> AnyResult<()> {
309        let (key, revocations) = tokio::task::spawn_blocking(move || {
310            let key = load_or_create_cookie_key(&path)?;
311            let revocations =
312                ViewerRevocations::load(path.with_file_name("phone-cookie-revocations.json"))?;
313            Ok::<_, anyhow::Error>((key, revocations))
314        })
315        .await
316        .context("load viewer credentials task")??;
317        self.set_cookie_key(key)?;
318        self.viewer_revocations = Arc::new(revocations);
319        Ok(())
320    }
321
322    /// Install the controller's bounded background-task stop path.
323    pub fn set_background_task_stop_tx(&mut self, tx: mpsc::Sender<BackgroundTaskStopRequest>) {
324        self.background_task_stop_tx = tx;
325    }
326
327    /// Install the persisted bearer token for the `/api/v1` routes. Rotating
328    /// it revokes every client that still holds the old one.
329    pub fn set_api_token(&mut self, token: String) {
330        self.api_token = token;
331    }
332
333    /// Install the daemon-side backend the `/api/v1` routes drive sessions
334    /// through. Without it those routes answer 503.
335    pub fn set_subagent_backend(&mut self, backend: Arc<dyn api::SubagentBackend>) {
336        self.subagent = Some(backend);
337    }
338
339    /// Refuse launches, resumes and moves onto a target the check reports as
340    /// having no worker binary. Without it every target is admitted.
341    pub fn set_worker_source_check(&mut self, check: WorkerSourceCheck) {
342        self.worker_source_check = Some(check);
343    }
344
345    /// Read the remembered default from a specific preferences file instead
346    /// of the user's own. Tests point this at a temporary path.
347    pub fn set_preferences_path(&mut self, path: PathBuf) {
348        self.preferences_path = path;
349    }
350
351    /// Answer local engine checks with `probe` instead of this host's engines.
352    #[cfg(test)]
353    fn set_engine_probe(&mut self, probe: api::EngineProbe) {
354        self.engine_checks = Arc::new(api::LocalEngineChecks::new(probe));
355    }
356
357    #[cfg(test)]
358    fn with_test_credentials(mut self, code: &str, key: &[u8]) -> Self {
359        self.viewer_code = code.to_string();
360        self.login_token = "test-login-token".into();
361        self.cookie_key = key.to_vec();
362        self.secure_cookie = false;
363        self.api_token = "test-api-token".into();
364        self
365    }
366}
367
368/// Run the phone server until its shutdown token is cancelled.
369///
370/// This binds only the requested listener. It does not daemonize, provision a
371/// target, or keep sessions alive: controller availability is required, just
372/// like MJ's explicit remote-viewer model.
373pub async fn run_server(options: ServerOptions) -> AnyResult<()> {
374    let listener = tokio::net::TcpListener::bind(options.bind)
375        .await
376        .with_context(|| format!("bind web viewer to {}", options.bind))?;
377    run_server_on_listener(options, listener).await
378}
379
380/// Serve a reserved socket so readiness and advertised ports reflect a real listener.
381pub async fn run_server_on_listener(
382    options: ServerOptions,
383    listener: tokio::net::TcpListener,
384) -> AnyResult<()> {
385    let mut options = options;
386    let bind = listener.local_addr().context("read web viewer address")?;
387    let shutdown = options.shutdown.clone();
388    let viewer_code = options.viewer_code.clone();
389    let tls_config = options.tls_config.take();
390    let app = router(options);
391    println!("Mjolnir viewer code: {viewer_code}");
392    let listener = listener.into_std().context("prepare web viewer listener")?;
393    let handle = axum_server::Handle::new();
394    let shutdown_handle = handle.clone();
395    let serve = async move {
396        if let Some(tls_config) = tls_config {
397            axum_server::from_tcp_rustls(listener, tls_config)
398                .handle(handle)
399                .serve(app.into_make_service())
400                .await
401        } else {
402            axum_server::from_tcp(listener)
403                .handle(handle)
404                .serve(app.into_make_service())
405                .await
406        }
407    };
408    tokio::pin!(serve);
409    tokio::select! {
410        result = &mut serve => result,
411        _ = shutdown.cancelled() => {
412            shutdown_handle.graceful_shutdown(Some(Duration::from_secs(2)));
413            serve.await
414        }
415    }
416    .with_context(|| format!("serve web viewer on {bind}"))
417}
418
419mod viewer_feed;
420mod viewer_types;
421pub use viewer_types::*;
422mod actions;
423pub use actions::*;
424mod routes;
425use routes::*;
426mod handlers;
427use handlers::*;
428mod validation;
429use validation::*;
430mod errors;
431use errors::*;
432mod auth;
433pub use auth::*;
434mod assets;
435use assets::*;
436mod config_view;
437pub use config_view::*;
438
439#[cfg(test)]
440mod tests;