WIP fase 2b-2: rimosso login ChatGPT/OAuth/device code, agent/workload identity, backend-client; resta solo chiave API (test non tutti verdi)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-10 06:00:44 +01:00
parent 82347a58ed
commit 231e8cd136
661 changed files with 4924 additions and 64817 deletions

View File

@@ -52,10 +52,6 @@ pub enum PlanType {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProviderAccount {
ApiKey,
Chatgpt {
email: Option<String>,
plan_type: PlanType,
},
AmazonBedrock {
uses_codex_managed_credentials: bool,
},

View File

@@ -3,30 +3,16 @@ use serde::Serialize;
use strum_macros::Display;
use thiserror::Error;
/// Authentication mode for OpenAI-backed providers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, Serialize, Deserialize)]
/// Authentication mode for stored or host-supplied provider credentials.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AuthMode {
/// OpenAI API key provided by the caller and stored by Codex.
/// Provider API key supplied by the caller and stored by Codex.
ApiKey,
/// ChatGPT OAuth managed by Codex (tokens persisted and refreshed by Codex).
Chatgpt,
/// ChatGPT auth tokens supplied by an external host application.
#[serde(rename = "chatgptAuthTokens")]
#[strum(serialize = "chatgptAuthTokens")]
ChatgptAuthTokens,
/// Codex backend auth supplied as request headers.
/// Provider auth supplied as request headers by a host application.
#[serde(rename = "headers")]
#[strum(serialize = "headers")]
Headers,
/// Programmatic Codex auth backed by a registered Agent Identity.
#[serde(rename = "agentIdentity")]
#[strum(serialize = "agentIdentity")]
AgentIdentity,
/// Programmatic Codex auth backed by a personal access token.
#[serde(rename = "personalAccessToken")]
#[strum(serialize = "personalAccessToken")]
PersonalAccessToken,
/// Amazon Bedrock bearer token managed by Codex.
#[serde(rename = "bedrockApiKey")]
#[strum(serialize = "bedrockApiKey")]
@@ -37,32 +23,6 @@ pub enum AuthMode {
BedrockAccessKeys,
}
impl AuthMode {
/// Returns whether this mode represents an authenticated human ChatGPT account.
pub fn has_chatgpt_account(self) -> bool {
match self {
Self::Chatgpt | Self::ChatgptAuthTokens | Self::PersonalAccessToken => true,
Self::ApiKey
| Self::Headers
| Self::AgentIdentity
| Self::BedrockApiKey
| Self::BedrockAccessKeys => false,
}
}
/// Returns whether this mode is backed by Codex services rather than a direct model API.
pub fn uses_codex_backend(self) -> bool {
match self {
Self::Chatgpt
| Self::ChatgptAuthTokens
| Self::Headers
| Self::AgentIdentity
| Self::PersonalAccessToken => true,
Self::ApiKey | Self::BedrockApiKey | Self::BedrockAccessKeys => false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PlanType {

View File

@@ -1,5 +1,4 @@
use crate::ThreadId;
use crate::auth::KnownPlan;
use crate::auth::PlanType;
pub use crate::auth::RefreshTokenFailedError;
pub use crate::auth::RefreshTokenFailedReason;
@@ -197,9 +196,7 @@ pub enum CodexErrorDetails {
ConnectionFailed(ConnectionFailedError),
#[error("Quota exceeded. Check your plan and billing details.")]
QuotaExceeded,
#[error(
"To use Codex with your ChatGPT plan, upgrade to Plus: https://chatgpt.com/explore/plus."
)]
#[error("Your model provider account does not include access to this model.")]
UsageNotIncluded,
#[error("We’re currently experiencing high demand, which may cause temporary errors.")]
InternalServerError,
@@ -788,7 +785,7 @@ pub struct UsageLimitReachedError {
impl std::fmt::Display for UsageLimitReachedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Reserve is a fallback for exhausted ordinary usage, so keep the standard
// promo/plan recovery copy below instead of suggesting another model.
// recovery copy below instead of suggesting another model.
if let Some(limit_name) = self
.rate_limits
.as_ref()
@@ -832,63 +829,16 @@ impl std::fmt::Display for UsageLimitReachedError {
);
}
RateLimitReachedType::RateLimitReached => {
// Generic limits intentionally use the existing promo or plan copy below.
// Generic limits use the neutral copy below.
}
}
}
if let Some(promo_message) = &self.promo_message {
return write!(
f,
"You’ve hit your usage limit. {promo_message},{}",
retry_suffix_after_or(self.resets_at.as_ref())
);
}
let message = match self.plan_type.as_ref() {
Some(PlanType::Known(KnownPlan::Plus)) => format!(
"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits{}",
retry_suffix_after_or(self.resets_at.as_ref())
),
Some(PlanType::Known(
KnownPlan::Team
| KnownPlan::SelfServeBusinessProLite
| KnownPlan::SelfServeBusinessUsageBased
| KnownPlan::Business
| KnownPlan::Ent26
| KnownPlan::EnterpriseCbpAutomation
| KnownPlan::EnterpriseCbpUsageBased
| KnownPlan::Sci
| KnownPlan::SciPlus,
)) => {
format!(
"You’ve hit your usage limit. To get more access now, send a request to your admin{}",
retry_suffix_after_or(self.resets_at.as_ref())
)
}
Some(PlanType::Known(KnownPlan::Free)) | Some(PlanType::Known(KnownPlan::Go)) => {
format!(
"You’ve hit your usage limit. Upgrade to Plus to continue using Codex (https://chatgpt.com/explore/plus),{}",
retry_suffix_after_or(self.resets_at.as_ref())
)
}
Some(PlanType::Known(KnownPlan::Pro | KnownPlan::ProLite | KnownPlan::ProMax)) => {
format!(
"You’ve hit your usage limit. Visit https://chatgpt.com/settings/usage to purchase more credits{}",
retry_suffix_after_or(self.resets_at.as_ref())
)
}
Some(PlanType::Known(
KnownPlan::Enterprise | KnownPlan::Edu | KnownPlan::EduPlus | KnownPlan::EduPro,
)) => format!(
"You’ve hit your usage limit.{}",
retry_suffix(self.resets_at.as_ref())
),
Some(PlanType::Unknown(_)) | None => format!(
"You’ve hit your usage limit.{}",
retry_suffix(self.resets_at.as_ref())
),
};
// Plan upsells and server promo copy are ChatGPT-specific; keep the message neutral.
let message = format!(
"You’ve hit your usage limit with the model provider.{}",
retry_suffix(self.resets_at.as_ref())
);
write!(f, "{message}")
}

View File

@@ -1,4 +1,5 @@
use super::*;
use crate::auth::KnownPlan;
use crate::exec_output::StreamOutput;
use crate::protocol::RateLimitWindow;
use chrono::DateTime;
@@ -166,7 +167,7 @@ fn usage_limit_reached_error_formats_plus_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -175,7 +176,7 @@ fn usage_limit_reached_error_formats_rate_limit_reached_types() {
let cases = [
(
RateLimitReachedType::RateLimitReached,
"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again later.",
"You’ve hit your usage limit with the model provider. Try again later.",
),
(
RateLimitReachedType::WorkspaceOwnerCreditsDepleted,
@@ -330,7 +331,7 @@ fn usage_limit_reached_error_formats_free_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. Upgrade to Plus to continue using Codex (https://chatgpt.com/explore/plus), or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -346,7 +347,7 @@ fn usage_limit_reached_error_formats_go_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. Upgrade to Plus to continue using Codex (https://chatgpt.com/explore/plus), or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -362,7 +363,7 @@ fn usage_limit_reached_error_formats_default_when_none() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. Try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -381,7 +382,7 @@ fn usage_limit_reached_error_formats_team_plan() {
rate_limit_reached_type: None,
};
let expected = format!(
"You’ve hit your usage limit. To get more access now, send a request to your admin or try again at {expected_time}."
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
@@ -404,7 +405,7 @@ fn usage_limit_reached_error_formats_business_plan_without_reset() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. To get more access now, send a request to your admin or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
}
@@ -421,7 +422,7 @@ fn usage_limit_reached_error_formats_self_serve_business_prolite_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. To get more access now, send a request to your admin or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -437,7 +438,7 @@ fn usage_limit_reached_error_formats_self_serve_business_usage_based_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. To get more access now, send a request to your admin or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -453,7 +454,7 @@ fn usage_limit_reached_error_formats_enterprise_cbp_usage_based_plan() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. To get more access now, send a request to your admin or try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
@@ -475,7 +476,7 @@ fn usage_limit_reached_error_formats_default_for_other_plans() {
};
assert_eq!(
err.to_string(),
"You’ve hit your usage limit. Try again later."
"You’ve hit your usage limit with the model provider. Try again later."
);
}
}
@@ -495,7 +496,7 @@ fn usage_limit_reached_error_formats_pro_plan_with_reset() {
rate_limit_reached_type: None,
};
let expected = format!(
"You’ve hit your usage limit. Visit https://chatgpt.com/settings/usage to purchase more credits or try again at {expected_time}."
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
@@ -516,9 +517,7 @@ fn usage_limit_reached_error_hides_upsell_for_non_codex_limit_name() {
limit_name: Some("codex_other".to_string()),
..rate_limit_snapshot()
})),
promo_message: Some(
"Visit https://chatgpt.com/settings/usage to purchase more credits".to_string(),
),
promo_message: Some("Server promo copy".to_string()),
rate_limit_reached_type: None,
};
let expected = format!(
@@ -542,7 +541,9 @@ fn usage_limit_reached_includes_minutes_when_available() {
promo_message: None,
rate_limit_reached_type: None,
};
let expected = format!("You’ve hit your usage limit. Try again at {expected_time}.");
let expected = format!(
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
}
@@ -593,7 +594,7 @@ fn unexpected_status_prefers_error_message_when_present() {
body: r#"{"error":{"message":"Workspace is not authorized in this region."},"status":401}"#
.to_string(),
user_message: None,
url: Some("https://chatgpt.com/backend-api/codex/responses".to_string()),
url: Some("https://api.example.com/v1/responses".to_string()),
cf_ray: None,
request_id: Some("req-123".to_string()),
identity_authorization_error: None,
@@ -603,7 +604,7 @@ fn unexpected_status_prefers_error_message_when_present() {
assert_eq!(
err.to_string(),
format!(
"unexpected status {status}: Workspace is not authorized in this region., url: https://chatgpt.com/backend-api/codex/responses, request id: req-123"
"unexpected status {status}: Workspace is not authorized in this region., url: https://api.example.com/v1/responses, request id: req-123"
)
);
}
@@ -637,7 +638,7 @@ fn unexpected_status_includes_cf_ray_and_request_id() {
status: StatusCode::UNAUTHORIZED,
body: "plain text error".to_string(),
user_message: None,
url: Some("https://chatgpt.com/backend-api/codex/responses".to_string()),
url: Some("https://api.example.com/v1/responses".to_string()),
cf_ray: Some("9c81f9f18f2fa49d-LHR".to_string()),
request_id: Some("req-xyz".to_string()),
identity_authorization_error: None,
@@ -647,7 +648,7 @@ fn unexpected_status_includes_cf_ray_and_request_id() {
assert_eq!(
err.to_string(),
format!(
"unexpected status {status}: plain text error, url: https://chatgpt.com/backend-api/codex/responses, cf-ray: 9c81f9f18f2fa49d-LHR, request id: req-xyz"
"unexpected status {status}: plain text error, url: https://api.example.com/v1/responses, cf-ray: 9c81f9f18f2fa49d-LHR, request id: req-xyz"
)
);
}
@@ -658,7 +659,7 @@ fn unexpected_status_includes_identity_auth_details() {
status: StatusCode::UNAUTHORIZED,
body: "plain text error".to_string(),
user_message: None,
url: Some("https://chatgpt.com/backend-api/codex/models".to_string()),
url: Some("https://api.example.com/v1/models".to_string()),
cf_ray: Some("cf-ray-auth-401-test".to_string()),
request_id: Some("req-auth".to_string()),
identity_authorization_error: Some("missing_authorization_header".to_string()),
@@ -668,7 +669,7 @@ fn unexpected_status_includes_identity_auth_details() {
assert_eq!(
err.to_string(),
format!(
"unexpected status {status}: plain text error, url: https://chatgpt.com/backend-api/codex/models, cf-ray: cf-ray-auth-401-test, request id: req-auth, auth error: missing_authorization_header, auth error code: token_expired"
"unexpected status {status}: plain text error, url: https://api.example.com/v1/models, cf-ray: cf-ray-auth-401-test, request id: req-auth, auth error: missing_authorization_header, auth error code: token_expired"
)
);
}
@@ -688,7 +689,7 @@ fn usage_limit_reached_includes_hours_and_minutes() {
rate_limit_reached_type: None,
};
let expected = format!(
"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again at {expected_time}."
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
@@ -709,7 +710,9 @@ fn usage_limit_reached_includes_days_hours_minutes() {
promo_message: None,
rate_limit_reached_type: None,
};
let expected = format!("You’ve hit your usage limit. Try again at {expected_time}.");
let expected = format!(
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
}
@@ -728,13 +731,15 @@ fn usage_limit_reached_less_than_minute() {
promo_message: None,
rate_limit_reached_type: None,
};
let expected = format!("You’ve hit your usage limit. Try again at {expected_time}.");
let expected = format!(
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});
}
#[test]
fn usage_limit_reached_with_promo_message() {
fn usage_limit_reached_ignores_server_promo_message() {
let base = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
let resets_at = base + ChronoDuration::seconds(30);
with_now_override(base, move || {
@@ -750,7 +755,7 @@ fn usage_limit_reached_with_promo_message() {
rate_limit_reached_type: None,
};
let expected = format!(
"You’ve hit your usage limit. To continue using Codex, start a free trial of <PLAN> today, or try again at {expected_time}."
"You’ve hit your usage limit with the model provider. Try again at {expected_time}."
);
assert_eq!(err.to_string(), expected);
});