Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

41.4 KB
Raw Blame
//! NATS health-heartbeat publisher.
//!
//! In `health_mode=nats` the gateway expects every worker to
//! periodically publish a [`WorkerStatusMessage`]-shaped payload to
//! `sie.health.<worker_id>` so its `WorkerRegistry` knows the worker
//! exists, which `bundle` it serves, and which `pool_name` to route
//! NATS work to. Without this signal the gateway returns
//! `202 provisioning` for every request.
//!
//! See `packages/sie_gateway/src/discovery/nats_health.rs` for the
//! consumer side and `packages/sie_gateway/docs/architecture-guide.md`
//! for the routing contract.
//!
//! # What the gateway actually reads
//!
//! Only a small subset of fields is load-bearing for routing:
//!
//! * `bundle` — must match the bundle resolved by the gateway's
//!   model registry (lowercase compared); routes only consider
//!   workers whose bundle index entry contains them.
//! * `pool_name` — non-empty so [`resolve_queue_route`] can return
//!   the pool the dispatcher publishes work onto. An empty
//!   `pool_name` makes the worker invisible to the queue path.
//! * `machine_profile` — the hardware lane embedded in the queue
//!   subject. `X-SIE-MACHINE-PROFILE` constrains route resolution to
//!   this value; otherwise the gateway infers it from a healthy worker.
//! * `ready` — drives the `WorkerHealth::Healthy` transition. We
//!   gate on the local [`Readiness`] state so the heartbeat agrees
//!   with `/readyz`.
//!
//! * `unsupported_models` — routable model ids covered by
//!   `bundle_config_hash` that this worker cannot serve (for example a model
//!   whose adapter module its image lacks). The gateway does not route those
//!   models to this worker. Omitted when empty, which is the steady state.
//!
//! `loaded_models` and the GPU memory fields are mostly informational, but
//! the gateway also uses `loaded_models` as the per-model dispatch readiness
//! signal. Backends that need explicit residency, such as Candle, must only
//! publish models here after their readiness handshake has made them serveable.
//!
//! `loading_models` is the engine's in-progress load set. It rides this same
//! heartbeat, and the publisher sends the payload immediately when that set
//! changes. A failed IPC ping keeps the previous set until the last successful
//! ping is older than the gateway heartbeat timeout; the sidecar then clears
//! it. Heartbeats continue either way, so a wedged engine must not hold the
//! lane until liveness restarts the container.
//!
//! [`WorkerStatusMessage`]: https://github.com/superlinked/sie/blob/main/packages/sie_gateway/src/types/worker.rs
//! [`resolve_queue_route`]: https://github.com/superlinked/sie/blob/main/packages/sie_gateway/src/state/worker_registry.rs

use std::io::{self, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::RwLock;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use async_nats::Client;
use serde::Serialize;
use tokio::sync::Notify;
use tokio::task::JoinHandle;
use tokio::time::interval;
use tracing::{debug, info, warn};

use crate::backend::adapter_pool::{AdapterWorkerPool, NumericalProcessObservation};
use crate::readiness::Readiness;
use crate::runtime_state::RuntimeState;
use crate::shutdown::Shutdown;

pub type SharedBundleConfigHash = Arc<RwLock<String>>;
pub type SharedLoadedModels = Arc<RwLock<Vec<String>>>;
pub type SharedUnsupportedModels = Arc<RwLock<Vec<String>>>;
pub type SharedNumericalInventory = Arc<RwLock<Option<(Instant, serde_json::Value)>>>;

// Same wire budget as gateway types/worker.rs. Drop the whole observation;
// truncation would hide children that remain reachable through pool fallback.
const MAX_NUMERICAL_INVENTORY_BYTES: usize = 64 * 1024;
const MAX_NUMERICAL_CHILDREN: usize = 256;

struct InventoryWriter(Vec<u8>);

impl Write for InventoryWriter {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        if bytes.len() > MAX_NUMERICAL_INVENTORY_BYTES.saturating_sub(self.0.len()) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "numerical inventory exceeds wire budget",
            ));
        }
        self.0.extend_from_slice(bytes);
        Ok(bytes.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn bounded_inventory(
    children: &[NumericalProcessObservation],
    observed_at_unix_ms: u64,
) -> Option<serde_json::Value> {
    if children.is_empty() || children.len() > MAX_NUMERICAL_CHILDREN {
        return None;
    }
    #[derive(Serialize)]
    struct Inventory<'a> {
        observed_at_unix_ms: u64,
        children: &'a [NumericalProcessObservation],
    }
    let mut writer = InventoryWriter(Vec::new());
    serde_json::to_writer(
        &mut writer,
        &Inventory {
            observed_at_unix_ms,
            children,
        },
    )
    .ok()?;
    serde_json::from_slice(&writer.0).ok()
}

