diff --git a/crates/ide-agent/src/config.rs b/crates/ide-agent/src/config.rs index d97d1e15f8..08e3178e8d 100644 --- a/crates/ide-agent/src/config.rs +++ b/crates/ide-agent/src/config.rs @@ -2,8 +2,8 @@ //! the agent reports them over ACP. use crate::model::{ - AgentCommand, AgentCommandSource, AgentConfigOption, AgentConfigValue, AgentConfigVia, AgentCost, AgentLimit, - AgentModelPricing, AgentUsage, + AgentCommand, AgentCommandSource, AgentConfigChoice, AgentConfigOption, AgentConfigValue, AgentConfigVia, + AgentCost, AgentLimit, AgentModelPricing, AgentUsage, }; use serde_json::{Value, json}; @@ -54,6 +54,30 @@ pub(crate) fn bypass_approvals(config: &[AgentConfigOption]) -> bool { .any(|category| selected(category).zip(bypass_value(category)).is_some_and(|(value, bypass)| bypass(value))) } +/// Whether `value` of `option` approves everything without asking. +pub(crate) fn approves_everything(option: &AgentConfigOption, value: &str) -> bool { + option.category.as_deref().and_then(bypass_value).is_some_and(|bypass| bypass(value)) +} + +/// The choices that keep a session asking before it changes anything: `wanted` without values +/// that approve everything, and an asking value for each option of `options` that approves +/// everything as it stands. +pub(crate) fn asking_choices(options: &[AgentConfigOption], wanted: &[AgentConfigChoice]) -> Vec { + let mut out: Vec = wanted + .iter() + .filter(|c| !options.iter().any(|o| o.id == c.id && approves_everything(o, &c.value))) + .cloned() + .collect(); + let mut asking = options.to_vec(); + ask_for_approvals(&mut asking); + for (before, after) in options.iter().zip(&asking) { + let Some(value) = after.current.as_ref().filter(|v| before.current.as_ref() != Some(*v)) else { continue }; + out.retain(|c| c.id != after.id); + out.push(AgentConfigChoice { id: after.id.clone(), value: value.clone() }); + } + out +} + /// For an option of `category` that can approve everything, what tells its approving values. fn bypass_value(category: &str) -> Option bool> { match category { @@ -464,6 +488,22 @@ mod tests { assert_eq!(ids(&c), [("allow_all", Some("off")), ("approval", Some("ask"))]); } + #[test] + fn asking_choices_drop_and_override_values_that_approve_everything() { + let codex = options_of(&json!({"modes": {"currentModeId": "full-access", + "availableModes": [{"id": "read-only"}, {"id": "ask"}, {"id": "auto"}, {"id": "full-access"}]}})); + let mode = codex[0].id.clone(); + let choice = |id: &str, value: &str| AgentConfigChoice { id: id.into(), value: value.into() }; + // The source task's Full Access goes; the session's own Full Access is turned to Ask. + let wanted = asking_choices(&codex, &[choice(&mode, "full-access"), choice("model", "gpt")]); + assert_eq!(wanted, [choice("model", "gpt"), choice(&mode, "ask")]); + // A mode that already asks is left alone. + let mut auto = codex.clone(); + auto[0].current = Some("auto".into()); + assert_eq!(asking_choices(&auto, &[choice(&mode, "auto")]), [choice(&mode, "auto")]); + assert!(approves_everything(&codex[0], "full-access") && !approves_everything(&codex[0], "auto")); + } + #[test] fn effort_and_fast_categories_are_inferred_from_ids() { let mut c = options_of(&json!({ diff --git a/crates/ide-agent/src/lib.rs b/crates/ide-agent/src/lib.rs index 4501efe196..2fd2a8ee93 100644 --- a/crates/ide-agent/src/lib.rs +++ b/crates/ide-agent/src/lib.rs @@ -891,6 +891,8 @@ impl AgentManager { let prompt = clean(req.prompt).ok_or_else(|| IdeError::invalid("describe the topic first"))?; let title = title_of(&prompt); let mut config = self.merged_config(&spec.id, &req.config); + // The source task's choices may approve everything; the side conversation asks. + let wanted = config::asking_choices(&config, &req.config); config::ask_for_approvals(&mut config); if config::bypass_approvals(&config) { return Err(IdeError::invalid( @@ -933,7 +935,7 @@ impl AgentManager { task: task.clone(), session_id: None, entries: Vec::new(), - wanted: req.config, + wanted, before, saved: None, touched: None, @@ -1050,6 +1052,9 @@ impl AgentManager { if !o.values.iter().any(|v| v.value == value) { return Err(IdeError::invalid(format!("{} has no value {value}", o.name))); } + if s.ephemeral && config::approves_everything(o, &value) { + return Err(IdeError::invalid("a side conversation asks before changing anything")); + } o.clone() }; let previous = target.current.clone(); @@ -2365,7 +2370,10 @@ impl Task { } let (mut options, mut wanted) = { let s = self.state.lock(); - (if known { reported } else { s.task.config.clone() }, s.wanted.clone()) + let options = if known { reported } else { s.task.config.clone() }; + // A side conversation asks before anything, whatever mode the agent starts in. + let wanted = if s.ephemeral { config::asking_choices(&options, &s.wanted) } else { s.wanted.clone() }; + (options, wanted) }; config::add_client_options(provider_id, &mut options); // Changing model can replace the effort / context-tier choices; validate those only afterwards.