use std::collections::BTreeMap;
use std::fmt;
use std::path::{Path, PathBuf};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::HiveError;
pub(crate) const INSTANCE_KEY_FILE: &str = "instance.key";
pub(crate) const KX_KEY_FILE: &str = "kx.key";
pub(crate) const INSTANCE_FILE: &str = "instance.json";
pub(crate) const LEASE_FILE: &str = "lease.json";
pub(crate) const KEYS_FILE: &str = "keys.json";
pub(crate) const TOKEN_FILE: &str = "token.json";
pub(crate) const ACTIVITY_FILE: &str = "activity.json";
pub(crate) const SHARING_FILE: &str = "sharing.json";
pub(crate) const MATCH_FILE: &str = "vault-match.json";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InstanceRecord {
pub instance_id: String,
pub org_id: String,
pub org_name: String,
pub label: String,
pub url: String,
pub org_salt_id: String,
pub enrolled_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub web_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub org_kind: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub company_name: Option<String>,
}
impl InstanceRecord {
pub fn workspace_label(&self) -> String {
let name = &self.org_name;
match (self.company_name.as_deref(), self.org_kind.as_deref()) {
(Some(company), _) => format!("Company {company} · Team {name}"),
(None, Some("team")) => format!("Team {name}"),
(None, Some("personal")) => format!("Personal ({name})"),
_ => name.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LeaseRecord {
pub envelope: Value,
pub offset_seconds: i64,
pub received_at: String,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TokenRecord {
pub access_token: String,
pub expires_at: i64,
}
impl fmt::Debug for TokenRecord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokenRecord")
.field("access_token", &"<redacted>")
.field("expires_at", &self.expires_at)
.finish()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActivityRecord {
pub last_seen: Option<String>,
#[serde(default)]
pub last_success: BTreeMap<String, String>,
pub last_error: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub locked: Option<String>,
}
#[derive(Debug, Clone)]
pub struct HiveStore {
config: PathBuf,
data: PathBuf,
}
impl HiveStore {
pub fn open() -> Result<Self, HiveError> {
let config = crate::core::paths::config_dir_read_only().map_err(HiveError::Local)?;
let data = crate::core::paths::data_dir().map_err(HiveError::Local)?;
Ok(Self::at(config.join("hive"), data.join("hive")))
}
pub fn at(config_dir: impl Into<PathBuf>, data_dir: impl Into<PathBuf>) -> Self {
Self {
config: config_dir.into(),
data: data_dir.into(),
}
}
pub fn config_dir(&self) -> &Path {
&self.config
}
pub fn data_dir(&self) -> &Path {
&self.data
}
pub(crate) fn read_config_bytes(&self, name: &str) -> Result<Option<Vec<u8>>, HiveError> {
read_optional(&self.config.join(name))
}
pub(crate) fn write_config_bytes(&self, name: &str, bytes: &[u8]) -> Result<(), HiveError> {
write_private(&self.config, name, bytes)
}
pub(crate) fn read_config<T: DeserializeOwned>(
&self,
name: &str,
) -> Result<Option<T>, HiveError> {
read_json(&self.config.join(name))
}
pub(crate) fn write_config<T: Serialize>(
&self,
name: &str,
value: &T,
) -> Result<(), HiveError> {
write_private(&self.config, name, &to_json(value)?)
}
pub(crate) fn remove_config(&self, name: &str) -> Result<(), HiveError> {
remove_optional(&self.config.join(name))
}
pub fn instance(&self) -> Result<Option<InstanceRecord>, HiveError> {
self.read_config(INSTANCE_FILE)
}
pub fn activity(&self) -> ActivityRecord {
read_json(&self.data.join(ACTIVITY_FILE))
.ok()
.flatten()
.unwrap_or_default()
}
pub(crate) fn record_success(&self, channel: &str, when: &str) {
let mut activity = self.activity();
activity.last_seen = Some(when.to_string());
activity
.last_success
.insert(channel.to_string(), when.to_string());
activity.last_error = None;
if let Ok(bytes) = to_json(&activity) {
let _ = write_private(&self.data, ACTIVITY_FILE, &bytes);
}
}
pub(crate) fn record_locked(&self, reason: Option<&str>) {
let mut activity = self.activity();
if activity.locked.as_deref() == reason {
return;
}
activity.locked = reason.map(str::to_string);
if let Ok(bytes) = to_json(&activity) {
let _ = write_private(&self.data, ACTIVITY_FILE, &bytes);
}
}
pub(crate) fn record_error(&self, message: &str) {
let mut activity = self.activity();
activity.last_error = Some(message.to_string());
if let Ok(bytes) = to_json(&activity) {
let _ = write_private(&self.data, ACTIVITY_FILE, &bytes);
}
}
pub(crate) fn may_record(&self) -> bool {
self.config.join(INSTANCE_FILE).is_file()
&& !self.config.join(super::revoked::REVOKED_FILE).is_file()
}
pub(crate) fn recording_allowed(&self, config: &super::config::HiveConfig) -> bool {
config.enabled != Some(false) && config.metrics_upload && self.lease_live()
}
fn lease_live(&self) -> bool {
use lean_ctx_protocol::hive::lease::{HiveLeaseV1, LeaseState};
let Ok(Some(record)) = self.read_config::<LeaseRecord>(LEASE_FILE) else {
return false;
};
let Ok(lease) = serde_json::from_value::<HiveLeaseV1>(record.envelope["lease"].clone())
else {
return false;
};
let service_now =
(chrono::Utc::now() + chrono::Duration::seconds(record.offset_seconds)).fixed_offset();
matches!(
lease.state_at(service_now),
Ok(LeaseState::Valid | LeaseState::Grace)
)
}
pub fn forget(&self, keep_local: bool) -> Result<(), HiveError> {
super::receipts::stop_local_recording(&self.data);
for name in [
TOKEN_FILE,
LEASE_FILE,
KEYS_FILE,
INSTANCE_FILE,
KX_KEY_FILE,
INSTANCE_KEY_FILE,
SHARING_FILE,
MATCH_FILE,
super::revoked::REVOKED_FILE,
super::disconnect::PENDING_FILE,
] {
self.remove_config(name)?;
}
if !keep_local && self.data.exists() {
std::fs::remove_dir_all(&self.data)
.map_err(|e| HiveError::Local(format!("remove {}: {e}", self.data.display())))?;
}
Ok(())
}
}
fn to_json<T: Serialize>(value: &T) -> Result<Vec<u8>, HiveError> {
let mut bytes =
serde_json::to_vec_pretty(value).map_err(|e| HiveError::Local(format!("encode: {e}")))?;
bytes.push(b'\n');
Ok(bytes)
}
fn read_optional(path: &Path) -> Result<Option<Vec<u8>>, HiveError> {
match std::fs::read(path) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(HiveError::Local(format!("read {}: {e}", path.display()))),
}
}
fn read_json<T: DeserializeOwned>(path: &Path) -> Result<Option<T>, HiveError> {
let Some(bytes) = read_optional(path)? else {
return Ok(None);
};
serde_json::from_slice(&bytes)
.map(Some)
.map_err(|e| HiveError::Local(format!("{} is damaged: {e}", path.display())))
}
fn remove_optional(path: &Path) -> Result<(), HiveError> {
match std::fs::remove_file(path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(HiveError::Local(format!("remove {}: {e}", path.display()))),
}
}
pub(crate) fn write_private(dir: &Path, name: &str, bytes: &[u8]) -> Result<(), HiveError> {
private_dir(dir)?;
crate::core::secure_fs::write_secret_file(&dir.join(name), bytes)
.map_err(|e| HiveError::Local(format!("write {name} in {}: {e}", dir.display())))
}
fn private_dir(dir: &Path) -> Result<(), HiveError> {
let local =
|what: &str, e: std::io::Error| HiveError::Local(format!("{what} {}: {e}", dir.display()));
std::fs::create_dir_all(dir).map_err(|e| local("create", e))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))
.map_err(|e| local("restrict", e))?;
}
Ok(())
}
pub(crate) const DAMAGED_KEPT: usize = 3;
pub(crate) fn load_or_quarantine<T: DeserializeOwned>(
path: &Path,
accept: impl FnOnce(&T) -> bool,
) -> Result<Option<T>, HiveError> {
let Some(bytes) = read_optional(path)? else {
return Ok(None);
};
let why = match serde_json::from_slice::<T>(&bytes) {
Ok(value) if accept(&value) => return Ok(Some(value)),
Ok(_) => "a version this build does not know".to_string(),
Err(e) => e.to_string(),
};
let moved = quarantine(path)?;
Err(HiveError::Local(format!(
"{} is damaged ({why}); moved to {}",
path.display(),
moved.display()
)))
}
fn quarantine(path: &Path) -> Result<PathBuf, HiveError> {
let local = |e: std::io::Error| HiveError::Local(format!("quarantine {}: {e}", path.display()));
let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str())) else {
return Err(HiveError::Local(format!("{} has no name", path.display())));
};
let prefix = format!("{name}.damaged-");
let mut copies: Vec<(u64, PathBuf)> = std::fs::read_dir(dir)
.map_err(local)?
.filter_map(Result::ok)
.filter_map(|e| {
let n = e
.file_name()
.to_str()?
.strip_prefix(&prefix)?
.parse()
.ok()?;
Some((n, e.path()))
})
.collect();
copies.sort_unstable();
let next = copies.last().map_or(1, |(n, _)| n + 1);
let moved = dir.join(format!("{prefix}{next}"));
std::fs::rename(path, &moved).map_err(local)?;
copies.push((next, moved.clone()));
let excess = copies.len().saturating_sub(DAMAGED_KEPT);
for (_, old) in copies.drain(..excess) {
let _ = std::fs::remove_file(old);
}
Ok(moved)
}
const STATE_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(2);
pub(crate) struct StateLock(std::fs::File);
impl Drop for StateLock {
fn drop(&mut self) {
let _ = fs2::FileExt::unlock(&self.0);
}
}
pub(crate) fn lock_state(dir: &Path, name: &str) -> Result<StateLock, HiveError> {
lock_state_within(dir, name, STATE_LOCK_WAIT)
}
pub(crate) fn lock_state_within(
dir: &Path,
name: &str,
wait: std::time::Duration,
) -> Result<StateLock, HiveError> {
private_dir(dir)?;
let path = dir.join(format!("{name}.lock"));
let local = |what: &str| HiveError::Local(format!("{what} {}", path.display()));
let mut options = std::fs::OpenOptions::new();
options.read(true).write(true).create(true).truncate(false);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt as _;
options
.mode(0o600)
.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW);
}
let file = options
.open(&path)
.map_err(|e| local(&format!("open ({e})")))?;
crate::core::file_lock::acquire_exclusive_timeout(&file, wait)
.map_err(|e| local(&format!("lock ({e})")))?;
Ok(StateLock(file))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn workspace_label_names_personal_team_and_company() {
let mut r = InstanceRecord {
instance_id: "inst_1".into(),
org_id: "org_1".into(),
org_name: "Platform".into(),
label: "mbp".into(),
url: "https://hive.example".into(),
org_salt_id: "s".into(),
enrolled_at: "2026-10-26T10:00:00Z".into(),
web_url: None,
org_kind: None,
company_name: None,
};
assert_eq!(r.workspace_label(), "Platform");
r.org_kind = Some("team".into());
assert_eq!(r.workspace_label(), "Team Platform");
r.company_name = Some("Acme".into());
assert_eq!(r.workspace_label(), "Company Acme · Team Platform");
r.company_name = None;
r.org_kind = Some("personal".into());
assert_eq!(r.workspace_label(), "Personal (Platform)");
let old: InstanceRecord = serde_json::from_value(serde_json::json!({
"instance_id": "i", "org_id": "o", "org_name": "Acme", "label": "l", "url": "u",
"org_salt_id": "s", "enrolled_at": "t"
}))
.unwrap();
assert_eq!((old.org_kind, old.company_name), (None, None));
}
#[test]
fn private_files_are_atomic_and_owner_only() {
let dir = tempfile::tempdir().unwrap();
let store = HiveStore::at(dir.path().join("config"), dir.path().join("data"));
let token = TokenRecord {
access_token: "atl_secret".into(),
expires_at: 1,
};
store.write_config(TOKEN_FILE, &token).unwrap();
assert_eq!(
store.read_config::<TokenRecord>(TOKEN_FILE).unwrap(),
Some(token.clone())
);
assert!(!format!("{token:?}").contains("atl_secret"));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777;
assert_eq!(mode(store.config_dir()), 0o700);
assert_eq!(mode(&store.config_dir().join(TOKEN_FILE)), 0o600);
}
let leftovers: Vec<_> = std::fs::read_dir(store.config_dir())
.unwrap()
.filter_map(Result::ok)
.filter(|e| e.file_name().to_string_lossy().ends_with(".tmp"))
.collect();
assert!(leftovers.is_empty());
}
#[test]
fn damaged_state_is_moved_aside_and_only_three_copies_stay() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("state.json");
let load = |accept: bool| load_or_quarantine::<Value>(&path, |_| accept);
assert_eq!(load(true).unwrap(), None, "missing");
std::fs::write(&path, b"{\"ok\": 1}").unwrap();
assert_eq!(load(true).unwrap(), Some(serde_json::json!({"ok": 1})));
let error = load(false).unwrap_err().to_string();
assert!(error.contains("damaged-1"), "{error}");
assert!(!path.exists());
for n in 0..4 {
std::fs::write(&path, format!("broken {n}")).unwrap();
assert!(load(true).is_err());
}
let mut kept: Vec<String> = std::fs::read_dir(dir.path())
.unwrap()
.filter_map(|e| e.ok()?.file_name().into_string().ok())
.collect();
kept.sort();
assert_eq!(
kept,
[
"state.json.damaged-3",
"state.json.damaged-4",
"state.json.damaged-5"
]
);
assert_eq!(
std::fs::read(dir.path().join("state.json.damaged-5")).unwrap(),
b"broken 3",
"the newest copy is the last damaged file"
);
}
#[test]
fn a_held_state_lock_is_not_taken_twice() {
let dir = tempfile::tempdir().unwrap();
let held = lock_state(dir.path(), "leases.json").unwrap();
assert!(lock_state_within(dir.path(), "leases.json", std::time::Duration::ZERO).is_err());
drop(held);
assert!(lock_state_within(dir.path(), "leases.json", std::time::Duration::ZERO).is_ok());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let mode = std::fs::metadata(dir.path().join("leases.json.lock"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o600);
}
}
#[test]
fn forget_keeps_local_data_on_request() {
let dir = tempfile::tempdir().unwrap();
let store = HiveStore::at(dir.path().join("config"), dir.path().join("data"));
store
.write_config(TOKEN_FILE, &serde_json::json!({}))
.unwrap();
store.record_success("lease", "2026-10-20T12:00:00Z");
store.forget(true).unwrap();
assert!(store.read_config_bytes(TOKEN_FILE).unwrap().is_none());
assert_eq!(
store.activity().last_seen.as_deref(),
Some("2026-10-20T12:00:00Z")
);
store.forget(false).unwrap();
assert!(store.activity().last_seen.is_none());
}
}