Compare commits

...
8 Commits
Author SHA1 Message Date
ZyyandCodex d787195f4e chore: bump version to 3.48.0
Co-authored-by: Codex <noreply@openai.com>
2026-11-14 10:48:12 +08:00
ZyyandCodex 8681d6851e fix(deps): update Quinn and rustix to patched releases
Co-authored-by: Codex <noreply@openai.com>
2026-11-14 10:43:50 +08:00
d99ecf9aaf fix(openai): align model operations with global client profiles (#708)
Apply the global request profile to account catalog discovery, warmup,
and diagnostic routing. Freeze each operation's identity through the
existing provider profile resolver.

Refresh verified offline releases and centralize their version, artifact,
and environment defaults. Cover ZIP64 MSIX metadata and keep release
update tests independent of the current bundled versions.

Fixes #703

Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-11-14 10:40:12 +08:00
ZyyandZyy 8f40bfda7c fix(openai): preserve Responses identity semantics (#707)
Co-authored-by: Zyy <zyycn@users.noreply.github.com>
2026-11-14 10:36:23 +08:00
ZyyandCodex 72215fe62a feat(web): enable PWA installation with SVG icon
Add an installable web app manifest with standalone launch behavior and document deployment requirements.

Co-authored-by: Codex <noreply@openai.com>
2026-11-13 17:50:05 +08:00
f61995e61d feat(settings): remove request history retention cap (#702)
* feat(settings): remove request history retention cap

Allow positive retention days beyond 31 throughout the form, domain rules,
and database constraint. Keep the existing default and saved settings.
Bound cleanup and account usage queries by stored data so large retention
windows remain safe without expanding empty history.

Co-authored-by: Codex <noreply@openai.com>

* test(admin): align retention policy coverage with positive windows

Accept long request retention windows while preserving zero rejection
for each target and independent event and audit retention settings.

Co-authored-by: Codex <noreply@openai.com>

---------

Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-11-13 17:29:21 +08:00
ZyyandCodex f85d80d756 fix(openai): release websocket resources under backpressure
Make event delivery respond to downstream cancellation and pool shutdown. Return or discard sockets before publishing terminal events while preserving normal connection reuse.

Co-authored-by: Codex <noreply@openai.com>
2026-11-13 15:38:22 +08:00
github-actions[bot] e1f84c210b chore(release): update release channels to 3.47.0 2026-11-13 05:01:40 +00:00
62 changed files with 1225 additions and 336 deletions
+6 -6
View File
@@ -3492,9 +3492,9 @@ dependencies = [
[[package]]
name = "quinn-proto"
version = "0.11.18"
version = "0.11.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9746dbde176634f4f2f1faf2404e30a31b2bc1e9cafb5329c95d8177a18c9fc"
checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe"
dependencies = [
"bytes",
"getrandom 0.4.3",
@@ -3514,9 +3514,9 @@ dependencies = [
[[package]]
name = "quinn-udp"
version = "0.5.15"
version = "0.5.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694"
checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016"
dependencies = [
"cfg_aliases",
"libc",
@@ -3827,9 +3827,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "1.1.4"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
dependencies = [
"bitflags",
"errno",
@@ -24,7 +24,7 @@ impl RetentionPolicy {
audit_days: u32,
) -> Result<(), &'static str> {
for (valid, field) in [
((1..=31).contains(&usage_days), "usage_retention_days"),
(usage_days > 0, "usage_retention_days"),
(ops_days > 0, "ops_event_retention_days"),
(audit_days > 0, "audit_retention_days"),
] {
@@ -38,7 +38,7 @@ impl RetentionPolicy {
pub fn try_new(usage_days: u32, ops_days: u32, audit_days: u32) -> Result<Self, AdminError> {
if Self::validate_values(usage_days, ops_days, audit_days).is_err() {
return Err(AdminError::invalid(
"请求保留期必须为 1~31 天,事件与审计保留期必须为正数",
"请求、事件与审计保留期必须为正整数天数",
));
}
Ok(Self {
@@ -3,25 +3,15 @@
use gateway_admin::model::retention::{RetentionPolicy, RetentionTarget};
#[test]
fn retention_policy_rejects_invalid_windows_and_preserves_independent_windows() {
for (usage, ops, audit) in [
(0, 1, 1),
(32, 1, 1),
(u32::MAX, 1, 1),
(1, 0, 1),
(1, 1, 0),
] {
fn retention_policy_rejects_zero_for_each_target() {
for (usage, ops, audit) in [(0, 1, 1), (1, 0, 1), (1, 1, 0)] {
assert!(RetentionPolicy::try_new(usage, ops, audit).is_err());
}
let policy = RetentionPolicy::try_new(31, 7, 90).unwrap();
assert_eq!(policy.days(RetentionTarget::ModelRequests), 31);
assert_eq!(policy.days(RetentionTarget::OpsEvents), 7);
assert_eq!(policy.days(RetentionTarget::AdminAuditEvents), 90);
}
#[test]
fn request_retention_accepts_short_windows_without_changing_other_targets() {
for days in [1, 3, 7, 15, 30, 31] {
fn request_retention_accepts_positive_windows_without_changing_other_targets() {
for days in [1, 31, 32, 365, 3650, u32::MAX] {
let policy = RetentionPolicy::try_new(days, 30, 90).unwrap();
assert_eq!(policy.days(RetentionTarget::ModelRequests), days);
assert_eq!(policy.days(RetentionTarget::OpsEvents), 30);
@@ -357,11 +357,11 @@ fn settings_request_should_reject_unknown_rotation_strategy() {
}
#[tokio::test]
async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it() {
async fn request_retention_round_trips_and_invalid_days_do_not_replace_it() {
let fixture = AdminTestFixture::new().await;
fixture.auth.insert_session("valid-session");
let mut body = update_body();
for days in [1, 3, 7, 15, 30, 31] {
for days in [1, 31, 32, 365, 3650, u32::MAX] {
body["usageRetentionDays"] = json!(days);
let response = app(fixture.state())
.oneshot(request(
@@ -378,8 +378,7 @@ async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it(
}
for (days, expected_status) in [
(json!(0), StatusCode::BAD_REQUEST),
(json!(32), StatusCode::BAD_REQUEST),
(json!(u32::MAX), StatusCode::BAD_REQUEST),
(json!(u64::from(u32::MAX) + 1), StatusCode::BAD_REQUEST),
(json!(-1), StatusCode::UNPROCESSABLE_ENTITY),
(json!(1.5), StatusCode::UNPROCESSABLE_ENTITY),
(json!(null), StatusCode::UNPROCESSABLE_ENTITY),
@@ -402,7 +401,7 @@ async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it(
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response_json(response).await["data"]["usageRetentionDays"],
31
u32::MAX
);
}
@@ -5,6 +5,14 @@ use crate::{account::OpaqueProviderData, identity::ProviderKind, routing::Config
use futures::future::BoxFuture;
pub trait ProviderRuntimePolicyPort: Send + Sync {
/// 后台模型操作读取 Provider 全局画像,不读取单个 Client Key 的覆盖
fn load_request_profile<'a>(
&'a self,
_provider: &'a ProviderKind,
) -> BoxFuture<'a, Result<Option<OpaqueProviderData>, ProviderStoreError>> {
Box::pin(async { Ok(None) })
}
/// 原子推进预热执行游标,重启或时钟回拨后不重复领取已消费的时刻
fn claim_warmup_slot<'a>(
&'a self,
@@ -1247,13 +1247,13 @@ impl RuntimeSnapshot {
operation: &Operation,
context: &RoutingContext,
) -> Result<RoutingPlan, RoutingError> {
self.plan_inner(
public_model,
operation,
self.all_account_scope(),
context,
false,
)
// 探测没有 Client Key,沿用同一发布快照的全局画像
let scope = self
.all_account_scope()
.as_ref()
.clone()
.with_request_profiles(self.settings().request_profiles().clone());
self.plan_inner(public_model, operation, Arc::new(scope), context, false)
}
fn plan_inner(
@@ -3443,7 +3443,7 @@ fn provider_local_probe_failure_should_remain_distinct_from_upstream() {
}
#[test]
fn diagnostic_probe_does_not_apply_data_plane_account_model_policy() {
fn diagnostic_probe_uses_global_profile_without_data_plane_account_model_policy() {
let provider = ProviderKind::new("openai").expect("provider");
let account_id = ProviderAccountId::new("acct_probe").expect("account");
let snapshot = probe_snapshot().with_account_directory(Arc::new(RuntimeAccountDirectory::new(
@@ -3458,10 +3458,21 @@ fn diagnostic_probe_does_not_apply_data_plane_account_model_policy() {
),
)]),
)));
let profile = gateway_core::account::OpaqueProviderData::new(
json!({"identity":"global-probe"})
.as_object()
.unwrap()
.clone(),
);
let settings = snapshot
.settings()
.clone()
.with_request_profiles(BTreeMap::from([(provider.clone(), profile.clone())]));
let snapshot = snapshot.with_settings(&settings).unwrap();
let service = DefaultExecutionService::new(
RuntimeSnapshotHandle::new(snapshot),
Arc::new(TrackingExecutionStore::default()),
ProviderRegistry::new([Arc::new(LocalFailingProvider) as Arc<dyn Provider>])
ProviderRegistry::new([Arc::new(ProfileCheckingProvider(profile)) as Arc<dyn Provider>])
.expect("providers"),
Arc::new(UnusedAdmissions),
Arc::new(UnusedContinuation),
@@ -3862,6 +3873,36 @@ impl Provider for FailingProvider {
struct LocalFailingProvider;
struct ProfileCheckingProvider(gateway_core::account::OpaqueProviderData);
#[async_trait]
impl Provider for ProfileCheckingProvider {
fn name(&self) -> &'static str {
"openai"
}
fn catalog_generation(&self) -> ProviderCatalogGeneration {
ProviderCatalogGeneration::default()
}
async fn query_model_capabilities(
&self,
) -> Result<Vec<ProviderModelCapabilities>, ProviderError> {
Ok(Vec::new())
}
async fn execute(
self: Arc<Self>,
request: ProviderRequest,
context: AttemptContext,
) -> Result<ProviderStream, ProviderError> {
assert_eq!(context.request_profile(), Some(&self.0));
Arc::new(LocalFailingProvider)
.execute(request, context)
.await
}
}
#[async_trait]
impl Provider for LocalFailingProvider {
fn name(&self) -> &'static str {
@@ -658,20 +658,41 @@ fn account_costs(state: &MetricState) -> StoreResult<Vec<AccountCost>> {
.collect())
}
async fn retained_account_usage_range(
snapshot: &mut AnalyticsSnapshot<'_>,
now: DateTime<Utc>,
) -> StoreResult<ObservabilityRange> {
// 只为实际存在的数据规划时间桶,超长保留期不展开空白历史或溢出日期
let (days, earliest): (i64, DateTime<Utc>) = sqlx::query_as(
"select usage_retention_days, least(
(select started_at from model_requests order by started_at, id limit 1),
(select started_at from request_analytics_facts order by started_at, id limit 1),
(select bucket_start from request_analytics_1h where collection = 'usage_contribution' order by bucket_start limit 1),
$1::timestamptz - interval '1 day'
) from runtime_settings where id = 1",
)
.bind(now)
.fetch_one(&mut *snapshot.tx)
.await
.map_err(|error| postgres_unavailable("read retained account usage range", error))?;
let duration =
TimeDelta::try_days(days).ok_or_else(|| invalid("invalid account usage retention"))?;
let start = now
.checked_sub_signed(duration)
.unwrap_or(earliest)
.max(earliest);
ObservabilityRange::new(start, now)
}
pub(crate) async fn account_usage_sort_values(
snapshot: &mut AnalyticsSnapshot<'_>,
now: DateTime<Utc>,
) -> StoreResult<(Vec<String>, Vec<i64>, Vec<Option<DateTime<Utc>>>)> {
let days: i32 =
sqlx::query_scalar("select usage_retention_days::integer from runtime_settings where id=1")
.fetch_one(&mut *snapshot.tx)
.await
.map_err(|error| postgres_unavailable("read account usage retention", error))?;
let range = retained_account_usage_range(snapshot, now).await?;
let ids: Vec<String> = sqlx::query_scalar("select id from provider_accounts")
.fetch_all(&mut *snapshot.tx)
.await
.map_err(|error| postgres_unavailable("read account usage sort identities", error))?;
let range = ObservabilityRange::new(now - TimeDelta::days(i64::from(days)), now)?;
let mut totals = BTreeMap::<String, MetricState>::new();
for ((account, _), state) in snapshot.account_states(range, &ids, false).await? {
totals.entry(account).or_default().merge(&state)?;
@@ -699,11 +720,7 @@ pub(crate) async fn account_group_usage(
}
let mut snapshot = AnalyticsSnapshot::new_for_accounts(connection).await?;
let now = Utc::now();
let days: i32 =
sqlx::query_scalar("select usage_retention_days::integer from runtime_settings where id=1")
.fetch_one(&mut *snapshot.tx)
.await
.map_err(|error| postgres_unavailable("read group usage retention", error))?;
let retained = retained_account_usage_range(&mut snapshot, now).await?;
let memberships:Vec<(String,String)>=sqlx::query_as("select account_group_id,provider_account_id from account_group_accounts where account_group_id=any($1::text[])").bind(group_ids).fetch_all(&mut *snapshot.tx).await.map_err(|error|postgres_unavailable("read group usage membership",error))?;
let ids = memberships
.iter()
@@ -711,7 +728,6 @@ pub(crate) async fn account_group_usage(
.collect::<BTreeSet<_>>()
.into_iter()
.collect::<Vec<_>>();
let retained = ObservabilityRange::new(now - TimeDelta::days(i64::from(days)), now)?;
let today = ObservabilityRange::new(
timezone
.day_start(now)
@@ -2,7 +2,7 @@
use crate::{admin_store_error, postgres_unavailable};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use chrono::{DateTime, TimeDelta, Utc};
use gateway_admin::{
model::retention::{RetentionPolicy, RetentionTarget},
ports::{
@@ -54,15 +54,26 @@ impl RetentionStore for PgRetentionRepository {
policy: RetentionPolicy,
limit: NonZeroU32,
) -> AdminStoreResult<u64> {
let oldest_sql = match target {
RetentionTarget::ModelRequests => {
"select completed_at from model_requests where outcome <> 'running' order by completed_at limit 1"
}
RetentionTarget::OpsEvents => {
"select created_at from ops_events where model_request_id is null order by created_at limit 1"
}
RetentionTarget::AdminAuditEvents => {
"select created_at from admin_audit_events order by created_at limit 1"
}
};
let sql = match target {
RetentionTarget::ModelRequests => {
"delete from model_requests
where ctid in (
select ctid from model_requests
where outcome <> 'running'
and completed_at < $1 - ($2 * interval '1 day')
and completed_at < $1
order by completed_at
limit $3
limit $2
for update skip locked
)"
}
@@ -71,9 +82,9 @@ impl RetentionStore for PgRetentionRepository {
where ctid in (
select ctid from ops_events
where model_request_id is null
and created_at < $1 - ($2 * interval '1 day')
and created_at < $1
order by created_at
limit $3
limit $2
for update skip locked
)"
}
@@ -81,9 +92,9 @@ impl RetentionStore for PgRetentionRepository {
"delete from admin_audit_events
where ctid in (
select ctid from admin_audit_events
where created_at < $1 - ($2 * interval '1 day')
where created_at < $1
order by created_at
limit $3
limit $2
for update skip locked
)"
}
@@ -102,14 +113,25 @@ impl RetentionStore for PgRetentionRepository {
.execute(&mut *transaction)
.await
.map_err(|source| unavailable("set retention lock timeout", source))?;
let deleted = sqlx::query(sql)
.bind(now)
.bind(i64::from(policy.days(target)))
.bind(i64::from(limit.get()))
.execute(&mut *transaction)
let oldest: Option<DateTime<Utc>> = sqlx::query_scalar(oldest_sql)
.fetch_optional(&mut *transaction)
.await
.map(|result| result.rows_affected())
.map_err(|source| unavailable("delete expired records", source))?;
.map_err(|source| unavailable("read oldest retained record", source))?;
// 超长窗口覆盖全部现存记录时无需删除,也不向数据库传递越界日期
let cutoff = now
.checked_sub_signed(TimeDelta::days(i64::from(policy.days(target))))
.filter(|cutoff| oldest.is_some_and(|oldest| oldest < *cutoff));
let deleted = if let Some(cutoff) = cutoff {
sqlx::query(sql)
.bind(cutoff)
.bind(i64::from(limit.get()))
.execute(&mut *transaction)
.await
.map(|result| result.rows_affected())
.map_err(|source| unavailable("delete expired records", source))?
} else {
0
};
transaction
.commit()
.await
@@ -195,6 +195,30 @@ impl ProviderRuntimePolicyPort for PgRuntimeSettingsRepository {
Ok(claimed)
})
}
fn load_request_profile<'a>(
&'a self,
provider: &'a gateway_core::routing::ProviderKind,
) -> futures::future::BoxFuture<
'a,
Result<Option<gateway_core::account::OpaqueProviderData>, ProviderStoreError>,
> {
Box::pin(async move {
let document = sqlx::query_scalar::<
_,
Option<sqlx::types::Json<serde_json::Map<String, serde_json::Value>>>,
>(
"select provider_request_profiles_json -> $1 from runtime_settings where id = 1",
)
.bind(provider.as_str())
.fetch_one(&self.pool)
.await
.map_err(|source| {
crate::provider_unavailable("load Provider request profile", source)
})?;
Ok(document.map(|document| gateway_core::account::OpaqueProviderData::new(document.0)))
})
}
fn initialize_request_profile<'a>(
&'a self,
provider: &'a gateway_core::routing::ProviderKind,
@@ -693,9 +693,16 @@ async fn group_costs_follow_current_members_and_pending_source_replacements() {
}
}
assert!(ready, "group fixture must publish rollups");
let costs = load_retained_group_costs(&groups).await;
assert_eq!(costs[MIXED_GROUP], "3.25");
assert_eq!(costs[EMPTY_GROUP], "0");
for days in [31_i64, 365, i64::from(u32::MAX)] {
sqlx::query("update runtime_settings set usage_retention_days=$1")
.bind(days)
.execute(&database.pool)
.await
.unwrap();
let costs = load_retained_group_costs(&groups).await;
assert_eq!(costs[MIXED_GROUP], "3.25");
assert_eq!(costs[EMPTY_GROUP], "0");
}
sqlx::query("update account_group_accounts set account_group_id=$1 where provider_account_id='acct_group_move'")
.bind(EMPTY_GROUP).execute(&database.pool).await.unwrap();
@@ -105,6 +105,11 @@ async fn quota_windows_reconcile_moved_requests_and_pending_deletions_without_wa
assert_eq!(new.usage.reasoning_output_tokens, None);
assert_eq!(new.usage.models[0].model, "new-model");
assert_eq!(new.usage.costs[0].amount.as_str(), "4.75");
sqlx::query("update runtime_settings set usage_retention_days=$1")
.bind(i64::from(u32::MAX))
.execute(&database.pool)
.await
.unwrap();
for field in [AccountSortField::Usage, AccountSortField::LastUsedAt] {
let ranked = store
.list_accounts(
@@ -1,4 +1,4 @@
//! 验证短保留期升级、分批清理边界与统计异步收敛期间的查询可用性
//! 验证保留期读写、分批清理边界与统计异步收敛期间的查询可用性
use std::{num::NonZeroU32, time::Duration};
@@ -20,6 +20,66 @@ use super::{
observability_query_budget, observability_repository,
};
#[tokio::test]
async fn long_retention_round_trips_and_cleans_only_expired_requests() {
use gateway_store::postgres::{PgRuntimeSettingsRepository, RuntimeSettingsRepository};
let Some(database) = TestDatabase::create("long_retention").await else {
return;
};
let settings = PgRuntimeSettingsRepository::new(database.pool.clone());
let retention = PgRetentionRepository::new(database.pool.clone());
let now = Utc::now();
for (id, age) in [("old", 400), ("month", 40), ("recent", 1)] {
seed(&database.pool, id, now - ChronoDuration::days(age), &[]).await;
}
for (days, expected_deleted) in [(u32::MAX, 0), (3650, 0), (365, 1), (32, 1), (1, 0)] {
let mut update = super::runtime_settings::settings_with_margin(300);
update.values.usage_retention_days = days;
settings.update_runtime_settings(update).await.unwrap();
assert_eq!(
settings
.load_runtime_settings()
.await
.unwrap()
.values
.usage_retention_days,
days
);
let policy = retention.load_policy().await.unwrap();
assert_eq!(policy.days(RetentionTarget::ModelRequests), days);
assert_eq!(
retention
.purge_batch(
RetentionTarget::ModelRequests,
now,
policy,
NonZeroU32::new(10).unwrap()
)
.await
.unwrap(),
expected_deleted
);
}
let mut invalid = super::runtime_settings::settings_with_margin(300);
invalid.values.usage_retention_days = 0;
assert!(settings.update_runtime_settings(invalid).await.is_err());
assert_eq!(
retention
.load_policy()
.await
.unwrap()
.days(RetentionTarget::ModelRequests),
1
);
let remaining: Vec<String> = sqlx::query_scalar("select id from model_requests order by id")
.fetch_all(&database.pool)
.await
.unwrap();
assert_eq!(remaining, ["recent"]);
database.close().await;
}
#[tokio::test]
async fn short_retention_keeps_statistics_readable_until_each_deletion_batch_is_projected() {
use gateway_admin::model::observability::{AnalyticsState, DashboardQuery, TimeRange};
@@ -1015,6 +1015,13 @@ async fn request_profile_projection_is_revision_consistent_and_includes_key_over
};
let repository = PgRuntimeSettingsRepository::new(database.pool.clone());
let provider = ProviderKind::new("xai").unwrap();
assert!(
repository
.load_request_profile(&provider)
.await
.unwrap()
.is_none()
);
let document = |marker: &str| {
OpaqueProviderData::new(
serde_json::json!({"marker":marker})
@@ -1056,6 +1063,10 @@ async fn request_profile_projection_is_revision_consistent_and_includes_key_over
assert_eq!(profiles.len(), 2);
assert!(profiles.contains(&document("global")));
assert!(profiles.contains(&document("key")));
assert_eq!(
repository.load_request_profile(&provider).await.unwrap(),
Some(document("global"))
);
repository
.update_runtime_settings(settings_with_margin(7200))
@@ -1957,6 +1957,11 @@ fn map_quota_error(error: CodexCredentialQuotaError) -> ProviderAdminError {
use CodexCredentialQuotaError as Error;
use ProviderAdminErrorKind as Kind;
let (kind, public_message, upstream_message) = match error {
Error::Profile(source) => {
return provider_admin_error(Kind::Unavailable)
.with_public_message("OpenAI 账号预热客户端身份不可用")
.with_source(source);
}
Error::InvalidCredentialData => (
Kind::Invalid,
"OpenAI 额度查询凭据无效,请检查账号授权",
@@ -2131,6 +2136,11 @@ fn map_catalog_error(error: CodexCredentialCatalogError) -> ProviderAdminError {
"OpenAI 模型查询失败,请检查出站连接与上游服务",
),
Error::Cache => (Kind::Unavailable, "OpenAI 模型目录缓存暂不可用"),
Error::Profile(source) => {
return provider_admin_error(Kind::Unavailable)
.with_public_message("OpenAI 模型目录客户端身份不可用")
.with_source(source);
}
};
provider_admin_error(kind).with_public_message(message)
}
@@ -15,6 +15,7 @@ use gateway_core::account::{
};
use gateway_core::provider_ports::{
ProviderCatalogCacheKey, ProviderCatalogCachePort, ProviderCatalogScope,
ProviderRuntimePolicyPort,
};
use gateway_core::routing::{
FrozenAccountScope, ModelPresentation, ProviderCatalogGeneration, ProviderKind,
@@ -243,6 +244,8 @@ pub enum CodexCredentialCatalogError {
InvalidEtag,
#[error("Codex model catalog changed during refresh")]
ConcurrentUpdate,
#[error("Codex model catalog client profile is unavailable")]
Profile(#[source] gateway_core::error::ErrorSource),
}
#[derive(Default)]
@@ -273,6 +276,7 @@ pub struct CodexCredentialCatalogService {
http: reqwest::Client,
base_url: String,
account_catalog_cache: Arc<dyn ProviderCatalogCachePort>,
runtime_policy: Arc<dyn ProviderRuntimePolicyPort>,
cache: Arc<RwLock<CatalogCacheState>>,
refresh_notification: Arc<Notify>,
refresh_lock: Arc<tokio::sync::Mutex<()>>,
@@ -286,6 +290,7 @@ impl CodexCredentialCatalogService {
http: reqwest::Client,
base_url: String,
account_catalog_cache: Arc<dyn ProviderCatalogCachePort>,
runtime_policy: Arc<dyn ProviderRuntimePolicyPort>,
) -> Self {
Self {
repository,
@@ -293,6 +298,7 @@ impl CodexCredentialCatalogService {
http,
base_url,
account_catalog_cache,
runtime_policy,
cache: Arc::new(RwLock::new(CatalogCacheState::default())),
refresh_notification: Arc::new(Notify::new()),
refresh_lock: Arc::new(tokio::sync::Mutex::new(())),
@@ -300,6 +306,21 @@ impl CodexCredentialCatalogService {
}
}
/// 每轮发现冻结一次全局画像,使目录的版本参数与请求头使用同一身份
async fn discovery_client(&self) -> Result<CodexBackendClient, CodexCredentialCatalogError> {
let profile = self
.profile
.resolve_global_request_profile(self.runtime_policy.as_ref())
.await
.map_err(CodexCredentialCatalogError::Profile)?;
let client = CodexBackendClient::new(
self.http.clone(),
self.base_url.clone(),
self.profile.clone(),
);
Ok(client.with_request_profile(profile))
}
/// 原生条目取自后台发现的账号目录;用当前账号版本和冻结的 Key 政策双重过滤
pub async fn client_model_catalog(
&self,
@@ -500,11 +521,7 @@ impl CodexCredentialCatalogService {
.filter(|account| account.provider().as_str() == "openai")
.ok_or(CodexCredentialCatalogError::NoEligibleCredential)?;
let scope = CodexCatalogScope::for_account(&account);
let client = CodexBackendClient::new(
self.http.clone(),
self.base_url.clone(),
self.profile.clone(),
);
let client = self.discovery_client().await?;
let revision = self.state_revision()?;
let (catalog, entry) = self
.fetch_observation(&client, &account, &scope, None)
@@ -573,11 +590,7 @@ impl CodexCredentialCatalogService {
&self,
account: &ProviderAccount,
) -> Result<(Vec<CodexCatalogModel>, SystemTime), CodexCredentialCatalogError> {
let client = CodexBackendClient::new(
self.http.clone(),
self.base_url.clone(),
self.profile.clone(),
);
let client = self.discovery_client().await?;
let fetched = self.fetch_account_models(&client, account).await?;
if fetched.models.is_empty() {
return Err(CodexCredentialCatalogError::NoEligibleCredential);
@@ -289,11 +289,7 @@ impl CodexCredentialCatalogService {
.cached()?
.ok_or(CodexCredentialCatalogError::NoEligibleCredential);
}
let client = CodexBackendClient::new(
self.http.clone(),
self.base_url.clone(),
self.profile.clone(),
);
let client = self.discovery_client().await?;
let mut pace = tokio::time::interval(REFRESH_SPACING);
pace.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
let starts = stream::unfold(pace, |mut clock| async move {
@@ -140,6 +140,8 @@ pub struct CodexWarmupSummary {
#[derive(Debug, Error)]
pub enum CodexCredentialQuotaError {
#[error("Codex warmup client profile is unavailable")]
Profile(#[source] gateway_core::error::ErrorSource),
#[error("Codex quota response is invalid")]
InvalidCredentialData,
#[error("Codex OAuth access token must be refreshed before querying quota")]
@@ -140,11 +140,17 @@ impl CodexCredentialQuotaService {
})
.collect::<BTreeMap<_, _>>();
let profile = self
.profile
.resolve_global_request_profile(self.runtime_policy.as_ref())
.await
.map_err(CodexCredentialQuotaError::Profile)?;
let client = CodexBackendClient::new(
self.http.clone(),
self.base_url.clone(),
self.profile.clone(),
);
)
.with_request_profile(profile);
for account in accounts {
if account.quota().is_exhausted() {
@@ -152,6 +152,7 @@ pub async fn initialize(
http.clone(),
config.base_url().to_owned(),
ports.catalog_cache(),
ports.runtime_policy(),
));
if let Err(error) = catalog.restore().await {
tracing::warn!(%error,"OpenAI model catalog could not be restored");
@@ -15,7 +15,7 @@ use super::{
};
impl CodexBackendClient {
/// 客户端目录按调用方版本协商;后台目录仍使用经过核验的服务端画像版本
/// 目录优先使用显式协商版本,否则沿用调用方冻结的画像版本
pub async fn fetch_models_with_context(
&self,
context: CodexRequestContext<'_>,
@@ -11,7 +11,7 @@ use super::{
CodexRequestContext, CodexTransportMetrics, elapsed_duration_millis, http_version_name,
},
endpoints::endpoint_url,
headers::insert_optional_protocol_header,
headers::{insert_optional_header, insert_optional_protocol_header},
response_meta,
};
@@ -41,6 +41,11 @@ impl CodexBackendClient {
};
let profile = self.profile.snapshot();
let mut headers = self.model_request_headers(&profile, context)?;
insert_optional_header(
&mut headers,
"x-codex-installation-id",
context.installation_id,
)?;
super::headers::append_passthrough_headers(&mut headers, passthrough_headers);
// 独立端点没有 Responses 原连接归属,续接状态不能跨账号继承
// turn metadata 随后仅使用当前 lease 已处理的值
@@ -19,7 +19,7 @@ use super::{
},
diagnostics::CodexUpstreamDiagnostics,
endpoints::endpoint_url,
headers::is_managed_identity_header,
headers::{insert_optional_header, is_managed_identity_header},
response_meta,
};
@@ -33,6 +33,11 @@ impl CodexBackendClient {
) -> CodexClientResult<HeaderMap> {
let profile = self.profile.snapshot();
let mut headers = self.model_request_headers(&profile, context)?;
insert_optional_header(
&mut headers,
"x-codex-installation-id",
context.installation_id,
)?;
for header in extra_protocol_headers {
let name = HeaderName::from_bytes(header.name().as_bytes())?;
if is_managed_identity_header(name.as_str())
@@ -41,12 +41,35 @@ fn scoped_id(installation_id: &str, original: &str) -> String {
let hash = digest.finalize();
let mut bytes = [0_u8; 16];
bytes.copy_from_slice(&hash[..16]);
// 保持官方 UUID 的形状;各类引用共享映射,保留相同原值的等值关系
bytes[6] = (bytes[6] & 0x0f) | 0x40;
if let Some(id) = uuid::Uuid::parse_str(original).ok().filter(|id| {
id.get_version() == Some(uuid::Version::SortRand)
&& id.get_variant() == uuid::Variant::RFC4122
}) {
// 官方线程与会话使用 UUIDv7;保留毫秒时间,仅按账号隔离随机部分
bytes[..6].copy_from_slice(&id.as_bytes()[..6]);
bytes[6] = (bytes[6] & 0x0f) | 0x70;
} else {
// 自定义缓存键及非 v7 标识沿用稳定映射,不为它们补造创建时间
bytes[6] = (bytes[6] & 0x0f) | 0x40;
}
bytes[8] = (bytes[8] & 0x3f) | 0x80;
uuid::Uuid::from_bytes(bytes).to_string()
}
fn scoped_field_id(installation_id: &str, field: &str, original: &str) -> String {
// context_window_id 是独立标识,仅官方复合窗口字段引用 thread_id
if matches!(field, "window_id" | "x-codex-window-id" | "codexWindowId")
&& let Some((thread_id, number)) = original.split_once(':')
&& uuid::Uuid::parse_str(thread_id).is_ok()
&& number
.parse::<u64>()
.is_ok_and(|value| value.to_string() == number)
{
return format!("{}:{number}", scoped_id(installation_id, thread_id));
}
scoped_id(installation_id, original)
}
/// Search 的 id 是会话关联键,仅替换该值的原始字节,保留其他字段、重复键和空白
pub(super) fn scope_search_body(body: bytes::Bytes, installation_id: Option<&str>) -> bytes::Bytes {
#[derive(serde::Deserialize)]
@@ -85,7 +108,7 @@ fn map_fields(object: &mut Map<String, Value>, installation_id: &str, fields: &[
if let Some(Value::String(value)) = object.get_mut(*key)
&& !value.is_empty()
{
*value = scoped_id(installation_id, value);
*value = scoped_field_id(installation_id, key, value);
changed += 1;
}
}
@@ -123,7 +146,7 @@ pub(super) fn scope_headers(headers: &mut HeaderMap, installation_id: Option<&st
.ok()
.filter(|value| !value.is_empty())
.and_then(|value| {
HeaderValue::from_str(&scoped_id(installation_id, value)).ok()
HeaderValue::from_str(&scoped_field_id(installation_id, name, value)).ok()
})
.unwrap_or_else(|| value.clone())
})
@@ -28,7 +28,7 @@ pub(super) fn is_managed_identity_header(name: &str) -> bool {
| "chatgpt-project-id"
| "openai-organization"
| "openai-project"
// 安装头与 client_metadata 共用本次请求的安装身份
// 安装身份由所选账号生成,不继承下游安装头
| "x-codex-installation-id"
// 画像和设备证明须属于选定账号,不能由下游覆盖
| "originator"
@@ -138,11 +138,6 @@ impl CodexBackendClient {
};
let mut headers = build_codex_model_headers(profile, context.authorization, account_id)?;
insert_optional_header(&mut headers, "cookie", cookie_header)?;
insert_optional_header(
&mut headers,
"x-codex-installation-id",
context.installation_id,
)?;
Ok(headers)
}
@@ -0,0 +1,117 @@
//! OpenAI 客户端的离线版本、制品身份与预设环境默认值
//!
//! 更新时一并核验配套版本、构建号、时间及各架构制品身份
//! 各平台分别核验,版本相同不表示可以借用另一平台的资料
use chrono::{DateTime, Utc};
pub(super) const DESKTOP_ORIGINATOR: &str = "Codex Desktop";
pub(super) const CLI_ORIGINATOR: &str = "codex_cli_rs";
pub(super) const TUI_ORIGINATOR: &str = "codex-tui";
pub(super) const EXEC_ORIGINATOR: &str = "codex_exec";
pub(super) const TERMINAL: &str = "unknown";
pub(super) struct Environment {
pub os_type: &'static str,
pub os_version: &'static str,
pub arch: &'static str,
}
pub(super) const MACOS_ENVIRONMENT: Environment = Environment {
os_type: "Mac OS",
os_version: "15.7.1",
arch: "arm64",
};
pub(super) const LINUX_ENVIRONMENT: Environment = Environment {
os_type: "Linux",
os_version: "6.8.0",
arch: "x86_64",
};
pub(super) const WINDOWS_ENVIRONMENT: Environment = Environment {
os_type: "Windows",
os_version: "10.0.26100",
arch: "x86_64",
};
pub(super) struct CoreRelease {
pub version: &'static str,
pub verified_at_millis: i64,
}
impl CoreRelease {
pub fn verified_at(&self) -> DateTime<Utc> {
DateTime::UNIX_EPOCH + chrono::Duration::milliseconds(self.verified_at_millis)
}
}
pub(super) struct DesktopRelease {
pub core: CoreRelease,
pub version: &'static str,
pub build: &'static str,
}
pub(super) struct DesktopArtifact {
pub arch: &'static str,
pub size: u64,
pub etag: &'static str,
}
// 官方 npm 稳定标签及六个平台依赖;核验于 2026-11-14T02:09:52Z
pub(super) const CLI_RELEASE: CoreRelease = CoreRelease {
version: "0.169.0",
verified_at_millis: 1_794_622_192_000,
};
// macOS arm64 ZIP;核验于 2026-11-14T02:09:52Z
pub(super) const MACOS_RELEASE: DesktopRelease = DesktopRelease {
core: CoreRelease {
version: "0.169.0-alpha.11.1",
verified_at_millis: 1_794_622_192_000,
},
version: "26.1111.21308",
build: "27194",
};
// Windows 两种架构的 MSIX;核验于 2026-11-14T02:09:52Z
pub(super) const WINDOWS_RELEASE: DesktopRelease = DesktopRelease {
core: CoreRelease {
version: "0.167.0-alpha.4.1",
verified_at_millis: 1_794_622_192_000,
},
version: "26.1106.51428",
build: "26605",
};
pub(super) const WINDOWS_ARTIFACTS: [DesktopArtifact; 2] = [
DesktopArtifact {
arch: "x86_64",
size: 1_057_680_859,
etag: "\"0x8DF3FEB5404E49A\"",
},
DesktopArtifact {
arch: "arm64",
size: 1_158_775_140,
etag: "\"0x8DF3FEB5ABF50ED\"",
},
];
// Linux 两种架构的 DEB;核验于 2026-11-14T02:09:52Z
pub(super) const LINUX_RELEASE: DesktopRelease = DesktopRelease {
core: CoreRelease {
version: "0.169.0-alpha.11.1",
verified_at_millis: 1_794_622_192_000,
},
version: "26.1111.21308",
build: "27194",
};
pub(super) const LINUX_ARTIFACTS: [DesktopArtifact; 2] = [
DesktopArtifact {
arch: "x86_64",
size: 567_877_422,
etag: "\"0x8DF4126A0871331\"",
},
DesktopArtifact {
arch: "arm64",
size: 553_033_238,
etag: "\"0x8DF41269D464E6A\"",
},
];
@@ -12,7 +12,7 @@ use reqwest::{Client, redirect::Policy};
use serde::Deserialize;
use super::selection::{ClientKind, ClientPlatform, ClientRelease, object};
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState};
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState, baseline};
const ENDPOINT: &str = "https://registry.npmjs.org/@openai%2Fcodex/latest";
const MAX_BYTES: usize = 256 * 1024;
@@ -218,19 +218,16 @@ impl CliReleaseService {
}
pub(super) fn seed_releases(state: &CodexWireProfileState) {
// 2026-09-18 核对官方 npm latest 及六个平台依赖;这里只提供离线启动资料
for (platform, arch, _) in TARGETS {
state.seed_client_release(
ClientKind::Cli,
platform,
arch,
ClientRelease {
codex_version: "0.155.0".to_owned(),
codex_version: baseline::CLI_RELEASE.version.to_owned(),
desktop_version: None,
desktop_build: None,
verified_at: chrono::DateTime::parse_from_rfc3339("2026-09-18T00:00:00Z")
.ok()
.map(|time| time.with_timezone(&Utc)),
verified_at: Some(baseline::CLI_RELEASE.verified_at()),
},
);
}
@@ -2,6 +2,9 @@
use chrono::DateTime;
use gateway_core::account::OpaqueProviderData;
use gateway_core::error::ErrorSource;
use gateway_core::provider_ports::ProviderRuntimePolicyPort;
use gateway_core::routing::ProviderKind;
use serde::Deserialize;
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
@@ -11,6 +14,26 @@ use super::selection::{
};
use super::{CodexWireProfile, CodexWireProfileState};
impl CodexWireProfileState {
/// 后台模型操作没有 Client Key,按当前全局选择冻结整套身份
pub(crate) async fn resolve_global_request_profile(
&self,
policy: &dyn ProviderRuntimePolicyPort,
) -> Result<CodexWireProfile, ErrorSource> {
let provider = ProviderKind::new("openai").map_err(ErrorSource::new)?;
let Some(configuration) = policy
.load_request_profile(&provider)
.await
.map_err(ErrorSource::new)?
else {
return Ok(self.snapshot());
};
RequestProfileSelection::parse(&configuration)
.and_then(|selection| selection.resolve(self))
.map_err(ErrorSource::new)
}
}
/// 已解析的配置;没有 mode 的预设配置复用官方版本与平台合同
#[derive(Debug, Clone)]
pub struct RequestProfileSelection {
@@ -20,6 +20,7 @@ use url::Url;
use self::desktop_artifact::{CodexDesktopArtifactError, fetch_codex_core_version};
mod baseline;
pub mod cli_release;
mod context;
pub mod desktop_artifact;
@@ -82,22 +83,23 @@ pub struct CodexWireProfile {
impl Default for CodexWireProfile {
fn default() -> Self {
let release = baseline::MACOS_RELEASE;
let environment = baseline::MACOS_ENVIRONMENT;
Self {
client_kind: ClientKind::Desktop,
originator: "Codex Desktop".to_owned(),
codex_version: "0.153.4".to_owned(),
desktop_version: "26.901.51231".to_owned(),
desktop_build: "8109".to_owned(),
os_type: "Mac OS".to_owned(),
os_version: "15.7.1".to_owned(),
arch: "arm64".to_owned(),
terminal: "unknown".to_owned(),
originator: baseline::DESKTOP_ORIGINATOR.to_owned(),
codex_version: release.core.version.to_owned(),
desktop_version: release.version.to_owned(),
desktop_build: release.build.to_owned(),
os_type: environment.os_type.to_owned(),
os_version: environment.os_version.to_owned(),
arch: environment.arch.to_owned(),
terminal: baseline::TERMINAL.to_owned(),
exact_user_agent: None,
installation_seed: None,
residency: None,
account_routing_override: None,
// 制品核验于 2026-09-06T03:26:12.084Z;进程启动不构成重新核验
verified_at: DateTime::UNIX_EPOCH + chrono::Duration::milliseconds(1_788_665_172_084),
verified_at: release.core.verified_at(),
}
}
}
@@ -3,7 +3,7 @@
use super::selection::{
ClientKind, ClientPlatform, ClientProfileSelection, ClientRelease, VersionMode, object,
};
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState};
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState, baseline};
use chrono::Utc;
use futures::future::BoxFuture;
use gateway_core::provider_ports::{ProviderArtifactProfile, ProviderArtifactProfileCachePort};
@@ -280,43 +280,44 @@ fn sequence(target: DesktopTarget, release: &ClientRelease) -> io::Result<u64> {
}
fn baseline_artifacts() -> BTreeMap<DesktopTarget, VerifiedArtifact> {
// 2026-09-18 分别读取四个官方安装包,版本元组与 ETag 均来自同一个包
// 离线可直接选择自动模式;ETag 变化后必须重新核验,不能延用另一平台的版本
let identities = [
(774_919_598, "\"0x8DF141FFDF86290\""),
(771_727_321, "\"0x8DF14202C49201F\""),
(417_337_930, "\"0x8DF150AE1091545\""),
(395_993_890, "\"0x8DF150ADC810C82\""),
];
TARGETS
.into_iter()
.zip(identities)
.map(|(target, (size, etag))| {
let (core, version, build) = if target.platform == ClientPlatform::Windows {
("0.154.0-alpha.6.2", "26.908.70816", "9275")
} else {
("0.155.0-alpha.9", "26.915.31029", "9771")
};
[
(
ClientPlatform::Windows,
baseline::WINDOWS_RELEASE,
baseline::WINDOWS_ARTIFACTS,
),
(
ClientPlatform::Linux,
baseline::LINUX_RELEASE,
baseline::LINUX_ARTIFACTS,
),
]
.into_iter()
.flat_map(|(platform, release, artifacts)| {
artifacts.into_iter().map(move |artifact| {
(
target,
DesktopTarget {
platform,
arch: artifact.arch,
},
VerifiedArtifact {
identity: ArtifactIdentity {
size,
etag: etag.to_owned(),
},
release: ClientRelease {
codex_version: core.to_owned(),
desktop_version: Some(version.to_owned()),
desktop_build: Some(build.to_owned()),
verified_at: chrono::DateTime::parse_from_rfc3339("2026-09-18T00:00:00Z")
.ok()
.map(|time| time.with_timezone(&Utc)),
codex_version: release.core.version.to_owned(),
desktop_version: Some(release.version.to_owned()),
desktop_build: Some(release.build.to_owned()),
verified_at: Some(release.core.verified_at()),
},
identity: ArtifactIdentity {
size: artifact.size,
etag: artifact.etag.to_owned(),
},
},
)
})
.collect()
})
.collect()
}
pub(super) fn seed_releases(state: &CodexWireProfileState) {
for (target, artifact) in baseline_artifacts() {
state.seed_client_release(
@@ -5,7 +5,7 @@ use gateway_core::account::OpaqueProviderData;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::{CodexWireProfile, CodexWireProfileState};
use super::{CodexWireProfile, CodexWireProfileState, baseline};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
@@ -25,8 +25,8 @@ pub enum CliEntry {
impl CliEntry {
pub const fn originator(self) -> &'static str {
match self {
Self::Tui => "codex-tui",
Self::Exec => "codex_exec",
Self::Tui => baseline::TUI_ORIGINATOR,
Self::Exec => baseline::EXEC_ORIGINATOR,
}
}
}
@@ -41,25 +41,22 @@ pub enum ClientPlatform {
impl ClientPlatform {
pub const fn os_type(self) -> &'static str {
match self {
Self::Macos => "Mac OS",
Self::Linux => "Linux",
Self::Windows => "Windows",
}
self.default_environment().os_type
}
fn default_os_version(self) -> &'static str {
match self {
Self::Macos => "15.7.1",
Self::Linux => "6.8.0",
Self::Windows => "10.0.26100",
}
self.default_environment().os_version
}
fn default_arch(self) -> &'static str {
self.default_environment().arch
}
const fn default_environment(self) -> baseline::Environment {
match self {
Self::Macos => "arm64",
Self::Linux | Self::Windows => "x86_64",
Self::Macos => baseline::MACOS_ENVIRONMENT,
Self::Linux => baseline::LINUX_ENVIRONMENT,
Self::Windows => baseline::WINDOWS_ENVIRONMENT,
}
}
}
@@ -138,8 +135,10 @@ impl ClientProfileSelection {
fn default_originator(&self) -> &'static str {
match self.client {
ClientKind::Desktop => "Codex Desktop",
ClientKind::Cli => self.cli_entry.map_or("codex_cli_rs", CliEntry::originator),
ClientKind::Desktop => baseline::DESKTOP_ORIGINATOR,
ClientKind::Cli => self
.cli_entry
.map_or(baseline::CLI_ORIGINATOR, CliEntry::originator),
}
}
@@ -275,7 +274,7 @@ impl ClientProfileSelection {
terminal: self
.terminal
.clone()
.unwrap_or_else(|| "unknown".to_owned()),
.unwrap_or_else(|| baseline::TERMINAL.to_owned()),
exact_user_agent: None,
installation_seed: None,
residency: deployment.residency,
@@ -370,7 +369,7 @@ impl CodexWireProfileState {
"configuration": configuration,
"automaticAvailable": available,
"reason": (!available).then_some("暂不支持自动更新"),
"defaults": { "originator": configuration.default_originator(), "osType": platform.os_type(), "osVersion": platform.default_os_version(), "arch": platform.default_arch(), "terminal": "unknown" },
"defaults": { "originator": configuration.default_originator(), "osType": platform.os_type(), "osVersion": platform.default_os_version(), "arch": platform.default_arch(), "terminal": baseline::TERMINAL },
}));
}
}
@@ -97,12 +97,14 @@ pub(in crate::transport::websocket) fn stream_websocket_response(
pool_return,
reused_connection,
stream_idle_timeout,
trace,
shutdown,
downstream_cancellation,
delivery: WebSocketStreamDelivery {
trace,
shutdown,
downstream_cancellation,
tx,
},
rate_limit_updates: rate_limit_updates_for_task,
response_metadata_updates: response_metadata_updates_for_task,
tx,
})
.await;
};
@@ -133,33 +135,69 @@ pub(in crate::transport::websocket) fn stream_websocket_response(
}
struct WebSocketStreamForwardState {
trace: TraceContext,
websocket: PumpedWebSocket,
metadata: CodexWebSocketConnectionMetadata,
pool_return: Option<WebSocketStreamPoolReturn>,
reused_connection: bool,
stream_idle_timeout: Option<Duration>,
shutdown: CancellationToken,
downstream_cancellation: Option<gateway_core::lifecycle::CancellationToken>,
rate_limit_updates: CodexWebSocketRateLimitUpdates,
response_metadata_updates: CodexWebSocketResponseMetadataUpdates,
delivery: WebSocketStreamDelivery,
}
struct WebSocketStreamDelivery {
trace: TraceContext,
shutdown: CancellationToken,
downstream_cancellation: Option<gateway_core::lifecycle::CancellationToken>,
tx: mpsc::Sender<Result<Bytes, CodexWebSocketExchangeError>>,
}
impl WebSocketStreamDelivery {
async fn cancelled(&self) -> StreamWebSocketDiscardReason {
let (reason, discard_reason) = tokio::select! {
biased;
() = async {
match &self.downstream_cancellation {
Some(cancellation) => cancellation.cancelled().await,
None => std::future::pending().await,
}
} => ("downstream_connection_closed", StreamWebSocketDiscardReason::ClientCancelled),
() = self.tx.closed() => ("receiver_dropped", StreamWebSocketDiscardReason::ClientCancelled),
() = self.shutdown.cancelled() => ("pool_shutdown", StreamWebSocketDiscardReason::PoolShutdown),
};
self.trace
.record("upstream.cancelled", json!({"reason": reason}));
discard_reason
}
async fn send(
&self,
event: Result<Bytes, CodexWebSocketExchangeError>,
) -> Result<(), StreamWebSocketDiscardReason> {
// 终态与错误也可能被满队列阻塞,所有交付等待都必须响应取消和停机
tokio::select! {
biased;
reason = self.cancelled() => Err(reason),
result = self.tx.send(event) => result.map_err(|_| {
self.trace.record("upstream.forward.failed", json!({"reason": "receiver_dropped"}));
StreamWebSocketDiscardReason::DownstreamSendFailed
}),
}
}
}
async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
let WebSocketStreamForwardState {
trace,
mut websocket,
mut metadata,
pool_return,
reused_connection,
stream_idle_timeout,
shutdown,
downstream_cancellation,
rate_limit_updates,
response_metadata_updates,
tx,
delivery,
} = state;
let trace = &delivery.trace;
let mut pool_return = pool_return;
let mut continuation = pool_return
.as_mut()
@@ -172,38 +210,8 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
loop {
let message = tokio::select! {
biased;
() = async {
match &downstream_cancellation {
Some(cancellation) => cancellation.cancelled().await,
None => std::future::pending().await,
}
} => {
trace.record("upstream.cancelled", json!({"reason": "downstream_connection_closed"}));
discard_stream_websocket(
websocket,
pool_return,
StreamWebSocketDiscardReason::ClientCancelled,
).await;
return;
}
// 客户端断开下游 SSE 流:立即丢弃连接并释放池 slot,
// 不再傻等上游 idle 超时(否则同会话后续请求会一直 bypass/busy)
() = tx.closed() => {
trace.record("upstream.cancelled", json!({"reason": "receiver_dropped"}));
discard_stream_websocket(
websocket,
pool_return,
StreamWebSocketDiscardReason::ClientCancelled,
).await;
return;
}
() = shutdown.cancelled() => {
trace.record("upstream.cancelled", json!({"reason": "pool_shutdown"}));
discard_stream_websocket(
websocket,
pool_return,
StreamWebSocketDiscardReason::PoolShutdown,
).await;
reason = delivery.cancelled() => {
discard_stream_websocket(websocket, pool_return, reason).await;
return;
}
message = next_websocket_message(
@@ -239,7 +247,7 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
} else {
error
};
let _ = tx.send(Err(error)).await;
let _ = delivery.send(Err(error)).await;
return;
}
};
@@ -258,7 +266,7 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
StreamWebSocketDiscardReason::UnexpectedBinaryEvent,
)
.await;
let _ = tx
let _ = delivery
.send(Err(error.with_connection_observation(observation)))
.await;
return;
@@ -293,7 +301,7 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
} else {
error
};
let _ = tx.send(Err(error)).await;
let _ = delivery.send(Err(error)).await;
return;
}
_ => continue,
@@ -315,7 +323,7 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
Some(observation) => error.with_connection_observation(observation),
None => error,
};
let _ = tx.send(Err(error)).await;
let _ = delivery.send(Err(error)).await;
return;
}
};
@@ -336,19 +344,6 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
ExchangeAction::Forward { terminal } => terminal,
ExchangeAction::Ignore => continue,
};
if tx.send(Ok(Bytes::from(raw))).await.is_err() {
trace.record(
"upstream.forward.failed",
json!({"reason": "receiver_dropped"}),
);
discard_stream_websocket(
websocket,
pool_return,
StreamWebSocketDiscardReason::DownstreamSendFailed,
)
.await;
return;
}
if let Some(terminal) = terminal {
trace.record(
"upstream.terminal",
@@ -377,6 +372,12 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
.await;
}
}
// 先归还续接状态或释放 socket,终态交付不能延迟资源收尾
let _ = delivery.send(Ok(Bytes::from(raw))).await;
return;
}
if let Err(reason) = delivery.send(Ok(Bytes::from(raw))).await {
discard_stream_websocket(websocket, pool_return, reason).await;
return;
}
}
@@ -427,7 +428,7 @@ async fn forward_websocket_response_stream(state: WebSocketStreamForwardState) {
} else {
error
};
let _ = tx.send(Err(error)).await;
let _ = delivery.send(Err(error)).await;
}
async fn finish_stream_websocket(
@@ -293,10 +293,7 @@ async fn backend_http_should_preserve_business_headers_without_downstream_transp
raw_header_values(&raw, "x-openai-account-routing-override"),
vec![b"configured-route".to_vec()]
);
assert_eq!(
raw_header_values(&raw, "x-codex-installation-id"),
vec![b"lease-installation".to_vec()]
);
assert!(raw_header_values(&raw, "x-codex-installation-id").is_empty());
let session = raw_header_values(&raw, "session-id");
assert!(uuid::Uuid::parse_str(std::str::from_utf8(&session[0]).unwrap()).is_ok());
// 指纹头由运行时画像生成,不透传客户端值
@@ -486,10 +483,7 @@ async fn backend_websocket_should_preserve_business_headers_without_downstream_t
values("x-openai-account-routing-override"),
vec![b"configured-route".to_vec()]
);
assert_eq!(
values("x-codex-installation-id"),
vec![b"lease-installation".to_vec()]
);
assert!(values("x-codex-installation-id").is_empty());
let session = values("session-id");
assert!(uuid::Uuid::parse_str(std::str::from_utf8(&session[0]).unwrap()).is_ok());
assert_eq!(values("x-codex-turn-state"), vec![b"turn-ascii".to_vec()]);
@@ -23,6 +23,82 @@ use crate::support::{MemoryAccountStore, catalog_cache, profile, secret};
const OFFICIAL_FIXTURE: &[u8] =
include_bytes!("../../transport/fixtures/official_models_snapshot.json");
#[tokio::test]
async fn discovery_refresh_and_export_use_the_current_global_profile() {
use gateway_core::account::OpaqueProviderData;
use serde_json::json;
let document =
|value: serde_json::Value| OpaqueProviderData::new(value.as_object().unwrap().clone());
let fixed = |version| {
document(
json!({"client":"cli","platform":"macos","versionMode":"fixed","codexVersion":version}),
)
};
let store = Arc::new(MemoryAccountStore::default());
let first = seed_account(&store, "acct_profile_a").await;
let second = seed_account(&store, "acct_profile_b").await;
let server = MockServer::start().await;
for (version, originator, count) in [
("0.169.0", "codex_cli_rs", 2),
("0.170.0", "codex_cli_rs", 1),
("0.171.0", "catalog-test", 1),
] {
Mock::given(method("GET"))
.and(path("/codex/models"))
.and(query_param("client_version", version))
.and(header("version", version))
.and(header("originator", originator))
.and(move |request: &Request| request.headers.get("user-agent").and_then(|v| v.to_str().ok()).is_some_and(|ua| ua.starts_with(&format!("{originator}/{version}"))))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({"models":[{"slug":format!("model-{version}"),"display_name":"Native","future_field":true}]})))
.expect(count).mount(&server).await;
}
let policy = Arc::new(crate::support::RequestProfileRuntimePolicy(
std::sync::Mutex::new(fixed("0.169.0")),
));
let baseline = wire_profile();
let service = CodexCredentialCatalogService::new(
store.repository(),
baseline.clone(),
reqwest::Client::builder().no_proxy().build().unwrap(),
server.uri(),
catalog_cache(),
policy.clone(),
);
service.refresh_catalogs().await.unwrap();
for account in [&first, &second] {
assert_eq!(
service.cached_account_models(account).unwrap().unwrap(),
["model-0.169.0"]
);
}
*policy.0.lock().unwrap() = fixed("0.170.0");
assert_eq!(
service
.refresh_account_catalog(first.id())
.await
.unwrap()
.models(),
["model-0.170.0"]
);
*policy.0.lock().unwrap() = document(
json!({"mode":"custom","userAgent":"catalog-test/0.171.0","originator":"catalog-test","codexVersion":"0.171.0"}),
);
let (models, _) = service.account_catalog_documents(&first).await.unwrap();
assert_eq!(models[0].request_model().as_str(), "model-0.171.0");
assert_eq!(baseline.snapshot().codex_version, "0.144.0");
*policy.0.lock().unwrap() = document(json!({"mode":"custom","userAgent":"invalid"}));
assert!(matches!(
service.refresh_account_catalog(first.id()).await,
Err(CodexCredentialCatalogError::Profile(_))
));
assert_eq!(
service.cached_account_models(&first).unwrap().unwrap(),
["model-0.170.0"]
);
}
struct ReplacingCatalogResponder {
calls: Arc<AtomicUsize>,
}
@@ -95,6 +171,7 @@ fn service_with_catalog_cache(
.expect("client"),
base_url,
catalog_cache,
crate::support::runtime_policy(),
)
}
@@ -242,6 +242,7 @@ fn selector_with_runtime(
http.clone(),
OFFICIAL_CODEX_BASE_URL.to_owned(),
crate::support::catalog_cache(),
crate::support::runtime_policy(),
),
);
let quota = Arc::new(CodexCredentialQuotaService::new(
@@ -218,6 +218,8 @@ async fn complete_with_name(
#[tokio::test]
async fn authorize_url_matches_the_official_desktop_parameter_contract() {
let wire_profile = profile();
let desktop_version = wire_profile.snapshot().desktop_version;
let service = CodexOAuthAdminService::new(
Arc::new(PendingStore::default()),
Arc::new(Exchanger {
@@ -225,7 +227,7 @@ async fn authorize_url_matches_the_official_desktop_parameter_contract() {
}),
Arc::new(MemoryAccountStore::default()),
CodexCredentialAdmin,
profile(),
wire_profile,
);
let started = service
.start_authorization(StartCodexOAuthAuthorization {
@@ -326,7 +328,7 @@ async fn authorize_url_matches_the_official_desktop_parameter_contract() {
);
assert_eq!(
parameters.get("codex_app_version").map(String::as_str),
Some("26.901.51231")
Some(desktop_version.as_str())
);
let surface_stable_id = parameters
.get("source_surface_stable_id")
@@ -192,7 +192,7 @@ async fn warmup_consumes_completed_sse_and_persists_rate_limit_event() {
}
#[tokio::test]
async fn warmup_sends_the_lowest_reasoning_effort_supported_by_the_catalog() {
async fn warmup_uses_global_profile_and_lowest_reasoning_effort() {
let server = MockServer::start().await;
let sse = concat!(
"event: response.completed\n",
@@ -201,19 +201,27 @@ async fn warmup_sends_the_lowest_reasoning_effort_supported_by_the_catalog() {
);
Mock::given(method("POST"))
.and(path("/codex/responses"))
.and(wiremock::matchers::header("version", "0.169.0"))
.and(wiremock::matchers::header("originator", "codex_cli_rs"))
.respond_with(ResponseTemplate::new(200).set_body_raw(sse, "text/event-stream"))
.expect(1)
.mount(&server)
.await;
let store = Arc::new(MemoryAccountStore::default());
create_account(&store, "acct_warmup_effort").await;
let service = quota_service_with_base_url(
&store,
let policy = Arc::new(crate::support::RequestProfileRuntimePolicy(std::sync::Mutex::new(
OpaqueProviderData::new(json!({"client":"cli","platform":"macos","versionMode":"fixed","codexVersion":"0.169.0"}).as_object().unwrap().clone()),
)));
let service = CodexCredentialQuotaService::new(
store.repository(),
wire_profile(),
reqwest::Client::builder()
.no_proxy()
.build()
.expect("client"),
server.uri(),
Arc::new(crate::support::MemoryCooldownPort::new()),
policy.clone(),
);
let summary = service.execute_warmup("test-model").await.expect("warmup");
@@ -226,6 +234,16 @@ async fn warmup_sends_the_lowest_reasoning_effort_supported_by_the_catalog() {
serde_json::from_slice(&decoded).expect("warmup body should be valid JSON");
// 上游按目录拒绝目录外的档位,none 曾让全部定时预热只留下 error 终态
assert_eq!(body["reasoning"]["effort"], "low");
*policy.0.lock().unwrap() = OpaqueProviderData::new(
json!({"mode":"custom","userAgent":"invalid"})
.as_object()
.unwrap()
.clone(),
);
assert!(matches!(
service.execute_warmup("test-model").await,
Err(CodexCredentialQuotaError::Profile(_))
));
}
#[tokio::test]
@@ -13,6 +13,7 @@ fn cookie_provider(store: &Arc<MemoryAccountStore>, base_url: String) -> Arc<Cod
http.clone(),
base_url.clone(),
catalog_cache(),
crate::support::runtime_policy(),
));
let quota = Arc::new(CodexCredentialQuotaService::new(
store.repository(),
@@ -2,16 +2,21 @@
use super::*;
fn metadata() -> Value {
json!({"session_id":"source-session", "thread_id":"source-thread", "turn_id":"source-turn",
"parent_thread_id":"source-session", "forked_from_thread_id":"source-session",
const SESSION_ID: &str = "019b1234-5678-7abc-8def-0123456789ab";
const THREAD_ID: &str = "019b1234-5679-7def-8abc-0123456789ab";
const CONTEXT_WINDOW_ID: &str = "3b11bc82-c666-417d-b9ff-2a34fd040385";
fn metadata(window_number: u64) -> Value {
json!({"session_id":SESSION_ID, "thread_id":THREAD_ID, "turn_id":"source-turn",
"parent_thread_id":SESSION_ID, "forked_from_thread_id":SESSION_ID,
"parent_turn_id":"source-turn", "root_turn_id":"source-turn",
"window_id":"source-window", "context_window_id":"source-window", "future":{"keep":true},
"window_id":format!("{THREAD_ID}:{window_number}"), "window_number":window_number,
"context_window_id":CONTEXT_WINDOW_ID, "future":{"keep":true},
"sentry-trace":"source-trace", "baggage":"sentry-environment=synthetic"})
}
fn payload(websocket: bool) -> ProtocolPayload {
let mut full = metadata();
fn payload(websocket: bool, window_number: u64) -> ProtocolPayload {
let mut full = metadata(window_number);
full["tool_namespaces_info"] = json!({"inventory":"body-only"});
let mut input = account_isolation::isolation_history()
.as_array()
@@ -31,17 +36,17 @@ fn payload(websocket: bool) -> ProtocolPayload {
}
input.insert(0, json!({"role":"user", "content":[{"type":"input_text", "text":concat!(
"<external_codex_git_state><environment_context><git>",
r#"{"sessionId":"source-session","threadId":"source-thread","clientUserMessageId":"source-turn","path":"/workspace/project"}"#,
r#"{"sessionId":"019b1234-5678-7abc-8def-0123456789ab","threadId":"019b1234-5679-7def-8abc-0123456789ab","clientUserMessageId":"source-turn","path":"/workspace/project"}"#,
"</git></environment_context></external_codex_git_state>"
)}], "internal_chat_message_metadata_passthrough":{
"turn_id":"source-turn", "content_item_kinds":["additional_content.codex_git_state"]
}}));
ProtocolPayload::json_object("openai", json!({
"model":"gpt-5.4", "input":input,
"prompt_cache_key":"custom-cache-key", "client_metadata":{
"session_id":"source-session", "thread_id":"source-thread", "turn_id":"source-turn",
"prompt_cache_key":SESSION_ID, "client_metadata":{
"session_id":SESSION_ID, "thread_id":THREAD_ID, "turn_id":"source-turn",
"x-client-turn-id":"source-turn",
"x-codex-window-id":"source-window", "x-codex-parent-thread-id":"source-session",
"x-codex-window-id":format!("{THREAD_ID}:{window_number}"), "x-codex-parent-thread-id":SESSION_ID,
"x-codex-turn-metadata":full.to_string(),
"ws_request_header_traceparent":"00-11111111111111111111111111111111-2222222222222222-01",
"ws_request_header_tracestate":"vendor=source",
@@ -50,9 +55,10 @@ fn payload(websocket: bool) -> ProtocolPayload {
}
}).as_object().unwrap().clone()).unwrap().with_context(Map::from_iter([
("use_websocket".into(), json!(websocket)),
("session_id".into(), json!("source-session")),
("thread_id".into(), json!("source-thread")),
("turn_metadata".into(), json!(metadata().to_string())),
("session_id".into(), json!(SESSION_ID)),
("thread_id".into(), json!(THREAD_ID)),
("turn_metadata".into(), json!(metadata(window_number).to_string())),
("codex_window_id".into(), json!(format!("{THREAD_ID}:{window_number}"))),
("opaque_request_headers".into(), json!([
["x-client-turn-id",STANDARD.encode("source-turn")],
["traceparent",STANDARD.encode("00-11111111111111111111111111111111-2222222222222222-01")],
@@ -74,7 +80,7 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
create_account(&stores[1], "acct_scope_new").await;
let mut captures = Vec::new();
for account in [0, 0, 1, 0] {
for websocket in [false, true] {
for (window_number, websocket) in [(0, false), (0, true), (1, false), (1, true)] {
let (base_url, capture): (
String,
BoxFuture<'static, (reqwest::header::HeaderMap, Value)>,
@@ -126,6 +132,7 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
"openai",
Operation::Generate(GenerateRequest::from_protocol_payload(payload(
websocket,
window_number,
))),
),
context("req_correlation", CancellationToken::new()),
@@ -161,19 +168,18 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
flat["ws_request_header_baggage"],
"sentry-environment=synthetic"
);
assert_eq!(
headers["x-codex-installation-id"].to_str().unwrap(),
flat["x-codex-installation-id"]
);
assert!(!headers.contains_key("x-codex-installation-id"));
let header: Value =
serde_json::from_str(headers["x-codex-turn-metadata"].to_str().unwrap()).unwrap();
let full: Value =
serde_json::from_str(flat["x-codex-turn-metadata"].as_str().unwrap()).unwrap();
for (field, original) in [
("session_id", "source-session"),
("thread_id", "source-thread"),
] {
assert_scoped_id(flat[field].as_str().unwrap(), original);
for (field, original) in [("session_id", SESSION_ID), ("thread_id", THREAD_ID)] {
let mapped = flat[field].as_str().unwrap();
assert_scoped_id(mapped, original);
let mapped_uuid = uuid::Uuid::parse_str(mapped).unwrap();
let original_uuid = uuid::Uuid::parse_str(original).unwrap();
assert_eq!(mapped_uuid.get_version(), Some(uuid::Version::SortRand));
assert_eq!(mapped_uuid.get_timestamp(), original_uuid.get_timestamp());
assert_eq!(flat[field], header[field]);
assert_eq!(flat[field], full[field]);
}
@@ -191,10 +197,26 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
}
assert_eq!(full["parent_thread_id"], flat["session_id"]);
assert_eq!(full["forked_from_thread_id"], flat["session_id"]);
assert_eq!(full["window_id"], full["context_window_id"]);
let mapped_thread = flat["thread_id"].as_str().unwrap();
let expected_window = format!("{mapped_thread}:{window_number}");
assert_eq!(headers["x-codex-window-id"], expected_window);
assert_eq!(flat["x-codex-window-id"], expected_window);
assert_eq!(
headers["x-client-request-id"].to_str().unwrap(),
mapped_thread
);
for metadata in [&header, &full] {
assert_eq!(metadata["window_id"], expected_window);
assert_eq!(metadata["window_number"], window_number);
assert_scoped_id(
metadata["context_window_id"].as_str().unwrap(),
CONTEXT_WINDOW_ID,
);
assert_ne!(metadata["context_window_id"], metadata["window_id"]);
}
assert!(header.get("tool_namespaces_info").is_none());
assert_eq!(full["tool_namespaces_info"]["inventory"], "body-only");
assert_eq!(full["future"], metadata()["future"]);
assert_eq!(full["future"], metadata(window_number)["future"]);
let input = body["input"].as_array().unwrap();
assert_eq!(
input[0]["internal_chat_message_metadata_passthrough"],
@@ -242,10 +264,8 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
assert_eq!(git[desktop], flat[wire]);
}
assert_eq!(git["path"], "/workspace/project");
assert_scoped_id(
body["prompt_cache_key"].as_str().unwrap(),
"custom-cache-key",
);
assert_scoped_id(body["prompt_cache_key"].as_str().unwrap(), SESSION_ID);
assert_eq!(body["prompt_cache_key"], flat["session_id"]);
captures.push((
[
flat["session_id"].clone(),
@@ -257,13 +277,69 @@ async fn account_correlation_preserves_turns_across_transports_and_account_switc
));
}
}
for index in [1, 2, 3, 6, 7] {
for index in [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15] {
assert_eq!(captures[0], captures[index]);
}
assert_eq!(captures[4], captures[5]);
assert_ne!(captures[0].0, captures[4].0);
assert_ne!(captures[0].1, captures[4].1);
assert_eq!(captures[0].2, captures[4].2);
for index in [9, 10, 11] {
assert_eq!(captures[8], captures[index]);
}
assert_ne!(captures[0].0, captures[8].0);
assert_ne!(captures[0].1, captures[8].1);
assert_eq!(captures[0].2, captures[8].2);
}
#[tokio::test]
async fn window_aliases_preserve_structure_without_reinterpreting_opaque_ids() {
for (original, number) in [
(format!("{THREAD_ID}:0"), Some(0)),
(format!("{THREAD_ID}:1"), Some(1)),
("custom-window".to_owned(), None),
(format!("{THREAD_ID}:next"), None),
] {
let metadata = json!({
"thread_id": THREAD_ID,
"window_id": original,
"codexWindowId": original,
"x-codex-window-id": original,
"context_window_id": original,
});
let request = capture_scoped_http_request(
"req_window_aliases",
"acct_scope_same",
"acct_scope_same",
json!({
"model":"gpt-5.4", "input":[],
"turnMetadata": metadata,
"turn_metadata": metadata.to_string(),
"client_metadata": metadata,
})
.as_object()
.unwrap()
.clone(),
Map::new(),
)
.await;
let body = captured_request_body(&request);
let encoded: Value = serde_json::from_str(body["turn_metadata"].as_str().unwrap()).unwrap();
for mapped in [&body["turnMetadata"], &encoded, &body["client_metadata"]] {
let window = mapped["window_id"].as_str().unwrap();
assert_eq!(mapped["codexWindowId"], window);
assert_eq!(mapped["x-codex-window-id"], window);
if let Some(number) = number {
assert_eq!(
window,
format!("{}:{number}", mapped["thread_id"].as_str().unwrap())
);
assert_ne!(mapped["context_window_id"], window);
} else {
assert_scoped_id(window, &original);
assert_eq!(mapped["context_window_id"], window);
}
let context =
uuid::Uuid::parse_str(mapped["context_window_id"].as_str().unwrap()).unwrap();
assert_eq!(context.get_version(), Some(uuid::Version::Random));
}
}
}
#[tokio::test]
@@ -285,7 +361,7 @@ async fn image_generation_and_edit_preserve_the_client_turn_identity() {
.unwrap()
.with_context(Map::from_iter([
("image_turn_id".into(), json!("source-turn")),
("turn_metadata".into(), json!(metadata().to_string())),
("turn_metadata".into(), json!(metadata(0).to_string())),
])),
));
let mut stream = provider
@@ -1284,6 +1284,7 @@ fn provider_and_quota_with_runtime_ports(
http.clone(),
base_url.clone(),
catalog_cache(),
crate::support::runtime_policy(),
));
let quota = Arc::new(CodexCredentialQuotaService::new(
store.repository(),
@@ -6326,8 +6327,10 @@ async fn installation_seed_scopes_http_identity_and_cache_by_key_and_account() {
.iter()
.zip(&bodies)
.map(|(request, body)| {
let id = request.headers["x-codex-installation-id"].to_str().unwrap();
assert_eq!(id, body["client_metadata"]["x-codex-installation-id"]);
let id = body["client_metadata"]["x-codex-installation-id"]
.as_str()
.unwrap();
assert!(!request.headers.contains_key("x-codex-installation-id"));
assert!(uuid::Uuid::parse_str(id).is_ok());
assert_ne!(id, KEY_INSTALLATION_SEED);
assert_ne!(id, other_seed);
@@ -6359,7 +6362,7 @@ async fn installation_seed_scopes_http_identity_and_cache_by_key_and_account() {
}
#[tokio::test]
async fn http_account_scoping_replaces_downstream_installation_header() {
async fn http_account_scoping_keeps_installation_in_metadata() {
for owner in ["acct_scope_same", "acct_scope_old"] {
let raw = r#"{"installation_id":"client-installation","future":true}"#;
let request = capture_scoped_http_request(
@@ -6400,10 +6403,7 @@ async fn http_account_scoping_replaces_downstream_installation_header() {
assert_ne!(installation_id, "client-installation");
assert!(uuid::Uuid::parse_str(installation_id).is_ok());
assert_eq!(body["installation_id"], installation_id);
assert_eq!(
captured_header_values(&request, "x-codex-installation-id"),
vec![installation_id.as_bytes().to_vec()]
);
assert!(captured_header_values(&request, "x-codex-installation-id").is_empty());
let headers = captured_header_values(&request, "x-codex-turn-metadata");
assert_eq!(headers.len(), 1);
for encoded in [
@@ -6421,7 +6421,7 @@ async fn http_account_scoping_replaces_downstream_installation_header() {
}
#[tokio::test]
async fn websocket_account_scoping_replaces_downstream_installation_header() {
async fn websocket_account_scoping_keeps_installation_in_metadata() {
for owner in ["acct_scope_same", "acct_scope_old"] {
let raw = r#"{"installation_id":"client-installation","future":true}"#;
let store = Arc::new(MemoryAccountStore::default());
@@ -6510,7 +6510,7 @@ async fn websocket_account_scoping_replaces_downstream_installation_header() {
assert_ne!(installation_id, KEY_INSTALLATION_SEED);
assert!(uuid::Uuid::parse_str(installation_id).is_ok());
assert_eq!(body["installation_id"], installation_id);
assert_eq!(header["x-codex-installation-id"], installation_id);
assert!(!header.contains_key("x-codex-installation-id"));
for encoded in [
header["x-codex-turn-metadata"]
.to_str()
@@ -6582,10 +6582,7 @@ async fn same_account_scope_preserves_future_protocol_shapes() {
body.pointer("/client_metadata/x-codex-installation-id")
);
assert!(body.pointer("/client_metadata/installation_id").is_none());
assert_eq!(
request.headers["x-codex-installation-id"].to_str().unwrap(),
body["client_metadata"]["x-codex-installation-id"]
);
assert!(!request.headers.contains_key("x-codex-installation-id"));
}
#[tokio::test]
@@ -1365,6 +1365,39 @@ pub(crate) fn profile(account_id: &str) -> CodexAccountProfile {
}
}
pub(crate) struct RequestProfileRuntimePolicy(
pub(crate) std::sync::Mutex<gateway_core::account::OpaqueProviderData>,
);
impl gateway_core::provider_ports::ProviderRuntimePolicyPort for RequestProfileRuntimePolicy {
fn load_request_profile<'a>(
&'a self,
provider: &'a ProviderKind,
) -> futures::future::BoxFuture<
'a,
Result<
Option<gateway_core::account::OpaqueProviderData>,
gateway_core::provider_ports::ProviderStoreError,
>,
> {
assert_eq!(provider.as_str(), "openai");
let profile = self.0.lock().unwrap().clone();
Box::pin(async move { Ok(Some(profile)) })
}
fn load_refresh_policy(
&self,
) -> futures::future::BoxFuture<
'_,
Result<
gateway_core::provider_ports::ProviderRefreshPolicy,
gateway_core::provider_ports::ProviderStoreError,
>,
> {
Box::pin(async { crate::support::runtime_policy().load_refresh_policy().await })
}
}
pub(crate) struct StaticRuntimePolicy;
impl ProviderRuntimePolicyPort for StaticRuntimePolicy {
@@ -226,7 +226,6 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
("x-codex-parent-thread-id", "parent-456"),
("x-openai-subagent", "future_codex_mode"),
("x-openai-memgen-request", "true"),
("x-codex-installation-id", "install-123"),
("session-id", "cp_derived"),
] {
let value = &headers.iter().find(|(header, _)| header == name).unwrap().1;
@@ -241,6 +240,7 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
}
}
for forbidden in [
"x-codex-installation-id",
"x-codex-turn-id",
"x-openai-internal-codex-residency",
"accept",
@@ -303,7 +303,7 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
codex_window_id: request.codex_window_id.as_deref(),
parent_thread_id: request.parent_thread_id.as_deref(),
cookie_header: Some("cf_clearance=old"),
installation_id: Some("install-1"),
installation_id: Some("3b11bc82-c666-417d-b9ff-2a34fd040385"),
session_id: Some("session-1"),
thread_id: None,
client_request_id: None,
@@ -345,7 +345,7 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
let scoped_session = read_header_value(&raw_request, "session-id").unwrap();
assert_ne!(scoped_session, "session-1");
assert!(uuid::Uuid::parse_str(scoped_session).is_ok());
for required in [
let mut expected_names = vec![
"authorization",
"chatgpt-account-id",
"originator",
@@ -362,14 +362,17 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
"x-responsesapi-include-timing-metrics",
"version",
"x-codex-parent-thread-id",
] {
assert!(header_names.iter().any(|name| name == required));
}
assert_eq!(
read_header_value(&raw_request, "x-codex-installation-id"),
Some("install-1")
);
"x-codex-routing-hint",
"content-encoding",
"content-length",
"host",
];
let mut header_names = header_names;
expected_names.sort_unstable();
header_names.sort_unstable();
assert_eq!(header_names, expected_names);
for forbidden in [
"x-codex-installation-id",
"x-codex-turn-id",
"x-openai-internal-codex-residency",
"sec-ch-ua",
@@ -64,6 +64,11 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
..Default::default()
};
let frozen = automatic.resolve(&state).unwrap();
// 从当前基线构造后继发布,升级离线版本无需重写缓存更新场景
let mut next = semver::Version::parse(&frozen.codex_version).unwrap();
next.patch += 1;
let next_sequence = 1 + next.major * 1_000_000 + next.minor * 1_000 + next.patch;
let next_version = next.to_string();
let fixed = ClientProfileSelection {
version_mode: VersionMode::Fixed,
codex_version: Some(frozen.codex_version.clone()),
@@ -83,9 +88,11 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
ProviderArtifactProfile::new(
provider.clone(),
"cli-linux-x64".into(),
157_001,
next_sequence,
chrono::Utc::now().into(),
OpaqueProviderData::new(json!({"version":"0.157.0"}).as_object().unwrap().clone()),
OpaqueProviderData::new(
json!({"version":next_version}).as_object().unwrap().clone(),
),
),
Duration::from_secs(86400),
)
@@ -100,11 +107,11 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
let current = selection.resolve(&state).unwrap();
let headers = build_codex_model_headers(&current, "Bearer fixture", None).unwrap();
let expected = format!(
"{originator}/0.157.0 (Alpine Linux 3.24.1; x86_64) xterm-256color ({originator}; 0.157.0)"
"{originator}/{next_version} (Alpine Linux 3.24.1; x86_64) xterm-256color ({originator}; {next_version})"
);
assert_eq!(headers["user-agent"], expected);
assert_eq!(headers["originator"], originator);
assert_eq!(headers["version"], "0.157.0");
assert_eq!(headers["version"], next_version.as_str());
}
assert_eq!(
fixed.resolve(&state).unwrap().user_agent(),
@@ -114,7 +121,6 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
custom.resolve(&state).unwrap().user_agent(),
frozen.user_agent()
);
assert_eq!(frozen.codex_version, "0.155.0");
// 缓存恢复与启动种子构成观察历史,滞后档位据此钉住上一版。
let lagged = ClientProfileSelection {
@@ -123,7 +129,7 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
};
assert_eq!(
lagged.resolve(&state).unwrap().user_agent(),
"codex-tui/0.155.0 (Alpine Linux 3.24.1; x86_64) xterm-256color (codex-tui; 0.155.0)"
frozen.user_agent()
);
let restarted = CodexWireProfileState::new(super::wire_profile());
@@ -150,6 +150,39 @@ fn windows_reads_versions_within_budget_despite_large_unrelated_entries() {
}
}
#[test]
fn windows_reads_zip64_end_records_with_sentinel_directory_fields() {
let mut bytes = zip(false, &core(true, false));
let expected = read_windows(Cursor::new(&bytes), "x86_64").unwrap();
let end = bytes.len() - 22;
let mut eocd = bytes.split_off(end);
assert_eq!(&eocd[..4], b"PK\x05\x06");
let entries = u64::from(u16::from_le_bytes(eocd[10..12].try_into().unwrap()));
let directory_size = u64::from(u32::from_le_bytes(eocd[12..16].try_into().unwrap()));
let directory_offset = u64::from(u32::from_le_bytes(eocd[16..20].try_into().unwrap()));
// MSIX 即使不足 4 GiB 也使用 ZIP64 尾记录,并把普通 EOCD 目录字段置为哨兵
bytes.extend_from_slice(b"PK\x06\x06");
bytes.extend_from_slice(&44_u64.to_le_bytes());
bytes.extend_from_slice(&45_u16.to_le_bytes());
bytes.extend_from_slice(&45_u16.to_le_bytes());
bytes.extend_from_slice(&[0; 8]);
for value in [entries, entries, directory_size, directory_offset] {
bytes.extend_from_slice(&value.to_le_bytes());
}
bytes.extend_from_slice(b"PK\x06\x07");
bytes.extend_from_slice(&0_u32.to_le_bytes());
bytes.extend_from_slice(&(end as u64).to_le_bytes());
bytes.extend_from_slice(&1_u32.to_le_bytes());
eocd[8..20].fill(0xff);
bytes.extend(eocd);
assert_eq!(
read_windows(ReadBudget(Cursor::new(&bytes).take(64 * 1024)), "x86_64").unwrap(),
expected
);
bytes[end..end + 4].fill(0);
assert!(read_windows(Cursor::new(bytes), "x86_64").is_err());
}
#[test]
fn truncated_or_corrupt_archives_do_not_publish_versions() {
let zip = zip(false, &core(true, false));
@@ -63,14 +63,23 @@ impl DesktopArtifactTransport for Transport {
previous: Option<VerifiedArtifact>,
) -> BoxFuture<'_, io::Result<VerifiedArtifact>> {
Box::pin(async move {
if previous.is_some() {
*self.seen_previous.lock().unwrap() += 1;
}
let previous = previous.expect("target has an offline artifact");
*self.seen_previous.lock().unwrap() += 1;
let round = *self.round.lock().unwrap();
if round == 1 && target == TARGETS[0] {
return Err(io::Error::other("fixture outage"));
}
let build = if round == 2 { 1 } else { 20000 + round };
let build = if round == 2 {
1
} else {
previous
.release
.desktop_build
.unwrap()
.parse::<u64>()
.unwrap()
+ 1
};
Ok(serde_json::from_value(json!({"identity":{"etag":format!("\"{build}\""),"size":1000}, "release":{"codexVersion":format!("0.200.{round}"), "desktopVersion":format!("30.1.{round}"), "desktopBuild":build.to_string(), "verifiedAt":null}})).unwrap())
})
}
@@ -4,12 +4,19 @@ use gateway_core::account::OpaqueProviderData;
use provider_openai::transport::profile::selection::{
CliEntry, ClientKind, ClientPlatform, ClientProfileSelection, VersionMode,
};
use provider_openai::transport::profile::{CodexBundledReleaseProfile, CodexWireProfileState};
use provider_openai::transport::profile::{
CodexBundledReleaseProfile, CodexWireProfile, CodexWireProfileState,
};
use serde_json::json;
#[test]
fn six_presets_have_explicit_availability_and_cli_has_no_desktop_suffix() {
let state = CodexWireProfileState::new(super::wire_profile());
let state = CodexWireProfileState::new(CodexWireProfile::default());
assert_eq!(
ClientProfileSelection::default().resolve(&state).unwrap(),
state.snapshot(),
"default requests and the default preset must use the same identity"
);
let options = state.selection_options().unwrap();
let presets = options.expose_to_provider()["presets"].as_array().unwrap();
assert_eq!(presets.len(), 6);
@@ -34,6 +41,7 @@ fn six_presets_have_explicit_availability_and_cli_has_no_desktop_suffix() {
assert!(profile.desktop_version.is_empty());
assert!(!profile.user_agent().contains("Desktop"));
assert!(profile.user_agent().ends_with("unknown"));
assert!(profile.verified_at > chrono::DateTime::UNIX_EPOCH);
}
for platform in [ClientPlatform::Linux, ClientPlatform::Windows] {
for arch in ["arm64", "x86_64"] {
@@ -46,6 +54,7 @@ fn six_presets_have_explicit_availability_and_cli_has_no_desktop_suffix() {
assert_eq!(profile.os_type, platform.os_type());
assert_eq!(profile.arch, arch);
assert!(profile.desktop_version.starts_with("26."));
assert!(profile.verified_at > chrono::DateTime::UNIX_EPOCH);
}
}
let unsupported = ClientProfileSelection {
@@ -1,8 +1,213 @@
//! 验证上游停止读请求时,发送超时仍能返回并释放连接
//! 验证上下游背压、取消与终态下的连接和任务回收
use super::*;
use gateway_core::{
diagnostics::TraceContext, engine::response_control::ResponseControl,
lifecycle::CancellationToken,
};
use std::assert_matches;
async fn wait_for_forwarded_frames(trace: &TraceContext, stage: &str, count: u64) {
timeout(Duration::from_secs(2), async {
while trace.snapshot().unwrap()["stageCounts"][stage]
.as_u64()
.unwrap_or_default()
< count
{
tokio::task::yield_now().await;
}
})
.await
.expect("forwarder should reach the backpressured frame");
}
fn response_tail(kind: &str) -> Message {
match kind {
"binary" => Message::Binary(vec![0].into()),
"delta" => Message::Text(
json!({"type":"response.output_text.delta", "delta":"x"})
.to_string()
.into(),
),
"completed" => {
Message::Text(completed_websocket_response("resp_backpressure", 2, 1).into())
}
kind => Message::Text(
json!({"type":format!("response.{kind}"), "response":{
"id":"resp_backpressure", "status":kind, "output":[]
}})
.to_string()
.into(),
),
}
}
async fn send_backpressured_response(
websocket: &mut tokio_tungstenite::WebSocketStream<TcpStream>,
tail: &str,
) {
// 先填满交付队列,后续帧由诊断计数确认已到达 forwarder,不靠 sleep 猜测调度
for _ in 0..16 {
websocket.send(response_tail("delta")).await.unwrap();
}
websocket.send(response_tail(tail)).await.unwrap();
}
async fn expect_upstream_socket_closed(
websocket: &mut tokio_tungstenite::WebSocketStream<TcpStream>,
) {
let close = timeout(Duration::from_secs(2), websocket.next())
.await
.expect("cancellation must release the socket while the receiver remains alive");
assert_matches!(
close,
None | Some(Ok(Message::Close(_)))
| Some(Err(tungstenite::Error::Protocol(
tungstenite::error::ProtocolError::ResetWithoutClosingHandshake
)))
);
}
#[tokio::test]
async fn backpressured_delivery_should_stop_on_downstream_cancel_or_pool_shutdown() {
for cancel_downstream in [true, false] {
for tail in ["delta", "binary", "completed", "failed", "incomplete"] {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let mut server = tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();
let mut websocket = accept_codex_test_websocket(socket).await;
websocket.next().await.unwrap().unwrap();
send_backpressured_response(&mut websocket, tail).await;
expect_upstream_socket_closed(&mut websocket).await;
});
let pool = Arc::new(CodexWebSocketPool::default());
let backend = CodexBackendClient::new(
reqwest::Client::builder().no_proxy().build().unwrap(),
format!("http://{addr}"),
test_wire_profile(),
)
.with_websocket_pool(Arc::clone(&pool));
let cancellation = CancellationToken::new();
let control =
ResponseControl::default().with_connection_cancellation(cancellation.clone());
let trace = TraceContext::new("backpressured_delivery");
let mut context = request_context("backpressured_delivery", Some("chatgpt-account"))
.with_response_control(Some(&control));
context.trace = Some(&trace);
let mut request = pooled_websocket_request("backpressured_delivery");
request.downstream_websocket_connection_id = Some("downstream_backpressure".into());
let mut response = backend
.create_response_stream(&request, context)
.await
.unwrap();
let (stage, count) = if tail == "binary" {
("upstream.binary", 1)
} else {
("upstream.event", 17)
};
wait_for_forwarded_frames(&trace, stage, count).await;
if cancel_downstream {
cancellation.cancel();
// 必须在 shutdown 前看到物理连接关闭,避免停机掩盖下游取消失效
(&mut server).await.unwrap();
}
timeout(Duration::from_secs(2), pool.shutdown())
.await
.expect("shutdown must join delivery even with a full receiver");
if !cancel_downstream {
(&mut server).await.unwrap();
}
// 收尾前不消费也不丢弃 body,已交付的事件仍可正常读完
let mut buffered = 0;
while response.body.next().await.transpose().unwrap().is_some() {
buffered += 1;
}
assert_eq!(
buffered, 16,
"tail={tail}, cancel_downstream={cancel_downstream}"
);
}
}
}
#[tokio::test]
async fn terminal_should_release_or_reuse_socket_before_delivery() {
for tail in ["completed", "failed", "incomplete"] {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let mut server = tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();
let mut websocket = accept_codex_test_websocket(socket).await;
websocket.next().await.unwrap().unwrap();
send_backpressured_response(&mut websocket, tail).await;
if tail == "completed" {
let next = timeout(Duration::from_secs(2), websocket.next())
.await
.expect("continuation should reuse the completed socket")
.unwrap()
.unwrap();
let Message::Text(next) = next else {
panic!("expected continuation request");
};
assert_eq!(
serde_json::from_str::<Value>(&next).unwrap()["previous_response_id"],
"resp_backpressure"
);
websocket
.send(Message::Text(
completed_websocket_response("resp_next", 2, 1).into(),
))
.await
.unwrap();
}
expect_upstream_socket_closed(&mut websocket).await;
});
let pool = Arc::new(CodexWebSocketPool::default());
let backend = CodexBackendClient::new(
reqwest::Client::builder().no_proxy().build().unwrap(),
format!("http://{addr}"),
test_wire_profile(),
)
.with_websocket_pool(Arc::clone(&pool));
let trace = TraceContext::new("terminal_delivery");
let mut context = request_context("terminal_delivery", Some("chatgpt-account"));
context.trace = Some(&trace);
let mut request = pooled_websocket_request("terminal_delivery");
let mut response = backend
.create_response_stream(&request, context)
.await
.unwrap();
wait_for_forwarded_frames(&trace, "upstream.event", 17).await;
if tail == "completed" {
request.set_previous_response_id(Some("resp_backpressure".into()));
request.previous_response_scope = Some(PreviousResponseScope::ConnectionLocal);
let next = backend
.create_response(
&request,
request_context("terminal_next", Some("chatgpt-account")),
)
.await
.expect("completed continuation should be reusable before terminal consumption");
assert!(next.websocket_pool_decision.unwrap().is_reuse());
} else {
// 失败终态必须释放 socket,不能依赖客户端读走终态
(&mut server).await.unwrap();
}
let mut events = Vec::new();
while let Some(chunk) = response.body.next().await {
events.push(serde_json::from_slice::<Value>(&chunk.unwrap()).unwrap());
}
assert_eq!(events.len(), 17);
assert_eq!(events.last().unwrap()["type"], format!("response.{tail}"));
pool.shutdown().await;
if tail == "completed" {
(&mut server).await.unwrap();
}
}
}
#[tokio::test]
async fn send_timeout_should_abort_stalled_pump_and_allow_next_request() {
let socket = tokio::net::TcpSocket::new_v4().unwrap();
@@ -100,6 +100,7 @@ async fn official_websocket_wire_fingerprint_should_remain_aligned() {
session_id: Some("session-fingerprint"),
thread_id: Some("thread-fingerprint"),
client_request_id: Some("request-fingerprint"),
installation_id: Some("3b11bc82-c666-417d-b9ff-2a34fd040385"),
..request_context("req_wire_fingerprint", Some("account-fingerprint"))
},
)
@@ -151,7 +152,6 @@ async fn official_websocket_wire_fingerprint_should_remain_aligned() {
read_header_value(&opening, "originator"),
read_header_value(&opening, "version"),
read_header_value(&opening, "openai-beta"),
read_header_value(&opening, "x-client-request-id"),
read_header_value(&opening, "x-codex-routing-hint"),
),
(
@@ -160,10 +160,12 @@ async fn official_websocket_wire_fingerprint_should_remain_aligned() {
Some("codex_cli_rs"),
Some("1.2.3"),
Some("responses_websockets=2026-02-06"),
Some("request-fingerprint"),
Some("model=gpt-5.5"),
)
);
let request_id = read_header_value(&opening, "x-client-request-id").unwrap();
assert_ne!(request_id, "request-fingerprint");
assert!(uuid::Uuid::parse_str(request_id).is_ok());
assert_eq!(
STANDARD
.decode(websocket_key)
+1
View File
@@ -39,3 +39,4 @@ b215ea90d5c8753aeb8192b17ffe65f7a95c1d620aaeaf9fc4337d8cf6f2634e 0037_retry_aft
a6b4e81d38e5d70776a75eba22749354573ebcb186160ff9eb45fd91157b3e0d 0039_queue_timeouts_quota_credits_and_installation_seed.sql
cd9e31400fc6b0d49a6515048c104341fca2cf44f290d89c401b41f94ead84a3 0040_account_scheduling_key_budgets_and_peer_routing.sql
aeff99fee7cef5a553ae49ffe27a48f49e31ccfb3fbc61a2482c9b109478202d 0041_account_model_catalogs.sql
19eba6b493647da51996831f72d7bc83e95416c06f207ea685f8f5e2edea7c64 0042_usage_retention_days.sql
@@ -0,0 +1,7 @@
-- 保留期仅要求正整数天数,沿用当前设置与默认值
alter table runtime_settings drop constraint runtime_settings_retention_ck;
alter table runtime_settings add constraint runtime_settings_retention_ck check (
usage_retention_days > 0
and ops_event_retention_days > 0
and audit_retention_days > 0
);
+6
View File
@@ -385,6 +385,12 @@ HTTP 在数据库迁移和初始化完成后才监听。迁移期间启动日志
不要把未脱敏的 `docker compose config` 或 `docker inspect` 输出上传到工单;它们会包含
PostgreSQL/Redis 启动密码。日常校验使用 `config --quiet`
### 安装管理端应用
使用 Chrome、Edge 等支持 PWA 的浏览器访问管理端,可通过地址栏或浏览器菜单安装应用,安装后以独立窗口打开站点首页,沿用现有登录流程
安装入口要求 HTTPS,或本机的 `http://localhost`、`http://127.0.0.1`;普通 HTTP 局域网地址不满足条件。入口是否显示还取决于浏览器、访问交互及是否已安装。应用需要联网访问网关,不提供离线使用
### 启动后的运行设置
在管理端修改以下设置,保存后对新请求生效,重启不会覆盖已保存的选择:
+14 -7
View File
@@ -500,7 +500,9 @@ Responses API Key 与 OAuth 共用模拟客户端画像(`User-Agent`、`origin
包括会话、线程、Lite 和其他业务扩展头。
上游认证只来自选中的账号;API Key 不携带 OAuth Cookie、ChatGPT 账号身份或下游的
`X-OpenAI-Actor-Authorization` 托管认证声明。
`x-codex-installation-id` 在 Key 配置种子时,由种子、Key ID 与选中账号 ID 稳定派生;未配置时使用账号已持久化的安装 ID,与正文 `client_metadata` 中的安装身份一致
Responses 的安装身份通过正文 `client_metadata` 及 turn metadata 发送,不默认添加独立的
`x-codex-installation-id` 请求头,也不透传下游同名头。Key 配置种子时,安装身份由种子、Key ID 与选中账号 ID
稳定派生;未配置时使用账号已持久化的安装 ID。Images、Search 和 Live 保留各自的安装头
`x-openai-internal-codex-residency` 和 `x-openai-account-routing-override` 分别由部署配置
`openai.residency`、`openai.account_routing_override` 控制,配置值原样发送并覆盖客户端同名头,
省略或 `null` 时删除对应请求头。配置说明见 [部署与运维](../deploy/README.md#启动后的运行设置)
@@ -519,9 +521,12 @@ Responses 明确换号时清理 `traceparent`、`tracestate`、`sentry-trace`、
`x-openai-fedramp`,同时清理正文、`client_metadata`、turn metadata 中的同名字段及
`ws_request_header_*` 副本。同账号及归属未知时保留原值,业务输入和工具结果保持原始内容
会话、线程、窗口及父子线程引用在出站时按本次安装身份稳定映射为 UUID;
默认使用账号安装身份;Key 配置种子时,每个 Key × 账号组合拥有独立的安装身份,换账号会改变映射。HTTP 请求头、正文 metadata、WS 副本及
已识别的 Desktop 自动 Git 上下文使用一致映射。
会话、线程及父子线程引用在出站时按本次安装身份稳定映射。UUIDv7 输入保留原始毫秒时间戳和版本,
随机部分按安装身份改写;其他标识映射为 UUIDv4 形状,不补造创建时间。
官方格式的窗口 ID 保留 `{映射后的 thread_id}:{原窗口序号}` 结构,`context_window_id` 作为独立标识处理;
非官方格式的窗口 ID 按普通标识映射。
默认使用账号安装身份;Key 配置种子时,每个 Key × 账号组合拥有独立的安装身份,换账号会改变映射。
HTTP 请求头、正文 metadata、WS 副本及已识别的 Desktop 自动 Git 上下文使用一致映射,UUIDv7 的创建时间仍可见。
轮次及父子轮次引用保留客户端原值,包括输入项的 `internal_chat_message_metadata_passthrough.turn_id`、
请求 metadata 中的 `turn_id`、`parent_turn_id`、`root_turn_id`,以及 `x-client-turn-id`、
Images 的 `x-codex-image-turn-id` 和 Desktop 自动 Git 上下文的 `clientUserMessageId`。
@@ -1498,7 +1503,7 @@ OAuth 等待回调期间不持有保护;提交仍拒绝已删除或连接配
### 账号连接测试 SSE
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,不参与普通账号轮换。成功流沿用
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,采用当前全局客户端身份,不参与普通账号轮换。成功流沿用
`test_start`、`request`、`content`、`test_complete` 事件;失败事件为:
```json
@@ -2165,7 +2170,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
### 历史保留
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,支持 1~31 的整数天数。
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,须为正整数,不设业务上限。
`opsEventRetentionDays` 与 `auditRetentionDays` 分别控制独立运维事件和管理审计,默认 30、90,均须为正整数
清理任务每轮读取已保存设置,按完成时间分批删除过期请求及关联事件,不删除运行中的请求;有界单轮结束后等待一分钟再检查。
@@ -2186,6 +2191,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
0% 或缺少活跃证据时允许一次轻量预热,不通过倒计时差值判断活跃,也不额外回查额度。
因此已激活但显示 0% 的账号也可能收到预热请求。
不存在的本地时刻跳过,重复时刻只执行较早一次;执行进度跨重启保留,时钟回拨不补跑已领取时刻之前的时段。
预热每轮采用当前 OpenAI 全局客户端身份,不使用单个 Key 的覆盖。
只有收到响应成功终态才记为预热成功;预热不计入客户端业务用量
### 请求位置
@@ -2425,7 +2431,8 @@ models.dev 同步只导入可表示为当前文本 Token 计价的 OpenAI/xAI
`openaiClientProfile` 保存通用选择,首次默认 `MacOS · Desktop · 自动最新`。
兼容字段的更新语义见上节。初始化不读取 YAML 身份字段。
该配置作用于 Client Key 的 OpenAI 模型请求,适用于 HTTP/SSE、WebSocket、Images 和 Search。
该配置作用于 Client Key 的 OpenAI 模型请求,适用于 HTTP/SSE、WebSocket、Images 和 Search,
也用于账号模型目录的后台发现、手动刷新、导出及定时预热;下次执行时采用当前全局选择,不使用单个 Key 的覆盖。
不改变 xAI、入站客户端版本门禁、账号认证或后台 Desktop 专属操作
自定义配置使用 `{ "mode": "custom", "userAgent": "完整 UA" }`。
+6 -1
View File
@@ -531,6 +531,7 @@ OpenAI 的连接池监督该取消信号,按当前归属保护绑定连接;
重新绑定或切到 HTTP 时移除失去用途的旧连接,失效 reservation 不能重新归池。
普通空闲期限复用池槽位的 `idle_since`,只在归池时重置;心跳不刷新业务空闲,建连与在途不参与空闲回收。
回收先移出可复用集合,再在锁外有界关闭并等待 pump/socket 释放,物理容量随 socket 归还;批量关闭并行执行。
上游转发等待下游消费时仍响应连接取消和停机;终态先完成连接归还或销毁,再交付下游。
Codex OAuth 上游 WS 在首个请求写入后记录搜索声明,握手完成但尚未发送请求的连接仍未初始化。
后续首次声明搜索时,连接池回收不兼容的原连接;普通新链重新建连,连接内续接交由客户端完整重放。
该判断共用于 HTTP 与 WS 入站,不修改客户端工具声明,也不保留对话正文。
@@ -600,12 +601,16 @@ OpenAI 模型目录用于发现与原生 Responses 账号资格筛选。
各 Provider 唯一负责默认值、校验、版本来源及 UA 生成,Admin 提供管理和生效预览。
首次初始化只写入内置默认选择;YAML 不定义客户端身份,也不作为数据库初始化或请求解析的来源。
官方发布资料与用户选择分开:OpenAI 在 Redis 按 Provider、客户端、平台、架构隔离可重建版本缓存,
离线版本、制品身份及预设环境默认值集中在
[`transport/profile/baseline.rs`](../backend/crates/providers/openai/src/transport/profile/baseline.rs),由发布检查与预设解析共用。
Desktop 完整制品元组原子更新。CLI 的 TUI/Exec 入口共用官方 CLI 版本资料,Provider 在一次身份解析中
生成一致的 UA、入口后缀与配套请求头,后台更新不修改用户选择的运行环境。
完整自定义 UA 经 Provider 校验后原样传递,配套头统一解析;固定和自定义配置不受后台更新影响。
OpenAI Provider 按 Key 种子、Key ID 与账号 ID 派生出站安装身份;未配置种子时使用账号身份,不改写账号凭据。
普通连接按生效身份匹配,精确续写固定原连接与身份;配置合同见 [Key 客户端身份](api.md#key-客户端身份)。
后台账号和 Desktop 专属操作使用独立官方 Desktop 画像,不接受 Key 覆盖。
OpenAI 账号目录发现和定时预热每轮读取并解析一次当前全局画像,查询版本和请求头使用同一快照,不接受 Key 覆盖。
其余后台账号和 Desktop 专属操作使用独立官方 Desktop 画像。
管理端连接测试从本次发布快照携带全局画像,经 Provider 解析后冻结为探测身份。
xAI 以内置画像为版本检查基线,在进程内更新 Grok CLI 发布资料;模型与压缩请求使用已保存的用户选择,
OAuth、后台目录和额度查询使用内置官方画像。两者均不回写 `config.yaml`
+1
View File
@@ -7,6 +7,7 @@
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="manifest" href="/manifest.webmanifest" />
<title>Codex Proxy RS</title>
</head>
<body>
+15
View File
@@ -0,0 +1,15 @@
{
"id": "/",
"name": "Codex Proxy RS",
"short_name": "Codex Proxy",
"start_url": "/",
"scope": "/",
"display": "standalone",
"icons": [
{
"src": "/favicon.svg",
"sizes": "any",
"type": "image/svg+xml"
}
]
}
+2 -2
View File
@@ -309,7 +309,7 @@
"retentionDaysLabel": "History retention (d)"
},
"retentionDays": "Retention (d)",
"enterDays": "Please enter 1–31 days",
"enterDays": "Enter retention days",
"day": "d",
"location": {
"requestLocationOverride": "Request location override",
@@ -626,7 +626,7 @@
"invalidBodyLimit": "Responses decompression limit must be a positive integer in MiB",
"invalidQuotaThreshold": "Remaining weekly quota threshold must be an integer from 0 to 100, leave blank for no limit",
"invalidResetLeadTime": "Reset credit lead time must be an integer from 1 to 1,440 minutes",
"invalidRetention": "History retention must be an integer from 1 to 31 days",
"invalidRetention": "History retention must be a positive integer",
"invalidQueueSettings": "Queue capacity must be an integer from 0 to 1,000 and timeout from 1 to 120 seconds",
"invalidProbeModel": "The probe model must be at most 128 characters without control characters",
"invalidWarmupCron": "Enter a valid Cron expression",
@@ -309,7 +309,7 @@
"retentionDaysLabel": "历史保留天数"
},
"retentionDays": "保留天数",
"enterDays": "请输入 1–31 天",
"enterDays": "请输入保留天数",
"day": "天",
"location": {
"requestLocationOverride": "请求位置覆盖",
@@ -626,7 +626,7 @@
"invalidBodyLimit": "Responses 解压上限应为有效的正整数(MiB)",
"invalidQuotaThreshold": "剩余周额度上限应为 0~100 的整数,留空表示不限制",
"invalidResetLeadTime": "重置卡提前使用时间应为 1~1440 分钟的整数",
"invalidRetention": "历史保留天数应为 1–31 的整数",
"invalidRetention": "历史保留天数应为正整数",
"invalidQueueSettings": "队列容量应为 0~1000 的整数,排队超时应为 1~120 秒的整数",
"invalidProbeModel": "探测模型最多 128 个字符,不能包含控制字符",
"invalidWarmupCron": "请输入有效的 Cron 表达式",
@@ -309,7 +309,7 @@
"retentionDaysLabel": "歷史保留天數"
},
"retentionDays": "保留天數",
"enterDays": "請輸入 1–31 天",
"enterDays": "請輸入保留天數",
"day": "天",
"location": {
"requestLocationOverride": "請求位置覆蓋",
@@ -626,7 +626,7 @@
"invalidBodyLimit": "Responses 解壓上限應爲有效的正整數(MiB)",
"invalidQuotaThreshold": "剩餘周額度上限應爲 0~100 的整數,留空表示不限制",
"invalidResetLeadTime": "重置卡提前使用時間應爲 1~1440 分鐘的整數",
"invalidRetention": "歷史保留天數應爲 1–31 的整數",
"invalidRetention": "歷史保留天數應爲正整數",
"invalidQueueSettings": "隊列容量應爲 0~1000 的整數,排隊超時應爲 1~120 秒的整數",
"invalidProbeModel": "探測模型最多 128 個字符,不能包含控制字符",
"invalidWarmupCron": "請輸入有效的 Cron 表達式",
@@ -12,7 +12,7 @@ const model = defineModel<string>({ required: true })
<ZCard :title="t('settings.history.title')" :description="t('settings.history.retentionHelp')">
<div class="grid min-w-0 gap-4.5 max-w-6xl sm:grid-cols-2 sm:items-end">
<ZFormItem :show-message="Boolean(model.trim())" prop="usageRetentionDays" :label="t('settings.retentionDays')" :description="t('settings.history.retentionConstraints')">
<ZInput v-model="model" :aria-label="t('settings.history.retentionDaysLabel')" type="number" min="1" max="31" step="1" :placeholder="t('settings.enterDays')">
<ZInput v-model="model" :aria-label="t('settings.history.retentionDaysLabel')" type="number" min="1" step="1" :placeholder="t('settings.enterDays')">
<template #suffix>
{{ t('settings.day') }}
</template>
@@ -9,7 +9,7 @@ type SettingsForm = ReturnType<typeof useSettingsForm>['form']
export function useSettingsFormRules(form: () => SettingsForm) {
const { t } = useI18n({ useScope: 'global' })
const integer = (min: number, max: number, message: string): FormRule => ({
const integer = (min: number, max: number | undefined, message: string): FormRule => ({
type: 'integer',
required: true,
min,
@@ -47,7 +47,7 @@ export function useSettingsFormRules(form: () => SettingsForm) {
...integer(0, 100, t('settings.form.invalidQuotaThreshold')),
required: false,
},
'usageRetentionDays': integer(1, 31, t('settings.form.invalidRetention')),
'usageRetentionDays': integer(1, undefined, t('settings.form.invalidRetention')),
'minCodexDesktopVersion': version(),
'minCodexCliVersion': version(),
'accountWarmupCron': [
+9 -7
View File
@@ -1,19 +1,21 @@
# v3.47.0
# v3.48.0
## 新增功能
- 账号预热:支持配置多条独立 Cron 规则,分别安排预热时间,同一分钟命中多条规则也只执行一次
- 请求历史:保留期可设为超过 31 天的正整数,默认仍为 31 天,升级保留已有设置
- 管理端:支持在兼容浏览器中安装为独立窗口应用,需通过 HTTPS 或本机回环地址访问,使用时仍需联网
## 优化改进
- 用量统计:减少分组重算的临时文件写入
- 统计任务:可通过 `store.pool.analytics_projection_work_mem_mb` 调整增量投影和历史回填事务的工作内存,默认沿用 PostgreSQL 设置
- OpenAI 客户端画像:无法在线获取版本时,内置 CLI 版本为 0.169.0,macOS 和 Linux Desktop 的 Core 版本为 0.169.0-alpha.11.1,Windows 为 0.167.0-alpha.4.1
## 问题修复
- OpenAI 模型调度:目录刷新期间或刷新失败后保留最近一次成功目录的排除规则,避免选中不支持目标模型的账号
- 管理界面:恢复主题预设的页面、卡片和浮层底色,保留自定义背景及功能色
- OpenAI 账号:模型目录刷新、导出、预热和连接测试使用已保存的全局客户端画像,修复选用新版 CLI 后目录仍可能缺少模型的问题
- Responses:HTTP 和 WebSocket 请求通过正文传递安装身份,默认不再额外发送独立的安装 ID 请求头
- WebSocket:下游读取缓慢时,取消请求或关闭连接池仍能及时释放上游连接
- 依赖安全:更新 Quinn 和 rustix,修复依赖审计报告的问题
## 升级须知
- 原有单条预热配置可直接沿用,API 中的多条规则以半角分号分隔,总长度含分隔符不超过 255 字节
- Responses 的 UUIDv7 会话和线程标识、Codex 复合窗口标识在升级后会映射为新值;账号隔离保留,UUIDv7 的原始创建时间和窗口序号对上游可见
+5 -5
View File
@@ -1,7 +1,7 @@
version: 3.47.0
version: 3.48.0
published:
'3':
stable: 3.46.0
rc: 3.46.0
beta: 3.46.0
alpha: 3.46.0
stable: 3.47.0
rc: 3.47.0
beta: 3.47.0
alpha: 3.47.0