feat(#2290): split ensure_runtime — dirs to lifecycle, listeners to mcp_sockets supervisor

- lifecycle::ensure_agent_runtime_dir(name): pure filesystem op, no
  Coordinator dep. Creates /run/hyperhive/agents/<name> without touching
  the MCP listener map.

- workers/mcp_sockets::spawn_poll(coord): 10 s reconcile loop (same shape
  as agent_sockets::spawn_poll). Converges 'agent running => MCP listener
  bound'. First tick is immediate so hive-c0re restarts re-register all
  running agents without waiting a full interval. Fixes the dead-listener-
  after-daemon-restart gap.

- All ensure_runtime() call sites updated:
  - Prebuild/Swap/WriteDropin: Coordinator::agent_dir() (pure, no IO)
  - Reconcile-Start: ensure_agent_runtime_dir + agent_dir (dir may be
    missing after reboot; listener deferred to supervisor)
  - run_create / handle_spawn: ensure_agent_runtime_dir + register_agent
    (eager on first spawn so socket ready before harness first turn)
  - apply_commit / merge_config_pr: ensure_agent_runtime_dir + agent_dir
  - Manager (auto_update): ensure_agent_runtime_dir + agent_dir
    (manager has no MCP listener; socket_server::start_manager owns it)

- ensure_runtime() retained in Coordinator with updated doc pointing at
  the preferred split form. No callers remain outside tests.
This commit is contained in:
atlas 2026-07-08 23:27:48 +02:00
commit 3d919b596f
10 changed files with 147 additions and 30 deletions

View file

@ -153,7 +153,10 @@ pub async fn ensure_root_agent(coord: &Arc<Coordinator>) -> Result<()> {
return Ok(());
}
tracing::info!("manager container missing — spawning");
let runtime = coord.ensure_runtime(MANAGER_NAME)?;
lifecycle::ensure_agent_runtime_dir(MANAGER_NAME)?;
// Manager has no MCP listener (socket_server::start_manager owns its
// socket); just need the dir + path value.
let runtime = Coordinator::agent_dir(MANAGER_NAME);
let hive = coord.hive_env();
let paths = Coordinator::agent_paths(MANAGER_NAME, runtime);
lifecycle::spawn(MANAGER_NAME, &hive, &paths).await?;

View file

@ -0,0 +1,59 @@
//! MCP socket listener reconcile loop.
//!
//! Periodically checks that every running agent container has a bound
//! MCP listener registered in the `Coordinator`. Any agent that is running
//! but whose listener has gone (e.g. after a hive-c0re restart that cleared
//! `/run/hyperhive/agents/`) gets re-registered automatically.
//!
//! Same shape as `agent_sockets::spawn_poll` — a simple 10 s tick loop that
//! converges "agent container running ⇒ MCP listener bound". The self-healing
//! guarantee means the start path no longer needs to call `register_agent`
//! directly (though spawn still does for eagerness); callers only need
//! `lifecycle::ensure_agent_runtime_dir` to create the bind-mount source.
use std::sync::Arc;
use crate::coordinator::Coordinator;
/// Spawn the MCP socket listener reconcile loop.
///
/// Every 10 s the loop lists running agent containers and calls
/// `register_agent` for any that lack a bound listener. The first tick fires
/// immediately so hive-c0re restarts re-register all running agents without
/// waiting a full interval.
pub fn spawn_poll(coord: Arc<Coordinator>) {
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
loop {
interval.tick().await;
reconcile_once(&coord).await;
}
});
}
async fn reconcile_once(coord: &Arc<Coordinator>) {
let running = match crate::lifecycle::list().await {
Ok(names) => names,
Err(e) => {
tracing::debug!(error = ?e, "mcp_sockets poll: failed to list agents");
return;
}
};
let registered = coord.list_agents();
for container in running {
let Some(name) = container.strip_prefix(crate::lifecycle::AGENT_PREFIX) else {
continue;
};
if !registered.contains(&name.to_owned()) {
if let Err(e) = coord.register_agent(name) {
tracing::warn!(
agent = %name,
error = ?e,
"mcp_sockets poll: register_agent failed"
);
} else {
tracing::debug!(agent = %name, "mcp_sockets poll: registered missing listener");
}
}
}
}

View file

@ -1,12 +1,13 @@
//! Background tasks and periodic sweeps: crash/login watcher, the
//! reminder and scheduled-prompt delivery loops, boot-time auto-update
//! reconcile, the agent-sockets.json writer loop, and knowledge-repo
//! sync. Each submodule is re-exported at the crate root, so
//! `crate::crash_watch::…` etc. keep working unchanged.
//! reconcile, the agent-sockets.json writer loop, the MCP socket listener
//! reconcile loop, and knowledge-repo sync. Each submodule is re-exported
//! at the crate root, so `crate::crash_watch::…` etc. keep working unchanged.
pub mod agent_sockets;
pub mod auto_update;
pub mod crash_watch;
pub mod knowledge;
pub mod mcp_sockets;
pub mod reminder_scheduler;
pub mod scheduled_prompts_worker;