/// Poll independently of normal IPC pings and NATS health publication. A slow
/// or unsupported diagnostic RPC must never consume the readiness budget.
pub fn spawn_numerical_inventory(
    pool: Arc<AdapterWorkerPool>,
    inventory: SharedNumericalInventory,
    shutdown: Arc<Shutdown>,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let mut tick = interval(DEFAULT_PUBLISH_INTERVAL);
        tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            tokio::select! {
                biased;
                _ = shutdown.wait() => break,
                _ = tick.tick() => {}
            }
            let started = Instant::now();
            let observed_at_unix_ms = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .ok()
                .and_then(|duration| u64::try_from(duration.as_millis()).ok())
                .unwrap_or(0);
            let value = if pool.child_count() <= MAX_NUMERICAL_CHILDREN && observed_at_unix_ms > 0 {
                tokio::select! {
                    biased;
                    _ = shutdown.wait() => break,
                    result = tokio::time::timeout(DEFAULT_PUBLISH_INTERVAL, pool.numerical_process_inventory()) => {
                        result.ok().and_then(|children| bounded_inventory(&children, observed_at_unix_ms))
                    }
                }
            } else {
                None
            };
            if let Ok(mut guard) = inventory.write() {
                *guard = value.map(|value| (started, value));
            }
        }
    })
}

/// Default subject prefix used by the gateway. Workers always
/// publish to `sie.health.<worker_id>` because the gateway's
/// `extract_worker_url_from_status` reads `splitn(3, '.')` on the
/// subject, so any sub-token segments would confuse URL extraction.
pub const HEALTH_SUBJECT_PREFIX: &str = "sie.health";

/// Default interval between heartbeats. Matches the gateway's
/// `NatsHealthManager::start_heartbeat_loop` 5 s tick — anything
/// faster wastes NATS bandwidth and anything slower risks the
/// gateway flipping the worker unhealthy via `check_heartbeats`
/// (default `heartbeat_timeout` is 30 s, so 5 s gives a 6× margin).
pub const DEFAULT_PUBLISH_INTERVAL: Duration = Duration::from_secs(5);
const SATURATION_SET_PERCENT: i64 = 90;
const SATURATION_CLEAR_PERCENT: i64 = 70;

/// Static config for the heartbeat publisher.
///
/// Most fields are captured at startup. The ready flag comes from
/// [`Readiness`], and `bundle_config_hash` is shared with the live config
/// subscriber so gateway readiness can observe successful worker-side apply.
#[derive(Clone)]
pub struct HealthPublisherConfig {
    /// Stable identifier used as the subject leaf and as the
    /// fallback URL the gateway constructs. Should match
    /// `WorkerConfig::worker_id`.
    pub worker_id: String,
    /// The bundle this pod's adapter is serving. The gateway's
    /// per-bundle index keys on this (lowercased), so it must
    /// match `BundleConfig.id`. Sourced from `SIE_BUNDLE`.
    pub bundle: String,
    /// Pool name. The gateway publishes work onto
    /// `sie.work.<pool_name>.<machine_profile>.<bundle>.<model>`; the
    /// worker must echo the same pool here or queue-route resolution
    /// returns `None` and requests 202-provision forever. Sourced from
    /// `SIE_POOL`.
    pub pool_name: String,
    /// Optional machine-profile label (e.g. `l4`, `a100`).
    /// Consulted only when the inbound request carries
    /// `X-SIE-MACHINE-PROFILE`. Empty means "any GPU".
    pub machine_profile: String,
    /// Reported GPU count. The registry coerces `0 -> 1` so a
    /// missing value still routes; we still want to surface the
    /// real count when known so admin views show accurate totals.
    pub gpu_count: i32,
    /// Bundle-config hash so the gateway can correlate against its own
    /// model registry epoch. Updated by the config subscriber after a
    /// successful backend config apply.
    pub bundle_config_hash: SharedBundleConfigHash,
    /// Models covered by `bundle_config_hash` that the backend cannot serve.
    /// Committed together with the hash by the config subscriber.
    pub unsupported_models: SharedUnsupportedModels,
    /// Models the colocated backend reports as loaded. Updated by the IPC
    /// heartbeat, not by config apply.
    pub loaded_models: SharedLoadedModels,
    /// Models the backend still has in `_loading`. Same update path as
    /// `loaded_models`, plus an immediate republish when the set changes.
    pub loading_models: SharedLoadedModels,
    /// Wakes [`spawn`] to publish the current payload before `interval` elapses.
    pub publish_now: Arc<Notify>,
    /// Independently polled process diagnostics; never grants routing authority.
    pub numerical_process_inventory: SharedNumericalInventory,
    /// Positive only when every backend child supports the method fence.
    pub execution_authority_v1: Arc<AtomicBool>,
    /// Positive only when every backend child re-verifies numerical admissions.
    pub numerical_admission_v1: Arc<AtomicBool>,
    /// The versioned pull consumer must also be running.
    pub authority_consumer_ready: Arc<AtomicBool>,
    /// The admitted-work pull consumer must also be running.
    pub admission_consumer_ready: Arc<AtomicBool>,
    /// Runtime pressure/capacity gauges mirrored into the heartbeat payload.
    pub runtime_state: Arc<RuntimeState>,
    /// How often to publish. Defaults to [`DEFAULT_PUBLISH_INTERVAL`].
    pub interval: Duration,
}

