mirror of
https://github.com/openai/codex.git
synced 2026-09-16 12:13:30 +00:00
We currently see a behavior that looks like this:
```
2025-04-25T16:52:24.552789Z WARN codex_core::codex: stream disconnected - retrying turn (1/10 in 232ms)...
codex> event: BackgroundEvent { message: "stream error: stream disconnected before completion: Transport error: error decoding response body; retrying 1/10 in 232ms…" }
2025-04-25T16:52:54.789885Z WARN codex_core::codex: stream disconnected - retrying turn (2/10 in 418ms)...
codex> event: BackgroundEvent { message: "stream error: stream disconnected before completion: Transport error: error decoding response body; retrying 2/10 in 418ms…" }
```
This PR contains a few different fixes that attempt to resolve/improve
this:
1. **Remove overall client timeout.** I think
[this](https://github.com/openai/codex/pull/658/files#diff-c39945d3c42f29b506ff54b7fa2be0795b06d7ad97f1bf33956f60e3c6f19c19L173)
is perhaps the big fix -- it looks to me like this was actually timing
out even if events were still coming through, and that was causing a
disconnect right in the middle of a healthy stream.
2. **Cap response sizes.** We were frequently sending MUCH larger
responses than the upstream typescript `codex`, and that was definitely
not helping. [Fix
here](https://github.com/openai/codex/pull/658/files#diff-d792bef59aa3ee8cb0cbad8b176dbfefe451c227ac89919da7c3e536a9d6cdc0R21-R26)
for that one.
3. **Much higher idle timeout.** Our idle timeout value was much lower
than typescript.
4. **Sub-linear backoff.** We were much too aggressively backing off,
[this](https://github.com/openai/codex/pull/658/files#diff-5d5959b95c6239e6188516da5c6b7eb78154cd9cfedfb9f753d30a7b6d6b8b06R30-R33)
makes it sub-exponential but maintains the jitter and such.
I was seeing that `stream error: stream disconnected` behavior
constantly, and anecdotally I can no longer reproduce. It feels much
snappier.
30 lines
962 B
Rust
30 lines
962 B
Rust
use std::time::Duration;
|
|
|
|
use env_flags::env_flags;
|
|
|
|
use crate::error::CodexErr;
|
|
use crate::error::Result;
|
|
|
|
env_flags! {
|
|
pub OPENAI_DEFAULT_MODEL: &str = "o3";
|
|
pub OPENAI_API_BASE: &str = "https://api.openai.com";
|
|
pub OPENAI_API_KEY: Option<&str> = None;
|
|
pub OPENAI_TIMEOUT_MS: Duration = Duration::from_millis(300_000), |value| {
|
|
value.parse().map(Duration::from_millis)
|
|
};
|
|
pub OPENAI_REQUEST_MAX_RETRIES: u64 = 4;
|
|
pub OPENAI_STREAM_MAX_RETRIES: u64 = 10;
|
|
|
|
// We generally don't want to disconnect; this updates the timeout to be five minutes
|
|
// which matches the upstream typescript codex impl.
|
|
pub OPENAI_STREAM_IDLE_TIMEOUT_MS: Duration = Duration::from_millis(300_000), |value| {
|
|
value.parse().map(Duration::from_millis)
|
|
};
|
|
|
|
pub CODEX_RS_SSE_FIXTURE: Option<&str> = None;
|
|
}
|
|
|
|
pub fn get_api_key() -> Result<&'static str> {
|
|
OPENAI_API_KEY.ok_or_else(|| CodexErr::EnvVar("OPENAI_API_KEY"))
|
|
}
|