1use crate::config::Config;
2use chrono::{DateTime, Duration, Utc};
3use codewhale_config::ProviderKind;
4use serde::{Deserialize, Serialize};
5use sha2::{Digest, Sha256};
6use std::io::Read;
7#[cfg(unix)]
8use std::os::unix::fs::OpenOptionsExt;
9use std::path::{Path, PathBuf};
10use std::sync::Mutex;
11use std::time::SystemTime;
12
13pub const MAX_MODEL_CACHE_BYTES: u64 = 4 * 1024 * 1024;
14
15const MODEL_CACHE_MAX_AGE: Duration = Duration::hours(24);
16
17const MAX_FUTURE_CLOCK_SKEW: Duration = Duration::minutes(5);
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum CodexModelCacheFreshness {
21 Fresh,
22 Missing,
23 Stale,
24 Invalid,
25}
26
27impl CodexModelCacheFreshness {
28 #[must_use]
29 pub const fn picker_label(self) -> &'static str {
30 match self {
31 Self::Fresh => "ChatGPT OAuth",
32 Self::Missing => "OAuth roster missing",
33 Self::Stale => "OAuth roster stale",
34 Self::Invalid => "OAuth roster invalid",
35 }
36 }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct CodexModelRoster {
41 pub models: Vec<CodexModelMetadata>,
42 pub freshness: CodexModelCacheFreshness,
43 pub fetched_at: Option<DateTime<Utc>>,
44 pub observed_at: Option<DateTime<Utc>>,
45 pub source: &'static str,
46 pub observation_persisted: bool,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct CodexModelMetadata {
51 pub id: String,
52 #[serde(default)]
53 pub display_name: Option<String>,
54 pub context_window: Option<u32>,
55 pub reasoning: Option<bool>,
56 pub efforts: Vec<String>,
57}
58
59impl CodexModelRoster {
60 fn fallback(freshness: CodexModelCacheFreshness, fetched_at: Option<DateTime<Utc>>) -> Self {
61 Self {
62 models: Vec::new(),
63 freshness,
64 fetched_at,
65 observed_at: None,
66 source: "chatgpt_plan_api",
67 observation_persisted: false,
68 }
69 }
70
71 #[must_use]
72 pub fn model_ids(&self) -> Vec<String> {
73 self.models.iter().map(|model| model.id.clone()).collect()
74 }
75
76 #[must_use]
77 pub fn metadata_for(&self, id: &str) -> Option<&CodexModelMetadata> {
78 self.models
79 .iter()
80 .find(|model| model.id.eq_ignore_ascii_case(id.trim()))
81 }
82
83 #[must_use]
84 pub fn preferred_model_id(&self) -> Option<&str> {
85 (self.freshness == CodexModelCacheFreshness::Fresh)
86 .then(|| self.models.first().map(|model| model.id.as_str()))
87 .flatten()
88 }
89}
90
91#[derive(Serialize, Deserialize)]
92pub struct CatalogSnapshot {
93 pub fetched_at: DateTime<Utc>,
94 pub models: Vec<CodexModelMetadata>,
95}
96
97type RosterCacheKey = (PathBuf, Option<SystemTime>, u64);
98
99static ROSTER_MEMO: Mutex<Option<(RosterCacheKey, CodexModelRoster)>> = Mutex::new(None);
100
101#[must_use]
103pub fn model_roster() -> CodexModelRoster {
104 CodexModelRoster::fallback(CodexModelCacheFreshness::Missing, None)
105}
106
107pub fn cached_roster_for(config: &Config) -> CodexModelRoster {
108 let Some(path) = snapshot_path(config) else {
109 return model_roster();
110 };
111 let key = match std::fs::symlink_metadata(&path) {
112 Ok(metadata) if !metadata.file_type().is_file() => {
113 return CodexModelRoster::fallback(CodexModelCacheFreshness::Invalid, None);
114 }
115 Ok(metadata) => (path.clone(), metadata.modified().ok(), metadata.len()),
116 Err(_) => (path.clone(), None, 0),
117 };
118 let now = Utc::now();
119 if let Ok(memo) = ROSTER_MEMO.lock()
120 && let Some((cached_key, roster)) = memo.as_ref()
121 && *cached_key == key
122 && roster.freshness == CodexModelCacheFreshness::Fresh
123 && roster
124 .fetched_at
125 .is_some_and(|fetched| now.signed_duration_since(fetched) <= MODEL_CACHE_MAX_AGE)
126 {
127 return roster.clone();
128 }
129 let roster = load_snapshot(&path, now);
130 if let Ok(mut memo) = ROSTER_MEMO.lock() {
131 *memo = Some((key, roster.clone()));
132 }
133 roster
134}
135
136fn registration_key(issuer: &str, client_id: &str, subject: &str) -> String {
137 let mut identity = Sha256::new();
138 identity.update(b"codewhale-chatgpt-plan-roster-v1\0");
139 for value in [issuer, client_id, subject] {
140 identity.update((value.len() as u64).to_le_bytes());
141 identity.update(value.as_bytes());
142 }
143 identity
144 .finalize()
145 .iter()
146 .map(|byte| format!("{byte:02x}"))
147 .collect()
148}
149
150pub fn snapshot_path(config: &Config) -> Option<PathBuf> {
151 let identity = config
152 .builtin_provider_identity(ProviderKind::OpenaiCodex)
153 .ok()?;
154 if config.provider_uses_custom_endpoint(&identity) {
155 return None;
156 }
157 let registration = crate::oauth::official_chatgpt_registration(config).ok()?;
158 let catalog_path = crate::catalog_cache::cache_path()?;
159 Some(catalog_path.parent()?.join(format!(
160 "chatgpt-plan-v2-{}.json",
161 registration_key(
162 ®istration.issuer,
163 ®istration.client_id,
164 ®istration.subject
165 )
166 )))
167}
168
169pub fn load_snapshot(path: &Path, now: DateTime<Utc>) -> CodexModelRoster {
170 let bytes = match read_cache_bytes(path) {
171 Ok(bytes) => bytes,
172 Err(freshness) => return CodexModelRoster::fallback(freshness, None),
173 };
174 let snapshot: CatalogSnapshot = match serde_json::from_slice(&bytes) {
175 Ok(snapshot) => snapshot,
176 Err(_) => return CodexModelRoster::fallback(CodexModelCacheFreshness::Invalid, None),
177 };
178 let age = now.signed_duration_since(snapshot.fetched_at);
179 if age < -MAX_FUTURE_CLOCK_SKEW
180 || snapshot.models.iter().any(|model| {
181 !crate::catalog_cache::valid_catalog_model_id(&model.id)
182 || model
183 .display_name
184 .as_ref()
185 .is_some_and(|name| name.len() > 512 || name.chars().any(char::is_control))
186 || model.efforts.len() > 16
187 || model.efforts.iter().any(|effort| !valid_effort(effort))
188 || model
189 .context_window
190 .is_some_and(|window| !(1..=16_000_000).contains(&window))
191 })
192 {
193 return CodexModelRoster::fallback(
194 CodexModelCacheFreshness::Invalid,
195 Some(snapshot.fetched_at),
196 );
197 }
198 if age > MODEL_CACHE_MAX_AGE {
199 return CodexModelRoster {
200 models: snapshot.models,
201 freshness: CodexModelCacheFreshness::Stale,
202 fetched_at: Some(snapshot.fetched_at),
203 observed_at: None,
204 source: "chatgpt_plan_api",
205 observation_persisted: true,
206 };
207 }
208 CodexModelRoster {
209 models: snapshot.models,
210 freshness: CodexModelCacheFreshness::Fresh,
211 fetched_at: Some(snapshot.fetched_at),
212 observed_at: None,
213 source: "chatgpt_plan_api",
214 observation_persisted: true,
215 }
216}
217
218fn valid_effort(effort: &str) -> bool {
219 !effort.is_empty()
220 && effort.len() <= 32
221 && effort
222 .bytes()
223 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
224}
225
226fn read_cache_bytes(path: &Path) -> Result<Vec<u8>, CodexModelCacheFreshness> {
227 let path_metadata = match std::fs::symlink_metadata(path) {
228 Ok(metadata) => metadata,
229 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
230 return Err(CodexModelCacheFreshness::Missing);
231 }
232 Err(_) => return Err(CodexModelCacheFreshness::Invalid),
233 };
234 if !path_metadata.file_type().is_file() || path_metadata.len() > MAX_MODEL_CACHE_BYTES {
235 return Err(CodexModelCacheFreshness::Invalid);
236 }
237 let mut file = match open_cache_file(path) {
238 Ok(file) => file,
239 Err(_) => return Err(CodexModelCacheFreshness::Invalid),
240 };
241 let metadata = match file.metadata() {
242 Ok(metadata) => metadata,
243 Err(_) => return Err(CodexModelCacheFreshness::Invalid),
244 };
245 if !metadata.file_type().is_file() || metadata.len() > MAX_MODEL_CACHE_BYTES {
246 return Err(CodexModelCacheFreshness::Invalid);
247 }
248
249 let mut bytes = Vec::with_capacity(metadata.len().min(MAX_MODEL_CACHE_BYTES) as usize);
250 if file
251 .by_ref()
252 .take(MAX_MODEL_CACHE_BYTES + 1)
253 .read_to_end(&mut bytes)
254 .is_err()
255 || bytes.len() as u64 > MAX_MODEL_CACHE_BYTES
256 {
257 return Err(CodexModelCacheFreshness::Invalid);
258 }
259 Ok(bytes)
260}
261
262#[cfg(any(test, feature = "test-support"))]
263pub fn install_test_chatgpt_roster(config: &Config, ids: &[&str]) -> anyhow::Result<()> {
264 install_test_chatgpt_roster_with_metadata(
265 config,
266 ids.iter()
267 .map(|id| CodexModelMetadata {
268 id: (*id).to_string(),
269 display_name: None,
270 context_window: None,
271 reasoning: None,
272 efforts: Vec::new(),
273 })
274 .collect(),
275 )
276}
277
278#[cfg(any(test, feature = "test-support"))]
279pub fn install_test_chatgpt_roster_with_metadata(
280 config: &Config,
281 models: Vec<CodexModelMetadata>,
282) -> anyhow::Result<()> {
283 let path = snapshot_path(config)
284 .ok_or_else(|| anyhow::anyhow!("test needs an owned ChatGPT registration"))?;
285 let snapshot = CatalogSnapshot {
286 fetched_at: Utc::now(),
287 models,
288 };
289 codewhale_config::persistence::atomic_write(&path, &serde_json::to_vec(&snapshot)?)?;
290 if let Ok(mut memo) = ROSTER_MEMO.lock() {
291 *memo = None;
292 }
293 Ok(())
294}
295
296fn open_cache_file(path: &Path) -> std::io::Result<std::fs::File> {
297 let mut options = std::fs::OpenOptions::new();
298 options.read(true);
299 #[cfg(unix)]
300 options.custom_flags(libc::O_NOFOLLOW);
301 options.open(path)
302}
303
304pub fn invalidate_roster_cache() {
305 if let Ok(mut memo) = ROSTER_MEMO.lock() {
306 *memo = None;
307 }
308}
309
310#[cfg(test)]
311mod tests {
312
313 use super::*;
314
315 fn model(id: &str) -> CodexModelMetadata {
316 CodexModelMetadata {
317 id: id.to_string(),
318 display_name: Some(format!("Label for {id}")),
319 context_window: None,
320 reasoning: None,
321 efforts: Vec::new(),
322 }
323 }
324
325 fn save(path: &Path, fetched_at: DateTime<Utc>, models: Vec<CodexModelMetadata>) {
326 std::fs::write(
327 path,
328 serde_json::to_vec(&CatalogSnapshot { fetched_at, models }).unwrap(),
329 )
330 .unwrap();
331 }
332
333 #[test]
334 fn registration_scope_separates_accounts_workspaces_and_issuers() {
335 let key = registration_key("https://auth.openai.com", "oaiapp_workspace_a", "account_a");
336 for other in [
337 registration_key("https://auth.openai.com", "oaiapp_workspace_a", "account_b"),
338 registration_key("https://auth.openai.com", "oaiapp_workspace_b", "account_a"),
339 registration_key("https://other.example", "oaiapp_workspace_a", "account_a"),
340 ] {
341 assert_ne!(key, other);
342 }
343 assert!(!key.contains("account_a"));
344 assert!(!key.contains("workspace_a"));
345 }
346
347 #[test]
348 fn own_snapshot_preserves_provider_order_and_labels_without_inventing_limits() {
349 let directory = tempfile::tempdir().unwrap();
350 let path = directory.path().join("roster.json");
351 let now = Utc::now();
352 save(&path, now, vec![model("z-first"), model("a-second")]);
353 let roster = load_snapshot(&path, now);
354 assert_eq!(roster.model_ids(), ["z-first", "a-second"]);
355 assert_eq!(roster.preferred_model_id(), Some("z-first"));
356 let metadata = roster.metadata_for("a-second").unwrap();
357 assert_eq!(metadata.display_name.as_deref(), Some("Label for a-second"));
358 assert_eq!(metadata.context_window, None);
359 assert_eq!(metadata.reasoning, None);
360 assert!(metadata.efforts.is_empty());
361 assert_eq!(roster.source, "chatgpt_plan_api");
362 assert!(roster.observation_persisted);
363 }
364
365 #[test]
366 fn missing_stale_future_and_unsafe_snapshots_offer_no_entitlements() {
367 let directory = tempfile::tempdir().unwrap();
368 let path = directory.path().join("roster.json");
369 let now = Utc::now();
370 assert!(load_snapshot(&path, now).model_ids().is_empty());
371 save(
372 &path,
373 now - MODEL_CACHE_MAX_AGE - Duration::seconds(1),
374 vec![model("old")],
375 );
376 let stale = load_snapshot(&path, now);
377 assert_eq!(stale.freshness, CodexModelCacheFreshness::Stale);
378 assert_eq!(stale.model_ids(), ["old"]);
379 assert_eq!(stale.preferred_model_id(), None);
380 save(
381 &path,
382 now + MAX_FUTURE_CLOCK_SKEW + Duration::seconds(1),
383 vec![model("future")],
384 );
385 assert_eq!(
386 load_snapshot(&path, now).freshness,
387 CodexModelCacheFreshness::Invalid
388 );
389 let mut unsafe_label = model("safe-id");
390 unsafe_label.display_name = Some("Unsafe\u{1b}[31m".to_string());
391 save(&path, now, vec![unsafe_label]);
392 assert_eq!(
393 load_snapshot(&path, now).freshness,
394 CodexModelCacheFreshness::Invalid
395 );
396 assert!(model_roster().models.is_empty());
397 }
398
399 #[cfg(unix)]
400 #[test]
401 fn cache_symlinks_are_rejected() {
402 let directory = tempfile::tempdir().unwrap();
403 let target = directory.path().join("target.json");
404 let path = directory.path().join("roster.json");
405 let now = Utc::now();
406 save(&target, now, vec![model("safe")]);
407 std::os::unix::fs::symlink(target, &path).unwrap();
408 assert_eq!(
409 load_snapshot(&path, now).freshness,
410 CodexModelCacheFreshness::Invalid
411 );
412 }
413}