use assay_monitor::probes::{ProbeAttachment, ProbeOutcome, EGRESS_PEER_PROBE};
use assay_runner_schema::{
CgroupCorrelationStatus, KernelLayerStatus, NetworkEndpointClaimScope,
NetworkProtocolCoverageStatus, ObservationHealth, PolicyLayerStatus, SdkLayerStatus,
};
#[cfg(test)]
const CONNECT_PROBE: &str = "sys_enter_connect";
pub(crate) fn new_run_id() -> String {
format!("monitor_{:032x}", rand::random::<u128>())
}
pub(crate) fn build(
run_id: &str,
attachment: &ProbeAttachment,
ringbuf_drops: u64,
policy_declared: bool,
) -> ObservationHealth {
let network_protocol_coverage =
if attachment.outcome(EGRESS_PEER_PROBE) == Some(ProbeOutcome::Attached) {
NetworkProtocolCoverageStatus::ConnectOnly
} else {
NetworkProtocolCoverageStatus::Absent
};
let mut health = ObservationHealth::new(run_id, "linux")
.with_policy_layer(if policy_declared {
PolicyLayerStatus::Present
} else {
PolicyLayerStatus::Absent
})
.with_sdk_layer(SdkLayerStatus::Absent)
.with_cgroup_correlation(CgroupCorrelationStatus::Clean);
health.kernel_layer = if attachment.attached_probes().is_empty() {
KernelLayerStatus::Absent
} else if ringbuf_drops > 0 {
KernelLayerStatus::PartialRingbufDrops
} else {
KernelLayerStatus::Complete
};
health = health.with_ringbuf_drops(ringbuf_drops);
health.network_protocol_coverage = network_protocol_coverage;
health.network_endpoint_claim_scope = match network_protocol_coverage {
NetworkProtocolCoverageStatus::ConnectOnly => NetworkEndpointClaimScope::DiagnosticOnly,
_ => NetworkEndpointClaimScope::NotApplicable,
};
for probe in attachment.skipped_probes() {
health
.notes
.push(format!("probe not attached: {probe} (surface unobserved)"));
}
health
}
pub(crate) fn write_to(
health: &ObservationHealth,
writer: &mut impl std::io::Write,
) -> std::io::Result<()> {
serde_json::to_writer_pretty(&mut *writer, health).map_err(std::io::Error::other)?;
writer.write_all(b"\n")?;
writer.flush()
}
#[cfg(test)]
mod tests {
use super::*;
use assay_runner_schema::KernelLayerStatus;
fn attachment(attached: &[&'static str], skipped: &[&'static str]) -> ProbeAttachment {
let mut a = ProbeAttachment::default();
for probe in attached {
a.attached(probe);
}
for probe in skipped {
a.skipped(probe);
}
a
}
#[test]
fn the_connect_tracepoint_alone_does_not_earn_network_coverage() {
let health = build("run1", &attachment(&[CONNECT_PROBE], &[]), 0, false);
assert_eq!(
health.network_protocol_coverage,
NetworkProtocolCoverageStatus::Absent,
"the tracepoint sees connects but supplies no peers, so it cannot ground an absence claim"
);
}
#[test]
fn an_attached_but_silent_connect_probe_still_reports_coverage() {
let health = build("run1", &attachment(&[EGRESS_PEER_PROBE], &[]), 0, false);
assert_eq!(
health.network_protocol_coverage,
NetworkProtocolCoverageStatus::ConnectOnly
);
assert_eq!(
health.network_endpoint_claim_scope,
NetworkEndpointClaimScope::DiagnosticOnly
);
}
#[test]
fn an_unattached_connect_probe_reports_absent_coverage() {
let health = build("run1", &attachment(&[], &[CONNECT_PROBE]), 0, false);
assert_eq!(
health.network_protocol_coverage,
NetworkProtocolCoverageStatus::Absent
);
assert_eq!(
health.network_endpoint_claim_scope,
NetworkEndpointClaimScope::NotApplicable
);
}
#[test]
fn every_unattached_probe_is_named_in_the_notes() {
let health = build(
"run1",
&attachment(&["lsm:file_open"], &["sys_enter_fork", CONNECT_PROBE]),
0,
false,
);
assert!(health
.notes
.iter()
.any(|n| n.contains("sys_enter_fork") && n.contains("unobserved")));
assert!(health.notes.iter().any(|n| n.contains(CONNECT_PROBE)));
}
#[test]
fn a_run_where_nothing_attached_has_an_absent_kernel_layer() {
let health = build("run1", &attachment(&[], &[CONNECT_PROBE]), 0, false);
assert_eq!(health.kernel_layer, KernelLayerStatus::Absent);
}
#[test]
fn lost_records_degrade_the_kernel_layer() {
let clean = build("run1", &attachment(&[CONNECT_PROBE], &[]), 0, false);
assert_eq!(clean.kernel_layer, KernelLayerStatus::Complete);
let lossy = build("run1", &attachment(&[CONNECT_PROBE], &[]), 7, false);
assert_eq!(lossy.kernel_layer, KernelLayerStatus::PartialRingbufDrops);
assert_eq!(lossy.ringbuf_drops, 7);
}
#[test]
fn a_declared_policy_is_the_only_thing_that_makes_the_policy_layer_present() {
assert_eq!(
build("run1", &attachment(&[], &[]), 0, false).policy_layer,
PolicyLayerStatus::Absent
);
assert_eq!(
build("run1", &attachment(&[], &[]), 0, true).policy_layer,
PolicyLayerStatus::Present
);
}
#[test]
fn distinct_invocations_do_not_share_a_run_id_when_posture_matches() {
let first = new_run_id();
let second = new_run_id();
assert_ne!(
first, second,
"equal monitor posture does not prove two artifacts came from one invocation"
);
assert!(first.starts_with("monitor_"));
assert!(second.starts_with("monitor_"));
}
#[test]
fn the_artifact_round_trips() {
let health = build(
"run1",
&attachment(&[CONNECT_PROBE], &["sys_enter_fork"]),
2,
true,
);
let json = serde_json::to_string(&health).expect("serialize");
let back: ObservationHealth = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back, health);
assert_eq!(back.schema, "assay.runner.observation_health.v0");
}
}