Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 401fe541a1 fix(agents): side conversations run in the agent's most careful mode 2026-11-16 19:09:13 +08:00
2 змінених файлів з 61 додано та 28 видалено

No files matched your search

+43 -24
Переглянути файл
@@ -54,30 +54,45 @@ 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))
/// Mode values that ask before anything changes the project, most careful first: Codex's
/// Read Only (its Ask still edits the workspace), Claude Code's default (its Accept Edits
/// doesn't ask), then plan modes.
const ASKING_MODES: [&str; 4] = ["read-only", "default", "plan", "ask"];
/// Moves `config` to settings that ask before anything changes the project (a side
/// conversation): approvals that approve everything ask, and the mode is the most careful one.
pub(crate) fn keep_asking(config: &mut [AgentConfigOption]) {
ask_for_approvals(config);
for option in config.iter_mut().filter(|o| o.category.as_deref() == Some("mode")) {
if let Some(mode) = ASKING_MODES.iter().find(|m| option.values.iter().any(|v| v.value == **m)) {
option.current = Some((*mode).to_owned());
}
}
}
/// 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.
/// The choices that keep a session asking before it changes anything: `wanted` without its
/// mode and approval choices, and the [`keep_asking`] value of each such option of `options`.
pub(crate) fn asking_choices(options: &[AgentConfigOption], wanted: &[AgentConfigChoice]) -> Vec<AgentConfigChoice> {
let mut out: Vec<AgentConfigChoice> = wanted
.iter()
.filter(|c| !options.iter().any(|o| o.id == c.id && approves_everything(o, &c.value)))
.cloned()
.collect();
let guarded = |o: &AgentConfigOption| o.category.as_deref().and_then(bypass_value).is_some();
let mut out: Vec<AgentConfigChoice> =
wanted.iter().filter(|c| !options.iter().any(|o| o.id == c.id && guarded(o))).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() });
keep_asking(&mut asking);
for option in asking.iter().filter(|o| guarded(o)) {
if let Some(value) = &option.current {
out.push(AgentConfigChoice { id: option.id.clone(), value: value.clone() });
}
}
out
}
/// Whether `config` is what [`keep_asking`] makes of it.
pub(crate) fn keeps_asking(config: &[AgentConfigOption]) -> bool {
let mut asking = config.to_vec();
keep_asking(&mut asking);
asking.iter().zip(config).all(|(a, b)| a.current == b.current)
}
/// For an option of `category` that can approve everything, what tells its approving values.
fn bypass_value(category: &str) -> Option<fn(&str) -> bool> {
match category {
@@ -489,19 +504,23 @@ mod tests {
}
#[test]
fn asking_choices_drop_and_override_values_that_approve_everything() {
fn asking_choices_keep_a_side_conversation_from_changing_anything() {
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.
// Codex goes to Read Only (its Ask edits the workspace), whatever the source task chose.
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"));
assert_eq!(wanted, [choice("model", "gpt"), choice(&mode, "read-only")]);
assert_eq!(asking_choices(&codex, &[choice(&mode, "ask")]), [choice(&mode, "read-only")]);
// Claude Code's Accept Edits doesn't ask: default does.
let claude = options_of(&json!({"modes": {"currentModeId": "acceptEdits",
"availableModes": [{"id": "default"}, {"id": "acceptEdits"}, {"id": "plan"}, {"id": "bypassPermissions"}]}}));
assert_eq!(asking_choices(&claude, &[]), [choice(&claude[0].id, "default")]);
assert!(!keeps_asking(&claude));
let mut safe = claude.clone();
keep_asking(&mut safe);
assert!(keeps_asking(&safe));
}
#[test]
+18 -4
Переглянути файл
@@ -891,9 +891,9 @@ 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.
// The source task's choices may change the project without asking; the side conversation asks.
let wanted = config::asking_choices(&config, &req.config);
config::ask_for_approvals(&mut config);
config::keep_asking(&mut config);
if config::bypass_approvals(&config) {
return Err(IdeError::invalid(
"this agent has no mode that asks before changing things, so it can't run a side conversation",
@@ -1052,8 +1052,14 @@ 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"));
if s.ephemeral {
let mut next = s.task.config.clone();
if let Some(n) = next.iter_mut().find(|n| n.id == option) {
n.current = Some(value.clone());
}
if !config::keeps_asking(&next) {
return Err(IdeError::invalid("a side conversation asks before changing anything"));
}
}
o.clone()
};
@@ -2425,6 +2431,14 @@ impl Task {
Err(e) => tracing::warn!("agent task {}: setting {} to {} failed: {e}", self.id, o.id, choice.value),
}
}
// A side conversation never runs in a mode that changes things without asking: when the
// agent refused the switch, it doesn't start.
if self.state.lock().ephemeral && !config::keeps_asking(&options) {
return Err(SessionOpenError {
message: "The agent didn't switch to a mode that asks before changing anything, so the side conversation can't start".into(),
allow_acp_fallback: false,
});
}
let model = model_of(&options).filter(|_| model_options);
self.shared.remember(provider_id, Some(&options), None, model.as_deref());
{