use std::path::{Path, PathBuf};
use lean_ctx_protocol::Sha256Digest;
use lean_ctx_protocol::context_gateway::ContextDecisionReceiptV1;
use serde::{Deserialize, Serialize};
pub(crate) const LATEST_CAPACITY: usize = 64;
pub(crate) const TASK_CAPACITY: usize = 256;
const MAX_INDEX_BYTES: usize = TASK_CAPACITY * 70 + 256;
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct LatestIndex {
schema_version: u32,
receipts: Vec<String>,
}
fn gateway_dir() -> Option<PathBuf> {
crate::core::data_dir::lean_ctx_data_dir()
.ok()
.map(|dir| dir.join("gateway"))
}
fn project_key(project_root: &str) -> String {
let digest = blake3::hash(project_root.as_bytes()).to_hex();
digest[..16].to_owned()
}
fn receipt_path(dir: &Path, digest: &Sha256Digest) -> PathBuf {
dir.join("receipts").join(format!("{}.json", digest.hex()))
}
fn latest_path(dir: &Path, project_root: &str) -> PathBuf {
dir.join("latest")
.join(format!("{}.json", project_key(project_root)))
}
fn task_path(dir: &Path, scope: &str, task_id: &str) -> PathBuf {
let mut hasher = blake3::Hasher::new();
hasher.update(scope.as_bytes());
hasher.update(&[0]);
hasher.update(task_id.as_bytes());
let digest = hasher.finalize().to_hex();
dir.join("tasks").join(format!("{}.json", &digest[..32]))
}
fn is_digest_hex(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
fn read_index(path: &Path) -> Result<LatestIndex, ()> {
use std::io::Read;
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(LatestIndex::default());
}
Err(_) => return Err(()),
};
let mut bytes = Vec::new();
file.take(MAX_INDEX_BYTES as u64 + 1)
.read_to_end(&mut bytes)
.map_err(|_| ())?;
if bytes.len() > MAX_INDEX_BYTES {
return Err(());
}
let index: LatestIndex = serde_json::from_slice(&bytes).map_err(|_| ())?;
if index.schema_version != 1
|| index.receipts.len() > TASK_CAPACITY
|| !index.receipts.iter().all(|hex| is_digest_hex(hex))
{
return Err(());
}
Ok(index)
}
fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
crate::core::atomic_fs::try_atomic_write(path, bytes, None).map_err(|e| e.to_string())
}
pub(crate) fn lock_task_files(dir: &Path) -> Option<std::fs::File> {
use fs2::FileExt;
std::fs::create_dir_all(dir).ok()?;
let file = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(dir.join(".index.lock"))
.ok()?;
let deadline = std::time::Instant::now() + LOCK_WAIT;
loop {
match file.try_lock_exclusive() {
Ok(()) => return Some(file),
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
if std::time::Instant::now() >= deadline {
return None;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(_) => return None,
}
}
}
const LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(2);
fn incomplete_marker(dir: &Path, scope: &str, task_id: &str) -> PathBuf {
task_path(dir, scope, task_id).with_extension("incomplete")
}
pub(crate) fn persist(
receipt: &ContextDecisionReceiptV1,
project_root: &str,
task_scope: Option<&str>,
) -> Result<Sha256Digest, String> {
let dir = gateway_dir().ok_or("no lean-ctx data directory")?;
persist_in(&dir, receipt, project_root, task_scope)
}
pub(crate) fn persist_in(
dir: &Path,
receipt: &ContextDecisionReceiptV1,
project_root: &str,
task_scope: Option<&str>,
) -> Result<Sha256Digest, String> {
let digest = receipt.canonical_digest().map_err(|e| e.to_string())?;
let value = serde_json::to_value(receipt).map_err(|e| e.to_string())?;
let bytes = serde_json::to_vec(&value).map_err(|e| e.to_string())?;
let path = receipt_path(dir, &digest);
if !path.exists() {
write_atomic(&path, &bytes)?;
}
let Some(_lock) = lock_task_files(dir) else {
if let (Some(task), Some(scope)) = (&receipt.task, task_scope) {
let marker = incomplete_marker(dir, scope, task.as_str());
let marked = marker
.parent()
.map_or(Ok(()), std::fs::create_dir_all)
.and_then(|()| std::fs::write(&marker, b""));
if let Err(error) = marked {
tracing::warn!("task receipt index skipped and not marked incomplete: {error}");
}
}
return Ok(digest);
};
let index_path = latest_path(dir, project_root);
let mut index = read_index(&index_path).unwrap_or_default();
index.schema_version = 1;
index.receipts.retain(|existing| existing != digest.hex());
index.receipts.insert(0, digest.hex().to_owned());
index.receipts.truncate(LATEST_CAPACITY);
write_atomic(
&index_path,
&serde_json::to_vec(&index).map_err(|e| e.to_string())?,
)?;
if let (Some(task), Some(scope)) = (&receipt.task, task_scope) {
let task_index_path = task_path(dir, scope, task.as_str());
if let Ok(mut task_index) = read_index(&task_index_path)
&& task_index.receipts.len() < TASK_CAPACITY
&& !task_index.receipts.iter().any(|hex| hex == digest.hex())
{
task_index.schema_version = 1;
task_index.receipts.push(digest.hex().to_owned());
write_atomic(
&task_index_path,
&serde_json::to_vec(&task_index).map_err(|e| e.to_string())?,
)?;
}
}
Ok(digest)
}
#[derive(Debug, Clone)]
pub(crate) struct StoredReceipt {
pub(crate) digest: Sha256Digest,
pub(crate) receipt: ContextDecisionReceiptV1,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum LoadError {
Missing,
Unreadable,
Tampered,
}
pub(crate) fn load_in(dir: &Path, hex: &str) -> Result<StoredReceipt, LoadError> {
let expected = Sha256Digest::new(format!("sha256:{hex}")).map_err(|_| LoadError::Unreadable)?;
let bytes = std::fs::read(receipt_path(dir, &expected)).map_err(|_| LoadError::Missing)?;
let receipt: ContextDecisionReceiptV1 =
serde_json::from_slice(&bytes).map_err(|_| LoadError::Unreadable)?;
let actual = receipt
.canonical_digest()
.map_err(|_| LoadError::Tampered)?;
if actual != expected {
return Err(LoadError::Tampered);
}
Ok(StoredReceipt {
digest: expected,
receipt,
})
}
pub(crate) fn latest(
project_root: &str,
limit: usize,
) -> Vec<(String, Result<StoredReceipt, LoadError>)> {
let Some(dir) = gateway_dir() else {
return Vec::new();
};
latest_in(&dir, project_root, limit)
}
pub(crate) fn latest_in(
dir: &Path,
project_root: &str,
limit: usize,
) -> Vec<(String, Result<StoredReceipt, LoadError>)> {
read_index(&latest_path(dir, project_root))
.unwrap_or_default()
.receipts
.into_iter()
.take(limit)
.map(|hex| {
let loaded = load_in(dir, &hex);
(hex, loaded)
})
.collect()
}
#[derive(Debug, Clone)]
pub(crate) struct TaskReceipts {
pub(crate) entries: Vec<(String, Result<StoredReceipt, LoadError>)>,
pub(crate) index_complete: bool,
}
impl TaskReceipts {
pub(crate) fn complete(&self) -> bool {
self.index_complete && self.entries.iter().all(|(_, loaded)| loaded.is_ok())
}
pub(crate) fn verified(&self) -> impl Iterator<Item = &ContextDecisionReceiptV1> {
self.entries
.iter()
.filter_map(|(_, loaded)| loaded.as_ref().ok().map(|stored| &stored.receipt))
}
}
pub(crate) fn for_task(scope: &str, task_id: &str) -> TaskReceipts {
match gateway_dir() {
Some(dir) => for_task_in(&dir, scope, task_id),
None => TaskReceipts {
entries: Vec::new(),
index_complete: true,
},
}
}
pub(crate) fn for_task_in(dir: &Path, scope: &str, task_id: &str) -> TaskReceipts {
let Ok(index) = read_index(&task_path(dir, scope, task_id)) else {
return TaskReceipts {
entries: Vec::new(),
index_complete: false,
};
};
let index_complete =
index.receipts.len() < TASK_CAPACITY && !incomplete_marker(dir, scope, task_id).exists();
let entries = index
.receipts
.into_iter()
.map(|hex| {
let loaded = load_in(dir, &hex).and_then(|stored| {
if stored
.receipt
.task
.as_ref()
.map(lean_ctx_protocol::TaskId::as_str)
== Some(task_id)
{
Ok(stored)
} else {
Err(LoadError::Tampered)
}
});
(hex, loaded)
})
.collect();
TaskReceipts {
entries,
index_complete,
}
}