From 0ae6b911ac73eee7cbb9fb83d9a4785f2263e77e Mon Sep 17 00:00:00 2001 From: damocles Date: Wed, 18 Nov 2026 20:18:15 +0100 Subject: [PATCH] hive-bash-mcp: test the command counter against an in-memory exporter --- hive-bash-mcp/Cargo.toml | 1 + hive-bash-mcp/src/stats.rs | 84 +++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/hive-bash-mcp/Cargo.toml b/hive-bash-mcp/Cargo.toml index 22b777755..dec2181d5 100644 --- a/hive-bash-mcp/Cargo.toml +++ b/hive-bash-mcp/Cargo.toml @@ -35,6 +35,7 @@ tracing.workspace = true tracing-subscriber.workspace = true [dev-dependencies] +opentelemetry_sdk = { workspace = true, features = ["testing"] } tempfile = "3" # `hive-bash-daemon` — long-running per-agent bash task runner. Spawns diff --git a/hive-bash-mcp/src/stats.rs b/hive-bash-mcp/src/stats.rs index 371674168..5fbbeb006 100644 --- a/hive-bash-mcp/src/stats.rs +++ b/hive-bash-mcp/src/stats.rs @@ -55,16 +55,20 @@ fn build() -> Option { .build(); SdkMeterProvider::builder().with_reader(reader).build() })?; - let commands = provider - .meter("hyperhive.bash") - .u64_counter("hyperhive.agent.bash.commands") - .build(); + let commands = commands_counter(&provider); Some(Exporter { _provider: provider, commands, }) } +fn commands_counter(provider: &SdkMeterProvider) -> Counter { + provider + .meter("hyperhive.bash") + .u64_counter("hyperhive.agent.bash.commands") + .build() +} + /// Builtins whose presence as a segment head means the *real* command /// is in a later segment (e.g. the `cargo` in `cd repo && cargo build`), /// so we skip the whole segment. @@ -152,19 +156,77 @@ pub fn command_head(cmd: &str) -> Option { /// Count one bash task's normalised command head. No-op when OTEL isn't /// configured or the command has no identifiable head. pub fn record_command(cmd: &str) { - let Some(head) = command_head(cmd) else { - return; - }; - let Some(exporter) = EXPORTER.get_or_init(build).as_ref() else { - return; - }; - exporter.commands.add(1, &[KeyValue::new("command", head)]); + if let Some(exporter) = EXPORTER.get_or_init(build).as_ref() { + count_command(&exporter.commands, cmd); + } +} + +/// Add one to `counter`, keyed by `cmd`'s normalised head; nothing when it +/// has none. +fn count_command(counter: &Counter, cmd: &str) { + if let Some(head) = command_head(cmd) { + counter.add(1, &[KeyValue::new("command", head)]); + } } #[cfg(test)] mod tests { + use std::collections::BTreeMap; + + use opentelemetry_sdk::metrics::InMemoryMetricExporter; + use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData, ScopeMetrics}; + use super::*; + /// What the counter exported for each `command`, after a flush. + fn counted(commands: &[&str]) -> BTreeMap { + let exporter = InMemoryMetricExporter::default(); + let provider = SdkMeterProvider::builder() + .with_reader(PeriodicReader::builder(exporter.clone()).build()) + .build(); + let counter = commands_counter(&provider); + for cmd in commands { + count_command(&counter, cmd); + } + provider.force_flush().expect("flush"); + let mut counts = BTreeMap::new(); + for resource in exporter.get_finished_metrics().expect("metrics") { + for metric in resource.scope_metrics().flat_map(ScopeMetrics::metrics) { + assert_eq!(metric.name(), "hyperhive.agent.bash.commands"); + let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data() else { + panic!("a counter exports a u64 sum"); + }; + for point in sum.data_points() { + let command = point + .attributes() + .find(|kv| kv.key.as_str() == "command") + .map(|kv| kv.value.to_string()) + .expect("every count carries its command"); + counts.insert(command, point.value()); + } + } + } + counts + } + + #[test] + fn each_task_counts_once_under_its_command_head() { + let counts = counted(&[ + "cd /repo && cargo build", + "cargo test -p x", + "sudo env A=1 hive-forge view 1", + ]); + assert_eq!( + counts, + BTreeMap::from([("cargo".to_owned(), 2), ("hive-forge".to_owned(), 1)]) + ); + } + + #[test] + fn a_command_without_a_head_counts_nothing() { + assert!(counted(&["cd /repo", " "]).is_empty()); + } + #[test] fn skips_leading_cd_to_the_real_tool() { assert_eq!(