wip more informative authrequired error

This commit is contained in:
Owen Lin
2026-01-20 16:23:55 -08:00
parent 4a9c2bcc5a
commit 2aa08e92b2
5 changed files with 382 additions and 64 deletions
+19
View File
@@ -179,6 +179,8 @@ pub struct Codex {
pub(crate) rx_event: Receiver<Event>,
// Last known status of the agent.
pub(crate) agent_status: watch::Receiver<AgentStatus>,
pub(crate) auth_manager: Arc<AuthManager>,
pub(crate) requires_openai_auth: bool,
}
/// Wrapper returned by [`Codex::spawn`] containing the spawned [`Codex`],
@@ -230,6 +232,10 @@ impl Codex {
session_source: SessionSource,
agent_control: AgentControl,
) -> CodexResult<CodexSpawnOk> {
if config.model_provider.requires_openai_auth && auth_manager.get_auth_mode().is_none() {
return Err(CodexErr::AuthRequired);
}
let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY);
let (tx_event, rx_event) = async_channel::unbounded();
@@ -254,6 +260,7 @@ impl Codex {
.await
.map_err(|err| CodexErr::Fatal(format!("failed to load rules: {err}")))?;
let requires_openai_auth = config.model_provider.requires_openai_auth;
let config = Arc::new(config);
let _ = models_manager
.list_models(
@@ -315,6 +322,8 @@ impl Codex {
tx_sub,
rx_event,
agent_status: agent_status_rx,
auth_manager,
requires_openai_auth,
};
#[allow(deprecated)]
@@ -327,6 +336,16 @@ impl Codex {
/// Submit the `op` wrapped in a `Submission` with a unique ID.
pub async fn submit(&self, op: Op) -> CodexResult<String> {
if self.requires_openai_auth
&& matches!(
op,
Op::UserInput { .. } | Op::UserTurn { .. } | Op::Review { .. }
)
&& self.auth_manager.get_auth_mode().is_none()
{
return Err(CodexErr::AuthRequired);
}
let id = self
.next_id
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
+18
View File
@@ -42,6 +42,8 @@ pub(crate) async fn run_codex_thread_interactive(
cancel_token: CancellationToken,
initial_history: Option<InitialHistory>,
) -> Result<Codex, CodexErr> {
let requires_openai_auth = config.model_provider.requires_openai_auth;
let auth_manager_clone = Arc::clone(&auth_manager);
let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY);
let (tx_ops, rx_ops) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY);
@@ -88,6 +90,8 @@ pub(crate) async fn run_codex_thread_interactive(
tx_sub: tx_ops,
rx_event: rx_sub,
agent_status: codex.agent_status.clone(),
auth_manager: auth_manager_clone,
requires_openai_auth,
})
}
@@ -105,6 +109,8 @@ pub(crate) async fn run_codex_thread_one_shot(
cancel_token: CancellationToken,
initial_history: Option<InitialHistory>,
) -> Result<Codex, CodexErr> {
let requires_openai_auth = config.model_provider.requires_openai_auth;
let auth_manager_clone = Arc::clone(&auth_manager);
// Use a child token so we can stop the delegate after completion without
// requiring the caller to cancel the parent token.
let child_cancel = cancel_token.child_token();
@@ -162,6 +168,8 @@ pub(crate) async fn run_codex_thread_one_shot(
rx_event: rx_bridge,
tx_sub: tx_closed,
agent_status,
auth_manager: auth_manager_clone,
requires_openai_auth,
})
}
@@ -361,6 +369,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::AuthManager;
use crate::auth::AuthCredentialsStoreMode;
use async_channel::bounded;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::AgentStatus;
@@ -368,6 +378,7 @@ mod tests {
use codex_protocol::protocol::TurnAbortReason;
use codex_protocol::protocol::TurnAbortedEvent;
use pretty_assertions::assert_eq;
use std::path::PathBuf;
use tokio::sync::watch;
#[tokio::test]
@@ -375,11 +386,18 @@ mod tests {
let (tx_events, rx_events) = bounded(1);
let (tx_sub, rx_sub) = bounded(SUBMISSION_CHANNEL_CAPACITY);
let (_agent_status_tx, agent_status) = watch::channel(AgentStatus::PendingInit);
let auth_manager = AuthManager::shared(
PathBuf::from("non-existent"),
false,
AuthCredentialsStoreMode::File,
);
let codex = Arc::new(Codex {
next_id: AtomicU64::new(0),
tx_sub,
rx_event: rx_events,
agent_status,
auth_manager,
requires_openai_auth: false,
});
let (session, ctx, _rx_evt) = crate::codex::make_session_and_context_with_rx().await;
+5 -1
View File
@@ -103,6 +103,9 @@ pub enum CodexErr {
#[error("{0}")]
InvalidRequest(String),
#[error("OpenAI authentication required.")]
AuthRequired,
/// Invalid image.
#[error("Image poisoning")]
InvalidImageRequest(),
@@ -201,7 +204,8 @@ impl CodexErr {
| CodexErr::ThreadNotFound(_)
| CodexErr::Spawn
| CodexErr::SessionConfiguredNotFirstEvent
| CodexErr::UsageLimitReached(_) => false,
| CodexErr::UsageLimitReached(_)
| CodexErr::AuthRequired => false,
CodexErr::Stream(..)
| CodexErr::Timeout
| CodexErr::UnexpectedStatus(_)