|
| 1 | +use std::sync::Mutex; |
| 2 | + |
| 3 | +use data_studio_agent::traits::{CancelMap, EventEmitter}; |
| 4 | +use serde_json::Value; |
| 5 | + |
| 6 | +use crate::ai_permissions::AccessDenial; |
| 7 | + |
| 8 | +pub(crate) struct AgentRunEmitter<E> { |
| 9 | + pub inner: E, |
| 10 | + pub cancel_map: CancelMap, |
| 11 | + pub blocked: Mutex<Option<Value>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl<E: EventEmitter> EventEmitter for AgentRunEmitter<E> { |
| 15 | + fn emit(&self, event: &str, mut payload: Value) { |
| 16 | + if event == "agent-loop-tool-result" && payload["denied"] == true { |
| 17 | + let denial = AccessDenial::from_message( |
| 18 | + payload["envelope"]["summary"] |
| 19 | + .as_str() |
| 20 | + .unwrap_or("Access denied"), |
| 21 | + ); |
| 22 | + payload["envelope"]["summary"] = Value::String(denial.message.clone()); |
| 23 | + payload["denial"] = serde_json::to_value(&denial).unwrap(); |
| 24 | + if denial.kind != "user" { |
| 25 | + let mut blocked = self |
| 26 | + .blocked |
| 27 | + .lock() |
| 28 | + .unwrap_or_else(|error| error.into_inner()); |
| 29 | + if blocked.is_none() { |
| 30 | + *blocked = Some(payload.clone()); |
| 31 | + if let Some(session) = payload["session_id"].as_str() { |
| 32 | + // Use the runtime's cancellation boundary after it records the denied result. |
| 33 | + let _ = data_studio_agent::loop_runner::cancel_agent_loop( |
| 34 | + session, |
| 35 | + &self.cancel_map, |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + let blocked = self |
| 42 | + .blocked |
| 43 | + .lock() |
| 44 | + .unwrap_or_else(|error| error.into_inner()) |
| 45 | + .is_some(); |
| 46 | + if blocked |
| 47 | + && (event == "agent-loop-done" |
| 48 | + || event == "agent-loop-iteration" |
| 49 | + || (event == "agent-loop-error" && payload["error"] == "cancelled")) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + self.inner.emit(event, payload); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use super::*; |
| 60 | + use serde_json::json; |
| 61 | + use std::{collections::HashMap, sync::Arc}; |
| 62 | + |
| 63 | + #[derive(Default)] |
| 64 | + struct Events(Mutex<Vec<(String, Value)>>); |
| 65 | + impl EventEmitter for Events { |
| 66 | + fn emit(&self, event: &str, payload: Value) { |
| 67 | + self.0.lock().unwrap().push((event.into(), payload)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn permission_denial_cancels_further_work_without_hiding_the_result() { |
| 73 | + let (tx, mut rx) = tokio::sync::oneshot::channel(); |
| 74 | + let emitter = AgentRunEmitter { |
| 75 | + inner: Events::default(), |
| 76 | + cancel_map: Arc::new(Mutex::new(HashMap::from([("chat".into(), tx)]))), |
| 77 | + blocked: Mutex::new(None), |
| 78 | + }; |
| 79 | + let denial = AccessDenial::permission( |
| 80 | + crate::ai_permissions::Permission::Manage, |
| 81 | + "db", |
| 82 | + "connection", |
| 83 | + ); |
| 84 | + emitter.emit("agent-loop-tool-result", json!({"session_id":"chat","tool_call_id":"call","denied":true,"envelope":{"summary":denial.encode()}})); |
| 85 | + assert_eq!(rx.try_recv(), Ok(())); |
| 86 | + emitter.emit("agent-loop-error", json!({"error":"cancelled"})); |
| 87 | + emitter.emit("agent-loop-iteration", json!({})); |
| 88 | + let events = emitter.inner.0.lock().unwrap(); |
| 89 | + assert_eq!(events.len(), 1); |
| 90 | + assert_eq!(events[0].1["denial"]["source"], "connection"); |
| 91 | + assert_eq!(events[0].1["envelope"]["summary"], denial.message); |
| 92 | + assert!(emitter.blocked.lock().unwrap().is_some()); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn user_rejection_does_not_become_a_permission_block() { |
| 97 | + let emitter = AgentRunEmitter { |
| 98 | + inner: Events::default(), |
| 99 | + cancel_map: Arc::new(Mutex::new(HashMap::new())), |
| 100 | + blocked: Mutex::new(None), |
| 101 | + }; |
| 102 | + emitter.emit( |
| 103 | + "agent-loop-tool-result", |
| 104 | + json!({"denied":true,"envelope":{"summary":"Tool call was not approved"}}), |
| 105 | + ); |
| 106 | + assert!(emitter.blocked.lock().unwrap().is_none()); |
| 107 | + assert_eq!( |
| 108 | + emitter.inner.0.lock().unwrap()[0].1["denial"]["kind"], |
| 109 | + "user" |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + #[tokio::test] |
| 114 | + async fn denied_preflight_persists_result_and_stops_before_another_model_turn() { |
| 115 | + use async_trait::async_trait; |
| 116 | + use data_studio_agent::{ |
| 117 | + storage::{db::AgentDb, session_store::SqliteSessionStore}, |
| 118 | + tool_executor::{ToolCallPreparation, ToolEnvelope, ToolExecutor}, |
| 119 | + }; |
| 120 | + use std::sync::atomic::{AtomicUsize, Ordering}; |
| 121 | + |
| 122 | + struct Deny; |
| 123 | + #[async_trait] |
| 124 | + impl ToolExecutor for Deny { |
| 125 | + fn prepare( |
| 126 | + &self, |
| 127 | + _: &str, |
| 128 | + _: &Value, |
| 129 | + _: &Value, |
| 130 | + _: &HashMap<String, Value>, |
| 131 | + ) -> Result<ToolCallPreparation, String> { |
| 132 | + Err(AccessDenial::permission( |
| 133 | + crate::ai_permissions::Permission::Manage, |
| 134 | + "db", |
| 135 | + "system", |
| 136 | + ) |
| 137 | + .encode()) |
| 138 | + } |
| 139 | + async fn execute(&self, _: &str, _: &Value, _: &Value) -> Result<ToolEnvelope, String> { |
| 140 | + panic!("a blocked action must not execute") |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + let requests = Arc::new(AtomicUsize::new(0)); |
| 145 | + let count = requests.clone(); |
| 146 | + let response = format!( |
| 147 | + "data: {}\n\ndata: {}\n\ndata: [DONE]\n\n", |
| 148 | + json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call","type":"function","function":{"name":"sqlkit__execute_ddl","arguments":"{\"connection_id\":\"db\",\"sql\":\"CREATE TABLE example(id INT)\"}"}}]}}]}), |
| 149 | + json!({"choices":[{"delta":{},"finish_reason":"tool_calls"}]}) |
| 150 | + ); |
| 151 | + let router = axum::Router::new().route( |
| 152 | + "/v1/chat/completions", |
| 153 | + axum::routing::post(move || { |
| 154 | + count.fetch_add(1, Ordering::SeqCst); |
| 155 | + let response = response.clone(); |
| 156 | + async move { ([("content-type", "text/event-stream")], response) } |
| 157 | + }), |
| 158 | + ); |
| 159 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 160 | + let address = listener.local_addr().unwrap(); |
| 161 | + let server = tokio::spawn(async move { axum::serve(listener, router).await.unwrap() }); |
| 162 | + let db = AgentDb(Arc::new(Mutex::new( |
| 163 | + rusqlite::Connection::open_in_memory().unwrap(), |
| 164 | + ))); |
| 165 | + data_studio_agent::storage::db::migrate(&db).unwrap(); |
| 166 | + db.0.lock().unwrap().execute("INSERT INTO agent_sessions (id,title,created_at,updated_at) VALUES ('chat','Chat',0,0)", []).unwrap(); |
| 167 | + let cancel_map = Arc::new(Mutex::new(HashMap::new())); |
| 168 | + let emitter = AgentRunEmitter { |
| 169 | + inner: Events::default(), |
| 170 | + cancel_map: cancel_map.clone(), |
| 171 | + blocked: Mutex::new(None), |
| 172 | + }; |
| 173 | + let settings = json!({"baseUrl":format!("http://{address}"),"model":"gpt-4o-mini","apiKey":"synthetic","proxyMode":"none","tools":[{"type":"function","function":{"name":"sqlkit__execute_ddl","parameters":{"type":"object"}}}],"maxIterations":3}); |
| 174 | + let result = tokio::time::timeout( |
| 175 | + std::time::Duration::from_secs(10), |
| 176 | + data_studio_agent::loop_runner::run_agent_loop( |
| 177 | + "chat", |
| 178 | + "Create example", |
| 179 | + &settings, |
| 180 | + &SqliteSessionStore::new(db.clone()), |
| 181 | + &emitter, |
| 182 | + &Deny, |
| 183 | + HashMap::from([("db".into(), json!({"connectionId":"db"}))]), |
| 184 | + Value::Null, |
| 185 | + &Arc::new(Mutex::new(HashMap::new())), |
| 186 | + &cancel_map, |
| 187 | + &|_| false, |
| 188 | + &|_, _| false, |
| 189 | + ), |
| 190 | + ) |
| 191 | + .await |
| 192 | + .unwrap(); |
| 193 | + server.abort(); |
| 194 | + assert_eq!(result.unwrap_err(), "cancelled"); |
| 195 | + assert_eq!(requests.load(Ordering::SeqCst), 1); |
| 196 | + assert!(emitter.blocked.lock().unwrap().is_some()); |
| 197 | + let conn = db.0.lock().unwrap(); |
| 198 | + let status: String = conn |
| 199 | + .query_row( |
| 200 | + "SELECT status FROM agent_tool_calls WHERE id='call'", |
| 201 | + [], |
| 202 | + |row| row.get(0), |
| 203 | + ) |
| 204 | + .unwrap(); |
| 205 | + assert_eq!(status, "denied"); |
| 206 | + let result_count: i64 = conn |
| 207 | + .query_row( |
| 208 | + "SELECT COUNT(*) FROM agent_messages WHERE role='tool'", |
| 209 | + [], |
| 210 | + |row| row.get(0), |
| 211 | + ) |
| 212 | + .unwrap(); |
| 213 | + assert_eq!(result_count, 1); |
| 214 | + } |
| 215 | +} |
0 commit comments