hex/src/history.rs
Dax b9ba1eb4a2
One hex control CLI and protocol on every OS (#544)
* One hex control CLI and protocol on every OS

One command tree (control::cli) on macOS, Linux, and Windows, carried by
each OS's owner-only transport: the macOS local API, the Linux socket's
Request::Control, and a new Windows named pipe (windows_control) that only
this user's SID can open and whose server the client verifies. Linux and
Windows share one host (control::host) through thin adapters; what an OS
can't do answers unsupported at runtime.

Windows gains dictation (with stop --print), paste-last, quit, restart,
start, a live status, and every annotate command; Linux and Windows gain
settings, model, window, vocabulary, history clear, check-for-updates,
launch-at-login, onboarding reset, logs, commands init|check, and annotate
status. hex status reports the OS's capabilities, settings name the
capability they need (set refuses, list marks), and a parity test requires
a recorded reason for every capability that differs across OSes.

* Windows control pipe: always hold an unconnected instance; check the client's user

* Shared control: meetings, history, and mcp on every OS now that the meetings store does
2026-10-30 22:41:39 -04:00

913 lines
32 KiB
Rust

//! Retained dictation history: an owner-only, bounded, crash-safe store of
//! successful dictation results.
//!
//! History is a product record, deliberately separate from the diagnostic
//! Activity event stream. Entries hold text and bounded metadata only; audio
//! is never retained here. Every retention choice remains subject to hard
//! entry and byte caps, writes are atomic, and files are owner-only.
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex, TryLockError, mpsc};
use std::thread;
use std::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
const VERSION: u32 = 1;
const MAX_ENTRIES: usize = 10_000;
const MAX_TEXT_BYTES: usize = 16 * 1024;
const MAX_LABEL_BYTES: usize = 256;
const MAX_FALLBACK_BYTES: usize = 1_024;
const MAX_TOTAL_TEXT_BYTES: usize = 16 * 1024 * 1024;
const PRUNE_INTERVAL: Duration = Duration::from_secs(60);
pub const MAX_SEARCH_RESULTS: usize = 200;
/// How long successful results remain in retained history.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum HistoryRetention {
Off,
Day,
#[default]
Week,
Month,
Forever,
}
impl HistoryRetention {
pub const ALL: [Self; 5] = [Self::Off, Self::Day, Self::Week, Self::Month, Self::Forever];
pub const fn label(self) -> &'static str {
match self {
Self::Off => "Off",
Self::Day => "24 hours",
Self::Week => "7 days",
Self::Month => "30 days",
Self::Forever => "Forever",
}
}
pub const fn is_off(self) -> bool {
matches!(self, Self::Off)
}
pub const fn max_age_ms(self) -> Option<u64> {
const HOUR_MS: u64 = 60 * 60 * 1_000;
match self {
Self::Off | Self::Forever => None,
Self::Day => Some(24 * HOUR_MS),
Self::Week => Some(7 * 24 * HOUR_MS),
Self::Month => Some(30 * 24 * HOUR_MS),
}
}
}
/// Which successful output produced an entry.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum HistoryKind {
Dictation,
Send,
VoiceAction,
/// `hex dictation stop --print`: never pasted, so Paste Last skips it.
Printed,
}
impl HistoryKind {
pub const fn label(self) -> &'static str {
match self {
Self::Dictation => "Dictation",
Self::Send => "Send",
Self::VoiceAction => "Voice Action",
Self::Printed => "Printed",
}
}
}
/// Bounded record of the post-processing that shaped the final text.
pub type HistoryProcessing = crate::events::DictationProcessing;
/// One retained successful result.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct HistoryEntry {
pub id: u64,
pub timestamp_ms: u64,
pub kind: HistoryKind,
/// Corrected local transcript before mode processing.
pub raw_text: String,
/// Text that was actually inserted.
pub final_text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub application: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub processing: Option<HistoryProcessing>,
#[serde(default)]
pub audio_ms: u64,
#[serde(default)]
pub inference_ms: u64,
#[serde(default)]
pub total_ms: u64,
}
impl HistoryEntry {
fn text_bytes(&self) -> usize {
self.raw_text.len() + self.final_text.len()
}
fn matches(&self, needle: &str) -> bool {
let matches_field = |field: &str| field.to_lowercase().contains(needle);
matches_field(&self.raw_text)
|| matches_field(&self.final_text)
|| self.application.as_deref().is_some_and(matches_field)
|| self
.processing
.as_ref()
.is_some_and(|processing| matches_field(&processing.profile))
}
}
/// A successful result awaiting a history identity.
#[derive(Clone, Debug)]
pub struct HistoryDraft {
pub kind: HistoryKind,
pub raw_text: String,
pub final_text: String,
pub application: Option<String>,
pub processing: Option<HistoryProcessing>,
pub audio_ms: u64,
pub inference_ms: u64,
pub total_ms: u64,
}
#[derive(Serialize)]
struct SavedHistory<'a> {
version: u32,
next_id: u64,
entries: &'a [HistoryEntry],
}
#[derive(Deserialize)]
struct LoadedHistory {
version: u32,
next_id: u64,
entries: Vec<HistoryEntry>,
}
/// Read one atomic file snapshot without recovery, pruning writes, or workers.
/// Opening once pins the old or new inode while the app replaces the file.
pub fn read_snapshot(path: &std::path::Path) -> io::Result<Vec<HistoryEntry>> {
use std::io::Read;
// Text can expand sixfold when JSON-escaped, plus bounded metadata.
const MAX_FILE_BYTES: u64 = (MAX_TOTAL_TEXT_BYTES * 6
+ MAX_ENTRIES * (6 * (2 * MAX_LABEL_BYTES + MAX_FALLBACK_BYTES) + 1_024))
as u64;
let file = match fs::File::open(path) {
Ok(file) => file,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(error) => return Err(error),
};
let mut bytes = Vec::new();
file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?;
if bytes.len() as u64 > MAX_FILE_BYTES {
return Err(io::Error::other("history file exceeds the read limit"));
}
let loaded: LoadedHistory = serde_json::from_slice(&bytes).map_err(io::Error::other)?;
if loaded.version != VERSION {
return Err(io::Error::other("unsupported history version"));
}
Ok(loaded.entries)
}
/// Owner-only bounded history store. Entries are held oldest-first.
pub struct HistoryStore {
path: PathBuf,
entries: Vec<HistoryEntry>,
next_id: u64,
retention: HistoryRetention,
prune_pending: bool,
prune_error_reported: bool,
/// The file as this store last read or wrote it. Linux runs the
/// listener and the History pane in separate processes, so each store
/// rereads the file when another process changed it.
file_state: Option<(SystemTime, u64)>,
}
fn file_state(path: &std::path::Path) -> Option<(SystemTime, u64)> {
let metadata = fs::metadata(path).ok()?;
Some((metadata.modified().ok()?, metadata.len()))
}
impl HistoryStore {
/// Open the store, recover from unreadable content, and prune expired
/// entries. A malformed file is preserved beside the store instead of
/// being silently destroyed.
pub fn open(path: PathBuf, retention: HistoryRetention, now_ms: u64) -> Self {
let mut store = Self {
path,
entries: Vec::new(),
next_id: 1,
retention,
prune_pending: false,
prune_error_reported: false,
file_state: None,
};
store.file_state = file_state(&store.path);
match fs::read(&store.path) {
Ok(bytes) => match serde_json::from_slice::<LoadedHistory>(&bytes) {
Ok(loaded) if loaded.version == VERSION => {
let max_entry_id = loaded.entries.iter().map(|entry| entry.id).max();
store.next_id = loaded
.next_id
.max(max_entry_id.map_or(1, |id| id.saturating_add(1)));
store.entries = loaded.entries;
store.entries.sort_by_key(|entry| entry.id);
}
Ok(loaded) => {
tracing::warn!(version = loaded.version, "unsupported history version");
store.preserve_corrupt();
}
Err(error) => {
tracing::warn!(%error, path = %store.path.display(), "history file is malformed");
store.preserve_corrupt();
}
},
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => {
tracing::warn!(%error, path = %store.path.display(), "could not read history");
}
}
store.expire(now_ms);
store
}
/// Rereads the file if another process wrote it since this store last
/// read or wrote it. An unreadable file keeps the entries in memory.
pub fn sync(&mut self) {
let current = file_state(&self.path);
if current == self.file_state {
return;
}
self.file_state = current;
let loaded = match fs::read(&self.path) {
Ok(bytes) => serde_json::from_slice::<LoadedHistory>(&bytes)
.ok()
.filter(|loaded| loaded.version == VERSION),
Err(error) if error.kind() == io::ErrorKind::NotFound => Some(LoadedHistory {
version: VERSION,
next_id: self.next_id,
entries: Vec::new(),
}),
Err(_) => None,
};
let Some(loaded) = loaded else {
tracing::warn!(path = %self.path.display(), "could not reread the changed history");
return;
};
let max_entry_id = loaded.entries.iter().map(|entry| entry.id).max();
self.next_id = self
.next_id
.max(loaded.next_id)
.max(max_entry_id.map_or(1, |id| id.saturating_add(1)));
self.entries = loaded.entries;
self.entries.sort_by_key(|entry| entry.id);
}
/// Record one successful result. Returns the stable entry ID, or `None`
/// while retention is off.
pub fn record(&mut self, draft: HistoryDraft, now_ms: u64) -> io::Result<Option<u64>> {
if self.retention.is_off() {
return Ok(None);
}
let id = self.next_id;
self.next_id = self.next_id.saturating_add(1);
self.entries.push(HistoryEntry {
id,
timestamp_ms: now_ms,
kind: draft.kind,
raw_text: truncated(&draft.raw_text, MAX_TEXT_BYTES),
final_text: truncated(&draft.final_text, MAX_TEXT_BYTES),
application: draft
.application
.map(|application| truncated(&application, MAX_LABEL_BYTES)),
processing: draft.processing.map(|processing| HistoryProcessing {
profile: truncated(&processing.profile, MAX_LABEL_BYTES),
latency_ms: processing.latency_ms,
fallback: processing
.fallback
.map(|fallback| truncated(&fallback, MAX_FALLBACK_BYTES)),
}),
audio_ms: draft.audio_ms,
inference_ms: draft.inference_ms,
total_ms: draft.total_ms,
});
self.prune(now_ms);
self.persist()?;
Ok(Some(id))
}
/// Delete one entry. Returns whether it existed.
pub fn delete(&mut self, id: u64) -> io::Result<bool> {
let before = self.entries.len();
self.entries.retain(|entry| entry.id != id);
if self.entries.len() == before {
return Ok(false);
}
self.persist()?;
Ok(true)
}
/// Delete every entry.
pub fn clear(&mut self) -> io::Result<()> {
if self.entries.is_empty() {
return Ok(());
}
self.entries.clear();
self.persist()
}
/// Change retention and immediately prune to the new window. Turning
/// history off stops recording but keeps existing entries until they are
/// deleted explicitly.
pub fn set_retention(&mut self, retention: HistoryRetention, now_ms: u64) -> io::Result<()> {
if self.retention == retention {
return Ok(());
}
self.retention = retention;
if self.prune(now_ms) {
self.persist()?;
}
Ok(())
}
/// All entries, newest first.
pub fn entries(&self) -> impl Iterator<Item = &HistoryEntry> {
self.entries.iter().rev()
}
#[cfg(test)]
pub fn entry(&self, id: u64) -> Option<&HistoryEntry> {
self.entries.iter().find(|entry| entry.id == id)
}
#[cfg(test)]
pub fn len(&self) -> usize {
self.entries.len()
}
/// Case-insensitive substring search over text, application, and
/// processing profile. Newest first and bounded.
pub fn search(&self, query: &str, now_ms: u64) -> Vec<&HistoryEntry> {
let needle = query.trim().to_lowercase();
let cutoff = self
.retention
.max_age_ms()
.map(|max_age| now_ms.saturating_sub(max_age));
self.entries()
.filter(|entry| cutoff.is_none_or(|cutoff| entry.timestamp_ms >= cutoff))
.filter(|entry| needle.is_empty() || entry.matches(&needle))
.take(MAX_SEARCH_RESULTS)
.collect()
}
fn expire(&mut self, now_ms: u64) {
self.prune(now_ms);
if self.prune_pending
&& let Err(error) = self.persist()
{
if !self.prune_error_reported {
tracing::warn!(%error, "could not persist pruned history; will retry");
}
self.prune_error_reported = true;
}
}
fn prune(&mut self, now_ms: u64) -> bool {
let mut changed = false;
if let Some(max_age) = self.retention.max_age_ms() {
let cutoff = now_ms.saturating_sub(max_age);
let before = self.entries.len();
self.entries.retain(|entry| entry.timestamp_ms >= cutoff);
changed |= self.entries.len() != before;
}
if self.entries.len() > MAX_ENTRIES {
self.entries.drain(..self.entries.len() - MAX_ENTRIES);
changed = true;
}
let mut total: usize = self.entries.iter().map(HistoryEntry::text_bytes).sum();
let mut remove = 0;
while total > MAX_TOTAL_TEXT_BYTES && remove + 1 < self.entries.len() {
total -= self.entries[remove].text_bytes();
remove += 1;
}
self.entries.drain(..remove);
changed |= remove > 0;
self.prune_pending |= changed;
changed
}
fn persist(&mut self) -> io::Result<()> {
let saved = SavedHistory {
version: VERSION,
next_id: self.next_id,
entries: &self.entries,
};
let json = serde_json::to_vec(&saved).map_err(io::Error::other)?;
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)?;
}
let temporary = self.path.with_extension("json.tmp");
{
let mut file = fs::File::create(&temporary)?;
restrict_to_owner(&file)?;
file.write_all(&json)?;
file.sync_all()?;
}
fs::rename(&temporary, &self.path)?;
self.file_state = file_state(&self.path);
self.prune_pending = false;
self.prune_error_reported = false;
Ok(())
}
fn preserve_corrupt(&self) {
let backup = self.path.with_extension("json.corrupt");
if let Err(error) = fs::rename(&self.path, &backup) {
tracing::warn!(%error, "could not preserve the malformed history file");
}
}
}
/// Thread-safe shared handle over one history store.
#[derive(Clone)]
pub struct History {
store: Arc<Mutex<HistoryStore>>,
// Only handles own senders: dropping the last clone wakes and stops the worker.
_shutdown: mpsc::Sender<()>,
}
impl History {
pub fn new(store: HistoryStore) -> Self {
Self::with_clock(store, PRUNE_INTERVAL, now_ms)
}
fn with_clock(
store: HistoryStore,
interval: Duration,
clock: impl Fn() -> u64 + Send + 'static,
) -> Self {
let store = Arc::new(Mutex::new(store));
let (shutdown, stopped) = mpsc::channel();
let worker_store = Arc::clone(&store);
if let Err(error) = thread::Builder::new()
.name("history-retention".into())
.spawn(move || {
while let Err(mpsc::RecvTimeoutError::Timeout) = stopped.recv_timeout(interval) {
let mut store = match worker_store.try_lock() {
Ok(store) => store,
Err(TryLockError::WouldBlock) => continue,
Err(TryLockError::Poisoned(error)) => error.into_inner(),
};
store.expire(clock());
}
})
{
tracing::warn!(%error, "could not start history retention worker");
}
Self {
store,
_shutdown: shutdown,
}
}
/// Open the default store in Application Support.
pub fn open_default(retention: HistoryRetention) -> color_eyre::Result<Self> {
let path = crate::app_paths::support_dir()?.join("history.json");
Ok(Self::new(HistoryStore::open(path, retention, now_ms())))
}
pub fn record(&self, draft: HistoryDraft) -> io::Result<Option<u64>> {
self.synced().record(draft, now_ms())
}
pub fn delete(&self, id: u64) -> io::Result<bool> {
self.synced().delete(id)
}
pub fn clear(&self) -> io::Result<()> {
self.synced().clear()
}
pub fn set_retention(&self, retention: HistoryRetention) -> io::Result<()> {
self.synced().set_retention(retention, now_ms())
}
/// Bounded snapshot of matching entries, newest first.
pub fn search(&self, query: &str) -> Vec<HistoryEntry> {
self.synced()
.search(query, now_ms())
.into_iter()
.cloned()
.collect()
}
fn synced(&self) -> std::sync::MutexGuard<'_, HistoryStore> {
let mut store = self.store.lock().unwrap_or_else(|error| error.into_inner());
store.sync();
store
}
}
pub use crate::events::now_ms;
fn truncated(text: &str, max_bytes: usize) -> String {
text[..text.floor_char_boundary(max_bytes)].to_string()
}
#[cfg(unix)]
fn restrict_to_owner(file: &fs::File) -> io::Result<()> {
use std::os::unix::fs::PermissionsExt;
file.set_permissions(fs::Permissions::from_mode(0o600))
}
#[cfg(not(unix))]
fn restrict_to_owner(_file: &fs::File) -> io::Result<()> {
Ok(())
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use std::time::UNIX_EPOCH;
fn temp_path(name: &str) -> PathBuf {
let directory =
std::env::temp_dir().join(format!("hex-history-{}-{name}", std::process::id()));
let _ = fs::remove_dir_all(&directory);
fs::create_dir_all(&directory).unwrap();
directory.join("history.json")
}
fn draft(text: &str) -> HistoryDraft {
HistoryDraft {
kind: HistoryKind::Dictation,
raw_text: format!("raw {text}"),
final_text: text.to_string(),
application: Some("Zed".into()),
processing: Some(HistoryProcessing {
profile: "Messages".into(),
latency_ms: 120,
fallback: None,
}),
audio_ms: 900,
inference_ms: 80,
total_ms: 1_100,
}
}
#[test]
fn stores_in_two_processes_see_each_others_writes() {
let path = temp_path("two-processes");
let mut listener = HistoryStore::open(path.clone(), HistoryRetention::Week, 1_000);
let mut window = HistoryStore::open(path.clone(), HistoryRetention::Week, 1_000);
let first = listener.record(draft("first"), 1_000).unwrap().unwrap();
window.sync();
assert!(window.entry(first).is_some());
window.delete(first).unwrap();
listener.sync();
let second = listener.record(draft("second"), 2_000).unwrap().unwrap();
assert!(second > first);
window.sync();
assert!(window.entry(first).is_none(), "a deletion is never undone");
assert!(window.entry(second).is_some());
}
#[test]
fn records_survive_reopen_with_monotonic_ids() {
let path = temp_path("reopen");
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Week, 1_000);
let first = store.record(draft("first"), 1_000).unwrap().unwrap();
let second = store.record(draft("second"), 2_000).unwrap().unwrap();
assert!(second > first);
store.delete(second).unwrap();
let mut reopened = HistoryStore::open(path, HistoryRetention::Week, 2_500);
assert_eq!(reopened.len(), 1);
let third = reopened.record(draft("third"), 3_000).unwrap().unwrap();
assert!(third > second, "deleted IDs are never reused");
let texts: Vec<_> = reopened
.entries()
.map(|entry| entry.final_text.as_str())
.collect();
assert_eq!(texts, ["third", "first"]);
}
#[test]
fn retention_prunes_expired_entries_on_open_and_write() {
let path = temp_path("retention");
let day_ms = 24 * 60 * 60 * 1_000;
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Day, 0);
store.record(draft("old"), 1_000).unwrap();
store.record(draft("fresh"), day_ms).unwrap();
// A write one day later prunes the first entry exactly at the cutoff.
store.record(draft("newest"), day_ms + 1_001).unwrap();
let texts: Vec<_> = store
.entries()
.map(|entry| entry.final_text.as_str())
.collect();
assert_eq!(texts, ["newest", "fresh"]);
let reopened = HistoryStore::open(path, HistoryRetention::Day, 2 * day_ms + 500);
let texts: Vec<_> = reopened
.entries()
.map(|entry| entry.final_text.as_str())
.collect();
assert_eq!(texts, ["newest"]);
}
#[test]
fn search_hides_expired_entries_before_the_next_idle_prune() {
let path = temp_path("search-expiry");
let day_ms = HistoryRetention::Day.max_age_ms().unwrap();
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Day, 0);
store.record(draft("old"), 1_000).unwrap();
store.record(draft("fresh"), 2_000).unwrap();
fs::OpenOptions::new()
.write(true)
.open(&path)
.unwrap()
.set_modified(UNIX_EPOCH)
.unwrap();
assert_eq!(store.search("", day_ms + 1_000).len(), 2);
assert!(store.search("old", day_ms + 1_001).is_empty());
let matches = store.search("", day_ms + 1_001);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].final_text, "fresh");
assert_eq!(
store.len(),
2,
"reads filter without writing on the UI thread"
);
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), UNIX_EPOCH);
}
#[test]
fn idle_worker_expires_entries_on_disk_without_recording_or_reading() {
use std::sync::atomic::{AtomicU64, Ordering};
let path = temp_path("idle-expiry");
let day_ms = HistoryRetention::Day.max_age_ms().unwrap();
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Day, 0);
store.record(draft("old"), 1_000).unwrap();
store.record(draft("fresh"), 2_000).unwrap();
let clock = Arc::new(AtomicU64::new(2_000));
let worker_clock = Arc::clone(&clock);
let (ticks, ticked) = mpsc::channel();
let history = History::with_clock(store, Duration::from_millis(10), move || {
let now = worker_clock.load(Ordering::SeqCst);
let _ = ticks.send(now);
now
});
let tick = |now| {
clock.store(now, Ordering::SeqCst);
while ticked.recv_timeout(Duration::from_secs(2)).unwrap() != now {}
// The clock is sampled under the lock; acquiring it waits for that prune.
drop(history.synced());
};
fs::OpenOptions::new()
.write(true)
.open(&path)
.unwrap()
.set_modified(UNIX_EPOCH)
.unwrap();
tick(day_ms + 1_000);
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), UNIX_EPOCH);
tick(day_ms + 1_001);
let saved: LoadedHistory = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
assert_eq!(saved.entries.len(), 1);
assert_eq!(saved.entries[0].final_text, "fresh");
tick(day_ms + 2_001);
let saved: LoadedHistory = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
assert!(saved.entries.is_empty());
fs::OpenOptions::new()
.write(true)
.open(&path)
.unwrap()
.set_modified(UNIX_EPOCH)
.unwrap();
tick(2 * day_ms);
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), UNIX_EPOCH);
}
#[test]
fn failed_expiry_persistence_retries_without_new_removals() {
let path = temp_path("expiry-retry");
let day_ms = HistoryRetention::Day.max_age_ms().unwrap();
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Day, 0);
store.record(draft("old"), 1_000).unwrap();
let temporary = path.with_extension("json.tmp");
fs::create_dir(&temporary).unwrap();
store.expire(day_ms + 1_001);
assert!(store.entries.is_empty());
assert!(store.prune_pending);
assert!(store.prune_error_reported);
let saved: LoadedHistory = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
assert_eq!(saved.entries.len(), 1);
store.expire(day_ms + 1_002);
assert!(store.prune_pending);
assert!(store.prune_error_reported);
fs::remove_dir(temporary).unwrap();
store.expire(day_ms + 1_003);
assert!(!store.prune_pending);
assert!(!store.prune_error_reported);
let saved: LoadedHistory = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
assert!(saved.entries.is_empty());
}
#[test]
fn idle_worker_stops_when_the_last_history_handle_is_dropped() {
let path = temp_path("worker-lifecycle");
let store = HistoryStore::open(path, HistoryRetention::Day, 0);
let (alive, stopped) = mpsc::channel::<()>();
let history = History::with_clock(store, Duration::from_secs(24 * 60 * 60), move || {
let _alive = &alive;
0
});
let clone = history.clone();
drop(history);
assert_eq!(stopped.try_recv(), Err(mpsc::TryRecvError::Empty));
drop(clone);
assert_eq!(
stopped.recv_timeout(Duration::from_secs(2)),
Err(mpsc::RecvTimeoutError::Disconnected),
"shutdown must wake the worker rather than wait for its timer"
);
}
#[test]
fn choosing_week_retention_from_month_keeps_entries_newer_than_a_week() {
let day_ms = 24 * 60 * 60 * 1_000;
let now = 30 * day_ms;
let path = temp_path("month-to-week");
let mut store = HistoryStore::open(path, HistoryRetention::Month, 0);
store
.record(draft("twenty days old"), now - 20 * day_ms)
.unwrap();
store
.record(draft("two days old"), now - 2 * day_ms)
.unwrap();
store.set_retention(HistoryRetention::Week, now).unwrap();
let texts: Vec<_> = store
.entries()
.map(|entry| entry.final_text.as_str())
.collect();
assert_eq!(texts, ["two days old"]);
}
#[test]
fn off_records_nothing_but_preserves_existing_entries() {
let path = temp_path("off");
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Week, 1_000);
store.record(draft("kept"), 1_000).unwrap();
store.set_retention(HistoryRetention::Off, 1_500).unwrap();
fs::OpenOptions::new()
.write(true)
.open(&path)
.unwrap()
.set_modified(UNIX_EPOCH)
.unwrap();
assert_eq!(store.record(draft("dropped"), 2_000).unwrap(), None);
let later = HistoryRetention::Month.max_age_ms().unwrap() + 2_000;
store.expire(later);
assert_eq!(store.len(), 1);
assert_eq!(store.search("kept", later).len(), 1);
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), UNIX_EPOCH);
}
#[test]
fn hard_caps_bound_entries_and_text_regardless_of_retention() {
let path = temp_path("caps");
let mut store = HistoryStore::open(path, HistoryRetention::Forever, 0);
store.record(draft("entry"), 0).unwrap();
store.entries = (0..MAX_ENTRIES + 5)
.map(|id| HistoryEntry {
id: id as u64,
..store.entries[0].clone()
})
.collect();
store.prune(0);
assert_eq!(store.len(), MAX_ENTRIES);
assert_eq!(store.entries[0].id, 5, "the oldest entries are evicted");
store.next_id = (MAX_ENTRIES + 5) as u64;
let oversized = "x".repeat(MAX_TEXT_BYTES + 100);
let id = store.record(draft(&oversized), 9_999_999).unwrap().unwrap();
assert_eq!(store.entry(id).unwrap().final_text.len(), MAX_TEXT_BYTES);
}
#[test]
fn total_text_bytes_evict_oldest_entries() {
let path = temp_path("total-bytes");
let mut store = HistoryStore::open(path, HistoryRetention::Forever, 0);
let big = "y".repeat(MAX_TEXT_BYTES - 10);
let per_entry = 2 * big.len() + 8;
let fits = MAX_TOTAL_TEXT_BYTES / per_entry;
store.record(draft(&big), 0).unwrap();
store.entries = (0..fits + 3)
.map(|id| HistoryEntry {
id: id as u64,
..store.entries[0].clone()
})
.collect();
store.prune(0);
let total: usize = store
.entries()
.map(|entry| entry.raw_text.len() + entry.final_text.len())
.sum();
assert!(total <= MAX_TOTAL_TEXT_BYTES);
assert!(store.len() < fits + 3);
assert!(
store.entries[0].id > 0,
"byte pressure also evicts oldest first"
);
}
#[test]
fn search_is_case_insensitive_across_text_application_and_profile() {
let path = temp_path("search");
let mut store = HistoryStore::open(path, HistoryRetention::Week, 0);
store.record(draft("Hello World"), 1_000).unwrap();
store.record(draft("other text"), 2_000).unwrap();
assert_eq!(store.search("hello world", 2_000).len(), 1);
assert_eq!(store.search("zed", 2_000).len(), 2);
assert_eq!(store.search("messages", 2_000).len(), 2);
assert_eq!(store.search("absent", 2_000).len(), 0);
assert_eq!(
store.search(" ", 2_000).len(),
2,
"blank queries list everything"
);
}
#[test]
fn clear_removes_everything_durably() {
let path = temp_path("clear");
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Week, 0);
store.record(draft("one"), 1_000).unwrap();
store.record(draft("two"), 2_000).unwrap();
store.clear().unwrap();
assert_eq!(store.len(), 0);
let reopened = HistoryStore::open(path, HistoryRetention::Week, 3_000);
assert_eq!(reopened.len(), 0);
}
#[test]
fn malformed_files_are_preserved_and_recovered_from() {
let path = temp_path("malformed");
fs::write(&path, b"{ not json").unwrap();
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Week, 0);
assert_eq!(store.len(), 0);
assert!(path.with_extension("json.corrupt").exists());
store.record(draft("fresh"), 1_000).unwrap();
let reopened = HistoryStore::open(path, HistoryRetention::Week, 2_000);
assert_eq!(reopened.len(), 1);
}
#[cfg(unix)]
#[test]
fn history_files_are_owner_only() {
use std::os::unix::fs::PermissionsExt;
let path = temp_path("permissions");
let mut store = HistoryStore::open(path.clone(), HistoryRetention::Week, 0);
store.record(draft("private"), 1_000).unwrap();
let mode = fs::metadata(&path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
#[test]
fn truncation_respects_character_boundaries() {
let text = "é".repeat(MAX_TEXT_BYTES);
let truncated = truncated(&text, MAX_TEXT_BYTES);
assert!(truncated.len() <= MAX_TEXT_BYTES);
assert!(text.starts_with(&truncated));
}
}