Skip to content

Commit 0a1983c

Browse files
committed
refactor: own AI permission policy and enforcement in the application
1 parent 5fd79ce commit 0a1983c

10 files changed

Lines changed: 1603 additions & 174 deletions

File tree

‎docs/AI_PERMISSIONS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ Read & modify data includes data deletion but excludes structure and administrat
1212

1313
Legacy allowlists migrate to explicit connection decisions preserving exclusions. When an allowlist existed, new connections default to no access. Existing per-connection restrictions are preserved. Corrupt or unreadable policy files deny execution rather than falling back to permissive settings. Policy writes are atomic and take effect for subsequent calls without restarting the bridge.
1414

15-
Both applications pin the shared permission implementation to an exact Git revision in their Cargo manifests and lockfiles so builds use the same policy behavior.
15+
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.
16+
17+
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.

‎src-tauri/Cargo.lock‎

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

‎src-tauri/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ http = "1"
8787
log = "0.4"
8888
futures = "0.3"
8989
rand = "0.8"
90-
# Pin the shared AI permission chain to a reproducible revision.
91-
data-studio-agent = { git = "https://github.com/geek-fun/data-studio-agent.git", rev = "c36fb2b16a4031053c3990dcd4d4b8e84d4d597f" }
90+
# 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" }
9292

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

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use async_trait::async_trait;
2-
use data_studio_agent::tool_executor::{ToolEnvelope, ToolExecutor, ToolResultMetadata};
2+
use data_studio_agent::tool_executor::{
3+
ToolCallPreparation, ToolEnvelope, ToolExecutor, ToolResultMetadata,
4+
};
35
use serde_json::Value;
6+
use std::collections::HashMap;
47

58
const TOOL_ENVELOPE_MAX_CHARS: usize = 32768;
69
const TOOL_ENVELOPE_SUMMARY_CHARS: usize = 4096;
@@ -17,32 +20,25 @@ pub struct SqlKitToolExecutor;
1720

1821
#[async_trait]
1922
impl ToolExecutor for SqlKitToolExecutor {
23+
fn prepare(
24+
&self,
25+
tool_name: &str,
26+
arguments: &Value,
27+
config: &Value,
28+
connections: &HashMap<String, Value>,
29+
) -> Result<ToolCallPreparation, String> {
30+
crate::ai_permissions::prepare_agent_call(tool_name, arguments, config, connections)
31+
}
32+
2033
async fn execute(
2134
&self,
2235
tool_name: &str,
2336
arguments: &Value,
2437
connection_config: &Value,
2538
) -> Result<ToolEnvelope, String> {
2639
let start = std::time::Instant::now();
27-
let app = crate::APP_HANDLE.get().ok_or("App not initialized")?;
28-
let policy = crate::mcp_bridge::load_permission_policy(app)?;
29-
let cap = data_studio_agent::capabilities::registry::registry()
30-
.get(tool_name)
31-
.ok_or("Unknown capability")?;
32-
let id = connection_config
33-
.get("connectionId")
34-
.and_then(Value::as_str);
35-
let restriction = connection_config
36-
.get("permissions")
37-
.filter(|v| !v.is_null())
38-
.map(|v| {
39-
serde_json::from_value::<
40-
data_studio_agent::capabilities::permissions::PermissionRules,
41-
>(v.clone())
42-
})
43-
.transpose()
44-
.map_err(|e| e.to_string())?;
45-
policy.authorize(cap, arguments, id, restriction.as_ref())?;
40+
let policy =
41+
crate::ai_permissions::authorize_tool_call(tool_name, arguments, connection_config)?;
4642

4743
let conn_opt = if connection_config.is_null() {
4844
None

‎src-tauri/src/agent_adapters.rs‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,8 @@ pub async fn run_agent_loop(
6868
.unwrap_or(false)
6969
};
7070

71-
let policy_app = app.clone();
72-
let permission_connections = connections.clone();
73-
let should_deny = move |tool_name: &str, conn_id: Option<&str>| -> bool {
74-
let Some(cap) = data_studio_agent::capabilities::registry::registry().get(tool_name) else {
75-
return true;
76-
};
77-
if matches!(
78-
cap.source_kind,
79-
data_studio_agent::capabilities::types::SourceKind::AppLocal
80-
| data_studio_agent::capabilities::types::SourceKind::File
81-
) {
82-
return false;
83-
}
84-
let Some(id) = conn_id
85-
.and_then(|id| permission_connections.get(id))
86-
.and_then(|cfg| cfg.get("connectionId"))
87-
.and_then(Value::as_str)
88-
else {
89-
return true;
90-
};
91-
crate::mcp_bridge::load_permission_policy(&policy_app)
92-
.map(|policy| !policy.effective_permissions(id).any_allowed())
93-
.unwrap_or(true)
94-
};
71+
// The executor owns authorization before confirmation and again before execution.
72+
let legacy_deny_hook = |_: &str, _: Option<&str>| false;
9573

9674
lib::loop_runner::run_agent_loop(
9775
&session_id,
@@ -105,7 +83,7 @@ pub async fn run_agent_loop(
10583
&confirm_map,
10684
&cancel_map,
10785
&is_parallel_ok,
108-
&should_deny,
86+
&legacy_deny_hook,
10987
)
11088
.await
11189
}

0 commit comments

Comments
 (0)