Persist disabled plugin IDs in thread settings (#44332)

## What changed

- Add `disabled_plugin_ids` to thread startup options, settings overrides, snapshots, and persisted turn contexts. Omitted overrides preserve the current list; an empty list clears it.
- Restore the selection on resume and capture it before truncating history for a fork. Prefer the owning thread's latest settings snapshot, falling back only to the latest turn context so stale history cannot resurrect an older selection.
- Freeze the selection at turn admission and retain compatibility with histories that lack the new fields.

## Testing

Add coverage for owned settings, explicit clears, latest-turn fallback, and legacy snapshots. Extend settings tests to cover plugin selection during runtime restoration and compaction, and verify initial turn persistence without extra settings checkpoints in both history modes.

GitOrigin-RevId: 75ffbff5763cd93cc5ac69d73b08b237f59bc009
This commit is contained in:
Matthew Zeng authored and copyberry committed 2026-09-09 23:23:59 +00:00
1 parent e722303e38
commit b5544d5732
29 files changed
+374 -12

No files matched your search

+28
View File
@@ -453,6 +453,34 @@ fn session_cwd_from_items(items: &[RolloutItem]) -> Option<PathBuf> {
})
}
/// Returns a thread's latest plugin selection, with a turn-context fallback.
///
/// Forked history may contain ancestor snapshots, and compaction may append a
/// frozen turn context after an update. Neither can replace thread-owned settings.
/// Without an owned snapshot, only the latest turn context supplies the initial
/// selection; a missing field must not resurrect a selection from an older turn.
pub fn latest_disabled_plugin_ids(items: &[RolloutItem], thread_id: ThreadId) -> Option<&[String]> {
if let Some(ids) = items.iter().rev().find_map(|item| {
if let RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(event)) = item
&& event.thread_id == Some(thread_id)
{
Some(event.thread_settings.disabled_plugin_ids.as_slice())
} else {
None
}
}) {
return Some(ids);
}
items
.iter()
.rev()
.find_map(|item| match item {
RolloutItem::TurnContext(context) => Some(context),
_ => None,
})
.and_then(|context| context.disabled_plugin_ids.as_deref())
}
fn multi_agent_version_from_items(
items: &[RolloutItem],
thread_id: Option<ThreadId>,
+125
View File
@@ -1,11 +1,136 @@
use anyhow::Result;
use codex_protocol::models::ConfigurationReasoning;
use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::protocol::ThreadSettingsSnapshot;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
fn thread_settings_snapshot(disabled_plugin_ids: Vec<String>) -> Result<ThreadSettingsSnapshot> {
Ok(serde_json::from_value(json!({
"model": "gpt-5",
"model_provider_id": "openai",
"approval_policy": "never",
"approvals_reviewer": "user",
"permission_profile": codex_protocol::models::PermissionProfile::read_only(),
"cwd": std::env::current_dir()?,
"collaboration_mode": {
"mode": "default",
"settings": {
"model": "gpt-5",
"reasoning_effort": null,
"developer_instructions": null
}
},
"disabled_plugin_ids": disabled_plugin_ids
}))?)
}
#[test]
fn latest_disabled_plugin_ids_preserves_owned_updates_and_clears() -> Result<()> {
let thread_id = ThreadId::new();
let ancestor_id = ThreadId::new();
let selected = thread_settings_snapshot(vec!["example@marketplace".to_string()])?;
let cleared = thread_settings_snapshot(Vec::new())?;
let item = |thread_id, thread_settings| {
RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(
codex_protocol::protocol::ThreadSettingsAppliedEvent {
thread_id,
thread_settings,
},
))
};
let foreign_items = vec![
item(Some(ancestor_id), selected.clone()),
item(/*thread_id*/ None, selected.clone()),
];
assert_eq!(latest_disabled_plugin_ids(&foreign_items, thread_id), None);
let mut history = vec![item(Some(thread_id), selected.clone())];
history.extend(foreign_items.clone());
assert_eq!(
latest_disabled_plugin_ids(&history, thread_id),
Some(selected.disabled_plugin_ids.as_slice())
);
history.push(item(Some(thread_id), cleared.clone()));
history.extend(foreign_items);
history.push(turn_context_with_disabled_plugins(Some(
selected.disabled_plugin_ids,
))?);
assert_eq!(
latest_disabled_plugin_ids(&history, thread_id),
Some(cleared.disabled_plugin_ids.as_slice())
);
Ok(())
}
fn turn_context_with_disabled_plugins(
disabled_plugin_ids: Option<Vec<String>>,
) -> Result<RolloutItem> {
let mut context: TurnContextItem = serde_json::from_value(json!({
"cwd": std::env::current_dir()?,
"approval_policy": "never",
"sandbox_policy": { "type": "danger-full-access" },
"model": "gpt-5",
"summary": "auto"
}))?;
context.disabled_plugin_ids = disabled_plugin_ids;
Ok(RolloutItem::TurnContext(context))
}
#[test]
fn latest_disabled_plugin_ids_falls_back_only_to_latest_turn_context() -> Result<()> {
let thread_id = ThreadId::new();
let selected = vec!["example@marketplace".to_string()];
let mut history = vec![turn_context_with_disabled_plugins(Some(selected.clone()))?];
assert_eq!(
latest_disabled_plugin_ids(&history, thread_id),
Some(selected.as_slice())
);
history.push(RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(
codex_protocol::protocol::ThreadSettingsAppliedEvent {
thread_id: Some(ThreadId::new()),
thread_settings: thread_settings_snapshot(Vec::new())?,
},
)));
assert_eq!(
latest_disabled_plugin_ids(&history, thread_id),
Some(selected.as_slice())
);
let cleared = Vec::new();
history.push(turn_context_with_disabled_plugins(Some(cleared.clone()))?);
assert_eq!(
latest_disabled_plugin_ids(&history, thread_id),
Some(cleared.as_slice())
);
history.push(turn_context_with_disabled_plugins(
/*disabled_plugin_ids*/ None,
)?);
assert_eq!(latest_disabled_plugin_ids(&history, thread_id), None);
Ok(())
}
#[test]
fn older_thread_settings_snapshot_defaults_disabled_plugins_to_empty() -> Result<()> {
let expected = thread_settings_snapshot(Vec::new())?;
let mut legacy = serde_json::to_value(&expected)?;
legacy
.as_object_mut()
.unwrap()
.remove("disabled_plugin_ids");
assert_eq!(
serde_json::from_value::<ThreadSettingsSnapshot>(legacy)?,
expected
);
Ok(())
}
#[test]
fn response_item_envelope_accessors_preserve_item() {
let expected_item = ResponseItem::Message {