From 560d111005271837fbd584a761b9912b6557d8ab Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 19:43:14 +0900 Subject: [PATCH 1/6] feat(binding): keep engine state per session and swap it into the globals Two apps in one process shared the single process-wide engine state. Gather the persistent state into values that can be exchanged with the live globals: - css::engine_state::CssState holds the prefix, debug flag, atom hoist threshold and plan, slot reservation, class map, file map, canonical map, file routes, custom shorthands, theme tokens and Tailwind definitions. css::test_state::TestStateGuard is now built on it. - extractor::ExtractorCaches holds the vanilla-extract imported runs and stripped TypeScript of the calling thread. - devup_ui_binding_core::session keeps session 0 live from the start and parks every other session's EngineState (CSS state + stylesheet). select_session swaps a parked state with the live globals, with_session runs a closure under the session lock and swaps back, also on unwind. Extractor caches are parked per thread and aligned on every switch, since they hold Rc values and session file numbers. - PerSession keys host values by the live session; the WASM and native module resolvers use it. Existing call sites read the globals unchanged; until a host creates a session the engine behaves as before. Refs #686 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_engine_sessions_state.json | 8 + Cargo.lock | 1 + bindings/devup-ui-binding/src/host.rs | 15 +- bindings/devup-ui-wasm/src/lib.rs | 10 +- bindings/devup-ui-wasm/src/reservation.rs | 22 +- bindings/devup-ui-wasm/src/state.rs | 9 +- bindings/devup-ui-wasm/src/state/tests.rs | 2 +- libs/binding/Cargo.toml | 3 + libs/binding/src/lib.rs | 7 + libs/binding/src/session.rs | 240 ++++++++++++++++++ libs/binding/src/session/tests.rs | 238 +++++++++++++++++ libs/css/src/engine_state.rs | 69 +++++ libs/css/src/file_map.rs | 4 +- libs/css/src/file_routes.rs | 2 +- libs/css/src/lib.rs | 3 +- libs/css/src/test_state.rs | 68 +---- libs/css/src/test_state/tests.rs | 2 + libs/extractor/src/lib.rs | 1 + libs/extractor/src/vanilla_extract.rs | 31 ++- 19 files changed, 638 insertions(+), 97 deletions(-) create mode 100644 .changepacks/changepack_log_engine_sessions_state.json create mode 100644 libs/binding/src/session.rs create mode 100644 libs/binding/src/session/tests.rs create mode 100644 libs/css/src/engine_state.rs diff --git a/.changepacks/changepack_log_engine_sessions_state.json b/.changepacks/changepack_log_engine_sessions_state.json new file mode 100644 index 000000000..1ffc362ab --- /dev/null +++ b/.changepacks/changepack_log_engine_sessions_state.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-wasm/package.json": "Patch", + "bindings/devup-ui-binding/package.json": "Patch" + }, + "note": "The engine keeps its state per engine session: class and file numbering, routes, prefix, debug, atom hoisting, slot reservation, shorthands, theme tokens, Tailwind definitions, the stylesheet, the stylesheet caches and the module resolver move together when a session is switched. Session 0 is the only one until a host creates another, so a single app sees no change", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/Cargo.lock b/Cargo.lock index fc7ea0d27..e36f2f33a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,6 +598,7 @@ dependencies = [ "css", "extractor", "serde_json", + "serial_test", "sheet", ] diff --git a/bindings/devup-ui-binding/src/host.rs b/bindings/devup-ui-binding/src/host.rs index 4ec9812c2..7b8a7aa5d 100644 --- a/bindings/devup-ui-binding/src/host.rs +++ b/bindings/devup-ui-binding/src/host.rs @@ -16,20 +16,23 @@ pub struct Resolved { pub code: String, } type Resolver = FunctionRef, Option>>; -struct State(Option>); +/// The resolver each engine session set in this environment +struct State(core::PerSession>); /// Initialize environment-local ownership without overwriting existing state. #[napi] pub fn init(env: Env) -> Result<()> { if env.get_instance_data::()?.is_none() { - env.set_instance_data(State(None), (), |context| drop(context.value))?; + env.set_instance_data(State(core::PerSession::new()), (), |context| { + drop(context.value); + })?; } Ok(()) } /// Replace the owned synchronous resolver in the current environment only. #[napi] pub fn set_resolver(env: Env, resolver: Option) -> Result<()> { - state(&env)?.0 = resolver.map(Rc::new); + state(&env)?.0.set_live(resolver.map(Rc::new)); Ok(()) } fn state(env: &Env) -> Result<&'static mut State> { @@ -41,10 +44,10 @@ fn state(env: &Env) -> Result<&'static mut State> { pub fn reset(env: Env, full: bool) -> Result<()> { let state = state(&env)?; if full { - core::reset_state_internal(|| state.0 = None); + core::reset_state_internal(|| state.0 = core::PerSession::new()); } else { core::reset_build_state_internal(); - state.0 = None; + state.0.set_live(None); } Ok(()) } @@ -56,7 +59,7 @@ fn with_resolver( &dyn Fn() -> std::result::Result<(), String>, ) -> Result, ) -> Result { - let current = state(&env)?.0.clone(); + let current = state(&env)?.0.live(); let diagnostics = Rc::new(RefCell::new(diagnostics)); let failure = Rc::new(RefCell::new(None)); let resolver = current.map(|current| { diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index 6c2e7ed92..2831f375d 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -38,7 +38,9 @@ mod css_output_edge_tests; mod dynamic_as_sibling_props_tests; thread_local! { - static MODULE_RESOLVER: RefCell> = const { RefCell::new(None) }; + /// The resolver `setModuleResolver` set in each engine session + static MODULE_RESOLVER: RefCell> = + const { RefCell::new(binding_core::PerSession::new()) }; } #[cfg(not(tarpaulin_include))] @@ -302,7 +304,7 @@ pub fn seed_file_map(files: Vec) { /// alone. Theme, shorthands and debug mode are set by every build, and stay. pub fn reset_build_state_internal() { binding_core::reset_build_state_internal(); - MODULE_RESOLVER.with_borrow_mut(|current| *current = None); + MODULE_RESOLVER.with_borrow_mut(|current| current.set_live(None)); } #[wasm_bindgen(js_name = "resetBuildState")] @@ -464,7 +466,7 @@ fn code_extract_internal_impl( #[wasm_bindgen(js_name = "setModuleResolver")] #[cfg(not(tarpaulin_include))] pub fn set_module_resolver(resolver: Option) { - MODULE_RESOLVER.with_borrow_mut(|current| *current = resolver); + MODULE_RESOLVER.with_borrow_mut(|current| current.set_live(resolver)); } #[cfg(not(tarpaulin_include))] @@ -529,7 +531,7 @@ fn call_module_resolver( /// The resolver set by `setModuleResolver`, if any #[cfg(not(tarpaulin_include))] fn resolver_from_js(state: std::rc::Rc>) -> Option> { - let resolver = MODULE_RESOLVER.with_borrow(Clone::clone)?; + let resolver = MODULE_RESOLVER.with_borrow(binding_core::PerSession::live)?; Some(Box::new(move |specifier: &str, importer: &str| { let request = state.borrow().request(specifier, importer)?; call_module_resolver(&resolver, request, &state) diff --git a/bindings/devup-ui-wasm/src/reservation.rs b/bindings/devup-ui-wasm/src/reservation.rs index f3c692439..baf3cb9b4 100644 --- a/bindings/devup-ui-wasm/src/reservation.rs +++ b/bindings/devup-ui-wasm/src/reservation.rs @@ -37,16 +37,18 @@ pub fn reserve_class_slots( import_aliases: import_aliases_from_js(import_aliases, 5)?, ..ExtractOption::default() }; - let resolver = MODULE_RESOLVER.with_borrow(Clone::clone).map(|resolver| { - let state = Rc::new(RefCell::new(ResolverState::default())); - for input in &inputs { - state.borrow_mut().add_source(&input.filename, &input.code); - } - move |specifier: &str, importer: &str| { - let request = state.borrow().request(specifier, importer)?; - call_module_resolver(&resolver, request, &state) - } - }); + let resolver = MODULE_RESOLVER + .with_borrow(devup_ui_binding_core::PerSession::live) + .map(|resolver| { + let state = Rc::new(RefCell::new(ResolverState::default())); + for input in &inputs { + state.borrow_mut().add_source(&input.filename, &input.code); + } + move |specifier: &str, importer: &str| { + let request = state.borrow().request(specifier, importer)?; + call_module_resolver(&resolver, request, &state) + } + }); reserve_class_slots_internal( &inputs, option, diff --git a/bindings/devup-ui-wasm/src/state.rs b/bindings/devup-ui-wasm/src/state.rs index d503fad73..9b42bc82a 100644 --- a/bindings/devup-ui-wasm/src/state.rs +++ b/bindings/devup-ui-wasm/src/state.rs @@ -4,7 +4,7 @@ use wasm_bindgen::prelude::*; /// Never call during extraction or a resolver callback, or between build environments. pub fn reset_state_internal() { devup_ui_binding_core::reset_state_internal(|| { - super::MODULE_RESOLVER.with_borrow_mut(|resolver| *resolver = None); + super::MODULE_RESOLVER.with_borrow_mut(|resolver| *resolver = Default::default()); }); } @@ -17,7 +17,7 @@ pub fn reset_state_for_testing() { #[cfg(test)] pub(crate) struct TestStateGuard { sheet: sheet::StyleSheet, - resolver: Option, + resolver: devup_ui_binding_core::PerSession, _engine: extractor::test_state::TestStateGuard, } @@ -26,7 +26,7 @@ impl TestStateGuard { pub(crate) fn new() -> Self { Self { sheet: super::with_style_sheet_mut(std::mem::take), - resolver: super::MODULE_RESOLVER.with_borrow_mut(Option::take), + resolver: super::MODULE_RESOLVER.with_borrow_mut(std::mem::take), _engine: extractor::test_state::TestStateGuard::new(), } } @@ -36,6 +36,7 @@ impl TestStateGuard { impl Drop for TestStateGuard { fn drop(&mut self) { super::with_style_sheet_mut(|sheet| *sheet = std::mem::take(&mut self.sheet)); - super::MODULE_RESOLVER.with_borrow_mut(|resolver| *resolver = self.resolver.take()); + super::MODULE_RESOLVER + .with_borrow_mut(|resolver| *resolver = std::mem::take(&mut self.resolver)); } } diff --git a/bindings/devup-ui-wasm/src/state/tests.rs b/bindings/devup-ui-wasm/src/state/tests.rs index d9b2bb1d9..20c08efcb 100644 --- a/bindings/devup-ui-wasm/src/state/tests.rs +++ b/bindings/devup-ui-wasm/src/state/tests.rs @@ -52,7 +52,7 @@ fn reset_returns_fresh_extraction_when_engine_is_dirty() { reset_state_for_testing(); assert_eq!(export_sheet_internal().unwrap(), empty_sheet); assert_eq!(get_default_theme().unwrap(), None); - assert!(MODULE_RESOLVER.with_borrow(Option::is_none)); + assert!(MODULE_RESOLVER.with_borrow(|resolver| resolver.live().is_none())); assert_eq!(get_prefix(), None); assert!(!is_debug()); assert_eq!(css::atom_hoist::atom_hoist_threshold(), None); diff --git a/libs/binding/Cargo.toml b/libs/binding/Cargo.toml index aa50e11f4..bf580965a 100644 --- a/libs/binding/Cargo.toml +++ b/libs/binding/Cargo.toml @@ -18,3 +18,6 @@ sheet = { path = "../sheet" } css = { path = "../css" } serde_json = "1.0.154" bimap = { version = "0.6.3", features = ["serde"] } + +[dev-dependencies] +serial_test = "4.0.1" diff --git a/libs/binding/src/lib.rs b/libs/binding/src/lib.rs index cc89b9a04..4df2acd16 100644 --- a/libs/binding/src/lib.rs +++ b/libs/binding/src/lib.rs @@ -3,6 +3,7 @@ mod output; mod reservation; mod resolver_state; +pub mod session; pub use css::atom_hoist::set_atom_hoist; pub use css::debug::{is_debug, set_debug}; @@ -13,6 +14,9 @@ pub use extractor::{has_devup_ui, has_devup_ui_with}; pub use output::{CssOptions, ExtractionMode, Output, SourceMapMode, code_extract}; pub use reservation::{ReservationInput, reserve_class_slots_internal}; pub use resolver_state::{ResolverRequest, ResolverState}; +pub use session::{ + PerSession, create_session, drop_session, live_session, select_session, with_session, +}; use css::class_map::{set_class_map, with_class_map}; use css::file_map::{set_canonical_map, set_file_map, with_canonical_map, with_file_map}; @@ -127,7 +131,10 @@ pub fn reset_build_state_internal() { } /// Clear test state at a quiescent boundary, clearing the host resolver before registries. +/// +/// Every engine session but 0 goes, and session 0 starts clean. pub fn reset_state_internal(clear_resolver: impl FnOnce()) { + session::reset_sessions(); with_style_sheet_mut(|sheet| *sheet = StyleSheet::default()); clear_resolver(); css::test_state::reset_state_for_testing(); diff --git a/libs/binding/src/session.rs b/libs/binding/src/session.rs new file mode 100644 index 000000000..f060cbe73 --- /dev/null +++ b/libs/binding/src/session.rs @@ -0,0 +1,240 @@ +//! Engine sessions: independent engine states in one process. +//! +//! The engine's globals always hold one session's state, the live session. +//! Every other session's state is parked here. Switching to a session +//! exchanges its parked state with the live globals, so the code that reads +//! them does not know about sessions. Session 0 exists from the start; until a +//! host creates another, the engine behaves as a single process-wide engine. +//! +//! The parked CSS numbering and stylesheet may cross threads. The extractor's +//! stylesheet caches stay on the thread that filled them, so each thread keeps +//! its own per-session caches and aligns them on every switch. + +use std::cell::{Cell, RefCell}; +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Mutex, MutexGuard, PoisonError}; + +use css::engine_state::CssState; +use extractor::ExtractorCaches; +use sheet::StyleSheet; + +use crate::with_style_sheet_mut; + +/// Everything one session keeps between calls, apart from thread-bound caches: +/// the CSS crate's numbering, registries and settings, and the stylesheet. +#[derive(Default)] +pub struct EngineState { + css: CssState, + sheet: StyleSheet, +} + +impl EngineState { + /// Exchange this state with the live one. + pub fn swap_live(&mut self) { + self.css.swap_live(); + with_style_sheet_mut(|live| std::mem::swap(live, &mut self.sheet)); + } +} + +struct Sessions { + next: u32, + parked: BTreeMap, +} + +#[derive(Default)] +struct ThreadCaches { + live: u32, + parked: BTreeMap, +} + +/// The session whose state the globals hold. +static LIVE: AtomicU32 = AtomicU32::new(0); + +static SESSIONS: Mutex = Mutex::new(Sessions { + next: 1, + parked: BTreeMap::new(), +}); + +/// Held while [`with_session`] runs, so no other thread switches under it. +static RUNNING: Mutex<()> = Mutex::new(()); + +thread_local! { + static CACHES: RefCell = RefCell::default(); + /// Whether this thread is inside [`with_session`] + static INSIDE: Cell = const { Cell::new(false) }; +} + +fn sessions() -> MutexGuard<'static, Sessions> { + SESSIONS.lock().unwrap_or_else(PoisonError::into_inner) +} + +fn running() -> MutexGuard<'static, ()> { + RUNNING.lock().unwrap_or_else(PoisonError::into_inner) +} + +fn unknown(id: u32) -> String { + format!("engine session {id} does not exist: create it with createSession() first") +} + +fn busy(live: u32) -> String { + format!("engine session {live} is running a call: switch sessions only after that call returns") +} + +/// The session whose state is live. +pub fn live_session() -> u32 { + LIVE.load(Ordering::Acquire) +} + +/// Park the live state and make session `id` live, returning the session +/// that was live. +fn switch(id: u32) -> Result { + let live = live_session(); + if id != live { + // Callers hold the session lock, so the state can be swapped unlocked + let mut state = sessions().parked.remove(&id).ok_or_else(|| unknown(id))?; + state.swap_live(); + sessions().parked.insert(live, state); + LIVE.store(id, Ordering::Release); + } + align_caches(id); + Ok(live) +} + +/// Make this thread's live caches those of session `id`. +fn align_caches(id: u32) { + CACHES.with_borrow_mut(|caches| { + if caches.live != id { + let mut next = caches.parked.remove(&id).unwrap_or_default(); + next.swap_live(); + caches.parked.insert(caches.live, next); + caches.live = id; + } + }); +} + +/// Create a session with a clean engine state, returning its id. +/// +/// Ids are never handed out twice, even after the session is dropped. +pub fn create_session() -> u32 { + let mut sessions = sessions(); + let id = sessions.next; + sessions.next = id.saturating_add(1); + sessions.parked.insert(id, EngineState::default()); + id +} + +/// Make session `id` live until another is selected, returning the session +/// that was live. +/// +/// # Errors +/// When the session does not exist, or a [`with_session`] call is running on +/// this thread with another session live. +pub fn select_session(id: u32) -> Result { + if INSIDE.get() { + let live = live_session(); + return if id == live { + Ok(live) + } else { + Err(busy(live)) + }; + } + let _running = running(); + switch(id) +} + +/// Drop session `id` and its state, returning whether it existed. Dropping +/// the live session makes session 0 live first. +/// +/// Caches the session left on other threads go when those threads end. +/// +/// # Errors +/// For session 0, and when the session is live inside a running +/// [`with_session`] call. +pub fn drop_session(id: u32) -> Result { + if id == 0 { + return Err("engine session 0 cannot be dropped".to_string()); + } + if live_session() == id { + select_session(0)?; + } + CACHES.with_borrow_mut(|caches| caches.parked.remove(&id)); + Ok(sessions().parked.remove(&id).is_some()) +} + +/// Switches back to the session that was live, also on unwind. +struct Restore(u32); + +impl Drop for Restore { + fn drop(&mut self) { + INSIDE.set(false); + switch(self.0).ok(); + } +} + +/// Run `f` with session `id` live, then make the previous session live +/// again, holding the session lock throughout. +/// +/// A nested call on the same thread runs directly when `id` is already live. +/// +/// # Errors +/// When the session does not exist, or a nested call names another session. +pub fn with_session(id: u32, f: impl FnOnce() -> R) -> Result { + if INSIDE.get() { + let live = live_session(); + return if id == live { Ok(f()) } else { Err(busy(live)) }; + } + let _running = running(); + let _restore = Restore(switch(id)?); + INSIDE.set(true); + Ok(f()) +} + +/// Forget every session but 0 and make it live, for the full test reset that +/// then clears the live state. +pub fn reset_sessions() { + sessions().parked.clear(); + LIVE.store(0, Ordering::Release); + CACHES.with_borrow_mut(|caches| *caches = ThreadCaches::default()); +} + +/// A value a host adapter keeps for each session itself, such as the module +/// resolver it calls back into: the live session's value is the one in use. +#[derive(Debug)] +pub struct PerSession(BTreeMap); + +impl Default for PerSession { + fn default() -> Self { + Self::new() + } +} + +impl PerSession { + pub const fn new() -> Self { + Self(BTreeMap::new()) + } +} + +impl PerSession { + /// The live session's value. + pub fn live(&self) -> Option { + self.0.get(&live_session()).cloned() + } + + /// Set or clear the live session's value. + pub fn set_live(&mut self, value: Option) { + let live = live_session(); + match value { + Some(value) => self.0.insert(live, value), + None => self.0.remove(&live), + }; + } + + /// Forget a dropped session's value. + pub fn remove(&mut self, id: u32) { + self.0.remove(&id); + } +} + +#[cfg(test)] +mod tests; diff --git a/libs/binding/src/session/tests.rs b/libs/binding/src/session/tests.rs new file mode 100644 index 000000000..89fe1569c --- /dev/null +++ b/libs/binding/src/session/tests.rs @@ -0,0 +1,238 @@ +use super::*; +use crate::{ + ExtractionMode, SourceMapMode, code_extract, get_css, register_theme_internal, with_style_sheet, +}; +use extractor::ExtractOption; +use serial_test::serial; + +/// Clean engine state for the test, and only session 0 afterwards. +struct Clean { + _sheet: SheetGuard, + _engine: extractor::test_state::TestStateGuard, +} + +struct SheetGuard(StyleSheet); + +impl Drop for SheetGuard { + fn drop(&mut self) { + reset_sessions(); + with_style_sheet_mut(|sheet| std::mem::swap(sheet, &mut self.0)); + } +} + +fn clean() -> Clean { + reset_sessions(); + Clean { + _sheet: SheetGuard(with_style_sheet_mut(std::mem::take)), + _engine: extractor::test_state::TestStateGuard::new(), + } +} + +fn extract(filename: &str, color: &str) -> String { + code_extract( + ( + filename, + &format!("import {{Box}} from '@devup-ui/react'; ;"), + ), + ExtractOption { + single_css: true, + ..ExtractOption::default() + }, + ExtractionMode { + source_map: SourceMapMode::Skip, + resolver: None, + resolver_check: None, + import_main_css_in_css: false, + }, + ) + .map_or_else(|error| panic!("{error}"), |output| output.code) +} + +#[test] +#[serial] +fn sessions_keep_their_own_numbering_settings_and_sheet() { + let _clean = clean(); + // Given: session 0 extracted one file under a prefix + css::set_prefix(Some("zero-".into())); + extract("zero.tsx", "red"); + let session = create_session(); + // When: another session extracts the same file differently + let code = with_session(session, || { + assert_eq!(live_session(), session); + assert_eq!(css::get_prefix(), None); + assert_eq!(css::file_map::get_file_map().len(), 0); + register_theme_internal( + serde_json::from_str(r##"{"colors":{"default":{"brand":"#fff"}}}"##) + .unwrap_or_else(|error| panic!("{error}")), + ); + extract("other.tsx", "blue") + }); + // Then: each session reads only its own state + assert_eq!(live_session(), 0); + assert_eq!(css::get_prefix().as_deref(), Some("zero-")); + assert!(get_css(None, false).contains("color:red")); + assert!(!get_css(None, false).contains("color:blue")); + assert!(with_style_sheet(|sheet| sheet.theme.colors.is_empty())); + assert!(code.is_ok_and(|code| code.contains("className=\"a\""))); + let css = with_session(session, || { + assert!( + css::file_map::get_file_map() + .get_by_left("zero.tsx") + .is_none() + ); + get_css(None, false) + }); + assert!(css.is_ok_and(|css| css.contains("color:blue") && css.contains("--brand"))); +} + +#[test] +#[serial] +fn selecting_a_session_keeps_it_live_until_another_is_selected() { + let _clean = clean(); + let first = create_session(); + let second = create_session(); + assert_ne!(first, second); + assert_eq!(select_session(first), Ok(0)); + css::set_prefix(Some("first-".into())); + assert_eq!(select_session(first), Ok(first)); + assert_eq!(select_session(second), Ok(first)); + assert_eq!(css::get_prefix(), None); + assert_eq!(select_session(first), Ok(second)); + assert_eq!(css::get_prefix().as_deref(), Some("first-")); + assert_eq!(select_session(0), Ok(first)); + assert_eq!(css::get_prefix(), None); +} + +#[test] +#[serial] +fn unknown_sessions_are_refused() { + let _clean = clean(); + let message = unknown(42); + assert_eq!(select_session(42), Err(message.clone())); + assert_eq!(with_session(42, || ()), Err(message)); + assert_eq!(live_session(), 0); +} + +#[test] +#[serial] +fn nested_calls_may_not_switch_sessions() { + let _clean = clean(); + let session = create_session(); + let nested = with_session(session, || { + ( + with_session(session, live_session), + with_session(0, live_session), + select_session(session), + select_session(0), + drop_session(session), + ) + }); + assert_eq!( + nested, + Ok(( + Ok(session), + Err(busy(session)), + Ok(session), + Err(busy(session)), + Err(busy(session)), + )) + ); + assert_eq!(live_session(), 0); +} + +#[test] +#[serial] +fn a_panicking_call_still_switches_back() { + let _clean = clean(); + let session = create_session(); + let result = std::panic::catch_unwind(|| { + with_session(session, || { + css::set_prefix(Some("lost-".into())); + panic!("call failed"); + }) + }); + assert!(result.is_err()); + assert_eq!(live_session(), 0); + assert_eq!(css::get_prefix(), None); + assert_eq!( + with_session(session, css::get_prefix), + Ok(Some("lost-".into())) + ); +} + +#[test] +#[serial] +fn dropping_sessions() { + let _clean = clean(); + assert_eq!( + drop_session(0), + Err("engine session 0 cannot be dropped".to_string()) + ); + let parked = create_session(); + let live = create_session(); + assert_eq!(drop_session(parked), Ok(true)); + assert_eq!(drop_session(parked), Ok(false)); + assert_eq!(select_session(parked), Err(unknown(parked))); + css::set_prefix(Some("zero-".into())); + assert_eq!(select_session(live), Ok(0)); + // Dropping the live session makes session 0 live again + assert_eq!(drop_session(live), Ok(true)); + assert_eq!(live_session(), 0); + assert_eq!(css::get_prefix().as_deref(), Some("zero-")); + // Ids are not handed out twice + assert!(create_session() > live); +} + +#[test] +#[serial] +fn caches_follow_the_session_on_each_thread() { + let _clean = clean(); + let session = create_session(); + assert_eq!(select_session(session), Ok(0)); + // Another thread aligns its own caches when it switches + let other = std::thread::spawn(move || { + let selected = (select_session(session), select_session(0)); + CACHES.with_borrow(|caches| (selected, caches.live, caches.parked.len())) + }) + .join(); + assert!(other.is_ok_and(|other| other == ((Ok(session), Ok(session)), 0, 1))); + assert_eq!(live_session(), 0); + CACHES.with_borrow(|caches| assert_eq!((caches.live, caches.parked.len()), (session, 1))); + assert_eq!(select_session(0), Ok(0)); + CACHES.with_borrow(|caches| assert_eq!((caches.live, caches.parked.len()), (0, 1))); +} + +#[test] +#[serial] +fn per_session_values_follow_the_live_session() { + let _clean = clean(); + let mut values = PerSession::::default(); + values.set_live(Some(1)); + let session = create_session(); + assert_eq!( + with_session(session, || { + let before = values.live(); + values.set_live(Some(2)); + (before, values.live()) + }), + Ok((None, Some(2))) + ); + assert_eq!(values.live(), Some(1)); + values.remove(0); + assert_eq!(values.live(), None); + values.set_live(Some(3)); + values.set_live(None); + assert_eq!(values.live(), None); + assert!(format!("{values:?}").contains('2')); +} + +#[test] +#[serial] +fn full_reset_forgets_every_session() { + let _clean = clean(); + let session = create_session(); + assert_eq!(select_session(session), Ok(0)); + crate::reset_state_internal(|| {}); + assert_eq!(live_session(), 0); + assert_eq!(select_session(session), Err(unknown(session))); +} diff --git a/libs/css/src/engine_state.rs b/libs/css/src/engine_state.rs new file mode 100644 index 000000000..b4752fc82 --- /dev/null +++ b/libs/css/src/engine_state.rs @@ -0,0 +1,69 @@ +//! Everything the CSS crate keeps between calls, held as one value so an +//! engine session can own it while another session's state is live. + +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::sync::atomic::Ordering; + +use crate::{ + atom_hoist, class_map, debug, file_map, file_routes, slot_reservation, tailwind_definitions, + theme_tokens, +}; + +/// The prefix, debug flag, atom hoisting, class and file numbering, routes, +/// shorthands, theme tokens and Tailwind definitions of one engine session. +/// +/// The default value is a clean engine, as after a full reset. +#[derive(Default)] +pub struct CssState { + prefix: Option, + debug: bool, + hoist: Option, + plan: Option>, + reservation: slot_reservation::State, + classes: HashMap>, + files: bimap::BiHashMap, + canonical: HashMap, + routes: HashMap>, + shorthands: (BTreeMap>, bool), + tokens: theme_tokens::ThemeTokenRegistry, + tailwind: tailwind_definitions::Definitions, +} + +impl CssState { + /// Exchange this state with the live one: afterwards the live engine runs + /// on what this value held, and this value holds what was live. + pub fn swap_live(&mut self) { + let prefix = crate::get_prefix(); + crate::set_prefix(std::mem::replace(&mut self.prefix, prefix)); + let debug = debug::is_debug(); + debug::set_debug(std::mem::replace(&mut self.debug, debug)); + let hoist = atom_hoist::atom_hoist_threshold(); + atom_hoist::set_atom_hoist(std::mem::replace(&mut self.hoist, hoist)); + let plan = atom_hoist::atom_plan(); + atom_hoist::restore_atom_plan(std::mem::replace(&mut self.plan, plan)); + self.reservation = slot_reservation::replace_state(self.reservation); + class_map::with_class_map_mut(|live| std::mem::swap(live, &mut self.classes)); + file_map::with_file_map_mut(|live| std::mem::swap(live, &mut self.files)); + file_map::with_canonical_map_mut(|live| std::mem::swap(live, &mut self.canonical)); + file_routes::with_file_routes_mut(|live| std::mem::swap(live, &mut self.routes)); + self.shorthands = replace_shorthands(std::mem::take(&mut self.shorthands)); + self.tokens = theme_tokens::replace_registry(std::mem::take(&mut self.tokens)); + self.tailwind = + tailwind_definitions::replace_definitions(std::mem::take(&mut self.tailwind)); + } +} + +/// Install `state` as the live custom shorthands, returning the previous ones +/// and clearing a poisoned lock. +pub(crate) fn replace_shorthands( + state: (BTreeMap>, bool), +) -> (BTreeMap>, bool) { + let mut registry = crate::CUSTOM_SHORTHANDS + .write() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let old = std::mem::replace(&mut *registry, state.0); + let present = crate::HAS_CUSTOM_SHORTHANDS.swap(state.1, Ordering::Relaxed); + drop(registry); + crate::CUSTOM_SHORTHANDS.clear_poison(); + (old, present) +} diff --git a/libs/css/src/file_map.rs b/libs/css/src/file_map.rs index 24faf84a0..92cdd05f2 100644 --- a/libs/css/src/file_map.rs +++ b/libs/css/src/file_map.rs @@ -38,7 +38,7 @@ where } #[inline] -fn with_file_map_mut(f: F) -> R +pub(crate) fn with_file_map_mut(f: F) -> R where F: FnOnce(&mut BiHashMap) -> R, { @@ -150,7 +150,7 @@ where } #[inline] -fn with_canonical_map_mut(f: F) -> R +pub(crate) fn with_canonical_map_mut(f: F) -> R where F: FnOnce(&mut std::collections::HashMap) -> R, { diff --git a/libs/css/src/file_routes.rs b/libs/css/src/file_routes.rs index bb16a8154..367407bde 100644 --- a/libs/css/src/file_routes.rs +++ b/libs/css/src/file_routes.rs @@ -42,7 +42,7 @@ where } #[inline] -fn with_file_routes_mut(f: F) -> R +pub(crate) fn with_file_routes_mut(f: F) -> R where F: FnOnce(&mut HashMap>) -> R, { diff --git a/libs/css/src/lib.rs b/libs/css/src/lib.rs index 00dcc341d..cf10b9305 100644 --- a/libs/css/src/lib.rs +++ b/libs/css/src/lib.rs @@ -6,6 +6,7 @@ pub mod atom_name; pub mod class_map; mod constant; pub mod debug; +pub mod engine_state; pub mod file_map; pub mod file_routes; pub mod is_special_property; @@ -267,7 +268,7 @@ static HAS_CUSTOM_SHORTHANDS: AtomicBool = AtomicBool::new(false); /// Clear custom aliases at a quiescent test boundary, including the fast-path flag. pub fn reset_custom_shorthands() { - test_state::replace_shorthands((BTreeMap::new(), false)); + engine_state::replace_shorthands((BTreeMap::new(), false)); } /// Replace the custom shorthand registry used by style extraction. diff --git a/libs/css/src/test_state.rs b/libs/css/src/test_state.rs index 4a0929e5c..597b2850b 100644 --- a/libs/css/src/test_state.rs +++ b/libs/css/src/test_state.rs @@ -2,29 +2,14 @@ //! they provide cleanup, not mutual exclusion. Keep scopes on their creating thread //! and use them only when extraction and callbacks are quiescent. -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::marker::PhantomData; use std::rc::Rc; -use std::sync::atomic::Ordering; use crate::{ - atom_hoist, class_map, debug, file_map, file_routes, slot_reservation, tailwind_definitions, - theme_tokens, + atom_hoist, class_map, debug, engine_state::CssState, file_map, file_routes, slot_reservation, + tailwind_definitions, theme_tokens, }; -pub(crate) fn replace_shorthands( - state: (BTreeMap>, bool), -) -> (BTreeMap>, bool) { - let mut registry = crate::CUSTOM_SHORTHANDS - .write() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let old = std::mem::replace(&mut *registry, state.0); - let present = crate::HAS_CUSTOM_SHORTHANDS.swap(state.1, Ordering::Relaxed); - drop(registry); - crate::CUSTOM_SHORTHANDS.clear_poison(); - (old, present) -} - /// Reset CSS-owned persistent state without retaining a snapshot. pub fn reset_state_for_testing() { crate::set_prefix(None); @@ -45,41 +30,19 @@ pub fn reset_state_for_testing() { /// including unwind and nested scopes. Hold a named binding and the serial lock. #[must_use] pub struct TestStateGuard { - prefix: Option, - debug: bool, - hoist: Option, - plan: Option>, - reservation: slot_reservation::State, - classes: HashMap>, - files: bimap::BiHashMap, - canonical: HashMap, - routes: HashMap>, - shorthands: (BTreeMap>, bool), - tokens: theme_tokens::ThemeTokenRegistry, - tailwind: tailwind_definitions::Definitions, + outer: CssState, same_thread: PhantomData>, } impl TestStateGuard { - /// Snapshot owned values; no state lock is held during the test body. + /// Move the live state into the guard; no state lock is held during the test body. pub fn new() -> Self { - let guard = Self { - prefix: crate::get_prefix(), - debug: debug::is_debug(), - hoist: atom_hoist::atom_hoist_threshold(), - plan: atom_hoist::atom_plan(), - reservation: slot_reservation::replace_state(Default::default()), - classes: class_map::get_class_map(), - files: file_map::get_file_map(), - canonical: file_map::get_canonical_map(), - routes: file_routes::get_file_routes(), - shorthands: replace_shorthands((BTreeMap::new(), false)), - tokens: theme_tokens::replace_registry(Default::default()), - tailwind: tailwind_definitions::replace_definitions(Default::default()), + let mut outer = CssState::default(); + outer.swap_live(); + Self { + outer, same_thread: PhantomData, - }; - reset_state_for_testing(); - guard + } } } @@ -91,18 +54,7 @@ impl Default for TestStateGuard { impl Drop for TestStateGuard { fn drop(&mut self) { - crate::set_prefix(self.prefix.take()); - debug::set_debug(self.debug); - atom_hoist::set_atom_hoist(self.hoist); - atom_hoist::restore_atom_plan(self.plan.take()); - slot_reservation::replace_state(std::mem::take(&mut self.reservation)); - class_map::set_class_map(std::mem::take(&mut self.classes)); - file_map::set_file_map(std::mem::take(&mut self.files)); - file_map::set_canonical_map(std::mem::take(&mut self.canonical)); - file_routes::set_file_routes(std::mem::take(&mut self.routes)); - replace_shorthands(std::mem::take(&mut self.shorthands)); - theme_tokens::replace_registry(std::mem::take(&mut self.tokens)); - tailwind_definitions::replace_definitions(std::mem::take(&mut self.tailwind)); + self.outer.swap_live(); } } diff --git a/libs/css/src/test_state/tests.rs b/libs/css/src/test_state/tests.rs index 82057e0f9..57805e6a8 100644 --- a/libs/css/src/test_state/tests.rs +++ b/libs/css/src/test_state/tests.rs @@ -1,5 +1,7 @@ use super::*; use serial_test::serial; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::sync::atomic::Ordering; fn seed() { crate::set_prefix(Some("outer".into())); diff --git a/libs/extractor/src/lib.rs b/libs/extractor/src/lib.rs index 71b38efe2..30a1db9f4 100644 --- a/libs/extractor/src/lib.rs +++ b/libs/extractor/src/lib.rs @@ -148,6 +148,7 @@ mod utf16_boundary_tests; mod util_type; mod utils; mod vanilla_extract; +pub use vanilla_extract::ExtractorCaches; mod visit; mod dead_properties; diff --git a/libs/extractor/src/vanilla_extract.rs b/libs/extractor/src/vanilla_extract.rs index a501b6da3..2859980bc 100644 --- a/libs/extractor/src/vanilla_extract.rs +++ b/libs/extractor/src/vanilla_extract.rs @@ -514,27 +514,38 @@ fn sandbox_error( type ImportedRuns = FxHashMap; -#[derive(Debug)] -pub(crate) struct CacheGuard { +/// The stylesheet runs and stripped TypeScript one engine session cached on +/// the calling thread: the runs carry that session's file numbers, so two +/// sessions never share them. +#[derive(Debug, Default)] +pub struct ExtractorCaches { imported: ImportedRuns, stripped: FxHashMap)>, - same_thread: std::marker::PhantomData>, } +impl ExtractorCaches { + /// Exchange these caches with the calling thread's live ones. + pub fn swap_live(&mut self) { + IMPORTED_RUNS.with_borrow_mut(|runs| std::mem::swap(runs, &mut self.imported)); + STRIPPED.with_borrow_mut(|stripped| std::mem::swap(stripped, &mut self.stripped)); + } +} + +/// Empty caches until dropped, when the outer ones return. +#[derive(Debug)] +pub(crate) struct CacheGuard(ExtractorCaches); + impl Default for CacheGuard { fn default() -> Self { - Self { - imported: IMPORTED_RUNS.with_borrow_mut(std::mem::take), - stripped: STRIPPED.with_borrow_mut(std::mem::take), - same_thread: std::marker::PhantomData, - } + let mut outer = ExtractorCaches::default(); + outer.swap_live(); + Self(outer) } } impl Drop for CacheGuard { fn drop(&mut self) { - IMPORTED_RUNS.with_borrow_mut(|runs| *runs = std::mem::take(&mut self.imported)); - STRIPPED.with_borrow_mut(|stripped| *stripped = std::mem::take(&mut self.stripped)); + self.0.swap_live(); } } From c5c0cfdbeca644398243986f09e1523542a29f3c Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 19:51:59 +0900 Subject: [PATCH 2/6] feat(binding): expose createSession, selectSession and dropSession Add the engine session calls to the WASM and native engines: - createSession() returns the id of a new session with a clean state - selectSession(id) makes it live; every later call works on its state - dropSession(id) drops it with its module resolver, returning whether it existed; dropping the live session makes session 0 live The shared argument boundary checks the id as an unsigned 32-bit integer before engine entry. The ABI grows from 29 to 32 names: the WASM glue check, the native adapter and typings, the e2e ABI list, the validation positions (47 in 20 functions, 283 invalid-input cases) and a parity corpus scenario follow it. Refs #686 Co-Authored-By: Claude Opus 5.5 --- ...angepack_log_engine_sessions_bindings.json | 8 +++ bindings/argument-validation.cjs | 4 ++ bindings/devup-ui-binding/README.md | 4 +- bindings/devup-ui-binding/adapter.cjs | 3 ++ bindings/devup-ui-binding/index.d.cts | 8 ++- bindings/devup-ui-binding/index.mjs | 3 ++ bindings/devup-ui-binding/src/host.rs | 19 +++++++ bindings/devup-ui-binding/src/lib.rs | 5 +- bindings/devup-ui-wasm/README.md | 10 ++++ bindings/devup-ui-wasm/script.js | 6 +-- bindings/devup-ui-wasm/src/lib.rs | 53 +++++++++++++++++++ e2e/binding/corpus/state.mjs | 15 ++++++ e2e/binding/host.mjs | 6 +-- e2e/binding/package/consumer.mjs | 2 +- e2e/binding/validation-cases.mjs | 8 ++- .../src/__tests__/wasm-abi.test.ts | 29 ++++++++++ 16 files changed, 170 insertions(+), 13 deletions(-) create mode 100644 .changepacks/changepack_log_engine_sessions_bindings.json diff --git a/.changepacks/changepack_log_engine_sessions_bindings.json b/.changepacks/changepack_log_engine_sessions_bindings.json new file mode 100644 index 000000000..f43836947 --- /dev/null +++ b/.changepacks/changepack_log_engine_sessions_bindings.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-wasm/package.json": "Patch", + "bindings/devup-ui-binding/package.json": "Patch" + }, + "note": "New `createSession()`, `selectSession(id)` and `dropSession(id)` exports on the WASM and native engines: a session is an engine state of its own (numbering, sheet, theme, settings and module resolver), and every call works on the selected one. Session 0 is selected from the start. `selectSession` and `dropSession` throw a `TypeError` for an id that is not an unsigned 32-bit integer, and an `Error` for an unknown session or for dropping session 0", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/bindings/argument-validation.cjs b/bindings/argument-validation.cjs index 66e41fb08..a06d30016 100644 --- a/bindings/argument-validation.cjs +++ b/bindings/argument-validation.cjs @@ -5,6 +5,7 @@ module.exports = function createBoundary() { const descriptors = { codeExtract: extraction, codeExtractWithoutSourceMap: extraction, + dropSession: 'id:u', getCss: 'file_num:n import_main_css:b', getThemeInterface: 'package_name:s color_interface_name:s typography_interface_name:s length_interface_name:s shadows_interface_name:s theme_interface_name:s', @@ -19,6 +20,7 @@ module.exports = function createBoundary() { reserveClassSlots: 'rows:rows _package:s single_css:b import_main_css_in_code:b _import_main_css_in_css:b import_aliases:aliases', seedFileMap: 'files:a', + selectSession: 'id:u', setAtomHoist: 'threshold:n', setDebug: 'debug:b', setModuleResolver: 'resolver:f', @@ -31,6 +33,7 @@ module.exports = function createBoundary() { p: 'a string, null or undefined', f: 'a function, null or undefined', a: 'an array of strings', + u: 'a session id', aliases: 'an alias mapping', canonical: 'a canonical mapping', class: 'a nested class-index mapping', @@ -50,6 +53,7 @@ module.exports = function createBoundary() { p: (value) => value == null || typeof value === 'string', f: (value) => value == null || typeof value === 'function', a: Array.isArray, + u: (value) => Number.isInteger(value) && value >= 0 && value <= 0xffffffff, } let frame const fail = (slot) => { diff --git a/bindings/devup-ui-binding/README.md b/bindings/devup-ui-binding/README.md index 993025949..d61337617 100644 --- a/bindings/devup-ui-binding/README.md +++ b/bindings/devup-ui-binding/README.md @@ -2,7 +2,7 @@ Synchronous native build-time compiler for Node.js and Bun. Both `require('@devup-ui/binding')` and named ESM imports expose exactly the existing -29 compiler names, with identical function/class identities and no default init. +32 compiler names, with identical function/class identities and no default init. Authored CommonJS/ESM declarations are included; no WASM dependency is required. The private loader selects one optional platform sibling: Windows MSVC, macOS, @@ -96,4 +96,4 @@ The decoder is private and target-bounded: unused Serde hints use trait forwardi Malformed-enum error paths follow shipped serde-wasm-bindgen 0.6.5: Object.entries result length is read and coerced once for the check, then read and coerced again for the invalid-length diagnostic when the first length is not one, before any index read. The second value supplies the diagnostic; errors thrown by its getter or coercion propagate unchanged. Valid enums retain one length read. -The shared native/WASM boundary validates ordinary calls at 45 argument positions in 18 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 267-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. +The shared native/WASM boundary validates ordinary calls at 47 argument positions in 20 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 283-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. diff --git a/bindings/devup-ui-binding/adapter.cjs b/bindings/devup-ui-binding/adapter.cjs index 77bf341e9..64ba6bfce 100644 --- a/bindings/devup-ui-binding/adapter.cjs +++ b/bindings/devup-ui-binding/adapter.cjs @@ -82,6 +82,8 @@ exports.createBinding = function createBinding(raw) { Output: raw.Output, codeExtract, codeExtractWithoutSourceMap, + createSession: () => raw.createSession(), + dropSession: (id) => raw.dropSession(id), exportCanonicalMap: raw.exportCanonicalMap, exportClassMap: raw.exportClassMap, exportFileMap: raw.exportFileMap, @@ -107,6 +109,7 @@ exports.createBinding = function createBinding(raw) { resetBuildState: () => raw.reset(false), resetStateForTesting: () => raw.reset(true), seedFileMap, + selectSession: (id) => raw.selectSession(id), setAtomHoist: (value) => raw.setAtomHoist(index(value)), setDebug: (value) => raw.setDebug(value), setModuleResolver, diff --git a/bindings/devup-ui-binding/index.d.cts b/bindings/devup-ui-binding/index.d.cts index cd21d7210..4c885885c 100644 --- a/bindings/devup-ui-binding/index.d.cts +++ b/bindings/devup-ui-binding/index.d.cts @@ -11,7 +11,7 @@ export class Output { readonly updatedBaseStyle: boolean } -// Positional signatures intentionally match the existing 29-name compiler ABI. +// Positional signatures intentionally match the existing 32-name compiler ABI. export function codeExtract( filename: string, code: string, @@ -32,6 +32,10 @@ export function codeExtractWithoutSourceMap( importMainCssInCss: boolean, importAliases: unknown, ): Output +/** Create an engine session with a clean state; select it before each call. */ +export function createSession(): number +/** Drop a session and its state; dropping the live one makes session 0 live. */ +export function dropSession(id: number): boolean export function exportCanonicalMap(): string export function exportClassMap(): string export function exportFileMap(): string @@ -76,6 +80,8 @@ export function reserveClassSlots( export function resetBuildState(): void export function resetStateForTesting(): void export function seedFileMap(files: string[]): void +/** Make a session live: later calls work on its state until another is selected. */ +export function selectSession(id: number): void export function setAtomHoist(threshold?: number | null): void export function setDebug(debug: boolean): void export function setModuleResolver( diff --git a/bindings/devup-ui-binding/index.mjs b/bindings/devup-ui-binding/index.mjs index c9f981b23..6e6ac9ec5 100644 --- a/bindings/devup-ui-binding/index.mjs +++ b/bindings/devup-ui-binding/index.mjs @@ -4,6 +4,8 @@ export const { Output, codeExtract, codeExtractWithoutSourceMap, + createSession, + dropSession, exportCanonicalMap, exportClassMap, exportFileMap, @@ -26,6 +28,7 @@ export const { resetBuildState, resetStateForTesting, seedFileMap, + selectSession, setAtomHoist, setDebug, setModuleResolver, diff --git a/bindings/devup-ui-binding/src/host.rs b/bindings/devup-ui-binding/src/host.rs index 7b8a7aa5d..0be73bb61 100644 --- a/bindings/devup-ui-binding/src/host.rs +++ b/bindings/devup-ui-binding/src/host.rs @@ -51,6 +51,25 @@ pub fn reset(env: Env, full: bool) -> Result<()> { } Ok(()) } +/// Create an engine session with a clean state and return its id. +#[napi] +pub fn create_session() -> u32 { + core::create_session() +} +/// Make session `id` live until another is selected. +#[napi] +pub fn select_session(env: Env, id: u32) -> Result<()> { + core::select_session(id) + .map(drop) + .map_err(|error| binding_error(env, error)) +} +/// Drop session `id` with its state and this environment's resolver for it. +#[napi] +pub fn drop_session(env: Env, id: u32) -> Result { + let dropped = core::drop_session(id).map_err(|error| binding_error(env, error))?; + state(&env)?.0.remove(id); + Ok(dropped) +} fn with_resolver( env: Env, diagnostics: core::ResolverState, diff --git a/bindings/devup-ui-binding/src/lib.rs b/bindings/devup-ui-binding/src/lib.rs index 205575d4c..c3767ec26 100644 --- a/bindings/devup-ui-binding/src/lib.rs +++ b/bindings/devup-ui-binding/src/lib.rs @@ -5,7 +5,10 @@ mod host; mod output; mod value; -pub use host::{Resolved, extract, has_devup_ui, init, reserve, reset, set_resolver}; +pub use host::{ + Resolved, create_session, drop_session, extract, has_devup_ui, init, reserve, reset, + select_session, set_resolver, +}; pub use output::NativeOutput; use de::Wasm32Index; diff --git a/bindings/devup-ui-wasm/README.md b/bindings/devup-ui-wasm/README.md index 1032dec5d..61256885b 100644 --- a/bindings/devup-ui-wasm/README.md +++ b/bindings/devup-ui-wasm/README.md @@ -66,6 +66,16 @@ unwind. Binding tests compose these with sheet/resolver snapshots. Keep a named guard on its creating thread and retain `#[serial]`: guards clean up but do not serialize concurrent tests. Cleanup does not run for panic=abort. +## Sessions + +`createSession()` returns the id of a new engine session with a clean state, and +`selectSession(id)` makes it live: every later call reads and changes that +session's state, resolver included, until another is selected. Session 0 is +live from the start, so a single app needs neither. Two apps in one process +each select their own session before each call. `dropSession(id)` forgets a +session (dropping the live one makes session 0 live), and the full test reset +forgets every session but 0. + ## Errors A source the build cannot compile makes `codeExtract` throw an `Error`. Its diff --git a/bindings/devup-ui-wasm/script.js b/bindings/devup-ui-wasm/script.js index 279a62ba6..db98fa291 100644 --- a/bindings/devup-ui-wasm/script.js +++ b/bindings/devup-ui-wasm/script.js @@ -39,9 +39,9 @@ for (const operation of ['begin', 'end', 'error', 'fail']) ) assert.equal([...source.matchAll(/__devupBoundary/g)].length, 4) const names = [...source.matchAll(/^exports\.(\w+) = \1;/gm)] -assert.equal(names.length, 29) -assert.equal(new Set(names.map((match) => match[1])).size, 29) -assert.equal([...source.matchAll(/^exports\./gm)].length, 29) +assert.equal(names.length, 32) +assert.equal(new Set(names.map((match) => match[1])).size, 32) +assert.equal([...source.matchAll(/^exports\./gm)].length, 32) assert.equal(source.includes('module.exports'), false) for (const name of ['codeExtract', 'codeExtractWithoutSourceMap']) { const signatures = [ diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index 2831f375d..58891e5f9 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -297,6 +297,40 @@ pub fn seed_file_map(files: Vec) { binding_core::seed_file_map(&files); } +/// Create an engine session with a clean state and return its id. +/// +/// Two apps in one process each keep their own session: select it before +/// every call so the call reads and changes only that app's state. Session 0 +/// is live until another is selected. +#[wasm_bindgen(js_name = "createSession")] +#[must_use] +pub fn create_session() -> u32 { + binding_core::create_session() +} + +/// Make session `id` live: every later call works on its state until +/// another session is selected. +#[wasm_bindgen(js_name = "selectSession")] +#[cfg(not(tarpaulin_include))] +pub fn select_session(id: u32) -> Result<(), JsValue> { + binding_core::select_session(id).map(drop).map_err(js_error) +} + +/// Drop session `id` with its state and resolver (testable without `JsValue`) +pub fn drop_session_internal(id: u32) -> Result { + let dropped = binding_core::drop_session(id)?; + MODULE_RESOLVER.with_borrow_mut(|resolvers| resolvers.remove(id)); + Ok(dropped) +} + +/// Drop session `id` with its state and module resolver, returning whether +/// it existed. Dropping the live session makes session 0 live. +#[wasm_bindgen(js_name = "dropSession")] +#[cfg(not(tarpaulin_include))] +pub fn drop_session(id: u32) -> Result { + drop_session_internal(id).map_err(js_error) +} + /// Forget everything one build left in the engine. /// /// That is names, numbers, styles, buckets, routes, the prefix, atom hoisting @@ -1721,6 +1755,25 @@ mod tests { assert!(json_str.starts_with('{') || json_str.starts_with("[]")); } + #[test] + #[serial] + fn sessions_keep_their_own_prefix_until_dropped() { + let _state = TestStateGuard::new(); + // Given: a second session with its own prefix + let session = create_session(); + assert_eq!(binding_core::select_session(session), Ok(0)); + set_prefix(Some("app-".into())); + // When / Then: session 0 keeps its own, and the dropped session goes + assert_eq!(binding_core::select_session(0), Ok(session)); + assert_eq!(get_prefix(), None); + assert_eq!( + drop_session_internal(0), + Err("engine session 0 cannot be dropped".to_string()) + ); + assert_eq!(drop_session_internal(session), Ok(true)); + assert_eq!(drop_session_internal(session), Ok(false)); + } + #[test] #[serial] fn test_exported_maps_are_canonical_json() { diff --git a/e2e/binding/corpus/state.mjs b/e2e/binding/corpus/state.mjs index 04e6ff084..6bf3e1d8f 100644 --- a/e2e/binding/corpus/state.mjs +++ b/e2e/binding/corpus/state.mjs @@ -42,6 +42,21 @@ add('reservation-malformed-second-row-arity-nonmutation', (h) => { h.call('reserveClassSlots', value, pkg, false, false, false, {}) h.call('reserveClassSlots', rows, pkg, false, false, false, { bad: 7 }) }) +add('sessions-keep-apart-until-dropped', (h) => { + h.call('setPrefix', 'zero-') + h.extract(source) + const session = h.call('createSession').value.value + h.call('selectSession', session) + h.call('registerTheme', theme) + h.extract(box('color="blue"'), { filename: 'src/second.tsx' }) + h.call('selectSession', 0) + h.extract(source) + h.call('selectSession', 7) + h.call('dropSession', 0) + h.call('selectSession', session) + h.call('dropSession', session) + h.call('dropSession', session) +}) add('four-raw-states-explicit-import-roundtrip', (h) => { h.call('registerTheme', theme) h.call('importCanonicalMap', { [file]: 'src/root.tsx' }) diff --git a/e2e/binding/host.mjs b/e2e/binding/host.mjs index 12046f85a..a64ec126d 100644 --- a/e2e/binding/host.mjs +++ b/e2e/binding/host.mjs @@ -68,7 +68,7 @@ save('provenance.json', { scenarios: corpus.map((c) => c.name), }) const names = - 'Output codeExtract codeExtractWithoutSourceMap exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme reserveClassSlots resetBuildState resetStateForTesting seedFileMap setAtomHoist setDebug setModuleResolver setPrefix'.split( + 'Output codeExtract codeExtractWithoutSourceMap createSession dropSession exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme reserveClassSlots resetBuildState resetStateForTesting seedFileMap selectSession setAtomHoist setDebug setModuleResolver setPrefix'.split( ' ', ) const abi = { @@ -199,8 +199,8 @@ const h = { ) }, } -assert.equal(corpus.length, 25) -assert.equal(new Set(corpus.map((c) => c.name)).size, 25) +assert.equal(corpus.length, 26) +assert.equal(new Set(corpus.map((c) => c.name)).size, 26) h.note('public-error-inventory', runDiagnostics(w)) for (const test of corpus) { scenario = test.name diff --git a/e2e/binding/package/consumer.mjs b/e2e/binding/package/consumer.mjs index 076623429..1de06ad40 100644 --- a/e2e/binding/package/consumer.mjs +++ b/e2e/binding/package/consumer.mjs @@ -136,7 +136,7 @@ export async function consume(options) { ] .map((match) => match[1]) .sort() - assert.equal(names.length, 29) + assert.equal(names.length, 32) for (const manager of ['npm', 'bun']) { for (const omit of [false, true]) { const startRequest = server.requests.length diff --git a/e2e/binding/validation-cases.mjs b/e2e/binding/validation-cases.mjs index c90b0fda0..846892b59 100644 --- a/e2e/binding/validation-cases.mjs +++ b/e2e/binding/validation-cases.mjs @@ -20,6 +20,7 @@ const extractSlots = export const positions = [ ['codeExtract', extractSlots, extraction], ['codeExtractWithoutSourceMap', extractSlots, extraction], + ['dropSession', 'id:session', () => [1]], ['getCss', 'file_num:n import_main_css:b', () => [null, false]], [ 'getThemeInterface', @@ -44,6 +45,7 @@ export const positions = [ () => [[row], pkg, false, false, false, {}], ], ['seedFileMap', 'files:seed', () => [['src/new.tsx']]], + ['selectSession', 'id:session', () => [0]], ['setAtomHoist', 'threshold:n', () => [2]], ['setDebug', 'debug:b', () => [true]], ['setModuleResolver', 'resolver:resolver', () => [null]], @@ -61,6 +63,7 @@ const labels = { prefix: 'a string, null or undefined', resolver: 'a function, null or undefined', seed: 'an array of strings', + session: 'a session id', aliases: 'an alias mapping', canonical: 'a canonical mapping', class: 'a nested class-index mapping', @@ -80,6 +83,7 @@ const invalid = { b: [7, 'false', null, undefined], n: ['2', false, 1n, Symbol('number'), {}], prefix: [false, true, 7, {}], + session: ['1', false, -1, 1.5, 2 ** 32, null, undefined, {}], resolver: [false, true, 7, 'callback', {}], seed: [ ...roots, @@ -153,6 +157,6 @@ export const cases = positions.flatMap((position) => .map((good, index) => (index === position.index ? value : good)), })), ) -assert.equal(positions.length, 45) -assert.equal(new Set(positions.map((p) => p.fn)).size, 18) +assert.equal(positions.length, 47) +assert.equal(new Set(positions.map((p) => p.fn)).size, 20) assert.equal(new Set(cases.map((c) => c.id)).size, cases.length) diff --git a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts index 2edae3e78..83646582e 100644 --- a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts +++ b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts @@ -44,6 +44,8 @@ describe('@devup-ui/wasm exports', () => { 'Output', 'codeExtract', 'codeExtractWithoutSourceMap', + 'createSession', + 'dropSession', 'exportCanonicalMap', 'exportClassMap', 'exportFileMap', @@ -66,6 +68,7 @@ describe('@devup-ui/wasm exports', () => { 'resetBuildState', 'resetStateForTesting', 'seedFileMap', + 'selectSession', 'setAtomHoist', 'setDebug', 'setModuleResolver', @@ -393,3 +396,29 @@ describe('@devup-ui/wasm reservation', () => { expect(wasm.getUnreservedClassSlotCount()).toBe(0) }) }) + +describe('@devup-ui/wasm sessions', () => { + it('keeps each session apart until it is dropped', () => { + const prefix = wasm.getPrefix() + const session = wasm.createSession() + wasm.selectSession(session) + expect(wasm.getPrefix()).toBeUndefined() + wasm.setPrefix('app-') + wasm.selectSession(0) + expect(wasm.getPrefix()).toBe(prefix) + expect(() => wasm.selectSession(session + 1)).toThrow( + `engine session ${session + 1} does not exist`, + ) + expect(() => wasm.selectSession(-1)).toThrow( + 'selectSession(): argument id must be a session id', + ) + wasm.selectSession(session) + expect(wasm.getPrefix()).toBe('app-') + expect(wasm.dropSession(session)).toBe(true) + expect(wasm.getPrefix()).toBe(prefix) + expect(wasm.dropSession(session)).toBe(false) + expect(() => wasm.dropSession(0)).toThrow( + 'engine session 0 cannot be dropped', + ) + }) +}) From 7f8b40842474ebb24a71e536cb2bc04783037707 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 19:55:02 +0900 Subject: [PATCH 3/6] test(plugin-utils): count the three session exports in the compiler shape Refs #686 Co-Authored-By: Claude Opus 5.5 --- packages/plugin-utils/src/__tests__/compiler-types.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-utils/src/__tests__/compiler-types.test.ts b/packages/plugin-utils/src/__tests__/compiler-types.test.ts index 10e4921eb..8e15dfbea 100644 --- a/packages/plugin-utils/src/__tests__/compiler-types.test.ts +++ b/packages/plugin-utils/src/__tests__/compiler-types.test.ts @@ -78,7 +78,7 @@ describe('compiler structural types', () => { Object.keys(compiler).filter( (key) => key !== 'Output' && key !== 'default', ), - ).toHaveLength(28) + ).toHaveLength(31) } finally { compiler.resetStateForTesting() } From 0913645707b69e21241aae8783433af675393f28 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 19:58:29 +0900 Subject: [PATCH 4/6] test(binding): report the twenty validated functions in the summary Refs #686 Co-Authored-By: Claude Opus 5.5 --- e2e/binding/validation.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/binding/validation.mjs b/e2e/binding/validation.mjs index 499bb1b34..5a94d03f1 100644 --- a/e2e/binding/validation.mjs +++ b/e2e/binding/validation.mjs @@ -114,7 +114,7 @@ for (const [engine, entry] of [ const summary = { cases: cases.length, positions: positions.length, - functions: 18, + functions: 20, engines: 2, runs: runs.length, failed: runs.filter((run) => run.failed).length, From ad4c05f37e0d5f8aaf6ade6c732c12073d473621 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 19:59:28 +0900 Subject: [PATCH 5/6] feat(plugin-utils): add openCompilerSession for per-app engine state openCompilerSession(compiler) creates an engine session on a shared compiler and returns { id, compiler, close }. Every function read from the scoped compiler selects the session before it runs; the calls are synchronous and JavaScript is single-threaded, so no other session's call can run between the selection and the call. close() drops the session, after which calls through the scoped compiler throw. Refs #686 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_compiler_sessions.json | 7 ++ .../src/__tests__/compiler-session.test.ts | 78 +++++++++++++++++++ packages/plugin-utils/src/compiler-session.ts | 46 +++++++++++ packages/plugin-utils/src/index.ts | 1 + 4 files changed, 132 insertions(+) create mode 100644 .changepacks/changepack_log_compiler_sessions.json create mode 100644 packages/plugin-utils/src/__tests__/compiler-session.test.ts create mode 100644 packages/plugin-utils/src/compiler-session.ts diff --git a/.changepacks/changepack_log_compiler_sessions.json b/.changepacks/changepack_log_compiler_sessions.json new file mode 100644 index 000000000..acaa995ce --- /dev/null +++ b/.changepacks/changepack_log_compiler_sessions.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/plugin-utils/package.json": "Patch" + }, + "note": "New `openCompilerSession(compiler)`: it opens an engine session of its own on a shared compiler and returns `{ id, compiler, close }`. Every function of the scoped `compiler` selects the session before it runs, so two apps in one process keep their numbering, sheet, theme and settings apart; `close()` drops the session", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/packages/plugin-utils/src/__tests__/compiler-session.test.ts b/packages/plugin-utils/src/__tests__/compiler-session.test.ts new file mode 100644 index 000000000..519695be4 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/compiler-session.test.ts @@ -0,0 +1,78 @@ +import * as wasm from '@devup-ui/wasm' +import { afterEach, describe, expect, it } from 'bun:test' + +import { openCompilerSession } from '../compiler-session' +import type { DevupCompiler } from '../compiler-types' + +const compiler: DevupCompiler = wasm +const pkg = '@devup-ui/react' +const source = `import { Box } from '${pkg}'; export const view = ` + +function extract(target: DevupCompiler, file: string) { + const output = target.codeExtract( + file, + source, + pkg, + 'df', + true, + false, + false, + {}, + ) + try { + return output.code + } finally { + output.free() + } +} + +afterEach(() => { + compiler.resetStateForTesting() +}) + +describe('openCompilerSession', () => { + it('keeps each session apart on one shared compiler', () => { + // Given: two apps, each with its own session and prefix. + const first = openCompilerSession(compiler) + const second = openCompilerSession(compiler) + expect(first.id).not.toBe(second.id) + first.compiler.setPrefix('one-') + second.compiler.setPrefix('two-') + + // When: their calls interleave on the shared compiler. + const one = extract(first.compiler, 'src/one.tsx') + const two = extract(second.compiler, 'src/two.tsx') + const again = extract(first.compiler, 'src/one.tsx') + + // Then: each call ran on its own session's state. + expect(one).toContain('one-') + expect(two).toContain('two-') + expect(again).toBe(one) + expect(first.compiler.getCss(null, false)).not.toContain('two-') + expect(second.compiler.getCss(null, false)).not.toContain('one-') + first.close() + second.close() + }) + + it('reuses one wrapper per function and passes other values through', () => { + // Given: a session over the module namespace. + const session = openCompilerSession(compiler) + + // When / Then: function reads stay stable, other reads are untouched. + expect(session.compiler.getCss).toBe(session.compiler.getCss) + expect(Reflect.get(session.compiler, Symbol.toStringTag)).toBe('Module') + session.close() + }) + + it('drops the session on close so later calls through it throw', () => { + // Given: a closed session. + const session = openCompilerSession(compiler) + session.close() + + // When / Then: selecting it again is refused by the engine. + expect(() => session.compiler.getCss(null, false)).toThrow( + `engine session ${session.id} does not exist`, + ) + expect(compiler.getCss(null, false)).toBeString() + }) +}) diff --git a/packages/plugin-utils/src/compiler-session.ts b/packages/plugin-utils/src/compiler-session.ts new file mode 100644 index 000000000..3b514efb6 --- /dev/null +++ b/packages/plugin-utils/src/compiler-session.ts @@ -0,0 +1,46 @@ +import type { DevupCompiler } from './compiler-types' + +/** One app's engine session on a shared compiler. */ +export interface CompilerSession { + /** The engine session every call through `compiler` selects first. */ + readonly id: number + /** + * The shared compiler, scoped to this session: each function selects the + * session, then runs. JavaScript is single-threaded and the calls are + * synchronous, so no other session's call can run between the two. + */ + readonly compiler: DevupCompiler + /** Drop the session and its engine state; later calls through `compiler` throw. */ + close(): void +} + +/** + * Open an engine session of its own on `compiler`, so two apps in one process + * never read or change each other's numbering, sheet, theme or settings. + */ +export function openCompilerSession(compiler: DevupCompiler): CompilerSession { + const id = compiler.createSession() + const scoped = new Map() + return { + id, + compiler: new Proxy(compiler, { + get(target, key) { + const value: unknown = Reflect.get(target, key) + if (typeof value !== 'function') return value + let wrapper = scoped.get(key) + if (!wrapper) { + // Read the function at call time, so a later replacement applies + wrapper = function (this: unknown, ...args: unknown[]) { + target.selectSession(id) + return Reflect.apply(Reflect.get(target, key), this, args) + } + scoped.set(key, wrapper) + } + return wrapper + }, + }), + close() { + compiler.dropSession(id) + }, + } +} diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index bfc7b1e1b..60a6de28b 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -4,6 +4,7 @@ export { type ResettableEngine, } from './build-session' export { loadCompiler } from './compiler' +export { type CompilerSession, openCompilerSession } from './compiler-session' export type { DevupCompiler, DevupCompilerOutput, From 10a91a501052057164eedfee6464107660d99515 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 20:07:57 +0900 Subject: [PATCH 6/6] feat(next-plugin): keep one coordinator and engine session per app The Turbopack coordinator was a module-level singleton: starting a second app in the same process closed the first app's listener, and both apps shared one engine state. Coordinators are now kept in a registry keyed by the resolved distDir: - openCoordinatorCompiler(distDir, compiler) opens the app's own engine session (with the module resolver) on first use; the plugin runs its setup through it and hands it to the coordinator - startCoordinator replaces only the previous coordinator of the same app and takes a reference; each coordinator keeps its extraction tracking in a state of its own - close() is idempotent and releases the reference; the last close drops the app's session and its registry entry - moving an app to another engine opens a new session there Refs #686 Co-Authored-By: Claude Opus 5.5 --- ...ngepack_log_next_coordinator_registry.json | 7 + packages/next-plugin/README.md | 7 +- .../src/__tests__/coordinator.test.ts | 80 ++++++ .../next-plugin/src/__tests__/plugin.test.ts | 7 +- .../src/__tests__/source-map.wasm.test.ts | 1 + packages/next-plugin/src/coordinator.ts | 270 ++++++++++++------ packages/next-plugin/src/plugin.ts | 7 +- 7 files changed, 279 insertions(+), 100 deletions(-) create mode 100644 .changepacks/changepack_log_next_coordinator_registry.json diff --git a/.changepacks/changepack_log_next_coordinator_registry.json b/.changepacks/changepack_log_next_coordinator_registry.json new file mode 100644 index 000000000..e9651da97 --- /dev/null +++ b/.changepacks/changepack_log_next_coordinator_registry.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/next-plugin/package.json": "Patch" + }, + "note": "Turbopack apps in one process no longer share one coordinator: coordinators are kept per resolved `distDir`, each with its own engine session, extraction tracking and listener. Starting a second app no longer closes the first app's coordinator, and a config re-evaluation replaces only its own app's coordinator; the app's session is dropped when its last coordinator closes", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/packages/next-plugin/README.md b/packages/next-plugin/README.md index 631d06541..97f42e0b3 100644 --- a/packages/next-plugin/README.md +++ b/packages/next-plugin/README.md @@ -211,8 +211,11 @@ The build-time compiler defaults to `native` for Webpack and Turbopack. Select WASM explicitly with `DevupUI(nextConfig, { engine: 'wasm' })`. The chosen engine is retained by configuration, prewarm, extraction, CSS and delayed callbacks. Turbopack loader options carry only the engine string; the coordinator retains -the selected physical compiler. Independent concurrent builds in one process -are unsupported. +the selected physical compiler. Turbopack apps in one process keep one +coordinator and one engine session per resolved `distDir`, so their numbering, +sheet, theme and settings stay apart; give each app its own `distDir`. A config +re-evaluation of the same app replaces only that app's coordinator. Independent +concurrent Webpack builds in one process are unsupported. Native load failures identify the required platform package and retain the original cause. There is no automatic fallback to WASM. Install optional native diff --git a/packages/next-plugin/src/__tests__/coordinator.test.ts b/packages/next-plugin/src/__tests__/coordinator.test.ts index 7d346a6ba..3e9902c5a 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -16,8 +16,10 @@ import { } from 'bun:test' import { + coordinatorKey, type CoordinatorOptions, flushCoordinatorWrites, + openCoordinatorCompiler, resetCoordinator, startCoordinator, } from '../coordinator' @@ -39,6 +41,7 @@ function makeOptions( overrides: Partial = {}, ): CoordinatorOptions { return { + distDir: tmpDir, wasm, package: '@devup-ui/react', cssDir: join(tmpDir, 'css'), @@ -860,6 +863,83 @@ describe('coordinator', () => { second.close() }) + it('keeps the coordinators of different apps running side by side', async () => { + // Given: two apps in one process, each with its own distDir and port file. + const app = (name: string) => + makeOptions({ + distDir: join(tmpDir, name), + coordinatorPortFile: join(tmpDir, name, 'coordinator.port'), + }) + const portOf = (options: CoordinatorOptions) => + parseInt( + ( + writeFileSyncSpy.mock.calls.findLast( + ([path]) => path === options.coordinatorPortFile, + ) as [string, string] + )[1], + ) + const one = app('one') + const two = app('two') + + // When: the second app starts its coordinator after the first. + const first = startCoordinator(one) + const second = startCoordinator(two) + await new Promise((resolve) => setTimeout(resolve, 100)) + + // Then: both answer, and closing one leaves the other running. + expect(await httpRequest(portOf(one), 'GET', '/health')).toEqual({ + status: 200, + body: 'ok', + }) + first.close() + expect(await httpRequest(portOf(two), 'GET', '/health')).toEqual({ + status: 200, + body: 'ok', + }) + second.close() + }) + + it('keeps one engine session per app until its last coordinator closes', () => { + // Given: one app's engine, read through two spellings of its distDir. + const distDir = join(tmpDir, 'app') + const engine = openCoordinatorCompiler(distDir, wasm) + expect(openCoordinatorCompiler(join(distDir, 'x', '..'), wasm)).toBe(engine) + expect(coordinatorKey(join(distDir, 'x', '..'))).toBe( + coordinatorKey(distDir), + ) + const other = openCoordinatorCompiler(join(tmpDir, 'other'), wasm) + expect(other).not.toBe(engine) + engine.setPrefix('app-') + expect(other.exportSheet()).not.toContain('app-') + + // When: a config re-evaluation starts a second coordinator for the app. + const options = makeOptions({ distDir, wasm: engine }) + const first = startCoordinator(options) + const second = startCoordinator(options) + first.close() + first.close() + + // Then: the session outlives the superseded coordinator, even closed twice. + expect(engine.getCss(null, false)).toBeString() + second.close() + expect(() => engine.getCss(null, false)).toThrow('does not exist') + expect(openCoordinatorCompiler(distDir, wasm)).not.toBe(engine) + }) + + it('opens a new session when an app moves to another engine', () => { + // Given: an app whose session lives in the WASM engine. + const distDir = join(tmpDir, 'moving') + const before = openCoordinatorCompiler(distDir, wasm) + + // When: a re-evaluation hands over another engine namespace. + const after = openCoordinatorCompiler(distDir, new Proxy(wasm, {})) + + // Then: the first session is dropped and a new one is used. + expect(after).not.toBe(before) + expect(() => before.getCss(null, false)).toThrow('does not exist') + expect(after.getCss(null, false)).toBeString() + }) + it('should touch devup-ui.css to invalidate Turbopack cache when singleCss=false and new CSS collected', async () => { codeExtractSpy.mockReturnValue({ code: 'import "./../../df/devup-ui/devup-ui-5.css";\nconst x = 1;', diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..e64b817c2 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -165,6 +165,7 @@ beforeEach(() => { }) afterEach(() => { + coordinatorModule.resetCoordinator() resetTurboSetupCacheForTesting() setWasmForTesting(undefined) setWebpackPluginForTesting(undefined) @@ -981,7 +982,8 @@ describe('DevupUINextPlugin', () => { }, }) expect(startCoordinatorSpy).toHaveBeenCalledWith({ - wasm, + distDir: 'df', + wasm: coordinatorModule.openCoordinatorCompiler('df', wasm), package: '@devup-ui/react', cssDir: resolve('df', 'devup-ui'), singleCss: false, @@ -1268,7 +1270,8 @@ export const box = style({ color: 'red' })` // Verify coordinator was started with correct options expect(startCoordinatorSpy).toHaveBeenCalledWith({ - wasm, + distDir: 'df', + wasm: coordinatorModule.openCoordinatorCompiler('df', wasm), package: '@devup-ui/react', cssDir: resolve('df', 'devup-ui'), singleCss: false, diff --git a/packages/next-plugin/src/__tests__/source-map.wasm.test.ts b/packages/next-plugin/src/__tests__/source-map.wasm.test.ts index dd404834e..57b01bf65 100644 --- a/packages/next-plugin/src/__tests__/source-map.wasm.test.ts +++ b/packages/next-plugin/src/__tests__/source-map.wasm.test.ts @@ -206,6 +206,7 @@ describe.each([false, true])('Next singleCss with prewarm=%s', (prewarm) => { try { coordinator = startCoordinator({ ...options, + distDir: root, wasm, sourceMap, importAliases: {}, diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 892f8287d..20b0ac66d 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -1,14 +1,24 @@ import { writeFile, writeFileSync } from 'node:fs' import { createServer, type IncomingMessage, type Server } from 'node:http' -import { basename, dirname, join, relative } from 'node:path' +import { basename, dirname, join, relative, resolve } from 'node:path' -import { getFileNumByFilename } from '@devup-ui/plugin-utils' +import { + type CompilerSession, + getFileNumByFilename, + openCompilerSession, +} from '@devup-ui/plugin-utils' import { formatPortFile, removeOwnPortFile } from './coordinator-port' import { elapsedMs, profileStart, reportProfile } from './profile' -import type { DevupWasm } from './wasm' +import { type DevupWasm, withModuleResolver } from './wasm' export interface CoordinatorOptions { + /** + * The app's output directory. Coordinators are kept per resolved `distDir`: + * starting one replaces only the previous coordinator of the same app. + */ + distDir: string + /** The app's engine, from `openCoordinatorCompiler(distDir, ...)`. */ wasm: DevupWasm package: string cssDir: string @@ -170,8 +180,6 @@ function readBody(req: IncomingMessage): Promise { }) } -let server: Server | null = null - // Extraction tracking for waitForIdle. // // The CSS loader fetches the live sheet from `/css?waitForIdle=true`. In @@ -191,43 +199,84 @@ let server: Server | null = null // extraction batches. Empirically the gap between Turbopack extraction // "waves" in a 64-route landing build can exceed the previous 500ms, which // caused the snapshot to capture only the early routes' styles. -let activeExtractions = 0 -let totalExtractions = 0 -let lastCompletedAt = 0 -let pendingExtractStarts = 0 -let idleThresholdMs = 2500 -let quietMs = 10_000 -let maxWaitMs = 60_000 +// +// Per-bucket completion tracking (deterministic replacement for waitForIdle on +// collapsed chunks). +// +// Single-importer collapse merges several source files into ONE shared CSS +// chunk (a "bucket"). That chunk is only complete once EVERY member file has +// been extracted. Turbopack, however, may request the chunk as soon as ONE +// member's import resolves. The old global idle heuristic guessed completion +// and dropped late members' atoms when extraction "waves" exceeded the idle +// threshold (flaky CI rendering). Instead we wait for the bucket's KNOWN +// members (from the canonical map) — no guessing, no extra extraction. +// +// Each coordinator keeps this tracking in a state of its own, so two apps in +// one process never count each other's extractions. +interface CoordinatorState { + activeExtractions: number + totalExtractions: number + lastCompletedAt: number + pendingExtractStarts: number + idleThresholdMs: number + quietMs: number + maxWaitMs: number + extractedFiles: Set + fileNumToBucket: Map + bucketToMembers: Map> + canonicalMap: Record + // Route-reachable runtime files the base sheet must wait for (cwd-relative + // POSIX). When populated, base-css completion is deterministic; empty falls + // back to the idle heuristic. See CoordinatorOptions.expectedBaseFiles. + expectedBaseFiles: Set +} + +function createState(options: CoordinatorOptions): CoordinatorState { + return { + activeExtractions: 0, + totalExtractions: 0, + lastCompletedAt: 0, + pendingExtractStarts: 0, + idleThresholdMs: options.idleThresholdMs ?? 2500, + quietMs: options.quietMs ?? 10_000, + maxWaitMs: options.maxWaitMs ?? 60_000, + extractedFiles: new Set(options.prewarmedFiles), + fileNumToBucket: new Map(), + bucketToMembers: buildBucketToMembers(options.canonicalMap), + canonicalMap: options.canonicalMap, + expectedBaseFiles: new Set(options.expectedBaseFiles), + } +} // Legacy fail-open signal for callers that could not prewarm a deterministic // file set. It only observes extraction traffic; it is NOT a Turbopack // compilation-complete signal. Production builds seed `extractedFiles` with // their prewarmed route closure and therefore do not depend on this path. -function bundlerQuiet(now: number): boolean { +function bundlerQuiet(state: CoordinatorState, now: number): boolean { return ( - totalExtractions > 0 && - activeExtractions === 0 && - pendingExtractStarts === 0 && - now - lastCompletedAt >= quietMs + state.totalExtractions > 0 && + state.activeExtractions === 0 && + state.pendingExtractStarts === 0 && + now - state.lastCompletedAt >= state.quietMs ) } -function baseFilesComplete(): boolean { +function baseFilesComplete(state: CoordinatorState): boolean { // Deterministic: the base sheet is complete once every route-reachable file // has been extracted by either the production prewarm or `/extract`. - if (expectedBaseFiles.size === 0) return false - for (const file of expectedBaseFiles) { - if (!extractedFiles.has(file)) return false + if (state.expectedBaseFiles.size === 0) return false + for (const file of state.expectedBaseFiles) { + if (!state.extractedFiles.has(file)) return false } return true } -function waitForBase(): Promise { +function waitForBase(state: CoordinatorState): Promise { const start = Date.now() return new Promise((resolve) => { const check = () => { const now = Date.now() - if (now - start > maxWaitMs) { + if (now - start > state.maxWaitMs) { // Last-resort backstop (see waitForBucket). Never fires on a healthy // build: either every expected file extracts, or the idle fallback // resolves first. @@ -235,24 +284,24 @@ function waitForBase(): Promise { return } // Primary, deterministic path. - if (baseFilesComplete()) { + if (baseFilesComplete(state)) { resolve() return } // Legacy fail-open for a caller that supplied expected files without // prewarming them. The Next plugin's production path completes above. - if (expectedBaseFiles.size > 0 && bundlerQuiet(now)) { + if (state.expectedBaseFiles.size > 0 && bundlerQuiet(state, now)) { resolve() return } // Fallback ONLY when no deterministic signal exists (no routes detected / // pre-pass failed -> expectedBaseFiles empty): the legacy idle heuristic. if ( - expectedBaseFiles.size === 0 && - totalExtractions > 0 && - activeExtractions === 0 && - pendingExtractStarts === 0 && - now - lastCompletedAt >= idleThresholdMs + state.expectedBaseFiles.size === 0 && + state.totalExtractions > 0 && + state.activeExtractions === 0 && + state.pendingExtractStarts === 0 && + now - state.lastCompletedAt >= state.idleThresholdMs ) { resolve() return @@ -263,25 +312,6 @@ function waitForBase(): Promise { }) } -// Per-bucket completion tracking (deterministic replacement for waitForIdle on -// collapsed chunks). -// -// Single-importer collapse merges several source files into ONE shared CSS -// chunk (a "bucket"). That chunk is only complete once EVERY member file has -// been extracted. Turbopack, however, may request the chunk as soon as ONE -// member's import resolves. The old global idle heuristic guessed completion -// and dropped late members' atoms when extraction "waves" exceeded the idle -// threshold (flaky CI rendering). Instead we wait for the bucket's KNOWN -// members (from the canonical map) — no guessing, no extra extraction. -const extractedFiles = new Set() -const fileNumToBucket = new Map() -let bucketToMembers = new Map>() -let canonicalMapRef: Record = {} -// Route-reachable runtime files the base sheet must wait for (cwd-relative -// POSIX). When populated, base-css completion is deterministic; empty falls back -// to the idle heuristic. See CoordinatorOptions.expectedBaseFiles. -let expectedBaseFiles = new Set() - function buildBucketToMembers( canonicalMap: Record, ): Map> { @@ -300,8 +330,9 @@ function buildBucketToMembers( return map } -function waitForBucket(bucket: string): Promise { - const members = bucketToMembers.get(bucket) ?? new Set([bucket]) +function waitForBucket(state: CoordinatorState, bucket: string): Promise { + const { extractedFiles, maxWaitMs } = state + const members = state.bucketToMembers.get(bucket) ?? new Set([bucket]) const start = Date.now() return new Promise((resolve) => { const check = () => { @@ -321,7 +352,7 @@ function waitForBucket(bucket: string): Promise { // prewarming. Quiet time alone cannot distinguish an erased import from // a later Turbopack extraction wave; production builds complete via the // allExtracted branch above. - if (bundlerQuiet(now)) { + if (bundlerQuiet(state, now)) { const missing = [...members].filter((m) => !extractedFiles.has(m)) console.info( `[devup-ui] coordinator: serving bucket "${bucket}" through the legacy quiet fallback; treating unreported member(s) as erased imports: ${missing.join(', ')}`, @@ -348,16 +379,75 @@ function waitForBucket(bucket: string): Promise { }) } +// One entry per Next app in this process, keyed by its resolved `distDir`. Each +// app keeps its own engine session, listener and tracking state, so a second +// app never closes the first one's coordinator or reads its sheet. Repeated +// config evaluations of the same app share the entry; it is released when the +// last coordinator started for it closes. +interface CoordinatorApp { + key: string + session: CompilerSession | null + /** The shared engine `session` lives in. */ + engine: DevupWasm | null + server: Server | null + refs: number +} + +const apps = new Map() + +/** @internal The registry key of a `distDir`: absolute and normalized. */ +export function coordinatorKey(distDir: string): string { + const key = resolve(distDir) + return process.platform === 'win32' ? key.toLowerCase() : key +} + +function appFor(distDir: string): CoordinatorApp { + const key = coordinatorKey(distDir) + let app = apps.get(key) + if (!app) { + app = { key, session: null, engine: null, server: null, refs: 0 } + apps.set(key, app) + } + return app +} + +/** + * The engine of the app at `distDir`: one session of its own on the shared + * `compiler`, opened on first use and kept until the app's last coordinator + * closes. Run the app's setup and pass it to `startCoordinator` as `wasm`. + */ +export function openCoordinatorCompiler( + distDir: string, + compiler: DevupWasm, +): DevupWasm { + const app = appFor(distDir) + if (!app.session || app.engine !== compiler) { + // A session lives in one engine: a re-evaluation on another engine + // (`engine` option changed) starts over on that one + app.session?.close() + app.session = openCompilerSession(compiler) + app.engine = compiler + } + return withModuleResolver(app.session.compiler) +} + +function releaseApp(app: CoordinatorApp): void { + app.refs-- + // Every coordinator started for the app has closed its own listener by now + if (app.refs > 0) return + app.session?.close() + apps.delete(app.key) +} + export function startCoordinator(options: CoordinatorOptions): { close: () => void } { + const app = appFor(options.distDir) // Next may evaluate its config more than once in the same process. Close the - // previous listener before replacing it so its HTTP server and request - // closure do not remain live for the rest of the build. - if (server) { - server.close() - server = null - } + // app's previous listener before replacing it so its HTTP server and request + // closure do not remain live for the rest of the build. Other apps' listeners + // stay untouched. + app.server?.close() const { wasm, package: libPackage, @@ -381,15 +471,8 @@ export function startCoordinator(options: CoordinatorOptions): { const extract = options.sourceMap === false ? codeExtractWithoutSourceMap : codeExtract - idleThresholdMs = options.idleThresholdMs ?? 2500 - quietMs = options.quietMs ?? 10_000 - maxWaitMs = options.maxWaitMs ?? 60_000 - canonicalMapRef = options.canonicalMap - bucketToMembers = buildBucketToMembers(options.canonicalMap) - expectedBaseFiles = new Set(options.expectedBaseFiles ?? []) - extractedFiles.clear() - for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) - fileNumToBucket.clear() + const state = createState(options) + const { extractedFiles, fileNumToBucket } = state const coordinatorServer = createServer(async (req, res) => { const url = new URL(req.url ?? '/', `http://${req.headers.host}`) @@ -411,12 +494,12 @@ export function startCoordinator(options: CoordinatorOptions): { if (fileNum != null && fileNumToBucket.has(fileNum)) { // Deterministic: block until every member of this collapsed bucket // has been extracted, then serve the complete chunk. - await waitForBucket(fileNumToBucket.get(fileNum)!) + await waitForBucket(state, fileNumToBucket.get(fileNum)!) } else { // Base css (no fileNum) or a bucket no member has reported yet: // wait for the deterministic route-reachable file set (idle fallback // only when that set is unavailable). - await waitForBase() + await waitForBase(state) } } @@ -437,15 +520,15 @@ export function startCoordinator(options: CoordinatorOptions): { // window between the request hitting this handler and `activeExtractions++` // below — making it falsely conclude the build is idle even though // more extractions are imminent. - pendingExtractStarts++ + state.pendingExtractStarts++ let promotedToActive = false let extractedFilename: string | undefined try { const bodyStartedAt = profileStart() const body = JSON.parse(await readBody(req)) const bodyDurationMs = elapsedMs(bodyStartedAt) - activeExtractions++ - pendingExtractStarts-- + state.activeExtractions++ + state.pendingExtractStarts-- promotedToActive = true const { filename, code, resourcePath } = body as { filename: string @@ -525,7 +608,10 @@ export function startCoordinator(options: CoordinatorOptions): { if (fileNum != null) { // Record this bucket's fileNum -> canonical bucket path so /css can // wait for the bucket's members before serving it. - fileNumToBucket.set(fileNum, canonicalMapRef[filename] ?? filename) + fileNumToBucket.set( + fileNum, + state.canonicalMap[filename] ?? filename, + ) } } @@ -644,17 +730,17 @@ export function startCoordinator(options: CoordinatorOptions): { ) } finally { if (promotedToActive) { - activeExtractions-- + state.activeExtractions-- } else { // readBody/JSON.parse threw before we promoted to active, so the // pending slot is still ours to release. - pendingExtractStarts-- + state.pendingExtractStarts-- } // Mark the file processed (success OR failure) so per-bucket waiters // never hang on a file that errored — fail open, like the idle path. if (extractedFilename != null) extractedFiles.add(extractedFilename) - totalExtractions++ - lastCompletedAt = Date.now() + state.totalExtractions++ + state.lastCompletedAt = Date.now() } return } @@ -663,7 +749,8 @@ export function startCoordinator(options: CoordinatorOptions): { res.end('Not Found') }) - server = coordinatorServer + app.server = coordinatorServer + app.refs++ coordinatorServer.listen(0, '127.0.0.1', () => { const addr = coordinatorServer.address() if (addr && typeof addr !== 'string') { @@ -671,6 +758,7 @@ export function startCoordinator(options: CoordinatorOptions): { } }) + let closed = false return { close: () => { // Fire-and-forget drain of any in-flight serialized writes so the @@ -679,10 +767,13 @@ export function startCoordinator(options: CoordinatorOptions): { // `process.on('exit', ...)` where awaiting is not possible). void flushPendingWrites() coordinatorServer.close() - if (server === coordinatorServer) { - server = null + if (closed) return + closed = true + if (app.server === coordinatorServer) { + app.server = null removeOwnPortFile(coordinatorPortFile) } + releaseApp(app) }, } } @@ -692,22 +783,11 @@ export const flushCoordinatorWrites = (): Promise => flushPendingWrites() /** @internal Reset coordinator state for testing purposes only */ export const resetCoordinator = () => { - if (server) { - server.close() - server = null + for (const app of apps.values()) { + app.server?.close() + app.session?.close() } - activeExtractions = 0 - totalExtractions = 0 - lastCompletedAt = 0 - pendingExtractStarts = 0 - idleThresholdMs = 2500 - quietMs = 10_000 - maxWaitMs = 60_000 - extractedFiles.clear() - fileNumToBucket.clear() - bucketToMembers = new Map() - canonicalMapRef = {} - expectedBaseFiles = new Set() + apps.clear() writeChain.clear() latestContent.clear() } diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index aa92f663e..0f749f24c 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -29,6 +29,7 @@ import { import { type NextConfig } from 'next' import { + openCoordinatorCompiler, type PrewarmedOutput, startCoordinator, takeExtractOutput, @@ -243,7 +244,10 @@ export function DevupUI( }) if (!existsSync(gitignoreFile)) writeFileSync(gitignoreFile, '*') - const wasm = loadWasm(engine) + // The engine is shared by every Next app in this process; this app works + // on an engine session of its own, so another app's setup or extraction + // never changes its numbering, sheet, theme or settings. + const wasm = openCoordinatorCompiler(distDir, loadWasm(engine)) const { codeExtract, codeExtractWithoutSourceMap, @@ -485,6 +489,7 @@ export function DevupUI( removeStalePortFile(coordinatorPortFile) const coordinator = startCoordinator({ + distDir, wasm, package: libPackage, cssDir,