mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-24 00:00:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ee9abf9d2 | ||
|
|
779ccf4e6c | ||
|
|
d13a4a2138 | ||
|
|
a0e7f38f7f | ||
|
|
41f97ac836 | ||
|
|
975b73b33b | ||
|
|
d787195f4e | ||
|
|
8681d6851e | ||
|
|
d99ecf9aaf | ||
|
|
8f40bfda7c | ||
|
|
72215fe62a | ||
|
|
f61995e61d | ||
|
|
f85d80d756 | ||
|
|
e1f84c210b |
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 {
|
||||
|
||||
@@ -63,6 +63,7 @@ pub struct RuntimeSettingsValues {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: gateway_core::account::ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1180,6 +1180,7 @@ impl SettingsStore for StaticSettingsStore {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 30,
|
||||
|
||||
@@ -51,6 +51,7 @@ fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -1432,6 +1432,7 @@ impl SettingsStore for FixtureSettingsStore {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -257,8 +257,8 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
&["RequestLocation"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/smart_scheduling.rs",
|
||||
&["SmartSchedulingValues", "ResetWindowWeights"][..],
|
||||
"../gateway-core/src/account/scheduling.rs",
|
||||
&["SmartSchedulingValues", "ResetWindowValues"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/metering/pricing.rs",
|
||||
@@ -326,10 +326,10 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
}
|
||||
types.push(contract_type(
|
||||
item,
|
||||
if *name == "SmartSchedulingValues" {
|
||||
"SmartSchedulingConfig"
|
||||
} else {
|
||||
name
|
||||
match *name {
|
||||
"SmartSchedulingValues" => "SmartSchedulingConfig",
|
||||
"ResetWindowValues" => "ResetWindowWeights",
|
||||
_ => name,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
@@ -343,6 +343,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(
|
||||
),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -54,6 +54,7 @@ pub struct RuntimeSettingsFields {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: gateway_core::account::ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u64,
|
||||
@@ -77,6 +78,7 @@ pub struct RuntimeSettingsView {
|
||||
pub values: RuntimeSettingsFields,
|
||||
pub config_revision: u64,
|
||||
pub smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights,
|
||||
pub provider_request_profiles: ProviderRequestProfiles,
|
||||
/// 固定兼容字段;值始终从 provider_request_profiles 派生
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
@@ -150,6 +152,8 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
|
||||
provider_request_profiles,
|
||||
model_mappings: wire_model_mappings(settings.model_mappings),
|
||||
smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights::default(
|
||||
),
|
||||
rotation_strategy: settings.rotation_strategy.as_str().to_owned(),
|
||||
updated_at_display: time.datetime(&settings.updated_at),
|
||||
updated_at: settings.updated_at,
|
||||
@@ -765,6 +769,7 @@ impl RuntimeSettingsFields {
|
||||
openai_session_affinity_ttl_hours: self.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: self.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: self.smart_scheduling,
|
||||
quota_reset_window_weights: self.quota_reset_window_weights,
|
||||
min_codex_desktop_version: self.min_codex_desktop_version,
|
||||
min_codex_cli_version: self.min_codex_cli_version,
|
||||
usage_retention_days: u32::try_from(self.usage_retention_days)
|
||||
@@ -808,6 +813,7 @@ impl From<gateway_admin::model::settings::RuntimeSettingsValues> for RuntimeSett
|
||||
openai_session_affinity_ttl_hours: settings.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: settings.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: settings.smart_scheduling,
|
||||
quota_reset_window_weights: settings.quota_reset_window_weights,
|
||||
min_codex_desktop_version: settings.min_codex_desktop_version,
|
||||
min_codex_cli_version: settings.min_codex_cli_version,
|
||||
usage_retention_days: u64::from(settings.usage_retention_days),
|
||||
|
||||
@@ -664,6 +664,7 @@ impl SettingsStore for MemorySettingsStore {
|
||||
.values
|
||||
.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: command.values.smart_scheduling,
|
||||
quota_reset_window_weights: command.values.quota_reset_window_weights,
|
||||
min_codex_desktop_version: command.values.min_codex_desktop_version,
|
||||
min_codex_cli_version: command.values.min_codex_cli_version,
|
||||
usage_retention_days: command.values.usage_retention_days,
|
||||
@@ -1940,6 +1941,7 @@ fn test_runtime_settings() -> RuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -63,6 +63,7 @@ fn update_body() -> Value {
|
||||
"responsesMaxDecompressedBodyBytes": 67108864,
|
||||
"rotationStrategy": "round_robin",
|
||||
"smartScheduling": gateway_core::account::SmartSchedulingConfig::default(),
|
||||
"quotaResetWindowWeights": gateway_core::account::ResetWindowWeights::default(),
|
||||
"minCodexDesktopVersion": "26.825.6671",
|
||||
"minCodexCliVersion": "0.40.0",
|
||||
"usageRetentionDays": 31,
|
||||
@@ -264,12 +265,14 @@ fn error_protection_requires_an_explicit_policy_in_settings_replacement() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_intact() {
|
||||
async fn scheduling_settings_round_trip_and_invalid_updates_leave_the_saved_value_intact() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let mut body = update_body();
|
||||
let custom = json!({"loadWeight": 2.0, "quotaWeight": 0.0, "healthWeight": 1.0, "latencyWeight": 0.5, "resetWeight": 1.2, "queueWeight": 2.3, "resetWindowWeights":{"fiveHour":0.0,"weekly":1.5,"monthly":0.0}});
|
||||
body["smartScheduling"] = custom.clone();
|
||||
let windows = json!({"fiveHour":2.5,"weekly":0.0,"monthly":1.0});
|
||||
body["quotaResetWindowWeights"] = windows.clone();
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
@@ -281,6 +284,7 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let response = response_json(response).await;
|
||||
assert_eq!(response["data"]["smartScheduling"], custom);
|
||||
assert_eq!(response["data"]["quotaResetWindowWeights"], windows);
|
||||
assert_eq!(
|
||||
response["data"]["smartSchedulingDefaults"],
|
||||
json!(gateway_core::account::SmartSchedulingConfig::default())
|
||||
@@ -301,7 +305,7 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
missing.as_object_mut().unwrap().remove(field);
|
||||
invalid_values.push(missing);
|
||||
}
|
||||
for windows in [
|
||||
let invalid_windows = [
|
||||
json!({}),
|
||||
json!({"weekly":1}),
|
||||
json!(null),
|
||||
@@ -309,41 +313,53 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
json!({"fiveHour":-1,"weekly":1,"monthly":0}),
|
||||
json!({"fiveHour":0,"weekly":1,"monthly":10.1}),
|
||||
json!({"fiveHour":0,"weekly":1,"monthly":0,"daily":1}),
|
||||
] {
|
||||
];
|
||||
for windows in &invalid_windows {
|
||||
let mut invalid = custom.clone();
|
||||
invalid["resetWindowWeights"] = windows;
|
||||
invalid["resetWindowWeights"] = windows.clone();
|
||||
invalid_values.push(invalid);
|
||||
}
|
||||
for invalid in invalid_values {
|
||||
body["smartScheduling"] = invalid;
|
||||
for (field, invalid) in invalid_values
|
||||
.into_iter()
|
||||
.map(|value| ("smartScheduling", value))
|
||||
.chain(
|
||||
invalid_windows
|
||||
.into_iter()
|
||||
.map(|value| ("quotaResetWindowWeights", value)),
|
||||
)
|
||||
{
|
||||
let mut invalid_body = body.clone();
|
||||
invalid_body[field] = invalid;
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body.clone()),
|
||||
Some(invalid_body),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
for field in ["smartScheduling", "quotaResetWindowWeights"] {
|
||||
let mut missing = body.clone();
|
||||
missing.as_object_mut().unwrap().remove(field);
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(missing),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
body.as_object_mut().unwrap().remove("smartScheduling");
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(Method::GET, "/api/admin/settings", None))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response_json(response).await["data"]["smartScheduling"],
|
||||
custom
|
||||
);
|
||||
let saved = response_json(response).await;
|
||||
assert_eq!(saved["data"]["smartScheduling"], custom);
|
||||
assert_eq!(saved["data"]["quotaResetWindowWeights"], windows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -357,11 +373,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 +394,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 +417,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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ use axum::{
|
||||
body::{Body, to_bytes},
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use bytes::Bytes;
|
||||
use futures::{StreamExt as _, future::BoxFuture};
|
||||
use gateway_core::{
|
||||
@@ -122,10 +123,12 @@ async fn native_json_preserves_extensions_and_uses_client_key_authentication() {
|
||||
)]);
|
||||
let trace = session.trace.clone();
|
||||
let (router, execution) = fixture(session).await;
|
||||
let response = router
|
||||
.oneshot(request("/v1/messages", false))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut incoming = request("/v1/messages", false);
|
||||
incoming.headers_mut().insert(
|
||||
"x-claude-code-session-id",
|
||||
"claude-session".parse().unwrap(),
|
||||
);
|
||||
let response = router.oneshot(incoming).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(bytes, raw);
|
||||
@@ -136,6 +139,15 @@ async fn native_json_preserves_extensions_and_uses_client_key_authentication() {
|
||||
panic!("generate")
|
||||
};
|
||||
assert_eq!(generation.protocol_payload().protocol(), "anthropic");
|
||||
assert!(
|
||||
generation.protocol_payload().context()["opaque_request_headers"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!([
|
||||
"x-claude-code-session-id",
|
||||
STANDARD.encode("claude-session")
|
||||
]))
|
||||
);
|
||||
assert_eq!(
|
||||
generation.protocol_payload().body()["future_extension"]["preserved"],
|
||||
true
|
||||
|
||||
@@ -17,17 +17,19 @@ mod quota_limits;
|
||||
pub use quota_limits::{AccountQuotaLimits, QuotaMergeBase, QuotaPauseDecision};
|
||||
mod proxy;
|
||||
pub use proxy::{InvalidOutboundProxy, OutboundProxy};
|
||||
mod scheduling;
|
||||
pub mod scope;
|
||||
mod selection;
|
||||
mod smart_scheduling;
|
||||
mod store;
|
||||
mod upstream;
|
||||
|
||||
pub use error::CredentialError;
|
||||
pub use model::*;
|
||||
pub use scheduling::{
|
||||
QuotaResetPeriod, ResetWindowWeights, SchedulingConfigError, SmartSchedulingConfig,
|
||||
};
|
||||
pub(crate) use selection::smart_score;
|
||||
pub use selection::*;
|
||||
pub use smart_scheduling::{QuotaResetPeriod, SmartSchedulingConfig, SmartSchedulingConfigError};
|
||||
pub use store::{
|
||||
AccountCatalogObservation, AccountCatalogOmissions, ProviderAccountStore, StoredAccountCatalog,
|
||||
};
|
||||
|
||||
+88
-44
@@ -1,4 +1,4 @@
|
||||
//! 智能调度参数;固定十分位保证快照可精确比较,浮点数只用于 wire 与评分
|
||||
//! 账号调度参数与共享重置窗口评分;固定十分位保证快照可精确比较,浮点数只用于 wire 与评分
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||
#[serde(into = "SmartSchedulingValues")]
|
||||
pub struct SmartSchedulingConfig {
|
||||
weights: [u8; 6],
|
||||
reset_windows: [u8; 3],
|
||||
reset_windows: ResetWindowWeights,
|
||||
}
|
||||
|
||||
/// 账号通用额度的周期;Provider 按上游周期事实投影,不依赖窗口名称和槽位
|
||||
@@ -30,9 +30,9 @@ impl QuotaResetPeriod {
|
||||
}
|
||||
}
|
||||
|
||||
fn weight_tenths(value: f64, field: &'static str) -> Result<u8, SmartSchedulingConfigError> {
|
||||
fn weight_tenths(value: f64, field: &'static str) -> Result<u8, SchedulingConfigError> {
|
||||
if !(0.0..=10.0).contains(&value) || (value * 10.0).round() / 10.0 != value {
|
||||
return Err(SmartSchedulingConfigError::InvalidWeight(field));
|
||||
return Err(SchedulingConfigError::InvalidWeight(field));
|
||||
}
|
||||
Ok((value * 10.0).round() as u8)
|
||||
}
|
||||
@@ -52,13 +52,13 @@ impl Default for SmartSchedulingConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
weights: [10, 8, 10, 5, 0, 0],
|
||||
reset_windows: [0; 3],
|
||||
reset_windows: ResetWindowWeights::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum SmartSchedulingConfigError {
|
||||
pub enum SchedulingConfigError {
|
||||
#[error("{0} must be between 0 and 10 with at most one decimal place")]
|
||||
InvalidWeight(&'static str),
|
||||
#[error("at least one smart scheduling weight must be positive")]
|
||||
@@ -67,7 +67,7 @@ pub enum SmartSchedulingConfigError {
|
||||
|
||||
impl SmartSchedulingConfig {
|
||||
/// 顺序为负载、剩余额度、健康、首输出延迟、额度重置和排队压力
|
||||
pub fn new(weights: [f64; 6]) -> Result<Self, SmartSchedulingConfigError> {
|
||||
pub fn new(weights: [f64; 6]) -> Result<Self, SchedulingConfigError> {
|
||||
let mut tenths = [0; 6];
|
||||
for (index, field) in [
|
||||
"loadWeight",
|
||||
@@ -83,7 +83,7 @@ impl SmartSchedulingConfig {
|
||||
tenths[index] = weight_tenths(weights[index], field)?;
|
||||
}
|
||||
if tenths == [0; 6] {
|
||||
return Err(SmartSchedulingConfigError::EmptyWeights);
|
||||
return Err(SchedulingConfigError::EmptyWeights);
|
||||
}
|
||||
Ok(Self {
|
||||
weights: tenths,
|
||||
@@ -97,35 +97,14 @@ impl SmartSchedulingConfig {
|
||||
}
|
||||
|
||||
fn reset_enabled(self) -> bool {
|
||||
self.weights[4] > 0 && self.reset_windows != [0; 3]
|
||||
self.weights[4] > 0 && self.reset_windows.enabled()
|
||||
}
|
||||
|
||||
pub(crate) fn reset_score(self, signals: &AccountRuntimeSignals, now: SystemTime) -> f64 {
|
||||
if !self.reset_enabled() {
|
||||
return 0.0;
|
||||
}
|
||||
let mut scores = [0.0_f64; 3];
|
||||
for window in signals.quota_reset_windows.as_deref().unwrap_or_default() {
|
||||
let Ok(remaining) = window.reset_at.duration_since(now) else {
|
||||
continue;
|
||||
};
|
||||
if remaining.is_zero() {
|
||||
continue;
|
||||
}
|
||||
let half = f64::from(window.period.seconds()) / 7.0;
|
||||
let score = half / (half + remaining.as_secs_f64());
|
||||
// 同一周期只计最近的有效重置,避免重复额度桶叠加得分
|
||||
let slot = &mut scores[window.period as usize];
|
||||
*slot = slot.max(score);
|
||||
}
|
||||
// 缺失周期得零分,分母固定,避免少报窗口反而提高排名
|
||||
let weights = self.reset_windows.map(f64::from);
|
||||
scores
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(score, w)| score * w)
|
||||
.sum::<f64>()
|
||||
/ weights.into_iter().sum::<f64>()
|
||||
self.reset_windows.score(signals, now)
|
||||
}
|
||||
|
||||
pub(crate) fn score_tolerance(self) -> f64 {
|
||||
@@ -157,14 +136,14 @@ struct SmartSchedulingValues {
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ResetWindowWeights {
|
||||
struct ResetWindowValues {
|
||||
five_hour: f64,
|
||||
weekly: f64,
|
||||
monthly: f64,
|
||||
}
|
||||
|
||||
impl TryFrom<SmartSchedulingValues> for SmartSchedulingConfig {
|
||||
type Error = SmartSchedulingConfigError;
|
||||
type Error = SchedulingConfigError;
|
||||
|
||||
fn try_from(value: SmartSchedulingValues) -> Result<Self, Self::Error> {
|
||||
let mut config = Self::new([
|
||||
@@ -175,12 +154,7 @@ impl TryFrom<SmartSchedulingValues> for SmartSchedulingConfig {
|
||||
value.reset_weight,
|
||||
value.queue_weight,
|
||||
])?;
|
||||
let windows = value.reset_window_weights;
|
||||
config.reset_windows = [
|
||||
weight_tenths(windows.five_hour, "fiveHour")?,
|
||||
weight_tenths(windows.weekly, "weekly")?,
|
||||
weight_tenths(windows.monthly, "monthly")?,
|
||||
];
|
||||
config.reset_windows = value.reset_window_weights;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
@@ -202,11 +176,81 @@ impl From<SmartSchedulingConfig> for SmartSchedulingValues {
|
||||
latency_weight,
|
||||
reset_weight,
|
||||
queue_weight,
|
||||
reset_window_weights: ResetWindowWeights {
|
||||
five_hour: f64::from(value.reset_windows[0]) / 10.0,
|
||||
weekly: f64::from(value.reset_windows[1]) / 10.0,
|
||||
monthly: f64::from(value.reset_windows[2]) / 10.0,
|
||||
},
|
||||
reset_window_weights: value.reset_windows,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 两种调度策略共用的重置窗口系数,各策略独立持有配置
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
|
||||
#[serde(into = "ResetWindowValues")]
|
||||
pub struct ResetWindowWeights {
|
||||
weights: [u8; 3],
|
||||
}
|
||||
|
||||
impl ResetWindowWeights {
|
||||
/// 顺序为 5 小时、周、月;全零表示未指定窗口
|
||||
pub fn new(weights: [f64; 3]) -> Result<Self, SchedulingConfigError> {
|
||||
Ok(Self {
|
||||
weights: [
|
||||
weight_tenths(weights[0], "fiveHour")?,
|
||||
weight_tenths(weights[1], "weekly")?,
|
||||
weight_tenths(weights[2], "monthly")?,
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn enabled(self) -> bool {
|
||||
self.weights != [0; 3]
|
||||
}
|
||||
|
||||
pub(crate) fn score(self, signals: &AccountRuntimeSignals, now: SystemTime) -> f64 {
|
||||
if !self.enabled() {
|
||||
return 0.0;
|
||||
}
|
||||
let mut scores = [0.0_f64; 3];
|
||||
for window in signals.quota_reset_windows.as_deref().unwrap_or_default() {
|
||||
let Ok(remaining) = window.reset_at.duration_since(now) else {
|
||||
continue;
|
||||
};
|
||||
if remaining.is_zero() {
|
||||
continue;
|
||||
}
|
||||
let half = f64::from(window.period.seconds()) / 7.0;
|
||||
let score = half / (half + remaining.as_secs_f64());
|
||||
// 同一周期只计最近的有效重置,避免重复额度桶叠加得分
|
||||
let slot = &mut scores[window.period as usize];
|
||||
*slot = slot.max(score);
|
||||
}
|
||||
// 缺失周期得零分,分母固定,避免少报窗口反而提高排名
|
||||
let weights = self.weights.map(f64::from);
|
||||
scores
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(score, w)| score * w)
|
||||
.sum::<f64>()
|
||||
/ weights.into_iter().sum::<f64>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ResetWindowWeights {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
// 与智能调度一致,先恢复 flatten 缓冲的小数再校验
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
let values =
|
||||
serde_json::from_value::<ResetWindowValues>(value).map_err(serde::de::Error::custom)?;
|
||||
Self::new([values.five_hour, values.weekly, values.monthly])
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResetWindowWeights> for ResetWindowValues {
|
||||
fn from(value: ResetWindowWeights) -> Self {
|
||||
let [five_hour, weekly, monthly] = value.weights.map(|weight| f64::from(weight) / 10.0);
|
||||
Self {
|
||||
five_hour,
|
||||
weekly,
|
||||
monthly,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ use crate::concurrency::{CapacityWait, ConcurrencyQueuePolicy, QueueRejection};
|
||||
use crate::identity::ProviderKind;
|
||||
|
||||
use super::{
|
||||
AccountConcurrency, AccountStatus, ProviderAccount, ProviderAccountId, SmartSchedulingConfig,
|
||||
AccountConcurrency, AccountStatus, ProviderAccount, ProviderAccountId, ResetWindowWeights,
|
||||
SmartSchedulingConfig,
|
||||
};
|
||||
|
||||
/// `runtime_settings.rotation_strategy` 的稳定值
|
||||
@@ -90,6 +91,7 @@ impl AccountAffinity {
|
||||
pub struct AccountSelectionPolicy {
|
||||
strategy: RotationStrategy,
|
||||
smart_scheduling: SmartSchedulingConfig,
|
||||
quota_reset_window_weights: ResetWindowWeights,
|
||||
max_concurrent_per_account: AccountConcurrency,
|
||||
queue_policy: ConcurrencyQueuePolicy,
|
||||
openai_guardian_reserved_concurrency: u32,
|
||||
@@ -108,6 +110,7 @@ impl AccountSelectionPolicy {
|
||||
Self {
|
||||
strategy,
|
||||
smart_scheduling: SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: ResetWindowWeights::default(),
|
||||
max_concurrent_per_account: max_concurrent_per_account.into(),
|
||||
openai_guardian_reserved_concurrency: 0,
|
||||
openai_account_affinity: AccountAffinity::default(),
|
||||
@@ -121,6 +124,17 @@ impl AccountSelectionPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_quota_reset_window_weights(mut self, weights: ResetWindowWeights) -> Self {
|
||||
self.quota_reset_window_weights = weights;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn quota_reset_window_weights(self) -> ResetWindowWeights {
|
||||
self.quota_reset_window_weights
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_openai_account_affinity(mut self, affinity: AccountAffinity) -> Self {
|
||||
self.openai_account_affinity = affinity;
|
||||
@@ -799,24 +813,31 @@ impl AccountSelector {
|
||||
let candidate = match context.policy.strategy() {
|
||||
RotationStrategy::QuotaResetPriority => {
|
||||
eligible.sort_by(|left, right| {
|
||||
(
|
||||
left.signals.quota_reset_at.is_none(),
|
||||
left.signals.quota_reset_at,
|
||||
)
|
||||
.cmp(&(
|
||||
right.signals.quota_reset_at.is_none(),
|
||||
right.signals.quota_reset_at,
|
||||
))
|
||||
.then_with(|| {
|
||||
capacity_utilization(left, context)
|
||||
.total_cmp(&capacity_utilization(right, context))
|
||||
})
|
||||
.then_with(|| {
|
||||
left.signals
|
||||
.last_started_at
|
||||
.cmp(&right.signals.last_started_at)
|
||||
})
|
||||
.then_with(|| left.account.id().cmp(right.account.id()))
|
||||
let windows = context.policy.quota_reset_window_weights();
|
||||
if windows.enabled() {
|
||||
windows
|
||||
.score(&right.signals, context.now)
|
||||
.total_cmp(&windows.score(&left.signals, context.now))
|
||||
} else {
|
||||
(
|
||||
left.signals.quota_reset_at.is_none(),
|
||||
left.signals.quota_reset_at,
|
||||
)
|
||||
.cmp(&(
|
||||
right.signals.quota_reset_at.is_none(),
|
||||
right.signals.quota_reset_at,
|
||||
))
|
||||
}
|
||||
.then_with(|| {
|
||||
capacity_utilization(left, context)
|
||||
.total_cmp(&capacity_utilization(right, context))
|
||||
})
|
||||
.then_with(|| {
|
||||
left.signals
|
||||
.last_started_at
|
||||
.cmp(&right.signals.last_started_at)
|
||||
})
|
||||
.then_with(|| left.account.id().cmp(right.account.id()))
|
||||
});
|
||||
eligible.first().copied()?
|
||||
}
|
||||
|
||||
@@ -5,6 +5,14 @@ use crate::{account::OpaqueProviderData, identity::ProviderKind, routing::Config
|
||||
use futures::future::BoxFuture;
|
||||
|
||||
pub trait ProviderRuntimePolicyPort: Send + Sync {
|
||||
/// 后台模型操作读取 Provider 全局画像,不读取单个 Client Key 的覆盖
|
||||
fn load_request_profile<'a>(
|
||||
&'a self,
|
||||
_provider: &'a ProviderKind,
|
||||
) -> BoxFuture<'a, Result<Option<OpaqueProviderData>, ProviderStoreError>> {
|
||||
Box::pin(async { Ok(None) })
|
||||
}
|
||||
|
||||
/// 原子推进预热执行游标,重启或时钟回拨后不重复领取已消费的时刻
|
||||
fn claim_warmup_slot<'a>(
|
||||
&'a self,
|
||||
|
||||
@@ -1247,13 +1247,13 @@ impl RuntimeSnapshot {
|
||||
operation: &Operation,
|
||||
context: &RoutingContext,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
self.all_account_scope(),
|
||||
context,
|
||||
false,
|
||||
)
|
||||
// 探测没有 Client Key,沿用同一发布快照的全局画像
|
||||
let scope = self
|
||||
.all_account_scope()
|
||||
.as_ref()
|
||||
.clone()
|
||||
.with_request_profiles(self.settings().request_profiles().clone());
|
||||
self.plan_inner(public_model, operation, Arc::new(scope), context, false)
|
||||
}
|
||||
|
||||
fn plan_inner(
|
||||
|
||||
@@ -101,6 +101,7 @@ impl CompiledSettings {
|
||||
u64::from(settings.openai_session_affinity_ttl_hours) * 3600,
|
||||
))
|
||||
.with_smart_scheduling(settings.smart_scheduling)
|
||||
.with_quota_reset_window_weights(settings.quota_reset_window_weights)
|
||||
.with_queue(ConcurrencyQueuePolicy {
|
||||
max_waiting: settings.max_waiting_per_account,
|
||||
timeout: Duration::from_secs(u64::from(settings.account_queue_timeout_seconds)),
|
||||
|
||||
@@ -30,6 +30,8 @@ pub struct SettingsValues {
|
||||
pub(crate) openai_session_affinity_ttl_hours: u32,
|
||||
pub(crate) responses_max_decompressed_body_bytes: u64,
|
||||
pub(crate) smart_scheduling: crate::account::SmartSchedulingConfig,
|
||||
#[serde(default)]
|
||||
pub(crate) quota_reset_window_weights: crate::account::ResetWindowWeights,
|
||||
pub(crate) rotation_strategy: String,
|
||||
pub(crate) model_mappings: Arc<BTreeMap<String, String>>,
|
||||
pub(crate) min_codex_desktop_version: Option<String>,
|
||||
@@ -111,6 +113,15 @@ impl SettingsValues {
|
||||
&self.request_profiles
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_quota_reset_window_weights(
|
||||
mut self,
|
||||
weights: crate::account::ResetWindowWeights,
|
||||
) -> Self {
|
||||
self.quota_reset_window_weights = weights;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_smart_scheduling(
|
||||
mut self,
|
||||
@@ -201,6 +212,7 @@ impl SettingsValues {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: crate::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: crate::account::ResetWindowWeights::default(),
|
||||
rotation_strategy: rotation_strategy.into(),
|
||||
model_mappings: Arc::new(model_mappings),
|
||||
min_codex_desktop_version,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
mod location;
|
||||
mod model_access;
|
||||
mod proxy;
|
||||
mod quota_reset_priority;
|
||||
mod scope;
|
||||
mod selection;
|
||||
mod selection_diagnostics;
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
//! 验证重置窗口独立排序、相对系数及亲和与可用性边界
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use gateway_core::account::{
|
||||
AccountQuotaResetWindow, AccountSelector, QuotaResetPeriod, ResetWindowWeights,
|
||||
RotationStrategy,
|
||||
};
|
||||
|
||||
use super::{candidate, context};
|
||||
|
||||
#[test]
|
||||
fn selected_windows_rank_strictly_without_mixing_runtime_scores_or_rotating_near_ties() {
|
||||
let mut context = context(RotationStrategy::QuotaResetPriority);
|
||||
let mut early_weekly = candidate("acct_early_weekly", 2, Some(100));
|
||||
early_weekly.signals.failure_rate_basis_points = Some(9_000);
|
||||
early_weekly.signals.first_output_latency_ms = Some(20_000);
|
||||
let mut late_weekly = candidate("acct_late_weekly", 0, Some(9_900));
|
||||
late_weekly.signals.failure_rate_basis_points = Some(0);
|
||||
late_weekly.signals.first_output_latency_ms = Some(10);
|
||||
for (candidate, short, weekly) in [
|
||||
(&mut early_weekly, 14_400, 6 * 86_400),
|
||||
(&mut late_weekly, 3_600, 6 * 86_400 + 43_200),
|
||||
] {
|
||||
candidate.signals.quota_reset_at = Some(context.now + Duration::from_secs(short));
|
||||
candidate.signals.quota_reset_windows = Some(
|
||||
[
|
||||
(QuotaResetPeriod::FiveHour, short),
|
||||
(QuotaResetPeriod::Weekly, weekly),
|
||||
]
|
||||
.map(|(period, seconds)| AccountQuotaResetWindow {
|
||||
period,
|
||||
reset_at: context.now + Duration::from_secs(seconds),
|
||||
})
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
let mut candidates = [early_weekly, late_weekly];
|
||||
for (weights, expected) in [
|
||||
([0.0, 0.0, 0.0], "acct_late_weekly"),
|
||||
([1.0, 0.0, 0.0], "acct_late_weekly"),
|
||||
([0.0, 0.1, 0.0], "acct_early_weekly"),
|
||||
([0.0, 1.0, 0.0], "acct_early_weekly"),
|
||||
([0.0, 10.0, 0.0], "acct_early_weekly"),
|
||||
([1.0, 0.1, 0.0], "acct_late_weekly"),
|
||||
([10.0, 1.0, 0.0], "acct_late_weekly"),
|
||||
([0.1, 10.0, 0.0], "acct_early_weekly"),
|
||||
] {
|
||||
context.policy = context
|
||||
.policy
|
||||
.with_quota_reset_window_weights(ResetWindowWeights::new(weights).unwrap());
|
||||
for cursor in 0..6 {
|
||||
context.round_robin_cursor = cursor;
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
expected,
|
||||
"weights {weights:?}, cursor {cursor}"
|
||||
);
|
||||
}
|
||||
}
|
||||
context.preferred_account = Some(candidates[1].account.id().clone());
|
||||
let selected = AccountSelector.select(&candidates, &context).unwrap();
|
||||
assert_eq!(
|
||||
selected.candidate().account.id().as_str(),
|
||||
"acct_late_weekly"
|
||||
);
|
||||
assert_eq!(
|
||||
selected.preferred(),
|
||||
gateway_core::account::PreferredAccountSelection::Hit
|
||||
);
|
||||
|
||||
context.preferred_account = None;
|
||||
candidates[0].signals.in_flight = 3;
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
"acct_late_weekly"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_or_elapsed_selected_windows_do_not_fall_back_to_disabled_periods() {
|
||||
let mut context = context(RotationStrategy::QuotaResetPriority);
|
||||
context.policy = context
|
||||
.policy
|
||||
.with_quota_reset_window_weights(ResetWindowWeights::new([0.0, 0.0, 1.0]).unwrap());
|
||||
let mut known = candidate("acct_known_monthly", 1, None);
|
||||
known.signals.quota_reset_windows = Some(
|
||||
vec![AccountQuotaResetWindow {
|
||||
period: QuotaResetPeriod::Monthly,
|
||||
reset_at: context.now + Duration::from_secs(30 * 86_400),
|
||||
}]
|
||||
.into(),
|
||||
);
|
||||
for reset_at in [
|
||||
None,
|
||||
Some(context.now),
|
||||
Some(context.now - Duration::from_secs(1)),
|
||||
] {
|
||||
let mut missing = candidate("acct_earlier_other_period", 0, None);
|
||||
missing.signals.quota_reset_at = Some(context.now + Duration::from_secs(1));
|
||||
missing.signals.quota_reset_windows = Some(
|
||||
reset_at
|
||||
.into_iter()
|
||||
.map(|reset_at| AccountQuotaResetWindow {
|
||||
period: QuotaResetPeriod::Monthly,
|
||||
reset_at,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into(),
|
||||
);
|
||||
let candidates = [missing, known.clone()];
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
"acct_known_monthly"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1228,6 +1228,7 @@ fn plan_with_protection(
|
||||
None,
|
||||
)
|
||||
.with_smart_scheduling(account_selection_policy.smart_scheduling())
|
||||
.with_quota_reset_window_weights(account_selection_policy.quota_reset_window_weights())
|
||||
.with_max_account_rotations(account_selection_policy.max_account_rotations())
|
||||
.with_openai_account_affinity(account_selection_policy.openai_account_affinity())
|
||||
.with_openai_session_affinity_ttl_hours(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -877,11 +877,15 @@ fn compiler_accepts_unlimited_default_account_concurrency() {
|
||||
|
||||
#[test]
|
||||
fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
use gateway_core::account::SmartSchedulingConfig;
|
||||
use gateway_core::account::{ResetWindowWeights, SmartSchedulingConfig};
|
||||
let configs = [
|
||||
SmartSchedulingConfig::default(),
|
||||
SmartSchedulingConfig::new([0.0, 2.0, 1.0, 0.5, 1.2, 2.3]).unwrap(),
|
||||
];
|
||||
let windows = [
|
||||
ResetWindowWeights::default(),
|
||||
ResetWindowWeights::new([0.0, 1.5, 0.0]).unwrap(),
|
||||
];
|
||||
let mut plans = Vec::new();
|
||||
for (index, config) in configs.into_iter().enumerate() {
|
||||
let facts = SnapshotFacts::new(
|
||||
@@ -889,6 +893,7 @@ fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
revision(index as u64 + 1),
|
||||
SettingsValues::new(3, "smart", BTreeMap::new(), None, None)
|
||||
.with_smart_scheduling(config)
|
||||
.with_quota_reset_window_weights(windows[index])
|
||||
.with_openai_guardian_reserved_concurrency(index as u32),
|
||||
vec![],
|
||||
vec![],
|
||||
@@ -921,6 +926,10 @@ fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
index as u32
|
||||
);
|
||||
assert_eq!(plan.account_selection_policy().smart_scheduling(), config);
|
||||
assert_eq!(
|
||||
plan.account_selection_policy().quota_reset_window_weights(),
|
||||
windows[index]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ pub struct RuntimeSettings {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
@@ -194,6 +195,7 @@ pub struct ReplaceRuntimeSettings {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
@@ -405,6 +407,7 @@ impl From<RuntimeSettings> for ReplaceRuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: settings.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: settings.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: settings.smart_scheduling,
|
||||
quota_reset_window_weights: settings.quota_reset_window_weights,
|
||||
min_codex_desktop_version: settings.min_codex_desktop_version,
|
||||
min_codex_cli_version: settings.min_codex_cli_version,
|
||||
usage_retention_days: settings.usage_retention_days,
|
||||
|
||||
@@ -5,6 +5,7 @@ mod content;
|
||||
mod error;
|
||||
mod request;
|
||||
mod response;
|
||||
mod session;
|
||||
mod stream;
|
||||
mod thinking;
|
||||
mod tools;
|
||||
@@ -20,6 +21,7 @@ pub use request::{
|
||||
pub use response::{
|
||||
messages_to_responses_event, messages_to_responses_response, responses_to_messages_response,
|
||||
};
|
||||
pub use session::claude_session_id;
|
||||
pub use stream::{
|
||||
MessagesAccumulator, MessagesToResponsesStream, ResponsesToMessagesStream,
|
||||
message_to_stream_events,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Claude Code 明确会话身份的请求头与 metadata 解析
|
||||
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
/// 优先使用 Claude Code 会话头,再读取新旧 metadata.user_id 中的会话标识
|
||||
///
|
||||
/// 普通用户标识与对话内容不作为会话身份
|
||||
#[must_use]
|
||||
pub fn claude_session_id(body: &Map<String, Value>, header: Option<&str>) -> Option<String> {
|
||||
if let Some(session) = header.and_then(valid_session_id) {
|
||||
return Some(session.to_owned());
|
||||
}
|
||||
let user_id = body.get("metadata")?.get("user_id")?.as_str()?.trim();
|
||||
if user_id.starts_with('{') {
|
||||
let metadata: Value = serde_json::from_str(user_id).ok()?;
|
||||
return metadata
|
||||
.get("session_id")?
|
||||
.as_str()
|
||||
.and_then(valid_session_id)
|
||||
.map(str::to_owned);
|
||||
}
|
||||
let (_, session) = user_id.rsplit_once("_session_")?;
|
||||
let session = valid_session_id(session)?;
|
||||
session
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_hexdigit() || byte == b'-')
|
||||
.then(|| session.to_owned())
|
||||
}
|
||||
|
||||
fn valid_session_id(value: &str) -> Option<&str> {
|
||||
if value.chars().any(char::is_control) {
|
||||
return None;
|
||||
}
|
||||
let value = value.trim();
|
||||
(!value.is_empty() && value.len() <= 256).then_some(value)
|
||||
}
|
||||
@@ -3,5 +3,6 @@
|
||||
mod capabilities;
|
||||
mod request;
|
||||
mod response;
|
||||
mod session;
|
||||
mod stream;
|
||||
mod usage;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
//! Claude Code 会话来源优先级、格式兼容与无效身份边界
|
||||
|
||||
use gateway_protocol::anthropic::claude_session_id;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn explicit_claude_sessions_follow_header_then_metadata() {
|
||||
let session = "12345678-1234-1234-1234-123456789abc";
|
||||
for (header, user_id, expected) in [
|
||||
(
|
||||
Some(" header-session "),
|
||||
json!("{\"session_id\":\"body-session\"}"),
|
||||
Some("header-session"),
|
||||
),
|
||||
(
|
||||
None,
|
||||
json!("{\"device_id\":\"device\",\"session_id\":\"body-session\"}"),
|
||||
Some("body-session"),
|
||||
),
|
||||
(
|
||||
None,
|
||||
json!(format!("user_device_account__session_{session}")),
|
||||
Some(session),
|
||||
),
|
||||
(
|
||||
Some(""),
|
||||
json!(format!("user_{}_session_{session}", "x".repeat(300))),
|
||||
Some(session),
|
||||
),
|
||||
(None, json!("ordinary-user-id"), None),
|
||||
(None, json!("{\"device_id\":\"device\"}"), None),
|
||||
(None, json!("{invalid}"), None),
|
||||
(None, json!({"session_id":"not-a-string"}), None),
|
||||
(None, json!("user_device_session_"), None),
|
||||
(None, json!("user_device_session_not-a-uuid"), None),
|
||||
] {
|
||||
let body =
|
||||
json!({"metadata":{"user_id":user_id},"messages":[{"role":"user","content":"hello"}]});
|
||||
assert_eq!(
|
||||
claude_session_id(body.as_object().unwrap(), header).as_deref(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_session_signals_do_not_bind_users_or_message_content() {
|
||||
for invalid in [String::new(), "x".repeat(257), "bad\nidentity".to_owned()] {
|
||||
let body = json!({"metadata":{"user_id":json!({"session_id":invalid}).to_string()}});
|
||||
assert_eq!(
|
||||
claude_session_id(body.as_object().unwrap(), Some(&invalid)),
|
||||
None
|
||||
);
|
||||
let fallback = json!({"metadata":{"user_id":"{\"session_id\":\"valid-fallback\"}"}});
|
||||
assert_eq!(
|
||||
claude_session_id(fallback.as_object().unwrap(), Some(&invalid)).as_deref(),
|
||||
Some("valid-fallback")
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
claude_session_id(
|
||||
json!({"messages":[{"role":"user","content":"same opening"}]})
|
||||
.as_object()
|
||||
.unwrap(),
|
||||
None
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
@@ -114,6 +114,7 @@ impl SettingsStore for AdminSettingsStoreAdapter {
|
||||
"responses_max_decompressed_body_bytes".to_owned(),
|
||||
"rotation_strategy".to_owned(),
|
||||
"smart_scheduling_json".to_owned(),
|
||||
"quota_reset_window_weights_json".to_owned(),
|
||||
"min_codex_desktop_version".to_owned(),
|
||||
"min_codex_cli_version".to_owned(),
|
||||
"retention".to_owned(),
|
||||
|
||||
@@ -658,20 +658,41 @@ fn account_costs(state: &MetricState) -> StoreResult<Vec<AccountCost>> {
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn retained_account_usage_range(
|
||||
snapshot: &mut AnalyticsSnapshot<'_>,
|
||||
now: DateTime<Utc>,
|
||||
) -> StoreResult<ObservabilityRange> {
|
||||
// 只为实际存在的数据规划时间桶,超长保留期不展开空白历史或溢出日期
|
||||
let (days, earliest): (i64, DateTime<Utc>) = sqlx::query_as(
|
||||
"select usage_retention_days, least(
|
||||
(select started_at from model_requests order by started_at, id limit 1),
|
||||
(select started_at from request_analytics_facts order by started_at, id limit 1),
|
||||
(select bucket_start from request_analytics_1h where collection = 'usage_contribution' order by bucket_start limit 1),
|
||||
$1::timestamptz - interval '1 day'
|
||||
) from runtime_settings where id = 1",
|
||||
)
|
||||
.bind(now)
|
||||
.fetch_one(&mut *snapshot.tx)
|
||||
.await
|
||||
.map_err(|error| postgres_unavailable("read retained account usage range", error))?;
|
||||
let duration =
|
||||
TimeDelta::try_days(days).ok_or_else(|| invalid("invalid account usage retention"))?;
|
||||
let start = now
|
||||
.checked_sub_signed(duration)
|
||||
.unwrap_or(earliest)
|
||||
.max(earliest);
|
||||
ObservabilityRange::new(start, now)
|
||||
}
|
||||
|
||||
pub(crate) async fn account_usage_sort_values(
|
||||
snapshot: &mut AnalyticsSnapshot<'_>,
|
||||
now: DateTime<Utc>,
|
||||
) -> StoreResult<(Vec<String>, Vec<i64>, Vec<Option<DateTime<Utc>>>)> {
|
||||
let days: i32 =
|
||||
sqlx::query_scalar("select usage_retention_days::integer from runtime_settings where id=1")
|
||||
.fetch_one(&mut *snapshot.tx)
|
||||
.await
|
||||
.map_err(|error| postgres_unavailable("read account usage retention", error))?;
|
||||
let range = retained_account_usage_range(snapshot, now).await?;
|
||||
let ids: Vec<String> = sqlx::query_scalar("select id from provider_accounts")
|
||||
.fetch_all(&mut *snapshot.tx)
|
||||
.await
|
||||
.map_err(|error| postgres_unavailable("read account usage sort identities", error))?;
|
||||
let range = ObservabilityRange::new(now - TimeDelta::days(i64::from(days)), now)?;
|
||||
let mut totals = BTreeMap::<String, MetricState>::new();
|
||||
for ((account, _), state) in snapshot.account_states(range, &ids, false).await? {
|
||||
totals.entry(account).or_default().merge(&state)?;
|
||||
@@ -699,11 +720,7 @@ pub(crate) async fn account_group_usage(
|
||||
}
|
||||
let mut snapshot = AnalyticsSnapshot::new_for_accounts(connection).await?;
|
||||
let now = Utc::now();
|
||||
let days: i32 =
|
||||
sqlx::query_scalar("select usage_retention_days::integer from runtime_settings where id=1")
|
||||
.fetch_one(&mut *snapshot.tx)
|
||||
.await
|
||||
.map_err(|error| postgres_unavailable("read group usage retention", error))?;
|
||||
let retained = retained_account_usage_range(&mut snapshot, now).await?;
|
||||
let memberships:Vec<(String,String)>=sqlx::query_as("select account_group_id,provider_account_id from account_group_accounts where account_group_id=any($1::text[])").bind(group_ids).fetch_all(&mut *snapshot.tx).await.map_err(|error|postgres_unavailable("read group usage membership",error))?;
|
||||
let ids = memberships
|
||||
.iter()
|
||||
@@ -711,7 +728,6 @@ pub(crate) async fn account_group_usage(
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
let retained = ObservabilityRange::new(now - TimeDelta::days(i64::from(days)), now)?;
|
||||
let today = ObservabilityRange::new(
|
||||
timezone
|
||||
.day_start(now)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::{admin_store_error, postgres_unavailable};
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use gateway_admin::{
|
||||
model::retention::{RetentionPolicy, RetentionTarget},
|
||||
ports::{
|
||||
@@ -54,15 +54,26 @@ impl RetentionStore for PgRetentionRepository {
|
||||
policy: RetentionPolicy,
|
||||
limit: NonZeroU32,
|
||||
) -> AdminStoreResult<u64> {
|
||||
let oldest_sql = match target {
|
||||
RetentionTarget::ModelRequests => {
|
||||
"select completed_at from model_requests where outcome <> 'running' order by completed_at limit 1"
|
||||
}
|
||||
RetentionTarget::OpsEvents => {
|
||||
"select created_at from ops_events where model_request_id is null order by created_at limit 1"
|
||||
}
|
||||
RetentionTarget::AdminAuditEvents => {
|
||||
"select created_at from admin_audit_events order by created_at limit 1"
|
||||
}
|
||||
};
|
||||
let sql = match target {
|
||||
RetentionTarget::ModelRequests => {
|
||||
"delete from model_requests
|
||||
where ctid in (
|
||||
select ctid from model_requests
|
||||
where outcome <> 'running'
|
||||
and completed_at < $1 - ($2 * interval '1 day')
|
||||
and completed_at < $1
|
||||
order by completed_at
|
||||
limit $3
|
||||
limit $2
|
||||
for update skip locked
|
||||
)"
|
||||
}
|
||||
@@ -71,9 +82,9 @@ impl RetentionStore for PgRetentionRepository {
|
||||
where ctid in (
|
||||
select ctid from ops_events
|
||||
where model_request_id is null
|
||||
and created_at < $1 - ($2 * interval '1 day')
|
||||
and created_at < $1
|
||||
order by created_at
|
||||
limit $3
|
||||
limit $2
|
||||
for update skip locked
|
||||
)"
|
||||
}
|
||||
@@ -81,9 +92,9 @@ impl RetentionStore for PgRetentionRepository {
|
||||
"delete from admin_audit_events
|
||||
where ctid in (
|
||||
select ctid from admin_audit_events
|
||||
where created_at < $1 - ($2 * interval '1 day')
|
||||
where created_at < $1
|
||||
order by created_at
|
||||
limit $3
|
||||
limit $2
|
||||
for update skip locked
|
||||
)"
|
||||
}
|
||||
@@ -102,14 +113,25 @@ impl RetentionStore for PgRetentionRepository {
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(|source| unavailable("set retention lock timeout", source))?;
|
||||
let deleted = sqlx::query(sql)
|
||||
.bind(now)
|
||||
.bind(i64::from(policy.days(target)))
|
||||
.bind(i64::from(limit.get()))
|
||||
.execute(&mut *transaction)
|
||||
let oldest: Option<DateTime<Utc>> = sqlx::query_scalar(oldest_sql)
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map(|result| result.rows_affected())
|
||||
.map_err(|source| unavailable("delete expired records", source))?;
|
||||
.map_err(|source| unavailable("read oldest retained record", source))?;
|
||||
// 超长窗口覆盖全部现存记录时无需删除,也不向数据库传递越界日期
|
||||
let cutoff = now
|
||||
.checked_sub_signed(TimeDelta::days(i64::from(policy.days(target))))
|
||||
.filter(|cutoff| oldest.is_some_and(|oldest| oldest < *cutoff));
|
||||
let deleted = if let Some(cutoff) = cutoff {
|
||||
sqlx::query(sql)
|
||||
.bind(cutoff)
|
||||
.bind(i64::from(limit.get()))
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map(|result| result.rows_affected())
|
||||
.map_err(|source| unavailable("delete expired records", source))?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
transaction
|
||||
.commit()
|
||||
.await
|
||||
|
||||
@@ -153,7 +153,7 @@ pub(crate) async fn load_runtime_settings_from_pool(pool: &PgPool) -> StoreResul
|
||||
let row = sqlx::query_as::<_, RuntimeSettingsRow>(
|
||||
"select provider_request_profiles_json, config_revision, admin_api_key, refresh_margin_seconds, request_location_json, request_location_enabled, codex_privacy_policy_json,
|
||||
refresh_concurrency, max_concurrent_per_account,
|
||||
rotation_strategy, smart_scheduling_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
audit_retention_days, min_codex_desktop_version,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
account_error_protection_json, account_error_protection_revision,
|
||||
@@ -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,
|
||||
@@ -309,7 +333,7 @@ pub(crate) async fn load_runtime_settings_in_transaction(
|
||||
let row = sqlx::query_as::<_, RuntimeSettingsRow>(
|
||||
"select provider_request_profiles_json, config_revision, admin_api_key, refresh_margin_seconds, request_location_json, request_location_enabled, codex_privacy_policy_json,
|
||||
refresh_concurrency, max_concurrent_per_account,
|
||||
rotation_strategy, smart_scheduling_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, usage_retention_days, ops_event_retention_days,
|
||||
audit_retention_days, min_codex_desktop_version,
|
||||
min_codex_cli_version, updated_at, responses_max_decompressed_body_bytes, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours,
|
||||
account_error_protection_json, account_error_protection_revision,
|
||||
@@ -395,6 +419,7 @@ pub(crate) async fn update_runtime_settings_in_transaction(
|
||||
account_queue_timeout_seconds = $32,
|
||||
switch_account_on_queue_failure = $33,
|
||||
reset_credit_on_low_weekly_quota = $34,
|
||||
quota_reset_window_weights_json = $35,
|
||||
updated_at = now()
|
||||
where id = 1
|
||||
returning config_revision",
|
||||
@@ -441,6 +466,7 @@ pub(crate) async fn update_runtime_settings_in_transaction(
|
||||
.bind(i64::from(update.values.account_queue_timeout_seconds))
|
||||
.bind(update.values.switch_account_on_queue_failure)
|
||||
.bind(update.values.reset_credit_on_low_weekly_quota)
|
||||
.bind(sqlx::types::Json(update.values.quota_reset_window_weights))
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await
|
||||
.map_err(|source| postgres_unavailable("update runtime settings in transaction", source))?
|
||||
@@ -514,6 +540,7 @@ struct RuntimeSettingsRow {
|
||||
refresh_concurrency: i64,
|
||||
max_concurrent_per_account: i64,
|
||||
smart_scheduling_json: sqlx::types::Json<gateway_core::account::SmartSchedulingConfig>,
|
||||
quota_reset_window_weights_json: sqlx::types::Json<gateway_core::account::ResetWindowWeights>,
|
||||
rotation_strategy: String,
|
||||
codex_privacy_policy_json:
|
||||
sqlx::types::Json<gateway_core::settings::privacy::CodexPrivacyPolicy>,
|
||||
@@ -576,6 +603,7 @@ fn runtime_settings_from_row(row: RuntimeSettingsRow) -> StoreResult<RuntimeSett
|
||||
refresh_concurrency: to_u32(row.refresh_concurrency)?,
|
||||
max_concurrent_per_account: to_u32(row.max_concurrent_per_account)?,
|
||||
smart_scheduling: row.smart_scheduling_json.0,
|
||||
quota_reset_window_weights: row.quota_reset_window_weights_json.0,
|
||||
codex_privacy_policy: row.codex_privacy_policy_json.0,
|
||||
request_location_enabled: row.request_location_enabled,
|
||||
request_location: row
|
||||
|
||||
@@ -221,6 +221,7 @@ struct SnapshotSettingsRow {
|
||||
account_error_protection_json: sqlx::types::Json<gateway_core::account::AccountErrorProtection>,
|
||||
max_concurrent_per_account: i64,
|
||||
smart_scheduling_json: sqlx::types::Json<gateway_core::account::SmartSchedulingConfig>,
|
||||
quota_reset_window_weights_json: sqlx::types::Json<gateway_core::account::ResetWindowWeights>,
|
||||
rotation_strategy: String,
|
||||
model_mappings_json: sqlx::types::Json<BTreeMap<String, String>>,
|
||||
min_codex_desktop_version: Option<String>,
|
||||
@@ -248,7 +249,7 @@ async fn load_settings(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
) -> StoreResult<(Revision, SettingsValues)> {
|
||||
let row = sqlx::query_as::<_, SnapshotSettingsRow>(
|
||||
"select config_revision, account_error_protection_revision, account_error_protection_json, max_concurrent_per_account, rotation_strategy, smart_scheduling_json, model_mappings_json, min_codex_desktop_version, min_codex_cli_version, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours, request_location_json, request_location_enabled, codex_privacy_policy_json, responses_max_decompressed_body_bytes, provider_request_profiles_json, pricing_overrides_json, pricing_synced_json from runtime_settings where id = 1",
|
||||
"select config_revision, account_error_protection_revision, account_error_protection_json, max_concurrent_per_account, rotation_strategy, smart_scheduling_json, quota_reset_window_weights_json, model_mappings_json, min_codex_desktop_version, min_codex_cli_version, max_waiting_per_key, max_waiting_per_account, key_queue_timeout_seconds, account_queue_timeout_seconds, switch_account_on_queue_failure, max_retry_after_seconds, openai_guardian_reserved_concurrency, openai_account_affinity, max_account_rotations, openai_session_affinity_ttl_hours, request_location_json, request_location_enabled, codex_privacy_policy_json, responses_max_decompressed_body_bytes, provider_request_profiles_json, pricing_overrides_json, pricing_synced_json from runtime_settings where id = 1",
|
||||
)
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await
|
||||
@@ -281,6 +282,7 @@ async fn load_settings(
|
||||
.with_max_retry_after_seconds(to_u32(row.max_retry_after_seconds)?)
|
||||
.with_openai_session_affinity_ttl_hours(to_u32(row.openai_session_affinity_ttl_hours)?)
|
||||
.with_smart_scheduling(row.smart_scheduling_json.0)
|
||||
.with_quota_reset_window_weights(row.quota_reset_window_weights_json.0)
|
||||
.with_request_profiles(decode_request_profiles(
|
||||
row.provider_request_profiles_json.0,
|
||||
)?)
|
||||
|
||||
@@ -693,9 +693,16 @@ async fn group_costs_follow_current_members_and_pending_source_replacements() {
|
||||
}
|
||||
}
|
||||
assert!(ready, "group fixture must publish rollups");
|
||||
let costs = load_retained_group_costs(&groups).await;
|
||||
assert_eq!(costs[MIXED_GROUP], "3.25");
|
||||
assert_eq!(costs[EMPTY_GROUP], "0");
|
||||
for days in [31_i64, 365, i64::from(u32::MAX)] {
|
||||
sqlx::query("update runtime_settings set usage_retention_days=$1")
|
||||
.bind(days)
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let costs = load_retained_group_costs(&groups).await;
|
||||
assert_eq!(costs[MIXED_GROUP], "3.25");
|
||||
assert_eq!(costs[EMPTY_GROUP], "0");
|
||||
}
|
||||
|
||||
sqlx::query("update account_group_accounts set account_group_id=$1 where provider_account_id='acct_group_move'")
|
||||
.bind(EMPTY_GROUP).execute(&database.pool).await.unwrap();
|
||||
|
||||
@@ -105,6 +105,11 @@ async fn quota_windows_reconcile_moved_requests_and_pending_deletions_without_wa
|
||||
assert_eq!(new.usage.reasoning_output_tokens, None);
|
||||
assert_eq!(new.usage.models[0].model, "new-model");
|
||||
assert_eq!(new.usage.costs[0].amount.as_str(), "4.75");
|
||||
sqlx::query("update runtime_settings set usage_retention_days=$1")
|
||||
.bind(i64::from(u32::MAX))
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
for field in [AccountSortField::Usage, AccountSortField::LastUsedAt] {
|
||||
let ranked = store
|
||||
.list_accounts(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! 验证短保留期升级、分批清理边界与统计异步收敛期间的查询可用性
|
||||
//! 验证保留期读写、分批清理边界与统计异步收敛期间的查询可用性
|
||||
|
||||
use std::{num::NonZeroU32, time::Duration};
|
||||
|
||||
@@ -20,6 +20,66 @@ use super::{
|
||||
observability_query_budget, observability_repository,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn long_retention_round_trips_and_cleans_only_expired_requests() {
|
||||
use gateway_store::postgres::{PgRuntimeSettingsRepository, RuntimeSettingsRepository};
|
||||
|
||||
let Some(database) = TestDatabase::create("long_retention").await else {
|
||||
return;
|
||||
};
|
||||
let settings = PgRuntimeSettingsRepository::new(database.pool.clone());
|
||||
let retention = PgRetentionRepository::new(database.pool.clone());
|
||||
let now = Utc::now();
|
||||
for (id, age) in [("old", 400), ("month", 40), ("recent", 1)] {
|
||||
seed(&database.pool, id, now - ChronoDuration::days(age), &[]).await;
|
||||
}
|
||||
for (days, expected_deleted) in [(u32::MAX, 0), (3650, 0), (365, 1), (32, 1), (1, 0)] {
|
||||
let mut update = super::runtime_settings::settings_with_margin(300);
|
||||
update.values.usage_retention_days = days;
|
||||
settings.update_runtime_settings(update).await.unwrap();
|
||||
assert_eq!(
|
||||
settings
|
||||
.load_runtime_settings()
|
||||
.await
|
||||
.unwrap()
|
||||
.values
|
||||
.usage_retention_days,
|
||||
days
|
||||
);
|
||||
let policy = retention.load_policy().await.unwrap();
|
||||
assert_eq!(policy.days(RetentionTarget::ModelRequests), days);
|
||||
assert_eq!(
|
||||
retention
|
||||
.purge_batch(
|
||||
RetentionTarget::ModelRequests,
|
||||
now,
|
||||
policy,
|
||||
NonZeroU32::new(10).unwrap()
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
expected_deleted
|
||||
);
|
||||
}
|
||||
let mut invalid = super::runtime_settings::settings_with_margin(300);
|
||||
invalid.values.usage_retention_days = 0;
|
||||
assert!(settings.update_runtime_settings(invalid).await.is_err());
|
||||
assert_eq!(
|
||||
retention
|
||||
.load_policy()
|
||||
.await
|
||||
.unwrap()
|
||||
.days(RetentionTarget::ModelRequests),
|
||||
1
|
||||
);
|
||||
let remaining: Vec<String> = sqlx::query_scalar("select id from model_requests order by id")
|
||||
.fetch_all(&database.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(remaining, ["recent"]);
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn short_retention_keeps_statistics_readable_until_each_deletion_batch_is_projected() {
|
||||
use gateway_admin::model::observability::{AnalyticsState, DashboardQuery, TimeRange};
|
||||
|
||||
@@ -40,6 +40,7 @@ pub(super) fn settings_with_margin(refresh_margin_seconds: u64) -> RuntimeSettin
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
@@ -113,7 +114,7 @@ async fn privacy_policy_defaults_off_and_round_trips_in_snapshot() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn smart_settings_round_trip_publish_and_restore_database_defaults() {
|
||||
async fn scheduling_settings_round_trip_publish_and_restore_database_defaults() {
|
||||
use gateway_core::account::SmartSchedulingConfig;
|
||||
use gateway_store::postgres::{PgRuntimeSnapshotRepository, RuntimeSnapshotRepository};
|
||||
let Some(database) = TestDatabase::create("smart_config").await else {
|
||||
@@ -126,6 +127,8 @@ async fn smart_settings_round_trip_publish_and_restore_database_defaults() {
|
||||
SmartSchedulingConfig::default()
|
||||
);
|
||||
let mut update = settings_with_margin(3600);
|
||||
let windows = gateway_core::account::ResetWindowWeights::new([0.0, 1.5, 0.0]).unwrap();
|
||||
update.values.quota_reset_window_weights = windows;
|
||||
let expected = serde_json::json!({
|
||||
"loadWeight": 0.0, "quotaWeight": 2.0, "healthWeight": 1.0, "latencyWeight": 0.5, "resetWeight": 1.2, "queueWeight": 2.3,
|
||||
"resetWindowWeights": {"fiveHour":2.0,"weekly":8.0,"monthly":0.0}
|
||||
@@ -137,12 +140,17 @@ async fn smart_settings_round_trip_publish_and_restore_database_defaults() {
|
||||
serde_json::to_value(reloaded.values.smart_scheduling).unwrap(),
|
||||
expected
|
||||
);
|
||||
assert_eq!(reloaded.values.quota_reset_window_weights, windows);
|
||||
let snapshot = PgRuntimeSnapshotRepository::new(database.pool.clone())
|
||||
.load_runtime_snapshot()
|
||||
.await
|
||||
.unwrap();
|
||||
let snapshot_values = serde_json::to_value(&snapshot.settings).unwrap();
|
||||
assert_eq!(snapshot_values["smart_scheduling"], expected);
|
||||
assert_eq!(
|
||||
snapshot_values["quota_reset_window_weights"],
|
||||
serde_json::to_value(windows).unwrap()
|
||||
);
|
||||
assert!(snapshot.config_revision > before.config_revision);
|
||||
sqlx::query("update runtime_settings set smart_scheduling_json = default where id = 1")
|
||||
.execute(&database.pool)
|
||||
@@ -1015,6 +1023,13 @@ async fn request_profile_projection_is_revision_consistent_and_includes_key_over
|
||||
};
|
||||
let repository = PgRuntimeSettingsRepository::new(database.pool.clone());
|
||||
let provider = ProviderKind::new("xai").unwrap();
|
||||
assert!(
|
||||
repository
|
||||
.load_request_profile(&provider)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
let document = |marker: &str| {
|
||||
OpaqueProviderData::new(
|
||||
serde_json::json!({"marker":marker})
|
||||
@@ -1056,6 +1071,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)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! 逻辑会话账号绑定与线程传输隔离键的单向派生
|
||||
|
||||
use gateway_core::operation::RawJsonPayload;
|
||||
use gateway_core::operation::{ProtocolPayload, RawJsonPayload};
|
||||
use gateway_core::policy::ClientApiKeyId;
|
||||
use gateway_core::provider_ports::{ProviderSessionAffinityKey, ProviderSessionAlias};
|
||||
use serde_json::{Map, Value};
|
||||
@@ -148,6 +148,38 @@ pub(crate) fn derive_codex_session_affinity(
|
||||
.map(|affinity| affinity.with_follow_only(request.client_account_follow_only))
|
||||
}
|
||||
|
||||
/// Messages 的会话只参与账号绑定,不解释为 OpenAI 轮次或原生续接身份
|
||||
pub(crate) fn derive_messages_session_affinity(
|
||||
payload: &ProtocolPayload,
|
||||
headers: &[gateway_core::engine::middleware::MiddlewareHeader],
|
||||
client_api_key_id: &ClientApiKeyId,
|
||||
) -> Option<CodexSessionAffinity> {
|
||||
if payload.protocol() != gateway_protocol::anthropic::ANTHROPIC_PROTOCOL {
|
||||
return None;
|
||||
}
|
||||
let passthrough = crate::transport::request::decode_passthrough_headers(payload.context());
|
||||
let header = headers
|
||||
.iter()
|
||||
.find(|header| {
|
||||
header
|
||||
.name()
|
||||
.eq_ignore_ascii_case("x-claude-code-session-id")
|
||||
})
|
||||
.map(|header| std::str::from_utf8(header.value()).ok())
|
||||
.unwrap_or_else(|| {
|
||||
passthrough
|
||||
.get("x-claude-code-session-id")
|
||||
.and_then(|value| value.to_str().ok())
|
||||
});
|
||||
let session = gateway_protocol::anthropic::claude_session_id(payload.body(), header)?;
|
||||
session_affinity(
|
||||
"claude-session",
|
||||
&session,
|
||||
Some(&session),
|
||||
client_api_key_id,
|
||||
)
|
||||
}
|
||||
|
||||
/// 仅供选号使用。缓存键与输入正文均不作为会话、轮次或传输身份。
|
||||
pub(crate) fn derive_cache_preference(
|
||||
request: &CodexResponsesRequest,
|
||||
|
||||
@@ -15,6 +15,7 @@ use gateway_core::account::{
|
||||
};
|
||||
use gateway_core::provider_ports::{
|
||||
ProviderCatalogCacheKey, ProviderCatalogCachePort, ProviderCatalogScope,
|
||||
ProviderRuntimePolicyPort,
|
||||
};
|
||||
use gateway_core::routing::{
|
||||
FrozenAccountScope, ModelPresentation, ProviderCatalogGeneration, ProviderKind,
|
||||
@@ -243,6 +244,8 @@ pub enum CodexCredentialCatalogError {
|
||||
InvalidEtag,
|
||||
#[error("Codex model catalog changed during refresh")]
|
||||
ConcurrentUpdate,
|
||||
#[error("Codex model catalog client profile is unavailable")]
|
||||
Profile(#[source] gateway_core::error::ErrorSource),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -273,6 +276,7 @@ pub struct CodexCredentialCatalogService {
|
||||
http: reqwest::Client,
|
||||
base_url: String,
|
||||
account_catalog_cache: Arc<dyn ProviderCatalogCachePort>,
|
||||
runtime_policy: Arc<dyn ProviderRuntimePolicyPort>,
|
||||
cache: Arc<RwLock<CatalogCacheState>>,
|
||||
refresh_notification: Arc<Notify>,
|
||||
refresh_lock: Arc<tokio::sync::Mutex<()>>,
|
||||
@@ -286,6 +290,7 @@ impl CodexCredentialCatalogService {
|
||||
http: reqwest::Client,
|
||||
base_url: String,
|
||||
account_catalog_cache: Arc<dyn ProviderCatalogCachePort>,
|
||||
runtime_policy: Arc<dyn ProviderRuntimePolicyPort>,
|
||||
) -> Self {
|
||||
Self {
|
||||
repository,
|
||||
@@ -293,6 +298,7 @@ impl CodexCredentialCatalogService {
|
||||
http,
|
||||
base_url,
|
||||
account_catalog_cache,
|
||||
runtime_policy,
|
||||
cache: Arc::new(RwLock::new(CatalogCacheState::default())),
|
||||
refresh_notification: Arc::new(Notify::new()),
|
||||
refresh_lock: Arc::new(tokio::sync::Mutex::new(())),
|
||||
@@ -300,6 +306,21 @@ impl CodexCredentialCatalogService {
|
||||
}
|
||||
}
|
||||
|
||||
/// 每轮发现冻结一次全局画像,使目录的版本参数与请求头使用同一身份
|
||||
async fn discovery_client(&self) -> Result<CodexBackendClient, CodexCredentialCatalogError> {
|
||||
let profile = self
|
||||
.profile
|
||||
.resolve_global_request_profile(self.runtime_policy.as_ref())
|
||||
.await
|
||||
.map_err(CodexCredentialCatalogError::Profile)?;
|
||||
let client = CodexBackendClient::new(
|
||||
self.http.clone(),
|
||||
self.base_url.clone(),
|
||||
self.profile.clone(),
|
||||
);
|
||||
Ok(client.with_request_profile(profile))
|
||||
}
|
||||
|
||||
/// 原生条目取自后台发现的账号目录;用当前账号版本和冻结的 Key 政策双重过滤
|
||||
pub async fn client_model_catalog(
|
||||
&self,
|
||||
@@ -500,11 +521,7 @@ impl CodexCredentialCatalogService {
|
||||
.filter(|account| account.provider().as_str() == "openai")
|
||||
.ok_or(CodexCredentialCatalogError::NoEligibleCredential)?;
|
||||
let scope = CodexCatalogScope::for_account(&account);
|
||||
let client = CodexBackendClient::new(
|
||||
self.http.clone(),
|
||||
self.base_url.clone(),
|
||||
self.profile.clone(),
|
||||
);
|
||||
let client = self.discovery_client().await?;
|
||||
let revision = self.state_revision()?;
|
||||
let (catalog, entry) = self
|
||||
.fetch_observation(&client, &account, &scope, None)
|
||||
@@ -573,11 +590,7 @@ impl CodexCredentialCatalogService {
|
||||
&self,
|
||||
account: &ProviderAccount,
|
||||
) -> Result<(Vec<CodexCatalogModel>, SystemTime), CodexCredentialCatalogError> {
|
||||
let client = CodexBackendClient::new(
|
||||
self.http.clone(),
|
||||
self.base_url.clone(),
|
||||
self.profile.clone(),
|
||||
);
|
||||
let client = self.discovery_client().await?;
|
||||
let fetched = self.fetch_account_models(&client, account).await?;
|
||||
if fetched.models.is_empty() {
|
||||
return Err(CodexCredentialCatalogError::NoEligibleCredential);
|
||||
|
||||
@@ -289,11 +289,7 @@ impl CodexCredentialCatalogService {
|
||||
.cached()?
|
||||
.ok_or(CodexCredentialCatalogError::NoEligibleCredential);
|
||||
}
|
||||
let client = CodexBackendClient::new(
|
||||
self.http.clone(),
|
||||
self.base_url.clone(),
|
||||
self.profile.clone(),
|
||||
);
|
||||
let client = self.discovery_client().await?;
|
||||
let mut pace = tokio::time::interval(REFRESH_SPACING);
|
||||
pace.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||
let starts = stream::unfold(pace, |mut clock| async move {
|
||||
|
||||
@@ -22,7 +22,8 @@ pub(crate) use affinity::{
|
||||
CodexSessionAffinity, derive_cache_preference, derive_codex_cyber_policy_session_key,
|
||||
derive_codex_endpoint_session_affinity, derive_codex_session_affinity,
|
||||
derive_codex_transport_key, derive_endpoint_affinity_with_headers,
|
||||
derive_live_session_affinity, derive_previous_response_id_hash, derive_turn_alias,
|
||||
derive_live_session_affinity, derive_messages_session_affinity,
|
||||
derive_previous_response_id_hash, derive_turn_alias,
|
||||
};
|
||||
pub(crate) use eligibility::CodexSelectionModel;
|
||||
pub(crate) use oauth::oauth_owner_ref;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -30,6 +30,26 @@ impl CodexProvider {
|
||||
if !matches!(source, OPENAI_PROTOCOL | ANTHROPIC_PROTOCOL) {
|
||||
return Err(invalid_request());
|
||||
}
|
||||
if !context.is_diagnostic_required_account()
|
||||
&& (source == ANTHROPIC_PROTOCOL
|
||||
|| selected.lease.authentication().api_protocol() == ApiKeyProtocol::Messages)
|
||||
{
|
||||
// attempt 改写后的身份必须在跨协议转换前复验,避免绕过已有账号绑定
|
||||
let affinity = crate::credential::derive_messages_session_affinity(
|
||||
generate.protocol_payload(),
|
||||
&headers,
|
||||
context.client_api_key_ref(),
|
||||
);
|
||||
self.selector
|
||||
.validate_translated_selection(
|
||||
&mut selected.lease,
|
||||
affinity.as_ref(),
|
||||
None,
|
||||
context.account_selection_policy(),
|
||||
)
|
||||
.await
|
||||
.map_err(map_selection_error)?;
|
||||
}
|
||||
if selected.lease.authentication().api_protocol() == ApiKeyProtocol::Messages {
|
||||
if context.continuation_attempt() == ContinuationAttempt::Native
|
||||
|| generate.provider_session_state(PROVIDER_NAME).is_some()
|
||||
@@ -124,21 +144,10 @@ impl CodexProvider {
|
||||
selected: SelectedGenerate,
|
||||
) -> Result<ProviderStream, ProviderError> {
|
||||
let SelectedGenerate {
|
||||
mut lease,
|
||||
lease,
|
||||
account_selection_wait_ms,
|
||||
..
|
||||
} = selected;
|
||||
if !context.is_diagnostic_required_account() {
|
||||
self.selector
|
||||
.validate_translated_selection(
|
||||
&mut lease,
|
||||
None,
|
||||
None,
|
||||
context.account_selection_policy(),
|
||||
)
|
||||
.await
|
||||
.map_err(map_selection_error)?;
|
||||
}
|
||||
self.messages_stream(
|
||||
body,
|
||||
source_context,
|
||||
|
||||
@@ -673,8 +673,7 @@ impl Provider for CodexProvider {
|
||||
{
|
||||
return Err(continuation_replay_required_error("scope_unavailable"));
|
||||
}
|
||||
// 其他协议必须先取得真实账号,再按固定 attempt 阶段调用转换器;选号前不能
|
||||
// 把未知正文当成 OpenAI wire 解释会话、亲和或传输字段
|
||||
// 跨协议转换必须在选号后执行;选号前只按各自协议读取明确会话身份
|
||||
let upstream = (generate.protocol_payload().protocol() == OPENAI_PROTOCOL)
|
||||
.then(|| encode_generate_request(generate, upstream_model.as_str(), None))
|
||||
.transpose()
|
||||
@@ -726,10 +725,16 @@ impl Provider for CodexProvider {
|
||||
requires_websocket,
|
||||
)
|
||||
});
|
||||
// 内容亲和只交给选号器;中间件复验、轮次关联和原生续接仍只认真实会话
|
||||
let messages_affinity = crate::credential::derive_messages_session_affinity(
|
||||
generate.protocol_payload(),
|
||||
&[],
|
||||
context.client_api_key_ref(),
|
||||
);
|
||||
// Messages 身份与内容亲和仅交给选号器,不建立 OpenAI 轮次或原生续接关联
|
||||
let account_affinity = selection_session_affinity
|
||||
.as_ref()
|
||||
.or(cache_preference.as_ref());
|
||||
.or(cache_preference.as_ref())
|
||||
.or(messages_affinity.as_ref());
|
||||
let selection_started_at = Instant::now();
|
||||
let selection = async {
|
||||
self.selector
|
||||
|
||||
@@ -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\"",
|
||||
},
|
||||
];
|
||||
@@ -1,18 +1,22 @@
|
||||
//! CLI 稳定发布来自官方 npm 包及其平台依赖,不与 Desktop 的内嵌版本混用
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::Utc;
|
||||
use futures::StreamExt as _;
|
||||
use futures::{StreamExt as _, future::BoxFuture};
|
||||
use gateway_core::provider_ports::{ProviderArtifactProfile, ProviderArtifactProfileCachePort};
|
||||
use gateway_core::routing::ProviderKind;
|
||||
use reqwest::{Client, redirect::Policy};
|
||||
use serde::Deserialize;
|
||||
use tokio::time::Instant;
|
||||
|
||||
use super::selection::{ClientKind, ClientPlatform, ClientRelease, object};
|
||||
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState};
|
||||
use super::{
|
||||
ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL, ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState,
|
||||
baseline,
|
||||
};
|
||||
|
||||
const ENDPOINT: &str = "https://registry.npmjs.org/@openai%2Fcodex/latest";
|
||||
const MAX_BYTES: usize = 256 * 1024;
|
||||
@@ -67,11 +71,21 @@ fn sequence(version: &str) -> Result<u64, CliReleaseError> {
|
||||
Ok(1 + version.major * 1_000_000 + version.minor * 1_000 + version.patch)
|
||||
}
|
||||
|
||||
pub struct CliReleaseService {
|
||||
/// 官方 npm 发布读取边界
|
||||
pub trait CliReleaseTransport: Send + Sync {
|
||||
fn fetch(&self) -> BoxFuture<'_, Result<String, CliReleaseError>>;
|
||||
}
|
||||
|
||||
struct OfficialCliReleaseTransport {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
pub struct CliReleaseService {
|
||||
transport: Arc<dyn CliReleaseTransport>,
|
||||
provider: ProviderKind,
|
||||
state: CodexWireProfileState,
|
||||
cache: Arc<dyn ProviderArtifactProfileCachePort>,
|
||||
cache_writes: Mutex<BTreeMap<String, Instant>>,
|
||||
}
|
||||
|
||||
impl CliReleaseService {
|
||||
@@ -88,12 +102,27 @@ impl CliReleaseService {
|
||||
.timeout(Duration::from_secs(30))
|
||||
.build()
|
||||
.map_err(|_| CliReleaseError::Fetch)?;
|
||||
Ok(Self {
|
||||
client,
|
||||
Ok(Self::with_transport(
|
||||
provider,
|
||||
state,
|
||||
cache,
|
||||
})
|
||||
Arc::new(OfficialCliReleaseTransport { client }),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn with_transport(
|
||||
provider: ProviderKind,
|
||||
state: CodexWireProfileState,
|
||||
cache: Arc<dyn ProviderArtifactProfileCachePort>,
|
||||
transport: Arc<dyn CliReleaseTransport>,
|
||||
) -> Self {
|
||||
Self {
|
||||
transport,
|
||||
provider,
|
||||
state,
|
||||
cache,
|
||||
cache_writes: Mutex::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn restore(&self) {
|
||||
@@ -135,7 +164,7 @@ impl CliReleaseService {
|
||||
}
|
||||
|
||||
pub async fn refresh(&self) -> Result<(), CliReleaseError> {
|
||||
let result = self.fetch().await;
|
||||
let result = self.transport.fetch().await;
|
||||
for (platform, arch, target) in TARGETS {
|
||||
let outcome = match &result {
|
||||
Ok(version) => self.publish(platform, arch, target, version).await,
|
||||
@@ -155,16 +184,29 @@ impl CliReleaseService {
|
||||
version: &str,
|
||||
) -> Result<ClientRelease, String> {
|
||||
let next = sequence(version).map_err(|error| error.to_string())?;
|
||||
if self
|
||||
.state
|
||||
.client_release(ClientKind::Cli, platform, arch)
|
||||
.is_some_and(|current| {
|
||||
sequence(¤t.codex_version).is_ok_and(|current| current > next)
|
||||
})
|
||||
{
|
||||
let current = self.state.client_release(ClientKind::Cli, platform, arch);
|
||||
if current.as_ref().is_some_and(|current| {
|
||||
sequence(¤t.codex_version).is_ok_and(|current| current > next)
|
||||
}) {
|
||||
return Err("官方 CLI 发布版本低于当前已核验版本".to_owned());
|
||||
}
|
||||
let verified_at = Utc::now();
|
||||
let unchanged = current.filter(|current| current.codex_version == version);
|
||||
if let Some(current) = &unchanged {
|
||||
let fresh = self
|
||||
.cache_writes
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.get(target)
|
||||
.is_some_and(|written| written.elapsed() < ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL);
|
||||
if fresh {
|
||||
return Ok(current.clone());
|
||||
}
|
||||
}
|
||||
// 同版探测和缓存续期保留实际核验时间,只有新版才产生新的核验时间
|
||||
let verified_at = unchanged
|
||||
.as_ref()
|
||||
.and_then(|release| release.verified_at)
|
||||
.unwrap_or_else(Utc::now);
|
||||
let payload =
|
||||
object(&serde_json::json!({"version": version})).map_err(|error| error.to_string())?;
|
||||
let profile = ProviderArtifactProfile::new(
|
||||
@@ -179,58 +221,65 @@ impl CliReleaseService {
|
||||
.replace_if_newer(profile, ARTIFACT_PROFILE_CACHE_TTL)
|
||||
.await
|
||||
{
|
||||
Ok(true) => Ok(ClientRelease {
|
||||
codex_version: version.to_owned(),
|
||||
desktop_version: None,
|
||||
desktop_build: None,
|
||||
verified_at: Some(verified_at),
|
||||
}),
|
||||
Ok(true) => {
|
||||
self.cache_writes
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.insert(target.to_owned(), Instant::now());
|
||||
Ok(ClientRelease {
|
||||
codex_version: version.to_owned(),
|
||||
desktop_version: None,
|
||||
desktop_build: None,
|
||||
verified_at: Some(verified_at),
|
||||
})
|
||||
}
|
||||
Ok(false) => Err("CLI 发布缓存已更新,等待下次同步".to_owned()),
|
||||
Err(_) => Err("CLI 发布版本缓存写入失败".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch(&self) -> Result<String, CliReleaseError> {
|
||||
let response = self
|
||||
.client
|
||||
.get(ENDPOINT)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| CliReleaseError::Fetch)?;
|
||||
if !response.status().is_success()
|
||||
|| response
|
||||
.content_length()
|
||||
.is_some_and(|length| length > MAX_BYTES as u64)
|
||||
{
|
||||
return Err(CliReleaseError::Fetch);
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
let mut stream = response.bytes_stream();
|
||||
while let Some(chunk) = stream.next().await {
|
||||
let chunk = chunk.map_err(|_| CliReleaseError::Fetch)?;
|
||||
if bytes.len().saturating_add(chunk.len()) > MAX_BYTES {
|
||||
return Err(CliReleaseError::Invalid);
|
||||
impl CliReleaseTransport for OfficialCliReleaseTransport {
|
||||
fn fetch(&self) -> BoxFuture<'_, Result<String, CliReleaseError>> {
|
||||
Box::pin(async move {
|
||||
let response = self
|
||||
.client
|
||||
.get(ENDPOINT)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| CliReleaseError::Fetch)?;
|
||||
if !response.status().is_success()
|
||||
|| response
|
||||
.content_length()
|
||||
.is_some_and(|length| length > MAX_BYTES as u64)
|
||||
{
|
||||
return Err(CliReleaseError::Fetch);
|
||||
}
|
||||
bytes.extend_from_slice(&chunk);
|
||||
}
|
||||
parse_cli_release(&bytes)
|
||||
let mut bytes = Vec::new();
|
||||
let mut stream = response.bytes_stream();
|
||||
while let Some(chunk) = stream.next().await {
|
||||
let chunk = chunk.map_err(|_| CliReleaseError::Fetch)?;
|
||||
if bytes.len().saturating_add(chunk.len()) > MAX_BYTES {
|
||||
return Err(CliReleaseError::Invalid);
|
||||
}
|
||||
bytes.extend_from_slice(&chunk);
|
||||
}
|
||||
parse_cli_release(&bytes)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -33,11 +34,12 @@ use selection::{ClientKind, ClientPlatform, ClientRelease};
|
||||
pub const CODEX_DESKTOP_APPCAST_URL: &str =
|
||||
"https://persistent.oaistatic.com/codex-app-prod/appcast.xml";
|
||||
/// 官方发布元数据检查周期
|
||||
pub const APPCAST_POLL_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
pub const APPCAST_POLL_INTERVAL: Duration = Duration::from_secs(60 * 60);
|
||||
|
||||
const APPCAST_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
const MAX_APPCAST_BYTES: usize = 1024 * 1024;
|
||||
const ARTIFACT_PROFILE_CACHE_TTL: Duration = Duration::from_secs(30 * 24 * 60 * 60);
|
||||
const ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
const ARTIFACT_PROFILE_SCHEMA_VERSION: u64 = 1;
|
||||
|
||||
/// 保留已有配置类型入口;位置规则由出口领域统一维护
|
||||
@@ -82,22 +84,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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -632,6 +635,7 @@ pub struct CodexDesktopReleaseService {
|
||||
profile: CodexWireProfileState,
|
||||
cache: CodexArtifactProfileCache,
|
||||
verified: Arc<RwLock<Option<CodexBundledReleaseProfile>>>,
|
||||
cache_written_at: Arc<RwLock<Option<tokio::time::Instant>>>,
|
||||
}
|
||||
|
||||
impl CodexDesktopReleaseService {
|
||||
@@ -666,6 +670,7 @@ impl CodexDesktopReleaseService {
|
||||
profile,
|
||||
cache,
|
||||
verified: Arc::new(RwLock::new(verified)),
|
||||
cache_written_at: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -730,10 +735,24 @@ impl CodexDesktopReleaseService {
|
||||
{
|
||||
return Err(CodexDesktopReleaseError::InconsistentRelease);
|
||||
}
|
||||
if !self.cache.replace_if_newer(&verified).await? {
|
||||
return Err(CodexDesktopReleaseError::ReleaseRollback);
|
||||
// 轻量检查只推进观察时间,缓存单独按天续期
|
||||
let renewal_due = self
|
||||
.cache_written_at
|
||||
.read()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.is_none_or(|written| {
|
||||
written.elapsed() >= ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL
|
||||
});
|
||||
if renewal_due {
|
||||
if !self.cache.replace_if_newer(&verified).await? {
|
||||
return Err(CodexDesktopReleaseError::ReleaseRollback);
|
||||
}
|
||||
*self
|
||||
.cache_written_at
|
||||
.write()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) =
|
||||
Some(tokio::time::Instant::now());
|
||||
}
|
||||
self.profile.update_bundled_release(&verified);
|
||||
return Ok(release);
|
||||
}
|
||||
}
|
||||
@@ -764,6 +783,10 @@ impl CodexDesktopReleaseService {
|
||||
.verified
|
||||
.write()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(published);
|
||||
*self
|
||||
.cache_written_at
|
||||
.write()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(tokio::time::Instant::now());
|
||||
Ok(release)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
use super::selection::{
|
||||
ClientKind, ClientPlatform, ClientProfileSelection, ClientRelease, VersionMode, object,
|
||||
};
|
||||
use super::{ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState};
|
||||
use super::{
|
||||
ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL, ARTIFACT_PROFILE_CACHE_TTL, CodexWireProfileState,
|
||||
baseline,
|
||||
};
|
||||
use chrono::Utc;
|
||||
use futures::future::BoxFuture;
|
||||
use gateway_core::provider_ports::{ProviderArtifactProfile, ProviderArtifactProfileCachePort};
|
||||
@@ -135,6 +138,7 @@ pub struct PlatformDesktopReleaseService {
|
||||
cache: Arc<dyn ProviderArtifactProfileCachePort>,
|
||||
transport: Arc<dyn DesktopArtifactTransport>,
|
||||
verified: Mutex<BTreeMap<DesktopTarget, VerifiedArtifact>>,
|
||||
cache_written_at: Mutex<BTreeMap<DesktopTarget, tokio::time::Instant>>,
|
||||
}
|
||||
impl PlatformDesktopReleaseService {
|
||||
pub fn new(
|
||||
@@ -149,6 +153,7 @@ impl PlatformDesktopReleaseService {
|
||||
cache,
|
||||
transport,
|
||||
verified: Mutex::new(baseline_artifacts()),
|
||||
cache_written_at: Mutex::default(),
|
||||
}
|
||||
}
|
||||
pub async fn restore(&self) {
|
||||
@@ -206,14 +211,32 @@ impl PlatformDesktopReleaseService {
|
||||
.cloned();
|
||||
let mut artifact = self
|
||||
.transport
|
||||
.fetch(target, previous)
|
||||
.fetch(target, previous.clone())
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
let sequence = sequence(target, &artifact.release).map_err(|error| error.to_string())?;
|
||||
if !self.accepts(target, &artifact.release) {
|
||||
return Err("官方 Desktop 发布低于当前版本或同构建版本不一致".to_owned());
|
||||
}
|
||||
let verified_at = Utc::now();
|
||||
let unchanged = previous
|
||||
.as_ref()
|
||||
.is_some_and(|previous| previous.identity == artifact.identity);
|
||||
if unchanged
|
||||
&& self
|
||||
.cache_written_at
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.get(&target)
|
||||
.is_some_and(|written| written.elapsed() < ARTIFACT_PROFILE_CACHE_RENEW_INTERVAL)
|
||||
{
|
||||
return Ok(artifact.release);
|
||||
}
|
||||
// 相同制品只续期缓存,保留实际提取版本时的核验时间
|
||||
let verified_at = if unchanged {
|
||||
artifact.release.verified_at.unwrap_or_else(Utc::now)
|
||||
} else {
|
||||
Utc::now()
|
||||
};
|
||||
// 核验时间属于缓存信封,避免同构建重复检查被判定为不同制品
|
||||
artifact.release.verified_at = None;
|
||||
let payload =
|
||||
@@ -235,6 +258,10 @@ impl PlatformDesktopReleaseService {
|
||||
Ok(false) => return Err("Desktop 发布缓存已更新,等待下次同步".to_owned()),
|
||||
Err(_) => return Err("Desktop 发布缓存写入失败".to_owned()),
|
||||
}
|
||||
self.cache_written_at
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.insert(target, tokio::time::Instant::now());
|
||||
artifact.release.verified_at = Some(verified_at);
|
||||
self.verified
|
||||
.lock()
|
||||
@@ -280,43 +307,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,
|
||||
@@ -283,7 +282,7 @@ impl ClientProfileSelection {
|
||||
verified_at: release.verified_at.unwrap_or(DateTime::UNIX_EPOCH),
|
||||
};
|
||||
if let Some(entry) = self.cli_entry {
|
||||
// 入口后缀使用同一次解析得到的 Core 版本,避免每日更新后头部与后缀混用
|
||||
// 入口后缀使用同一次解析得到的 Core 版本,避免版本更新后头部与后缀混用
|
||||
// originator 可单独覆盖;入口名与官方 clientInfo.name 的语义保持一致
|
||||
profile.exact_user_agent = Some(format!(
|
||||
"{} ({}; {})",
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
//! Messages 会话在原生与 Responses 上游的选号、隔离和最终身份复验
|
||||
|
||||
use super::*;
|
||||
use gateway_core::account::AccountAffinity;
|
||||
|
||||
const SESSION: &str = "12345678-1234-1234-1234-123456789abc";
|
||||
|
||||
fn operation(header: Option<&str>, user_id: Value) -> Operation {
|
||||
let body = json!({"model":"gpt-5.4", "max_tokens":128, "stream":true,
|
||||
"metadata":{"user_id":user_id}, "messages":[{"role":"user","content":"hello"}]});
|
||||
let mut context = Map::new();
|
||||
if let Some(header) = header {
|
||||
context.insert(
|
||||
"opaque_request_headers".to_owned(),
|
||||
json!([["x-claude-code-session-id", STANDARD.encode(header)]]),
|
||||
);
|
||||
}
|
||||
Operation::Generate(GenerateRequest::from_protocol_payload(
|
||||
ProtocolPayload::json_object("anthropic", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(context),
|
||||
))
|
||||
}
|
||||
|
||||
fn attempt(client: &str) -> AttemptContext {
|
||||
AttemptContext::new(
|
||||
RequestAttemptContext::new(
|
||||
ModelRequestId::new("req_messages_affinity").unwrap(),
|
||||
ClientApiKeyId::new(client).unwrap(),
|
||||
),
|
||||
NonZeroU32::MIN,
|
||||
SystemTime::now() + Duration::from_secs(5),
|
||||
account_policy()
|
||||
.with_openai_account_affinity(AccountAffinity::Strict)
|
||||
.with_queue(gateway_core::concurrency::ConcurrencyQueuePolicy {
|
||||
max_waiting: 1,
|
||||
timeout: Duration::from_secs(2),
|
||||
}),
|
||||
AccountAttemptContext::new(BTreeSet::new(), None, None)
|
||||
.with_account_scope(contract_account_scope()),
|
||||
None,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
}
|
||||
|
||||
async fn fixture(
|
||||
native_messages: bool,
|
||||
) -> (
|
||||
MockServer,
|
||||
Arc<CodexProvider>,
|
||||
Arc<MemorySessionAffinity>,
|
||||
Arc<TestLeaseCoordinator>,
|
||||
) {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path(if native_messages {
|
||||
"/messages"
|
||||
} else {
|
||||
"/codex/responses"
|
||||
}))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_raw(
|
||||
if native_messages {
|
||||
MESSAGES_SSE
|
||||
} else {
|
||||
CAPTURE_COMPLETED_SSE
|
||||
},
|
||||
"text/event-stream",
|
||||
))
|
||||
.mount(&server)
|
||||
.await;
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
for id in ["acct_affinity_switch_a", "acct_affinity_switch_b"] {
|
||||
if native_messages {
|
||||
store
|
||||
.seed_api_key_for_protocol(
|
||||
id,
|
||||
server.uri(),
|
||||
ResponsesTransport::Http,
|
||||
ApiKeyProtocol::Messages,
|
||||
)
|
||||
.await;
|
||||
} else {
|
||||
create_account(&store, id).await;
|
||||
store.set_oauth_transport(id, ResponsesTransport::Http);
|
||||
}
|
||||
}
|
||||
let affinity = Arc::new(MemorySessionAffinity::default());
|
||||
let leases = Arc::new(TestLeaseCoordinator::default());
|
||||
let provider = provider_with_affinity_and_base_url_and_leases(
|
||||
&store,
|
||||
affinity.clone(),
|
||||
server.uri(),
|
||||
leases.clone(),
|
||||
);
|
||||
(server, provider, affinity, leases)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn claude_sessions_keep_accounts_across_identity_formats_and_isolate_client_keys() {
|
||||
for native_messages in [false, true] {
|
||||
let (server, provider, affinity, _) = fixture(native_messages).await;
|
||||
let mut selected = Vec::new();
|
||||
let mut keys = Vec::new();
|
||||
for (client, header, user_id) in [
|
||||
(
|
||||
"key_openai_contract",
|
||||
Some(SESSION),
|
||||
json!("{\"session_id\":\"ignored-body-session\"}"),
|
||||
),
|
||||
(
|
||||
"key_openai_contract",
|
||||
None,
|
||||
json!(json!({"session_id":SESSION}).to_string()),
|
||||
),
|
||||
(
|
||||
"key_openai_contract",
|
||||
None,
|
||||
json!(format!("user_device_account__session_{SESSION}")),
|
||||
),
|
||||
("different-client", Some(SESSION), json!("ordinary-user")),
|
||||
(
|
||||
"key_openai_contract",
|
||||
Some("different-session"),
|
||||
json!("ordinary-user"),
|
||||
),
|
||||
] {
|
||||
let before = affinity.lookup_keys().len();
|
||||
let mut stream = provider
|
||||
.clone()
|
||||
.execute(
|
||||
planned_request("openai", operation(header, user_id)),
|
||||
attempt(client),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
selected.push(stream.metadata().provider_account_id().clone());
|
||||
keys.push(affinity.lookup_keys()[before].clone());
|
||||
while let Some(event) = stream.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
}
|
||||
assert!(selected[..3].iter().all(|account| account == &selected[0]));
|
||||
assert!(keys[..3].iter().all(|key| key == &keys[0]));
|
||||
assert_ne!(keys[3], keys[0]);
|
||||
assert_ne!(keys[4], keys[0]);
|
||||
assert_eq!(affinity.binding_count(), 3);
|
||||
assert!(
|
||||
affinity.alias_ttls().is_empty(),
|
||||
"Claude sessions must not create OpenAI turn aliases"
|
||||
);
|
||||
|
||||
let before = affinity.lookup_keys().len();
|
||||
drop(
|
||||
provider
|
||||
.clone()
|
||||
.execute(
|
||||
planned_request("openai", operation(None, json!("ordinary-user"))),
|
||||
attempt("key_openai_contract"),
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
affinity.lookup_keys().len(),
|
||||
before,
|
||||
"ordinary users and message text do not identify sessions"
|
||||
);
|
||||
assert_eq!(server.received_requests().await.unwrap().len(), 5);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn messages_strict_affinity_does_not_send_to_an_idle_alternate_when_bound_account_is_busy() {
|
||||
let (server, provider, affinity, leases) = fixture(false).await;
|
||||
let request = || planned_request("openai", operation(Some(SESSION), json!("user")));
|
||||
let first = provider
|
||||
.clone()
|
||||
.execute(request(), attempt("key_openai_contract"))
|
||||
.await
|
||||
.unwrap();
|
||||
let bound = first.metadata().provider_account_id().clone();
|
||||
drop(first);
|
||||
leases.busy_accounts.lock().unwrap().insert(bound.clone());
|
||||
let context = attempt("key_openai_contract");
|
||||
let cancellation = context.cancellation().clone();
|
||||
let pending = tokio::spawn(provider.execute(request(), context));
|
||||
tokio::time::timeout(Duration::from_secs(1), async {
|
||||
while leases.requests.lock().unwrap().len() < 2 {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
cancellation.cancel();
|
||||
assert!(pending.await.unwrap().is_err());
|
||||
assert!(
|
||||
leases
|
||||
.requests
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|request| request.account_id() == &bound)
|
||||
);
|
||||
assert_eq!(affinity.binding_count(), 1);
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ChangeClaudeSession;
|
||||
|
||||
impl MiddlewarePlan for ChangeClaudeSession {
|
||||
fn handle(
|
||||
&self,
|
||||
_: MiddlewareContext,
|
||||
request: MiddlewareRequest,
|
||||
next: MiddlewareNext,
|
||||
) -> BoxFuture<'static, Result<MiddlewareResponse, MiddlewareError>> {
|
||||
Box::pin(async move {
|
||||
let protocol = request.protocol().to_owned();
|
||||
let body = request.body().clone();
|
||||
let mut headers = request.headers().to_vec();
|
||||
headers.push(MiddlewareHeader::new(
|
||||
"x-claude-code-session-id",
|
||||
Bytes::from_static(b"other-session"),
|
||||
));
|
||||
next.run(request.replace_parts(protocol, headers, body, None)?)
|
||||
.await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rewritten_claude_session_cannot_bypass_another_accounts_binding() {
|
||||
for native_messages in [false, true] {
|
||||
let (server, provider, affinity, _) = fixture(native_messages).await;
|
||||
drop(
|
||||
provider
|
||||
.clone()
|
||||
.execute(
|
||||
planned_request("openai", operation(Some(SESSION), json!("user"))),
|
||||
attempt("key_openai_contract"),
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
let key = affinity.lookup_keys()[0].clone();
|
||||
affinity.seed_binding(
|
||||
&ProviderKind::new("openai").unwrap(),
|
||||
&key,
|
||||
ProviderAccountId::new("acct_affinity_switch_a").unwrap(),
|
||||
);
|
||||
drop(
|
||||
provider
|
||||
.clone()
|
||||
.execute(
|
||||
planned_request("openai", operation(Some("other-session"), json!("user"))),
|
||||
attempt("key_openai_contract"),
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
let other = affinity.lookup_keys().last().unwrap().clone();
|
||||
affinity.seed_binding(
|
||||
&ProviderKind::new("openai").unwrap(),
|
||||
&other,
|
||||
ProviderAccountId::new("acct_affinity_switch_b").unwrap(),
|
||||
);
|
||||
let result = provider
|
||||
.execute(
|
||||
planned_request("openai", operation(Some(SESSION), json!("user"))),
|
||||
context_with_middleware(
|
||||
"req_rewritten_claude_session",
|
||||
Arc::new(ChangeClaudeSession),
|
||||
FastMode::Default,
|
||||
),
|
||||
)
|
||||
.await;
|
||||
let error = result
|
||||
.err()
|
||||
.expect("conflicting final identity must fail before sending");
|
||||
assert_eq!(error.send_state(), UpstreamSendState::NotSent);
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
+2
@@ -1,5 +1,7 @@
|
||||
//! Messages 原生 wire、跨协议头隔离和本地计数合同
|
||||
|
||||
mod affinity;
|
||||
|
||||
use super::*;
|
||||
use gateway_core::event::ProviderResponseTimings;
|
||||
use gateway_core::operation::TokenCountRequest;
|
||||
@@ -1284,6 +1284,7 @@ fn provider_and_quota_with_runtime_ports(
|
||||
http.clone(),
|
||||
base_url.clone(),
|
||||
catalog_cache(),
|
||||
crate::support::runtime_policy(),
|
||||
));
|
||||
let quota = Arc::new(CodexCredentialQuotaService::new(
|
||||
store.repository(),
|
||||
@@ -6326,8 +6327,10 @@ async fn installation_seed_scopes_http_identity_and_cache_by_key_and_account() {
|
||||
.iter()
|
||||
.zip(&bodies)
|
||||
.map(|(request, body)| {
|
||||
let id = request.headers["x-codex-installation-id"].to_str().unwrap();
|
||||
assert_eq!(id, body["client_metadata"]["x-codex-installation-id"]);
|
||||
let id = body["client_metadata"]["x-codex-installation-id"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert!(!request.headers.contains_key("x-codex-installation-id"));
|
||||
assert!(uuid::Uuid::parse_str(id).is_ok());
|
||||
assert_ne!(id, KEY_INSTALLATION_SEED);
|
||||
assert_ne!(id, other_seed);
|
||||
@@ -6359,7 +6362,7 @@ async fn installation_seed_scopes_http_identity_and_cache_by_key_and_account() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_account_scoping_replaces_downstream_installation_header() {
|
||||
async fn http_account_scoping_keeps_installation_in_metadata() {
|
||||
for owner in ["acct_scope_same", "acct_scope_old"] {
|
||||
let raw = r#"{"installation_id":"client-installation","future":true}"#;
|
||||
let request = capture_scoped_http_request(
|
||||
@@ -6400,10 +6403,7 @@ async fn http_account_scoping_replaces_downstream_installation_header() {
|
||||
assert_ne!(installation_id, "client-installation");
|
||||
assert!(uuid::Uuid::parse_str(installation_id).is_ok());
|
||||
assert_eq!(body["installation_id"], installation_id);
|
||||
assert_eq!(
|
||||
captured_header_values(&request, "x-codex-installation-id"),
|
||||
vec![installation_id.as_bytes().to_vec()]
|
||||
);
|
||||
assert!(captured_header_values(&request, "x-codex-installation-id").is_empty());
|
||||
let headers = captured_header_values(&request, "x-codex-turn-metadata");
|
||||
assert_eq!(headers.len(), 1);
|
||||
for encoded in [
|
||||
@@ -6421,7 +6421,7 @@ async fn http_account_scoping_replaces_downstream_installation_header() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn websocket_account_scoping_replaces_downstream_installation_header() {
|
||||
async fn websocket_account_scoping_keeps_installation_in_metadata() {
|
||||
for owner in ["acct_scope_same", "acct_scope_old"] {
|
||||
let raw = r#"{"installation_id":"client-installation","future":true}"#;
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
@@ -6510,7 +6510,7 @@ async fn websocket_account_scoping_replaces_downstream_installation_header() {
|
||||
assert_ne!(installation_id, KEY_INSTALLATION_SEED);
|
||||
assert!(uuid::Uuid::parse_str(installation_id).is_ok());
|
||||
assert_eq!(body["installation_id"], installation_id);
|
||||
assert_eq!(header["x-codex-installation-id"], installation_id);
|
||||
assert!(!header.contains_key("x-codex-installation-id"));
|
||||
for encoded in [
|
||||
header["x-codex-turn-metadata"]
|
||||
.to_str()
|
||||
@@ -6582,10 +6582,7 @@ async fn same_account_scope_preserves_future_protocol_shapes() {
|
||||
body.pointer("/client_metadata/x-codex-installation-id")
|
||||
);
|
||||
assert!(body.pointer("/client_metadata/installation_id").is_none());
|
||||
assert_eq!(
|
||||
request.headers["x-codex-installation-id"].to_str().unwrap(),
|
||||
body["client_metadata"]["x-codex-installation-id"]
|
||||
);
|
||||
assert!(!request.headers.contains_key("x-codex-installation-id"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -1365,6 +1365,39 @@ pub(crate) fn profile(account_id: &str) -> CodexAccountProfile {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RequestProfileRuntimePolicy(
|
||||
pub(crate) std::sync::Mutex<gateway_core::account::OpaqueProviderData>,
|
||||
);
|
||||
|
||||
impl gateway_core::provider_ports::ProviderRuntimePolicyPort for RequestProfileRuntimePolicy {
|
||||
fn load_request_profile<'a>(
|
||||
&'a self,
|
||||
provider: &'a ProviderKind,
|
||||
) -> futures::future::BoxFuture<
|
||||
'a,
|
||||
Result<
|
||||
Option<gateway_core::account::OpaqueProviderData>,
|
||||
gateway_core::provider_ports::ProviderStoreError,
|
||||
>,
|
||||
> {
|
||||
assert_eq!(provider.as_str(), "openai");
|
||||
let profile = self.0.lock().unwrap().clone();
|
||||
Box::pin(async move { Ok(Some(profile)) })
|
||||
}
|
||||
|
||||
fn load_refresh_policy(
|
||||
&self,
|
||||
) -> futures::future::BoxFuture<
|
||||
'_,
|
||||
Result<
|
||||
gateway_core::provider_ports::ProviderRefreshPolicy,
|
||||
gateway_core::provider_ports::ProviderStoreError,
|
||||
>,
|
||||
> {
|
||||
Box::pin(async { crate::support::runtime_policy().load_refresh_policy().await })
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct StaticRuntimePolicy;
|
||||
|
||||
impl ProviderRuntimePolicyPort for StaticRuntimePolicy {
|
||||
|
||||
@@ -226,7 +226,6 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
("x-codex-parent-thread-id", "parent-456"),
|
||||
("x-openai-subagent", "future_codex_mode"),
|
||||
("x-openai-memgen-request", "true"),
|
||||
("x-codex-installation-id", "install-123"),
|
||||
("session-id", "cp_derived"),
|
||||
] {
|
||||
let value = &headers.iter().find(|(header, _)| header == name).unwrap().1;
|
||||
@@ -241,6 +240,7 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
}
|
||||
}
|
||||
for forbidden in [
|
||||
"x-codex-installation-id",
|
||||
"x-codex-turn-id",
|
||||
"x-openai-internal-codex-residency",
|
||||
"accept",
|
||||
@@ -303,7 +303,7 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
parent_thread_id: request.parent_thread_id.as_deref(),
|
||||
cookie_header: Some("cf_clearance=old"),
|
||||
installation_id: Some("install-1"),
|
||||
installation_id: Some("3b11bc82-c666-417d-b9ff-2a34fd040385"),
|
||||
session_id: Some("session-1"),
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
@@ -345,7 +345,7 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
let scoped_session = read_header_value(&raw_request, "session-id").unwrap();
|
||||
assert_ne!(scoped_session, "session-1");
|
||||
assert!(uuid::Uuid::parse_str(scoped_session).is_ok());
|
||||
for required in [
|
||||
let mut expected_names = vec![
|
||||
"authorization",
|
||||
"chatgpt-account-id",
|
||||
"originator",
|
||||
@@ -362,14 +362,17 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
"x-responsesapi-include-timing-metrics",
|
||||
"version",
|
||||
"x-codex-parent-thread-id",
|
||||
] {
|
||||
assert!(header_names.iter().any(|name| name == required));
|
||||
}
|
||||
assert_eq!(
|
||||
read_header_value(&raw_request, "x-codex-installation-id"),
|
||||
Some("install-1")
|
||||
);
|
||||
"x-codex-routing-hint",
|
||||
"content-encoding",
|
||||
"content-length",
|
||||
"host",
|
||||
];
|
||||
let mut header_names = header_names;
|
||||
expected_names.sort_unstable();
|
||||
header_names.sort_unstable();
|
||||
assert_eq!(header_names, expected_names);
|
||||
for forbidden in [
|
||||
"x-codex-installation-id",
|
||||
"x-codex-turn-id",
|
||||
"x-openai-internal-codex-residency",
|
||||
"sec-ch-ua",
|
||||
|
||||
@@ -64,6 +64,11 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
|
||||
..Default::default()
|
||||
};
|
||||
let frozen = automatic.resolve(&state).unwrap();
|
||||
// 从当前基线构造后继发布,升级离线版本无需重写缓存更新场景
|
||||
let mut next = semver::Version::parse(&frozen.codex_version).unwrap();
|
||||
next.patch += 1;
|
||||
let next_sequence = 1 + next.major * 1_000_000 + next.minor * 1_000 + next.patch;
|
||||
let next_version = next.to_string();
|
||||
let fixed = ClientProfileSelection {
|
||||
version_mode: VersionMode::Fixed,
|
||||
codex_version: Some(frozen.codex_version.clone()),
|
||||
@@ -77,15 +82,17 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
// 复用官方版本服务的恢复入口,模拟每日检查已核验并写入的下一份发布资料
|
||||
// 复用官方版本服务的恢复入口,模拟版本检查已核验并写入的下一份发布资料
|
||||
cache
|
||||
.replace_if_newer(
|
||||
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());
|
||||
@@ -136,3 +142,178 @@ async fn official_cli_cache_updates_entry_headers_together_and_preserves_frozen_
|
||||
automatic.resolve(&state).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
struct CliTransport {
|
||||
version: std::sync::Mutex<String>,
|
||||
}
|
||||
|
||||
impl provider_openai::transport::profile::cli_release::CliReleaseTransport for CliTransport {
|
||||
fn fetch(
|
||||
&self,
|
||||
) -> futures::future::BoxFuture<
|
||||
'_,
|
||||
Result<String, provider_openai::transport::profile::cli_release::CliReleaseError>,
|
||||
> {
|
||||
Box::pin(async move { Ok(self.version.lock().unwrap().clone()) })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CliCache {
|
||||
writes: std::sync::Mutex<std::collections::BTreeMap<String, usize>>,
|
||||
failing_target: std::sync::Mutex<Option<String>>,
|
||||
}
|
||||
|
||||
impl gateway_core::provider_ports::ProviderArtifactProfileCachePort for CliCache {
|
||||
fn replace_if_newer(
|
||||
&self,
|
||||
profile: gateway_core::provider_ports::ProviderArtifactProfile,
|
||||
_ttl: std::time::Duration,
|
||||
) -> futures::future::BoxFuture<
|
||||
'_,
|
||||
Result<bool, gateway_core::provider_ports::ProviderStoreError>,
|
||||
> {
|
||||
Box::pin(async move {
|
||||
*self
|
||||
.writes
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(profile.artifact_key().to_owned())
|
||||
.or_default() += 1;
|
||||
if self.failing_target.lock().unwrap().as_deref() == Some(profile.artifact_key()) {
|
||||
return Err(gateway_core::provider_ports::ProviderStoreError::new(
|
||||
gateway_core::provider_ports::ProviderStoreErrorKind::Unavailable,
|
||||
"write CLI fixture",
|
||||
));
|
||||
}
|
||||
Ok(true)
|
||||
})
|
||||
}
|
||||
|
||||
fn read<'a>(
|
||||
&'a self,
|
||||
_provider: &'a gateway_core::routing::ProviderKind,
|
||||
_key: &'a str,
|
||||
) -> futures::future::BoxFuture<
|
||||
'a,
|
||||
Result<
|
||||
Option<gateway_core::provider_ports::ProviderArtifactProfile>,
|
||||
gateway_core::provider_ports::ProviderStoreError,
|
||||
>,
|
||||
> {
|
||||
Box::pin(async { Ok(None) })
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn unchanged_cli_release_only_renews_cache_daily_and_preserves_verification_time() {
|
||||
use provider_openai::transport::profile::{
|
||||
CodexWireProfileState,
|
||||
cli_release::CliReleaseService,
|
||||
selection::{ClientKind, ClientPlatform},
|
||||
};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
let state = CodexWireProfileState::new(super::wire_profile());
|
||||
let cache = Arc::new(CliCache::default());
|
||||
let transport = Arc::new(CliTransport {
|
||||
version: std::sync::Mutex::new("1.0.0".to_owned()),
|
||||
});
|
||||
let service = CliReleaseService::with_transport(
|
||||
gateway_core::routing::ProviderKind::new("openai").unwrap(),
|
||||
state.clone(),
|
||||
cache.clone(),
|
||||
transport,
|
||||
);
|
||||
service.refresh().await.unwrap();
|
||||
let verified = state
|
||||
.client_release(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.unwrap();
|
||||
tokio::time::advance(Duration::from_secs(3600)).await;
|
||||
service.refresh().await.unwrap();
|
||||
assert!(
|
||||
cache
|
||||
.writes
|
||||
.lock()
|
||||
.unwrap()
|
||||
.values()
|
||||
.all(|count| *count == 1)
|
||||
);
|
||||
tokio::time::advance(Duration::from_secs(23 * 3600)).await;
|
||||
service.refresh().await.unwrap();
|
||||
assert!(
|
||||
cache
|
||||
.writes
|
||||
.lock()
|
||||
.unwrap()
|
||||
.values()
|
||||
.all(|count| *count == 2)
|
||||
);
|
||||
assert_eq!(
|
||||
state
|
||||
.client_release(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.unwrap(),
|
||||
verified
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn cli_partial_cache_failure_retries_the_same_release_without_republishing_other_targets() {
|
||||
use provider_openai::transport::profile::{
|
||||
CodexWireProfileState,
|
||||
cli_release::CliReleaseService,
|
||||
selection::{ClientKind, ClientPlatform},
|
||||
};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
let state = CodexWireProfileState::new(super::wire_profile());
|
||||
let original = state
|
||||
.client_release(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.unwrap();
|
||||
let cache = Arc::new(CliCache::default());
|
||||
*cache.failing_target.lock().unwrap() = Some("cli-linux-x64".to_owned());
|
||||
let transport = Arc::new(CliTransport {
|
||||
version: std::sync::Mutex::new("1.0.0".to_owned()),
|
||||
});
|
||||
let service = CliReleaseService::with_transport(
|
||||
gateway_core::routing::ProviderKind::new("openai").unwrap(),
|
||||
state.clone(),
|
||||
cache.clone(),
|
||||
transport,
|
||||
);
|
||||
service.refresh().await.unwrap();
|
||||
assert_eq!(
|
||||
state
|
||||
.client_release(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.unwrap(),
|
||||
original
|
||||
);
|
||||
assert!(
|
||||
state
|
||||
.client_release_status(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.1
|
||||
.is_some()
|
||||
);
|
||||
*cache.failing_target.lock().unwrap() = None;
|
||||
tokio::time::advance(Duration::from_secs(3600)).await;
|
||||
service.refresh().await.unwrap();
|
||||
assert_eq!(
|
||||
state
|
||||
.client_release(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.unwrap()
|
||||
.codex_version,
|
||||
"1.0.0"
|
||||
);
|
||||
assert!(
|
||||
state
|
||||
.client_release_status(ClientKind::Cli, ClientPlatform::Linux, "x86_64")
|
||||
.1
|
||||
.is_none()
|
||||
);
|
||||
let writes = cache.writes.lock().unwrap();
|
||||
assert_eq!(writes["cli-linux-x64"], 2);
|
||||
assert!(
|
||||
writes
|
||||
.iter()
|
||||
.filter(|(key, _)| key.as_str() != "cli-linux-x64")
|
||||
.all(|(_, count)| *count == 1)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! 客户端画像测试入口,以及 Desktop 发行解析和原子更新测试
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -75,14 +76,16 @@ impl CodexDesktopReleaseTransport for ReleaseTransport {
|
||||
#[derive(Default)]
|
||||
struct ArtifactProfiles {
|
||||
profile: Mutex<Option<ProviderArtifactProfile>>,
|
||||
fail_writes: bool,
|
||||
fail_writes: AtomicBool,
|
||||
writes: AtomicUsize,
|
||||
}
|
||||
|
||||
impl ArtifactProfiles {
|
||||
fn failing() -> Self {
|
||||
Self {
|
||||
profile: Mutex::new(None),
|
||||
fail_writes: true,
|
||||
fail_writes: AtomicBool::new(true),
|
||||
writes: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,7 +97,8 @@ impl ProviderArtifactProfileCachePort for ArtifactProfiles {
|
||||
_ttl: Duration,
|
||||
) -> BoxFuture<'_, Result<bool, ProviderStoreError>> {
|
||||
Box::pin(async move {
|
||||
if self.fail_writes {
|
||||
self.writes.fetch_add(1, Ordering::SeqCst);
|
||||
if self.fail_writes.load(Ordering::SeqCst) {
|
||||
return Err(ProviderStoreError::new(
|
||||
ProviderStoreErrorKind::Unavailable,
|
||||
"replace test artifact profile",
|
||||
@@ -272,19 +276,34 @@ async fn bundled_release_update_should_change_core_and_desktop_identity_atomical
|
||||
assert_eq!(transport.core_probes(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unchanged_verified_release_should_refresh_cache_without_downloading_core_again() {
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn unchanged_verified_release_renews_cache_daily_without_downloading_core_again() {
|
||||
let state = CodexWireProfileState::new(wire_profile());
|
||||
let latest = release("26.810.41047", "6570");
|
||||
let transport = Arc::new(ReleaseTransport::new(
|
||||
[Ok(latest.clone()), Ok(latest)],
|
||||
(0..5).map(|_| Ok(latest.clone())),
|
||||
[Ok("0.148.0-alpha.9".to_owned())],
|
||||
));
|
||||
let cache = Arc::new(ArtifactProfiles::default());
|
||||
let service = service(state, transport.clone(), cache, None);
|
||||
let service = service(state.clone(), transport.clone(), cache.clone(), None);
|
||||
|
||||
service.refresh().await.expect("first release refresh");
|
||||
let verified = state.snapshot();
|
||||
tokio::time::advance(Duration::from_secs(60 * 60)).await;
|
||||
service.refresh().await.expect("cached release refresh");
|
||||
assert_eq!(cache.writes.load(Ordering::SeqCst), 1);
|
||||
|
||||
tokio::time::advance(Duration::from_secs(23 * 60 * 60)).await;
|
||||
cache.fail_writes.store(true, Ordering::SeqCst);
|
||||
service.refresh().await.expect_err("renewal failure");
|
||||
assert_eq!(state.snapshot(), verified);
|
||||
cache.fail_writes.store(false, Ordering::SeqCst);
|
||||
service.refresh().await.expect("retry renewal");
|
||||
assert_eq!(cache.writes.load(Ordering::SeqCst), 3);
|
||||
service.refresh().await.expect("renewed cache");
|
||||
assert_eq!(cache.writes.load(Ordering::SeqCst), 3);
|
||||
assert_eq!(state.snapshot(), verified);
|
||||
assert!(service.status().snapshot().last_error.is_none());
|
||||
|
||||
assert_eq!(transport.core_probes(), 1);
|
||||
}
|
||||
@@ -329,15 +348,11 @@ async fn cache_failure_should_not_publish_a_partially_verified_profile() {
|
||||
let original = wire_profile();
|
||||
let state = CodexWireProfileState::new(original.clone());
|
||||
let transport = Arc::new(ReleaseTransport::new(
|
||||
[Ok(release("26.810.41047", "6570"))],
|
||||
[Ok("0.148.0-alpha.9".to_owned())],
|
||||
(0..2).map(|_| Ok(release("26.810.41047", "6570"))),
|
||||
(0..2).map(|_| Ok("0.148.0-alpha.9".to_owned())),
|
||||
));
|
||||
let service = service(
|
||||
state.clone(),
|
||||
transport,
|
||||
Arc::new(ArtifactProfiles::failing()),
|
||||
None,
|
||||
);
|
||||
let cache = Arc::new(ArtifactProfiles::failing());
|
||||
let service = service(state.clone(), transport, cache.clone(), None);
|
||||
|
||||
service
|
||||
.refresh()
|
||||
@@ -346,6 +361,9 @@ async fn cache_failure_should_not_publish_a_partially_verified_profile() {
|
||||
|
||||
assert_eq!(state.snapshot(), original);
|
||||
assert!(service.status().snapshot().latest.is_none());
|
||||
cache.fail_writes.store(false, Ordering::SeqCst);
|
||||
service.refresh().await.expect("retry same release");
|
||||
assert_eq!(state.snapshot().desktop_build, "6570");
|
||||
}
|
||||
|
||||
/// 滞后档位按发布观察序列解析:启动种子、历次核验发布与失败观察共同构成历史。
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -14,6 +14,7 @@ use provider_openai::transport::profile::selection::{ClientKind, ClientProfileSe
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::io;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -24,7 +25,8 @@ mod xz;
|
||||
#[derive(Default)]
|
||||
struct Cache {
|
||||
profiles: Mutex<BTreeMap<String, ProviderArtifactProfile>>,
|
||||
reject: bool,
|
||||
reject: AtomicBool,
|
||||
writes: AtomicUsize,
|
||||
}
|
||||
impl ProviderArtifactProfileCachePort for Cache {
|
||||
fn read<'a>(
|
||||
@@ -40,7 +42,8 @@ impl ProviderArtifactProfileCachePort for Cache {
|
||||
_ttl: Duration,
|
||||
) -> BoxFuture<'_, Result<bool, ProviderStoreError>> {
|
||||
Box::pin(async move {
|
||||
if self.reject {
|
||||
self.writes.fetch_add(1, Ordering::Relaxed);
|
||||
if self.reject.load(Ordering::Relaxed) {
|
||||
return Ok(false);
|
||||
}
|
||||
self.profiles
|
||||
@@ -63,14 +66,26 @@ 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 };
|
||||
if round == 3 {
|
||||
return Ok(previous);
|
||||
}
|
||||
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())
|
||||
})
|
||||
}
|
||||
@@ -127,22 +142,85 @@ async fn releases_are_independent_atomic_cached_and_preserved_on_failures_or_rol
|
||||
.await;
|
||||
assert_eq!(selection.resolve(&restored).unwrap(), frozen);
|
||||
}
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn unchanged_artifacts_renew_daily_and_keep_verification_time() {
|
||||
let state = CodexWireProfileState::new(super::wire_profile());
|
||||
let cache = Arc::new(Cache::default());
|
||||
let transport = Arc::new(Transport::default());
|
||||
*transport.round.lock().unwrap() = 3;
|
||||
let service = service(state.clone(), cache.clone(), transport);
|
||||
let target = TARGETS[0];
|
||||
let original = state.client_release(ClientKind::Desktop, target.platform, target.arch);
|
||||
service.refresh().await;
|
||||
assert_eq!(cache.writes.load(Ordering::Relaxed), 4);
|
||||
tokio::time::advance(Duration::from_secs(60 * 60)).await;
|
||||
service.refresh().await;
|
||||
assert_eq!(cache.writes.load(Ordering::Relaxed), 4);
|
||||
for day in 1..=31 {
|
||||
tokio::time::advance(Duration::from_secs(24 * 60 * 60)).await;
|
||||
service.refresh().await;
|
||||
assert_eq!(cache.writes.load(Ordering::Relaxed), 4 * (day + 1));
|
||||
assert_eq!(
|
||||
state.client_release(ClientKind::Desktop, target.platform, target.arch),
|
||||
original
|
||||
);
|
||||
}
|
||||
cache.reject.store(true, Ordering::Relaxed);
|
||||
tokio::time::advance(Duration::from_secs(24 * 60 * 60)).await;
|
||||
service.refresh().await;
|
||||
assert!(
|
||||
state
|
||||
.client_release_status(ClientKind::Desktop, target.platform, target.arch)
|
||||
.1
|
||||
.is_some()
|
||||
);
|
||||
cache.reject.store(false, Ordering::Relaxed);
|
||||
tokio::time::advance(Duration::from_secs(60 * 60)).await;
|
||||
service.refresh().await;
|
||||
assert_eq!(cache.writes.load(Ordering::Relaxed), 4 * 34);
|
||||
assert!(
|
||||
state
|
||||
.client_release_status(ClientKind::Desktop, target.platform, target.arch)
|
||||
.1
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
state.client_release(ClientKind::Desktop, target.platform, target.arch),
|
||||
original
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cache_rejection_keeps_the_previous_release() {
|
||||
async fn cache_rejection_preserves_previous_release_and_retries_same_artifact() {
|
||||
let state = CodexWireProfileState::new(super::wire_profile());
|
||||
let before = state.client_release(ClientKind::Desktop, TARGETS[0].platform, TARGETS[0].arch);
|
||||
service(
|
||||
state.clone(),
|
||||
Arc::new(Cache {
|
||||
reject: true,
|
||||
..Default::default()
|
||||
}),
|
||||
Arc::new(Transport::default()),
|
||||
)
|
||||
.refresh()
|
||||
.await;
|
||||
let cache = Arc::new(Cache {
|
||||
reject: AtomicBool::new(true),
|
||||
..Default::default()
|
||||
});
|
||||
let transport = Arc::new(Transport::default());
|
||||
let service = service(state.clone(), cache.clone(), transport);
|
||||
service.refresh().await;
|
||||
assert_eq!(
|
||||
state.client_release(ClientKind::Desktop, TARGETS[0].platform, TARGETS[0].arch),
|
||||
before
|
||||
);
|
||||
cache.reject.store(false, Ordering::Relaxed);
|
||||
service.refresh().await;
|
||||
assert_eq!(cache.writes.load(Ordering::Relaxed), 8);
|
||||
for target in TARGETS {
|
||||
assert_eq!(
|
||||
state
|
||||
.client_release(ClientKind::Desktop, target.platform, target.arch)
|
||||
.unwrap()
|
||||
.codex_version,
|
||||
"0.200.0"
|
||||
);
|
||||
assert!(
|
||||
state
|
||||
.client_release_status(ClientKind::Desktop, target.platform, target.arch)
|
||||
.1
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,5 @@ 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
|
||||
223589308df852154004385b13c91a03714114fb9472f00472099bcf2c131cb3 0043_quota_reset_window_weights.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
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
-- 两种调度策略分别保存窗口偏好,全零沿用最早重置优先
|
||||
alter table runtime_settings
|
||||
add column quota_reset_window_weights_json jsonb not null
|
||||
default '{"fiveHour":0,"weekly":0,"monthly":0}'::jsonb;
|
||||
@@ -385,6 +385,14 @@ HTTP 在数据库迁移和初始化完成后才监听。迁移期间启动日志
|
||||
不要把未脱敏的 `docker compose config` 或 `docker inspect` 输出上传到工单;它们会包含
|
||||
PostgreSQL/Redis 启动密码。日常校验使用 `config --quiet`
|
||||
|
||||
### 安装管理端应用
|
||||
|
||||
使用 Chrome、Edge 等支持 PWA 的浏览器访问管理端,可通过地址栏或浏览器菜单安装应用,安装后以独立窗口打开站点首页,沿用现有登录流程
|
||||
|
||||
在支持自定义标题栏的桌面浏览器中,可用窗口右上角的按钮显示或隐藏系统标题栏。隐藏后仍可拖动顶部空白区域移动窗口
|
||||
|
||||
安装入口要求 HTTPS,或本机的 `http://localhost`、`http://127.0.0.1`;普通 HTTP 局域网地址不满足条件。入口是否显示还取决于浏览器、访问交互及是否已安装。应用需要联网访问网关,不提供离线使用
|
||||
|
||||
### 启动后的运行设置
|
||||
|
||||
在管理端修改以下设置,保存后对新请求生效,重启不会覆盖已保存的选择:
|
||||
|
||||
+40
-13
@@ -354,6 +354,11 @@ Responses 上游使用 `$.input`,Messages 上游使用 `$.messages`
|
||||
`stream` 缺省或 `false` 返回 JSON,`true` 返回 SSE。每轮由客户端携带所需的完整上下文。Claude Code 用 `--resume` 恢复本地会话后,仍通过 `messages` 发送上下文;网关不缓存对话正文用于下一轮。
|
||||
Messages 没有服务端历史续接或 WebSocket 入口
|
||||
|
||||
Messages 生成请求通过 OpenAI Provider 选号时,优先使用 `x-claude-code-session-id` 识别 Claude Code 会话,
|
||||
缺少有效头部时读取 `metadata.user_id` JSON 字符串中的 `session_id` 或旧格式 `_session_<id>` 后缀。
|
||||
会话标识最长 256 字节,不接受控制字符;普通用户标识和对话内容不用于绑定账号。
|
||||
同一 Client Key 下的会话沿用[账号亲和与换号](#账号亲和与换号)设置,转向 Responses 上游时也适用
|
||||
|
||||
```bash
|
||||
curl https://gateway.example.com/v1/messages \
|
||||
-H "x-api-key: ${CPR_CLIENT_KEY}" \
|
||||
@@ -500,7 +505,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 +526,12 @@ Responses 明确换号时清理 `traceparent`、`tracestate`、`sentry-trace`、
|
||||
`x-openai-fedramp`,同时清理正文、`client_metadata`、turn metadata 中的同名字段及
|
||||
`ws_request_header_*` 副本。同账号及归属未知时保留原值,业务输入和工具结果保持原始内容
|
||||
|
||||
会话、线程、窗口及父子线程引用在出站时按本次安装身份稳定映射为 UUID;
|
||||
默认使用账号安装身份;Key 配置种子时,每个 Key × 账号组合拥有独立的安装身份,换账号会改变映射。HTTP 请求头、正文 metadata、WS 副本及
|
||||
已识别的 Desktop 自动 Git 上下文使用一致映射。
|
||||
会话、线程及父子线程引用在出站时按本次安装身份稳定映射。UUIDv7 输入保留原始毫秒时间戳和版本,
|
||||
随机部分按安装身份改写;其他标识映射为 UUIDv4 形状,不补造创建时间。
|
||||
官方格式的窗口 ID 保留 `{映射后的 thread_id}:{原窗口序号}` 结构,`context_window_id` 作为独立标识处理;
|
||||
非官方格式的窗口 ID 按普通标识映射。
|
||||
默认使用账号安装身份;Key 配置种子时,每个 Key × 账号组合拥有独立的安装身份,换账号会改变映射。
|
||||
HTTP 请求头、正文 metadata、WS 副本及已识别的 Desktop 自动 Git 上下文使用一致映射,UUIDv7 的创建时间仍可见。
|
||||
轮次及父子轮次引用保留客户端原值,包括输入项的 `internal_chat_message_metadata_passthrough.turn_id`、
|
||||
请求 metadata 中的 `turn_id`、`parent_turn_id`、`root_turn_id`,以及 `x-client-turn-id`、
|
||||
Images 的 `x-codex-image-turn-id` 和 Desktop 自动 Git 上下文的 `clientUserMessageId`。
|
||||
@@ -1498,7 +1508,7 @@ OAuth 等待回调期间不持有保护;提交仍拒绝已删除或连接配
|
||||
|
||||
### 账号连接测试 SSE
|
||||
|
||||
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,不参与普通账号轮换。成功流沿用
|
||||
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,采用当前全局客户端身份,不参与普通账号轮换。成功流沿用
|
||||
`test_start`、`request`、`content`、`test_complete` 事件;失败事件为:
|
||||
|
||||
```json
|
||||
@@ -2155,7 +2165,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
| [并发与排队](#并发与排队) | `maxConcurrentPerAccount`、`maxWaitingPerKey`、`maxWaitingPerAccount`、`openaiGuardianReservedConcurrency`、`keyQueueTimeoutSeconds`、`accountQueueTimeoutSeconds`、`switchAccountOnQueueFailure` |
|
||||
| [上游重试等待](#容量拒绝与重试) | `maxRetryAfterSeconds` |
|
||||
| [亲和与换号](#账号亲和与换号) | `openaiAccountAffinity`、`openaiSessionAffinityTtlHours`、`maxAccountRotations` |
|
||||
| [调度策略](#智能调度) | `rotationStrategy`、`smartScheduling` |
|
||||
| [调度策略](#智能调度) | `rotationStrategy`、`smartScheduling`、`quotaResetWindowWeights` |
|
||||
| [请求解压](#请求解压上限) | `responsesMaxDecompressedBodyBytes` |
|
||||
| [客户端门禁](#1-鉴权与公共约定) | `minCodexDesktopVersion`、`minCodexCliVersion` |
|
||||
| [历史保留](#历史保留) | `usageRetentionDays`、`opsEventRetentionDays`、`auditRetentionDays` |
|
||||
@@ -2165,7 +2175,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
|
||||
### 历史保留
|
||||
|
||||
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,支持 1~31 的整数天数。
|
||||
`usageRetentionDays` 控制请求明细及其用量统计的保留天数,默认 31,须为正整数,不设业务上限。
|
||||
`opsEventRetentionDays` 与 `auditRetentionDays` 分别控制独立运维事件和管理审计,默认 30、90,均须为正整数
|
||||
|
||||
清理任务每轮读取已保存设置,按完成时间分批删除过期请求及关联事件,不删除运行中的请求;有界单轮结束后等待一分钟再检查。
|
||||
@@ -2186,6 +2196,7 @@ HTTP 返回 `429`,`error.code` 为 `key_daily_budget_exceeded` 或 `key_weekly
|
||||
0% 或缺少活跃证据时允许一次轻量预热,不通过倒计时差值判断活跃,也不额外回查额度。
|
||||
因此已激活但显示 0% 的账号也可能收到预热请求。
|
||||
不存在的本地时刻跳过,重复时刻只执行较早一次;执行进度跨重启保留,时钟回拨不补跑已领取时刻之前的时段。
|
||||
预热每轮采用当前 OpenAI 全局客户端身份,不使用单个 Key 的覆盖。
|
||||
只有收到响应成功终态才记为预热成功;预热不计入客户端业务用量
|
||||
|
||||
### 请求位置
|
||||
@@ -2293,7 +2304,7 @@ Guardian 以 `subagent_kind` 或 `client_metadata.x-openai-subagent` 值 `guardi
|
||||
|
||||
### 账号亲和与换号
|
||||
|
||||
`openaiAccountAffinity` 控制 Codex 账号亲和,默认 `strict`(严格):
|
||||
`openaiAccountAffinity` 控制 OpenAI Provider 的账号亲和,包括带明确 Claude Code 会话身份的 [Messages 请求](#messages-请求),默认 `strict`(严格):
|
||||
|
||||
- `relaxed`(宽松):会话内请求直接按 `rotationStrategy` 选号,不优先主账号
|
||||
- `preferred`(优先)优先使用会话主账号,短时满载、限流或模型不匹配时临时分流,后备按同一会话的稳定顺序参与既有调度
|
||||
@@ -2354,13 +2365,26 @@ Chat Completions 和 Responses 通过原生 OpenAI Provider 选号时,缺少
|
||||
缺少必填系数、系数为 `null`、未知字段及无效数值均返回 `422`,不写入配置。读取设置额外返回只读
|
||||
`smartSchedulingDefaults`,内容为上述默认对象,用于恢复默认与自定义状态比较,不可提交到更新接口
|
||||
|
||||
`resetWeight` 控制重置分在总评分中的比重。`resetWindowWeights` 中的 `fiveHour`、`weekly`、`monthly` 分别表示 5 小时、周和月的相对系数。三项均为 0~10、最多一位小数。默认均为 0,旧配置省略整个字段时也取 0;全部为 0 时不计重置分。设为 `{"fiveHour":0,"weekly":1,"monthly":0}` 可只按周重置评分。只计算账号通用窗口,未知、缺失或已过期的周期贡献为零;独立的 `quota_reset_priority` 策略仍按最早有效重置时间排序
|
||||
`resetWeight` 控制重置分在总评分中的比重。`resetWindowWeights` 中的 `fiveHour`、`weekly`、`monthly` 分别表示 5 小时、周和月的相对系数。三项均为 0~10、最多一位小数。默认均为 0,旧配置省略整个字段时也取 0;全部为 0 时不计重置分。设为 `{"fiveHour":0,"weekly":1,"monthly":0}` 可只按周重置评分。只计算账号通用窗口,未知、缺失或已过期的周期贡献为零
|
||||
|
||||
`queueWeight` 只在没有可立即使用的账号、需要选择等待队列时参与评分,越大越偏向等待人数少的账号。
|
||||
设为 `0` 时沿用最短队列规则;启用后结合其余五项评分选择队列。队列人数限于当前进程,只有开启账号排队才会生效,
|
||||
不影响已入队请求的位置、队内 FIFO、容量上限或等待超时。两项默认均为 `0`
|
||||
|
||||
没有可用亲和时,在最高可用权重层内按配置评分。原生续写账号绑定仍是硬约束。
|
||||
额度调度 `quota_reset_priority` 使用独立的必填完整对象 `quotaResetWindowWeights`,与智能调度分别保存:
|
||||
|
||||
```json
|
||||
{ "fiveHour": 0.0, "weekly": 0.0, "monthly": 0.0 }
|
||||
```
|
||||
|
||||
三项范围均为 `0~10`、最多一位小数,全部为 `0` 时按账号最早有效重置时间排序。
|
||||
任一项大于 `0` 时,只比较所选窗口的加权重置分,严格优先最高分,不混入其他评分维度,也不轮换近似同分账号。
|
||||
设为 `{"fiveHour":0,"weekly":1,"monthly":0}` 即可优先周重置更早的账号;只有一项非零时,调大该系数不会改变排序。
|
||||
缺失或已过期的所选窗口不加分,不回退到未启用窗口。同分时依次比较并发占用、上次使用时间和账号 ID。
|
||||
读取设置额外返回只读 `quotaResetWindowWeightsDefaults`,内容为上述全零对象,更新时不可提交该只读字段
|
||||
|
||||
两种策略均在没有可用亲和时,于最高可用权重层内选择账号,继续遵守账号资格、额度和并发限制。
|
||||
原生续写账号绑定仍是硬约束。
|
||||
配置随运行设置原子保存和发布,新请求使用新值,已开始请求及其重试沿用原快照,无需重启
|
||||
|
||||
### 模型定价
|
||||
@@ -2425,7 +2449,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" }`。
|
||||
@@ -2461,13 +2486,14 @@ TUI 默认标识为 `codex-tui`,Exec 为 `codex_exec`,入口后缀使用同
|
||||
因此 Desktop 预设保持默认即可;CLI 预设建议配置真实值,多 Key 可通过各自的画像覆盖差异化
|
||||
|
||||
`versionLag` 让 `latest` 模式跟随官方发布但滞后 N 个已观察版本采用,省略时采用最新版本。
|
||||
档位基于网关观察到的发布序列解析:启动种子与历次 24 小时检查的核验版本按时间倒序构成历史,
|
||||
档位基于网关观察到的发布序列解析:启动种子与历次检查的核验版本按时间倒序构成历史,
|
||||
取第 N 项;序列不足时回退最旧已核验版本,不会因此解析失败。历史仅保留在进程内,
|
||||
重启后从启动种子与制品缓存重新积累
|
||||
|
||||
六套预设均支持自动更新:macOS Desktop 支持 arm64,Windows/Linux Desktop 及三套 CLI 支持 arm64、x86_64。
|
||||
预设接口的 `automaticAvailable`、`reason` 表示当前组合的可用性;自定义架构可能使自动解析不可用。
|
||||
每 24 小时后台检查官方稳定发布,失败保留同组合上次有效版本;固定值不受后台更新影响。
|
||||
启动时立即检查官方稳定发布,之后每轮检查完成等待 1 小时再检查。
|
||||
版本变化后提取并更新画像,失败保留同组合上次有效版本,下一轮重试;固定值不受后台更新影响。
|
||||
Desktop 的应用版本、Core 和构建号来自同一平台、架构的官方制品:macOS ZIP、Windows MSIX、Linux DEB。
|
||||
Windows/Linux 通过 ETag 检查更新,未变化时复用已核验版本;CLI 依据官方 npm 稳定标签和对应平台依赖
|
||||
|
||||
@@ -2478,6 +2504,7 @@ Windows/Linux 通过 ETag 检查更新,未变化时复用已核验版本;CLI
|
||||
完整自定义配置不携带官方制品核验时间。
|
||||
客户端画像配置控制应用层请求字段,不切换操作系统的 TLS 实现。默认 HTTP 使用 native TLS,
|
||||
WebSocket 使用 rustls;配置自定义 CA 时 HTTP 也使用 rustls。TLS 指纹需按实际部署平台与传输路径核验。
|
||||
版本未变化时只更新 `checkedAt`,保留 `verifiedAt`;缓存续期不改写核验时间。
|
||||
未完成本次启动检查时 `checkedAt` 为 `null`。非法或当前不可用的选择返回 `400`,保存失败不提交其他修改
|
||||
|
||||
Responses 的自动 Desktop Git 上下文与请求头使用同一画像快照:已有 `appServerVersion` 同步为 Core 版本,
|
||||
|
||||
+15
-3
@@ -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`
|
||||
|
||||
@@ -673,9 +678,10 @@ xAI 按套餐读取有效期内的目录缓存。两者在没有对应目录时
|
||||
### 智能调度
|
||||
|
||||
Core 定义、校验并统一拥有评分规则,内置选号与诊断复用同一函数;Store 随运行设置和配置 revision 原子保存,
|
||||
快照编译器将其纳入 `AccountSelectionPolicy`。近似最优容差随立即选号的有效系数总和缩放
|
||||
快照编译器将其纳入 `AccountSelectionPolicy`。智能调度混合各项评分,近似最优容差随立即选号的有效系数总和缩放
|
||||
|
||||
- 额度重置按 5 小时、周、月分别评分。Provider 复用已有额度快照的周期分类,只投影账号通用窗口。每个周期取最近的未来重置,以周期的七分之一为尺度 `H`,得分为 `H / (H + 剩余秒数)`。三个周期按所配系数取加权平均,缺失、未知或过期不加分,分母不随账号变化;三项默认为 0,此时重置不参与评分及容差计算
|
||||
- 额度重置按 5 小时、周、月分别评分。Provider 复用已有额度快照的周期分类,只投影账号通用窗口。每个周期取最近的未来重置,以周期的七分之一为尺度 `H`,得分为 `H / (H + 剩余秒数)`。三个周期按所配系数取加权平均,缺失、未知或过期不加分,分母不随账号变化。两种调度策略复用这一规则,分别保存窗口系数
|
||||
- 智能调度的窗口系数全为 0 时,重置不参与评分及容差计算。额度调度只按独立窗口系数比较重置分,严格选择最高分,不参与近似同分轮换;全零时沿用最早有效重置时间排序。同分继续按并发占用、上次使用时间和账号 ID 决定,资格、权重层和亲和约束保持在策略排序之前
|
||||
- 排队系数只在选择等待队列时结合其他评分和进程内实时等待人数,不影响立即选号分数与容差;系数为零时选最短队列。
|
||||
`ConcurrencyWaitQueue` 在同一锁内读取队长并入队,保持已有位置、同级 FIFO、取消回收、容量和共享等待预算;Provider 传入候选须已限定续写范围
|
||||
- 健康分按已发送尝试的结果计算账号级平滑失败率并随时间衰减;明确容量拒绝的平滑权重为 0.4,其他可计分失败和成功为 0.2
|
||||
@@ -728,6 +734,8 @@ OpenAI 固定归属请求直接读取目标账号,其余请求按冻结的账
|
||||
|
||||
Store 只保存带版本的绑定事实,Provider 在取得租约后、发送前原子认领、续期或按模式迁移;快照冲突释放租约并重新选择。
|
||||
首次认领不依赖根线程先发请求,独立后台任务也使用自身会话绑定。
|
||||
Messages 的 Claude Code 明确会话身份由 `gateway-protocol::anthropic` 解析,OpenAI Provider 在选号前按 Client Key 派生独立命名空间的亲和键,
|
||||
沿用现有绑定与调度策略;原生执行在 attempt 改写后、协议转换前复验身份,不建立 OpenAI 轮次关联或原生续接归属。
|
||||
OpenAI Provider 对无显式会话的 Responses 和 Chat Completions 入站请求,可按客户端缓存键或对话开头形成账号亲和。Messages 入站请求不按对话内容绑定账号。
|
||||
亲和键按 Client Key 隔离,与会话键使用不同命名空间,不建立轮次关联或原生续接归属。
|
||||
内容亲和存储不可用时,优先模式按普通调度继续选号,严格模式停止发送。
|
||||
@@ -1292,6 +1300,10 @@ Worker 由各 Bundle 贡献、由 Host 统一监督:
|
||||
以及账号错误保护恢复 worker(到期解除或探测恢复)、重置卡自动使用和公共 CPR 互联同步
|
||||
- Provider:credential refresh、quota/catalog 健康、账号预热和官方版本/etag 检查
|
||||
|
||||
OpenAI 客户端画像由 Provider 每小时检查发布元数据,发布标识变化后才提取对应制品版本并发布画像。
|
||||
同版本的有效制品缓存每 24 小时续期,保留 30 天;续期保留原核验时间,失败不推进成功写入游标。
|
||||
版本及缓存更新失败保留当前画像,下轮继续尝试;Host 只监督调度与取消。
|
||||
|
||||
互联同步使用账号出口的受管 HTTP 客户端,不跟随跳转;网络等待不持有数据库事务。
|
||||
运行期排程、并发预算、连接失效和观测提交见 [CPR 互联观测](#cpr-互联观测),失败遵守退避及上游 `Retry-After`
|
||||
|
||||
|
||||
@@ -7,6 +7,12 @@
|
||||
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" />
|
||||
<meta
|
||||
name="theme-color"
|
||||
media="(display-mode: standalone), (display-mode: window-controls-overlay)"
|
||||
content="#ffffff"
|
||||
/>
|
||||
<title>Codex Proxy RS</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"id": "/",
|
||||
"name": "Codex Proxy RS",
|
||||
"short_name": "Codex Proxy",
|
||||
"start_url": "/",
|
||||
"scope": "/",
|
||||
"display": "standalone",
|
||||
"display_override": ["window-controls-overlay"],
|
||||
"icons": [
|
||||
{
|
||||
"src": "/favicon.svg",
|
||||
"sizes": "any",
|
||||
"type": "image/svg+xml"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ZConfigProvider } from '@codex-proxy/ui'
|
||||
import { useDebounceFn, useDevicePixelRatio, useEventListener, useWindowSize } from '@vueuse/core'
|
||||
import { watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { RouterView } from 'vue-router'
|
||||
@@ -7,11 +8,47 @@ import { applyLocale } from '@/i18n/locale'
|
||||
import { uiLocale } from '@/i18n/ui'
|
||||
|
||||
const { locale } = useI18n({ useScope: 'global' })
|
||||
const overlay = (navigator as Navigator & { windowControlsOverlay?: { getTitlebarAreaRect: () => DOMRect } }).windowControlsOverlay
|
||||
if (overlay && matchMedia('(display-mode: standalone), (display-mode: window-controls-overlay)').matches) {
|
||||
const root = document.documentElement
|
||||
root.classList.add('pwa-window')
|
||||
const { height } = useWindowSize({ type: 'outer' })
|
||||
const { pixelRatio } = useDevicePixelRatio()
|
||||
const rememberHeight = () => {
|
||||
const viewport = window.visualViewport?.height ?? window.innerHeight
|
||||
const next = viewport - overlay.getTitlebarAreaRect().bottom
|
||||
const previous = Number.parseFloat(root.style.getPropertyValue('--app-height'))
|
||||
if (!Number.isFinite(previous) || Math.abs(next - previous) >= 1)
|
||||
root.style.setProperty('--app-height', `${next}px`)
|
||||
}
|
||||
// 调整外窗时立即交回 CSS;macOS 还原动画中,外窗和内容视口可能在不同帧才停止变化
|
||||
watch([height, pixelRatio], () => root.style.removeProperty('--app-height'))
|
||||
const settled = useDebounceFn(rememberHeight, 80)
|
||||
useEventListener(window.visualViewport ?? window, 'resize', settled)
|
||||
rememberHeight()
|
||||
}
|
||||
|
||||
watch(locale, applyLocale, { immediate: true })
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ZConfigProvider :locale="uiLocale">
|
||||
<div class="pwa-titlebar hidden bg-z-bg-container" aria-hidden="true" />
|
||||
<RouterView />
|
||||
</ZConfigProvider>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
@media (display-mode: window-controls-overlay) {
|
||||
.pwa-titlebar {
|
||||
display: block;
|
||||
position: fixed;
|
||||
z-index: 51;
|
||||
top: env(titlebar-area-y);
|
||||
left: env(titlebar-area-x);
|
||||
width: env(titlebar-area-width);
|
||||
height: env(titlebar-area-height);
|
||||
-webkit-app-region: drag;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -23,6 +23,12 @@ export type AccountAffinity = 'relaxed' | 'preferred' | 'strict'
|
||||
|
||||
export type RotationStrategy = 'smart' | 'quota_reset_priority' | 'round_robin' | 'sticky'
|
||||
|
||||
export interface ResetWindowWeights {
|
||||
fiveHour: number
|
||||
weekly: number
|
||||
monthly: number
|
||||
}
|
||||
|
||||
export interface SmartSchedulingConfig {
|
||||
loadWeight: number
|
||||
quotaWeight: number
|
||||
@@ -30,13 +36,15 @@ export interface SmartSchedulingConfig {
|
||||
latencyWeight: number
|
||||
resetWeight: number
|
||||
queueWeight: number
|
||||
resetWindowWeights: { fiveHour: number, weekly: number, monthly: number }
|
||||
resetWindowWeights: ResetWindowWeights
|
||||
}
|
||||
|
||||
export interface RuntimeSettings {
|
||||
configRevision: number
|
||||
smartScheduling: SmartSchedulingConfig
|
||||
smartSchedulingDefaults: SmartSchedulingConfig
|
||||
quotaResetWindowWeights: ResetWindowWeights
|
||||
quotaResetWindowWeightsDefaults: ResetWindowWeights
|
||||
providerRequestProfiles: ProviderRequestProfiles
|
||||
openaiClientProfile: ClientProfileSelection | null
|
||||
xaiClientProfile: XaiClientProfileSelection | null
|
||||
@@ -118,7 +126,7 @@ export function getSettings(options: RequestOptions = {}) {
|
||||
})
|
||||
}
|
||||
|
||||
type UpdateSettingsParam = Omit<RuntimeSettings, 'updatedAt' | 'updatedAtDisplay' | 'smartSchedulingDefaults' | 'openaiClientProfile' | 'xaiClientProfile' | 'providerRequestProfiles'> & {
|
||||
type UpdateSettingsParam = Omit<RuntimeSettings, 'updatedAt' | 'updatedAtDisplay' | 'smartSchedulingDefaults' | 'quotaResetWindowWeightsDefaults' | 'openaiClientProfile' | 'xaiClientProfile' | 'providerRequestProfiles'> & {
|
||||
providerRequestProfiles: ProviderRequestProfileUpdates
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"totalSlots": "Total slots",
|
||||
"freeSlots": "Available slots",
|
||||
"smart": "Smart scheduling",
|
||||
"quotaReset": "Quota reset priority",
|
||||
"quotaReset": "Quota scheduling",
|
||||
"sticky": "Sticky",
|
||||
"title": "Account scheduling",
|
||||
"description": "Capacity, concurrency and allocation strategies",
|
||||
|
||||
@@ -154,7 +154,7 @@
|
||||
"quotaGroup": "Quota",
|
||||
"queueGroup": "Queue",
|
||||
"windowMode": "Reset window weights",
|
||||
"windowHelp": "Higher weights give a window more influence on the reset score. Set a weight to 0 to ignore that window.",
|
||||
"windowHelp": "Higher weights give a window more influence on the reset score; set a weight to 0 to ignore that window",
|
||||
"fiveHour": "5 hours",
|
||||
"weekly": "Weekly",
|
||||
"monthly": "Monthly",
|
||||
@@ -177,7 +177,6 @@
|
||||
},
|
||||
"enterValueFromToUpToOne": "Please enter a value from 0 to 10, up to one decimal place",
|
||||
"description": "Adjust account selection preferences",
|
||||
"enabledHelp": "Applies when smart scheduling is enabled",
|
||||
"about": "About {label}",
|
||||
"zeroWeightHelp": "Set to 0 to exclude a factor from scoring",
|
||||
"weightsRequired": "At least one weight must be greater than 0"
|
||||
@@ -309,7 +308,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",
|
||||
@@ -343,12 +342,13 @@
|
||||
"smartSchedulingSettings": "Smart scheduling settings",
|
||||
"smartLabel": "Smart",
|
||||
"smartDescription": "Adjustable weights for load, remaining quota, health, and latency",
|
||||
"quotaResetLabel": "Soonest reset",
|
||||
"quotaResetLabel": "Quota scheduling",
|
||||
"quotaResetDescription": "Use remaining quota on accounts closest to reset",
|
||||
"roundRobinLabel": "Round robin",
|
||||
"roundRobinDescription": "Cycle through available accounts in order",
|
||||
"stickyLabel": "Sticky",
|
||||
"stickyDescription": "Reuse the most recent account until unavailable"
|
||||
"stickyDescription": "Reuse the most recent account until unavailable",
|
||||
"quotaResetPrioritySettings": "Quota scheduling settings"
|
||||
},
|
||||
"access": {
|
||||
"accessRestrictions": "Access restrictions",
|
||||
@@ -626,7 +626,7 @@
|
||||
"invalidBodyLimit": "Responses decompression limit must be a positive integer in MiB",
|
||||
"invalidQuotaThreshold": "Remaining weekly quota threshold must be an integer from 0 to 100, leave blank for no limit",
|
||||
"invalidResetLeadTime": "Reset credit lead time must be an integer from 1 to 1,440 minutes",
|
||||
"invalidRetention": "History retention must be an integer from 1 to 31 days",
|
||||
"invalidRetention": "History retention must be a positive integer",
|
||||
"invalidQueueSettings": "Queue capacity must be an integer from 0 to 1,000 and timeout from 1 to 120 seconds",
|
||||
"invalidProbeModel": "The probe model must be at most 128 characters without control characters",
|
||||
"invalidWarmupCron": "Enter a valid Cron expression",
|
||||
@@ -734,6 +734,10 @@
|
||||
"moreValues": "{values} OR {count} more",
|
||||
"messageHelp": "Contains checks the first 8 KiB of the error message, preserving complete characters, equals compares the full message, longer messages are not treated as missing",
|
||||
"limitsHelp": "Up to 32 rules, 8 conditions per rule, and 16 values per condition, all rule text and the probe model share a 64 KiB UTF-8 limit"
|
||||
},
|
||||
"resetPriority": {
|
||||
"description": "Rank by reset times and weights of the selected windows",
|
||||
"windowHelp": "Weights control each window's relative influence; 0 ignores a window, and all zeros use the earliest valid reset time"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"totalSlots": "总槽位",
|
||||
"freeSlots": "空闲槽位",
|
||||
"smart": "智能调度",
|
||||
"quotaReset": "额度重置优先",
|
||||
"quotaReset": "额度调度",
|
||||
"sticky": "粘滞",
|
||||
"title": "账号调度",
|
||||
"description": "容量、并发与分配策略",
|
||||
|
||||
@@ -154,7 +154,7 @@
|
||||
"quotaGroup": "额度",
|
||||
"queueGroup": "排队",
|
||||
"windowMode": "重置窗口系数",
|
||||
"windowHelp": "系数越大,该窗口越影响重置评分。设为 0 可忽略该窗口。",
|
||||
"windowHelp": "系数越大,该窗口越影响重置评分,设为 0 可忽略该窗口",
|
||||
"fiveHour": "5 小时",
|
||||
"weekly": "周",
|
||||
"monthly": "月",
|
||||
@@ -177,7 +177,6 @@
|
||||
},
|
||||
"enterValueFromToUpToOne": "请输入 0~10 的数值,最多一位小数",
|
||||
"description": "调整账号选择偏好",
|
||||
"enabledHelp": "仅在启用智能调度后生效",
|
||||
"about": "{label}说明",
|
||||
"zeroWeightHelp": "0 表示不参与评分",
|
||||
"weightsRequired": "至少一项系数需要大于 0"
|
||||
@@ -309,7 +308,7 @@
|
||||
"retentionDaysLabel": "历史保留天数"
|
||||
},
|
||||
"retentionDays": "保留天数",
|
||||
"enterDays": "请输入 1–31 天",
|
||||
"enterDays": "请输入保留天数",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestLocationOverride": "请求位置覆盖",
|
||||
@@ -343,12 +342,13 @@
|
||||
"smartSchedulingSettings": "智能调度设置",
|
||||
"smartLabel": "智能调度",
|
||||
"smartDescription": "综合负载、剩余额度、健康和延迟评分,支持自定义评分偏好",
|
||||
"quotaResetLabel": "额度重置优先",
|
||||
"quotaResetLabel": "额度调度",
|
||||
"quotaResetDescription": "优先选择额度窗口更快重置的账号,适合在重置前消耗剩余额度",
|
||||
"roundRobinLabel": "轮询调度",
|
||||
"roundRobinDescription": "在可用候选账号间按顺序轮转,分配结果最可预测",
|
||||
"stickyLabel": "粘性调度",
|
||||
"stickyDescription": "优先复用最近使用的账号,直到不可用后再切换"
|
||||
"stickyDescription": "优先复用最近使用的账号,直到不可用后再切换",
|
||||
"quotaResetPrioritySettings": "额度调度设置"
|
||||
},
|
||||
"access": {
|
||||
"accessRestrictions": "访问限制",
|
||||
@@ -626,7 +626,7 @@
|
||||
"invalidBodyLimit": "Responses 解压上限应为有效的正整数(MiB)",
|
||||
"invalidQuotaThreshold": "剩余周额度上限应为 0~100 的整数,留空表示不限制",
|
||||
"invalidResetLeadTime": "重置卡提前使用时间应为 1~1440 分钟的整数",
|
||||
"invalidRetention": "历史保留天数应为 1–31 的整数",
|
||||
"invalidRetention": "历史保留天数应为正整数",
|
||||
"invalidQueueSettings": "队列容量应为 0~1000 的整数,排队超时应为 1~120 秒的整数",
|
||||
"invalidProbeModel": "探测模型最多 128 个字符,不能包含控制字符",
|
||||
"invalidWarmupCron": "请输入有效的 Cron 表达式",
|
||||
@@ -734,6 +734,10 @@
|
||||
"moreValues": "{values} 或其他 {count} 项",
|
||||
"messageHelp": "包含匹配检查错误信息的前 8 KiB,保留完整字符,等值匹配比较完整信息,超长信息不会被视为字段缺失",
|
||||
"limitsHelp": "最多 32 条规则,每条 8 个条件,每个条件 16 个候选值,所有规则文本与探测模型合计不超过 64 KiB UTF-8"
|
||||
},
|
||||
"resetPriority": {
|
||||
"description": "按所选窗口的重置时间和系数排序",
|
||||
"windowHelp": "系数决定各窗口的相对占比,0 表示忽略该窗口,全部为 0 时按最近的有效重置时间排序"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"totalSlots": "總槽位",
|
||||
"freeSlots": "空閒槽位",
|
||||
"smart": "智能調度",
|
||||
"quotaReset": "額度重置優先",
|
||||
"quotaReset": "額度調度",
|
||||
"sticky": "粘滯",
|
||||
"title": "賬號調度",
|
||||
"description": "容量、併發與分配策略",
|
||||
|
||||
@@ -154,7 +154,7 @@
|
||||
"quotaGroup": "額度",
|
||||
"queueGroup": "排隊",
|
||||
"windowMode": "重置視窗係數",
|
||||
"windowHelp": "係數越大,該窗口越影響重置評分。設為 0 可忽略該窗口。",
|
||||
"windowHelp": "係數越大,該窗口越影響重置評分,設為 0 可忽略該窗口",
|
||||
"fiveHour": "5 小時",
|
||||
"weekly": "週",
|
||||
"monthly": "月",
|
||||
@@ -177,7 +177,6 @@
|
||||
},
|
||||
"enterValueFromToUpToOne": "請輸入 0~10 的數值,最多一位小數",
|
||||
"description": "調整賬號選擇偏好",
|
||||
"enabledHelp": "僅在啓用智能調度後生效",
|
||||
"about": "{label}說明",
|
||||
"zeroWeightHelp": "0 表示不參與評分",
|
||||
"weightsRequired": "至少一項係數需要大於 0"
|
||||
@@ -309,7 +308,7 @@
|
||||
"retentionDaysLabel": "歷史保留天數"
|
||||
},
|
||||
"retentionDays": "保留天數",
|
||||
"enterDays": "請輸入 1–31 天",
|
||||
"enterDays": "請輸入保留天數",
|
||||
"day": "天",
|
||||
"location": {
|
||||
"requestLocationOverride": "請求位置覆蓋",
|
||||
@@ -343,12 +342,13 @@
|
||||
"smartSchedulingSettings": "智能調度設置",
|
||||
"smartLabel": "智能調度",
|
||||
"smartDescription": "綜合負載、剩餘額度、健康和延遲評分,支持自定義評分偏好",
|
||||
"quotaResetLabel": "額度重置優先",
|
||||
"quotaResetLabel": "額度調度",
|
||||
"quotaResetDescription": "優先選擇額度窗口更快重置的賬號,適合在重置前消耗剩餘額度",
|
||||
"roundRobinLabel": "輪詢調度",
|
||||
"roundRobinDescription": "在可用候選賬號間按順序輪轉,分配結果最可預測",
|
||||
"stickyLabel": "粘性調度",
|
||||
"stickyDescription": "優先複用最近使用的賬號,直到不可用後再切換"
|
||||
"stickyDescription": "優先複用最近使用的賬號,直到不可用後再切換",
|
||||
"quotaResetPrioritySettings": "額度調度設置"
|
||||
},
|
||||
"access": {
|
||||
"accessRestrictions": "訪問限制",
|
||||
@@ -626,7 +626,7 @@
|
||||
"invalidBodyLimit": "Responses 解壓上限應爲有效的正整數(MiB)",
|
||||
"invalidQuotaThreshold": "剩餘周額度上限應爲 0~100 的整數,留空表示不限制",
|
||||
"invalidResetLeadTime": "重置卡提前使用時間應爲 1~1440 分鐘的整數",
|
||||
"invalidRetention": "歷史保留天數應爲 1–31 的整數",
|
||||
"invalidRetention": "歷史保留天數應爲正整數",
|
||||
"invalidQueueSettings": "隊列容量應爲 0~1000 的整數,排隊超時應爲 1~120 秒的整數",
|
||||
"invalidProbeModel": "探測模型最多 128 個字符,不能包含控制字符",
|
||||
"invalidWarmupCron": "請輸入有效的 Cron 表達式",
|
||||
@@ -734,6 +734,10 @@
|
||||
"moreValues": "{values} 或其他 {count} 項",
|
||||
"messageHelp": "包含匹配檢查錯誤信息的前 8 KiB,保留完整字符,等值匹配比較完整信息,超長信息不會被視爲字段缺失",
|
||||
"limitsHelp": "最多 32 條規則,每條 8 個條件,每個條件 16 個候選值,所有規則文本與探測模型合計不超過 64 KiB UTF-8"
|
||||
},
|
||||
"resetPriority": {
|
||||
"description": "按所選視窗的重置時間和係數排序",
|
||||
"windowHelp": "係數決定各視窗的相對占比,0 表示忽略該視窗,全部為 0 時按最近的有效重置時間排序"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ watch(
|
||||
<main class="relative isolate h-dvh min-w-0 flex-1 overflow-hidden">
|
||||
<!-- 让主滚动区自身绘制不透明底色,浏览器可在合成线程滚动 -->
|
||||
<ZScrollbar ref="pageScrollbarRef" class="[&>.z-scrollbar-hidden]:bg-z-bg-layout">
|
||||
<div class="flex min-h-full min-w-0 flex-col p-4 min-[961px]:p-6">
|
||||
<div class="page-slot flex min-h-full min-w-0 flex-col p-4 min-[961px]:p-6">
|
||||
<RouterView v-slot="{ Component }">
|
||||
<component :is="Component" class="min-h-0 flex-1" />
|
||||
</RouterView>
|
||||
@@ -88,7 +88,7 @@ watch(
|
||||
|
||||
<Teleport to="body">
|
||||
<Transition name="mobile-sidebar" @after-leave="restoreFocus">
|
||||
<div v-if="mobileSidebarOpen" :inert="!mobileSidebarOpen" role="presentation" class="fixed inset-0 z-50 min-[961px]:hidden" @keydown="handleKeydown">
|
||||
<div v-if="mobileSidebarOpen" :inert="!mobileSidebarOpen" role="presentation" class="fixed inset-x-0 top-(--app-titlebar-inset,0px) bottom-0 z-50 min-[961px]:hidden" @keydown="handleKeydown">
|
||||
<button
|
||||
type="button"
|
||||
tabindex="-1"
|
||||
|
||||
@@ -130,6 +130,7 @@ export const useThemeStore = defineStore(
|
||||
|
||||
function commitTheme(theme: Theme): void {
|
||||
applyTheme(document.documentElement, theme)
|
||||
document.querySelector('meta[name="theme-color"]')?.setAttribute('content', theme.tokens['color-bg-container'])
|
||||
appliedThemeSignature = themeSignature(theme)
|
||||
themeRevision.value += 1
|
||||
}
|
||||
|
||||
@@ -1,3 +1,40 @@
|
||||
@import 'tailwindcss/theme.css' layer(theme);
|
||||
@import '@codex-proxy/ui/tailwind.css';
|
||||
@import 'tailwindcss/utilities.css' layer(utilities);
|
||||
|
||||
.pwa-window {
|
||||
--app-height: calc(100dvh - env(titlebar-area-y, 0px) - env(titlebar-area-height, 0px));
|
||||
--app-titlebar-inset: max(0px, 100dvh - var(--app-height));
|
||||
}
|
||||
|
||||
.pwa-window,
|
||||
.pwa-window body {
|
||||
background-color: var(--z-color-bg-container);
|
||||
}
|
||||
|
||||
.pwa-window body {
|
||||
padding-top: var(--app-titlebar-inset);
|
||||
}
|
||||
|
||||
.pwa-window .h-dvh,
|
||||
.pwa-window .page-slot {
|
||||
height: var(--app-height);
|
||||
}
|
||||
|
||||
.pwa-window .min-h-dvh {
|
||||
min-height: var(--app-height);
|
||||
}
|
||||
|
||||
/* 满屏面板不能高于正文;矮窗仍由外层滚动到列表底部 */
|
||||
.pwa-window .page-slot > * {
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
.pwa-window .page-slot > .h-full {
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
/* 通知挂在 body,固定定位不会避开标题栏 */
|
||||
.pwa-window article[id^='z-notification-'] {
|
||||
translate: 0 var(--app-titlebar-inset);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<script setup lang="ts">
|
||||
import type { ResetWindowWeights } from '@/api'
|
||||
import { ZButton, ZDialog } from '@codex-proxy/ui'
|
||||
import { ref, shallowRef, useId, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { validSchedulingWeight } from '../utils/scheduling'
|
||||
import ResetWindowWeightsFields from './ResetWindowWeightsFields.vue'
|
||||
|
||||
const props = defineProps<{
|
||||
config: ResetWindowWeights
|
||||
defaults: ResetWindowWeights
|
||||
}>()
|
||||
const emit = defineEmits<{ confirm: [config: ResetWindowWeights] }>()
|
||||
const open = defineModel<boolean>({ required: true })
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
const formId = useId()
|
||||
const draft = ref({ fiveHour: '', weekly: '', monthly: '' })
|
||||
const submitted = shallowRef(false)
|
||||
|
||||
function replaceDraft(config: ResetWindowWeights) {
|
||||
for (const key of Object.keys(draft.value) as (keyof ResetWindowWeights)[])
|
||||
draft.value[key] = String(config[key])
|
||||
submitted.value = false
|
||||
}
|
||||
|
||||
watch(open, (value) => {
|
||||
if (value)
|
||||
replaceDraft(props.config)
|
||||
}, { immediate: true })
|
||||
|
||||
function confirm() {
|
||||
submitted.value = true
|
||||
if (!Object.values(draft.value).every(validSchedulingWeight))
|
||||
return
|
||||
emit('confirm', {
|
||||
fiveHour: Number(draft.value.fiveHour),
|
||||
weekly: Number(draft.value.weekly),
|
||||
monthly: Number(draft.value.monthly),
|
||||
})
|
||||
open.value = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ZDialog v-model="open" :title="t('settings.rotation.quotaResetPrioritySettings')" :description="t('settings.resetPriority.description')" width="36rem">
|
||||
<form :id="formId" class="grid gap-5" novalidate @submit.prevent="confirm">
|
||||
<ResetWindowWeightsFields v-model="draft" :submitted="submitted" :help="t('settings.resetPriority.windowHelp')" />
|
||||
</form>
|
||||
<template #footer>
|
||||
<ZButton class="mr-auto" @click="replaceDraft(defaults)">
|
||||
{{ t('common.actions.reset') }}
|
||||
</ZButton>
|
||||
<ZButton @click="open = false">
|
||||
{{ t('common.actions.cancel') }}
|
||||
</ZButton>
|
||||
<ZButton native-type="submit" :form="formId" type="primary">
|
||||
{{ t('common.actions.confirm') }}
|
||||
</ZButton>
|
||||
</template>
|
||||
</ZDialog>
|
||||
</template>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user