Skip to main content

mj_controller/
server_runtime.rs

1//! The phone-oriented remote-control server: its HTTP surface, the controller
2//! actions phones request, and the concurrency limits that keep them safe.
3
4use std::net::{Ipv4Addr, SocketAddr};
5use std::path::PathBuf;
6use std::sync::Arc;
7use std::sync::atomic::{AtomicBool, Ordering};
8use std::time::{Duration, Instant};
9
10use anyhow::{Context, Result, bail};
11pub(crate) mod api;
12mod api_activity;
13pub(crate) mod profile_catalog;
14mod publication;
15
16use mj_core::config::{Config, HarnessProfile, PhoneConfig, is_bare_project_target};
17use mj_core::refusal::Refusal;
18use mj_core::remote_git::display_url;
19use mj_core::state::{MaterializedSession, ProjectSourceIdentity, SessionRecord, State};
20
21use crate::controller::Controller;
22use crate::quota::ProfileQuota;
23use crate::server::{
24    ActionOutcome, BackgroundTaskStopFailure, BackgroundTaskStopRequest, BrowserTranscript,
25    ControllerAction, ControllerRequest, MovePreparationRequest, PreflightFailure,
26    ReadReceiptRequest, ResumeQueueDisposition, ServerOptions, ViewerActivityDetails,
27    ViewerActivityKind, ViewerBackgroundTask, ViewerMoveRecovery, ViewerQueuedPrompt, ViewerQuota,
28    ViewerSnapshot, ViewerUserShell,
29};
30use crate::session_manager::{SessionManagerChannels, SessionManagerControl, new_command_id};
31use crate::tailscale::TailscaleTls;
32#[cfg(test)]
33use crate::targets::ProcessExecutor;
34use crate::targets::{CancellableProcessExecutor, CommandExecutor};
35use mj_core::relay::RelayCommand;
36use mj_core::workspace::WorkspaceRecord;
37
38use crate::controller::config_only_controller;
39use crate::daemon::{
40    CreateSessionControl, CreateSessionRequest, ResumeSessionRequest, RuntimeState,
41};
42use crate::pollers::{
43    QUOTA_STALE_AFTER, QuotaRefreshBatch, apply_worker_record_update, projected_queued_prompts,
44    queued_prompt_projection, quota_refresh_profiles,
45};
46
47#[derive(Debug, Clone)]
48pub struct ServerArgs {
49    bind: String,
50    tailscale_detect: bool,
51    tls_cert: Option<PathBuf>,
52    tls_key: Option<PathBuf>,
53}
54
55impl From<&PhoneConfig> for ServerArgs {
56    fn from(config: &PhoneConfig) -> Self {
57        Self {
58            bind: config.bind.clone(),
59            tailscale_detect: config.tailscale_detect,
60            tls_cert: config.tls_cert.clone(),
61            tls_key: config.tls_key.clone(),
62        }
63    }
64}
65
66const TAILSCALE_COMMAND_TIMEOUT: Duration = Duration::from_secs(120);
67const TAILSCALE_RENEW_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
68
69mod projection;
70use projection::*;
71mod args;
72use args::*;
73mod phone_actions;
74use phone_actions::*;
75mod run;
76pub(crate) use run::*;
77mod support;
78use support::*;
79mod preflight;
80use preflight::*;
81mod actions;
82pub(crate) use actions::republish_quota_profiles;
83use actions::*;
84mod project_sources;
85use project_sources::*;
86mod snapshot;
87use snapshot::*;
88
89#[cfg(test)]
90mod tests;