fix(ai): preserve Dameng database and schema scopes

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-02 09:01:22 +08:00
1 parent 4511920d86
commit 61dc49ca4d
16 files changed
+634 -76

No files matched your search

+35 -14
View File
@@ -76,6 +76,8 @@ pub struct AgentLoopContext {
pub state: Arc<AppState>,
pub connection_id: String,
pub database: String,
/// Selected schema that scopes Agent metadata and SQL execution.
pub schema: Option<String>,
pub db_type: DatabaseType,
pub cli_mcp_server_command: Option<CliAgentCommandSpec>,
pub sql_permissions: agent_tools::AgentSqlPermissions,
@@ -149,6 +151,7 @@ pub async fn run_agent_loop(
connection_id: agent_ctx.connection_id.clone(),
connection_name,
database: agent_ctx.database.clone(),
schema: agent_ctx.schema.clone(),
agent_mode: is_agent_mode,
allow_writes: agent_ctx.sql_permissions.allow_writes,
allow_dangerous: agent_ctx.sql_permissions.allow_dangerous,
@@ -435,6 +438,7 @@ pub async fn run_agent_loop(
let state2 = Arc::clone(&agent_ctx.state);
let conn2 = agent_ctx.connection_id.clone();
let db2 = agent_ctx.database.clone();
let schema2 = agent_ctx.schema.clone();
let db_type = agent_ctx.db_type;
let sql_permissions = agent_ctx.sql_permissions.clone();
@@ -452,25 +456,39 @@ pub async fn run_agent_loop(
};
// Run parallel group
let parallel_futures: Vec<_> = parallel_indices
.iter()
.map(|&i| {
let tc = make_tc(&collected_tool_calls[i]);
let state = Arc::clone(&state2);
let conn = conn2.clone();
let db = db2.clone();
let perms = sql_permissions.clone();
async move { agent_tools::execute_tool(&tc, &state, &conn, &db, &db_type, perms).await }
})
.collect();
let parallel_futures: Vec<_> =
parallel_indices
.iter()
.map(|&i| {
let tc = make_tc(&collected_tool_calls[i]);
let state = Arc::clone(&state2);
let conn = conn2.clone();
let db = db2.clone();
let schema = schema2.clone();
let perms = sql_permissions.clone();
async move {
agent_tools::execute_tool(&tc, &state, &conn, &db, schema.as_deref(), &db_type, perms).await
}
})
.collect();
let parallel_results = join_all(parallel_futures).await;
// Run sequential group one-by-one
let mut sequential_results = Vec::with_capacity(sequential_indices.len());
for &i in &sequential_indices {
let tc = make_tc(&collected_tool_calls[i]);
sequential_results
.push(agent_tools::execute_tool(&tc, &state2, &conn2, &db2, &db_type, sql_permissions.clone()).await);
sequential_results.push(
agent_tools::execute_tool(
&tc,
&state2,
&conn2,
&db2,
schema2.as_deref(),
&db_type,
sql_permissions.clone(),
)
.await,
);
}
// Merge results back into original order
@@ -866,7 +884,7 @@ async fn build_schema_prompt(agent_ctx: &AgentLoopContext, system_prompt: &str)
&agent_ctx.state,
&agent_ctx.connection_id,
&agent_ctx.database,
"",
agent_ctx.schema.as_deref().unwrap_or(""),
None,
Some(50), // smaller limit for prompt injection
None,
@@ -879,6 +897,9 @@ async fn build_schema_prompt(agent_ctx: &AgentLoopContext, system_prompt: &str)
Ok(tables) if !tables.is_empty() => {
enriched.push_str("\n\n## Database Schema (for context — no tools available)\n");
enriched.push_str(&format!("Database: {}\n", agent_ctx.database));
if let Some(schema) = agent_ctx.schema.as_deref() {
enriched.push_str(&format!("Schema: {schema}\n"));
}
enriched.push_str("Tables:\n");
for t in &tables {
enriched.push_str(&format!(" - {} ({})", t.name, t.table_type));
+246 -18
View File
@@ -66,22 +66,27 @@ pub fn verify_confirmed_target(
confirmed_write_sql: Option<String>,
confirmed_connection_id: Option<String>,
confirmed_database: Option<String>,
confirmed_schema: Option<String>,
actual_connection_id: &str,
actual_database: &str,
actual_schema: Option<&str>,
) -> (Option<bool>, Option<String>) {
let Some(ref confirmed_sql) = confirmed_write_sql else {
return (allow_write_sql, confirmed_write_sql);
};
// Only verify when a write SQL was actually confirmed.
let target_mismatch = confirmed_connection_id.as_deref() != Some(actual_connection_id)
|| confirmed_database.as_deref() != Some(actual_database);
|| confirmed_database.as_deref() != Some(actual_database)
|| confirmed_schema.as_deref() != actual_schema;
if target_mismatch {
log::warn!(
"Write-SQL grant voided: confirmed target (conn={:?}, db={:?}) does not match actual (conn={}, db={}).",
"Write-SQL grant voided: confirmed target (conn={:?}, db={:?}, schema={:?}) does not match actual (conn={}, db={}, schema={:?}).",
confirmed_connection_id,
confirmed_database,
confirmed_schema,
actual_connection_id,
actual_database,
actual_schema,
);
// SQL can contain literals or credentials. Keep diagnostic visibility
// behind the shared debug-only redaction boundary.
@@ -313,21 +318,25 @@ pub async fn execute_tool(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
db_type: &DatabaseType,
sql_permissions: AgentSqlPermissions,
) -> ToolResult {
let result = match tool_call.name.as_str() {
"list_tables" => execute_list_tables(tool_call, state, connection_id, database, db_type).await,
"get_columns" => execute_get_columns(tool_call, state, connection_id, database, db_type).await,
"list_tables" => execute_list_tables(tool_call, state, connection_id, database, default_schema, db_type).await,
"get_columns" => execute_get_columns(tool_call, state, connection_id, database, default_schema, db_type).await,
"execute_query" => {
execute_execute_query(tool_call, state, connection_id, database, db_type, sql_permissions).await
execute_execute_query(tool_call, state, connection_id, database, default_schema, db_type, sql_permissions)
.await
}
"get_sample_data" => {
execute_get_sample_data(tool_call, state, connection_id, database, default_schema, db_type).await
}
"get_sample_data" => execute_get_sample_data(tool_call, state, connection_id, database, db_type).await,
"list_collections" => execute_list_collections(tool_call, state, connection_id, database, db_type).await,
"browse_collection" => execute_browse_collection(tool_call, state, connection_id, database, db_type).await,
"explain_query" => {
let (text_result, explain_data) =
execute_explain_query(tool_call, state, connection_id, database, db_type).await;
execute_explain_query(tool_call, state, connection_id, database, default_schema, db_type).await;
match text_result {
Ok(content) => {
return ToolResult {
@@ -375,9 +384,10 @@ async fn execute_list_tables(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
_db_type: &DatabaseType,
) -> Result<String, String> {
let schema = tool_call.arguments.get("schema").and_then(|v| v.as_str()).unwrap_or("").to_string();
let schema = effective_schema(tool_call, default_schema).unwrap_or_default();
// Request one extra to detect whether more tables exist beyond the limit.
let tables = crate::schema::list_tables_core(
@@ -426,6 +436,7 @@ async fn execute_get_columns(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
_db_type: &DatabaseType,
) -> Result<String, String> {
let table = tool_call
@@ -447,7 +458,7 @@ async fn execute_get_columns(
return Err(format!("Table name contains invalid characters: '{}'", table));
}
let schema = tool_call.arguments.get("schema").and_then(|v| v.as_str()).unwrap_or("").to_string();
let schema = effective_schema(tool_call, default_schema).unwrap_or_default();
let columns = crate::schema::get_columns_core(state, connection_id, database, &schema, &table)
.await
@@ -529,6 +540,7 @@ async fn execute_execute_query(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
db_type: &DatabaseType,
sql_permissions: AgentSqlPermissions,
) -> Result<String, String> {
@@ -588,9 +600,16 @@ async fn execute_execute_query(
client_session_id: client_session_id.map(str::to_string),
..Default::default()
};
let result =
crate::query::execute_sql_statement_with_options(state, connection_id, database, sql, None, None, options)
.await?;
let result = crate::query::execute_sql_statement_with_options(
state,
connection_id,
database,
sql,
default_schema,
None,
options,
)
.await?;
format_query_result_as_text(&result, limit)
}
@@ -647,6 +666,7 @@ async fn execute_get_sample_data(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
db_type: &DatabaseType,
) -> Result<String, String> {
let table =
@@ -659,7 +679,7 @@ async fn execute_get_sample_data(
return Err(format!("Table name contains invalid characters: '{}'", table));
}
let schema = tool_call.arguments.get("schema").and_then(|v| v.as_str()).map(str::trim).filter(|s| !s.is_empty());
let schema = effective_schema(tool_call, default_schema);
let limit = tool_call
.arguments
.get("limit")
@@ -669,7 +689,7 @@ async fn execute_get_sample_data(
// Reuse the table-data builder so identifier quoting and row limiting follow
// the active database instead of assuming PostgreSQL syntax.
let sql = build_sample_data_sql(db_type, schema, table, limit);
let sql = build_sample_data_sql(db_type, schema.as_deref(), table, limit);
// Delegate to execute_execute_query with a synthetic tool call
let synthetic_call = ToolCall {
@@ -678,8 +698,16 @@ async fn execute_get_sample_data(
arguments: serde_json::json!({ "sql": sql, "limit": limit }),
provider_payload: None,
};
execute_execute_query(&synthetic_call, state, connection_id, database, db_type, AgentSqlPermissions::default())
.await
execute_execute_query(
&synthetic_call,
state,
connection_id,
database,
schema.as_deref(),
db_type,
AgentSqlPermissions::default(),
)
.await
}
fn build_sample_data_sql(db_type: &DatabaseType, schema: Option<&str>, table: &str, limit: usize) -> String {
@@ -699,6 +727,7 @@ async fn execute_explain_query(
state: &Arc<AppState>,
connection_id: &str,
database: &str,
default_schema: Option<&str>,
db_type: &DatabaseType,
) -> (Result<String, String>, Option<serde_json::Value>) {
let sql = match tool_call.arguments.get("sql").and_then(|v| v.as_str()) {
@@ -733,7 +762,7 @@ async fn execute_explain_query(
state,
connection_id,
Some(database),
None,
default_schema,
sql,
Some("explain"),
)
@@ -764,7 +793,7 @@ async fn execute_explain_query(
connection_id,
database,
&explain_sql,
None,
default_schema,
None,
options,
)
@@ -784,6 +813,20 @@ async fn execute_explain_query(
(Ok(text), explain_data)
}
/// A selected context schema is authoritative for an Agent run. If none was
/// selected, keep the existing per-tool schema parameter behavior.
fn effective_schema(tool_call: &ToolCall, default_schema: Option<&str>) -> Option<String> {
default_schema.map(str::trim).filter(|schema| !schema.is_empty()).map(ToOwned::to_owned).or_else(|| {
tool_call
.arguments
.get("schema")
.and_then(|value| value.as_str())
.map(str::trim)
.filter(|schema| !schema.is_empty())
.map(ToOwned::to_owned)
})
}
/// Execute list_collections tool (vector databases).
async fn execute_list_collections(
_tool_call: &ToolCall,
@@ -933,6 +976,114 @@ async fn resolve_chroma_collection_uuid(
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
use crate::connection::PoolKind;
#[cfg(unix)]
use crate::db::agent_driver::{AgentDriverClient, AgentLaunchSpec};
#[cfg(unix)]
use crate::models::connection::{default_redis_key_separator, ConnectionConfig};
#[cfg(unix)]
use crate::storage::Storage;
#[cfg(unix)]
async fn spawn_recording_agent(record_path: &std::path::Path) -> (AgentDriverClient, tempfile::NamedTempFile) {
use std::io::Write;
let mut script = tempfile::NamedTempFile::new().unwrap();
write!(
script,
r#"import json
import sys
record_path = sys.argv[1]
print(json.dumps({{"ready": True}}), flush=True)
for line in sys.stdin:
request = json.loads(line)
with open(record_path, "a", encoding="utf-8") as record:
record.write(json.dumps(request) + "\n")
result = {{
"columns": [],
"column_types": [],
"column_sortables": [],
"rows": [],
"affected_rows": 1,
"execution_time_ms": 0,
"truncated": False,
"session_id": None,
"has_more": False
}}
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
"#
)
.unwrap();
script.flush().unwrap();
let client = AgentDriverClient::spawn(
AgentLaunchSpec::new("python3")
.with_args([script.path().to_string_lossy().to_string(), record_path.to_string_lossy().to_string()]),
)
.await
.unwrap();
(client, script)
}
#[cfg(unix)]
fn dameng_test_connection() -> ConnectionConfig {
ConnectionConfig {
id: "dameng-1".to_string(),
name: "Dameng".to_string(),
note: String::new(),
db_type: DatabaseType::Dameng,
driver_profile: None,
driver_label: None,
url_params: None,
agent_java_options: Vec::new(),
host: "localhost".to_string(),
port: 5236,
username: "APP_USER".to_string(),
password: String::new(),
database: Some("APPDB".to_string()),
visible_databases: None,
visible_schemas: None,
show_system_schemas: false,
attached_databases: Vec::new(),
init_script: None,
color: None,
transport_layers: Vec::new(),
connect_timeout_secs: 10,
query_timeout_secs: 30,
idle_timeout_secs: 60,
keepalive_interval_secs: 30,
ssl: false,
ca_cert_path: String::new(),
client_cert_path: String::new(),
client_key_path: String::new(),
sysdba: false,
oracle_connection_type: None,
connection_string: None,
redis_connection_mode: None,
redis_sentinel_master: String::new(),
redis_sentinel_nodes: String::new(),
redis_sentinel_username: String::new(),
redis_sentinel_password: String::new(),
redis_sentinel_tls: false,
redis_cluster_nodes: String::new(),
redis_key_separator: default_redis_key_separator(),
redis_scan_page_size: None,
redis_database_aliases: Default::default(),
etcd_endpoints: String::new(),
gbase_server: String::new(),
informix_server: String::new(),
external_config: None,
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
read_only: false,
is_production: false,
production_databases: vec![],
database_info: None,
}
}
#[test]
fn vector_read_only_tools_do_not_include_collection_browsing() {
@@ -1004,6 +1155,83 @@ mod tests {
);
}
#[test]
fn selected_schema_overrides_the_tool_schema_argument() {
let call = ToolCall {
id: "call-1".to_string(),
name: "list_tables".to_string(),
arguments: serde_json::json!({ "schema": "OTHER" }),
provider_payload: None,
};
assert_eq!(effective_schema(&call, Some("REPORTING")).as_deref(), Some("REPORTING"));
assert_eq!(effective_schema(&call, None).as_deref(), Some("OTHER"));
}
#[cfg(unix)]
#[tokio::test]
async fn dameng_agent_queries_and_confirmed_writes_use_selected_schema() {
let temp_dir = tempfile::tempdir().unwrap();
let record_path = temp_dir.path().join("agent-requests.jsonl");
let (client, _script) = spawn_recording_agent(&record_path).await;
let storage = Storage::open(&temp_dir.path().join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new(storage));
let connection = dameng_test_connection();
state.configs.write().await.insert(connection.id.clone(), connection);
state.connections.write().await.insert("dameng-1:APPDB".to_string(), PoolKind::agent(client));
let read = ToolCall {
id: "read".to_string(),
name: "execute_query".to_string(),
arguments: json!({ "sql": "SELECT * FROM orders" }),
provider_payload: None,
};
let read_result = execute_tool(
&read,
&state,
"dameng-1",
"APPDB",
Some("REPORTING"),
&DatabaseType::Dameng,
AgentSqlPermissions::default(),
)
.await;
assert!(!read_result.is_error, "{}", read_result.content);
let confirmed_sql = "DELETE FROM orders WHERE id = 1";
let write = ToolCall {
id: "write".to_string(),
name: "execute_query".to_string(),
arguments: json!({ "sql": confirmed_sql }),
provider_payload: None,
};
let write_result = execute_tool(
&write,
&state,
"dameng-1",
"APPDB",
Some("REPORTING"),
&DatabaseType::Dameng,
confirmed_write_sql_permissions(false, true, Some(confirmed_sql.to_string())),
)
.await;
assert!(!write_result.is_error, "{}", write_result.content);
let requests = std::fs::read_to_string(&record_path)
.unwrap()
.lines()
.map(|line| serde_json::from_str::<serde_json::Value>(line).unwrap())
.filter(|request| request["method"] == "execute_query")
.collect::<Vec<_>>();
assert_eq!(requests.len(), 2);
assert_eq!(requests[0]["params"]["sql"], "SELECT * FROM orders");
assert_eq!(requests[1]["params"]["sql"], confirmed_sql);
for request in requests {
assert_eq!(request["params"]["database"], "APPDB");
assert_eq!(request["params"]["schema"], "REPORTING");
}
}
#[test]
fn build_browse_query_qdrant() {
let q = build_browse_query(&DatabaseType::Qdrant, "articles", "", 10).unwrap();
@@ -588,6 +588,7 @@ mod tests {
connection_id: "conn-1".to_string(),
connection_name: "local".to_string(),
database: "demo".to_string(),
schema: None,
agent_mode: true,
allow_writes: false,
allow_dangerous: false,
+10 -3
View File
@@ -14,6 +14,7 @@ pub struct CliAgentRunOptions {
pub connection_id: String,
pub connection_name: String,
pub database: String,
pub schema: Option<String>,
pub agent_mode: bool,
pub allow_writes: bool,
pub allow_dangerous: bool,
@@ -71,6 +72,9 @@ pub fn dbx_mcp_scope_env(options: &CliAgentRunOptions) -> Vec<(&'static str, Str
("DBX_MCP_SCOPE_CONNECTION_NAME", options.connection_name.clone()),
("DBX_MCP_SCOPE_DATABASE", options.database.clone()),
];
if let Some(schema) = options.schema.as_deref().filter(|schema| !schema.trim().is_empty()) {
env.push(("DBX_MCP_SCOPE_SCHEMA", schema.to_string()));
}
if let Some(ref sql) = options.confirmed_write_sql {
env.push(("DBX_MCP_CONFIRMED_WRITE_SQL", sql.clone()));
}
@@ -84,9 +88,10 @@ mod scope_env_tests {
#[test]
fn confirmed_write_sql_is_passed_to_the_scoped_mcp_subprocess() {
let options = CliAgentRunOptions {
connection_id: "connection-1".to_string(),
connection_name: "staging".to_string(),
database: "app".to_string(),
connection_id: "dameng-1".to_string(),
connection_name: "Dameng".to_string(),
database: "APPDB".to_string(),
schema: Some("REPORTING".to_string()),
agent_mode: true,
allow_writes: true,
allow_dangerous: true,
@@ -98,6 +103,8 @@ mod scope_env_tests {
assert!(env.contains(&("DBX_MCP_CONFIRMED_WRITE_SQL", "DELETE FROM sessions WHERE id = 7".to_string())));
assert!(env.contains(&("DBX_MCP_ALLOW_WRITES", "1".to_string())));
assert!(env.contains(&("DBX_MCP_ALLOW_DANGEROUS_SQL", "1".to_string())));
assert!(env.contains(&("DBX_MCP_SCOPE_DATABASE", "APPDB".to_string())));
assert!(env.contains(&("DBX_MCP_SCOPE_SCHEMA", "REPORTING".to_string())));
}
}
+1
View File
@@ -1045,6 +1045,7 @@ mod tests {
connection_id: "conn-1".to_string(),
connection_name: "local".to_string(),
database: "demo".to_string(),
schema: None,
agent_mode: true,
allow_writes: false,
allow_dangerous: false,
+2
View File
@@ -825,6 +825,7 @@ mod tests {
connection_id: "connection-1".to_string(),
connection_name: "Test connection".to_string(),
database: "dbx_test".to_string(),
schema: Some("reporting".to_string()),
agent_mode: true,
allow_writes: true,
allow_dangerous: false,
@@ -852,6 +853,7 @@ mod tests {
assert_eq!(env.get("DBX_MCP_SCOPE_CONNECTION_ID").map(String::as_str), Some("connection-1"));
assert_eq!(env.get("DBX_MCP_SCOPE_CONNECTION_NAME").map(String::as_str), Some("Test connection"));
assert_eq!(env.get("DBX_MCP_SCOPE_DATABASE").map(String::as_str), Some("dbx_test"));
assert_eq!(env.get("DBX_MCP_SCOPE_SCHEMA").map(String::as_str), Some("reporting"));
let enabled_tools = serde_json::from_str::<Vec<String>>(env.get("DBX_PI_ENABLED_TOOLS").unwrap()).unwrap();
assert!(enabled_tools.iter().any(|tool| tool == "dbx_execute_query"));