refactor: inject code mode runtime into codex sessions

Extract a CodeModeRuntime trait from codex-code-mode and make the core code-mode wrapper hold an injected runtime instead of constructing the native implementation inline. Thread an optional runtime override through Codex spawn/session startup and expose a ThreadManager entrypoint that can start a thread with a caller-provided runtime. Re-export the runtime trait from codex-core so downstream browser harness code can consume the seam without reaching into transitive dependencies.
This commit is contained in:
Jeremy Lewi
2026-04-13 09:40:19 -07:00
parent fa5d14a81f
commit 3ffd99cd3d
8 changed files with 99 additions and 7 deletions
+2
View File
@@ -22,9 +22,11 @@ pub use runtime::DEFAULT_WAIT_YIELD_TIME_MS;
pub use runtime::ExecuteRequest;
pub use runtime::RuntimeResponse;
pub use runtime::WaitRequest;
pub use service::CodeModeRuntime;
pub use service::CodeModeService;
pub use service::CodeModeTurnHost;
pub use service::CodeModeTurnWorker;
pub use service::CodeModeTurnWorkerHandle;
pub const PUBLIC_TOOL_NAME: &str = "exec";
pub const WAIT_TOOL_NAME: &str = "wait";
+46
View File
@@ -34,6 +34,22 @@ pub trait CodeModeTurnHost: Send + Sync {
async fn notify(&self, call_id: String, cell_id: String, text: String) -> Result<(), String>;
}
#[async_trait]
pub trait CodeModeRuntime: Send + Sync {
async fn stored_values(&self) -> HashMap<String, JsonValue>;
async fn replace_stored_values(&self, values: HashMap<String, JsonValue>);
async fn execute(&self, request: ExecuteRequest) -> Result<RuntimeResponse, String>;
async fn wait(&self, request: WaitRequest) -> Result<RuntimeResponse, String>;
fn start_turn_worker(
&self,
host: Arc<dyn CodeModeTurnHost>,
) -> Box<dyn CodeModeTurnWorkerHandle>;
}
#[derive(Clone)]
struct SessionHandle {
control_tx: mpsc::UnboundedSender<SessionControlCommand>,
@@ -219,6 +235,10 @@ pub struct CodeModeTurnWorker {
shutdown_tx: Option<oneshot::Sender<()>>,
}
pub trait CodeModeTurnWorkerHandle: Send {}
impl CodeModeTurnWorkerHandle for CodeModeTurnWorker {}
impl Drop for CodeModeTurnWorker {
fn drop(&mut self) {
if let Some(shutdown_tx) = self.shutdown_tx.take() {
@@ -227,6 +247,32 @@ impl Drop for CodeModeTurnWorker {
}
}
#[async_trait]
impl CodeModeRuntime for CodeModeService {
async fn stored_values(&self) -> HashMap<String, JsonValue> {
CodeModeService::stored_values(self).await
}
async fn replace_stored_values(&self, values: HashMap<String, JsonValue>) {
CodeModeService::replace_stored_values(self, values).await;
}
async fn execute(&self, request: ExecuteRequest) -> Result<RuntimeResponse, String> {
CodeModeService::execute(self, request).await
}
async fn wait(&self, request: WaitRequest) -> Result<RuntimeResponse, String> {
CodeModeService::wait(self, request).await
}
fn start_turn_worker(
&self,
host: Arc<dyn CodeModeTurnHost>,
) -> Box<dyn CodeModeTurnWorkerHandle> {
Box::new(CodeModeService::start_turn_worker(self, host))
}
}
enum SessionControlCommand {
Poll {
yield_time_ms: u64,