use std::sync::{Mutex, OnceLock};
use codewhale_config::WorkshopConfig;
static ACTIVE_WORKSHOP: OnceLock<Mutex<WorkshopConfig>> = OnceLock::new();
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
static ACTIVE_WORKSHOP_TEST_SERIAL: OnceLock<Mutex<()>> = OnceLock::new();
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
std::thread_local! {
static ACTIVE_WORKSHOP_TEST_SERIAL_HELD: std::cell::Cell<bool> = const {
std::cell::Cell::new(false)
};
}
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
pub struct ActiveWorkshopTestGuard {
_serial: std::sync::MutexGuard<'static, ()>,
_env: Option<crate::test_support::TestEnvLock>,
}
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
impl Drop for ActiveWorkshopTestGuard {
fn drop(&mut self) {
ACTIVE_WORKSHOP_TEST_SERIAL_HELD.with(|held| held.set(false));
}
}
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
pub fn active_workshop_test_guard() -> ActiveWorkshopTestGuard {
assert!(
!ACTIVE_WORKSHOP_TEST_SERIAL_HELD.with(std::cell::Cell::get),
"active workshop test guard is not reentrant"
);
let env = if crate::test_support::current_thread_holds_test_env_lock() {
None
} else {
Some(crate::test_support::lock_test_env())
};
let serial = ACTIVE_WORKSHOP_TEST_SERIAL
.get_or_init(|| Mutex::new(()))
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
ACTIVE_WORKSHOP_TEST_SERIAL_HELD.with(|held| held.set(true));
ActiveWorkshopTestGuard {
_serial: serial,
_env: env,
}
}
fn active_workshop_slot() -> &'static Mutex<WorkshopConfig> {
ACTIVE_WORKSHOP.get_or_init(|| Mutex::new(WorkshopConfig::default()))
}
pub fn install_active_workshop(config: Option<&WorkshopConfig>) -> WorkshopConfig {
#[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))]
let _test_serial = if ACTIVE_WORKSHOP_TEST_SERIAL_HELD.with(std::cell::Cell::get) {
None
} else {
Some(
ACTIVE_WORKSHOP_TEST_SERIAL
.get_or_init(|| Mutex::new(()))
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner),
)
};
let snapshot = config.cloned().unwrap_or_default();
let mut slot = active_workshop_slot()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
*slot = snapshot;
slot.clone()
}
#[must_use]
pub fn active_read_result_max_bytes() -> Option<usize> {
active_workshop_slot()
.lock()
.ok()
.and_then(|cfg| cfg.read_result_max_bytes.filter(|n| *n > 0))
}
#[must_use]
pub fn active_tool_result_max_bytes() -> Option<usize> {
active_workshop_slot()
.lock()
.ok()
.and_then(|cfg| cfg.tool_result_max_bytes.filter(|n| *n > 0))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn workshop_byte_budgets_raise_floor_only() {
let _guard = active_workshop_test_guard();
let installed = install_active_workshop(Some(&WorkshopConfig {
large_output_threshold_tokens: None,
per_tool_thresholds: None,
read_result_max_bytes: Some(102_400),
tool_result_max_bytes: Some(80_000),
}));
assert_eq!(installed.read_result_max_bytes, Some(102_400));
assert_eq!(installed.tool_result_max_bytes, Some(80_000));
assert_eq!(active_read_result_max_bytes(), Some(102_400));
assert_eq!(active_tool_result_max_bytes(), Some(80_000));
let cleared = install_active_workshop(None);
assert_eq!(cleared.read_result_max_bytes, None);
assert_eq!(cleared.tool_result_max_bytes, None);
assert_eq!(active_read_result_max_bytes(), None);
assert_eq!(active_tool_result_max_bytes(), None);
}
}