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.
207 lines
7.8 KiB
Rust
207 lines
7.8 KiB
Rust
//! Story 3.6 · AT-3.6-08 · фаза B — family gating и gauges по binding.
|
|
//!
|
|
//! `TD-B8` MCP-only facade вызовы в admin-процессе — no-op без handle/exemplar.
|
|
//! `TD-B9` `initialize_gauges` требует совпадающий binding, иначе no-op.
|
|
//! `TD-B10` guard чужого family при drop не создаёт series.
|
|
//!
|
|
//! Каждый кейс исполняется в fresh child (binding — process-global `OnceLock`).
|
|
|
|
use std::collections::BTreeSet;
|
|
use std::time::Duration;
|
|
|
|
use crank_metrics::{
|
|
DbPoolState, InFlightGuard, InvocationSource, McpMethod, McpOutcome, McpResponseMode,
|
|
MetricService, ToolErrorKind, ToolOutcome, UpstreamOperationKind, UpstreamOutcome,
|
|
exemplar_snapshot, initialize_gauges, record_cache_outcome, record_idempotency_outcome,
|
|
record_invocation_history_lost, record_mcp_request, record_mcp_request_with_exemplar,
|
|
record_tool_invocation, record_upstream_request, set_catalog, set_mcp_active_sessions,
|
|
};
|
|
use metrics_util::debugging::DebuggingRecorder;
|
|
|
|
const BINDING_ROLE_ENV: &str = "CRANK_METRICS_TEST_BINDING_ROLE";
|
|
|
|
fn route_to_child(role: &str, test: &str) -> bool {
|
|
match std::env::var(BINDING_ROLE_ENV).ok().as_deref() {
|
|
Some(actual) if actual == role => false,
|
|
Some(actual) => panic!("child for {test} runs with role {actual}, expected {role}"),
|
|
None => {
|
|
let output =
|
|
std::process::Command::new(std::env::current_exe().expect("current test binary"))
|
|
.args([test, "--exact", "--nocapture"])
|
|
.env(BINDING_ROLE_ENV, role)
|
|
.output()
|
|
.expect("re-executed binding child");
|
|
assert!(
|
|
output.status.success(),
|
|
"binding child {role}/{test} failed:\nstdout:\n{}\nstderr:\n{}",
|
|
String::from_utf8_lossy(&output.stdout),
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn bind_admin() {
|
|
crank_metrics::bind_metric_service(MetricService::AdminApi).expect("admin bind");
|
|
}
|
|
|
|
fn series_names(
|
|
samples: &[(
|
|
metrics_util::CompositeKey,
|
|
Option<metrics::Unit>,
|
|
Option<metrics::SharedString>,
|
|
metrics_util::debugging::DebugValue,
|
|
)],
|
|
) -> BTreeSet<String> {
|
|
samples
|
|
.iter()
|
|
.map(|(key, _, _, _)| key.key().name().to_owned())
|
|
.collect()
|
|
}
|
|
|
|
/// TD-B8 — MCP-only families в admin-процессе не создают ни handles, ни exemplars.
|
|
#[test]
|
|
fn mcp_only_calls_are_noop_in_admin_process() {
|
|
if route_to_child("admin_api", "mcp_only_calls_are_noop_in_admin_process") {
|
|
return;
|
|
}
|
|
bind_admin();
|
|
let recorder = DebuggingRecorder::new();
|
|
let snapshotter = recorder.snapshotter();
|
|
metrics::with_local_recorder(&recorder, || {
|
|
record_mcp_request(
|
|
McpMethod::ToolsCall,
|
|
McpResponseMode::Json,
|
|
McpOutcome::Success,
|
|
Duration::from_millis(1),
|
|
);
|
|
record_mcp_request_with_exemplar(
|
|
McpMethod::ToolsCall,
|
|
McpResponseMode::Json,
|
|
McpOutcome::Success,
|
|
Duration::from_millis(1),
|
|
crank_metrics::ExemplarTraceId::parse("0123456789abcdef0123456789abcdef"),
|
|
);
|
|
set_mcp_active_sessions(3);
|
|
crank_metrics::set_mcp_session_metrics_fresh(true);
|
|
set_catalog(7, 70, 1);
|
|
});
|
|
let samples = snapshotter.snapshot().into_vec();
|
|
assert!(
|
|
samples.is_empty(),
|
|
"MCP-only families обязаны быть no-op в admin: {:?}",
|
|
series_names(&samples)
|
|
);
|
|
assert!(
|
|
exemplar_snapshot().is_empty(),
|
|
"MCP-only exemplar не может создавать entry в admin-процессе"
|
|
);
|
|
|
|
// Общие families при этом продолжают работать.
|
|
let shared = DebuggingRecorder::new();
|
|
let shared_snapshotter = shared.snapshotter();
|
|
metrics::with_local_recorder(&shared, || {
|
|
record_tool_invocation(
|
|
InvocationSource::AgentToolCall,
|
|
ToolOutcome::Success,
|
|
ToolErrorKind::None,
|
|
Duration::from_millis(1),
|
|
);
|
|
record_upstream_request(
|
|
UpstreamOperationKind::Rest,
|
|
UpstreamOutcome::Success,
|
|
Duration::from_millis(1),
|
|
);
|
|
record_cache_outcome(crank_metrics::CacheOutcome::Hit);
|
|
record_idempotency_outcome(crank_metrics::IdempotencyOutcome::Replay);
|
|
record_invocation_history_lost();
|
|
});
|
|
let names = series_names(&shared_snapshotter.snapshot().into_vec());
|
|
assert!(
|
|
names.contains("crank_tool_invocations_total")
|
|
&& names.contains("crank_upstream_requests_total")
|
|
&& names.contains("crank_runtime_cache_total"),
|
|
"shared families обязаны работать в admin: {names:?}"
|
|
);
|
|
}
|
|
|
|
/// TD-B9 — `initialize_gauges` требует совпадающий binding.
|
|
#[test]
|
|
fn initialize_gauges_requires_matching_binding() {
|
|
if route_to_child("admin_api", "initialize_gauges_requires_matching_binding") {
|
|
return;
|
|
}
|
|
bind_admin();
|
|
|
|
let foreign = DebuggingRecorder::new();
|
|
let foreign_snapshotter = foreign.snapshotter();
|
|
metrics::with_local_recorder(&foreign, || {
|
|
initialize_gauges(MetricService::McpServer);
|
|
});
|
|
assert!(
|
|
foreign_snapshotter.snapshot().into_vec().is_empty(),
|
|
"initialize_gauges чужого сервиса обязан быть no-op"
|
|
);
|
|
|
|
let own = DebuggingRecorder::new();
|
|
let own_snapshotter = own.snapshotter();
|
|
metrics::with_local_recorder(&own, || {
|
|
initialize_gauges(MetricService::AdminApi);
|
|
});
|
|
let names = series_names(&own_snapshotter.snapshot().into_vec());
|
|
assert!(names.contains("crank_http_inflight"));
|
|
assert!(names.contains("crank_runtime_inflight"));
|
|
assert!(names.contains("crank_db_pool_connections"));
|
|
assert!(
|
|
names.iter().all(|name| !name.starts_with("crank_mcp_")),
|
|
"admin gauges не могут включать MCP-only series: {names:?}"
|
|
);
|
|
assert!(
|
|
names.iter().all(|name| !name.starts_with("crank_catalog_")),
|
|
"admin gauges не могут включать catalog series: {names:?}"
|
|
);
|
|
}
|
|
|
|
/// TD-B10 — guard чужого family при drop не создаёт series.
|
|
#[test]
|
|
fn foreign_family_guard_drop_creates_no_series() {
|
|
if route_to_child("admin_api", "foreign_family_guard_drop_creates_no_series") {
|
|
return;
|
|
}
|
|
bind_admin();
|
|
let recorder = DebuggingRecorder::new();
|
|
let snapshotter = recorder.snapshotter();
|
|
metrics::with_local_recorder(&recorder, || {
|
|
let stream = InFlightGuard::mcp_stream();
|
|
drop(stream);
|
|
let tool = crank_metrics::ToolInvocationMetrics::start(InvocationSource::AgentToolCall);
|
|
drop(tool);
|
|
let upstream = crank_metrics::UpstreamRequestMetrics::start(UpstreamOperationKind::Rest);
|
|
drop(upstream);
|
|
});
|
|
let names = series_names(&snapshotter.snapshot().into_vec());
|
|
assert!(
|
|
names.iter().all(|name| !name.starts_with("crank_mcp_")),
|
|
"guard чужого family не может создавать series: {names:?}"
|
|
);
|
|
assert!(
|
|
!names.contains("crank_mcp_active_streams"),
|
|
"foreign stream guard обязан быть no-op: {names:?}"
|
|
);
|
|
|
|
// Собственный family работает: guard admin http и shared families.
|
|
let own = DebuggingRecorder::new();
|
|
let own_snapshotter = own.snapshotter();
|
|
metrics::with_local_recorder(&own, || {
|
|
let http = InFlightGuard::http();
|
|
drop(http);
|
|
});
|
|
let own_names = series_names(&own_snapshotter.snapshot().into_vec());
|
|
assert!(
|
|
own_names.contains("crank_http_inflight"),
|
|
"admin http guard обязан регистрировать series: {own_names:?}"
|
|
);
|
|
let _ = DbPoolState::Idle;
|
|
}
|