fix: creating an instance of Codex requires a Config (#859)
I discovered that I accidentally introduced a change in
https://github.com/openai/codex/pull/829 where we load a fresh `Config`
in the middle of `codex.rs`:
c3e10e180a/codex-rs/core/src/codex.rs (L515-L522)
This is not good because the `Config` could differ from the one that has
the user's overrides specified from the CLI. Also, in unit tests, it
means the `Config` was picking up my personal settings as opposed to
using a vanilla config, which was problematic.
This PR cleans things up by moving the common case where
`Op::ConfigureSession` is derived from `Config` (originally done in
`codex_wrapper.rs`) and making it the standard way to initialize `Codex`
by putting it in `Codex::spawn()`. Note this also eliminates quite a bit
of boilerplate from the tests and relieves the caller of the
responsibility of minting out unique IDs when invoking `submit()`.
This commit is contained in:
@@ -22,8 +22,6 @@ use codex_core::config::Config;
|
||||
use codex_core::protocol::EventMsg;
|
||||
use codex_core::protocol::InputItem;
|
||||
use codex_core::protocol::Op;
|
||||
use codex_core::protocol::SandboxPolicy;
|
||||
use codex_core::protocol::Submission;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::time::timeout;
|
||||
|
||||
@@ -54,36 +52,10 @@ async fn spawn_codex() -> Codex {
|
||||
std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "2");
|
||||
}
|
||||
|
||||
let agent = Codex::spawn(std::sync::Arc::new(Notify::new())).unwrap();
|
||||
|
||||
let config = Config::load_default_config_for_test();
|
||||
agent
|
||||
.submit(Submission {
|
||||
id: "init".into(),
|
||||
op: Op::ConfigureSession {
|
||||
model: config.model,
|
||||
instructions: None,
|
||||
approval_policy: config.approval_policy,
|
||||
sandbox_policy: SandboxPolicy::new_read_only_policy(),
|
||||
disable_response_storage: false,
|
||||
notify: None,
|
||||
cwd: std::env::current_dir().unwrap(),
|
||||
},
|
||||
})
|
||||
let (agent, _init_id) = Codex::spawn(config, std::sync::Arc::new(Notify::new()))
|
||||
.await
|
||||
.expect("failed to submit init");
|
||||
|
||||
// Drain the SessionInitialized event so subsequent helper loops don't have
|
||||
// to special‑case it.
|
||||
loop {
|
||||
let ev = timeout(Duration::from_secs(30), agent.next_event())
|
||||
.await
|
||||
.expect("timeout waiting for init event")
|
||||
.expect("agent channel closed");
|
||||
if matches!(ev.msg, EventMsg::SessionConfigured { .. }) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
.unwrap();
|
||||
|
||||
agent
|
||||
}
|
||||
@@ -103,13 +75,10 @@ async fn live_streaming_and_prev_id_reset() {
|
||||
|
||||
// ---------- Task 1 ----------
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task1".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "Say the words 'stream test'".into(),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "Say the words 'stream test'".into(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -136,13 +105,10 @@ async fn live_streaming_and_prev_id_reset() {
|
||||
|
||||
// ---------- Task 2 (same session) ----------
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task2".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "Respond with exactly: second turn succeeded".into(),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "Respond with exactly: second turn succeeded".into(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -184,15 +150,12 @@ async fn live_shell_function_call() {
|
||||
const MARKER: &str = "codex_live_echo_ok";
|
||||
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task_fn".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: format!(
|
||||
"Use the shell function to run the command `echo {MARKER}` and no other commands."
|
||||
),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: format!(
|
||||
"Use the shell function to run the command `echo {MARKER}` and no other commands."
|
||||
),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -4,8 +4,6 @@ use codex_core::Codex;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::protocol::InputItem;
|
||||
use codex_core::protocol::Op;
|
||||
use codex_core::protocol::SandboxPolicy;
|
||||
use codex_core::protocol::Submission;
|
||||
use serde_json::Value;
|
||||
use tokio::time::timeout;
|
||||
use wiremock::Match;
|
||||
@@ -88,37 +86,17 @@ async fn keeps_previous_response_id_between_tasks() {
|
||||
std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "0");
|
||||
}
|
||||
|
||||
let codex = Codex::spawn(std::sync::Arc::new(tokio::sync::Notify::new())).unwrap();
|
||||
|
||||
// Init session
|
||||
let config = Config::load_default_config_for_test();
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "init".into(),
|
||||
op: Op::ConfigureSession {
|
||||
model: config.model,
|
||||
instructions: None,
|
||||
approval_policy: config.approval_policy,
|
||||
sandbox_policy: SandboxPolicy::new_read_only_policy(),
|
||||
disable_response_storage: false,
|
||||
notify: None,
|
||||
cwd: std::env::current_dir().unwrap(),
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
// drain init event
|
||||
let _ = codex.next_event().await.unwrap();
|
||||
let ctrl_c = std::sync::Arc::new(tokio::sync::Notify::new());
|
||||
let (codex, _init_id) = Codex::spawn(config, ctrl_c.clone()).await.unwrap();
|
||||
|
||||
// Task 1 – triggers first request (no previous_response_id)
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task1".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "hello".into(),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "hello".into(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -136,13 +114,10 @@ async fn keeps_previous_response_id_between_tasks() {
|
||||
|
||||
// Task 2 – should include `previous_response_id` (triggers second request)
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task2".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "again".into(),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "again".into(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -7,8 +7,6 @@ use codex_core::Codex;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::protocol::InputItem;
|
||||
use codex_core::protocol::Op;
|
||||
use codex_core::protocol::SandboxPolicy;
|
||||
use codex_core::protocol::Submission;
|
||||
use tokio::time::timeout;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
@@ -77,34 +75,15 @@ async fn retries_on_early_close() {
|
||||
std::env::set_var("OPENAI_STREAM_IDLE_TIMEOUT_MS", "2000");
|
||||
}
|
||||
|
||||
let codex = Codex::spawn(std::sync::Arc::new(tokio::sync::Notify::new())).unwrap();
|
||||
|
||||
let ctrl_c = std::sync::Arc::new(tokio::sync::Notify::new());
|
||||
let config = Config::load_default_config_for_test();
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "init".into(),
|
||||
op: Op::ConfigureSession {
|
||||
model: config.model,
|
||||
instructions: None,
|
||||
approval_policy: config.approval_policy,
|
||||
sandbox_policy: SandboxPolicy::new_read_only_policy(),
|
||||
disable_response_storage: false,
|
||||
notify: None,
|
||||
cwd: std::env::current_dir().unwrap(),
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let _ = codex.next_event().await.unwrap();
|
||||
let (codex, _init_id) = Codex::spawn(config, ctrl_c).await.unwrap();
|
||||
|
||||
codex
|
||||
.submit(Submission {
|
||||
id: "task".into(),
|
||||
op: Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "hello".into(),
|
||||
}],
|
||||
},
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
text: "hello".into(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user