resolve code mode host from install context

This commit is contained in:
Channing Conger
2026-07-10 00:29:53 +00:00
parent 8fc5ae3ddf
commit 941eba8ea7
5 changed files with 41 additions and 80 deletions

View File

@@ -352,13 +352,11 @@ jobs:
rm -rf "$bundle_root"
mkdir -p "$bundle_root/codex-resources"
cp "$dest/codex-${{ matrix.target }}" "$bundle_root/codex"
cp "$dest/codex-code-mode-host-${{ matrix.target }}" "$bundle_root/codex-code-mode-host"
cp "$dest/bwrap-${{ matrix.target }}" "$bundle_root/codex-resources/bwrap"
chmod 0755 \
"$bundle_root/codex" \
"$bundle_root/codex-code-mode-host" \
"$bundle_root/codex-resources/bwrap"
tar -C "$bundle_root" -cf - codex codex-code-mode-host codex-resources/bwrap |
tar -C "$bundle_root" -cf - codex codex-resources/bwrap |
zstd -T0 -19 -o "$dest/codex-${{ matrix.target }}-bundle.tar.zst"
fi

View File

@@ -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;

View File

@@ -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

View File

@@ -881,7 +881,9 @@ update_visible_command() {
replace_path_with_symlink "$BIN_PATH" "$CURRENT_LINK/$codex_relative_path" "$tmp_link"
if [ "$os" = "darwin" ] && [ -x "$release_dir/$code_mode_host_relative_path" ]; then
if [ "$os" = "darwin" ] &&
[ "$code_mode_host_relative_path" = "bin/codex-code-mode-host" ] &&
[ -x "$release_dir/$code_mode_host_relative_path" ]; then
replace_path_with_symlink \
"$CODE_MODE_HOST_BIN_PATH" \
"$CURRENT_LINK/$code_mode_host_relative_path" \
@@ -899,7 +901,9 @@ update_visible_command() {
verify_visible_command() {
"$BIN_PATH" --version >/dev/null
if [ "$os" = "darwin" ] && [ "$install_layout" = "package" ]; then
if [ "$os" = "darwin" ] && [ "$install_layout" = "package" ] &&
[ "$(package_code_mode_host_relative_path "$release_dir")" = \
"bin/codex-code-mode-host" ]; then
[ -x "$CODE_MODE_HOST_BIN_PATH" ]
fi
}

View File

@@ -88,7 +88,9 @@ class InstallShTest(unittest.TestCase):
self.assertIn("/codex-npm-", requests[1])
self.assertNotIn("codex-package_SHA256SUMS", requests[1])
def test_macos_install_exposes_code_mode_host_beside_codex(self) -> None:
def test_macos_install_only_exposes_legacy_code_mode_host_beside_codex(
self,
) -> None:
for host_dir in ("codex-resources", "bin"):
with (
self.subTest(host_dir=host_dir),
@@ -116,11 +118,14 @@ class InstallShTest(unittest.TestCase):
self.assertEqual(
os.readlink(codex_path), str(current / "bin" / "codex")
)
self.assertEqual(
os.readlink(host_path),
str(current / host_dir / "codex-code-mode-host"),
)
self.assertTrue(os.access(host_path, os.X_OK))
if host_dir == "bin":
self.assertEqual(
os.readlink(host_path),
str(current / host_dir / "codex-code-mode-host"),
)
self.assertTrue(os.access(host_path, os.X_OK))
else:
self.assertFalse(host_path.exists())
def run_installer(