Skip to content

Commit 00f75ca

Browse files
committed
Stabilize code mode execution
1 parent c9f8da9 commit 00f75ca

10 files changed

Lines changed: 862 additions & 107 deletions

File tree

codex-rs/code-mode/src/runtime/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,14 @@ pub(crate) fn spawn_runtime(
209209
.iter()
210210
.map(enabled_tool_metadata)
211211
.collect::<Vec<_>>();
212+
let runtime_handle = tokio::runtime::Handle::try_current()
213+
.map_err(|_| "failed to access Tokio runtime for code mode".to_string())?;
212214
let config = RuntimeConfig {
213215
tool_call_id: request.tool_call_id,
214216
enabled_tools,
215217
source: request.source,
216218
stored_values,
219+
runtime_handle,
217220
};
218221

219222
thread::spawn(move || {
@@ -240,6 +243,7 @@ struct RuntimeConfig {
240243
enabled_tools: Vec<EnabledToolMetadata>,
241244
source: String,
242245
stored_values: HashMap<String, JsonValue>,
246+
runtime_handle: tokio::runtime::Handle,
243247
}
244248

245249
pub(super) struct RuntimeState {
@@ -253,6 +257,7 @@ pub(super) struct RuntimeState {
253257
next_timeout_id: u64,
254258
tool_call_id: String,
255259
runtime_command_tx: std_mpsc::Sender<RuntimeCommand>,
260+
runtime_handle: tokio::runtime::Handle,
256261
exit_requested: bool,
257262
}
258263

@@ -311,6 +316,7 @@ fn run_runtime(
311316
next_timeout_id: 1,
312317
tool_call_id: config.tool_call_id,
313318
runtime_command_tx,
319+
runtime_handle: config.runtime_handle,
314320
exit_requested: false,
315321
});
316322

codex-rs/code-mode/src/runtime/timers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::thread;
21
use std::time::Duration;
32

43
use super::RuntimeCommand;
@@ -33,11 +32,12 @@ pub(super) fn schedule_timeout(
3332
let timeout_id = state.next_timeout_id;
3433
state.next_timeout_id = state.next_timeout_id.saturating_add(1);
3534
let runtime_command_tx = state.runtime_command_tx.clone();
35+
let runtime_handle = state.runtime_handle.clone();
3636
state
3737
.pending_timeouts
3838
.insert(timeout_id, ScheduledTimeout { callback });
39-
thread::spawn(move || {
40-
thread::sleep(Duration::from_millis(delay_ms));
39+
runtime_handle.spawn(async move {
40+
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
4141
let _ = runtime_command_tx.send(RuntimeCommand::TimeoutFired { id: timeout_id });
4242
});
4343

0 commit comments

Comments
 (0)