mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d787195f4e | ||
|
|
8681d6851e | ||
|
|
d99ecf9aaf | ||
|
|
8f40bfda7c | ||
|
|
72215fe62a | ||
|
|
f61995e61d | ||
|
|
f85d80d756 | ||
|
|
e1f84c210b | ||
|
|
0e02a056b5 | ||
|
|
1eac929e9d | ||
|
|
9b4a042be5 | ||
|
|
3bd042181e | ||
|
|
2cae28d7f4 | ||
|
|
bab781cbe0 |
Generated
+6
-6
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -423,7 +422,12 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
(false, "08:00", false),
|
||||
(true, "0 8 * * MON-FRI", true),
|
||||
(true, "60 8 * * *", false),
|
||||
(true, "0 8 * * *;30 13 * * *", true),
|
||||
(true, "0 8 * * *\n30 13 * * *", false),
|
||||
(true, "0 8 * * *;60 13 * * *", false),
|
||||
(false, "0 8 * * *;", false),
|
||||
(true, "0 8 * * *;;30 13 * * *", false),
|
||||
(true, "0 8 * * *\r\n30 13 * * *", false),
|
||||
] {
|
||||
let mut body = update_body();
|
||||
body["accountWarmupEnabled"] = json!(enabled);
|
||||
@@ -436,6 +440,11 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
assert_eq!(error.field(), "accountWarmupCron");
|
||||
}
|
||||
}
|
||||
let mut oversized = update_body();
|
||||
oversized["accountWarmupCron"] = json!(["* * * * *"; 26].join(";"));
|
||||
let request: UpdateRuntimeSettingsRequest = serde_json::from_value(oversized).unwrap();
|
||||
assert_eq!(request.validate().unwrap_err().field(), "accountWarmupCron");
|
||||
|
||||
let mut legacy = update_body();
|
||||
legacy.as_object_mut().unwrap().remove("accountWarmupCron");
|
||||
legacy["accountWarmupScheduleTime"] = json!("08:00");
|
||||
|
||||
@@ -28,11 +28,10 @@ pub struct StoredAccountCatalog {
|
||||
pub document: bytes::Bytes,
|
||||
}
|
||||
|
||||
/// 批量选号的存储优化:只有账号自己的当前版本、有效时间和目录摘要都匹配才可跳过
|
||||
/// 批量选号只按账号当前凭据和套餐的目录摘要排除候选
|
||||
pub struct AccountCatalogOmissions {
|
||||
pub documents: Vec<[u8; 32]>,
|
||||
pub exceptions: Vec<ProviderAccountId>,
|
||||
pub observed_after: std::time::SystemTime,
|
||||
pub as_of: std::time::SystemTime,
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
@@ -52,7 +60,7 @@ pub trait ProviderRuntimePolicyPort: Send + Sync {
|
||||
}
|
||||
}
|
||||
|
||||
/// 校验五段 Cron 预热计划
|
||||
/// 校验单条五段 Cron 表达式
|
||||
#[must_use]
|
||||
pub fn valid_warmup_cron(value: &str) -> bool {
|
||||
parse_warmup_cron(value).is_some()
|
||||
@@ -73,7 +81,7 @@ fn parse_warmup_cron(value: &str) -> Option<cron::Schedule> {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProviderWarmupPolicy {
|
||||
enabled: bool,
|
||||
cron: String,
|
||||
schedules: Vec<cron::Schedule>,
|
||||
model: Option<String>,
|
||||
}
|
||||
|
||||
@@ -84,29 +92,47 @@ impl ProviderWarmupPolicy {
|
||||
cron: &str,
|
||||
model: Option<&str>,
|
||||
) -> Result<(), &'static str> {
|
||||
if (enabled || !cron.is_empty()) && !valid_warmup_cron(cron) {
|
||||
Self::validate_and_parse(enabled, cron, model).map(|_| ())
|
||||
}
|
||||
|
||||
fn validate_and_parse(
|
||||
enabled: bool,
|
||||
cron: &str,
|
||||
model: Option<&str>,
|
||||
) -> Result<Vec<cron::Schedule>, &'static str> {
|
||||
if cron.len() > 255 || (enabled && cron.is_empty()) {
|
||||
return Err("account_warmup_cron");
|
||||
}
|
||||
// 分号分隔独立表达式,逗号保留给 Cron 字段内的列表
|
||||
let schedules = if cron.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
cron.split(';')
|
||||
.map(parse_warmup_cron)
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.ok_or("account_warmup_cron")?
|
||||
};
|
||||
if (enabled && model.is_none()) || !valid_optional_probe_model(model) {
|
||||
return Err("account_warmup_model");
|
||||
}
|
||||
Ok(())
|
||||
Ok(schedules)
|
||||
}
|
||||
|
||||
pub fn try_new(
|
||||
enabled: bool,
|
||||
cron: String,
|
||||
cron: &str,
|
||||
model: Option<String>,
|
||||
) -> Result<Self, ProviderStoreError> {
|
||||
if Self::validate_values(enabled, &cron, model.as_deref()).is_err() {
|
||||
return Err(ProviderStoreError::new(
|
||||
ProviderStoreErrorKind::InvalidData,
|
||||
"validate warmup policy",
|
||||
));
|
||||
}
|
||||
let schedules =
|
||||
Self::validate_and_parse(enabled, cron, model.as_deref()).map_err(|_| {
|
||||
ProviderStoreError::new(
|
||||
ProviderStoreErrorKind::InvalidData,
|
||||
"validate warmup policy",
|
||||
)
|
||||
})?;
|
||||
Ok(Self {
|
||||
enabled,
|
||||
cron,
|
||||
schedules,
|
||||
model,
|
||||
})
|
||||
}
|
||||
@@ -115,7 +141,7 @@ impl ProviderWarmupPolicy {
|
||||
pub const fn disabled() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
cron: String::new(),
|
||||
schedules: Vec::new(),
|
||||
model: None,
|
||||
}
|
||||
}
|
||||
@@ -125,11 +151,6 @@ impl ProviderWarmupPolicy {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cron(&self) -> &str {
|
||||
&self.cron
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn model(&self) -> Option<&str> {
|
||||
self.model.as_deref()
|
||||
@@ -138,7 +159,10 @@ impl ProviderWarmupPolicy {
|
||||
/// 匹配部署时区中的整分钟时刻,时区重叠及执行去重由 Worker 与存储端口负责
|
||||
#[must_use]
|
||||
pub fn matches_slot(&self, slot: chrono::NaiveDateTime) -> bool {
|
||||
parse_warmup_cron(&self.cron).is_some_and(|schedule| schedule.includes(slot.and_utc()))
|
||||
let instant = slot.and_utc();
|
||||
self.schedules
|
||||
.iter()
|
||||
.any(|schedule| schedule.includes(instant))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -252,14 +252,10 @@ fn warmup_cron_validation_and_calendar_matching() {
|
||||
] {
|
||||
assert!(!valid_warmup_cron(value), "{value:?}");
|
||||
}
|
||||
let policy = ProviderWarmupPolicy::try_new(
|
||||
true,
|
||||
"0 8,13 * * MON-FRI".to_owned(),
|
||||
Some("test-model".to_owned()),
|
||||
)
|
||||
.unwrap();
|
||||
let policy =
|
||||
ProviderWarmupPolicy::try_new(true, "0 8,13 * * MON-FRI", Some("test-model".to_owned()))
|
||||
.unwrap();
|
||||
assert!(policy.enabled());
|
||||
assert_eq!(policy.cron(), "0 8,13 * * MON-FRI");
|
||||
assert_eq!(policy.model(), Some("test-model"));
|
||||
for (day, hour, minute, expected) in [
|
||||
(9, 8, 0, true),
|
||||
@@ -275,19 +271,39 @@ fn warmup_cron_validation_and_calendar_matching() {
|
||||
.unwrap();
|
||||
assert_eq!(policy.matches_slot(slot), expected, "{slot}");
|
||||
}
|
||||
assert!(ProviderWarmupPolicy::try_new(false, String::new(), None).is_ok());
|
||||
assert!(
|
||||
ProviderWarmupPolicy::try_new(true, String::new(), Some("test-model".to_owned())).is_err()
|
||||
);
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "08:00".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "", None).is_ok());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "", Some("test-model".to_owned())).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "08:00", None).is_err());
|
||||
assert!(!valid_warmup_cron(&format!("0{}8 * * *", " ".repeat(256))));
|
||||
let leap = ProviderWarmupPolicy::try_new(false, "0 0 29 FEB *".to_owned(), None).unwrap();
|
||||
let leap = ProviderWarmupPolicy::try_new(false, "0 0 29 FEB *", None).unwrap();
|
||||
let slot = NaiveDate::from_ymd_opt(2028, 2, 29)
|
||||
.unwrap()
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.unwrap();
|
||||
assert!(leap.matches_slot(slot));
|
||||
assert!(!leap.matches_slot(slot + chrono::Duration::days(1)));
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "invalid".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "0 8 * * *".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "invalid", None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "0 8 * * *", None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warmup_matches_only_the_scheduled_hour_and_minute_pairs() {
|
||||
use chrono::NaiveDate;
|
||||
use gateway_core::provider_ports::ProviderWarmupPolicy;
|
||||
|
||||
let policy = ProviderWarmupPolicy::try_new(
|
||||
true,
|
||||
"58 5 * * *;59 10 * * *;0 16 * * *;1 21 * * *;0 16 * * MON-FRI",
|
||||
Some("test-model".to_owned()),
|
||||
)
|
||||
.unwrap();
|
||||
let date = NaiveDate::from_ymd_opt(2026, 11, 13).unwrap();
|
||||
for minute in 0..1440 {
|
||||
let slot = date.and_hms_opt(minute / 60, minute % 60, 0).unwrap();
|
||||
assert_eq!(
|
||||
policy.matches_slot(slot),
|
||||
[358, 659, 960, 1261].contains(&minute),
|
||||
"{slot}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,7 +336,8 @@ async fn connect(
|
||||
),
|
||||
};
|
||||
let analytics =
|
||||
postgres::PgRequestAnalyticsRepository::new(pool.clone(), observability_query_budget);
|
||||
postgres::PgRequestAnalyticsRepository::new(pool.clone(), observability_query_budget)
|
||||
.with_projection_work_mem_mb(config.pool.analytics_projection_work_mem_mb);
|
||||
Ok(StoreBundle {
|
||||
pool,
|
||||
analytics,
|
||||
|
||||
@@ -37,6 +37,7 @@ pub struct StorePoolConfig {
|
||||
pub max_connections: u32,
|
||||
pub acquire_timeout_seconds: u64,
|
||||
pub observability_timeout_seconds: u64,
|
||||
pub analytics_projection_work_mem_mb: Option<u32>,
|
||||
}
|
||||
|
||||
impl Default for StorePoolConfig {
|
||||
@@ -45,12 +46,15 @@ impl Default for StorePoolConfig {
|
||||
max_connections: 20,
|
||||
acquire_timeout_seconds: 5,
|
||||
observability_timeout_seconds: 120,
|
||||
analytics_projection_work_mem_mb: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StorePoolConfig {
|
||||
pub(crate) fn validate(&self) -> StoreResult<()> {
|
||||
// PostgreSQL 以有符号 32 位的 kB 保存 work_mem,配置按整 MB 填写
|
||||
const MAX_WORK_MEM_MB: u32 = i32::MAX as u32 / 1024;
|
||||
if self.max_connections < 2 || self.acquire_timeout_seconds == 0 {
|
||||
return Err(StoreError::InvalidData {
|
||||
source: None,
|
||||
@@ -69,6 +73,18 @@ impl StorePoolConfig {
|
||||
message: "pool.observability_timeout_seconds must be positive and fit PostgreSQL statement_timeout".to_owned(),
|
||||
});
|
||||
}
|
||||
if self
|
||||
.analytics_projection_work_mem_mb
|
||||
.is_some_and(|mb| !(1..=MAX_WORK_MEM_MB).contains(&mb))
|
||||
{
|
||||
return Err(StoreError::InvalidData {
|
||||
source: None,
|
||||
entity: "store config",
|
||||
message: format!(
|
||||
"pool.analytics_projection_work_mem_mb must be between 1 and {MAX_WORK_MEM_MB}"
|
||||
),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -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 super::*;
|
||||
|
||||
/// 输入 matching 提供 output_bucket、name 和完整事实列及 is_completed_usage
|
||||
/// 输入 matching 提供分组键、指标输入列及 is_completed_usage
|
||||
/// 精确分位数在整个结果组内计算,不把局部分位数合并成新的分位数
|
||||
pub(super) fn push_fact_aggregation(
|
||||
query: &mut QueryBuilder<Postgres>,
|
||||
|
||||
@@ -379,8 +379,17 @@ async fn rebuild_groups(
|
||||
rebuilt.insert((*key).clone(), MetricState::default());
|
||||
}
|
||||
// 等值桶键避免范围连接反复比较其他时间桶,范围和游标仍限定已发布前缀
|
||||
// 计数、费用和直方图复用匹配结果,只物化这些聚合需要的列
|
||||
let mut query = QueryBuilder::<Postgres>::new(
|
||||
"with matching as materialized (select mr.*,k.bucket_start as output_bucket,k.name,coalesce((",
|
||||
"with matching as materialized (select k.bucket_start as output_bucket,k.name,
|
||||
mr.started_at,mr.outcome,mr.client_status_code,mr.upstream_status_code,mr.attempt_count,
|
||||
mr.image_generation_succeeded,mr.error_kind,mr.ops_failure_count,mr.ops_rate_limited_count,
|
||||
mr.ops_auth_failure_count,mr.ops_provider_5xx_count,mr.input_tokens,mr.output_tokens,
|
||||
mr.cached_input_tokens,mr.cache_write_input_tokens,mr.reasoning_output_tokens,
|
||||
mr.image_input_tokens,mr.image_output_tokens,mr.total_tokens,
|
||||
mr.cost_source,mr.cost_currency,mr.cost_amount,mr.standard_cost_amount,mr.no_cache_cost_amount,
|
||||
mr.first_token_ms,mr.latency_ms,mr.admission_decision_ms,mr.account_selection_wait_ms,
|
||||
mr.upstream_response_ms,mr.capacity_used_slots,mr.capacity_total_slots,coalesce((",
|
||||
);
|
||||
query.push(completed_usage_fact_predicate("mr"))
|
||||
.push("),false) as is_completed_usage from jsonb_to_recordset(")
|
||||
|
||||
@@ -32,6 +32,7 @@ pub struct AnalyticsStatus {
|
||||
pub struct PgRequestAnalyticsRepository {
|
||||
pool: PgPool,
|
||||
query_budget: ObservabilityQueryBudget,
|
||||
projection_work_mem_mb: Option<u32>,
|
||||
billing_projector: Option<Arc<dyn UsageBillingProjector>>,
|
||||
}
|
||||
|
||||
@@ -41,10 +42,17 @@ impl PgRequestAnalyticsRepository {
|
||||
Self {
|
||||
pool,
|
||||
query_budget,
|
||||
projection_work_mem_mb: None,
|
||||
billing_projector: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_projection_work_mem_mb(mut self, work_mem_mb: Option<u32>) -> Self {
|
||||
self.projection_work_mem_mb = work_mem_mb;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_billing_projector(mut self, projector: Arc<dyn UsageBillingProjector>) -> Self {
|
||||
self.billing_projector = Some(projector);
|
||||
@@ -82,6 +90,10 @@ impl PgRequestAnalyticsRepository {
|
||||
transaction.commit().await.map_err(|error| postgres_unavailable("save analytics reset progress", error))?;
|
||||
return Ok(AnalyticsMaintenance { reset_progress: true, ..Default::default() });
|
||||
}
|
||||
if let Some(mb) = self.projection_work_mem_mb {
|
||||
sqlx::query("select set_config('work_mem',$1,true)").bind(format!("{mb}MB"))
|
||||
.execute(&mut *transaction).await.map_err(|error| postgres_unavailable("set analytics projection work_mem", error))?;
|
||||
}
|
||||
let mut result = AnalyticsMaintenance::default();
|
||||
// 历史删除可能一次排入大量旧任务,给仍存在的请求预留半批名额,避免实时统计饥饿
|
||||
// 其余名额仍按最早入队消费,两类任务都能前进;同一快照内选版本,沿用精确版本 ACK
|
||||
|
||||
@@ -144,8 +144,7 @@ impl PgProviderAccountRepository {
|
||||
query.push(" and not exists (select 1 from provider_account_model_catalogs c
|
||||
where c.account_id=provider_accounts.id and c.credential_revision=provider_accounts.credential_revision
|
||||
and c.plan_type is not distinct from provider_accounts.plan_type and c.document_hash=any(")
|
||||
.push_bind(hashes).push("::bytea[]) and c.observed_at > ").push_bind(DateTime::<Utc>::from(omit.observed_after))
|
||||
.push(" and c.observed_at <= ").push_bind(DateTime::<Utc>::from(omit.as_of))
|
||||
.push_bind(hashes).push("::bytea[]) and c.observed_at <= ").push_bind(DateTime::<Utc>::from(omit.as_of))
|
||||
.push(" and not (c.account_id=any(").push_bind(exceptions).push("::text[])))");
|
||||
}
|
||||
query.push(" order by provider_kind, name, id");
|
||||
|
||||
@@ -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,
|
||||
@@ -296,7 +320,7 @@ impl ProviderRuntimePolicyPort for PgRuntimeSettingsRepository {
|
||||
.map_err(|source| crate::provider_unavailable("load warmup policy", source))?;
|
||||
ProviderWarmupPolicy::try_new(
|
||||
settings.values.account_warmup_enabled,
|
||||
settings.values.account_warmup_cron,
|
||||
&settings.values.account_warmup_cron,
|
||||
settings.values.account_warmup_model,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ const PASSWORD: &str = "111111111111111111111111111111111111111111111111";
|
||||
#[test]
|
||||
fn store_config_should_derive_backup_staging_from_runtime_data_dir() {
|
||||
let root = tempfile::tempdir().expect("runtime data root");
|
||||
let mut config = valid_config();
|
||||
let mut config = config_with_pool(serde_json::json!({}));
|
||||
|
||||
config
|
||||
.resolve_and_validate(root.path())
|
||||
@@ -28,6 +28,7 @@ fn store_pool_config_defaults_reserve_capacity_for_observability() {
|
||||
assert_eq!(pool.max_connections, 20);
|
||||
assert_eq!(pool.acquire_timeout_seconds, 5);
|
||||
assert_eq!(pool.observability_timeout_seconds, 120);
|
||||
assert_eq!(pool.analytics_projection_work_mem_mb, None);
|
||||
assert_eq!(pool.observability_max_connections(), 16);
|
||||
assert_eq!(
|
||||
StorePoolConfig {
|
||||
@@ -68,7 +69,7 @@ fn store_config_rejects_a_pool_that_cannot_reserve_both_traffic_classes() {
|
||||
));
|
||||
}
|
||||
|
||||
fn valid_config() -> StoreConfig {
|
||||
fn config_with_pool(pool: serde_json::Value) -> StoreConfig {
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"database": {
|
||||
"url": "postgres://codex_proxy@127.0.0.1:5432/codex_proxy",
|
||||
@@ -78,10 +79,29 @@ fn valid_config() -> StoreConfig {
|
||||
"url": "redis://127.0.0.1:6379/",
|
||||
"password": PASSWORD,
|
||||
},
|
||||
"pool": pool,
|
||||
}))
|
||||
.expect("test Store configuration")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analytics_projection_work_mem_accepts_whole_mb_within_postgres_limits() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
for mb in [1_u32, 8, 2_097_151] {
|
||||
config_with_pool(serde_json::json!({"analytics_projection_work_mem_mb": mb}))
|
||||
.resolve_and_validate(root.path())
|
||||
.unwrap();
|
||||
}
|
||||
for mb in [0_u32, 2_097_152] {
|
||||
let error = config_with_pool(serde_json::json!({"analytics_projection_work_mem_mb": mb}))
|
||||
.resolve_and_validate(root.path())
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(error, StoreError::InvalidData { message, .. } if message.contains("analytics_projection_work_mem_mb"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_config_accepts_custom_observability_timeout_and_rejects_invalid_values() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -316,6 +316,80 @@ async fn incremental_deletions_repair_extrema_last_usage_and_zero_currency_prese
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn projection_work_mem_is_local_to_the_worker_transaction() {
|
||||
let Some(database) = TestDatabase::create("work_mem").await else {
|
||||
return;
|
||||
};
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(database.pool.connect_options().as_ref().clone())
|
||||
.await
|
||||
.unwrap();
|
||||
// 固定同一会话,直接记录事实写入和队列确认时的值,并在复用连接时检查恢复
|
||||
sqlx::raw_sql(
|
||||
"set work_mem='2MB';
|
||||
create table work_mem_audit(stage text, value text);
|
||||
create function record_work_mem() returns trigger language plpgsql as $$
|
||||
begin
|
||||
insert into work_mem_audit values (tg_argv[0], current_setting('work_mem'));
|
||||
return null;
|
||||
end $$;
|
||||
create trigger audit_fact after insert on request_analytics_facts
|
||||
for each row execute function record_work_mem('projection');
|
||||
create trigger audit_ack after delete on request_analytics_pending_requests
|
||||
for each row execute function record_work_mem('ack')",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let worker = PgRequestAnalyticsRepository::new(pool.clone(), observability_query_budget());
|
||||
let session = || {
|
||||
sqlx::query_as::<_, (i32, String)>("select pg_backend_pid(), current_setting('work_mem')")
|
||||
};
|
||||
let original = session().fetch_one(&pool).await.unwrap();
|
||||
for (worker, expected) in [
|
||||
(worker.clone(), "2MB"),
|
||||
(worker.with_projection_work_mem_mb(Some(8)), "8MB"),
|
||||
] {
|
||||
seed(&pool, expected, hour(), &[]).await;
|
||||
assert_eq!(worker.maintain(10).await.unwrap().projected_requests, 1);
|
||||
let audit: Vec<(String, String)> =
|
||||
sqlx::query_as("select stage, value from work_mem_audit order by stage")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
audit,
|
||||
[
|
||||
("ack".into(), "2MB".into()),
|
||||
("projection".into(), expected.into())
|
||||
]
|
||||
);
|
||||
assert_eq!(session().fetch_one(&pool).await.unwrap(), original);
|
||||
sqlx::query("truncate work_mem_audit")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
// 事实写入失败时投影事务回滚,原会话仍可供下一轮使用
|
||||
sqlx::query("alter table work_mem_audit add check (value <> '8MB')")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
seed(&pool, "rollback", hour(), &[]).await;
|
||||
let worker = PgRequestAnalyticsRepository::new(pool.clone(), observability_query_budget())
|
||||
.with_projection_work_mem_mb(Some(8));
|
||||
let error = worker.maintain(10).await.unwrap_err();
|
||||
assert!(
|
||||
error.to_string().contains("project analytics facts"),
|
||||
"{error}"
|
||||
);
|
||||
assert_eq!(session().fetch_one(&pool).await.unwrap(), original);
|
||||
pool.close().await;
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn incremental_summary_write_failure_rolls_back_facts_and_preserves_queue() {
|
||||
let Some(database) = TestDatabase::create("analytics_incremental_rollback").await else {
|
||||
|
||||
@@ -170,7 +170,7 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
|
||||
.iter()
|
||||
.all(|a| !matches!(a.account_id.as_str(), "acct_1" | "acct_2" | "acct_3"))
|
||||
);
|
||||
// 数据库只能排除有当前版本、未过期的账号证据;显式例外仍进入选号
|
||||
// 刷新到期不改变目录排除结果;凭据变化与显式例外仍进入选号
|
||||
let hash: Vec<u8> = sqlx::query_scalar("select sha256($1::bytea)")
|
||||
.bind(&document)
|
||||
.fetch_one(&database.pool)
|
||||
@@ -181,7 +181,6 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
|
||||
let filter = gateway_core::account::AccountCatalogOmissions {
|
||||
documents: vec![hash.try_into().unwrap()],
|
||||
exceptions: vec![ProviderAccountId::new("acct_5").unwrap()],
|
||||
observed_after: now - std::time::Duration::from_secs(1800),
|
||||
as_of: now,
|
||||
};
|
||||
let ids = (1..=10000)
|
||||
@@ -195,9 +194,6 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
|
||||
.iter()
|
||||
.map(|a| a.id().as_str())
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
assert_eq!(
|
||||
ids,
|
||||
std::collections::BTreeSet::from(["acct_1", "acct_4", "acct_5"])
|
||||
);
|
||||
assert_eq!(ids, std::collections::BTreeSet::from(["acct_1", "acct_5"]));
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -183,7 +183,7 @@ async fn warmup_settings_round_trip_with_database_constraint() {
|
||||
let repository = PgRuntimeSettingsRepository::new(database.pool.clone());
|
||||
let mut update = settings_with_margin(3_600);
|
||||
update.values.account_warmup_enabled = true;
|
||||
update.values.account_warmup_cron = "0 8,13 * * MON-FRI".to_owned();
|
||||
update.values.account_warmup_cron = "0 8,13 * * MON-FRI;59 10 * * *".to_owned();
|
||||
update.values.account_warmup_model = Some("test-model".to_owned());
|
||||
repository
|
||||
.update_runtime_settings(update)
|
||||
@@ -195,7 +195,10 @@ async fn warmup_settings_round_trip_with_database_constraint() {
|
||||
.await
|
||||
.expect("load warmup settings");
|
||||
assert!(settings.values.account_warmup_enabled);
|
||||
assert_eq!(settings.values.account_warmup_cron, "0 8,13 * * MON-FRI");
|
||||
assert_eq!(
|
||||
settings.values.account_warmup_cron,
|
||||
"0 8,13 * * MON-FRI;59 10 * * *"
|
||||
);
|
||||
assert_eq!(
|
||||
settings.values.account_warmup_model.as_deref(),
|
||||
Some("test-model")
|
||||
@@ -1012,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})
|
||||
@@ -1053,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,
|
||||
@@ -32,8 +33,7 @@ use crate::transport::{CodexBackendClient, CodexCatalogModel, CodexRequestContex
|
||||
|
||||
const MAX_RESPONSE_ETAG_BYTES: usize = 256;
|
||||
const ACCOUNT_CATALOG_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
|
||||
// 调度证据覆盖默认 15 分钟刷新周期及抖动,长期未刷新时恢复上游验证
|
||||
const SCHEDULING_CATALOG_MAX_AGE: Duration = Duration::from_secs(30 * 60);
|
||||
const CATALOG_REFRESH_AFTER: Duration = Duration::from_secs(30 * 60);
|
||||
const MAX_ACCOUNT_CATALOG_MODELS: usize = 2_048;
|
||||
const MODEL_CATALOG_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
|
||||
@@ -141,7 +141,6 @@ impl CatalogContent {
|
||||
struct AccountEntry {
|
||||
observed_at: SystemTime,
|
||||
content: Arc<CatalogContent>,
|
||||
invalidated: Arc<std::sync::atomic::AtomicBool>,
|
||||
etag: Option<String>,
|
||||
}
|
||||
|
||||
@@ -197,13 +196,7 @@ impl CodexCredentialCatalogSnapshot {
|
||||
pub(super) fn excludes(&self, account: &ProviderAccount, model: &str, now: SystemTime) -> bool {
|
||||
self.entries
|
||||
.get(&CodexCatalogScope::for_account(account))
|
||||
.is_some_and(|e| {
|
||||
!e.invalidated.load(std::sync::atomic::Ordering::Relaxed)
|
||||
&& now
|
||||
.duration_since(e.observed_at)
|
||||
.is_ok_and(|age| age < SCHEDULING_CATALOG_MAX_AGE)
|
||||
&& !e.content.ids.iter().any(|id| id == model)
|
||||
})
|
||||
.is_some_and(|e| e.observed_at <= now && !e.content.ids.iter().any(|id| id == model))
|
||||
}
|
||||
|
||||
pub fn account_models(&self, account: &ProviderAccount) -> Option<&[String]> {
|
||||
@@ -251,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)]
|
||||
@@ -281,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<()>>,
|
||||
@@ -294,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,
|
||||
@@ -301,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(())),
|
||||
@@ -308,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,
|
||||
@@ -466,14 +479,7 @@ impl CodexCredentialCatalogService {
|
||||
if ids.len() < MIN_CATALOG_PUSHDOWN {
|
||||
return None;
|
||||
}
|
||||
let cache = self.cache.read().ok()?;
|
||||
let snapshot = cache.snapshot.as_ref()?.clone();
|
||||
let pending = cache
|
||||
.pending
|
||||
.keys()
|
||||
.map(|s| s.account_id.clone())
|
||||
.collect::<BTreeSet<_>>();
|
||||
drop(cache);
|
||||
let snapshot = self.cached().ok()??;
|
||||
let documents = snapshot
|
||||
.sources
|
||||
.iter()
|
||||
@@ -483,16 +489,10 @@ impl CodexCredentialCatalogService {
|
||||
if documents.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let as_of = SystemTime::now();
|
||||
Some(gateway_core::account::AccountCatalogOmissions {
|
||||
documents,
|
||||
exceptions: ids
|
||||
.iter()
|
||||
.filter(|id| pending.contains(*id) || exempt(id))
|
||||
.cloned()
|
||||
.collect(),
|
||||
observed_after: as_of.checked_sub(SCHEDULING_CATALOG_MAX_AGE)?,
|
||||
as_of,
|
||||
exceptions: ids.iter().filter(|id| exempt(id)).cloned().collect(),
|
||||
as_of: SystemTime::now(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -521,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)
|
||||
@@ -582,7 +578,6 @@ impl CodexCredentialCatalogService {
|
||||
observed_at,
|
||||
content,
|
||||
etag: observed.etag,
|
||||
invalidated: Default::default(),
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -595,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);
|
||||
|
||||
@@ -41,7 +41,6 @@ impl CodexCredentialCatalogService {
|
||||
observed_at: a.observed_at,
|
||||
content: content.clone(),
|
||||
etag: a.etag,
|
||||
invalidated: Default::default(),
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -131,11 +130,6 @@ impl CodexCredentialCatalogService {
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
if let Some(entry) = state.snapshot.as_ref().and_then(|s| s.entries.get(&scope)) {
|
||||
entry
|
||||
.invalidated
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
state.pending.insert(scope, etag.to_owned());
|
||||
drop(state);
|
||||
self.refresh_notification.notify_one();
|
||||
@@ -210,15 +204,12 @@ impl CodexCredentialCatalogService {
|
||||
let Some(entry) = snapshot.entries.get(scope) else {
|
||||
return true;
|
||||
};
|
||||
if entry.etag.as_deref() == Some(tag)
|
||||
&& !entry.invalidated.load(std::sync::atomic::Ordering::Relaxed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
entry
|
||||
.invalidated
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
true
|
||||
// 发布其他账号时保留通知,只有该账号更新的观测才能确认 ETag
|
||||
entry.etag.as_deref() != Some(tag)
|
||||
|| old
|
||||
.as_ref()
|
||||
.and_then(|s| s.entries.get(scope))
|
||||
.is_some_and(|previous| previous.observed_at >= entry.observed_at)
|
||||
});
|
||||
}
|
||||
cache.snapshot = next.clone();
|
||||
@@ -289,7 +280,7 @@ impl CodexCredentialCatalogService {
|
||||
pending.contains_key(scope)
|
||||
|| !entries.get(scope).is_some_and(|e| {
|
||||
now.duration_since(e.observed_at)
|
||||
.is_ok_and(|age| age < SCHEDULING_CATALOG_MAX_AGE)
|
||||
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -298,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]
|
||||
|
||||
+46
-34
@@ -222,15 +222,23 @@ async fn account_allowlists_override_catalog_omissions_but_weight_affinity_and_p
|
||||
store.set_scheduling("acct_scope_new", None, AccountWeight::new(100).unwrap());
|
||||
let affinity = Arc::new(MemorySessionAffinity::default());
|
||||
let leases = Arc::new(TestLeaseCoordinator::default());
|
||||
let (provider, _) = provider_and_quota_with_catalog(
|
||||
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
|
||||
&store,
|
||||
affinity.clone(),
|
||||
native.uri(),
|
||||
leases.clone(),
|
||||
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
|
||||
)
|
||||
.await;
|
||||
Arc::new(MemoryCooldownPort::new()),
|
||||
crate::support::runtime_policy(),
|
||||
);
|
||||
catalog.refresh_catalogs().await.unwrap();
|
||||
provider.query_model_capabilities().await.unwrap();
|
||||
// 次账号目录待刷新时,仍须按各自的目录和白名单选号
|
||||
assert!(
|
||||
catalog
|
||||
.observe_response_etag(&store.account("acct_scope_new").unwrap(), "new")
|
||||
.unwrap()
|
||||
);
|
||||
for (model, expected) in [
|
||||
("gpt-6-luna", "acct_scope_new"),
|
||||
("gpt-6-astra", "acct_provider_contract"),
|
||||
@@ -364,7 +372,7 @@ async fn account_allowlists_override_catalog_omissions_but_weight_affinity_and_p
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn only_a_current_account_catalog_can_exclude_a_model() {
|
||||
async fn selection_uses_the_last_catalog_for_the_same_account_identity() {
|
||||
use gateway_core::account::{CredentialCasOutcome, CredentialCasUpdate, ProviderAccountUpdate};
|
||||
use provider_openai::credential::ResponsesTransport;
|
||||
|
||||
@@ -423,7 +431,16 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
|
||||
.expect(if scenario == "unknown_plan" { 0 } else { 1 })
|
||||
.mount(upstream)
|
||||
.await;
|
||||
let provider = if scenario == "expired" {
|
||||
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
|
||||
&store,
|
||||
Arc::new(MemorySessionAffinity::default()),
|
||||
native.uri(),
|
||||
Arc::new(TestLeaseCoordinator::default()),
|
||||
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
|
||||
Arc::new(MemoryCooldownPort::new()),
|
||||
crate::support::runtime_policy(),
|
||||
);
|
||||
if scenario == "expired" {
|
||||
use gateway_core::account::AccountCatalogObservation;
|
||||
let a = store.account("acct_scope_new").unwrap();
|
||||
store
|
||||
@@ -439,26 +456,8 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
|
||||
&store,
|
||||
Arc::new(MemorySessionAffinity::default()),
|
||||
native.uri(),
|
||||
Arc::new(TestLeaseCoordinator::default()),
|
||||
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
|
||||
Arc::new(MemoryCooldownPort::new()),
|
||||
crate::support::runtime_policy(),
|
||||
);
|
||||
catalog.restore().await.unwrap();
|
||||
// B 的失败不能清空上次成功结果,也不能继续以旧的缺失条目拒绝请求
|
||||
catalog.refresh_catalogs().await.unwrap();
|
||||
assert_eq!(
|
||||
catalog.cached_account_models(&a).unwrap().unwrap(),
|
||||
["gpt-6-luna"]
|
||||
);
|
||||
provider
|
||||
} else {
|
||||
provider_with_catalog_and_base_url(&store, native.uri()).await
|
||||
};
|
||||
}
|
||||
catalog.refresh_catalogs().await.unwrap();
|
||||
provider.query_model_capabilities().await.unwrap();
|
||||
if scenario == "rotated" {
|
||||
let account = store.account("acct_scope_new").unwrap();
|
||||
@@ -483,21 +482,34 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
|
||||
CredentialCasOutcome::Updated(_)
|
||||
));
|
||||
}
|
||||
let result = provider
|
||||
.execute(
|
||||
let execute = || {
|
||||
provider.clone().execute(
|
||||
planned_request_for_model("openai", operation("gpt-6-astra"), "gpt-6-astra"),
|
||||
context("req_unknown_catalog", CancellationToken::new()),
|
||||
)
|
||||
.await;
|
||||
if scenario == "unknown_plan" {
|
||||
// 未知套餐也有该账号亲自返回的目录,不能借套餐缺失绕过资格
|
||||
};
|
||||
if matches!(scenario, "unknown_plan" | "expired") {
|
||||
assert_eq!(
|
||||
result.err().unwrap().kind(),
|
||||
ProviderErrorKind::NoEligibleAccount
|
||||
execute().await.err().unwrap().kind(),
|
||||
ProviderErrorKind::NoEligibleAccount,
|
||||
"{scenario}"
|
||||
);
|
||||
continue;
|
||||
if scenario == "unknown_plan" {
|
||||
continue;
|
||||
}
|
||||
// 刷新失败保留排除结果,直到新目录确认该账号已支持模型
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/models"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"data": [{"id": "gpt-6-astra"}]
|
||||
})))
|
||||
.with_priority(1)
|
||||
.expect(1)
|
||||
.mount(&api)
|
||||
.await;
|
||||
catalog.refresh_catalogs().await.unwrap();
|
||||
}
|
||||
let mut stream = result.unwrap();
|
||||
let mut stream = execute().await.unwrap();
|
||||
assert_eq!(
|
||||
stream.metadata().provider_account_id().as_str(),
|
||||
"acct_scope_new",
|
||||
|
||||
@@ -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(¤t, "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)
|
||||
|
||||
@@ -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
|
||||
);
|
||||
@@ -122,6 +122,9 @@ store:
|
||||
应用默认上限为 20,允许的最小值为 2;降低上限可以限制实际数据库连接数,但也会增加请求、后台写入与管理查询之间的等待。
|
||||
如果出现连接获取超时,应结合数据库负载调大池容量,不要仅延长等待时间
|
||||
|
||||
统计 Worker 重算分组时,若临时文件写入较多且数据库有内存余量,可在 `store.pool` 下设置 `analytics_projection_work_mem_mb: 8`(单位 MB)。
|
||||
默认省略,沿用 PostgreSQL 的 `work_mem`;显式设置仅影响增量投影和历史回填所在的事务,队列确认、整桶重建和在线查询继续沿用原值
|
||||
|
||||
将 `deploy/compose.yaml` 中 `services.postgres.command` 替换为以下列表,保留 UTC 参数:
|
||||
|
||||
```yaml
|
||||
@@ -382,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 局域网地址不满足条件。入口是否显示还取决于浏览器、访问交互及是否已安装。应用需要联网访问网关,不提供离线使用
|
||||
|
||||
### 启动后的运行设置
|
||||
|
||||
在管理端修改以下设置,保存后对新请求生效,重启不会覆盖已保存的选择:
|
||||
|
||||
@@ -52,12 +52,13 @@ store:
|
||||
url: 'redis://127.0.0.1:6379/'
|
||||
# 与 PostgreSQL 共用密码格式要求,并满足 Redis 服务端密码策略
|
||||
password: &redis_password ''
|
||||
# 可选:PostgreSQL 连接池预算;下列值与当前推荐默认一致。
|
||||
# 可选:PostgreSQL 连接池与统计投影配置
|
||||
# 小内存低并发部署的池上限与数据库调优见 deploy/README.md#小内存优化
|
||||
# pool:
|
||||
# max_connections: 20
|
||||
# acquire_timeout_seconds: 5
|
||||
# observability_timeout_seconds: 120 # 观测操作保护时限,不是性能目标
|
||||
# analytics_projection_work_mem_mb: 8 # 仅统计投影事务;省略时沿用数据库设置
|
||||
|
||||
admin:
|
||||
# 不活动期限与从登录起计算的最长有效期,单位为分钟
|
||||
|
||||
+19
-14
@@ -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`。
|
||||
@@ -1373,11 +1378,9 @@ Key 预算及其访问状态随互联默认公开,不要求开启 `shareQuota`
|
||||
例如 Plus 设置 `allowlist` 并选中 luna 的实际 ID,Pro 设置 `denylist` 并选中同一 ID,即可严格分流;
|
||||
Pro 使用 `all` 时也能参与 luna 调度。套餐名称不自动生成或修改规则
|
||||
|
||||
原生 Responses 默认排除有效目录未列出目标模型的账号。OpenAI OAuth 或 API Key 账号的白名单精确包含映射后的上游 ID 时,
|
||||
允许通过该账号请求目录外模型,由上游确认是否可用。此设置只对配置它的账号生效,已有白名单同样适用。
|
||||
账号禁用、Key 分组、认证类型、黑名单和模型冷却仍参与选号。xAI 账号使用有效套餐目录与账号模型政策的交集。
|
||||
OpenAI 只使用该账号当前凭据和套餐对应、最近 30 分钟内同步的目录,不借用同套餐其他账号的模型。
|
||||
没有有效账号目录时交由上游验证,仍执行 `modelAccess`。xAI 使用已知套餐的有效目录缓存
|
||||
原生 OpenAI Responses 按账号当前凭据和套餐最近一次成功同步的目录选号。目录未列出的模型在刷新期间或刷新失败后仍被排除。
|
||||
账号白名单精确包含映射后的上游 ID 时,允许该账号尝试目录外模型;当前凭据和套餐没有目录时,也可在账号模型政策允许的范围内交由上游验证。
|
||||
账号禁用、Key 分组、认证类型、黑名单和模型冷却仍参与选号。xAI 按账号模型政策与有效的套餐目录筛选
|
||||
|
||||
原生 OpenAI Responses 上游返回 `error.code=model_not_found`,且状态为 400/403/404 或流内错误未提供状态时,
|
||||
该账号与模型组合冷却 30 分钟;其他模型仍可使用该账号。仅消息提及模型不支持不触发冷却;
|
||||
@@ -1500,7 +1503,7 @@ OAuth 等待回调期间不持有保护;提交仍拒绝已删除或连接配
|
||||
|
||||
### 账号连接测试 SSE
|
||||
|
||||
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,不参与普通账号轮换。成功流沿用
|
||||
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,采用当前全局客户端身份,不参与普通账号轮换。成功流沿用
|
||||
`test_start`、`request`、`content`、`test_complete` 事件;失败事件为:
|
||||
|
||||
```json
|
||||
@@ -2167,7 +2170,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
|
||||
### 历史保留
|
||||
|
||||
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,支持 1~31 的整数天数。
|
||||
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,须为正整数,不设业务上限。
|
||||
`opsEventRetentionDays` 与 `auditRetentionDays` 分别控制独立运维事件和管理审计,默认 30、90,均须为正整数
|
||||
|
||||
清理任务每轮读取已保存设置,按完成时间分批删除过期请求及关联事件,不删除运行中的请求;有界单轮结束后等待一分钟再检查。
|
||||
@@ -2178,8 +2181,8 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
|
||||
### 账号预热
|
||||
|
||||
定时账号预热默认关闭。`accountWarmupCron` 使用部署时区的五段 Cron 表达式,
|
||||
顺序为分、时、日、月、星期,总长度不超过 255 字节,默认空字符串,关闭时允许留空。
|
||||
定时账号预热默认关闭。`accountWarmupCron` 是字符串,使用半角分号(`;`)分隔多条五段 Cron 表达式,总长度(含分隔符)不超过 255 字节,各条不可留空。默认空字符串,关闭时允许留空。
|
||||
每条规则按部署时区依次匹配分、时、日、月、星期。任一规则命中即执行,例如 `"58 5 * * *;59 10 * * *"` 只在每天 05:58 和 10:59 触发。
|
||||
支持列表、范围和步长,例如 `0 8,13,19 * * *` 每天执行三次,`0 8 * * MON-FRI` 在工作日 08:00 执行。
|
||||
星期可用 `SUN` 至 `SAT` 或数字 1 至 7(日到六),同时指定日与星期时两者均须满足。
|
||||
同一分钟只领取一次;`accountWarmupModel` 默认 `null`,开启前必须填写有效表达式并显式选择模型。
|
||||
@@ -2188,6 +2191,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
0% 或缺少活跃证据时允许一次轻量预热,不通过倒计时差值判断活跃,也不额外回查额度。
|
||||
因此已激活但显示 0% 的账号也可能收到预热请求。
|
||||
不存在的本地时刻跳过,重复时刻只执行较早一次;执行进度跨重启保留,时钟回拨不补跑已领取时刻之前的时段。
|
||||
预热每轮采用当前 OpenAI 全局客户端身份,不使用单个 Key 的覆盖。
|
||||
只有收到响应成功终态才记为预热成功;预热不计入客户端业务用量
|
||||
|
||||
### 请求位置
|
||||
@@ -2427,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" }`。
|
||||
|
||||
+10
-6
@@ -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`
|
||||
|
||||
@@ -658,9 +663,8 @@ OpenAI Provider 的账号资格模块统一解释请求类型、认证与套餐
|
||||
资格检查复用账号持久事实,在获取租约前与冻结的模型黑白名单共同过滤候选;亲和与重试沿用同一检查
|
||||
|
||||
原生 Responses 在评分、亲和和租约前检查冻结的账号模型政策。OpenAI 账号白名单精确命中目标 ID 时允许上游验证,
|
||||
其余 OpenAI 账号及 xAI 账号按有效目录排除未列出的模型。OpenAI 目录按来源账号、凭据版本和套餐隔离;同套餐账号可以有不同的模型。
|
||||
OpenAI 只使用未满 30 分钟且尚未收到变化通知的账号目录排除候选。大池可在读取账号时按目录摘要筛选,数据库逐账号核对版本、套餐和观测时间;白名单、待刷新通知及受管适配器不参与该筛选。xAI 按套餐去重读取 TTL 目录缓存,选号不请求上游目录。
|
||||
没有有效目录时保留上游验证,不从其他账号借用 OpenAI 模型资格,也不从其他套餐借用 xAI 模型资格
|
||||
其余原生 OpenAI 请求按来源账号、凭据版本和套餐匹配最近一次成功的目录,排除未列出的模型。大池可在读取账号时按目录摘要筛选,白名单和受管适配器由选号时单独判断。
|
||||
xAI 按套餐读取有效期内的目录缓存。两者在没有对应目录时保留上游验证,选号不请求上游目录
|
||||
|
||||
原生 OpenAI Responses 按[官方 Codex 0.166.0](https://github.com/openai/codex/blob/6ff1e1ca3b30fc8b914720681fcf5fe62e95f856/codex-rs/codex-api/src/error.rs#L73)的结构化 `model_not_found` 错误码与状态合同识别模型拒绝,
|
||||
不从消息或任意正文推断模型权限;通过既有模型级冷却端口记录账号、凭据版本与上游模型,30 分钟内不再选用该组合。
|
||||
@@ -968,8 +972,8 @@ PostgreSQL schema 由迁移目录按编号管理;冻结、升级与校验规
|
||||
### 账号模型目录
|
||||
|
||||
OpenAI 在 PostgreSQL 保存账号最近一次成功的模型目录、凭据版本、观测时间及 ETag。启动时批量恢复,按不同正文解析一次,保留每个账号的独立归属。没有历史目录也能发布路由快照;能力读取与推理不等待上游查询。
|
||||
后台只刷新目录缺失、已过刷新窗口或收到新 ETag 的账号;凭据轮换后按新版本重新发现。失败保留最后成功的结果,过期资料只用于展示,不能据此排除候选账号。
|
||||
新鲜度窗口、刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护
|
||||
后台刷新缺少目录、距上次成功观测已满 30 分钟或收到新 ETag 的账号。刷新期间及失败后继续使用上次成功的目录;凭据或套餐变化后不沿用旧目录。
|
||||
刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护
|
||||
|
||||
OpenAI 从账号目录生成原生列表;先按冻结政策选择来源,再读取本地账号状态核对凭据版本。同一份正文可以复用,但不会把一个账号的模型授予同套餐的其他账号。客户端版本和 Key 身份不触发额外发现
|
||||
|
||||
|
||||
+4
-4
@@ -15,7 +15,7 @@
|
||||
设置保存在浏览器本地,不会同步到服务器或其他设备。只想换配色时,选择品牌色预置即可
|
||||
|
||||
下文说明实现和扩展方式。颜色使用 Ant Design 十阶色板与项目的明暗角色规则,
|
||||
中性表面从背景和文字 Seed 派生;Vue 组件通过 CSS Variables 与 Tailwind CSS 4 使用这些值。
|
||||
中性表面从当前底色和文字基色派生;Vue 组件通过 CSS Variables 与 Tailwind CSS 4 使用这些值。
|
||||
项目不依赖 Ant Design 组件库或 CSS-in-JS
|
||||
|
||||
> [!IMPORTANT]
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
- **Seed 与 Map 可追踪**:主色和功能色均按 Ant Design 从 Seed 派生 P1-P10;浅色 P6 等于 Seed,深色 P6
|
||||
经过暗色色板适配。全局 `colorPrimary` 保留原始 Seed;功能色 Base 保留 P6。文字和按钮组件可在派生层做对比度校正,Seed 不随之改写
|
||||
- **主色与表面分离**:品牌色负责交互和强调,页面、容器、浮层与文字由独立的背景和文本 Seed 派生
|
||||
- **主色与表面分离**:品牌色负责交互和强调,页面、容器与浮层使用预设底色或自定义背景,文字由文本 Seed 派生
|
||||
- **语义独立**:成功、警告、错误、信息和链接色各自派生,不随品牌色改变
|
||||
- **无边设计**:默认依靠表面色差、间距和轻阴影表达层级;边框只用于焦点、错误和必要分隔
|
||||
- **运行时可定制**:用户输入在浏览器中实时派生,因此使用 CSS Variables,不使用构建时 SCSS 变量
|
||||
@@ -205,7 +205,7 @@ Preset 的实心色使用 P6,普通 Container、Strong Container 与边界按
|
||||
- `colorText / Heading / Secondary / Tertiary / Quaternary / Disabled`
|
||||
- `colorBorder / BorderSecondary / Split / Shadow`
|
||||
|
||||
所有品牌色共用当前明暗模式的背景和文字基色。手动设置 `colorBgBase` 或 `colorTextBase` 后,两个模式都使用该字段的自定义值;单项恢复后重新跟随明暗模式。
|
||||
编辑器中的背景和文字基色只随明暗模式或手动设置改变。未指定 `colorBgBase` 时,页面、卡片和浮层从当前预设的明暗底色派生;无预设底色时使用模式默认值。手动设置 `colorBgBase` 后,两个模式均以该值为底色,直至单项恢复
|
||||
默认浅色采用中性锚点,默认深色使用 HSL 色调派生;自定义中性色按外观距离平滑过渡到背景和文字 Seed 的 RGB 混色结果。
|
||||
Input、阴影与其他 Component Token 从 Fill、Surface、Primary 和 Semantic 派生,不在常量文件维护整套颜色表
|
||||
|
||||
@@ -449,7 +449,7 @@ utility / arbitrary variant;Vue Transition、跨浏览器 Range、动态富文
|
||||
单独增加品牌配色:
|
||||
|
||||
1. 在 `ThemeColorPresetId` 增加稳定 ID
|
||||
2. 在 `themeColorPresets` 声明名称、描述与主色 Seed
|
||||
2. 在 `themeColorPresets` 声明名称、描述与主色 Seed;需要独立底色时在 `PRESET_SURFACE_BACKGROUNDS` 中指定浅色和深色值
|
||||
3. 不新增页面特判,确认预置可通过同一 `createTheme()` 派生
|
||||
4. 在浅色、深色和系统模式下检查文本、浮层、Input、表格与图表
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@boxicons/vue": "1.0.1",
|
||||
"@codex-proxy/ui": "github:zyycn/codex-proxy-ui#v0.10.0",
|
||||
"@codex-proxy/ui": "github:zyycn/codex-proxy-ui#v0.11.0",
|
||||
"@fontsource-variable/inter": "^5.3.0",
|
||||
"@fontsource-variable/jetbrains-mono": "^5.3.0",
|
||||
"@lucide/vue": "^1.70.0",
|
||||
|
||||
Generated
+6
-6
@@ -195,8 +195,8 @@ importers:
|
||||
specifier: 1.0.1
|
||||
version: 1.0.1(vue@3.5.45(typescript@6.0.3))
|
||||
'@codex-proxy/ui':
|
||||
specifier: github:zyycn/codex-proxy-ui#v0.10.0
|
||||
version: https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/45b076735d67f973588d93144078c7605cf1ade7(axios@1.21.0(debug@4.4.3))(change-case@5.4.4)(jiti@2.7.0)(postcss@8.5.30)(vite@8.3.6(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.0))(vue@3.5.45(typescript@6.0.3))(yaml@2.9.0)
|
||||
specifier: github:zyycn/codex-proxy-ui#v0.11.0
|
||||
version: https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/68458e62e7fb9fadbacea75ce48522f2ba85b8b2(axios@1.21.0(debug@4.4.3))(change-case@5.4.4)(jiti@2.7.0)(postcss@8.5.30)(vite@8.3.6(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.0))(vue@3.5.45(typescript@6.0.3))(yaml@2.9.0)
|
||||
'@fontsource-variable/inter':
|
||||
specifier: ^5.3.0
|
||||
version: 5.3.0
|
||||
@@ -456,9 +456,9 @@ packages:
|
||||
resolution: {integrity: sha512-dlT1m5e/0yUL0kRNcQn7yGLVThkgbB0Ga/1AmfDDC/8ik6AIiSf2QLQO2zPYvefsHP0aFgxO93cVLCCfDp7kzQ==}
|
||||
engines: {node: '>= 20.12.0'}
|
||||
|
||||
'@codex-proxy/ui@https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/45b076735d67f973588d93144078c7605cf1ade7':
|
||||
resolution: {gitHosted: true, integrity: sha512-TyY1/D8rzd1MwU5HUbKOEzca5q7nPqydWaRpb5iMUrSy6G4dXleZNSZd6VXAum9WXZrZGb9ZzBm9cOMIOg+/xg==, tarball: https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/45b076735d67f973588d93144078c7605cf1ade7}
|
||||
version: 0.10.0
|
||||
'@codex-proxy/ui@https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/68458e62e7fb9fadbacea75ce48522f2ba85b8b2':
|
||||
resolution: {gitHosted: true, integrity: sha512-VSw6WQntuIQmVLY1uMCouUKYbUZyzT4zDmA7CJ/fWT/yBZwvaH+i16SAp++twYYIvKwYMBR6WmNlV7RA6zXKOw==, tarball: https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/68458e62e7fb9fadbacea75ce48522f2ba85b8b2}
|
||||
version: 0.11.0
|
||||
engines: {node: '>=24'}
|
||||
peerDependencies:
|
||||
vite: ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0
|
||||
@@ -3530,7 +3530,7 @@ snapshots:
|
||||
fast-wrap-ansi: 0.2.2
|
||||
sisteransi: 1.0.5
|
||||
|
||||
'@codex-proxy/ui@https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/45b076735d67f973588d93144078c7605cf1ade7(axios@1.21.0(debug@4.4.3))(change-case@5.4.4)(jiti@2.7.0)(postcss@8.5.30)(vite@8.3.6(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.0))(vue@3.5.45(typescript@6.0.3))(yaml@2.9.0)':
|
||||
'@codex-proxy/ui@https://codeload.github.com/zyycn/codex-proxy-ui/tar.gz/68458e62e7fb9fadbacea75ce48522f2ba85b8b2(axios@1.21.0(debug@4.4.3))(change-case@5.4.4)(jiti@2.7.0)(postcss@8.5.30)(vite@8.3.6(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.0))(vue@3.5.45(typescript@6.0.3))(yaml@2.9.0)':
|
||||
dependencies:
|
||||
'@ant-design/colors': 8.0.1
|
||||
'@ant-design/fast-color': 3.0.1
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -208,13 +208,13 @@
|
||||
"description": "Try to start quota windows for idle Codex OAuth accounts",
|
||||
"enableWarmup": "Enable warmup",
|
||||
"cronExpression": "Cron expression",
|
||||
"cronHelp": "Five fields: minute hour day month weekday",
|
||||
"cronHelp": "Press Enter after each tag",
|
||||
"modelLabel": "Warmup model",
|
||||
"modelHelp": "Required when enabled, use an available model",
|
||||
"modelPlaceholder": "Enter model name",
|
||||
"helpTitle": "About account warmup",
|
||||
"help": "Sends a lightweight request to available Codex OAuth accounts, skipping exhausted quotas and confirmed active windows, accounts at 0% may be warmed up, activation is not guaranteed",
|
||||
"cronExample": "Uses the deployment time zone, for example 0 8 * * MON-FRI runs at 08:00 on weekdays, at most once per minute"
|
||||
"scheduleHelp": "Runs in the deployment time zone, once per matching minute"
|
||||
},
|
||||
"adminApiKey": {
|
||||
"adminApiKey": "Admin API key",
|
||||
@@ -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,10 +626,11 @@
|
||||
"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",
|
||||
"warmupCronTooLong": "Keep all rules within 255 bytes",
|
||||
"warmupModelRequired": "Select a model to enable warmup",
|
||||
"invalidWarmupModel": "Warmup model name must not exceed 128 characters",
|
||||
"settingsSaved": "Settings saved",
|
||||
|
||||
@@ -208,13 +208,13 @@
|
||||
"description": "尝试启动空闲 Codex OAuth 账号的额度窗口",
|
||||
"enableWarmup": "启用预热",
|
||||
"cronExpression": "Cron 表达式",
|
||||
"cronHelp": "五段表达式:分 时 日 月 星期",
|
||||
"cronHelp": "回车添加 Tag,支持多个",
|
||||
"modelLabel": "预热模型",
|
||||
"modelHelp": "启用时必填,需为账号可用模型",
|
||||
"modelPlaceholder": "输入模型名称",
|
||||
"helpTitle": "账号预热说明",
|
||||
"help": "向可用的 Codex OAuth 账号发送轻量请求,跳过额度耗尽和已确认活跃的窗口,0% 账号允许预热,不保证窗口成功启动",
|
||||
"cronExample": "按部署时区执行,例如 0 8 * * MON-FRI 表示工作日 08:00,同一分钟只执行一次"
|
||||
"scheduleHelp": "按部署时区执行,同一分钟只预热一次"
|
||||
},
|
||||
"adminApiKey": {
|
||||
"adminApiKey": "管理员 API密钥",
|
||||
@@ -309,7 +309,7 @@
|
||||
"retentionDaysLabel": "历史保留天数"
|
||||
},
|
||||
"retentionDays": "保留天数",
|
||||
"enterDays": "请输入 1–31 天",
|
||||
"enterDays": "请输入保留天数",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestLocationOverride": "请求位置覆盖",
|
||||
@@ -626,10 +626,11 @@
|
||||
"invalidBodyLimit": "Responses 解压上限应为有效的正整数(MiB)",
|
||||
"invalidQuotaThreshold": "剩余周额度上限应为 0~100 的整数,留空表示不限制",
|
||||
"invalidResetLeadTime": "重置卡提前使用时间应为 1~1440 分钟的整数",
|
||||
"invalidRetention": "历史保留天数应为 1–31 的整数",
|
||||
"invalidRetention": "历史保留天数应为正整数",
|
||||
"invalidQueueSettings": "队列容量应为 0~1000 的整数,排队超时应为 1~120 秒的整数",
|
||||
"invalidProbeModel": "探测模型最多 128 个字符,不能包含控制字符",
|
||||
"invalidWarmupCron": "请输入有效的 Cron 表达式",
|
||||
"warmupCronTooLong": "总长度不能超过 255 字节",
|
||||
"warmupModelRequired": "启用预激活时请选择模型",
|
||||
"invalidWarmupModel": "预激活模型名称不能超过 128 个字符",
|
||||
"settingsSaved": "设置已保存",
|
||||
|
||||
@@ -208,13 +208,13 @@
|
||||
"description": "嘗試啓動空閒 Codex OAuth 賬號的額度窗口",
|
||||
"enableWarmup": "啟用預熱",
|
||||
"cronExpression": "Cron 表達式",
|
||||
"cronHelp": "五段表達式:分 時 日 月 星期",
|
||||
"cronHelp": "按 Enter 新增 Tag,支援多個",
|
||||
"modelLabel": "預熱模型",
|
||||
"modelHelp": "啓用時必填,需爲賬號可用模型",
|
||||
"modelPlaceholder": "輸入模型名稱",
|
||||
"helpTitle": "賬號預熱說明",
|
||||
"help": "向可用的 Codex OAuth 帳號傳送輕量請求,略過額度耗盡和已確認活躍的視窗,0% 帳號允許預熱,不保證視窗成功啟動",
|
||||
"cronExample": "按部署時區執行,例如 0 8 * * MON-FRI 表示工作日 08:00,同一分鐘只執行一次"
|
||||
"scheduleHelp": "依部署時區執行,同一分鐘只預熱一次"
|
||||
},
|
||||
"adminApiKey": {
|
||||
"adminApiKey": "管理員 API密鑰",
|
||||
@@ -309,7 +309,7 @@
|
||||
"retentionDaysLabel": "歷史保留天數"
|
||||
},
|
||||
"retentionDays": "保留天數",
|
||||
"enterDays": "請輸入 1–31 天",
|
||||
"enterDays": "請輸入保留天數",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestLocationOverride": "請求位置覆蓋",
|
||||
@@ -626,10 +626,11 @@
|
||||
"invalidBodyLimit": "Responses 解壓上限應爲有效的正整數(MiB)",
|
||||
"invalidQuotaThreshold": "剩餘周額度上限應爲 0~100 的整數,留空表示不限制",
|
||||
"invalidResetLeadTime": "重置卡提前使用時間應爲 1~1440 分鐘的整數",
|
||||
"invalidRetention": "歷史保留天數應爲 1–31 的整數",
|
||||
"invalidRetention": "歷史保留天數應爲正整數",
|
||||
"invalidQueueSettings": "隊列容量應爲 0~1000 的整數,排隊超時應爲 1~120 秒的整數",
|
||||
"invalidProbeModel": "探測模型最多 128 個字符,不能包含控制字符",
|
||||
"invalidWarmupCron": "請輸入有效的 Cron 表達式",
|
||||
"warmupCronTooLong": "總長度不可超過 255 位元組",
|
||||
"warmupModelRequired": "啓用預激活時請選擇模型",
|
||||
"invalidWarmupModel": "預激活模型名稱不能超過 128 個字符",
|
||||
"settingsSaved": "設置已保存",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { ZCard, ZFormItem, ZIconButton, ZInput, ZPopover, ZSwitch } from '@codex-proxy/ui'
|
||||
import { ZCard, ZFormItem, ZIconButton, ZInput, ZInputTag, ZPopover, ZSwitch } from '@codex-proxy/ui'
|
||||
|
||||
import { CircleAlert, Clock, Sparkles } from '@lucide/vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
@@ -7,7 +7,7 @@ import { useI18n } from 'vue-i18n'
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
|
||||
const enabled = defineModel<boolean>('enabled', { required: true })
|
||||
const cron = defineModel<string>('cron', { required: true })
|
||||
const cron = defineModel<string[]>('cron', { required: true })
|
||||
const model = defineModel<string>('model', { required: true })
|
||||
</script>
|
||||
|
||||
@@ -26,7 +26,7 @@ const model = defineModel<string>('model', { required: true })
|
||||
</template>
|
||||
<div class="max-w-80 space-y-2 px-3 py-2 text-z-sm font-regular leading-relaxed text-z-text-secondary">
|
||||
<p>{{ t('settings.warmup.help') }}</p>
|
||||
<p>{{ t('settings.warmup.cronExample') }}</p>
|
||||
<p>{{ t('settings.warmup.scheduleHelp') }}</p>
|
||||
</div>
|
||||
</ZPopover>
|
||||
</span>
|
||||
@@ -39,14 +39,16 @@ const model = defineModel<string>('model', { required: true })
|
||||
/>
|
||||
|
||||
<ZFormItem
|
||||
:show-message="enabled"
|
||||
:show-message="enabled || cron.length > 0"
|
||||
prop="accountWarmupCron"
|
||||
:label="t('settings.warmup.cronExpression')"
|
||||
:description="t('settings.warmup.cronHelp')"
|
||||
:required="enabled"
|
||||
>
|
||||
<ZInput
|
||||
<ZInputTag
|
||||
v-model="cron"
|
||||
collapse-tags
|
||||
collapse-tags-tooltip
|
||||
:disabled="!enabled"
|
||||
:aria-label="t('settings.warmup.cronExpression')"
|
||||
placeholder="0 8 * * *"
|
||||
@@ -56,7 +58,7 @@ const model = defineModel<string>('model', { required: true })
|
||||
<template #prefix>
|
||||
<Clock class="size-4" />
|
||||
</template>
|
||||
</ZInput>
|
||||
</ZInputTag>
|
||||
</ZFormItem>
|
||||
|
||||
<ZFormItem
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -71,7 +71,7 @@ export function useSettingsForm() {
|
||||
probeModel: '',
|
||||
} as ProtectionDraft,
|
||||
accountWarmupEnabled: false,
|
||||
accountWarmupCron: '',
|
||||
accountWarmupCron: [] as string[],
|
||||
accountWarmupModel: '',
|
||||
resetCreditOnLowWeeklyQuota: false,
|
||||
resetCreditLeadMinutes: 10 as number | null,
|
||||
@@ -162,7 +162,9 @@ export function useSettingsForm() {
|
||||
probeModel: data.accountErrorProtection.probeModel ?? '',
|
||||
}
|
||||
form.accountWarmupEnabled = data.accountWarmupEnabled
|
||||
form.accountWarmupCron = data.accountWarmupCron ?? ''
|
||||
form.accountWarmupCron = data.accountWarmupCron
|
||||
? data.accountWarmupCron.split(';')
|
||||
: []
|
||||
form.accountWarmupModel = data.accountWarmupModel ?? ''
|
||||
form.resetCreditOnLowWeeklyQuota = data.resetCreditOnLowWeeklyQuota
|
||||
form.resetCreditLeadMinutes = data.resetCreditLeadMinutes
|
||||
@@ -294,7 +296,7 @@ export function useSettingsForm() {
|
||||
probeModel: trimProtectionText(form.accountErrorProtection.probeModel) || null,
|
||||
},
|
||||
accountWarmupEnabled: form.accountWarmupEnabled,
|
||||
accountWarmupCron: form.accountWarmupCron.trim(),
|
||||
accountWarmupCron: form.accountWarmupCron.join(';'),
|
||||
accountWarmupModel: form.accountWarmupModel.trim() || null,
|
||||
resetCreditOnLowWeeklyQuota: form.resetCreditOnLowWeeklyQuota,
|
||||
resetCreditLeadMinutes: (form.resetCreditOnLowWeeklyQuota ? savedSettings.form.resetCreditLeadMinutes : resetCreditLeadMinutes)!,
|
||||
|
||||
@@ -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,17 +47,22 @@ 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': {
|
||||
required: form().accountWarmupEnabled,
|
||||
pattern: /^\S+(?: +\S+){4}$/,
|
||||
max: 255,
|
||||
transform: value => value.trim(),
|
||||
trigger: ['blur', 'change'],
|
||||
message: t('settings.form.invalidWarmupCron'),
|
||||
},
|
||||
'accountWarmupCron': [
|
||||
{
|
||||
validator: (_rule, expressions: string[]) => (!form().accountWarmupEnabled || expressions.length > 0)
|
||||
&& expressions.every(expression => /^[^\s;]+(?: +[^\s;]+){4}$/u.test(expression)),
|
||||
message: t('settings.form.invalidWarmupCron'),
|
||||
trigger: ['blur', 'change'],
|
||||
},
|
||||
{
|
||||
validator: (_rule, expressions: string[]) => new TextEncoder().encode(expressions.join(';')).length <= 255,
|
||||
message: t('settings.form.warmupCronTooLong'),
|
||||
trigger: ['blur', 'change'],
|
||||
},
|
||||
],
|
||||
'accountWarmupModel': [
|
||||
{
|
||||
required: form().accountWarmupEnabled,
|
||||
|
||||
+1
-1
Submodule modules/ui updated: 45b076735d...68458e62e7
+12
-14
@@ -1,23 +1,21 @@
|
||||
# v3.46.0
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **出站代理规则已统一。** 账号请求、插件受管网络和网关下载优先使用显式指定的代理。账号未绑定代理或绑定的代理已禁用时,使用「部署默认出口」
|
||||
>
|
||||
> 默认出口按目标协议读取网关进程的环境变量:HTTP/WS 使用 `HTTP_PROXY`,HTTPS/WSS 使用 `HTTPS_PROXY`,未设置对应变量时使用 `ALL_PROXY`。命中 `NO_PROXY` 或没有适用的环境代理时直连;选中的代理连接失败会报错,不会回退直连
|
||||
>
|
||||
> 如果网关已设置环境代理,升级前请检查原先依赖直连的目标是否已列入 `NO_PROXY`。使用 Docker Compose 时,环境变量应配置在网关服务的 `environment` 中,修改后需重新创建容器
|
||||
# v3.48.0
|
||||
|
||||
## 新增功能
|
||||
|
||||
- 智能调度:可分别设置 5 小时、周、月额度重置对选号的影响,例如只优先选择周额度即将重置的账号
|
||||
- 请求历史:保留期可设为超过 31 天的正整数,默认仍为 31 天,升级保留已有设置
|
||||
- 管理端:支持在兼容浏览器中安装为独立窗口应用,需通过 HTTPS 或本机回环地址访问,使用时仍需联网
|
||||
|
||||
## 优化改进
|
||||
|
||||
- OpenAI 客户端画像:无法在线获取版本时,内置 CLI 版本为 0.169.0,macOS 和 Linux Desktop 的 Core 版本为 0.169.0-alpha.11.1,Windows 为 0.167.0-alpha.4.1
|
||||
|
||||
## 问题修复
|
||||
|
||||
- OpenAI WebSocket:按上游连接声明的消息大小上限发送请求,避免大请求过早切换到 HTTP;仅能在原连接续写的请求超限时,提示客户端重发完整历史
|
||||
- 系统更新:修复 macOS 和 Linux 移动只读目录时可能导致的更新或回滚失败,保留目录原有权限
|
||||
- 管理界面:切换主题预设时保留自定义功能色、背景色和文字色
|
||||
- OpenAI 账号:模型目录刷新、导出、预热和连接测试使用已保存的全局客户端画像,修复选用新版 CLI 后目录仍可能缺少模型的问题
|
||||
- Responses:HTTP 和 WebSocket 请求通过正文传递安装身份,默认不再额外发送独立的安装 ID 请求头
|
||||
- WebSocket:下游读取缓慢时,取消请求或关闭连接池仍能及时释放上游连接
|
||||
- 依赖安全:更新 Quinn 和 rustix,修复依赖审计报告的问题
|
||||
|
||||
## 升级须知
|
||||
|
||||
- 原有智能调度配置的三个重置窗口系数默认为 0;此前依赖额度重置评分的部署,请至少为一个窗口设置正系数
|
||||
- 发往 OpenAI Responses 上游的生成请求会忽略客户端指定的输出 Token 上限,实际输出可能超过该值;OAuth 和 API Key 账号均适用
|
||||
- Responses 的 UUIDv7 会话和线程标识、Codex 复合窗口标识在升级后会映射为新值;账号隔离保留,UUIDv7 的原始创建时间和窗口序号对上游可见
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
version: 3.46.0
|
||||
version: 3.48.0
|
||||
published:
|
||||
'3':
|
||||
stable: 3.45.2
|
||||
rc: 3.45.2
|
||||
beta: 3.45.2
|
||||
alpha: 3.45.2
|
||||
stable: 3.47.0
|
||||
rc: 3.47.0
|
||||
beta: 3.47.0
|
||||
alpha: 3.47.0
|
||||
|
||||
Reference in New Issue
Block a user