//! AT-3.17-02: completion identifiers never reach metric labels. //! //! The MCP request metric is recorded with a closed `McpMethod` label and the completion service emits //! only fixed labels, counts and the served revision. This test proves it against a real recorder: the //! rendered series of a snapshot taken after completion traffic carries no Prompt name, URI template, //! argument name, suggestion value or Agent slug, and every `method` label value stays inside the //! fixed enum. use std::{collections::BTreeSet, time::Duration}; use metrics_util::debugging::DebuggingRecorder; use serde_json::{Value, json}; use super::fixtures::*; /// The closed `method` label set of `crank_mcp_requests_total` (crank-metrics `McpMethod`). const METHOD_LABELS: [&str; 9] = [ "initialize", "initialized", "ping", "tools_list", "tools_call", "notification", "unsupported", "response", "invalid", ]; fn request(reference: Value, argument: &str, value: &str) -> Value { json!({"ref": reference, "argument": {"name": argument, "value": value}}) } #[tokio::test] async fn completion_identifiers_never_reach_metric_labels() { crank_observability::bind_metric_service(crank_observability::MetricService::McpServer) .expect("explicit MCP service binding before the recorder"); let recorder = DebuggingRecorder::new(); let snapshotter = recorder.snapshotter(); recorder .install() .expect("isolated completion test recorder"); let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { for params in [ // A matched published value, a partially matched token and an unavailable reference: the // three shapes that a naive implementation would be tempted to label with. request( json!({"type": "ref/prompt", "name": "greeting"}), "tone", "warm", ), request( json!({"type": "ref/resource", "uri": BOUND_TEMPLATE_URI}), "region", "apac", ), request( json!({"type": "ref/prompt", "name": "no-such-prompt"}), "tone", "no-such-token", ), ] { let _ = endpoint.on_revision(revision, params).await; } } tokio::time::sleep(Duration::from_millis(25)).await; let snapshot = snapshotter.snapshot().into_vec(); assert!( !snapshot.is_empty(), "the completion traffic must be observable at all, otherwise this check proves nothing" ); let rendered = format!("{snapshot:?}"); for forbidden in [ "greeting", "prm_completion_greeting", "completion-agent", "completion-orders", "no-such-prompt", "no-such-token", "warm", "apac", "tone", "region", ] { assert!( !rendered.contains(forbidden), "a completion identifier leaked into metric labels: {forbidden:?}" ); } let mut observed_methods = BTreeSet::new(); for (key, _, _, _) in &snapshot { if key.key().name() != "crank_mcp_requests_total" { continue; } for label in key.key().labels() { if label.key() == "method" { observed_methods.insert(label.value().to_owned()); } } } assert!( !observed_methods.is_empty(), "the completion requests must produce `crank_mcp_requests_total` series" ); for method in &observed_methods { assert!( METHOD_LABELS.contains(&method.as_str()), "the `method` label must stay inside the fixed taxonomy, got {method:?} \ from {observed_methods:?}" ); } }