From 044ff5523c3d4f695a13e9ff8385145c5dccfb7f Mon Sep 17 00:00:00 2001 From: damocles Date: Mon, 1 Jun 2026 13:43:02 +0200 Subject: [PATCH] fix: call matrix::sync_agent on rebuild, mirrors startup sweep --- hive-c0re/src/auto_update.rs | 6 ++++++ hive-c0re/src/matrix.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/hive-c0re/src/auto_update.rs b/hive-c0re/src/auto_update.rs index 295532d4..012e2863 100644 --- a/hive-c0re/src/auto_update.rs +++ b/hive-c0re/src/auto_update.rs @@ -119,6 +119,12 @@ pub async fn rebuild_agent( // (e.g. first-spawn seeding failed transiently) without // requiring a full hive-c0re restart. crate::forge::sync_agent(name, crate::forge::core_token().as_deref()).await; + // Mirror the matrix side of the startup sweep: if hive-matrix + // is present, ensure this agent has a registered user + token. + // Idempotent (skips if token file already exists). Keeps the + // rebuild path equivalent to the startup sweep for newly-spawned + // agents that missed ensure_all(). + crate::matrix::sync_agent_standalone(name).await; // Wake the agent on its next turn so claude sees a // "you were rebuilt — check /state/ for notes, --continue // session intact" hint. Covers dashboard rebuild, admin diff --git a/hive-c0re/src/matrix.rs b/hive-c0re/src/matrix.rs index 678e9707..a0b31240 100644 --- a/hive-c0re/src/matrix.rs +++ b/hive-c0re/src/matrix.rs @@ -280,6 +280,35 @@ pub async fn sync_agent(client: &reqwest::Client, name: &str, register_token: &s } } +/// Standalone per-agent sync that handles its own setup: checks if +/// hive-matrix is present, reads the registration token, and builds +/// an HTTP client before delegating to [`sync_agent`]. Mirrors the +/// setup in [`ensure_all`] so the rebuild path and the startup sweep +/// stay equivalent. No-op when the matrix container is absent. +pub async fn sync_agent_standalone(name: &str) { + if !is_present().await { + return; + } + let register_token = match ensure_register_token() { + Ok(t) => t, + Err(e) => { + tracing::warn!(%name, error = ?e, "matrix: ensure_register_token failed"); + return; + } + }; + let client = match reqwest::Client::builder() + .timeout(std::time::Duration::from_secs(HTTP_TIMEOUT_SECS)) + .build() + { + Ok(c) => c, + Err(e) => { + tracing::warn!(%name, error = ?e, "matrix: build HTTP client failed"); + return; + } + }; + sync_agent(&client, name, ®ister_token).await; +} + /// Sweep every existing container (manager + sub-agents) and ensure /// each has a matrix user + token on the local homeserver. Called once /// at hive-c0re startup, alongside `forge::ensure_all`. No-op when the