impl HealthPublisherConfig {
    /// Subject this pod publishes to. Kept as a method so the
    /// gateway-side parser (`splitn(3, '.')`) and this side stay
    /// in lockstep — change the prefix here and the gateway will
    /// see no health messages.
    pub fn subject(&self) -> String {
        format!("{}.{}", HEALTH_SUBJECT_PREFIX, self.worker_id)
    }
}

/// Wire shape published to NATS. Mirrors the gateway's
/// [`crate::types::WorkerStatusMessage`] subset that's actually
/// load-bearing for routing.
///
/// Serialised as JSON because the gateway's
/// `handle_nats_message` tries msgpack first and falls back to
/// JSON; JSON keeps the payload trivially `nats sub`-debuggable
/// from a shell, which matters for the post-deploy smoke test
/// we do every time. Both formats are accepted, so we can switch
/// to msgpack later without a flag day on either side.
#[derive(Debug, Serialize)]
struct WorkerStatusPayload<'a> {
    name: &'a str,
    ready: bool,
    terminated: bool,
    supports_execution_authority_v1: bool,
    supports_numerical_admission_v1: bool,
    supports_numerical_admission_subject_v1: bool,
    gpu_count: i32,
    total_gpu_slots: i32,
    ready_gpu_slots: i32,
    machine_profile: &'a str,
    pool_name: &'a str,
    bundle: &'a str,
    bundle_config_hash: &'a str,
    loaded_models: &'a [String],
    loading_models: &'a [String],
    queue_depth: i32,
    pending_cost: i64,
    inflight_batches: i32,
    saturated: bool,
    #[serde(skip_serializing_if = "<[String]>::is_empty")]
    unsupported_models: &'a [String],
    #[serde(skip_serializing_if = "Option::is_none")]
    numerical_process_inventory: Option<&'a serde_json::Value>,
}

fn encode_payload(
    config: &HealthPublisherConfig,
    ready: bool,
    terminated: bool,
) -> Result<Vec<u8>, serde_json::Error> {
    let hash = config
        .bundle_config_hash
        .read()
        .expect("bundle config hash lock poisoned");
    let loaded_models_guard = match config.loaded_models.read() {
        Ok(guard) => Some(guard),
        Err(_) => {
            warn!("loaded_models lock poisoned; publishing empty loaded model list");
            None
        }
    };
    let loaded_models = match &loaded_models_guard {
        Some(guard) => guard.as_slice(),
        None => &[],
    };
    let loading_models_guard = match config.loading_models.read() {
        Ok(guard) => Some(guard),
        Err(_) => {
            warn!("loading_models lock poisoned; publishing an empty in-progress load set");
            None
        }
    };
    let loading_models = match &loading_models_guard {
        Some(guard) => guard.as_slice(),
        None => &[],
    };
    // Read under the hash guard taken above: the config subscriber writes the
    // hash and this list under the same order, so the pair is consistent.
    let unsupported_models = config
        .unsupported_models
        .read()
        .expect("unsupported models lock poisoned");
    let configured_slots = config.gpu_count.max(1);
    let total_gpu_slots = clamp_i64_to_i32(
        config
            .runtime_state
            .worker_gpu_slots_total
            .get()
            .max(configured_slots.into()),
    );
    let ready_gpu_slots = if ready {
        clamp_i64_to_i32(
            config
                .runtime_state
                .worker_gpu_slots_ready
                .get()
                .clamp(0, total_gpu_slots.into()),
        )
    } else {
        0
    };
    config
        .runtime_state
        .worker_gpu_slots_total
        .set(total_gpu_slots.into());
    config
        .runtime_state
        .worker_gpu_slots_ready
        .set(ready_gpu_slots.into());
    let queue_depth = clamp_i64_to_i32(config.runtime_state.worker_queue_depth.get().max(0));
    let pending_cost = config.runtime_state.worker_pending_cost.get().max(0);
    let inflight_batches = clamp_i64_to_i32(config.runtime_state.inflight_batches.get().max(0));
    refresh_worker_saturation(&config.runtime_state);
    let saturated = config.runtime_state.worker_saturated.get() > 0;
    let inventory_guard = config.numerical_process_inventory.read().ok();
    let numerical_process_inventory = inventory_guard.as_deref().and_then(|inventory| {
        inventory
            .as_ref()
            .filter(|(observed, value)| {
                !terminated
                    && observed.elapsed() <= DEFAULT_PUBLISH_INTERVAL * 2
                    && serde_json::to_vec(value)
                        .is_ok_and(|bytes| bytes.len() <= MAX_NUMERICAL_INVENTORY_BYTES)
            })
            .map(|(_, value)| value)
    });
    let supports_execution_authority_v1 = ready
        && !terminated
        && !hash.is_empty()
        && config.execution_authority_v1.load(Ordering::Acquire)
        && config.authority_consumer_ready.load(Ordering::Acquire);
    let supports_numerical_admission_v1 =
        supports_execution_authority_v1 && config.numerical_admission_v1.load(Ordering::Acquire);
    let payload = WorkerStatusPayload {
        name: &config.worker_id,
        ready,
        terminated,
        supports_execution_authority_v1,
        supports_numerical_admission_v1,
        supports_numerical_admission_subject_v1: supports_numerical_admission_v1
            && config.admission_consumer_ready.load(Ordering::Acquire),
        gpu_count: config.gpu_count,
        total_gpu_slots,
        ready_gpu_slots,
        machine_profile: &config.machine_profile,
        pool_name: &config.pool_name,
        bundle: &config.bundle,
        bundle_config_hash: hash.as_str(),
        loaded_models,
        loading_models,
        queue_depth,
        pending_cost,
        inflight_batches,
        saturated,
        unsupported_models: unsupported_models.as_slice(),
        numerical_process_inventory,
    };
    serde_json::to_vec(&payload)
}

