Skip to content

Commit 4705345

Browse files
committed
fix: enforce live AI permissions at application dispatch
1 parent 0a1983c commit 4705345

16 files changed

Lines changed: 881 additions & 281 deletions

File tree

‎docs/AI_PERMISSIONS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ Legacy allowlists migrate to explicit connection decisions preserving exclusions
1515
Both applications pin the shared agent runtime to an exact Git revision in their Cargo manifests and lockfiles. Each application owns its permission policy and database-operation classification.
1616

1717
The application-owned `ai_permissions` module resolves policy, migrates legacy settings, classifies database payloads, and gates both MCP and built-in agent execution. The shared agent routes requests through an application preparation hook and carries its confirmation risk and context; it does not resolve this permission chain. Execution rechecks the application policy on every attempt.
18+
19+
AI schema reads use the same application gate as tool calls. Cached schema and editor context are included only when the associated connections permit reading. Manual UI capability invocations retain their existing access behavior.
20+
21+
Agent executors are bound to a conversation and its connection/source identities. Before dispatch they reload connection policy, current attachment restrictions, and confirmation rules. Detaching or deleting a source prevents subsequent dispatches. The sidebar uses its bound active connection without requiring a Data Studio attachment.
22+
23+
Preparation rejects denied calls before displaying confirmation. A queued call whose confirmation requirements become stricter returns to the existing approval flow. Permission denials are terminal and do not trigger transport retries; approval never bypasses a later denial. Rechecks occur at dispatch and do not cancel operations already dispatched or erase context already sent to a model.

‎src-tauri/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src-tauri/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ log = "0.4"
8888
futures = "0.3"
8989
rand = "0.8"
9090
# Pin the shared agent runtime to a reproducible revision.
91-
data-studio-agent = { git = "https://github.com/geek-fun/data-studio-agent.git", rev = "b1cfd5cdf7ffd269763de558cd9062e0e1e55895" }
91+
data-studio-agent = { git = "https://github.com/geek-fun/data-studio-agent.git", rev = "63f3bd2b98353f0e696a410f71fd42fe3a656931" }
9292

9393
# Archive extraction (JRE downloads)
9494
flate2 = "1.0"

‎src-tauri/src/agent/executor.rs‎

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_trait::async_trait;
22
use data_studio_agent::tool_executor::{
3-
ToolCallPreparation, ToolEnvelope, ToolExecutor, ToolResultMetadata,
3+
ToolCallPreparation, ToolEnvelope, ToolExecutionError, ToolExecutor, ToolResultMetadata,
44
};
55
use serde_json::Value;
66
use std::collections::HashMap;
@@ -16,7 +16,9 @@ fn char_truncate(input: &str, max_chars: usize) -> (String, bool) {
1616
(truncated, true)
1717
}
1818

19-
pub struct SqlKitToolExecutor;
19+
pub(crate) struct SqlKitToolExecutor {
20+
pub(crate) access: crate::ai_permissions::AgentAccess,
21+
}
2022

