Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

7.5 KB
Raw Blame

health

In-process health registry for the OpenHuman core. Tracks per-component liveness (status, last-ok / last-error timestamps, restart counts) plus process metadata (PID, uptime), exposes a snapshot over JSON-RPC/CLI, and keeps itself current by subscribing to system/channel domain events on the global event bus. State is purely in-memory (a process-global OnceLock registry): nothing is persisted to disk. It also serves static system info (version/OS/arch/PID).

Responsibilities

  • Maintain a process-global registry of named components, each with status, updated_at, last_ok, last_error, restart_count.
  • Provide mutators: mark_component_ok, mark_component_error, bump_component_restart.
  • Produce a point-in-time HealthSnapshot (PID, uptime seconds, components map) and its JSON form.
  • Classify a snapshot into a HealthVerdict (verdict()): a single degraded non-critical component no longer makes the container unhealthy: /health returns 503 only when a critical component (CRITICAL_COMPONENTS = core) is unhealthy; non-critical components (scheduler, channels, update_checker, …) return 200 + a degraded flag (#3312).
  • Drive component state automatically from DomainEvents via an event-bus subscriber.
  • Expose health.snapshot and health.system_info RPC/CLI controllers.

Key files

File Role
crates/openhuman-core/src/platform/health/mod.rs Export-only: declares modules, re-exports core::*, ops::*, pub use ops as rpc, and the controller-schema pair (all_health_controller_schemas / all_health_registered_controllers).
crates/openhuman-core/src/platform/health/core.rs The registry itself: ComponentHealth/HealthSnapshot types, the OnceLock<HealthRegistry> singleton (started_at + Mutex<BTreeMap>), and the mutators/snapshot/snapshot_json functions.
crates/openhuman-core/src/platform/health/ops.rs RPC handler logic returning Outcome<T>: health_snapshot() and system_info() (+ SystemInfo type).
crates/openhuman-core/src/platform/health/schemas.rs Controller schemas + handle_snapshot/handle_system_info async handlers that delegate to ops and serialize via into_cli_compatible_json.
crates/openhuman-core/src/platform/health/bus.rs HealthSubscriber (EventHandler) and register_health_subscriber(); maps domain events to registry mutations.
crates/openhuman-core/src/platform/health/tools.rs Read-only, default-on LLM tools health_snapshot / health_system_info, re-exported through crates/openhuman-core/src/tools/mod.rs.

Public surface

From core.rs (re-exported via pub use core::*):

  • Types: ComponentHealth, HealthSnapshot, HealthVerdict.
  • Functions: mark_component_ok(component), mark_component_error(component, error), bump_component_restart(component), snapshot() -> HealthSnapshot, snapshot_json() -> serde_json::Value, verdict(&HealthSnapshot) -> HealthVerdict, is_critical_component(name) -> bool.

The HTTP GET /health handler (openhuman_rpc::server::http::health::health_handler) uses verdict() for its status code (200 unless a critical component is unhealthy) and adds healthy / degraded / critical_unhealthy / degraded_components fields alongside the components map in the body. The components map shape is unchanged: the new fields are additive.

From ops.rs (re-exported via pub use ops::*, also aliased pub use ops as rpc):

  • health_snapshot() -> Outcome<serde_json::Value>, system_info() -> Outcome<SystemInfo>, and the SystemInfo struct.

From bus.rs: HealthSubscriber, register_health_subscriber().

From schemas.rs (re-exported under aliased names): all_health_controller_schemas, all_health_registered_controllers.

RPC / controllers

Two controllers in the health namespace:

Method Inputs Outputs
openhuman.health_snapshot none snapshot (JSON): full serialized HealthSnapshot.
openhuman.health_system_info none version, os, arch, pid.

system_info's version is CARGO_PKG_VERSION; os/arch come from std::env::consts. Legacy callers may send openhuman.system_info, which the alias table rewrites to health_system_info before dispatch.

Events

Subscriber HealthSubscriber (name = "health::registry") registered via register_health_subscriber() on the global event bus, filtered to the system and channel domains. It reacts to:

DomainEvent Action
SystemStartup { component } mark_component_ok(component)
HealthChanged { component, healthy, message } OK if healthy, else mark_component_error with message (default "unknown health error")
HealthRestarted { component } bump_component_restart(component)
ChannelConnected { channel } mark_component_ok("channel:<channel>")
ChannelDisconnected { channel, reason } mark_component_error("channel:<channel>", reason)

It only subscribes; it does not publish events.

Persistence

None on disk. State lives in a process-global OnceLock<HealthRegistry> (lazy-initialized) holding a Mutex<BTreeMap<String, ComponentHealth>> and an Instant start time. Cleared on process exit; uptime resets each launch.

Dependencies

  • crate::core::bus::BUS.subscribe (crate::core::events::DomainEvent, tinybus::SubscriptionHandle): to receive system/channel events.
  • crate::core::all (ControllerFuture, RegisteredController) and crate::core::{ControllerSchema, FieldSchema, TypeSchema}: controller registry wiring.
  • crate::core::Outcome: RPC handler return contract.
  • External crates: chrono (RFC3339 timestamps), parking_lot::Mutex, serde/serde_json, async_trait.

Used by

  • crates/openhuman-core/src/core/all.rs: registers all_health_* controllers into the registry.
  • crates/openhuman-rpc/src/server/http/health.rs: references health (snapshot/system_info surface).
  • crates/openhuman-core/src/channels/runtime/{startup,supervision}.rs and crates/openhuman-core/src/channels/tests/health.rs: channel runtime updates component health.
  • crates/openhuman-core/src/cron/scheduler.rs, crates/openhuman-core/src/platform/update/scheduler.rs: emit/consume health signals.

Notes / gotchas

  • The registry is a global singleton; tests use UUID-suffixed component names to avoid cross-test contention rather than resetting shared state.
  • upsert_component creates entries lazily with initial status "starting" and always refreshes updated_at after the update closure.
  • mark_component_ok clears last_error; mark_component_error leaves last_ok intact (so the last-known-good time survives a failure).
  • restart_count uses saturating_add (won't overflow).
  • The system_info schema declares pid as TypeSchema::U64, matching SystemInfo.pid: u32, which serializes as a JSON number. It declared TypeSchema::String until #6074; the wire value has always been a number, so that fix moved only the declaration: and with it the generated frontend types and the model-facing tool output_schema.
  • bus.rs short-circuits double registration via a OnceLock and warns (does not panic) if the bus isn't initialized.

Further reading