fix(openai): preserve model exclusions during catalog refresh

Use the last successful catalog for the same account, credential revision, and plan until a refresh replaces it. Apply the same exclusions in memory and in PostgreSQL while preserving explicit allowlists and accounts with no catalog.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Zyy
2026-11-13 10:16:12 +08:00
co-authored by Codex
parent 2cae28d7f4
commit 3bd042181e
8 changed files with 69 additions and 97 deletions
@@ -28,11 +28,10 @@ pub struct StoredAccountCatalog {
pub document: bytes::Bytes,
}
/// 批量选号的存储优化:只有账号自己的当前版本、有效时间和目录摘要都匹配才可跳过
/// 批量选号只按账号当前凭据和套餐的目录摘要排除候选
pub struct AccountCatalogOmissions {
pub documents: Vec<[u8; 32]>,
pub exceptions: Vec<ProviderAccountId>,
pub observed_after: std::time::SystemTime,
pub as_of: std::time::SystemTime,
}
@@ -144,8 +144,7 @@ impl PgProviderAccountRepository {
query.push(" and not exists (select 1 from provider_account_model_catalogs c
where c.account_id=provider_accounts.id and c.credential_revision=provider_accounts.credential_revision
and c.plan_type is not distinct from provider_accounts.plan_type and c.document_hash=any(")
.push_bind(hashes).push("::bytea[]) and c.observed_at > ").push_bind(DateTime::<Utc>::from(omit.observed_after))
.push(" and c.observed_at <= ").push_bind(DateTime::<Utc>::from(omit.as_of))
.push_bind(hashes).push("::bytea[]) and c.observed_at <= ").push_bind(DateTime::<Utc>::from(omit.as_of))
.push(" and not (c.account_id=any(").push_bind(exceptions).push("::text[])))");
}
query.push(" order by provider_kind, name, id");
@@ -170,7 +170,7 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
.iter()
.all(|a| !matches!(a.account_id.as_str(), "acct_1" | "acct_2" | "acct_3"))
);
// 数据库只能排除有当前版本、未过期的账号证据;显式例外仍进入选号
// 刷新到期不改变目录排除结果;凭据变化与显式例外仍进入选号
let hash: Vec<u8> = sqlx::query_scalar("select sha256($1::bytea)")
.bind(&document)
.fetch_one(&database.pool)
@@ -181,7 +181,6 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
let filter = gateway_core::account::AccountCatalogOmissions {
documents: vec![hash.try_into().unwrap()],
exceptions: vec![ProviderAccountId::new("acct_5").unwrap()],
observed_after: now - std::time::Duration::from_secs(1800),
as_of: now,
};
let ids = (1..=10000)
@@ -195,9 +194,6 @@ async fn bulk_catalog_restore_keeps_ten_thousand_owners_and_fences_late_writes()
.iter()
.map(|a| a.id().as_str())
.collect::<std::collections::BTreeSet<_>>();
assert_eq!(
ids,
std::collections::BTreeSet::from(["acct_1", "acct_4", "acct_5"])
);
assert_eq!(ids, std::collections::BTreeSet::from(["acct_1", "acct_5"]));
database.close().await;
}
@@ -32,8 +32,7 @@ use crate::transport::{CodexBackendClient, CodexCatalogModel, CodexRequestContex
const MAX_RESPONSE_ETAG_BYTES: usize = 256;
const ACCOUNT_CATALOG_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
// 调度证据覆盖默认 15 分钟刷新周期及抖动,长期未刷新时恢复上游验证
const SCHEDULING_CATALOG_MAX_AGE: Duration = Duration::from_secs(30 * 60);
const CATALOG_REFRESH_AFTER: Duration = Duration::from_secs(30 * 60);
const MAX_ACCOUNT_CATALOG_MODELS: usize = 2_048;
const MODEL_CATALOG_TIMEOUT: Duration = Duration::from_secs(15);
@@ -141,7 +140,6 @@ impl CatalogContent {
struct AccountEntry {
observed_at: SystemTime,
content: Arc<CatalogContent>,
invalidated: Arc<std::sync::atomic::AtomicBool>,
etag: Option<String>,
}
@@ -197,13 +195,7 @@ impl CodexCredentialCatalogSnapshot {
pub(super) fn excludes(&self, account: &ProviderAccount, model: &str, now: SystemTime) -> bool {
self.entries
.get(&CodexCatalogScope::for_account(account))
.is_some_and(|e| {
!e.invalidated.load(std::sync::atomic::Ordering::Relaxed)
&& now
.duration_since(e.observed_at)
.is_ok_and(|age| age < SCHEDULING_CATALOG_MAX_AGE)
&& !e.content.ids.iter().any(|id| id == model)
})
.is_some_and(|e| e.observed_at <= now && !e.content.ids.iter().any(|id| id == model))
}
pub fn account_models(&self, account: &ProviderAccount) -> Option<&[String]> {
@@ -466,14 +458,7 @@ impl CodexCredentialCatalogService {
if ids.len() < MIN_CATALOG_PUSHDOWN {
return None;
}
let cache = self.cache.read().ok()?;
let snapshot = cache.snapshot.as_ref()?.clone();
let pending = cache
.pending
.keys()
.map(|s| s.account_id.clone())
.collect::<BTreeSet<_>>();
drop(cache);
let snapshot = self.cached().ok()??;
let documents = snapshot
.sources
.iter()
@@ -483,16 +468,10 @@ impl CodexCredentialCatalogService {
if documents.is_empty() {
return None;
}
let as_of = SystemTime::now();
Some(gateway_core::account::AccountCatalogOmissions {
documents,
exceptions: ids
.iter()
.filter(|id| pending.contains(*id) || exempt(id))
.cloned()
.collect(),
observed_after: as_of.checked_sub(SCHEDULING_CATALOG_MAX_AGE)?,
as_of,
exceptions: ids.iter().filter(|id| exempt(id)).cloned().collect(),
as_of: SystemTime::now(),
})
}
@@ -582,7 +561,6 @@ impl CodexCredentialCatalogService {
observed_at,
content,
etag: observed.etag,
invalidated: Default::default(),
},
))
}
@@ -41,7 +41,6 @@ impl CodexCredentialCatalogService {
observed_at: a.observed_at,
content: content.clone(),
etag: a.etag,
invalidated: Default::default(),
},
);
}
@@ -131,11 +130,6 @@ impl CodexCredentialCatalogService {
{
return Ok(false);
}
if let Some(entry) = state.snapshot.as_ref().and_then(|s| s.entries.get(&scope)) {
entry
.invalidated
.store(true, std::sync::atomic::Ordering::Relaxed);
}
state.pending.insert(scope, etag.to_owned());
drop(state);
self.refresh_notification.notify_one();
@@ -210,15 +204,12 @@ impl CodexCredentialCatalogService {
let Some(entry) = snapshot.entries.get(scope) else {
return true;
};
if entry.etag.as_deref() == Some(tag)
&& !entry.invalidated.load(std::sync::atomic::Ordering::Relaxed)
{
return false;
}
entry
.invalidated
.store(true, std::sync::atomic::Ordering::Relaxed);
true
// 发布其他账号时保留通知,只有该账号更新的观测才能确认 ETag
entry.etag.as_deref() != Some(tag)
|| old
.as_ref()
.and_then(|s| s.entries.get(scope))
.is_some_and(|previous| previous.observed_at >= entry.observed_at)
});
}
cache.snapshot = next.clone();
@@ -289,7 +280,7 @@ impl CodexCredentialCatalogService {
pending.contains_key(scope)
|| !entries.get(scope).is_some_and(|e| {
now.duration_since(e.observed_at)
.is_ok_and(|age| age < SCHEDULING_CATALOG_MAX_AGE)
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
})
})
.collect::<Vec<_>>();
@@ -222,15 +222,23 @@ async fn account_allowlists_override_catalog_omissions_but_weight_affinity_and_p
store.set_scheduling("acct_scope_new", None, AccountWeight::new(100).unwrap());
let affinity = Arc::new(MemorySessionAffinity::default());
let leases = Arc::new(TestLeaseCoordinator::default());
let (provider, _) = provider_and_quota_with_catalog(
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
&store,
affinity.clone(),
native.uri(),
leases.clone(),
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
)
.await;
Arc::new(MemoryCooldownPort::new()),
crate::support::runtime_policy(),
);
catalog.refresh_catalogs().await.unwrap();
provider.query_model_capabilities().await.unwrap();
// 次账号目录待刷新时,仍须按各自的目录和白名单选号
assert!(
catalog
.observe_response_etag(&store.account("acct_scope_new").unwrap(), "new")
.unwrap()
);
for (model, expected) in [
("gpt-6-luna", "acct_scope_new"),
("gpt-6-astra", "acct_provider_contract"),
@@ -364,7 +372,7 @@ async fn account_allowlists_override_catalog_omissions_but_weight_affinity_and_p
}
#[tokio::test]
async fn only_a_current_account_catalog_can_exclude_a_model() {
async fn selection_uses_the_last_catalog_for_the_same_account_identity() {
use gateway_core::account::{CredentialCasOutcome, CredentialCasUpdate, ProviderAccountUpdate};
use provider_openai::credential::ResponsesTransport;
@@ -423,7 +431,16 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
.expect(if scenario == "unknown_plan" { 0 } else { 1 })
.mount(upstream)
.await;
let provider = if scenario == "expired" {
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
&store,
Arc::new(MemorySessionAffinity::default()),
native.uri(),
Arc::new(TestLeaseCoordinator::default()),
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
Arc::new(MemoryCooldownPort::new()),
crate::support::runtime_policy(),
);
if scenario == "expired" {
use gateway_core::account::AccountCatalogObservation;
let a = store.account("acct_scope_new").unwrap();
store
@@ -439,26 +456,8 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
)
.await
.unwrap();
let (provider, _, _, catalog) = provider_and_quota_with_runtime_ports(
&store,
Arc::new(MemorySessionAffinity::default()),
native.uri(),
Arc::new(TestLeaseCoordinator::default()),
u32::try_from(DEFAULT_STREAM_MAX_RETRIES).unwrap(),
Arc::new(MemoryCooldownPort::new()),
crate::support::runtime_policy(),
);
catalog.restore().await.unwrap();
// B 的失败不能清空上次成功结果,也不能继续以旧的缺失条目拒绝请求
catalog.refresh_catalogs().await.unwrap();
assert_eq!(
catalog.cached_account_models(&a).unwrap().unwrap(),
["gpt-6-luna"]
);
provider
} else {
provider_with_catalog_and_base_url(&store, native.uri()).await
};
}
catalog.refresh_catalogs().await.unwrap();
provider.query_model_capabilities().await.unwrap();
if scenario == "rotated" {
let account = store.account("acct_scope_new").unwrap();
@@ -483,21 +482,34 @@ async fn only_a_current_account_catalog_can_exclude_a_model() {
CredentialCasOutcome::Updated(_)
));
}
let result = provider
.execute(
let execute = || {
provider.clone().execute(
planned_request_for_model("openai", operation("gpt-6-astra"), "gpt-6-astra"),
context("req_unknown_catalog", CancellationToken::new()),
)
.await;
if scenario == "unknown_plan" {
// 未知套餐也有该账号亲自返回的目录,不能借套餐缺失绕过资格
};
if matches!(scenario, "unknown_plan" | "expired") {
assert_eq!(
result.err().unwrap().kind(),
ProviderErrorKind::NoEligibleAccount
execute().await.err().unwrap().kind(),
ProviderErrorKind::NoEligibleAccount,
"{scenario}"
);
continue;
if scenario == "unknown_plan" {
continue;
}
// 刷新失败保留排除结果,直到新目录确认该账号已支持模型
Mock::given(method("GET"))
.and(path("/models"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": [{"id": "gpt-6-astra"}]
})))
.with_priority(1)
.expect(1)
.mount(&api)
.await;
catalog.refresh_catalogs().await.unwrap();
}
let mut stream = result.unwrap();
let mut stream = execute().await.unwrap();
assert_eq!(
stream.metadata().provider_account_id().as_str(),
"acct_scope_new",
+3 -5
View File
@@ -1373,11 +1373,9 @@ Key 预算及其访问状态随互联默认公开,不要求开启 `shareQuota`
例如 Plus 设置 `allowlist` 并选中 luna 的实际 ID,Pro 设置 `denylist` 并选中同一 ID,即可严格分流;
Pro 使用 `all` 时也能参与 luna 调度。套餐名称不自动生成或修改规则
原生 Responses 默认排除有效目录未列出目标模型的账号。OpenAI OAuth 或 API Key 账号的白名单精确包含映射后的上游 ID 时,
允许通过该账号请求目录外模型,由上游确认是否可用。此设置只对配置它的账号生效,已有白名单同样适用。
账号禁用、Key 分组、认证类型、黑名单和模型冷却仍参与选号。xAI 账号使用有效套餐目录与账号模型政策的交集。
OpenAI 只使用该账号当前凭据和套餐对应、最近 30 分钟内同步的目录,不借用同套餐其他账号的模型。
没有有效账号目录时交由上游验证,仍执行 `modelAccess`。xAI 使用已知套餐的有效目录缓存
原生 OpenAI Responses 按账号当前凭据和套餐最近一次成功同步的目录选号。目录未列出的模型在刷新期间或刷新失败后仍被排除。
账号白名单精确包含映射后的上游 ID 时,允许该账号尝试目录外模型;当前凭据和套餐没有目录时,也可在账号模型政策允许的范围内交由上游验证。
账号禁用、Key 分组、认证类型、黑名单和模型冷却仍参与选号。xAI 按账号模型政策与有效的套餐目录筛选
原生 OpenAI Responses 上游返回 `error.code=model_not_found`,且状态为 400/403/404 或流内错误未提供状态时,
该账号与模型组合冷却 30 分钟;其他模型仍可使用该账号。仅消息提及模型不支持不触发冷却;
+4 -5
View File
@@ -658,9 +658,8 @@ OpenAI Provider 的账号资格模块统一解释请求类型、认证与套餐
资格检查复用账号持久事实,在获取租约前与冻结的模型黑白名单共同过滤候选;亲和与重试沿用同一检查
原生 Responses 在评分、亲和和租约前检查冻结的账号模型政策。OpenAI 账号白名单精确命中目标 ID 时允许上游验证,
其余 OpenAI 账号及 xAI 账号按有效目录排除未列出的模型。OpenAI 目录按来源账号、凭据版本和套餐隔离;同套餐账号可以有不同的模型。
OpenAI 只使用未满 30 分钟且尚未收到变化通知的账号目录排除候选。大池可在读取账号时按目录摘要筛选,数据库逐账号核对版本、套餐和观测时间;白名单、待刷新通知及受管适配器不参与该筛选。xAI 按套餐去重读取 TTL 目录缓存,选号不请求上游目录。
没有有效目录时保留上游验证,不从其他账号借用 OpenAI 模型资格,也不从其他套餐借用 xAI 模型资格
其余原生 OpenAI 请求按来源账号、凭据版本和套餐匹配最近一次成功的目录,排除未列出的模型。大池可在读取账号时按目录摘要筛选,白名单和受管适配器由选号时单独判断。
xAI 按套餐读取有效期内的目录缓存。两者在没有对应目录时保留上游验证,选号不请求上游目录
原生 OpenAI Responses 按[官方 Codex 0.166.0](https://github.com/openai/codex/blob/6ff1e1ca3b30fc8b914720681fcf5fe62e95f856/codex-rs/codex-api/src/error.rs#L73)的结构化 `model_not_found` 错误码与状态合同识别模型拒绝,
不从消息或任意正文推断模型权限;通过既有模型级冷却端口记录账号、凭据版本与上游模型,30 分钟内不再选用该组合。
@@ -968,8 +967,8 @@ PostgreSQL schema 由迁移目录按编号管理;冻结、升级与校验规
### 账号模型目录
OpenAI 在 PostgreSQL 保存账号最近一次成功的模型目录、凭据版本、观测时间及 ETag。启动时批量恢复,按不同正文解析一次,保留每个账号的独立归属。没有历史目录也能发布路由快照;能力读取与推理不等待上游查询。
后台只刷新目录缺失、已过刷新窗口或收到新 ETag 的账号;凭据轮换后按新版本重新发现。失败保留最后成功的结果,过期资料只用于展示,不能据此排除候选账号。
新鲜度窗口、刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护
后台刷新缺少目录、距上次成功观测已满 30 分钟或收到新 ETag 的账号。刷新期间及失败后继续使用上次成功的目录;凭据或套餐变化后不沿用旧目录。
刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护
OpenAI 从账号目录生成原生列表;先按冻结政策选择来源,再读取本地账号状态核对凭据版本。同一份正文可以复用,但不会把一个账号的模型授予同套餐的其他账号。客户端版本和 Key 身份不触发额外发现