rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Which job records the daemon forgets: the oldest ended subagent and
//! workflow runs beyond the newest [`ENDED_RUNS_KEPT`]. Without a cap the
//! store, and every `rho/job/list`, grows by each run a session starts.
//!
//! Session jobs are never forgotten here: a session's job record holds its
//! worktree and grant, which reopening the session reads. Nor is a run that
//! a kept job descends from, which `runs_under` walks to find the session
//! it runs under, so the cap may keep a few more.

use std::collections::{BTreeMap, HashSet};

use super::{JobId, JobRecord, JobSubject};

/// How many ended subagent and workflow runs the daemon keeps.
///
/// Measured on a heavy user's daemon (2026-11-30): 508 ended subagent runs
/// over 8 days, 74 a day at the median and 100 on the busiest day, with
/// record files of 358 bytes at the median and 10 KB at most. 1000 keeps at
/// least ten days of runs at that busiest rate, so `/jobs` history and the
/// runs a session reads back stay listed, while a `rho/job/list` of every
/// job stays near its measured 2.3 ms for 786 jobs.
pub(super) const ENDED_RUNS_KEPT: usize = 1000;

/// The ended subagent and workflow runs in `jobs` beyond the newest
/// `kept`, oldest first, skipping any run a job that stays descends from:
/// one under way, a session's, or a newer run.
pub(super) fn beyond_kept(jobs: &BTreeMap<JobId, JobRecord>, kept: usize) -> Vec<JobId> {
    let mut ended: Vec<&JobRecord> = jobs
        .values()
        .filter(|record| {
            let run = match record.subject {
                JobSubject::Session { .. } => false,
                JobSubject::Subagent { .. } | JobSubject::Workflow { .. } => true,
            };
            run && !record.state.in_progress()
        })
        .collect();
    ended.sort_by(|a, b| (a.created_at, &a.id).cmp(&(b.created_at, &b.id)));
    let beyond: HashSet<&JobId> = ended
        .iter()
        .take(ended.len().saturating_sub(kept))
        .map(|record| &record.id)
        .collect();
    // `runs_under` walks a job's parents to the session it runs under, so
    // a job that stays keeps every run it descends from.
    let mut ancestors: HashSet<&JobId> = HashSet::new();
    for record in jobs.values().filter(|record| !beyond.contains(&record.id)) {
        let mut parent = record.parent.as_ref();
        while let Some(id) = parent {
            if !ancestors.insert(id) {
                break;
            }
            parent = jobs.get(id).and_then(|record| record.parent.as_ref());
        }
    }
    ended
        .into_iter()
        .filter(|record| beyond.contains(&record.id) && !ancestors.contains(&record.id))
        .map(|record| record.id.clone())
        .collect()
}

#[cfg(test)]
#[path = "retention_tests.rs"]
mod tests;