use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "outcome", rename_all = "snake_case")]
pub enum EgressRefutation {
Corroborated { peer: String },
Refuted { watched_surface: &'static str },
NoCoverage { reason: &'static str },
CoverageDegraded { reason: &'static str },
}
impl EgressRefutation {
#[must_use]
pub fn refutes(&self) -> bool {
matches!(self, Self::Refuted { .. })
}
}
const EGRESS_PROBE_SURFACE: &str = "cgroup_sock_addr:connect4";
#[must_use]
pub fn refute_egress(
observation_health: Option<&Value>,
observed_peers: &[String],
expected_peer: Option<&str>,
) -> EgressRefutation {
let Some(oh) = observation_health else {
return EgressRefutation::NoCoverage {
reason: "no observation_health artifact: nothing declares what was watched",
};
};
match oh.get("kernel_layer").and_then(Value::as_str) {
Some("complete") => {}
Some("partial_ringbuf_drops") => {
return EgressRefutation::CoverageDegraded {
reason: "kernel layer lost records to ring-buffer pressure; a missing connect may \
have been dropped rather than absent",
}
}
_ => {
return EgressRefutation::NoCoverage {
reason: "kernel layer absent: nothing was watching",
}
}
}
if oh.get("ringbuf_drops").and_then(Value::as_u64).unwrap_or(0) > 0 {
return EgressRefutation::CoverageDegraded {
reason: "ring-buffer drops recorded: the observer knows it missed events",
};
}
match oh
.get("network_protocol_coverage")
.and_then(Value::as_str)
.unwrap_or("unknown")
{
"connect_only" | "connect_and_datagram_peer_observed" => {}
"datagram_peer_observed" => {
return EgressRefutation::CoverageDegraded {
reason:
"datagram peers observed but connect was not: a connect-based egress could \
have occurred unseen",
}
}
"absent" => {
return EgressRefutation::NoCoverage {
reason: "network coverage absent: no egress probe attached",
}
}
_ => {
return EgressRefutation::NoCoverage {
reason: "network coverage unknown: the run does not declare whether it watched",
}
}
}
match oh
.get("cgroup_correlation")
.and_then(Value::as_str)
.unwrap_or("failed")
{
"clean" => {}
"partial" => {
return EgressRefutation::CoverageDegraded {
reason:
"cgroup correlation partial: an egress may have occurred under a cgroup the \
run could not attribute",
}
}
_ => {
return EgressRefutation::NoCoverage {
reason: "cgroup correlation failed: observations cannot be tied to this workload",
}
}
}
match expected_peer {
Some(peer) if observed_peers.iter().any(|p| p == peer) => EgressRefutation::Corroborated {
peer: peer.to_string(),
},
_ => EgressRefutation::Refuted {
watched_surface: EGRESS_PROBE_SURFACE,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn clean_health() -> Value {
json!({
"schema": "assay.runner.observation_health.v0",
"kernel_layer": "complete",
"ringbuf_drops": 0,
"network_protocol_coverage": "connect_only",
"cgroup_correlation": "clean",
})
}
#[test]
fn a_watched_run_that_saw_nothing_refutes_the_claim() {
let r = refute_egress(Some(&clean_health()), &[], Some("api.github.com:443"));
assert!(r.refutes(), "{r:?}");
match r {
EgressRefutation::Refuted { watched_surface } => {
assert_eq!(
watched_surface, EGRESS_PROBE_SURFACE,
"a refutation names its surface"
)
}
other => panic!("{other:?}"),
}
}
#[test]
fn a_watched_run_that_saw_the_connect_corroborates() {
let peers = vec!["api.github.com:443".to_string()];
let r = refute_egress(Some(&clean_health()), &peers, Some("api.github.com:443"));
assert!(matches!(r, EgressRefutation::Corroborated { .. }), "{r:?}");
}
#[test]
fn no_artifact_cannot_refute() {
let r = refute_egress(None, &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(matches!(r, EgressRefutation::NoCoverage { .. }), "{r:?}");
}
#[test]
fn ringbuf_drops_cannot_refute() {
let mut h = clean_health();
h["ringbuf_drops"] = json!(3);
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(
matches!(r, EgressRefutation::CoverageDegraded { .. }),
"{r:?}"
);
}
#[test]
fn an_unattached_network_probe_cannot_refute() {
let mut h = clean_health();
h["network_protocol_coverage"] = json!("absent");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(matches!(r, EgressRefutation::NoCoverage { .. }), "{r:?}");
}
#[test]
fn unknown_coverage_cannot_refute() {
let mut h = clean_health();
h["network_protocol_coverage"] = json!("unknown");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
}
#[test]
fn partial_cgroup_correlation_cannot_refute() {
let mut h = clean_health();
h["cgroup_correlation"] = json!("partial");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(
matches!(r, EgressRefutation::CoverageDegraded { .. }),
"{r:?}"
);
}
#[test]
fn failed_cgroup_correlation_cannot_refute() {
let mut h = clean_health();
h["cgroup_correlation"] = json!("failed");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(matches!(r, EgressRefutation::NoCoverage { .. }), "{r:?}");
}
#[test]
fn a_degraded_kernel_layer_cannot_refute() {
let mut h = clean_health();
h["kernel_layer"] = json!("partial_ringbuf_drops");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
assert!(
matches!(r, EgressRefutation::CoverageDegraded { .. }),
"{r:?}"
);
}
#[test]
fn datagram_only_coverage_cannot_refute_a_connect() {
let mut h = clean_health();
h["network_protocol_coverage"] = json!("datagram_peer_observed");
let r = refute_egress(Some(&h), &[], Some("api.github.com:443"));
assert!(!r.refutes());
}
#[test]
fn the_coverage_guard_is_what_produces_the_refusal_not_the_empty_peer_set() {
let blind = json!({
"kernel_layer": "complete", "ringbuf_drops": 0,
"network_protocol_coverage": "absent", "cgroup_correlation": "clean",
});
assert!(!refute_egress(Some(&blind), &[], Some("peer")).refutes());
let mut watching = blind.clone();
watching["network_protocol_coverage"] = json!("connect_only");
assert!(
refute_egress(Some(&watching), &[], Some("peer")).refutes(),
"same empty peer set, only coverage changed: the guard must be what decides"
);
}
#[test]
fn every_blind_state_is_distinguishable_from_a_clean_refutation() {
let blind = [
json!({"kernel_layer": "absent"}),
json!({"kernel_layer": "complete", "ringbuf_drops": 1, "network_protocol_coverage": "connect_only", "cgroup_correlation": "clean"}),
json!({"kernel_layer": "complete", "ringbuf_drops": 0, "network_protocol_coverage": "absent", "cgroup_correlation": "clean"}),
json!({"kernel_layer": "complete", "ringbuf_drops": 0, "network_protocol_coverage": "connect_only", "cgroup_correlation": "partial"}),
];
for h in blind {
let r = refute_egress(Some(&h), &[], Some("peer"));
assert!(!r.refutes(), "blind run must not refute: {h} -> {r:?}");
}
assert!(refute_egress(Some(&clean_health()), &[], Some("peer")).refutes());
}
}