A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
pa-daemon: cache the ledger artifact index and deleted bucket per replay (#3619)
Sebastian Müllerb9c1c2ffbf76
diff --git a/crates/pa-daemon/src/rlm_ledger.rs b/crates/pa-daemon/src/rlm_ledger.rsindex 7f8d8edce3..0b81c1c49f 100644--- a/crates/pa-daemon/src/rlm_ledger.rs+++ b/crates/pa-daemon/src/rlm_ledger.rs@@ -20,7 +20,7 @@ use std::fs::{self, File, OpenOptions};use std::io::{Read, Seek, SeekFrom, Write};use std::path::{Path, PathBuf};use std::sync::atomic::{AtomicBool, Ordering};-use std::sync::{Arc, Mutex};+use std::sync::{Arc, Mutex, OnceLock};use anyhow::{bail, Context, Result};use serde::{Deserialize, Serialize};@@ -33,7 +33,7 @@ mod replay;use replay::{canonicalize_dir, edge_key, file_identity, is_file, parse_ledger_line, sole_edge_by_child_id,- LedgerRecord, LivePathResolver, ReplaySnapshot, ReplayState,+ DeletedBucket, LedgerRecord, LivePathResolver, ReplaySnapshot, ReplayState,};mod legacy_registry;@@ -133,15 +133,18 @@ pub fn rlm_ledger_path(agent_dir: &Path, sessions_dir: &Path) -> PathBuf {/// resolve; the survivors' recorded endpoints do - a dead endpoint drops/// the edge, a moved path resolves through its durable session id - and/// the resolved paths replace the recorded ones.-fn resolve_live_edges(state: &ReplayState, resolver: &mut LivePathResolver) -> Vec<RlmLedgerEdge> {+fn resolve_live_edges(+ state: &ReplayState,+ resolver: &mut LivePathResolver,+) -> Result<Vec<RlmLedgerEdge>> {let mut edges = Vec::with_capacity(state.edges.len());for edge in &state.edges {if edge.deleted.is_some() {continue;}let (Some(child), Some(parent)) = (- resolver.resolve(&edge.child),- resolver.resolve(&edge.parent),+ resolver.resolve(&edge.child)?,+ resolver.resolve(&edge.parent)?,) else {continue;};@@ -151,7 +154,7 @@ fn resolve_live_edges(state: &ReplayState, resolver: &mut LivePathResolver) -> V..edge.clone()});}- edges+ Ok(edges)}/// Fold parsed records into the replayed edge set, keeping only the@@ -477,19 +480,45 @@ impl RlmSpawnLedger {/// # Errors////// Returns an error when the ledger replay fails (an oversized- /// ledger).+ /// ledger) or a prior panic poisoned the deleted-usage cache.pub fn deleted_descendant_usage_by_parent(&self,) -> Result<HashMap<String, crate::session_usage::SessionUsageSummary>> {+ self.seed_once()?;+ let state = self.replay_cached()?;+ let inputs = state+ .edges+ .iter()+ .filter(|edge| edge.deleted.is_some())+ .map(|edge| file_identity(Path::new(&edge.child)))+ .collect::<Result<Vec<_>>>()?;+ let mut cached = state+ .deleted_bucket+ .lock()+ .map_err(|_| anyhow::anyhow!("deleted bucket lock poisoned"))?;+ if let Some(bucket) = cached.as_ref().filter(|bucket| bucket.inputs == inputs) {+ return Ok(bucket.usage.clone());+ }+ let usage = self.fold_deleted_descendant_usage(&state.edges);+ *cached = Some(DeletedBucket {+ inputs,+ usage: usage.clone(),+ });+ Ok(usage)+ }++ fn fold_deleted_descendant_usage(+ &self,+ edges: &[RlmLedgerEdge],+ ) -> HashMap<String, crate::session_usage::SessionUsageSummary> {use crate::session_usage::SessionUsageSummary;- let edges = self.edges(true)?;let canonical = |path: &str| {canonical_session_path(Path::new(path)).to_string_lossy().to_string()};let mut live_paths: HashSet<String> = HashSet::new();- for edge in &edges {+ for edge in edges {if edge.deleted.is_none() {live_paths.insert(canonical(&edge.child));}@@ -498,7 +527,7 @@ impl RlmSpawnLedger {// the child (and its snapshot) for its parent.let mut children_by_parent: HashMap<String, Vec<String>> = HashMap::new();let mut snapshot_by_path: HashMap<String, Option<SessionUsageSummary>> = HashMap::new();- for edge in &edges {+ for edge in edges {if edge.deleted.is_none() {continue;}@@ -612,7 +641,7 @@ impl RlmSpawnLedger {usage_by_parent.insert(parent.clone(), total);}}- Ok(usage_by_parent)+ usage_by_parent}/// Replay edges without liveness reconciliation. Deleted edges are@@ -644,9 +673,12 @@ impl RlmSpawnLedger {pub fn live_edges(&self) -> Result<Vec<RlmLedgerEdge>> {self.seed_once()?;let state = self.replay_cached()?;- let mut resolver =- LivePathResolver::new(self.agent_dir.clone(), self.canonical_sessions_dir.clone());- Ok(resolve_live_edges(&state, &mut resolver))+ let mut resolver = LivePathResolver::new(+ self.agent_dir.clone(),+ self.canonical_sessions_dir.clone(),+ &state.artifact_index,+ );+ resolve_live_edges(&state, &mut resolver)}/// The opened parent file's live edges: only the records of children@@ -664,9 +696,12 @@ impl RlmSpawnLedger {self.seed_once()?;let parent_file = canonical_session_path(parent_file);let state = self.replay_of_parent(&parent_file)?;- let mut resolver =- LivePathResolver::new(self.agent_dir.clone(), self.canonical_sessions_dir.clone());- let mut edges = resolve_live_edges(&state, &mut resolver);+ let mut resolver = LivePathResolver::new(+ self.agent_dir.clone(),+ self.canonical_sessions_dir.clone(),+ &state.artifact_index,+ );+ let mut edges = resolve_live_edges(&state, &mut resolver)?;edges.retain(|edge| canonical_session_path(Path::new(&edge.parent)) == parent_file);Ok(edges)}diff --git a/crates/pa-daemon/src/rlm_ledger/replay.rs b/crates/pa-daemon/src/rlm_ledger/replay.rsindex 0a66622d66..22d9c3b82a 100644--- a/crates/pa-daemon/src/rlm_ledger/replay.rs+++ b/crates/pa-daemon/src/rlm_ledger/replay.rs@@ -5,7 +5,7 @@use anyhow::Context as _;use super::{- bail, canonical_session_path, fs, json, Arc, HashMap, Path, PathBuf, Result,+ bail, canonical_session_path, fs, json, Arc, HashMap, Mutex, OnceLock, Path, PathBuf, Result,RlmLedgerDeleteReason, RlmLedgerEdge, Value,};@@ -156,6 +156,16 @@ pub(super) fn parse_ledger_line(line: &str, index: usize) -> Result<Option<Ledgepub(super) struct ReplayState {pub(super) edges: Vec<RlmLedgerEdge>,pub(super) index: HashMap<String, usize>,+ pub(super) artifact_index: OnceLock<Mutex<ArtifactIndex>>,+ pub(super) deleted_bucket: Mutex<Option<DeletedBucket>>,+}++/// Ledger rows are immutable within a replay, but restore/archive and transcript+/// edits can change the inputs of the deleted-spend fold without any append.+#[derive(Debug)]+pub(super) struct DeletedBucket {+ pub(super) inputs: Vec<Option<FileIdentity>>,+ pub(super) usage: HashMap<String, crate::session_usage::SessionUsageSummary>,}pub(super) fn edge_key(child_id: &str, child: &str) -> String {@@ -167,69 +177,122 @@ pub(super) fn edge_key(child_id: &str, child: &str) -> String {/// One `live_edges` liveness pass: a recorded path that stats resolves/// as itself; a moved file resolves through its durable session id (the-/// file-name stem) against the sessions dir and the artifacts tree. The-/// per-pass cache keeps the artifact walk to one pass per read.-pub(super) struct LivePathResolver {+/// file-name stem) against the sessions dir and the artifacts tree.+pub(super) struct LivePathResolver<'a> {agent_dir: PathBuf,sessions_dir: PathBuf,pub(super) resolved: HashMap<String, Option<PathBuf>>,- pub(super) artifact_index: Option<HashMap<String, PathBuf>>,+ artifact_index: &'a OnceLock<Mutex<ArtifactIndex>>,+ artifact_checked: bool,}-impl LivePathResolver {- pub(super) fn new(agent_dir: PathBuf, sessions_dir: PathBuf) -> Self {+impl<'a> LivePathResolver<'a> {+ pub(super) fn new(+ agent_dir: PathBuf,+ sessions_dir: PathBuf,+ artifact_index: &'a OnceLock<Mutex<ArtifactIndex>>,+ ) -> Self {LivePathResolver {agent_dir,sessions_dir,resolved: HashMap::new(),- artifact_index: None,+ artifact_index,+ artifact_checked: false,}}/// The live session file for one recorded edge path, `None` when the/// session is gone everywhere (the edge endpoint is dead).- pub(super) fn resolve(&mut self, recorded: &str) -> Option<PathBuf> {+ pub(super) fn resolve(&mut self, recorded: &str) -> Result<Option<PathBuf>> {if let Some(hit) = self.resolved.get(recorded) {- return hit.clone();+ return Ok(hit.clone());}- let live = self.resolve_uncached(recorded);+ let live = self.resolve_uncached(recorded)?;self.resolved.insert(recorded.to_string(), live.clone());- live+ Ok(live)}- fn resolve_uncached(&mut self, recorded: &str) -> Option<PathBuf> {+ fn resolve_uncached(&mut self, recorded: &str) -> Result<Option<PathBuf>> {let recorded_path = Path::new(recorded);if is_file(recorded_path) {- return Some(recorded_path.to_path_buf());+ return Ok(Some(recorded_path.to_path_buf()));}- let id = recorded_path.file_stem()?.to_string_lossy().to_string();+ let Some(stem) = recorded_path.file_stem() else {+ return Ok(None);+ };+ let id = stem.to_string_lossy();if id.is_empty() {- return None;+ return Ok(None);}let sessions_candidate = self.sessions_dir.join(format!("{id}.jsonl"));if is_file(&sessions_candidate) {- return Some(sessions_candidate);+ return Ok(Some(sessions_candidate));+ }+ if is_file(+ &crate::session_archive::archive_dir(&self.agent_dir)+ .join(crate::session_store::session_file_name(&id)),+ ) {+ return Ok(None);}- let index = self+ let mut index = self.artifact_index- .get_or_insert_with(|| artifact_session_index(&self.agent_dir));- index.get(&id).cloned()+ .get_or_init(|| Mutex::new(artifact_session_index(&self.agent_dir)))+ .lock()+ .map_err(|_| anyhow::anyhow!("artifact index cache lock poisoned"))?;+ // Directory identities cover additions, deletion and relocation, including+ // new partitions. Check once per read; unchanged misses retain the shared+ // negative result instead of enumerating every artifact file again.+ if !self.artifact_checked {+ if index+ .directories+ .iter()+ .any(|(path, identity)| path_identity(path) != *identity)+ {+ *index = artifact_session_index(&self.agent_dir);+ }+ self.artifact_checked = true;+ }+ Ok(index+ .paths+ .get(id.as_ref())+ .filter(|path| is_file(path))+ .cloned())}}+#[derive(Debug, Default)]+pub(super) struct ArtifactIndex {+ paths: HashMap<String, PathBuf>,+ directories: Vec<(PathBuf, Option<FileIdentity>)>,+}++#[cfg(test)]+thread_local! {+ pub(super) static ARTIFACT_SCANS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };+}+/// The session files under the artifacts tree, keyed by their durable session id (the file-name/// stem); non-session `.jsonl` sidecars key by their own stems and never collide.-fn artifact_session_index(agent_dir: &Path) -> HashMap<String, PathBuf> {- let mut index = HashMap::new();+fn artifact_session_index(agent_dir: &Path) -> ArtifactIndex {+ #[cfg(test)]+ ARTIFACT_SCANS.with(|count| count.set(count.get() + 1));+ let mut index = ArtifactIndex::default();let root = agent_dir.join(crate::context_tree_children::RLM_SESSION_ARTIFACTS_DIR);+ index.directories.push((root.clone(), path_identity(&root)));let Ok(parents) = std::fs::read_dir(&root) else {return index;};for parent in parents.flatten() {+ index+ .directories+ .push((parent.path(), path_identity(&parent.path())));let Ok(subs) = std::fs::read_dir(parent.path()) else {continue;};for sub in subs.flatten() {+ index+ .directories+ .push((sub.path(), path_identity(&sub.path())));let Ok(files) = std::fs::read_dir(sub.path()) else {continue;};@@ -237,7 +300,7 @@ fn artifact_session_index(agent_dir: &Path) -> HashMap<String, PathBuf> {let path = file.path();if path.extension().and_then(|ext| ext.to_str()) == Some("jsonl") {if let Some(stem) = path.file_stem() {- index.insert(stem.to_string_lossy().to_string(), path);+ index.paths.insert(stem.to_string_lossy().to_string(), path);}}}@@ -307,10 +370,14 @@ pub(super) fn sole_edge_by_child_id(state: &ReplayState, child_id: &str) -> Opti}pub(super) fn file_identity(path: &Path) -> Result<Option<FileIdentity>> {+ Ok(path_identity(path))+}++fn path_identity(path: &Path) -> Option<FileIdentity> {let Ok(metadata) = fs::metadata(path) else {- return Ok(None);+ return None;};- Ok(Some(FileIdentity {+ Some(FileIdentity {size: metadata.len(),mtime: metadata.modified().ok(),#[cfg(unix)]@@ -318,7 +385,7 @@ pub(super) fn file_identity(path: &Path) -> Result<Option<FileIdentity>> {use std::os::unix::fs::MetadataExt;Some(metadata.ino())},- }))+ })}pub(super) fn is_file(path: &Path) -> bool {diff --git a/crates/pa-daemon/src/rlm_ledger/tests.rs b/crates/pa-daemon/src/rlm_ledger/tests.rsindex da3c2199f0..e0e147fffb 100644--- a/crates/pa-daemon/src/rlm_ledger/tests.rs+++ b/crates/pa-daemon/src/rlm_ledger/tests.rs@@ -356,6 +356,64 @@ fn moved_edge_paths_resolve_through_the_session_id() {assert_eq!(raw[0].child, recorded_child);}+#[test]+fn the_artifact_index_updates_after_a_new_partition() {+ let agent_dir = temp_dir("index-partition");+ let sessions_dir = agent_dir.join("sessions");+ fs::create_dir_all(&sessions_dir).unwrap();+ let ledger = ledger_over(&agent_dir, &sessions_dir);++ let parent = sessions_dir.join("sess-p.jsonl");+ fs::write(&parent, "{}").unwrap();+ let first = agent_dir+ .join("session-artifacts")+ .join("sess-p")+ .join("sub-1")+ .join("sess-1.jsonl");+ fs::create_dir_all(first.parent().unwrap()).unwrap();+ fs::write(&first, "{}").unwrap();+ let spawn = |child_id: &str, child: &str| {+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: child_id.into(),+ parent: parent.to_string_lossy().into(),+ child: child.into(),+ depth: 1,+ name: "w".into(),+ })+ .unwrap();+ };+ spawn("sub-1", "/gone-root/sess-1.jsonl");+ let edges = ledger.live_edges().unwrap();+ assert_eq!(edges.len(), 1, "{edges:?}");+ assert_eq!(+ edges[0].child,+ canonical_session_path(&first).to_string_lossy().to_string()+ );++ let second = agent_dir+ .join("session-artifacts")+ .join("sess-p")+ .join("sub-2")+ .join("sess-2.jsonl");+ fs::create_dir_all(second.parent().unwrap()).unwrap();+ fs::write(&second, "{}").unwrap();+ spawn("sub-2", "/gone-root/sess-2.jsonl");+ let edges = ledger.live_edges().unwrap();+ assert_eq!(edges.len(), 2, "{edges:?}");+ let appended = edges+ .iter()+ .find(|edge| edge.child_id == "sub-2")+ .expect("the appended edge");+ assert_eq!(+ appended.child,+ canonical_session_path(&second)+ .to_string_lossy()+ .to_string(),+ "the new partition resolves after its append: {edges:?}"+ );+}+/// The parent-scoped live read bounds the liveness work to the opened/// parent's own edges: foreign edges with missing endpoints never/// resolve (no per-edge stats, no artifacts-tree walk), while the own@@ -434,8 +492,12 @@ fn live_edges_of_parent_bounds_the_liveness_work() {);let state = ledger.replay_of_parent(&parent).unwrap();- let mut resolver = LivePathResolver::new(agent_dir.clone(), sessions_dir.clone());- let scoped = resolve_live_edges(&state, &mut resolver);+ let mut resolver = LivePathResolver::new(+ agent_dir.clone(),+ sessions_dir.clone(),+ &state.artifact_index,+ );+ let scoped = resolve_live_edges(&state, &mut resolver).unwrap();assert_eq!(scoped.len(), 2, "{scoped:?}");assert_eq!(resolver.resolved.len(),@@ -444,16 +506,52 @@ fn live_edges_of_parent_bounds_the_liveness_work() {resolver.resolved.keys().collect::<Vec<_>>());assert!(- resolver.artifact_index.is_none(),+ state.artifact_index.get().is_none(),"no foreign miss walks the artifacts tree");let full = ledger.replay_cached().unwrap();- let mut unscoped = LivePathResolver::new(agent_dir, sessions_dir);- assert_eq!(resolve_live_edges(&full, &mut unscoped).len(), 2);- assert!(unscoped.artifact_index.is_some());+ let mut unscoped = LivePathResolver::new(agent_dir, sessions_dir, &full.artifact_index);+ assert_eq!(resolve_live_edges(&full, &mut unscoped).unwrap().len(), 2);+ assert!(full.artifact_index.get().is_some());assert_eq!(unscoped.resolved.len(), 103);}+#[test]+fn a_dead_endpoint_under_an_archived_parent_resolves_without_a_tree_walk() {+ let agent_dir = temp_dir("archive-dead");+ let sessions_dir = agent_dir.join("sessions");+ fs::create_dir_all(&sessions_dir).unwrap();+ let ledger = ledger_over(&agent_dir, &sessions_dir);+ let child = sessions_dir.join("sess-c.jsonl");+ fs::write(&child, "{}").unwrap();+ let archived = crate::session_archive::archive_dir(&agent_dir)+ .join(crate::session_store::session_file_name("sess-p"));+ fs::create_dir_all(archived.parent().unwrap()).unwrap();+ fs::write(&archived, "{}").unwrap();+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: "sub-1".into(),+ parent: "/old-root/sessions/sess-p.jsonl".into(),+ child: child.to_string_lossy().into(),+ depth: 1,+ name: "w".into(),+ })+ .unwrap();+ assert!(+ ledger.live_edges().unwrap().is_empty(),+ "an archived parent is a dead endpoint"+ );+ assert!(+ ledger+ .replay_cached()+ .unwrap()+ .artifact_index+ .get()+ .is_none(),+ "the archive stat answers without the artifacts walk"+ );+}+/// A parent that moved (recorded path gone, same stem under the sessions/// dir) still lists its children through the scoped read; an edge whose/// recorded parent merely shares the opened file's stem but resolves to@@ -1254,3 +1352,179 @@ fn bucket_skips_recreated_live_paths() {let bucket = ledger.deleted_descendant_usage_by_parent().unwrap();assert!(bucket.is_empty(), "a live path never bills the bucket");}++#[test]+fn the_bucket_updates_after_an_append_on_the_same_ledger() {+ let dir = temp_dir("bucket-append");+ let ledger = ledger_for(&dir);+ let sessions = dir.join("sessions");+ fs::create_dir_all(&sessions).unwrap();+ let parent = sessions.join("p.jsonl");+ let child_1 = sessions.join("c1.jsonl");+ let child_2 = sessions.join("c2.jsonl");+ fs::write(&parent, "{}").unwrap();+ fs::write(&child_1, "{}").unwrap();+ fs::write(&child_2, "{}").unwrap();+ let parent_key = crate::lease::canonical_session_path(&parent)+ .to_string_lossy()+ .to_string();+ let spawn_delete = |child_id: &str, child: &Path, cost: f64| {+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: child_id.into(),+ parent: parent.to_string_lossy().into(),+ child: child.to_string_lossy().into(),+ depth: 1,+ name: "w".into(),+ })+ .unwrap();+ ledger+ .append_delete_with_usage(+ child_id,+ &child.to_string_lossy(),+ RlmLedgerDeleteReason::User,+ &usage_summary(cost),+ )+ .unwrap();+ fs::remove_file(child).unwrap();+ };+ spawn_delete("sub-1", &child_1, 0.25);+ let bucket = ledger.deleted_descendant_usage_by_parent().unwrap();+ assert_eq!(+ bucket,+ HashMap::from([(parent_key.clone(), usage_summary(0.25))]),+ "the first delete bills: {bucket:?}"+ );+ spawn_delete("sub-2", &child_2, 0.50);+ let bucket = ledger.deleted_descendant_usage_by_parent().unwrap();+ assert_eq!(+ bucket,+ HashMap::from([(+ parent_key,+ SessionUsageSummary {+ input_tokens: 2_000,+ output_tokens: 200,+ cost: 0.75,+ }+ )]),+ "the appended delete bills on the same instance: {bucket:?}"+ );+}++#[test]+fn a_restored_tombstoned_child_stops_billing_the_parent_without_an_append() {+ let dir = temp_dir("bucket-restore");+ let sessions = dir.join("sessions");+ fs::create_dir_all(&sessions).unwrap();+ let parent = sessions.join("parent.jsonl");+ let child = sessions.join("child.jsonl");+ fs::write(&parent, "{}").unwrap();+ fs::write(&child, assistant_usage_row("m1", 0.25)).unwrap();+ let ledger = ledger_for(&dir);+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: "sub-child".into(),+ parent: parent.to_string_lossy().into(),+ child: child.to_string_lossy().into(),+ depth: 1,+ name: "child".into(),+ })+ .unwrap();+ ledger+ .append_delete_with_usage(+ "sub-child",+ &child.to_string_lossy(),+ RlmLedgerDeleteReason::User,+ &usage_summary(0.25),+ )+ .unwrap();+ let archive = crate::session_archive::archive_dir(&dir);+ fs::create_dir_all(&archive).unwrap();+ let archived = archive.join(crate::session_store::session_file_name("child"));+ fs::rename(&child, &archived).unwrap();+ let key = canonical_session_path(&parent)+ .to_string_lossy()+ .into_owned();+ assert_eq!(+ ledger.deleted_descendant_usage_by_parent().unwrap(),+ HashMap::from([(key, usage_summary(0.25))])+ );+ crate::session_archive::restore_session(&archive, &sessions, "child").unwrap();+ assert_eq!(+ ledger.deleted_descendant_usage_by_parent().unwrap(),+ HashMap::new()+ );+}++#[test]+fn the_artifact_index_rechecks_deleted_and_relocated_transcripts_without_an_append() {+ let dir = temp_dir("index-disk-change");+ let sessions = dir.join("sessions");+ fs::create_dir_all(&sessions).unwrap();+ let parent = sessions.join("parent.jsonl");+ fs::write(&parent, "{}").unwrap();+ let child = dir.join("session-artifacts/parent/sub/child.jsonl");+ fs::create_dir_all(child.parent().unwrap()).unwrap();+ fs::write(&child, "{}").unwrap();+ let ledger = ledger_for(&dir);+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: "sub".into(),+ parent: parent.to_string_lossy().into(),+ child: "/gone/child.jsonl".into(),+ depth: 1,+ name: "child".into(),+ })+ .unwrap();+ let initial = ledger.live_edges().unwrap();+ assert_eq!(initial.len(), 1);+ fs::remove_file(&child).unwrap();+ assert_eq!(ledger.live_edges().unwrap(), Vec::new());+ let relocated = dir.join("session-artifacts/other/sub/child.jsonl");+ fs::create_dir_all(relocated.parent().unwrap()).unwrap();+ fs::write(&relocated, "{}").unwrap();+ let mut expected = initial;+ expected[0].child = canonical_session_path(&relocated)+ .to_string_lossy()+ .into_owned();+ assert_eq!(ledger.live_edges().unwrap(), expected);+}++#[test]+fn artifact_misses_stay_cached_until_a_directory_changes() {+ let dir = temp_dir("index-negative-cache");+ let sessions = dir.join("sessions");+ fs::create_dir_all(&sessions).unwrap();+ let parent = sessions.join("parent.jsonl");+ fs::write(&parent, "{}").unwrap();+ let partition = dir.join("session-artifacts/parent/sub");+ fs::create_dir_all(&partition).unwrap();+ let ledger = ledger_for(&dir);+ ledger+ .append_spawn(&RlmSpawnInput {+ child_id: "sub".into(),+ parent: parent.to_string_lossy().into(),+ child: "/gone/child.jsonl".into(),+ depth: 1,+ name: "child".into(),+ })+ .unwrap();+ let scans = replay::ARTIFACT_SCANS.with(std::cell::Cell::get);+ assert_eq!(ledger.live_edges().unwrap(), Vec::new());+ assert_eq!(ledger.live_edges().unwrap(), Vec::new());+ assert_eq!(replay::ARTIFACT_SCANS.with(std::cell::Cell::get), scans + 1);+ let restored = partition.join("child.jsonl");+ fs::write(&restored, "{}").unwrap();+ let expected = vec![RlmLedgerEdge {+ child_id: "sub".into(),+ parent: canonical_session_path(&parent).to_string_lossy().into(),+ child: canonical_session_path(&restored).to_string_lossy().into(),+ depth: 1,+ name: "child".into(),+ deleted: None,+ deleted_usage: None,+ }];+ assert_eq!(ledger.live_edges().unwrap(), expected);+ assert_eq!(ledger.live_edges().unwrap(), expected);+ assert_eq!(replay::ARTIFACT_SCANS.with(std::cell::Cell::get), scans + 2);+}diff --git a/crates/pa-daemon/src/stop_cleanup.rs b/crates/pa-daemon/src/stop_cleanup.rsindex bca4225cda..39bf46f4a4 100644--- a/crates/pa-daemon/src/stop_cleanup.rs+++ b/crates/pa-daemon/src/stop_cleanup.rs@@ -699,6 +699,13 @@ mod tests {// The transcript dies (the sweep or a later delete): the bucket's// snapshot is now the only carrier — the spend survives.std::fs::remove_file(&child_file).unwrap();+ ledger+ .append_delete(+ "sub-1",+ &child_file.to_string_lossy(),+ crate::rlm_ledger::RlmLedgerDeleteReason::User,+ )+ .unwrap();let bucket = ledger.deleted_descendant_usage_by_parent().unwrap();assert!((bucket[&parent_key].cost - 0.06).abs() < 1e-9);}