Files
crank/crates/crank-community-mcp/src/app/dispatch_observer.rs
T
bsodfather 54ce770682
CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.

Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
  Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
  3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
  revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
  version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
  dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
  resources/list admission fix for bound template publications.

Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
  `just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
  a real MCP Inspector 2.12.0 client smoke.

Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
  runs only on main), story events run the changed packages, the full workspace suite
  stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
  release entry of the tag instead of dying with the runner workspace.

Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.

Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
2026-10-25 09:08:22 +00:00

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;