fn clamp_i64_to_i32(value: i64) -> i32 {
    i32::try_from(value).unwrap_or_else(|_| if value.is_negative() { 0 } else { i32::MAX })
}

fn refresh_worker_saturation(runtime_state: &RuntimeState) {
    let ready_slots = runtime_state.worker_gpu_slots_ready.get().max(0);
    if ready_slots == 0 {
        runtime_state.worker_saturated.set(0);
        return;
    }

    let capacity = ready_slots
        .saturating_mul(crate::dispatcher::default_max_concurrent_batches().max(1) as i64)
        .max(1);
    let load = runtime_state
        .worker_queue_depth
        .get()
        .max(0)
        .saturating_add(runtime_state.inflight_batches.get().max(0));
    let threshold = if runtime_state.worker_saturated.get() > 0 {
        SATURATION_CLEAR_PERCENT
    } else {
        SATURATION_SET_PERCENT
    };
    let saturated = load.saturating_mul(100) >= capacity.saturating_mul(threshold);
    runtime_state.worker_saturated.set(i64::from(saturated));
}

/// Emit the complete runtime-pressure snapshot once per shared IPC heartbeat.
/// The heartbeat exists in both NATS and local-ingest modes, so this avoids a
/// topology-specific duplicate observation path.
pub(crate) fn record_runtime_telemetry(runtime_state: &RuntimeState) {
    refresh_worker_saturation(runtime_state);
    let nonnegative = |value: i64| u64::try_from(value.max(0)).unwrap_or(u64::MAX);
    runtime_state.telemetry.runtime_snapshot(
        nonnegative(runtime_state.worker_gpu_slots_total.get()),
        nonnegative(runtime_state.worker_gpu_slots_ready.get()),
        nonnegative(runtime_state.worker_queue_depth.get()),
        nonnegative(runtime_state.worker_pending_cost.get()),
        nonnegative(runtime_state.inflight_batches.get()),
        runtime_state.worker_saturated.get() > 0,
    );
}

pub async fn publish_tombstone(
    nats: &Client,
    config: &HealthPublisherConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let subject = config.subject();
    let bytes = encode_payload(config, false, true)?;
    nats.publish(subject.clone(), bytes.into()).await?;
    nats.flush().await?;
    info!(subject = %subject, "nats-health: shutdown tombstone published");
    Ok(())
}

async fn publish_heartbeat(
    nats: &Client,
    config: &HealthPublisherConfig,
    readiness: &Readiness,
    subject: &str,
    consecutive_failures: &mut u64,
) {
    let snap = readiness.snapshot();
    let ready = snap.is_ready();
    let bytes = match encode_payload(config, ready, false) {
        Ok(b) => b,
        Err(e) => {
            warn!(
                error = %e,
                "nats-health: serde_json encode failed (unexpected for static shape)"
            );
            return;
        }
    };
    match nats.publish(subject.to_string(), bytes.into()).await {
        Ok(_) => {
            if *consecutive_failures > 0 {
                info!(
                    consecutive_failures,
                    subject = %subject,
                    "nats-health: publish recovered"
                );
            }
            *consecutive_failures = 0;
            debug!(subject = %subject, ready, "nats-health: heartbeat published");
        }
        Err(e) => {
            *consecutive_failures = consecutive_failures.saturating_add(1);
            // Same warn cadence as `spawn_heartbeat`:
            // first failure, then 5 / 30 / every 120
            // so a sustained NATS outage doesn't
            // drown the log file.
            if *consecutive_failures == 1
                || *consecutive_failures == 5
                || *consecutive_failures == 30
                || consecutive_failures.is_multiple_of(120)
            {
                warn!(
                    consecutive_failures,
                    subject = %subject,
                    error = %e,
                    "nats-health: publish failed"
                );
            } else {
                debug!(
                    consecutive_failures,
                    subject = %subject,
                    error = %e,
                    "nats-health: publish still failing"
                );
            }
        }
    }
}

