use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar};
use std::time::Duration;
use super::registry::ProjectKey;
use crate::domain::{FreshnessStatus, RootBinding, StatePlacement};
const EMBED_OBSERVER_POLL: Duration = Duration::from_millis(200);
const REFRESHING_VISIBILITY_WINDOW: Duration = Duration::from_millis(25);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmbeddedIdentity(std::num::NonZeroU64);
static NEXT_EMBEDDED: AtomicU64 = AtomicU64::new(1);
impl EmbeddedIdentity {
pub fn fresh() -> Self {
let raw = NEXT_EMBEDDED.fetch_add(1, Ordering::Relaxed);
Self(std::num::NonZeroU64::new(raw).expect("embedded counter starts at 1"))
}
pub(crate) fn raw(&self) -> u64 {
self.0.get()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EmbedRefusal {
SourceAlreadyOpen {
held_by: EmbeddedIdentity,
},
WouldSelfWait,
AlreadyClosed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CloseReceipt {
identity: EmbeddedIdentity,
performed_shutdown: bool,
was_final_owner: bool,
}
impl CloseReceipt {
pub fn identity(&self) -> EmbeddedIdentity {
self.identity
}
pub fn performed_shutdown(&self) -> bool {
self.performed_shutdown
}
pub fn was_final_owner(&self) -> bool {
self.was_final_owner
}
}
thread_local! {
static FINALIZING: std::cell::Cell<Option<EmbeddedIdentity>> =
const { std::cell::Cell::new(None) };
}
#[derive(Debug, Default)]
pub struct EmbeddedSourceFactory {
open: std::sync::Mutex<HashMap<ProjectKey, OpenEmbeddedSource>>,
shutdown: AtomicBool,
}
#[derive(Debug)]
struct OpenEmbeddedSource {
identity: EmbeddedIdentity,
owner: Option<EmbeddedIdentity>,
opening: bool,
binding: Option<Arc<EmbeddedBinding>>,
}
#[cfg(feature = "__test-internals")]
static PANIC_AFTER_START_ROOT: std::sync::Mutex<Option<ProjectKey>> = std::sync::Mutex::new(None);
#[cfg(feature = "__test-internals")]
pub fn panic_after_start_for_test(root: &std::path::Path) {
let crate::domain::RootResolution::Bound(binding) = crate::discovery::resolve_root_candidate(
root,
crate::domain::RootCandidateSource::McpClientRoot,
crate::domain::RootRequestMode::Automatic,
) else {
panic!("test root must resolve to an embedded binding");
};
*PANIC_AFTER_START_ROOT
.lock()
.expect("embedded panic hook mutex") = Some(ProjectKey::new(&binding.root_id.0));
}
#[cfg(feature = "__test-internals")]
fn take_panic_after_start_for_test(key: &ProjectKey) -> bool {
let mut configured = PANIC_AFTER_START_ROOT
.lock()
.expect("embedded panic hook mutex");
if configured.as_ref() == Some(key) {
configured.take();
true
} else {
false
}
}
#[cfg(feature = "__test-internals")]
struct JoinEnteredState {
entered: std::sync::Mutex<bool>,
reached: Condvar,
}
#[cfg(feature = "__test-internals")]
static JOIN_ENTERED: std::sync::OnceLock<
std::sync::Mutex<HashMap<PathBuf, Arc<JoinEnteredState>>>,
> = std::sync::OnceLock::new();
#[cfg(feature = "__test-internals")]
pub struct JoinEnteredForTest {
root: PathBuf,
state: Arc<JoinEnteredState>,
}
#[cfg(feature = "__test-internals")]
pub fn watch_join_entered_for_test(root: &std::path::Path) -> JoinEnteredForTest {
let root = crate::live_index::store::normalize_root(root);
let state = Arc::new(JoinEnteredState {
entered: std::sync::Mutex::new(false),
reached: Condvar::new(),
});
let watches = JOIN_ENTERED.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
let previous = watches
.lock()
.expect("join-entered watch registry")
.insert(root.clone(), Arc::clone(&state));
assert!(
previous.is_none(),
"a root can hold only one join-entered watch"
);
JoinEnteredForTest { root, state }
}
#[cfg(feature = "__test-internals")]
impl JoinEnteredForTest {
pub fn wait_until_entered(&self, timeout: Duration) -> bool {
let entered = self.state.entered.lock().expect("join-entered watch");
let (entered, _) = self
.state
.reached
.wait_timeout_while(entered, timeout, |entered| !*entered)
.expect("join-entered watch");
*entered
}
}
#[cfg(feature = "__test-internals")]
impl Drop for JoinEnteredForTest {
fn drop(&mut self) {
let watches = JOIN_ENTERED.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
let mut watches = watches.lock().expect("join-entered watch registry");
if watches
.get(&self.root)
.is_some_and(|current| Arc::ptr_eq(current, &self.state))
{
watches.remove(&self.root);
}
}
}
#[cfg(feature = "__test-internals")]
fn notify_join_entered(root: &std::path::Path) {
let root = crate::live_index::store::normalize_root(root);
let Some(state) = JOIN_ENTERED
.get()
.and_then(|watches| watches.lock().ok()?.get(&root).cloned())
else {
return;
};
*state.entered.lock().expect("join-entered watch") = true;
state.reached.notify_all();
}
#[cfg(feature = "__test-internals")]
struct OpenAfterReserveState {
holding: std::sync::Mutex<bool>,
entered: std::sync::Mutex<bool>,
reached: Condvar,
}
#[cfg(feature = "__test-internals")]
static OPEN_AFTER_RESERVE: std::sync::OnceLock<
std::sync::Mutex<HashMap<ProjectKey, Arc<OpenAfterReserveState>>>,
> = std::sync::OnceLock::new();
#[cfg(feature = "__test-internals")]
pub struct OpenAfterReserveHoldForTest {
key: ProjectKey,
state: Arc<OpenAfterReserveState>,
}
#[cfg(feature = "__test-internals")]
pub fn hold_open_after_reserve_for_test(root: &std::path::Path) -> OpenAfterReserveHoldForTest {
let crate::domain::RootResolution::Bound(binding) = crate::discovery::resolve_root_candidate(
root,
crate::domain::RootCandidateSource::McpClientRoot,
crate::domain::RootRequestMode::Automatic,
) else {
panic!("test root must resolve to an embedded binding");
};
let key = ProjectKey::new(&binding.root_id.0);
let state = Arc::new(OpenAfterReserveState {
holding: std::sync::Mutex::new(true),
entered: std::sync::Mutex::new(false),
reached: Condvar::new(),
});
let holds = OPEN_AFTER_RESERVE.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
let previous = holds
.lock()
.expect("open-after-reserve hold registry")
.insert(key.clone(), Arc::clone(&state));
assert!(
previous.is_none(),
"a root can hold only one open-after-reserve gate"
);
OpenAfterReserveHoldForTest { key, state }
}
#[cfg(feature = "__test-internals")]
impl OpenAfterReserveHoldForTest {
pub fn wait_until_entered(&self, timeout: Duration) -> bool {
let entered = self.state.entered.lock().expect("open-after-reserve hold");
let (entered, _) = self
.state
.reached
.wait_timeout_while(entered, timeout, |entered| !*entered)
.expect("open-after-reserve hold");
*entered
}
pub fn release(&self) {
*self.state.holding.lock().expect("open-after-reserve hold") = false;
self.state.reached.notify_all();
}
}
#[cfg(feature = "__test-internals")]
impl Drop for OpenAfterReserveHoldForTest {
fn drop(&mut self) {
self.release();
let holds = OPEN_AFTER_RESERVE.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
let mut holds = holds.lock().expect("open-after-reserve hold registry");
if holds
.get(&self.key)
.is_some_and(|current| Arc::ptr_eq(current, &self.state))
{
holds.remove(&self.key);
}
}
}
#[cfg(feature = "__test-internals")]
fn pause_open_after_reserve_for_test(key: &ProjectKey) {
let Some(state) = OPEN_AFTER_RESERVE
.get()
.and_then(|holds| holds.lock().ok()?.get(key).cloned())
else {
return;
};
let mut entered = state.entered.lock().expect("open-after-reserve hold");
if *entered {
return;
}
*entered = true;
drop(entered);
state.reached.notify_all();
let holding = state.holding.lock().expect("open-after-reserve hold");
drop(
state
.reached
.wait_while(holding, |holding| *holding)
.expect("open-after-reserve hold"),
);
}
struct OpenRollback {
factory: Arc<EmbeddedSourceFactory>,
key: ProjectKey,
identity: EmbeddedIdentity,
binding: Option<Arc<EmbeddedBinding>>,
committed: bool,
}
impl OpenRollback {
fn reserve(
factory: Arc<EmbeddedSourceFactory>,
key: ProjectKey,
identity: EmbeddedIdentity,
) -> Self {
Self {
factory,
key,
identity,
binding: None,
committed: false,
}
}
fn bind(&mut self, binding: Arc<EmbeddedBinding>) {
self.binding = Some(binding);
}
fn commit(mut self) {
self.committed = true;
}
}
impl Drop for OpenRollback {
fn drop(&mut self) {
if self.committed {
return;
}
if let Some(binding) = &self.binding {
binding.shutdown();
}
let mut open = self
.factory
.open
.lock()
.expect("embedded registration mutex");
if open
.get(&self.key)
.is_some_and(|source| source.identity == self.identity && source.opening)
{
open.remove(&self.key);
}
self.factory
.shutdown
.store(open.is_empty(), Ordering::Release);
drop(open);
let _ = super::activation::process_project_registry().stop(&self.key);
}
}
#[derive(Debug)]
pub(crate) enum EmbeddedOpenError {
SourceAlreadyOpen,
AdmissionUnavailable,
WorkerUnavailable,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct EmbeddedShutdownReport {
pub closed_sources: u64,
pub joined_workers: u64,
}
#[derive(Debug, Clone)]
struct EmbeddedRuntimeState {
phase: super::public_api::SourceRuntimePhase,
current_publication_identity: Option<String>,
observer_epoch: u64,
source_version: u64,
change_revision: u64,
#[cfg(feature = "embed")]
lifecycle_epoch: u64,
}
#[derive(Debug, Default)]
struct WorkerControl {
stop: bool,
refresh_requested: bool,
reset_pending: bool,
}
#[cfg(feature = "embed")]
struct GraphLeaseEntry {
expires_at: std::time::Instant,
snapshot: Arc<super::embed_query::EmbeddedQuerySnapshot>,
}
#[cfg(feature = "embed")]
pub struct GraphPublicationLease {
binding: std::sync::Weak<EmbeddedBinding>,
id: u64,
source_id: u64,
lifecycle_epoch: u64,
}
#[cfg(feature = "embed")]
impl std::fmt::Debug for GraphPublicationLease {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GraphPublicationLease")
.finish_non_exhaustive()
}
}
#[cfg(feature = "embed")]
impl Drop for GraphPublicationLease {
fn drop(&mut self) {
if let Some(binding) = self.binding.upgrade() {
let mut state = binding.state.lock().expect("embedded state mutex");
let mut leases = binding.graph_leases.lock().expect("graph lease mutex");
let was_full = leases.len() >= 8;
let removed = leases.remove(&self.id).is_some();
drop(leases);
if was_full && removed {
binding.advance_change(&mut state);
}
}
}
}
#[cfg(feature = "embed")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceChangeVersion {
source_id: u64,
revision: u64,
}
struct EmbeddedBinding {
identity: EmbeddedIdentity,
key: ProjectKey,
root: PathBuf,
state_placement: StatePlacement,
runtime: super::activation::ProjectRuntimeHandle,
#[cfg(feature = "embed")]
authority: Arc<super::activation::ProjectSourceAuthority>,
#[cfg(feature = "embed")]
state_anchor: Option<AdmittedStateAnchor>,
#[cfg(feature = "embed")]
restored_mtimes: std::sync::Mutex<Option<HashMap<String, u64>>>,
#[cfg(feature = "embed")]
git_view: std::sync::Mutex<Option<Arc<super::embed_git::PreparedGitView>>>,
#[cfg(feature = "embed")]
temporal_cache: Arc<super::embed_temporal::TemporalCache>,
#[cfg(feature = "embed")]
impact_symbols: super::embed_file_impact::ImpactSymbolStore,
#[cfg(feature = "embed")]
stel_store: super::embed_stel::StelStoreSlot,
#[cfg(feature = "embed")]
open_reset: Option<crate::embed::parity::source_options::SnapshotResetReceipt>,
#[cfg(feature = "embed")]
control_directory: Option<PathBuf>,
#[cfg(feature = "embed")]
trust_mode: crate::embed::parity::source_options::ProjectConfigTrustMode,
state: std::sync::Mutex<EmbeddedRuntimeState>,
change_wake: Condvar,
#[cfg(feature = "embed")]
refresh_snapshot: std::sync::Mutex<Option<Arc<super::embed_query::EmbeddedQuerySnapshot>>>,
#[cfg(feature = "embed")]
graph_leases: std::sync::Mutex<HashMap<u64, GraphLeaseEntry>>,
#[cfg(feature = "embed")]
next_graph_lease: AtomicU64,
control: std::sync::Mutex<WorkerControl>,
wake: Condvar,
worker: std::sync::Mutex<Option<std::thread::JoinHandle<()>>>,
shutdown_started: AtomicBool,
progress: Arc<crate::live_index::store::ReloadProgressSink>,
}
#[cfg(feature = "embed")]
#[derive(Clone)]
pub(super) struct AdmittedStateAnchor {
lease: Arc<super::physical_root::PhysicalRootLease>,
stable_key: [u8; 16],
}
#[cfg(feature = "embed")]
impl AdmittedStateAnchor {
fn capture(
placement: &StatePlacement,
authority: &super::activation::ProjectSourceAuthority,
) -> Result<Option<Self>, EmbeddedOpenError> {
let opened = match placement {
StatePlacement::MemoryOnly { .. } => return Ok(None),
StatePlacement::ProjectLocal { .. } => authority
.with_source_anchor_read(|root| {
root.child_directory(std::path::Path::new(".symforge"), false)
})
.map_err(|_| EmbeddedOpenError::AdmissionUnavailable)?,
StatePlacement::UserLocal { directory, .. } => {
super::physical_root::PhysicalRootLease::take(directory.as_path())
}
};
let stable_key = opened
.opened_stable_key()
.ok_or(EmbeddedOpenError::AdmissionUnavailable)?;
Ok(Some(Self {
lease: Arc::new(opened.parked()),
stable_key,
}))
}
pub(super) fn lease(&self) -> &super::physical_root::PhysicalRootLease {
&self.lease
}
pub(super) fn stable_key(&self) -> [u8; 16] {
self.stable_key
}
}
impl std::fmt::Debug for EmbeddedBinding {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("EmbeddedBinding")
.field("identity", &self.identity)
.field("root", &self.root)
.field("state", &self.state.lock().expect("embedded state mutex"))
.finish_non_exhaustive()
}
}
impl EmbeddedBinding {
#[allow(clippy::too_many_arguments)]
fn new(
identity: EmbeddedIdentity,
key: ProjectKey,
root: PathBuf,
state_placement: StatePlacement,
runtime: super::activation::ProjectRuntimeHandle,
#[cfg(feature = "embed")] authority: Arc<super::activation::ProjectSourceAuthority>,
#[cfg(feature = "embed")] state_anchor: Option<AdmittedStateAnchor>,
#[cfg(feature = "embed")] restored_mtimes: Option<HashMap<String, u64>>,
#[cfg(feature = "embed")] open_reset: Option<
crate::embed::parity::source_options::SnapshotResetReceipt,
>,
#[cfg(feature = "embed")] control_directory: Option<PathBuf>,
#[cfg(feature = "embed")]
trust_mode: crate::embed::parity::source_options::ProjectConfigTrustMode,
) -> Arc<Self> {
Arc::new(Self {
identity,
key,
root,
state_placement,
runtime,
#[cfg(feature = "embed")]
authority,
#[cfg(feature = "embed")]
state_anchor,
#[cfg(feature = "embed")]
restored_mtimes: std::sync::Mutex::new(restored_mtimes),
#[cfg(feature = "embed")]
git_view: std::sync::Mutex::new(None),
#[cfg(feature = "embed")]
temporal_cache: Arc::default(),
#[cfg(feature = "embed")]
impact_symbols: Default::default(),
#[cfg(feature = "embed")]
stel_store: Default::default(),
#[cfg(feature = "embed")]
open_reset: open_reset.clone(),
#[cfg(feature = "embed")]
control_directory,
#[cfg(feature = "embed")]
trust_mode,
state: std::sync::Mutex::new(EmbeddedRuntimeState {
phase: super::public_api::SourceRuntimePhase::Loading,
current_publication_identity: None,
observer_epoch: 0,
source_version: 0,
change_revision: 0,
#[cfg(feature = "embed")]
lifecycle_epoch: 0,
}),
change_wake: Condvar::new(),
#[cfg(feature = "embed")]
refresh_snapshot: std::sync::Mutex::new(None),
#[cfg(feature = "embed")]
graph_leases: std::sync::Mutex::new(HashMap::new()),
#[cfg(feature = "embed")]
next_graph_lease: AtomicU64::new(1),
control: std::sync::Mutex::new(WorkerControl {
#[cfg(feature = "embed")]
reset_pending: open_reset.is_some(),
..WorkerControl::default()
}),
wake: Condvar::new(),
worker: std::sync::Mutex::new(None),
shutdown_started: AtomicBool::new(false),
progress: crate::live_index::store::ReloadProgressSink::new(),
})
}
fn start(self: &Arc<Self>) -> std::io::Result<()> {
let worker_binding = Arc::clone(self);
let worker = std::thread::Builder::new()
.name(format!("symforge-embed-{}", self.identity.raw()))
.spawn(move || worker_binding.run())?;
*self.worker.lock().expect("embedded worker mutex") = Some(worker);
Ok(())
}
fn run(&self) {
{
let mut state = self.state.lock().expect("embedded state mutex");
state.observer_epoch = 1;
}
#[cfg(feature = "embed")]
let restored = self
.restored_mtimes
.lock()
.expect("embedded restored seed mutex")
.take();
#[cfg(feature = "embed")]
let mut observed_fingerprint = if let Some(mtimes) = restored {
crate::live_index::persist::background_verify_bound(
self.runtime.data_plane(),
&self.root,
&mtimes,
&self.authority,
|| self.shutdown_started.load(Ordering::Acquire),
);
self.publish_observed_current()
} else {
self.reload_and_publish()
};
#[cfg(not(feature = "embed"))]
let mut observed_fingerprint = self.reload_and_publish();
self.complete_pending_reset();
loop {
let mut control = self.control.lock().expect("embedded control mutex");
let (next, _) = self
.wake
.wait_timeout_while(control, EMBED_OBSERVER_POLL, |control| {
!control.stop && !control.refresh_requested
})
.expect("embedded control mutex");
control = next;
if control.stop {
break;
}
let explicit_refresh = std::mem::take(&mut control.refresh_requested);
drop(control);
#[cfg(feature = "embed")]
{
let mut state = self.state.lock().expect("embedded state mutex");
self.prune_graph_leases(&mut state);
}
let next_fingerprint = self.observe_fingerprint();
if self.shutdown_started.load(Ordering::Acquire) {
break;
}
let disk_changed = match (&observed_fingerprint, &next_fingerprint) {
(Some(previous), Some(current)) => previous != current,
(None, Some(_)) | (Some(_), None) => true,
(None, None) => false,
};
let blocked = self.state.lock().expect("embedded state mutex").phase
== super::public_api::SourceRuntimePhase::Blocked;
if explicit_refresh || disk_changed || blocked {
self.set_phase(super::public_api::SourceRuntimePhase::Refreshing);
if self.wait_refresh_visibility_or_stop() {
break;
}
observed_fingerprint = self.reload_and_publish();
if self.shutdown_started.load(Ordering::Acquire) {
break;
}
self.complete_pending_reset();
} else {
observed_fingerprint = next_fingerprint;
}
}
self.set_phase(super::public_api::SourceRuntimePhase::Stopped);
#[cfg(feature = "embed")]
self.git_view
.lock()
.expect("embedded Git view mutex")
.take();
}
fn complete_pending_reset(&self) {
let mut control = self.control.lock().expect("embedded control mutex");
if !control.reset_pending {
return;
}
let current = self.state.lock().expect("embedded state mutex").phase
== super::public_api::SourceRuntimePhase::Current;
if current {
control.reset_pending = false;
drop(control);
self.runtime.data_plane().mark_index_folder_reset();
}
}
fn wait_refresh_visibility_or_stop(&self) -> bool {
let control = self.control.lock().expect("embedded control mutex");
let (control, _) = self
.wake
.wait_timeout_while(control, REFRESHING_VISIBILITY_WINDOW, |control| {
!control.stop
})
.expect("embedded control mutex");
control.stop
}
fn reload_and_publish(&self) -> Option<String> {
#[cfg(feature = "embed")]
if self.authority.verify_physical_root_anchor().is_err() {
self.set_blocked();
return None;
}
self.progress.reset();
let _progress = crate::live_index::store::register_reload_progress(
&self.root,
Arc::clone(&self.progress),
);
let exclusions = crate::discovery::SourceExclusions::for_state_placement(
&self.root,
&self.state_placement,
);
let result = self.runtime.data_plane().refresh_bound_tree_cancellable(
&self.root,
self.state_placement.directory().cloned(),
exclusions,
&self.shutdown_started,
);
if let Err(error) = result {
if self.shutdown_started.load(Ordering::Acquire)
|| crate::live_index::store::reload_was_cancelled(&error)
{
return None;
}
self.set_blocked();
return None;
}
self.publish_observed_current()
}
#[cfg(feature = "embed")]
fn freshen_exact_path(
&self,
relative_path: &str,
) -> Result<(), super::guidance::freshen::TargetedFreshenRefusal> {
if self.shutdown_started.load(Ordering::Acquire)
|| self.authority.verify_physical_root_anchor().is_err()
|| self.state.lock().expect("embedded state mutex").phase
!= super::public_api::SourceRuntimePhase::Current
{
return Ok(());
}
#[cfg(test)]
if let Some(hook) =
refresh_lifecycle_tests::BEFORE_FRESHEN_GENERATION.with(|hook| hook.take())
{
hook();
}
let shared = self.runtime.data_plane();
let expected_gen = shared.current_project_generation();
if super::guidance::freshen::freshen_exact_path(
shared,
expected_gen,
&self.root,
&self.authority,
relative_path,
)? {
self.publish_freshened();
}
Ok(())
}
#[cfg(feature = "embed")]
fn analyze_file_impact(
&self,
request: &crate::embed::parity::file_impact::FileImpactRequest,
stop: &dyn Fn() -> Result<(), crate::embed::parity::QueryRefusalKind>,
) -> Result<super::embed_file_impact::FileImpactAdmission, crate::embed::parity::QueryRefusalKind>
{
if self.shutdown_started.load(Ordering::Acquire)
|| self.authority.verify_physical_root_anchor().is_err()
|| self.state.lock().expect("embedded state mutex").phase
!= super::public_api::SourceRuntimePhase::Current
{
return Err(crate::embed::parity::QueryRefusalKind::SourceUnavailable);
}
let shared = self.runtime.data_plane();
let before = shared.published_generation().publication_generation;
let result = super::embed_file_impact::admit(
shared,
&self.root,
&self.authority,
&self.impact_symbols,
request,
stop,
);
if shared.published_generation().publication_generation != before {
self.publish_freshened();
}
result
}
#[cfg(feature = "embed")]
fn publish_freshened(&self) {
let mut state = self.state.lock().expect("embedded state mutex");
let published = self.runtime.data_plane().published_generation();
if self.shutdown_started.load(Ordering::Acquire)
|| state.phase != super::public_api::SourceRuntimePhase::Current
|| !matches!(published.freshness.as_ref(), FreshnessStatus::Current)
{
return;
}
state.source_version = state.source_version.saturating_add(1).max(1);
state.current_publication_identity = Some(format!(
"embed-publication-{}-{}",
self.identity.raw(),
published.publication_generation
));
self.advance_change(&mut state);
}
fn publish_observed_current(&self) -> Option<String> {
#[cfg(feature = "embed")]
if self.authority.verify_physical_root_anchor().is_err() {
self.set_blocked();
return None;
}
if self.shutdown_started.load(Ordering::Acquire) {
return None;
}
let Some(fingerprint) = self.observe_fingerprint() else {
if self.shutdown_started.load(Ordering::Acquire) {
return None;
}
self.set_blocked();
return None;
};
#[cfg(all(test, feature = "embed"))]
if let Some(hook) = refresh_lifecycle_tests::BEFORE_OBSERVED_STATE.with(|hook| hook.take())
{
hook();
}
let mut state = self.state.lock().expect("embedded state mutex");
if self.shutdown_started.load(Ordering::Acquire) {
return None;
}
let published = self.runtime.data_plane().published_generation();
if !matches!(published.freshness.as_ref(), FreshnessStatus::Current) {
drop(state);
self.set_blocked();
return Some(fingerprint);
}
state.source_version = state.source_version.saturating_add(1).max(1);
state.current_publication_identity = Some(format!(
"embed-publication-{}-{}",
self.identity.raw(),
published.publication_generation
));
state.phase = super::public_api::SourceRuntimePhase::Current;
#[cfg(feature = "embed")]
{
*self
.refresh_snapshot
.lock()
.expect("refresh snapshot mutex") = None;
}
self.advance_change(&mut state);
Some(fingerprint)
}
fn observe_fingerprint(&self) -> Option<String> {
if self.shutdown_started.load(Ordering::Acquire) {
return None;
}
#[cfg(feature = "embed")]
self.authority.verify_physical_root_anchor().ok()?;
let exclusions = crate::discovery::SourceExclusions::for_state_placement(
&self.root,
&self.state_placement,
);
let plan = crate::discovery::scout_repository_with_exclusions_cancellable(
&self.root,
&exclusions,
&self.shutdown_started,
)
.ok()?;
if self.shutdown_started.load(Ordering::Acquire) {
return None;
}
#[cfg(feature = "embed")]
self.authority.verify_physical_root_anchor().ok()?;
Some(crate::hash::digest_hex(format!("{plan:?}").as_bytes()))
}
#[cfg(feature = "embed")]
fn complete_query_snapshot(
&self,
state: &EmbeddedRuntimeState,
normalized: &[u8],
) -> Result<super::embed_query::EmbeddedQuerySnapshot, super::public_api::EmbedSourceRefusal>
{
use super::public_api::{
OperationKind, RetryAdvice, SourceRefusalKind, SourceRuntimePhase,
};
let refuse = |kind, retry| {
super::public_api::bound_source_refusal(
kind,
OperationKind::IndexCensus,
retry,
normalized,
)
};
if self.shutdown_started.load(Ordering::Acquire)
|| matches!(
state.phase,
SourceRuntimePhase::Stopping | SourceRuntimePhase::Stopped
)
{
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::Never,
));
}
let index = self
.runtime
.acquire()
.map_err(|_| refuse(SourceRefusalKind::AdmissionUnavailable, RetryAdvice::Never))?;
let authority = Arc::clone(&self.authority);
authority
.verify_physical_root_anchor()
.map_err(|_| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::Never))?;
if state.phase == SourceRuntimePhase::Refreshing {
return self
.refresh_snapshot
.lock()
.expect("refresh snapshot mutex")
.as_ref()
.filter(|snapshot| snapshot.lifecycle_epoch == state.lifecycle_epoch)
.map(|snapshot| (**snapshot).clone())
.ok_or_else(|| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::OnEvent));
}
if state.phase != SourceRuntimePhase::Current {
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::OnEvent,
));
}
let authority_publication = authority
.current_publication()
.ok_or_else(|| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::OnEvent))?;
let source_set = index.published_source_set();
let generation = source_set.current_generation();
let publication_identity = format!(
"embed-publication-{}-{}",
self.identity.raw(),
generation.publication_generation,
);
if state.current_publication_identity.as_deref() != Some(&publication_identity)
|| !matches!(generation.freshness.as_ref(), FreshnessStatus::Current)
|| authority.current_publication() != Some(authority_publication)
{
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::OnEvent,
));
}
if self.shutdown_started.load(Ordering::Acquire) {
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::Never,
));
}
let binding_identity = format!("source-{}", self.identity.raw());
let serving_publication_identity = super::embed_query::serving_publication_identity(
&binding_identity,
&publication_identity,
state.source_version,
);
Ok(super::embed_query::EmbeddedQuerySnapshot {
root: self.root.clone(),
project_state: self.state_placement.directory().cloned(),
state_dir: self
.state_placement
.directory()
.map(|directory| directory.as_path().to_path_buf()),
source_set,
generation,
binding_identity,
publication_identity,
serving_publication_identity,
source_version: state.source_version,
lifecycle_epoch: state.lifecycle_epoch,
source_phase: SourceRuntimePhase::Current,
authority_publication,
authority,
state_anchor: self.state_anchor.clone(),
temporal_cache: Arc::clone(&self.temporal_cache),
control_directory: self.control_directory.clone(),
trust_mode: self.trust_mode,
})
}
#[cfg(feature = "embed")]
fn prune_graph_leases(&self, state: &mut EmbeddedRuntimeState) {
let now = std::time::Instant::now();
let mut leases = self.graph_leases.lock().expect("graph lease mutex");
let was_full = leases.len() >= 8;
leases.retain(|_, entry| entry.expires_at > now);
if was_full && leases.len() < 8 {
self.advance_change(state);
}
}
fn advance_change(&self, state: &mut EmbeddedRuntimeState) {
state.change_revision = state.change_revision.wrapping_add(1);
self.change_wake.notify_all();
}
fn set_phase(&self, phase: super::public_api::SourceRuntimePhase) {
let mut state = self.state.lock().expect("embedded state mutex");
if self.shutdown_started.load(Ordering::Acquire)
&& !matches!(
phase,
super::public_api::SourceRuntimePhase::Stopping
| super::public_api::SourceRuntimePhase::Stopped
)
{
return;
}
if state.phase == phase {
return;
}
#[cfg(feature = "embed")]
if phase == super::public_api::SourceRuntimePhase::Refreshing
&& state.phase == super::public_api::SourceRuntimePhase::Current
{
let snapshot = self
.complete_query_snapshot(&state, b"refresh-snapshot")
.ok()
.map(|mut snapshot| {
snapshot.source_phase = super::public_api::SourceRuntimePhase::Refreshing;
Arc::new(snapshot)
});
*self
.refresh_snapshot
.lock()
.expect("refresh snapshot mutex") = snapshot;
}
#[cfg(feature = "embed")]
if matches!(
phase,
super::public_api::SourceRuntimePhase::Current
| super::public_api::SourceRuntimePhase::Blocked
| super::public_api::SourceRuntimePhase::Stopping
| super::public_api::SourceRuntimePhase::Stopped
) {
*self
.refresh_snapshot
.lock()
.expect("refresh snapshot mutex") = None;
}
state.phase = phase;
self.advance_change(&mut state);
if matches!(
phase,
super::public_api::SourceRuntimePhase::Blocked
| super::public_api::SourceRuntimePhase::Stopping
| super::public_api::SourceRuntimePhase::Stopped
) {
#[cfg(feature = "embed")]
{
state.lifecycle_epoch = state.lifecycle_epoch.wrapping_add(1);
self.graph_leases.lock().expect("graph lease mutex").clear();
}
}
}
fn set_blocked(&self) {
let mut state = self.state.lock().expect("embedded state mutex");
if self.shutdown_started.load(Ordering::Acquire) {
return;
}
if state.phase != super::public_api::SourceRuntimePhase::Blocked {
state.phase = super::public_api::SourceRuntimePhase::Blocked;
state.current_publication_identity = None;
#[cfg(feature = "embed")]
{
state.lifecycle_epoch = state.lifecycle_epoch.wrapping_add(1);
}
self.advance_change(&mut state);
}
#[cfg(feature = "embed")]
{
*self
.refresh_snapshot
.lock()
.expect("refresh snapshot mutex") = None;
self.graph_leases.lock().expect("graph lease mutex").clear();
}
}
fn runtime_view(&self) -> super::public_api::SourceRuntimeView {
let state = self.state.lock().expect("embedded state mutex").clone();
super::public_api::SourceRuntimeView {
binding_identity: format!("source-{}", self.identity.raw()),
current_publication_identity: state.current_publication_identity,
observer_epoch: state.observer_epoch,
phase: state.phase,
source_version: state.source_version,
}
}
fn current_claim(
&self,
operation: crate::lifecycle_identity::OperationKind,
normalized_arguments: &[u8],
) -> Result<CurrentEmbedClaim, super::public_api::EmbedSourceRefusal> {
let state = self.state.lock().expect("embedded state mutex");
if state.phase != super::public_api::SourceRuntimePhase::Current {
let retry = match state.phase {
super::public_api::SourceRuntimePhase::Blocked => {
crate::lifecycle_identity::RetryAdvice::Operator
}
super::public_api::SourceRuntimePhase::Stopped
| super::public_api::SourceRuntimePhase::Stopping => {
crate::lifecycle_identity::RetryAdvice::Never
}
_ => crate::lifecycle_identity::RetryAdvice::OnEvent,
};
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
operation,
retry,
normalized_arguments,
));
}
let publication_identity = state
.current_publication_identity
.clone()
.expect("Current embedded source has a publication identity");
Ok(CurrentEmbedClaim {
binding_identity: format!("source-{}", self.identity.raw()),
publication_identity,
source_version: state.source_version,
})
}
fn request_refresh(&self, reset: bool) -> Option<u64> {
let state = self.state.lock().expect("embedded state mutex");
if matches!(
state.phase,
super::public_api::SourceRuntimePhase::Stopped
| super::public_api::SourceRuntimePhase::Stopping
) {
return None;
}
let version = state.source_version;
drop(state);
let mut control = self.control.lock().expect("embedded control mutex");
control.refresh_requested = true;
control.reset_pending |= reset;
self.wake.notify_all();
Some(version)
}
fn shutdown(&self) -> (u64, u64) {
if self.shutdown_started.swap(true, Ordering::AcqRel) {
let version = self
.state
.lock()
.expect("embedded state mutex")
.source_version;
return (version, 0);
}
self.set_phase(super::public_api::SourceRuntimePhase::Stopping);
{
let mut control = self.control.lock().expect("embedded control mutex");
control.stop = true;
self.wake.notify_all();
}
let worker = self.worker.lock().expect("embedded worker mutex").take();
#[cfg(feature = "__test-internals")]
notify_join_entered(&self.root);
let joined = worker
.map(|worker| {
let _ = worker.join();
1
})
.unwrap_or(0);
self.set_phase(super::public_api::SourceRuntimePhase::Stopped);
if let Err(refusal) = super::activation::process_project_registry().stop(&self.key) {
tracing::debug!(?refusal, "embedded admission stop refused");
}
let version = self
.state
.lock()
.expect("embedded state mutex")
.source_version;
(version, joined)
}
}
struct CurrentEmbedClaim {
binding_identity: String,
publication_identity: String,
source_version: u64,
}
fn normalized_path_prefix(prefix: Option<&str>) -> Option<String> {
prefix.map(|prefix| {
prefix
.replace('\\', "/")
.trim_start_matches("./")
.trim_end_matches('/')
.to_string()
})
}
fn path_matches_prefix(path: &str, prefix: Option<&str>) -> bool {
match normalized_path_prefix(prefix) {
None => true,
Some(prefix) if prefix.is_empty() => true,
Some(prefix) => crate::live_index::search::PathScope::prefix(prefix).matches(path),
}
}
fn knowledge_lifecycle_label(
lifecycle: crate::live_index::knowledge_authority::KnowledgeLifecycle,
) -> String {
format!("{lifecycle:?}").to_ascii_lowercase()
}
fn knowledge_coverage_label(
coverage: &crate::live_index::knowledge_bridge::DerivedCoverage,
) -> String {
match coverage {
crate::live_index::knowledge_bridge::DerivedCoverage::Complete => "complete".to_string(),
crate::live_index::knowledge_bridge::DerivedCoverage::Loading => "loading".to_string(),
crate::live_index::knowledge_bridge::DerivedCoverage::Truncated { .. } => {
"truncated".to_string()
}
}
}
fn knowledge_withheld_label(
reason: crate::live_index::knowledge_retrieve::KnowledgeWithheldReason,
) -> String {
match reason {
crate::live_index::knowledge_retrieve::KnowledgeWithheldReason::PolicyWithheld => {
"policy_withheld".to_string()
}
}
}
fn bounded_preview(line: &str) -> String {
const MAX_PREVIEW_CHARS: usize = 500;
let mut chars = line.chars();
let preview: String = chars.by_ref().take(MAX_PREVIEW_CHARS).collect();
if chars.next().is_some() {
format!("{preview}…")
} else {
preview
}
}
impl EmbeddedSourceFactory {
pub fn new() -> Arc<Self> {
Arc::new(Self::default())
}
#[cfg(feature = "__test-internals")]
pub fn open(self: &Arc<Self>, key: ProjectKey) -> Result<EmbeddedSourceHandle, EmbedRefusal> {
let mut open = self.open.lock().expect("embedded registration mutex");
if let Some(held_by) = open.get(&key) {
return Err(EmbedRefusal::SourceAlreadyOpen {
held_by: held_by.identity,
});
}
let identity = EmbeddedIdentity::fresh();
open.insert(
key.clone(),
OpenEmbeddedSource {
identity,
owner: None,
opening: false,
binding: None,
},
);
self.shutdown.store(false, Ordering::Release);
Ok(EmbeddedSourceHandle {
identity,
key,
registration: Arc::clone(self),
binding: None,
closed: AtomicBool::new(false),
_not_unwind_safe: std::marker::PhantomData,
})
}
pub(crate) fn open_bound(
self: &Arc<Self>,
binding: RootBinding,
state_placement: StatePlacement,
owner: EmbeddedIdentity,
) -> Result<EmbeddedSourceHandle, EmbeddedOpenError> {
self.open_bound_with_reset(
binding,
state_placement,
owner,
false,
None,
#[cfg(feature = "embed")]
Default::default(),
)
}
pub(crate) fn open_bound_with_reset(
self: &Arc<Self>,
binding: RootBinding,
state_placement: StatePlacement,
owner: EmbeddedIdentity,
reset_snapshot_state: bool,
control_directory: Option<PathBuf>,
#[cfg(feature = "embed")]
trust_mode: crate::embed::parity::source_options::ProjectConfigTrustMode,
) -> Result<EmbeddedSourceHandle, EmbeddedOpenError> {
let key = ProjectKey::new(&binding.root_id.0);
let identity = EmbeddedIdentity::fresh();
{
let mut open = self.open.lock().expect("embedded registration mutex");
if open.contains_key(&key) {
return Err(EmbeddedOpenError::SourceAlreadyOpen);
}
open.insert(
key.clone(),
OpenEmbeddedSource {
identity,
owner: Some(owner),
opening: true,
binding: None,
},
);
self.shutdown.store(false, Ordering::Release);
}
#[cfg(feature = "__test-internals")]
pause_open_after_reserve_for_test(&key);
let mut rollback = OpenRollback::reserve(Arc::clone(self), key.clone(), identity);
let admission = super::activation::admit_project_with_outcome(
super::process_runtime::SurfaceKind::Embed,
&binding.canonical_root,
&binding.root_id.0,
binding.access_mode,
&state_placement,
)
.map_err(|_| EmbeddedOpenError::AdmissionUnavailable)?;
#[cfg(feature = "embed")]
let authority = {
let candidate = super::activation::project_source_authority(&binding.canonical_root);
let admitted_root = admission
.slot()
.binding()
.map_err(|_| EmbeddedOpenError::AdmissionUnavailable)?
.physical_root();
if candidate.admission_binding().physical_root() != admitted_root {
return Err(EmbeddedOpenError::AdmissionUnavailable);
}
candidate
};
#[cfg(feature = "embed")]
let state_anchor = AdmittedStateAnchor::capture(&state_placement, &authority)?;
#[cfg(feature = "embed")]
let open_reset = if reset_snapshot_state {
let report = crate::live_index::persist::reset_snapshot_state(
&binding.canonical_root,
&state_placement,
)
.map_err(|_| EmbeddedOpenError::AdmissionUnavailable)?;
Some(crate::embed::parity::source_options::SnapshotResetReceipt::from_report(&report))
} else {
None
};
#[cfg(not(feature = "embed"))]
let _ = (reset_snapshot_state, control_directory);
#[cfg(feature = "embed")]
let restored = super::embed_restore::load_admitted_snapshot(
&binding.canonical_root,
&state_placement,
state_anchor.as_ref(),
&authority,
)
.map_err(|_| EmbeddedOpenError::AdmissionUnavailable)?;
#[cfg(feature = "embed")]
let (index, restored_mtimes) = match restored {
Some(seed) => (seed.index, Some(seed.mtimes)),
None => (crate::live_index::store::LiveIndex::empty(), None),
};
#[cfg(not(feature = "embed"))]
let index = crate::live_index::store::LiveIndex::empty();
let runtime =
super::activation::ProjectRuntimeHandle::bind_admitted(index, admission.into_slot());
let source = EmbeddedBinding::new(
identity,
key.clone(),
binding.canonical_root,
state_placement,
runtime,
#[cfg(feature = "embed")]
authority,
#[cfg(feature = "embed")]
state_anchor,
#[cfg(feature = "embed")]
restored_mtimes,
#[cfg(feature = "embed")]
open_reset,
#[cfg(feature = "embed")]
control_directory,
#[cfg(feature = "embed")]
trust_mode,
);
rollback.bind(Arc::clone(&source));
if source.start().is_err() {
return Err(EmbeddedOpenError::WorkerUnavailable);
}
#[cfg(feature = "__test-internals")]
if take_panic_after_start_for_test(&key) {
panic!("embedded open panic injected after worker start");
}
let promoted = {
let mut open = self.open.lock().expect("embedded registration mutex");
match open.get_mut(&key) {
Some(reservation) if reservation.identity == identity && reservation.opening => {
reservation.opening = false;
reservation.binding = Some(Arc::clone(&source));
true
}
_ => false,
}
};
if !promoted {
return Err(EmbeddedOpenError::WorkerUnavailable);
}
rollback.commit();
Ok(EmbeddedSourceHandle {
identity,
key,
registration: Arc::clone(self),
binding: Some(source),
closed: AtomicBool::new(false),
_not_unwind_safe: std::marker::PhantomData,
})
}
pub fn open_count(&self) -> usize {
self.open.lock().expect("embedded registration mutex").len()
}
pub fn has_shut_down(&self) -> bool {
self.shutdown.load(Ordering::Acquire)
}
fn close_one(&self, key: &ProjectKey, identity: EmbeddedIdentity) -> (bool, bool, u64, u64) {
let (matched, source) = {
let open = self.open.lock().expect("embedded registration mutex");
let matched = open
.get(key)
.is_some_and(|source| source.identity == identity);
let source = open
.get(key)
.filter(|source| source.identity == identity)
.and_then(|source| source.binding.as_ref().map(Arc::clone));
(matched, source)
};
let (terminal_version, joined_workers) = source
.as_ref()
.map(|source| source.shutdown())
.unwrap_or((0, 0));
let (performed, final_owner) = if matched {
let mut open = self.open.lock().expect("embedded registration mutex");
if open
.get(key)
.is_some_and(|source| source.identity == identity)
{
open.remove(key);
let final_owner = open.is_empty();
if final_owner {
self.shutdown.store(true, Ordering::Release);
}
(true, final_owner)
} else {
(false, false)
}
} else {
(false, false)
};
(performed, final_owner, terminal_version, joined_workers)
}
pub(crate) fn shutdown_owner(&self, owner: EmbeddedIdentity) -> EmbeddedShutdownReport {
let closing: Vec<(ProjectKey, Option<Arc<EmbeddedBinding>>)> = {
let open = self.open.lock().expect("embedded registration mutex");
open.iter()
.filter(|(_, source)| source.owner == Some(owner))
.map(|(key, source)| (key.clone(), source.binding.clone()))
.collect()
};
let mut closed_sources = 0u64;
let mut joined_workers = 0u64;
for (_, binding) in &closing {
let joined = binding
.as_ref()
.map(|binding| binding.shutdown().1)
.unwrap_or(0);
closed_sources = closed_sources.saturating_add(1);
joined_workers = joined_workers.saturating_add(joined);
}
let mut open = self.open.lock().expect("embedded registration mutex");
for (key, _) in &closing {
if open
.get(key)
.is_some_and(|source| source.owner == Some(owner))
{
open.remove(key);
}
}
self.shutdown.store(open.is_empty(), Ordering::Release);
EmbeddedShutdownReport {
closed_sources,
joined_workers,
}
}
}
#[cfg(feature = "embed")]
impl super::public_api::ProcessRuntimeApi {
pub fn open_embedded_source_with_options(
&self,
spec: super::public_api::EmbeddedSourceSpec,
options: crate::embed::parity::source_options::EmbeddedOpenOptions,
) -> Result<EmbeddedSourceHandle, super::public_api::EmbedSourceRefusal> {
use super::public_api::{
OperationKind, RetryAdvice, SourceRefusalKind, bound_source_refusal,
};
use crate::embed::parity::source_options::{StateSelectionError, select_state_placement};
let normalized = format!(
"current_worktree={:?};state={:?};allow_protected_root={};reset_snapshot_state={};replay_control_directory={:?};project_config_trust_mode={:?}",
spec.root,
options.state,
options.allow_protected_root,
options.reset_snapshot_state,
options.replay_control_directory,
options.project_config_trust_mode
);
let refuse = |kind, retry| {
bound_source_refusal(
kind,
OperationKind::OpenEmbeddedSource,
retry,
normalized.as_bytes(),
)
};
let (candidate_source, request_mode) = if options.allow_protected_root {
(
crate::domain::RootCandidateSource::ExplicitIndexFolder,
crate::domain::RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
)
} else {
(
crate::domain::RootCandidateSource::McpClientRoot,
crate::domain::RootRequestMode::Automatic,
)
};
let binding = match crate::discovery::resolve_root_candidate(
&spec.root,
candidate_source,
request_mode,
) {
crate::domain::RootResolution::Bound(binding) => binding,
crate::domain::RootResolution::Unbound { .. } => {
return Err(refuse(
SourceRefusalKind::InvalidSelection,
RetryAdvice::Operator,
));
}
};
if options
.replay_control_directory
.as_deref()
.is_some_and(|directory| !directory.is_absolute() || !directory.is_dir())
{
return Err(refuse(
SourceRefusalKind::InvalidSelection,
RetryAdvice::Operator,
));
}
let placement =
select_state_placement(&binding, &options).map_err(|error| match error {
StateSelectionError::InvalidSelection => {
refuse(SourceRefusalKind::InvalidSelection, RetryAdvice::Operator)
}
StateSelectionError::Unavailable => refuse(
SourceRefusalKind::AdmissionUnavailable,
RetryAdvice::Operator,
),
})?;
self.owner
.factory()
.open_bound_with_reset(
binding,
placement,
self.owner.identity(),
options.reset_snapshot_state,
options
.replay_control_directory
.map(|directory| directory.join("embed-host")),
options.project_config_trust_mode,
)
.map_err(|error| match error {
EmbeddedOpenError::SourceAlreadyOpen => refuse(
SourceRefusalKind::SelectionUnavailable,
RetryAdvice::OnEvent,
),
EmbeddedOpenError::AdmissionUnavailable => refuse(
SourceRefusalKind::AdmissionUnavailable,
RetryAdvice::Automatic,
),
EmbeddedOpenError::WorkerUnavailable => {
refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::Automatic)
}
})
}
}
#[derive(Debug)]
pub(crate) struct EmbeddedRuntimeOwner {
identity: EmbeddedIdentity,
factory: Arc<EmbeddedSourceFactory>,
}
impl EmbeddedRuntimeOwner {
pub(crate) fn new(factory: Arc<EmbeddedSourceFactory>) -> Arc<Self> {
Arc::new(Self {
identity: EmbeddedIdentity::fresh(),
factory,
})
}
pub(crate) fn factory(&self) -> &Arc<EmbeddedSourceFactory> {
&self.factory
}
pub(crate) fn identity(&self) -> EmbeddedIdentity {
self.identity
}
pub(crate) fn shutdown(&self) -> EmbeddedShutdownReport {
self.factory.shutdown_owner(self.identity)
}
}
impl Drop for EmbeddedRuntimeOwner {
fn drop(&mut self) {
self.shutdown();
}
}
#[derive(Debug)]
pub struct EmbeddedSourceHandle {
identity: EmbeddedIdentity,
key: ProjectKey,
registration: Arc<EmbeddedSourceFactory>,
binding: Option<Arc<EmbeddedBinding>>,
closed: AtomicBool,
_not_unwind_safe: super::public_api::NotUnwindSafe,
}
impl EmbeddedSourceHandle {
#[cfg(feature = "embed")]
pub fn prepare_git_view(
&self,
options: &crate::embed::parity::source_options::GitPreparationOptions,
control: &crate::embed::parity::host::OperationControl,
) -> Result<
crate::embed::parity::source_options::GitPreparationClaim,
crate::embed::parity::source_options::GitPreparationRefusal,
> {
use crate::embed::parity::source_options::{
GitPreparationRefusal, GitPreparationRefusalKind,
};
let unavailable = || GitPreparationRefusal {
kind: GitPreparationRefusalKind::SourceUnavailable,
};
let snapshot = self
.capture_query_snapshot(b"symforge.embed.git-preparation.v1")
.map_err(|_| unavailable())?;
let binding = self.binding.as_ref().ok_or_else(unavailable)?;
let mut current = binding.git_view.lock().expect("embedded Git view mutex");
let (prepared, claim) =
super::embed_git::prepare(&snapshot, options, control, current.as_ref())?;
let final_snapshot = self
.capture_query_snapshot(b"symforge.embed.git-preparation.v1")
.map_err(|_| unavailable())?;
if final_snapshot.serving_publication_identity != snapshot.serving_publication_identity
|| binding.shutdown_started.load(Ordering::Acquire)
{
return Err(unavailable());
}
control.check().map_err(|stop| GitPreparationRefusal {
kind: match stop {
crate::embed::parity::host::OperationStop::Cancelled => {
GitPreparationRefusalKind::Cancelled
}
crate::embed::parity::host::OperationStop::DeadlineExceeded => {
GitPreparationRefusalKind::DeadlineExceeded
}
},
})?;
*current = Some(prepared);
Ok(claim)
}
#[cfg(all(feature = "embed", feature = "__test-internals"))]
pub fn prepared_git_repository_for_test(&self) -> Option<git2::Repository> {
let view = self.binding.as_ref()?.git_view.lock().ok()?.clone()?;
view.repository().ok()
}
#[cfg(feature = "embed")]
pub fn query_allowing_refresh(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_allowing_refresh(self, request, limits, None)
}
#[cfg(feature = "embed")]
pub fn query_allowing_refresh_with_control(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
control: &crate::embed::parity::host::OperationControl,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_allowing_refresh(self, request, limits, Some(control))
}
#[cfg(feature = "embed")]
pub fn query_with_graph_lease(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
lease: &GraphPublicationLease,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_graph_lease(self, request, limits, None, lease)
}
#[cfg(feature = "embed")]
pub fn query_with_graph_lease_and_control(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
lease: &GraphPublicationLease,
control: &crate::embed::parity::host::OperationControl,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_graph_lease(self, request, limits, Some(control), lease)
}
#[cfg(feature = "embed")]
pub fn query(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute(self, request, limits)
}
#[cfg(feature = "embed")]
pub fn validate_syntax_from_disk(
&self,
path: &str,
limits: crate::embed::parity::QueryLimits,
) -> Result<crate::embed::parity::DiskSyntaxObservation, super::embed_query::QueryRefusal> {
super::embed_query::validate_syntax_from_disk(self, path, limits)
}
#[cfg(feature = "embed")]
pub fn query_with_control(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
control: &crate::embed::parity::host::OperationControl,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_control(self, request, limits, control)
}
#[cfg(feature = "embed")]
pub fn new_query_session(
&self,
) -> Result<crate::embed::parity::session::QuerySession, super::public_api::EmbedSourceRefusal>
{
let snapshot = self.capture_query_snapshot(b"symforge.embed.session.v1")?;
Ok(super::embed_session::QuerySession::new(&snapshot))
}
#[cfg(feature = "embed")]
pub fn new_query_session_with_limits(
&self,
limits: crate::embed::parity::session::SessionCacheLimits,
) -> Result<
crate::embed::parity::session::QuerySession,
crate::embed::parity::session::SessionCreationRefusal,
> {
use crate::embed::parity::session::{SessionCacheLimits, SessionCreationRefusal};
let defaults = SessionCacheLimits::default();
if limits.max_bytes > defaults.max_bytes || limits.max_entries > defaults.max_entries {
return Err(SessionCreationRefusal::InvalidLimits);
}
let snapshot = self
.capture_query_snapshot(b"symforge.embed.session.v1")
.map_err(|_| SessionCreationRefusal::SourceUnavailable)?;
Ok(super::embed_session::QuerySession::with_limits(
&snapshot, limits,
))
}
#[cfg(feature = "embed")]
pub fn query_with_session(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
session: &crate::embed::parity::session::QuerySession,
control: Option<&crate::embed::parity::host::OperationControl>,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_session(self, request, limits, session, control)
}
#[cfg(feature = "embed")]
pub fn query_with_policy(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
policy: crate::embed::parity::QueryPolicy,
session: Option<&crate::embed::parity::session::QuerySession>,
control: Option<&crate::embed::parity::host::OperationControl>,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_policy(self, request, limits, policy, session, control)
}
#[cfg(feature = "embed")]
pub(crate) fn query_with_policy_admitted(
&self,
request: &crate::embed::parity::QueryRequest,
limits: crate::embed::parity::QueryLimits,
policy: crate::embed::parity::QueryPolicy,
session: Option<&crate::embed::parity::session::QuerySession>,
control: Option<&crate::embed::parity::host::OperationControl>,
admit: impl FnOnce(&super::embed_query::QueryClaim) -> bool,
) -> Result<super::embed_query::QueryClaim, super::embed_query::QueryRefusal> {
super::embed_query::execute_with_policy_admitted(
self, request, limits, policy, session, control, admit,
)
}
#[cfg(feature = "embed")]
pub(super) fn bound_replay_placement(&self) -> Option<(PathBuf, Option<PathBuf>)> {
if self.closed.load(Ordering::Acquire) {
return None;
}
let binding = self.binding.as_ref()?;
let state = binding.state.lock().expect("embedded state mutex");
if binding.shutdown_started.load(Ordering::Acquire)
|| !matches!(
state.phase,
super::public_api::SourceRuntimePhase::Loading
| super::public_api::SourceRuntimePhase::Current
| super::public_api::SourceRuntimePhase::Refreshing
)
|| self.closed.load(Ordering::Acquire)
{
return None;
}
Some((
binding.root.clone(),
binding
.state_placement
.directory()
.map(|directory| directory.as_path().to_path_buf()),
))
}
#[cfg(feature = "embed")]
pub(super) fn bound_replay_authority(
&self,
) -> Option<Arc<super::activation::ProjectSourceAuthority>> {
self.bound_replay_placement()?;
Some(Arc::clone(&self.binding.as_ref()?.authority))
}
#[cfg(feature = "embed")]
pub(super) fn bound_state_anchor(&self) -> Option<AdmittedStateAnchor> {
self.bound_replay_placement()?;
self.binding.as_ref()?.state_anchor.clone()
}
#[cfg(feature = "embed")]
pub(super) fn freshen_exact_path(
&self,
relative_path: &str,
) -> Result<(), super::guidance::freshen::TargetedFreshenRefusal> {
match &self.binding {
Some(binding) if !self.closed.load(Ordering::Acquire) => {
binding.freshen_exact_path(relative_path)
}
_ => Ok(()),
}
}
#[cfg(feature = "embed")]
pub(super) fn analyze_file_impact(
&self,
request: &crate::embed::parity::file_impact::FileImpactRequest,
stop: &dyn Fn() -> Result<(), crate::embed::parity::QueryRefusalKind>,
) -> Result<super::embed_file_impact::FileImpactAdmission, crate::embed::parity::QueryRefusalKind>
{
match &self.binding {
Some(binding) if !self.closed.load(Ordering::Acquire) => {
binding.analyze_file_impact(request, stop)
}
_ => Err(crate::embed::parity::QueryRefusalKind::SourceUnavailable),
}
}
#[cfg(feature = "embed")]
pub(super) fn capture_query_snapshot_allowing_refresh(
&self,
normalized: &[u8],
) -> Result<super::embed_query::EmbeddedQuerySnapshot, super::public_api::EmbedSourceRefusal>
{
use super::public_api::{OperationKind, RetryAdvice, SourceRefusalKind};
let unavailable = || {
super::public_api::bound_source_refusal(
SourceRefusalKind::SourceUnavailable,
OperationKind::IndexCensus,
RetryAdvice::Never,
normalized,
)
};
if self.closed.load(Ordering::Acquire) {
return Err(unavailable());
}
let binding = self.binding.as_ref().ok_or_else(unavailable)?;
let state = binding.state.lock().expect("embedded state mutex");
let snapshot = binding.complete_query_snapshot(&state, normalized)?;
if self.closed.load(Ordering::Acquire) {
return Err(unavailable());
}
Ok(snapshot)
}
#[cfg(feature = "embed")]
pub(super) fn validate_refresh_snapshot(
&self,
lifecycle_epoch: u64,
normalized: &[u8],
) -> Result<(), super::public_api::EmbedSourceRefusal> {
use super::public_api::{
OperationKind, RetryAdvice, SourceRefusalKind, SourceRuntimePhase,
};
let refuse = |kind, retry| {
super::public_api::bound_source_refusal(
kind,
OperationKind::IndexCensus,
retry,
normalized,
)
};
if self.closed.load(Ordering::Acquire) {
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::Never,
));
}
let binding = self
.binding
.as_ref()
.ok_or_else(|| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::Never))?;
let state = binding.state.lock().expect("embedded state mutex");
if self.closed.load(Ordering::Acquire)
|| binding.shutdown_started.load(Ordering::Acquire)
|| matches!(
state.phase,
SourceRuntimePhase::Stopping | SourceRuntimePhase::Stopped
)
{
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::Never,
));
}
binding
.runtime
.acquire()
.map_err(|_| refuse(SourceRefusalKind::AdmissionUnavailable, RetryAdvice::Never))?;
binding
.authority
.verify_physical_root_anchor()
.map_err(|_| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::Never))?;
if lifecycle_epoch != state.lifecycle_epoch
|| !matches!(
state.phase,
SourceRuntimePhase::Current | SourceRuntimePhase::Refreshing
)
{
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::OnEvent,
));
}
Ok(())
}
#[cfg(feature = "embed")]
pub fn begin_graph_publication_lease(
&self,
timeout: Duration,
) -> Result<GraphPublicationLease, super::public_api::EmbedSourceRefusal> {
use super::public_api::{OperationKind, RetryAdvice, SourceRefusalKind};
let normalized = b"begin-graph-publication-lease";
let unavailable = || {
super::public_api::bound_source_refusal(
SourceRefusalKind::SourceUnavailable,
OperationKind::IndexCensus,
RetryAdvice::Never,
normalized,
)
};
if self.closed.load(Ordering::Acquire) {
return Err(unavailable());
}
let binding = self.binding.as_ref().ok_or_else(unavailable)?;
let mut state = binding.state.lock().expect("embedded state mutex");
if self.closed.load(Ordering::Acquire) {
return Err(unavailable());
}
let snapshot = binding.complete_query_snapshot(&state, normalized)?;
binding.prune_graph_leases(&mut state);
let mut leases = binding.graph_leases.lock().expect("graph lease mutex");
if leases.len() >= 8 {
return Err(super::public_api::bound_source_refusal(
SourceRefusalKind::AdmissionUnavailable,
OperationKind::IndexCensus,
RetryAdvice::OnEvent,
normalized,
));
}
let id = binding.next_graph_lease.fetch_add(1, Ordering::Relaxed);
let duration = timeout.min(Duration::from_secs(30));
let expires_at = std::time::Instant::now()
.checked_add(duration)
.unwrap_or_else(std::time::Instant::now);
leases.insert(
id,
GraphLeaseEntry {
expires_at,
snapshot: Arc::new(snapshot),
},
);
if self.closed.load(Ordering::Acquire) {
leases.remove(&id);
return Err(super::public_api::bound_source_refusal(
SourceRefusalKind::SourceUnavailable,
OperationKind::IndexCensus,
RetryAdvice::Never,
normalized,
));
}
if leases.len() == 8 {
binding.change_wake.notify_all();
}
Ok(GraphPublicationLease {
binding: Arc::downgrade(binding),
id,
source_id: self.identity.raw(),
lifecycle_epoch: state.lifecycle_epoch,
})
}
#[cfg(feature = "embed")]
pub(super) fn capture_graph_lease_snapshot(
&self,
lease: &GraphPublicationLease,
) -> Result<super::embed_query::EmbeddedQuerySnapshot, crate::embed::parity::QueryRefusalKind>
{
use crate::embed::parity::QueryRefusalKind;
let Some(binding) = self.binding.as_ref() else {
return Err(QueryRefusalKind::StaleHandle);
};
if lease.source_id != self.identity.raw()
|| lease
.binding
.upgrade()
.is_none_or(|leased| !Arc::ptr_eq(&leased, binding))
{
return Err(QueryRefusalKind::StaleHandle);
}
let mut state = binding.state.lock().expect("embedded state mutex");
if self.closed.load(Ordering::Acquire)
|| binding.authority.verify_physical_root_anchor().is_err()
|| matches!(
state.phase,
super::public_api::SourceRuntimePhase::Blocked
| super::public_api::SourceRuntimePhase::Stopping
| super::public_api::SourceRuntimePhase::Stopped
)
{
return Err(QueryRefusalKind::SourceUnavailable);
}
if binding.runtime.acquire().is_err() {
return Err(QueryRefusalKind::AdmissionUnavailable);
}
if lease.lifecycle_epoch != state.lifecycle_epoch {
return Err(QueryRefusalKind::SourceUnavailable);
}
binding.prune_graph_leases(&mut state);
binding
.graph_leases
.lock()
.expect("graph lease mutex")
.get(&lease.id)
.map(|entry| (*entry.snapshot).clone())
.ok_or(QueryRefusalKind::GraphPublicationLeaseExpired)
}
#[cfg(feature = "embed")]
pub(super) fn validate_graph_lease(
&self,
lease: &GraphPublicationLease,
) -> Result<(), crate::embed::parity::QueryRefusalKind> {
self.capture_graph_lease_snapshot(lease).map(|_| ())
}
#[cfg(feature = "embed")]
pub(super) fn capture_query_snapshot(
&self,
normalized: &[u8],
) -> Result<super::embed_query::EmbeddedQuerySnapshot, super::public_api::EmbedSourceRefusal>
{
use super::public_api::{
OperationKind, RetryAdvice, SourceRefusalKind, SourceRuntimePhase,
};
let refuse = |kind, retry| {
super::public_api::bound_source_refusal(
kind,
OperationKind::IndexCensus,
retry,
normalized,
)
};
if self.closed.load(Ordering::Acquire) {
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::Never,
));
}
let binding = self
.binding
.as_ref()
.ok_or_else(|| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::Never))?;
let state = binding.state.lock().expect("embedded state mutex");
if state.phase != SourceRuntimePhase::Current {
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::OnEvent,
));
}
let index = binding
.runtime
.acquire()
.map_err(|_| refuse(SourceRefusalKind::AdmissionUnavailable, RetryAdvice::Never))?;
let authority = Arc::clone(&binding.authority);
authority
.verify_physical_root_anchor()
.map_err(|_| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::OnEvent))?;
let authority_publication = authority
.current_publication()
.ok_or_else(|| refuse(SourceRefusalKind::SourceUnavailable, RetryAdvice::OnEvent))?;
let source_set = index.published_source_set();
let generation = source_set.current_generation();
let publication_identity = format!(
"embed-publication-{}-{}",
self.identity.raw(),
generation.publication_generation
);
if state.current_publication_identity.as_deref() != Some(&publication_identity)
|| !matches!(generation.freshness.as_ref(), FreshnessStatus::Current)
|| authority.current_publication() != Some(authority_publication)
|| self.closed.load(Ordering::Acquire)
{
return Err(refuse(
SourceRefusalKind::SourceUnavailable,
RetryAdvice::OnEvent,
));
}
let binding_identity = format!("source-{}", self.identity.raw());
let serving_publication_identity = super::embed_query::serving_publication_identity(
&binding_identity,
&publication_identity,
state.source_version,
);
Ok(super::embed_query::EmbeddedQuerySnapshot {
root: binding.root.clone(),
project_state: binding.state_placement.directory().cloned(),
state_dir: binding
.state_placement
.directory()
.map(|directory| directory.as_path().to_path_buf()),
source_set,
generation,
binding_identity,
publication_identity,
serving_publication_identity,
source_version: state.source_version,
lifecycle_epoch: state.lifecycle_epoch,
source_phase: SourceRuntimePhase::Current,
authority_publication,
authority,
state_anchor: binding.state_anchor.clone(),
temporal_cache: Arc::clone(&binding.temporal_cache),
control_directory: binding.control_directory.clone(),
trust_mode: binding.trust_mode,
})
}
#[cfg(feature = "embed")]
pub(super) fn capture_disk_observation_context(
&self,
) -> Option<(
Arc<super::activation::ProjectSourceAuthority>,
Arc<crate::live_index::PublishedGeneration>,
super::public_api::SourceRuntimePhase,
)> {
if self.closed.load(Ordering::Acquire) {
return None;
}
let binding = self.binding.as_ref()?;
let phase = binding.state.lock().expect("embedded state mutex").phase;
let index = binding.runtime.acquire().ok()?;
let generation = index.published_source_set().current_generation();
Some((Arc::clone(&binding.authority), generation, phase))
}
#[cfg(feature = "embed")]
pub(super) fn source_authority(
&self,
) -> Option<Arc<super::activation::ProjectSourceAuthority>> {
Some(Arc::clone(&self.binding.as_ref()?.authority))
}
#[cfg(feature = "embed")]
pub(super) fn stel_store(
&self,
policy: crate::embed::parity::QueryPolicy,
) -> Option<Arc<crate::stel::ledger_store::StelLedgerStore>> {
let binding = self.binding.as_ref()?;
binding
.stel_store
.get(&binding.state_placement, policy, || {
format!("embedded-{}", binding.identity.0)
})
}
#[cfg(feature = "embed")]
pub(super) fn capture_health_context(
&self,
) -> Option<(crate::live_index::SharedIndex, PathBuf, StatePlacement)> {
if self.closed.load(Ordering::Acquire) {
return None;
}
let binding = self.binding.as_ref()?;
let index = binding.runtime.acquire().ok()?;
Some((
Arc::clone(index),
binding.root.clone(),
binding.state_placement.clone(),
))
}
#[cfg(feature = "embed")]
pub(super) fn capture_curation_context(
&self,
normalized: &[u8],
) -> Result<
(
super::embed_query::EmbeddedQuerySnapshot,
crate::live_index::SharedIndex,
StatePlacement,
),
super::public_api::EmbedSourceRefusal,
> {
let snapshot = self.capture_query_snapshot(normalized)?;
let refuse = || {
super::public_api::bound_source_refusal(
super::public_api::SourceRefusalKind::SourceUnavailable,
super::public_api::OperationKind::IndexCensus,
super::public_api::RetryAdvice::OnEvent,
normalized,
)
};
let binding = self.binding.as_ref().ok_or_else(refuse)?;
let index = binding.runtime.acquire().map_err(|_| refuse())?;
if self.closed.load(Ordering::Acquire)
|| !Arc::ptr_eq(&index.published_generation(), &snapshot.generation)
{
return Err(refuse());
}
Ok((snapshot, Arc::clone(index), binding.state_placement.clone()))
}
pub fn identity(&self) -> EmbeddedIdentity {
self.identity
}
pub fn key(&self) -> &ProjectKey {
&self.key
}
pub fn is_open(&self) -> bool {
!self.closed.load(Ordering::Acquire)
&& self.binding.as_ref().is_none_or(|binding| {
!matches!(
binding.runtime_view().phase,
super::public_api::SourceRuntimePhase::Stopped
| super::public_api::SourceRuntimePhase::Stopping
)
})
}
pub fn close(&self) -> Result<CloseReceipt, EmbedRefusal> {
if FINALIZING.with(std::cell::Cell::get) == Some(self.identity) {
return Err(EmbedRefusal::WouldSelfWait);
}
if self.closed.swap(true, Ordering::AcqRel) {
return Err(EmbedRefusal::AlreadyClosed);
}
let (performed, final_owner, _, _) = self.registration.close_one(&self.key, self.identity);
Ok(CloseReceipt {
identity: self.identity,
performed_shutdown: performed,
was_final_owner: final_owner,
})
}
pub fn begin_close(&self) -> SourceCloseReceipt {
let performed = if self.closed.swap(true, Ordering::AcqRel) {
false
} else {
let (performed, _final_owner, terminal_version, _joined_workers) =
self.registration.close_one(&self.key, self.identity);
return SourceCloseReceipt {
identity: self.identity,
performed_shutdown: performed,
terminal_source_version: terminal_version,
_not_unwind_safe: std::marker::PhantomData,
};
};
SourceCloseReceipt {
identity: self.identity,
performed_shutdown: performed,
terminal_source_version: self
.binding
.as_ref()
.map(|binding| binding.runtime_view().source_version)
.unwrap_or(0),
_not_unwind_safe: std::marker::PhantomData,
}
}
#[cfg(feature = "embed")]
pub fn change_version(&self) -> SourceChangeVersion {
SourceChangeVersion {
source_id: self.identity.raw(),
revision: self.binding.as_ref().map_or(0, |binding| {
binding
.state
.lock()
.expect("embedded state mutex")
.change_revision
}),
}
}
#[cfg(feature = "embed")]
pub fn wait_for_change(
&self,
since: SourceChangeVersion,
timeout: Duration,
) -> Result<super::public_api::SourceRuntimeView, super::public_api::EmbedSourceRefusal> {
use super::public_api::{OperationKind, RetryAdvice, SourceRefusalKind};
let Some(binding) = self.binding.as_ref() else {
return Ok(self.runtime_view());
};
if since.source_id != self.identity.raw() {
return Err(super::public_api::bound_source_refusal(
SourceRefusalKind::SourceUnavailable,
OperationKind::IndexCensus,
RetryAdvice::Never,
b"foreign-source-change-version",
));
}
let started = std::time::Instant::now();
let mut state = binding.state.lock().expect("embedded state mutex");
while state.change_revision == since.revision && !self.closed.load(Ordering::Acquire) {
binding.prune_graph_leases(&mut state);
if state.change_revision != since.revision {
break;
}
let remaining = timeout.saturating_sub(started.elapsed());
if remaining.is_zero() {
break;
}
let next_expiry = {
let leases = binding.graph_leases.lock().expect("graph lease mutex");
if leases.len() >= 8 {
leases.values().map(|entry| entry.expires_at).min()
} else {
None
}
};
let wait = next_expiry.map_or(remaining, |expiry| {
remaining.min(expiry.saturating_duration_since(std::time::Instant::now()))
});
let (next, _) = binding
.change_wake
.wait_timeout(state, wait)
.expect("embedded state mutex");
state = next;
}
Ok(super::public_api::SourceRuntimeView {
binding_identity: format!("source-{}", self.identity.raw()),
current_publication_identity: state.current_publication_identity.clone(),
observer_epoch: state.observer_epoch,
phase: state.phase,
source_version: state.source_version,
})
}
pub fn runtime_view(&self) -> super::public_api::SourceRuntimeView {
self.binding.as_ref().map_or_else(
|| super::public_api::SourceRuntimeView {
binding_identity: format!("source-{}", self.identity.raw()),
current_publication_identity: None,
observer_epoch: 0,
phase: if self.closed.load(Ordering::Acquire) {
super::public_api::SourceRuntimePhase::Stopped
} else {
super::public_api::SourceRuntimePhase::Loading
},
source_version: 0,
},
|binding| binding.runtime_view(),
)
}
pub fn search_symbols(
&self,
request: &super::public_api::SymbolSearchRequest,
) -> Result<
super::public_api::EmbedClaim<super::public_api::SymbolSearchResult>,
super::public_api::EmbedSourceRefusal,
> {
let normalized = format!(
"query={:?};path_prefix={:?};limit={}",
request.query, request.path_prefix, request.limit
);
if self.closed.load(Ordering::Acquire) {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchSymbols,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::SearchSymbols,
));
};
let claim = binding.current_claim(
crate::lifecycle_identity::OperationKind::SearchSymbols,
normalized.as_bytes(),
)?;
let index = binding.runtime.acquire().map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchSymbols,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
)
})?;
let live = index.read();
let query = request.query.as_deref().map(str::to_lowercase);
let mut matches = Vec::new();
for (path, file) in live.all_files() {
if !path_matches_prefix(path, request.path_prefix.as_deref()) {
continue;
}
for symbol in &file.symbols {
if query
.as_deref()
.is_some_and(|query| !symbol.name.to_lowercase().contains(query))
{
continue;
}
matches.push((
symbol.sort_order,
super::public_api::SymbolMatch {
name: symbol.name.clone(),
kind: symbol.kind.to_string(),
path: path.clone(),
start_line: symbol.line_range.0.saturating_add(1),
end_line: symbol.line_range.1.saturating_add(1),
},
));
}
}
matches.sort_by(|(left_order, left), (right_order, right)| {
left.path
.cmp(&right.path)
.then_with(|| left.start_line.cmp(&right.start_line))
.then_with(|| left_order.cmp(right_order))
.then_with(|| left.name.cmp(&right.name))
.then_with(|| left.kind.cmp(&right.kind))
});
let limit = request.limit as usize;
let truncated = matches.len() > limit;
let matches = matches
.into_iter()
.take(limit)
.map(|(_, value)| value)
.collect();
Ok(super::public_api::live_claim(
super::public_api::SymbolSearchResult { matches, truncated },
crate::lifecycle_identity::OperationKind::SearchSymbols,
normalized.as_bytes(),
&claim.binding_identity,
&claim.publication_identity,
claim.source_version,
))
}
pub fn search_text(
&self,
request: &super::public_api::TextSearchRequest,
) -> Result<
super::public_api::EmbedClaim<super::public_api::TextSearchResult>,
super::public_api::EmbedSourceRefusal,
> {
let normalized = format!(
"query={:?};path_prefix={:?};limit={};case_sensitive={}",
request.query, request.path_prefix, request.limit, request.case_sensitive
);
if request.query.is_empty() {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::InvalidSelection,
crate::lifecycle_identity::OperationKind::SearchText,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
));
}
if self.closed.load(Ordering::Acquire) {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchText,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::SearchText,
));
};
let claim = binding.current_claim(
crate::lifecycle_identity::OperationKind::SearchText,
normalized.as_bytes(),
)?;
let matcher = regex::RegexBuilder::new(®ex::escape(&request.query))
.case_insensitive(!request.case_sensitive)
.build()
.map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::InvalidSelection,
crate::lifecycle_identity::OperationKind::SearchText,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
)
})?;
let index = binding.runtime.acquire().map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchText,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
)
})?;
let live = index.read();
let limit = request.limit as usize;
let mut matches = Vec::new();
let mut truncated = false;
'files: for (path, file) in live.all_files() {
if !path_matches_prefix(path, request.path_prefix.as_deref()) {
continue;
}
let Ok(content) = std::str::from_utf8(&file.content) else {
continue;
};
let mut line_start = 0usize;
for (line_index, line) in content.split_inclusive('\n').enumerate() {
let searchable = line.strip_suffix('\n').unwrap_or(line);
for found in matcher.find_iter(searchable) {
if matches.len() == limit {
truncated = true;
break 'files;
}
let byte_start = line_start.saturating_add(found.start());
let byte_end = line_start.saturating_add(found.end());
matches.push(super::public_api::TextMatch {
path: path.clone(),
line: u32::try_from(line_index.saturating_add(1)).unwrap_or(u32::MAX),
byte_start: byte_start as u64,
byte_end: byte_end as u64,
preview: bounded_preview(searchable.trim_end_matches('\r')),
});
}
line_start = line_start.saturating_add(line.len());
}
}
Ok(super::public_api::live_claim(
super::public_api::TextSearchResult { matches, truncated },
crate::lifecycle_identity::OperationKind::SearchText,
normalized.as_bytes(),
&claim.binding_identity,
&claim.publication_identity,
claim.source_version,
))
}
pub fn index_census(
&self,
) -> Result<
super::public_api::EmbedClaim<super::public_api::IndexCensus>,
super::public_api::EmbedSourceRefusal,
> {
let normalized = b"index-census";
if self.closed.load(Ordering::Acquire) {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::IndexCensus,
crate::lifecycle_identity::RetryAdvice::Never,
normalized,
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::IndexCensus,
));
};
let claim = binding.current_claim(
crate::lifecycle_identity::OperationKind::IndexCensus,
normalized,
)?;
let index = binding.runtime.acquire().map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::IndexCensus,
crate::lifecycle_identity::RetryAdvice::Never,
normalized,
)
})?;
let generation = index.published_generation();
let parsed: std::collections::BTreeSet<String> = generation
.live
.all_files()
.filter(|(_, file)| {
matches!(
file.parse_status,
crate::live_index::store::ParseStatus::Parsed
| crate::live_index::store::ParseStatus::PartialParse { .. }
)
})
.map(|(path, _)| path.clone())
.collect();
crate::live_index::store::pause_census_after_first_read_for_test(&binding.root);
let files: Vec<super::public_api::CensusFile> = generation
.outline
.files
.iter()
.filter(|file| parsed.contains(&file.relative_path))
.map(|file| {
let (reference_count, parse_status) = generation
.live
.get_file(&file.relative_path)
.map(|indexed| {
(
u32::try_from(indexed.references.len()).unwrap_or(u32::MAX),
match &indexed.parse_status {
crate::live_index::store::ParseStatus::Parsed => {
super::public_api::CensusParseStatus::Parsed
}
crate::live_index::store::ParseStatus::PartialParse { .. } => {
super::public_api::CensusParseStatus::PartialParse
}
crate::live_index::store::ParseStatus::Failed { .. } => {
super::public_api::CensusParseStatus::Failed
}
},
)
})
.unwrap_or((0, super::public_api::CensusParseStatus::Parsed));
super::public_api::CensusFile {
path: file.relative_path.clone(),
language: file.language.name().to_string(),
symbol_count: u32::try_from(file.symbol_count).unwrap_or(u32::MAX),
reference_count,
parse_status,
}
})
.collect();
let total_files = files.len() as u64;
let total_symbols = files.iter().map(|file| u64::from(file.symbol_count)).sum();
Ok(super::public_api::live_claim(
super::public_api::IndexCensus {
total_files,
total_symbols,
files,
snapshot_verify: match &generation.health.snapshot_verify_state {
crate::live_index::store::SnapshotVerifyState::NotNeeded => {
super::public_api::CensusVerifyPhase::NotNeeded
}
crate::live_index::store::SnapshotVerifyState::Pending => {
super::public_api::CensusVerifyPhase::Pending
}
crate::live_index::store::SnapshotVerifyState::Running(_) => {
super::public_api::CensusVerifyPhase::Running
}
crate::live_index::store::SnapshotVerifyState::Completed(_) => {
super::public_api::CensusVerifyPhase::Completed
}
crate::live_index::store::SnapshotVerifyState::Failed(_) => {
super::public_api::CensusVerifyPhase::Failed
}
},
},
crate::lifecycle_identity::OperationKind::IndexCensus,
normalized,
&claim.binding_identity,
&claim.publication_identity,
claim.source_version,
))
}
pub fn index_progress(&self) -> super::public_api::IndexProgress {
self.binding
.as_ref()
.map_or_else(super::public_api::IndexProgress::default, |binding| {
let (files_discovered, files_parsed, symbols_found) = binding.progress.snapshot();
super::public_api::IndexProgress {
files_discovered,
files_parsed,
symbols_found,
}
})
}
pub fn search_knowledge(
&self,
request: &super::public_api::KnowledgeSearchRequest,
) -> Result<
super::public_api::EmbedClaim<super::public_api::KnowledgeSearchResult>,
super::public_api::EmbedSourceRefusal,
> {
let normalized = format!(
"query={:?};path_prefix={:?};limit={}",
request.query, request.path_prefix, request.limit
);
if self.closed.load(Ordering::Acquire) {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchKnowledge,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::SearchKnowledge,
));
};
let parsed = crate::live_index::knowledge_retrieve::KnowledgeRetrieveRequest::parse(
&request.query,
request.path_prefix.as_deref(),
crate::live_index::knowledge_retrieve::KnowledgeRetrieveAuthorityScope::Default,
request.limit as usize,
)
.map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::InvalidSelection,
crate::lifecycle_identity::OperationKind::SearchKnowledge,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
)
})?;
let claim = binding.current_claim(
crate::lifecycle_identity::OperationKind::SearchKnowledge,
normalized.as_bytes(),
)?;
let index = binding.runtime.acquire().map_err(|_| {
super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::SearchKnowledge,
crate::lifecycle_identity::RetryAdvice::Never,
normalized.as_bytes(),
)
})?;
let generation = index.published_generation();
let retrieved = crate::live_index::knowledge_retrieve::retrieve_knowledge(
&[
crate::live_index::knowledge_retrieve::KnowledgeRetrieveLane {
generation: &generation,
label: "current",
},
],
&parsed,
);
let matches = retrieved
.hits
.into_iter()
.map(|hit| super::public_api::KnowledgeMatch {
path: hit.path,
heading_path: hit.heading_path,
preview: hit.preview,
content_hash: hit.content_hash,
provenance_ids: hit.authority.provenance_ids,
relationship_evidence: hit
.relationship_evidence
.iter()
.map(|evidence| evidence.preview_token())
.collect(),
authority: knowledge_lifecycle_label(hit.authority.lifecycle),
coverage: knowledge_coverage_label(&hit.authority.coverage),
})
.collect();
Ok(super::public_api::live_claim(
super::public_api::KnowledgeSearchResult {
matches,
truncated: retrieved.truncated,
withheld_count: retrieved.withheld_count as u64,
withheld_reasons: retrieved
.withheld_reasons
.into_iter()
.map(knowledge_withheld_label)
.collect(),
},
crate::lifecycle_identity::OperationKind::SearchKnowledge,
normalized.as_bytes(),
&claim.binding_identity,
&claim.publication_identity,
claim.source_version,
))
}
#[cfg(feature = "__test-internals")]
pub fn revoke_admission_for_test(&self) {
if let Some(binding) = &self.binding {
binding.runtime.revoke_admission_for_test();
}
}
#[cfg(feature = "__test-internals")]
pub fn cancel_reload_for_test(&self) {
if let Some(binding) = &self.binding {
binding.shutdown_started.store(true, Ordering::Release);
let mut control = binding.control.lock().expect("embedded control mutex");
control.stop = true;
binding.wake.notify_all();
}
}
pub fn request_refresh(
&self,
) -> Result<super::public_api::EmbedRefreshTicket, super::public_api::EmbedSourceRefusal> {
let normalized = b"refresh-current-worktree";
if self.closed.load(Ordering::Acquire) {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::RefreshSource,
crate::lifecycle_identity::RetryAdvice::Never,
normalized,
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::RefreshSource,
));
};
let Some(source_version) = binding.request_refresh(false) else {
return Err(super::public_api::bound_source_refusal(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::OperationKind::RefreshSource,
crate::lifecycle_identity::RetryAdvice::Never,
normalized,
));
};
Ok(super::public_api::refresh_ticket(
normalized,
source_version,
))
}
#[cfg(feature = "embed")]
pub fn request_refresh_with_reset(
&self,
) -> Result<
(
super::public_api::EmbedRefreshTicket,
crate::embed::parity::source_options::SnapshotResetReceipt,
),
super::public_api::EmbedSourceRefusal,
> {
let normalized = b"refresh-current-worktree;reset_snapshot_state=true";
let refuse = |kind, retry| {
super::public_api::bound_source_refusal(
kind,
crate::lifecycle_identity::OperationKind::RefreshSource,
retry,
normalized,
)
};
if self.closed.load(Ordering::Acquire) {
return Err(refuse(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::RetryAdvice::Never,
));
}
let Some(binding) = self.binding.as_ref() else {
return Err(super::public_api::dark_unbound_refusal(
crate::lifecycle_identity::OperationKind::RefreshSource,
));
};
let report = crate::live_index::persist::reset_snapshot_state(
&binding.root,
&binding.state_placement,
)
.map_err(|_| {
refuse(
crate::lifecycle_identity::SourceRefusalKind::AdmissionUnavailable,
crate::lifecycle_identity::RetryAdvice::Operator,
)
})?;
let Some(source_version) = binding.request_refresh(true) else {
return Err(refuse(
crate::lifecycle_identity::SourceRefusalKind::SourceUnavailable,
crate::lifecycle_identity::RetryAdvice::Never,
));
};
Ok((
super::public_api::refresh_ticket(normalized, source_version),
crate::embed::parity::source_options::SnapshotResetReceipt::from_report(&report),
))
}
#[cfg(feature = "embed")]
pub fn open_reset_receipt(
&self,
) -> Option<crate::embed::parity::source_options::SnapshotResetReceipt> {
self.binding.as_ref()?.open_reset.clone()
}
#[cfg(all(test, feature = "server"))]
pub fn self_wait_probe_for_test(&self) -> Result<SourceCloseReport, ReceiptWaitError> {
let receipt = self.begin_close();
self.finalize(|| receipt.wait_for_test())
}
pub fn finalize<R>(&self, finalizer: impl FnOnce() -> R) -> R {
struct Guard(Option<EmbeddedIdentity>);
impl Drop for Guard {
fn drop(&mut self) {
FINALIZING.with(|flag| flag.set(self.0));
}
}
let _guard = Guard(FINALIZING.with(|flag| flag.replace(Some(self.identity))));
finalizer()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReceiptWaitError {
DeadlineElapsed,
WouldSelfWait,
}
impl std::fmt::Display for ReceiptWaitError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DeadlineElapsed => {
write!(f, "the deadline passed before the wait completed")
}
Self::WouldSelfWait => write!(
f,
"waiting on this receipt from inside its own source's finalizer \
would wait on the calling thread itself"
),
}
}
}
impl std::error::Error for ReceiptWaitError {}
#[derive(Debug)]
pub struct SourceCloseReceipt {
identity: EmbeddedIdentity,
performed_shutdown: bool,
terminal_source_version: u64,
_not_unwind_safe: super::public_api::NotUnwindSafe,
}
impl SourceCloseReceipt {
pub fn wait(
&self,
deadline: std::time::Instant,
) -> Result<super::public_api::SourceCloseReport, ReceiptWaitError> {
let _ = deadline;
if FINALIZING.with(std::cell::Cell::get) == Some(self.identity) {
return Err(ReceiptWaitError::WouldSelfWait);
}
Ok(super::public_api::SourceCloseReport {
already_terminal: !self.performed_shutdown,
terminal_source_version: self.terminal_source_version,
})
}
#[cfg(all(test, feature = "server"))]
pub fn wait_for_test(&self) -> Result<SourceCloseReport, ReceiptWaitError> {
if FINALIZING.with(std::cell::Cell::get) == Some(self.identity) {
return Err(ReceiptWaitError::WouldSelfWait);
}
Ok(SourceCloseReport {
finalized: true,
performed_shutdown: self.performed_shutdown,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceCloseReport {
finalized: bool,
performed_shutdown: bool,
}
impl SourceCloseReport {
pub fn finalized(&self) -> bool {
self.finalized
}
pub fn performed_shutdown(&self) -> bool {
self.performed_shutdown
}
}
impl Drop for EmbeddedSourceHandle {
fn drop(&mut self) {
if !self.closed.swap(true, Ordering::AcqRel) {
let _ = self.registration.close_one(&self.key, self.identity);
}
}
}
#[cfg(all(test, feature = "embed"))]
mod refresh_lifecycle_tests {
use super::*;
use crate::embed::parity::{QueryLimits, QueryRefusalKind, QueryRequest};
use crate::embed::{EmbeddedSourceSpec, ProcessIndexRuntime, SourceRuntimePhase};
thread_local! {
pub(super) static BEFORE_FRESHEN_GENERATION: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
const { std::cell::RefCell::new(None) };
pub(super) static BEFORE_OBSERVED_STATE: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
const { std::cell::RefCell::new(None) };
}
fn wait_current(handle: &EmbeddedSourceHandle, after: u64) {
let started = std::time::Instant::now();
loop {
let since = handle.change_version();
let view = handle.runtime_view();
if view.phase == SourceRuntimePhase::Current && view.source_version > after {
return;
}
let remaining = Duration::from_secs(20).saturating_sub(started.elapsed());
assert!(
!remaining.is_zero(),
"source did not become Current: {view:?}"
);
handle.wait_for_change(since, remaining).unwrap();
}
}
fn open_current() -> (
tempfile::TempDir,
ProcessIndexRuntime,
Arc<EmbeddedSourceHandle>,
) {
let root = tempfile::tempdir().unwrap();
std::fs::write(root.path().join("lib.rs"), "pub fn loaded() {}\n").unwrap();
let runtime = ProcessIndexRuntime::acquire().unwrap();
let handle = Arc::new(
runtime
.open_embedded_source(EmbeddedSourceSpec::current_worktree(
root.path().to_path_buf(),
))
.unwrap(),
);
wait_current(&handle, 0);
(root, runtime, handle)
}
#[test]
fn observed_publication_includes_freshening_admitted_before_refresh() {
let (root, _runtime, handle) = open_current();
let binding = Arc::clone(handle.binding.as_ref().unwrap());
let control = binding.control.lock().unwrap();
let (admitted_tx, admitted_rx) = std::sync::mpsc::sync_channel(1);
let (resume_tx, resume_rx) = std::sync::mpsc::sync_channel(1);
let (finished_tx, finished_rx) = std::sync::mpsc::sync_channel(1);
let freshening = Arc::clone(&binding);
let freshener = std::thread::spawn(move || {
BEFORE_FRESHEN_GENERATION.with(|hook| {
*hook.borrow_mut() = Some(Box::new(move || {
admitted_tx.send(()).unwrap();
resume_rx.recv_timeout(Duration::from_secs(5)).unwrap();
}));
});
let result = freshening.freshen_exact_path("lib.rs");
finished_tx.send(result).unwrap();
});
admitted_rx.recv_timeout(Duration::from_secs(5)).unwrap();
binding.set_phase(SourceRuntimePhase::Refreshing);
let shared = binding.runtime.data_plane();
let previous_project_generation = shared.current_project_generation();
shared
.refresh_bound_tree_cancellable(
&binding.root,
binding.state_placement.directory().cloned(),
crate::discovery::SourceExclusions::for_state_placement(
&binding.root,
&binding.state_placement,
),
&binding.shutdown_started,
)
.unwrap();
assert!(shared.current_project_generation() > previous_project_generation);
let before = shared.published_generation().publication_generation;
let mtime = shared.read().get_file("lib.rs").unwrap().mtime_secs;
let final_bytes = b"pub fn freshened_after_reload() {}\n";
std::fs::write(root.path().join("lib.rs"), final_bytes).unwrap();
filetime::set_file_mtime(
root.path().join("lib.rs"),
filetime::FileTime::from_unix_time(mtime as i64 + 2, 0),
)
.unwrap();
let publishing = Arc::clone(&binding);
BEFORE_OBSERVED_STATE.with(|hook| {
*hook.borrow_mut() = Some(Box::new(move || {
resume_tx.send(()).unwrap();
finished_rx
.recv_timeout(Duration::from_secs(5))
.unwrap()
.unwrap();
assert_eq!(
publishing.runtime_view().phase,
SourceRuntimePhase::Refreshing
);
assert!(
publishing
.runtime
.data_plane()
.published_generation()
.publication_generation
> before
);
}));
});
let fingerprint = binding.publish_observed_current().unwrap();
let published = shared.published_generation();
let snapshot = handle.capture_query_snapshot(b"freshened-during-refresh");
let unchanged_fingerprint = binding.observe_fingerprint();
let current_identity = binding
.state
.lock()
.unwrap()
.current_publication_identity
.clone();
let assertions = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
assert_eq!(Some(fingerprint), unchanged_fingerprint);
assert_eq!(handle.runtime_view().phase, SourceRuntimePhase::Current);
assert_eq!(
current_identity,
Some(format!(
"embed-publication-{}-{}",
binding.identity.raw(),
published.publication_generation
)),
"Current lifecycle identity must match the freshened publication"
);
let snapshot = snapshot.expect("Current must capture the latest freshened publication");
assert_eq!(
snapshot.generation.publication_generation,
published.publication_generation
);
assert_eq!(
snapshot
.generation
.live
.get_file("lib.rs")
.unwrap()
.content
.as_slice(),
final_bytes
);
assert!(
handle
.query(
&QueryRequest::File {
path: "lib.rs".into(),
start_line: None,
end_line: None,
},
QueryLimits::default(),
)
.is_ok()
);
}));
drop(control);
freshener.join().unwrap();
if let Err(panic) = assertions {
std::panic::resume_unwind(panic);
}
}
#[test]
fn observed_non_current_freshness_blocks_without_relocking_state() {
const CHILD_ENV: &str = "SYMFORGE_NON_CURRENT_PUBLICATION_CHILD";
if std::env::var_os(CHILD_ENV).is_some() {
let (_root, _runtime, handle) = open_current();
let binding = Arc::clone(handle.binding.as_ref().unwrap());
let control = binding.control.lock().unwrap();
binding
.runtime
.data_plane()
.set_freshness_status(FreshnessStatus::Verifying);
assert!(binding.publish_observed_current().is_some());
assert_eq!(handle.runtime_view().phase, SourceRuntimePhase::Blocked);
drop(control);
return;
}
let mut child = crate::process_util::hidden_command(std::env::current_exe().unwrap())
.args([
"--exact",
"internals::index_lifecycle::embedded::refresh_lifecycle_tests::observed_non_current_freshness_blocks_without_relocking_state",
"--nocapture",
])
.env(CHILD_ENV, "1")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(30);
loop {
match child.try_wait() {
Ok(Some(_)) => break,
Ok(None) if std::time::Instant::now() < deadline => {
std::thread::sleep(Duration::from_millis(25));
}
outcome => {
let _ = child.kill();
let reaped = child.wait();
panic!(
"non-Current publication child timed out or could not be observed: {outcome:?}; reaped: {reaped:?}"
);
}
}
}
let output = child.wait_with_output().unwrap();
assert!(
output.status.success(),
"non-Current publication child failed: {} {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(
String::from_utf8_lossy(&output.stdout).contains("running 1 test"),
"child must execute the exact non-Current publication fixture"
);
}
#[test]
#[cfg(feature = "__test-internals")]
fn lease_begin_preserves_transient_and_invalid_root_retry_advice() {
use super::super::public_api::{RetryAdvice, SourceRefusalKind};
let parent = tempfile::tempdir().unwrap();
let root = parent.path().join("source");
std::fs::create_dir(&root).unwrap();
std::fs::write(root.join("lib.rs"), "pub fn loaded() {}\n").unwrap();
let reload_hold = crate::live_index::store::hold_reload_after_discovery_for_test(&root);
let runtime = ProcessIndexRuntime::acquire().unwrap();
let handle = runtime
.open_embedded_source(EmbeddedSourceSpec::current_worktree(root.clone()))
.unwrap();
let gate = reload_hold;
assert!(gate.wait_until_blocked(Duration::from_secs(5)));
assert_eq!(handle.runtime_view().phase, SourceRuntimePhase::Loading);
let loading = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap_err();
assert_eq!(loading.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(loading.retry(), RetryAdvice::OnEvent);
let binding = handle.binding.as_ref().unwrap();
binding.set_phase(SourceRuntimePhase::Refreshing);
let refreshing = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap_err();
assert_eq!(refreshing.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(refreshing.retry(), RetryAdvice::OnEvent);
std::fs::rename(&root, parent.path().join("original")).unwrap();
std::fs::create_dir(&root).unwrap();
let invalid = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap_err();
assert_eq!(invalid.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(invalid.retry(), RetryAdvice::Never);
let captured = handle
.capture_query_snapshot_allowing_refresh(b"invalid-root")
.err()
.expect("invalid root must refuse capture");
assert_eq!(captured.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(captured.retry(), RetryAdvice::Never);
let completed = handle
.validate_refresh_snapshot(0, b"invalid-root")
.unwrap_err();
assert_eq!(completed.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(completed.retry(), RetryAdvice::Never);
drop(gate);
}
#[test]
#[cfg(feature = "__test-internals")]
fn blocked_recovery_invalidates_leases_and_retained_completion() {
use super::super::public_api::{RetryAdvice, SourceRefusalKind};
let (root, _runtime, handle) = open_current();
let initial = handle.runtime_view();
let lease = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap();
assert_eq!(format!("{lease:?}"), "GraphPublicationLease { .. }");
let snapshot = handle
.capture_query_snapshot_allowing_refresh(b"before-blocked")
.unwrap();
let gate = crate::live_index::store::hold_reload_after_discovery_for_test(root.path());
handle.request_refresh().unwrap();
assert!(gate.wait_until_blocked(Duration::from_secs(5)));
assert!(handle.capture_graph_lease_snapshot(&lease).is_ok());
let binding = handle.binding.as_ref().unwrap();
binding.set_blocked();
binding.set_phase(SourceRuntimePhase::Refreshing);
assert_eq!(
handle
.capture_graph_lease_snapshot(&lease)
.err()
.expect("invalidated lease must refuse"),
QueryRefusalKind::SourceUnavailable,
);
let completion = handle
.validate_refresh_snapshot(snapshot.lifecycle_epoch, b"after-blocked")
.unwrap_err();
assert_eq!(completion.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(completion.retry(), RetryAdvice::OnEvent);
let unavailable = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap_err();
assert_eq!(unavailable.kind(), SourceRefusalKind::SourceUnavailable);
assert_eq!(unavailable.retry(), RetryAdvice::OnEvent);
drop(gate);
wait_current(&handle, initial.source_version);
assert_eq!(
handle
.capture_graph_lease_snapshot(&lease)
.err()
.expect("invalidated lease must refuse"),
QueryRefusalKind::SourceUnavailable,
);
let replacement = handle
.begin_graph_publication_lease(Duration::from_secs(20))
.unwrap();
assert!(handle.capture_graph_lease_snapshot(&replacement).is_ok());
}
#[test]
fn unsupported_freshening_request_refuses_before_taking_source_state() {
let (root, _runtime, handle) = open_current();
let request = QueryRequest::Syntax {
path: "lib.rs".into(),
};
assert_eq!(request.freshen_path(), Some("lib.rs"));
let binding = handle.binding.as_ref().unwrap();
let state = binding.state.lock().unwrap();
std::fs::write(root.path().join("lib.rs"), "pub fn changed() {}\n").unwrap();
let (sent, received) = std::sync::mpsc::sync_channel(1);
let querying = Arc::clone(&handle);
let worker = std::thread::spawn(move || {
sent.send(querying.query_allowing_refresh(&request, QueryLimits::default()))
.unwrap();
});
let result = received.recv_timeout(Duration::from_secs(2));
drop(state);
worker.join().unwrap();
let refusal = result
.expect("unsupported request entered the source/freshening path")
.unwrap_err();
assert_eq!(refusal.kind(), QueryRefusalKind::UnsupportedOption);
assert_eq!(
refusal.retry(),
super::super::public_api::RetryAdvice::Never
);
}
}