|
| 1 | +//! Managed-agent definition validation at local mutation boundaries. |
| 2 | +
|
| 3 | +use crate::managed_agents::{CreateManagedAgentRequest, ManagedAgentRecord}; |
| 4 | + |
| 5 | +pub(super) fn validate_create_definition( |
| 6 | + name: &str, |
| 7 | + persona_id: Option<&str>, |
| 8 | + input: &CreateManagedAgentRequest, |
| 9 | +) -> Result<(), String> { |
| 10 | + validate_definition_fields(name, persona_id, input.system_prompt.as_deref()) |
| 11 | +} |
| 12 | + |
| 13 | +fn validate_definition_fields( |
| 14 | + name: &str, |
| 15 | + persona_id: Option<&str>, |
| 16 | + system_prompt: Option<&str>, |
| 17 | +) -> Result<(), String> { |
| 18 | + crate::managed_agents::validate_managed_agent_definition_text(name, persona_id, system_prompt) |
| 19 | + .map_err(|error| format!("Managed agent definition is unsafe: {error}")) |
| 20 | +} |
| 21 | + |
| 22 | +/// Apply definition-owned update fields, then validate the complete |
| 23 | +/// prospective definition before the caller can persist it. |
| 24 | +pub(super) fn apply_model_provider_prompt_update( |
| 25 | + record: &mut ManagedAgentRecord, |
| 26 | + model: Option<Option<String>>, |
| 27 | + provider: Option<Option<String>>, |
| 28 | + system_prompt: Option<Option<String>>, |
| 29 | +) -> Result<(), String> { |
| 30 | + if record.persona_id.is_none() { |
| 31 | + if let Some(model_update) = model { |
| 32 | + record.model = model_update; |
| 33 | + } |
| 34 | + if let Some(provider_update) = provider { |
| 35 | + record.provider = provider_update; |
| 36 | + } |
| 37 | + if let Some(prompt_update) = system_prompt { |
| 38 | + record.system_prompt = prompt_update; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + validate_definition_fields( |
| 43 | + &record.name, |
| 44 | + record.persona_id.as_deref(), |
| 45 | + record.system_prompt.as_deref(), |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + |
| 53 | + fn standalone_record() -> ManagedAgentRecord { |
| 54 | + serde_json::from_value(serde_json::json!({ |
| 55 | + "pubkey": "standalone1", |
| 56 | + "name": "standalone-agent", |
| 57 | + "private_key_nsec": "nsec1fake", |
| 58 | + "relay_url": "wss://localhost:3000", |
| 59 | + "acp_command": "buzz-acp", |
| 60 | + "agent_command": "goose", |
| 61 | + "agent_args": [], |
| 62 | + "mcp_command": "", |
| 63 | + "turn_timeout_seconds": 320, |
| 64 | + "system_prompt": "safe prompt", |
| 65 | + "model": null, |
| 66 | + "provider": null, |
| 67 | + "env_vars": {}, |
| 68 | + "created_at": "2026-01-01T00:00:00Z", |
| 69 | + "updated_at": "2026-01-01T00:00:00Z", |
| 70 | + "last_started_at": null, |
| 71 | + "last_stopped_at": null, |
| 72 | + "last_exit_code": null, |
| 73 | + "last_error": null |
| 74 | + })) |
| 75 | + .expect("standalone agent record") |
| 76 | + } |
| 77 | + |
| 78 | + fn create_request(system_prompt: &str) -> CreateManagedAgentRequest { |
| 79 | + serde_json::from_value(serde_json::json!({ |
| 80 | + "name": "Reviewer", |
| 81 | + "systemPrompt": system_prompt |
| 82 | + })) |
| 83 | + .expect("create request") |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn create_rejects_invisible_definition_less_name_or_prompt() { |
| 88 | + for (name, prompt, code) in [ |
| 89 | + ("Review\u{200B}er", "Review code.", "U+200B"), |
| 90 | + ("Reviewer", "Review\u{202E} code.", "U+202E"), |
| 91 | + ] { |
| 92 | + let input = create_request(prompt); |
| 93 | + let error = validate_create_definition(name, None, &input) |
| 94 | + .expect_err("create must reject unsafe definition text"); |
| 95 | + assert!(error.contains(code), "unexpected error: {error}"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn create_accepts_visible_multiline_definition_less_prompt() { |
| 101 | + let input = create_request("Review changes.\n\tCall out security risks."); |
| 102 | + validate_create_definition("Reviewer 🐝", None, &input) |
| 103 | + .expect("visible multiline instructions should remain valid"); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn update_rejects_invisible_definition_less_name_or_prompt() { |
| 108 | + let mut unsafe_prompt = standalone_record(); |
| 109 | + let error = apply_model_provider_prompt_update( |
| 110 | + &mut unsafe_prompt, |
| 111 | + None, |
| 112 | + None, |
| 113 | + Some(Some("Review\u{200B} code.".to_string())), |
| 114 | + ) |
| 115 | + .expect_err("definition-less prompt update must reject invisible text"); |
| 116 | + assert!(error.contains("U+200B"), "unexpected error: {error}"); |
| 117 | + |
| 118 | + let mut unsafe_name = standalone_record(); |
| 119 | + unsafe_name.name = "Review\u{202E}er".to_string(); |
| 120 | + let error = apply_model_provider_prompt_update(&mut unsafe_name, None, None, None) |
| 121 | + .expect_err("definition-less name update must reject formatting controls"); |
| 122 | + assert!(error.contains("U+202E"), "unexpected error: {error}"); |
| 123 | + } |
| 124 | +} |
0 commit comments