1use cpa_core::gostr::GoStr;
18use std::collections::{BTreeMap, HashSet};
19use std::panic::{AssertUnwindSafe, catch_unwind};
20use std::path::{Path, PathBuf};
21use std::sync::{Arc, PoisonError};
22use std::time::{Duration, Instant, SystemTime};
23
24use cpa_core::config::{Config, credentials};
25use cpa_core::credential::{Credential, Source};
26use sha2::{Digest, Sha256};
27
28use crate::fs_events::{Events, Targets};
29use crate::management::Management;
30
31const TICK: Duration = Duration::from_millis(50);
32const CONFIG_SETTLE: Duration = Duration::from_millis(150);
34const AUTH_SETTLE: Duration = Duration::from_millis(50);
36const RACY: Duration = Duration::from_secs(2);
39
40type Hash = [u8; 32];
41
42#[derive(Clone, Default, PartialEq, Eq)]
44struct Snapshot {
45 config: Option<Hash>,
47 auth: BTreeMap<PathBuf, Hash>,
48}
49
50#[derive(Clone, Copy, PartialEq, Eq)]
52struct Stat {
53 len: u64,
54 ino: u64,
55 mtime: (i64, i64),
56 ctime: (i64, i64),
57}
58
59impl Stat {
60 #[cfg(unix)]
61 fn of(meta: &std::fs::Metadata) -> Self {
62 use std::os::unix::fs::MetadataExt;
63 Self {
64 len: meta.len(),
65 ino: meta.ino(),
66 mtime: (meta.mtime(), meta.mtime_nsec()),
67 ctime: (meta.ctime(), meta.ctime_nsec()),
68 }
69 }
70
71 #[cfg(not(unix))]
75 fn of(meta: &std::fs::Metadata) -> Self {
76 let mtime = meta
77 .modified()
78 .ok()
79 .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
80 .map_or((0, 0), |d| (d.as_secs() as i64, i64::from(d.subsec_nanos())));
81 Self {
82 len: meta.len(),
83 ino: 0,
84 mtime,
85 ctime: (0, 0),
86 }
87 }
88}
89
90#[derive(Default)]
92struct HashCache(BTreeMap<PathBuf, (Stat, Hash)>);
93
94impl HashCache {
95 fn hash(&mut self, path: &Path, meta: &std::fs::Metadata, now: SystemTime) -> Option<Hash> {
97 let stat = Stat::of(meta);
98 let racy = meta
99 .modified()
100 .map_or(true, |m| now.duration_since(m).map_or(true, |age| age < RACY));
101 if let Some((cached, hash)) = self.0.get(path)
102 && *cached == stat
103 && !racy
104 {
105 return Some(*hash);
106 }
107 let data = std::fs::read(path).ok()?;
108 let hash: Hash = Sha256::digest(&data).into();
109 self.0.insert(path.to_owned(), (stat, hash));
110 Some(hash)
111 }
112}
113
114fn is_auth_json(path: &Path) -> bool {
115 path.file_name()
117 .is_some_and(|n| n.to_string_lossy().go_lower().ends_with(".json"))
118}
119
120fn observe(state: &Management, cache: &mut HashCache) -> Snapshot {
121 let now = SystemTime::now();
122 let mut seen = Vec::new();
123 let config = std::fs::metadata(&state.path)
124 .ok()
125 .and_then(|meta| cache.hash(&state.path, &meta, now));
126 seen.push(state.path.clone());
127 let mut auth = BTreeMap::new();
128 let dir = state.rt.config().auth_dir.clone();
129 for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() {
131 let path = entry.path();
132 let Ok(meta) = std::fs::metadata(&path) else { continue };
133 if !meta.is_file() || !is_auth_json(&path) {
134 continue;
135 }
136 let hash = cache.hash(&path, &meta, now).unwrap_or_default();
138 seen.push(path.clone());
139 auth.insert(path, hash);
140 }
141 cache.0.retain(|path, _| seen.contains(path));
142 Snapshot { config, auth }
143}
144
145fn log_auth_changes(before: &Snapshot, after: &Snapshot) {
147 let name = |p: &Path| p.file_name().unwrap_or_default().to_string_lossy().into_owned();
148 for (path, hash) in &after.auth {
149 let op = match before.auth.get(path) {
150 None => "CREATE",
151 Some(old) if old != hash => "WRITE",
152 Some(_) => continue,
153 };
154 tracing::info!("auth file changed ({op}): {}, processing incrementally", name(path));
155 }
156 for path in before.auth.keys().filter(|p| !after.auth.contains_key(*p)) {
157 tracing::info!("auth file changed (REMOVE): {}, processing incrementally", name(path));
158 }
159}
160
161pub fn start(state: &Arc<Management>) -> tokio::task::JoinHandle<()> {
162 let first = targets(state, None);
163 let plugin_auth = Arc::new(tokio::sync::Notify::new());
166 credentials::on_plugin_auth_change(Some(Arc::new({
167 let plugin_auth = plugin_auth.clone();
168 move || plugin_auth.notify_one()
169 })));
170 let weak = Arc::downgrade(state);
171 let state = state.clone();
172 tokio::spawn(async move {
173 let started = tokio::task::spawn_blocking(move || {
179 let snapshot = {
180 let _guard = state.disk.lock().unwrap_or_else(PoisonError::into_inner);
181 let hash = std::fs::read(&state.path)
182 .ok()
183 .map(|data| <Hash>::from(Sha256::digest(data)));
184 (state.rt.config(), hash)
185 };
186 let events = catch_unwind(AssertUnwindSafe(|| Events::new(&first))).unwrap_or_default();
187 (snapshot, events)
188 })
189 .await;
190 let Ok(((mut previous_config, initial_hash), mut events)) = started else {
191 return;
192 };
193 let state = weak;
194 let mut cache = HashCache::default();
195 let mut applied: Option<Snapshot> = None;
198 let mut observed: Option<Snapshot> = None;
199 let mut since = Instant::now();
200 let mut applied_epoch = credentials::plugin_auth_epoch();
202 loop {
203 let woke = applied.is_some() && applied == observed && applied_epoch == credentials::plugin_auth_epoch();
204 if woke {
205 tokio::select! {
206 () = events.changed() => {}
207 () = plugin_auth.notified() => {}
208 }
209 } else {
210 tokio::time::sleep(TICK).await;
211 }
212 let Some(state) = state.upgrade() else {
213 return;
214 };
215 let wake_targets = woke.then(|| targets(&state, observed.as_ref()));
220 let looked = tokio::task::spawn_blocking({
223 let state = state.clone();
224 move || {
225 let snapshot = catch_unwind(AssertUnwindSafe(|| {
226 if let Some(t) = wake_targets {
227 events.retarget(&t);
228 }
229 let _guard = state.disk.lock().unwrap_or_else(PoisonError::into_inner);
230 observe(&state, &mut cache)
231 }));
232 (snapshot.ok(), cache, events)
233 }
234 })
235 .await;
236 let snapshot = match looked {
237 Ok((snapshot, returned_cache, returned_events)) => {
238 cache = returned_cache;
239 events = returned_events;
240 snapshot
241 }
242 Err(_) => {
244 cache = HashCache::default();
245 events = Events::default();
246 None
247 }
248 };
249 let Some(snapshot) = snapshot else {
250 tracing::error!(
251 "config watcher: looking at the config and auth files failed; retrying on the next change"
252 );
253 continue;
254 };
255 if observed.as_ref() != Some(&snapshot) {
256 observed = Some(snapshot);
257 since = Instant::now();
258 continue;
259 }
260 let Some(current) = observed.clone() else { continue };
261 let epoch = credentials::plugin_auth_epoch();
262 if applied.as_ref() == Some(¤t) && applied_epoch == epoch {
263 continue;
264 }
265 let config_changed = applied.as_ref().map_or(initial_hash, |a| a.config) != current.config;
266 let read_config = config_changed || applied.is_none();
270 if since.elapsed() < if read_config { CONFIG_SETTLE } else { AUTH_SETTLE } {
271 continue;
272 }
273 if let Some(before) = &applied {
274 log_auth_changes(before, ¤t);
275 if config_changed && current.config.is_some() {
276 tracing::info!("config file changed, reloading: {}", state.path.display());
277 }
278 }
279 applied_epoch = epoch;
283 let reloaded = tokio::task::spawn_blocking({
284 let state = state.clone();
285 let current = current.clone();
286 move || {
287 let loaded = catch_unwind(AssertUnwindSafe(|| reload_config(&state, read_config, ¤t)));
288 let _ = catch_unwind(AssertUnwindSafe(|| {
289 events.retarget(&targets(&state, Some(¤t)));
290 }));
291 (loaded.map_err(|_| ()), events)
292 }
293 })
294 .await;
295 let (loaded, read) = match reloaded {
296 Ok((loaded, returned)) => {
297 events = returned;
298 match loaded {
299 Ok((loaded, read)) => (Ok(loaded), Some(read)),
300 Err(()) => (Err(()), None),
301 }
302 }
303 Err(_) => {
304 events = Events::default();
305 (Err(()), None)
306 }
307 };
308 if loaded.is_err() {
309 tracing::error!("config watcher: reload failed unexpectedly; retrying on the next change");
310 }
311 if let Some(before) = &applied {
312 let accepted = matches!(loaded, Ok(Ok(Some(_))));
313 persist_changes(&state, before, ¤t, config_changed && accepted);
314 }
315 match loaded {
316 Ok(Ok(Some(next_config))) => {
317 if config_changed || next_config.document != previous_config.document {
320 crate::config_diff::log(&previous_config, &next_config);
321 tracing::info!("config successfully reloaded, triggering client reload");
322 }
323 previous_config = Arc::new(next_config);
324 }
325 Ok(Err(error)) if config_changed || applied.is_none() => match error {
326 ReloadError::Missing(e) => tracing::error!(
327 "failed to read config file for hash check: open {}: {}",
328 state.path.display(),
329 go_errno_text(&e)
330 ),
331 ReloadError::Invalid(e) => tracing::error!("failed to reload config: {e:#}"),
332 },
333 _ => {}
334 }
335 applied = Some(record(&mut cache, current, read));
338 }
339 })
340}
341
342fn record(cache: &mut HashCache, observed: Snapshot, read: Option<Snapshot>) -> Snapshot {
348 let read = read.unwrap_or_else(|| observed.clone());
349 if read != observed {
350 *cache = HashCache::default();
351 }
352 read
353}
354
355fn targets(state: &Management, current: Option<&Snapshot>) -> Targets {
358 Targets {
359 config: state.path.clone(),
360 auth_dir: state.rt.config().auth_dir.clone(),
361 files: current.map(|s| s.auth.keys().cloned().collect()).unwrap_or_default(),
362 }
363}
364
365fn persist_changes(state: &Management, before: &Snapshot, after: &Snapshot, config_accepted: bool) {
373 let Some(store) = state.store.clone() else {
374 return;
375 };
376 let config = config_accepted && std::fs::metadata(&state.path).is_ok_and(|m| m.len() > 0);
377 let mut auth = Vec::new();
378 for (path, hash) in &after.auth {
379 if before.auth.get(path) == Some(hash) {
380 continue;
381 }
382 let object = std::fs::read(path)
383 .ok()
384 .filter(|data| !data.is_empty())
385 .is_some_and(|data| serde_json::from_slice::<serde_json::Map<String, serde_json::Value>>(&data).is_ok());
386 if object {
387 auth.push(("Sync", path.clone()));
388 }
389 }
390 for path in before.auth.keys().filter(|p| !after.auth.contains_key(*p)) {
391 auth.push(("Remove", path.clone()));
392 }
393 if !config && auth.is_empty() {
394 return;
395 }
396 tokio::spawn(async move {
397 let bound = Duration::from_secs(30);
398 if config {
399 match tokio::time::timeout(bound, store.persist_config()).await {
400 Ok(Ok(())) => {}
401 Ok(Err(error)) => tracing::error!("failed to persist config change: {error:#}"),
402 Err(_) => tracing::error!("failed to persist config change: context deadline exceeded"),
403 }
404 }
405 for (verb, path) in auth {
406 let name = path
407 .file_name()
408 .map(|n| n.to_string_lossy().into_owned())
409 .unwrap_or_default();
410 let message = format!("{verb} auth {name}");
411 match tokio::time::timeout(bound, store.persist_auth_files(message, vec![path])).await {
412 Ok(Ok(())) => {}
413 Ok(Err(error)) => tracing::error!("failed to persist auth changes: {error:#}"),
414 Err(_) => tracing::error!("failed to persist auth changes: context deadline exceeded"),
415 }
416 }
417 });
418}
419
420fn go_errno_text(e: &std::io::Error) -> String {
422 let text = e.to_string();
423 let text = text.split(" (os error").next().unwrap_or_default();
424 let mut chars = text.chars();
425 chars
426 .next()
427 .map_or_else(String::new, |c| c.to_lowercase().chain(chars).collect())
428}
429
430#[derive(Debug)]
432pub enum ReloadError {
433 Missing(std::io::Error),
435 Invalid(anyhow::Error),
437}
438
439impl std::fmt::Display for ReloadError {
440 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
441 match self {
442 Self::Missing(e) => write!(f, "{e}"),
443 Self::Invalid(e) => write!(f, "{e:#}"),
444 }
445 }
446}
447
448impl std::error::Error for ReloadError {}
449
450pub fn reload(state: &Management) -> Result<(), ReloadError> {
456 reload_config(state, true, &Snapshot::default()).0.map(|_| ())
457}
458
459fn reload_config(
463 state: &Management,
464 read_config: bool,
465 observed: &Snapshot,
466) -> (Result<Option<Config>, ReloadError>, Snapshot) {
467 if credentials::plugin_auth_parser_active() {
472 credentials::preparse_auth_dir(&state.rt.config());
473 }
474 let _guard = state.disk.lock().unwrap_or_else(PoisonError::into_inner);
475 let mut read = Snapshot {
476 config: observed.config,
477 auth: BTreeMap::new(),
478 };
479 let (mut config, config_error, accepted) = match read_config.then(|| std::fs::read(&state.path)) {
482 None => ((*state.rt.config()).clone(), None, false),
483 Some(Err(error)) => {
484 read.config = None;
485 let error = if std::fs::metadata(&state.path).is_err() {
486 ReloadError::Missing(error)
487 } else {
488 ReloadError::Invalid(error.into())
489 };
490 ((*state.rt.config()).clone(), Some(error), false)
491 }
492 Some(Ok(data)) => {
493 read.config = Some(Sha256::digest(&data).into());
494 let loaded = (!data.is_empty()).then(|| {
495 std::str::from_utf8(&data)
496 .map_err(anyhow::Error::from)
497 .and_then(|text| Config::load_text(&state.path, text))
498 });
499 match loaded {
500 None => ((*state.rt.config()).clone(), None, false),
502 Some(Ok(config)) => (config, None, true),
503 Some(Err(error)) => ((*state.rt.config()).clone(), Some(ReloadError::Invalid(error)), false),
504 }
505 }
506 };
507 state.lock_auth_dir(&mut config);
508 let mut scan = credentials::scan_auth_dir(&config);
509 keep_incomplete(state, &mut scan.credentials, &scan.incomplete);
510 read.auth = scan.read.clone();
511 for path in &scan.unapplied {
512 read.auth.remove(path);
513 }
514 for (path, hash) in &observed.auth {
517 if *hash == Hash::default() {
518 read.auth.entry(path.clone()).or_insert(*hash);
519 }
520 }
521 let result = accepted.then(|| config.clone());
522 state.publish(config, Some(scan));
523 let result = match config_error {
524 Some(error) => Err(error),
525 None => Ok(result),
526 };
527 (result, read)
528}
529
530fn keep_incomplete(state: &Management, files: &mut Vec<Credential>, incomplete: &HashSet<PathBuf>) {
536 for existing in state.rt.store().snapshot() {
537 if let Source::File(path) = &existing.source
538 && incomplete.contains(path)
539 && !files.iter().any(|c| c.id == existing.id)
540 {
541 files.push(Credential::clone(&existing));
542 }
543 }
544}
545
546#[cfg(test)]
547mod tests {
548 use super::*;
549
550 fn state() -> (Arc<Management>, PathBuf) {
551 let dir = std::env::temp_dir().join(format!("cpa-watch-outcome-{}", uuid::Uuid::new_v4()));
552 std::fs::create_dir_all(&dir).unwrap();
553 let path = dir.join("config.yaml");
554 std::fs::write(&path, format!("oauth: {{auth-dir: {}}}\n", dir.display())).unwrap();
555 let rt = Arc::new(crate::testing::runtime(
556 Config::load(&path).unwrap(),
557 Vec::new(),
558 cpa_exec::Executors {
559 claude: cpa_exec::claude::ClaudeExecutor::new("http://127.0.0.1:9").unwrap(),
560 codex: Default::default(),
561 devices: Default::default(),
562 openai: Default::default(),
563 google: Default::default(),
564 },
565 ));
566 (Management::new(rt, path), dir)
567 }
568
569 #[tokio::test]
573 async fn accepted_config_is_captured_and_skipped_reloads_do_not_advance_it() {
574 let (state, dir) = state();
575 let config = |port| format!("port: {port}\noauth: {{auth-dir: {}}}\n", dir.display());
576 std::fs::write(&state.path, config(9001)).unwrap();
577 let accepted = reload_config(&state, true, &Snapshot::default()).0.unwrap().unwrap();
578 state.rt.publish_config(Config::parse(&config(9002)).unwrap());
579 assert_eq!(accepted.port, 9001); std::fs::write(&state.path, "").unwrap();
581 assert!(reload_config(&state, true, &Snapshot::default()).0.unwrap().is_none());
582 assert_eq!(state.rt.config().port, 9002);
583 std::fs::write(&state.path, config(9003)).unwrap();
584 assert!(reload_config(&state, false, &Snapshot::default()).0.unwrap().is_none()); assert_eq!(state.rt.config().port, 9002);
586 std::fs::remove_dir_all(dir).unwrap();
587 }
588
589 fn step(state: &Management, cache: &mut HashCache, read_config: bool, observed: &Snapshot) -> Snapshot {
592 let (_, read) = reload_config(state, read_config, observed);
593 record(cache, observed.clone(), Some(read))
594 }
595
596 #[tokio::test]
606 async fn the_watcher_records_what_its_reload_read_not_what_it_observed() {
607 let (state, dir) = state();
608 let mut cache = HashCache::default();
609 let config = |port| format!("port: {port}\noauth: {{auth-dir: {}}}\n", dir.display());
610 std::fs::write(&state.path, config(9001)).unwrap();
611 let observed = observe(&state, &mut cache);
612 std::fs::write(&state.path, config(9002)).unwrap(); let applied = step(&state, &mut cache, true, &observed);
614 assert_eq!(state.rt.config().port, 9002);
615 std::fs::write(&state.path, config(9001)).unwrap(); let restored = observe(&state, &mut cache);
617 assert!(restored != applied, "the restore must be reloaded");
618 step(&state, &mut cache, true, &restored);
619 assert_eq!(state.rt.config().port, 9001);
620
621 let file = dir.join("a.json");
622 let account = |email: &str| format!(r#"{{"type":"codex","email":"{email}"}}"#);
623 let email = |state: &Management| {
624 let creds = state.rt.store().snapshot();
625 let cred = creds.iter().find(|c| c.id == "a.json").expect("a.json is loaded");
626 cred.str("email").unwrap_or_default().to_owned()
627 };
628 std::fs::write(&file, account("a@example.com")).unwrap();
629 reload(&state).unwrap();
630 std::fs::write(&file, account("b@example.com")).unwrap();
631 let observed = observe(&state, &mut cache);
632 std::fs::write(&file, "").unwrap(); let applied = step(&state, &mut cache, false, &observed);
634 assert_eq!(email(&state), "a@example.com"); std::fs::write(&file, account("b@example.com")).unwrap(); let finished = observe(&state, &mut cache);
637 assert!(
638 finished == observed && finished != applied,
639 "the finished write must be reloaded"
640 );
641 let applied = step(&state, &mut cache, false, &finished);
642 assert_eq!(email(&state), "b@example.com");
643 assert!(observe(&state, &mut cache) == applied, "nothing left to reload");
644 std::fs::remove_dir_all(dir).unwrap();
645 }
646
647 #[cfg(target_os = "linux")]
651 #[tokio::test]
652 async fn an_unreadable_empty_config_converges() {
653 let (state, dir) = state();
654 let mut cache = HashCache::default();
655 let port = state.rt.config().port;
656 std::fs::remove_file(&state.path).unwrap();
657 std::os::unix::fs::symlink("/proc/self/mem", &state.path).unwrap();
659 let observed = observe(&state, &mut cache);
660 assert_eq!(observed.config, None);
661 let applied = step(&state, &mut cache, true, &observed);
662 assert!(observe(&state, &mut cache) == applied, "no reload without a change");
663 assert_eq!(state.rt.config().port, port);
664 std::fs::remove_file(&state.path).unwrap();
665 std::fs::write(
666 &state.path,
667 format!("port: 9005\noauth: {{auth-dir: {}}}\n", dir.display()),
668 )
669 .unwrap();
670 let changed = observe(&state, &mut cache);
671 assert!(changed != applied);
672 step(&state, &mut cache, true, &changed);
673 assert_eq!(state.rt.config().port, 9005);
674 std::fs::remove_dir_all(dir).unwrap();
675 }
676
677 #[cfg(unix)]
681 #[tokio::test]
682 async fn a_stale_cached_hash_does_not_reload_forever() {
683 let (state, dir) = state();
684 let mut cache = HashCache::default();
685 let file = dir.join("a.json");
686 std::fs::write(&file, r#"{"type":"codex","email":"b@example.com"}"#).unwrap();
687 let old = SystemTime::now() - Duration::from_secs(60);
688 std::fs::File::options()
689 .write(true)
690 .open(&file)
691 .unwrap()
692 .set_modified(old)
693 .unwrap();
694 let meta = std::fs::metadata(&file).unwrap();
696 cache.0.insert(file.clone(), (Stat::of(&meta), [7; 32]));
697 let observed = observe(&state, &mut cache);
698 assert_eq!(observed.auth[&file], [7; 32]);
699 let applied = step(&state, &mut cache, false, &observed);
700 assert!(
701 state
702 .rt
703 .store()
704 .snapshot()
705 .iter()
706 .any(|c| c.str("email") == Some("b@example.com"))
707 );
708 assert!(observe(&state, &mut cache) == applied, "converges after one reload");
709 std::fs::remove_dir_all(dir).unwrap();
710 }
711
712 #[tokio::test]
716 async fn incomplete_auth_writes_keep_the_credential_until_the_file_is_valid_json() {
717 let (state, dir) = state();
718 let file = dir.join("a.json");
719 let loaded = |state: &Management| state.rt.store().snapshot().iter().any(|c| c.id == "a.json");
720 std::fs::write(&file, r#"{"type":"codex","email":"a@example.com"}"#).unwrap();
721 reload(&state).unwrap();
722 assert!(loaded(&state));
723 for (contents, kept) in [
724 ("", true),
725 (r#"{"type":"codex""#, true),
726 (" \n", true),
727 ("null", false),
728 ("{}", false),
729 ] {
730 std::fs::write(&file, r#"{"type":"codex","email":"a@example.com"}"#).unwrap();
731 reload(&state).unwrap();
732 std::fs::write(&file, contents).unwrap();
733 reload(&state).unwrap();
734 assert_eq!(loaded(&state), kept, "{contents:?}");
735 }
736 std::fs::write(&file, r#"{"type":"codex"}"#).unwrap();
737 reload(&state).unwrap();
738 std::fs::remove_file(&file).unwrap();
739 reload(&state).unwrap();
740 assert!(!loaded(&state));
741 std::fs::remove_dir_all(dir).unwrap();
742 }
743
744 #[tokio::test]
749 async fn a_removed_and_recreated_file_keeps_its_credential() {
750 let (state, dir) = state();
751 let file = dir.join("a.json");
752 let good = |email: &str| format!(r#"{{"type":"codex","email":"{email}"}}"#);
753 let loaded = |state: &Management| {
754 state
755 .rt
756 .store()
757 .snapshot()
758 .iter()
759 .find(|c| c.id == "a.json")
760 .and_then(|c| c.str("email").map(str::to_owned))
761 };
762 std::fs::write(&file, good("a@example.com")).unwrap();
763 reload(&state).unwrap();
764 assert_eq!(loaded(&state).as_deref(), Some("a@example.com"));
765 std::fs::remove_file(&file).unwrap();
766 std::fs::write(&file, good("b@example.com")).unwrap();
767 reload(&state).unwrap();
768 assert_eq!(
769 loaded(&state).as_deref(),
770 Some("b@example.com"),
771 "re-created before the reload"
772 );
773 std::fs::remove_file(&file).unwrap();
774 reload(&state).unwrap();
775 assert_eq!(loaded(&state), None, "removed");
776 std::fs::write(&file, good("c@example.com")).unwrap();
777 reload(&state).unwrap();
778 assert_eq!(
779 loaded(&state).as_deref(),
780 Some("c@example.com"),
781 "re-created after the reload"
782 );
783 std::fs::remove_dir_all(dir).unwrap();
784 }
785
786 #[tokio::test]
790 async fn a_file_completed_after_the_scan_keeps_its_credential() {
791 let (state, dir) = state();
792 let file = dir.join("a.json");
793 let good = r#"{"type":"codex","email":"a@example.com"}"#;
794 std::fs::write(&file, good).unwrap();
795 reload(&state).unwrap();
796 std::fs::write(&file, r#"{"type":"codex""#).unwrap();
797 let scan = credentials::scan_auth_dir(&state.rt.config());
798 let mut files = scan.credentials;
799 assert!(scan.incomplete.contains(&file) && files.is_empty());
800 std::fs::write(&file, good).unwrap();
801 keep_incomplete(&state, &mut files, &scan.incomplete);
802 assert_eq!(files.iter().map(|c| c.id.as_str()).collect::<Vec<_>>(), ["a.json"]);
803 std::fs::remove_dir_all(dir).unwrap();
804 }
805
806 #[tokio::test]
809 async fn a_reload_scan_does_not_undo_a_refresh_committed_after_it() {
810 let (state, dir) = state();
811 std::fs::write(dir.join("a.json"), r#"{"type":"codex","access_token":"old"}"#).unwrap();
812 reload(&state).unwrap();
813 let config = (*state.rt.config()).clone();
814 let scan = credentials::scan_auth_dir(&config);
815 let store = state.rt.store();
816 let revision = store.get("a.json").unwrap().revision;
817 let mut patch = cpa_core::credential::MetadataPatch::default();
818 patch.set.insert("access_token".into(), "refreshed".into());
819 store.apply_patch("a.json", revision, &patch).unwrap();
820 state.publish(config, Some(scan));
821 assert_eq!(store.get("a.json").unwrap().metadata["access_token"], "refreshed");
822 reload(&state).unwrap();
823 assert_eq!(store.get("a.json").unwrap().metadata["access_token"], "refreshed");
824 std::fs::remove_dir_all(dir).unwrap();
825 }
826
827 #[tokio::test]
832 async fn a_reload_does_not_undo_a_write_to_an_upload_fallback() {
833 for relative in [false, true] {
834 let (state, dir) = state();
835 let local = PathBuf::from(format!("cpa-watch-relative-{}", uuid::Uuid::new_v4()));
838 if relative {
839 std::fs::create_dir_all(&local).unwrap();
840 std::fs::write(&state.path, format!("oauth: {{auth-dir: {}}}\n", local.display())).unwrap();
841 }
842 reload(&state).unwrap();
843 let config = (*state.rt.config()).clone();
844 assert_eq!(config.auth_dir.is_relative(), relative);
845 let file = std::path::absolute(config.auth_dir.join("a.json")).unwrap();
847 let a = r#"{"type":"gemini","v":"A"}"#;
848 std::fs::write(&file, a).unwrap();
849 state.fallbacks.lock().unwrap().insert(file.clone());
850 reload(&state).unwrap();
851 let store = state.rt.store();
852 let before = store.get("a.json").unwrap();
853 assert_eq!(before.metadata["v"], "A");
854 let scan = credentials::scan_auth_dir(&config);
855 std::fs::write(&file, r#"{"type":"gemini","v":"B"}"#).unwrap();
856 let persisted = file.clone();
857 crate::management::BEFORE_RECONCILE.lock().unwrap().push((
858 config.auth_dir.clone(),
859 Box::new(move || std::fs::write(persisted, a).unwrap()),
860 ));
861 state.publish(config, Some(scan));
862 let after = store.get("a.json").unwrap();
863 assert_eq!(
864 (&after.metadata["v"], after.revision),
865 (&"A".into(), before.revision),
866 "relative: {relative}"
867 );
868 std::fs::remove_dir_all(dir).unwrap();
869 if relative {
870 std::fs::remove_dir_all(local).unwrap();
871 }
872 }
873 }
874
875 #[tokio::test]
879 async fn a_fallback_whose_replacement_was_rejected_stays_tracked() {
880 let (state, dir) = state();
881 let file = dir.join("a.json");
882 let a = r#"{"type":"gemini","v":"A"}"#;
883 std::fs::write(&file, a).unwrap();
884 state.fallbacks.lock().unwrap().insert(file.clone());
885 reload(&state).unwrap();
886 let store = state.rt.store();
887 let before = store.get("a.json").unwrap();
888 let codex = r#"{"type":"codex","access_token":"B"}"#;
889 std::fs::write(&file, codex).unwrap();
890 let config = (*state.rt.config()).clone();
891 let scan = credentials::scan_auth_dir(&config);
892 let persisted = file.clone();
893 crate::management::BEFORE_RECONCILE.lock().unwrap().push((
894 config.auth_dir.clone(),
895 Box::new(move || std::fs::write(persisted, a).unwrap()),
896 ));
897 state.publish(config, Some(scan));
898 let kept = store.get("a.json").unwrap();
899 assert_eq!((kept.provider.as_str(), kept.revision), ("gemini", before.revision));
900 reload(&state).unwrap();
901 assert_eq!(store.get("a.json").unwrap().provider, "gemini");
902 assert!(state.fallbacks.lock().unwrap().contains(&file));
903 std::fs::write(&file, codex).unwrap();
904 reload(&state).unwrap();
905 assert_eq!(store.get("a.json").unwrap().provider, "codex");
906 assert!(state.fallbacks.lock().unwrap().is_empty());
907 std::fs::remove_dir_all(dir).unwrap();
908 }
909
910 #[cfg(unix)]
915 #[tokio::test]
916 async fn an_unreadable_file_drops_its_credential() {
917 let (state, dir) = state();
918 let file = dir.join("a.json");
919 std::fs::write(&file, r#"{"type":"codex","email":"a@example.com"}"#).unwrap();
920 reload(&state).unwrap();
921 std::fs::remove_file(&file).unwrap();
922 std::os::unix::fs::symlink(&file, &file).unwrap();
923 assert!(std::fs::read(&file).is_err());
924 reload(&state).unwrap();
925 assert!(!state.rt.store().snapshot().iter().any(|c| c.id == "a.json"));
926 std::fs::remove_dir_all(dir).unwrap();
927 }
928
929 #[cfg(unix)]
934 #[tokio::test]
935 async fn a_file_replaced_by_a_special_file_link_drops_its_credential() {
936 let (state, dir) = state();
937 let file = dir.join("a.json");
938 std::fs::write(&file, r#"{"type":"codex","email":"a@example.com"}"#).unwrap();
939 reload(&state).unwrap();
940 let loaded = |state: &Management| state.rt.store().snapshot().iter().any(|c| c.id == "a.json");
941 assert!(loaded(&state));
942 let capture = Capture::default();
943 let _guard = capture.install();
944 let watcher = start(&state);
945 tokio::time::sleep(Duration::from_millis(500)).await;
946 std::fs::remove_file(&file).unwrap();
947 std::os::unix::fs::symlink("/dev/null", &file).unwrap();
948 capture.wait_for("auth file changed (REMOVE): a.json").await;
949 tokio::time::timeout(Duration::from_secs(3), async {
950 while loaded(&state) {
951 tokio::time::sleep(TICK).await;
952 }
953 })
954 .await
955 .expect("the reload the replacement triggered drops the credential");
956 std::fs::remove_file(&file).unwrap();
957 tokio::time::sleep(Duration::from_millis(500)).await;
958 assert!(!loaded(&state));
959 watcher.abort();
960 let _ = watcher.await;
961 std::fs::remove_dir_all(dir).unwrap();
962 }
963
964 #[derive(Clone, Default)]
965 struct Capture(Arc<std::sync::Mutex<Vec<u8>>>, Arc<tokio::sync::Notify>);
966 impl std::io::Write for Capture {
967 fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
968 self.0.lock().unwrap().extend_from_slice(bytes);
969 self.1.notify_one();
970 Ok(bytes.len())
971 }
972 fn flush(&mut self) -> std::io::Result<()> {
973 Ok(())
974 }
975 }
976
977 impl Capture {
978 fn install(&self) -> tracing::subscriber::DefaultGuard {
979 let writer = self.clone();
980 let subscriber = tracing_subscriber::fmt()
981 .with_ansi(false)
982 .without_time()
983 .with_writer(move || writer.clone())
984 .finish();
985 tracing::subscriber::set_default(subscriber)
986 }
987
988 fn text(&self) -> String {
989 String::from_utf8(self.0.lock().unwrap().clone()).unwrap()
990 }
991
992 async fn wait_for(&self, needle: &str) {
993 tokio::time::timeout(Duration::from_secs(3), async {
994 loop {
995 let notified = self.1.notified();
996 if self.text().contains(needle) {
997 break;
998 }
999 notified.await;
1000 }
1001 })
1002 .await
1003 .unwrap_or_else(|_| panic!("{needle:?} was not logged:\n{}", self.text()));
1004 }
1005 }
1006
1007 #[tokio::test]
1012 async fn a_rewritten_token_clears_its_401_block_and_keeps_a_429() {
1013 use crate::runtime::{Outcome, Selection};
1014 use cpa_core::exec::{ExecError, FailureScope};
1015 let (state, dir) = state();
1016 let file = dir.join("claude-a.json");
1017 let doc = |token: &str, email: &str| {
1018 serde_json::json!({"type": "claude", "access_token": token, "refresh_token": "fake-rt", "email": email})
1019 .to_string()
1020 };
1021 std::fs::write(&file, doc("sk-ant-oat-old-fake", "a@example.invalid")).unwrap();
1022 let watcher = start(&state);
1023 let store = state.rt.store();
1024 let wait = |want: &'static str| {
1025 let store = store.clone();
1026 async move {
1027 tokio::time::timeout(Duration::from_secs(5), async {
1028 while store.get("claude-a.json").is_none_or(|c| c.str("email") != Some(want)) {
1029 tokio::time::sleep(Duration::from_millis(20)).await;
1030 }
1031 })
1032 .await
1033 .unwrap_or_else(|_| panic!("{want} was not reloaded"));
1034 }
1035 };
1036 wait("a@example.invalid").await;
1037 let select = |model: &str| store.select(Selection::new("claude", model));
1038 let mut quota = ExecError::local(429, FailureScope::Model, "rate limited");
1039 quota.retry_after = Some(Duration::from_secs(3600));
1040 select("q").unwrap().complete(Outcome::Failure(quota));
1041 let unauthorized = ExecError::local(
1042 401,
1043 FailureScope::Credential,
1044 "authentication_error: OAuth access token",
1045 );
1046 select("m").unwrap().complete(Outcome::Failure(unauthorized));
1047 assert!(select("m").is_none(), "the 401 blocks the credential");
1048 std::fs::write(&file, doc("sk-ant-oat-old-fake", "b@example.invalid")).unwrap();
1050 wait("b@example.invalid").await;
1051 assert!(select("m").is_none(), "an edit that keeps the tokens keeps the block");
1052 std::fs::write(&file, doc("sk-ant-oat-new-fake", "c@example.invalid")).unwrap();
1053 wait("c@example.invalid").await;
1054 let lease = select("m").expect("the new token is selectable at once");
1055 assert_eq!(lease.credential.str("access_token"), Some("sk-ant-oat-new-fake"));
1056 drop(lease);
1057 assert!(select("q").is_none(), "the 429 cooldown survives the reload");
1058 let left = store.cooldowns("claude-a.json");
1059 assert_eq!(
1060 left.iter().map(|c| (c.model.as_str(), c.status)).collect::<Vec<_>>(),
1061 [("q", 429)]
1062 );
1063 watcher.abort();
1064 let _ = watcher.await;
1065 std::fs::remove_dir_all(dir).unwrap();
1066 }
1067
1068 #[tokio::test]
1069 async fn edit_before_first_stable_observation_is_logged_once() {
1070 let (state, dir) = state();
1071 let capture = Capture::default();
1072 let _guard = capture.install();
1073 let watcher = start(&state);
1074 std::fs::write(
1075 &state.path,
1076 format!("port: 9001\noauth: {{auth-dir: {}}}\n", dir.display()),
1077 )
1078 .unwrap();
1079 capture.wait_for("port: 0 -> 9001").await;
1080 watcher.abort();
1081 let _ = watcher.await;
1082 assert_eq!(capture.text().matches("port: 0 -> 9001").count(), 1);
1083 std::fs::remove_dir_all(dir).unwrap();
1084 }
1085
1086 #[tokio::test]
1087 async fn edit_between_runtime_load_and_start_is_applied() {
1088 let (state, dir) = state();
1089 let capture = Capture::default();
1090 let _guard = capture.install();
1091 std::fs::write(
1093 &state.path,
1094 format!("port: 9001\noauth: {{auth-dir: {}}}\n", dir.display()),
1095 )
1096 .unwrap();
1097 let watcher = start(&state);
1098 capture.wait_for("config successfully reloaded").await;
1099 assert_eq!(state.rt.config().port, 9001);
1100 watcher.abort();
1101 let _ = watcher.await;
1102 let text = capture.text();
1103 assert_eq!(text.matches("port: 0 -> 9001").count(), 1);
1104 assert_eq!(text.matches("config successfully reloaded").count(), 1);
1105 std::fs::remove_dir_all(dir).unwrap();
1106 }
1107
1108 #[tokio::test]
1109 async fn unchanged_config_is_read_quietly_on_first_observation() {
1110 let (state, dir) = state();
1111 let capture = Capture::default();
1112 let _guard = capture.install();
1113 let initial = state.rt.config();
1114 let watcher = start(&state);
1115 tokio::time::timeout(Duration::from_secs(3), async {
1119 while Arc::ptr_eq(&state.rt.config(), &initial) {
1120 tokio::time::sleep(TICK).await;
1121 }
1122 })
1123 .await
1124 .unwrap();
1125 std::fs::write(dir.join("a.json"), "{}").unwrap();
1126 capture.wait_for("auth file changed (CREATE): a.json").await;
1127 watcher.abort();
1128 let _ = watcher.await;
1129 let text = capture.text();
1130 assert!(!text.contains("config successfully reloaded"), "{text}");
1131 assert!(!text.contains("config changes detected"), "{text}");
1132 std::fs::remove_dir_all(dir).unwrap();
1133 }
1134
1135 #[tokio::test]
1138 async fn auth_folder_created_after_start_is_followed() {
1139 let (state, dir) = state();
1140 let auth = dir.join("later");
1141 std::fs::write(&state.path, format!("oauth: {{auth-dir: {}}}\n", auth.display())).unwrap();
1142 reload(&state).unwrap();
1143 let capture = Capture::default();
1144 let _guard = capture.install();
1145 let initial = state.rt.config();
1146 let watcher = start(&state);
1147 tokio::time::timeout(Duration::from_secs(3), async {
1148 while Arc::ptr_eq(&state.rt.config(), &initial) {
1149 tokio::time::sleep(TICK).await;
1150 }
1151 })
1152 .await
1153 .unwrap();
1154 std::fs::create_dir(&auth).unwrap();
1155 tokio::time::sleep(Duration::from_millis(500)).await;
1156 std::fs::write(auth.join("a.json"), "{}").unwrap();
1157 capture.wait_for("auth file changed (CREATE): a.json").await;
1158 watcher.abort();
1159 let _ = watcher.await;
1160 std::fs::remove_dir_all(dir).unwrap();
1161 }
1162
1163 #[test]
1164 fn hash_cache_rereads_only_changed_or_racy_files() {
1165 let dir = std::env::temp_dir().join(format!("cpa-watch-cache-{}", std::process::id()));
1166 std::fs::create_dir_all(&dir).unwrap();
1167 let path = dir.join("a.json");
1168 std::fs::write(&path, b"one").unwrap();
1169 let old = SystemTime::now() - Duration::from_secs(60);
1170 std::fs::File::options()
1171 .write(true)
1172 .open(&path)
1173 .unwrap()
1174 .set_modified(old)
1175 .unwrap();
1176 let mut cache = HashCache::default();
1177 let now = SystemTime::now();
1178 let first = cache.hash(&path, &std::fs::metadata(&path).unwrap(), now).unwrap();
1179 let stat = cache.0[&path].0;
1182 cache.0.insert(path.clone(), (stat, [7; 32]));
1183 assert_eq!(
1184 cache.hash(&path, &std::fs::metadata(&path).unwrap(), now),
1185 Some([7; 32])
1186 );
1187 std::fs::write(&path, b"two!").unwrap();
1189 std::fs::File::options()
1190 .write(true)
1191 .open(&path)
1192 .unwrap()
1193 .set_modified(old)
1194 .unwrap();
1195 let second = cache.hash(&path, &std::fs::metadata(&path).unwrap(), now).unwrap();
1196 assert_ne!(first, second);
1197 assert_eq!(second, <Hash>::from(Sha256::digest(b"two!")));
1198 std::fs::write(&path, b"thr!").unwrap();
1200 let meta = std::fs::metadata(&path).unwrap();
1201 cache.0.insert(path.clone(), (Stat::of(&meta), [7; 32]));
1202 assert_eq!(
1203 cache.hash(&path, &meta, SystemTime::now()),
1204 Some(Sha256::digest(b"thr!").into())
1205 );
1206 let _ = std::fs::remove_file(&path);
1207 }
1208
1209 #[tokio::test]
1214 async fn a_deferred_file_keeps_a_refresh_committed_before_publication() {
1215 use cpa_core::config::credentials::{ParseRequest, Parsed, ParsedAuth, PluginAuthParser};
1216 struct Acme;
1217 impl PluginAuthParser for Acme {
1218 fn parse_auths(&self, _: &ParseRequest<'_>) -> Parsed {
1219 Parsed::Handled(vec![ParsedAuth {
1220 provider: "acme".into(),
1221 id: "acme-a".into(),
1222 metadata: serde_json::json!({"type": "acme", "token": "old"})
1223 .as_object()
1224 .cloned()
1225 .unwrap(),
1226 ..Default::default()
1227 }])
1228 }
1229 }
1230 credentials::with_thread_plugin_auth_parser(Arc::new(Acme), || {
1232 let (state, dir) = state();
1233 let file = dir.join("acme.json");
1234 std::fs::write(&file, r#"{"type":"acme","v":1}"#).unwrap();
1235 reload(&state).unwrap();
1236 let store = state.rt.store().clone();
1237 assert_eq!(store.get("acme-a").unwrap().metadata["token"], "old");
1238 std::fs::write(&file, r#"{"type":"acme","v":2}"#).unwrap();
1240 let config = (*state.rt.config()).clone();
1241 let scan = credentials::scan_auth_dir(&config);
1242 assert!(scan.deferred.contains(&file));
1243 let refreshed = store.clone();
1244 crate::management::BEFORE_RECONCILE.lock().unwrap().push((
1245 config.auth_dir.clone(),
1246 Box::new(move || {
1247 let current = refreshed.get("acme-a").unwrap();
1248 let mut patch = cpa_core::credential::MetadataPatch::default();
1249 patch.set.insert("token".into(), "refreshed".into());
1250 refreshed.apply_patch("acme-a", current.revision, &patch).unwrap();
1251 }),
1252 ));
1253 state.publish(config, Some(scan));
1254 assert_eq!(store.get("acme-a").unwrap().metadata["token"], "refreshed");
1255 std::fs::remove_dir_all(dir).unwrap();
1256 });
1257 }
1258
1259 #[tokio::test]
1264 async fn a_new_auth_dir_publishes_a_plugin_bundle_only_once_parsed() {
1265 use cpa_core::config::credentials::{ParseRequest, Parsed, ParsedAuth, PluginAuthParser};
1266 struct Bundles;
1267 impl PluginAuthParser for Bundles {
1268 fn parse_auths(&self, req: &ParseRequest<'_>) -> Parsed {
1269 if req.file_name != "bundle.json" {
1270 return Parsed::NotHandled;
1271 }
1272 let auth = |id: &str| ParsedAuth {
1273 provider: "claude".into(),
1274 id: id.into(),
1275 metadata: serde_json::json!({"type": "claude", "access_token": id})
1276 .as_object()
1277 .cloned()
1278 .unwrap(),
1279 ..Default::default()
1280 };
1281 Parsed::Handled(vec![auth("claude-a"), auth("claude-b")])
1282 }
1283 }
1284 credentials::with_thread_plugin_auth_parser(Arc::new(Bundles), || {
1286 let (state, dir) = state();
1287 reload(&state).unwrap();
1288 let other = dir.join("other");
1289 std::fs::create_dir_all(&other).unwrap();
1290 std::fs::write(
1291 other.join("bundle.json"),
1292 r#"{"type":"claude","accounts":[{"access_token":"a"},{"access_token":"b"}]}"#,
1293 )
1294 .unwrap();
1295 std::fs::write(&state.path, format!("oauth: {{auth-dir: {}}}\n", other.display())).unwrap();
1296 reload(&state).unwrap();
1297 let ids = || -> Vec<String> {
1298 let mut ids: Vec<String> = state.rt.store().snapshot().iter().map(|c| c.id.clone()).collect();
1299 ids.sort();
1300 ids
1301 };
1302 assert!(ids().is_empty(), "published before it was parsed: {:?}", ids());
1303 reload(&state).unwrap();
1304 assert_eq!(ids(), ["claude-a", "claude-b"]);
1305 std::fs::remove_dir_all(dir).unwrap();
1306 });
1307 }
1308}