refactor: single now_unix in hive_sh4re::wire_time

replaces 15 per-module copies (now_unix/now_secs) across hive-c0re and
hive-ag3nt; wire_time already owns the epoch-seconds convention
This commit is contained in:
müde 2026-07-06 21:58:32 +02:00
commit c84028ddcf
16 changed files with 45 additions and 125 deletions

View file

@ -16,6 +16,7 @@ use hive_claude::TokenUsage;
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use hive_sh4re::wire_time::now_unix;
const CHANNEL_CAPACITY: usize = 256;
/// Max `LiveEvent`s the `Bus` returns from `history()` and keeps in
@ -213,13 +214,6 @@ pub fn write_forge_cursor<S: std::hash::BuildHasher>(
write_harness_json(&v);
}
fn now_unix() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.and_then(|d| i64::try_from(d.as_secs()).ok())
.unwrap_or(0)
}
const SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS events (

View file

@ -7,6 +7,7 @@ use crate::events::Bus;
use crate::mcp::REDELIVERY_HINT;
use crate::turn::{TurnError, TurnOutcome};
use crate::turn_stats::TurnStatRow;
pub use hive_sh4re::wire_time::now_unix;
/// Assemble the per-turn wake prompt string. The role/tools/etc. live in the
/// system prompt; this is just the wake signal body. `id` is the broker row
@ -46,13 +47,6 @@ pub fn format_wake_prompt(
/// Current time as a Unix timestamp (seconds). Returns 0 on any error.
#[must_use]
pub fn now_unix() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.and_then(|d| i64::try_from(d.as_secs()).ok())
.unwrap_or(0)
}
/// Field-named args for [`build_row`]. Mirrors the turn-stats row
/// columns; `outcome` and `bus` borrow for the duration of the call.

View file

@ -15,6 +15,7 @@ use rusqlite::{Connection, OpenFlags};
use serde::Serialize;
use hive_sh4re::ReminderStats;
use hive_sh4re::wire_time::now_unix;
/// Window param accepted by `/api/stats?window=`. Each maps to a
/// total span + the bucket width used to roll up trend series.
@ -208,7 +209,7 @@ fn default_path() -> PathBuf {
}
fn empty_snapshot(window: Window) -> Snapshot {
let now = now_secs();
let now = now_unix();
let from = now - window.span_secs();
let buckets = fill_buckets(from, now, window.bucket_secs(), &HashMap::new());
Snapshot {
@ -239,7 +240,7 @@ fn snapshot(path: &Path, window: Window) -> Result<Snapshot> {
// matches hive-c0re's host-side reader (`hive_stats::read_agent`).
conn.busy_timeout(std::time::Duration::from_millis(500))
.with_context(|| format!("set busy_timeout on {}", path.display()))?;
let now = now_secs();
let now = now_unix();
// Fixed windows look back a constant span; `all` starts at the earliest
// recorded turn (`MIN(started_at)`, falling back to `now` on an empty
// table) and sizes its buckets adaptively from that span.
@ -567,11 +568,6 @@ fn u64_from_i64(v: i64) -> u64 {
u64::try_from(v).unwrap_or(0)
}
fn now_secs() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX))
}
#[cfg(test)]
mod tests {
@ -638,7 +634,7 @@ mod tests {
fn snapshot_aggregates_rows() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
let now = now_secs();
let now = now_unix();
seed_db(
&db,
&[
@ -727,7 +723,7 @@ mod tests {
fn bash_breakdown_empty_without_table() {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(&db, &[(now_secs() - 100, 1000, "opus", "recv", "ok", "{}")]);
seed_db(&db, &[(now_unix() - 100, 1000, "opus", "recv", "ok", "{}")]);
let s = snapshot(&db, Window::Day).unwrap();
assert!(s.bash_breakdown.is_empty());
}
@ -739,7 +735,7 @@ mod tests {
let db = tmp_db();
let _ = std::fs::remove_file(&db);
seed_db(&db, &[]);
let now = now_secs();
let now = now_unix();
let conn = Connection::open(&db).unwrap();
conn.execute_batch("CREATE TABLE bash_commands (ts INTEGER NOT NULL, head TEXT NOT NULL);")
.unwrap();

View file

@ -15,9 +15,10 @@
//! the honest fix is to clean them up where they live.
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;
use rusqlite::{Connection, Result, params};
use hive_sh4re::wire_time::now_unix;
/// How often the sweep runs.
const VACUUM_INTERVAL: Duration = Duration::from_hours(1);
@ -131,10 +132,3 @@ fn vacuum_events(path: &Path) -> Result<u64> {
Ok(u64::try_from(removed).unwrap_or(0))
}
fn now_unix() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.and_then(|d| i64::try_from(d.as_secs()).ok())
.unwrap_or(0)
}