| |
| |
| |
| |
| |
|
|
| use crate::config::SpfConfig; |
| use serde::{Deserialize, Serialize}; |
|
|
| |
| #[derive(Debug, Clone, Serialize, Deserialize)] |
| pub struct ComplexityResult { |
| pub tool: String, |
| pub c: u64, |
| pub tier: String, |
| pub analyze_percent: u8, |
| pub build_percent: u8, |
| pub a_optimal_tokens: u64, |
| pub requires_approval: bool, |
| } |
|
|
| |
| |
| #[derive(Debug, Clone, Deserialize, Default)] |
| pub struct ToolParams { |
| |
| pub file_path: Option<String>, |
| |
| pub old_string: Option<String>, |
| pub new_string: Option<String>, |
| pub replace_all: Option<bool>, |
| |
| pub content: Option<String>, |
| |
| pub command: Option<String>, |
| |
| pub query: Option<String>, |
| pub pattern: Option<String>, |
| pub path: Option<String>, |
| |
| pub collection: Option<String>, |
| pub limit: Option<u64>, |
| pub text: Option<String>, |
| pub title: Option<String>, |
| |
| pub url: Option<String>, |
| pub topic: Option<String>, |
| pub category: Option<String>, |
| |
| pub tool_name: Option<String>, |
| pub reason: Option<String>, |
| pub peer_key: Option<String>, |
| pub message: Option<String>, |
| pub prompt: Option<String>, |
| pub batch_size: Option<u64>, |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| fn calc_complex_factor(content_len: u64, has_risk: bool, is_architectural: bool) -> u64 { |
| let mut complex: u64 = 0; |
| |
| |
| if content_len > 200 { complex += 1; } |
| if content_len > 1000 { complex += 1; } |
| if content_len > 5000 { complex += 1; } |
| |
| |
| if has_risk { complex += 1; } |
| |
| |
| if is_architectural { complex = complex.max(3); } |
| |
| complex.min(4) |
| } |
|
|
| |
| fn calc_files_factor(path: &str, pattern: &str, cmd: &str) -> u64 { |
| |
| if cmd.contains("find") || cmd.contains("xargs") || cmd.contains("-r ") { |
| return 100; |
| } |
| |
| |
| if pattern.contains("**") || path.contains("**") || cmd.contains("**") { |
| return 50; |
| } |
| |
| |
| if pattern.contains("*") || path.contains("*") || cmd.contains("*") { |
| return 20; |
| } |
| |
| |
| if path == "." || path == "/" || path.ends_with("src") || path.ends_with("lib") { |
| return 20; |
| } |
| |
| |
| 1 |
| } |
|
|
| |
| fn is_architectural_file(path: &str) -> bool { |
| let p = path.to_lowercase(); |
| p.contains("config") || p.contains("main.") || p.contains("lib.") |
| || p.contains("mod.") || p.contains("cargo.toml") || p.contains("package.json") |
| || p.contains(".env") || p.contains("settings") || p.contains("schema") |
| || p.ends_with("rc") || p.ends_with(".yaml") || p.ends_with(".yml") |
| } |
|
|
| |
| fn has_risk_indicators(content: &str) -> bool { |
| content.contains("delete") || content.contains("drop") || content.contains("remove") |
| || content.contains("truncate") || content.contains("override") |
| || content.contains("force") || content.contains("unsafe") |
| || content.contains("rm ") || content.contains("sudo") |
| } |
|
|
| |
| pub fn calculate_c(tool: &str, params: &ToolParams, config: &SpfConfig) -> u64 { |
| let (basic, dependencies, complex_factor, files) = match tool { |
| "Edit" | "spf_edit" => { |
| let old_str = params.old_string.as_deref().unwrap_or(""); |
| let new_str = params.new_string.as_deref().unwrap_or(""); |
| let old_len = old_str.len() as u64; |
| let new_len = new_str.len() as u64; |
| let total_len = old_len + new_len; |
| let file_path = params.file_path.as_deref().unwrap_or(""); |
| |
| let basic = config.complexity_weights.edit.basic + total_len / 20; |
| |
| |
| let mut deps = if params.replace_all.unwrap_or(false) { 3u64 } else { 1 }; |
| if total_len > 500 { deps += 1; } |
| |
| |
| let has_risk = has_risk_indicators(new_str); |
| let is_arch = is_architectural_file(file_path); |
| let complex = calc_complex_factor(total_len, has_risk, is_arch); |
| |
| |
| let files = if params.replace_all.unwrap_or(false) { 5u64 } else { 1 }; |
| |
| (basic, deps, complex, files) |
| } |
|
|
| "Write" | "spf_write" => { |
| let content = params.content.as_deref().unwrap_or(""); |
| let content_len = content.len() as u64; |
| let file_path = params.file_path.as_deref().unwrap_or(""); |
|
|
| let basic = config.complexity_weights.write.basic + content_len / 50; |
| |
| |
| let mut deps = config.complexity_weights.write.dependencies; |
| if content.contains("import ") || content.contains("require(") |
| || content.contains("use ") || content.contains("mod ") { |
| deps += 2; |
| } |
| |
| |
| let has_risk = has_risk_indicators(content); |
| let is_arch = is_architectural_file(file_path); |
| let complex = calc_complex_factor(content_len, has_risk, is_arch); |
| |
| (basic, deps, complex, 1u64) |
| } |
|
|
| "Bash" | "spf_bash" => { |
| let cmd = params.command.as_deref().unwrap_or(""); |
|
|
| |
| let is_dangerous = config.dangerous_commands.iter().any(|d| cmd.contains(d.as_str())); |
| |
| let is_git = cmd.contains("git push") || cmd.contains("git reset") |
| || cmd.contains("git rebase") || cmd.contains("git merge"); |
| |
| let is_piped = cmd.contains("&&") || cmd.contains("|"); |
| |
| |
| let files = calc_files_factor("", "", cmd); |
| |
| |
| let pipe_count = cmd.matches("|").count() as u64; |
| let chain_count = cmd.matches("&&").count() as u64; |
| |
| if is_dangerous { |
| let w = &config.complexity_weights.bash_dangerous; |
| |
| (w.basic, w.dependencies + pipe_count + chain_count, 3u64.max(w.complex), files) |
| } else if is_git { |
| let w = &config.complexity_weights.bash_git; |
| |
| (w.basic, w.dependencies + pipe_count, 2u64.max(w.complex), files) |
| } else if is_piped { |
| let w = &config.complexity_weights.bash_piped; |
| |
| let complex = (1 + pipe_count).min(3); |
| (w.basic, w.dependencies + pipe_count + chain_count, complex, files) |
| } else { |
| let w = &config.complexity_weights.bash_simple; |
| (w.basic, w.dependencies, w.complex, files) |
| } |
| } |
|
|
| "Read" | "spf_read" => { |
| |
| let w = &config.complexity_weights.read; |
| (w.basic, w.dependencies, w.complex, w.files) |
| } |
|
|
| "Glob" | "spf_glob" | "Grep" | "spf_grep" => { |
| let w = &config.complexity_weights.search; |
| let path = params.path.as_deref().unwrap_or("."); |
| let pattern = params.pattern.as_deref().unwrap_or(""); |
| |
| |
| let files = calc_files_factor(path, pattern, ""); |
| |
| |
| let complex = if pattern.len() > 50 { 1u64 } else { w.complex }; |
| |
| (w.basic, w.dependencies, complex, files) |
| } |
|
|
| |
| "brain_search" | "spf_brain_search" => { |
| let limit = params.limit.unwrap_or(5); |
| (10, limit, 0, 1) |
| } |
| "brain_store" | "spf_brain_store" => { |
| let text_len = params.text.as_ref().map(|s| s.len()).unwrap_or(0) as u64; |
| (20 + text_len / 50, 2, if text_len > 5000 { 1 } else { 0 }, 1) |
| } |
| "brain_index" | "spf_brain_index" => (50, 5, 1, 10), |
| "brain_recall" | "spf_brain_recall" | |
| "brain_context" | "spf_brain_context" | |
| "brain_list" | "spf_brain_list" | |
| "brain_status" | "spf_brain_status" | |
| "brain_list_docs" | "spf_brain_list_docs" | |
| "brain_get_doc" | "spf_brain_get_doc" => (10, 1, 0, 1), |
|
|
| |
| "rag_collect_web" | "spf_rag_collect_web" => (50, 10, 1, 5), |
| "rag_fetch_url" | "spf_rag_fetch_url" => (30, 5, 1, 1), |
| "rag_collect_file" | "spf_rag_collect_file" => (15, 2, 0, 1), |
| "rag_collect_folder" | "spf_rag_collect_folder" => (30, 5, 0, 10), |
| "rag_index_gathered" | "spf_rag_index_gathered" => (40, 5, 1, 10), |
| "rag_collect_drop" | "spf_rag_collect_drop" => (25, 3, 0, 5), |
| "rag_collect_rss" | "spf_rag_collect_rss" => (25, 5, 0, 5), |
| "rag_dedupe" | "spf_rag_dedupe" => (20, 3, 0, 1), |
| "rag_smart_search" | "spf_rag_smart_search" | |
| "rag_auto_fetch_gaps" | "spf_rag_auto_fetch_gaps" => (40, 8, 1, 5), |
| "rag_fulfill_search" | "spf_rag_fulfill_search" => (20, 3, 0, 1), |
| "rag_status" | "spf_rag_status" | |
| "rag_list_gathered" | "spf_rag_list_gathered" | |
| "rag_bandwidth_status" | "spf_rag_bandwidth_status" | |
| "rag_list_feeds" | "spf_rag_list_feeds" | |
| "rag_pending_searches" | "spf_rag_pending_searches" => (8, 1, 0, 1), |
|
|
| |
| "web_fetch" | "spf_web_fetch" => (30, 5, 1, 1), |
| "web_search" | "spf_web_search" => (25, 3, 0, 1), |
| "web_download" | "spf_web_download" => (30, 5, 1, 1), |
| "web_api" | "spf_web_api" => (25, 3, 0, 1), |
| |
| "web_connect" | "spf_web_connect" => (20, 2, 0, 1), |
| "web_navigate" | "spf_web_navigate" => (30, 5, 1, 1), |
| "web_click" | "spf_web_click" => (15, 2, 0, 1), |
| "web_fill" | "spf_web_fill" => (15, 2, 0, 1), |
| "web_select" | "spf_web_select" => (10, 1, 0, 1), |
| "web_eval" | "spf_web_eval" => (40, 8, 2, 1), |
| "web_screenshot" | "spf_web_screenshot" => (10, 1, 0, 1), |
| "web_design" | "spf_web_design" => (20, 3, 0, 1), |
| "web_page" | "spf_web_page" => (10, 1, 0, 1), |
|
|
| |
|
|
| |
| "notebook_edit" | "spf_notebook_edit" => (15, 2, 0, 1), |
|
|
| |
| "status" | "spf_status" | "session" | "spf_session" | |
| "calculate" | "spf_calculate" => (5, 0, 0, 1), |
|
|
| |
| _ => { |
| let w = &config.complexity_weights.unknown; |
| (w.basic, w.dependencies, w.complex, w.files) |
| } |
| }; |
|
|
| |
| |
| let c = basic.saturating_pow(config.formula.basic_power) |
| .saturating_add(dependencies.saturating_pow(config.formula.deps_power)) |
| .saturating_add(complex_factor.saturating_pow(config.formula.complex_power)) |
| .saturating_add(files.saturating_mul(config.formula.files_multiplier)); |
|
|
| c |
| } |
|
|
| |
| pub fn a_optimal(c: u64, config: &SpfConfig) -> u64 { |
| let c_f = if c == 0 { 1.0 } else { c as f64 }; |
| let result = config.formula.w_eff * (1.0 - 1.0 / (c_f + config.formula.e).ln()); |
| result.max(0.0) as u64 |
| } |
|
|
| |
| pub fn calculate(tool: &str, params: &ToolParams, config: &SpfConfig) -> ComplexityResult { |
| let c = calculate_c(tool, params, config); |
| let (tier, analyze, build, requires_approval) = config.get_tier(c); |
| let tokens = a_optimal(c, config); |
|
|
| ComplexityResult { |
| tool: tool.to_string(), |
| c, |
| tier: tier.to_string(), |
| analyze_percent: analyze, |
| build_percent: build, |
| a_optimal_tokens: tokens, |
| requires_approval, |
| } |
| } |
|
|
| |
| |
| |
|
|
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| use crate::config::SpfConfig; |
|
|
| fn default_config() -> SpfConfig { |
| SpfConfig::default() |
| } |
|
|
| #[test] |
| fn read_produces_simple_tier() { |
| let config = default_config(); |
| let params = ToolParams::default(); |
| let result = calculate("spf_read", ¶ms, &config); |
| assert_eq!(result.tier, "SIMPLE"); |
| assert!(result.c < 500, "Read C={} should be < 500", result.c); |
| } |
|
|
| #[test] |
| fn simple_bash_is_simple_tier() { |
| let config = default_config(); |
| let params = ToolParams { command: Some("ls -la".to_string()), ..Default::default() }; |
| let result = calculate("spf_bash", ¶ms, &config); |
| assert_eq!(result.tier, "SIMPLE", "Simple bash C={} tier={}", result.c, result.tier); |
| } |
|
|
| #[test] |
| fn dangerous_bash_is_critical_tier() { |
| let config = default_config(); |
| let params = ToolParams { command: Some("rm -rf / --no-preserve-root".to_string()), ..Default::default() }; |
| let result = calculate("spf_bash", ¶ms, &config); |
| assert_eq!(result.tier, "CRITICAL", "Dangerous bash C={} should be CRITICAL", result.c); |
| assert!(result.c >= 10000); |
| } |
|
|
| #[test] |
| fn status_tool_is_minimal_complexity() { |
| let config = default_config(); |
| let params = ToolParams::default(); |
| let result = calculate("spf_status", ¶ms, &config); |
| assert!(result.c < 100, "Status C={} should be minimal", result.c); |
| assert_eq!(result.tier, "SIMPLE"); |
| } |
|
|
| #[test] |
| fn unknown_tool_uses_default_weights() { |
| let config = default_config(); |
| let params = ToolParams::default(); |
| let c = calculate_c("totally_unknown_tool", ¶ms, &config); |
| |
| |
| assert!(c >= 2000, "Unknown tool C={} should be >= 2000 (LIGHT+)", c); |
| } |
|
|
| #[test] |
| fn a_optimal_within_bounds() { |
| let config = default_config(); |
| let tokens = a_optimal(100, &config); |
| assert!(tokens > 0, "a_optimal(100) should be > 0"); |
| assert!(tokens < 40000, "a_optimal(100)={} should be < W_eff(40000)", tokens); |
| } |
|
|
| #[test] |
| fn a_optimal_zero_input() { |
| let config = default_config(); |
| let tokens = a_optimal(0, &config); |
| |
| assert!(tokens > 0, "a_optimal(0)={} should still be > 0", tokens); |
| } |
|
|
| #[test] |
| fn risk_indicators_detected() { |
| assert!(has_risk_indicators("please delete this file")); |
| assert!(has_risk_indicators("sudo make install")); |
| assert!(has_risk_indicators("rm -rf everything")); |
| assert!(!has_risk_indicators("create a new file")); |
| assert!(!has_risk_indicators("read the documentation")); |
| } |
| } |
|
|