Files
javis_bot/app/src/features.rs
javis-bot 39ac20682e feat(app): PC control through agent-desktop, with the emergency stop and the anti-cheat block
app/src/pc.rs runs every pc_action from the worker (admin.spec.md, "PC
control"): one agent-desktop 1.2.0 run per command with a 15 s limit and
its state in the data folder, never --force or raw input, typed text
only through stdin. Each action is decided by permissions::decide for
its asker (not registered, never, ask without consent), then refused
during the emergency stop, while an anti-cheat game runs (Vanguard,
EAC, BattlEye, FACEIT, GameGuard, XIGNCODE3 and the person's list), on
an administrator window and in a password field. Results carry the
window and its elements read again (200 at most, cut to 120 chars),
agent-desktop's error codes become Korean reasons, and each action is
recorded as pc.<action> with the program's file name only.

The emergency stop is a global hotkey (pcControl.stopHotkey, default
Ctrl+Shift+F12) on its own message loop; it kills the running command
and blocks everything but reading until pressed again or lifted on the
PC control screen. features.pcControl (on by default, nothing happens
until someone is registered) and the pinned exe hash decide whether
the brain gets the tool. The installer ships agent-desktop.exe from the
pinned release archive, both hashes checked.

Tested with a fake agent-desktop (342 lib tests pass) and cargo check
for x86_64-pc-windows-gnu; not yet run against real agent-desktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 22:42:48 +09:00

168 lines
7.3 KiB
Rust

//! The brain's features and MCP servers (`app/src/admin.spec.md`, "Features and MCP servers"):
//! what the app writes into the brain's settings, and the MCP servers' environment values, which
//! are secrets and reach the worker only through its environment.
use serde_json::{json, Map, Value};
use crate::secrets::{Secrets, SecretsError};
use crate::settings::{FeatureSettings, McpServerSettings, Settings};
/// The worker's environment variable with the MCP servers' environment values
/// (`voice_worker/brain.py`, `MCP_ENV_VAR`).
pub const MCP_ENV_VAR: &str = "JARVIS_MCP_ENV";
/// The brain's built-in tools behind each feature switch (`jarvis/tools/registry.py`).
pub const FEATURE_TOOLS: &[(&str, &[&str])] = &[
("weather", &["getWeather"]),
("webSearch", &["webSearch", "fetchWebPage"]),
("meals", &["logMeal", "fetchMeals", "deleteMeal"]),
("browser", &["botBrowser"]),
("schedule", &["schedule"]),
("notes", &["personalNotes"]),
("collect", &["collectedNews"]),
];
/// The built-in tools of the features that are off, `screenView` unless screen viewing is
/// available (admin.spec.md, "Screen viewing") and `pcControl` unless PC control is (`pc`: checked
/// with agent-desktop's hash as the services start, admin.spec.md, "PC control").
pub fn disabled_tools(s: &Settings, pc: bool) -> Vec<&'static str> {
let mut off = feature_tools_off(&s.features);
if !crate::screen::available(s) {
off.push("screenView");
}
if !pc {
off.push("pcControl");
}
off
}
fn feature_tools_off(f: &FeatureSettings) -> Vec<&'static str> {
let on = |key: &str| match key {
"weather" => f.weather,
"webSearch" => f.web_search,
"browser" => f.browser,
"schedule" => f.schedule,
"notes" => f.notes,
"collect" => f.collect,
_ => f.meals,
};
FEATURE_TOOLS.iter().filter(|(k, _)| !on(k)).flat_map(|(_, tools)| tools.iter().copied()).collect()
}
/// The brain's `mcps`: the enabled servers, without their environment values.
pub fn brain_mcps(servers: &[McpServerSettings]) -> Value {
Value::Object(servers.iter().filter(|s| s.enabled)
.map(|s| (s.name.clone(), json!({"transport": "stdio", "command": s.command, "args": s.args})))
.collect())
}
/// Where one environment value of a server is kept in the secrets.
pub fn secret_name(server: &str, var: &str) -> String {
format!("mcp:{server}:{var}")
}
/// `{server: {NAME: value}}` for the enabled servers, with the values that are stored.
pub fn mcp_env(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<Value, SecretsError> {
let mut out = Map::new();
for s in servers.iter().filter(|s| s.enabled) {
let mut vars = Map::new();
for var in &s.env {
if let Some(v) = secrets.get(&secret_name(&s.name, var))? {
vars.insert(var.clone(), Value::String(v));
}
}
if !vars.is_empty() {
out.insert(s.name.clone(), Value::Object(vars));
}
}
Ok(Value::Object(out))
}
/// Which environment values are stored: `{server: {NAME: true|false}}` (never the values).
pub fn env_status(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<Value, SecretsError> {
let names = secrets.names()?;
Ok(Value::Object(servers.iter().map(|s| {
let vars = s.env.iter().map(|v| (v.clone(), Value::Bool(names.contains(&secret_name(&s.name, v))))).collect();
(s.name.clone(), Value::Object(vars))
}).collect()))
}
/// Remove the stored values of servers and names no longer listed.
pub fn prune(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<(), SecretsError> {
let keep: Vec<String> = servers.iter().flat_map(|s| s.env.iter().map(|v| secret_name(&s.name, v))).collect();
for name in secrets.names()? {
if name.starts_with("mcp:") && !keep.contains(&name) {
secrets.remove(&name)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::datadir::temp_dir;
use crate::settings::parse_settings;
fn servers(v: Value) -> Vec<McpServerSettings> {
parse_settings(&json!({"mcpServers": v})).unwrap().mcp_servers
}
#[test]
fn the_feature_tools_are_the_brains_tool_names() {
let registry = std::fs::read_to_string(
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../src/jarvis/tools/registry.py")).unwrap();
for (_, tools) in FEATURE_TOOLS {
for t in *tools {
assert!(registry.contains(&format!("\"{t}\":")), "{t} is not a built-in tool");
}
}
}
#[test]
fn features_that_are_off_turn_their_tools_off() {
let mut s = Settings::default();
s.pc_control.allowed_users = vec![1];
let screen = if cfg!(windows) { vec![] } else { vec!["screenView"] };
assert_eq!(disabled_tools(&s, true), screen, "all on by default");
s.features = FeatureSettings { weather: false, web_search: true, meals: false, collect: false, browser: false, browser_share: false, schedule: false, notes: false, screen: true, pc_control: true };
let mut want = vec!["getWeather", "logMeal", "fetchMeals", "deleteMeal", "botBrowser", "schedule", "personalNotes", "collectedNews"];
want.extend(&screen);
assert_eq!(disabled_tools(&s, true), want);
// Screen viewing needs someone registered for PC control (and Windows).
assert!(disabled_tools(&Settings::default(), true).contains(&"screenView"));
s.features.screen = false;
assert!(disabled_tools(&s, true).contains(&"screenView"));
// PC control is offered only when it is available (`pc::available` and agent-desktop).
assert!(disabled_tools(&s, false).contains(&"pcControl"));
assert!(!disabled_tools(&s, true).contains(&"pcControl"));
}
#[test]
fn only_enabled_servers_reach_the_brain_and_their_values_only_through_the_environment() {
let s = servers(json!([
{"name": "brave", "command": "npx", "args": ["-y", "brave"], "env": ["BRAVE_API_KEY", "UNSET"]},
{"name": "off", "command": "uvx", "env": ["K"], "enabled": false}]));
assert_eq!(brain_mcps(&s), json!({"brave": {"transport": "stdio", "command": "npx", "args": ["-y", "brave"]}}));
let secrets = Secrets::new(&temp_dir("mcpenv").join("secrets.dat"));
secrets.set(&secret_name("brave", "BRAVE_API_KEY"), "key-1").unwrap();
secrets.set(&secret_name("off", "K"), "key-2").unwrap();
assert_eq!(mcp_env(&secrets, &s).unwrap(), json!({"brave": {"BRAVE_API_KEY": "key-1"}}));
assert_eq!(env_status(&secrets, &s).unwrap(),
json!({"brave": {"BRAVE_API_KEY": true, "UNSET": false}, "off": {"K": true}}));
}
#[test]
fn values_of_removed_servers_and_names_are_deleted() {
let secrets = Secrets::new(&temp_dir("mcpprune").join("secrets.dat"));
secrets.set(crate::secrets::DISCORD_TOKEN, "tok").unwrap();
for (srv, var) in [("a", "K1"), ("a", "K2"), ("b", "K")] {
secrets.set(&secret_name(srv, var), "v").unwrap();
}
prune(&secrets, &servers(json!([{"name": "a", "command": "x", "env": ["K1"]}]))).unwrap();
let mut left = secrets.names().unwrap();
left.sort();
assert_eq!(left, vec![crate::secrets::DISCORD_TOKEN.to_string(), secret_name("a", "K1")]);
}
}