pub mod cache;
pub mod ironobs;
pub mod recording;
pub mod report;
use chrono::{DateTime, Utc};
use recording::{InProgress, Session};
use std::collections::{BTreeMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
pub const RECORDINGS_ENV: &str = "IRONMON_RECORDINGS_DIR";
pub const CLAUDE_CODE_ENV: &str = "IRONMON_CLAUDE_CODE_DIR";
pub const CODEX_ENV: &str = "IRONMON_CODEX_DIR";
pub const QUERY_DEADLINE: Duration = Duration::from_secs(2);
pub fn recordings_dir() -> PathBuf {
match std::env::var_os(RECORDINGS_ENV) {
Some(p) if !p.is_empty() => PathBuf::from(p),
_ => cache_root().join("agents").join("recordings"),
}
}
pub fn claude_code_dir() -> PathBuf {
match std::env::var_os(CLAUDE_CODE_ENV) {
Some(p) if !p.is_empty() => PathBuf::from(p),
_ => home_dir().join(".claude").join("projects"),
}
}
pub fn codex_dir() -> PathBuf {
match std::env::var_os(CODEX_ENV) {
Some(p) if !p.is_empty() => PathBuf::from(p),
_ => home_dir().join(".codex").join("sessions"),
}
}
fn cache_root() -> PathBuf {
#[cfg(windows)]
{
let base = std::env::var_os("LOCALAPPDATA")
.map(PathBuf::from)
.unwrap_or_else(|| home_dir().join("AppData").join("Local"));
base.join("ironmon")
}
#[cfg(not(windows))]
{
home_dir().join(".cache").join("ironmon")
}
}
fn home_dir() -> PathBuf {
std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
}
pub fn key_of(name: &str) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in name.bytes() {
h ^= u64::from(b);
h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Producer {
pub key: &'static str,
pub label: &'static str,
pub not_read: Option<&'static str>,
}
pub const KNOWN_PRODUCERS: &[Producer] = &[
Producer {
key: "claude_code",
label: "Claude Code",
not_read: None,
},
Producer {
key: "codex",
label: "Codex CLI",
not_read: None,
},
Producer {
key: "gemini_cli",
label: "Gemini CLI",
not_read: Some(
"not covered: Gemini CLI stores no chats on disk, so there is nothing to read",
),
},
];
pub const REMOTE_NOT_READ: &str =
"cloud and hosted agent sessions have no source readable from this machine";
pub const REMOTE_COST_POINTER: &str =
"provider cost and usage are on the Inference tab, from the provider's admin API";
#[derive(Debug, Clone, PartialEq)]
pub struct Scan {
pub dir: PathBuf,
pub dir_exists: bool,
pub files: usize,
pub file_bytes: u64,
pub corrupt_files: usize,
pub refused: BTreeMap<String, usize>,
pub files_truncated: usize,
pub sessions: Vec<Session>,
pub newest_written: Option<DateTime<Utc>>,
pub taken_at: DateTime<Utc>,
}
impl Scan {
pub fn empty(dir: &Path) -> Self {
Self {
dir: dir.to_path_buf(),
dir_exists: false,
files: 0,
file_bytes: 0,
corrupt_files: 0,
refused: BTreeMap::new(),
files_truncated: 0,
sessions: Vec::new(),
newest_written: None,
taken_at: Utc::now(),
}
}
pub fn is_complete(&self) -> bool {
self.files_truncated == 0
}
pub fn recordings(&self) -> usize {
self.sessions.len()
}
pub fn sessions_count(&self) -> usize {
self.sessions
.iter()
.filter(|s| recording::is_top_level(s))
.count()
}
pub fn active_count(&self) -> usize {
self.sessions
.iter()
.filter(|s| s.in_progress == InProgress::True)
.count()
}
pub fn unevidenced_count(&self) -> usize {
self.sessions
.iter()
.filter(|s| s.in_progress == InProgress::NotEvidenced)
.count()
}
pub fn subagent_count(&self) -> usize {
self.sessions.iter().filter(|s| s.is_subagent).count()
}
pub fn workflow_count(&self) -> usize {
self.sessions
.iter()
.filter(|s| recording::is_top_level(s) && s.child_count > 0)
.count()
}
pub fn orphan_count(&self) -> usize {
self.sessions.iter().filter(|s| s.parent_missing).count()
}
pub fn refused_count(&self) -> usize {
self.refused.values().sum()
}
pub fn input_tokens(&self) -> Option<u64> {
sum_opt(self.sessions.iter().map(|s| s.input_tokens))
}
pub fn output_tokens(&self) -> Option<u64> {
sum_opt(self.sessions.iter().map(|s| s.output_tokens))
}
pub fn producer_count(&self, key: &str) -> usize {
self.sessions.iter().filter(|s| s.producer == key).count()
}
pub fn producers(&self) -> Vec<String> {
let mut v: Vec<String> = self.sessions.iter().map(|s| s.producer.clone()).collect();
v.sort();
v.dedup();
v
}
}
fn sum_opt(values: impl Iterator<Item = Option<u64>>) -> Option<u64> {
values
.flatten()
.fold(None, |acc, v| Some(acc.unwrap_or(0).saturating_add(v)))
}
#[derive(Debug, Clone, Copy)]
pub struct Limits {
pub max_files: usize,
pub max_bytes: u64,
pub deadline: Option<Instant>,
}
pub const UNBOUNDED: Limits = Limits {
max_files: usize::MAX,
max_bytes: u64::MAX,
deadline: None,
};
pub fn query_limits() -> Limits {
Limits {
max_files: usize::MAX,
max_bytes: u64::MAX,
deadline: Some(Instant::now() + QUERY_DEADLINE),
}
}
pub fn scan_dir(dir: &Path, limits: Limits) -> Scan {
scan_with(dir, limits, &mut cache::Cache::default())
}
pub fn scan_with(dir: &Path, limits: Limits, cache: &mut cache::Cache) -> Scan {
let mut scan = Scan::empty(dir);
let Ok(read) = std::fs::read_dir(dir) else {
return scan;
};
scan.dir_exists = true;
let mut files: Vec<(String, PathBuf)> = read
.filter_map(Result::ok)
.filter_map(|e| {
let path = e.path();
let is_json = path
.extension()
.is_some_and(|x| x.eq_ignore_ascii_case("json"));
(is_json && path.is_file())
.then(|| (e.file_name().to_string_lossy().into_owned(), path))
})
.filter(|(name, _)| name != ironobs::LEGACY_OUTPUT)
.collect();
files.sort();
let mut live = HashSet::with_capacity(files.len());
let mut sessions = Vec::new();
for (name, path) in &files {
let key = key_of(name);
live.insert(key);
let past_deadline = limits.deadline.is_some_and(|d| Instant::now() >= d);
if past_deadline || scan.files >= limits.max_files {
scan.files_truncated += 1;
continue;
}
let Ok(meta) = std::fs::metadata(path) else {
scan.files += 1;
scan.corrupt_files += 1;
continue;
};
let len = meta.len();
if scan.file_bytes.saturating_add(len) > limits.max_bytes {
scan.files_truncated += 1;
continue;
}
let mtime = cache::mtime_ms(&meta);
let entry = match cache.entries.get(&key) {
Some(e) if e.len == len && e.mtime_ms == mtime => e.clone(),
_ => {
let parsed = cache::parse_file(path, key, len, mtime);
cache.entries.insert(key, parsed.clone());
parsed
}
};
scan.files += 1;
scan.file_bytes = scan.file_bytes.saturating_add(len);
if let Some(t) = cache::datetime_of_ms(mtime) {
scan.newest_written = Some(scan.newest_written.map_or(t, |n| n.max(t)));
}
if entry.corrupt {
scan.corrupt_files += 1;
continue;
}
for (reason, n) in &entry.refused {
*scan.refused.entry(reason.clone()).or_default() += n;
}
sessions.extend(
entry
.sessions
.into_iter()
.map(cache::StoredSession::into_session),
);
}
recording::link(&mut sessions);
cache.entries.retain(|k, _| live.contains(k));
scan.sessions = sessions;
scan
}
pub fn query(dir: &Path) -> Scan {
let mut cache = cache::load(dir);
scan_with(dir, query_limits(), &mut cache)
}
pub fn producer_key(raw: &str) -> String {
recording::sanitise_producer(Some(raw))
}
pub fn is_active(s: &Session) -> bool {
s.in_progress == InProgress::True
}
pub fn relative(t: DateTime<Utc>, now: DateTime<Utc>) -> String {
let secs = (now - t).num_seconds();
let (n, future) = if secs < 0 {
(-secs, true)
} else {
(secs, false)
};
let body = if n < 45 {
return "just now".to_string();
} else if n < 3600 {
format!("{}m", (n + 30) / 60)
} else if n < 86_400 {
format!("{}h", (n + 1800) / 3600)
} else {
format!("{}d", (n + 43_200) / 86_400)
};
if future {
format!("in {body}")
} else {
format!("{body} ago")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EngineRequests {
pub active: Option<u64>,
pub recent: Option<usize>,
pub reachable: bool,
pub ever_answered: bool,
}
pub fn engine_requests(states: &[crate::inference::SourceState]) -> Option<EngineRequests> {
let st = states
.iter()
.find(|s| s.kind == crate::inference::ProviderKind::IronWorks)?;
let section = st.latest.as_ref().and_then(|s| s.ironworks.as_ref());
Some(EngineRequests {
active: section.and_then(|s| s.requests_active),
recent: section.and_then(|s| s.recent.as_ref().map(|r| r.len())),
reachable: st.reachable,
ever_answered: st.latest.is_some(),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn write(dir: &Path, name: &str, body: &str) {
std::fs::write(dir.join(name), body).unwrap();
}
fn tmp(name: &str) -> PathBuf {
let dir =
std::env::temp_dir().join(format!("ironmon-agents-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn a_missing_directory_is_not_an_empty_one() {
let dir = std::env::temp_dir().join("ironmon-agents-definitely-absent-dir");
let scan = query(&dir);
assert!(!scan.dir_exists);
assert_eq!(scan.recordings(), 0);
}
#[test]
fn an_empty_directory_exists_and_holds_nothing() {
let dir = tmp("empty");
let scan = query(&dir);
assert!(scan.dir_exists);
assert_eq!(scan.recordings(), 0);
assert_eq!(scan.files, 0);
}
#[test]
fn a_corrupt_file_is_reported_and_the_rest_are_read() {
let dir = tmp("corrupt");
write(
&dir,
"a_good.json",
r#"[{"run":"a","in_progress":true,"events":[]}]"#,
);
write(&dir, "b_bad.json", "{ this is not json");
write(
&dir,
"c_good.json",
r#"{"run":"c","in_progress":false,"events":[]}"#,
);
write(&dir, "notes.txt", "ignored, not a recording file");
let scan = query(&dir);
assert_eq!(scan.files, 3);
assert_eq!(scan.corrupt_files, 1);
assert_eq!(scan.recordings(), 2);
assert_eq!(scan.active_count(), 1);
}
#[test]
fn unknown_format_is_refused_and_counted_beside_good_ones() {
let dir = tmp("refused");
write(
&dir,
"f.json",
r#"[{"format":"ironobserver.recording/3","run":"n","events":[]},{"run":"o","events":[]}]"#,
);
let scan = query(&dir);
assert_eq!(scan.recordings(), 1);
assert_eq!(scan.refused_count(), 1);
}
#[test]
fn subagents_are_not_counted_as_sessions() {
let dir = tmp("workflow");
write(
&dir,
"w.json",
r#"[{"run":"root","producer":"claude-code","events":[]},
{"run":"sub","parent":"root","producer":"claude-code","events":[]}]"#,
);
let scan = query(&dir);
assert_eq!(scan.recordings(), 2);
assert_eq!(scan.sessions_count(), 1);
assert_eq!(scan.subagent_count(), 1);
assert_eq!(scan.workflow_count(), 1);
assert_eq!(scan.producer_count("claude_code"), 2);
}
#[test]
fn a_subagent_in_another_file_still_links_to_its_parent() {
let dir = tmp("cross-file");
write(&dir, "a.json", r#"{"run":"root","events":[]}"#);
write(
&dir,
"b.json",
r#"{"run":"child","parent":"root","events":[]}"#,
);
let scan = query(&dir);
assert_eq!(scan.sessions_count(), 1);
assert_eq!(scan.subagent_count(), 1);
assert_eq!(scan.workflow_count(), 1);
}
#[test]
fn the_bound_counts_files_it_did_not_read() {
let dir = tmp("bounded");
for i in 0..5 {
write(&dir, &format!("{i}.json"), r#"{"run":"x","events":[]}"#);
}
let scan = scan_dir(
&dir,
Limits {
max_files: 2,
max_bytes: u64::MAX,
deadline: None,
},
);
assert_eq!(scan.files, 2);
assert_eq!(scan.files_truncated, 3);
assert!(!scan.is_complete());
}
#[test]
fn a_passed_deadline_reads_nothing_and_says_so() {
let dir = tmp("deadline");
write(&dir, "a.json", r#"{"run":"x","events":[]}"#);
let scan = scan_dir(
&dir,
Limits {
max_files: usize::MAX,
max_bytes: u64::MAX,
deadline: Some(Instant::now()),
},
);
assert_eq!(scan.files, 0);
assert_eq!(scan.files_truncated, 1);
}
#[test]
fn the_cache_reuses_a_summary_while_the_file_is_unchanged() {
let dir = tmp("cache-reuse");
write(&dir, "a.json", r#"{"run":"x","events":[]}"#);
let mut cache = cache::Cache::default();
let first = scan_with(&dir, UNBOUNDED, &mut cache);
assert_eq!(first.recordings(), 1);
let path = dir.join("a.json");
let mtime = std::fs::metadata(&path).unwrap().modified().unwrap();
let mut body = std::fs::read(&path).unwrap();
body[0] = b'!';
std::fs::write(&path, &body).unwrap();
let f = std::fs::File::options().write(true).open(&path).unwrap();
f.set_modified(mtime).unwrap();
drop(f);
let second = scan_with(&dir, UNBOUNDED, &mut cache);
assert_eq!(second.recordings(), 1, "the unchanged file was read again");
assert_eq!(second.corrupt_files, 0);
}
#[test]
fn a_changed_file_is_read_again() {
let dir = tmp("cache-changed");
write(&dir, "a.json", r#"{"run":"x","events":[]}"#);
let mut cache = cache::Cache::default();
assert_eq!(scan_with(&dir, UNBOUNDED, &mut cache).recordings(), 1);
write(
&dir,
"a.json",
r#"[{"run":"x","events":[]},{"run":"y","events":[]}]"#,
);
assert_eq!(scan_with(&dir, UNBOUNDED, &mut cache).recordings(), 2);
}
#[test]
fn counts_of_zero_recordings_are_distinguishable_from_absence() {
let dir = tmp("zero");
let scan = query(&dir);
assert_eq!(scan.recordings(), 0);
assert!(scan.dir_exists);
assert_eq!(scan.input_tokens(), None);
}
#[test]
fn relative_times_read_naturally() {
let now = Utc::now();
assert_eq!(relative(now, now), "just now");
assert_eq!(relative(now - chrono::Duration::minutes(3), now), "3m ago");
assert_eq!(relative(now - chrono::Duration::hours(2), now), "2h ago");
assert_eq!(relative(now + chrono::Duration::hours(5), now), "in 5h");
}
#[test]
fn producer_keys_match_the_id_form() {
assert_eq!(producer_key("claude-code"), "claude_code");
assert_eq!(producer_key("Gemini CLI"), "gemini_cli");
}
#[test]
fn keys_are_stable_and_distinguish_names() {
assert_eq!(key_of("a.json"), key_of("a.json"));
assert_ne!(key_of("a.json"), key_of("b.json"));
}
}