hive-bash-mcp: count bash commands over OTEL in the daemon, drop the sqlite hand-off
This commit is contained in:
parent
9ec67d7a18
commit
076df804e4
2 changed files with 100 additions and 184 deletions
|
|
@ -20,14 +20,6 @@ pub fn tasks_dir() -> PathBuf {
|
|||
harness_dir().join("bash-tasks")
|
||||
}
|
||||
|
||||
/// Per-agent sqlite in the harness dir: the runner appends normalised
|
||||
/// bash-command heads to its `bash_commands` table, which the harness reads for
|
||||
/// its OTEL bash-command counter.
|
||||
#[must_use]
|
||||
pub fn turn_stats_db() -> PathBuf {
|
||||
harness_dir().join("hyperhive-turn-stats.sqlite")
|
||||
}
|
||||
|
||||
/// The harness-served in-agent todo socket (loose-ends v2). The runner
|
||||
/// pushes bash-task todos here — `upsert_todo` while a task is active,
|
||||
/// a keyless `done` todo on completion — instead of firing a direct
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
//! Capture of normalised shell-command "heads" per bash task into the
|
||||
//! per-agent turn-stats sqlite (`bash_commands` table), read by the
|
||||
//! harness for its OTEL bash-command counter.
|
||||
//! The OTEL bash-command counter: each bash task's normalised shell-command
|
||||
//! "head", counted as `hyperhive.agent.bash.commands` keyed by `command`.
|
||||
//!
|
||||
//! Best-effort: a missing db, a lock, or a parse miss is logged and
|
||||
//! swallowed — recording a stat never affects task execution. The
|
||||
//! read/aggregation side is the agent harness's `stats.rs`, which reads the
|
||||
//! same `bash_commands(ts INTEGER, head TEXT)` rows.
|
||||
//! Best-effort: without `OTEL_EXPORTER_OTLP_ENDPOINT`, or when the exporter
|
||||
//! fails to build, or when the command has no head, nothing is counted —
|
||||
//! recording a stat never affects task execution. The daemon exports the
|
||||
//! counter itself, on the same endpoint and resource attributes the harness
|
||||
//! uses for its own metrics.
|
||||
//!
|
||||
//! Normalisation is deliberately rough (quoting-naive) — it just needs
|
||||
//! to name the tool an operator would recognise. The dominant agent
|
||||
|
|
@ -13,11 +13,57 @@
|
|||
//! `cd …`-style builtins and prefix-runners like `sudo`/`env` to find
|
||||
//! the first real command, then take its basename.
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration;
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use opentelemetry::KeyValue;
|
||||
use opentelemetry::metrics::{Counter, MeterProvider as _};
|
||||
use opentelemetry_otlp::{MetricExporter, Protocol, WithExportConfig};
|
||||
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
|
||||
|
||||
use crate::paths;
|
||||
/// Export cadence, the same as the harness's metrics.
|
||||
const EXPORT_INTERVAL: Duration = Duration::from_mins(1);
|
||||
|
||||
struct Exporter {
|
||||
/// Kept alive for the process lifetime - the `PeriodicReader` only
|
||||
/// exports while its provider lives.
|
||||
_provider: SdkMeterProvider,
|
||||
commands: Counter<u64>,
|
||||
}
|
||||
|
||||
/// Built on the first recorded command. `None` when OTEL isn't configured or
|
||||
/// the exporter failed to build; recording is then a cheap no-op.
|
||||
static EXPORTER: OnceLock<Option<Exporter>> = OnceLock::new();
|
||||
|
||||
fn build() -> Option<Exporter> {
|
||||
if !std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT").is_ok_and(|s| !s.trim().is_empty()) {
|
||||
return None;
|
||||
}
|
||||
// http/json with the blocking client and no explicit endpoint or
|
||||
// resource, as in the harness's exporter: the `PeriodicReader` exports
|
||||
// from a thread with no Tokio reactor, and the SDK reads the endpoint
|
||||
// and `OTEL_RESOURCE_ATTRIBUTES` from the container-wide environment.
|
||||
let provider = MetricExporter::builder()
|
||||
.with_http()
|
||||
.with_protocol(Protocol::HttpJson)
|
||||
.build()
|
||||
.inspect_err(|e| tracing::warn!(error = %e, "bash stats: OTEL exporter not built"))
|
||||
.ok()
|
||||
.map(|exporter| {
|
||||
let reader = PeriodicReader::builder(exporter)
|
||||
.with_interval(EXPORT_INTERVAL)
|
||||
.build();
|
||||
SdkMeterProvider::builder().with_reader(reader).build()
|
||||
})?;
|
||||
let commands = provider
|
||||
.meter("hyperhive.bash")
|
||||
.u64_counter("hyperhive.agent.bash.commands")
|
||||
.build();
|
||||
Some(Exporter {
|
||||
_provider: provider,
|
||||
commands,
|
||||
})
|
||||
}
|
||||
|
||||
/// 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`),
|
||||
|
|
@ -103,38 +149,16 @@ pub fn command_head(cmd: &str) -> Option<String> {
|
|||
segments.into_iter().find_map(segment_head)
|
||||
}
|
||||
|
||||
/// Record one bash task's normalised command head. Best-effort: any
|
||||
/// error (no db yet, locked, unparseable command) is logged at debug
|
||||
/// and swallowed.
|
||||
/// 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;
|
||||
};
|
||||
if let Err(e) = record_head(&head) {
|
||||
tracing::debug!(error = ?e, head = %head, "bash stats: record failed");
|
||||
}
|
||||
}
|
||||
|
||||
fn record_head(head: &str) -> rusqlite::Result<()> {
|
||||
let conn = Connection::open(paths::turn_stats_db())?;
|
||||
// The harness writes turn rows to this same db (rollback-journal,
|
||||
// not WAL) and sibling bash tasks may write concurrently — wait out
|
||||
// a transient lock rather than dropping the stat on SQLITE_BUSY.
|
||||
conn.busy_timeout(std::time::Duration::from_secs(2))?;
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS bash_commands (ts INTEGER NOT NULL, head TEXT NOT NULL)",
|
||||
[],
|
||||
)?;
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs()
|
||||
.cast_signed();
|
||||
conn.execute(
|
||||
"INSERT INTO bash_commands (ts, head) VALUES (?1, ?2)",
|
||||
params![now, head],
|
||||
)?;
|
||||
Ok(())
|
||||
let Some(exporter) = EXPORTER.get_or_init(build).as_ref() else {
|
||||
return;
|
||||
};
|
||||
exporter.commands.add(1, &[KeyValue::new("command", head)]);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue