Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea2fc2fd40 | ||
|
|
1884f253da | ||
|
|
b601f2aa81 | ||
|
|
6aedd64e5b | ||
|
|
ce4a157adf | ||
|
|
d90859dcb3 | ||
|
|
0f633830ca | ||
|
|
8c6f3501af |
No files matched your search
@@ -48,6 +48,7 @@ use crate::facts::AppMentionedInput;
|
||||
use crate::facts::AppUsedInput;
|
||||
use crate::facts::CodexCompactionEvent;
|
||||
use crate::facts::CodexErrKind;
|
||||
use crate::facts::CodexPluginScriptLifecycleEvent;
|
||||
use crate::facts::CompactionImplementation;
|
||||
use crate::facts::CompactionPhase;
|
||||
use crate::facts::CompactionReason;
|
||||
@@ -62,6 +63,8 @@ use crate::facts::HookRunInput;
|
||||
use crate::facts::InputError;
|
||||
use crate::facts::InvocationType;
|
||||
use crate::facts::PluginInstallFailedInput;
|
||||
use crate::facts::PluginScriptLifecycleStatus;
|
||||
use crate::facts::PluginScriptSkill;
|
||||
use crate::facts::PluginState;
|
||||
use crate::facts::PluginStateChangedInput;
|
||||
use crate::facts::PluginUsedInput;
|
||||
@@ -3339,6 +3342,121 @@ async fn reducer_includes_plugin_id_for_plugin_skill_invocations() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reducer_enriches_plugin_script_lifecycle_events_without_sensitive_paths() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
let mut events = Vec::new();
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Initialize {
|
||||
connection_id: 7,
|
||||
params: InitializeParams {
|
||||
client_info: ClientInfo {
|
||||
name: "codex-tui".to_string(),
|
||||
title: None,
|
||||
version: "1.0.0".to_string(),
|
||||
},
|
||||
capabilities: Some(InitializeCapabilities {
|
||||
experimental_api: true,
|
||||
request_attestation: false,
|
||||
opt_out_notification_methods: None,
|
||||
}),
|
||||
},
|
||||
product_client_id: DEFAULT_ORIGINATOR.to_string(),
|
||||
runtime: sample_runtime_metadata(),
|
||||
rpc_transport: AppServerRpcTransport::Stdio,
|
||||
},
|
||||
&mut events,
|
||||
)
|
||||
.await;
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::ClientResponse {
|
||||
connection_id: 7,
|
||||
request_id: RequestId::Integer(1),
|
||||
response: Box::new(sample_thread_start_response(
|
||||
"thread-1", /*ephemeral*/ false, "gpt-5",
|
||||
)),
|
||||
},
|
||||
&mut events,
|
||||
)
|
||||
.await;
|
||||
events.clear();
|
||||
|
||||
let sensitive_skill_path =
|
||||
PathBuf::from("/Users/private/.codex/plugins/cache/openai/sample/skills/doc/SKILL.md");
|
||||
let skill = PluginScriptSkill {
|
||||
skill_name: "sample:doc".to_string(),
|
||||
skill_path: sensitive_skill_path.clone(),
|
||||
};
|
||||
let expected_skill_id = skill_id_for_local_skill(
|
||||
/*repo_url*/ None,
|
||||
/*repo_root*/ None,
|
||||
sensitive_skill_path.as_path(),
|
||||
"sample:doc",
|
||||
);
|
||||
for (status, duration_ms, exit_code) in [
|
||||
(PluginScriptLifecycleStatus::Started, None, None),
|
||||
(PluginScriptLifecycleStatus::Completed, Some(42), Some(0)),
|
||||
] {
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Custom(CustomAnalyticsFact::PluginScriptLifecycle(Box::new(
|
||||
CodexPluginScriptLifecycleEvent {
|
||||
thread_id: "thread-1".to_string(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
plugin_id: "sample@openai-curated".to_string(),
|
||||
execution_id: "execution-1".to_string(),
|
||||
script_path: "scripts/run.py".to_string(),
|
||||
timestamp: "2026-06-12T17:39:31.000Z".to_string(),
|
||||
status,
|
||||
duration_ms,
|
||||
exit_code,
|
||||
skill: Some(skill.clone()),
|
||||
},
|
||||
))),
|
||||
&mut events,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let payload = serde_json::to_value(&events).expect("serialize events");
|
||||
assert_eq!(payload.as_array().map(Vec::len), Some(2));
|
||||
let started = &payload[0];
|
||||
let completed = &payload[1];
|
||||
assert_eq!(started["event_type"], "codex_plugin_lifecycle_event");
|
||||
assert_eq!(started["event_params"]["version"], 1);
|
||||
assert_eq!(started["event_params"]["thread_id"], "thread-1");
|
||||
assert_eq!(started["event_params"]["session_id"], "session-thread-1");
|
||||
assert_eq!(started["event_params"]["turn_id"], "turn-1");
|
||||
assert_eq!(
|
||||
started["event_params"]["app_server_client"]["product_client_id"],
|
||||
DEFAULT_ORIGINATOR
|
||||
);
|
||||
assert_eq!(
|
||||
started["event_params"]["plugin_id"],
|
||||
"sample@openai-curated"
|
||||
);
|
||||
assert_eq!(started["event_params"]["execution_id"], "execution-1");
|
||||
assert_eq!(started["event_params"]["script_path"], "scripts/run.py");
|
||||
assert_eq!(
|
||||
started["event_params"]["timestamp"],
|
||||
"2026-06-12T17:39:31.000Z"
|
||||
);
|
||||
assert_eq!(started["event_params"]["status"], "started");
|
||||
assert_eq!(started["event_params"]["skill_id"], expected_skill_id);
|
||||
assert!(started["event_params"].get("duration_ms").is_none());
|
||||
assert!(started["event_params"].get("exit_code").is_none());
|
||||
|
||||
assert_eq!(completed["event_params"]["status"], "completed");
|
||||
assert_eq!(completed["event_params"]["duration_ms"], 42);
|
||||
assert_eq!(completed["event_params"]["exit_code"], 0);
|
||||
assert_eq!(completed["event_params"]["skill_id"], expected_skill_id);
|
||||
let serialized = payload.to_string();
|
||||
assert!(!serialized.contains("/Users/private"));
|
||||
assert!(!serialized.contains("SKILL.md"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reducer_ingests_hook_run_fact() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
|
||||
@@ -313,6 +313,15 @@ impl AnalyticsEventsClient {
|
||||
))));
|
||||
}
|
||||
|
||||
pub fn track_plugin_script_lifecycle(
|
||||
&self,
|
||||
event: crate::facts::CodexPluginScriptLifecycleEvent,
|
||||
) {
|
||||
self.record_fact(AnalyticsFact::Custom(
|
||||
CustomAnalyticsFact::PluginScriptLifecycle(Box::new(event)),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn track_turn_resolved_config(&self, fact: TurnResolvedConfigFact) {
|
||||
self.record_fact(AnalyticsFact::Custom(
|
||||
CustomAnalyticsFact::TurnResolvedConfig(Box::new(fact)),
|
||||
|
||||
@@ -14,6 +14,7 @@ use crate::facts::CompactionTrigger;
|
||||
use crate::facts::GoalEventKind;
|
||||
use crate::facts::HookRunFact;
|
||||
use crate::facts::InvocationType;
|
||||
use crate::facts::PluginScriptLifecycleStatus;
|
||||
use crate::facts::PluginState;
|
||||
use crate::facts::SubAgentThreadStartedInput;
|
||||
use crate::facts::ThreadInitializationMode;
|
||||
@@ -66,6 +67,7 @@ pub(crate) enum TrackEventRequest {
|
||||
HookRun(CodexHookRunEventRequest),
|
||||
Compaction(Box<CodexCompactionEventRequest>),
|
||||
Goal(Box<CodexGoalEventRequest>),
|
||||
PluginScriptLifecycle(Box<CodexPluginScriptLifecycleEventRequest>),
|
||||
TurnEvent(Box<CodexTurnEventRequest>),
|
||||
TurnSteer(CodexTurnSteerEventRequest),
|
||||
CommandExecution(CodexCommandExecutionEventRequest),
|
||||
@@ -842,6 +844,35 @@ pub(crate) struct CodexGoalEventRequest {
|
||||
pub(crate) event_params: CodexGoalEventParams,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexPluginScriptLifecycleEventParams {
|
||||
pub(crate) version: u32,
|
||||
pub(crate) thread_id: String,
|
||||
pub(crate) session_id: String,
|
||||
pub(crate) turn_id: String,
|
||||
pub(crate) app_server_client: CodexAppServerClientMetadata,
|
||||
pub(crate) runtime: CodexRuntimeMetadata,
|
||||
pub(crate) thread_source: Option<ThreadSource>,
|
||||
pub(crate) subagent_source: Option<String>,
|
||||
pub(crate) parent_thread_id: Option<String>,
|
||||
pub(crate) plugin_id: String,
|
||||
pub(crate) execution_id: String,
|
||||
pub(crate) script_path: String,
|
||||
pub(crate) timestamp: String,
|
||||
pub(crate) status: PluginScriptLifecycleStatus,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) duration_ms: Option<u64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) exit_code: Option<i32>,
|
||||
pub(crate) skill_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexPluginScriptLifecycleEventRequest {
|
||||
pub(crate) event_type: &'static str,
|
||||
pub(crate) event_params: CodexPluginScriptLifecycleEventParams,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct CodexTurnEventParams {
|
||||
pub(crate) thread_id: String,
|
||||
|
||||
@@ -441,6 +441,35 @@ pub struct CodexGoalEvent {
|
||||
pub cumulative_time_accounted_seconds: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum PluginScriptLifecycleStatus {
|
||||
Started,
|
||||
Completed,
|
||||
Failed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PluginScriptSkill {
|
||||
pub skill_name: String,
|
||||
pub skill_path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CodexPluginScriptLifecycleEvent {
|
||||
pub thread_id: String,
|
||||
pub turn_id: String,
|
||||
pub plugin_id: String,
|
||||
pub execution_id: String,
|
||||
pub script_path: String,
|
||||
pub timestamp: String,
|
||||
pub status: PluginScriptLifecycleStatus,
|
||||
pub duration_ms: Option<u64>,
|
||||
pub exit_code: Option<i32>,
|
||||
pub skill: Option<PluginScriptSkill>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) enum AnalyticsFact {
|
||||
Initialize {
|
||||
@@ -493,6 +522,7 @@ pub(crate) enum CustomAnalyticsFact {
|
||||
SubAgentThreadStarted(SubAgentThreadStartedInput),
|
||||
Compaction(Box<CodexCompactionEvent>),
|
||||
Goal(Box<CodexGoalEvent>),
|
||||
PluginScriptLifecycle(Box<CodexPluginScriptLifecycleEvent>),
|
||||
GuardianReview(Box<GuardianReviewEventParams>),
|
||||
TurnResolvedConfig(Box<TurnResolvedConfigFact>),
|
||||
TurnTokenUsage(Box<TurnTokenUsageFact>),
|
||||
|
||||
@@ -29,6 +29,7 @@ pub use facts::AppInvocation;
|
||||
pub use facts::CodexCompactionEvent;
|
||||
pub use facts::CodexErrKind;
|
||||
pub use facts::CodexGoalEvent;
|
||||
pub use facts::CodexPluginScriptLifecycleEvent;
|
||||
pub use facts::CodexTurnSteerEvent;
|
||||
pub use facts::CompactionImplementation;
|
||||
pub use facts::CompactionPhase;
|
||||
@@ -42,6 +43,8 @@ pub use facts::GoalEventKind;
|
||||
pub use facts::HookRunFact;
|
||||
pub use facts::InputError;
|
||||
pub use facts::InvocationType;
|
||||
pub use facts::PluginScriptLifecycleStatus;
|
||||
pub use facts::PluginScriptSkill;
|
||||
pub use facts::SkillInvocation;
|
||||
pub use facts::SubAgentThreadStartedInput;
|
||||
pub use facts::ThreadInitializationMode;
|
||||
|
||||
@@ -28,6 +28,8 @@ use crate::events::CodexOnboardingExternalAgentImportFailureMetadata;
|
||||
use crate::events::CodexPluginEventRequest;
|
||||
use crate::events::CodexPluginInstallFailedEventRequest;
|
||||
use crate::events::CodexPluginInstallFailedMetadata;
|
||||
use crate::events::CodexPluginScriptLifecycleEventParams;
|
||||
use crate::events::CodexPluginScriptLifecycleEventRequest;
|
||||
use crate::events::CodexPluginUsedEventRequest;
|
||||
use crate::events::CodexReviewEventParams;
|
||||
use crate::events::CodexReviewEventRequest;
|
||||
@@ -71,6 +73,7 @@ use crate::facts::AppMentionedInput;
|
||||
use crate::facts::AppUsedInput;
|
||||
use crate::facts::CodexCompactionEvent;
|
||||
use crate::facts::CodexGoalEvent;
|
||||
use crate::facts::CodexPluginScriptLifecycleEvent;
|
||||
use crate::facts::CustomAnalyticsFact;
|
||||
use crate::facts::ExternalAgentConfigImportCompletedInput;
|
||||
use crate::facts::ExternalAgentConfigImportFailureInput;
|
||||
@@ -214,6 +217,16 @@ impl<'a> AnalyticsDropSite<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn plugin_script_lifecycle(input: &'a CodexPluginScriptLifecycleEvent) -> Self {
|
||||
Self {
|
||||
event_name: "plugin script lifecycle",
|
||||
thread_id: &input.thread_id,
|
||||
turn_id: Some(&input.turn_id),
|
||||
review_id: None,
|
||||
item_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_item(
|
||||
notification: &'a codex_app_server_protocol::ItemCompletedNotification,
|
||||
item_id: &'a str,
|
||||
@@ -487,6 +500,9 @@ impl AnalyticsReducer {
|
||||
CustomAnalyticsFact::Goal(input) => {
|
||||
self.ingest_goal(*input, out);
|
||||
}
|
||||
CustomAnalyticsFact::PluginScriptLifecycle(input) => {
|
||||
self.ingest_plugin_script_lifecycle(*input, out).await;
|
||||
}
|
||||
CustomAnalyticsFact::GuardianReview(input) => {
|
||||
self.ingest_guardian_review(*input, out);
|
||||
}
|
||||
@@ -1383,6 +1399,60 @@ impl AnalyticsReducer {
|
||||
})));
|
||||
}
|
||||
|
||||
async fn ingest_plugin_script_lifecycle(
|
||||
&mut self,
|
||||
input: CodexPluginScriptLifecycleEvent,
|
||||
out: &mut Vec<TrackEventRequest>,
|
||||
) {
|
||||
let Some((connection_state, thread_metadata)) =
|
||||
self.thread_context_or_warn(AnalyticsDropSite::plugin_script_lifecycle(&input))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let skill_id = if let Some(skill) = input.skill.as_ref() {
|
||||
let repo_root = get_git_repo_root(skill.skill_path.as_path());
|
||||
let repo_url = if let Some(root) = repo_root.as_ref() {
|
||||
collect_git_info(root)
|
||||
.await
|
||||
.and_then(|info| info.repository_url)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Some(skill_id_for_local_skill(
|
||||
repo_url.as_deref(),
|
||||
repo_root.as_deref(),
|
||||
skill.skill_path.as_path(),
|
||||
skill.skill_name.as_str(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
out.push(TrackEventRequest::PluginScriptLifecycle(Box::new(
|
||||
CodexPluginScriptLifecycleEventRequest {
|
||||
event_type: "codex_plugin_lifecycle_event",
|
||||
event_params: CodexPluginScriptLifecycleEventParams {
|
||||
version: 1,
|
||||
thread_id: input.thread_id,
|
||||
session_id: thread_metadata.session_id.clone(),
|
||||
turn_id: input.turn_id,
|
||||
app_server_client: connection_state.app_server_client.clone(),
|
||||
runtime: connection_state.runtime.clone(),
|
||||
thread_source: thread_metadata.thread_source.clone(),
|
||||
subagent_source: thread_metadata.subagent_source.clone(),
|
||||
parent_thread_id: thread_metadata.parent_thread_id.clone(),
|
||||
plugin_id: input.plugin_id,
|
||||
execution_id: input.execution_id,
|
||||
script_path: input.script_path,
|
||||
timestamp: input.timestamp,
|
||||
status: input.status,
|
||||
duration_ms: input.duration_ms,
|
||||
exit_code: input.exit_code,
|
||||
skill_id,
|
||||
},
|
||||
},
|
||||
)));
|
||||
}
|
||||
|
||||
fn ingest_guardian_review_completed(
|
||||
&mut self,
|
||||
notification: codex_app_server_protocol::ItemGuardianApprovalReviewCompletedNotification,
|
||||
|
||||
@@ -10,6 +10,8 @@ use crate::marketplace::list_marketplaces;
|
||||
use crate::marketplace::load_marketplace;
|
||||
use crate::remote::REMOTE_GLOBAL_MARKETPLACE_NAME;
|
||||
use crate::remote::RemoteInstalledPlugin;
|
||||
use crate::startup_sync::curated_plugins_repo_path;
|
||||
use crate::startup_sync::read_curated_plugins_sha;
|
||||
use crate::store::PluginStore;
|
||||
use crate::store::plugin_version_for_source;
|
||||
use codex_app_server_protocol::AuthMode;
|
||||
@@ -665,6 +667,7 @@ async fn load_plugin(
|
||||
config_name,
|
||||
manifest_name: None,
|
||||
manifest_description: None,
|
||||
is_first_party: false,
|
||||
root,
|
||||
enabled: plugin.enabled,
|
||||
skill_roots: Vec::new(),
|
||||
@@ -705,6 +708,7 @@ async fn load_plugin(
|
||||
return loaded_plugin;
|
||||
};
|
||||
|
||||
loaded_plugin.is_first_party = is_openai_curated_plugin(&loaded_plugin_id, &plugin_root, store);
|
||||
let manifest_paths = &manifest.paths;
|
||||
match scope {
|
||||
PluginLoadScope::AllCapabilities {
|
||||
@@ -746,6 +750,37 @@ async fn load_plugin(
|
||||
loaded_plugin
|
||||
}
|
||||
|
||||
fn is_openai_curated_plugin(
|
||||
plugin_id: &PluginId,
|
||||
loaded_root: &AbsolutePathBuf,
|
||||
store: &PluginStore,
|
||||
) -> bool {
|
||||
if !is_openai_curated_marketplace_name(&plugin_id.marketplace_name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let Some(curated_sha) = read_curated_plugins_sha(store.codex_home()) else {
|
||||
return false;
|
||||
};
|
||||
let expected_version = curated_plugin_cache_version(&curated_sha);
|
||||
let expected_root = store.plugin_root(plugin_id, &expected_version);
|
||||
if loaded_root != &expected_root || !expected_root.as_path().is_dir() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let marketplace_path =
|
||||
curated_plugins_repo_path(store.codex_home()).join(".agents/plugins/marketplace.json");
|
||||
let Ok(marketplace_path) = AbsolutePathBuf::try_from(marketplace_path) else {
|
||||
return false;
|
||||
};
|
||||
load_marketplace(&marketplace_path).is_ok_and(|marketplace| {
|
||||
marketplace
|
||||
.plugins
|
||||
.iter()
|
||||
.any(|plugin| plugin.name == plugin_id.plugin_name)
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_plugin_mcp_server_policy(config: &mut McpServerConfig, policy: &PluginMcpServerConfig) {
|
||||
config.enabled = policy.enabled;
|
||||
if let Some(approval_mode) = policy.default_tools_approval_mode {
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
use super::*;
|
||||
use crate::PluginLoadOutcome;
|
||||
use crate::manifest::load_plugin_manifest;
|
||||
use crate::startup_sync::curated_plugins_repo_path;
|
||||
use crate::test_support::TEST_CURATED_PLUGIN_CACHE_VERSION;
|
||||
use crate::test_support::TEST_CURATED_PLUGIN_SHA;
|
||||
use crate::test_support::write_curated_plugin_sha_with;
|
||||
use crate::test_support::write_file;
|
||||
use crate::test_support::write_openai_curated_marketplace;
|
||||
use codex_config::ConfigLayerEntry;
|
||||
use codex_config::ConfigLayerSource;
|
||||
use codex_config::ConfigRequirements;
|
||||
@@ -226,6 +232,136 @@ fn curated_plugin_cache_version_preserves_non_git_sha_versions() {
|
||||
assert_eq!(curated_plugin_cache_version("0123456"), "0123456");
|
||||
}
|
||||
|
||||
fn curated_plugin_config_layer(temp_dir: &TempDir, plugin_name: &str) -> ConfigLayerStack {
|
||||
ConfigLayerStack::new(
|
||||
vec![user_layer(
|
||||
user_config_path(temp_dir, "config.toml"),
|
||||
&format!(
|
||||
r#"[plugins."{plugin_name}@openai-curated"]
|
||||
enabled = true
|
||||
"#,
|
||||
),
|
||||
)],
|
||||
ConfigRequirements::default(),
|
||||
ConfigRequirementsToml::default(),
|
||||
)
|
||||
.expect("valid curated plugin config")
|
||||
}
|
||||
|
||||
fn write_curated_cached_plugin(codex_home: &Path, plugin_name: &str, version: &str) {
|
||||
let plugin_root = codex_home
|
||||
.join("plugins/cache/openai-curated")
|
||||
.join(plugin_name)
|
||||
.join(version);
|
||||
write_file(
|
||||
&plugin_root.join(".codex-plugin/plugin.json"),
|
||||
&format!(r#"{{"name":"{plugin_name}"}}"#),
|
||||
);
|
||||
write_file(&plugin_root.join("skills/SKILL.md"), "skill");
|
||||
}
|
||||
|
||||
async fn load_curated_plugin(temp_dir: &TempDir, plugin_name: &str) -> PluginLoadOutcome {
|
||||
PluginLoadOutcome::from_plugins(
|
||||
load_plugins_from_layer_stack(
|
||||
&curated_plugin_config_layer(temp_dir, plugin_name),
|
||||
HashMap::new(),
|
||||
&PluginStore::new(temp_dir.path().to_path_buf()),
|
||||
Some(Product::Codex),
|
||||
/*prefer_remote_curated_conflicts*/ false,
|
||||
)
|
||||
.await,
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_curated_plugin_uses_synced_sha_root_when_it_is_only_installed_root() {
|
||||
let temp_dir = TempDir::new().expect("tempdir");
|
||||
let curated_root = curated_plugins_repo_path(temp_dir.path());
|
||||
write_openai_curated_marketplace(&curated_root, &["slack"]);
|
||||
write_curated_plugin_sha_with(temp_dir.path(), TEST_CURATED_PLUGIN_SHA);
|
||||
write_curated_cached_plugin(temp_dir.path(), "slack", TEST_CURATED_PLUGIN_CACHE_VERSION);
|
||||
|
||||
let outcome = load_curated_plugin(&temp_dir, "slack").await;
|
||||
let plugin = outcome.plugins().first().expect("configured plugin");
|
||||
let synced_root = AbsolutePathBuf::try_from(temp_dir.path().join(format!(
|
||||
"plugins/cache/openai-curated/slack/{TEST_CURATED_PLUGIN_CACHE_VERSION}"
|
||||
)))
|
||||
.expect("synced plugin root");
|
||||
|
||||
assert_eq!(plugin.root, synced_root);
|
||||
assert!(plugin.is_first_party);
|
||||
assert_eq!(
|
||||
outcome.effective_first_party_plugin_roots(),
|
||||
vec![codex_plugin::FirstPartyPluginRoot {
|
||||
plugin_id: "slack@openai-curated".to_string(),
|
||||
plugin_root: synced_root,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_curated_plugin_without_synced_cache_root_is_not_first_party() {
|
||||
let temp_dir = TempDir::new().expect("tempdir");
|
||||
let curated_root = curated_plugins_repo_path(temp_dir.path());
|
||||
write_openai_curated_marketplace(&curated_root, &["slack"]);
|
||||
write_curated_plugin_sha_with(temp_dir.path(), TEST_CURATED_PLUGIN_SHA);
|
||||
write_curated_cached_plugin(temp_dir.path(), "slack", "local");
|
||||
|
||||
let outcome = load_curated_plugin(&temp_dir, "slack").await;
|
||||
|
||||
assert!(
|
||||
!outcome
|
||||
.plugins()
|
||||
.first()
|
||||
.expect("configured plugin")
|
||||
.is_first_party
|
||||
);
|
||||
assert_eq!(outcome.effective_first_party_plugin_roots(), Vec::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_curated_plugin_keeps_local_root_priority_over_synced_sha_root() {
|
||||
let temp_dir = TempDir::new().expect("tempdir");
|
||||
let curated_root = curated_plugins_repo_path(temp_dir.path());
|
||||
write_openai_curated_marketplace(&curated_root, &["slack"]);
|
||||
write_curated_plugin_sha_with(temp_dir.path(), TEST_CURATED_PLUGIN_SHA);
|
||||
write_curated_cached_plugin(temp_dir.path(), "slack", "local");
|
||||
write_curated_cached_plugin(temp_dir.path(), "slack", TEST_CURATED_PLUGIN_CACHE_VERSION);
|
||||
|
||||
let outcome = load_curated_plugin(&temp_dir, "slack").await;
|
||||
let plugin = outcome.plugins().first().expect("configured plugin");
|
||||
let local_root = AbsolutePathBuf::try_from(
|
||||
temp_dir
|
||||
.path()
|
||||
.join("plugins/cache/openai-curated/slack/local"),
|
||||
)
|
||||
.expect("local plugin root");
|
||||
|
||||
assert_eq!(plugin.root, local_root);
|
||||
assert!(!plugin.is_first_party);
|
||||
assert_eq!(outcome.effective_first_party_plugin_roots(), Vec::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_curated_plugin_missing_from_synced_marketplace_is_not_first_party() {
|
||||
let temp_dir = TempDir::new().expect("tempdir");
|
||||
let curated_root = curated_plugins_repo_path(temp_dir.path());
|
||||
write_openai_curated_marketplace(&curated_root, &["github"]);
|
||||
write_curated_plugin_sha_with(temp_dir.path(), TEST_CURATED_PLUGIN_SHA);
|
||||
write_curated_cached_plugin(temp_dir.path(), "slack", TEST_CURATED_PLUGIN_CACHE_VERSION);
|
||||
|
||||
let outcome = load_curated_plugin(&temp_dir, "slack").await;
|
||||
|
||||
assert!(
|
||||
!outcome
|
||||
.plugins()
|
||||
.first()
|
||||
.expect("configured plugin")
|
||||
.is_first_party
|
||||
);
|
||||
assert_eq!(outcome.effective_first_party_plugin_roots(), Vec::new());
|
||||
}
|
||||
|
||||
fn plugin_id() -> PluginId {
|
||||
PluginId::parse("demo-plugin@test-marketplace").expect("plugin id")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::LoadedPlugin;
|
||||
use super::PluginLoadOutcome;
|
||||
use crate::OPENAI_CURATED_MARKETPLACE_NAME;
|
||||
use crate::app_mcp_routing::apply_app_mcp_routing_policy;
|
||||
use crate::installed_marketplaces::installed_marketplace_roots_from_layer_stack;
|
||||
use crate::is_openai_curated_marketplace_name;
|
||||
@@ -1091,6 +1092,7 @@ impl PluginsManager {
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
self.validate_curated_marketplace_install_source(&request.marketplace_path, &resolved)?;
|
||||
let plugin_id = resolved.plugin_id.clone();
|
||||
match self.install_resolved_plugin(resolved).await {
|
||||
Ok(outcome) => Ok(outcome),
|
||||
@@ -1122,6 +1124,7 @@ impl PluginsManager {
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
self.validate_curated_marketplace_install_source(&request.marketplace_path, &resolved)?;
|
||||
let plugin_id = resolved.plugin_id.as_key();
|
||||
// This only forwards the backend mutation before the local install flow.
|
||||
if let Err(err) = crate::remote_legacy::enable_remote_plugin(
|
||||
@@ -1208,6 +1211,33 @@ impl PluginsManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_curated_marketplace_install_source(
|
||||
&self,
|
||||
marketplace_path: &AbsolutePathBuf,
|
||||
resolved: &ResolvedMarketplacePlugin,
|
||||
) -> Result<(), PluginInstallError> {
|
||||
if resolved.plugin_id.marketplace_name != OPENAI_CURATED_MARKETPLACE_NAME {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let expected_path = curated_plugins_repo_path(self.codex_home.as_path())
|
||||
.join(".agents/plugins/marketplace.json");
|
||||
let expected_path = AbsolutePathBuf::try_from(expected_path)
|
||||
.ok()
|
||||
.and_then(|path| path.canonicalize().ok());
|
||||
let requested_path = marketplace_path.canonicalize().ok();
|
||||
if requested_path
|
||||
.zip(expected_path)
|
||||
.is_none_or(|(requested, expected)| requested != expected)
|
||||
{
|
||||
return Err(PluginStoreError::Invalid(format!(
|
||||
"marketplace '{OPENAI_CURATED_MARKETPLACE_NAME}' can only be installed from the synced curated marketplace"
|
||||
))
|
||||
.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn install_resolved_plugin(
|
||||
&self,
|
||||
resolved: ResolvedMarketplacePlugin,
|
||||
|
||||
@@ -622,6 +622,7 @@ async fn load_plugins_loads_default_skills_and_mcp_servers() {
|
||||
manifest_description: Some(
|
||||
"Plugin that includes the sample MCP server and Skills".to_string(),
|
||||
),
|
||||
is_first_party: false,
|
||||
root: AbsolutePathBuf::try_from(plugin_root.clone()).unwrap(),
|
||||
enabled: true,
|
||||
skill_roots: vec![plugin_root.join("skills").abs()],
|
||||
@@ -1586,6 +1587,7 @@ async fn load_plugins_preserves_disabled_plugins_without_effective_contributions
|
||||
config_name: "sample@test".to_string(),
|
||||
manifest_name: None,
|
||||
manifest_description: None,
|
||||
is_first_party: false,
|
||||
root: AbsolutePathBuf::try_from(plugin_root).unwrap(),
|
||||
enabled: false,
|
||||
skill_roots: Vec::new(),
|
||||
@@ -1754,6 +1756,7 @@ fn capability_index_filters_inactive_and_zero_capability_plugins() {
|
||||
config_name: config_name.to_string(),
|
||||
manifest_name: Some(manifest_name.to_string()),
|
||||
manifest_description: None,
|
||||
is_first_party: false,
|
||||
root: AbsolutePathBuf::try_from(codex_home.path().join(dir_name)).unwrap(),
|
||||
enabled: true,
|
||||
skill_roots: Vec::new(),
|
||||
@@ -2077,6 +2080,38 @@ async fn install_openai_curated_plugin_uses_short_sha_cache_version() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn install_plugin_rejects_spoofed_openai_curated_marketplace() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let curated_root = curated_plugins_repo_path(tmp.path());
|
||||
write_openai_curated_marketplace(&curated_root, &["slack"]);
|
||||
write_curated_plugin_sha(tmp.path(), TEST_CURATED_PLUGIN_SHA);
|
||||
let spoofed_root = tmp.path().join("spoofed-marketplace");
|
||||
write_openai_curated_marketplace(&spoofed_root, &["slack"]);
|
||||
|
||||
let err = PluginsManager::new(tmp.path().to_path_buf())
|
||||
.install_plugin(PluginInstallRequest {
|
||||
plugin_name: "slack".to_string(),
|
||||
marketplace_path: AbsolutePathBuf::try_from(
|
||||
spoofed_root.join(".agents/plugins/marketplace.json"),
|
||||
)
|
||||
.unwrap(),
|
||||
})
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(err.is_invalid_request());
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("can only be installed from the synced curated marketplace")
|
||||
);
|
||||
assert!(
|
||||
!tmp.path()
|
||||
.join("plugins/cache/openai-curated/slack")
|
||||
.exists()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn install_plugin_uses_manifest_version_for_non_curated_plugins() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -26,6 +26,7 @@ pub struct PluginInstallResult {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PluginStore {
|
||||
codex_home: AbsolutePathBuf,
|
||||
root: AbsolutePathBuf,
|
||||
data_root: AbsolutePathBuf,
|
||||
}
|
||||
@@ -42,8 +43,18 @@ impl PluginStore {
|
||||
let data_root =
|
||||
AbsolutePathBuf::from_absolute_path_checked(codex_home.join(PLUGINS_DATA_DIR))
|
||||
.map_err(|err| PluginStoreError::io("failed to resolve plugin data root", err))?;
|
||||
let codex_home = AbsolutePathBuf::from_absolute_path_checked(codex_home)
|
||||
.map_err(|err| PluginStoreError::io("failed to resolve Codex home", err))?;
|
||||
|
||||
Ok(Self { root, data_root })
|
||||
Ok(Self {
|
||||
codex_home,
|
||||
root,
|
||||
data_root,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn codex_home(&self) -> &AbsolutePathBuf {
|
||||
&self.codex_home
|
||||
}
|
||||
|
||||
pub fn root(&self) -> &AbsolutePathBuf {
|
||||
|
||||
@@ -503,6 +503,7 @@ async fn get_raw_output_result(
|
||||
windows_sandbox_policy_cwd,
|
||||
windows_sandbox_workspace_roots,
|
||||
windows_sandbox_filesystem_overrides,
|
||||
after_spawn,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -583,6 +584,7 @@ async fn exec_windows_sandbox(
|
||||
windows_sandbox_policy_cwd: &AbsolutePathBuf,
|
||||
windows_sandbox_workspace_roots: &[AbsolutePathBuf],
|
||||
windows_sandbox_filesystem_overrides: Option<&WindowsSandboxFilesystemOverrides>,
|
||||
after_spawn: Option<Box<dyn FnOnce() + Send>>,
|
||||
) -> Result<RawExecToolCallOutput> {
|
||||
use crate::config::find_codex_home;
|
||||
use codex_windows_sandbox::run_windows_sandbox_capture_for_permission_profile_elevated;
|
||||
@@ -662,6 +664,7 @@ async fn exec_windows_sandbox(
|
||||
write_roots_override: elevated_write_roots_override.as_deref(),
|
||||
deny_read_paths_override: &additional_deny_read_paths,
|
||||
deny_write_paths_override: &additional_deny_write_paths,
|
||||
after_spawn,
|
||||
},
|
||||
)
|
||||
} else {
|
||||
@@ -677,6 +680,7 @@ async fn exec_windows_sandbox(
|
||||
&additional_deny_read_paths,
|
||||
&additional_deny_write_paths,
|
||||
windows_sandbox_private_desktop,
|
||||
after_spawn,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -69,6 +69,8 @@ pub use mention_syntax::PLUGIN_TEXT_MENTION_SIGIL;
|
||||
pub use mention_syntax::TOOL_MENTION_SIGIL;
|
||||
pub use utils::path_utils;
|
||||
pub mod personality_migration;
|
||||
#[allow(dead_code)]
|
||||
mod plugin_script_resolver;
|
||||
pub(crate) mod plugins;
|
||||
#[doc(hidden)]
|
||||
pub(crate) mod prompt_debug;
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
use std::path::Path;
|
||||
|
||||
use codex_analytics::PluginScriptSkill;
|
||||
use codex_plugin::FirstPartyPluginRoot;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
|
||||
use crate::shell::ShellType;
|
||||
use crate::skills::SkillLoadOutcome;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ResolvedPluginScript {
|
||||
pub(crate) plugin_id: String,
|
||||
pub(crate) script_path: String,
|
||||
pub(crate) skill: Option<PluginScriptSkill>,
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_plugin_script(
|
||||
plugin_roots: &[FirstPartyPluginRoot],
|
||||
skills_outcome: &SkillLoadOutcome,
|
||||
command: &str,
|
||||
cwd: &AbsolutePathBuf,
|
||||
shell_type: ShellType,
|
||||
) -> Option<ResolvedPluginScript> {
|
||||
let script_token = script_token(command, shell_type)?;
|
||||
let script_path = Path::new(&script_token);
|
||||
let script_path = if script_path.is_absolute() {
|
||||
AbsolutePathBuf::try_from(script_path).ok()?
|
||||
} else {
|
||||
cwd.join(script_path)
|
||||
};
|
||||
let script_path = script_path.canonicalize().ok()?;
|
||||
script_path.as_path().is_file().then_some(())?;
|
||||
|
||||
let (root, plugin_root) = plugin_roots
|
||||
.iter()
|
||||
.filter_map(|root| {
|
||||
let plugin_root = root.plugin_root.canonicalize().ok()?;
|
||||
script_path.strip_prefix(&plugin_root).ok()?;
|
||||
Some((root, plugin_root))
|
||||
})
|
||||
.max_by_key(|(_, plugin_root)| plugin_root.components().count())?;
|
||||
let relative = script_path.strip_prefix(plugin_root).ok()?;
|
||||
if relative.as_os_str().is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(ResolvedPluginScript {
|
||||
plugin_id: root.plugin_id.clone(),
|
||||
script_path: normalized_relative_path(relative)?,
|
||||
skill: skill_for_script(skills_outcome, &root.plugin_id, &script_path),
|
||||
})
|
||||
}
|
||||
|
||||
fn skill_for_script(
|
||||
skills_outcome: &SkillLoadOutcome,
|
||||
plugin_id: &str,
|
||||
script_path: &Path,
|
||||
) -> Option<PluginScriptSkill> {
|
||||
skills_outcome
|
||||
.skills
|
||||
.iter()
|
||||
.filter_map(|skill| {
|
||||
if skill.plugin_id.as_deref() != Some(plugin_id)
|
||||
|| !skills_outcome.is_skill_enabled(skill)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let scripts_dir = skill.path_to_skills_md.parent()?.join("scripts");
|
||||
let scripts_dir = scripts_dir.canonicalize().ok()?;
|
||||
script_path.strip_prefix(&scripts_dir).ok()?;
|
||||
Some((skill, scripts_dir))
|
||||
})
|
||||
.max_by_key(|(_, scripts_dir)| scripts_dir.components().count())
|
||||
.map(|(skill, _)| PluginScriptSkill {
|
||||
skill_name: skill.name.clone(),
|
||||
skill_path: skill.path_to_skills_md.clone().into_path_buf(),
|
||||
})
|
||||
}
|
||||
|
||||
fn script_token(command: &str, shell_type: ShellType) -> Option<String> {
|
||||
let tokens = command_tokens(command, shell_type)?;
|
||||
let program = tokens.first()?;
|
||||
let windows_shell = matches!(shell_type, ShellType::PowerShell | ShellType::Cmd);
|
||||
let basename = if windows_shell {
|
||||
program.rsplit(['/', '\\']).next()?.to_ascii_lowercase()
|
||||
} else {
|
||||
Path::new(program)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())?
|
||||
.to_string()
|
||||
};
|
||||
let basename = if windows_shell {
|
||||
basename.strip_suffix(".exe").unwrap_or(&basename)
|
||||
} else {
|
||||
&basename
|
||||
};
|
||||
if !is_safe_script_candidate(program, shell_type) {
|
||||
return None;
|
||||
}
|
||||
let args = &tokens[1..];
|
||||
let path_qualified_program =
|
||||
Path::new(program).is_absolute() || program.contains('/') || program.contains('\\');
|
||||
let runner_script = if path_qualified_program {
|
||||
None
|
||||
} else {
|
||||
match basename {
|
||||
"python" | "python3" => script_after_allowed_options(args, &["-u"]),
|
||||
"bash" | "zsh" | "sh" => script_after_allowed_options(args, &["-e"]),
|
||||
"node" => args.first().filter(|arg| !arg.starts_with('-')).cloned(),
|
||||
"pwsh" | "powershell" => match args {
|
||||
[option, script, ..]
|
||||
if matches!(option.to_ascii_lowercase().as_str(), "-file" | "-f") =>
|
||||
{
|
||||
Some(script.clone())
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
if let Some(runner_script) = runner_script {
|
||||
return is_safe_script_candidate(&runner_script, shell_type).then_some(runner_script);
|
||||
}
|
||||
if matches!(
|
||||
basename,
|
||||
"python" | "python3" | "bash" | "zsh" | "sh" | "node" | "pwsh" | "powershell"
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
|
||||
path_qualified_program.then(|| program.clone())
|
||||
}
|
||||
|
||||
fn is_safe_script_candidate(token: &str, shell_type: ShellType) -> bool {
|
||||
let expands_shell_paths =
|
||||
matches!(shell_type, ShellType::Bash | ShellType::Sh | ShellType::Zsh);
|
||||
!(expands_shell_paths && has_shell_path_expansion(token)
|
||||
|| shell_type == ShellType::Zsh && token.starts_with('='))
|
||||
}
|
||||
|
||||
fn has_shell_path_expansion(token: &str) -> bool {
|
||||
token.starts_with('~') || token.contains(['\\', '*', '?', '[', ']', '{', '}'])
|
||||
}
|
||||
|
||||
fn script_after_allowed_options(args: &[String], allowed_options: &[&str]) -> Option<String> {
|
||||
let mut args = args.iter();
|
||||
loop {
|
||||
let arg = args.next()?;
|
||||
if !arg.starts_with('-') {
|
||||
return Some(arg.clone());
|
||||
}
|
||||
if !allowed_options.contains(&arg.as_str()) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn command_tokens(command: &str, shell_type: ShellType) -> Option<Vec<String>> {
|
||||
match shell_type {
|
||||
ShellType::Bash | ShellType::Sh | ShellType::Zsh => {
|
||||
let tree = codex_shell_command::bash::try_parse_shell(command)?;
|
||||
let mut commands =
|
||||
codex_shell_command::bash::try_parse_word_only_commands_sequence(&tree, command)?;
|
||||
let [tokens] = commands.as_mut_slice() else {
|
||||
return None;
|
||||
};
|
||||
Some(std::mem::take(tokens))
|
||||
}
|
||||
ShellType::PowerShell => split_powershell_command(command),
|
||||
ShellType::Cmd => split_cmd_command(command),
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits one plain PowerShell-style command without treating backslashes as
|
||||
/// escapes. Compound commands are rejected because lifecycle events attach to
|
||||
/// the spawned shell process and cannot represent multiple child scripts.
|
||||
fn split_powershell_command(command: &str) -> Option<Vec<String>> {
|
||||
if command.contains(['$', '~', '`', '(', ')', '{', '}', ',', '<', '>', '@', '#'])
|
||||
|| matches!(command.trim_start().chars().next(), Some('\'' | '"'))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
split_windows_command(command)
|
||||
}
|
||||
|
||||
fn split_windows_command(command: &str) -> Option<Vec<String>> {
|
||||
let mut chars = command.chars().peekable();
|
||||
let mut tokens = Vec::new();
|
||||
let mut token = String::new();
|
||||
let mut quote = None;
|
||||
let mut saw_token = false;
|
||||
|
||||
while let Some(ch) = chars.next() {
|
||||
if let Some(active_quote) = quote {
|
||||
if ch == '`' && active_quote == '"' {
|
||||
token.push(chars.next()?);
|
||||
} else if ch == active_quote {
|
||||
if chars.peek() == Some(&active_quote) {
|
||||
token.push(active_quote);
|
||||
chars.next();
|
||||
} else {
|
||||
quote = None;
|
||||
}
|
||||
} else {
|
||||
token.push(ch);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match ch {
|
||||
'\'' | '"' => {
|
||||
quote = Some(ch);
|
||||
saw_token = true;
|
||||
}
|
||||
'`' => {
|
||||
token.push(chars.next()?);
|
||||
saw_token = true;
|
||||
}
|
||||
' ' | '\t' => {
|
||||
if saw_token {
|
||||
tokens.push(std::mem::take(&mut token));
|
||||
saw_token = false;
|
||||
}
|
||||
}
|
||||
'&' if tokens.is_empty() && !saw_token => {
|
||||
if !chars.peek().is_some_and(|next| next.is_whitespace()) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
'&' | '|' | ';' | '\r' | '\n' => return None,
|
||||
_ => {
|
||||
token.push(ch);
|
||||
saw_token = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if quote.is_some() {
|
||||
return None;
|
||||
}
|
||||
if saw_token {
|
||||
tokens.push(token);
|
||||
}
|
||||
(!tokens.is_empty()).then_some(tokens)
|
||||
}
|
||||
|
||||
fn split_cmd_command(command: &str) -> Option<Vec<String>> {
|
||||
if command.chars().any(|ch| {
|
||||
matches!(
|
||||
ch,
|
||||
'\'' | '`' | '^' | '%' | '!' | '&' | '|' | '<' | '>' | '(' | ')' | '\r' | '\n'
|
||||
)
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
split_windows_command(command)
|
||||
}
|
||||
|
||||
fn normalized_relative_path(path: &Path) -> Option<String> {
|
||||
path.components()
|
||||
.filter_map(|component| match component {
|
||||
std::path::Component::Normal(value) => Some(value.to_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.map(|components| components.join("/"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "plugin_script_resolver_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,338 @@
|
||||
use codex_core_skills::SkillMetadata;
|
||||
use codex_protocol::protocol::SkillScope;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn fixture() -> (TempDir, AbsolutePathBuf, Vec<FirstPartyPluginRoot>) {
|
||||
let temp = tempfile::tempdir().expect("tempdir");
|
||||
let root = AbsolutePathBuf::try_from(temp.path().join("plugin")).expect("absolute path");
|
||||
fs::create_dir_all(root.join("skills/demo/scripts")).expect("create scripts directory");
|
||||
fs::write(root.join("skills/demo/SKILL.md"), "# Demo").expect("write skill");
|
||||
fs::write(root.join("skills/demo/scripts/run.py"), "print('ok')").expect("write script");
|
||||
let roots = vec![FirstPartyPluginRoot {
|
||||
plugin_id: "openai/demo".to_string(),
|
||||
plugin_root: root.clone(),
|
||||
}];
|
||||
(temp, root, roots)
|
||||
}
|
||||
|
||||
fn skill(root: &AbsolutePathBuf, name: &str, path: &str) -> SkillMetadata {
|
||||
SkillMetadata {
|
||||
name: name.to_string(),
|
||||
description: String::new(),
|
||||
short_description: None,
|
||||
interface: None,
|
||||
dependencies: None,
|
||||
policy: None,
|
||||
path_to_skills_md: root.join(path),
|
||||
scope: SkillScope::User,
|
||||
plugin_id: Some("openai/demo".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn skill_outcome(root: &AbsolutePathBuf) -> SkillLoadOutcome {
|
||||
let mut outcome = SkillLoadOutcome::default();
|
||||
outcome.skills = vec![skill(root, "demo", "skills/demo/SKILL.md")];
|
||||
outcome
|
||||
}
|
||||
|
||||
fn resolve(
|
||||
roots: &[FirstPartyPluginRoot],
|
||||
skills: &SkillLoadOutcome,
|
||||
command: &str,
|
||||
cwd: &AbsolutePathBuf,
|
||||
) -> Option<ResolvedPluginScript> {
|
||||
resolve_plugin_script(roots, skills, command, cwd, ShellType::Bash)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_interpreter_script_to_plugin_relative_path_and_skill() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
let resolved = resolve(
|
||||
&roots,
|
||||
&skill_outcome(&root),
|
||||
"python skills/demo/scripts/run.py --secret argument",
|
||||
&root,
|
||||
)
|
||||
.expect("plugin script");
|
||||
|
||||
assert_eq!(resolved.plugin_id, "openai/demo");
|
||||
assert_eq!(resolved.script_path, "skills/demo/scripts/run.py");
|
||||
assert_eq!(resolved.skill.expect("skill").skill_name, "demo");
|
||||
|
||||
let absolute = resolve(
|
||||
&roots,
|
||||
&SkillLoadOutcome::default(),
|
||||
root.join("skills/demo/scripts/run.py")
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/")
|
||||
.as_ref(),
|
||||
&root,
|
||||
)
|
||||
.expect("absolute plugin script");
|
||||
assert_eq!(absolute.script_path, "skills/demo/scripts/run.py");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_direct_executable_without_a_known_extension() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
fs::create_dir_all(root.join("bin")).expect("create bin");
|
||||
fs::write(root.join("bin/run"), "#!/bin/sh\n").expect("write executable");
|
||||
|
||||
let resolved =
|
||||
resolve(&roots, &SkillLoadOutcome::default(), "./bin/run", &root).expect("plugin script");
|
||||
|
||||
assert_eq!(resolved.script_path, "bin/run");
|
||||
assert!(resolved.skill.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_plugin_and_symlink_escape_paths() {
|
||||
let (temp, root, roots) = fixture();
|
||||
let outside = AbsolutePathBuf::try_from(temp.path().join("outside.py")).expect("absolute path");
|
||||
fs::write(&outside, "print('outside')").expect("write outside script");
|
||||
|
||||
for command in [
|
||||
outside.to_string_lossy().into_owned(),
|
||||
"skills/demo/scripts".to_string(),
|
||||
] {
|
||||
assert!(
|
||||
resolve(&roots, &SkillLoadOutcome::default(), &command, &root).is_none(),
|
||||
"unexpected lifecycle attribution for {command}"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::os::unix::fs::symlink(&outside, root.join("escape.py")).expect("create symlink");
|
||||
assert!(
|
||||
resolve(
|
||||
&roots,
|
||||
&SkillLoadOutcome::default(),
|
||||
"python escape.py",
|
||||
&root,
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_skill_owns_its_deepest_matching_scripts_directory() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
fs::create_dir_all(root.join("skills/demo/scripts/nested/scripts"))
|
||||
.expect("create nested scripts");
|
||||
fs::write(root.join("skills/demo/scripts/nested/SKILL.md"), "# Nested")
|
||||
.expect("write nested skill");
|
||||
fs::write(
|
||||
root.join("skills/demo/scripts/nested/scripts/run.py"),
|
||||
"print('ok')",
|
||||
)
|
||||
.expect("write nested script");
|
||||
let mut skills = skill_outcome(&root);
|
||||
skills.skills.push(skill(
|
||||
&root,
|
||||
"nested",
|
||||
"skills/demo/scripts/nested/SKILL.md",
|
||||
));
|
||||
|
||||
let resolved = resolve(
|
||||
&roots,
|
||||
&skills,
|
||||
"python skills/demo/scripts/nested/scripts/run.py",
|
||||
&root,
|
||||
)
|
||||
.expect("nested plugin script");
|
||||
assert_eq!(resolved.skill.expect("skill").skill_name, "nested");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_node_and_shell_scripts() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
fs::write(root.join("skills/demo/scripts/run.js"), "console.log('ok')")
|
||||
.expect("write node script");
|
||||
fs::write(root.join("skills/demo/scripts/run.sh"), "echo ok").expect("write shell script");
|
||||
|
||||
for (command, expected) in [
|
||||
(
|
||||
"node skills/demo/scripts/run.js",
|
||||
"skills/demo/scripts/run.js",
|
||||
),
|
||||
(
|
||||
"sh skills/demo/scripts/run.sh",
|
||||
"skills/demo/scripts/run.sh",
|
||||
),
|
||||
(
|
||||
"python -u skills/demo/scripts/run.py",
|
||||
"skills/demo/scripts/run.py",
|
||||
),
|
||||
(
|
||||
"sh -e skills/demo/scripts/run.sh",
|
||||
"skills/demo/scripts/run.sh",
|
||||
),
|
||||
] {
|
||||
let resolved =
|
||||
resolve(&roots, &SkillLoadOutcome::default(), command, &root).expect("plugin script");
|
||||
assert_eq!(resolved.script_path, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_shell_path_expansion_candidates() {
|
||||
for shell_type in [ShellType::Bash, ShellType::Sh, ShellType::Zsh] {
|
||||
for command in [
|
||||
"python ~/run.py",
|
||||
"~/run",
|
||||
"python scripts/z?.py",
|
||||
"python scripts/*.py",
|
||||
"scripts/*.sh",
|
||||
"python scripts/[rz]un.py",
|
||||
"python scripts/{run,skip}.py",
|
||||
r#"python scripts/run\ script.py"#,
|
||||
r#"python "scripts/run\\script.py""#,
|
||||
] {
|
||||
assert!(
|
||||
script_token(command, shell_type).is_none(),
|
||||
"unexpected shell expansion attribution for {shell_type:?}: {command}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_zsh_equals_expansion_candidates() {
|
||||
for command in ["python =foo", "node =foo", "=foo"] {
|
||||
assert!(
|
||||
script_token(command, ShellType::Zsh).is_none(),
|
||||
"unexpected Zsh EQUALS attribution for {command}"
|
||||
);
|
||||
}
|
||||
|
||||
for shell_type in [ShellType::Bash, ShellType::Sh] {
|
||||
for command in ["python =foo", "node =foo"] {
|
||||
assert_eq!(
|
||||
script_token(command, shell_type),
|
||||
Some("=foo".to_string()),
|
||||
"unexpected literal equals rejection for {shell_type:?}: {command}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_compound_commands_and_runner_options() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
for command in [
|
||||
"python skills/demo/scripts/run.py && python skills/demo/scripts/run.py",
|
||||
"python -c skills/demo/scripts/run.py",
|
||||
"node --loader skills/demo/scripts/loader.js skills/demo/scripts/run.js",
|
||||
"env -C skills/demo python scripts/run.py",
|
||||
"timeout 1 python skills/demo/scripts/run.py",
|
||||
"uv run skills/demo/scripts/run.py",
|
||||
"cd skills/demo && python scripts/run.py",
|
||||
r#"C:\tmp\python.exe skills/demo/scripts/run.py"#,
|
||||
] {
|
||||
assert!(
|
||||
resolve(&roots, &SkillLoadOutcome::default(), command, &root).is_none(),
|
||||
"unexpected lifecycle attribution for {command}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(windows))]
|
||||
fn direct_executable_matching_interpreter_name_is_case_sensitive() {
|
||||
let (_temp, root, roots) = fixture();
|
||||
fs::write(root.join("Python"), "#!/bin/sh\n").expect("write case-sensitive executable");
|
||||
|
||||
let resolved = resolve(&roots, &SkillLoadOutcome::default(), "./Python", &root)
|
||||
.expect("case-sensitive direct executable");
|
||||
assert_eq!(resolved.script_path, "Python");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn powershell_split_preserves_paths_and_rejects_compounds() {
|
||||
assert_eq!(
|
||||
command_tokens(
|
||||
r#"pwsh.exe -File C:\Users\me\plugin\scripts\run.ps1"#,
|
||||
ShellType::PowerShell,
|
||||
),
|
||||
Some(vec![
|
||||
"pwsh.exe".to_string(),
|
||||
"-File".to_string(),
|
||||
r#"C:\Users\me\plugin\scripts\run.ps1"#.to_string(),
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
command_tokens(
|
||||
r#"& 'C:\Program Files\plugin\scripts\run.ps1'"#,
|
||||
ShellType::PowerShell,
|
||||
),
|
||||
Some(vec![
|
||||
r#"C:\Program Files\plugin\scripts\run.ps1"#.to_string()
|
||||
])
|
||||
);
|
||||
assert!(
|
||||
command_tokens(
|
||||
r#"'C:\Program Files\plugin\scripts\run.ps1'"#,
|
||||
ShellType::PowerShell
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
for command in [
|
||||
r#"& 'C:\plugin`name\scripts\run.ps1'"#,
|
||||
r#"& "C:\plugin` name\scripts\run.ps1""#,
|
||||
r#"& "C:\plugin`u{2e}\scripts\run.ps1""#,
|
||||
] {
|
||||
assert!(
|
||||
command_tokens(command, ShellType::PowerShell).is_none(),
|
||||
"unexpected PowerShell backtick attribution for {command}"
|
||||
);
|
||||
}
|
||||
for command in [
|
||||
r#"python "$HOME\run.py""#,
|
||||
r#"python $HOME\run.py"#,
|
||||
r#"& "$HOME\run.ps1""#,
|
||||
r#"& ~\run.ps1"#,
|
||||
"python (Get-Command outside-script).Source",
|
||||
"python {outside-script}",
|
||||
"python @(outside-script)",
|
||||
"python scripts/run.py > outside.txt",
|
||||
"python scripts/run.py # outside",
|
||||
] {
|
||||
assert!(
|
||||
command_tokens(command, ShellType::PowerShell).is_none(),
|
||||
"unexpected PowerShell expansion attribution for {command}"
|
||||
);
|
||||
}
|
||||
assert!(command_tokens("python a.py; python b.py", ShellType::PowerShell).is_none());
|
||||
assert_eq!(
|
||||
command_tokens(r#"C:\plugin\scripts\run.cmd"#, ShellType::Cmd),
|
||||
Some(vec![r#"C:\plugin\scripts\run.cmd"#.to_string()])
|
||||
);
|
||||
assert_eq!(
|
||||
command_tokens(
|
||||
r#""C:\Program Files\plugin\scripts\run.bat""#,
|
||||
ShellType::Cmd
|
||||
),
|
||||
Some(vec![
|
||||
r#"C:\Program Files\plugin\scripts\run.bat"#.to_string()
|
||||
])
|
||||
);
|
||||
assert!(command_tokens("python run.py`&whoami", ShellType::Cmd).is_none());
|
||||
assert!(command_tokens(r#"'C:\plugin\scripts\run.cmd'"#, ShellType::Cmd).is_none());
|
||||
assert!(script_token(r#"C:\tmp\python.exe run.py"#, ShellType::Cmd).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn rejects_non_utf8_normalized_paths() {
|
||||
use std::ffi::OsString;
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
|
||||
let path = OsString::from_vec(b"scripts/run\xff.py".to_vec());
|
||||
assert_eq!(normalized_relative_path(Path::new(&path)), None);
|
||||
}
|
||||
@@ -136,6 +136,7 @@ pub(super) async fn spawn_review_thread(
|
||||
turn_metadata_state,
|
||||
extension_data,
|
||||
turn_skills: TurnSkillsContext::new(parent_turn_context.turn_skills.snapshot.clone()),
|
||||
first_party_plugin_roots: Arc::clone(&parent_turn_context.first_party_plugin_roots),
|
||||
turn_timing_state: Arc::new(TurnTimingState::default()),
|
||||
terminal_error: Arc::new(Mutex::new(None)),
|
||||
server_model_warning_emitted: AtomicBool::new(false),
|
||||
|
||||
@@ -5048,6 +5048,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
.plugins_manager
|
||||
.plugins_for_config(&per_turn_config.plugins_config_input())
|
||||
.await;
|
||||
let first_party_plugin_roots = plugin_outcome.effective_first_party_plugin_roots();
|
||||
let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots();
|
||||
let skills_input =
|
||||
crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots);
|
||||
@@ -5075,6 +5076,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
session_configuration.cwd().clone(),
|
||||
"turn_id".to_string(),
|
||||
skills_snapshot,
|
||||
first_party_plugin_roots,
|
||||
);
|
||||
|
||||
let session = Session {
|
||||
@@ -7091,6 +7093,7 @@ where
|
||||
.plugins_manager
|
||||
.plugins_for_config(&per_turn_config.plugins_config_input())
|
||||
.await;
|
||||
let first_party_plugin_roots = plugin_outcome.effective_first_party_plugin_roots();
|
||||
let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots();
|
||||
let skills_input =
|
||||
crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots);
|
||||
@@ -7118,6 +7121,7 @@ where
|
||||
session_configuration.cwd().clone(),
|
||||
"turn_id".to_string(),
|
||||
skills_snapshot,
|
||||
first_party_plugin_roots,
|
||||
));
|
||||
|
||||
let session = Arc::new(Session {
|
||||
|
||||
@@ -6,6 +6,7 @@ use codex_core_skills::HostSkillsSnapshot;
|
||||
use codex_file_system::FileSystemSandboxContext;
|
||||
use codex_model_provider::SharedModelProvider;
|
||||
use codex_model_provider::create_model_provider;
|
||||
use codex_plugin::FirstPartyPluginRoot;
|
||||
use codex_protocol::SessionId;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::AdditionalPermissionProfile;
|
||||
@@ -138,6 +139,7 @@ pub struct TurnContext {
|
||||
pub(crate) turn_metadata_state: Arc<TurnMetadataState>,
|
||||
pub(crate) extension_data: Arc<codex_extension_api::ExtensionData>,
|
||||
pub(crate) turn_skills: TurnSkillsContext,
|
||||
pub(crate) first_party_plugin_roots: Arc<Vec<FirstPartyPluginRoot>>,
|
||||
pub(crate) turn_timing_state: Arc<TurnTimingState>,
|
||||
pub(crate) terminal_error: Arc<Mutex<Option<String>>>,
|
||||
pub(crate) server_model_warning_emitted: AtomicBool,
|
||||
@@ -289,6 +291,7 @@ impl TurnContext {
|
||||
turn_metadata_state: self.turn_metadata_state.clone(),
|
||||
extension_data: Arc::clone(&self.extension_data),
|
||||
turn_skills: self.turn_skills.clone(),
|
||||
first_party_plugin_roots: Arc::clone(&self.first_party_plugin_roots),
|
||||
turn_timing_state: Arc::clone(&self.turn_timing_state),
|
||||
terminal_error: Arc::clone(&self.terminal_error),
|
||||
server_model_warning_emitted: AtomicBool::new(
|
||||
@@ -494,6 +497,7 @@ impl Session {
|
||||
cwd: AbsolutePathBuf,
|
||||
sub_id: String,
|
||||
skills_snapshot: HostSkillsSnapshot,
|
||||
first_party_plugin_roots: Vec<FirstPartyPluginRoot>,
|
||||
) -> TurnContext {
|
||||
let reasoning_effort = session_configuration.collaboration_mode.reasoning_effort();
|
||||
let reasoning_summary = session_configuration
|
||||
@@ -575,6 +579,7 @@ impl Session {
|
||||
turn_metadata_state,
|
||||
extension_data,
|
||||
turn_skills: TurnSkillsContext::new(skills_snapshot),
|
||||
first_party_plugin_roots: Arc::new(first_party_plugin_roots),
|
||||
turn_timing_state: Arc::new(TurnTimingState::default()),
|
||||
terminal_error: Arc::new(Mutex::new(None)),
|
||||
server_model_warning_emitted: AtomicBool::new(false),
|
||||
@@ -727,6 +732,7 @@ impl Session {
|
||||
.plugins_manager
|
||||
.plugins_for_config(&per_turn_config.plugins_config_input())
|
||||
.await;
|
||||
let first_party_plugin_roots = plugin_outcome.effective_first_party_plugin_roots();
|
||||
let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots();
|
||||
let skills_input = skills_load_input_from_config(&per_turn_config, effective_skill_roots);
|
||||
let fs = primary_turn_environment
|
||||
@@ -764,6 +770,7 @@ impl Session {
|
||||
cwd,
|
||||
sub_id,
|
||||
skills_snapshot,
|
||||
first_party_plugin_roots,
|
||||
);
|
||||
turn_context.realtime_active = self.conversation.running_state().await.is_some();
|
||||
|
||||
|
||||
@@ -124,6 +124,7 @@ pub(crate) fn spawn_exit_watcher(
|
||||
|
||||
let duration = Instant::now().saturating_duration_since(started_at);
|
||||
if let Some(message) = process.failure_message() {
|
||||
process.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);
|
||||
emit_failed_exec_end_for_unified_exec(
|
||||
session_ref,
|
||||
turn_ref,
|
||||
@@ -139,6 +140,7 @@ pub(crate) fn spawn_exit_watcher(
|
||||
.await;
|
||||
} else {
|
||||
let exit_code = process.exit_code().unwrap_or(-1);
|
||||
process.notify_lifecycle_finished(Some(exit_code), /*failed*/ false);
|
||||
emit_exec_end_for_unified_exec(
|
||||
session_ref,
|
||||
turn_ref,
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::session::turn_context::TurnContext;
|
||||
use crate::tools::context::ExecCommandToolOutput;
|
||||
use crate::unified_exec::WriteStdinRequest;
|
||||
use crate::unified_exec::process::OutputHandles;
|
||||
use crate::unified_exec::process_manager::INTERRUPT;
|
||||
use codex_exec_server::ExecProcess;
|
||||
use codex_exec_server::ExecProcessEventReceiver;
|
||||
use codex_exec_server::ExecProcessFuture;
|
||||
@@ -30,6 +31,8 @@ use pretty_assertions::assert_eq;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::sync::watch;
|
||||
use tokio::time::Duration;
|
||||
@@ -203,12 +206,28 @@ async fn exec_command_with_tty(
|
||||
#[derive(Debug)]
|
||||
struct TestSpawnLifecycle {
|
||||
inherited_fds: Vec<i32>,
|
||||
after_spawn: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl SpawnLifecycle for TestSpawnLifecycle {
|
||||
fn inherited_fds(&self) -> Vec<i32> {
|
||||
self.inherited_fds.clone()
|
||||
}
|
||||
|
||||
fn after_spawn(&mut self) {
|
||||
self.after_spawn.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RecordingCancellationLifecycle {
|
||||
cancelled: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl SpawnLifecycle for RecordingCancellationLifecycle {
|
||||
fn mark_cancelled(&self) {
|
||||
self.cancelled.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
struct BlockingTerminateExecProcess {
|
||||
@@ -216,6 +235,7 @@ struct BlockingTerminateExecProcess {
|
||||
terminate_started: watch::Sender<bool>,
|
||||
allow_terminate: Arc<Notify>,
|
||||
wake_tx: watch::Sender<u64>,
|
||||
signal_error: Option<String>,
|
||||
}
|
||||
|
||||
impl BlockingTerminateExecProcess {
|
||||
@@ -270,7 +290,14 @@ impl ExecProcess for BlockingTerminateExecProcess {
|
||||
}
|
||||
|
||||
fn signal(&self, _signal: ProcessSignal) -> ExecProcessFuture<'_, ()> {
|
||||
Box::pin(async { Ok(()) })
|
||||
Box::pin(async {
|
||||
match self.signal_error.as_ref() {
|
||||
Some(signal_error) => Err(codex_exec_server::ExecServerError::Protocol(
|
||||
signal_error.clone(),
|
||||
)),
|
||||
None => Ok(()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn terminate(&self) -> ExecProcessFuture<'_, ()> {
|
||||
@@ -282,6 +309,8 @@ async fn blocking_terminate_unified_process(
|
||||
process_id: i32,
|
||||
terminate_started: watch::Sender<bool>,
|
||||
allow_terminate: Arc<Notify>,
|
||||
signal_error: Option<String>,
|
||||
spawn_lifecycle: SpawnLifecycleHandle,
|
||||
) -> anyhow::Result<Arc<UnifiedExecProcess>> {
|
||||
let (wake_tx, _wake_rx) = watch::channel(0);
|
||||
Ok(Arc::new(
|
||||
@@ -292,9 +321,11 @@ async fn blocking_terminate_unified_process(
|
||||
terminate_started,
|
||||
allow_terminate,
|
||||
wake_tx,
|
||||
signal_error,
|
||||
}),
|
||||
},
|
||||
SandboxType::None,
|
||||
spawn_lifecycle,
|
||||
)
|
||||
.await?,
|
||||
))
|
||||
@@ -665,6 +696,8 @@ async fn terminating_initial_exec_command_rechecks_initial_response_state() -> a
|
||||
process_id,
|
||||
terminate_started_tx,
|
||||
Arc::clone(&allow_terminate),
|
||||
/*signal_error*/ None,
|
||||
Box::new(NoopSpawnLifecycle),
|
||||
)
|
||||
.await?;
|
||||
#[allow(deprecated)]
|
||||
@@ -737,6 +770,8 @@ async fn terminating_during_stdin_poll_returns_exited_response() -> anyhow::Resu
|
||||
process_id,
|
||||
terminate_started_tx,
|
||||
Arc::clone(&allow_terminate),
|
||||
/*signal_error*/ None,
|
||||
Box::new(NoopSpawnLifecycle),
|
||||
)
|
||||
.await?;
|
||||
#[allow(deprecated)]
|
||||
@@ -796,6 +831,106 @@ async fn terminating_during_stdin_poll_returns_exited_response() -> anyhow::Resu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn remote_interrupt_failure_does_not_mark_lifecycle_cancelled() -> anyhow::Result<()> {
|
||||
let (session, turn) = test_session_and_turn().await;
|
||||
let manager = &session.services.unified_exec_manager;
|
||||
let process_id = manager.allocate_process_id().await;
|
||||
let (terminate_started_tx, _terminate_started_rx) = watch::channel(false);
|
||||
let allow_terminate = Arc::new(Notify::new());
|
||||
let cancelled = Arc::new(AtomicBool::new(false));
|
||||
let process = blocking_terminate_unified_process(
|
||||
process_id,
|
||||
terminate_started_tx,
|
||||
Arc::clone(&allow_terminate),
|
||||
Some("interrupt unavailable".to_string()),
|
||||
Box::new(RecordingCancellationLifecycle {
|
||||
cancelled: Arc::clone(&cancelled),
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
#[allow(deprecated)]
|
||||
let cwd = turn.cwd.clone();
|
||||
manager.process_store.lock().await.processes.insert(
|
||||
process_id,
|
||||
ProcessEntry {
|
||||
process: Arc::clone(&process),
|
||||
call_id: "call".to_string(),
|
||||
process_id,
|
||||
cwd: cwd.into(),
|
||||
initial_exec_command_active: Arc::new(std::sync::atomic::AtomicBool::new(false)),
|
||||
hook_command: "sleep 60".to_string(),
|
||||
tty: false,
|
||||
network_approval: None,
|
||||
session: Arc::downgrade(&session),
|
||||
last_used: Instant::now(),
|
||||
},
|
||||
);
|
||||
|
||||
let err = write_stdin(&session, process_id, INTERRUPT, /*yield_time_ms*/ 100)
|
||||
.await
|
||||
.expect_err("remote interrupt should fail");
|
||||
assert!(matches!(
|
||||
err,
|
||||
UnifiedExecError::ProcessFailed { message }
|
||||
if message.contains("interrupt unavailable")
|
||||
));
|
||||
assert!(!cancelled.load(Ordering::SeqCst));
|
||||
|
||||
manager.release_process_id(process_id).await;
|
||||
allow_terminate.notify_one();
|
||||
process.terminate_confirmed().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn local_spawn_lifecycle_runs_only_after_successful_creation() -> anyhow::Result<()> {
|
||||
let (_, turn) = make_session_and_context().await;
|
||||
#[allow(deprecated)]
|
||||
let cwd = turn.cwd.clone();
|
||||
let environment = codex_exec_server::Environment::default_for_tests();
|
||||
let manager = UnifiedExecProcessManager::default();
|
||||
|
||||
let after_spawn = Arc::new(AtomicBool::new(false));
|
||||
let request = test_exec_request(
|
||||
&turn,
|
||||
vec!["bash".to_string(), "-lc".to_string(), "true".to_string()],
|
||||
cwd.clone(),
|
||||
shell_env(),
|
||||
);
|
||||
manager
|
||||
.open_session_with_exec_env(
|
||||
/*process_id*/ 1234,
|
||||
&request,
|
||||
/*tty*/ false,
|
||||
Box::new(TestSpawnLifecycle {
|
||||
inherited_fds: Vec::new(),
|
||||
after_spawn: Arc::clone(&after_spawn),
|
||||
}),
|
||||
&environment,
|
||||
)
|
||||
.await?;
|
||||
assert!(after_spawn.load(Ordering::SeqCst));
|
||||
|
||||
let after_failed_spawn = Arc::new(AtomicBool::new(false));
|
||||
let request = test_exec_request(&turn, Vec::new(), cwd, shell_env());
|
||||
manager
|
||||
.open_session_with_exec_env(
|
||||
/*process_id*/ 1235,
|
||||
&request,
|
||||
/*tty*/ false,
|
||||
Box::new(TestSpawnLifecycle {
|
||||
inherited_fds: Vec::new(),
|
||||
after_spawn: Arc::clone(&after_failed_spawn),
|
||||
}),
|
||||
&environment,
|
||||
)
|
||||
.await
|
||||
.expect_err("missing command should fail before spawn");
|
||||
assert!(!after_failed_spawn.load(Ordering::SeqCst));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn completed_pipe_commands_preserve_exit_code() -> anyhow::Result<()> {
|
||||
let (_, turn) = make_session_and_context().await;
|
||||
@@ -908,6 +1043,7 @@ async fn remote_exec_server_rejects_inherited_fd_launches() -> anyhow::Result<()
|
||||
);
|
||||
|
||||
let manager = UnifiedExecProcessManager::default();
|
||||
let after_spawn = Arc::new(AtomicBool::new(false));
|
||||
let err = manager
|
||||
.open_session_with_exec_env(
|
||||
/*process_id*/ 1234,
|
||||
@@ -915,6 +1051,7 @@ async fn remote_exec_server_rejects_inherited_fd_launches() -> anyhow::Result<()
|
||||
/*tty*/ true,
|
||||
Box::new(TestSpawnLifecycle {
|
||||
inherited_fds: vec![42],
|
||||
after_spawn: Arc::clone(&after_spawn),
|
||||
}),
|
||||
turn.environments
|
||||
.primary()
|
||||
@@ -929,5 +1066,6 @@ async fn remote_exec_server_rejects_inherited_fd_launches() -> anyhow::Result<()
|
||||
err.to_string(),
|
||||
"Failed to create unified exec process: remote exec-server does not support inherited file descriptors"
|
||||
);
|
||||
assert!(!after_spawn.load(Ordering::SeqCst));
|
||||
Ok(())
|
||||
}
|
||||
@@ -33,6 +33,11 @@ use super::head_tail_buffer::HeadTailBuffer;
|
||||
use super::process_state::ProcessState;
|
||||
|
||||
const EARLY_EXIT_GRACE_PERIOD: Duration = Duration::from_millis(150);
|
||||
/// Process lifecycle hooks for successful spawn and terminal observation.
|
||||
///
|
||||
/// Default methods are no-op. `after_spawn` runs only after child creation
|
||||
/// succeeds. The manager, watcher, and `Drop` can all observe terminal state,
|
||||
/// so implementations must make terminal emission idempotent.
|
||||
pub(crate) trait SpawnLifecycle: std::fmt::Debug + Send + Sync {
|
||||
/// Returns file descriptors that must stay open across the child `exec()`.
|
||||
///
|
||||
@@ -43,7 +48,14 @@ pub(crate) trait SpawnLifecycle: std::fmt::Debug + Send + Sync {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Releases parent-only spawn resources after successful child creation.
|
||||
fn after_spawn(&mut self) {}
|
||||
|
||||
/// Records cancellation when orchestration stops a still-live process.
|
||||
fn mark_cancelled(&self) {}
|
||||
|
||||
/// Records the process terminal state; implementations must be idempotent.
|
||||
fn finish(&self, _exit_code: Option<i32>, _failed: bool) {}
|
||||
}
|
||||
|
||||
pub(crate) type SpawnLifecycleHandle = Box<dyn SpawnLifecycle>;
|
||||
@@ -85,7 +97,7 @@ pub(crate) struct UnifiedExecProcess {
|
||||
state_rx: watch::Receiver<ProcessState>,
|
||||
output_task: Option<JoinHandle<()>>,
|
||||
sandbox_type: SandboxType,
|
||||
_spawn_lifecycle: Option<SpawnLifecycleHandle>,
|
||||
spawn_lifecycle: Option<SpawnLifecycleHandle>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for UnifiedExecProcess {
|
||||
@@ -126,7 +138,19 @@ impl UnifiedExecProcess {
|
||||
state_rx,
|
||||
output_task: None,
|
||||
sandbox_type,
|
||||
_spawn_lifecycle: spawn_lifecycle,
|
||||
spawn_lifecycle,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn notify_lifecycle_cancelled(&self) {
|
||||
if let Some(spawn_lifecycle) = self.spawn_lifecycle.as_ref() {
|
||||
spawn_lifecycle.mark_cancelled();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn notify_lifecycle_finished(&self, exit_code: Option<i32>, failed: bool) {
|
||||
if let Some(spawn_lifecycle) = self.spawn_lifecycle.as_ref() {
|
||||
spawn_lifecycle.finish(exit_code, failed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +251,7 @@ impl UnifiedExecProcess {
|
||||
.map_err(|err| UnifiedExecError::process_failed(err.to_string()))?;
|
||||
}
|
||||
}
|
||||
self.notify_lifecycle_cancelled();
|
||||
self.signal_exit(self.exit_code());
|
||||
self.finish_termination();
|
||||
Ok(())
|
||||
@@ -249,6 +274,7 @@ impl UnifiedExecProcess {
|
||||
if state.failure_message.is_none() {
|
||||
let _ = self.state_tx.send_replace(state.failed(message));
|
||||
}
|
||||
self.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);
|
||||
self.terminate();
|
||||
}
|
||||
|
||||
@@ -307,6 +333,8 @@ impl UnifiedExecProcess {
|
||||
} else {
|
||||
snippet
|
||||
};
|
||||
let state = self.state_rx.borrow().clone();
|
||||
let _ = self.state_tx.send_replace(state.failed(message.clone()));
|
||||
return Err(UnifiedExecError::sandbox_denied(message, exec_output));
|
||||
}
|
||||
Ok(())
|
||||
@@ -375,9 +403,10 @@ impl UnifiedExecProcess {
|
||||
pub(super) async fn from_exec_server_started(
|
||||
started: StartedExecProcess,
|
||||
sandbox_type: SandboxType,
|
||||
spawn_lifecycle: SpawnLifecycleHandle,
|
||||
) -> Result<Self, UnifiedExecError> {
|
||||
let process_handle = ProcessHandle::ExecServer(Arc::clone(&started.process));
|
||||
let mut managed = Self::new(process_handle, sandbox_type, /*spawn_lifecycle*/ None);
|
||||
let mut managed = Self::new(process_handle, sandbox_type, Some(spawn_lifecycle));
|
||||
let output_handles = managed.output_handles();
|
||||
managed.output_task = Some(Self::spawn_exec_server_output_task(
|
||||
started,
|
||||
@@ -534,6 +563,12 @@ impl UnifiedExecProcess {
|
||||
|
||||
impl Drop for UnifiedExecProcess {
|
||||
fn drop(&mut self) {
|
||||
let has_exited = self.has_exited();
|
||||
if !has_exited {
|
||||
self.notify_lifecycle_cancelled();
|
||||
}
|
||||
let failed = self.state_rx.borrow().failure_message.is_some();
|
||||
self.notify_lifecycle_finished(self.exit_code(), failed || !has_exited);
|
||||
self.terminate();
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ const UNIFIED_EXEC_ENV: [(&str, &str); 10] = [
|
||||
const NETWORK_ACCESS_DENIED_MESSAGE: &str =
|
||||
"Network access was denied by the Codex sandbox network proxy.";
|
||||
const LATE_NETWORK_DENIAL_GRACE_PERIOD: Duration = Duration::from_millis(100);
|
||||
const INTERRUPT: &str = "\u{3}";
|
||||
pub(super) const INTERRUPT: &str = "\u{3}";
|
||||
|
||||
/// Test-only override for deterministic unified exec process IDs.
|
||||
///
|
||||
@@ -280,6 +280,7 @@ async fn finish_deferred_network_approval_after_process_exit_for_session(
|
||||
|
||||
fn fail_process_with_message(process: &UnifiedExecProcess, message: String) -> UnifiedExecError {
|
||||
if let Some(message) = process.failure_message() {
|
||||
process.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);
|
||||
process.terminate();
|
||||
return UnifiedExecError::process_failed(message);
|
||||
}
|
||||
@@ -503,6 +504,7 @@ impl UnifiedExecProcessManager {
|
||||
return Err(fail_process_with_message(process.as_ref(), message));
|
||||
}
|
||||
if let Some(message) = process.failure_message() {
|
||||
process.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);
|
||||
let finish_result = finish_deferred_network_approval_for_session(
|
||||
Some(&context.session),
|
||||
deferred_network_approval.take(),
|
||||
@@ -576,6 +578,7 @@ impl UnifiedExecProcessManager {
|
||||
}
|
||||
let exit_code = process.exit_code();
|
||||
let exit = exit_code.unwrap_or(-1);
|
||||
process.notify_lifecycle_finished(exit_code, /*failed*/ false);
|
||||
emit_exec_end_for_unified_exec(
|
||||
Arc::clone(&context.session),
|
||||
Arc::clone(&context.turn),
|
||||
@@ -640,6 +643,7 @@ impl UnifiedExecProcessManager {
|
||||
if !tty {
|
||||
if request.input == INTERRUPT {
|
||||
process.interrupt().await?;
|
||||
process.notify_lifecycle_cancelled();
|
||||
} else {
|
||||
return Err(UnifiedExecError::StdinClosed);
|
||||
}
|
||||
@@ -655,6 +659,9 @@ impl UnifiedExecProcessManager {
|
||||
if matches!(status, ProcessStatus::Exited { .. }) {
|
||||
status_after_write = Some(status);
|
||||
} else if matches!(err, UnifiedExecError::ProcessFailed { .. }) {
|
||||
process.notify_lifecycle_finished(
|
||||
/*exit_code*/ None, /*failed*/ true,
|
||||
);
|
||||
process.terminate();
|
||||
self.release_process_id(process_id).await;
|
||||
return Err(err);
|
||||
@@ -704,6 +711,7 @@ impl UnifiedExecProcessManager {
|
||||
return Err(fail_process_with_message(process.as_ref(), message));
|
||||
}
|
||||
if let Some(message) = process.failure_message() {
|
||||
process.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);
|
||||
let finish_result = finish_deferred_network_approval_for_session(
|
||||
session.as_ref(),
|
||||
network_approval.clone(),
|
||||
@@ -870,6 +878,9 @@ impl UnifiedExecProcessManager {
|
||||
// network-approval cleanup only after dropping that lock.
|
||||
if let Some(pruned_entry) = pruned_entry {
|
||||
unregister_network_approval_for_entry(&pruned_entry).await;
|
||||
if !pruned_entry.process.has_exited() {
|
||||
pruned_entry.process.notify_lifecycle_cancelled();
|
||||
}
|
||||
pruned_entry.process.terminate();
|
||||
}
|
||||
|
||||
@@ -974,13 +985,11 @@ impl UnifiedExecProcessManager {
|
||||
.await
|
||||
}
|
||||
};
|
||||
let spawned =
|
||||
spawned.map_err(|err| UnifiedExecError::create_process(err.to_string()))?;
|
||||
spawn_lifecycle.after_spawn();
|
||||
return UnifiedExecProcess::from_spawned(
|
||||
spawned.map_err(|err| UnifiedExecError::create_process(err.to_string()))?,
|
||||
request.sandbox,
|
||||
spawn_lifecycle,
|
||||
)
|
||||
.await;
|
||||
return UnifiedExecProcess::from_spawned(spawned, request.sandbox, spawn_lifecycle)
|
||||
.await;
|
||||
}
|
||||
if environment.is_remote() {
|
||||
if !inherited_fds.is_empty() {
|
||||
@@ -995,7 +1004,12 @@ impl UnifiedExecProcessManager {
|
||||
.await
|
||||
.map_err(|err| UnifiedExecError::create_process(err.to_string()))?;
|
||||
spawn_lifecycle.after_spawn();
|
||||
return UnifiedExecProcess::from_exec_server_started(started, request.sandbox).await;
|
||||
return UnifiedExecProcess::from_exec_server_started(
|
||||
started,
|
||||
request.sandbox,
|
||||
spawn_lifecycle,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
// TODO(anp): Keep PathUri through the local PTY/process launch boundary.
|
||||
@@ -1322,6 +1336,9 @@ impl UnifiedExecProcessManager {
|
||||
|
||||
for entry in entries {
|
||||
unregister_network_approval_for_entry(&entry).await;
|
||||
if !entry.process.has_exited() {
|
||||
entry.process.notify_lifecycle_cancelled();
|
||||
}
|
||||
entry.process.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
use super::process::NoopSpawnLifecycle;
|
||||
use super::process::SpawnLifecycle;
|
||||
use super::process::UnifiedExecProcess;
|
||||
use crate::unified_exec::UnifiedExecError;
|
||||
use codex_exec_server::ExecOutputStream;
|
||||
use codex_exec_server::ExecProcess;
|
||||
use codex_exec_server::ExecProcessEventReceiver;
|
||||
use codex_exec_server::ExecProcessFuture;
|
||||
use codex_exec_server::ExecServerError;
|
||||
use codex_exec_server::ProcessId;
|
||||
use codex_exec_server::ProcessOutputChunk;
|
||||
use codex_exec_server::ProcessSignal;
|
||||
use codex_exec_server::ReadResponse;
|
||||
use codex_exec_server::StartedExecProcess;
|
||||
@@ -14,10 +18,42 @@ use codex_sandboxing::SandboxType;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::watch;
|
||||
use tokio::time::Duration;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct RecordingLifecycleState {
|
||||
cancelled: AtomicBool,
|
||||
finishes: std::sync::Mutex<Vec<(Option<i32>, bool)>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RecordingLifecycle {
|
||||
state: Arc<RecordingLifecycleState>,
|
||||
}
|
||||
|
||||
impl SpawnLifecycle for RecordingLifecycle {
|
||||
fn mark_cancelled(&self) {
|
||||
self.state.cancelled.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn finish(&self, exit_code: Option<i32>, failed: bool) {
|
||||
self.state
|
||||
.finishes
|
||||
.lock()
|
||||
.expect("finish state")
|
||||
.push((exit_code, failed));
|
||||
}
|
||||
}
|
||||
|
||||
fn recording_lifecycle() -> (Arc<RecordingLifecycleState>, Box<RecordingLifecycle>) {
|
||||
let state = Arc::new(RecordingLifecycleState::default());
|
||||
(Arc::clone(&state), Box::new(RecordingLifecycle { state }))
|
||||
}
|
||||
|
||||
struct MockExecProcess {
|
||||
process_id: ProcessId,
|
||||
write_response: WriteResponse,
|
||||
@@ -86,9 +122,10 @@ impl ExecProcess for MockExecProcess {
|
||||
}
|
||||
}
|
||||
|
||||
async fn remote_process(
|
||||
async fn remote_process_with_lifecycle(
|
||||
write_status: WriteStatus,
|
||||
terminate_error: Option<String>,
|
||||
spawn_lifecycle: Box<dyn SpawnLifecycle>,
|
||||
) -> UnifiedExecProcess {
|
||||
let (wake_tx, _wake_rx) = watch::channel(0);
|
||||
let started = StartedExecProcess {
|
||||
@@ -103,11 +140,18 @@ async fn remote_process(
|
||||
}),
|
||||
};
|
||||
|
||||
UnifiedExecProcess::from_exec_server_started(started, SandboxType::None)
|
||||
UnifiedExecProcess::from_exec_server_started(started, SandboxType::None, spawn_lifecycle)
|
||||
.await
|
||||
.expect("remote process should start")
|
||||
}
|
||||
|
||||
async fn remote_process(
|
||||
write_status: WriteStatus,
|
||||
terminate_error: Option<String>,
|
||||
) -> UnifiedExecProcess {
|
||||
remote_process_with_lifecycle(write_status, terminate_error, Box::new(NoopSpawnLifecycle)).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_write_unknown_process_marks_process_exited() {
|
||||
let process = remote_process(WriteStatus::UnknownProcess, /*terminate_error*/ None).await;
|
||||
@@ -148,11 +192,81 @@ async fn fail_and_terminate_preserves_failure_message() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fail_and_terminate_forwards_terminal_failure() {
|
||||
let (state, lifecycle) = recording_lifecycle();
|
||||
let process = remote_process_with_lifecycle(
|
||||
WriteStatus::Accepted,
|
||||
/*terminate_error*/ None,
|
||||
lifecycle,
|
||||
)
|
||||
.await;
|
||||
|
||||
process.fail_and_terminate("network denied".to_string());
|
||||
|
||||
assert_eq!(
|
||||
*state.finishes.lock().expect("finish state"),
|
||||
vec![(None, true)]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_live_process_marks_cancelled_and_failed() {
|
||||
let (state, lifecycle) = recording_lifecycle();
|
||||
let process = remote_process_with_lifecycle(
|
||||
WriteStatus::Accepted,
|
||||
/*terminate_error*/ None,
|
||||
lifecycle,
|
||||
)
|
||||
.await;
|
||||
|
||||
drop(process);
|
||||
|
||||
assert!(state.cancelled.load(Ordering::SeqCst));
|
||||
assert_eq!(
|
||||
*state.finishes.lock().expect("finish state"),
|
||||
vec![(None, true)]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_exited_process_does_not_mark_cancelled() {
|
||||
let (state, lifecycle) = recording_lifecycle();
|
||||
let process = remote_process_with_lifecycle(
|
||||
WriteStatus::UnknownProcess,
|
||||
/*terminate_error*/ None,
|
||||
lifecycle,
|
||||
)
|
||||
.await;
|
||||
process
|
||||
.write(b"hello")
|
||||
.await
|
||||
.expect_err("expected write failure");
|
||||
|
||||
drop(process);
|
||||
|
||||
assert!(!state.cancelled.load(Ordering::SeqCst));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn noop_spawn_lifecycle_preserves_process_behavior() {
|
||||
let process = remote_process(WriteStatus::Accepted, /*terminate_error*/ None).await;
|
||||
|
||||
process.fail_and_terminate("network denied".to_string());
|
||||
|
||||
assert_eq!(
|
||||
process.failure_message(),
|
||||
Some("network denied".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_terminate_confirmed_updates_state_on_success_only() {
|
||||
let process = remote_process(
|
||||
let (failed_state, lifecycle) = recording_lifecycle();
|
||||
let process = remote_process_with_lifecycle(
|
||||
WriteStatus::Accepted,
|
||||
Some("terminate unavailable".to_string()),
|
||||
lifecycle,
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -163,8 +277,15 @@ async fn remote_terminate_confirmed_updates_state_on_success_only() {
|
||||
|
||||
assert!(matches!(err, UnifiedExecError::ProcessFailed { .. }));
|
||||
assert!(!process.has_exited());
|
||||
assert!(!failed_state.cancelled.load(Ordering::SeqCst));
|
||||
|
||||
let process = remote_process(WriteStatus::Accepted, /*terminate_error*/ None).await;
|
||||
let (succeeded_state, lifecycle) = recording_lifecycle();
|
||||
let process = remote_process_with_lifecycle(
|
||||
WriteStatus::Accepted,
|
||||
/*terminate_error*/ None,
|
||||
lifecycle,
|
||||
)
|
||||
.await;
|
||||
|
||||
process
|
||||
.terminate_confirmed()
|
||||
@@ -172,6 +293,46 @@ async fn remote_terminate_confirmed_updates_state_on_success_only() {
|
||||
.expect("terminate should succeed");
|
||||
|
||||
assert!(process.has_exited());
|
||||
assert!(succeeded_state.cancelled.load(Ordering::SeqCst));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sandbox_denied_early_exit_finishes_failed() {
|
||||
let (state, lifecycle) = recording_lifecycle();
|
||||
let (wake_tx, _wake_rx) = watch::channel(0);
|
||||
let started = StartedExecProcess {
|
||||
process: Arc::new(MockExecProcess {
|
||||
process_id: "test-process".to_string().into(),
|
||||
write_response: WriteResponse {
|
||||
status: WriteStatus::Accepted,
|
||||
},
|
||||
read_responses: Mutex::new(VecDeque::from([ReadResponse {
|
||||
chunks: vec![ProcessOutputChunk {
|
||||
seq: 1,
|
||||
stream: ExecOutputStream::Stderr,
|
||||
chunk: b"Operation not permitted".to_vec().into(),
|
||||
}],
|
||||
next_seq: 2,
|
||||
exited: true,
|
||||
exit_code: Some(1),
|
||||
closed: true,
|
||||
failure: None,
|
||||
}])),
|
||||
terminate_error: None,
|
||||
wake_tx,
|
||||
}),
|
||||
};
|
||||
|
||||
let err =
|
||||
UnifiedExecProcess::from_exec_server_started(started, SandboxType::LinuxSeccomp, lifecycle)
|
||||
.await
|
||||
.expect_err("sandbox denial should fail startup");
|
||||
|
||||
assert!(matches!(err, UnifiedExecError::SandboxDenied { .. }));
|
||||
assert_eq!(
|
||||
*state.finishes.lock().expect("finish state"),
|
||||
vec![(Some(1), true)]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -201,9 +362,13 @@ async fn remote_process_waits_for_early_exit_event() {
|
||||
let _ = wake_tx.send(1);
|
||||
});
|
||||
|
||||
let process = UnifiedExecProcess::from_exec_server_started(started, SandboxType::None)
|
||||
.await
|
||||
.expect("remote process should observe early exit");
|
||||
let process = UnifiedExecProcess::from_exec_server_started(
|
||||
started,
|
||||
SandboxType::None,
|
||||
Box::new(NoopSpawnLifecycle),
|
||||
)
|
||||
.await
|
||||
.expect("remote process should observe early exit");
|
||||
|
||||
assert!(process.has_exited());
|
||||
assert_eq!(process.exit_code(), Some(17));
|
||||
|
||||
@@ -13,6 +13,7 @@ mod provider;
|
||||
use codex_config::HookEventsToml;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
pub use load_outcome::EffectiveSkillRoots;
|
||||
pub use load_outcome::FirstPartyPluginRoot;
|
||||
pub use load_outcome::LoadedPlugin;
|
||||
pub use load_outcome::PluginLoadOutcome;
|
||||
pub use load_outcome::prompt_safe_plugin_description;
|
||||
|
||||
@@ -11,6 +11,11 @@ use crate::PluginHookSource;
|
||||
use crate::app_connector_ids_from_declarations;
|
||||
|
||||
const MAX_CAPABILITY_SUMMARY_DESCRIPTION_LEN: usize = 1024;
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FirstPartyPluginRoot {
|
||||
pub plugin_id: String,
|
||||
pub plugin_root: AbsolutePathBuf,
|
||||
}
|
||||
|
||||
/// A plugin that was loaded from disk, including merged MCP server definitions.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -18,6 +23,7 @@ pub struct LoadedPlugin<M> {
|
||||
pub config_name: String,
|
||||
pub manifest_name: Option<String>,
|
||||
pub manifest_description: Option<String>,
|
||||
pub is_first_party: bool,
|
||||
pub root: AbsolutePathBuf,
|
||||
pub enabled: bool,
|
||||
pub skill_roots: Vec<AbsolutePathBuf>,
|
||||
@@ -141,6 +147,26 @@ impl<M: Clone> PluginLoadOutcome<M> {
|
||||
skill_roots
|
||||
}
|
||||
|
||||
pub fn effective_first_party_plugin_roots(&self) -> Vec<FirstPartyPluginRoot> {
|
||||
let mut plugin_roots = Vec::new();
|
||||
let mut seen_paths = HashSet::new();
|
||||
for plugin in self
|
||||
.plugins
|
||||
.iter()
|
||||
.filter(|plugin| plugin.is_active() && plugin.is_first_party)
|
||||
{
|
||||
if seen_paths.insert(plugin.root.clone()) {
|
||||
plugin_roots.push(FirstPartyPluginRoot {
|
||||
plugin_id: plugin.config_name.clone(),
|
||||
plugin_root: plugin.root.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
plugin_roots.sort_unstable_by(|a, b| a.plugin_root.cmp(&b.plugin_root));
|
||||
plugin_roots
|
||||
}
|
||||
|
||||
pub fn effective_mcp_servers(&self) -> HashMap<String, M> {
|
||||
let mut mcp_servers = HashMap::new();
|
||||
for plugin in self.plugins.iter().filter(|plugin| plugin.is_active()) {
|
||||
@@ -219,6 +245,7 @@ mod tests {
|
||||
config_name: config_name.to_string(),
|
||||
manifest_name: None,
|
||||
manifest_description: None,
|
||||
is_first_party: false,
|
||||
root: test_path(config_name),
|
||||
enabled: true,
|
||||
skill_roots,
|
||||
@@ -249,4 +276,38 @@ mod tests {
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_first_party_plugin_roots_only_includes_active_openai_plugins() {
|
||||
let shared_root = test_path("shared-plugin");
|
||||
let mut first_party = loaded_plugin("first@test", Vec::new());
|
||||
first_party.root = shared_root.clone();
|
||||
first_party.is_first_party = true;
|
||||
let mut duplicate = loaded_plugin("duplicate@test", Vec::new());
|
||||
duplicate.root = shared_root.clone();
|
||||
duplicate.is_first_party = true;
|
||||
let third_party = loaded_plugin("third@test", Vec::new());
|
||||
let mut disabled = loaded_plugin("disabled@test", Vec::new());
|
||||
disabled.is_first_party = true;
|
||||
disabled.enabled = false;
|
||||
let mut broken = loaded_plugin("broken@test", Vec::new());
|
||||
broken.is_first_party = true;
|
||||
broken.error = Some("broken".to_string());
|
||||
|
||||
let outcome = PluginLoadOutcome::from_plugins(vec![
|
||||
first_party,
|
||||
duplicate,
|
||||
third_party,
|
||||
disabled,
|
||||
broken,
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
outcome.effective_first_party_plugin_roots(),
|
||||
vec![FirstPartyPluginRoot {
|
||||
plugin_id: "first@test".to_string(),
|
||||
plugin_root: shared_root,
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ pub struct ElevatedSandboxProfileCaptureRequest<'a> {
|
||||
pub write_roots_override: Option<&'a [PathBuf]>,
|
||||
pub deny_read_paths_override: &'a [AbsolutePathBuf],
|
||||
pub deny_write_paths_override: &'a [AbsolutePathBuf],
|
||||
pub after_spawn: Option<Box<dyn FnOnce() + Send>>,
|
||||
}
|
||||
|
||||
mod windows_impl {
|
||||
@@ -115,6 +116,7 @@ mod windows_impl {
|
||||
write_roots_override,
|
||||
deny_read_paths_override,
|
||||
deny_write_paths_override,
|
||||
after_spawn,
|
||||
} = request;
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::try_from_permission_profile_for_workspace_roots(
|
||||
@@ -225,6 +227,9 @@ mod windows_impl {
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
if let Some(after_spawn) = after_spawn {
|
||||
after_spawn();
|
||||
}
|
||||
let (pipe_write, mut pipe_read) = transport.into_files();
|
||||
let cancel_writer = spawn_cancel_writer(&pipe_write, cancellation)?;
|
||||
|
||||
|
||||
@@ -475,6 +475,7 @@ mod windows_impl {
|
||||
&[],
|
||||
&[],
|
||||
use_private_desktop,
|
||||
/*after_spawn*/ None,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -491,6 +492,7 @@ mod windows_impl {
|
||||
additional_deny_read_paths: &[AbsolutePathBuf],
|
||||
additional_deny_write_paths: &[AbsolutePathBuf],
|
||||
use_private_desktop: bool,
|
||||
after_spawn: Option<Box<dyn FnOnce() + Send>>,
|
||||
) -> Result<CaptureResult> {
|
||||
let additional_deny_read_paths = additional_deny_read_paths
|
||||
.iter()
|
||||
@@ -576,6 +578,9 @@ mod windows_impl {
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
if let Some(after_spawn) = after_spawn {
|
||||
after_spawn();
|
||||
}
|
||||
let pi = created.process_info;
|
||||
let _desktop = created;
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::ipc_framed::Message;
|
||||
use crate::ipc_framed::decode_bytes;
|
||||
use crate::ipc_framed::read_frame;
|
||||
use crate::run_windows_sandbox_capture;
|
||||
use crate::run_windows_sandbox_capture_with_filesystem_overrides;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_pty::ProcessDriver;
|
||||
@@ -425,7 +426,9 @@ fn legacy_capture_powershell_emits_output() {
|
||||
let codex_home = sandbox_home("legacy-capture-pwsh");
|
||||
println!("capture pwsh codex_home={}", codex_home.path().display());
|
||||
let permission_profile = PermissionProfile::workspace_write();
|
||||
let result = run_windows_sandbox_capture(
|
||||
let spawned = Arc::new(AtomicBool::new(false));
|
||||
let spawned_for_callback = Arc::clone(&spawned);
|
||||
let result = run_windows_sandbox_capture_with_filesystem_overrides(
|
||||
&permission_profile,
|
||||
workspace_roots_for(cwd.as_path()).as_slice(),
|
||||
codex_home.path(),
|
||||
@@ -439,9 +442,15 @@ fn legacy_capture_powershell_emits_output() {
|
||||
HashMap::new(),
|
||||
Some(10_000),
|
||||
/*cancellation*/ None,
|
||||
/*additional_deny_read_paths*/ &[],
|
||||
/*additional_deny_write_paths*/ &[],
|
||||
/*use_private_desktop*/ true,
|
||||
Some(Box::new(move || {
|
||||
spawned_for_callback.store(true, Ordering::SeqCst);
|
||||
})),
|
||||
)
|
||||
.expect("run legacy capture powershell");
|
||||
assert!(spawned.load(Ordering::SeqCst));
|
||||
println!("capture pwsh exit_code={}", result.exit_code);
|
||||
println!("capture pwsh timed_out={}", result.timed_out);
|
||||
let stdout = String::from_utf8_lossy(&result.stdout);
|
||||
|
||||
Reference in new issue
Block a user