mirror of
https://github.com/openai/codex.git
synced 2026-10-20 17:38:45 +00:00
resolve code mode host from install context
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
@@ -25,8 +24,6 @@ use self::connection::Connection;
|
||||
use self::connection::ConnectionError;
|
||||
use self::connection::RemoteSession;
|
||||
use self::connection::SessionCleanup;
|
||||
use crate::NoopCodeModeSessionDelegate;
|
||||
|
||||
mod connection;
|
||||
|
||||
type ShutdownResultReceiver = watch::Receiver<Option<Result<(), String>>>;
|
||||
@@ -50,6 +47,12 @@ impl ProcessOwnedCodeModeSessionProvider {
|
||||
}
|
||||
}
|
||||
|
||||
fn in_process() -> Self {
|
||||
Self {
|
||||
state: StdMutex::new(ProviderState::InProcess),
|
||||
}
|
||||
}
|
||||
|
||||
fn process_host(&self) -> Option<Arc<OwnedProcessHost>> {
|
||||
match &*self
|
||||
.state
|
||||
@@ -64,7 +67,10 @@ impl ProcessOwnedCodeModeSessionProvider {
|
||||
|
||||
impl Default for ProcessOwnedCodeModeSessionProvider {
|
||||
fn default() -> Self {
|
||||
Self::with_host_program(default_host_program())
|
||||
match InstallContext::current().code_mode_host_program() {
|
||||
Some(host_program) => Self::with_host_program(host_program),
|
||||
None => Self::in_process(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,13 +196,6 @@ pub struct ProcessOwnedCodeModeSession {
|
||||
}
|
||||
|
||||
impl ProcessOwnedCodeModeSession {
|
||||
pub fn new() -> Self {
|
||||
Self::with_process_host(
|
||||
Arc::new(NoopCodeModeSessionDelegate),
|
||||
Arc::new(OwnedProcessHost::new(default_host_program())),
|
||||
)
|
||||
}
|
||||
|
||||
fn with_process_host(
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
process_host: Arc<OwnedProcessHost>,
|
||||
@@ -465,12 +464,6 @@ impl Drop for ProcessOwnedCodeModeSession {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ProcessOwnedCodeModeSession {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeModeSession for ProcessOwnedCodeModeSession {
|
||||
fn execute<'a>(
|
||||
&'a self,
|
||||
@@ -492,26 +485,6 @@ impl CodeModeSession for ProcessOwnedCodeModeSession {
|
||||
}
|
||||
}
|
||||
|
||||
fn default_host_program() -> PathBuf {
|
||||
InstallContext::current()
|
||||
.code_mode_host_program()
|
||||
.unwrap_or_else(|| resolve_host_program(std::env::current_exe()))
|
||||
}
|
||||
|
||||
fn resolve_host_program(current_exe: io::Result<PathBuf>) -> PathBuf {
|
||||
let executable_name = if cfg!(windows) {
|
||||
"codex-code-mode-host.exe"
|
||||
} else {
|
||||
"codex-code-mode-host"
|
||||
};
|
||||
if let Ok(current_exe) = current_exe
|
||||
&& let Some(parent) = current_exe.parent()
|
||||
{
|
||||
return parent.join(executable_name);
|
||||
}
|
||||
PathBuf::from(executable_name)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "remote_session_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_code_mode_protocol::CodeModeSessionProvider;
|
||||
@@ -8,14 +6,16 @@ use codex_code_mode_protocol::FunctionCallOutputContentItem;
|
||||
use codex_code_mode_protocol::RuntimeResponse;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::OwnedProcessHost;
|
||||
use super::ProcessOwnedCodeModeSession;
|
||||
use super::ProcessOwnedCodeModeSessionProvider;
|
||||
use super::resolve_host_program;
|
||||
use crate::NoopCodeModeSessionDelegate;
|
||||
|
||||
#[test]
|
||||
fn provider_reuses_its_live_process_host() {
|
||||
let provider = ProcessOwnedCodeModeSessionProvider::default();
|
||||
let provider = ProcessOwnedCodeModeSessionProvider::with_host_program(
|
||||
"codex-code-mode-host-for-test".into(),
|
||||
);
|
||||
|
||||
let first = provider.process_host().expect("owned process host");
|
||||
let second = provider.process_host().expect("owned process host");
|
||||
@@ -24,34 +24,10 @@ fn provider_reuses_its_live_process_host() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_program_is_next_to_the_main_executable_even_when_missing() {
|
||||
let executable_name = if cfg!(windows) {
|
||||
"codex-code-mode-host.exe"
|
||||
} else {
|
||||
"codex-code-mode-host"
|
||||
};
|
||||
fn provider_without_host_program_uses_in_process_mode() {
|
||||
let provider = ProcessOwnedCodeModeSessionProvider::in_process();
|
||||
|
||||
assert_eq!(
|
||||
resolve_host_program(Ok(PathBuf::from("/opt/codex/bin/codex"))),
|
||||
PathBuf::from("/opt/codex/bin").join(executable_name)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_program_falls_back_to_its_name_when_main_executable_is_unknown() {
|
||||
let executable_name = if cfg!(windows) {
|
||||
"codex-code-mode-host.exe"
|
||||
} else {
|
||||
"codex-code-mode-host"
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
resolve_host_program(Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"missing executable"
|
||||
))),
|
||||
PathBuf::from(executable_name)
|
||||
);
|
||||
assert!(provider.process_host().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -92,7 +68,12 @@ async fn provider_falls_back_to_in_process_session_when_host_is_missing() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn shutdown_before_open_does_not_spawn_the_host() {
|
||||
let session = ProcessOwnedCodeModeSession::new();
|
||||
let session = ProcessOwnedCodeModeSession::with_process_host(
|
||||
Arc::new(NoopCodeModeSessionDelegate),
|
||||
Arc::new(OwnedProcessHost::new(
|
||||
"codex-code-mode-host-does-not-exist".into(),
|
||||
)),
|
||||
);
|
||||
|
||||
session.shutdown().await.expect("shutdown session");
|
||||
let error = session
|
||||
|
||||
Reference in New Issue
Block a user