Watch
0
0
Fork
You've already forked hyperhive
0

hive-agent: test the telemetry, sink and runtime error paths of the turn loop

This commit is contained in:
damocles 2026-11-16 20:55:21 +01:00 • committed by mara
commit 07028557d1

View file

@ -905,7 +905,7 @@ fn archive_session(bus: &Bus, session: &AgentSession) {
#[cfg(test)]
mod tests {
use super::{PercentPolicy, SoftLimitPolicy, auto_reset_watermark_tokens};
use super::{BusSink, PercentPolicy, SoftLimitPolicy, auto_reset_watermark_tokens};
use super::{
PermissionAsk, TurnError, TurnOutcome, acp_permits, apply_telemetry, cache_disabled_note,
cache_is_cold, emit_turn_end, error_to_turn, nonzero_or, parse_u64, report_compact,
@ -913,6 +913,7 @@ mod tests {
};
use crate::events::{Bus, LiveEvent};
use hive_claude::{CompactionPolicy, Usage};
use hive_runtime::Sink as _;
use hive_runtime::{AcpError, Error};
/// The swarm's soft limit replaces the harness's percent: 20% of a
@ -1027,6 +1028,116 @@ mod tests {
assert_eq!(resolved, None);
}
fn usage(input: u64) -> hive_claude::TokenUsage {
let mut u = hive_claude::TokenUsage::default();
u.input_tokens = input;
u
}
/// A completed turn's telemetry reaches the bus: context and cost usage,
/// the model the API resolved and the window it reported.
#[test]
fn a_turns_telemetry_is_recorded_on_the_bus() {
let _dir = crate::paths::harness_dir_guard();
let bus = Bus::new();
let mut t = hive_claude::Telemetry::default();
t.context = usage(1_000);
t.cost = usage(5_000);
t.model = Some("claude-opus-4-8".to_owned());
t.context_window = Some(200_000);
apply_telemetry(&bus, &t);
assert_eq!(bus.last_ctx_usage().unwrap().context_tokens(), 1_000);
assert_eq!(bus.last_cost_usage().unwrap().context_tokens(), 5_000);
assert_eq!(
bus.last_resolved_model().as_deref(),
Some("claude-opus-4-8")
);
assert_eq!(bus.api_context_window(), Some(200_000));
}
/// A turn that reported a `result` but no `assistant` event has cost and no
/// context: the cost stands in, so the badge and the auto-reset watermark
/// do not fall to zero.
#[test]
fn a_turn_with_cost_but_no_context_uses_the_cost_as_context() {
let _dir = crate::paths::harness_dir_guard();
let bus = Bus::new();
let mut t = hive_claude::Telemetry::default();
t.cost = usage(7_000);
apply_telemetry(&bus, &t);
assert_eq!(bus.last_ctx_usage().unwrap().context_tokens(), 7_000);
assert_eq!(bus.api_context_window(), None);
}
/// A turn that parsed nothing leaves the usage and the window of the turn
/// before it alone.
#[test]
fn a_turn_that_parsed_nothing_keeps_the_previous_usage_and_window() {
let _dir = crate::paths::harness_dir_guard();
let bus = Bus::new();
let mut t = hive_claude::Telemetry::default();
t.context = usage(1_000);
t.cost = usage(1_000);
t.context_window = Some(200_000);
apply_telemetry(&bus, &t);
apply_telemetry(&bus, &hive_claude::Telemetry::default());
assert_eq!(bus.last_ctx_usage().unwrap().context_tokens(), 1_000);
assert_eq!(bus.api_context_window(), Some(200_000));
}
/// What claude prints reaches the terminal: a raw event as a stream event,
/// anything that is not json and stderr as notes.
#[test]
fn the_sink_puts_claudes_output_on_the_bus() {
let _dir = crate::paths::harness_dir_guard();
let bus = Bus::new();
let mut events = bus.subscribe();
let sink = BusSink::new(&bus);
sink.on_event(&serde_json::json!({"type": "assistant"}));
sink.on_stdout_line("not json");
sink.on_stderr_line("a warning");
let mut got = Vec::new();
while let Ok(e) = events.try_recv() {
got.push(match e.event {
LiveEvent::Stream(v) => format!("stream {}", v["type"]),
LiveEvent::Note { text } => text,
_ => String::new(),
});
}
got.retain(|s| !s.is_empty());
assert_eq!(
got,
[
"stream \"assistant\"",
"(non-json) not json",
"stderr: a warning"
]
);
}
/// The claude driver's sentinels reach the serve loop as their own turn
/// errors through the runtime layer.
#[test]
fn claude_runtime_errors_map_to_the_turn_errors_the_serve_loop_reacts_to() {
use hive_runtime::Error;
assert!(matches!(
runtime_error_to_turn(Error::Claude(hive_claude::Error::AuthFailed)),
Err(TurnError::AuthFailed)
));
assert!(matches!(
runtime_error_to_turn(Error::Claude(hive_claude::Error::IdleTimeout)),
Err(TurnError::ApiStall)
));
}
#[test]
fn an_absent_or_blank_value_is_not_a_number() {
assert_eq!(parse_u64(None), None);