| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| use anyhow::{Context, Result}; |
| use clap::{Parser, Subcommand}; |
| use spf_smart_gate::{ |
| agent_state::AgentStateDb, calculate, config, config::CommandPerm, |
| config_db::SpfConfigDb, fs::SpfFs, |
| gate, mcp, paths, session::Session, storage::SpfStorage, |
| utf8_safe, worker, |
| }; |
| use std::path::PathBuf; |
|
|
| fn default_storage_path() -> PathBuf { |
| paths::spf_root().join("LIVE/SESSION/SESSION.DB") |
| } |
|
|
| #[derive(Parser)] |
| #[command(name = "spf-smart-gate")] |
| #[command(author = "Joseph Stone")] |
| #[command(version = "3.0.0")] |
| #[command(about = "SPF Smart Gateway - MCP command gateway with LMDB-backed configuration")] |
| struct Cli { |
| |
| #[arg(short, long, default_value_os_t = default_storage_path())] |
| storage: PathBuf, |
|
|
| #[command(subcommand)] |
| command: Commands, |
| } |
|
|
| #[derive(Subcommand)] |
| enum Commands { |
| |
| Serve { |
| |
| #[arg(long)] |
| http_port: Option<u16>, |
| }, |
|
|
| |
| Worker { |
| |
| #[arg(long, default_value = "writer")] |
| role: String, |
| |
| #[arg(long)] |
| checkpoint: Option<String>, |
| |
| #[arg(long, default_value = "4")] |
| max_concurrent: usize, |
| }, |
|
|
| |
| Gate { |
| |
| tool: String, |
|
|
| |
| params: String, |
| }, |
|
|
| |
| Calculate { |
| |
| tool: String, |
|
|
| |
| params: String, |
| }, |
|
|
| |
| Status, |
|
|
| |
| Session, |
|
|
| |
| Reset, |
|
|
| |
| InitConfig, |
|
|
| |
| |
| |
| RefreshPaths { |
| |
| #[arg(long)] |
| dry_run: bool, |
| }, |
|
|
| |
| |
| |
| FsImport { |
| |
| virtual_path: String, |
|
|
| |
| device_file: PathBuf, |
|
|
| |
| #[arg(long)] |
| dry_run: bool, |
| }, |
|
|
| |
| |
| |
| FsExport { |
| |
| virtual_path: String, |
|
|
| |
| device_file: PathBuf, |
| }, |
|
|
| |
| ConfigImport { |
| |
| json_file: PathBuf, |
|
|
| |
| #[arg(long)] |
| dry_run: bool, |
| }, |
|
|
| |
| ConfigExport { |
| |
| json_file: PathBuf, |
| }, |
|
|
| |
| |
| |
| |
| |
|
|
| |
| WhitelistAdd { |
| |
| context: String, |
| |
| command: String, |
| |
| #[arg(long, default_value = "read")] |
| perm: String, |
| }, |
|
|
| |
| WhitelistRemove { |
| |
| context: String, |
| |
| command: String, |
| }, |
|
|
| |
| WhitelistList, |
|
|
| |
| WhitelistAddPath { |
| |
| path: String, |
| }, |
| } |
|
|
| fn main() -> Result<()> { |
| |
| |
| let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn,spf_smart_gate=info")).try_init(); |
|
|
| let cli = Cli::parse(); |
|
|
| |
| std::fs::create_dir_all(&cli.storage) |
| .with_context(|| format!("Failed to create storage dir {:?}", cli.storage))?; |
|
|
| |
| let config_db_path = paths::spf_root().join("LIVE/CONFIG/CONFIG.DB"); |
| let config_db = SpfConfigDb::open(&config_db_path) |
| .with_context(|| format!("Failed to open SPF_CONFIG LMDB at {:?}", config_db_path))?; |
|
|
| let config = config_db.load_full_config() |
| .with_context(|| "Failed to load config from LMDB")?; |
|
|
| |
| let storage = SpfStorage::open(&cli.storage) |
| .with_context(|| format!("Failed to open storage at {:?}", cli.storage))?; |
|
|
| |
| let session = storage.load_session()?.unwrap_or_else(Session::new); |
|
|
| match &cli.command { |
| Commands::Serve { http_port } => { |
| |
| let mut http_config = config::HttpConfig::load( |
| &paths::spf_root().join("LIVE/CONFIG/http.json") |
| ).unwrap_or_default(); |
|
|
| |
| if let Some(port) = http_port { |
| http_config.port = *port; |
| if http_config.transport == "stdio" { |
| http_config.transport = "both".to_string(); |
| } |
| } |
|
|
| |
| if let Ok(key) = std::env::var("SPF_API_KEY") { |
| if !key.is_empty() { |
| http_config.api_key = key; |
| } |
| } |
|
|
| |
| if http_config.api_key.is_empty() { |
| use rand::Rng; |
| let key_bytes: [u8; 32] = rand::rng().random(); |
| http_config.api_key = hex::encode(key_bytes); |
| |
| let config_path = paths::spf_root().join("LIVE/CONFIG/http.json"); |
| if let Some(parent) = config_path.parent() { |
| std::fs::create_dir_all(parent).ok(); |
| } |
| if let Ok(json) = serde_json::to_string_pretty(&http_config) { |
| std::fs::write(&config_path, json).ok(); |
| } |
| eprintln!("[SPF] Generated API key: {}", http_config.api_key); |
| } |
|
|
| |
| mcp::run(config, config_db, session, storage, http_config); |
| |
| } |
|
|
| Commands::Worker { role, checkpoint, max_concurrent } => { |
| |
| let transformer_config = config::TransformerConfig::load( |
| &paths::spf_root().join("LIVE/CONFIG/transformer.json") |
| ).unwrap_or_default(); |
|
|
| |
| let worker_config = worker::WorkerConfig { |
| role: role.clone(), |
| checkpoint_override: checkpoint.clone(), |
| max_concurrent: *max_concurrent, |
| ..Default::default() |
| }; |
|
|
| |
| let transformer_state = worker::init_transformer(&transformer_config, &worker_config) |
| .expect("Failed to initialize transformer"); |
| let transformer = std::sync::Arc::new(std::sync::RwLock::new(transformer_state)); |
|
|
| |
| let http_config = config::HttpConfig::load( |
| &paths::spf_root().join("LIVE/CONFIG/http.json") |
| ).unwrap_or_default(); |
|
|
| |
| mcp::run_worker(config, config_db, session, storage, http_config, |
| transformer, transformer_config, worker_config); |
| |
| } |
|
|
| Commands::Gate { tool, params } => { |
| let params: calculate::ToolParams = serde_json::from_str(params) |
| .with_context(|| format!("Invalid params JSON: {}", params))?; |
|
|
| let decision = gate::process(tool, ¶ms, &config, &session, &spf_smart_gate::dispatch::Source::Stdio); |
|
|
| println!("{}", serde_json::to_string_pretty(&decision)?); |
|
|
| if !decision.allowed { |
| std::process::exit(1); |
| } |
|
|
| |
| storage.save_session(&session)?; |
| } |
|
|
| Commands::Calculate { tool, params } => { |
| let params: calculate::ToolParams = serde_json::from_str(params) |
| .with_context(|| format!("Invalid params JSON: {}", params))?; |
|
|
| let result = calculate::calculate(tool, ¶ms, &config); |
|
|
| println!("{}", serde_json::to_string_pretty(&result)?); |
|
|
| |
| storage.save_session(&session)?; |
| } |
|
|
| Commands::Status => { |
| println!("SPF Smart Gateway v3.0.0"); |
| println!("Mode: {:?}", config.enforce_mode); |
| println!("Storage: {:?}", cli.storage); |
| println!("Config: LMDB (CONFIG/CONFIG.DB)"); |
| println!(); |
| println!("Session: {}", session.status_summary()); |
| println!(); |
| println!("Tiers:"); |
| println!(" SIMPLE < 500 | 40% analyze / 60% build"); |
| println!(" LIGHT < 2000 | 60% analyze / 40% build"); |
| println!(" MEDIUM < 10000 | 75% analyze / 25% build"); |
| println!(" CRITICAL > 10000 | 95% analyze / 5% build (requires approval)"); |
| println!(); |
| println!("Formula: a_optimal(C) = {} x (1 - 1/ln(C + e))", config.formula.w_eff); |
| println!("Complexity: C = basic^1 + deps^7 + complex^10 + files x 10"); |
| } |
|
|
| Commands::Session => { |
| println!("{}", serde_json::to_string_pretty(&session)?); |
| } |
|
|
| Commands::Reset => { |
| let new_session = Session::new(); |
| storage.save_session(&new_session)?; |
| println!("Session reset."); |
| } |
|
|
| Commands::InitConfig => { |
| |
| |
| let (config_count, paths_count, patterns_count) = config_db.stats()?; |
| println!("SPF_CONFIG LMDB initialized at {:?}", config_db_path); |
| println!(" Config entries: {}", config_count); |
| println!(" Path rules: {}", paths_count); |
| println!(" Dangerous patterns: {}", patterns_count); |
| println!(); |
| println!("Config is stored in LMDB, not JSON files."); |
| println!("Use MCP tools or direct LMDB access to modify."); |
| } |
|
|
| Commands::RefreshPaths { dry_run } => { |
| let root = paths::spf_root().to_string_lossy().to_string(); |
| let home = paths::actual_home().to_string_lossy().to_string(); |
| let sys_pkg = spf_smart_gate::paths::system_pkg_path(); |
|
|
| |
| let new_allowed: Vec<String> = vec![ |
| format!("{}/", home), |
| ]; |
| let new_blocked: Vec<String> = vec![ |
| "/tmp".to_string(), |
| "/etc".to_string(), |
| "/usr".to_string(), |
| "/system".to_string(), |
| sys_pkg, |
| format!("{}/src/", root), |
| format!("{}/LIVE/SPF_FS/blobs/", root), |
| format!("{}/Cargo.toml", root), |
| format!("{}/Cargo.lock", root), |
| format!("{}/.claude/", home), |
| ]; |
|
|
| |
| let current_rules = config_db.list_path_rules()?; |
| let cur_allowed: Vec<&str> = current_rules.iter() |
| .filter(|(t, _)| t == "allowed").map(|(_, p)| p.as_str()).collect(); |
| let cur_blocked: Vec<&str> = current_rules.iter() |
| .filter(|(t, _)| t == "blocked").map(|(_, p)| p.as_str()).collect(); |
|
|
| println!("=== SPF Refresh Paths ==="); |
| println!("SPF_ROOT: {}", root); |
| println!("HOME: {}", home); |
| println!(); |
| println!("CURRENT allowed ({}):", cur_allowed.len()); |
| for p in &cur_allowed { println!(" + {}", p); } |
| println!("CURRENT blocked ({}):", cur_blocked.len()); |
| for p in &cur_blocked { println!(" - {}", p); } |
| println!(); |
| println!("NEW allowed ({}):", new_allowed.len()); |
| for p in &new_allowed { println!(" + {}", p); } |
| println!("NEW blocked ({}):", new_blocked.len()); |
| for p in &new_blocked { println!(" - {}", p); } |
|
|
| if *dry_run { |
| println!(); |
| println!("[DRY RUN] No changes written."); |
| } else { |
| |
| for (rule_type, path) in ¤t_rules { |
| config_db.remove_path_rule(rule_type, path)?; |
| } |
| |
| for p in &new_allowed { |
| config_db.allow_path(p)?; |
| } |
| for p in &new_blocked { |
| config_db.block_path(p)?; |
| } |
| println!(); |
| println!("Path rules updated. {} allowed, {} blocked.", |
| new_allowed.len(), new_blocked.len()); |
| println!("All other config preserved (tiers, formula, weights, etc.)"); |
| } |
| } |
|
|
| |
| |
| |
| |
|
|
| Commands::FsImport { virtual_path, device_file, dry_run } => { |
| let data = std::fs::read(device_file) |
| .with_context(|| format!("Failed to read device file: {:?}", device_file))?; |
|
|
| println!("fs-import: {:?} -> {}", device_file, virtual_path); |
| println!(" Size: {} bytes", data.len()); |
|
|
| if *dry_run { |
| println!(" [DRY RUN] No changes made."); |
| return Ok(()); |
| } |
|
|
| |
| if virtual_path.starts_with("/home/agent/") { |
| |
| let relative = virtual_path.strip_prefix("/home/agent/").unwrap_or(virtual_path); |
| let agent_db_path = paths::spf_root().join("LIVE/LMDB5/LMDB5.DB"); |
| let agent_db = AgentStateDb::open(&agent_db_path) |
| .with_context(|| format!("Failed to open LMDB5 at {:?}", agent_db_path))?; |
|
|
| let content = utf8_safe::encode_for_lmdb(&data); |
| let key = format!("file:{}", relative); |
| agent_db.set_state(&key, &content) |
| .with_context(|| format!("Failed to store in LMDB5: {}", key))?; |
|
|
| |
| let stored = agent_db.get_state(&key)? |
| .ok_or_else(|| anyhow::anyhow!("Write succeeded but read-back failed: {}", key))?; |
|
|
| println!(" Target: LMDB5.DB (AgentState)"); |
| println!(" Key: {}", key); |
| println!(" Stored: {} bytes", stored.len()); |
| println!(" OK"); |
| } else { |
| |
| let fs_path = paths::spf_root().join("LIVE/SPF_FS"); |
| let spf_fs = SpfFs::open(&fs_path) |
| .with_context(|| format!("Failed to open SPF_FS at {:?}", fs_path))?; |
|
|
| spf_fs.write(virtual_path, &data) |
| .with_context(|| format!("Failed to write to virtual path: {}", virtual_path))?; |
|
|
| |
| let meta = spf_fs.stat(virtual_path)? |
| .ok_or_else(|| anyhow::anyhow!("Write succeeded but stat failed for: {}", virtual_path))?; |
|
|
| println!(" Target: SPF_FS.DB"); |
| println!(" Written: {} bytes (version {})", meta.size, meta.version); |
| if let Some(ref checksum) = meta.checksum { |
| println!(" Checksum: {}", &checksum[..16]); |
| } |
| println!(" OK"); |
| } |
| } |
|
|
| Commands::FsExport { virtual_path, device_file } => { |
| |
| let data: Vec<u8> = if virtual_path.starts_with("/home/agent/") { |
| |
| let relative = virtual_path.strip_prefix("/home/agent/").unwrap_or(virtual_path); |
| let agent_db_path = paths::spf_root().join("LIVE/LMDB5/LMDB5.DB"); |
| let agent_db = AgentStateDb::open(&agent_db_path) |
| .with_context(|| format!("Failed to open LMDB5 at {:?}", agent_db_path))?; |
|
|
| let key = format!("file:{}", relative); |
| let content = agent_db.get_state(&key)? |
| .ok_or_else(|| anyhow::anyhow!("Not found in LMDB5: {}", key))?; |
|
|
| println!(" Source: LMDB5.DB (AgentState)"); |
| println!(" Key: {}", key); |
| content.into_bytes() |
| } else { |
| |
| let fs_path = paths::spf_root().join("LIVE/SPF_FS"); |
| let spf_fs = SpfFs::open(&fs_path) |
| .with_context(|| format!("Failed to open SPF_FS at {:?}", fs_path))?; |
|
|
| println!(" Source: SPF_FS.DB"); |
| spf_fs.read(virtual_path) |
| .with_context(|| format!("Failed to read virtual path: {}", virtual_path))? |
| }; |
|
|
| |
| if let Some(parent) = device_file.parent() { |
| std::fs::create_dir_all(parent)?; |
| } |
|
|
| std::fs::write(device_file, &data) |
| .with_context(|| format!("Failed to write device file: {:?}", device_file))?; |
|
|
| println!("fs-export: {} -> {:?}", virtual_path, device_file); |
| println!(" Size: {} bytes", data.len()); |
| println!(" OK"); |
| } |
|
|
| |
| |
| |
|
|
| Commands::ConfigImport { json_file, dry_run } => { |
| let json_str = std::fs::read_to_string(json_file) |
| .with_context(|| format!("Failed to read config file: {:?}", json_file))?; |
|
|
| let json: serde_json::Value = serde_json::from_str(&json_str) |
| .with_context(|| "Invalid JSON in config file")?; |
|
|
| println!("config-import: {:?}", json_file); |
|
|
| |
| if let Some(mode) = json.get("enforce_mode").and_then(|v| v.as_str()) { |
| println!(" enforce_mode: {}", mode); |
| if !dry_run { |
| let mode = serde_json::from_value(json["enforce_mode"].clone())?; |
| config_db.set_enforce_mode(&mode)?; |
| } |
| } |
|
|
| |
| if let Some(tiers_val) = json.get("tiers") { |
| println!(" tiers: present"); |
| if !dry_run { |
| let tiers = serde_json::from_value(tiers_val.clone())?; |
| config_db.set_tiers(&tiers)?; |
| } |
| } |
|
|
| |
| if let Some(formula_val) = json.get("formula") { |
| println!(" formula: present"); |
| if !dry_run { |
| let formula = serde_json::from_value(formula_val.clone())?; |
| config_db.set_formula(&formula)?; |
| } |
| } |
|
|
| |
| if let Some(weights_val) = json.get("weights") { |
| println!(" weights: present"); |
| if !dry_run { |
| let weights = serde_json::from_value(weights_val.clone())?; |
| config_db.set_weights(&weights)?; |
| } |
| } |
|
|
| |
| if let Some(paths) = json.get("allowed_paths").and_then(|v| v.as_array()) { |
| println!(" allowed_paths: {} entries", paths.len()); |
| if !dry_run { |
| for path in paths { |
| if let Some(p) = path.as_str() { |
| config_db.allow_path(p)?; |
| } |
| } |
| } |
| } |
|
|
| |
| if let Some(paths) = json.get("blocked_paths").and_then(|v| v.as_array()) { |
| println!(" blocked_paths: {} entries", paths.len()); |
| if !dry_run { |
| for path in paths { |
| if let Some(p) = path.as_str() { |
| config_db.block_path(p)?; |
| } |
| } |
| } |
| } |
|
|
| |
| if let Some(patterns) = json.get("dangerous_patterns").and_then(|v| v.as_object()) { |
| println!(" dangerous_patterns: {} entries", patterns.len()); |
| if !dry_run { |
| for (pattern, severity) in patterns { |
| let sev = severity.as_u64().unwrap_or(5) as u8; |
| config_db.add_dangerous_pattern(pattern, sev)?; |
| } |
| } |
| } |
|
|
| |
| if let Some(obj) = json.get("config").and_then(|v| v.as_object()) { |
| println!(" config scalars: {} entries", obj.len()); |
| if !dry_run { |
| for (key, value) in obj { |
| if let Some(v) = value.as_str() { |
| config_db.set("spf", key, v)?; |
| } |
| } |
| } |
| } |
|
|
| if *dry_run { |
| println!(" [DRY RUN] No changes made."); |
| } else { |
| let (config_count, paths_count, patterns_count) = config_db.stats()?; |
| println!(" Imported. DB now: {} configs, {} paths, {} patterns", config_count, paths_count, patterns_count); |
| } |
| println!(" OK"); |
| } |
|
|
| Commands::ConfigExport { json_file } => { |
| |
| let path_rules = config_db.list_path_rules()?; |
| let mut allowed_paths = Vec::new(); |
| let mut blocked_paths = Vec::new(); |
| for (rule_type, path) in &path_rules { |
| match rule_type.as_str() { |
| "allowed" => allowed_paths.push(path.clone()), |
| "blocked" => blocked_paths.push(path.clone()), |
| _ => {} |
| } |
| } |
|
|
| let dangerous_patterns = config_db.list_dangerous_patterns()?; |
| let mut patterns_map = serde_json::Map::new(); |
| for (pattern, severity) in &dangerous_patterns { |
| patterns_map.insert(pattern.clone(), serde_json::json!(severity)); |
| } |
|
|
| let export = serde_json::json!({ |
| "version": config.version, |
| "enforce_mode": config.enforce_mode, |
| "tiers": config.tiers, |
| "formula": config.formula, |
| "weights": config.complexity_weights, |
| "allowed_paths": allowed_paths, |
| "blocked_paths": blocked_paths, |
| "dangerous_patterns": patterns_map, |
| "config": { |
| "require_read_before_edit": config.require_read_before_edit.to_string(), |
| "max_write_size": config.max_write_size.to_string(), |
| } |
| }); |
|
|
| |
| if let Some(parent) = json_file.parent() { |
| std::fs::create_dir_all(parent)?; |
| } |
|
|
| let json_str = serde_json::to_string_pretty(&export)?; |
| std::fs::write(json_file, &json_str) |
| .with_context(|| format!("Failed to write config export: {:?}", json_file))?; |
|
|
| println!("config-export: -> {:?}", json_file); |
| println!(" {} configs, {} path rules, {} patterns", |
| path_rules.len(), allowed_paths.len() + blocked_paths.len(), dangerous_patterns.len()); |
| println!(" {} bytes written", json_str.len()); |
| println!(" OK"); |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| Commands::WhitelistAdd { context, command, perm } => { |
| let ctx = match context.as_str() { |
| "user" => "user_fs", |
| "sandbox" => "sandbox", |
| _ => { eprintln!("Context must be 'user' or 'sandbox'"); std::process::exit(1); } |
| }; |
| let permission = match perm.as_str() { |
| "read" => CommandPerm::read_only(), |
| "read-write" => CommandPerm::read_write(), |
| "full" => CommandPerm::full(), |
| _ => { eprintln!("Perm must be 'read', 'read-write', or 'full'"); std::process::exit(1); } |
| }; |
| config_db.add_command(ctx, command, permission)?; |
| println!("Added: {}:{} = R:{} W:{} X:{}", ctx, command, permission.read, permission.write, permission.execute); |
| println!("Restart SPF for changes to take effect."); |
| } |
|
|
| Commands::WhitelistRemove { context, command } => { |
| let ctx = match context.as_str() { |
| "user" => "user_fs", |
| "sandbox" => "sandbox", |
| _ => { eprintln!("Context must be 'user' or 'sandbox'"); std::process::exit(1); } |
| }; |
| let removed = config_db.remove_command(ctx, command)?; |
| if removed { |
| println!("Removed: {}:{}", ctx, command); |
| } else { |
| println!("Not found: {}:{}", ctx, command); |
| } |
| println!("Restart SPF for changes to take effect."); |
| } |
|
|
| Commands::WhitelistList => { |
| println!("=== Command Whitelists ==="); |
| println!(); |
| println!("USER FILESYSTEM:"); |
| for (cmd, perm) in config_db.list_commands("user_fs")? { |
| println!(" {} — R:{} W:{} X:{}", cmd, perm.read, perm.write, perm.execute); |
| } |
| println!(); |
| println!("SANDBOX:"); |
| for (cmd, perm) in config_db.list_commands("sandbox")? { |
| println!(" {} — R:{} W:{} X:{}", cmd, perm.read, perm.write, perm.execute); |
| } |
| println!(); |
| println!("USER FS PATHS:"); |
| for path in config_db.list_user_fs_paths()? { |
| println!(" {}", path); |
| } |
| } |
|
|
| Commands::WhitelistAddPath { path } => { |
| config_db.add_user_fs_path(path)?; |
| println!("Added user FS path: {}", path); |
| println!("Restart SPF for changes to take effect."); |
| } |
| } |
|
|
| Ok(()) |
| } |
|
|