pub(super) const DEFAULT_CONTEXT_WINDOW: usize = 128_000;
pub(super) const PHI_REREAD_ALPHA: f64 = 0.5;
pub(super) const GWT_IGNITION_Z: f64 = 1.5;
pub(super) const GWT_MIN_ENTRIES: usize = 4;
pub(super) fn ledger_path(agent_id: &str) -> Result<std::path::PathBuf, String> {
let dir = crate::core::paths::state_dir()?;
if agent_id == "default" {
Ok(dir.join("context_ledger.json"))
} else {
let ledger_dir = dir.join("ledger");
let safe_id: String = agent_id
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' {
c
} else {
'_'
}
})
.collect();
Ok(ledger_dir.join(format!("{safe_id}.json")))
}
}
pub(super) fn atomic_write_json(path: &std::path::Path, data: &str) {
let _ = crate::config_io::write_atomic(path, data);
}
#[cfg(unix)]
pub(super) fn acquire_ledger_lock(path: &std::path::Path) -> Option<std::fs::File> {
use std::os::unix::io::AsRawFd;
let lock_path = path.with_extension("json.lock");
let file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(&lock_path)
.ok()?;
let fd = file.as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if ret != 0 {
use std::time::{Duration, Instant};
let deadline = Instant::now() + Duration::from_secs(2);
loop {
std::thread::sleep(Duration::from_millis(50));
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if ret == 0 {
break;
}
if Instant::now() >= deadline {
return None;
}
}
}
Some(file)
}
#[cfg(not(unix))]
pub(super) fn acquire_ledger_lock(_path: &std::path::Path) -> Option<std::fs::File> {
None
}