mod external_root;
use crate::durable_store::{SharedSession, lock as lock_session};
use jsrt::sync;
use serde_json::{Value, json};
use session_journal::Write;
use std::collections::HashMap;
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::Duration;
use usage::stats::{SESSION_STATS_MODULE, STATS_VERSION, cell_apis, classify_nudge, stats_lane};
const MAX_KEYS: usize = 200;
pub const DEFAULT_FLUSH: Duration = Duration::from_millis(500);
fn count(value: Option<&Value>) -> f64 {
match value.and_then(Value::as_f64) {
Some(n) if n.is_finite() && n >= 0.0 => n,
_ => 0.0,
}
}
fn empty_cells() -> Value {
json!({"count": 0, "failed": 0, "apis": {}})
}
fn empty_guard() -> Value {
json!({"checks": 0, "blocked": 0, "unchecked": 0, "afterChecks": 0, "afterFindings": 0, "ms": 0})
}
pub fn empty_session_stats(root: &str, now: u64) -> Value {
json!({
"version": STATS_VERSION,
"root": root,
"since": now,
"updatedAt": now,
"cells": {"root": empty_cells(), "subagents": empty_cells(), "other": empty_cells()},
"hostCalls": {},
"secretsMasked": 0,
"guards": {},
"nudges": {"toolRounds": 0, "wait": 0, "skill": 0},
"usageLimitBlocks": 0,
"merges": {},
"childModels": {},
"externalTurns": {"count": 0, "wallMs": 0},
})
}
pub fn parse_session_stats(saved: &Value) -> Option<Value> {
usage::stats::parse_session_stats(Some(saved)).map(|stats| stats.to_json())
}
pub enum Stored {
Fresh,
Document(Value),
Foreign,
}
pub fn read_stored(saved: Option<&Value>) -> Stored {
let Some(Value::Object(document)) = saved else {
return Stored::Fresh;
};
let mut document = document.clone();
match document.get("version") {
None => {
document.insert("version".into(), json!(STATS_VERSION));
}
Some(version) if version.as_f64() == Some(STATS_VERSION as f64) && version.is_number() => {}
Some(_) => return Stored::Foreign,
}
parse_session_stats(&Value::Object(document)).map_or(Stored::Fresh, Stored::Document)
}
pub trait StatsStore: Send + Sync {
fn read(&self) -> Result<Option<Value>, String>;
fn write(&self, document: &Value) -> Result<(), String>;
}
pub struct SessionStore(pub SharedSession);
impl StatsStore for SessionStore {
fn read(&self) -> Result<Option<Value>, String> {
Ok(lock_session(&self.0)
.value(session_journal::ns::MODULE, SESSION_STATS_MODULE)
.cloned())
}
fn write(&self, document: &Value) -> Result<(), String> {
lock_session(&self.0)
.commit(&[Write::set(
session_journal::ns::MODULE,
SESSION_STATS_MODULE,
document.clone(),
)])
.map(|_| ())
}
}
pub type Clock = Arc<dyn Fn() -> u64 + Send + Sync>;
pub struct GuardOutcome<'a> {
pub guard: &'a str,
pub phase: &'a str,
pub outcome: &'a str,
pub ms: f64,
}
type Change = Box<dyn Fn(&mut Value) + Send>;
struct State {
document: Value,
store: Option<Arc<dyn StatsStore>>,
early: Option<Vec<Change>>,
dirty: bool,
timer: bool,
closed: bool,
foreign: bool,
open_turns: HashMap<String, u64>,
}
struct Shared {
state: Mutex<State>,
wake: Condvar,
writes: Mutex<()>,
now: Clock,
flush_after: Duration,
}
pub struct SessionStats {
shared: Arc<Shared>,
}
impl Drop for SessionStats {
fn drop(&mut self) {
sync::lock(&self.shared.state).timer = false;
self.shared.wake.notify_all();
}
}
fn add(value: &mut Value, n: f64) {
*value = jsrt::json::number(count(Some(value)) + n);
}
fn keep(map: &mut Value, key: &str, value: &str) {
let Some(map) = map.as_object_mut() else {
return;
};
map.shift_remove(key);
map.insert(key.to_string(), json!(value));
let excess = map.len().saturating_sub(MAX_KEYS);
let old: Vec<String> = map.keys().take(excess).cloned().collect();
for key in old {
map.shift_remove(&key);
}
}
impl SessionStats {
pub fn new(root: &str, now: Clock, flush_after: Duration) -> SessionStats {
let document = empty_session_stats(root, now());
SessionStats {
shared: Arc::new(Shared {
state: Mutex::new(State {
document,
store: None,
early: Some(Vec::new()),
dirty: false,
timer: false,
closed: false,
foreign: false,
open_turns: HashMap::new(),
}),
wake: Condvar::new(),
writes: Mutex::new(()),
now,
flush_after,
}),
}
}
pub fn system(root: &str) -> SessionStats {
SessionStats::new(root, Arc::new(|| jsrt::date::now_ms() as u64), DEFAULT_FLUSH)
}
pub fn attach(&self, store: Arc<dyn StatsStore>) {
let read = store.read();
let mut state = sync::lock(&self.shared.state);
if state.store.is_some() || state.early.is_none() {
return;
}
match read.map(|saved| read_stored(saved.as_ref())) {
Ok(Stored::Document(mut stored)) => {
stored["root"] = state.document["root"].clone();
state.document = stored;
}
Ok(Stored::Foreign) | Err(_) => state.foreign = true,
Ok(Stored::Fresh) => {}
}
state.store = Some(store);
let early = state.early.take().unwrap_or_default();
let changed = !early.is_empty();
for change in &early {
change(&mut state.document);
}
if changed {
self.schedule(&mut state);
}
}
fn change(&self, change: impl Fn(&mut Value) + Send + 'static) {
let mut state = sync::lock(&self.shared.state);
if state.closed {
return;
}
if let Some(early) = &mut state.early {
early.push(Box::new(change));
return;
}
change(&mut state.document);
self.schedule(&mut state);
}
fn schedule(&self, state: &mut State) {
state.dirty = true;
state.document["updatedAt"] = json!((self.shared.now)());
if state.timer || state.closed {
return;
}
state.timer = true;
let shared = self.shared.clone();
std::thread::spawn(move || {
let state = sync::lock(&shared.state);
let (mut state, _) = shared
.wake
.wait_timeout_while(state, shared.flush_after, |s| s.timer)
.unwrap_or_else(PoisonError::into_inner);
if !state.timer {
return;
}
state.timer = false;
drop(state);
save(&shared);
});
}
pub fn touch(&self) {
self.change(|_| {});
}
pub fn cell(&self, lane: &str, code: &str, failed: bool, masked: usize) {
let (apis, cells) = (cell_apis(code), stats_lane(lane));
self.change(move |document| {
let cells = &mut document["cells"][cells];
add(&mut cells["count"], 1.0);
if failed {
add(&mut cells["failed"], 1.0);
}
for api in &apis {
let entry = &mut cells["apis"][*api];
add(entry, 1.0);
}
add(&mut document["secretsMasked"], masked as f64);
});
}
pub fn host_call(&self, kind: &str) {
let kind = kind.to_string();
self.change(move |document| {
let Some(calls) = document["hostCalls"].as_object_mut() else {
return;
};
if !calls.contains_key(&kind) && calls.len() >= MAX_KEYS {
return;
}
let entry = calls.entry(kind.clone()).or_insert(json!(0));
add(entry, 1.0);
});
}
pub fn guard(&self, record: &GuardOutcome<'_>) {
let (name, ms) = (record.guard.to_string(), record.ms);
let before = matches!(record.phase, "before_write" | "before_shell");
let (blocked, unchecked) = (record.outcome == "blocked", record.outcome == "unchecked");
let findings = matches!(record.outcome, "findings" | "unchecked");
self.change(move |document| {
let Some(guards) = document["guards"].as_object_mut() else {
return;
};
let guard = guards.entry(name.clone()).or_insert_with(empty_guard);
add(&mut guard["ms"], count(Some(&json!(ms))));
if before {
add(&mut guard["checks"], 1.0);
if blocked {
add(&mut guard["blocked"], 1.0);
}
if unchecked {
add(&mut guard["unchecked"], 1.0);
}
} else {
add(&mut guard["afterChecks"], 1.0);
if findings {
add(&mut guard["afterFindings"], 1.0);
}
}
});
}
pub fn nudge(&self, message: &str) {
let Some(kind) = classify_nudge(message) else {
return;
};
self.change(move |document| add(&mut document["nudges"][kind], 1.0));
}
pub fn usage_limit_block(&self) {
self.change(|document| add(&mut document["usageLimitBlocks"], 1.0));
}
pub fn merge(&self, task_id: &str, status: &str) {
let (task_id, status) = (task_id.to_string(), status.to_string());
self.change(move |document| keep(&mut document["merges"], &task_id, &status));
}
pub fn child_model(&self, task_id: &str, model: &str) {
let (task_id, model) = (task_id.to_string(), model.to_string());
self.change(move |document| keep(&mut document["childModels"], &task_id, &model));
}
pub fn external_turn_started(&self, turn: &str) {
{
let mut state = sync::lock(&self.shared.state);
if state.open_turns.contains_key(turn) {
return;
}
let now = (self.shared.now)();
state.open_turns.insert(turn.to_string(), now);
}
self.change(|document| add(&mut document["externalTurns"]["count"], 1.0));
}
pub fn external_turn_ended(&self, turn: &str) {
let elapsed = {
let mut state = sync::lock(&self.shared.state);
let Some(started) = state.open_turns.remove(turn) else {
return;
};
(self.shared.now)().saturating_sub(started)
};
self.change(move |document| add(&mut document["externalTurns"]["wallMs"], elapsed as f64));
}
pub fn snapshot(&self) -> Value {
let state = sync::lock(&self.shared.state);
let mut document = state.document.clone();
for change in state.early.iter().flatten() {
change(&mut document);
}
document
}
pub fn flush(&self) {
{
let mut state = sync::lock(&self.shared.state);
state.timer = false;
}
self.shared.wake.notify_all();
save(&self.shared);
}
pub fn close(&self) {
let open: Vec<String> = sync::lock(&self.shared.state).open_turns.keys().cloned().collect();
for turn in open {
self.external_turn_ended(&turn);
}
self.flush();
let mut state = sync::lock(&self.shared.state);
state.closed = true;
state.timer = false;
drop(state);
self.shared.wake.notify_all();
}
}
fn save(shared: &Shared) {
let _one_at_a_time = sync::lock(&shared.writes);
let (store, document) = {
let mut state = sync::lock(&shared.state);
let Some(store) = state.store.clone() else {
return;
};
if !state.dirty || state.foreign {
return;
}
state.dirty = false;
(store, state.document.clone())
};
if store.write(&document).is_err() {
sync::lock(&shared.state).dirty = true;
}
}