Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
156 lines
6.1 KiB
Rust
156 lines
6.1 KiB
Rust
//! Private, request-scoped semantic observer of the BUILD-internal harness (Story 3.1 section 7.2).
|
|
//!
|
|
//! The endpoint integration tests of `apps/mcp-server` cannot see `#[cfg(test)]` markers of this
|
|
//! crate, so the observing behavioural test lives inside the crate, with the same in-crate access as
|
|
//! `protocol::registry::tests::`. This module is `#[cfg(test)]`-only: it is absent from every
|
|
//! production build and exposes no public API.
|
|
//!
|
|
//! The observer is **request-scoped**: the counters live in a `tokio::task_local!` slot that the
|
|
//! test installs around one request, so two concurrently served requests never share a counter and
|
|
//! no global racing state exists. A marker without an installed scope is a no-op, which is what
|
|
//! happens in every other test of this crate.
|
|
//!
|
|
//! The markers are honest names, not one dispatch total (section 7.2): `ADMISSION_ENTER` is the
|
|
//! external request entry, `ERA_MODERN_ENTER`/`ERA_LEGACY_ENTER` mean the request actually entered
|
|
//! that era-adapter, and `TOOLS_SERVICE_ENTER` means the request entered the owning Tools
|
|
//! service/handler. They prove "no reroute" (a pre-admission reject yields zero era and zero service
|
|
//! entries; an in-adapter reject yields exactly its own era and no service entry) and they never
|
|
//! claim "no residual state" on their own.
|
|
//!
|
|
//! Only the markers, the request scope and the counters live here. The behavioural harness that
|
|
//! drives real observed requests through the real app is the `tests` child module below, whose file
|
|
//! sits at `crates/crank-community-mcp/src/dispatch_observer_harness.rs`: `scripts/
|
|
//! check-execution-boundaries.py` reserves the whole `crates/crank-community-mcp/src/app/` prefix
|
|
//! for service modules without an HTTP client or a persistence dependency, while the harness needs
|
|
//! both. The module *path* stays `app::dispatch_observer::tests`, so the selector
|
|
//! `cargo test -p crank-community-mcp --lib app::dispatch_observer::` still selects the whole
|
|
//! section 7.2 delta table.
|
|
|
|
use std::{
|
|
future::Future,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicUsize, Ordering},
|
|
},
|
|
};
|
|
|
|
use tokio::task_local;
|
|
|
|
task_local! {
|
|
static OBSERVER: Arc<CounterSet>;
|
|
}
|
|
|
|
task_local! {
|
|
static REGISTRATION: Arc<crate::protocol::CapabilityRegistry>;
|
|
}
|
|
|
|
/// The per-request counters of one observed request.
|
|
#[derive(Debug, Default)]
|
|
pub(crate) struct CounterSet {
|
|
admission: AtomicUsize,
|
|
era_modern: AtomicUsize,
|
|
era_legacy: AtomicUsize,
|
|
tools_service: AtomicUsize,
|
|
}
|
|
|
|
impl CounterSet {
|
|
/// Read the counters of one observed request as one comparable snapshot.
|
|
pub(crate) fn snapshot(&self) -> Snapshot {
|
|
Snapshot {
|
|
admission: self.admission.load(Ordering::SeqCst),
|
|
era_modern: self.era_modern.load(Ordering::SeqCst),
|
|
era_legacy: self.era_legacy.load(Ordering::SeqCst),
|
|
tools_service: self.tools_service.load(Ordering::SeqCst),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One comparable set of marker deltas of a single observed request.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) struct Snapshot {
|
|
pub(crate) admission: usize,
|
|
pub(crate) era_modern: usize,
|
|
pub(crate) era_legacy: usize,
|
|
pub(crate) tools_service: usize,
|
|
}
|
|
|
|
impl Snapshot {
|
|
/// The expected deltas of one case (section 7.2 table) as one comparable value.
|
|
pub(crate) const fn new(
|
|
admission: usize,
|
|
era_modern: usize,
|
|
era_legacy: usize,
|
|
tools_service: usize,
|
|
) -> Self {
|
|
Self {
|
|
admission,
|
|
era_modern,
|
|
era_legacy,
|
|
tools_service,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Run one observed request: every marker increment inside `future` is recorded on `counters`.
|
|
pub(crate) async fn observe<F: Future>(counters: Arc<CounterSet>, future: F) -> F::Output {
|
|
OBSERVER.scope(counters, future).await
|
|
}
|
|
|
|
/// Run one observed request with an explicit registration snapshot (F5 add/remove proof).
|
|
///
|
|
/// The snapshot is request-scoped like the counters: a request served inside `future` asks *this*
|
|
/// registration for its dispatch decision and its advertisement, so one test can prove that adding or
|
|
/// removing a private inert descriptor changes what the real router does, without touching the
|
|
/// production table and without a process-global override that other tests could observe.
|
|
pub(crate) async fn observe_with_registration<F: Future>(
|
|
registration: Arc<crate::protocol::CapabilityRegistry>,
|
|
future: F,
|
|
) -> F::Output {
|
|
REGISTRATION.scope(registration, future).await
|
|
}
|
|
|
|
/// The registration installed for the current request, if the observing test installed one.
|
|
pub(crate) fn installed_registration() -> Option<Arc<crate::protocol::CapabilityRegistry>> {
|
|
REGISTRATION.try_with(Arc::clone).ok()
|
|
}
|
|
|
|
/// `ADMISSION_ENTER`: the external request entry, not a dispatch.
|
|
pub(crate) fn admission_enter() {
|
|
increment(|counters| &counters.admission);
|
|
}
|
|
|
|
/// `ERA_MODERN_ENTER`: the request was served by the modern era-adapter.
|
|
pub(crate) fn era_modern_enter() {
|
|
increment(|counters| &counters.era_modern);
|
|
}
|
|
|
|
/// `ERA_LEGACY_ENTER`: the request was served by the legacy era-adapter.
|
|
pub(crate) fn era_legacy_enter() {
|
|
increment(|counters| &counters.era_legacy);
|
|
}
|
|
|
|
/// `TOOLS_SERVICE_ENTER`: the request entered the owning Tools service/handler.
|
|
pub(crate) fn tools_service_enter() {
|
|
increment(|counters| &counters.tools_service);
|
|
}
|
|
|
|
fn increment(counter: impl Fn(&CounterSet) -> &AtomicUsize) {
|
|
let _ = OBSERVER.try_with(|counters| {
|
|
counter(counters).fetch_add(1, Ordering::SeqCst);
|
|
});
|
|
}
|
|
|
|
/// Fixtures and helpers of the section 7.2 test seam (its file lives outside `src/app/` for the same
|
|
/// execution-boundary reason as the tests below).
|
|
#[cfg(test)]
|
|
#[path = "../dispatch_observer_support.rs"]
|
|
mod support;
|
|
|
|
/// The behavioural section 7.2 harness: real app, real PostgreSQL, one counted loopback upstream and
|
|
/// one request-scoped observer per request. Its file lives outside `src/app/` only because the
|
|
/// repository's execution-boundary check owns that prefix (see the module documentation above); the
|
|
/// module path stays `app::dispatch_observer::tests`.
|
|
#[cfg(test)]
|
|
#[path = "../dispatch_observer_harness.rs"]
|
|
mod tests;
|