diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index de41690..2fc561e 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -1180,19 +1180,79 @@ fn health_check(options: &Options) -> Result<()> { let surface = node .get_required::("latest") .context("Muninn telemetry surface is unavailable")?; - if surface.state == "idle" || surface.state == "active" { - println!( - "Muninn healthy: {} on {} ({})", - surface.surface_id, surface.host_id, surface.state - ); - Ok(()) - } else { - Err(anyhow!( + if surface.state != "idle" && surface.state != "active" { + return Err(anyhow!( "Muninn telemetry surface is {}: {}", surface.state, surface.detail - )) + )); } + + verify_move_sources_fresh(options, &node)?; + + println!( + "Muninn healthy: {} on {} ({})", + surface.surface_id, surface.host_id, surface.state + ); + Ok(()) +} + +fn verify_move_sources_fresh( + options: &Options, + node: &cultmesh_rs::CultMeshNode, +) -> Result<()> { + if options.move_state_sources.is_empty() { + return Ok(()); + } + + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .context("system clock is before Unix epoch")? + .as_secs(); + let max_age_seconds = options + .interval_seconds + .unwrap_or(15) + .saturating_mul(4) + .max(30); + let states = node.cache().get_all::()?; + + for source in &options.move_state_sources { + let latest = states + .iter() + .filter(|state| state.host_id == options.host_id && state.move_id == source.move_id) + .max_by_key(|state| state.sequence) + .ok_or_else(|| { + anyhow!( + "Muninn Move source {} has no controller-state records", + source.move_id + ) + })?; + let observed_seconds = parse_unix_timestamp(&latest.observed_at).with_context(|| { + format!( + "Muninn Move source {} has invalid observed_at", + source.move_id + ) + })?; + let age = now.saturating_sub(observed_seconds); + if age > max_age_seconds { + return Err(anyhow!( + "Muninn Move source {} is stale: observed {} seconds ago, max {}", + source.move_id, + age, + max_age_seconds + )); + } + } + + Ok(()) +} + +fn parse_unix_timestamp(value: &str) -> Result { + value + .strip_prefix("unix-") + .ok_or_else(|| anyhow!("timestamp must start with unix-"))? + .parse() + .context("timestamp seconds must be an integer") } fn request_move_light(options: Options) -> Result<()> { diff --git a/scripts/health-nightwing-muninn.ps1 b/scripts/health-nightwing-muninn.ps1 index 8e9d65d..d1fc0d7 100644 --- a/scripts/health-nightwing-muninn.ps1 +++ b/scripts/health-nightwing-muninn.ps1 @@ -2,7 +2,9 @@ param( [string] $SshTarget = "nightwing", [string] $MuninnExe = "/home/metacrat/.local/bin/muninn", [string] $StorePath = "/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc", - [string] $PidPath = "/home/metacrat/.local/state/gamecult/muninn/muninn.pid" + [string] $PidPath = "/home/metacrat/.local/state/gamecult/muninn/muninn.pid", + [string] $MoveState = "move-usb=/dev/input/by-id/usb-Sony_Computer_Entertainment_Motion_Controller-joystick", + [int] $IntervalSeconds = 15 ) $ErrorActionPreference = "Stop" @@ -21,7 +23,11 @@ else echo 'Muninn serve process is not running on Nightwing' >&2 exit 1 fi -'$MuninnExe' --health --store '$StorePath' +'$MuninnExe' --health \ + --store '$StorePath' \ + --host nightwing \ + --move-state '$MoveState' \ + --interval-seconds '$IntervalSeconds' "@ ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget $remoteScript diff --git a/scripts/restart-nightwing-muninn.ps1 b/scripts/restart-nightwing-muninn.ps1 index 22f4eb0..03a09d6 100644 --- a/scripts/restart-nightwing-muninn.ps1 +++ b/scripts/restart-nightwing-muninn.ps1 @@ -4,6 +4,7 @@ param( [string] $StorePath = "/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc", [string] $LogRoot = "/home/metacrat/.local/state/gamecult/muninn", [string] $MoveState = "move-usb=/dev/input/by-id/usb-Sony_Computer_Entertainment_Motion_Controller-joystick", + [string] $MoveEvidenceStream = "muninn:nightwing:move-evidence", [int] $IntervalSeconds = 15 ) @@ -28,6 +29,7 @@ nohup '$MuninnExe' serve \ --log-root '$LogRoot' \ --host nightwing \ --move-state '$MoveState' \ + --move-evidence-stream '$MoveEvidenceStream' \ --interval-seconds '$IntervalSeconds' \ > '$LogRoot/muninn-serve.out.log' \ 2> '$LogRoot/muninn-serve.err.log' \