/// Spawn the heartbeat-publisher loop. The returned [`JoinHandle`]
/// is owned by `run()` so shutdown can `.abort()` it after the
/// pull loop exits — same lifecycle as `crate::spawn_heartbeat`.
///
/// `config.publish_now` republishes the same payload as soon as the
/// in-progress load set changes. That is not a second broker subject.
///
/// Failure modes (non-fatal):
///
/// * NATS publish errors — logged, counter bumped via WARN every
///   5 / 30 / 120 ticks (same backoff pattern as the IPC heartbeat
///   to avoid spamming during a NATS outage).
/// * Serde encode errors — should be unreachable for a static
///   payload shape; logged and skipped.
pub fn spawn(
    nats: Client,
    config: HealthPublisherConfig,
    readiness: Arc<Readiness>,
    shutdown: Arc<Shutdown>,
) -> JoinHandle<()> {
    let subject = config.subject();
    info!(
        subject = %subject,
        bundle = %config.bundle,
        pool = %config.pool_name,
        machine_profile = %config.machine_profile,
        gpu_count = config.gpu_count,
        interval_ms = config.interval.as_millis() as u64,
        "nats-health: heartbeat publisher starting"
    );

    tokio::spawn(async move {
        let mut tick = interval(config.interval);
        tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

        let mut consecutive_failures: u64 = 0;
        loop {
            let wait = shutdown.wait();
            let publish_now = config.publish_now.notified();
            tokio::select! {
                biased;
                _ = wait => {
                    debug!(subject = %subject, "nats-health: shutdown observed; publishing tombstone");
                    if let Err(e) = publish_tombstone(&nats, &config).await {
                        warn!(
                            subject = %subject,
                            error = %e,
                            "nats-health: shutdown tombstone publish failed"
                        );
                    }
                    return;
                }
                _ = tick.tick() => {
                    publish_heartbeat(&nats, &config, &readiness, &subject, &mut consecutive_failures).await;
                }
                _ = publish_now => {
                    publish_heartbeat(&nats, &config, &readiness, &subject, &mut consecutive_failures).await;
                }
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::adapter_pool::NumericalSnapshotStatus;
    use crate::protocol::ipc_types::{
        NumericalProfileObservation, NumericalProfileSnapshotResponse,
    };
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::UnixListener;

    fn cfg() -> HealthPublisherConfig {
        HealthPublisherConfig {
            worker_id: "worker-l4-0".into(),
            bundle: "default".into(),
            pool_name: "l4".into(),
            machine_profile: "l4".into(),
            gpu_count: 1,
            bundle_config_hash: Arc::new(RwLock::new("hash-abc".into())),
            unsupported_models: Arc::new(RwLock::new(Vec::new())),
            loaded_models: Arc::new(RwLock::new(Vec::new())),
            loading_models: Arc::new(RwLock::new(Vec::new())),
            publish_now: Arc::new(Notify::new()),
            numerical_process_inventory: Arc::new(RwLock::new(None)),
            execution_authority_v1: Arc::new(AtomicBool::new(false)),
            numerical_admission_v1: Arc::new(AtomicBool::new(false)),
            authority_consumer_ready: Arc::new(AtomicBool::new(false)),
            admission_consumer_ready: Arc::new(AtomicBool::new(false)),
            runtime_state: Arc::new(RuntimeState::new()),
            interval: DEFAULT_PUBLISH_INTERVAL,
        }
    }

    #[tokio::test]
    async fn pending_diagnostic_rpc_does_not_block_health_or_shutdown() {
        let dir = tempfile::Builder::new()
            .prefix("sie-diag-")
            .tempdir_in("/tmp")
            .unwrap();
        let path = dir.path().join("worker.sock");
        let listener = UnixListener::bind(&path).unwrap();
        let requested = Arc::new(tokio::sync::Notify::new());
        let request_signal = Arc::clone(&requested);
        let worker = tokio::spawn(async move {
            let mut connections = tokio::task::JoinSet::new();
            loop {
                let (mut socket, _) = listener.accept().await.unwrap();
                let request_signal = Arc::clone(&request_signal);
                connections.spawn(async move {
                    loop {
                        let mut length = [0; 4];
                        if socket.read_exact(&mut length).await.is_err() {
                            return;
                        }
                        let mut bytes = vec![0; u32::from_be_bytes(length) as usize];
                        socket.read_exact(&mut bytes).await.unwrap();
                        let request: serde_json::Value = rmp_serde::from_slice(&bytes).unwrap();
                        if request["method"] == "NumericalProfileSnapshot" {
                            request_signal.notify_one();
                            // Keep the diagnostic request pending while normal
                            // health continues on its separate connection.
                            std::future::pending::<()>().await;
                        }
                        assert_eq!(request["method"], "Ping");
                        let response = rmp_serde::to_vec_named(&serde_json::json!({
                            "version": crate::ipc_types::IPC_VERSION,
                            "request_id": request["request_id"], "ok": true,
                            "body": {"ready": true}
                        }))
                        .unwrap();
                        socket
                            .write_all(&(response.len() as u32).to_be_bytes())
                            .await
                            .unwrap();
                        socket.write_all(&response).await.unwrap();
                    }
                });
            }
        });
        let c = cfg();
        let pool = AdapterWorkerPool::new(&[path], 1, 30, 30, Arc::clone(&c.runtime_state));
        let shutdown = Arc::new(Shutdown::new());
        let poller = spawn_numerical_inventory(
            Arc::clone(&pool),
            Arc::clone(&c.numerical_process_inventory),
            Arc::clone(&shutdown),
        );
        tokio::time::timeout(Duration::from_secs(2), requested.notified())
            .await
            .unwrap();
        let ping = tokio::time::timeout(Duration::from_secs(1), pool.primary_ipc().ping(0.0))
            .await
            .unwrap()
            .unwrap();
        assert!(ping.ready);
        let payload: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(payload["ready"], true);
        assert!(payload.get("numerical_process_inventory").is_none());
        shutdown.fire();
        tokio::time::timeout(Duration::from_secs(1), poller)
            .await
            .unwrap()
            .unwrap();
        worker.abort();
        let _ = worker.await;
    }

    #[test]
    fn diagnostic_inventory_keeps_unavailable_children_and_never_grants_authority() {
        let children = vec![NumericalProcessObservation {
            child_index: 0,
            status: NumericalSnapshotStatus::Unavailable,
            snapshot: None,
        }];
        let c = cfg();
        let value = bounded_inventory(&children, 123).unwrap();
        *c.numerical_process_inventory.write().unwrap() = Some((Instant::now(), value.clone()));
        let payload: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(payload["numerical_process_inventory"], value);
        assert_eq!(payload["ready"], true);
        assert_eq!(payload["supports_execution_authority_v1"], false);
    }

    #[test]
    fn oversized_inventory_is_dropped_whole_while_health_survives() {
        let mut children = vec![NumericalProcessObservation {
            child_index: 0,
            status: NumericalSnapshotStatus::Observed,
            snapshot: Some(NumericalProfileSnapshotResponse {
                runtime_instance_id: Some("a".repeat(64)),
                complete: true,
                profiles: (0..100)
                    .map(|index| NumericalProfileObservation {
                        model_id: format!("{index}{}", "x".repeat(1020)),
                        model_contract_sha256: Some("b".repeat(64)),
                        local_identity: None,
                        remote_contract_sha256: None,
                        remote_execution_sha256: None,
                        admission: None,
                    })
                    .collect(),
            }),
        }];
        assert!(bounded_inventory(&children, 1).is_none());
        children = (0..=MAX_NUMERICAL_CHILDREN)
            .map(|index| NumericalProcessObservation {
                child_index: index,
                status: NumericalSnapshotStatus::Unavailable,
                snapshot: None,
            })
            .collect();
        assert!(bounded_inventory(&children, 1).is_none());
        let c = cfg();
        *c.numerical_process_inventory.write().unwrap() = Some((
            Instant::now(),
            serde_json::json!({
                "padding": "x".repeat(MAX_NUMERICAL_INVENTORY_BYTES)
            }),
        ));
        let payload: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert!(payload.get("numerical_process_inventory").is_none());
        assert_eq!(payload["ready"], true);
        assert_eq!(payload["bundle_config_hash"], "hash-abc");
    }

    #[test]
    fn stale_and_shutdown_observations_are_not_republished() {
        let c = cfg();
        for (observed, terminated) in [
            (Instant::now() - DEFAULT_PUBLISH_INTERVAL * 3, false),
            (Instant::now(), true),
        ] {
            *c.numerical_process_inventory.write().unwrap() =
                Some((observed, serde_json::json!({"children": []})));
            let payload: serde_json::Value =
                serde_json::from_slice(&encode_payload(&c, !terminated, terminated).unwrap())
                    .unwrap();
            assert!(payload.get("numerical_process_inventory").is_none());
            assert_eq!(payload["terminated"], terminated);
        }
    }

    #[test]
    fn subject_is_prefix_dot_worker_id() {
        // Gateway's `extract_worker_url_from_status` does
        // `splitn(3, '.')` so the worker_id must occupy a single
        // subject token. We don't enforce that here (NATS
        // tolerates more dots) but the convention is one token.
        assert_eq!(cfg().subject(), "sie.health.worker-l4-0");
    }

    #[test]
    fn authority_health_requires_every_positive_prerequisite() {
        for missing in ["none", "backend", "consumer", "hash", "ready", "terminated"] {
            let c = cfg();
            c.execution_authority_v1
                .store(missing != "backend", Ordering::Release);
            c.authority_consumer_ready
                .store(missing != "consumer", Ordering::Release);
            if missing == "hash" {
                c.bundle_config_hash.write().unwrap().clear();
            }
            let payload: serde_json::Value = serde_json::from_slice(
                &encode_payload(&c, missing != "ready", missing == "terminated").unwrap(),
            )
            .unwrap();
            assert_eq!(
                payload["supports_execution_authority_v1"],
                missing == "none",
                "{missing}"
            );
        }
    }

    #[test]
    fn numerical_admission_health_requires_authority_the_backend_method_and_its_consumer() {
        for (authority, backend, consumer) in [
            (true, true, true),
            (true, false, true),
            (false, true, true),
            (true, true, false),
        ] {
            let c = cfg();
            c.execution_authority_v1.store(authority, Ordering::Release);
            c.authority_consumer_ready.store(true, Ordering::Release);
            c.numerical_admission_v1.store(backend, Ordering::Release);
            c.admission_consumer_ready
                .store(consumer, Ordering::Release);
            let payload: serde_json::Value =
                serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
            assert_eq!(
                payload["supports_numerical_admission_v1"],
                authority && backend,
                "{authority} {backend} {consumer}"
            );
            assert_eq!(
                payload["supports_numerical_admission_subject_v1"],
                authority && backend && consumer,
                "{authority} {backend} {consumer}"
            );
        }
    }

    #[test]
    fn payload_roundtrips_to_gateway_shape() {
        // Serialises to the exact field set
        // `WorkerStatusMessage::deserialize` reads — every
        // load-bearing field present, nothing extra.
        let c = cfg();
        let json = {
            let hash = c.bundle_config_hash.read().unwrap();
            let payload = WorkerStatusPayload {
                name: &c.worker_id,
                ready: true,
                terminated: false,
                supports_execution_authority_v1: false,
                supports_numerical_admission_v1: false,
                supports_numerical_admission_subject_v1: false,
                gpu_count: c.gpu_count,
                total_gpu_slots: 1,
                ready_gpu_slots: 1,
                machine_profile: &c.machine_profile,
                pool_name: &c.pool_name,
                bundle: &c.bundle,
                bundle_config_hash: hash.as_str(),
                queue_depth: 0,
                pending_cost: 0,
                inflight_batches: 0,
                saturated: false,
                loaded_models: &[],
                loading_models: &[],
                unsupported_models: &[],
                numerical_process_inventory: None,
            };
            serde_json::to_value(&payload).unwrap()
        };
        assert_eq!(json["name"], "worker-l4-0");
        assert_eq!(json["ready"], true);
        assert_eq!(json["terminated"], false);
        assert_eq!(json["gpu_count"], 1);
        assert_eq!(json["total_gpu_slots"], 1);
        assert_eq!(json["ready_gpu_slots"], 1);
        assert_eq!(json["queue_depth"], 0);
        assert_eq!(json["pending_cost"], 0);
        assert_eq!(json["inflight_batches"], 0);
        assert_eq!(json["saturated"], false);
        assert_eq!(json["machine_profile"], "l4");
        assert_eq!(json["pool_name"], "l4");
        assert_eq!(json["bundle"], "default");
        assert_eq!(json["bundle_config_hash"], "hash-abc");
        assert_eq!(json["loaded_models"], serde_json::json!([]));
    }

    #[test]
    fn payload_reads_updated_bundle_config_hash() {
        let c = cfg();
        {
            let mut hash = c.bundle_config_hash.write().unwrap();
            *hash = "hash-next".into();
        }
        let json = {
            let hash = c.bundle_config_hash.read().unwrap();
            let payload = WorkerStatusPayload {
                name: &c.worker_id,
                ready: true,
                terminated: false,
                supports_execution_authority_v1: false,
                supports_numerical_admission_v1: false,
                supports_numerical_admission_subject_v1: false,
                gpu_count: c.gpu_count,
                total_gpu_slots: 1,
                ready_gpu_slots: 1,
                machine_profile: &c.machine_profile,
                pool_name: &c.pool_name,
                bundle: &c.bundle,
                bundle_config_hash: hash.as_str(),
                queue_depth: 0,
                pending_cost: 0,
                inflight_batches: 0,
                saturated: false,
                loaded_models: &[],
                loading_models: &[],
                unsupported_models: &[],
                numerical_process_inventory: None,
            };
            serde_json::to_value(&payload).unwrap()
        };
        assert_eq!(json["bundle_config_hash"], "hash-next");
    }

    #[test]
    fn payload_reads_updated_loaded_models() {
        let c = cfg();
        {
            let mut models = c.loaded_models.write().unwrap();
            *models = vec!["model/a".into(), "model/b".into()];
        }
        let json: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(
            json["loaded_models"],
            serde_json::json!(["model/a", "model/b"])
        );
        assert_eq!(json["loading_models"], serde_json::json!([]));
    }

    #[test]
    fn payload_reads_updated_loading_models() {
        let c = cfg();
        {
            let mut models = c.loading_models.write().unwrap();
            *models = vec!["org/loading".into()];
        }
        let json: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(json["loading_models"], serde_json::json!(["org/loading"]));
        assert_eq!(json["loaded_models"], serde_json::json!([]));
    }

    #[test]
    fn payload_fields_match_the_worker_status_wire_fixture() {
        let fixture: serde_json::Value =
            serde_json::from_str(include_str!("../../wire-fixtures/worker_status.json"))
                .expect("worker_status fixture parses");
        let fields: std::collections::BTreeSet<String> = fixture["fields"]
            .as_array()
            .expect("fields")
            .iter()
            .map(|field| field.as_str().expect("field name").to_string())
            .collect();
        let omitted_when_empty: std::collections::BTreeSet<String> = fixture["omitted_when_empty"]
            .as_object()
            .expect("omitted_when_empty")
            .keys()
            .cloned()
            .collect();
        let keys = |json: &serde_json::Value| -> std::collections::BTreeSet<String> {
            json.as_object()
                .expect("payload object")
                .keys()
                .cloned()
                .collect()
        };

        let c = cfg();
        let steady: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(
            keys(&steady),
            fields.difference(&omitted_when_empty).cloned().collect()
        );

        let unsupported: Vec<String> =
            serde_json::from_value(fixture["example"]["unsupported_models"].clone())
                .expect("example unsupported_models");
        *c.unsupported_models.write().unwrap() = unsupported;
        let partial: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(keys(&partial), fields);
        assert_eq!(
            partial["unsupported_models"],
            fixture["example"]["unsupported_models"]
        );
    }

    #[test]
    fn tombstone_payload_marks_not_ready_and_terminated() {
        let c = cfg();
        let json: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, false, true).unwrap()).unwrap();

        assert_eq!(json["name"], "worker-l4-0");
        assert_eq!(json["ready"], false);
        assert_eq!(json["terminated"], true);
        assert_eq!(json["gpu_count"], 1);
        assert_eq!(json["total_gpu_slots"], 1);
        assert_eq!(json["ready_gpu_slots"], 0);
        assert_eq!(json["machine_profile"], "l4");
        assert_eq!(json["pool_name"], "l4");
        assert_eq!(json["bundle"], "default");
        assert_eq!(json["bundle_config_hash"], "hash-abc");
        assert_eq!(json["loaded_models"], serde_json::json!([]));
    }

    #[test]
    fn payload_includes_runtime_pressure_metrics() {
        let c = cfg();
        c.runtime_state.worker_gpu_slots_ready.set(1);
        c.runtime_state.worker_queue_depth.set(1_000_000);
        c.runtime_state.worker_pending_cost.set(1234);
        c.runtime_state.inflight_batches.set(2);

        let json: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();

        assert_eq!(json["queue_depth"], 1_000_000);
        assert_eq!(json["pending_cost"], 1234);
        assert_eq!(json["inflight_batches"], 2);
        assert_eq!(json["saturated"], true);
    }

    #[test]
    fn payload_derives_saturation_from_worker_pressure() {
        let c = cfg();
        c.runtime_state.worker_gpu_slots_ready.set(1);
        c.runtime_state.worker_queue_depth.set(1_000_000);

        let saturated: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(saturated["saturated"], true);

        c.runtime_state.worker_queue_depth.set(0);
        let cleared: serde_json::Value =
            serde_json::from_slice(&encode_payload(&c, true, false).unwrap()).unwrap();
        assert_eq!(cleared["saturated"], false);
    }

    #[test]
    fn payload_uses_local_readiness_for_ready_flag() {
        // Heartbeat must reflect /readyz state — once draining
        // we publish ready=false so the gateway transitions us
        // to Unhealthy ASAP rather than waiting for the 30 s
        // heartbeat_timeout to elapse.
        let r = Readiness::new(2_000, 3);
        r.record_ping_success();
        assert!(r.snapshot().is_ready());

        r.mark_draining();
        assert!(!r.snapshot().is_ready());
    }
}