2123
#[async_trait]
2224
impl ToolExecutor for SqlKitToolExecutor {
@@ -25,9 +27,9 @@ impl ToolExecutor for SqlKitToolExecutor {
2527
tool_name: &str,
2628
arguments: &Value,
2729
config: &Value,
28-
connections: &HashMap<String, Value>,
30+
_connections: &HashMap<String, Value>,
2931
) -> Result<ToolCallPreparation, String> {
30-
crate::ai_permissions::prepare_agent_call(tool_name, arguments, config, connections)
32+
crate::ai_permissions::prepare_agent_call(&self.access, tool_name, arguments, config)
3133
}
3234

3335
async fn execute(
@@ -36,50 +38,25 @@ impl ToolExecutor for SqlKitToolExecutor {
3638
arguments: &Value,
3739
connection_config: &Value,
3840
) -> Result<ToolEnvelope, String> {
39-
let start = std::time::Instant::now();
40-
let policy =
41-
crate::ai_permissions::authorize_tool_call(tool_name, arguments, connection_config)?;
42-
43-
let conn_opt = if connection_config.is_null() {
44-
None
45-
} else {
46-
Some(connection_config.clone())
47-
};
41+
self.execute_checked(tool_name, arguments, connection_config)
42+
.await
43+
.map_err(|e| e.to_string())
44+
}
4845

49-
let raw = data_studio_agent::capabilities::registry::invoke_capability_inner(
46+
async fn execute_checked(
47+
&self,
48+
tool_name: &str,
49+
arguments: &Value,
50+
connection_config: &Value,
51+
) -> Result<ToolEnvelope, ToolExecutionError> {
52+
let start = std::time::Instant::now();
53+
let raw = crate::ai_permissions::invoke_ai(
5054
tool_name,
51-
arguments.clone(),
52-
conn_opt,
55+
arguments,
56+
connection_config,
57+
Some(&self.access),
5358
)
5459
.await?;
55-
56-
let raw = if tool_name.ends_with("__list_connections") {
57-
let mut result: Value = serde_json::from_str(&raw).map_err(|e| e.to_string())?;
58-
if let Some(connections) = result.get("connections").and_then(Value::as_array) {
59-
let filtered = connections
60-
.iter()
61-
.filter(|connection| {
62-
let id = match &connection["id"] {
63-
Value::String(id) => id.clone(),
64-
Value::Number(id) => id.to_string(),
65-
_ => return false,
66-
};
67-
let in_scope = connection_config
68-
.get("__allowed_connection_ids")
69-
.and_then(Value::as_array)
70-
.is_some_and(|ids| {
71-
ids.iter().any(|allowed| allowed.as_str() == Some(&id))
72-
});
73-
in_scope && policy.effective_permissions(&id).any_allowed()
74-
})
75-
.cloned()
76-
.collect::<Vec<_>>();
77-
result["connections"] = serde_json::json!(filtered);
78-
}
79-
result.to_string()
80-
} else {
81-
raw
82-
};
8360
let duration_ms = start.elapsed().as_millis() as u64;
8461
let (full_result, truncated) = char_truncate(&raw, TOOL_ENVELOPE_MAX_CHARS);
8562
let (summary, _) = char_truncate(&full_result, TOOL_ENVELOPE_SUMMARY_CHARS);

‎src-tauri/src/agent_adapters.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! to the shared library.
66
77
use std::collections::HashMap;
8-
use std::sync::Arc;
98

109
use data_studio_agent as lib;
1110
use data_studio_agent::storage;
@@ -48,9 +47,6 @@ pub async fn run_agent_loop(
4847
let confirm_map: ConfirmMap = confirm_state.inner().clone();
4948
let cancel_state: State<CancelMap> = app.state::<CancelMap>();
5049
let cancel_map: CancelMap = cancel_state.inner().clone();
51-
let executor_state: State<Arc<dyn lib::ToolExecutor>> =
52-
app.state::<Arc<dyn lib::ToolExecutor>>();
53-
let executor: Arc<dyn lib::ToolExecutor> = executor_state.inner().clone();
5450

5551
let connections: HashMap<String, Value> = settings
5652
.get("connections")
@@ -61,6 +57,20 @@ pub async fn run_agent_loop(
6157
.cloned()
6258
.unwrap_or(Value::Null);
6359

60+
let studio = match settings.get("contextKind").and_then(Value::as_str) {
61+
Some("dataStudio") => true,
62+
Some("sidebarAssistant") => false,
63+
_ => return Err("Unknown AI context".into()),
64+
};
65+
let executor = crate::agent::executor::SqlKitToolExecutor {
66+
access: crate::ai_permissions::AgentAccess {
67+
session_id: session_id.clone(),
68+
connections: connections.clone(),
69+
studio,
70+
db: db_state.inner().clone(),
71+
},
72+
};
73+
6474
let is_parallel_ok = |name: &str| -> bool {
6575
data_studio_agent::capabilities::registry::registry()
6676
.get(name)
@@ -77,7 +87,7 @@ pub async fn run_agent_loop(
7787
&settings,
7888
&store,
7989
&emitter,
80-
executor.as_ref(),
90+
&executor,
8191
connections,
8292
fallback,
8393
&confirm_map,

0 commit comments

Comments
 (0)