use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{BufRead, Read};
use std::path::{Path, PathBuf};
use super::{
AuditEntry, AuditEventType, ChainVerifyResult, SNAPSHOT_ATTEMPTS, checkpoint,
compute_entry_hash, is_seal, lock_trail_shared, release_read_lock, segments,
still_holds_prefix, trail_path, v2,
};
fn hashed_fields(value: &serde_json::Value) -> Option<serde_json::Value> {
let get = |key: &str| value.get(key).cloned();
Some(serde_json::json!({
"agent_id": get("agent_id")?,
"tool": get("tool")?,
"action": value.get("action").cloned().unwrap_or(serde_json::Value::Null),
"input_hash": get("input_hash")?,
"output_tokens": get("output_tokens")?,
"role": get("role")?,
"event_type": get("event_type")?,
}))
}
fn invalid_at(total: usize, unknown: usize) -> ChainVerifyResult {
ChainVerifyResult {
total_entries: total,
valid: false,
first_invalid_at: Some(total),
source_present: true,
unknown_event_types: unknown,
}
}
fn unavailable() -> ChainVerifyResult {
ChainVerifyResult {
total_entries: 0,
valid: false,
first_invalid_at: Some(0),
source_present: false,
unknown_event_types: 0,
}
}
pub(super) fn verify_chain_with_drift(
agent_id: Option<&str>,
) -> (ChainVerifyResult, Option<(String, String)>) {
let Some(path) = trail_path() else {
return (unavailable(), None);
};
verify_chain_with_drift_at(&path, agent_id)
}
fn verify_chain_with_drift_at(
path: &Path,
agent_id: Option<&str>,
) -> (ChainVerifyResult, Option<(String, String)>) {
verify_chain_mode(path, agent_id, false)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(super) struct ChainState {
pub(super) prev_hash: String,
pub(super) head: Option<String>,
pub(super) total: usize,
unknown: usize,
pending: std::collections::BTreeMap<String, (String, String)>,
tally: SignedTallyEvidence,
security: SignedTallyEvidence,
sealed: Option<u64>,
}
impl ChainState {
pub(super) fn genesis() -> Self {
Self {
prev_hash: "genesis".to_string(),
head: None,
total: 0,
unknown: 0,
pending: std::collections::BTreeMap::new(),
tally: SignedTallyEvidence::default(),
security: SignedTallyEvidence::default(),
sealed: None,
}
}
fn invalid(&self) -> (ChainVerifyResult, Option<(String, String)>) {
(invalid_at(self.total, self.unknown), None)
}
fn valid(&self, agent_id: Option<&str>) -> (ChainVerifyResult, Option<(String, String)>) {
(
ChainVerifyResult {
total_entries: self.total,
valid: true,
first_invalid_at: None,
source_present: true,
unknown_event_types: self.unknown,
},
agent_id.and_then(|id| self.pending.get(id).cloned()),
)
}
fn advance(
&mut self,
value: serde_json::Value,
key: &mut Option<Result<ed25519_dalek::VerifyingKey, ()>>,
) -> Result<(), ()> {
let (Some(entry_prev), Some(entry_hash)) = (
value.get("prev_hash").and_then(serde_json::Value::as_str),
value.get("entry_hash").and_then(serde_json::Value::as_str),
) else {
return Err(());
};
let partial = hashed_fields(&value).ok_or(())?;
if entry_prev != self.prev_hash {
return Err(());
}
let data_json = serde_json::to_string(&partial).unwrap_or_default();
if entry_hash != compute_entry_hash(&self.prev_hash, &data_json) {
return Err(());
}
let entry_hash = entry_hash.to_string();
let version = v2::version_of(&value).ok_or(())?;
let newer_version = match version {
0 => return Err(()),
1 => false,
v2::ENTRY_VERSION => {
let fields = v2::v2_fields(&value).ok_or(())?;
let claimed = value.get("v2_hash").and_then(serde_json::Value::as_str);
if claimed != Some(v2::compute_v2_hash(self.head(), &fields).as_str()) {
return Err(());
}
false
}
_ => true,
};
let head = v2::head_of(&value, version).ok_or(())?;
let entry = serde_json::from_value::<AuditEntry>(value).map_err(|_| ())?;
let seal = is_seal(&entry.event_type);
if (matches!(&entry.event_type, AuditEventType::Unknown(_)) && !seal) || newer_version {
self.unknown += 1;
}
self.sealed = if seal {
Some(segments::sealed_seq(entry.action.as_deref()).ok_or(())?)
} else {
None
};
let signature_required = seal
|| matches!(
&entry.event_type,
AuditEventType::AgentDriftDetected | AuditEventType::AgentDriftAcknowledged
);
if signature_required && entry.signature.is_none() {
return Err(());
}
if let Some(signature) = entry.signature.as_deref() {
let signature = crate::core::agent_identity::hex_decode(signature).map_err(|_| ())?;
let Ok(public_key) = key
.get_or_insert_with(|| {
crate::core::agent_identity::load_public_key("lean-ctx").map_err(|_| ())
})
.as_ref()
else {
return Err(());
};
if !crate::core::agent_identity::verify_signature(
public_key.as_bytes(),
entry.entry_hash.as_bytes(),
&signature,
) {
return Err(());
}
}
if let Some((kind, n, _)) = entry
.action
.as_deref()
.and_then(crate::core::security_events::parse_action)
{
self.security.record(kind, n, &entry.entry_hash);
if kind.in_signed_tally() {
self.tally.record(kind, n, &entry.entry_hash);
}
}
match entry.event_type {
AuditEventType::AgentDriftDetected => {
if let Some(evidence) = entry.action {
self.pending.insert(
entry.agent_id,
(
entry.timestamp,
format!("{evidence} observation={}", entry.entry_hash),
),
);
}
}
AuditEventType::AgentDriftAcknowledged
if self
.pending
.get(&entry.agent_id)
.is_some_and(|(_, evidence)| entry.action.as_ref() == Some(evidence)) =>
{
self.pending.remove(&entry.agent_id);
}
_ => {}
}
self.head = (head != entry_hash).then_some(head);
self.prev_hash = entry_hash;
self.total += 1;
Ok(())
}
fn head(&self) -> &str {
self.head.as_deref().unwrap_or(&self.prev_hash)
}
pub(super) fn verify_stream(&mut self, reader: &mut impl BufRead) -> Result<(u64, bool), ()> {
let mut consumed = 0u64;
let mut ends_on_newline = true;
let mut buf = Vec::new();
let mut key = None;
loop {
buf.clear();
let read = reader.read_until(b'\n', &mut buf).map_err(|_| ())?;
if read == 0 {
return Ok((consumed, ends_on_newline));
}
consumed += read as u64;
ends_on_newline = buf.last() == Some(&b'\n');
if ends_on_newline {
buf.pop();
if buf.last() == Some(&b'\r') {
buf.pop();
}
}
let line = std::str::from_utf8(&buf).map_err(|_| ())?;
let mut saw_value = false;
for parsed in serde_json::Deserializer::from_str(line).into_iter::<serde_json::Value>()
{
saw_value = true;
self.advance(parsed.map_err(|_| ())?, &mut key)?;
}
if !saw_value {
return Err(());
}
}
}
}
fn verify_chain_mode(
path: &Path,
agent_id: Option<&str>,
force_full: bool,
) -> (ChainVerifyResult, Option<(String, String)>) {
verify_chain_mode_at(
path,
agent_id,
force_full,
lean_ctx_time::UtcDateTime::now().timestamp(),
)
}
pub(super) fn verify_chain_mode_at(
path: &Path,
agent_id: Option<&str>,
force_full: bool,
now: i64,
) -> (ChainVerifyResult, Option<(String, String)>) {
match verify_state_at(path, force_full, now) {
Verified::Missing => (
ChainVerifyResult {
total_entries: 0,
valid: true,
first_invalid_at: None,
source_present: false,
unknown_event_types: 0,
},
None,
),
Verified::Unavailable => (unavailable(), None),
Verified::Invalid(state) => state.invalid(),
Verified::Valid(state) => state.valid(agent_id),
}
}
enum Verified {
Missing,
Unavailable,
Invalid(ChainState),
Valid(ChainState),
}
fn verify_state_at(path: &Path, force_full: bool, now: i64) -> Verified {
for _ in 0..SNAPSHOT_ATTEMPTS {
let sealed = segments::sealed(path);
let active = match fs::File::open(path) {
Ok(file) => Some(file),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(_) => return Verified::Unavailable,
};
if segments::sealed(path) == sealed {
return verify_snapshot(path, &sealed, active, force_full, now);
}
}
Verified::Unavailable
}
fn verify_snapshot(
path: &Path,
sealed: &[(u64, PathBuf)],
active: Option<fs::File>,
force_full: bool,
now: i64,
) -> Verified {
if sealed.is_empty() && active.is_none() {
return Verified::Missing;
}
let day = usize::try_from(now.div_euclid(24 * 60 * 60)).unwrap_or(0);
let sample = day.checked_rem(sealed.len());
let mut state = ChainState::genesis();
for (index, (seq, segment)) in sealed.iter().enumerate() {
let Ok(file) = fs::File::open(segment) else {
return Verified::Unavailable;
};
let max_age = (Some(index) == sample).then_some(checkpoint::FULL_REVERIFY_SECS);
state = match verify_file(segment, &file, &state, force_full, max_age, now) {
Verified::Valid(state) if state.sealed == Some(*seq) => state,
Verified::Valid(state) => return Verified::Invalid(state),
other => return other,
};
}
match active {
Some(file) => verify_file(
path,
&file,
&state,
force_full,
Some(checkpoint::FULL_REVERIFY_SECS),
now,
),
None => Verified::Valid(state),
}
}
fn verify_file(
path: &Path,
file: &fs::File,
start: &ChainState,
force_full: bool,
max_age: Option<i64>,
now: i64,
) -> Verified {
use std::io::{Seek, SeekFrom};
let mut cursor = file;
if lock_trail_shared(file).is_err() {
return Verified::Unavailable;
}
let store = checkpoint::Store::for_trail(path);
let stamp = checkpoint::FileStamp::of(file);
let Some(len) = stamp
.as_ref()
.map(|stamp| stamp.size)
.or_else(|| file.metadata().ok().map(|meta| meta.len()))
else {
return Verified::Unavailable;
};
release_read_lock(file);
if !force_full {
if let (Some(store), Some(stamp)) = (&store, &stamp) {
if let Some(resumed) = store.resume(file, stamp, start, max_age, now) {
let mut state = resumed.state;
if resumed.offset == stamp.size {
return Verified::Valid(state);
}
if cursor.seek(SeekFrom::Start(resumed.offset)).is_ok() {
let suffix = state.verify_stream(&mut std::io::BufReader::new(
file.take(len - resumed.offset),
));
if !still_holds_prefix(file, len) {
return Verified::Unavailable;
}
if let Ok((read, clean)) = suffix {
if clean && resumed.offset + read == stamp.size {
store.save(file, stamp, start, &state, resumed.full_verified_at);
}
return Verified::Valid(state);
}
}
}
}
}
if cursor.seek(SeekFrom::Start(0)).is_err() {
return Verified::Unavailable;
}
let mut state = start.clone();
let pass = state.verify_stream(&mut std::io::BufReader::new(file.take(len)));
if !still_holds_prefix(file, len) {
return Verified::Unavailable;
}
let Ok((read, clean)) = pass else {
if let Some(store) = &store {
store.discard();
}
return Verified::Invalid(state);
};
if let (Some(store), Some(stamp)) = (&store, &stamp) {
if clean && read == stamp.size {
store.save(file, stamp, start, &state, now);
}
}
Verified::Valid(state)
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignedTallyEvidence {
pub counts: crate::core::security_events::SecurityCounts,
pub entries: u64,
pub first_hash: Option<String>,
pub last_hash: Option<String>,
}
impl SignedTallyEvidence {
fn record(&mut self, kind: crate::core::security_events::SecurityKind, n: u64, hash: &str) {
self.counts.add(kind, n);
self.entries += 1;
self.first_hash.get_or_insert_with(|| hash.to_string());
self.last_hash = Some(hash.to_string());
}
}
pub fn verified_signed_tally_at(path: &Path) -> Option<SignedTallyEvidence> {
match verify_state_at(path, false, lean_ctx_time::UtcDateTime::now().timestamp()) {
Verified::Missing => Some(SignedTallyEvidence::default()),
Verified::Valid(state) => Some(state.tally),
Verified::Unavailable | Verified::Invalid(_) => None,
}
}
pub fn verified_security_at(path: &Path) -> (ChainVerifyResult, Option<SignedTallyEvidence>) {
match verify_state_at(path, false, lean_ctx_time::UtcDateTime::now().timestamp()) {
Verified::Missing => (
ChainVerifyResult {
total_entries: 0,
valid: true,
first_invalid_at: None,
source_present: false,
unknown_event_types: 0,
},
Some(SignedTallyEvidence::default()),
),
Verified::Unavailable => (unavailable(), None),
Verified::Invalid(state) => (state.invalid().0, None),
Verified::Valid(state) => (state.valid(None).0, Some(state.security)),
}
}
pub fn verify_chain() -> ChainVerifyResult {
verify_chain_with_drift(None).0
}
pub fn verify_chain_full() -> ChainVerifyResult {
let Some(path) = trail_path() else {
return unavailable();
};
verify_chain_mode(&path, None, true).0
}
pub fn verify_chain_at(path: &Path) -> ChainVerifyResult {
verify_chain_with_drift_at(path, None).0
}