дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
A tool call needing confirmation in a project served in the background (build_project, execute_run_configuration, http_run_request, destructive plugin tools) failed with "no project window is available" unless brave mode was on. It now asks in the trusted project window focused last, with the usual dialog naming the background project and its path. Requests queue there; the dialog keeps the keyboard focus where it is and answers no key typed outside it, and the Dock icon bounces when the app isn't active. When that window closes, switches project, reloads or loses trust, the request moves to the next trusted project window, within the same 180 s deadline. Tools whose result needs a window (run_command, open_file_in_editor, xdebug_start_debugger_session and the breakpoint tools) refuse in a background project before asking. Closes #7035 Co-authored-by: lleviathe <[email protected]> Co-authored-by: lleviathe <[email protected]>
358 рядки
17 KiB
Rust
358 рядки
17 KiB
Rust
//! Database and MCP calls through a real sandboxed API 1.4 component.
|
|
mod common;
|
|
|
|
use ide_core::{CancellationToken, IdeEvent, event::McpUiRequest};
|
|
use ide_host::{
|
|
Host, HostConfig,
|
|
mcp::Registry,
|
|
ops,
|
|
plugins::{Consent, HostWorkspace, PluginEvent, PluginHost, WindowRequest},
|
|
};
|
|
use ide_mcp::{ToolCallError, ToolRegistry};
|
|
use ide_plugin::{Invocation, Limits};
|
|
use std::{
|
|
path::{Path, PathBuf},
|
|
sync::{Arc, Mutex},
|
|
time::Duration,
|
|
};
|
|
const ID: &str = "test.data-tools";
|
|
fn fixture() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/data-tools")
|
|
}
|
|
struct Harness {
|
|
host: Arc<Host>,
|
|
plugins: Arc<PluginHost>,
|
|
root: PathBuf,
|
|
events: Arc<Mutex<Vec<(String, IdeEvent)>>>,
|
|
requests: Arc<Mutex<Vec<(String, PluginEvent)>>>,
|
|
_tmp: tempfile::TempDir,
|
|
}
|
|
impl Harness {
|
|
fn new() -> Self {
|
|
Self::with_timeout(Duration::from_millis(250))
|
|
}
|
|
fn with_timeout(timeout: Duration) -> Self {
|
|
ide_ssh::secrets::testing::use_memory_store();
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
|
|
std::fs::create_dir(&root).unwrap();
|
|
let events = Arc::new(Mutex::new(vec![]));
|
|
let out = events.clone();
|
|
let host = Arc::new(Host::new(HostConfig {
|
|
sink_for: Box::new(move |w| {
|
|
let (w, out) = (w.to_owned(), out.clone());
|
|
Arc::new(move |e| out.lock().unwrap().push((w.clone(), e)))
|
|
}),
|
|
local_data_dir: tmp.path().join("local"),
|
|
..common::host_config(tmp.path())
|
|
}));
|
|
std::fs::write(root.join("db.sqlite"), "").unwrap();
|
|
host.open_project("main", &root).unwrap();
|
|
host.open_project("second", &root).unwrap();
|
|
let requests = Arc::new(Mutex::new(vec![]));
|
|
let out = requests.clone();
|
|
let plugins = Arc::new(
|
|
PluginHost::new(
|
|
tmp.path().join("plugins"),
|
|
Arc::new(move |w: Option<&str>, e| out.lock().unwrap().push((w.unwrap_or_default().to_owned(), e))),
|
|
Limits { timeout, ..Default::default() },
|
|
)
|
|
.unwrap(),
|
|
);
|
|
plugins.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
|
|
host.set_plugin_host(&plugins);
|
|
plugins.install_from_dir(&fixture(), &Consent::all()).unwrap();
|
|
Self { host, plugins, root, events, requests, _tmp: tmp }
|
|
}
|
|
fn invocation(&self) -> Invocation {
|
|
Invocation { client: Some("main".into()), workspace_root: Some(self.root.clone()), ..Default::default() }
|
|
}
|
|
fn run(&self, id: &str, arg: Option<&str>) -> ide_core::IdeResult<Option<String>> {
|
|
self.plugins.run_command(ID, id, arg, self.invocation())
|
|
}
|
|
/// The folder `inventory` next to the project, listed to be served without a window.
|
|
fn serve_inventory(&self) -> PathBuf {
|
|
let inventory = self.root.parent().unwrap().join("inventory");
|
|
std::fs::create_dir(&inventory).unwrap();
|
|
for (key, value) in [
|
|
("enabled", serde_json::json!(true)),
|
|
("backgroundProjects", serde_json::json!("listed")),
|
|
("backgroundProjectPaths", serde_json::json!([inventory])),
|
|
] {
|
|
let key = format!("mcpServer.{key}");
|
|
self.host.global_settings.set(ide_workspace::SettingsScope::Global, &key, value).unwrap();
|
|
}
|
|
inventory
|
|
}
|
|
fn source(&self) -> String {
|
|
serde_json::json!({"id":"local","name":"Example","driver":"sqlite","database":self.root.join("db.sqlite").to_string_lossy(),"options":[]}).to_string()
|
|
}
|
|
}
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn database_calls_enforce_grants_trust_ownership_and_uninstall_lifetime() {
|
|
// Nothing here tests the command budget, and a slow runner's first calls can exceed 250 ms.
|
|
let f = Harness::with_timeout(Duration::from_secs(10));
|
|
assert!(f.run("sources", None).is_err());
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
let source = f.source();
|
|
f.run("upsert", Some(&source)).unwrap();
|
|
assert_eq!(f.run("sources", None).unwrap().as_deref(), Some("[\"local\"]"));
|
|
f.run("credential", Some("never-on-disk")).unwrap();
|
|
let source = f.host.project("main").unwrap().db.plugin_source(ID, "local").unwrap();
|
|
let store = ide_db::secrets::SecretStore::new(f._tmp.path().join("data/db-passwords.json"), &f.root);
|
|
assert_eq!(store.get(&source.id).as_deref(), Some("never-on-disk"));
|
|
let file = f.root.join(".openidea/data-sources.toml");
|
|
assert!(!std::fs::read_to_string(&file).unwrap().contains("never-on-disk"));
|
|
for command in ["remove", "connect"] {
|
|
assert!(f.run(command, Some("foreign")).is_err());
|
|
}
|
|
assert!(f.events.lock().unwrap().iter().any(|(w, e)| w == "second" && matches!(e, IdeEvent::DbSourcesChanged)));
|
|
f.plugins.set_enabled(ID, false).unwrap();
|
|
assert!(file.exists());
|
|
f.plugins.set_enabled(ID, true).unwrap();
|
|
assert!(f.host.project("main").unwrap().db.plugin_source(ID, "local").is_ok());
|
|
f.host.set_mcp_executable(Some(PathBuf::from("/app/openidea")));
|
|
let command: serde_json::Value = serde_json::from_str(&f.run("stdio", None).unwrap().unwrap()).unwrap();
|
|
assert_eq!(command[1], "--mcp-stdio");
|
|
f.host.set_mcp_executable(None);
|
|
assert_eq!(f.run("stdio", None).unwrap().as_deref(), Some("null"));
|
|
// Closed project cleanup uses the host-owned journal, not only current windows.
|
|
f.host.close_project("main");
|
|
f.host.close_project("second");
|
|
f.plugins.uninstall(ID).unwrap();
|
|
assert!(ide_db::source::load(&f.root).unwrap().is_empty());
|
|
assert!(store.get(&source.id).is_none());
|
|
}
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn database_connect_asks_only_the_invoking_window_and_uses_normal_connection_flow() {
|
|
let f = Harness::new();
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
f.run("upsert", Some(&f.source())).unwrap();
|
|
let (plugins, invocation) = (f.plugins.clone(), f.invocation());
|
|
let run = tokio::task::spawn_blocking(move || plugins.run_command(ID, "connect", None, invocation));
|
|
let (window, request, source) = tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
let found = f.requests.lock().unwrap().iter().find_map(|(w, e)| match e {
|
|
PluginEvent::WindowRequest { request_id, request: WindowRequest::DatabaseConnect { id }, .. } => {
|
|
Some((w.clone(), request_id.clone(), id.clone()))
|
|
}
|
|
_ => None,
|
|
});
|
|
if let Some(v) = found {
|
|
break v;
|
|
}
|
|
tokio::task::yield_now().await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(window, "main");
|
|
assert!(!f.plugins.answer_command("second", &request, ide_host::plugins::CommandAnswer::Value(None)));
|
|
tokio::time::sleep(Duration::from_millis(350)).await; // Password entry can outlive the 250 ms command budget.
|
|
ops::db::db_connect(&f.host, "main", source.to_string()).await.unwrap();
|
|
f.plugins.answer_command("main", &request, ide_host::plugins::CommandAnswer::Value(None));
|
|
run.await.unwrap().unwrap();
|
|
assert!(!f.host.project("main").unwrap().db.connected().is_empty());
|
|
}
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn mcp_listing_dispatch_confirmation_and_lifecycle_use_plugin_authority() {
|
|
let f = Harness::new();
|
|
let registry = Arc::new(Registry::new(f.host.clone()));
|
|
assert!(!registry.tools(&f.root).unwrap().iter().any(|t| t.name == "test_data_tools__write"));
|
|
assert!(registry.call(&f.root, "test_data_tools__write", "{}", CancellationToken::new()).is_err());
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
let tools = registry.tools(&f.root).unwrap();
|
|
let name = "test_data_tools__echo";
|
|
assert!(tools.iter().any(|t| t.name == name && t.description.contains(ID)));
|
|
assert!(matches!(
|
|
registry.call(&f.root, name, r#"{"text":12}"#, CancellationToken::new()),
|
|
Err(ToolCallError::Failed(_))
|
|
));
|
|
let result = registry.call(&f.root, name, r#"{"text":"hello"}"#, CancellationToken::new()).unwrap();
|
|
assert!(result.contains("hello"));
|
|
let denied = registry.call(&f.root, "test_data_tools__denied", "{}", CancellationToken::new()).unwrap_err();
|
|
assert!(format!("{denied:?}").contains("process:sh"));
|
|
f.host.mcp_requests.set_ready("main", true);
|
|
let (r, root) = (registry.clone(), f.root.clone());
|
|
let call =
|
|
tokio::task::spawn_blocking(move || r.call(&root, "test_data_tools__erase", "{}", CancellationToken::new()));
|
|
let request = tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
if let Some(id) = f.events.lock().unwrap().iter().find_map(|(_, e)| match e {
|
|
IdeEvent::McpRequest { id, request: McpUiRequest::Confirm { .. } } => Some(*id),
|
|
_ => None,
|
|
}) {
|
|
break id;
|
|
}
|
|
tokio::task::yield_now().await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
assert!(f.host.mcp_requests.answer("second", request, true, None).is_err());
|
|
f.host.mcp_requests.answer("main", request, false, None).unwrap();
|
|
assert!(call.await.unwrap().is_err());
|
|
f.plugins.set_enabled(ID, false).unwrap();
|
|
assert!(!registry.tools(&f.root).unwrap().iter().any(|t| t.name == name));
|
|
assert!(registry.call(&f.root, name, "{}", CancellationToken::new()).is_err());
|
|
f.plugins.set_enabled(ID, true).unwrap();
|
|
assert!(registry.tools(&f.root).unwrap().iter().any(|t| t.name == name));
|
|
let (r, root) = (registry.clone(), f.root.clone());
|
|
let spin =
|
|
tokio::task::spawn_blocking(move || r.call(&root, "test_data_tools__spin", "{}", CancellationToken::new()));
|
|
assert!(spin.await.unwrap().is_err(), "command budget interrupts wasm");
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn a_destructive_plugin_tool_of_a_background_project_asks_in_a_project_window() {
|
|
let f = Harness::with_timeout(Duration::from_secs(10));
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
let inventory = f.serve_inventory();
|
|
f.host.trust.decide(&inventory, true, false).unwrap();
|
|
f.host.mcp_requests.set_ready("main", true);
|
|
let (registry, root) = (Registry::new(f.host.clone()), inventory.clone());
|
|
let call = tokio::task::spawn_blocking(move || {
|
|
registry.call(&root, "test_data_tools__erase", "{}", CancellationToken::new())
|
|
});
|
|
let (window, id, background) = tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
if let Some(found) = f.events.lock().unwrap().iter().find_map(|(w, e)| match e {
|
|
IdeEvent::McpRequest { id, request: McpUiRequest::Confirm { background, .. } } => {
|
|
Some((w.clone(), *id, *background))
|
|
}
|
|
_ => None,
|
|
}) {
|
|
break found;
|
|
}
|
|
tokio::task::yield_now().await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
assert_eq!((window.as_str(), background), ("main", true));
|
|
f.host.mcp_requests.answer("main", id, false, None).unwrap();
|
|
let denied = call.await.unwrap().unwrap_err();
|
|
assert!(format!("{denied:?}").contains("denied by the user"), "{denied:?}");
|
|
f.host.close_project(&ide_host::mcp::BackgroundProjects::key(&inventory));
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn plugin_tools_take_the_project_path_and_refuse_an_untrusted_project_before_it_loads() {
|
|
let f = Harness::with_timeout(Duration::from_secs(10));
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
let inventory = f.serve_inventory();
|
|
let registry = Registry::new(f.host.clone());
|
|
// A connection without a project of its own lists the tool with `projectPath`, which its
|
|
// calls have to pass, and the plugin's strict schema still takes the call.
|
|
let listed = registry.tools_without_project();
|
|
let echo = listed.iter().find(|t| t.name == "test_data_tools__echo").unwrap();
|
|
assert!(echo.input_schema["properties"]["projectPath"].is_object(), "{}", echo.input_schema);
|
|
let arguments = serde_json::json!({ "text": "hello", "projectPath": f.root }).to_string();
|
|
let result = registry.call(&f.root, "test_data_tools__echo", &arguments, CancellationToken::new()).unwrap();
|
|
assert!(result.contains("hello"), "{result}");
|
|
// A tool that writes refuses the untrusted folder, and nothing loads.
|
|
let refused = registry.call(&inventory, "test_data_tools__write", "{}", CancellationToken::new()).unwrap_err();
|
|
let restricted = "Project is in Restricted Mode. Trust the project to enable execution.";
|
|
assert_eq!(refused, ToolCallError::Failed(restricted.into()));
|
|
assert!(f.host.mcp_background.snapshot(&f.host, std::time::Instant::now()).is_empty());
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn a_plugin_tool_call_holds_its_background_project_until_it_ends() {
|
|
use ide_host::mcp::{BackgroundProjects, McpBackgroundHold};
|
|
// The fixture's `spin` runs until it is cancelled or its budget ends.
|
|
let f = Harness::with_timeout(Duration::from_secs(10));
|
|
let inventory = f.serve_inventory();
|
|
let held_by = || {
|
|
let loaded = f.host.mcp_background.snapshot(&f.host, std::time::Instant::now());
|
|
loaded.into_iter().next().map(|project| project.kept_open_by)
|
|
};
|
|
let (registry, root, cancel) = (Registry::new(f.host.clone()), inventory.clone(), CancellationToken::new());
|
|
let stop = cancel.clone();
|
|
let call = tokio::task::spawn_blocking(move || registry.call(&root, "test_data_tools__spin", "{}", cancel));
|
|
tokio::time::timeout(Duration::from_secs(10), async {
|
|
while held_by() != Some(Some(McpBackgroundHold::Request)) {
|
|
tokio::task::yield_now().await;
|
|
}
|
|
})
|
|
.await
|
|
.expect("the call holds the project while the plugin runs");
|
|
stop.cancel();
|
|
assert!(call.await.unwrap().is_err());
|
|
assert_eq!(held_by(), Some(None), "the project stays loaded with nothing holding it");
|
|
f.host.close_project(&BackgroundProjects::key(&inventory));
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn database_imports_are_denied_without_the_optional_grant() {
|
|
let f = Harness::new();
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
f.plugins.set_capability(ID, "database.sources", false).unwrap();
|
|
for command in ["upsert", "remove", "sources", "credential", "connect"] {
|
|
let err = f.run(command, Some(&f.source())).unwrap_err();
|
|
assert!(err.message.contains("database.sources"), "{command}: {err}");
|
|
}
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn official_api_examples_install_and_render_real_component_contributions() {
|
|
// The real examples' first calls compile and scan; 250 ms is too tight on Windows runners.
|
|
let f = Harness::with_timeout(Duration::from_secs(10));
|
|
f.host.set_project_trust(&f.root, true, false).await.unwrap();
|
|
let official = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../plugins/official");
|
|
for name in ["blame-annotations", "todo-finder"] {
|
|
f.plugins.install_from_dir(&official.join(name), &Consent::all()).unwrap();
|
|
}
|
|
std::fs::write(f.root.join("notes.txt"), "TODO example\n").unwrap();
|
|
let doc = ide_plugin::ProviderDocument {
|
|
path: f.root.join("notes.txt"),
|
|
language: "text".into(),
|
|
version: 1,
|
|
text: "TODO example\n".into(),
|
|
};
|
|
assert!(eventually(|| {
|
|
f.plugins
|
|
.language_code_lenses(&doc)
|
|
.iter()
|
|
.any(|(owner, lens)| owner == "openidea.blame-annotations" && lens.command == "annotate")
|
|
}));
|
|
f.plugins.run_command("openidea.todo-finder", "find", None, f.invocation()).unwrap();
|
|
let view = f.plugins.render_view("openidea.todo-finder", "todos", None, f.invocation()).unwrap();
|
|
let ide_plugin::ViewBody::List(items) = view.body else { panic!("expected TODO list") };
|
|
assert_eq!(items.len(), 1);
|
|
assert!(items[0].label.contains("TODO example"));
|
|
#[cfg(unix)]
|
|
{
|
|
let path = std::env::join_paths([official.join("container-runner"), official.join("test-runner")]).unwrap();
|
|
f.plugins.set_program_path(Arc::new(move || Some(path.clone())));
|
|
for name in ["container-runner", "test-runner"] {
|
|
f.plugins.install_from_dir(&official.join(name), &Consent::all()).unwrap();
|
|
}
|
|
std::fs::write(f.root.join("example.tests"), "test adds\n").unwrap();
|
|
let doc =
|
|
ide_plugin::ProviderDocument { path: f.root.join("example.tests"), text: "test adds\n".into(), ..doc };
|
|
assert!(eventually(|| {
|
|
f.plugins
|
|
.language_gutter_actions(&doc, Some(&f.root))
|
|
.iter()
|
|
.any(|(owner, action)| owner == "openidea.test-runner" && action.argument.as_deref() == Some("adds"))
|
|
}));
|
|
f.plugins.refresh_run_configurations(&f.root);
|
|
let configs = f.plugins.run_detection().detect(&f.root);
|
|
assert!(configs.iter().any(|c| c.id == "plugin:openidea.test-runner:tests"));
|
|
}
|
|
}
|
|
|
|
/// Editor-path calls drop a result that misses their budget, as the first call into a cold
|
|
/// component can on a slow machine: ask again until it arrives.
|
|
fn eventually(mut ready: impl FnMut() -> bool) -> bool {
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
|
|
while std::time::Instant::now() < deadline {
|
|
if ready() {
|
|
return true;
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(100));
|
|
}
|
|
false
|
|
}
|