Skip to content

Commit 2eca39f

Browse files
authored
Merge pull request #483 from OpenCoven/fix/query-config-home-test-race
test(query): hold one config-home lock wherever a test changes the home
2 parents 524db31 + 9112a81 commit 2eca39f

3 files changed

Lines changed: 55 additions & 48 deletions

File tree

‎src-rust/crates/query/src/goal_loop.rs‎

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,11 @@ pub fn mark_goal_complete(session_id: &str) -> Result<(), String> {
351351

352352
#[cfg(test)]
353353
mod tests {
354-
use std::{path::PathBuf, sync::Mutex};
354+
use std::path::PathBuf;
355355

356356
use super::*;
357357
use claurst_core::{GoalError, GoalStore};
358358

359-
static DEFAULT_GOAL_STORE_ENV_LOCK: Mutex<()> = Mutex::new(());
360-
361359
fn goal_path(dir: &tempfile::TempDir) -> PathBuf {
362360
dir.path().join("goals.sqlite")
363361
}
@@ -524,10 +522,9 @@ mod tests {
524522

525523
#[test]
526524
fn default_wrapper_reports_concurrent_pause_at_runaway_boundary() {
527-
let _env_lock = DEFAULT_GOAL_STORE_ENV_LOCK.lock().unwrap();
525+
let env_lock = crate::CONFIG_HOME_ENV_LOCK.blocking_lock();
528526
let dir = tempfile::tempdir().unwrap();
529-
let previous_home = std::env::var_os("COVEN_CODE_HOME");
530-
std::env::set_var("COVEN_CODE_HOME", dir.path());
527+
let _home = crate::ConfigHomeOverride::set(&env_lock, dir.path());
531528

532529
let path = GoalStore::default_path();
533530
let store = GoalStore::open(&path).unwrap();
@@ -536,11 +533,6 @@ mod tests {
536533

537534
let decision = check_and_continue_goal("session", 999, 1);
538535
assert_terminal_stop(decision, GoalStatus::Paused);
539-
540-
match previous_home {
541-
Some(home) => std::env::set_var("COVEN_CODE_HOME", home),
542-
None => std::env::remove_var("COVEN_CODE_HOME"),
543-
}
544536
}
545537

546538
#[test]
@@ -570,10 +562,9 @@ mod tests {
570562

571563
#[test]
572564
fn default_wrapper_does_not_pause_a_replacement_at_runaway_boundary() {
573-
let _env_lock = DEFAULT_GOAL_STORE_ENV_LOCK.lock().unwrap();
565+
let env_lock = crate::CONFIG_HOME_ENV_LOCK.blocking_lock();
574566
let dir = tempfile::tempdir().unwrap();
575-
let previous_home = std::env::var_os("COVEN_CODE_HOME");
576-
std::env::set_var("COVEN_CODE_HOME", dir.path());
567+
let _home = crate::ConfigHomeOverride::set(&env_lock, dir.path());
577568

578569
let path = GoalStore::default_path();
579570
let store = GoalStore::open(&path).unwrap();
@@ -582,11 +573,6 @@ mod tests {
582573

583574
let decision = check_and_continue_goal("session", 999, 1);
584575

585-
match previous_home {
586-
Some(home) => std::env::set_var("COVEN_CODE_HOME", home),
587-
None => std::env::remove_var("COVEN_CODE_HOME"),
588-
}
589-
590576
assert!(matches!(
591577
decision,
592578
GoalContinuation::Stop {
@@ -612,10 +598,9 @@ mod tests {
612598

613599
#[test]
614600
fn default_wrapper_does_not_record_progress_on_a_replacement() {
615-
let _env_lock = DEFAULT_GOAL_STORE_ENV_LOCK.lock().unwrap();
601+
let env_lock = crate::CONFIG_HOME_ENV_LOCK.blocking_lock();
616602
let dir = tempfile::tempdir().unwrap();
617-
let previous_home = std::env::var_os("COVEN_CODE_HOME");
618-
std::env::set_var("COVEN_CODE_HOME", dir.path());
603+
let _home = crate::ConfigHomeOverride::set(&env_lock, dir.path());
619604

620605
let path = GoalStore::default_path();
621606
let store = GoalStore::open(&path).unwrap();
@@ -626,11 +611,6 @@ mod tests {
626611

627612
let decision = check_and_continue_goal("session", 123, 7);
628613

629-
match previous_home {
630-
Some(home) => std::env::set_var("COVEN_CODE_HOME", home),
631-
None => std::env::remove_var("COVEN_CODE_HOME"),
632-
}
633-
634614
assert!(matches!(
635615
decision,
636616
GoalContinuation::Stop {
@@ -658,10 +638,9 @@ mod tests {
658638

659639
#[test]
660640
fn default_wrapper_does_not_budget_limit_a_replacement() {
661-
let _env_lock = DEFAULT_GOAL_STORE_ENV_LOCK.lock().unwrap();
641+
let env_lock = crate::CONFIG_HOME_ENV_LOCK.blocking_lock();
662642
let dir = tempfile::tempdir().unwrap();
663-
let previous_home = std::env::var_os("COVEN_CODE_HOME");
664-
std::env::set_var("COVEN_CODE_HOME", dir.path());
643+
let _home = crate::ConfigHomeOverride::set(&env_lock, dir.path());
665644

666645
let path = GoalStore::default_path();
667646
let store = GoalStore::open(&path).unwrap();
@@ -672,11 +651,6 @@ mod tests {
672651

673652
let decision = check_and_continue_goal("session", 100, 7);
674653

675-
match previous_home {
676-
Some(home) => std::env::set_var("COVEN_CODE_HOME", home),
677-
None => std::env::remove_var("COVEN_CODE_HOME"),
678-
}
679-
680654
assert!(matches!(
681655
decision,
682656
GoalContinuation::Stop {
@@ -736,10 +710,9 @@ mod tests {
736710

737711
#[test]
738712
fn default_wrappers_terminal_goals_stop_without_recording_progress() {
739-
let _env_lock = DEFAULT_GOAL_STORE_ENV_LOCK.lock().unwrap();
713+
let env_lock = crate::CONFIG_HOME_ENV_LOCK.blocking_lock();
740714
let dir = tempfile::tempdir().unwrap();
741-
let previous_home = std::env::var_os("COVEN_CODE_HOME");
742-
std::env::set_var("COVEN_CODE_HOME", dir.path());
715+
let _home = crate::ConfigHomeOverride::set(&env_lock, dir.path());
743716

744717
let path = GoalStore::default_path();
745718
let store = GoalStore::open(&path).unwrap();
@@ -759,11 +732,6 @@ mod tests {
759732
assert_terminal_stop(decision, status);
760733
assert_progress_unchanged(&store, session_id);
761734
}
762-
763-
match previous_home {
764-
Some(home) => std::env::set_var("COVEN_CODE_HOME", home),
765-
None => std::env::remove_var("COVEN_CODE_HOME"),
766-
}
767735
}
768736

769737
#[test]

‎src-rust/crates/query/src/lib.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@ tokio::task_local! {
125125
static TEST_STREAM_STALL_TIMEOUT: std::time::Duration;
126126
}
127127

128+
/// Held by every test in this crate that changes `COVEN_CODE_HOME` or needs
129+
/// it to stay the same across calls, since tests share one process
130+
/// environment.
131+
#[cfg(test)]
132+
static CONFIG_HOME_ENV_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
133+
134+
/// Points `COVEN_CODE_HOME` at a directory until dropped, then puts back what
135+
/// was there, also when the test panics. Setting it takes the
136+
/// [`CONFIG_HOME_ENV_LOCK`] guard, which must outlive it; declare it after
137+
/// the directory so it drops first.
138+
#[cfg(test)]
139+
struct ConfigHomeOverride(Option<std::ffi::OsString>);
140+
141+
#[cfg(test)]
142+
impl ConfigHomeOverride {
143+
fn set(_lock: &tokio::sync::MutexGuard<'_, ()>, home: &std::path::Path) -> Self {
144+
let previous = std::env::var_os("COVEN_CODE_HOME");
145+
std::env::set_var("COVEN_CODE_HOME", home);
146+
Self(previous)
147+
}
148+
}
149+
150+
#[cfg(test)]
151+
impl Drop for ConfigHomeOverride {
152+
fn drop(&mut self) {
153+
match self.0.take() {
154+
Some(previous) => std::env::set_var("COVEN_CODE_HOME", previous),
155+
None => std::env::remove_var("COVEN_CODE_HOME"),
156+
}
157+
}
158+
}
159+
128160
// Goal checks bound the next observable request/work boundary. They cannot
129161
// atomically interrupt work already started in another process or a tool.
130162
async fn wait_for_goal_revocation(

‎src-rust/crates/query/src/web_fetch_provenance_tests.rs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,20 @@ async fn web_fetch_provenance_survives_durable_dispatch_and_context_budgeting()
246246
_ => false,
247247
}));
248248
// This is the actual history API used after an interactive query returns,
249-
// not a serialization of the separate durable event above.
249+
// not a serialization of the separate durable event above. It resolves the
250+
// config home on each call, so the home must not change in between, and
251+
// the session belongs in a home of its own.
250252
let mut session = claurst_core::history::ConversationSession::new("scripted".into());
251253
session.messages = working_messages;
252-
claurst_core::history::save_session(&session).await.unwrap();
253-
let resumed = claurst_core::history::load_session(&session.id)
254-
.await
255-
.unwrap();
254+
let resumed = {
255+
let env_lock = CONFIG_HOME_ENV_LOCK.lock().await;
256+
let home = tempfile::tempdir().unwrap();
257+
let _home = ConfigHomeOverride::set(&env_lock, home.path());
258+
claurst_core::history::save_session(&session).await.unwrap();
259+
claurst_core::history::load_session(&session.id)
260+
.await
261+
.unwrap()
262+
};
256263
let restored = resumed
257264
.messages
258265
.iter()

0 commit comments

Comments
 (0)