mirror of
https://github.com/openai/codex.git
synced 2026-05-04 21:32:21 +03:00
codex: preserve no-executor mode and gate js_repl
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use crate::ExecServerClient;
|
||||
@@ -9,6 +10,8 @@ use crate::file_system::ExecutorFileSystem;
|
||||
use crate::local_file_system::LocalFileSystem;
|
||||
use crate::local_process::LocalProcess;
|
||||
use crate::process::ExecBackend;
|
||||
use crate::process::StartedExecProcess;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::remote_file_system::RemoteFileSystem;
|
||||
use crate::remote_process::RemoteProcess;
|
||||
|
||||
@@ -20,14 +23,21 @@ pub trait ExecutorEnvironment: Send + Sync {
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct EnvironmentManager {
|
||||
exec_server_url: Option<String>,
|
||||
executor_mode: ExecutorMode,
|
||||
current_environment: OnceCell<Arc<Environment>>,
|
||||
}
|
||||
|
||||
impl EnvironmentManager {
|
||||
pub fn new(exec_server_url: Option<String>) -> Self {
|
||||
Self {
|
||||
exec_server_url: normalize_exec_server_url(exec_server_url),
|
||||
executor_mode: parse_executor_mode(exec_server_url),
|
||||
current_environment: OnceCell::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_environment(environment: &Environment) -> Self {
|
||||
Self {
|
||||
executor_mode: environment.executor_mode.clone(),
|
||||
current_environment: OnceCell::new(),
|
||||
}
|
||||
}
|
||||
@@ -37,14 +47,14 @@ impl EnvironmentManager {
|
||||
}
|
||||
|
||||
pub fn exec_server_url(&self) -> Option<&str> {
|
||||
self.exec_server_url.as_deref()
|
||||
self.executor_mode.remote_exec_server_url()
|
||||
}
|
||||
|
||||
pub async fn current(&self) -> Result<Arc<Environment>, ExecServerError> {
|
||||
self.current_environment
|
||||
.get_or_try_init(|| async {
|
||||
Ok(Arc::new(
|
||||
Environment::create(self.exec_server_url.clone()).await?,
|
||||
Environment::create_with_mode(self.executor_mode.clone()).await?,
|
||||
))
|
||||
})
|
||||
.await
|
||||
@@ -52,9 +62,32 @@ impl EnvironmentManager {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
enum ExecutorMode {
|
||||
#[default]
|
||||
LocalExecutor,
|
||||
RemoteExecutor {
|
||||
url: String,
|
||||
},
|
||||
NoExecutor,
|
||||
}
|
||||
|
||||
impl ExecutorMode {
|
||||
fn remote_exec_server_url(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::RemoteExecutor { url } => Some(url.as_str()),
|
||||
Self::LocalExecutor | Self::NoExecutor => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn has_attached_executor(&self) -> bool {
|
||||
!matches!(self, Self::NoExecutor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Environment {
|
||||
exec_server_url: Option<String>,
|
||||
executor_mode: ExecutorMode,
|
||||
remote_exec_server_client: Option<ExecServerClient>,
|
||||
exec_backend: Arc<dyn ExecBackend>,
|
||||
}
|
||||
@@ -70,7 +103,7 @@ impl Default for Environment {
|
||||
}
|
||||
|
||||
Self {
|
||||
exec_server_url: None,
|
||||
executor_mode: ExecutorMode::LocalExecutor,
|
||||
remote_exec_server_client: None,
|
||||
exec_backend: Arc::new(local_process),
|
||||
}
|
||||
@@ -80,18 +113,21 @@ impl Default for Environment {
|
||||
impl std::fmt::Debug for Environment {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Environment")
|
||||
.field("exec_server_url", &self.exec_server_url)
|
||||
.field("executor_mode", &self.executor_mode)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub async fn create(exec_server_url: Option<String>) -> Result<Self, ExecServerError> {
|
||||
let exec_server_url = normalize_exec_server_url(exec_server_url);
|
||||
let remote_exec_server_client = if let Some(url) = &exec_server_url {
|
||||
Self::create_with_mode(parse_executor_mode(exec_server_url)).await
|
||||
}
|
||||
|
||||
async fn create_with_mode(executor_mode: ExecutorMode) -> Result<Self, ExecServerError> {
|
||||
let remote_exec_server_client = if let Some(url) = executor_mode.remote_exec_server_url() {
|
||||
Some(
|
||||
ExecServerClient::connect_websocket(RemoteExecServerConnectArgs {
|
||||
websocket_url: url.clone(),
|
||||
websocket_url: url.to_string(),
|
||||
client_name: "codex-environment".to_string(),
|
||||
connect_timeout: std::time::Duration::from_secs(5),
|
||||
initialize_timeout: std::time::Duration::from_secs(5),
|
||||
@@ -105,6 +141,8 @@ impl Environment {
|
||||
let exec_backend: Arc<dyn ExecBackend> =
|
||||
if let Some(client) = remote_exec_server_client.clone() {
|
||||
Arc::new(RemoteProcess::new(client))
|
||||
} else if matches!(executor_mode, ExecutorMode::NoExecutor) {
|
||||
Arc::new(NoAttachedExecutorBackend)
|
||||
} else {
|
||||
let local_process = LocalProcess::default();
|
||||
local_process
|
||||
@@ -117,14 +155,18 @@ impl Environment {
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
exec_server_url,
|
||||
executor_mode,
|
||||
remote_exec_server_client,
|
||||
exec_backend,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn exec_server_url(&self) -> Option<&str> {
|
||||
self.exec_server_url.as_deref()
|
||||
self.executor_mode.remote_exec_server_url()
|
||||
}
|
||||
|
||||
pub fn has_attached_executor(&self) -> bool {
|
||||
self.executor_mode.has_attached_executor()
|
||||
}
|
||||
|
||||
pub fn get_exec_backend(&self) -> Arc<dyn ExecBackend> {
|
||||
@@ -140,11 +182,26 @@ impl Environment {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_exec_server_url(exec_server_url: Option<String>) -> Option<String> {
|
||||
exec_server_url.and_then(|url| {
|
||||
let url = url.trim();
|
||||
(!url.is_empty()).then(|| url.to_string())
|
||||
})
|
||||
#[derive(Clone, Default)]
|
||||
struct NoAttachedExecutorBackend;
|
||||
|
||||
#[async_trait]
|
||||
impl ExecBackend for NoAttachedExecutorBackend {
|
||||
async fn start(&self, _params: ExecParams) -> Result<StartedExecProcess, ExecServerError> {
|
||||
Err(ExecServerError::Protocol(
|
||||
"no attached executor is configured for this session".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_executor_mode(exec_server_url: Option<String>) -> ExecutorMode {
|
||||
match exec_server_url.as_deref().map(str::trim) {
|
||||
None | Some("") => ExecutorMode::LocalExecutor,
|
||||
Some(url) if url.eq_ignore_ascii_case("none") => ExecutorMode::NoExecutor,
|
||||
Some(url) => ExecutorMode::RemoteExecutor {
|
||||
url: url.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
impl ExecutorEnvironment for Environment {
|
||||
@@ -169,6 +226,8 @@ mod tests {
|
||||
.expect("create environment");
|
||||
|
||||
assert_eq!(environment.exec_server_url(), None);
|
||||
assert!(environment.has_attached_executor());
|
||||
assert_eq!(environment.executor_mode, ExecutorMode::LocalExecutor);
|
||||
assert!(environment.remote_exec_server_client.is_none());
|
||||
}
|
||||
|
||||
@@ -176,7 +235,37 @@ mod tests {
|
||||
fn environment_manager_normalizes_empty_url() {
|
||||
let manager = EnvironmentManager::new(Some(String::new()));
|
||||
|
||||
assert_eq!(manager.exec_server_url(), None);
|
||||
assert_eq!(manager.executor_mode, ExecutorMode::LocalExecutor);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_manager_preserves_no_executor_setting() {
|
||||
let manager = EnvironmentManager::new(Some("none".to_string()));
|
||||
|
||||
assert_eq!(manager.executor_mode, ExecutorMode::NoExecutor);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_executor_mode_preserves_no_executor_semantics() {
|
||||
assert_eq!(parse_executor_mode(None), ExecutorMode::LocalExecutor);
|
||||
assert_eq!(
|
||||
parse_executor_mode(Some(String::new())),
|
||||
ExecutorMode::LocalExecutor
|
||||
);
|
||||
assert_eq!(
|
||||
parse_executor_mode(Some("none".to_string())),
|
||||
ExecutorMode::NoExecutor
|
||||
);
|
||||
assert_eq!(
|
||||
parse_executor_mode(Some("NONE".to_string())),
|
||||
ExecutorMode::NoExecutor
|
||||
);
|
||||
assert_eq!(
|
||||
parse_executor_mode(Some("ws://localhost:1234".to_string())),
|
||||
ExecutorMode::RemoteExecutor {
|
||||
url: "ws://localhost:1234".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -208,4 +297,41 @@ mod tests {
|
||||
|
||||
assert_eq!(response.process.process_id().as_str(), "default-env-proc");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_executor_environment_disables_attached_executor() {
|
||||
let environment = Environment::create(Some("none".to_string()))
|
||||
.await
|
||||
.expect("create environment");
|
||||
|
||||
assert_eq!(environment.exec_server_url(), None);
|
||||
assert!(!environment.has_attached_executor());
|
||||
assert_eq!(environment.executor_mode, ExecutorMode::NoExecutor);
|
||||
assert!(environment.remote_exec_server_client.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_executor_environment_rejects_exec_start() {
|
||||
let environment = Environment::create(Some("none".to_string()))
|
||||
.await
|
||||
.expect("create environment");
|
||||
|
||||
let err = environment
|
||||
.get_exec_backend()
|
||||
.start(crate::ExecParams {
|
||||
process_id: ProcessId::from("no-executor-proc"),
|
||||
argv: vec!["true".to_string()],
|
||||
cwd: std::env::current_dir().expect("read current dir"),
|
||||
env: Default::default(),
|
||||
tty: false,
|
||||
arg0: None,
|
||||
})
|
||||
.await
|
||||
.expect_err("no-executor backend should reject starts");
|
||||
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"exec-server protocol error: no attached executor is configured for this session"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user