drop the sqlite event log nothing reads, and the comments naming routes that are gone
This commit is contained in:
parent
6289b8d64b
commit
cbda9c162f
1 changed files with 71 additions and 305 deletions
|
|
@ -8,13 +8,11 @@
|
|||
//! future events; the dashboard JS deals with the cold-start case by
|
||||
//! showing "connecting…" until the first event arrives.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
use chrono::Utc;
|
||||
use hive_claude::TokenUsage;
|
||||
use rusqlite::{Connection, params};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swarm_wire::agent_inference::Source;
|
||||
use tokio::sync::broadcast;
|
||||
|
|
@ -25,38 +23,15 @@ use crate::harness_state::{
|
|||
};
|
||||
|
||||
const CHANNEL_CAPACITY: usize = 256;
|
||||
/// Path to the persisted event db. Overridable via `HYPERHIVE_EVENTS_DB`
|
||||
/// for dev / tests; otherwise derived from the agent's harness dir.
|
||||
fn events_db_path() -> PathBuf {
|
||||
std::env::var_os("HYPERHIVE_EVENTS_DB").map_or_else(
|
||||
|| crate::paths::harness_dir().join("hyperhive-events.sqlite"),
|
||||
PathBuf::from,
|
||||
)
|
||||
}
|
||||
|
||||
const SCHEMA: &str = "
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ts INTEGER NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
payload_json TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_events_ts ON events (ts);
|
||||
";
|
||||
|
||||
/// Envelope carried over the broadcast channel: the `LiveEvent` itself
|
||||
/// plus a monotonic per-process seq stamped by `Bus::emit`. SSE consumers
|
||||
/// serialize this directly (seq becomes a sibling of the `kind` tag);
|
||||
/// clients use seq to dedupe their buffered live traffic against the
|
||||
/// snapshot/history responses (drop anything with `seq <= snapshot.seq`).
|
||||
/// serialize this directly (seq becomes a sibling of the `kind` tag).
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct BusEvent {
|
||||
pub seq: u64,
|
||||
/// Unix seconds at emit time. Serialized as a sibling of the `kind`
|
||||
/// tag so the agent terminal can render turn start/end times (and
|
||||
/// turn duration) on the live stream; history rows carry the same
|
||||
/// `ts` field sourced from the persisted `events.ts` column, so the
|
||||
/// renderer reads `ts` identically for live + scrollback.
|
||||
/// turn duration) on the live stream.
|
||||
pub ts: i64,
|
||||
#[serde(flatten)]
|
||||
pub event: LiveEvent,
|
||||
|
|
@ -86,9 +61,9 @@ pub enum LiveEvent {
|
|||
/// enums can't flatten a tag onto a primitive newtype, and serde
|
||||
/// fails serialization at runtime — silently, because the SSE
|
||||
/// handler's `filter_map(... .ok()? ...)` swallows the error, so a
|
||||
/// regression here goes unnoticed until the sqlite history shows
|
||||
/// `"null"` rows. The web UI's `note` renderer already reads
|
||||
/// `ev.text`, so the wire shape matches without a JS change.
|
||||
/// regression here goes unnoticed until a reader sees
|
||||
/// `"null"` rows. The `note` renderer reads `ev.text`, so the wire shape
|
||||
/// matches without a JS change.
|
||||
Note { text: String },
|
||||
/// Turn finished. `ok=false` means claude exited non-zero or the
|
||||
/// harness hit a transport error.
|
||||
|
|
@ -121,54 +96,8 @@ pub enum LiveEvent {
|
|||
TurnStateChanged { state: TurnState, since_unix: i64 },
|
||||
}
|
||||
|
||||
/// sqlite-backed event log. Wraps a `Connection` behind a `Mutex` so the
|
||||
/// `Bus` (which clones cheaply) shares one writer.
|
||||
struct EventStore {
|
||||
conn: Mutex<Connection>,
|
||||
}
|
||||
|
||||
impl EventStore {
|
||||
fn open(path: &Path) -> rusqlite::Result<Self> {
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
let conn = Connection::open(path)?;
|
||||
crate::sqlite_writer::configure_wal(&conn)?;
|
||||
conn.execute_batch(SCHEMA)?;
|
||||
Ok(Self {
|
||||
conn: Mutex::new(conn),
|
||||
})
|
||||
}
|
||||
|
||||
fn append(&self, event: &LiveEvent) -> rusqlite::Result<()> {
|
||||
let ts = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()
|
||||
.and_then(|d| i64::try_from(d.as_secs()).ok())
|
||||
.unwrap_or(0);
|
||||
let kind = match event {
|
||||
LiveEvent::TurnStart { .. } => "turn_start",
|
||||
LiveEvent::Stream(_) => "stream",
|
||||
LiveEvent::Note { .. } => "note",
|
||||
LiveEvent::TurnEnd { .. } => "turn_end",
|
||||
LiveEvent::StatusChanged { .. } => "status_changed",
|
||||
LiveEvent::ModelChanged { .. } => "model_changed",
|
||||
LiveEvent::EffortChanged { .. } => "effort_changed",
|
||||
LiveEvent::TokenUsageChanged { .. } => "token_usage_changed",
|
||||
LiveEvent::TurnStateChanged { .. } => "turn_state_changed",
|
||||
};
|
||||
let payload = serde_json::to_string(event).unwrap_or_else(|_| "null".into());
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO events (ts, kind, payload_json) VALUES (?1, ?2, ?3)",
|
||||
params![ts, kind, payload],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Authoritative turn-loop state. The harness owns it; the web UI
|
||||
/// reads via `/api/state` and renders. Lives alongside the bus
|
||||
/// is published to the swarm queue for the swarm UI. Lives alongside the bus
|
||||
/// because everyone who has a `Bus` already has the right handle to
|
||||
/// poke the state on transitions.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
|
|
@ -196,13 +125,7 @@ pub struct Bus {
|
|||
/// Monotonic per-process counter stamped onto every `BusEvent`.
|
||||
/// Persisted nowhere — a harness restart resets seq to 0; clients
|
||||
/// always treat reconnect as "fresh state, fresh stream of seqs."
|
||||
/// Historical events served from sqlite carry no seq (they predate
|
||||
/// the live channel the seq is meant to dedupe against).
|
||||
event_seq: Arc<AtomicU64>,
|
||||
/// Persistent event log. `None` only if opening the sqlite db failed
|
||||
/// at construction — we keep going so the harness doesn't die on a
|
||||
/// missing state dir mount in dev / test scenarios.
|
||||
store: Option<Arc<EventStore>>,
|
||||
/// Current turn-loop state + since-when (unix seconds).
|
||||
state: Arc<Mutex<(TurnState, i64)>>,
|
||||
/// Model name passed to `claude --model`. Default `haiku`; a value the
|
||||
|
|
@ -229,8 +152,8 @@ pub struct Bus {
|
|||
/// `hyperhive-harness.json` (see `harness_state`), so the host can
|
||||
/// surface the status on the dashboard without a live socket call.
|
||||
rate_limited: Arc<AtomicBool>,
|
||||
/// One-shot: operator asked to reset the claude session (`POST
|
||||
/// /api/new-session`). Consumed at the *next* turn boundary by
|
||||
/// One-shot: operator asked to reset the claude session (the swarm
|
||||
/// UI's `/clear`). Consumed at the *next* turn boundary by
|
||||
/// `turn::drive_turn`, which archives the current session so the turn
|
||||
/// starts fresh. Deferred (not applied in the handler) so the archive
|
||||
/// never races a claude process mid-write — one claude per container,
|
||||
|
|
@ -258,7 +181,7 @@ pub struct Bus {
|
|||
post_compact_wake: Arc<Mutex<Option<String>>>,
|
||||
/// Stops the turn in flight, for a runtime that offers it (ACP). Set once
|
||||
/// by the serve loop when it builds the session; unset on claude, whose
|
||||
/// turn `/api/cancel` stops by signalling the `claude` process.
|
||||
/// turn the swarm UI's cancel stops by signalling the `claude` process.
|
||||
turn_canceller: Arc<OnceLock<hive_runtime::Canceller>>,
|
||||
/// Current fresh-claude-session id (FK to `sessions.id`). Set by the
|
||||
/// bin loop after minting a session row on a fresh start; stamped onto
|
||||
|
|
@ -306,18 +229,10 @@ pub struct Bus {
|
|||
}
|
||||
|
||||
impl Bus {
|
||||
/// Open the events db (path from `events_db_path()`). On failure, fall back
|
||||
/// to a no-store bus — the harness still works, just without persistent history.
|
||||
/// A bus with no subscribers and the model, effort and rate-limit state the harness
|
||||
/// last recorded.
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
let path = events_db_path();
|
||||
let store = match EventStore::open(&path) {
|
||||
Ok(s) => Some(Arc::new(s)),
|
||||
Err(e) => {
|
||||
tracing::warn!(error = ?e, path = %path.display(), "events db open failed; running without history");
|
||||
None
|
||||
}
|
||||
};
|
||||
let (tx, _) = broadcast::channel(CHANNEL_CAPACITY);
|
||||
let (initial_model, model_source) = local_model(configured_model());
|
||||
let (initial_effort, effort_source) = local_effort(configured_effort());
|
||||
|
|
@ -328,7 +243,6 @@ impl Bus {
|
|||
Self {
|
||||
tx: Arc::new(tx),
|
||||
event_seq: Arc::new(AtomicU64::new(0)),
|
||||
store,
|
||||
state: Arc::new(Mutex::new((TurnState::Idle, Utc::now().timestamp()))),
|
||||
model: Arc::new(Mutex::new(initial_model)),
|
||||
effort: Arc::new(Mutex::new(initial_effort)),
|
||||
|
|
@ -368,7 +282,7 @@ impl Bus {
|
|||
self.turn_canceller.get()
|
||||
}
|
||||
|
||||
/// Request a session reset (operator `POST /api/new-session`). Deferred:
|
||||
/// Request a session reset (the swarm UI's `/clear`). Deferred:
|
||||
/// the flag is consumed at the next turn boundary by `drive_turn`, which
|
||||
/// archives the current session so no claude process is mid-write when the
|
||||
/// file is renamed. Idempotent — two clicks before the next turn still
|
||||
|
|
@ -766,10 +680,9 @@ impl Bus {
|
|||
}
|
||||
|
||||
/// Broadcast a status flip (online / `needs_login_idle` / `rate_limited`).
|
||||
/// Called by the bin entry points + `turn::wait_for_login` + the
|
||||
/// `/api/logout` handler — every site that mutates the
|
||||
/// `Arc<Mutex<LoginState>>` should also call this so the web UI
|
||||
/// drops its periodic /api/state poll while a turn loop is running.
|
||||
/// Called by the bin entry points + `turn::wait_for_login` + the login
|
||||
/// watch — every site that mutates the `Arc<Mutex<LoginState>>` should
|
||||
/// also call this so the status the swarm reads follows it.
|
||||
///
|
||||
/// `hyperhive-harness.json` persists across harness restarts so the
|
||||
/// host-side dashboard can render the status without a live socket call:
|
||||
|
|
@ -830,11 +743,6 @@ impl Bus {
|
|||
}
|
||||
|
||||
pub fn emit(&self, event: LiveEvent) {
|
||||
if let Some(store) = &self.store
|
||||
&& let Err(e) = store.append(&event)
|
||||
{
|
||||
tracing::warn!(error = ?e, "events: append failed");
|
||||
}
|
||||
let envelope = BusEvent {
|
||||
seq: self.next_seq(),
|
||||
ts: Utc::now().timestamp(),
|
||||
|
|
@ -902,8 +810,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn bus_event_serializes_ts_and_seq_beside_kind() {
|
||||
// Live SSE frame: same `ts` sibling as history (plus `seq`), so the
|
||||
// renderer is path-agnostic between live + scrollback.
|
||||
// Live SSE frame: `ts` and `seq` are siblings of `kind`.
|
||||
let v = serde_json::to_value(BusEvent {
|
||||
seq: 7,
|
||||
ts: 1_700_000_000,
|
||||
|
|
|
|||
Loading…
Reference in a new issue