use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::{BTreeMap, HashMap};
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write};
use std::path::Path;
const MAX_RECORD: usize = 1 << 20;
const MAX_BYTES: u64 = 128 << 20;
const MAX_ADMISSIONS: usize = 4096;
const CALL_COMPLETION_RESERVE: u64 = 4096;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Admission {
pub binding: String,
pub submission: String,
pub execution_id: String,
pub scope: String,
pub actor: String,
pub status: Value,
#[serde(default)]
pub bridge_evidence: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CallEvidence {
pub call_id: u64,
pub method: String,
pub completion: Option<CallCompletion>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CallCompletion {
pub reply_received: bool,
pub ok: Option<bool>,
#[serde(default)]
pub operation: Option<OperationObservation>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct OperationObservation {
pub operation_id: String,
pub state: Option<String>,
pub effects: Option<String>,
}
#[derive(Clone, Debug)]
pub struct Record {
pub admission: Admission,
pub outcome: Option<Value>,
pub calls: BTreeMap<u64, CallEvidence>,
}
impl Record {
pub fn recovery_evidence(&self) -> Value {
json!({
"hostCallRecordingEnabled":self.admission.bridge_evidence,
"operationsEvidenceComplete":false,"automaticReplay":false,
"calls":self.calls.values().collect::<Vec<_>>(),
"unacknowledgedCallIds":self.calls.values().filter(|c| !c.completion.as_ref().is_some_and(|v| v.reply_received)).map(|c| c.call_id).collect::<Vec<_>>(),
"observationsMayBeStale":true
})
}
}
#[derive(Deserialize, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
enum Entry {
Admit { admission: Admission },
Finish { execution_id: String, outcome: Value },
CallStarted { execution_id: String, call_id: u64, method: String },
CallFinished { execution_id: String, call_id: u64, completion: CallCompletion },
}
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct Frame {
version: u32,
sha256: String,
payload: Value,
}
pub struct Journal {
file: File, bytes: u64,
poisoned: bool,
records: HashMap<String, Record>,
bindings: HashMap<String, String>,
}
impl Journal {
pub fn open(directory: &Path) -> Result<Self, String> {
let parent = directory.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
if !parent.is_dir() { return Err("create the persistent state directory's parent before opening the journal".into()); }
crate::host::private_dir(directory)?;
#[cfg(unix)]
File::open(parent).and_then(|f| f.sync_all()).map_err(|e| e.to_string())?;
let path = directory.join("executions.jsonl");
if std::fs::symlink_metadata(&path).is_ok_and(|m| !m.is_file() || m.file_type().is_symlink()) {
return Err("execution journal must be a regular file, not a symlink".into());
}
let mut options = OpenOptions::new();
options.read(true).append(true).create(true);
#[cfg(unix)] {
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let file = options.open(&path).map_err(|e| e.to_string())?;
file.try_lock().map_err(|_| "persistent state directory is already in use or cannot be locked".to_string())?;
#[cfg(unix)] {
use std::os::unix::fs::PermissionsExt;
file.set_permissions(std::fs::Permissions::from_mode(0o600)).map_err(|e| e.to_string())?;
}
file.sync_all().map_err(|e| e.to_string())?;
#[cfg(unix)]
File::open(directory).and_then(|f| f.sync_all()).map_err(|e| e.to_string())?;
let bytes = file.metadata().map_err(|e| e.to_string())?.len();
if bytes > MAX_BYTES { return Err("execution journal exceeds its storage budget".into()); }
let mut journal = Self { file, bytes, poisoned: false, records: HashMap::new(), bindings: HashMap::new() };
let mut reader = BufReader::new(journal.file.try_clone().map_err(|e| e.to_string())?);
reader.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?;
loop {
let mut line = Vec::new();
let count = Read::by_ref(&mut reader).take((MAX_RECORD + 1) as u64).read_until(b'\n', &mut line).map_err(|e| e.to_string())?;
if count == 0 { break; }
if count > MAX_RECORD || line.last() != Some(&b'\n') {
return Err("execution journal has an oversized or truncated record; reconciliation required".into());
}
let frame: Frame = serde_json::from_slice(&line).map_err(|_| "invalid execution journal frame")?;
let canonical = super::canon::canonical(&frame.payload)?;
if frame.version != 1 || super::canon::sha256_hex(canonical.as_bytes()) != frame.sha256 {
return Err("execution journal checksum/version mismatch; reconciliation required".into());
}
let entry: Entry = serde_json::from_value(frame.payload).map_err(|_| "invalid execution journal entry")?;
journal.validate(&entry)?;
journal.apply(entry);
}
Ok(journal)
}
pub fn binding(scope: &str, actor: &str, namespace: &str, key: &str) -> String {
super::canon::sha256_hex(json!([scope, actor, namespace, key]).to_string().as_bytes())
}
pub fn by_binding(&self, binding: &str, submission: &str) -> Result<Option<&Record>, String> {
let Some(id) = self.bindings.get(binding) else { return Ok(None) };
let record = &self.records[id];
if record.admission.submission != submission { return Err("idempotency_conflict".into()); }
Ok(Some(record))
}
pub fn by_id(&self, id: &str, scope: &str, actor: &str) -> Option<&Record> {
self.records.get(id).filter(|r| r.admission.scope == scope && r.admission.actor == actor)
}
pub fn unfinished(&self) -> Vec<Admission> {
self.records.values().filter(|r| r.outcome.is_none()).map(|r| r.admission.clone()).collect()
}
pub fn admit(&mut self, admission: Admission) -> Result<(), String> {
self.append(Entry::Admit { admission })
}
pub fn finish(&mut self, execution_id: &str, outcome: Value) -> Result<(), String> {
self.append(Entry::Finish { execution_id: execution_id.into(), outcome })
}
pub fn start_call(&mut self, execution_id: &str, call_id: u64, method: &str) -> Result<(), String> {
self.append(Entry::CallStarted { execution_id: execution_id.into(), call_id, method: method.into() })
}
pub fn finish_call(&mut self, execution_id: &str, call_id: u64, completion: CallCompletion) -> Result<(), String> {
self.append(Entry::CallFinished { execution_id: execution_id.into(), call_id, completion })
}
fn validate(&self, entry: &Entry) -> Result<(), String> {
match entry {
Entry::Admit { admission: a } => {
if self.records.len() >= MAX_ADMISSIONS { return Err("execution journal admission capacity reached".into()); }
if self.bindings.contains_key(&a.binding) || self.records.contains_key(&a.execution_id) {
return Err("duplicate durable admission".into());
}
let digest = |s: &str| s.len() == 64 && s.bytes().all(|b| b.is_ascii_hexdigit());
if a.execution_id.len() > 128 || a.status["executionId"] != a.execution_id || !digest(&a.binding) || !digest(&a.submission) {
return Err("invalid durable admission identity".into());
}
}
Entry::CallStarted { execution_id, call_id, method } => {
let record = self.records.get(execution_id).ok_or("call without admission")?;
if record.outcome.is_some() || record.calls.len() >= 1024 || record.calls.contains_key(call_id) {
return Err("call admission refused: terminal execution, duplicate call, or call capacity reached".into());
}
if method.is_empty() || method.len() > 128 || !method.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'.' || b == b'_') {
return Err("invalid call method metadata".into());
}
}
Entry::CallFinished { execution_id, call_id, completion } => {
let call = self.records.get(execution_id).and_then(|r| r.calls.get(call_id)).ok_or("completion without call admission")?;
if let Some(op) = &completion.operation {
if (!op.operation_id.is_ascii() || super::wire::handle_shape("op", &op.operation_id) == super::wire::HandleShape::Malformed)
|| op.state.as_deref().is_some_and(|s| !matches!(s, "queued" | "running" | "evaluating" | "committing" | "succeeded" | "failed" | "cancelled"))
|| op.effects.as_deref().is_some_and(|s| !matches!(s, "none" | "committed" | "unknown" | "partial")) {
return Err("invalid operation observation".into());
}
}
if call.completion.is_some() || (!completion.reply_received && (completion.ok.is_some() || completion.operation.is_some())) {
return Err("invalid or duplicate call completion".into());
}
}
Entry::Finish { execution_id, outcome } => {
let record = self.records.get(execution_id).ok_or("terminal record without admission")?;
if record.outcome.is_some() { return Err("terminal execution outcome is immutable".into()); }
if outcome["executionId"] != *execution_id || !matches!(outcome["state"].as_str(), Some("succeeded" | "failed" | "cancelled")) {
return Err("invalid terminal execution outcome".into());
}
}
}
Ok(())
}
fn append(&mut self, entry: Entry) -> Result<(), String> {
if self.poisoned { return Err("execution journal unavailable after a write failure; reconciliation required".into()); }
self.validate(&entry)?;
let payload = serde_json::to_value(&entry).map_err(|e| e.to_string())?;
let sha256 = super::canon::sha256_hex(super::canon::canonical(&payload)?.as_bytes());
let mut bytes = serde_json::to_vec(&Frame { version: 1, sha256, payload }).map_err(|e| e.to_string())?;
bytes.push(b'\n');
if bytes.len() > MAX_RECORD || self.bytes + bytes.len() as u64 > MAX_BYTES {
return Err("execution journal storage budget reached; existing admission evidence is preserved".into());
}
let mut unfinished = self.records.values().filter(|r| r.outcome.is_none()).count() as u64;
let mut pending = self.records.values().flat_map(|r| r.calls.values()).filter(|c| c.completion.is_none()).count() as u64;
match &entry {
Entry::Admit { .. } => unfinished += 1,
Entry::Finish { .. } => unfinished -= 1,
Entry::CallStarted { .. } => pending += 1,
Entry::CallFinished { .. } => pending -= 1,
}
if self.bytes + bytes.len() as u64 + unfinished * MAX_RECORD as u64 + pending * CALL_COMPLETION_RESERVE > MAX_BYTES {
return Err("execution journal has no unreserved outcome capacity".into());
}
if let Err(e) = self.file.write_all(&bytes).and_then(|()| self.file.sync_all()) {
self.poisoned = true;
return Err(format!("execution journal write failed; reconciliation required: {e}"));
}
self.bytes += bytes.len() as u64;
self.apply(entry);
Ok(())
}
fn apply(&mut self, entry: Entry) {
match entry {
Entry::Admit { admission } => {
self.bindings.insert(admission.binding.clone(), admission.execution_id.clone());
self.records.insert(admission.execution_id.clone(), Record { admission, outcome: None, calls: BTreeMap::new() });
}
Entry::CallStarted { execution_id, call_id, method } => {
self.records.get_mut(&execution_id).unwrap().calls.insert(call_id, CallEvidence { call_id, method, completion: None });
}
Entry::CallFinished { execution_id, call_id, completion } => {
self.records.get_mut(&execution_id).unwrap().calls.get_mut(&call_id).unwrap().completion = Some(completion);
}
Entry::Finish { execution_id, outcome } => self.records.get_mut(&execution_id).unwrap().outcome = Some(outcome),
}
}
}