use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, MutexGuard};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::llm::Usage;
use crate::record::{
canonicalize_value, sha256_hex, ArtifactRef, Effect, EffectReceipt, EventStatus, PayloadRef,
RunEvent, RunEventKind, INLINE_PAYLOAD_MAX_BYTES,
};
pub const PARENT_EVENT_KEY: &str = "rusty.parent_event";
#[derive(Debug, Clone, Default)]
pub enum Clock {
#[default]
System,
Logical(LogicalClock),
}
impl Clock {
pub fn logical(start_ms: u64, tick_ms: u64) -> Self {
Clock::Logical(LogicalClock {
state: Arc::new(Mutex::new(LogicalClockState {
now_ms: start_ms,
tick_ms,
})),
})
}
pub fn now(&self) -> DateTime<Utc> {
match self {
Clock::System => Utc::now(),
Clock::Logical(logical) => {
let mut state = logical.lock();
let millis = state.now_ms;
state.now_ms = state.now_ms.saturating_add(state.tick_ms);
DateTime::from_timestamp_millis(millis.min(i64::MAX as u64) as i64)
.unwrap_or(DateTime::UNIX_EPOCH)
}
}
}
pub fn now_ms(&self) -> u64 {
self.now().timestamp_millis().max(0) as u64
}
}
#[derive(Debug, Clone)]
pub struct LogicalClock {
state: Arc<Mutex<LogicalClockState>>,
}
#[derive(Debug)]
struct LogicalClockState {
now_ms: u64,
tick_ms: u64,
}
impl LogicalClock {
fn lock(&self) -> MutexGuard<'_, LogicalClockState> {
self.state.lock().unwrap_or_else(|e| e.into_inner())
}
}
#[derive(Debug, Clone, Default)]
pub enum RngSource {
#[default]
System,
Seeded(SeededRng),
}
impl RngSource {
pub fn seeded(seed: u64) -> Self {
RngSource::Seeded(SeededRng {
rng: Arc::new(Mutex::new(ChaCha8Rng::seed_from_u64(seed))),
})
}
pub fn uuid(&self) -> uuid::Uuid {
match self {
RngSource::System => uuid::Uuid::new_v4(),
RngSource::Seeded(seeded) => {
use rand::RngCore as _;
let mut bytes = [0u8; 16];
let mut rng = seeded.rng.lock().unwrap_or_else(|e| e.into_inner());
rng.fill_bytes(&mut bytes);
uuid::Builder::from_random_bytes(bytes).into_uuid()
}
}
}
pub fn uuid_string(&self) -> String {
self.uuid().to_string()
}
pub fn next_f64(&self) -> f64 {
let bits = match self {
RngSource::System => {
let bytes = self.uuid().into_bytes();
u64::from_le_bytes(bytes[..8].try_into().expect("a uuid is 16 bytes"))
}
RngSource::Seeded(seeded) => {
use rand::RngCore as _;
seeded
.rng
.lock()
.unwrap_or_else(|e| e.into_inner())
.next_u64()
}
};
(bits >> 11) as f64 / 9_007_199_254_740_992.0
}
}
#[derive(Debug, Clone)]
pub struct SeededRng {
rng: Arc<Mutex<ChaCha8Rng>>,
}
#[derive(Debug, Clone)]
pub struct EventDraft {
kind: RunEventKind,
effect: Effect,
node_id: Option<String>,
input: Option<Value>,
output: Option<Value>,
latency_ms: Option<u64>,
tokens: Option<Usage>,
cost_usd: Option<f64>,
status: EventStatus,
parent: Option<String>,
}
impl EventDraft {
pub fn new(kind: RunEventKind, effect: Effect) -> Self {
Self {
kind,
effect,
node_id: None,
input: None,
output: None,
latency_ms: None,
tokens: None,
cost_usd: None,
status: EventStatus::Ok,
parent: None,
}
}
pub fn node(mut self, node_id: impl Into<String>) -> Self {
self.node_id = Some(node_id.into());
self
}
pub fn input(mut self, value: Value) -> Self {
self.input = Some(value);
self
}
pub fn output(mut self, value: Value) -> Self {
self.output = Some(value);
self
}
pub fn latency_ms(mut self, latency_ms: u64) -> Self {
self.latency_ms = Some(latency_ms);
self
}
pub fn tokens(mut self, usage: Usage) -> Self {
self.tokens = Some(usage);
self
}
pub fn cost_usd(mut self, cost: f64) -> Self {
self.cost_usd = Some(cost);
self
}
pub fn status(mut self, status: EventStatus) -> Self {
self.status = status;
self
}
pub fn parent(mut self, parent: impl Into<String>) -> Self {
self.parent = Some(parent.into());
self
}
}
#[derive(Debug)]
struct JournalInner {
events: Vec<RunEvent>,
artifacts: BTreeMap<String, Value>,
head_hash: String,
}
#[derive(Debug, Clone)]
pub struct Journal {
run_id: String,
thread_id: String,
clock: Clock,
inner: Arc<Mutex<JournalInner>>,
}
impl Journal {
pub fn new(run_id: impl Into<String>, thread_id: impl Into<String>, clock: Clock) -> Self {
Self {
run_id: run_id.into(),
thread_id: thread_id.into(),
clock,
inner: Arc::new(Mutex::new(JournalInner {
events: Vec::new(),
artifacts: BTreeMap::new(),
head_hash: sha256_hex(b""),
})),
}
}
pub fn from_snapshot(snapshot: JournalSnapshot, clock: Clock) -> crate::error::Result<Self> {
if !snapshot.artifact_refs.is_empty() {
return Err(crate::error::RustyError::Serialization(
serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"journal snapshot references an external artifact store; \
load it with Journal::from_snapshot_with_store",
)),
));
}
let mut head_hash = sha256_hex(b"");
for event in &snapshot.events {
head_hash = chained_hash(&head_hash, event)?;
}
if head_hash != snapshot.head_hash {
return Err(crate::error::RustyError::Serialization(
serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"journal snapshot head hash mismatch: events recompute to \
{head_hash}, snapshot claims {}",
snapshot.head_hash
),
)),
));
}
let run_id = snapshot.run_id.clone();
let thread_id = snapshot.thread_id.clone();
Ok(Self {
run_id,
thread_id,
clock,
inner: Arc::new(Mutex::new(JournalInner {
events: snapshot.events,
artifacts: snapshot.artifacts,
head_hash,
})),
})
}
pub fn run_id(&self) -> &str {
&self.run_id
}
pub fn thread_id(&self) -> &str {
&self.thread_id
}
pub fn clock(&self) -> &Clock {
&self.clock
}
fn lock(&self) -> MutexGuard<'_, JournalInner> {
self.inner.lock().unwrap_or_else(|e| e.into_inner())
}
pub fn record(&self, draft: EventDraft) -> String {
let recorded_at = self.clock.now();
let mut inner = self.lock();
let seq = inner.events.len() as u64;
let id = format!("{}:{seq}", self.run_id);
let input = draft
.input
.map(|value| store_payload(&mut inner.artifacts, value));
let output = draft
.output
.map(|value| store_payload(&mut inner.artifacts, value));
let event = RunEvent {
id: id.clone(),
run_id: self.run_id.clone(),
thread_id: self.thread_id.clone(),
node_id: draft.node_id,
seq,
kind: draft.kind,
effect: draft.effect,
input,
output,
latency_ms: draft.latency_ms,
tokens: draft.tokens,
cost_usd: draft.cost_usd,
status: draft.status,
parent: draft.parent,
recorded_at,
};
match serde_json::to_vec(&canonicalized_for_hash(&event)) {
Ok(bytes) => {
inner.head_hash = sha256_hex(&[inner.head_hash.as_bytes(), &bytes].concat());
}
Err(e) => {
tracing::warn!(error = %e, "failed to serialize event for head hash");
inner.head_hash =
sha256_hex(&[inner.head_hash.as_bytes(), b"!unhashable"].concat());
}
}
inner.events.push(event);
id
}
pub fn events(&self) -> Vec<RunEvent> {
self.lock().events.clone()
}
pub fn len(&self) -> usize {
self.lock().events.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn head_hash(&self) -> String {
self.lock().head_hash.clone()
}
pub fn head_ref(&self) -> crate::record::JournalRef {
let inner = self.lock();
crate::record::JournalRef {
events: inner.events.len() as u64,
sha256: inner.head_hash.clone(),
}
}
pub fn snapshot(&self) -> JournalSnapshot {
let inner = self.lock();
JournalSnapshot {
run_id: self.run_id.clone(),
thread_id: self.thread_id.clone(),
events: inner.events.clone(),
artifacts: inner.artifacts.clone(),
artifact_refs: BTreeMap::new(),
head_hash: inner.head_hash.clone(),
}
}
pub async fn snapshot_externalized(
&self,
store: &dyn ArtifactStore,
) -> crate::error::Result<JournalSnapshot> {
let mut snapshot = self.snapshot();
let artifacts = std::mem::take(&mut snapshot.artifacts);
for (sha256, value) in artifacts {
let bytes = serde_json::to_vec(&value).expect("a serde_json::Value always serializes");
let reference = store.put(&bytes).await?;
debug_assert_eq!(
reference.sha256, sha256,
"artifact map keys are content addresses by construction"
);
snapshot.artifact_refs.insert(sha256, reference);
}
Ok(snapshot)
}
pub async fn from_snapshot_with_store(
mut snapshot: JournalSnapshot,
store: &dyn ArtifactStore,
clock: Clock,
) -> crate::error::Result<Self> {
let refs = std::mem::take(&mut snapshot.artifact_refs);
for (sha256, reference) in refs {
let bytes = store.get(&reference.sha256).await?;
let value: Value = serde_json::from_slice(&bytes).map_err(|e| {
crate::error::RustyError::Serialization(serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("artifact `{sha256}` is not valid JSON: {e}"),
)))
})?;
snapshot.artifacts.insert(sha256, value);
}
Self::from_snapshot(snapshot, clock)
}
pub fn resolve(&self, payload: &PayloadRef) -> Option<Value> {
match payload {
PayloadRef::Inline(value) => Some(value.clone()),
PayloadRef::Artifact(reference) => {
self.lock().artifacts.get(&reference.sha256).cloned()
}
}
}
pub fn record_effect_receipt(&self, receipt: &EffectReceipt, parent: Option<String>) -> String {
let mut draft = EventDraft::new(RunEventKind::EffectReceipt, Effect::Idempotent)
.output(serde_json::to_value(receipt).unwrap_or_else(
|_| serde_json::json!({ "idempotency_key": receipt.idempotency_key }),
));
if let Some(parent) = parent {
draft = draft.parent(parent);
}
self.record(draft)
}
pub fn memory(&self, source: crate::memory::MemorySource) -> crate::memory::JournaledMemory {
crate::memory::JournaledMemory::new(self, source)
}
}
fn store_payload(artifacts: &mut BTreeMap<String, Value>, value: Value) -> PayloadRef {
let Ok(bytes) = serde_json::to_vec(&value) else {
return PayloadRef::Inline(value);
};
if bytes.len() <= INLINE_PAYLOAD_MAX_BYTES {
return PayloadRef::Inline(value);
}
let reference = ArtifactRef {
sha256: sha256_hex(&bytes),
bytes: bytes.len() as u64,
};
artifacts.entry(reference.sha256.clone()).or_insert(value);
PayloadRef::Artifact(reference)
}
fn chained_hash(prev: &str, event: &RunEvent) -> crate::error::Result<String> {
let bytes = serde_json::to_vec(&canonicalized_for_hash(event))?;
Ok(sha256_hex(&[prev.as_bytes(), &bytes].concat()))
}
pub(crate) fn recompute_head_hash(events: &[RunEvent]) -> crate::error::Result<String> {
let mut head_hash = sha256_hex(b"");
for event in events {
head_hash = chained_hash(&head_hash, event)?;
}
Ok(head_hash)
}
fn canonicalized_for_hash(event: &RunEvent) -> RunEvent {
fn canonicalized_payload(payload: &Option<PayloadRef>) -> Option<PayloadRef> {
match payload {
Some(PayloadRef::Inline(value)) => Some(PayloadRef::Inline(canonicalize_value(value))),
other => other.clone(),
}
}
let mut canonical = event.clone();
canonical.input = canonicalized_payload(&event.input);
canonical.output = canonicalized_payload(&event.output);
canonical
}
#[async_trait]
pub trait ArtifactStore: Send + Sync {
async fn put(&self, bytes: &[u8]) -> crate::error::Result<ArtifactRef>;
async fn get(&self, sha256: &str) -> crate::error::Result<Vec<u8>>;
async fn contains(&self, sha256: &str) -> crate::error::Result<bool>;
}
fn artifact_io_error(context: String, e: std::io::Error) -> crate::error::RustyError {
crate::error::RustyError::Serialization(serde_json::Error::io(std::io::Error::new(
e.kind(),
format!("{context}: {e}"),
)))
}
#[derive(Debug, Clone)]
pub struct FileArtifactStore {
dir: PathBuf,
}
impl FileArtifactStore {
pub fn new(dir: impl Into<PathBuf>) -> Self {
Self { dir: dir.into() }
}
pub fn dir(&self) -> &std::path::Path {
&self.dir
}
fn artifact_path(&self, sha256: &str) -> PathBuf {
self.dir.join(sha256)
}
}
#[async_trait]
impl ArtifactStore for FileArtifactStore {
async fn put(&self, bytes: &[u8]) -> crate::error::Result<ArtifactRef> {
let reference = ArtifactRef {
sha256: sha256_hex(bytes),
bytes: bytes.len() as u64,
};
let path = self.artifact_path(&reference.sha256);
if tokio::fs::try_exists(&path)
.await
.map_err(|e| artifact_io_error(format!("stat artifact `{}`", path.display()), e))?
{
return Ok(reference);
}
tokio::fs::create_dir_all(&self.dir).await.map_err(|e| {
artifact_io_error(format!("create artifact dir `{}`", self.dir.display()), e)
})?;
crate::checkpoint::JsonFileCheckpointer::atomic_write(&path, bytes).await?;
Ok(reference)
}
async fn get(&self, sha256: &str) -> crate::error::Result<Vec<u8>> {
let path = self.artifact_path(sha256);
let bytes = tokio::fs::read(&path)
.await
.map_err(|e| artifact_io_error(format!("read artifact `{sha256}`"), e))?;
let actual = sha256_hex(&bytes);
if actual != sha256 {
return Err(crate::error::RustyError::Serialization(
serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"artifact integrity failure: `{sha256}` re-hashes to `{actual}`; \
the stored bytes are corrupt"
),
)),
));
}
Ok(bytes)
}
async fn contains(&self, sha256: &str) -> crate::error::Result<bool> {
tokio::fs::try_exists(self.artifact_path(sha256))
.await
.map_err(|e| artifact_io_error(format!("stat artifact `{sha256}`"), e))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JournalSnapshot {
pub run_id: String,
pub thread_id: String,
pub events: Vec<RunEvent>,
pub artifacts: BTreeMap<String, Value>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub artifact_refs: BTreeMap<String, ArtifactRef>,
pub head_hash: String,
}
impl JournalSnapshot {
pub fn find_effect_receipt(&self, idempotency_key: &str) -> Option<EffectReceipt> {
self.receipts()
.find(|receipt| receipt.idempotency_key == idempotency_key)
}
pub fn find_effect_receipt_by_effect_id(
&self,
effect_id: &crate::effects::EffectId,
) -> Option<EffectReceipt> {
self.receipts()
.find(|receipt| receipt.effect_id.as_deref() == Some(effect_id.as_str()))
}
fn receipts(&self) -> impl Iterator<Item = EffectReceipt> + '_ {
self.events
.iter()
.filter(|event| event.kind == RunEventKind::EffectReceipt)
.filter_map(|event| {
let value = match event.output.as_ref()? {
PayloadRef::Inline(value) => value.clone(),
PayloadRef::Artifact(reference) => {
self.artifacts.get(&reference.sha256)?.clone()
}
};
serde_json::from_value(value).ok()
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn journal() -> Journal {
Journal::new("run-1", "thread-1", Clock::System)
}
#[test]
fn record_assigns_sequence_and_deterministic_ids() {
let j = journal();
let a = j.record(EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure));
let b = j.record(
EventDraft::new(RunEventKind::NodeInput, Effect::Pure)
.node("n")
.parent(a.clone()),
);
assert_eq!(a, "run-1:0");
assert_eq!(b, "run-1:1");
let events = j.events();
assert_eq!(events[1].seq, 1);
assert_eq!(events[1].parent.as_deref(), Some("run-1:0"));
assert_eq!(events[1].node_id.as_deref(), Some("n"));
assert_eq!(j.len(), 2);
assert!(!j.is_empty());
}
#[test]
fn large_payloads_are_content_addressed_and_resolvable() {
let j = journal();
let big = json!({"blob": "x".repeat(INLINE_PAYLOAD_MAX_BYTES)});
j.record(
EventDraft::new(RunEventKind::ModelCall, Effect::NonIdempotent).output(big.clone()),
);
let event = &j.events()[0];
let reference = match event.output.as_ref() {
Some(PayloadRef::Artifact(reference)) => reference.clone(),
other => panic!("expected artifact reference, got {other:?}"),
};
assert_eq!(
reference.bytes as usize,
serde_json::to_vec(&big).unwrap().len()
);
assert_eq!(j.resolve(event.output.as_ref().unwrap()), Some(big));
}
#[test]
fn head_hash_advances_and_binds_content() {
let j1 = journal();
let j2 = journal();
assert_eq!(j1.head_hash(), j2.head_hash());
let draft = || EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure);
j1.record(draft());
assert_ne!(j1.head_hash(), j2.head_hash());
j2.record(draft());
assert_ne!(j1.head_hash(), j2.head_hash());
}
#[test]
fn snapshot_roundtrip_reverifies_head() {
let j = journal();
j.record(
EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure).input(json!({"step": 0})),
);
j.record(
EventDraft::new(RunEventKind::NodeInput, Effect::Pure)
.node("a")
.output(json!({"blob": "y".repeat(INLINE_PAYLOAD_MAX_BYTES)})),
);
let snapshot = j.snapshot();
let wire = serde_json::to_string(&snapshot).unwrap();
let parsed: JournalSnapshot = serde_json::from_str(&wire).unwrap();
let rebuilt = Journal::from_snapshot(parsed, Clock::System).unwrap();
assert_eq!(rebuilt.head_hash(), j.head_hash());
assert_eq!(rebuilt.events(), j.events());
let mut tampered = j.snapshot();
tampered.events[0].status = EventStatus::Error;
assert!(Journal::from_snapshot(tampered, Clock::System).is_err());
}
#[test]
fn head_hash_is_stable_across_payload_key_order() {
let forward: Value =
serde_json::from_str(r#"{"alpha": 1, "beta": {"x": 1, "y": 2}, "gamma": [3]}"#)
.unwrap();
let reverse: Value =
serde_json::from_str(r#"{"gamma": [3], "beta": {"y": 2, "x": 1}, "alpha": 1}"#)
.unwrap();
assert_eq!(forward, reverse);
let journal_a = Journal::new("run-1", "thread-1", Clock::logical(1_000_000, 5));
let journal_b = Journal::new("run-1", "thread-1", Clock::logical(1_000_000, 5));
journal_a.record(
EventDraft::new(RunEventKind::NodeInput, Effect::Pure)
.node("n")
.input(forward.clone())
.output(reverse.clone()),
);
journal_b.record(
EventDraft::new(RunEventKind::NodeInput, Effect::Pure)
.node("n")
.input(reverse)
.output(forward),
);
assert_eq!(journal_a.events(), journal_b.events());
assert_eq!(journal_a.head_hash(), journal_b.head_hash());
let snapshot: JournalSnapshot =
serde_json::from_str(&serde_json::to_string(&journal_a.snapshot()).unwrap()).unwrap();
let rebuilt = Journal::from_snapshot(snapshot, Clock::System).unwrap();
assert_eq!(rebuilt.head_hash(), journal_a.head_hash());
}
#[test]
fn logical_clock_ticks_deterministically() {
let clock = Clock::logical(1_000_000, 5);
let a = clock.now();
let b = clock.now();
assert_eq!(b.timestamp_millis() - a.timestamp_millis(), 5);
assert_eq!(a.timestamp_millis(), 1_000_000);
let same = Clock::logical(1_000_000, 5);
assert_eq!(same.now(), a);
assert_eq!(same.now(), b);
}
#[test]
fn seeded_rng_mints_reproducible_v4_uuids() {
let a = RngSource::seeded(42);
let b = RngSource::seeded(42);
assert_eq!(a.uuid(), b.uuid());
assert_eq!(a.uuid(), b.uuid());
let id = a.uuid();
assert_eq!(id.get_version(), Some(uuid::Version::Random));
assert_ne!(RngSource::seeded(1).uuid(), RngSource::seeded(2).uuid());
}
#[test]
fn system_defaults_match_pre_r05_shapes() {
assert!(matches!(Clock::default(), Clock::System));
assert!(matches!(RngSource::default(), RngSource::System));
}
fn receipt(key: &str) -> EffectReceipt {
EffectReceipt {
provider: "stripe".into(),
provider_id: "ch_3PKd".into(),
idempotency_key: key.into(),
task_id: Some("task-9".into()),
effect_id: None,
}
}
#[test]
fn effect_receipt_records_with_parentage_and_is_findable_by_key() {
let j = journal();
let step = j.record(EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure));
let event_id = j.record_effect_receipt(&receipt("run-1:charge:7"), Some(step.clone()));
let events = j.events();
let recorded = events.last().unwrap();
assert_eq!(recorded.id, event_id);
assert_eq!(recorded.kind, RunEventKind::EffectReceipt);
assert_eq!(recorded.effect, Effect::Idempotent);
assert_eq!(recorded.parent.as_deref(), Some(step.as_str()));
let snapshot = j.snapshot();
assert_eq!(
snapshot.find_effect_receipt("run-1:charge:7"),
Some(receipt("run-1:charge:7"))
);
assert_eq!(snapshot.find_effect_receipt("run-1:charge:8"), None);
let rebuilt = Journal::from_snapshot(snapshot, Clock::System).unwrap();
let snapshot = rebuilt.snapshot();
assert_eq!(
snapshot.find_effect_receipt("run-1:charge:7"),
Some(receipt("run-1:charge:7"))
);
}
#[test]
fn effect_receipt_records_without_a_parent() {
let j = journal();
j.record_effect_receipt(&receipt("k"), None);
let event = j.events().pop().unwrap();
assert_eq!(event.parent, None);
}
#[test]
fn effect_receipt_is_findable_by_effect_id() {
use crate::effects::derive_effect_id;
let j = journal();
let committed = derive_effect_id(
"run-1",
"charge_card",
&sha256_hex(b"input"),
Some("run-1:charge:7"),
);
let mut with_id = receipt("run-1:charge:7");
with_id.effect_id = Some(committed.as_str().to_owned());
j.record_effect_receipt(&with_id, None);
j.record_effect_receipt(&receipt("run-1:legacy:1"), None);
let snapshot = j.snapshot();
assert_eq!(
snapshot.find_effect_receipt_by_effect_id(&committed),
Some(with_id)
);
let other = derive_effect_id(
"run-1",
"charge_card",
&sha256_hex(b"input"),
Some("run-1:charge:8"),
);
assert_eq!(snapshot.find_effect_receipt_by_effect_id(&other), None);
assert!(snapshot
.find_effect_receipt_by_effect_id(&derive_effect_id("run-1", "legacy", "x", None))
.is_none());
}
struct TestDir(PathBuf);
impl TestDir {
fn new() -> Self {
Self(std::env::temp_dir().join(format!("rusty-artifact-test-{}", uuid::Uuid::new_v4())))
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn big_payload() -> Value {
json!({"blob": "x".repeat(INLINE_PAYLOAD_MAX_BYTES)})
}
#[tokio::test]
async fn file_artifact_store_roundtrip_and_dedupe() {
let tmp = TestDir::new();
let store = FileArtifactStore::new(tmp.0.clone());
let bytes = serde_json::to_vec(&big_payload()).unwrap();
let reference = store.put(&bytes).await.unwrap();
assert_eq!(reference.sha256, sha256_hex(&bytes));
assert_eq!(reference.bytes as usize, bytes.len());
assert!(store.contains(&reference.sha256).await.unwrap());
assert!(!store.contains(&sha256_hex(b"never stored")).await.unwrap());
let again = store.put(&bytes).await.unwrap();
assert_eq!(again, reference);
assert_eq!(std::fs::read_dir(&tmp.0).unwrap().count(), 1);
assert_eq!(store.get(&reference.sha256).await.unwrap(), bytes);
}
#[tokio::test]
async fn file_artifact_store_detects_corruption_on_read() {
let tmp = TestDir::new();
let store = FileArtifactStore::new(tmp.0.clone());
let bytes = b"genuine payload".repeat(1024);
let reference = store.put(&bytes).await.unwrap();
std::fs::write(tmp.0.join(&reference.sha256), b"tampered").unwrap();
let err = store.get(&reference.sha256).await.unwrap_err();
assert!(err.to_string().contains("integrity failure"), "got: {err}");
assert!(store.get(&sha256_hex(b"missing")).await.is_err());
}
#[tokio::test]
async fn externalized_snapshot_spills_and_resolves_artifacts() {
let tmp = TestDir::new();
let store = FileArtifactStore::new(tmp.0.clone());
let j = journal();
let big = big_payload();
j.record(EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure));
j.record(
EventDraft::new(RunEventKind::ModelCall, Effect::NonIdempotent).output(big.clone()),
);
let externalized = j.snapshot_externalized(&store).await.unwrap();
assert!(externalized.artifacts.is_empty());
assert_eq!(externalized.artifact_refs.len(), 1);
let wire = serde_json::to_string(&externalized).unwrap();
assert!(wire.contains("\"artifact_refs\""));
let event = &externalized.events[1];
let Some(PayloadRef::Artifact(reference)) = event.output.as_ref() else {
panic!("large payload must be artifact-referenced");
};
assert!(store.contains(&reference.sha256).await.unwrap());
let parsed: JournalSnapshot = serde_json::from_str(&wire).unwrap();
assert!(Journal::from_snapshot(parsed, Clock::System).is_err());
let parsed: JournalSnapshot = serde_json::from_str(&wire).unwrap();
let rebuilt = Journal::from_snapshot_with_store(parsed, &store, Clock::System)
.await
.unwrap();
assert_eq!(rebuilt.head_hash(), j.head_hash());
assert_eq!(
rebuilt.resolve(rebuilt.events()[1].output.as_ref().unwrap()),
Some(big)
);
let re = rebuilt.snapshot_externalized(&store).await.unwrap();
assert_eq!(re.artifact_refs, externalized.artifact_refs);
}
#[test]
fn embedded_byte_snapshots_keep_deserializing() {
let j = journal();
j.record(
EventDraft::new(RunEventKind::SuperStepStart, Effect::Pure).input(json!({"s": 0})),
);
let snapshot = j.snapshot();
let mut wire: serde_json::Map<String, Value> =
serde_json::from_str(&serde_json::to_string(&snapshot).unwrap()).unwrap();
assert!(
wire.remove("artifact_refs").is_none(),
"embedded snapshots must not emit artifact_refs"
);
let text = serde_json::to_string(&Value::Object(wire)).unwrap();
let parsed: JournalSnapshot = serde_json::from_str(&text).unwrap();
assert!(parsed.artifact_refs.is_empty());
let rebuilt = Journal::from_snapshot(parsed, Clock::System).unwrap();
assert_eq!(rebuilt.head_hash(), j.head_hash());
}
}