diff --git a/backend/crates/providers/openai/src/credential/catalog/mod.rs b/backend/crates/providers/openai/src/credential/catalog/mod.rs index 2479bf8d1..665091600 100644 --- a/backend/crates/providers/openai/src/credential/catalog/mod.rs +++ b/backend/crates/providers/openai/src/credential/catalog/mod.rs @@ -33,7 +33,7 @@ use crate::transport::{CodexBackendClient, CodexCatalogModel, CodexRequestContex const MAX_RESPONSE_ETAG_BYTES: usize = 256; const ACCOUNT_CATALOG_CACHE_TTL: Duration = Duration::from_secs(5 * 60); -const CATALOG_REFRESH_AFTER: Duration = Duration::from_secs(30 * 60); +const CATALOG_REFRESH_AFTER: Duration = Duration::from_hours(1); const MAX_ACCOUNT_CATALOG_MODELS: usize = 2_048; const MODEL_CATALOG_TIMEOUT: Duration = Duration::from_secs(15); @@ -137,14 +137,31 @@ impl CatalogContent { } } -#[derive(Clone)] struct AccountEntry { observed_at: SystemTime, content: Arc, etag: Option, + // 目录返回不同版本时,触发这次查询的标记只用于去重 + trigger_etag: Option, + // 响应只确认这份目录的刷新期限,正文观测时间仍用于并发发布和持久化 + etag_confirmed_at: std::sync::Mutex>, } -type Entries = BTreeMap; +impl AccountEntry { + fn recognizes_etag(&self, etag: &str) -> bool { + self.etag.as_deref() == Some(etag) || self.trigger_etag.as_deref() == Some(etag) + } + + fn is_fresh(&self, now: SystemTime) -> bool { + now.duration_since(self.observed_at) + .is_ok_and(|age| age < CATALOG_REFRESH_AFTER) + || self.etag_confirmed_at.lock().is_ok_and(|confirmed| { + confirmed.is_some_and(|at| at.elapsed() < CATALOG_REFRESH_AFTER) + }) + } +} + +type Entries = BTreeMap>; pub struct CodexCredentialCatalogSnapshot { models: Vec, @@ -551,7 +568,7 @@ impl CodexCredentialCatalogService { revision: account.revision(), plan: account.plan_type().map(str::to_owned), observed_at, - etag: etag.clone().or(fetched.etag), + etag: fetched.etag.or_else(|| etag.clone()), }; if !self .repository @@ -577,7 +594,9 @@ impl CodexCredentialCatalogService { AccountEntry { observed_at, content, + trigger_etag: etag.filter(|tag| Some(tag) != observed.etag.as_ref()), etag: observed.etag, + etag_confirmed_at: Default::default(), }, )) } diff --git a/backend/crates/providers/openai/src/credential/catalog/refresh.rs b/backend/crates/providers/openai/src/credential/catalog/refresh.rs index ae08b840e..47367420b 100644 --- a/backend/crates/providers/openai/src/credential/catalog/refresh.rs +++ b/backend/crates/providers/openai/src/credential/catalog/refresh.rs @@ -37,11 +37,13 @@ impl CodexCredentialCatalogService { }; entries.insert( scope, - AccountEntry { + Arc::new(AccountEntry { observed_at: a.observed_at, content: content.clone(), etag: a.etag, - }, + trigger_etag: None, + etag_confirmed_at: Default::default(), + }), ); } } @@ -119,15 +121,21 @@ impl CodexCredentialCatalogService { .cache .write() .map_err(|_| CodexCredentialCatalogError::Cache)?; - if state.pending.get(&scope).map(String::as_str) == Some(etag) - || !state.pending.contains_key(&scope) - && state - .snapshot - .as_ref() - .and_then(|s| s.entries.get(&scope)) - .and_then(|e| e.etag.as_deref()) - == Some(etag) + let pending = state.pending.get(&scope); + if pending.is_some_and(|tag| tag == etag) { + return Ok(false); + } + if let Some(entry) = state.snapshot.as_ref().and_then(|s| s.entries.get(&scope)) + && entry.recognizes_etag(etag) { + // 已收到变更时,迟到的旧标记不能覆盖待刷新版本 + if pending.is_none() && entry.etag.as_deref() == Some(etag) { + let mut confirmed_at = entry + .etag_confirmed_at + .lock() + .map_err(|_| CodexCredentialCatalogError::Cache)?; + *confirmed_at = Some(tokio::time::Instant::now()); + } return Ok(false); } state.pending.insert(scope, etag.to_owned()); @@ -166,7 +174,7 @@ impl CodexCredentialCatalogService { .get(&scope) .is_none_or(|old| old.observed_at < entry.observed_at) { - entries.insert(scope, entry); + entries.insert(scope, Arc::new(entry)); } } self.publish(revision, entries)?; @@ -205,7 +213,7 @@ impl CodexCredentialCatalogService { return true; }; // 发布其他账号时保留通知,只有该账号更新的观测才能确认 ETag - entry.etag.as_deref() != Some(tag) + !entry.recognizes_etag(tag) || old .as_ref() .and_then(|s| s.entries.get(scope)) @@ -278,10 +286,7 @@ impl CodexCredentialCatalogService { .into_iter() .filter(|(scope, _)| { pending.contains_key(scope) - || !entries.get(scope).is_some_and(|e| { - now.duration_since(e.observed_at) - .is_ok_and(|age| age < CATALOG_REFRESH_AFTER) - }) + || !entries.get(scope).is_some_and(|entry| entry.is_fresh(now)) }) .collect::>(); if due.is_empty() { diff --git a/backend/crates/providers/openai/tests/credential/catalog/mod.rs b/backend/crates/providers/openai/tests/credential/catalog/mod.rs index ebd3132db..abc0a55a3 100644 --- a/backend/crates/providers/openai/tests/credential/catalog/mod.rs +++ b/backend/crates/providers/openai/tests/credential/catalog/mod.rs @@ -738,6 +738,108 @@ async fn invalidation_queues_background_refresh_without_an_upstream_etag() { ); } +#[tokio::test] +async fn matching_etag_renews_only_its_account_until_the_next_change_or_expiry() { + use gateway_core::account::AccountCatalogObservation; + use std::time::{Duration, SystemTime}; + + let store = Arc::new(MemoryAccountStore::default()); + let active = seed_account(&store, "acct_active").await; + let idle = seed_account(&store, "acct_idle").await; + let document = [&[0_u8][..], OFFICIAL_FIXTURE].concat(); + let old_tag = r#""v1""#; + let new_tag = r#""v2""#; + let observed_at = SystemTime::now() - Duration::from_mins(61); + for account in [&active, &idle] { + assert!( + store + .save_account_catalog( + &AccountCatalogObservation { + account_id: account.id().clone(), + revision: account.revision(), + plan: account.plan_type().map(str::to_owned), + observed_at, + etag: Some(old_tag.to_owned()), + }, + &document + ) + .await + .unwrap() + ); + } + let server = MockServer::start().await; + for account in [&active, &idle] { + Mock::given(method("GET")) + .and(path("/codex/models")) + .and(header( + "chatgpt-account-id", + format!("chatgpt-{}", account.id()), + )) + .respond_with( + ResponseTemplate::new(200) + .insert_header("etag", old_tag) + .set_body_raw(OFFICIAL_FIXTURE, "application/json"), + ) + .expect(1) + .mount(&server) + .await; + } + let service = service_with_catalog_cache(&store, server.uri(), catalog_cache()); + service.restore().await.unwrap(); + assert!(!service.observe_response_etag(&active, old_tag).unwrap()); + service.refresh_catalogs().await.unwrap(); + let requests = server.received_requests().await.unwrap(); + assert_eq!(requests.len(), 1); + assert_eq!( + requests[0].headers["chatgpt-account-id"], + "chatgpt-acct_idle" + ); + + tokio::time::pause(); + tokio::time::advance(Duration::from_mins(59)).await; + service.refresh_catalogs().await.unwrap(); + assert_eq!(server.received_requests().await.unwrap().len(), 1); + tokio::time::advance(Duration::from_mins(1)).await; + tokio::time::resume(); + service.refresh_catalogs().await.unwrap(); + server.verify().await; + + server.reset().await; + Mock::given(method("GET")) + .and(path("/codex/models")) + .and(header("chatgpt-account-id", "chatgpt-acct_active")) + .respond_with( + ResponseTemplate::new(200) + .insert_header("etag", new_tag) + .set_body_json( + serde_json::json!({"models":[{"slug":"active-only","display_name":"Active"}]}), + ), + ) + .expect(1) + .mount(&server) + .await; + assert!(!service.observe_response_etag(&active, old_tag).unwrap()); + assert!( + service + .observe_response_etag(&active, "change-hint") + .unwrap() + ); + assert!(!service.observe_response_etag(&active, old_tag).unwrap()); + service.refresh_catalogs().await.unwrap(); + assert!( + !service + .observe_response_etag(&active, "change-hint") + .unwrap() + ); + assert!(!service.observe_response_etag(&active, new_tag).unwrap()); + service.refresh_catalogs().await.unwrap(); + assert_eq!( + service.cached_account_models(&active).unwrap().unwrap(), + ["active-only"] + ); + server.verify().await; +} + #[tokio::test] async fn simultaneous_periodic_refreshes_share_the_completed_scan() { let store = Arc::new(MemoryAccountStore::default()); diff --git a/docs/architecture.md b/docs/architecture.md index d060aa75e..65718be47 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -981,7 +981,8 @@ PostgreSQL schema 由迁移目录按编号管理;冻结、升级与校验规 ### 账号模型目录 OpenAI 在 PostgreSQL 保存账号最近一次成功的模型目录、凭据版本、观测时间及 ETag。启动时批量恢复,按不同正文解析一次,保留每个账号的独立归属。没有历史目录也能发布路由快照;能力读取与推理不等待上游查询。 -后台刷新缺少目录、距上次成功观测已满 30 分钟或收到新 ETag 的账号。刷新期间及失败后继续使用上次成功的目录;凭据或套餐变化后不沿用旧目录。 +账号缺少目录或收到新 ETag 时,后台重新查询该账号。正常响应的 ETag 与该账号当前凭据和套餐的目录一致时,在内存中续期。距上次成功拉取或确认满 1 小时后,由后台复查。重启后从持久化的目录观测时间起算。 +刷新期间及失败后继续使用上次成功的目录。凭据或套餐变化后重新发现模型。 刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护 OpenAI 从账号目录生成原生列表;先按冻结政策选择来源,再读取本地账号状态核对凭据版本。同一份正文可以复用,但不会把一个账号的模型授予同套餐的其他账号。客户端版本和 Key 身份不触发额外发现