Watch
0
0
Fork
You've already forked hyperhive
0

c0re: keep power intent in memory, drop agent_power/kv tables and the stop snapshot

This commit is contained in:
damocles 2026-11-07 01:22:18 +01:00 • committed by mara
commit b77fa9edc3

View file

@ -21,12 +21,6 @@ use crate::socket_server::{self, AgentSocket};
/// fine, the seq dedupe makes a reconnect resync safe.
const DASHBOARD_CHANNEL: usize = 256;
/// Broker `kv` key under which the broad-stop running-agents snapshot is
/// persisted, so a `hivectl start` after a hive-c0re restart can still
/// restore only the previously-running agents. See
/// `Coordinator::set_last_stopped_running`.
const LAST_STOPPED_RUNNING_KEY: &str = "last_stopped_running";
pub struct Coordinator {
pub broker: Arc<Broker>,
/// Full build-log capture. `lifecycle::run` `start()`s a row per attempt,
@ -107,17 +101,6 @@ pub struct Coordinator {
/// orchestrations that can be in flight for different agents
/// simultaneously.
pause_pending: Mutex<HashSet<String>>,
/// Logical agent names that were running at the last broad-scope
/// `hivectl stop`. A subsequent broad-scope `hivectl start` restores
/// only this set (intersected with the requested scope) rather than
/// every configured container, so agents the operator intentionally
/// left stopped stay stopped. `None` when no broad stop has happened
/// since the last start (or since daemon boot) — start then falls
/// back to "start all". Mirrored to the broker `kv` table
/// (`last_stopped_running` key) so the snapshot also survives a
/// hive-c0re restart between the stop-all and the start: this
/// in-memory copy is the fast path, the persisted copy the backstop.
last_stopped_running: Mutex<Option<Vec<String>>>,
/// Unified wire-facing event channel feeding the dashboard SSE
/// stream. Carries broker messages (mirrored from `broker.subscribe`
/// by the forwarder task in `main.rs`) and dashboard-only mutation
@ -365,7 +348,7 @@ impl Coordinator {
// without us having to thread an `Arc<BuildLogs>` through every public entry
// point in the lifecycle surface.
crate::build_logs::install(build_logs.clone());
let power = Arc::new(crate::power::PowerStore::open(db_path).context("open agent_power")?);
let power = Arc::new(crate::power::PowerStore::default());
let (dashboard_events, _) = broadcast::channel(DASHBOARD_CHANNEL);
let (shutdown_tx, _) = watch::channel(false);
Ok(Self {
@ -382,7 +365,6 @@ impl Coordinator {
recent_transient: Mutex::new(HashMap::new()),
graceful_stop_pending: Mutex::new(HashSet::new()),
pause_pending: Mutex::new(HashSet::new()),
last_stopped_running: Mutex::new(None),
dashboard_events,
event_seq: AtomicU64::new(0),
@ -677,48 +659,6 @@ impl Coordinator {
self.pause_pending.lock().unwrap().remove(name);
}
/// Record the set of agents that were running at a broad-scope
/// `hivectl stop`, so the next broad-scope `start` restores exactly
/// this set. See the `last_stopped_running` field doc. Persists a
/// copy to the broker `kv` table (best-effort) so the snapshot
/// survives a hive-c0re restart; the in-memory copy is the fast path.
pub fn set_last_stopped_running(&self, agents: Vec<String>) {
match serde_json::to_string(&agents) {
Ok(json) => {
if let Err(e) = self.broker.kv_set(LAST_STOPPED_RUNNING_KEY, &json) {
tracing::warn!(error = ?e, "persist last_stopped_running failed (in-memory copy still set)");
}
}
Err(e) => tracing::warn!(error = ?e, "serialise last_stopped_running failed"),
}
*self.last_stopped_running.lock().unwrap() = Some(agents);
}
/// Take (and clear) the recorded broad-stop running set, if any. A
/// broad-scope `start` uses this to restore only the previously
/// running agents; `None` means "no record — start all". Falls back
/// to the persisted `kv` copy when the in-memory snapshot is empty
/// (hive-c0re restarted between the stop-all and the start). Clears
/// the persisted copy either way — the snapshot is one-shot.
pub fn take_last_stopped_running(&self) -> Option<Vec<String>> {
let result = self
.last_stopped_running
.lock()
.unwrap()
.take()
.or_else(|| {
self.broker
.kv_get(LAST_STOPPED_RUNNING_KEY)
.ok()
.flatten()
.and_then(|json| serde_json::from_str::<Vec<String>>(&json).ok())
});
if let Err(e) = self.broker.kv_delete(LAST_STOPPED_RUNNING_KEY) {
tracing::warn!(error = ?e, "clear persisted last_stopped_running failed");
}
result
}
/// Per-agent `deliberate_stop` for transients cleared within the last
/// `grace` seconds — i.e. agents the operator just acted on, whose stop the
/// crash watcher should NOT classify as a crash. Lazily reaps entries older