Skip to content

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

fix(agent): sweep on the build path, and log the sweep once\n\n`start_boot_once_jobs` runs from `serve()`, so an embedder that only calls\n`CoreBuilder::build()` and then `invoke()` never reached the sweep — and\n`openhuman.agent_runs_active` is dispatchable the moment `build()` returns, so\nexactly that caller kept reading the previous process's graveyard. The sweep\nmoves into `build()`, which every runtime goes through.\n\nLogging: the reaper and its caller each emitted an info line for the same\ncount, and the reaper announced a sweep on every clean boot. One info line now,\nonly when something was reaped.\n\nTests: `a_build_only_runtime_is_swept_before_it_can_be_invoked` seeds a running\nrun, builds with `ServiceSet::none()` + `DomainSet::harness()`, and asserts the\nrun is cancelled — taking `TEST_ENV_LOCK` because `OPENHUMAN_WORKSPACE` is\nprocess-global, and asserting the build resolved the workspace it seeded so the\ntest cannot pass by sweeping somewhere else. reaper 3 green.\n\nClaude-Session: https://claude.ai/code/session_01SRSNnqQsokuGmkbpLoLCGy\n

yh928bfb18832bf83
diff --git a/src/core/runtime/builder.rs b/src/core/runtime/builder.rs
index f69a0cbcb0..599d155a6e 100644
--- a/src/core/runtime/builder.rs
+++ b/src/core/runtime/builder.rs
@@ -602,6 +602,20 @@ impl CoreBuilder {
)
.await?;
+ // Reap agent runs orphaned by a previous process (crash / restart /
+ // deploy). Here, and not with the other boot-once jobs, because those
+ // run from `serve()`: an embedder that only calls `build()` and then
+ // `invoke()` never reaches them, and `openhuman.agent_runs_active` is
+ // dispatchable the moment this returns. The core is a single in-process
+ // runtime, so a run left Pending/Running/Interrupted in the durable
+ // status store has no executor to advance it and would be listed as
+ // active forever. Best-effort — a store that cannot be read logs and
+ // reaps nothing rather than failing the build.
+ if let Some(cfg) = config.as_ref() {
+ crate::openhuman::agent::tinyagents::reaper::reap_orphaned_runs(&cfg.workspace_dir)
+ .await;
+ }
+
Ok(CoreRuntime {
ctx,
config,
diff --git a/src/core/runtime/services.rs b/src/core/runtime/services.rs
index 129f1038f5..ad5a5e5d0c 100644
--- a/src/core/runtime/services.rs
+++ b/src/core/runtime/services.rs
@@ -354,17 +354,9 @@ pub fn start_bootstrap_jobs(services: ServiceSet, _config: &Config) {
pub async fn start_boot_once_jobs(services: ServiceSet, config: &Config) {
run_legacy_migrations(config).await;
- // Reap agent runs orphaned by a previous process (crash / restart / deploy).
- // The core is a single in-process runtime, so any run still
- // Pending/Running/Interrupted in the durable status store has no executor
- // left to advance it and would otherwise linger in `agent_runs_active`
- // indefinitely. Runs before readiness (regardless of ServiceSet) so the
- // first active-runs read reflects reality. Best-effort — never blocks boot.
- let reaped =
- crate::openhuman::tinyagents::reaper::reap_orphaned_runs(&config.workspace_dir).await;
- if reaped > 0 {
- log::info!("[runtime] startup run sweep reaped {reaped} orphaned agent run(s)");
- }
+ // The orphaned-run sweep does NOT live here. It runs in
+ // `CoreBuilder::build`, which every runtime goes through — these jobs only
+ // run from `serve()`, so a build-only embedder would never be swept.
if services.harness_init {
let cfg_for_init = config.clone();
diff --git a/src/openhuman/agent/tinyagents/reaper.rs b/src/openhuman/agent/tinyagents/reaper.rs
index d7ab45eb25..88c824c4d4 100644
--- a/src/openhuman/agent/tinyagents/reaper.rs
+++ b/src/openhuman/agent/tinyagents/reaper.rs
@@ -11,7 +11,7 @@
//! On startup we sweep every still-active run to the terminal `Cancelled` state
//! with an explanatory error so the active listing reflects reality. This is
//! the one *writer* over the status seam that
-//! [`crate::openhuman::tinyagents::journal`] exposes; the replay/status
+//! [`crate::openhuman::agent::tinyagents::journal`] exposes; the replay/status
//! controllers ([`super::replay`]) stay strictly read-only.
use std::path::Path;
@@ -21,8 +21,8 @@ use tinyagents::harness::events::HarnessRunStatus;
use tinyagents::harness::ids::{ExecutionStatus, HarnessPhase};
use tinyagents::harness::observability::HarnessStatusStore;
-use crate::openhuman::session_import::ops::open_session_stores;
-use crate::openhuman::tinyagents::journal::FileStatusStore;
+use crate::openhuman::agent::session_import::ops::open_session_stores;
+use crate::openhuman::agent::tinyagents::journal::FileStatusStore;
/// Error recorded on a run reaped by the startup sweep. Stable + grep-friendly
/// so an operator (or a test) can tell a reaped run from a genuinely failed one.
@@ -52,7 +52,7 @@ pub(crate) async fn reap_orphaned_runs(workspace: &Path) -> usize {
log::debug!("[agent] startup run sweep: no orphaned runs");
return 0;
}
- log::info!(
+ log::debug!(
"[agent] startup run sweep: {} orphaned run(s) to reap",
active.len()
);
@@ -71,7 +71,13 @@ pub(crate) async fn reap_orphaned_runs(workspace: &Path) -> usize {
}
}
}
- log::info!("[agent] startup run sweep: reaped {reaped} orphaned run(s)");
+ // One operator-visible line for the whole sweep, and only when it did
+ // something: a clean boot is the normal case and says nothing.
+ if reaped > 0 {
+ log::info!("[agent] startup run sweep: reaped {reaped} orphaned run(s)");
+ } else {
+ log::debug!("[agent] startup run sweep: nothing to reap");
+ }
reaped
}
@@ -99,7 +105,7 @@ mod tests {
use tinyagents::harness::ids::ComponentId;
- use crate::openhuman::tinyagents::journal::mint_run_id;
+ use crate::openhuman::agent::tinyagents::journal::mint_run_id;
/// Build a fresh status in the given non-terminal state and persist it.
async fn seed_status(store: &FileStatusStore, status_kind: ExecutionStatus) -> String {
@@ -178,4 +184,66 @@ mod tests {
assert_eq!(reap_orphaned_runs(&tmp).await, 0);
let _ = std::fs::remove_dir_all(&tmp);
}
+
+ /// The sweep has to happen on the path a **build-only** embedder takes.
+ ///
+ /// `CoreRuntime::invoke` dispatches `openhuman.agent_runs_active` the moment
+ /// `build()` returns, with no transport and no background services in
+ /// between, so a sweep that lived with the other boot-once jobs (which run
+ /// from `serve()`) would leave that caller reading the previous process's
+ /// graveyard. This pins the sweep to the build path with every optional
+ /// service off.
+ #[tokio::test]
+ async fn a_build_only_runtime_is_swept_before_it_can_be_invoked() {
+ use crate::core::runtime::{CoreBuilder, DomainSet, ServiceSet};
+ use crate::core::types::HostKind;
+
+ let tmp = std::env::temp_dir().join(format!("oh-reaper-boot-{}", uuid::Uuid::new_v4()));
+ // `OPENHUMAN_WORKSPACE` names the root; the resolved workspace is the
+ // `workspace` directory under it, which is what the sweep will read.
+ let workspace = tmp.join("workspace");
+ let store = FileStatusStore::new(open_session_stores(&workspace).kv);
+ let orphan = seed_status(&store, ExecutionStatus::Running).await;
+ assert_eq!(store.list_active().await.unwrap().len(), 1);
+
+ // Point the runtime at this workspace, then build it with no transport
+ // and no services — the shape `examples/embed_headless.rs` documents.
+ // `OPENHUMAN_WORKSPACE` is process-global; take the same lock the other
+ // env-mutating tests do so a parallel test cannot read ours.
+ let _env = crate::openhuman::config::TEST_ENV_LOCK
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+ let previous = std::env::var("OPENHUMAN_WORKSPACE").ok();
+ std::env::set_var("OPENHUMAN_WORKSPACE", &tmp);
+ let built = CoreBuilder::new(HostKind::Cli)
+ .services(ServiceSet::none())
+ .domains(DomainSet::harness())
+ .build()
+ .await;
+ match previous {
+ Some(value) => std::env::set_var("OPENHUMAN_WORKSPACE", value),
+ None => std::env::remove_var("OPENHUMAN_WORKSPACE"),
+ }
+ let built = built.expect("a headless build succeeds");
+ assert_eq!(
+ built.context().workspace_dir().ok(),
+ Some(workspace.clone()),
+ "the build must have resolved the workspace this test seeded"
+ );
+
+ let after = store
+ .get_status(&orphan)
+ .await
+ .unwrap()
+ .expect("the seeded run is still readable");
+ assert_eq!(
+ after.status,
+ ExecutionStatus::Cancelled,
+ "build() must reap before any RPC can be dispatched"
+ );
+ assert_eq!(after.error.as_deref(), Some(ORPHAN_REAP_REASON));
+ assert!(store.list_active().await.unwrap().is_empty());
+
+ let _ = std::fs::remove_dir_all(&tmp);
+ }
}