mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
agent: Prompt for always_confirm patterns on in-project file edits (#67037)
`edit_file` and `write_file` skip the permission prompt for files inside the project whenever the settings decision is `Confirm`. That is intended for a `confirm` default, where in-project edits are reviewed after the agent makes them. It also swallowed explicit `always_confirm` patterns, which the docs say always prompt. - `authorize_file_edit` now prompts when an `always_confirm` pattern matches the path. It checks both the raw path and its normalized form, so a `..` segment can't sidestep a pattern. - A `confirm` default is unchanged: in-project edits still don't prompt. - The docs, `default.json` and the `ToolPermissionMode::Confirm` doc comment described `confirm` as "always prompt". They now describe what the edit tools do with a `confirm` default and point to `always_confirm` for prompting on specific files. Related to #62276. This is an alternative to #66684, which would prompt on every in-project edit under the default settings. Release Notes: - Fixed `always_confirm` patterns for the `edit_file` and `write_file` agent tools not prompting for files inside the project.
This commit is contained in:
@@ -1262,7 +1262,9 @@
|
||||
// Global default permission when no tool-specific rules match.
|
||||
// "allow" - Auto-approve without prompting
|
||||
// "deny" - Auto-reject
|
||||
// "confirm" - Always prompt (default)
|
||||
// "confirm" - Prompt for approval (default). The edit_file and
|
||||
// write_file tools don't prompt for files inside the project unless
|
||||
// an "always_confirm" pattern matches.
|
||||
"default": "confirm",
|
||||
// Per-tool permission rules. Regex patterns match against tool input text.
|
||||
// The per-tool "default" also applies to MCP tools.
|
||||
|
||||
@@ -541,6 +541,26 @@ pub fn decide_permission_for_path(
|
||||
decide_permission_for_paths(tool_name, &[raw_path.to_string()], settings)
|
||||
}
|
||||
|
||||
/// Returns whether an `always_confirm` pattern for the tool matches the raw
|
||||
/// input path or its normalized form.
|
||||
///
|
||||
/// A `Confirm` decision doesn't say whether it came from such a pattern or
|
||||
/// from a `confirm` default, and the file edit tools treat the two differently.
|
||||
pub fn path_matches_always_confirm(
|
||||
tool_name: &str,
|
||||
raw_path: &str,
|
||||
settings: &AgentSettings,
|
||||
) -> bool {
|
||||
let Some(rules) = settings.tool_permissions.tools.get(tool_name) else {
|
||||
return false;
|
||||
};
|
||||
let normalized_path = normalize_path(raw_path);
|
||||
rules
|
||||
.always_confirm
|
||||
.iter()
|
||||
.any(|rule| rule.is_match(raw_path) || rule.is_match(&normalized_path))
|
||||
}
|
||||
|
||||
pub fn most_restrictive(
|
||||
a: ToolPermissionDecision,
|
||||
b: ToolPermissionDecision,
|
||||
|
||||
@@ -1627,6 +1627,57 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_streaming_authorize_always_confirm_in_project(cx: &mut TestAppContext) {
|
||||
let (edit_tool, _project, _action_log, _fs, _thread) = setup_test(
|
||||
cx,
|
||||
json!({"src": {"main.rs": ""}, "secrets": {"key.pem": ""}}),
|
||||
)
|
||||
.await;
|
||||
|
||||
for default in [
|
||||
settings::ToolPermissionMode::Confirm,
|
||||
settings::ToolPermissionMode::Allow,
|
||||
] {
|
||||
cx.update(|cx| {
|
||||
let mut settings = agent_settings::AgentSettings::get_global(cx).clone();
|
||||
settings.tool_permissions.default = default;
|
||||
settings.tool_permissions.tools.insert(
|
||||
EditFileTool::NAME.into(),
|
||||
agent_settings::ToolRules {
|
||||
always_confirm: vec![
|
||||
agent_settings::CompiledRegex::new("^root/secrets/", false).unwrap(),
|
||||
],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
agent_settings::AgentSettings::override_global(settings, cx);
|
||||
});
|
||||
|
||||
// The second path only matches the pattern once `..` is collapsed.
|
||||
for path in ["root/secrets/key.pem", "root/src/../secrets/key.pem"] {
|
||||
let (stream_tx, mut stream_rx) = ToolCallEventStream::test();
|
||||
let _auth =
|
||||
cx.update(|cx| edit_tool.authorize(&PathBuf::from(path), &stream_tx, cx));
|
||||
let event = stream_rx.expect_authorization().await;
|
||||
assert_eq!(
|
||||
event.tool_call.title,
|
||||
MaybeUndefined::Value(format!("Edit `{path}`")),
|
||||
"default: {default:?}"
|
||||
);
|
||||
}
|
||||
|
||||
let (stream_tx, mut stream_rx) = ToolCallEventStream::test();
|
||||
cx.update(|cx| edit_tool.authorize(&PathBuf::from("root/src/main.rs"), &stream_tx, cx))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
stream_rx.try_recv().is_err(),
|
||||
"a path that matches no pattern should not prompt (default: {default:?})"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// `.agents/foo/../skills/SKILL.md` would slip past the raw
|
||||
/// `is_agents_skills_path` check (the components `.agents` and
|
||||
/// `skills` aren't consecutive once `..` sits between them), but it
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Thread, ToolCallEventStream, ToolPermissionContext, ToolPermissionDecision,
|
||||
decide_permission_for_path,
|
||||
decide_permission_for_path, path_matches_always_confirm,
|
||||
};
|
||||
use agent_client_protocol::v2::schema as acp;
|
||||
use agent_skills::is_agents_skills_path;
|
||||
@@ -624,6 +624,13 @@ pub fn collect_symlink_escapes<'a>(
|
||||
/// two sequential prompts for the same operation would degrade UX without
|
||||
/// meaningfully improving security, since the user must already approve the
|
||||
/// more specific symlink-escape prompt.
|
||||
///
|
||||
/// # Edits inside the project
|
||||
///
|
||||
/// A `confirm` default deliberately doesn't prompt for edits inside the
|
||||
/// project, because those are reviewed after the fact. An `always_confirm`
|
||||
/// pattern is an explicit request to prompt for a path, so it prompts inside
|
||||
/// the project too.
|
||||
pub fn authorize_file_edit(
|
||||
tool_name: &str,
|
||||
path: &Path,
|
||||
@@ -640,6 +647,8 @@ pub fn authorize_file_edit(
|
||||
return Task::ready(Err(anyhow!("{}", reason)));
|
||||
}
|
||||
|
||||
let matches_always_confirm = path_matches_always_confirm(tool_name, &path_str, settings);
|
||||
|
||||
let path_owned = path.to_path_buf();
|
||||
let title = format!("Edit {}", util::markdown::MarkdownInlineCode(&path_str));
|
||||
let tool_name = tool_name.to_string();
|
||||
@@ -776,6 +785,20 @@ pub fn authorize_file_edit(
|
||||
None => {}
|
||||
}
|
||||
|
||||
// Settings are not consulted again here because they are only
|
||||
// evaluated against the raw path, which can miss a pattern that
|
||||
// matched the normalized one.
|
||||
if matches_always_confirm {
|
||||
let authorize = cx.update(|cx| {
|
||||
let context = ToolPermissionContext::new(
|
||||
&tool_name,
|
||||
vec![path_owned.to_string_lossy().to_string()],
|
||||
);
|
||||
event_stream.authorize_always_prompt(&title, context, cx)
|
||||
});
|
||||
return authorize.await;
|
||||
}
|
||||
|
||||
match resolved {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => {
|
||||
|
||||
@@ -401,6 +401,71 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_streaming_write_always_confirm_in_project(cx: &mut TestAppContext) {
|
||||
let (write_tool, _project, _action_log, fs, _thread) =
|
||||
setup_test(cx, json!({"secrets": {"key.pem": "old"}})).await;
|
||||
cx.update(|cx| {
|
||||
let mut settings = agent_settings::AgentSettings::get_global(cx).clone();
|
||||
settings.tool_permissions.tools.insert(
|
||||
WriteFileTool::NAME.into(),
|
||||
agent_settings::ToolRules {
|
||||
always_confirm: vec![
|
||||
agent_settings::CompiledRegex::new("^root/secrets/", false).unwrap(),
|
||||
],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
agent_settings::AgentSettings::override_global(settings, cx);
|
||||
});
|
||||
|
||||
let (event_stream, mut event_rx) = ToolCallEventStream::test();
|
||||
let task = cx.update(|cx| {
|
||||
write_tool.clone().run(
|
||||
ToolInput::resolved(WriteFileToolInput {
|
||||
path: "root/secrets/key.pem".into(),
|
||||
content: "new".into(),
|
||||
}),
|
||||
event_stream,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
event_rx.expect_update_fields().await;
|
||||
let auth = event_rx.expect_authorization().await;
|
||||
assert_eq!(
|
||||
auth.tool_call.title.value().map(String::as_str),
|
||||
Some("Edit `root/secrets/key.pem`")
|
||||
);
|
||||
|
||||
cx.run_until_parked();
|
||||
assert_eq!(
|
||||
fs.load(path!("/root/secrets/key.pem").as_ref())
|
||||
.await
|
||||
.unwrap(),
|
||||
"old",
|
||||
"the file must not be written before the prompt is answered"
|
||||
);
|
||||
|
||||
auth.response
|
||||
.send(acp_thread::SelectedPermissionOutcome::new(
|
||||
acp::PermissionOptionId::new("allow"),
|
||||
acp::PermissionOptionKind::AllowOnce,
|
||||
))
|
||||
.expect("authorization response should send");
|
||||
|
||||
let EditSessionOutput::Success { new_text, .. } = task.await.unwrap() else {
|
||||
panic!("expected success");
|
||||
};
|
||||
assert_eq!(new_text, "new");
|
||||
assert_eq!(
|
||||
fs.load(path!("/root/secrets/key.pem").as_ref())
|
||||
.await
|
||||
.unwrap(),
|
||||
"new"
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_streaming_path_completeness_heuristic(cx: &mut TestAppContext) {
|
||||
let (write_tool, _project, _action_log, _fs, _thread) =
|
||||
|
||||
@@ -1144,7 +1144,9 @@ pub enum ToolPermissionMode {
|
||||
Allow,
|
||||
/// Auto-reject with an error.
|
||||
Deny,
|
||||
/// Always prompt for confirmation (default behavior).
|
||||
/// Prompt for confirmation (default behavior).
|
||||
/// The `edit_file` and `write_file` tools don't prompt for files inside
|
||||
/// the project unless an `always_confirm` pattern matches.
|
||||
#[default]
|
||||
Confirm,
|
||||
}
|
||||
|
||||
@@ -90,6 +90,9 @@ For model-invoked [Skills](./skills.md), use the `skill` tool. A user-invoked `/
|
||||
| `always_deny` | Patterns that block immediately—highest priority, cannot be overridden |
|
||||
| `always_confirm` | Patterns that always prompt, even when `tool_permissions.default` is `"allow"` |
|
||||
|
||||
For `edit_file` and `write_file`, a `"confirm"` default does not prompt for files inside the project.
|
||||
See [File Edits Inside the Project](#file-edits-inside-the-project).
|
||||
|
||||
### Pattern Syntax
|
||||
|
||||
```json [settings]
|
||||
@@ -141,6 +144,41 @@ To auto-approve all tool actions:
|
||||
|
||||
This bypasses confirmation prompts for most tools, but `always_deny`, `always_confirm`, built-in security rules, and paths inside Zed settings directories still prompt or block.
|
||||
|
||||
## File Edits Inside the Project {#file-edits-inside-the-project}
|
||||
|
||||
`edit_file` and `write_file` handle a `"confirm"` default differently from other tools.
|
||||
When no pattern matches and the fallback is `"confirm"`, the agent edits files inside your project without prompting.
|
||||
This applies to both the global `default` and the tool's own `default`.
|
||||
You [review the changes](./agent-panel.md#reviewing-changes) after the agent makes them.
|
||||
|
||||
A `"confirm"` default still prompts before the agent edits:
|
||||
|
||||
- Files inside Zed settings folders (`.zed/` and the global config folder)
|
||||
- Agent skill files (`.agents/skills/`)
|
||||
- Files that a symlink places outside the project
|
||||
|
||||
To get a prompt before the agent edits specific project files, add `always_confirm` patterns for them.
|
||||
A pattern that matches every path, such as `.*`, prompts before every edit.
|
||||
|
||||
```json [settings]
|
||||
{
|
||||
"agent": {
|
||||
"tool_permissions": {
|
||||
"tools": {
|
||||
"edit_file": {
|
||||
"always_confirm": [{ "pattern": "\\.github/workflows/" }]
|
||||
},
|
||||
"write_file": {
|
||||
"always_confirm": [{ "pattern": "\\.github/workflows/" }]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The prompt shows the file path, not the proposed change.
|
||||
|
||||
## Shell Compatibility
|
||||
|
||||
For the `terminal` tool, Zed parses chained commands (e.g., `echo hello && rm file`) to check each sub-command against your patterns.
|
||||
@@ -230,11 +268,7 @@ MCP tools only support the tool-level option.
|
||||
"tool_permissions": {
|
||||
"tools": {
|
||||
"edit_file": {
|
||||
"default": "confirm",
|
||||
"always_allow": [
|
||||
{ "pattern": "\\.(md|txt|json)$" },
|
||||
{ "pattern": "^src/" }
|
||||
],
|
||||
"always_confirm": [{ "pattern": "\\.github/workflows/" }],
|
||||
"always_deny": [
|
||||
{ "pattern": "\\.env" },
|
||||
{ "pattern": "secrets?/" },
|
||||
|
||||
Reference in New Issue
Block a user